Skip to content

feat(runtime): auto-record decision trajectories + emit ai-hist contract (Workstream B) - #207

Merged
khaliqgant merged 3 commits into
mainfrom
feat/runtime-trajectory-recording
Jun 6, 2026
Merged

khaliqgant merged 3 commits into
mainfrom
feat/runtime-trajectory-recording

Conversation

@khaliqgant

Copy link
Copy Markdown
Member

Workstream B — runtime trajectory recording (the WHY)

Wires the previously-unwired agent-trajectories SDK into the runtime so every persona run records its own decision trajectory and emits a compacted, contract-shaped artifact for the ai-hist trajectory sync source (codex-1, Workstream A).

Stacked on #206 (Workstream C). Base is feat/persona-aihist-mcp-trajectory-enforce so the diff here is only the runtime work. Retarget to main once #206 merges.

What changed (packages/runtime only)

  • types.ts — TrajectoryContext (chapter/note/decide/error/done) on WorkforceCtx, plus the locked CompactedTrajectoryContract shape.
  • trajectory.ts (new) — per-persona TrajectoryRecorder over TrajectoryClient. Opens a trajectory per run, auto-finalizes on completion, writes the contract to $TRAJECTORY_ROOT/<personaId>/compacted/<id>.json: decisions flattened to {question,chosen,reasoning,alternatives[]}, retrospective projected to its subset, personaId/projectId stamped. autoCompact {mechanical,markdown} enabled (best-effort).
  • ctx.ts — instantiate the recorder per persona, expose ctx.trajectory, reserve it against integration clobbering, honor opts.trajectoryRoot.
  • runner.ts — drive begin/complete/fail around the dispatch loop. Recording is best-effort and never breaks the run it observes.
  • cloud-defaults.ts — inject the ai-hist MCP (resolveAiHistFromEnv) into the harness spec, gated on recordTrajectories. The lead's handoff piece.

The two guarantees

  1. write-root === MCP-root. Both the recorder and the MCP injection key off the same TRAJECTORY_ROOT with no implicit default → the MCP reads back exactly what the runtime wrote (or, with no root, the recorder writes nothing and there is nothing to mis-read).
  2. No silent writes. Recording activates only when recordTrajectories !== false and a trajectory root resolves — it never writes to the process cwd.

Contract (frozen A↔B interface with ai-hist)

{ id, version, personaId, projectId, task:{title,description}, status, startedAt, completedAt, decisions:[{question,chosen,reasoning,alternatives[]}], retrospective:{summary,approach,learnings[],confidence} } — no type:"compacted"/sourceTrajectories[], so ai-hist's defensive filter keeps it. Aggregate trail compact output is deliberately NOT the interface.

Tests / verification

  • 6 new tests: records a run, contract shape, auto-finalize, abandon-on-error, done idempotency, disabled/no-root no-ops.
  • Full runtime suite 80/80 green; runtime + deploy + mcp-workforce + workload-router + cli all build.

🤖 Generated with Claude Code

Wire the (previously unwired) agent-trajectories SDK into the runtime so
every persona run records its own decision trajectory (the WHY) and emits a
compacted, contract-shaped artifact for the ai-hist trajectory sync source.

- types: add TrajectoryContext (chapter/note/decide/error/done) on
  WorkforceCtx, plus the locked CompactedTrajectoryContract shape.
- trajectory.ts: per-persona TrajectoryRecorder over TrajectoryClient. Opens a
  trajectory per run, auto-finalizes on done, and writes the contract to
  $TRAJECTORY_ROOT/<personaId>/compacted/<id>.json (decisions flattened to
  {question,chosen,reasoning,alternatives[]}, retrospective subset, personaId/
  projectId stamped). Recording activates only when recordTrajectories !== false
  AND a trajectory root resolves (TRAJECTORY_ROOT or explicit) — never writes to
  the process cwd. autoCompact{mechanical,markdown} enabled (best-effort).
- ctx: instantiate the recorder per persona, expose ctx.trajectory, reserve the
  field against integration clobbering; honor opts.trajectoryRoot.
