@ethosagent/core 0.1.1 → 0.2.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LLMProvider, ToolRegistry, PersonalityRegistry, MemoryProvider, SessionStore, HookRegistry, ContextInjector, Session, SessionFilter, StoredMessage, SessionUsage, SearchResult, MemoryLoadContext, MemoryUpdate, PersonalityConfig, VoidHooks, ModifyingHooks, ClaimingHooks, Tool, ToolContext, ToolResult } from '@ethosagent/types';
1
+ import { HookRegistry, LLMProvider, ToolRegistry, PersonalityRegistry, MemoryProvider, SessionStore, ContextInjector, Storage, Session, SessionFilter, StoredMessage, SessionUsage, SearchResult, MemoryLoadContext, MemoryUpdate, PersonalityConfig, VoidHooks, ModifyingHooks, ClaimingHooks, Tool, ToolFilterOpts, ToolContext, ToolResult } from '@ethosagent/types';
2
2
 
3
3
  type AgentEvent = {
4
4
  type: 'text_delta';
@@ -16,12 +16,22 @@ type AgentEvent = {
16
16
  toolName: string;
17
17
  message: string;
18
18
  percent?: number;
19
+ audience: 'internal' | 'user';
19
20
  } | {
20
21
  type: 'tool_end';
21
22
  toolCallId: string;
22
23
  toolName: string;
23
24
  ok: boolean;
24
25
  durationMs: number;
26
+ audience?: 'internal' | 'user';
27
+ /**
28
+ * Tool output body — the success value when `ok`, or the error
29
+ * message when `ok: false`. Optional so consumers that only care
30
+ * about the status (CLI ASCII chips, telemetry) can ignore it.
31
+ * The web chip surfaces this on expand-on-click without a
32
+ * follow-up history fetch.
33
+ */
34
+ result?: string;
25
35
  } | {
26
36
  type: 'usage';
27
37
  inputTokens: number;
@@ -47,6 +57,26 @@ interface AgentLoopConfig {
47
57
  session?: SessionStore;
48
58
  hooks?: HookRegistry;
49
59
  injectors?: ContextInjector[];
60
+ /**
61
+ * Maps each plugin-registered injector to its plugin id so AgentLoop can
62
+ * gate injectors by personality. Built-in injectors are absent (always fire).
63
+ * Populated by PluginApiImpl.registerInjector(); passed through from wiring.
64
+ */
65
+ injectorPluginIds?: Map<ContextInjector, string>;
66
+ /**
67
+ * Base Storage instance handed to tools via `ToolContext.storage` after
68
+ * being decorated with a ScopedStorage that enforces the active
69
+ * personality's `fs_reach` allowlist. When unset, ToolContext.storage is
70
+ * left undefined and tools fall back to unrestricted node:fs (legacy
71
+ * behavior — existing CLI/TUI tests don't need a storage instance).
72
+ */
73
+ storage?: Storage;
74
+ /**
75
+ * Absolute path to ~/.ethos/ used for `${ETHOS_HOME}` substitution in
76
+ * `fs_reach` paths. Defaults to `${HOME}/.ethos`. Required only when
77
+ * `storage` is set.
78
+ */
79
+ dataDir?: string;
50
80
  modelRouting?: Record<string, string>;
51
81
  options?: {
52
82
  maxIterations?: number;
@@ -54,12 +84,37 @@ interface AgentLoopConfig {
54
84
  platform?: string;
55
85
  workingDir?: string;
56
86
  resultBudgetChars?: number;
87
+ /**
88
+ * Hard cap on total tool calls per user turn (across all LLM iterations).
89
+ * Defaults to 20. Trips a `tool_progress` warning and exits cleanly.
90
+ * See plan/IMPROVEMENT.md P1-3.
91
+ */
92
+ maxToolCallsPerTurn?: number;
93
+ /**
94
+ * Hard cap on the number of times the same tool name can be invoked in a
95
+ * single turn. Catches the "infinite loop on a single tool" failure mode
96
+ * (e.g. tts loop reported as OpenClaw #67744). Defaults to 5.
97
+ */
98
+ maxIdenticalToolCalls?: number;
99
+ /**
100
+ * Default streaming watchdog in milliseconds. If no chunk arrives from the
101
+ * LLM within this window, the agent aborts the stream and emits an error.
102
+ * Reset on every chunk. Personalities can override via
103
+ * `personality.streamingTimeoutMs`. Defaults to 120000 (2 minutes).
104
+ */
105
+ streamingTimeoutMs?: number;
57
106
  };
58
107
  }
59
108
  interface RunOptions {
60
109
  sessionKey?: string;
61
110
  personalityId?: string;
62
111
  abortSignal?: AbortSignal;
112
+ /**
113
+ * Identifier surfaced to tools as `ToolContext.agentId`. Delegation tools
114
+ * use this to thread spawn depth (`depth:N`) into child loops so
115
+ * `MAX_SPAWN_DEPTH` can be enforced across recursive sub-agent calls.
116
+ */
117
+ agentId?: string;
63
118
  }
64
119
  declare class AgentLoop {
65
120
  private readonly llm;
@@ -67,18 +122,29 @@ declare class AgentLoop {
67
122
  private readonly personalities;
68
123
  private readonly memory;
69
124
  private readonly session;
70
- private readonly hooks;
125
+ /** Public so surfaces (web, ACP) can register late-binding hooks they own
126
+ * without re-running the whole wiring factory. The CLI/TUI register hooks
127
+ * before construction; web registers an approval hook after createAgentLoop
128
+ * returns. */
129
+ readonly hooks: HookRegistry;
71
130
  private readonly injectors;
131
+ private readonly injectorPluginIds;
72
132
  private readonly maxIterations;
73
133
  private readonly historyLimit;
74
134
  private readonly platform;
75
135
  private readonly workingDir;
76
136
  private readonly resultBudgetChars;
137
+ private readonly maxToolCallsPerTurn;
138
+ private readonly maxIdenticalToolCalls;
139
+ private readonly streamingTimeoutMs;
77
140
  private readonly modelRouting;
141
+ private readonly storage?;
142
+ private readonly dataDir?;
78
143
  constructor(config: AgentLoopConfig);
79
144
  run(text: string, opts?: RunOptions): AsyncGenerator<AgentEvent>;
80
145
  private handleChunk;
81
146
  private toLLMMessages;
147
+ private buildScopedStorage;
82
148
  }
83
149
 
84
150
  declare class InMemorySessionStore implements SessionStore {
@@ -119,6 +185,7 @@ declare class DefaultPersonalityRegistry implements PersonalityRegistry {
119
185
  getDefault(): PersonalityConfig;
120
186
  setDefault(id: string): void;
121
187
  loadFromDirectory(_dir: string): Promise<void>;
188
+ remove(id: string): void;
122
189
  }
123
190
 
124
191
  declare class DefaultHookRegistry implements HookRegistry {
@@ -135,9 +202,9 @@ declare class DefaultHookRegistry implements HookRegistry {
135
202
  registerClaiming<K extends keyof ClaimingHooks>(name: K, handler: (payload: ClaimingHooks[K][0]) => Promise<ClaimingHooks[K][1]>, opts?: {
136
203
  pluginId?: string;
137
204
  }): () => void;
138
- fireVoid<K extends keyof VoidHooks>(name: K, payload: VoidHooks[K]): Promise<void>;
139
- fireModifying<K extends keyof ModifyingHooks>(name: K, payload: ModifyingHooks[K][0]): Promise<ModifyingHooks[K][1]>;
140
- fireClaiming<K extends keyof ClaimingHooks>(name: K, payload: ClaimingHooks[K][0]): Promise<ClaimingHooks[K][1]>;
205
+ fireVoid<K extends keyof VoidHooks>(name: K, payload: VoidHooks[K], allowedPlugins?: string[]): Promise<void>;
206
+ fireModifying<K extends keyof ModifyingHooks>(name: K, payload: ModifyingHooks[K][0], allowedPlugins?: string[]): Promise<ModifyingHooks[K][1]>;
207
+ fireClaiming<K extends keyof ClaimingHooks>(name: K, payload: ClaimingHooks[K][0], allowedPlugins?: string[]): Promise<ClaimingHooks[K][1]>;
141
208
  unregisterPlugin(pluginId: string): void;
142
209
  private remove;
143
210
  }
@@ -153,13 +220,15 @@ declare class PluginRegistry<T, C = unknown> {
153
220
 
154
221
  declare class DefaultToolRegistry implements ToolRegistry {
155
222
  private readonly tools;
156
- register(tool: Tool): void;
223
+ register(tool: Tool, opts?: {
224
+ pluginId?: string;
225
+ }): void;
157
226
  registerAll(tools: Tool[]): void;
158
227
  unregister(name: string): void;
159
228
  get(name: string): Tool | undefined;
160
229
  getAvailable(): Tool[];
161
230
  getForToolset(toolset: string): Tool[];
162
- toDefinitions(allowedTools?: string[]): {
231
+ toDefinitions(allowedTools?: string[], filterOpts?: ToolFilterOpts): {
163
232
  name: string;
164
233
  description: string;
165
234
  parameters: Record<string, unknown>;
@@ -168,7 +237,7 @@ declare class DefaultToolRegistry implements ToolRegistry {
168
237
  toolCallId: string;
169
238
  name: string;
170
239
  args: unknown;
171
- }>, ctx: ToolContext, allowedTools?: string[]): Promise<Array<{
240
+ }>, ctx: ToolContext, allowedTools?: string[], filterOpts?: ToolFilterOpts): Promise<Array<{
172
241
  toolCallId: string;
173
242
  name: string;
174
243
  result: ToolResult;
package/dist/index.js CHANGED
@@ -1,3 +1,8 @@
1
+ // src/agent-loop.ts
2
+ import { homedir } from "os";
3
+ import { join } from "path";
4
+ import { ScopedStorage } from "@ethosagent/storage-fs";
5
+
1
6
  // src/defaults/in-memory-session.ts
2
7
  var InMemorySessionStore = class {
3
8
  sessions = /* @__PURE__ */ new Map();
@@ -150,9 +155,17 @@ var DefaultPersonalityRegistry = class {
150
155
  }
151
156
  async loadFromDirectory(_dir) {
152
157
  }
158
+ remove(id) {
159
+ this.personalities.delete(id);
160
+ }
153
161
  };
154
162
 
155
163
  // src/hook-registry.ts
164
+ function isAllowed(h, allowedPlugins) {
165
+ if (allowedPlugins === void 0) return true;
166
+ if (!h.pluginId) return true;
167
+ return allowedPlugins.includes(h.pluginId);
168
+ }
156
169
  var DefaultHookRegistry = class {
157
170
  voidHandlers = /* @__PURE__ */ new Map();
158
171
  modifyingHandlers = /* @__PURE__ */ new Map();
@@ -192,13 +205,18 @@ var DefaultHookRegistry = class {
192
205
  }
193
206
  // Void hooks: all handlers run in parallel via Promise.allSettled.
194
207
  // Failures are logged but never propagate (fail-open by default).
195
- async fireVoid(name, payload) {
196
- const handlers = this.voidHandlers.get(name) ?? [];
208
+ // allowedPlugins gates plugin-registered handlers; built-in handlers always fire.
209
+ async fireVoid(name, payload, allowedPlugins) {
210
+ const handlers = (this.voidHandlers.get(name) ?? []).filter(
211
+ (h) => isAllowed(h, allowedPlugins)
212
+ );
197
213
  await Promise.allSettled(handlers.map((h) => h.handler(payload)));
198
214
  }
199
215
  // Modifying hooks: handlers run sequentially; results are merged (first non-null value per key wins).
200
- async fireModifying(name, payload) {
201
- const handlers = this.modifyingHandlers.get(name) ?? [];
216
+ async fireModifying(name, payload, allowedPlugins) {
217
+ const handlers = (this.modifyingHandlers.get(name) ?? []).filter(
218
+ (h) => isAllowed(h, allowedPlugins)
219
+ );
202
220
  const merged = {};
203
221
  for (const h of handlers) {
204
222
  try {
@@ -216,8 +234,10 @@ var DefaultHookRegistry = class {
216
234
  return merged;
217
235
  }
218
236
  // Claiming hooks: handlers run sequentially, stop after first { handled: true }.
219
- async fireClaiming(name, payload) {
220
- const handlers = this.claimingHandlers.get(name) ?? [];
237
+ async fireClaiming(name, payload, allowedPlugins) {
238
+ const handlers = (this.claimingHandlers.get(name) ?? []).filter(
239
+ (h) => isAllowed(h, allowedPlugins)
240
+ );
221
241
  for (const h of handlers) {
222
242
  try {
223
243
  const result = await h.handler(payload);
@@ -249,10 +269,26 @@ var DefaultHookRegistry = class {
249
269
  };
250
270
 
251
271
  // src/tool-registry.ts
272
+ function mcpServerName(toolName) {
273
+ if (!toolName.startsWith("mcp__")) return void 0;
274
+ return toolName.split("__")[1];
275
+ }
276
+ function passesFilter(entry, filterOpts) {
277
+ if (!filterOpts) return true;
278
+ const { allowedMcpServers, allowedPlugins } = filterOpts;
279
+ if (allowedMcpServers !== void 0) {
280
+ const server = mcpServerName(entry.tool.name);
281
+ if (server !== void 0 && !allowedMcpServers.includes(server)) return false;
282
+ }
283
+ if (allowedPlugins !== void 0 && entry.pluginId !== void 0) {
284
+ if (!allowedPlugins.includes(entry.pluginId)) return false;
285
+ }
286
+ return true;
287
+ }
252
288
  var DefaultToolRegistry = class {
253
289
  tools = /* @__PURE__ */ new Map();
254
- register(tool) {
255
- this.tools.set(tool.name, tool);
290
+ register(tool, opts) {
291
+ this.tools.set(tool.name, { tool, pluginId: opts?.pluginId });
256
292
  }
257
293
  registerAll(tools) {
258
294
  for (const tool of tools) {
@@ -263,27 +299,33 @@ var DefaultToolRegistry = class {
263
299
  this.tools.delete(name);
264
300
  }
265
301
  get(name) {
266
- return this.tools.get(name);
302
+ return this.tools.get(name)?.tool;
267
303
  }
268
304
  getAvailable() {
269
- return [...this.tools.values()].filter((t) => !t.isAvailable || t.isAvailable());
305
+ return [...this.tools.values()].filter((e) => !e.tool.isAvailable || e.tool.isAvailable()).map((e) => e.tool);
270
306
  }
271
307
  getForToolset(toolset) {
272
308
  return this.getAvailable().filter((t) => t.toolset === toolset);
273
309
  }
274
- toDefinitions(allowedTools) {
275
- const available = this.getAvailable();
276
- const filtered = allowedTools && allowedTools.length > 0 ? available.filter((t) => allowedTools.includes(t.name)) : available;
277
- return filtered.map((t) => ({
278
- name: t.name,
279
- description: t.description,
280
- parameters: t.schema
310
+ toDefinitions(allowedTools, filterOpts) {
311
+ const entries = [...this.tools.values()].filter(
312
+ (e) => !e.tool.isAvailable || e.tool.isAvailable()
313
+ );
314
+ const filtered = entries.filter((e) => {
315
+ if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(e.tool.name))
316
+ return false;
317
+ return passesFilter(e, filterOpts);
318
+ });
319
+ return filtered.map((e) => ({
320
+ name: e.tool.name,
321
+ description: e.tool.description,
322
+ parameters: e.tool.schema
281
323
  }));
282
324
  }
283
325
  // Runs all tool calls in parallel. Results are returned in input order.
284
326
  // Budget is split evenly across parallel calls; each result is post-trimmed to budget.
285
- // allowedTools enforces toolset at execution time (belt-and-suspenders after toDefinitions filtering).
286
- async executeParallel(calls, ctx, allowedTools) {
327
+ // allowedTools + filterOpts enforce tool access at execution time (belt-and-suspenders).
328
+ async executeParallel(calls, ctx, allowedTools, filterOpts) {
287
329
  const perCallBudget = Math.floor(ctx.resultBudgetChars / Math.max(calls.length, 1));
288
330
  const results = await Promise.allSettled(
289
331
  calls.map(async (call) => {
@@ -298,8 +340,8 @@ var DefaultToolRegistry = class {
298
340
  }
299
341
  };
300
342
  }
301
- const tool = this.tools.get(call.name);
302
- if (!tool) {
343
+ const entry = this.tools.get(call.name);
344
+ if (!entry) {
303
345
  return {
304
346
  toolCallId: call.toolCallId,
305
347
  name: call.name,
@@ -310,7 +352,18 @@ var DefaultToolRegistry = class {
310
352
  }
311
353
  };
312
354
  }
313
- if (tool.isAvailable && !tool.isAvailable()) {
355
+ if (!passesFilter(entry, filterOpts)) {
356
+ return {
357
+ toolCallId: call.toolCallId,
358
+ name: call.name,
359
+ result: {
360
+ ok: false,
361
+ error: `Tool ${call.name} is not permitted for this personality`,
362
+ code: "not_available"
363
+ }
364
+ };
365
+ }
366
+ if (entry.tool.isAvailable && !entry.tool.isAvailable()) {
314
367
  return {
315
368
  toolCallId: call.toolCallId,
316
369
  name: call.name,
@@ -321,10 +374,10 @@ var DefaultToolRegistry = class {
321
374
  }
322
375
  };
323
376
  }
324
- const budget = Math.min(perCallBudget, tool.maxResultChars ?? perCallBudget);
377
+ const budget = Math.min(perCallBudget, entry.tool.maxResultChars ?? perCallBudget);
325
378
  const toolCtx = { ...ctx, resultBudgetChars: budget };
326
379
  try {
327
- const result = await tool.execute(call.args, toolCtx);
380
+ const result = await entry.tool.execute(call.args, toolCtx);
328
381
  if (result.ok && result.value.length > budget) {
329
382
  return {
330
383
  toolCallId: call.toolCallId,
@@ -373,14 +426,24 @@ var AgentLoop = class {
373
426
  personalities;
374
427
  memory;
375
428
  session;
429
+ /** Public so surfaces (web, ACP) can register late-binding hooks they own
430
+ * without re-running the whole wiring factory. The CLI/TUI register hooks
431
+ * before construction; web registers an approval hook after createAgentLoop
432
+ * returns. */
376
433
  hooks;
377
434
  injectors;
435
+ injectorPluginIds;
378
436
  maxIterations;
379
437
  historyLimit;
380
438
  platform;
381
439
  workingDir;
382
440
  resultBudgetChars;
441
+ maxToolCallsPerTurn;
442
+ maxIdenticalToolCalls;
443
+ streamingTimeoutMs;
383
444
  modelRouting;
445
+ storage;
446
+ dataDir;
384
447
  constructor(config) {
385
448
  this.llm = config.llm;
386
449
  this.tools = config.tools ?? new DefaultToolRegistry();
@@ -389,12 +452,18 @@ var AgentLoop = class {
389
452
  this.session = config.session ?? new InMemorySessionStore();
390
453
  this.hooks = config.hooks ?? new DefaultHookRegistry();
391
454
  this.injectors = (config.injectors ?? []).sort((a, b) => b.priority - a.priority);
455
+ this.injectorPluginIds = config.injectorPluginIds ?? /* @__PURE__ */ new Map();
392
456
  this.maxIterations = config.options?.maxIterations ?? 50;
393
457
  this.historyLimit = config.options?.historyLimit ?? 200;
394
458
  this.platform = config.options?.platform ?? "cli";
395
459
  this.workingDir = config.options?.workingDir ?? process.cwd();
396
460
  this.resultBudgetChars = config.options?.resultBudgetChars ?? 8e4;
461
+ this.maxToolCallsPerTurn = config.options?.maxToolCallsPerTurn ?? 20;
462
+ this.maxIdenticalToolCalls = config.options?.maxIdenticalToolCalls ?? 5;
463
+ this.streamingTimeoutMs = config.options?.streamingTimeoutMs ?? 12e4;
397
464
  this.modelRouting = config.modelRouting ?? {};
465
+ if (config.storage) this.storage = config.storage;
466
+ if (config.dataDir) this.dataDir = config.dataDir;
398
467
  }
399
468
  async *run(text, opts = {}) {
400
469
  const abortSignal = opts.abortSignal ?? new AbortController().signal;
@@ -421,12 +490,21 @@ var AgentLoop = class {
421
490
  const effectiveModel = this.modelRouting[personality.id] ?? this.llm.model;
422
491
  const modelOverride = effectiveModel !== this.llm.model ? effectiveModel : void 0;
423
492
  const allowedTools = personality.toolset?.length ? personality.toolset : void 0;
424
- await this.hooks.fireVoid("session_start", {
425
- sessionId,
426
- sessionKey,
427
- platform: this.platform,
428
- personalityId: personality.id
429
- });
493
+ const allowedPlugins = personality.plugins ?? [];
494
+ const filterOpts = {
495
+ allowedMcpServers: personality.mcp_servers,
496
+ allowedPlugins
497
+ };
498
+ await this.hooks.fireVoid(
499
+ "session_start",
500
+ {
501
+ sessionId,
502
+ sessionKey,
503
+ platform: this.platform,
504
+ personalityId: personality.id
505
+ },
506
+ allowedPlugins
507
+ );
430
508
  await this.session.appendMessage({
431
509
  sessionId,
432
510
  role: "user",
@@ -440,6 +518,7 @@ var AgentLoop = class {
440
518
  platform: this.platform,
441
519
  workingDir: this.workingDir,
442
520
  personalityId: personality.id,
521
+ memoryScope: personality.memoryScope,
443
522
  query: text
444
523
  });
445
524
  const promptCtx = {
@@ -454,15 +533,13 @@ var AgentLoop = class {
454
533
  personalityId: personality.id
455
534
  };
456
535
  const systemParts = [];
457
- if (personality.ethosFile) {
458
- try {
459
- const fs = await import("fs/promises");
460
- const identity = await fs.readFile(personality.ethosFile, "utf-8");
461
- systemParts.push(identity.trim());
462
- } catch {
463
- }
536
+ if (personality.ethosFile && this.storage) {
537
+ const identity = await this.storage.read(personality.ethosFile);
538
+ if (identity) systemParts.push(identity.trim());
464
539
  }
465
540
  for (const injector of this.injectors) {
541
+ const injPluginId = this.injectorPluginIds.get(injector);
542
+ if (injPluginId !== void 0 && !allowedPlugins.includes(injPluginId)) continue;
466
543
  if (injector.shouldInject && !injector.shouldInject(promptCtx)) continue;
467
544
  const result = await injector.inject(promptCtx);
468
545
  if (result) {
@@ -481,11 +558,15 @@ var AgentLoop = class {
481
558
 
482
559
  ${memCtx.content}`);
483
560
  }
484
- const buildResult = await this.hooks.fireModifying("before_prompt_build", {
485
- sessionId,
486
- personalityId: personality.id,
487
- history
488
- });
561
+ const buildResult = await this.hooks.fireModifying(
562
+ "before_prompt_build",
563
+ {
564
+ sessionId,
565
+ personalityId: personality.id,
566
+ history
567
+ },
568
+ allowedPlugins
569
+ );
489
570
  if (buildResult.overrideSystem) {
490
571
  systemParts.length = 0;
491
572
  systemParts.push(buildResult.overrideSystem);
@@ -497,39 +578,103 @@ ${memCtx.content}`);
497
578
  const llmMessages = this.toLLMMessages(history);
498
579
  let fullText = "";
499
580
  let turnCount = 0;
581
+ let totalToolCalls = 0;
582
+ const toolNameCounts = /* @__PURE__ */ new Map();
500
583
  for (let iteration = 0; iteration < this.maxIterations; iteration++) {
501
584
  if (abortSignal.aborted) {
502
585
  yield { type: "error", error: "Aborted", code: "aborted" };
503
586
  return;
504
587
  }
505
- await this.hooks.fireVoid("before_llm_call", {
506
- sessionId,
507
- model: this.llm.model,
508
- turnNumber: turnCount
509
- });
588
+ if (totalToolCalls >= this.maxToolCallsPerTurn) {
589
+ yield {
590
+ type: "tool_progress",
591
+ toolName: "_budget",
592
+ message: `Stopped: hit ${this.maxToolCallsPerTurn}-tool-call budget for this turn`,
593
+ audience: "user"
594
+ };
595
+ break;
596
+ }
597
+ const overusedTool = [...toolNameCounts.entries()].find(
598
+ ([, count]) => count >= this.maxIdenticalToolCalls
599
+ );
600
+ if (overusedTool) {
601
+ yield {
602
+ type: "tool_progress",
603
+ toolName: overusedTool[0],
604
+ message: `Stopped: ${overusedTool[0]} called ${overusedTool[1]} times in one turn (likely loop)`,
605
+ audience: "user"
606
+ };
607
+ break;
608
+ }
609
+ await this.hooks.fireVoid(
610
+ "before_llm_call",
611
+ {
612
+ sessionId,
613
+ model: this.llm.model,
614
+ turnNumber: turnCount
615
+ },
616
+ allowedPlugins
617
+ );
510
618
  const pendingToolCalls = [];
511
619
  let chunkText = "";
620
+ const watchdogMs = personality.streamingTimeoutMs ?? this.streamingTimeoutMs;
621
+ const watchdogController = new AbortController();
622
+ const combinedSignal = AbortSignal.any([abortSignal, watchdogController.signal]);
623
+ let watchdogTimer;
624
+ const armWatchdog = () => {
625
+ if (watchdogTimer) clearTimeout(watchdogTimer);
626
+ watchdogTimer = setTimeout(() => watchdogController.abort(), watchdogMs);
627
+ };
628
+ const disarmWatchdog = () => {
629
+ if (watchdogTimer) clearTimeout(watchdogTimer);
630
+ watchdogTimer = void 0;
631
+ };
512
632
  try {
513
- const stream = this.llm.complete(llmMessages, this.tools.toDefinitions(allowedTools), {
633
+ armWatchdog();
634
+ const stream = this.llm.complete(llmMessages, this.tools.toDefinitions(allowedTools, filterOpts), {
514
635
  system: systemPrompt,
515
636
  cacheSystemPrompt: true,
516
- abortSignal,
637
+ abortSignal: combinedSignal,
517
638
  ...modelOverride ? { modelOverride } : {}
518
639
  });
519
640
  for await (const chunk of stream) {
520
641
  if (abortSignal.aborted) break;
642
+ if (watchdogController.signal.aborted) break;
643
+ armWatchdog();
521
644
  yield* this.handleChunk(chunk, pendingToolCalls, (t) => {
522
645
  chunkText += t;
523
646
  fullText += t;
524
647
  });
525
648
  }
649
+ disarmWatchdog();
650
+ if (watchdogController.signal.aborted && !abortSignal.aborted) {
651
+ yield {
652
+ type: "error",
653
+ error: `LLM stream stalled \u2014 no chunk for ${watchdogMs}ms`,
654
+ code: "streaming_timeout"
655
+ };
656
+ return;
657
+ }
526
658
  } catch (err) {
659
+ disarmWatchdog();
660
+ if (watchdogController.signal.aborted && !abortSignal.aborted) {
661
+ yield {
662
+ type: "error",
663
+ error: `LLM stream stalled \u2014 no chunk for ${watchdogMs}ms`,
664
+ code: "streaming_timeout"
665
+ };
666
+ return;
667
+ }
527
668
  const msg = err instanceof Error ? err.message : String(err);
528
669
  yield { type: "error", error: msg, code: "llm_error" };
529
670
  return;
530
671
  }
531
672
  turnCount++;
532
673
  const completedToolCalls = pendingToolCalls.filter((tc) => tc.args !== void 0);
674
+ totalToolCalls += completedToolCalls.length;
675
+ for (const tc of completedToolCalls) {
676
+ toolNameCounts.set(tc.toolName, (toolNameCounts.get(tc.toolName) ?? 0) + 1);
677
+ }
533
678
  await this.session.appendMessage({
534
679
  sessionId,
535
680
  role: "assistant",
@@ -542,11 +687,15 @@ ${memCtx.content}`);
542
687
  }))
543
688
  }
544
689
  });
545
- await this.hooks.fireVoid("after_llm_call", {
546
- sessionId,
547
- text: chunkText,
548
- usage: { inputTokens: 0, outputTokens: 0 }
549
- });
690
+ await this.hooks.fireVoid(
691
+ "after_llm_call",
692
+ {
693
+ sessionId,
694
+ text: chunkText,
695
+ usage: { inputTokens: 0, outputTokens: 0 }
696
+ },
697
+ allowedPlugins
698
+ );
550
699
  if (completedToolCalls.length > 0) {
551
700
  const assistantContent = [];
552
701
  if (chunkText) assistantContent.push({ type: "text", text: chunkText });
@@ -563,32 +712,50 @@ ${memCtx.content}`);
563
712
  llmMessages.push({ role: "assistant", content: chunkText });
564
713
  break;
565
714
  }
715
+ const progressBuffer = [];
716
+ const scopedStorage = this.buildScopedStorage(personality);
566
717
  const toolCtxBase = {
567
718
  sessionId,
568
719
  sessionKey,
569
720
  platform: this.platform,
570
721
  workingDir: this.workingDir,
722
+ agentId: opts.agentId,
723
+ personalityId: personality.id,
724
+ memoryScope: personality.memoryScope,
571
725
  currentTurn: turnCount,
572
726
  messageCount: allMessages.length + turnCount,
573
727
  abortSignal,
574
- emit: (_event) => {
728
+ emit: (event) => {
729
+ progressBuffer.push({
730
+ toolName: event.toolName,
731
+ message: event.message,
732
+ ...event.percent !== void 0 && { percent: event.percent },
733
+ audience: event.audience ?? "internal"
734
+ });
575
735
  },
576
- resultBudgetChars: this.resultBudgetChars
736
+ resultBudgetChars: this.resultBudgetChars,
737
+ ...scopedStorage ? { storage: scopedStorage } : {}
577
738
  };
578
739
  const prepped = [];
579
740
  for (const tc of completedToolCalls) {
580
- const beforeResult = await this.hooks.fireModifying("before_tool_call", {
581
- sessionId,
582
- toolName: tc.toolName,
583
- args: tc.args
584
- });
741
+ const beforeResult = await this.hooks.fireModifying(
742
+ "before_tool_call",
743
+ {
744
+ sessionId,
745
+ toolCallId: tc.toolCallId,
746
+ toolName: tc.toolName,
747
+ args: tc.args
748
+ },
749
+ allowedPlugins
750
+ );
585
751
  if (beforeResult.error) {
586
752
  yield {
587
753
  type: "tool_end",
588
754
  toolCallId: tc.toolCallId,
589
755
  toolName: tc.toolName,
590
756
  ok: false,
591
- durationMs: 0
757
+ durationMs: 0,
758
+ result: beforeResult.error
592
759
  };
593
760
  prepped.push({
594
761
  toolCallId: tc.toolCallId,
@@ -609,8 +776,12 @@ ${memCtx.content}`);
609
776
  }
610
777
  const execInputs = prepped.filter((p) => p.rejected === void 0).map((p) => ({ toolCallId: p.toolCallId, name: p.name, args: p.args }));
611
778
  const startedAt = Date.now();
612
- const execResults = execInputs.length > 0 ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools) : [];
779
+ const execResults = execInputs.length > 0 ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools, filterOpts) : [];
613
780
  const execResultMap = new Map(execResults.map((r) => [r.toolCallId, r]));
781
+ for (const ev of progressBuffer) {
782
+ yield { type: "tool_progress", ...ev };
783
+ }
784
+ progressBuffer.length = 0;
614
785
  const toolResultContent = [];
615
786
  for (const p of prepped) {
616
787
  const durationMs = Date.now() - startedAt;
@@ -629,14 +800,19 @@ ${memCtx.content}`);
629
800
  toolCallId: p.toolCallId,
630
801
  toolName: p.name,
631
802
  ok: result.ok,
632
- durationMs
803
+ durationMs,
804
+ result: result.ok ? result.value : result.error
633
805
  };
634
- await this.hooks.fireVoid("after_tool_call", {
635
- sessionId,
636
- toolName: p.name,
637
- result,
638
- durationMs
639
- });
806
+ await this.hooks.fireVoid(
807
+ "after_tool_call",
808
+ {
809
+ sessionId,
810
+ toolName: p.name,
811
+ result,
812
+ durationMs
813
+ },
814
+ allowedPlugins
815
+ );
640
816
  }
641
817
  await this.session.appendMessage({
642
818
  sessionId,
@@ -654,12 +830,8 @@ ${memCtx.content}`);
654
830
  }
655
831
  llmMessages.push({ role: "user", content: toolResultContent });
656
832
  }
657
- await this.memory.sync(
658
- { sessionId, sessionKey, platform: this.platform, workingDir: this.workingDir },
659
- []
660
- );
661
833
  await this.session.updateUsage(sessionId, { apiCallCount: turnCount });
662
- await this.hooks.fireVoid("agent_done", { sessionId, text: fullText, turnCount });
834
+ await this.hooks.fireVoid("agent_done", { sessionId, text: fullText, turnCount }, allowedPlugins);
663
835
  yield { type: "done", text: fullText, turnCount };
664
836
  }
665
837
  *handleChunk(chunk, pendingToolCalls, onText) {
@@ -743,7 +915,32 @@ ${memCtx.content}`);
743
915
  }
744
916
  return messages;
745
917
  }
918
+ // ---------------------------------------------------------------------------
919
+ // Per-turn ScopedStorage construction (Phase 4 — fs_reach enforcement).
920
+ //
921
+ // When the AgentLoop was wired with `storage` + `dataDir`, build a
922
+ // ScopedStorage decorated with the active personality's `fs_reach`
923
+ // allowlist for this turn. Substitutions (${ETHOS_HOME} / ${self} /
924
+ // ${CWD}) are resolved here so the underlying storage-fs class stays
925
+ // pristine. When `fs_reach` is unset, fall back to a sensible default:
926
+ // read: [<ethosHome>/personalities/<self>/, <ethosHome>/skills/, <cwd>]
927
+ // write: [<ethosHome>/personalities/<self>/, <cwd>]
928
+ // ---------------------------------------------------------------------------
929
+ buildScopedStorage(personality) {
930
+ if (!this.storage) return void 0;
931
+ const ethosHome = this.dataDir ?? join(homedir(), ".ethos");
932
+ const cwd = this.workingDir;
933
+ const self = personality.id;
934
+ const ownDir = `${join(ethosHome, "personalities", self)}/`;
935
+ const fsReach = personality.fs_reach;
936
+ const readPrefixes = fsReach?.read && fsReach.read.length > 0 ? fsReach.read.map((p) => substitute(p, { ethosHome, self, cwd })) : [ownDir, `${join(ethosHome, "skills")}/`, cwd];
937
+ const writePrefixes = fsReach?.write && fsReach.write.length > 0 ? fsReach.write.map((p) => substitute(p, { ethosHome, self, cwd })) : [ownDir, cwd];
938
+ return new ScopedStorage(this.storage, { read: readPrefixes, write: writePrefixes });
939
+ }
746
940
  };
941
+ function substitute(template, vars) {
942
+ return template.replace(/\$\{ETHOS_HOME\}/g, vars.ethosHome).replace(/\$\{self\}/g, vars.self).replace(/\$\{CWD\}/g, vars.cwd);
943
+ }
747
944
 
748
945
  // src/plugin-registry.ts
749
946
  var PluginRegistry = class {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/defaults/in-memory-session.ts","../src/defaults/noop-memory.ts","../src/defaults/noop-personality.ts","../src/hook-registry.ts","../src/tool-registry.ts","../src/agent-loop.ts","../src/plugin-registry.ts"],"sourcesContent":["import type {\n SearchResult,\n Session,\n SessionFilter,\n SessionStore,\n SessionUsage,\n StoredMessage,\n} from '@ethosagent/types';\n\nexport class InMemorySessionStore implements SessionStore {\n private sessions = new Map<string, Session>();\n private messages = new Map<string, StoredMessage[]>();\n private idCounter = 0;\n\n async createSession(data: Omit<Session, 'id' | 'createdAt' | 'updatedAt'>): Promise<Session> {\n const session: Session = {\n ...data,\n id: `session_${++this.idCounter}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n this.sessions.set(session.id, session);\n this.messages.set(session.id, []);\n return session;\n }\n\n async getSession(id: string): Promise<Session | null> {\n return this.sessions.get(id) ?? null;\n }\n\n async getSessionByKey(key: string): Promise<Session | null> {\n for (const s of this.sessions.values()) {\n if (s.key === key) return s;\n }\n return null;\n }\n\n async updateSession(id: string, patch: Partial<Session>): Promise<void> {\n const session = this.sessions.get(id);\n if (!session) throw new Error(`Session not found: ${id}`);\n this.sessions.set(id, { ...session, ...patch, updatedAt: new Date() });\n }\n\n async deleteSession(id: string): Promise<void> {\n this.sessions.delete(id);\n this.messages.delete(id);\n }\n\n async listSessions(filter?: SessionFilter): Promise<Session[]> {\n let results = [...this.sessions.values()];\n if (filter?.platform) results = results.filter((s) => s.platform === filter.platform);\n if (filter?.personalityId)\n results = results.filter((s) => s.personalityId === filter.personalityId);\n if (filter?.workingDir) results = results.filter((s) => s.workingDir === filter.workingDir);\n if (filter?.since) {\n const since = filter.since;\n results = results.filter((s) => s.createdAt >= since);\n }\n results.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());\n const offset = filter?.offset ?? 0;\n const limit = filter?.limit ?? results.length;\n return results.slice(offset, offset + limit);\n }\n\n async appendMessage(data: Omit<StoredMessage, 'id' | 'timestamp'>): Promise<StoredMessage> {\n const message: StoredMessage = {\n ...data,\n id: `msg_${++this.idCounter}`,\n timestamp: new Date(),\n };\n const list = this.messages.get(data.sessionId) ?? [];\n list.push(message);\n this.messages.set(data.sessionId, list);\n return message;\n }\n\n async getMessages(\n sessionId: string,\n options?: { limit?: number; offset?: number },\n ): Promise<StoredMessage[]> {\n const all = this.messages.get(sessionId) ?? [];\n const offset = options?.offset ?? 0;\n // Return most-recent messages: trim from the tail, then skip `offset` from the end\n const end = all.length - offset;\n const start = options?.limit ? Math.max(0, end - options.limit) : 0;\n return all.slice(start, end);\n }\n\n async updateUsage(sessionId: string, delta: Partial<SessionUsage>): Promise<void> {\n const session = this.sessions.get(sessionId);\n if (!session) return;\n const usage = { ...session.usage };\n for (const [k, v] of Object.entries(delta) as [keyof SessionUsage, number][]) {\n (usage[k] as number) += v;\n }\n this.sessions.set(sessionId, { ...session, usage, updatedAt: new Date() });\n }\n\n async search(\n query: string,\n options?: { limit?: number; sessionId?: string },\n ): Promise<SearchResult[]> {\n const results: SearchResult[] = [];\n const lower = query.toLowerCase();\n for (const [sessionId, msgs] of this.messages.entries()) {\n if (options?.sessionId && sessionId !== options.sessionId) continue;\n for (const msg of msgs) {\n const idx = msg.content.toLowerCase().indexOf(lower);\n if (idx >= 0) {\n results.push({\n sessionId,\n messageId: msg.id,\n snippet: msg.content.slice(Math.max(0, idx - 50), idx + 150),\n score: 1,\n timestamp: msg.timestamp,\n });\n }\n }\n }\n results.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());\n return results.slice(0, options?.limit ?? 20);\n }\n\n async pruneOldSessions(olderThan: Date): Promise<number> {\n let count = 0;\n for (const [id, session] of this.sessions.entries()) {\n if (session.updatedAt < olderThan) {\n this.sessions.delete(id);\n this.messages.delete(id);\n count++;\n }\n }\n return count;\n }\n\n async vacuum(): Promise<void> {\n // No-op for in-memory store\n }\n}\n","import type { MemoryLoadContext, MemoryProvider, MemoryUpdate } from '@ethosagent/types';\n\nexport class NoopMemoryProvider implements MemoryProvider {\n async prefetch(_ctx: MemoryLoadContext) {\n return null;\n }\n\n async sync(_ctx: MemoryLoadContext, _updates: MemoryUpdate[]): Promise<void> {\n // No-op\n }\n}\n","import type { PersonalityConfig, PersonalityRegistry } from '@ethosagent/types';\n\nconst DEFAULT_PERSONALITY: PersonalityConfig = {\n id: 'default',\n name: 'Default',\n description: 'Default Ethos personality',\n};\n\nexport class DefaultPersonalityRegistry implements PersonalityRegistry {\n private readonly personalities = new Map<string, PersonalityConfig>([\n ['default', DEFAULT_PERSONALITY],\n ]);\n private defaultId = 'default';\n\n define(config: PersonalityConfig): void {\n this.personalities.set(config.id, config);\n }\n\n get(id: string): PersonalityConfig | undefined {\n return this.personalities.get(id);\n }\n\n list(): PersonalityConfig[] {\n return [...this.personalities.values()];\n }\n\n getDefault(): PersonalityConfig {\n return this.personalities.get(this.defaultId) ?? DEFAULT_PERSONALITY;\n }\n\n setDefault(id: string): void {\n if (!this.personalities.has(id)) {\n throw new Error(`Unknown personality: ${id}`);\n }\n this.defaultId = id;\n }\n\n async loadFromDirectory(_dir: string): Promise<void> {\n // Implemented in extensions/personalities\n }\n}\n","import type { ClaimingHooks, HookRegistry, ModifyingHooks, VoidHooks } from '@ethosagent/types';\n\ntype AnyHandler = (...args: unknown[]) => Promise<unknown>;\n\ninterface RegisteredHandler {\n handler: AnyHandler;\n pluginId?: string;\n failurePolicy: 'fail-open' | 'fail-closed';\n}\n\nexport class DefaultHookRegistry implements HookRegistry {\n private readonly voidHandlers = new Map<string, RegisteredHandler[]>();\n private readonly modifyingHandlers = new Map<string, RegisteredHandler[]>();\n private readonly claimingHandlers = new Map<string, RegisteredHandler[]>();\n\n registerVoid<K extends keyof VoidHooks>(\n name: K,\n handler: (payload: VoidHooks[K]) => Promise<void>,\n opts?: { pluginId?: string; failurePolicy?: 'fail-open' | 'fail-closed' },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: opts?.failurePolicy ?? 'fail-open',\n };\n const list = this.voidHandlers.get(name) ?? [];\n list.push(entry);\n this.voidHandlers.set(name, list);\n return () => this.remove(this.voidHandlers, name, entry);\n }\n\n registerModifying<K extends keyof ModifyingHooks>(\n name: K,\n handler: (payload: ModifyingHooks[K][0]) => Promise<Partial<ModifyingHooks[K][1]> | null>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.modifyingHandlers.get(name) ?? [];\n list.push(entry);\n this.modifyingHandlers.set(name, list);\n return () => this.remove(this.modifyingHandlers, name, entry);\n }\n\n registerClaiming<K extends keyof ClaimingHooks>(\n name: K,\n handler: (payload: ClaimingHooks[K][0]) => Promise<ClaimingHooks[K][1]>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.claimingHandlers.get(name) ?? [];\n list.push(entry);\n this.claimingHandlers.set(name, list);\n return () => this.remove(this.claimingHandlers, name, entry);\n }\n\n // Void hooks: all handlers run in parallel via Promise.allSettled.\n // Failures are logged but never propagate (fail-open by default).\n async fireVoid<K extends keyof VoidHooks>(name: K, payload: VoidHooks[K]): Promise<void> {\n const handlers = this.voidHandlers.get(name) ?? [];\n await Promise.allSettled(handlers.map((h) => h.handler(payload)));\n }\n\n // Modifying hooks: handlers run sequentially; results are merged (first non-null value per key wins).\n async fireModifying<K extends keyof ModifyingHooks>(\n name: K,\n payload: ModifyingHooks[K][0],\n ): Promise<ModifyingHooks[K][1]> {\n const handlers = this.modifyingHandlers.get(name) ?? [];\n const merged: Record<string, unknown> = {};\n for (const h of handlers) {\n try {\n const result = await h.handler(payload);\n if (result && typeof result === 'object') {\n for (const [k, v] of Object.entries(result)) {\n if (!(k in merged) && v !== null && v !== undefined) {\n merged[k] = v;\n }\n }\n }\n } catch {\n // fail-open: continue with other handlers\n }\n }\n return merged as ModifyingHooks[K][1];\n }\n\n // Claiming hooks: handlers run sequentially, stop after first { handled: true }.\n async fireClaiming<K extends keyof ClaimingHooks>(\n name: K,\n payload: ClaimingHooks[K][0],\n ): Promise<ClaimingHooks[K][1]> {\n const handlers = this.claimingHandlers.get(name) ?? [];\n for (const h of handlers) {\n try {\n const result = (await h.handler(payload)) as ClaimingHooks[K][1];\n if (result && (result as { handled: boolean }).handled) {\n return result;\n }\n } catch {\n // fail-open: try next handler\n }\n }\n return { handled: false } as ClaimingHooks[K][1];\n }\n\n unregisterPlugin(pluginId: string): void {\n for (const map of [this.voidHandlers, this.modifyingHandlers, this.claimingHandlers]) {\n for (const [name, handlers] of map.entries()) {\n map.set(\n name,\n handlers.filter((h) => h.pluginId !== pluginId),\n );\n }\n }\n }\n\n private remove(\n map: Map<string, RegisteredHandler[]>,\n name: string,\n entry: RegisteredHandler,\n ): void {\n const list = map.get(name) ?? [];\n map.set(\n name,\n list.filter((h) => h !== entry),\n );\n }\n}\n","import type { Tool, ToolContext, ToolRegistry, ToolResult } from '@ethosagent/types';\n\nexport class DefaultToolRegistry implements ToolRegistry {\n private readonly tools = new Map<string, Tool>();\n\n register(tool: Tool): void {\n this.tools.set(tool.name, tool);\n }\n\n registerAll(tools: Tool[]): void {\n for (const tool of tools) {\n this.register(tool);\n }\n }\n\n unregister(name: string): void {\n this.tools.delete(name);\n }\n\n get(name: string): Tool | undefined {\n return this.tools.get(name);\n }\n\n getAvailable(): Tool[] {\n return [...this.tools.values()].filter((t) => !t.isAvailable || t.isAvailable());\n }\n\n getForToolset(toolset: string): Tool[] {\n return this.getAvailable().filter((t) => t.toolset === toolset);\n }\n\n toDefinitions(allowedTools?: string[]) {\n const available = this.getAvailable();\n const filtered =\n allowedTools && allowedTools.length > 0\n ? available.filter((t) => allowedTools.includes(t.name))\n : available;\n return filtered.map((t) => ({\n name: t.name,\n description: t.description,\n parameters: t.schema,\n }));\n }\n\n // Runs all tool calls in parallel. Results are returned in input order.\n // Budget is split evenly across parallel calls; each result is post-trimmed to budget.\n // allowedTools enforces toolset at execution time (belt-and-suspenders after toDefinitions filtering).\n async executeParallel(\n calls: Array<{ toolCallId: string; name: string; args: unknown }>,\n ctx: ToolContext,\n allowedTools?: string[],\n ): Promise<Array<{ toolCallId: string; name: string; result: ToolResult }>> {\n const perCallBudget = Math.floor(ctx.resultBudgetChars / Math.max(calls.length, 1));\n\n const results = await Promise.allSettled(\n calls.map(async (call) => {\n if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(call.name)) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not permitted for this personality`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const tool = this.tools.get(call.name);\n if (!tool) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Unknown tool: ${call.name}`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n if (tool.isAvailable && !tool.isAvailable()) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not currently available`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const budget = Math.min(perCallBudget, tool.maxResultChars ?? perCallBudget);\n const toolCtx: ToolContext = { ...ctx, resultBudgetChars: budget };\n\n try {\n const result = await tool.execute(call.args, toolCtx);\n // Post-trim result to budget\n if (result.ok && result.value.length > budget) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: true,\n value: `${result.value.slice(0, budget)}\\n[truncated — ${result.value.length} chars total]`,\n } as ToolResult,\n };\n }\n return { toolCallId: call.toolCallId, name: call.name, result };\n } catch (err) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: err instanceof Error ? err.message : String(err),\n code: 'execution_failed',\n } as ToolResult,\n };\n }\n }),\n );\n\n // Unwrap settled results — always return, never throw\n return results.map((r, i) => {\n if (r.status === 'fulfilled') return r.value;\n const call = calls[i] ?? { toolCallId: 'unknown', name: 'unknown', args: {} };\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: String(r.reason),\n code: 'execution_failed',\n } as ToolResult,\n };\n });\n }\n}\n","import type {\n CompletionChunk,\n ContextInjector,\n HookRegistry,\n LLMProvider,\n MemoryProvider,\n Message,\n MessageContent,\n PersonalityRegistry,\n PromptContext,\n SessionStore,\n StoredMessage,\n ToolRegistry,\n ToolResult,\n} from '@ethosagent/types';\n\nimport { InMemorySessionStore } from './defaults/in-memory-session';\nimport { NoopMemoryProvider } from './defaults/noop-memory';\nimport { DefaultPersonalityRegistry } from './defaults/noop-personality';\nimport { DefaultHookRegistry } from './hook-registry';\nimport { DefaultToolRegistry } from './tool-registry';\n\n// ---------------------------------------------------------------------------\n// Agent events emitted by run()\n// ---------------------------------------------------------------------------\n\nexport type AgentEvent =\n | { type: 'text_delta'; text: string }\n | { type: 'thinking_delta'; thinking: string }\n | { type: 'tool_start'; toolCallId: string; toolName: string; args: unknown }\n | { type: 'tool_progress'; toolName: string; message: string; percent?: number }\n | { type: 'tool_end'; toolCallId: string; toolName: string; ok: boolean; durationMs: number }\n | { type: 'usage'; inputTokens: number; outputTokens: number; estimatedCostUsd: number }\n | { type: 'error'; error: string; code: string }\n | { type: 'done'; text: string; turnCount: number }\n // Emitted once after context injectors run; carries any metadata they wrote to PromptContext.meta.\n | { type: 'context_meta'; data: Record<string, unknown> };\n\n// ---------------------------------------------------------------------------\n// Config\n// ---------------------------------------------------------------------------\n\nexport interface AgentLoopConfig {\n llm: LLMProvider;\n tools?: ToolRegistry;\n personalities?: PersonalityRegistry;\n memory?: MemoryProvider;\n session?: SessionStore;\n hooks?: HookRegistry;\n injectors?: ContextInjector[];\n // Maps personality ID → model ID. Resolution: modelRouting[id] → personality.model → llm.model\n modelRouting?: Record<string, string>;\n options?: {\n maxIterations?: number;\n historyLimit?: number;\n platform?: string;\n workingDir?: string;\n resultBudgetChars?: number;\n };\n}\n\nexport interface RunOptions {\n sessionKey?: string;\n personalityId?: string;\n abortSignal?: AbortSignal;\n}\n\n// ---------------------------------------------------------------------------\n// AgentLoop\n// ---------------------------------------------------------------------------\n\nexport class AgentLoop {\n private readonly llm: LLMProvider;\n private readonly tools: ToolRegistry;\n private readonly personalities: PersonalityRegistry;\n private readonly memory: MemoryProvider;\n private readonly session: SessionStore;\n private readonly hooks: HookRegistry;\n private readonly injectors: ContextInjector[];\n private readonly maxIterations: number;\n private readonly historyLimit: number;\n private readonly platform: string;\n private readonly workingDir: string;\n private readonly resultBudgetChars: number;\n private readonly modelRouting: Record<string, string>;\n\n constructor(config: AgentLoopConfig) {\n this.llm = config.llm;\n this.tools = config.tools ?? new DefaultToolRegistry();\n this.personalities = config.personalities ?? new DefaultPersonalityRegistry();\n this.memory = config.memory ?? new NoopMemoryProvider();\n this.session = config.session ?? new InMemorySessionStore();\n this.hooks = config.hooks ?? new DefaultHookRegistry();\n this.injectors = (config.injectors ?? []).sort((a, b) => b.priority - a.priority);\n this.maxIterations = config.options?.maxIterations ?? 50;\n this.historyLimit = config.options?.historyLimit ?? 200;\n this.platform = config.options?.platform ?? 'cli';\n this.workingDir = config.options?.workingDir ?? process.cwd();\n this.resultBudgetChars = config.options?.resultBudgetChars ?? 80_000;\n this.modelRouting = config.modelRouting ?? {};\n }\n\n async *run(text: string, opts: RunOptions = {}): AsyncGenerator<AgentEvent> {\n const abortSignal = opts.abortSignal ?? new AbortController().signal;\n const sessionKey = opts.sessionKey ?? `${this.platform}:default`;\n\n // Step 1: Resolve or create session\n const ethosSession =\n (await this.session.getSessionByKey(sessionKey)) ??\n (await this.session.createSession({\n key: sessionKey,\n platform: this.platform,\n model: this.llm.model,\n provider: this.llm.name,\n personalityId: opts.personalityId,\n workingDir: this.workingDir,\n usage: {\n inputTokens: 0,\n outputTokens: 0,\n cacheReadTokens: 0,\n cacheCreationTokens: 0,\n estimatedCostUsd: 0,\n apiCallCount: 0,\n compactionCount: 0,\n },\n }));\n\n const sessionId = ethosSession.id;\n const personality =\n (opts.personalityId ? this.personalities.get(opts.personalityId) : null) ??\n this.personalities.getDefault();\n\n // Resolve effective model: explicit per-personality routing > LLM base model.\n // personality.model is intentionally skipped — those IDs are Anthropic-specific\n // and break non-Anthropic providers (OpenRouter, Gemini, Ollama, etc.).\n // Configure overrides via modelRouting in ~/.ethos/config.yaml instead.\n const effectiveModel = this.modelRouting[personality.id] ?? this.llm.model;\n const modelOverride = effectiveModel !== this.llm.model ? effectiveModel : undefined;\n // Allowed tool names for this personality (undefined = no restriction)\n const allowedTools = personality.toolset?.length ? personality.toolset : undefined;\n\n // Step 2: Fire session_start hooks\n await this.hooks.fireVoid('session_start', {\n sessionId,\n sessionKey,\n platform: this.platform,\n personalityId: personality.id,\n });\n\n // Step 3: Persist the user message\n await this.session.appendMessage({\n sessionId,\n role: 'user',\n content: text,\n });\n\n // Step 4: Load history (trimmed to most-recent limit)\n const allMessages = await this.session.getMessages(sessionId, { limit: this.historyLimit });\n const history = allMessages.filter((m) => m.role !== 'system');\n\n // Step 5: Prefetch memory\n const memCtx = await this.memory.prefetch({\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n personalityId: personality.id,\n query: text,\n });\n\n // Step 6: Build system prompt from injectors\n const promptCtx: PromptContext = {\n sessionId,\n sessionKey,\n platform: this.platform,\n model: this.llm.model,\n history,\n workingDir: this.workingDir,\n isDm: true,\n turnNumber: allMessages.length,\n personalityId: personality.id,\n };\n\n const systemParts: string[] = [];\n\n // ETHOS.md / personality identity\n if (personality.ethosFile) {\n try {\n const fs = await import('node:fs/promises');\n const identity = await fs.readFile(personality.ethosFile, 'utf-8');\n systemParts.push(identity.trim());\n } catch {\n // ethosFile not readable — skip\n }\n }\n\n // Context injectors sorted by priority (already sorted in constructor)\n for (const injector of this.injectors) {\n if (injector.shouldInject && !injector.shouldInject(promptCtx)) continue;\n const result = await injector.inject(promptCtx);\n if (result) {\n if (result.position === 'prepend') {\n systemParts.unshift(result.content);\n } else {\n systemParts.push(result.content);\n }\n }\n }\n\n // Emit injector metadata (e.g. skill_files_used) so eval harness can capture it.\n if (promptCtx.meta && Object.keys(promptCtx.meta).length > 0) {\n yield { type: 'context_meta', data: promptCtx.meta };\n }\n\n // Memory injected last, as context about the user\n if (memCtx) {\n systemParts.push(`## Memory\\n\\n${memCtx.content}`);\n }\n\n // Step 7: Before-prompt-build modifying hooks (plugins can prepend/append/override)\n const buildResult = await this.hooks.fireModifying('before_prompt_build', {\n sessionId,\n personalityId: personality.id,\n history,\n });\n\n if (buildResult.overrideSystem) {\n systemParts.length = 0;\n systemParts.push(buildResult.overrideSystem);\n } else {\n if (buildResult.prependSystem) systemParts.unshift(buildResult.prependSystem);\n if (buildResult.appendSystem) systemParts.push(buildResult.appendSystem);\n }\n\n const systemPrompt = systemParts.join('\\n\\n').trim() || undefined;\n\n // Step 8: Agentic loop — LLM call → tool use → LLM call → ...\n const llmMessages = this.toLLMMessages(history);\n let fullText = '';\n let turnCount = 0;\n\n for (let iteration = 0; iteration < this.maxIterations; iteration++) {\n if (abortSignal.aborted) {\n yield { type: 'error', error: 'Aborted', code: 'aborted' };\n return;\n }\n\n // Fire before_llm_call\n await this.hooks.fireVoid('before_llm_call', {\n sessionId,\n model: this.llm.model,\n turnNumber: turnCount,\n });\n\n // Stream LLM response\n const pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }> = [];\n let chunkText = '';\n\n try {\n const stream = this.llm.complete(llmMessages, this.tools.toDefinitions(allowedTools), {\n system: systemPrompt,\n cacheSystemPrompt: true,\n abortSignal,\n ...(modelOverride ? { modelOverride } : {}),\n });\n\n for await (const chunk of stream) {\n if (abortSignal.aborted) break;\n yield* this.handleChunk(chunk, pendingToolCalls, (t) => {\n chunkText += t;\n fullText += t;\n });\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n yield { type: 'error', error: msg, code: 'llm_error' };\n return;\n }\n\n turnCount++;\n\n // Determine which tool calls completed parsing\n const completedToolCalls = pendingToolCalls.filter((tc) => tc.args !== undefined);\n\n // Persist assistant message — include tool_use references so history is LLM-replayable\n await this.session.appendMessage({\n sessionId,\n role: 'assistant',\n content: chunkText,\n ...(completedToolCalls.length > 0 && {\n toolCalls: completedToolCalls.map((tc) => ({\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n })),\n }),\n });\n\n // Fire after_llm_call\n await this.hooks.fireVoid('after_llm_call', {\n sessionId,\n text: chunkText,\n usage: { inputTokens: 0, outputTokens: 0 },\n });\n\n // Push assistant message with proper content blocks for next iteration\n if (completedToolCalls.length > 0) {\n const assistantContent: MessageContent[] = [];\n if (chunkText) assistantContent.push({ type: 'text', text: chunkText });\n for (const tc of completedToolCalls) {\n assistantContent.push({\n type: 'tool_use',\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n });\n }\n llmMessages.push({ role: 'assistant', content: assistantContent });\n } else {\n llmMessages.push({ role: 'assistant', content: chunkText });\n break;\n }\n\n // Step 9: Pre-flight hooks → execute non-rejected tools → collect all results\n const toolCtxBase = {\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n currentTurn: turnCount,\n messageCount: allMessages.length + turnCount,\n abortSignal,\n emit: (_event: {\n type: 'progress';\n toolName: string;\n message: string;\n percent?: number;\n }) => {\n // Progress emission wired in Phase 6 (terminal streaming)\n },\n resultBudgetChars: this.resultBudgetChars,\n };\n\n // Run before_tool_call hooks; build exec list with effective args\n // Rejected tools get tool_end ok:false + an error tool_result sent back to LLM\n type Prepped = { toolCallId: string; name: string; args: unknown; rejected?: string };\n const prepped: Prepped[] = [];\n\n for (const tc of completedToolCalls) {\n const beforeResult = await this.hooks.fireModifying('before_tool_call', {\n sessionId,\n toolName: tc.toolName,\n args: tc.args,\n });\n\n if (beforeResult.error) {\n yield {\n type: 'tool_end',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n ok: false,\n durationMs: 0,\n };\n prepped.push({\n toolCallId: tc.toolCallId,\n name: tc.toolName,\n args: tc.args,\n rejected: beforeResult.error,\n });\n continue;\n }\n\n const effectiveArgs = beforeResult.args ?? tc.args;\n yield {\n type: 'tool_start',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n args: effectiveArgs,\n };\n prepped.push({ toolCallId: tc.toolCallId, name: tc.toolName, args: effectiveArgs });\n }\n\n // Execute only non-rejected tools; results keyed by toolCallId\n const execInputs = prepped\n .filter((p) => p.rejected === undefined)\n .map((p) => ({ toolCallId: p.toolCallId, name: p.name, args: p.args }));\n\n const startedAt = Date.now();\n const execResults =\n execInputs.length > 0\n ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools)\n : [];\n const execResultMap = new Map(execResults.map((r) => [r.toolCallId, r]));\n\n // Persist results + emit tool_end + build tool_result content blocks (original order)\n const toolResultContent: MessageContent[] = [];\n\n for (const p of prepped) {\n const durationMs = Date.now() - startedAt;\n let result: ToolResult;\n\n if (p.rejected !== undefined) {\n result = { ok: false, error: p.rejected, code: 'execution_failed' };\n // tool_end already emitted above; no after_tool_call hook for blocked tools\n } else {\n const execResult = execResultMap.get(p.toolCallId);\n result = execResult?.result ?? {\n ok: false,\n error: 'Tool result missing',\n code: 'execution_failed',\n };\n yield {\n type: 'tool_end',\n toolCallId: p.toolCallId,\n toolName: p.name,\n ok: result.ok,\n durationMs,\n };\n await this.hooks.fireVoid('after_tool_call', {\n sessionId,\n toolName: p.name,\n result,\n durationMs,\n });\n }\n\n // Persist every result (rejected or not) so history matches what LLM sees\n await this.session.appendMessage({\n sessionId,\n role: 'tool_result',\n content: result.ok ? result.value : result.error,\n toolCallId: p.toolCallId,\n toolName: p.name,\n });\n\n toolResultContent.push({\n type: 'tool_result',\n tool_use_id: p.toolCallId,\n content: result.ok ? result.value : result.error,\n is_error: !result.ok,\n });\n }\n\n // Feed all tool results back to LLM as a single user message with content blocks\n llmMessages.push({ role: 'user', content: toolResultContent });\n }\n\n // Step 10: Sync memory\n await this.memory.sync(\n { sessionId, sessionKey, platform: this.platform, workingDir: this.workingDir },\n [],\n );\n\n // Step 11: Update usage\n await this.session.updateUsage(sessionId, { apiCallCount: turnCount });\n\n // Step 12: Fire agent_done\n await this.hooks.fireVoid('agent_done', { sessionId, text: fullText, turnCount });\n\n yield { type: 'done', text: fullText, turnCount };\n }\n\n private *handleChunk(\n chunk: CompletionChunk,\n pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }>,\n onText: (t: string) => void,\n ): Generator<AgentEvent> {\n switch (chunk.type) {\n case 'text_delta':\n onText(chunk.text);\n yield { type: 'text_delta', text: chunk.text };\n break;\n\n case 'thinking_delta':\n yield { type: 'thinking_delta', thinking: chunk.thinking };\n break;\n\n case 'tool_use_start':\n pendingToolCalls.push({\n toolCallId: chunk.toolCallId,\n toolName: chunk.toolName,\n partialJson: '',\n });\n break;\n\n case 'tool_use_delta': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) tc.partialJson += chunk.partialJson;\n break;\n }\n\n case 'tool_use_end': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) {\n try {\n tc.args = JSON.parse(chunk.inputJson || tc.partialJson);\n } catch {\n tc.args = {};\n }\n }\n break;\n }\n\n case 'usage':\n yield {\n type: 'usage',\n inputTokens: chunk.usage.inputTokens,\n outputTokens: chunk.usage.outputTokens,\n estimatedCostUsd: chunk.usage.estimatedCostUsd,\n };\n break;\n\n case 'done':\n // finishReason available here for future context-compaction (Phase 3)\n break;\n }\n }\n\n // Reconstruct LLM-ready messages from stored history.\n // Assistant messages with tool calls produce proper tool_use content blocks.\n // Consecutive tool_result rows are grouped into a single user message.\n private toLLMMessages(stored: StoredMessage[]): Message[] {\n const messages: Message[] = [];\n\n for (const msg of stored) {\n if (msg.role === 'system') continue;\n\n if (msg.role === 'user') {\n messages.push({ role: 'user', content: msg.content });\n } else if (msg.role === 'assistant') {\n if (msg.toolCalls && msg.toolCalls.length > 0) {\n const content: MessageContent[] = [];\n if (msg.content) content.push({ type: 'text', text: msg.content });\n for (const tc of msg.toolCalls) {\n content.push({ type: 'tool_use', id: tc.id, name: tc.name, input: tc.input });\n }\n messages.push({ role: 'assistant', content });\n } else {\n messages.push({ role: 'assistant', content: msg.content });\n }\n } else if (msg.role === 'tool_result') {\n const resultBlock: MessageContent = {\n type: 'tool_result',\n tool_use_id: msg.toolCallId ?? '',\n content: msg.content,\n is_error: false,\n };\n const last = messages[messages.length - 1];\n // Append to existing tool_result user message, or start a new one\n if (last?.role === 'user' && Array.isArray(last.content)) {\n (last.content as MessageContent[]).push(resultBlock);\n } else {\n messages.push({ role: 'user', content: [resultBlock] });\n }\n }\n }\n\n return messages;\n }\n}\n","// Generic plugin registry — adapted from praxis PluginRegistry pattern.\n// Each subsystem (tools, channels, memory backends) gets its own instance.\n\nexport type PluginFactory<T, C = unknown> = (config: C) => T | null;\n\nexport class PluginRegistry<T, C = unknown> {\n private readonly factories = new Map<string, PluginFactory<T, C>>();\n\n register(type: string, factory: PluginFactory<T, C>): void {\n this.factories.set(type, factory);\n }\n\n create(type: string, config: C): T {\n const factory = this.factories.get(type);\n if (!factory) {\n throw new Error(\n `Unknown plugin type: \"${type}\". Registered: ${[...this.factories.keys()].join(', ')}`,\n );\n }\n const instance = factory(config);\n if (!instance) {\n throw new Error(`Plugin factory for \"${type}\" returned null`);\n }\n return instance;\n }\n\n has(type: string): boolean {\n return this.factories.has(type);\n }\n\n types(): string[] {\n return [...this.factories.keys()];\n }\n}\n"],"mappings":";AASO,IAAM,uBAAN,MAAmD;AAAA,EAChD,WAAW,oBAAI,IAAqB;AAAA,EACpC,WAAW,oBAAI,IAA6B;AAAA,EAC5C,YAAY;AAAA,EAEpB,MAAM,cAAc,MAAyE;AAC3F,UAAM,UAAmB;AAAA,MACvB,GAAG;AAAA,MACH,IAAI,WAAW,EAAE,KAAK,SAAS;AAAA,MAC/B,WAAW,oBAAI,KAAK;AAAA,MACpB,WAAW,oBAAI,KAAK;AAAA,IACtB;AACA,SAAK,SAAS,IAAI,QAAQ,IAAI,OAAO;AACrC,SAAK,SAAS,IAAI,QAAQ,IAAI,CAAC,CAAC;AAChC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,WAAW,IAAqC;AACpD,WAAO,KAAK,SAAS,IAAI,EAAE,KAAK;AAAA,EAClC;AAAA,EAEA,MAAM,gBAAgB,KAAsC;AAC1D,eAAW,KAAK,KAAK,SAAS,OAAO,GAAG;AACtC,UAAI,EAAE,QAAQ,IAAK,QAAO;AAAA,IAC5B;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,cAAc,IAAY,OAAwC;AACtE,UAAM,UAAU,KAAK,SAAS,IAAI,EAAE;AACpC,QAAI,CAAC,QAAS,OAAM,IAAI,MAAM,sBAAsB,EAAE,EAAE;AACxD,SAAK,SAAS,IAAI,IAAI,EAAE,GAAG,SAAS,GAAG,OAAO,WAAW,oBAAI,KAAK,EAAE,CAAC;AAAA,EACvE;AAAA,EAEA,MAAM,cAAc,IAA2B;AAC7C,SAAK,SAAS,OAAO,EAAE;AACvB,SAAK,SAAS,OAAO,EAAE;A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,KAAK,CAAC;AAAA,EAClC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/agent-loop.ts","../src/defaults/in-memory-session.ts","../src/defaults/noop-memory.ts","../src/defaults/noop-personality.ts","../src/hook-registry.ts","../src/tool-registry.ts","../src/plugin-registry.ts"],"sourcesContent":["import { homedir } from 'node:os';\nimport { join } from 'node:path';\nimport { ScopedStorage } from '@ethosagent/storage-fs';\nimport type {\n CompletionChunk,\n ContextInjector,\n HookRegistry,\n LLMProvider,\n MemoryProvider,\n Message,\n MessageContent,\n PersonalityConfig,\n PersonalityRegistry,\n PromptContext,\n SessionStore,\n Storage,\n StoredMessage,\n ToolFilterOpts,\n ToolRegistry,\n ToolResult,\n} from '@ethosagent/types';\n\nimport { InMemorySessionStore } from './defaults/in-memory-session';\nimport { NoopMemoryProvider } from './defaults/noop-memory';\nimport { DefaultPersonalityRegistry } from './defaults/noop-personality';\nimport { DefaultHookRegistry } from './hook-registry';\nimport { DefaultToolRegistry } from './tool-registry';\n\n// ---------------------------------------------------------------------------\n// Agent events emitted by run()\n// ---------------------------------------------------------------------------\n\nexport type AgentEvent =\n | { type: 'text_delta'; text: string }\n | { type: 'thinking_delta'; thinking: string }\n | { type: 'tool_start'; toolCallId: string; toolName: string; args: unknown }\n // Phase 30.2 — `audience` gates whether channel adapters / chat.ts surface\n // this event to the user. Default is `'internal'`; tools opt in to `'user'`\n // per event. Framework-emitted budget warnings are `'user'` (see step 7).\n | {\n type: 'tool_progress';\n toolName: string;\n message: string;\n percent?: number;\n audience: 'internal' | 'user';\n }\n | {\n type: 'tool_end';\n toolCallId: string;\n toolName: string;\n ok: boolean;\n durationMs: number;\n // Phase 30.2 — same boundary applies to tool_end success rendering.\n // Failures (`ok: false`) ignore the field and always render.\n audience?: 'internal' | 'user';\n /**\n * Tool output body — the success value when `ok`, or the error\n * message when `ok: false`. Optional so consumers that only care\n * about the status (CLI ASCII chips, telemetry) can ignore it.\n * The web chip surfaces this on expand-on-click without a\n * follow-up history fetch.\n */\n result?: string;\n }\n | { type: 'usage'; inputTokens: number; outputTokens: number; estimatedCostUsd: number }\n | { type: 'error'; error: string; code: string }\n | { type: 'done'; text: string; turnCount: number }\n // Emitted once after context injectors run; carries any metadata they wrote to PromptContext.meta.\n | { type: 'context_meta'; data: Record<string, unknown> };\n\n// ---------------------------------------------------------------------------\n// Config\n// ---------------------------------------------------------------------------\n\nexport interface AgentLoopConfig {\n llm: LLMProvider;\n tools?: ToolRegistry;\n personalities?: PersonalityRegistry;\n memory?: MemoryProvider;\n session?: SessionStore;\n hooks?: HookRegistry;\n injectors?: ContextInjector[];\n /**\n * Maps each plugin-registered injector to its plugin id so AgentLoop can\n * gate injectors by personality. Built-in injectors are absent (always fire).\n * Populated by PluginApiImpl.registerInjector(); passed through from wiring.\n */\n injectorPluginIds?: Map<ContextInjector, string>;\n /**\n * Base Storage instance handed to tools via `ToolContext.storage` after\n * being decorated with a ScopedStorage that enforces the active\n * personality's `fs_reach` allowlist. When unset, ToolContext.storage is\n * left undefined and tools fall back to unrestricted node:fs (legacy\n * behavior — existing CLI/TUI tests don't need a storage instance).\n */\n storage?: Storage;\n /**\n * Absolute path to ~/.ethos/ used for `${ETHOS_HOME}` substitution in\n * `fs_reach` paths. Defaults to `${HOME}/.ethos`. Required only when\n * `storage` is set.\n */\n dataDir?: string;\n // Maps personality ID → model ID. Resolution: modelRouting[id] → personality.model → llm.model\n modelRouting?: Record<string, string>;\n options?: {\n maxIterations?: number;\n historyLimit?: number;\n platform?: string;\n workingDir?: string;\n resultBudgetChars?: number;\n /**\n * Hard cap on total tool calls per user turn (across all LLM iterations).\n * Defaults to 20. Trips a `tool_progress` warning and exits cleanly.\n * See plan/IMPROVEMENT.md P1-3.\n */\n maxToolCallsPerTurn?: number;\n /**\n * Hard cap on the number of times the same tool name can be invoked in a\n * single turn. Catches the \"infinite loop on a single tool\" failure mode\n * (e.g. tts loop reported as OpenClaw #67744). Defaults to 5.\n */\n maxIdenticalToolCalls?: number;\n /**\n * Default streaming watchdog in milliseconds. If no chunk arrives from the\n * LLM within this window, the agent aborts the stream and emits an error.\n * Reset on every chunk. Personalities can override via\n * `personality.streamingTimeoutMs`. Defaults to 120000 (2 minutes).\n */\n streamingTimeoutMs?: number;\n };\n}\n\nexport interface RunOptions {\n sessionKey?: string;\n personalityId?: string;\n abortSignal?: AbortSignal;\n /**\n * Identifier surfaced to tools as `ToolContext.agentId`. Delegation tools\n * use this to thread spawn depth (`depth:N`) into child loops so\n * `MAX_SPAWN_DEPTH` can be enforced across recursive sub-agent calls.\n */\n agentId?: string;\n}\n\n// ---------------------------------------------------------------------------\n// AgentLoop\n// ---------------------------------------------------------------------------\n\nexport class AgentLoop {\n private readonly llm: LLMProvider;\n private readonly tools: ToolRegistry;\n private readonly personalities: PersonalityRegistry;\n private readonly memory: MemoryProvider;\n private readonly session: SessionStore;\n /** Public so surfaces (web, ACP) can register late-binding hooks they own\n * without re-running the whole wiring factory. The CLI/TUI register hooks\n * before construction; web registers an approval hook after createAgentLoop\n * returns. */\n readonly hooks: HookRegistry;\n private readonly injectors: ContextInjector[];\n private readonly injectorPluginIds: Map<ContextInjector, string>;\n private readonly maxIterations: number;\n private readonly historyLimit: number;\n private readonly platform: string;\n private readonly workingDir: string;\n private readonly resultBudgetChars: number;\n private readonly maxToolCallsPerTurn: number;\n private readonly maxIdenticalToolCalls: number;\n private readonly streamingTimeoutMs: number;\n private readonly modelRouting: Record<string, string>;\n private readonly storage?: Storage;\n private readonly dataDir?: string;\n\n constructor(config: AgentLoopConfig) {\n this.llm = config.llm;\n this.tools = config.tools ?? new DefaultToolRegistry();\n this.personalities = config.personalities ?? new DefaultPersonalityRegistry();\n this.memory = config.memory ?? new NoopMemoryProvider();\n this.session = config.session ?? new InMemorySessionStore();\n this.hooks = config.hooks ?? new DefaultHookRegistry();\n this.injectors = (config.injectors ?? []).sort((a, b) => b.priority - a.priority);\n this.injectorPluginIds = config.injectorPluginIds ?? new Map();\n this.maxIterations = config.options?.maxIterations ?? 50;\n this.historyLimit = config.options?.historyLimit ?? 200;\n this.platform = config.options?.platform ?? 'cli';\n this.workingDir = config.options?.workingDir ?? process.cwd();\n this.resultBudgetChars = config.options?.resultBudgetChars ?? 80_000;\n this.maxToolCallsPerTurn = config.options?.maxToolCallsPerTurn ?? 20;\n this.maxIdenticalToolCalls = config.options?.maxIdenticalToolCalls ?? 5;\n this.streamingTimeoutMs = config.options?.streamingTimeoutMs ?? 120_000;\n this.modelRouting = config.modelRouting ?? {};\n if (config.storage) this.storage = config.storage;\n if (config.dataDir) this.dataDir = config.dataDir;\n }\n\n async *run(text: string, opts: RunOptions = {}): AsyncGenerator<AgentEvent> {\n const abortSignal = opts.abortSignal ?? new AbortController().signal;\n const sessionKey = opts.sessionKey ?? `${this.platform}:default`;\n\n // Step 1: Resolve or create session\n const ethosSession =\n (await this.session.getSessionByKey(sessionKey)) ??\n (await this.session.createSession({\n key: sessionKey,\n platform: this.platform,\n model: this.llm.model,\n provider: this.llm.name,\n personalityId: opts.personalityId,\n workingDir: this.workingDir,\n usage: {\n inputTokens: 0,\n outputTokens: 0,\n cacheReadTokens: 0,\n cacheCreationTokens: 0,\n estimatedCostUsd: 0,\n apiCallCount: 0,\n compactionCount: 0,\n },\n }));\n\n const sessionId = ethosSession.id;\n const personality =\n (opts.personalityId ? this.personalities.get(opts.personalityId) : null) ??\n this.personalities.getDefault();\n\n // Resolve effective model: explicit per-personality routing > LLM base model.\n // personality.model is intentionally skipped — those IDs are Anthropic-specific\n // and break non-Anthropic providers (OpenRouter, Gemini, Ollama, etc.).\n // Configure overrides via modelRouting in ~/.ethos/config.yaml instead.\n const effectiveModel = this.modelRouting[personality.id] ?? this.llm.model;\n const modelOverride = effectiveModel !== this.llm.model ? effectiveModel : undefined;\n // Allowed tool names for this personality (undefined = no restriction)\n const allowedTools = personality.toolset?.length ? personality.toolset : undefined;\n // Per-personality plugin + MCP gate (default-deny: missing field = no access)\n const allowedPlugins = personality.plugins ?? [];\n const filterOpts: ToolFilterOpts = {\n allowedMcpServers: personality.mcp_servers,\n allowedPlugins,\n };\n\n // Step 2: Fire session_start hooks\n await this.hooks.fireVoid(\n 'session_start',\n {\n sessionId,\n sessionKey,\n platform: this.platform,\n personalityId: personality.id,\n },\n allowedPlugins,\n );\n\n // Step 3: Persist the user message.\n //\n // Subagent task contract: the delegated task always lives in the child's\n // first user message (this `text`). It is NEVER copied into the system\n // prompt, NEVER injected via memory, and NEVER duplicated across both.\n // The regression test in\n // `extensions/tools-delegation/src/__tests__/task-contract.test.ts`\n // captures every `LLMProvider.complete()` request and asserts the marker\n // never appears in `opts.system` and appears exactly once across all\n // user-role messages.\n await this.session.appendMessage({\n sessionId,\n role: 'user',\n content: text,\n });\n\n // Step 4: Load history (trimmed to most-recent limit)\n const allMessages = await this.session.getMessages(sessionId, { limit: this.historyLimit });\n const history = allMessages.filter((m) => m.role !== 'system');\n\n // Step 5: Prefetch memory\n const memCtx = await this.memory.prefetch({\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n personalityId: personality.id,\n memoryScope: personality.memoryScope,\n query: text,\n });\n\n // Step 6: Build system prompt from injectors\n const promptCtx: PromptContext = {\n sessionId,\n sessionKey,\n platform: this.platform,\n model: this.llm.model,\n history,\n workingDir: this.workingDir,\n isDm: true,\n turnNumber: allMessages.length,\n personalityId: personality.id,\n };\n\n const systemParts: string[] = [];\n\n // ETHOS.md / personality identity — routes through Storage so ScopedStorage\n // and InMemoryStorage fixtures work correctly. Only runs when storage is\n // wired (production always provides it; tests without a real ethosFile skip).\n if (personality.ethosFile && this.storage) {\n const identity = await this.storage.read(personality.ethosFile);\n if (identity) systemParts.push(identity.trim());\n }\n\n // Context injectors sorted by priority (already sorted in constructor)\n for (const injector of this.injectors) {\n // Plugin-registered injectors only fire when the plugin is permitted.\n const injPluginId = this.injectorPluginIds.get(injector);\n if (injPluginId !== undefined && !allowedPlugins.includes(injPluginId)) continue;\n if (injector.shouldInject && !injector.shouldInject(promptCtx)) continue;\n const result = await injector.inject(promptCtx);\n if (result) {\n if (result.position === 'prepend') {\n systemParts.unshift(result.content);\n } else {\n systemParts.push(result.content);\n }\n }\n }\n\n // Emit injector metadata (e.g. skill_files_used) so eval harness can capture it.\n if (promptCtx.meta && Object.keys(promptCtx.meta).length > 0) {\n yield { type: 'context_meta', data: promptCtx.meta };\n }\n\n // Memory injected last, as context about the user\n if (memCtx) {\n systemParts.push(`## Memory\\n\\n${memCtx.content}`);\n }\n\n // Step 7: Before-prompt-build modifying hooks (plugins can prepend/append/override)\n const buildResult = await this.hooks.fireModifying(\n 'before_prompt_build',\n {\n sessionId,\n personalityId: personality.id,\n history,\n },\n allowedPlugins,\n );\n\n if (buildResult.overrideSystem) {\n systemParts.length = 0;\n systemParts.push(buildResult.overrideSystem);\n } else {\n if (buildResult.prependSystem) systemParts.unshift(buildResult.prependSystem);\n if (buildResult.appendSystem) systemParts.push(buildResult.appendSystem);\n }\n\n const systemPrompt = systemParts.join('\\n\\n').trim() || undefined;\n\n // Step 8: Agentic loop — LLM call → tool use → LLM call → ...\n const llmMessages = this.toLLMMessages(history);\n let fullText = '';\n let turnCount = 0;\n\n // Tool-call budget tracking — prevents runaway loops (see IMPROVEMENT.md P1-3).\n // Counted across all iterations within a single user turn.\n let totalToolCalls = 0;\n const toolNameCounts = new Map<string, number>();\n\n for (let iteration = 0; iteration < this.maxIterations; iteration++) {\n if (abortSignal.aborted) {\n yield { type: 'error', error: 'Aborted', code: 'aborted' };\n return;\n }\n\n // Budget guard: bail before the next LLM call if we've already exceeded\n // either the total tool-call budget or the per-tool repeat budget. The\n // previous iteration's tool_result is in llmMessages, so the LLM history\n // stays valid; we just refuse to call again.\n if (totalToolCalls >= this.maxToolCallsPerTurn) {\n yield {\n type: 'tool_progress',\n toolName: '_budget',\n message: `Stopped: hit ${this.maxToolCallsPerTurn}-tool-call budget for this turn`,\n audience: 'user',\n };\n break;\n }\n const overusedTool = [...toolNameCounts.entries()].find(\n ([, count]) => count >= this.maxIdenticalToolCalls,\n );\n if (overusedTool) {\n yield {\n type: 'tool_progress',\n toolName: overusedTool[0],\n message: `Stopped: ${overusedTool[0]} called ${overusedTool[1]} times in one turn (likely loop)`,\n audience: 'user',\n };\n break;\n }\n\n // Fire before_llm_call\n await this.hooks.fireVoid(\n 'before_llm_call',\n {\n sessionId,\n model: this.llm.model,\n turnNumber: turnCount,\n },\n allowedPlugins,\n );\n\n // Stream LLM response\n const pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }> = [];\n let chunkText = '';\n\n // Streaming watchdog: cancel the stream if no chunk arrives within the\n // per-personality window. Reset every chunk so slow-but-progressing\n // reasoning is unaffected. See IMPROVEMENT.md P1-2 / OpenClaw #68596.\n const watchdogMs = personality.streamingTimeoutMs ?? this.streamingTimeoutMs;\n const watchdogController = new AbortController();\n const combinedSignal = AbortSignal.any([abortSignal, watchdogController.signal]);\n let watchdogTimer: ReturnType<typeof setTimeout> | undefined;\n const armWatchdog = () => {\n if (watchdogTimer) clearTimeout(watchdogTimer);\n watchdogTimer = setTimeout(() => watchdogController.abort(), watchdogMs);\n };\n const disarmWatchdog = () => {\n if (watchdogTimer) clearTimeout(watchdogTimer);\n watchdogTimer = undefined;\n };\n\n try {\n armWatchdog();\n const stream = this.llm.complete(llmMessages, this.tools.toDefinitions(allowedTools, filterOpts), {\n system: systemPrompt,\n cacheSystemPrompt: true,\n abortSignal: combinedSignal,\n ...(modelOverride ? { modelOverride } : {}),\n });\n\n for await (const chunk of stream) {\n if (abortSignal.aborted) break;\n if (watchdogController.signal.aborted) break;\n armWatchdog();\n yield* this.handleChunk(chunk, pendingToolCalls, (t) => {\n chunkText += t;\n fullText += t;\n });\n }\n disarmWatchdog();\n\n if (watchdogController.signal.aborted && !abortSignal.aborted) {\n yield {\n type: 'error',\n error: `LLM stream stalled — no chunk for ${watchdogMs}ms`,\n code: 'streaming_timeout',\n };\n return;\n }\n } catch (err) {\n disarmWatchdog();\n if (watchdogController.signal.aborted && !abortSignal.aborted) {\n yield {\n type: 'error',\n error: `LLM stream stalled — no chunk for ${watchdogMs}ms`,\n code: 'streaming_timeout',\n };\n return;\n }\n const msg = err instanceof Error ? err.message : String(err);\n yield { type: 'error', error: msg, code: 'llm_error' };\n return;\n }\n\n turnCount++;\n\n // Determine which tool calls completed parsing\n const completedToolCalls = pendingToolCalls.filter((tc) => tc.args !== undefined);\n\n // Update budget counters — these gate the NEXT iteration's LLM call.\n totalToolCalls += completedToolCalls.length;\n for (const tc of completedToolCalls) {\n toolNameCounts.set(tc.toolName, (toolNameCounts.get(tc.toolName) ?? 0) + 1);\n }\n\n // Persist assistant message — include tool_use references so history is LLM-replayable\n await this.session.appendMessage({\n sessionId,\n role: 'assistant',\n content: chunkText,\n ...(completedToolCalls.length > 0 && {\n toolCalls: completedToolCalls.map((tc) => ({\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n })),\n }),\n });\n\n // Fire after_llm_call\n await this.hooks.fireVoid(\n 'after_llm_call',\n {\n sessionId,\n text: chunkText,\n usage: { inputTokens: 0, outputTokens: 0 },\n },\n allowedPlugins,\n );\n\n // Push assistant message with proper content blocks for next iteration\n if (completedToolCalls.length > 0) {\n const assistantContent: MessageContent[] = [];\n if (chunkText) assistantContent.push({ type: 'text', text: chunkText });\n for (const tc of completedToolCalls) {\n assistantContent.push({\n type: 'tool_use',\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n });\n }\n llmMessages.push({ role: 'assistant', content: assistantContent });\n } else {\n llmMessages.push({ role: 'assistant', content: chunkText });\n break;\n }\n\n // Step 9: Pre-flight hooks → execute non-rejected tools → collect all results\n\n // Phase 30.2 — tools call ctx.emit() during execution; AsyncGenerator can't\n // yield from a sync callback, so we buffer per-batch then drain after\n // executeParallel resolves. Order is preserved (insertion = call order).\n const progressBuffer: Array<{\n toolName: string;\n message: string;\n percent?: number;\n audience: 'internal' | 'user';\n }> = [];\n\n const scopedStorage = this.buildScopedStorage(personality);\n\n const toolCtxBase = {\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n agentId: opts.agentId,\n personalityId: personality.id,\n memoryScope: personality.memoryScope,\n currentTurn: turnCount,\n messageCount: allMessages.length + turnCount,\n abortSignal,\n emit: (event: {\n type: 'progress';\n toolName: string;\n message: string;\n percent?: number;\n audience?: 'internal' | 'user';\n }) => {\n progressBuffer.push({\n toolName: event.toolName,\n message: event.message,\n ...(event.percent !== undefined && { percent: event.percent }),\n audience: event.audience ?? 'internal',\n });\n },\n resultBudgetChars: this.resultBudgetChars,\n ...(scopedStorage ? { storage: scopedStorage } : {}),\n };\n\n // Run before_tool_call hooks; build exec list with effective args\n // Rejected tools get tool_end ok:false + an error tool_result sent back to LLM\n type Prepped = { toolCallId: string; name: string; args: unknown; rejected?: string };\n const prepped: Prepped[] = [];\n\n for (const tc of completedToolCalls) {\n const beforeResult = await this.hooks.fireModifying(\n 'before_tool_call',\n {\n sessionId,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n args: tc.args,\n },\n allowedPlugins,\n );\n\n if (beforeResult.error) {\n yield {\n type: 'tool_end',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n ok: false,\n durationMs: 0,\n result: beforeResult.error,\n };\n prepped.push({\n toolCallId: tc.toolCallId,\n name: tc.toolName,\n args: tc.args,\n rejected: beforeResult.error,\n });\n continue;\n }\n\n const effectiveArgs = beforeResult.args ?? tc.args;\n yield {\n type: 'tool_start',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n args: effectiveArgs,\n };\n prepped.push({ toolCallId: tc.toolCallId, name: tc.toolName, args: effectiveArgs });\n }\n\n // Execute only non-rejected tools; results keyed by toolCallId\n const execInputs = prepped\n .filter((p) => p.rejected === undefined)\n .map((p) => ({ toolCallId: p.toolCallId, name: p.name, args: p.args }));\n\n const startedAt = Date.now();\n const execResults =\n execInputs.length > 0\n ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools, filterOpts)\n : [];\n const execResultMap = new Map(execResults.map((r) => [r.toolCallId, r]));\n\n // Drain any progress events tools emitted during execution. Order is\n // call-order (across the parallel batch) — close enough for users; the\n // exact interleaving doesn't matter when ctx.emit is best-effort.\n for (const ev of progressBuffer) {\n yield { type: 'tool_progress', ...ev };\n }\n progressBuffer.length = 0;\n\n // Persist results + emit tool_end + build tool_result content blocks (original order)\n const toolResultContent: MessageContent[] = [];\n\n for (const p of prepped) {\n const durationMs = Date.now() - startedAt;\n let result: ToolResult;\n\n if (p.rejected !== undefined) {\n result = { ok: false, error: p.rejected, code: 'execution_failed' };\n // tool_end already emitted above; no after_tool_call hook for blocked tools\n } else {\n const execResult = execResultMap.get(p.toolCallId);\n result = execResult?.result ?? {\n ok: false,\n error: 'Tool result missing',\n code: 'execution_failed',\n };\n yield {\n type: 'tool_end',\n toolCallId: p.toolCallId,\n toolName: p.name,\n ok: result.ok,\n durationMs,\n result: result.ok ? result.value : result.error,\n };\n await this.hooks.fireVoid(\n 'after_tool_call',\n {\n sessionId,\n toolName: p.name,\n result,\n durationMs,\n },\n allowedPlugins,\n );\n }\n\n // Persist every result (rejected or not) so history matches what LLM sees\n await this.session.appendMessage({\n sessionId,\n role: 'tool_result',\n content: result.ok ? result.value : result.error,\n toolCallId: p.toolCallId,\n toolName: p.name,\n });\n\n toolResultContent.push({\n type: 'tool_result',\n tool_use_id: p.toolCallId,\n content: result.ok ? result.value : result.error,\n is_error: !result.ok,\n });\n }\n\n // Feed all tool results back to LLM as a single user message with content blocks\n llmMessages.push({ role: 'user', content: toolResultContent });\n }\n\n // Step 10: Memory writes flow through the `memory_save` tool during the\n // turn (see extensions/tools-memory). The agent-loop itself produces no\n // updates, so there's nothing to sync here.\n\n // Step 11: Update usage\n await this.session.updateUsage(sessionId, { apiCallCount: turnCount });\n\n // Step 12: Fire agent_done\n await this.hooks.fireVoid('agent_done', { sessionId, text: fullText, turnCount }, allowedPlugins);\n\n yield { type: 'done', text: fullText, turnCount };\n }\n\n private *handleChunk(\n chunk: CompletionChunk,\n pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }>,\n onText: (t: string) => void,\n ): Generator<AgentEvent> {\n switch (chunk.type) {\n case 'text_delta':\n onText(chunk.text);\n yield { type: 'text_delta', text: chunk.text };\n break;\n\n case 'thinking_delta':\n yield { type: 'thinking_delta', thinking: chunk.thinking };\n break;\n\n case 'tool_use_start':\n pendingToolCalls.push({\n toolCallId: chunk.toolCallId,\n toolName: chunk.toolName,\n partialJson: '',\n });\n break;\n\n case 'tool_use_delta': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) tc.partialJson += chunk.partialJson;\n break;\n }\n\n case 'tool_use_end': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) {\n try {\n tc.args = JSON.parse(chunk.inputJson || tc.partialJson);\n } catch {\n tc.args = {};\n }\n }\n break;\n }\n\n case 'usage':\n yield {\n type: 'usage',\n inputTokens: chunk.usage.inputTokens,\n outputTokens: chunk.usage.outputTokens,\n estimatedCostUsd: chunk.usage.estimatedCostUsd,\n };\n break;\n\n case 'done':\n // finishReason available here for future context-compaction (Phase 3)\n break;\n }\n }\n\n // Reconstruct LLM-ready messages from stored history.\n // Assistant messages with tool calls produce proper tool_use content blocks.\n // Consecutive tool_result rows are grouped into a single user message.\n private toLLMMessages(stored: StoredMessage[]): Message[] {\n const messages: Message[] = [];\n\n for (const msg of stored) {\n if (msg.role === 'system') continue;\n\n if (msg.role === 'user') {\n messages.push({ role: 'user', content: msg.content });\n } else if (msg.role === 'assistant') {\n if (msg.toolCalls && msg.toolCalls.length > 0) {\n const content: MessageContent[] = [];\n if (msg.content) content.push({ type: 'text', text: msg.content });\n for (const tc of msg.toolCalls) {\n content.push({ type: 'tool_use', id: tc.id, name: tc.name, input: tc.input });\n }\n messages.push({ role: 'assistant', content });\n } else {\n messages.push({ role: 'assistant', content: msg.content });\n }\n } else if (msg.role === 'tool_result') {\n const resultBlock: MessageContent = {\n type: 'tool_result',\n tool_use_id: msg.toolCallId ?? '',\n content: msg.content,\n is_error: false,\n };\n const last = messages[messages.length - 1];\n // Append to existing tool_result user message, or start a new one\n if (last?.role === 'user' && Array.isArray(last.content)) {\n (last.content as MessageContent[]).push(resultBlock);\n } else {\n messages.push({ role: 'user', content: [resultBlock] });\n }\n }\n }\n\n return messages;\n }\n\n // ---------------------------------------------------------------------------\n // Per-turn ScopedStorage construction (Phase 4 — fs_reach enforcement).\n //\n // When the AgentLoop was wired with `storage` + `dataDir`, build a\n // ScopedStorage decorated with the active personality's `fs_reach`\n // allowlist for this turn. Substitutions (${ETHOS_HOME} / ${self} /\n // ${CWD}) are resolved here so the underlying storage-fs class stays\n // pristine. When `fs_reach` is unset, fall back to a sensible default:\n // read: [<ethosHome>/personalities/<self>/, <ethosHome>/skills/, <cwd>]\n // write: [<ethosHome>/personalities/<self>/, <cwd>]\n // ---------------------------------------------------------------------------\n\n private buildScopedStorage(personality: PersonalityConfig): Storage | undefined {\n if (!this.storage) return undefined;\n\n const ethosHome = this.dataDir ?? join(homedir(), '.ethos');\n const cwd = this.workingDir;\n const self = personality.id;\n const ownDir = `${join(ethosHome, 'personalities', self)}/`;\n\n const fsReach = personality.fs_reach;\n const readPrefixes =\n fsReach?.read && fsReach.read.length > 0\n ? fsReach.read.map((p) => substitute(p, { ethosHome, self, cwd }))\n : [ownDir, `${join(ethosHome, 'skills')}/`, cwd];\n const writePrefixes =\n fsReach?.write && fsReach.write.length > 0\n ? fsReach.write.map((p) => substitute(p, { ethosHome, self, cwd }))\n : [ownDir, cwd];\n\n return new ScopedStorage(this.storage, { read: readPrefixes, write: writePrefixes });\n }\n}\n\nfunction substitute(\n template: string,\n vars: { ethosHome: string; self: string; cwd: string },\n): string {\n return template\n .replace(/\\$\\{ETHOS_HOME\\}/g, vars.ethosHome)\n .replace(/\\$\\{self\\}/g, vars.self)\n .replace(/\\$\\{CWD\\}/g, vars.cwd);\n}\n","import type {\n SearchResult,\n Session,\n SessionFilter,\n SessionStore,\n SessionUsage,\n StoredMessage,\n} from '@ethosagent/types';\n\nexport class InMemorySessionStore implements SessionStore {\n private sessions = new Map<string, Session>();\n private messages = new Map<string, StoredMessage[]>();\n private idCounter = 0;\n\n async createSession(data: Omit<Session, 'id' | 'createdAt' | 'updatedAt'>): Promise<Session> {\n const session: Session = {\n ...data,\n id: `session_${++this.idCounter}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n this.sessions.set(session.id, session);\n this.messages.set(session.id, []);\n return session;\n }\n\n async getSession(id: string): Promise<Session | null> {\n return this.sessions.get(id) ?? null;\n }\n\n async getSessionByKey(key: string): Promise<Session | null> {\n for (const s of this.sessions.values()) {\n if (s.key === key) return s;\n }\n return null;\n }\n\n async updateSession(id: string, patch: Partial<Session>): Promise<void> {\n const session = this.sessions.get(id);\n if (!session) throw new Error(`Session not found: ${id}`);\n this.sessions.set(id, { ...session, ...patch, updatedAt: new Date() });\n }\n\n async deleteSession(id: string): Promise<void> {\n this.sessions.delete(id);\n this.messages.delete(id);\n }\n\n async listSessions(filter?: SessionFilter): Promise<Session[]> {\n let results = [...this.sessions.values()];\n if (filter?.platform) results = results.filter((s) => s.platform === filter.platform);\n if (filter?.personalityId)\n results = results.filter((s) => s.personalityId === filter.personalityId);\n if (filter?.workingDir) results = results.filter((s) => s.workingDir === filter.workingDir);\n if (filter?.since) {\n const since = filter.since;\n results = results.filter((s) => s.createdAt >= since);\n }\n results.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());\n const offset = filter?.offset ?? 0;\n const limit = filter?.limit ?? results.length;\n return results.slice(offset, offset + limit);\n }\n\n async appendMessage(data: Omit<StoredMessage, 'id' | 'timestamp'>): Promise<StoredMessage> {\n const message: StoredMessage = {\n ...data,\n id: `msg_${++this.idCounter}`,\n timestamp: new Date(),\n };\n const list = this.messages.get(data.sessionId) ?? [];\n list.push(message);\n this.messages.set(data.sessionId, list);\n return message;\n }\n\n async getMessages(\n sessionId: string,\n options?: { limit?: number; offset?: number },\n ): Promise<StoredMessage[]> {\n const all = this.messages.get(sessionId) ?? [];\n const offset = options?.offset ?? 0;\n // Return most-recent messages: trim from the tail, then skip `offset` from the end\n const end = all.length - offset;\n const start = options?.limit ? Math.max(0, end - options.limit) : 0;\n return all.slice(start, end);\n }\n\n async updateUsage(sessionId: string, delta: Partial<SessionUsage>): Promise<void> {\n const session = this.sessions.get(sessionId);\n if (!session) return;\n const usage = { ...session.usage };\n for (const [k, v] of Object.entries(delta) as [keyof SessionUsage, number][]) {\n (usage[k] as number) += v;\n }\n this.sessions.set(sessionId, { ...session, usage, updatedAt: new Date() });\n }\n\n async search(\n query: string,\n options?: { limit?: number; sessionId?: string },\n ): Promise<SearchResult[]> {\n const results: SearchResult[] = [];\n const lower = query.toLowerCase();\n for (const [sessionId, msgs] of this.messages.entries()) {\n if (options?.sessionId && sessionId !== options.sessionId) continue;\n for (const msg of msgs) {\n const idx = msg.content.toLowerCase().indexOf(lower);\n if (idx >= 0) {\n results.push({\n sessionId,\n messageId: msg.id,\n snippet: msg.content.slice(Math.max(0, idx - 50), idx + 150),\n score: 1,\n timestamp: msg.timestamp,\n });\n }\n }\n }\n results.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());\n return results.slice(0, options?.limit ?? 20);\n }\n\n async pruneOldSessions(olderThan: Date): Promise<number> {\n let count = 0;\n for (const [id, session] of this.sessions.entries()) {\n if (session.updatedAt < olderThan) {\n this.sessions.delete(id);\n this.messages.delete(id);\n count++;\n }\n }\n return count;\n }\n\n async vacuum(): Promise<void> {\n // No-op for in-memory store\n }\n}\n","import type { MemoryLoadContext, MemoryProvider, MemoryUpdate } from '@ethosagent/types';\n\nexport class NoopMemoryProvider implements MemoryProvider {\n async prefetch(_ctx: MemoryLoadContext) {\n return null;\n }\n\n async sync(_ctx: MemoryLoadContext, _updates: MemoryUpdate[]): Promise<void> {\n // No-op\n }\n}\n","import type { PersonalityConfig, PersonalityRegistry } from '@ethosagent/types';\n\nconst DEFAULT_PERSONALITY: PersonalityConfig = {\n id: 'default',\n name: 'Default',\n description: 'Default Ethos personality',\n};\n\nexport class DefaultPersonalityRegistry implements PersonalityRegistry {\n private readonly personalities = new Map<string, PersonalityConfig>([\n ['default', DEFAULT_PERSONALITY],\n ]);\n private defaultId = 'default';\n\n define(config: PersonalityConfig): void {\n this.personalities.set(config.id, config);\n }\n\n get(id: string): PersonalityConfig | undefined {\n return this.personalities.get(id);\n }\n\n list(): PersonalityConfig[] {\n return [...this.personalities.values()];\n }\n\n getDefault(): PersonalityConfig {\n return this.personalities.get(this.defaultId) ?? DEFAULT_PERSONALITY;\n }\n\n setDefault(id: string): void {\n if (!this.personalities.has(id)) {\n throw new Error(`Unknown personality: ${id}`);\n }\n this.defaultId = id;\n }\n\n async loadFromDirectory(_dir: string): Promise<void> {\n // Implemented in extensions/personalities\n }\n\n remove(id: string): void {\n this.personalities.delete(id);\n }\n}\n","import type { ClaimingHooks, HookRegistry, ModifyingHooks, VoidHooks } from '@ethosagent/types';\n\ntype AnyHandler = (...args: unknown[]) => Promise<unknown>;\n\ninterface RegisteredHandler {\n handler: AnyHandler;\n pluginId?: string;\n failurePolicy: 'fail-open' | 'fail-closed';\n}\n\n/** Returns true when a handler should fire given the allowedPlugins filter. */\nfunction isAllowed(h: RegisteredHandler, allowedPlugins: string[] | undefined): boolean {\n if (allowedPlugins === undefined) return true; // no filter — fire all\n if (!h.pluginId) return true; // built-in handler — always fires\n return allowedPlugins.includes(h.pluginId);\n}\n\nexport class DefaultHookRegistry implements HookRegistry {\n private readonly voidHandlers = new Map<string, RegisteredHandler[]>();\n private readonly modifyingHandlers = new Map<string, RegisteredHandler[]>();\n private readonly claimingHandlers = new Map<string, RegisteredHandler[]>();\n\n registerVoid<K extends keyof VoidHooks>(\n name: K,\n handler: (payload: VoidHooks[K]) => Promise<void>,\n opts?: { pluginId?: string; failurePolicy?: 'fail-open' | 'fail-closed' },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: opts?.failurePolicy ?? 'fail-open',\n };\n const list = this.voidHandlers.get(name) ?? [];\n list.push(entry);\n this.voidHandlers.set(name, list);\n return () => this.remove(this.voidHandlers, name, entry);\n }\n\n registerModifying<K extends keyof ModifyingHooks>(\n name: K,\n handler: (payload: ModifyingHooks[K][0]) => Promise<Partial<ModifyingHooks[K][1]> | null>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.modifyingHandlers.get(name) ?? [];\n list.push(entry);\n this.modifyingHandlers.set(name, list);\n return () => this.remove(this.modifyingHandlers, name, entry);\n }\n\n registerClaiming<K extends keyof ClaimingHooks>(\n name: K,\n handler: (payload: ClaimingHooks[K][0]) => Promise<ClaimingHooks[K][1]>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.claimingHandlers.get(name) ?? [];\n list.push(entry);\n this.claimingHandlers.set(name, list);\n return () => this.remove(this.claimingHandlers, name, entry);\n }\n\n // Void hooks: all handlers run in parallel via Promise.allSettled.\n // Failures are logged but never propagate (fail-open by default).\n // allowedPlugins gates plugin-registered handlers; built-in handlers always fire.\n async fireVoid<K extends keyof VoidHooks>(\n name: K,\n payload: VoidHooks[K],\n allowedPlugins?: string[],\n ): Promise<void> {\n const handlers = (this.voidHandlers.get(name) ?? []).filter((h) =>\n isAllowed(h, allowedPlugins),\n );\n await Promise.allSettled(handlers.map((h) => h.handler(payload)));\n }\n\n // Modifying hooks: handlers run sequentially; results are merged (first non-null value per key wins).\n async fireModifying<K extends keyof ModifyingHooks>(\n name: K,\n payload: ModifyingHooks[K][0],\n allowedPlugins?: string[],\n ): Promise<ModifyingHooks[K][1]> {\n const handlers = (this.modifyingHandlers.get(name) ?? []).filter((h) =>\n isAllowed(h, allowedPlugins),\n );\n const merged: Record<string, unknown> = {};\n for (const h of handlers) {\n try {\n const result = await h.handler(payload);\n if (result && typeof result === 'object') {\n for (const [k, v] of Object.entries(result)) {\n if (!(k in merged) && v !== null && v !== undefined) {\n merged[k] = v;\n }\n }\n }\n } catch {\n // fail-open: continue with other handlers\n }\n }\n return merged as ModifyingHooks[K][1];\n }\n\n // Claiming hooks: handlers run sequentially, stop after first { handled: true }.\n async fireClaiming<K extends keyof ClaimingHooks>(\n name: K,\n payload: ClaimingHooks[K][0],\n allowedPlugins?: string[],\n ): Promise<ClaimingHooks[K][1]> {\n const handlers = (this.claimingHandlers.get(name) ?? []).filter((h) =>\n isAllowed(h, allowedPlugins),\n );\n for (const h of handlers) {\n try {\n const result = (await h.handler(payload)) as ClaimingHooks[K][1];\n if (result && (result as { handled: boolean }).handled) {\n return result;\n }\n } catch {\n // fail-open: try next handler\n }\n }\n return { handled: false } as ClaimingHooks[K][1];\n }\n\n unregisterPlugin(pluginId: string): void {\n for (const map of [this.voidHandlers, this.modifyingHandlers, this.claimingHandlers]) {\n for (const [name, handlers] of map.entries()) {\n map.set(\n name,\n handlers.filter((h) => h.pluginId !== pluginId),\n );\n }\n }\n }\n\n private remove(\n map: Map<string, RegisteredHandler[]>,\n name: string,\n entry: RegisteredHandler,\n ): void {\n const list = map.get(name) ?? [];\n map.set(\n name,\n list.filter((h) => h !== entry),\n );\n }\n}\n","import type { Tool, ToolContext, ToolFilterOpts, ToolRegistry, ToolResult } from '@ethosagent/types';\n\ninterface ToolEntry {\n tool: Tool;\n pluginId?: string;\n}\n\n/** Extract MCP server name from `mcp__<server>__<tool>` naming convention. */\nfunction mcpServerName(toolName: string): string | undefined {\n if (!toolName.startsWith('mcp__')) return undefined;\n return toolName.split('__')[1];\n}\n\n/** Returns true when a tool passes the MCP server + plugin filters. */\nfunction passesFilter(entry: ToolEntry, filterOpts: ToolFilterOpts | undefined): boolean {\n if (!filterOpts) return true;\n\n const { allowedMcpServers, allowedPlugins } = filterOpts;\n\n // MCP server gate: MCP tools only appear when their server is in the allowlist.\n if (allowedMcpServers !== undefined) {\n const server = mcpServerName(entry.tool.name);\n if (server !== undefined && !allowedMcpServers.includes(server)) return false;\n }\n\n // Plugin gate: plugin tools only appear when their plugin is in the allowlist.\n if (allowedPlugins !== undefined && entry.pluginId !== undefined) {\n if (!allowedPlugins.includes(entry.pluginId)) return false;\n }\n\n return true;\n}\n\nexport class DefaultToolRegistry implements ToolRegistry {\n private readonly tools = new Map<string, ToolEntry>();\n\n register(tool: Tool, opts?: { pluginId?: string }): void {\n this.tools.set(tool.name, { tool, pluginId: opts?.pluginId });\n }\n\n registerAll(tools: Tool[]): void {\n for (const tool of tools) {\n this.register(tool);\n }\n }\n\n unregister(name: string): void {\n this.tools.delete(name);\n }\n\n get(name: string): Tool | undefined {\n return this.tools.get(name)?.tool;\n }\n\n getAvailable(): Tool[] {\n return [...this.tools.values()]\n .filter((e) => !e.tool.isAvailable || e.tool.isAvailable())\n .map((e) => e.tool);\n }\n\n getForToolset(toolset: string): Tool[] {\n return this.getAvailable().filter((t) => t.toolset === toolset);\n }\n\n toDefinitions(allowedTools?: string[], filterOpts?: ToolFilterOpts) {\n const entries = [...this.tools.values()].filter(\n (e) => !e.tool.isAvailable || e.tool.isAvailable(),\n );\n\n const filtered = entries.filter((e) => {\n if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(e.tool.name))\n return false;\n return passesFilter(e, filterOpts);\n });\n\n return filtered.map((e) => ({\n name: e.tool.name,\n description: e.tool.description,\n parameters: e.tool.schema,\n }));\n }\n\n // Runs all tool calls in parallel. Results are returned in input order.\n // Budget is split evenly across parallel calls; each result is post-trimmed to budget.\n // allowedTools + filterOpts enforce tool access at execution time (belt-and-suspenders).\n async executeParallel(\n calls: Array<{ toolCallId: string; name: string; args: unknown }>,\n ctx: ToolContext,\n allowedTools?: string[],\n filterOpts?: ToolFilterOpts,\n ): Promise<Array<{ toolCallId: string; name: string; result: ToolResult }>> {\n const perCallBudget = Math.floor(ctx.resultBudgetChars / Math.max(calls.length, 1));\n\n const results = await Promise.allSettled(\n calls.map(async (call) => {\n // Name-based allowlist check\n if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(call.name)) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not permitted for this personality`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const entry = this.tools.get(call.name);\n if (!entry) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Unknown tool: ${call.name}`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n // MCP server + plugin filter check\n if (!passesFilter(entry, filterOpts)) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not permitted for this personality`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n if (entry.tool.isAvailable && !entry.tool.isAvailable()) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not currently available`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const budget = Math.min(perCallBudget, entry.tool.maxResultChars ?? perCallBudget);\n const toolCtx: ToolContext = { ...ctx, resultBudgetChars: budget };\n\n try {\n const result = await entry.tool.execute(call.args, toolCtx);\n // Post-trim result to budget\n if (result.ok && result.value.length > budget) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: true,\n value: `${result.value.slice(0, budget)}\\n[truncated — ${result.value.length} chars total]`,\n } as ToolResult,\n };\n }\n return { toolCallId: call.toolCallId, name: call.name, result };\n } catch (err) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: err instanceof Error ? err.message : String(err),\n code: 'execution_failed',\n } as ToolResult,\n };\n }\n }),\n );\n\n // Unwrap settled results — always return, never throw\n return results.map((r, i) => {\n if (r.status === 'fulfilled') return r.value;\n const call = calls[i] ?? { toolCallId: 'unknown', name: 'unknown', args: {} };\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: String(r.reason),\n code: 'execution_failed',\n } as ToolResult,\n };\n });\n }\n}\n","// Generic plugin registry — adapted from praxis PluginRegistry pattern.\n// Each subsystem (tools, channels, memory backends) gets its own instance.\n\nexport type PluginFactory<T, C = unknown> = (config: C) => T | null;\n\nexport class PluginRegistry<T, C = unknown> {\n private readonly factories = new Map<string, PluginFactory<T, C>>();\n\n register(type: string, factory: PluginFactory<T, C>): void {\n this.factories.set(type, factory);\n }\n\n create(type: string, config: C): T {\n const factory = this.factories.get(type);\n if (!factory) {\n throw new Error(\n `Unknown plugin type: \"${type}\". Registered: ${[...this.factories.keys()].join(', ')}`,\n );\n }\n const instance = factory(config);\n if (!instance) {\n throw new Error(`Plugin factory for \"${type}\" returned null`);\n }\n return instance;\n }\n\n has(type: string): boolean {\n return this.factories.has(type);\n }\n\n types(): string[] {\n return [...this.factories.keys()];\n }\n}\n"],"mappings":";AAAA,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB,SAAS,qBAAqB;;;ACOvB,IAAM,uBAAN,MAAmD;AAAA,EAChD,WAAW,oBAAI,IAAqB;AAAA,EACpC,WAAW,oBAAI,IAA6B;AAAA,EAC5C,YAAY;AAAA,EAEpB,MAAM,cAAc,MAAyE;AAC3F,UAAM,UAAmB;AAAA,MACvB,GAAG;AAAA,MACH,IAAI,WAAW,EAAE,KAAK,SAAS;AAAA,MAC/B,WAAW,oBAAI,KAAK;AAAA,MACpB,WAAW,oBAAI,KAAK;AAAA,IACtB;AACA,SAAK,SAAS,IAAI,QAAQ,IAAI,OAAO;AACrC,SAAK,SAAS,IAAI,QAAQ,IAAI,CAAC,CAAC;AAChC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,WAAW,IAAqC;AACpD,WAAO,KAAK,SAAS,IAAI,EAAE,KAAK;AAAA,EAClC;AAAA,EAEA,MAAM,gBAAgB,KAAsC;AAC1D,eAAW,KAAK,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AACjC,UAAM,UAAU,KAAK,UAAU,IAAI,IAAI;AACvC,QAAI,CAAC,SAAS;AACZ,YAAM,IAAI;AAAA,QACR,yBAAyB,IAAI,kBAAkB,CAAC,GAAG,KAAK,UAAU,KAAK,CAAC,EAAE,KAAK,IAAI,CAAC;AAAA,MACtF;AAAA,IACF;AACA,UAAM,WAAW,QAAQ,MAAM;AAC/B,QAAI,CAAC,UAAU;AACb,YAAM,IAAI,MAAM,uBAAuB,IAAI,iBAAiB;AAAA,IAC9D;AACA,WAAO;AAAA,EACT;AAAA,EAEA,IAAI,MAAuB;AACzB,WAAO,KAAK,UAAU,IAAI,IAAI;AAAA,EAChC;AAAA,EAEA,QAAkB;AAChB,WAAO,CAAC,GAAG,KAAK,UAAU,KAAK,CAAC;AAAA,EAClC;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethosagent/core",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Core agent loop, tool registry, and hook registry for the Ethos agent framework",
@@ -19,7 +19,8 @@
19
19
  }
20
20
  },
21
21
  "dependencies": {
22
- "@ethosagent/types": "0.1.1"
22
+ "@ethosagent/storage-fs": "0.0.0",
23
+ "@ethosagent/types": "0.2.0"
23
24
  },
24
25
  "scripts": {
25
26
  "build": "tsup src/index.ts --format esm --dts --sourcemap"