Repository navigation
feat(core): take execute options on a first line of raw code for raw-text models - #65709
rekram1-node wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
I'd merge this, though I couldn't check it end to end with a real GPT model. The changed tests pass (104), including the Tool.snapshot tests: a Lark model's execute takes { code }, and // @exec: {"timeout": 5000} reaches the job runner as { timeout: 5000 }. The compiled requests send GPT a custom Lark tool and Claude a JSON tool. tsc -b passes in packages/core. I didn't check that OpenAI accepts the grammar or that GPT writes the options line in practice; that needs real credentials.
Public API: no SDK, HTTP, config, CLI or event changes. Plugins that read execute's definition will see { code } for Lark models and { code, timeout, background } for others, as the description notes. Hooks on a call see the fields either way.
One point to decide before merging (inline comment): an invalid options line is quietly ignored rather than reported to the model.
| const code = (input as { code?: unknown }).code | ||
| if (typeof code !== "string") return input | ||
| const line = /^[ \t]*\/\/ @exec:([^\r\n]*)\r?\n/.exec(code) | ||
| const options = line ? Option.getOrUndefined(decodeOptions(line[1])) : undefined |
There was a problem hiding this comment.
With JSON fields, an invalid timeout (e.g. -1 or "5s") fails validation and the model is told. With the options line, a line that doesn't decode stays in the code as a comment and the script runs with the default timeout in the foreground, so the model never learns its options were ignored. Maybe return a tool error when a line starts with // @exec: but doesn't decode, so both forms behave the same?
Also, the description says unknown keys stay in the code, but Schema.Struct drops them and the line is still applied (the yield_time_ms test checks exactly that). Worth updating whichever one is wrong.
There was a problem hiding this comment.
Fixed both: a line starting with // @exec: that isn't valid options, or has no program after it, now fails the call with a message saying how to write it, like an invalid JSON field. Unknown keys are still dropped, as they are from JSON input, and the PR description now says so.
There was a problem hiding this comment.
I checked e23f3991b and both fixes work.
- Invalid line:
splitOptionsnow looks for// @exec:with optional leading spaces (packages/core/src/codemode/tool.ts). If that line is there but has no program after it, or its JSON doesn't decode to the options ({timeout},-1,"5s", an array), the call fails with aTool.ErrorfromTool.snapshot(packages/core/src/tool.ts~L392) that says how to write the line. Code without the prefix is passed through unchanged. - Unknown keys: they're dropped and the line is still applied, which is what the test, the doc comment and the PR description now all say.
One difference from JSON input, which you can probably ignore: the options error is raised before beforeExecute, so hooks never see a bad options line. A bad JSON field still reaches the hooks. If you want them to match, run the split after the hook, or pass the hook the original input on failure.
What I ran: I checked out pr/head in a temporary worktree and ran bun test on codemode-execute-options.test.ts, tool-registry.test.ts and tool-execute.test.ts: 104 pass, 0 fail. Typecheck didn't run: bun typecheck failed with "Script not found "check"" in that worktree. I didn't run the full core suite.
Checkout: the local pr-65709 branch is behind pr/head, and it has an uncommitted bun.lock change I didn't make. I didn't change anything in it, and I removed the temporary worktree afterwards.
There was a problem hiding this comment.
The earlier point is addressed: a // @exec: line that isn't valid options, or has no program after it, now fails the call with a message saying how to write it. The description now matches the code on unknown keys. The changed tests pass (104) and tsc -b passes in packages/core. I found nothing new in this commit. Public API is unchanged beyond what the first review noted: plugins see execute's definition as { code } for Lark models.
There was a problem hiding this comment.
The earlier point is still addressed. The rename to // @options: is consistent: the grammar and the parser both build their pattern from OPTIONS_PREFIX. The TUI and app script labels skip the line. The changed tests pass in core (104), tui (3) and app (7), and tsc -b passes in all three. I couldn't show the label change in the TUI because the simulated model doesn't produce options lines. A before/after screenshot of the Scripts tab with a // @options: script would help. Public API is unchanged beyond what the first review noted.
28e5e3f to
01b2102
Compare
There was a problem hiding this comment.
The earlier point stays addressed, and the move to prepare and jobOptions keeps the options line inside execute. One regression in the new naming: without full_codemode (the default), and for jobs saved before this change, every background script in the TUI Scripts tab and the app's background list is now called "Code Mode script" instead of by its first line of code (see inline comments). The changed tests pass in core (105), tui (3) and app (8), and tsc -b passes in all three. I confirmed the TUI regression with a throwaway test of scriptLabel. Public API is unchanged beyond what the first review noted.
| id, | ||
| type: "codemode", | ||
| title: "Code Mode script", | ||
| title: options?.title ?? "Code Mode script", |
There was a problem hiding this comment.
This still falls back to "Code Mode script". In Tool.snapshot, the options (and so the title) are only passed with full_codemode and when the call isn't a top-level call to a Code Mode tool (full && !found ? CodeModeTool.jobOptions(...) : undefined). Every other Code Mode job, and every job saved before this change, gets this fixed title. Since the TUI and app labels now prefer job.title, those scripts all show as "Code Mode script". Could the first-line title always be passed, separately from timeout/background? Or drop the default here, and have the labels treat the old "Code Mode script" string as no title.
There was a problem hiding this comment.
Fixed: the title now rides on the backgrounded execute call's metadata, and the TUI and app labels use that title first, then the call's first line of code, then the job title, as before. Older calls and Code Mode tools called at the top level keep their old labels.
There was a problem hiding this comment.
Yes, this fixes it as of 3e469374e6. I see no regression for the cases I raised:
- Titled scripts:
job.tsnow passes the title in each progress update and returns it in the handoff metadata ({ jobID, title }).scriptLabelinpackages/tui/src/util/session.ts, and both thescriptsandblockingpaths inpackages/app/src/session/requests/background.ts, readmetadata.titlefirst. - Calls without a title (no
full_codemode, a Code Mode tool called at the top level, or calls saved before this change) fall back to the first line ofinput.code, as before. The job's own title,"Code Mode script"when none was given, is only used when no matching execute call is found, as before. So the fixed default no longer covers up labels taken from the code. - Options line:
preparestrips it beforejobOptionscomputes the title, so the title is the first line of the actual program, not// @options: ….
I ran codemode-execute-options.test.ts and tool-registry.test.ts in core, plus packages/tui test/util/session.test.ts: all pass. I didn't run the packages/app browser test (session-background.test.ts) and didn't re-check the CLI live. I made no changes; bun.lock was already modified in the checkout, and I left it alone.
| // A script is named by its job's title, which the execute tool sets from its code. A job without one falls back to the | ||
| // first line of the code that started it. | ||
| export function scriptLabel(messages: readonly SessionMessageInfo[], job: JobInfo) { | ||
| if (job.title) return job.title |
There was a problem hiding this comment.
job.title is always set for Code Mode jobs ("Code Mode script" by default), so this returns early and the first-line fallback below never runs. On dev this returns the first line of code, e.g. await build(). Here it returns "Code Mode script" for every script without full_codemode and for older jobs. The app's job list (label: job.title ?? … in background.ts) has the same problem.
There was a problem hiding this comment.
Fixed: the title now rides on the backgrounded execute call's metadata, and the TUI and app labels use that title first, then the call's first line of code, then the job title, as before. Older calls and Code Mode tools called at the top level keep their old labels.
There was a problem hiding this comment.
Looks right on 3e469374e6. Nothing I'd block on.
- Core:
jobOptions(codemode/tool.ts:181) builds the title fromevent.inputafterpreparehas run, so the title never includes a// @options:line.job.tsadds the title to both the running progress and the backgrounded call's finalmetadata. Withoutfull_codemodethere's no title, so the job keeps"Code Mode script"and the call's metadata stays{ jobID }. - TUI (
util/session.ts,scriptLabel): the call'smetadata.titlecomes first, then the first line ofinput.code, thenjob.title, then"Script". Old calls and non-full Code Mode get the same first-line label as ondev. The first-line fallback can only see an// @options:line when the call has no title, and a call written with that line has a title. - App (
background.ts:75,107): same order for both the backgrounded-script map and the running call's label. The job list at:183still useslatest?.label ?? info.title, which is fine because the call's label comes first.
I ran packages/tui test/util/session.test.ts (3 pass) and packages/core codemode-execute-options.test.ts + tool-registry.test.ts (63 pass) in a temporary worktree at pr/head, then removed it. I didn't run the app browser test (session-background.test.ts).
The shared checkout is at 67d72be94b, one commit behind pr/head, and has an uncommitted bun.lock change that isn't mine. I left both alone.
There was a problem hiding this comment.
The script naming regression is fixed: the TUI and app labels now use the call's saved title, then the first line of the code, and job.title only last. Scripts without full_codemode and older jobs keep their first-line names. I re-ran a throwaway scriptLabel check (it now returns the first line of code again). The changed tests pass in core (105), tui (3) and app (8), and tsc -b passes in all three. I found nothing new. Public API is unchanged beyond what the first review noted.
3e46937 to
2eec4e3
Compare
There was a problem hiding this comment.
The earlier threads are all addressed. The per-script title and its "Code Mode script" default are gone, and the TUI and app name scripts by their first line of code again, skipping // @ lines. An invalid options line still fails the call with instructions. The changed tests pass in core (115), tui (3) and app (8), and tsc -b passes in all three. A throwaway scriptLabel check gives the first line of code with and without the options line. Public API: no SDK, HTTP, config or CLI changes; plugins reading execute's definition see { code } for raw-text models (the trade-off in the description). ModelTools/Tool.Policy gain an internal rawText field. I found nothing new.
Issue for this PR
One of three options for the same problem, next to #65849 and #65714. Only one should merge.
Type of change
What does this PR do?
With
full_codemode, GPT now getsexecuteas a raw-text custom tool and can still settimeoutandbackground. It writes them on an optional first line of its code:Every other model keeps the JSON fields
{ code, timeout?, background? }. Withoutfull_codemodenothing changes.How it works (core only;
packages/aiandpackages/schemaare untouched):modelTools(the existing per-model tool policy) also reportsrawText: the model'scapabilities.freeformincludes"text".Tool.snapshotpasses it toCodeModeTool.create, which then declaresexecutewith onlycodeand explains the line in its description.session/context.tsis unchanged.executeis called,Tool.snapshotrunsCodeModeTool.preparebefore hooks. It moves the line intotimeoutandbackground, soexecute.beforehooks and the job runner see the same fields they would get from JSON. The plugin API doesn't change. Unknown keys are dropped. A line that isn't valid options, or has nothing after it, fails the call with a message saying how to write it.prepareruns for every model, so a line written before a model switch still works.// @, so labels show the program instead of the options line.The line format is known only in
codemode/tool.ts. The clients only know the generic "skip// @directive lines" rule.Trade-offs
rawText.// @...line when naming a script, not just this one.ModelToolshas a newrawTextfield. It doesn't change which tools are hidden or direct.How did you verify your code works?
packages/core:codemode-execute-options.test.ts(input shape and description per model, parsing with CRLF, leading spaces and unknown keys, errors for bad JSON, bad values and a missing program),tool-registry.test.ts(throughTool.snapshot, the line reaches the job runner as{ timeout: 5000 }for raw-text and JSON models, an invalid line fails, and withoutfull_codemodeit's only a comment),tool-execute.test.ts(compiled requests: GPT gets a custom text tool with the line described, Claude gets JSON withtimeout/background),tool-model-policy.test.ts(rawText). The core tests that touchfull_codemode,modelToolsorCodeModeJobpass: 378 pass, 2 fail. The 2 failures are ShellTool process-substitution tests that also fail ondevwith macOS bash.packages/tui:test/util/session.test.ts.packages/app:test-browser/session-background.test.ts.bun typecheckin core, tui and app, plusbun run checkat the root.OPENCODE_FLAGS=full_codemodewithopenai/gpt-6.2-sol. The model sent a custom-tool call// @options: {"background": true}\nreturn 6 * 7, which ran as a background job and reported 42. The stored input kept the line.Screenshots / recordings
N/A
Checklist