@sanity/ailf 2.8.0 → 3.0.0

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 (91) hide show
  1. package/dist/_vendor/ailf-core/artifact-capture/association.d.ts +35 -0
  2. package/dist/_vendor/ailf-core/artifact-capture/association.js +28 -0
  3. package/dist/_vendor/ailf-core/artifact-registry.d.ts +124 -23
  4. package/dist/_vendor/ailf-core/artifact-registry.js +708 -64
  5. package/dist/_vendor/ailf-core/batch-signing.d.ts +64 -0
  6. package/dist/_vendor/ailf-core/batch-signing.js +23 -0
  7. package/dist/_vendor/ailf-core/index.d.ts +3 -2
  8. package/dist/_vendor/ailf-core/index.js +3 -2
  9. package/dist/_vendor/ailf-core/ports/artifact-writer.d.ts +59 -20
  10. package/dist/_vendor/ailf-core/ports/artifact-writer.js +33 -10
  11. package/dist/_vendor/ailf-core/ports/context.d.ts +20 -17
  12. package/dist/_vendor/ailf-core/ports/index.d.ts +0 -2
  13. package/dist/_vendor/ailf-core/schemas/pipeline.d.ts +6 -6
  14. package/dist/_vendor/ailf-core/services/index.d.ts +1 -0
  15. package/dist/_vendor/ailf-core/services/index.js +1 -0
  16. package/dist/_vendor/ailf-core/services/slim-report-summary.d.ts +31 -0
  17. package/dist/_vendor/ailf-core/services/slim-report-summary.js +217 -0
  18. package/dist/_vendor/ailf-core/types/branded-ids.d.ts +33 -0
  19. package/dist/_vendor/ailf-core/types/index.d.ts +202 -23
  20. package/dist/adapters/config-sources/file-config-adapter.js +0 -4
  21. package/dist/artifact-capture/accumulating-artifact-writer.d.ts +50 -0
  22. package/dist/artifact-capture/accumulating-artifact-writer.js +111 -0
  23. package/dist/artifact-capture/api-gateway-artifact-writer.d.ts +17 -4
  24. package/dist/artifact-capture/api-gateway-artifact-writer.js +58 -7
  25. package/dist/artifact-capture/emit-file.d.ts +28 -0
  26. package/dist/artifact-capture/emit-file.js +56 -0
  27. package/dist/artifact-capture/fanout-artifact-writer.d.ts +39 -0
  28. package/dist/artifact-capture/fanout-artifact-writer.js +76 -0
  29. package/dist/artifact-capture/gcs-artifact-writer.d.ts +40 -3
  30. package/dist/artifact-capture/gcs-artifact-writer.js +238 -14
  31. package/dist/artifact-capture/local-fs-artifact-writer.d.ts +71 -0
  32. package/dist/artifact-capture/local-fs-artifact-writer.js +273 -0
  33. package/dist/artifact-capture/redact-artifact.d.ts +3 -5
  34. package/dist/artifact-capture/redact-artifact.js +3 -5
  35. package/dist/cli.js +56 -2
  36. package/dist/commands/explain-handler.js +4 -4
  37. package/dist/commands/pipeline-action.d.ts +5 -4
  38. package/dist/commands/pipeline-action.js +33 -16
  39. package/dist/commands/pipeline.d.ts +4 -4
  40. package/dist/commands/pipeline.js +4 -4
  41. package/dist/commands/publish.js +4 -1
  42. package/dist/commands/runs.d.ts +18 -0
  43. package/dist/commands/runs.js +71 -0
  44. package/dist/composition-root.d.ts +13 -10
  45. package/dist/composition-root.js +74 -46
  46. package/dist/orchestration/build-app-context.js +4 -7
  47. package/dist/orchestration/pipeline-orchestrator.d.ts +1 -1
  48. package/dist/orchestration/pipeline-orchestrator.js +37 -46
  49. package/dist/orchestration/steps/calculate-scores-step.d.ts +1 -1
  50. package/dist/orchestration/steps/calculate-scores-step.js +19 -19
  51. package/dist/orchestration/steps/callback-step.d.ts +1 -1
  52. package/dist/orchestration/steps/callback-step.js +6 -4
  53. package/dist/orchestration/steps/compare-step.d.ts +1 -1
  54. package/dist/orchestration/steps/compare-step.js +4 -2
  55. package/dist/orchestration/steps/discovery-report-step.d.ts +1 -1
  56. package/dist/orchestration/steps/discovery-report-step.js +4 -1
  57. package/dist/orchestration/steps/fetch-docs-step.js +9 -15
  58. package/dist/orchestration/steps/finalize-run-step.js +21 -7
  59. package/dist/orchestration/steps/gap-analysis-step.js +34 -6
  60. package/dist/orchestration/steps/generate-configs-step.d.ts +1 -1
  61. package/dist/orchestration/steps/generate-configs-step.js +11 -11
  62. package/dist/orchestration/steps/publish-report-step.d.ts +1 -1
  63. package/dist/orchestration/steps/publish-report-step.js +24 -19
  64. package/dist/orchestration/steps/readiness-step.d.ts +1 -1
  65. package/dist/orchestration/steps/readiness-step.js +4 -1
  66. package/dist/orchestration/steps/report-step.d.ts +1 -1
  67. package/dist/orchestration/steps/report-step.js +6 -3
  68. package/dist/orchestration/steps/run-eval-step.js +14 -9
  69. package/dist/pipeline/compare.d.ts +2 -2
  70. package/dist/pipeline/emit-eval-results.d.ts +38 -0
  71. package/dist/pipeline/emit-eval-results.js +100 -0
  72. package/dist/pipeline/map-request-to-config.js +0 -4
  73. package/package.json +1 -1
  74. package/dist/_vendor/ailf-core/artifact-capture/noop-collector.d.ts +0 -14
  75. package/dist/_vendor/ailf-core/artifact-capture/noop-collector.js +0 -25
  76. package/dist/_vendor/ailf-core/ports/artifact-collector.d.ts +0 -94
  77. package/dist/_vendor/ailf-core/ports/artifact-collector.js +0 -13
  78. package/dist/_vendor/ailf-core/ports/capture-comparator.d.ts +0 -138
  79. package/dist/_vendor/ailf-core/ports/capture-comparator.js +0 -10
  80. package/dist/artifact-capture/comparator.d.ts +0 -22
  81. package/dist/artifact-capture/comparator.js +0 -493
  82. package/dist/artifact-capture/filesystem-collector.d.ts +0 -42
  83. package/dist/artifact-capture/filesystem-collector.js +0 -237
  84. package/dist/artifact-capture/gcs-collector.d.ts +0 -55
  85. package/dist/artifact-capture/gcs-collector.js +0 -117
  86. package/dist/commands/capture-compare.d.ts +0 -15
  87. package/dist/commands/capture-compare.js +0 -253
  88. package/dist/commands/capture-list.d.ts +0 -12
  89. package/dist/commands/capture-list.js +0 -150
  90. package/dist/commands/capture.d.ts +0 -9
  91. package/dist/commands/capture.js +0 -16
