Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions docs/codex-skill-runtime.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/harness-portability.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CLI research, trusted Web provider configuration, automatic-library proof backen

Codex Skill single research can additionally select `--skill-delegation read-only --skill-max-agents 2..4`. A native dynamic tool dispatches independent named read-only threads through the host, with one shared request gate and accounting ledger. This is host-mediated native threading; Codex's built-in V1/V2 spawn remains disabled because both inherit the primary writer's permission. Each investigator is archived before returning advisory findings. Parent shutdown confirms that the owned process group stopped before source freeze. Native permission profiles and dynamic tools require the app-server experimental capability, verified against Codex 0.169.0. Unsupported protocols or permission bindings fail explicitly and stop the owned tree.

Codex also supports `--research-mode skill --skill-delegation team --skill-max-agents 3`, with a total of 2–8 agents including Lead. It uses one host Lead (`workers=1`, `coordination=independent`) and the `theoremos_team` dynamic tool. Lead decides decomposition and adoption; children retain native threads, write Lean in isolated worktrees and communicate or deliver asynchronously. The host checks exact deliveries after writer shutdown and rechecks them during integration. See the [team launch and delivery protocol](codex-skill-runtime.md#agent-organized-proof-writing-team). This bridge is Codex-only; host-cooperative workflows remain available across all four harnesses. Checked team artifacts do not grant whole-problem acceptance.
Codex also supports `--research-mode skill --skill-delegation team --skill-max-agents 3`, with a total of 2–8 agents including Lead. It uses one host Lead (`workers=1`, `coordination=independent`). Native teams use Codex collaboration tools for dispatch and messaging and `theoremos_team` for proof handoffs. Teams using host tools also coordinate through `theoremos_team`. Lead decides decomposition and adoption; children retain native threads, write Lean in isolated worktrees and communicate or deliver asynchronously. The host checks exact deliveries after writer shutdown and rechecks them during integration. See the [team launch and delivery protocol](codex-skill-runtime.md#agent-organized-proof-writing-team). This bridge is Codex-only; host-cooperative workflows remain available across all four harnesses. Checked team artifacts do not grant whole-problem acceptance.

## Launch

Expand Down
20 changes: 10 additions & 10 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bun run build

构建生成 `out/main/math-solver.js`、`search-lean.js`、`check-lean.js` 和 `proof-search.js`,并输出 JSON。记录其中的 **`buildManifestRef`**,运行时需要传入这个文件的绝对路径。源码与 bundle 均校验哈希,修改运行时代码后需重新构建。

CLI 默认使用 `~/.config/deepscientist/math-sol-codex` 中的 Codex profile(本地 Sub2API 网关的 `deepseek-flash` / `max`,网关 `http://127.0.0.1:8080/v1`)。本机 profile 的认证命令按需读取已有部署的 Westlake 号池密钥,无需复制密钥或手动 export。也可通过 `CODEX_HOME` 指向其他已有 profile。
CLI 默认使用 `~/.config/deepscientist/math-sol-codex` 中的 Codex profile,通过本地 Sub2API 网关 `http://127.0.0.1:8080/v1` 调用模型。`run` 的默认模型为 `deepseek-flash`,新运行的默认推理强度为 `high`。本机 profile 的认证命令按需读取已有部署的 Westlake 号池密钥,无需复制密钥或手动 export。也可通过 `CODEX_HOME` 指向其他已有 profile。

`compare --provider sub2api` 和 `sample-putnam` 创建的独立 profile 使用环境变量 `SUB2API_WESTLAKE_API_KEY`;启动这些实验前需要在调用环境中设置它。密钥不会写入实验 profile 或数学工具环境文件。

Expand Down Expand Up @@ -101,7 +101,7 @@ bun run math run \
--output "$MATH_RUN_ROOT/result.json"
```

默认启用四个 Agent,解题预算为 30 分钟、合计 200 次模型调度。运行向标准输出发送 JSON 进度事件,并将结果写入 `result.json`。
默认启用四个 Agent。CLI 默认不限制解题时间、模型调度次数和 token 用量。需要限制时,可设置 `--max-run-seconds`、`--max-turns` 或 `--max-total-tokens`。Web worker 会传入该次运行的预算。运行向标准输出发送 JSON 进度事件,并将结果写入 `result.json`。

首次配置环境时,可在上述命令末尾加 `--startup-only`,只检查准备条件,不调用模型。预检会创建运行状态;之后使用相同参数加 `--resume` 开始求解。

Expand Down Expand Up @@ -346,21 +346,21 @@ Lead 的普通规划和初始规划执行 90 秒后收到保存提醒,120 秒
| 参数 | 默认值 | 作用 |
| --- | --- | --- |
| `--model` | `deepseek-flash` | 所选 provider 提供的模型 ID |
| `--effort` | `max` | 推理强度(Sub2API DeepSeek 支持 low/high/max) |
| `--effort` | `high` | 推理强度(Sub2API DeepSeek 支持 low/high/max) |
| `--workers` | `4` | Agent 总数,支持 1–8;四人以上增加专门的辅助引理 Prover |
| `--method` | `direct` | 仅支持 `direct`,保留以兼容已有命令 |
| `--coordination` | `cooperative` | 协作,或 `independent` 独立并行尝试 |
| `--continuation-manifest` | 无 | 新建时绑定显式历史附件;恢复时沿用同一只读快照 |
| `--scheduler` | `plan` | 按计划顺序调度;`critical-path` 按依赖链排序;`continuation` 优先承接已证成果的主路径任务 |
| `--prepare-timeout-seconds` | `1800` | 环境准备与依赖预编译预算 |
| `--max-run-seconds` | `1800` | 准备完成后的共享解题时间预算 |
| `--max-turns` | `200` | 所有 Agent 合计模型调度次数 |
| `--prepare-timeout-seconds` | `0` | 环境准备与依赖预编译预算 |
| `--max-run-seconds` | `0` | 准备完成后的共享解题时间预算 |
| `--max-turns` | `0` | 所有 Agent 合计模型调度次数 |
| `--max-total-tokens` | `0` | 全部角色累计的 provider 报告 token 阈值;`0` 不限。达到后停止新模型请求,在途请求仍可完成并产生超额 |
| `--max-turn-seconds` | `180` | 单轮时间限制;停止写入后检查已保存源码 |
| `--proof-timeout-seconds` | `1800` | 单次宿主证明检查预算;超时保存可恢复检查点 |
| `--evaluator-timeout-seconds` | `1800` | 集成后 Lean 复核与最终评测共享的独立时间预算 |
| `--max-turn-seconds` | `0` | 单轮时间限制;停止写入后检查已保存源码 |
| `--proof-timeout-seconds` | `0` | 单次宿主证明检查预算;超时保存可恢复检查点 |
| `--evaluator-timeout-seconds` | `0` | 集成后 Lean 复核与最终评测共享的独立时间预算 |

`--prepare-timeout-seconds`、`--max-run-seconds`、`--max-turns`、`--max-turn-seconds`、`--proof-timeout-seconds` 与 `--evaluator-timeout-seconds` 可设为 `0` 关闭对应限制。证明检查预算独立于总解题时间;关闭总解题时间限制不会关闭证明检查期限。同一源码、任务和环境的检查超时后保留候选,恢复时逐次延长检查时间,最多 24 小时。
`--prepare-timeout-seconds`、`--max-run-seconds`、`--max-turns`、`--max-turn-seconds`、`--proof-timeout-seconds` 与 `--evaluator-timeout-seconds` 在 CLI 中均默认为 `0`,表示不限。可以分别设置解题、证明检查和最终评测的时限。显式设置证明检查时限后,同一源码、任务和环境的检查超时会保留候选,恢复时逐次延长检查时间,最多 24 小时。

`--max-total-tokens` 包含规划、研究、证明和审阅角色,按跨线程累计消费计算;恢复时继续使用历史用量,省略参数时保留已存额度。它依据 provider 的事后通知阻止新请求,不能保证并发在途消费不超过阈值。报告保留超额与用量完整性;有限额度下缺失已执行请求的用量会停止继续派发。旧运行未记录逐轮用量完整性,不能直接当作严格 token 对照。默认不限 token,不为已有长跑增加停止条件。原生 provider 的用量按完整 turn 记账:turn 被 `--max-turn-seconds` 截断时该轮用量记为不完整,有限额度会以 usage-unavailable 停止;需要有限 token 额度时应同时放宽 `--max-turn-seconds`(或设为 0)。

Expand Down
13 changes: 10 additions & 3 deletions scripts/codex-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ All Cargo targets, logs and binaries stay under `--output`. On success,
not restarted. Source edits beyond the checked-in patches are preserved and
rejected, so freeze the adapter patch before running a final build.

New team runs pin this executable and freeze the native transport in their run
record. Saved runs without that transport keep their historical host-tool team.
An incompatible binary fails the capability handshake before a model turn.
New native team runs pin the executable's real path and digest and record
`agentTeam.transport: native-agent-control`. On certain compatibility or transport
errors during initial app-server startup, the solver can retry with
`theoremos_team` host tools. The failed writer must have stopped, and the Lead
must have no open session, saved thread, started turn or model request, or team
member. The solver emits `team_transport_fallback` and saves `host-tools` for
resume. Authentication failures and cancellation follow the normal error path.
A missing binary can fail earlier during configuration. New teams using the
direct Westlake Chat profile start with host tools. Checkpoints that predate the
transport field also use host tools.

After the ordinary headless runtime build, exercise the real binary with fixed
loopback Responses and independent process/workspace isolation:
Expand Down
24 changes: 10 additions & 14 deletions src/runtime/codexSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export async function createCodexMathSession(options: MathAgentOptions): Promise
let turnHasOutput = false;
const earlyOutputTurns = new Set<string>();
let completion: { resolve: (turn: Json) => void; reject: (error: Error) => void } | undefined;
let compacting: { resolve: (turn?: Json) => void; reject: (error: Error) => void;
let compacting: { resolve: (turn: Json) => void; reject: (error: Error) => void;
turnId?: string; installed?: boolean } | undefined;
const earlyCompletions = new Map<string, Json>();
const completedMessages = new Map<string, string>();
Expand Down Expand Up @@ -740,12 +740,10 @@ export async function createCodexMathSession(options: MathAgentOptions): Promise
const { id } = object(params.turn);
if (typeof id === 'string') compacting.turnId ??= id;
}
if (compacting && (method === 'thread/compacted' || method === 'item/completed' && object(params.item).type === 'contextCompaction')) {
if (!nativeControl) compacting.resolve();
else if (method === 'item/completed' && params.turnId === compacting.turnId) compacting.installed = true;
}
if (method === 'item/started' || method === 'item/completed') {
const item = object(params.item);
if (compacting?.turnId && method === 'item/completed' && params.turnId === compacting.turnId
&& item.type === 'contextCompaction') compacting.installed = true;
if (isTurnOutputItem(item)) recordTurnOutput(params.turnId);
observePublicItem(method, params, item);
if (method === 'item/completed' && completion && typeof params.turnId === 'string' &&
Expand Down Expand Up @@ -796,12 +794,10 @@ export async function createCodexMathSession(options: MathAgentOptions): Promise
const turn = object(params.turn);
if (typeof turn.id !== 'string' || typeof turn.status !== 'string' ||
!['completed', 'interrupted', 'failed'].includes(turn.status)) return;
if (compacting && (!compacting.turnId || turn.id === compacting.turnId)) {
if (compacting && turn.id === compacting.turnId) {
if (turn.status !== 'completed') compacting.reject(new Error('math_codex_compaction_' + turn.status));
else if (nativeControl) {
if (compacting.installed) compacting.resolve(turn);
else compacting.reject(new Error('math_codex_compaction_missing_summary'));
}
else if (compacting.installed) compacting.resolve(turn);
else compacting.reject(new Error('math_codex_compaction_missing_summary'));
}
if (turn.id === activeTurn && completion) completion.resolve(turn);
else if (completion && !activeTurn) {
Expand Down Expand Up @@ -872,8 +868,8 @@ export async function createCodexMathSession(options: MathAgentOptions): Promise
...(tools ? { dynamicTools: tools.definitions } : {}),
} } : undefined;
// Codex restores history internally. Echoing it can exceed the stdio frame limit.
// Keep the historical usage notification for direct providers with an active budget gate.
const metadataOnlyResume = nativeControl && (usesRequestReceipts || !options.requestGate);
// Retain historical usage replay for sessions that depend on native thread totals.
const metadataOnlyResume = usesRequestReceipts || (nativeControl && !options.requestGate);
const threadSettings = {
...settings,
...(options.threadId ? { threadId: options.threadId, ...(metadataOnlyResume ? { excludeTurns: true } : {}) } : {}),
Expand Down Expand Up @@ -940,7 +936,7 @@ export async function createCodexMathSession(options: MathAgentOptions): Promise
options.signal?.throwIfAborted();
if (closing || transportFailure) throw transportFailure ?? new Error('math_codex_transport_closed:closing');
if (completion || activeTurn || compacting) throw new Error('math_codex_turn_already_running');
const done = new Promise<Json | undefined>((resolve, reject) => { compacting = { resolve, reject }; });
const done = new Promise<Json>((resolve, reject) => { compacting = { resolve, reject }; });
turnSettled = false; turnStopping = false; turnCheckpointReached = false; requestTurn++;
const cancel = scheduleMathTimeout(() => {
compacting?.reject(new Error('math_codex_compaction_timeout'));
Expand All @@ -950,7 +946,7 @@ export async function createCodexMathSession(options: MathAgentOptions): Promise
options.signal?.addEventListener('abort', stop, { once: true });
try {
const [, turn] = await Promise.all([rpc.request('thread/compact/start', { threadId }), done]);
if (turn && nativeControl) await nativeControl.waitTurnSettled(String(turn.id), turn.status);
if (nativeControl) await nativeControl.waitTurnSettled(String(turn.id), turn.status);
}
catch (error) { await closeSession(); options.signal?.throwIfAborted(); throw error; }
finally {
Expand Down
12 changes: 12 additions & 0 deletions tests/codexSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ readline.createInterface({ input: process.stdin }).on('line', line => {
modelProvider: fixtureInstruction === 'wrong-provider' ? 'wrong-provider' : settings.config.model_provider,
...(fixtureInstruction === 'ignore-permissions' ? {} : { activePermissionProfile: { id: settings.config.default_permissions } }),
approvalPolicy: settings.approvalPolicy, reasoningEffort: settings.config.model_reasoning_effort });
if (call.method === 'thread/resume' && fixtureInstruction === 'restore-usage' && !settings.excludeTurns) {
send({ method: 'thread/tokenUsage/updated', params: { threadId: settings.threadId, turnId: 'saved-turn', tokenUsage: {
total: { totalTokens: 123 }, last: { inputTokens: 31 },
} } });
}
}
if (call.method === 'mcpServerStatus/list') result({ data: Object.keys(settings.config.mcp_servers)
.filter(name => settings.config.mcp_servers[name].enabled).map(name => ({ name,
Expand Down Expand Up @@ -761,6 +766,13 @@ readline.createInterface({ input: process.stdin }).on('line', line => {
expect(await done).toBe('interrupted');
});

it('receives historical usage when resuming with native thread accounting', async () => {
session = await createCodexMathSession({ ...options, threadId: 'retained-thread', instructions: 'restore-usage',
requestGate: () => ({ allowed: true, settle: () => {} }) });
expect(session.usageSource).toBe('native-thread-totals');
await vi.waitFor(() => expect(options.onUsage).toHaveBeenCalledWith({ totalTokens: 123 }, { turnId: 'saved-turn' }));
});

it('requires turn settlement before confirming writer shutdown and retains the resumable thread', async () => {
session = await createCodexMathSession({ ...options, threadId: 'freeze-thread' });
let started = false;
Expand Down
Loading
Loading