@nylorun/core 0.22.0-beta → 0.23.0-beta

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 (73) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/README.md +1 -1
  3. package/dist/compatibility.d.ts +32 -16
  4. package/dist/compatibility.js +33 -17
  5. package/dist/contracts.d.ts +531 -88
  6. package/dist/contracts.js +297 -199
  7. package/dist/define.d.ts +8 -12
  8. package/dist/define.js +7 -11
  9. package/dist/definition/assemble.d.ts +1 -3
  10. package/dist/definition/assemble.js +4 -8
  11. package/dist/definition/bound.d.ts +1 -1
  12. package/dist/definition/delegate.d.ts +5 -0
  13. package/dist/definition/delegate.js +6 -1
  14. package/dist/definition/from.d.ts +4 -2
  15. package/dist/definition/from.js +11 -29
  16. package/dist/definition/http-tool.d.ts +5 -3
  17. package/dist/definition/http-tool.js +8 -5
  18. package/dist/definition/manifest.d.ts +7 -6
  19. package/dist/definition/manifest.js +7 -6
  20. package/dist/definition/mcp.d.ts +1 -1
  21. package/dist/definition/media-reference.d.ts +2 -11
  22. package/dist/definition/media-reference.js +0 -12
  23. package/dist/definition/model-failure.js +1 -0
  24. package/dist/definition/removed.js +1 -0
  25. package/dist/definition/trace.d.ts +0 -7
  26. package/dist/definition/trace.js +0 -34
  27. package/dist/definition/variant.d.ts +1 -1
  28. package/dist/definition/variant.js +1 -3
  29. package/dist/harness-api/artifacts.d.ts +84 -0
  30. package/dist/harness-api/artifacts.js +128 -0
  31. package/dist/harness-api/gates.d.ts +0 -4
  32. package/dist/harness-api/gates.js +0 -4
  33. package/dist/harness-api/harness-api.schema.json +2422 -924
  34. package/dist/harness-api/index.d.ts +9 -6
  35. package/dist/harness-api/index.js +9 -6
  36. package/dist/harness-api/json-schema.js +5 -2
  37. package/dist/harness-api/mcp.d.ts +77 -30
  38. package/dist/harness-api/mcp.js +42 -25
  39. package/dist/harness-api/messages.d.ts +45 -17
  40. package/dist/harness-api/messages.js +20 -5
  41. package/dist/harness-api/schema.d.ts +538 -198
  42. package/dist/harness-api/schema.js +298 -59
  43. package/dist/harness-api/transcript.d.ts +8 -10
  44. package/dist/harness-api/transcript.js +27 -11
  45. package/dist/harness-api/transport.d.ts +7 -1
  46. package/dist/harness-api/transport.js +10 -7
  47. package/dist/schemas/agent-manifest.schema.json +5 -146
  48. package/dist/schemas/environment-manifest.schema.json +56 -16
  49. package/dist/types/manifest.d.ts +3 -13
  50. package/dist/types/model.d.ts +10 -5
  51. package/dist/utils/definition-files.d.ts +20 -6
  52. package/dist/utils/definition-files.js +31 -17
  53. package/dist/utils/file-systems.d.ts +20 -0
  54. package/dist/utils/file-systems.js +20 -0
  55. package/dist/utils/harness-id.d.ts +4 -1
  56. package/dist/utils/harness-id.js +4 -1
  57. package/dist/utils/media-types.d.ts +30 -0
  58. package/dist/utils/media-types.js +128 -0
  59. package/dist/utils/runtime-limits.d.ts +229 -0
  60. package/dist/utils/runtime-limits.js +482 -0
  61. package/dist/utils/runtime-tools.d.ts +8 -16
  62. package/dist/utils/runtime-tools.js +8 -27
  63. package/dist/utils/sandbox.d.ts +4 -1
  64. package/dist/utils/sandbox.js +4 -1
  65. package/package.json +1 -1
  66. package/dist/definition/deferred-tools.d.ts +0 -27
  67. package/dist/definition/deferred-tools.js +0 -82
  68. package/dist/definition/runtime-manifest.d.ts +0 -30
  69. package/dist/definition/runtime-manifest.js +0 -132
  70. package/dist/definition/sandbox-tools.d.ts +0 -5
  71. package/dist/definition/sandbox-tools.js +0 -109
  72. package/dist/definition/skill-tools.d.ts +0 -23
  73. package/dist/definition/skill-tools.js +0 -76
@@ -7,17 +7,40 @@
7
7
  import { z } from "zod";
8
8
  import { ABORT_REASONS, HARNESS_API_VERSION, HARNESS_CLAIMS, HARNESS_ERROR_CODES, } from "./messages.js";
9
9
  import { HarnessApiError } from "./errors.js";
10
- import { EffectOutcomeSchema } from "../contracts.js";
10
+ import { ARTIFACT_NAME_MAX, ENVIRONMENT_KINDS, EffectOutcomeSchema, ModelInputSchema } from "../contracts.js";
11
+ import { HarnessLimitsSchema, RUNTIME_LIMIT_CEILINGS } from "../utils/runtime-limits.js";
11
12
  import { SANDBOX_TOOL_NAMES } from "../utils/sandbox.js";
