maka-agent 0.2.0-dev.52.20260926 → 0.2.0-dev.53.20260927
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/dist/activation-command.js +11 -3
- package/dist/pi-tui-mcp-status.js +73 -5
- package/dist/pi-tui-runner.js +1 -1
- package/dist/runtime-host-run-command.js +39 -6
- package/dist/runtime-host-run-mcp.js +161 -0
- package/dist/runtime-host-session-driver.js +22 -2
- package/dist/tui-copy-catalog.js +6 -0
- package/dist/tui-mcp-control.js +11 -2
- package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
- package/node_modules/@maka/core/dist/attachments.js +7 -0
- package/node_modules/@maka/core/dist/events.js +11 -0
- package/node_modules/@maka/core/dist/provider-registry.js +11 -2
- package/node_modules/@maka/core/dist/turn-origin.js +6 -0
- package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +1 -5
- package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +80 -24
- package/node_modules/@maka/runtime/dist/bots/proxied-fetch.js +5 -4
- package/node_modules/@maka/runtime/dist/computer-use-tools.js +136 -100
- package/node_modules/@maka/runtime/dist/context-budget-helpers.js +4 -0
- package/node_modules/@maka/runtime/dist/conversation-copy.js +51 -205
- package/node_modules/@maka/runtime/dist/edit-replace.js +7 -7
- package/node_modules/@maka/runtime/dist/filesystem-executor.js +4 -20
- package/node_modules/@maka/runtime/dist/filesystem-worker/operations.js +17 -28
- package/node_modules/@maka/runtime/dist/format-json.js +39 -0
- package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +1 -6
- package/node_modules/@maka/runtime/dist/image-file.js +22 -14
- package/node_modules/@maka/runtime/dist/model-adapter.js +18 -2
- package/node_modules/@maka/runtime/dist/model-history.js +201 -3
- package/node_modules/@maka/runtime/dist/network/abortable-connector.js +55 -0
- package/node_modules/@maka/runtime/dist/network/proxy-dispatcher.js +72 -37
- package/node_modules/@maka/runtime/dist/network/proxy-test.js +1 -1
- package/node_modules/@maka/runtime/dist/network/scoped-fetch-transport.js +7 -2
- package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +1 -3
- package/node_modules/@maka/runtime/dist/provider-error-classification.js +36 -8
- package/node_modules/@maka/runtime/dist/recovery-resolver.js +1 -91
- package/node_modules/@maka/runtime/dist/runtime-kernel.js +44 -0
- package/node_modules/@maka/runtime/dist/session-manager.js +54 -58
- package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +84 -2
- package/node_modules/@maka/runtime/dist/workers/filesystem-worker.js +63 -51
- package/node_modules/@maka/runtime/dist/workspace-executor.js +5 -6
- package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +27 -6
- package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +4 -1
- package/node_modules/@maka/runtime-host/dist/client/host-handoff.js +9 -9
- package/node_modules/@maka/runtime-host/dist/client/session-catalog-summary.js +9 -1
- package/node_modules/@maka/runtime-host/dist/protocol/index.js +12 -1
- package/node_modules/@maka/runtime-host/dist/protocol/session-catalog.js +28 -6
- package/node_modules/@maka/runtime-host/dist/protocol/turn.js +12 -2
- package/node_modules/@maka/runtime-host/dist/server/client-capability-coordinator.js +10 -7
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +10 -2
- package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +2 -0
- package/node_modules/@maka/runtime-host/dist/server/interactive-turn-coordinator.js +4 -1
- package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +4 -2
- package/node_modules/@maka/runtime-host/dist/server/operation-dispatcher.js +4 -0
- package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +6 -5
- package/node_modules/@maka/runtime-host/dist/server/safe-boundary-resume-policy.js +27 -0
- package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +5 -3
- package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +7 -2
- package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +11 -141
- package/node_modules/@maka/runtime-host/dist/server/session-revision-graph-references.js +46 -75
- package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +18 -2
- package/node_modules/@maka/runtime-host/dist/server/workhub-result-coordinator.js +33 -3
- package/node_modules/@maka/runtime-host/dist/transport/framed-byte-stream-transport.js +39 -5
- package/node_modules/@maka/storage/dist/agent-run-store-contract.js +7 -1
- package/node_modules/@maka/storage/dist/artifact-store.js +1 -5
- package/node_modules/@maka/storage/dist/artifact-stores.js +0 -3
- package/node_modules/@maka/storage/dist/atomic-file-write.js +1 -0
- package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +107 -57
- package/node_modules/@maka/storage/dist/codex-session-adapter.js +227 -19
- package/node_modules/@maka/storage/dist/external-session-importer.js +1 -1
- package/node_modules/@maka/storage/dist/mcp-config-store.js +22 -3
- package/node_modules/@maka/storage/dist/settings-store.js +281 -13
- package/package.json +1 -1
|
@@ -80,19 +80,93 @@ export class CodexSessionAdapter {
|
|
|
80
80
|
}
|
|
81
81
|
async readSession(sessionId) {
|
|
82
82
|
assertSafeCodexSessionId(sessionId);
|
|
83
|
-
const
|
|
84
|
-
if (!
|
|
83
|
+
const found = await this.findCatalogEntry(sessionId);
|
|
84
|
+
if (!found)
|
|
85
85
|
throw new ExternalSessionNotFoundError();
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
const { entry: catalogEntry, fromStateDatabase } = found;
|
|
87
|
+
const rollouts = lazyRolloutIndex(this.codexHome);
|
|
88
|
+
// Codex's state row selects the thread's current rollout. Without one,
|
|
89
|
+
// Codex takes the newest file named for the thread, and so does this.
|
|
90
|
+
const current = fromStateDatabase
|
|
91
|
+
? catalogEntry.rolloutPath
|
|
92
|
+
: ((await this.newestThreadRollout(sessionId, rollouts)) ?? catalogEntry.rolloutPath);
|
|
93
|
+
const segments = await this.rolloutLineage(current, sessionId, rollouts);
|
|
94
|
+
return convertCodexRollout(segments, sessionId, catalogEntry.name, catalogEntry.cwd, {
|
|
90
95
|
maxRolloutBytes: this.maxRolloutBytes,
|
|
91
96
|
maxRecordBytes: this.maxRecordBytes,
|
|
92
97
|
maxConvertedBytes: this.maxConvertedBytes,
|
|
93
98
|
maxMessages: this.maxMessages,
|
|
94
99
|
});
|
|
95
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* The rollout ranges that make up a thread's history, oldest first.
|
|
103
|
+
*
|
|
104
|
+
* Codex never rewrites a rollout. `thread/revert` starts a new file for the
|
|
105
|
+
* same thread, `rollout-<ts>-<threadId>_<rolloutId>.jsonl`, whose
|
|
106
|
+
* `session_meta.history_base` names the rollout it continues and the byte
|
|
107
|
+
* offset where the kept history ends, then moves the state row to it. What
|
|
108
|
+
* follows that offset was reverted (typically an interrupted turn whose
|
|
109
|
+
* message was then edited and sent again) and is no longer part of the
|
|
110
|
+
* thread. Codex rebuilds history by following those pointers, and so does
|
|
111
|
+
* this; the older files are never read past the offset their successor keeps.
|
|
112
|
+
*/
|
|
113
|
+
async rolloutLineage(currentPath, sessionId, rollouts) {
|
|
114
|
+
const segments = [];
|
|
115
|
+
let path = await this.resolveRolloutPath(currentPath, sessionId);
|
|
116
|
+
if (!path)
|
|
117
|
+
throw new ExternalSessionNotFoundError();
|
|
118
|
+
// Every later rollout of a thread is one a revert started, so a history
|
|
119
|
+
// base in the opening rollout can only be a fork's, into its parent. The
|
|
120
|
+
// converter still checks that the file names the thread.
|
|
121
|
+
if (isOpeningRolloutFilename(basename(path), sessionId))
|
|
122
|
+
return [{ path }];
|
|
123
|
+
let endByteOffset;
|
|
124
|
+
for (;;) {
|
|
125
|
+
if (segments.some((segment) => segment.path === path)) {
|
|
126
|
+
throw new Error(`Codex rollout history loops back on itself: expected ${sessionId}`);
|
|
127
|
+
}
|
|
128
|
+
// A file name is not proof of what a rollout holds, and the file the
|
|
129
|
+
// state row names is no exception: each range must name the thread.
|
|
130
|
+
const meta = await readRolloutHeadMeta(path, sessionId);
|
|
131
|
+
if (meta?.id !== sessionId) {
|
|
132
|
+
throw new Error(`Codex rollout Session id mismatch: expected ${sessionId}`);
|
|
133
|
+
}
|
|
134
|
+
segments.push(endByteOffset === undefined ? { path } : { path, endByteOffset });
|
|
135
|
+
const base = meta.historyBase;
|
|
136
|
+
if (!base)
|
|
137
|
+
break;
|
|
138
|
+
// No candidate cap here: the catalog cap guards a listing that would
|
|
139
|
+
// otherwise grow without bound, whereas this lookup keeps one file per
|
|
140
|
+
// rollout, and a store too large to list must not also become a store
|
|
141
|
+
// whose reverted threads cannot be imported.
|
|
142
|
+
const located = (await rollouts()).get(base.rolloutId);
|
|
143
|
+
// Missing history fails the import rather than quietly shortening it.
|
|
144
|
+
if (!located)
|
|
145
|
+
throw new Error(`Codex rollout history base is missing: expected ${sessionId}`);
|
|
146
|
+
// A fork's history starts in its parent's rollout, which belongs to the
|
|
147
|
+
// parent thread; this thread's own history ends here.
|
|
148
|
+
if (located.threadId !== sessionId)
|
|
149
|
+
break;
|
|
150
|
+
path = await this.resolveRolloutPath(located.path, sessionId);
|
|
151
|
+
if (!path)
|
|
152
|
+
throw new Error(`Codex rollout history base is missing: expected ${sessionId}`);
|
|
153
|
+
endByteOffset = base.endByteOffset;
|
|
154
|
+
}
|
|
155
|
+
return segments.reverse();
|
|
156
|
+
}
|
|
157
|
+
async newestThreadRollout(sessionId, rollouts) {
|
|
158
|
+
// The timestamps in the names make a code-unit sort chronological.
|
|
159
|
+
const named = [...(await rollouts()).values()]
|
|
160
|
+
.filter((rollout) => rollout.threadId === sessionId)
|
|
161
|
+
.map((rollout) => rollout.path)
|
|
162
|
+
.sort((left, right) => (basename(left) < basename(right) ? 1 : -1));
|
|
163
|
+
for (const candidate of named) {
|
|
164
|
+
const path = await this.resolveRolloutPath(candidate, sessionId);
|
|
165
|
+
if (path && (await readRolloutHeadMeta(path, sessionId))?.id === sessionId)
|
|
166
|
+
return path;
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
96
170
|
async findCatalogEntry(sessionId) {
|
|
97
171
|
for (const dbPath of await codexStateDbsNewestFirst(this.codexHome)) {
|
|
98
172
|
const rows = await readCodexThreadRows(dbPath, { includeArchived: true }, sessionId);
|
|
@@ -101,11 +175,12 @@ export class CodexSessionAdapter {
|
|
|
101
175
|
for (const row of rows) {
|
|
102
176
|
const entry = await this.entryFromRow(row);
|
|
103
177
|
if (entry?.id === sessionId)
|
|
104
|
-
return entry;
|
|
178
|
+
return { entry, fromStateDatabase: true };
|
|
105
179
|
}
|
|
106
180
|
break;
|
|
107
181
|
}
|
|
108
|
-
|
|
182
|
+
const entry = await this.findRolloutEntry(sessionId);
|
|
183
|
+
return entry ? { entry, fromStateDatabase: false } : undefined;
|
|
109
184
|
}
|
|
110
185
|
async entryFromRow(row) {
|
|
111
186
|
if (!isSafeCodexSessionId(row.id))
|
|
@@ -212,7 +287,7 @@ export class CodexSessionAdapter {
|
|
|
212
287
|
[join(this.codexHome, 'archived_sessions'), true],
|
|
213
288
|
]) {
|
|
214
289
|
for await (const candidate of iterateRolloutFiles(root, archived)) {
|
|
215
|
-
if (!
|
|
290
|
+
if (!isOpeningRolloutFilename(basename(candidate.path), sessionId))
|
|
216
291
|
continue;
|
|
217
292
|
const head = await readUtf8Prefix(candidate.path, CODEX_ROLLOUT_HEAD_BYTES).catch(() => undefined);
|
|
218
293
|
if (head === undefined)
|
|
@@ -244,10 +319,16 @@ export class CodexSessionAdapter {
|
|
|
244
319
|
}
|
|
245
320
|
}
|
|
246
321
|
}
|
|
247
|
-
async function convertCodexRollout(
|
|
322
|
+
async function convertCodexRollout(segments, expectedSessionId, fallbackName, fallbackCwd, limits) {
|
|
248
323
|
const converter = new CodexRolloutConverter(expectedSessionId, fallbackName, fallbackCwd, limits);
|
|
249
|
-
|
|
250
|
-
|
|
324
|
+
// Line numbers and the byte budget run across the whole lineage: generated
|
|
325
|
+
// message ids are keyed by line, so restarting per file would collide.
|
|
326
|
+
const cursor = { line: 0, bytes: 0 };
|
|
327
|
+
for (const { path, endByteOffset } of segments) {
|
|
328
|
+
converter.beginRollout();
|
|
329
|
+
for await (const record of readCodexRolloutRecords(path, expectedSessionId, limits, cursor, endByteOffset)) {
|
|
330
|
+
converter.accept(record);
|
|
331
|
+
}
|
|
251
332
|
}
|
|
252
333
|
return converter.finish();
|
|
253
334
|
}
|
|
@@ -265,6 +346,7 @@ class CodexRolloutConverter {
|
|
|
265
346
|
firstUserText;
|
|
266
347
|
metaCwd = '';
|
|
267
348
|
hasSessionMeta = false;
|
|
349
|
+
awaitingRolloutMeta = false;
|
|
268
350
|
convertedBytes = 0;
|
|
269
351
|
constructor(expectedSessionId, fallbackName, fallbackCwd, limits) {
|
|
270
352
|
this.expectedSessionId = expectedSessionId;
|
|
@@ -272,15 +354,26 @@ class CodexRolloutConverter {
|
|
|
272
354
|
this.fallbackCwd = fallbackCwd;
|
|
273
355
|
this.limits = limits;
|
|
274
356
|
}
|
|
357
|
+
/**
|
|
358
|
+
* Marks the start of the next rollout file of the thread, whose own
|
|
359
|
+
* `session_meta` must name the thread too: an earlier rollout is located by
|
|
360
|
+
* file name, and a name is not proof of ownership.
|
|
361
|
+
*/
|
|
362
|
+
beginRollout() {
|
|
363
|
+
this.awaitingRolloutMeta = true;
|
|
364
|
+
}
|
|
275
365
|
accept(record) {
|
|
276
366
|
const envelope = record.value;
|
|
277
|
-
if (envelope.type === 'session_meta' &&
|
|
278
|
-
this.
|
|
367
|
+
if (envelope.type === 'session_meta' && this.awaitingRolloutMeta) {
|
|
368
|
+
this.awaitingRolloutMeta = false;
|
|
279
369
|
const metaPayload = asRecord(envelope.payload);
|
|
280
370
|
const actualSessionId = stringField(metaPayload, 'session_id') ?? stringField(metaPayload, 'id');
|
|
281
371
|
if (actualSessionId !== this.expectedSessionId) {
|
|
282
372
|
throw new Error(`Codex rollout Session id mismatch: expected ${this.expectedSessionId}`);
|
|
283
373
|
}
|
|
374
|
+
if (this.hasSessionMeta)
|
|
375
|
+
return;
|
|
376
|
+
this.hasSessionMeta = true;
|
|
284
377
|
this.metaCwd = safeCodexCwd(metaPayload?.cwd);
|
|
285
378
|
this.activeModel = stringField(metaPayload, 'model_provider') ?? this.activeModel;
|
|
286
379
|
const timestamp = normalizeEpochMs(envelope.timestamp);
|
|
@@ -288,6 +381,11 @@ class CodexRolloutConverter {
|
|
|
288
381
|
this.lastTimestamp = Math.max(this.lastTimestamp, timestamp);
|
|
289
382
|
return;
|
|
290
383
|
}
|
|
384
|
+
// `hasSessionMeta` is lineage-wide, so the opening file's metadata would
|
|
385
|
+
// otherwise vouch for every later file. Each rollout proves its own.
|
|
386
|
+
if (this.awaitingRolloutMeta) {
|
|
387
|
+
throw new Error(`Codex rollout records precede its Session metadata: expected ${this.expectedSessionId}`);
|
|
388
|
+
}
|
|
291
389
|
const payload = asRecord(envelope.payload);
|
|
292
390
|
if (!payload)
|
|
293
391
|
return;
|
|
@@ -558,20 +656,24 @@ class CodexRolloutConverter {
|
|
|
558
656
|
this.messages.push(message);
|
|
559
657
|
}
|
|
560
658
|
}
|
|
561
|
-
async function* readCodexRolloutRecords(path, sessionId, limits) {
|
|
659
|
+
async function* readCodexRolloutRecords(path, sessionId, limits, cursor = { line: 0, bytes: 0 }, endByteOffset) {
|
|
562
660
|
const handle = await open(path, 'r');
|
|
563
661
|
try {
|
|
564
662
|
const metadata = await handle.stat();
|
|
565
663
|
if (!metadata.isFile())
|
|
566
664
|
throw new Error('Codex rollout is not a regular file');
|
|
567
|
-
if (
|
|
665
|
+
if (endByteOffset !== undefined && endByteOffset > metadata.size) {
|
|
666
|
+
throw new Error(`Codex rollout history base ends past its rollout: expected ${sessionId}`);
|
|
667
|
+
}
|
|
668
|
+
const snapshotBytes = endByteOffset ?? metadata.size;
|
|
669
|
+
if (cursor.bytes + snapshotBytes > limits.maxRolloutBytes) {
|
|
568
670
|
throw new ExternalSessionLimitError('transcript_bytes', limits.maxRolloutBytes, `Codex rollout exceeds ${limits.maxRolloutBytes} bytes`);
|
|
569
671
|
}
|
|
570
|
-
|
|
672
|
+
cursor.bytes += snapshotBytes;
|
|
571
673
|
const pending = [];
|
|
572
674
|
let pendingBytes = 0;
|
|
573
675
|
let observedBytes = 0;
|
|
574
|
-
let line =
|
|
676
|
+
let line = cursor.line;
|
|
575
677
|
if (snapshotBytes > 0) {
|
|
576
678
|
for await (const value of handle.createReadStream({
|
|
577
679
|
autoClose: false,
|
|
@@ -614,10 +716,14 @@ async function* readCodexRolloutRecords(path, sessionId, limits) {
|
|
|
614
716
|
throw new Error('Codex rollout changed while being read');
|
|
615
717
|
if (pendingBytes > 0) {
|
|
616
718
|
line += 1;
|
|
617
|
-
|
|
719
|
+
// Only the rollout still being written can end mid-record; a history
|
|
720
|
+
// base's offset lands on a record boundary, so a partial record there
|
|
721
|
+
// is corrupt.
|
|
722
|
+
const record = parseCodexRolloutLine(pending.length === 1 ? pending[0] : Buffer.concat(pending, pendingBytes), sessionId, line, endByteOffset === undefined);
|
|
618
723
|
if (record)
|
|
619
724
|
yield record;
|
|
620
725
|
}
|
|
726
|
+
cursor.line = line;
|
|
621
727
|
}
|
|
622
728
|
finally {
|
|
623
729
|
await handle.close();
|
|
@@ -874,6 +980,10 @@ async function nextRolloutCatalogBatch(codexHome, query, keyset, limit, maxCandi
|
|
|
874
980
|
const entry = catalogEntryFromRolloutHead(head, candidate);
|
|
875
981
|
if (!entry || !matchesQuery(entry, query))
|
|
876
982
|
continue;
|
|
983
|
+
// A reverted thread's later rollouts repeat its `session_meta`; list the
|
|
984
|
+
// thread once, from its opening rollout.
|
|
985
|
+
if (!isOpeningRolloutFilename(basename(candidate.path), entry.id))
|
|
986
|
+
continue;
|
|
877
987
|
const rolloutPath = await resolvePath(candidate, entry.id);
|
|
878
988
|
if (!rolloutPath)
|
|
879
989
|
continue;
|
|
@@ -1069,9 +1179,107 @@ function matchesQuery(entry, query) {
|
|
|
1069
1179
|
function stateGeneration(path) {
|
|
1070
1180
|
return Number(path.match(/\d+/)?.[0] ?? 0);
|
|
1071
1181
|
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Codex names a thread's opening rollout `rollout-<ts>-<id>.jsonl` and each
|
|
1184
|
+
* rollout a revert starts for it `rollout-<ts>-<id>_<rolloutId>.jsonl`; every
|
|
1185
|
+
* one records the thread's own id in `session_meta`. Only `_` separates the
|
|
1186
|
+
* two ids, since the timestamp and the thread id are `-`-joined.
|
|
1187
|
+
*/
|
|
1072
1188
|
function rolloutFilenameMatchesId(filename, sessionId) {
|
|
1189
|
+
if (!filename.startsWith('rollout-') || !filename.endsWith('.jsonl'))
|
|
1190
|
+
return false;
|
|
1191
|
+
const stem = filename.slice(0, -'.jsonl'.length);
|
|
1192
|
+
if (stem.endsWith(`-${sessionId}`))
|
|
1193
|
+
return true;
|
|
1194
|
+
const marker = `-${sessionId}_`;
|
|
1195
|
+
const childStart = stem.lastIndexOf(marker) + marker.length;
|
|
1196
|
+
return childStart >= marker.length && childStart < stem.length && !stem.includes('_', childStart);
|
|
1197
|
+
}
|
|
1198
|
+
function isOpeningRolloutFilename(filename, sessionId) {
|
|
1073
1199
|
return filename.endsWith(`-${sessionId}.jsonl`);
|
|
1074
1200
|
}
|
|
1201
|
+
/**
|
|
1202
|
+
* The thread and rollout ids in a rollout file name, split as Codex splits
|
|
1203
|
+
* them: at the first `_` after the timestamp. An opening rollout's id is its
|
|
1204
|
+
* thread's.
|
|
1205
|
+
*/
|
|
1206
|
+
function rolloutFileIds(filename) {
|
|
1207
|
+
const ids = /^rollout-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-(.+)\.jsonl$/.exec(filename)?.[1];
|
|
1208
|
+
if (ids === undefined)
|
|
1209
|
+
return undefined;
|
|
1210
|
+
const split = ids.indexOf('_');
|
|
1211
|
+
const threadId = split === -1 ? ids : ids.slice(0, split);
|
|
1212
|
+
const rolloutId = split === -1 ? ids : ids.slice(split + 1);
|
|
1213
|
+
if (!isSafeCodexSessionId(threadId) || !isSafeCodexSessionId(rolloutId))
|
|
1214
|
+
return undefined;
|
|
1215
|
+
return { threadId, rolloutId };
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Every rollout file by rollout id, built from names alone on first use. The
|
|
1219
|
+
* `sessions` copy wins over an `archived_sessions` one, as in Codex, so a
|
|
1220
|
+
* rollout seen in both mid-archive is read once.
|
|
1221
|
+
*/
|
|
1222
|
+
function lazyRolloutIndex(codexHome) {
|
|
1223
|
+
let index;
|
|
1224
|
+
return () => (index ??= indexRollouts(codexHome));
|
|
1225
|
+
}
|
|
1226
|
+
async function indexRollouts(codexHome) {
|
|
1227
|
+
const index = new Map();
|
|
1228
|
+
for (const [root, archived] of [
|
|
1229
|
+
[join(codexHome, 'sessions'), false],
|
|
1230
|
+
[join(codexHome, 'archived_sessions'), true],
|
|
1231
|
+
]) {
|
|
1232
|
+
for await (const candidate of iterateRolloutFiles(root, archived)) {
|
|
1233
|
+
const ids = rolloutFileIds(basename(candidate.path));
|
|
1234
|
+
if (!ids || index.has(ids.rolloutId))
|
|
1235
|
+
continue;
|
|
1236
|
+
index.set(ids.rolloutId, { threadId: ids.threadId, path: candidate.path });
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
return index;
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* The first `session_meta` in a rollout's head: the thread it names and the
|
|
1243
|
+
* history it continues. Reading only the head is enough because Codex writes
|
|
1244
|
+
* that record first, and a rollout that buries it behind other records is
|
|
1245
|
+
* refused while converting rather than trusted here.
|
|
1246
|
+
*/
|
|
1247
|
+
async function readRolloutHeadMeta(path, sessionId) {
|
|
1248
|
+
const head = await readUtf8Prefix(path, CODEX_ROLLOUT_HEAD_BYTES).catch(() => undefined);
|
|
1249
|
+
if (head === undefined)
|
|
1250
|
+
return undefined;
|
|
1251
|
+
for (const line of head.split('\n')) {
|
|
1252
|
+
let record;
|
|
1253
|
+
try {
|
|
1254
|
+
record = JSON.parse(line);
|
|
1255
|
+
}
|
|
1256
|
+
catch {
|
|
1257
|
+
continue;
|
|
1258
|
+
}
|
|
1259
|
+
if (!isRecord(record) || record.type !== 'session_meta')
|
|
1260
|
+
continue;
|
|
1261
|
+
const payload = asRecord(record.payload);
|
|
1262
|
+
return {
|
|
1263
|
+
id: stringField(payload, 'session_id') ?? stringField(payload, 'id'),
|
|
1264
|
+
historyBase: codexHistoryBase(payload?.history_base, sessionId),
|
|
1265
|
+
};
|
|
1266
|
+
}
|
|
1267
|
+
return undefined;
|
|
1268
|
+
}
|
|
1269
|
+
function codexHistoryBase(value, sessionId) {
|
|
1270
|
+
if (value === undefined || value === null)
|
|
1271
|
+
return undefined;
|
|
1272
|
+
const base = asRecord(value);
|
|
1273
|
+
const rolloutId = stringField(base, 'thread_id');
|
|
1274
|
+
const endByteOffset = base?.end_byte_offset;
|
|
1275
|
+
if (!isSafeCodexSessionId(rolloutId) ||
|
|
1276
|
+
typeof endByteOffset !== 'number' ||
|
|
1277
|
+
!Number.isSafeInteger(endByteOffset) ||
|
|
1278
|
+
endByteOffset <= 0) {
|
|
1279
|
+
throw new Error(`Invalid Codex rollout history base: expected ${sessionId}`);
|
|
1280
|
+
}
|
|
1281
|
+
return { rolloutId, endByteOffset };
|
|
1282
|
+
}
|
|
1075
1283
|
function generatedCodexId(sessionId, kind, line) {
|
|
1076
1284
|
return `codex-${sessionId}-${kind}-${line}`;
|
|
1077
1285
|
}
|
|
@@ -51,7 +51,7 @@ export class ExternalSessionImporter {
|
|
|
51
51
|
sanitizeExternalSessionTitle(request.sourceSessionId);
|
|
52
52
|
if (name.length === 0)
|
|
53
53
|
throw new Error('External Session title is empty after sanitization');
|
|
54
|
-
const cwd =
|
|
54
|
+
const cwd = request.target.cwd ?? sanitizeExternalSessionCwd(external.metadata.cwd);
|
|
55
55
|
return this.sessions.createImportedSession({
|
|
56
56
|
...request.target,
|
|
57
57
|
cwd,
|
|
@@ -37,10 +37,12 @@ const CHANGE_SETTLE_MS = 150;
|
|
|
37
37
|
export class McpConfigSourceError extends Error {
|
|
38
38
|
reason;
|
|
39
39
|
version;
|
|
40
|
-
|
|
40
|
+
path;
|
|
41
|
+
constructor(reason, version, message = reason, path) {
|
|
41
42
|
super(message);
|
|
42
43
|
this.reason = reason;
|
|
43
44
|
this.version = version;
|
|
45
|
+
this.path = path;
|
|
44
46
|
this.name = 'McpConfigSourceError';
|
|
45
47
|
}
|
|
46
48
|
}
|
|
@@ -132,6 +134,11 @@ export function normalizeMcpConfig(value) {
|
|
|
132
134
|
}
|
|
133
135
|
return { version: MCP_CONFIG_VERSION, mcpServers: { ...mcpServers } };
|
|
134
136
|
}
|
|
137
|
+
// Accept an optional UTF-8 BOM at the document boundary only. Keep size checks
|
|
138
|
+
// on the original input and leave all other JSON/schema validation unchanged.
|
|
139
|
+
function parseMcpJson(source) {
|
|
140
|
+
return JSON.parse(source.startsWith('\uFEFF') ? source.slice(1) : source);
|
|
141
|
+
}
|
|
135
142
|
/** Parse either a wrapped mcp.json document or a direct server map while
|
|
136
143
|
* preserving the source wrapper version until schema validation completes.
|
|
137
144
|
* Import presentation belongs to the caller; config interpretation lives here
|
|
@@ -142,7 +149,7 @@ export function normalizeMcpImport(source) {
|
|
|
142
149
|
}
|
|
143
150
|
let value;
|
|
144
151
|
try {
|
|
145
|
-
value =
|
|
152
|
+
value = parseMcpJson(source);
|
|
146
153
|
}
|
|
147
154
|
catch {
|
|
148
155
|
throw new McpConfigSourceError('invalid-json', undefined, 'MCP config must be valid JSON');
|
|
@@ -219,7 +226,19 @@ class FileMcpConfigStore {
|
|
|
219
226
|
if (Buffer.byteLength(text, 'utf8') > MAX_CONFIG_BYTES) {
|
|
220
227
|
throw new Error('MCP config exceeds 1 MiB');
|
|
221
228
|
}
|
|
222
|
-
|
|
229
|
+
let persisted;
|
|
230
|
+
try {
|
|
231
|
+
persisted = parseMcpJson(text);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
if (!(error instanceof SyntaxError))
|
|
235
|
+
throw error;
|
|
236
|
+
// JSON.parse can quote credentials in its message. Report the location
|
|
237
|
+
// and recovery action without retaining those source bytes in an error.
|
|
238
|
+
throw new McpConfigSourceError('invalid-json', undefined, `MCP config at ${this.path} contains invalid JSON. The file was not modified. ` +
|
|
239
|
+
'Close the app, back up and repair this file before retrying.', this.path);
|
|
240
|
+
}
|
|
241
|
+
return normalizeMcpConfig(persisted);
|
|
223
242
|
}
|
|
224
243
|
async readOrCreate() {
|
|
225
244
|
try {
|