Skip to content

Drive Claude through the Agent SDK instead of claude -p stream-json - #250

Closed
snowopsdev wants to merge 9 commits into
TypeSafeAI:mainfrom
snowopsdev:snowopsdev/ref/claude-agent-sdk
Closed

snowopsdev wants to merge 9 commits into
TypeSafeAI:mainfrom
snowopsdev:snowopsdev/ref/claude-agent-sdk

Conversation

@snowopsdev

@snowopsdev snowopsdev commented Oct 15, 2026 •

Copy link
Copy Markdown

Replace the Claude backend's hand-written stdio control protocol with the official @anthropic-ai/[email protected], using the user's installed claude executable and existing CLI login. Model/mode selection, resume, CLAUDE.md, hooks, skills, MCP tools and background work keep the same Modex surfaces. Packaged builds exclude the SDK's bundled native CLI packages.

Stop allows a graceful SDK interrupt before escalating to SIGTERM/SIGKILL, drains late messages and waits for process close. CLI failures preserve stderr and exit codes. Permission callbacks wait for tool rows, withdraw cancelled cards and approval-rule decisions, and suppress stale responses through the SDK. The frozen lockfile matches the exact dependency pin.

Upstream ad90f09 is integrated, preserving routine approval denials, the shared Jev decision service, and SDK cancellation, and existing CLI fixtures answer SDK initialization and process user messages separately from controls. Codex's upstream follow-up identified a deferred-launch cancellation race. The process factory now forwards the SDK's cancellation signal; a real-SDK regression verifies that a launch attempted after Stop exits with AbortError.

Local verification on macOS Apple Silicon with Node 24.14.0 and pnpm 10.34.5 passes after the main integration: frozen install, workspace build/typechecks, and 1,656 tests (19 skips). Six targeted Electron cases pass: SDK approvals/withdrawal/Stop/exit diagnostics, all three routines acceptance cases, and both notes-tree cases. Independent review of the approval merge found no actionable issues. The full browser/Electron matrix and native iPhone suite were not rerun.

Installed-Claude smoke reached Claude Code 2.1.301 but the account's weekly usage limit prevented live model acceptance; the real SDK/CLI protocol and UI checks use local scripted executables. All commits in this PR are verified by GitHub. The follow-up commits use the registered GPG key 05896A04CA8D92EA. Detailed fixes, validation and rerun commands.

Continues the reviewed work in snowopsdev#1.

In-app screenshots

Captured from the running Electron app using the real Agent SDK and a local scripted CLI. The app processes real permission requests, cancellation, interruption and exit errors; these captures do not use a live model.

Awaiting tool approval

Awaiting tool approval

Approved command and file change

Approved command and file change

Withdrawn approval while the turn continues

Withdrawn approval while the turn continues

Gracefully stopped turn

Gracefully stopped turn

CLI stderr and recovery actions

CLI stderr and recovery actions

The screenshot-only documentation commit follows tested implementation 1199621. Capture provenance is recorded beside the images. This PR replaces #249 and uses snowopsdev/ref/claude-agent-sdk.

Signing repair preserved every file, commit message, author and merge parent. Reviewed commit e1f893e maps to 13c6530, and tested implementation 1199621 maps to e105d4f; their trees are identical.

cursoragent and others added 4 commits October 15, 2026 21:02
Point query() at the user's installed claude executable so the existing
CLI login, CLAUDE.md, hooks, MCP servers and skills still apply. Map
canUseTool onto Modex approval cards, keep Stop/interrupt and resume,
and omit the SDK's bundled native CLI from packaged builds.

Tests inject a scripted query() so FakeProcess still covers approvals,
denial, Stop, and concurrent subagents without speaking the real SDK
handshake.

Co-authored-by: AJ <[email protected]>
Wait for Claude to close after Stop, allow graceful interruption, and
preserve CLI exit diagnostics through SDK errors. Withdraw cancelled
approvals throughout the runner and approval rules, and order permission
cards after tool rows.

Exercise the real SDK with offline CLI fixtures, add opt-in installed-CLI
smoke tests, align the pinned lockfile, and record verification evidence.

