codex-supervisor-mcp 0.0.0-stage → 0.5.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.
@@ -0,0 +1,176 @@
1
+ // Maps the Codex exec JSONL stream onto the work state machine.
2
+ //
3
+ // Invariant: status is the lifecycle and is only ever assigned queued /
4
+ // running / completed / failed / cancelled / lost. Progress details go to
5
+ // phase, which is only meaningful while the work is running.
6
+
7
+ function getEventType(event) {
8
+ return event.type ?? event.event ?? event.msg?.type ?? "unknown";
9
+ }
10
+
11
+ function getItem(event) {
12
+ return event.item ?? event.msg?.item ?? event.data?.item ?? null;
13
+ }
14
+
15
+ function inferCommand(item) {
16
+ return item?.command ?? item?.cmd ?? item?.arguments?.cmd ?? item?.text ?? null;
17
+ }
18
+
19
+ function inferAgentMessage(item) {
20
+ return item?.text ?? item?.message ?? item?.content ?? item?.output_text ?? null;
21
+ }
22
+
23
+ // Codex reports edits as {"changes":[{"path": "...", "kind": "add"}]}. The
24
+ // older flat shapes are kept for compatibility with other event producers.
25
+ function inferChangedFiles(item) {
26
+ const candidates = [item?.path, item?.file, item?.file_path, item?.filename].filter(Boolean);
27
+ if (Array.isArray(item?.changes)) {
28
+ for (const change of item.changes) {
29
+ const path = change?.path ?? change?.file;
30
+ if (path) candidates.push(path);
31
+ }
32
+ }
33
+ if (Array.isArray(item?.files)) candidates.push(...item.files);
34
+ return candidates;
35
+ }
36
+
37
+ const ITEM_PHASE = new Map([
38
+ ["command_execution", "command"],
39
+ ["file_change", "editing"],
40
+ ["agent_message", "reporting"],
41
+ ["reasoning", "thinking"],
42
+ ["todo_list", "thinking"],
43
+ ["mcp_tool_call", "command"]
44
+ ]);
45
+
46
+ // Codex emits informational items as `item.type === "error"` (config warnings,
47
+ // "long threads drift" heads-ups). They are not task failures, so they are
48
+ // recorded as notices instead of setting the error field.
49
+ function appendNotice(task, message) {
50
+ if (!message) return task;
51
+ const existing = task.notices ? task.notices.split("\n") : [];
52
+ if (existing.includes(message)) return task;
53
+ const combined = [...existing, message].join("\n");
54
+ return { ...task, notices: combined.length > 4000 ? combined.slice(-4000) : combined };
55
+ }
56
+
57
+ const GOAL_EVENT_TYPES = new Set(["thread.goal.updated", "thread_goal_updated", "goal.updated"]);
58
+
59
+ function applyGoalEvent(next, event) {
60
+ const goal = event.goal ?? event.msg?.goal ?? event.data?.goal ?? event;
61
+ if (!goal) return next;
62
+ if (goal.status) next.goal_status = goal.status;
63
+ if (goal.tokens_used ?? goal.tokensUsed) next.goal_tokens_used = goal.tokens_used ?? goal.tokensUsed;
64
+ if (goal.token_budget ?? goal.tokenBudget) next.goal_token_budget = goal.token_budget ?? goal.tokenBudget;
65
+ if (goal.time_used_seconds ?? goal.timeUsedSeconds) {
66
+ next.goal_time_used_seconds = goal.time_used_seconds ?? goal.timeUsedSeconds;
67
+ }
68
+ if (goal.objective) next.goal_objective = goal.objective;
69
+ next.goal_updated_at = new Date().toISOString();
70
+ return next;
71
+ }
72
+
73
+ export function applyCodexEvent(task, event) {
74
+ const now = new Date().toISOString();
75
+ const eventType = getEventType(event);
76
+ const item = getItem(event);
77
+ const itemType = item?.type ?? item?.kind ?? item?.name ?? null;
78
+ let next = {
79
+ ...task,
80
+ last_event_at: now,
81
+ last_event_type: eventType
82
+ };
83
+
84
+ if (eventType === "thread.started") {
85
+ next.status = "running";
86
+ next.phase = "starting";
87
+ const threadId = event.thread_id ?? event.threadId ?? event.msg?.thread_id;
88
+ if (threadId) next.thread_id = threadId;
89
+ return next;
90
+ }
91
+
92
+ if (eventType === "turn.started") {
93
+ next.status = "running";
94
+ next.phase = "thinking";
95
+ return next;
96
+ }
97
+
98
+ if (ITEM_PHASE.has(itemType) && (eventType === "item.started" || eventType === "item.updated")) {
99
+ next.status = "running";
100
+ next.phase = ITEM_PHASE.get(itemType);
101
+ if (itemType === "command_execution") {
102
+ next.current_command = inferCommand(item);
103
+ next.current_action = next.current_command ? `Running ${next.current_command}` : "Running command";
104
+ } else if (itemType === "file_change") {
105
+ next.current_action = "Editing files";
106
+ } else if (itemType !== "reasoning" && itemType !== "todo_list") {
107
+ next.current_action = `Processing ${itemType}`;
108
+ }
109
+ return next;
110
+ }
111
+
112
+ if (eventType === "item.completed") {
113
+ if (itemType === "command_execution") {
114
+ const command = inferCommand(item);
115
+ next.phase = "command";
116
+ next.current_command = null;
117
+ next.commands = [
118
+ ...(next.commands ?? []),
119
+ {
120
+ command,
121
+ exit_code: item?.exit_code ?? item?.exitCode ?? null,
122
+ status: item?.status ?? "completed",
123
+ completed_at: now
124
+ }
125
+ ];
126
+ return next;
127
+ }
128
+ if (itemType === "file_change") {
129
+ const files = inferChangedFiles(item);
130
+ next.phase = "editing";
131
+ next.changed_files = Array.from(new Set([...(next.changed_files ?? []), ...files]));
132
+ return next;
133
+ }
134
+ if (itemType === "agent_message") {
135
+ const message = inferAgentMessage(item);
136
+ next.phase = "reporting";
137
+ if (message) next.last_message = message;
138
+ return next;
139
+ }
140
+ if (itemType === "error") {
141
+ return appendNotice(next, item?.message ?? item?.text ?? null);
142
+ }
143
+ if (itemType === "reasoning" || itemType === "mcp_tool_call" || itemType === "todo_list") {
144
+ return next;
145
+ }
146
+ }
147
+
148
+ if (eventType === "turn.completed") {
149
+ next.status = "completed";
150
+ next.phase = null;
151
+ next.current_action = "Completed";
152
+ next.current_command = null;
153
+ next.completed_at = now;
154
+ return next;
155
+ }
156
+
157
+ if (eventType === "turn.failed") {
158
+ next.status = "failed";
159
+ next.phase = null;
160
+ next.current_action = "Failed";
161
+ next.current_command = null;
162
+ next.completed_at = now;
163
+ next.error = event.error ?? event.message ?? event.msg?.error ?? "Codex turn failed";
164
+ return next;
165
+ }
166
+
167
+ if (eventType === "error") {
168
+ return appendNotice(next, event.message ?? event.error ?? "Codex reported an error event");
169
+ }
170
+
171
+ if (GOAL_EVENT_TYPES.has(eventType)) {
172
+ return applyGoalEvent(next, event);
173
+ }
174
+
175
+ return next;
176
+ }
@@ -0,0 +1,51 @@
1
+ // Read-only view of Codex's own thread goals.
2
+ //
3
+ // Codex stores goals in $CODEX_HOME/goals_1.sqlite (table thread_goals). The
4
+ // supervisor never writes there: Codex owns that file. When a worker thread
5
+ // has a native goal we surface it; when it does not, the supervisor falls back
6
+ // to the goal it recorded at dispatch time.
7
+ import { existsSync } from "node:fs";
8
+ import { DatabaseSync } from "node:sqlite";
9
+ import { goalsDbPath } from "./paths.js";
10
+ import { mapGoalStatus } from "./status.js";
11
+
12
+ function toCamelCase(status) {
13
+ return String(status).replace(/_([a-z])/g, (_match, letter) => letter.toUpperCase());
14
+ }
15
+
16
+ export function readNativeGoal(threadId) {
17
+ if (!threadId || !existsSync(goalsDbPath)) return null;
18
+ let db;
19
+ try {
20
+ db = new DatabaseSync(goalsDbPath, { readOnly: true });
21
+ db.exec("PRAGMA busy_timeout=2000;");
22
+ const row = db.prepare(`
23
+ SELECT goal_id, objective, status, token_budget, tokens_used,
24
+ time_used_seconds, created_at_ms, updated_at_ms
25
+ FROM thread_goals
26
+ WHERE thread_id = ?
27
+ `).get(threadId);
28
+ if (!row) return null;
29
+ return {
30
+ objective: row.objective,
31
+ status: toCamelCase(row.status),
32
+ storage_status: row.status,
33
+ work_status: mapGoalStatus(row.status),
34
+ token_budget: row.token_budget ?? null,
35
+ tokens_used: row.tokens_used ?? 0,
36
+ time_used_seconds: row.time_used_seconds ?? 0,
37
+ created_at: row.created_at_ms ? new Date(row.created_at_ms).toISOString() : null,
38
+ updated_at: row.updated_at_ms ? new Date(row.updated_at_ms).toISOString() : null
39
+ };
40
+ } catch {
41
+ // A locked or half-migrated Codex database must never break a supervisor
42
+ // call; the supervisor goal is still returned by the caller.
43
+ return null;
44
+ } finally {
45
+ try {
46
+ db?.close();
47
+ } catch {
48
+ /* already closed */
49
+ }
50
+ }
51
+ }