refactor: add internal/eventbus as the broker-neutral messaging contract
dependencies-and-contracts.md rule 1 says domains must not import
internal/infrastructure. internal/daemon and internal/agentexec both did,
and for exactly the thing the doc carves out: "internal/eventbus is an
approved shared application contract ... it MUST remain broker-neutral."
The Publisher/Consumer/Requester interfaces existed but lived inside the
NATS package, so a caller could depend on the behaviour yet still had to
import the implementation to name it. They now live in internal/eventbus,
and nats.Client asserts against eventbus.Bus.
The larger violation was ownership of meaning. The doc says "message schemas
stay with their owning domains; the event bus transports messages, it does
not own their meaning." The bus exported TaskEnvelope and a PublishTask
method, so the transport knew what a unit of work was, and it exported the
archie.agent.* subject helpers, so it also owned the agent protocol.
- TaskEnvelope, its routing Kind, the label vocabulary and the task
subjects move to internal/domain/workintake, which encodes and decodes
its own bytes. This starts internal/domain/ per organisation.md.
- The agent request/response/system subjects move to internal/agentexec,
which defines what those messages mean.
- internal/workflow keeps only the kind-to-registry mapping; the label
table stays single and lives with the domain that reads forge issues.
- The bus now carries opaque payloads on plain-string subjects.
Consumers declare the smallest interface they need rather than taking the
whole Bus: daemon.TaskBus is publish-unique/fetch/request, agentexec.
RequestBus is publish-request/reply-inbox, and archie-agent's stageBus is
fetch/respond. None can reach what it does not use.
Two defects this surfaced:
- archie-agent declared its own JetStream stream with a second copy of the
name, subjects, retention, dedup window, MaxDeliver and TTLs that
nats.Connect already sets -- two definitions of one stream, free to
diverge. Config now takes Subjects and FilterSubject from composition,
which is what let the agent use the shared client and delete its
hand-rolled setup.
- Replying to a request needs core NATS, not the JetStream publish path:
a reply inbox is ephemeral and belongs to no stream, so a durable
publish to it fails. The old code hid this by using the raw connection.
The contract now distinguishes Respond from Publish. The agentexec
round-trip test caught it after I had made the same mistake in
archie-agent.
Also translated the SDK's ErrNoResponders into eventbus.ErrNoResponders, so
the daemon's taskrun retry no longer imports the NATS SDK to name a
transient condition. The SDK is now confined to the nats package, the *rpc
packages (themselves NATS infrastructure), and cmd/*.
The agentexec test fake previously hand-rolled a JetStream consumer; it now
drains the client's own consumer, which is both closer to production and
avoids conflicting consumers on a work-queue stream.
Verified: go build, go vet, gofmt clean; full test suite passes.
This commit is contained in:
1 parent
69dce53bfd
commit
9d5040fd65
23 files changed
+787
-600
No files matched your search
@@ -219,3 +219,5 @@
|
||||
{"id":"int-d2b3b90cb93d38eb23d3d2120738768a","kind":"field_change","created_at":"2026-07-29T00:13:03.819548678Z","actor":"Sam Catlow","issue_id":"archie-core-vq4","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Closed"}}
|
||||
{"id":"int-fc8b703ac5f77deef0c0fa7fa4c6fd91","kind":"field_change","created_at":"2026-07-29T00:14:32.561829752Z","actor":"Sam Catlow","issue_id":"archie-core-24g","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Completed"}}
|
||||
{"id":"int-121dfb79c17bb556e48b19bbfe6c1b0d","kind":"field_change","created_at":"2026-07-29T00:15:26.026966964Z","actor":"Sam Catlow","issue_id":"archie-core-qa4.4","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Closed"}}
|
||||
{"id":"int-56175e7ff25f356ec9f6f1b44d96597d","kind":"field_change","created_at":"2026-07-29T01:11:44.064703641Z","actor":"Sam Catlow","issue_id":"archie-core-uxk","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Closed"}}
|
||||
{"id":"int-07f5fbc5b999b3c33395e21ef0c068dc","kind":"field_change","created_at":"2026-07-29T01:11:44.270013581Z","actor":"Sam Catlow","issue_id":"archie-core-dkf","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Closed"}}
|
||||
+2
-2
@@ -53,7 +53,7 @@
|
||||
{"_type":"issue","id":"archie-core-abg.18","title":"[EPIC] Memory: pluggable provider architecture with file-backed persistence","description":"Pluggable memory system to match Hermes parity:\\n\\n1. **MemoryProvider interface**: Abstract interface with methods for initialize(), get_tool_schemas(), prefetch(), sync_turn(), and optional lifecycle hooks (on_session_end, on_session_switch, on_pre_compress, on_memory_write).\\n\\n2. **Built-in file-backed memory**: MEMORY.md and USER.md stored per-session/profile. LLM-callable memory tool with add/replace/remove actions. Frozen snapshot at session start (preserves prefix cache), immediate disk write on updates. Section-sign delimited entries with substring matching.\\n\\n3. **MemoryManager**: Orchestrates built-in + at most one external provider. Background serialized write executor. Prefetch with timeout (8s). Streaming context scrubber that strips injected memory spans from model output.\\n\\nRef: docs/prds/hermes-gap-analysis.md section 4","acceptance_criteria":"An external memory provider (e.g. Honcho, Mem0) can be plugged in via config. The built-in memory tool persists across sessions. Conversation context is recalled on session resume.","status":"closed","priority":0,"issue_type":"epic","owner":"sam@catlow.tech","created_at":"2026-07-25T14:02:22Z","created_by":"Sam Catlow","updated_at":"2026-07-28T17:04:41Z","closed_at":"2026-07-28T17:04:41Z","close_reason":"Superseded by merged work: all 32 children complete (32/32, bd flags it 'eligible for close'). MemoryProvider interface, built-in file-backed memory, MemoryManager, scrubber, drain and threat scanning all landed in internal/memory. Nothing left in scope.","dependencies":[{"issue_id":"archie-core-abg.18","depends_on_id":"archie-core-abg","type":"parent-child","created_at":"2026-07-26T00:02:22Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-abg.16","title":"[EPIC] Gateway: rich message model, adapter lifecycle, session management","description":"Foundational gateway infrastructure to match Hermes parity. Three interdependent pieces:\\n\\n1. **Message model**: Replace the 3-field Message struct with a rich MessageEvent (MessageType enum: text/photo/video/audio/voice/document/sticker/command, media_urls, reply_to context, metadata dict, source). This is the wire type every adapter produces.\\n\\n2. **Adapter lifecycle**: Replace the 3-method Gateway interface with a BasePlatformAdapter interface: connect()/disconnect()/send() as required methods, plus optional overrides for typing indicators, edit/delete, image/voice/video/document send, interactive UX (clarify buttons, approval dialogs). Capability flags (supports_code_blocks, splits_long_messages, supports_async_delivery).\\n\\n3. **Session management**: SessionSource struct (platform, chat_id, chat_type, user_id, thread_id, guild_id, profile), AsyncSessionStore for persistent session storage, SessionContext for system prompt injection.\\n\\nAll three blocks together enable adding new platforms as plugins without touching core gateway code.\\n\\nRef: docs/prds/hermes-gap-analysis.md sections 1-2","acceptance_criteria":"New adapters can be added by implementing a Go interface with connect/disconnect/send + optional media methods. Messages carry full metadata through the pipeline. Sessions persist across gateway restarts.","status":"open","priority":0,"issue_type":"epic","owner":"sam@catlow.tech","created_at":"2026-07-25T14:02:21Z","created_by":"Sam Catlow","updated_at":"2026-07-25T14:02:21Z","dependencies":[{"issue_id":"archie-core-abg.16","depends_on_id":"archie-core-abg","type":"parent-child","created_at":"2026-07-26T00:02:21Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-jfg","title":"Spawned archie-agent containers not on daemon's docker network — can't reach NATS","description":"Root cause of 'agent still failing on the server': archied's container.Pool.Acquire creates archie-agent containers via the Docker SDK with no NetworkMode set, so Docker attaches them to the default bridge network. archied and nats run on the compose-created 'archie-core_default' network instead, so the spawned container's hostname resolution for 'nats' (from NATS_URL=nats://nats:4222) fails and archie-agent can't connect. Confirmed on carina host logs: archied logs show repeating 'taskrun request failed err=\"nats: no responders available for request\"' for every task, containers created and destroyed within ~1.5s (consistent with archie-agent's NATS client failing fast). Fixed by detecting the daemon's own container's network in NewPool (selfNetwork, via ContainerInspect on os.Hostname()) and setting HostConfig.NetworkMode on spawned containers to match.","status":"closed","priority":0,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-07-24T03:10:26Z","created_by":"Sam Catlow","updated_at":"2026-07-25T13:04:34Z","closed_at":"2026-07-25T13:04:34Z","close_reason":"Fix already implemented and verified: selfNetwork() in internal/container/pool.go detects the daemon's own container network via ContainerInspect(os.Hostname()) and Acquire() sets HostConfig.NetworkMode to match, so spawned archie-agent containers join the same compose network as nats. Build and internal/container tests pass.","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-uxk","title":"gateway: restore /whoami /profile /sessions /resume /agents handlers","description":"These five command handlers were lost during parallel merges of the Telegram\nworkers. The Router fields they need (Identity, Models, Sessions, Agents)\nalready exist; the handlers and their Route cases do not.\n\n18 tests in internal/gateway/gateway_test.go already specify the exact\nbehaviour. They are currently failing. Run them and make them pass:\n\n go test ./internal/gateway/...\n\nFailing: TestRouteWhoami, TestRouteWhoamiNoIdentity, TestRouteWhoamiWithModel,\nTestRouteProfile, TestRouteProfileNoIdentity, TestRouteSessionsEmpty,\nTestRouteSessionsNotConfigured, TestRouteSessionsError, TestRouteResumeNoArg,\nTestRouteResumeNotConfigured, TestRouteResumeNoMatch, TestRouteResumeAmbiguous,\nTestRouteResumeExactMatch, TestRouteAgentsEmpty, TestRouteAgentsNotConfigured,\nTestRouteAgentsWithTasks, TestRouteAgentsError, TestLocalCommandsIncludesNewCommands\n\nScope:\n- ONLY internal/gateway/. Do not touch any other package.\n- Read the tests first; they define the exact reply text fragments expected\n (\"not configured\", \"No sessions\", \"Usage\", etc). Do not invent wording.\n- Follow the shape of the existing handlers in gateway.go and\n session_commands.go: handleX methods on *Router returning (string, error).\n- A nil dependency means \"not configured\" and returns a reply, not an error.\n A dependency that returns an error must propagate it (see TestRouteSessionsError).\n- Add the new commands to the Route switch and to LocalCommands.\n\nConstraints:\n- Do NOT add //nolint or otherwise suppress lint findings. Fix issues properly.\n- Do NOT write architecture docs, ledgers, plans or new beads. Just the code.\n- Do NOT modify the tests to make them pass. The tests are correct.\n\nDone when: go build ./... , go vet ./... , gofmt -l . clean, and\ngo test ./internal/gateway/... passes with zero failures.","status":"open","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-29T00:58:16Z","created_by":"Sam Catlow","updated_at":"2026-07-29T00:58:16Z","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-uxk","title":"gateway: restore /whoami /profile /sessions /resume /agents handlers","description":"These five command handlers were lost during parallel merges of the Telegram\nworkers. The Router fields they need (Identity, Models, Sessions, Agents)\nalready exist; the handlers and their Route cases do not.\n\n18 tests in internal/gateway/gateway_test.go already specify the exact\nbehaviour. They are currently failing. Run them and make them pass:\n\n go test ./internal/gateway/...\n\nFailing: TestRouteWhoami, TestRouteWhoamiNoIdentity, TestRouteWhoamiWithModel,\nTestRouteProfile, TestRouteProfileNoIdentity, TestRouteSessionsEmpty,\nTestRouteSessionsNotConfigured, TestRouteSessionsError, TestRouteResumeNoArg,\nTestRouteResumeNotConfigured, TestRouteResumeNoMatch, TestRouteResumeAmbiguous,\nTestRouteResumeExactMatch, TestRouteAgentsEmpty, TestRouteAgentsNotConfigured,\nTestRouteAgentsWithTasks, TestRouteAgentsError, TestLocalCommandsIncludesNewCommands\n\nScope:\n- ONLY internal/gateway/. Do not touch any other package.\n- Read the tests first; they define the exact reply text fragments expected\n (\"not configured\", \"No sessions\", \"Usage\", etc). Do not invent wording.\n- Follow the shape of the existing handlers in gateway.go and\n session_commands.go: handleX methods on *Router returning (string, error).\n- A nil dependency means \"not configured\" and returns a reply, not an error.\n A dependency that returns an error must propagate it (see TestRouteSessionsError).\n- Add the new commands to the Route switch and to LocalCommands.\n\nConstraints:\n- Do NOT add //nolint or otherwise suppress lint findings. Fix issues properly.\n- Do NOT write architecture docs, ledgers, plans or new beads. Just the code.\n- Do NOT modify the tests to make them pass. The tests are correct.\n\nDone when: go build ./... , go vet ./... , gofmt -l . clean, and\ngo test ./internal/gateway/... passes with zero failures.","status":"closed","priority":1,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-29T00:58:16Z","created_by":"Sam Catlow","updated_at":"2026-07-29T01:11:44Z","closed_at":"2026-07-29T01:11:44Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-o3l","title":"infrastructure/eventbus/nats: relocate NATS transport","description":"Create internal/infrastructure/eventbus/nats as a properly structured replacement for internal/nats. This is NOT a mechanical move: the existing code has no interfaces, leaks vendor types (Conn() *nats.Conn, PublishMsg(*nats.Msg), Fetch() jetstream.Msg) into all four consumers, uses a 7-positional-parameter publish with stringly-typed comma-separated labels, stores an unused logger, returns (nil, nil) for 'no message', and hardcodes stream/consumer/timeout config. Rebuild it with: separate files per concern; a Message wrapper so jetstream types stop crossing the boundary; Publisher/Consumer interfaces; a Config struct; sentinel errors; consistent %w wrapping; real logging. Then update cmd/archied, cmd/archie-agent, internal/daemon and internal/agentexec to the new API.","acceptance_criteria":"internal/nats no longer exists; internal/infrastructure/eventbus/nats has its contents. All imports updated. task check clean. Container and NATS behaviour unchanged.","status":"closed","priority":1,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-29T00:02:50Z","created_by":"Sam Catlow","updated_at":"2026-07-29T00:14:19Z","started_at":"2026-07-29T00:04:35Z","closed_at":"2026-07-29T00:06:37Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-vq4","title":"infrastructure/configuration: external input boundary","description":"Read docs/architecture/configuration.md. Create internal/infrastructure/configuration as the external config input boundary. It owns TOML/YAML decoding, env and secret references, base/overlay precedence, compatibility aliases, input validation, and provenance. It must NOT own domain behaviour. Preserve current LoadOverlay behaviour exactly and keep every existing TOML field name working. Translate the decoded input into the current internal/config.Config shape at the composition boundary as an explicitly-marked temporary adapter, so runtime behaviour is unchanged. Do not migrate consumers in this bead.","acceptance_criteria":"internal/infrastructure/configuration exists and is the loader used by cmd/archied. Old config files still load with identical effective values. task check clean. No consumer outside cmd/archied changed. The temporary adapter carries a comment naming its deletion condition.","status":"closed","priority":1,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-29T00:02:49Z","created_by":"Sam Catlow","updated_at":"2026-07-29T00:13:04Z","started_at":"2026-07-29T00:04:30Z","closed_at":"2026-07-29T00:13:04Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-24g","title":"Trim the remaining agent skills to factual reference","description":"The .agents/skills/ library is 4283 lines across 12 skills. An audit found most carry useful facts buried in process ceremony. Five worst offenders were already parked at /work/apps/archie-skills-parked. Trim what remains to roughly 2200 lines total. KEEP: concrete commands, package anchors, config wiring tables, NellDB shapes, symptom-to-diagnostic indexes, known regressions, domain glossary, operational hazards, false-confidence warnings. CUT: mandated N-step procedures, handoff templates, evidence packets, acceptance checklists, ownership/deletion/rollback field lists, sibling-skill routing prose, repeated 'reject unless' instructions, dated tool snapshots. Preserve every skill's YAML frontmatter name and description. Do not delete any skill file outright.","acceptance_criteria":"Total line count of .agents/skills/*/*.md is between 1900 and 2500. All 12 skills still exist with valid frontmatter. No factual command, path, or table was lost - only procedure and ceremony. go build ./... still clean.","status":"closed","priority":1,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-28T17:18:09Z","created_by":"Sam Catlow","updated_at":"2026-07-29T00:14:32Z","started_at":"2026-07-28T17:18:44Z","closed_at":"2026-07-29T00:14:32Z","close_reason":"Completed","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
@@ -150,7 +150,7 @@
|
||||
{"_type":"issue","id":"archie-core-s1r","title":"Test forge fakes missing LinkBranch method broke go vet/test for whole module","description":"forge.Forge gained LinkBranch(ctx, owner, repo, issueNumber, branch) error in the branch-linking feature (ff4c51e) but the hand-rolled test doubles (stubForge in cmd/archied/main_test.go, prCapturingForge in cmd/archie-agent/taskrun_test.go, testForge in internal/daemon/daemon_test.go, fakeForge in internal/forgerpc/forgerpc_test.go) never got the method added, so they stopped satisfying forge.Forge. This broke go vet ./... and go test ./... for the entire module, not just those 4 packages, since go vet type-checks all test files as part of module vet. Fixed by adding LinkBranch stubs to all four fakes.","status":"closed","priority":1,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-07-24T03:04:53Z","created_by":"Sam Catlow","updated_at":"2026-07-24T03:04:58Z","closed_at":"2026-07-24T03:04:58Z","close_reason":"Added LinkBranch stub methods to stubForge, prCapturingForge, testForge, and fakeForge. go build/vet/test ./... now clean across the whole module.","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-633","title":"config.docker.toml missing required fields, no overlay merge implemented","description":"config.docker.toml was rewritten to be a docs-as-overlay file (comment says base+overlay merge) but internal/config.Load has no merge logic and docker-compose.yml mounts only config.docker.toml at /etc/archie/config.toml. It is missing bot_user, [forge], [dispatch], [models], [providers], [budgets] which Load requires -\u003e daemon fails to start in docker as currently configured. Implement real base+overlay config loading (or restore a full standalone docker config) so there is one source of truth.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-24T02:42:07Z","created_by":"Sam Catlow","updated_at":"2026-07-24T02:44:53Z","started_at":"2026-07-24T02:42:11Z","closed_at":"2026-07-24T02:44:53Z","close_reason":"Implemented config.LoadOverlay (base decode + overlay decode into same struct, then shared finalize/validate), wired -config-overlay flag in cmd/archied, fixed docker-compose.yml to mount both base config.toml and config.docker.toml overlay, minimized config.docker.toml to true diff-only overlay, added tests proving overlay-omitted required fields (bot_user, repos) still come from base.","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-u64","title":"Daemon: process multiple tasks concurrently (max_concurrency enforcement)","description":"Daemon currently processes one task at a time (docs/prds/archie-core-v2-architecture.md Phase 2). This is the core blocker between current state and any multi-agent scaling mode — needed before container max_concurrency or same-repo concurrency can matter. Wire the daemon's dispatch loop to run N tasks in parallel, respecting a configurable max_concurrency.","design":"internal/container/pool.go already enforces max_concurrency for container lifecycle — reuse that limit/pattern in the daemon's task dispatch rather than inventing a second concurrency control.","acceptance_criteria":"Daemon dispatch loop runs multiple tasks concurrently up to a configured max_concurrency limit; single-task-at-a-time behavior is gone.","status":"closed","priority":1,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:31Z","created_by":"Sam Catlow","updated_at":"2026-07-23T21:37:42Z","started_at":"2026-07-23T21:29:41Z","closed_at":"2026-07-23T21:37:42Z","close_reason":"Implemented concurrent SQLite and NATS task dispatch using containers.max_concurrency, with default same-repo serialization and race-tested dispatcher coverage.","dependency_count":0,"dependent_count":2,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-dkf","title":"telegram: publish /new /retry /undo in menu and help","description":"TestPublishedCommandsMatchExecutableCommandSurface in\ninternal/channels/telegram/telegram_test.go fails: /new, /retry and /undo are\nexecutable commands but are missing from the published command menu and the\nhelp text, so users cannot discover them.\n\nRun: go test ./internal/channels/telegram/...\n\nScope:\n- ONLY internal/channels/telegram/. Do not touch any other package.\n- Read the test to see how it derives the executable command surface, then add\n the three commands to whichever menu/help structures it compares against.\n- Match the existing entries' wording style and ordering conventions.\n\nConstraints:\n- Do NOT add //nolint or otherwise suppress lint findings.\n- Do NOT modify the test to make it pass. The test is correct: these commands\n really are missing from the menu.\n- Do NOT write architecture docs, ledgers, plans or new beads.\n\nDone when: go build ./... , go vet ./... , gofmt -l . clean, and\ngo test ./internal/channels/telegram/... passes.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-29T00:58:25Z","created_by":"Sam Catlow","updated_at":"2026-07-29T00:58:25Z","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-dkf","title":"telegram: publish /new /retry /undo in menu and help","description":"TestPublishedCommandsMatchExecutableCommandSurface in\ninternal/channels/telegram/telegram_test.go fails: /new, /retry and /undo are\nexecutable commands but are missing from the published command menu and the\nhelp text, so users cannot discover them.\n\nRun: go test ./internal/channels/telegram/...\n\nScope:\n- ONLY internal/channels/telegram/. Do not touch any other package.\n- Read the test to see how it derives the executable command surface, then add\n the three commands to whichever menu/help structures it compares against.\n- Match the existing entries' wording style and ordering conventions.\n\nConstraints:\n- Do NOT add //nolint or otherwise suppress lint findings.\n- Do NOT modify the test to make it pass. The test is correct: these commands\n really are missing from the menu.\n- Do NOT write architecture docs, ledgers, plans or new beads.\n\nDone when: go build ./... , go vet ./... , gofmt -l . clean, and\ngo test ./internal/channels/telegram/... passes.","status":"closed","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-29T00:58:25Z","created_by":"Sam Catlow","updated_at":"2026-07-29T01:11:44Z","closed_at":"2026-07-29T01:11:44Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-5d7.1","title":"Docsgen: emit deterministic contract data JSON","description":"Begin the generated-documentation implementation with the smallest vertical slice: normalize the existing Archie contract schemas into deterministic JSON under docs-2/data/generated for VitePress consumption, without generating Markdown pages. Preserve the nested tools module and existing source-of-truth imports.","acceptance_criteria":"A failing test first proves docsgen emits valid generated JSON at the data path; repeated generation is byte-identical; tools tests and vet pass; generated-documentation.md reflects the agreed data-loader architecture.","notes":"Implemented the first docs generation slice test-first: docsgen now emits deterministic JSON contract data at docs-2/data/generated/contracts.json, carries generator provenance, rejects duplicate published names and conflicting definitions, validates resolved internal references in tests, and keeps the current hardcoded contract set as an explicit migration adapter with deletion criteria. Scoped tools tests, vet, and golangci-lint pass; repeated generation is byte-identical; adversarial review reports zero surviving findings.","status":"closed","priority":2,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-28T09:17:08Z","created_by":"Sam Catlow","updated_at":"2026-07-28T09:32:22Z","started_at":"2026-07-28T09:17:13Z","closed_at":"2026-07-28T09:32:22Z","close_reason":"Deterministic contract data JSON slice implemented test-first, validated, documented, and independently reviewed with zero surviving findings.","dependencies":[{"issue_id":"archie-core-5d7.1","depends_on_id":"archie-core-5d7","type":"parent-child","created_at":"2026-07-28T19:17:08Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-qfx","title":"Flaky: TestRunTaskExecutesBootstrapWorkflowEndToEnd races on NATS responders","description":"cmd/archie-agent. Intermittently fails with ParkReason 'stage prepare: archie.worktree.prepare: nats: no responders available for request'; passes on re-run (-count=3 clean). The test does not wait for the worktreerpc subscription to be established before dispatching. A flaky test in task check is poison: it trains everyone to re-run instead of read, which is how a real regression gets waved through.","status":"open","priority":2,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-07-27T15:27:55Z","created_by":"Sam Catlow","updated_at":"2026-07-27T15:27:55Z","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-5dn","title":"Document Hermes capabilities missing from Archie","description":"Investigate Hermes' available features and commands, compare them with Archie, and publish a simple Markdown TODO checklist excluding Kanban.","status":"closed","priority":2,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-27T12:09:56Z","created_by":"Sam Catlow","updated_at":"2026-07-27T12:11:14Z","started_at":"2026-07-27T12:10:00Z","closed_at":"2026-07-27T12:11:14Z","close_reason":"Investigated local Hermes CLI and current Archie implementation; added the requested parity TODO checklist.","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
|
||||
Executable
BIN
Binary file not shown.
+44
-69
@@ -10,6 +10,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -20,21 +21,23 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
arnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
)
|
||||
|
||||
const (
|
||||
streamName = "ARCHIE_TASKS"
|
||||
consumerName = "archie-agent"
|
||||
dedupWindow = 2 * time.Minute
|
||||
pollTimeout = 5 * time.Second
|
||||
ackWait = 30 * time.Minute
|
||||
|
||||
// ackWait exceeds the longest stage runtime: a stage still working must
|
||||
// not have its message redelivered to another worker.
|
||||
ackWait = 30 * time.Minute
|
||||
|
||||
// taskRunSubjectWildcard matches full-task handoffs (archie.taskrun.<id>).
|
||||
// A queue group means only one archie-agent instance answers each
|
||||
@@ -65,23 +68,29 @@ func run() int {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
// Connect to NATS.
|
||||
var natsOptions []nats.Option
|
||||
if natsToken != "" {
|
||||
natsOptions = append(natsOptions, nats.Token(natsToken))
|
||||
}
|
||||
nc, err := nats.Connect(natsURL, natsOptions...)
|
||||
// One bus client owns the stream definition. archied declares the same
|
||||
// stream with the same subjects; this process previously declared its
|
||||
// own copy, free to disagree about retention, dedup window and TTLs.
|
||||
bus, err := arnats.Connect(ctx, arnats.Config{
|
||||
URL: natsURL,
|
||||
Token: natsToken,
|
||||
Subjects: []string{workintake.SubjectTaskWildcard, agentexec.SubjectAgentWildcard},
|
||||
ConsumerName: *consumer,
|
||||
FilterSubject: agentexec.SubjectAgentWildcard,
|
||||
PollTimeout: pollTimeout,
|
||||
AckWait: ackWait,
|
||||
}, log)
|
||||
if err != nil {
|
||||
log.Error("nats connect failed", "err", err)
|
||||
return 1
|
||||
}
|
||||
defer nc.Close()
|
||||
log.Info("nats connected", "url", natsURL)
|
||||
defer bus.Close()
|
||||
|
||||
// JetStream setup.
|
||||
_, cons, err := setupJetStream(ctx, nc, *consumer)
|
||||
// Full-task handoffs use core NATS request/reply with a queue group,
|
||||
// which the JetStream contract does not express.
|
||||
nc, err := bus.CoreConn()
|
||||
if err != nil {
|
||||
log.Error("jetstream setup failed", "err", err)
|
||||
log.Error("nats connection unavailable", "err", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -98,37 +107,7 @@ func run() int {
|
||||
}()
|
||||
|
||||
log.Info("archie-agent ready", "nats", natsURL, "consumer", *consumer)
|
||||
return runMainLoop(ctx, cons, nc, log)
|
||||
}
|
||||
|
||||
func setupJetStream(ctx context.Context, nc *nats.Conn, consumerName string) (jetstream.JetStream, jetstream.Consumer, error) {
|
||||
js, err := jetstream.New(nc)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
stream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{
|
||||
Name: streamName,
|
||||
Subjects: []string{"archie.task.>", "archie.agent.>"},
|
||||
Storage: jetstream.FileStorage,
|
||||
Retention: jetstream.WorkQueuePolicy,
|
||||
Duplicates: dedupWindow,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cons, err := stream.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig{
|
||||
Name: consumerName,
|
||||
Durable: consumerName,
|
||||
FilterSubject: arnats.SubjectAgentWildcard,
|
||||
AckPolicy: jetstream.AckExplicitPolicy,
|
||||
MaxDeliver: 3,
|
||||
AckWait: ackWait,
|
||||
InactiveThreshold: 24 * time.Hour,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return js, cons, nil
|
||||
return runMainLoop(ctx, bus, log)
|
||||
}
|
||||
|
||||
// subscribeTaskRuns prefers a dedicated per-task subscription over the
|
||||
@@ -167,15 +146,25 @@ func bootTaskID(mountDir string, log *slog.Logger) (int64, bool) {
|
||||
return payload.ID, true
|
||||
}
|
||||
|
||||
// stageBus is the messaging this loop needs: pull the next stage request and
|
||||
// answer it on the requester's inbox.
|
||||
type stageBus interface {
|
||||
Fetch(ctx context.Context) (eventbus.Message, error)
|
||||
Respond(ctx context.Context, replyAddress string, payload []byte) error
|
||||
}
|
||||
|
||||
// runMainLoop runs the fetch-handle-ack loop until the context is cancelled.
|
||||
func runMainLoop(ctx context.Context, cons jetstream.Consumer, nc *nats.Conn, log *slog.Logger) int {
|
||||
func runMainLoop(ctx context.Context, bus stageBus, log *slog.Logger) int {
|
||||
for {
|
||||
if ctx.Err() != nil {
|
||||
log.Info("archie-agent shutting down")
|
||||
return 0
|
||||
}
|
||||
batch, err := cons.Fetch(1, jetstream.FetchMaxWait(pollTimeout))
|
||||
if err != nil {
|
||||
msg, err := bus.Fetch(ctx)
|
||||
switch {
|
||||
case errors.Is(err, eventbus.ErrNoMessage):
|
||||
continue
|
||||
case err != nil:
|
||||
if ctx.Err() != nil {
|
||||
return 0
|
||||
}
|
||||
@@ -183,21 +172,7 @@ func runMainLoop(ctx context.Context, cons jetstream.Consumer, nc *nats.Conn, lo
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
}
|
||||
var msg jetstream.Msg
|
||||
for m := range batch.Messages() {
|
||||
if m != nil {
|
||||
msg = m
|
||||
break
|
||||
}
|
||||
}
|
||||
if err := batch.Error(); err != nil {
|
||||
log.Error("fetch batch error", "err", err)
|
||||
continue
|
||||
}
|
||||
if msg == nil {
|
||||
continue
|
||||
}
|
||||
if err := handle(ctx, msg, nc, log); err != nil {
|
||||
if err := handle(ctx, msg, bus, log); err != nil {
|
||||
log.Error("handle failed", "err", err)
|
||||
if err := msg.Nak(); err != nil {
|
||||
log.Warn("nak failed", "err", err)
|
||||
@@ -218,15 +193,15 @@ func natsConnectionSettings(flagURL string, getenv func(string) string) (url, to
|
||||
return url, getenv("NATS_TOKEN")
|
||||
}
|
||||
|
||||
func handle(ctx context.Context, msg jetstream.Msg, nc *nats.Conn, log *slog.Logger) error {
|
||||
func handle(ctx context.Context, msg eventbus.Message, bus stageBus, log *slog.Logger) error {
|
||||
var req agentexec.AgentRequestMessage
|
||||
if err := json.Unmarshal(msg.Data(), &req); err != nil {
|
||||
return fmt.Errorf("decode request: %w", err)
|
||||
}
|
||||
|
||||
replyTo := msg.Headers().Get(arnats.ReplyHeader)
|
||||
if replyTo == "" {
|
||||
return fmt.Errorf("missing %s header", arnats.ReplyHeader)
|
||||
replyTo, err := msg.ReplyAddress()
|
||||
if err != nil {
|
||||
return fmt.Errorf("stage request on %s: %w", msg.Subject(), err)
|
||||
}
|
||||
|
||||
log.Info("processing stage",
|
||||
@@ -246,7 +221,7 @@ func handle(ctx context.Context, msg jetstream.Msg, nc *nats.Conn, log *slog.Log
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode response: %w", err)
|
||||
}
|
||||
if err := nc.Publish(replyTo, data); err != nil {
|
||||
if err := bus.Respond(ctx, replyTo, data); err != nil {
|
||||
return fmt.Errorf("publish response: %w", err)
|
||||
}
|
||||
|
||||
|
||||
+9
-4
@@ -30,6 +30,7 @@ import (
|
||||
"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/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
@@ -208,9 +209,13 @@ func run() int {
|
||||
log.Error("nats credentials", "err", err)
|
||||
return 1
|
||||
}
|
||||
// Composition owns the subject list: the bus must not know which
|
||||
// subjects belong to which domain.
|
||||
natsClient, err = nats.Connect(ctx, nats.Config{
|
||||
URL: cfg.NATS.URL,
|
||||
Token: natsToken,
|
||||
URL: cfg.NATS.URL,
|
||||
Token: natsToken,
|
||||
Subjects: []string{workintake.SubjectTaskWildcard, agentexec.SubjectAgentWildcard},
|
||||
FilterSubject: workintake.SubjectTaskWildcard,
|
||||
}, log)
|
||||
if err != nil {
|
||||
log.Error("nats connect failed", "err", err)
|
||||
@@ -353,7 +358,7 @@ func run() int {
|
||||
return 1
|
||||
}
|
||||
agentRunner = &agentexec.NATSRunner{
|
||||
Nats: natsClient,
|
||||
Bus: natsClient,
|
||||
Providers: providers,
|
||||
Log: log,
|
||||
}
|
||||
@@ -543,7 +548,7 @@ func run() int {
|
||||
Storage: storeBackend,
|
||||
Log: log,
|
||||
CustomStages: wfeval.Discover,
|
||||
Nats: natsClient,
|
||||
Tasks: natsClient,
|
||||
ContainerPool: containerPool,
|
||||
Memory: memManager,
|
||||
Guardrails: guardrails,
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
arnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// AgentRequestMessage is the NATS payload for an agent stage execution request.
|
||||
@@ -37,10 +37,24 @@ type AgentResponseEnvelope struct {
|
||||
|
||||
const defaultAgentTimeout = 30 * time.Minute
|
||||
|
||||
// NATSRunner sends agent stage execution requests over NATS JetStream and
|
||||
// waits for replies on core NATS inboxes. It implements Runner.
|
||||
// RequestBus is the messaging this runner needs: publish a request carrying a
|
||||
// reply address, and create the inbox that address points at.
|
||||
//
|
||||
// Declared here rather than taking eventbus.Bus because a domain defines the
|
||||
// smallest interface required to do its work -- this runner never fetches or
|
||||
// subscribes, and should not be able to.
|
||||
type RequestBus interface {
|
||||
PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error
|
||||
NewReplyInbox() (eventbus.ReplyInbox, error)
|
||||
}
|
||||
|
||||
// NATSRunner sends agent stage execution requests over the bus and waits for
|
||||
// replies on a one-shot inbox. It implements Runner.
|
||||
//
|
||||
// The name is historical: it holds no NATS type and works with any bus
|
||||
// satisfying RequestBus.
|
||||
type NATSRunner struct {
|
||||
Nats *arnats.Client
|
||||
Bus RequestBus
|
||||
Providers map[string]Provider
|
||||
Log *slog.Logger
|
||||
}
|
||||
@@ -53,7 +67,7 @@ func (r *NATSRunner) Run(ctx context.Context, workspace string, req Request) (Re
|
||||
}
|
||||
|
||||
// 1. Create reply inbox with auto-unsubscribe after one message.
|
||||
replySub, err := r.Nats.NewReplyInbox()
|
||||
replySub, err := r.Bus.NewReplyInbox()
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("nats reply inbox: %w", err)
|
||||
}
|
||||
@@ -84,8 +98,8 @@ func (r *NATSRunner) Run(ctx context.Context, workspace string, req Request) (Re
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
subject := arnats.SubjectForAgentRequest(req.TaskID)
|
||||
if err := r.Nats.PublishRequest(ctx, subject, replySub.Subject(), data); err != nil {
|
||||
subject := SubjectForRequest(req.TaskID)
|
||||
if err := r.Bus.PublishRequest(ctx, subject, replySub.Subject(), data); err != nil {
|
||||
return Result{}, fmt.Errorf("publish agent request: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,12 @@ import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats-server/v2/server"
|
||||
natssrv "github.com/nats-io/nats-server/v2/test"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
arnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
)
|
||||
|
||||
@@ -169,8 +168,8 @@ func TestHandleMessageRoutesSystemMessagesSeparately(t *testing.T) {
|
||||
}
|
||||
|
||||
// Verify the subject functions exist and are distinct.
|
||||
respSubj := arnats.SubjectForAgentResponse(42)
|
||||
sysSubj := arnats.SubjectForAgentSystem(42)
|
||||
respSubj := SubjectForResponse(42)
|
||||
sysSubj := SubjectForSystem(42)
|
||||
if respSubj == sysSubj || respSubj == "" || sysSubj == "" {
|
||||
t.Error("Gap 3: subject functions broken -- verify SubjectForAgentResponse " +
|
||||
"and SubjectForAgentSystem return distinct, non-empty subjects.")
|
||||
@@ -189,78 +188,73 @@ func startEmbeddedNATS(t *testing.T) *server.Server {
|
||||
return srv
|
||||
}
|
||||
|
||||
func startHandler(t *testing.T, nc *natsio.Conn) {
|
||||
func startHandler(t *testing.T, client *arnats.Client) {
|
||||
t.Helper()
|
||||
js, err := jetstream.New(nc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stream, err := js.CreateOrUpdateStream(context.Background(), jetstream.StreamConfig{
|
||||
Name: "ARCHIE_TASKS",
|
||||
Subjects: []string{"archie.task.>", "archie.agent.>"},
|
||||
Storage: jetstream.FileStorage,
|
||||
Retention: jetstream.WorkQueuePolicy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cons, err := stream.CreateOrUpdateConsumer(context.Background(), jetstream.ConsumerConfig{
|
||||
Name: "test-agent",
|
||||
FilterSubject: arnats.SubjectAgentWildcard,
|
||||
AckPolicy: jetstream.AckExplicitPolicy,
|
||||
MaxDeliver: 1,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
// Drain the client's own consumer rather than subscribing a second one:
|
||||
// the stream uses work-queue retention, so two consumers with
|
||||
// overlapping filter subjects conflict. This mirrors archie-agent.
|
||||
go func() {
|
||||
for {
|
||||
batch, err := cons.Fetch(1, jetstream.FetchMaxWait(5*time.Second))
|
||||
for ctx.Err() == nil {
|
||||
msg, err := client.Fetch(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
continue
|
||||
}
|
||||
for msg := range batch.Messages() {
|
||||
if msg == nil {
|
||||
continue
|
||||
}
|
||||
var req AgentRequestMessage
|
||||
_ = json.Unmarshal(msg.Data(), &req)
|
||||
replyTo := msg.Headers().Get(arnats.ReplyHeader)
|
||||
resp := AgentResponseEnvelope{
|
||||
Version: req.Request.Version,
|
||||
Result: Result{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: req.TaskID,
|
||||
Attempt: req.Attempt,
|
||||
Stage: req.Stage,
|
||||
Status: StatusPassed,
|
||||
Summary: "handler-reply",
|
||||
TokensUsed: 42,
|
||||
},
|
||||
}
|
||||
data, _ := json.Marshal(resp)
|
||||
_ = nc.Publish(replyTo, data)
|
||||
_ = msg.Ack()
|
||||
if err := replyToStage(ctx, client, msg); err != nil {
|
||||
t.Errorf("stage handler: %v", err)
|
||||
_ = msg.Nak()
|
||||
continue
|
||||
}
|
||||
_ = msg.Ack()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// replyToStage answers one stage request with a canned passing result.
|
||||
func replyToStage(ctx context.Context, client *arnats.Client, msg eventbus.Message) error {
|
||||
var req AgentRequestMessage
|
||||
if err := json.Unmarshal(msg.Data(), &req); err != nil {
|
||||
return err
|
||||
}
|
||||
replyTo, err := msg.ReplyAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.Marshal(AgentResponseEnvelope{
|
||||
Version: req.Request.Version,
|
||||
Result: Result{
|
||||
Version: ProtocolVersion,
|
||||
TaskID: req.TaskID,
|
||||
Attempt: req.Attempt,
|
||||
Stage: req.Stage,
|
||||
Status: StatusPassed,
|
||||
Summary: "handler-reply",
|
||||
TokensUsed: 42,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.Respond(ctx, replyTo, data)
|
||||
}
|
||||
|
||||
func TestNATSRunnerRoundTrip(t *testing.T) {
|
||||
srv := startEmbeddedNATS(t)
|
||||
url := srv.ClientURL()
|
||||
|
||||
ctx := context.Background()
|
||||
client, err := arnats.Connect(ctx, arnats.Config{URL: url}, slog.New(slog.DiscardHandler))
|
||||
client, err := arnats.Connect(ctx, arnats.Config{URL: url, Subjects: []string{workintake.SubjectTaskWildcard, SubjectAgentWildcard}, FilterSubject: SubjectAgentWildcard}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
startHandler(t, mustCoreConn(t, client))
|
||||
startHandler(t, client)
|
||||
|
||||
runner := &NATSRunner{
|
||||
Nats: client,
|
||||
Bus: client,
|
||||
Providers: map[string]Provider{"openai": {Class: "openai", APIKeyEnv: "TEST_KEY"}},
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
@@ -289,14 +283,3 @@ func TestNATSRunnerRoundTrip(t *testing.T) {
|
||||
t.Fatalf("expected 42 tokens, got %d", result.TokensUsed)
|
||||
}
|
||||
}
|
||||
|
||||
// mustCoreConn returns the raw NATS connection for tests that drive core-NATS
|
||||
// subscriptions directly, failing the test if the client is not connected.
|
||||
func mustCoreConn(t *testing.T, c *arnats.Client) *natsio.Conn {
|
||||
t.Helper()
|
||||
conn, err := c.CoreConn()
|
||||
if err != nil {
|
||||
t.Fatalf("CoreConn: %v", err)
|
||||
}
|
||||
return conn
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package agentexec
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Agent execution subjects.
|
||||
//
|
||||
// These belong to this package because it defines what an agent request and
|
||||
// response mean. They previously sat in the NATS package, which made the
|
||||
// transport the owner of Archie's agent protocol; see
|
||||
// docs/architecture/dependencies-and-contracts.md -- message schemas and the
|
||||
// subjects addressing them stay with the domain that defines them.
|
||||
//
|
||||
// The stage travels in the payload, not the subject: one subject per task
|
||||
// carrying sequential stage messages.
|
||||
const (
|
||||
// SubjectAgentWildcard matches every agent subject.
|
||||
SubjectAgentWildcard = "archie.agent.>"
|
||||
|
||||
subjectAgentPrefix = "archie.agent."
|
||||
)
|
||||
|
||||
// SubjectForRequest returns the subject carrying stage execution requests for
|
||||
// a task.
|
||||
func SubjectForRequest(taskID int64) string {
|
||||
return subjectForTask(taskID, "request")
|
||||
}
|
||||
|
||||
// SubjectForResponse returns the subject for agent output destined for human
|
||||
// channels. The daemon reviews these before forwarding to issue comments,
|
||||
// labels, or pull requests.
|
||||
func SubjectForResponse(taskID int64) string {
|
||||
return subjectForTask(taskID, "response")
|
||||
}
|
||||
|
||||
// SubjectForSystem returns the subject for internal agent messages such as
|
||||
// log dumps, health, and PII warnings. The daemon reads these for
|
||||
// observability and never forwards them.
|
||||
func SubjectForSystem(taskID int64) string {
|
||||
return subjectForTask(taskID, "system")
|
||||
}
|
||||
|
||||
// subjectForTask builds a per-task agent subject.
|
||||
func subjectForTask(taskID int64, kind string) string {
|
||||
return subjectAgentPrefix + strconv.FormatInt(taskID, 10) + "." + kind
|
||||
}
|
||||
+51
-24
@@ -15,16 +15,16 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"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/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
arnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
"github.com/samcharles93/archie-core/internal/memory"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
@@ -36,6 +36,18 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
)
|
||||
|
||||
// TaskBus is the messaging the daemon needs: announce discovered work,
|
||||
// collect it back, and ask a worker to run a task.
|
||||
//
|
||||
// Declared here rather than taking eventbus.Bus because a domain defines the
|
||||
// smallest interface required to do its work -- the daemon never subscribes
|
||||
// or creates reply inboxes.
|
||||
type TaskBus interface {
|
||||
PublishUnique(ctx context.Context, subject, idempotencyKey string, payload []byte) error
|
||||
Fetch(ctx context.Context) (eventbus.Message, error)
|
||||
Request(ctx context.Context, subject string, payload []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
type Daemon struct {
|
||||
Cfg config.Config
|
||||
Store store.TaskStore
|
||||
@@ -46,9 +58,9 @@ type Daemon struct {
|
||||
Bus *events.Bus
|
||||
Workflows workflow.Registry
|
||||
Log *slog.Logger
|
||||
// Nats is the optional NATS client for task distribution. Nil means
|
||||
// NATS is not configured; the existing SQLite ClaimNext flow is used.
|
||||
Nats *arnats.Client
|
||||
// Tasks is the optional message bus for task distribution. Nil means no
|
||||
// bus is configured; the existing SQLite ClaimNext flow is used.
|
||||
Tasks TaskBus
|
||||
// TaskRunReadyTimeout bounds how long runViaAgent retries an initial
|
||||
// taskrun request that fails with nats.ErrNoResponders, giving a
|
||||
// freshly spawned archie-agent container time to connect to NATS, set
|
||||
@@ -227,7 +239,7 @@ func (d *Daemon) cycleForIdentity(ctx context.Context, id *IdentityRunner) {
|
||||
issues := d.pollIssuesWithForge(ctx, id.Forge, repo)
|
||||
for _, is := range issues {
|
||||
labels := strings.Join(is.Labels, ",")
|
||||
if d.Nats != nil {
|
||||
if d.Tasks != nil {
|
||||
d.pollNATS(ctx, id.Forge, repo, is, labels, id.Name)
|
||||
} else {
|
||||
d.pollSQLite(ctx, id.Forge, repo, is, labels, id.Name)
|
||||
@@ -236,7 +248,7 @@ func (d *Daemon) cycleForIdentity(ctx context.Context, id *IdentityRunner) {
|
||||
}
|
||||
// Draining is shared: ClaimNext returns the oldest queued task
|
||||
// regardless of which identity enqueued it.
|
||||
if d.Nats != nil {
|
||||
if d.Tasks != nil {
|
||||
d.drainNATS(ctx)
|
||||
} else {
|
||||
d.drainSQLite(ctx)
|
||||
@@ -300,7 +312,7 @@ func (d *Daemon) Cycle(ctx context.Context) {
|
||||
d.poll(ctx)
|
||||
d.reconcilePRs(ctx)
|
||||
d.checkWaiting(ctx)
|
||||
if d.Nats != nil {
|
||||
if d.Tasks != nil {
|
||||
d.drainNATS(ctx)
|
||||
} else {
|
||||
d.drainSQLite(ctx)
|
||||
@@ -346,8 +358,8 @@ func (d *Daemon) drainSQLite(ctx context.Context) {
|
||||
func (d *Daemon) drainNATS(ctx context.Context) {
|
||||
dispatcher := newTaskDispatcher(d.Cfg.Containers.MaxConcurrency, d.allowConcurrentFor)
|
||||
for ctx.Err() == nil {
|
||||
msg, err := d.Nats.Fetch(ctx)
|
||||
if err != nil && !errors.Is(err, arnats.ErrNoMessage) {
|
||||
msg, err := d.Tasks.Fetch(ctx)
|
||||
if err != nil && !errors.Is(err, eventbus.ErrNoMessage) {
|
||||
d.Log.Error("nats fetch failed", "err", err)
|
||||
break
|
||||
}
|
||||
@@ -376,8 +388,8 @@ func (d *Daemon) drainNATS(ctx context.Context) {
|
||||
// submitNATSTask decodes a fetched message and queues it for processing. A
|
||||
// message that cannot be decoded is acked rather than redelivered: it will
|
||||
// never decode on a retry, so leaving it unacked would block the queue.
|
||||
func (d *Daemon) submitNATSTask(ctx context.Context, dispatcher *taskDispatcher, msg arnats.Message) {
|
||||
envelope, err := msg.Task()
|
||||
func (d *Daemon) submitNATSTask(ctx context.Context, dispatcher *taskDispatcher, msg eventbus.Message) {
|
||||
envelope, err := workintake.DecodeTask(msg.Data())
|
||||
if err != nil {
|
||||
d.Log.Error("nats decode failed", "subject", msg.Subject(), "err", err)
|
||||
_ = msg.Ack()
|
||||
@@ -465,7 +477,7 @@ func (d *Daemon) poll(ctx context.Context) {
|
||||
issues := d.pollIssues(ctx, repo)
|
||||
for _, is := range issues {
|
||||
labels := strings.Join(is.Labels, ",")
|
||||
if d.Nats != nil {
|
||||
if d.Tasks != nil {
|
||||
d.pollNATS(ctx, d.Forge, repo, is, labels, "")
|
||||
} else {
|
||||
d.pollSQLite(ctx, d.Forge, repo, is, labels, "")
|
||||
@@ -505,10 +517,10 @@ func (d *Daemon) pollNATS(ctx context.Context, fg forge.Forge, repo config.Repo,
|
||||
d.maybeRetryParked(ctx, repo, is)
|
||||
return
|
||||
}
|
||||
// Classify here, not in the bus: the label vocabulary belongs to the
|
||||
// workflow package, which routes on the same kinds.
|
||||
parsed := workflow.SplitLabels(labels)
|
||||
if err := d.Nats.PublishTask(ctx, arnats.TaskEnvelope{
|
||||
// Classify and encode here: the envelope, its routing kind and its
|
||||
// subject belong to the work-intake domain, not to the transport.
|
||||
parsed := workintake.SplitLabels(labels)
|
||||
task := workintake.TaskEnvelope{
|
||||
Owner: repo.Owner,
|
||||
Repo: repo.Name,
|
||||
Number: is.Number,
|
||||
@@ -516,14 +528,29 @@ func (d *Daemon) pollNATS(ctx context.Context, fg forge.Forge, repo config.Repo,
|
||||
Body: is.Body,
|
||||
Labels: parsed,
|
||||
Identity: identity,
|
||||
Kind: arnats.TaskKind(workflow.KindForLabels(parsed)),
|
||||
}); err != nil {
|
||||
d.Log.Error("nats publish failed", "repo", repo.FullName(), "issue", is.Number, "err", err)
|
||||
Kind: workintake.KindForLabels(parsed),
|
||||
}
|
||||
if err := d.publishTask(ctx, task); err != nil {
|
||||
d.Log.Error("task publish failed", "repo", repo.FullName(), "issue", is.Number, "err", err)
|
||||
return
|
||||
}
|
||||
d.acknowledge(ctx, fg, repo, is)
|
||||
}
|
||||
|
||||
// publishTask validates, encodes and publishes a task envelope. The
|
||||
// idempotency key means rediscovering the same issue on a later poll does not
|
||||
// enqueue the work twice.
|
||||
func (d *Daemon) publishTask(ctx context.Context, task workintake.TaskEnvelope) error {
|
||||
if err := task.Kind.Validate(); err != nil {
|
||||
return fmt.Errorf("publish task %s: %w", task.Ref(), err)
|
||||
}
|
||||
payload, err := task.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.Tasks.PublishUnique(ctx, task.Subject(), task.IdempotencyKey(), payload)
|
||||
}
|
||||
|
||||
// acknowledge posts the pickup reaction, state label, and queued event
|
||||
// using fg -- the forge client that owns repo (identity-scoped or root).
|
||||
func (d *Daemon) acknowledge(ctx context.Context, fg forge.Forge, repo config.Repo, is forge.Issue) {
|
||||
@@ -543,8 +570,8 @@ func (d *Daemon) acknowledge(ctx context.Context, fg forge.Forge, repo config.Re
|
||||
// processNATSTask decodes a NATS message, writes it to SQLite, claims it,
|
||||
// and runs the workflow. The message is ack'd on terminal (park is a valid
|
||||
// outcome). Nak on transient errors so NATS redelivers.
|
||||
func (d *Daemon) processNATSTask(ctx context.Context, msg arnats.Message) {
|
||||
tm, err := msg.Task()
|
||||
func (d *Daemon) processNATSTask(ctx context.Context, msg eventbus.Message) {
|
||||
tm, err := workintake.DecodeTask(msg.Data())
|
||||
if err != nil {
|
||||
d.Log.Error("nats decode failed", "err", err)
|
||||
if err := msg.Ack(); err != nil {
|
||||
@@ -1098,11 +1125,11 @@ func (d *Daemon) requestTaskRun(ctx context.Context, taskID int64, data []byte)
|
||||
deadline := time.Now().Add(d.taskRunReadyTimeout())
|
||||
backoff := d.taskRunRetryBackoff()
|
||||
for {
|
||||
reply, err := d.Nats.Request(ctx, subject, data)
|
||||
reply, err := d.Tasks.Request(ctx, subject, data)
|
||||
if err == nil {
|
||||
return reply, nil
|
||||
}
|
||||
if !errors.Is(err, natsio.ErrNoResponders) || !time.Now().Before(deadline) {
|
||||
if !errors.Is(err, eventbus.ErrNoResponders) || !time.Now().Before(deadline) {
|
||||
return nil, err
|
||||
}
|
||||
select {
|
||||
|
||||
@@ -15,7 +15,9 @@ import (
|
||||
natssrv "github.com/nats-io/nats-server/v2/test"
|
||||
natsio "github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
arnats "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
@@ -270,10 +272,13 @@ func startEmbeddedNATSForDaemon(t *testing.T) *server.Server {
|
||||
return srv
|
||||
}
|
||||
|
||||
func daemonWithNATS(t *testing.T) (*Daemon, *store.Store) {
|
||||
// daemonWithNATS returns the daemon, its store, and the concrete bus
|
||||
// client -- tests that drive core-NATS subscriptions directly need the
|
||||
// connection, which the TaskBus contract deliberately does not expose.
|
||||
func daemonWithNATS(t *testing.T) (*Daemon, *store.Store, *arnats.Client) {
|
||||
t.Helper()
|
||||
srv := startEmbeddedNATSForDaemon(t)
|
||||
client, err := arnats.Connect(context.Background(), arnats.Config{URL: srv.ClientURL()}, slog.New(slog.DiscardHandler))
|
||||
client, err := arnats.Connect(context.Background(), arnats.Config{URL: srv.ClientURL(), Subjects: []string{workintake.SubjectTaskWildcard, agentexec.SubjectAgentWildcard}, FilterSubject: workintake.SubjectTaskWildcard}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("nats connect: %v", err)
|
||||
}
|
||||
@@ -287,14 +292,14 @@ func daemonWithNATS(t *testing.T) (*Daemon, *store.Store) {
|
||||
}},
|
||||
},
|
||||
Store: s,
|
||||
Nats: client,
|
||||
Tasks: client,
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
return d, s
|
||||
return d, s, client
|
||||
}
|
||||
|
||||
func TestRunViaAgentParksOnRequestFailure(t *testing.T) {
|
||||
d, s := daemonWithNATS(t)
|
||||
d, s, _ := daemonWithNATS(t)
|
||||
// Bound the no-responders retry window tightly so this test proves
|
||||
// "no archie-agent ever showed up" parks the task, without waiting out
|
||||
// the real production default (20s).
|
||||
@@ -335,7 +340,7 @@ func TestRunViaAgentParksOnRequestFailure(t *testing.T) {
|
||||
// registers shortly after -- and asserts the retry recovers instead of
|
||||
// parking.
|
||||
func TestRunViaAgentRetriesUntilResponderAppears(t *testing.T) {
|
||||
d, s := daemonWithNATS(t)
|
||||
d, s, busClient := daemonWithNATS(t)
|
||||
d.TaskRunReadyTimeout = time.Second
|
||||
d.TaskRunRetryBackoff = 20 * time.Millisecond
|
||||
ctx := context.Background()
|
||||
@@ -353,7 +358,7 @@ func TestRunViaAgentRetriesUntilResponderAppears(t *testing.T) {
|
||||
// this retry loop exists to survive.
|
||||
go func() {
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
coreConn, connErr := d.Nats.CoreConn()
|
||||
coreConn, connErr := busClient.CoreConn()
|
||||
if connErr != nil {
|
||||
// Fatalf from a non-test goroutine does not stop the test.
|
||||
t.Errorf("CoreConn: %v", connErr)
|
||||
@@ -386,7 +391,7 @@ func TestRunViaAgentRetriesUntilResponderAppears(t *testing.T) {
|
||||
// no-responders case: it must return promptly (well under the retry
|
||||
// window) rather than looping through backoff attempts.
|
||||
func TestRunViaAgentDoesNotRetryOnContextCancellation(t *testing.T) {
|
||||
d, s := daemonWithNATS(t)
|
||||
d, s, _ := daemonWithNATS(t)
|
||||
d.TaskRunReadyTimeout = 5 * time.Second
|
||||
d.TaskRunRetryBackoff = 500 * time.Millisecond
|
||||
|
||||
@@ -409,7 +414,7 @@ func TestRunViaAgentDoesNotRetryOnContextCancellation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunViaAgentParksOnRunError(t *testing.T) {
|
||||
d, s := daemonWithNATS(t)
|
||||
d, s, busClient := daemonWithNATS(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 2, "t", "b", "", ""); err != nil {
|
||||
@@ -420,7 +425,7 @@ func TestRunViaAgentParksOnRunError(t *testing.T) {
|
||||
t.Fatalf("claim: (%v, %v)", task, err)
|
||||
}
|
||||
|
||||
sub, err := mustCoreConn(t, d.Nats).Subscribe(taskrun.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
sub, err := mustCoreConn(t, busClient).Subscribe(taskrun.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
data, _ := json.Marshal(taskrun.Response{Error: "registry build failed"})
|
||||
_ = msg.Respond(data)
|
||||
})
|
||||
@@ -441,7 +446,7 @@ func TestRunViaAgentParksOnRunError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunViaAgentSendsExpectedRequest(t *testing.T) {
|
||||
d, s := daemonWithNATS(t)
|
||||
d, s, busClient := daemonWithNATS(t)
|
||||
d.Cfg.DiffCapLines = 999
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -454,7 +459,7 @@ func TestRunViaAgentSendsExpectedRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
received := make(chan taskrun.Request, 1)
|
||||
sub, err := mustCoreConn(t, d.Nats).Subscribe(taskrun.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
sub, err := mustCoreConn(t, busClient).Subscribe(taskrun.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
var req taskrun.Request
|
||||
_ = json.Unmarshal(msg.Data, &req)
|
||||
received <- req
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
// Package workintake owns the promotion of a discovered external issue into
|
||||
// durable, workflow-backed work.
|
||||
//
|
||||
// It owns the vocabulary of that promotion -- the task envelope, its routing
|
||||
// kind, and the subjects it is addressed to -- because the domain that
|
||||
// defines a message's meaning owns its schema. These previously lived in the
|
||||
// NATS package, which made the transport responsible for knowing what a unit
|
||||
// of work was; see docs/architecture/dependencies-and-contracts.md.
|
||||
//
|
||||
// This package names no broker. It produces and consumes bytes; carrying
|
||||
// them is internal/eventbus's job.
|
||||
package workintake
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// dedupKeyPrefix namespaces idempotency keys so they cannot collide with
|
||||
// another producer's on a shared broker.
|
||||
const dedupKeyPrefix = "archie:"
|
||||
|
||||
// TaskEnvelope is the wire payload announcing a discovered issue.
|
||||
//
|
||||
// Publishing once took seven positional parameters, six of them strings,
|
||||
// which made a transposed argument a silent bug. Callers build this struct.
|
||||
type TaskEnvelope struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
|
||||
// Labels are the issue's labels. On the wire this stays a
|
||||
// comma-separated string for compatibility with already-queued messages,
|
||||
// but in Go it is a slice.
|
||||
Labels []string `json:"-"`
|
||||
|
||||
// LabelsRaw is the comma-separated wire form, kept in sync by the JSON
|
||||
// methods. Callers should use Labels.
|
||||
LabelsRaw string `json:"labels"`
|
||||
|
||||
// Identity is the archie identity whose forge poll discovered the issue,
|
||||
// empty for single-identity deployments. Carried so the consuming daemon
|
||||
// enqueues the task under the right owner.
|
||||
Identity string `json:"identity,omitempty"`
|
||||
|
||||
// Kind is the routing category, chosen by the publisher. Empty means
|
||||
// KindDefault, which is also what messages queued before this field
|
||||
// existed decode to.
|
||||
Kind Kind `json:"kind,omitempty"`
|
||||
}
|
||||
|
||||
// envelopeWire avoids infinite recursion in the JSON methods below.
|
||||
type envelopeWire TaskEnvelope
|
||||
|
||||
// MarshalJSON flattens Labels into the comma-separated wire field.
|
||||
func (t TaskEnvelope) MarshalJSON() ([]byte, error) {
|
||||
t.LabelsRaw = strings.Join(t.Labels, ",")
|
||||
data, err := json.Marshal(envelopeWire(t))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal task envelope: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON expands the comma-separated wire field into Labels.
|
||||
func (t *TaskEnvelope) UnmarshalJSON(data []byte) error {
|
||||
var wire envelopeWire
|
||||
if err := json.Unmarshal(data, &wire); err != nil {
|
||||
return fmt.Errorf("unmarshal task envelope: %w", err)
|
||||
}
|
||||
*t = TaskEnvelope(wire)
|
||||
t.Labels = SplitLabels(t.LabelsRaw)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Encode renders the envelope for transport.
|
||||
func (t TaskEnvelope) Encode() ([]byte, error) {
|
||||
data, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode task %s: %w", t.Ref(), err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// DecodeTask parses a transported envelope.
|
||||
func DecodeTask(data []byte) (TaskEnvelope, error) {
|
||||
var envelope TaskEnvelope
|
||||
if err := json.Unmarshal(data, &envelope); err != nil {
|
||||
return TaskEnvelope{}, fmt.Errorf("decode task envelope: %w", err)
|
||||
}
|
||||
return envelope, nil
|
||||
}
|
||||
|
||||
// Ref identifies the issue for logs and error messages.
|
||||
func (t TaskEnvelope) Ref() string {
|
||||
return t.Owner + "/" + t.Repo + "#" + strconv.Itoa(t.Number)
|
||||
}
|
||||
|
||||
// IdempotencyKey identifies this issue for delivery deduplication, so
|
||||
// rediscovering it on a later poll does not enqueue the same work twice.
|
||||
func (t TaskEnvelope) IdempotencyKey() string {
|
||||
return dedupKeyPrefix + t.Owner + "/" + t.Repo + "/" + strconv.Itoa(t.Number)
|
||||
}
|
||||
|
||||
// Subject returns the address this envelope routes to.
|
||||
func (t TaskEnvelope) Subject() string { return SubjectForKind(t.Kind) }
|
||||
|
||||
// SplitLabels parses the comma-separated wire form, dropping empties so
|
||||
// "bug,,feature" and a trailing comma do not produce blank labels.
|
||||
func SplitLabels(raw string) []string {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(raw, ",")
|
||||
labels := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if trimmed := strings.TrimSpace(part); trimmed != "" {
|
||||
labels = append(labels, trimmed)
|
||||
}
|
||||
}
|
||||
if len(labels) == 0 {
|
||||
return nil
|
||||
}
|
||||
return labels
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package workintake
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Task subjects. Each encodes the routing kind so a multi-daemon deployment
|
||||
// can filter by the work it wants.
|
||||
const (
|
||||
SubjectTaskBug = "archie.task.bug"
|
||||
SubjectTaskFeature = "archie.task.feature"
|
||||
SubjectTaskBootstrap = "archie.task.bootstrap"
|
||||
SubjectTaskDefault = "archie.task.default"
|
||||
|
||||
// SubjectTaskWildcard matches every task subject.
|
||||
SubjectTaskWildcard = "archie.task.>"
|
||||
)
|
||||
|
||||
// ErrUnknownKind reports a kind with no subject. Routing it to the default
|
||||
// queue instead would silently misdeliver work.
|
||||
var ErrUnknownKind = errors.New("workintake: unknown task kind")
|
||||
|
||||
// Kind is the routing category an issue's labels select.
|
||||
//
|
||||
// It is the single axis both the workflow registry and the task subjects
|
||||
// route on. The label table was once written twice -- a switch in
|
||||
// workflow.Route and a subject lookup in the NATS package -- so adding a
|
||||
// label meant remembering both, and the transport had to know what a forge
|
||||
// label was.
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
KindBug Kind = "bug"
|
||||
KindFeature Kind = "feature"
|
||||
KindBootstrap Kind = "bootstrap"
|
||||
|
||||
// KindDefault routes issues carrying no recognised label.
|
||||
KindDefault Kind = "default"
|
||||
)
|
||||
|
||||
// labelKinds maps a forge issue label to its routing kind. This is the one
|
||||
// place the label vocabulary is defined.
|
||||
var labelKinds = map[string]Kind{
|
||||
"bug": KindBug,
|
||||
"feature": KindFeature,
|
||||
"bootstrap": KindBootstrap,
|
||||
}
|
||||
|
||||
// kindSubjects is the closed set of routable kinds.
|
||||
var kindSubjects = map[Kind]string{
|
||||
KindBug: SubjectTaskBug,
|
||||
KindFeature: SubjectTaskFeature,
|
||||
KindBootstrap: SubjectTaskBootstrap,
|
||||
KindDefault: SubjectTaskDefault,
|
||||
}
|
||||
|
||||
// Validate reports whether the kind is routable. The zero kind is accepted
|
||||
// and means KindDefault, so a caller that does not classify still publishes.
|
||||
func (k Kind) Validate() error {
|
||||
if k == "" {
|
||||
return nil
|
||||
}
|
||||
if _, ok := kindSubjects[k]; !ok {
|
||||
return fmt.Errorf("%w: %q", ErrUnknownKind, string(k))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SubjectForKind renders a kind as its task subject. The zero and any
|
||||
// unrecognised kind render as SubjectTaskDefault so a message is never
|
||||
// addressed to an unroutable subject; publishers validate first.
|
||||
func SubjectForKind(kind Kind) string {
|
||||
if subject, ok := kindSubjects[kind]; ok {
|
||||
return subject
|
||||
}
|
||||
return SubjectTaskDefault
|
||||
}
|
||||
|
||||
// KindForLabels returns the routing kind for a set of issue labels. The first
|
||||
// recognised label wins; an empty or unrecognised set is KindDefault.
|
||||
func KindForLabels(labels []string) Kind {
|
||||
for _, kind := range KindsForLabels(labels) {
|
||||
return kind
|
||||
}
|
||||
return KindDefault
|
||||
}
|
||||
|
||||
// KindsForLabels returns every recognised kind in label order.
|
||||
//
|
||||
// Workflow routing needs the full ordered set rather than just the first:
|
||||
// when a task is labelled "bug,feature" and no "tdd" workflow is registered,
|
||||
// it must still fall through to "feasibility" rather than to the default.
|
||||
func KindsForLabels(labels []string) []Kind {
|
||||
var kinds []Kind
|
||||
for _, label := range labels {
|
||||
if kind, ok := labelKinds[strings.TrimSpace(label)]; ok {
|
||||
kinds = append(kinds, kind)
|
||||
}
|
||||
}
|
||||
return kinds
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Package eventbus is the broker-neutral messaging contract.
|
||||
//
|
||||
// It defines only the messaging behaviour Archie requires: publication,
|
||||
// subscription, request/reply, and delivery outcomes. It names no broker.
|
||||
// NATS subjects, JetStream configuration, consumer groups, acknowledgement
|
||||
// mechanics, reconnection and serialization live in
|
||||
// internal/infrastructure/eventbus/nats, which implements these interfaces.
|
||||
//
|
||||
// Per docs/architecture/dependencies-and-contracts.md, domains depend on this
|
||||
// package and never on an infrastructure implementation; only application
|
||||
// composition chooses a concrete broker.
|
||||
//
|
||||
// # The bus does not own message meaning
|
||||
//
|
||||
// This package deliberately transports opaque payloads. Message schemas
|
||||
// belong to the domain that defines them -- work intake owns its task
|
||||
// envelope, agent execution owns its request and response types -- and each
|
||||
// domain owns the subjects it addresses. A previous version of the NATS
|
||||
// package exported a TaskEnvelope type and a PublishTask method, which made
|
||||
// the transport responsible for knowing what a unit of work was.
|
||||
//
|
||||
// Subjects are plain strings here. Constraining their syntax would encode one
|
||||
// broker's addressing rules into the contract.
|
||||
package eventbus
|
||||
@@ -0,0 +1,123 @@
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Sentinel outcomes callers match with errors.Is. These are bus semantics,
|
||||
// not broker details, so they belong in the contract: an implementation that
|
||||
// cannot express them cannot satisfy it.
|
||||
var (
|
||||
// ErrNoMessage reports that a fetch completed normally with nothing
|
||||
// queued. Polling an idle queue is an expected outcome, not a failure.
|
||||
ErrNoMessage = errors.New("eventbus: no message available")
|
||||
|
||||
// ErrNotConnected reports use of a bus whose connection is closed or was
|
||||
// never established.
|
||||
ErrNotConnected = errors.New("eventbus: not connected")
|
||||
|
||||
// ErrNoReplyAddress reports a message carrying nowhere to send a reply,
|
||||
// so a request cannot be answered.
|
||||
ErrNoReplyAddress = errors.New("eventbus: message has no reply address")
|
||||
|
||||
// ErrNoResponders reports a request sent to a subject nobody is
|
||||
// listening on. It is usually transient -- a worker that has connected
|
||||
// but not yet subscribed -- so callers typically retry within a deadline
|
||||
// rather than failing immediately.
|
||||
ErrNoResponders = errors.New("eventbus: no responders")
|
||||
)
|
||||
|
||||
// Message is one delivered message. The payload is opaque: decoding it is the
|
||||
// owning domain's job.
|
||||
type Message interface {
|
||||
// Data returns the raw payload.
|
||||
Data() []byte
|
||||
|
||||
// Subject returns the address the message arrived on.
|
||||
Subject() string
|
||||
|
||||
// ReplyAddress returns where a response should be sent, or
|
||||
// ErrNoReplyAddress when the sender expects no reply. How the address
|
||||
// travels is the implementation's concern.
|
||||
ReplyAddress() (string, error)
|
||||
|
||||
// Ack marks the message handled so it is not redelivered.
|
||||
Ack() error
|
||||
|
||||
// Nak returns the message for redelivery.
|
||||
Nak() error
|
||||
}
|
||||
|
||||
// Publisher sends messages.
|
||||
type Publisher interface {
|
||||
// Publish sends payload to subject.
|
||||
Publish(ctx context.Context, subject string, payload []byte) error
|
||||
|
||||
// PublishUnique sends payload to subject carrying an idempotency key.
|
||||
// Republishing the same key within the implementation's deduplication
|
||||
// window is suppressed.
|
||||
//
|
||||
// This is a required semantic rather than a broker detail: work intake
|
||||
// rediscovers the same issue on every poll, so at-least-once delivery
|
||||
// without deduplication would enqueue it repeatedly.
|
||||
PublishUnique(ctx context.Context, subject, idempotencyKey string, payload []byte) error
|
||||
|
||||
// PublishRequest sends payload to subject, recording replyTo as the
|
||||
// address for the response. It reports ErrNoReplyAddress when replyTo is
|
||||
// empty, since a request nobody can answer is a caller error rather than
|
||||
// a fire-and-forget publish.
|
||||
PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error
|
||||
|
||||
// Respond answers a request on the address taken from
|
||||
// Message.ReplyAddress.
|
||||
//
|
||||
// This is distinct from Publish, not a convenience over it: a reply
|
||||
// address is an ephemeral inbox belonging to one waiting caller, not a
|
||||
// durable subject. Sending a reply through the durable path fails,
|
||||
// because the inbox is not part of any stream.
|
||||
Respond(ctx context.Context, replyAddress string, payload []byte) error
|
||||
}
|
||||
|
||||
// Consumer receives messages.
|
||||
type Consumer interface {
|
||||
// Fetch returns the next message, or ErrNoMessage if none arrives before
|
||||
// the implementation's poll deadline.
|
||||
Fetch(ctx context.Context) (Message, error)
|
||||
|
||||
// Subscribe delivers messages matching filterSubject to handle until ctx
|
||||
// is done. A handler returning an error causes redelivery.
|
||||
Subscribe(ctx context.Context, filterSubject string, handle func(Message) error) error
|
||||
}
|
||||
|
||||
// Requester performs request/reply.
|
||||
type Requester interface {
|
||||
// Request sends payload to subject and waits for a single response.
|
||||
Request(ctx context.Context, subject string, payload []byte) ([]byte, error)
|
||||
|
||||
// NewReplyInbox creates an address for one expected response. Use it when
|
||||
// the reply must be awaited separately from sending, which Request cannot
|
||||
// express -- publishing work and collecting its result are distinct steps
|
||||
// when the responder may not exist yet.
|
||||
NewReplyInbox() (ReplyInbox, error)
|
||||
}
|
||||
|
||||
// ReplyInbox is a one-shot address awaiting a single response.
|
||||
type ReplyInbox interface {
|
||||
// Subject is the address to advertise to the responder.
|
||||
Subject() string
|
||||
|
||||
// Next blocks until the reply arrives or ctx is done.
|
||||
Next(ctx context.Context) ([]byte, error)
|
||||
|
||||
// Close releases the inbox.
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Bus is the full contract. Depend on the narrowest interface that fits:
|
||||
// a publisher-only caller should accept Publisher, not Bus.
|
||||
type Bus interface {
|
||||
Publisher
|
||||
Consumer
|
||||
Requester
|
||||
}
|
||||
@@ -7,35 +7,14 @@ import (
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// Publisher publishes messages onto the bus. Declared as an interface so
|
||||
// callers depend on behaviour rather than *Client, and can be tested without a
|
||||
// broker -- the previous package exported only a concrete struct.
|
||||
type Publisher interface {
|
||||
PublishTask(ctx context.Context, task TaskEnvelope) error
|
||||
PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error
|
||||
Publish(ctx context.Context, subject string, payload []byte) error
|
||||
}
|
||||
|
||||
// Consumer reads messages from the bus.
|
||||
type Consumer interface {
|
||||
// Fetch returns the next message, or ErrNoMessage if none arrives before
|
||||
// the configured poll timeout.
|
||||
Fetch(ctx context.Context) (Message, error)
|
||||
}
|
||||
|
||||
// Requester performs core-NATS request/reply outside JetStream.
|
||||
type Requester interface {
|
||||
NewReplyInbox() (ReplyInbox, error)
|
||||
}
|
||||
|
||||
// Compile-time proof that Client satisfies every boundary interface.
|
||||
var (
|
||||
_ Publisher = (*Client)(nil)
|
||||
_ Consumer = (*Client)(nil)
|
||||
_ Requester = (*Client)(nil)
|
||||
)
|
||||
// Compile-time proof that Client satisfies the broker-neutral contract.
|
||||
// The interfaces live in internal/eventbus so domains can depend on the
|
||||
// behaviour without importing this package.
|
||||
var _ eventbus.Bus = (*Client)(nil)
|
||||
|
||||
// Client owns the NATS connection and its JetStream stream and consumer.
|
||||
type Client struct {
|
||||
@@ -81,7 +60,7 @@ func Connect(ctx context.Context, cfg Config, log *slog.Logger) (*Client, error)
|
||||
|
||||
stream, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{
|
||||
Name: cfg.StreamName,
|
||||
Subjects: []string{SubjectTaskWildcard, SubjectAgentWildcard},
|
||||
Subjects: cfg.Subjects,
|
||||
Storage: jetstream.FileStorage,
|
||||
Retention: jetstream.WorkQueuePolicy,
|
||||
Duplicates: cfg.DedupWindow,
|
||||
@@ -93,7 +72,7 @@ func Connect(ctx context.Context, cfg Config, log *slog.Logger) (*Client, error)
|
||||
consumer, err := stream.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig{
|
||||
Name: cfg.ConsumerName,
|
||||
Durable: cfg.ConsumerName,
|
||||
FilterSubject: SubjectTaskWildcard,
|
||||
FilterSubject: cfg.FilterSubject,
|
||||
AckPolicy: jetstream.AckExplicitPolicy,
|
||||
MaxDeliver: cfg.MaxDeliver,
|
||||
AckWait: cfg.AckWait,
|
||||
@@ -156,7 +135,7 @@ func (c *Client) CoreConn() (*nats.Conn, error) { return c.connection() }
|
||||
// connection returns the live connection, or ErrNotConnected.
|
||||
func (c *Client) connection() (*nats.Conn, error) {
|
||||
if c == nil || c.conn == nil || c.conn.IsClosed() {
|
||||
return nil, ErrNotConnected
|
||||
return nil, eventbus.ErrNotConnected
|
||||
}
|
||||
return c.conn, nil
|
||||
}
|
||||
@@ -15,10 +15,6 @@ const (
|
||||
DefaultAckWait = 5 * time.Minute
|
||||
DefaultMaxDeliver = 3
|
||||
DefaultInactiveTTL = 24 * time.Hour
|
||||
|
||||
// dedupKeyPrefix namespaces the Nats-Msg-Id used for JetStream dedup so
|
||||
// archie's keys cannot collide with another publisher on a shared server.
|
||||
dedupKeyPrefix = "archie:"
|
||||
)
|
||||
|
||||
// Config describes how to reach NATS and how the stream and consumer should
|
||||
@@ -31,12 +27,25 @@ type Config struct {
|
||||
// Token authenticates the connection. Empty means no token auth.
|
||||
Token string
|
||||
|
||||
// StreamName is the JetStream stream holding task and agent subjects.
|
||||
// StreamName is the JetStream stream holding the bound subjects.
|
||||
StreamName string
|
||||
|
||||
// ConsumerName is the durable pull consumer the daemon reads tasks from.
|
||||
// Subjects are the subjects the stream carries. Required.
|
||||
//
|
||||
// Composition supplies these rather than the package hardcoding them: the
|
||||
// bus must not know which subjects belong to which domain, and archied
|
||||
// and archie-agent previously declared the same stream separately, free
|
||||
// to disagree about its subjects, retention and dedup window.
|
||||
Subjects []string
|
||||
|
||||
// ConsumerName is the durable pull consumer Fetch reads from.
|
||||
ConsumerName string
|
||||
|
||||
// FilterSubject restricts that consumer to a subset of the stream --
|
||||
// archied consumes task subjects, archie-agent consumes agent subjects.
|
||||
// Required.
|
||||
FilterSubject string
|
||||
|
||||
// DedupWindow is how long JetStream remembers a Nats-Msg-Id, suppressing
|
||||
// republished duplicates of the same issue within the window.
|
||||
DedupWindow time.Duration
|
||||
@@ -60,6 +69,12 @@ func (c Config) Validate() error {
|
||||
if c.URL == "" {
|
||||
return fmt.Errorf("%w: URL is required", ErrInvalidConfig)
|
||||
}
|
||||
if len(c.Subjects) == 0 {
|
||||
return fmt.Errorf("%w: Subjects is required", ErrInvalidConfig)
|
||||
}
|
||||
if c.FilterSubject == "" {
|
||||
return fmt.Errorf("%w: FilterSubject is required", ErrInvalidConfig)
|
||||
}
|
||||
if c.PollTimeout < 0 || c.AckWait < 0 || c.DedupWindow < 0 {
|
||||
return fmt.Errorf("%w: durations must not be negative", ErrInvalidConfig)
|
||||
}
|
||||
|
||||
@@ -5,21 +5,23 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// Fetch returns the next task message from the pull consumer.
|
||||
// Fetch returns the next message from the pull consumer.
|
||||
//
|
||||
// It reports ErrNoMessage when the poll timeout expires with nothing queued.
|
||||
// The previous API returned (nil, nil) in that case, which every caller had to
|
||||
// remember to distinguish from a real message.
|
||||
func (c *Client) Fetch(ctx context.Context) (Message, error) {
|
||||
// It reports eventbus.ErrNoMessage when the poll timeout expires with nothing
|
||||
// queued. The original API returned (nil, nil) in that case, which every
|
||||
// caller had to remember to distinguish from a real message.
|
||||
func (c *Client) Fetch(ctx context.Context) (eventbus.Message, error) {
|
||||
if _, err := c.connection(); err != nil {
|
||||
return Message{}, fmt.Errorf("fetch: %w", err)
|
||||
return nil, fmt.Errorf("fetch: %w", err)
|
||||
}
|
||||
|
||||
batch, err := c.consumer.Fetch(1, jetstream.FetchMaxWait(c.cfg.PollTimeout))
|
||||
if err != nil {
|
||||
return Message{}, fmt.Errorf("fetch from %s: %w", c.cfg.ConsumerName, err)
|
||||
return nil, fmt.Errorf("fetch from %s: %w", c.cfg.ConsumerName, err)
|
||||
}
|
||||
|
||||
for msg := range batch.Messages() {
|
||||
@@ -30,21 +32,21 @@ func (c *Client) Fetch(ctx context.Context) (Message, error) {
|
||||
// redelivers it rather than losing it to a shutting-down daemon.
|
||||
if err := ctx.Err(); err != nil {
|
||||
_ = msg.Nak()
|
||||
return Message{}, fmt.Errorf("fetch from %s: %w", c.cfg.ConsumerName, err)
|
||||
return nil, fmt.Errorf("fetch from %s: %w", c.cfg.ConsumerName, err)
|
||||
}
|
||||
return Message{msg: msg}, nil
|
||||
return message{msg: msg}, nil
|
||||
}
|
||||
|
||||
if err := batch.Error(); err != nil {
|
||||
return Message{}, fmt.Errorf("fetch batch from %s: %w", c.cfg.ConsumerName, err)
|
||||
return nil, fmt.Errorf("fetch batch from %s: %w", c.cfg.ConsumerName, err)
|
||||
}
|
||||
return Message{}, ErrNoMessage
|
||||
return nil, eventbus.ErrNoMessage
|
||||
}
|
||||
|
||||
// Subscribe delivers agent-subject messages to handle until ctx is done. It
|
||||
// exists so workers do not need the SDK's consume machinery; handle receives
|
||||
// the same boundary Message type as Fetch.
|
||||
func (c *Client) Subscribe(ctx context.Context, filterSubject string, handle func(Message) error) error {
|
||||
// Subscribe delivers messages matching filterSubject to handle until ctx is
|
||||
// done. It exists so callers do not need the SDK's consume machinery; handle
|
||||
// receives the same boundary type as Fetch.
|
||||
func (c *Client) Subscribe(ctx context.Context, filterSubject string, handle func(eventbus.Message) error) error {
|
||||
if _, err := c.connection(); err != nil {
|
||||
return fmt.Errorf("subscribe %s: %w", filterSubject, err)
|
||||
}
|
||||
@@ -60,7 +62,7 @@ func (c *Client) Subscribe(ctx context.Context, filterSubject string, handle fun
|
||||
}
|
||||
|
||||
consumeCtx, err := consumer.Consume(func(msg jetstream.Msg) {
|
||||
if err := handle(Message{msg: msg}); err != nil {
|
||||
if err := handle(message{msg: msg}); err != nil {
|
||||
c.log.Error("message handler failed",
|
||||
"subject", msg.Subject(), "error", err)
|
||||
_ = msg.Nak()
|
||||
|
||||
@@ -2,27 +2,10 @@ package nats
|
||||
|
||||
import "errors"
|
||||
|
||||
// Sentinel errors callers are expected to match with errors.Is. Anything else
|
||||
// returned by this package is wrapped with operation context via %w.
|
||||
var (
|
||||
// ErrNoMessage reports that a fetch completed normally but no message was
|
||||
// available before the poll deadline. It is an expected outcome of polling
|
||||
// an idle queue, not a failure -- the previous API signalled this with a
|
||||
// (nil, nil) return, which callers had to remember to check for.
|
||||
ErrNoMessage = errors.New("nats: no message available")
|
||||
|
||||
// ErrNotConnected reports use of a client whose connection is closed or
|
||||
// was never established.
|
||||
ErrNotConnected = errors.New("nats: not connected")
|
||||
|
||||
// ErrNoReplyAddress reports a request message that carried no reply
|
||||
// inbox in its headers, so no response can be routed back.
|
||||
ErrNoReplyAddress = errors.New("nats: message has no reply address")
|
||||
|
||||
// ErrInvalidConfig reports a Config that cannot produce a usable client.
|
||||
ErrInvalidConfig = errors.New("nats: invalid configuration")
|
||||
|
||||
// ErrUnknownTaskKind reports a publish whose TaskKind has no subject.
|
||||
// Routing to the default queue instead would silently misdeliver work.
|
||||
ErrUnknownTaskKind = errors.New("nats: unknown task kind")
|
||||
)
|
||||
// ErrInvalidConfig reports a Config that cannot produce a usable client.
|
||||
//
|
||||
// The delivery-outcome sentinels a caller matches on -- no message, not
|
||||
// connected, no reply address -- belong to the broker-neutral contract in
|
||||
// internal/eventbus, so a caller can handle them without knowing which broker
|
||||
// is in use. This package returns those, not copies of them.
|
||||
var ErrInvalidConfig = errors.New("nats: invalid configuration")
|
||||
@@ -1,125 +1,52 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// TaskEnvelope is the wire payload for a discovered issue.
|
||||
// replyHeader carries the requester's reply inbox.
|
||||
//
|
||||
// Publishing previously took seven positional parameters, six of them strings,
|
||||
// which made transposed arguments a silent bug. Callers now build this struct.
|
||||
type TaskEnvelope struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
// JetStream consumes a message's Reply field for its own PubAck, so the
|
||||
// address travels as a header instead. That is a NATS detail: the contract
|
||||
// exposes it as [eventbus.Message.ReplyAddress] and callers never name the
|
||||
// header.
|
||||
const replyHeader = "X-Archie-Reply"
|
||||
|
||||
// Labels are the issue's labels. On the wire this stays a
|
||||
// comma-separated string for compatibility with already-queued messages,
|
||||
// but in Go it is a slice -- the previous API passed a comma-joined
|
||||
// string and re-split it internally on every publish.
|
||||
Labels []string `json:"-"`
|
||||
// idempotencyHeader is JetStream's deduplication key. Messages republished
|
||||
// with the same value inside Config.DedupWindow are suppressed by the server.
|
||||
const idempotencyHeader = "Nats-Msg-Id"
|
||||
|
||||
// LabelsRaw is the comma-separated wire form. Kept in sync by MarshalJSON
|
||||
// and UnmarshalJSON; callers should use Labels.
|
||||
LabelsRaw string `json:"labels"`
|
||||
|
||||
// Identity is the archie identity whose forge poll discovered the issue,
|
||||
// empty for single-identity deployments. Carried so the consuming daemon
|
||||
// enqueues the task under the right owner.
|
||||
Identity string `json:"identity,omitempty"`
|
||||
|
||||
// Kind is the routing category, chosen by the publisher. Empty means
|
||||
// TaskKindDefault, which is also what messages queued before this field
|
||||
// existed decode to.
|
||||
Kind TaskKind `json:"kind,omitempty"`
|
||||
}
|
||||
|
||||
// taskEnvelopeWire avoids infinite recursion in the JSON methods below.
|
||||
type taskEnvelopeWire TaskEnvelope
|
||||
|
||||
// MarshalJSON flattens Labels into the comma-separated wire field.
|
||||
func (t TaskEnvelope) MarshalJSON() ([]byte, error) {
|
||||
t.LabelsRaw = strings.Join(t.Labels, ",")
|
||||
data, err := json.Marshal(taskEnvelopeWire(t))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal task envelope: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON expands the comma-separated wire field into Labels.
|
||||
func (t *TaskEnvelope) UnmarshalJSON(data []byte) error {
|
||||
var wire taskEnvelopeWire
|
||||
if err := json.Unmarshal(data, &wire); err != nil {
|
||||
return fmt.Errorf("unmarshal task envelope: %w", err)
|
||||
}
|
||||
*t = TaskEnvelope(wire)
|
||||
t.Labels = splitLabels(t.LabelsRaw)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DedupKey returns the JetStream Nats-Msg-Id for this task. Republishing the
|
||||
// same issue inside Config.DedupWindow is suppressed by the server.
|
||||
func (t TaskEnvelope) DedupKey() string {
|
||||
return fmt.Sprintf("%s%s/%s/%d", dedupKeyPrefix, t.Owner, t.Repo, t.Number)
|
||||
}
|
||||
|
||||
// Subject returns the task subject this envelope routes to.
|
||||
func (t TaskEnvelope) Subject() string { return SubjectForKind(t.Kind) }
|
||||
|
||||
// splitLabels parses the comma-separated wire form, dropping empty entries so
|
||||
// "bug,,feature" and a trailing comma do not produce blank labels.
|
||||
func splitLabels(raw string) []string {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(raw, ",")
|
||||
labels := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if trimmed := strings.TrimSpace(part); trimmed != "" {
|
||||
labels = append(labels, trimmed)
|
||||
}
|
||||
}
|
||||
if len(labels) == 0 {
|
||||
return nil
|
||||
}
|
||||
return labels
|
||||
}
|
||||
|
||||
// Message is a broker message with the SDK type kept behind the boundary.
|
||||
// message adapts a jetstream.Msg to the broker-neutral contract.
|
||||
//
|
||||
// Fetch previously handed callers a jetstream.Msg directly, so every consumer
|
||||
// imported the NATS SDK and the broker stopped being replaceable.
|
||||
type Message struct {
|
||||
// It is unexported: callers receive it as an eventbus.Message, so no consumer
|
||||
// can reach the SDK type through it. Fetch once returned jetstream.Msg
|
||||
// directly, which put the NATS SDK in every consumer's imports.
|
||||
type message struct {
|
||||
msg jetstream.Msg
|
||||
}
|
||||
|
||||
var _ eventbus.Message = message{}
|
||||
|
||||
// Data returns the raw payload.
|
||||
func (m Message) Data() []byte { return m.msg.Data() }
|
||||
func (m message) Data() []byte { return m.msg.Data() }
|
||||
|
||||
// Subject returns the subject the message arrived on.
|
||||
func (m Message) Subject() string { return m.msg.Subject() }
|
||||
func (m message) Subject() string { return m.msg.Subject() }
|
||||
|
||||
// Header returns a single header value, or "" when absent.
|
||||
func (m Message) Header(key string) string { return m.msg.Headers().Get(key) }
|
||||
|
||||
// ReplyAddress returns the requester's reply inbox, or ErrNoReplyAddress when
|
||||
// the message carries none.
|
||||
func (m Message) ReplyAddress() (string, error) {
|
||||
if addr := m.Header(ReplyHeader); addr != "" {
|
||||
// ReplyAddress returns the requester's reply inbox.
|
||||
func (m message) ReplyAddress() (string, error) {
|
||||
if addr := m.msg.Headers().Get(replyHeader); addr != "" {
|
||||
return addr, nil
|
||||
}
|
||||
return "", fmt.Errorf("%w: header %s", ErrNoReplyAddress, ReplyHeader)
|
||||
return "", fmt.Errorf("%w: on %s", eventbus.ErrNoReplyAddress, m.Subject())
|
||||
}
|
||||
|
||||
// Ack marks the message handled so JetStream will not redeliver it.
|
||||
func (m Message) Ack() error {
|
||||
func (m message) Ack() error {
|
||||
if err := m.msg.Ack(); err != nil {
|
||||
return fmt.Errorf("ack message on %s: %w", m.Subject(), err)
|
||||
}
|
||||
@@ -127,18 +54,9 @@ func (m Message) Ack() error {
|
||||
}
|
||||
|
||||
// Nak returns the message for redelivery.
|
||||
func (m Message) Nak() error {
|
||||
func (m message) Nak() error {
|
||||
if err := m.msg.Nak(); err != nil {
|
||||
return fmt.Errorf("nak message on %s: %w", m.Subject(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Task decodes the message as a TaskEnvelope.
|
||||
func (m Message) Task() (TaskEnvelope, error) {
|
||||
var envelope TaskEnvelope
|
||||
if err := json.Unmarshal(m.Data(), &envelope); err != nil {
|
||||
return TaskEnvelope{}, fmt.Errorf("decode task on %s: %w", m.Subject(), err)
|
||||
}
|
||||
return envelope, nil
|
||||
}
|
||||
@@ -2,60 +2,56 @@ package nats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/eventbus"
|
||||
)
|
||||
|
||||
// PublishTask publishes a discovered issue to the subject its kind selects.
|
||||
// The dedup header suppresses republishing the same issue inside the
|
||||
// configured dedup window.
|
||||
func (c *Client) PublishTask(ctx context.Context, task TaskEnvelope) error {
|
||||
if err := task.Kind.Validate(); err != nil {
|
||||
return fmt.Errorf("publish task %s/%s#%d: %w", task.Owner, task.Repo, task.Number, err)
|
||||
}
|
||||
|
||||
data, err := json.Marshal(task)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode task %s/%s#%d: %w", task.Owner, task.Repo, task.Number, err)
|
||||
}
|
||||
|
||||
header := nats.Header{}
|
||||
header.Set("Nats-Msg-Id", task.DedupKey())
|
||||
|
||||
subject := task.Subject()
|
||||
if err := c.publish(ctx, &nats.Msg{Subject: subject, Data: data, Header: header}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.log.Debug("published task",
|
||||
"subject", subject,
|
||||
"kind", string(task.Kind),
|
||||
"owner", task.Owner,
|
||||
"repo", task.Repo,
|
||||
"number", task.Number,
|
||||
"identity", task.Identity)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishRequest publishes a stage execution request, recording replyTo in the
|
||||
// reply header so the worker knows where to answer. JetStream consumes a
|
||||
// message's Reply field for its own PubAck, hence the header.
|
||||
func (c *Client) PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error {
|
||||
if replyTo == "" {
|
||||
return fmt.Errorf("%w: publishing %s", ErrNoReplyAddress, subject)
|
||||
}
|
||||
header := nats.Header{}
|
||||
header.Set(ReplyHeader, replyTo)
|
||||
return c.publish(ctx, &nats.Msg{Subject: subject, Data: payload, Header: header})
|
||||
}
|
||||
|
||||
// Publish sends a payload to a subject with no reply routing.
|
||||
func (c *Client) Publish(ctx context.Context, subject string, payload []byte) error {
|
||||
return c.publish(ctx, &nats.Msg{Subject: subject, Data: payload})
|
||||
}
|
||||
|
||||
// PublishUnique sends a payload carrying an idempotency key, so republishing
|
||||
// the same key inside Config.DedupWindow is suppressed by the server.
|
||||
func (c *Client) PublishUnique(ctx context.Context, subject, idempotencyKey string, payload []byte) error {
|
||||
header := nats.Header{}
|
||||
header.Set(idempotencyHeader, idempotencyKey)
|
||||
return c.publish(ctx, &nats.Msg{Subject: subject, Data: payload, Header: header})
|
||||
}
|
||||
|
||||
// Respond answers a request on an ephemeral reply inbox.
|
||||
//
|
||||
// It uses core NATS rather than the JetStream path the other publish methods
|
||||
// take: the inbox is not a stream subject, so a JetStream publish to it has
|
||||
// no stream to accept the message and fails.
|
||||
func (c *Client) Respond(ctx context.Context, replyAddress string, payload []byte) error {
|
||||
if replyAddress == "" {
|
||||
return fmt.Errorf("%w: nothing to respond to", eventbus.ErrNoReplyAddress)
|
||||
}
|
||||
conn, err := c.connection()
|
||||
if err != nil {
|
||||
return fmt.Errorf("respond on %s: %w", replyAddress, err)
|
||||
}
|
||||
if err := conn.Publish(replyAddress, payload); err != nil {
|
||||
return fmt.Errorf("respond on %s: %w", replyAddress, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PublishRequest sends a payload recording replyTo as the response address.
|
||||
func (c *Client) PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error {
|
||||
if replyTo == "" {
|
||||
return fmt.Errorf("%w: publishing to %s", eventbus.ErrNoReplyAddress, subject)
|
||||
}
|
||||
header := nats.Header{}
|
||||
header.Set(replyHeader, replyTo)
|
||||
return c.publish(ctx, &nats.Msg{Subject: subject, Data: payload, Header: header})
|
||||
}
|
||||
|
||||
// publish is the single JetStream publish path, so every caller gets the same
|
||||
// connection check and error context.
|
||||
func (c *Client) publish(ctx context.Context, msg *nats.Msg) error {
|
||||
@@ -65,15 +61,15 @@ func (c *Client) publish(ctx context.Context, msg *nats.Msg) error {
|
||||
if _, err := c.js.PublishMsg(ctx, msg); err != nil {
|
||||
return fmt.Errorf("publish to %s: %w", msg.Subject, err)
|
||||
}
|
||||
c.log.Debug("published", "subject", msg.Subject, "bytes", len(msg.Data))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Request performs a core-NATS request/reply and returns the response payload.
|
||||
//
|
||||
// This exists so callers never need the raw connection: the previous API
|
||||
// exposed Conn() *nats.Conn, which put the SDK type in every caller's imports.
|
||||
// nats.ErrNoResponders is returned unwrapped enough for errors.Is to match, so
|
||||
// callers can still retry while a responder starts up.
|
||||
// This exists so callers never need the raw connection: the original API
|
||||
// exposed Conn() *nats.Conn, which put the SDK type in every caller's
|
||||
// imports.
|
||||
func (c *Client) Request(ctx context.Context, subject string, payload []byte) ([]byte, error) {
|
||||
conn, err := c.connection()
|
||||
if err != nil {
|
||||
@@ -81,21 +77,29 @@ func (c *Client) Request(ctx context.Context, subject string, payload []byte) ([
|
||||
}
|
||||
msg, err := conn.RequestWithContext(ctx, subject, payload)
|
||||
if err != nil {
|
||||
// Translate the SDK's no-responder error into the contract's, so a
|
||||
// caller can retry a not-yet-subscribed worker without importing
|
||||
// the NATS SDK to name the condition.
|
||||
if errors.Is(err, nats.ErrNoResponders) {
|
||||
return nil, fmt.Errorf("request %s: %w", subject, eventbus.ErrNoResponders)
|
||||
}
|
||||
return nil, fmt.Errorf("request %s: %w", subject, err)
|
||||
}
|
||||
return msg.Data, nil
|
||||
}
|
||||
|
||||
// ReplyInbox is a one-shot subscription awaiting a single response.
|
||||
type ReplyInbox struct {
|
||||
// replyInbox is a one-shot subscription awaiting a single response.
|
||||
type replyInbox struct {
|
||||
sub *nats.Subscription
|
||||
}
|
||||
|
||||
var _ eventbus.ReplyInbox = replyInbox{}
|
||||
|
||||
// Subject is the inbox address to advertise to the responder.
|
||||
func (r ReplyInbox) Subject() string { return r.sub.Subject }
|
||||
func (r replyInbox) Subject() string { return r.sub.Subject }
|
||||
|
||||
// Next blocks until the reply arrives or ctx is done.
|
||||
func (r ReplyInbox) Next(ctx context.Context) ([]byte, error) {
|
||||
func (r replyInbox) Next(ctx context.Context) ([]byte, error) {
|
||||
msg, err := r.sub.NextMsgWithContext(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("await reply on %s: %w", r.sub.Subject, err)
|
||||
@@ -104,7 +108,7 @@ func (r ReplyInbox) Next(ctx context.Context) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Close unsubscribes the inbox.
|
||||
func (r ReplyInbox) Close() error {
|
||||
func (r replyInbox) Close() error {
|
||||
if err := r.sub.Unsubscribe(); err != nil {
|
||||
return fmt.Errorf("close reply inbox %s: %w", r.sub.Subject, err)
|
||||
}
|
||||
@@ -113,18 +117,18 @@ func (r ReplyInbox) Close() error {
|
||||
|
||||
// NewReplyInbox creates a unique inbox that auto-unsubscribes after one
|
||||
// message.
|
||||
func (c *Client) NewReplyInbox() (ReplyInbox, error) {
|
||||
func (c *Client) NewReplyInbox() (eventbus.ReplyInbox, error) {
|
||||
conn, err := c.connection()
|
||||
if err != nil {
|
||||
return ReplyInbox{}, fmt.Errorf("create reply inbox: %w", err)
|
||||
return nil, fmt.Errorf("create reply inbox: %w", err)
|
||||
}
|
||||
sub, err := conn.SubscribeSync(conn.NewInbox())
|
||||
if err != nil {
|
||||
return ReplyInbox{}, fmt.Errorf("subscribe reply inbox: %w", err)
|
||||
return nil, fmt.Errorf("subscribe reply inbox: %w", err)
|
||||
}
|
||||
if err := sub.AutoUnsubscribe(1); err != nil {
|
||||
_ = sub.Unsubscribe()
|
||||
return ReplyInbox{}, fmt.Errorf("configure reply inbox %s: %w", sub.Subject, err)
|
||||
return nil, fmt.Errorf("configure reply inbox %s: %w", sub.Subject, err)
|
||||
}
|
||||
return ReplyInbox{sub: sub}, nil
|
||||
return replyInbox{sub: sub}, nil
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
package nats
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Task distribution subjects. Each encodes the workflow type so a multi-daemon
|
||||
// deployment can filter by workflow.
|
||||
const (
|
||||
SubjectTaskBug = "archie.task.bug"
|
||||
SubjectTaskFeature = "archie.task.feature"
|
||||
SubjectTaskBootstrap = "archie.task.bootstrap"
|
||||
SubjectTaskDefault = "archie.task.default"
|
||||
|
||||
// SubjectTaskWildcard matches every task subject.
|
||||
SubjectTaskWildcard = "archie.task.>"
|
||||
|
||||
// SubjectAgentWildcard matches every agent stage execution subject.
|
||||
SubjectAgentWildcard = "archie.agent.>"
|
||||
)
|
||||
|
||||
// ReplyHeader carries the requester's reply inbox. JetStream consumes a
|
||||
// message's Reply field for its own PubAck, so the address travels as a header.
|
||||
const ReplyHeader = "X-Archie-Reply"
|
||||
|
||||
// TaskKind is the routing token in a task subject.
|
||||
//
|
||||
// The bus deliberately does not derive this itself. It previously inspected
|
||||
// forge issue labels to choose a subject, which put a forge vocabulary
|
||||
// ("bug", "feature") inside infrastructure and duplicated the label table
|
||||
// already owned by the workflow package. The publisher now decides the kind
|
||||
// and the bus only renders it as a subject.
|
||||
type TaskKind string
|
||||
|
||||
const (
|
||||
TaskKindBug TaskKind = "bug"
|
||||
TaskKindFeature TaskKind = "feature"
|
||||
TaskKindBootstrap TaskKind = "bootstrap"
|
||||
|
||||
// TaskKindDefault carries tasks that match no more specific kind.
|
||||
TaskKindDefault TaskKind = "default"
|
||||
)
|
||||
|
||||
// taskSubjects is the closed set of routable kinds. A kind absent from this
|
||||
// map has no subject, so publishing it is a caller error rather than a
|
||||
// silent delivery to the default queue.
|
||||
var taskSubjects = map[TaskKind]string{
|
||||
TaskKindBug: SubjectTaskBug,
|
||||
TaskKindFeature: SubjectTaskFeature,
|
||||
TaskKindBootstrap: SubjectTaskBootstrap,
|
||||
TaskKindDefault: SubjectTaskDefault,
|
||||
}
|
||||
|
||||
// Validate reports whether the kind is routable. The zero kind is accepted
|
||||
// and means TaskKindDefault, so callers that do not classify still publish.
|
||||
func (k TaskKind) Validate() error {
|
||||
if k == "" {
|
||||
return nil
|
||||
}
|
||||
if _, ok := taskSubjects[k]; !ok {
|
||||
return fmt.Errorf("%w: %q", ErrUnknownTaskKind, string(k))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SubjectForKind renders a kind as its task subject. The zero and any
|
||||
// unrecognised kind render as SubjectTaskDefault so a message is never
|
||||
// published to an unroutable subject; PublishTask rejects unknown kinds
|
||||
// before reaching here.
|
||||
func SubjectForKind(kind TaskKind) string {
|
||||
if subject, ok := taskSubjects[kind]; ok {
|
||||
return subject
|
||||
}
|
||||
return SubjectTaskDefault
|
||||
}
|
||||
|
||||
// SubjectForAgentRequest returns the subject carrying stage execution requests
|
||||
// for a task. The stage travels in the payload, not the subject: one subject
|
||||
// per task with sequential stage messages.
|
||||
func SubjectForAgentRequest(taskID int64) string {
|
||||
return fmt.Sprintf("archie.agent.%d.request", taskID)
|
||||
}
|
||||
|
||||
// SubjectForAgentResponse returns the subject for agent output destined for
|
||||
// human channels. The daemon reviews these before forwarding to issue
|
||||
// comments, labels, or pull requests.
|
||||
func SubjectForAgentResponse(taskID int64) string {
|
||||
return fmt.Sprintf("archie.agent.%d.response", taskID)
|
||||
}
|
||||
|
||||
// SubjectForAgentSystem returns the subject for internal agent messages such
|
||||
// as log dumps, health, and PII warnings. The daemon reads these for
|
||||
// observability and never forwards them.
|
||||
func SubjectForAgentSystem(taskID int64) string {
|
||||
return fmt.Sprintf("archie.agent.%d.system", taskID)
|
||||
}
|
||||
@@ -1,82 +1,33 @@
|
||||
package workflow
|
||||
|
||||
import "strings"
|
||||
import "github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
|
||||
// Kind is the routing category an issue's labels select.
|
||||
//
|
||||
// It is the single axis both the workflow registry and the event bus route
|
||||
// on. Before this existed the label table was written twice -- once here as
|
||||
// a switch in Route, once in the NATS package as a subject lookup -- so
|
||||
// adding a label meant remembering both, and the event bus had to know what
|
||||
// a forge label was.
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
KindBug Kind = "bug"
|
||||
KindFeature Kind = "feature"
|
||||
KindBootstrap Kind = "bootstrap"
|
||||
|
||||
// KindDefault is the routing category for issues carrying no
|
||||
// recognised label.
|
||||
KindDefault Kind = "default"
|
||||
)
|
||||
|
||||
// labelKinds maps a forge issue label to its routing kind. This is the one
|
||||
// place the label vocabulary is defined.
|
||||
var labelKinds = map[string]Kind{
|
||||
"bug": KindBug,
|
||||
"feature": KindFeature,
|
||||
"bootstrap": KindBootstrap,
|
||||
}
|
||||
|
||||
// kindWorkflows names the registry entry each kind prefers. Bootstrap
|
||||
// kindWorkflows names the registry entry each intake kind prefers. Bootstrap
|
||||
// exercises the full pipeline deterministically (no LLM spend) -- invites,
|
||||
// clone, push, PR, labels.
|
||||
var kindWorkflows = map[Kind]string{
|
||||
KindBug: "tdd",
|
||||
KindFeature: "feasibility",
|
||||
KindBootstrap: "bootstrap",
|
||||
}
|
||||
|
||||
// KindForLabels returns the routing kind for a set of issue labels. The
|
||||
// first recognised label wins; an empty or unrecognised set is KindDefault.
|
||||
func KindForLabels(labels []string) Kind {
|
||||
for _, kind := range KindsForLabels(labels) {
|
||||
return kind
|
||||
}
|
||||
return KindDefault
|
||||
}
|
||||
|
||||
// KindsForLabels returns every recognised kind in label order.
|
||||
//
|
||||
// Route needs the full ordered set rather than just the first: when a task
|
||||
// is labelled "bug,feature" and no "tdd" workflow is registered, it must
|
||||
// still fall through to "feasibility" rather than to the default.
|
||||
func KindsForLabels(labels []string) []Kind {
|
||||
var kinds []Kind
|
||||
for _, label := range labels {
|
||||
if kind, ok := labelKinds[strings.TrimSpace(label)]; ok {
|
||||
kinds = append(kinds, kind)
|
||||
}
|
||||
}
|
||||
return kinds
|
||||
// The label vocabulary itself belongs to workintake, which is what reads
|
||||
// forge issues. This package owns only the choice of workflow for a kind:
|
||||
// keeping both here meant the transport and the registry each had their own
|
||||
// copy of the label table.
|
||||
var kindWorkflows = map[workintake.Kind]string{
|
||||
workintake.KindBug: "tdd",
|
||||
workintake.KindFeature: "feasibility",
|
||||
workintake.KindBootstrap: "bootstrap",
|
||||
}
|
||||
|
||||
// SplitLabels parses the comma-separated label form used on store.Task,
|
||||
// dropping blanks so "bug,," and a trailing comma do not yield empty labels.
|
||||
func SplitLabels(raw string) []string {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(raw, ",")
|
||||
labels := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if trimmed := strings.TrimSpace(part); trimmed != "" {
|
||||
labels = append(labels, trimmed)
|
||||
// workflowForLabels returns the registered workflow name for a task's
|
||||
// labels, trying each recognised kind in label order so a "bug,feature" task
|
||||
// still reaches feasibility when no tdd workflow is registered.
|
||||
func workflowForLabels(reg Registry, labels string) (Workflow, bool) {
|
||||
for _, kind := range workintake.KindsForLabels(workintake.SplitLabels(labels)) {
|
||||
name, ok := kindWorkflows[kind]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if wf, ok := reg[name]; ok {
|
||||
return wf, true
|
||||
}
|
||||
}
|
||||
if len(labels) == 0 {
|
||||
return nil
|
||||
}
|
||||
return labels
|
||||
return Workflow{}, false
|
||||
}
|
||||
@@ -150,14 +150,8 @@ func Route(t *store.Task, reg Registry) Workflow {
|
||||
return wf
|
||||
}
|
||||
}
|
||||
for _, kind := range KindsForLabels(SplitLabels(t.Labels)) {
|
||||
name, ok := kindWorkflows[kind]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if wf, ok := reg[name]; ok {
|
||||
return wf
|
||||
}
|
||||
if wf, ok := workflowForLabels(reg, t.Labels); ok {
|
||||
return wf
|
||||
}
|
||||
if wf, ok := reg["implement"]; ok {
|
||||
return wf
|
||||
|
||||
Reference in new issue
Block a user