loopengine 0.0.20 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/PROTOCOL.md +245 -0
  2. package/README.md +53 -10
  3. package/dist/core/agent-config.d.ts +282 -3
  4. package/dist/core/budget.d.ts +42 -0
  5. package/dist/core/budget.js +39 -0
  6. package/dist/core/compaction.d.ts +43 -0
  7. package/dist/core/compaction.js +70 -0
  8. package/dist/core/http-notifier.d.ts +10 -0
  9. package/dist/core/http-notifier.js +116 -0
  10. package/dist/core/http-notify-triggers/database.d.ts +19 -0
  11. package/dist/core/http-notify-triggers/database.js +53 -0
  12. package/dist/core/http-notify-triggers/email.d.ts +44 -0
  13. package/dist/core/http-notify-triggers/email.js +126 -0
  14. package/dist/core/http-notify-triggers/lark.d.ts +28 -0
  15. package/dist/core/http-notify-triggers/lark.js +168 -0
  16. package/dist/core/http-notify-triggers/redis.d.ts +33 -0
  17. package/dist/core/http-notify-triggers/redis.js +54 -0
  18. package/dist/core/http-notify-triggers/slack.d.ts +21 -0
  19. package/dist/core/http-notify-triggers/slack.js +91 -0
  20. package/dist/core/http-notify-triggers/webhook.d.ts +27 -0
  21. package/dist/core/http-notify-triggers/webhook.js +94 -0
  22. package/dist/core/loop-events.d.ts +60 -29
  23. package/dist/core/model-calls/openai-model-call.js +33 -17
  24. package/dist/core/recovery.d.ts +49 -0
  25. package/dist/core/recovery.js +124 -0
  26. package/dist/core/run-agent.d.ts +143 -15
  27. package/dist/core/run-agent.js +321 -66
  28. package/dist/core/session-store.d.ts +2 -1
  29. package/dist/core/session-store.js +6 -4
  30. package/dist/core/system-skills/system-skills/composio-large-outputs/SKILL.md +25 -0
  31. package/dist/core/system-tools/ask_user.d.ts +97 -31
  32. package/dist/core/system-tools/ask_user.js +157 -58
  33. package/dist/core/system-tools/index.d.ts +1 -1
  34. package/dist/core/system-tools/index.js +1 -1
  35. package/dist/core/vector-index.js +1 -1
  36. package/dist/package.json +6 -0
  37. package/package.json +16 -6
  38. package/protocol/loop-event.schema.json +257 -0
  39. /package/{core → dist/core}/system-skills/composio-large-outputs/SKILL.md +0 -0
@@ -9,13 +9,16 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
9
9
  import { parse as parseYaml } from 'yaml';
10
10
  import { Gate, RuleSet, ConsoleApprover } from 'actauth';
11
11
  import { SkillGarden } from 'skillgarden';
12
- import { ContextClipper } from 'contextclip';
12
+ import { BudgetTracker } from './budget.js';
13
+ import { Compactor } from './compaction.js';
13
14
  import { ToolLane } from 'toollane';
14
- import { Reflow } from 'reflowkit';
15
+ import { Recovery } from './recovery.js';
15
16
  import { loadAgentModule } from './discover-agents.js';
16
17
  import { agentAsTool } from './agent-as-tool.js';
17
18
  import { loadGatewayToolsFromDir } from './gateway-tools.js';
18
- import { systemTools, createAskUserTool } from './system-tools/index.js';
19
+ import { systemTools, createAskUserTool, CliQuestionHandler, isDurableQuestionHandler } from './system-tools/index.js';
20
+ import { resolveHttpNotifier } from './http-notifier.js';
21
+ import { CRASH_RECOVERY_CONTINUATION } from './session-store.js';
19
22
  export { systemTools } from './system-tools/index.js';
20
23
  // Resolved relative to *this file's own location* (via import.meta.url),
21
24
  // not process.cwd() — the same reasoning core/agent-registry.ts's own
