Files
PaperClipAI/tests/runner-e2e/server-entry.ts
T
2026-10-27 11:48:17 -05:00

96 lines
5.4 KiB
TypeScript

import { CHAT_SETUP_HEALTH_ID } from "./chat-setup-health.js";
import { installChatReadinessObserver } from "./chat-readiness-observer.js";
// This entrypoint is used only by isolated Runner E2E instances. Production
// service code has no test flag, delay, altered prompt, or private test API.
import { ServerResponse } from "node:http";
import { PaperclipRunnerToolAuthority } from "../../server/src/services/native-runtime/paperclip-runner-tool-authority.js";
import { canonicalDocumentIssueId, contextCommentGateSelected, holdCommittedDocumentResponse, isArmed } from "./context-comment-gate.js";
import { holdInteractionResponse } from "./interaction-response-gate.js";
import { installOwnedEmailResponseLoss } from "./chat-catalog-domains/runtime-email-fault.js";
import { installOwnedEmailReceiptDuplicate } from "./chat-catalog-domains/runtime-email-duplicate.js";
const ids: string[] = JSON.parse(process.env.PAPERCLIP_RUNNER_E2E_EXECUTION_IDS ?? "[]");
if (ids.length && ids.every(id => id === CHAT_SETUP_HEALTH_ID || id.startsWith("chat-features.runner-opencode.local."))) {
const { requireServerAdapter } = await import("../../server/src/adapters/index.js");
await installChatReadinessObserver({ executionIds: ids, temporaryRoot: process.env.PAPERCLIP_RUNNER_E2E_TEMP_ROOT ?? "",
adapter: requireServerAdapter("paperclip_runner") });
}
await installOwnedEmailResponseLoss({ executionIds: ids, attemptId: process.env.PAPERCLIP_E2E_ACCOUNTING_ATTEMPT_ID ?? "", temporaryRoot: process.env.PAPERCLIP_RUNNER_E2E_TEMP_ROOT ?? "" });
await installOwnedEmailReceiptDuplicate({ executionIds: ids, attemptId: process.env.PAPERCLIP_E2E_ACCOUNTING_ATTEMPT_ID ?? "", temporaryRoot: process.env.PAPERCLIP_RUNNER_E2E_TEMP_ROOT ?? "" });
const contextCommentGate = contextCommentGateSelected(ids);
const completionBusyGate = ids.some(id => id.startsWith("completion-updates.") && id.endsWith(".handoff-completion-busy"));
if (ids.some((id) => id.endsWith(".accept-while-running"))) {
const held = new Set<string>();
const hold = async (value: any) => {
const interaction = value?.interaction ?? value;
if (!interaction?.sourceRunId || interaction.status !== "pending" ||
!["request_confirmation", "request_checkbox_confirmation"].includes(interaction.kind) || held.has(interaction.id)) return;
held.add(interaction.id);
await holdInteractionResponse({
deadlineAt: Date.now() + 90_000,
loadStatus: async () => {
const response = await fetch(`http://127.0.0.1:${process.env.PAPERCLIP_RUNNER_E2E_PORT}/api/issues/${interaction.issueId}/interactions`);
if (!response.ok) throw new Error(`Approval barrier read failed: ${response.status}`);
const rows = await response.json() as Array<{ id: string; status: string }>;
const row = rows.find((candidate) => candidate.id === interaction.id);
if (!row) throw new Error("Approval barrier lost its committed card");
return row.status;
},
});
};
const execute = PaperclipRunnerToolAuthority.prototype.execute;
PaperclipRunnerToolAuthority.prototype.execute = async function (...args) {
const result = await execute.apply(this, args);
if (args[0].tool === "request_human_input") await hold(result);
return result;
};
const end = ServerResponse.prototype.end;
ServerResponse.prototype.end = function (this: ServerResponse, ...args: any[]) {
const body = args[0];
let interaction: any;
if (this.req.method === "POST" && /\/interactions(?:\?|$)/.test(this.req.url ?? "") &&
this.statusCode >= 200 && this.statusCode < 300 && (typeof body === "string" || Buffer.isBuffer(body))) {
try { interaction = JSON.parse(body.toString()); } catch { /* non-JSON response */ }
}
if (interaction?.sourceRunId && ["request_confirmation", "request_checkbox_confirmation"].includes(interaction.kind)) {
void hold(interaction).then(() => Reflect.apply(end, this, args), (error) => this.destroy(error));
return this;
}
return Reflect.apply(end, this, args);
} as typeof end;
}
if (contextCommentGate || completionBusyGate) {
const heldIssues = new Set<string>();
const holdFirstDocument = async (issueId: string) => {
if (heldIssues.has(issueId) || (!contextCommentGate && !await isArmed(issueId))) return;
heldIssues.add(issueId);
await holdCommittedDocumentResponse(issueId, Date.now() + (completionBusyGate ? 300_000 : 90_000));
};
const execute = PaperclipRunnerToolAuthority.prototype.execute;
PaperclipRunnerToolAuthority.prototype.execute = async function (...args) {
const result = await execute.apply(this, args);
if (args[0].tool === "write_document") {
const issueId = this.binding?.issueId;
if (typeof issueId === "string") await holdFirstDocument(issueId);
}
return result;
};
const end = ServerResponse.prototype.end;
ServerResponse.prototype.end = function (this: ServerResponse, ...args: any[]) {
const body = args[0];
const request = this.req as typeof this.req & { originalUrl?: string };
const issueId = this.req.method === "PUT"
? canonicalDocumentIssueId(request.url, body, request.originalUrl)
: undefined;
if (issueId && this.statusCode >= 200 && this.statusCode < 300) {
void holdFirstDocument(decodeURIComponent(issueId)).then(
() => Reflect.apply(end, this, args),
(error) => this.destroy(error),
);
return this;
}
return Reflect.apply(end, this, args);
} as typeof end;
}
await import("../../cli/src/index.js");