@akagilnc/pi-workflow-roles 0.1.6126 → 0.1.6134

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.
Files changed (36) hide show
  1. package/dist/acp-host/production-host.js +15738 -15781
  2. package/dist/headless-host/production-host.js +6214 -6257
  3. package/dist/public-cli/invocation.js +1 -1
  4. package/dist/public-cli/main.js +11211 -11254
  5. package/dist/public-cli/post-admission.js +1 -1
  6. package/dist/public-cli/run-lifecycle.js +1 -4
  7. package/dist/role-run-path-rewrite.js +18 -62
  8. package/dist/role-run-relocation.js +6 -272
  9. package/dist/run-ticket-number.js +0 -15
  10. package/dist/ticket-provenance-contracts.js +0 -24
  11. package/dist/ticket-provenance.js +1 -1
  12. package/package.json +2 -3
  13. package/scripts/build-package.mjs +0 -30
  14. package/src/public-cli/invocation.ts +2 -1
  15. package/src/public-cli/post-admission.ts +2 -1
  16. package/src/public-cli/run-lifecycle.ts +1 -4
  17. package/src/role-run-path-rewrite.ts +33 -92
  18. package/src/role-run-relocation.ts +5 -322
  19. package/src/run-ticket-number.ts +5 -38
  20. package/src/ticket-provenance-contracts.ts +0 -32
  21. package/src/ticket-provenance.ts +1 -1
  22. package/dist/book-topology-migration-placement.js +0 -404
  23. package/dist/book-topology-migration.js +0 -125
  24. package/dist/book-topology-migrators-t11.js +0 -677
  25. package/dist/book-topology-mixed-volume-migrators.js +0 -322
  26. package/dist/book-topology-partition-migrators.js +0 -23
  27. package/dist/book-topology-record-class-migrators.js +0 -671
  28. package/dist/book-topology-runs-migrator.js +0 -272
  29. package/dist/migrate-book-topology.js +0 -13982
  30. package/src/book-topology-migration-placement.ts +0 -501
  31. package/src/book-topology-migration.ts +0 -213
  32. package/src/book-topology-migrators-t11.ts +0 -857
  33. package/src/book-topology-mixed-volume-migrators.ts +0 -453
  34. package/src/book-topology-partition-migrators.ts +0 -25
  35. package/src/book-topology-record-class-migrators.ts +0 -882
  36. package/src/book-topology-runs-migrator.ts +0 -397