@@ -48,16 +51,16 @@ export const systemSkillsDir = join(dirname(fileURLToPath(import.meta.url)), 'sy
48
51
  export function dedupeToolsByName(list) {
49
52
  return [...new Map(list.map((t) => [t.name, t])).values()];
50
53
  }
51
- /** ContextClip only ever needs a flat string per message to estimate
52
- * token usage — it doesn't need to understand tool_use/tool_result
54
+ /** budget.ts/compaction.ts only ever need a flat string per message to
55
+ * estimate token usage — neither needs to understand tool_use/tool_result
53
56
  * structure, so this is a one-way, read-only projection, never something
54
57
  * that needs to be un-projected back. */
55
- function flattenForContextClip(message) {
58
+ function flattenForBudget(message) {
56
59
  if (typeof message.content === 'string')
57
60
  return { role: message.role, content: message.content };
58
61
  const text = message.content
59
62
  .map((block) => {
60
- if (block.type === 'text')
63
+ if (block.type === 'text' || block.type === 'thinking')
61
64
  return block.text ?? '';
62
65
  if (block.type === 'tool_use')
63
66
  return `[tool_use ${block.name}(${JSON.stringify(block.input)})]`;
@@ -244,7 +247,7 @@ export async function loadSubagentAsTools(config) {
244
247
  }
245
248
  return tools;
246
249
  }
247
- export async function runAgent(config, modelCall, userMessage, history = [], options = {}) {
250
+ async function buildTurnContext(config, modelCall, options) {
248
251
  const log = options.onEvent ?? (() => { });
249
252
  // tenant: resolved by the caller (adapters/http.ts calls
250
253
  // AgentConfig.tenantFor with the request's headers/body; every other
@@ -295,65 +298,45 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
295
298
  // Also the tail-preservation window recover() below relies on — kept as
296
299
  // one named constant so the two can never drift out of sync.
297
300
  const tailMessages = 4;
298
- const contextClip = new ContextClipper({ budgetTokens: config.contextBudgetTokens ?? 8000, tailMessages });
301
+ // Two separate objects, not one — budget.ts's BudgetTracker (read-only
302
+ // check) and compaction.ts's Compactor (the actual recovery) are
303
+ // deliberately distinct capabilities now, constructed with the same
304
+ // budgetTokens/softThreshold so a nudge firing and compaction's own
305
+ // recovery target agree — see budget.ts's own BudgetTrackerOptions doc
306
+ // comment.
307
+ const budgetTracker = new BudgetTracker({ budgetTokens: config.contextBudgetTokens ?? 8000 });
308
+ const compactor = new Compactor({ budgetTokens: config.contextBudgetTokens ?? 8000, softThreshold: budgetTracker.softThreshold, tailMessages });
299
309
  const rules = loadRules(config);
300
- const gate = new Gate(rules, config.approver ?? options.approver ?? new ConsoleApprover());
310
+ // AgentConfig.httpNotifier only ever stands in for the http channel —
311
+ // see its own doc comment for why cli/http_stream never consult it at
312
+ // all, not even as a fallback.
313
+ const httpNotifier = resolveHttpNotifier(config);
314
+ // httpNotifier.approver, when the http channel has one, wins outright
315
+ // over options.approver — the same "the agent's own explicit choice for
316
+ // this channel beats whatever the adapter would otherwise default to"
317
+ // precedent a channel-keyed override always had here. This ordering
318
+ // matters in practice, not just on paper: adapters/http.ts's own plain
319
+ // /messages route always passes *some* options.approver (its own
320
+ // deployment-wide durable default, or a live tracked one) — checking
321
+ // httpNotifier first is what lets a single agent's own httpNotifier
322
+ // actually take effect there, rather than being silently shadowed by
323
+ // that per-deployment default every time.
324
+ const approver = (options.channel === 'http' ? httpNotifier.approver : undefined) ?? options.approver ?? new ConsoleApprover();
325
+ const gate = new Gate(rules, approver);
326
+ // Exact mirror of approver's own resolution above, same reasoning and
327
+ // same ordering (httpNotifier checked before options.questionHandler,
328
+ // not after — see approver's own comment for why that order matters in
329
+ // practice, not just on paper) — see RunAgentOptions.questionHandler's
330
+ // own doc comment for the one place this still isn't a full mirror of
331
+ // approver (only the durable branch below actually reads this; the live
332
+ // branch keeps resolving independently, via onQuestionPending).
333
+ const questionHandler = (options.channel === 'http' ? httpNotifier.questionHandler : undefined) ?? options.questionHandler ?? new CliQuestionHandler();
301
334
  // No explicit isSafeTool: fall back to each called tool's own `safe`
302
335
  // flag (looked up by name) rather than defaulting every tool to unsafe
303
336
  // outright — see ToolDefinition.safe's own doc comment for why this is
304
337
  // the less powerful of the two (name-only, no per-call nuance).
305
338
  const isSafeTool = config.isSafeTool ?? ((call) => tools.some((t) => t.name === call.name && t.safe === true));
306
339
  const toolLane = new ToolLane({ isSafe: isSafeTool });
307
- let messages = [...history, { role: 'user', content: userMessage }];
308
- // Tracked in parallel with `messages`, but never rewritten wholesale by
309
- // recovery the way `messages` is — this is the actual, authoritative
310
- // answer to "what did this turn add," independent of how many times (if
311
- // any) the head of `messages` got drained/summarized along the way. See
312
- // pushMessage() below and the reconciliation inside onPromptTooLong.
313
- let newMessages = [{ role: 'user', content: userMessage }];
314
- function pushMessage(message) {
315
- messages.push(message);
316
- newMessages.push(message);
317
- }
318
- const reflow = new Reflow({
319
- onPromptTooLong: async (currentMessages) => {
320
- // newMessages (this turn's own content — not durably stored
321
- // anywhere else yet) must never be handed to ContextClip at all.
322
- // An earlier version of this reconciliation tried to recover
323
- // newMessages *after* compaction by reusing whatever ContextClip's
324
- // synthetic head contained — that's unsound: ContextClip's cheap
325
- // "drain" stage doesn't produce a head at all, it just deletes old
326
- // messages outright, trusting there's a durable copy elsewhere.
327
- // That trust is only valid for the *prior* portion; only that
328
- // portion is safe to compact at all.
329
- const priorCount = currentMessages.length - newMessages.length;
330
- const priorPortion = currentMessages.slice(0, priorCount);
331
- const result = await contextClip.recover(priorPortion.map(flattenForContextClip));
332
- log({ type: 'reflow:recover', from: currentMessages.length, to: result.messages.length + newMessages.length });
333
- if (result.action === 'unchanged')
334
- return currentMessages;
335
- // Same tail-preservation reuse as before, just scoped to
336
- // priorPortion alone now — newMessages is appended back whole,
337
- // always, regardless of how large it's grown this turn.
338
- const preservedTailCount = Math.min(tailMessages, priorPortion.length);
339
- const structuredTail = priorPortion.slice(priorPortion.length - preservedTailCount);
340
- const newHead = result.messages.slice(0, result.messages.length - preservedTailCount);
341
- const recovered = [...newHead, ...structuredTail, ...newMessages];
342
- // Reflow.call() only ever returns {value, recoveries, truncated} —
343
- // it never hands back whatever `currentMessages` became after a
344
- // retry, so recovery would otherwise only ever affect the one
345
- // retried call. Reassigning the outer `messages` binding here (this
346
- // hook is the only place that has both the recovered array and a
347
- // reason to persist it) is what makes recovery durable: every
348
- // subsequent contextClip.check() in this loop, and the `history`
349
- // this function eventually returns, both see the compacted
350
- // conversation from this point on — not the original, ever-growing
351
- // one. newMessages itself needs no reconciliation at all — it was
352
- // never part of what could be compacted away.
353
- messages = recovered;
354
- return recovered;
355
- },
356
- });
357
340
  const toolsByName = new Map(tools.map((t) => [t.name, t]));
358
341
  // Object identity, not name — an agent that overrides a system tool's
359
342
  // name with its own ToolDefinition (dedupeToolsByName's own "config
@@ -404,6 +387,81 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
404
387
  // (or ping-ponging) tool calls forever — checked before spending a model
405
388
  // call on the (maxTurns + 1)-th turn, not after.
406
389
  const maxTurns = config.maxTurns ?? 25;
390
+ return {
391
+ modelCall,
392
+ log,
393
+ scope,
394
+ sessionId: options.sessionId,
395
+ skillGarden,
396
+ toolsByName,
397
+ systemToolInstances,
398
+ toolSchemas,
399
+ systemPrompt,
400
+ budgetTracker,
401
+ compactor,
402
+ gate,
403
+ toolLane,
404
+ maxTurns,
405
+ tailMessages,
406
+ askUserTool,
407
+ questionHandler,
408
+ httpNotifier,
409
+ };
410
+ }
411
+ /** The turn's actual ReAct loop — shared by runAgent (a fresh turn, its
412
+ * own starterMessage is the user's own message) and resumeAgent (a
413
+ * paused turn's completing tool_result message as starterMessage,
414
+ * picking up right where run-agent.ts's own bucket-then-execute left
415
+ * off — see RunAgentResult.stopReason's own 'pending_approval' doc
416
+ * comment). `messages`/`newMessages` are owned by the caller (built
417
+ * fresh in each case) but mutated here via pushMessage, same as the
418
+ * single function this was split out of always did. */
419
+ async function runLoop(ctx, messages, newMessages, starterMessage) {
420
+ const { modelCall, log, scope, sessionId, skillGarden, toolsByName, systemToolInstances, toolSchemas, systemPrompt, budgetTracker, compactor, gate, toolLane, maxTurns, tailMessages, askUserTool, questionHandler } = ctx;
421
+ function pushMessage(message) {
422
+ messages.push(message);
423
+ newMessages.push(message);
424
+ }
425
+ const recovery = new Recovery({
426
+ onPromptTooLong: async (currentMessages) => {
427
+ // newMessages (this turn's own content — not durably stored
428
+ // anywhere else yet) must never be handed to the compactor at all.
429
+ // An earlier version of this reconciliation tried to recover
430
+ // newMessages *after* compaction by reusing whatever the
431
+ // compactor's synthetic head contained — that's unsound: its cheap
432
+ // "drain" stage doesn't produce a head at all, it just deletes old
433
+ // messages outright, trusting there's a durable copy elsewhere.
434
+ // That trust is only valid for the *prior* portion; only that
435
+ // portion is safe to compact at all.
436
+ const priorCount = currentMessages.length - newMessages.length;
437
+ const priorPortion = currentMessages.slice(0, priorCount);
438
+ const result = await compactor.recover(priorPortion.map(flattenForBudget));
439
+ log({ type: 'prompt:compaction', from: currentMessages.length, to: result.messages.length + newMessages.length });
440
+ if (result.action === 'unchanged')
441
+ return currentMessages;
442
+ // Same tail-preservation reuse as before, just scoped to
443
+ // priorPortion alone now — newMessages is appended back whole,
444
+ // always, regardless of how large it's grown this turn.
445
+ const preservedTailCount = Math.min(tailMessages, priorPortion.length);
446
+ const structuredTail = priorPortion.slice(priorPortion.length - preservedTailCount);
447
+ const newHead = result.messages.slice(0, result.messages.length - preservedTailCount);
448
+ const recovered = [...newHead, ...structuredTail, ...newMessages];
449
+ // Recovery.call() only ever returns {value, recoveries, truncated} —
450
+ // it never hands back whatever `currentMessages` became after a
451
+ // retry, so recovery would otherwise only ever affect the one
452
+ // retried call. Reassigning the outer `messages` binding here (this
453
+ // hook is the only place that has both the recovered array and a
454
+ // reason to persist it) is what makes recovery durable: every
455
+ // subsequent budgetTracker.check() in this loop, and the `history`
456
+ // this function eventually returns, both see the compacted
457
+ // conversation from this point on — not the original, ever-growing
458
+ // one. newMessages itself needs no reconciliation at all — it was
459
+ // never part of what could be compacted away.
460
+ messages = recovered;
461
+ return recovered;
462
+ },
463
+ });
464
+ pushMessage(starterMessage);
407
465
  let turn = 0;
408
466
  for (;;) {
409
467
  turn++;
@@ -413,13 +471,18 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
413
471
  log({ type: 'loop:max_turns', maxTurns });
414
472
  return { text, history: messages, newMessages, stopReason: 'max_turns' };
415
473
  }
416
- const budget = contextClip.check(messages.map(flattenForContextClip));
417
- log({ type: 'contextclip:check', ...budget });
474
+ const budget = budgetTracker.check(messages.map(flattenForBudget));
475
+ log({ type: 'budget:check', ...budget });
418
476
  if (budget.action === 'nudge' && budget.nudge)
419
477
  pushMessage(budget.nudge);
420
- const { value: response, recoveries } = await reflow.call((msgs) => modelCall(msgs, systemPrompt, toolSchemas), messages);
421
- if (recoveries.length > 0)
422
- log({ type: 'reflow:recoveries', recoveries });
478
+ // recovery.call()'s own return also includes `recoveries` (which of
479
+ // its three failure modes fired this call) and `truncated` — not
480
+ // used here: prompt:compaction above already reports the one
481
+ // recovery type that's actually wired to a real hook (onPromptTooLong),
482
+ // and there's no second type yet to justify a second, more generic
483
+ // event just to aggregate it (see PromptCompactionEvent's own doc
484
+ // comment for the removed 'recovery:summary').
485
+ const { value: response } = await recovery.call((msgs) => modelCall(msgs, systemPrompt, toolSchemas), messages);
423
486
  // The model's full response — text and tool_use blocks alike, with
424
487
  // real ids — becomes this turn's assistant message verbatim. Every
425
488
  // tool_use block emitted here gets a matching tool_result pushed
@@ -447,6 +510,15 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
447
510
  const resultBlocks = [];
448
511
  const approvedCalls = [];
449
512
  const deniedTools = [];
513
+ // A DurableApprover/DurableQuestionHandler deferred this call to a
514
+ // durable record instead of resolving it inline (see
515
+ // HUMAN_IN_THE_LOOP.md) — collected separately from
516
+ // approvedCalls/deniedTools since it's neither run now nor denied now.
517
+ // No tool_result is pushed for these yet; the batch's post-loop
518
+ // handling below decides what happens to them. One shared array for
519
+ // both kinds (not two parallel ones) — see PendingItem's own doc
520
+ // comment for why a mixed batch has to share one checkpoint.
521
+ const pending = [];
450
522
  // Keyed by tool_use id — approvedCalls (a toollane LaneCall) has no
451
523
  // room for extra fields of its own, and toolLane.run's own results
452
524
  // only carry `id`/`name`/status, not anything about the *decision*
@@ -464,10 +536,42 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
464
536
  // so it still needs a tool_result to answer it, same as any other.
465
537
  if (block.name === 'Skill' && skillGarden) {
466
538
  const body = skillGarden.invoke(block.input.skill, block.input?.args);
467
- log({ type: 'skillgarden:invoke', skill: block.input.skill });
539
+ log({ type: 'skill:loaded', skill: block.input.skill });
468
540
  resultBlocks.push({ type: 'tool_result', tool_use_id: block.id, content: body });
469
541
  continue;
470
542
  }
543
+ // Checked before the systemToolInstances bypass just below (which
544
+ // would otherwise auto-run ask_user's own execute() straight away,
545
+ // in-process) — when the resolved QuestionHandler for this channel
546
+ // is durable (isDurableQuestionHandler — see its own doc comment,
547
+ // mirroring Gate's isDurableApprover), a system_ask_user call gets
548
+ // the exact same bucket-then-execute treatment a durably-gated tool
549
+ // call gets from gate.evaluate() below, never actually invoking the
550
+ // tool's own execute()/onPending live path at all. See PendingItem's
551
+ // own doc comment for why this shares `pending` with approvals
552
+ // rather than its own separate array. Identity, not name
553
+ // (`toolsByName.get(...) === askUserTool`, not `block.name ===
554
+ // 'system_ask_user'`) — same reasoning the systemToolInstances check
555
+ // just below already uses: an agent that deliberately overrode the
556
+ // name with its own ToolDefinition must still fall through to
557
+ // gate.evaluate() like any other tool, not get silently swallowed
558
+ // into the durable-question bucket just because the name happens to
559
+ // match. The *live* case (questionHandler resolved to something,
560
+ // just not durable — including the hard CliQuestionHandler default)
561
+ // deliberately falls through to the systemToolInstances bypass below
562
+ // unchanged — see RunAgentOptions.questionHandler's own doc comment
563
+ // for why that half isn't wired to this resolved value yet.
564
+ if (toolsByName.get(block.name) === askUserTool && isDurableQuestionHandler(questionHandler)) {
565
+ const question = String(block.input?.question ?? '');
566
+ const questionOptions = Array.isArray(block.input?.options) ? block.input.options.map(String) : undefined;
567
+ const { pendingId } = questionHandler.notifyPendingQuestion(question, questionOptions, scope.agent, sessionId);
568
+ // No 'tool:started'/'tool:result' here — same reasoning the
569
+ // durably-gated 'pending' branch below has: nothing to report
570
+ // until a resolve call answers it, later, possibly in a
571
+ // different process.
572
+ pending.push({ kind: 'question', toolUseId: block.id, tool: block.name, args: { question, options: questionOptions }, pendingId, reason: question });
573
+ continue;
574
+ }
471
575
  // See systemToolInstances' own doc comment above — bypasses
472
576
  // gate.evaluate entirely, not just auto-approves through it, since
473
577
  // ask_user itself can be the approver's own only way to ask a human
@@ -507,6 +611,15 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
507
611
  execute: () => toolsByName.get(block.name).execute(block.input ?? {}),
508
612
  });
509
613
  }
614
+ else if (decision.decision === 'pending') {
615
+ // No 'tool:started'/'tool:result' here — unlike a live 'ask',
616
+ // this call isn't running and isn't decided yet; it genuinely
617
+ // has nothing to report until a resolve call answers it, later,
618
+ // possibly in a different process. See 'loop:pending_approval'
619
+ // below (once every block in the batch is known) for the live
620
+ // signal this batch needed a durable decision.
621
+ pending.push({ kind: 'approval', toolUseId: block.id, tool: block.name, args: block.input ?? {}, pendingId: decision.pendingId, reason: decision.reason });
622
+ }
510
623
  else {
511
624
  // Every requested tool gets exactly one tool_result back — a
512
625
  // denied call is not simply dropped, or the model has no way to
@@ -556,6 +669,26 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
556
669
  // just above.
557
670
  log({ type: 'tool:result', id: call.id, tool: call.name, args: approvedMetaById.get(call.id)?.args, detailText: skipReason, statusText: 'Skipped.' });
558
671
  }
672
+ // A pending call/question closes exactly the same way an
673
+ // approved-but-never-run one does above — see HUMAN_IN_THE_LOOP.md's
674
+ // own "denial closes the checkpoint immediately" semantics: since
675
+ // this never actually becomes a checkpoint (the batch is resolved
676
+ // synchronously right here), there's nothing durable left dangling —
677
+ // only the DurableApprover's/DurableQuestionHandler's own pendingId
678
+ // record, orphaned but harmless: a resolve call against it later
679
+ // finds no checkpoint at all (never created) and no-ops, same as any
680
+ // other unknown pendingId.
681
+ for (const pendingItem of pending) {
682
+ log({ type: 'loop:skipped', name: pendingItem.tool, deniedTools });
683
+ const skipReason = `a sibling tool call in this turn (${deniedTools.join(', ')}) was denied`;
684
+ resultBlocks.push({
685
+ type: 'tool_result',
686
+ tool_use_id: pendingItem.toolUseId,
687
+ content: `skipped: ${skipReason}`,
688
+ is_error: true,
689
+ });
690
+ log({ type: 'tool:result', id: pendingItem.toolUseId, tool: pendingItem.tool, args: pendingItem.args, detailText: skipReason, statusText: 'Skipped.' });
691
+ }
559
692
  }
560
693
  else {
561
694
  // result.id is the same id LaneCall.id was given above — the exact
@@ -591,6 +724,30 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
591
724
  statusText: result.status === 'fulfilled' ? 'Approved.' : 'Error.',
592
725
  });
593
726
  }
