@nightloom/isy 1.0.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/LICENSE +21 -0
- package/README.md +69 -0
- package/dist/agents/claude.d.ts +6 -0
- package/dist/agents/claude.js +66 -0
- package/dist/agents/claude.js.map +1 -0
- package/dist/agents/codex-records.d.ts +34 -0
- package/dist/agents/codex-records.js +492 -0
- package/dist/agents/codex-records.js.map +1 -0
- package/dist/agents/codex.d.ts +5 -0
- package/dist/agents/codex.js +205 -0
- package/dist/agents/codex.js.map +1 -0
- package/dist/agents/index.d.ts +25 -0
- package/dist/agents/index.js +50 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/kimi-records.d.ts +24 -0
- package/dist/agents/kimi-records.js +252 -0
- package/dist/agents/kimi-records.js.map +1 -0
- package/dist/agents/kimi-wire.d.ts +8 -0
- package/dist/agents/kimi-wire.js +158 -0
- package/dist/agents/kimi-wire.js.map +1 -0
- package/dist/agents/kimi.d.ts +6 -0
- package/dist/agents/kimi.js +332 -0
- package/dist/agents/kimi.js.map +1 -0
- package/dist/agents/types.d.ts +83 -0
- package/dist/agents/types.js +2 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/alert.d.ts +24 -0
- package/dist/alert.js +68 -0
- package/dist/alert.js.map +1 -0
- package/dist/api.d.ts +103 -0
- package/dist/api.js +171 -0
- package/dist/api.js.map +1 -0
- package/dist/commands/analyze.d.ts +61 -0
- package/dist/commands/analyze.js +198 -0
- package/dist/commands/analyze.js.map +1 -0
- package/dist/commands/check.d.ts +55 -0
- package/dist/commands/check.js +180 -0
- package/dist/commands/check.js.map +1 -0
- package/dist/commands/commit.d.ts +26 -0
- package/dist/commands/commit.js +130 -0
- package/dist/commands/commit.js.map +1 -0
- package/dist/commands/health.d.ts +47 -0
- package/dist/commands/health.js +366 -0
- package/dist/commands/health.js.map +1 -0
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.js +173 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/logout.d.ts +1 -0
- package/dist/commands/logout.js +30 -0
- package/dist/commands/logout.js.map +1 -0
- package/dist/commands/notes.d.ts +11 -0
- package/dist/commands/notes.js +49 -0
- package/dist/commands/notes.js.map +1 -0
- package/dist/commands/scan.d.ts +47 -0
- package/dist/commands/scan.js +199 -0
- package/dist/commands/scan.js.map +1 -0
- package/dist/commands/settings.d.ts +15 -0
- package/dist/commands/settings.js +94 -0
- package/dist/commands/settings.js.map +1 -0
- package/dist/commands/status.d.ts +61 -0
- package/dist/commands/status.js +151 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/upload.d.ts +42 -0
- package/dist/commands/upload.js +429 -0
- package/dist/commands/upload.js.map +1 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.js +32 -0
- package/dist/config.js.map +1 -0
- package/dist/detectors.d.ts +43 -0
- package/dist/detectors.js +1026 -0
- package/dist/detectors.js.map +1 -0
- package/dist/git.d.ts +22 -0
- package/dist/git.js +68 -0
- package/dist/git.js.map +1 -0
- package/dist/githook.d.ts +71 -0
- package/dist/githook.js +238 -0
- package/dist/githook.js.map +1 -0
- package/dist/hook.d.ts +39 -0
- package/dist/hook.js +173 -0
- package/dist/hook.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +205 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +19 -0
- package/dist/log.js +97 -0
- package/dist/log.js.map +1 -0
- package/dist/mask-paths.d.ts +75 -0
- package/dist/mask-paths.js +364 -0
- package/dist/mask-paths.js.map +1 -0
- package/dist/pairing.d.ts +60 -0
- package/dist/pairing.js +137 -0
- package/dist/pairing.js.map +1 -0
- package/dist/parser.d.ts +48 -0
- package/dist/parser.js +516 -0
- package/dist/parser.js.map +1 -0
- package/dist/paths.d.ts +41 -0
- package/dist/paths.js +181 -0
- package/dist/paths.js.map +1 -0
- package/dist/plan.d.ts +25 -0
- package/dist/plan.js +39 -0
- package/dist/plan.js.map +1 -0
- package/dist/queue.d.ts +26 -0
- package/dist/queue.js +120 -0
- package/dist/queue.js.map +1 -0
- package/dist/redact.d.ts +29 -0
- package/dist/redact.js +254 -0
- package/dist/redact.js.map +1 -0
- package/dist/spawn.d.ts +13 -0
- package/dist/spawn.js +32 -0
- package/dist/spawn.js.map +1 -0
- package/dist/stage0.d.ts +20 -0
- package/dist/stage0.js +90 -0
- package/dist/stage0.js.map +1 -0
- package/dist/state.d.ts +84 -0
- package/dist/state.js +150 -0
- package/dist/state.js.map +1 -0
- package/dist/test/analyze.test.d.ts +1 -0
- package/dist/test/analyze.test.js +408 -0
- package/dist/test/analyze.test.js.map +1 -0
- package/dist/test/check.test.d.ts +1 -0
- package/dist/test/check.test.js +193 -0
- package/dist/test/check.test.js.map +1 -0
- package/dist/test/cli.test.d.ts +1 -0
- package/dist/test/cli.test.js +132 -0
- package/dist/test/cli.test.js.map +1 -0
- package/dist/test/codex.test.d.ts +1 -0
- package/dist/test/codex.test.js +585 -0
- package/dist/test/codex.test.js.map +1 -0
- package/dist/test/commit.test.d.ts +1 -0
- package/dist/test/commit.test.js +430 -0
- package/dist/test/commit.test.js.map +1 -0
- package/dist/test/git.test.d.ts +1 -0
- package/dist/test/git.test.js +75 -0
- package/dist/test/git.test.js.map +1 -0
- package/dist/test/health.test.d.ts +1 -0
- package/dist/test/health.test.js +156 -0
- package/dist/test/health.test.js.map +1 -0
- package/dist/test/hook.test.d.ts +1 -0
- package/dist/test/hook.test.js +144 -0
- package/dist/test/hook.test.js.map +1 -0
- package/dist/test/kimi.test.d.ts +1 -0
- package/dist/test/kimi.test.js +349 -0
- package/dist/test/kimi.test.js.map +1 -0
- package/dist/test/mask-paths.test.d.ts +1 -0
- package/dist/test/mask-paths.test.js +230 -0
- package/dist/test/mask-paths.test.js.map +1 -0
- package/dist/test/notes.test.d.ts +1 -0
- package/dist/test/notes.test.js +41 -0
- package/dist/test/notes.test.js.map +1 -0
- package/dist/test/pairing.test.d.ts +1 -0
- package/dist/test/pairing.test.js +138 -0
- package/dist/test/pairing.test.js.map +1 -0
- package/dist/test/parser.test.d.ts +1 -0
- package/dist/test/parser.test.js +312 -0
- package/dist/test/parser.test.js.map +1 -0
- package/dist/test/plan.test.d.ts +1 -0
- package/dist/test/plan.test.js +34 -0
- package/dist/test/plan.test.js.map +1 -0
- package/dist/test/redact.test.d.ts +1 -0
- package/dist/test/redact.test.js +303 -0
- package/dist/test/redact.test.js.map +1 -0
- package/dist/test/settings.test.d.ts +1 -0
- package/dist/test/settings.test.js +32 -0
- package/dist/test/settings.test.js.map +1 -0
- package/dist/test/stage0.test.d.ts +1 -0
- package/dist/test/stage0.test.js +607 -0
- package/dist/test/stage0.test.js.map +1 -0
- package/dist/test/sweep.test.d.ts +1 -0
- package/dist/test/sweep.test.js +298 -0
- package/dist/test/sweep.test.js.map +1 -0
- package/dist/test/update.test.d.ts +1 -0
- package/dist/test/update.test.js +26 -0
- package/dist/test/update.test.js.map +1 -0
- package/dist/test/upload.test.d.ts +1 -0
- package/dist/test/upload.test.js +331 -0
- package/dist/test/upload.test.js.map +1 -0
- package/dist/types.d.ts +142 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/update.d.ts +17 -0
- package/dist/update.js +46 -0
- package/dist/update.js.map +1 -0
- package/package.json +60 -0
- package/src/agents/claude.ts +89 -0
- package/src/agents/codex-records.ts +533 -0
- package/src/agents/codex.ts +238 -0
- package/src/agents/index.ts +58 -0
- package/src/agents/kimi-records.ts +274 -0
- package/src/agents/kimi-wire.ts +179 -0
- package/src/agents/kimi.ts +390 -0
- package/src/agents/types.ts +92 -0
- package/src/alert.ts +68 -0
- package/src/api.ts +269 -0
- package/src/commands/analyze.ts +308 -0
- package/src/commands/check.ts +239 -0
- package/src/commands/commit.ts +179 -0
- package/src/commands/health.ts +444 -0
- package/src/commands/init.ts +186 -0
- package/src/commands/logout.ts +33 -0
- package/src/commands/notes.ts +57 -0
- package/src/commands/scan.ts +278 -0
- package/src/commands/settings.ts +107 -0
- package/src/commands/status.ts +236 -0
- package/src/commands/upload.ts +524 -0
- package/src/config.ts +33 -0
- package/src/detectors.ts +1138 -0
- package/src/git.ts +86 -0
- package/src/githook.ts +269 -0
- package/src/hook.ts +237 -0
- package/src/index.ts +229 -0
- package/src/log.ts +113 -0
- package/src/mask-paths.ts +434 -0
- package/src/pairing.ts +190 -0
- package/src/parser.ts +581 -0
- package/src/paths.ts +191 -0
- package/src/plan.ts +56 -0
- package/src/queue.ts +145 -0
- package/src/redact.ts +335 -0
- package/src/spawn.ts +33 -0
- package/src/stage0.ts +121 -0
- package/src/state.ts +208 -0
- package/src/types.ts +165 -0
- package/src/update.ts +46 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { access } from "node:fs/promises";
|
|
2
|
+
import { withParkedAlerts } from "../alert.js";
|
|
3
|
+
import { ISY_HOOKS, installHook, installedHooks, removeHook, settingsLocation } from "../hook.js";
|
|
4
|
+
import { parseTranscriptFile } from "../parser.js";
|
|
5
|
+
import {
|
|
6
|
+
allClaudeSessions,
|
|
7
|
+
claudeConfigDir,
|
|
8
|
+
claudeProjectsDir,
|
|
9
|
+
findSessions,
|
|
10
|
+
firstCwd,
|
|
11
|
+
projectDir,
|
|
12
|
+
} from "../paths.js";
|
|
13
|
+
import { redactTranscriptFile } from "../redact.js";
|
|
14
|
+
import type { Agent, AgentHook, HookInput } from "./types.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Claude Code. This is a thin front onto the code that already shipped —
|
|
18
|
+
* hook.ts, paths.ts and redact.ts are unchanged and still carry their own tests.
|
|
19
|
+
*/
|
|
20
|
+
export const claudeAgent: Agent = {
|
|
21
|
+
id: "claude",
|
|
22
|
+
label: "Claude Code",
|
|
23
|
+
|
|
24
|
+
async present(): Promise<boolean> {
|
|
25
|
+
try {
|
|
26
|
+
await access(claudeConfigDir());
|
|
27
|
+
return true;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
configLocation(): string {
|
|
34
|
+
return settingsLocation();
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
hooks(): AgentHook[] {
|
|
38
|
+
return ISY_HOOKS;
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
transcriptDir(cwd: string): string {
|
|
42
|
+
return projectDir(cwd);
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
transcriptRoot: claudeProjectsDir,
|
|
46
|
+
|
|
47
|
+
sessionsIn(cwd: string) {
|
|
48
|
+
return findSessions(cwd);
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
allSessions: allClaudeSessions,
|
|
52
|
+
|
|
53
|
+
cwdOf(path: string) {
|
|
54
|
+
return firstCwd(path);
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
parsedSession(path: string) {
|
|
58
|
+
return parseTranscriptFile(path);
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
async transcriptFor(hook: HookInput | undefined, cwd: string): Promise<string | undefined> {
|
|
62
|
+
return hook?.transcript_path ?? (await findSessions(cwd))[0]?.path;
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
async redactedLines(
|
|
66
|
+
path: string,
|
|
67
|
+
options: { extraPatterns?: readonly string[]; cwd?: string },
|
|
68
|
+
): Promise<string[]> {
|
|
69
|
+
// Streams the file rather than loading it: real transcripts reach tens of MB.
|
|
70
|
+
const { lines } = await redactTranscriptFile(path, {
|
|
71
|
+
extraPatterns: options.extraPatterns,
|
|
72
|
+
cwd: options.cwd,
|
|
73
|
+
});
|
|
74
|
+
return lines;
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
hooksInstalled: installedHooks,
|
|
78
|
+
installHooks: installHook,
|
|
79
|
+
removeHooks: removeHook,
|
|
80
|
+
|
|
81
|
+
// Claude Code renders `systemMessage` from a hook's stdout for the user. At
|
|
82
|
+
// SessionStart that stdout is also the only route a line parked by unattended
|
|
83
|
+
// work has — a sweep spawned with its output going nowhere, an alert raised
|
|
84
|
+
// where another CLI had no terminal — so the backlog goes out ahead of it.
|
|
85
|
+
async deliver(message: string, event?: string): Promise<void> {
|
|
86
|
+
const text = event === "SessionStart" ? await withParkedAlerts(message) : message;
|
|
87
|
+
console.log(JSON.stringify({ systemMessage: text }));
|
|
88
|
+
},
|
|
89
|
+
};
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codex CLI keeps a session as a `rollout-*.jsonl` event log: one JSON object
|
|
3
|
+
* per line, `{timestamp, type, payload}`. The conversation lives in
|
|
4
|
+
* `type: "response_item"` lines; everything else is bookkeeping.
|
|
5
|
+
*
|
|
6
|
+
* Output is Claude-Code-record-shaped JSONL, because the server re-parses the
|
|
7
|
+
* uploaded transcript with the same parseLines this package exports. One shape
|
|
8
|
+
* downstream, one adapter here.
|
|
9
|
+
*
|
|
10
|
+
* Everything here is deliberately tolerant: an unrecognised shape yields no
|
|
11
|
+
* record rather than a wrong one.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Codex runs shell commands under several tool names across builds. */
|
|
15
|
+
const SHELL_TOOLS = new Set(["exec_command", "shell", "shell_command", "local_shell_call"]);
|
|
16
|
+
|
|
17
|
+
/** A call into the `exec` harness's own tools, inside the JS snippet Codex runs. */
|
|
18
|
+
const HARNESS_CALL = /tools\.(exec_command|apply_patch)\s*\(/g;
|
|
19
|
+
/** `({cmd:"…"` — bare or quoted key — and nothing glued onto the literal after it. */
|
|
20
|
+
const CMD_ARGUMENT = /^\s*\{[^}]*?(?:\bcmd|"cmd")\s*:\s*("(?:[^"\\]|\\.)*")\s*[,}]/s;
|
|
21
|
+
/** `("*** Begin Patch…")` — one string literal and nothing glued onto it. */
|
|
22
|
+
const PATCH_ARGUMENT = /^\s*("(?:[^"\\]|\\.)*")\s*[,)]/s;
|
|
23
|
+
/** Any call into the harness, to tell a script that only looks from one that may write. */
|
|
24
|
+
const ANY_HARNESS_CALL = /tools\.([A-Za-z0-9_]+)\s*\(/g;
|
|
25
|
+
/** Harness tools that fetch or look and never touch the working tree. */
|
|
26
|
+
const READ_ONLY_HARNESS = new Set(["web__run", "view_image", "image_gen__imagegen"]);
|
|
27
|
+
/** An `exec` script that outran its yield; a later `wait` names this cell. */
|
|
28
|
+
const RUNNING_CELL = /^Script running with cell ID (\S+)/;
|
|
29
|
+
/** A command that outran its yield leaves its shell session's id in the result. */
|
|
30
|
+
const EXEC_SESSION = /"session_id"\s*:\s*(\d+)/g;
|
|
31
|
+
|
|
32
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
33
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function asString(value: unknown): string | undefined {
|
|
37
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A Codex rollout opens with its session_meta line. */
|
|
41
|
+
export function isCodexTranscript(firstLine: string): boolean {
|
|
42
|
+
try {
|
|
43
|
+
const parsed: unknown = JSON.parse(firstLine);
|
|
44
|
+
return isObject(parsed) && parsed.type === "session_meta";
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Text out of a content array of `{type, text}` parts, or a plain string. */
|
|
51
|
+
function contentText(content: unknown): string {
|
|
52
|
+
if (typeof content === "string") return content;
|
|
53
|
+
if (!Array.isArray(content)) return "";
|
|
54
|
+
|
|
55
|
+
const parts: string[] = [];
|
|
56
|
+
for (const part of content) {
|
|
57
|
+
if (typeof part === "string") parts.push(part);
|
|
58
|
+
else if (isObject(part) && typeof part.text === "string") parts.push(part.text);
|
|
59
|
+
}
|
|
60
|
+
return parts.join("\n");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Reasoning Codex actually recorded in the clear. `encrypted_content` is an
|
|
65
|
+
* opaque blob — megabytes of it per session — and is deliberately dropped:
|
|
66
|
+
* it cannot be analysed and must never reach the server.
|
|
67
|
+
*/
|
|
68
|
+
function reasoningText(payload: Record<string, unknown>): string | undefined {
|
|
69
|
+
const summary = contentText(payload.summary);
|
|
70
|
+
const content = contentText(payload.content);
|
|
71
|
+
const text = [summary, content].filter((part) => part.length > 0).join("\n");
|
|
72
|
+
return text.length > 0 ? text : undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface PatchEdit {
|
|
76
|
+
tool: string;
|
|
77
|
+
input: Record<string, unknown>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface Hunk {
|
|
81
|
+
old_string: string;
|
|
82
|
+
new_string: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* One `apply_patch` envelope as Claude-shaped edit tool calls.
|
|
87
|
+
*
|
|
88
|
+
* A patch can touch several files, so this returns one call per file:
|
|
89
|
+
* `MultiEdit` carries every hunk of an updated file as an `edits` array, which
|
|
90
|
+
* is the shape parser.ts already unpacks into per-hunk FileEdits.
|
|
91
|
+
*/
|
|
92
|
+
export function parseApplyPatch(patch: string): PatchEdit[] {
|
|
93
|
+
const edits: PatchEdit[] = [];
|
|
94
|
+
|
|
95
|
+
/** The file section being read: an update collecting hunks, or a new file. */
|
|
96
|
+
let update: { path: string; hunks: Hunk[]; open?: { old: string[]; new: string[] } } | undefined;
|
|
97
|
+
let added: { path: string; body: string[] } | undefined;
|
|
98
|
+
|
|
99
|
+
const closeHunk = (): void => {
|
|
100
|
+
if (!update?.open) return;
|
|
101
|
+
// A section with no `+`/`-` at all is context Codex emitted for position only.
|
|
102
|
+
if (update.open.old.length > 0 || update.open.new.length > 0) {
|
|
103
|
+
update.hunks.push({
|
|
104
|
+
old_string: update.open.old.join("\n"),
|
|
105
|
+
new_string: update.open.new.join("\n"),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
update.open = undefined;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const closeFile = (): void => {
|
|
112
|
+
closeHunk();
|
|
113
|
+
|
|
114
|
+
if (update) {
|
|
115
|
+
if (update.hunks.length > 0) {
|
|
116
|
+
edits.push({ tool: "MultiEdit", input: { file_path: update.path, edits: update.hunks } });
|
|
117
|
+
} else {
|
|
118
|
+
// A file named by the patch with nothing parsed under it: still an edit,
|
|
119
|
+
// just one whose before and after we could not recover.
|
|
120
|
+
edits.push({ tool: "Edit", input: { file_path: update.path } });
|
|
121
|
+
}
|
|
122
|
+
update = undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (added) {
|
|
126
|
+
edits.push({ tool: "Write", input: { file_path: added.path, content: added.body.join("\n") } });
|
|
127
|
+
added = undefined;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
for (const line of patch.split("\n")) {
|
|
132
|
+
const updating = /^\*\*\* (?:Update|Move) File: (.+)$/.exec(line);
|
|
133
|
+
if (updating) {
|
|
134
|
+
closeFile();
|
|
135
|
+
update = { path: updating[1]!.trim(), hunks: [] };
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const adding = /^\*\*\* Add File: (.+)$/.exec(line);
|
|
140
|
+
if (adding) {
|
|
141
|
+
closeFile();
|
|
142
|
+
added = { path: adding[1]!.trim(), body: [] };
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const deleting = /^\*\*\* Delete File: (.+)$/.exec(line);
|
|
147
|
+
if (deleting) {
|
|
148
|
+
closeFile();
|
|
149
|
+
// Nothing records what the file held, so this is an edit with no diff.
|
|
150
|
+
edits.push({ tool: "Edit", input: { file_path: deleting[1]!.trim() } });
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (/^\*\*\* (?:Begin|End) Patch\s*$/.test(line)) {
|
|
155
|
+
closeFile();
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (added) {
|
|
160
|
+
if (line.startsWith("+")) added.body.push(line.slice(1));
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!update) continue;
|
|
165
|
+
|
|
166
|
+
if (line.startsWith("@@")) {
|
|
167
|
+
closeHunk();
|
|
168
|
+
update.open = { old: [], new: [] };
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
update.open ??= { old: [], new: [] };
|
|
173
|
+
if (line.startsWith("-")) update.open.old.push(line.slice(1));
|
|
174
|
+
else if (line.startsWith("+")) update.open.new.push(line.slice(1));
|
|
175
|
+
else if (line.startsWith(" ")) {
|
|
176
|
+
// Context belongs to both sides, so old_string and new_string stay
|
|
177
|
+
// anchored the way an Edit's own strings are.
|
|
178
|
+
update.open.old.push(line.slice(1));
|
|
179
|
+
update.open.new.push(line.slice(1));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
closeFile();
|
|
184
|
+
return edits;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
/** A JS string literal as its text, when it is also valid JSON. */
|
|
189
|
+
function literalText(literal: string): string | undefined {
|
|
190
|
+
try {
|
|
191
|
+
const text: unknown = JSON.parse(literal);
|
|
192
|
+
return typeof text === "string" ? text : undefined;
|
|
193
|
+
} catch {
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Every tool call an `exec` harness script makes, in order: each
|
|
200
|
+
* `tools.exec_command` as a Bash call and each `tools.apply_patch` as the edits
|
|
201
|
+
* it applies. Newer Codex builds route nearly everything through this harness,
|
|
202
|
+
* so reading only its first command left their edits invisible to stage 0.
|
|
203
|
+
*
|
|
204
|
+
* `opaque` says a call was there whose argument is not one plain literal —
|
|
205
|
+
* `{cmd: "sed -n '" + range + "p' a.ts"}`, a variable, a patch that parsed to
|
|
206
|
+
* nothing. Half a command would lie to the shell detectors, so such a call is
|
|
207
|
+
* left out, and the script keeps its own name beside what was read.
|
|
208
|
+
*
|
|
209
|
+
* ponytail: literal arguments only; reading the rest means running the snippet.
|
|
210
|
+
*/
|
|
211
|
+
function harnessCalls(script: string): { calls: PatchEdit[]; opaque: boolean } {
|
|
212
|
+
const calls: PatchEdit[] = [];
|
|
213
|
+
let opaque = false;
|
|
214
|
+
|
|
215
|
+
for (const match of script.matchAll(HARNESS_CALL)) {
|
|
216
|
+
const rest = script.slice(match.index + match[0].length);
|
|
217
|
+
const shell = match[1] === "exec_command";
|
|
218
|
+
const literal = (shell ? CMD_ARGUMENT : PATCH_ARGUMENT).exec(rest)?.[1];
|
|
219
|
+
const text = literal === undefined ? undefined : literalText(literal);
|
|
220
|
+
const read: PatchEdit[] =
|
|
221
|
+
text === undefined ? [] : shell ? [{ tool: "Bash", input: { command: text } }] : parseApplyPatch(text);
|
|
222
|
+
|
|
223
|
+
if (read.length === 0) opaque = true;
|
|
224
|
+
calls.push(...read);
|
|
225
|
+
}
|
|
226
|
+
return { calls, opaque };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The shell session a script does nothing but poll: one `tools.write_stdin`
|
|
231
|
+
* that types nothing. It is how Codex waits on a command that outran its
|
|
232
|
+
* yield, as `wait` is for a script. Typing into the shell is an act of its own,
|
|
233
|
+
* and stays a tool.
|
|
234
|
+
*/
|
|
235
|
+
function polledSession(script: string): string | undefined {
|
|
236
|
+
const used = [...script.matchAll(ANY_HARNESS_CALL)];
|
|
237
|
+
if (used.length !== 1 || used[0]![1] !== "write_stdin") return undefined;
|
|
238
|
+
if (!/\bchars"?\s*:\s*""/.test(script)) return undefined;
|
|
239
|
+
return /\bsession_id"?\s*:\s*(\d+)/.exec(script)?.[1];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** JSON arguments, whether Codex sent an object or a serialised string. */
|
|
243
|
+
function toolArguments(payload: Record<string, unknown>): Record<string, unknown> {
|
|
244
|
+
const raw = payload.arguments ?? payload.input;
|
|
245
|
+
if (isObject(raw)) return raw;
|
|
246
|
+
if (typeof raw !== "string") return {};
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
const parsed: unknown = JSON.parse(raw);
|
|
250
|
+
return isObject(parsed) ? parsed : {};
|
|
251
|
+
} catch {
|
|
252
|
+
// A truncated or non-JSON argument string: keep the call, lose the
|
|
253
|
+
// arguments. Better than dropping the tool use entirely.
|
|
254
|
+
return {};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* An argv array as a command line. A `sh -c` wrapper unwraps to its script;
|
|
260
|
+
* anything else is the command itself, joined whole.
|
|
261
|
+
*/
|
|
262
|
+
function argvCommand(argv: unknown[]): string | undefined {
|
|
263
|
+
const parts = argv.filter((part): part is string => typeof part === "string");
|
|
264
|
+
if (parts.length === 0) return undefined;
|
|
265
|
+
// `["bash", "-lc", "<script>"]` — the script is the command that ran.
|
|
266
|
+
const shellish = /(?:^|\/)(?:ba|z)?sh$/.test(parts[0]!);
|
|
267
|
+
const flag = shellish ? parts.findIndex((part, i) => i > 0 && /^-[a-z]*c/.test(part)) : -1;
|
|
268
|
+
if (flag >= 0) return flag + 1 < parts.length ? parts[flag + 1] : undefined;
|
|
269
|
+
return parts.join(" ");
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The shell command behind a Codex tool call, whatever wrapper it arrived in. */
|
|
273
|
+
function shellCommand(name: string, payload: Record<string, unknown>): string | undefined {
|
|
274
|
+
if (SHELL_TOOLS.has(name)) {
|
|
275
|
+
const args = toolArguments(payload);
|
|
276
|
+
const direct = asString(args.cmd) ?? asString(args.command);
|
|
277
|
+
if (direct) return direct;
|
|
278
|
+
// Older builds send an argv array, as the Responses API shell tool does;
|
|
279
|
+
// `local_shell_call` keeps it under `action.command` instead.
|
|
280
|
+
const argv = Array.isArray(args.command)
|
|
281
|
+
? args.command
|
|
282
|
+
: isObject(payload.action) && Array.isArray(payload.action.command)
|
|
283
|
+
? payload.action.command
|
|
284
|
+
: undefined;
|
|
285
|
+
return argv ? argvCommand(argv) : undefined;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return undefined;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Tool_use blocks for calls that share one Codex call id. Only the block at
|
|
293
|
+
* `owner` keeps the bare id, so the tool result still lands on it; the rest
|
|
294
|
+
* are suffixed to stay unique.
|
|
295
|
+
*/
|
|
296
|
+
function sharedIdBlocks(calls: PatchEdit[], callId: string, owner: number): Record<string, unknown>[] {
|
|
297
|
+
return calls.map((call, index) => ({
|
|
298
|
+
type: "tool_use",
|
|
299
|
+
id: index === owner ? callId : `${callId}#${index}`,
|
|
300
|
+
name: call.tool,
|
|
301
|
+
input: call.input,
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Claude-shaped tool_use blocks for one Codex tool call. */
|
|
306
|
+
function toolUseBlocks(payload: Record<string, unknown>): Record<string, unknown>[] {
|
|
307
|
+
// A `local_shell_call` has no `name` — its type is the tool's name.
|
|
308
|
+
const name = asString(payload.name) ?? (payload.type === "local_shell_call" ? "local_shell_call" : undefined);
|
|
309
|
+
const callId = asString(payload.call_id) ?? asString(payload.id);
|
|
310
|
+
if (!name || !callId) return [];
|
|
311
|
+
|
|
312
|
+
if (name === "exec") {
|
|
313
|
+
const script = asString(payload.input) ?? "";
|
|
314
|
+
// A script that only searches the web or looks at an image writes nothing,
|
|
315
|
+
// so it keeps the harness tool's own name, which parser.ts knows is no edit.
|
|
316
|
+
const used = [...script.matchAll(ANY_HARNESS_CALL)].map((match) => match[1]!);
|
|
317
|
+
if (used.length > 0 && used.every((tool) => READ_ONLY_HARNESS.has(tool))) {
|
|
318
|
+
const looks = used.map((tool, index) => ({ tool, input: index === 0 ? { script } : {} }));
|
|
319
|
+
return sharedIdBlocks(looks, callId, 0);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const { calls, opaque } = harnessCalls(script);
|
|
323
|
+
// Nothing read, or not all of it: the harness stays in view under its own
|
|
324
|
+
// name, which parser.ts counts as a tool that may have edited a file.
|
|
325
|
+
if (calls.length === 0 || opaque) calls.push({ tool: "exec", input: { script: payload.input } });
|
|
326
|
+
// One result comes back for the whole script, and it goes to the first
|
|
327
|
+
// command: the failure detectors read Bash results, an edit has none.
|
|
328
|
+
// ponytail: a script running several commands pins all their output on the
|
|
329
|
+
// first; splitting it needs the harness's `text()` parts matched to calls.
|
|
330
|
+
const firstCommand = calls.findIndex((call) => call.tool === "Bash");
|
|
331
|
+
return sharedIdBlocks(calls, callId, Math.max(firstCommand, 0));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const command = shellCommand(name, payload);
|
|
335
|
+
if (command !== undefined) {
|
|
336
|
+
return [{ type: "tool_use", id: callId, name: "Bash", input: { command } }];
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (name === "apply_patch") {
|
|
340
|
+
const patch = asString(payload.input) ?? asString(toolArguments(payload).input);
|
|
341
|
+
const edits = patch ? parseApplyPatch(patch) : [];
|
|
342
|
+
if (edits.length > 0) return sharedIdBlocks(edits, callId, 0);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Anything else — update_plan, MCP calls — is kept under its own name so it
|
|
346
|
+
// still counts as a tool use.
|
|
347
|
+
return [{ type: "tool_use", id: callId, name, input: toolArguments(payload) }];
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** The text of a tool result, whichever output shape Codex used. */
|
|
351
|
+
function outputText(payload: Record<string, unknown>): string {
|
|
352
|
+
const output = payload.output;
|
|
353
|
+
if (typeof output === "string") return output;
|
|
354
|
+
return contentText(output);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Codex exec output carries its exit code — as "Process exited with code N" in
|
|
359
|
+
* plain text, as `metadata.exit_code` in a JSON envelope, or as `exit_code` in
|
|
360
|
+
* each result object an `exec` script prints under its plain-text header, so
|
|
361
|
+
* the output never parses whole. A non-zero code is what Claude records as
|
|
362
|
+
* `is_error`, and the failure detectors key off it; any failing command fails
|
|
363
|
+
* the call. An `exit_code` quoted inside an output string is escaped, and does
|
|
364
|
+
* not match.
|
|
365
|
+
*/
|
|
366
|
+
function execFailed(text: string): boolean {
|
|
367
|
+
const exited = /Process exited with code (\d+)/.exec(text);
|
|
368
|
+
if (exited) return exited[1] !== "0";
|
|
369
|
+
return [...text.matchAll(/"exit_code"\s*:\s*(-?\d+)/g)].some((code) => code[1] !== "0");
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export interface CodexOptions {
|
|
373
|
+
sessionId?: string;
|
|
374
|
+
cwd?: string;
|
|
375
|
+
version?: string;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** Turn one Codex rollout into Claude-shaped transcript records. */
|
|
379
|
+
export function toClaudeRecords(rollout: Iterable<string>, options: CodexOptions = {}): string[] {
|
|
380
|
+
const out: string[] = [];
|
|
381
|
+
let index = 0;
|
|
382
|
+
let parent: string | null = null;
|
|
383
|
+
|
|
384
|
+
let sessionId = options.sessionId;
|
|
385
|
+
let cwd = options.cwd;
|
|
386
|
+
let version = options.version;
|
|
387
|
+
|
|
388
|
+
/** Call ids of `exec` scripts: only their output can name a running cell. */
|
|
389
|
+
const scripts = new Set<string>();
|
|
390
|
+
/** A script that outran its yield, by cell id, to the call id that started it. */
|
|
391
|
+
const cells = new Map<string, string>();
|
|
392
|
+
/** A `wait` on a known cell, to that script's call id. */
|
|
393
|
+
const waits = new Map<string, string>();
|
|
394
|
+
/** A shell session still running, by its id, to the script that started it. */
|
|
395
|
+
const shells = new Map<string, string>();
|
|
396
|
+
|
|
397
|
+
const emit = (record: Record<string, unknown>): void => {
|
|
398
|
+
const uuid = `codex-${index}`;
|
|
399
|
+
out.push(
|
|
400
|
+
JSON.stringify({
|
|
401
|
+
...record,
|
|
402
|
+
uuid,
|
|
403
|
+
parentUuid: parent,
|
|
404
|
+
sessionId,
|
|
405
|
+
cwd,
|
|
406
|
+
version: version ?? "codex",
|
|
407
|
+
isSidechain: false,
|
|
408
|
+
}),
|
|
409
|
+
);
|
|
410
|
+
parent = uuid;
|
|
411
|
+
index += 1;
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
for (const line of rollout) {
|
|
415
|
+
const trimmed = line.trim();
|
|
416
|
+
if (trimmed.length === 0) continue;
|
|
417
|
+
|
|
418
|
+
let parsed: unknown;
|
|
419
|
+
try {
|
|
420
|
+
parsed = JSON.parse(trimmed);
|
|
421
|
+
} catch {
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
if (!isObject(parsed)) continue;
|
|
425
|
+
|
|
426
|
+
const timestamp = asString(parsed.timestamp);
|
|
427
|
+
const payload = isObject(parsed.payload) ? parsed.payload : undefined;
|
|
428
|
+
if (!payload) continue;
|
|
429
|
+
|
|
430
|
+
if (parsed.type === "session_meta") {
|
|
431
|
+
// `base_instructions` is the frozen system prompt: never ours to analyse
|
|
432
|
+
// and never worth uploading.
|
|
433
|
+
sessionId ??= asString(payload.session_id) ?? asString(payload.id);
|
|
434
|
+
cwd ??= asString(payload.cwd);
|
|
435
|
+
version ??= asString(payload.cli_version);
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// `event_msg` restates what `response_item` already records, and
|
|
440
|
+
// `turn_context` / `world_state` are bookkeeping.
|
|
441
|
+
if (parsed.type !== "response_item") continue;
|
|
442
|
+
|
|
443
|
+
const kind = asString(payload.type);
|
|
444
|
+
|
|
445
|
+
if (kind === "message") {
|
|
446
|
+
const role = asString(payload.role);
|
|
447
|
+
// `developer` messages are harness-injected context, not the user's work.
|
|
448
|
+
if (role !== "user" && role !== "assistant") continue;
|
|
449
|
+
|
|
450
|
+
const text = contentText(payload.content);
|
|
451
|
+
if (!text) continue;
|
|
452
|
+
|
|
453
|
+
emit({
|
|
454
|
+
type: role,
|
|
455
|
+
timestamp,
|
|
456
|
+
message: { role, content: [{ type: "text", text }] },
|
|
457
|
+
});
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (kind === "reasoning") {
|
|
462
|
+
const thinking = reasoningText(payload);
|
|
463
|
+
if (!thinking) continue;
|
|
464
|
+
emit({
|
|
465
|
+
type: "assistant",
|
|
466
|
+
timestamp,
|
|
467
|
+
message: { role: "assistant", content: [{ type: "thinking", thinking }] },
|
|
468
|
+
});
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (kind === "function_call" || kind === "custom_tool_call" || kind === "local_shell_call") {
|
|
473
|
+
const callId = asString(payload.call_id);
|
|
474
|
+
if (callId && payload.name === "exec") {
|
|
475
|
+
// A poll on a shell this rollout started folds into the script that
|
|
476
|
+
// started it, for the same reason a `wait` does below.
|
|
477
|
+
const polled = shells.get(polledSession(asString(payload.input) ?? "") ?? "");
|
|
478
|
+
if (polled) {
|
|
479
|
+
waits.set(callId, polled);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
scripts.add(callId);
|
|
483
|
+
}
|
|
484
|
+
// `wait` polls a script that outran its yield. The script is the tool use
|
|
485
|
+
// and the wait only how its result arrived, so the result goes back to the
|
|
486
|
+
// script, where the failure detectors read its exit code. A wait on a cell
|
|
487
|
+
// this rollout never started stays a tool of its own.
|
|
488
|
+
if (callId && payload.name === "wait") {
|
|
489
|
+
const script = cells.get(String(toolArguments(payload).cell_id));
|
|
490
|
+
if (script) {
|
|
491
|
+
waits.set(callId, script);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const blocks = toolUseBlocks(payload);
|
|
496
|
+
if (blocks.length === 0) continue;
|
|
497
|
+
emit({ type: "assistant", timestamp, message: { role: "assistant", content: blocks } });
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (kind === "function_call_output" || kind === "custom_tool_call_output") {
|
|
502
|
+
const callId = asString(payload.call_id);
|
|
503
|
+
if (!callId) continue;
|
|
504
|
+
const text = outputText(payload);
|
|
505
|
+
const failed = execFailed(text);
|
|
506
|
+
|
|
507
|
+
const running = scripts.has(callId) ? RUNNING_CELL.exec(text) : null;
|
|
508
|
+
if (running) cells.set(running[1]!, callId);
|
|
509
|
+
if (scripts.has(callId)) {
|
|
510
|
+
for (const shell of text.matchAll(EXEC_SESSION)) if (!shells.has(shell[1]!)) shells.set(shell[1]!, callId);
|
|
511
|
+
}
|
|
512
|
+
// Each poll replaces the result before it — the parser keeps a tool's last
|
|
513
|
+
// one — so the script ends up carrying its final exit code.
|
|
514
|
+
// ponytail: earlier polls' output is replaced, not joined; join them when
|
|
515
|
+
// a detector needs the whole output of a long script.
|
|
516
|
+
const id = waits.get(callId) ?? callId;
|
|
517
|
+
|
|
518
|
+
emit({
|
|
519
|
+
type: "user",
|
|
520
|
+
timestamp,
|
|
521
|
+
message: {
|
|
522
|
+
role: "user",
|
|
523
|
+
content: [
|
|
524
|
+
{ type: "tool_result", tool_use_id: id, content: text, ...(failed ? { is_error: true } : {}) },
|
|
525
|
+
],
|
|
526
|
+
},
|
|
527
|
+
toolUseResult: { stdout: text },
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return out;
|
|
533
|
+
}
|