@yaag/runtime 0.26.0 → 0.28.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 (66) hide show
  1. package/package.json +1 -1
  2. package/src/agent/agent-name.ts +22 -0
  3. package/src/agent/agent-names.ts +5 -1
  4. package/src/agent/agent.ts +34 -17
  5. package/src/agent/define-agent.ts +4 -0
  6. package/src/agent/fork.ts +50 -12
  7. package/src/agent/host-fork.ts +85 -0
  8. package/src/agent/index.ts +1 -0
  9. package/src/agent/spawn-definition-request.ts +41 -6
  10. package/src/agent/spawn-open.ts +54 -107
  11. package/src/agent/spawn-parent.ts +64 -0
  12. package/src/agent/spawn-plan.ts +120 -0
  13. package/src/agent/spawn-request.ts +26 -6
  14. package/src/agent/spawn.ts +47 -6
  15. package/src/ask/ask-exchange-options.ts +3 -6
  16. package/src/ask/ask-exchange.ts +48 -108
  17. package/src/ask/ask-guard.ts +207 -0
  18. package/src/ask/ask-timing.ts +12 -0
  19. package/src/ask/index.ts +1 -0
  20. package/src/cassette/cassette-loader.ts +9 -20
  21. package/src/cassette/cassette-schema.ts +11 -1
  22. package/src/cassette/cassette.ts +16 -34
  23. package/src/cassette/checkpoint.ts +306 -0
  24. package/src/cassette/index.ts +16 -7
  25. package/src/cassette/parked-asks.ts +38 -0
  26. package/src/cassette/replay-divergence.ts +7 -102
  27. package/src/cassette/resume-identity.ts +5 -1
  28. package/src/cassette/resume-preconditions.ts +14 -0
  29. package/src/cassette/resume-transport.ts +26 -32
  30. package/src/cassette/run-identity.ts +109 -0
  31. package/src/cassette/session-answer.ts +51 -114
  32. package/src/cassette/spawn-identity.ts +328 -0
  33. package/src/contract/error-codes.ts +4 -0
  34. package/src/contract/group.ts +38 -0
  35. package/src/contract/index.ts +2 -0
  36. package/src/contract/spawn-options.ts +48 -7
  37. package/src/error-factories.ts +8 -0
  38. package/src/events.ts +22 -2
  39. package/src/index.ts +7 -0
  40. package/src/pi-session/append-tool-result.ts +165 -0
  41. package/src/pi-session/index.ts +30 -0
  42. package/src/pi-session/pi-session-error.ts +34 -0
  43. package/src/pi-session/session-copy.ts +113 -0
  44. package/src/pi-session/session-entries.ts +130 -0
  45. package/src/public-api.ts +2 -0
  46. package/src/question-summary/index.ts +4 -8
  47. package/src/question-summary/summarize-question.ts +1 -1
  48. package/src/question-summary/summary-policy.ts +5 -2
  49. package/src/run/agent-questions.ts +82 -0
  50. package/src/run/answers.ts +22 -1
  51. package/src/run/groups.ts +111 -0
  52. package/src/run/host-session.ts +233 -0
  53. package/src/run/index.ts +8 -0
  54. package/src/run/run-context.ts +15 -0
  55. package/src/run/run.ts +208 -182
  56. package/src/summary/index.ts +1 -0
  57. package/src/summary/summary-agent.ts +6 -0
  58. package/src/summary/summary-group.ts +31 -0
  59. package/src/summary/summary.ts +12 -0
  60. package/src/transport/live-transport.ts +19 -0
  61. package/src/transport/pi-state.ts +10 -1
  62. package/src/transport/transport.ts +20 -9
  63. package/src/types.ts +3 -0
  64. package/src/cassette/checkpoint-flush.ts +0 -101
  65. package/src/cassette/run-checkpoint.ts +0 -272
  66. package/src/question-summary/session-copy.ts +0 -169
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaag/runtime",
3
- "version": "0.26.0",
3
+ "version": "0.28.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -0,0 +1,22 @@
1
+ import { YaagError } from "../errors.ts";
2
+
3
+ /**
4
+ * Refuses an Agent name that forges a path segment.
5
+ *
6
+ * The node path of the TUI and the Group path of ADR-0070 both split on "/"
7
+ * and ":", so a name with either character would read as more than one
8
+ * segment. Nothing is sanitized: the name is refused.
9
+ *
10
+ * This is its own module because `defineAgent` calls it, and the vendored
11
+ * author type tree follows value imports (ADR-0045): the name allocator of
12
+ * `agent-names.ts` must stay out of that tree.
13
+ */
14
+ export function assertAgentName(name: string): void {
15
+ if (name.includes("/") || name.includes(":")) {
16
+ throw new YaagError(
17
+ "AGENT_NAME_INVALID",
18
+ `agent name "${name}" is invalid: a name must not contain "/" or ":"`,
19
+ name,
20
+ );
21
+ }
22
+ }
@@ -1,15 +1,19 @@
1
+ import { assertAgentName } from "./agent-name.ts";
2
+
1
3
  /**
2
4
  * Every Lifecycle Event identifies its Agent, so an unnamed Agent still needs a
3
5
  * name and a duplicate is suffixed rather than rejected — spawning in a loop
4
6
  * must not fail.
5
7
  *
6
- * `taken` is mutated: the chosen name is reserved.
8
+ * `taken` is mutated: the chosen name is reserved. A requested name that holds
9
+ * "/" or ":" is refused with `AGENT_NAME_INVALID` before anything is reserved.
7
10
  */