727
+ // At least one call this batch is durably pending — resultBlocks is
728
+ // incomplete (missing an entry for every pending item) and must
729
+ // NOT be pushed as the completing tool_result message: a real model
730
+ // API expects exactly one, complete, tool_result per tool_use
731
+ // message, not a partial one now and a follow-up later. Stop here
732
+ // instead — newMessages already ends at the dangling assistant
733
+ // tool_use message pushed above, the same crash-recovery shape
734
+ // session-store.ts's own hasUnresolvedToolCall already detects (see
735
+ // RunAgentResult.stopReason's own doc comment). Turning this into a
736
+ // durable TurnCheckpoint is the caller's job — this loop does no I/O.
737
+ if (pending.length > 0) {
738
+ // A mixed batch (both kinds present) reports 'pending_approval' —
739
+ // see RunAgentResult.stopReason's own doc comment for why that's
740
+ // the right call, not an arbitrary tie-break.
741
+ const stopReason = pending.some((p) => p.kind === 'approval') ? 'pending_approval' : 'pending_question';
742
+ log({ type: stopReason === 'pending_question' ? 'loop:pending_question' : 'loop:pending_approval', pendingIds: pending.map((p) => p.pendingId) });
743
+ return {
744
+ text: `Stopped — awaiting durable ${stopReason === 'pending_question' ? 'answer' : 'approval'} for: ${pending.map((p) => p.tool).join(', ')}.`,
745
+ history: messages,
746
+ newMessages,
747
+ stopReason,
748
+ pending: { resultsSoFar: resultBlocks, outstanding: pending },
749
+ };
750
+ }
594
751
  }
