diff --git a/maps/agent-access-map/onboarding.md b/maps/agent-access-map/onboarding.md index 9c5dded3eff..5160136841c 100644 --- a/maps/agent-access-map/onboarding.md +++ b/maps/agent-access-map/onboarding.md @@ -189,6 +189,16 @@ conversational resolution with a genuine later same-task user message supplies acceptance; an agent-only resolution does not. A plan approval must name the current same-task revision. Newer pending/rejected proposals stop new creation. Already admitted task replay and ordinary delegation retain their existing rules. + +Native `paperclip_finish` does not grant completion authority from an opening +answer. The onboarding parent requires a trusted saved human decision on its +latest proposal. Acceptance permits completion under the existing policy; +rejection permits acknowledgment and closure without authorizing new work. +The native status commit checks this decision again under its existing task/run +locks. This adds no new agent endpoint or cross-company authority. The +[native completion controls](../../server/src/services/native-runtime/paperclip-control-plane-port.test.ts) +check persisted outcomes on disposable PostgreSQL; they are not live Cloud +credential or onboarding qualification. Source: [creation admission](../../server/src/services/onboarding-first-task-admission.ts). Controls: [native/REST PostgreSQL regressions](../../server/src/__tests__/onboarding-first-task-admission.test.ts). These controls do not qualify a live Cloud provider journey. diff --git a/maps/feature-map/onboarding.md b/maps/feature-map/onboarding.md index 347613e3829..d90025b2a31 100644 --- a/maps/feature-map/onboarding.md +++ b/maps/feature-map/onboarding.md @@ -142,6 +142,15 @@ admit new work. [Admission controls](../../server/src/__tests__/onboarding-first exercise native and REST calls against disposable PostgreSQL; live model and Cloud onboarding remain separate evidence. +Native completion also requires a saved human proposal decision. An opening +answer alone must leave the first task in review. Verify that premature native +completion is refused, including a result already saved before finalization. +Accepted proposals and acknowledged human rejections can close the first task; +rejection still does not authorize new tasks. The +[native completion controls](../../server/src/services/native-runtime/paperclip-control-plane-port.test.ts) +exercise these boundaries against disposable PostgreSQL. These controls do not +identify a historical Cloud failure or establish live onboarding success. + For accepted file-producing work, verify exactly one self-assigned child with a self-contained description. The onboarding run must yield through the existing dependency/block disposition so the child's own execution can publish its file. diff --git a/maps/feature-map/runs-adapters.md b/maps/feature-map/runs-adapters.md index 28d3c31f6d9..9432c9a792b 100644 --- a/maps/feature-map/runs-adapters.md +++ b/maps/feature-map/runs-adapters.md @@ -74,6 +74,14 @@ mutations. Qualify a useful model task separately from saving the account. ### `session` +For a repository project that inherits storage-plugin defaults, run a task and +send two follow-ups. Binding the workspace must preserve the effective policy, +session fingerprint, and workspace identity. An actual project-policy change +must still reset the session and preserve the stored workspace owner. The +[session admission regression](../../server/src/__tests__/heartbeat-session-preparation.test.ts) +checks this with PostgreSQL and synthetic provider execution; it is not live +warm-process or latency evidence. + For `runs-adapters/session`, complete a task in a disposable warm sandbox and retain its saved workspace file. Let the warm session close normally and verify that the retained lease has a complete, lease-bound harness backup before diff --git a/packages/db/src/client.ts b/packages/db/src/client.ts index acc568486f8..1d98d929d7a 100644 --- a/packages/db/src/client.ts +++ b/packages/db/src/client.ts @@ -506,6 +506,7 @@ const MIGRATIONS_WITH_TOP_LEVEL_STATEMENTS = new Set([ "0385_open_madame_web.sql", "0386_steady_epoch.sql", "0387_boring_professor_monster.sql", + "0384_funny_adam_warlock.sql", "0383_tool_review_company_expiry.sql", ]); diff --git a/server/src/__tests__/heartbeat-session-preparation.test.ts b/server/src/__tests__/heartbeat-session-preparation.test.ts index 784b3ff2df4..13d0544016b 100644 --- a/server/src/__tests__/heartbeat-session-preparation.test.ts +++ b/server/src/__tests__/heartbeat-session-preparation.test.ts @@ -1,9 +1,15 @@ import { randomUUID } from "node:crypto"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { eq } from "drizzle-orm"; -import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { agents, agentTaskSessions, authUsers, companies, companyMemberships, createDb, heartbeatRuns, issues, plugins, projects } from "@paperclipai/db"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { agents, agentTaskSessions, authUsers, companies, companyMemberships, createDb, executionWorkspaces, heartbeatRuns, issues, plugins, projects } from "@paperclipai/db"; import { registerServerAdapter, unregisterServerAdapter, type AdapterExecutionContext } from "../adapters/index.js"; import { heartbeatService } from "../services/heartbeat.js"; +import { instanceSettingsService } from "../services/instance-settings.js"; +import type { PluginWorkerManager } from "../services/plugin-worker-manager.js"; +import * as environmentOrchestrator from "../services/environment-run-orchestrator.js"; import { createPluginWorkspaceFixture } from "./helpers/plugin-workspaces.js"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; @@ -29,7 +35,7 @@ describePostgres("heartbeat session preparation through adapter dispatch", () => captured.set(input.runId, input); // Settle the fixture's work so no automatic unfinished-task retry // competes with the next explicit session-continuation wake. - await db.update(issues).set({ status: "in_review" }).where(eq(issues.id, String(input.context.issueId))); + await db.update(issues).set({ status: "done" }).where(eq(issues.id, String(input.context.issueId))); return { exitCode: 0, signal: null, timedOut: false, sessionId: "saved-session", sessionParams: { sessionId: "saved-session", adapterState: "preserved" }, resultJson: {} }; }, testEnvironment: async () => ({ adapterType, status: "pass", checks: [], testedAt: new Date().toISOString() }), @@ -54,6 +60,7 @@ describePostgres("heartbeat session preparation through adapter dispatch", () => } async function execute(f: Awaited>, context: Record = {}) { + await db.update(issues).set({ status: "todo" }).where(eq(issues.id, f.issueId)); const run = await heartbeat.wakeup(f.agentId, { source: "on_demand", triggerDetail: "manual", reason: "manual", payload: { issueId: f.issueId }, contextSnapshot: { issueId: f.issueId, ...context }, @@ -105,6 +112,62 @@ describePostgres("heartbeat session preparation through adapter dispatch", () => } finally { await db.delete(plugins).where(eq(plugins.id, storage.pluginId)); } }); + it("keeps inherited repository policy stable through binding and retains the provider on policy changes", async () => { + const f = await seed(); + const storage = await createPluginWorkspaceFixture(db, f.companyId); + const [plugin] = await db.select().from(plugins).where(eq(plugins.id, storage.pluginId)); + await db.update(plugins).set({ manifestJson: { ...plugin.manifestJson, workspaceProvider: { + ...plugin.manifestJson.workspaceProvider!, defaultForProjects: { mode: "isolated_workspace", strategy: "git_worktree" }, + } } }).where(eq(plugins.id, storage.pluginId)); + await db.update(projects).set({ executionWorkspacePolicy: null }).where(eq(projects.id, storage.input.projectId)); + await db.update(issues).set({ projectId: storage.input.projectId, projectWorkspaceId: storage.input.projectWorkspaceId, + executionWorkspaceSettings: { workspaceStrategy: { type: "git_worktree", branchTemplate: "work" } }, + }).where(eq(issues.id, f.issueId)); + await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: true, enableIsolatedWorkspacesByDefault: true }); + const cwd = await mkdtemp(join(tmpdir(), "session-provider-fixture-")); + const createOrchestrator = environmentOrchestrator.environmentRunOrchestrator; + const realization = vi.spyOn(environmentOrchestrator, "environmentRunOrchestrator").mockImplementation((...args) => ({ + ...createOrchestrator(...args), + realizeForRun: async input => { + input.executionWorkspace.cwd = cwd; + return { lease: input.lease, persistedExecutionWorkspace: input.persistedExecutionWorkspace, + workspaceRealization: {}, executionTarget: null, remoteExecution: null }; + }, + })); + const originalHeartbeat = heartbeat; + heartbeat = heartbeatService(db, { pluginWorkerManager: storage.workers as PluginWorkerManager }); + try { + const first = await execute(f); + const [bound] = await db.select().from(issues).where(eq(issues.id, f.issueId)); + expect(bound.executionWorkspaceId).toEqual(expect.any(String)); + const [workspace] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, bound.executionWorkspaceId!)); + expect([...captured.values()].filter(input => input.context.issueId === f.issueId)).toHaveLength(1); + expect(workspace).toMatchObject({ providerType: "plugin", strategyType: "git_worktree" }); + for (let turn = 0; turn < 2; turn++) { + const next = await execute(f); + expect(next.input.runtime.sessionId).toBe("saved-session"); + expect(next.input.config.workspaceStrategy).toEqual(first.input.config.workspaceStrategy); + expect(next.finished.resultJson).toMatchObject({ configFreshness: { + session: { reset: false, taskSessionReused: true, changedCategories: [] }, + workspace: { action: "reuse", workspaceReused: true, changedCategories: [], activeWorkspaceId: bound.executionWorkspaceId }, + } }); + } + await db.update(projects).set({ executionWorkspacePolicy: { enabled: true, allowIssueOverride: false } }).where(eq(projects.id, storage.input.projectId)); + const changed = await execute(f); + expect(changed.input.runtime.sessionId).toBeNull(); + expect(changed.finished.resultJson).toMatchObject({ configFreshness: { session: { reset: true, changedCategories: ["workspaceConfig"] } } }); + const [retained] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, bound.executionWorkspaceId!)); + expect(retained.metadata?.workspaceProviderOwner).toBe(storage.pluginId); + } finally { + await heartbeat.drainActiveRunExecutions(); + heartbeat = originalHeartbeat; + realization.mockRestore(); + await rm(cwd, { recursive: true, force: true }); + await db.delete(plugins).where(eq(plugins.id, storage.pluginId)); + await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: false, enableIsolatedWorkspacesByDefault: false }); + } + }); + it("starts fresh when effective model configuration changes and persists the reason", async () => { const f = await seed(); await execute(f); diff --git a/server/src/index.ts b/server/src/index.ts index 409a95e7a45..37661c37c0e 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,4 +1,5 @@ import { registerDecisionArchiveDeliveryWork } from "./services/decision-archive-delivery-work.js"; + import { registerToolReviewProjectionWork } from "./services/tool-review-projection-work.js"; import { registerToolReviewInterruptionWork } from "./services/tool-review-interruption-work.js"; import { registerToolReviewRecoveryWork } from "./services/tool-review-recovery-work.js"; diff --git a/server/src/services/delivery-work-notifications.ts b/server/src/services/delivery-work-notifications.ts index 07c82bd8963..230be70dcf8 100644 --- a/server/src/services/delivery-work-notifications.ts +++ b/server/src/services/delivery-work-notifications.ts @@ -10,6 +10,7 @@ export const DELIVERY_QUEUES = { executionDisposition: "execution-disposition", environmentCleanup: "environment-cleanup", decisionArchiveDelivery: "decision-archive-delivery", + toolReviewProjection: "tool-review-projection", toolReviewInterruption: "tool-review-interruption", toolReviewRecovery: "tool-review-recovery", diff --git a/server/src/services/heartbeat.ts b/server/src/services/heartbeat.ts index 59d2bd04a61..82f12b71f10 100644 --- a/server/src/services/heartbeat.ts +++ b/server/src/services/heartbeat.ts @@ -2380,7 +2380,7 @@ export function heartbeatService( !boundSourceWorkspace && !taskWorkspaceIntentRow?.intent && !issueContext?.projectWorkspaceId ? taskProjectContext : null ); - const parsedProjectExecutionWorkspacePolicy = workspacePolicyContext?.hasWorkspace && !boundSourceWorkspace + const parsedProjectExecutionWorkspacePolicy = workspacePolicyContext?.hasWorkspace ? await effectiveProjectWorkspacePolicy(db, agent.companyId, workspacePolicyContext.executionWorkspacePolicy, workspacePolicyContext.hasGitSource) : parseProjectExecutionWorkspacePolicy(workspacePolicyContext?.executionWorkspacePolicy); const projectExecutionWorkspacePolicy = diff --git a/server/src/services/native-runtime/native-completion-feedback.ts b/server/src/services/native-runtime/native-completion-feedback.ts index 9bb94b39c72..668af156fc3 100644 --- a/server/src/services/native-runtime/native-completion-feedback.ts +++ b/server/src/services/native-runtime/native-completion-feedback.ts @@ -1,4 +1,5 @@ import { pendingTaskRepository } from "../task-workspace-tools.js"; +import { onboardingFirstTaskCompletionAllowed } from "../onboarding-first-task-admission.js"; import { eligibleIssueMonitorWait } from "../issue-monitors.js"; import { assertNativeCaseHandoffResult } from "./native-case-handoff.js"; import { activeIssueInteractionCondition, ordinaryQuestionCondition } from "../issue-question-context.js"; @@ -140,6 +141,11 @@ export async function nativeCompletionFeedback( if (issue.executionRunId && issue.executionRunId !== runId) { return "Report accepted; a newer run owns the task. Do not claim this report changed its status."; } + if (result.reportedWorkDisposition === "done" && !await onboardingFirstTaskCompletionAllowed(db, { + companyId: run.companyId, issueId: issue.id, runId, + })) { + throw new Error("First-task proposal requires a persisted human decision before completing onboarding. The opening answer supplies scope, not approval. Present the proposal and keep this task in_review until the human accepts or rejects it; acknowledge a rejection without executing that work."); + } const continuation = run.contextSnapshot?.executionContinuation as { objective?: unknown } | undefined; const objective = typeof continuation?.objective === "string" ? continuation.objective : [issue.title, issue.description].filter(Boolean).join("\n"); diff --git a/server/src/services/native-runtime/native-run-finalizer.ts b/server/src/services/native-runtime/native-run-finalizer.ts index 4ade80add27..88efc195b86 100644 --- a/server/src/services/native-runtime/native-run-finalizer.ts +++ b/server/src/services/native-runtime/native-run-finalizer.ts @@ -1,4 +1,5 @@ import { pendingNativeGovernance } from "./native-governance.js"; +import { onboardingFirstTaskCompletionAllowed } from "../onboarding-first-task-admission.js"; import { notifyExecutionRunWork } from "../execution-control-notifications.js"; import { reconcileMuseNativeTerminalRun } from "./muse-terminal-reconciliation.js"; import { assertNativeCaseHandoffResult } from "./native-case-handoff.js"; @@ -1429,7 +1430,7 @@ export async function finalizeNativeRun(input: { authoritativeIssue.executionRunId !== run.id && authoritativeIssue.checkoutRunId !== run.id ? await readNativeMonitorDelivery(input.db, { companyId: run.companyId, issueId: coordinator.issueId, runId: run.id }) : null; - const proposedDecision = resolveNativeFinalizerStatus({ + let proposedDecision = resolveNativeFinalizerStatus({ caseHandoffAuthorized: Boolean(await assertNativeCaseHandoffResult(input.db, run.id, authoritativeIssue.id, result)), monitorWaitDispatched: monitorDelivery !== null, monitorWaitPreserved: preservedMonitorWait !== null, @@ -1471,6 +1472,14 @@ export async function finalizeNativeRun(input: { agentId: run.agentId, priorIssueStatus: authoritativeStatus(authoritativeIssue.status), }); + // Already accepted results still require the server-owned first-task decision. + // Keep the task open without inventing an interaction or rerunning the work. + if (proposedDecision.statusAction === "done" && !await onboardingFirstTaskCompletionAllowed(input.db, { + companyId: run.companyId, issueId: authoritativeIssue.id, runId: run.id, + })) { + proposedDecision = { ...proposedDecision, statusAction: "in_review", toStatus: "in_review", + reasonCode: "onboarding_first_task_decision_required", unblockDescriptor: null, effects: [{ kind: "release_checkout" }] }; + } const decision = conversationNativeDecision({ conversation: isConversation(authoritativeIssue), terminalState, workspaceFinalizeStatus: input.workspaceFinalizeStatus, hasGovernanceGate: !!governanceGate, diff --git a/server/src/services/native-runtime/paperclip-control-plane-port.test.ts b/server/src/services/native-runtime/paperclip-control-plane-port.test.ts index f8b9eb1d022..4676979de87 100644 --- a/server/src/services/native-runtime/paperclip-control-plane-port.test.ts +++ b/server/src/services/native-runtime/paperclip-control-plane-port.test.ts @@ -2,6 +2,7 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { asc, eq, sql } from "drizzle-orm"; import { generateKeyPairSync, randomUUID } from "node:crypto"; import { renderPaperclipWakePrompt } from "@paperclipai/adapter-utils/server-utils"; +import { ONBOARDING_FIRST_TASK_ORIGIN_KIND } from "@paperclipai/shared"; import { readCompletedAssistantMessageCandidate, resolveHeartbeatRunResponse, selectHeartbeatRunFinalAgentMessage } from "../heartbeat-run-summary.js"; import { activityLog, @@ -46,11 +47,13 @@ import { NativeRunCoordinatorStore } from "./native-run-coordinator-store.js"; import { finalizeNativeRun, repairCommittedNativeChatResponse } from "./native-run-finalizer.js"; import { nativeRuntimeContextFixture } from "./runtime-context.test-fixture.js"; import { issueThreadInteractionService } from "../issue-thread-interactions.js"; +import { documentService } from "../documents.js"; import { claimNativeReviewExecutionLock, getNativeReviewAssignment } from "./native-review-participant.js"; import { claimQueuedNativeReviewRun } from "./native-review-dispatch.js"; import { stageNativeRunnerWakeAttachments } from "./native-runner-file-handoff.js"; import { PaperclipRunnerToolAuthority } from "./paperclip-runner-tool-authority.js"; import { nativeCompletionFeedback } from "./native-completion-feedback.js"; +import { buildOnboardingFirstTaskOpeningQuestion, ONBOARDING_FIRST_TASK_OPENING_QUESTION_ID } from "../onboarding-first-task-assets.js"; import { assertCurrentWakeCommentsRead } from "./current-wake-comments.js"; import { buildExecutionContinuation } from "../execution-continuation.js"; import { childReviewOutcomes } from "./child-review-outcomes.js"; @@ -1709,6 +1712,191 @@ describe("PaperclipControlPlanePort conformance", () => { }); + const allowedFirstTaskCompletionScenarios = [ + "ordinary task", "accepted proposal", "accepted conversational proposal", "accepted current plan", "acknowledged rejection", "acknowledged plan rejection", + ] as const; + const refusedFirstTaskCompletionScenarios = [ + "opening answer only", "agent-resolved without human comment", "quarantined human comment", "newest proposal pending", "stale plan acceptance", + ] as const; + type FirstTaskCompletionScenario = typeof allowedFirstTaskCompletionScenarios[number] + | typeof refusedFirstTaskCompletionScenarios[number]; + + async function prepareFirstTaskCompletion(scenario: FirstTaskCompletionScenario) { + const companyId = randomUUID(); + const agentId = randomUUID(); + const issueId = randomUUID(); + const runId = randomUUID(); + const localContractId = randomUUID(); + const runnerInstanceId = randomUUID(); + const userId = `first-task-board-${randomUUID()}`; + const identity = { ...CONTROL_PLANE_CONFORMANCE_OPEN.identity, companyId, agentId, issueId, runId }; + const onboarding = scenario !== "ordinary task"; + const rejected = scenario === "acknowledged rejection" || scenario === "acknowledged plan rejection"; + await db.insert(companies).values({ id: companyId, name: "First-task completion", issuePrefix: `F${companyId.slice(0, 6).toUpperCase()}` }); + await db.insert(authUsers).values({ id: userId, name: "First-task board", email: `${userId}@example.test`, createdAt: new Date(), updatedAt: new Date() }); + await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: userId, status: "active", membershipRole: "member" }); + await db.insert(agents).values({ id: agentId, companyId, name: "First-task agent", adapterType: "codex_local", status: "running" }); + await db.insert(issues).values({ id: issueId, companyId, title: onboarding ? "Paperclip onboarding" : "Ordinary low-risk task", + description: onboarding ? "/first-task\nSingle-task proposal mode: confirmation" : "Complete the requested ordinary task. Mentioning /first-task does not grant the server-owned onboarding origin.", + originKind: onboarding ? ONBOARDING_FIRST_TASK_ORIGIN_KIND : "manual", status: "in_progress", assigneeAgentId: agentId, + responsibleUserId: userId, workMode: "standard" }); + await db.insert(completionContracts).values({ id: localContractId, companyId, issueId, revision: 1, + schemaVersion: "paperclip.completion-contract.v1", policyVersion: "phase6-v3", risk: "low", completionAuthority: "agent_claim_policy", + incompleteCriteriaPolicy: "preserve_non_terminal", contractJson: { revision: "first-task-native-finish-v1", + objective: onboarding ? "Understand the user request, propose one task, and honor their decision" : "Complete the requested ordinary task", + criteria: [{ id: "objective", requirement: "Complete the requested workflow" }] }, + canonicalSha256: localContractId, createdByActorType: "system", createdByActorId: "test" }); + await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, responsibleUserId: userId, status: "succeeded", runtimeMode: "native", + nativeIssueId: issueId, nativeSessionId: identity.sessionId, runnerInstanceId, completionContractId: localContractId, + completionContractSha256: localContractId, contextSnapshot: { issueId, + // A claimed acceptance copied into continuation context is not the + // persisted human decision; the opening-answer cases must still refuse it. + ...(scenario === "opening answer only" ? { executionContinuation: { humanResponses: [{ id: randomUUID(), + kind: "request_confirmation", status: "accepted", resolvedByUserId: userId, result: { version: 1, outcome: "accepted" } }] } } : {}), + } }); + await db.update(issues).set({ executionRunId: runId }).where(eq(issues.id, issueId)); + if (onboarding) { + await db.insert(issueThreadInteractions).values({ companyId, issueId, kind: "ask_user_questions", status: "answered", + createdByAgentId: agentId, payload: await buildOnboardingFirstTaskOpeningQuestion(), + result: { version: 1, answers: [{ questionId: ONBOARDING_FIRST_TASK_OPENING_QUESTION_ID, optionIds: ["task"], otherText: "Help organize my next task." }] }, + resolvedByUserId: userId, resolvedAt: new Date() }); + } + if (onboarding && scenario !== "opening answer only") { + const proposalAt = new Date(Date.now() - 2_000); + const plan = scenario === "accepted current plan" || scenario === "stale plan acceptance" || scenario === "acknowledged plan rejection" + ? (await documentService(db).upsertIssueDocument({ issueId, key: "plan", title: "First task plan", format: "markdown", + body: "Retain this plan for the user's next task.", createdByAgentId: agentId, createdByRunId: runId })).document + : null; + const conversational = scenario === "accepted conversational proposal" || scenario === "quarantined human comment"; + const comment = conversational ? (await db.insert(issueComments).values({ companyId, issueId, + authorUserId: userId, authorType: "user", body: "Yes, proceed with that proposal.", + createdAt: new Date(proposalAt.getTime() + 1_000), + ...(scenario === "quarantined human comment" + ? { sourceTrust: { preset: "low_trust_review" as const, disposition: "quarantined" as const } } : {}), + }).returning())[0]! : null; + const agentResolved = conversational || scenario === "agent-resolved without human comment"; + const outcome = rejected ? "rejected" as const : "accepted" as const; + // Persisted agent interpretations need a real, trusted human reply. The + // missing/quarantined variants simulate an untrusted stored result. + await db.insert(issueThreadInteractions).values({ companyId, issueId, + kind: plan ? "request_checkbox_confirmation" : "request_confirmation", status: outcome, + createdByAgentId: agentId, sourceRunId: runId, createdAt: proposalAt, + payload: plan ? { version: 1, prompt: "May I retain this plan?", + options: [{ id: "retain-plan", label: "Retain the plan" }], defaultSelectedOptionIds: ["retain-plan"], minSelected: 1, + target: { type: "issue_document", key: "plan", issueId, documentId: plan.id, revisionId: plan.latestRevisionId! }, + } : { version: 1, prompt: "May I create one task to organize your agenda?" }, + result: { version: 1, outcome, ...(plan && outcome === "accepted" ? { selectedOptionIds: ["retain-plan"] } : {}), + ...(comment ? { commentId: comment.id } : {}), + ...(outcome === "rejected" ? { reason: "Do not do this work." } : {}) }, + resolvedByUserId: agentResolved ? null : userId, + resolvedByAgentId: agentResolved ? agentId : null, resolvedByRunId: agentResolved ? runId : null, + resolvedAt: new Date(), + }); + if (scenario === "newest proposal pending") { + await db.insert(issueThreadInteractions).values({ companyId, issueId, + kind: "request_checkbox_confirmation", status: "pending", createdByAgentId: agentId, sourceRunId: runId, + createdAt: new Date(proposalAt.getTime() + 1_000), + payload: { version: 1, prompt: "Accept the revised task scope?", options: [{ id: "revised", label: "Use revised scope" }], + defaultSelectedOptionIds: ["revised"], minSelected: 1 }, + }); + } + if (scenario === "stale plan acceptance" || scenario === "acknowledged plan rejection") { + await documentService(db).upsertIssueDocument({ issueId, key: "plan", format: "markdown", + baseRevisionId: plan!.latestRevisionId, body: "A revised scope that has no persisted human decision.", + createdByAgentId: agentId, createdByRunId: runId }); + } + } + const port = new PaperclipControlPlanePort(db, { companyId, issueId, runId, agentId, sessionId: identity.sessionId, + completionContractId: localContractId, completionContractSha256: localContractId, + sourceInstanceId: runnerInstanceId, controlPlaneSourceInstanceId: "first-task-native-finish-control" }); + await port.openRun({ identity, backendKind: "mock", sourceInstanceId: runnerInstanceId }); + const result: PrpStructuredRunResult = { ...structuredClone(CONTROL_PLANE_CONFORMANCE_RESULT), + summary: rejected + ? "You declined the proposal. I will stop here and have not created or performed the proposed task." + : "The requested workflow is complete.", + completionClaim: { contractRevision: "first-task-native-finish-v1", objectiveSatisfied: true, + criteria: [{ criterionId: "objective", status: "satisfied", evidenceRefs: [] }], remainingWork: [] }, + evidence: [], verification: [{ commandOrCheck: "Check the current task state", status: "passed" }], attentionRequests: [] }; + return { companyId, agentId, issueId, runId, port, result }; + } + + it.each(refusedFirstTaskCompletionScenarios)( + "refuses native completion feedback without a trusted current first-task decision (%s)", async (scenario) => { + const { issueId, runId, result } = await prepareFirstTaskCompletion(scenario); + await expect(nativeCompletionFeedback(db, runId, result)) + .rejects.toThrow("First-task proposal requires a persisted human decision"); + expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0]!.status).toBe("in_progress"); + expect(await db.select().from(statusDecisions).where(eq(statusDecisions.issueId, issueId))).toEqual([]); + expect(await db.select().from(nativeRunResults).where(eq(nativeRunResults.runId, runId))).toEqual([]); + }); + + it.each([...allowedFirstTaskCompletionScenarios, ...refusedFirstTaskCompletionScenarios])( + "honors the first-task human decision during native completion (%s)", async (scenario) => { + const { issueId, runId, port, result } = await prepareFirstTaskCompletion(scenario); + const allowed = allowedFirstTaskCompletionScenarios.some(candidate => candidate === scenario); + if (allowed) await expect(nativeCompletionFeedback(db, runId, result)).resolves.toContain("Completion report accepted"); + // Invalid results bypass only early feedback to exercise the backstop for a + // report already committed by a disconnected or older controller. + await port.completeRun({ result, terminal: CONTROL_PLANE_CONFORMANCE_TERMINAL, callerResultId: "first-task-native-finish-result" }); + await finalizeNativeRun({ db, runId, workspaceFinalizeStatus: "succeeded", projectRunStatus: true }); + + const [completedIssue] = await db.select().from(issues).where(eq(issues.id, issueId)); + const decisions = await db.select().from(statusDecisions).where(eq(statusDecisions.issueId, issueId)); + if (!allowed) { + const expectedReason = scenario === "newest proposal pending" ? "governed_gate_pending" : "onboarding_first_task_decision_required"; + expect(completedIssue).toMatchObject({ status: "in_review" }); + expect(decisions).toContainEqual(expect.objectContaining({ toStatus: "in_review", reasonCode: expectedReason })); + expect(decisions).not.toContainEqual(expect.objectContaining({ toStatus: "done" })); + expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)))[0]!.resultJson) + .toMatchObject({ finalizationReasonCode: expectedReason }); + } else { + expect(completedIssue).toMatchObject({ status: "done" }); + expect(decisions).toContainEqual(expect.objectContaining({ toStatus: "done", reasonCode: "completion_claim_policy_accepted" })); + } + }); + + it("rechecks the first-task human decision atomically before native status commit", async () => { + const { companyId, agentId, issueId, runId, port, result } = await prepareFirstTaskCompletion("accepted proposal"); + await expect(nativeCompletionFeedback(db, runId, result)).resolves.toContain("Completion report accepted"); + await port.completeRun({ result, terminal: CONTROL_PLANE_CONFORMANCE_TERMINAL, callerResultId: "first-task-race-result" }); + expect(await db.select().from(workAssessments).where(eq(workAssessments.runId, runId))).toEqual([]); + + const interactionId = randomUUID(); + const trigger = `first_task_race_${randomUUID().replace(/-/g, "")}`; + // Assessment persistence follows the policy read and precedes status commit. + // Insert a newer proposal at that real database boundary, exactly once. + await db.execute(sql.raw(`create function ${trigger}() returns trigger language plpgsql as $$ + begin + if NEW.issue_id = '${issueId}'::uuid and NEW.run_id = '${runId}'::uuid then + insert into issue_thread_interactions + (id, company_id, issue_id, kind, status, created_by_agent_id, source_run_id, payload, created_at) + values + ('${interactionId}'::uuid, '${companyId}'::uuid, '${issueId}'::uuid, + 'request_confirmation', 'pending', '${agentId}'::uuid, '${runId}'::uuid, + '{"version":1,"prompt":"Accept the revised task scope?"}'::jsonb, clock_timestamp()) + on conflict (id) do nothing; + end if; + return NEW; + end $$`)); + try { + await db.execute(sql.raw(`create trigger ${trigger} before insert on work_assessments + for each row execute function ${trigger}()`)); + await finalizeNativeRun({ db, runId, workspaceFinalizeStatus: "succeeded", projectRunStatus: true }); + + expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, interactionId)))[0]) + .toMatchObject({ status: "pending", issueId }); + expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0]!.status).toBe("in_review"); + const decisions = await db.select().from(statusDecisions).where(eq(statusDecisions.issueId, issueId)); + expect(decisions).toEqual([expect.objectContaining({ toStatus: "in_review", reasonCode: "governed_gate_pending" })]); + expect(decisions).not.toContainEqual(expect.objectContaining({ toStatus: "done" })); + expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId)))[0]) + .toMatchObject({ nativePhase: "committed", resultJson: { finalizationReasonCode: "governed_gate_pending" } }); + } finally { + await db.execute(sql.raw(`drop trigger if exists ${trigger} on work_assessments`)); + await db.execute(sql.raw(`drop function if exists ${trigger}()`)); + } + }); + it.each(["none", "queued", "claimed", "deferred_issue_execution", "coalesced", "current_run", "current_intent"])( "preserves completion caveats and pending child delivery (%s)", async (delivery) => { const identity = CONTROL_PLANE_CONFORMANCE_OPEN.identity; diff --git a/server/src/services/native-runtime/status-decision-committer.ts b/server/src/services/native-runtime/status-decision-committer.ts index 0e34904c5b3..f19bb8cc244 100644 --- a/server/src/services/native-runtime/status-decision-committer.ts +++ b/server/src/services/native-runtime/status-decision-committer.ts @@ -1,4 +1,5 @@ import { scheduledIssueMonitorWait } from "../issue-monitors.js"; +import { onboardingFirstTaskCompletionAllowed } from "../onboarding-first-task-admission.js"; import { hasPublishedGitHubReview, readInheritedGitHubReviewWait } from "../chat-channels/github/review-wait.js"; import { pendingNativeGovernance } from "./native-governance.js"; import { findSupersededExternalReviewRequests, supersedeExternalReviewRequestsInTransaction } from "./superseded-external-review-requests.js"; @@ -1657,6 +1658,11 @@ export async function commitNativeStatusDecision(input: { ) { throw new NativeStatusRaceError(); } + // Recheck under the issue lock, retaining run/proposal/document proof until + // commit. A newer decision cannot race an earlier first-task Done assessment. + if (input.decision.statusAction === "done" && !await onboardingFirstTaskCompletionAllowed(tx as unknown as Db, { + companyId: input.companyId, issueId: input.issueId, runId: input.runId, + })) throw new NativeStatusRaceError(); if (reasonCode === "scheduled_monitor_waiting") { const expected = input.requireMonitorWait; const inReview = input.decision.statusAction === "in_review" && input.decision.toStatus === "in_review"; diff --git a/server/src/services/onboarding-first-task-admission.ts b/server/src/services/onboarding-first-task-admission.ts index e649120bb6b..00182df9e1e 100644 --- a/server/src/services/onboarding-first-task-admission.ts +++ b/server/src/services/onboarding-first-task-admission.ts @@ -59,72 +59,107 @@ export async function assertOnboardingFirstTaskCreationAllowed(tx: Db, input: { )).for("share"); if (!run || runIssueId(run) !== source.id) throw acceptanceRequired(); + if (await readOnboardingFirstTaskDecision(tx, { companyId: input.companyId, issueId: source.id, outcomes: ["accepted"] }) !== "accepted") throw acceptanceRequired(); +} + +/** Shared persisted-decision proof; creation and completion retain different authority. */ +async function readOnboardingFirstTaskDecision(tx: Db, input: { + companyId: string; issueId: string; outcomes: Array<"accepted" | "rejected">; +}): Promise<"accepted" | "rejected" | null> { // Select the newest proposal before checking its outcome: a later pending or // rejected proposal must never fall back to an older accepted scope. Governed // tool/credential actions and custom completion reviews are not proposals. const [proposal] = await tx.select().from(issueThreadInteractions).where(and( - eq(issueThreadInteractions.companyId, input.companyId), eq(issueThreadInteractions.issueId, source.id), + eq(issueThreadInteractions.companyId, input.companyId), eq(issueThreadInteractions.issueId, input.issueId), inArray(issueThreadInteractions.kind, ["request_confirmation", "request_checkbox_confirmation"]), sql`NOT (${issueThreadInteractions.payload} ?| ARRAY['toolAction', 'secretProposal', 'connectionAuthorization'])`, sql`COALESCE(${issueThreadInteractions.payload}->'target'->>'type', '') <> 'custom'`, )).orderBy(desc(issueThreadInteractions.createdAt), desc(issueThreadInteractions.id)).limit(1).for("share"); - if (!proposal || proposal.status !== "accepted" || !proposal.resolvedAt) throw acceptanceRequired(); + if (!proposal || (proposal.status !== "accepted" && proposal.status !== "rejected") || + !input.outcomes.includes(proposal.status) || !proposal.resolvedAt) return null; const checkbox = proposal.kind === "request_checkbox_confirmation"; const payload = checkbox ? requestCheckboxConfirmationPayloadSchema.safeParse(proposal.payload) : requestConfirmationPayloadSchema.safeParse(proposal.payload); const result = checkbox ? requestCheckboxConfirmationResultSchema.safeParse(proposal.result) : requestConfirmationResultSchema.safeParse(proposal.result); - if (!payload.success || !result.success || result.data.outcome !== "accepted") throw acceptanceRequired(); - if (checkbox) { + if (!payload.success || !result.success || result.data.outcome !== proposal.status) return null; + if (checkbox && proposal.status === "accepted") { const selected = object(result.data).selectedOptionIds; - if (!("options" in payload.data) || !Array.isArray(selected) || selected.length === 0) throw acceptanceRequired(); + if (!("options" in payload.data) || !Array.isArray(selected) || selected.length === 0) return null; const allowed = new Set(payload.data.options.map(option => option.id)); if (selected.some(id => typeof id !== "string" || !allowed.has(id)) || selected.length < payload.data.minSelected - || (payload.data.maxSelected != null && selected.length > payload.data.maxSelected)) throw acceptanceRequired(); + || (payload.data.maxSelected != null && selected.length > payload.data.maxSelected)) return null; } if (proposal.sourceRunId) { const [origin] = await tx.select(runOriginFields).from(heartbeatRuns).where(and( eq(heartbeatRuns.id, proposal.sourceRunId), eq(heartbeatRuns.companyId, input.companyId), )).for("share"); - if (!origin || runIssueId(origin) !== source.id || origin.agentId !== proposal.createdByAgentId) throw acceptanceRequired(); + if (!origin || runIssueId(origin) !== input.issueId || origin.agentId !== proposal.createdByAgentId) return null; } else if (proposal.createdByAgentId) { const [creator] = await tx.select({ companyId: agents.companyId }).from(agents).where(eq(agents.id, proposal.createdByAgentId)); - if (creator?.companyId !== input.companyId) throw acceptanceRequired(); - } else if (!proposal.createdByUserId) throw acceptanceRequired(); + if (creator?.companyId !== input.companyId) return null; + } else if (!proposal.createdByUserId) return null; const directHumanDecision = Boolean(proposal.resolvedByUserId && !proposal.resolvedByAgentId && !proposal.resolvedByRunId); if (!directHumanDecision) { const commentId = result.data.commentId; - if (!commentId || !proposal.resolvedByAgentId || !proposal.resolvedByRunId) throw acceptanceRequired(); + if (!commentId || !proposal.resolvedByAgentId || !proposal.resolvedByRunId) return null; const [comment] = await tx.select().from(issueComments).where(and( - eq(issueComments.id, commentId), eq(issueComments.companyId, input.companyId), eq(issueComments.issueId, source.id), + eq(issueComments.id, commentId), eq(issueComments.companyId, input.companyId), eq(issueComments.issueId, input.issueId), )).for("share"); if (!comment || comment.deletedAt || comment.authorType !== "user" || !comment.authorUserId || comment.authorAgentId || comment.createdByRunId || comment.derivedAuthorAgentId || comment.derivedCreatedByRunId || comment.sourceTrust?.disposition === "quarantined" || comment.createdAt < proposal.createdAt - || comment.body.trim() === "/new") throw acceptanceRequired(); + || comment.body.trim() === "/new") return null; const [resolver] = await tx.select(runOriginFields).from(heartbeatRuns).where(and( eq(heartbeatRuns.id, proposal.resolvedByRunId), eq(heartbeatRuns.companyId, input.companyId), eq(heartbeatRuns.agentId, proposal.resolvedByAgentId), )).for("share"); - if (!resolver || runIssueId(resolver) !== source.id - || (resolver.responsibleUserId && resolver.responsibleUserId !== comment.authorUserId)) throw acceptanceRequired(); + if (!resolver || runIssueId(resolver) !== input.issueId + || (resolver.responsibleUserId && resolver.responsibleUserId !== comment.authorUserId)) return null; } + // Refusal permits acknowledgement and closure, never execution. An agent-only + // plan edit cannot turn that human refusal into permission to execute. + if (proposal.status === "rejected") return "rejected"; + const target = payload.data.target; const [plan] = await tx.select({ documentId: documents.id, revisionId: documents.latestRevisionId }) .from(issueDocuments).innerJoin(documents, eq(documents.id, issueDocuments.documentId)).where(and( eq(issueDocuments.companyId, input.companyId), eq(documents.companyId, input.companyId), - eq(issueDocuments.issueId, source.id), eq(issueDocuments.key, "plan"), + eq(issueDocuments.issueId, input.issueId), eq(issueDocuments.key, "plan"), )).for("share", { of: documents }); // Document writers do not all lock the issue, so hold the document row too. // A saved plan must be accepted by revision; an untargeted card cannot stand // in for approval of a different or subsequently edited document. if (plan || target) { if (!target || target.type !== "issue_document" || target.key !== "plan" - || (target.issueId && target.issueId !== source.id) || !plan || plan.revisionId !== target.revisionId - || (target.documentId && plan.documentId !== target.documentId)) throw acceptanceRequired(); + || (target.issueId && target.issueId !== input.issueId) || !plan || plan.revisionId !== target.revisionId + || (target.documentId && plan.documentId !== target.documentId)) return null; } + return "accepted"; +} + +/** Completion may acknowledge a trusted refusal; it never authorizes rejected work. */ +export async function onboardingFirstTaskCompletionAllowed(tx: Db, input: { + companyId: string; issueId: string; runId: string; +}): Promise { + const [source] = await tx.select({ id: issues.id, originKind: issues.originKind }).from(issues).where(and( + eq(issues.id, input.issueId), eq(issues.companyId, input.companyId), + )); + if (!source) return false; + if (source.originKind !== ONBOARDING_FIRST_TASK_ORIGIN_KIND) return true; + // Only the first-task path takes additional authority locks. At status commit + // these are held until the decision is written, in issue/run/proposal order. + const [lockedSource] = await tx.select({ id: issues.id }).from(issues).where(and( + eq(issues.id, source.id), eq(issues.companyId, input.companyId), eq(issues.originKind, ONBOARDING_FIRST_TASK_ORIGIN_KIND), + )).for("update"); + if (!lockedSource) return false; + const [run] = await tx.select({ ...runOriginFields, runtimeMode: heartbeatRuns.runtimeMode }).from(heartbeatRuns).where(and( + eq(heartbeatRuns.id, input.runId), eq(heartbeatRuns.companyId, input.companyId), + )).for("share"); + if (!run || run.runtimeMode !== "native" || run.nativeIssueId !== source.id) return false; + return await readOnboardingFirstTaskDecision(tx, { companyId: input.companyId, issueId: source.id, outcomes: ["accepted", "rejected"] }) !== null; } diff --git a/tests/runner-e2e/OPERATIONS.md b/tests/runner-e2e/OPERATIONS.md index 1481b4451b1..be61693fb07 100644 --- a/tests/runner-e2e/OPERATIONS.md +++ b/tests/runner-e2e/OPERATIONS.md @@ -909,3 +909,42 @@ reservations and model-readiness status separately. It does not release any historical hold or certify a full-lifecycle vendor cost bound. Missing model receipts, conflicts, active requests, excess commitments, and the existing product, cleanup, feedback and trace checks still fail qualification. + + +### Explicit bounded Daytona gateway qualification + +A manual manifest can set `remoteGateway: "cloudflare"` for the native Codex +or Claude marker/feedback Daytona cells. Install `cloudflared` on the controller +PATH, or set `PAPERCLIP_EVAL_CLOUDFLARED` to its executable path. Empty overrides +use the PATH default. The parent starts its existing model +gateway and native OTLP relay, then exposes only their authenticated endpoints +through one owned HTTPS quick tunnel. It verifies a fresh health nonce before +fixture setup, waits up to 120 seconds for registration and 120 seconds for +public readiness, and does not retry tunnel creation or provider work. + +Only attempt-scoped tokens leave the controller. Provider and Braintrust keys +stay in the parent; the fixture/server retains the existing Daytona provisioning +credential separately. The model gateway still enforces its campaign allowance, +request count, concurrency, deadline and receipt journal. The bridge streams +responses and preserves terminal-usage draining after a client disconnect. +The tunnel receives no ambient provider credentials or Cloudflare config. Its +exposure closes at the campaign deadline or four hours after startup, whichever +comes first, and on attempt cleanup. Owned gateway, +relay and tunnel cleanup failures remain failures. A configured remote route +is not live acceptance; retain the actual Daytona result, cleanup, feedback, +native traces and complete model receipts. + +The Daytona image runs Linux/x64. ARM or macOS controllers use the image's +verified daemon by default; only an explicit cross-compiled remote override +replaces it. + +Daily and weekly selections remain local. This explicit remote option adds no +scheduled coverage or public report publication. + +The worker generates the attempt's trace parent. The relay validates the project +in both the header and payload, but does not pin that worker-generated parent. +Treat the attempt OTLP token as authority to submit trace attribution within +this eval project. Keep it limited to the trusted harness/provider runtime; +never expose it to test prompts or unrelated clients. The project check is not +an attempt-isolation guarantee. Indexing verification is a separate retained +or scheduled check; the manual launcher alone does not perform it. diff --git a/tests/runner-e2e/PRODUCT-JOURNEYS.md b/tests/runner-e2e/PRODUCT-JOURNEYS.md index 2d23dc214e1..a86949df8e7 100644 --- a/tests/runner-e2e/PRODUCT-JOURNEYS.md +++ b/tests/runner-e2e/PRODUCT-JOURNEYS.md @@ -118,7 +118,8 @@ ownership manifests. An expired lease permits cleanup recovery, not reuse. Managed self-hosted instances are removed after every attempt. Existing Cloud stacks retain the QA account and declared baseline while removing verified run-owned fixtures. Successful disposable stacks are deleted. Failed disposable -stacks are stopped and retained for 24 hours, then swept. Interrupted cleanup +stacks are stopped and retained for up to 24 hours. The deployment provider +declares earlier cleanup eligibility to leave time for its sweeper. Interrupted cleanup remains visible and retains exclusive stack ownership. QA identity and inbox never belong to disposable ownership. diff --git a/tests/runner-e2e/README.md b/tests/runner-e2e/README.md index 554b10b32d1..af2ba3758b4 100644 --- a/tests/runner-e2e/README.md +++ b/tests/runner-e2e/README.md @@ -261,6 +261,16 @@ status, known adapters, and allowlisted outcome/check/server codes. The case report renders these fields explicitly; older results without the receipt remain valid. This diagnostic does not change the journey grade or qualify a live run. +When the canonical first-task journey fails `onboarding_work_before_acceptance`, +`productJourney.onboardingPreAcceptance` retains the failed checkpoint and every +observed guard violation: unexpected task set, existing attachment, non-planning +document, or first task already Done. The closed version-1 receipt contains only +counts, whether the first task matches the canonical parent, and closed first-task +and canonical-parent statuses. Reports validate and display these observations; +they do not infer a product cause. Only the failing boundary is retained, without +task/document IDs, document keys, text, or bodies. Historical results may omit the +receipt. The original error, failed grade, and dependent blocked outcomes remain. + ## Live provider connection journeys See [PROVIDER-CONNECTIONS.md](PROVIDER-CONNECTIONS.md) for the explicit-only diff --git a/tests/runner-e2e/SECURITY.md b/tests/runner-e2e/SECURITY.md index 30e2f2bc1a9..c3dde9aef93 100644 --- a/tests/runner-e2e/SECURITY.md +++ b/tests/runner-e2e/SECURITY.md @@ -28,6 +28,16 @@ Unknown codes become `unrecognized`; raw bodies, messages, URLs, headers, and browser errors are excluded. Reports validate the receipt before rendering its explicit fields, and historical results may omit it. +The optional `onboardingPreAcceptance` receipt captures only a failing first-task +acceptance guard. Its closed version-1 fields are a known checkpoint, up to four +distinct guard-violation codes, nonnegative integer task/attachment/document +counts, a canonical-parent match boolean, and closed first-task/canonical-parent +statuses (`missing` and `unknown` remain explicit). It excludes all identifiers, +task text, document keys and bodies, filenames, URLs, credentials, and raw API +responses. Validation rejects extra or inconsistent fields before report use; +older results may omit the receipt. It preserves the original failure grade and +does not attribute a product cause or authorize acceptance. + ## Attended connection authentication See [PROVIDER-CONNECTIONS.md](PROVIDER-CONNECTIONS.md) for the explicit-only diff --git a/tests/runner-e2e/braintrust.test.ts b/tests/runner-e2e/braintrust.test.ts index 118621d3ffe..da60c202173 100644 --- a/tests/runner-e2e/braintrust.test.ts +++ b/tests/runner-e2e/braintrust.test.ts @@ -211,3 +211,13 @@ it("retains finalization export failures without changing the recorded product r }, transport)).toMatchObject({ status: "failed", errorCode: "invalid_finalization_context" }); expect(transport).not.toHaveBeenCalled(); }); + + +it("binds remote OTLP to the explicitly assigned eval gateway origin", () => { + const origin = "https://synthetic-eval.trycloudflare.com"; + const env = { PAPERCLIP_E2E_BRAINTRUST_PROJECT_ID: projectId, BRAINTRUST_API_KEY: "synthetic-token", + PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN: origin, PAPERCLIP_E2E_OTLP_RELAY_URL: `${origin}/otel` }; + expect(evalBraintrustConfig(env)).toMatchObject({ remoteOrigin: origin, endpointBaseUrl: `${origin}/otel` }); + expect(() => evalBraintrustConfig({ ...env, PAPERCLIP_E2E_OTLP_RELAY_URL: "https://different-eval.trycloudflare.com/otel" })).toThrow(); + expect(buildPaperclipServerEnvironment(env).PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN).toBeUndefined(); +}); diff --git a/tests/runner-e2e/braintrust.ts b/tests/runner-e2e/braintrust.ts index adb5d94e4b9..83b7c2091f5 100644 --- a/tests/runner-e2e/braintrust.ts +++ b/tests/runner-e2e/braintrust.ts @@ -1,13 +1,15 @@ import { randomBytes } from "node:crypto"; import { createRequire } from "node:module"; +import { isEvalRemoteOrigin } from "./eval-remote-gateway.js"; import type { RunnerApi } from "./api.js"; import type { FailureClass, RunnerE2EResult } from "./types.js"; export const EVAL_OBSERVABILITY_SECRET_NAMES = ["BRAINTRUST_API_KEY"] as const; const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; -export interface EvalBraintrustConfig { projectId: string; apiKey: string; endpointBaseUrl?: string } +export interface EvalBraintrustConfig { projectId: string; apiKey: string; endpointBaseUrl?: string; remoteOrigin?: string } function evalOtlpEndpoint(config: EvalBraintrustConfig) { - if (config.endpointBaseUrl !== undefined && !/^http:\/\/127\.0\.0\.1:[1-9][0-9]{0,4}\/otel$/.test(config.endpointBaseUrl)) { + if (config.remoteOrigin !== undefined && !isEvalRemoteOrigin(config.remoteOrigin)) throw new Error("Invalid eval remote OTLP origin"); + if (config.endpointBaseUrl !== undefined && !(config.remoteOrigin && config.endpointBaseUrl === `${config.remoteOrigin}/otel`) && !/^http:\/\/127\.0\.0\.1:[1-9][0-9]{0,4}\/otel$/.test(config.endpointBaseUrl)) { throw new Error("Eval OTLP relay must use the parent-owned loopback endpoint"); } if (config.endpointBaseUrl && Number(new URL(config.endpointBaseUrl).port) > 65535) throw new Error("Invalid eval OTLP relay port"); @@ -20,6 +22,7 @@ export function evalBraintrustConfig(env: NodeJS.ProcessEnv): EvalBraintrustConf throw new Error("Eval Braintrust export requires an explicit project UUID and API key"); } const config = { projectId, apiKey: env.BRAINTRUST_API_KEY.trim(), + ...(env.PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN ? { remoteOrigin: env.PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN } : {}), ...(env.PAPERCLIP_E2E_OTLP_RELAY_URL ? { endpointBaseUrl: env.PAPERCLIP_E2E_OTLP_RELAY_URL } : {}) }; evalOtlpEndpoint(config); return config; diff --git a/tests/runner-e2e/case-outcome.ts b/tests/runner-e2e/case-outcome.ts index 489f16f8384..ca463553322 100644 --- a/tests/runner-e2e/case-outcome.ts +++ b/tests/runner-e2e/case-outcome.ts @@ -1,6 +1,7 @@ import type { RunnerE2EResult } from "./types.js"; import { isBlockedConnectionResult } from "./connection-evidence.js"; import { validateOnboardingConnectEvidence } from "./product-journeys/onboarding-connect.js"; +import { validateOnboardingPreAcceptanceEvidence } from "./product-journeys/onboarding-pre-acceptance.js"; const html = (value: unknown) => String(value ?? "").replace( /[&<>"']/g, @@ -31,6 +32,20 @@ function renderOnboardingConnect(result: RunnerE2EResult): string { ${response.truncatedChecks ? "

Only the first 32 validation checks were retained.

" : ""}`; }).join("")}`; } +function renderOnboardingPreAcceptance(result: RunnerE2EResult): string { + const receipt = result.productJourney?.onboardingPreAcceptance; + if (receipt === undefined) return ""; + try { validateOnboardingPreAcceptanceEvidence(receipt); } + catch { return "

Onboarding pre-acceptance evidence unavailable: invalid receipt.

"; } + return `
Onboarding pre-acceptance evidence +

Checkpoint: ${html(receipt.checkpoint)}

+

Observed guard violations:

    ${receipt.violations.map(code => `
  • ${html(code)}
  • `).join("")}
+
${[ + ["Tasks", receipt.taskCount], ["Attachments", receipt.attachmentCount], ["Documents", receipt.documentCount], + ["Non-planning documents", receipt.nonPlanningDocumentCount], ["First task matches canonical parent", receipt.firstTaskMatchesCanonicalParent], + ["First task status", receipt.firstTaskStatus], ["Canonical parent status", receipt.canonicalParentStatus], + ].map(([label, value]) => `
${html(label)}
${html(value)}
`).join("")}
`; +} /** An interrupted first-task journey is not evidence of failed downstream behavior. */ export function isIncompleteFirstTaskResult( result: RunnerE2EResult, @@ -93,6 +108,7 @@ export function renderCaseOutcome( ${result.productJourney?.operations ? `

Eval operations: ${html(result.productJourney.operations.campaignId)} · ${result.productJourney.operations.requestCount} requests · model cost $${html(result.productJourney.operations.modelCostUsd ?? "unknown")} (setup $${html(result.productJourney.operations.setupCostUsd ?? "unknown")}, counted once). Feedback: ${html(result.productJourney.operations.feedback.collection)} · ${result.productJourney.operations.feedback.complaints} complaints · ${result.productJourney.operations.feedback.suggestions} suggestions.

` : ""} ${result.productJourney?.operations?.traceIds.length ? `
Trace correlation IDs
    ${result.productJourney.operations.traceIds.map(id => `
  • ${html(id)}
  • `).join("")}
` : ""} ${renderOnboardingConnect(result)} + ${renderOnboardingPreAcceptance(result)} ${result.providerConnection ? `

Target: ${html(result.providerConnection.target.origin)} · ${html(result.providerConnection.target.commit ?? "revision unavailable")} · ${result.providerConnection.assisted ? "Assisted login" : "Unassisted"} · ${html(result.providerConnection.authFreshness)} browser · ${html(result.providerConnection.entry)} / ${html(result.providerConnection.method)} · stopped at ${html(result.providerConnection.phase)}

` : ""} ${failures.length ? `

Failed checks:

    ${failures.map((c) => `
  • ${html(c.id)} — ${html(c.detail)}
  • `).join("")}
` : checks.length ? `

No behavioral matcher failed.${notReached.length ? " The journey is incomplete; unexercised checks are not passes." : failed ? " The overall failure came from a separate run, cleanup, or evidence check." : ""}

` : ""} ${notReached.length ? `

Not reached:

    ${notReached.map((c) => `
  • ${html(c.id)} — ${html(c.notReached)}
  • `).join("")}
` : ""} diff --git a/tests/runner-e2e/daytona-image-content.ts b/tests/runner-e2e/daytona-image-content.ts index 8ab17a54a2e..917bcd3d592 100644 --- a/tests/runner-e2e/daytona-image-content.ts +++ b/tests/runner-e2e/daytona-image-content.ts @@ -67,6 +67,7 @@ export const DAYTONA_IMAGE_INPUT_PATHS = [ "packages/paperclip-runner/test/fixtures/copilot-profile-v58-identity.json", "packages/paperclip-runner/test/fixtures/copilot-profile-v61-identity.json", "packages/paperclip-runner/test/fixtures/copilot-profile-v65-identity.json", + "packages/paperclip-runner/test/fixtures/copilot-profile-v66-identity.json", "packages/paperclip-runner/scripts/materialize-cursor-distribution.mjs", "packages/paperclip-runner/scripts/cursor-runtime-patch.mjs", "packages/paperclip-runner/scripts/build-verified-provider-entrypoints.mjs", diff --git a/tests/runner-e2e/eval-gateway-drain.test.ts b/tests/runner-e2e/eval-gateway-drain.test.ts index 24e6f43e776..9c5a73a06cc 100644 --- a/tests/runner-e2e/eval-gateway-drain.test.ts +++ b/tests/runner-e2e/eval-gateway-drain.test.ts @@ -3,6 +3,7 @@ import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, expect, it, vi } from "vitest"; import { openEvalCostLedger, readEvalCostLedger } from "./cost-ledger.js"; +import { startEvalRemoteBridge } from "./eval-remote-gateway.js"; import { forwardEvalRequest, startEvalGateway, type EvalGatewayReceipt, type EvalGatewayRoute } from "./eval-gateway.js"; const cleanups: Array<() => Promise> = []; @@ -181,3 +182,19 @@ it.each(["persist", "observe", "finish"] as const)("records a disconnect during expect(f.ledger.snapshot()).toMatchObject({ knownUsd: "0.000070000", heldUsd: "0.000000000", unknownRequests: 0 }); expect(transport).toHaveBeenCalledOnce(); }); + +it("retains terminal usage when a remote bridge client disconnects during shutdown", async () => { + const f = await fixture(), started = deferred(), release = deferred(); + const transport = vi.fn(async () => { started.resolve(); await release.promise; return finalResponse("openai"); }); + const gateway = await startEvalGateway({ ...f, transport }); cleanups.push(() => gateway.close()); + const bridge = await startEvalRemoteBridge({ gateway, deadlineAt: f.deadlineAt }); cleanups.push(() => bridge.close()); + const client = new AbortController(); + const result = fetch(bridge.baseUrl + "/responses", { method: "POST", signal: client.signal, + headers: { authorization: `Bearer ${gateway.token}` }, body: JSON.stringify({ model: "fixture", input: "hello" }) }).catch(error => error); + await started.promise; client.abort(); await bridge.close(); + const closing = gateway.close(); release.resolve(); await closing; + expect(await result).not.toBeInstanceOf(Response); + expect(transport).toHaveBeenCalledOnce(); + expect(f.receipts.at(-1)).toMatchObject({ clientDisconnected: true, final: true, usageComplete: true, costUsd: "0.000070000" }); + expect(f.ledger.snapshot()).toMatchObject({ knownUsd: "0.000070000", heldUsd: "0.000000000", unknownRequests: 0, inflight: 0 }); +}); diff --git a/tests/runner-e2e/eval-remote-gateway.test.ts b/tests/runner-e2e/eval-remote-gateway.test.ts new file mode 100644 index 00000000000..befe8d1e366 --- /dev/null +++ b/tests/runner-e2e/eval-remote-gateway.test.ts @@ -0,0 +1,101 @@ +import { mkdtemp, readFile, writeFile, rm, readdir } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { createServer } from "node:http"; +import { once } from "node:events"; +import { expect, it, vi } from "vitest"; +import { isEvalRemoteOrigin, startEvalRemoteBridge, startEvalRemoteGateway } from "./eval-remote-gateway.js"; + +it("forwards only authenticated model/OTLP routes and closes owned exposure", async () => { + const calls: Array<{ url: string; auth?: string; body: string }> = []; + const upstream = createServer(async (request, response) => { + let body = ""; for await (const chunk of request) body += chunk; + calls.push({ url: request.url!, auth: request.headers.authorization, body }); + response.writeHead(200, { "content-type": "text/event-stream" }); response.end("data: synthetic\n\n"); + }); + upstream.listen(0, "127.0.0.1"); await once(upstream, "listening"); + const address = upstream.address(); if (!address || typeof address === "string") throw new Error("Fixture did not bind"); + const url = `http://127.0.0.1:${address.port}`, modelToken = "a".repeat(64), otlpToken = "b".repeat(64); + const bridge = await startEvalRemoteBridge({ gateway: { baseUrl: url, token: modelToken }, + observability: { baseUrl: url + "/otel", token: otlpToken }, deadlineAt: Date.now() + 60_000 }); + try { + expect(await (await fetch(bridge.baseUrl + "/health")).json()).toEqual({ nonce: bridge.nonce }); + for (const path of ["/v1/responses", "/v1/messages?beta=true", "/otel/v1/traces", "/otel/v1/logs"]) { + expect((await fetch(bridge.baseUrl + path, { method: "POST" })).status).toBe(401); + const token = path.startsWith("/otel") ? otlpToken : modelToken; + const response = await fetch(bridge.baseUrl + path, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: "{}" }); + expect(response.status).toBe(200); expect(await response.text()).toBe("data: synthetic\n\n"); + } + expect(calls).toHaveLength(4); + expect(calls.map(call => call.url)).toEqual(["/v1/responses", "/v1/messages?beta=true", "/otel/v1/traces", "/otel/v1/logs"]); + expect(calls.map(call => call.auth)).toEqual([`Bearer ${modelToken}`, `Bearer ${modelToken}`, `Bearer ${otlpToken}`, `Bearer ${otlpToken}`]); + expect((await fetch(bridge.baseUrl + "/admin", { method: "POST", headers: { authorization: `Bearer ${modelToken}` } })).status).toBe(404); + expect((await fetch(bridge.baseUrl + "/otel/v1/traces", { method: "POST", headers: { authorization: `Bearer ${modelToken}` } })).status).toBe(401); + await bridge.close(); await expect(fetch(bridge.baseUrl + "/health")).rejects.toThrow(); + } finally { await bridge.close(); upstream.closeAllConnections(); await new Promise(resolve => upstream.close(() => resolve())); } +}); + +it("admits only HTTPS quick-tunnel origins and bounded loopback destinations", async () => { + expect(isEvalRemoteOrigin("https://synthetic-fixture.trycloudflare.com")).toBe(true); + for (const value of ["https://foreign.invalid", "http://synthetic.trycloudflare.com", "https://synthetic.trycloudflare.com/path", "https://synthetic.trycloudflare.com?token=private"]) expect(isEvalRemoteOrigin(value)).toBe(false); + await expect(startEvalRemoteBridge({ gateway: { baseUrl: "https://foreign.invalid", token: "a".repeat(64) }, deadlineAt: Date.now() + 60_000 })).rejects.toThrow("policy"); +}); + + +it("fails before remote admission when the tunnel executable is unavailable", async () => { + await expect(startEvalRemoteGateway({ gateway: { baseUrl: "http://127.0.0.1:1", token: "a".repeat(64) }, + deadlineAt: Date.now() + 5000 }, { PAPERCLIP_EVAL_CLOUDFLARED: "/missing/synthetic-cloudflared", PATH: "" })) + .rejects.toThrow("Remote eval tunnel executable unavailable"); +}); + +it("closes remote exposure at its original campaign deadline", async () => { + const bridge = await startEvalRemoteBridge({ gateway: { baseUrl: "http://127.0.0.1:1", token: "a".repeat(64) }, deadlineAt: Date.now() + 50 }); + try { + await new Promise(resolve => setTimeout(resolve, 100)); + await expect(fetch(bridge.baseUrl + "/health")).rejects.toThrow(); + } finally { await bridge.close(); } +}); + + +it.each(["explicit", "empty-override"])("waits for registration and the correct public nonce, then terminates the owned tunnel: %s", async mode => { + const root = await mkdtemp(path.join(tmpdir(), "eval-fake-tunnel-")); + const executable = path.join(root, "cloudflared"), state = path.join(root, "state.json"), stopped = path.join(root, "stopped"); + const origin = "https://synthetic-owned.trycloudflare.com"; + const script = `#!${process.execPath} +const fs = require("node:fs"); +fs.writeFileSync(${JSON.stringify(state)}, JSON.stringify({baseUrl:process.argv.at(-1),pid:process.pid,env:process.env})); +process.stderr.write("${origin}\\n"); +setTimeout(() => process.stderr.write("Registered tunnel connection\\n"), 20); +process.on("SIGTERM", () => { fs.writeFileSync(${JSON.stringify(stopped)}, "stopped"); process.exit(0); }); +setInterval(() => {}, 1000); +`; + await writeFile(executable, script, { mode: 0o700 }); + const realFetch = globalThis.fetch; let healthChecks = 0; + const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async url => { + expect(String(url)).toBe(origin + "/health"); + if (++healthChecks === 1) return new Response(JSON.stringify({ nonce: "foreign" }), { status: 200 }); + const { baseUrl } = JSON.parse(await readFile(state, "utf8")); + return realFetch(baseUrl + "/health"); + }); + let remote: Awaited> | undefined; + try { + remote = await startEvalRemoteGateway({ gateway: { baseUrl: "http://127.0.0.1:1", token: "a".repeat(64) }, + deadlineAt: Date.now() + 10_000 }, { PAPERCLIP_EVAL_CLOUDFLARED: mode === "explicit" ? executable : "", PATH: root, OPENAI_API_KEY: "must-not-leave" }); + expect(remote.baseUrl).toBe(origin); expect(healthChecks).toBe(2); + const { baseUrl, pid, env } = JSON.parse(await readFile(state, "utf8")); + expect(env.OPENAI_API_KEY).toBeUndefined(); expect(env.HOME).not.toBe(process.env.HOME); + await remote.close(); + expect(await readFile(stopped, "utf8")).toBe("stopped"); + expect(() => process.kill(pid, 0)).toThrow(); + await expect(realFetch(baseUrl + "/health")).rejects.toThrow(); + await expect(readFile(path.join(env.HOME, "anything"))).rejects.toThrow(); + } finally { fetchSpy.mockRestore(); await remote?.close(); await rm(root, { recursive: true, force: true }); } +}, 15_000); + + +it("cleans the private tunnel home after synchronous spawn rejection", async () => { + const before = new Set((await readdir(tmpdir())).filter(name => name.startsWith("eval-cloudflare-"))); + await expect(startEvalRemoteGateway({ gateway: { baseUrl: "http://127.0.0.1:1", token: "a".repeat(64) }, + deadlineAt: Date.now() + 60_000 }, { PAPERCLIP_EVAL_CLOUDFLARED: "invalid\0executable", PATH: "" })).rejects.toThrow(); + expect((await readdir(tmpdir())).filter(name => name.startsWith("eval-cloudflare-") && !before.has(name))).toEqual([]); +}); diff --git a/tests/runner-e2e/eval-remote-gateway.ts b/tests/runner-e2e/eval-remote-gateway.ts new file mode 100644 index 00000000000..2d5d1d85054 --- /dev/null +++ b/tests/runner-e2e/eval-remote-gateway.ts @@ -0,0 +1,119 @@ +import { spawn } from "node:child_process"; +import { randomBytes, timingSafeEqual } from "node:crypto"; +import { once } from "node:events"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { createServer, request as httpRequest } from "node:http"; + +export const isEvalRemoteOrigin = (value: string) => /^https:\/\/[a-z0-9-]+\.trycloudflare\.com$/.test(value); + +export async function startEvalRemoteBridge(input: { + gateway: { baseUrl: string; token: string }; + observability?: { baseUrl: string; token: string }; + deadlineAt: number; +}) { + if (!Number.isFinite(input.deadlineAt) || input.deadlineAt <= Date.now() || input.deadlineAt > Date.now() + 4 * 3_600_000 + || !/^http:\/\/127\.0\.0\.1:[1-9][0-9]{0,4}$/.test(input.gateway.baseUrl) + || input.observability && !/^http:\/\/127\.0\.0\.1:[1-9][0-9]{0,4}\/otel$/.test(input.observability.baseUrl) + || !/^[a-f0-9]{64}$/.test(input.gateway.token) + || input.observability && !/^[a-f0-9]{64}$/.test(input.observability.token)) throw new Error("Invalid remote eval bridge policy"); + try { new URL(input.gateway.baseUrl); if (input.observability) new URL(input.observability.baseUrl); } + catch { throw new Error("Invalid remote eval bridge policy"); } + const nonce = randomBytes(32).toString("hex"); + let closed = false; + const server = createServer((incoming, outgoing) => { + if (closed || Date.now() >= input.deadlineAt) { outgoing.writeHead(503).end(); return; } + if (incoming.method === "GET" && incoming.url === "/health") { + outgoing.writeHead(200, { "content-type": "application/json", "cache-control": "no-store" }).end(JSON.stringify({ nonce })); return; + } + const model = ["/responses", "/v1/responses", "/messages", "/messages?beta=true", "/v1/messages", "/v1/messages?beta=true"].includes(incoming.url ?? ""); + const otlp = ["/otel/v1/traces", "/otel/v1/logs"].includes(incoming.url ?? ""); + const destination = model ? input.gateway : otlp ? input.observability : undefined; + if (incoming.method !== "POST" || !destination) { outgoing.writeHead(404).end(); return; } + const auth = String(incoming.headers.authorization ?? "").replace(/^Bearer /, "") || String(incoming.headers["x-api-key"] ?? ""); + const presented = Buffer.from(auth), expected = Buffer.from(destination.token); + if (presented.length !== expected.length || !timingSafeEqual(presented, expected)) { outgoing.writeHead(401).end(); return; } + const suffix = otlp ? incoming.url!.slice("/otel".length) : incoming.url!; + const headers: Record = { authorization: `Bearer ${destination.token}` }; + for (const name of ["content-type", "content-encoding", "anthropic-beta", "anthropic-version", "x-bt-parent"]) { + const value = incoming.headers[name]; if (typeof value === "string") headers[name] = value; + } + const upstream = httpRequest(destination.baseUrl + suffix, { method: "POST", headers }, response => { + if (outgoing.destroyed) { response.destroy(); return; } + outgoing.writeHead(response.statusCode ?? 502, response.headers); response.pipe(outgoing); + response.on("error", () => outgoing.destroy()); + }); + upstream.on("error", () => { if (!outgoing.headersSent) outgoing.writeHead(502).end(); else outgoing.destroy(); }); + incoming.on("aborted", () => upstream.destroy()); + incoming.on("error", () => upstream.destroy()); + outgoing.on("error", () => upstream.destroy()); + outgoing.on("close", () => { if (!outgoing.writableEnded) upstream.destroy(); }); + incoming.pipe(upstream); + }); + server.requestTimeout = 15_000; server.headersTimeout = 10_000; + server.listen(0, "127.0.0.1"); await once(server, "listening"); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Remote eval bridge did not bind loopback"); + let timer: ReturnType; + let closing: Promise | undefined; + const close = () => closing ??= (async () => { + closed = true; clearTimeout(timer); + server.closeAllConnections(); await new Promise(resolve => server.close(() => resolve())); + })(); + timer = setTimeout(() => { void close().catch(() => {}); }, input.deadlineAt - Date.now()); timer.unref(); + return { baseUrl: `http://127.0.0.1:${address.port}`, nonce, close }; +} + +export async function startEvalRemoteGateway(input: Parameters[0], env: NodeJS.ProcessEnv) { + const bridge = await startEvalRemoteBridge(input); + let home: string; + try { home = await mkdtemp(path.join(tmpdir(), "eval-cloudflare-")); } + catch (error) { await bridge.close(); throw error; } + let tunnel: ReturnType | undefined; + let closing: Promise | undefined, deadlineTimer: ReturnType | undefined; + const close = () => closing ??= (async () => { + clearTimeout(deadlineTimer); + const errors: unknown[] = []; + try { await bridge.close(); } catch (error) { errors.push(error); } + try { + if (tunnel?.pid && tunnel.exitCode === null && tunnel.signalCode === null) { + const exited = once(tunnel, "exit").catch(() => {}); + tunnel.kill("SIGTERM"); const timer = setTimeout(() => tunnel?.kill("SIGKILL"), 5000); + try { await exited; } finally { clearTimeout(timer); } + } + } catch (error) { errors.push(error); } + try { await rm(home, { recursive: true, force: true }); } catch (error) { errors.push(error); } + if (errors.length) throw new AggregateError(errors, "Remote eval exposure cleanup failed"); + })(); + try { + tunnel = spawn(env.PAPERCLIP_EVAL_CLOUDFLARED?.trim() || "cloudflared", + ["tunnel", "--no-autoupdate", "--protocol", "http2", "--url", bridge.baseUrl], + { stdio: ["ignore", "pipe", "pipe"], env: { PATH: env.PATH, HOME: home, TMPDIR: home } }); + deadlineTimer = setTimeout(() => { void close().catch(() => {}); }, input.deadlineAt - Date.now()); deadlineTimer.unref(); + const child = tunnel; + const origin = await new Promise((resolve, reject) => { + let output = ""; + const timer = setTimeout(() => finish(new Error("Remote eval tunnel startup timed out")), Math.min(120_000, input.deadlineAt - Date.now())); + const finish = (error?: Error, value?: string) => { clearTimeout(timer); if (error) reject(error); else resolve(value!); }; + child.once("error", () => finish(new Error("Remote eval tunnel executable unavailable"))); + child.once("exit", () => finish(new Error("Remote eval tunnel exited"))); + const read = (chunk: Buffer) => { + output = (output + chunk.toString()).slice(-32768); + const value = output.match(/https:\/\/[a-z0-9-]+\.trycloudflare\.com/)?.[0]; + if (value && output.includes("Registered tunnel connection")) finish(undefined, value); + }; + child.stdout!.on("data", read); child.stderr!.on("data", read); + }); + const readyBy = Math.min(Date.now() + 120_000, input.deadlineAt); + while (Date.now() < readyBy) { + try { + const response = await fetch(origin + "/health", { redirect: "error", signal: AbortSignal.timeout(Math.min(5000, readyBy - Date.now())) }); + if (response.ok && (await response.json() as { nonce?: string }).nonce === bridge.nonce) return { baseUrl: origin, close }; + await response.body?.cancel(); + } catch {} + await new Promise(resolve => setTimeout(resolve, 1000)); + } + throw new Error("Remote eval tunnel not publicly reachable before admission"); + } catch (error) { await close(); throw error; } +} diff --git a/tests/runner-e2e/harness-env.ts b/tests/runner-e2e/harness-env.ts index 2ea3cd91a40..9aa865b0c1f 100644 --- a/tests/runner-e2e/harness-env.ts +++ b/tests/runner-e2e/harness-env.ts @@ -2,6 +2,7 @@ import { installedDaytonaPluginKeys } from "./installed-daytona-plugin.js"; import { QUALIFIED_ACPX_PROFILES } from "../../packages/paperclip-runner/src/drivers/acpx/qualified-profiles.js"; import { assertInstalledCliSelection, installedCliKeys, usesInstalledCli } from "./installed-cli.js"; import path from "node:path"; +import { isEvalRemoteOrigin } from "./eval-remote-gateway.js"; import { chatNeedsApiTools, isManagedHiringCase } from "./chat-cases.js"; import { CREDENTIAL_NAMES } from "./types.js"; import type { MatrixExecution } from "./types.js"; @@ -14,6 +15,7 @@ const AMBIENT_PAPERCLIP_CREDENTIAL_KEYS = [ "PAPERCLIP_E2E_GATEWAY_TOKEN", "PAPERCLIP_E2E_GATEWAY_PHASE_PATH", "PAPERCLIP_E2E_OTLP_RELAY_URL", + "PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN", "PAPERCLIP_SETUP_TOKEN", "PAPERCLIP_SECRETS_MASTER_KEY", "PAPERCLIP_SECRETS_MASTER_KEY_FILE", @@ -41,6 +43,8 @@ export function applyEvalGatewayEnvironment(env: NodeJS.ProcessEnv, credential: || PROVIDER_SECRET_KEY.test(name) && /KEY|TOKEN|SECRET|PASSWORD/.test(name)) delete env[name]; } delete env.PAPERCLIP_E2E_OTLP_RELAY_URL; + delete env.PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN; + if (isEvalRemoteOrigin(gateway.baseUrl)) env.PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN = gateway.baseUrl; env[credential] = gateway.token; env.PAPERCLIP_E2E_GATEWAY_BASE_URL = gateway.baseUrl; env.PAPERCLIP_E2E_GATEWAY_TOKEN = gateway.token; @@ -103,13 +107,13 @@ export function resolvePaperclipRemoteRunnerBinaryForHarness( runnerBinary: string | undefined, configuredPath = process.env.PAPERCLIP_RUNNER_REMOTE_BINARY_PATH, platform: NodeJS.Platform = process.platform, + architecture: NodeJS.Architecture = process.arch, ): string | undefined { if (configuredPath?.trim()) return configuredPath; if (!runnerBinary) return undefined; - // Daytona runs Linux. A default debug binary built by a macOS developer is - // Mach-O and cannot be staged into that sandbox. Leave the remote override - // unset so the pinned Daytona image's verified runnerd is discovered instead. - if (platform !== "linux") return undefined; + // The pinned Daytona runtime is Linux/x64. Other controller binaries cannot + // run there; discover its verified runnerd unless an explicit cross-build is set. + if (platform !== "linux" || architecture !== "x64") return undefined; return executions.some( (execution) => execution.profile.generation === "native" && @@ -133,6 +137,7 @@ export function buildRunnerE2EProcessEnvironment( const result = { ...source }; delete result.PAPERCLIP_E2E_GATEWAY_PHASE_PATH; delete result.PAPERCLIP_E2E_OTLP_RELAY_URL; + delete result.PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN; // Announcements are unrelated to the scenarios and obscure screenshot evidence. result.PAPERCLIP_ANNOUNCEMENTS_ENABLED = "false"; delete result.OPENCODE_ALLOW_ALL_MODELS; diff --git a/tests/runner-e2e/launch.ts b/tests/runner-e2e/launch.ts index b65d4f9fecb..74ec9179866 100644 --- a/tests/runner-e2e/launch.ts +++ b/tests/runner-e2e/launch.ts @@ -595,6 +595,9 @@ async function runAttempt(input: { }; if (gateway) { applyEvalGatewayEnvironment(childEnv, execution.profile.credential, gateway); + // The fixture/server controller needs Daytona provisioning access. The + // remote provider still receives only the scoped model and OTLP tokens. + if (execution.environment.id === "daytona") childEnv.DAYTONA_API_KEY = process.env.DAYTONA_API_KEY; } // The database URLs are stripped here and again at the Playwright web-server // boundary so a developer's shell can never redirect this paid test. diff --git a/tests/runner-e2e/live-fixtures.ts b/tests/runner-e2e/live-fixtures.ts index 348db2d08af..517b25b3d90 100644 --- a/tests/runner-e2e/live-fixtures.ts +++ b/tests/runner-e2e/live-fixtures.ts @@ -1,3 +1,4 @@ +import { isEvalRemoteOrigin } from "./eval-remote-gateway.js"; import type { AiConnectionBinding } from "../../packages/shared/src/ai-connections.js"; import { verifyInstalledDaytonaPlugin } from "./installed-daytona-plugin.js"; import { NATIVE_COMPLETION_BUDGET_CENTS } from "./native-completion-defaults.js"; @@ -280,8 +281,9 @@ export async function setupLiveFixtures(input: { const gatewayUrl = process.env.PAPERCLIP_E2E_GATEWAY_BASE_URL; const gatewayToken = process.env.PAPERCLIP_E2E_GATEWAY_TOKEN; const budgetedGateway = Boolean(gatewayUrl || gatewayToken); - if (budgetedGateway && (!gatewayUrl || !gatewayToken || !/^http:\/\/127\.0\.0\.1:[0-9]+$/.test(gatewayUrl) - || execution.environment.id !== "local" || !["core-compatibility", "feedback-persistence"].includes(execution.suite.id))) throw new Error("Unqualified fixture gateway route"); + if (budgetedGateway && (!gatewayUrl || !gatewayToken || !(execution.environment.id === "local" ? /^http:\/\/127\.0\.0\.1:[0-9]+$/.test(gatewayUrl) + : isEvalRemoteOrigin(gatewayUrl) && gatewayUrl === process.env.PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN) + || !["local", "daytona"].includes(execution.environment.id) || !["core-compatibility", "feedback-persistence"].includes(execution.suite.id))) throw new Error("Unqualified fixture gateway route"); if (managedHiring || savedCopilot || budgetedGateway) { registry.register({ id: "ai-connection", diff --git a/tests/runner-e2e/operations.test.ts b/tests/runner-e2e/operations.test.ts index 452403765fd..8e03148dfdc 100644 --- a/tests/runner-e2e/operations.test.ts +++ b/tests/runner-e2e/operations.test.ts @@ -3,6 +3,7 @@ import { access, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promise import { createHash } from "node:crypto"; import path from "node:path"; import { tmpdir } from "node:os"; +import * as remoteGateway from "./eval-remote-gateway.js"; import { runnerMatrix } from "./catalog.js"; import { validateEvalOperationsManifest, openEvalOperations, type EvalOperationsManifest } from "./operations.js"; import { setupLiveFixtures } from "./live-fixtures.js"; @@ -17,7 +18,7 @@ function manifest(): EvalOperationsManifest { models: [{ provider: "openai", maximumInputTokens: 1000, maximumOutputTokens: 100, timeoutMs: 1000, price: { provider: "openai", model: cell.profile.model, version: "fixture-only", sourceUrl: "https://provider.invalid/fixture", inputUsdPerMillion: "2", cachedReadUsdPerMillion: "1", outputUsdPerMillion: "10", cacheWrite30mUsdPerMillion: "2", cacheWrite12hUsdPerMillion: "2" } }] }; } -afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); }); +afterEach(() => { vi.restoreAllMocks(); vi.unstubAllEnvs(); vi.unstubAllGlobals(); }); it("admits only the exact bounded local selection and explicitly priced models", () => { expect(validateEvalOperationsManifest(manifest(), [cell], 1)).toMatchObject({ route: "direct" }); expect(() => validateEvalOperationsManifest({ ...manifest(), executionIds: [] }, [cell], 1)).toThrow("selection"); @@ -191,3 +192,44 @@ it.each([ expect(agent!.budgetMonthlyCents).toBe(1_000); } }); + + +it("requires explicit remote gateway admission and infrastructure credentials before opening a journal", async () => { + const remote = runnerMatrix.find(e => e.suite.id === cell.suite.id && e.profile.id === cell.profile.id && e.environment.id === "daytona")!; + const value = { ...manifest(), executionIds: [remote.id], remoteGateway: "cloudflare" as const }; + expect(validateEvalOperationsManifest(value, [remote], 0)).toBe(value); + const root = await mkdtemp(path.join(tmpdir(), "eval-remote-preflight-")); + try { + value.ledgerPath = path.join(root, "ledger.jsonl"); + const file = path.join(root, "manifest.json"); await writeFile(file, JSON.stringify(value)); + await expect(openEvalOperations(file, [remote], 0, { OPENAI_API_KEY: "synthetic" })).rejects.toThrow("credential"); + await expect(access(value.ledgerPath)).rejects.toMatchObject({ code: "ENOENT" }); + } finally { await rm(root, { recursive: true, force: true }); } +}); + +it("gives remote workers scoped gateway/relay URLs and closes both local listeners and the tunnel", async () => { + const remote = runnerMatrix.find(e => e.suite.id === cell.suite.id && e.profile.id === cell.profile.id && e.environment.id === "daytona")!; + const root = await mkdtemp(path.join(tmpdir(), "eval-remote-operation-")); + const close = vi.fn(async () => {}), origin = "https://synthetic-owned.trycloudflare.com"; + const start = vi.spyOn(remoteGateway, "startEvalRemoteGateway").mockResolvedValue({ baseUrl: origin, close }); + const value = { ...manifest(), executionIds: [remote.id], remoteGateway: "cloudflare" as const, ledgerPath: path.join(root, "ledger.jsonl") }; + value.limits.deadlineAt = new Date(Date.now() + 24 * 3_600_000).toISOString(); + const file = path.join(root, "manifest.json"), env = { OPENAI_API_KEY: "provider-private", DAYTONA_API_KEY: "infra-private", + BRAINTRUST_API_KEY: "trace-private", PAPERCLIP_E2E_BRAINTRUST_PROJECT_ID: "11111111-1111-4111-8111-111111111111" }; + let operations: Awaited> | undefined; + try { + await writeFile(file, JSON.stringify(value)); operations = await openEvalOperations(file, [remote], 0, env); + const endpoint = await operations.start(remote, "remote-one", 1); + const worker: NodeJS.ProcessEnv = { ...env }; applyEvalGatewayEnvironment(worker, remote.profile.credential, endpoint); + expect(worker).toMatchObject({ PAPERCLIP_E2E_GATEWAY_BASE_URL: origin, PAPERCLIP_E2E_REMOTE_GATEWAY_ORIGIN: origin, + PAPERCLIP_E2E_OTLP_RELAY_URL: `${origin}/otel`, OPENAI_API_KEY: endpoint.token, BRAINTRUST_API_KEY: endpoint.observability!.token }); + expect(JSON.stringify(worker)).not.toMatch(/provider-private|infra-private|trace-private/); + const local = start.mock.calls[0]![0]; + expect(local.gateway.baseUrl).toMatch(/^http:\/\/127\.0\.0\.1:/); + expect(local.deadlineAt).toBeLessThanOrEqual(Date.now() + 4 * 3_600_000); + expect(local.deadlineAt).toBeLessThan(Date.parse(value.limits.deadlineAt)); + await operations.close(); operations = undefined; expect(close).toHaveBeenCalledOnce(); + await expect(fetch(local.gateway.baseUrl)).rejects.toThrow(); + await expect(fetch(local.observability!.baseUrl)).rejects.toThrow(); + } finally { await operations?.close(); await rm(root, { recursive: true, force: true }); } +}); diff --git a/tests/runner-e2e/operations.ts b/tests/runner-e2e/operations.ts index ed4e82d1cee..831520dd9e2 100644 --- a/tests/runner-e2e/operations.ts +++ b/tests/runner-e2e/operations.ts @@ -4,6 +4,7 @@ import { createHash } from "node:crypto"; import { createEvalAttemptPhase, readEvalAttemptPhase } from "./attempt-phase.js"; import { EvalSpendState, openEvalCostLedger, type EvalSpendLimits } from "./cost-ledger.js"; import { evalRequestReservation, startEvalGateway, type EvalGatewayRoute, type EvalGatewayReceipt } from "./eval-gateway.js"; +import { startEvalRemoteGateway } from "./eval-remote-gateway.js"; import type { MatrixExecution } from "./types.js"; import { evalBraintrustConfig } from "./braintrust.js"; import { startEvalOtlpRelay } from "./eval-otlp-relay.js"; @@ -17,13 +18,15 @@ export interface EvalOperationsManifest { maxAttemptsPerExecution: number; route: "direct" | "braintrust"; projectId?: string; + remoteGateway?: "cloudflare"; models: Array>; } const fields = (value: object, names: string[]) => { if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).some(name => !names.includes(name))) throw new Error("Invalid eval operations manifest fields"); }; export function validateEvalOperationsManifest(value: EvalOperationsManifest, executions: readonly MatrixExecution[], maxRetries: number) { - fields(value, ["version", "limits", "ledgerPath", "executionIds", "maxAttemptsPerExecution", "route", "projectId", "models"]); + fields(value, ["version", "limits", "ledgerPath", "executionIds", "maxAttemptsPerExecution", "route", "projectId", "remoteGateway", "models"]); + if (value.remoteGateway !== undefined && value.remoteGateway !== "cloudflare") throw new Error("Invalid remote eval gateway policy"); const initial = new EvalSpendState(value.limits).snapshot(); const initialModelAllowance = usdToUnits(value.limits.maxUsd) - usdToUnits(initial.committedUsd); if (value.version !== 1 || !["direct", "braintrust"].includes(value.route) || !path.isAbsolute(value.ledgerPath) @@ -46,9 +49,11 @@ export function validateEvalOperationsManifest(value: EvalOperationsManifest, ex models.set(key, usdToUnits(evalRequestReservation({ ...model, mode: value.route, apiKey: "validation" }))); } for (const execution of executions) { - if (execution.environment.id !== "local" || !["core-compatibility", "feedback-persistence"].includes(execution.suite.id) + if (!(execution.environment.id === "local" || execution.environment.id === "daytona" && value.remoteGateway === "cloudflare" + && ["runner-codex", "runner-acpx-claude"].includes(execution.profile.id)) || !["core-compatibility", "feedback-persistence"].includes(execution.suite.id) || !["runner-codex", "runner-acpx-claude", "legacy-codex", "legacy-claude"].includes(execution.profile.id) - || execution.requiredCredentials.length !== 1) throw new Error(`Unqualified budgeted eval route: ${execution.id}`); + || execution.requiredCredentials.length !== (execution.environment.id === "daytona" ? 2 : 1) + || execution.requiredCredentials.filter(name => name !== "DAYTONA_API_KEY").length !== 1) throw new Error(`Unqualified budgeted eval route: ${execution.id}`); const provider = execution.profile.credential === "ANTHROPIC_API_KEY" ? "anthropic" : "openai"; if (!models.has(`${provider}:${execution.profile.model}`)) throw new Error(`Missing exact model pricing: ${execution.id}`); if (models.get(`${provider}:${execution.profile.model}`)! > initialModelAllowance) { @@ -65,7 +70,8 @@ export async function openEvalOperations(file: string, executions: readonly Matr || Date.parse(manifest.limits.deadlineAt) >= Date.parse(manifest.limits.overhead.expiresAt))) { throw new Error("Operating overhead allowance does not cover the campaign window"); } - const required = manifest.route === "braintrust" ? ["BRAINTRUST_API_KEY"] : executions.map(execution => execution.profile.credential); + const required = [...(manifest.route === "braintrust" ? ["BRAINTRUST_API_KEY"] : executions.map(execution => execution.profile.credential)), + ...(executions.some(execution => execution.environment.id === "daytona") ? ["DAYTONA_API_KEY"] : [])]; if (required.some(name => !env[name]?.trim())) throw new Error("Missing eval gateway upstream credential"); const observability = evalBraintrustConfig({ ...env, PAPERCLIP_E2E_OTLP_RELAY_URL: undefined }); if (observability && manifest.route === "braintrust" && observability.projectId !== manifest.projectId) throw new Error("Eval gateway and OTLP projects differ"); @@ -99,14 +105,19 @@ export async function openEvalOperations(file: string, executions: readonly Matr persist: (receipt: EvalGatewayReceipt) => atomicJson(path.join(receiptDirectory, `${receipt.requestId}.json`), receipt), }); let relay: Awaited> | undefined; + let remote: Awaited> | undefined; try { if (observability) relay = await startEvalOtlpRelay({ projectId: observability.projectId, apiKey: observability.apiKey, campaignId: manifest.limits.campaignId, attemptId, deadlineAt: Math.min(Date.parse(manifest.limits.deadlineAt), Date.now() + 4 * 3_600_000), receiptPath: path.join(`${manifest.ledgerPath}.otlp`, `${createHash("sha256").update(attemptId).digest("hex")}.jsonl`) }); - } catch (error) { await gateway.close(); throw error; } + if (execution.environment.id === "daytona") remote = await startEvalRemoteGateway({ gateway, observability: relay, + deadlineAt: Math.min(Date.parse(manifest.limits.deadlineAt), Date.now() + 4 * 3_600_000) }, env); + } catch (error) { await Promise.allSettled([gateway.close(), relay?.close()]); throw error; } let closing: Promise | undefined; - const endpoint = { ...gateway, phasePath, observability: relay, close: () => closing ??= (async () => { - const results = await Promise.allSettled([gateway.close(), relay?.close()]); + const endpoint = { ...gateway, baseUrl: remote?.baseUrl ?? gateway.baseUrl, phasePath, + observability: relay ? { ...relay, baseUrl: remote ? `${remote.baseUrl}/otel` : relay.baseUrl } : undefined, close: () => closing ??= (async () => { + const remoteResults = await Promise.allSettled([remote?.close()]); + const results = [...remoteResults, ...await Promise.allSettled([gateway.close(), relay?.close()])]; active.delete(endpoint); if (results.some(result => result.status === "rejected")) throw new Error("Eval gateway or OTLP relay cleanup failed"); })() }; diff --git a/tests/runner-e2e/pi-controls-evidence.test.ts b/tests/runner-e2e/pi-controls-evidence.test.ts index fe8e458c89b..5c49d899045 100644 --- a/tests/runner-e2e/pi-controls-evidence.test.ts +++ b/tests/runner-e2e/pi-controls-evidence.test.ts @@ -188,8 +188,8 @@ describe("Pi controls catalog admission", () => { // Extended v3 states the strict file oracle's task-wide Bash limit. const pi = runnerMatrix.find(c => c.profile.qualificationCandidate === "pi")!.profile; expect(pi.modelQualification?.qualificationId).toBe("pi:0.0.33:1.0.0:openrouter"); - expect(runnerSuites.find(s => s.id === "pi-native")!.definitionMetadata).toMatchObject({ version: 23, remoteProcExit: "separately-confirmed-absence-after-read-failure", taskCreation: "explicit-title-and-creation-response-id", agentMemoryParent: "public-managed-file-seed-before-admission", incompleteTerminalCleanup: "retirement-retained-with-failed-watch", profileVersion: 29, agentMemoryContent: "utf8-nonce-plus-final-lf", agentMemoryPrompt: "single-json-write-and-content-bound-native-read-both-runs", agentMemoryReadAuthority: "local-withheld-or-exact-remote-agent-run-file", taskPromptTransport: "fenced-markdown-paste-and-multiline-literal-escapes", nativeFinish: "current-contract-objective-evidence-refs", providerFaultExecutable: "stable-preinstalled-runner-link-and-snapshot-node-inode-with-held-bootstrap-fd-3-or-7" }); - expect(runnerSuites.find(s => s.id === "pi-controls")!.definitionMetadata).toMatchObject({ version: 10, remoteProcExit: "separately-confirmed-absence-after-read-failure", taskCreation: "explicit-title-and-creation-response-id", profileVersion: 29, + expect(runnerSuites.find(s => s.id === "pi-native")!.definitionMetadata).toMatchObject({ version: 23, remoteProcExit: "separately-confirmed-absence-after-read-failure", taskCreation: "explicit-title-and-creation-response-id", agentMemoryParent: "public-managed-file-seed-before-admission", incompleteTerminalCleanup: "retirement-retained-with-failed-watch", profileVersion: 30, agentMemoryContent: "utf8-nonce-plus-final-lf", agentMemoryPrompt: "single-json-write-and-content-bound-native-read-both-runs", agentMemoryReadAuthority: "local-withheld-or-exact-remote-agent-run-file", taskPromptTransport: "fenced-markdown-paste-and-multiline-literal-escapes", nativeFinish: "current-contract-objective-evidence-refs", providerFaultExecutable: "stable-preinstalled-runner-link-and-snapshot-node-inode-with-held-bootstrap-fd-3-or-7" }); + expect(runnerSuites.find(s => s.id === "pi-controls")!.definitionMetadata).toMatchObject({ version: 10, remoteProcExit: "separately-confirmed-absence-after-read-failure", taskCreation: "explicit-title-and-creation-response-id", profileVersion: 30, remoteProcessIdentity: "observer-pid-startTicks-bootId", controlPlaneSettlement: "required-scoped-result-and-terminal-after-runner" }); expect(runnerSuites.find(s => s.id === "extended-harnesses")!.definitionMetadata).toMatchObject({ version: 6, piFileArtifactTitle: "exact-filename", piFileCommandTransport: "fenced-bash-markdown-paste" }); @@ -199,7 +199,7 @@ describe("Pi controls catalog admission", () => { } const hashes = Object.fromEntries(runnerSuites.filter(s => ["pi-native", "native-active-stop", "extended-harnesses", "rich-acp-warm-continuity"].includes(s.id)).map(s => [s.id, suiteDefinitionHash(s)])); expect(hashes).toEqual({ - "pi-native": "d7d691088af76d460e353a3982f7732e6a65b2e5a23a66d2ef0d534f9d8dae48", + "pi-native": "04d49c6e098c24091d2939108a97722d1b8353e39192b87e98ee33377f183001", "native-active-stop": "4654a2169bff96cf8377fdd62441f5cabea5c613f447e518830f6b1efd82c051", "rich-acp-warm-continuity": "7e8a0012f2f67fba03cd94403b096a07cfc95cdb7aa568b4c3fd18b852d735f3", "extended-harnesses": "d5bcf5de0ade3a54e4e1560e76feb5d8a787039d56d2379cd501626d85ed7418", diff --git a/tests/runner-e2e/product-journeys/campaign.test.ts b/tests/runner-e2e/product-journeys/campaign.test.ts index 27d42b934f9..e039157c8f1 100644 --- a/tests/runner-e2e/product-journeys/campaign.test.ts +++ b/tests/runner-e2e/product-journeys/campaign.test.ts @@ -5,6 +5,10 @@ import os from "node:os"; import { pathToFileURL } from "node:url"; import { createConnectionProof } from "../connection-evidence.js"; import { retainOnboardingArtifact, validateOnboardingArtifact } from "./artifact-evidence.js"; +import { validateOnboardingPreAcceptanceEvidence, type OnboardingPreAcceptanceEvidence } from "./onboarding-pre-acceptance.js"; +const preAcceptanceEvidence: OnboardingPreAcceptanceEvidence = { version: 1, checkpoint: "awaiting-proposal", violations: ["attachment_present"], + taskCount: 1, attachmentCount: 1, documentCount: 0, nonPlanningDocumentCount: 0, + firstTaskMatchesCanonicalParent: true, firstTaskStatus: "in_review", canonicalParentStatus: "in_review" }; const persistenceFault = vi.hoisted(() => ({ kind: null as "write" | "rename" | null, active: false, latestReport: null as any })); vi.mock("node:fs/promises", async importOriginal => { const actual = await importOriginal(); @@ -33,7 +37,7 @@ import { regenerateRunnerDashboard } from "../dashboard-regenerate.js"; const roots: string[]=[]; afterEach(async()=>{vi.useRealTimers(); vi.restoreAllMocks(); persistenceFault.kind=null; persistenceFault.active=false; persistenceFault.latestReport=null; process.exitCode=0; await Promise.all(roots.splice(0).map(root=>rm(root,{recursive:true,force:true})));}); type Hook = "collect" | "release" | "stop" | "finalizeEvidence" | "checkpointEvidence"; -async function run(options:{authFailure?:boolean;onboardingFailure?:boolean;missingArtifact?:boolean;drift?:boolean;cleanupFailure?:boolean;collectionFailure?:boolean;mini?:boolean;missingReceipt?:boolean;zeroRequests?:boolean;missingCheck?:boolean;finalizationFailure?:boolean;persistenceFailure?:"write"|"rename";persistenceAfter?:"collection"|"release";stallHook?:Hook;onHook?:(hook:Hook,signal?:AbortSignal)=>void}={}) { +async function run(options:{authFailure?:boolean;onboardingFailure?:boolean;preAcceptanceFailure?:boolean;missingArtifact?:boolean;drift?:boolean;cleanupFailure?:boolean;collectionFailure?:boolean;mini?:boolean;missingReceipt?:boolean;zeroRequests?:boolean;missingCheck?:boolean;finalizationFailure?:boolean;persistenceFailure?:"write"|"rename";persistenceAfter?:"collection"|"release";stallHook?:Hook;onHook?:(hook:Hook,signal?:AbortSignal)=>void}={}) { const root=await mkdtemp(path.join(os.tmpdir(),"journey-campaign-test-")); roots.push(root); const module=path.join(root,"provider.mjs"); const proof = createConnectionProof(); @@ -44,7 +48,7 @@ let onboarded=false; const context={identity,company:{id:'company-1',issuePrefix:'QA'},api:{post:async()=>({id:'issue-fixture-1',identifier:'QA-1'})},startingState:'onboarding',capabilities:['company','board-user','task-write'],page:{async screenshot(){} }}; export const provider={version:1, async acquire(input){return { async authenticate(){${options.authFailure ? "throw {outcome:'failed',code:'login_code_expired'};" : ""}}, -async onboard(evidence){${options.onboardingFailure ? "throw {outcome:'failed',code:'onboarding_failed'};" : `${options.missingArtifact ? "" : `evidence.artifact=${JSON.stringify(artifact)};`}evidence.checks.push(...['fresh-workspace','ui-connection-and-native-agent','first-task-complete','downloadable-artifact-verified'${options.missingCheck ? "" : ",'native-trace-verified'"}].map(id=>({id,passed:true})));onboarded=true;return context;`}}, +async onboard(evidence){${options.preAcceptanceFailure ? `evidence.onboardingPreAcceptance=${JSON.stringify(preAcceptanceEvidence)};throw {outcome:'failed',code:'onboarding_work_before_acceptance'};` : options.onboardingFailure ? "throw {outcome:'failed',code:'onboarding_failed'};" : `${options.missingArtifact ? "" : `evidence.artifact=${JSON.stringify(artifact)};`}evidence.checks.push(...['fresh-workspace','ui-connection-and-native-agent','first-task-complete','downloadable-artifact-verified'${options.missingCheck ? "" : ",'native-trace-verified'"}].map(id=>({id,passed:true})));onboarded=true;return context;`}}, async prepare(){throw Error('onboarding receipt was not reused')}, async identity(){return {...identity,commit:'${(options.drift?"b":"a").repeat(40)}'}}, async collect(){${options.collectionFailure ? "throw Error('private collection error');" : options.missingReceipt ? "return [];" : `return onboarded?[{journeyId:'cloud-onboarding',runIds:['run-1'],usage:{runs:[{runId:'run-1',usage:{costUsd:0.01}}]},operations:{campaignId:input.campaignId,attemptId:input.campaignId+':cloud-onboarding:1',modelCostUsd:0.01,setupCostUsd:0.005,requestCount:${options.zeroRequests ? 0 : 2},feedback:{collection:'complete',complaints:0,suggestions:0},traceIds:['a'.repeat(32)]}}]:[];`}}, async own(){}, async release(){${options.cleanupFailure ? "throw Error('private raw error');" : "return 'released';"}}, async stop(){} @@ -109,6 +113,17 @@ describe("product journey campaign boundary outcomes",()=>{ expect(campaign.results.map((r:any)=>r.productJourney.outcome)).toEqual(["failed","blocked"]); expect(campaign.results[0].productJourney.phases.find((p:any)=>p.phase==="journey").outcome).toBe("failed"); }); + it("preserves failed pre-acceptance evidence in normalized results and blocks dependent reuse", async () => { + const campaign = await run({ preAcceptanceFailure: true }); + const [onboarding, dependent] = campaign.results; + expect(campaign.results.map((row:any) => row.productJourney.outcome)).toEqual(["failed", "blocked"]); + expect(onboarding).toMatchObject({ status: "failed", failureClass: "candidate_failure", error: "onboarding_work_before_acceptance" }); + expect(onboarding.productJourney.onboardingPreAcceptance).toEqual(preAcceptanceEvidence); + expect(() => validateOnboardingPreAcceptanceEvidence(onboarding.productJourney.onboardingPreAcceptance)).not.toThrow(); + expect(onboarding.productJourney.phases).toContainEqual({ phase: "journey", outcome: "failed", code: "onboarding_work_before_acceptance" }); + expect(dependent.error).toBe("prerequisite_journey_failed"); + expect(dependent.productJourney.onboardingPreAcceptance).toBeUndefined(); + }); it("reuses a verified onboarding context for the same shared story",async()=>{ const campaign=await run(); expect(campaign.results.map((r:any)=>r.status)).toEqual(["passed","passed"]); diff --git a/tests/runner-e2e/product-journeys/catalog.ts b/tests/runner-e2e/product-journeys/catalog.ts index c018e769aab..6077ff48c36 100644 --- a/tests/runner-e2e/product-journeys/catalog.ts +++ b/tests/runner-e2e/product-journeys/catalog.ts @@ -9,7 +9,7 @@ export function preparationPreview(config: TargetConfig) { target: { id: config.id, mode: config.mode, executionEnvironment: config.executionEnvironment }, journeys: config.journeys.map(id => productJourneys.find(journey => journey.id === id)!), configuration: config.baseline, - lifetime: config.mode === "existing-cloud" ? "persistent; clean run-owned resources only" : config.mode === "new-cloud" ? "disposable; delete on pass, retain failures for 24 hours" : "managed instance; remove after every attempt", + lifetime: config.mode === "existing-cloud" ? "persistent; clean run-owned resources only" : config.mode === "new-cloud" ? "disposable; delete on pass, retain failures up to 24 hours" : "managed instance; remove after every attempt", limits: config.limits, authentication: config.mode === "managed-self-hosted" ? "local-trusted" : "normal email-code sign-in", modelProfileId: config.modelProfileId ?? null, maxDisposableStacks: config.mode === "new-cloud" ? 1 : 0, diff --git a/tests/runner-e2e/product-journeys/cloud-onboarding.test.ts b/tests/runner-e2e/product-journeys/cloud-onboarding.test.ts index 0878ade8065..68adbdd2d53 100644 --- a/tests/runner-e2e/product-journeys/cloud-onboarding.test.ts +++ b/tests/runner-e2e/product-journeys/cloud-onboarding.test.ts @@ -2,6 +2,8 @@ import { describe, expect, it, vi } from "vitest"; import { cloudOnboarding, type CloudOnboardingInput } from "./cloud-onboarding.js"; import { onboardingFirstTask } from "./onboarding-first-task.js"; import { retainOnboardingArtifact } from "./artifact-evidence.js"; +import { JourneyError } from "./types.js"; +import type { OnboardingPreAcceptanceEvidence } from "./onboarding-pre-acceptance.js"; vi.mock("@playwright/test", () => ({ expect: (locator: { isVisible(): Promise }) => ({ toBeVisible: async () => { expect(await locator.isVisible()).toBe(true); }, @@ -118,6 +120,20 @@ function fixture(entry: Entry, options: { intentFailure?: boolean; qualification } describe("Cloud onboarding visible controls", () => { + it("retains pre-acceptance evidence while preserving the failed error and withholding completion markers", async () => { + const f = fixture("portfolio"); + const receipt: OnboardingPreAcceptanceEvidence = { version: 1, checkpoint: "awaiting-proposal", violations: ["attachment_present"], + taskCount: 1, attachmentCount: 1, documentCount: 0, nonPlanningDocumentCount: 0, + firstTaskMatchesCanonicalParent: true, firstTaskStatus: "in_review", canonicalParentStatus: "in_review" }; + vi.mocked(onboardingFirstTask).mockImplementationOnce(async input => { + input.onPreAcceptanceFailure?.(receipt); + throw new JourneyError("failed", "onboarding_work_before_acceptance"); + }); + await expect(cloudOnboarding(f.input)).rejects.toMatchObject({ outcome: "failed", code: "onboarding_work_before_acceptance" }); + expect(JSON.parse(JSON.stringify(f.input.evidence)).onboardingPreAcceptance).toEqual(receipt); + expect(f.input.evidence.checks.map(check => check.id)).toEqual(["fresh-workspace", "ui-connection-and-native-agent"]); + expect(f.events).not.toContain("native:verified"); + }); it.each(["first-run", "portfolio", "create-form"] as const)("creates from %s with intent before dispatch and qualification before paid Connect", async entry => { const f = fixture(entry); const result = await cloudOnboarding(f.input); diff --git a/tests/runner-e2e/product-journeys/cloud-onboarding.ts b/tests/runner-e2e/product-journeys/cloud-onboarding.ts index 6f9a75634a7..215871b411d 100644 --- a/tests/runner-e2e/product-journeys/cloud-onboarding.ts +++ b/tests/runner-e2e/product-journeys/cloud-onboarding.ts @@ -83,7 +83,8 @@ export async function cloudOnboarding(input: CloudOnboardingInput): Promise { evidence.artifact = artifact; } }); + own: input.own, observeRun: input.observeRun, onArtifact: artifact => { evidence.artifact = artifact; }, + onPreAcceptanceFailure: receipt => { evidence.onboardingPreAcceptance = receipt; } }); await input.verifyExecution(context, [completed.artifact.originatingRunId]); evidence.checks.push({ id: "first-task-complete", passed: true }, { id: "downloadable-artifact-verified", passed: true }, { id: "native-trace-verified", passed: true }); diff --git a/tests/runner-e2e/product-journeys/launch.ts b/tests/runner-e2e/product-journeys/launch.ts index 410bb7725bf..980822013fd 100644 --- a/tests/runner-e2e/product-journeys/launch.ts +++ b/tests/runner-e2e/product-journeys/launch.ts @@ -99,7 +99,7 @@ export async function launchProductJourneys(args: string[], repositoryRoot: stri try { sha = execFileSync("git", ["rev-parse", "HEAD"], { cwd: repositoryRoot, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim(); } catch { /* Packaged harnesses may not include Git metadata. */ } const source = resolveRunnerE2ESource({ sha, ref: null, workflowRunUrl: null }); const definitionHash = createHash("sha256").update(JSON.stringify(productJourneys)); - for (const name of ["catalog.ts", "task-properties.ts", "cloud-onboarding.ts", "onboarding-connect.ts", "onboarding-first-task.ts", "artifact-evidence.ts", "fixtures.ts", "launch.ts", "types.ts", "collection.ts", "local-target.ts"]) definitionHash.update(await readFile(path.join(import.meta.dirname, name))); + for (const name of ["catalog.ts", "task-properties.ts", "cloud-onboarding.ts", "onboarding-connect.ts", "onboarding-first-task.ts", "onboarding-pre-acceptance.ts", "artifact-evidence.ts", "fixtures.ts", "launch.ts", "types.ts", "collection.ts", "local-target.ts"]) definitionHash.update(await readFile(path.join(import.meta.dirname, name))); const suiteDefinitionHash = definitionHash.digest("hex"); const expected = config.journeys.map(id => `product-journeys.${config.id}.${id === "cloud-onboarding" ? config.modelProfileId : "browser"}.${config.executionEnvironment}.${id}`); let reportPersisted = false; diff --git a/tests/runner-e2e/product-journeys/onboarding-first-task.test.ts b/tests/runner-e2e/product-journeys/onboarding-first-task.test.ts index ab452f1901c..2e559aa8584 100644 --- a/tests/runner-e2e/product-journeys/onboarding-first-task.test.ts +++ b/tests/runner-e2e/product-journeys/onboarding-first-task.test.ts @@ -4,6 +4,7 @@ import { createConnectionProof } from "../connection-evidence.js"; import { connectionDelay } from "../connection-target.js"; import { onboardingFirstTask, type OnboardingFirstTaskInput } from "./onboarding-first-task.js"; import { validateOnboardingArtifact } from "./artifact-evidence.js"; +import { validateOnboardingPreAcceptanceEvidence } from "./onboarding-pre-acceptance.js"; vi.mock("../connection-target.js", () => ({ connectionDelay: vi.fn(async () => {}) })); type Row = { id: string; [key: string]: any }; @@ -92,6 +93,90 @@ function fixture(options: Options = {}) { afterEach(() => { vi.restoreAllMocks(); vi.mocked(connectionDelay).mockReset().mockResolvedValue(undefined); }); describe("Cloud canonical onboarding first task", () => { + it.each([ + ["unexpected task set", "unexpected_task_set", (state: Parameters>[1]) => { state.tasks.push({ id: "private-early-task", title: "private task text", status: "todo" }); }, { taskCount: 2 }], + ["attachment", "attachment_present", (state: Parameters>[1]) => { state.attachments.push({ id: "private-attachment", issueId: "parent", originalFilename: "private-file.txt" }); }, { attachmentCount: 1 }], + ["non-planning document", "non_planning_document_present", (state: Parameters>[1]) => { state.documents.push({ id: "private-document", issueId: "parent", key: "private-result-key", body: "private document body" }); }, { documentCount: 1, nonPlanningDocumentCount: 1 }], + ["first task done", "first_task_done", (state: Parameters>[1]) => { state.tasks[0]!.status = "done"; }, { firstTaskStatus: "done", canonicalParentStatus: "done" }], + ] as const)("retains the observed %s violation at the failing acceptance boundary", async (_label, violation, mutate, observed) => { + const f = fixture({ mutate(phase, state) { if (phase === "proposal") mutate(state); } }); + const capture = vi.fn(); + f.input.onPreAcceptanceFailure = capture; + await expect(onboardingFirstTask(f.input)).rejects.toMatchObject({ outcome: "failed", code: "onboarding_work_before_acceptance" }); + expect(capture).toHaveBeenCalledExactlyOnceWith({ version: 1, checkpoint: "awaiting-proposal", violations: [violation], + taskCount: 1, attachmentCount: 0, documentCount: 0, nonPlanningDocumentCount: 0, + firstTaskMatchesCanonicalParent: true, firstTaskStatus: "in_review", canonicalParentStatus: "in_review", ...observed }); + expect(() => validateOnboardingPreAcceptanceEvidence(JSON.parse(JSON.stringify(capture.mock.calls[0]![0])))).not.toThrow(); + const retained = JSON.stringify(capture.mock.calls[0]![0]); + for (const text of ["private-", "parent", "proposal-run", "qa.example.test", "connection", "campaign-1"]) expect(retained).not.toContain(text); + expect(f.events).not.toContain("click:Approve"); + }); + it("retains every simultaneous guard violation from the same failed snapshot", async () => { + const f = fixture({ plan: true, mutate(phase, state) { + if (phase !== "proposal") return; + state.tasks[0]!.status = "done"; + state.tasks.push({ id: "private-early-task" }); + state.attachments.push({ id: "private-attachment", issueId: "parent" }); + state.documents.push({ id: "private-document", issueId: "parent", key: "private-result", body: "private body" }); + } }); + const capture = vi.fn(); + f.input.onPreAcceptanceFailure = capture; + await expect(onboardingFirstTask(f.input)).rejects.toMatchObject({ code: "onboarding_work_before_acceptance" }); + expect(capture).toHaveBeenCalledExactlyOnceWith({ version: 1, checkpoint: "awaiting-proposal", + violations: ["unexpected_task_set", "attachment_present", "non_planning_document_present", "first_task_done"], + taskCount: 2, attachmentCount: 1, documentCount: 2, nonPlanningDocumentCount: 1, + firstTaskMatchesCanonicalParent: true, firstTaskStatus: "done", canonicalParentStatus: "done" }); + expect(f.events).not.toContain("click:Confirm selection"); + }); + it.each(["missing", "replaced", "reordered", "unknown-status"] as const)("records closed task observations when the snapshot is %s", async mode => { + const f = fixture({ mutate(phase, state) { + if (phase !== "proposal") return; + if (mode === "missing") state.tasks = []; + if (mode === "replaced") state.tasks = [{ id: "private-replacement", status: "done" }]; + if (mode === "reordered") state.tasks.unshift({ id: "private-first", status: "done" }); + if (mode === "unknown-status") { state.tasks[0]!.status = "private future status"; state.attachments.push({ id: "private-attachment", issueId: "parent" }); } + } }); + const capture = vi.fn(); + f.input.onPreAcceptanceFailure = capture; + await expect(onboardingFirstTask(f.input)).rejects.toMatchObject({ code: "onboarding_work_before_acceptance" }); + expect(capture).toHaveBeenCalledOnce(); + expect(capture.mock.calls[0]![0]).toMatchObject({ + violations: mode === "unknown-status" ? ["attachment_present"] : mode === "missing" ? ["unexpected_task_set"] : ["unexpected_task_set", "first_task_done"], + taskCount: mode === "missing" ? 0 : mode === "reordered" ? 2 : 1, + firstTaskMatchesCanonicalParent: mode === "unknown-status", + firstTaskStatus: mode === "missing" ? "missing" : mode === "unknown-status" ? "unknown" : "done", + canonicalParentStatus: mode === "reordered" ? "in_review" : mode === "unknown-status" ? "unknown" : "missing", + }); + expect(JSON.stringify(capture.mock.calls[0]![0])).not.toContain("private"); + expect(f.events).not.toContain("click:Approve"); + }); + it("retains an opening-boundary failure before the opening answer dispatches work", async () => { + const f = fixture({ mutate(phase, state) { if (phase === "opening") state.tasks[0]!.status = "done"; } }); + const capture = vi.fn(); + f.input.onPreAcceptanceFailure = capture; + await expect(onboardingFirstTask(f.input)).rejects.toMatchObject({ code: "onboarding_work_before_acceptance" }); + expect(capture).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ checkpoint: "before-opening-answer", violations: ["first_task_done"] })); + expect(f.events).not.toContain("click:Continue"); + }); + it("does not claim the first-slot Done guard when only a later canonical parent is Done", async () => { + const f = fixture({ mutate(phase, state) { + if (phase !== "proposal") return; + state.tasks[0]!.status = "done"; + state.tasks.unshift({ id: "private-first", status: "todo" }); + } }); + const capture = vi.fn(); + f.input.onPreAcceptanceFailure = capture; + await expect(onboardingFirstTask(f.input)).rejects.toMatchObject({ code: "onboarding_work_before_acceptance" }); + expect(capture).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ violations: ["unexpected_task_set"], + firstTaskMatchesCanonicalParent: false, firstTaskStatus: "todo", canonicalParentStatus: "done" })); + }); + it.each([false, true])("does not retain failure snapshots during clean canonical success, plan mode %s", async plan => { + const f = fixture({ plan }); + const capture = vi.fn(); + f.input.onPreAcceptanceFailure = capture; + await expect(onboardingFirstTask(f.input)).resolves.toMatchObject({ onboardingIssueId: "parent", childIssueId: "child" }); + expect(capture).not.toHaveBeenCalled(); + }); it.each(["reload", "download-link"] as const)("retains the independently verified download when later %s fails", async readbackFailure => { const f = fixture({ readbackFailure }); const capture = vi.fn(); diff --git a/tests/runner-e2e/product-journeys/onboarding-first-task.ts b/tests/runner-e2e/product-journeys/onboarding-first-task.ts index ee50b6466ad..474bd615a84 100644 --- a/tests/runner-e2e/product-journeys/onboarding-first-task.ts +++ b/tests/runner-e2e/product-journeys/onboarding-first-task.ts @@ -6,6 +6,7 @@ import { activeRuns, firstTaskCompletionSettled, type Row } from "../first-task- import { isBlockedUnstartedWake, isTerminalUnstartedWake } from "../non-execution-wake.js"; import { JourneyError, type JourneyContext, type OnboardingArtifactEvidence, type TargetSession } from "./types.js"; import { retainOnboardingArtifact } from "./artifact-evidence.js"; +import { preAcceptanceTaskStatus, preAcceptanceViolations, type OnboardingPreAcceptanceEvidence } from "./onboarding-pre-acceptance.js"; export interface OnboardingFirstTaskInput { context: JourneyContext; @@ -18,6 +19,7 @@ export interface OnboardingFirstTaskInput { own: TargetSession["own"]; observeRun(run: Row): Promise; onArtifact?(artifact: OnboardingArtifactEvidence): void; + onPreAcceptanceFailure?(evidence: OnboardingPreAcceptanceEvidence): void; } export interface OnboardingFirstTaskResult { runIds: string[]; @@ -91,9 +93,19 @@ export async function onboardingFirstTask(input: OnboardingFirstTaskInput): Prom fail("onboarding_first_task_failed"); } }; - const assertBeforeAcceptance = (snapshot: Snapshot, parentId: string) => { - if (snapshot.tasks.length !== 1 || snapshot.tasks[0]!.id !== parentId || snapshot.attachments.length || - snapshot.documents.some(document => !isPlanningDocument(document)) || snapshot.tasks[0]!.status === "done") fail("onboarding_work_before_acceptance"); + const assertBeforeAcceptance = (snapshot: Snapshot, parentId: string, checkpoint: OnboardingPreAcceptanceEvidence["checkpoint"]) => { + const observed = { + taskCount: snapshot.tasks.length, attachmentCount: snapshot.attachments.length, + documentCount: snapshot.documents.length, nonPlanningDocumentCount: snapshot.documents.filter(document => !isPlanningDocument(document)).length, + firstTaskMatchesCanonicalParent: snapshot.tasks[0]?.id === parentId, + firstTaskStatus: preAcceptanceTaskStatus(snapshot.tasks[0]), + canonicalParentStatus: preAcceptanceTaskStatus(snapshot.tasks.find(task => task.id === parentId)), + }; + const violations = preAcceptanceViolations(observed); + if (violations.length) { + input.onPreAcceptanceFailure?.({ version: 1, checkpoint, violations, ...observed }); + fail("onboarding_work_before_acceptance"); + } assertRuns(snapshot, [parentId]); }; @@ -105,7 +117,7 @@ export async function onboardingFirstTask(input: OnboardingFirstTaskInput): Prom await input.own({ kind: "issue", name: parent.title, id: parent.id }); snapshot = await load(parent.id); if (seen.size) fail("onboarding_opening_started_model_work"); - assertBeforeAcceptance(snapshot, parent.id); + assertBeforeAcceptance(snapshot, parent.id, "before-opening-answer"); const opening = snapshot.interactions.filter(row => row.kind === "ask_user_questions" && row.status === "pending"); const questions = opening[0]?.payload?.questionSet?.questions ?? opening[0]?.payload?.questions ?? []; const question = questions.find((row: Row) => row.id === "first-task-opening"); @@ -129,7 +141,7 @@ export async function onboardingFirstTask(input: OnboardingFirstTaskInput): Prom let proposal: Row | undefined; while (!proposal) { snapshot = await load(parent.id); - assertBeforeAcceptance(snapshot, parent.id); + assertBeforeAcceptance(snapshot, parent.id, "awaiting-proposal"); const pending = snapshot.interactions.filter(row => row.status === "pending"); const proposals = pending.filter(isProposal); if (proposals.length > 1 || pending.some(row => row.kind === "ask_user_questions" && row.id !== opening[0]!.id)) fail("onboarding_proposal_scope_unclear"); diff --git a/tests/runner-e2e/product-journeys/onboarding-pre-acceptance.test.ts b/tests/runner-e2e/product-journeys/onboarding-pre-acceptance.test.ts new file mode 100644 index 00000000000..84e8ca87aa1 --- /dev/null +++ b/tests/runner-e2e/product-journeys/onboarding-pre-acceptance.test.ts @@ -0,0 +1,105 @@ +import { describe, expect, it } from "vitest"; +import { validateRetainedRunnerResult } from "../result-validation.js"; +import { renderCaseOutcome } from "../case-outcome.js"; +import type { RunnerE2EResult } from "../types.js"; +import type { OnboardingPreAcceptanceEvidence } from "./onboarding-pre-acceptance.js"; + +function result(): RunnerE2EResult { + return { schema: "paperclip.runner-e2e.result/v2", suiteId: "product-journeys", suiteDefinitionHash: "test", + executionId: "product-journeys.qa.runner-codex.local.cloud-onboarding", profileId: "runner-codex", environmentId: "local", caseId: "cloud-onboarding", + provider: "codex", model: "synthetic-model", runtimeMode: "native", status: "failed", error: "onboarding_work_before_acceptance", cleanup: "passed", attempt: 1, + startedAt: "2026-01-01T00:00:00Z", finishedAt: "2026-01-01T00:00:01Z", durationMs: 1000, + productJourney: { outcome: "failed", startingState: "onboarding", phases: [{ phase: "journey", outcome: "failed", code: "onboarding_work_before_acceptance" }], + checks: [], privacy: "private", resourceDisposition: "retained" } }; +} +function preAcceptanceReceipt(): OnboardingPreAcceptanceEvidence { + return { version: 1, checkpoint: "awaiting-proposal", violations: ["attachment_present"], + taskCount: 1, attachmentCount: 1, documentCount: 0, nonPlanningDocumentCount: 0, + firstTaskMatchesCanonicalParent: true, firstTaskStatus: "in_review", canonicalParentStatus: "in_review" }; +} +function passedResult(): RunnerE2EResult { + const retained = result(); + retained.status = "passed"; + delete retained.error; + retained.runIds = ["run-1"]; + Object.assign(retained.productJourney!, { outcome: "passed", campaignId: "campaign", attemptId: "attempt", + phases: [{ phase: "journey", outcome: "passed" }], resourceDisposition: "released", + checks: ["fresh-workspace", "ui-connection-and-native-agent", "first-task-complete", "downloadable-artifact-verified", "native-trace-verified"].map(id => ({ id, passed: true })), + deployment: { kind: "cloud", targetId: "qa", origin: "https://qa.example.test", commit: "a".repeat(40), cloudRelease: "release", runtimeImage: "image", configurationHash: "config" }, + operations: { campaignId: "campaign", attemptId: "attempt", modelCostUsd: null, setupCostUsd: null, requestCount: 1, + feedback: { collection: "complete", complaints: 0, suggestions: 0 }, traceIds: ["trace-1"] } }); + return retained; +} +describe("onboarding pre-acceptance retained evidence", () => { + it("accepts historical results without pre-acceptance evidence and preserves optional receipts in reports", () => { + const retained = result(); + expect(() => validateRetainedRunnerResult(retained)).not.toThrow(); + expect(renderCaseOutcome(retained, true, [])).not.toContain("Onboarding pre-acceptance evidence"); + Object.assign(retained.productJourney!, { onboardingPreAcceptance: preAcceptanceReceipt() }); + const roundTrip = JSON.parse(JSON.stringify(retained)); + expect(() => validateRetainedRunnerResult(roundTrip)).not.toThrow(); + const output = renderCaseOutcome(roundTrip, true, []); + for (const text of ["Onboarding pre-acceptance evidence", "awaiting-proposal", "attachment_present", "First task status", "Canonical parent status", "in_review", "Attachments", "Non-planning documents", "onboarding_work_before_acceptance"]) expect(output).toContain(text); + expect(roundTrip.status).toBe("failed"); + expect(roundTrip.productJourney.outcome).toBe("failed"); + }); + it.each([ + ["a passed result", passedResult], + ["another case", (): RunnerE2EResult => ({ ...result(), caseId: "task-properties", + executionId: "product-journeys.qa.browser.local.task-properties", profileId: "browser", provider: null, model: null, runtimeMode: "none" })], + ["another suite", (): RunnerE2EResult => ({ ...result(), suiteId: "first-task", executionId: "first-task.runner-codex.local.cloud-onboarding" })], + ] as const)("rejects a valid receipt on %s after accepting the same result without it", (_label, makeResult) => { + const retained = makeResult(); + expect(() => validateRetainedRunnerResult(retained)).not.toThrow(); + retained.productJourney!.onboardingPreAcceptance = preAcceptanceReceipt(); + expect(() => validateRetainedRunnerResult(retained)).toThrow("result.productJourney.onboardingPreAcceptance"); + }); + it.each(["passed", "blocked", "unsupported"] as const)("rejects a valid receipt on a %s journey outcome after accepting the same result without it", outcome => { + const retained = result(); + retained.productJourney!.outcome = outcome; + expect(() => validateRetainedRunnerResult(retained)).not.toThrow(); + retained.productJourney!.onboardingPreAcceptance = preAcceptanceReceipt(); + expect(() => validateRetainedRunnerResult(retained)).toThrow("result.productJourney.onboardingPreAcceptance"); + }); + it.each(["onboarding_artifact_evidence_missing", undefined])("rejects a valid receipt for an unrelated or absent failure code (%s)", error => { + const retained = result(); + retained.error = error; + expect(() => validateRetainedRunnerResult(retained)).not.toThrow(); + retained.productJourney!.onboardingPreAcceptance = preAcceptanceReceipt(); + expect(() => validateRetainedRunnerResult(retained)).toThrow("result.productJourney.onboardingPreAcceptance"); + }); + it.each([ + ["raw bodies", (): unknown => ({ ...preAcceptanceReceipt(), body: "private-body-must-not-render" })], + ["task identifiers", (): unknown => ({ ...preAcceptanceReceipt(), taskIds: ["private-task"] })], + ["document keys", (): unknown => ({ ...preAcceptanceReceipt(), documentKeys: ["private-key"] })], + ["unknown version", (): unknown => ({ ...preAcceptanceReceipt(), version: 2 })], + ["unknown checkpoint", (): unknown => ({ ...preAcceptanceReceipt(), checkpoint: "private text" })], + ["unknown violation", (): unknown => ({ ...preAcceptanceReceipt(), violations: ["private text"] })], + ["duplicate violations", (): unknown => ({ ...preAcceptanceReceipt(), violations: ["attachment_present", "attachment_present"] })], + ["empty violations", (): unknown => ({ ...preAcceptanceReceipt(), violations: [] })], + ["inconsistent violations", (): unknown => ({ ...preAcceptanceReceipt(), violations: ["first_task_done"] })], + ["negative counts", (): unknown => ({ ...preAcceptanceReceipt(), attachmentCount: -1 })], + ["fractional counts", (): unknown => ({ ...preAcceptanceReceipt(), taskCount: 1.5 })], + ["unsafe counts", (): unknown => ({ ...preAcceptanceReceipt(), documentCount: Number.MAX_SAFE_INTEGER + 1 })], + ["invalid document counts", (): unknown => ({ ...preAcceptanceReceipt(), nonPlanningDocumentCount: 1 })], + ["raw task status", (): unknown => ({ ...preAcceptanceReceipt(), firstTaskStatus: "private status" })], + ["raw parent status", (): unknown => ({ ...preAcceptanceReceipt(), canonicalParentStatus: "private status" })], + ["nonboolean match", (): unknown => ({ ...preAcceptanceReceipt(), firstTaskMatchesCanonicalParent: "true" })], + ["inconsistent task match", (): unknown => ({ ...preAcceptanceReceipt(), firstTaskMatchesCanonicalParent: false })], + ["inconsistent matched statuses", (): unknown => ({ ...preAcceptanceReceipt(), canonicalParentStatus: "done" })], + ["missing fields", (): unknown => ({ version: 1 })], + ["null", (): unknown => null], + ] as const)("rejects pre-acceptance evidence containing %s", (_label, makeReceipt) => { + const retained = result(); + Object.assign(retained.productJourney!, { onboardingPreAcceptance: makeReceipt() }); + expect(() => validateRetainedRunnerResult(retained)).toThrow("result.productJourney.onboardingPreAcceptance"); + }); + it("renders a fixed fallback for malformed pre-acceptance evidence without exposing its fields", () => { + const retained = result(); + Object.assign(retained.productJourney!, { onboardingPreAcceptance: { ...preAcceptanceReceipt(), body: "private-body-must-not-render" } }); + const output = renderCaseOutcome(retained, false, []); + expect(output).toContain("Onboarding pre-acceptance evidence unavailable: invalid receipt"); + expect(output).not.toContain("private-body-must-not-render"); + expect(output).not.toContain("attachment_present"); + }); +}); diff --git a/tests/runner-e2e/product-journeys/onboarding-pre-acceptance.ts b/tests/runner-e2e/product-journeys/onboarding-pre-acceptance.ts new file mode 100644 index 00000000000..0b0fe7e650e --- /dev/null +++ b/tests/runner-e2e/product-journeys/onboarding-pre-acceptance.ts @@ -0,0 +1,59 @@ +import { ISSUE_STATUSES } from "../../../packages/shared/src/constants.js"; + +const checkpoints = ["before-opening-answer", "awaiting-proposal"] as const; +const violations = ["unexpected_task_set", "attachment_present", "non_planning_document_present", "first_task_done"] as const; +const statuses = [...ISSUE_STATUSES, "missing", "unknown"] as const; + +/** Scalar observations from the first failing pre-acceptance guard; never raw rows. */ +export interface OnboardingPreAcceptanceEvidence { + version: 1; + checkpoint: typeof checkpoints[number]; + violations: Array; + taskCount: number; + attachmentCount: number; + documentCount: number; + nonPlanningDocumentCount: number; + firstTaskMatchesCanonicalParent: boolean; + firstTaskStatus: typeof statuses[number]; + canonicalParentStatus: typeof statuses[number]; +} + +const receiptKeys = { + version: true, checkpoint: true, violations: true, taskCount: true, attachmentCount: true, + documentCount: true, nonPlanningDocumentCount: true, firstTaskMatchesCanonicalParent: true, + firstTaskStatus: true, canonicalParentStatus: true, +} satisfies Record; + +export function preAcceptanceTaskStatus(task: { status?: unknown; [key: string]: unknown } | undefined): OnboardingPreAcceptanceEvidence["firstTaskStatus"] { + if (!task) return "missing"; + return ISSUE_STATUSES.find(status => status === task.status) ?? "unknown"; +} + +export function preAcceptanceViolations(observed: Pick): OnboardingPreAcceptanceEvidence["violations"] { + return [ + ...(observed.taskCount !== 1 || !observed.firstTaskMatchesCanonicalParent ? ["unexpected_task_set" as const] : []), + ...(observed.attachmentCount ? ["attachment_present" as const] : []), + ...(observed.nonPlanningDocumentCount ? ["non_planning_document_present" as const] : []), + ...(observed.firstTaskStatus === "done" ? ["first_task_done" as const] : []), + ]; +} + +/** Closed validation is shared by normalized-result readers and explicit report rendering. */ +export function validateOnboardingPreAcceptanceEvidence(value: unknown): asserts value is OnboardingPreAcceptanceEvidence { + const invalid = (): never => { throw new Error("invalid_onboarding_pre_acceptance_evidence"); }; + const keys = Object.keys(receiptKeys); + if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).length !== keys.length || keys.some(key => !Object.hasOwn(value, key))) invalid(); + const receipt = value as OnboardingPreAcceptanceEvidence; + if (receipt.version !== 1 || !checkpoints.includes(receipt.checkpoint) || + !Array.isArray(receipt.violations) || !receipt.violations.length || receipt.violations.length > violations.length || + receipt.violations.some(code => !violations.includes(code)) || new Set(receipt.violations).size !== receipt.violations.length || + [receipt.taskCount, receipt.attachmentCount, receipt.documentCount, receipt.nonPlanningDocumentCount].some(count => !Number.isSafeInteger(count) || count < 0) || + receipt.nonPlanningDocumentCount > receipt.documentCount || typeof receipt.firstTaskMatchesCanonicalParent !== "boolean" || + !statuses.includes(receipt.firstTaskStatus) || !statuses.includes(receipt.canonicalParentStatus)) invalid(); + if (receipt.taskCount === 0 && (receipt.firstTaskStatus !== "missing" || receipt.canonicalParentStatus !== "missing" || receipt.firstTaskMatchesCanonicalParent) || + receipt.taskCount > 0 && receipt.firstTaskStatus === "missing" || + receipt.firstTaskMatchesCanonicalParent && receipt.firstTaskStatus !== receipt.canonicalParentStatus || + receipt.taskCount === 1 && !receipt.firstTaskMatchesCanonicalParent && receipt.canonicalParentStatus !== "missing" || + preAcceptanceViolations(receipt).join(",") !== receipt.violations.join(",")) invalid(); +} diff --git a/tests/runner-e2e/product-journeys/types.ts b/tests/runner-e2e/product-journeys/types.ts index da0b054a606..00bdeb5c164 100644 --- a/tests/runner-e2e/product-journeys/types.ts +++ b/tests/runner-e2e/product-journeys/types.ts @@ -29,6 +29,7 @@ export interface JourneyEvidence { operations?: JourneyOperations; artifact?: OnboardingArtifactEvidence; onboardingConnect?: import("./onboarding-connect.js").OnboardingConnectEvidence; + onboardingPreAcceptance?: import("./onboarding-pre-acceptance.js").OnboardingPreAcceptanceEvidence; } export interface OnboardingArtifactEvidence { schema: "paperclip.product-journey.artifact/v1"; diff --git a/tests/runner-e2e/result-validation.ts b/tests/runner-e2e/result-validation.ts index 56f38ed0bec..3d6beece13e 100644 --- a/tests/runner-e2e/result-validation.ts +++ b/tests/runner-e2e/result-validation.ts @@ -6,6 +6,7 @@ import type { RunnerE2ECampaignAccounting, RunnerE2EResult } from "./types.js"; import { DEPENDABLE_CELLS } from "./dependable-qualification.js"; import { validateOnboardingConnectEvidence } from "./product-journeys/onboarding-connect.js"; import { validateOnboardingArtifact } from "./product-journeys/artifact-evidence.js"; +import { validateOnboardingPreAcceptanceEvidence } from "./product-journeys/onboarding-pre-acceptance.js"; type Rule = (value: unknown, at: string) => void; const invalid = (at: string): never => { @@ -346,6 +347,7 @@ const fields = { targetId: optional(string), campaignId: optional(string), attemptId: optional(string), onboardingConnect: optional((value, at) => { try { validateOnboardingConnectEvidence(value); } catch { invalid(at); } }), + onboardingPreAcceptance: optional((value, at) => { try { validateOnboardingPreAcceptanceEvidence(value); } catch { invalid(at); } }), artifact: optional((value, at) => { try { validateOnboardingArtifact(value); } catch { invalid(at); } }), operations: optional(closedShape({ campaignId: string, attemptId: string, modelCostUsd: nullable(number), setupCostUsd: nullable(number), requestCount: integer, feedback: closedShape({ collection: oneOf("complete", "partial", "unavailable"), complaints: integer, suggestions: integer }), traceIds: array(string) })), @@ -404,6 +406,9 @@ export function validateRetainedRunnerResult( result(value, "result"); const checked = value as RunnerE2EResult; const artifact = checked.productJourney?.artifact; + if (checked.productJourney?.onboardingPreAcceptance && (checked.suiteId !== "product-journeys" || checked.caseId !== "cloud-onboarding" || + checked.status !== "failed" || checked.productJourney.outcome !== "failed" || + checked.error !== "onboarding_work_before_acceptance")) invalid("result.productJourney.onboardingPreAcceptance"); if (artifact && (checked.suiteId !== "product-journeys" || checked.caseId !== "cloud-onboarding" || checked.runIds && !checked.runIds.includes(artifact.originatingRunId))) invalid("result.productJourney.artifact"); if (checked.workerTiming !== undefined) { diff --git a/tests/runner-e2e/support.test.ts b/tests/runner-e2e/support.test.ts index 7de63dd9068..07c22457fe6 100644 --- a/tests/runner-e2e/support.test.ts +++ b/tests/runner-e2e/support.test.ts @@ -175,6 +175,7 @@ describe("runner E2E local binary resolution", () => { runnerBinary, undefined, "linux", + "x64", ), ).toBe(runnerBinary); expect( @@ -183,6 +184,7 @@ describe("runner E2E local binary resolution", () => { runnerBinary, undefined, "linux", + "x64", ), ).toBeUndefined(); expect( @@ -201,7 +203,14 @@ describe("runner E2E local binary resolution", () => { "darwin", ), ).toBe("/cross-compiled/paperclip-runnerd"); + expect(resolvePaperclipRemoteRunnerBinaryForHarness( + [remoteNativeExecution], runnerBinary, undefined, "linux", "arm64", + )).toBeUndefined(); + expect(resolvePaperclipRemoteRunnerBinaryForHarness( + [remoteNativeExecution], runnerBinary, "/cross-compiled/linux-x64-runnerd", "linux", "arm64", + )).toBe("/cross-compiled/linux-x64-runnerd"); }); + }); describe("runner E2E provider environment", () => {