@aragon-agent/core 0.2.75 → 0.2.76

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.
@@ -116,11 +116,16 @@ export declare class Agent {
116
116
  * (`llm-network-error-dead-run` F).
117
117
  */
118
118
  private watchdogFired;
119
+ /** Where the run was when the watchdog fired; captured THEN, before the abort unwinds. */
120
+ private stall;
121
+ private activity;
119
122
  /** Failure facts for the NEXT `agent_end`; cleared as they are emitted. */
120
123
  private endFailure;
121
124
  private running;
122
125
  private idlePromiseResolvers;
123
126
  constructor(config: AgentConfig);
127
+ /** The watchdog's own evidence: what was last seen, and for how long nothing was. */
128
+ private describeStall;
124
129
  get state(): AgentState;
125
130
  /**
126
131
  * Send a user message and run the Agent loop until completion.
@@ -18,6 +18,90 @@ const DEFAULT_LLM_CALL_TIMEOUT = 300_000;
18
18
  const DEFAULT_IDLE_TIMEOUT = 60_000;
19
19
  const DEFAULT_TOOL_TIMEOUT = 120_000;
20
20
  const DEFAULT_CODE_TIMEOUT = 30_000;
21
+ function freshActivity() {
22
+ return {
23
+ lastEvent: 'none',
24
+ phase: 'awaiting_response',
25
+ toolName: undefined,
26
+ monotonicAt: performance.now(),
27
+ wallAt: Date.now(),
28
+ eventsSeen: 0,
29
+ toolsInFlight: 0,
30
+ };
31
+ }
32
+ /** `message_update` and friends read better in a log as `message_update:text_delta`. */
33
+ function describeEvent(event) {
34
+ return event.type === 'message_update'
35
+ ? `message_update:${event.streamEvent.type}`
36
+ : event.type;
37
+ }
38
+ /**
39
+ * Fold one event into the activity record. Mutates `a`; allocation-free because it
40
+ * runs for every streamed delta of every run, which is the only reason this is
41
+ * not written as a pure `(a, event) => a`.
42
+ */
43
+ function recordActivity(a, event) {
44
+ a.lastEvent = describeEvent(event);
45
+ a.monotonicAt = performance.now();
46
+ a.wallAt = Date.now();
47
+ a.eventsSeen += 1;
48
+ switch (event.type) {
49
+ case 'agent_start':
50
+ a.toolsInFlight = 0;
51
+ a.phase = 'awaiting_response';
52
+ a.toolName = undefined;
53
+ return;
54
+ case 'turn_start':
55
+ a.phase = 'awaiting_response';
56
+ a.toolName = undefined;
57
+ return;
58
+ case 'turn_end':
59
+ a.phase = 'between_turns';
60
+ return;
61
+ case 'tool_execution_start':
62
+ case 'code_execution_start':
63
+ a.toolsInFlight += 1;
64
+ a.phase = 'tool_running';
65
+ a.toolName = event.type === 'tool_execution_start' ? event.toolName : 'code';
66
+ return;
67
+ case 'tool_execution_end':
68
+ case 'code_execution_end':
69
+ a.toolsInFlight = Math.max(0, a.toolsInFlight - 1);
70
+ if (a.toolsInFlight === 0) {
71
+ a.phase = 'awaiting_response';
72
+ a.toolName = undefined;
73
+ }
74
+ return;
75
+ case 'message_update':
76
+ break;
77
+ default:
78
+ return;
79
+ }
80
+ switch (event.streamEvent.type) {
81
+ case 'thinking_start':
82
+ case 'thinking_delta':
83
+ a.phase = 'thinking';
84
+ return;
85
+ case 'text_delta':
86
+ a.phase = 'text';
87
+ return;
88
+ case 'tool_call_start':
89
+ a.phase = 'tool_input';
90
+ a.toolName = event.streamEvent.toolName;
91
+ return;
92
+ case 'tool_call_delta':
93
+ a.phase = 'tool_input';
94
+ return;
95
+ case 'retry_attempt':
96
+ case 'stream_restart':
97
+ // A new request is going out: the silence that follows is before ITS first byte.
98
+ a.phase = 'awaiting_response';
99
+ a.toolName = undefined;
100
+ return;
101
+ default:
102
+ return;
103
+ }
104
+ }
21
105
  // ---------------------------------------------------------------------------
22
106
  // Agent
23
107
  // ---------------------------------------------------------------------------
@@ -56,6 +140,9 @@ export class Agent {
56
140
  * (`llm-network-error-dead-run` F).
57
141
  */
58
142
  watchdogFired = false;
143
+ /** Where the run was when the watchdog fired; captured THEN, before the abort unwinds. */
144
+ stall = null;
145
+ activity = freshActivity();
59
146
  /** Failure facts for the NEXT `agent_end`; cleared as they are emitted. */
60
147
  endFailure = null;
61
148
  running = false;
@@ -91,9 +178,26 @@ export class Agent {
91
178
  console.error('[Agent] idle watchdog fired — aborting');
92
179
  // BEFORE the abort, so the fact survives the unwind this call starts.
93
180
  this.watchdogFired = true;
181
+ // CAPTURED NOW, NOT IN `finally`: the abort makes the loop emit events of its
182
+ // own while it unwinds, and each of them would move `lastEvent` to something
183
+ // that is not where the silence began.
184
+ this.stall = this.describeStall();
94
185
  this.abort();
95
186
  });
96
187
  }