595
752
  pushMessage({ role: 'user', content: resultBlocks });
596
753
  // Stop here, don't loop back for another model call — same reasoning
@@ -607,3 +764,101 @@ export async function runAgent(config, modelCall, userMessage, history = [], opt
607
764
  }
608
765
  }
609
766
  }
767
+ /** Invokes an AgentConfig.onRunStart/onRunFinish callback per its own
768
+ * never-awaited contract (see AgentConfig's own doc comments on each) —
769
+ * whether `call()` throws synchronously or returns a rejected Promise,
770
+ * the failure is logged, never surfaced to (or awaited by) the loop that
771
+ * triggered it. One shared implementation for both hooks and both of
772
+ * runAgent/resumeAgent's own call sites, rather than four copies of the
773
+ * same try/catch-and-maybe-.catch dance. */
774
+ function fireLifecycleHook(label, agentName, call) {
775
+ try {
776
+ const result = call();
777
+ if (result && typeof result.catch === 'function') {
778
+ result.catch((err) => console.error(`[loopengine] ${label} threw for agent '${agentName}':`, err));
779
+ }
780
+ }
781
+ catch (err) {
782
+ console.error(`[loopengine] ${label} threw for agent '${agentName}':`, err);
783
+ }
784
+ }
785
+ /** `cli` and `http_stream` both already deliver the start/finish signal
786
+ * to whoever's actually waiting, synchronously, as part of that channel's
787
+ * own normal response — a terminal that just ran the command, or an SSE
788
+ * connection that already carries its own `session`/`done` events on the
789
+ * exact same stream. Firing onRunStart/onRunFinish there too would just
790
+ * be a second, redundant copy of information the caller already has, so
791
+ * neither hook is invoked for those two channels — not left to each
792
+ * implementation to notice and filter out itself the way `channel` in
793
+ * the fired context still lets you filter *within* the channels this
794
+ * does fire for (`http`, or no channel at all — a bespoke script has no
795
+ * such guaranteed synchronous delivery to assume either way, so it still
796
+ * fires there). */
797
+ function shouldFireLifecycleHooks(channel) {
798
+ return channel !== 'cli' && channel !== 'http_stream';
799
+ }
800
+ export async function runAgent(config, modelCall, userMessage, history = [], options = {}) {
801
+ const ctx = await buildTurnContext(config, modelCall, options);
802
+ // config.onRunStart, if set, still wins outright over notifier's own
803
+ // derived hook — see NotifierConfig's own doc comment on precedence.
804
+ const onRunStart = config.onRunStart ?? (options.channel === 'http' ? ctx.httpNotifier.onRunStart : undefined);
805
+ if (onRunStart && shouldFireLifecycleHooks(options.channel)) {
806
+ fireLifecycleHook('onRunStart', config.name, () => onRunStart({ agent: config.name, tenant: ctx.scope.tenant, sessionId: options.sessionId, channel: options.channel, trigger: 'message' }));
807
+ }
808
+ const result = await runLoop(ctx, [...history], [], { role: 'user', content: userMessage });
809
+ const onRunFinish = config.onRunFinish ?? (options.channel === 'http' ? ctx.httpNotifier.onRunFinish : undefined);
810
+ if (onRunFinish && !result.pending && shouldFireLifecycleHooks(options.channel)) {
811
+ fireLifecycleHook('onRunFinish', config.name, () => onRunFinish({ agent: config.name, tenant: ctx.scope.tenant, sessionId: options.sessionId, channel: options.channel, text: result.text, stopReason: result.stopReason }));
812
+ }
813
+ return result;
814
+ }
815
+ /** Continues a turn durably paused on RunAgentResult.stopReason ===
816
+ * 'pending_approval' — see HUMAN_IN_THE_LOOP.md for the full design.
817
+ * `history` is durable session history including the dangling assistant
818
+ * tool_use message the pause left behind (exactly what a caller's
819
+ * SessionStore.getHistory/withSession already returns — this needs no
820
+ * special resumed-session handling, since that message is real, already
821
+ * on disk). `resolution` is the *complete* set of tool_result blocks
822
+ * answering that message — every entry from a resolved TurnCheckpoint's
823
+ * own resultsSoFar, once its outstanding is empty; assembling that is the
824
+ * caller's job (core/durable-approvals.ts's own CheckpointStore only
825
+ * stores it, it doesn't execute tools or know when a batch is complete —
826
+ * see that module's own doc comment), not something this function
827
+ * derives on its own.
828
+ *
829
+ * Pushes `resolution` as the completing message and picks the loop back
830
+ * up from there — a fresh model call, now with the full picture, exactly
831
+ * like a synchronous 'ask' would have continued if it hadn't needed to
832
+ * pause at all. Can itself return with stopReason 'pending_approval'
833
+ * again (the model's next round of tool calls hits another durably-gated
834
+ * one), 'denied', 'max_turns', or a genuine finish — runLoop handles all
835
+ * of those uniformly regardless of how this turn started. */
836
+ export async function resumeAgent(config, modelCall, history, resolution, options = {}) {
837
+ const ctx = await buildTurnContext(config, modelCall, options);
838
+ const onRunStart = config.onRunStart ?? (options.channel === 'http' ? ctx.httpNotifier.onRunStart : undefined);
839
+ if (onRunStart && shouldFireLifecycleHooks(options.channel)) {
840
+ fireLifecycleHook('onRunStart', config.name, () => onRunStart({ agent: config.name, tenant: ctx.scope.tenant, sessionId: options.sessionId, channel: options.channel, trigger: 'resolution' }));
841
+ }
842
+ // sessions.withSession's own load (adapters/http.ts's respondAfterResolution)
843
+ // runs through the exact same hasUnresolvedToolCall check real crash
844
+ // recovery uses — a durable pause deliberately leaves the identical
845
+ // dangling-tool_use shape (see HUMAN_IN_THE_LOOP.md's own "same
846
+ // crash-recovery shape... entered on purpose"), so session-store.ts has
847
+ // no way to tell the two apart and injects its own synthetic
848
+ // CRASH_RECOVERY_CONTINUATION message regardless. Only resumeAgent
849
+ // itself knows, by the fact that it's being called at all with a real
850
+ // resolution in hand, that this wasn't a crash — so it's the only place
851
+ // that can safely strip that message back out before it ends up wedged
852
+ // between the assistant's tool_calls message and the tool_result
853
+ // answering it, which every OpenAI-wire-compatible provider rejects
854
+ // outright (an assistant message with tool_calls must be immediately
855
+ // followed by one tool message per call, nothing in between).
856
+ const lastMessage = history[history.length - 1];
857
+ const effectiveHistory = lastMessage?.role === 'user' && lastMessage.content === CRASH_RECOVERY_CONTINUATION ? history.slice(0, -1) : history;
858
+ const result = await runLoop(ctx, [...effectiveHistory], [], { role: 'user', content: resolution });
859
+ const onRunFinish = config.onRunFinish ?? (options.channel === 'http' ? ctx.httpNotifier.onRunFinish : undefined);
860
+ if (onRunFinish && !result.pending && shouldFireLifecycleHooks(options.channel)) {
861
+ fireLifecycleHook('onRunFinish', config.name, () => onRunFinish({ agent: config.name, tenant: ctx.scope.tenant, sessionId: options.sessionId, channel: options.channel, text: result.text, stopReason: result.stopReason }));
862
+ }
863
+ return result;
864
+ }
@@ -6,7 +6,7 @@ export interface SessionResult<T> {
6
6
  * This has to come from the caller explicitly (runAgent's own
7
7
  * RunAgentResult.newMessages) rather than being inferred by diffing a
8
8
  * returned `history` against what withSession loaded: that only works
9
- * if history only ever grows, which isn't true once ContextClip
9
+ * if history only ever grows, which isn't true once compaction.ts's own
10
10
  * recovery can shrink/rewrite it mid-turn — see RunAgentResult's own
11
11
  * doc comment for the full reasoning. */
12
12
  newMessages: Message[];
@@ -42,6 +42,7 @@ export declare class KeyedMutex {
42
42
  * work across all keys has settled. */
43
43
  size(): number;
44
44
  }
45
+ export declare const CRASH_RECOVERY_CONTINUATION = "[session resumed after an interruption \u2014 the assistant had requested tool calls but the process stopped before any results were recorded; treat those tools as not having run]";
45
46
  /** Wraps a SessionKnit log behind the flat load/run/append contract
46
47
  * runAgent's callers use. Shared by both the file- and Redis-backed
47
48
  * stores below — only the Storage and the lock differ. */
@@ -75,11 +75,13 @@ function hasUnresolvedToolCall(message) {
75
75
  Array.isArray(message.content) &&
76
76
  message.content.some((block) => block.type === 'tool_use'));
77
77
  }