@@ -1,397 +0,0 @@
1
- /**
2
- * #865 T9: migrate retained runs into ticket-scoped or unbound placement.
3
- * Inventory covers legacy flat `runs/`, existing `<ticket>/runs/`, and
4
- * `unbound/runs/`. Attribution reuses the shared migrating-run helper;
5
- * already-canonical ticket trees copy as-is. Unbound leaves that already
6
- * hold a board typed ticketNumber place under that ticket (#863 stock).
7
- */
8
- import { cp, mkdir, rename, readdir, stat, writeFile } from "node:fs/promises";
9
- import { basename, dirname, join, relative, sep } from "node:path";
10
-
11
- import {
12
- destinationRunDirectory,
13
- isTicketNumberString,
14
- listBackupRunLeaves,
15
- listMigrationBookKeys,
16
- resolveMigratingRunTicket,
17
- } from "./book-topology-migration-placement.ts";
18
- import {
19
- reconcileMigrationPartition,
20
- type BookTopologyMigrationContext,
21
- type BookTopologyPartitionMigrator,
22
- type MigrationItemOutcome,
23
- } from "./book-topology-migration.ts";
24
- import { listBookRunDirectories } from "./role-run-placement.ts";
25
- import {
26
- rewriteRoleRunDurablePages,
27
- type RunDirectoryPathRewrite,
28
- } from "./role-run-relocation.ts";
29
- import { renderCurrentSync } from "./run-dossier.ts";
30
- import {
31
- MIGRATION_TICKET_DERIVATION_PAGE,
32
- readMigrationBoardTicketNumber,
33
- } from "./run-ticket-number.ts";
34
-
35
- import { isEnoent } from "./unknown-value.ts";
36
-
37
- const RUNS_PARTITION = "runs";
38
-
39
- const RUN_LEAF =
40
- /^([A-Za-z0-9][A-Za-z0-9._-]*)@([A-Za-z][A-Za-z0-9_-]*)$/;
41
-
42
- type RunLeaf = { readonly runId: string; readonly role: string };
43
-
44
- type PlannedRunMove = {
45
- readonly sourcePath: string;
46
- readonly sourceIdentity: string;
47
- readonly isDirectory: boolean;
48
- readonly disposition: "placed" | "unbound";
49
- readonly targetPath: string;
50
- readonly historicalRunDirectory: string | undefined;
51
- readonly derivation:
52
- | { readonly ticketNumber: number; readonly projectRoot: string; readonly sourcePage: string }
53
- | undefined;
54
- };
55
-
56
- function parseRunLeaf(name: string): RunLeaf | undefined {
57
- const match = RUN_LEAF.exec(name);
58
- if (match === null) return undefined;
59
- return { runId: match[1]!, role: match[2]! };
60
- }
61
-
62
- async function pathExists(path: string): Promise<boolean> {
63
- try {
64
- await stat(path);
65
- return true;
66
- } catch (error) {
67
- if (isEnoent(error)) return false;
68
- throw error;
69
- }
70
- }
71
-
72
- async function writeDerivationPage(
73
- targetRunDirectory: string,
74
- derivation: {
75
- readonly ticketNumber: number;
76
- readonly projectRoot: string;
77
- readonly sourcePage: string;
78
- },
79
- ): Promise<void> {
80
- const page = {
81
- ticketNumber: derivation.ticketNumber,
82
- derivation: "worktree-path-basename" as const,
83
- source: {
84
- page: derivation.sourcePage,
85
- field: "projectRoot",
86
- path: derivation.projectRoot,
87
- basename: basename(derivation.projectRoot),
88
- },
89
- };
90
- await writeFile(
91
- join(targetRunDirectory, MIGRATION_TICKET_DERIVATION_PAGE),
92
- `${JSON.stringify(page, null, 2)}\n`,
93
- "utf8",
94
- );
95
- }
96
-
97
- async function copyRunTree(
98
- source: string,
99
- target: string,
100
- isDirectory: boolean,
101
- ): Promise<void> {
102
- await mkdir(dirname(target), { recursive: true });
103
- if (isDirectory) {
104
- await cp(source, target, { recursive: true, preserveTimestamps: true });
105
- return;
106
- }
107
- await cp(source, target, { preserveTimestamps: true });
108
- }
109
-
110
- async function planBookMoves(
111
- backupBooksDirectory: string,
112
- booksDirectory: string,
113
- bookKey: string,
114
- ): Promise<readonly PlannedRunMove[]> {
115
- const backupBook = join(backupBooksDirectory, bookKey);
116
- const claimed = new Map<string, string>();
117
- const planned: PlannedRunMove[] = [];
118
-
119
- for (const leaf of await listBackupRunLeaves(backupBook)) {
120
- const sourceIdentity = relative(backupBooksDirectory, leaf.sourcePath)
121
- .split(sep)
122
- .join("/");
123
- const parsed = leaf.isDirectory ? parseRunLeaf(leaf.leafName) : undefined;
124
-
125
- let targetPath: string;
126
- let disposition: "placed" | "unbound";
127
- let historicalRunDirectory: string | undefined;
128
- let derivation: PlannedRunMove["derivation"];
129
-
130
- if (leaf.layout === "issues") {
131
- // issues/<ticket>/runs/<leaf> → <ticket>/runs/<leaf>; conflict refuses below.
132
- const parts = leaf.relativePath.replaceAll("\\", "/").split("/");
133
- const ticketStr = parts[1];
134
- if (ticketStr !== undefined && isTicketNumberString(ticketStr)) {
135
- targetPath = join(booksDirectory, bookKey, ticketStr, "runs", leaf.leafName);
136
- disposition = "placed";
137
- historicalRunDirectory = join(
138
- booksDirectory,
139
- bookKey,
140
- "issues",
141
- ticketStr,
142
- "runs",
143
- leaf.leafName,
144
- );
145
- } else {
146
- targetPath = join(booksDirectory, bookKey, "unbound", "runs", leaf.leafName);
147
- disposition = "unbound";
148
- historicalRunDirectory = join(booksDirectory, bookKey, leaf.relativePath);
149
- }
150
- derivation = undefined;
151
- } else if (leaf.layout === "unbound") {
152
- // #863: board typed ticket on an unbound leaf → place under that ticket.
153
- // No board ticket → stay unbound. Never invent from prose or derivation.
154
- const boardTicket = leaf.isDirectory
155
- ? await readMigrationBoardTicketNumber(leaf.sourcePath)
156
- : undefined;
157
- if (parsed !== undefined && boardTicket !== undefined) {
158
- targetPath = destinationRunDirectory(
159
- booksDirectory,
160
- bookKey,
161
- boardTicket,
162
- parsed.runId,
163
- parsed.role,
164
- );
165
- disposition = "placed";
166
- historicalRunDirectory = join(
167
- booksDirectory,
168
- bookKey,
169
- "unbound",
170
- "runs",
171
- leaf.leafName,
172
- );
173
- } else {
174
- targetPath = join(booksDirectory, bookKey, leaf.relativePath);
175
- disposition = "unbound";
176
- historicalRunDirectory = undefined;
177
- }
178
- derivation = undefined;
179
- } else if (leaf.layout !== "flat") {
180
- // Already-canonical `<ticket>/runs/` — copy as-is.
181
- targetPath = join(booksDirectory, bookKey, leaf.relativePath);
182
- disposition = "placed";
183
- historicalRunDirectory = undefined;
184
- derivation = undefined;
185
- } else if (parsed === undefined) {
186
- targetPath = join(booksDirectory, bookKey, "unbound", "runs", leaf.leafName);
187
- disposition = "unbound";
188
- historicalRunDirectory = join(booksDirectory, bookKey, "runs", leaf.leafName);
189
- derivation = undefined;
190
- } else {
191
- const ticket = await resolveMigratingRunTicket(leaf.sourcePath);
192
- targetPath = destinationRunDirectory(
193
- booksDirectory,
194
- bookKey,
195
- ticket.ticketNumber,
196
- parsed.runId,
197
- parsed.role,
198
- );
199
- disposition = ticket.ticketNumber === undefined ? "unbound" : "placed";
200
- historicalRunDirectory = join(booksDirectory, bookKey, "runs", leaf.leafName);
201
- derivation =
202
- ticket.derivation?.method === "project-root-basename"
203
- && ticket.ticketNumber !== undefined
204
- ? {
205
- ticketNumber: ticket.ticketNumber,
206
- projectRoot: ticket.derivation.source,
207
- sourcePage: ticket.derivation.sourcePage,
208
- }
209
- : undefined;
210
- }
211
-
212
- const prior = claimed.get(targetPath);
213
- if (prior !== undefined) {
214
- throw new Error(
215
- `book topology migration refuses to overwrite ${targetPath} (already claimed by ${prior}) with ${leaf.sourcePath}`,
216
- );
217
- }
218
- claimed.set(targetPath, leaf.sourcePath);
219
- planned.push({
220
- sourcePath: leaf.sourcePath,
221
- sourceIdentity,
222
- isDirectory: leaf.isDirectory,
223
- disposition,
224
- targetPath,
225
- historicalRunDirectory,
226
- derivation,
227
- });
228
- }
229
- return planned;
230
- }
231
-
232
- export const bookTopologyRunsMigrator: BookTopologyPartitionMigrator = {
233
- partition: RUNS_PARTITION,
234
- async migrate(context: BookTopologyMigrationContext) {
235
- const outcomes: MigrationItemOutcome[] = [];
236
- const { backupBooksDirectory, booksDirectory } = context;
237
-
238
- for (const bookKey of await listMigrationBookKeys(backupBooksDirectory)) {
239
- const planned = await planBookMoves(
240
- backupBooksDirectory,
241
- booksDirectory,
242
- bookKey,
243
- );
244
-
245
- const crossRunRewrites: RunDirectoryPathRewrite[] = planned
246
- .filter((move) => move.isDirectory && move.historicalRunDirectory !== undefined)
247
- .map((move) => ({
248
- oldRunDirectory: move.historicalRunDirectory!,
249
- newRunDirectory: move.targetPath,
250
- }));
251
-
252
- for (const move of planned) {
253
- if (await pathExists(move.targetPath)) {
254
- throw new Error(
255
- `book topology migration refuses to overwrite ${move.targetPath} with ${move.sourcePath}`,
256
- );
257
- }
258
- await copyRunTree(move.sourcePath, move.targetPath, move.isDirectory);
259
- if (move.isDirectory) {
260
- await rewriteRoleRunDurablePages({
261
- pagesDirectory: move.targetPath,
262
- oldRunDirectory: move.historicalRunDirectory ?? move.targetPath,
263
- newRunDirectory: move.targetPath,
264
- crossRunRewrites,
265
- });
266
- }
267
- if (move.isDirectory && move.derivation !== undefined) {
268
- await writeDerivationPage(move.targetPath, move.derivation);
269
- }
270
-
271
- outcomes.push({
272
- disposition: move.disposition,
273
- source: move.sourceIdentity,
274
- });
275
- }
276
- }
277
-
278
- return reconcileMigrationPartition(RUNS_PARTITION, "entries", outcomes);
279
- },
280
- };
281
-
282
- export type BoardBoundUnboundRelocation = {
283
- readonly from: string;
284
- readonly to: string;
285
- readonly ticketNumber: number;
286
- };
287
-
288
- type PlannedBoardBoundMove = {
289
- readonly sourcePath: string;
290
- readonly targetPath: string;
291
- readonly ticketNumber: number;
292
- };
293
-
294
- /**
295
- * Live in-place repair for already-migrated trees (#863 stock): plan the full
296
- * board-bound unbound→ticket batch first, refuse any overwrite before mutation,
297
- * rewrite durable pages (including batch `crossRunRewrites`) while sources still
298
- * sit at unbound, then rename. Parse/write failures therefore leave sources in
299
- * place so a retry can finish the same closure; rename-only retries still find
300
- * remaining unbound sources. Leaves without a board ticket stay unbound.
301
- */
302
- export async function relocateBoardBoundUnboundRunsInBook(
303
- bookDirectory: string,
304
- ): Promise<readonly BoardBoundUnboundRelocation[]> {
305
- const bookKey = basename(bookDirectory);
306
- const booksDirectory = dirname(bookDirectory);
307
- const unboundRuns = join(bookDirectory, "unbound", "runs");
308
- let entries;
309
- try {
310
- entries = await readdir(unboundRuns, { withFileTypes: true });
311
- } catch (error) {
312
- if (isEnoent(error)) return [];
313
- throw error;
314
- }
315
-
316
- const planned: PlannedBoardBoundMove[] = [];
317
- for (const entry of entries) {
318
- if (!entry.isDirectory()) continue;
319
- const parsed = parseRunLeaf(entry.name);
320
- if (parsed === undefined) continue;
321
- const sourcePath = join(unboundRuns, entry.name);
322
- const boardTicket = await readMigrationBoardTicketNumber(sourcePath);
323
- if (boardTicket === undefined) continue;
324
- const targetPath = destinationRunDirectory(
325
- booksDirectory,
326
- bookKey,
327
- boardTicket,
328
- parsed.runId,
329
- parsed.role,
330
- );
331
- planned.push({
332
- sourcePath,
333
- targetPath,
334
- ticketNumber: boardTicket,
335
- });
336
- }
337
-
338
- if (planned.length === 0) return [];
339
-
340
- for (const move of planned) {
341
- if (await pathExists(move.targetPath)) {
342
- throw new Error(
343
- `board-bound unbound relocate refuses to overwrite ${move.targetPath} with ${move.sourcePath}`,
344
- );
345
- }
346
- }
347
-
348
- const crossRunRewrites: RunDirectoryPathRewrite[] = planned.map((move) => ({
349
- oldRunDirectory: move.sourcePath,
350
- newRunDirectory: move.targetPath,
351
- }));
352
- const relocatedBySource = new Map(
353
- planned.map((move) => [move.sourcePath, move] as const),
354
- );
355
-
356
- // Rewrite before any rename so durable-page failures remain retryable from
357
- // the same unbound sources (failure-honesty: no relocatedCount=0 whitewash).
358
- for (const runDir of await listBookRunDirectories(bookDirectory)) {
359
- const move = relocatedBySource.get(runDir);
360
- await rewriteRoleRunDurablePages({
361
- pagesDirectory: runDir,
362
- oldRunDirectory: move?.sourcePath ?? runDir,
363
- newRunDirectory: move?.targetPath ?? runDir,
364
- crossRunRewrites,
365
- });
366
- }
367
-
368
- for (const move of planned) {
369
- await mkdir(dirname(move.targetPath), { recursive: true });
370
- await rename(move.sourcePath, move.targetPath);
371
- // Same live relocate seam: derived host.original follows the new run directory.
372
- renderCurrentSync(move.targetPath);
373
- }
374
-
375
- return planned.map((move) => ({
376
- from: move.sourcePath,
377
- to: move.targetPath,
378
- ticketNumber: move.ticketNumber,
379
- }));
380
- }
381
-
382
- /**
383
- * Walk every book under `booksDirectory` and relocate board-bound unbound
384
- * runs in place without the whole-books topology migration's in-flight gate.
385
- */
386
- export async function relocateBoardBoundUnboundRunsInBooks(
387
- booksDirectory: string,
388
- ): Promise<readonly BoardBoundUnboundRelocation[]> {
389
- const relocated: BoardBoundUnboundRelocation[] = [];
390
- for (const bookKey of await listMigrationBookKeys(booksDirectory)) {
391
- const batch = await relocateBoardBoundUnboundRunsInBook(
392
- join(booksDirectory, bookKey),
393
- );
394
- relocated.push(...batch);
395
- }
396
- return relocated;
397
- }