188
+ /** The watchdog's own evidence: what was last seen, and for how long nothing was. */
189
+ describeStall() {
190
+ const a = this.activity;
191
+ return {
192
+ phase: a.phase,
193
+ lastEvent: a.lastEvent,
194
+ msSinceLastEvent: Math.round(performance.now() - a.monotonicAt),
195
+ wallMsSinceLastEvent: Date.now() - a.wallAt,
196
+ eventsSeen: a.eventsSeen,
197
+ idleTimeoutMs: this.idleTimeout,
198
+ ...(a.toolName !== undefined ? { toolName: a.toolName } : {}),
199
+ };
200
+ }
97
201
  // =========================================================================
98
202
  // Public readonly state
99
203
  // =========================================================================
@@ -244,6 +348,7 @@ export class Agent {
244
348
  // the watchdog armed across a 30-second backoff and the run is aborted for
245
349
  // being "idle" while it is provably waiting on purpose.
246
350
  this.applyWatchdogPolicy(event);
351
+ recordActivity(this.activity, event);
247
352
  for (const listener of this.listeners) {
248
353
  try {
249
354
  listener(event);
@@ -331,6 +436,8 @@ export class Agent {
331
436
  async runLoopWithLifecycle() {
332
437
  this.running = true;
333
438
  this.watchdogFired = false;
439
+ this.stall = null;
440
+ this.activity = freshActivity();
334
441
  this.abortController = new AbortController();
335
442
  this.watchdog.start();
336
443
  this.emit({ type: 'agent_start' });
@@ -392,9 +499,16 @@ export class Agent {
392
499
  // turn is safe. `endFailure` wins when both could apply: the loop reached
393
500
  // an error of its own, which is the more specific fact.
394
501
  const failure = this.watchdogFired && !this.endFailure
395
- ? { errored: true, reason: 'idle_watchdog', errorType: 'timeout', retryable: true }
502
+ ? {
503
+ errored: true,
504
+ reason: 'idle_watchdog',
505
+ errorType: 'timeout',
506
+ retryable: true,
507
+ ...(this.stall ? { stall: this.stall } : {}),
508
+ }
396
509
  : (this.endFailure ?? {});
397
510
  this.endFailure = null;
511
+ this.stall = null;
398
512
  this.emit({
399
513
  type: 'agent_end',
400
514
  messages: this.messageManager.getAll(),
package/dist/index.d.ts CHANGED
@@ -42,7 +42,7 @@ export { createSkillFindTool } from './skills/skill-find-tool.js';
42
42
  export { computeToolPolicy, evaluateToolCall } from './skills/tool-policy.js';
43
43
  export { SKILL_CATALOG_MAX_BYTES, SKILL_BODY_MAX_BYTES, SKILL_RESULT_MAX_BYTES, ALWAYS_SKILLS_MAX_BYTES, SKILL_MD_MAX_BYTES, SKILL_DESC_LINE_MAX, SKILL_FILES_MAX, SKILL_FIND_MAX_BYTES, SKILL_NAME_PATTERN, } from './skills/constants.js';
44
44
  export type { SkillScope, SkillActivation, SkillFrontmatter, SkillRecord, SkillFileRef, SkillHost, SkillManifest, SkillValidationIssue, SkillCatalogOptions, SkillBodyOptions, SkillFindOptions, SkillIntegrity, SkillUsageStat, SkillUsageMap, SkillInvocationOptions, SkillPlatform, SkillPolicyView, SkillToolPolicyMode, ToolPolicyDecision, ToolPolicyInput, ToolPolicySource, ToolPolicyVerdict, } from './skills/types.js';
45
- export type { AgentEvent, AgentEventListener, AgentStartEvent, AgentEndEvent, TurnStartEvent, SteeringAcceptedEvent, TurnEndEvent, MessageUpdateEvent, ToolExecutionStartEvent, ToolExecutionEndEvent, CodeExecutionStartEvent, CodeExecutionEndEvent, CompactionStartEvent, CompactionEndEvent, } from './types.js';
45
+ export type { AgentEvent, AgentEventListener, AgentStartEvent, AgentEndEvent, AgentStallInfo, AgentStallPhase, TurnStartEvent, SteeringAcceptedEvent, TurnEndEvent, MessageUpdateEvent, ToolExecutionStartEvent, ToolExecutionEndEvent, CodeExecutionStartEvent, CodeExecutionEndEvent, CompactionStartEvent, CompactionEndEvent, } from './types.js';
46
46
  export { Agent } from './engine/agent.js';
47
47
  export type { AgentConfig, AgentState } from './engine/agent.js';
48
48
  export type { ModelRef } from './engine/agent-loop.js';
package/dist/types.d.ts CHANGED
@@ -18,6 +18,38 @@ export interface AgentStartEvent {
18
18
  * feature: `Object.keys(end)` is `['messages', 'type']` on the happy path.
19
19
  */
20
20
  export type AgentEndReason = 'llm_error' | 'idle_watchdog';
21
+ /**
22
+ * What the run was doing when the idle watchdog gave up on it
23
+ * (`team-idle-watchdog-dead-run` F0).
24
+ *
25
+ * The `run_end` log row used to say only "the watchdog fired", so a silence that
26
+ * began while the model was still waiting for its first byte, one that began in
27
+ * the middle of a long tool-call argument and one that began while a local tool
28
+ * was running all looked identical, and the next investigation had to infer the
29
+ * stage from the gap to the previous log line. Every field is a FACT the engine
30
+ * already holds at that instant; nothing here is a guess about why.
31
+ *
32
+ * `msSinceLastEvent` (monotonic) and `wallMsSinceLastEvent` (wall clock) are both
33
+ * recorded on purpose: when the machine was suspended the two diverge by the
34
+ * length of the sleep, which is the only way to tell "the network went quiet"
35
+ * from "this laptop was asleep" in a log read afterwards.
36
+ */
37
+ export type AgentStallPhase = 'awaiting_response' | 'thinking' | 'text' | 'tool_input' | 'tool_running' | 'between_turns';
38
+ export interface AgentStallInfo {
39
+ phase: AgentStallPhase;
40
+ /** The last engine event before the silence (`message_update:text_delta`, ...). */
41
+ lastEvent: string;
42
+ /** Silence measured on the monotonic clock (`performance.now`). */
43
+ msSinceLastEvent: number;
44
+ /** The same silence on the wall clock (`Date.now`). */
45
+ wallMsSinceLastEvent: number;
46
+ /** Engine events emitted since `agent_start`. NOT raw SSE frames: the engine never sees those. */
47
+ eventsSeen: number;
48
+ /** The window the watchdog was armed with. */
49
+ idleTimeoutMs: number;
50
+ /** The tool being written (`tool_input`) or run (`tool_running`), when there was one. */
51
+ toolName?: string;
52
+ }
21
53
  export interface AgentEndEvent {
22
54
  type: 'agent_end';
23
55
  messages: Message[];
@@ -39,6 +71,12 @@ export interface AgentEndEvent {
39
71
  */
40
72
  retryable?: boolean;
41
73
  reason?: AgentEndReason;
74
+ /**
75
+ * Present ONLY when `reason` is `idle_watchdog`: where the run went quiet.
76
+ * Absent on every other `agent_end`, so the happy path keeps its
77
+ * `['messages', 'type']` shape.
78
+ */
79
+ stall?: AgentStallInfo;
42
80
  }
43
81
  export interface TurnStartEvent {
44
82
  type: 'turn_start';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aragon-agent/core",
3
- "version": "0.2.75",
3
+ "version": "0.2.76",
4
4
  "description": "AragonAgent core engine — LLM provider adapters, streaming, tool registry/execution, steering, idle watchdog, and an optional isolated-vm sandbox.",
5
5
  "license": "MIT",
6
6
  "author": "AragonAgent contributors",