Skip to content

feat(core): take execute options on a first line of raw code for raw-text models - #65709

Open
rekram1-node wants to merge 1 commit into
devfrom
exec-options-line
Open

rekram1-node wants to merge 1 commit into
devfrom
exec-options-line

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

One of three options for the same problem, next to #65849 and #65714. Only one should merge.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

With full_codemode, GPT now gets execute as a raw-text custom tool and can still set timeout and background. It writes them on an optional first line of its code:

// @options: {"background": true}
return 6 * 7

Every other model keeps the JSON fields { code, timeout?, background? }. Without full_codemode nothing changes.

How it works (core only; packages/ai and packages/schema are untouched):

  • modelTools (the existing per-model tool policy) also reports rawText: the model's capabilities.freeform includes "text". Tool.snapshot passes it to CodeModeTool.create, which then declares execute with only code and explains the line in its description. session/context.ts is unchanged.
  • When execute is called, Tool.snapshot runs CodeModeTool.prepare before hooks. It moves the line into timeout and background, so execute.before hooks 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. prepare runs for every model, so a line written before a model switch still works.
  • The stored call keeps the line exactly as the model wrote it, so history replays it unchanged. The TUI and app name background scripts by their first line of code, and now skip lines starting with // @, 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

  • There's no grammar, so nothing forces the line's format. A malformed line fails the call with instructions instead.
  • The tool's shape depends on the session's model, so the snapshot cache key includes rawText.
  • The clients skip any leading // @... line when naming a script, not just this one.
  • ModelTools has a new rawText field. 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 (through Tool.snapshot, the line reaches the job runner as { timeout: 5000 } for raw-text and JSON models, an invalid line fails, and without full_codemode it's only a comment), tool-execute.test.ts (compiled requests: GPT gets a custom text tool with the line described, Claude gets JSON with timeout/background), tool-model-policy.test.ts (rawText). The core tests that touch full_codemode, modelTools or CodeModeJob pass: 378 pass, 2 fail. The 2 failures are ShellTool process-substitution tests that also fail on dev with macOS bash.
  • packages/tui: test/util/session.test.ts. packages/app: test-browser/session-background.test.ts.
  • bun typecheck in core, tui and app, plus bun run check at the root.
  • Live: OPENCODE_FLAGS=full_codemode with openai/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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread packages/core/src/codemode/tool.ts Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I checked e23f3991b and both fixes work.

  • Invalid line: splitOptions now 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 a Tool.Error from Tool.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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@rekram1-node rekram1-node changed the title feat(core): take execute's options on a first line of raw code for Lark models feat(core): take execute's options on a first line of raw code for raw-text models Nov 23, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread packages/core/src/codemode/job.ts Outdated
id,
type: "codemode",
title: "Code Mode script",
title: options?.title ?? "Code Mode script",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, this fixes it as of 3e469374e6. I see no regression for the cases I raised:

  • Titled scripts: job.ts now passes the title in each progress update and returns it in the handoff metadata ({ jobID, title }). scriptLabel in packages/tui/src/util/session.ts, and both the scripts and blocking paths in packages/app/src/session/requests/background.ts, read metadata.title first.
  • 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 of input.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: prepare strips it before jobOptions computes 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.

Comment thread packages/tui/src/util/session.ts Outdated
// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks right on 3e469374e6. Nothing I'd block on.

  • Core: jobOptions (codemode/tool.ts:181) builds the title from event.input after prepare has run, so the title never includes a // @options: line. job.ts adds the title to both the running progress and the backgrounded call's final metadata. Without full_codemode there's no title, so the job keeps "Code Mode script" and the call's metadata stays { jobID }.
  • TUI (util/session.ts, scriptLabel): the call's metadata.title comes first, then the first line of input.code, then job.title, then "Script". Old calls and non-full Code Mode get the same first-line label as on dev. 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 :183 still uses latest?.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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@rekram1-node rekram1-node changed the title feat(core): take execute's options on a first line of raw code for raw-text models feat(core): take execute options on a first line of raw code for raw-text models Nov 23, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

This branch has not been deployed

No deployments
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