Skip to content

feat(sdk): add run-ID-addressed controls - #1642

Merged
samuv merged 16 commits into
plan/sdk-run-controlsfrom
impl/sdk-run-controls
Sep 17, 2026
Merged

samuv merged 16 commits into
plan/sdk-run-controlsfrom
impl/sdk-run-controls

Conversation

@samuv

@samuv samuv commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add prompt-free, run-ID-addressed resolve, cancel, steer, and steer-retraction controls over unary gRPC and strict HTTP routes.
  • Add atomic ordinary-ask resolution, strict stale-run semantics, server-owned restart rehydration, and lifecycle/lease safeguards.
  • Export Session.controls(runId) and RunControls from the TypeScript SDK, with typed acknowledgements, feature gating, request options, media support, and malformed-response validation.
  • Add hermetic TCP, UDS, HTTP, and daemon-restart coverage plus generated API reference, example, architecture notes, and user guides.

Development stage

  • Plan / Interface
  • Implementation — stacked on the open Plan / Interface PR under the directing user's explicit stack-before-merge authorization
  • Combined
  • Spike / Routine

Contract linkage

  • Work classification: Architectural
  • Classification rationale: additive public protobuf, HTTP, engine, and TypeScript SDK contracts plus durable run lifecycle behavior
  • Decision record: ADR 0346
  • Human waiver of spine: Yes — the directing user explicitly authorized the implementation to proceed as the next stack layer before the Plan PR merges
  • Acceptance plan: docs/acceptance/sdk-run-controls.md
  • Human decisions resolved and recorded: Yes
  • Plan / Interface PR: plan: run-ID-addressed SDK controls #1635
  • Approved Plan commit baseline: 180d18f9df776e20abe4c2540294dbf09fa6a831
  • Panel-reviewed implementation baseline: 172b55fb52927f5fed286592d98696c03f19930c

Interface conformance

Matches the approved contract. No material deviations.

Issue relationship

Closes #1630

Type of change

  • Behavioral/interface plan
  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation/process

Test plan

  • Acceptance-plan checker
  • Linting (task lint on the implementation candidate; focused Go/SDK lint after repairs)
  • Offline test suite (task test) — deferred to PR CI at the directing user's request; focused changed-package and race suites pass
  • Offline demo (go run ./cmd/mecademo) — deferred to PR CI/workflow follow-up
  • Docs generation and strict link checks (task docs)
  • User-docs site build (task site:build)
  • Guarded engine API compatibility (task api:check)
  • Intentional engine API update (task api:update plus engine/CHANGELOG.md)
  • Strict acceptance trace: this plan resolves 25/25 ACs; unrelated repository baseline failures remain outside this diff
  • Final implementation panel review: zero blockers, zero important findings, three advisory deduplication/reuse notes
  • TypeScript SDK unit suite: 287/287 before focused repair additions
  • Focused SDK run-control unit tests: 18/18
  • Real-wire run-control test over gRPC TCP, gRPC UDS, HTTP, and daemon restart
  • Focused server lifecycle/race tests, including five consecutive -race runs for stale retraction sentinels

Changes

Area Change
Engine Atomic ordinary permission-ask resolution for root and surfaced-child asks
Server Four strict controls, exact stale/error precedence, post-lease reload, detached relay ownership, shutdown/drain linearization
SDK Session.controls(runId), RunControls, typed acknowledgements, feature/request/media handling
Tests Unit, race, real-wire, restart, transport, malformed-response, and isolation coverage
Docs Generated API reports/reference, durable attachment example, SDK guides, architecture and lifecycle inventories

User-facing change

Applications that retain a session ID and run ID can now resolve an ordinary permission ask, cancel, steer with text or media, or retract a pending steer without retaining the original live event stream. Controls remain exact-run and fail stale rather than targeting a successor.

Special notes for reviewers

  • This PR intentionally targets plan/sdk-run-controls; do not retarget it to main while PR plan: run-ID-addressed SDK controls #1635 is open.
  • A lost unary acknowledgement is ambiguous by design and must be reconciled from authoritative session/activity state.
  • Plan approval remains separate from ordinary ask resolution.
  • The SDK changelog is owned by the generated release workflow and is intentionally unchanged here.

@samuv
samuv added this pull request to stack #1643 September 16, 2026 18:26
@samuv samuv changed the title impl/sdk run controls feat(sdk): add run-ID-addressed controls Sep 16, 2026
@samuv
samuv force-pushed the impl/sdk-run-controls branch 2 times, most recently from 36e5900 to d50c0d4 Compare September 16, 2026 18:51
@samuv
samuv force-pushed the impl/sdk-run-controls branch from d50c0d4 to 7274802 Compare September 17, 2026 07:36
@samuv samuv self-assigned this Sep 17, 2026
@kantord

