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37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import path from "node:path";
import { performance } from "node:perf_hooks";
import { pathToFileURL } from "node:url";
import { toErrorObject } from "@openclaw/normalization-core/error-coercion";
import type { DB as OpenClawStateKyselyDatabase } from "../src/state/openclaw-state-db.generated.js";
import {
  WORKER_RESULT_SENTINEL,
  type MemorySample,
  type RegistryLifecycleCounts,
  type RegistrySnapshot,
  type RetainedMemoryMetrics,
  type WorkerResult,
} from "./bench-task-registry-sqlite.js";
import { classifyBoundedUnsignedDecimal } from "./lib/arg-utils.mts";

type WorkerOptions = {
  size: number;
  cycles: number;
  warmup: number;
  stateDir: string;
};

type TimingSample = {
  registrationMs: number;
  terminalMs: number;
  teardownMs: number;
  registration: RegistrySnapshot;
  terminal: RegistrySnapshot;
  teardown: RegistrySnapshot;
};

type BenchmarkStateDatabase = Pick<
  OpenClawStateKyselyDatabase,
  "task_delivery_state" | "task_runs"
>;

type TaskRecordApi = Pick<
  typeof import("../src/tasks/task-registry-record-api.js"),
  "createTaskRecord" | "markTaskTerminalById"
>;

type TaskRegistryQueryApi = Pick<
  typeof import("../src/tasks/task-registry-query.js"),
  "deleteTaskRecordById"
>;

function parseInteger(raw: string | undefined, flag: string, min: number, max: number): number {
  const result = classifyBoundedUnsignedDecimal(raw, min, max);
  if (result.kind === "syntax") {
    throw new Error(`${flag} must be an integer`);
  }
  if (result.kind !== "value") {
    throw new Error(`${flag} must be between ${min} and ${max}`);
  }
  return result.value;
}

function parseOptions(argv: string[]): WorkerOptions {
  const values = new Map<string, string>();
  for (let index = 0; index < argv.length; index += 2) {
    const flag = argv[index];
    const value = argv[index + 1];
    if (!flag?.startsWith("--") || !value || value.startsWith("--")) {
      throw new Error(`invalid worker argument near ${flag ?? "end"}`);
    }
    if (values.has(flag)) {
      throw new Error(`${flag} was provided more than once`);
    }
    values.set(flag, value);
  }
  const stateDir = values.get("--state-dir");
  if (!stateDir || !path.isAbsolute(stateDir)) {
    throw new Error("--state-dir must be an absolute path");
  }
  return {
    size: parseInteger(values.get("--size"), "--size", 1, 4096),
    cycles: parseInteger(values.get("--cycles"), "--cycles", 1, 200),
    warmup: parseInteger(values.get("--warmup"), "--warmup", 0, 20),
    stateDir,
  };
}

function processPeakRssBytes(): number {
  return Math.max(0, Math.round(process.resourceUsage().maxRSS * 1024));
}

function forceGc(): void {
  const gc = (globalThis as typeof globalThis & { gc?: () => void }).gc;
  if (!gc) {
    throw new Error("benchmark worker requires --expose-gc");
  }
  gc();
  gc();
}

function readMemorySample(cycle: number): MemorySample {
  const memory = process.memoryUsage();
  return {
    cycle,
    heapUsedBytes: memory.heapUsed,
    heapTotalBytes: memory.heapTotal,
    rssBytes: memory.rss,
    externalBytes: memory.external,
    arrayBuffersBytes: memory.arrayBuffers,
    processPeakRssBytes: processPeakRssBytes(),
  };
}

function slope(values: number[]): number {
  if (values.length < 2) {
    return 0;
  }
  const xMean = (values.length - 1) / 2;
  const yMean = values.reduce((sum, value) => sum + value, 0) / values.length;
  let numerator = 0;
  let denominator = 0;
  for (let index = 0; index < values.length; index += 1) {
    const xDelta = index - xMean;
    numerator += xDelta * ((values[index] ?? 0) - yMean);
    denominator += xDelta * xDelta;
  }
  return denominator === 0 ? 0 : numerator / denominator;
}

