Merge crew/t2db-41: archie-core-t2db.41
This commit is contained in:
commit
fd6b568fa4
36 files changed
+2284
-264
No files matched your search
@@ -484,7 +484,7 @@
|
||||
"url": "/dev/prds/state-store-contract/",
|
||||
"title": "State Store — contract boundary \u0026 transport (ratification)",
|
||||
"section": "PRDs",
|
||||
"body": "# State Store — contract boundary \u0026 transport (ratification)\n\n**Status:** Approved\n**Revision:** 2e\n**Date:** 2026-09-06\n\nThis is the pre-implementation design document that Phase 2 subtasks `.4.2` (generalise\n`storerpc` as the State Store gRPC contract) and `.4.3` (stand up `archie-state-store`)\nimplement against.\n**Beads milestone:** archie-core-8cda.4.1\n**Parent:** `docs/prds/service-decomposition.md` (open-question Q4 **RESOLVED**)\n\nThis document fixes the **contract boundary** and **transport** for the State Store service.\nIt does not redesign Archie's domain model, does not invent a second store, and does **not**\nsplit `archie.db`. It records method ownership, DTO/wire representation, error semantics,\nlimits, mode/config shape, local-adapter compatibility, and cutover/deletion rules so that\n`.4.2` and `.4.3` have exactly one authoritative surface to build against.\n\n\u003e **Storage superseded.** The store behind this contract is PostgreSQL:\n\u003e `internal/infrastructure/postgres` implements the producer-owned interfaces in\n\u003e `internal/domain/storecontract` and `workflow.Store`, over the database\n\u003e `database_url` names. `internal/store`, the SQLite file and the local adapter\n\u003e are gone; where this document says `archie.db`, `*store.Store` or\n\u003e `internal/store`, read the State Store's PostgreSQL database and\n\u003e `internal/infrastructure/postgres`. The contract, ownership split, error\n\u003e semantics and cutover rules stand.\n\n\u003e **Current state — read before `.4.2` starts.** The §12 step 2 relocation (`archie-core-8cda.\n4.8`) has landed: `Task`/`Status*`/`Source*` and the 3-method `workflow.Store` now live in\n\u003e `internal/domain/workflow`, the six production workflow files import no `internal/store`\n\u003e symbol, and `storerpc`/the NATS transport assert against `workflow.Store` instead of\n\u003e `store.WorkflowStore`. The broader Phase 2 acceptance criterion (State Store as its own\n\u003e service behind gRPC) is still open — that is what `.4.2`/`.4.3` build next.\n\n\u003e **Relationship to migration-decisions §4 — not replaced.** `docs/architecture/\nmigration-decisions.md` §4 (`store.Task → WorkflowExecution`, `mutable Stage →\nStepExecution history`, `RetryCount/Attempt → Attempt records`, versioned Workflow defs)\n\u003e **remains the authoritative, open document for the long-term Workflow migration.** This PRD\n\u003e does **not** replace it. It only pulls the minimal `Task`/`Status`/`Source` type + interface\n\u003e relocation forward as a Phase 2 prerequisite (because the acceptance criterion requires it);\n\u003e the rest of §4 stays its own migration and stays open.\n\n---\n\n## 1. Decision (the invariant everything follows)\n\nArchie is composed such that a single **`archie-state-store`** service owns the one existing\nSQLite file (`archie.db`) and exposes it over **gRPC behind narrow typed contracts**. There is\nexactly one store, one file, one service. No database-per-service, no distributed\ntransactions, no second store abstraction.\n\nTwo adapter families satisfy the contract interfaces:\n\n- **Local adapter** — `*store.Store` (the in-process SQLite implementation).\n- **Remote adapter** — `*staterpc.Client` (wraps the generated gRPC client).\n\nEach adapter carries an explicit compile-time assertion against the contract interfaces it\nserves. Composition picks local vs. remote by the presence of `[services.state].target`\n(empty = local; set = dial the gRPC service). Default is **local** (the State Store is not yet\nextracted), which is the PRD §3 posture; it differs from the gateway's remote-default only\nbecause the gateway is already a separate deployment and the State Store is not.\n\n\u003e **Contract-ownership fork, decided (rev. 2c — resolves the domain contradiction).**\n\u003e Ownership splits by the consumer's _layer_, per the dependency contract's rule #2\n\u003e (\"a domain defines the smallest interfaces required to perform its work\") and rule #7\n\u003e (\"database rows … MUST NOT leak into domain models\"):\n\u003e\n\u003e - The **workflow domain** (`internal/domain/workflow`) is the only domain-layer consumer of\n\u003e store state. It defines its own **consumer-owned** narrow `Store` interface\n\u003e (Update/Transition/InsertEvent) and its own **domain-owned** `Task` type plus\n\u003e `Status`/`Source` vocabulary. It imports **zero** `internal/store` symbols — satisfying the\n\u003e Phase 2 acceptance criterion. This is the minimal bound of migration-decisions §4\n\u003e (`store.Task → WorkflowExecution`), pulled forward **because** the acceptance criterion\n\u003e requires it.\n\u003e - The **daemon / webui / intake** store surfaces (`TaskStore`, `CaptureStore`,\n\u003e `MappingStore`, `BindingStore`, `BindingDispatcher`, `BindingTaskCreator`) stay\n\u003e **producer-owned** in `internal/store`, because those consumers are application Line truncated
|
||||
"body": "# State Store — contract boundary \u0026 transport (ratification)\n\n**Status:** Approved\n**Revision:** 2e\n**Date:** 2026-09-06\n\nThis is the pre-implementation design document that Phase 2 subtasks `.4.2` (generalise\n`storerpc` as the State Store gRPC contract) and `.4.3` (stand up `archie-state-store`)\nimplement against.\n**Beads milestone:** archie-core-8cda.4.1\n**Parent:** `docs/prds/service-decomposition.md` (open-question Q4 **RESOLVED**)\n\nThis document fixes the **contract boundary** and **transport** for the State Store service.\nIt does not redesign Archie's domain model, does not invent a second store, and does **not**\nsplit `archie.db`. It records method ownership, DTO/wire representation, error semantics,\nlimits, mode/config shape, local-adapter compatibility, and cutover/deletion rules so that\n`.4.2` and `.4.3` have exactly one authoritative surface to build against.\n\n\u003e **Storage superseded.** The store behind this contract is PostgreSQL:\n\u003e `internal/infrastructure/postgres` implements the producer-owned interfaces in\n\u003e `internal/domain/storecontract` and `workflow.Store`, over the database\n\u003e `database_url` names. `internal/store`, the SQLite file and the local adapter\n\u003e are gone; where this document says `archie.db`, `*store.Store` or\n\u003e `internal/store`, read the State Store's PostgreSQL database and\n\u003e `internal/infrastructure/postgres`. The contract, ownership split, error\n\u003e semantics and cutover rules stand.\n\n\u003e **Current state — read before `.4.2` starts.** The §12 step 2 relocation (`archie-core-8cda.\n4.8`) has landed: `Task`/`Status*`/`Source*` and the 3-method `workflow.Store` now live in\n\u003e `internal/domain/workflow`, the six production workflow files import no `internal/store`\n\u003e symbol, and `storerpc`/the NATS transport assert against `workflow.Store` instead of\n\u003e `store.WorkflowStore`. The broader Phase 2 acceptance criterion (State Store as its own\n\u003e service behind gRPC) is still open — that is what `.4.2`/`.4.3` build next.\n\n\u003e **Relationship to migration-decisions §4 — not replaced.** `docs/architecture/\nmigration-decisions.md` §4 (`store.Task → WorkflowExecution`, `mutable Stage →\nStepExecution history`, `RetryCount/Attempt → Attempt records`, versioned Workflow defs)\n\u003e **remains the authoritative, open document for the long-term Workflow migration.** This PRD\n\u003e does **not** replace it. It only pulls the minimal `Task`/`Status`/`Source` type + interface\n\u003e relocation forward as a Phase 2 prerequisite (because the acceptance criterion requires it);\n\u003e the rest of §4 stays its own migration and stays open.\n\n---\n\n## 1. Decision (the invariant everything follows)\n\nArchie is composed such that a single **`archie-state-store`** service owns the one existing\nSQLite file (`archie.db`) and exposes it over **gRPC behind narrow typed contracts**. There is\nexactly one store, one file, one service. No database-per-service, no distributed\ntransactions, no second store abstraction.\n\nTwo adapter families satisfy the contract interfaces:\n\n- **Local adapter** — `*store.Store` (the in-process SQLite implementation).\n- **Remote adapter** — `*staterpc.Client` (wraps the generated gRPC client).\n\nEach adapter carries an explicit compile-time assertion against the contract interfaces it\nserves. Composition picks local vs. remote by the presence of `[services.state].target`\n(empty = local; set = dial the gRPC service). Default is **local** (the State Store is not yet\nextracted), which is the PRD §3 posture; it differs from the gateway's remote-default only\nbecause the gateway is already a separate deployment and the State Store is not.\n\n\u003e **Contract-ownership fork, decided (rev. 2c — resolves the domain contradiction).**\n\u003e Ownership splits by the consumer's _layer_, per the dependency contract's rule #2\n\u003e (\"a domain defines the smallest interfaces required to perform its work\") and rule #7\n\u003e (\"database rows … MUST NOT leak into domain models\"):\n\u003e\n\u003e - The **workflow domain** (`internal/domain/workflow`) is the only domain-layer consumer of\n\u003e store state. It defines its own **consumer-owned** narrow `Store` interface\n\u003e (Update/Transition/InsertEvent) and its own **domain-owned** `Task` type plus\n\u003e `Status`/`Source` vocabulary. It imports **zero** `internal/store` symbols — satisfying the\n\u003e Phase 2 acceptance criterion. This is the minimal bound of migration-decisions §4\n\u003e (`store.Task → WorkflowExecution`), pulled forward **because** the acceptance criterion\n\u003e requires it.\n\u003e - The **daemon / webui / intake** store surfaces (`TaskStore`, `CaptureStore`,\n\u003e `MappingStore`, `BindingStore`, `BindingDispatcher`, `BindingTaskCreator`) stay\n\u003e **producer-owned** in `internal/store`, because those consumers are application Line truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/status-health-surface/",
|
||||
@@ -533,6 +533,12 @@
|
||||
"title": "Webhook intake security -- decision",
|
||||
"section": "PRDs",
|
||||
"body": "# Webhook intake security -- decision\n\n**Status:** Approved\n**Date:** 2026-08-22\n**Beads issue:** `archie-core-t2db.5`, blocks `archie-core-t2db.4`\n\n## Why this is decided ahead of the code it constrains\n\n`archie-core-t2db` (no-code playbooks) and `archie-core-7d5u` (typed event\nsources) both land a public intake surface whose eventual output is an agent\nwith tool access and a repo. Neither capture (`t2db.1`) nor the intake family\n(`7d5u.3`) has shipped yet, so this decision exists to be a contract the two\nepics build against, not a retrofit once the surface exists. A webhook URL\nthat can start an agent is remote code execution with extra steps if any of\nthe five points below is missing on day one.\n\n## 1. Per-source authentication\n\nReuse the per-route HMAC-SHA256 scheme already in\n`internal/channels/webhook/webhook.go` (`validateHMAC`), unchanged: a source\nregisters with a shared secret; the signature is checked against the raw\nbody. Do not invent a second scheme for intake.\n\n**Capture and triggering split at the signature, not at a separate flag.** An\nevent with a missing or invalid signature is still captured (visible in the\ninspector, marked unauthenticated) so an operator can diagnose a\nmisconfigured sender -- but an unauthenticated event can never be matched\nagainst a binding. Authentication is what promotes an event from \"something\narrived\" to \"something that can run a workflow.\"\n\n## 2. Human approval before a binding arms\n\nA binding has three states: `draft -\u003e pending_approval -\u003e armed`. Only\n`armed` bindings are evaluated against incoming events. Creating or editing a\nbinding's matcher, mapping, or target workflow always sets it to\n`pending_approval`, including edits to an already-armed binding -- an edit is\nnot silently re-armed, or the approval gate is decorative. Approval requires\nan authenticated dashboard session; there is no unattended approval path.\n\nModel: `internal/channels/telegram`'s dangerous-command approval flow\n(`dangerousAction` / `pendingApproval` in `dangerous.go` / `approval.go`) is\nthe existing precedent for \"a human decision gates code from running\" in this\ncodebase -- follow its shape rather than designing a new approval primitive.\n\n## 3. Rate and volume limits, per source\n\nA token-bucket limiter keyed by source ID, not global -- one noisy or hostile\nsender must not exhaust another source's capture budget. Precedent for\nper-key volume capping: `internal/pairing/store.go`'s `MaxPendingPerPlatform`.\n\nOn limit exceeded, the request is rejected (HTTP 429), not silently dropped.\nA silently dropped event is indistinguishable from a sender that never fired\n-- the same invisible-failure shape the update-verification work\n(`archie-core-522`) exists to prevent. The rejection itself is observable in\nthe inspector as a distinct outcome from \"not received.\"\n\n## 4. Blast radius bounded by code, not by prompt wording\n\nA playbook-triggered task runs through exactly the same enforcement as every\nother task: the `[[repos.gate]]` quality gate, `diff_cap_lines`, the\nsandboxed per-task container (`internal/container`), worktree isolation, and\n\"the model never runs git.\" No new relaxation is introduced for\nplaybook-triggered work, and none is needed -- the existing constraints are\nthe control.\n\nMapped payload fields are treated as ordinary user-controlled text inside an\nagent prompt, exactly like an inbound chat message never as trusted\ninstructions. \"The agent should ignore instructions in the payload\" is not a\ncontrol and must not appear as one anywhere in this feature -- per the\nenvironmental-enforcement principle (`docs/architecture/agent-system.md`,\n\"Existing invariants to preserve\"), the boundary is the gate and the sandbox,\nnot a sentence in the system prompt.\n\n## 5. Retention and redaction of captured payloads\n\n**Retention:** captured events (bound or not) are purged after 7 days,\nconfigurable per deployment. Unbound events exist to be mapped against; nothing\njustifies keeping them indefinitely once that window has passed, and an\nunbounded capture store is the same disk-exhaustion risk item 3 addresses at\nthe request-rate level.\n\n**Redaction:** before a captured payload is persisted, apply the same\nkey-name heuristic `internal/gateway/stream.go`'s `sensitiveParameterKey`\nalready uses for tool-call parameters (matches `token`, `secret`, `password`,\n`passwd`, `api_key`, `apikey`, `authorization`, `credential`, `cookie`,\n`private_key` against normalised JSON keys) and replace matching values with\nthe same `[redacted]` marker. This is best-effort, not a guarantee -- a\nsender that names a secret field something the heuristic misses will still\nhave it captured. Document that limitation where captured payloads are\ndisplayed, rather than implying the redaction is complete.\n\n**Visibility:** captured payloads, redacted or not, are visible only to an\nauthenticated dashboarLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/workflow-calls/",
|
||||
"title": "Workflows calling workflows",
|
||||
"section": "PRDs",
|
||||
"body": "# Workflows calling workflows\n\n**Status:** Draft\n**Date:** 2026-09-26\n**Bead:** `archie-core-t2db.41`\n**Extends:** `docs/prds/event-automation.md` (workflow/binding/profile model), `docs/prds/state-store-contract.md` (two new RPCs), `docs/prds/execution-tree-state-machine.md` (call kind)\n\n## The call, end to end\n\n```\ncaller run (in agent container)\n workflow.call step ──StartCall──▶ State Store EnqueueCallTask(caller, workflow, inputs)\n │ callee task row: org/identity/owner/repo\n │ derived from the caller row server-side\n ▼\n callee task (queued, org of the caller)\n │ daemon claims, pins the callee's OWN\n │ definition → its OWN agent profile\n ▼\n callee run (its own container, own tree)\n wait: true ──poll──▶ State Store WorkflowCallStatus(caller, callee)\n wait: false ──continue\n```\n\nA `workflow.call` step starts the callee as a first-class task and, with\n`wait: true`, waits for its terminal state. The callee is never run \"inside\"\nthe caller: it gets its own task row, its own pinned definition, its own\nprofile, its own container, and its own outcome on its own run\n(`execution-tree-state-machine.md`).\n\n## Save-time checks (the collection is the authority)\n\nAll semantic checks run in `workflow.ValidateDefinitionCollection`, the gate\nevery save and load of the `workflow-definitions` resource passes through.\nA single-definition parse cannot see callees, so it validates shape only.\n\n- **Callee exists.** A call to a workflow outside the collection is refused.\n- **Inputs checked.** A call value is a literal or a reference\n (`inputs.\u003cname\u003e`) to the calling workflow's declared input. References must\n name a declared input; the resolved type must satisfy the callee's declared\n type; unknown callee inputs are refused; a required callee input must be\n assigned.\n- **Self-calls refused.** The call graph over the collection is walked from\n every workflow; a workflow that reaches itself, directly or through other\n workflows, is refused with the cycle path. (Cycles can otherwise deadlock:\n `wait: true` callers hold their containers.)\n- **Repository.** `workflow.call` needs no repository, so it is legal in a\n `repository: none` workflow: the callee inherits the caller's task record\n (owner/repo/identity/org), and the callee's own `repository` mode decides\n whether its run clones.\n- **Org enablement** (bead `archie-core-t2db.42`): the single\n instance-wide collection the current model serves is what \"enabled for\n the caller's org\" resolves against — an undefined callee parks. The\n callee's org is derived from the caller's row, so a callee is never\n visible outside its caller's org. The per-org enable/disable catalogue\n lands with `archie-core-t2db.42` and becomes the one source of this\n predicate.\n\n## Runtime\n\n**Depth limit.** `workflow.MaxCallDepth` (5). The calling stage refuses a call\nthat would exceed it; the store re-checks on insert (callee depth = caller\ndepth + 1), so no path can enqueue past the limit.\n\n**Starting the callee.** The caller runs inside a container whose task-scoped\nState Store grant authorizes only `Update`/`Transition`/`InsertEvent` on its\nown task — it can neither enqueue a task nor read another one. One new\ncontract RPC starts the callee:\n\n- `EnqueueCallTask(caller_task_id, workflow, inputs_json) → task`\n — the store reads the caller row, refuses a caller that is not `running`,\n refuses past the depth limit, and inserts the callee row deriving org,\n workspace, identity, owner, repo and a fresh synthetic issue number from\n the caller (the same org COALESCE `EnqueueIssue`/`InsertChatTask` use). The\n callee row carries `call_parent_task_id` and `call_depth`.\n\n**Waiting on the callee.** One new read RPC returns a call's callee:\n\n- `WorkflowCallStatus(caller_task_id, call_task_id) → (status, detail)` — the\n handler verifies `call_parent_task_id` matches, so a caller reads only its\n own callees, and returns the callee's status plus its latest transition\n detail (the callee's summary — the wait:true \"outputs\" until a structured\n outputs model lands).\n\nBoth RPCs are admitted to task-scoped grants on `caller_task_id == granted\ntask ID` (`authorizesTaskScopedCall` gains two request-shape cases). This is\nthe one sanctioned widening of a task grant: the callee is the caller's child,\nand starting it is the mechanism by which one event becomes several runs. The\nwidenings stay request-shape checks; the parent-child authorization is a\nhandler-side row check, so the interceptor never reads the database.\n\n**Schema.** `tasks` gains Line truncated
|
||||
}
|
||||
],
|
||||
"nav": [
|
||||
@@ -898,6 +904,10 @@
|
||||
{
|
||||
"title": "Webhook intake security -- decision",
|
||||
"url": "/dev/prds/webhook-intake-security/"
|
||||
},
|
||||
{
|
||||
"title": "Workflows calling workflows",
|
||||
"url": "/dev/prds/workflow-calls/"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -265,13 +265,14 @@ _view_, not new RPCs — `Update`/`Transition`/`InsertEvent` are shared with
|
||||
| Binding | `InsertBinding`, `GetBinding`, `ListBindings`, `UpdateBinding`, `DeleteBinding`, `ApproveBinding` |
|
||||
| Dispatch | `ArmedBindingsForSource`, `RecordDispatch`, `ListUndispatchedCaptures` |
|
||||
| Playbook | `RecordPlaybookDispatch`, `DeletePlaybookDispatches` |
|
||||
| WorkflowCaller | `EnqueueCallTask`, `WorkflowCallStatus` |
|
||||
| BindingTaskCreator | `EnqueueBindingTask` |
|
||||
| Config snapshot | `PutConfigSnapshot`, `GetConfigSnapshot` |
|
||||
| Apply status | `PutApplyStatus`, `ListApplyStatus` |
|
||||
| Channel status | `PutChannelStatus`, `ListChannelStatus` |
|
||||
| Task log | `ReadTaskLog`, `StreamTaskLogContent` |
|
||||
|
||||
That is **51 unique contract RPCs** across one service (the `TaskEvents.Close` method is dropped).
|
||||
That is **53 unique contract RPCs** across one service (the `TaskEvents.Close` method is dropped).
|
||||
`Close()` is **excluded** from the wire (it is server lifecycle, not a client call) — see §11.
|
||||
|
||||
The channel-status pair follows the config snapshot's split for the same reason: the writer is the process that HOSTS the channels -- the Messaging Service, which alone observes whether a channel is starting, running or failed -- and the reader is the UI process. Both RPCs are administrative, so a task-scoped grant reaches neither. A channel's state is runtime state rather than a settings document, which is why it is not a `ControlPlaneService` resource kind: a failed channel is not a document anybody edits. Asking for a reload travels on its own surface rather than through this one; see `docs/architecture/migration-decisions.md`, "Channel state to the dashboard".
|
||||
@@ -306,6 +307,19 @@ condition, and the panel that collapsed them told operators task logging was swi
|
||||
it was not. The Go facade is `storecontract.TaskLogStore` (2 methods), carried by
|
||||
`*staterpc.Client` alongside the rest.
|
||||
|
||||
The workflow-caller pair was added by `archie-core-t2db.41` (see
|
||||
`docs/prds/workflow-calls.md`). It is the one surface a task-scoped grant
|
||||
reaches beyond the three task.Store RPCs: a `workflow.call` step must start
|
||||
its callee and, with `wait: true`, read it back, and the callee is the
|
||||
caller's child. Both requests carry the caller's task ID, which the grant
|
||||
check verifies; `EnqueueCallTask` derives everything else from the caller's
|
||||
row and refuses a caller that is not running or a call past the depth limit
|
||||
(`ErrCallCallerNotRunning` / `ErrCallDepthExceeded`), and `WorkflowCallStatus`
|
||||
re-checks the parent-child relation against the row before answering
|
||||
(`ErrCallNotYours`), so the interceptor itself never reads the database. The
|
||||
facade is `workflow/task.Caller` (2 methods), carried by `*staterpc.Client`
|
||||
and the PostgreSQL store alike.
|
||||
|
||||
The playbook pair was added by `archie-core-t2db.17`'s implementation (see
|
||||
`docs/prds/eda-playbook-engine.md`, execution-time gap 2). It is administrative
|
||||
for the same reason as the config-snapshot and apply-status pairs, and
|
||||
@@ -551,9 +565,12 @@ stated explicitly rather than left implicit.
|
||||
cannot leave a grant valid indefinitely.
|
||||
- **Validation and scope (`.4.7`):** a gRPC interceptor (`staterpc.TaskGrants.UnaryInterceptor`)
|
||||
validates the token against the State Store's own issued-grant set; missing/unknown/expired →
|
||||
`codes.Unauthenticated`. Unlike the daemon's own administrative token (full access to all ~40
|
||||
RPCs), a task-scoped grant additionally authorises only `Update`/`Transition`/`InsertEvent` on
|
||||
its own task ID — every other RPC, including the streaming capture surface and
|
||||
`codes.Unauthenticated`. Unlike the daemon's own administrative token (full access to all RPCs),
|
||||
a task-scoped grant additionally authorises only `Update`/`Transition`/`InsertEvent` on
|
||||
its own task ID, plus the two workflow-caller RPCs naming that same task ID
|
||||
(`EnqueueCallTask`/`WorkflowCallStatus`, `docs/prds/workflow-calls.md` — the one
|
||||
sanctioned widening: the callee is the caller's child). Every other RPC, including the
|
||||
streaming capture surface and
|
||||
`RegisterTaskGrant`/`RevokeTaskGrant` themselves, is `codes.PermissionDenied` for a task-scoped
|
||||
caller. The token is carried in gRPC metadata, never in a URL.
|
||||
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
# Workflows calling workflows
|
||||
|
||||
**Status:** Draft
|
||||
**Date:** 2026-09-26
|
||||
**Bead:** `archie-core-t2db.41`
|
||||
**Extends:** `docs/prds/event-automation.md` (workflow/binding/profile model), `docs/prds/state-store-contract.md` (two new RPCs), `docs/prds/execution-tree-state-machine.md` (call kind)
|
||||
|
||||
## The call, end to end
|
||||
|
||||
```
|
||||
caller run (in agent container)
|
||||
workflow.call step ──StartCall──▶ State Store EnqueueCallTask(caller, workflow, inputs)
|
||||
│ callee task row: org/identity/owner/repo
|
||||
│ derived from the caller row server-side
|
||||
▼
|
||||
callee task (queued, org of the caller)
|
||||
│ daemon claims, pins the callee's OWN
|
||||
│ definition → its OWN agent profile
|
||||
▼
|
||||
callee run (its own container, own tree)
|
||||
wait: true ──poll──▶ State Store WorkflowCallStatus(caller, callee)
|
||||
wait: false ──continue
|
||||
```
|
||||
|
||||
A `workflow.call` step starts the callee as a first-class task and, with
|
||||
`wait: true`, waits for its terminal state. The callee is never run "inside"
|
||||
the caller: it gets its own task row, its own pinned definition, its own
|
||||
profile, its own container, and its own outcome on its own run
|
||||
(`execution-tree-state-machine.md`).
|
||||
|
||||
## Save-time checks (the collection is the authority)
|
||||
|
||||
All semantic checks run in `workflow.ValidateDefinitionCollection`, the gate
|
||||
every save and load of the `workflow-definitions` resource passes through.
|
||||
A single-definition parse cannot see callees, so it validates shape only.
|
||||
|
||||
- **Callee exists.** A call to a workflow outside the collection is refused.
|
||||
- **Inputs checked.** A call value is a literal or a reference
|
||||
(`inputs.<name>`) to the calling workflow's declared input. References must
|
||||
name a declared input; the resolved type must satisfy the callee's declared
|
||||
type; unknown callee inputs are refused; a required callee input must be
|
||||
assigned.
|
||||
- **Self-calls refused.** The call graph over the collection is walked from
|
||||
every workflow; a workflow that reaches itself, directly or through other
|
||||
workflows, is refused with the cycle path. (Cycles can otherwise deadlock:
|
||||
`wait: true` callers hold their containers.)
|
||||
- **Repository.** `workflow.call` needs no repository, so it is legal in a
|
||||
`repository: none` workflow: the callee inherits the caller's task record
|
||||
(owner/repo/identity/org), and the callee's own `repository` mode decides
|
||||
whether its run clones.
|
||||
- **Org enablement** (bead `archie-core-t2db.42`): the single
|
||||
instance-wide collection the current model serves is what "enabled for
|
||||
the caller's org" resolves against — an undefined callee parks. The
|
||||
callee's org is derived from the caller's row, so a callee is never
|
||||
visible outside its caller's org. The per-org enable/disable catalogue
|
||||
lands with `archie-core-t2db.42` and becomes the one source of this
|
||||
predicate.
|
||||
|
||||
## Runtime
|
||||
|
||||
**Depth limit.** `workflow.MaxCallDepth` (5). The calling stage refuses a call
|
||||
that would exceed it; the store re-checks on insert (callee depth = caller
|
||||
depth + 1), so no path can enqueue past the limit.
|
||||
|
||||
**Starting the callee.** The caller runs inside a container whose task-scoped
|
||||
State Store grant authorizes only `Update`/`Transition`/`InsertEvent` on its
|
||||
own task — it can neither enqueue a task nor read another one. One new
|
||||
contract RPC starts the callee:
|
||||
|
||||
- `EnqueueCallTask(caller_task_id, workflow, inputs_json) → task`
|
||||
— the store reads the caller row, refuses a caller that is not `running`,
|
||||
refuses past the depth limit, and inserts the callee row deriving org,
|
||||
workspace, identity, owner, repo and a fresh synthetic issue number from
|
||||
the caller (the same org COALESCE `EnqueueIssue`/`InsertChatTask` use). The
|
||||
callee row carries `call_parent_task_id` and `call_depth`.
|
||||
|
||||
**Waiting on the callee.** One new read RPC returns a call's callee:
|
||||
|
||||
- `WorkflowCallStatus(caller_task_id, call_task_id) → (status, detail)` — the
|
||||
handler verifies `call_parent_task_id` matches, so a caller reads only its
|
||||
own callees, and returns the callee's status plus its latest transition
|
||||
detail (the callee's summary — the wait:true "outputs" until a structured
|
||||
outputs model lands).
|
||||
|
||||
Both RPCs are admitted to task-scoped grants on `caller_task_id == granted
|
||||
task ID` (`authorizesTaskScopedCall` gains two request-shape cases). This is
|
||||
the one sanctioned widening of a task grant: the callee is the caller's child,
|
||||
and starting it is the mechanism by which one event becomes several runs. The
|
||||
widenings stay request-shape checks; the parent-child authorization is a
|
||||
handler-side row check, so the interceptor never reads the database.
|
||||
|
||||
**Schema.** `tasks` gains `call_parent_task_id` (bigint, default 0) and
|
||||
`call_depth` (int, default 0); both cross the wire on the `Task` message.
|
||||
Columns default so existing writers and rows read as depth-0 root runs.
|
||||
|
||||
**The engine capability.** The workflow domain owns a consumer facade,
|
||||
`task.Caller` (2 methods), carried by `*staterpc.Client` and wired into
|
||||
`TaskContext.Calls` by the agent worker's composition root — the same shape
|
||||
as `workflow.Store`. A runner with no `Calls` capability fails a
|
||||
`workflow.call` step with a named error rather than pretending.
|
||||
|
||||
**Semantics.**
|
||||
|
||||
- `wait: false` — start, record a durable `workflow_call_started` event, and
|
||||
continue. The callee's outcome is reported on its own run.
|
||||
- `wait: true` — start, then poll `WorkflowCallStatus` (2 s, bounded by the
|
||||
task's wall clock) until the callee is terminal. `completed`, `pr_open` and
|
||||
`merged` succeed the step; `parked`, `failed`, `rejected`, `dead` and
|
||||
`closed_wont_do` fail it with the callee's detail in the error. A durable
|
||||
`workflow_call_finished` event carries the terminal status and detail.
|
||||
- The caller's own stage/attempt accounting is unchanged: the call is one
|
||||
stage that took as long as its callee.
|
||||
- A callee inherits the caller's task record but is otherwise independent:
|
||||
retry, park and cancel of the callee never rewrite the caller. Cancelling a
|
||||
caller cancels its callees through the operator action on each
|
||||
(`execution-tree-state-machine.md` "Cancellation"); until the tree state
|
||||
machine lands, a caller cancelled while waiting fails its stage and leaves
|
||||
the callee running — the same semantics as `wait: false`.
|
||||
|
||||
## What is deliberately not here
|
||||
|
||||
- **Declared outputs.** `wait: true` receives the callee's terminal detail,
|
||||
not structured outputs; the outputs model is its own bead.
|
||||
- **The StepExecution tree.** The `call` kind is designed for in
|
||||
`execution-tree-state-machine.md`; this design lands the behaviour without
|
||||
the per-step records.
|
||||
- **Per-org enablement** — bead `archie-core-t2db.42`'s catalogue is the
|
||||
source once it exists.
|
||||
@@ -183,6 +183,7 @@ func worktreeOwnerID(env string) int {
|
||||
type taskDependencies struct {
|
||||
forge workflow.Forger
|
||||
store workflow.Store
|
||||
calls task.Caller
|
||||
trees remoteTrees
|
||||
events agentexec.EventPublisher
|
||||
// steps is the workflow step vocabulary pinned definitions are compiled
|
||||
@@ -314,6 +315,7 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
Cfg: req.Cfg.ToConfig(),
|
||||
Forge: dependencies.forge,
|
||||
Store: dependencies.store,
|
||||
Calls: dependencies.calls,
|
||||
Trees: trees,
|
||||
Agent: agent,
|
||||
Reviewer: reviewer,
|
||||
|
||||
@@ -382,7 +382,7 @@ func startStateStoreGRPC(t *testing.T, local *pgstore.TaskDB) string {
|
||||
eda := pgstore.EDA(t, nil)
|
||||
staterpc.RegisterServer(server, staterpc.Deps{
|
||||
Tasks: local, Captures: eda, Mappings: eda, Bindings: eda,
|
||||
BindingDispatcher: eda, BindingTaskCreator: local,
|
||||
BindingDispatcher: eda, BindingTaskCreator: local, WorkflowCalls: local,
|
||||
})
|
||||
go func() { _ = server.Serve(listener) }()
|
||||
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
)
|
||||
|
||||
// stubRunner is the agent runner that lets a test workflow end in an
|
||||
// agent.run tail without an LLM: the call step under test needs no agent,
|
||||
// but the workflow must still end with an outcome.
|
||||
type stubRunner struct{}
|
||||
|
||||
func (stubRunner) Run(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
return agentexec.Result{
|
||||
Version: agentexec.ProtocolVersion, TaskID: req.TaskID, Attempt: req.Attempt,
|
||||
Stage: req.Stage, Status: agentexec.StatusPassed, Summary: "reported",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// fakeScratchTrees is the Trees for a scratch task: the workspace already
|
||||
// exists (the call step needs none, the agent.run tail works in it), and
|
||||
// nothing publishes.
|
||||
type fakeScratchTrees struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
func (f *fakeScratchTrees) Prepare(context.Context, string, string, string, int, string, string, string) (string, string, error) {
|
||||
return f.dir, "", nil
|
||||
}
|
||||
|
||||
func (*fakeScratchTrees) CommitAll(context.Context, string, string) (bool, error) { return false, nil }
|
||||
func (*fakeScratchTrees) Push(context.Context) error { return nil }
|
||||
func (*fakeScratchTrees) Diff(context.Context, string, string) (string, error) { return "", nil }
|
||||
func (*fakeScratchTrees) ChangedFiles(context.Context, string, string) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*fakeScratchTrees) ChangedLines(context.Context, string, string) (int, error) { return 0, nil }
|
||||
func (*fakeScratchTrees) Snapshot(context.Context, string, string) error { return nil }
|
||||
|
||||
// TestExecuteTaskRequestStartsWorkflowCallCallee proves the engine's
|
||||
// task.Caller wiring end to end: a run of a repository-none workflow carrying
|
||||
// one workflow.call step starts a real callee task row through the same
|
||||
// State Store gRPC contract the production worker dials
|
||||
// (docs/prds/workflow-calls.md), and the caller carries on to its tail.
|
||||
func TestExecuteTaskRequestStartsWorkflowCallCallee(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
st := pgstore.Open(t)
|
||||
|
||||
const callerYAML = "id: caller\nrepository: none\ninputs:\n src_ip: {type: string}\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n inputs: {src_ip: \"inputs.src_ip\"}\n wait: false\n - type: agent.run\n settings:\n mission: report\n"
|
||||
// A real caller row, claimed and running, so the store's enqueue reads
|
||||
// the same row shape production does.
|
||||
task, err := st.EnqueueChatTask(ctx, "", "", "workflow.call integration", "", "caller", "")
|
||||
if err != nil {
|
||||
t.Fatalf("seed caller task: %v", err)
|
||||
}
|
||||
if err := st.Transition(ctx, task.ID, task.Status, workflow.StatusRunning, "started"); err != nil {
|
||||
t.Fatalf("claim caller: %v", err)
|
||||
}
|
||||
task.Status = workflow.StatusRunning
|
||||
task.Inputs = map[string]any{"src_ip": "10.0.0.9"}
|
||||
task.WorkflowDefinitionYAML = callerYAML
|
||||
task.WorkflowDefinitionDigest = workflow.DigestDefinition(callerYAML)
|
||||
if err := st.Update(ctx, task); err != nil {
|
||||
t.Fatalf("seed caller task: %v", err)
|
||||
}
|
||||
|
||||
transport, err := agentnats.Connect(ctx, agentnats.Config{StateStoreURL: startStateStoreGRPC(t, st)}, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(transport.Close)
|
||||
|
||||
workDir := t.TempDir()
|
||||
request := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: config.Repo{},
|
||||
Cfg: config.Config{Models: map[string]string{"builder": "p/m"}}.ForTask(),
|
||||
WorkflowDefinition: callerYAML,
|
||||
}
|
||||
dependencies := taskDependencies{
|
||||
store: transport.Store(rpcTimeout),
|
||||
calls: transport.Calls(rpcTimeout),
|
||||
trees: &fakeScratchTrees{dir: workDir},
|
||||
steps: testSteps(t),
|
||||
}
|
||||
fakeRunner := runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner { return stubRunner{} })
|
||||
response, err := runTask(ctx, request, dependencies, fakeRunner, workDir, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Status != workflow.StatusCompleted {
|
||||
t.Fatalf("response status = %q (park reason %q), want completed", response.Status, response.Task.ParkReason)
|
||||
}
|
||||
|
||||
callee := calleeTaskOf(t, ctx, st, task.ID)
|
||||
if callee.Workflow != "callee" || callee.CallParentTaskID != task.ID || callee.CallDepth != 1 {
|
||||
t.Fatalf("callee = %+v, want workflow callee linked to task %d at depth 1", callee, task.ID)
|
||||
}
|
||||
if callee.Inputs["src_ip"] != "10.0.0.9" {
|
||||
t.Fatalf("callee inputs = %+v, want the call's resolved reference", callee.Inputs)
|
||||
}
|
||||
if callee.Org != org.OrgID("default") {
|
||||
t.Fatalf("callee org = %q, want the caller's %q", callee.Org, task.Org)
|
||||
}
|
||||
}
|
||||
|
||||
// calleeTaskOf reads the one callee task whose call_parent_task_id is
|
||||
// callerTaskID, straight off the pool the test owns.
|
||||
func calleeTaskOf(t *testing.T, ctx context.Context, st *pgstore.TaskDB, callerTaskID int64) *workflow.Task {
|
||||
t.Helper()
|
||||
rows, err := st.Pool.Query(ctx,
|
||||
`SELECT id, workflow, call_parent_task_id, call_depth, org_id, inputs
|
||||
FROM tasks WHERE call_parent_task_id = $1`, callerTaskID)
|
||||
if err != nil {
|
||||
t.Fatalf("callee query: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var callees []*workflow.Task
|
||||
for rows.Next() {
|
||||
var id, parent, depth int64
|
||||
var wf, orgID, inputs string
|
||||
if err := rows.Scan(&id, &wf, &parent, &depth, &orgID, &inputs); err != nil {
|
||||
t.Fatalf("callee scan: %v", err)
|
||||
}
|
||||
decoded, _ := workflowtask.DecodeInputs(inputs)
|
||||
callees = append(callees, &workflow.Task{
|
||||
ID: id, Workflow: wf, CallParentTaskID: parent, CallDepth: int(depth),
|
||||
Org: org.OrgID(orgID), Inputs: decoded,
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatalf("callee rows: %v", err)
|
||||
}
|
||||
if len(callees) != 1 {
|
||||
t.Fatalf("callee tasks of %d = %+v, want exactly one", callerTaskID, callees)
|
||||
}
|
||||
return callees[0]
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/agentboot"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/agentgit"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
@@ -50,6 +51,7 @@ type workerTransport interface {
|
||||
SubscribeTasks(context.Context, int64, agentnats.TaskHandler, *slog.Logger) (agentnats.Subscription, error)
|
||||
Forger(string, time.Duration) workflow.Forger
|
||||
Store(time.Duration) workflow.Store
|
||||
Calls(time.Duration) task.Caller
|
||||
Trees(string, string, time.Duration) agentnats.RemoteTrees
|
||||
}
|
||||
|
||||
@@ -152,6 +154,7 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies
|
||||
type taskServiceTransport interface {
|
||||
Forger(string, time.Duration) workflow.Forger
|
||||
Store(time.Duration) workflow.Store
|
||||
Calls(time.Duration) task.Caller
|
||||
Trees(string, string, time.Duration) agentnats.RemoteTrees
|
||||
EventPublisher() agentexec.EventPublisher
|
||||
}
|
||||
@@ -161,6 +164,7 @@ func executeTaskRequest(ctx context.Context, request taskrun.Request, transport
|
||||
dependencies := taskDependencies{
|
||||
forge: transport.Forger(request.Task.Identity, rpcTimeout),
|
||||
store: transport.Store(rpcTimeout),
|
||||
calls: transport.Calls(rpcTimeout),
|
||||
trees: transport.Trees(request.Task.Identity, request.WorktreeGrant, rpcTimeout),
|
||||
events: transport.EventPublisher(),
|
||||
steps: steps,
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
)
|
||||
|
||||
@@ -75,6 +76,8 @@ func (*workerTransportStub) Forger(string, time.Duration) workflow.Forger { retu
|
||||
|
||||
func (*workerTransportStub) Store(time.Duration) workflow.Store { return nil }
|
||||
|
||||
func (*workerTransportStub) Calls(time.Duration) task.Caller { return nil }
|
||||
|
||||
func (*workerTransportStub) Trees(string, string, time.Duration) agentnats.RemoteTrees { return nil }
|
||||
|
||||
func TestRunPassesCompleteTransportConfiguration(t *testing.T) {
|
||||
|
||||
@@ -284,6 +284,9 @@ func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps {
|
||||
if btc, ok := b.st.(storecontract.BindingTaskCreator); ok {
|
||||
deps.BindingTaskCreator = btc
|
||||
}
|
||||
if wc, ok := b.st.(storecontract.WorkflowCaller); ok {
|
||||
deps.WorkflowCalls = wc
|
||||
}
|
||||
if css, ok := b.st.(storecontract.ConfigSnapshotStore); ok {
|
||||
deps.ConfigSnapshots = css
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -98,6 +98,8 @@ const (
|
||||
StateStoreService_DeletePlaybookDispatches_FullMethodName = "/state.v1.StateStoreService/DeletePlaybookDispatches"
|
||||
StateStoreService_StreamUndispatchedCaptures_FullMethodName = "/state.v1.StateStoreService/StreamUndispatchedCaptures"
|
||||
StateStoreService_EnqueueBindingTask_FullMethodName = "/state.v1.StateStoreService/EnqueueBindingTask"
|
||||
StateStoreService_EnqueueCallTask_FullMethodName = "/state.v1.StateStoreService/EnqueueCallTask"
|
||||
StateStoreService_WorkflowCallStatus_FullMethodName = "/state.v1.StateStoreService/WorkflowCallStatus"
|
||||
)
|
||||
|
||||
// StateStoreServiceClient is the client API for StateStoreService service.
|
||||
@@ -243,6 +245,13 @@ type StateStoreServiceClient interface {
|
||||
StreamUndispatchedCaptures(ctx context.Context, in *StreamUndispatchedCapturesRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[StreamUndispatchedCapturesResponse], error)
|
||||
// BindingTaskCreator
|
||||
EnqueueBindingTask(ctx context.Context, in *EnqueueBindingTaskRequest, opts ...grpc.CallOption) (*EnqueueBindingTaskResponse, error)
|
||||
// WorkflowCaller: a workflow.call step starts its callee and, with
|
||||
// wait:true, reads it back while waiting. Both are the one sanctioned
|
||||
// widening of a task-scoped grant -- the callee is the caller's child
|
||||
// (docs/prds/workflow-calls.md). CallStatus re-checks the parent-child
|
||||
// relation server-side, so the grant only names the caller.
|
||||
EnqueueCallTask(ctx context.Context, in *EnqueueCallTaskRequest, opts ...grpc.CallOption) (*EnqueueCallTaskResponse, error)
|
||||
WorkflowCallStatus(ctx context.Context, in *WorkflowCallStatusRequest, opts ...grpc.CallOption) (*WorkflowCallStatusResponse, error)
|
||||
}
|
||||
|
||||
type stateStoreServiceClient struct {
|
||||
@@ -1070,6 +1079,26 @@ func (c *stateStoreServiceClient) EnqueueBindingTask(ctx context.Context, in *En
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) EnqueueCallTask(ctx context.Context, in *EnqueueCallTaskRequest, opts ...grpc.CallOption) (*EnqueueCallTaskResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(EnqueueCallTaskResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_EnqueueCallTask_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) WorkflowCallStatus(ctx context.Context, in *WorkflowCallStatusRequest, opts ...grpc.CallOption) (*WorkflowCallStatusResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(WorkflowCallStatusResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_WorkflowCallStatus_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// StateStoreServiceServer is the server API for StateStoreService service.
|
||||
// All implementations must embed UnimplementedStateStoreServiceServer
|
||||
// for forward compatibility.
|
||||
@@ -1213,6 +1242,13 @@ type StateStoreServiceServer interface {
|
||||
StreamUndispatchedCaptures(*StreamUndispatchedCapturesRequest, grpc.ServerStreamingServer[StreamUndispatchedCapturesResponse]) error
|
||||
// BindingTaskCreator
|
||||
EnqueueBindingTask(context.Context, *EnqueueBindingTaskRequest) (*EnqueueBindingTaskResponse, error)
|
||||
// WorkflowCaller: a workflow.call step starts its callee and, with
|
||||
// wait:true, reads it back while waiting. Both are the one sanctioned
|
||||
// widening of a task-scoped grant -- the callee is the caller's child
|
||||
// (docs/prds/workflow-calls.md). CallStatus re-checks the parent-child
|
||||
// relation server-side, so the grant only names the caller.
|
||||
EnqueueCallTask(context.Context, *EnqueueCallTaskRequest) (*EnqueueCallTaskResponse, error)
|
||||
WorkflowCallStatus(context.Context, *WorkflowCallStatusRequest) (*WorkflowCallStatusResponse, error)
|
||||
mustEmbedUnimplementedStateStoreServiceServer()
|
||||
}
|
||||
|
||||
@@ -1460,6 +1496,12 @@ func (UnimplementedStateStoreServiceServer) StreamUndispatchedCaptures(*StreamUn
|
||||
func (UnimplementedStateStoreServiceServer) EnqueueBindingTask(context.Context, *EnqueueBindingTaskRequest) (*EnqueueBindingTaskResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method EnqueueBindingTask not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) EnqueueCallTask(context.Context, *EnqueueCallTaskRequest) (*EnqueueCallTaskResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method EnqueueCallTask not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) WorkflowCallStatus(context.Context, *WorkflowCallStatusRequest) (*WorkflowCallStatusResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method WorkflowCallStatus not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) mustEmbedUnimplementedStateStoreServiceServer() {}
|
||||
func (UnimplementedStateStoreServiceServer) testEmbeddedByValue() {}
|
||||
|
||||
@@ -2882,6 +2924,42 @@ func _StateStoreService_EnqueueBindingTask_Handler(srv interface{}, ctx context.
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_EnqueueCallTask_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(EnqueueCallTaskRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).EnqueueCallTask(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_EnqueueCallTask_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).EnqueueCallTask(ctx, req.(*EnqueueCallTaskRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_WorkflowCallStatus_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(WorkflowCallStatusRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).WorkflowCallStatus(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_WorkflowCallStatus_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).WorkflowCallStatus(ctx, req.(*WorkflowCallStatusRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// StateStoreService_ServiceDesc is the grpc.ServiceDesc for StateStoreService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
@@ -3193,6 +3271,14 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "EnqueueBindingTask",
|
||||
Handler: _StateStoreService_EnqueueBindingTask_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "EnqueueCallTask",
|
||||
Handler: _StateStoreService_EnqueueCallTask_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "WorkflowCallStatus",
|
||||
Handler: _StateStoreService_WorkflowCallStatus_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
|
||||
@@ -254,6 +254,15 @@ type BindingTaskCreator interface {
|
||||
EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, inputs map[string]any) (*task.Task, error)
|
||||
}
|
||||
|
||||
// WorkflowCaller is the consumer surface a workflow.call step crosses to
|
||||
// start its callee and read it back while waiting (docs/prds/workflow-calls.md).
|
||||
// It is the workflow engine's own task.Caller contract, satisfied by the
|
||||
// PostgreSQL store and *staterpc.Client alike -- the same shape as
|
||||
// workflow.Store. The caller task ID is explicit so both the wire grant and
|
||||
// the store's own row check answer the same question: is this call this
|
||||
// task's call?
|
||||
type WorkflowCaller = task.Caller
|
||||
|
||||
// PlaybookDispatcher is the idempotency-ledger surface for side-effecting
|
||||
// playbook actions (docs/prds/eda-playbook-engine.md gap 2).
|
||||
// RecordPlaybookDispatch writes the (playbook, version, event, action) row
|
||||
@@ -421,6 +430,18 @@ var (
|
||||
// the sentinel is shared so a caller treats "already recorded" the same
|
||||
// way across the two surfaces.
|
||||
ErrAlreadyDispatched = errors.New("store: binding already dispatched for capture")
|
||||
// ErrCallNotYours is returned when a workflow.call status read names a
|
||||
// task that is not a callee of the calling task, or the caller itself
|
||||
// does not exist: a caller reads only the tasks it started
|
||||
// (docs/prds/workflow-calls.md).
|
||||
ErrCallNotYours = errors.New("store: task is not a callee of the calling task")
|
||||
// ErrCallCallerNotRunning is returned when a workflow.call enqueue names
|
||||
// a caller that is not running: a stale retry, or a bug in the caller.
|
||||
ErrCallCallerNotRunning = errors.New("store: workflow.call caller is not running")
|
||||
// ErrCallDepthExceeded is returned when a workflow.call enqueue would
|
||||
// pass workflow.MaxCallDepth. The engine refuses the same call first;
|
||||
// the store re-checks because it owns the table.
|
||||
ErrCallDepthExceeded = errors.New("store: workflow.call would pass the depth limit")
|
||||
// ErrMappingNotFound is returned when a mapping ID does not exist.
|
||||
ErrMappingNotFound = errors.New("store: mapping not found")
|
||||
// ErrEventTypeNotFound is returned when an event type ID does not exist.
|
||||
|
||||
@@ -25,8 +25,9 @@ type YAMLDefinition struct {
|
||||
|
||||
// repoFreeStepTypes are the step types that run without a repository. A
|
||||
// workflow whose repository is not required may use only these, since it may
|
||||
// run with no worktree at all.
|
||||
var repoFreeStepTypes = map[string]bool{AgentRunStepName: true}
|
||||
// run with no worktree at all. workflow.call needs none of the caller's own:
|
||||
// the callee's own repository mode decides whether its run clones.
|
||||
var repoFreeStepTypes = map[string]bool{AgentRunStepName: true, WorkflowCallStepName: true}
|
||||
|
||||
// StepRecord selects one registered step type and supplies its typed settings.
|
||||
type StepRecord struct {
|
||||
@@ -112,9 +113,13 @@ type (
|
||||
WorkflowDefinitionCollection = task.WorkflowDefinitionCollection
|
||||
)
|
||||
|
||||
// ValidateDefinitionCollection rejects malformed, duplicate, or mismatched definitions.
|
||||
// ValidateDefinitionCollection rejects malformed, duplicate, or mismatched
|
||||
// definitions, and every workflow.call step a definition carries: the callee
|
||||
// must exist, its inputs must be satisfiable, and the call graph must be
|
||||
// acyclic (docs/prds/workflow-calls.md).
|
||||
func ValidateDefinitionCollection(collection WorkflowDefinitionCollection, registry StepRegistry) error {
|
||||
seen := make(map[string]struct{}, len(collection.Definitions))
|
||||
parsed := make(map[string]YAMLDefinition, len(collection.Definitions))
|
||||
for _, entry := range collection.Definitions {
|
||||
if _, ok := seen[entry.ID]; ok {
|
||||
return fmt.Errorf("duplicate workflow id %q", entry.ID)
|
||||
@@ -127,8 +132,9 @@ func ValidateDefinitionCollection(collection WorkflowDefinitionCollection, regis
|
||||
if definition.ID != entry.ID {
|
||||
return fmt.Errorf("workflow entry id %q does not match YAML id %q", entry.ID, definition.ID)
|
||||
}
|
||||
parsed[entry.ID] = definition
|
||||
}
|
||||
return nil
|
||||
return validateWorkflowCalls(parsed)
|
||||
}
|
||||
|
||||
// DecodeDefinitionCollection strictly decodes the control-plane projection.
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package task
|
||||
|
||||
import "context"
|
||||
|
||||
// Caller is what a workflow.call step uses to start a callee run and read it
|
||||
// back while waiting. It is the consumer-owned contract the workflow engine
|
||||
// holds (the same shape as Store), satisfied by *staterpc.Client for an
|
||||
// agent-container run: the callee is a first-class task the State Store
|
||||
// derives from the caller's row (docs/prds/workflow-calls.md).
|
||||
type Caller interface {
|
||||
// StartCall enqueues the callee task for callerTaskID's workflow.call
|
||||
// step: org, workspace, identity, owner, repo and issue number are
|
||||
// derived from the caller's row, the callee carries the call's inputs,
|
||||
// and the callee's depth is the caller's + 1. The store refuses a
|
||||
// caller that is not running and a call past the depth limit.
|
||||
StartCall(ctx context.Context, callerTaskID int64, workflow string, inputs map[string]any) (*Task, error)
|
||||
// CallStatus reads one call's callee: its status and latest transition
|
||||
// detail. A store only answers for a task whose call_parent_task_id is
|
||||
// callerTaskID, so a caller reads only its own callees.
|
||||
CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (status, detail string, err error)
|
||||
}
|
||||
@@ -122,6 +122,14 @@ type Task struct {
|
||||
// budget: one shared counter made N operator retries eat the
|
||||
// review-remediation budget and vice versa.
|
||||
RemediationRounds int `json:"remediation_rounds"`
|
||||
// CallParentTaskID is the task whose workflow.call step started this
|
||||
// one; 0 on a root run. A caller reads only the tasks it started,
|
||||
// through WorkflowCallStatus.
|
||||
CallParentTaskID int64 `json:"call_parent_task_id,omitempty"`
|
||||
// CallDepth counts workflow.call hops from the root run. The engine
|
||||
// refuses a call past workflow.MaxCallDepth, and the store re-checks on
|
||||
// insert, so no enqueue path can pass the limit.
|
||||
CallDepth int `json:"call_depth,omitempty"`
|
||||
// CreatedAt and UpdatedAt are the SQLite row timestamps, exposed so
|
||||
// callers can show a task's age and last activity. They are written by
|
||||
// column defaults and the UPDATE statements, never by the caller.
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/skill"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
@@ -82,6 +83,11 @@ type TaskContext struct {
|
||||
Store Store
|
||||
Trees Trees
|
||||
Agent agentexec.Runner
|
||||
// Calls starts workflow.call callees and reads them back while a
|
||||
// wait:true caller waits. Nil is only safe for a workflow with no
|
||||
// workflow.call step: such a step in a runner with no capability
|
||||
// fails the run with a named error (docs/prds/workflow-calls.md).
|
||||
Calls task.Caller
|
||||
// Reviewer runs the adversarial self-review stage (StageReview). Nil
|
||||
// is only safe when Repo.ReviewEnabled is false; StageReview parks
|
||||
// rather than silently skipping if it is enabled with no Reviewer
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
// WorkflowCallStepName is the step type that starts another workflow as its
|
||||
// own run. The callee is a first-class task the State Store derives from the
|
||||
// caller's row: its own pinned definition, its own agent profile, its own
|
||||
// container and its own outcome (docs/prds/workflow-calls.md).
|
||||
const WorkflowCallStepName = "workflow.call"
|
||||
|
||||
// MaxCallDepth bounds how deep one run may call callees. A cycle is refused
|
||||
// at save, so the limit is not a loop guard -- it bounds a legitimate fan-out
|
||||
// chain, and every wait:true caller on the path holds its container while it
|
||||
// waits, so an unbounded depth would pin unbounded containers.
|
||||
const MaxCallDepth = 5
|
||||
|
||||
// callPollInterval is how often a wait:true caller re-reads its callee's
|
||||
// status. A var so tests do not sleep.
|
||||
var callPollInterval = 2 * time.Second
|
||||
|
||||
// workflowCallSettings are the workflow.call step's settings. An inputs value
|
||||
// is either a literal or a reference to the calling workflow's declared
|
||||
// input, written "inputs.<name>" -- the PRD's `inputs: { src_ip:
|
||||
// inputs.src_ip }` shape.
|
||||
type workflowCallSettings struct {
|
||||
Workflow string `yaml:"workflow"`
|
||||
Inputs map[string]any `yaml:"inputs,omitempty"`
|
||||
Wait bool `yaml:"wait,omitempty"`
|
||||
}
|
||||
|
||||
// callReference is the prefix an inputs value must carry to be read as a
|
||||
// reference to the calling workflow's declared input.
|
||||
const callReference = "inputs."
|
||||
|
||||
// refName is the identifier grammar a reference suffix must match, shared
|
||||
// with the workflow interface's input names.
|
||||
var refName = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
|
||||
// WorkflowCallStepType contributes the workflow.call step type.
|
||||
func WorkflowCallStepType() StepType {
|
||||
return StepType{Name: WorkflowCallStepName, Factory: newWorkflowCallStage}
|
||||
}
|
||||
|
||||
func newWorkflowCallStage(settings yaml.Node) (Stage, error) {
|
||||
var s workflowCallSettings
|
||||
if settings.Kind == 0 {
|
||||
return Stage{}, fmt.Errorf("%s: settings.workflow is required", WorkflowCallStepName)
|
||||
}
|
||||
if err := settings.Decode(&s); err != nil {
|
||||
return Stage{}, fmt.Errorf("%s: %w", WorkflowCallStepName, err)
|
||||
}
|
||||
if strings.TrimSpace(s.Workflow) == "" {
|
||||
return Stage{}, fmt.Errorf("%s: settings.workflow is required", WorkflowCallStepName)
|
||||
}
|
||||
for name, value := range s.Inputs {
|
||||
if _, ok := value.(map[string]any); ok {
|
||||
return Stage{}, fmt.Errorf("%s: input %q must be a literal or an inputs.<name> reference, not a nested document", WorkflowCallStepName, name)
|
||||
}
|
||||
if ref, ok := value.(string); ok && strings.HasPrefix(ref, callReference) && !refName.MatchString(strings.TrimPrefix(ref, callReference)) {
|
||||
return Stage{}, fmt.Errorf("%s: input %q reference %q must name an input as inputs.<name>", WorkflowCallStepName, name, ref)
|
||||
}
|
||||
}
|
||||
return Stage{Name: WorkflowCallStepName, Run: func(ctx context.Context, tc *TaskContext) error {
|
||||
return runWorkflowCall(ctx, s, tc)
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// runWorkflowCall starts the callee and, with wait true, waits for its
|
||||
// terminal state. The callee reports on its own run; the call is recorded on
|
||||
// the caller's timeline.
|
||||
func runWorkflowCall(ctx context.Context, s workflowCallSettings, tc *TaskContext) error {
|
||||
if tc.Calls == nil {
|
||||
return fmt.Errorf("%s: this runner carries no callee capability", WorkflowCallStepName)
|
||||
}
|
||||
if tc.Task.CallDepth+1 > MaxCallDepth {
|
||||
return fmt.Errorf("%s: call depth %d would pass the limit of %d", WorkflowCallStepName, tc.Task.CallDepth+1, MaxCallDepth)
|
||||
}
|
||||
inputs, err := resolveCallInputs(s.Inputs, tc.Task.Inputs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", WorkflowCallStepName, err)
|
||||
}
|
||||
callee, err := tc.Calls.StartCall(ctx, tc.Task.ID, s.Workflow, inputs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: start %q: %w", WorkflowCallStepName, s.Workflow, err)
|
||||
}
|
||||
started := map[string]any{"callee_task_id": callee.ID, "workflow": s.Workflow, "wait": s.Wait}
|
||||
if err := tc.EmitDurable(ctx, events.KindWorkflowCallStarted, WorkflowCallStepName,
|
||||
fmt.Sprintf("started %q as task %d", s.Workflow, callee.ID), started); err != nil {
|
||||
tc.Log.Warn("workflow call start not persisted", "err", err)
|
||||
}
|
||||
if !s.Wait {
|
||||
return nil
|
||||
}
|
||||
return awaitCallee(ctx, s, tc, callee.ID)
|
||||
}
|
||||
|
||||
// resolveCallInputs turns the call's saved values into the callee's inputs: a
|
||||
// reference reads the caller's actual input, a literal passes through.
|
||||
func resolveCallInputs(call, callerInputs map[string]any) (map[string]any, error) {
|
||||
if len(call) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
resolved := make(map[string]any, len(call))
|
||||
for name, value := range call {
|
||||
if ref, ok := value.(string); ok && isCallReference(ref) {
|
||||
got, present := callerInputs[strings.TrimPrefix(ref, callReference)]
|
||||
if !present {
|
||||
return nil, fmt.Errorf("input %q references %q, which the calling workflow's run does not carry", name, ref)
|
||||
}
|
||||
resolved[name] = got
|
||||
continue
|
||||
}
|
||||
resolved[name] = value
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
// isCallReference reports whether a saved string value is a reference to the
|
||||
// calling workflow's declared input. A literal that merely begins with
|
||||
// "inputs." but does not name an input reads as the literal it is.
|
||||
func isCallReference(value string) bool {
|
||||
return strings.HasPrefix(value, callReference) && refName.MatchString(strings.TrimPrefix(value, callReference))
|
||||
}
|
||||
|
||||
// callEnded reports whether a callee status ends the caller's wait. A parked
|
||||
// callee ends it too: the callee will not move without an operator, so the
|
||||
// caller must not wait for one.
|
||||
func callEnded(status string) bool {
|
||||
switch status {
|
||||
case StatusCompleted, StatusPROpen, StatusMerged, StatusParked, StatusDead, StatusRejected, StatusClosedWontDo:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// awaitCallee polls the callee until it is terminal, failing the stage with
|
||||
// the callee's detail when the callee did not succeed. The caller's own wall
|
||||
// clock bounds the wait.
|
||||
func awaitCallee(ctx context.Context, s workflowCallSettings, tc *TaskContext, callTaskID int64) error {
|
||||
for {
|
||||
status, detail, err := tc.Calls.CallStatus(ctx, tc.Task.ID, callTaskID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: read task %d: %w", WorkflowCallStepName, callTaskID, err)
|
||||
}
|
||||
if callEnded(status) {
|
||||
if callSucceeded(status) {
|
||||
if err := tc.EmitDurable(ctx, events.KindWorkflowCallFinished, WorkflowCallStepName,
|
||||
fmt.Sprintf("%q (task %d) finished: %s", s.Workflow, callTaskID, detail),
|
||||
map[string]any{"callee_task_id": callTaskID, "workflow": s.Workflow, "status": status, "detail": detail}); err != nil {
|
||||
tc.Log.Warn("workflow call finish not persisted", "err", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s: callee %q (task %d) ended %s: %s", WorkflowCallStepName, s.Workflow, callTaskID, status, detail)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(callPollInterval):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// callSucceeded reports whether a callee's terminal state satisfies its
|
||||
// caller. pr_open and merged are successes: a repository callee opens its PR
|
||||
// and reports on it as its own outcome.
|
||||
func callSucceeded(status string) bool {
|
||||
switch status {
|
||||
case StatusCompleted, StatusPROpen, StatusMerged:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// validateWorkflowCalls checks every workflow.call step against the rest of
|
||||
// the collection: the callee must exist, the call's inputs must satisfy the
|
||||
// callee's declared inputs, and no workflow may reach itself through calls
|
||||
// (docs/prds/workflow-calls.md).
|
||||
func validateWorkflowCalls(parsed map[string]YAMLDefinition) error {
|
||||
calls := make(map[string][]string, len(parsed))
|
||||
for id, d := range parsed {
|
||||
for i, step := range d.Steps {
|
||||
if step.Type != WorkflowCallStepName {
|
||||
continue
|
||||
}
|
||||
s, err := decodeCallSettings(step)
|
||||
if err != nil {
|
||||
return fmt.Errorf("workflow %q step %d: %w", id, i+1, err)
|
||||
}
|
||||
callee, ok := parsed[s.Workflow]
|
||||
if !ok {
|
||||
return fmt.Errorf("workflow %q step %d calls %q, which is not defined", id, i+1, s.Workflow)
|
||||
}
|
||||
if err := checkCallInputs(id, i, d.WorkflowInterface, callee, s.Inputs); err != nil {
|
||||
return err
|
||||
}
|
||||
calls[id] = append(calls[id], s.Workflow)
|
||||
}
|
||||
}
|
||||
return refuseCallCycles(calls)
|
||||
}
|
||||
|
||||
func decodeCallSettings(step StepRecord) (workflowCallSettings, error) {
|
||||
var s workflowCallSettings
|
||||
if err := step.Settings.Decode(&s); err != nil {
|
||||
return workflowCallSettings{}, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// checkCallInputs validates one call's saved inputs against the callee's
|
||||
// declared inputs, resolving references against the calling workflow's own
|
||||
// declared input types. Values are not known at save time -- types are.
|
||||
func checkCallInputs(callerID string, index int, caller task.WorkflowInterface, callee YAMLDefinition, inputs map[string]any) error {
|
||||
for name, value := range inputs {
|
||||
spec, ok := callee.Inputs[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("workflow %q step %d: input %q is not declared by %q", callerID, index+1, name, callee.ID)
|
||||
}
|
||||
resolved, err := callInputType(caller, name, value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("workflow %q step %d: %w", callerID, index+1, err)
|
||||
}
|
||||
if resolved == "" {
|
||||
// A null literal satisfies the type check vacuously; the
|
||||
// callee's own run enforces it against the real value.
|
||||
continue
|
||||
}
|
||||
if !task.TypeAccepts(spec.Type, resolved) {
|
||||
return fmt.Errorf("workflow %q step %d: input %q is %s, want %s", callerID, index+1, name, resolved, spec.Type)
|
||||
}
|
||||
}
|
||||
for name, spec := range callee.Inputs {
|
||||
if _, assigned := inputs[name]; !assigned && spec.Required {
|
||||
return fmt.Errorf("workflow %q step %d: input %q is required by %q", callerID, index+1, name, callee.ID)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// callInputType names the type a saved call value carries at run time: a
|
||||
// reference carries the calling workflow's declared input type, a literal its
|
||||
// own JSON type. An error means the reference names an input the caller does
|
||||
// not declare.
|
||||
func callInputType(caller task.WorkflowInterface, name string, value any) (string, error) {
|
||||
if ref, ok := value.(string); ok && isCallReference(ref) {
|
||||
spec, ok := caller.Inputs[strings.TrimPrefix(ref, callReference)]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("input %q references %q, which the calling workflow does not declare", name, ref)
|
||||
}
|
||||
return spec.Type, nil
|
||||
}
|
||||
if value == nil {
|
||||
return "", nil
|
||||
}
|
||||
return task.ValueType(value), nil
|
||||
}
|
||||
|
||||
// refuseCallCycles walks the call graph from every workflow and refuses one
|
||||
// that reaches itself, directly or through other workflows, naming the path.
|
||||
func refuseCallCycles(calls map[string][]string) error {
|
||||
const (
|
||||
unseen = 0
|
||||
open = 1
|
||||
done = 2
|
||||
)
|
||||
state := make(map[string]int, len(calls))
|
||||
var path []string
|
||||
var walk func(id string) error
|
||||
walk = func(id string) error {
|
||||
state[id] = open
|
||||
path = append(path, id)
|
||||
defer func() { state[id] = done; path = path[:len(path)-1] }()
|
||||
for _, callee := range calls[id] {
|
||||
switch state[callee] {
|
||||
case open:
|
||||
cycle := append(append([]string{}, path[cycleStart(path, callee):]...), callee)
|
||||
return fmt.Errorf("workflow %q calls itself through %s", callee, strings.Join(cycle, " -> "))
|
||||
case unseen:
|
||||
if err := walk(callee); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for id := range calls {
|
||||
if state[id] == unseen {
|
||||
if err := walk(id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// cycleStart finds where the open path enters the cycle, so the reported path
|
||||
// runs from the cycle's first workflow rather than from the DFS root.
|
||||
func cycleStart(path []string, cycleRoot string) int {
|
||||
for i, id := range path {
|
||||
if id == cycleRoot {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
|
||||
func callRegistry() StepRegistry {
|
||||
registry := BuiltinStepRegistry()
|
||||
registry[AgentRunStepName] = newAgentRunStage
|
||||
registry[WorkflowCallStepName] = newWorkflowCallStage
|
||||
return registry
|
||||
}
|
||||
|
||||
// fakeCaller records StartCall/CallStatus and answers the poll with a
|
||||
// scripted sequence of statuses.
|
||||
type fakeCaller struct {
|
||||
starts []startCall
|
||||
statuses map[int64][]statusAt
|
||||
statusErr error
|
||||
startErr error
|
||||
}
|
||||
|
||||
type startCall struct {
|
||||
callerID int64
|
||||
workflow string
|
||||
inputs map[string]any
|
||||
}
|
||||
|
||||
type statusAt struct {
|
||||
status string
|
||||
detail string
|
||||
}
|
||||
|
||||
func (f *fakeCaller) StartCall(_ context.Context, callerID int64, workflow string, inputs map[string]any) (*task.Task, error) {
|
||||
if f.startErr != nil {
|
||||
return nil, f.startErr
|
||||
}
|
||||
f.starts = append(f.starts, startCall{callerID: callerID, workflow: workflow, inputs: inputs})
|
||||
callee := &task.Task{ID: int64(len(f.starts) + 100), Workflow: workflow, CallParentTaskID: callerID, CallDepth: 1, Status: StatusQueued}
|
||||
return callee, nil
|
||||
}
|
||||
|
||||
func (f *fakeCaller) CallStatus(_ context.Context, callerID, callID int64) (string, string, error) {
|
||||
if f.statusErr != nil {
|
||||
return "", "", f.statusErr
|
||||
}
|
||||
seq := f.statuses[callID]
|
||||
if len(seq) == 0 {
|
||||
return "", "", nil
|
||||
}
|
||||
at := seq[0]
|
||||
if len(seq) > 1 {
|
||||
f.statuses[callID] = seq[1:]
|
||||
}
|
||||
return at.status, at.detail, nil
|
||||
}
|
||||
|
||||
func callerYAML(callee string, extra ...string) string {
|
||||
body := "id: caller\nsteps:\n - type: workflow.call\n settings:\n workflow: " + callee + "\n"
|
||||
for _, line := range extra {
|
||||
body += line + "\n"
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// ---- Save-time: collection validation ----
|
||||
|
||||
func TestValidateCollectionWorkflowCalls(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
collection WorkflowDefinitionCollection
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "callee is not defined",
|
||||
collection: collectionOf(
|
||||
callerYAML("missing"),
|
||||
),
|
||||
want: `calls "missing", which is not defined`,
|
||||
},
|
||||
{
|
||||
name: "direct self-call",
|
||||
collection: collectionOf(
|
||||
"id: w\nsteps:\n - type: workflow.call\n settings:\n workflow: w\n",
|
||||
),
|
||||
want: `calls itself through w -> w`,
|
||||
},
|
||||
{
|
||||
name: "indirect cycle",
|
||||
collection: collectionOf(
|
||||
"id: a\nsteps:\n - type: workflow.call\n settings:\n workflow: b\n",
|
||||
"id: b\nsteps:\n - type: workflow.call\n settings:\n workflow: a\n",
|
||||
),
|
||||
want: "calls itself through a -> b -> a",
|
||||
},
|
||||
{
|
||||
name: "reference to an undeclared caller input",
|
||||
collection: collectionOf(
|
||||
callerYAML("callee", " inputs: {src_ip: \"inputs.missing\"}"),
|
||||
"id: callee\ninputs:\n src_ip: {type: string}\nsteps:\n - type: agent.run\n settings:\n mission: m\n",
|
||||
),
|
||||
want: `input "src_ip" references "inputs.missing", which the calling workflow does not declare`,
|
||||
},
|
||||
{
|
||||
name: "callee input type mismatch",
|
||||
collection: collectionOf(
|
||||
"id: caller\ninputs:\n src_ip: {type: string}\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n inputs: {src_ip: \"inputs.src_ip\"}\n",
|
||||
"id: callee\ninputs:\n src_ip: {type: number}\nsteps:\n - type: agent.run\n settings:\n mission: m\n",
|
||||
),
|
||||
want: `input "src_ip" is string, want number`,
|
||||
},
|
||||
{
|
||||
name: "required callee input is not assigned",
|
||||
collection: collectionOf(
|
||||
"id: caller\ninputs:\n src_ip: {type: string}\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n inputs: {severity: \"inputs.src_ip\"}\n",
|
||||
"id: callee\ninputs:\n src_ip: {type: string, required: true}\n severity: {type: string}\nsteps:\n - type: agent.run\n settings:\n mission: m\n",
|
||||
),
|
||||
want: `input "src_ip" is required by "callee"`,
|
||||
},
|
||||
{
|
||||
name: "undeclared callee input",
|
||||
collection: collectionOf(
|
||||
"id: caller\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n inputs: {extra: 7}\n",
|
||||
"id: callee\nsteps:\n - type: agent.run\n settings:\n mission: m\n",
|
||||
),
|
||||
want: `input "extra" is not declared by "callee"`,
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := ValidateDefinitionCollection(test.collection, callRegistry())
|
||||
if err == nil || !strings.Contains(err.Error(), test.want) {
|
||||
t.Fatalf("ValidateDefinitionCollection() error = %v, want containing %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
valid := collectionOf(
|
||||
"id: caller\nrepository: none\ninputs:\n src_ip: {type: string, required: true}\n n: {type: number}\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n inputs: {src_ip: \"inputs.src_ip\", n: 3}\n",
|
||||
"id: callee\ninputs:\n src_ip: {type: string, required: true}\n n: {type: number}\nsteps:\n - type: agent.run\n settings:\n mission: m\n",
|
||||
)
|
||||
if err := ValidateDefinitionCollection(valid, callRegistry()); err != nil {
|
||||
t.Fatalf("ValidateDefinitionCollection(valid) = %v, want nil", err)
|
||||
}
|
||||
|
||||
// A chain of distinct workflows is not a cycle.
|
||||
chain := collectionOf(
|
||||
"id: a\nsteps:\n - type: workflow.call\n settings:\n workflow: b\n",
|
||||
"id: b\nsteps:\n - type: workflow.call\n settings:\n workflow: c\n",
|
||||
"id: c\nsteps:\n - type: agent.run\n settings:\n mission: m\n",
|
||||
)
|
||||
if err := ValidateDefinitionCollection(chain, callRegistry()); err != nil {
|
||||
t.Fatalf("ValidateDefinitionCollection(chain) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func collectionOf(yamls ...string) WorkflowDefinitionCollection {
|
||||
c := WorkflowDefinitionCollection{}
|
||||
for _, y := range yamls {
|
||||
c.Definitions = append(c.Definitions, WorkflowDefinitionEntry{ID: yamlID(y), YAML: y})
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func yamlID(y string) string {
|
||||
for line := range strings.SplitSeq(y, "\n") {
|
||||
if after, ok := strings.CutPrefix(line, "id: "); ok {
|
||||
return after
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ---- Runtime: the stage ----
|
||||
|
||||
func compileCaller(t *testing.T, yaml string) Workflow {
|
||||
t.Helper()
|
||||
wf, err := ParseAndCompile(yaml, callRegistry())
|
||||
if err != nil {
|
||||
t.Fatalf("ParseAndCompile: %v", err)
|
||||
}
|
||||
return wf
|
||||
}
|
||||
|
||||
func TestWorkflowCallStageRefusesWithoutCapability(t *testing.T) {
|
||||
wf := compileCaller(t, "id: caller\nrepository: none\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n")
|
||||
store := &recordingStore{}
|
||||
tc := &TaskContext{Task: &Task{ID: 3, Attempt: 1}, Store: store, Trees: &fakeTrees{}, Log: slog.New(slog.DiscardHandler)}
|
||||
Run(context.Background(), wf, tc)
|
||||
if !strings.Contains(tc.Task.ParkReason, "callee capability") {
|
||||
t.Fatalf("park reason %q; want a park naming the missing capability", tc.Task.ParkReason)
|
||||
}
|
||||
if len(store.ofKind(events.KindWorkflowCallStarted)) != 0 {
|
||||
t.Error("a refused call must not record a started event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowCallStageDepthLimit(t *testing.T) {
|
||||
wf := compileCaller(t, "id: caller\nrepository: none\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n")
|
||||
caller := &fakeCaller{}
|
||||
store := &recordingStore{}
|
||||
deep := &Task{ID: 3, Attempt: 1, CallDepth: MaxCallDepth}
|
||||
tc := &TaskContext{Task: deep, Calls: caller, Store: store, Trees: &fakeTrees{}, Log: slog.New(slog.DiscardHandler)}
|
||||
Run(context.Background(), wf, tc)
|
||||
if len(caller.starts) != 0 {
|
||||
t.Fatalf("StartCall ran %d times at the depth limit, want none", len(caller.starts))
|
||||
}
|
||||
if !strings.Contains(deep.ParkReason, "depth") {
|
||||
t.Fatalf("park reason %q; want a park naming the depth limit", deep.ParkReason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowCallWaitFalseStartsAndContinues(t *testing.T) {
|
||||
wf := compileCaller(t, "id: caller\nrepository: none\ninputs:\n src_ip: {type: string}\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n inputs: {src_ip: \"inputs.src_ip\"}\n wait: false\n - type: agent.run\n settings:\n mission: report\n")
|
||||
caller := &fakeCaller{}
|
||||
store := &recordingStore{}
|
||||
tc := &TaskContext{
|
||||
Task: &Task{ID: 3, Attempt: 1, Inputs: map[string]any{"src_ip": "10.0.0.9"}},
|
||||
Calls: caller, Store: store, Trees: &fakeTrees{dir: "/scratch/3"}, Log: slog.New(slog.DiscardHandler),
|
||||
Agent: &fakeAgentRunner{result: agentexec.Result{Status: agentexec.StatusPassed, Summary: "reported"}},
|
||||
Cfg: config.Config{Models: map[string]string{"builder": "p/m"}},
|
||||
}
|
||||
Run(context.Background(), wf, tc)
|
||||
if len(caller.starts) != 1 {
|
||||
t.Fatalf("StartCall ran %d times, want exactly one", len(caller.starts))
|
||||
}
|
||||
start := caller.starts[0]
|
||||
if start.callerID != 3 || start.workflow != "callee" {
|
||||
t.Errorf("start = %+v, want caller 3 calling callee", start)
|
||||
}
|
||||
if got := start.inputs["src_ip"]; got != "10.0.0.9" {
|
||||
t.Errorf("input src_ip = %v, want the caller's resolved input", got)
|
||||
}
|
||||
if tc.Outcome.Status != StatusCompleted {
|
||||
t.Errorf("outcome = %+v, want the caller to continue and complete", tc.Outcome)
|
||||
}
|
||||
started := store.ofKind(events.KindWorkflowCallStarted)
|
||||
if len(started) != 1 || started[0].Data["callee_task_id"] != int64(101) {
|
||||
t.Errorf("started events = %+v, want one carrying callee task 101", started)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowCallWaitTrueWaitsForTerminalCallee(t *testing.T) {
|
||||
wf := compileCaller(t, "id: caller\nrepository: none\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n wait: true\n - type: agent.run\n settings:\n mission: report\n")
|
||||
caller := &fakeCaller{statuses: map[int64][]statusAt{
|
||||
101: {{StatusQueued, ""}, {StatusRunning, ""}, {StatusCompleted, "contained 10.0.0.9"}},
|
||||
}}
|
||||
store := &recordingStore{}
|
||||
tc := &TaskContext{
|
||||
Task: &Task{ID: 3, Attempt: 1}, Calls: caller, Store: store,
|
||||
Trees: &fakeTrees{dir: "/scratch/3"}, Log: slog.New(slog.DiscardHandler),
|
||||
Agent: &fakeAgentRunner{result: agentexec.Result{Status: agentexec.StatusPassed, Summary: "reported"}},
|
||||
Cfg: config.Config{Models: map[string]string{"builder": "p/m"}},
|
||||
}
|
||||
callPollInterval = time.Millisecond
|
||||
defer func() { callPollInterval = 2 * time.Second }()
|
||||
Run(context.Background(), wf, tc)
|
||||
if tc.Outcome.Status != StatusCompleted {
|
||||
t.Errorf("outcome = %+v, want completed after the callee succeeded", tc.Outcome)
|
||||
}
|
||||
finished := store.ofKind(events.KindWorkflowCallFinished)
|
||||
if len(finished) != 1 || finished[0].Data["status"] != StatusCompleted || finished[0].Data["detail"] != "contained 10.0.0.9" {
|
||||
t.Errorf("finished events = %+v, want the callee's terminal status and detail", finished)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowCallWaitTrueFailsWhenCalleeFails(t *testing.T) {
|
||||
wf := compileCaller(t, "id: caller\nrepository: none\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n wait: true\n")
|
||||
caller := &fakeCaller{statuses: map[int64][]statusAt{
|
||||
101: {{StatusParked, "needs an operator"}},
|
||||
}}
|
||||
store := &recordingStore{}
|
||||
tc := &TaskContext{
|
||||
Task: &Task{ID: 3, Attempt: 1}, Calls: caller, Store: store,
|
||||
Trees: &fakeTrees{}, Log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
callPollInterval = time.Millisecond
|
||||
defer func() { callPollInterval = 2 * time.Second }()
|
||||
Run(context.Background(), wf, tc)
|
||||
if !strings.Contains(tc.Task.ParkReason, "needs an operator") {
|
||||
t.Fatalf("reason %q; want a park carrying the callee's detail", tc.Task.ParkReason)
|
||||
}
|
||||
if len(store.ofKind(events.KindWorkflowCallFinished)) != 0 {
|
||||
t.Error("a failed call must not record a finished event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowCallWaitTrueStartFailure(t *testing.T) {
|
||||
wf := compileCaller(t, "id: caller\nrepository: none\nsteps:\n - type: workflow.call\n settings:\n workflow: callee\n wait: true\n")
|
||||
caller := &fakeCaller{startErr: errors.New("callee is not enabled for this org")}
|
||||
store := &recordingStore{}
|
||||
tc := &TaskContext{
|
||||
Task: &Task{ID: 3, Attempt: 1}, Calls: caller, Store: store,
|
||||
Trees: &fakeTrees{}, Log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
Run(context.Background(), wf, tc)
|
||||
if !strings.Contains(tc.Task.ParkReason, "callee is not enabled") {
|
||||
t.Fatalf("reason %q; want the start failure parked", tc.Task.ParkReason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowCallSettingsValidation(t *testing.T) {
|
||||
for _, test := range []struct{ name, yaml, want string }{
|
||||
{"no workflow named", "id: w\nrepository: none\nsteps:\n - type: workflow.call\n", "settings.workflow is required"},
|
||||
{"works in a repository-free workflow", "id: w\nrepository: none\nsteps:\n - type: workflow.call\n settings:\n workflow: other\n", ""},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := ParseDefinition(test.yaml, callRegistry())
|
||||
if test.want == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("ParseDefinition() = %v, want nil", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), test.want) {
|
||||
t.Fatalf("ParseDefinition() error = %v, want containing %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -119,6 +119,16 @@ const (
|
||||
// the sender. data: source, capture_id, binding_id.
|
||||
KindUnsignedEvent = "unsigned_event"
|
||||
|
||||
// KindWorkflowCallStarted marks the moment a workflow.call step
|
||||
// started its callee run (docs/prds/workflow-calls.md). data:
|
||||
// callee_task_id, workflow, wait.
|
||||
KindWorkflowCallStarted = "workflow_call_started"
|
||||
// KindWorkflowCallFinished carries the terminal state of a waited-on
|
||||
// callee back onto the caller's timeline. data: callee_task_id,
|
||||
// workflow, status, detail. Only a call the caller waited on records
|
||||
// one; a wait:false callee reports on its own run.
|
||||
KindWorkflowCallFinished = "workflow_call_finished"
|
||||
|
||||
// KindUpdateReport carries the phase-2 outcome of a dashboard-initiated
|
||||
// update -- whether the restarted daemon came back up healthy and on
|
||||
// the version it claimed, relayed once on the boot that finds the
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
@@ -154,6 +155,39 @@ func (t *Transport) Store(timeout time.Duration) workflow.Store {
|
||||
return deadlineStore{Store: t.state, timeout: timeout}
|
||||
}
|
||||
|
||||
// Calls constructs the workflow.call RPC client over the same long-lived
|
||||
// State Store client (docs/prds/workflow-calls.md), bounded by timeout the
|
||||
// same way Store() is.
|
||||
func (t *Transport) Calls(timeout time.Duration) task.Caller {
|
||||
return deadlineCaller{Caller: t.state, timeout: timeout}
|
||||
}
|
||||
|
||||
// deadlineCaller applies a default per-call timeout to task.Caller calls
|
||||
// whose context carries no deadline of its own, matching deadlineStore.
|
||||
type deadlineCaller struct {
|
||||
task.Caller
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (d deadlineCaller) withDeadline(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if _, ok := ctx.Deadline(); ok || d.timeout <= 0 {
|
||||
return ctx, func() {}
|
||||
}
|
||||
return context.WithTimeout(ctx, d.timeout)
|
||||
}
|
||||
|
||||
func (d deadlineCaller) StartCall(ctx context.Context, callerTaskID int64, wf string, inputs map[string]any) (*task.Task, error) {
|
||||
ctx, cancel := d.withDeadline(ctx)
|
||||
defer cancel()
|
||||
return d.Caller.StartCall(ctx, callerTaskID, wf, inputs)
|
||||
}
|
||||
|
||||
func (d deadlineCaller) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, error) {
|
||||
ctx, cancel := d.withDeadline(ctx)
|
||||
defer cancel()
|
||||
return d.Caller.CallStatus(ctx, callerTaskID, callTaskID)
|
||||
}
|
||||
|
||||
// deadlineStore applies a default per-call timeout to workflow.Store calls
|
||||
// whose context carries no deadline of its own, matching storerpc.Client's
|
||||
// Timeout field semantics for the gRPC path.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
-- Workflow calls: the callee linkage on the task row
|
||||
-- (docs/prds/workflow-calls.md).
|
||||
--
|
||||
-- tasks.call_parent_task_id names the task whose workflow.call step started
|
||||
-- this one (0 on a root run) and call_depth counts workflow.call hops from
|
||||
-- the root. Both default so every existing row and writer reads as a depth-0
|
||||
-- root run; the depth limit itself is re-checked by the engine and the
|
||||
-- enqueue, so no path can write past it.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
ALTER TABLE tasks ADD COLUMN call_parent_task_id bigint NOT NULL DEFAULT 0;
|
||||
ALTER TABLE tasks ADD COLUMN call_depth int NOT NULL DEFAULT 0;
|
||||
CREATE INDEX idx_tasks_call_parent ON tasks (call_parent_task_id);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP INDEX idx_tasks_call_parent;
|
||||
ALTER TABLE tasks DROP COLUMN call_depth;
|
||||
ALTER TABLE tasks DROP COLUMN call_parent_task_id;
|
||||
@@ -0,0 +1,118 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: call_tasks.sql
|
||||
|
||||
package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const callStatusDetail = `-- name: CallStatusDetail :one
|
||||
SELECT detail FROM transitions WHERE task_id = $1 ORDER BY id DESC LIMIT 1
|
||||
`
|
||||
|
||||
// The latest transition detail of one call's callee, the summary a wait:true
|
||||
// caller receives (docs/prds/workflow-calls.md).
|
||||
func (q *Queries) CallStatusDetail(ctx context.Context, taskID int64) (string, error) {
|
||||
row := q.db.QueryRow(ctx, callStatusDetail, taskID)
|
||||
var detail string
|
||||
err := row.Scan(&detail)
|
||||
return detail, err
|
||||
}
|
||||
|
||||
const enqueueCallTask = `-- name: EnqueueCallTask :one
|
||||
WITH caller AS (
|
||||
SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.stage, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth FROM tasks t WHERE t.id = $1 FOR UPDATE
|
||||
)
|
||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth)
|
||||
SELECT
|
||||
caller.owner,
|
||||
caller.repo,
|
||||
COALESCE((
|
||||
SELECT MAX(existing.issue_number) FROM tasks existing
|
||||
WHERE existing.owner = caller.owner
|
||||
AND existing.repo = caller.repo
|
||||
AND existing.source = 'chat'
|
||||
), $2::bigint) + 1,
|
||||
$3, $4, 'chat', $5, 'chat', caller.identity,
|
||||
caller.org_id, caller.workspace_id, $6,
|
||||
caller.id, caller.call_depth + 1
|
||||
FROM caller
|
||||
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $7::int
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth
|
||||
`
|
||||
|
||||
type EnqueueCallTaskParams struct {
|
||||
ID int64
|
||||
FallbackIssueNumber int64
|
||||
Title string
|
||||
Body string
|
||||
Workflow string
|
||||
Inputs string
|
||||
MaxDepth int32
|
||||
}
|
||||
|
||||
// The callee of a workflow.call step (docs/prds/workflow-calls.md). The
|
||||
// caller row is read FOR UPDATE, so two steps of the same run cannot race a
|
||||
// depth re-check, and the callee inherits the caller's org, workspace,
|
||||
// identity, owner and repo: a called workflow works the same run's context
|
||||
// under its own definition and profile. The issue number is a fresh
|
||||
// synthetic one for the inherited owner/repo, the same allocator
|
||||
// InsertChatTask uses. The insert refuses a caller that is not running and
|
||||
// a call that would pass the depth limit (the engine checks it too; the
|
||||
// store is what owns the table, so it re-checks).
|
||||
func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, enqueueCallTask,
|
||||
arg.ID,
|
||||
arg.FallbackIssueNumber,
|
||||
arg.Title,
|
||||
arg.Body,
|
||||
arg.Workflow,
|
||||
arg.Inputs,
|
||||
arg.MaxDepth,
|
||||
)
|
||||
var i Task
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Owner,
|
||||
&i.Repo,
|
||||
&i.IssueNumber,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.Labels,
|
||||
&i.Status,
|
||||
&i.Workflow,
|
||||
&i.Stage,
|
||||
&i.Branch,
|
||||
&i.Plan,
|
||||
&i.Notes,
|
||||
&i.PrNumber,
|
||||
&i.TokensUsed,
|
||||
&i.Iterations,
|
||||
&i.Attempt,
|
||||
&i.ParkReason,
|
||||
&i.WatchCommentID,
|
||||
&i.ParkClass,
|
||||
&i.RemediationRounds,
|
||||
&i.RetryCount,
|
||||
&i.Source,
|
||||
&i.Identity,
|
||||
&i.BindingID,
|
||||
&i.BindingVersion,
|
||||
&i.ReviewPayload,
|
||||
&i.WorkflowDefinitionVersion,
|
||||
&i.WorkflowDefinitionDigest,
|
||||
&i.WorkflowDefinitionYaml,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.ReviewCursor,
|
||||
&i.Inputs,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -323,6 +323,8 @@ type Task struct {
|
||||
Inputs string
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
CallParentTaskID int64
|
||||
CallDepth int32
|
||||
}
|
||||
|
||||
type ToolCall struct {
|
||||
|
||||
@@ -48,7 +48,7 @@ func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediation
|
||||
const claimByIssue = `-- name: ClaimByIssue :one
|
||||
UPDATE tasks SET status = 'running', attempt = attempt + 1, updated_at = now()
|
||||
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth
|
||||
`
|
||||
|
||||
type ClaimByIssueParams struct {
|
||||
@@ -97,6 +97,8 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas
|
||||
&i.Inputs,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -111,7 +113,7 @@ WHERE id = (
|
||||
FOR UPDATE SKIP LOCKED
|
||||
LIMIT 1
|
||||
)
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth
|
||||
`
|
||||
|
||||
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
|
||||
@@ -157,6 +159,8 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
|
||||
&i.Inputs,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -266,7 +270,7 @@ VALUES (
|
||||
'default'
|
||||
)
|
||||
)
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id
|
||||
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth
|
||||
`
|
||||
|
||||
type InsertChatTaskParams struct {
|
||||
@@ -331,6 +335,8 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
||||
&i.Inputs,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -619,7 +625,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
|
||||
}
|
||||
|
||||
const taskByID = `-- name: TaskByID :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id FROM tasks WHERE id = $1
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth FROM tasks WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
@@ -662,12 +668,14 @@ func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
&i.Inputs,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const taskByIssue = `-- name: TaskByIssue :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
|
||||
`
|
||||
|
||||
type TaskByIssueParams struct {
|
||||
@@ -716,12 +724,14 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
|
||||
&i.Inputs,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const taskByPR = `-- name: TaskByPR :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
|
||||
`
|
||||
|
||||
type TaskByPRParams struct {
|
||||
@@ -779,6 +789,8 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
|
||||
&i.Inputs,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
-- name: EnqueueCallTask :one
|
||||
-- The callee of a workflow.call step (docs/prds/workflow-calls.md). The
|
||||
-- caller row is read FOR UPDATE, so two steps of the same run cannot race a
|
||||
-- depth re-check, and the callee inherits the caller's org, workspace,
|
||||
-- identity, owner and repo: a called workflow works the same run's context
|
||||
-- under its own definition and profile. The issue number is a fresh
|
||||
-- synthetic one for the inherited owner/repo, the same allocator
|
||||
-- InsertChatTask uses. The insert refuses a caller that is not running and
|
||||
-- a call that would pass the depth limit (the engine checks it too; the
|
||||
-- store is what owns the table, so it re-checks).
|
||||
WITH caller AS (
|
||||
SELECT t.* FROM tasks t WHERE t.id = $1 FOR UPDATE
|
||||
)
|
||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth)
|
||||
SELECT
|
||||
caller.owner,
|
||||
caller.repo,
|
||||
COALESCE((
|
||||
SELECT MAX(existing.issue_number) FROM tasks existing
|
||||
WHERE existing.owner = caller.owner
|
||||
AND existing.repo = caller.repo
|
||||
AND existing.source = 'chat'
|
||||
), sqlc.arg('fallback_issue_number')::bigint) + 1,
|
||||
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', caller.identity,
|
||||
caller.org_id, caller.workspace_id, sqlc.arg(inputs),
|
||||
caller.id, caller.call_depth + 1
|
||||
FROM caller
|
||||
WHERE caller.status = 'running' AND caller.call_depth + 1 <= sqlc.arg('max_depth')::int
|
||||
RETURNING *;
|
||||
|
||||
-- name: CallStatusDetail :one
|
||||
-- The latest transition detail of one call's callee, the summary a wait:true
|
||||
-- caller receives (docs/prds/workflow-calls.md).
|
||||
SELECT detail FROM transitions WHERE task_id = $1 ORDER BY id DESC LIMIT 1;
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
task "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
@@ -79,7 +79,7 @@ const syntheticIssueNumberBase = 1_000_000_000_000_000
|
||||
func taskFromRow(t postgresdb.Task) *workflow.Task {
|
||||
// The inputs column is only ever written by EncodeInputs, so a decode
|
||||
// failure cannot arise from stored data this package produced.
|
||||
inputs, _ := workflowtask.DecodeInputs(t.Inputs)
|
||||
inputs, _ := task.DecodeInputs(t.Inputs)
|
||||
return &workflow.Task{
|
||||
ID: t.ID,
|
||||
Owner: t.Owner,
|
||||
@@ -114,6 +114,8 @@ func taskFromRow(t postgresdb.Task) *workflow.Task {
|
||||
ReviewPayload: t.ReviewPayload,
|
||||
ParkClass: t.ParkClass,
|
||||
RemediationRounds: int(t.RemediationRounds),
|
||||
CallParentTaskID: t.CallParentTaskID,
|
||||
CallDepth: int(t.CallDepth),
|
||||
CreatedAt: t.CreatedAt,
|
||||
UpdatedAt: t.UpdatedAt,
|
||||
}
|
||||
@@ -126,6 +128,7 @@ var (
|
||||
_ storecontract.ConfigSnapshotStore = (*Store)(nil)
|
||||
_ storecontract.ChannelStatusStore = (*Store)(nil)
|
||||
_ storecontract.ApplyStatusStore = (*Store)(nil)
|
||||
_ task.Caller = (*Store)(nil)
|
||||
_ workflow.Store = (*Store)(nil)
|
||||
)
|
||||
|
||||
@@ -159,7 +162,7 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
|
||||
// EnqueueBindingTask enqueues a binding-triggered task and stamps its binding
|
||||
// provenance in a second statement (best-effort, as in the SQLite store).
|
||||
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, inputs map[string]any) (*workflow.Task, error) {
|
||||
encoded, err := workflowtask.EncodeInputs(inputs)
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -539,3 +542,80 @@ func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, err
|
||||
}
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
|
||||
// StartCall enqueues the callee of callerTaskID's workflow.call step
|
||||
// (docs/prds/workflow-calls.md). The store owns the table, so it re-checks
|
||||
// both runtime invariants the engine checks: the caller must be running and
|
||||
// the call must not pass workflow.MaxCallDepth. The callee inherits the
|
||||
// caller's org, workspace, identity, owner and repo, and takes a fresh
|
||||
// synthetic issue number, the same allocator chat tasks use.
|
||||
func (s *Store) StartCall(ctx context.Context, callerTaskID int64, wf string, inputs map[string]any) (*workflow.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
callee, err := s.queries().EnqueueCallTask(ctx, postgresdb.EnqueueCallTaskParams{
|
||||
ID: callerTaskID,
|
||||
FallbackIssueNumber: syntheticIssueNumberBase - 1,
|
||||
Title: fmt.Sprintf("workflow.call %s from task %d", wf, callerTaskID),
|
||||
Body: fmt.Sprintf("Started by a workflow.call step from task %d (workflow %q); the call's inputs travel with the task.", callerTaskID, wf),
|
||||
Workflow: wf,
|
||||
Inputs: encoded,
|
||||
MaxDepth: int32(workflow.MaxCallDepth),
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
// The WITH reads the caller FOR UPDATE before inserting, so "no
|
||||
// rows" means the caller is missing, not running, or past the depth
|
||||
// limit. Name which, off a plain read.
|
||||
return nil, s.callRefusal(ctx, callerTaskID)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return taskFromRow(callee), nil
|
||||
}
|
||||
|
||||
// callRefusal names why EnqueueCallTask inserted nothing. The three refusals
|
||||
// are distinct failures an operator reads differently: a missing caller is a
|
||||
// problem, a caller that is not running is a stale retry, and a depth
|
||||
// refusal is the limit doing its job. The two behavioural ones carry the
|
||||
// caller's detail in the log and the sentinel across the wire.
|
||||
func (s *Store) callRefusal(ctx context.Context, callerTaskID int64) error {
|
||||
caller, err := s.queries().TaskByID(ctx, callerTaskID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("store: workflow.call caller task %d does not exist: %w", callerTaskID, storecontract.ErrCallNotYours)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if caller.Status != "running" {
|
||||
return fmt.Errorf("store: workflow.call caller task %d is %s: %w", callerTaskID, caller.Status, storecontract.ErrCallCallerNotRunning)
|
||||
}
|
||||
return fmt.Errorf("store: workflow.call from task %d at depth %d: %w", callerTaskID, caller.CallDepth, storecontract.ErrCallDepthExceeded)
|
||||
}
|
||||
|
||||
// CallStatus reads one call's callee for a waiting caller: its status and
|
||||
// latest transition detail. The parent check is the row check the wire grant
|
||||
// cannot do: a caller reads only the tasks it started.
|
||||
func (s *Store) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, error) {
|
||||
callee, err := s.queries().TaskByID(ctx, callTaskID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", "", storecontract.ErrCallNotYours
|
||||
}
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if callee.CallParentTaskID != callerTaskID {
|
||||
return "", "", storecontract.ErrCallNotYours
|
||||
}
|
||||
// A callee that has not moved yet has no transition row; its queue
|
||||
// status is the whole answer, and the empty detail says so.
|
||||
detail, err := s.queries().CallStatusDetail(ctx, callTaskID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return callee.Status, "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return callee.Status, detail, nil
|
||||
}
|
||||
@@ -44,6 +44,7 @@ func (c *Client) Close() error { return nil }
|
||||
|
||||
var (
|
||||
_ task.Store = (*Client)(nil)
|
||||
_ task.Caller = (*Client)(nil)
|
||||
_ storecontract.TaskStore = (*Client)(nil)
|
||||
_ storecontract.TaskLogStore = (*Client)(nil)
|
||||
_ storecontract.CaptureStore = (*Client)(nil)
|
||||
@@ -566,6 +567,32 @@ func (c *Client) EnqueueBindingTask(ctx context.Context, owner, repo, title, bod
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
// WorkflowCaller: the workflow.call step's two RPCs (docs/prds/
|
||||
// workflow-calls.md). StartCall encodes the call's inputs in the same form
|
||||
// every other wire crossing uses; CallStatus rehydrates the sentinels the
|
||||
// server maps, so a caller's errors.Is(err, storecontract.ErrCallNotYours)
|
||||
// works across the wire.
|
||||
|
||||
func (c *Client) StartCall(ctx context.Context, callerTaskID int64, wf string, inputs map[string]any) (*task.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := c.client.EnqueueCallTask(ctx, &pb.EnqueueCallTaskRequest{CallerTaskId: callerTaskID, Workflow: wf, InputsJson: encoded})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return taskValue(r.Task), nil
|
||||
}
|
||||
|
||||
func (c *Client) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, error) {
|
||||
r, err := c.client.WorkflowCallStatus(ctx, &pb.WorkflowCallStatusRequest{CallerTaskId: callerTaskID, CallTaskId: callTaskID})
|
||||
if err != nil {
|
||||
return "", "", unmapError(err)
|
||||
}
|
||||
return r.Status, r.Detail, nil
|
||||
}
|
||||
|
||||
// Task log
|
||||
|
||||
// TaskLog reads one task attempt's decoded log over the wire. found=false with
|
||||
|
||||
@@ -81,7 +81,7 @@ func remoteTaskStore(t *testing.T, local *pgstore.TaskDB, logs storecontract.Tas
|
||||
listener := bufconn.Listen(1 << 20)
|
||||
server := grpc.NewServer()
|
||||
deps := Deps{
|
||||
Tasks: local, BindingTaskCreator: local,
|
||||
Tasks: local, BindingTaskCreator: local, WorkflowCalls: local,
|
||||
ConfigSnapshots: local, ApplyStatus: local,
|
||||
TaskLogs: logs,
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ var (
|
||||
errBindingDispatchUnavailable = status.Error(codes.Unavailable, "binding dispatcher unavailable")
|
||||
errBindingTaskCreatorUnavailable = status.Error(codes.Unavailable, "binding task creator unavailable")
|
||||
errPlaybookDispatcherUnavailable = status.Error(codes.Unavailable, "playbook dispatcher unavailable")
|
||||
errWorkflowCallerUnavailable = status.Error(codes.Unavailable, "workflow caller unavailable")
|
||||
// errTaskLogsUnavailable is the "this process cannot read task logs at
|
||||
// all" answer, and it is deliberately distinct from a found=false read
|
||||
// result: only the first is a deployment matter. A dashboard that receives
|
||||
@@ -89,6 +90,11 @@ type Deps struct {
|
||||
Sources storecontract.SourceStore
|
||||
BindingDispatcher storecontract.BindingDispatcher
|
||||
BindingTaskCreator storecontract.BindingTaskCreator
|
||||
// WorkflowCalls is the workflow.call surface (docs/prds/workflow-calls.md):
|
||||
// the workflow engine's task.Caller over this contract. Optional: nil
|
||||
// answers both RPCs with codes.Unavailable, which is the honest answer
|
||||
// for a store that owns no task table.
|
||||
WorkflowCalls storecontract.WorkflowCaller
|
||||
// PlaybookDispatcher is the side-effecting-action idempotency ledger
|
||||
// (docs/prds/eda-playbook-engine.md gap 2). Optional: nil disables the
|
||||
// two playbook dispatch RPCs with codes.Unavailable.
|
||||
@@ -801,6 +807,40 @@ func (s *server) EnqueueBindingTask(ctx context.Context, r *pb.EnqueueBindingTas
|
||||
return &pb.EnqueueBindingTaskResponse{Task: taskProto(t)}, nil
|
||||
}
|
||||
|
||||
// WorkflowCaller: a workflow.call step starts its callee and reads it back
|
||||
// while waiting (docs/prds/workflow-calls.md). The grant already named the
|
||||
// caller; the store's own row checks are what make both calls answer only
|
||||
// for this caller's own callees.
|
||||
|
||||
func (s *server) EnqueueCallTask(ctx context.Context, r *pb.EnqueueCallTaskRequest) (*pb.EnqueueCallTaskResponse, error) {
|
||||
if s.deps.WorkflowCalls == nil {
|
||||
return nil, errWorkflowCallerUnavailable
|
||||
}
|
||||
if r.CallerTaskId <= 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "caller task id is required")
|
||||
}
|
||||
inputs, err := task.DecodeInputs(r.InputsJson)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
t, err := s.deps.WorkflowCalls.StartCall(ctx, r.CallerTaskId, r.Workflow, inputs)
|
||||
if err != nil {
|
||||
return nil, s.logErr("EnqueueCallTask", err)
|
||||
}
|
||||
return &pb.EnqueueCallTaskResponse{Task: taskProto(t), Accepted: true}, nil
|
||||
}
|
||||
|
||||
func (s *server) WorkflowCallStatus(ctx context.Context, r *pb.WorkflowCallStatusRequest) (*pb.WorkflowCallStatusResponse, error) {
|
||||
if s.deps.WorkflowCalls == nil {
|
||||
return nil, errWorkflowCallerUnavailable
|
||||
}
|
||||
status, detail, err := s.deps.WorkflowCalls.CallStatus(ctx, r.CallerTaskId, r.CallTaskId)
|
||||
if err != nil {
|
||||
return nil, s.logErr("WorkflowCallStatus", err)
|
||||
}
|
||||
return &pb.WorkflowCallStatusResponse{Status: status, Detail: detail}, nil
|
||||
}
|
||||
|
||||
// Task log
|
||||
|
||||
func (s *server) taskLogs() (storecontract.TaskLogStore, error) {
|
||||
|
||||
@@ -90,10 +90,17 @@ func (g *TaskGrants) taskFor(token string) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// authorizesTaskScopedCall reports whether req -- a call to one of the three
|
||||
// task.Store RPCs a task grant may ever authorize -- targets taskID.
|
||||
// Every other RPC (including RegisterTaskGrant/RevokeTaskGrant themselves)
|
||||
// falls through to false: a task grant can only ever narrow, never expand.
|
||||
// authorizesTaskScopedCall reports whether req -- a call to one of the RPCs
|
||||
// a task grant may ever authorize -- targets taskID. Every other RPC
|
||||
// (including RegisterTaskGrant/RevokeTaskGrant themselves) falls through to
|
||||
// false: a task grant can only ever narrow, never expand.
|
||||
//
|
||||
// The two workflow.call RPCs are the one sanctioned widening
|
||||
// (docs/prds/workflow-calls.md): a workflow.call step must start its callee
|
||||
// and read it back, and the callee is the caller's child. Both requests
|
||||
// carry the caller's own task ID, which the grant check verifies; the
|
||||
// parent-child relation is a handler-side row check, so this interceptor
|
||||
// never reads the database.
|
||||
func authorizesTaskScopedCall(fullMethod string, req any, taskID int64) bool {
|
||||
switch fullMethod {
|
||||
case pb.StateStoreService_Update_FullMethodName:
|
||||
@@ -105,6 +112,12 @@ func authorizesTaskScopedCall(fullMethod string, req any, taskID int64) bool {
|
||||
case pb.StateStoreService_InsertEvent_FullMethodName:
|
||||
r, ok := req.(*pb.InsertEventRequest)
|
||||
return ok && r.Event != nil && r.Event.TaskId == taskID
|
||||
case pb.StateStoreService_EnqueueCallTask_FullMethodName:
|
||||
r, ok := req.(*pb.EnqueueCallTaskRequest)
|
||||
return ok && r.CallerTaskId == taskID
|
||||
case pb.StateStoreService_WorkflowCallStatus_FullMethodName:
|
||||
r, ok := req.(*pb.WorkflowCallStatusRequest)
|
||||
return ok && r.CallerTaskId == taskID
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
)
|
||||
|
||||
// TestTaskGrantAuthorizesTheWorkflowCallRPCs: a workflow.call step starts its
|
||||
// callee and reads it back while waiting, so the caller's task grant must
|
||||
// reach the two call RPCs for its own task -- and nothing else's
|
||||
// (docs/prds/workflow-calls.md). CallStatus's parent check stays server-side,
|
||||
// so the grant itself only ever verifies the request names the granted task.
|
||||
func TestTaskGrantAuthorizesTheWorkflowCallRPCs(t *testing.T) {
|
||||
const adminToken = "daemon-admin-token"
|
||||
_, dial := grantsServer(t, adminToken)
|
||||
admin := dial(t, adminToken)
|
||||
ctx := t.Context()
|
||||
|
||||
caller, err := admin.EnqueueChatTask(ctx, "acme", "widget", "caller", "body", "implement", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other, err := admin.EnqueueChatTask(ctx, "acme", "widget", "other", "body", "implement", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := admin.Transition(ctx, caller.ID, caller.Status, workflow.StatusRunning, "started"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
workerToken, err := admin.RegisterTaskGrant(ctx, caller.ID, callGrantLifetime)
|
||||
if err != nil {
|
||||
t.Fatalf("RegisterTaskGrant: %v", err)
|
||||
}
|
||||
worker := dial(t, workerToken)
|
||||
|
||||
callee, err := worker.StartCall(ctx, caller.ID, "callee", map[string]any{"src_ip": "10.0.0.9"})
|
||||
if err != nil {
|
||||
t.Fatalf("task grant should authorize StartCall on its own task: %v", err)
|
||||
}
|
||||
if callee.CallParentTaskID != caller.ID || callee.CallDepth != 1 {
|
||||
t.Fatalf("callee = %+v, want the parent link and depth 1", callee)
|
||||
}
|
||||
if _, _, err := worker.CallStatus(ctx, caller.ID, callee.ID); err != nil {
|
||||
t.Fatalf("task grant should authorize CallStatus on its own callees: %v", err)
|
||||
}
|
||||
if _, _, err := worker.CallStatus(ctx, caller.ID, other.ID); !errors.Is(err, storecontract.ErrCallNotYours) {
|
||||
t.Fatalf("CallStatus for a task that is not this caller's callee = %v, want ErrCallNotYours", err)
|
||||
}
|
||||
|
||||
// The grant names one caller: another task's grant cannot start calls
|
||||
// under it, and the granted caller cannot be named by someone else's
|
||||
// token -- the request-shape check refuses both before the store runs.
|
||||
if _, err := worker.StartCall(ctx, other.ID, "callee", nil); status.Code(err) != codes.PermissionDenied {
|
||||
t.Fatalf("StartCall for another task = %v, want PermissionDenied", err)
|
||||
}
|
||||
if _, _, err := worker.CallStatus(ctx, other.ID, callee.ID); status.Code(err) != codes.PermissionDenied {
|
||||
t.Fatalf("CallStatus for another task = %v, want PermissionDenied", err)
|
||||
}
|
||||
|
||||
// The sentinels survive the wire: a stale retry surfaces the documented
|
||||
// refusal, not an opaque internal. The admin dial carries it because the
|
||||
// worker's grant names a caller that is still running; the refusal is
|
||||
// what a later grant for a finished caller's task would see.
|
||||
if _, err := admin.StartCall(ctx, callee.ID, "deeper", nil); !errors.Is(err, storecontract.ErrCallCallerNotRunning) {
|
||||
t.Fatalf("StartCall from a completed caller = %v, want ErrCallCallerNotRunning", err)
|
||||
}
|
||||
}
|
||||
|
||||
// callGrantLifetime mirrors the daemon's defaultGrantLifetime; the value is
|
||||
// not under test, only the scoping.
|
||||
const callGrantLifetime = defaultGrantLifetime
|
||||
|
||||
// The wire requests this grant widens are the two call RPCs only; this
|
||||
// compile-time check pins the full-method names the interceptor cases match
|
||||
// on, so a rename on the proto side is caught here rather than at runtime.
|
||||
var (
|
||||
_ = pb.StateStoreService_EnqueueCallTask_FullMethodName
|
||||
_ = pb.StateStoreService_WorkflowCallStatus_FullMethodName
|
||||
)
|
||||
@@ -41,6 +41,7 @@ func grantsServer(t *testing.T, adminToken string) (grants *TaskGrants, dial fun
|
||||
Tasks: local, ConfigSnapshots: local, ApplyStatus: local,
|
||||
Captures: eda, Mappings: eda, Bindings: eda,
|
||||
BindingDispatcher: eda, PlaybookDispatcher: eda, EventTypes: eda,
|
||||
BindingTaskCreator: local, WorkflowCalls: local,
|
||||
Grants: grants,
|
||||
})
|
||||
go func() { _ = server.Serve(listener) }()
|
||||
@@ -163,7 +164,7 @@ func TestUpdateRejectsNilTask(t *testing.T) {
|
||||
// Transition/InsertEvent on its own task ID, never the admin-only surface
|
||||
// (StatusCounts here stands in for any of the other ~37 RPCs) and never
|
||||
// another task's ID.
|
||||
func TestTaskGrantScopesWorkerToItsOwnThreeRPCs(t *testing.T) {
|
||||
func TestTaskGrantScopesWorkerToItsOwnRPCs(t *testing.T) {
|
||||
const adminToken = "daemon-admin-token"
|
||||
grants, dial := grantsServer(t, adminToken)
|
||||
admin := dial(t, adminToken)
|
||||
|
||||
@@ -80,6 +80,8 @@ func taskProto(t *task.Task) *pb.Task { //nolint:dupl // mirror-image field-by-f
|
||||
WorkflowDefinitionDigest: t.WorkflowDefinitionDigest,
|
||||
WorkflowDefinitionYaml: t.WorkflowDefinitionYAML,
|
||||
Org: string(t.Org),
|
||||
CallParentTaskId: t.CallParentTaskID,
|
||||
CallDepth: int32(t.CallDepth),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +107,8 @@ func taskValue(t *pb.Task) *task.Task { //nolint:dupl // see taskProto above
|
||||
WorkflowDefinitionDigest: t.WorkflowDefinitionDigest,
|
||||
WorkflowDefinitionYAML: t.WorkflowDefinitionYaml,
|
||||
Org: org.OrgID(t.Org),
|
||||
CallParentTaskID: t.CallParentTaskId,
|
||||
CallDepth: int(t.CallDepth),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -453,6 +457,9 @@ var wireErrors = []struct {
|
||||
{storecontract.ErrEventTypeNotFound, codes.NotFound, msgEventTypeNotFound},
|
||||
{storecontract.ErrSourceNotFound, codes.NotFound, msgSourceNotFound},
|
||||
{storecontract.ErrAlreadyDispatched, codes.AlreadyExists, msgAlreadyDispatched},
|
||||
{storecontract.ErrCallNotYours, codes.PermissionDenied, storecontract.ErrCallNotYours.Error()},
|
||||
{storecontract.ErrCallCallerNotRunning, codes.FailedPrecondition, storecontract.ErrCallCallerNotRunning.Error()},
|
||||
{storecontract.ErrCallDepthExceeded, codes.FailedPrecondition, storecontract.ErrCallDepthExceeded.Error()},
|
||||
{storecontract.ErrSourcePathTaken, codes.AlreadyExists, msgSourcePathTaken},
|
||||
{storepkg.ErrNotFound, codes.NotFound, storepkg.ErrNotFound.Error()},
|
||||
{storepkg.ErrInstalled, codes.AlreadyExists, storepkg.ErrInstalled.Error()},
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package staterpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// workflowCallerContract is the workflow.call surface both adapters must
|
||||
// satisfy identically (§11), including refusal-sentinel fidelity across the
|
||||
// wire (docs/prds/workflow-calls.md).
|
||||
type workflowCallerContract interface {
|
||||
storecontract.WorkflowCaller
|
||||
}
|
||||
|
||||
// TestWorkflowCallerConformance drives StartCall/CallStatus through the local
|
||||
// PostgreSQL store and a bufconn-backed gRPC client.
|
||||
func TestWorkflowCallerConformance(t *testing.T) {
|
||||
for _, mode := range []string{"local", "grpc"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
local := pgstore.Open(t)
|
||||
var c workflowCallerContract = local
|
||||
if mode == "grpc" {
|
||||
c = remoteTaskStore(t, local, nil, nil)
|
||||
}
|
||||
|
||||
caller, err := local.EnqueueChatTask(ctx, "acme", "widget", "caller", "body", "implement", "")
|
||||
if err != nil {
|
||||
t.Fatalf("EnqueueChatTask: %v", err)
|
||||
}
|
||||
if err := local.Transition(ctx, caller.ID, caller.Status, workflow.StatusRunning, "started"); err != nil {
|
||||
t.Fatalf("Transition caller: %v", err)
|
||||
}
|
||||
|
||||
// A running caller's callee inherits its row and links back to it.
|
||||
callee, err := c.StartCall(ctx, caller.ID, "callee", map[string]any{"src_ip": "10.0.0.9"})
|
||||
if err != nil || callee == nil {
|
||||
t.Fatalf("StartCall: (%+v, %v)", callee, err)
|
||||
}
|
||||
if callee.Workflow != "callee" || callee.CallParentTaskID != caller.ID || callee.CallDepth != 1 {
|
||||
t.Fatalf("callee = %+v, want the parent link and depth 1", callee)
|
||||
}
|
||||
if callee.Org != caller.Org || callee.Identity != caller.Identity {
|
||||
t.Fatalf("callee org/identity = %q/%q, want the caller's %q/%q", callee.Org, callee.Identity, caller.Org, caller.Identity)
|
||||
}
|
||||
if callee.Owner != caller.Owner || callee.Repo != caller.Repo {
|
||||
t.Fatalf("callee owner/repo = %s/%s, want the caller's %s/%s", callee.Owner, callee.Repo, caller.Owner, caller.Repo)
|
||||
}
|
||||
if callee.Source != "chat" || callee.IssueNumber == caller.IssueNumber {
|
||||
t.Fatalf("callee source/issue = %s/%d, want a chat source with a fresh synthetic number", callee.Source, callee.IssueNumber)
|
||||
}
|
||||
if callee.Inputs["src_ip"] != "10.0.0.9" {
|
||||
t.Fatalf("callee inputs = %+v, want the call's inputs", callee.Inputs)
|
||||
}
|
||||
|
||||
// A callee that has not moved yet answers its queued status with
|
||||
// no detail; after a transition the detail is the latest one.
|
||||
status, detail, err := c.CallStatus(ctx, caller.ID, callee.ID)
|
||||
if err != nil || status != workflow.StatusQueued || detail != "" {
|
||||
t.Fatalf("CallStatus queued = (%q, %q, %v), want (queued, \"\", nil)", status, detail, err)
|
||||
}
|
||||
if err := local.Transition(ctx, callee.ID, callee.Status, workflow.StatusRunning, "started"); err != nil {
|
||||
t.Fatalf("Transition callee to running: %v", err)
|
||||
}
|
||||
if err := local.Transition(ctx, callee.ID, workflow.StatusRunning, workflow.StatusCompleted, "contained 10.0.0.9"); err != nil {
|
||||
t.Fatalf("Transition callee to completed: %v", err)
|
||||
}
|
||||
status, detail, err = c.CallStatus(ctx, caller.ID, callee.ID)
|
||||
if err != nil || status != workflow.StatusCompleted || detail != "contained 10.0.0.9" {
|
||||
t.Fatalf("CallStatus completed = (%q, %q, %v), want the terminal status and detail", status, detail, err)
|
||||
}
|
||||
|
||||
// A caller reads only its own callees, over both adapters.
|
||||
_, _, err = c.CallStatus(ctx, 99999, callee.ID)
|
||||
if !errors.Is(err, storecontract.ErrCallNotYours) {
|
||||
t.Fatalf("CallStatus foreign caller = %v, want ErrCallNotYours", err)
|
||||
}
|
||||
_, _, err = c.CallStatus(ctx, caller.ID, 99999)
|
||||
if !errors.Is(err, storecontract.ErrCallNotYours) {
|
||||
t.Fatalf("CallStatus missing callee = %v, want ErrCallNotYours", err)
|
||||
}
|
||||
|
||||
// Refusals: a caller that is not running, and the depth limit. The
|
||||
// sentinels cross the wire, so both adapters report the same one.
|
||||
if _, err := c.StartCall(ctx, callee.ID, "deeper", nil); !errors.Is(err, storecontract.ErrCallCallerNotRunning) {
|
||||
t.Fatalf("StartCall from a completed caller = %v, want ErrCallCallerNotRunning", err)
|
||||
}
|
||||
// The depth limit is re-checked by the store, not just the engine:
|
||||
// walk a chain of callees, claiming each so it is running, until
|
||||
// the insert itself refuses at MaxCallDepth.
|
||||
current, err := c.StartCall(ctx, caller.ID, "deeper", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("StartCall at depth 1: %v", err)
|
||||
}
|
||||
for current.CallDepth < workflow.MaxCallDepth {
|
||||
claimed, err := local.ClaimByIssue(ctx, current.Owner, current.Repo, current.IssueNumber)
|
||||
if err != nil || claimed == nil {
|
||||
t.Fatalf("claim depth %d callee: (%+v, %v)", current.CallDepth, claimed, err)
|
||||
}
|
||||
current, err = c.StartCall(ctx, current.ID, "deeper", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("StartCall at depth %d: %v", claimed.CallDepth+1, err)
|
||||
}
|
||||
}
|
||||
if _, err := local.ClaimByIssue(ctx, current.Owner, current.Repo, current.IssueNumber); err != nil {
|
||||
t.Fatalf("claim depth %d callee: %v", current.CallDepth, err)
|
||||
}
|
||||
if _, err := c.StartCall(ctx, current.ID, "deeper", nil); !errors.Is(err, storecontract.ErrCallDepthExceeded) {
|
||||
t.Fatalf("StartCall past the depth limit = %v, want ErrCallDepthExceeded", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ type eventSteps struct{}
|
||||
func (eventSteps) Name() string { return "event" }
|
||||
|
||||
func (eventSteps) StepTypes() []workflow.StepType {
|
||||
return []workflow.StepType{workflow.AgentRunStepType()}
|
||||
return []workflow.StepType{workflow.AgentRunStepType(), workflow.WorkflowCallStepType()}
|
||||
}
|
||||
|
||||
// Providers returns the provider set the roots that resolve a workflow step
|
||||
|
||||
@@ -157,6 +157,14 @@ service StateStoreService {
|
||||
|
||||
// BindingTaskCreator
|
||||
rpc EnqueueBindingTask(EnqueueBindingTaskRequest) returns (EnqueueBindingTaskResponse);
|
||||
|
||||
// WorkflowCaller: a workflow.call step starts its callee and, with
|
||||
// wait:true, reads it back while waiting. Both are the one sanctioned
|
||||
// widening of a task-scoped grant -- the callee is the caller's child
|
||||
// (docs/prds/workflow-calls.md). CallStatus re-checks the parent-child
|
||||
// relation server-side, so the grant only names the caller.
|
||||
rpc EnqueueCallTask(EnqueueCallTaskRequest) returns (EnqueueCallTaskResponse);
|
||||
rpc WorkflowCallStatus(WorkflowCallStatusRequest) returns (WorkflowCallStatusResponse);
|
||||
}
|
||||
|
||||
message RegisterTaskGrantRequest {
|
||||
@@ -282,6 +290,12 @@ message Task {
|
||||
// identity when the row is written; a task of a single-operator install
|
||||
// belongs to the default org (docs/prds/orgs-and-access.md).
|
||||
string org = 35;
|
||||
// The workflow.call linkage (docs/prds/workflow-calls.md): the task whose
|
||||
// workflow.call step started this one (0 on a root run) and how many
|
||||
// call hops this row is from that root. Columns default, so an older
|
||||
// writer reads as a depth-0 root run.
|
||||
int64 call_parent_task_id = 36;
|
||||
int32 call_depth = 37;
|
||||
}
|
||||
|
||||
// Event mirrors internal/events.Event. Data is carried as a JSON object
|
||||
@@ -862,6 +876,29 @@ message EnqueueBindingTaskRequest {
|
||||
}
|
||||
message EnqueueBindingTaskResponse { Task task = 1; }
|
||||
|
||||
// workflow.call: the caller's grant names the caller; the store derives the
|
||||
// rest from the caller's row (docs/prds/workflow-calls.md).
|
||||
message EnqueueCallTaskRequest {
|
||||
int64 caller_task_id = 1;
|
||||
string workflow = 2;
|
||||
string inputs_json = 3;
|
||||
}
|
||||
message EnqueueCallTaskResponse {
|
||||
Task task = 1;
|
||||
bool accepted = 2;
|
||||
}
|
||||
|
||||
message WorkflowCallStatusRequest {
|
||||
int64 caller_task_id = 1;
|
||||
int64 call_task_id = 2;
|
||||
}
|
||||
message WorkflowCallStatusResponse {
|
||||
// status is the callee's task status and detail its latest transition
|
||||
// detail (empty before the callee first moves).
|
||||
string status = 1;
|
||||
string detail = 2;
|
||||
}
|
||||
|
||||
message InstalledPackage {
|
||||
string org_id = 1;
|
||||
string name = 2;
|
||||
|
||||
Reference in new issue
Block a user