@@ -0,0 +1,111 @@
1
+ /**
2
+ * accumulating-artifact-writer.ts
3
+ *
4
+ * Decorator that wraps any `ArtifactWriter` and accumulates every
5
+ * successful `emit()` / `appendNdjson()` return into a run-scoped
6
+ * manifest slice. FinalizeRunStep reads the accumulator at the end of
7
+ * a pipeline and writes a `runs/{runId}/manifest.json` populated with
8
+ * one entry per produced artifact type.
9
+ *
10
+ * Before W0051 revisit: only `calculate-scores-step` registered its
11
+ * ref (for `testOutputs`) into `state.artifactRefs`; every other
12
+ * producer discarded the returned ref. The result was empty
13
+ * `Report.artifactManifest` fields and no per-entry preview lookup
14
+ * for Studio hooks. Wrapping the writer at the composition-root level
15
+ * closes that gap without per-producer bookkeeping.
16
+ *
17
+ * Merging rules (per type):
18
+ * - `bulk`: last-writer-wins. A pipeline that emits the same bulk
19
+ * artifact twice overwrites — matches GCS semantics.
20
+ * - `per-entry`: entries accumulate into a keyed map. A later emit
21
+ * at the same `entries[].key` replaces the earlier one.
22
+ *
23
+ * The decorator holds no disk state; the `_resetAccumulated()` hook is
24
+ * for unit tests that rerun emit sequences within a single writer.
25
+ *
26
+ * @see docs/decisions/D0033-unified-run-anchored-artifact-capture.md (§ M5)
27
+ */
28
+ export class AccumulatingArtifactWriter {
29
+ /**
30
+ * Exposed so composition-root tests can assert on the underlying backend
31
+ * (LocalFilesystemArtifactWriter, FanoutArtifactWriter, etc.) without
32
+ * plumbing a separate accessor. Treat as read-only.
33
+ */
34
+ inner;
35
+ accumulated = {};
36
+ constructor(inner) {
37
+ this.inner = inner;
38
+ }
39
+ /** Snapshot of every ref produced this far, keyed by artifact type. */
40
+ getAccumulatedArtifactRefs() {
41
+ return { ...this.accumulated };
42
+ }
43
+ /** Test-only. Clears accumulated refs without touching the inner writer. */
44
+ _resetAccumulated() {
45
+ for (const k of Object.keys(this.accumulated)) {
46
+ delete this.accumulated[k];
47
+ }
48
+ }
49
+ // ---- ArtifactWriter surface --------------------------------------------
50
+ async emit(type, association, payload) {
51
+ const ref = await this.inner.emit(type, association, payload);
52
+ if (ref)
53
+ this.mergeRef(type, ref);
54
+ return ref;
55
+ }
56
+ async appendNdjson(type, association, rows) {
57
+ const ref = await this.inner.appendNdjson(type, association, rows);
58
+ if (ref)
59
+ this.mergeRef(type, ref);
60
+ return ref;
61
+ }
62
+ async writeManifest(runId, manifest) {
63
+ return this.inner.writeManifest(runId, manifest);
64
+ }
65
+ /** @deprecated — forwarded to the inner writer without accumulation. */
66
+ async writeBulk(type, runId, data) {
67
+ const ref = await this.inner.writeBulk(type, runId, data);
68
+ if (ref)
69
+ this.mergeRef(type, ref);
70
+ return ref;
71
+ }
72
+ /** @deprecated — forwarded to the inner writer without accumulation. */
73
+ async writePerEntry(type, runId, entries) {
74
+ const ref = await this.inner.writePerEntry(type, runId, entries);
75
+ if (ref)
76
+ this.mergeRef(type, ref);
77
+ return ref;
78
+ }
79
+ // ---- Merge rules --------------------------------------------------------
80
+ mergeRef(type, ref) {
81
+ const existing = this.accumulated[type];
82
+ if (!existing) {
83
+ this.accumulated[type] = ref;
84
+ return;
85
+ }
86
+ // Bulk: last-writer-wins. A step that re-emits a bulk artifact (e.g.
87
+ // a rerun of calculate-scores) overwrites the earlier body on disk,
88
+ // so the manifest reflects the latest.
89
+ if (ref.layout === "bulk") {
90
+ this.accumulated[type] = ref;
91
+ return;
92
+ }
93
+ // Per-entry: merge entries by key. Duplicate keys replace (last write
94
+ // wins at that key — matches local/gcs overwrite semantics).
95
+ const merged = new Map();
96
+ for (const e of existing.entries ?? [])
97
+ merged.set(e.key, e);
98
+ for (const e of ref.entries ?? [])
99
+ merged.set(e.key, e);
100
+ const entries = Array.from(merged.values());
101
+ this.accumulated[type] = {
102
+ ...existing,
103
+ layout: "per-entry",
104
+ entries,
105
+ entryCount: entries.length,
106
+ bytes: entries.reduce((sum, e) => sum + (e.bytes ?? 0), 0),
107
+ // `store`, `bucket`, `path` stay from the first ref — per-entry
108
+ // paths are descriptor-derived and stable across calls.
109
+ };
110
+ }
111
+ }
@@ -10,15 +10,24 @@
10
10
  * - Per-entry: GET {apiBaseUrl}/v1/runs/{runId}/artifacts/{type}/{entryKey}/upload-url