kantord commented Sep 17, 2026

Copy link
Copy Markdown
Member

Panel review — PR #1642 (impl/sdk-run-controls)

Fixed point: origin/plan/sdk-run-controls...origin/impl/sdk-run-controls, scoped to 41 core-logic + hand-written test files (excluded: generated protobuf/SDK codegen, docs, JSON fixtures — 61 files / 9070+/3166− lines total in the full PR)


Spec — does this implement what was asked?

Source: issue #1630, ADR 0346, docs/acceptance/sdk-run-controls.md

No blocker/important findings. Matches spec closely, including the hard parts (proto field numbers, atomic ResolveOrdinaryAsk, the 4×4 stale/error matrix, context-transfer ownership, cancelSteer gates).

  • [advisory] run.Cancel() → cancelRegisteredRun(state) was retrofitted onto several pre-existing legacy call sites beyond the 4 new RPCs (Converse relay, SSE relays, onLeaseLost, Drain, approvePlan). Root-cause necessary, not scope creep — but wider blast radius than "existing routes don't change" implies. Worth a second look at legacy-path equivalence.
  • [advisory] TestADR_0346_StaleControlCannotMutateSuccessor doesn't include resolveAsk in the non-disclosure assertion (cancel/steer/cancelSteer only) — coverage gap, not a functional bug.
  • [advisory] durable-attachment.ts example overwrites runId on every event with no child-run filtering — docs nuance, child addressing is explicitly out of scope.
  • [advisory] mockscript.go's new capabilities field is test-support plumbing only.

Standards — does this follow project conventions?