Refs #1
Co-Authored-By: Codex <[email protected]>
Retain upstream's release and security corrections and preserve both
independent in-flight status entries.

Co-Authored-By: Codex <[email protected]>
Exercise approval, withdrawal, graceful Stop and CLI exit diagnostics
through the real SDK, runner, IPC and Electron UI with a local CLI.
Align the existing Claude fixtures with SDK initialization and validate
the candidate against current upstream.

Co-Authored-By: Codex <[email protected]>
@vercel

vercel Bot commented Oct 15, 2026

Copy link
Copy Markdown

@snowopsdev is attempting to deploy a commit to the 0xBuns Team on Vercel.

A member of the Team first needs to authorize it.

Record real Electron captures of approval, withdrawal, Stop and CLI
failure through the shipped SDK and local executable fixture. Include
capture provenance and link the images from the upstream review.

Co-Authored-By: Codex <[email protected]>
@snowopsdev snowopsdev changed the title ref(claude): Drive turns through the Agent SDK Drive Claude through the Agent SDK instead of claude -p stream-json Oct 15, 2026
@BunsDev BunsDev self-assigned this Oct 15, 2026
@snowopsdev
snowopsdev marked this pull request as ready for review October 15, 2026 22:27
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T22:37:12.264126Z 4dbf102 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@snowopsdev
snowopsdev force-pushed the snowopsdev/ref/claude-agent-sdk branch from 4dbf102 to 6c847ec Compare October 15, 2026 22:33

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4dbf102944

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Pass the SDK spawn signal through to the CLI launcher so a deferred
launch cannot outlive a stopped turn. Cover cancellation after the
Stop grace with the real SDK and a real child process.

Refs TypeSafeAI#250
Co-Authored-By: Codex <[email protected]>
snowopsdev and others added 3 commits October 15, 2026 19:48
Preserve routine approval denials and the shared decision service while
retaining SDK request cancellation. Adapt the integration tests to the
merged APIs and keep release status accurate.

Refs TypeSafeAI#250
Co-Authored-By: Codex <[email protected]>
Bring the PR current with the notes-tree performance change that landed
while the previous main integration was being verified. Preserve the
resolved SDK approval and routine behavior.

Refs TypeSafeAI#250
Co-Authored-By: Codex <[email protected]>
@snowopsdev

snowopsdev commented Oct 16, 2026 •

Copy link
Copy Markdown
Author

One relevant update: Anthropic’s October 7 help article says the Agent SDK, claude -p, and third-party apps can still use subscription limits. The SDK overview still requires prior approval for third-party products offering claude.ai login or rate limits. These statements warrant clarification from Anthropic before treating either as definitive for Modex.

BunsDev added a commit that referenced this pull request Oct 16, 2026
docs/status.md gets a Blocked section above Next, holding what only Val can move: the
first published-to-published update, the #250 decision with its local test results and
the Agent SDK policy question, the 1Password CLI integration, and the worktree and branch
cleanup that needs the Codex coordinator or Val's approval. It notes that the rest of the
post-v0.1.0 queue has landed. The text follows the draft from the other session on this
goal (modex-5d), updated after the queue drained.

Known rough edges: the iPhone /resume entry gets the first two failures seen with the new
fixture logging, which show the fixture paged out on a memory-starved runner.

Coven-Harness: Claude Code / claude-opus-5-5
@BunsDev

BunsDev commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Thank you for this. It's careful work, and the lifecycle handling is better than what main has today. We're closing it because of a policy question, not because of the code.

Why it's closed: Modex will keep driving Claude through claude -p stream-json. The Agent SDK docs (overview, legal and compliance) say that third-party products built on the Agent SDK may not offer claude.ai login or rate limits unless Anthropic has approved them. The SDK labels every CLI process it launches as an SDK client (CLAUDE_CODE_ENTRYPOINT=sdk-ts, plus CLAUDE_AGENT_SDK_CLIENT_APP=modex here), so with this PR Modex users' subscription turns would be reported as exactly that case. We would also be changing the CLI-only rule in AGENTS.md. We'd rather not do either without Anthropic's approval.