- runner: drive begin/complete/fail around the dispatch loop; recording is
  best-effort and never breaks the run it observes.
- cloud-defaults: inject the ai-hist MCP (resolveAiHistFromEnv) into the harness
  spec, gated on recordTrajectories. Keys off the SAME TRAJECTORY_ROOT the
  recorder writes to, so the MCP reads back exactly what the runtime wrote.

A↔B contract is the frozen interface with ai-hist (codex-1); aggregate
`trail compact` output is deliberately NOT the interface.

Tests: 6 new (records a run, contract shape, auto-finalize, abandon-on-error,
done idempotency, disabled/no-root no-ops). Full runtime suite 80/80 green;
runtime+deploy+mcp-workforce+workload-router+cli all build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codeant-ai

codeant-ai Bot commented Jun 6, 2026

Copy link
Copy Markdown

Your free trial PR review limit of 300 PRs has been reached. Please upgrade your plan to continue using CodeAnt AI.

@coderabbitai

coderabbitai Bot commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@agent-relay-code[bot], we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 8 minutes and 9 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 072cb4ef-13d4-434c-84a3-4ceff0f204d9

📥 Commits

Reviewing files that changed from the base of the PR and between 575ae0e and ae1992d.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • examples/notion-essay-pr/notion-essay-pr.smoke.test.ts
  • packages/runtime/package.json
  • packages/runtime/src/cloud-defaults.test.ts
  • packages/runtime/src/cloud-defaults.ts
  • packages/runtime/src/ctx.ts
  • packages/runtime/src/runner.ts
  • packages/runtime/src/trajectory.test.ts
  • packages/runtime/src/trajectory.ts
  • packages/runtime/src/types.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/runtime-trajectory-recording

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces trajectory recording capabilities to the runtime, integrating the agent-trajectories dependency. It adds configuration resolution for the ai-hist MCP server, integrates a trajectory recorder into the context builder (buildCtx), manages the recorder's lifecycle during event dispatching, and defines corresponding TypeScript interfaces and tests. The review feedback highlights two key improvements: returning undefined from resolveAiHistFromEnv when both trajectoryRoot and dbPath are empty to prevent starting an unnecessary MCP server, and using the (string & {}) idiom in the CompactedTrajectoryContract status type to preserve IDE autocomplete.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +623 to +626
return {
...(trajectoryRoot ? { trajectoryRoot } : {}),
...(dbPath ? { dbPath } : {})
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If both trajectoryRoot and dbPath are empty or undefined, this function currently returns an empty object {}. This causes aiHist to be truthy, which injects an empty aiHist configuration into the harness spec and starts the "ai-hist" MCP server unnecessarily even when trajectory recording is completely inactive. Returning undefined when both are empty avoids starting the unnecessary MCP server.

  if (!trajectoryRoot && !dbPath) return undefined;
  return {
    ...(trajectoryRoot ? { trajectoryRoot } : {}),
    ...(dbPath ? { dbPath } : {})
  };

personaId: string;
projectId: string | null;
task: { title: string; description: string | null };
status: 'active' | 'completed' | 'abandoned' | string;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In TypeScript, unioning literal types with string causes the type to collapse into string, which loses IDE autocomplete/intellisense for the literal values. To preserve autocomplete for 'active' | 'completed' | 'abandoned' while still allowing any arbitrary string, use the (string & {}) idiom.

Suggested change
status: 'active' | 'completed' | 'abandoned' | string;
status: 'active' | 'completed' | 'abandoned' | (string & {});

Base automatically changed from feat/persona-aihist-mcp-trajectory-enforce to main June 6, 2026 12:33
… write+MCP)

cloud-defaults computes the trajectory root ONCE and feeds the same value to
both the recorder write-root (threaded into buildCtx by the runner) and the
ai-hist MCP env, so write-root === MCP-root by construction.

- resolveCloudTrajectoryRoot: env.TRAJECTORY_ROOT wins; otherwise defaults to
  <workspaceRoot>/.trajectories, but ONLY in a real cloud workspace (configured
  mount or accessible /workspace). Local/tests stay opt-in via env so the
  runtime never writes to a developer's cwd (loud-on-misconfig preserved).
