Repository navigation
Description
Context
Issue 2 created the agent_runs table. This issue adds packages/core/src/services/agents/agentRunStore.ts, the only module that writes run rows. Several processes touch a run concurrently: the API (trigger, approve/reject, cancel), the daemon (schedule and deferred retry), and the worker (report and acting phases). A guarded state machine — every transition is a compare-and-set UPDATE ... WHERE state IN (allowed) — prevents a late worker write from resurrecting a cancelled run, the same technique McpOperations uses for receipts.
This module also implements "previous reports as a first-class input": listPreviousReports(definitionId, limit, beforeRunId) returns the newest completed reports so a recurring agent can diff against last time.
Requirements
createAgentRun({ definition, trigger, triggerSource, idempotencyKey, initialState })inserts withonConflict(['definition_id','idempotency_key']).ignore()and returns{ run, created }. A replay with the same key returns the existing run (created: false).transitionAgentRun(id, from: AgentRunState[], to, patch)— guarded update; returns the updated run ornullif the run was no longer in an allowed state. Terminal states always setfinished_at.- An explicit allowed-transition table
AGENT_RUN_TRANSITIONSdocumented in code:queued → running | deferred | skipped | cancelled | faileddeferred → queued | skipped | cancelledrunning → report_ready | failed | cancelledreport_ready → completed (dry_run) | awaiting_approval (preview) | acting (auto) | failedawaiting_approval → acting | rejected | cancelledacting → completed | failed | cancelled
getAgentRun(id, ownerId),listAgentRuns(definitionId, ownerId, page),getAgentRunById(id)(internal),listDueDeferredRuns(now, limit).listPreviousReports(definitionId, { limit, beforeCreatedAt })returning{ runId, reportedAt, report }[]newest-first, only runs that actually produced a report (report_ready,awaiting_approval,acting,completed,rejected).rowToAgentRunmapper and export frompackages/core/src/index.ts.- Tests in
test/agentRunStore.test.ts.
Implementation Specification
transitionAgentRunvalidatestoagainstAGENT_RUN_TRANSITIONSfor everyfromand throws a programming error for an illegal pair (catches mistakes in review/tests rather than at runtime).reportis truncated toAGENT_REPORT_MAX_CHARSon write andreport_truncatedset; the full agent output remains available through the underlying task's logs.- Deferred runs store
deferred_untiland incrementdeferrals(issue 11/12 use this).
Acceptance Criteria
- Creating twice with the same idempotency key yields one row and
created: falsethe second time. - A transition from
cancelledtoreport_readyreturnsnulland leaves the row unchanged. -
listPreviousReportsexcludes failed/skipped runs and the current run, ordered newest first, bounded bylimit. - Owner scoping:
getAgentRun(id, 'other')returnsundefined. - Tests pass.
Created by ProPR AI Planner
Activity
propr-dev commentedon Oct 6, 2026
Suggested Implementation:
Implementation Approach
All run writes go through one guarded state machine. We chose compare-and-set SQL updates (
WHERE state IN (...)) over row locks or Redis locks because SQLite serializes writes anyway, the pattern is already proven inMcpOperationsandtaskSubmissionService, and it makes races observable (nullresult) rather than silent. Idempotency uses a unique(definition_id, idempotency_key)index withINSERT ... ON CONFLICT IGNORE, the same asmcp_operations, so an external caller (GitHub Action, webhook) can safely retry the trigger primitive.File:
packages/core/src/services/agents/agentRunStore.ts(new file)Why this file: The one writer for
agent_runs. API routes, the scheduler and the worker all call these functions, so lifecycle rules cannot diverge between processes.File:
packages/core/src/index.tsWhy this file: Expose the store to API, worker and daemon.
File:
test/agentRunStore.test.ts(new file)Why this file: Exercises idempotency, guarded transitions and previous-report selection against the real migration.
propr-dev commentedon Oct 6, 2026
🤖 AI processing has started for this issue using claude agent with claude-opus-5-5 model.
I'll analyze the problem and work on a solution. This may take a few minutes.
Processing Details:
claudeclaude-opus-5-52802/claude-opus-5-5-agents-v1-3-17-agent-run--20261006-1344-3if2800-epic-agents-v1-y7tissue-2802-20261006-1344-claude-opus-5-5-3if🔍 Track Task Execution