What we verified locally, on head e38c274:

  • pnpm install --frozen-lockfile, build and unit tests passed: 1,436 desktop passed and 19 skipped.
  • pnpm test:e2e passed: 312 desktop tests passed and 3 skipped, plus 7 site tests. Your claude-sdk.spec.ts passed.

What could still land: the graceful interrupt before SIGTERM, draining late messages, withdrawing cancelled approval cards, and keeping CLI stderr and exit codes all apply to the stream-json backend as well. A PR that brings those to backends/claude.ts, without the SDK, would be very welcome.

Coven-Harness: Claude Code / claude-opus-5-5

@BunsDev BunsDev closed this Oct 18, 2026
BunsDev added a commit that referenced this pull request Oct 18, 2026
The release Mac's v0.0.12 updated itself to v0.1.0 through its banner at 2026-10-18 01:53:56 UTC.
app/update-recovery.json records the attempt with outcome installed, and its sha512 matches the
published ZIP. Modex relaunched by itself, About shows 0.1.0, and the unsent drafts survived. The
review and status now cite this evidence in place of the earlier report from another Mac.

Everything Blocked held is resolved: with Val's approval, the last 20 archived worktrees were
pruned, so the section goes. Next gains #250's lifecycle fixes for the claude -p backend.

Coven-Harness: Claude Code / claude-opus-5-5
BunsDev added a commit that referenced this pull request Oct 18, 2026
* Record the first published-to-published update

On 2026-10-17 Val upgraded a v0.0.12 install to v0.1.0 through its own update banner, on a Mac
other than the release Mac. Modex relaunched by itself, About showed 0.1.0, and an unsent draft
survived. The v0.1.0 review records this in place of the pending check, and the v0.1.0 section
of status notes it.

Next drops the update item and keeps its Windows and Linux note as an item of its own. Blocked
now holds only the worktrees the pruning tool cannot prove merged. The update, #250 (closed:
Modex keeps claude -p), 1Password and the remote-branch cleanup are resolved.

Coven-Harness: Claude Code / claude-opus-5-5

* Record the update on the release Mac and clear Blocked

The release Mac's v0.0.12 updated itself to v0.1.0 through its banner at 2026-10-18 01:53:56 UTC.
app/update-recovery.json records the attempt with outcome installed, and its sha512 matches the
published ZIP. Modex relaunched by itself, About shows 0.1.0, and the unsent drafts survived. The
review and status now cite this evidence in place of the earlier report from another Mac.

Everything Blocked held is resolved: with Val's approval, the last 20 archived worktrees were
pruned, so the section goes. Next gains #250's lifecycle fixes for the claude -p backend.

