@yaag/runtime 0.30.0 → 0.31.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.30.0",
3
+ "version": "0.31.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -16,7 +16,8 @@
16
16
  "scripts": {
17
17
  "typecheck": "tsc --noEmit",
18
18
  "test": "bun test --timeout 30000 ./test",
19
- "test:e2e": "bun test --timeout 30000 e2e"
19
+ "test:e2e": "bun test --timeout 30000 ./e2e",
20
+ "test:live": "bun test --timeout 30000 ./live"
20
21
  },
21
22
  "dependencies": {
22
23
  "typebox": "1.3.7"
@@ -6,9 +6,15 @@ export interface AgentUsageSnapshot {
6
6
  }
7
7
 
8
8
  /**
9
- * Accumulates complete assistant `message_end` usage across an Agent's lifetime.
9
+ * Accumulates the `usage` of each assistant and `toolResult` `message_end`
10
+ * frame across an Agent's lifetime.
10
11
  *
11
- * Invalid or non-assistant frames never change the reported accounting.
12
+ * A tool result's `usage` is nested model work that the tool reports, for
13
+ * example a client-side `advisor` call or a `yaag_run` inside the Agent
14
+ * (ADR-0043 amendment). The rule reads only the role and `usage`, never the
15
+ * tool name or `details`. Invalid frames and other roles never change the
16
+ * accounting. `agent_exit` replaces this value with pi's session stats, which
17
+ * already include tool-result usage.
12
18
  */
13
19
  export class AgentUsage {
14
20
  readonly #report: (snapshot: AgentUsageSnapshot) => void;
@@ -19,7 +25,7 @@ export class AgentUsage {
19
25
  this.#report = report;
20
26
  }
21
27
 
22
- /** Observes one raw Agent frame and reports after each valid assistant completion. */
28
+ /** Observes one raw Agent frame and reports after each valid assistant or tool-result usage. */
23
29
  observe(frame: Frame): void {
24
30
  const usage = usageFromFrame(frame);
25
31
  if (usage === null) return;
@@ -48,8 +54,8 @@ export class AgentUsage {
48
54
 
49
55
  function usageFromFrame(frame: Frame): AgentUsageSnapshot | null {
50
56
  if (frame.type !== "message_end" || !isRecord(frame.message)) return null;
51
- if (frame.message.role !== "assistant" || !isRecord(frame.message.usage)) return null;
52
- const usage = frame.message.usage;
57
+ const { role, usage } = frame.message;
58
+ if ((role !== "assistant" && role !== "toolResult") || !isRecord(usage)) return null;
53
59
  if (!isNumber(usage.input) || !isNumber(usage.output) || !isNumber(usage.cacheRead)) return null;
54
60
  if (!isNumber(usage.cacheWrite) || !isNumber(usage.totalTokens) || !isRecord(usage.cost))
55
61
  return null;
@@ -75,7 +75,8 @@ export function resumeTransport(
75
75
  ...(agent.sessionFile === undefined ? {} : { sessionFile: agent.sessionFile }),
76
76
  ...(worktree === undefined ? {} : { worktree }),
77
77
  });
78
- return new ResumeTransport(agent, live, options, restoration);
78
+ const prefix = cassette.run.outcome === "interrupted" ? settledPrefix(agent) : agent;
79
+ return new ResumeTransport(prefix, live, options, restoration);
79
80
  },
80
81
 
81
82
  // A changed spawn must not silently inherit the recorded model, so the
@@ -318,3 +319,20 @@ function continuationOptions(agent: CassetteAgent, original: OpenOptions): OpenO
318
319
  function worktreeResolution(agent: CassetteAgent): CassetteAgent["worktree"] {
319
320
  return agent.spawn.worktree === true ? agent.worktree : undefined;
320
321
  }
322
+
323
+ /** Only the final Ask can still be in flight at a hard death (ADR-0031). Earlier failures replay. */
324
+ function settledPrefix(agent: CassetteAgent): CassetteAgent {
325
+ const ask = agent.asks.at(-1);
326
+ if (
327
+ ask === undefined ||
328
+ ask.incomplete !== undefined ||
329
+ ask.limit !== undefined ||
330
+ ask.stalled !== undefined ||
331
+ ask.outcome !== undefined ||
332
+ ask.recovered === true ||
333
+ ask.receivedFrames.some((frame) => frame.type === "agent_settled")
334
+ ) {
335
+ return agent;
336
+ }
337
+ return { ...agent, asks: agent.asks.slice(0, -1) };
338
+ }
@@ -22,6 +22,9 @@ const ThinkingLevelSchema = Type.Union(THINKING_LEVELS.map((level) => Type.Liter
22
22
  * `profile` is the one top-level scalar key. Unlike `agents.extensions`, which
23
23
  * concatenates across the layers, it does not concatenate: the last layer that
24
24
  * states it wins (ADR-0052).
25
+ *
26
+ * `hc.peekTerminals` switches on the Peek Terminals in hc (ADR-0072). Like
27
+ * `profile`, it is last-layer-wins.
25
28
  */
26
29
  export const YaagConfigSchema = Type.Object(
27
30
  {
@@ -55,6 +58,12 @@ export const YaagConfigSchema = Type.Object(
55
58
  { additionalProperties: false },
56
59
  ),
57
60
  ),
61
+ hc: Type.Optional(
62
+ Type.Object(
63
+ { peekTerminals: Type.Optional(Type.Boolean()) },
64
+ { additionalProperties: false },
65
+ ),
66
+ ),
58
67
  },
59
68
  { additionalProperties: false },
60
69
  );
@@ -59,6 +59,10 @@ export interface EffectiveConfig {
59
59
  * goes through `questionSummaryOf`, which applies the defaults.
60
60
  */
61
61
  readonly askUserSummary?: QuestionSummaryConfig;
62
+ /**
63
+ * The last layer that set `hc.peekTerminals` (ADR-0072); absent means `false`.
64
+ */
65
+ readonly hcPeekTerminals?: boolean;
62
66
  }
63
67
 
64
68
  /** What to discover and read. */
@@ -149,10 +153,15 @@ function mergeLayers(found: readonly LoadedLayer[]): EffectiveConfig {
149
153
  (last, { layer }) => layer.document.profile ?? last,
150
154
  undefined,
151
155
  );
156
+ const hcPeekTerminals = found.reduce<boolean | undefined>(
157
+ (last, { layer }) => layer.document.hc?.peekTerminals ?? last,
158
+ undefined,
159
+ );
152
160
  return Object.freeze({
153
161
  extensions: Object.freeze(extensions),
154
162
  files: Object.freeze(found.map(({ layer }) => layer.file)),
155
163
  ...(profile === undefined ? {} : { profile }),
164
+ ...(hcPeekTerminals === undefined ? {} : { hcPeekTerminals }),
156
165
  askUserSummary: mergeQuestionSummary(found),
157
166
  });
158
167
  }
package/src/events.ts CHANGED
@@ -223,9 +223,15 @@ export type LifecycleEventBody =
223
223
  | {
224
224
  readonly type: "agent_usage";
225
225
  readonly agent: string;
226
- /** Cumulative assistant-completion usage observed so far. */
226
+ /**
227
+ * Cumulative live usage: assistant completions plus the nested usage that
228
+ * tool results report (ADR-0043). `agent_exit` replaces it.
229
+ */
227
230
  readonly tokens: TokenBreakdown;
228
- /** Cumulative assistant-completion cost observed so far. */
231
+ /**
232
+ * Cumulative live cost: assistant completions plus the nested cost that
233
+ * tool results report (ADR-0043). `agent_exit` replaces it.
234
+ */
229
235
  readonly cost: number;
230
236
  }
231
237
  | {
@@ -108,7 +108,10 @@ export type AgentInfo = IdleAgentInfo | AskingAgentInfo | ExitedAgentInfo;
108
108
  /** Internal fold record, deliberately identical to the public observer projection. */
109
109
  export type AgentRecord = AgentInfo;
110
110
 
111
- /** Cumulative assistant-completion usage observed while an Agent is still live. */
111
+ /**
112
+ * Cumulative live usage while an Agent is still live: assistant completions plus
113
+ * the nested usage that tool results report (ADR-0043). `agent_exit` replaces it.
114
+ */
112
115
  export interface AgentUsageObservation {
113
116
  readonly tokens: TokenBreakdown;
114
117
  readonly cost: number;
@@ -375,7 +378,7 @@ function cutShortAskMs(agent: AgentRecord, at: number | null): number {
375
378
 
376
379
  /**
377
380
  * Computes Run-wide accounting from every Agent's newest snapshot. A live Agent
378
- * without an assistant completion contributes zero; an exited Agent without
381
+ * without observed usage contributes zero; an exited Agent without
379
382
  * final stats preserves the established unknown-accounting semantics.
380
383
  */
381
384
  export function totalsFromAgents(agents: Readonly<Record<string, AgentRecord>>): {