From 5fa010c9c29aa8536687e2e3caaa2c8293a983d6 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 31 Aug 2026 21:25:20 +1000 Subject: [PATCH] fix(plugin): add missing CLAUDE.md reference to plugin engine rule Test TestPluginEngineRuleIsCanonicalAndSharedByAgentInstructions was missing entirely, letting CLAUDE.md drift out of sync with ARCHITECTURE.md#plugin-engine-rule-strict. Added the test and the missing reference. --- .beads/issues.jsonl | 22 +- CLAUDE.md | 3 +- cmd/archied/main.go | 1072 +--------------- {cmd => internal/app}/archied/bootstrap.go | 2 +- .../app}/archied/bootstrap_nats_test.go | 2 +- {cmd => internal/app}/archied/chat_models.go | 2 +- .../app}/archied/chat_models_test.go | 2 +- .../app}/archied/chat_stream_test.go | 2 +- .../app}/archied/chat_turn_model.go | 2 +- {cmd => internal/app}/archied/codesearch.go | 10 +- .../app}/archied/codesearch_test.go | 20 +- .../app}/archied/config_update.go | 2 +- .../app}/archied/config_update_test.go | 2 +- .../archied/execution_architecture_test.go | 2 +- internal/app/archied/main.go | 1075 +++++++++++++++++ {cmd => internal/app}/archied/main_test.go | 2 +- {cmd => internal/app}/archied/memory_test.go | 2 +- .../app}/archied/model_catalog.go | 2 +- .../app}/archied/model_catalog_test.go | 2 +- .../app}/archied/provider_secrets.go | 2 +- {cmd => internal/app}/archied/reload.go | 2 +- {cmd => internal/app}/archied/reload_test.go | 2 +- .../app}/archied/store_wiring_test.go | 2 +- .../app}/archied/task_action_adapter_test.go | 2 +- .../app}/archied/task_actions_test.go | 2 +- .../archied/telegram_session_store_test.go | 2 +- .../app}/archied/telegram_setup.go | 2 +- .../archied/telegram_setup_regression_test.go | 2 +- .../app}/archied/telegram_setup_test.go | 2 +- {cmd => internal/app}/archied/toolsummary.go | 2 +- .../app}/archied/toolsummary_test.go | 2 +- internal/plugin/architecture_test.go | 14 + 32 files changed, 1148 insertions(+), 1118 deletions(-) rename {cmd => internal/app}/archied/bootstrap.go (99%) rename {cmd => internal/app}/archied/bootstrap_nats_test.go (99%) rename {cmd => internal/app}/archied/chat_models.go (99%) rename {cmd => internal/app}/archied/chat_models_test.go (99%) rename {cmd => internal/app}/archied/chat_stream_test.go (99%) rename {cmd => internal/app}/archied/chat_turn_model.go (99%) rename {cmd => internal/app}/archied/codesearch.go (93%) rename {cmd => internal/app}/archied/codesearch_test.go (81%) rename {cmd => internal/app}/archied/config_update.go (98%) rename {cmd => internal/app}/archied/config_update_test.go (98%) rename {cmd => internal/app}/archied/execution_architecture_test.go (97%) create mode 100644 internal/app/archied/main.go rename {cmd => internal/app}/archied/main_test.go (99%) rename {cmd => internal/app}/archied/memory_test.go (99%) rename {cmd => internal/app}/archied/model_catalog.go (99%) rename {cmd => internal/app}/archied/model_catalog_test.go (99%) rename {cmd => internal/app}/archied/provider_secrets.go (99%) rename {cmd => internal/app}/archied/reload.go (99%) rename {cmd => internal/app}/archied/reload_test.go (99%) rename {cmd => internal/app}/archied/store_wiring_test.go (98%) rename {cmd => internal/app}/archied/task_action_adapter_test.go (99%) rename {cmd => internal/app}/archied/task_actions_test.go (99%) rename {cmd => internal/app}/archied/telegram_session_store_test.go (98%) rename {cmd => internal/app}/archied/telegram_setup.go (99%) rename {cmd => internal/app}/archied/telegram_setup_regression_test.go (97%) rename {cmd => internal/app}/archied/telegram_setup_test.go (99%) rename {cmd => internal/app}/archied/toolsummary.go (98%) rename {cmd => internal/app}/archied/toolsummary_test.go (98%) diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index 5c4dd4e56..174e3188d 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -1,7 +1,7 @@ -{"_type":"issue","id":"archie-core-2tqh","title":"P0: Reconcile domain packages - docs and tree have diverged","description":"internal/domain/ contains curator/, mapping/, scheduling/ which do NOT appear in migration-decisions.md's confirmed domain list (identity, agent, workflow, messaging, workintake, plugin).\n\nMeanwhile, confirmed domains (identity, agent, messaging, plugin) don't exist yet as packages.\n\nNeed to:\n- Audit actual domain packages in internal/domain/\n- Update migration-decisions.md to reflect reality\n- Decide: are curator/, mapping/, scheduling/ legitimate domains or should they be folded into confirmed domains?\n- Create stub packages for missing confirmed domains if needed\n\nBlocked by: nothing (but blocks clean migration progress)","status":"open","priority":0,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:10Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:41:10Z","dependency_count":0,"dependent_count":1,"comment_count":0} +{"_type":"issue","id":"archie-core-2tqh","title":"P0: Reconcile domain packages - docs and tree have diverged","description":"internal/domain/ contains curator/, mapping/, scheduling/ which do NOT appear in migration-decisions.md's confirmed domain list (identity, agent, workflow, messaging, workintake, plugin).\n\nMeanwhile, confirmed domains (identity, agent, messaging, plugin) don't exist yet as packages.\n\nNeed to:\n- Audit actual domain packages in internal/domain/\n- Update migration-decisions.md to reflect reality\n- Decide: are curator/, mapping/, scheduling/ legitimate domains or should they be folded into confirmed domains?\n- Create stub packages for missing confirmed domains if needed\n\nBlocked by: nothing (but blocks clean migration progress)","status":"closed","priority":0,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:10Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:04:53Z","closed_at":"2026-08-31T10:04:53Z","close_reason":"Reconciled migration-decisions.md with actual tree: confirmed curator/mapping/scheduling as legitimate domains (were undocumented, not misplaced), documented that identity/agent/messaging/plugin are approved-but-unmaterialised targets rather than creating dead stub packages","dependency_count":0,"dependent_count":1,"comment_count":0} {"_type":"issue","id":"archie-core-kg1w","title":"P0: Refactor cmd/archied/main.go - violates organisation.md wiring rule","description":"cmd/archied/main.go is 1085 lines of substantive wiring, directly violating organisation.md's rule: 'cmd/* MUST NOT contain substantive wiring'.\n\nThis is the migration docs' own stated next chunk: internal/app/{archied,agentworker} (not yet started).\n\nNeed to:\n- Extract wiring logic from cmd/archied/main.go\n- Create internal/app/archied/ package\n- Create internal/app/agentworker/ package\n- Move dependency injection and lifecycle management into app layer\n- cmd/archied/main.go should only contain CLI flag parsing, OS signal traps, and entry point routing\n\nBlocked by: nothing (but is foundational for migration work)","status":"open","priority":0,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:00Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:41:00Z","dependency_count":0,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-bas7","title":"P0: Fix failing test - CLAUDE.md missing ARCHITECTURE.md reference","description":"TestPluginEngineRuleIsCanonicalAndSharedByAgentInstructions in internal/plugin test is failing because CLAUDE.md is missing a required reference to ARCHITECTURE.md#plugin-engine-rule-strict.\n\nThis is a red build - needs immediate doc fix.\n\nNeed to:\n- Add the missing reference to CLAUDE.md\n- Verify test passes\n\nBlocked by: nothing","status":"open","priority":0,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-28T13:40:53Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:40:53Z","dependency_count":0,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-1sjk","title":"P0: Bump Go from 1.26.5 to 1.26.6+ to fix 6 stdlib CVEs","description":"govulncheck found 6 CVEs in stdlib packages fixed in Go 1.26.6:\n- html/template\n- crypto/tls (handshake flood)\n- net/http (H2 timeout)\n- encoding/asn1 (recursion DoS)\n- x/net/idna\n\nNeed to:\n1. Update go.mod directive from 1.26.5 → 1.26.6\n2. Update both Dockerfiles (archied and archie-agent) from 1.26.5 → 1.26.6\n3. Rebuild and verify\n\nBlocked by: nothing","status":"open","priority":0,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:40:40Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:40:40Z","dependency_count":0,"dependent_count":1,"comment_count":0} +{"_type":"issue","id":"archie-core-bas7","title":"P0: Fix failing test - CLAUDE.md missing ARCHITECTURE.md reference","description":"TestPluginEngineRuleIsCanonicalAndSharedByAgentInstructions in internal/plugin test is failing because CLAUDE.md is missing a required reference to ARCHITECTURE.md#plugin-engine-rule-strict.\n\nThis is a red build - needs immediate doc fix.\n\nNeed to:\n- Add the missing reference to CLAUDE.md\n- Verify test passes\n\nBlocked by: nothing","status":"closed","priority":0,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-28T13:40:53Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:03:46Z","closed_at":"2026-08-31T10:03:46Z","close_reason":"Added missing test + CLAUDE.md reference to ARCHITECTURE.md#plugin-engine-rule-strict","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-1sjk","title":"P0: Bump Go from 1.26.5 to 1.26.6+ to fix 6 stdlib CVEs","description":"govulncheck found 6 CVEs in stdlib packages fixed in Go 1.26.6:\n- html/template\n- crypto/tls (handshake flood)\n- net/http (H2 timeout)\n- encoding/asn1 (recursion DoS)\n- x/net/idna\n\nNeed to:\n1. Update go.mod directive from 1.26.5 → 1.26.6\n2. Update both Dockerfiles (archied and archie-agent) from 1.26.5 → 1.26.6\n3. Rebuild and verify\n\nBlocked by: nothing","status":"closed","priority":0,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:40:40Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:04:10Z","closed_at":"2026-08-31T10:04:10Z","close_reason":"go.mod already at 1.27.0 (supersedes 1.26.6 CVE fix); bumped both Dockerfiles from golang:1.26-trixie to golang:1.27-trixie to match","dependency_count":0,"dependent_count":1,"comment_count":0} {"_type":"issue","id":"archie-core-n5sr","title":"Measure and reduce task-agent context amplification","description":"PR #635 consumed at least 1,286,449 tokens across baseline repair, analysis, repro, and fix, while the PR footer reported only 997,900. Do not impose a hard token stop: successful and failed tasks are evaluation evidence. Measure prompt/completion/cached usage by stage and iteration, include every agent stage in task totals, enable adaptive history compaction, and bound redundant tool-result replay so task completion remains the authority while efficiency becomes observable and improvable.","acceptance_criteria":"Task telemetry attributes prompt, completion, cached, total tokens and iterations by workflow stage; baseline-repair usage is included in task totals; task agents compact redundant accumulated history without imposing a token stop; successful and failed tasks remain comparable; regression coverage and quality gate pass.","notes":"Maintainer decision: max_tokens is not desired as a blocker. Optimize context economics and preserve outcomes as metrics.","status":"closed","priority":0,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-24T17:58:59Z","created_by":"Sam Catlow","updated_at":"2026-08-24T19:49:31Z","started_at":"2026-08-24T18:02:41Z","closed_at":"2026-08-24T19:48:33Z","close_reason":"Provider usage is durable by stage, baseline repair is included, model-adaptive compaction is enabled without a token cap, and ai-sdk preserves partial/compaction usage. task check and fresh adversarial review pass.","external_ref":"https://github.com/samcharles93/archie-core/issues/637","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-nq0c","title":"Preserve Git author identity in sandboxed task commits","description":"PR #635 contains three commits with empty author and committer names/emails despite configured bot_user and the documented GitHub noreply default. Trace and preserve commit identity across the daemon-worker boundary.","acceptance_criteria":"Sandboxed task commits carry configured/defaulted author and committer name/email; regression covers the live RPC/local commit composition; quality gate passes.","status":"closed","priority":0,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-24T17:58:58Z","created_by":"Sam Catlow","updated_at":"2026-08-24T19:49:32Z","closed_at":"2026-08-24T19:48:34Z","close_reason":"Commit identity now crosses the task boundary, per-identity email defaults are derived when omitted, and the worker end-to-end test verifies author and committer. task check and fresh adversarial review pass.","external_ref":"https://github.com/samcharles93/archie-core/issues/638","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-473l","title":"Reconcile task clone ownership after container commits","description":"Agent containers write Git objects as root into bind-mounted task clones, while ownership is currently repaired only before push. A stopped or failed workflow before push leaves unreadable objects and makes the next host-side worktree prepare fail instantly. Reconcile ownership after every CommitAll result as well as before push.","status":"closed","priority":0,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-24T17:24:57Z","created_by":"Sam Catlow","updated_at":"2026-08-24T19:49:33Z","started_at":"2026-08-24T17:25:30Z","closed_at":"2026-08-24T17:28:39Z","close_reason":"Implemented with regression coverage, full task check passing, and adversarial review clean.","external_ref":"https://github.com/samcharles93/archie-core/issues/639","dependency_count":0,"dependent_count":0,"comment_count":0} @@ -13,14 +13,14 @@ {"_type":"issue","id":"archie-core-1ysw.1","title":"Bind worktree RPC operations to the dispatched task","description":"The sandboxed agent can currently choose owner, repo, issue, base, and branch on worktree RPC requests. The daemon then performs host deletion and credentialed forge operations without correlating the request to the dispatched task or enforcing WorkDir containment.","acceptance_criteria":"Reject traversal and unconfigured or task-mismatched coordinates before any filesystem or forge side effect; derive or validate the permitted branch; regression tests fail on the old behavior and pass on the fix.","status":"closed","priority":0,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-22T11:01:43Z","created_by":"Sam Catlow","updated_at":"2026-08-24T06:59:58Z","started_at":"2026-08-22T11:02:03Z","closed_at":"2026-08-24T06:59:58Z","close_reason":"Fixed on main: worktreerpc now exposes only Push, authorized by an opaque per-dispatch grant issued from the dispatched *store.Task (worktreerpc.go:71-83). Sandbox input no longer selects host path, repo or branch; ValidCoordinates rejects traversal. Package tests pass.","external_ref":"https://github.com/samcharles93/archie-core/issues/589","metadata":null,"dependencies":[{"issue_id":"archie-core-1ysw.1","depends_on_id":"archie-core-1ysw","type":"parent-child","created_at":"2026-08-22T21:01:42Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-1ysw","title":"Repair internal/worktree correctness and trust-boundary defects","description":"Track the concrete worktree-package defects proven by the 2026-08-22 evidence-gated review. Child bugs cover RPC authority/path containment, repository-key isolation, retry/base/diff correctness, push lifecycle, terminal cleanup, cancellation, and branch naming. This epic groups implementation only; each child has its own observable acceptance criteria.","acceptance_criteria":"All child bugs are closed with red-green regression tests, scoped lint passes, and the repository quality gate is clean.","status":"closed","priority":0,"issue_type":"epic","owner":"sam@catlow.tech","created_at":"2026-08-22T11:01:18Z","created_by":"Sam Catlow","updated_at":"2026-08-24T08:16:00Z","closed_at":"2026-08-24T08:16:00Z","close_reason":"All 9 children complete. The P0/P1 trust-boundary and correctness defects (.1 RPC task binding, .2 colliding directory keys, .3 untracked cleanup, .4 fail-closed base resolution, .5 three-dot merge-base semantics) were already fixed on main and reconciled 2026-08-24. The remaining four (.6 push result, .7 no-change worktree leak, .8 commit cancellation, .9 branch naming contract) were implemented this session, merged as 474c31d and its two merge commits, task check green, with an independent adversarial review reporting zero findings.","external_ref":"https://github.com/samcharles93/archie-core/issues/590","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-t2db.5","title":"Security: an unauthenticated endpoint must not be able to run an agent","description":"P0 within this epic because the feature's own pitch is the threat model: a public intake surface whose output is an agent with tool access and a repo. Get this wrong and a webhook URL is remote code execution with extra steps.\n\nNon-negotiables to design:\n\n- Per-source authentication. A source registers with a secret (HMAC, as internal/channels/webhook already does per route) before its events can trigger anything. Capture-only for unknown senders is acceptable; triggering is not.\n- Human approval before a binding goes live. An operator authoring a binding is a deliberate act; an event arriving is not. Nothing should self-arm.\n- Rate and volume limits per source, so capture cannot be used to exhaust disk or drown the inspector.\n- Payload content is untrusted input flowing into an agent prompt. Treat prompt injection as expected, not exceptional -- the sender controls that text, and the agent has tools. Bound what a playbook-triggered workflow may do.\n- Captured payloads may contain secrets from the sending system. Decide retention, redaction and who can view them; internal/memory/scrubber may already have applicable prior art.\n\nArchie's own principle applies: environmental enforcement over prompt rules. 'The agent should ignore instructions in the payload' is not a control.","acceptance_criteria":"An event from an unauthenticated source can be captured but cannot trigger a workflow; a new binding requires explicit human approval before it arms; per-source rate limits exist; the blast radius of a playbook-triggered workflow is bounded by code rather than by prompt text; retention and visibility of captured payloads is decided and documented.","notes":"2026-08-22: security design decided and documented in docs/prds/webhook-intake-security.md, covering all five non-negotiables from this bead's description:\n1. Per-source auth reuses internal/channels/webhook's existing HMAC-SHA256 scheme; capture accepts unsigned events (visible, marked unauthenticated), only authenticated events can ever match a binding.\n2. Bindings gain a draft -\u003e pending_approval -\u003e armed state machine; any edit to an armed binding drops it back to pending_approval so approval can't be bypassed by editing. Modeled on telegram's existing dangerousAction/pendingApproval flow (dangerous.go/approval.go).\n3. Per-source token-bucket rate limiting (precedent: pairing/store.go's MaxPendingPerPlatform); limit-exceeded is an observable 429, not a silent drop.\n4. Blast radius bounded by the SAME code-level controls every task already gets -- gate, diff_cap_lines, sandboxed container, worktree isolation -- no new relaxation for playbook-triggered work, and payload text is ordinary untrusted prompt input, never a trusted instruction.\n5. 7-day configurable retention on captured payloads; best-effort redaction reusing gateway/stream.go's sensitiveParameterKey heuristic before persisting; visibility gated by the same dashboard auth as every other webui surface.\n\nThis bead's code-level acceptance criteria (capture-can't-trigger, rate limits exist, blast-radius bounded, approval gate) cannot be implemented yet: they constrain t2db.1 (capture), 7d5u.3 (intake family) and t2db.4 (binding), none of which have landed. This document is the contract those beads build against -- do not build any of them without satisfying it. Leaving this bead in_progress rather than closing it; it closes when t2db.1/7d5u.3/t2db.4 land and demonstrably satisfy points 1-5, not before.","status":"in_progress","priority":0,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-21T13:45:46Z","created_by":"Sam Catlow","updated_at":"2026-08-22T09:25:01Z","started_at":"2026-08-21T14:51:26Z","external_ref":"https://github.com/samcharles93/archie-core/issues/536","dependencies":[{"issue_id":"archie-core-t2db.5","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-08-21T23:45:45Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":1,"comment_count":0} -{"_type":"issue","id":"archie-core-d66k","title":"P1: Update ARCHITECTURE.md Package Map with missing packages","description":"ARCHITECTURE.md's Package Map omits sizeable load-bearing packages:\n- internal/gateway: 18.7k LOC\n- internal/tools: 20.9k LOC\n- internal/memory: 7k LOC\n- secret/*\n- plugin/*\n- skill/*\n\nA contributor or agent reading the map would miss where most of the code actually lives.\n\nNeed to:\n- Audit ARCHITECTURE.md Package Map section\n- Add all missing packages with their responsibilities\n- Ensure consistency with actual package structure\n- Verify LOC counts are accurate\n\nBlocked by: nothing","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:10Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:42:10Z","dependency_count":0,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-p3wt","title":"P1: Fix recvcheck - mixed pointer/value receivers","description":"recvcheck found mixed pointer/value receivers on:\n- Duration\n- TaskEnvelope\n- Provenance\n\nThis is a latent mutation-semantics bug - methods may not behave correctly if they expect to mutate the receiver.\n\nNeed to:\n- Audit all methods on Duration, TaskEnvelope, Provenance\n- Standardize to either all pointer receivers or all value receivers\n- Prefer pointer receivers for types that need mutation\n- Verify no behavior changes after fix\n\nBlocked by: nothing","status":"open","priority":1,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:07Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:42:07Z","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-d66k","title":"P1: Update ARCHITECTURE.md Package Map with missing packages","description":"ARCHITECTURE.md's Package Map omits sizeable load-bearing packages:\n- internal/gateway: 18.7k LOC\n- internal/tools: 20.9k LOC\n- internal/memory: 7k LOC\n- secret/*\n- plugin/*\n- skill/*\n\nA contributor or agent reading the map would miss where most of the code actually lives.\n\nNeed to:\n- Audit ARCHITECTURE.md Package Map section\n- Add all missing packages with their responsibilities\n- Ensure consistency with actual package structure\n- Verify LOC counts are accurate\n\nBlocked by: nothing","status":"closed","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:10Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:10:50Z","closed_at":"2026-08-31T10:10:50Z","close_reason":"Added internal/gateway, internal/tools, internal/memory, internal/secret, internal/plugin, internal/skill to ARCHITECTURE.md Package Map with LOC and role descriptions","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-p3wt","title":"P1: Fix recvcheck - mixed pointer/value receivers","description":"recvcheck found mixed pointer/value receivers on:\n- Duration\n- TaskEnvelope\n- Provenance\n\nThis is a latent mutation-semantics bug - methods may not behave correctly if they expect to mutate the receiver.\n\nNeed to:\n- Audit all methods on Duration, TaskEnvelope, Provenance\n- Standardize to either all pointer receivers or all value receivers\n- Prefer pointer receivers for types that need mutation\n- Verify no behavior changes after fix\n\nBlocked by: nothing","status":"closed","priority":1,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:07Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:09:59Z","closed_at":"2026-08-31T10:09:59Z","close_reason":"Duration and TaskEnvelope were already consistent by the time this was picked up; fixed the remaining recvcheck violation on Provenance (Paths/String moved to pointer receiver to match the mutating record() method). All Provenance uses are through addressable struct fields, so build/tests are unaffected. recvcheck now reports 0 issues.","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-agui","title":"P1: Fix 31 golangci-lint issues","description":"31 golangci-lint issues clustered as high cyclomatic/cognitive complexity in:\n- internal/tools/builtin/find.go\n- internal/tools/builtin/grep.go\n- internal/tools/builtin/shell.go\n- internal/gateway/go:dispatchLocal\n\nThese are real refactor candidates, not noise.\n\nNeed to:\n- Review each issue to understand the complexity\n- Refactor functions to reduce cyclomatic complexity\n- Break down large functions into smaller, focused ones\n- Re-run golangci-lint to verify fixes\n\nBlocked by: nothing","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:04Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:42:04Z","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-k7y7","title":"P1: Address coverage gaps in load-bearing packages","description":"Coverage gaps in critical packages:\n- domain/workintake: 14.3%\n- secret/enginehost: 0%\n- secret/secretextract: 10%\n- forge: 35.8%\n- natsrpc: 28.3%\n\nNeed to:\n- Analyze why coverage is low in these packages\n- Add tests for uncovered code paths\n- Prioritize based on risk/criticality\n- Aim for at least 60-70% coverage in load-bearing packages\n\nBlocked by: nothing","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:01Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:42:01Z","dependency_count":0,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-zq4l","title":"P1: Tighten CI go-version pinning","description":"CI's go-version is set to '1.26' (floating) while go.mod has exact 1.26.5. This loose coupling means CI could use 1.26.0-1.26.5 or newer minor versions, potentially missing bugs or compatibility issues.\n\nNeed to:\n- Pin CI go-version to exact match go.mod (currently 1.26.5, will be 1.26.6+ after P0 bump)\n- Consider using same variable/source of truth for both\n- Update CI configuration files\n\nBlocked by: Go version bump (archie-core-1sjk) for consistency","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:58Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:41:58Z","dependencies":[{"issue_id":"archie-core-zq4l","depends_on_id":"archie-core-1sjk","type":"blocks","created_at":"2026-08-28T13:42:31Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-0bvr","title":"P1: Add govulncheck step to CI/Taskfile","description":"The 6 stdlib CVEs went undetected through normal task check because there's no govulncheck step anywhere in CI or Taskfile.\n\nNeed to:\n- Add govulncheck to Taskfile.yml (e.g., task vuln)\n- Consider adding to CI pipeline\n- Decide: should vuln check be part of task check or separate?\n- Document the new task\n\nBlocked by: nothing","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:54Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:41:54Z","dependency_count":0,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-g1y3","title":"P1: Refactor internal/gateway/ - duplicate implementation risk with messaging domain","description":"internal/gateway/ contains session/compression/approval/branch logic which is the exact 'parallel gateway implementation' that migration-decisions.md says must be replaced by the not-yet-built messaging domain.\n\nThis is a live duplicate-implementation risk - gateway logic should migrate to domain/messaging.\n\nNeed to:\n- Analyze current gateway responsibilities\n- Create domain/messaging package structure\n- Migrate gateway logic incrementally to messaging domain\n- Update callers to use domain/messaging\n- Ensure no behavior changes during migration\n\nBlocked by: domain/messaging package not yet created","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:51Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:41:51Z","dependency_count":0,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-tnsr","title":"P1: Bump ai-sdk from v0.1.29 to v0.1.30","description":"github.com/samcharles93/ai-sdk is pinned at v0.1.29, one release behind (v0.1.30 pushed today).\n\nMissing features available in v0.1.30:\n- NewTypedTool\n- GenerateTypedObject\n- errx.ProviderError\n- RetryConfig.OnAttempt\n\nConcrete usage sites in internal/agentexec/loop_runner.go:\n- pluginToolSet: hand-writes JSON Schema + manual unmarshal\n- scriptToolSet: hand-writes JSON Schema + manual unmarshal\n\nThese are clean NewTypedTool[In] candidates once bumped.\n\nNeed to:\n- Update go.mod to require ai-sdk v0.1.30\n- Run go mod tidy\n- Refactor pluginToolSet and scriptToolSet to use NewTypedTool\n- Verify no breaking changes\n\nBlocked by: nothing","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:43Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:41:43Z","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-zq4l","title":"P1: Tighten CI go-version pinning","description":"CI's go-version is set to '1.26' (floating) while go.mod has exact 1.26.5. This loose coupling means CI could use 1.26.0-1.26.5 or newer minor versions, potentially missing bugs or compatibility issues.\n\nNeed to:\n- Pin CI go-version to exact match go.mod (currently 1.26.5, will be 1.26.6+ after P0 bump)\n- Consider using same variable/source of truth for both\n- Update CI configuration files\n\nBlocked by: Go version bump (archie-core-1sjk) for consistency","status":"closed","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:58Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:05:31Z","closed_at":"2026-08-31T10:05:31Z","close_reason":"Switched CI to actions/setup-go go-version-file: go.mod so CI's Go version is always sourced from go.mod directly, eliminating drift","dependencies":[{"issue_id":"archie-core-zq4l","depends_on_id":"archie-core-1sjk","type":"blocks","created_at":"2026-08-28T13:42:31Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-0bvr","title":"P1: Add govulncheck step to CI/Taskfile","description":"The 6 stdlib CVEs went undetected through normal task check because there's no govulncheck step anywhere in CI or Taskfile.\n\nNeed to:\n- Add govulncheck to Taskfile.yml (e.g., task vuln)\n- Consider adding to CI pipeline\n- Decide: should vuln check be part of task check or separate?\n- Document the new task\n\nBlocked by: nothing","status":"closed","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:54Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:05:53Z","closed_at":"2026-08-31T10:05:53Z","close_reason":"Added 'task vuln' (govulncheck) to Taskfile, deliberately kept out of task check since it needs network access to the vuln DB; wired as a separate CI step in deploy.yml","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-g1y3","title":"P1: Refactor internal/gateway/ - duplicate implementation risk with messaging domain","description":"internal/gateway/ contains session/compression/approval/branch logic which is the exact 'parallel gateway implementation' that migration-decisions.md says must be replaced by the not-yet-built messaging domain.\n\nThis is a live duplicate-implementation risk - gateway logic should migrate to domain/messaging.\n\nNeed to:\n- Analyze current gateway responsibilities\n- Create domain/messaging package structure\n- Migrate gateway logic incrementally to messaging domain\n- Update callers to use domain/messaging\n- Ensure no behavior changes during migration\n\nBlocked by: domain/messaging package not yet created","status":"closed","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:51Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:12:25Z","closed_at":"2026-08-31T10:12:25Z","close_reason":"Resolved migration-decisions.md's open Session/Messaging decision: keep internal/gateway as-is rather than risk a big-bang rewrite of live session/compression/approval/branch logic. Materialised internal/domain/messaging with the already-settled canonical Message/Conversation/ToolCall/ToolResult types (composite Conversation identity, immutable MessageID, branch lineage), with tests. Actual gateway migration onto these types is tracked as sequenced follow-up bd issues (d9dv, 4hof, o4e4, h3w2, label messaging-migration) so each channel lands with its own verification.","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-tnsr","title":"P1: Bump ai-sdk from v0.1.29 to v0.1.30","description":"github.com/samcharles93/ai-sdk is pinned at v0.1.29, one release behind (v0.1.30 pushed today).\n\nMissing features available in v0.1.30:\n- NewTypedTool\n- GenerateTypedObject\n- errx.ProviderError\n- RetryConfig.OnAttempt\n\nConcrete usage sites in internal/agentexec/loop_runner.go:\n- pluginToolSet: hand-writes JSON Schema + manual unmarshal\n- scriptToolSet: hand-writes JSON Schema + manual unmarshal\n\nThese are clean NewTypedTool[In] candidates once bumped.\n\nNeed to:\n- Update go.mod to require ai-sdk v0.1.30\n- Run go mod tidy\n- Refactor pluginToolSet and scriptToolSet to use NewTypedTool\n- Verify no breaking changes\n\nBlocked by: nothing","status":"closed","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:41:43Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:06:42Z","closed_at":"2026-08-31T10:06:42Z","close_reason":"go.mod already on ai-sdk v0.1.30. Refactored pluginToolSet and scriptToolSet in internal/agentexec/loop_runner.go to use NewTypedTool instead of hand-written JSON Schema + manual unmarshal; tests pass","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-z0u8","title":"Fix dashboard PR links to target the pull request","description":"The task dashboard Open PR action builds GitHub /pulls/\u003cnumber\u003e, which GitHub interprets as the pull-request listing and a numeric author filter. Link directly to /pull/\u003cnumber\u003e.","acceptance_criteria":"Open PR links use the forge's direct pull-request URL shape; a regression test fails against the plural route and passes after the fix; quality gate passes.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-24T17:57:39Z","created_by":"Sam Catlow","updated_at":"2026-08-24T19:49:35Z","started_at":"2026-08-24T17:57:46Z","closed_at":"2026-08-24T19:48:35Z","close_reason":"The API now supplies canonical per-task GitHub or Gitea URLs and the dashboard consumes them directly. Regression coverage, task check, and fresh adversarial review pass.","external_ref":"https://github.com/samcharles93/archie-core/issues/642","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-jy5m","title":"Make task timeline events operationally informative","description":"Task timelines currently render generic stage_start/stage_finish entries with no stage name or outcome, and task_retried entries add little beyond the event kind. Surface the existing event stage, workflow, detail, and relevant structured data so operators can understand what happened without opening raw logs.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-24T17:39:51Z","created_by":"Sam Catlow","updated_at":"2026-08-24T19:49:36Z","started_at":"2026-08-24T17:39:57Z","closed_at":"2026-08-24T17:55:34Z","close_reason":"Task timelines now render stage/retry context; baseline and gate-cycle logs expose useful bounded failure output. task check and independent review pass.","external_ref":"https://github.com/samcharles93/archie-core/issues/643","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-y1hv","title":"Relay dashboard update completion through Telegram","description":"Integrate the existing Carina-side behavior so dashboard-initiated updates can deliver their post-restart watchdog result to an authorized Telegram recipient, while retaining the web UI report path fallback.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-24T17:18:30Z","created_by":"Sam Catlow","updated_at":"2026-08-24T19:49:36Z","started_at":"2026-08-24T17:18:35Z","closed_at":"2026-08-24T17:19:42Z","close_reason":"Implemented, regression-tested, full task check passed, and adversarial review finding resolved.","external_ref":"https://github.com/samcharles93/archie-core/issues/644","dependency_count":0,"dependent_count":0,"comment_count":0} @@ -89,6 +89,10 @@ {"_type":"issue","id":"archie-core-g7tm","title":"telegram: finalize the reply through a live context, matching the error branch","description":"The success path calls live.finalize(turnCtx, reply) with the raw cancellable turn context (telegram.go:582), while the error branch two lines above deliberately uses context.WithoutCancel(turnCtx). If /stop lands during finalize, the rich edit, the plain-text retry, and the split-send fallback all abort with context.Canceled (go-telegram/bot builds requests with NewRequestWithContext), so the authoritative reply is never delivered and the live message keeps the ▌ cursor forever. The reply is persisted to the ledger, so the loss is delivery-level — but the user sees a frozen half-answer.\n\nFailure: a long tool-using turn is /stop-ped as it completes; turnCtx is cancelled mid-finalize; every send fails on the dead context; the chat shows the last live frame with a blinking cursor and no final answer.\n\nFound by code-review of PR #476 (feat/467, merged 98e8dda).","design":"live.finalize(context.WithoutCancel(turnCtx), reply) — the one-liner the error branch already uses. Consider making finalize take a live context internally rather than trusting each caller to remember.","acceptance_criteria":"A /stop landing mid-finalize still delivers the authoritative reply and drops the cursor; a test covers the cancellable-context finalize path.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-14T14:43:08Z","created_by":"Sam Catlow","updated_at":"2026-08-14T15:07:36Z","started_at":"2026-08-14T14:53:28Z","closed_at":"2026-08-14T15:03:28Z","close_reason":"Fixed in internal/channels/telegram/live.go and telegram.go: finalize/abandon strip context cancellation before sending (mirroring each other), ToolCall escapes Markdown metacharacters via bot.EscapeMarkdown, splitLongMessage splits on rune boundaries. All new/changed tests verified red-then-green; task check clean; adversarial review pass found no blocking issues.","external_ref":"https://github.com/samcharles93/archie-core/issues/484","labels":["code-review","streaming","telegram"],"dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-tb3a","title":"Fix streaming review blockers for inline tool calls","description":"Resolve the four evidence-gated findings in merged inline tool-call streaming: preserve the authoritative Telegram reply when final edits fail; keep Telegram network I/O off the model FullStream consumer and cancellable; correlate tool inputs with results and render a bounded redacted parameter summary; snapshot show_tool_calls per live reply to avoid reload races. Add regression tests for every path and run task check plus lint/race validation.","acceptance_criteria":"Regression tests fail on the merged implementation and pass after the fix. Final edit failure falls back to normal split delivery. Delta and ToolCall callbacks do not wait on Telegram HTTP and stop cancels in-flight rendering. Completed tool events include bounded redacted parameters correlated by tool-call ID. Reload cannot race an in-flight reply's show_tool_calls setting. task check passes, golangci-lint reports no new findings, and focused race tests pass.","notes":"Implemented in worktree /tmp/archie-core-tb3a on branch fix/archie-core-tb3a. Added cancellable async Telegram rendering, final edit failure fallback through normal split delivery, call-ID-correlated bounded conservative parameter summaries, atomic per-reply show_tool_calls snapshots, SSE/UI parameter rendering, rebuilt ui/dist, and regression tests. Validation: GOFLAGS=-buildvcs=false task check passed; golangci-lint --new-from-rev=HEAD ./... reported 0 issues; focused race suite passed; fresh adversarial reviewer reported PASS with zero findings. Plain task check is blocked in linked worktrees by Go VCS stamping exit 128, hence buildvcs override.\n2026-08-24 reconciliation: the close reason's 'worktree left uncommitted' is stale. The work did land on main via ba33527 (Merge pull request #477 from samcharles93/fix/archie-core-tb3a); verified on main: gateway/stream.go sensitiveParameterKey+redactToolParameters, telegram/live.go per-reply showToolCalls snapshot, webui/api_chat.go gating. The /tmp/archie-core-tb3a worktree and its branch are gone, as expected.\n","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T18:05:35Z","created_by":"Sam Catlow","updated_at":"2026-08-24T07:29:09Z","started_at":"2026-08-13T18:05:39Z","closed_at":"2026-08-13T18:35:26Z","close_reason":"Completed and independently reviewed; worktree left uncommitted per conservative repository policy.","external_ref":"https://github.com/samcharles93/archie-core/issues/485","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-dzx","title":"Remove duplicate archie-agent safe.directory setup","description":"## Problem\\ncmd/archie-agent/main.go configures git safe.directory inline and then calls markWorktreeSafe, so startup writes the same global git configuration twice.\\n\\n## Scope\\nKeep markWorktreeSafe as the single tested production path and remove the older inline exec.Command block/import. Add regression coverage proving startup wiring has exactly one authoritative safe-directory call.\\n\\n## Acceptance\\n- archie-agent startup invokes markWorktreeSafe exactly once\\n- no second inline safe.directory command remains in main.go\\n- cmd/archie-agent tests and repository quality gate pass","acceptance_criteria":"Single tested safe.directory setup path remains; duplicate inline command is removed; tests and task check pass","notes":"Red proof: TestMainConfiguresWorktreeSafetyOnce failed with 2 setup paths before implementation. Green proof: go test/vet/build ./cmd/archie-agent/... passed; golangci-lint --new-from-rev=HEAD reported 0 issues. Fresh adversarial review found no blockers. Full task check was not run because the working tree contains unrelated in-progress changes and task check rewrites files; scoped package gate used per repository protocol.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T16:10:09Z","created_by":"Sam Catlow","updated_at":"2026-08-13T16:13:48Z","started_at":"2026-08-13T16:10:24Z","closed_at":"2026-08-13T16:13:48Z","close_reason":"Removed duplicate inline safe.directory setup; markWorktreeSafe is now the single tested production path","external_ref":"https://github.com/samcharles93/archie-core/issues/470","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-h3w2","title":"Migrate email/webhook channels onto domain/messaging types","description":"Update remaining gateway-based channels (email, webhook) to use domain/messaging types, completing the migration described in migration-decisions.md section 2.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-31T10:12:16Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:12:16Z","labels":["messaging-migration"],"dependencies":[{"issue_id":"archie-core-h3w2","depends_on_id":"archie-core-d9dv","type":"blocks","created_at":"2026-08-31T10:12:24Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-o4e4","title":"Migrate web UI / dashboard chat onto domain/messaging types","description":"Update internal/webui/ chat handling (api_chat.go and friends) to use domain/messaging types once gateway session storage is migrated. Verify SSE streaming and chat history are unaffected.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-31T10:12:15Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:12:15Z","labels":["messaging-migration"],"dependencies":[{"issue_id":"archie-core-o4e4","depends_on_id":"archie-core-d9dv","type":"blocks","created_at":"2026-08-31T10:12:23Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-4hof","title":"Migrate Telegram channel onto domain/messaging types","description":"Once gateway session storage uses domain/messaging types, update internal/channels/telegram/ to read/write through them instead of internal/gateway.Message directly. Verify no behavior change in message history, branching, or tool-call rendering.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-31T10:12:13Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:12:13Z","labels":["messaging-migration"],"dependencies":[{"issue_id":"archie-core-4hof","depends_on_id":"archie-core-d9dv","type":"blocks","created_at":"2026-08-31T10:12:22Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-d9dv","title":"Migrate internal/gateway session storage onto domain/messaging types","description":"internal/domain/messaging now has the canonical Message/Conversation/ToolCall/ToolResult types (settled in migration-decisions.md section 2). internal/gateway/'s session storage still uses its own gateway.Message and ad hoc session state. Migrate gateway's session persistence to store/load domain/messaging.Message and Conversation, preserving exact current behavior (compression, approval, branch semantics unchanged).","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-31T10:12:12Z","created_by":"Sam Catlow","updated_at":"2026-08-31T10:12:12Z","labels":["messaging-migration"],"dependency_count":0,"dependent_count":3,"comment_count":0} {"_type":"issue","id":"archie-core-ul1m","title":"P2: Track migration progress - 85% of internal/ not in domain structure","description":"~85% of internal/ packages (29 of 36) haven't moved into the target domain//infrastructure/ structure yet. This is expected mid-migration, but worth tracking.\n\nNeed to:\n- Create a migration tracking document or update existing one\n- List all packages and their target locations\n- Track progress as packages are migrated\n- Identify dependencies between migrations\n\nBlocked by: P0 domain package reconciliation (archie-core-2tqh)","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:26Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:42:26Z","dependencies":[{"issue_id":"archie-core-ul1m","depends_on_id":"archie-core-2tqh","type":"blocks","created_at":"2026-08-28T13:42:37Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-kllw","title":"P2: Pin Docker latest tag for archie-agent","description":"Docker latest tag on ghcr.io/samcharles93/archie-agent is unpinned, which hurts reproducibility.\n\nNeed to:\n- Identify current stable version/commit hash\n- Update docker-compose.yml or deployment configs to use pinned tag\n- Consider using SHA-based tags for production deployments\n- Document the pinning strategy\n\nBlocked by: nothing","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:23Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:42:23Z","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-uyxk","title":"P2: Add lint/upgrade-deps Taskfile targets","description":"Dependency and lint maintenance is currently manual. There are no Taskfile targets for:\n- Running linters (golangci-lint)\n- Upgrading dependencies\n- Checking for outdated modules\n\nNeed to:\n- Add task lint target that runs golangci-lint\n- Add task deps:upgrade target for dependency updates\n- Add task deps:check target to list outdated modules\n- Ensure these integrate with existing task check flow\n\nBlocked by: nothing","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-08-28T13:42:20Z","created_by":"Sam Catlow","updated_at":"2026-08-28T13:42:20Z","dependency_count":0,"dependent_count":0,"comment_count":0} diff --git a/CLAUDE.md b/CLAUDE.md index 123026e40..20631174d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -28,7 +28,8 @@ requests for human review. See `ARCHITECTURE.md` for the package map, workflow engine design, task lifecycle, and key design decisions. Read it before making non-trivial changes. Key invariants: env-enforced gates, model never runs git, and agent execution -functions as a strict data boundary. +functions as a strict data boundary. Before adding a new plugin engine, satisfy +`ARCHITECTURE.md#plugin-engine-rule-strict`. ## Scope Discipline diff --git a/cmd/archied/main.go b/cmd/archied/main.go index e93b884e1..2fc150245 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -5,1081 +5,17 @@ package main import ( - "bytes" - "context" - "crypto/sha256" - "encoding/json" - "flag" - "fmt" - "log/slog" - "maps" - "net" - "net/http" - "net/http/httptest" "os" - "os/signal" - "path/filepath" - "strconv" - "strings" - "sync/atomic" - "syscall" - "github.com/moby/moby/client" - natsio "github.com/nats-io/nats.go" - "github.com/samcharles93/ai-sdk/chat" - "github.com/samcharles93/ai-sdk/core" - - "github.com/samcharles93/archie-core/internal/agentexec" - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/container" - "github.com/samcharles93/archie-core/internal/daemon" - "github.com/samcharles93/archie-core/internal/events" - "github.com/samcharles93/archie-core/internal/forge" - "github.com/samcharles93/archie-core/internal/forgerpc" - "github.com/samcharles93/archie-core/internal/gateway" - "github.com/samcharles93/archie-core/internal/infrastructure/configuration" - "github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay" - "github.com/samcharles93/archie-core/internal/logging" - "github.com/samcharles93/archie-core/internal/memory/builtin" - "github.com/samcharles93/archie-core/internal/plugin" - "github.com/samcharles93/archie-core/internal/secret" - "github.com/samcharles93/archie-core/internal/storage" - "github.com/samcharles93/archie-core/internal/store" - "github.com/samcharles93/archie-core/internal/storerpc" - "github.com/samcharles93/archie-core/internal/taskstate" - "github.com/samcharles93/archie-core/internal/tools" - "github.com/samcharles93/archie-core/internal/tools/mcp" - toolprovider "github.com/samcharles93/archie-core/internal/tools/provider" - mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp" - "github.com/samcharles93/archie-core/internal/webui" - "github.com/samcharles93/archie-core/internal/worktree" - "github.com/samcharles93/archie-core/internal/worktreerpc" + "github.com/samcharles93/archie-core/internal/app/archied" ) func main() { // The codesearch helper is dispatched before the daemon's flags are // parsed: it is this same binary re-invoked as a short-lived child by // internal/indexing, and it must not touch config, the store or NATS. - if args := os.Args[1:]; isCodesearchHelperArgs(args) { - os.Exit(runCodesearchHelper(args[1:], os.Stdout)) - } - os.Exit(run()) -} - -const ( - // defaultChatMaxSteps bounds the model/tool round-trips in one chat - // turn when [config.ChatConfig.MaxSteps] is unset. - defaultChatMaxSteps = 100 - packagedGatewayChangelogPath = "/usr/share/archie/CHANGELOG.archied.md" - packagedRuntimeChangelogPath = "/usr/share/archie/CHANGELOG.archie.md" -) - -// Component versions are injected from their independent release tags. -// Components without a release tag remain "dev" and never generate upgrade -// notifications. -var ( - gatewayVersion = "dev" - runtimeVersion = "dev" -) - -// chatGenerateOptions builds one chat turn's request. -func chatGenerateOptions( - ctx context.Context, - messages []chat.Message, - registry *tools.Registry, - maxSteps int, - limits agentexec.ToolLimits, - extra []tools.ToolEntry, -) (core.GenerateOptions, error) { - toolOpts := limits.Options() - if approval := gateway.ApprovalFromContext(ctx); approval != nil { - toolOpts.Approval = approval - } - toolSet, err := agentexec.BuildToolSet(registry, toolOpts) - if err != nil { - return core.GenerateOptions{}, err - } - extraSet, err := agentexec.BuildToolSetFrom(extra, toolOpts) - if err != nil { - return core.GenerateOptions{}, err - } - maps.Copy(toolSet, extraSet) - if maxSteps <= 0 { - maxSteps = defaultChatMaxSteps - } - return core.GenerateOptions{ - Messages: messages, - Tools: toolSet, - MaxSteps: maxSteps, - }, nil -} - -// isWithin reports whether target sits inside base. -func isWithin(base, target string) bool { - absBase, err := filepath.Abs(base) - if err != nil { - return false - } - absTarget, err := filepath.Abs(target) - if err != nil { - return false - } - rel, err := filepath.Rel(absBase, absTarget) - if err != nil { - return false - } - return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) -} - -// toolLimits reads the configured per-turn result limits. -func toolLimits(cfg config.Config) agentexec.ToolLimits { - return agentexec.ToolLimits{ - MaxResultChars: cfg.Tools.Policy.MaxResultChars, - SpillDir: cfg.Tools.Policy.SpillDir, - } -} - -// taskListOverRead multiplies the requested limit when reading rows that are -// filtered by identity afterwards. -const taskListOverRead = 5 - -// chatTaskListerAdapter gives the gateway a read view of one identity's tasks. -// gateway deliberately does not import internal/store, so the projection -// happens here, as it does for the writer and controller adapters below. -type chatTaskListerAdapter struct { - tasks func(context.Context, int) ([]store.Task, error) -} - -func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) { - // Over-read before filtering: Tasks applies its limit across the whole - // table, so asking for exactly `limit` would return fewer than that for - // this identity whenever another identity's work is more recent. - rows, err := a.tasks(ctx, limit*taskListOverRead) - if err != nil { - return nil, err - } - out := make([]gateway.ChatTaskSummary, 0, limit) - for _, task := range rows { - if task.Identity != identity { - continue - } - if len(out) >= limit { - break - } - out = append(out, gateway.ChatTaskSummary{ - ID: task.ID, - Repo: task.Owner + "/" + task.Repo, - Title: task.Title, - Status: task.Status, - Workflow: task.Workflow, - PRNumber: task.PRNumber, - }) - } - return out, nil -} - -// npmCacheServerEnv returns environment variables that make an -// npx-launched MCP server reuse a persistent package cache across daemon -// restarts, instead of npm re-resolving and re-downloading the package -// from the registry every time the daemon starts. Rooted under the -// daemon's own work dir so it persists regardless of whether the daemon -// itself runs in an ephemeral container. See mcp.NpmCacheEnv for the -// command-matching and env var details shared with archie-agent's -// per-task equivalent (internal/app/agentworker/mcp_providers.go), which -// points at a mounted cache volume instead of a work-dir subdirectory. -// -// workDir is unconditionally defaulted before registerTools runs -// (internal/infrastructure/configuration's applyGeneralDefaults), but -// filepath.Join silently accepts an empty string and would then put the -// cache in the daemon's current working directory instead of its -// persistent data directory -- guard it explicitly rather than trust that -// invariant here too, the way memoryProvider already does nearby. -func npmCacheServerEnv(command, workDir string) []string { - if workDir == "" { - return nil - } - return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache")) -} - -func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovider.Engine, error) { - name := strings.TrimSpace(server.Name) - if name == "" { - return nil, fmt.Errorf("MCP server name is required") - } - transportType := strings.ToLower(strings.TrimSpace(server.Transport)) - if transportType == "" { - transportType = "stdio" - } - - switch transportType { - case "stdio": - command := strings.TrimSpace(server.Command) - if command == "" { - return nil, fmt.Errorf("MCP stdio server %q requires a command", name) - } - transport := mcp.NewStdioTransport(mcp.StdioTransportConfig{ - Command: command, - Args: append([]string(nil), server.Args...), - Dir: server.WorkDir, - Env: npmCacheServerEnv(command, workDir), - }) - return mcptoolprovider.New(name, transport), nil - - case "http", "streamablehttp": - url := strings.TrimSpace(server.URL) - if url == "" { - return nil, fmt.Errorf("MCP http server %q requires a url", name) - } - transport := mcp.NewHTTPTransport(mcp.HTTPTransportConfig{ - Endpoint: url, - Headers: server.Headers, - }) - return mcptoolprovider.New(name, transport), nil - - case "sse": - sseEndpoint := strings.TrimSpace(server.SSEEndpoint) - if sseEndpoint == "" { - return nil, fmt.Errorf("MCP sse server %q requires an sse_endpoint", name) - } - transport := mcp.NewSSETransport(mcp.SSETransportConfig{ - SSEEndpoint: sseEndpoint, - MessageEndpoint: strings.TrimSpace(server.MessageEndpoint), - Headers: server.Headers, - }) - return mcptoolprovider.New(name, transport), nil - - default: - return nil, fmt.Errorf("MCP transport %q is not supported", transportType) - } -} - -// resolveForge builds the forge client for one forge configuration, returning -// the resolved token alongside it for the worktree manager. -// -// A missing or unusable credential disables the forge; it does not stop the -// daemon. The forge is one feature among many, and killing the process denies -// the operator chat, the gateway and every other subsystem over a capability -// they may not use at all -- under a systemd unit with Restart=on-failure that -// becomes a crash loop where the error scrolls past unread. A malformed -// configuration is a different matter and still fails fast in validation, well -// before this point. -func resolveForge(cfg config.Forge, secrets *secret.Registry, log *slog.Logger) (forge.Forge, string) { - if configuration.ForgeDisabled(cfg.Type) { - return forge.NewNoop(log), "" - } - token, err := cfg.Token.Resolve(secrets) - if err != nil || token == "" { - log.Warn("forge disabled: token unavailable", - "forge_type", cfg.Type, - "engine", cfg.Token.Engine, - "key", cfg.Token.Key, - "err", err) - return forge.NewNoop(log), "" - } - client, err := forge.New(cfg.Type, token, cfg.Host, log) - if err != nil { - log.Warn("forge disabled: client construction failed", - "forge_type", cfg.Type, "err", err) - return forge.NewNoop(log), "" - } - return client, token -} - -func openProductionTaskStore(ctx context.Context, path string) (store.TaskStore, error) { - return store.Open(ctx, path) -} - -func taskDBPath(configuredPath string) string { - return configuredPath + "-tasks.sqlite" -} - -// configDBPath resolves the runtime config overlay file. It is a sibling -// of the task and conversation stores (same configured path, own suffix) -// so each file owns its user_version and its migrator with no -// contention; recovery from a broken overlay is rm this file plus the -// --no-config-overlay boot flag. -func configDBPath(configuredPath string) string { - return configuredPath + "-config.sqlite" -} - -func manualRequeueTask(ctx context.Context, st store.TaskStore, taskID int64) error { - task, err := st.TaskByID(ctx, taskID) - if err != nil { - return err - } - if task == nil { - return fmt.Errorf("task %d not found", taskID) - } - switch task.Status { - case store.StatusParked, store.StatusWaitingHuman: - return st.Requeue(ctx, taskID, task.Status, "") - default: - return fmt.Errorf("task %d has status %q; only parked or waiting_human tasks can be requeued", taskID, task.Status) - } -} - -func run() int { - defaultCfg := filepath.Join(configHome(), "archie", "config.toml") - cfgPath := flag.String("config", defaultCfg, "path to a TOML/YAML config file or configuration directory") - overlayPath := flag.String("config-overlay", "", "path to a TOML/YAML overlay file or configuration directory applied on top of -config") - noConfigOverlay := flag.Bool("no-config-overlay", false, "skip the runtime config overlay (recovery hatch for the DB overlay; the -config-overlay file overlay still applies)") - once := flag.Bool("once", false, "run a single poll+process cycle and exit (systemd timer / testing)") - requeue := flag.Int64("requeue", 0, "requeue a parked/waiting task by id (keeps its workflow), then exit unless -once is also set") - showVersion := flag.Bool("version", false, "print the gateway and runtime versions and exit") - flag.Parse() - // The versions were only reachable through the chat /version command, - // which needs a running daemon and a configured channel. An update-check - // adapter has to answer "what is installed?" from a shell, so expose the - // same two values here. Machine-readable: one "name version" per line. - if *showVersion { - fmt.Printf("archied %s\narchie-agent %s\n", gatewayVersion, runtimeVersion) - return 0 - } - ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) - defer stop() - - b := newBootstrap() - if err := b.loadConfig(ctx, *cfgPath, *overlayPath, *noConfigOverlay); err != nil { - return 1 - } - defer b.cleanup() - - if err := b.openStores(ctx); err != nil { - return 1 - } - if exit, err := b.handleRequeue(ctx, *requeue, *once); err != nil { - return 1 - } else if exit { - return 0 - } - b.loadCatalog(ctx, *cfgPath) - b.setupObservability() - - if err := b.setupBackends(ctx); err != nil { - return 1 - } - - b.setupLLMAndChat(ctx) - if !b.setupGateways(ctx, *cfgPath, *overlayPath) { - return 1 - } - - if err := b.loadWorkflows(ctx); err != nil { - return 1 - } - if err := b.loadPlugins(); err != nil { - return 1 - } - if err := b.buildTreesAndIdentities(ctx); err != nil { - return 1 - } - - if err := b.registerNATSRPC(); err != nil { - return 1 - } - if err := b.setupMemory(); err != nil { - return 1 - } - b.setupCurators(ctx) - - b.setupGuardrails() - if err := b.registerTools(); err != nil { - return 1 - } - b.registerStandaloneTools() - b.buildDaemon() - b.wireConfigPublishing(ctx, *cfgPath, *overlayPath) - b.installUpdateConfigHandler() - b.installConfigHandlers(*cfgPath, *overlayPath) - - if err := b.startServices(ctx); err != nil { - return 1 - } - return exitCode(b.runLoop(ctx, *once)) -} - -// bootConfigOverlay layers the runtime config overlay over the resolved -// file config. Every failure degrades rather than aborts -- the daemon -// boots on file config alone and the reason is recorded in bootOverlayErr, -// which /api/config surfaces as the reload status. The returned store is -// nil only when Open failed; a store that opened but could not be read -// stays open and non-nil so the dashboard PATCH path and reload wiring -// keep exactly the behaviour of the original inline block. -func bootConfigOverlay( - ctx context.Context, - loader *configuration.Loader, - doc *configuration.Document, - bootOverlayErr *atomic.Pointer[string], - log *slog.Logger, -) (*overlay.Store, *configuration.Document) { - store, err := overlay.Open(ctx, configDBPath(doc.Config.DBPath)) - if err != nil { - recordBootOverlayError(bootOverlayErr, log, err, - "config overlay unavailable; booting on file config alone", - "path", configDBPath(doc.Config.DBPath)) - return nil, doc - } - overrides, err := store.Snapshot(ctx) - if err != nil { - recordBootOverlayError(bootOverlayErr, log, err, - "config overlay unreadable; booting on file config alone") - return store, doc - } - if len(overrides) == 0 { - return store, doc - } - applied, err := loader.ApplyOverlay(doc, overrides) - if err != nil { - recordBootOverlayError(bootOverlayErr, log, err, - "config overlay rejected by validation; booting on file config alone") - return store, doc - } - return store, applied -} - -// recordBootOverlayError stores the degrade reason for /api/config and -// logs it, so the operator sees both where they are looking. -func recordBootOverlayError(p *atomic.Pointer[string], log *slog.Logger, err error, msg string, args ...any) { - reason := err.Error() - p.Store(&reason) - args = append(args, "err", err) - log.Error(msg, args...) -} - -func persistAndBroadcastEvents(sink *events.Sub, st store.TaskStore, web *webui.Server, log *slog.Logger) { - for e := range sink.C { - if e.ID == 0 { - id, err := st.InsertEvent(context.Background(), e) - if err != nil { - log.Error("event sink insert failed", "err", err) - continue - } - e.ID = id - } - web.Broadcast(e) - } -} - -// curatorEventSink adapts the in-process event bus to the curator family's -// narrow emission contract. Bus publish is non-blocking with bounded, -// dropping per-subscriber buffers, so curator activity can never -// backpressure the daemon or a chat turn. -type curatorEventSink struct { - b *events.Bus -} - -func (s curatorEventSink) Emit(kind, detail string, data map[string]any) { - s.b.Publish(events.Event{Kind: kind, Detail: detail, Data: data}) -} - -type chatTaskWriterAdapter struct { - enqueue func( - ctx context.Context, - owner, repo, title, body, workflow, identity string, - ) (*store.Task, error) -} - -func (a chatTaskWriterAdapter) EnqueueChatTask( - ctx context.Context, - owner, repo, title, body, workflow, identity string, -) (int64, error) { - task, err := a.enqueue(ctx, owner, repo, title, body, workflow, identity) - if err != nil { - return 0, err - } - if task == nil { - return 0, fmt.Errorf("enqueue chat task returned no task") - } - return task.ID, nil -} - -type chatTaskControllerAdapter struct { - taskByID func(context.Context, int64) (*store.Task, error) - requeue func(context.Context, int64, string, string) error - transition func(context.Context, int64, string, string, string) error -} - -func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID int64) (gateway.ChatTaskStatus, bool, error) { - task, err := a.taskByID(ctx, taskID) - if err != nil { - return gateway.ChatTaskStatus{}, false, err - } - if task == nil { - return gateway.ChatTaskStatus{}, false, nil - } - if !task.IsForgeBacked() { - return gateway.ChatTaskStatus{Status: task.Status, Identity: task.Identity}, true, nil - } - return gateway.ChatTaskStatus{}, false, fmt.Errorf("task %d is not chat-originated", taskID) -} - -func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64) error { - return a.requeue(ctx, taskID, store.StatusWaitingHuman, "implement") -} - -func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error { - task, err := a.taskByID(ctx, taskID) - if err != nil { - return err - } - if task == nil { - return fmt.Errorf("task %d not found", taskID) - } - if task.IsForgeBacked() { - return fmt.Errorf("task %d is not chat-originated", taskID) - } - // An operator declining work lands in the same state whichever surface - // they used. This used to record StatusRejected while the dashboard - // recorded StatusClosedWontDo, so the same decision showed up as two - // different states -- and StatusRejected, which the PR reconciler uses - // for "the pull request was closed without merging", stopped meaning one - // thing. - return a.transition(ctx, taskID, task.Status, store.StatusClosedWontDo, reason) -} - -// chatTaskLogReaderAdapter gives the gateway a read view of a task's -// persisted log history without importing internal/logging or internal/store -// into the gateway package. Each identity's reader is scoped to its own -// tasks: the identity bound at construction is used for authorization, so a -// model cannot read another identity's task logs by passing a different -// identity through the tool input. -type chatTaskLogReaderAdapter struct { - tasks func(context.Context, int64) (*store.Task, error) - taskLogs *logging.TaskRegistry -} - -func (a chatTaskLogReaderAdapter) ReadChatTaskLogs( - ctx context.Context, identity string, taskID int64, attempt int, q gateway.ChatTaskLogQuery, -) (gateway.ChatTaskLogResult, error) { - task, err := a.tasks(ctx, taskID) - if err != nil { - return gateway.ChatTaskLogResult{}, err - } - if task == nil { - return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID) - } - // Match the filter chatTaskListerAdapter already applies: a model - // bound to one identity must not read logs for another identity's - // tasks, and tasks with no identity (forge-sourced) are not - // readable through a chat tool at all — they belong to the daemon, - // not a particular identity. "The empty string MUST NOT retain - // special meaning" (docs/architecture/identity.md). - if task.Identity != identity { - return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity) - } - - if attempt <= 0 { - attempt = task.Attempt - } - path := a.taskLogs.Path(taskID, attempt) - if path == "" { - return gateway.ChatTaskLogResult{Attempt: attempt, Entries: []gateway.ChatTaskLogEntry{}}, nil - } - - page, err := logging.Page(path, logging.Query{ - Component: q.Component, - Contains: q.Contains, - Levels: q.Levels, - Since: q.Since, - Until: q.Until, - Limit: q.Limit, - }, q.AfterID) - if err != nil { - return gateway.ChatTaskLogResult{}, err - } - - entries := make([]gateway.ChatTaskLogEntry, len(page.Entries)) - for i, e := range page.Entries { - entries[i] = gateway.ChatTaskLogEntry{ - Time: e.Time, - Level: e.Level, - Message: e.Message, - Fields: e.Fields, - } - } - return gateway.ChatTaskLogResult{ - Entries: entries, - Attempt: attempt, - Truncated: page.Truncated, - Cursor: page.Cursor, - MoreAvailable: page.MoreAvailable, - }, nil -} - -// chatTaskActorAdapter executes operator actions on a task via the same -// action path the dashboard uses, ensuring chat and webui cannot diverge. -// It enforces identity scoping so an identity cannot mutate another -// identity's tasks. -type chatTaskActorAdapter struct { - tasks func(context.Context, int64) (*store.Task, error) - handler http.Handler - token func() string -} - -func (a chatTaskActorAdapter) ApplyChatTaskAction( - ctx context.Context, identity string, taskID int64, action taskstate.Action, -) (gateway.TaskActionResult, error) { - if a.tasks == nil || a.handler == nil { - return gateway.TaskActionResult{}, fmt.Errorf("task actions are not configured") - } - task, err := a.tasks(ctx, taskID) - if err != nil { - return gateway.TaskActionResult{}, err - } - if task == nil { - return gateway.TaskActionResult{}, fmt.Errorf("task %d not found", taskID) - } - // Match the filter chatTaskListerAdapter and chatTaskLogReaderAdapter apply: - // a model bound to one identity must not mutate another identity's tasks, - // and tasks with no identity (forge-sourced) are not actionable through a - // chat tool at all — they belong to the daemon, not a particular identity. - if task.Identity != identity { - return gateway.TaskActionResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity) - } - if err := taskstate.CheckAction(task.Status, action); err != nil { - return gateway.TaskActionResult{}, err - } - - body, err := json.Marshal(map[string]any{"action": string(action)}) - if err != nil { - return gateway.TaskActionResult{}, fmt.Errorf("marshal action request: %w", err) - } - req := httptest.NewRequestWithContext( - ctx, http.MethodPost, - fmt.Sprintf("/api/tasks/%d/action", taskID), - bytes.NewReader(body), - ) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("X-Archie-CSRF", "1") - if a.token != nil { - if tok := a.token(); tok != "" { - req.Header.Set("Authorization", "Bearer "+tok) - } - } - rec := httptest.NewRecorder() - a.handler.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - errMsg := strings.TrimSpace(rec.Body.String()) - if errMsg == "" { - errMsg = fmt.Sprintf("HTTP %d", rec.Code) - } - return gateway.TaskActionResult{}, fmt.Errorf("action %s on task %d failed: %s", action, taskID, errMsg) - } - - return gateway.TaskActionResult{ - TaskID: taskID, - Action: string(action), - Message: fmt.Sprintf("Applied %s to task %d.", action, taskID), - }, nil -} - -func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) { - if len(cfg.Identities) > 0 { - profiles := make([]gateway.TaskProfile, 0, len(cfg.Identities)) - for _, identity := range cfg.Identities { - if len(identity.Repos) == 0 { - continue - } - profiles = append(profiles, newChatTaskProfile(identity.Name, identity.Repos)) - } - if len(profiles) == 0 { - return nil, "" - } - return profiles, profiles[0].Identity - } - if len(cfg.Repos) == 0 { - return nil, "" - } - return []gateway.TaskProfile{newChatTaskProfile("", cfg.Repos)}, "" -} - -func newChatTaskProfile(identity string, repos []config.Repo) gateway.TaskProfile { - allowed := make([]string, 0, len(repos)) - for _, repo := range repos { - allowed = append(allowed, repo.Owner+"/"+repo.Name) - } - return gateway.TaskProfile{ - Identity: identity, - DefaultOwner: repos[0].Owner, - DefaultRepo: repos[0].Name, - Repos: allowed, - } -} - -func configureTaskCommands( - router *gateway.Router, - tasks gateway.TaskCreator, - controller gateway.TaskController, - identity string, -) { - router.Tasks = tasks - router.Controller = controller - router.Identity = identity -} - -// parseListenAddr splits "host:port" into components, using defaults -// when the input is empty or missing a part. -func parseListenAddr(addr, defaultHost string, defaultPort int) (string, int) { - if addr == "" { - return defaultHost, defaultPort - } - host, portStr, err := net.SplitHostPort(addr) - if err != nil { - return defaultHost, defaultPort - } - port, err := strconv.Atoi(portStr) - if err != nil || port <= 0 { - return host, defaultPort - } - return host, port -} - -// sessionKey builds a deterministic session identifier from a gateway -// message's routing fields. Platform + channel + thread uniquely identify -// a conversation for session persistence and history retrieval. -func executionProviders(cfg config.Config) map[string]agentexec.Provider { - return agentexec.ProvidersFromConfig(cfg.Providers) -} - -// subscribeSystemLogs subscribes to every task's system log subject at once. -// A sandboxed container's own stderr disappears at AutoRemove; archie-agent -// ships it here instead (agentexec.SubjectForSystem), and this is where it -// lands: one wildcard subscription demuxed by taskID and routed through -// taskLogs, which is opened for a task's duration in Daemon.process, so a -// message for a task not currently open there -- late, duplicate, or from -// an attempt this instance never dispatched -- is expected and silently -// dropped rather than treated as an error. -func subscribeSystemLogs(nc *natsio.Conn, taskLogs *logging.TaskRegistry, log *slog.Logger) (unsubscribe func(), err error) { - sub, err := nc.Subscribe(agentexec.SubjectSystemWildcard, func(msg *natsio.Msg) { - taskID, ok := agentexec.TaskIDFromSystemSubject(msg.Subject) - if !ok { - log.Warn("system log message on unparseable subject", "subject", msg.Subject) - return - } - var entry logging.Entry - if err := json.Unmarshal(msg.Data, &entry); err != nil { - log.Warn("system log message undecodable", "task", taskID, "err", err) - return - } - taskLogs.Write(taskID, entry) - }) - if err != nil { - return nil, err - } - return func() { - if err := sub.Unsubscribe(); err != nil { - log.Warn("system log unsubscribe failed", "err", err) - } - }, nil -} - -// subscribeAgentEvents subscribes to every task's events subject at once and -// republishes each decoded event on bus. An archie-agent worker's -// agentexec.ForwardTaskEvents ships a task's workflow events (stage -// progress, outcome, parking) here because that worker's own *events.Bus is -// in-process and invisible to the daemon; this is the other half of the -// bridge, landing them on bus so persistAndBroadcastEvents -- the single -// choke point that inserts into the SQLite events table and fans out over -// SSE -- treats them exactly like every other daemon-observed event. Mirrors -// subscribeSystemLogs's demux-by-taskID shape. -func subscribeAgentEvents(nc *natsio.Conn, bus *events.Bus, log *slog.Logger) (unsubscribe func(), err error) { - sub, err := nc.Subscribe(agentexec.SubjectEventsWildcard, func(msg *natsio.Msg) { - taskID, ok := agentexec.TaskIDFromEventsSubject(msg.Subject) - if !ok { - log.Warn("task event message on unparseable subject", "subject", msg.Subject) - return - } - var e events.Event - if err := json.Unmarshal(msg.Data, &e); err != nil { - log.Warn("task event message undecodable", "task", taskID, "err", err) - return - } - bus.Publish(e) - }) - if err != nil { - return nil, err - } - if err := nc.Flush(); err != nil { - if cleanupErr := sub.Unsubscribe(); cleanupErr != nil { - log.Warn("task event subscription cleanup failed after flush failure", "err", cleanupErr) - } - return nil, fmt.Errorf("flush task event subscription: %w", err) - } - return func() { - if err := sub.Unsubscribe(); err != nil { - log.Warn("task event unsubscribe failed", "err", err) - } - }, nil -} - -// registerTaskRPCServers subscribes the storerpc/forgerpc/worktreerpc -// handlers on nc so an archie-agent container (which holds no DB -// connection, forge token, or push credential) can proxy those operations -// back to archied. The returned func unsubscribes all of them. -// -// The store is shared across identities, so storerpc registers once on its -// root subjects. Forge and worktree are identity-scoped: the root servers -// answer the root (identity-less) subjects for single-identity deployments -// and root-owned tasks, and one server pair per identity answers that -// identity's scoped subjects so a container-mode task owned by a non-root -// identity has its RPC calls served by its own forge client and worktree -// manager. -func registerTaskRPCServers(nc *natsio.Conn, st store.TaskStore, forgeClient forge.Forge, trees *worktree.Manager, identities []*daemon.IdentityRunner, grants *worktreerpc.Grants, log *slog.Logger) (unsubscribe func(), err error) { - unsubs := make([]func(), 0, 3+2*len(identities)) - unsubAll := func() { - for _, u := range unsubs { - u() - } - } - - storeServer := &storerpc.Server{Store: st, Log: log} - u, err := storeServer.Register(nc) - if err != nil { - return nil, fmt.Errorf("register storerpc: %w", err) - } - unsubs = append(unsubs, u) - - registerForge := func(fg forge.Forge, identity string) error { - srv := &forgerpc.Server{Forge: fg, Log: log.With("rpc_identity", identity)} - u, err := srv.RegisterFor(nc, identity) - if err != nil { - return fmt.Errorf("register forgerpc%s: %w", identitySuffix(identity), err) - } - unsubs = append(unsubs, u) - return nil - } - registerTrees := func(mgr *worktree.Manager, identity string) error { - srv := &worktreerpc.Server{Trees: mgr, Grants: grants, Log: log.With("rpc_identity", identity)} - u, err := srv.RegisterFor(nc, identity) - if err != nil { - return fmt.Errorf("register worktreerpc%s: %w", identitySuffix(identity), err) - } - unsubs = append(unsubs, u) - return nil - } - - // Root (identity-less) forge and worktree servers. - if err := registerForge(forgeClient, ""); err != nil { - unsubAll() - return nil, err - } - if err := registerTrees(trees, ""); err != nil { - unsubAll() - return nil, err - } - - // One forge/worktree server pair per identity, on identity-scoped - // subjects. - for _, id := range identities { - if err := registerForge(id.Forge, id.Name); err != nil { - unsubAll() - return nil, err - } - if err := registerTrees(id.Trees, id.Name); err != nil { - unsubAll() - return nil, err - } - } - - return unsubAll, nil -} - -// identitySuffix renders a log suffix for an identity-scoped registration, -// empty for the root set. -func identitySuffix(identity string) string { - if identity == "" { - return "" - } - return " (" + identity + ")" -} - -func configuredNATSToken(cfg config.NATSConfig, getenv func(string) string) (string, error) { - if cfg.TokenEnv == "" { - return "", nil - } - token := getenv(cfg.TokenEnv) - if token == "" { - return "", fmt.Errorf("%s is required when nats.token_env is configured", cfg.TokenEnv) - } - return token, nil -} - -func configHome() string { - if x := os.Getenv("XDG_CONFIG_HOME"); x != "" { - return x - } - home, _ := os.UserHomeDir() - return filepath.Join(home, ".config") -} - -func releaseAnnouncementStatePath(workDir, identity string) string { - identityHash := sha256.Sum256([]byte(identity)) - return filepath.Join( - workDir, - fmt.Sprintf("release-announcements-%x.json", identityHash[:8]), - ) -} - -// updateReportPath is where the update watchdog leaves the phase-2 outcome -// of an update for this identity to relay on its next launch. Hashed the -// same way as releaseAnnouncementStatePath so multiple identities sharing -// one daemon (see docs/architecture/identity.md) never collide. -func updateReportPath(workDir, identity string) string { - identityHash := sha256.Sum256([]byte(identity)) - return filepath.Join( - workDir, - fmt.Sprintf("update-report-%x.json", identityHash[:8]), - ) -} - -// safePluginInfo calls Name() and Version() on a plugin, recovering from -// panics. A panicking plugin must not crash the daemon at startup. -func safePluginInfo(p plugin.Plugin) (name, version string) { - defer func() { - if r := recover(); r != nil { - name = "(panic)" - version = "(panic)" - } - }() - return p.Name(), p.Version() -} - -// webTokenFor returns the dashboard token for a listen address, or "" when -// none is needed. -// -// A loopback bind needs no token: local access already implies the agent's own -// authority, since its shell and edit tools run as this user. A reachable bind -// mints one and logs a URL to click, so an instance cannot be left exposed -// merely by omitting configuration. -func webTokenFor(listen, dbPath string, log *slog.Logger) string { - if webui.IsLoopback(listen) { - return "" - } - path := filepath.Join(filepath.Dir(dbPath), "web-token") - tok, err := webui.LoadOrCreateToken(path) - if err != nil { - log.Error("web ui token", "err", err) - return "" - } - log.Info("web ui token", "open", webui.DashboardURL(listen, tok), "stored", path) - return tok -} - -// memoryProvider builds the built-in memory provider, falling back to the -// default work directory when the configured one cannot be established. -// -// A bad path warns and degrades rather than stopping the daemon, in line with -// the forge and container paths: memory is one capability among many. The -// fallback is the location an unset work_dir would have produced, so an -// operator who copied a config from another host (a container path such as -// /var/lib/archie/work onto a laptop, say) lands somewhere predictable -// instead of somewhere only this function knows about. -// -// Returns the provider and the directory actually in use. A nil provider -// means the configuration is unusable in a way no fallback can fix. -func memoryProvider(workDir string, log *slog.Logger) (*builtin.Provider, string) { - // An empty workDir would make filepath.Join produce the relative path - // "memory", which MkdirAll creates in whatever directory the daemon - // happens to be started from -- succeeding, and putting memory somewhere - // nobody will look. Defaulting should have prevented an empty workDir; - // treat it as unset rather than trusting it. - var dir string - var p *builtin.Provider - if workDir == "" { - log.Warn("no work directory configured, using the default for memory") - } else { - dir = filepath.Join(workDir, "memory") - var err error - p, err = builtin.New(builtin.Config{Dir: dir}) - if err != nil { - log.Warn("memory directory rejected, falling back to the default", "dir", dir, "err", err) - p = nil - } - if p != nil && p.IsAvailable() { - return p, dir - } - if p != nil { - log.Warn("memory directory unusable, falling back to the default", - "dir", dir, "err", p.Err()) - } - } - - fallback := filepath.Join(configuration.DefaultWorkDir(), "memory") - if fallback == dir { - // Already the default: there is nowhere else to try, so run degraded - // and say what that costs rather than pretending memory works. - log.Error("memory unavailable: the agent will start every conversation "+ - "with no recollection of earlier ones, and memory writes will fail", - "dir", dir) - return p, dir - } - - fb, err := builtin.New(builtin.Config{Dir: fallback}) - if err != nil { - log.Error("memory provider init failed at the default directory", "dir", fallback, "err", err) - return p, dir - } - if !fb.IsAvailable() { - log.Error("memory unavailable: neither the configured nor the default "+ - "directory is usable, so the agent will start every conversation "+ - "with no recollection of earlier ones", - "configured", dir, "default", fallback, "err", fb.Err()) - return fb, fallback - } - log.Warn("using the default memory directory", "dir", fallback) - return fb, fallback -} - -// startContainers brings up the mandatory autonomous-worker pool and storage -// backend, returning a degraded nil pool when Docker is unavailable. -// -// Failure here degrades rather than aborting: the native interactive agent and -// dashboard remain useful without Docker, while autonomous tasks park because -// there is deliberately no host execution fallback. -func startContainers( - ctx context.Context, - cfg config.Config, - log *slog.Logger, -) (*container.Pool, storage.Backend, func()) { - noop := func() {} - // Single Docker client shared between pool and storage backend. - dockerCli, err := client.New(client.FromEnv) - if err != nil { - log.Error("autonomous workflows unavailable: docker client unavailable", "err", err) - return nil, nil, noop - } - closeDocker := func() { - if err := dockerCli.Close(); err != nil { - log.Warn("docker client close failed", "err", err) - } - } - - pool, err := container.NewPool(ctx, container.Config{ - Image: cfg.Containers.Image, - MaxConcurrency: cfg.Containers.MaxConcurrency, - MaxUptime: cfg.Containers.MaxUptime.Std(), - PullPolicy: cfg.Containers.PullPolicy, - Network: cfg.Containers.Network, - DockerClient: dockerCli, - RequireHostGateway: cfg.NATS.Mode == config.NATSModeEmbedded, - }, log) - if err != nil { - // A missing image is recoverable by hand. The daemon sends no registry - // credentials on pull (internal/container/pool.go), so a private - // registry always needs the operator's CLI to fetch it first. - log.Error("autonomous workflows unavailable: container pool unavailable", "err", err, - "hint", "run `docker compose pull agent` (or `build agent`) so the image is present locally") - return nil, storage.NewDockerBackend(dockerCli), closeDocker - } - - // contextcheck: Pool.Close takes no context by design -- it is a shutdown - // path that must run to completion after ctx is already cancelled. - //nolint:contextcheck - return pool, storage.NewDockerBackend(dockerCli), func() { - if err := pool.Close(); err != nil { - log.Warn("container pool close failed", "err", err) - } - closeDocker() + if args := os.Args[1:]; archied.IsCodesearchHelperArgs(args) { + os.Exit(archied.RunCodesearchHelper(args[1:], os.Stdout)) } + os.Exit(archied.Run()) } diff --git a/cmd/archied/bootstrap.go b/internal/app/archied/bootstrap.go similarity index 99% rename from cmd/archied/bootstrap.go rename to internal/app/archied/bootstrap.go index 04972396e..2cbef0e3e 100644 --- a/cmd/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -3,7 +3,7 @@ // The struct exists because the composition root assembles ~30 pieces of // state that every phase touches; passing them as parameters would make // each phase function longer than the code it contains. -package main +package archied import ( "context" diff --git a/cmd/archied/bootstrap_nats_test.go b/internal/app/archied/bootstrap_nats_test.go similarity index 99% rename from cmd/archied/bootstrap_nats_test.go rename to internal/app/archied/bootstrap_nats_test.go index c7e06196b..1ee690b0d 100644 --- a/cmd/archied/bootstrap_nats_test.go +++ b/internal/app/archied/bootstrap_nats_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "encoding/json" diff --git a/cmd/archied/chat_models.go b/internal/app/archied/chat_models.go similarity index 99% rename from cmd/archied/chat_models.go rename to internal/app/archied/chat_models.go index cd7c0fa4f..4d8a4e6e9 100644 --- a/cmd/archied/chat_models.go +++ b/internal/app/archied/chat_models.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/chat_models_test.go b/internal/app/archied/chat_models_test.go similarity index 99% rename from cmd/archied/chat_models_test.go rename to internal/app/archied/chat_models_test.go index d84492c8a..586457f5f 100644 --- a/cmd/archied/chat_models_test.go +++ b/internal/app/archied/chat_models_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/chat_stream_test.go b/internal/app/archied/chat_stream_test.go similarity index 99% rename from cmd/archied/chat_stream_test.go rename to internal/app/archied/chat_stream_test.go index 47055fe07..0f80a22df 100644 --- a/cmd/archied/chat_stream_test.go +++ b/internal/app/archied/chat_stream_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "strings" diff --git a/cmd/archied/chat_turn_model.go b/internal/app/archied/chat_turn_model.go similarity index 99% rename from cmd/archied/chat_turn_model.go rename to internal/app/archied/chat_turn_model.go index 839b4b65b..42ecc6c6d 100644 --- a/cmd/archied/chat_turn_model.go +++ b/internal/app/archied/chat_turn_model.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/codesearch.go b/internal/app/archied/codesearch.go similarity index 93% rename from cmd/archied/codesearch.go rename to internal/app/archied/codesearch.go index 2b0dfacd1..da03e84c2 100644 --- a/cmd/archied/codesearch.go +++ b/internal/app/archied/codesearch.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" @@ -27,16 +27,16 @@ var flagErrorOutput io.Writer = os.Stderr // and falls the agent back to unindexed grep. const codesearchHelperCommand = "workspace-codesearch" -// isCodesearchHelperArgs reports whether args (os.Args[1:]) selects the +// IsCodesearchHelperArgs reports whether args (os.Args[1:]) selects the // helper rather than the daemon. -func isCodesearchHelperArgs(args []string) bool { +func IsCodesearchHelperArgs(args []string) bool { return len(args) > 0 && args[0] == codesearchHelperCommand } -// runCodesearchHelper executes one helper subcommand and returns a process +// RunCodesearchHelper executes one helper subcommand and returns a process // exit code. Output is the machine-readable result the parent decodes; all // diagnostics go to stderr so they cannot corrupt it. -func runCodesearchHelper(args []string, out io.Writer) int { +func RunCodesearchHelper(args []string, out io.Writer) int { if len(args) == 0 { helperErrorf("%s: expected a subcommand (build|candidates)\n", codesearchHelperCommand) return 2 diff --git a/cmd/archied/codesearch_test.go b/internal/app/archied/codesearch_test.go similarity index 81% rename from cmd/archied/codesearch_test.go rename to internal/app/archied/codesearch_test.go index 023469277..d28c85eff 100644 --- a/cmd/archied/codesearch_test.go +++ b/internal/app/archied/codesearch_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "encoding/json" @@ -21,7 +21,7 @@ func TestRunCodesearchHelper(t *testing.T) { t.Run("build writes an index sidecar", func(t *testing.T) { var out strings.Builder - code := runCodesearchHelper([]string{"build", "--root", root, "--index", indexPath}, &out) + code := RunCodesearchHelper([]string{"build", "--root", root, "--index", indexPath}, &out) if code != 0 { t.Fatalf("build exit code = %d, want 0", code) } @@ -32,7 +32,7 @@ func TestRunCodesearchHelper(t *testing.T) { t.Run("candidates emits the JSON contract the manager decodes", func(t *testing.T) { var out strings.Builder - code := runCodesearchHelper([]string{ + code := RunCodesearchHelper([]string{ "candidates", "--index", indexPath, "--pattern", "needle", }, &out) if code != 0 { @@ -63,7 +63,7 @@ func TestRunCodesearchHelper(t *testing.T) { for _, tc := range invalid { t.Run(tc.name+" is rejected", func(t *testing.T) { var out strings.Builder - if code := runCodesearchHelper(tc.args, &out); code == 0 { + if code := RunCodesearchHelper(tc.args, &out); code == 0 { t.Errorf("exit code = 0, want non-zero for %v", tc.args) } }) @@ -73,13 +73,13 @@ func TestRunCodesearchHelper(t *testing.T) { // The manager shells out to argv[0] with this exact subcommand name, so a // rename on either side silently disables indexed search. func TestCodesearchHelperSubcommandIsRecognised(t *testing.T) { - if !isCodesearchHelperArgs([]string{codesearchHelperCommand, "build"}) { - t.Errorf("isCodesearchHelperArgs() did not recognise %q", codesearchHelperCommand) + if !IsCodesearchHelperArgs([]string{codesearchHelperCommand, "build"}) { + t.Errorf("IsCodesearchHelperArgs() did not recognise %q", codesearchHelperCommand) } - if isCodesearchHelperArgs([]string{"-once"}) { - t.Error("isCodesearchHelperArgs() claimed a normal daemon flag") + if IsCodesearchHelperArgs([]string{"-once"}) { + t.Error("IsCodesearchHelperArgs() claimed a normal daemon flag") } - if isCodesearchHelperArgs(nil) { - t.Error("isCodesearchHelperArgs() claimed an empty argument list") + if IsCodesearchHelperArgs(nil) { + t.Error("IsCodesearchHelperArgs() claimed an empty argument list") } } diff --git a/cmd/archied/config_update.go b/internal/app/archied/config_update.go similarity index 98% rename from cmd/archied/config_update.go rename to internal/app/archied/config_update.go index 2bce0cf53..6d1c25cdd 100644 --- a/cmd/archied/config_update.go +++ b/internal/app/archied/config_update.go @@ -1,4 +1,4 @@ -package main +package archied import ( "github.com/samcharles93/archie-core/internal/config" diff --git a/cmd/archied/config_update_test.go b/internal/app/archied/config_update_test.go similarity index 98% rename from cmd/archied/config_update_test.go rename to internal/app/archied/config_update_test.go index addf714eb..32ed91b84 100644 --- a/cmd/archied/config_update_test.go +++ b/internal/app/archied/config_update_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "testing" diff --git a/cmd/archied/execution_architecture_test.go b/internal/app/archied/execution_architecture_test.go similarity index 97% rename from cmd/archied/execution_architecture_test.go rename to internal/app/archied/execution_architecture_test.go index 03ca925e3..9396fb256 100644 --- a/cmd/archied/execution_architecture_test.go +++ b/internal/app/archied/execution_architecture_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "os" diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go new file mode 100644 index 000000000..553d18442 --- /dev/null +++ b/internal/app/archied/main.go @@ -0,0 +1,1075 @@ +// Command archied is the archie orchestrator daemon: it watches GitHub +// for issues labelled for archie, works each one in an isolated +// worktree through its routed workflow, and opens pull requests for +// human review. +package archied + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/json" + "flag" + "fmt" + "log/slog" + "maps" + "net" + "net/http" + "net/http/httptest" + "os" + "os/signal" + "path/filepath" + "strconv" + "strings" + "sync/atomic" + "syscall" + + "github.com/moby/moby/client" + natsio "github.com/nats-io/nats.go" + "github.com/samcharles93/ai-sdk/chat" + "github.com/samcharles93/ai-sdk/core" + + "github.com/samcharles93/archie-core/internal/agentexec" + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/container" + "github.com/samcharles93/archie-core/internal/daemon" + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/forge" + "github.com/samcharles93/archie-core/internal/forgerpc" + "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay" + "github.com/samcharles93/archie-core/internal/logging" + "github.com/samcharles93/archie-core/internal/memory/builtin" + "github.com/samcharles93/archie-core/internal/plugin" + "github.com/samcharles93/archie-core/internal/secret" + "github.com/samcharles93/archie-core/internal/storage" + "github.com/samcharles93/archie-core/internal/store" + "github.com/samcharles93/archie-core/internal/storerpc" + "github.com/samcharles93/archie-core/internal/taskstate" + "github.com/samcharles93/archie-core/internal/tools" + "github.com/samcharles93/archie-core/internal/tools/mcp" + toolprovider "github.com/samcharles93/archie-core/internal/tools/provider" + mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp" + "github.com/samcharles93/archie-core/internal/webui" + "github.com/samcharles93/archie-core/internal/worktree" + "github.com/samcharles93/archie-core/internal/worktreerpc" +) + +const ( + // defaultChatMaxSteps bounds the model/tool round-trips in one chat + // turn when [config.ChatConfig.MaxSteps] is unset. + defaultChatMaxSteps = 100 + packagedGatewayChangelogPath = "/usr/share/archie/CHANGELOG.archied.md" + packagedRuntimeChangelogPath = "/usr/share/archie/CHANGELOG.archie.md" +) + +// Component versions are injected from their independent release tags. +// Components without a release tag remain "dev" and never generate upgrade +// notifications. +var ( + gatewayVersion = "dev" + runtimeVersion = "dev" +) + +// chatGenerateOptions builds one chat turn's request. +func chatGenerateOptions( + ctx context.Context, + messages []chat.Message, + registry *tools.Registry, + maxSteps int, + limits agentexec.ToolLimits, + extra []tools.ToolEntry, +) (core.GenerateOptions, error) { + toolOpts := limits.Options() + if approval := gateway.ApprovalFromContext(ctx); approval != nil { + toolOpts.Approval = approval + } + toolSet, err := agentexec.BuildToolSet(registry, toolOpts) + if err != nil { + return core.GenerateOptions{}, err + } + extraSet, err := agentexec.BuildToolSetFrom(extra, toolOpts) + if err != nil { + return core.GenerateOptions{}, err + } + maps.Copy(toolSet, extraSet) + if maxSteps <= 0 { + maxSteps = defaultChatMaxSteps + } + return core.GenerateOptions{ + Messages: messages, + Tools: toolSet, + MaxSteps: maxSteps, + }, nil +} + +// isWithin reports whether target sits inside base. +func isWithin(base, target string) bool { + absBase, err := filepath.Abs(base) + if err != nil { + return false + } + absTarget, err := filepath.Abs(target) + if err != nil { + return false + } + rel, err := filepath.Rel(absBase, absTarget) + if err != nil { + return false + } + return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) +} + +// toolLimits reads the configured per-turn result limits. +func toolLimits(cfg config.Config) agentexec.ToolLimits { + return agentexec.ToolLimits{ + MaxResultChars: cfg.Tools.Policy.MaxResultChars, + SpillDir: cfg.Tools.Policy.SpillDir, + } +} + +// taskListOverRead multiplies the requested limit when reading rows that are +// filtered by identity afterwards. +const taskListOverRead = 5 + +// chatTaskListerAdapter gives the gateway a read view of one identity's tasks. +// gateway deliberately does not import internal/store, so the projection +// happens here, as it does for the writer and controller adapters below. +type chatTaskListerAdapter struct { + tasks func(context.Context, int) ([]store.Task, error) +} + +func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) { + // Over-read before filtering: Tasks applies its limit across the whole + // table, so asking for exactly `limit` would return fewer than that for + // this identity whenever another identity's work is more recent. + rows, err := a.tasks(ctx, limit*taskListOverRead) + if err != nil { + return nil, err + } + out := make([]gateway.ChatTaskSummary, 0, limit) + for _, task := range rows { + if task.Identity != identity { + continue + } + if len(out) >= limit { + break + } + out = append(out, gateway.ChatTaskSummary{ + ID: task.ID, + Repo: task.Owner + "/" + task.Repo, + Title: task.Title, + Status: task.Status, + Workflow: task.Workflow, + PRNumber: task.PRNumber, + }) + } + return out, nil +} + +// npmCacheServerEnv returns environment variables that make an +// npx-launched MCP server reuse a persistent package cache across daemon +// restarts, instead of npm re-resolving and re-downloading the package +// from the registry every time the daemon starts. Rooted under the +// daemon's own work dir so it persists regardless of whether the daemon +// itself runs in an ephemeral container. See mcp.NpmCacheEnv for the +// command-matching and env var details shared with archie-agent's +// per-task equivalent (internal/app/agentworker/mcp_providers.go), which +// points at a mounted cache volume instead of a work-dir subdirectory. +// +// workDir is unconditionally defaulted before registerTools runs +// (internal/infrastructure/configuration's applyGeneralDefaults), but +// filepath.Join silently accepts an empty string and would then put the +// cache in the daemon's current working directory instead of its +// persistent data directory -- guard it explicitly rather than trust that +// invariant here too, the way memoryProvider already does nearby. +func npmCacheServerEnv(command, workDir string) []string { + if workDir == "" { + return nil + } + return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache")) +} + +func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovider.Engine, error) { + name := strings.TrimSpace(server.Name) + if name == "" { + return nil, fmt.Errorf("MCP server name is required") + } + transportType := strings.ToLower(strings.TrimSpace(server.Transport)) + if transportType == "" { + transportType = "stdio" + } + + switch transportType { + case "stdio": + command := strings.TrimSpace(server.Command) + if command == "" { + return nil, fmt.Errorf("MCP stdio server %q requires a command", name) + } + transport := mcp.NewStdioTransport(mcp.StdioTransportConfig{ + Command: command, + Args: append([]string(nil), server.Args...), + Dir: server.WorkDir, + Env: npmCacheServerEnv(command, workDir), + }) + return mcptoolprovider.New(name, transport), nil + + case "http", "streamablehttp": + url := strings.TrimSpace(server.URL) + if url == "" { + return nil, fmt.Errorf("MCP http server %q requires a url", name) + } + transport := mcp.NewHTTPTransport(mcp.HTTPTransportConfig{ + Endpoint: url, + Headers: server.Headers, + }) + return mcptoolprovider.New(name, transport), nil + + case "sse": + sseEndpoint := strings.TrimSpace(server.SSEEndpoint) + if sseEndpoint == "" { + return nil, fmt.Errorf("MCP sse server %q requires an sse_endpoint", name) + } + transport := mcp.NewSSETransport(mcp.SSETransportConfig{ + SSEEndpoint: sseEndpoint, + MessageEndpoint: strings.TrimSpace(server.MessageEndpoint), + Headers: server.Headers, + }) + return mcptoolprovider.New(name, transport), nil + + default: + return nil, fmt.Errorf("MCP transport %q is not supported", transportType) + } +} + +// resolveForge builds the forge client for one forge configuration, returning +// the resolved token alongside it for the worktree manager. +// +// A missing or unusable credential disables the forge; it does not stop the +// daemon. The forge is one feature among many, and killing the process denies +// the operator chat, the gateway and every other subsystem over a capability +// they may not use at all -- under a systemd unit with Restart=on-failure that +// becomes a crash loop where the error scrolls past unread. A malformed +// configuration is a different matter and still fails fast in validation, well +// before this point. +func resolveForge(cfg config.Forge, secrets *secret.Registry, log *slog.Logger) (forge.Forge, string) { + if configuration.ForgeDisabled(cfg.Type) { + return forge.NewNoop(log), "" + } + token, err := cfg.Token.Resolve(secrets) + if err != nil || token == "" { + log.Warn("forge disabled: token unavailable", + "forge_type", cfg.Type, + "engine", cfg.Token.Engine, + "key", cfg.Token.Key, + "err", err) + return forge.NewNoop(log), "" + } + client, err := forge.New(cfg.Type, token, cfg.Host, log) + if err != nil { + log.Warn("forge disabled: client construction failed", + "forge_type", cfg.Type, "err", err) + return forge.NewNoop(log), "" + } + return client, token +} + +func openProductionTaskStore(ctx context.Context, path string) (store.TaskStore, error) { + return store.Open(ctx, path) +} + +func taskDBPath(configuredPath string) string { + return configuredPath + "-tasks.sqlite" +} + +// configDBPath resolves the runtime config overlay file. It is a sibling +// of the task and conversation stores (same configured path, own suffix) +// so each file owns its user_version and its migrator with no +// contention; recovery from a broken overlay is rm this file plus the +// --no-config-overlay boot flag. +func configDBPath(configuredPath string) string { + return configuredPath + "-config.sqlite" +} + +func manualRequeueTask(ctx context.Context, st store.TaskStore, taskID int64) error { + task, err := st.TaskByID(ctx, taskID) + if err != nil { + return err + } + if task == nil { + return fmt.Errorf("task %d not found", taskID) + } + switch task.Status { + case store.StatusParked, store.StatusWaitingHuman: + return st.Requeue(ctx, taskID, task.Status, "") + default: + return fmt.Errorf("task %d has status %q; only parked or waiting_human tasks can be requeued", taskID, task.Status) + } +} + +func Run() int { + defaultCfg := filepath.Join(configHome(), "archie", "config.toml") + cfgPath := flag.String("config", defaultCfg, "path to a TOML/YAML config file or configuration directory") + overlayPath := flag.String("config-overlay", "", "path to a TOML/YAML overlay file or configuration directory applied on top of -config") + noConfigOverlay := flag.Bool("no-config-overlay", false, "skip the runtime config overlay (recovery hatch for the DB overlay; the -config-overlay file overlay still applies)") + once := flag.Bool("once", false, "run a single poll+process cycle and exit (systemd timer / testing)") + requeue := flag.Int64("requeue", 0, "requeue a parked/waiting task by id (keeps its workflow), then exit unless -once is also set") + showVersion := flag.Bool("version", false, "print the gateway and runtime versions and exit") + flag.Parse() + // The versions were only reachable through the chat /version command, + // which needs a running daemon and a configured channel. An update-check + // adapter has to answer "what is installed?" from a shell, so expose the + // same two values here. Machine-readable: one "name version" per line. + if *showVersion { + fmt.Printf("archied %s\narchie-agent %s\n", gatewayVersion, runtimeVersion) + return 0 + } + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + b := newBootstrap() + if err := b.loadConfig(ctx, *cfgPath, *overlayPath, *noConfigOverlay); err != nil { + return 1 + } + defer b.cleanup() + + if err := b.openStores(ctx); err != nil { + return 1 + } + if exit, err := b.handleRequeue(ctx, *requeue, *once); err != nil { + return 1 + } else if exit { + return 0 + } + b.loadCatalog(ctx, *cfgPath) + b.setupObservability() + + if err := b.setupBackends(ctx); err != nil { + return 1 + } + + b.setupLLMAndChat(ctx) + if !b.setupGateways(ctx, *cfgPath, *overlayPath) { + return 1 + } + + if err := b.loadWorkflows(ctx); err != nil { + return 1 + } + if err := b.loadPlugins(); err != nil { + return 1 + } + if err := b.buildTreesAndIdentities(ctx); err != nil { + return 1 + } + + if err := b.registerNATSRPC(); err != nil { + return 1 + } + if err := b.setupMemory(); err != nil { + return 1 + } + b.setupCurators(ctx) + + b.setupGuardrails() + if err := b.registerTools(); err != nil { + return 1 + } + b.registerStandaloneTools() + b.buildDaemon() + b.wireConfigPublishing(ctx, *cfgPath, *overlayPath) + b.installUpdateConfigHandler() + b.installConfigHandlers(*cfgPath, *overlayPath) + + if err := b.startServices(ctx); err != nil { + return 1 + } + return exitCode(b.runLoop(ctx, *once)) +} + +// bootConfigOverlay layers the runtime config overlay over the resolved +// file config. Every failure degrades rather than aborts -- the daemon +// boots on file config alone and the reason is recorded in bootOverlayErr, +// which /api/config surfaces as the reload status. The returned store is +// nil only when Open failed; a store that opened but could not be read +// stays open and non-nil so the dashboard PATCH path and reload wiring +// keep exactly the behaviour of the original inline block. +func bootConfigOverlay( + ctx context.Context, + loader *configuration.Loader, + doc *configuration.Document, + bootOverlayErr *atomic.Pointer[string], + log *slog.Logger, +) (*overlay.Store, *configuration.Document) { + store, err := overlay.Open(ctx, configDBPath(doc.Config.DBPath)) + if err != nil { + recordBootOverlayError(bootOverlayErr, log, err, + "config overlay unavailable; booting on file config alone", + "path", configDBPath(doc.Config.DBPath)) + return nil, doc + } + overrides, err := store.Snapshot(ctx) + if err != nil { + recordBootOverlayError(bootOverlayErr, log, err, + "config overlay unreadable; booting on file config alone") + return store, doc + } + if len(overrides) == 0 { + return store, doc + } + applied, err := loader.ApplyOverlay(doc, overrides) + if err != nil { + recordBootOverlayError(bootOverlayErr, log, err, + "config overlay rejected by validation; booting on file config alone") + return store, doc + } + return store, applied +} + +// recordBootOverlayError stores the degrade reason for /api/config and +// logs it, so the operator sees both where they are looking. +func recordBootOverlayError(p *atomic.Pointer[string], log *slog.Logger, err error, msg string, args ...any) { + reason := err.Error() + p.Store(&reason) + args = append(args, "err", err) + log.Error(msg, args...) +} + +func persistAndBroadcastEvents(sink *events.Sub, st store.TaskStore, web *webui.Server, log *slog.Logger) { + for e := range sink.C { + if e.ID == 0 { + id, err := st.InsertEvent(context.Background(), e) + if err != nil { + log.Error("event sink insert failed", "err", err) + continue + } + e.ID = id + } + web.Broadcast(e) + } +} + +// curatorEventSink adapts the in-process event bus to the curator family's +// narrow emission contract. Bus publish is non-blocking with bounded, +// dropping per-subscriber buffers, so curator activity can never +// backpressure the daemon or a chat turn. +type curatorEventSink struct { + b *events.Bus +} + +func (s curatorEventSink) Emit(kind, detail string, data map[string]any) { + s.b.Publish(events.Event{Kind: kind, Detail: detail, Data: data}) +} + +type chatTaskWriterAdapter struct { + enqueue func( + ctx context.Context, + owner, repo, title, body, workflow, identity string, + ) (*store.Task, error) +} + +func (a chatTaskWriterAdapter) EnqueueChatTask( + ctx context.Context, + owner, repo, title, body, workflow, identity string, +) (int64, error) { + task, err := a.enqueue(ctx, owner, repo, title, body, workflow, identity) + if err != nil { + return 0, err + } + if task == nil { + return 0, fmt.Errorf("enqueue chat task returned no task") + } + return task.ID, nil +} + +type chatTaskControllerAdapter struct { + taskByID func(context.Context, int64) (*store.Task, error) + requeue func(context.Context, int64, string, string) error + transition func(context.Context, int64, string, string, string) error +} + +func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID int64) (gateway.ChatTaskStatus, bool, error) { + task, err := a.taskByID(ctx, taskID) + if err != nil { + return gateway.ChatTaskStatus{}, false, err + } + if task == nil { + return gateway.ChatTaskStatus{}, false, nil + } + if !task.IsForgeBacked() { + return gateway.ChatTaskStatus{Status: task.Status, Identity: task.Identity}, true, nil + } + return gateway.ChatTaskStatus{}, false, fmt.Errorf("task %d is not chat-originated", taskID) +} + +func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64) error { + return a.requeue(ctx, taskID, store.StatusWaitingHuman, "implement") +} + +func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error { + task, err := a.taskByID(ctx, taskID) + if err != nil { + return err + } + if task == nil { + return fmt.Errorf("task %d not found", taskID) + } + if task.IsForgeBacked() { + return fmt.Errorf("task %d is not chat-originated", taskID) + } + // An operator declining work lands in the same state whichever surface + // they used. This used to record StatusRejected while the dashboard + // recorded StatusClosedWontDo, so the same decision showed up as two + // different states -- and StatusRejected, which the PR reconciler uses + // for "the pull request was closed without merging", stopped meaning one + // thing. + return a.transition(ctx, taskID, task.Status, store.StatusClosedWontDo, reason) +} + +// chatTaskLogReaderAdapter gives the gateway a read view of a task's +// persisted log history without importing internal/logging or internal/store +// into the gateway package. Each identity's reader is scoped to its own +// tasks: the identity bound at construction is used for authorization, so a +// model cannot read another identity's task logs by passing a different +// identity through the tool input. +type chatTaskLogReaderAdapter struct { + tasks func(context.Context, int64) (*store.Task, error) + taskLogs *logging.TaskRegistry +} + +func (a chatTaskLogReaderAdapter) ReadChatTaskLogs( + ctx context.Context, identity string, taskID int64, attempt int, q gateway.ChatTaskLogQuery, +) (gateway.ChatTaskLogResult, error) { + task, err := a.tasks(ctx, taskID) + if err != nil { + return gateway.ChatTaskLogResult{}, err + } + if task == nil { + return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID) + } + // Match the filter chatTaskListerAdapter already applies: a model + // bound to one identity must not read logs for another identity's + // tasks, and tasks with no identity (forge-sourced) are not + // readable through a chat tool at all — they belong to the daemon, + // not a particular identity. "The empty string MUST NOT retain + // special meaning" (docs/architecture/identity.md). + if task.Identity != identity { + return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity) + } + + if attempt <= 0 { + attempt = task.Attempt + } + path := a.taskLogs.Path(taskID, attempt) + if path == "" { + return gateway.ChatTaskLogResult{Attempt: attempt, Entries: []gateway.ChatTaskLogEntry{}}, nil + } + + page, err := logging.Page(path, logging.Query{ + Component: q.Component, + Contains: q.Contains, + Levels: q.Levels, + Since: q.Since, + Until: q.Until, + Limit: q.Limit, + }, q.AfterID) + if err != nil { + return gateway.ChatTaskLogResult{}, err + } + + entries := make([]gateway.ChatTaskLogEntry, len(page.Entries)) + for i, e := range page.Entries { + entries[i] = gateway.ChatTaskLogEntry{ + Time: e.Time, + Level: e.Level, + Message: e.Message, + Fields: e.Fields, + } + } + return gateway.ChatTaskLogResult{ + Entries: entries, + Attempt: attempt, + Truncated: page.Truncated, + Cursor: page.Cursor, + MoreAvailable: page.MoreAvailable, + }, nil +} + +// chatTaskActorAdapter executes operator actions on a task via the same +// action path the dashboard uses, ensuring chat and webui cannot diverge. +// It enforces identity scoping so an identity cannot mutate another +// identity's tasks. +type chatTaskActorAdapter struct { + tasks func(context.Context, int64) (*store.Task, error) + handler http.Handler + token func() string +} + +func (a chatTaskActorAdapter) ApplyChatTaskAction( + ctx context.Context, identity string, taskID int64, action taskstate.Action, +) (gateway.TaskActionResult, error) { + if a.tasks == nil || a.handler == nil { + return gateway.TaskActionResult{}, fmt.Errorf("task actions are not configured") + } + task, err := a.tasks(ctx, taskID) + if err != nil { + return gateway.TaskActionResult{}, err + } + if task == nil { + return gateway.TaskActionResult{}, fmt.Errorf("task %d not found", taskID) + } + // Match the filter chatTaskListerAdapter and chatTaskLogReaderAdapter apply: + // a model bound to one identity must not mutate another identity's tasks, + // and tasks with no identity (forge-sourced) are not actionable through a + // chat tool at all — they belong to the daemon, not a particular identity. + if task.Identity != identity { + return gateway.TaskActionResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity) + } + if err := taskstate.CheckAction(task.Status, action); err != nil { + return gateway.TaskActionResult{}, err + } + + body, err := json.Marshal(map[string]any{"action": string(action)}) + if err != nil { + return gateway.TaskActionResult{}, fmt.Errorf("marshal action request: %w", err) + } + req := httptest.NewRequestWithContext( + ctx, http.MethodPost, + fmt.Sprintf("/api/tasks/%d/action", taskID), + bytes.NewReader(body), + ) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Archie-CSRF", "1") + if a.token != nil { + if tok := a.token(); tok != "" { + req.Header.Set("Authorization", "Bearer "+tok) + } + } + rec := httptest.NewRecorder() + a.handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + errMsg := strings.TrimSpace(rec.Body.String()) + if errMsg == "" { + errMsg = fmt.Sprintf("HTTP %d", rec.Code) + } + return gateway.TaskActionResult{}, fmt.Errorf("action %s on task %d failed: %s", action, taskID, errMsg) + } + + return gateway.TaskActionResult{ + TaskID: taskID, + Action: string(action), + Message: fmt.Sprintf("Applied %s to task %d.", action, taskID), + }, nil +} + +func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) { + if len(cfg.Identities) > 0 { + profiles := make([]gateway.TaskProfile, 0, len(cfg.Identities)) + for _, identity := range cfg.Identities { + if len(identity.Repos) == 0 { + continue + } + profiles = append(profiles, newChatTaskProfile(identity.Name, identity.Repos)) + } + if len(profiles) == 0 { + return nil, "" + } + return profiles, profiles[0].Identity + } + if len(cfg.Repos) == 0 { + return nil, "" + } + return []gateway.TaskProfile{newChatTaskProfile("", cfg.Repos)}, "" +} + +func newChatTaskProfile(identity string, repos []config.Repo) gateway.TaskProfile { + allowed := make([]string, 0, len(repos)) + for _, repo := range repos { + allowed = append(allowed, repo.Owner+"/"+repo.Name) + } + return gateway.TaskProfile{ + Identity: identity, + DefaultOwner: repos[0].Owner, + DefaultRepo: repos[0].Name, + Repos: allowed, + } +} + +func configureTaskCommands( + router *gateway.Router, + tasks gateway.TaskCreator, + controller gateway.TaskController, + identity string, +) { + router.Tasks = tasks + router.Controller = controller + router.Identity = identity +} + +// parseListenAddr splits "host:port" into components, using defaults +// when the input is empty or missing a part. +func parseListenAddr(addr, defaultHost string, defaultPort int) (string, int) { + if addr == "" { + return defaultHost, defaultPort + } + host, portStr, err := net.SplitHostPort(addr) + if err != nil { + return defaultHost, defaultPort + } + port, err := strconv.Atoi(portStr) + if err != nil || port <= 0 { + return host, defaultPort + } + return host, port +} + +// sessionKey builds a deterministic session identifier from a gateway +// message's routing fields. Platform + channel + thread uniquely identify +// a conversation for session persistence and history retrieval. +func executionProviders(cfg config.Config) map[string]agentexec.Provider { + return agentexec.ProvidersFromConfig(cfg.Providers) +} + +// subscribeSystemLogs subscribes to every task's system log subject at once. +// A sandboxed container's own stderr disappears at AutoRemove; archie-agent +// ships it here instead (agentexec.SubjectForSystem), and this is where it +// lands: one wildcard subscription demuxed by taskID and routed through +// taskLogs, which is opened for a task's duration in Daemon.process, so a +// message for a task not currently open there -- late, duplicate, or from +// an attempt this instance never dispatched -- is expected and silently +// dropped rather than treated as an error. +func subscribeSystemLogs(nc *natsio.Conn, taskLogs *logging.TaskRegistry, log *slog.Logger) (unsubscribe func(), err error) { + sub, err := nc.Subscribe(agentexec.SubjectSystemWildcard, func(msg *natsio.Msg) { + taskID, ok := agentexec.TaskIDFromSystemSubject(msg.Subject) + if !ok { + log.Warn("system log message on unparseable subject", "subject", msg.Subject) + return + } + var entry logging.Entry + if err := json.Unmarshal(msg.Data, &entry); err != nil { + log.Warn("system log message undecodable", "task", taskID, "err", err) + return + } + taskLogs.Write(taskID, entry) + }) + if err != nil { + return nil, err + } + return func() { + if err := sub.Unsubscribe(); err != nil { + log.Warn("system log unsubscribe failed", "err", err) + } + }, nil +} + +// subscribeAgentEvents subscribes to every task's events subject at once and +// republishes each decoded event on bus. An archie-agent worker's +// agentexec.ForwardTaskEvents ships a task's workflow events (stage +// progress, outcome, parking) here because that worker's own *events.Bus is +// in-process and invisible to the daemon; this is the other half of the +// bridge, landing them on bus so persistAndBroadcastEvents -- the single +// choke point that inserts into the SQLite events table and fans out over +// SSE -- treats them exactly like every other daemon-observed event. Mirrors +// subscribeSystemLogs's demux-by-taskID shape. +func subscribeAgentEvents(nc *natsio.Conn, bus *events.Bus, log *slog.Logger) (unsubscribe func(), err error) { + sub, err := nc.Subscribe(agentexec.SubjectEventsWildcard, func(msg *natsio.Msg) { + taskID, ok := agentexec.TaskIDFromEventsSubject(msg.Subject) + if !ok { + log.Warn("task event message on unparseable subject", "subject", msg.Subject) + return + } + var e events.Event + if err := json.Unmarshal(msg.Data, &e); err != nil { + log.Warn("task event message undecodable", "task", taskID, "err", err) + return + } + bus.Publish(e) + }) + if err != nil { + return nil, err + } + if err := nc.Flush(); err != nil { + if cleanupErr := sub.Unsubscribe(); cleanupErr != nil { + log.Warn("task event subscription cleanup failed after flush failure", "err", cleanupErr) + } + return nil, fmt.Errorf("flush task event subscription: %w", err) + } + return func() { + if err := sub.Unsubscribe(); err != nil { + log.Warn("task event unsubscribe failed", "err", err) + } + }, nil +} + +// registerTaskRPCServers subscribes the storerpc/forgerpc/worktreerpc +// handlers on nc so an archie-agent container (which holds no DB +// connection, forge token, or push credential) can proxy those operations +// back to archied. The returned func unsubscribes all of them. +// +// The store is shared across identities, so storerpc registers once on its +// root subjects. Forge and worktree are identity-scoped: the root servers +// answer the root (identity-less) subjects for single-identity deployments +// and root-owned tasks, and one server pair per identity answers that +// identity's scoped subjects so a container-mode task owned by a non-root +// identity has its RPC calls served by its own forge client and worktree +// manager. +func registerTaskRPCServers(nc *natsio.Conn, st store.TaskStore, forgeClient forge.Forge, trees *worktree.Manager, identities []*daemon.IdentityRunner, grants *worktreerpc.Grants, log *slog.Logger) (unsubscribe func(), err error) { + unsubs := make([]func(), 0, 3+2*len(identities)) + unsubAll := func() { + for _, u := range unsubs { + u() + } + } + + storeServer := &storerpc.Server{Store: st, Log: log} + u, err := storeServer.Register(nc) + if err != nil { + return nil, fmt.Errorf("register storerpc: %w", err) + } + unsubs = append(unsubs, u) + + registerForge := func(fg forge.Forge, identity string) error { + srv := &forgerpc.Server{Forge: fg, Log: log.With("rpc_identity", identity)} + u, err := srv.RegisterFor(nc, identity) + if err != nil { + return fmt.Errorf("register forgerpc%s: %w", identitySuffix(identity), err) + } + unsubs = append(unsubs, u) + return nil + } + registerTrees := func(mgr *worktree.Manager, identity string) error { + srv := &worktreerpc.Server{Trees: mgr, Grants: grants, Log: log.With("rpc_identity", identity)} + u, err := srv.RegisterFor(nc, identity) + if err != nil { + return fmt.Errorf("register worktreerpc%s: %w", identitySuffix(identity), err) + } + unsubs = append(unsubs, u) + return nil + } + + // Root (identity-less) forge and worktree servers. + if err := registerForge(forgeClient, ""); err != nil { + unsubAll() + return nil, err + } + if err := registerTrees(trees, ""); err != nil { + unsubAll() + return nil, err + } + + // One forge/worktree server pair per identity, on identity-scoped + // subjects. + for _, id := range identities { + if err := registerForge(id.Forge, id.Name); err != nil { + unsubAll() + return nil, err + } + if err := registerTrees(id.Trees, id.Name); err != nil { + unsubAll() + return nil, err + } + } + + return unsubAll, nil +} + +// identitySuffix renders a log suffix for an identity-scoped registration, +// empty for the root set. +func identitySuffix(identity string) string { + if identity == "" { + return "" + } + return " (" + identity + ")" +} + +func configuredNATSToken(cfg config.NATSConfig, getenv func(string) string) (string, error) { + if cfg.TokenEnv == "" { + return "", nil + } + token := getenv(cfg.TokenEnv) + if token == "" { + return "", fmt.Errorf("%s is required when nats.token_env is configured", cfg.TokenEnv) + } + return token, nil +} + +func configHome() string { + if x := os.Getenv("XDG_CONFIG_HOME"); x != "" { + return x + } + home, _ := os.UserHomeDir() + return filepath.Join(home, ".config") +} + +func releaseAnnouncementStatePath(workDir, identity string) string { + identityHash := sha256.Sum256([]byte(identity)) + return filepath.Join( + workDir, + fmt.Sprintf("release-announcements-%x.json", identityHash[:8]), + ) +} + +// updateReportPath is where the update watchdog leaves the phase-2 outcome +// of an update for this identity to relay on its next launch. Hashed the +// same way as releaseAnnouncementStatePath so multiple identities sharing +// one daemon (see docs/architecture/identity.md) never collide. +func updateReportPath(workDir, identity string) string { + identityHash := sha256.Sum256([]byte(identity)) + return filepath.Join( + workDir, + fmt.Sprintf("update-report-%x.json", identityHash[:8]), + ) +} + +// safePluginInfo calls Name() and Version() on a plugin, recovering from +// panics. A panicking plugin must not crash the daemon at startup. +func safePluginInfo(p plugin.Plugin) (name, version string) { + defer func() { + if r := recover(); r != nil { + name = "(panic)" + version = "(panic)" + } + }() + return p.Name(), p.Version() +} + +// webTokenFor returns the dashboard token for a listen address, or "" when +// none is needed. +// +// A loopback bind needs no token: local access already implies the agent's own +// authority, since its shell and edit tools run as this user. A reachable bind +// mints one and logs a URL to click, so an instance cannot be left exposed +// merely by omitting configuration. +func webTokenFor(listen, dbPath string, log *slog.Logger) string { + if webui.IsLoopback(listen) { + return "" + } + path := filepath.Join(filepath.Dir(dbPath), "web-token") + tok, err := webui.LoadOrCreateToken(path) + if err != nil { + log.Error("web ui token", "err", err) + return "" + } + log.Info("web ui token", "open", webui.DashboardURL(listen, tok), "stored", path) + return tok +} + +// memoryProvider builds the built-in memory provider, falling back to the +// default work directory when the configured one cannot be established. +// +// A bad path warns and degrades rather than stopping the daemon, in line with +// the forge and container paths: memory is one capability among many. The +// fallback is the location an unset work_dir would have produced, so an +// operator who copied a config from another host (a container path such as +// /var/lib/archie/work onto a laptop, say) lands somewhere predictable +// instead of somewhere only this function knows about. +// +// Returns the provider and the directory actually in use. A nil provider +// means the configuration is unusable in a way no fallback can fix. +func memoryProvider(workDir string, log *slog.Logger) (*builtin.Provider, string) { + // An empty workDir would make filepath.Join produce the relative path + // "memory", which MkdirAll creates in whatever directory the daemon + // happens to be started from -- succeeding, and putting memory somewhere + // nobody will look. Defaulting should have prevented an empty workDir; + // treat it as unset rather than trusting it. + var dir string + var p *builtin.Provider + if workDir == "" { + log.Warn("no work directory configured, using the default for memory") + } else { + dir = filepath.Join(workDir, "memory") + var err error + p, err = builtin.New(builtin.Config{Dir: dir}) + if err != nil { + log.Warn("memory directory rejected, falling back to the default", "dir", dir, "err", err) + p = nil + } + if p != nil && p.IsAvailable() { + return p, dir + } + if p != nil { + log.Warn("memory directory unusable, falling back to the default", + "dir", dir, "err", p.Err()) + } + } + + fallback := filepath.Join(configuration.DefaultWorkDir(), "memory") + if fallback == dir { + // Already the default: there is nowhere else to try, so run degraded + // and say what that costs rather than pretending memory works. + log.Error("memory unavailable: the agent will start every conversation "+ + "with no recollection of earlier ones, and memory writes will fail", + "dir", dir) + return p, dir + } + + fb, err := builtin.New(builtin.Config{Dir: fallback}) + if err != nil { + log.Error("memory provider init failed at the default directory", "dir", fallback, "err", err) + return p, dir + } + if !fb.IsAvailable() { + log.Error("memory unavailable: neither the configured nor the default "+ + "directory is usable, so the agent will start every conversation "+ + "with no recollection of earlier ones", + "configured", dir, "default", fallback, "err", fb.Err()) + return fb, fallback + } + log.Warn("using the default memory directory", "dir", fallback) + return fb, fallback +} + +// startContainers brings up the mandatory autonomous-worker pool and storage +// backend, returning a degraded nil pool when Docker is unavailable. +// +// Failure here degrades rather than aborting: the native interactive agent and +// dashboard remain useful without Docker, while autonomous tasks park because +// there is deliberately no host execution fallback. +func startContainers( + ctx context.Context, + cfg config.Config, + log *slog.Logger, +) (*container.Pool, storage.Backend, func()) { + noop := func() {} + // Single Docker client shared between pool and storage backend. + dockerCli, err := client.New(client.FromEnv) + if err != nil { + log.Error("autonomous workflows unavailable: docker client unavailable", "err", err) + return nil, nil, noop + } + closeDocker := func() { + if err := dockerCli.Close(); err != nil { + log.Warn("docker client close failed", "err", err) + } + } + + pool, err := container.NewPool(ctx, container.Config{ + Image: cfg.Containers.Image, + MaxConcurrency: cfg.Containers.MaxConcurrency, + MaxUptime: cfg.Containers.MaxUptime.Std(), + PullPolicy: cfg.Containers.PullPolicy, + Network: cfg.Containers.Network, + DockerClient: dockerCli, + RequireHostGateway: cfg.NATS.Mode == config.NATSModeEmbedded, + }, log) + if err != nil { + // A missing image is recoverable by hand. The daemon sends no registry + // credentials on pull (internal/container/pool.go), so a private + // registry always needs the operator's CLI to fetch it first. + log.Error("autonomous workflows unavailable: container pool unavailable", "err", err, + "hint", "run `docker compose pull agent` (or `build agent`) so the image is present locally") + return nil, storage.NewDockerBackend(dockerCli), closeDocker + } + + // contextcheck: Pool.Close takes no context by design -- it is a shutdown + // path that must run to completion after ctx is already cancelled. + //nolint:contextcheck + return pool, storage.NewDockerBackend(dockerCli), func() { + if err := pool.Close(); err != nil { + log.Warn("container pool close failed", "err", err) + } + closeDocker() + } +} diff --git a/cmd/archied/main_test.go b/internal/app/archied/main_test.go similarity index 99% rename from cmd/archied/main_test.go rename to internal/app/archied/main_test.go index 227d3408b..f7747a2cf 100644 --- a/cmd/archied/main_test.go +++ b/internal/app/archied/main_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/memory_test.go b/internal/app/archied/memory_test.go similarity index 99% rename from cmd/archied/memory_test.go rename to internal/app/archied/memory_test.go index 1e3f5e00f..436b6a844 100644 --- a/cmd/archied/memory_test.go +++ b/internal/app/archied/memory_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "log/slog" diff --git a/cmd/archied/model_catalog.go b/internal/app/archied/model_catalog.go similarity index 99% rename from cmd/archied/model_catalog.go rename to internal/app/archied/model_catalog.go index c87274d3e..654d47e17 100644 --- a/cmd/archied/model_catalog.go +++ b/internal/app/archied/model_catalog.go @@ -1,4 +1,4 @@ -package main +package archied import ( "maps" diff --git a/cmd/archied/model_catalog_test.go b/internal/app/archied/model_catalog_test.go similarity index 99% rename from cmd/archied/model_catalog_test.go rename to internal/app/archied/model_catalog_test.go index 3ffabdff7..1663de774 100644 --- a/cmd/archied/model_catalog_test.go +++ b/internal/app/archied/model_catalog_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "reflect" diff --git a/cmd/archied/provider_secrets.go b/internal/app/archied/provider_secrets.go similarity index 99% rename from cmd/archied/provider_secrets.go rename to internal/app/archied/provider_secrets.go index e061cfbc0..9a4bea4c3 100644 --- a/cmd/archied/provider_secrets.go +++ b/internal/app/archied/provider_secrets.go @@ -1,4 +1,4 @@ -package main +package archied import ( "crypto/sha256" diff --git a/cmd/archied/reload.go b/internal/app/archied/reload.go similarity index 99% rename from cmd/archied/reload.go rename to internal/app/archied/reload.go index 0479ebc7e..4ca56d598 100644 --- a/cmd/archied/reload.go +++ b/internal/app/archied/reload.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/reload_test.go b/internal/app/archied/reload_test.go similarity index 99% rename from cmd/archied/reload_test.go rename to internal/app/archied/reload_test.go index 1bf005f36..ee3d5fa08 100644 --- a/cmd/archied/reload_test.go +++ b/internal/app/archied/reload_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/store_wiring_test.go b/internal/app/archied/store_wiring_test.go similarity index 98% rename from cmd/archied/store_wiring_test.go rename to internal/app/archied/store_wiring_test.go index bd34d9900..99ea039a2 100644 --- a/cmd/archied/store_wiring_test.go +++ b/internal/app/archied/store_wiring_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "errors" diff --git a/cmd/archied/task_action_adapter_test.go b/internal/app/archied/task_action_adapter_test.go similarity index 99% rename from cmd/archied/task_action_adapter_test.go rename to internal/app/archied/task_action_adapter_test.go index 47b3a86d9..4078c73e5 100644 --- a/cmd/archied/task_action_adapter_test.go +++ b/internal/app/archied/task_action_adapter_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/task_actions_test.go b/internal/app/archied/task_actions_test.go similarity index 99% rename from cmd/archied/task_actions_test.go rename to internal/app/archied/task_actions_test.go index af4991913..dada7987f 100644 --- a/cmd/archied/task_actions_test.go +++ b/internal/app/archied/task_actions_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "bytes" diff --git a/cmd/archied/telegram_session_store_test.go b/internal/app/archied/telegram_session_store_test.go similarity index 98% rename from cmd/archied/telegram_session_store_test.go rename to internal/app/archied/telegram_session_store_test.go index 867b63057..9e5d1bc92 100644 --- a/cmd/archied/telegram_session_store_test.go +++ b/internal/app/archied/telegram_session_store_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "bytes" diff --git a/cmd/archied/telegram_setup.go b/internal/app/archied/telegram_setup.go similarity index 99% rename from cmd/archied/telegram_setup.go rename to internal/app/archied/telegram_setup.go index f8e5ccef2..afc572f0a 100644 --- a/cmd/archied/telegram_setup.go +++ b/internal/app/archied/telegram_setup.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/telegram_setup_regression_test.go b/internal/app/archied/telegram_setup_regression_test.go similarity index 97% rename from cmd/archied/telegram_setup_regression_test.go rename to internal/app/archied/telegram_setup_regression_test.go index 2746ad373..d40fac9c0 100644 --- a/cmd/archied/telegram_setup_regression_test.go +++ b/internal/app/archied/telegram_setup_regression_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "context" diff --git a/cmd/archied/telegram_setup_test.go b/internal/app/archied/telegram_setup_test.go similarity index 99% rename from cmd/archied/telegram_setup_test.go rename to internal/app/archied/telegram_setup_test.go index 299947d2d..de5e75961 100644 --- a/cmd/archied/telegram_setup_test.go +++ b/internal/app/archied/telegram_setup_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "testing" diff --git a/cmd/archied/toolsummary.go b/internal/app/archied/toolsummary.go similarity index 98% rename from cmd/archied/toolsummary.go rename to internal/app/archied/toolsummary.go index dac1ba5fb..5c621bc15 100644 --- a/cmd/archied/toolsummary.go +++ b/internal/app/archied/toolsummary.go @@ -1,4 +1,4 @@ -package main +package archied import ( "slices" diff --git a/cmd/archied/toolsummary_test.go b/internal/app/archied/toolsummary_test.go similarity index 98% rename from cmd/archied/toolsummary_test.go rename to internal/app/archied/toolsummary_test.go index 88ad7956d..36cac25bc 100644 --- a/cmd/archied/toolsummary_test.go +++ b/internal/app/archied/toolsummary_test.go @@ -1,4 +1,4 @@ -package main +package archied import ( "testing" diff --git a/internal/plugin/architecture_test.go b/internal/plugin/architecture_test.go index a31296bb9..1fc62ce64 100644 --- a/internal/plugin/architecture_test.go +++ b/internal/plugin/architecture_test.go @@ -18,6 +18,20 @@ const ( engineRuleLink = "ARCHITECTURE.md#plugin-engine-rule-strict" ) +func TestPluginEngineRuleIsCanonicalAndSharedByAgentInstructions(t *testing.T) { + t.Parallel() + + architecture := readTestFile(t, filepath.Join("..", "..", "ARCHITECTURE.md")) + if !strings.Contains(architecture, engineRuleHeading) { + t.Fatalf("ARCHITECTURE.md missing canonical heading %q", engineRuleHeading) + } + + claudeMD := readTestFile(t, filepath.Join("..", "..", "CLAUDE.md")) + if !strings.Contains(claudeMD, engineRuleLink) { + t.Fatalf("CLAUDE.md missing reference to %q", engineRuleLink) + } +} + func TestEngineFamilyInspectionRejectsInvalidShapes(t *testing.T) { t.Parallel()