function retainedMemorySlopes(samples: MemorySample[]): RetainedMemoryMetrics {
  return {
    heapUsedBytes: slope(samples.map((sample) => sample.heapUsedBytes)),
    heapTotalBytes: slope(samples.map((sample) => sample.heapTotalBytes)),
    rssBytes: slope(samples.map((sample) => sample.rssBytes)),
    externalBytes: slope(samples.map((sample) => sample.externalBytes)),
    arrayBuffersBytes: slope(samples.map((sample) => sample.arrayBuffersBytes)),
  };
}

function retainedMemoryDelta(
  baseline: MemorySample,
  finalSample: MemorySample,
): RetainedMemoryMetrics {
  return {
    heapUsedBytes: finalSample.heapUsedBytes - baseline.heapUsedBytes,
    heapTotalBytes: finalSample.heapTotalBytes - baseline.heapTotalBytes,
    rssBytes: finalSample.rssBytes - baseline.rssBytes,
    externalBytes: finalSample.externalBytes - baseline.externalBytes,
    arrayBuffersBytes: finalSample.arrayBuffersBytes - baseline.arrayBuffersBytes,
  };
}

function assertLifecycleCounts(
  actual: RegistryLifecycleCounts,
  expected: RegistryLifecycleCounts,
  phase: string,
  surface: string,
): void {
  for (const field of Object.keys(expected) as Array<keyof RegistryLifecycleCounts>) {
    if (actual[field] !== expected[field]) {
      throw new Error(
        `${phase} ${surface} invariant failed: ${JSON.stringify({ expected, actual })}`,
      );
    }
  }
}

function assertSnapshot(
  actual: RegistrySnapshot,
  expected: RegistryLifecycleCounts,
  phase: string,
): void {
  for (const surface of ["memory", "sqlite"] as const) {
    assertLifecycleCounts(actual[surface], expected, phase, surface);
  }
}

async function createCountReader() {
  const [{ executeSqliteQuerySync, getNodeSqliteKysely }, stateDb, state] = await Promise.all([
    import("../src/infra/kysely-sync.js"),
    import("../src/state/openclaw-state-db.js"),
    import("../src/tasks/task-registry-state.js"),
  ]);
  return (): RegistrySnapshot => {
    const database = stateDb.openOpenClawStateDatabase();
    const db = getNodeSqliteKysely<BenchmarkStateDatabase>(database.db);
    const taskRows = executeSqliteQuerySync(
      database.db,
      db.selectFrom("task_runs").select(["status", "delivery_status", "terminal_outcome"]),
    ).rows;
    const deliveryRows = executeSqliteQuerySync(
      database.db,
      db.selectFrom("task_delivery_state").select(({ fn }) => fn.countAll<number>().as("count")),
    ).rows[0]?.count;
    const summarize = (
      taskRecords: Iterable<{
        status: string;
        deliveryStatus: string;
        terminalOutcome?: string | null;
      }>,
      deliveryStateCount: number,
    ): RegistryLifecycleCounts => {
      let taskCount = 0;
      let runningTasks = 0;
      let succeededTasks = 0;
      let pendingDeliveryTasks = 0;
      let succeededTerminalOutcomes = 0;
      for (const task of taskRecords) {
        taskCount += 1;
        runningTasks += task.status === "running" ? 1 : 0;
        succeededTasks += task.status === "succeeded" ? 1 : 0;
        pendingDeliveryTasks += task.deliveryStatus === "pending" ? 1 : 0;
        succeededTerminalOutcomes += task.terminalOutcome === "succeeded" ? 1 : 0;
      }
      return {
        taskCount,
        deliveryStateCount,
        runningTasks,
        succeededTasks,
        pendingDeliveryTasks,
        succeededTerminalOutcomes,
      };
    };
    return {
      memory: summarize(
        [...state.tasks.values()].map((task) => ({
          status: task.status,
          deliveryStatus: task.deliveryStatus,
          terminalOutcome: task.terminalOutcome,
        })),
        state.taskDeliveryStates.size,
      ),
      sqlite: summarize(
        taskRows.map((task) => ({
          status: task.status,
          deliveryStatus: task.delivery_status,
          terminalOutcome: task.terminal_outcome,
        })),
        deliveryRows ?? 0,
      ),
    };
  };
}

