codex-supervisor-mcp 0.13.5 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +2 -2
- package/README.md +2 -2
- package/package.json +19 -63
- package/skills/codex-supervisor/SKILL.md +5 -4
- package/src/{acceptance.js → app/acceptance.js} +54 -68
- package/src/app/auto-cleanup.js +90 -0
- package/src/app/cancel.js +60 -0
- package/src/app/cleanup-facts.js +96 -0
- package/src/app/cleanup-plan.js +164 -0
- package/src/app/compose.js +7 -0
- package/src/{context-compiler.js → app/context-compiler.js} +7 -13
- package/src/app/dispatch.js +396 -0
- package/src/app/followup.js +98 -0
- package/src/app/land-chain.js +133 -0
- package/src/app/landing.js +357 -0
- package/src/app/ownership.js +57 -0
- package/src/app/rebase.js +216 -0
- package/src/app/restore-worktree.js +50 -0
- package/src/app/resume.js +181 -0
- package/src/app/run-state.js +26 -0
- package/src/app/settled.js +28 -0
- package/src/{side-chat.js → app/side-chat.js} +82 -265
- package/src/app/sse.js +20 -0
- package/src/app/start-run.js +77 -0
- package/src/app/verify-push.js +114 -0
- package/src/app/worktree-setup.js +176 -0
- package/src/{acceptance-baseline.js → domain/acceptance-baseline.js} +2 -1
- package/src/domain/acceptance-spec.js +30 -0
- package/src/domain/cleanup-policy.js +44 -0
- package/src/domain/command-outcome.js +6 -0
- package/src/{overview-delta.js → domain/overview-delta.js} +7 -3
- package/src/domain/side-turn-view.js +73 -0
- package/src/{worker-preflight.js → domain/worker-preflight.js} +1 -1
- package/src/{cleanup-hint.js → infra/cleanup-hint.js} +7 -8
- package/src/infra/git.js +30 -0
- package/src/infra/process.js +59 -0
- package/src/{sandbox-roots.js → infra/sandbox-roots.js} +2 -1
- package/src/{skill-sync.js → infra/skill-sync.js} +1 -1
- package/src/infra/store/db.js +265 -0
- package/src/infra/store/events.js +146 -0
- package/src/infra/store/facts.js +338 -0
- package/src/infra/store/reconcile.js +172 -0
- package/src/infra/store/records.js +171 -0
- package/src/infra/store/runner-readers.js +16 -0
- package/src/infra/store/schema.js +306 -0
- package/src/infra/store/sessions.js +138 -0
- package/src/infra/store/tasks.js +337 -0
- package/src/{auto-update-worker.js → infra/update/auto-update-worker.js} +4 -4
- package/src/{auto-update.js → infra/update/auto-update.js} +3 -3
- package/src/{update-check.js → infra/update/update-check.js} +4 -4
- package/src/{workspace-kind.js → infra/workspace-kind.js} +5 -7
- package/src/{worktree.js → infra/worktree.js} +56 -41
- package/src/{acceptance-cli.js → interface/bin/acceptance-cli.js} +7 -3
- package/src/{cleanup.js → interface/bin/cleanup.js} +4 -3
- package/src/{monitor.jsx → interface/bin/monitor.jsx} +4 -1
- package/src/{setup.js → interface/bin/setup.js} +7 -6
- package/src/{stage.js → interface/bin/stage.js} +1 -1
- package/src/{web-server.js → interface/bin/web-server.js} +2 -2
- package/src/interface/mcp/meta.js +7 -0
- package/src/interface/mcp/result.js +34 -0
- package/src/interface/mcp/schemas.js +24 -0
- package/src/interface/mcp/server.js +94 -0
- package/src/interface/mcp/tools/delivery.js +197 -0
- package/src/interface/mcp/tools/dispatch.js +267 -0
- package/src/interface/mcp/tools/facts.js +239 -0
- package/src/interface/mcp/tools/overview.js +202 -0
- package/src/interface/mcp/tools/poll.js +58 -0
- package/src/interface/mcp/tools/results.js +284 -0
- package/src/interface/mcp/tools/review.js +135 -0
- package/src/interface/mcp/tools/wait.js +145 -0
- package/src/interface/mcp/update-state.js +34 -0
- package/src/interface/mcp/views/overview.js +183 -0
- package/src/interface/mcp/views/task.js +223 -0
- package/src/interface/web/app.js +226 -0
- package/src/interface/web/views.js +224 -0
- package/src/mcp-server.js +3 -2223
- package/src/run-host/entry.js +23 -0
- package/src/run-host/finalize.js +236 -0
- package/src/run-host/launch.js +63 -0
- package/src/run-host/live-run.js +73 -0
- package/src/run-host/outcome-facts.js +61 -0
- package/src/run-host/streams.js +76 -0
- package/src/run-host/track.js +128 -0
- package/src/{runner-adapter.js → runners/codex/adapter.js} +6 -6
- package/src/runners/codex/chat-chunks.js +97 -0
- package/src/{event-parser.js → runners/codex/event-parser.js} +57 -55
- package/src/{goal-store.js → runners/codex/goal-store.js} +2 -2
- package/src/{mcp-config.js → runners/codex/mcp-config.js} +1 -1
- package/src/cleanup-plan.js +0 -158
- package/src/codex-runner.js +0 -1586
- package/src/landing.js +0 -507
- package/src/task-store.js +0 -1800
- package/src/web-app.js +0 -425
- /package/src/{failure-class.js → domain/failure-class.js} +0 -0
- /package/src/{owned-changes.js → domain/owned-changes.js} +0 -0
- /package/src/{prompt.js → domain/prompt.js} +0 -0
- /package/src/{status.js → domain/status.js} +0 -0
- /package/src/{thread-health.js → domain/thread-health.js} +0 -0
- /package/src/{truncate.js → domain/truncate.js} +0 -0
- /package/src/{worker-changes.js → domain/worker-changes.js} +0 -0
- /package/src/{worker-env.js → domain/worker-env.js} +0 -0
- /package/src/{worker-report.js → domain/worker-report.js} +0 -0
- /package/src/{worker-report.schema.json → domain/worker-report.schema.json} +0 -0
- /package/src/{bin-resolver.js → infra/bin-resolver.js} +0 -0
- /package/src/{external-output.js → infra/external-output.js} +0 -0
- /package/src/{handoff.js → infra/handoff.js} +0 -0
- /package/src/{paths.js → infra/paths.js} +0 -0
- /package/src/{stage-store.js → infra/stage-store.js} +0 -0
- /package/src/{tool-guard.js → runners/codex/tool-guard.js} +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { buildHostGoalSync } from "../../../domain/prompt.js";
|
|
2
|
+
import { ACTIVE_STATUSES, TASK_STATUSES } from "../../../domain/status.js";
|
|
3
|
+
import { truncateEventStrings } from "../../../domain/truncate.js";
|
|
4
|
+
import { getSession, upsertSession } from "../../../infra/store/sessions.js";
|
|
5
|
+
import { getTask, readTasks, readTasksBySession, searchTasks } from "../../../infra/store/tasks.js";
|
|
6
|
+
import { listTaskEventKinds, readTaskEvents } from "../../../infra/store/events.js";
|
|
7
|
+
import { reconcileDetachedActiveTasks } from "../../../infra/store/reconcile.js";
|
|
8
|
+
import { packageVersion } from "../meta.js";
|
|
9
|
+
import { errorResult, textResult } from "../result.js";
|
|
10
|
+
import { acceptanceCheck } from "../schemas.js";
|
|
11
|
+
import { autoUpdateState, refreshUpdateState, updateField, updateState } from "../update-state.js";
|
|
12
|
+
import { buildOverview, workRow } from "../views/overview.js";
|
|
13
|
+
import { statusView, summarizeTask } from "../views/task.js";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
export const listCodexWorkersTool = [
|
|
17
|
+
"list_codex_workers",
|
|
18
|
+
{
|
|
19
|
+
title: "List Codex workers",
|
|
20
|
+
description: "List tracked Codex workers. Defaults to active workers only.",
|
|
21
|
+
inputSchema: {
|
|
22
|
+
status: z.enum(TASK_STATUSES).optional(),
|
|
23
|
+
includeHistory: z.boolean().default(false),
|
|
24
|
+
includeDetails: z.boolean().default(false)
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
async ({ status, includeHistory, includeDetails } = {}) => {
|
|
28
|
+
await reconcileDetachedActiveTasks();
|
|
29
|
+
const tasks = await readTasks();
|
|
30
|
+
const filtered = status
|
|
31
|
+
? tasks.filter((task) => task.status === status)
|
|
32
|
+
: includeHistory
|
|
33
|
+
? tasks
|
|
34
|
+
: tasks.filter((task) => ACTIVE_STATUSES.has(task.status));
|
|
35
|
+
return textResult(includeDetails ? filtered : filtered.map(summarizeTask));
|
|
36
|
+
}
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
export const getOrchestrationOverviewTool = [
|
|
40
|
+
"get_orchestration_overview",
|
|
41
|
+
{
|
|
42
|
+
title: "Get orchestration overview",
|
|
43
|
+
description:
|
|
44
|
+
"Compact status table for every worker, budgeted to stay under ~2k tokens. This is the default read for an orchestrating main thread; fetch details per worker only when needed.",
|
|
45
|
+
inputSchema: {
|
|
46
|
+
status: z.enum(TASK_STATUSES).optional(),
|
|
47
|
+
limit: z.number().int().min(1).max(200).default(50)
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async ({ status, limit } = {}) => {
|
|
51
|
+
await reconcileDetachedActiveTasks();
|
|
52
|
+
const tasks = await readTasks();
|
|
53
|
+
const filtered = (status ? tasks.filter((task) => task.status === status) : tasks).slice(-limit);
|
|
54
|
+
// version and update ride inside the budgeted payload so approx_tokens
|
|
55
|
+
// still reports the real cost of the whole reply.
|
|
56
|
+
return textResult(
|
|
57
|
+
buildOverview(filtered, {
|
|
58
|
+
version: packageVersion,
|
|
59
|
+
update: updateField() ?? { update_available: false, installed_version: packageVersion, checked: updateState?.source ?? "pending" }
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
export const getSessionWorksTool = [
|
|
66
|
+
"get_session_works",
|
|
67
|
+
{
|
|
68
|
+
title: "Get session works",
|
|
69
|
+
description:
|
|
70
|
+
"Every worker dispatched under one session_id, oldest first. This is the reconnect path: a main-thread session that died can list the whole batch it started without holding any of it in context.",
|
|
71
|
+
inputSchema: {
|
|
72
|
+
session_id: z.string().min(1)
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
async ({ session_id }) => {
|
|
76
|
+
const tasks = await readTasksBySession(session_id);
|
|
77
|
+
const meta = await getSession(session_id);
|
|
78
|
+
return textResult({
|
|
79
|
+
session_id,
|
|
80
|
+
title: meta?.title ?? null,
|
|
81
|
+
note: meta?.note ?? null,
|
|
82
|
+
objective: meta?.objective ?? null,
|
|
83
|
+
default_acceptance: meta?.default_acceptance ?? [],
|
|
84
|
+
approval_state: meta?.approval_state ?? "approved",
|
|
85
|
+
policy_revision: meta?.policy_revision ?? 0,
|
|
86
|
+
count: tasks.length,
|
|
87
|
+
active: tasks.filter((task) => ACTIVE_STATUSES.has(task.status)).length,
|
|
88
|
+
works: tasks.map(workRow)
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
export const checkForUpdateTool = [
|
|
94
|
+
"check_for_update",
|
|
95
|
+
{
|
|
96
|
+
title: "Check for update",
|
|
97
|
+
description:
|
|
98
|
+
"Ask the npm registry whether a newer codex-supervisor-mcp is published, and whether the installed files match the published tarball. Returns installed and latest version, integrity check, and the install command. The same check runs on startup; call this to force a fresh one.",
|
|
99
|
+
inputSchema: {
|
|
100
|
+
force: z.boolean().default(true)
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
async ({ force }) => {
|
|
104
|
+
const result = await refreshUpdateState({ force });
|
|
105
|
+
if (!result) return textResult({ error: "update_check_failed" });
|
|
106
|
+
return textResult({ ...result, auto_update: autoUpdateState });
|
|
107
|
+
}
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
export const describeSessionTool = [
|
|
111
|
+
"describe_session",
|
|
112
|
+
{
|
|
113
|
+
title: "Describe session",
|
|
114
|
+
description:
|
|
115
|
+
"Record what a session is about: a one-line title and an optional note. Readers of the store (the web view, get_session_works) show these instead of the bare session id. Fields you omit are kept.",
|
|
116
|
+
inputSchema: {
|
|
117
|
+
session_id: z.string().min(1),
|
|
118
|
+
title: z.string().min(1).max(120).optional(),
|
|
119
|
+
note: z.string().min(1).max(2000).optional(),
|
|
120
|
+
objective: z.string().min(1).max(5000).optional(),
|
|
121
|
+
default_acceptance: z.array(acceptanceCheck).max(10).optional(),
|
|
122
|
+
approval_state: z.enum(["approved", "waiting_user"]).optional(),
|
|
123
|
+
expected_revision: z.number().int().nonnegative().optional()
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
async ({ session_id, title, note, objective, default_acceptance, approval_state, expected_revision }) => {
|
|
127
|
+
try {
|
|
128
|
+
const row = await upsertSession({
|
|
129
|
+
id: session_id, title: title ?? null, note: note ?? null, objective,
|
|
130
|
+
defaultAcceptance: default_acceptance, approvalState: approval_state, expectedRevision: expected_revision
|
|
131
|
+
});
|
|
132
|
+
return textResult({ ...row, session_id, host_goal_sync: buildHostGoalSync(row) });
|
|
133
|
+
} catch (error) { return errorResult(error); }
|
|
134
|
+
}
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
export const searchWorksTool = [
|
|
138
|
+
"search_works",
|
|
139
|
+
{
|
|
140
|
+
title: "Search works",
|
|
141
|
+
description:
|
|
142
|
+
"Substring search over title, goal, prompt and last message of every tracked worker, newest first. Use it to find an earlier work by keyword instead of paging through history.",
|
|
143
|
+
inputSchema: {
|
|
144
|
+
query: z.string().min(1),
|
|
145
|
+
limit: z.number().int().positive().max(100).default(20)
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
async ({ query, limit }) => {
|
|
149
|
+
const tasks = await searchTasks(query, limit);
|
|
150
|
+
return textResult({
|
|
151
|
+
query,
|
|
152
|
+
count: tasks.length,
|
|
153
|
+
works: tasks.map(workRow)
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
export const getCodexWorkerStatusTool = [
|
|
159
|
+
"get_codex_worker_status",
|
|
160
|
+
{
|
|
161
|
+
title: "Get Codex worker status",
|
|
162
|
+
description:
|
|
163
|
+
"Read the latest normalized status for one Codex worker: lifecycle, phase, current action, changed files, goal, recent commands, and the task prompt (clipped by default). Set includePrompt to read the prompt in full, or use get_codex_worker_events for the raw stream.",
|
|
164
|
+
inputSchema: {
|
|
165
|
+
task_id: z.string().min(1),
|
|
166
|
+
includePrompt: z.boolean().default(false),
|
|
167
|
+
promptMaxChars: z.number().int().min(0).max(1000000).default(5000)
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
async ({ task_id, includePrompt, promptMaxChars }) => {
|
|
171
|
+
const task = await getTask(task_id);
|
|
172
|
+
if (!task) return textResult({ error: "task_not_found", task_id });
|
|
173
|
+
return textResult(statusView(task, { includePrompt, promptMaxChars }));
|
|
174
|
+
}
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
export const getCodexWorkerEventsTool = [
|
|
178
|
+
"get_codex_worker_events",
|
|
179
|
+
{
|
|
180
|
+
title: "Get Codex worker events",
|
|
181
|
+
description:
|
|
182
|
+
"Read recent raw Codex JSONL events for one worker. limit, maxChars and kinds keep the read inside a context budget; maxChars truncates the middle of oversized strings the same way Codex truncates its own output.",
|
|
183
|
+
inputSchema: {
|
|
184
|
+
task_id: z.string().min(1),
|
|
185
|
+
limit: z.number().int().min(1).max(1000).default(100),
|
|
186
|
+
maxChars: z.number().int().min(0).max(1000000).optional(),
|
|
187
|
+
kinds: z.array(z.string().min(1)).optional()
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
async ({ task_id, limit, maxChars, kinds }) => {
|
|
191
|
+
const task = await getTask(task_id);
|
|
192
|
+
if (!task) return textResult({ error: "task_not_found", task_id });
|
|
193
|
+
const events = await readTaskEvents(task_id, limit, kinds ?? null);
|
|
194
|
+
const trimmed = maxChars === undefined ? events : events.map((event) => truncateEventStrings(event, maxChars));
|
|
195
|
+
return textResult({
|
|
196
|
+
task_id,
|
|
197
|
+
count: trimmed.length,
|
|
198
|
+
available_kinds: await listTaskEventKinds(task_id),
|
|
199
|
+
events: trimmed
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
];
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { sweepCleanup } from "../../../app/auto-cleanup.js";
|
|
2
|
+
import { recoverEligibleRuns } from "../../../app/resume.js";
|
|
3
|
+
import { buildOverviewDelta } from "../../../domain/overview-delta.js";
|
|
4
|
+
import { collectWorkerChanges } from "../../../domain/worker-changes.js";
|
|
5
|
+
import { cleanupHint } from "../../../infra/cleanup-hint.js";
|
|
6
|
+
import { readObserverCursor, readTasks, writeObserverCursor } from "../../../infra/store/tasks.js";
|
|
7
|
+
import { reconcileDetachedActiveTasks } from "../../../infra/store/reconcile.js";
|
|
8
|
+
import { textResult } from "../result.js";
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
|
|
12
|
+
const observerId = process.env.CODEX_SUPERVISOR_OBSERVER_ID?.trim() || `mcp-${randomUUID()}`;
|
|
13
|
+
const deltaObserverId = process.env.CODEX_SUPERVISOR_DELTA_OBSERVER_ID?.trim() || `${observerId}:delta`;
|
|
14
|
+
let observerRead = Promise.resolve();
|
|
15
|
+
let deltaRead = Promise.resolve();
|
|
16
|
+
export const overviewDeltaTool = [
|
|
17
|
+
"overview_delta",
|
|
18
|
+
{
|
|
19
|
+
title: "Get orchestration delta",
|
|
20
|
+
description: "Return a small cursor-based orchestration view: active, needs_attention, ready_to_land, ready_to_clean and changed task summaries, plus `cleaned` when safe worktrees were removed on this call. It never returns full history, prompts, diffs, events or worker capsules.",
|
|
21
|
+
inputSchema: { cursor: z.record(z.string(), z.string()).optional() }
|
|
22
|
+
},
|
|
23
|
+
async ({ cursor }) => {
|
|
24
|
+
const read = deltaRead.then(async () => {
|
|
25
|
+
await reconcileDetachedActiveTasks();
|
|
26
|
+
const tasks = await readTasks();
|
|
27
|
+
const previous = cursor ?? await readObserverCursor(deltaObserverId);
|
|
28
|
+
const cleaned = await sweepCleanup();
|
|
29
|
+
const result = buildOverviewDelta(cleaned.length ? await readTasks() : tasks, previous);
|
|
30
|
+
await writeObserverCursor(deltaObserverId, result.cursor);
|
|
31
|
+
const { cursor: nextCursor, ...reply } = result;
|
|
32
|
+
return textResult({ ...reply, next_cursor: nextCursor, ...(cleaned.length ? { cleaned } : {}) });
|
|
33
|
+
});
|
|
34
|
+
deltaRead = read.catch(() => {});
|
|
35
|
+
return read;
|
|
36
|
+
}
|
|
37
|
+
];
|
|
38
|
+
export const checkWorkersTool = ["check_workers", {
|
|
39
|
+
title: "Check changed workers",
|
|
40
|
+
description: "Check newly settled runs, actionable failures and active threads crossing the handoff threshold; automatically resume a dead lost/no_work first run once, except approval, authentication, permission and quota waits. Handoff reminders occur once per run and never cancel, replace or resume a live worker. Safe worktrees are removed along the way and listed under `cleaned`. Unchanged state is quiet. Call from a host-scheduled loop; it cannot wake a closed host. Set a distinct CODEX_SUPERVISOR_OBSERVER_ID per main thread to retain its cursor across restarts.",
|
|
41
|
+
inputSchema: {}
|
|
42
|
+
}, async () => {
|
|
43
|
+
const read = observerRead.then(async () => {
|
|
44
|
+
await reconcileDetachedActiveTasks();
|
|
45
|
+
const recoveries = await recoverEligibleRuns();
|
|
46
|
+
const result = collectWorkerChanges(await readTasks(), await readObserverCursor(observerId));
|
|
47
|
+
await writeObserverCursor(observerId, result.cursor);
|
|
48
|
+
const { cursor, ...reply } = result;
|
|
49
|
+
// The cleanup hint rides along without touching quiet: it is a standing fact
|
|
50
|
+
// about the store, not a new worker event.
|
|
51
|
+
const cleaned = await sweepCleanup();
|
|
52
|
+
const cleanup = await cleanupHint();
|
|
53
|
+
const extras = { ...(cleaned.length ? { cleaned } : {}), ...(cleanup ? { cleanup } : {}) };
|
|
54
|
+
return textResult({ ...reply, quiet: reply.quiet && recoveries.length === 0, recoveries, ...extras });
|
|
55
|
+
});
|
|
56
|
+
observerRead = read.catch(() => {});
|
|
57
|
+
return read;
|
|
58
|
+
}];
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { ensureTaskWorktree } from "../../../app/restore-worktree.js";
|
|
2
|
+
import { askWorker, endSideSession, latestSideTurn } from "../../../app/side-chat.js";
|
|
3
|
+
import { isGoalNeedingAttention, TERMINAL_STATUSES } from "../../../domain/status.js";
|
|
4
|
+
import { truncateMiddleChars, truncateReport } from "../../../domain/truncate.js";
|
|
5
|
+
import { getTask, recordTerminalChangedFiles } from "../../../infra/store/tasks.js";
|
|
6
|
+
import { readAgentMessages } from "../../../infra/store/events.js";
|
|
7
|
+
import { baseBehind, readTaskChanges, readWorktreeDiff, splitPatchHunks, workspaceSetupIgnorePaths, worktreeRef } from "../../../infra/worktree.js";
|
|
8
|
+
import { summarizeVerification } from "../../../runners/codex/event-parser.js";
|
|
9
|
+
import { readNativeGoal, readNativeGoalState } from "../../../runners/codex/goal-store.js";
|
|
10
|
+
import { textResult } from "../result.js";
|
|
11
|
+
import { buildWorkerSummary, externalOutputView, reportView, runEvidenceView } from "../views/overview.js";
|
|
12
|
+
import { acceptanceView } from "../views/task.js";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
export const getWorkerGoalTool = [
|
|
16
|
+
"get_worker_goal",
|
|
17
|
+
{
|
|
18
|
+
title: "Get worker goal",
|
|
19
|
+
description:
|
|
20
|
+
"Read the goal recorded for a worker, plus the native Codex thread goal when the thread has one (status, tokens used, time used).",
|
|
21
|
+
inputSchema: {
|
|
22
|
+
task_id: z.string().min(1)
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
async ({ task_id }) => {
|
|
26
|
+
const task = await getTask(task_id);
|
|
27
|
+
if (!task) return textResult({ error: "task_not_found", task_id });
|
|
28
|
+
return textResult({
|
|
29
|
+
task_id,
|
|
30
|
+
thread_id: task.thread_id,
|
|
31
|
+
status: task.status,
|
|
32
|
+
objective: task.goal_objective,
|
|
33
|
+
token_budget: task.goal_token_budget,
|
|
34
|
+
goal_status: task.goal_status,
|
|
35
|
+
needs_attention: isGoalNeedingAttention(task.goal_status),
|
|
36
|
+
native_goal: readNativeGoal(task.thread_id),
|
|
37
|
+
native_goal_read: readNativeGoalState(task.thread_id)
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
export const getWorkerSummaryTool = [
|
|
43
|
+
"get_worker_summary",
|
|
44
|
+
{
|
|
45
|
+
title: "Get worker summary",
|
|
46
|
+
description:
|
|
47
|
+
"One compact summary per worker: goal, status, changed files, last command, last message. Cheap enough for a main thread to read every worker at the end of a run.",
|
|
48
|
+
inputSchema: {
|
|
49
|
+
task_id: z.string().min(1)
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
async ({ task_id }) => {
|
|
53
|
+
const task = await getTask(task_id);
|
|
54
|
+
if (!task) return textResult({ error: "task_not_found", task_id });
|
|
55
|
+
const summary = buildWorkerSummary(task, readNativeGoal(task.thread_id));
|
|
56
|
+
// Cache the worktree diff on the row once the worker is done, so the
|
|
57
|
+
// overview's count stops lagging. A running row is the runner's to write.
|
|
58
|
+
if (TERMINAL_STATUSES.has(task.status) && summary.changed_files.length !== (task.changed_files ?? []).length) {
|
|
59
|
+
await recordTerminalChangedFiles(task.id, summary.changed_files);
|
|
60
|
+
}
|
|
61
|
+
return textResult(summary);
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
export const getWorkerResultTool = [
|
|
66
|
+
"get_worker_result",
|
|
67
|
+
{
|
|
68
|
+
title: "Get worker result",
|
|
69
|
+
description:
|
|
70
|
+
"Read a worker's own final report. The overview and the wait response clip the last message; this is the door for the actual conclusion. Returns the last N agent messages (each clipped past maxChars, default 6000 bytes, with truncated: true and the full size so you can re-read it whole), the status fields that tell a real finish from a crash, verification (the commands the worker ran, their exit codes and the tail of any failing output, to hold its report against), acceptance (the supervisor's own run of the dispatch-time acceptance commands, independent of the report: configured, passed, and each check's exit code and output tail; passed: false means resume the worker with that output instead of reading its report, passed: true against a report that says blocked or not done means trust the checks and read the diff), changed_file_count, and base_behind (how many commits the project's HEAD is past the worktree's base; resume with rebaseOnto when it is not 0). includeFiles: true adds the changed_files list; get_worker_diff is the ground truth for what changed.",
|
|
71
|
+
inputSchema: {
|
|
72
|
+
task_id: z.string().min(1),
|
|
73
|
+
limit: z.number().int().min(1).max(20).default(1),
|
|
74
|
+
// Per report, UTF-8 bytes. 0 means no clipping.
|
|
75
|
+
maxChars: z.number().int().min(0).max(1000000).default(6000),
|
|
76
|
+
includeFiles: z.boolean().default(false)
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
async ({ task_id, limit, maxChars, includeFiles }) => {
|
|
80
|
+
const task = await getTask(task_id);
|
|
81
|
+
if (!task) return textResult({ error: "task_not_found", task_id });
|
|
82
|
+
let reports = await readAgentMessages(task_id, limit);
|
|
83
|
+
let source = "event_stream";
|
|
84
|
+
if (reports.length === 0 && task.last_message) {
|
|
85
|
+
// Older rows can predate this tool, but the task row keeps the newest
|
|
86
|
+
// report even when the stream query finds nothing.
|
|
87
|
+
reports = [task.last_message];
|
|
88
|
+
source = "task_row";
|
|
89
|
+
}
|
|
90
|
+
const reportBytes = reports.map((text) => Buffer.byteLength(text, "utf8"));
|
|
91
|
+
const clipped = maxChars === 0 ? reports : reports.map((text) => truncateReport(text, maxChars));
|
|
92
|
+
const truncated = clipped.some((text, index) => text !== reports[index]);
|
|
93
|
+
const changed = readTaskChanges(task);
|
|
94
|
+
const behind = task.worktree_path ? baseBehind(task.project_root, task.base_commit) : null;
|
|
95
|
+
// A worker that ran in place has no worktree to diff, so this list is
|
|
96
|
+
// the only record of what it touched; it is never hidden for those.
|
|
97
|
+
const listFiles = includeFiles || !task.worktree_path;
|
|
98
|
+
return textResult({
|
|
99
|
+
task_id,
|
|
100
|
+
title: task.title,
|
|
101
|
+
status: task.status,
|
|
102
|
+
phase: task.phase ?? null,
|
|
103
|
+
exit_code: task.exit_code ?? null,
|
|
104
|
+
thread_id: task.thread_id,
|
|
105
|
+
session_id: task.session_id ?? null,
|
|
106
|
+
workspace_kind: task.workspace_kind ?? "legacy",
|
|
107
|
+
worktree_path: task.worktree_path,
|
|
108
|
+
goal_status: task.goal_status,
|
|
109
|
+
attention_reason: task.attention_reason ?? null,
|
|
110
|
+
run_evidence: runEvidenceView(task),
|
|
111
|
+
structured_report: reportView(task, maxChars),
|
|
112
|
+
input_context: task.input_context ?? null,
|
|
113
|
+
external_outputs: externalOutputView(task, maxChars || 6000),
|
|
114
|
+
changed_file_count: changed.length,
|
|
115
|
+
changed_files: listFiles ? changed : undefined,
|
|
116
|
+
base_commit: task.base_commit ?? null,
|
|
117
|
+
base_behind: behind,
|
|
118
|
+
landed: task.landed_at ? { at: task.landed_at, head: task.landed_head ?? null } : null,
|
|
119
|
+
acceptance: acceptanceView(task, behind),
|
|
120
|
+
verification: summarizeVerification(task),
|
|
121
|
+
report_count: clipped.length,
|
|
122
|
+
report_bytes: reportBytes,
|
|
123
|
+
truncated,
|
|
124
|
+
next_step: truncated
|
|
125
|
+
? `a report was clipped to ${maxChars} bytes; call again with maxChars: ${Math.max(...reportBytes)} (or 0) to read it whole`
|
|
126
|
+
: null,
|
|
127
|
+
reports: clipped,
|
|
128
|
+
source,
|
|
129
|
+
error: task.error ?? null,
|
|
130
|
+
completed_at: task.completed_at ?? null
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
// Cleanup keeps the branch; the diff is read from a worktree cut from it
|
|
136
|
+
// again. Null when the worktree is there, a refusal when it cannot come back.
|
|
137
|
+
async function restoreRefusal(task) {
|
|
138
|
+
try {
|
|
139
|
+
await ensureTaskWorktree(task);
|
|
140
|
+
return null;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
return textResult({ error: error.code ?? "worktree_restore_failed", task_id: task.id, reason: error.message.split("\n")[0] });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const getWorkerDiffTool = [
|
|
147
|
+
"get_worker_diff",
|
|
148
|
+
{
|
|
149
|
+
title: "Get worker diff",
|
|
150
|
+
description:
|
|
151
|
+
"The worker's actual changes. By default this is a complete file/hunk index; request one file to page through hunks with pageSize or cursor. Supplying maxChars without file preserves the legacy bounded full-diff response. This is the ground truth to check a worker's report against.",
|
|
152
|
+
inputSchema: {
|
|
153
|
+
task_id: z.string().min(1),
|
|
154
|
+
maxChars: z.number().int().min(0).max(1000000).optional(),
|
|
155
|
+
file: z.string().min(1).optional(),
|
|
156
|
+
hunk: z.number().int().min(0).optional(),
|
|
157
|
+
pageSize: z.number().int().min(1).max(20).default(4),
|
|
158
|
+
cursor: z.string().min(1).optional(),
|
|
159
|
+
paths: z.array(z.string().min(1)).optional()
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
async ({ task_id, maxChars, file, hunk, pageSize, cursor, paths }) => {
|
|
163
|
+
const task = await getTask(task_id);
|
|
164
|
+
if (!task) return textResult({ error: "task_not_found", task_id });
|
|
165
|
+
if (!task.worktree_path) {
|
|
166
|
+
return textResult({ error: "no_worktree", task_id, external_outputs: externalOutputView(task, maxChars), reason: "this worker ran in place (its cwd is not a git repository or the worktree could not be created), so there is no isolated diff to read; call get_worker_result with includeFiles: true for the files it touched" });
|
|
167
|
+
}
|
|
168
|
+
const refusal = await restoreRefusal(task);
|
|
169
|
+
if (refusal) return refusal;
|
|
170
|
+
if (!file && maxChars !== undefined) {
|
|
171
|
+
const diff = readWorktreeDiff(task.worktree_path, worktreeRef(task), { maxChars, paths: paths ?? [], ignorePaths: workspaceSetupIgnorePaths(task) });
|
|
172
|
+
return textResult({
|
|
173
|
+
task_id,
|
|
174
|
+
title: task.title,
|
|
175
|
+
status: task.status,
|
|
176
|
+
worktree_path: task.worktree_path,
|
|
177
|
+
branch: `codex/${task.id}`,
|
|
178
|
+
...diff,
|
|
179
|
+
external_outputs: externalOutputView(task, maxChars)
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
const diffOptions = { maxChars: 10_000_000, paths: paths ?? [], ignorePaths: workspaceSetupIgnorePaths(task) };
|
|
183
|
+
const diff = readWorktreeDiff(task.worktree_path, worktreeRef(task), diffOptions);
|
|
184
|
+
if (diff.error) return textResult({ ...diff, task_id });
|
|
185
|
+
const files = diff.files.map((entry) => {
|
|
186
|
+
const parsed = splitPatchHunks(entry);
|
|
187
|
+
const { path, status, from, additions, deletions } = entry;
|
|
188
|
+
return { path, status, from, additions, deletions, hunk_count: parsed.hunks.length };
|
|
189
|
+
});
|
|
190
|
+
const base = {
|
|
191
|
+
task_id,
|
|
192
|
+
title: task.title,
|
|
193
|
+
status: task.status,
|
|
194
|
+
worktree_path: task.worktree_path,
|
|
195
|
+
branch: `codex/${task.id}`,
|
|
196
|
+
...diff,
|
|
197
|
+
files,
|
|
198
|
+
external_outputs: externalOutputView(task, maxChars || 6000)
|
|
199
|
+
};
|
|
200
|
+
if (!file) return textResult({ ...base, summary_only: true, next_step: "choose files[].path, then request file plus hunk or cursor" });
|
|
201
|
+
const selected = diff.files.find((entry) => entry.path === file);
|
|
202
|
+
if (!selected) return textResult({ error: "diff_file_not_found", task_id, file, files });
|
|
203
|
+
const parsed = splitPatchHunks(selected);
|
|
204
|
+
let offset = 0;
|
|
205
|
+
if (cursor) {
|
|
206
|
+
try {
|
|
207
|
+
const value = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
|
|
208
|
+
if (value.task_id !== task_id || value.file !== file || !Number.isInteger(value.offset) || value.offset < 0) throw new Error("mismatch");
|
|
209
|
+
offset = value.offset;
|
|
210
|
+
} catch {
|
|
211
|
+
return textResult({ error: "invalid_diff_cursor", task_id, file });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (hunk !== undefined) offset = hunk;
|
|
215
|
+
if (offset >= parsed.hunks.length) return textResult({ error: "diff_hunk_not_found", task_id, file, hunk: offset, hunk_count: parsed.hunks.length });
|
|
216
|
+
let remaining = maxChars ?? 24000;
|
|
217
|
+
const hunks = [];
|
|
218
|
+
for (let index = offset; index < parsed.hunks.length && hunks.length < pageSize; index += 1) {
|
|
219
|
+
if (remaining <= 0) break;
|
|
220
|
+
const item = parsed.hunks[index];
|
|
221
|
+
const patchText = item.patch.length > remaining ? truncateMiddleChars(item.patch, remaining) : item.patch;
|
|
222
|
+
hunks.push({ ...item, patch: patchText, truncated: patchText.length < item.patch.length });
|
|
223
|
+
remaining -= patchText.length;
|
|
224
|
+
}
|
|
225
|
+
const nextOffset = offset + hunks.length;
|
|
226
|
+
const nextCursor = nextOffset < parsed.hunks.length
|
|
227
|
+
? Buffer.from(JSON.stringify({ task_id, file, offset: nextOffset })).toString("base64url")
|
|
228
|
+
: null;
|
|
229
|
+
return textResult({
|
|
230
|
+
...base,
|
|
231
|
+
file: {
|
|
232
|
+
path: selected.path, status: selected.status, from: selected.from,
|
|
233
|
+
additions: selected.additions, deletions: selected.deletions, hunk_count: parsed.hunks.length
|
|
234
|
+
},
|
|
235
|
+
hunks,
|
|
236
|
+
next_cursor: nextCursor,
|
|
237
|
+
summary_only: false
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
];
|
|
241
|
+
|
|
242
|
+
export const askCodexWorkerTool = [
|
|
243
|
+
"ask_codex_worker",
|
|
244
|
+
{
|
|
245
|
+
title: "Ask a worker on the side",
|
|
246
|
+
description:
|
|
247
|
+
"Ask a finished or running worker a question without touching its thread: the worker's Codex thread is forked once into a read-only side session (like Codex's own /btw), the question is asked there, and later questions resume that fork. The fork carries everything the worker saw and did, so 'why did you change X' or 'where is Y handled' costs one short answer instead of a read of the event stream. The fork has no network and no MCP tools and is told so; it answers from its context and the worktree. Once the worker has been resumed, the next question automatically takes a new fork from the updated thread (refreshed: \"worker_resumed\"); fresh: true forces a new fork. Changes still go through resume_codex_worker. Without a question it returns the latest side turn (for an answer that outran timeoutMs). end: true deletes the fork.",
|
|
248
|
+
inputSchema: {
|
|
249
|
+
task_id: z.string().min(1),
|
|
250
|
+
question: z.string().min(1).optional(),
|
|
251
|
+
timeoutMs: z.number().int().min(1000).max(600000).default(110000),
|
|
252
|
+
maxChars: z.number().int().min(0).max(1000000).default(8000),
|
|
253
|
+
fresh: z.boolean().default(false),
|
|
254
|
+
end: z.boolean().default(false)
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
async ({ task_id, question, timeoutMs, maxChars, fresh, end }) => {
|
|
258
|
+
const task = await getTask(task_id);
|
|
259
|
+
if (!task) return textResult({ error: "task_not_found", task_id });
|
|
260
|
+
const clipAnswer = (state) => {
|
|
261
|
+
if (state?.turn?.answer && maxChars !== undefined) state.turn.answer = truncateMiddleChars(state.turn.answer, maxChars);
|
|
262
|
+
return state;
|
|
263
|
+
};
|
|
264
|
+
if (end) {
|
|
265
|
+
const ended = await endSideSession(task_id);
|
|
266
|
+
return textResult({ task_id, ...ended });
|
|
267
|
+
}
|
|
268
|
+
if (!question) {
|
|
269
|
+
return textResult({ task_id, ...clipAnswer(await latestSideTurn(task_id)) });
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
const state = clipAnswer(await askWorker({ task, question, timeoutMs, fresh }));
|
|
273
|
+
const next = state.timed_out
|
|
274
|
+
? `The side turn is still running; call ask_codex_worker again with only task_id to read the answer when it lands.`
|
|
275
|
+
: null;
|
|
276
|
+
return textResult({ task_id, ...state, next_step: next });
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (error.code === "busy") {
|
|
279
|
+
return textResult({ task_id, error: "busy", reason: error.message, ...clipAnswer(await latestSideTurn(task_id)) });
|
|
280
|
+
}
|
|
281
|
+
return textResult({ task_id, error: error.code ?? "side_turn_failed", reason: error.message });
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
];
|