@yaag/runtime 0.12.0 → 0.14.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaag/runtime",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -151,6 +151,7 @@ export async function openAgent(
151
151
  const resolvedCwd = opened.startup.worktree?.cwd ?? cwd;
152
152
  const branch = opened.startup.worktree?.branch;
153
153
  const sessionFile = opened.startup.sessionFile;
154
+ const thinking = opened.startup.thinking;
154
155
  const agent = new Agent({
155
156
  name,
156
157
  cwd: resolvedCwd,
@@ -176,6 +177,7 @@ export async function openAgent(
176
177
  cwd: resolvedCwd,
177
178
  ...(branch === undefined ? {} : { branch }),
178
179
  ...(sessionFile === undefined ? {} : { sessionFile }),
180
+ ...(thinking === undefined ? {} : { thinking }),
179
181
  ...(parent === undefined ? {} : { parent }),
180
182
  ...(request.fork === undefined ? {} : { origin: "fork" as const }),
181
183
  });
@@ -1,5 +1,5 @@
1
1
  import type { AgentActivity, AskOutputChannel, EventSink } from "../events.ts";
2
- import type { ModelFallback } from "../model/index.ts";
2
+ import type { ModelFallback, ThinkingLevel } from "../model/index.ts";
3
3
  import type { NodeSnapshot } from "../node/index.ts";
4
4
  import { agentAskPath, childPath } from "../node/index.ts";
5
5
  import { promptGist } from "../prompt/index.ts";
@@ -71,8 +71,13 @@ export class AskEvents {
71
71
  * Agent-scoped like `fallback`: the pattern side of the story stays in
72
72
  * `model_fallback`, and this carries pi's own `provider/id`.
73
73
  */
74
- model = (model: string): void => {
75
- this.#emit({ type: "agent_model", agent: this.#agent, model });
74
+ model = (change: { readonly model: string; readonly thinking?: ThinkingLevel }): void => {
75
+ this.#emit({
76
+ type: "agent_model",
77
+ agent: this.#agent,
78
+ model: change.model,
79
+ ...(change.thinking === undefined ? {} : { thinking: change.thinking }),
80
+ });
76
81
  };
77
82
 
78
83
  /** A `normal` cause is the absent default, so ordinary settlements stay lean. */
@@ -108,7 +108,10 @@ class AskExchange {
108
108
  fallback.onSwapped(selection.model ?? swapped.model, swapped.model);
109
109
  // Only a swap that landed reports a model: a refused `set_model` throws
110
110
  // above and re-enters the loop, so the loop itself stays event-free.
111
- this.#events.model(swapped.model);
111
+ this.#events.model({
112
+ model: swapped.model,
113
+ ...(swapped.thinking === undefined ? {} : { thinking: swapped.thinking }),
114
+ });
112
115
  },
113
116
  });
114
117
  }
package/src/events.ts CHANGED
@@ -7,11 +7,11 @@
7
7
  * dedicated fd arrives with the extension.
8
8
  */
9
9
  import type { SettlementCause } from "./ask/index.ts";
10
- import type { ModelErrorReason } from "./model/index.ts";
10
+ import type { ModelErrorReason, ThinkingLevel } from "./model/index.ts";
11
11
  import type { TokenBreakdown, WorktreeResolution } from "./transport/index.ts";
12
12
 
13
13
  export type { SettlementCause } from "./ask/index.ts";
14
- export type { ModelErrorReason } from "./model/index.ts";
14
+ export type { ModelErrorReason, ThinkingLevel } from "./model/index.ts";
15
15
 
16
16
  /**
17
17
  * A Run's outcome (ADR-0022). `paused` arrives with the pause slice.
@@ -75,6 +75,12 @@ export type LifecycleEventBody =
75
75
  readonly parent?: string;
76
76
  /** How the Agent came to be; absent means "spawn". */
77
77
  readonly origin?: SpawnOrigin;
78
+ /**
79
+ * The thinking level the Agent runs at, as pi reported it in the startup
80
+ * `get_state`. Absent on events from an older CLI and on
81
+ * Cassette-playback Agents; a renderer then shows the model alone.
82
+ */
83
+ readonly thinking?: ThinkingLevel;
78
84
  }
79
85
  | {
80
86
  readonly type: "ask_start";
@@ -160,6 +166,11 @@ export type LifecycleEventBody =
160
166
  readonly agent: string;
161
167
  /** pi's concrete `provider/id` for the model the Agent runs now. */
162
168
  readonly model: string;
169
+ /**
170
+ * Present only when the swap set a level with `set_thinking_level`. Its
171
+ * absence means the level is unchanged as far as yaag observed.
172
+ */
173
+ readonly thinking?: ThinkingLevel;
163
174
  }
164
175
  | {
165
176
  readonly type: "agent_usage";
package/src/index.ts CHANGED
@@ -103,6 +103,8 @@ export type {
103
103
  ThinkingResolver,
104
104
  ThinkingSpec,
105
105
  } from "./model/index.ts";
106
+ // The closed thinking-level list, so a parser outside the runtime has one authority.
107
+ export { isThinkingLevel } from "./model/index.ts";
106
108
  export type { DecodedNode, NodeDecoder, NodePath, NodeSnapshot } from "./node/index.ts";
107
109
  export {
108
110
  agentAskPath,
@@ -30,4 +30,4 @@ export {
30
30
  resolveModel,
31
31
  retryOnModelFailure,
32
32
  } from "./resolution-loop.ts";
33
- export type { ThinkingLevel } from "./thinking-level.ts";
33
+ export { isThinkingLevel, type ThinkingLevel } from "./thinking-level.ts";
@@ -2,6 +2,7 @@ import { agentError } from "../error-factories.ts";
2
2
  import type { CommandResponse, Frame } from "../transport/index.ts";
3
3
  import { matchAvailableModel, readAvailableModel, readAvailableModels } from "./model-match.ts";
4
4
  import type { ModelSelection } from "./model-resolution.ts";
5
+ import type { ThinkingLevel } from "./thinking-level.ts";
5
6
 
6
7
  /** Sends one command frame to the live Agent. The Connection satisfies it. */
7
8
  export type ModelSwapCommand = (frame: Frame) => Promise<CommandResponse>;
@@ -10,6 +11,11 @@ export type ModelSwapCommand = (frame: Frame) => Promise<CommandResponse>;
10
11
  export interface ModelSwapResult {
11
12
  /** `provider/id` pi reports for the new model. */
12
13
  readonly model: string;
14
+ /**
15
+ * The level `set_thinking_level` landed; absent when the selection named
16
+ * none, in which case yaag never observed a level change.
17
+ */
18
+ readonly thinking?: ThinkingLevel;
13
19
  }
14
20
 
15
21
  /**
@@ -67,7 +73,10 @@ export async function swapModel(options: {
67
73
  throw failed(options.agent, thinking.error ?? "set_thinking_level was rejected");
68
74
  }
69
75
  }
70
- return { model: reportedModel(response.data) ?? `${match.model.provider}/${match.model.id}` };
76
+ return {
77
+ model: reportedModel(response.data) ?? `${match.model.provider}/${match.model.id}`,
78
+ ...(options.selection.thinking === undefined ? {} : { thinking: options.selection.thinking }),
79
+ };
71
80
  }
72
81
 
73
82
  /** pi answers `set_model` with the full Model object it switched to. */
@@ -1,9 +1,9 @@
1
- import type { AgentActivity, SpawnOrigin } from "../events.ts";
1
+ import type { AgentActivity, SpawnOrigin, ThinkingLevel } from "../events.ts";
2
2
  import type { TokenBreakdown, WorktreeResolution } from "../transport/index.ts";
3
3
  import type { ModelFallbackInfo } from "./summary-fallbacks.ts";
4
4
  import type { NodeInfo } from "./summary-nodes.ts";
5
5
 
6
- export type { AgentActivity, SpawnOrigin } from "../events.ts";
6
+ export type { AgentActivity, SpawnOrigin, ThinkingLevel } from "../events.ts";
7
7
  export type { ModelFallbackInfo } from "./summary-fallbacks.ts";
8
8
  export type { NodeInfo } from "./summary-nodes.ts";
9
9
 
@@ -27,6 +27,8 @@ interface AgentInfoBase {
27
27
  * A `resolvedModel` pattern never lands here.
28
28
  */
29
29
  readonly model: string | null;
30
+ /** The thinking level the Agent runs at; null when no event ever reported one. */
31
+ readonly thinking: ThinkingLevel | null;
30
32
  readonly cwd: string | null;
31
33
  readonly branch: string | null;
32
34
  /** pi's session file for this Agent, when the spawn reported one; a Peek reads it. */
@@ -111,6 +113,7 @@ export interface AgentExitObservation {
111
113
  export function placeholderAgent(): IdleAgentInfo {
112
114
  return {
113
115
  model: null,
116
+ thinking: null,
114
117
  cwd: null,
115
118
  branch: null,
116
119
  sessionFile: null,
@@ -143,6 +146,7 @@ export function spawnAgent(
143
146
  current: AgentRecord | undefined,
144
147
  identity: {
145
148
  readonly model: string;
149
+ readonly thinking?: ThinkingLevel;
146
150
  readonly cwd: string;
147
151
  readonly branch?: string;
148
152
  readonly sessionFile?: string;
@@ -155,6 +159,7 @@ export function spawnAgent(
155
159
  return {
156
160
  ...placeholderAgent(),
157
161
  model: identity.model,
162
+ thinking: identity.thinking ?? null,
158
163
  cwd: identity.cwd,
159
164
  branch: identity.branch ?? null,
160
165
  sessionFile: identity.sessionFile ?? null,
@@ -166,6 +171,7 @@ export function spawnAgent(
166
171
  return {
167
172
  ...current,
168
173
  model: current.model ?? identity.model,
174
+ thinking: current.thinking ?? identity.thinking ?? null,
169
175
  cwd: current.cwd ?? identity.cwd,
170
176
  branch: current.branch ?? identity.branch ?? null,
171
177
  sessionFile: current.sessionFile ?? identity.sessionFile ?? null,
@@ -11,8 +11,12 @@ type AgentModelEvent = Extract<LifecycleEventBody, { readonly type: "agent_model
11
11
  * `state`, `askIndex`, `activity`, `stateChangedAt` and the bounded fallback
12
12
  * table stay as they are. An exited Agent is skipped, because its model is
13
13
  * frozen together with its final accounting.
14
+ *
15
+ * An event that names no level leaves the last known level standing: yaag only
16
+ * observes a level it set itself (ADR-0038/0041), so a swap without a level
17
+ * reports none rather than an unknown one.
14
18
  */
15
19
  export function applyAgentModel(agent: AgentRecord, event: AgentModelEvent): AgentRecord {
16
20
  if (agent.state === "exited") return agent;
17
- return { ...agent, model: event.model };
21
+ return { ...agent, model: event.model, thinking: event.thinking ?? agent.thinking };
18
22
  }
@@ -1,7 +1,7 @@
1
1
  import { YaagError } from "../errors.ts";
2
2
  import { FrameQueue } from "./frame-queue.ts";
3
3
  import { decodeFrames } from "./jsonl.ts";
4
- import { piCommand, readModel, readSessionFile, readStats } from "./pi-state.ts";
4
+ import { piCommand, readModel, readStartup, readStats } from "./pi-state.ts";
5
5
  import { reap } from "./reap.ts";
6
6
  import { appendStderrTail, stderrDiagnostic } from "./stderr-tail.ts";
7
7
  import {
@@ -103,8 +103,7 @@ class LiveTransport implements AgentTransport {
103
103
  verifyEffectiveTools(contract, await probe.wait());
104
104
  }
105
105
  this.model = model;
106
- const sessionFile = readSessionFile(state);
107
- observeStartup?.(sessionFile === null ? {} : { sessionFile });
106
+ observeStartup?.(readStartup(state));
108
107
  } catch (error) {
109
108
  // Startup has not returned this transport to spawn.ts, so cleanup belongs here.
110
109
  // A secondary stats or reap failure must not hide the startup diagnostic.
@@ -1,5 +1,6 @@
1
1
  import { REPORT_RESULT_EXTENSION_PATH, REPORT_RESULT_TOOL_NAME } from "../ask-contract/index.ts";
2
2
  import type { AgentProgress } from "../contract/index.ts";
3
+ import { isThinkingLevel, type ThinkingLevel } from "../model/index.ts";
3
4
  import { SYSTEM_PROMPT_RECORDER_EXTENSION_PATH } from "./system-prompt-recorder.ts";
4
5
  import type {
5
6
  AgentStats,
@@ -7,6 +8,7 @@ import type {
7
8
  Frame,
8
9
  OpenOptions,
9
10
  TokenBreakdown,
11
+ TransportStartup,
10
12
  } from "./transport.ts";
11
13
 
12
14
  export type { AgentProgress } from "../contract/index.ts";
@@ -137,6 +139,31 @@ export function readAgentProgress(data: unknown): AgentProgress | null {
137
139
  };
138
140
  }
139
141
 
142
+ /**
143
+ * The startup facts one `get_state` response reports, as the live transport
144
+ * hands them to its startup observer. A fact pi did not report is left out
145
+ * rather than set to a null, so an observer sees only what pi actually said.
146
+ */
147
+ export function readStartup(response: Frame): TransportStartup {
148
+ const sessionFile = readSessionFile(response);
149
+ const thinking = readThinkingLevel(response);
150
+ return {
151
+ ...(sessionFile === null ? {} : { sessionFile }),
152
+ ...(thinking === null ? {} : { thinking }),
153
+ };
154
+ }
155
+
156
+ /**
157
+ * The Agent's effective thinking level from a startup `get_state` response (pi
158
+ * `docs/rpc.md`, `data.thinkingLevel`). Null when pi reported none or reported
159
+ * an unknown level, so an unrecognized value never reaches a renderer.
160
+ */
161
+ export function readThinkingLevel(response: Frame): ThinkingLevel | null {
162
+ const data: unknown = response.data;
163
+ if (typeof data !== "object" || data === null || !("thinkingLevel" in data)) return null;
164
+ return isThinkingLevel(data.thinkingLevel) ? data.thinkingLevel : null;
165
+ }
166
+
140
167
  /** Returns pi's persisted session path from a startup `get_state` response, when valid. */
141
168
  export function readSessionFile(response: Frame): string | null {
142
169
  const data: unknown = response.data;
@@ -256,6 +256,8 @@ export interface WorktreeResolution {
256
256
  /** Startup facts a factory may report without widening AgentTransport. */
257
257
  export interface TransportStartup {
258
258
  readonly sessionFile?: string;
259
+ /** The thinking level pi reported at startup; absent when it reported none. */
260
+ readonly thinking?: ThinkingLevel;
259
261
  readonly worktree?: WorktreeResolution;
260
262
  }
261
263