11
11
  * - Manifest: GET {apiBaseUrl}/v1/runs/{runId}/artifacts/manifest/upload-url
12
12
  *
13
- * The Gateway routes are wired in Phase 10 (W0047).
13
+ * ## W0049 API surface
14
+ *
15
+ * - `emit(type, association, payload)` — canonical single-shot write. Uses
16
+ * the registry to resolve `layout` and the signing endpoint.
17
+ * - `appendNdjson` — NOT IMPLEMENTED. The API Gateway has no batch-signing
18
+ * endpoint yet (W0052), and per-object signing would issue one sign call
19
+ * per row, which blows the Vercel Function budget. Throws
20
+ * `NotImplementedError` so the gap is explicit rather than a silent no-op.
21
+ * - `writeBulk` / `writePerEntry` — @deprecated legacy surface; removal in W0052.
14
22
  *
15
23
  * Design principles:
16
- * - P5: Non-blocking — any failure returns null and warns, never throws.
24
+ * - P5: Non-blocking — any non-structural failure returns null and warns.
17
25
  * - Stateless — no client state between calls.
18
26
  *
19
27
  * @see docs/decisions/D0032-run-anchored-artifact-store.md
28
+ * @see docs/decisions/D0033-unified-run-anchored-artifact-capture.md
20
29
  */