12
- import { McpSnapshotSchema } from "./mcp.js";
13
+ import { McpFactsSchema, McpSnapshotSchema } from "./mcp.js";
14
+ /** An id in a Harness API frame, at most 512 characters: an identifier's grammar is the protocol's (it crosses the Runtime API, the Harness API and tokens, and means the same in every installation), so its length is a constant of protocol 15, not a setting. */
13
15
  const id = z.string().min(1).max(512);
16
+ const hex64 = z.string().regex(/^[0-9a-f]{64}$/);
14
17
  const runId = z.object({ runId: id });
15
18
  /** An engine document (a manifest, a checkpoint): an object here, which the engine checks. */
16
19
  const document = z.record(z.string(), z.unknown());
17
- /** The payload of one `transcript.updated` event. */
18
- export const TranscriptUpdateSchema = z
19
- .object({ keep: z.number().int().min(0), entries: z.array(z.unknown()), length: z.number().int().min(0) })
20
+ const edit = { keep: z.number().int().min(0), entries: z.array(z.unknown()), length: z.number().int().min(0) };
21
+ /**
22
+ * What a harness says of a compaction it made (protocol 15): why, its estimated tokens before and
23
+ * after, and the model calls of this turn that wrote the summary (`purpose: "summary"`), in
24
+ * order. Core checks them and writes one `context.compacted` from them and the last call.
25
+ */
26
+ export const CompactionFactsSchema = z
27
+ .object({
28
+ trigger: z.enum(["threshold", "overflow"]),
29
+ tokensBefore: z.number().int().min(0),
30
+ tokensAfter: z.number().int().min(0),
31
+ summaryEffectIds: z.array(id).min(1),
32
+ })
20
33
  .strict();
