@noetic-tools/core 5.19.1-next.1 → 5.20.0-next.1

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 (150) hide show
  1. package/dist/adapters/openrouter.js +5 -0
  2. package/dist/builders/control-flow-builders.d.ts +15 -6
  3. package/dist/builders/control-flow-builders.js +8 -0
  4. package/dist/builders/decide-builders.d.ts +2 -5
  5. package/dist/builders/dynamic-workflow.d.ts +16 -1
  6. package/dist/builders/dynamic-workflow.js +222 -11
  7. package/dist/builders/every.d.ts +3 -3
  8. package/dist/builders/every.js +2 -1
  9. package/dist/builders/provide-builder.d.ts +3 -2
  10. package/dist/builders/provide-builder.js +2 -1
  11. package/dist/builders/sequence.d.ts +14 -0
  12. package/dist/builders/sequence.js +103 -0
  13. package/dist/builders/spawn-builder.d.ts +3 -2
  14. package/dist/builders/spawn-builder.js +3 -2
  15. package/dist/builders/step-builders.d.ts +80 -1
  16. package/dist/builders/step-builders.js +115 -1
  17. package/dist/builders/verify-builder.d.ts +79 -0
  18. package/dist/builders/verify-builder.js +69 -0
  19. package/dist/builders/workflow-child-nodes.d.ts +21 -0
  20. package/dist/builders/workflow-child-nodes.js +335 -0
  21. package/dist/builders/workflow-connection-nodes.d.ts +17 -0
  22. package/dist/builders/workflow-connection-nodes.js +160 -0
  23. package/dist/builders/workflow-connections.d.ts +90 -0
  24. package/dist/builders/workflow-connections.js +560 -0
  25. package/dist/builders/workflow-control-nodes.d.ts +13 -0
  26. package/dist/builders/workflow-control-nodes.js +213 -0
  27. package/dist/builders/workflow-hydrator.d.ts +44 -3
  28. package/dist/builders/workflow-hydrator.js +53 -495
  29. package/dist/builders/workflow-verify.d.ts +19 -0
  30. package/dist/builders/workflow-verify.js +194 -0
  31. package/dist/conditions/conditions.d.ts +7 -5
  32. package/dist/conditions/conditions.js +7 -4
  33. package/dist/connections/blobs.d.ts +33 -0
  34. package/dist/connections/blobs.js +77 -0
  35. package/dist/connections/cron-schedule.d.ts +32 -0
  36. package/dist/connections/cron-schedule.js +213 -0
  37. package/dist/connections/durability.d.ts +65 -0
  38. package/dist/connections/durability.js +54 -0
  39. package/dist/connections/errors.d.ts +30 -0
  40. package/dist/connections/errors.js +100 -0
  41. package/dist/connections/event-stream.d.ts +24 -0
  42. package/dist/connections/event-stream.js +176 -0
  43. package/dist/connections/gates.d.ts +94 -0
  44. package/dist/connections/gates.js +271 -0
  45. package/dist/connections/glob.d.ts +10 -0
  46. package/dist/connections/glob.js +46 -0
  47. package/dist/connections/handle.d.ts +22 -0
  48. package/dist/connections/handle.js +84 -0
  49. package/dist/connections/harness-wiring.d.ts +52 -0
  50. package/dist/connections/harness-wiring.js +143 -0
  51. package/dist/connections/in-memory-connection.d.ts +147 -0
  52. package/dist/connections/in-memory-connection.js +593 -0
  53. package/dist/connections/index.d.ts +34 -0
  54. package/dist/connections/index.js +23 -0
  55. package/dist/connections/json-schema.d.ts +28 -0
  56. package/dist/connections/json-schema.js +338 -0
  57. package/dist/connections/model-tools.d.ts +46 -0
  58. package/dist/connections/model-tools.js +466 -0
  59. package/dist/connections/negotiation.d.ts +30 -0
  60. package/dist/connections/negotiation.js +159 -0
  61. package/dist/connections/policy.d.ts +21 -0
  62. package/dist/connections/policy.js +34 -0
  63. package/dist/connections/pool.d.ts +111 -0
  64. package/dist/connections/pool.js +339 -0
  65. package/dist/connections/refs.d.ts +74 -0
  66. package/dist/connections/refs.js +316 -0
  67. package/dist/connections/registry-types.d.ts +141 -0
  68. package/dist/connections/registry-types.js +14 -0
  69. package/dist/connections/registry.d.ts +125 -0
  70. package/dist/connections/registry.js +775 -0
  71. package/dist/connections/secrets.d.ts +59 -0
  72. package/dist/connections/secrets.js +153 -0
  73. package/dist/connections/settle.d.ts +9 -0
  74. package/dist/connections/settle.js +26 -0
  75. package/dist/connections/sources.d.ts +27 -0
  76. package/dist/connections/sources.js +300 -0
  77. package/dist/connections/triggers.d.ts +78 -0
  78. package/dist/connections/triggers.js +310 -0
  79. package/dist/connections/values.d.ts +26 -0
  80. package/dist/connections/values.js +93 -0
  81. package/dist/connections/views.d.ts +31 -0
  82. package/dist/connections/views.js +176 -0
  83. package/dist/expression/check.js +80 -1
  84. package/dist/expression/jsonata.d.ts +14 -4
  85. package/dist/expression/jsonata.js +27 -9
  86. package/dist/expression/step-outputs.d.ts +25 -0
  87. package/dist/expression/step-outputs.js +78 -0
  88. package/dist/harness/agent-harness.d.ts +69 -166
  89. package/dist/harness/agent-harness.js +170 -262
  90. package/dist/harness/call-span-collector.d.ts +33 -0
  91. package/dist/harness/call-span-collector.js +83 -0
  92. package/dist/harness/call-spend.d.ts +2 -0
  93. package/dist/harness/call-spend.js +10 -14
  94. package/dist/harness/connection-gate.d.ts +78 -0
  95. package/dist/harness/connection-gate.js +169 -0
  96. package/dist/harness/deps/interpreter.d.ts +1 -0
  97. package/dist/harness/deps/interpreter.js +1 -0
  98. package/dist/harness/deps/runtime.d.ts +1 -1
  99. package/dist/harness/deps/types.d.ts +1 -1
  100. package/dist/harness/harness-options.d.ts +153 -0
  101. package/dist/harness/harness-options.js +36 -0
  102. package/dist/harness/harness-tracing.d.ts +16 -0
  103. package/dist/harness/harness-tracing.js +35 -0
  104. package/dist/harness/llm-client.d.ts +71 -0
  105. package/dist/harness/llm-client.js +218 -0
  106. package/dist/harness/model-call.d.ts +2 -7
  107. package/dist/harness/model-call.js +141 -558
  108. package/dist/harness/model-request.d.ts +42 -0
  109. package/dist/harness/model-request.js +159 -0
  110. package/dist/harness/model-rounds.d.ts +18 -2
  111. package/dist/harness/model-rounds.js +39 -8
  112. package/dist/harness/model-stream.d.ts +39 -0
  113. package/dist/harness/model-stream.js +131 -0
  114. package/dist/harness/model-tool-round.d.ts +37 -0
  115. package/dist/harness/model-tool-round.js +197 -0
  116. package/dist/harness/spend.d.ts +2 -0
  117. package/dist/harness/spend.js +5 -1
  118. package/dist/index.d.ts +31 -6
  119. package/dist/index.js +18 -1
  120. package/dist/interpreter/connection-steps.d.ts +194 -0
  121. package/dist/interpreter/connection-steps.js +537 -0
  122. package/dist/interpreter/execute-action.d.ts +2 -8
  123. package/dist/interpreter/execute-action.js +3 -178
  124. package/dist/interpreter/execute-call.d.ts +19 -0
  125. package/dist/interpreter/execute-call.js +99 -0
  126. package/dist/interpreter/execute-control.d.ts +6 -1
  127. package/dist/interpreter/execute-control.js +14 -0
  128. package/dist/interpreter/execute-invoke-tool.d.ts +11 -0
  129. package/dist/interpreter/execute-invoke-tool.js +183 -0
  130. package/dist/interpreter/execute-verify.d.ts +21 -0
  131. package/dist/interpreter/execute-verify.js +391 -0
  132. package/dist/interpreter/execute-wait.d.ts +21 -0
  133. package/dist/interpreter/execute-wait.js +233 -0
  134. package/dist/interpreter/execute.js +24 -133
  135. package/dist/interpreter/step-kind-dispatch.d.ts +24 -0
  136. package/dist/interpreter/step-kind-dispatch.js +157 -0
  137. package/dist/interpreter/step-walk.d.ts +2 -0
  138. package/dist/interpreter/step-walk.js +24 -0
  139. package/dist/interpreter/verify-host.d.ts +40 -0
  140. package/dist/interpreter/verify-host.js +245 -0
  141. package/dist/runtime/durable/step-ledger.d.ts +34 -2
  142. package/dist/runtime/durable/step-ledger.js +97 -3
  143. package/dist/runtime/in-memory-artifact-store.d.ts +24 -0
  144. package/dist/runtime/in-memory-artifact-store.js +240 -0
  145. package/dist/schemas/workflow.d.ts +2 -2
  146. package/dist/schemas/workflow.js +1 -1
  147. package/dist/util/harness-internals.d.ts +23 -0
  148. package/dist/util/harness-internals.js +35 -0
  149. package/package.json +2 -2
  150. package/schema/noetic-workflow.schema.json +1170 -92