21
- import { type ArtifactEntry, type ArtifactRef, type ArtifactType, type ArtifactWriter, type RunId, type RunManifest } from "../_vendor/ailf-core/index.d.ts";
30
+ import { type ArtifactEntry, type ArtifactRef, type ArtifactType, type ArtifactWriter, type AssociationValues, type RunId, type RunManifest } from "../_vendor/ailf-core/index.d.ts";
22
31
  export interface ApiGatewayArtifactWriterOptions {
23
32
  /** Base URL of the API gateway (e.g., "https://ailf-api.sanity.build"). */
24
33
  apiBaseUrl: string;
@@ -30,9 +39,13 @@ export interface ApiGatewayArtifactWriterOptions {
30
39
  export declare class ApiGatewayArtifactWriter implements ArtifactWriter {
31
40
  private readonly options;
32
41
  constructor(options: ApiGatewayArtifactWriterOptions);
42
+ emit<T extends ArtifactType>(type: T, association: AssociationValues, payload: unknown): Promise<ArtifactRef | null>;
43
+ appendNdjson(): Promise<ArtifactRef | null>;
44
+ writeManifest(runId: RunId, manifest: RunManifest): Promise<ArtifactRef | null>;
45
+ /** @deprecated Use `emit()` instead. */
33
46
  writeBulk(type: ArtifactType, runId: RunId, data: unknown): Promise<ArtifactRef | null>;
47
+ /** @deprecated Use `emit()` per entry instead. */
34
48
  writePerEntry(type: ArtifactType, runId: RunId, entries: readonly ArtifactEntry[]): Promise<ArtifactRef | null>;
35
- writeManifest(runId: RunId, manifest: RunManifest): Promise<ArtifactRef | null>;
36
49
  private putJson;
37
50
  private putJsonRaw;
38
51
  private fetchSignedUrl;
@@ -10,20 +10,73 @@
10
10
  * - Per-entry: GET {apiBaseUrl}/v1/runs/{runId}/artifacts/{type}/{entryKey}/upload-url
11
11
  * - Manifest: GET {apiBaseUrl}/v1/runs/{runId}/artifacts/manifest/upload-url
12
12
  *
13
- * The Gateway routes are wired in Phase 10 (W0047).
13
+ * ## W0049 API surface
14
+ *
15
+ * - `emit(type, association, payload)` — canonical single-shot write. Uses
16
+ * the registry to resolve `layout` and the signing endpoint.
17
+ * - `appendNdjson` — NOT IMPLEMENTED. The API Gateway has no batch-signing
18
+ * endpoint yet (W0052), and per-object signing would issue one sign call
19
+ * per row, which blows the Vercel Function budget. Throws
20
+ * `NotImplementedError` so the gap is explicit rather than a silent no-op.
21
+ * - `writeBulk` / `writePerEntry` — @deprecated legacy surface; removal in W0052.
14
22
  *
15
23
  * Design principles:
16
- * - P5: Non-blocking — any failure returns null and warns, never throws.
24
+ * - P5: Non-blocking — any non-structural failure returns null and warns.
17
25
  * - Stateless — no client state between calls.
18
26
  *
19
27
  * @see docs/decisions/D0032-run-anchored-artifact-store.md
28
+ * @see docs/decisions/D0033-unified-run-anchored-artifact-capture.md
20
29
  */
21
- import { ARTIFACT_REGISTRY, } from "../_vendor/ailf-core/index.js";
30
+ import { ARTIFACT_REGISTRY, NotImplementedError, } from "../_vendor/ailf-core/index.js";
22
31
  export class ApiGatewayArtifactWriter {
23
32
  options;
24
33
  constructor(options) {
25
34
  this.options = options;
26
35
  }
36
+ // ---- Canonical W0049 API ------------------------------------------------
37
+ async emit(type, association, payload) {
38
+ const descriptor = ARTIFACT_REGISTRY[type];
39
+ const runId = association.run;
40
+ if (!runId) {
41
+ console.warn(` ⚠️ emit("${type}"): association.run is required, skipping`);
42
+ return null;
43
+ }
44
+ if (descriptor.layout === "bulk") {
45
+ const uploadUrlPath = `/v1/runs/${encodeURIComponent(runId)}/artifacts/${encodeURIComponent(type)}/upload-url`;
46
+ return this.putJson(uploadUrlPath, payload, {
47
+ layout: "bulk",
48
+ entryCount: entryCountOf(payload),
49
+ });
50
+ }
51
+ // per-entry
52
+ const entryKey = descriptor.formatEntryKey(association);
53
+ const uploadUrlPath = `/v1/runs/${encodeURIComponent(runId)}/artifacts/${encodeURIComponent(type)}/${encodeURIComponent(entryKey)}/upload-url`;
54
+ const result = await this.putJsonRaw(uploadUrlPath, payload);
55
+ if (!result)
56
+ return null;
57
+ return {
58
+ store: "gcs",
59
+ bucket: result.bucket,
60
+ path: `runs/${runId}/${descriptor.slug}`,
61
+ bytes: result.bytes,
62
+ entryCount: 1,
63
+ layout: "per-entry",
64
+ entries: [{ key: entryKey, bytes: result.bytes, association }],
65
+ };
66
+ }
67
+ async appendNdjson() {
68
+ throw new NotImplementedError("ApiGatewayArtifactWriter.appendNdjson is not supported. " +
69
+ "NDJSON streaming for traces requires the batch signing endpoint " +
70
+ "(W0052). Producers should use GcsArtifactWriter directly when " +
71
+ "running locally, or defer traces emission until the gateway lands " +
72
+ "the batch route.");
73
+ }
74
+ async writeManifest(runId, manifest) {
75
+ const uploadUrlPath = `/v1/runs/${encodeURIComponent(runId)}/artifacts/manifest/upload-url`;
76
+ return this.putJson(uploadUrlPath, manifest, { layout: "bulk" });
77
+ }
78
+ // ---- Deprecated legacy surface (W0052) ----------------------------------
79
+ /** @deprecated Use `emit()` instead. */
27
80
  async writeBulk(type, runId, data) {
28
81
  const uploadUrlPath = `/v1/runs/${encodeURIComponent(runId)}/artifacts/${encodeURIComponent(type)}/upload-url`;
29
82
  return this.putJson(uploadUrlPath, data, {
@@ -31,6 +84,7 @@ export class ApiGatewayArtifactWriter {
31
84
  entryCount: entryCountOf(data),
32
85
  });
33
86
  }
87
+ /** @deprecated Use `emit()` per entry instead. */
34
88
  async writePerEntry(type, runId, entries) {
35
89
  const descriptor = ARTIFACT_REGISTRY[type];
36
90
  if (!descriptor.parseEntryKey) {
@@ -66,10 +120,7 @@ export class ApiGatewayArtifactWriter {
66
120
  entries: uploaded,
67
121
  };
68
122
  }
69
- async writeManifest(runId, manifest) {
70
- const uploadUrlPath = `/v1/runs/${encodeURIComponent(runId)}/artifacts/manifest/upload-url`;
71
- return this.putJson(uploadUrlPath, manifest, { layout: "bulk" });
72
- }
123
+ // ---- Internals ----------------------------------------------------------
73
124
  async putJson(uploadUrlPath, data, meta) {
74
125
  const result = await this.putJsonRaw(uploadUrlPath, data);
75
126
  if (!result)
@@ -0,0 +1,28 @@
1
+ /**
2
+ * emitFileContents() — reads a file from disk and hands its contents to
3
+ * the writer's `emit()`. Shim for the legacy `captureFile(path)` pattern
4
+ * now that the port takes in-memory payloads.
5
+ *
6
+ * Covers the ~13 producer sites that write a file for user-facing output
7
+ * (e.g. promptfoo writes YAML configs and JSON results) and then need to
8
+ * also capture them as artifacts. Reading at emit-time is uniform, keeps
9
+ * the port narrow, and costs nothing at the sizes in play (all descriptors
10
+ * cap ≤10 MB; most are ≤256 KB).
11
+ *
12
+ * Failures are non-blocking per P5 — a missing file or unparseable JSON
13
+ * returns null + warns rather than throwing, so the pipeline keeps moving
14
+ * even if the user-facing file wasn't produced.
15
+ *
16
+ * See `tasks/plan.md § Q2` for the design rationale.
17
+ */
18
+ import { type ArtifactRef, type ArtifactType, type ArtifactWriter, type AssociationValues } from "../_vendor/ailf-core/index.d.ts";
19
+ /**
20
+ * Read a file from disk, parse it per the descriptor's mime, and emit it.
21
+ *
22
+ * - JSON mime: file contents are `JSON.parse`d into an object before `emit()`.
23
+ * - Markdown / YAML mime: file contents are passed to `emit()` as a string.
24
+ * - NDJSON: not supported by `emit()` — use `appendNdjson()` directly instead.
25
+ *
26
+ * Returns null (with a warn) on any error. Never throws.
27
+ */
28
+ export declare function emitFileContents<T extends ArtifactType>(writer: ArtifactWriter, type: T, association: AssociationValues, filePath: string): Promise<ArtifactRef | null>;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * emitFileContents() — reads a file from disk and hands its contents to
3
+ * the writer's `emit()`. Shim for the legacy `captureFile(path)` pattern
4
+ * now that the port takes in-memory payloads.
5
+ *
6
+ * Covers the ~13 producer sites that write a file for user-facing output
7
+ * (e.g. promptfoo writes YAML configs and JSON results) and then need to
8
+ * also capture them as artifacts. Reading at emit-time is uniform, keeps
9
+ * the port narrow, and costs nothing at the sizes in play (all descriptors
10
+ * cap ≤10 MB; most are ≤256 KB).
11
+ *
12
+ * Failures are non-blocking per P5 — a missing file or unparseable JSON
13
+ * returns null + warns rather than throwing, so the pipeline keeps moving
14
+ * even if the user-facing file wasn't produced.
15
+ *
16
+ * See `tasks/plan.md § Q2` for the design rationale.
17
+ */
18
+ import { promises as fs } from "node:fs";
19
+ import { ARTIFACT_REGISTRY, } from "../_vendor/ailf-core/index.js";
20
+ /**
21
+ * Read a file from disk, parse it per the descriptor's mime, and emit it.
22
+ *
23
+ * - JSON mime: file contents are `JSON.parse`d into an object before `emit()`.
24
+ * - Markdown / YAML mime: file contents are passed to `emit()` as a string.
25
+ * - NDJSON: not supported by `emit()` — use `appendNdjson()` directly instead.
26
+ *
27
+ * Returns null (with a warn) on any error. Never throws.
28
+ */
29
+ export async function emitFileContents(writer, type, association, filePath) {
30
+ const descriptor = ARTIFACT_REGISTRY[type];
31
+ if (descriptor.mime === "application/x-ndjson") {
32
+ console.warn(` ⚠️ emitFileContents("${type}", ${filePath}): NDJSON types require appendNdjson(), not emit() — skipping`);
33
+ return null;
34
+ }
35
+ let contents;
36
+ try {
37
+ contents = await fs.readFile(filePath, "utf-8");
38
+ }
39
+ catch (err) {
40
+ const message = err instanceof Error ? err.message : String(err);
41
+ console.warn(` ⚠️ emitFileContents read failed (non-blocking): ${filePath} — ${message}`);
42
+ return null;
43
+ }
44
+ let payload = contents;
45
+ if (descriptor.mime === "application/json") {
46
+ try {
47
+ payload = JSON.parse(contents);
48
+ }
49
+ catch (err) {
50
+ const message = err instanceof Error ? err.message : String(err);
51
+ console.warn(` ⚠️ emitFileContents parse failed (non-blocking): ${filePath} — ${message}`);
52
+ return null;
53
+ }
54
+ }
55
+ return writer.emit(type, association, payload);
56
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * FanoutArtifactWriter — layers multiple writers so each `emit()` fans out
3
+ * to every configured backend.
4
+ *
5
+ * D0033 M4 default wiring:
6
+ * `FanoutArtifactWriter([ LocalFilesystemArtifactWriter, GcsArtifactWriter ])`
7
+ *
8
+ * Semantics:
9
+ * - Fan out in declaration order. Every writer runs, even if earlier ones fail.
10
+ * - Return the **first non-null ArtifactRef**. Local is listed first, so a
11
+ * local success + GCS failure still produces a non-null ref pointing at
12
+ * local — the pipeline succeeds and Studio retrieval works against the
13
+ * local tree with a warning logged for the GCS leg.
14
+ * - Failures on individual writers warn (via their own P5 paths) but do
15
+ * not propagate. The fanout never throws.
16
+ *
17
+ * This writer is a composition primitive — it adds no I/O of its own.
18
+ * Tests verify the fanout semantics against `NoOpArtifactWriter` instances
19
+ * plus a recording test double.
20
+ *
21
+ * @see docs/decisions/D0033-unified-run-anchored-artifact-capture.md (§ M4)
22
+ */
23
+ import type { ArtifactEntry, ArtifactRef, ArtifactType, ArtifactWriter, AssociationValues, RunId, RunManifest } from "../_vendor/ailf-core/index.d.ts";
24
+ export declare class FanoutArtifactWriter implements ArtifactWriter {
25
+ private readonly writers;
26
+ constructor(writers: readonly ArtifactWriter[]);
27
+ emit<T extends ArtifactType>(type: T, association: AssociationValues, payload: unknown): Promise<ArtifactRef | null>;
28
+ appendNdjson<T extends ArtifactType>(type: T, association: AssociationValues, rows: readonly unknown[]): Promise<ArtifactRef | null>;
29
+ writeManifest(runId: RunId, manifest: RunManifest): Promise<ArtifactRef | null>;
30
+ /** @deprecated Use `emit()` instead. */
31
+ writeBulk(type: ArtifactType, runId: RunId, data: unknown): Promise<ArtifactRef | null>;
32
+ /** @deprecated Use `emit()` per entry instead. */
33
+ writePerEntry(type: ArtifactType, runId: RunId, entries: readonly ArtifactEntry[]): Promise<ArtifactRef | null>;
34
+ /**
35
+ * Run `op` against every delegate writer. Any individual writer's rejected
36
+ * promise is caught and logged; the fanout itself never rejects.
37
+ */
38
+ private runAll;
39
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * FanoutArtifactWriter — layers multiple writers so each `emit()` fans out
3
+ * to every configured backend.
4
+ *
5
+ * D0033 M4 default wiring:
6
+ * `FanoutArtifactWriter([ LocalFilesystemArtifactWriter, GcsArtifactWriter ])`
7
+ *
8
+ * Semantics:
9
+ * - Fan out in declaration order. Every writer runs, even if earlier ones fail.
10
+ * - Return the **first non-null ArtifactRef**. Local is listed first, so a
11
+ * local success + GCS failure still produces a non-null ref pointing at
12
+ * local — the pipeline succeeds and Studio retrieval works against the
13
+ * local tree with a warning logged for the GCS leg.
14
+ * - Failures on individual writers warn (via their own P5 paths) but do
15
+ * not propagate. The fanout never throws.
16
+ *
17
+ * This writer is a composition primitive — it adds no I/O of its own.
18
+ * Tests verify the fanout semantics against `NoOpArtifactWriter` instances
19
+ * plus a recording test double.
20
+ *
21
+ * @see docs/decisions/D0033-unified-run-anchored-artifact-capture.md (§ M4)
22
+ */
23
+ export class FanoutArtifactWriter {
24
+ writers;
25
+ constructor(writers) {
26
+ if (writers.length === 0) {
27
+ throw new Error("FanoutArtifactWriter requires at least one delegate writer");
28
+ }
29
+ this.writers = writers;
30
+ }
31
+ async emit(type, association, payload) {
32
+ const refs = await this.runAll((w) => w.emit(type, association, payload));
33
+ return firstNonNull(refs);
34
+ }
35
+ async appendNdjson(type, association, rows) {
36
+ const refs = await this.runAll((w) => w.appendNdjson(type, association, rows));
37
+ return firstNonNull(refs);
38
+ }
39
+ async writeManifest(runId, manifest) {
40
+ const refs = await this.runAll((w) => w.writeManifest(runId, manifest));
41
+ return firstNonNull(refs);
42
+ }
43
+ /** @deprecated Use `emit()` instead. */
44
+ async writeBulk(type, runId, data) {
45
+ const refs = await this.runAll((w) => w.writeBulk(type, runId, data));
46
+ return firstNonNull(refs);
47
+ }
48
+ /** @deprecated Use `emit()` per entry instead. */
49
+ async writePerEntry(type, runId, entries) {
50
+ const refs = await this.runAll((w) => w.writePerEntry(type, runId, entries));
51
+ return firstNonNull(refs);
52
+ }
53
+ /**
54
+ * Run `op` against every delegate writer. Any individual writer's rejected
55
+ * promise is caught and logged; the fanout itself never rejects.
56
+ */
57
+ async runAll(op) {
58
+ return Promise.all(this.writers.map(async (writer) => {
59
+ try {
60
+ return await op(writer);
61
+ }
62
+ catch (err) {
63
+ const message = err instanceof Error ? err.message : String(err);
64
+ console.warn(` ⚠️ Fanout delegate threw (non-blocking): ${writer.constructor.name} — ${message}`);
65
+ return null;
66
+ }
67
+ }));
68
+ }
69
+ }
70
+ function firstNonNull(refs) {
71
+ for (const ref of refs) {
72
+ if (ref !== null)
73
+ return ref;
74
+ }
75
+ return null;
76
+ }
@@ -7,24 +7,61 @@
7
7
  *
8
8
  * Paths come from `ARTIFACT_REGISTRY` so writers, signers, and readers agree.
9
9
  *
10
+ * ## W0049 API surface
11
+ *
12
+ * - `emit(type, association, payload)` — the canonical single-shot write.
13
+ * Dispatch on `descriptor.layout` is internal; callers pass axis values
14
+ * and the writer resolves the path.
15
+ * - `appendNdjson(type, association, rows)` — streaming-append for `traces`.
16
+ * Each call writes a numbered part object (`.ndjson.part-NNNN`); the
17
+ * parts are composed into the final object lazily at `writeManifest` time
18
+ * via GCS object compose. When a stream accumulates > 32 parts the writer
19
+ * rolls up parts into intermediate composites to stay under the GCS
20
+ * compose cap.
21
+ * - `writeBulk` / `writePerEntry` — @deprecated legacy surface. Removal in W0052.
22
+ *
10
23
  * Design principles:
11
24
  * - P5: Non-blocking — upload failure returns null, never throws.
12
25
  * - Lazy client — Storage created on first write.
13
26
  *
14
27
  * @see docs/decisions/D0032-run-anchored-artifact-store.md
28
+ * @see docs/decisions/D0033-unified-run-anchored-artifact-capture.md
15
29
  */
16
- import { type ArtifactEntry, type ArtifactRef, type ArtifactType, type ArtifactWriter, type RunId, type RunManifest } from "../_vendor/ailf-core/index.d.ts";
30
+ import { Storage } from "@google-cloud/storage";
31
+ import { type ArtifactEntry, type ArtifactRef, type ArtifactType, type ArtifactWriter, type AssociationValues, type RunId, type RunManifest } from "../_vendor/ailf-core/index.d.ts";
17
32
  export interface GcsArtifactWriterOptions {
18
33
  /** GCS bucket name (e.g., "ailf-artifacts") */
19
34
  bucket: string;
35
+ /**
36
+ * Optional pre-constructed Storage client. When omitted the writer
37
+ * constructs one lazily from Application Default Credentials. Test code
38
+ * supplies a fake here to avoid real network calls.
39
+ */
40
+ storage?: Storage;
20
41
  }
21
42
  export declare class GcsArtifactWriter implements ArtifactWriter {
22
43
  private client;
23
44
  private readonly options;
45
+ private readonly ndjsonStreams;
24
46
  constructor(options: GcsArtifactWriterOptions);
47
+ emit<T extends ArtifactType>(type: T, association: AssociationValues, payload: unknown): Promise<ArtifactRef | null>;
48
+ appendNdjson<T extends ArtifactType>(type: T, association: AssociationValues, rows: readonly unknown[]): Promise<ArtifactRef | null>;
49
+ writeManifest(runId: RunId, manifest: RunManifest): Promise<ArtifactRef | null>;
50
+ /** @deprecated Use `emit()` instead. Routes through the same GCS I/O. */
25
51
  writeBulk(type: ArtifactType, runId: RunId, data: unknown): Promise<ArtifactRef | null>;
52
+ /** @deprecated Use `emit()` per entry instead. */
26
53
  writePerEntry(type: ArtifactType, runId: RunId, entries: readonly ArtifactEntry[]): Promise<ArtifactRef | null>;
27
- writeManifest(runId: RunId, manifest: RunManifest): Promise<ArtifactRef | null>;
28
- private putJson;
54
+ /**
55
+ * Compose all buffered NDJSON streams into their final objects. Called
56
+ * from `writeManifest` so the manifest is the single sync point for
57
+ * NDJSON finalization.
58
+ *
59
+ * When `partCount > GCS_COMPOSE_MAX`, parts are rolled up in groups of
60
+ * `GCS_COMPOSE_MAX` into intermediate composites, then composed. This
61
+ * stays under the per-call source cap at the cost of one extra round
62
+ * trip per 32 additional parts.
63
+ */
64
+ private finalizeNdjsonStreams;
65
+ private putBody;
29
66
  private getClient;
30
67
  }