|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { createHash } from "node:crypto"; |
| 3 | +import type { TestContext } from "node:test"; |
| 4 | +import { |
| 5 | + OpenShellGateway, |
| 6 | + openShellProviderName, |
| 7 | +} from "../../apps/controller/src/backends/openshell.ts"; |
| 8 | +import { OpenShellCredentialGatewayDriver } from "../../apps/controller/src/drivers/credential-gateway/openshell.ts"; |
| 9 | +import type { |
| 10 | + KubernetesComputeDriver, |
| 11 | + KubernetesComputeDriverOptions, |
| 12 | +} from "../../apps/controller/src/drivers/compute/kubernetes/index.ts"; |
| 13 | +import type { OpenShellSandboxCreateRequest } from "../../apps/controller/src/drivers/sandbox/openshell-gateway-client.ts"; |
| 14 | +import { asRecord } from "../../packages/utils/src/index.ts"; |
| 15 | +import type { |
| 16 | + AgentRevision, |
| 17 | + ComputeRevisionContext, |
| 18 | + CredentialGatewayDriver, |
| 19 | + CredentialSource, |
| 20 | + CredentialSourceLoginMode, |
| 21 | +} from "../../packages/contracts/src/index.ts"; |
| 22 | +import { |
| 23 | + WORKSPACE_DEFAULTS, |
| 24 | + WORKSPACE_DEFAULTS_ID, |
| 25 | +} from "../../packages/contracts/src/workspace-defaults.mjs"; |
| 26 | +import { inspectOpenShellCodexCommand } from "../fixtures/openshell-codex-command.ts"; |
| 27 | +import { |
| 28 | + captureOpenShellHarness, |
| 29 | + providerEndpointFixture, |
| 30 | + tlsPinnedWorkspaceSetupCode, |
| 31 | +} from "../fixtures/kubernetes-provider-endpoint.mjs"; |
| 32 | +import { admitRevisionHarness as admitRevisionHarnessFixture } from "../helpers/kubernetes-routed-scenarios.mjs"; |
| 33 | +import { buildSandboxInstallation } from "./sandbox-driver-installation-fixture.ts"; |
| 34 | + |
| 35 | +const admitRevisionHarness: (driver: KubernetesComputeDriver, revision: AgentRevision) => void = |
| 36 | + admitRevisionHarnessFixture; |
| 37 | + |
| 38 | +type HarnessCapture = Omit<ReturnType<typeof captureOpenShellHarness>, "request"> & { |
| 39 | + readonly request: unknown; |
| 40 | +}; |
| 41 | + |
| 42 | +type Mutable<T> = { -readonly [Key in keyof T]: T[Key] }; |
| 43 | + |
| 44 | +type CommandFixtureRevision = Mutable<Omit<AgentRevision, "compute">> & { |
| 45 | + compute: Mutable<AgentRevision["compute"]>; |
| 46 | +}; |
| 47 | + |
| 48 | +function capturedRequest(value: unknown): Pick<OpenShellSandboxCreateRequest, "name" | "spec"> { |
| 49 | + const request = asRecord(value); |
| 50 | + assert.ok(request); |
| 51 | + assert.ok(typeof request.name === "string"); |
| 52 | + const spec = asRecord(request.spec); |
| 53 | + assert.ok(spec); |
| 54 | + return { name: request.name, spec }; |
| 55 | +} |
| 56 | + |
| 57 | +function commandArguments(value: unknown): string[] { |
| 58 | + assert.ok(Array.isArray(value)); |
| 59 | + const args: unknown[] = value; |
| 60 | + assert.ok(args.every((arg): arg is string => typeof arg === "string")); |
| 61 | + return args; |
| 62 | +} |
| 63 | + |
| 64 | +// The command budget applies after Compute adds the workspace verifier and |
| 65 | +// OpenShell adds links, setup wait and any account bootstrap to CreateSandbox. |
| 66 | +export async function verifyAdmittedComputeCommands(t: TestContext): Promise<void> { |
| 67 | + const installation = buildSandboxInstallation(); |
| 68 | + const [backend] = installation.backend; |
| 69 | + assert.ok(backend); |
| 70 | + const gateway = installation.drivers.credential_gateway; |
| 71 | + const catalogDriver = new OpenShellCredentialGatewayDriver(gateway.configuration, { |
| 72 | + id: gateway.id, |
| 73 | + backend: { |
| 74 | + id: backend.id, |
| 75 | + // OpenShell advertises Codex OAuth only when its Backend provides credential refresh. |
| 76 | + drivers: { ...backend.drivers, credential_refresh: "openshell-refresh" }, |
| 77 | + client: new OpenShellGateway(backend.configuration), |
| 78 | + }, |
| 79 | + }); |
| 80 | + const catalog = await catalogDriver.listSourceTypes({ signal: t.signal }); |
| 81 | + const cases: ReadonlyArray<{ |
| 82 | + loginMode: Extract<CredentialSourceLoginMode, "codex_pat" | "chatgptAuthTokens">; |
| 83 | + hasSetup: boolean; |
| 84 | + admitted: boolean; |
| 85 | + }> = [ |
| 86 | + { loginMode: "codex_pat", hasSetup: true, admitted: true }, |
| 87 | + { loginMode: "codex_pat", hasSetup: false, admitted: true }, |
| 88 | + { loginMode: "codex_pat", hasSetup: true, admitted: false }, |
| 89 | + { loginMode: "codex_pat", hasSetup: false, admitted: false }, |
| 90 | + { loginMode: "chatgptAuthTokens", hasSetup: true, admitted: true }, |
| 91 | + ]; |
| 92 | + for (const { loginMode, hasSetup, admitted } of cases) { |
| 93 | + await t.test(`${loginMode}, setup ${hasSetup}, new admission ${admitted}`, async (t) => { |
| 94 | + const options: NonNullable<Parameters<typeof providerEndpointFixture>[0]> & { |
| 95 | + servicePrincipalCredentials: KubernetesComputeDriverOptions["servicePrincipalCredentials"]; |
| 96 | + revisionOverrides: Pick<AgentRevision, "id" | "agentId" | "servicePrincipalId">; |
| 97 | + } = { |
| 98 | + servicePrincipalCredentials: { mode: "disabled" }, |
| 99 | + revisionOverrides: { |
| 100 | + id: "rev_2c9250cd-6aa2-4493-a73e-2422ec6c1537", |
| 101 | + agentId: "agt_9bee809d-62ec-4d41-b68d-25ead74b3a4d", |
| 102 | + servicePrincipalId: "spn_02aa5adc-0b5c-417d-b8d4-1a1de5157809", |
| 103 | + }, |
| 104 | + }; |
| 105 | + const fixture = providerEndpointFixture(options); |
| 106 | + const { driver: compute, sandbox } = fixture; |
| 107 | + const revision: CommandFixtureRevision = fixture.revision; |
| 108 | + const capture: HarnessCapture = captureOpenShellHarness(fixture, sandbox); |
| 109 | + const source: CredentialSource = { |
| 110 | + id: "cs_324fc26c-59b3-43e0-b057-53fce26a2f19", |
| 111 | + namespaceId: revision.namespaceId, |
| 112 | + type: loginMode === "codex_pat" ? "codex-pat" : "codex-oauth", |
| 113 | + name: "fixture source", |
| 114 | + config: {}, |
| 115 | + secrets: {}, |
| 116 | + ownerCapability: "credential_gateway", |
| 117 | + driverId: catalogDriver.id, |
| 118 | + state: "ready", |
| 119 | + createdAt: revision.createdAt, |
| 120 | + }; |
| 121 | + const sourceType = catalog.find((entry) => entry.type === source.type); |
| 122 | + assert.ok(sourceType); |
| 123 | + const ref = openShellProviderName(source.id); |
| 124 | + revision.harnessAuth = { |
| 125 | + method: "credential_source", |
| 126 | + sourceId: source.id, |
| 127 | + sourceType: source.type, |
| 128 | + credentialGatewayId: source.driverId, |
| 129 | + loginMode, |
| 130 | + }; |
| 131 | + const credentialGateway: Pick< |
| 132 | + CredentialGatewayDriver, |
| 133 | + "id" | "capability" | "implementation" | "listSourceTypes" | "attachForRevision" |
| 134 | + > = { |
| 135 | + id: source.driverId, |
| 136 | + capability: "credential_gateway", |
| 137 | + implementation: "openshell", |
| 138 | + listSourceTypes: async () => [sourceType], |
| 139 | + attachForRevision: async () => [ |
| 140 | + { |
| 141 | + sourceId: source.id, |
| 142 | + ref, |
| 143 | + ...(loginMode === "chatgptAuthTokens" && { |
| 144 | + externalChatgptAuth: { |
| 145 | + accessTokenPlaceholder: `openshell:resolve:env:s${"1".repeat(64)}_CODEX_ACCESS_TOKEN`, |
| 146 | + accountId: "fixture-account", |
| 147 | + planType: "business", |
| 148 | + }, |
| 149 | + }), |
| 150 | + }, |
| 151 | + ], |
| 152 | + }; |
| 153 | + assert.ok(Object.hasOwn(compute, "credentialGatewayDriver")); |
| 154 | + Object.assign(compute, { credentialGatewayDriver: credentialGateway }); |
| 155 | + revision.configuration = capture.openshell.configureAgent( |
| 156 | + revision.configuration, |
| 157 | + revision.harness, |
| 158 | + ); |
| 159 | + admitRevisionHarness(compute, revision); |
| 160 | + if (!admitted) { |
| 161 | + // Pre-upgrade PAT revisions did not save a Compute admission. |
| 162 | + delete revision.compute.admission; |
| 163 | + } |
| 164 | + const workspaceSetup = { |
| 165 | + id: "workspace-setup-1f8d6823-aa87-4cba-ae3e-62352e892d12", |
| 166 | + namespaceId: revision.namespaceId, |
| 167 | + agentId: revision.agentId, |
| 168 | + defaultsId: WORKSPACE_DEFAULTS_ID, |
| 169 | + completed: false, |
| 170 | + files: { ...WORKSPACE_DEFAULTS, "AGENTS.md": "Private initial Agent instructions\n" }, |
| 171 | + }; |
| 172 | + const context: Pick<ComputeRevisionContext, "harnessAuth" | "workspaceSetup"> = { |
| 173 | + harnessAuth: { ...revision.harnessAuth, source }, |
| 174 | + ...(hasSetup && { workspaceSetup }), |
| 175 | + }; |
| 176 | + const enrollment = compute["nodeEnrollment"]; |
| 177 | + assert.ok(enrollment); |
| 178 | + const createSetup = enrollment.createSetup; |
| 179 | + enrollment.createSetup = async (url: string, nodeUrl: string, signal: AbortSignal) => { |
| 180 | + const setup = await createSetup(url, nodeUrl, signal); |
| 181 | + return { ...setup, setupCode: tlsPinnedWorkspaceSetupCode(nodeUrl, setup.expiresAtMs) }; |
| 182 | + }; |
| 183 | + |
| 184 | + await fixture.prepare(undefined, context); |
| 185 | + fixture.setGatewayServing(); |
| 186 | + // Let the real Compute enrollment write the Agent-owned Secret. |
| 187 | + const provision = (input = context) => |
| 188 | + assert.rejects(fixture.prepare(undefined, input), (error) => error === capture.provisioned); |
| 189 | + await provision(); |
| 190 | + const { name, spec } = capturedRequest(capture.request); |
| 191 | + const command = commandArguments(spec.command); |
| 192 | + const { loader, supervisor, codex } = inspectOpenShellCodexCommand(t, command, { |
| 193 | + compact: admitted && (hasSetup || loginMode === "codex_pat"), |
| 194 | + current: admitted, |
| 195 | + }); |
| 196 | + if (!admitted) { |
| 197 | + // Stock 0.1.6 Compute and OpenShell at 4d68b3b7 render these exact commands. |
| 198 | + // Revisions with initial setup embed that release's workspace verifier. |
| 199 | + assert.equal( |
| 200 | + createHash("sha256").update(JSON.stringify(command)).digest("hex"), |
| 201 | + hasSetup |
| 202 | + ? "b491545115bb907a872b0fe34b56cfc850dd684cd495ec280bb86e4a3f1cf3bc" |
| 203 | + : "a0d8025d10aeb7dbf26ca8271ad2cecb0eacb3ef1cd0bfafbf22ce251d9bd1fa", |
| 204 | + ); |
| 205 | + } |
| 206 | + const startup = loader + supervisor; |
| 207 | + assert.equal(startup.includes(".oce-workspace-setup.json"), hasSetup); |
| 208 | + if (hasSetup) { |
| 209 | + assert.match(supervisor, new RegExp(workspaceSetup.id)); |
| 210 | + assert.doesNotMatch(supervisor, /Private initial Agent instructions/); |
| 211 | + } |
| 212 | + assert.equal( |
| 213 | + codex.includes("unsupported_l7_protocol"), |
| 214 | + admitted && loginMode === "codex_pat", |
| 215 | + ); |
| 216 | + const environment = asRecord(spec.environment); |
| 217 | + assert.ok(environment); |
| 218 | + assert.equal(environment.CODEX_LOGIN_MODE, loginMode); |
| 219 | + assert.equal( |
| 220 | + environment.OCE_CODEX_CREDENTIAL_SOURCE, |
| 221 | + loginMode === "codex_pat" ? "1" : undefined, |
| 222 | + ); |
| 223 | + assert.equal( |
| 224 | + startup.includes("OCE_CODEX_CHATGPT_ACCOUNT"), |
| 225 | + loginMode === "chatgptAuthTokens", |
| 226 | + ); |
| 227 | + |
| 228 | + if (!admitted && hasSetup) { |
| 229 | + // Completing setup keeps the legacy Sandbox adoptable, while even one |
| 230 | + // byte of command drift in its stored spec must be refused. |
| 231 | + await provision({ |
| 232 | + ...context, |
| 233 | + workspaceSetup: { ...workspaceSetup, files: {}, completed: true }, |
| 234 | + }); |
| 235 | + const stored = capture.gateway.sandboxes.get(name); |
| 236 | + assert.ok(stored?.spec); |
| 237 | + const storedCommand = commandArguments(stored.spec.command); |
| 238 | + const loaderArgument = storedCommand[7]; |
| 239 | + assert.equal(typeof loaderArgument, "string"); |
| 240 | + storedCommand[7] = loaderArgument + " "; |
| 241 | + await assert.rejects( |
| 242 | + fixture.prepare(undefined, context), |
| 243 | + /without exact AgentRevision ownership and content/, |
| 244 | + ); |
| 245 | + } |
| 246 | + }); |
| 247 | + } |
| 248 | +} |
0 commit comments