async function runCycle(
  size: number,
  serial: number,
  readSnapshot: () => RegistrySnapshot,
  taskRecordApi: TaskRecordApi,
  taskRegistryQuery: TaskRegistryQueryApi,
): Promise<TimingSample> {
  const taskIds: string[] = [];
  const startedAt = Date.now();
  const registrationStartedAt = performance.now();
  for (let index = 0; index < size; index += 1) {
    const task = taskRecordApi.createTaskRecord({
      runtime: "subagent",
      requesterSessionKey: "agent:benchmark:main",
      ownerKey: "agent:benchmark:main",
      scopeKind: "session",
      requesterOrigin: { channel: "benchmark", to: "task-registry" },
      childSessionKey: `agent:benchmark:subagent:${serial}:${index}`,
      runId: `benchmark-task-${serial}-${index}`,
      task: `durable registration benchmark ${index}`,
      status: "running",
      deliveryStatus: "pending",
      notifyPolicy: "silent",
      startedAt,
      lastEventAt: startedAt,
    });
    if (!task) {
      throw new Error(`task registration failed at ${index + 1}/${size}`);
    }
    taskIds.push(task.taskId);
  }
  const registrationMs = performance.now() - registrationStartedAt;
  const emptyCounts: RegistryLifecycleCounts = {
    taskCount: 0,
    deliveryStateCount: 0,
    runningTasks: 0,
    succeededTasks: 0,
    pendingDeliveryTasks: 0,
    succeededTerminalOutcomes: 0,
  };
  const registration = readSnapshot();
  assertSnapshot(
    registration,
    {
      ...emptyCounts,
      taskCount: size,
      deliveryStateCount: size,
      runningTasks: size,
      pendingDeliveryTasks: size,
    },
    "registration",
  );

  const terminalStartedAt = performance.now();
  for (const taskId of taskIds) {
    const endedAt = Date.now();
    const task = taskRecordApi.markTaskTerminalById({
      taskId,
      status: "succeeded",
      endedAt,
      lastEventAt: endedAt,
      terminalOutcome: "succeeded",
    });
    if (!task) {
      throw new Error(`terminal transition failed for task ${taskId}`);
    }
  }
  const terminalMs = performance.now() - terminalStartedAt;
  const terminal = readSnapshot();
  assertSnapshot(
    terminal,
    {
      ...emptyCounts,
      taskCount: size,
      deliveryStateCount: size,
      succeededTasks: size,
      pendingDeliveryTasks: size,
      succeededTerminalOutcomes: size,
    },
    "terminal",
  );

  const teardownStartedAt = performance.now();
  for (const taskId of taskIds) {
    if (!taskRegistryQuery.deleteTaskRecordById(taskId)) {
      throw new Error(`teardown failed for task ${taskId}`);
    }
  }
  const teardownMs = performance.now() - teardownStartedAt;
  const teardown = readSnapshot();
  assertSnapshot(teardown, emptyCounts, "teardown");
  return { registrationMs, terminalMs, teardownMs, registration, terminal, teardown };
}

async function resetRuntime(persist: boolean): Promise<void> {
  const [tasks, stateDb] = await Promise.all([
    import("../src/tasks/task-runtime.test-helpers.js"),
    import("../src/state/openclaw-state-db.js"),
  ]);
  tasks.resetTaskRegistryForTests({ persist });
  stateDb.closeOpenClawStateDatabaseForTest();
}

