docks-kit 0.22.13 → 0.22.20
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/AGENTS.md +94 -80
- package/README.md +7 -7
- package/cli/docs/command-guards.md +7 -3
- package/cli/docs/flags.md +59 -9
- package/cli/docs/install.md +2 -2
- package/cli/docs/omp-context.md +14 -8
- package/cli/docs/omp-models.md +1 -1
- package/cli/docs/overview.md +1 -1
- package/cli/docs/platforms.md +4 -2
- package/cli/docs/plugins.md +8 -5
- package/cli/docs/sync-layers.md +6 -4
- package/cli/docs/toolchain.md +24 -14
- package/cli/src/argv.ts +7 -4
- package/cli/src/argvValidate.ts +2 -16
- package/cli/src/commands/docs.ts +1 -1
- package/cli/src/commands/omp.ts +56 -15
- package/cli/src/commands/sync.ts +6 -6
- package/cli/src/commands/tidy.ts +83 -26
- package/cli/src/engine-native/DESIGN.md +93 -56
- package/cli/src/engine-native/atomicWrite.ts +168 -8
- package/cli/src/engine-native/codexConfig.ts +2 -6
- package/cli/src/engine-native/codexPlugins.ts +1 -1
- package/cli/src/engine-native/codexSync.ts +137 -26
- package/cli/src/engine-native/codexToml.ts +4 -86
- package/cli/src/engine-native/deps.ts +51 -51
- package/cli/src/engine-native/exec.ts +3 -0
- package/cli/src/engine-native/index.ts +1 -1
- package/cli/src/engine-native/kitLock.ts +121 -0
- package/cli/src/engine-native/modes.ts +49 -31
- package/cli/src/engine-native/ompChild.ts +26 -0
- package/cli/src/engine-native/ompFastMode.ts +14 -7
- package/cli/src/engine-native/ompFileDeploy.ts +12 -6
- package/cli/src/engine-native/ompMarketplace.ts +25 -18
- package/cli/src/engine-native/ompPlugins.ts +23 -14
- package/cli/src/engine-native/ompRemovals.ts +51 -23
- package/cli/src/engine-native/ompSync.ts +66 -32
- package/cli/src/engine-native/os/targets.ts +16 -4
- package/cli/src/engine-native/os/types.ts +1 -1
- package/cli/src/engine-native/parseArgs.ts +27 -17
- package/cli/src/engine-native/processes.ts +16 -0
- package/cli/src/engine-native/scratch.ts +18 -0
- package/cli/src/engine-native/skillsInstall.ts +27 -2
- package/cli/src/engine-native/skillsPrune.ts +51 -49
- package/cli/src/engine-native/skillsRecords.ts +138 -0
- package/cli/src/engine-native/skillsSync.ts +16 -13
- package/cli/src/engine-native/syncDispatch.ts +186 -4
- package/cli/src/engine-native/tidy.ts +102 -119
- package/cli/src/engine-native/tomlLines.ts +92 -0
- package/cli/src/generated/sotPayload.ts +4 -4
- package/cli/src/main.ts +5 -30
- package/cli/src/manifests.ts +24 -4
- package/package.json +2 -2
- package/cli/src/engine-native/tidyLock.ts +0 -163
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* EngineNative `sync agents` pipeline: universal-skill bootstrap
|
|
3
3
|
* (`npx skills@<pin> add`), Claude symlink healing, --prune reconcile against
|
|
4
|
-
* the kit-
|
|
4
|
+
* the kit-owned record, and the record writes.
|
|
5
5
|
*
|
|
6
6
|
* Split: manifest parsing lives in `skillsManifest.ts`, link mechanics and
|
|
7
|
-
* healing in `skillsLinks.ts`, the install pass in `skillsInstall.ts`,
|
|
8
|
-
*
|
|
9
|
-
* orchestration and re-exports the public surface so
|
|
10
|
-
* keep working.
|
|
7
|
+
* healing in `skillsLinks.ts`, the install pass in `skillsInstall.ts`, the
|
|
8
|
+
* ownership records in `skillsRecords.ts`, and the prune in `skillsPrune.ts`.
|
|
9
|
+
* This module keeps the orchestration and re-exports the public surface so
|
|
10
|
+
* existing import paths keep working.
|
|
11
11
|
*/
|
|
12
12
|
import { mkdirSync } from "node:fs";
|
|
13
13
|
import { payloadText } from "../payload";
|
|
14
14
|
import { p } from "./exec";
|
|
15
15
|
import type { Ctx } from "./index";
|
|
16
16
|
import { syncUniversal, type SkillsState } from "./skillsInstall";
|
|
17
|
-
import {
|
|
17
|
+
import { reconcileRemovals } from "./skillsPrune";
|
|
18
|
+
import { readSkillRecords, settlePendingSlug, writeSkillRecords } from "./skillsRecords";
|
|
18
19
|
|
|
19
20
|
export type { SkillsState } from "./skillsInstall";
|
|
20
21
|
export { COPY_MARKER, linkOrCopy, type LinkOutcome } from "./skillsLinks";
|
|
@@ -24,16 +25,18 @@ export async function skillsSync(ctx: Ctx): Promise<SkillsState> {
|
|
|
24
25
|
const state: SkillsState = { present: 0 };
|
|
25
26
|
const skillsDir = p(ctx.agentsDir, "skills");
|
|
26
27
|
const manifest = payloadText("SoT/.agents/skills.txt");
|
|
27
|
-
const
|
|
28
|
+
const records = readSkillRecords(ctx.agentsDir);
|
|
28
29
|
|
|
29
30
|
if (!ctx.dryRun) mkdirSync(skillsDir, { recursive: true });
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
// A pending slug that an earlier sync left: its install ran, but its record did not.
|
|
33
|
+
for (const slug of [...records.pending]) settlePendingSlug(records, skillsDir, slug);
|
|
34
|
+
writeSkillRecords(ctx, records);
|
|
35
|
+
|
|
36
|
+
await syncUniversal(ctx, state, skillsDir, manifest, records);
|
|
37
|
+
// An owned slug leaves the record only after a successful --prune removes it.
|
|
38
|
+
if (ctx.prune) reconcileRemovals(ctx, manifest, records);
|
|
39
|
+
writeSkillRecords(ctx, records);
|
|
37
40
|
return state;
|
|
38
41
|
}
|
|
39
42
|
|
|
@@ -4,14 +4,28 @@
|
|
|
4
4
|
* Split from index.ts; the `sync`/`model`/`toolchain` entry routing still
|
|
5
5
|
* lives there.
|
|
6
6
|
*/
|
|
7
|
+
import { existsSync, mkdirSync, realpathSync } from "node:fs";
|
|
8
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
9
|
+
|
|
7
10
|
import { payloadText } from "../payload";
|
|
8
11
|
import { claudeNextSteps, claudeSummary, claudeSync, type ClaudeRuntimeState } from "./claudeSync";
|
|
9
12
|
import { preflightClaudeSettings } from "./claudeSettings";
|
|
13
|
+
import { codexHome } from "./codexPaths";
|
|
10
14
|
import { codexNextSteps, codexSummary, codexSync } from "./codexSync";
|
|
11
15
|
import type { Ctx } from "./index";
|
|
12
|
-
import {
|
|
16
|
+
import { compareCodepoints } from "./jq";
|
|
17
|
+
import { acquireKitLock, releaseKitLock, type KitLock } from "./kitLock";
|
|
18
|
+
import {
|
|
19
|
+
ompNextSteps,
|
|
20
|
+
ompSummary,
|
|
21
|
+
ompSync,
|
|
22
|
+
ompWriteFolders,
|
|
23
|
+
type OmpState,
|
|
24
|
+
type OmpWriteFolders,
|
|
25
|
+
} from "./ompSync";
|
|
13
26
|
import { skillsNextSteps, skillsSummary, skillsSync, type SkillsState } from "./skillsSync";
|
|
14
27
|
import { ExitError, parseArgs, validateModifierFlags } from "./parseArgs";
|
|
28
|
+
import { folderSyncLockPath, syncLockPath } from "./scratch";
|
|
15
29
|
import {
|
|
16
30
|
runBounded,
|
|
17
31
|
syncConcurrencyForManifest,
|
|
@@ -19,8 +33,158 @@ import {
|
|
|
19
33
|
type SyncTask,
|
|
20
34
|
} from "./syncConcurrency";
|
|
21
35
|
|
|
36
|
+
/** The default time that a sync or a model write waits for another sync to release the sync lock. */
|
|
37
|
+
export const SYNC_LOCK_WAIT_MS = 120_000;
|
|
38
|
+
const SYNC_LOCK_WAIT_ERROR = "DOCKS_KIT_SYNC_LOCK_WAIT_MS must be a non-negative safe integer";
|
|
39
|
+
|
|
40
|
+
/** Print one error line, as `Logger.err` does. */
|
|
41
|
+
type ErrorSink = (message: string) => void;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The sync lock wait bound: `DOCKS_KIT_SYNC_LOCK_WAIT_MS` in ms, or
|
|
45
|
+
* `SYNC_LOCK_WAIT_MS` when unset or empty. A value above
|
|
46
|
+
* `Number.MAX_SAFE_INTEGER` exits 2: `Number` would round it or make it
|
|
47
|
+
* Infinity, and an Infinity wait never ends.
|
|
48
|
+
*/
|
|
49
|
+
function syncLockWaitMs(err: ErrorSink): number {
|
|
50
|
+
const configured = process.env["DOCKS_KIT_SYNC_LOCK_WAIT_MS"];
|
|
51
|
+
if (configured === undefined || configured === "") return SYNC_LOCK_WAIT_MS;
|
|
52
|
+
if (/^\d+$/.test(configured) && Number.isSafeInteger(Number(configured))) {
|
|
53
|
+
return Number(configured);
|
|
54
|
+
}
|
|
55
|
+
err(SYNC_LOCK_WAIT_ERROR);
|
|
56
|
+
throw new ExitError(2);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The tokens that skip the sync lock. `-h` and `--help` make `parseArgs`
|
|
61
|
+
* print the usage or an error and exit before it reads `kit.db`. A
|
|
62
|
+
* `--dry-run` sync writes no kit file except a `kit.db` migration, which
|
|
63
|
+
* `kitDb.ts` makes safe for parallel runs.
|
|
64
|
+
*/
|
|
65
|
+
const UNLOCKED_TOKENS: Record<string, true> = { "--dry-run": true, "-h": true, "--help": true };
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Run `body` under each lock of `paths`, taken in that order. When another
|
|
69
|
+
* sync keeps a lock past the wait bound, print the held line and return 1
|
|
70
|
+
* without a call to `body`. Each taken lock goes when the run ends, also when
|
|
71
|
+
* it throws; the OS releases the locks when the process ends.
|
|
72
|
+
*
|
|
73
|
+
* With `privateDirectory` false, each path is a folder lock. A missing folder
|
|
74
|
+
* is created just before its lock, so the locks of its ancestors in the set
|
|
75
|
+
* are already held. Each folder that this creates gets mode 0700, as
|
|
76
|
+
* `ompFileDeploy.ts ensureDirectory` gives the parents of an omp folder. An
|
|
77
|
+
* existing folder keeps its mode: `mkdirSync` applies `mode` only to the
|
|
78
|
+
* folders that it creates.
|
|
79
|
+
*/
|
|
80
|
+
async function withKitLocks(
|
|
81
|
+
err: ErrorSink,
|
|
82
|
+
paths: ReadonlyArray<string>,
|
|
83
|
+
privateDirectory: boolean,
|
|
84
|
+
body: () => Promise<number>,
|
|
85
|
+
): Promise<number> {
|
|
86
|
+
const waitMs = syncLockWaitMs(err);
|
|
87
|
+
const held: Array<KitLock> = [];
|
|
88
|
+
try {
|
|
89
|
+
for (const path of paths) {
|
|
90
|
+
if (!privateDirectory) mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
91
|
+
const result = await acquireKitLock(path, { waitMs, privateDirectory });
|
|
92
|
+
if (result.kind === "held") {
|
|
93
|
+
err(`another docks-kit sync holds ${path}; run sync again after it ends`);
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
held.push(result.lock);
|
|
97
|
+
}
|
|
98
|
+
return await body();
|
|
99
|
+
} finally {
|
|
100
|
+
for (const lock of held.reverse()) releaseKitLock(lock);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Run `body` under the sync lock `syncLockPath(ctx.home)`. Two syncs of the
|
|
106
|
+
* same home meet at this lock. A dry run takes no lock: `args` holds one of
|
|
107
|
+
* `UNLOCKED_TOKENS`, or `ctx.dryRun` is true. `makeCtx` sets `ctx.dryRun`
|
|
108
|
+
* from the `DRY_RUN` env global only on the channel that reads env globals.
|
|
109
|
+
*/
|
|
110
|
+
export async function withSyncLock(
|
|
111
|
+
ctx: Ctx,
|
|
112
|
+
args: ReadonlyArray<string>,
|
|
113
|
+
body: () => Promise<number>,
|
|
114
|
+
): Promise<number> {
|
|
115
|
+
if (ctx.dryRun || args.some((arg) => Object.hasOwn(UNLOCKED_TOKENS, arg))) return await body();
|
|
116
|
+
return await withKitLocks(ctx.services.logger.err, [syncLockPath(ctx.home)], true, body);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The canonical path of `folder`, which can be missing: the real path of its
|
|
121
|
+
* nearest existing ancestor, joined with the rest of the path.
|
|
122
|
+
*/
|
|
123
|
+
function canonicalFolder(folder: string): string {
|
|
124
|
+
const missing: Array<string> = [];
|
|
125
|
+
let current = folder;
|
|
126
|
+
while (!existsSync(current) && dirname(current) !== current) {
|
|
127
|
+
missing.unshift(basename(current));
|
|
128
|
+
current = dirname(current);
|
|
129
|
+
}
|
|
130
|
+
return join(realpathSync.native(current), ...missing);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The folder lock path of each folder, by the canonical path of the folder,
|
|
135
|
+
* with no duplicates, sorted by folder. Every sync takes its folder locks in
|
|
136
|
+
* this order, so no two syncs can wait on each other in a cycle. An ancestor
|
|
137
|
+
* sorts before its descendants, and a nested folder keeps its own lock.
|
|
138
|
+
*/
|
|
139
|
+
export function folderLockPaths(folders: ReadonlyArray<string>): Array<string> {
|
|
140
|
+
const canonical = new Set(folders.map(canonicalFolder));
|
|
141
|
+
return [...canonical].sort(compareCodepoints).map(folderSyncLockPath);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Run `body` under the folder lock of each folder in `folders`. Take it after
|
|
146
|
+
* the sync lock. Syncs of different homes can share a Codex, agents, or omp
|
|
147
|
+
* folder, and their home locks differ. A missing folder is created under the
|
|
148
|
+
* locks of its ancestors in the set. A dry run takes no folder lock and
|
|
149
|
+
* creates nothing.
|
|
150
|
+
*/
|
|
151
|
+
export async function withFolderLocks(
|
|
152
|
+
ctx: Ctx,
|
|
153
|
+
folders: ReadonlyArray<string>,
|
|
154
|
+
body: () => Promise<number>,
|
|
155
|
+
): Promise<number> {
|
|
156
|
+
if (ctx.dryRun) return await body();
|
|
157
|
+
return await withKitLocks(ctx.services.logger.err, folderLockPaths(folders), false, body);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Run `body` under the sync lock of `home` and the folder locks of `folders`,
|
|
162
|
+
* with the wait and the held line of a sync. A command that writes a sync
|
|
163
|
+
* file outside a sync run uses it. Print each error line with `err`.
|
|
164
|
+
*/
|
|
165
|
+
export async function withHomeAndFolderLocks(
|
|
166
|
+
home: string,
|
|
167
|
+
folders: ReadonlyArray<string>,
|
|
168
|
+
err: ErrorSink,
|
|
169
|
+
body: () => Promise<number>,
|
|
170
|
+
): Promise<number> {
|
|
171
|
+
return await withKitLocks(err, [syncLockPath(home)], true, () =>
|
|
172
|
+
withKitLocks(err, folderLockPaths(folders), false, body),
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Run a sync. The sync lock comes first: `parseArgs` reads the harness
|
|
178
|
+
* selection from `kit.db`, and that read can migrate `kit.db` and rename a
|
|
179
|
+
* legacy `state.json`. The folder locks come after `parseArgs` selects the
|
|
180
|
+
* targets.
|
|
181
|
+
*/
|
|
22
182
|
export async function engineSync(ctx: Ctx, args: ReadonlyArray<string>): Promise<number> {
|
|
23
|
-
|
|
183
|
+
return await withSyncLock(ctx, args, () => runSync(ctx, args));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function runSync(ctx: Ctx, args: ReadonlyArray<string>): Promise<number> {
|
|
187
|
+
const { err } = ctx.services.logger;
|
|
24
188
|
parseArgs(ctx, args);
|
|
25
189
|
await validateModifierFlags(ctx);
|
|
26
190
|
const configuredConcurrency = process.env["DOCKS_KIT_SYNC_CONCURRENCY"];
|
|
@@ -36,6 +200,24 @@ export async function engineSync(ctx: Ctx, args: ReadonlyArray<string>): Promise
|
|
|
36
200
|
err("DOCKS_KIT_SYNC_CONCURRENCY must be 1, 2, or 3");
|
|
37
201
|
throw new ExitError(2);
|
|
38
202
|
}
|
|
203
|
+
|
|
204
|
+
// The folders that hold ownership records or omp files. A writing pipeline
|
|
205
|
+
// creates its folders anyway, so `withFolderLocks` creates each missing one
|
|
206
|
+
// under the locks of its ancestors in the set. The omp folders are resolved
|
|
207
|
+
// once, here: the omp pipeline and its omp children use these paths, so a
|
|
208
|
+
// data root that appears after the locks cannot move them.
|
|
209
|
+
const omp = ctx.syncOmp ? ompWriteFolders(ctx) : undefined;
|
|
210
|
+
const folders = [
|
|
211
|
+
...(ctx.syncCodex ? [codexHome(ctx.home)] : []),
|
|
212
|
+
...(ctx.syncAgents ? [resolve(ctx.agentsDir)] : []),
|
|
213
|
+
...(omp === undefined ? [] : [omp.paths.agentDir, omp.intercomDir, omp.paths.dataRoot]),
|
|
214
|
+
];
|
|
215
|
+
return await withFolderLocks(ctx, folders, () => runPipelines(ctx, omp));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Run the selected pipelines. `omp` holds the omp folders when omp is selected. */
|
|
219
|
+
async function runPipelines(ctx: Ctx, omp: OmpWriteFolders | undefined): Promise<number> {
|
|
220
|
+
const { acquireTerminal, echo } = ctx.services.logger;
|
|
39
221
|
// An abort-class check runs before any pipeline starts, so the abort stops the whole sync.
|
|
40
222
|
preflightClaudeSettings(ctx);
|
|
41
223
|
|
|
@@ -71,10 +253,10 @@ export async function engineSync(ctx: Ctx, args: ReadonlyArray<string>): Promise
|
|
|
71
253
|
run: async () => ({ kind: "skills", state: await skillsSync(ctx) }),
|
|
72
254
|
});
|
|
73
255
|
}
|
|
74
|
-
if (
|
|
256
|
+
if (omp !== undefined) {
|
|
75
257
|
selected.push({
|
|
76
258
|
name: "omp",
|
|
77
|
-
run: async () => ({ kind: "omp", state: await ompSync(ctx) }),
|
|
259
|
+
run: async () => ({ kind: "omp", state: await ompSync(ctx, omp) }),
|
|
78
260
|
});
|
|
79
261
|
}
|
|
80
262
|
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
* Safe sweep of agent leftovers. `planTidy` lists what can go: merged idle
|
|
3
3
|
* linked worktrees and their containers, containers of gone or idle agent
|
|
4
4
|
* directories, orphaned processes in agent directories, and idle scratch
|
|
5
|
-
* entries. `applyTidy` performs a plan
|
|
6
|
-
* closed: a candidate that a check cannot prove safe stays, with
|
|
5
|
+
* entries. `applyTidy` performs a plan; its caller holds the tidy lock. Each
|
|
6
|
+
* check fails closed: a candidate that a check cannot prove safe stays, with
|
|
7
|
+
* its reason.
|
|
7
8
|
*/
|
|
8
9
|
import {
|
|
9
10
|
appendFileSync,
|
|
@@ -20,9 +21,9 @@ import { basename, dirname, isAbsolute, join, relative, resolve } from "node:pat
|
|
|
20
21
|
|
|
21
22
|
import { spawnProcess, type AsyncProcessResult } from "./exec";
|
|
22
23
|
import { ensureDirectory } from "./ompFileDeploy";
|
|
24
|
+
import { processExited } from "./processes";
|
|
23
25
|
import { isSameOrInside, kitCacheDir, removeTemporaryPath } from "./scratch";
|
|
24
26
|
import { runBounded } from "./syncConcurrency";
|
|
25
|
-
import { acquireTidyLock, releaseTidyLock } from "./tidyLock";
|
|
26
27
|
|
|
27
28
|
export const HOUR_MS = 3_600_000;
|
|
28
29
|
export const DAY_MS = 24 * HOUR_MS;
|
|
@@ -51,10 +52,6 @@ export function agentWorktreeRoot(home: string, env: NodeJS.ProcessEnv): string
|
|
|
51
52
|
return join(dataHome, "agent-worktrees");
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
export function tidyLockPath(home: string): string {
|
|
55
|
-
return join(kitCacheDir(home), "tidy.lock");
|
|
56
|
-
}
|
|
57
|
-
|
|
58
55
|
export function tidyLogPath(home: string): string {
|
|
59
56
|
return join(kitCacheDir(home), "tidy.log");
|
|
60
57
|
}
|
|
@@ -324,12 +321,7 @@ async function darwinProcesses(uid: number): Promise<ReadonlyArray<ProcessRow> |
|
|
|
324
321
|
return resolveProcessCwds(rows, (pid) => {
|
|
325
322
|
const cwd = cwds.get(pid);
|
|
326
323
|
if (cwd !== undefined) return { kind: "cwd", path: cwd };
|
|
327
|
-
|
|
328
|
-
process.kill(pid, 0);
|
|
329
|
-
} catch (cause) {
|
|
330
|
-
if (errorCode(cause) === "ESRCH") return { kind: "exited" };
|
|
331
|
-
}
|
|
332
|
-
return { kind: "unreadable" };
|
|
324
|
+
return processExited(pid) ? { kind: "exited" } : { kind: "unreadable" };
|
|
333
325
|
});
|
|
334
326
|
}
|
|
335
327
|
|
|
@@ -1162,135 +1154,126 @@ async function removeProjectNetworks(
|
|
|
1162
1154
|
}
|
|
1163
1155
|
|
|
1164
1156
|
/**
|
|
1165
|
-
* Perform a plan
|
|
1166
|
-
* (their containers first), prune each
|
|
1167
|
-
*
|
|
1168
|
-
* Every Docker call uses the
|
|
1157
|
+
* Perform a plan. The caller holds the tidy lock from before `planTidy`. Order:
|
|
1158
|
+
* stop processes, remove worktrees (their containers first), prune each
|
|
1159
|
+
* touched repository, stop containers, remove containers, remove scratch
|
|
1160
|
+
* entries. Never `git worktree remove --force`. Every Docker call uses the
|
|
1161
|
+
* endpoint that the plan verified.
|
|
1169
1162
|
*/
|
|
1170
1163
|
export async function applyTidy(
|
|
1171
1164
|
plan: TidyPlan,
|
|
1172
1165
|
inputs: TidyInputs,
|
|
1173
1166
|
host: TidyHost,
|
|
1174
1167
|
): Promise<{
|
|
1175
|
-
readonly locked: boolean;
|
|
1176
1168
|
readonly failures: number;
|
|
1177
1169
|
readonly lines: ReadonlyArray<string>;
|
|
1178
1170
|
}> {
|
|
1179
1171
|
ensureDirectory(kitCacheDir(inputs.home));
|
|
1180
|
-
const
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
lines.push(`failed ${subject}: ${detail}`);
|
|
1194
|
-
};
|
|
1195
|
-
const byKind = <K extends TidyAction["kind"]>(kind: K) =>
|
|
1196
|
-
plan.actions.filter(
|
|
1197
|
-
(action): action is Extract<TidyAction, { kind: K }> => action.kind === kind,
|
|
1198
|
-
);
|
|
1199
|
-
|
|
1200
|
-
for (const action of byKind("stop-process")) {
|
|
1201
|
-
const subject = actionSubject(action);
|
|
1202
|
-
try {
|
|
1203
|
-
process.kill(action.pid, "SIGTERM");
|
|
1204
|
-
done("stopped", subject);
|
|
1205
|
-
} catch (cause) {
|
|
1206
|
-
if (errorCode(cause) === "ESRCH") done("stopped", subject);
|
|
1207
|
-
else failed(subject, errorMessage(cause));
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
// A container action without a verified local endpoint fails and changes nothing.
|
|
1211
|
-
const runDocker = localDocker(host, plan.docker);
|
|
1212
|
-
const noDocker =
|
|
1213
|
-
plan.docker.kind === "missing" ? "docker unavailable" : notLocalMessage(plan.docker.endpoint);
|
|
1214
|
-
|
|
1215
|
-
const touched = new Set<string>();
|
|
1216
|
-
for (const action of byKind("remove-worktree")) {
|
|
1217
|
-
const subject = actionSubject(action);
|
|
1218
|
-
if (action.containerIds.length > 0) {
|
|
1219
|
-
if (runDocker === undefined) {
|
|
1220
|
-
failed(subject, noDocker);
|
|
1221
|
-
continue;
|
|
1222
|
-
}
|
|
1223
|
-
const removed = await runDocker(["rm", "--force", "--volumes", ...action.containerIds]);
|
|
1224
|
-
if (removed.code !== 0) {
|
|
1225
|
-
failed(subject, `docker rm: ${firstLine(removed)}`);
|
|
1226
|
-
continue;
|
|
1227
|
-
}
|
|
1228
|
-
await removeProjectNetworks(action.projects, runDocker);
|
|
1229
|
-
}
|
|
1230
|
-
touched.add(action.repo);
|
|
1231
|
-
const result = await host.git(["-C", action.repo, "worktree", "remove", "--", action.path]);
|
|
1232
|
-
if (result.code === 0) done("removed", subject);
|
|
1233
|
-
else failed(subject, firstLine(result));
|
|
1234
|
-
}
|
|
1235
|
-
for (const repo of touched) {
|
|
1236
|
-
const result = await host.git(["-C", repo, "worktree", "prune"]);
|
|
1237
|
-
if (result.code !== 0) failed(`worktree prune in ${repo}`, firstLine(result));
|
|
1238
|
-
}
|
|
1172
|
+
const lines: Array<string> = [];
|
|
1173
|
+
let failures = 0;
|
|
1174
|
+
const done = (outcome: "removed" | "stopped", subject: string): void => {
|
|
1175
|
+
lines.push(`${outcome} ${subject}`);
|
|
1176
|
+
};
|
|
1177
|
+
const failed = (subject: string, detail: string): void => {
|
|
1178
|
+
failures += 1;
|
|
1179
|
+
lines.push(`failed ${subject}: ${detail}`);
|
|
1180
|
+
};
|
|
1181
|
+
const byKind = <K extends TidyAction["kind"]>(kind: K) =>
|
|
1182
|
+
plan.actions.filter(
|
|
1183
|
+
(action): action is Extract<TidyAction, { kind: K }> => action.kind === kind,
|
|
1184
|
+
);
|
|
1239
1185
|
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
if (
|
|
1247
|
-
else failed(
|
|
1186
|
+
for (const action of byKind("stop-process")) {
|
|
1187
|
+
const subject = actionSubject(action);
|
|
1188
|
+
try {
|
|
1189
|
+
process.kill(action.pid, "SIGTERM");
|
|
1190
|
+
done("stopped", subject);
|
|
1191
|
+
} catch (cause) {
|
|
1192
|
+
if (errorCode(cause) === "ESRCH") done("stopped", subject);
|
|
1193
|
+
else failed(subject, errorMessage(cause));
|
|
1248
1194
|
}
|
|
1249
|
-
|
|
1250
|
-
|
|
1195
|
+
}
|
|
1196
|
+
// A container action without a verified local endpoint fails and changes nothing.
|
|
1197
|
+
const runDocker = localDocker(host, plan.docker);
|
|
1198
|
+
const noDocker =
|
|
1199
|
+
plan.docker.kind === "missing" ? "docker unavailable" : notLocalMessage(plan.docker.endpoint);
|
|
1200
|
+
|
|
1201
|
+
const touched = new Set<string>();
|
|
1202
|
+
for (const action of byKind("remove-worktree")) {
|
|
1203
|
+
const subject = actionSubject(action);
|
|
1204
|
+
if (action.containerIds.length > 0) {
|
|
1251
1205
|
if (runDocker === undefined) {
|
|
1252
|
-
failed(
|
|
1206
|
+
failed(subject, noDocker);
|
|
1253
1207
|
continue;
|
|
1254
1208
|
}
|
|
1255
|
-
const
|
|
1256
|
-
if (
|
|
1257
|
-
failed(
|
|
1209
|
+
const removed = await runDocker(["rm", "--force", "--volumes", ...action.containerIds]);
|
|
1210
|
+
if (removed.code !== 0) {
|
|
1211
|
+
failed(subject, `docker rm: ${firstLine(removed)}`);
|
|
1258
1212
|
continue;
|
|
1259
1213
|
}
|
|
1260
|
-
await removeProjectNetworks(action.
|
|
1261
|
-
done("removed", actionSubject(action));
|
|
1214
|
+
await removeProjectNetworks(action.projects, runDocker);
|
|
1262
1215
|
}
|
|
1216
|
+
touched.add(action.repo);
|
|
1217
|
+
const result = await host.git(["-C", action.repo, "worktree", "remove", "--", action.path]);
|
|
1218
|
+
if (result.code === 0) done("removed", subject);
|
|
1219
|
+
else failed(subject, firstLine(result));
|
|
1220
|
+
}
|
|
1221
|
+
for (const repo of touched) {
|
|
1222
|
+
const result = await host.git(["-C", repo, "worktree", "prune"]);
|
|
1223
|
+
if (result.code !== 0) failed(`worktree prune in ${repo}`, firstLine(result));
|
|
1224
|
+
}
|
|
1263
1225
|
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
failed(subject, "the scratch root is refused");
|
|
1269
|
-
continue;
|
|
1270
|
-
}
|
|
1271
|
-
let activity: number | undefined;
|
|
1272
|
-
try {
|
|
1273
|
-
activity = entryActivity(action.path);
|
|
1274
|
-
} catch (cause) {
|
|
1275
|
-
lines.push(`kept ${subject}: ${errorMessage(cause)}`);
|
|
1276
|
-
continue;
|
|
1277
|
-
}
|
|
1278
|
-
if (activity !== undefined && host.now() - activity < SCRATCH_IDLE_MS) {
|
|
1279
|
-
lines.push(`kept ${subject}: active ${hours(host.now() - activity)} h ago`);
|
|
1280
|
-
continue;
|
|
1281
|
-
}
|
|
1282
|
-
const outcome = removeTemporaryPath(action.path, [scratch], inputs.home);
|
|
1283
|
-
if (outcome.kind === "removed") done("removed", subject);
|
|
1284
|
-
else failed(subject, outcome.reason);
|
|
1226
|
+
for (const action of byKind("stop-containers")) {
|
|
1227
|
+
if (runDocker === undefined) {
|
|
1228
|
+
failed(actionSubject(action), noDocker);
|
|
1229
|
+
continue;
|
|
1285
1230
|
}
|
|
1231
|
+
const result = await runDocker(["stop", ...action.ids]);
|
|
1232
|
+
if (result.code === 0) done("stopped", actionSubject(action));
|
|
1233
|
+
else failed(actionSubject(action), firstLine(result));
|
|
1234
|
+
}
|
|
1286
1235
|
|
|
1236
|
+
for (const action of byKind("remove-containers")) {
|
|
1237
|
+
if (runDocker === undefined) {
|
|
1238
|
+
failed(actionSubject(action), noDocker);
|
|
1239
|
+
continue;
|
|
1240
|
+
}
|
|
1241
|
+
const result = await runDocker(["rm", "--force", "--volumes", ...action.ids]);
|
|
1242
|
+
if (result.code !== 0) {
|
|
1243
|
+
failed(actionSubject(action), firstLine(result));
|
|
1244
|
+
continue;
|
|
1245
|
+
}
|
|
1246
|
+
await removeProjectNetworks(action.project === "" ? [] : [action.project], runDocker);
|
|
1247
|
+
done("removed", actionSubject(action));
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
const scratch = agentRoots(inputs).scratchRoot;
|
|
1251
|
+
for (const action of byKind("remove-scratch")) {
|
|
1252
|
+
const subject = actionSubject(action);
|
|
1253
|
+
if (scratch === undefined) {
|
|
1254
|
+
failed(subject, "the scratch root is refused");
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
let activity: number | undefined;
|
|
1287
1258
|
try {
|
|
1288
|
-
|
|
1259
|
+
activity = entryActivity(action.path);
|
|
1289
1260
|
} catch (cause) {
|
|
1290
|
-
|
|
1261
|
+
lines.push(`kept ${subject}: ${errorMessage(cause)}`);
|
|
1262
|
+
continue;
|
|
1263
|
+
}
|
|
1264
|
+
if (activity !== undefined && host.now() - activity < SCRATCH_IDLE_MS) {
|
|
1265
|
+
lines.push(`kept ${subject}: active ${hours(host.now() - activity)} h ago`);
|
|
1266
|
+
continue;
|
|
1291
1267
|
}
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1268
|
+
const outcome = removeTemporaryPath(action.path, [scratch], inputs.home);
|
|
1269
|
+
if (outcome.kind === "removed") done("removed", subject);
|
|
1270
|
+
else failed(subject, outcome.reason);
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
try {
|
|
1274
|
+
appendLog(tidyLogPath(inputs.home), lines, host.now());
|
|
1275
|
+
} catch (cause) {
|
|
1276
|
+
failed(`log ${tidyLogPath(inputs.home)}`, errorMessage(cause));
|
|
1295
1277
|
}
|
|
1278
|
+
return { failures, lines };
|
|
1296
1279
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The line grammar of the line-based TOML passes: table header lines, key
|
|
3
|
+
* lines, and the lines that continue a value from an earlier line. The module
|
|
4
|
+
* imports nothing, so `manifests.ts` can read a Codex config without a runtime
|
|
5
|
+
* cycle through `codexToml.ts` and `efforts.ts`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function isTomlHeaderLine(line: string): boolean {
|
|
9
|
+
return /^[ \t]*\[/.test(line);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Offset just past the quoted string that starts at `start`, or the line end when it stays open. */
|
|
13
|
+
function skipQuoted(line: string, start: number): number {
|
|
14
|
+
const quote = line[start];
|
|
15
|
+
let offset = start + 1;
|
|
16
|
+
while (offset < line.length) {
|
|
17
|
+
if (quote === '"' && line[offset] === "\\") {
|
|
18
|
+
offset += 2;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (line[offset] === quote) return offset + 1;
|
|
22
|
+
offset++;
|
|
23
|
+
}
|
|
24
|
+
return offset;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Offset of the `=` that ends the key on a key line, or undefined for other lines. */
|
|
28
|
+
export function keyValueSeparator(line: string): number | undefined {
|
|
29
|
+
if (/^[ \t]*($|#|\[)/.test(line)) return undefined;
|
|
30
|
+
let offset = 0;
|
|
31
|
+
while (offset < line.length) {
|
|
32
|
+
const char = line[offset]!;
|
|
33
|
+
if (char === "=") return offset;
|
|
34
|
+
offset = char === '"' || char === "'" ? skipQuoted(line, offset) : offset + 1;
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Mark each line that continues a value opened on an earlier line: the body of
|
|
41
|
+
* a `"""` or `'''` string, or the rest of an array or inline table that spans
|
|
42
|
+
* lines. A marked line is never a table header or a key line.
|
|
43
|
+
*/
|
|
44
|
+
export function tomlContinuationLines(lines: ReadonlyArray<string>): Array<boolean> {
|
|
45
|
+
const continuation: Array<boolean> = [];
|
|
46
|
+
let openString: string | undefined;
|
|
47
|
+
let depth = 0;
|
|
48
|
+
const scanValue = (line: string, start: number): void => {
|
|
49
|
+
let offset = start;
|
|
50
|
+
while (offset < line.length) {
|
|
51
|
+
if (openString !== undefined) {
|
|
52
|
+
if (openString === '"""' && line[offset] === "\\") {
|
|
53
|
+
offset += 2;
|
|
54
|
+
} else if (line.startsWith(openString, offset)) {
|
|
55
|
+
// A closing delimiter can carry up to two extra quotes of string content.
|
|
56
|
+
let end = offset + 3;
|
|
57
|
+
while (end < offset + 5 && line[end] === openString[0]) end++;
|
|
58
|
+
offset = end;
|
|
59
|
+
openString = undefined;
|
|
60
|
+
} else {
|
|
61
|
+
offset++;
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const char = line[offset]!;
|
|
66
|
+
if (char === "#") return;
|
|
67
|
+
if (line.startsWith('"""', offset) || line.startsWith("'''", offset)) {
|
|
68
|
+
openString = line.slice(offset, offset + 3);
|
|
69
|
+
offset += 3;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (char === '"' || char === "'") {
|
|
73
|
+
offset = skipQuoted(line, offset);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (char === "[" || char === "{") depth++;
|
|
77
|
+
else if ((char === "]" || char === "}") && depth > 0) depth--;
|
|
78
|
+
offset++;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
for (const line of lines) {
|
|
82
|
+
if (openString !== undefined || depth > 0) {
|
|
83
|
+
continuation.push(true);
|
|
84
|
+
scanValue(line, 0);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
continuation.push(false);
|
|
88
|
+
const separator = keyValueSeparator(line);
|
|
89
|
+
if (separator !== undefined) scanValue(line, separator + 1);
|
|
90
|
+
}
|
|
91
|
+
return continuation;
|
|
92
|
+
}
|