- CloudRuntimeDefaults.trajectoryRoot exposed; runner threads it into buildCtx.
- resolveAiHistFromEnv takes the resolved root as its default so the injected
  ai-hist MCP reads exactly what the recorder writes.

Cloud personas now record by default (satisfies "saved automatically") without
breaking the write-root===MCP-root guarantee.

Tests: +2 (explicit env wins; cloud-workspace default). Runtime suite 82/82.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codeant-ai

codeant-ai Bot commented Jun 6, 2026

Copy link
Copy Markdown

Your free trial PR review limit of 300 PRs has been reached. Please upgrade your plan to continue using CodeAnt AI.

@agent-relay-code

Copy link
Copy Markdown
Contributor

Fixed one PR breakage outside the changed-file set: the examples/notion-essay-pr smoke-test WorkforceCtx mock now includes the new no-op trajectory surface required by the PR.

Local verification passed:

  • corepack pnpm --filter @agentworkforce/runtime test
  • corepack pnpm exec tsc -p examples/tsconfig.json --noEmit
  • corepack pnpm -r typecheck
  • Throwaway compiled smoke test: notion-essay-pr.smoke.test.js

Note: the root pnpm script cannot run directly here because the global pnpm shim is unavailable, so I used corepack pnpm equivalents.

@codeant-ai

codeant-ai Bot commented Jun 6, 2026

Copy link
Copy Markdown

Your free trial PR review limit of 300 PRs has been reached. Please upgrade your plan to continue using CodeAnt AI.

@agent-relay-code

Copy link
Copy Markdown
Contributor

✅ pr-reviewer applied fixes — committed and pushed ae1992d to this PR. The notes below describe what changed.

Fixed one PR breakage outside the changed-file set: the examples/notion-essay-pr smoke-test WorkforceCtx mock now includes the new no-op trajectory surface required by the PR.

Local verification passed:

  • corepack pnpm --filter @agentworkforce/runtime test
  • corepack pnpm exec tsc -p examples/tsconfig.json --noEmit
  • corepack pnpm -r typecheck
  • Throwaway compiled smoke test: notion-essay-pr.smoke.test.js

Note: the root pnpm script cannot run directly here because the global pnpm shim is unavailable, so I used corepack pnpm equivalents.

@khaliqgant
khaliqgant merged commit 621a679 into main Jun 6, 2026
3 checks passed
@khaliqgant
khaliqgant deleted the feat/runtime-trajectory-recording branch June 6, 2026 12:41
@agent-relay-code

Copy link
Copy Markdown
Contributor

Updated the deploy e2e Notion essay smoke mock to satisfy the current WorkforceCtx contract: added no-op credentials and trajectory members.

Validated locally with:

  • corepack pnpm --filter @agentworkforce/runtime test
  • corepack pnpm --filter @agentworkforce/deploy test
  • corepack pnpm -r typecheck
  • corepack pnpm run typecheck:examples
  • Temporary TypeScript project compile for packages/deploy/test/e2e/notion-essay-pr.smoke.test.ts
  • corepack pnpm -r test

No bot review artifacts were present in .workforce beyond the diff/context files.

@agent-relay-code

Copy link
Copy Markdown
Contributor

✅ pr-reviewer applied fixes — committed and pushed e155361 to this PR. The notes below describe what changed.

Updated the deploy e2e Notion essay smoke mock to satisfy the current WorkforceCtx contract: added no-op credentials and trajectory members.

Validated locally with:

  • corepack pnpm --filter @agentworkforce/runtime test
  • corepack pnpm --filter @agentworkforce/deploy test
  • corepack pnpm -r typecheck
  • corepack pnpm run typecheck:examples
  • Temporary TypeScript project compile for packages/deploy/test/e2e/notion-essay-pr.smoke.test.ts
  • corepack pnpm -r test

No bot review artifacts were present in .workforce beyond the diff/context files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant