@flyingrobots/parley-emit 0.1.0 → 0.2.4
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/CHANGELOG.md +295 -0
- package/README.md +517 -15
- package/package.json +1 -1
- package/src/check.ts +198 -0
- package/src/history.ts +229 -0
- package/src/index.ts +7 -1
- package/src/location.ts +47 -4
- package/src/refusal.ts +54 -0
- package/src/run.ts +279 -15
- package/src/schemas.ts +635 -1
- package/src/tool.ts +160 -25
- package/src/transforms/declared.ts +49 -0
- package/src/transforms/secret-patterns.ts +84 -0
- package/src/transforms/secrets.ts +153 -0
package/src/run.ts
CHANGED
|
@@ -4,8 +4,11 @@
|
|
|
4
4
|
// and is never scrubbed: the run id and the receipt path were chosen secret-free at startup.
|
|
5
5
|
import { closeSync, mkdirSync, openSync, writeSync } from "node:fs";
|
|
6
6
|
import { join } from "node:path";
|
|
7
|
+
import { checkRecord } from "./check.ts";
|
|
8
|
+
import type { HistoryProjection, HistorySink } from "./history.ts";
|
|
7
9
|
import { computeInv, digestInput, sha256, type InputDecl, type InputDescriptor } from "./inv.ts";
|
|
8
|
-
import { scrub } from "./redact.ts";
|
|
10
|
+
import { carriesSecret, scrub } from "./redact.ts";
|
|
11
|
+
import { isHistoryRefusal, type HistoryRefusal } from "./refusal.ts";
|
|
9
12
|
|
|
10
13
|
export interface Sink { write(line: string): void }
|
|
11
14
|
export const SIDECAR_THRESHOLD = 65_536;
|
|
@@ -15,10 +18,15 @@ const ENVELOPE_KEYS = ["t", "seq", "run", "_schema"] as const;
|
|
|
15
18
|
* Writer-owned string fields never scrubbed in a writer-built record: digests (a secret cannot
|
|
16
19
|
* occur in one except by hex coincidence, and replacing it would corrupt the digest) and public
|
|
17
20
|
* identity the tool declared and every discovery line prints regardless: the tool's name and
|
|
18
|
-
* version, a command's name, a schema's id and version
|
|
19
|
-
* writer
|
|
21
|
+
* version, a command's name, a schema's id and version, and the `code` of a diagnostic the
|
|
22
|
+
* writer itself raises, which is the writer's own stable identifier and not a value a caller
|
|
23
|
+
* supplied — replacing part of it would put a line on the stream that no longer validates
|
|
24
|
+
* against its schema, which is the one thing redaction must not do. A tool's own diagnostic is
|
|
25
|
+
* not writer-built and its `code` is scrubbed like anything else. Tool-supplied values that
|
|
26
|
+
* reach a writer-built record (`resolved`, input descriptors) are scrubbed in full before it is
|
|
27
|
+
* built.
|
|
20
28
|
*/
|
|
21
|
-
const DIGEST_KEYS = new Set(["sha256", "inv", "tool", "v", "name", "id", "version"]);
|
|
29
|
+
export const DIGEST_KEYS = new Set(["sha256", "inv", "tool", "v", "name", "id", "version", "code"]);
|
|
22
30
|
|
|
23
31
|
export const SCHEMA_IDS = {
|
|
24
32
|
bootstrap: { id: "parley.run.bootstrap", version: "1.1.1" },
|
|
@@ -34,6 +42,7 @@ export const SCHEMA_IDS = {
|
|
|
34
42
|
command: { id: "parley.command", version: "1.0.0" },
|
|
35
43
|
schema: { id: "parley.schema", version: "1.0.0" },
|
|
36
44
|
page: { id: "parley.page", version: "1.0.0" },
|
|
45
|
+
historyTransform: { id: "parley.history.transform", version: "1.0.0" },
|
|
37
46
|
} as const;
|
|
38
47
|
|
|
39
48
|
export type SchemaRef = { readonly id: string; readonly version: string };
|
|
@@ -54,10 +63,14 @@ export interface RunInit {
|
|
|
54
63
|
secretValues: Set<string>; now: () => number; cwd: string;
|
|
55
64
|
command: string; resolved: unknown; inputs: InputDecl[];
|
|
56
65
|
headRedacted: boolean; human?: (line: Record_) => string | null;
|
|
66
|
+
/** The shared history stream of §8, when one is configured: it receives the receipt's bytes. */
|
|
67
|
+
history?: HistorySink;
|
|
68
|
+
/** The projection applied to the stream copy, when a transform is configured (§8, informative). */
|
|
69
|
+
projection?: HistoryProjection;
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
type SinkName = "stdout" | "stderr" | "receipt";
|
|
60
|
-
interface Emit { to: { stdout?: boolean; stderr?: boolean; receipt?: boolean }; writer?: boolean; noSidecar?: boolean; deferFailures?: boolean }
|
|
73
|
+
interface Emit { to: { stdout?: boolean; stderr?: boolean; receipt?: boolean }; writer?: boolean; noSidecar?: boolean; deferFailures?: boolean; verbatim?: string }
|
|
61
74
|
|
|
62
75
|
export class Run {
|
|
63
76
|
readonly id: string; readonly tool: string; readonly v: string; readonly session?: string;
|
|
@@ -69,9 +82,17 @@ export class Run {
|
|
|
69
82
|
private redactedAny: boolean;
|
|
70
83
|
private failedSinks = new Set<SinkName>();
|
|
71
84
|
private sidecarFailed = false;
|
|
85
|
+
private historyFailed = false;
|
|
86
|
+
private projectionSeq: number | null = null;
|
|
87
|
+
private projectionWarning: Diagnostic | null = null;
|
|
88
|
+
private withheldWarnings: Diagnostic[] = [];
|
|
89
|
+
private projecting = 0;
|
|
90
|
+
private flushingWithheld = false;
|
|
91
|
+
private withheld = 0;
|
|
72
92
|
private deferred: { sink: SinkName; error: unknown }[] = [];
|
|
73
93
|
private inputsSettled = false;
|
|
74
94
|
private supplied = new Map<string, Uint8Array>();
|
|
95
|
+
private unread = new Map<string, string>();
|
|
75
96
|
private finalStatus: number | null = null;
|
|
76
97
|
private readonly init: RunInit;
|
|
77
98
|
|
|
@@ -106,7 +127,11 @@ export class Run {
|
|
|
106
127
|
private write(sink: SinkName, text: string, defer = false): boolean {
|
|
107
128
|
if (this.failedSinks.has(sink)) return false;
|
|
108
129
|
try {
|
|
109
|
-
if (sink === "receipt") {
|
|
130
|
+
if (sink === "receipt") {
|
|
131
|
+
if (!this.receiptOpen) return false;
|
|
132
|
+
writeSync(this.init.receiptFd, text);
|
|
133
|
+
this.mirrorToHistory(text);
|
|
134
|
+
}
|
|
110
135
|
else this.init[sink].write(text);
|
|
111
136
|
return true;
|
|
112
137
|
} catch (e) {
|
|
@@ -125,6 +150,174 @@ export class Run {
|
|
|
125
150
|
this.emit({ to: { stderr: sink !== "stderr", receipt: sink !== "receipt" }, writer: true }, { event: "error", code: sink === "sidecar" ? "sink.sidecar_failed" : "sink.write_failed", problem: `writing to ${sink} failed: ${msg}`, remedy: "the run will close with status 4; whatever other sinks received is preserved" }, SCHEMA_IDS.error);
|
|
126
151
|
}
|
|
127
152
|
|
|
153
|
+
/**
|
|
154
|
+
* The shared history stream (§8) gets the receipt's bytes, after they are in the receipt: the
|
|
155
|
+
* same line, so a declared secret cannot reach it that did not reach the receipt. It is an
|
|
156
|
+
* opt-in extra and the dependency runs one way, so a failure is one `warning` and nothing
|
|
157
|
+
* else: not an error, not a sink failure, no change to the exit status. The stream is dropped
|
|
158
|
+
* at the first failure, so a broken stream is reported once and the run carries on.
|
|
159
|
+
*
|
|
160
|
+
* With a transform configured the stream copy is a projection of those bytes rather than the
|
|
161
|
+
* bytes themselves, and the writer enforces the contract around it: see `project`. Withholding
|
|
162
|
+
* leaves a `seq` gap on the stream and nowhere else.
|
|
163
|
+
*/
|
|
164
|
+
private mirrorToHistory(text: string): void {
|
|
165
|
+
const h = this.init.history;
|
|
166
|
+
if (h === undefined || this.historyFailed) return;
|
|
167
|
+
const p = this.init.projection;
|
|
168
|
+
let line: string | null = text;
|
|
169
|
+
if (p !== undefined) {
|
|
170
|
+
this.projecting += 1;
|
|
171
|
+
try { line = this.project(text, p); } finally { this.projecting -= 1; }
|
|
172
|
+
}
|
|
173
|
+
if (line !== null) this.appendToHistory(h, line);
|
|
174
|
+
if (this.projecting === 0) this.flushWithheld();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private appendToHistory(h: HistorySink, line: string): void {
|
|
178
|
+
try { h.append(line); } catch (e) {
|
|
179
|
+
this.historyFailed = true;
|
|
180
|
+
try { h.close(); } catch { /* already closed */ }
|
|
181
|
+
if (this.closed) return;
|
|
182
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
183
|
+
try {
|
|
184
|
+
this.writerWarning({ code: "history.write_failed", problem: `appending to the shared history stream at ${h.path} failed: ${msg}; the receipt is unaffected and the rest of this run is not in the stream`, remedy: `free space or fix permissions on ${h.dir}, or set PARLEY_HISTORY=off; the receipt is the record and the stream is a convenience` });
|
|
185
|
+
} catch { /* the run ended between the write and the warning */ }
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* One receipt line through the configured transform, with the four rules the writer enforces
|
|
191
|
+
* around it (flyingrobots#57). Returns the line to append, or `null` to withhold it.
|
|
192
|
+
*
|
|
193
|
+
* 1. The transform is handed the parsed record the receipt already holds, so it sees the
|
|
194
|
+
* redacted payload of §5.3 and never the raw one.
|
|
195
|
+
* 2. Nothing is the transform's envelope to change: `event`, `t`, `seq`, `run` and `_schema`
|
|
196
|
+
* are taken off whatever it returns and the receipt line's are put back, so the two
|
|
197
|
+
* copies of one event agree on all five (§1.3).
|
|
198
|
+
* 3. The returned body is re-validated against the schema for its **exact** advertised pair
|
|
199
|
+
* (§4.5) and withheld when it no longer validates. A pair no schema document is held for
|
|
200
|
+
* cannot be re-validated, so it is withheld too: an unchecked projection is never passed
|
|
201
|
+
* off as a checked one.
|
|
202
|
+
* 4. Anything that goes wrong withholds the line and warns, naming the `seq`; a warning is
|
|
203
|
+
* never an error and never changes the exit status (§6.1).
|
|
204
|
+
*/
|
|
205
|
+
private project(text: string, p: HistoryProjection): string | null {
|
|
206
|
+
let rec: Record_;
|
|
207
|
+
try { rec = JSON.parse(text) as Record_; } catch { return null; }
|
|
208
|
+
const seq = typeof rec.seq === "number" ? rec.seq : -1;
|
|
209
|
+
// Nothing from here may escape. This runs inside the receipt's own write, so a throw that got
|
|
210
|
+
// out would be reported as a receipt sink failure and close the run with status 4 — the stream
|
|
211
|
+
// failing the run it is an optional observer of, which §8 forbids. A record that cannot be
|
|
212
|
+
// serialised (a cycle, a BigInt) is withheld like any other failure.
|
|
213
|
+
try { return this.projectOne(rec, seq, p); }
|
|
214
|
+
catch (e) {
|
|
215
|
+
const msg = e instanceof Error ? e.message || e.name : String(e);
|
|
216
|
+
this.withhold({ code: "history.transform_failed", problem: `the projection of seq ${seq} by ${p.name} could not be completed: ${msg}; that line is not on the shared stream and the receipt is unaffected`, remedy: "return a record that serialises as JSON: no cycles, no bigint, nothing the receipt line did not already carry" });
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private projectOne(rec: Record_, seq: number, p: HistoryProjection): string | null {
|
|
222
|
+
const stamp = rec._schema as { id?: unknown; version?: unknown } | undefined;
|
|
223
|
+
const pair = `${typeof stamp?.id === "string" ? stamp.id : "?"}@${typeof stamp?.version === "string" ? stamp.version : "?"}`;
|
|
224
|
+
// The envelope is taken before the transform runs, not read back off the record afterwards: a
|
|
225
|
+
// transform that mutates the record it was handed and returns it would otherwise hand back its
|
|
226
|
+
// own envelope, and the receipt line's is the only one that may stand (§1.3).
|
|
227
|
+
const envelope = { event: rec.event, t: rec.t, seq: rec.seq, run: rec.run, _schema: { id: stamp?.id, version: stamp?.version } };
|
|
228
|
+
let returned: unknown;
|
|
229
|
+
try { returned = p.fn(rec); } catch (e) {
|
|
230
|
+
const msg = e instanceof Error ? e.message || e.name : String(e);
|
|
231
|
+
this.withhold({ code: "history.transform_failed", problem: `the history transform ${p.name} threw on seq ${seq}: ${msg}; that line is not on the shared stream and the receipt is unaffected`, remedy: "fix the transform or remove it; the stream copy has a gap at that seq and names the projection on its opening line" });
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
// Nothing returned is the configured way to withhold a line: a gap, not a defect.
|
|
235
|
+
if (returned === undefined || returned === null) return null;
|
|
236
|
+
// A refusal is the transform withholding the line *and saying why* (flyingrobots#60). The
|
|
237
|
+
// code is the transform's; the envelope, the exit status and the promise that no declared
|
|
238
|
+
// secret leaves this way are still the writer's.
|
|
239
|
+
if (isHistoryRefusal(returned)) { this.refused(returned, seq, p); return null; }
|
|
240
|
+
if (typeof returned !== "object" || Array.isArray(returned)) {
|
|
241
|
+
this.withhold({ code: "history.transform_invalid", problem: `the history transform ${p.name} returned ${Array.isArray(returned) ? "an array" : typeof returned} for seq ${seq}, which is neither a record nor nothing; that line is not on the shared stream`, remedy: "return the record, a projection of it, or nothing to withhold the line" });
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
const { event: _event, t: _t, seq: _seq, run: _run, _schema: _stamp, ...body } = returned as Record_;
|
|
245
|
+
if (this.init.secretValues.size > 0 && carriesSecret(JSON.stringify(body), this.init.secretValues)) {
|
|
246
|
+
this.withhold({ code: "history.transform_unredacted", problem: `the history transform ${p.name} returned a body for seq ${seq} carrying a declared secret value the writer had already replaced; that line is not on the shared stream`, remedy: "a transform narrows a record; it never restores a value §5.3 replaced" });
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
const doc = p.schemas.get(pair);
|
|
250
|
+
if (doc === undefined) {
|
|
251
|
+
this.withhold({ code: "history.transform_uncheckable", problem: `seq ${seq} advertises ${pair}, which this writer holds no schema document for, so the projection by ${p.name} could not be re-validated and that line is not on the shared stream`, remedy: "declare the schema on the command's emitted type (spec §4.2) so a projection of it can be checked, or remove the transform" });
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
const out: Record_ = { event: envelope.event, t: envelope.t, seq: envelope.seq, run: envelope.run, ...body, _schema: envelope._schema };
|
|
255
|
+
const verdict = checkRecord(out, doc);
|
|
256
|
+
if (!verdict.ok) {
|
|
257
|
+
if ("unsupported" in verdict) {
|
|
258
|
+
this.withhold({ code: "history.transform_uncheckable", problem: `the schema for ${pair} uses ${verdict.unsupported}, which this writer's re-validation does not implement, so the projection of seq ${seq} by ${p.name} could not be checked and that line is not on the shared stream`, remedy: "state the type's schema within the subset the writer can check, or remove the transform" });
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
this.withhold({ code: "history.transform_invalid", problem: `the body the history transform ${p.name} returned for seq ${seq} no longer validates against ${pair}: ${verdict.errors.slice(0, 3).join("; ")}; that line is not on the shared stream`, remedy: "project within the message's schema, or withhold the line by returning nothing" });
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
return JSON.stringify(out) + "\n";
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* A line the transform refused, reported under the transform's own `code` (flyingrobots#60).
|
|
269
|
+
* Two things are checked first, because a warning's `code` is the one string the writer does
|
|
270
|
+
* not scrub (§5.3) and an unusable one would be a withheld line with no warning at all:
|
|
271
|
+
*
|
|
272
|
+
* - A `code` that is not spec §6's shape, a `problem` that is empty, or either of them not a
|
|
273
|
+
* string at all, is the transform being broken, which is `history.transform_failed` like
|
|
274
|
+
* any other way of being broken. A `remedy` that is not a string is dropped rather than
|
|
275
|
+
* emitted, because a `warning` whose `remedy` is a number does not validate (§4.5) and the
|
|
276
|
+
* stream is not the only copy: stderr and the receipt have no re-validation behind them.
|
|
277
|
+
* - A `code` carrying a declared secret value would disclose it on stderr and in the
|
|
278
|
+
* receipt, so the refusal is itself refused: `history.transform_unredacted`, the same code
|
|
279
|
+
* a body that restored a replaced value gets. The `problem` and `remedy` are scrubbed like
|
|
280
|
+
* any other tool-supplied string, so they need no check of their own.
|
|
281
|
+
*/
|
|
282
|
+
private refused(r: HistoryRefusal, seq: number, p: HistoryProjection): void {
|
|
283
|
+
if (typeof r.code !== "string" || typeof r.problem !== "string" || !CODE.test(r.code) || !r.problem) {
|
|
284
|
+
this.withhold({ code: "history.transform_failed", problem: `the history transform ${p.name} refused seq ${seq} with ${JSON.stringify(r.code)} and ${JSON.stringify(r.problem)}, which are not a code of two or more dot-separated lowercase segments and a non-empty problem; that line is not on the shared stream and the refusal could not be reported under its own code`, remedy: "build the refusal with refuse(code, problem), which checks both at the point they are written (spec §6)" });
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (this.init.secretValues.size > 0 && carriesSecret(r.code, this.init.secretValues)) {
|
|
288
|
+
this.withhold({ code: "history.transform_unredacted", problem: `the history transform ${p.name} refused seq ${seq} under a code carrying a declared secret value; a diagnostic's code is never scrubbed, so neither that line nor that refusal's code reaches any stream`, remedy: "refuse under a code the transform chose, not one built from a value it was handed (spec §5.3)" });
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
this.withhold({ code: r.code, problem: r.problem, ...(typeof r.remedy === "string" ? { remedy: r.remedy } : {}) });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* A withheld line, held until the current mirror is finished: the warning is itself a record,
|
|
296
|
+
* and a record goes through the transform too. A projection that fails on a warning line
|
|
297
|
+
* withholds it and does not warn again, so one failure cannot generate another for ever; the
|
|
298
|
+
* stream's gaps, not the warning count, are the ground truth for what it is missing.
|
|
299
|
+
*/
|
|
300
|
+
private withhold(d: Diagnostic): void {
|
|
301
|
+
this.withheld += 1;
|
|
302
|
+
if (this.flushingWithheld || this.closed) return;
|
|
303
|
+
this.withheldWarnings.push(d);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private flushWithheld(): void {
|
|
307
|
+
if (this.flushingWithheld || this.withheldWarnings.length === 0) return;
|
|
308
|
+
this.flushingWithheld = true;
|
|
309
|
+
try {
|
|
310
|
+
while (this.withheldWarnings.length > 0 && !this.closed) {
|
|
311
|
+
const d = this.withheldWarnings.shift()!;
|
|
312
|
+
try { this.writerWarning(d); } catch { /* the run ended between the withhold and the warning */ }
|
|
313
|
+
}
|
|
314
|
+
this.withheldWarnings = [];
|
|
315
|
+
} finally { this.flushingWithheld = false; }
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** How many lines the configured transform kept off the stream, warnings about them included. */
|
|
319
|
+
get withheldFromHistory(): number { return this.withheld; }
|
|
320
|
+
|
|
128
321
|
/** Internal: report sink failures held back during the bootstrap write, now that the head is in place (§5: the head is the receipt's first line). */
|
|
129
322
|
flushDeferredFailures(): void {
|
|
130
323
|
const held = this.deferred; this.deferred = [];
|
|
@@ -142,7 +335,7 @@ export class Run {
|
|
|
142
335
|
if (!this.rendererFailed && !this.closed) {
|
|
143
336
|
this.rendererFailed = true;
|
|
144
337
|
const msg = e instanceof Error ? e.message : String(e);
|
|
145
|
-
queueMicrotask(() => { try { if (!this.closed) this.
|
|
338
|
+
queueMicrotask(() => { try { if (!this.closed) this.writerWarning({ code: "human.renderer_failed", problem: `the tool's --human renderer threw: ${msg}; the default rendering is used for the rest of the run`, remedy: "fix the renderer; the receipt is complete regardless" }); } catch { /* the run ended first */ } });
|
|
146
339
|
}
|
|
147
340
|
}
|
|
148
341
|
if (typeof custom === "string") return custom.endsWith("\n") ? custom : custom + "\n";
|
|
@@ -157,10 +350,18 @@ export class Run {
|
|
|
157
350
|
}
|
|
158
351
|
}
|
|
159
352
|
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
353
|
+
/**
|
|
354
|
+
* Scrub a payload's values. `event` is envelope, named by the writer or the tool's schema, and is
|
|
355
|
+
* never a secret. For a writer-built record, the one field named by `verbatim` is left as it is:
|
|
356
|
+
* the schema document of a `schema` line, a static declaration a secret cannot reach (§5.3,
|
|
357
|
+
* flyingrobots/parley#111).
|
|
358
|
+
*/
|
|
359
|
+
private scrubPayload(payload: Record_, found: string[], writer: boolean, verbatim?: string): Record_ {
|
|
360
|
+
const { event, ...all } = payload;
|
|
361
|
+
const rest: Record_ = { ...all };
|
|
362
|
+
if (verbatim !== undefined) delete rest[verbatim];
|
|
363
|
+
let scrubbed = scrub(rest, this.init.secretValues, "", found, writer ? { skipKeys: DIGEST_KEYS, scalars: false } : {}) as Record_;
|
|
364
|
+
if (verbatim !== undefined && verbatim in all) scrubbed = { ...scrubbed, [verbatim]: all[verbatim] };
|
|
164
365
|
return event === undefined ? scrubbed : { event, ...scrubbed };
|
|
165
366
|
}
|
|
166
367
|
|
|
@@ -172,7 +373,7 @@ export class Run {
|
|
|
172
373
|
if (this.closed) throw new ParleyClosed();
|
|
173
374
|
this.refuseEnvelopeKeys(payload, "a record"); this.refuseEnvelopeKeys(receiptExtra, "receiptExtra");
|
|
174
375
|
const found: string[] = [];
|
|
175
|
-
const scrubbed = this.scrubPayload(payload, found, how.writer === true);
|
|
376
|
+
const scrubbed = this.scrubPayload(payload, found, how.writer === true, how.writer === true ? how.verbatim : undefined);
|
|
176
377
|
const extraFound: string[] = [];
|
|
177
378
|
const extra = receiptExtra ? this.scrubPayload(receiptExtra, extraFound, how.writer === true) : undefined;
|
|
178
379
|
const at = this.next();
|
|
@@ -219,15 +420,50 @@ export class Run {
|
|
|
219
420
|
|
|
220
421
|
/** Internal: the bootstrap line, stdout only, seq 0 (§3.1). A stdout failure here is reported after the head. */
|
|
221
422
|
bootstrap(inv: string | undefined): void {
|
|
222
|
-
|
|
423
|
+
// `paths` is open and the protocol defines only `receipt`; `history` is documented by this
|
|
424
|
+
// writer and is present only when a stream is configured (spec §3.1, §8).
|
|
425
|
+
const paths: Record_ = { receipt: this.receiptPath, ...(this.init.history ? { history: this.init.history.path } : {}) };
|
|
426
|
+
const rec: Record_ = { event: "run", tool: this.tool, v: this.v, ...(inv ? { inv } : {}), ...(this.session ? { session: this.session } : {}), paths, ...(this.dryRun ? { dry_run: true } : {}) };
|
|
223
427
|
this.emit({ to: { stdout: true }, writer: true, deferFailures: true }, rec, SCHEMA_IDS.bootstrap);
|
|
224
428
|
}
|
|
225
429
|
|
|
430
|
+
/**
|
|
431
|
+
* Internal: the opening line of a projected stream copy (§8), stream only and before the head,
|
|
432
|
+
* so a reader of the shared timeline meets the projection before any projected line of the run.
|
|
433
|
+
* It takes a `seq` of its own and reaches no other sink, which is why the receipt has no line
|
|
434
|
+
* at that `seq`; `noteProjection` records that in the receipt once the head is in place. A
|
|
435
|
+
* failure to append it is held back for the same reason the bootstrap line's is.
|
|
436
|
+
*/
|
|
437
|
+
declareProjection(): void {
|
|
438
|
+
const h = this.init.history, p = this.init.projection;
|
|
439
|
+
if (h === undefined || p === undefined || this.historyFailed) return;
|
|
440
|
+
const at = this.next();
|
|
441
|
+
this.projectionSeq = at.seq;
|
|
442
|
+
const rec = this.envelope(this.scrubPayload({ event: "history.transform", name: p.name, sha256: p.sha256 }, [], true), SCHEMA_IDS.historyTransform, at);
|
|
443
|
+
try { h.append(JSON.stringify(rec) + "\n"); }
|
|
444
|
+
catch (e) {
|
|
445
|
+
this.historyFailed = true;
|
|
446
|
+
this.projectionSeq = null;
|
|
447
|
+
try { h.close(); } catch { /* already closed */ }
|
|
448
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
449
|
+
this.projectionWarning = { code: "history.write_failed", problem: `appending the projection's opening line to the shared history stream at ${h.path} failed: ${msg}; this run is not in the stream and the receipt is unaffected`, remedy: `free space or fix permissions on ${h.dir}, or set PARLEY_HISTORY=off; the receipt is the record and the stream is a convenience` };
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** Internal: after the head, the warning the opening line could not raise, and the decision that accounts for the `seq` it took (§5). */
|
|
454
|
+
noteProjection(): void {
|
|
455
|
+
const p = this.init.projection;
|
|
456
|
+
if (p === undefined) return;
|
|
457
|
+
if (this.projectionWarning !== null) { const d = this.projectionWarning; this.projectionWarning = null; this.writerWarning(d); }
|
|
458
|
+
if (this.projectionSeq === null) return;
|
|
459
|
+
this.step(`the shared stream's copy of this run is a projection by ${p.name}`, `a history transform is configured, so the stream copy is narrowed and says so: seq ${this.projectionSeq} is its parley.history.transform line, which reaches the stream alone and is why this receipt has no line at that seq. The transform's source digests to ${p.sha256}. The receipt is never projected (§8, informative)`);
|
|
460
|
+
}
|
|
461
|
+
|
|
226
462
|
/** Internal: the receipt head, receipt only, never a sidecar (§5). */
|
|
227
463
|
head(fields: Record_): void { this.emit({ to: { receipt: true }, writer: true, noSidecar: true }, { event: "receipt.head", ...fields }, SCHEMA_IDS.head); }
|
|
228
464
|
|
|
229
465
|
/** Internal: a discovery line (§4.1), a writer-built record describing public identity. */
|
|
230
|
-
describe(record: Record_, schema: SchemaRef): number { return this.emit({ to: { stdout: true, receipt: true }, writer: true }, record, schema); }
|
|
466
|
+
describe(record: Record_, schema: SchemaRef, verbatim?: string): number { return this.emit({ to: { stdout: true, receipt: true }, writer: true, ...(verbatim !== undefined ? { verbatim } : {}) }, record, schema); }
|
|
231
467
|
|
|
232
468
|
/** A result: to stdout and the receipt. `receiptExtra` goes to the receipt copy only. */
|
|
233
469
|
result(record: Record_, schema: SchemaRef, receiptExtra?: Record_): number {
|
|
@@ -248,6 +484,13 @@ export class Run {
|
|
|
248
484
|
|
|
249
485
|
warning(d: Diagnostic): number { return this.emit({ to: { stderr: true, receipt: true } }, { event: "warning", ...this.diagnostic(d) }, SCHEMA_IDS.warning); }
|
|
250
486
|
|
|
487
|
+
/**
|
|
488
|
+
* Internal: a warning the writer itself raises, such as a relocated receipt or an unusable
|
|
489
|
+
* history stream. Its `code` is the writer's own identifier, so it is not scrubbed; the
|
|
490
|
+
* `problem` and `remedy`, which carry paths, are.
|
|
491
|
+
*/
|
|
492
|
+
writerWarning(d: Diagnostic): number { return this.emit({ to: { stderr: true, receipt: true }, writer: true }, { event: "warning", ...this.diagnostic(d) }, SCHEMA_IDS.warning); }
|
|
493
|
+
|
|
251
494
|
/** A structured error (§6). The run then closes with status 2 unless `end` is given another non-zero status. `receiptExtra` goes to the receipt copy only. */
|
|
252
495
|
error(d: Diagnostic, receiptExtra?: Record_): number {
|
|
253
496
|
this.errorReported = true;
|
|
@@ -265,13 +508,32 @@ export class Run {
|
|
|
265
508
|
/** Supply the bytes of a declared input the writer cannot read itself, such as stdin. */
|
|
266
509
|
input(path: string, bytes: Uint8Array | string): void {
|
|
267
510
|
this.supplied.set(path, typeof bytes === "string" ? Buffer.from(bytes, "utf8") : bytes);
|
|
511
|
+
this.unread.delete(path);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Say that the program could not read a declared input, so the writer records it with `error`
|
|
516
|
+
* and no digest, and omits `inv`, rather than reading the path itself at finalisation and
|
|
517
|
+
* digesting bytes the program never saw (§2.2, flyingrobots/parley#110). Refused for a path
|
|
518
|
+
* that is not a declared input, and once the inputs are settled.
|
|
519
|
+
*/
|
|
520
|
+
inputUnreadable(path: string, code = "input.unreadable"): void {
|
|
521
|
+
if (this.closed) throw new ParleyClosed();
|
|
522
|
+
if (this.inputsSettled) throw new TypeError("the inputs are already settled; call inputUnreadable before finaliseInputs or end (spec §2.2)");
|
|
523
|
+
if (!this.init.inputs.some((i) => i.path === path)) throw new TypeError(`${JSON.stringify(path)} is not a declared input of this run`);
|
|
524
|
+
if (!CODE.test(code)) throw new TypeError(`code ${JSON.stringify(code)} is not two or more dot-separated lowercase segments (spec §6)`);
|
|
525
|
+
this.supplied.delete(path);
|
|
526
|
+
this.unread.set(path, code);
|
|
268
527
|
}
|
|
269
528
|
|
|
270
529
|
/** Digest the declared inputs and emit `receipt.inputs` with `inv` once settled (§2.2). Idempotent; `end` calls it. */
|
|
271
530
|
finaliseInputs(): InputDescriptor[] {
|
|
272
531
|
if (this.inputsSettled || this.init.inputs.length === 0) { this.inputsSettled = true; return []; }
|
|
273
532
|
this.inputsSettled = true;
|
|
274
|
-
const raw = this.init.inputs.map((i) =>
|
|
533
|
+
const raw = this.init.inputs.map((i): InputDescriptor => {
|
|
534
|
+
const code = this.unread.get(i.path);
|
|
535
|
+
return code === undefined ? digestInput(this.cwd, i, this.supplied.get(i.path)) : { path: i.path, ...(i.role ? { role: i.role } : {}), error: code };
|
|
536
|
+
});
|
|
275
537
|
// The preimage and the record both take the redacted descriptors (§2.2: no secret value in inv).
|
|
276
538
|
const inputs = scrub(raw, this.init.secretValues, "", [], { skipKeys: DIGEST_KEYS, scalars: false }) as InputDescriptor[];
|
|
277
539
|
const inv = computeInv({ tool: this.tool, v: this.v, command: this.init.command, resolved: this.init.resolved, inputs });
|
|
@@ -298,6 +560,8 @@ export class Run {
|
|
|
298
560
|
this.receiptOpen = false;
|
|
299
561
|
let closeOk = true;
|
|
300
562
|
try { closeSync(this.init.receiptFd); } catch { closeOk = false; }
|
|
563
|
+
// The stream is not the record: closing it cannot change the status (§8 is informative).
|
|
564
|
+
try { this.init.history?.close(); } catch { /* the stream was already gone */ }
|
|
301
565
|
this.finalStatus = okOut && okRec && closeOk ? s : 4;
|
|
302
566
|
return this.finalStatus;
|
|
303
567
|
}
|