Coven-Harness: Claude Code / claude-opus-5-5
BunsDev added a commit that referenced this pull request Oct 18, 2026
Only docs/status.md conflicted. CF-2 stays in In flight, now naming
#286 and the stacked repair #291. The duplicate Next item for #250's
lifecycle fixes is dropped, and the remaining items are renumbered.
No code changes.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5
BunsDev added a commit that referenced this pull request Oct 18, 2026
…t, withdrawn prompts (CF-2) (#286)

* Reproduce Claude Stop, withdrawal and drain faults on claude -p

Add failing regressions for #275 against the stream-json backend, with
protocol shapes captured live from Claude Code 2.1.302:

- Stop sends SIGTERM at once instead of the native interrupt, and drops
  every message after it, including the turn's final reply snapshot.
- A process that never confirms its exit holds Stop forever.
- control_cancel_request is ignored, so a cancelled approval card stays
  actionable, holds a drained turn open, and a late answer is written.
- An answer after the turn settled writes to the ended stdin and raises
  an uncaught ERR_STREAM_WRITE_AFTER_END.
- keep_alive re-arms the idle window; the final unterminated line,
  split-UTF-8 stderr, and stderr or exit codes after a failed result are
  lost; a killed process reports "exited with code null".
- Failure details copy MCP route tokens from argv.

The fake process now models exit versus close, signals it ignores,
held stdio and raw chunks, and answers interrupts the way the live CLI
did. The interfaces the tests use are declared without behaviour:
TurnSink.approval's withdrawal signal, TurnResult.stop, the approval
item's withdrawn reason, and the timing and liveness knobs.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Stop Claude with its native interrupt and drain its last output

Stop now writes Claude Code's own interrupt control request and ends
stdin, as the 2.1.302 CLI needs to stop a running tool. Every message
the CLI still prints is read: the final reply snapshot, the rejected tool
result and the closing result, which after Stop is no failure. The turn
ends when the process exits. One that has not exited 1.5 s after Stop
gets SIGTERM, then SIGKILL 1.5 s later. If no exit follows within 2 s,
the stop is reported unconfirmed instead of waiting forever. The result
says which of these happened (exited, terminated, killed or
unconfirmed), with exit code, signal and pid.

control_cancel_request withdraws the matching can_use_tool. The sink is
told through an abort signal, the request stops holding the turn open,
and no answer is sent: the CLI ignores one for a request it cancelled.

Writes go only to an open stdin of an unfinished turn, and EPIPE is
absorbed. This removes the uncaught ERR_STREAM_WRITE_AFTER_END raised
by a late approval.

The turn now settles on exit plus a bounded drain, not only on close,
so a descendant holding the pipes cannot keep it open:
- stderr is decoded across chunks, and the last stdout line is read
  even without its newline.
- A failed result waits up to 1 s for the stderr and exit code that
  follow it.
- A signal death says so.
- keep_alive no longer re-arms the idle window.

Failure details redact MCP route tokens from argv.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Withdraw approval cards Claude cancels and say how Stop ended

The runner closes a card when its backend withdraws the request. It is
patched once to answer "no" with a withdrawn reason (native, stopped or
ended), so older windows and the phone show it closed. Its promise
settles "no", the thread stops waiting, and a late answer finds nothing
pending. A request withdrawn while the approval gate is still deciding
shows no card at all, and Jev is cancelled with it. Stop and the end of a
turn label their leftover cards the same way instead of "Denied".

A stopped turn's notice now says how it ended: "Stopped.", or that
Modex had to terminate or kill the CLI, or, as a warning, that the
process never confirmed its exit. Until such a process is gone, deleting
or retiring the thread is refused with its pid. The worktree, thread and
attachments it may still use stay.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Keep a failure that precedes Stop, and redact a failed launch's argv

Independent review of the lifecycle change found three low-severity
faults, each now covered by a regression:

- Stop pressed while a failed result was draining reported a clean stop
  ("exited" with no exit seen), dropped the failure card and cancelled
  the SIGTERM/SIGKILL escalation. The failure now stands, and Stop's
  escalation outlives the turn's settlement.
- A success result inside the failure's drain window completed the
  turn. Once a turn has failed, later results are ignored.
- A launch that throws synchronously still put the raw argv, MCP route
  tokens included, into the failure details.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Answer Claude's native interrupt in the title-naming fixture

The scripted Claude in titles.spec only recorded a cancelled naming run
on SIGTERM. Stop now interrupts Claude natively and ends its input, and
the fixture exited cleanly on that before any SIGTERM was due, so "a
manual rename cancels pending naming" waited for a marker that never
came. The run was cancelled all the same.

The fixture now acknowledges the interrupt and closes the turn as
Claude Code 2.1.302 does, and records the cancellation there, as its
Codex branch already does for turn/interrupt.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Record the Claude lifecycle evidence and move it to In flight

The receipt holds:
- the Claude Code versions and the live 2.1.302 captures, with the
  probe script;
- each fault on main with its change, and the timing budgets;
- the red-then-green record and the full check results at 62ac15c
  and d11cdb6;
- the independent review;
- the acceptance-criteria map;
- what is still unproven: a live-model Stop through the app, Windows
  and Linux escalation, and the Companion's label for withdrawn cards.

Status moves #250's lifecycle item from Next to In flight.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Record a tool call Stop interrupted as interrupted

After the interrupt, Claude Code closes each tool call it stopped with
an error tool_result ("The user doesn't want to proceed with this tool
use."). CF-1 mapped every error result Modex had not refused to failed,
so a stopped Bash call read as a failure. An error result that arrives
after Stop is now interrupted. The Claude mapping in
docs/conversation-events.md says so.

The scripted CLI now sends that rejection for the calls it was stopped
in, as the live 2.1.302 capture does.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Record the final checks after merging CF-1

The receipt names 172b13f as the tested code and records its full
checks: frozen install, build, typecheck, unit tests, and desktop and
site E2E. It also covers how CF-2 sits on CF-1's contract and the
post-Stop tool outcome. Status notes that a stopped tool call reads
Stopped.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Add opt-in live Claude Stop acceptance and record its first pass

e2e/claude-live-stop.spec.ts drives the built app with the real
`claude -p` and a real model. It runs only with MODEX_CLAUDE_LIVE=1,
because each run spends about five turns of the signed-in account's
usage. It covers four Stops:
- with a permission card waiting;
- while an approved 30 s command runs;
- with the same thread resumed afterwards;
- while a reply streams.

Against 52b159a, with Claude Code 2.1.302 and Haiku, all four passed.
Each ended without a failure card:
- Card: idle in 0.86 s.
- Running command: SIGTERM after the 1.5 s grace, idle in 2.33 s, the
  row interrupted, and its file never written.
- Resume: replied "resumed".
- Stream: idle in 0.86 s, with 670 characters kept.

The receipt adds these results and the full checks at 52b159a,
including two E2E setup failures from a full disk and their passing
rerun.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Keep failed Claude turns owned until Stop confirms exit

Preserve the original failure while waiting for bounded Stop confirmation, and retain unconfirmed process ownership regardless of turn outcome. Cover the backend result and refusal to delete its live worktree.

Coven-Harness: Codex / gpt-6

* Require a real stop on every live step, and record a6ea0b0's checks

Haiku can finish a 400-word story before Stop lands. The live run then
honestly records a completed turn, but that is no test of a mid-stream
Stop. The streaming step now asks for 2,000 words, and every step must
end with a "Stopped." notice. Against a6ea0b0 all four steps passed:
- card: 0.93 s;
- running command: SIGTERM, 2.36 s, interrupted;
- resume: "resumed";
- stream: 1.37 s, 834 characters kept.

The receipt records the full checks at a6ea0b0, which is up to date
with main at fb76aac:
- frozen install, build, typecheck and unit tests;
- desktop E2E 322 passed and 4 skipped, with one sidebar popover
  failure that passes on rerun and in CI;
- site 7/7;
- CI green on all 12 check runs.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Record the final checks and live run at 7c9d62c

The tested code is 7c9d62c, up to date with main at d3711c6 (CF-5,
first part). At that SHA:
- CI: all 12 check runs green.
- Local: frozen install, build, typecheck and unit tests pass.
- Local desktop E2E: 322 passed and 4 skipped, with 5 timeouts under
  load in specs that never start Claude. All 26 cases in those specs
  pass when rerun alone.
- Site: 7/7.
- Live acceptance: Claude Code 2.1.302 with Haiku passed every
  step, each with a Stopped. notice.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Record the checks and live run at 13faaeb

13faaeb is up to date with main at f6976df (CF-7, first part).
- Targeted: 228/228 unit tests and 28/28 Electron cases pass.
- Live: Claude Code 2.1.302 with Haiku again stops at every step
  with a Stopped. notice.
- Full check: PR CI runs it at the head. The last full local run
  stays recorded at 7c9d62c.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Record the checks at 61cc65d after merging #291

61cc65d is up to date with main at 04b7ec7, with #291 merged.
- Local: install, build, typecheck, unit tests and E2E all pass with
  no reruns (desktop 329 passed, 4 opt-in skips; site 7/7).
- Live: Claude Code 2.1.302 with Haiku passes every Stop step.
- CI attempt 1: 10 checks passed, but unit + e2e failed 3 cases in
  CF-5's conversation-evidence spec, a folding race in its setup
  reported on #278. The re-run passed, so CI is green at 61cc65d.

Status now names #291 as merged into #286.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5

* Record CF-8's Stop acceptance against CF-2

After merging main at d3dfe76, CF-8's conversation-fidelity spec passes
against CF-2's interrupt-then-grace Stop, alongside the claude-lifecycle
and titles specs: 13/13. Code is unchanged from 61cc65d apart from
CF-8's spec from main.

Refs #275

Coven-Harness: Claude Code / claude-opus-5-5
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.

3 participants