@capybara-hq/testing 0.1.0-rc.15 → 0.1.0-rc.16

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.
@@ -108,7 +108,13 @@ export interface Harness {
108
108
  * 另一个副本照常工作。已退出时是空操作。
109
109
  */
110
110
  kill(index: number): Promise<void>;
111
- /** 停两个副本(15s 后 SIGKILL)、删临时库、关自有容器;幂等。 */
111
+ /**
112
+ * 停两个副本(15s 后 SIGKILL)、删临时库、关自有容器;幂等。Before the replicas stop it asserts
113
+ * that they left no queued run without its job (neither in the outbox nor in BullMQ: the "queue
114
+ * lost jobs" canary of the kernel's run-orphan-check, transactional outbox spec §11); after the
115
+ * cleanup it rejects naming such runs, or with the check's own error. A database or Redis it
116
+ * cannot reach skips the check.
117
+ */
112
118
  stop(): Promise<void>;
113
119
  }
114
120
  /**
@@ -3,6 +3,7 @@ import { randomBytes } from "node:crypto";
3
3
  import { createRequire } from "node:module";
4
4
  import { extname, resolve } from "node:path";
5
5
  import { fileURLToPath, pathToFileURL } from "node:url";
6
+ import { Redis } from "ioredis";
6
7
  import { createFreshDatabase, sharedPgAdminUrl, startPostgresContainer, startTestRedis, } from "./containers.js";
7
8
  /** 副本在响应头里标明自己的序号(runner 把 `http.replicaId` 设为 `"0"` / `"1"`)。 */
8
9
  export const REPLICA_HEADER = "x-capybara-replica";
@@ -20,6 +21,41 @@ const ALL_ROLES = ["api", "worker", "gateway"];
20
21
  const STOP_TIMEOUT_MS = 15_000;
21
22
  const START_TIMEOUT_MS = 60_000;
22
23
  const STDERR_TAIL = 4000;
24
+ /**
25
+ * The queued runs of the harness's database whose job is neither in the outbox nor in BullMQ (the
26
+ * kernel's `queuedRunsWithoutJob`; a BullMQ job is the hash `bull:<queue>:<jobId>`). Null only when
27
+ * the database or Redis cannot be reached (a test that stopped them: the check is skipped); any
28
+ * other error is thrown.
29
+ */
30
+ async function queuedOrphansOf(dbUrl, redisUrl) {
31
+ // loaded here, not at the top: a process that only spawns replicas (or is killed) never needs
32
+ // the kernel in its own module graph
33
+ const { createDb, kernelDb, queuedRunsWithoutJob } = await import("@capybara-hq/kernel");
34
+ const { sql } = await import("kysely");
35
+ const raw = createDb(dbUrl, 1, () => { }, { connectTimeoutMs: 5000 });
36
+ const redis = new Redis(redisUrl, {
37
+ lazyConnect: true,
38
+ connectTimeout: 5000,
39
+ maxRetriesPerRequest: 1,
40
+ retryStrategy: () => null,
41
+ });
42
+ redis.on("error", () => { });
43
+ try {
44
+ try {
45
+ await redis.connect();
46
+ await sql `SELECT 1`.execute(raw);
47
+ }
48
+ catch {
49
+ return null;
50
+ }
51
+ const found = await queuedRunsWithoutJob(kernelDb(raw), async (queue, jobId) => (await redis.exists(`bull:${queue}:${jobId}`)) === 1, { minAgeMs: 0 });
52
+ return found.map((c) => `${c.runId} (conversation ${c.conversationId}, job ${c.jobId})`);
53
+ }
54
+ finally {
55
+ redis.disconnect();
56
+ await raw.destroy().catch(() => { });
57
+ }
58
+ }
23
59
  /** runner 与本文件同目录;源码下是 .ts,编译后是 .js。 */
24
60
  const runnerPath = fileURLToPath(new URL(`./replica-runner${extname(fileURLToPath(import.meta.url))}`, import.meta.url));