78
+ // Exported so resumeAgent (run-agent.ts) can strip this exact message
79
+ // back out when it turns out the "interruption" wasn't a crash at all —
80
+ // see that constant's own doc comment for why the two can't be told
81
+ // apart at the point sessionknit's own resume() runs.
82
+ export const CRASH_RECOVERY_CONTINUATION = '[session resumed after an interruption — the assistant had requested tool calls but the process stopped before any results were recorded; treat those tools as not having run]';
78
83
  function buildContinuation() {
79
- return {
80
- role: 'user',
81
- content: '[session resumed after an interruption — the assistant had requested tool calls but the process stopped before any results were recorded; treat those tools as not having run]',
82
- };
84
+ return { role: 'user', content: CRASH_RECOVERY_CONTINUATION };
83
85
  }
84
86
  /** Wraps a SessionKnit log behind the flat load/run/append contract
85
87
  * runAgent's callers use. Shared by both the file- and Redis-backed
@@ -0,0 +1,25 @@
1
+ ---
2
+ name: composio-large-outputs
3
+ description: How to retrieve a gateway tool's real output when its result says storedInFile is true instead of returning the data inline.
4
+ ---
5
+
6
+ # Gateway tools with a large result
7
+
8
+ Some gateway tools (Composio-sourced ones in particular) return a small
9
+ pointer object instead of their real output when the actual result is
10
+ too large to return inline:
11
+
12
+ ```
13
+ {
14
+ "successful": true,
15
+ "storedInFile": true,
16
+ "tokenCount": 10234,
17
+ "outputFilePath": "/tmp/.../SOME_TOOL_OUTPUT_xxxxx.json"
18
+ }
19
+ ```
20
+
21
+ When you see `storedInFile: true` in a tool result, the real data is
22
+ **not** in that object — it's in the file at `outputFilePath`. Call
23
+ `system_read_file` with that exact path to retrieve the actual content
24
+ before answering. Don't guess at an answer, summarize from the pointer
25
+ alone, or claim you don't have the data — read the file first.