@@ -0,0 +1,245 @@
1
+ /**
2
+ * What core hands the verifier for one verify (spec 37 § Under the hood): the
3
+ * subject and witnesses as gated `VerifyConnection`s, `act` run through the
4
+ * usual gates, and the run document stored as a blob. Built against the
5
+ * registry contract and `@noetic-tools/types/verify` only — core never imports
6
+ * `@noetic-tools/verify` or `open-verification`.
7
+ */
8
+ import { frameworkCast, NoeticConfigError } from '@noetic-tools/types';
9
+ import { AnomalyKind, OvpChannelIdSchema, VerifyConfigErrorCode } from '@noetic-tools/types/verify';
10
+ import { z } from 'zod';
11
+ import { createInMemoryArtifactStore } from '../runtime/in-memory-artifact-store.js';
12
+ import { harnessInternal } from '../util/harness-internals.js';
13
+ import { idempotencyKeyFor, runRootOf, stepPathOf } from './connection-steps.js';
14
+ //#region Constants
15
+ /** The resource a server lists its blind spots at (spec 36 § Parts every server provides). */
16
+ const COVERAGE_RESOURCE = 'noetic://coverage';
17
+ /** The operation a server reports anomalies through. */
18
+ const DETECT_OP = 'noetic.detect';
19
+ const BlindSpotSchema = z.object({
20
+ kind: z.enum([
21
+ 'channel-unobserved',
22
+ 'boundary-uncrossable',
23
+ 'buffer-truncated',
24
+ 'redacted',
25
+ 'still-in-flight',
26
+ 'effect-elsewhere',
27
+ ]),
28
+ channel: OvpChannelIdSchema.optional(),
29
+ detail: z.string().min(1),
30
+ impeaching: z.boolean(),
31
+ remedy: z.string().optional(),
32
+ });
33
+ /** A named OVP anomaly kind or an `x-` kind; anything else is not OVP and is dropped. */
34
+ const AnomalyKindSchema = z.union([
35
+ z.enum(AnomalyKind),
36
+ z.string().regex(/^x-[a-z0-9-]+$/),
37
+ ]);
38
+ const AnomalySchema = z.object({
39
+ kind: AnomalyKindSchema,
40
+ tier: z.enum([
41
+ 'observed',
42
+ 'absence-derived',
43
+ 'advisory',
44
+ ]),
45
+ claim: z.string(),
46
+ counter: z.string(),
47
+ between: z.tuple([
48
+ OvpChannelIdSchema,
49
+ OvpChannelIdSchema,
50
+ ]),
51
+ evidence: z.array(z.string()).default([]),
52
+ });
53
+ const ReportListSchema = z.union([
54
+ z.array(z.unknown()),
55
+ z.object({
56
+ blindSpots: z.array(z.unknown()),
57
+ }),
58
+ z.object({
59
+ anomalies: z.array(z.unknown()),
60
+ }),
61
+ ]);
62
+ /** A store per harness for runs whose harness configures none (spec 08: in memory by default). */
63
+ const FALLBACK_STORES = new WeakMap();
64
+ //#endregion
65
+ //#region Helpers
66
+ function harnessOf(ctx) {
67
+ return frameworkCast(ctx.harness);
68
+ }
69
+ function hasCoverage(resolved) {
70
+ return Object.keys(resolved.manifest.resources).some((name) => name.startsWith(COVERAGE_RESOURCE));
71
+ }
72
+ /** The entries of a server's report: a bare list, or one under `blindSpots`/`anomalies`. */
73
+ function reportEntries(raw) {
74
+ const parsed = ReportListSchema.safeParse(raw);
75
+ if (!parsed.success) {
76
+ return [];
77
+ }
78
+ const data = parsed.data;
79
+ if (Array.isArray(data)) {
80
+ return data;
81
+ }
82
+ return 'blindSpots' in data ? data.blindSpots : data.anomalies;
83
+ }
84
+ /**
85
+ * Each entry that is valid OVP, by `schema`; one that is not is dropped, so a
86
+ * stored run stays an OVP VerificationRun any OVP tool can read.
87
+ */
88
+ function validEntries(raw, schema) {
89
+ return reportEntries(raw).flatMap((entry) => {
90
+ const parsed = schema.safeParse(entry);
91
+ return parsed.success
92
+ ? [
93
+ parsed.data,
94
+ ]
95
+ : [];
96
+ });
97
+ }
98
+ function blindSpotsFrom(raw) {
99
+ return validEntries(raw, BlindSpotSchema);
100
+ }
101
+ function anomaliesFrom(raw) {
102
+ return validEntries(raw, AnomalySchema);
103
+ }
104
+ /** A witness may run only operations without effects (spec 37 § Witnesses). */
105
+ function refuseWitnessEffects(name, op, info) {
106
+ if (info !== undefined && info.effects !== true) {
107
+ return;
108
+ }
109
+ throw new NoeticConfigError({
110
+ code: VerifyConfigErrorCode.Invalid,
111
+ message: `Witness '${name}' was asked to run '${op}', which ${info ? 'has effects' : 'it does not offer'}.`,
112
+ hint: 'A witness can look and cannot touch: run only its operations without effects, or make it the subject.',
113
+ });
114
+ }
115
+ //#endregion
116
+ //#region Public API
117
+ /** The verifier the harness registered (`new AgentHarness({ verification })`), if any. */
118
+ export function verificationOf(ctx) {
119
+ return harnessInternal(ctx.harness, 'verifier');
120
+ }
121
+ /** The harness artifact store (`environment.artifacts`), or an in-memory one per harness. */
122
+ export function artifactsOf(ctx) {
123
+ const harness = harnessOf(ctx);
124
+ const configured = harness.artifacts;
125
+ if (configured) {
126
+ return configured;
127
+ }
128
+ const key = frameworkCast(ctx.harness);
129
+ const existing = FALLBACK_STORES.get(key);
130
+ if (existing) {
131
+ return existing;
132
+ }
133
+ const store = createInMemoryArtifactStore();
134
+ FALLBACK_STORES.set(key, store);
135
+ return store;
136
+ }
137
+ /** A participant as `Verifier.prepare` sees it. */
138
+ export function participantOf(resolved, role) {
139
+ return {
140
+ name: resolved.name,
141
+ role,
142
+ identity: resolved.identity(),
143
+ manifest: resolved.manifest,
144
+ senses: resolved.manifest.senses,
145
+ ...(resolved.manifest.determinism === undefined
146
+ ? {}
147
+ : {
148
+ determinism: resolved.manifest.determinism,
149
+ }),
150
+ };
151
+ }
152
+ /**
153
+ * A resolved connection as the verifier may use it: every call goes through the
154
+ * registry's gates, and a witness refuses anything with effects.
155
+ */
156
+ export function verifyConnectionOf(opts) {
157
+ const { resolved, role, ctx, stepId } = opts;
158
+ let calls = 0;
159
+ const call = async (op, args, callOpts) => {
160
+ if (role === 'witness') {
161
+ refuseWitnessEffects(resolved.name, op, resolved.manifest.operations[op]);
162
+ }
163
+ calls += 1;
164
+ return resolved.call(op, args, {
165
+ signal: callOpts.signal,
166
+ caller: 'step',
167
+ idempotencyKey: `${idempotencyKeyFor(ctx, stepId)}:verify:${resolved.name}:${calls}`,
168
+ stepId,
169
+ });
170
+ };
171
+ return {
172
+ name: resolved.name,
173
+ manifest: resolved.manifest,
174
+ identity: () => resolved.identity(),
175
+ call,
176
+ read: (resource, readOpts) => resolved.read(resource, readOpts),
177
+ listen: (req) => resolved.listen({
178
+ ...req,
179
+ cursor: null,
180
+ }),
181
+ ...(hasCoverage(resolved)
182
+ ? {
183
+ coverage: async (since, covOpts) => blindSpotsFrom(await resolved.read(since === null
184
+ ? COVERAGE_RESOURCE
185
+ : `${COVERAGE_RESOURCE}?since=${encodeURIComponent(since)}`, covOpts)),
186
+ }
187
+ : {}),
188
+ ...(resolved.manifest.operations[DETECT_OP] === undefined
189
+ ? {}
190
+ : {
191
+ detect: async (since, detectOpts) => anomaliesFrom(await call(DETECT_OP, {
192
+ since,
193
+ }, detectOpts)),
194
+ }),
195
+ };
196
+ }
197
+ /** Runs `act` in sequence, each step's output the next one's input. Never rejects: a throw becomes `error`. */
198
+ export async function runAct(opts) {
199
+ const { steps, input, ctx, executeStep, touched } = opts;
200
+ let current = input;
201
+ try {
202
+ for (const child of steps) {
203
+ current = await executeStep(child, frameworkCast(current), ctx);
204
+ }
205
+ return {
206
+ output: current,
207
+ pendingTasks: [],
208
+ touched,
209
+ };
210
+ }
211
+ catch (error) {
212
+ return {
213
+ error: error instanceof Error ? error : new Error(String(error)),
214
+ pendingTasks: [],
215
+ touched,
216
+ };
217
+ }
218
+ }
219
+ /** Stores a run document as a blob linked to the run, pinned, and notes it on the trace. */
220
+ export async function storeRun(opts) {
221
+ const { ctx, stepId, run } = opts;
222
+ const store = artifactsOf(ctx);
223
+ const bytes = new TextEncoder().encode(JSON.stringify(run));
224
+ const ref = await store.put(bytes, {
225
+ mimeType: 'application/json',
226
+ name: `${stepId}.verification-run.json`,
227
+ });
228
+ const stepPath = stepPathOf(ctx);
229
+ await store.attach(ref.$blob.uri, {
230
+ executionId: runRootOf(ctx).id,
231
+ source: 'verify',
232
+ pinned: true,
233
+ ...(stepPath === undefined
234
+ ? {}
235
+ : {
236
+ stepPath,
237
+ }),
238
+ });
239
+ ctx.span.addEvent('verify.run', {
240
+ stepId,
241
+ uri: ref.$blob.uri,
242
+ });
243
+ return ref;
244
+ }
245
+ //#endregion
@@ -44,10 +44,23 @@ export declare const StepLedgerEntrySchema: z.ZodObject<{
44
44
  inputHash: z.ZodOptional<z.ZodString>;
45
45
  items: z.ZodOptional<z.ZodArray<z.ZodUnknown>>;
46
46
  streamed: z.ZodOptional<z.ZodArray<z.ZodNumber>>;
47
+ connections: z.ZodOptional<z.ZodArray<z.ZodObject<{
48
+ name: z.ZodString;
49
+ surface: z.ZodString;
50
+ instance: z.ZodString;
51
+ }, z.core.$strip>>>;
47
52
  completedAt: z.ZodString;
48
53
  }, z.core.$strip>;
