feat(store): workflow.call contract — callee enqueue, parent-scoped status read, task-scoped grant widening (archie-core-t2db.41)

This commit is contained in:
sam committed 2026-09-26 13:36:41 +10:00
1 parent 208a1a9c38
commit a3c0c76615
24 files changed
+1268 -254

No files matched your search

@@ -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() })
+4
View File
@@ -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,
+3
View File
@@ -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) {
+3
View File
@@ -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.
@@ -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;
+83 -3
View File
@@ -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,114 @@
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.StatusCompleted, "contained 10.0.0.9"); err != nil {
t.Fatalf("Transition callee: %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)
}
})
}
}
+37
View File
@@ -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;