Repository navigation
D: pass the plan schema to Claude; refuse Codex in planning and questions (#75) #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,59 @@ | ||
| import { join } from 'node:path'; | ||
| import type { InvocationHandle } from '../contract.ts'; | ||
| import { createClaudeCommand, createPhasePolicy } from '../policy.ts'; | ||
| import { readCapturedFile } from '../container/profile.ts'; | ||
| import { createClaudeCommand, createPhasePolicy, MAX_COMMAND_SCHEMA_BYTES } from '../policy.ts'; | ||
| import { launchInvocation } from './supervisor.ts'; | ||
| import { setUpProfile } from './setup.ts'; | ||
| import { assertAdapterRequest, createAdapterInvocationBudget, type AgentAdapterOptions, type AgentAdapterRequest } from './types.ts'; | ||
|
|
||
| export function parseClaudeOutput(raw: Buffer): { text: string; providerFailed: boolean } { | ||
| /** | ||
| * Read Claude's `--output-format json` envelope. A result whose `subtype` is not `success`, or whose `is_error` is | ||
| * true, is a provider failure; it can omit `result`. With `--json-schema` (planning), a successful answer needs | ||
| * `subtype: "success"` and is the schema-validated `structured_output` object, returned as JSON text; `result` then | ||
| * holds only Claude's prose. Plain-text envelopes without a `subtype` are still accepted. | ||
| */ | ||
| export function parseClaudeOutput(raw: Buffer, structured = false): { text: string; providerFailed: boolean } { | ||
| const envelope = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(raw)) as | ||
| { result?: unknown; is_error?: unknown }; | ||
| if (typeof envelope.result !== 'string' || typeof envelope.is_error !== 'boolean') | ||
| { subtype?: unknown; result?: unknown; is_error?: unknown; structured_output?: unknown }; | ||
| if (typeof envelope.is_error !== 'boolean' || (envelope.result !== undefined && typeof envelope.result !== 'string') | ||
| || (envelope.subtype !== undefined && typeof envelope.subtype !== 'string')) | ||
| throw new Error('Claude returned a malformed output envelope.'); | ||
| return Object.freeze({ text: envelope.result, providerFailed: envelope.is_error }); | ||
| const prose = envelope.result ?? ''; | ||
| if (envelope.is_error || (envelope.subtype !== undefined && envelope.subtype !== 'success')) | ||
| return Object.freeze({ text: prose, providerFailed: true }); | ||
| if (!structured) { | ||
| if (envelope.result === undefined) throw new Error('Claude returned a malformed output envelope.'); | ||
| return Object.freeze({ text: prose, providerFailed: false }); | ||
| } | ||
| // Claude always reports a subtype; a schema-constrained answer is accepted only from an explicit success. | ||
| if (envelope.subtype !== 'success') throw new Error('Claude returned a malformed output envelope.'); | ||
| const answer = envelope.structured_output; | ||
| if (answer === null || typeof answer !== 'object' || Array.isArray(answer)) | ||
| throw new Error('Claude returned no structured output for a schema-constrained answer.'); | ||
| return Object.freeze({ text: JSON.stringify(answer), providerFailed: false }); | ||
| } | ||
|
|
||
| /** | ||
| * Read the planning schema for `--json-schema` with the profile's bounded no-follow reader. The profile captures the | ||
| * mounted file again and refuses a command whose schema differs. | ||
| */ | ||
| export function readPlanningSchema(inputDirectory: string): string { | ||
| const { content } = readCapturedFile(join(inputDirectory, 'schema.json'), 'Planning schema', MAX_COMMAND_SCHEMA_BYTES); | ||
| return new TextDecoder('utf-8', { fatal: true }).decode(content); | ||
| } | ||
|
|
||
| export function startClaudeInvocation(request: AgentAdapterRequest, | ||
| oauthToken: string, options: AgentAdapterOptions = {}): InvocationHandle { | ||
| if (!oauthToken || oauthToken.includes('\0')) throw new Error('Claude OAuth token is malformed.'); | ||
| const policy = createPhasePolicy(request.invocation); | ||
| const remaining = createAdapterInvocationBudget(request.invocation, options.timeoutMs); | ||
| const structured = policy.phase === 'planning'; | ||
| // Invalid input throws here, before anything is allocated; only Docker setup runs inside the handle. | ||
| const command = createClaudeCommand(policy, request.prompt, | ||
| structured ? readPlanningSchema(request.inputDirectory) : undefined); | ||
| assertAdapterRequest(request); | ||
| return launchInvocation(request.invocation, remaining, (signal, start) => setUpProfile(request, remaining, signal, | ||
| network => ({ ...request, policy, network, command: createClaudeCommand(policy, request.prompt), | ||
| claudeToken: oauthToken }), | ||
| network => ({ ...request, policy, network, command, claudeToken: oauthToken }), | ||
| profile => start(profile, { ...options, secrets: { CLAUDE_CODE_OAUTH_TOKEN: oauthToken }, | ||
| invocationBudget: remaining, decode: (_profile, raw) => parseClaudeOutput(raw) }))); | ||
| invocationBudget: remaining, decode: (_profile, raw) => parseClaudeOutput(raw, structured) }))); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.