t3code-session-manager 0.0.0-stage → 0.4.1
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/.claude-plugin/marketplace.json +21 -0
- package/.claude-plugin/plugin.json +19 -0
- package/CHANGELOG.md +149 -0
- package/LICENSE +21 -0
- package/README.md +661 -2
- package/README.pt.md +617 -0
- package/bin/entry.d.mts +2 -0
- package/bin/entry.mjs +19 -0
- package/bin/t3sm.mjs +44 -0
- package/commands/import.md +30 -0
- package/commands/organize.md +36 -0
- package/commands/status.md +24 -0
- package/commands/sync.md +26 -0
- package/dist/apply/apply.js +993 -0
- package/dist/apply/claudeWriter.js +168 -0
- package/dist/apply/copyTranscript.js +116 -0
- package/dist/apply/importer.js +697 -0
- package/dist/apply/materialize.js +335 -0
- package/dist/apply/waves.js +108 -0
- package/dist/assistant/core.js +295 -0
- package/dist/assistant/deps.js +256 -0
- package/dist/assistant/i18n.js +38 -0
- package/dist/assistant/messages.en.js +494 -0
- package/dist/assistant/messages.pt.js +495 -0
- package/dist/assistant/runner.js +64 -0
- package/dist/assistant/steps.js +1362 -0
- package/dist/build.json +1 -0
- package/dist/cli/applyScreen.js +158 -0
- package/dist/cli/commands/accounts.js +254 -0
- package/dist/cli/commands/apply.js +199 -0
- package/dist/cli/commands/changelog.js +61 -0
- package/dist/cli/commands/doctor.js +105 -0
- package/dist/cli/commands/help.js +158 -0
- package/dist/cli/commands/merge.js +224 -0
- package/dist/cli/commands/organize.js +872 -0
- package/dist/cli/commands/plan.js +161 -0
- package/dist/cli/commands/projects.js +430 -0
- package/dist/cli/commands/quarantine.js +203 -0
- package/dist/cli/commands/recover.js +230 -0
- package/dist/cli/commands/rollback.js +281 -0
- package/dist/cli/commands/scan.js +117 -0
- package/dist/cli/commands/schedule.js +288 -0
- package/dist/cli/commands/start.js +45 -0
- package/dist/cli/commands/status.js +615 -0
- package/dist/cli/commands/sync.js +336 -0
- package/dist/cli/commands/update.js +398 -0
- package/dist/cli/errors.js +53 -0
- package/dist/cli/failures.js +91 -0
- package/dist/cli/flags.js +53 -0
- package/dist/cli/help.en.js +549 -0
- package/dist/cli/help.pt.js +566 -0
- package/dist/cli/helpText.js +211 -0
- package/dist/cli/helpTopics.js +58 -0
- package/dist/cli/locked.js +12 -0
- package/dist/cli/main.js +155 -0
- package/dist/cli/screen.js +3 -0
- package/dist/cli/ui.js +111 -0
- package/dist/cli/words.js +2 -0
- package/dist/core/activity.js +16 -0
- package/dist/core/changelog.js +47 -0
- package/dist/core/config.js +530 -0
- package/dist/core/fsutil.js +171 -0
- package/dist/core/glob.js +34 -0
- package/dist/core/history.js +32 -0
- package/dist/core/journal.js +274 -0
- package/dist/core/lock.js +337 -0
- package/dist/core/mask.js +31 -0
- package/dist/core/paths.js +68 -0
- package/dist/core/pid.js +101 -0
- package/dist/core/run.js +4 -0
- package/dist/core/semver.js +18 -0
- package/dist/core/sysroot.js +29 -0
- package/dist/core/types.js +229 -0
- package/dist/core/uuid.js +29 -0
- package/dist/core/version.js +72 -0
- package/dist/help/convert.md +20 -0
- package/dist/help/convert.pt.md +21 -0
- package/dist/help/cwd-unreadable.md +22 -0
- package/dist/help/cwd-unreadable.pt.md +22 -0
- package/dist/help/decisions.md +20 -0
- package/dist/help/decisions.pt.md +20 -0
- package/dist/help/errors.md +20 -0
- package/dist/help/errors.pt.md +22 -0
- package/dist/help/held.md +20 -0
- package/dist/help/held.pt.md +20 -0
- package/dist/help/history.md +27 -0
- package/dist/help/history.pt.md +29 -0
- package/dist/help/instance-unavailable.md +18 -0
- package/dist/help/instance-unavailable.pt.md +19 -0
- package/dist/help/not-imported.md +19 -0
- package/dist/help/not-imported.pt.md +19 -0
- package/dist/help/orphaned.md +19 -0
- package/dist/help/orphaned.pt.md +19 -0
- package/dist/help/project-mismatch.md +15 -0
- package/dist/help/project-mismatch.pt.md +15 -0
- package/dist/help/t3-native.md +22 -0
- package/dist/help/t3-native.pt.md +22 -0
- package/dist/help/updates.md +26 -0
- package/dist/help/updates.pt.md +28 -0
- package/dist/help/watcher.md +24 -0
- package/dist/help/watcher.pt.md +25 -0
- package/dist/i18n/cli.js +41 -0
- package/dist/i18n/lang.js +55 -0
- package/dist/i18n/messages.en.js +1140 -0
- package/dist/i18n/messages.pt.js +1149 -0
- package/dist/i18n/words.js +12 -0
- package/dist/ops/accounts.js +170 -0
- package/dist/ops/autoUpdate.js +223 -0
- package/dist/ops/discoverAccounts.js +343 -0
- package/dist/ops/doctor.js +811 -0
- package/dist/ops/elevated.js +47 -0
- package/dist/ops/errorCodes.js +39 -0
- package/dist/ops/follow.js +558 -0
- package/dist/ops/merge.js +830 -0
- package/dist/ops/notify.js +249 -0
- package/dist/ops/plugin.js +92 -0
- package/dist/ops/projects.js +113 -0
- package/dist/ops/quarantine.js +415 -0
- package/dist/ops/rollback.js +868 -0
- package/dist/ops/schedule.js +464 -0
- package/dist/ops/successor.js +51 -0
- package/dist/ops/sync.js +859 -0
- package/dist/ops/syncStatus.js +290 -0
- package/dist/ops/update.js +273 -0
- package/dist/ops/updater.js +641 -0
- package/dist/ops/watch.js +676 -0
- package/dist/ops/watcherControl.js +212 -0
- package/dist/ops/watcherHealth.js +75 -0
- package/dist/organize/apply.js +495 -0
- package/dist/organize/history.js +192 -0
- package/dist/organize/plan.js +283 -0
- package/dist/organize/rename.js +58 -0
- package/dist/organize/sessions.js +100 -0
- package/dist/organize/titles.js +70 -0
- package/dist/organize/types.js +73 -0
- package/dist/plan/categories.js +104 -0
- package/dist/plan/dedupe.js +137 -0
- package/dist/plan/folders.js +82 -0
- package/dist/plan/names.js +93 -0
- package/dist/plan/ownership.js +316 -0
- package/dist/plan/planner.js +669 -0
- package/dist/plan/rehome.js +101 -0
- package/dist/plan/report.js +407 -0
- package/dist/sources/cache.js +89 -0
- package/dist/sources/claude.js +297 -0
- package/dist/sources/codex.js +496 -0
- package/dist/sources/flatten.js +23 -0
- package/dist/sources/jsonl.js +169 -0
- package/dist/sources/native.js +233 -0
- package/dist/sources/opencode.js +483 -0
- package/dist/sources/pi.js +133 -0
- package/dist/sources/registry.js +67 -0
- package/dist/sources/types.js +80 -0
- package/dist/t3/api.js +492 -0
- package/dist/t3/auth.js +248 -0
- package/dist/t3/compat.js +62 -0
- package/dist/t3/decode.js +408 -0
- package/dist/t3/errors.js +185 -0
- package/dist/t3/rpc.js +185 -0
- package/dist/t3/runtime.js +80 -0
- package/dist/ui/caps.js +56 -0
- package/dist/ui/components.js +202 -0
- package/dist/ui/layout.js +205 -0
- package/dist/ui/list.js +423 -0
- package/dist/ui/locale.js +73 -0
- package/dist/ui/organizePreview.js +72 -0
- package/dist/ui/progress.js +237 -0
- package/dist/ui/style.js +32 -0
- package/dist/ui/terminal.js +126 -0
- package/dist/ui/text.js +186 -0
- package/package.json +58 -4
- package/skills/t3code-sessions/SKILL.md +183 -0
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
import { open, stat } from "node:fs/promises";
|
|
4
|
+
import { basename, join, resolve } from "node:path";
|
|
5
|
+
import { createZstdDecompress } from "node:zlib";
|
|
6
|
+
|
|
7
|
+
import { isActive } from "../core/activity.js";
|
|
8
|
+
import { createNativeAdapter, } from "./native.js";
|
|
9
|
+
import { LineTooLongError, entryKind, extractText, isoOrNull, listDir, obj, scanJsonlChunks, str, } from "./jsonl.js";
|
|
10
|
+
import { TOOL_TEXT_MAX, toolInput, toolLine } from "./flatten.js";
|
|
11
|
+
import { CWD_SCAN_MAX_BYTES, CWD_SCAN_MAX_RECORDS, SessionActiveError, } from "./types.js";
|
|
12
|
+
|
|
13
|
+
/** Codex home: `CODEX_HOME` from the host environment, else `~/.codex` (T3 AgentSessionScanner.ts:1416-1447). */
|
|
14
|
+
export function codexHome(env ) {
|
|
15
|
+
const fromEnv = (env.env ?? process.env).CODEX_HOME;
|
|
16
|
+
return fromEnv !== undefined && fromEnv.trim() !== "" ? resolve(fromEnv) : join(env.home, ".codex");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const COMPRESSED_EXT = ".jsonl.zst";
|
|
20
|
+
const isPlainRollout = (name ) => name.startsWith("rollout-") && name.endsWith(".jsonl");
|
|
21
|
+
const isCompressedRollout = (name ) => name.startsWith("rollout-") && name.endsWith(COMPRESSED_EXT);
|
|
22
|
+
const ROLLOUT_NAME_RE = /^rollout-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i;
|
|
23
|
+
|
|
24
|
+
/** Decompressed size above which a compressed rollout is not converted (flag / skip reason `too-large`). */
|
|
25
|
+
export const MAX_DECOMPRESSED_BYTES = 512 * 1024 * 1024;
|
|
26
|
+
/** Longest line of a compressed rollout read; a longer one is `too-large` (it is never buffered whole). */
|
|
27
|
+
export const MAX_LINE_BYTES = 64 * 1024 * 1024;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Display id of a compressed rollout (Codex CLI >= 0.167 compresses older rollouts to `rollout-*.jsonl.zst`), else
|
|
31
|
+
* null: the id from a `rollout-<timestamp>-<uuid>` name, else the stem. T3 Code lists only `rollout-*.jsonl`, so t3sm
|
|
32
|
+
* converts compressed ones (never writing into ~/.codex); with `codex.convertCompressed: false` they are listed
|
|
33
|
+
* unopened under this id.
|
|
34
|
+
*/
|
|
35
|
+
export function compressedRolloutId(path ) {
|
|
36
|
+
const name = basename(path);
|
|
37
|
+
if (!isCompressedRollout(name)) return null;
|
|
38
|
+
const stem = name.slice(0, -COMPRESSED_EXT.length);
|
|
39
|
+
return ROLLOUT_NAME_RE.exec(stem)?.[1] ?? stem;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether compressed rollouts are converted in this env (`config.codex.convertCompressed`, default true). */
|
|
43
|
+
export const convertCompressed = (env ) => env.config.codex?.convertCompressed !== false;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Files: `sessions/<year>/<month>/<day>/rollout-*.jsonl`, regular files only (symlinks followed, as T3
|
|
47
|
+
* stats every entry), no recursion beyond the day directory (T3 AgentSessionScanner.ts:1213-1270).
|
|
48
|
+
* Compressed `rollout-*.jsonl.zst` files are listed too (t3sm only: converted, see native.ts).
|
|
49
|
+
*/
|
|
50
|
+
async function* files(env , onError ) {
|
|
51
|
+
const sessions = join(codexHome(env), "sessions");
|
|
52
|
+
const names = async (dir , want ) => {
|
|
53
|
+
const out = [];
|
|
54
|
+
for (const d of await listDir(dir)) if ((await entryKind(dir, d, onError)) === want) out.push(d.name);
|
|
55
|
+
return out.sort().reverse();
|
|
56
|
+
};
|
|
57
|
+
for (const y of await names(sessions, "dir")) {
|
|
58
|
+
for (const m of await names(join(sessions, y), "dir")) {
|
|
59
|
+
for (const d of await names(join(sessions, y, m), "dir")) {
|
|
60
|
+
const dir = join(sessions, y, m, d);
|
|
61
|
+
for (const f of await names(dir, "file")) {
|
|
62
|
+
if (isPlainRollout(f) || isCompressedRollout(f)) yield join(dir, f);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const hash = (t ) => createHash("sha1").update(t).digest("hex");
|
|
70
|
+
|
|
71
|
+
// ---------- byte sources ----------
|
|
72
|
+
|
|
73
|
+
class TooLargeError extends Error {
|
|
74
|
+
name = "TooLargeError";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The Frame_Content_Size of the zstd frame starting at `b` (RFC 8878 §3.1.1.1), or null when the header does not
|
|
79
|
+
* carry one (or is not a zstd frame). Codex writes one frame per rollout with its size (measured on 108 real files).
|
|
80
|
+
*/
|
|
81
|
+
export function zstdFrameContentSize(b ) {
|
|
82
|
+
if (b.length < 6 || b.readUInt32LE(0) !== 0xfd2fb528) return null;
|
|
83
|
+
const d = b[4] ;
|
|
84
|
+
const flag = d >> 6, single = (d >> 5) & 1;
|
|
85
|
+
const at = 5 + (single ? 0 : 1) + [0, 1, 2, 4][d & 3] ;
|
|
86
|
+
const size = flag === 0 ? (single ? 1 : 0) : [0, 2, 4, 8][flag] ;
|
|
87
|
+
if (size === 0 || b.length < at + size) return null;
|
|
88
|
+
if (size === 1) return b[at] ;
|
|
89
|
+
if (size === 2) return b.readUInt16LE(at) + 256;
|
|
90
|
+
if (size === 4) return b.readUInt32LE(at);
|
|
91
|
+
return Number(b.readBigUInt64LE(at));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const zstdError = (code , message ) => Object.assign(new Error(message), { code });
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The zstd frames of a file, from their headers and block headers only (RFC 8878 §3.1; no decompression): where each
|
|
100
|
+
* frame starts and ends and the size it declares. Skippable frames are stepped over. Strict: a frame or block that runs
|
|
101
|
+
* past the end of the file is `ZSTD_truncated`; bytes that are not a frame, or a reserved block type, `ZSTD_corrupt`.
|
|
102
|
+
* Node's zstd stream decodes only the first frame and ends silently on a truncated one, so this is what makes a
|
|
103
|
+
* partial read impossible.
|
|
104
|
+
*/
|
|
105
|
+
async function zstdFrames(path ) {
|
|
106
|
+
const fh = await open(path, "r");
|
|
107
|
+
try {
|
|
108
|
+
const size = (await fh.stat()).size;
|
|
109
|
+
const read = async (pos , n ) => {
|
|
110
|
+
const b = Buffer.alloc(n);
|
|
111
|
+
const { bytesRead } = await fh.read(b, 0, n, pos);
|
|
112
|
+
return b.subarray(0, bytesRead);
|
|
113
|
+
};
|
|
114
|
+
const cut = (what ) => zstdError("ZSTD_truncated", `truncated zstd file: ${what} runs past its end (${size} bytes)`);
|
|
115
|
+
if (size === 0) throw zstdError("ZSTD_truncated", "empty zstd file");
|
|
116
|
+
const frames = [];
|
|
117
|
+
let pos = 0;
|
|
118
|
+
while (pos < size) {
|
|
119
|
+
const head = await read(pos, 18);
|
|
120
|
+
if (head.length < 4) throw cut(`the frame at byte ${pos}`);
|
|
121
|
+
const magic = head.readUInt32LE(0);
|
|
122
|
+
if ((magic & 0xfffffff0) === 0x184d2a50) { // skippable frame: 4-byte magic, 4-byte size, data
|
|
123
|
+
if (head.length < 8) throw cut(`the skippable frame at byte ${pos}`);
|
|
124
|
+
pos += 8 + head.readUInt32LE(4);
|
|
125
|
+
if (pos > size) throw cut("a skippable frame");
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (magic !== 0xfd2fb528) throw zstdError("ZSTD_corrupt", `not a zstd frame at byte ${pos}`);
|
|
129
|
+
if (head.length < 5) throw cut(`the frame header at byte ${pos}`);
|
|
130
|
+
const d = head[4] ;
|
|
131
|
+
if ((d >> 3) & 1) throw zstdError("ZSTD_corrupt", `reserved bit set in the frame header at byte ${pos}`);
|
|
132
|
+
const flag = d >> 6, single = (d >> 5) & 1;
|
|
133
|
+
const headerSize = 5 + (single ? 0 : 1) + [0, 1, 2, 4][d & 3] + (flag === 0 ? (single ? 1 : 0) : [0, 2, 4, 8][flag] );
|
|
134
|
+
if (head.length < headerSize) throw cut(`the frame header at byte ${pos}`);
|
|
135
|
+
const contentSize = zstdFrameContentSize(head);
|
|
136
|
+
let p = pos + headerSize;
|
|
137
|
+
for (;;) {
|
|
138
|
+
const bh = await read(p, 3);
|
|
139
|
+
if (bh.length < 3) throw cut(`a block header at byte ${p}`);
|
|
140
|
+
const v = bh[0] | (bh[1] << 8) | (bh[2] << 16);
|
|
141
|
+
const type = (v >> 1) & 3;
|
|
142
|
+
if (type === 3) throw zstdError("ZSTD_corrupt", `reserved block type at byte ${p}`);
|
|
143
|
+
p += 3 + (type === 1 ? 1 : v >>> 3); // RLE blocks carry one byte
|
|
144
|
+
if (p > size) throw cut(`a block`);
|
|
145
|
+
if (v & 1) break; // last block
|
|
146
|
+
}
|
|
147
|
+
if ((d >> 2) & 1) { // content checksum
|
|
148
|
+
p += 4;
|
|
149
|
+
if (p > size) throw cut("the frame checksum");
|
|
150
|
+
}
|
|
151
|
+
frames.push({ start: pos, end: p, contentSize });
|
|
152
|
+
pos = p;
|
|
153
|
+
}
|
|
154
|
+
return frames;
|
|
155
|
+
} finally {
|
|
156
|
+
await fh.close();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The decompressed bytes of a `.jsonl.zst`, streamed frame after frame; throws TooLargeError once more than `maxBytes`
|
|
162
|
+
* came out. Completeness is strict: the frame structure must be whole (zstdFrames), each frame must decode to the
|
|
163
|
+
* size it declares, and a file with a frame that declares no size (a streaming compressor) must end with a newline.
|
|
164
|
+
*/
|
|
165
|
+
async function* zstdChunks(path , readOpts , maxBytes ) {
|
|
166
|
+
const frames = await zstdFrames(path);
|
|
167
|
+
let total = 0;
|
|
168
|
+
let lastByte = -1;
|
|
169
|
+
let undeclared = false;
|
|
170
|
+
for (const f of frames) {
|
|
171
|
+
const src = createReadStream(path, { ...readOpts, start: f.start, end: f.end - 1 });
|
|
172
|
+
const z = createZstdDecompress();
|
|
173
|
+
src.on("error", (e) => z.destroy(e));
|
|
174
|
+
src.pipe(z);
|
|
175
|
+
let n = 0;
|
|
176
|
+
try {
|
|
177
|
+
for await (const c of z ) {
|
|
178
|
+
n += c.length;
|
|
179
|
+
total += c.length;
|
|
180
|
+
if (total > maxBytes) throw new TooLargeError(`more than ${maxBytes} bytes decompressed`);
|
|
181
|
+
if (c.length > 0) lastByte = c[c.length - 1] ;
|
|
182
|
+
yield c;
|
|
183
|
+
}
|
|
184
|
+
} finally {
|
|
185
|
+
src.destroy();
|
|
186
|
+
z.destroy();
|
|
187
|
+
}
|
|
188
|
+
if (f.contentSize === null) undeclared = true;
|
|
189
|
+
else if (n !== f.contentSize) throw zstdError("ZSTD_truncated", `zstd frame at byte ${f.start} decoded to ${n} of ${f.contentSize} bytes`);
|
|
190
|
+
}
|
|
191
|
+
if (total === 0) throw zstdError("ZSTD_truncated", "no data in the zstd file");
|
|
192
|
+
if (undeclared && lastByte !== 0x0a) throw zstdError("ZSTD_truncated", "the decompressed rollout does not end with a complete line");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A rollout's bytes: decompressed for `.jsonl.zst`, as they are for `.jsonl`. */
|
|
196
|
+
function rolloutChunks(path , readOpts , maxBytes ) {
|
|
197
|
+
return path.endsWith(COMPRESSED_EXT) ? zstdChunks(path, readOpts, maxBytes) : (createReadStream(path, readOpts) );
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ---------- the message rules (one implementation for discovery and conversion) ----------
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
/** What one pass over a rollout yields: the scan (discovery's measure), plus the messages when collected. */
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
/** At most this much of a tool input or output is kept: TOOL_TEXT_MAX of the joined text is all that is shown. */
|
|
213
|
+
const keep = (s ) => (s.length > TOOL_TEXT_MAX + 1 ? s.slice(0, TOOL_TEXT_MAX + 1) : s);
|
|
214
|
+
|
|
215
|
+
function toolOutput(v ) {
|
|
216
|
+
if (typeof v === "string") return v;
|
|
217
|
+
const o = obj(v);
|
|
218
|
+
if (o && typeof o.output === "string") return o.output;
|
|
219
|
+
if (o && typeof o.content === "string") return o.content;
|
|
220
|
+
const t = extractText(o?.content ?? v);
|
|
221
|
+
return t !== "" ? t : v === undefined ? "" : JSON.stringify(v);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** A function_call's `arguments` (a JSON string) as the user would recognise it. */
|
|
225
|
+
function argsInput(v ) {
|
|
226
|
+
if (typeof v !== "string") return toolInput(v);
|
|
227
|
+
try {
|
|
228
|
+
return toolInput(JSON.parse(v));
|
|
229
|
+
} catch {
|
|
230
|
+
return v;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* One pass over a rollout's records. Rules copied from T3 (apps/server/src/project/AgentSessionScanner.ts):
|
|
236
|
+
* - session id: the first `session_meta` record with a non-empty `payload.id`, else `payload.session_id`
|
|
237
|
+
* (lines 541-543). The rollout file name is NOT the id and T3 never imports a rollout without one
|
|
238
|
+
* (lines 409-410, 600), so `id` stays null here (flag `no-session-id`; no stem fallback for Codex).
|
|
239
|
+
* - cwd: first record with a non-blank top-level `cwd`, else (after the subagent check) `payload.cwd`
|
|
240
|
+
* (extractCwd, 677-712). A `session_meta` whose `payload.source` has a `subagent`/`subAgent` key and
|
|
241
|
+
* no top-level cwd stops discovery (686-697): flag `subagent`, cwd null.
|
|
242
|
+
* - cwd window: first 1 MiB and 1000 lines, blank lines included (lines 59, 68, 947-957, 988-991).
|
|
243
|
+
* - messages (parseAgentSessionRecords, 433-476 pre-pass and 541-596 main pass), replayed per segment,
|
|
244
|
+
* a segment being the records up to and including an assistant response_item message:
|
|
245
|
+
* pre-pass: a response-item user message with a turn_id (and text) whose text equals a non-empty
|
|
246
|
+
* event_msg user_message text of the segment makes that turn_id canonical, and every response-item
|
|
247
|
+
* user message of the segment with a canonical turn_id is dropped (433-446). Main pass: an event
|
|
248
|
+
* user_message also removes an earlier retained response-item user copy with the same text (554-560);
|
|
249
|
+
* a response-item user message is dropped when canonical or when a retained event message with the
|
|
250
|
+
* same text precedes it in the segment (583-588, 494-509). Response items need non-empty extracted
|
|
251
|
+
* text. Simplification: an assistant message with empty text also closes the segment here, while T3
|
|
252
|
+
* closes it only in the pre-pass (negligible).
|
|
253
|
+
* Conversion (`collect`) keeps the same messages, in order, plus t3sm's own additions for the converted transcript:
|
|
254
|
+
* tool calls (function_call, custom_tool_call, local_shell_call, web_search_call with their outputs) as assistant
|
|
255
|
+
* text `[tool <name>] <input> → <output>`; reasoning and every other record are dropped. Tool lines are not counted
|
|
256
|
+
* (messageCount stays T3's measure, the one discovery reports).
|
|
257
|
+
*/
|
|
258
|
+
async function reduceRollout(chunks , o ) {
|
|
259
|
+
const r = {
|
|
260
|
+
id: null, cwd: null, startedAt: null, updatedAt: null, messageCount: 0, visibleUserCount: 0,
|
|
261
|
+
corruptLines: 0, cwdOffsetBytes: 0, cwdReadable: false, subagent: false, messages: [],
|
|
262
|
+
};
|
|
263
|
+
const out = [];
|
|
264
|
+
const calls = new Map ();
|
|
265
|
+
let seg = [];
|
|
266
|
+
let lastTs = "";
|
|
267
|
+
|
|
268
|
+
const flushSegment = () => {
|
|
269
|
+
const eventTexts = new Set ();
|
|
270
|
+
for (const it of seg) if (it.k === "event") eventTexts.add(hash(it.text));
|
|
271
|
+
const canonical = new Set ();
|
|
272
|
+
for (const it of seg) if (it.k === "user" && it.turn !== null && eventTexts.has(hash(it.text))) canonical.add(it.turn);
|
|
273
|
+
const retained = [];
|
|
274
|
+
for (const it of seg) {
|
|
275
|
+
if (it.k === "tool") { retained.push({ key: null, it }); continue; }
|
|
276
|
+
const key = hash(it.text);
|
|
277
|
+
if (it.k === "event") {
|
|
278
|
+
for (let i = retained.length - 1; i >= 0; i--) {
|
|
279
|
+
const x = retained[i] ;
|
|
280
|
+
if (x.key !== null && x.fromResponse && x.key === key) {
|
|
281
|
+
retained.splice(i, 1);
|
|
282
|
+
r.messageCount--;
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
retained.push({ fromResponse: false, key, it });
|
|
287
|
+
r.messageCount++;
|
|
288
|
+
r.visibleUserCount++;
|
|
289
|
+
} else {
|
|
290
|
+
if (it.turn !== null && canonical.has(it.turn)) continue;
|
|
291
|
+
if (retained.some((m) => m.key !== null && !m.fromResponse && m.key === key)) continue;
|
|
292
|
+
retained.push({ fromResponse: true, key, it });
|
|
293
|
+
r.messageCount++;
|
|
294
|
+
r.visibleUserCount++;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (o.collect) {
|
|
298
|
+
for (const { it } of retained) out.push(it.k === "tool" ? { role: "assistant", tool: it.tool, ts: it.ts } : { role: "user", text: it.text, ts: it.ts });
|
|
299
|
+
}
|
|
300
|
+
seg = [];
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
const totals = await scanJsonlChunks(chunks, (rec, info) => {
|
|
305
|
+
const payload = obj(rec.payload);
|
|
306
|
+
if (r.id === null && rec.type === "session_meta" && payload) {
|
|
307
|
+
r.id = str(payload.id) || str(payload.session_id) || null;
|
|
308
|
+
}
|
|
309
|
+
if (!r.cwd && !r.subagent) {
|
|
310
|
+
let cwd = typeof rec.cwd === "string" && rec.cwd.trim() !== "" ? rec.cwd : null;
|
|
311
|
+
if (cwd === null && rec.type === "session_meta" && payload) {
|
|
312
|
+
const src = obj(payload.source);
|
|
313
|
+
if (src && (Object.hasOwn(src, "subagent") || Object.hasOwn(src, "subAgent"))) {
|
|
314
|
+
r.subagent = true;
|
|
315
|
+
if (o.stopAtSubagent) return "stop"; // skipped whatever follows
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (cwd === null && !r.subagent && payload && typeof payload.cwd === "string" && payload.cwd.trim() !== "") cwd = payload.cwd;
|
|
319
|
+
if (cwd !== null) {
|
|
320
|
+
r.cwd = cwd;
|
|
321
|
+
r.cwdOffsetBytes = info.offset;
|
|
322
|
+
r.cwdReadable = info.offset <= CWD_SCAN_MAX_BYTES && info.recordIndex <= CWD_SCAN_MAX_RECORDS;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const ts = isoOrNull(rec.timestamp);
|
|
326
|
+
if (ts !== null) {
|
|
327
|
+
r.startedAt ??= ts;
|
|
328
|
+
r.updatedAt = ts;
|
|
329
|
+
lastTs = ts;
|
|
330
|
+
}
|
|
331
|
+
if (!payload) return;
|
|
332
|
+
if (rec.type === "event_msg" && payload.type === "user_message") {
|
|
333
|
+
const text = typeof payload.message === "string" ? payload.message.trim() : "";
|
|
334
|
+
if (text !== "") seg.push({ k: "event", text, ts: lastTs });
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (rec.type !== "response_item") return;
|
|
338
|
+
if (payload.type === "message" && (payload.role === "user" || payload.role === "assistant")) {
|
|
339
|
+
if (payload.role === "assistant") {
|
|
340
|
+
flushSegment();
|
|
341
|
+
const text = extractText(payload.content);
|
|
342
|
+
if (text !== "") {
|
|
343
|
+
r.messageCount++;
|
|
344
|
+
if (o.collect) out.push({ role: "assistant", text, ts: lastTs });
|
|
345
|
+
}
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const text = extractText(payload.content);
|
|
349
|
+
if (text === "") return;
|
|
350
|
+
const turnRaw = obj(payload.internal_chat_message_metadata_passthrough)?.turn_id;
|
|
351
|
+
const turn = typeof turnRaw === "string" && turnRaw.trim() !== "" ? turnRaw : null;
|
|
352
|
+
seg.push({ k: "user", text, turn, ts: lastTs });
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (!o.collect) return;
|
|
356
|
+
const call = (name , input ) => {
|
|
357
|
+
const tool = { name: name || "unknown", input: keep(input), output: null };
|
|
358
|
+
const id = str(payload.call_id);
|
|
359
|
+
if (id !== "") calls.set(id, tool);
|
|
360
|
+
seg.push({ k: "tool", tool, ts: lastTs });
|
|
361
|
+
};
|
|
362
|
+
switch (payload.type) {
|
|
363
|
+
case "function_call": call(str(payload.name), argsInput(payload.arguments)); break;
|
|
364
|
+
case "custom_tool_call": call(str(payload.name), typeof payload.input === "string" ? payload.input : toolInput(payload.input)); break;
|
|
365
|
+
case "local_shell_call": {
|
|
366
|
+
const cmd = obj(payload.action)?.command;
|
|
367
|
+
call("shell", Array.isArray(cmd) ? cmd.join(" ") : toolInput(payload.action));
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
case "web_search_call": {
|
|
371
|
+
const q = obj(payload.action)?.query;
|
|
372
|
+
call("web_search", typeof q === "string" ? q : toolInput(payload.action));
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
case "function_call_output":
|
|
376
|
+
case "custom_tool_call_output": {
|
|
377
|
+
const t = calls.get(str(payload.call_id));
|
|
378
|
+
if (t) t.output = keep(toolOutput(payload.output));
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
default: break; // reasoning and everything else: dropped
|
|
382
|
+
}
|
|
383
|
+
}, { maxLineBytes: o.maxLineBytes });
|
|
384
|
+
r.corruptLines = totals.corruptLines;
|
|
385
|
+
if (r.cwd === null) r.cwdOffsetBytes = totals.totalBytes;
|
|
386
|
+
} catch (e) {
|
|
387
|
+
if (!(e instanceof TooLargeError) && !(e instanceof LineTooLongError)) throw e;
|
|
388
|
+
r.tooLarge = true;
|
|
389
|
+
}
|
|
390
|
+
flushSegment();
|
|
391
|
+
if (o.collect) {
|
|
392
|
+
r.messages = out.map((m) => ({
|
|
393
|
+
role: m.role,
|
|
394
|
+
text: "tool" in m ? toolLine(m.tool.name, m.tool.input, m.tool.output ?? "(no output)") : m.text,
|
|
395
|
+
timestamp: m.ts,
|
|
396
|
+
}));
|
|
397
|
+
}
|
|
398
|
+
return r;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
/** Reducer options for a path: compressed rollouts get the per-line cap and stop at a subagent meta (when scanning). */
|
|
404
|
+
const capsFor = (path , ro ) =>
|
|
405
|
+
(path.endsWith(COMPRESSED_EXT) ? { maxLineBytes: ro.maxLineBytes ?? MAX_LINE_BYTES } : {});
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Discovery's scan of a rollout (`.jsonl`, or `.jsonl.zst` decompressed while streaming). A compressed rollout stops
|
|
409
|
+
* at a subagent meta (skipped anyway) and at the decompressed-size cap (`tooLarge`); a corrupt stream throws.
|
|
410
|
+
*/
|
|
411
|
+
export async function scanRollout(path , readOpts = {}) {
|
|
412
|
+
const max = readOpts.maxDecompressedBytes ?? MAX_DECOMPRESSED_BYTES;
|
|
413
|
+
const { messages: _m, ...scan } = await reduceRollout(rolloutChunks(path, readOpts, max), { collect: false, stopAtSubagent: path.endsWith(COMPRESSED_EXT), ...capsFor(path, readOpts) });
|
|
414
|
+
return scan;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** The whole rollout (`.jsonl` or `.jsonl.zst`): the scan plus the canonical messages (the conversion's input). */
|
|
418
|
+
export function readRollout(path , readOpts = {}) {
|
|
419
|
+
const max = readOpts.maxDecompressedBytes ?? MAX_DECOMPRESSED_BYTES;
|
|
420
|
+
return reduceRollout(rolloutChunks(path, readOpts, max), { collect: true, stopAtSubagent: false, ...capsFor(path, readOpts) });
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// ---------- adapter ----------
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
export function createCodexAdapter(opts = {}) {
|
|
440
|
+
const max = opts.maxDecompressedBytes ?? MAX_DECOMPRESSED_BYTES;
|
|
441
|
+
const now = opts.now ?? Date.now;
|
|
442
|
+
const native = createNativeAdapter({
|
|
443
|
+
id: "codex",
|
|
444
|
+
cacheFile: "codex-discovery.json",
|
|
445
|
+
// 4: compressed rollouts are scanned (decompressed) and carry `tooLarge`. 5: every zstd frame decoded, strict
|
|
446
|
+
// completeness, per-line cap (results of version 4 may be partial reads).
|
|
447
|
+
cacheVersion: 5,
|
|
448
|
+
root: (env) => join(codexHome(env), "sessions"),
|
|
449
|
+
files,
|
|
450
|
+
scan: (p, ro) => scanRollout(p, { ...ro, maxDecompressedBytes: max, maxLineBytes: opts.maxLineBytes }),
|
|
451
|
+
stemFallback: false,
|
|
452
|
+
compressed: { id: compressedRolloutId, convert: convertCompressed },
|
|
453
|
+
}, opts);
|
|
454
|
+
const adapter = Object.assign(native, {
|
|
455
|
+
async read(s ) {
|
|
456
|
+
const st = await stat(s.file.path);
|
|
457
|
+
if (isActive(st.mtimeMs, now())) {
|
|
458
|
+
throw new SessionActiveError(`codex session ${s.nativeId} was written ${Math.max(0, Math.round((now() - st.mtimeMs) / 1000))} s ago; it may be in use`);
|
|
459
|
+
}
|
|
460
|
+
const r = await readRollout(s.file.path, { ...opts.readOptions, maxDecompressedBytes: max, maxLineBytes: opts.maxLineBytes });
|
|
461
|
+
if (r.tooLarge) throw new Error(`codex session ${s.nativeId} is larger than ${Math.round(max / 1048576)} MiB decompressed (too-large)`);
|
|
462
|
+
if (r.id !== s.nativeId) throw new Error(`${s.file.path} holds session ${r.id ?? "(no id)"}, not ${s.nativeId}`);
|
|
463
|
+
if (r.cwd === null) throw new Error(`codex session ${s.nativeId} records no working directory`);
|
|
464
|
+
const mtimeIso = new Date(st.mtimeMs).toISOString();
|
|
465
|
+
return {
|
|
466
|
+
harness: "codex", nativeId: r.id, cwd: r.cwd, title: null, startedAt: r.startedAt ?? mtimeIso, messages: r.messages,
|
|
467
|
+
activity: { updatedAt: r.updatedAt ?? mtimeIso, messageCount: r.messageCount },
|
|
468
|
+
};
|
|
469
|
+
},
|
|
470
|
+
});
|
|
471
|
+
return adapter;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
export const codexAdapter = createCodexAdapter();
|
|
475
|
+
|
|
476
|
+
/** Flags that make a Codex session skipped whatever T3 holds (doctor's count; the planner decides the same way). */
|
|
477
|
+
const SKIPPING = ["duplicate-file", "subagent", "no-session-id", "only-hidden", "no-cwd", "cwd-unreadable", "over-4gib", "too-large"];
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Codex sessions by how they reach T3 (doctor): `importable` natively (plain rollouts), `convertible` (compressed,
|
|
481
|
+
* converted into Claude threads), `skipped` (duplicate files, sub-agents, no id / cwd, too large, compressed with
|
|
482
|
+
* conversion off) and `readErrors` (files or folders that could not be read: not sessions). `sessionIds`: the native
|
|
483
|
+
* ids of the importable and convertible ones (doctor counts those already in T3, under either form). Read-only:
|
|
484
|
+
* discovery reads its cache (a warm cache decompresses nothing) and never saves it; nothing else is written.
|
|
485
|
+
*/
|
|
486
|
+
export async function countCodexSessions(env , adapter = createCodexAdapter()) {
|
|
487
|
+
const n = { importable: 0, convertible: 0, skipped: 0, readErrors: 0, sessionIds: [] };
|
|
488
|
+
for await (const s of adapter.discover({ ...env, readOnly: true })) {
|
|
489
|
+
if (s.flags.some((f) => SKIPPING.includes(f))) n.skipped++;
|
|
490
|
+
else if (s.mode === "convert") { n.convertible++; n.sessionIds.push(s.nativeId); }
|
|
491
|
+
else if (s.flags.includes("compressed")) n.skipped++;
|
|
492
|
+
else { n.importable++; n.sessionIds.push(s.nativeId); }
|
|
493
|
+
}
|
|
494
|
+
n.readErrors = adapter.lastErrors.length;
|
|
495
|
+
return n;
|
|
496
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Tool calls as conversation text, shared by every convert-mode reader (SourceAdapter.read contract):
|
|
2
|
+
// `[tool <name>] <input> → <output>`, the part after the name at most TOOL_TEXT_MAX characters (`…` appended when clipped).
|
|
3
|
+
|
|
4
|
+
/** Characters of `<input> → <output>` kept per flattened tool call. */
|
|
5
|
+
export const TOOL_TEXT_MAX = 500;
|
|
6
|
+
|
|
7
|
+
export function clip(s , max ) {
|
|
8
|
+
return s.length <= max ? s : `${s.slice(0, max)}…`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** A tool input as the user would recognise it: the lone string argument (`ls -la`), else compact JSON. */
|
|
12
|
+
export function toolInput(input ) {
|
|
13
|
+
if (input !== null && typeof input === "object" && !Array.isArray(input)) {
|
|
14
|
+
const vals = Object.values(input );
|
|
15
|
+
if (vals.length === 1 && typeof vals[0] === "string") return vals[0];
|
|
16
|
+
}
|
|
17
|
+
return input === undefined ? "" : JSON.stringify(input);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** One flattened tool call line. */
|
|
21
|
+
export function toolLine(name , input , output ) {
|
|
22
|
+
return `[tool ${name}] ${clip(`${input} → ${output}`, TOOL_TEXT_MAX)}`;
|
|
23
|
+
}
|