8
11
  export function uniqueAgentName(
9
12
  requested: string | undefined,
10
13
  ordinal: number,
11
14
  taken: Set<string>,
12
15
  ): string {
16
+ if (requested !== undefined) assertAgentName(requested);
13
17
  const base = requested ?? `a${ordinal + 1}`;
14
18
  let name = base;
15
19
  for (let suffix = 2; taken.has(name); suffix++) {
@@ -1,5 +1,5 @@
1
1
  import type { Static, TSchema } from "typebox";
2
- import type { EffectiveAskOptions } from "../ask/index.ts";
2
+ import type { AskTiming, EffectiveAskOptions } from "../ask/index.ts";
3
3
  import { exchangeAsk } from "../ask/index.ts";
4
4
  import { REPORT_RESULT_WRAP_UP, ReportResultTool, wrapUpMessage } from "../ask-contract/index.ts";
5
5
  import type { AskAdmission, AskInFlight, AskSettled } from "../control/index.ts";
@@ -55,16 +55,14 @@ export interface AgentOptions {
55
55
  readonly askDefaults?: AskOptions;
56
56
  /** Definition identity recorded on its Asks, outside replay identity. */
57
57
  readonly definitionName?: string;
58
- /** Test-only override for the fixed duration-limit grace. */
59
- readonly askLimitGraceMs?: number;
60
- /** Test-only override for the bounded wait for `agent_settled` after an idle abort. */
61
- readonly idleAbortSettleMs?: number;
62
- /** Test-only override for the bounded wait for the Stall Watchdog probe. */
63
- readonly stallProbeSettleMs?: number;
58
+ /** Test-only waits of the Ask Guard's controls; absent in production. */
59
+ readonly timing?: AskTiming;
64
60
  /** Run-scoped Ask gate; a closed gate refuses every later Ask (ADR-0024). */
65
61
  readonly askAdmission?: AskAdmission;
66
62
  /** Run-scoped sink of Agent questions; absent when this Agent cannot ask (ADR-0023). */
67
63
  readonly questions?: AgentQuestions;
64
+ /** The Group this Agent sits in (ADR-0070); a fork inherits it. */
65
+ readonly group?: string;
68
66
  }
69
67
 
70
68
  /** One Agent as an Orchestration Program sees it. Lives above the transport seam. */
@@ -79,9 +77,7 @@ export class Agent implements Handle {
79
77
  readonly #spawnOptions: ResolvedSpawnOptions;
80
78
  readonly #askDefaults: AskOptions;
81
79
  readonly #definitionName: string | undefined;
82
- readonly #askLimitGraceMs: number | undefined;
83
- readonly #idleAbortSettleMs: number | undefined;
84
- readonly #stallProbeSettleMs: number | undefined;
80
+ readonly #timing: AskTiming | undefined;
85
81
  readonly #modelFallback: AgentModelFallback | undefined;
86
82
  #model: string;
87
83
  #candidate: string;
@@ -91,6 +87,7 @@ export class Agent implements Handle {
91
87
  readonly #fork: ForkSpawner | undefined;
92
88
  readonly #askAdmission: AskAdmission | undefined;
93
89
  readonly #questions: AgentQuestions | undefined;
90
+ readonly #group: string | undefined;
94
91
  #busy = false;
95
92
  #inFlight: AskInFlight | null = null;
96
93
  #incomplete = false;
@@ -113,11 +110,10 @@ export class Agent implements Handle {
113
110
  this.#definitionName = options.definitionName;
114
111
  this.#sessionFile = options.sessionFile;
115
112
  this.#fork = options.fork;
116
- this.#askLimitGraceMs = options.askLimitGraceMs;
117
- this.#idleAbortSettleMs = options.idleAbortSettleMs;
118
- this.#stallProbeSettleMs = options.stallProbeSettleMs;
113
+ this.#timing = options.timing;
119
114
  this.#askAdmission = options.askAdmission;
120
115
  this.#questions = options.questions;
116
+ this.#group = options.group;
121
117
  this.#usage = new AgentUsage((snapshot) =>
122
118
  this.#emit({ type: "agent_usage", agent: this.name, ...snapshot }),
123
119
  );
@@ -188,9 +184,7 @@ export class Agent implements Handle {
188
184
  },
189
185
  },
190
186
  close: () => void this.close().catch(() => {}),
191
- askLimitGraceMs: this.#askLimitGraceMs,
192
- idleAbortSettleMs: this.#idleAbortSettleMs,
193
- stallProbeSettleMs: this.#stallProbeSettleMs,
187
+ ...(this.#timing === undefined ? {} : { timing: this.#timing }),
194
188
  ...(this.#questions === undefined
195
189
  ? {}
196
190
  : { questions: this.#questions, park: () => inFlight.park() }),
@@ -284,7 +278,18 @@ export class Agent implements Handle {
284
278
  });
285
279
  }
286
280
 
287
- /** Spawns a new Agent from a copy of this Agent's session (ADR-0044). */
281
+ /**
282
+ * Spawns a new Agent from a copy of this Agent's session (ADR-0044).
283
+ *
284
+ * Four refusals, in this order: `FORK_REFUSED` when the Run cannot open a
285
+ * fork at all, `FORK_REFUSED` when the overrides name `fork`, because that
286
+ * verb belongs to `ctx.spawn({ fork: ctx.host })` and never to a fork of an
287
+ * Agent (ADR-0068), `FORK_DURING_ASK` while an Ask is in flight, and
288
+ * `FORK_REFUSED` when the tail of the session is undefined or the Agent
289
+ * holds no session file to copy. The value check comes before the state
290
+ * checks, so a wrong call is refused the same way whatever the Agent is
291
+ * doing.
292
+ */
288
293
  fork(overrides?: ForkOptions): Promise<Handle> {
289
294
  const spawner = this.#fork;
290
295
  if (spawner === undefined) {
@@ -292,6 +297,17 @@ export class Agent implements Handle {
292
297
  agentError(this.name, "FORK_REFUSED", "this Agent's Run cannot open a fork"),
293
298
  );
294
299
  }
300
+ // A Host Source is a spawn verb, not a fork override (ADR-0068). The key is
301
+ // absent from `ForkOptions`, so it can only arrive untyped.
302
+ if (overrides !== undefined && typeof overrides === "object" && "fork" in overrides) {
303
+ return Promise.reject(
304
+ agentError(
305
+ this.name,
306
+ "FORK_REFUSED",
307
+ "handle.fork() forks this Agent — a Host Source belongs to ctx.spawn({ fork })",
308
+ ),
309
+ );
310
+ }
295
311
  if (this.#busy) {
296
312
  return Promise.reject(
297
313
  agentError(this.name, "FORK_DURING_ASK", "an Ask is in flight — fork between Asks"),
@@ -321,6 +337,7 @@ export class Agent implements Handle {
321
337
  worktree: this.#worktree(),
322
338
  settledAsks: this.#settledAsks,
323
339
  handle: this,
340
+ group: this.#group,
324
341
  },
325
342
  overrides,
326
343
  );
@@ -1,4 +1,5 @@
1
1
  import type { AgentPolicy } from "../contract/index.ts";
2
+ import { assertAgentName } from "./agent-name.ts";
2
3
 
3
4
  export type { ThinkingLevel } from "../contract/index.ts";
4
5
 
@@ -31,6 +32,8 @@ const arrayFields = [
31
32
  /**
32
33
  * Declares an Agent Definition: reusable policy, independent of where it runs.
33
34
  *
35
+ * A name that holds "/" or ":" throws `AGENT_NAME_INVALID`.
36
+ *
34
37
  * Throws a TypeError when `name` is missing or blank, or when `cwd`/`worktree`
35
38
  * appear — those are topology, chosen at spawn time, not baked into a definition.
36
39
  * The config is defensively copied and deep-frozen, so later mutation of the
@@ -42,6 +45,7 @@ export function defineAgent(config: AgentConfig): AgentDefinition {
42
45
  if (typeof config.name !== "string" || config.name.trim() === "") {
43
46
  throw new TypeError("defineAgent: name is required and must be non-empty");
44
47
  }
48
+ assertAgentName(config.name);
45
49
  if ("cwd" in config || "worktree" in config) {
46
50
  throw new TypeError(
47
51
  "defineAgent: cwd and worktree are topology concerns — set them at spawn time, not in the definition",
package/src/agent/fork.ts CHANGED
@@ -6,6 +6,7 @@ import type {
6
6
  ResolvedSpawnOptions,
7
7
  SpawnOptions,
8
8
  } from "../types.ts";
9
+ import { withoutForkVerbs } from "./host-fork.ts";
9
10
 
10
11
  /** What one Agent hands the spawn gate when it is forked (ADR-0044). */
11
12
  export interface ForkSource {
@@ -21,19 +22,42 @@ export interface ForkSource {
21
22
  readonly settledAsks: number;
22
23
  /** The source Handle itself, which becomes the fork's default Parent Link. */
23
24
  readonly handle: Handle;
25
+ /**
26
+ * The Group of the source; a fork lands in it unless the overrides name
27
+ * another Group (ADR-0070).
28
+ */
29
+ readonly group: string | undefined;
24
30
  }
25
31
 
26
32
  /** Opens one fork. The spawn gate owns it, so a fork walks the normal spawn path. */
27
33
  export type ForkSpawner = (source: ForkSource, overrides?: ForkOptions) => Promise<Handle>;
28
34
 
29
- /** Spawn identity and launch facts that only a fork carries. */
30
- export interface ForkIdentity {
31
- readonly forkOf: string;
32
- readonly forkAsks: number;
33
- /** The session pi copies with `--fork`; machine-specific, never identity. */
34
- readonly forkSession: string;
35
- readonly worktreeFrom?: WorktreeResolution;
36
- }
35
+ /**
36
+ * Spawn identity and launch facts that only a fork carries.
37
+ *
38
+ * The two fork kinds are one union, so no site can build a half fork: an Agent
39
+ * fork names its source Agent and the Asks it had settled, while a Host Fork
40
+ * names neither and carries the Host Session identity instead (ADR-0068).
41
+ */
42
+ export type ForkIdentity =
43
+ | {
44
+ readonly kind: "agent";
45
+ readonly forkOf: string;
46
+ readonly forkAsks: number;
47
+ /** The session pi copies with `--fork`; machine-specific, never identity. */
48
+ readonly forkSession: string;
49
+ readonly worktreeFrom?: WorktreeResolution;
50
+ }
51
+ | {
52
+ readonly kind: "host";
53
+ /**
54
+ * The copy of the Host Session pi forks with `--fork`. Absent when the
55
+ * Run holds no copy — a replay or a resume reconstructed its Host Source.
56
+ */
57
+ readonly forkSession?: string;
58
+ /** SHA-256 of the Host Session bytes as read; spawn identity (ADR-0068). */
59
+ readonly hostHash: string;
60
+ };
37
61
 
38
62
  /** One fork request: what to spawn, whose child it is, and whether to compact it. */
39
63
  export interface ForkRequest {
@@ -41,6 +65,8 @@ export interface ForkRequest {
41
65
  readonly spawnOptions: SpawnOptions;
42
66
  /** The raw Parent Link, defaulting to the fork source. */
43
67
  readonly parent: unknown;
68
+ /** The source's Group, inherited unless the overrides name another Group. */
69
+ readonly group: string | undefined;
44
70
  /** The fork's own `askDefaults` when given, else the source's; never merged. */
45
71
  readonly askDefaults: AskOptions | undefined;
46
72
  readonly fork: ForkIdentity;
@@ -58,9 +84,15 @@ export interface ForkRequest {
58
84
  *
59
85
  * Inheritance is verbatim, with two deliberate exceptions: `name` is dropped,
60
86
  * so the child gets a fresh auto-name unless the overrides give it one, and
61
- * `compact` never reaches the spawn options, because it is a fork verb rather
62
- * than a property of the Agent. `askDefaults` travels beside the spawn
63
- * options, as on every spawn path, and an explicit one replaces the source's.
87
+ * neither fork verb reaches the spawn options, because `fork` and `compact`
88
+ * are verbs of the spawn rather than properties of the Agent (ADR-0068).
89
+ * The rule holds on both sides: the spawn gate strips both verbs before the
90
+ * render and `withSelection` strips them again, so a source cannot carry one
91
+ * to inherit, and `withoutForkVerbs` here drops an overridden one. `compact`
92
+ * travels as a field of the request instead, and an untyped `fork` is refused
93
+ * by `handle.fork()` above this function.
94
+ * `askDefaults` travels beside the spawn options, as on every spawn path, and
95
+ * an explicit one replaces the source's.
64
96
  */
65
97
  export function forkRequest(source: ForkSource, overrides?: ForkOptions): ForkRequest {
66
98
  if (source.sessionFile === undefined) {
@@ -69,10 +101,16 @@ export function forkRequest(source: ForkSource, overrides?: ForkOptions): ForkRe
69
101
  const { name: _name, ...inherited } = source.spawnOptions;
70
102
  const { compact, parent, askDefaults, ...explicit } = overrides ?? {};
71
103
  return {
72
- spawnOptions: { ...inherited, ...stripUndefined(explicit) },
104
+ // A Host Source belongs to `ctx.spawn({ fork })`: `handle.fork()` refuses
105
+ // one, and this strip is the wall behind that refusal.
106
+ spawnOptions: withoutForkVerbs({ ...inherited, ...stripUndefined(explicit) }),
73
107
  parent: parent ?? source.handle,
108
+ // No condition here: `treeFields` drops it when the override resolves to a
109
+ // Group position.
110
+ group: source.group,
74
111
  askDefaults: askDefaults ?? source.askDefaults,
75
112
  fork: {
113
+ kind: "agent",
76
114
  forkOf: source.name,
77
115
  forkAsks: source.settledAsks,
78
116
  forkSession: source.sessionFile,
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The Host Fork branch of the spawn gate: `ctx.spawn({ fork: ctx.host })`
3
+ * (ADR-0068).
4
+ *
5
+ * A Host Fork walks the ordinary spawn path. This module does two things and
6
+ * nothing else: it reads the Host Source a program handed back into the fork
7
+ * facts the open path already understands, and it refuses every other use of
8
+ * `fork` and `compact` before any transport work starts.
9
+ *
10
+ * Neither key may reach the rendered, resolved or recorded spawn options — the
11
+ * peeked request is what a resume compares against the recorded spawn — so the
12
+ * open path strips both, and this module is the only reader of them.
13
+ */
14
+ import { YaagError } from "../errors.ts";
15
+ import type { SpawnOptions } from "../types.ts";
16
+ import type { ForkIdentity } from "./fork.ts";
17
+
18
+ /** The Host Source as the Run built it; the brand itself is type-only. */
19
+ interface RuntimeHostSource {
20
+ readonly kind: "host";
21
+ /** Absent when the Run holds no copy: a replayed or resumed Host Source. */
22
+ readonly sessionFile?: string;
23
+ readonly hostHash: string;
24
+ }
25
+
26
+ /** What the spawn gate reads out of one options object. */
27
+ export interface HostForkRequest {
28
+ /** The fork facts, absent for an ordinary spawn. */
29
+ readonly fork?: ForkIdentity;
30
+ /** The compaction the sugar runs after the Agent opened (ADR-0043). */
31
+ readonly compact?: boolean | string;
32
+ }
33
+
34
+ /**
35
+ * Reads the Host Fork request of one spawn.
36
+ *
37
+ * Refuses with `FORK_REFUSED` when `fork` is present but is not a Host Source
38
+ * this Run prepared — `undefined` slipping past the type, or a foreign object —
39
+ * and with `OPTIONS_CONFLICT` when `compact` is given without `fork`, because
40
+ * compaction is a fork verb and not a property of an Agent.
41
+ */
42
+ export function hostForkRequest(options: SpawnOptions): HostForkRequest {
43
+ const holdsFork = "fork" in options;
44
+ const compact = options.compact;
45
+ if (!holdsFork) {
46
+ if (compact !== undefined) {
47
+ throw new YaagError("OPTIONS_CONFLICT", "cannot use compact without fork");
48
+ }
49
+ return {};
50
+ }
51
+ const source = options.fork;
52
+ if (!isRuntimeHostSource(source)) {
53
+ throw new YaagError(
54
+ "FORK_REFUSED",
55
+ 'spawn option "fork" must be the Run\'s ctx.host — a program cannot build one',
56
+ );
57
+ }
58
+ return {
59
+ fork: {
60
+ kind: "host",
61
+ ...(source.sessionFile === undefined ? {} : { forkSession: source.sessionFile }),
62
+ hostHash: source.hostHash,
63
+ },
64
+ ...(compact === undefined ? {} : { compact }),
65
+ };
66
+ }
67
+
68
+ /** The spawn options without the two fork verbs, which no consumer may see. */
69
+ export function withoutForkVerbs(options: SpawnOptions): SpawnOptions {
70
+ const { fork: _fork, compact: _compact, ...rest } = options;
71
+ return rest;
72
+ }
73
+
74
+ function isRuntimeHostSource(value: unknown): value is RuntimeHostSource {
75
+ return (
76
+ typeof value === "object" &&
77
+ value !== null &&
78
+ "kind" in value &&
79
+ value.kind === "host" &&
80
+ // The copy path is optional, but a present one must still be a path.
81
+ (!("sessionFile" in value) || typeof value.sessionFile === "string") &&
82
+ "hostHash" in value &&
83
+ typeof value.hostHash === "string"
84
+ );
85
+ }
@@ -15,4 +15,5 @@ export {
15
15
  export { type ForkSource, type ForkSpawner, forkRequest } from "./fork.ts";
16
16
  export { makeSpawn, type SpawnDependencies, type SpawnGate } from "./spawn.ts";
17
17
  export type { OpenAgentRequest } from "./spawn-open.ts";
18
+ export { resolveParent } from "./spawn-parent.ts";
18
19
  export { isSpawnableDefinition } from "./spawnable-definition.ts";
@@ -3,11 +3,11 @@
3
3
  * Definition, with its override validation. Separate from `spawn.ts` so the
4
4
  * profiled path can reuse it without a cycle.
5
5
  */
6
- import type { SpawnOptions, SpawnOverrides } from "../types.ts";
6
+ import type { HostSource, SpawnOptions, SpawnOverrides } from "../types.ts";
7
7
  import { type AgentDefinition, agentDefinitionConfig, isAgentDefinition } from "./define-agent.ts";
8
8
  import type { OpenAgentRequest } from "./spawn-open.ts";
9
9
 
10
- /** Topology overrides after validation; `parent` stays raw for `resolveParent`. */
10
+ /** Topology overrides after validation; `parent` stays raw for `resolveParentPosition`. */
11
11
  type SpawnTopology = Omit<SpawnOverrides, "parent">;
12
12
 
13
13
  type MutableSpawnOverrides = { -readonly [Key in keyof SpawnTopology]: SpawnTopology[Key] };
@@ -26,8 +26,8 @@ interface ValidatedOverrides {
26
26
  * `askDefaults` leaves the spawn options and travels beside them, so it never
27
27
  * enters the recorded spawn options or the Cassette.
28
28
  * Throws a TypeError when the overrides are not an object, when they name a
29
- * key that is not `name`, `cwd`, `worktree` or `parent`, or when one of those
30
- * carries the wrong type. `parent` stays raw: `resolveParent` checks it.
29
+ * key that is not `name`, `cwd`, `worktree`, `parent`, `fork` or `compact`, or when one of those
30
+ * carries the wrong type. `parent` stays raw: `resolveParentPosition` checks it.
31
31
  */
32
32
  export function resolveRequest(
33
33
  definitionOrOptions: SpawnOptions | AgentDefinition,
@@ -45,6 +45,10 @@ export function resolveRequest(
45
45
  name: topology.name ?? name,
46
46
  ...(topology.cwd === undefined ? {} : { cwd: topology.cwd }),
47
47
  ...(topology.worktree === undefined ? {} : { worktree: topology.worktree }),
48
+ // Presence, not value: `ctx.spawn(def, { fork: undefined })` must reach the
49
+ // spawn gate and be refused there, exactly as the plain-options path is.
50
+ ...("fork" in topology ? { fork: topology.fork } : {}),
51
+ ...("compact" in topology ? { compact: topology.compact } : {}),
48
52
  },
49
53
  parent,
50
54
  askDefaults,
@@ -61,6 +65,11 @@ function definedFields<T extends object>(policy: T): Partial<T> {
61
65
  return copy;
62
66
  }
63
67
 
68
+ /** The shape a fork source has at runtime; the brand itself is type-only. */
69
+ function isHostSource(value: unknown): value is HostSource {
70
+ return typeof value === "object" && value !== null && "kind" in value && value.kind === "host";
71
+ }
72
+
64
73
  function validateOverrides(overrides: unknown): ValidatedOverrides {
65
74
  if (overrides === undefined) return { topology: {}, parent: undefined };
66
75
  if (typeof overrides !== "object" || overrides === null || Array.isArray(overrides)) {
@@ -69,14 +78,21 @@ function validateOverrides(overrides: unknown): ValidatedOverrides {
69
78
  );
70
79
  }
71
80
  for (const key of Object.keys(overrides)) {
72
- if (key !== "name" && key !== "cwd" && key !== "worktree" && key !== "parent") {
81
+ if (
82
+ key !== "name" &&
83
+ key !== "cwd" &&
84
+ key !== "worktree" &&
85
+ key !== "parent" &&
86
+ key !== "fork" &&
87
+ key !== "compact"
88
+ ) {
73
89
  throw new TypeError(
74
90
  `spawn override "${key}" is not allowed: definitions own policy and spawn overrides own topology`,
75
91
  );
76
92
  }
77
93
  }
78
94
  const topology: MutableSpawnOverrides = {};
79
- // `parent` is never narrowed here: `resolveParent` owns that check, so the
95
+ // `parent` is never narrowed here: `resolveParentPosition` owns that check, so the
80
96
  // error text stays in one place and no unchecked value becomes a Handle.
81
97
  let parent: unknown;
82
98
  for (const [key, value] of Object.entries(overrides)) {
@@ -99,6 +115,25 @@ function validateOverrides(overrides: unknown): ValidatedOverrides {
99
115
  }
100
116
  topology.worktree = value;
101
117
  }
118
+ // Both keys are checked here and then carried into the spawn options, where
119
+ // the spawn gate reads them: `host-fork.ts` turns them into the Host Fork
120
+ // facts or refuses them, and `spawn-open.ts` strips both above the prompt
121
+ // render, so neither reaches a rendered, resolved or recorded spawn
122
+ // request (ADR-0068). The check here is a shape check only: a wrong-typed
123
+ // value is a TypeError, and a well-shaped foreign object is FORK_REFUSED
124
+ // at the gate.
125
+ if (key === "fork") {
126
+ if (value !== undefined && !isHostSource(value)) {
127
+ throw new TypeError('spawn override "fork" must be a fork source when present');
128
+ }
129
+ topology.fork = value;
130
+ }
131
+ if (key === "compact") {
132
+ if (value !== undefined && typeof value !== "boolean" && typeof value !== "string") {
133
+ throw new TypeError('spawn override "compact" must be a boolean or a string when present');
134
+ }
135
+ topology.compact = value;
136
+ }
102
137
  }
103
138
  return { topology, parent };
104
139
  }