25
61
  /**
@@ -120,6 +156,8 @@ export async function createDualReplicaHarness(opts) {
120
156
  const runners = [];
121
157
  let dropDb;
122
158
  let stopPromise;
159
+ /** Set once both replicas started: `stop()` then checks for queued runs without a job. */
160
+ let checkOrphans;
123
161
  const stopRunner = async (r) => {
124
162
  if (r.child.exitCode !== null || r.child.signalCode !== null)
125
163
  return;
@@ -138,9 +176,18 @@ export async function createDualReplicaHarness(opts) {
138
176
  };
139
177
  const stop = () => {
140
178
  stopPromise ??= (async () => {
179
+ const checked = await checkOrphans?.().then((orphans) => ({ orphans }), (error) => ({ error }));
141
180
  await Promise.allSettled(runners.map(stopRunner));
142
181
  await Promise.allSettled([dropDb?.()]);
143
182
  await Promise.allSettled(owned.map((c) => c.stop()));
183
+ if (checked && "error" in checked)
184
+ throw checked.error;
185
+ const orphans = checked?.orphans ?? null;
186
+ if (orphans !== null && orphans.length > 0) {
187
+ throw new Error(`the replicas left ${orphans.length} queued run(s) without a job (neither owed by the ` +
188
+ `outbox nor in BullMQ; the run-orphan-check would report "the queue lost jobs"): ` +
189
+ orphans.join(", "));
190
+ }
144
191
  })();
145
192
  return stopPromise;
146
193
  };
@@ -216,6 +263,7 @@ export async function createDualReplicaHarness(opts) {
216
263
  };
217
264
  let calls = 0;
218
265
  let extraChain = Promise.resolve();
266
+ checkOrphans = () => queuedOrphansOf(db.url, redisUrl);
219
267
  const kill = async (index) => {
220
268
  const { runner: r } = runnerAt(index);
221
269
  if (r.child.exitCode === null && r.child.signalCode === null)
@@ -97,7 +97,8 @@ export declare function joinOrg(k: Pick<TestKernel, "http">, owner: KernelReques
97
97
  }>;
98
98
  /**
99
99
  * Creates an org-visible agent; returns its id. An agent has every tool the org offers unless
100
- * `disabledTools` turns it off (tools default-on spec); with several tools model-fake answers
100
+ * `disabledTools` turns it off (tools default-on spec), and every skill on in the org unless
101
+ * `disabledSkills` turns it off (skills default-on spec); with several tools model-fake answers
101
102
  * ordinary messages instead of calling one.
102
103
  */
103
104
  export declare function createAgent(req: KernelRequest, orgId: string, opts?: {
@@ -106,6 +107,7 @@ export declare function createAgent(req: KernelRequest, orgId: string, opts?: {
106
107
  model: string;
107
108
  };
108
109
  disabledTools?: unknown[];
110
+ disabledSkills?: unknown[];
109
111
  }): Promise<string>;
110
112
  /** Creates a conversation with the agent; returns its id. */
111
113
  export declare function createConversation(req: KernelRequest, orgId: string, agentId: string): Promise<string>;
@@ -179,7 +179,8 @@ export async function joinOrg(k, owner, orgId, name, role = "member") {
179
179
  }
180
180
  /**
181
181
  * Creates an org-visible agent; returns its id. An agent has every tool the org offers unless
182
- * `disabledTools` turns it off (tools default-on spec); with several tools model-fake answers
182
+ * `disabledTools` turns it off (tools default-on spec), and every skill on in the org unless
183
+ * `disabledSkills` turns it off (skills default-on spec); with several tools model-fake answers
183
184
  * ordinary messages instead of calling one.
184
185
  */
185
186
  export async function createAgent(req, orgId, opts = {}) {
@@ -192,6 +193,7 @@ export async function createAgent(req, orgId, opts = {}) {
192
193
  instructions: "Be helpful.",
193
194
  model: opts.model ?? { provider: "model-fake", model: "fake-1" },
194
195
  disabledTools: opts.disabledTools ?? [],
196
+ ...(opts.disabledSkills !== undefined && { disabledSkills: opts.disabledSkills }),
195
197
  },
196
198
  visibility: "org",
197
199
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/testing",
3
- "version": "0.1.0-rc.15",
3
+ "version": "0.1.0-rc.16",
4
4
  "description": "Test helpers for Capybara: test containers, dual-replica harness and fake third-party servers",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -47,17 +47,17 @@
47
47
  "tsx": "^4.23.15",
48
48
  "ws": "^8.22.0",
49
49
  "zod": "^4.6.7",
50
- "@capybara-hq/kernel": "^0.1.0-rc.16"
50
+ "@capybara-hq/kernel": "^0.1.0-rc.24"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/pg": "^8.23.1",
54
54
  "@types/ws": "^8.18.2",
55
- "@capybara-hq/plugin-client": "^0.1.0-rc.2",
56
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.12"
55
+ "@capybara-hq/plugin-client": "^0.1.0-rc.4",
56
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.18"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "vitest": "^5.0.5",
60
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.12",
60
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.18",
61
61
  "@capybara-hq/plugin-tools": "^0.1.0-rc.13"
62
62
  },
63
63
  "peerDependenciesMeta": {