49
54
  /** @public A completed step recorded for replay. */
50
55
  export type StepLedgerEntry = z.infer<typeof StepLedgerEntrySchema>;
56
+ /** @public A connection a ledger entry used, with the identity it had then. */
57
+ export type StepLedgerConnectionUse = NonNullable<StepLedgerEntry['connections']>[number];
58
+ /** @public A replayable entry's connection uses, in recording order. */
59
+ export interface StepLedgerConnectionUses {
60
+ readonly seq: number;
61
+ readonly path: string;
62
+ readonly uses: ReadonlyArray<StepLedgerConnectionUse>;
63
+ }
51
64
  /**
52
65
  * @public
53
66
  * Bounds on what one execution's ledger retains. Both caps degrade resume rather than
@@ -145,7 +158,7 @@ export declare function createStepLedgerStore(opts: {
145
158
  }): StepLedgerStore;
146
159
  /** @public What retention did to one execution's ledger. Observable for host logging. */
147
160
  export interface StepLedgerStats {
148
- /** Entries written. */
161
+ /** Paths recorded. A record that replaces the entry at its path counts no more. */
149
162
  recorded: number;
150
163
  /** Entries not written because their output exceeded `maxEntryBytes`. */
151
164
  droppedOversize: number;
@@ -177,6 +190,8 @@ export declare class StepLedger {
177
190
  private readonly replayable;
178
191
  /** Sequence number holding each live entry, recovered or recorded, by path. */
179
192
  private readonly live;
193
+ /** Connections used at each path this run, attached to the entry recorded there or above. */
194
+ private readonly connectionNotes;
180
195
  /** Sequence number the next recorded entry takes. */
181
196
  private seq;
182
197
  private readonly store?;
@@ -215,13 +230,30 @@ export declare class StepLedger {
215
230
  * Best-effort: a failed write costs resumability, not the run. Resolves whether
216
231
  * the entry was stored.
217
232
  */
218
- record(entry: StepLedgerEntry): Promise<boolean>;
233
+ record(recorded: StepLedgerEntry): Promise<boolean>;
234
+ /** Drop the entry recorded at `path`, when there is one. */
235
+ drop(path: string): Promise<void>;
236
+ /**
237
+ * Notes that the step at `path` used a connection with the given identity; the
238
+ * entry recorded at `path` (or at any path above it) carries it.
239
+ */
240
+ noteConnection(path: string, use: StepLedgerConnectionUse): void;
241
+ /** Every replayable entry that used a connection, in recording order. */
242
+ connectionUses(): StepLedgerConnectionUses[];
243
+ /**
244
+ * Discards every replayable entry recorded at or after sequence `seq`: the
245
+ * connection a step there used was replaced, so it and everything after it run
246
+ * live (spec 35 § Durability). Resolves the number discarded.
247
+ */
248
+ discardFrom(seq: number): Promise<number>;
219
249
  /**
220
250
  * Drop every positional entry. A host calls this once a run has finished and the
221
251
  * checkpoint holds its result: every later resume starts past them, so they are dead.
222
252
  * `key:` entries stay — they are keyed by content, and a later run may ask the same.
223
253
  */
224
254
  dropPaths(): Promise<void>;
255
+ /** `entry` carrying every connection noted at its path or beneath it. */
256
+ private withConnections;
225
257
  private prune;
226
258
  private deleteSeq;
227
259
  /**
@@ -60,6 +60,18 @@ export const StepLedgerEntrySchema = z.object({
60
60
  * streamed again on replay when the harness sets `emitItemsOnReplay`.
61
61
  */
62
62
  streamed: z.array(z.number().int().nonnegative()).optional(),
63
+ /**
64
+ * The connections the step (or anything beneath it) used, each with the identity
65
+ * it had (spec 35 § Durability). A resumed run discards the ledger from the first
66
+ * entry whose connection was replaced since.
67
+ */
68
+ connections: z
69
+ .array(z.object({
70
+ name: z.string(),
71
+ surface: z.string(),
72
+ instance: z.string(),
73
+ }))
74
+ .optional(),
63
75
  completedAt: z.string(),
64
76
  });
65
77
  /** @public Retention applied when a harness does not configure `stepLedgerRetention`. */
@@ -235,6 +247,8 @@ export class StepLedger {
235
247
  replayable = new Map();
236
248
  /** Sequence number holding each live entry, recovered or recorded, by path. */
237
249
  live = new LiveEntries();
250
+ /** Connections used at each path this run, attached to the entry recorded there or above. */
251
+ connectionNotes = new Map();
238
252
  /** Sequence number the next recorded entry takes. */
239
253
  seq;
240
254
  store;
@@ -296,6 +310,9 @@ export class StepLedger {
296
310
  return undefined;
297
311
  }
298
312
  if (matches(entry, step)) {
313
+ for (const use of entry.connections ?? []) {
314
+ this.noteConnection(path, use);
315
+ }
299
316
  return entry;
300
317
  }
301
318
  if (path.startsWith(REPLAY_KEY_PREFIX)) {
@@ -317,10 +334,11 @@ export class StepLedger {
317
334
  * Best-effort: a failed write costs resumability, not the run. Resolves whether
318
335
  * the entry was stored.
319
336
  */
320
- async record(entry) {
337
+ async record(recorded) {
321
338
  if (!this.store) {
322
339
  return false;
323
340
  }
341
+ const entry = this.withConnections(recorded);
324
342
  if (!this.retainable(entry)) {
325
343
  return false;
326
344
  }
@@ -338,8 +356,10 @@ export class StepLedger {
338
356
  }
339
357
  const replaced = this.live.get(entry.path);
340
358
  this.live.set(entry.path, seq);
341
- this.counts.recorded += 1;
342
- if (replaced !== undefined) {
359
+ if (replaced === undefined) {
360
+ this.counts.recorded += 1;
361
+ }
362
+ else {
343
363
  await this.deleteSeq(replaced);
344
364
  }
345
365
  if (!entry.path.startsWith(REPLAY_KEY_PREFIX)) {
@@ -347,6 +367,54 @@ export class StepLedger {
347
367
  }
348
368
  return true;
349
369
  }
370
+ /** Drop the entry recorded at `path`, when there is one. */
371
+ async drop(path) {
372
+ await this.prune([
373
+ path,
374
+ ]);
375
+ }
376
+ /**
377
+ * Notes that the step at `path` used a connection with the given identity; the
378
+ * entry recorded at `path` (or at any path above it) carries it.
379
+ */
380
+ noteConnection(path, use) {
381
+ const notes = this.connectionNotes.get(path) ?? [];
382
+ if (!notes.some((n) => sameUse(n, use))) {
383
+ notes.push(use);
384
+ }
385
+ this.connectionNotes.set(path, notes);
386
+ }
387
+ /** Every replayable entry that used a connection, in recording order. */
388
+ connectionUses() {
389
+ const found = [];
390
+ for (const [path, entry] of this.replayable) {
391
+ const seq = this.live.get(path);
392
+ if (seq === undefined || !entry.connections || entry.connections.length === 0) {
393
+ continue;
394
+ }
395
+ found.push({
396
+ seq,
397
+ path,
398
+ uses: entry.connections,
399
+ });
400
+ }
401
+ return found.sort((a, b) => a.seq - b.seq);
402
+ }
403
+ /**
404
+ * Discards every replayable entry recorded at or after sequence `seq`: the
405
+ * connection a step there used was replaced, so it and everything after it run
406
+ * live (spec 35 § Durability). Resolves the number discarded.
407
+ */
408
+ async discardFrom(seq) {
409
+ const dead = [
410
+ ...this.replayable.keys(),
411
+ ].filter((path) => (this.live.get(path) ?? -1) >= seq);
412
+ await this.prune(dead);
413
+ for (const path of dead) {
414
+ this.replayable.delete(path);
415
+ }
416
+ return dead.length;
417
+ }
350
418
  /**
351
419
  * Drop every positional entry. A host calls this once a run has finished and the
352
420
  * checkpoint holds its result: every later resume starts past them, so they are dead.
@@ -362,6 +430,29 @@ export class StepLedger {
362
430
  }
363
431
  }
364
432
  }
433
+ /** `entry` carrying every connection noted at its path or beneath it. */
434
+ withConnections(entry) {
435
+ const uses = [
436
+ ...(entry.connections ?? []),
437
+ ];
438
+ for (const [path, notes] of this.connectionNotes) {
439
+ if (path !== entry.path && !isBeneath(path, entry.path)) {
440
+ continue;
441
+ }
442
+ for (const use of notes) {
443
+ if (!uses.some((u) => sameUse(u, use))) {
444
+ uses.push(use);
445
+ }
446
+ }
447
+ }
448
+ if (uses.length === 0) {
449
+ return entry;
450
+ }
451
+ return {
452
+ ...entry,
453
+ connections: uses,
454
+ };
455
+ }
365
456
  async prune(dead) {
366
457
  for (const path of dead) {
367
458
  const seq = this.live.get(path);
@@ -425,6 +516,9 @@ export class StepLedger {
425
516
  console.warn(`${message} The step will re-run on resume; further drops are silent.`);
426
517
  }
427
518
  }
519
+ function sameUse(a, b) {
520
+ return a.name === b.name && a.surface === b.surface && a.instance === b.instance;
521
+ }
428
522
  function matches(entry, step) {
429
523
  if (entry.stepId !== step.id || entry.kind !== step.kind) {
430
524
  return false;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The default artifact store (spec 08 § Artifact Store): content-addressed
3
+ * bytes in memory, linked to runs, with per-run retention. Used when
4
+ * `environment.artifacts` is unset.
5
+ *
6
+ * - Identical content is stored once (`artifact://sha256/<hex>`).
7
+ * - When a run exceeds a cap, its oldest unpinned blobs are dropped and the
8
+ * drop is recorded on the run's manifest (`state: 'dropped'`). When pinned
9
+ * blobs alone exceed a cap, new blobs are still stored and marked `overCap`.
10
+ * - `clearRun` removes one run's links; content no run links to is removed.
11
+ */
12
+ import type { ArtifactRetention, ArtifactStore } from '@noetic-tools/types';
13
+ /** Options for `createInMemoryArtifactStore`. */
14
+ export interface InMemoryArtifactStoreOptions {
15
+ readonly retention?: Partial<ArtifactRetention>;
16
+ /** Clock for `attachedAt` and `ttlMs`; default `Date.now`. */
17
+ readonly now?: () => number;
18
+ }
19
+ /**
20
+ * @public
21
+ * An `ArtifactStore` that keeps content in memory. The harness uses one when
22
+ * `environment.artifacts` is unset.
23
+ */
24
+ export declare function createInMemoryArtifactStore(options?: InMemoryArtifactStoreOptions): ArtifactStore;