34
+ /**
35
+ * The payload of one `transcript.updated` event: keep the first `keep` entries, append `entries`,
36
+ * giving `length` entries. A harness that replaced older history with a summary marks the edit
37
+ * `kind: "compaction"` with its facts (the first chunk of the edit; the record never parses the
38
+ * entries to find one).
39
+ */
40
+ export const TranscriptUpdateSchema = z.union([
41
+ z.object(edit).strict(),
42
+ z.object({ kind: z.literal("compaction"), ...edit, compaction: CompactionFactsSchema }).strict(),
43
+ ]);
21
44
  export const FrameSchema = z.union([
22
45
  z.object({ t: z.literal("req"), id: z.number().int().positive(), m: z.string().min(1), p: z.unknown() }).strict(),
23
46
  z.object({ t: z.literal("res"), id: z.number().int().positive(), ok: z.literal(true), r: z.unknown() }).strict(),
@@ -33,12 +56,51 @@ export const FrameSchema = z.union([
33
56
  z.object({ t: z.literal("abort"), id: z.number().int().positive() }).strict(),
34
57
  z.object({ t: z.literal("ping") }).strict(),
35
58
  ]);
59
+ /**
60
+ * What a tool call runs (protocol 15). The record routes a call by its target, never by the
61
+ * tool's name: the name the model used (`toolName`) is for display, and no name is reserved.
62
+ *
63
+ * - `mcp`: the tool `tool` (its own name on the server) of the MCP server `server` that the call's
64
+ * agent declares (`effect.agent`, absent for the root), as the session discovered it
65
+ * (`turn.start.mcp`); the harness calls it through the Tool Gate. The record refuses one the
66
+ * session did not discover (`tool.unknown`) or the turn disables (`tool.disabled`).
67
+ * - `http`: the HTTP tool the agent declares as `tool`; the harness calls it through the Tool Gate.
68
+ * - `sandbox`: one of the sandbox operations (`bash`, `read`, `write`, `edit`, `grep`, `glob`,
69
+ * named by `toolName`) in the session's sandbox, which the harness runs. Only a session with a
70
+ * sandbox runs one.
71
+ * - `harness`: a tool the harness runs in its own process over the Harness API's seams, such as
72
+ * our engine's `load_skill`, `read_artifact` and `tool_search`. Its egress gate is its only
73
+ * control.
74
+ *
75
+ * Tool policy and approval key on the identity (`server` and `tool`, or the HTTP tool's declared
76
+ * name), never on the model's name. A tool call without a target, or with one the session lacks,
77
+ * fails to the model (`tool.unavailable`), and nothing runs.
78
+ */
79
+ export const EffectTargetSchema = z.discriminatedUnion("kind", [
80
+ z.object({ kind: z.literal("mcp"), server: z.string().min(1), tool: z.string().min(1) }).strict(),
81
+ z.object({ kind: z.literal("http"), tool: z.string().min(1) }).strict(),
82
+ z.object({ kind: z.literal("sandbox") }).strict(),
83
+ z.object({ kind: z.literal("harness") }).strict(),
84
+ ]);
36
85
  /**
37
86
  * One effect the engine asks for: a `HostEffect`, by structure. Model intents carry no `input`.
38
87
  * `context` carries the ids the record derives its events from: a model effect's
39
88
  * `invocationId` (the step), and a tool effect's `callId` (the model's tool call id) and
40
89
  * `invocationId`; without them the call is journaled but no `tool.completed` is written. A
41
- * tool effect may name its step (`stepId`), which shares one result budget among its calls.
90
+ * tool effect may name its step (`stepId`).
91
+ *
92
+ * A model effect that writes a compaction's summary says so (`purpose: "summary"`): it is no
93
+ * assistant message, and the compaction edit that follows names it (`CompactionFactsSchema`).
94
+ *
95
+ * An agent's tool effect names its `target` (what runs it: for an MCP or HTTP tool, its
96
+ * identity) beside the name the model used (`toolName`, for display), and may name the `skill` it
97
+ * ran (a skill tool's skill, or a sandbox call in a skill's folder): the record keeps the skill,
98
+ * and the plugin that brought it, when the agent has that skill. The target is in the request
99
+ * hash, so a replay that names another tool is `effect_drift`.
100
+ *
101
+ * Each outcome the harness reports is one record entry: past `turn.start.limits.recordEntryBytes`
102
+ * it is refused (`limit_exceeded`), so a harness stores a large tool result with
103
+ * `artifact.write` and reports a preview.
42
104
  *
43
105
  * `requestHash` (`effectRequestHash`) covers the whole request: for a model effect, the intent
44
106
  * with `input` set to its `ModelCall`, though the intent is sent without it. A replay that asks
@@ -54,6 +116,10 @@ export const EffectIntentSchema = z
54
116
  kind: z.enum(["model", "tool", "delegation", "agent"]),
55
117
  agent: z.object({ id: z.string(), path: z.string(), delegationId: z.string().optional() }).strict().optional(),
56
118
  toolName: z.string().optional(),
119
+ target: EffectTargetSchema.optional(),
120
+ skill: z.string().min(1).optional(),
121
+ /** A model call that summarizes history for a compaction: the record writes no `message.assistant` for it. */
122
+ purpose: z.literal("summary").optional(),
57
123
  path: z.string().optional(),
58
124
  key: z.string().optional(),
59
125
  iterations: z.string().optional(),
@@ -65,10 +131,27 @@ export const EffectIntentSchema = z
65
131
  export const RecordedOutcomeSchema = z
66
132
  .object({ effectId: id, requestHash: z.string(), outcome: EffectOutcomeSchema })
67
133
  .strict();
134
+ /**
135
+ * An artifact as the artifact seam shows it to a harness: one version of a file (or a folder,
136
+ * whose bytes the seam does not serve). `tenantWide` marks a Tenant-wide artifact (no session),
137
+ * which the run reads only because the session's messages named it.
138
+ */
139
+ export const ArtifactMetaSchema = z
140
+ .object({
141
+ artifactId: id,
142
+ kind: z.enum(["file", "folder"]),
143
+ name: z.string(),
144
+ version: z.number().int().min(1),
145
+ contentType: z.string(),
146
+ size: z.number().int().nonnegative(),
147
+ sha256: z.string().regex(/^[0-9a-f]{64}$/),
148
+ tenantWide: z.boolean(),
149
+ })
150
+ .strict();
68
151
  /** A workspace's compute record, as the harness keeps it. */
69
152
  export const WorkspaceRecordSchema = z
70
153
  .object({
71
- key: z.string().min(1).max(512),
154
+ key: id,
72
155
  sessionId: id,
73
156
  environmentId: id.optional(),
74
157
  backend: z.string(),
@@ -97,30 +180,6 @@ export const WorkspaceCallSchema = z
97
180
  input: z.unknown(),
98
181
  })
99
182
  .strict();
100
- /**
101
- * A file of a workspace read as bytes (`save_artifact`, F8.1), at most `maxBytes`. The answer is
102
- * `{kind: "read", path, base64}`, `{kind: "missing", path}` or a failed tool outcome.
103
- */
104
- export const WorkspaceBytesCallSchema = z
105
- .object({
106
- session: WorkspaceSessionSchema,
107
- spec: z.unknown(),
108
- bytes: z.object({ path: z.string().min(1), maxBytes: z.number().int().positive() }).strict(),
109
- })
110
- .strict();
111
- /**
112
- * The regular files under a directory of a workspace, recursively, at most `maxEntries + 1`
113
- * (the turn-end export, F8.2). The answer is `{kind: "listed", path, listing: {entries,
114
- * truncated}}`, `{kind: "missing", path}` (no sandbox yet, or not a directory) or a failed tool
115
- * outcome.
116
- */
117
- export const WorkspaceListCallSchema = z
118
- .object({
119
- session: WorkspaceSessionSchema,
120
- spec: z.unknown(),
121
- list: z.object({ dir: z.string().min(1), maxEntries: z.number().int().positive() }).strict(),
122
- })
123
- .strict();
124
183
  /**
125
184
  * One operation on the disk of a workspace (R2d step 3): `list` its entries (files with mode and
126
185
  * modification time, links, empty directories; paths relative to the volume, `workspace/…` and
@@ -136,7 +195,7 @@ export const DiskOpSchema = z.union([
136
195
  .object({
137
196
  path: z.string().min(1),
138
197
  offset: z.number().int().nonnegative(),
139
- length: z.number().int().positive().max(16 * 1024 * 1024),
198
+ length: z.number().int().positive().max(RUNTIME_LIMIT_CEILINGS.chunkBytes.value),
140
199
  })
141
200
  .strict(),
142
201
  })
@@ -177,9 +236,11 @@ export const RunRoutingSchema = z
177
236
  .object({
178
237
  /** The session's pinned manifest: an effect's agent resolves against it. */
179
238
  rootManifest: document,
180
- /** The session's MCP snapshot, once its first run recorded one (`session.mcp`). */
181
- mcpSnapshot: McpSnapshotSchema.optional(),
182
- /** The session's sandbox: the session, and the Environment whose sandbox it is. */
239
+ /**
240
+ * The session's sandbox: the session, and the Environment whose sandbox it is. `spec` is the
241
+ * sandbox spec the session pinned when it opened (`SandboxManifest`); absent, the session has
242
+ * no sandbox, and a `sandbox` call fails.
243
+ */
183
244
  sandbox: z
184
245
  .object({
185
246
  ownerId: id,
@@ -187,7 +248,7 @@ export const RunRoutingSchema = z
187
248
  spec: z.unknown().optional(),
188
249
  /**
189
250
  * Variables every sandbox command gets (R2c, D50): each `environment_secret` name set to
190
- * the sentinel `nylorun-managed`, and each `environment_variable`'s value. Never a secret.
251
+ * the sentinel `nylorun_managed`, and each `environment_variable`'s value. Never a secret.
191
252
  */
192
253
  environment: z.record(z.string(), z.string()).optional(),
193
254
  })
@@ -201,7 +262,7 @@ export const RunRoutingSchema = z
201
262
  * metadata?}` with user content parts, `{kind: "continue"}` for the next segment of a yielded
202
263
  * turn, or an approval or response event. Effect ids are prefixed `<turnId>:<segment>:`
203
264
  * (`effectIdOf`), and every intent carries `manifestHash`. `state` is the harness's own, as its
204
- * last output left it: core stores it unread (at most `MAX_HARNESS_STATE_BYTES` for a harness
265
+ * last output left it: core stores it unread (at most `limits.harnessStateBytes` for a harness
205
266
  * other than ours) and folds the transcript every segment, whatever the state holds. A model
206
267
  * call's `executionId` is the session id.
207
268
  */
@@ -221,20 +282,34 @@ export const TraceContextSchema = z
221
282
  })
222
283
  .strict();
223
284
  /**
224
- * A tool a harness runs itself (R4.2, Q3), declared in `hello`: the model may call it, and the
225
- * harness journals each call as a `tool` effect with intent and outcome, like a sandbox tool, so
226
- * the log records what really ran. Its name may not be one the agent or the Runtime already uses. It runs in
227
- * the harness's own process or pod: the egress gate is its only control.
285
+ * The model a turn's calls use when they name none (protocol 15): the Tenant's model, as the
286
+ * Model Gate resolves it. `provider` and `id` name it (`fixture` for the fixture model); `inputs`
287
+ * is what it reads, text first, in `MODEL_INPUTS` order (its catalog's inputs and the operator's
288
+ * additions); `contextWindow` and `maxOutputTokens` are in tokens. A harness decides from it how
289
+ * each file reaches the model; the gate refuses a part the model cannot take
290
+ * (`unsupported_input`).
291
+ */
292
+ export const ModelDescriptorSchema = z
293
+ .object({
294
+ provider: z.string().min(1),
295
+ id: z.string().min(1),
296
+ inputs: z.array(ModelInputSchema).min(1),
297
+ contextWindow: z.number().int().positive(),
298
+ maxOutputTokens: z.number().int().positive(),
299
+ reasoning: z.boolean().optional(),
300
+ })
301
+ .strict();
302
+ /**
303
+ * The Environment a turn runs in (protocol 15): its id and kind and, when the session has a
304
+ * sandbox, the workspace its commands start in. A harness writes the model's instructions and
305
+ * defines its sandbox tools from it; the sandbox's spec is the routing's (`routing.sandbox.spec`).
228
306
  */
229
- export const NativeToolSchema = z
307
+ export const TurnEnvironmentSchema = z
230
308
  .object({
231
- // `__` names an MCP server's tools, which are discovered after a turn starts.
232
- name: z
233
- .string()
234
- .regex(/^[A-Za-z_][A-Za-z0-9_-]{0,63}$/)
235
- .refine((name) => !name.includes("__"), { message: "A harness's tool name must not contain '__'" }),
236
- description: z.string().max(4096).optional(),
237
- inputSchema: z.record(z.string(), z.unknown()),
309
+ id,
310
+ kind: z.enum(ENVIRONMENT_KINDS),
311
+ /** Present when the session has a sandbox. */
312
+ sandbox: z.object({ workspace: z.string().regex(/^\//) }).strict().optional(),
238
313
  })
239
314
  .strict();
240
315
  /** What a run starts from: `turn.start` (message, continue, resume) or `approval.answer`. */
@@ -246,7 +321,13 @@ export const TurnStartSchema = z
246
321
  manifest: document,
247
322
  /** The segment's checkpoint; an agent's state has no transcript (`transcript.cursor`). */
248
323
  checkpoint: document,
249
- sessionTools: z.array(z.unknown()).readonly().optional(),
324
+ /**
325
+ * The session's MCP discovery as this turn sees it (protocol 15), once its first run recorded
326
+ * it (`session.mcp`): each server of the agent tree that connected, its instructions and its
327
+ * tools, raw, with whether the turn's manifest leaves each `enabled`. The harness names the
328
+ * tools for the model and calls one by its target `{kind: "mcp", server, tool}`.
329
+ */
330
+ mcp: McpFactsSchema.optional(),
250
331
  /** Completed outcomes of this segment, resolved without asking core. */
251
332
  outcomes: z.array(RecordedOutcomeSchema).readonly(),
252
333
  /** The record position the session's transcript was folded at. */
@@ -269,6 +350,24 @@ export const TurnStartSchema = z
269
350
  * spans join the turn's trace. The Runtime never ingests a harness's spans.
270
351
  */
271
352
  trace: TraceContextSchema.optional(),
353
+ /**
354
+ * The Runtime limits in force when the segment started (the Tenant's, `PUT
355
+ * /v1/tenant/limits`): what one record entry (an effect outcome, a claimed event, a transcript
356
+ * update), model call, model file, tool answer, custom harness state, Harness API chunk and
357
+ * folder artifact may hold. Past one, core refuses the request with `limit_exceeded`. Only
358
+ * the limits a harness must respect (`HarnessLimitsSchema`); one it does not know is dropped.
359
+ */
360
+ limits: HarnessLimitsSchema,
361
+ /**
362
+ * The model the turn's calls use when they name none (protocol 15): what it reads and its
363
+ * window, as the segment started. Absent for a flow, and when the Tenant has no model.
364
+ */
365
+ model: ModelDescriptorSchema.optional(),
366
+ /**
367
+ * The Environment the session runs in (protocol 15): what our engine writes the sandbox notes
368
+ * and defines the sandbox tools from. Absent when the session names none, and for a flow.
369
+ */
370
+ environment: TurnEnvironmentSchema.optional(),
272
371
  })
273
372
  .strict();
274
373
  /**
@@ -333,9 +432,7 @@ export const harnessRequests = {
333
432
  api: z.number().int(),
334
433
  name: z.string(),
335
434
  version: z.string(),
336
- features: z
337
- .object({ workspace: z.unknown().optional(), tools: z.array(NativeToolSchema).max(64).optional() })
338
- .strict(),
435
+ features: z.object({ workspace: z.unknown().optional() }).strict(),
339
436
  })
340
437
  .strict(),
341
438
  result: z
@@ -393,16 +490,159 @@ export const harnessRequests = {
393
490
  },
394
491
  "session.mcp": {
395
492
  params: runId.extend({ snapshot: McpSnapshotSchema.optional(), diagnostics: z.array(z.unknown()) }).strict(),
396
- result: z.object({ snapshot: McpSnapshotSchema.optional(), sessionTools: z.array(z.unknown()) }).strict(),
493
+ result: z.object({ mcp: McpFactsSchema.optional() }).strict(),
397
494
  },
398
495
  /**
399
496
  * A definition file's bytes, base64 (track R2 M4): one the run's definition names, for the
400
- * skills its sandbox mounts.
497
+ * skills its sandbox mounts. With `offset` and `length` (at most `turn.start.limits.chunkBytes`,
498
+ * its default without `length`; more is `limit_exceeded`), only those bytes: fewer than
499
+ * `length`, none past the end, once the file ends. Without them the whole file, which must then
500
+ * fit one frame.
401
501
  */
402
502
  "definition.file": {
403
- params: runId.extend({ sha256: z.string().regex(/^sha256:[0-9a-f]{64}$/) }).strict(),
503
+ params: runId
504
+ .extend({
505
+ sha256: z.string().regex(/^sha256:[0-9a-f]{64}$/),
506
+ offset: z.number().int().nonnegative().optional(),
507
+ length: z.number().int().positive().max(RUNTIME_LIMIT_CEILINGS.chunkBytes.value).optional(),
508
+ })
509
+ .strict(),
404
510
  result: z.object({ base64: z.string() }).strict(),
405
511
  },
512
+ /**
513
+ * An artifact the run's session may read (the artifact seam, protocol 15): one of the session's
514
+ * own, or a Tenant-wide one its messages named; anything else is `not_found`, alike. The answer
515
+ * carries the version's metadata (`version`, default the latest) and its bytes from `offset`
516
+ * (default 0), base64: at most `length`, which defaults to and is held to
517
+ * `turn.start.limits.chunkBytes`; `length: 0` asks for the metadata alone. `eof` says no bytes
518
+ * are left past this chunk. A folder answers its metadata, with no bytes.
519
+ */
520
+ "artifact.read": {
521
+ params: runId
522
+ .extend({
523
+ artifactId: id,
524
+ version: z.number().int().min(1).optional(),
525
+ offset: z.number().int().nonnegative().optional(),
526
+ length: z.number().int().nonnegative().optional(),
527
+ })
528
+ .strict(),
529
+ result: z
530
+ .object({
531
+ artifact: ArtifactMetaSchema,
532
+ offset: z.number().int().nonnegative(),
533
+ base64: z.string(),
534
+ eof: z.boolean(),
535
+ })
536
+ .strict(),
537
+ },
538
+ /**
539
+ * Writes a file artifact of the run's session, or a new version of one of its own (the artifact
540
+ * seam, protocol 15), streamed in chunks of at most `turn.start.limits.chunkBytes`:
541
+ *
542
+ * - `begin` opens an upload, answered `open` with its `uploadId` and the most bytes it may hold
543
+ * now (`maxBytes`: the file limit, or the room left in a total). `key` makes the write
544
+ * idempotent within the session: a key already written answers `written` with `created:
545
+ * false` and the version it wrote. With `base64`, the whole body comes in the `begin`, which
546
+ * then commits at once. `effectId` links the artifact to a tool call of the run's turn, whose
547
+ * call id its `artifact.created` event carries.
548
+ * - `chunk` sends the bytes from `offset`, which must equal the bytes received so far; a repeat
549
+ * of the last chunk is answered as before.
550
+ * - `commit` stores the version (`written`); `abort` drops the upload. An upload the run leaves
551
+ * open ends with the run.
552
+ *
553
+ * With `kind: "folder"`, `begin` opens a folder version instead: a new version of the session's
554
+ * folder `artifactId`, or with `sameName` of its newest folder named `name` (made when there is
555
+ * none), or else a new folder. Its answer adds `maxFileBytes` (one file's limit) and
556
+ * `maxEntries` (`limits.folderEntries`); `maxBytes` is the most its files may hold together.
557
+ * Each file starts with `file` (its path in the folder, size and SHA-256, the whole body in
558
+ * `base64` when it fits one chunk), and goes on with `chunk`s naming it by `sha256`, from
559
+ * `offset` within the file. Core always takes every byte, stored or not (it holds content once,
560
+ * by hash), and refuses a file whose bytes do not match its hash (`invalid`; the upload ends).
561
+ * `commit` answers `written` with `unchanged: true` and no new version when the files are the
562
+ * folder's latest version's, and `refused` when a total would be passed.
563
+ */
564
+ "artifact.write": {
565
+ params: z.union([
566
+ runId
567
+ .extend({
568
+ op: z.literal("begin"),
569
+ key: id,
570
+ artifactId: id.optional(),
571
+ name: z.string().min(1).max(ARTIFACT_NAME_MAX).optional(),
572
+ contentType: z.string().min(1).optional(),
573
+ size: z.number().int().nonnegative().optional(),
574
+ effectId: id.optional(),
575
+ base64: z.string().optional(),
576
+ })
577
+ .strict(),
578
+ runId
579
+ .extend({
580
+ op: z.literal("begin"),
581
+ kind: z.literal("folder"),
582
+ key: id,
583
+ name: z.string().min(1).max(ARTIFACT_NAME_MAX),
584
+ artifactId: id.optional(),
585
+ sameName: z.literal(true).optional(),
586
+ })
587
+ .strict(),
588
+ runId
589
+ .extend({
590
+ op: z.literal("file"),
591
+ uploadId: id,
592
+ path: z.string().min(1),
593
+ size: z.number().int().nonnegative(),
594
+ sha256: hex64,
595
+ contentType: z.string().min(1).optional(),
596
+ base64: z.string().optional(),
597
+ })
598
+ .strict(),
599
+ runId
600
+ .extend({
601
+ op: z.literal("chunk"),
602
+ uploadId: id,
603
+ /** A folder upload's file this chunk continues. */
604
+ sha256: hex64.optional(),
605
+ offset: z.number().int().nonnegative(),
606
+ base64: z.string().min(1),
607
+ })
608
+ .strict(),
609
+ runId.extend({ op: z.literal("commit"), uploadId: id }).strict(),
610
+ runId.extend({ op: z.literal("abort"), uploadId: id }).strict(),
611
+ ]),
612
+ result: z.union([
613
+ z
614
+ .object({
615
+ status: z.literal("open"),
616
+ uploadId: id,
617
+ maxBytes: z.number().int().nonnegative(),
618
+ /** A folder upload: one file's limit, and the most files it may hold. */
619
+ maxFileBytes: z.number().int().nonnegative().optional(),
620
+ maxEntries: z.number().int().positive().optional(),
621
+ /** Bytes received: of the upload, or of a folder upload's file in progress. */
622
+ received: z.number().int().nonnegative(),
623
+ })
624
+ .strict(),
625
+ z
626
+ .object({
627
+ status: z.literal("written"),
628
+ artifact: ArtifactMetaSchema,
629
+ created: z.boolean(),
630
+ /** A folder whose files are its latest version's: no version was added. */
631
+ unchanged: z.literal(true).optional(),
632
+ })
633
+ .strict(),
634
+ z
635
+ .object({
636
+ status: z.literal("refused"),
637
+ /** The total a folder version would pass: the Tenant's, or the session's Environment's. */
638
+ reason: z.enum(["tenant_total", "environment_total"]),
639
+ message: z.string(),
640
+ limit: z.number().int().nonnegative(),
641
+ })
642
+ .strict(),
643
+ z.object({ status: z.literal("aborted") }).strict(),
644
+ ]),
645
+ },
406
646
  "turn.completed": { params: TurnOutputSchema, result: settled },
407
647
  "turn.paused": { params: TurnOutputSchema, result: settled },
408
648
  "turn.waiting": { params: TurnOutputSchema, result: settled },
@@ -413,12 +653,11 @@ export const harnessRequests = {
413
653
  /**
414
654
  * Requests core sends to a harness that declared `workspace` (F6.2), with their answers. A
415
655
  * sandbox tool's answer is its `SandboxToolOutcome`; the harness claims its `environment.*` events
416
- * while the request is in flight. `workspace.read` stays generic: F8.2 exports outputs
417
- * through it.
656
+ * while the request is in flight.
418
657
  */
419
658
  export const coreRequests = {
420
659
  "workspace.read": {
421
- params: z.union([WorkspaceCallSchema, WorkspaceBytesCallSchema, WorkspaceListCallSchema, WorkspaceDiskCallSchema]),
660
+ params: z.union([WorkspaceCallSchema, WorkspaceDiskCallSchema]),
422
661
  result: outcomeObject,
423
662
  },
424
663
  "workspace.write": { params: z.union([WorkspaceCallSchema, WorkspaceDiskCallSchema]), result: outcomeObject },
@@ -4,15 +4,9 @@
4
4
  * records and folds them, and a harness, which computes them and keeps its cache with them.
5
5
  */
6
6
  import type { z } from "zod";
7
- import type { TranscriptUpdateSchema } from "./schema.js";
7
+ import type { CompactionFactsSchema, TranscriptUpdateSchema } from "./schema.js";
8
8
  /** The payload of one `transcript.updated` event. */
9
9
  export type TranscriptUpdate = z.infer<typeof TranscriptUpdateSchema>;
10
- /**
11
- * An edit's entries are split so each event stays well under S2's 1 MiB record limit: under
12
- * 64 KiB when no entry is larger, as a step with one tool result is once the result fits its
13
- * 32 KiB cap (R2b C11). An entry larger than this is an event of its own.
14
- */
15
- export declare const CHUNK_BYTES: number;
16
10
  export declare class TranscriptFoldError extends Error {
17
11
  constructor(message: string);
18
12
  }
@@ -21,10 +15,14 @@ export declare function transcriptOf(state: unknown): unknown[];
21
15
  /** `state` with `transcript` (what the engine runs on). */
22
16
  export declare function withTranscript<T>(state: T, transcript: readonly unknown[]): T;
23
17
  /**
24
- * The edits from `before` to `after`, split into chunks of at most `CHUNK_BYTES` of entries.
25
- * Empty when nothing changed.
18
+ * The edits from `before` to `after`, each one `transcript.updated` event: an edit's entries fill
19
+ * each event up to `recordEntryBytes` (the run's `turn.start.limits`), the most one record entry
20
+ * holds, with room for the edit's own fields. An entry larger than that is an event of its own,
21
+ * which the record refuses (`limit_exceeded`). Empty when nothing changed. With `compaction`, the
22
+ * edit replaced older history with a summary: its first event says so (`kind: "compaction"`,
23
+ * with the facts).
26
24
  */
27
- export declare function transcriptUpdates(before: readonly unknown[], after: readonly unknown[], chunkBytes?: number): TranscriptUpdate[];
25
+ export declare function transcriptUpdates(before: readonly unknown[], after: readonly unknown[], recordEntryBytes: number, compaction?: z.infer<typeof CompactionFactsSchema>): TranscriptUpdate[];
28
26
  /** Applies one edit. Throws `TranscriptFoldError` when it does not fit the transcript. */
29
27
  export declare function applyUpdate(transcript: readonly unknown[], update: TranscriptUpdate): unknown[];
30
28
  /** Applies edits in order. */
@@ -1,9 +1,3 @@
1
- /**
2
- * An edit's entries are split so each event stays well under S2's 1 MiB record limit: under
3
- * 64 KiB when no entry is larger, as a step with one tool result is once the result fits its
4
- * 32 KiB cap (R2b C11). An entry larger than this is an event of its own.
5
- */
6
- export const CHUNK_BYTES = 48 * 1024;
7
1
  export class TranscriptFoldError extends Error {
8
2
  constructor(message) {
9
3
  super(message);
@@ -22,11 +16,32 @@ export function withTranscript(state, transcript) {
22
16
  return state;
23
17
  return { ...state, transcript: [...transcript] };
24
18
  }
19
+ /** The bytes of `text` as UTF-8, as the record measures an entry. */
20
+ const utf8Bytes = (text) => new TextEncoder().encode(text).length;
25
21
  /**
26
- * The edits from `before` to `after`, split into chunks of at most `CHUNK_BYTES` of entries.
27
- * Empty when nothing changed.
22
+ * The edits from `before` to `after`, each one `transcript.updated` event: an edit's entries fill
23
+ * each event up to `recordEntryBytes` (the run's `turn.start.limits`), the most one record entry
24
+ * holds, with room for the edit's own fields. An entry larger than that is an event of its own,
25
+ * which the record refuses (`limit_exceeded`). Empty when nothing changed. With `compaction`, the
26
+ * edit replaced older history with a summary: its first event says so (`kind: "compaction"`,
27
+ * with the facts).
28
28
  */
29
- export function transcriptUpdates(before, after, chunkBytes = CHUNK_BYTES) {
29
+ export function transcriptUpdates(before, after, recordEntryBytes, compaction) {
30
+ // The fields beside the entries, at their longest: the first event's, and every other's.
31
+ const fields = utf8Bytes(JSON.stringify({
32
+ kind: "compaction",
33
+ keep: Number.MAX_SAFE_INTEGER,
34
+ entries: [],
35
+ length: Number.MAX_SAFE_INTEGER,
36
+ ...(compaction === undefined ? {} : { compaction }),
37
+ }));
38
+ const updates = edits(before, after, recordEntryBytes - fields);
39
+ if (compaction === undefined || updates.length === 0)
40
+ return updates;
41
+ const [first, ...rest] = updates;
42
+ return [{ kind: "compaction", ...first, compaction }, ...rest];
43
+ }
44
+ function edits(before, after, entryBytes) {
30
45
  let keep = 0;
31
46
  while (keep < before.length && keep < after.length && same(before[keep], after[keep]))
32
47
  keep += 1;
@@ -46,8 +61,9 @@ export function transcriptUpdates(before, after, chunkBytes = CHUNK_BYTES) {
46
61
  bytes = 0;
47
62
  };
48
63
  for (const entry of added) {
49
- const size = JSON.stringify(entry).length;
50
- if (chunk.length > 0 && bytes + size > chunkBytes)
64
+ // The entry's JSON and the comma before the next.
65
+ const size = utf8Bytes(JSON.stringify(entry)) + 1;
66
+ if (chunk.length > 0 && bytes + size > entryBytes)
51
67
  flush();
52
68
  chunk.push(entry);
53
69
  bytes += size;
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * - the transport: path `/nylorun/harness/v1` and header `Nylorun-Harness-Api: 1`, for the
5
5
  * WebSocket and its frames (`FrameSchema`), one JSON text message per frame;
6
- * - the messages: `HARNESS_API_VERSION` (2), which a harness names in `hello.api` and core
6
+ * - the messages: `HARNESS_API_VERSION`, which a harness names in `hello.api` and core
7
7
  * checks;
8
8
  * - the Runtime's protocol, which the Runtime and Management APIs carry, never this one.
9
9
  *
@@ -29,6 +29,12 @@ export declare const HOST_RENEW_PATH = "/nylorun/harness/v1/host/renew";
29
29
  * mounted join file (afresh each time: a reset writes a new one), and the id the harness keeps on
30
30
  * the volume (`/harness/volume-id`), so a volume recreated empty is told apart.
31
31
  */
32
+ /**
33
+ * A pod's UID or a volume id: a Kubernetes UID is an RFC 4122 UUID (36 characters); 64 leaves a
34
+ * volume id room. With the join token's 512, an identifier's grammar is the protocol's (it crosses the Runtime API, the Harness API and tokens, and means the same in every installation), so its length is a constant of protocol 15, not a setting.
35
+ */
36
+ export declare const POD_UID_PATTERN: RegExp;
37
+ export declare const JOIN_TOKEN_MAX = 512;
32
38
  export declare const HostJoinRequestSchema: z.ZodObject<{
33
39
  sandboxId: z.ZodString;
34
40
  podUid: z.ZodString;
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * - the transport: path `/nylorun/harness/v1` and header `Nylorun-Harness-Api: 1`, for the
5
5
  * WebSocket and its frames (`FrameSchema`), one JSON text message per frame;
6
- * - the messages: `HARNESS_API_VERSION` (2), which a harness names in `hello.api` and core
6
+ * - the messages: `HARNESS_API_VERSION`, which a harness names in `hello.api` and core
7
7
  * checks;
8
8
  * - the Runtime's protocol, which the Runtime and Management APIs carry, never this one.
9
9
  *
@@ -29,14 +29,17 @@ export const HOST_RENEW_PATH = `${HARNESS_API_PATH}/host/renew`;
29
29
  * mounted join file (afresh each time: a reset writes a new one), and the id the harness keeps on
30
30
  * the volume (`/harness/volume-id`), so a volume recreated empty is told apart.
31
31
  */
32
+ /**
33
+ * A pod's UID or a volume id: a Kubernetes UID is an RFC 4122 UUID (36 characters); 64 leaves a
34
+ * volume id room. With the join token's 512, an identifier's grammar is the protocol's (it crosses the Runtime API, the Harness API and tokens, and means the same in every installation), so its length is a constant of protocol 15, not a setting.
35
+ */
36
+ export const POD_UID_PATTERN = /^[A-Za-z0-9-]{1,64}$/;
37
+ export const JOIN_TOKEN_MAX = 512;
32
38
  export const HostJoinRequestSchema = z.strictObject({
33
39
  sandboxId: z.string().min(1),
34
- podUid: z.string().regex(/^[A-Za-z0-9-]{1,64}$/),
35
- joinToken: z.string().min(1).max(512),
36
- volumeId: z
37
- .string()
38
- .regex(/^[A-Za-z0-9-]{1,64}$/)
39
- .optional(),
40
+ podUid: z.string().regex(POD_UID_PATTERN),
41
+ joinToken: z.string().min(1).max(JOIN_TOKEN_MAX),
42
+ volumeId: z.string().regex(POD_UID_PATTERN).optional(),
40
43
  });
41
44
  /**
42
45
  * What `host/join` and `host/renew` answer. The host token is the WebSocket bearer; the egress