@aviaratech/ai-delivery 0.3.36 → 0.3.38
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/README.md +5 -6
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +1 -3
- package/dist/agent.js.map +2 -2
- package/dist/callerErrors.test.js +0 -14
- package/dist/callerErrors.test.js.map +2 -2
- package/dist/cli.js +0 -15
- package/dist/cli.js.map +2 -2
- package/dist/contributorChecks.test.js +5 -2
- package/dist/contributorChecks.test.js.map +2 -2
- package/dist/dispatch.d.ts +1 -1
- package/dist/dispatch.js +1 -15
- package/dist/dispatch.js.map +2 -2
- package/dist/fixtures/discovery.d.ts +16 -16
- package/dist/git.d.ts +0 -3
- package/dist/git.js +2 -25
- package/dist/git.js.map +2 -2
- package/dist/github/commandIdentityPolicy.js +0 -2
- package/dist/github/commandIdentityPolicy.js.map +2 -2
- package/dist/issue.js +7 -2
- package/dist/issue.js.map +2 -2
- package/dist/issueInfoReadback.test.js +59 -1
- package/dist/issueInfoReadback.test.js.map +2 -2
- package/dist/issueIntake.test.js +0 -7
- package/dist/issueIntake.test.js.map +2 -2
- package/dist/mcp/tools.d.ts +0 -36
- package/dist/mcp/tools.js +0 -34
- package/dist/mcp/tools.js.map +2 -2
- package/dist/pluginPackaging.test.js +0 -47
- package/dist/pluginPackaging.test.js.map +2 -2
- package/dist/pr.d.ts +0 -55
- package/dist/pr.js +0 -211
- package/dist/pr.js.map +3 -3
- package/dist/pr.test.js +5 -737
- package/dist/pr.test.js.map +3 -3
- package/dist/prAssociation.test.js +1 -1
- package/dist/prAssociation.test.js.map +2 -2
- package/dist/prDispatch.test.js +4 -31
- package/dist/prDispatch.test.js.map +2 -2
- package/dist/readView.d.ts +1 -0
- package/dist/readView.js +1 -0
- package/dist/readView.js.map +2 -2
- package/dist/readView.test.js +29 -2
- package/dist/readView.test.js.map +2 -2
- package/dist/stdoutRegression.test.js +0 -38
- package/dist/stdoutRegression.test.js.map +2 -2
- package/dist/targetPolicyRoutes.test.js +1 -32
- package/dist/targetPolicyRoutes.test.js.map +2 -2
- package/dist/targetRepositoryExecution.test.js +0 -12
- package/dist/targetRepositoryExecution.test.js.map +2 -2
- package/dist/utils/atomicJson.d.ts +0 -2
- package/dist/utils/atomicJson.js +0 -4
- package/dist/utils/atomicJson.js.map +2 -2
- package/dist/utils/filesystem.d.ts +0 -8
- package/dist/utils/filesystem.js +1 -12
- package/dist/utils/filesystem.js.map +2 -2
- package/dist/verification.d.ts +0 -35
- package/dist/verification.js +1 -47
- package/dist/verification.js.map +2 -2
- package/dist/version.js +24 -2
- package/dist/version.js.map +2 -2
- package/package.json +1 -1
- package/plugins/ai-delivery/.claude-plugin/plugin.json +2 -2
- package/plugins/ai-delivery/plugin.json +1 -1
- package/plugins/ai-delivery/runtime/dist/cli.js +128 -135
- package/plugins/ai-delivery/runtime/package.json +1 -1
- package/plugins/ai-delivery/skills/pr-handoff/SKILL.md +4 -4
- package/dist/services/deliveryAdmission.d.ts +0 -49
- package/dist/services/deliveryAdmission.js +0 -142
- package/dist/services/deliveryAdmission.js.map +0 -7
- package/dist/services/worktreeRegistry.d.ts +0 -342
- package/dist/services/worktreeRegistry.js +0 -831
- package/dist/services/worktreeRegistry.js.map +0 -7
- package/dist/worktree.d.ts +0 -16
- package/dist/worktree.js +0 -108
- package/dist/worktree.js.map +0 -7
|
@@ -1,831 +0,0 @@
|
|
|
1
|
-
import { closeSync, existsSync, fsyncSync, lstatSync, openSync, readFileSync, readdirSync } from "node:fs";
|
|
2
|
-
import { dirname, join, resolve } from "node:path";
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
import {
|
|
5
|
-
assertPrivateFile,
|
|
6
|
-
CoordinateSchema,
|
|
7
|
-
DigestSchema,
|
|
8
|
-
digestBytes,
|
|
9
|
-
stableJson,
|
|
10
|
-
writeCreateOnly
|
|
11
|
-
} from "../delivery/common.js";
|
|
12
|
-
import { digestValue } from "../delivery/index.js";
|
|
13
|
-
import { gitCommonDir } from "../git.js";
|
|
14
|
-
import { RuntimeAdmissionSchema } from "./deliveryAdmission.js";
|
|
15
|
-
import { logInfo, logWarn } from "../logger.js";
|
|
16
|
-
import { ensureDataDirectory, ISSUE_CLI_DATA_DIR } from "../utils/filesystem.js";
|
|
17
|
-
import {
|
|
18
|
-
ensurePrivateDirectoryDurably,
|
|
19
|
-
writeJsonFileAtomically,
|
|
20
|
-
writePrivateJsonFileAtomically
|
|
21
|
-
} from "../utils/atomicJson.js";
|
|
22
|
-
import { withLock } from "../utils/lockfile.js";
|
|
23
|
-
const REGISTRY_PATH = `${ISSUE_CLI_DATA_DIR}/worktrees.json`;
|
|
24
|
-
const LOCK_RETRY_MESSAGE = "Worktree registry locked by another process. Retrying...";
|
|
25
|
-
const ContinuationRowSchema = z.strictObject({
|
|
26
|
-
branch: z.string().min(1),
|
|
27
|
-
createdAt: z.string().min(1),
|
|
28
|
-
identity: z.string().min(1),
|
|
29
|
-
issueNumber: z.number().int().positive(),
|
|
30
|
-
path: z.string().min(1),
|
|
31
|
-
prNumber: z.number().int().positive().optional(),
|
|
32
|
-
status: z.enum(["active", "error", "merged", "pr-published", "stale"]),
|
|
33
|
-
type: z.literal("issue"),
|
|
34
|
-
updatedAt: z.string().min(1)
|
|
35
|
-
}).transform(({ prNumber, ...row }) => ({ ...row, ...prNumber === void 0 ? {} : { prNumber } }));
|
|
36
|
-
const CommittedContinuationPlanSchema = z.strictObject({
|
|
37
|
-
schemaVersion: z.literal("ai-delivery.committed-continuation-plan@1"),
|
|
38
|
-
planId: DigestSchema,
|
|
39
|
-
repository: z.string().min(1),
|
|
40
|
-
repoRoot: z.string().min(1),
|
|
41
|
-
configDigest: DigestSchema,
|
|
42
|
-
currentRuntime: RuntimeAdmissionSchema,
|
|
43
|
-
row: ContinuationRowSchema,
|
|
44
|
-
replacement: ContinuationRowSchema,
|
|
45
|
-
head: CoordinateSchema,
|
|
46
|
-
terminalHead: CoordinateSchema,
|
|
47
|
-
terminalMergeId: DigestSchema,
|
|
48
|
-
operator: z.strictObject({
|
|
49
|
-
actorLogin: z.string().min(1),
|
|
50
|
-
credentialIdentity: z.string().regex(/^user:[1-9]\d*$/u)
|
|
51
|
-
}),
|
|
52
|
-
reviewerActor: z.string().min(1),
|
|
53
|
-
preserved: z.array(z.strictObject({ path: z.string().min(1), digest: DigestSchema })).min(1)
|
|
54
|
-
}).superRefine((plan, ctx) => {
|
|
55
|
-
const { planId, ...content } = plan;
|
|
56
|
-
const { prNumber: _pr, identity: _identity, status: _status, updatedAt: _updated, ...original } = plan.row;
|
|
57
|
-
const { identity: _newIdentity, status: _newStatus, updatedAt: _newUpdated, ...replacement } = plan.replacement;
|
|
58
|
-
if (planId !== digestValue(content) || plan.row.status !== "merged" || !plan.row.prNumber || plan.replacement.status !== "active" || plan.replacement.prNumber !== void 0 || stableJson(original) !== stableJson(replacement) || plan.head.sha === plan.terminalHead.sha || new Set(plan.preserved.map((file) => file.path)).size !== plan.preserved.length)
|
|
59
|
-
ctx.addIssue({ code: "custom", message: "Committed continuation plan identity or disposition is invalid." });
|
|
60
|
-
});
|
|
61
|
-
const ContinuationIntentSchema = z.strictObject({
|
|
62
|
-
schemaVersion: z.literal("ai-delivery.committed-continuation-intent@1"),
|
|
63
|
-
intentId: DigestSchema,
|
|
64
|
-
plan: CommittedContinuationPlanSchema,
|
|
65
|
-
authorityCommentId: z.number().int().positive(),
|
|
66
|
-
acceptanceCommentId: z.number().int().positive()
|
|
67
|
-
}).superRefine(({ intentId, ...content }, ctx) => {
|
|
68
|
-
if (intentId !== digestValue(content))
|
|
69
|
-
ctx.addIssue({ code: "custom", message: "Continuation intent identity is invalid." });
|
|
70
|
-
});
|
|
71
|
-
const ContinuationCompletionSchema = z.strictObject({
|
|
72
|
-
schemaVersion: z.literal("ai-delivery.committed-continuation-completion@1"),
|
|
73
|
-
completionId: DigestSchema,
|
|
74
|
-
intentId: DigestSchema,
|
|
75
|
-
planId: DigestSchema,
|
|
76
|
-
row: ContinuationRowSchema,
|
|
77
|
-
head: CoordinateSchema,
|
|
78
|
-
completed: z.literal(true)
|
|
79
|
-
}).superRefine(({ completionId, ...content }, ctx) => {
|
|
80
|
-
if (completionId !== digestValue(content))
|
|
81
|
-
ctx.addIssue({ code: "custom", message: "Continuation completion identity is invalid." });
|
|
82
|
-
});
|
|
83
|
-
function committedContinuationPlanPath(root, issueNumber, head) {
|
|
84
|
-
return join(gitCommonDir(root), "ai-delivery", "continuations", String(issueNumber), `${head}.plan.json`);
|
|
85
|
-
}
|
|
86
|
-
function continuationIntentPath(plan) {
|
|
87
|
-
return committedContinuationPlanPath(plan.repoRoot, plan.row.issueNumber, plan.head.sha).replace(
|
|
88
|
-
/\.plan\.json$/u,
|
|
89
|
-
".intent.json"
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
function continuationCompletionPath(plan) {
|
|
93
|
-
return continuationIntentPath(plan).replace(/\.intent\.json$/u, ".completion.json");
|
|
94
|
-
}
|
|
95
|
-
function continuationBeforeimagePath(plan, digest) {
|
|
96
|
-
return join(gitCommonDir(plan.repoRoot), "ai-delivery", "continuations", "evidence", `${digest.slice(7)}.bin`);
|
|
97
|
-
}
|
|
98
|
-
function assertContinuationPreserved(plan, beforeimages = false) {
|
|
99
|
-
for (const file of plan.preserved) {
|
|
100
|
-
if (digestBytes(assertPrivateFile(file.path)) !== file.digest || beforeimages && digestBytes(assertPrivateFile(continuationBeforeimagePath(plan, file.digest))) !== file.digest)
|
|
101
|
-
throw new Error("Committed continuation original evidence changed.");
|
|
102
|
-
}
|
|
103
|
-
if (beforeimages) {
|
|
104
|
-
const bytes = Buffer.from(stableJson(plan.row));
|
|
105
|
-
if (!assertPrivateFile(continuationBeforeimagePath(plan, digestBytes(bytes))).equals(bytes))
|
|
106
|
-
throw new Error("Committed continuation original row beforeimage changed.");
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
function pendingCommittedContinuation(issueNumber, root) {
|
|
110
|
-
const directory = join(gitCommonDir(root), "ai-delivery", "continuations", String(issueNumber));
|
|
111
|
-
if (!existsSync(directory)) return void 0;
|
|
112
|
-
let pending;
|
|
113
|
-
for (const name of readdirSync(directory).filter((name2) => name2.endsWith(".intent.json")).sort()) {
|
|
114
|
-
const intent = ContinuationIntentSchema.parse(
|
|
115
|
-
JSON.parse(assertPrivateFile(join(directory, name)).toString("utf8"))
|
|
116
|
-
);
|
|
117
|
-
const plan = intent.plan;
|
|
118
|
-
if (plan.repoRoot !== resolve(root) || plan.row.issueNumber !== issueNumber || join(directory, name) !== continuationIntentPath(plan))
|
|
119
|
-
throw new Error("Committed continuation checkpoint identity disagrees.");
|
|
120
|
-
assertContinuationPreserved(plan, true);
|
|
121
|
-
const completionPath = continuationCompletionPath(plan);
|
|
122
|
-
if (existsSync(completionPath)) {
|
|
123
|
-
const completion = ContinuationCompletionSchema.parse(
|
|
124
|
-
JSON.parse(assertPrivateFile(completionPath).toString("utf8"))
|
|
125
|
-
);
|
|
126
|
-
if (completion.intentId !== intent.intentId || completion.planId !== plan.planId || stableJson(completion.row) !== stableJson(plan.replacement) || stableJson(completion.head) !== stableJson(plan.head))
|
|
127
|
-
throw new Error("Committed continuation completion disagrees with its intent.");
|
|
128
|
-
assertOwnerWitness(plan.replacement, root);
|
|
129
|
-
prepareCommittedContinuationDirectory(root, directory, false);
|
|
130
|
-
prepareCommittedContinuationDirectory(
|
|
131
|
-
root,
|
|
132
|
-
dirname(continuationBeforeimagePath(plan, plan.preserved[0].digest)),
|
|
133
|
-
false
|
|
134
|
-
);
|
|
135
|
-
syncCommittedContinuationDirectory(completionPath);
|
|
136
|
-
} else {
|
|
137
|
-
if (pending) throw new Error("Issue has multiple incomplete committed continuations.");
|
|
138
|
-
pending = intent;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return pending;
|
|
142
|
-
}
|
|
143
|
-
function ownerContent(entry) {
|
|
144
|
-
if (!entry.identity) throw new Error("ai-delivery worktree ownership requires the preparing identity.");
|
|
145
|
-
return {
|
|
146
|
-
branch: entry.branch,
|
|
147
|
-
identity: entry.identity,
|
|
148
|
-
...entry.issueNumber === void 0 ? {} : { issueNumber: entry.issueNumber },
|
|
149
|
-
path: entry.path,
|
|
150
|
-
...entry.prNumber === void 0 || entry.type === "issue" ? {} : { prNumber: entry.prNumber },
|
|
151
|
-
schemaVersion: "ai-delivery.worktree-owner@1",
|
|
152
|
-
type: entry.type
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
function ownerPath(root, entry) {
|
|
156
|
-
const id = digestValue(ownerContent(entry)).slice("sha256:".length);
|
|
157
|
-
return join(gitCommonDir(root), "ai-delivery", "worktree-owners", `${id}.json`);
|
|
158
|
-
}
|
|
159
|
-
function assertAiDeliveryWorktreeOwner(entry, root) {
|
|
160
|
-
if (entry.issueNumber !== void 0 && pendingCommittedContinuation(entry.issueNumber, root))
|
|
161
|
-
throw new Error("Committed continuation is pending or incomplete; resume supported develop.");
|
|
162
|
-
assertOwnerWitness(entry, root);
|
|
163
|
-
}
|
|
164
|
-
function assertOwnerWitness(entry, root) {
|
|
165
|
-
const content = ownerContent(entry);
|
|
166
|
-
const path = ownerPath(root, entry);
|
|
167
|
-
let stored;
|
|
168
|
-
try {
|
|
169
|
-
stored = JSON.parse(assertPrivateFile(path).toString("utf8"));
|
|
170
|
-
} catch {
|
|
171
|
-
throw new Error("Worktree lacks its exact ai-delivery ownership witness.");
|
|
172
|
-
}
|
|
173
|
-
if (JSON.stringify(stored) !== JSON.stringify({ ...content, ownerId: digestValue(content) })) {
|
|
174
|
-
throw new Error("Worktree ownership witness disagrees with the canonical registry identity.");
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
function assertIssueWorktreeTransitionAdmission(issueNumber, root, committedRecovery = false) {
|
|
178
|
-
if (pendingCommittedContinuation(issueNumber, root) && !committedRecovery)
|
|
179
|
-
throw new Error("Committed continuation is pending or incomplete; resume supported develop.");
|
|
180
|
-
}
|
|
181
|
-
function writeWorktreeOwner(entry, root, transition = false) {
|
|
182
|
-
const content = ownerContent(entry);
|
|
183
|
-
writeCreateOnly(ownerPath(root, entry), Buffer.from(JSON.stringify({ ...content, ownerId: digestValue(content) })));
|
|
184
|
-
if (!transition) assertAiDeliveryWorktreeOwner(entry, root);
|
|
185
|
-
}
|
|
186
|
-
function writeCommittedContinuationCheckpoint(path, value) {
|
|
187
|
-
const assertEqual = () => {
|
|
188
|
-
const stored = JSON.parse(assertPrivateFile(path).toString("utf8"));
|
|
189
|
-
if (stableJson(stored) !== stableJson(value)) throw new Error("Committed continuation checkpoint changed.");
|
|
190
|
-
};
|
|
191
|
-
try {
|
|
192
|
-
assertEqual();
|
|
193
|
-
} catch (error) {
|
|
194
|
-
if (error.code !== "ENOENT") throw error;
|
|
195
|
-
writePrivateJsonFileAtomically(path, value);
|
|
196
|
-
assertEqual();
|
|
197
|
-
}
|
|
198
|
-
syncCommittedContinuationDirectory(path);
|
|
199
|
-
}
|
|
200
|
-
function syncCommittedContinuationDirectory(path) {
|
|
201
|
-
const directory = openSync(dirname(path), "r");
|
|
202
|
-
try {
|
|
203
|
-
fsyncSync(directory);
|
|
204
|
-
} finally {
|
|
205
|
-
closeSync(directory);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
function prepareCommittedContinuationDirectory(root, directory, create = true) {
|
|
209
|
-
const deliveryDirectory = join(gitCommonDir(root), "ai-delivery");
|
|
210
|
-
const continuationRoot = join(deliveryDirectory, "continuations");
|
|
211
|
-
const paths = [deliveryDirectory, continuationRoot, directory];
|
|
212
|
-
const validate = (path, missing) => {
|
|
213
|
-
let metadata;
|
|
214
|
-
try {
|
|
215
|
-
metadata = lstatSync(path);
|
|
216
|
-
} catch (error) {
|
|
217
|
-
if (missing && error.code === "ENOENT") return;
|
|
218
|
-
throw error;
|
|
219
|
-
}
|
|
220
|
-
if (!metadata.isDirectory() || metadata.isSymbolicLink() || path !== deliveryDirectory && (metadata.mode & 63) !== 0)
|
|
221
|
-
throw new Error("Committed continuation evidence requires a private directory.");
|
|
222
|
-
};
|
|
223
|
-
for (const path of paths) validate(path, create && path !== deliveryDirectory);
|
|
224
|
-
if (create) ensurePrivateDirectoryDurably(directory);
|
|
225
|
-
for (const path of paths) {
|
|
226
|
-
validate(path, false);
|
|
227
|
-
syncCommittedContinuationDirectory(path);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
async function withCommittedContinuationRegistry(root, issueNumber, operation) {
|
|
231
|
-
ensureRegistryDirectory(root);
|
|
232
|
-
return withLock(REGISTRY_PATH, {
|
|
233
|
-
projectRoot: root,
|
|
234
|
-
timeout: 5e3,
|
|
235
|
-
operation: async () => {
|
|
236
|
-
const pending = pendingCommittedContinuation(issueNumber, root);
|
|
237
|
-
let intent = pending;
|
|
238
|
-
let current;
|
|
239
|
-
const readCurrent = () => {
|
|
240
|
-
const registry = loadRegistryStrict(root);
|
|
241
|
-
const matches = registry.worktrees.filter((row2) => row2.type === "issue" && row2.issueNumber === issueNumber);
|
|
242
|
-
const row = matches[0];
|
|
243
|
-
if (matches.length !== 1 || !row)
|
|
244
|
-
throw new Error("Committed continuation requires one exact canonical issue row.");
|
|
245
|
-
if (registry.worktrees.some((other) => other !== row && registryIdentitiesOverlap(other, row)))
|
|
246
|
-
throw new Error("Committed continuation overlaps another canonical owner.");
|
|
247
|
-
assertOwnerWitness(row, root);
|
|
248
|
-
return { registry, row };
|
|
249
|
-
};
|
|
250
|
-
current = readCurrent().row;
|
|
251
|
-
if (pending && ![pending.plan.row, pending.plan.replacement].some((row) => stableJson(row) === stableJson(current)))
|
|
252
|
-
throw new Error("Committed continuation canonical row drifted.");
|
|
253
|
-
const original = pending?.plan.row ?? current;
|
|
254
|
-
assertOwnerWitness(original, root);
|
|
255
|
-
const witnessPath = ownerPath(root, original);
|
|
256
|
-
const ownerWitness = { path: witnessPath, digest: digestBytes(assertPrivateFile(witnessPath)) };
|
|
257
|
-
return operation({
|
|
258
|
-
current,
|
|
259
|
-
pending,
|
|
260
|
-
ownerWitness,
|
|
261
|
-
prepare: () => prepareCommittedContinuationDirectory(
|
|
262
|
-
root,
|
|
263
|
-
join(gitCommonDir(root), "ai-delivery", "continuations", String(issueNumber))
|
|
264
|
-
),
|
|
265
|
-
begin: (raw, ids) => {
|
|
266
|
-
const plan = CommittedContinuationPlanSchema.parse(raw);
|
|
267
|
-
if (plan.repoRoot !== resolve(root) || plan.row.issueNumber !== issueNumber || stableJson(plan.row) !== stableJson(original) || !plan.preserved.some((file) => stableJson(file) === stableJson(ownerWitness)))
|
|
268
|
-
throw new Error("Committed continuation lacks exact original custody.");
|
|
269
|
-
const content = { schemaVersion: "ai-delivery.committed-continuation-intent@1", plan, ...ids };
|
|
270
|
-
const next = ContinuationIntentSchema.parse({ ...content, intentId: digestValue(content) });
|
|
271
|
-
if (intent && stableJson(next) !== stableJson(intent))
|
|
272
|
-
throw new Error("Committed continuation intent drifted.");
|
|
273
|
-
assertContinuationPreserved(plan);
|
|
274
|
-
const evidenceDirectory = dirname(continuationBeforeimagePath(plan, plan.preserved[0].digest));
|
|
275
|
-
prepareCommittedContinuationDirectory(root, evidenceDirectory);
|
|
276
|
-
for (const file of plan.preserved)
|
|
277
|
-
writeCreateOnly(continuationBeforeimagePath(plan, file.digest), assertPrivateFile(file.path), file.digest);
|
|
278
|
-
const rowBytes = Buffer.from(stableJson(plan.row));
|
|
279
|
-
writeCreateOnly(continuationBeforeimagePath(plan, digestBytes(rowBytes)), rowBytes, digestBytes(rowBytes));
|
|
280
|
-
writeCommittedContinuationCheckpoint(continuationIntentPath(plan), next);
|
|
281
|
-
intent = next;
|
|
282
|
-
},
|
|
283
|
-
commit: () => {
|
|
284
|
-
if (!intent) throw new Error("Committed continuation lacks its durable intent.");
|
|
285
|
-
const { registry, row } = readCurrent();
|
|
286
|
-
if (stableJson(row) !== stableJson(current))
|
|
287
|
-
throw new Error("Committed continuation canonical row changed before commit.");
|
|
288
|
-
assertContinuationPreserved(intent.plan, true);
|
|
289
|
-
const replacementOwner = ownerContent(intent.plan.replacement);
|
|
290
|
-
writeCommittedContinuationCheckpoint(ownerPath(root, intent.plan.replacement), {
|
|
291
|
-
...replacementOwner,
|
|
292
|
-
ownerId: digestValue(replacementOwner)
|
|
293
|
-
});
|
|
294
|
-
registry.worktrees.splice(registry.worktrees.indexOf(row), 1, intent.plan.replacement);
|
|
295
|
-
saveRegistryStrict(root, registry);
|
|
296
|
-
current = intent.plan.replacement;
|
|
297
|
-
},
|
|
298
|
-
complete: () => {
|
|
299
|
-
if (!intent || stableJson(readCurrent().row) !== stableJson(intent.plan.replacement))
|
|
300
|
-
throw new Error("Committed continuation replacement row is incomplete.");
|
|
301
|
-
assertContinuationPreserved(intent.plan, true);
|
|
302
|
-
assertOwnerWitness(intent.plan.replacement, root);
|
|
303
|
-
const content = {
|
|
304
|
-
schemaVersion: "ai-delivery.committed-continuation-completion@1",
|
|
305
|
-
intentId: intent.intentId,
|
|
306
|
-
planId: intent.plan.planId,
|
|
307
|
-
row: intent.plan.replacement,
|
|
308
|
-
head: intent.plan.head,
|
|
309
|
-
completed: true
|
|
310
|
-
};
|
|
311
|
-
writeCommittedContinuationCheckpoint(continuationCompletionPath(intent.plan), {
|
|
312
|
-
...content,
|
|
313
|
-
completionId: digestValue(content)
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
async function addWorktreeEntry(entry, projectRoot) {
|
|
321
|
-
const root = projectRoot ?? process.cwd();
|
|
322
|
-
const normalizedEntry = {
|
|
323
|
-
...entry,
|
|
324
|
-
status: entry.status,
|
|
325
|
-
updatedAt: entry.updatedAt
|
|
326
|
-
};
|
|
327
|
-
ensureRegistryDirectory(root);
|
|
328
|
-
await withLock(REGISTRY_PATH, {
|
|
329
|
-
onTimeout: () => {
|
|
330
|
-
logWarn(LOCK_RETRY_MESSAGE);
|
|
331
|
-
},
|
|
332
|
-
operation: () => {
|
|
333
|
-
const registry = loadRegistryStrict(root);
|
|
334
|
-
const collisions = registry.worktrees.filter((existing2) => registryIdentitiesOverlap(existing2, normalizedEntry));
|
|
335
|
-
const [existing] = collisions;
|
|
336
|
-
if (collisions.length > 0) {
|
|
337
|
-
if (collisions.length === 1 && existing !== void 0 && isIdempotentRegistration(existing, normalizedEntry)) {
|
|
338
|
-
assertAiDeliveryWorktreeOwner(existing, root);
|
|
339
|
-
logInfo(`Worktree already registered: ${normalizedEntry.path}`);
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
if (collisions.length === 1 && existing !== void 0 && isMergedIssueReactivation(existing, normalizedEntry)) {
|
|
343
|
-
assertAiDeliveryWorktreeOwner(existing, root);
|
|
344
|
-
const existingIndex = registry.worktrees.indexOf(existing);
|
|
345
|
-
registry.worktrees.splice(existingIndex, 1, {
|
|
346
|
-
...normalizedEntry,
|
|
347
|
-
createdAt: existing.createdAt
|
|
348
|
-
});
|
|
349
|
-
saveRegistryStrict(root, registry);
|
|
350
|
-
logInfo(`Reactivated merged issue worktree: ${normalizedEntry.path}`);
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
throw new Error(
|
|
354
|
-
`New worktree entry conflicts with existing canonical registry identity: ${normalizedEntry.path}.`
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
|
-
writeWorktreeOwner(normalizedEntry, root);
|
|
358
|
-
registry.worktrees.push(normalizedEntry);
|
|
359
|
-
logInfo(`Added to worktree registry: ${normalizedEntry.path}`);
|
|
360
|
-
saveRegistryStrict(root, registry);
|
|
361
|
-
},
|
|
362
|
-
projectRoot: root,
|
|
363
|
-
timeout: 5e3
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
function getIssueWorktreeStrict(issueNumber, projectRoot) {
|
|
367
|
-
const root = projectRoot ?? process.cwd();
|
|
368
|
-
const registry = loadRegistryStrict(root);
|
|
369
|
-
const matching = registry.worktrees.filter((entry2) => entry2.type === "issue" && entry2.issueNumber === issueNumber);
|
|
370
|
-
const entry = matching[0];
|
|
371
|
-
if (matching.length !== 1 || entry === void 0) {
|
|
372
|
-
throw new Error(
|
|
373
|
-
`Expected exactly one canonical registry row for issue #${String(issueNumber)}; found ${String(matching.length)}.`
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
assertAiDeliveryWorktreeOwner(entry, root);
|
|
377
|
-
return entry;
|
|
378
|
-
}
|
|
379
|
-
function assertNativeIssueTrackingAdmission(issueNumber, root) {
|
|
380
|
-
assertIssueWorktreeTransitionAdmission(issueNumber, root);
|
|
381
|
-
const matches = listWorktreesStrict(root).filter(
|
|
382
|
-
(entry) => entry.type === "issue" && entry.issueNumber === issueNumber
|
|
383
|
-
);
|
|
384
|
-
if (matches.length > 1) throw new Error(`Issue #${String(issueNumber)} has duplicate worktree owners.`);
|
|
385
|
-
}
|
|
386
|
-
function getStaleWorktrees(daysOld, projectRoot) {
|
|
387
|
-
const root = projectRoot ?? process.cwd();
|
|
388
|
-
const registry = loadRegistry(root);
|
|
389
|
-
const cutoffDate = /* @__PURE__ */ new Date();
|
|
390
|
-
cutoffDate.setDate(cutoffDate.getDate() - daysOld);
|
|
391
|
-
return registry.worktrees.filter((entry) => {
|
|
392
|
-
const updatedAt = new Date(entry.updatedAt);
|
|
393
|
-
return updatedAt < cutoffDate || entry.status === "stale" || entry.status === "merged";
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
function getWorktreeByIssue(issueNumber, projectRoot) {
|
|
397
|
-
const root = projectRoot ?? process.cwd();
|
|
398
|
-
const registry = loadRegistry(root);
|
|
399
|
-
return registry.worktrees.find((w) => w.type === "issue" && w.issueNumber === issueNumber) ?? null;
|
|
400
|
-
}
|
|
401
|
-
function getWorktreeByPR(prNumber, projectRoot) {
|
|
402
|
-
const root = projectRoot ?? process.cwd();
|
|
403
|
-
const registry = loadRegistry(root);
|
|
404
|
-
return registry.worktrees.find((w) => w.type === "pr" && w.prNumber === prNumber) ?? null;
|
|
405
|
-
}
|
|
406
|
-
function listWorktrees(projectRoot) {
|
|
407
|
-
const root = projectRoot ?? process.cwd();
|
|
408
|
-
const registry = loadRegistry(root);
|
|
409
|
-
return registry.worktrees;
|
|
410
|
-
}
|
|
411
|
-
function listWorktreesStrict(projectRoot) {
|
|
412
|
-
const root = projectRoot ?? process.cwd();
|
|
413
|
-
const registry = loadRegistryStrict(root);
|
|
414
|
-
return registry.worktrees;
|
|
415
|
-
}
|
|
416
|
-
async function markStaleEntries(thresholdMs, projectRoot) {
|
|
417
|
-
const root = projectRoot ?? process.cwd();
|
|
418
|
-
let updated = 0;
|
|
419
|
-
ensureRegistryDirectory(root);
|
|
420
|
-
await withLock(REGISTRY_PATH, {
|
|
421
|
-
onTimeout: () => {
|
|
422
|
-
logWarn(LOCK_RETRY_MESSAGE);
|
|
423
|
-
},
|
|
424
|
-
operation: () => {
|
|
425
|
-
const registry = loadRegistryStrict(root);
|
|
426
|
-
const now = Date.now();
|
|
427
|
-
const newWorktrees = registry.worktrees.map((entry) => {
|
|
428
|
-
const age = now - new Date(entry.updatedAt).getTime();
|
|
429
|
-
if (age >= thresholdMs && entry.type !== "standalone" && entry.status !== "stale" && entry.status !== "merged") {
|
|
430
|
-
assertAiDeliveryWorktreeOwner(entry, root);
|
|
431
|
-
updated += 1;
|
|
432
|
-
return {
|
|
433
|
-
...entry,
|
|
434
|
-
status: "stale",
|
|
435
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
return entry;
|
|
439
|
-
});
|
|
440
|
-
if (updated > 0) {
|
|
441
|
-
registry.worktrees = newWorktrees;
|
|
442
|
-
saveRegistryStrict(root, registry);
|
|
443
|
-
}
|
|
444
|
-
},
|
|
445
|
-
projectRoot: root,
|
|
446
|
-
timeout: 5e3
|
|
447
|
-
});
|
|
448
|
-
return updated;
|
|
449
|
-
}
|
|
450
|
-
async function reconcileCompletedIssueWorktreeDelivery(options) {
|
|
451
|
-
if (!Number.isSafeInteger(options.prNumber) || options.prNumber <= 0) {
|
|
452
|
-
throw new Error(`Canonical PR number must be a positive integer for issue #${String(options.issueNumber)}.`);
|
|
453
|
-
}
|
|
454
|
-
const root = options.projectRoot ?? process.cwd();
|
|
455
|
-
ensureRegistryDirectory(root);
|
|
456
|
-
return await withLock(REGISTRY_PATH, {
|
|
457
|
-
onTimeout: () => {
|
|
458
|
-
logWarn(LOCK_RETRY_MESSAGE);
|
|
459
|
-
},
|
|
460
|
-
operation: () => {
|
|
461
|
-
const registry = loadRegistryStrict(root);
|
|
462
|
-
const matchingIndex = requireCanonicalIssueIndex({ ...options, registry });
|
|
463
|
-
const entry = registry.worktrees[matchingIndex];
|
|
464
|
-
if (entry === void 0) {
|
|
465
|
-
throw new Error(
|
|
466
|
-
`Canonical registry row for issue #${String(options.issueNumber)} disappeared during reconciliation.`
|
|
467
|
-
);
|
|
468
|
-
}
|
|
469
|
-
assertAiDeliveryWorktreeOwner(entry, root);
|
|
470
|
-
if (options.removeSource !== void 0 && registry.worktrees.some(
|
|
471
|
-
(other, index) => index !== matchingIndex && registryIdentitiesOverlap(other, { ...entry, prNumber: options.prNumber })
|
|
472
|
-
))
|
|
473
|
-
throw new Error("Another registry owner claims this source; preserve both registrations before cleanup.");
|
|
474
|
-
if (entry.status === "merged" && entry.prNumber === options.prNumber && options.removeSource === void 0) {
|
|
475
|
-
return "already-reconciled";
|
|
476
|
-
}
|
|
477
|
-
if (!(entry.status === "active" && entry.prNumber === void 0) && !(entry.status === "pr-published" && entry.prNumber === options.prNumber) && !(entry.status === "merged" && entry.prNumber === options.prNumber)) {
|
|
478
|
-
throw new Error(
|
|
479
|
-
`Invalid completed-delivery reconciliation for #${String(options.issueNumber)}: ${entry.status}.`
|
|
480
|
-
);
|
|
481
|
-
}
|
|
482
|
-
registry.worktrees.splice(matchingIndex, 1, {
|
|
483
|
-
...entry,
|
|
484
|
-
prNumber: options.prNumber,
|
|
485
|
-
status: "merged",
|
|
486
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
487
|
-
});
|
|
488
|
-
if (options.removeSource !== void 0) {
|
|
489
|
-
options.removeSource(registry.worktrees[matchingIndex]);
|
|
490
|
-
registry.worktrees.splice(matchingIndex, 1);
|
|
491
|
-
}
|
|
492
|
-
saveRegistryStrict(root, registry);
|
|
493
|
-
if (options.removeSource === void 0)
|
|
494
|
-
assertIssueDeliveryReadback({
|
|
495
|
-
options: { ...options, status: "merged" },
|
|
496
|
-
registry: loadRegistryStrict(root)
|
|
497
|
-
});
|
|
498
|
-
return "reconciled";
|
|
499
|
-
},
|
|
500
|
-
projectRoot: root,
|
|
501
|
-
timeout: 5e3
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
async function removeActiveStandaloneWorktreeEntry(input) {
|
|
505
|
-
const root = input.projectRoot;
|
|
506
|
-
ensureRegistryDirectory(root);
|
|
507
|
-
await withLock(REGISTRY_PATH, {
|
|
508
|
-
onTimeout: () => {
|
|
509
|
-
logWarn(LOCK_RETRY_MESSAGE);
|
|
510
|
-
},
|
|
511
|
-
operation: () => {
|
|
512
|
-
const registry = loadRegistryStrict(root);
|
|
513
|
-
const matchingIndexes = registry.worktrees.flatMap(
|
|
514
|
-
(entry2, index) => entry2.path === input.path || entry2.branch === input.branch ? [index] : []
|
|
515
|
-
);
|
|
516
|
-
const [matchingIndex] = matchingIndexes;
|
|
517
|
-
const entry = matchingIndex === void 0 ? void 0 : registry.worktrees[matchingIndex];
|
|
518
|
-
if (matchingIndexes.length !== 1 || entry === void 0 || entry.path !== input.path || entry.branch !== input.branch || entry.type !== "standalone" || entry.status !== "active" || entry.issueNumber !== void 0 || entry.prNumber !== void 0) {
|
|
519
|
-
throw new Error("Expected one exact active standalone registry row for qualification cleanup.");
|
|
520
|
-
}
|
|
521
|
-
if (matchingIndex === void 0) {
|
|
522
|
-
throw new Error("Qualification cleanup registry identity disappeared before removal.");
|
|
523
|
-
}
|
|
524
|
-
assertAiDeliveryWorktreeOwner(entry, root);
|
|
525
|
-
registry.worktrees.splice(matchingIndex, 1);
|
|
526
|
-
saveRegistryStrict(root, registry);
|
|
527
|
-
const readback = loadRegistryStrict(root).worktrees;
|
|
528
|
-
if (readback.some((candidate) => candidate.path === input.path || candidate.branch === input.branch)) {
|
|
529
|
-
throw new Error("Qualification cleanup registry readback retained the standalone identity.");
|
|
530
|
-
}
|
|
531
|
-
},
|
|
532
|
-
projectRoot: root,
|
|
533
|
-
timeout: 5e3
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
async function removeWorktreeEntry(path, projectRoot) {
|
|
537
|
-
const root = projectRoot ?? process.cwd();
|
|
538
|
-
ensureRegistryDirectory(root);
|
|
539
|
-
return await withLock(REGISTRY_PATH, {
|
|
540
|
-
onTimeout: () => {
|
|
541
|
-
logWarn(LOCK_RETRY_MESSAGE);
|
|
542
|
-
},
|
|
543
|
-
operation: () => {
|
|
544
|
-
const registry = loadRegistryStrict(root);
|
|
545
|
-
const matching = registry.worktrees.filter((row) => row.path === path);
|
|
546
|
-
if (matching.length > 1) throw new Error("Worktree registry contains duplicate paths.");
|
|
547
|
-
if (matching[0] !== void 0) {
|
|
548
|
-
assertAiDeliveryWorktreeOwner(matching[0], root);
|
|
549
|
-
registry.worktrees = registry.worktrees.filter((row) => row.path !== path);
|
|
550
|
-
saveRegistryStrict(root, registry);
|
|
551
|
-
logInfo(`Removed from worktree registry: ${path}`);
|
|
552
|
-
return true;
|
|
553
|
-
}
|
|
554
|
-
return false;
|
|
555
|
-
},
|
|
556
|
-
projectRoot: root,
|
|
557
|
-
timeout: 5e3
|
|
558
|
-
});
|
|
559
|
-
}
|
|
560
|
-
async function updateIssueWorktreeDelivery(options) {
|
|
561
|
-
const { branch, issueNumber, path, status } = options;
|
|
562
|
-
const root = options.projectRoot ?? process.cwd();
|
|
563
|
-
ensureRegistryDirectory(root);
|
|
564
|
-
await withLock(REGISTRY_PATH, {
|
|
565
|
-
onTimeout: () => {
|
|
566
|
-
logWarn(LOCK_RETRY_MESSAGE);
|
|
567
|
-
},
|
|
568
|
-
operation: () => {
|
|
569
|
-
const registry = loadRegistryStrict(root);
|
|
570
|
-
const matchingIndex = requireCanonicalIssueIndex({ branch, issueNumber, path, registry });
|
|
571
|
-
const entry = registry.worktrees[matchingIndex];
|
|
572
|
-
if (entry === void 0) {
|
|
573
|
-
throw new Error(`Canonical registry row for issue #${String(issueNumber)} disappeared during transition.`);
|
|
574
|
-
}
|
|
575
|
-
assertAiDeliveryWorktreeOwner(entry, root);
|
|
576
|
-
assertIssueDeliveryTransition({ entry, options });
|
|
577
|
-
registry.worktrees.splice(matchingIndex, 1, {
|
|
578
|
-
...entry,
|
|
579
|
-
...options.status === "pr-published" ? { prNumber: options.prNumber } : {},
|
|
580
|
-
status,
|
|
581
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
582
|
-
});
|
|
583
|
-
saveRegistryStrict(root, registry);
|
|
584
|
-
assertIssueDeliveryReadback({ options, registry: loadRegistryStrict(root) });
|
|
585
|
-
},
|
|
586
|
-
projectRoot: root,
|
|
587
|
-
timeout: 5e3
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
function assertIssueDeliveryReadback({
|
|
591
|
-
options,
|
|
592
|
-
registry
|
|
593
|
-
}) {
|
|
594
|
-
const rows = registry.worktrees.filter(
|
|
595
|
-
(candidate) => candidate.type === "issue" && candidate.issueNumber === options.issueNumber
|
|
596
|
-
);
|
|
597
|
-
const entry = rows[0];
|
|
598
|
-
if (rows.length !== 1 || entry === void 0 || entry.branch !== options.branch || entry.path !== options.path || entry.status !== options.status || options.status === "pr-published" && entry.prNumber !== options.prNumber) {
|
|
599
|
-
throw new Error(`Canonical registry transition readback failed for issue #${String(options.issueNumber)}.`);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
function assertIssueDeliveryTransition({
|
|
603
|
-
entry,
|
|
604
|
-
options
|
|
605
|
-
}) {
|
|
606
|
-
if (options.status === "pr-published") {
|
|
607
|
-
if (!Number.isSafeInteger(options.prNumber) || options.prNumber <= 0) {
|
|
608
|
-
throw new Error(`Canonical PR number must be a positive integer for issue #${String(options.issueNumber)}.`);
|
|
609
|
-
}
|
|
610
|
-
if (entry.status === "active" && entry.prNumber === void 0) {
|
|
611
|
-
return;
|
|
612
|
-
}
|
|
613
|
-
if (entry.status === "pr-published" && entry.prNumber === options.prNumber) {
|
|
614
|
-
return;
|
|
615
|
-
}
|
|
616
|
-
if (entry.status === "pr-published" && entry.prNumber !== void 0) {
|
|
617
|
-
throw new Error(
|
|
618
|
-
`Issue #${String(options.issueNumber)} cannot replace canonical PR #${String(entry.prNumber)} with #${String(options.prNumber)}.`
|
|
619
|
-
);
|
|
620
|
-
}
|
|
621
|
-
throw new Error(
|
|
622
|
-
`Invalid issue delivery transition for #${String(options.issueNumber)}: ${entry.status} -> pr-published.`
|
|
623
|
-
);
|
|
624
|
-
}
|
|
625
|
-
if ((entry.status === "pr-published" || entry.status === "merged") && entry.prNumber !== void 0 && Number.isSafeInteger(entry.prNumber) && entry.prNumber > 0) {
|
|
626
|
-
return;
|
|
627
|
-
}
|
|
628
|
-
throw new Error(`Invalid issue delivery transition for #${String(options.issueNumber)}: ${entry.status} -> merged.`);
|
|
629
|
-
}
|
|
630
|
-
function ensureRegistryDirectory(root) {
|
|
631
|
-
ensureDataDirectory(root);
|
|
632
|
-
}
|
|
633
|
-
function isIdempotentRegistration(existing, incoming) {
|
|
634
|
-
return existing.branch === incoming.branch && existing.identity === incoming.identity && existing.issueNumber === incoming.issueNumber && existing.path === incoming.path && existing.prNumber === incoming.prNumber && existing.status === incoming.status && existing.type === incoming.type;
|
|
635
|
-
}
|
|
636
|
-
function isMergedIssueReactivation(existing, incoming) {
|
|
637
|
-
return existing.branch === incoming.branch && existing.identity === incoming.identity && existing.issueNumber === incoming.issueNumber && existing.path === incoming.path && existing.prNumber !== void 0 && existing.status === "merged" && existing.type === "issue" && incoming.prNumber === void 0 && incoming.status === "active" && incoming.type === "issue";
|
|
638
|
-
}
|
|
639
|
-
function isRecord(value) {
|
|
640
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
641
|
-
}
|
|
642
|
-
function loadRegistry(root) {
|
|
643
|
-
const registryPath = resolve(root, REGISTRY_PATH);
|
|
644
|
-
if (!existsSync(registryPath)) {
|
|
645
|
-
return { worktrees: [] };
|
|
646
|
-
}
|
|
647
|
-
try {
|
|
648
|
-
const raw = readFileSync(registryPath, "utf8");
|
|
649
|
-
const parsed = JSON.parse(raw);
|
|
650
|
-
return {
|
|
651
|
-
worktrees: parsed.worktrees.map((entry) => {
|
|
652
|
-
const status = normalizeStatus(entry.status);
|
|
653
|
-
return {
|
|
654
|
-
branch: entry.branch,
|
|
655
|
-
createdAt: entry.createdAt,
|
|
656
|
-
...entry.identity !== void 0 ? { identity: entry.identity } : {},
|
|
657
|
-
...entry.issueNumber !== void 0 ? { issueNumber: entry.issueNumber } : {},
|
|
658
|
-
path: entry.path,
|
|
659
|
-
...entry.prNumber !== void 0 ? { prNumber: entry.prNumber } : {},
|
|
660
|
-
status,
|
|
661
|
-
type: entry.type,
|
|
662
|
-
updatedAt: entry.updatedAt ?? entry.createdAt
|
|
663
|
-
};
|
|
664
|
-
})
|
|
665
|
-
};
|
|
666
|
-
} catch (error) {
|
|
667
|
-
logWarn(`Failed to load worktree registry: ${error instanceof Error ? error.message : String(error)}`);
|
|
668
|
-
return { worktrees: [] };
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
function loadRegistryStrict(root) {
|
|
672
|
-
const registryPath = resolve(root, REGISTRY_PATH);
|
|
673
|
-
if (!existsSync(registryPath)) {
|
|
674
|
-
return { worktrees: [] };
|
|
675
|
-
}
|
|
676
|
-
try {
|
|
677
|
-
const raw = readFileSync(registryPath, "utf8");
|
|
678
|
-
const parsed = JSON.parse(raw);
|
|
679
|
-
if (!isRecord(parsed) || Object.keys(parsed).length !== 1 || !Array.isArray(parsed.worktrees)) {
|
|
680
|
-
throw new Error("Registry must be an object with a worktrees array.");
|
|
681
|
-
}
|
|
682
|
-
return {
|
|
683
|
-
worktrees: parsed.worktrees.map((entry, index) => normalizeStrictWorktreeEntry(entry, index))
|
|
684
|
-
};
|
|
685
|
-
} catch (error) {
|
|
686
|
-
throw new Error(`Unable to read worktree registry: ${error instanceof Error ? error.message : String(error)}`);
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
function normalizeStatus(raw) {
|
|
690
|
-
if (raw === "merged" || raw === "pr-published" || raw === "active" || raw === "error" || raw === "stale") {
|
|
691
|
-
return raw;
|
|
692
|
-
}
|
|
693
|
-
throw new Error("Worktree registry status is invalid.");
|
|
694
|
-
}
|
|
695
|
-
function normalizeStrictWorktreeEntry(entry, index) {
|
|
696
|
-
if (!isRecord(entry)) {
|
|
697
|
-
throw new Error(`worktrees[${String(index)}] must be an object.`);
|
|
698
|
-
}
|
|
699
|
-
const allowed = /* @__PURE__ */ new Set([
|
|
700
|
-
"branch",
|
|
701
|
-
"createdAt",
|
|
702
|
-
"identity",
|
|
703
|
-
"issueNumber",
|
|
704
|
-
"path",
|
|
705
|
-
"prNumber",
|
|
706
|
-
"status",
|
|
707
|
-
"type",
|
|
708
|
-
"updatedAt"
|
|
709
|
-
]);
|
|
710
|
-
const unknown = Object.keys(entry).filter((key) => !allowed.has(key));
|
|
711
|
-
if (unknown.length > 0) {
|
|
712
|
-
throw new Error(`worktrees[${String(index)}] has unsupported fields: ${unknown.join(", ")}.`);
|
|
713
|
-
}
|
|
714
|
-
const branch = requiredString(entry.branch, `worktrees[${String(index)}].branch`);
|
|
715
|
-
const createdAt = requiredString(entry.createdAt, `worktrees[${String(index)}].createdAt`);
|
|
716
|
-
const path = requiredString(entry.path, `worktrees[${String(index)}].path`);
|
|
717
|
-
const type = requiredWorktreeType(entry.type, `worktrees[${String(index)}].type`);
|
|
718
|
-
const status = requiredWorktreeStatus(entry.status, `worktrees[${String(index)}].status`);
|
|
719
|
-
const identity = optionalString(entry.identity, `worktrees[${String(index)}].identity`);
|
|
720
|
-
const issueNumber = optionalPositiveInteger(entry.issueNumber, `worktrees[${String(index)}].issueNumber`);
|
|
721
|
-
const prNumber = optionalPositiveInteger(entry.prNumber, `worktrees[${String(index)}].prNumber`);
|
|
722
|
-
const updatedAt = requiredString(entry.updatedAt, `worktrees[${String(index)}].updatedAt`);
|
|
723
|
-
if (type === "issue" && issueNumber === void 0) {
|
|
724
|
-
throw new Error(`worktrees[${String(index)}].issueNumber is required for issue worktrees.`);
|
|
725
|
-
}
|
|
726
|
-
return {
|
|
727
|
-
branch,
|
|
728
|
-
createdAt,
|
|
729
|
-
...identity === void 0 ? {} : { identity },
|
|
730
|
-
...issueNumber === void 0 ? {} : { issueNumber },
|
|
731
|
-
path,
|
|
732
|
-
...prNumber === void 0 ? {} : { prNumber },
|
|
733
|
-
status,
|
|
734
|
-
type,
|
|
735
|
-
updatedAt
|
|
736
|
-
};
|
|
737
|
-
}
|
|
738
|
-
function optionalPositiveInteger(value, label) {
|
|
739
|
-
if (value === void 0) {
|
|
740
|
-
return void 0;
|
|
741
|
-
}
|
|
742
|
-
if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
|
|
743
|
-
throw new Error(`${label} must be a positive integer.`);
|
|
744
|
-
}
|
|
745
|
-
return value;
|
|
746
|
-
}
|
|
747
|
-
function optionalString(value, label) {
|
|
748
|
-
if (value === void 0) {
|
|
749
|
-
return void 0;
|
|
750
|
-
}
|
|
751
|
-
return requiredString(value, label);
|
|
752
|
-
}
|
|
753
|
-
function registryIdentitiesOverlap(left, right) {
|
|
754
|
-
return left.path === right.path || left.branch === right.branch || left.issueNumber !== void 0 && right.issueNumber !== void 0 && left.issueNumber === right.issueNumber || left.prNumber !== void 0 && right.prNumber !== void 0 && left.prNumber === right.prNumber;
|
|
755
|
-
}
|
|
756
|
-
function requireCanonicalIssueIndex({
|
|
757
|
-
branch,
|
|
758
|
-
issueNumber,
|
|
759
|
-
path,
|
|
760
|
-
registry
|
|
761
|
-
}) {
|
|
762
|
-
const issueIndexes = registry.worktrees.flatMap(
|
|
763
|
-
(entry2, index) => entry2.type === "issue" && entry2.issueNumber === issueNumber ? [index] : []
|
|
764
|
-
);
|
|
765
|
-
if (issueIndexes.length !== 1) {
|
|
766
|
-
throw new Error(
|
|
767
|
-
`Expected exactly one canonical registry row for issue #${String(issueNumber)}; found ${String(issueIndexes.length)}.`
|
|
768
|
-
);
|
|
769
|
-
}
|
|
770
|
-
const matchingIndex = issueIndexes[0];
|
|
771
|
-
const entry = matchingIndex === void 0 ? void 0 : registry.worktrees[matchingIndex];
|
|
772
|
-
if (matchingIndex === void 0 || entry === void 0) {
|
|
773
|
-
throw new Error(`Canonical registry row for issue #${String(issueNumber)} disappeared during transition.`);
|
|
774
|
-
}
|
|
775
|
-
if (entry.branch !== branch || entry.path !== path) {
|
|
776
|
-
throw new Error(
|
|
777
|
-
`Canonical registry row for issue #${String(issueNumber)} does not match branch ${branch} and path ${path}.`
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
return matchingIndex;
|
|
781
|
-
}
|
|
782
|
-
function requiredString(value, label) {
|
|
783
|
-
if (typeof value !== "string" || value.trim().length === 0) {
|
|
784
|
-
throw new Error(`${label} must be a non-empty string.`);
|
|
785
|
-
}
|
|
786
|
-
return value;
|
|
787
|
-
}
|
|
788
|
-
function requiredWorktreeStatus(value, label) {
|
|
789
|
-
if (value === "merged" || value === "pr-published" || value === "active" || value === "error" || value === "stale") {
|
|
790
|
-
return value;
|
|
791
|
-
}
|
|
792
|
-
throw new Error(`${label} is invalid.`);
|
|
793
|
-
}
|
|
794
|
-
function requiredWorktreeType(value, label) {
|
|
795
|
-
if (value === "issue" || value === "pr" || value === "standalone") {
|
|
796
|
-
return value;
|
|
797
|
-
}
|
|
798
|
-
throw new Error(`${label} is invalid.`);
|
|
799
|
-
}
|
|
800
|
-
function saveRegistryStrict(root, registry) {
|
|
801
|
-
ensureRegistryDirectory(root);
|
|
802
|
-
const registryPath = resolve(root, REGISTRY_PATH);
|
|
803
|
-
try {
|
|
804
|
-
writeJsonFileAtomically(registryPath, registry);
|
|
805
|
-
} catch (error) {
|
|
806
|
-
throw new Error(`Unable to save worktree registry: ${error instanceof Error ? error.message : String(error)}`);
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
export {
|
|
810
|
-
CommittedContinuationPlanSchema,
|
|
811
|
-
addWorktreeEntry,
|
|
812
|
-
assertAiDeliveryWorktreeOwner,
|
|
813
|
-
assertIssueWorktreeTransitionAdmission,
|
|
814
|
-
assertNativeIssueTrackingAdmission,
|
|
815
|
-
committedContinuationPlanPath,
|
|
816
|
-
getIssueWorktreeStrict,
|
|
817
|
-
getStaleWorktrees,
|
|
818
|
-
getWorktreeByIssue,
|
|
819
|
-
getWorktreeByPR,
|
|
820
|
-
listWorktrees,
|
|
821
|
-
listWorktreesStrict,
|
|
822
|
-
markStaleEntries,
|
|
823
|
-
pendingCommittedContinuation,
|
|
824
|
-
reconcileCompletedIssueWorktreeDelivery,
|
|
825
|
-
removeActiveStandaloneWorktreeEntry,
|
|
826
|
-
removeWorktreeEntry,
|
|
827
|
-
updateIssueWorktreeDelivery,
|
|
828
|
-
withCommittedContinuationRegistry,
|
|
829
|
-
writeCommittedContinuationCheckpoint
|
|
830
|
-
};
|
|
831
|
-
//# sourceMappingURL=worktreeRegistry.js.map
|