Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Skip the inline session claim while the awaited run is running (#2525)

Regan Bell9eb085cdb7ac
diff --git a/package.json b/package.json
index 4c268892e..255b49e7a 100644
--- a/package.json
+++ b/package.json
@@ -48,7 +48,7 @@
"sandbox:local:build": "bash scripts/local-sandbox-build.sh",
"sandbox:porter:build": "docker build --platform linux/amd64 -t \"${PORTER_SANDBOX_IMAGE:?Set PORTER_SANDBOX_IMAGE to the registry tag to publish}\" porter && docker push \"$PORTER_SANDBOX_IMAGE\"",
"deploy:porter-runner-image": "docker build --platform linux/amd64 -t \"${PORTER_DEPLOY_RUNNER_IMAGE:?Set PORTER_DEPLOY_RUNNER_IMAGE to the registry tag to publish}\" -f deploy/porter-runner/Dockerfile . && docker push \"$PORTER_DEPLOY_RUNNER_IMAGE\"",
- "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-toolcalls-migration.test.ts test/postgres-tapeless-store.test.ts test/semantic-memory-pg.test.ts test/semantic-memory-http.test.ts test/semantic-memory-large-pg.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-acl-file-id-migration.test.ts test/postgres-admin-grants.test.ts test/postgres-teams.test.ts test/postgres-decisions.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-linked-identity-membership.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-cron-run-as-migration.test.ts test/postgres-trigger-recipient-consent-migration.test.ts test/postgres-legacy-harness-migration.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/sentry-tracing-pg.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-app-page-views.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/run-priority.test.ts test/postgres-run-stream.test.ts test/postgres-items-migration.test.ts test/postgres-items-contract.test.ts test/pi-harness-replay-fidelity.test.ts test/migrate-main.test.ts",
+ "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-toolcalls-migration.test.ts test/postgres-tapeless-store.test.ts test/semantic-memory-pg.test.ts test/semantic-memory-http.test.ts test/semantic-memory-large-pg.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-acl-file-id-migration.test.ts test/postgres-admin-grants.test.ts test/postgres-teams.test.ts test/postgres-decisions.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-linked-identity-membership.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-cron-run-as-migration.test.ts test/postgres-trigger-recipient-consent-migration.test.ts test/postgres-legacy-harness-migration.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/sentry-tracing-pg.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-app-page-views.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/run-priority.test.ts test/inline-wait-claims.test.ts test/postgres-run-stream.test.ts test/postgres-items-migration.test.ts test/postgres-items-contract.test.ts test/pi-harness-replay-fidelity.test.ts test/migrate-main.test.ts",
"livetest": "node scripts/api-livetest.ts",
"bench:memory": "node --env-file-if-exists=.env scripts/memory-bench.ts",
"prepare": "node .husky/install.mjs",
diff --git a/src/api/app-helpers.ts b/src/api/app-helpers.ts
index 4fb4cb9d7..043cb1508 100644
--- a/src/api/app-helpers.ts
+++ b/src/api/app-helpers.ts
@@ -217,7 +217,8 @@ export function createAppHelpers(deps: AppDeps, app: App) {
}
const shutdown = deps.shutdown?.();
if (shutdown?.aborted) return { status: "queued", sessionId: run.sessionId, runId };
- const claimed = await deps.runs.claimForSession(run.sessionId, "inline", deps.leaseTtlMs);
+ const claimed =
+ run.status === "running" ? null : await deps.runs.claimForSession(run.sessionId, "inline", deps.leaseTtlMs);
if (claimed) {
try {
const result = await processRun(
diff --git a/test/inline-wait-claims.test.ts b/test/inline-wait-claims.test.ts
new file mode 100644
index 000000000..6f2259bb5
--- /dev/null
+++ b/test/inline-wait-claims.test.ts
@@ -0,0 +1,62 @@
+import "./support/auto-fake-sprites.ts";
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { randomUUID } from "node:crypto";
+import { mkdtempSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { setTimeout as sleep } from "node:timers/promises";
+import { buildApp } from "./support/build-app.ts";
+import { testConfig } from "./support/test-config.ts";
+import type { TurnRequest } from "../src/types.ts";
+
+const databaseUrl = process.env.DATABASE_URL;
+
+for (const backend of ["memory", "postgres"] as const) {
+ test(
+ `${backend}: a synchronous turn waiting on a run another worker holds does not poll claims`,
+ { skip: backend === "postgres" && !databaseUrl, timeout: 30_000 },
+ async () => {
+ const built = buildApp(
+ testConfig({
+ runWaitMs: 5_000,
+ ...(backend === "postgres"
+ ? {
+ databaseUrl: databaseUrl!,
+ sessionStore: "postgres" as const,
+ runStore: "postgres" as const,
+ dataDir: mkdtempSync(join(tmpdir(), "inline-wait-")),
+ }
+ : {}),
+ }),
+ );
+ try {
+ const request: TurnRequest = {
+ surface: "test",
+ actor: { externalId: "U1" },
+ conversation: { kind: "dm", threadRef: `inline-wait-${randomUUID()}` },
+ text: "hello",
+ idempotencyKey: `held-${randomUUID()}`,
+ };
+ const queued = await built.app.turn({ ...request, async: true });
+ const held = await built.runs.claimById(queued.runId!, "other-worker", 60_000);
+ assert.ok(held?.leaseToken);
+ let claims = 0;
+ const claimForSession = built.runs.claimForSession.bind(built.runs);
+ built.runs.claimForSession = async (...args) => {
+ claims += 1;
+ return claimForSession(...args);
+ };
+ const waiting = built.app.turn(request);
+ await sleep(500);
+ assert.equal(claims, 0);
+ await built.runs.complete(held.id, held.leaseToken, { status: "ok", reply: "done elsewhere" });
+ const result = await waiting;
+ assert.equal(result.status, "ok");
+ assert.equal(result.reply, "done elsewhere");
+ } finally {
+ await built.runtime.stop();
+ }
+ },
+ );
+}