diff --git a/packages/plugins/sdk/src/environment-tasks.ts b/packages/plugins/sdk/src/environment-tasks.ts index 0e99adb1512..e1f22476d4d 100644 --- a/packages/plugins/sdk/src/environment-tasks.ts +++ b/packages/plugins/sdk/src/environment-tasks.ts @@ -186,7 +186,8 @@ export const environmentTaskResultSchema = z.discriminatedUnion("kind", [ sha256: z.string().regex(/^[a-f0-9]{64}$/), }).strict(), z.object({ kind: z.literal("state"), taskId: providerId, state: z.record(z.string(), z.unknown()) }).strict(), - z.object({ kind: z.literal("prepared"), taskId: providerId, launch: preparedLaunch, projectAuthCheckAtSubmit: z.literal(true).optional() }).strict(), + z.object({ kind: z.literal("prepared"), taskId: providerId, launch: preparedLaunch, projectAuthCheckAtSubmit: z.literal(true).optional(), + projectAuthChecked: z.literal(true).optional() }).strict(), z.object({ kind: z.literal("credentials"), taskId: providerId, /** Null proves that cancellation fenced admission before the task could start. It must not mean an unavailable export. */ credential: environmentTaskCredentialSchema.nullable() }).strict(), z.object({ kind: z.literal("description"), taskId: providerId, diff --git a/server/src/__tests__/environment-task-runtime.test.ts b/server/src/__tests__/environment-task-runtime.test.ts index 35df7431481..968b51c38ac 100644 --- a/server/src/__tests__/environment-task-runtime.test.ts +++ b/server/src/__tests__/environment-task-runtime.test.ts @@ -344,7 +344,7 @@ describe("plugin-provided Runner execution", () => { await expect(executeEnvironmentTask(database(row(), []).db, denied as never, { companyId: "company", leaseId, operation })).rejects.toThrow("project unavailable"); expect(denied.call).not.toHaveBeenCalled(); }); - it.each([false, true])("prepares scoped configuration with submit-time auth %s", async atSubmit => { + it.each([[false, false], [true, false], [false, true], [true, true]])("prepares scoped configuration with submit-time auth %s and in-prepare auth %s", async (atSubmit, checked) => { const operation = { kind: "prepare" as const, projectIds: [projectId], requirements: { schema: "paperclip.runner.launch-requirements.v1" as const, identity: { runnerInstanceId: "runner", environmentLeaseId: leaseId, runId: "run", @@ -353,7 +353,8 @@ describe("plugin-provided Runner execution", () => { settings: {}, approvalPolicy: "on-request" as const, readOnly: false, preparedConversation: false, includeCollaborationModeInstructions: true, } }; - const result = { kind: "prepared", taskId: "attempt-1", ...(atSubmit ? {projectAuthCheckAtSubmit: true} : {}), launch: { configurationId: "configuration-1", + const result = { kind: "prepared", taskId: "attempt-1", ...(atSubmit ? {projectAuthCheckAtSubmit: true} : {}), + ...(checked ? {projectAuthChecked: true} : {}), launch: { configurationId: "configuration-1", requirementsDigest: `sha256:${"a".repeat(64)}`, provider: { kind: "codex" } } }; const workers = worker(result), store = database(); await expect(executeEnvironmentTask(store.db, workers as never, { companyId: "company", leaseId, operation })) @@ -361,9 +362,10 @@ describe("plugin-provided Runner execution", () => { expect(workers.call).toHaveBeenCalledWith("original", "environmentTask", expect.objectContaining({ companyId: "company", runId: "run", agentId: "agent", projectIds: [projectId], operation, }), 15_000); - expect(store.sets).toHaveLength(atSubmit ? 2 : 1); - if (atSubmit) expect(new PgDialect().sqlToQuery(store.sets[1].metadata as never).params).toContain(JSON.stringify({ - runnerTaskProjectAuthValidation: { configurationId: "configuration-1", provider: "openai", projectIds: [projectId], atSubmit: true }, + expect(store.sets).toHaveLength(atSubmit || checked ? 2 : 1); + if (atSubmit || checked) expect(new PgDialect().sqlToQuery(store.sets[1].metadata as never).params).toContain(JSON.stringify({ + runnerTaskProjectAuthValidation: { configurationId: "configuration-1", provider: "openai", projectIds: [projectId], + ...(checked ? {} : { atSubmit: true }) }, })); const denied = worker(result); const inactive = row(); inactive.lease.status = "released"; diff --git a/server/src/services/environment-task-runtime.ts b/server/src/services/environment-task-runtime.ts index f3582b55424..5e3b9abeb4a 100644 --- a/server/src/services/environment-task-runtime.ts +++ b/server/src/services/environment-task-runtime.ts @@ -534,12 +534,13 @@ export async function executeEnvironmentTask(db: Db, workers: Pick>'runnerTaskRetiring', 'false') != 'true'`)).returning({ id: environmentLeases.id }); if (saved.length !== 1) throw new Error("Plugin Runner project validation lease expired"); diff --git a/server/src/services/native-runtime/plugin-runner-task.test.ts b/server/src/services/native-runtime/plugin-runner-task.test.ts index ac0032bc46a..873c958eb32 100644 --- a/server/src/services/native-runtime/plugin-runner-task.test.ts +++ b/server/src/services/native-runtime/plugin-runner-task.test.ts @@ -831,6 +831,33 @@ it.each(["codex", "claude"])("reuses a warm %s process across run authorities wi expect(nextTask.mock.calls.filter(([op])=>op.kind==="stop")).toHaveLength(1); }); +it.each([ + ["checked inside prepare", true, ["prepare"]], + ["left to Core", false, ["prepare", "check_project_auth"]], +] as const)("a warm attach whose project authentication is %s makes only the round trips it needs", async (_label, checked, kinds) => { + const {task} = provider(); + ingress.connect.mockReturnValue({ready:Promise.resolve(),close:async()=>{}}); + const input = {taskId:"task",task,projectIds:[],harness:"codex",projectAuthCheckVersion:1 as const,warmTaskVersion:1 as const, + accountIdentity:"account",credentialIdentity:"revision",pollMs:1}; + const owner = createPluginRunnerTaskOwner(input); + await prepare(owner, launchRequirements("codex")); + const registration = await owner.registration({} as never,"run"); + const spec = launchSpec(); spec.nativeRunner!.lifecyclePolicy = {mode:"warm",idleTimeoutMs:300000}; + const handle = owner.launcher(spec); registration.activate(); await registration.ready(); + const originalTask=task.getMockImplementation()!; + const nextTask=vi.fn(async op=>{ + const result=await originalTask(op) as Record; + return op.kind === "prepare" && checked ? {...result,projectAuthChecked:true} : result; + }); + owner.rebind({...input,task:nextTask}); + const nextRegistration=await owner.registration({} as never,"next-run"); + const nextIdentity={...requirements.identity,runId:"next-run",turnId:"next-turn",itemId:"next-item"}; + await owner.driver.prepareLaunch!({...launchRequirements("codex"),identity:nextIdentity}); + expect(nextTask.mock.calls.map(([op])=>op.kind)).toEqual(kinds); + await registration.release(); nextRegistration.activate(); await nextRegistration.ready(); + await nextRegistration.release(); await nextRegistration.checkpoint("settled"); await handle.completion; +}); + it.each(["codex", "claude"])("adopts a verified retained %s task without submitting a replacement", async harness => { const {task}=provider(); ingress.connect.mockReturnValue({ready:Promise.resolve(),close:async()=>{}}); diff --git a/server/src/services/native-runtime/plugin-runner-task.ts b/server/src/services/native-runtime/plugin-runner-task.ts index c63dddf2d36..ba566839390 100644 --- a/server/src/services/native-runtime/plugin-runner-task.ts +++ b/server/src/services/native-runtime/plugin-runner-task.ts @@ -155,7 +155,7 @@ export function createPluginRunnerTaskOwner(input: { if (released || stopRequested) throw new Error("Plugin Runner preparation cancelled"); if (result.kind !== "prepared") throw new Error("Plugin Runner preparation unavailable"); if (input.projectAuthCheckVersion !== 1) throw new Error("Plugin Runner managed project authentication validation unavailable"); - if (launched || !result.projectAuthCheckAtSubmit) await checkProjectAuth(result.launch.configurationId); + if (!result.projectAuthChecked && (launched || !result.projectAuthCheckAtSubmit)) await checkProjectAuth(result.launch.configurationId); preparedLaunchId = result.launch.configurationId; preparedIdentity = requirements.identity; return result.launch; diff --git a/tests/runner-e2e/source-sdk-public-inputs.json b/tests/runner-e2e/source-sdk-public-inputs.json index cc252e3f1b7..26c3e22d86e 100644 --- a/tests/runner-e2e/source-sdk-public-inputs.json +++ b/tests/runner-e2e/source-sdk-public-inputs.json @@ -13,7 +13,7 @@ "packages/plugins/sdk/src/environment-stop-error.ts": "b0e03f8aa2b2170ba095da5c7138050fced3f3cb", "packages/plugins/sdk/src/environment-sync-error.ts": "7d6a51a95da49a7da4525aebbab6382dc5c44ae8", "packages/plugins/sdk/src/environment-task-error.ts": "98aae8a06743b38884b68cb9a3ec44c57b34c0e4", - "packages/plugins/sdk/src/environment-tasks.ts": "0e99adb15123cee99c979642a5d93efa48c46d09", + "packages/plugins/sdk/src/environment-tasks.ts": "e1f22476d4d73502bd4dc56afa3f863f3d1a275a", "packages/plugins/sdk/src/host-client-factory.ts": "e0301c390ea13f2340b669383f07af2455e48c8a", "packages/plugins/sdk/src/idle-drain.test.ts": "cc59f7881e7d46c89395a1d93d74f6c6b9b0a64a", "packages/plugins/sdk/src/idle-drain.ts": "ae6e4c6be4657a3d541396922b3bc551b8db557f",