Sources: AGENTS.md, .claude/rules/*.md, ADR 0027, ADR 0249, ADR 0346

  • [important] promoteDetachedRunAdmission duplicates promoteRunAdmission almost line-for-line, but the lease check diverges: new code calls the extracted validateHeldLeaseLocked(id), old code still inlines the same check. AGENTS.md names this exact "per-surface drift class" (ADR 0249 notes it's fired 3 times already). Retrofit the old function to call the same helper.
  • [important] Confirm no legacy /approve//cancel caller or test still asserts the old two-run-id stale-error wording, now that checkExpectedRun stopped naming the successor.
  • [advisory] mockscript.go's Capabilities{Audio,Image} addition is unrelated to run controls — scope creep worth noting in the PR description.
  • [advisory] resolvePersistedRunAsk's //nolint:gocyclo + deferred stopRequestLease until teardown — harmless.
  • [advisory] Confirm engine/CHANGELOG.md has the Added=minor entry for AskResolution/ResolveOrdinaryAsk.

Test adequacy — do the tests independently prove the contract?

AC3.1 atomicity is genuinely proven (32-goroutine race over the real mutex). But:

  • [important] AC4.5's cited test (TestSDKRunControls_Scenario4_CancelSteerRaceAndLeaseGates) isn't itself concurrent — it's sequential state mutation, one synchronous call per axis. The real concurrent race tests exist in the same file under different names. Traceability defect against the acceptance plan, not a functional gap.
  • [important] Two matrix rows have no Go-level regression test, only TS e2e coverage: (1) resolveAsk→stale_run_control on an ended/cancelled/replacement run, (2) persisted-awaiting-only row's cancel→no_active_run vs steer/cancelSteer→stale_run_control distinction. Real behavior is proven, but only at the slow/e2e layer — a regression here won't fail fast.
  • [advisory] No explicit child+NotPending ask-resolution test case (path is shared/proven via root case).
  • [advisory] AC1.2's test proves route/error parity, not Service-transition identity between HTTP/gRPC directly (covered via e2e instead).
  • [advisory] Run.Approve on an unknown askID isn't independently re-proven (pre-existing, untouched).

Domain — what do the specialist reviewers say?

Panel: secure-code-reviewer, software-architect, code-duplication-reviewer, library-reuse-reviewer

Cross-confirmed (1)

  • [important] Byte-identical locked live-run gate (generation/lease/cancelling/outcome/exact-run check) copy-pasted across all 4 new service.go methods (resolveLiveRunAsk, CancelRun, SteerRun, cancelRunSteer). One security invariant implemented 4 times — a future precondition change risks silently diverging per-operation error precedence. IMPLEMENTATION-NOTES.md already describes this as one rule, implemented in the plural.
    Sources: software-architect, code-duplication-reviewer
    Fix: extract checkLiveRunForControl/validateLiveRunLocked(id, st, expectedRunID, generation) error, called under the already-held locks; each site keeps its own post-check action.

Mechanical fixes (1)

  • [important] waitDetachedControlRelays (new) hand-rolls the same goroutine+channel+select/timeout shape already used for per-session engine close, ~4900 lines earlier in service.go (a 3rd near-identical copy exists for MCP broker attachments too). Extract one waitBounded(wait func(), what string) helper.
    Source: library-reuse-reviewer

Polish (3)

  • [advisory] New tiny control endpoints (resolve-ask/cancel/cancel-steer) inherit the 32MiB /prompt body cap via a shared decode helper — generous but pre-existing, not new. (secure-code-reviewer)
  • [advisory] UnsupportedFeatureError now carries two semantics — transient/negotiable (prompt_free_controls) vs. permanent API-shape (attached_run_controls) — fine today, worth watching if a consumer branches on .feature. (software-architect)
  • [advisory] childAskRouter.resolveOrdinary holds its own lock while calling into a child's lock (asymmetric vs. existing route(), which releases first) — safe today since children never nest their own router, but flag if that ever changes. (software-architect)

Gaps

  • (none — panel of 4 ran clean, no reviewer failures)

Summary

  • Spec axis: 4 findings (0 blocker, 0 important, 4 advisory)
  • Standards axis: 5 findings (0 blocker, 2 important, 3 advisory)
  • Test adequacy axis: 5 findings (0 blocker, 2 important, 3 advisory)
  • Domain axis: 0 blocker, 2 important (1 cross-confirmed), 3 advisory

No ship-blockers. The two most load-bearing findings, both important: the 4x copy-pasted service.go gate (cross-confirmed by 2 independent domain reviewers + independently spotted by Standards from a different angle — 3 total signals on the same root cause) and the promoteRunAdmission/promoteDetachedRunAdmission lease-check drift, which is the exact same "one invariant implemented N times" shape. Recommend fixing both together before merge — they're mechanical, same-day extractions, not design debates.

PANEL: ship_blockers=0 important=6 advisory=13 reviewer_failures=0


Fully or partially written by an AI agent.

@samuv
samuv force-pushed the impl/sdk-run-controls branch from 7274802 to 0834ecb Compare September 17, 2026 10:07
@samuv

samuv commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Addressed all six important panel findings in the latest stack update:

  • centralized the exact-live locked control gate shared by all four unary controls;
  • reused the shared held-lease validation in both admission paths;
  • extracted the repeated bounded relay-wait pattern;
  • audited legacy stale-error wording and removed the remaining drift risk;
  • added fast Go coverage for stale/terminal/replacement resolveAsk and the persisted-awaiting control matrix;
  • updated AC4.5 to trace the real concurrent linearization test.

The feature-related race-shard failure is also fixed by deterministically holding the resumed run at its second model request before asserting the duplicate ask resolution. The unrelated filewatch burst-coalescing failure was not changed because the PR does not touch that package and it passed repeated local race runs.

Focused verification on head 21f949df0:

  • server run-control race tests: -race -count=10;
  • acceptance-plan checker: 25 ACs, all decisions/proofs present;
  • plan-specific strict AC trace: 25 ACs, 0 failures;
  • git diff --check clean.

Fresh CI is now running on the rewritten stack.

Fully or partially written by an AI agent.

stacked auto-merge was automatically disabled September 17, 2026 13:45

Pull Request is not mergeable

@samuv
samuv force-pushed the impl/sdk-run-controls branch from 21f949d to 0773020 Compare September 17, 2026 13:50
samuv and others added 13 commits September 17, 2026 16:15
Relates to #1630

Co-authored-by: Codex <[email protected]>

Signed-off-by: Samuele Verzi <[email protected]>
Co-Authored-By: OpenAI Codex <[email protected]>

Signed-off-by: Samuele Verzi <[email protected]>
Co-Authored-By: OpenAI Codex <[email protected]>
Signed-off-by: Samuele Verzi <[email protected]>
Co-Authored-By: OpenAI Codex <[email protected]>
Signed-off-by: Samuele Verzi <[email protected]>
Co-Authored-By: OpenAI Codex <[email protected]>

Signed-off-by: Samuele Verzi <[email protected]>
samuv and others added 3 commits September 17, 2026 16:15
Signed-off-by: Samuele Verzi <[email protected]>
Co-Authored-By: OpenAI Codex <[email protected]>
Co-Authored-By: OpenAI Codex <[email protected]>
Signed-off-by: Samuele Verzi <[email protected]>
Co-Authored-By: OpenAI Codex <[email protected]>
Signed-off-by: Samuele Verzi <[email protected]>
@samuv
samuv force-pushed the impl/sdk-run-controls branch from 0773020 to b098d7c Compare September 17, 2026 14:17
@samuv
samuv merged commit 0bf37d0 into main Sep 17, 2026
36 checks passed
@samuv
samuv deleted the impl/sdk-run-controls branch September 17, 2026 14:44
This was referenced Sep 21, 2026
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.

SDK: expose run-ID-addressed controls for reattached runs

2 participants