@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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Database } from "bun:sqlite";
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
|
|
3
3
|
import { unlink } from "node:fs/promises";
|
|
4
4
|
import { createBunSqliteClient, type DbClient } from "../be/db-client";
|
|
5
5
|
|
|
@@ -319,29 +319,80 @@ describe("db-client SQLITE_BUSY retry", () => {
|
|
|
319
319
|
finalAttemptSpinMs: 50,
|
|
320
320
|
});
|
|
321
321
|
const writers = 60;
|
|
322
|
+
// The gate logs "(N spinning probes)" when the episode ends. Counting
|
|
323
|
+
// probes measures the thing that blocks the loop (each probe is one
|
|
324
|
+
// synchronous attemptSpinMs wait) without sampling wall-clock timer lag,
|
|
325
|
+
// which stalls for 700-1100ms on loaded CI runners although the gate held.
|
|
326
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
327
|
+
// Record every driver attempt with the busy_timeout it ran under and
|
|
328
|
+
// whether it hit BUSY. The gate's own probe count cannot see attempts it
|
|
329
|
+
// classed as gated, so this checks what the driver was actually told to
|
|
330
|
+
// wait, independent of the gate's bookkeeping and of runner scheduling.
|
|
331
|
+
const attempts: { spinMs: number; busy: boolean }[] = [];
|
|
332
|
+
let busyTimeoutMs = -1;
|
|
333
|
+
const exec = main.exec.bind(main);
|
|
334
|
+
const query = main.query.bind(main);
|
|
335
|
+
const execSpy = spyOn(main, "exec").mockImplementation(((sql: string) => {
|
|
336
|
+
const set = /^PRAGMA busy_timeout = (\d+)$/.exec(sql);
|
|
337
|
+
if (set) busyTimeoutMs = Number(set[1]);
|
|
338
|
+
return exec(sql);
|
|
339
|
+
}) as typeof main.exec);
|
|
340
|
+
const querySpy = spyOn(main, "query").mockImplementation(((sql: string) => {
|
|
341
|
+
const stmt = query(sql);
|
|
342
|
+
if (!sql.startsWith("INSERT INTO items")) return stmt;
|
|
343
|
+
return {
|
|
344
|
+
run: (...params: unknown[]) => {
|
|
345
|
+
const spinMs = busyTimeoutMs;
|
|
346
|
+
try {
|
|
347
|
+
const out = stmt.run(...(params as never[]));
|
|
348
|
+
attempts.push({ spinMs, busy: false });
|
|
349
|
+
return out;
|
|
350
|
+
} catch (err) {
|
|
351
|
+
attempts.push({ spinMs, busy: (err as { code?: string }).code === "SQLITE_BUSY" });
|
|
352
|
+
throw err;
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
}) as unknown as typeof main.query);
|
|
322
357
|
const released = holdWriteLock(1_500);
|
|
323
358
|
|
|
324
|
-
let maxLagMs = 0;
|
|
325
|
-
let last = performance.now();
|
|
326
|
-
const ticker = setInterval(() => {
|
|
327
|
-
const now = performance.now();
|
|
328
|
-
maxLagMs = Math.max(maxLagMs, now - last - 10);
|
|
329
|
-
last = now;
|
|
330
|
-
}, 10);
|
|
331
|
-
|
|
332
359
|
const startedAt = performance.now();
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
360
|
+
let results: PromiseSettledResult<unknown>[];
|
|
361
|
+
try {
|
|
362
|
+
results = await Promise.allSettled(
|
|
363
|
+
Array.from({ length: writers }, (_, i) =>
|
|
364
|
+
crowdClient.run("INSERT INTO items (name) VALUES (?)", [`crowd-${i}`]),
|
|
365
|
+
),
|
|
366
|
+
);
|
|
367
|
+
} finally {
|
|
368
|
+
await released;
|
|
369
|
+
execSpy.mockRestore();
|
|
370
|
+
querySpy.mockRestore();
|
|
371
|
+
}
|
|
338
372
|
const wallMs = performance.now() - startedAt;
|
|
339
|
-
|
|
340
|
-
|
|
373
|
+
const messages = warn.mock.calls.map((c) => String(c[0]));
|
|
374
|
+
warn.mockRestore();
|
|
341
375
|
|
|
342
376
|
expect(results.every((r) => r.status === "fulfilled")).toBe(true);
|
|
343
|
-
|
|
344
|
-
|
|
377
|
+
const episode = messages.map((m) => /released after \d+ms \((\d+) spinning probes\)/.exec(m));
|
|
378
|
+
const probes = Number(episode.find((m) => m)?.[1]);
|
|
379
|
+
// The episode must have been observed and closed, or the count proves nothing.
|
|
380
|
+
expect(Number.isFinite(probes)).toBe(true);
|
|
381
|
+
// Ungated every one of the 60 writers spins on every round of attempts
|
|
382
|
+
// (60 x 50ms = 3s of blocked loop per round). Gated, only one probe runs
|
|
383
|
+
// per gate interval (50, 100, 200, 400, 800ms), so a 1.5s hold needs ~6.
|
|
384
|
+
expect(probes).toBeGreaterThan(0);
|
|
385
|
+
expect(probes).toBeLessThanOrEqual(15);
|
|
386
|
+
const busy = attempts.filter((a) => a.busy);
|
|
387
|
+
const spinning = busy.filter((a) => a.spinMs > 0).length;
|
|
388
|
+
// Every other writer hit the held lock with a zero busy_timeout, so it
|
|
389
|
+
// failed in microseconds instead of blocking the loop. Without the gate
|
|
390
|
+
// there are no such attempts at all.
|
|
391
|
+
expect(busy.length - spinning).toBeGreaterThan(writers);
|
|
392
|
+
// The only BUSY attempts allowed to wait are the gate's probes: a gated
|
|
393
|
+
// attempt that waits any time (even 10ms x hundreds of attempts) shows up
|
|
394
|
+
// here as more spinning attempts than the gate reported probes.
|
|
395
|
+
expect(spinning).toBe(probes);
|
|
345
396
|
// Ungated the crowd needed several seconds of spin rounds after release.
|
|
346
397
|
expect(wallMs).toBeLessThan(3_000);
|
|
347
398
|
const rows = await crowdClient.query<{ n: number }>(
|
|
@@ -4,6 +4,8 @@ import { mkdtempSync, rmSync } from "node:fs";
|
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { createBunSqliteClient, type DbClient, type DbExecutor } from "../be/db-client";
|
|
7
|
+
import { withLoopPath } from "../be/event-loop-lag";
|
|
8
|
+
import { clearVolatileSecretsForTesting, registerVolatileSecret } from "../utils/secret-scrubber";
|
|
7
9
|
|
|
8
10
|
let raw: Database;
|
|
9
11
|
let client: DbClient;
|
|
@@ -452,7 +454,7 @@ describe("db-client slow statement warning", () => {
|
|
|
452
454
|
expect(elapsedMs).toBeGreaterThan(100);
|
|
453
455
|
const lines = slowWarnings(warn);
|
|
454
456
|
expect(lines).toHaveLength(1);
|
|
455
|
-
expect(lines[0]).toMatch(/blocked the event loop for \d+ms: \/\* padding/);
|
|
457
|
+
expect(lines[0]).toMatch(/blocked the event loop for \d+ms path=unknown: \/\* padding/);
|
|
456
458
|
expect(lines[0]).not.toContain(PARAM_SECRET);
|
|
457
459
|
expect(lines[0]).not.toContain("WITH RECURSIVE");
|
|
458
460
|
expect(lines[0]?.endsWith("…")).toBe(true);
|
|
@@ -461,6 +463,29 @@ describe("db-client slow statement warning", () => {
|
|
|
461
463
|
}
|
|
462
464
|
});
|
|
463
465
|
|
|
466
|
+
test("scrubs a secret that straddles the SQL cutoff and a secret in the path", async () => {
|
|
467
|
+
const secret = "slowsqlSECRET0123456789abcdef"; // gitleaks:allow (synthetic test fixture)
|
|
468
|
+
registerVolatileSecret(secret, "SLOW_SQL_TEST_SECRET");
|
|
469
|
+
// "/* " plus 186 chars puts the secret at char 189, across the 200-char cut.
|
|
470
|
+
const sql = `/* ${"p".repeat(185)} ${secret} */ WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c WHERE x < ?) SELECT count(*) AS n FROM c WHERE ? IS NOT NULL`;
|
|
471
|
+
expect(sql.indexOf(secret)).toBeLessThan(200);
|
|
472
|
+
expect(sql.indexOf(secret) + secret.length).toBeGreaterThan(200);
|
|
473
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
474
|
+
try {
|
|
475
|
+
const startedAt = performance.now();
|
|
476
|
+
await withLoopPath(`route ${secret}`, () => client.get(sql, [SLOW_ROWS, PARAM_SECRET]));
|
|
477
|
+
expect(performance.now() - startedAt).toBeGreaterThan(100);
|
|
478
|
+
const lines = slowWarnings(warn);
|
|
479
|
+
expect(lines).toHaveLength(1);
|
|
480
|
+
expect(lines[0]).not.toContain(secret.slice(0, 10));
|
|
481
|
+
expect(lines[0]).toContain("path=route [REDACTED:SLOW_SQL_TEST_SECRET]");
|
|
482
|
+
expect(lines[0]).toContain("[REDACTED:SLOW_SQL_TEST_SECRET]");
|
|
483
|
+
} finally {
|
|
484
|
+
warn.mockRestore();
|
|
485
|
+
clearVolatileSecretsForTesting();
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
|
|
464
489
|
test("logs a slow statement run inside a transaction", async () => {
|
|
465
490
|
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
466
491
|
try {
|
|
@@ -316,6 +316,7 @@ value:getLatestPageBySlug
|
|
|
316
316
|
value:getLatestScriptRunStepTaskByContextKey
|
|
317
317
|
value:getLatestStepForNode
|
|
318
318
|
value:getLatestTaskByContextKey
|
|
319
|
+
value:getLatestTaskByContextKeyPrefix
|
|
319
320
|
value:getLeadAgent
|
|
320
321
|
value:getLiveAgentCounts
|
|
321
322
|
value:getLogsByAgentId
|
|
@@ -651,6 +652,7 @@ value:getLatestLeadTaskByContextKey
|
|
|
651
652
|
value:getLatestLeadTaskInThread
|
|
652
653
|
value:getLatestScriptRunStepTaskByContextKey
|
|
653
654
|
value:getLatestTaskByContextKey
|
|
655
|
+
value:getLatestTaskByContextKeyPrefix
|
|
654
656
|
value:getMostRecentTaskInThread
|
|
655
657
|
value:getPendingChatRelayTasks
|
|
656
658
|
value:getPendingSlackRelayTasks
|
|
@@ -339,6 +339,7 @@ value:getLatestPageBySlug
|
|
|
339
339
|
value:getLatestScriptRunStepTaskByContextKey
|
|
340
340
|
value:getLatestStepForNode
|
|
341
341
|
value:getLatestTaskByContextKey
|
|
342
|
+
value:getLatestTaskByContextKeyPrefix
|
|
342
343
|
value:getLeadAgent
|
|
343
344
|
value:getLiveAgentCounts
|
|
344
345
|
value:getLogsByAgentId
|
|
@@ -376,7 +376,7 @@ describe("dsh harness", () => {
|
|
|
376
376
|
await rename(join(config.cwd, "dsh"), join(config.cwd, "npx"));
|
|
377
377
|
config.env = { ...config.env, DSH_BINARY: "", PATH: config.cwd };
|
|
378
378
|
await expect(new DshAdapter().createSession(config)).rejects.toThrow(
|
|
379
|
-
"dsh CLI not found. Install @deepseek-ai/dsh@0.2.2-alpha.
|
|
379
|
+
"dsh CLI not found. Install @deepseek-ai/dsh@0.2.2-alpha.3",
|
|
380
380
|
);
|
|
381
381
|
expect(await Bun.file(join(config.cwd, "invocation.json")).exists()).toBe(false);
|
|
382
382
|
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
|
|
3
|
+
import { createBunSqliteClient } from "../be/db-client";
|
|
4
|
+
import {
|
|
5
|
+
currentLoopPath,
|
|
6
|
+
formatStallLine,
|
|
7
|
+
recordStatement,
|
|
8
|
+
startEventLoopLagProbe,
|
|
9
|
+
stopEventLoopLagProbe,
|
|
10
|
+
withLoopPath,
|
|
11
|
+
} from "../be/event-loop-lag";
|
|
12
|
+
import { clearVolatileSecretsForTesting, registerVolatileSecret } from "../utils/secret-scrubber";
|
|
13
|
+
|
|
14
|
+
const PROBE_MS = 20;
|
|
15
|
+
|
|
16
|
+
function busyWait(ms: number): void {
|
|
17
|
+
const until = performance.now() + ms;
|
|
18
|
+
while (performance.now() < until) {
|
|
19
|
+
// Synchronous block: the event loop cannot run a timer.
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Wait two probe periods, so the first tick after a block has run. */
|
|
24
|
+
function settle(): Promise<void> {
|
|
25
|
+
return new Promise((resolve) => setTimeout(resolve, PROBE_MS * 3));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const stallLines = (spy: ReturnType<typeof spyOn>): string[] =>
|
|
29
|
+
spy.mock.calls.map((args) => String(args[0])).filter((l) => l.startsWith("[event-loop] stall"));
|
|
30
|
+
|
|
31
|
+
describe("event-loop lag probe", () => {
|
|
32
|
+
let warn: ReturnType<typeof spyOn>;
|
|
33
|
+
const savedThreshold = process.env.EVENT_LOOP_STALL_WARN_MS;
|
|
34
|
+
|
|
35
|
+
beforeEach(async () => {
|
|
36
|
+
delete process.env.EVENT_LOOP_STALL_WARN_MS;
|
|
37
|
+
warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
38
|
+
startEventLoopLagProbe(PROBE_MS);
|
|
39
|
+
await settle();
|
|
40
|
+
warn.mockClear();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
stopEventLoopLagProbe();
|
|
45
|
+
warn.mockRestore();
|
|
46
|
+
if (savedThreshold === undefined) delete process.env.EVENT_LOOP_STALL_WARN_MS;
|
|
47
|
+
else process.env.EVENT_LOOP_STALL_WARN_MS = savedThreshold;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("a synthetic >1 s block logs one stall line that names the code path", async () => {
|
|
51
|
+
await withLoopPath("test.path", async () => {
|
|
52
|
+
expect(currentLoopPath()).toBe("test.path");
|
|
53
|
+
// A microtask hop keeps the path, as an awaited DB call does.
|
|
54
|
+
await Promise.resolve();
|
|
55
|
+
recordStatement("SELECT 1 /* first */", 400);
|
|
56
|
+
recordStatement("SELECT 2 /* second */", 700);
|
|
57
|
+
busyWait(1100);
|
|
58
|
+
});
|
|
59
|
+
await settle();
|
|
60
|
+
|
|
61
|
+
const lines = stallLines(warn);
|
|
62
|
+
expect(lines).toHaveLength(1);
|
|
63
|
+
const line = lines[0]!;
|
|
64
|
+
expect(line).toMatch(/^\[event-loop\] stall \d+ms path=test\.path statements=2 db_ms=1100 /);
|
|
65
|
+
const stallMs = Number(line.match(/stall (\d+)ms/)?.[1]);
|
|
66
|
+
expect(stallMs).toBeGreaterThanOrEqual(1000);
|
|
67
|
+
expect(line).toContain("top=700ms:SELECT 2 /* second */|400ms:SELECT 1 /* first */");
|
|
68
|
+
expect(line).toContain("paths=test.path:2");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("a block under the threshold logs nothing", async () => {
|
|
72
|
+
withLoopPath("test.short", () => busyWait(300));
|
|
73
|
+
await settle();
|
|
74
|
+
expect(stallLines(warn)).toHaveLength(0);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("the threshold is read from EVENT_LOOP_STALL_WARN_MS", async () => {
|
|
78
|
+
process.env.EVENT_LOOP_STALL_WARN_MS = "200";
|
|
79
|
+
withLoopPath("test.env", () => {
|
|
80
|
+
recordStatement("SELECT 3", 50);
|
|
81
|
+
busyWait(300);
|
|
82
|
+
});
|
|
83
|
+
await settle();
|
|
84
|
+
const lines = stallLines(warn);
|
|
85
|
+
expect(lines).toHaveLength(1);
|
|
86
|
+
expect(lines[0]).toContain("path=test.env");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a chain of awaited DB statements is one stall attributed to its entry point", async () => {
|
|
90
|
+
const raw = new Database(":memory:");
|
|
91
|
+
const client = createBunSqliteClient(() => raw);
|
|
92
|
+
try {
|
|
93
|
+
// Pure CPU in one driver call. Params are never logged.
|
|
94
|
+
const sql =
|
|
95
|
+
"WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c WHERE x < ?) SELECT count(*) AS n FROM c WHERE ? IS NOT NULL";
|
|
96
|
+
// Calibrate: one statement of about 350 ms on this machine. Two passes,
|
|
97
|
+
// because the first (cold) pass overestimates the per-row cost.
|
|
98
|
+
let rows = 200_000;
|
|
99
|
+
for (let pass = 0; pass < 2; pass++) {
|
|
100
|
+
const t0 = performance.now();
|
|
101
|
+
await client.get(sql, [rows, "calibrate"]);
|
|
102
|
+
rows = Math.max(10_000, Math.round((350 * rows) / (performance.now() - t0)));
|
|
103
|
+
}
|
|
104
|
+
await settle();
|
|
105
|
+
warn.mockClear();
|
|
106
|
+
|
|
107
|
+
const startedAt = performance.now();
|
|
108
|
+
await withLoopPath("test.db-chain", async () => {
|
|
109
|
+
for (let i = 0; i < 5; i++) await client.get(sql, [rows, "param-secret"]);
|
|
110
|
+
});
|
|
111
|
+
const elapsedMs = performance.now() - startedAt;
|
|
112
|
+
await settle();
|
|
113
|
+
|
|
114
|
+
// Guard the premise: the chain must have run past the 1 s threshold.
|
|
115
|
+
expect(elapsedMs).toBeGreaterThan(1000);
|
|
116
|
+
const lines = stallLines(warn);
|
|
117
|
+
expect(lines).toHaveLength(1);
|
|
118
|
+
expect(lines[0]).toMatch(/path=test\.db-chain statements=5 /);
|
|
119
|
+
expect(lines[0]).toContain("paths=test.db-chain:5");
|
|
120
|
+
expect(lines[0]).not.toContain("param-secret");
|
|
121
|
+
// The per-statement slow line carries the same path key.
|
|
122
|
+
const slow = warn.mock.calls
|
|
123
|
+
.map((args) => String(args[0]))
|
|
124
|
+
.filter((l) => l.includes("[db-client] slow sync statement"));
|
|
125
|
+
expect(slow.length).toBe(5);
|
|
126
|
+
for (const l of slow) expect(l).toContain("path=test.db-chain:");
|
|
127
|
+
} finally {
|
|
128
|
+
raw.close();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe("formatStallLine", () => {
|
|
134
|
+
test("reports unknown with no statements recorded (non-SQLite block)", () => {
|
|
135
|
+
expect(formatStallLine(1234.4, [])).toBe(
|
|
136
|
+
"[event-loop] stall 1234ms path=unknown statements=0 db_ms=0",
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("names the path of the largest statement and counts every path", () => {
|
|
141
|
+
const line = formatStallLine(5213, [
|
|
142
|
+
{ path: "slack.watcher.tick", sql: "SELECT a", ms: 100 },
|
|
143
|
+
{ path: "mcp.tool memory-search", sql: "SELECT b", ms: 900 },
|
|
144
|
+
{ path: "slack.watcher.tick", sql: "SELECT c", ms: 200 },
|
|
145
|
+
{ path: "slack.watcher.tick", sql: "SELECT d", ms: 50 },
|
|
146
|
+
]);
|
|
147
|
+
expect(line).toBe(
|
|
148
|
+
"[event-loop] stall 5213ms path=mcp.tool memory-search statements=4 db_ms=1250 top=900ms:SELECT b|200ms:SELECT c|100ms:SELECT a paths=slack.watcher.tick:3,mcp.tool memory-search:1",
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("truncates and collapses SQL in the label", () => {
|
|
153
|
+
const sql = `SELECT\n\n ${"x, ".repeat(40)} FROM t`;
|
|
154
|
+
const line = formatStallLine(1000, [{ path: "p", sql, ms: 20 }]);
|
|
155
|
+
const label = line.match(/top=20ms:(.*) paths=/)?.[1] ?? "";
|
|
156
|
+
expect(label.length).toBe(60);
|
|
157
|
+
expect(label).not.toContain("\n");
|
|
158
|
+
expect(label.startsWith("SELECT x, x,")).toBe(true);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("secret scrubbing", () => {
|
|
162
|
+
const SECRET = "lagsecretVALUE0123456789abcdef"; // gitleaks:allow (synthetic test fixture)
|
|
163
|
+
|
|
164
|
+
beforeEach(() => registerVolatileSecret(SECRET, "LAG_TEST_SECRET"));
|
|
165
|
+
afterEach(() => clearVolatileSecretsForTesting());
|
|
166
|
+
|
|
167
|
+
test("a secret that straddles the label cutoff leaks no partial token", () => {
|
|
168
|
+
// "SELECT '" is 8 chars, so the secret starts at char 50 and the
|
|
169
|
+
// 60-char label cut falls inside it.
|
|
170
|
+
const sql = `SELECT '${"a".repeat(42)}${SECRET}' FROM t`;
|
|
171
|
+
expect(sql.indexOf(SECRET)).toBeLessThan(60);
|
|
172
|
+
expect(sql.indexOf(SECRET) + SECRET.length).toBeGreaterThan(60);
|
|
173
|
+
const line = formatStallLine(1000, [{ path: "p", sql, ms: 20 }]);
|
|
174
|
+
expect(line).not.toContain(SECRET.slice(0, 10));
|
|
175
|
+
expect(line).toContain("[REDACTED");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("a secret in a path is redacted from the complete line", () => {
|
|
179
|
+
const line = formatStallLine(1000, [{ path: `route ${SECRET}`, sql: "SELECT 1", ms: 20 }]);
|
|
180
|
+
expect(line).not.toContain(SECRET);
|
|
181
|
+
expect(line).toContain("path=route [REDACTED:LAG_TEST_SECRET]");
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
});
|
|
@@ -432,6 +432,121 @@ describe("self-test", () => {
|
|
|
432
432
|
]);
|
|
433
433
|
expect(queries).toHaveLength(0);
|
|
434
434
|
expect(counters.total).toBeUndefined();
|
|
435
|
+
// No fixtures configured: null, never a vacuous pass.
|
|
436
|
+
expect(kv["selftest:last"]).toMatchObject({ fixtures: [], fixturesOk: null });
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
const selftestEvent = {
|
|
440
|
+
tool: "store-progress",
|
|
441
|
+
args: { taskId: TASK_ID, status: "in_progress", progress: "comms-gate-selftest now" },
|
|
442
|
+
requestInfo: { callOrigin: "mcp" },
|
|
443
|
+
};
|
|
444
|
+
/** Controls answer as in the test above; fixture calls (judging_rules in state) get `fixtureScores`. */
|
|
445
|
+
const mockWithFixtures = (fixtureScores: (msg: string) => Record<string, number>) =>
|
|
446
|
+
mockJev((body) => {
|
|
447
|
+
const msg = String(body.state.agent_message);
|
|
448
|
+
if (body.state.judging_rules) {
|
|
449
|
+
const s = fixtureScores(msg);
|
|
450
|
+
const answers: Record<string, unknown> = {};
|
|
451
|
+
for (const id of Object.keys(body.questions)) answers[id] = { noul: s[id] ?? 1 };
|
|
452
|
+
return Response.json({ answers });
|
|
453
|
+
}
|
|
454
|
+
const bad = msg.startsWith("Great") || msg.startsWith("Prompt cache monitor");
|
|
455
|
+
const [dim] = Object.keys(body.questions);
|
|
456
|
+
return Response.json({ answers: { [dim!]: { noul: bad ? 0.1 : 0.9 } } });
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
test("judges each fixture like real traffic and records the failing one", async () => {
|
|
460
|
+
mockWithFixtures((msg) =>
|
|
461
|
+
msg.includes("Workflows 56/73") ? { plain: 0.14 } : { plain: 0.9, verbosity: 0.8 },
|
|
462
|
+
);
|
|
463
|
+
const { ctx, kv } = makeCtx({
|
|
464
|
+
threshold: 0.75,
|
|
465
|
+
fixtures: [
|
|
466
|
+
{ name: "dense", text: "📊 11-03 — all clear. Workflows 56/73 · agents 12/19." },
|
|
467
|
+
{
|
|
468
|
+
name: "reply",
|
|
469
|
+
text: "Went with option B. PR #123.",
|
|
470
|
+
prefs: { verbosity: "~400 chars max" },
|
|
471
|
+
request: "which option?",
|
|
472
|
+
},
|
|
473
|
+
],
|
|
474
|
+
});
|
|
475
|
+
await handler(selftestEvent, ctx);
|
|
476
|
+
const last = kv["selftest:last"] as any;
|
|
477
|
+
expect(last.controlsOk).toBe(true);
|
|
478
|
+
expect(last.fixturesOk).toBe(false);
|
|
479
|
+
expect(last.fixtures).toEqual([
|
|
480
|
+
{ name: "dense", scores: { plain: 0.14 }, failed: ["plain"], pass: false },
|
|
481
|
+
{ name: "reply", scores: { verbosity: 0.8, plain: 0.9 }, failed: [], pass: true },
|
|
482
|
+
]);
|
|
483
|
+
const fixtureCalls = jevCalls.filter((c) => c.body.state.judging_rules);
|
|
484
|
+
expect(fixtureCalls).toHaveLength(2);
|
|
485
|
+
// Same state shape and questions as the live gate.
|
|
486
|
+
expect(Object.keys(fixtureCalls[1]!.body.questions).sort()).toEqual(["plain", "verbosity"]);
|
|
487
|
+
expect(fixtureCalls[1]!.body.state).toMatchObject({
|
|
488
|
+
latest_request: "which option?",
|
|
489
|
+
requester_preferences: { verbosity: "~400 chars max" },
|
|
490
|
+
plain_language_rules: mod.PLAIN_RULES,
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
test("all fixtures passing sets fixturesOk", async () => {
|
|
495
|
+
mockWithFixtures(() => ({ plain: 0.9 }));
|
|
496
|
+
const { ctx, kv } = makeCtx({
|
|
497
|
+
fixtures: [{ name: "ok", text: "All clear, nothing for you." }],
|
|
498
|
+
});
|
|
499
|
+
await handler(selftestEvent, ctx);
|
|
500
|
+
expect(kv["selftest:last"]).toMatchObject({ fixturesOk: true });
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
test("a judge error fails only that fixture", async () => {
|
|
504
|
+
mockJev((body) => {
|
|
505
|
+
if (String(body.state.agent_message) === "boom") return new Response("x", { status: 503 });
|
|
506
|
+
const [dim] = Object.keys(body.questions);
|
|
507
|
+
const answers: Record<string, unknown> = {};
|
|
508
|
+
for (const id of Object.keys(body.questions)) answers[id] = { noul: 0.9 };
|
|
509
|
+
return Response.json({ answers: dim ? answers : {} });
|
|
510
|
+
});
|
|
511
|
+
const { ctx, kv } = makeCtx({
|
|
512
|
+
fixtures: [
|
|
513
|
+
{ name: "boom", text: "boom" },
|
|
514
|
+
{ name: "fine", text: "fine" },
|
|
515
|
+
],
|
|
516
|
+
});
|
|
517
|
+
await handler(selftestEvent, ctx);
|
|
518
|
+
const last = kv["selftest:last"] as any;
|
|
519
|
+
expect(last.fixturesOk).toBe(false);
|
|
520
|
+
expect(last.fixtures[0]).toMatchObject({ name: "boom", pass: false });
|
|
521
|
+
expect(last.fixtures[0].error).toContain("503");
|
|
522
|
+
expect(last.fixtures[1]).toMatchObject({ name: "fine", pass: true });
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
test("an explicit empty list or only invalid entries records null, never a pass", async () => {
|
|
526
|
+
for (const fixtures of [[], [{ text: "no name" }, 42]]) {
|
|
527
|
+
jevCalls.length = 0;
|
|
528
|
+
mockWithFixtures(() => ({ plain: 0.9 }));
|
|
529
|
+
const { ctx, kv } = makeCtx({ fixtures });
|
|
530
|
+
await handler(selftestEvent, ctx);
|
|
531
|
+
expect(kv["selftest:last"]).toMatchObject({
|
|
532
|
+
controlsOk: true,
|
|
533
|
+
fixtures: [],
|
|
534
|
+
fixturesOk: null,
|
|
535
|
+
});
|
|
536
|
+
expect(jevCalls.filter((c) => c.body.state.judging_rules)).toHaveLength(0);
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
test("invalid entries are skipped and never reset the rest of the config", () => {
|
|
541
|
+
const cfg = mod.cfgOf({
|
|
542
|
+
mode: "enforce",
|
|
543
|
+
fixtures: [{ text: "no name" }, 42, { name: "ok", text: "t" }],
|
|
544
|
+
});
|
|
545
|
+
expect(cfg.mode).toBe("enforce");
|
|
546
|
+
expect(mod.fixturesOf(cfg.fixtures)).toEqual([{ name: "ok", text: "t" }]);
|
|
547
|
+
expect(mod.fixturesOf("not an array")).toEqual([]);
|
|
548
|
+
const many = Array.from({ length: 12 }, (_, i) => ({ name: `f${i}`, text: "t" }));
|
|
549
|
+
expect(mod.fixturesOf(many)).toHaveLength(mod.MAX_FIXTURES);
|
|
435
550
|
});
|
|
436
551
|
});
|
|
437
552
|
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, mock, test } from "bun:test";
|
|
2
|
+
import { createServer, type Server } from "node:http";
|
|
3
|
+
import { connect } from "node:net";
|
|
4
|
+
import { listenOnFreePort } from "./test-net";
|
|
5
|
+
|
|
6
|
+
// Events nothing handles (check_run, workflow_run, push, ...) are acknowledged
|
|
7
|
+
// before their body is read; handled events and ping keep the signature check.
|
|
8
|
+
|
|
9
|
+
const GOOD_SIGNATURE = "sha256=good";
|
|
10
|
+
let handlePullRequestCalls = 0;
|
|
11
|
+
let handleCheckRunCalls = 0;
|
|
12
|
+
|
|
13
|
+
const realGithub = await import("../github");
|
|
14
|
+
mock.module("../github", () => ({
|
|
15
|
+
...realGithub,
|
|
16
|
+
isGitHubEnabled: () => true,
|
|
17
|
+
verifyWebhookSignature: async (_payload: string, signature: string | null) =>
|
|
18
|
+
signature === GOOD_SIGNATURE,
|
|
19
|
+
handlePullRequest: async () => {
|
|
20
|
+
handlePullRequestCalls++;
|
|
21
|
+
return { created: false };
|
|
22
|
+
},
|
|
23
|
+
handleCheckRun: async () => {
|
|
24
|
+
handleCheckRunCalls++;
|
|
25
|
+
return { created: false };
|
|
26
|
+
},
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
const { handleWebhooks } = await import("../http/webhooks");
|
|
30
|
+
|
|
31
|
+
let server: Server;
|
|
32
|
+
let baseUrl: string;
|
|
33
|
+
let port: number;
|
|
34
|
+
|
|
35
|
+
beforeAll(async () => {
|
|
36
|
+
server = createServer(async (req, res) => {
|
|
37
|
+
const handled = await handleWebhooks(req, res, ["api", "github", "webhook"]);
|
|
38
|
+
if (!handled) {
|
|
39
|
+
res.writeHead(404);
|
|
40
|
+
res.end();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
port = await listenOnFreePort(server);
|
|
44
|
+
baseUrl = `http://127.0.0.1:${port}/api/github/webhook`;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterAll(() => {
|
|
48
|
+
server.closeAllConnections();
|
|
49
|
+
server.close();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
function post(event: string | null, body: string, signature: string | null) {
|
|
53
|
+
const headers: Record<string, string> = { "content-type": "application/json" };
|
|
54
|
+
if (event !== null) headers["x-github-event"] = event;
|
|
55
|
+
if (signature !== null) headers["x-hub-signature-256"] = signature;
|
|
56
|
+
return fetch(baseUrl, { method: "POST", headers, body });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
describe("GitHub webhook early ack", () => {
|
|
60
|
+
test("unhandled event is acked without a signature and never reaches its handler", async () => {
|
|
61
|
+
const logs: string[] = [];
|
|
62
|
+
const origLog = console.log;
|
|
63
|
+
console.log = (...a: unknown[]) => {
|
|
64
|
+
logs.push(a.join(" "));
|
|
65
|
+
};
|
|
66
|
+
try {
|
|
67
|
+
const noSig = await post("check_run", JSON.stringify({ action: "completed" }), null);
|
|
68
|
+
expect(noSig.status).toBe(200);
|
|
69
|
+
expect(await noSig.json()).toEqual({ created: false });
|
|
70
|
+
|
|
71
|
+
const badSig = await post("workflow_run", "{}", "sha256=bad");
|
|
72
|
+
expect(badSig.status).toBe(200);
|
|
73
|
+
expect(await badSig.json()).toEqual({ created: false });
|
|
74
|
+
} finally {
|
|
75
|
+
console.log = origLog;
|
|
76
|
+
}
|
|
77
|
+
expect(handleCheckRunCalls).toBe(0);
|
|
78
|
+
expect(logs.some((l) => l.includes("[GitHub] Received"))).toBe(false);
|
|
79
|
+
expect(logs.some((l) => l.includes("Invalid webhook signature"))).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("response arrives before a slow body finishes and the server stays healthy", async () => {
|
|
83
|
+
const bodyDelayMs = 400;
|
|
84
|
+
const payload = JSON.stringify({ action: "completed", pad: "x".repeat(1000) });
|
|
85
|
+
const socket = connect(port, "127.0.0.1");
|
|
86
|
+
await new Promise<void>((resolve) => socket.once("connect", () => resolve()));
|
|
87
|
+
const start = Date.now();
|
|
88
|
+
const gotResponse = new Promise<number>((resolve) => {
|
|
89
|
+
socket.once("data", () => resolve(Date.now() - start));
|
|
90
|
+
});
|
|
91
|
+
socket.write(
|
|
92
|
+
[
|
|
93
|
+
"POST /api/github/webhook HTTP/1.1",
|
|
94
|
+
"Host: localhost",
|
|
95
|
+
"x-github-event: check_run",
|
|
96
|
+
"content-type: application/json",
|
|
97
|
+
`content-length: ${Buffer.byteLength(payload)}`,
|
|
98
|
+
"",
|
|
99
|
+
"",
|
|
100
|
+
].join("\r\n"),
|
|
101
|
+
);
|
|
102
|
+
socket.write(payload.slice(0, 10));
|
|
103
|
+
const elapsed = await gotResponse;
|
|
104
|
+
expect(elapsed).toBeLessThan(bodyDelayMs);
|
|
105
|
+
await Bun.sleep(bodyDelayMs);
|
|
106
|
+
socket.end(payload.slice(10));
|
|
107
|
+
socket.destroy();
|
|
108
|
+
|
|
109
|
+
const next = await post("pull_request", "{}", "sha256=bad");
|
|
110
|
+
expect(next.status).toBe(401);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("handled event with a bad signature is still rejected", async () => {
|
|
114
|
+
const res = await post("pull_request", "{}", "sha256=bad");
|
|
115
|
+
expect(res.status).toBe(401);
|
|
116
|
+
expect(handlePullRequestCalls).toBe(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("ping with a bad signature is rejected, with a good one returns pong", async () => {
|
|
120
|
+
expect((await post("ping", "{}", "sha256=bad")).status).toBe(401);
|
|
121
|
+
const ok = await post("ping", "{}", GOOD_SIGNATURE);
|
|
122
|
+
expect(ok.status).toBe(200);
|
|
123
|
+
expect(await ok.json()).toEqual({ message: "pong" });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("missing event header with a bad signature is still rejected", async () => {
|
|
127
|
+
const res = await post(null, "{}", "sha256=bad");
|
|
128
|
+
expect(res.status).toBe(401);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("handled event with a good signature still runs its handler", async () => {
|
|
132
|
+
const res = await post(
|
|
133
|
+
"pull_request",
|
|
134
|
+
JSON.stringify({
|
|
135
|
+
action: "opened",
|
|
136
|
+
repository: { full_name: "acme/repo" },
|
|
137
|
+
pull_request: { number: 1, title: "t", body: "", html_url: "u", user: { login: "a" } },
|
|
138
|
+
}),
|
|
139
|
+
GOOD_SIGNATURE,
|
|
140
|
+
);
|
|
141
|
+
expect(res.status).toBe(200);
|
|
142
|
+
expect(handlePullRequestCalls).toBe(1);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -154,6 +154,8 @@ describe("Health & Core", () => {
|
|
|
154
154
|
expect(status).toBe(200);
|
|
155
155
|
expect(body.status).toBe("ok");
|
|
156
156
|
expect(body.version).toBeDefined();
|
|
157
|
+
// Unauthenticated: no integration state, Slack included.
|
|
158
|
+
expect(Object.keys(body).sort()).toEqual(["status", "version"]);
|
|
157
159
|
});
|
|
158
160
|
|
|
159
161
|
test("OPTIONS returns 204 (CORS preflight)", async () => {
|