The best way to control Claude Code, Codex and more from any computer or phone
fix(server): Codex threads get the whole T3 orchestration instructions (#18415)
Co-authored-by: Claude <noreply@anthropic.com>
Michael Johnston committed
Oct 18, 2026 at 12:12 UTC
91adab7161babfa2b573d3655781fba315c004fe
4 files changed
+92
-18
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+3
-3
@@ -508,11 +508,11 @@ describe("CodexAdapterV2 runtime policy", () => {
508
509
assert.equal(params.collaborationMode?.mode, "default");
510
assert.include(
511
- params.additionalContext?.t3_code_orchestration?.value ?? "",
511
+ params.additionalContext?.t3_code_orchestration_01?.value ?? "",
512
"Use `delegate_task`",
513
);
514
assert.include(
515
- params.additionalContext?.t3_code_orchestration?.value ?? "",
515
+ params.additionalContext?.t3_code_orchestration_02?.value ?? "",
516
"structured object, never as JSON text",
517
);
518
}),
@@ -2996,7 +2996,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
2996
hasT3Mcp: true,
2997
});
2998
assert.include(
2999
- params.additionalContext?.t3_code_orchestration?.value ?? "",
2999
+ params.additionalContext?.t3_code_orchestration_01?.value ?? "",
3000
"delegate_task",
3001
);
3002
const entries = codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "work" });
apps/server/src/provider/CodexDeveloperInstructions.test.ts
+54
@@ -1,12 +1,66 @@
1
import * as NodeAssert from "node:assert/strict";
2
3
+import {
4
+ T3_CODE_ORCHESTRATION_INSTRUCTIONS,
5
+ T3_CODE_ORCHESTRATION_SECTIONS,
6
+} from "@t3tools/provider-core/server/orchestrationInstructions";
7
import { describe, it } from "vite-plus/test";
8
9
import {
10
buildCodexAdditionalContext,
11
buildCodexDeveloperInstructions,
12
+ CODEX_ADDITIONAL_CONTEXT_VALUE_MAX_BYTES,
13
} from "./CodexDeveloperInstructions.ts";
14
15
+describe("buildCodexAdditionalContext", () => {
16
+ describe.each([
17
+ { model: "gpt-5.3-codex", reasoningEffort: "high" },
18
+ { model: "custom-model", modelName: "Custom 🦊 Model", reasoningEffort: "xhigh" },
19
+ ])("with runtime $model", (runtime) => {
20
+ it.each([
21
+ undefined,
22
+ false,
23
+ true,
24
+ { browser: false, device: false },
25
+ { browser: true, device: false },
26
+ { browser: false, device: true },
27
+ { browser: true, device: true },
28
+ ])("keeps every entry within Codex's per-entry cap with tools %j", (availability) => {
29
+ const context = buildCodexAdditionalContext(runtime, availability);
30
+
31
+ for (const [key, entry] of Object.entries(context)) {
32
+ // Codex counts ceil(UTF-8 bytes / 4) tokens, excluding keys and wrappers.
33
+ const bytes = Buffer.byteLength(entry.value, "utf8");
34
+ const limit = CODEX_ADDITIONAL_CONTEXT_VALUE_MAX_BYTES;
35
+ NodeAssert.ok(
36
+ bytes <= limit,
37
+ `${key}: ${bytes} UTF-8 bytes exceeds Codex's ${limit}-byte per-entry cap by ${bytes - limit} bytes; split the instructions into separate entries to avoid silent truncation`,
38
+ );
39
+ }
40
+ });
41
+ });
42
+
43
+ it("delivers all orchestration sections in authored order when keys are sorted", () => {
44
+ const context = buildCodexAdditionalContext({
45
+ model: "gpt-5.3-codex",
46
+ reasoningEffort: "high",
47
+ });
48
+ const sections = Object.keys(context)
49
+ .filter((key) => key.startsWith("t3_code_orchestration_"))
50
+ .sort()
51
+ .map((key) => context[key]!);
52
+
53
+ NodeAssert.deepEqual(
54
+ sections,
55
+ T3_CODE_ORCHESTRATION_SECTIONS.map((value) => ({ kind: "application", value })),
56
+ );
57
+ NodeAssert.equal(
58
+ sections.map((entry) => entry.value).join("\n\n"),
59
+ T3_CODE_ORCHESTRATION_INSTRUCTIONS.trim(),
60
+ );
61
+ });
62
+});
63
+
64
describe("buildCodexDeveloperInstructions", () => {
65
it("appends runtime info after the mode instructions", () => {
66
const instructions = runtimeInstructions({
apps/server/src/provider/CodexDeveloperInstructions.ts
+16
-3
@@ -4,9 +4,16 @@ import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeI
4
5
import {
6
T3_CODE_BROWSER_TOOL_INSTRUCTIONS,
7
- T3_CODE_ORCHESTRATION_INSTRUCTIONS,
7
+ T3_CODE_ORCHESTRATION_SECTIONS,
8
} from "@t3tools/provider-core/server/orchestrationInstructions";
9
10
+/**
11
+ * Codex cuts the middle out of any `additionalContext` value over 1,000 tokens,
12
+ * which it counts as UTF-8 bytes divided by four. It does this silently, so
13
+ * every value built here has to fit.
14
+ */
15
+export const CODEX_ADDITIONAL_CONTEXT_VALUE_MAX_BYTES = 4_000;
16
+
17
const T3_CODE_DEVICE_TOOL_INSTRUCTIONS = `## T3 Code devices
18
19
The \`t3-code\` MCP server also exposes \`device_*\` tools for iOS Simulators and Android Emulators on this environment. For mobile verification, call \`device_list\`, then \`device_open\` so the user can watch the device in their Device panel; its result explains how to drive the device. Driving happens through the \`agent-device\` CLI, using the exact launcher path returned by \`device_open\`. Keep the host config and session flags returned by \`device_open\` on every command so concurrent devices stay independent: prefer \`agent-device snapshot -i\` refs over coordinates, and use \`device_screenshot\` when you need to see the screen. Prefer these tools and \`agent-device\` for opening and driving devices. Platform tools such as \`xcrun simctl\` and \`adb\` remain available for anything they do not cover, such as builds, logs, or port forwarding. If \`device_list\` reports a platform as unavailable, say so.`;
@@ -202,9 +209,15 @@ export function buildCodexAdditionalContext(
209
toolsAvailable: boolean | T3CodeToolAvailability = true,
210
): Record<string, V2TurnStartParams__AdditionalContextEntry> {
211
const tools = toolInstructions(toolsAvailable);
205
- // Separate keys keep each value under Codex's per-entry token cap.
212
+ // Separate keys keep each value under Codex's per-entry cap. Codex emits
213
+ // entries in key order, so the numbering preserves the authored order.
214
return {
207
- t3_code_orchestration: { kind: "application", value: T3_CODE_ORCHESTRATION_INSTRUCTIONS },
215
+ ...Object.fromEntries(
216
+ T3_CODE_ORCHESTRATION_SECTIONS.map((section, index) => [
217
+ `t3_code_orchestration_${String(index + 1).padStart(2, "0")}`,
218
+ { kind: "application" as const, value: section },
219
+ ]),
220
+ ),
221
t3_code_runtime: {
222
kind: "application",
223
value: buildRuntimeInstructions({ harness: "Codex", ...runtime }),
packages/provider-core/src/server/orchestrationInstructions.ts
+19
-12
@@ -1,19 +1,22 @@
1
import type { ProviderInteractionMode } from "@t3tools/contracts";
2
3
-export const T3_CODE_ORCHESTRATION_INSTRUCTIONS = `
4
-
5
-## T3 Code orchestration
3
+/**
4
+ * Authored as self-contained sections so a provider that limits the size of
5
+ * each instruction entry can deliver every one whole. Codex cuts the middle out
6
+ * of any entry over 4,000 bytes, so start a new section before one gets close.
7
+ */
8
+export const T3_CODE_ORCHESTRATION_SECTIONS: ReadonlyArray<string> = [
9
+ `## T3 Code orchestration
10
11
The \`t3-code\` MCP server provides app-owned orchestration. Treat these concepts distinctly:
12
13
- A delegated task/subagent is child work owned by the current thread. Use \`orchestrator_capabilities\` to discover the current provider/model IDs from the same live catalog as the composer, including configured custom models. Do not treat a native tool's model list as the full list of available subagent models. Prefer native subagent tools for same-provider work only when they support the chosen model. Use \`delegate_task\` with that provider instance and model when native tools cannot, including for same-provider work. Also use \`delegate_task\` for cross-provider or explicitly T3-owned child tasks. For a child task that needs a separate checkout, use \`delegate_task\` with \`isolation:"worktree"\` across any provider or model. It starts from the parent local commit and runs project setup; uncommitted edits are not copied. Retain each returned \`taskId\`, and use \`task_status\` or \`task_cancel\` to manage it. The returned \`childThreadId\` is backing storage for the subagent, not the target for starting another delegated review round.
14
- \`t3_thread_launch\` and \`create_threads\` create ordinary top-level T3 conversations. Use them only when the user explicitly asks for separate/new/top-level threads or conversations. Never use them merely because the user said "subagent" or requested parallel delegated work.
11
-- For every T3 delegated review round, call \`delegate_task\` again. Include the original brief, prior findings, responses, and unresolved objections in each new task prompt. Track each round by its own \`taskId\`. Use a distinct \`clientRequestId\` per round, stable across retries of that round. Do not use \`t3_thread_send\` on \`childThreadId\` to continue a delegated review.
12
-- \`schedule_task\` creates persistent recurring work in the app scheduler. Pass \`schedule\` as a structured object, never as JSON text: \`{"type":"interval","everyMs":3600000}\` for an interval, or \`{"type":"fixed_time","timeOfDay":"09:00","weekdays":[1,2,3,4,5]}\` for a wall-clock schedule, or \`{"type":"webhook"}\` to run on each request to the returned \`webhookUrl\` (the run sees the request only through \`{{body.path}}\`-style placeholders in the prompt). By default runs return to the current thread, which suits orchestrating: each trigger arrives here and you delegate or dedupe; set \`bindToCurrentThread=false\` only when the user wants a fresh thread for every run. After scheduling a timer, report the returned cadence and next run time; for a webhook, report its \`webhookUrl\`, or say T3 Connect remote access is needed if it is missing.
15
+- For every T3 delegated review round, call \`delegate_task\` again. Include the original brief, prior findings, responses, and unresolved objections in each new task prompt. Track each round by its own \`taskId\`. Use a distinct \`clientRequestId\` per round, stable across retries of that round. Do not use \`t3_thread_send\` on \`childThreadId\` to continue a delegated review.`,
16
+ `- \`schedule_task\` creates persistent recurring work in the app scheduler. Pass \`schedule\` as a structured object, never as JSON text: \`{"type":"interval","everyMs":3600000}\` for an interval, or \`{"type":"fixed_time","timeOfDay":"09:00","weekdays":[1,2,3,4,5]}\` for a wall-clock schedule, or \`{"type":"webhook"}\` to run on each request to the returned \`webhookUrl\` (the run sees the request only through \`{{body.path}}\`-style placeholders in the prompt). By default runs return to the current thread, which suits orchestrating: each trigger arrives here and you delegate or dedupe; set \`bindToCurrentThread=false\` only when the user wants a fresh thread for every run. After scheduling a timer, report the returned cadence and next run time; for a webhook, report its \`webhookUrl\`, or say T3 Connect remote access is needed if it is missing.
17
- When you need a secret from the user (a token, API key, or webhook signing secret), call \`request_secret\` so they enter it privately, then pass the returned \`secretRef\` to the tool that needs it, e.g. \`signature.secretRef\` on a webhook task for a sender that signs requests such as GitHub. A \`secretRef\` works once. Never ask for a secret in chat, never invent one, and never repeat one.
14
-- To mention another thread to the user, link it as \`[title](t3-thread://v1/<threadId>)\` with its exact \`threadId\`, not URL-encoded. T3 Code opens the thread in the app and shows its current title.
15
-
16
-### Choose the workspace before starting a new thread
18
+- To mention another thread to the user, link it as \`[title](t3-thread://v1/<threadId>)\` with its exact \`threadId\`, not URL-encoded. T3 Code opens the thread in the app and shows its current title.`,
19
+ `### Choose the workspace before starting a new thread
20
21
For independent implementation or a PR stack in its own worktree, use \`t3_thread_launch\` with an explicit \`workspaceStrategy\`. It creates or selects the workspace, binds the new thread to it, and prepares it before the agent starts. Put the task in \`message\`, not \`prompt\`:
22
@@ -25,15 +28,19 @@ For stacked work, set \`baseRef\` to the intended parent branch and \`startFromO
28
29
\`t3_thread_launch\` is the single-thread launch tool. Use \`create_threads\` only for a batch of threads intentionally sharing the caller's checkout: it always inherits the caller's project, branch, and worktree and has no workspace override. Asking an agent to run \`git worktree add\` or \`cd\` in its prompt does not update T3's thread binding. Select the workspace in the launch call instead. \`t3_worktree_handoff\` moves the calling thread, not another thread, and cannot move a thread already attached to a worktree.
30
28
-\`t3_thread_launch\` has no idempotency key. Retain its returned threadId and inspect it with \`t3_thread_read\` / \`t3_thread_wait\`; preparation can still be running after acceptance. If a launch fails or its response is lost, inspect \`t3_thread_list\` before retrying, since a thread may already exist.
29
-
30
-Tool names may include a harness-normalized MCP prefix, such as \`mcp__t3_code__delegate_task\`; the semantics are the same. Some harnesses attach optional MCP servers lazily: if an initial tool-catalog scan does not show T3 tools, do not conclude that cross-provider delegation is unavailable. Make one bounded direct attempt using the known T3 tool name on the next tool step. In Codex code mode, for example, call \`tools.mcp__t3_code__orchestrator_capabilities({})\` before reporting that the capability is absent. Keep polling/wait loops bounded, do not duplicate active work, and use stable \`clientRequestId\` values when retrying tools that accept them.
31
+\`t3_thread_launch\` has no idempotency key. Retain its returned threadId and inspect it with \`t3_thread_read\` / \`t3_thread_wait\`; preparation can still be running after acceptance. If a launch fails or its response is lost, inspect \`t3_thread_list\` before retrying, since a thread may already exist.`,
32
+ `Tool names may include a harness-normalized MCP prefix, such as \`mcp__t3_code__delegate_task\`; the semantics are the same. Some harnesses attach optional MCP servers lazily: if an initial tool-catalog scan does not show T3 tools, do not conclude that cross-provider delegation is unavailable. Make one bounded direct attempt using the known T3 tool name on the next tool step. In Codex code mode, for example, call \`tools.mcp__t3_code__orchestrator_capabilities({})\` before reporting that the capability is absent. Keep polling/wait loops bounded, do not duplicate active work, and use stable \`clientRequestId\` values when retrying tools that accept them.
33
34
ACP fallback: some ACP agents accept the injected MCP server but fail to expose its tools. When the T3 tools are absent and \`T3_ACP_MCP_NODE\` is present, call the same tools through the terminal: \`ELECTRON_RUN_AS_NODE=1 "$T3_ACP_MCP_NODE" \${T3_ACP_MCP_ENTRYPOINT:+"$T3_ACP_MCP_ENTRYPOINT"} acp-mcp-call orchestrator_capabilities '{}'\` (\`T3_ACP_MCP_ENTRYPOINT\` is unset when T3 runs as a standalone executable). Delegate with \`acp-mcp-call delegate_task '{"task":"...","target":{"providerInstanceId":"...","model":"..."},"mode":"async","clientRequestId":"..."}'\`. This is the supported T3 transport fallback, not an ordinary shell-based substitute for delegation.
35
36
### Showing visuals
37
36
-When a chart, table, diagram, image collage, or mockup would say more than prose, build a self-contained HTML page, check it with \`html_preview\`, then publish it with \`html_render\` before your final reply. The reader sees the page above that reply, so don't announce or restate it; add only what it doesn't say.
38
+When a chart, table, diagram, image collage, or mockup would say more than prose, build a self-contained HTML page, check it with \`html_preview\`, then publish it with \`html_render\` before your final reply. The reader sees the page above that reply, so don't announce or restate it; add only what it doesn't say.`,
39
+];
40
+
41
+export const T3_CODE_ORCHESTRATION_INSTRUCTIONS = `
42
+
43
+${T3_CODE_ORCHESTRATION_SECTIONS.join("\n\n")}
44
`;
45
46
export const T3_CODE_BROWSER_TOOL_INSTRUCTIONS = `