@desplega.ai/agent-swarm 2.7.9 → 2.7.10
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/{acp-adapter-dn4gdsgr.js → acp-adapter-srfhs1s9.js} +5 -5
- package/dist/{actions-g563mbn2.js → actions-b8aqhrsg.js} +19 -18
- package/dist/{amp-adapter-q61w05eb.js → amp-adapter-4c165vgn.js} +1 -1
- package/dist/app-m9khefrh.js +20 -0
- package/dist/{assistant-p477rjwj.js → assistant-rh4v3ntz.js} +21 -20
- package/dist/{boot-args-schema-h4qj462e.js → boot-args-schema-hwkc8bzv.js} +1 -1
- package/dist/{boot-reembed-n1ehfbsw.js → boot-reembed-5y6yw9sj.js} +1 -1
- package/dist/{boot-reembed-qw4mc0pt.js → boot-reembed-fdyk55h4.js} +1 -1
- package/dist/{boot-scrub-sweep-9w9jp4sm.js → boot-scrub-sweep-r5r8kqsn.js} +3 -3
- package/dist/{boot-seeds-qdaf3sdy.js → boot-seeds-8f9zt5cm.js} +5 -5
- package/dist/{claude-adapter-348p5hx9.js → claude-adapter-s5df247c.js} +5 -5
- package/dist/{claude-managed-setup-7787fxtp.js → claude-managed-setup-9gsmwzyx.js} +1 -1
- package/dist/{claude-sdk-session-hbszbt8a.js → claude-sdk-session-dq1j4nz9.js} +5 -5
- package/dist/{cli-btf6fw39.js → cli-0m7rtm11.js} +1 -1
- package/dist/{cli-cseh6f8w.js → cli-16phend6.js} +1 -1
- package/dist/{cli-ms5m5r1y.js → cli-18qt3few.js} +1 -1
- package/dist/{cli-sgs5k6yd.js → cli-2tsfpc70.js} +1 -1
- package/dist/{cli-rwymrnaz.js → cli-36wdfzgn.js} +8 -8
- package/dist/{cli-qzt90yze.js → cli-38yww4e9.js} +8 -8
- package/dist/{cli-2a8py3sx.js → cli-3xddv0rk.js} +4 -4
- package/dist/{cli-p0m0p34z.js → cli-4vwxp550.js} +2 -2
- package/dist/{cli-bc779n4v.js → cli-55bkx8hh.js} +1 -1
- package/dist/{cli-21159vea.js → cli-5wfz00be.js} +2 -2
- package/dist/{cli-9aezs55w.js → cli-5xcb8gxq.js} +4 -4
- package/dist/{cli-12xyjhws.js → cli-6m6b4bep.js} +2 -2
- package/dist/{cli-fayav105.js → cli-7wz05bx3.js} +292 -83
- package/dist/{cli-y8nbefpb.js → cli-8ewkreqd.js} +9 -5
- package/dist/{cli-0whpms1z.js → cli-an1knn2k.js} +4 -4
- package/dist/{cli-qmd3mse0.js → cli-bxz823sz.js} +1 -1
- package/dist/{cli-a0657jmf.js → cli-bzvs88tv.js} +1 -1
- package/dist/{cli-agyaeef4.js → cli-cr14kftt.js} +2 -2
- package/dist/{cli-715t1gx6.js → cli-eb2azq8n.js} +3 -3
- package/dist/{cli-530hkwdy.js → cli-ed6yqpf1.js} +2 -2
- package/dist/{cli-k70e3x9e.js → cli-eqk5p56g.js} +3 -3
- package/dist/{cli-4t0fmf3z.js → cli-g1px1g2e.js} +1 -1
- package/dist/{cli-09kmfkmq.js → cli-h49ejvmc.js} +1 -1
- package/dist/{cli-cvs2r2vq.js → cli-hfkwwy1h.js} +39 -5
- package/dist/{cli-neyag5p6.js → cli-j7d73g1y.js} +371 -116
- package/dist/{cli-p17970qv.js → cli-jg24scat.js} +2 -2
- package/dist/{cli-varqt4d1.js → cli-kx4c8j05.js} +16 -13
- package/dist/{cli-jfx48xrj.js → cli-mp9aa8g5.js} +1 -1
- package/dist/{cli-605nn699.js → cli-mr97t7cc.js} +644 -225
- package/dist/{cli-v3cf1ts9.js → cli-n07eecjs.js} +3 -3
- package/dist/{cli-y5rbvxhc.js → cli-n515ex5z.js} +14 -14
- package/dist/{cli-3tqcamp6.js → cli-n9abj89w.js} +13 -12
- package/dist/{cli-ypff1qwp.js → cli-n9ftpys4.js} +7 -7
- package/dist/{cli-xk879m3k.js → cli-nj3x45rw.js} +27 -27
- package/dist/{cli-9r5gtwjr.js → cli-pyrxt9gw.js} +196 -1
- package/dist/{cli-mnrkgg60.js → cli-q581b386.js} +2 -2
- package/dist/{cli-hwprv9e4.js → cli-rjyghzg4.js} +2 -2
- package/dist/{cli-650zv9q9.js → cli-rt0ag4x1.js} +3 -3
- package/dist/{cli-vxh6acp7.js → cli-td33w98c.js} +2 -2
- package/dist/{cli-tqq0hspb.js → cli-tmft5dz2.js} +1 -1
- package/dist/{cli-q26b9wre.js → cli-tph5wt3y.js} +4 -4
- package/dist/{cli-qkdmz8c8.js → cli-ttbqe3q4.js} +1 -1
- package/dist/{cli-zgv8gxgs.js → cli-v2fwbnsk.js} +6 -6
- package/dist/{cli-b932gexn.js → cli-w3bk0dgv.js} +1 -1
- package/dist/{cli-serm8zrx.js → cli-x9exydey.js} +1 -1
- package/dist/{cli-hae9j8sv.js → cli-xbtq4d6s.js} +3 -3
- package/dist/{cli-587857ys.js → cli-xd5ebp64.js} +3 -3
- package/dist/{cli-ejkvvqbe.js → cli-xncjscpm.js} +10 -10
- package/dist/{cli-gce09r66.js → cli-xvymtmv4.js} +2 -2
- package/dist/{cli-btc9cb4c.js → cli-yz7v0sh4.js} +2 -2
- package/dist/{cli-sya22y84.js → cli-z9cs5m0w.js} +3 -3
- package/dist/{cli-2ccpqyyf.js → cli-zjphdj1a.js} +71 -10
- package/dist/cli.js +15 -15
- package/dist/{codex-adapter-ysq6bzha.js → codex-adapter-yr60sme6.js} +3 -3
- package/dist/{codex-session-runner-qakzfx93.js → codex-session-runner-pch8x584.js} +3 -3
- package/dist/{commands-rs7hkv9r.js → commands-phjfbkv0.js} +3 -3
- package/dist/{controller-r5x9bg6e.js → controller-jkx994pz.js} +2 -2
- package/dist/{controllers-connect-498hrxvk.js → controllers-connect-c4448kwa.js} +20 -19
- package/dist/{cursor-adapter-d46qszer.js → cursor-adapter-ndebqnm8.js} +1 -1
- package/dist/{db-pm96nhdb.js → db-ef32tbfc.js} +4 -2
- package/dist/{db-s13dqhr6.js → db-ez6egt6v.js} +3 -3
- package/dist/{dispatcher-j3h81t7m.js → dispatcher-g3tt9hs9.js} +18 -17
- package/dist/{dsh-adapter-rw0d5rc4.js → dsh-adapter-tcyn3099.js} +1 -1
- package/dist/{easter-egg-3d-qtgfxgze.js → easter-egg-3d-e36hk5td.js} +1 -1
- package/dist/{embedding-space-xh8p4q4x.js → embedding-space-4kyh5qqj.js} +6 -6
- package/dist/{embeddings-wp745m91.js → embeddings-6a8v4a6t.js} +1 -1
- package/dist/{execute-2fzb9z74.js → execute-3cbg0twf.js} +1 -1
- package/dist/{external-channel-ys21h3sy.js → external-channel-se646snz.js} +2 -2
- package/dist/{fx-adapter-vz0b9z3g.js → fx-adapter-yw795hqq.js} +6 -6
- package/dist/{google-generative-ai-ytpgncx9.js → google-generative-ai-h7s3e9kk.js} +1 -1
- package/dist/{google-vertex-tqhze5j7.js → google-vertex-fhxaafzw.js} +1 -1
- package/dist/{grok-adapter-spea7zkj.js → grok-adapter-ttmcss2j.js} +6 -6
- package/dist/handlers-17wha3ze.js +33 -0
- package/dist/{heartbeat-8phn1kdq.js → heartbeat-gwewgdms.js} +21 -20
- package/dist/{hook-amcj4s10.js → hook-g3nhexsy.js} +5 -5
- package/dist/{hook-bc6n49qn.js → hook-hq93y5ej.js} +4 -4
- package/dist/{http-7374brv5.js → http-5a9p4h6y.js} +210 -69
- package/dist/{index-pxn30mse.js → index-0eny15vj.js} +8 -8
- package/dist/{index-5a9jtqde.js → index-0rvk3vd4.js} +46 -46
- package/dist/{index-fvzke5kx.js → index-1qw6f2dc.js} +10 -10
- package/dist/{index-p4hjwbvw.js → index-3c5pxbge.js} +1 -1
- package/dist/{index-q5cmtbz5.js → index-6haqt3v5.js} +12296 -11042
- package/dist/index-8arwq8mw.js +34 -0
- package/dist/{index-h7f61sy9.js → index-e5pfbsys.js} +3 -3
- package/dist/{index-c9y31ytf.js → index-hgn3c961.js} +12 -11
- package/dist/{index-hsad2t3r.js → index-m5gx1xd6.js} +38 -38
- package/dist/{index-5fx3b3kw.js → index-rvccek1w.js} +9 -9
- package/dist/{index-w4m9z29z.js → index-s72rqgsx.js} +1 -1
- package/dist/index-tpw054az.js +38 -0
- package/dist/{index-514d9pf6.js → index-w7em4ktq.js} +8 -8
- package/dist/index-waq75j2j.js +31 -0
- package/dist/{keepalive-wy29kbrg.js → keepalive-1j4n4dmk.js} +3 -3
- package/dist/lead-re73kdgc.js +72 -0
- package/dist/{maintenance-h2ds9w6c.js → maintenance-dmghacy5.js} +1 -1
- package/dist/{oauth-refresh-sweep-q7mtbyp3.js → oauth-refresh-sweep-yy06c03c.js} +2 -2
- package/dist/{onboard-vw8r2fdw.js → onboard-52f9t5dm.js} +3 -3
- package/dist/{opencode-adapter-mtb0953j.js → opencode-adapter-krnh9jkn.js} +4 -4
- package/dist/{otel-impl-j42czw3r.js → otel-impl-xjqz6wjj.js} +1 -1
- package/dist/{pi-mono-adapter-zk8pg9jr.js → pi-mono-adapter-dbxyp0qv.js} +6 -6
- package/dist/{pricing-refresh-x7nhv23d.js → pricing-refresh-ctdr74pd.js} +3 -3
- package/dist/{rbac-roles-bzvee6cf.js → rbac-roles-ehv8s10n.js} +3 -3
- package/dist/{rbac-roles-9044425x.js → rbac-roles-q0gr8eyj.js} +2 -2
- package/dist/{render-v2-t7bn4skz.js → render-v2-5b6chvcq.js} +9 -4
- package/dist/{resume-x94gemep.js → resume-5a5d2845.js} +6 -6
- package/dist/{runtime-breq6fmv.js → runtime-khxszyfp.js} +1 -1
- package/dist/{session-access-q7dmqjvt.js → session-access-gj0bjhqg.js} +4 -4
- package/dist/{session-source-backfill-440ysamr.js → session-source-backfill-2sy0krfk.js} +2 -2
- package/dist/{setup-07n4727s.js → setup-7ssfakk1.js} +1 -1
- package/dist/{virtual-modules-z5h6mcyf.js → virtual-modules-pjj0knwf.js} +3 -3
- package/dist/{work-objects-pnq5y534.js → work-objects-bf3syvhq.js} +22 -21
- package/dist/{worker-follow-up-7z1gfkr8.js → worker-follow-up-38xevga6.js} +18 -17
- package/dist/worker-r8mz7a65.js +72 -0
- package/openapi.json +1 -1
- package/package.json +3 -3
- package/src/apps/catalog.generated.json +55 -0
- package/src/apps/page-validator.ts +19 -0
- package/src/assets/key.ts +77 -4
- package/src/be/db/session-labels.ts +57 -0
- package/src/be/db/session-tree.ts +51 -7
- package/src/be/db/slack-trees.ts +146 -0
- package/src/be/db/tasks/read.ts +102 -7
- package/src/be/db-client.ts +8 -2
- package/src/be/db.ts +95 -84
- package/src/be/event-loop-lag.ts +146 -0
- package/src/be/mcp-overflow-sweep.ts +2 -1
- package/src/be/memory/providers/sqlite-store.ts +39 -8
- package/src/be/migrations/278_slack_render_v2_reconcile.sql +108 -0
- package/src/be/swarm-config-guard.ts +2 -0
- package/src/commands/context-preamble.ts +258 -0
- package/src/commands/onboard/agent-fs-version.ts +1 -1
- package/src/extensions/catalog.generated.json +3 -3
- package/src/heartbeat/heartbeat.ts +5 -4
- package/src/http/core.ts +9 -3
- package/src/http/index.ts +10 -6
- package/src/http/memory.ts +9 -4
- package/src/http/sessions.ts +16 -3
- package/src/http/utils.ts +18 -0
- package/src/http/webhooks.ts +48 -0
- package/src/integrations/chat-sdk/commands.ts +167 -31
- package/src/integrations/chat-sdk/inbound.ts +10 -8
- package/src/integrations/chat-sdk/message-text.ts +117 -0
- package/src/providers/amp-adapter.ts +1 -1
- package/src/providers/cursor-adapter.ts +1 -1
- package/src/providers/dsh-adapter.ts +1 -1
- package/src/scheduler/scheduler.ts +4 -1
- package/src/scripts-runtime/executors/quickjs-runner.ts +2 -0
- package/src/scripts-runtime/executors/quickjs-structured-clone.ts +180 -0
- package/src/scripts-runtime/quickjs-compat.ts +0 -1
- package/src/slack/app.ts +332 -10
- package/src/slack/handlers.ts +9 -2
- package/src/slack/index.ts +7 -1
- package/src/slack/lifecycle-lock.ts +29 -0
- package/src/slack/render-v2.ts +424 -212
- package/src/slack/socket-fence.ts +64 -0
- package/src/slack/watcher.ts +42 -8
- package/src/tests/apps-schema-form.test.ts +151 -0
- package/src/tests/chat-sdk-commands.test.ts +359 -2
- package/src/tests/chat-sdk-inbound.test.ts +20 -0
- package/src/tests/chat-sdk-slack.test.ts +189 -1
- package/src/tests/db-client-busy-retry.test.ts +69 -18
- package/src/tests/db-client.test.ts +26 -1
- package/src/tests/db-task-read-contract.test.ts +2 -0
- package/src/tests/db-task-write-contract.test.ts +1 -0
- package/src/tests/dsh-adapter.test.ts +1 -1
- package/src/tests/event-loop-lag.test.ts +184 -0
- package/src/tests/extensions-example-comms-adherence-gate.test.ts +115 -0
- package/src/tests/github-webhook-early-ack.test.ts +144 -0
- package/src/tests/http-api-integration.test.ts +2 -0
- package/src/tests/http-log-scrubbing.test.ts +68 -2
- package/src/tests/kv-storage.test.ts +92 -1
- package/src/tests/memory-key-paths.test.ts +57 -0
- package/src/tests/memory-retrieval-purge.test.ts +19 -0
- package/src/tests/quickjs-compat.test.ts +1 -1
- package/src/tests/runner-context-preamble.test.ts +552 -0
- package/src/tests/script-connections.test.ts +3 -0
- package/src/tests/script-executor-conformance.test.ts +188 -0
- package/src/tests/sessions-tree.test.ts +133 -2
- package/src/tests/slack-app-icon.test.ts +4 -0
- package/src/tests/slack-dm-implicit-mention.test.ts +236 -0
- package/src/tests/slack-file-tools-attachments.test.ts +1 -0
- package/src/tests/slack-read-boundaries.test.ts +4 -3
- package/src/tests/slack-render-v2-query-plans.test.ts +262 -3
- package/src/tests/slack-render-v2-reconcile.test.ts +201 -0
- package/src/tests/slack-socket-watchdog.test.ts +748 -0
- package/src/tests/vcs-identity-inheritance.test.ts +85 -0
- package/src/tools/utils.ts +16 -7
- package/templates/extensions/comms-adherence-gate/README.md +25 -0
- package/templates/extensions/comms-adherence-gate/hooks.ts +100 -11
- package/templates/extensions/comms-adherence-gate/manifest.json +1 -1
- package/templates/skills/apps/SKILL.md +7 -0
- package/templates/skills/apps/content.md +7 -0
- package/dist/app-a66ssc4k.js +0 -17
- package/dist/handlers-crckt5dq.js +0 -32
- package/dist/index-4x08sjtk.js +0 -30
- package/dist/index-8ybffts4.js +0 -37
- package/dist/index-nfy4kkg5.js +0 -33
- package/dist/lead-zdqxsvjn.js +0 -71
- package/dist/worker-y62rvvq3.js +0 -71
- package/dist/{cli-gxy8y1dt.js → cli-8bpgbr8k.js} +3 -3
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event-loop stall attribution.
|
|
3
|
+
*
|
|
4
|
+
* Synchronous SQLite runs on the main thread, and a chain of awaited DB calls
|
|
5
|
+
* resolves in the microtask queue, so the whole chain is one macrotask. A
|
|
6
|
+
* stall over 5 s makes the Slack Socket Mode client miss its pong. The OTel
|
|
7
|
+
* `nodejs.eventloop.delay.max` histogram shows that a stall happened, but not
|
|
8
|
+
* when or which code path ran it.
|
|
9
|
+
*
|
|
10
|
+
* This module adds:
|
|
11
|
+
* - a breadcrumb: `withLoopPath(path, fn)` names the entry point (HTTP route,
|
|
12
|
+
* MCP tool, timer) on an AsyncLocalStorage that follows `await`;
|
|
13
|
+
* - a bounded ring of the DB statements run since the last probe tick, fed by
|
|
14
|
+
* the DB client (`recordStatement`);
|
|
15
|
+
* - a `setTimeout` lag probe. A stall of N ms delays its tick by about N ms.
|
|
16
|
+
* The first tick after the stall sees exactly the statements of the stalled
|
|
17
|
+
* macrotask and logs one `[event-loop] stall` line.
|
|
18
|
+
*/
|
|
19
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
20
|
+
import { scrubSecrets } from "../utils/secret-scrubber";
|
|
21
|
+
|
|
22
|
+
const pathContext = new AsyncLocalStorage<string>();
|
|
23
|
+
|
|
24
|
+
/** Run `fn` with `path` as the entry point of everything it starts. */
|
|
25
|
+
export function withLoopPath<T>(path: string, fn: () => T): T {
|
|
26
|
+
return pathContext.run(path, fn);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Wrap `fn` so each call runs with `path` as its entry point. */
|
|
30
|
+
export function bindLoopPath<A extends unknown[], R>(
|
|
31
|
+
path: string,
|
|
32
|
+
fn: (...args: A) => R,
|
|
33
|
+
): (...args: A) => R {
|
|
34
|
+
return (...args) => pathContext.run(path, fn, ...args);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Entry point of the code that runs now, or `unknown`. */
|
|
38
|
+
export function currentLoopPath(): string {
|
|
39
|
+
return pathContext.getStore() ?? "unknown";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type RecordedStatement = { path: string; sql: string; ms: number };
|
|
43
|
+
|
|
44
|
+
/** Statements shorter than this cannot make a stall worth a log line. */
|
|
45
|
+
const STATEMENT_RECORD_MIN_MS = 10;
|
|
46
|
+
const STATEMENT_RING_MAX = 256;
|
|
47
|
+
const ring: RecordedStatement[] = [];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Called by the DB client for each driver call. Stores a reference to the SQL
|
|
51
|
+
* string only; the label is built only when a stall line is logged.
|
|
52
|
+
*/
|
|
53
|
+
export function recordStatement(sql: string, ms: number): void {
|
|
54
|
+
if (ms < STATEMENT_RECORD_MIN_MS) return;
|
|
55
|
+
if (ring.length === STATEMENT_RING_MAX) ring.shift();
|
|
56
|
+
ring.push({ path: currentLoopPath(), sql, ms });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const PROBE_MS = 100;
|
|
60
|
+
const DEFAULT_STALL_WARN_MS = 1000;
|
|
61
|
+
const TOP_STATEMENTS = 3;
|
|
62
|
+
const LABEL_MAX_CHARS = 60;
|
|
63
|
+
|
|
64
|
+
/** Read per tick, so a config reload applies without a restart. */
|
|
65
|
+
function stallWarnMs(): number {
|
|
66
|
+
const raw = Number(process.env.EVENT_LOOP_STALL_WARN_MS);
|
|
67
|
+
return Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_STALL_WARN_MS;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Scrub the complete SQL before truncation: a secret that straddles the cut
|
|
72
|
+
* would otherwise leave a partial token that no pattern matches.
|
|
73
|
+
*/
|
|
74
|
+
function statementLabel(sql: string): string {
|
|
75
|
+
return scrubSecrets(sql.replace(/\s+/g, " ").trim()).slice(0, LABEL_MAX_CHARS);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Build the stall line from the statements recorded during the stall.
|
|
80
|
+
* Params are never logged: they can carry secrets and user content.
|
|
81
|
+
*/
|
|
82
|
+
export function formatStallLine(lagMs: number, statements: readonly RecordedStatement[]): string {
|
|
83
|
+
let dbMs = 0;
|
|
84
|
+
const perPath = new Map<string, number>();
|
|
85
|
+
for (const s of statements) {
|
|
86
|
+
dbMs += s.ms;
|
|
87
|
+
perPath.set(s.path, (perPath.get(s.path) ?? 0) + 1);
|
|
88
|
+
}
|
|
89
|
+
const top = [...statements].sort((a, b) => b.ms - a.ms).slice(0, TOP_STATEMENTS);
|
|
90
|
+
const path = top[0]?.path ?? "unknown";
|
|
91
|
+
const parts = [
|
|
92
|
+
`[event-loop] stall ${Math.round(lagMs)}ms`,
|
|
93
|
+
`path=${path}`,
|
|
94
|
+
`statements=${statements.length}`,
|
|
95
|
+
`db_ms=${Math.round(dbMs)}`,
|
|
96
|
+
];
|
|
97
|
+
if (top.length > 0) {
|
|
98
|
+
parts.push(`top=${top.map((s) => `${Math.round(s.ms)}ms:${statementLabel(s.sql)}`).join("|")}`);
|
|
99
|
+
parts.push(
|
|
100
|
+
`paths=${[...perPath]
|
|
101
|
+
.sort((a, b) => b[1] - a[1])
|
|
102
|
+
.map(([p, n]) => `${p}:${n}`)
|
|
103
|
+
.join(",")}`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
// Paths are labels from code, but scrub the complete line at egress anyway.
|
|
107
|
+
return scrubSecrets(parts.join(" "));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let stopProbe: (() => void) | null = null;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Start the lag probe. A `setTimeout` chain, not `setInterval`: missed
|
|
114
|
+
* intervals coalesce and lose the lag value. Returns a stop function.
|
|
115
|
+
*/
|
|
116
|
+
export function startEventLoopLagProbe(probeMs = PROBE_MS): () => void {
|
|
117
|
+
if (stopProbe) return stopProbe;
|
|
118
|
+
let timer: ReturnType<typeof setTimeout>;
|
|
119
|
+
let expected = performance.now() + probeMs;
|
|
120
|
+
const schedule = () => {
|
|
121
|
+
timer = setTimeout(tick, probeMs);
|
|
122
|
+
timer.unref?.();
|
|
123
|
+
};
|
|
124
|
+
const tick = () => {
|
|
125
|
+
const now = performance.now();
|
|
126
|
+
const lag = now - expected;
|
|
127
|
+
if (lag >= stallWarnMs()) console.warn(formatStallLine(lag, ring));
|
|
128
|
+
ring.length = 0;
|
|
129
|
+
expected = now + probeMs;
|
|
130
|
+
schedule();
|
|
131
|
+
};
|
|
132
|
+
// The probe owns no entry point: its own ticks must not inherit the caller's.
|
|
133
|
+
pathContext.exit(schedule);
|
|
134
|
+
const stop = () => {
|
|
135
|
+
clearTimeout(timer);
|
|
136
|
+
ring.length = 0;
|
|
137
|
+
if (stopProbe === stop) stopProbe = null;
|
|
138
|
+
};
|
|
139
|
+
stopProbe = stop;
|
|
140
|
+
return stop;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Stop the probe started by `startEventLoopLagProbe`, if any. */
|
|
144
|
+
export function stopEventLoopLagProbe(): void {
|
|
145
|
+
stopProbe?.();
|
|
146
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MCP_OVERFLOW_NAMESPACE } from "../kv-overflow";
|
|
2
2
|
import { sweepExpiredKvPrefix } from "./db";
|
|
3
|
+
import { withLoopPath } from "./event-loop-lag";
|
|
3
4
|
|
|
4
5
|
export const MCP_OVERFLOW_SWEEP_INTERVAL_MS = 10 * 60 * 1000;
|
|
5
6
|
|
|
@@ -26,7 +27,7 @@ export async function runMcpOverflowSweep(now = Date.now()): Promise<number> {
|
|
|
26
27
|
export function startMcpOverflowSweep(intervalMs = MCP_OVERFLOW_SWEEP_INTERVAL_MS): void {
|
|
27
28
|
if (sweepTimer) return;
|
|
28
29
|
sweepTimer = setInterval(() => {
|
|
29
|
-
void runMcpOverflowSweep();
|
|
30
|
+
void withLoopPath("mcp-overflow.sweep", () => runMcpOverflowSweep());
|
|
30
31
|
}, intervalMs);
|
|
31
32
|
if (typeof sweepTimer?.unref === "function") sweepTimer.unref();
|
|
32
33
|
}
|
|
@@ -3,6 +3,7 @@ import { cosineSimilarity, deserializeEmbedding, serializeEmbedding } from "@/be
|
|
|
3
3
|
import { contentSha256 } from "@/commands/profile-sync";
|
|
4
4
|
import type { AgentMemory, AgentMemoryScope, AgentMemorySource } from "@/types";
|
|
5
5
|
import { scrubSecrets } from "@/utils/secret-scrubber";
|
|
6
|
+
import { currentLoopPath, withLoopPath } from "../../event-loop-lag";
|
|
6
7
|
import {
|
|
7
8
|
EMBEDDING_DIMENSIONS,
|
|
8
9
|
isHybridSearchEnabled,
|
|
@@ -613,6 +614,17 @@ export class SqliteMemoryStore implements MemoryStore {
|
|
|
613
614
|
embedding: Float32Array,
|
|
614
615
|
agentId: string,
|
|
615
616
|
options: MemorySearchOptions = {},
|
|
617
|
+
): Promise<MemoryCandidate[]> {
|
|
618
|
+
// Nested breadcrumb: the stall line keeps the caller and names the search.
|
|
619
|
+
return withLoopPath(`${currentLoopPath()} > memory.search`, () =>
|
|
620
|
+
this.searchInLoopPath(embedding, agentId, options),
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
private async searchInLoopPath(
|
|
625
|
+
embedding: Float32Array,
|
|
626
|
+
agentId: string,
|
|
627
|
+
options: MemorySearchOptions,
|
|
616
628
|
): Promise<MemoryCandidate[]> {
|
|
617
629
|
const {
|
|
618
630
|
scope = "all",
|
|
@@ -1308,24 +1320,43 @@ export class SqliteMemoryStore implements MemoryStore {
|
|
|
1308
1320
|
const db = getDbClient();
|
|
1309
1321
|
|
|
1310
1322
|
const expiredIds = await db.query<{ id: string }>(
|
|
1311
|
-
"SELECT id FROM agent_memory WHERE expiresAt IS NOT NULL AND expiresAt <= datetime('now')",
|
|
1323
|
+
"SELECT id FROM agent_memory WHERE expiresAt IS NOT NULL AND expiresAt <= datetime('now') ORDER BY id",
|
|
1312
1324
|
);
|
|
1313
1325
|
|
|
1314
1326
|
if (expiredIds.length === 0) return 0;
|
|
1315
1327
|
|
|
1316
1328
|
const batchSize = 500;
|
|
1329
|
+
let purged = 0;
|
|
1317
1330
|
for (let i = 0; i < expiredIds.length; i += batchSize) {
|
|
1318
|
-
|
|
1331
|
+
// Yield a macrotask between batches: chained awaits on the DB client are
|
|
1332
|
+
// microtasks, so without it the whole purge is one event-loop stall.
|
|
1333
|
+
if (i > 0) await new Promise<void>((resolve) => setImmediate(resolve));
|
|
1334
|
+
const batch = expiredIds.slice(i, i + batchSize).map((r) => r.id);
|
|
1335
|
+
// A memory can be curated (moved to /longterm, expiry cleared) during
|
|
1336
|
+
// the yield. Recheck expiry and delete the index rows and memory rows of
|
|
1337
|
+
// the batch in one transaction, so a live row never loses its index.
|
|
1338
|
+
purged += await db.transaction(async (tx) => {
|
|
1339
|
+
const placeholders = batch.map(() => "?").join(",");
|
|
1340
|
+
const still = await tx.query<{ id: string }>(
|
|
1341
|
+
`SELECT id FROM agent_memory WHERE id IN (${placeholders})
|
|
1342
|
+
AND expiresAt IS NOT NULL AND expiresAt <= datetime('now')`,
|
|
1343
|
+
batch,
|
|
1344
|
+
);
|
|
1345
|
+
const ids = still.map((r) => r.id);
|
|
1346
|
+
if (ids.length === 0) return 0;
|
|
1347
|
+
await this.purgeByIds(ids);
|
|
1348
|
+
const result = await tx.run(
|
|
1349
|
+
`DELETE FROM agent_memory WHERE id IN (${ids.map(() => "?").join(",")})`,
|
|
1350
|
+
ids,
|
|
1351
|
+
);
|
|
1352
|
+
return result.changes;
|
|
1353
|
+
});
|
|
1319
1354
|
}
|
|
1320
1355
|
|
|
1321
|
-
const result = await db.run(
|
|
1322
|
-
"DELETE FROM agent_memory WHERE expiresAt IS NOT NULL AND expiresAt <= datetime('now')",
|
|
1323
|
-
);
|
|
1324
|
-
|
|
1325
1356
|
console.log(
|
|
1326
|
-
`[memory] Purged ${
|
|
1357
|
+
`[memory] Purged ${purged} expired memory row(s) (candidates: ${expiredIds.length} id(s))`,
|
|
1327
1358
|
);
|
|
1328
|
-
return
|
|
1359
|
+
return purged;
|
|
1329
1360
|
}
|
|
1330
1361
|
|
|
1331
1362
|
async updateEmbedding(id: string, embedding: Float32Array, model: string): Promise<void> {
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
-- Cut the Slack render-v2 reconcile cost. bun:sqlite is synchronous, so each
|
|
2
|
+
-- discovery query in the 3 s render tick blocks the event loop for its full
|
|
3
|
+
-- run. All three scanned the whole render-v2 history on every tick.
|
|
4
|
+
|
|
5
|
+
-- Deferral resolution anchors on the few tasks that ever deferred instead of
|
|
6
|
+
-- on every outcome card.
|
|
7
|
+
CREATE INDEX IF NOT EXISTS idx_agent_tasks_deferred
|
|
8
|
+
ON agent_tasks(id)
|
|
9
|
+
WHERE deferredAt IS NOT NULL;
|
|
10
|
+
|
|
11
|
+
-- A tree is closed once the tree sweep has nothing left to do for it. The
|
|
12
|
+
-- sweep reads only open trees. Any write that could give the thread work
|
|
13
|
+
-- again (a task in the thread, an outcome card in the thread) reopens it
|
|
14
|
+
-- through the triggers below, so "closed" stays a pure cache of "the sweep
|
|
15
|
+
-- would skip it".
|
|
16
|
+
ALTER TABLE slack_messages ADD COLUMN closed_at TEXT;
|
|
17
|
+
|
|
18
|
+
CREATE INDEX IF NOT EXISTS idx_slack_messages_open_trees
|
|
19
|
+
ON slack_messages(created_at)
|
|
20
|
+
WHERE kind = 'tree' AND closed_at IS NULL;
|
|
21
|
+
|
|
22
|
+
-- Backfill: close every tree the sweep (getSlackTreeMessages) skips today.
|
|
23
|
+
-- Keep this predicate in step with SLACK_TREE_NEEDS_RENDER_SQL in src/be/db.ts.
|
|
24
|
+
UPDATE slack_messages AS tree
|
|
25
|
+
SET closed_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
|
|
26
|
+
WHERE tree.kind = 'tree'
|
|
27
|
+
AND tree.closed_at IS NULL
|
|
28
|
+
AND tree.ts NOT LIKE 'pending:%'
|
|
29
|
+
AND EXISTS (SELECT 1 FROM slack_render_v2_state WHERE id = 1)
|
|
30
|
+
AND NOT EXISTS (
|
|
31
|
+
SELECT 1 FROM agent_tasks task, slack_render_v2_state state
|
|
32
|
+
WHERE state.id = 1
|
|
33
|
+
AND task.slackChannelId = tree.channel_id
|
|
34
|
+
AND task.slackThreadTs = tree.thread_ts
|
|
35
|
+
AND task.createdAt >= state.activated_at
|
|
36
|
+
AND (
|
|
37
|
+
(task.source = 'slack'
|
|
38
|
+
AND task.status IN ('completed', 'failed', 'cancelled')
|
|
39
|
+
AND NOT EXISTS (
|
|
40
|
+
SELECT 1 FROM slack_messages outcome
|
|
41
|
+
WHERE outcome.kind = 'outcome'
|
|
42
|
+
AND outcome.task_id = task.id
|
|
43
|
+
AND (outcome.finalized_at IS NOT NULL OR outcome.delivery_abandoned_at IS NOT NULL)
|
|
44
|
+
))
|
|
45
|
+
OR task.status NOT IN ('completed', 'failed', 'cancelled', 'superseded', 'skipped')
|
|
46
|
+
OR task.lastUpdatedAt > tree.updated_at
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
AND NOT EXISTS (
|
|
50
|
+
SELECT 1 FROM slack_messages outcome
|
|
51
|
+
JOIN agent_tasks task ON task.id = outcome.task_id,
|
|
52
|
+
slack_render_v2_state state
|
|
53
|
+
WHERE state.id = 1
|
|
54
|
+
AND outcome.kind = 'outcome'
|
|
55
|
+
AND outcome.channel_id = tree.channel_id
|
|
56
|
+
AND outcome.thread_ts = tree.thread_ts
|
|
57
|
+
AND task.createdAt >= state.activated_at
|
|
58
|
+
AND (
|
|
59
|
+
(outcome.finalized_at IS NULL AND outcome.delivery_abandoned_at IS NULL)
|
|
60
|
+
OR outcome.updated_at > tree.updated_at
|
|
61
|
+
)
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
-- Reopen: a task in the thread was created or changed.
|
|
65
|
+
CREATE TRIGGER reopen_slack_tree_after_task_insert
|
|
66
|
+
AFTER INSERT ON agent_tasks
|
|
67
|
+
WHEN NEW.slackChannelId IS NOT NULL AND NEW.slackThreadTs IS NOT NULL
|
|
68
|
+
BEGIN
|
|
69
|
+
UPDATE slack_messages SET closed_at = NULL
|
|
70
|
+
WHERE kind = 'tree'
|
|
71
|
+
AND channel_id = NEW.slackChannelId
|
|
72
|
+
AND thread_ts = NEW.slackThreadTs
|
|
73
|
+
AND closed_at IS NOT NULL;
|
|
74
|
+
END;
|
|
75
|
+
|
|
76
|
+
CREATE TRIGGER reopen_slack_tree_after_task_update
|
|
77
|
+
AFTER UPDATE OF status, lastUpdatedAt, slackChannelId, slackThreadTs ON agent_tasks
|
|
78
|
+
WHEN NEW.slackChannelId IS NOT NULL AND NEW.slackThreadTs IS NOT NULL
|
|
79
|
+
BEGIN
|
|
80
|
+
UPDATE slack_messages SET closed_at = NULL
|
|
81
|
+
WHERE kind = 'tree'
|
|
82
|
+
AND channel_id = NEW.slackChannelId
|
|
83
|
+
AND thread_ts = NEW.slackThreadTs
|
|
84
|
+
AND closed_at IS NOT NULL;
|
|
85
|
+
END;
|
|
86
|
+
|
|
87
|
+
-- Reopen: an outcome card in the thread was created or changed.
|
|
88
|
+
CREATE TRIGGER reopen_slack_tree_after_outcome_insert
|
|
89
|
+
AFTER INSERT ON slack_messages
|
|
90
|
+
WHEN NEW.kind = 'outcome'
|
|
91
|
+
BEGIN
|
|
92
|
+
UPDATE slack_messages SET closed_at = NULL
|
|
93
|
+
WHERE kind = 'tree'
|
|
94
|
+
AND channel_id = NEW.channel_id
|
|
95
|
+
AND thread_ts = NEW.thread_ts
|
|
96
|
+
AND closed_at IS NOT NULL;
|
|
97
|
+
END;
|
|
98
|
+
|
|
99
|
+
CREATE TRIGGER reopen_slack_tree_after_outcome_update
|
|
100
|
+
AFTER UPDATE ON slack_messages
|
|
101
|
+
WHEN NEW.kind = 'outcome'
|
|
102
|
+
BEGIN
|
|
103
|
+
UPDATE slack_messages SET closed_at = NULL
|
|
104
|
+
WHERE kind = 'tree'
|
|
105
|
+
AND channel_id = NEW.channel_id
|
|
106
|
+
AND thread_ts = NEW.thread_ts
|
|
107
|
+
AND closed_at IS NOT NULL;
|
|
108
|
+
END;
|
|
@@ -598,6 +598,7 @@ const VALIDATED_KEYS: Record<string, ConfigValidator> = {
|
|
|
598
598
|
"SCRIPT_RUN_CONCURRENCY_CAP",
|
|
599
599
|
"WORKER_API_READY_TIMEOUT_SECONDS",
|
|
600
600
|
"DB_QUERY_HTTP_BUDGET_MS",
|
|
601
|
+
"EVENT_LOOP_STALL_WARN_MS",
|
|
601
602
|
"DB_QUERY_HTTP_MAX_ROWS",
|
|
602
603
|
"DB_QUERY_MCP_BUDGET_MS",
|
|
603
604
|
"DB_QUERY_MCP_MAX_ROWS",
|
|
@@ -605,6 +606,7 @@ const VALIDATED_KEYS: Record<string, ConfigValidator> = {
|
|
|
605
606
|
"SLACK_CONCLUSION_SETTLE_SEC",
|
|
606
607
|
"SLACK_CONCLUSION_TIMEOUT_MIN",
|
|
607
608
|
"SLACK_TREE_STALL_MIN",
|
|
609
|
+
"SLACK_TASK_WATCHER_INTERVAL_MS",
|
|
608
610
|
],
|
|
609
611
|
1,
|
|
610
612
|
),
|
|
@@ -316,6 +316,247 @@ function summarizeSessionLogLine(line: SessionLogForPreamble): string | null {
|
|
|
316
316
|
return snippet ? `[${ts}] ${snippet}` : null;
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
+
/**
|
|
320
|
+
* Tools with no side effect: an unanswered call to one of them is safe to
|
|
321
|
+
* repeat. Compared lowercased, so one entry covers Claude's `Read` and the
|
|
322
|
+
* `read` of pi, opencode, ACP, dsh and cursor.
|
|
323
|
+
*/
|
|
324
|
+
const READ_ONLY_TOOLS = new Set([
|
|
325
|
+
"read",
|
|
326
|
+
"grep",
|
|
327
|
+
"glob",
|
|
328
|
+
"ls",
|
|
329
|
+
"list",
|
|
330
|
+
"find",
|
|
331
|
+
"webfetch",
|
|
332
|
+
"websearch",
|
|
333
|
+
"web_search",
|
|
334
|
+
"toolsearch",
|
|
335
|
+
"tool_search",
|
|
336
|
+
"skill",
|
|
337
|
+
"todowrite",
|
|
338
|
+
"todoread",
|
|
339
|
+
"todo_list",
|
|
340
|
+
"shell_command_status",
|
|
341
|
+
]);
|
|
342
|
+
|
|
343
|
+
type ToolInput = Record<string, unknown> | undefined;
|
|
344
|
+
|
|
345
|
+
type ToolLogEvent =
|
|
346
|
+
| { kind: "use"; id: string | null; name: string; input: ToolInput }
|
|
347
|
+
| { kind: "input"; id: string; input: ToolInput }
|
|
348
|
+
| { kind: "result"; id: string };
|
|
349
|
+
|
|
350
|
+
function firstString(...values: unknown[]): string | null {
|
|
351
|
+
for (const v of values) if (typeof v === "string" && v) return v;
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
356
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
357
|
+
? (value as Record<string, unknown>)
|
|
358
|
+
: undefined;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Codex item types that are tool calls (mirrors the UI logs-parser codex adapter). */
|
|
362
|
+
const CODEX_TOOL_ITEMS = new Set([
|
|
363
|
+
"command_execution",
|
|
364
|
+
"mcp_tool_call",
|
|
365
|
+
"collab_tool_call",
|
|
366
|
+
"web_search",
|
|
367
|
+
"todo_list",
|
|
368
|
+
"file_change",
|
|
369
|
+
]);
|
|
370
|
+
|
|
371
|
+
function codexToolUse(item: Record<string, unknown>): ToolLogEvent {
|
|
372
|
+
const id = firstString(item.id);
|
|
373
|
+
if (item.type === "command_execution") {
|
|
374
|
+
const command = Array.isArray(item.command) ? item.command.join(" ") : item.command;
|
|
375
|
+
return { kind: "use", id, name: "bash", input: { command } };
|
|
376
|
+
}
|
|
377
|
+
if (item.type === "mcp_tool_call") {
|
|
378
|
+
return {
|
|
379
|
+
kind: "use",
|
|
380
|
+
id,
|
|
381
|
+
name: `${item.server ?? "mcp"}.${item.tool ?? "unknown"}`,
|
|
382
|
+
input: asRecord(item.arguments),
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
return { kind: "use", id, name: String(item.type), input: undefined };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Extract tool call / result events from one session_log line, in order.
|
|
390
|
+
* Each harness stores its own shape; every branch pairs on a call id:
|
|
391
|
+
* - claude, pi, amp: `message.content[]` `tool_use.id` / `tool_result.tool_use_id`
|
|
392
|
+
* - codex: `item.started` / `item.completed` on `item.id`
|
|
393
|
+
* - opencode, acp, grok, fx: `tool_start` / `tool_end` on `toolCallId`
|
|
394
|
+
* (ACP fills the real args in a later raw `tool_call_update.rawInput`)
|
|
395
|
+
* - claude-managed: `agent.{mcp_,}tool_use.id` / `agent.tool_result.tool_use_id`,
|
|
396
|
+
* `agent.mcp_tool_result.mcp_tool_use_id`
|
|
397
|
+
* - cursor: `tool_call` on `call_id`, `status: "running"` until it completes
|
|
398
|
+
* - dsh: `tool_call` / `tool_result` on `callId`
|
|
399
|
+
* A call without an id gets `id: null` and is never flagged.
|
|
400
|
+
*/
|
|
401
|
+
function extractToolLogEvents(line: SessionLogForPreamble): ToolLogEvent[] {
|
|
402
|
+
let parsed: unknown;
|
|
403
|
+
try {
|
|
404
|
+
parsed = JSON.parse(line.content);
|
|
405
|
+
} catch {
|
|
406
|
+
return [];
|
|
407
|
+
}
|
|
408
|
+
const obj = asRecord(parsed);
|
|
409
|
+
if (!obj) return [];
|
|
410
|
+
|
|
411
|
+
const content = asRecord(obj.message)?.content;
|
|
412
|
+
if (Array.isArray(content)) {
|
|
413
|
+
const events: ToolLogEvent[] = [];
|
|
414
|
+
for (const block of content) {
|
|
415
|
+
const b = asRecord(block);
|
|
416
|
+
if (!b) continue;
|
|
417
|
+
if (b.type === "tool_use" && typeof b.name === "string") {
|
|
418
|
+
events.push({ kind: "use", id: firstString(b.id), name: b.name, input: asRecord(b.input) });
|
|
419
|
+
} else if (b.type === "tool_result") {
|
|
420
|
+
const id = firstString(b.tool_use_id);
|
|
421
|
+
if (id) events.push({ kind: "result", id });
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return events;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const update = asRecord(obj.update);
|
|
428
|
+
if (update?.sessionUpdate === "tool_call_update") {
|
|
429
|
+
const id = firstString(update.toolCallId);
|
|
430
|
+
const input = asRecord(update.rawInput);
|
|
431
|
+
return id && input ? [{ kind: "input", id, input }] : [];
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const item = asRecord(obj.item);
|
|
435
|
+
const result = (...ids: unknown[]): ToolLogEvent[] => {
|
|
436
|
+
const id = firstString(...ids);
|
|
437
|
+
return id ? [{ kind: "result", id }] : [];
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
switch (obj.type) {
|
|
441
|
+
case "item.started": {
|
|
442
|
+
return item && CODEX_TOOL_ITEMS.has(String(item.type)) ? [codexToolUse(item)] : [];
|
|
443
|
+
}
|
|
444
|
+
case "item.completed":
|
|
445
|
+
return item && CODEX_TOOL_ITEMS.has(String(item.type)) ? result(item.id) : [];
|
|
446
|
+
case "tool_start":
|
|
447
|
+
return [
|
|
448
|
+
{
|
|
449
|
+
kind: "use",
|
|
450
|
+
id: firstString(obj.toolCallId),
|
|
451
|
+
name: firstString(obj.toolName) ?? "tool",
|
|
452
|
+
input: asRecord(obj.args),
|
|
453
|
+
},
|
|
454
|
+
];
|
|
455
|
+
case "tool_end":
|
|
456
|
+
return result(obj.toolCallId);
|
|
457
|
+
case "agent.tool_use":
|
|
458
|
+
case "agent.mcp_tool_use": {
|
|
459
|
+
const name = firstString(obj.name) ?? "tool";
|
|
460
|
+
const server = firstString(obj.mcp_server_name);
|
|
461
|
+
return [
|
|
462
|
+
{
|
|
463
|
+
kind: "use",
|
|
464
|
+
id: firstString(obj.id),
|
|
465
|
+
name: obj.type === "agent.mcp_tool_use" && server ? `${server}.${name}` : name,
|
|
466
|
+
input: asRecord(obj.input),
|
|
467
|
+
},
|
|
468
|
+
];
|
|
469
|
+
}
|
|
470
|
+
case "agent.tool_result":
|
|
471
|
+
return result(obj.tool_use_id);
|
|
472
|
+
case "agent.mcp_tool_result":
|
|
473
|
+
return result(obj.mcp_tool_use_id);
|
|
474
|
+
case "tool_call": {
|
|
475
|
+
// cursor: one `tool_call` row per state change on the same call_id.
|
|
476
|
+
if (typeof obj.status === "string" && obj.call_id !== undefined) {
|
|
477
|
+
if (obj.status !== "running") return result(obj.call_id);
|
|
478
|
+
const args = asRecord(obj.args);
|
|
479
|
+
const mcpTool = obj.name === "mcp" ? firstString(args?.toolName) : null;
|
|
480
|
+
return [
|
|
481
|
+
{
|
|
482
|
+
kind: "use",
|
|
483
|
+
id: firstString(obj.call_id),
|
|
484
|
+
name: mcpTool
|
|
485
|
+
? `mcp__${firstString(args?.providerIdentifier) ?? "mcp"}__${mcpTool}`
|
|
486
|
+
: (firstString(obj.name) ?? "tool"),
|
|
487
|
+
input: mcpTool ? asRecord(args?.args) : args,
|
|
488
|
+
},
|
|
489
|
+
];
|
|
490
|
+
}
|
|
491
|
+
// dsh
|
|
492
|
+
return [
|
|
493
|
+
{
|
|
494
|
+
kind: "use",
|
|
495
|
+
id: firstString(obj.callId),
|
|
496
|
+
name: firstString(obj.tool, obj.name) ?? "tool",
|
|
497
|
+
input: asRecord(obj.input),
|
|
498
|
+
},
|
|
499
|
+
];
|
|
500
|
+
}
|
|
501
|
+
case "tool_use":
|
|
502
|
+
return typeof obj.name === "string"
|
|
503
|
+
? [
|
|
504
|
+
{
|
|
505
|
+
kind: "use",
|
|
506
|
+
id: firstString(obj.id, obj.tool_use_id, obj.call_id, obj.callId, obj.toolCallId),
|
|
507
|
+
name: obj.name,
|
|
508
|
+
input: asRecord(obj.input),
|
|
509
|
+
},
|
|
510
|
+
]
|
|
511
|
+
: [];
|
|
512
|
+
case "tool_result":
|
|
513
|
+
return result(obj.tool_use_id, obj.call_id, obj.callId, obj.toolCallId, obj.id);
|
|
514
|
+
default:
|
|
515
|
+
return [];
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** Most unanswered calls named in the preamble line. */
|
|
520
|
+
const MAX_INTERRUPTED_CALLS = 3;
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Find the tool calls the interrupted session was waiting on: every call
|
|
524
|
+
* with an id that never got its result, in start order, so a parallel batch
|
|
525
|
+
* whose later call answered first still flags the earlier one (Codex: start
|
|
526
|
+
* push, start search, complete search). Read-only calls are skipped. Returns
|
|
527
|
+
* the newest `MAX_INTERRUPTED_CALLS` as `<tool> <arg summary>` joined by
|
|
528
|
+
* `; `, or null when nothing is open. A call without an id cannot be paired
|
|
529
|
+
* and is never flagged. The caller scrubs the result.
|
|
530
|
+
*/
|
|
531
|
+
function findInterruptedToolCall(logs: SessionLogForPreamble[]): string | null {
|
|
532
|
+
const open = new Map<string, Extract<ToolLogEvent, { kind: "use" }>>();
|
|
533
|
+
const answered = new Set<string>();
|
|
534
|
+
const lateInput = new Map<string, ToolInput>();
|
|
535
|
+
for (const line of logs) {
|
|
536
|
+
for (const event of extractToolLogEvents(line)) {
|
|
537
|
+
if (event.kind === "use") {
|
|
538
|
+
// cursor repeats a `running` row per state change on one call_id.
|
|
539
|
+
if (event.id && !answered.has(event.id) && !open.has(event.id)) open.set(event.id, event);
|
|
540
|
+
} else if (event.kind === "input") {
|
|
541
|
+
lateInput.set(event.id, event.input);
|
|
542
|
+
} else {
|
|
543
|
+
answered.add(event.id);
|
|
544
|
+
open.delete(event.id);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
const calls: string[] = [];
|
|
549
|
+
for (const use of open.values()) {
|
|
550
|
+
if (READ_ONLY_TOOLS.has(use.name.toLowerCase())) continue;
|
|
551
|
+
const input = { ...use.input, ...(use.id ? lateInput.get(use.id) : undefined) };
|
|
552
|
+
const primary = firstString(input?.file_path, input?.path, input?.command);
|
|
553
|
+
const raw = primary ?? (Object.keys(input).length > 0 ? JSON.stringify(input) : "");
|
|
554
|
+
const args = raw.replace(/\s+/g, " ").trim().slice(0, 200);
|
|
555
|
+
calls.push(args ? `${use.name} ${args}` : use.name);
|
|
556
|
+
}
|
|
557
|
+
return calls.length > 0 ? calls.slice(-MAX_INTERRUPTED_CALLS).join("; ") : null;
|
|
558
|
+
}
|
|
559
|
+
|
|
319
560
|
/**
|
|
320
561
|
* Build a resume-task preamble.
|
|
321
562
|
*
|
|
@@ -402,6 +643,22 @@ export async function buildResumeContextPreamble(
|
|
|
402
643
|
let artBudget = Math.floor(CONTEXT_PREAMBLE_RESUME_MAX_CHARS * 0.1);
|
|
403
644
|
const steeringBudget = Math.floor(CONTEXT_PREAMBLE_RESUME_MAX_CHARS * 0.1);
|
|
404
645
|
|
|
646
|
+
// An unanswered side-effecting call may have landed before the cut-off
|
|
647
|
+
// (git push, PR comment, message send). Flag it so the agent checks first.
|
|
648
|
+
// Only the newest attempt's session was cut; an earlier attempt's open call
|
|
649
|
+
// was already the previous resume's to check.
|
|
650
|
+
const interruptedSession = logsBatches.find((batch) => batch.length > 0) ?? [];
|
|
651
|
+
const interrupted = findInterruptedToolCall(
|
|
652
|
+
[...interruptedSession]
|
|
653
|
+
.sort((a, b) => a.createdAt.localeCompare(b.createdAt))
|
|
654
|
+
.slice(-CONTEXT_PREAMBLE_RESUME_SESSION_LOG_LIMIT),
|
|
655
|
+
);
|
|
656
|
+
const interruptedLine = interrupted
|
|
657
|
+
? scrubSecrets(
|
|
658
|
+
`Interrupted: ${interrupted} may have landed. Check its effect before calling it again.`,
|
|
659
|
+
)
|
|
660
|
+
: "";
|
|
661
|
+
|
|
405
662
|
const header = [
|
|
406
663
|
"\n---",
|
|
407
664
|
"## Resuming Interrupted Task",
|
|
@@ -418,6 +675,7 @@ export async function buildResumeContextPreamble(
|
|
|
418
675
|
chain.length === 2 ? "2nd" : chain.length === 3 ? "3rd" : `${chain.length}th`
|
|
419
676
|
} resume attempt).`
|
|
420
677
|
: "",
|
|
678
|
+
interruptedLine,
|
|
421
679
|
"",
|
|
422
680
|
"---",
|
|
423
681
|
"",
|