async function runBenchmark(options: WorkerOptions): Promise<WorkerResult> {
  await resetRuntime(true);
  // Load lifecycle owners before the baseline so warmup=0 still measures task churn,
  // not one-time module initialization retained by the worker.
  const [readSnapshot, taskRecordApi, taskRegistryQuery] = await Promise.all([
    createCountReader(),
    import("../src/tasks/task-registry-record-api.js"),
    import("../src/tasks/task-registry-query.js"),
  ]);
  const emptyCounts: RegistryLifecycleCounts = {
    taskCount: 0,
    deliveryStateCount: 0,
    runningTasks: 0,
    succeededTasks: 0,
    pendingDeliveryTasks: 0,
    succeededTerminalOutcomes: 0,
  };
  assertSnapshot(readSnapshot(), emptyCounts, "initial");
  const timingsMs = {
    registration: [] as number[],
    terminal: [] as number[],
    teardown: [] as number[],
  };
  const postGcSamples: MemorySample[] = [];
  let lastSample: TimingSample | undefined;
  for (let index = 0; index < options.warmup; index += 1) {
    await runCycle(options.size, index, readSnapshot, taskRecordApi, taskRegistryQuery);
    forceGc();
  }
  forceGc();
  const postGcBaseline = readMemorySample(-1);
  for (let index = 0; index < options.cycles; index += 1) {
    lastSample = await runCycle(
      options.size,
      options.warmup + index,
      readSnapshot,
      taskRecordApi,
      taskRegistryQuery,
    );
    forceGc();
    timingsMs.registration.push(lastSample.registrationMs);
    timingsMs.terminal.push(lastSample.terminalMs);
    timingsMs.teardown.push(lastSample.teardownMs);
    postGcSamples.push(readMemorySample(index));
  }
  if (!lastSample) {
    throw new Error("benchmark completed without a cycle");
  }
  const finalPostGcSample = postGcSamples.at(-1);
  if (!finalPostGcSample) {
    throw new Error("benchmark completed without a post-GC sample");
  }
  return {
    size: options.size,
    timingsMs,
    memory: {
      postGcBaseline,
      postGcSamples,
      retainedSlopesBytesPerCycle: retainedMemorySlopes(postGcSamples),
      retainedDeltasBytes: retainedMemoryDelta(postGcBaseline, finalPostGcSample),
      processPeakRssBytes: processPeakRssBytes(),
    },
    invariant: {
      ok: true,
      cyclesValidated: options.warmup + options.cycles,
      registration: lastSample.registration,
      terminal: lastSample.terminal,
      teardown: lastSample.teardown,
      serializedSharedConnection: true,
    },
  };
}

async function main(): Promise<void> {
  const options = parseOptions(process.argv.slice(2));
  const previousStateDir = process.env.OPENCLAW_STATE_DIR;
  const previousNodeEnv = process.env.NODE_ENV;
  let result: WorkerResult | undefined;
  let failure: unknown;
  process.env.OPENCLAW_STATE_DIR = options.stateDir;
  process.env.NODE_ENV = "test";
  try {
    const { pinRuntimePaths } = await import("../src/config/paths.js");
    pinRuntimePaths();
    result = await runBenchmark(options);
  } catch (error) {
    failure = error;
  } finally {
    try {
      await resetRuntime(false);
    } catch (error) {
      failure ??= error;
    } finally {
      if (previousStateDir === undefined) {
        delete process.env.OPENCLAW_STATE_DIR;
      } else {
        process.env.OPENCLAW_STATE_DIR = previousStateDir;
      }
      if (previousNodeEnv === undefined) {
        delete process.env.NODE_ENV;
      } else {
        process.env.NODE_ENV = previousNodeEnv;
      }
    }
  }
  if (failure) {
    throw toErrorObject(failure, "Task registry SQLite benchmark failed");
  }
  if (!result) {
    throw new Error("benchmark worker completed without a result");
  }
  process.stdout.write(`${WORKER_RESULT_SENTINEL}${JSON.stringify(result)}\n`);
}

if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
  try {
    await main();
  } catch (error) {
    console.error(error instanceof Error ? error.stack : String(error));
    process.exitCode = 1;
  } finally {
    if (process.exitCode && process.exitCode !== 0) {
      console.error(`[bench-task-registry-sqlite-worker] FAILED (exit ${process.exitCode})`);
    }
  }
}