Files
PaperClipAI/server/src/__tests__/helpers/native-question-owner.mjs
T
DottaandPaperclip 73cd8eb9a5 fix(server): deliver answers promptly during web handoff (#18530)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Its web servers can overlap while Railway replaces one deployment.
> - An answer can reach the replacement while the original server owns
the native execution.
> - A competing delivery claim can make the owner miss the first
notification.
> - The normal retry interval then delays the answer by 30 seconds.
> - This pull request gives pending answers a one-second retry in
ephemeral mode and verifies delivery between separate processes.

## Linked Issues or Issue Description

Refs #18518 and #18526. This closes a measured native-question delivery
delay during control-plane web deployment overlap.

## What Changed

- Register question delivery through its owning service. Use a
one-second retry for ephemeral servers and retain the configured cadence
for ordinary servers.
- Keep the existing durable claims, response identity, authorization and
continuation behavior.
- Add a two-process PostgreSQL regression for a claim held by the
non-owning server. Verify one command, settlement, idle behavior and
reactivation by another committed answer.
- Keep the assembled startup fixture on the real registration function.
Update the deployment plan with current evidence and remaining
acceptance work.

## Verification

- The same local two-process race took 30.157 seconds before the fix and
1.197 seconds after it. The command sink is simulated; PostgreSQL,
notification transport, interaction response, delivery claim and worker
are real.
- The cross-process regression and assembled startup suite passed 122
tests. No further sweeps or pending inspections occur after settlement.
A second answer wakes the owner through PostgreSQL without a test-owned
sweep.
- Server TypeScript compilation passed. All PR CI gates passed on
d1d426c0dc, including full tests, typecheck, build, browser shards and
serialized suites. Greptile reviewed that head at 5/5 with no findings.
- Live provider continuation, hosted staging deployments and production
acceptance are still separate work.

## Risks

- An unresolved answer causes more frequent reconciliation in ephemeral
mode. Settled history does not keep the worker active. Existing claim
fencing and bounded error attempts are unchanged.
- A 30-second abandoned claim still needs its existing stale-claim
recovery. This change addresses released claims, not proof of provider
liveness.
- The test simulates the provider command sink. It cannot establish that
a paid model consumed the answer or that a remote execution survived a
deploy.
- No schema, API or UI changes.

## Model Used

OpenAI Codex, GPT-6. Tool use, code execution and repository review. The
runtime does not expose a more specific model ID or context-window size.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-01 03:39:48 -06:00

94 lines
2.7 KiB
JavaScript

// Separate process: only the provider command sink is simulated.
import { createDb } from "@paperclipai/db";
import { createDeliveryWorkCoordinator } from "../../services/delivery-work-coordinator.ts";
import { deploymentNotifications } from "../../services/deployment-notifications.ts";
import { questionResponseDeliveryService, registerQuestionResponseDeliveryWork } from "../../services/question-response-delivery.ts";
import { deliverNativeQuestionResponse, registerNativeQuestionCommandTarget } from "../../services/native-runtime/native-question-bridge.ts";
let db,
coordinator,
bridge,
worker,
release,
sweeps = 0,
inspections = 0;
process.on("message", async message => {
try {
if (message.type === "init") {
db = createDb(message.url);
coordinator = createDeliveryWorkCoordinator({
owner: db,
canRun: () => true,
onError: () => process.send({
type: "error"
})
});
bridge = deploymentNotifications(db, coordinator, {
directDatabaseUrl: message.url
});
await bridge.ready();
release = registerNativeQuestionCommandTarget({
binding: message.binding,
queueCommand: (type, payload, commandId) => {
process.send({
type: "command",
commandType: type,
requestId: commandId,
payload,
commandId
});
return {
commandId,
controllerSeq: 1
};
}
});
process.send({
type: "ready"
});
} else if (message.type === "start-worker") {
const service = questionResponseDeliveryService(db, {
heartbeat: {
wakeup: async () => {
throw new Error("unexpected fallback");
}
},
resolveNativeQuestion: interaction => deliverNativeQuestionResponse(db, interaction)
});
worker = registerQuestionResponseDeliveryWork(coordinator, {
sweepPending: () => {
sweeps++;
return service.sweepPending();
},
hasPending: () => {
inspections++;
return service.hasPending();
}
});
await worker.ready;
process.send({
type: "worker-ready"
});
} else if (message.type === "snapshot") {
process.send({
type: "snapshot",
requestId: message.requestId,
sweeps,
inspections
});
} else if (message.type === "stop") {
release?.();
await worker?.stop();
await bridge?.stop();
await coordinator?.stop();
await db?.$client.end({
timeout: 1
});
process.exit(0);
}
} catch {
process.send({
type: "error"
});
}
});