@akagilnc/pi-workflow-roles 0.1.6111 → 0.1.6115

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.
@@ -26,7 +26,7 @@ import { basename, dirname, join, relative, sep } from "node:path";
26
26
  import { resolveHostDossierLandingPath } from "./host-session-record.ts";
27
27
  import type { HostContext } from "./host-contracts.ts";
28
28
  import { runDirectoryFromHostContext } from "./host-contracts.ts";
29
- import { receiptStatus } from "./packaged-role-registry.ts";
29
+ import { packagedGateStageLabel, receiptStatus } from "./packaged-role-registry.ts";
30
30
  import {
31
31
  isUnboundRunDirectory,
32
32
  sessionDirectoryOf,
@@ -35,7 +35,7 @@ import {
35
35
  } from "./role-run-placement.ts";
36
36
  import { readPageSync } from "./run-dossier.ts";
37
37
  import { parseSitianRecordText } from "./sitian-reader.ts";
38
- import { isRecord } from "./unknown-value.ts";
38
+ import { errorText, isRecord } from "./unknown-value.ts";
39
39
 
40
40
  const CURRENT_FILENAME = "current.jsonl" as const;
41
41
  const RECEIPTS_DIRNAME = "receipts" as const;
@@ -47,6 +47,11 @@ export type TicketProgressLine = {
47
47
  readonly seat: string;
48
48
  /** Seat-local round; reviewer dual-lens uses `<n>.<lens>`. Empty while unbound. */
49
49
  readonly round: string;
50
+ /**
51
+ * #1200 `<seat>#<round>` this row reviews or fixes. Officer rows copy the value
52
+ * pinned at admission; fixer rows copy the receipt's own `for`. Absent otherwise.
53
+ */
54
+ readonly for?: string;
50
55
  /** This-round summons instruction bytes; empty string when none. */
51
56
  readonly instruction: string;
52
57
  /** `git rev-parse HEAD` at seal from the seat worktree; empty on failure. */
@@ -62,18 +67,17 @@ export type TicketProgressLine = {
62
67
  };
63
68
 
64
69
  /**
65
- * Append-only progress JSONL — same posture as run-dossier row files:
66
- * sole Sitian decoder keeps reachable rows; malformed lines leave diagnostics
67
- * on stderr and do not abort seal/relocate.
70
+ * Append-only progress JSONL through the sole Sitian decoder: reachable rows
71
+ * plus one diagnostic per malformed line.
68
72
  */
69
- function parseProgressText(text: string, pathForDiagnostic: string): TicketProgressLine[] {
70
- if (text.trim() === "") return [];
73
+ function decodeProgressFile(path: string): {
74
+ readonly lines: TicketProgressLine[];
75
+ readonly malformed: readonly string[];
76
+ } {
77
+ if (!existsSync(path)) return { lines: [], malformed: [] };
78
+ const text = readFileSync(path, "utf8");
79
+ if (text.trim() === "") return { lines: [], malformed: [] };
71
80
  const { records, diagnostics } = parseSitianRecordText(text);
72
- for (const diagnostic of diagnostics) {
73
- process.stderr.write(
74
- `[ticket-progress] ${pathForDiagnostic} has malformed row(s); keeps reachable rows and retains the diagnostic: line ${diagnostic.line}: ${diagnostic.error}\n`,
75
- );
76
- }
77
81
  const lines: TicketProgressLine[] = [];
78
82
  for (const record of records) {
79
83
  const parsed: unknown = record;
@@ -81,12 +85,24 @@ function parseProgressText(text: string, pathForDiagnostic: string): TicketProgr
81
85
  if (typeof parsed.seat !== "string" || typeof parsed.round !== "string") continue;
82
86
  lines.push(parsed as unknown as TicketProgressLine);
83
87
  }
84
- return lines;
88
+ return {
89
+ lines,
90
+ malformed: diagnostics.map((diagnostic) => `line ${diagnostic.line}: ${diagnostic.error}`),
91
+ };
85
92
  }
86
93
 
94
+ /**
95
+ * Same posture as run-dossier row files: malformed lines leave diagnostics on
96
+ * stderr and do not abort seal/relocate.
97
+ */
87
98
  function readProgressFile(path: string): TicketProgressLine[] {
88
- if (!existsSync(path)) return [];
89
- return parseProgressText(readFileSync(path, "utf8"), path);
99
+ const { lines, malformed } = decodeProgressFile(path);
100
+ for (const diagnostic of malformed) {
101
+ process.stderr.write(
102
+ `[ticket-progress] ${path} has malformed row(s); keeps reachable rows and retains the diagnostic: ${diagnostic}\n`,
103
+ );
104
+ }
105
+ return lines;
90
106
  }
91
107
 
92
108
  /**
@@ -167,6 +183,44 @@ function statusAndSummaryFromPayload(payload: unknown): {
167
183
  };
168
184
  }
169
185
 
186
+ /**
187
+ * #1200「审 谁#轮」as the ticket has it now: the audited leg's latest row. Read
188
+ * at the officer's admission, before the officer runs. An audited leg with no
189
+ * round yet (unbound, or no accepted receipt) has none. A progress file that
190
+ * cannot be read or has malformed rows has none either and returns the cause
191
+ * for the caller to retain — the officer still runs.
192
+ */
193
+ export function readAuditedRound(auditedRunDirectory: string): {
194
+ readonly for?: string;
195
+ readonly fault?: string;
196
+ } {
197
+ try {
198
+ const subjectDirectory = subjectDirectoryOfRun(auditedRunDirectory);
199
+ if (subjectDirectory === undefined) return {};
200
+ const path = join(subjectDirectory, CURRENT_FILENAME);
201
+ const { lines, malformed } = decodeProgressFile(path);
202
+ if (malformed.length > 0) {
203
+ throw new Error(`${path} has malformed row(s): ${malformed.join("; ")}`);
204
+ }
205
+ const receipts = `runs/${basename(auditedRunDirectory)}/`;
206
+ const latest = lines.filter((line) => line.receipt.startsWith(receipts)).at(-1);
207
+ return latest === undefined ? {} : { for: `${latest.seat}#${latest.round}` };
208
+ } catch (error) {
209
+ return { fault: `audited round unavailable for ${auditedRunDirectory}: ${errorText(error)}` };
210
+ }
211
+ }
212
+
213
+ /**
214
+ * The row's `for`: gate officers copy the admission pin; the fixer's receipt
215
+ * reports its own (copied when it is a string, never checked). No other seat has one.
216
+ */
217
+ function forOfSealedRow(runDirectory: string, seat: string, payload: unknown): string | undefined {
218
+ const reported = packagedGateStageLabel(seat) !== undefined
219
+ ? readPageSync(runDirectory, "admitted")?.for
220
+ : seat === "fixer" && isRecord(payload) ? payload.for : undefined;
221
+ return typeof reported === "string" ? reported : undefined;
222
+ }
223
+
170
224
  export type WorktreeHeadCapture = {
171
225
  readonly head: string;
172
226
  readonly fault?: string;
@@ -308,11 +362,13 @@ export function landTicketProgress(input: {
308
362
  ? ""
309
363
  : relative(subjectDirectory, input.sessionFile).split(sep).join("/");
310
364
  const { status, summary } = statusAndSummaryFromPayload(input.accepted);
365
+ const reviewed = forOfSealedRow(runDirectory, input.role, input.accepted);
311
366
  const instruction = input.instruction;
312
367
  const line: TicketProgressLine = {
313
368
  at: new Date().toISOString(),
314
369
  seat: input.role,
315
370
  round,
371
+ ...(reviewed === undefined ? {} : { for: reviewed }),
316
372
  instruction,
317
373
  head: input.sealHead.head,
318
374
  ...(status === undefined ? {} : { status }),