@nowcrew/daemon 0.6.19 → 0.6.21
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/atomic-no-replace-rename.js +91 -0
- package/dist/completion-retransmitter-logging.js +16 -0
- package/dist/completion-retransmitter.js +39 -4
- package/dist/control-plane-url.js +4 -2
- package/dist/directory-projection-publication.js +105 -0
- package/dist/directory-projection.js +20 -4
- package/dist/execution-journal.js +40 -4
- package/dist/execution-posix-stop-proof.js +82 -0
- package/dist/execution-runner.js +68 -8
- package/dist/local-executor.js +67 -52
- package/dist/machine-info.js +8 -5
- package/dist/project-skills/capability.js +109 -0
- package/dist/project-skills/controller-convergence.js +57 -0
- package/dist/project-skills/controller.js +80 -24
- package/dist/project-skills/initialized-reconciler.js +4 -4
- package/dist/project-skills/projection-state-domain.js +19 -2
- package/dist/project-skills/projection-state-store.js +3 -2
- package/dist/project-skills/projection-state-transaction.js +5 -1
- package/dist/project-skills/projection-state.js +1 -1
- package/dist/project-skills/reconciler.js +275 -102
- package/dist/project-skills/runtime-launch.js +102 -0
- package/dist/project-skills/runtime-root-bootstrap.js +47 -0
- package/dist/project-skills/runtime-root-domain.js +268 -0
- package/dist/project-skills/runtime-root-gc.js +293 -0
- package/dist/project-skills/runtime-root-lease-artifact.js +46 -0
- package/dist/project-skills/runtime-root-leases.js +487 -0
- package/dist/project-skills/runtime-root-source-identity.js +60 -0
- package/dist/project-skills/runtime-root-startup.js +49 -0
- package/dist/project-skills/runtime-root-state-artifact-domain.js +143 -0
- package/dist/project-skills/runtime-root-state-index.js +356 -0
- package/dist/project-skills/runtime-root-store.js +722 -0
- package/dist/project-skills/serve-capability.js +28 -0
- package/dist/project-skills/serve-startup.js +22 -0
- package/dist/project-skills/types.js +1 -0
- package/dist/runtimes/codex-home-migration-cli.js +26 -0
- package/dist/runtimes/codex-home-migration.js +112 -0
- package/dist/runtimes/codex-home.js +200 -17
- package/dist/serve.js +60 -79
- package/dist/supervised-runtime.js +1 -5
- package/package.json +2 -1
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { WebSocket } from "ws";
|
|
2
|
+
import { daemonCapabilityBindings, } from "../machine-info.js";
|
|
3
|
+
import { prepareProjectSkillConnectionCapabilities, } from "./capability.js";
|
|
4
|
+
export function createProjectSkillConnectionCapabilityProvider(input) {
|
|
5
|
+
return async () => daemonCapabilityBindings(input.platform, await prepareProjectSkillConnectionCapabilities(input));
|
|
6
|
+
}
|
|
7
|
+
export function createProjectSkillMachineHelloPublisher(input) {
|
|
8
|
+
let latest = null;
|
|
9
|
+
let latestSocket = null;
|
|
10
|
+
const republish = () => {
|
|
11
|
+
if (latest === null || latestSocket?.readyState !== WebSocket.OPEN)
|
|
12
|
+
return;
|
|
13
|
+
const hello = input.effective(latest, input.status());
|
|
14
|
+
try {
|
|
15
|
+
latestSocket.send(JSON.stringify(hello));
|
|
16
|
+
input.onSent(hello);
|
|
17
|
+
}
|
|
18
|
+
catch { /* 非 OPEN,忽略 */ }
|
|
19
|
+
};
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
publish: (hello, socket) => {
|
|
22
|
+
latest = hello;
|
|
23
|
+
latestSocket = socket;
|
|
24
|
+
republish();
|
|
25
|
+
},
|
|
26
|
+
republish,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createExecutionJournal } from "../execution-journal.js";
|
|
2
|
+
import { reconcileExecutionJournal } from "../execution-recovery.js";
|
|
3
|
+
import { dslog, flushSlog } from "../slog.js";
|
|
4
|
+
export async function initializeProjectSkillProtectedExecutionJournal(input) {
|
|
5
|
+
return input.startup.initializeAfterLeaseProtection(input.config.agentsRoot, async (snapshot) => {
|
|
6
|
+
input.protectedExecutionIds.clear();
|
|
7
|
+
for (const executionId of snapshot)
|
|
8
|
+
input.protectedExecutionIds.add(executionId);
|
|
9
|
+
const journal = input.injectedJournal ?? createExecutionJournal(input.config.agentsRoot, {
|
|
10
|
+
protectedExecutionIds: () => input.protectedExecutionIds,
|
|
11
|
+
});
|
|
12
|
+
await reconcileExecutionJournal(journal, {
|
|
13
|
+
agentsRoot: input.config.agentsRoot,
|
|
14
|
+
serverUrl: input.config.serverUrl,
|
|
15
|
+
...(input.profileName === undefined ? {} : { profileName: input.profileName }),
|
|
16
|
+
log: dslog,
|
|
17
|
+
flush: flushSlog,
|
|
18
|
+
writeStderr: (line) => process.stderr.write(line),
|
|
19
|
+
});
|
|
20
|
+
return journal;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const PROJECT_SKILLS_CAPABILITY = "project_skills_v1";
|
|
2
|
+
export const PROJECT_SKILL_PROJECTION_V2_CAPABILITY = "project_skill_projection_v2";
|
|
2
3
|
export const MAX_PROJECT_ID_LENGTH = 64;
|
|
3
4
|
export const MAX_PROJECT_SKILL_NAME_LENGTH = 128;
|
|
4
5
|
export const MAX_PROJECT_SKILL_DESCRIPTION_LENGTH = 1_000;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { migrateCodexHome } from "./codex-home-migration.js";
|
|
4
|
+
const { values } = parseArgs({
|
|
5
|
+
args: process.argv.slice(2),
|
|
6
|
+
strict: true,
|
|
7
|
+
options: {
|
|
8
|
+
"agent-home": { type: "string" },
|
|
9
|
+
"source-home": { type: "string" },
|
|
10
|
+
"backup-root": { type: "string" },
|
|
11
|
+
yes: { type: "boolean", default: false },
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
function required(value, name) {
|
|
15
|
+
if (!value?.trim())
|
|
16
|
+
throw new Error(`Missing --${name}`);
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
if (!values.yes)
|
|
20
|
+
throw new Error("Refusing Codex home migration without --yes");
|
|
21
|
+
const result = await migrateCodexHome({
|
|
22
|
+
agentHome: required(values["agent-home"], "agent-home"),
|
|
23
|
+
sourceHome: required(values["source-home"], "source-home"),
|
|
24
|
+
backupRoot: required(values["backup-root"], "backup-root"),
|
|
25
|
+
});
|
|
26
|
+
process.stdout.write(`${result.status}${result.backupDir ? ` backup=${result.backupDir}` : ""}\n`);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import { lstat, mkdir, readlink, rename, rm, symlink, unlink, } from "node:fs/promises";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { atomicPrivateWrite } from "../atomic-private-write.js";
|
|
7
|
+
import { defaultCodexHome, materializeDefaultCodexHome } from "./codex-home.js";
|
|
8
|
+
const execFileAsync = promisify(execFile);
|
|
9
|
+
const SQLITE_FILES = ["state_5.sqlite", "state_5.sqlite-wal", "state_5.sqlite-shm"];
|
|
10
|
+
const MARKER_NAME = ".codex-home-migration.json";
|
|
11
|
+
async function exists(path) {
|
|
12
|
+
try {
|
|
13
|
+
await lstat(path);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (error.code === "ENOENT")
|
|
18
|
+
return false;
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function assertNoOpenSqliteFiles(codexHome) {
|
|
23
|
+
for (const name of SQLITE_FILES) {
|
|
24
|
+
const path = join(codexHome, name);
|
|
25
|
+
if (!(await exists(path)))
|
|
26
|
+
continue;
|
|
27
|
+
try {
|
|
28
|
+
const result = await execFileAsync("lsof", ["-t", "--", path], { encoding: "utf8" });
|
|
29
|
+
if (result.stdout.trim()) {
|
|
30
|
+
throw new Error(`Codex SQLite file is still open: ${path}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
const code = error.code;
|
|
35
|
+
if (code === 1)
|
|
36
|
+
continue;
|
|
37
|
+
if (code === "ENOENT")
|
|
38
|
+
throw new Error("lsof is required to verify Codex home quiescence");
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function symlinkTarget(path) {
|
|
44
|
+
try {
|
|
45
|
+
const info = await lstat(path);
|
|
46
|
+
if (!info.isSymbolicLink())
|
|
47
|
+
return null;
|
|
48
|
+
return await readlink(path);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
if (error.code === "ENOENT")
|
|
52
|
+
return null;
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export async function migrateCodexHome(input, dependencies = {}) {
|
|
57
|
+
const agentHome = resolve(input.agentHome);
|
|
58
|
+
const sourceHome = resolve(input.sourceHome);
|
|
59
|
+
const backupRoot = resolve(input.backupRoot);
|
|
60
|
+
const codexHome = defaultCodexHome(agentHome);
|
|
61
|
+
const sessions = join(codexHome, "sessions");
|
|
62
|
+
const index = join(codexHome, "session_index.jsonl");
|
|
63
|
+
const expectedSessions = resolve(sourceHome, "sessions");
|
|
64
|
+
const expectedIndex = resolve(sourceHome, "session_index.jsonl");
|
|
65
|
+
const marker = join(codexHome, MARKER_NAME);
|
|
66
|
+
const sessionLink = await symlinkTarget(sessions);
|
|
67
|
+
const indexLink = await symlinkTarget(index);
|
|
68
|
+
if (sessionLink === null && indexLink === null && await exists(marker)) {
|
|
69
|
+
return { status: "already-migrated" };
|
|
70
|
+
}
|
|
71
|
+
if (sessionLink === null || indexLink === null) {
|
|
72
|
+
throw new Error("Refusing migration: Codex home is not in the complete legacy symlink layout");
|
|
73
|
+
}
|
|
74
|
+
if (resolve(codexHome, sessionLink) !== expectedSessions || resolve(codexHome, indexLink) !== expectedIndex) {
|
|
75
|
+
throw new Error("Refusing migration: legacy symlinks do not point to the configured source home");
|
|
76
|
+
}
|
|
77
|
+
await (dependencies.ensureQuiesced ?? assertNoOpenSqliteFiles)(codexHome);
|
|
78
|
+
const stamp = (dependencies.now ?? (() => new Date()))().toISOString().replaceAll(/[^0-9]/g, "").slice(0, 14);
|
|
79
|
+
const backupDir = join(backupRoot, `${stamp}-${dependencies.id?.() ?? randomUUID()}`);
|
|
80
|
+
await mkdir(backupDir, { recursive: true, mode: 0o700 });
|
|
81
|
+
await symlink(sessionLink, join(backupDir, "sessions"), "dir");
|
|
82
|
+
await symlink(indexLink, join(backupDir, "session_index.jsonl"), "file");
|
|
83
|
+
const moved = [];
|
|
84
|
+
try {
|
|
85
|
+
for (const name of SQLITE_FILES) {
|
|
86
|
+
const current = join(codexHome, name);
|
|
87
|
+
if (await exists(current)) {
|
|
88
|
+
await rename(current, join(backupDir, name));
|
|
89
|
+
moved.push(name);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
await materializeDefaultCodexHome(agentHome, sourceHome);
|
|
93
|
+
await atomicPrivateWrite(marker, JSON.stringify({
|
|
94
|
+
version: 1,
|
|
95
|
+
migrated_at: new Date().toISOString(),
|
|
96
|
+
source_home: sourceHome,
|
|
97
|
+
backup_dir: backupDir,
|
|
98
|
+
}) + "\n");
|
|
99
|
+
return { status: "migrated", backupDir };
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
for (const name of moved.reverse()) {
|
|
103
|
+
await rename(join(backupDir, name), join(codexHome, name)).catch(() => undefined);
|
|
104
|
+
}
|
|
105
|
+
await rm(sessions, { recursive: true, force: true });
|
|
106
|
+
await unlink(index).catch(() => undefined);
|
|
107
|
+
await symlink(sessionLink, sessions, "dir").catch(() => undefined);
|
|
108
|
+
await symlink(indexLink, index, "file").catch(() => undefined);
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export const codexHomeMigrationMarkerName = MARKER_NAME;
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import { chmod, lstat, mkdir, symlink } from "node:fs/promises";
|
|
2
|
-
import {
|
|
1
|
+
import { chmod, copyFile, lstat, mkdir, open, readdir, readlink, rename, rm, symlink, unlink, link, writeFile, } from "node:fs/promises";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
3
4
|
const PRIVATE_CODEX_HOME = ".codex";
|
|
4
|
-
const SHARED_CODEX_ENTRIES = [
|
|
5
|
-
{ name: "auth.json", type: "file" },
|
|
6
|
-
{ name: "sessions", type: "dir" },
|
|
7
|
-
{ name: "session_index.jsonl", type: "file" },
|
|
8
|
-
];
|
|
9
5
|
export function defaultCodexHome(homeDir) {
|
|
10
6
|
return resolve(homeDir, PRIVATE_CODEX_HOME);
|
|
11
7
|
}
|
|
12
|
-
async function linkIfMissing(source, target
|
|
8
|
+
async function linkIfMissing(source, target) {
|
|
13
9
|
try {
|
|
14
10
|
await lstat(target);
|
|
15
11
|
return;
|
|
@@ -27,24 +23,211 @@ async function linkIfMissing(source, target, type) {
|
|
|
27
23
|
throw error;
|
|
28
24
|
}
|
|
29
25
|
try {
|
|
30
|
-
await symlink(source, target,
|
|
26
|
+
await symlink(source, target, "file");
|
|
31
27
|
}
|
|
32
28
|
catch (error) {
|
|
33
29
|
if (error.code !== "EEXIST")
|
|
34
30
|
throw error;
|
|
35
31
|
}
|
|
36
32
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
function isBelow(root, candidate) {
|
|
34
|
+
const child = relative(root, candidate);
|
|
35
|
+
return child !== "" && child !== ".." && !child.startsWith(`..${sep}`) && !child.startsWith(sep);
|
|
36
|
+
}
|
|
37
|
+
async function isExactSymlink(target, expectedSource) {
|
|
38
|
+
const linkTarget = await readlink(target);
|
|
39
|
+
return resolve(dirname(target), linkTarget) === resolve(expectedSource);
|
|
40
|
+
}
|
|
41
|
+
async function ensurePrivateSessions(target, legacySource) {
|
|
42
|
+
try {
|
|
43
|
+
const current = await lstat(target);
|
|
44
|
+
if (current.isSymbolicLink()) {
|
|
45
|
+
if (legacySource === undefined || !(await isExactSymlink(target, legacySource))) {
|
|
46
|
+
throw new Error(`Refusing to replace unexpected Codex sessions symlink: ${target}`);
|
|
47
|
+
}
|
|
48
|
+
const replacement = `${target}.private-${process.pid}-${Math.random().toString(36).slice(2)}`;
|
|
49
|
+
const legacyBackup = `${target}.legacy-${process.pid}-${Math.random().toString(36).slice(2)}`;
|
|
50
|
+
try {
|
|
51
|
+
await mkdir(replacement, { mode: 0o700 });
|
|
52
|
+
await rename(target, legacyBackup);
|
|
53
|
+
try {
|
|
54
|
+
await rename(replacement, target);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
await rename(legacyBackup, target).catch(() => undefined);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
await rm(replacement, { recursive: true, force: true }).catch(() => undefined);
|
|
63
|
+
await rm(legacyBackup, { recursive: true, force: true }).catch(() => undefined);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (!current.isDirectory()) {
|
|
67
|
+
throw new Error(`Codex sessions target is not a directory: ${target}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error.code !== "ENOENT")
|
|
72
|
+
throw error;
|
|
73
|
+
await mkdir(target, { mode: 0o700 });
|
|
74
|
+
}
|
|
75
|
+
await chmod(target, 0o700);
|
|
76
|
+
}
|
|
77
|
+
async function atomicCopyFile(source, target, mode) {
|
|
78
|
+
const parent = dirname(target);
|
|
79
|
+
await mkdir(parent, { recursive: true, mode: 0o700 });
|
|
80
|
+
const temporary = `${target}.tmp-${process.pid}-${Math.random().toString(36).slice(2)}`;
|
|
81
|
+
try {
|
|
82
|
+
await copyFile(source, temporary, constants.COPYFILE_EXCL);
|
|
83
|
+
await chmod(temporary, mode);
|
|
84
|
+
try {
|
|
85
|
+
await link(temporary, target);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (error.code !== "EEXIST")
|
|
90
|
+
throw error;
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
await unlink(temporary).catch(() => undefined);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function ensurePrivateIndex(target, source) {
|
|
99
|
+
let current;
|
|
100
|
+
try {
|
|
101
|
+
current = await lstat(target);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error.code !== "ENOENT")
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
if (current?.isSymbolicLink()) {
|
|
108
|
+
if (source === undefined || !(await isExactSymlink(target, source))) {
|
|
109
|
+
throw new Error(`Refusing to replace unexpected Codex session index symlink: ${target}`);
|
|
110
|
+
}
|
|
111
|
+
const replacement = `${target}.private-${process.pid}-${Math.random().toString(36).slice(2)}`;
|
|
112
|
+
const legacyBackup = `${target}.legacy-${process.pid}-${Math.random().toString(36).slice(2)}`;
|
|
113
|
+
try {
|
|
114
|
+
try {
|
|
115
|
+
await lstat(source);
|
|
116
|
+
await copyFile(source, replacement, constants.COPYFILE_EXCL);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (error.code !== "ENOENT")
|
|
120
|
+
throw error;
|
|
121
|
+
await writeFile(replacement, "", { encoding: "utf8", flag: "wx" });
|
|
122
|
+
}
|
|
123
|
+
await chmod(replacement, 0o600);
|
|
124
|
+
await rename(target, legacyBackup);
|
|
125
|
+
try {
|
|
126
|
+
await rename(replacement, target);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
await rename(legacyBackup, target).catch(() => undefined);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
await rm(replacement, { force: true }).catch(() => undefined);
|
|
135
|
+
await rm(legacyBackup, { force: true }).catch(() => undefined);
|
|
136
|
+
}
|
|
137
|
+
current = undefined;
|
|
138
|
+
}
|
|
139
|
+
if (current !== undefined && !current.isFile()) {
|
|
140
|
+
throw new Error(`Codex session index target is not a regular file: ${target}`);
|
|
141
|
+
}
|
|
142
|
+
if (current !== undefined) {
|
|
143
|
+
await chmod(target, 0o600);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (source !== undefined) {
|
|
147
|
+
try {
|
|
148
|
+
await lstat(source);
|
|
149
|
+
if (await atomicCopyFile(source, target, 0o600))
|
|
150
|
+
return;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (error.code !== "ENOENT")
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const handle = await open(target, "wx", 0o600).catch((error) => {
|
|
159
|
+
if (error.code === "EEXIST")
|
|
160
|
+
return null;
|
|
161
|
+
throw error;
|
|
162
|
+
});
|
|
163
|
+
await handle?.close();
|
|
164
|
+
}
|
|
165
|
+
async function findRollout(root, sessionId) {
|
|
166
|
+
const entries = await readdir(root, { withFileTypes: true }).catch((error) => {
|
|
167
|
+
if (error.code === "ENOENT")
|
|
168
|
+
return [];
|
|
169
|
+
throw error;
|
|
170
|
+
});
|
|
171
|
+
const matches = [];
|
|
172
|
+
for (const entry of entries) {
|
|
173
|
+
const path = join(root, entry.name);
|
|
174
|
+
if (entry.isDirectory()) {
|
|
175
|
+
const nested = await findRollout(path, sessionId);
|
|
176
|
+
if (nested !== null)
|
|
177
|
+
matches.push(nested);
|
|
178
|
+
}
|
|
179
|
+
else if (entry.isFile() && entry.name.includes(sessionId)) {
|
|
180
|
+
matches.push(path);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (matches.length > 1)
|
|
184
|
+
throw new Error(`Multiple Codex rollout files match session ${sessionId}`);
|
|
185
|
+
return matches[0] ?? null;
|
|
186
|
+
}
|
|
187
|
+
async function hydrateResumeRollout(sourceSessions, privateSessions, sessionId) {
|
|
188
|
+
if (sessionId.includes("/") || sessionId.includes("\\") || sessionId.includes("..")) {
|
|
189
|
+
throw new Error("Invalid Codex resume session id");
|
|
190
|
+
}
|
|
191
|
+
const sourcePath = await findRollout(sourceSessions, sessionId);
|
|
192
|
+
if (sourcePath === null)
|
|
193
|
+
return;
|
|
194
|
+
const relativePath = relative(sourceSessions, sourcePath);
|
|
195
|
+
const destination = resolve(privateSessions, relativePath);
|
|
196
|
+
if (!isBelow(resolve(privateSessions), destination)) {
|
|
197
|
+
throw new Error("Codex resume rollout escaped the private sessions directory");
|
|
198
|
+
}
|
|
199
|
+
let existing;
|
|
200
|
+
try {
|
|
201
|
+
existing = await lstat(destination);
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
if (error.code !== "ENOENT")
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
if (existing !== undefined) {
|
|
208
|
+
if (!existing.isFile() || existing.isSymbolicLink()) {
|
|
209
|
+
throw new Error(`Refusing to replace existing Codex rollout: ${destination}`);
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const mode = (await lstat(sourcePath)).mode & 0o777;
|
|
214
|
+
await atomicCopyFile(sourcePath, destination, mode);
|
|
215
|
+
}
|
|
216
|
+
/** Isolate Codex high-volume state per Agent while preserving auth and exact resume. */
|
|
217
|
+
export async function materializeDefaultCodexHome(agentHome, sourceHome, resumeSessionId) {
|
|
42
218
|
const codexHome = defaultCodexHome(agentHome);
|
|
43
219
|
await mkdir(codexHome, { recursive: true, mode: 0o700 });
|
|
44
220
|
await chmod(codexHome, 0o700);
|
|
45
221
|
const source = sourceHome === undefined ? null : resolve(sourceHome);
|
|
46
|
-
if (source
|
|
47
|
-
|
|
48
|
-
|
|
222
|
+
if (source !== null && source !== codexHome) {
|
|
223
|
+
await linkIfMissing(join(source, "auth.json"), join(codexHome, "auth.json"));
|
|
224
|
+
}
|
|
225
|
+
const sourceSessions = source === null || source === codexHome ? undefined : join(source, "sessions");
|
|
226
|
+
await ensurePrivateSessions(join(codexHome, "sessions"), sourceSessions);
|
|
227
|
+
const sourceIndex = source === null || source === codexHome ? undefined : join(source, "session_index.jsonl");
|
|
228
|
+
await ensurePrivateIndex(join(codexHome, "session_index.jsonl"), sourceIndex);
|
|
229
|
+
if (sourceSessions !== undefined && resumeSessionId) {
|
|
230
|
+
await hydrateResumeRollout(sourceSessions, join(codexHome, "sessions"), resumeSessionId);
|
|
231
|
+
}
|
|
49
232
|
return codexHome;
|
|
50
233
|
}
|