From 6d187c8da70653fec53b9590c0594a413cbeadcb Mon Sep 17 00:00:00 2001 From: "shumeiko.it" Date: Sun, 1 Nov 2026 11:13:35 +0100 Subject: [PATCH] test: remove the Windows external-agent lane The lane drove real Claude Code, Codex, Hermes and OpenClaw installs through Waggle Room runs, the coding-agent hooks and the hive-mind CLI, none of which is in the community edition. - scripts/test-windows-external-agents.ps1 and scripts/test-windows-official-auth-canaries.ps1 (the runners and their receipt sealing), scripts/verify-codex-tool-denial.mjs - tests/e2e/launcher-real-hook-lifecycle.spec.ts and tests/e2e/launcher-real-tool-lifecycle.spec.ts (Windows-only, skipped unless the runner set them up) - tests/integration/external-agent-collaboration.live.test.ts - the tauri-config tests that pinned those scripts --- packages/server/tests/tauri-config.test.ts | 1398 -------- scripts/test-windows-external-agents.ps1 | 1231 ------- .../test-windows-official-auth-canaries.ps1 | 2239 ------------- scripts/verify-codex-tool-denial.mjs | 2882 ----------------- .../e2e/launcher-real-hook-lifecycle.spec.ts | 221 -- .../e2e/launcher-real-tool-lifecycle.spec.ts | 357 -- .../external-agent-collaboration.live.test.ts | 344 -- 7 files changed, 8672 deletions(-) delete mode 100644 scripts/test-windows-external-agents.ps1 delete mode 100644 scripts/test-windows-official-auth-canaries.ps1 delete mode 100644 scripts/verify-codex-tool-denial.mjs delete mode 100644 tests/e2e/launcher-real-hook-lifecycle.spec.ts delete mode 100644 tests/e2e/launcher-real-tool-lifecycle.spec.ts delete mode 100644 tests/integration/external-agent-collaboration.live.test.ts diff --git a/packages/server/tests/tauri-config.test.ts b/packages/server/tests/tauri-config.test.ts index 51031ae..dd30465 100644 --- a/packages/server/tests/tauri-config.test.ts +++ b/packages/server/tests/tauri-config.test.ts @@ -1993,1404 +1993,6 @@ describe('CI/CD Configuration', () => { expect(includes.filter((candidate) => candidate.toLowerCase() === hookPath.toLowerCase())) .toHaveLength(1); }); - - it('guards the real Windows external-agent lane from ambient authority and server reuse', () => { - const script = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ); - const hookSpec = fs.readFileSync( - path.join(ROOT, 'tests', 'e2e', 'launcher-real-hook-lifecycle.spec.ts'), - 'utf-8', - ); - const toolSpec = fs.readFileSync( - path.join(ROOT, 'tests', 'e2e', 'launcher-real-tool-lifecycle.spec.ts'), - 'utf-8', - ); - const playwrightConfig = fs.readFileSync( - path.join(ROOT, 'playwright.config.ts'), - 'utf-8', - ); - - expect(script).toContain('[string[]]$HostIds = @()'); - expect(script).toContain("[string]$ReceiptDir = ''"); - expect(script).toContain("[string]$RunnerNode = ''"); - expect(script).toContain("'WAGGLE_E2E_HOST_IDS'"); - expect(script).toContain( - "Set-ProcessEnvironment -Name 'WAGGLE_E2E_HOST_IDS' -Value $requestedHostIds", - ); - expect(script).toContain('HostIds cannot contain empty values.'); - expect(script).toContain('$emptyHostIds.Count -gt 0'); - expect(script).toContain('HostIds cannot contain duplicate values:'); - expect(script).toContain("'--retries=0'"); - expect(script).toContain('$playwrightOutput = Join-Path $runRoot "playwright-$ReceiptName"'); - expect(script).toContain('& $script:runnerNodePath $script:playwrightCli @playwrightArgs'); - expect(script).toContain("'PLAYWRIGHT_JSON_OUTPUT_FILE'"); - expect(script).toContain("'--reporter=list,json'"); - expect(script).toContain('"$ReceiptName-report.json"'); - expect(playwrightConfig).toContain( - "url: new URL('/health', e2eBaseURL).toString()", - ); - expect(playwrightConfig).not.toContain('port: e2ePort'); - for (const spec of [hookSpec, toolSpec]) { - expect(spec).toContain('WAGGLE_E2E_HOST_IDS'); - expect(spec).toContain('Unknown WAGGLE_E2E_HOST_IDS'); - expect(spec).toContain('Invalid WAGGLE_E2E_HOST_IDS: empty host ID.'); - expect(spec).toContain('Duplicate WAGGLE_E2E_HOST_IDS:'); - } - expect(toolSpec).toContain('every explicitly requested host must be installed and healthy'); - expect(toolSpec).toContain( - "results.filter(result => result.status === 'unavailable').map(result => result.id)", - ); - const strictToolAssertionIndex = toolSpec.indexOf( - 'every explicitly requested host must be installed and healthy', - ); - const toolFinallyIndex = toolSpec.indexOf('} finally {', strictToolAssertionIndex); - const toolReceiptIndex = toolSpec.indexOf( - "testInfo.attach('windows-external-tool-route-summary'", - toolFinallyIndex, - ); - const receiptCleanupFinallyIndex = toolSpec.indexOf( - '} finally {', - toolReceiptIndex, - ); - const toolKillIndex = toolSpec.indexOf( - "request.post('/api/tools/kill'", - receiptCleanupFinallyIndex, - ); - expect(toolFinallyIndex).toBeGreaterThan(strictToolAssertionIndex); - expect(toolReceiptIndex).toBeGreaterThan(toolFinallyIndex); - expect(receiptCleanupFinallyIndex).toBeGreaterThan(toolReceiptIndex); - expect(toolKillIndex).toBeGreaterThan(receiptCleanupFinallyIndex); - - for (const name of [ - 'CLAUDE_CODE_OAUTH_TOKEN', 'OPENAI_ACCESS_TOKEN', 'GITHUB_TOKEN', - 'STRIPE_SECRET_KEY', 'DATABASE_URL', 'SSH_AUTH_SOCK', 'GIT_ASKPASS', - 'HTTPS_PROXY', 'AWS_SHARED_CREDENTIALS_FILE', - 'GOOGLE_APPLICATION_CREDENTIALS', 'KUBECONFIG', 'DOCKER_CONFIG', - 'NODE_OPTIONS', 'RENDER_API_KEY', - ]) { - expect(script, name).toContain(`'${name}'`); - expect(toolSpec, name).toContain(`'${name}'`); - } - expect(script).toContain('$ambientSecretVariables'); - expect(script).toContain('$secretNamePattern'); - expect(script).toContain("$rawReceiptRoot = Join-Path $runRoot 'raw-receipts'"); - expect(script).toContain('Publish-SafeReceipt'); - expect(script).toContain('Expected reporter receipt was not created'); - expect(script).toContain("kind = 'playwright-summary'"); - expect(script).toContain("kind = 'vitest-summary'"); - expect(script).toContain('Unknown reporter receipt schema'); - expect(script).toContain('Reporter receipt did not prove an exact successful lane'); - expect(script).toContain('Reporter receipt did not bind the expected test specification'); - expect(script).toContain('Safe receipt identity mismatch'); - expect(script).toContain('Safe receipt host roster mismatch'); - expect(script).toContain('Safe receipt target already exists'); - expect(script).toContain('Receipt contains absolute host paths'); - expect(script).toContain('[Convert]::FromBase64String'); - expect(script).toContain('captured secret values found for environment variables'); - expect(script).toContain('function Resolve-ReceiptLayout'); - expect(script).toContain('StagingRoot = Join-Path $parent ('); - expect(script).toContain('$receiptLayout = Resolve-ReceiptLayout -RequestedReceiptDir $ReceiptDir'); - expect(script).toContain('$receiptStagingOwned = $false'); - expect(script).toContain('$receiptStagingOwned = $true'); - expect(script).toContain('Publish-ReceiptSet'); - expect(script).toContain('[IO.Directory]::Move($StagingRoot, $FinalRoot)'); - expect(script).not.toContain("Set-ProcessEnvironment -Name 'PLAYWRIGHT_JSON_OUTPUT_FILE' -Value $receiptPath"); - expect(script).toContain("'WAGGLE_E2E_REUSE_EXISTING_SERVER'"); - expect(script).toContain( - "Set-ProcessEnvironment -Name 'WAGGLE_E2E_REUSE_EXISTING_SERVER' -Value '0'", - ); - for (const name of [ - 'USERPROFILE', 'HOME', 'HOMEDRIVE', 'HOMEPATH', 'APPDATA', 'LOCALAPPDATA', - 'CLAUDE_CONFIG_DIR', 'CODEX_HOME', 'HERMES_HOME', 'HERMES_PROFILE', - ]) { - expect(script).toContain(`'${name}'`); - } - expect(script).toContain( - '$environmentToRestore = @($profileVariables + $secretVariables + ' + - '$runnerVariables | Select-Object -Unique)', - ); - const isolatedHermesHome = ( - "Set-ProcessEnvironment -Name 'HERMES_HOME' " + - "-Value (Join-Path $hookProfile '.hermes')" - ); - expect(script).toContain(isolatedHermesHome); - expect(script.indexOf(isolatedHermesHome)).toBeLessThan( - script.indexOf( - "Invoke-PlaywrightLane -Spec 'tests/e2e/launcher-real-hook-lifecycle.spec.ts'", - ), - ); - expect(script).toContain('$authenticatedHermesLease = $null'); - expect(script).toContain('Refusing to overwrite an existing Hermes authenticated profile'); - expect(script).toContain('function New-OwnedHermesProfile'); - expect(script).toContain('function Remove-OwnedHermesProfile'); - expect(script).toContain('Hermes authenticated profile ownership verification failed'); - expect(script).toContain('$profileCreateExitCode = $LASTEXITCODE'); - expect(script).toContain('Complete-AuthenticatedIsolationCleanup `'); - expect(script).toContain('-HermesLease $authenticatedHermesLease `'); - expect(script).toContain('-SourceAuthEvidence $sourceAuthEvidence `'); - expect(script).toContain('ReceiptDir must be a fresh path owned by this run'); - const hookLaneIndex = script.indexOf( - "Invoke-PlaywrightLane -Spec 'tests/e2e/launcher-real-hook-lifecycle.spec.ts'", - ); - const successfulRestoreIndex = script.indexOf( - "foreach ($name in $profileVariables) { Restore-ProcessEnvironment -Name $name }", - hookLaneIndex, - ); - const realToolLaneIndex = script.indexOf( - "Invoke-PlaywrightLane -Spec 'tests/e2e/launcher-real-tool-lifecycle.spec.ts'", - ); - expect(successfulRestoreIndex).toBeGreaterThan(hookLaneIndex); - expect(successfulRestoreIndex).toBeLessThan(realToolLaneIndex); - - const failureRestoreIndex = script.lastIndexOf( - "foreach ($name in $environmentToRestore) { Restore-ProcessEnvironment -Name $name }", - ); - const outerFinallyIndex = script.lastIndexOf('} finally {', failureRestoreIndex); - expect(failureRestoreIndex).toBeGreaterThan(outerFinallyIndex); - expect(failureRestoreIndex).toBeLessThan( - script.indexOf('Remove-VerifiedTempTree -Target $runRoot', failureRestoreIndex), - ); - expect(script.indexOf('Publish-ReceiptSet -StagingRoot $receiptStagingRoot')).toBeGreaterThan( - script.indexOf('Remove-VerifiedTempTree -Target $runRoot', failureRestoreIndex), - ); - }); - - it.runIf(process.platform === 'win32')( - 'projects external-agent reporter output into strict path-free receipts', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const helperStart = runner.indexOf('function Assert-NoReparsePointInPath'); - const helperEnd = runner.indexOf('\nfunction Get-FreeLoopbackPort', helperStart); - expect(helperStart).toBeGreaterThanOrEqual(0); - expect(helperEnd).toBeGreaterThan(helperStart); - const helperSource = runner.slice(helperStart, helperEnd); - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-agent-receipt-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$repoRoot = 'C:\Users\Tester\repo' -$runRoot = Join-Path $PSScriptRoot 'owned-run' -$rawReceiptRoot = Join-Path $runRoot 'raw-receipts' -$receiptRoot = Join-Path $PSScriptRoot 'published-receipts' -$receiptStagingRoot = Join-Path $PSScriptRoot '.published-receipts.staging-probe' -$null = New-Item -ItemType Directory -Path $rawReceiptRoot -Force -$null = New-Item -ItemType Directory -Path $receiptStagingRoot -$secretVariables = @('WAGGLE_PROBE_API_KEY') -$originalEnvironment = @{ WAGGLE_PROBE_API_KEY = 'probe-secret-value-6f34a0' } -$receiptHostIds = @('claude-code', 'codex', 'hermes') -$authenticatedHostIds = @('claude-code', 'codex', 'hermes') - -function Assert-Probe([bool]$Condition, [string]$Message) { - if (-not $Condition) { throw $Message } -} -function Write-ProbeJson([string]$Path, $Value) { - [IO.File]::WriteAllText($Path, ($Value | ConvertTo-Json -Depth 10), [Text.UTF8Encoding]::new($false)) -} -function Assert-RejectedReceipt( - $Value, - [string]$Label, - [string]$ExpectedKind = 'playwright-summary', - [string]$ExpectedSpec = 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' -) { - $token = [guid]::NewGuid().ToString('N') - $rawPath = Join-Path $rawReceiptRoot "$token.raw.json" - $safePath = Join-Path $receiptStagingRoot "$token-report.json" - Write-ProbeJson $rawPath $Value - $rejected = $false - try { - Publish-SafeReceipt -RawReceiptPath $rawPath -SafeReceiptPath $safePath -ExpectedKind $ExpectedKind -ExpectedSpec $ExpectedSpec -ReceiptLane $token -ExpectedHostIds $receiptHostIds - } catch { $rejected = $true } - Assert-Probe $rejected "$Label was accepted" - Assert-Probe (-not (Test-Path -LiteralPath $rawPath)) "$Label raw receipt was retained" - Assert-Probe (-not (Test-Path -LiteralPath $safePath)) "$Label safe receipt was published" -} -function New-PassingPlaywrightReceipt( - [string]$Spec = 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' -) { - [pscustomobject]@{ - configFile = Join-Path $repoRoot 'playwright.config.ts' - config = [pscustomobject]@{ rootDir = Join-Path $repoRoot 'tests' } - stats = [pscustomobject]@{ expected = 1; unexpected = 0; flaky = 0; skipped = 0 } - suites = @([pscustomobject]@{ - file = $Spec.Substring('tests/'.Length) - attachments = @([pscustomobject]@{ - body = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('D:\private\tool.exe probe-secret-value-6f34a0')) - }) - }) - } -} - -$unownedSentinel = Join-Path $receiptStagingRoot 'unowned-sentinel.txt' -[IO.File]::WriteAllText($unownedSentinel, 'preserve-me', [Text.UTF8Encoding]::new($false)) -Remove-VerifiedReceiptStaging $receiptStagingRoot $receiptRoot $false -Assert-Probe ([IO.File]::ReadAllText($unownedSentinel) -ceq 'preserve-me') 'Unowned staging collision was deleted' -Remove-Item -LiteralPath $unownedSentinel -Force - -$playwrightRawPath = Join-Path $rawReceiptRoot 'hooks.raw.json' -$playwrightPath = Join-Path $receiptStagingRoot 'hooks-report.json' -Write-ProbeJson $playwrightRawPath (New-PassingPlaywrightReceipt) -Publish-SafeReceipt -RawReceiptPath $playwrightRawPath -SafeReceiptPath $playwrightPath -ExpectedKind 'playwright-summary' -ExpectedSpec 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' -ReceiptLane 'hooks' -ExpectedHostIds $receiptHostIds -Assert-Probe (-not (Test-Path -LiteralPath $playwrightRawPath)) 'Playwright raw receipt survived projection' -$playwrightText = [IO.File]::ReadAllText($playwrightPath) -$playwright = $playwrightText | ConvertFrom-Json -Assert-Probe ($playwright.kind -ceq 'playwright-summary') 'Playwright projection kind mismatch' -Assert-Probe ($playwright.success -is [bool] -and $playwright.success) 'Playwright projection did not prove success' -Assert-Probe ($playwright.spec -ceq 'tests/e2e/launcher-real-hook-lifecycle.spec.ts') 'Playwright spec identity missing' -Assert-Probe (($playwright.hostIds -join ',') -ceq ($receiptHostIds -join ',')) 'Playwright host roster missing' -Assert-Probe (-not $playwrightText.Contains('C:\Users')) 'Direct host path survived projection' -Assert-Probe (-not $playwrightText.Contains('probe-secret-value-6f34a0')) 'Secret survived projection' -Assert-Probe (-not $playwrightText.Contains('body')) 'Base64 attachment survived projection' - -$toolsRawPath = Join-Path $rawReceiptRoot 'tools.raw.json' -$toolsPath = Join-Path $receiptStagingRoot 'tools-report.json' -Write-ProbeJson $toolsRawPath (New-PassingPlaywrightReceipt 'tests/e2e/launcher-real-tool-lifecycle.spec.ts') -Publish-SafeReceipt -RawReceiptPath $toolsRawPath -SafeReceiptPath $toolsPath -ExpectedKind 'playwright-summary' -ExpectedSpec 'tests/e2e/launcher-real-tool-lifecycle.spec.ts' -ReceiptLane 'tools' -ExpectedHostIds $receiptHostIds - -$vitestRawPath = Join-Path $rawReceiptRoot 'authenticated-tasks.raw.json' -$vitestPath = Join-Path $receiptStagingRoot 'authenticated-tasks-report.json' -$vitest = [pscustomobject]@{ - success = $true - numTotalTestSuites = 2; numPassedTestSuites = 2; numFailedTestSuites = 0; numPendingTestSuites = 0 - numTotalTests = 1; numPassedTests = 1; numFailedTests = 0; numPendingTests = 0; numTodoTests = 0 - testResults = @([pscustomobject]@{ name = 'C:\Users\Tester\repo\tests\integration\external-agent-collaboration.live.test.ts' }) -} -Write-ProbeJson $vitestRawPath $vitest -Publish-SafeReceipt -RawReceiptPath $vitestRawPath -SafeReceiptPath $vitestPath -ExpectedKind 'vitest-summary' -ExpectedSpec 'tests/integration/external-agent-collaboration.live.test.ts' -ReceiptLane 'authenticated-tasks' -ExpectedHostIds $authenticatedHostIds -HermesProvider 'openai-codex' -HermesModel 'gpt-5.5' -Assert-Probe (-not (Test-Path -LiteralPath $vitestRawPath)) 'Vitest raw receipt survived projection' -$vitestProjection = [IO.File]::ReadAllText($vitestPath) | ConvertFrom-Json -Assert-Probe ($vitestProjection.kind -ceq 'vitest-summary') 'Vitest projection kind mismatch' -Assert-Probe ($vitestProjection.passedTests -eq 1) 'Vitest exact pass count was not retained' -Assert-Probe ($vitestProjection.hermesProvider -ceq 'openai-codex') 'Vitest Hermes provider identity missing' -Assert-Probe ($vitestProjection.hermesModel -ceq 'gpt-5.5') 'Vitest Hermes model identity missing' -Remove-Item -LiteralPath $vitestPath -Force - -Assert-RejectedReceipt (New-PassingPlaywrightReceipt 'tests/e2e/launcher-real-tool-lifecycle.spec.ts') 'Mismatched Playwright specification' -$spoofedPlaywright = New-PassingPlaywrightReceipt 'tests/e2e/launcher-real-tool-lifecycle.spec.ts' -$spoofedPlaywright.suites[0].attachments += [pscustomobject]@{ - note = 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' -} -Assert-RejectedReceipt $spoofedPlaywright 'Spoofed Playwright specification' -Assert-RejectedReceipt (New-PassingPlaywrightReceipt) 'Mismatched reporter kind' 'vitest-summary' 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' - -$spoofedVitest = $vitest | ConvertTo-Json -Depth 10 | ConvertFrom-Json -$spoofedVitest.testResults[0].name = 'C:\Users\Tester\repo\tests\integration\unrelated.test.ts' -$spoofedVitest.testResults[0] | Add-Member -NotePropertyName note -NotePropertyValue 'tests/integration/external-agent-collaboration.live.test.ts' -Assert-RejectedReceipt $spoofedVitest 'Spoofed Vitest specification' 'vitest-summary' 'tests/integration/external-agent-collaboration.live.test.ts' - -$extraResultVitest = $vitest | ConvertTo-Json -Depth 10 | ConvertFrom-Json -$extraResultVitest.testResults += [pscustomobject]@{ - name = 'C:\Users\Tester\repo\tests\integration\unrelated.test.ts' -} -Assert-RejectedReceipt $extraResultVitest 'Extra Vitest result file' 'vitest-summary' 'tests/integration/external-agent-collaboration.live.test.ts' - -$skipped = New-PassingPlaywrightReceipt -$skipped.stats.expected = 0 -$skipped.stats.skipped = 1 -Assert-RejectedReceipt $skipped 'Skipped Playwright receipt' - -$missingPlaywrightField = New-PassingPlaywrightReceipt -$missingPlaywrightField.stats.PSObject.Properties.Remove('flaky') -Assert-RejectedReceipt $missingPlaywrightField 'Omitted Playwright field' -$stringPlaywrightCounts = New-PassingPlaywrightReceipt -$stringPlaywrightCounts.stats.expected = '1' -$stringPlaywrightCounts.stats.unexpected = '0' -$stringPlaywrightCounts.stats.flaky = '0' -$stringPlaywrightCounts.stats.skipped = '0' -Assert-RejectedReceipt $stringPlaywrightCounts 'String Playwright counts' -$failedPlaywrightCast = New-PassingPlaywrightReceipt -$failedPlaywrightCast.stats.expected = 'not-a-number' -Assert-RejectedReceipt $failedPlaywrightCast 'Invalid Playwright count' - -$stringVitestBoolean = $vitest | ConvertTo-Json -Depth 10 | ConvertFrom-Json -$stringVitestBoolean.success = 'false' -Assert-RejectedReceipt $stringVitestBoolean 'String Vitest boolean' 'vitest-summary' 'tests/integration/external-agent-collaboration.live.test.ts' -$stringVitestCount = $vitest | ConvertTo-Json -Depth 10 | ConvertFrom-Json -$stringVitestCount.numTotalTests = '1' -Assert-RejectedReceipt $stringVitestCount 'String Vitest count' 'vitest-summary' 'tests/integration/external-agent-collaboration.live.test.ts' -$missingVitestField = $vitest | ConvertTo-Json -Depth 10 | ConvertFrom-Json -$missingVitestField.PSObject.Properties.Remove('numTodoTests') -Assert-RejectedReceipt $missingVitestField 'Omitted Vitest field' 'vitest-summary' 'tests/integration/external-agent-collaboration.live.test.ts' - -Assert-RejectedReceipt ([pscustomobject]@{ success = $true }) 'Malformed reporter schema' - -$invalidJsonPath = Join-Path $rawReceiptRoot 'invalid-json-report.json' -$invalidSafePath = Join-Path $receiptStagingRoot 'invalid-json-report.json' -[IO.File]::WriteAllText($invalidJsonPath, '{', [Text.UTF8Encoding]::new($false)) -$invalidJsonRejected = $false -try { - Publish-SafeReceipt -RawReceiptPath $invalidJsonPath -SafeReceiptPath $invalidSafePath -ExpectedKind 'playwright-summary' -ExpectedSpec 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' -ReceiptLane 'invalid-json' -ExpectedHostIds $receiptHostIds -} catch { $invalidJsonRejected = $true } -Assert-Probe $invalidJsonRejected 'Invalid reporter JSON was accepted' -Assert-Probe (-not (Test-Path -LiteralPath $invalidJsonPath)) 'Invalid raw JSON receipt was retained' -Assert-Probe (-not (Test-Path -LiteralPath $invalidSafePath)) 'Invalid JSON produced a safe receipt' - -$collisionStage = Join-Path $PSScriptRoot '.published-receipts.staging-collision' -$null = New-Item -ItemType Directory -Path $collisionStage -$savedStagingRoot = $receiptStagingRoot -$receiptStagingRoot = $collisionStage -$collisionRawPath = Join-Path $rawReceiptRoot 'collision.raw.json' -$collisionSafePath = Join-Path $collisionStage 'collision-report.json' -Write-ProbeJson $collisionRawPath (New-PassingPlaywrightReceipt) -[IO.File]::WriteAllText($collisionSafePath, 'sentinel', [Text.UTF8Encoding]::new($false)) -$writeRejected = $false -try { - Publish-SafeReceipt -RawReceiptPath $collisionRawPath -SafeReceiptPath $collisionSafePath -ExpectedKind 'playwright-summary' -ExpectedSpec 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' -ReceiptLane 'collision' -ExpectedHostIds $receiptHostIds -} catch { $writeRejected = $true } -Assert-Probe $writeRejected 'Projection write failure was accepted' -Assert-Probe (-not (Test-Path -LiteralPath $collisionRawPath)) 'Raw receipt survived projection collision' -Assert-Probe ([IO.File]::ReadAllText($collisionSafePath) -ceq 'sentinel') 'Existing safe receipt was overwritten' -$receiptStagingRoot = $savedStagingRoot -Remove-Item -LiteralPath $collisionStage -Recurse -Force - -$missingStage = Join-Path $PSScriptRoot '.published-receipts.staging-missing' -$null = New-Item -ItemType Directory -Path $missingStage -Copy-Item -LiteralPath $playwrightPath -Destination (Join-Path $missingStage 'hooks-report.json') -$missingFinal = Join-Path $PSScriptRoot 'missing-final' -$missingRejected = $false -try { Publish-ReceiptSet $missingStage $missingFinal @('hooks-report.json', 'tools-report.json') } catch { $missingRejected = $true } -Assert-Probe $missingRejected 'Incomplete receipt set was published' -Assert-Probe (-not (Test-Path -LiteralPath $missingFinal)) 'Incomplete final receipt directory exists' - -$extraStage = Join-Path $PSScriptRoot '.published-receipts.staging-extra' -$null = New-Item -ItemType Directory -Path $extraStage -Copy-Item -LiteralPath $playwrightPath -Destination (Join-Path $extraStage 'hooks-report.json') -Copy-Item -LiteralPath $toolsPath -Destination (Join-Path $extraStage 'tools-report.json') -Copy-Item -LiteralPath $toolsPath -Destination (Join-Path $extraStage 'unexpected-report.json') -$extraFinal = Join-Path $PSScriptRoot 'extra-final' -$extraRejected = $false -try { Publish-ReceiptSet $extraStage $extraFinal @('hooks-report.json', 'tools-report.json') } catch { $extraRejected = $true } -Assert-Probe $extraRejected 'Receipt set with an extra file was published' -Assert-Probe (-not (Test-Path -LiteralPath $extraFinal)) 'Extra final receipt directory exists' - -$outsideReceiptParent = Join-Path $PSScriptRoot 'outside-receipt-parent' -$receiptJunction = Join-Path $PSScriptRoot 'receipt-parent-junction' -$unsafeFinal = Join-Path $receiptJunction 'unsafe-final' -$unsafeStage = Join-Path $receiptJunction '.unsafe-final.staging-probe' -$null = New-Item -ItemType Directory -Path $outsideReceiptParent -$null = New-Item -ItemType Junction -Path $receiptJunction -Target $outsideReceiptParent -$null = New-Item -ItemType Directory -Path $unsafeStage -$outsideSentinel = Join-Path $unsafeStage 'preserve-me.txt' -[IO.File]::WriteAllText($outsideSentinel, 'preserve-me', [Text.UTF8Encoding]::new($false)) -$junctionCleanupRejected = $false -try { Remove-VerifiedReceiptStaging $unsafeStage $unsafeFinal $true } catch { - $junctionCleanupRejected = $_.Exception.Message -like '*reparse point*' -} -Assert-Probe $junctionCleanupRejected 'Receipt cleanup traversed a reparse ancestor' -Assert-Probe ([IO.File]::ReadAllText($outsideSentinel) -ceq 'preserve-me') 'Receipt cleanup deleted outside content' - -Publish-ReceiptSet $receiptStagingRoot $receiptRoot @('hooks-report.json', 'tools-report.json') -Assert-Probe (Test-Path -LiteralPath $receiptRoot -PathType Container) 'Exact receipt set was not published' -Assert-Probe (-not (Test-Path -LiteralPath $receiptStagingRoot)) 'Staging directory survived atomic publication' -$publishedNames = @(Get-ChildItem -LiteralPath $receiptRoot -File | ForEach-Object Name | Sort-Object) -Assert-Probe (($publishedNames -join ',') -ceq 'hooks-report.json,tools-report.json') 'Published receipt set was not exact' -Assert-Probe (@(Get-ChildItem -LiteralPath $rawReceiptRoot -File).Count -eq 0) 'Raw reporter receipts survived' -`; - - try { - fs.writeFileSync(probePath, `${helperSource}\n${fixtureSource}`, 'utf-8'); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'normalizes trailing-separator Windows receipt layout to an atomic sibling', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Resolve-ReceiptLayout'); - const functionEnd = runner.indexOf('\n$repoRoot', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-receipt-layout-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$expectedRoot = Join-Path $PSScriptRoot 'receipts' -$requestedRoot = $expectedRoot + [IO.Path]::DirectorySeparatorChar -$layout = Resolve-ReceiptLayout -RequestedReceiptDir $requestedRoot -if (-not $layout.Root.Equals($expectedRoot, [StringComparison]::OrdinalIgnoreCase)) { throw 'Receipt root was not normalized' } -if (-not $layout.Parent.Equals($PSScriptRoot, [StringComparison]::OrdinalIgnoreCase)) { throw 'Receipt parent was not the direct sibling parent' } -if (-not [IO.Path]::GetDirectoryName($layout.StagingRoot).Equals($PSScriptRoot, [StringComparison]::OrdinalIgnoreCase)) { throw 'Receipt staging was not a sibling' } -if (-not [IO.Path]::GetFileName($layout.StagingRoot).StartsWith('.receipts.staging-', [StringComparison]::Ordinal)) { throw 'Receipt staging name was not owned' } -if (Test-Path -LiteralPath $expectedRoot) { throw 'Final receipt directory was created during layout' } -`; - try { - fs.writeFileSync(probePath, `${runner.slice(functionStart, functionEnd)}\n${fixtureSource}`, 'utf-8'); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'preserves a colliding authenticated Hermes profile without invoking its CLI', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Assert-NoReparsePointInPath'); - const functionEnd = runner.indexOf('\nfunction Get-ReceiptStrings', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-hermes-collision-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$profilesRoot = Join-Path $PSScriptRoot 'profiles' -$profileName = 'wagglee2ecollision' -$profileHome = Join-Path $profilesRoot $profileName -$null = New-Item -ItemType Directory -Path $profileHome -$sentinel = Join-Path $profileHome 'sentinel.txt' -[IO.File]::WriteAllText($sentinel, 'preserve-me', [Text.UTF8Encoding]::new($false)) -$script:hermesInvocations = @() -function hermes { - $script:hermesInvocations += ($args -join ' ') - throw 'Hermes CLI must not run for a profile collision' -} -$lease = $null -$rejected = $false -try { $lease = New-OwnedHermesProfile -ProfilesRoot $profilesRoot -ProfileName $profileName -OwnedRoot $PSScriptRoot } -catch { $rejected = $_.Exception.Message -like '*Refusing to overwrite*' } -finally { if ($null -ne $lease) { Remove-OwnedHermesProfile -Lease $lease } } -if (-not $rejected) { throw 'Colliding Hermes profile was not rejected' } -if ($script:hermesInvocations.Count -ne 0) { throw 'Hermes CLI was invoked for a collision' } -if (-not (Test-Path -LiteralPath $profileHome -PathType Container)) { throw 'Colliding profile was deleted' } -if ([IO.File]::ReadAllText($sentinel) -cne 'preserve-me') { throw 'Colliding profile sentinel changed' } -if (@(Get-ChildItem -LiteralPath $profilesRoot -Force).Count -ne 1) { throw 'Unexpected profile artifact was created' } -`; - try { - fs.writeFileSync(probePath, `${runner.slice(functionStart, functionEnd)}\n${fixtureSource}`, 'utf-8'); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'never deletes an unsealed Hermes profile after setup failure', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Assert-NoReparsePointInPath'); - const functionEnd = runner.indexOf('\nfunction Invoke-NativePreflight', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-hermes-seal-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$profilesRoot = Join-Path $PSScriptRoot 'profiles' -$profileName = 'wagglee2esealfailure' -$profileHome = Join-Path $profilesRoot $profileName -$script:hermesInvocations = @() -$script:failDuringCreate = $false - -function Write-HermesOwnershipMarker { - throw 'forced ownership marker failure' -} - -function hermes { - $script:hermesInvocations += ($args -join ' ') - if ($args[0] -eq 'profile' -and $args[1] -eq 'create') { - $null = New-Item -ItemType Directory -Path $profileHome - if ($script:failDuringCreate) { - [IO.File]::WriteAllText((Join-Path $profileHome 'competing-sentinel.txt'), 'preserve-me') - throw 'forced terminating create failure' - } - $global:LASTEXITCODE = 0 - return - } - if ($args[0] -eq 'profile' -and $args[1] -eq 'delete') { - Remove-Item -LiteralPath $profileHome -Recurse -Force - $global:LASTEXITCODE = 0 - return - } - throw 'Unexpected Hermes CLI call' -} - -$rejected = $false -try { - $null = New-OwnedHermesProfile -ProfilesRoot $profilesRoot -ProfileName $profileName -OwnedRoot $PSScriptRoot -} catch { - $rejected = $_.Exception.Message -like '*forced ownership marker failure*' -} -if (-not $rejected) { throw 'Hermes ownership seal failure was not preserved' } -if (-not (Test-Path -LiteralPath $profileHome -PathType Container)) { throw 'Unsealed Hermes profile was deleted' } -Remove-Item -LiteralPath $profileHome -Recurse -Force - -$profileName = 'wagglee2ecreatefailure' -$profileHome = Join-Path $profilesRoot $profileName -$script:failDuringCreate = $true -$createRejected = $false -try { - $null = New-OwnedHermesProfile -ProfilesRoot $profilesRoot -ProfileName $profileName -OwnedRoot $PSScriptRoot -} catch { - $createRejected = $_.Exception.Message -like '*forced terminating create failure*' -} -if (-not $createRejected) { throw 'Terminating Hermes create failure was not preserved' } -if (-not (Test-Path -LiteralPath $profileHome -PathType Container)) { throw 'Competing Hermes profile was deleted' } -if ([IO.File]::ReadAllText((Join-Path $profileHome 'competing-sentinel.txt')) -cne 'preserve-me') { - throw 'Competing Hermes profile sentinel changed' -} -if (($script:hermesInvocations -join '|') -cne - 'profile create wagglee2esealfailure --no-alias --no-skills|profile create wagglee2ecreatefailure --no-alias --no-skills') { - throw 'Hermes setup failure invoked an unowned delete' -} -`; - - try { - fs.writeFileSync(probePath, `${runner.slice(functionStart, functionEnd)}\n${fixtureSource}`, 'utf-8'); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'checks source auth invariants even when Hermes profile cleanup fails', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Assert-NoReparsePointInPath'); - const functionEnd = runner.indexOf('\nfunction Get-ReceiptStrings', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-auth-cleanup-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$script:hashChecks = 0 -$ownedRoot = Join-Path $PSScriptRoot 'owned-profile' -$claudeCopy = Join-Path $ownedRoot '.claude\.credentials.json' -$codexCopy = Join-Path $ownedRoot '.codex\auth.json' -$hermesCopy = Join-Path $ownedRoot 'hermes-root\profiles\probe\auth.json' -$null = New-Item -ItemType Directory -Path ([IO.Path]::GetDirectoryName($claudeCopy)) -Force -$null = New-Item -ItemType Directory -Path ([IO.Path]::GetDirectoryName($codexCopy)) -Force -$null = New-Item -ItemType Directory -Path ([IO.Path]::GetDirectoryName($hermesCopy)) -Force -[IO.File]::WriteAllText($claudeCopy, 'claude-secret-copy') -[IO.File]::WriteAllText($codexCopy, 'codex-secret-copy') -[IO.File]::WriteAllText($hermesCopy, 'hermes-secret-copy') -function Set-ProcessEnvironment { } -function Restore-ProcessEnvironment { } -function Remove-OwnedHermesProfile { throw 'forced Hermes cleanup failure' } -function Get-FileHash { - $script:hashChecks += 1 - return [pscustomobject]@{ Hash = 'expected-hash' } -} - -$failure = $null -$lease = [pscustomobject]@{ ProfileName = 'probe' } -$evidence = @( - [pscustomobject]@{ Path = 'claude-auth'; Hash = 'expected-hash' }, - [pscustomobject]@{ Path = 'codex-auth'; Hash = 'expected-hash' }, - [pscustomobject]@{ Path = 'hermes-auth'; Hash = 'expected-hash' } -) -try { - Complete-AuthenticatedIsolationCleanup -HermesLease $lease -SourceAuthEvidence $evidence -ProfileVariables @() -IsolatedAuthPaths @($claudeCopy, $codexCopy, $hermesCopy) -OwnedRoot $ownedRoot -} catch { - $failure = $_.Exception.Message -} -if (Test-Path -LiteralPath $claudeCopy) { throw 'Claude authentication copy survived explicit cleanup' } -if (Test-Path -LiteralPath $codexCopy) { throw 'Codex authentication copy survived explicit cleanup' } -if (Test-Path -LiteralPath $hermesCopy) { throw 'Hermes authentication copy survived explicit cleanup' } - -$copySource = Join-Path $PSScriptRoot 'source-auth.json' -$outsideAuthRoot = Join-Path $PSScriptRoot 'outside-auth-root' -$redirectedAuthParent = Join-Path $ownedRoot 'redirected-auth' -[IO.File]::WriteAllText($copySource, 'copy-source') -$null = New-Item -ItemType Directory -Path $outsideAuthRoot -$null = New-Item -ItemType Junction -Path $redirectedAuthParent -Target $outsideAuthRoot -Remove-Item -LiteralPath $outsideAuthRoot -Recurse -Force -$copyRejected = $false -try { - Copy-IsolatedAuthenticationFile -Source $copySource -Destination (Join-Path $redirectedAuthParent 'auth.json') -OwnedRoot $ownedRoot -} catch { - $copyRejected = $_.Exception.Message -like '*reparse point*' -} -if (-not $copyRejected) { throw 'Authentication copy did not reject a dangling reparse ancestor' } -if ([IO.File]::Exists((Join-Path $outsideAuthRoot 'auth.json'))) { throw 'Authentication copy escaped through a reparse ancestor' } - -if ($script:hashChecks -ne 3) { throw 'Source auth hashes were skipped after cleanup failure' } -if ($failure -notlike '*forced Hermes cleanup failure*') { - throw 'Aggregated cleanup failure omitted the Hermes error' -} -`; - - try { - fs.writeFileSync(probePath, `${runner.slice(functionStart, functionEnd)}\n${fixtureSource}`, 'utf-8'); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'builds a shell-free Codex auth shim that preserves literal task arguments', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Assert-NoReparsePointInPath'); - const functionEnd = runner.indexOf('\nfunction New-OwnedHermesProfile', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-codex-shim-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const nodePath = process.execPath.replaceAll("'", "''"); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$fakeNpm = Join-Path $PSScriptRoot 'fake-npm' -$realEntry = Join-Path $fakeNpm 'node_modules\@openai\codex\bin\codex.js' -$null = New-Item -ItemType Directory -Path ([IO.Path]::GetDirectoryName($realEntry)) -$realEntrySource = @' -const fs = require('node:fs'); -fs.writeFileSync(process.env.PROBE_OUTPUT, JSON.stringify({ codexHome: process.env.CODEX_HOME, args: process.argv.slice(2) })); -'@ -[IO.File]::WriteAllText($realEntry, $realEntrySource, [Text.UTF8Encoding]::new($false)) -$realCmd = @' -@ECHO off -SET "_prog=node" -"%_prog%" "%dp0%\node_modules\@openai\codex\bin\codex.js" %* -'@ -[IO.File]::WriteAllText((Join-Path $fakeNpm 'codex.cmd'), $realCmd, [Text.ASCIIEncoding]::new()) -$env:PATH = $fakeNpm + [IO.Path]::PathSeparator + $env:PATH -$resolvedProbeCommand = Get-Command codex.cmd -CommandType Application -ErrorAction Stop | - Select-Object -First 1 -$resolvedProbeItem = Get-Item -LiteralPath $resolvedProbeCommand.Source -Force -if ($resolvedProbeItem.PSIsContainer -or ($resolvedProbeItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw "Fake Codex command was unsafe before helper: $($resolvedProbeCommand.Source) attrs=$($resolvedProbeItem.Attributes) dir=$($resolvedProbeItem.PSIsContainer)" -} -$ownedRoot = Join-Path $PSScriptRoot 'owned' -$codexHome = Join-Path $ownedRoot '.codex' -$shim = New-IsolatedCodexShim -ShimRoot (Join-Path $ownedRoot 'bin') -CodexHome $codexHome -OwnedRoot $ownedRoot -$shimText = [IO.File]::ReadAllText($shim.Shim) -if (-not $shimText.Contains('"%_prog%" "%dp0%\codex-isolated.mjs" %*')) { throw 'Generated shim was not npm-shaped' } -$env:PROBE_OUTPUT = Join-Path $PSScriptRoot 'result.json' -$arguments = @('literal%value&still-one', '-C', 'D:\work & data') -& '${nodePath}' $shim.Launcher @arguments -if ($LASTEXITCODE -ne 0) { throw 'Isolated Codex launcher failed' } -$result = [IO.File]::ReadAllText($env:PROBE_OUTPUT) | ConvertFrom-Json -if (-not $result.codexHome.Equals($codexHome, [StringComparison]::OrdinalIgnoreCase)) { throw 'CODEX_HOME was not isolated' } -if (($result.args -join '|') -cne ($arguments -join '|')) { throw 'Codex arguments changed during forwarding' } -`; - try { - fs.writeFileSync(probePath, `${runner.slice(functionStart, functionEnd)}\n${fixtureSource}`, 'utf-8'); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'removes owned Windows temp trees containing long Claude session paths', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Remove-VerifiedTempTree'); - const functionEnd = runner.indexOf('\nfunction Invoke-PlaywrightLane', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-long-cleanup-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$env:TEMP = $PSScriptRoot -$tempBase = $PSScriptRoot -function Start-Sleep { param([int]$Milliseconds) } -function ConvertTo-ExtendedPath([string]$Path) { - $full = [IO.Path]::GetFullPath($Path) - if ($full.StartsWith('\\')) { return '\\?\UNC\' + $full.Substring(2) } - return '\\?\' + $full -} -$target = Join-Path $PSScriptRoot 'owned-long-tree' -$nested = $target -while ((Join-Path $nested 'session.jsonl').Length -le 265) { - $nested = Join-Path $nested ('claude-session-' + ('x' * 40)) -} -$extendedTarget = ConvertTo-ExtendedPath $target -$extendedNested = ConvertTo-ExtendedPath $nested -$extendedFile = ConvertTo-ExtendedPath (Join-Path $nested 'session.jsonl') -$null = [IO.Directory]::CreateDirectory($extendedNested) -[IO.File]::WriteAllText($extendedFile, '{}', [Text.UTF8Encoding]::new($false)) -if (-not [IO.File]::Exists($extendedFile)) { throw 'Long-path fixture was not created' } -Remove-VerifiedTempTree -Target $target -if ([IO.Directory]::Exists($extendedTarget)) { throw 'Long-path temp tree survived cleanup' } -`; - try { - fs.writeFileSync( - probePath, - `${runner.slice(functionStart, functionEnd)}\n${fixtureSource}`, - 'utf-8', - ); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'accepts successful authentication status emitted on stderr by Windows CLIs', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Invoke-NativePreflight'); - const functionEnd = runner.indexOf('\nfunction Get-ReceiptStrings', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-auth-status-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const nodePath = process.execPath.replaceAll("'", "''"); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$script:runnerNodePath = '${nodePath}' -Invoke-NativePreflight -FilePath $script:runnerNodePath -ArgumentList @('-e', "process.stderr.write('Logged in using ChatGPT'); process.exit(0)") -FailureMessage 'status failed' -$rejected = $false -try { - Invoke-NativePreflight -FilePath $script:runnerNodePath -ArgumentList @('-e', "process.stderr.write('Authentication failed'); process.exit(7)") -FailureMessage 'status failed' -} catch { - $rejected = $_.Exception.Message -ceq 'status failed' -} -if (-not $rejected) { throw 'Non-zero authentication status was accepted' } -`; - - try { - fs.writeFileSync( - probePath, - `${runner.slice(functionStart, functionEnd)}\n${fixtureSource}`, - 'utf-8', - ); - const result = spawnSync( - 'powershell.exe', - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it.runIf(process.platform === 'win32')( - 'keeps Playwright raw output in the owned temp tree without ReceiptDir', - () => { - const runner = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const functionStart = runner.indexOf('function Invoke-PlaywrightLane'); - const functionEnd = runner.indexOf('\nfunction Invoke-VitestLane', functionStart); - expect(functionStart).toBeGreaterThanOrEqual(0); - expect(functionEnd).toBeGreaterThan(functionStart); - const functionSource = runner.slice(functionStart, functionEnd); - const probeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-playwright-output-probe-')); - const probePath = path.join(probeRoot, 'probe.ps1'); - const fakeRunner = path.join(probeRoot, 'fake-node.cmd'); - const fixtureSource = String.raw` -$ErrorActionPreference = 'Stop' -$runRoot = Join-Path $PSScriptRoot 'owned-run' -$null = New-Item -ItemType Directory -Path $runRoot -$receiptRoot = $null -$receiptStagingRoot = $null -$rawReceiptRoot = Join-Path $runRoot 'raw-receipts' -$script:runnerNodePath = Join-Path $PSScriptRoot 'fake-node.cmd' -$script:playwrightCli = 'fake-playwright-cli.js' -function Get-FreeLoopbackPort { 45678 } -function Set-ProcessEnvironment([string]$Name, [AllowNull()][string]$Value) {} -function Publish-SafeReceipt([string]$RawReceiptPath, [string]$SafeReceiptPath) { throw 'Unexpected receipt publisher call' } -$laneFailed = $false -try { - Invoke-PlaywrightLane -Spec 'fake.spec.ts' -DataDir (Join-Path $runRoot 'data') -ReceiptName 'hooks' -} catch { - $laneFailed = $true -} -if (-not $laneFailed) { throw 'Failing fake Playwright runner was accepted' } -$arguments = [IO.File]::ReadAllText((Join-Path $PSScriptRoot 'args.txt')) -$expectedOutput = Join-Path $runRoot 'playwright-hooks' -if (-not $arguments.Contains('--output')) { throw 'Playwright output flag was omitted without ReceiptDir' } -if (-not $arguments.Contains($expectedOutput)) { throw 'Playwright output escaped the owned run root' } -if ($arguments.Contains('test-results')) { throw 'Playwright default output directory remained reachable' } -`; - - try { - fs.writeFileSync(fakeRunner, '@echo off\r\n> "%~dp0args.txt" echo %*\r\nexit /b 1\r\n', 'utf-8'); - fs.writeFileSync(probePath, `${functionSource}\n${fixtureSource}`, 'utf-8'); - const result = spawnSync( - powershellProbeExecutable(), - ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', probePath], - { encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(result.status, result.stderr || result.stdout).toBe(0); - } finally { - fs.rmSync(probeRoot, { recursive: true, force: true }); - } - }, - 40_000, - ); - - it('hard-disables the legacy authenticated Windows external-agent task seal before any auth or temp work', () => { - const script = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-external-agents.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - - expect(script).toContain('[switch]$AuthenticatedTasks'); - const guard = [ - 'if ($AuthenticatedTasks) {', - " throw 'AuthenticatedTasks is disabled; use scripts/test-windows-official-auth-canaries.ps1 for no-copy user-auth evidence.'", - '}', - ].join('\n'); - const guardIndex = script.indexOf(guard); - expect(guardIndex).toBeGreaterThan(-1); - for (const protectedMarker of [ - "$repoRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..')).Path", - '$runRoot = Join-Path $tempBase', - '$sourceClaudeCredentials =', - 'Copy-IsolatedAuthenticationFile', - "Set-ProcessEnvironment -Name 'USERPROFILE' -Value $authenticatedProfileRoot", - "Invoke-VitestLane -Spec 'tests/integration/external-agent-collaboration.live.test.ts'", - ]) { - const protectedIndex = script.indexOf(protectedMarker); - expect(protectedIndex, protectedMarker).toBeGreaterThan(-1); - expect(guardIndex, protectedMarker).toBeLessThan(protectedIndex); - } - expect(script).toContain( - "Invoke-PlaywrightLane -Spec 'tests/e2e/launcher-real-hook-lifecycle.spec.ts'", - ); - expect(script).toContain( - "Invoke-PlaywrightLane -Spec 'tests/e2e/launcher-real-tool-lifecycle.spec.ts'", - ); - }); - - it('keeps the three-agent live collaboration contract regression-locked', () => { - const liveSpec = fs.readFileSync( - path.join(ROOT, 'tests', 'integration', 'external-agent-collaboration.live.test.ts'), - 'utf-8', - ); - - expect(liveSpec).toContain( - "const REQUIRED_TOOLS = ['claude-code', 'codex', 'hermes'] as const;", - ); - expect(liveSpec).not.toContain('context.skip('); - expect(liveSpec).toContain("{ toolId: 'claude-code', workspaceIds: [sourceWorkspaceId], access: 'read-only' }"); - expect(liveSpec).toContain("{ toolId: 'codex', workspaceIds: [sourceWorkspaceId], access: 'read-only' }"); - expect(liveSpec).toContain("{ toolId: 'hermes', workspaceIds: [sourceWorkspaceId], access: 'native' }"); - expect(liveSpec).toContain("{ toolId: 'hermes', workspaceIds: [synthesisWorkspaceId], access: 'native' }"); - expect(liveSpec).toContain('workspaceDigest(sourceWorkspaceDir)'); - expect(liveSpec).toContain('workspaceDigest(synthesisWorkspaceDir)'); - expect(liveSpec).toContain('summary.trim(), diagnostic).toBe(expectedCanaryLine)'); - expect(liveSpec).toContain("summary.trim(), diagnostic).toBe('NO_LOCAL_CANARY')"); - expect(liveSpec).toContain("expect(synthesis?.executor.toolId).toBe('hermes')"); - expect(liveSpec).not.toContain("'openclaw'"); - expect(liveSpec).toContain("memoryRefs.status === 'complete'"); - expect(liveSpec).toContain("new AgentRunRegistry(path.join(dataDir, 'agent-runs.json'))"); - expect(liveSpec).not.toContain('result: run.result'); - }); - - it('pins standalone Codex in the no-copy official-auth canary contract', () => { - const script = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-official-auth-canaries.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const denialProof = fs.readFileSync( - path.join(ROOT, 'scripts', 'verify-codex-tool-denial.mjs'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - - expect(script).toContain("[string]$CodexModel = 'gpt-5.5'"); - expect(script).toContain("$executionAcknowledgement = 'I_ACKNOWLEDGE_3_OFFICIAL_AUTH_CALLS'"); - expect(script).toContain("$codexExe = Resolve-Application -Name 'codex.exe'"); - expect(script).toContain('codexResolved = $true'); - expect(script).toContain("codexInstaller = Join-Path $repoRoot 'packages\\hive-mind-hooks-codex\\dist\\bin\\codex-hooks.js'"); - expect(script).toContain("codexSessionStart = Join-Path $repoRoot 'packages\\hive-mind-hooks-codex\\dist\\hooks\\session-start.js'"); - expect(script).toContain("codexUserPromptSubmit = Join-Path $repoRoot 'packages\\hive-mind-hooks-codex\\dist\\hooks\\user-prompt-submit.js'"); - expect(script).toContain("codexStop = Join-Path $repoRoot 'packages\\hive-mind-hooks-codex\\dist\\hooks\\stop.js'"); - expect(script).toContain("codexPreCompact = Join-Path $repoRoot 'packages\\hive-mind-hooks-codex\\dist\\hooks\\pre-compact.js'"); - expect(script).toContain("codexToolDenial = Join-Path $repoRoot 'scripts\\verify-codex-tool-denial.mjs'"); - - expect(script).toContain("-ArgumentList @($artifactPaths.codexInstaller, 'verify')"); - expect(script).toContain('$codexHookPasses -lt 9'); - expect(script).toContain('Codex hook verification did not satisfy the 9-check contract.'); - - expect(script).toContain("-ArgumentList @('login', 'status')"); - expect(script).toContain('Logged in using ChatGPT'); - expect(script).toContain( - 'Codex is not authenticated through the required first-party ChatGPT client session.', - ); - - expect(script).toContain("'--codex-exe', $codexExe"); - expect(script).toContain("'--hive-mind-cli', $artifactPaths.hiveMindCli"); - expect(script).toContain("'--receipt-dir', $codexProofDir"); - expect(script).toContain("'--expected-head', $ExpectedHead"); - expect(script).toContain("'--model', $CodexModel"); - expect(script).toContain("'--windows-powershell', $windowsPowerShell"); - expect(script).toContain("'--workspace', $codexWorkspace"); - expect(script).toContain("'--execute-paid'"); - expect(script).toContain("'--marker', $codexMarker"); - expect(script).toContain("'--ack', 'I_ACKNOWLEDGE_1_CODEX_OFFICIAL_AUTH_CALL'"); - expect(script).toContain('Codex zero-cost tool-denial proof and official-auth canary'); - expect(script).toContain('function Assert-JsonBoolean'); - expect(script).toContain('function Assert-JsonInteger'); - expect(script).toContain('function Assert-CodexToolDenialProof'); - expect(script).toContain('Assert-CodexToolDenialProof -Proof $codexDenialProof'); - expect(script).toContain('Invoke-CodexProofValidatorSelfTest'); - expect(script).toContain("$fixture.schemaVersion = '1'"); - expect(script).toContain('$fixture.pass = 1'); - expect(script).toContain('$fixture.hooks.extraCount = 1'); - expect(script).toContain('$fixture.green.sensitiveDataObserved = $true'); - expect(script).toContain('$fixture.paidInvocation.toolEventsObserved = 1'); - expect(script).toContain("-Marker $codexMarker -Source 'codex' -SessionId $codexSessionId"); - - expect(script).toContain('modelCalls = 3'); - expect(script).toContain('authStatusCalls = 3'); - expect(script).toContain('authFilesReadByHarness = 0'); - expect(script).toContain('authFilesCopied = 0'); - expect(script).toContain('authContentsSerialized = $false'); - expect(script).toContain( - 'codexAlternativeEnvironmentNamesBlanked = @($codexAlternativeAuthNames)', - ); - expect(script).toContain('method = \'chatgpt\''); - expect(script).toContain('paidModelCallsRequired = 1'); - expect(script).toContain('usageReceiptRequired = $false'); - expect(script).toContain('preCallCostCapAvailable = $false'); - expect(script).toContain('markerSha256 = Get-Sha256Text $codexMarker'); - expect(script).toContain('sessionIdSha256 = Get-Sha256Text $codexSessionId'); - expect(script).toContain('stdoutBytes = $codexRaw.StdoutBytes'); - expect(script).toContain('stdoutSha256 = $codexRaw.StdoutSha256'); - expect(script).toContain('stderrBytes = $codexRaw.StderrBytes'); - expect(script).toContain('stderrSha256 = $codexRaw.StderrSha256'); - expect(script).toContain('reportSha256 = $codexReportSha256'); - expect(script).toContain('invocationArgumentsSha256 = [string]$codexDenialProof.invocation.argumentsSha256'); - expect(script).toContain('hookGraphSha256 = [string]$codexDenialProof.hooks.graphSha256'); - expect(script).toContain('modelCatalogSha256 = [string]$codexDenialProof.paidInvocation.modelCatalogSha256'); - expect(script).toContain('paidThreadParamsSha256 = [string]$codexDenialProof.paidInvocation.threadParamsSha256'); - expect(script).toContain('paidTurnParamsSha256 = [string]$codexDenialProof.paidInvocation.turnParamsSha256'); - expect(script).toContain('mcpBoundarySha256 = [string]$codexDenialProof.mcpBoundary.postPaidSha256'); - expect(script).toContain('packagedHookArtifactsSha256 = [string]$codexDenialProof.hooks.artifactsSha256'); - expect(script).toContain('windowsPowerShellSha256 = [string]$codexDenialProof.artifacts.windowsPowerShellSha256'); - - for (const feature of [ - 'shell_tool', - 'unified_exec', - 'apps', - 'browser_use', - 'browser_use_external', - 'browser_use_full_cdp_access', - 'computer_use', - 'image_generation', - 'in_app_browser', - 'multi_agent', - 'multi_agent_v2', - 'goals', - 'skill_search', - 'tool_suggest', - 'workspace_dependencies', - 'skill_mcp_dependency_install', - 'plugins', - 'plugin_sharing', - 'remote_plugin', - 'mentions_v2', - ]) { - expect(denialProof).toContain(`'${feature}'`); - } - expect(denialProof).toContain("args.push('--disable', feature)"); - expect(denialProof).toContain("'web_search=\"disabled\"'"); - expect(denialProof).toContain("'tools.update_plan.enabled=false'"); - expect(denialProof).toContain("'tools.experimental_request_user_input.enabled=false'"); - expect(denialProof).toContain("'orchestrator.skills.enabled=false'"); - expect(denialProof).toContain("'orchestrator.mcp.enabled=false'"); - expect(denialProof).toContain('apply_patch_tool_type = null'); - expect(denialProof).toContain('use_responses_lite = false'); - expect(denialProof).toContain('dynamicTools: []'); - expect(denialProof).toContain('environments: []'); - expect(denialProof).toContain('allowProviderModelFallback: false'); - expect(denialProof).toContain( - '...(options.useDefaultEnvironmentForControl ? {} : { environments: [] })', - ); - expect(denialProof).toContain('useDefaultEnvironmentForControl: true'); - expect(denialProof).toContain("const controlModel = `waggle-control-${sha256(model).slice(0, 16)}`"); - expect(denialProof).toContain('selectedCapabilityRoots: []'); - expect(denialProof).toContain("matcher='*'"); - expect(denialProof).toContain("permissionDecision: 'deny'"); - expect(denialProof).toContain('permissionDecisionReason: DENIAL_REASON'); - expect(denialProof).toContain("hook.eventName === 'preToolUse'"); - expect(denialProof).toContain("JSON.stringify(redTools) === JSON.stringify(['view_image'])"); - expect(denialProof).toContain('sealedRequest.additionalToolCount === 0'); - expect(denialProof).toContain('sealedRequest.topLevelToolsIsArray'); - expect(denialProof).toContain('assertExactHookGraph'); - expect(denialProof).toContain('buildExpectedWaggleHooks'); - expect(denialProof).toContain('hook.commandSha256 === expectedWaggleCommands.get(hook.eventName)'); - expect(denialProof).toContain("hookStateEntries.push(`'${key}'={enabled=false}`)"); - expect(denialProof).toContain("hookStateEntries.push(`'${key}'={enabled=true,trusted_hash='${currentHash}'}`)"); - expect(denialProof).toContain("`hooks.state={${hookStateEntries.join(',')}}`"); - expect(denialProof).toContain('hooks.state= !hook.enabled)'); - expect(denialProof).toContain('verifyWindowsPowerShell'); - expect(denialProof).toContain('model_reasoning_effort="low"'); - expect(denialProof).toContain('model_provider="openai"'); - expect(denialProof).not.toContain('model_providers.openai.'); - expect(denialProof).not.toContain('waggle_chatgpt'); - const allowedNotificationPolicyStart = denialProof.indexOf( - 'const ALLOWED_NOTIFICATION_METHODS = new Set([', - ); - const allowedNotificationPolicy = denialProof.slice( - allowedNotificationPolicyStart, - denialProof.indexOf(']);', allowedNotificationPolicyStart) + 3, - ); - expect(allowedNotificationPolicy).toContain("'remoteControl/status/changed'"); - expect(allowedNotificationPolicy).not.toContain("'model/rerouted'"); - expect(denialProof).toContain("entry.message?.method === 'model/rerouted'"); - expect(denialProof).toContain('const FAIL_CLOSED_PROJECT_TRUST_WARNING ='); - expect(denialProof).toContain('function isFailClosedProjectTrustWarning(notification)'); - expect(denialProof).toContain( - "const allowedKeys = new Set(['details', 'path', 'range', 'summary']);", - ); - expect(denialProof).toContain('if (!keys.every((key) => allowedKeys.has(key))) return false;'); - expect(denialProof).toContain("if (lines.at(-1) !== '') return false;"); - expect(denialProof).toContain('&& !isFailClosedProjectTrustWarning(entry)'); - const auditFailureInvariantStart = denialProof.indexOf( - 'const EVENT_AUDIT_FAILURE_INVARIANTS = new Set([', - ); - const auditFailureInvariants = denialProof.slice( - auditFailureInvariantStart, - denialProof.indexOf(']);', auditFailureInvariantStart) + 3, - ); - for (const invariant of [ - 'failure-notification', - 'wrong-scope', - 'model-reroute', - 'unknown-notification-method', - 'unknown-item-type', - 'forbidden-tool-item', - 'agent-message-cardinality', - ]) { - expect(auditFailureInvariants).toContain(`'${invariant}'`); - } - const auditFailureKeysStart = denialProof.indexOf( - 'const EVENT_AUDIT_FAILURE_KEYS = [', - ); - const auditFailureKeys = denialProof.slice( - auditFailureKeysStart, - denialProof.indexOf('];', auditFailureKeysStart) + 2, - ); - for (const key of [ - 'invariant', - 'notificationCount', - 'failureEventsObserved', - 'wrongScopeEventsObserved', - 'rerouteEventsObserved', - 'unknownNotificationEventsObserved', - 'unknownItemEventsObserved', - 'toolEventsObserved', - 'notificationGraphSha256', - 'rejectedMethodSha256', - 'rejectedItemTypeSha256', - ]) { - expect(auditFailureKeys).toContain(`'${key}'`); - } - expect(denialProof).toContain('eventAuditFailure: null'); - expect(denialProof).toContain( - 'report.eventAuditFailure = sanitizeEventAuditFailure(error?.eventAuditFailure);', - ); - expect(denialProof).toContain('sensitiveDataObserved: false'); - expect(denialProof).toContain('paidCalls: 0'); - expect(denialProof).not.toContain('dangerously-bypass-hook-trust'); - - const receiptSource = script.slice(script.indexOf('$receipt = [ordered]@{')); - expect(receiptSource).not.toContain('$codexAuthText'); - expect(receiptSource).not.toContain('marker = $codexMarker'); - expect(receiptSource).not.toContain('sessionId = $codexSessionId'); - expect(script).not.toContain('$sourceCodexAuth'); - expect(script).not.toContain('$isolatedCodexAuth'); - expect(script).not.toContain("Join-Path $env:CODEX_HOME 'auth.json'"); - expect(script).not.toContain('Copy-Item'); - expect(script).not.toContain('rawStdout = $codexRaw.Stdout'); - expect(script).not.toContain('rawStderr = $codexRaw.Stderr'); - expect(script).not.toContain('rawAuthOutput ='); - expect(script).not.toContain('$codexPrompt'); - expect(script).not.toContain('$invalidCodexLines'); - - const helperSelfTest = spawnSync( - process.execPath, - [path.join(ROOT, 'scripts', 'verify-codex-tool-denial.mjs'), '--self-test'], - { cwd: ROOT, encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(helperSelfTest.status, helperSelfTest.stderr).toBe(0); - expect(JSON.parse(helperSelfTest.stdout)).toMatchObject({ - pass: true, - paidCalls: 0, - cases: 54, - }); - - if (process.platform === 'win32') { - const validatorSelfTest = spawnSync( - powershellProbeExecutable(), - [ - '-NoLogo', - '-NoProfile', - '-NonInteractive', - '-File', - path.join(ROOT, 'scripts', 'test-windows-official-auth-canaries.ps1'), - '-ExpectedHead', - '0000000000000000000000000000000000000000', - '-ReceiptDir', - path.join(os.tmpdir(), 'unused-waggle-codex-proof-validator-self-test'), - '-CodexProofValidatorSelfTest', - ], - { cwd: ROOT, encoding: 'utf-8', timeout: 30_000, windowsHide: true }, - ); - expect(validatorSelfTest.status, validatorSelfTest.stderr).toBe(0); - expect(JSON.parse(validatorSelfTest.stdout)).toMatchObject({ - pass: true, - paidCalls: 0, - cases: 52, - }); - } - }); - - it('stages Codex official setup before any model invocation', () => { - const script = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-official-auth-canaries.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const denialProof = fs.readFileSync( - path.join(ROOT, 'scripts', 'verify-codex-tool-denial.mjs'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - - expect(denialProof).toContain('const CANARY_STAGES = new Set(['); - for (const stage of [ - 'app-server-spawn', - 'initialize', - 'hooks-list', - 'pre-turn-boundary', - 'thread-start', - 'turn-start', - 'turn-completed', - 'event-audit', - 'app-server-close', - 'post-turn-invariants', - ]) { - expect(denialProof).toContain(`'${stage}'`); - } - expect(denialProof).toContain('function sanitizeProtocolCode(value)'); - expect(denialProof).toContain('value >= -2_147_483_648'); - expect(denialProof).toContain('value <= 2_147_483_647'); - expect(denialProof).toContain('const FAILURE_NOTIFICATION_METHODS = new Set(['); - for (const method of [ - 'error', - 'warning', - 'guardianWarning', - 'configWarning', - 'deprecationNotice', - ]) { - expect(denialProof).toContain(`'${method}'`); - } - for (const method of [ - 'account/updated', - 'mcpServer/startupStatus/updated', - 'model/safetyBuffering/updated', - 'model/verification', - 'thread/name/updated', - 'thread/settings/updated', - 'turn/moderationMetadata', - ]) { - expect(denialProof).toContain(`'${method}'`); - } - expect(denialProof).not.toContain("'rawResponse/completed'"); - expect(denialProof).not.toContain("'rawResponseItem/completed'"); - expect(denialProof).toContain("const setupOnly = flags['setup-only'] === true;"); - expect(denialProof).toContain('if (options.setupOnly) {'); - expect(denialProof).toContain('modelCalls: 0'); - expect(denialProof).toContain('turnStartCalls: 0'); - expect(denialProof).toContain("completedStage: 'thread-start'"); - expect(denialProof).toContain('failureStage: report.diagnostic.failureStage'); - expect(denialProof).toContain('protocolCode: report.diagnostic.protocolCode'); - - const setupBranchStart = denialProof.indexOf('if (options.setupOnly) {'); - const paidTurnStart = denialProof.indexOf("client.send({ method: 'turn/start'", setupBranchStart); - expect(setupBranchStart).toBeGreaterThan(-1); - expect(paidTurnStart).toBeGreaterThan(setupBranchStart); - expect(denialProof.slice(setupBranchStart, paidTurnStart)).not.toContain("method: 'turn/start'"); - - const closeCall = denialProof.indexOf('await client.close();', setupBranchStart); - const finalAudit = denialProof.indexOf('const finalEvents = eventAudit(', closeCall); - const finalProtocolCheck = denialProof.indexOf( - "'Codex app-server protocol failed after close'", - finalAudit, - ); - expect(closeCall).toBeGreaterThan(setupBranchStart); - expect(finalAudit).toBeGreaterThan(closeCall); - expect(finalProtocolCheck).toBeGreaterThan(finalAudit); - - expect(denialProof).toContain( - 'proof: { executed: false, paidCalls: 0, pass: null }', - ); - expect(denialProof).toContain( - 'report.proof = { executed: true, paidCalls: 0, pass: true };', - ); - expect(denialProof).toContain('proofStateSatisfied(report.proof, setupOnly)'); - - expect(script).toContain('function Assert-CodexSetupPreflight'); - expect(script).toContain('function Assert-CodexFailureStage'); - expect(script).toContain('function Assert-NullableProtocolCode'); - expect(script).toContain("'--setup-only'"); - expect(script).toContain('$codexSetupRaw = Invoke-CapturedProcess'); - expect(script).toContain('Assert-CodexSetupPreflight'); - expect(script).toContain('stage = $failureStage'); - expect(script).toContain('protocolCode = $protocolCode'); - expect(script).toContain( - "Assert-JsonBoolean -Value $Report.proof.executed -Expected $false `", - ); - expect(script).toContain("if ($null -ne $Report.proof.pass) {"); - expect(script).not.toContain("-Label 'Codex setup offline proof pass'"); - - const codexAuth = script.indexOf('$codexAuthRaw = Invoke-CapturedProcess'); - const hermesAuth = script.indexOf('$hermesAuthRaw = Invoke-CapturedProcess'); - const codexSetup = script.indexOf('$codexSetupRaw = Invoke-CapturedProcess'); - const claudeMarker = script.indexOf('$claudeMarker ='); - const claudeRun = script.indexOf('$claudeRaw = Invoke-CapturedProcess'); - const codexRun = script.indexOf('$codexRaw = Invoke-CapturedProcess'); - const hermesRun = script.indexOf('$hermesRaw = Invoke-CapturedProcess'); - expect(codexAuth).toBeGreaterThan(-1); - expect(hermesAuth).toBeGreaterThan(codexAuth); - expect(codexSetup).toBeGreaterThan(hermesAuth); - expect(claudeMarker).toBeGreaterThan(codexSetup); - expect(claudeRun).toBeGreaterThan(claudeMarker); - expect(codexRun).toBeGreaterThan(claudeRun); - expect(hermesRun).toBeGreaterThan(codexRun); - }); - - it('preserves only a whitelisted Codex diagnostic after a non-zero child', () => { - const script = fs.readFileSync( - path.join(ROOT, 'scripts', 'test-windows-official-auth-canaries.ps1'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - const denialProof = fs.readFileSync( - path.join(ROOT, 'scripts', 'verify-codex-tool-denial.mjs'), - 'utf-8', - ).replace(/\r\n/g, '\n'); - - expect(denialProof).toContain('function sanitizeTurnFailure'); - expect(denialProof).toContain('turnFailure: null'); - expect(denialProof).toContain('report.turnFailure = error?.turnFailure ?? null'); - expect(denialProof).toContain("client.notification('turn/completed', (entry) => ("); - expect(denialProof).toContain('entry.message?.params?.threadId === threadId'); - expect(denialProof).toContain('entry.message?.params?.turn?.id === turnId'); - expect(denialProof).toContain('turnFailure: report.turnFailure'); - - expect(script).toContain('function Assert-SanitizedCodexTurnFailure'); - expect(script).toContain('function New-SanitizedCodexFailureReceipt'); - expect(script).toContain("kind = 'windows-official-auth-codex-failure'"); - expect(script).toContain("Join-Path $receiptLayout.Staging 'official-auth-failure.json'"); - expect(script).toContain('turnFailure = Assert-SanitizedCodexTurnFailure'); - expect(script).not.toContain('failureReceipt.rawStdout'); - expect(script).not.toContain('failureReceipt.rawStderr'); - expect(script).not.toContain('failureReceipt.message'); - expect(script).not.toContain('failureReceipt.additionalDetails'); - - const codexRun = script.indexOf('$codexRaw = Invoke-CapturedProcess'); - const failureBranch = script.indexOf( - 'if ($codexRaw.TimedOut -or $codexRaw.ExitCode -ne 0)', - codexRun, - ); - const cleanup = script.indexOf('Remove-OwnedDirectory -Path $tempRoot', failureBranch); - const failureWrite = script.indexOf("'official-auth-failure.json'", cleanup); - const publish = script.indexOf('$published = $true', failureWrite); - const nonZero = script.indexOf('Assert-ProcessPassed -Result $codexRaw', publish); - expect(codexRun).toBeGreaterThan(-1); - expect(failureBranch).toBeGreaterThan(codexRun); - expect(cleanup).toBeGreaterThan(failureBranch); - expect(failureWrite).toBeGreaterThan(cleanup); - expect(publish).toBeGreaterThan(failureWrite); - expect(nonZero).toBeGreaterThan(publish); - }); }); describe('Release workflow (release.yml)', () => { diff --git a/scripts/test-windows-external-agents.ps1 b/scripts/test-windows-external-agents.ps1 deleted file mode 100644 index bf35de1..0000000 --- a/scripts/test-windows-external-agents.ps1 +++ /dev/null @@ -1,1231 +0,0 @@ -[CmdletBinding()] -param( - [string[]]$HostIds = @(), - [string]$ReceiptDir = '', - [string]$RunnerNode = '', - [switch]$AuthenticatedTasks -) - -$ErrorActionPreference = 'Stop' -if ($env:OS -ne 'Windows_NT') { - throw 'This guarded real-agent runner is Windows-only.' -} - -if ($AuthenticatedTasks) { - throw 'AuthenticatedTasks is disabled; use scripts/test-windows-official-auth-canaries.ps1 for no-copy user-auth evidence.' -} - -function Resolve-ReceiptLayout([string]$RequestedReceiptDir) { - if ([string]::IsNullOrWhiteSpace($RequestedReceiptDir)) { return $null } - $fullPath = [IO.Path]::GetFullPath($RequestedReceiptDir) - $trimmedPath = $fullPath.TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) - $trimmedRoot = [IO.Path]::GetPathRoot($fullPath).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) - if ([string]::IsNullOrWhiteSpace($trimmedPath) -or - $trimmedPath.Equals($trimmedRoot, [StringComparison]::OrdinalIgnoreCase)) { - throw 'ReceiptDir must name a child directory, not a filesystem root.' - } - $parent = [IO.Path]::GetFullPath([IO.Path]::GetDirectoryName($trimmedPath)) - $leaf = [IO.Path]::GetFileName($trimmedPath) - if ([string]::IsNullOrWhiteSpace($leaf)) { - throw 'ReceiptDir must have a non-empty final directory name.' - } - return [pscustomobject]@{ - Root = $trimmedPath - Parent = $parent - StagingRoot = Join-Path $parent ( - ".$leaf.staging-$([guid]::NewGuid().ToString('N'))" - ) - } -} - -$repoRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..')).Path -$script:runnerNodePath = if ([string]::IsNullOrWhiteSpace($RunnerNode)) { - (Get-Command node.exe -ErrorAction Stop).Source -} else { - (Resolve-Path -LiteralPath $RunnerNode -ErrorAction Stop).Path -} -$script:playwrightCli = (Resolve-Path -LiteralPath ( - Join-Path $repoRoot 'node_modules\playwright\cli.js' -)).Path -$script:vitestCli = (Resolve-Path -LiteralPath ( - Join-Path $repoRoot 'node_modules\vitest\vitest.mjs' -)).Path -$tempBase = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()) -$runRoot = Join-Path $tempBase ("waggle-windows-external-agents-" + [guid]::NewGuid().ToString('N')) -$hookProfile = Join-Path $runRoot 'hook-profile' -$receiptLayout = Resolve-ReceiptLayout -RequestedReceiptDir $ReceiptDir -$receiptRoot = if ($null -eq $receiptLayout) { $null } else { $receiptLayout.Root } -if ($AuthenticatedTasks -and $null -eq $receiptRoot) { - throw 'AuthenticatedTasks requires a fresh ReceiptDir for durable release evidence.' -} -$rawReceiptRoot = Join-Path $runRoot 'raw-receipts' -$receiptParent = if ($null -eq $receiptLayout) { $null } else { $receiptLayout.Parent } -$receiptStagingRoot = if ($null -eq $receiptLayout) { $null } else { $receiptLayout.StagingRoot } -$receiptStagingOwned = $false -$expectedReceiptNames = @('hooks-report.json', 'tools-report.json') -if ($AuthenticatedTasks) { $expectedReceiptNames += 'authenticated-tasks-report.json' } -$originalEnvironment = @{} -$profileVariables = @( - 'USERPROFILE', 'HOME', 'HOMEDRIVE', 'HOMEPATH', 'APPDATA', 'LOCALAPPDATA', - 'CLAUDE_CONFIG_DIR', 'CODEX_HOME', 'HERMES_HOME', 'HERMES_PROFILE' -) -$secretVariables = @( - 'ANTHROPIC_API_KEY', - 'ANTHROPIC_AUTH_TOKEN', - 'CLAUDE_CODE_OAUTH_TOKEN', - 'OPENAI_API_KEY', - 'OPENAI_ACCESS_TOKEN', - 'OPENROUTER_API_KEY', - 'GOOGLE_API_KEY', - 'GEMINI_API_KEY', - 'GROQ_API_KEY', - 'XAI_API_KEY', - 'MISTRAL_API_KEY', - 'COHERE_API_KEY', - 'DEEPSEEK_API_KEY', - 'AZURE_OPENAI_API_KEY', - 'AWS_ACCESS_KEY_ID', - 'AWS_SECRET_ACCESS_KEY', - 'AWS_SESSION_TOKEN', - 'AWS_PROFILE', - 'AWS_CONFIG_FILE', - 'AWS_SHARED_CREDENTIALS_FILE', - 'GOOGLE_APPLICATION_CREDENTIALS', - 'CLOUDSDK_CONFIG', - 'AZURE_CONFIG_DIR', - 'KUBECONFIG', - 'DOCKER_CONFIG', - 'DOCKER_HOST', - 'GITHUB_TOKEN', - 'GH_TOKEN', - 'STRIPE_SECRET_KEY', - 'DATABASE_URL', - 'NPM_TOKEN', - 'HF_TOKEN', - 'HUGGING_FACE_HUB_TOKEN', - 'RENDER_API_KEY', - 'SSH_AUTH_SOCK', - 'GIT_ASKPASS', - 'SSH_ASKPASS', - 'GIT_SSH_COMMAND', - 'HTTP_PROXY', - 'HTTPS_PROXY', - 'ALL_PROXY', - 'NODE_OPTIONS' -) -$secretNamePattern = '(?i)(^|_)(API_KEY|ACCESS_TOKEN|AUTH_TOKEN|TOKEN|SECRET|PASSWORD|CREDENTIALS?|COOKIE|DSN)(_|$)' -$ambientSecretVariables = @( - Get-ChildItem Env: | - Where-Object { $_.Name -match $secretNamePattern } | - ForEach-Object { $_.Name } -) -$secretVariables = @($secretVariables + $ambientSecretVariables | Select-Object -Unique) -$runnerVariables = @( - 'WAGGLE_E2E_HOST_IDS', - 'WAGGLE_E2E_REAL_HOOKS', - 'WAGGLE_E2E_HOOK_HOME', - 'WAGGLE_E2E_REAL_TOOLS', - 'WAGGLE_E2E_TEMP_ROOT', - 'WAGGLE_E2E_DATA_DIR', - 'WAGGLE_E2E_PORT', - 'WAGGLE_E2E_BASE_URL', - 'WAGGLE_E2E_SKIP_LITELLM', - 'WAGGLE_E2E_REUSE_EXISTING_SERVER', - 'PLAYWRIGHT_JSON_OUTPUT_FILE', - 'PATH', - 'WAGGLE_LIVE_EXTERNAL_AGENTS', - 'WAGGLE_LIVE_HERMES_PROVIDER', - 'WAGGLE_LIVE_HERMES_MODEL' -) -$environmentToRestore = @($profileVariables + $secretVariables + $runnerVariables | Select-Object -Unique) -$requestedHostIds = if ($HostIds.Count -eq 0) { - $null -} else { - $emptyHostIds = @( - $HostIds | Where-Object { $_ -match '(^|,)\s*(?=,|$)' } - ) - if ($emptyHostIds.Count -gt 0) { - throw 'HostIds cannot contain empty values.' - } - $normalizedHostIds = @( - $HostIds | - ForEach-Object { $_ -split ',' } | - ForEach-Object { $_.Trim() } - ) - $duplicateHostIds = @( - $normalizedHostIds | - Group-Object | - Where-Object { $_.Count -gt 1 } | - ForEach-Object { $_.Name } - ) - if ($duplicateHostIds.Count -gt 0) { - throw "HostIds cannot contain duplicate values: $($duplicateHostIds -join ', ')" - } - $normalizedHostIds -join ',' -} -$authenticatedHostIds = @('claude-code', 'codex', 'hermes') -if ($AuthenticatedTasks) { - if ($null -eq $requestedHostIds) { - $requestedHostIds = $authenticatedHostIds -join ',' - } - $selectedAuthenticatedHosts = @($requestedHostIds -split ',') - $hostDelta = @(Compare-Object -ReferenceObject $authenticatedHostIds -DifferenceObject $selectedAuthenticatedHosts) - if ($hostDelta.Count -gt 0 -or $selectedAuthenticatedHosts.Count -ne $authenticatedHostIds.Count) { - throw "AuthenticatedTasks requires exactly: $($authenticatedHostIds -join ', ')" - } - $requestedHostIds = $authenticatedHostIds -join ',' -} -$receiptHostIds = if ($null -eq $requestedHostIds) { @() } else { @($requestedHostIds -split ',') } - -foreach ($name in $environmentToRestore) { - $item = Get-Item -LiteralPath "Env:$name" -ErrorAction SilentlyContinue - $originalEnvironment[$name] = if ($null -eq $item) { $null } else { $item.Value } -} - -function Set-ProcessEnvironment([string]$Name, [AllowNull()][string]$Value) { - [Environment]::SetEnvironmentVariable($Name, $Value, 'Process') -} - -function Restore-ProcessEnvironment([string]$Name) { - Set-ProcessEnvironment -Name $Name -Value $originalEnvironment[$Name] -} - -function Assert-NoReparsePointInPath([string]$Path, [string]$FailureMessage) { - $current = [IO.Path]::GetFullPath($Path) - while (-not [string]::IsNullOrWhiteSpace($current)) { - $item = $null - try { - $item = Get-Item -LiteralPath $current -Force -ErrorAction Stop - } catch [System.Management.Automation.ItemNotFoundException] { - $item = $null - } - if ($null -ne $item -and ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw "$FailureMessage`: $current" - } - $parent = [IO.Path]::GetDirectoryName($current) - if ([string]::IsNullOrWhiteSpace($parent) -or - $parent.Equals($current, [StringComparison]::OrdinalIgnoreCase)) { - break - } - $current = $parent - } -} - -function Copy-IsolatedAuthenticationFile( - [string]$Source, - [string]$Destination, - [string]$OwnedRoot -) { - $sourceItem = Get-Item -LiteralPath $Source -Force -ErrorAction Stop - if ($sourceItem.PSIsContainer -or - ($sourceItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -or - $sourceItem.Length -le 0) { - throw 'Authentication source must be a non-empty regular file' - } - $resolvedOwnedRoot = [IO.Path]::GetFullPath($OwnedRoot).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) + [IO.Path]::DirectorySeparatorChar - $resolvedDestination = [IO.Path]::GetFullPath($Destination) - if (-not $resolvedDestination.StartsWith($resolvedOwnedRoot, [StringComparison]::OrdinalIgnoreCase)) { - throw 'Authentication copy escaped the owned run profile' - } - $destinationParent = [IO.Path]::GetDirectoryName($resolvedDestination) - Assert-NoReparsePointInPath -Path $destinationParent ` - -FailureMessage 'Authentication copy path contains a reparse point' - $null = New-Item -ItemType Directory -Path $destinationParent -Force - Assert-NoReparsePointInPath -Path $destinationParent ` - -FailureMessage 'Authentication copy path contains a reparse point' - [IO.File]::Copy($sourceItem.FullName, $resolvedDestination, $false) - $destinationItem = Get-Item -LiteralPath $resolvedDestination -Force - if ($destinationItem.PSIsContainer -or - ($destinationItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -or - $destinationItem.Length -ne $sourceItem.Length) { - throw 'Isolated authentication copy failed verification' - } -} - -function New-IsolatedCodexShim( - [string]$ShimRoot, - [string]$CodexHome, - [string]$OwnedRoot -) { - $resolvedOwnedRoot = [IO.Path]::GetFullPath($OwnedRoot).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) + [IO.Path]::DirectorySeparatorChar - $resolvedShimRoot = [IO.Path]::GetFullPath($ShimRoot) - $resolvedCodexHome = [IO.Path]::GetFullPath($CodexHome) - foreach ($candidate in @($resolvedShimRoot, $resolvedCodexHome)) { - if (-not ($candidate + [IO.Path]::DirectorySeparatorChar).StartsWith( - $resolvedOwnedRoot, - [StringComparison]::OrdinalIgnoreCase - )) { - throw 'Codex isolation path escaped the owned run profile' - } - Assert-NoReparsePointInPath -Path $candidate ` - -FailureMessage 'Codex isolation path contains a reparse point' - } - $realCodexCmd = (Get-Command codex.cmd -CommandType Application -ErrorAction Stop | - Select-Object -First 1).Source - $realCodexCmdItem = Get-Item -LiteralPath $realCodexCmd -Force - if ($realCodexCmdItem.PSIsContainer -or - ($realCodexCmdItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw 'Codex npm command must be a regular file' - } - $cmdText = [IO.File]::ReadAllText($realCodexCmdItem.FullName) - $entryMatch = [regex]::Match( - $cmdText, - '"%dp0%[\\/]+([^"\r\n]*node_modules[\\/]@openai[\\/]codex[\\/]bin[\\/]codex\.js)"', - [Text.RegularExpressions.RegexOptions]::IgnoreCase - ) - if (-not $entryMatch.Success) { - throw 'Codex npm command did not expose a shell-free JavaScript entrypoint' - } - $realCodexEntry = [IO.Path]::GetFullPath(( - Join-Path ([IO.Path]::GetDirectoryName($realCodexCmdItem.FullName)) $entryMatch.Groups[1].Value - )) - $realCodexEntryItem = Get-Item -LiteralPath $realCodexEntry -Force - if ($realCodexEntryItem.PSIsContainer -or - ($realCodexEntryItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw 'Codex JavaScript entrypoint must be a regular file' - } - $null = New-Item -ItemType Directory -Path $resolvedShimRoot - Assert-NoReparsePointInPath -Path $resolvedShimRoot ` - -FailureMessage 'Codex isolation path contains a reparse point' - $launcherPath = Join-Path $resolvedShimRoot 'codex-isolated.mjs' - $shimPath = Join-Path $resolvedShimRoot 'codex.cmd' - $encodedCodexHome = ConvertTo-Json -InputObject $resolvedCodexHome -Compress - $encodedCodexEntry = ConvertTo-Json -InputObject $realCodexEntryItem.FullName -Compress - $launcher = @" -import { spawn } from 'node:child_process'; -process.env.CODEX_HOME = $encodedCodexHome; -const child = spawn(process.execPath, [$encodedCodexEntry, ...process.argv.slice(2)], { - env: process.env, - stdio: 'inherit', - windowsHide: true, -}); -child.once('error', (error) => { - console.error(error instanceof Error ? error.message : String(error)); - process.exit(1); -}); -child.once('exit', (code) => process.exit(code ?? 1)); -"@ - $shim = @' -@ECHO off -SET "_prog=node" -"%_prog%" "%dp0%\codex-isolated.mjs" %* -'@ - [IO.File]::WriteAllText($launcherPath, $launcher, [Text.UTF8Encoding]::new($false)) - [IO.File]::WriteAllText($shimPath, $shim, [Text.ASCIIEncoding]::new()) - return [pscustomobject]@{ - Directory = $resolvedShimRoot - Launcher = $launcherPath - Shim = $shimPath - } -} - -function New-OwnedHermesProfile( - [string]$ProfilesRoot, - [string]$ProfileName, - [string]$OwnedRoot -) { - $resolvedProfilesRoot = [IO.Path]::GetFullPath($ProfilesRoot) - $resolvedOwnedRoot = [IO.Path]::GetFullPath($OwnedRoot).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) - $ownedPrefix = $resolvedOwnedRoot + [IO.Path]::DirectorySeparatorChar - if (-not ($resolvedProfilesRoot + [IO.Path]::DirectorySeparatorChar).StartsWith( - $ownedPrefix, - [StringComparison]::OrdinalIgnoreCase - )) { - throw 'Hermes authenticated profile root escaped the owned run tree' - } - Assert-NoReparsePointInPath -Path $resolvedProfilesRoot ` - -FailureMessage 'Hermes authenticated profile path contains a reparse point' - $profileHome = Join-Path $resolvedProfilesRoot $ProfileName - if (Test-Path -LiteralPath $profileHome) { - throw 'Refusing to overwrite an existing Hermes authenticated profile' - } - - $null = & hermes profile create $ProfileName --no-alias --no-skills - $profileCreateExitCode = $LASTEXITCODE - if ($profileCreateExitCode -ne 0 -or - -not (Test-Path -LiteralPath $profileHome -PathType Container)) { - throw 'Hermes authenticated profile creation failed' - } - Assert-NoReparsePointInPath -Path $profileHome ` - -FailureMessage 'Hermes authenticated profile path contains a reparse point' - $profileItem = Get-Item -LiteralPath $profileHome -Force - $resolvedProfileHome = [IO.Path]::GetFullPath($profileItem.FullName) - if (($profileItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -or - -not [IO.Path]::GetDirectoryName($resolvedProfileHome).Equals( - $resolvedProfilesRoot, - [StringComparison]::OrdinalIgnoreCase - )) { - throw 'Hermes authenticated profile ownership verification failed' - } - $ownerToken = [guid]::NewGuid().ToString('N') - $ownershipMarker = Join-Path $resolvedProfileHome ".waggle-e2e-owner-$ownerToken" - Write-HermesOwnershipMarker -Path $ownershipMarker -Token $ownerToken - return [pscustomobject]@{ - ProfileName = $ProfileName - ProfilesRoot = $resolvedProfilesRoot - ProfileHome = $resolvedProfileHome - OwnershipMarker = $ownershipMarker - OwnerToken = $ownerToken - } -} - -function Write-HermesOwnershipMarker([string]$Path, [string]$Token) { - $markerBytes = [Text.UTF8Encoding]::new($false).GetBytes($Token) - $markerStream = [IO.File]::Open( - $Path, - [IO.FileMode]::CreateNew, - [IO.FileAccess]::Write, - [IO.FileShare]::None - ) - try { - $markerStream.Write($markerBytes, 0, $markerBytes.Length) - } finally { - $markerStream.Dispose() - } -} - -function Remove-OwnedHermesProfile($Lease) { - if ($null -eq $Lease) { return } - $profileItem = Get-Item -LiteralPath $Lease.ProfileHome -Force - $markerItem = Get-Item -LiteralPath $Lease.OwnershipMarker -Force - $resolvedProfileHome = [IO.Path]::GetFullPath($profileItem.FullName) - if (-not $profileItem.PSIsContainer -or - ($profileItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -or - $markerItem.PSIsContainer -or - ($markerItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -or - -not [IO.Path]::GetDirectoryName($resolvedProfileHome).Equals( - [IO.Path]::GetFullPath($Lease.ProfilesRoot), - [StringComparison]::OrdinalIgnoreCase - ) -or - [IO.File]::ReadAllText($markerItem.FullName) -cne $Lease.OwnerToken) { - throw 'Hermes authenticated profile ownership verification failed' - } - $null = & hermes profile delete $Lease.ProfileName -y - $profileDeleteExitCode = $LASTEXITCODE - if ((Test-Path -LiteralPath $Lease.ProfileHome) -or $profileDeleteExitCode -ne 0) { - throw 'Hermes authenticated profile cleanup failed' - } -} - -function Invoke-NativePreflight( - [string]$FilePath, - [string[]]$ArgumentList, - [string]$FailureMessage -) { - $previousErrorActionPreference = $ErrorActionPreference - $exitCode = $null - try { - $ErrorActionPreference = 'Continue' - & $FilePath @ArgumentList *> $null - $exitCode = $LASTEXITCODE - } finally { - $ErrorActionPreference = $previousErrorActionPreference - } - if ($exitCode -ne 0) { throw $FailureMessage } -} - -function Remove-IsolatedAuthenticationFiles( - [string[]]$Paths, - [string]$OwnedRoot -) { - $errors = [Collections.Generic.List[string]]::new() - $resolvedOwnedRoot = [IO.Path]::GetFullPath($OwnedRoot).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) - $ownedPrefix = $resolvedOwnedRoot + [IO.Path]::DirectorySeparatorChar - foreach ($path in $Paths) { - try { - $resolvedPath = [IO.Path]::GetFullPath($path) - if (-not $resolvedPath.StartsWith($ownedPrefix, [StringComparison]::OrdinalIgnoreCase)) { - throw 'Authentication cleanup path escaped the owned run profile' - } - Assert-NoReparsePointInPath -Path $resolvedPath ` - -FailureMessage 'Authentication cleanup path contains a reparse point' - if (-not (Test-Path -LiteralPath $resolvedPath)) { continue } - $item = Get-Item -LiteralPath $resolvedPath -Force - if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw 'Authentication cleanup target must be a regular file' - } - $lastError = $null - for ($attempt = 0; $attempt -lt 8; $attempt += 1) { - try { - [IO.File]::Delete($resolvedPath) - } catch { - $lastError = $_ - } - if (-not (Test-Path -LiteralPath $resolvedPath)) { break } - Start-Sleep -Milliseconds 125 - } - if (Test-Path -LiteralPath $resolvedPath) { - if ($null -ne $lastError) { throw $lastError } - throw 'Authentication cleanup target survived deletion retries' - } - } catch { - $errors.Add("Failed to remove an isolated authentication copy: $($_.Exception.Message)") - } - } - if ($errors.Count -gt 0) { throw ($errors -join '; ') } -} - -function Complete-AuthenticatedIsolationCleanup( - $HermesLease, - [object[]]$SourceAuthEvidence, - [string[]]$ProfileVariables, - [string[]]$IsolatedAuthPaths, - [string]$OwnedRoot -) { - $errors = [Collections.Generic.List[string]]::new() - try { - Set-ProcessEnvironment -Name 'WAGGLE_LIVE_EXTERNAL_AGENTS' -Value $null - } catch { - $errors.Add("Failed to clear the live-agent flag: $($_.Exception.Message)") - } - try { - Remove-OwnedHermesProfile -Lease $HermesLease - } catch { - $errors.Add("Hermes profile cleanup failed: $($_.Exception.Message)") - } - foreach ($name in $ProfileVariables) { - try { - Restore-ProcessEnvironment -Name $name - } catch { - $errors.Add("Failed to restore $name`: $($_.Exception.Message)") - } - } - try { - Remove-IsolatedAuthenticationFiles -Paths $IsolatedAuthPaths -OwnedRoot $OwnedRoot - } catch { - $errors.Add($_.Exception.Message) - } - foreach ($evidence in $SourceAuthEvidence) { - try { - if ((Get-FileHash -LiteralPath $evidence.Path -Algorithm SHA256).Hash -cne $evidence.Hash) { - $errors.Add('A real external-agent authentication source changed during the isolated live test') - } - } catch { - $errors.Add("Could not verify source authentication file integrity: $($_.Exception.Message)") - } - } - if ($errors.Count -gt 0) { - throw "Authenticated isolation cleanup failed: $($errors -join '; ')" - } -} - -function Get-ReceiptStrings($Node) { - if ($null -eq $Node) { return } - if ($Node -is [string]) { - $Node - return - } - if ($Node -is [pscustomobject]) { - foreach ($property in $Node.PSObject.Properties) { - Get-ReceiptStrings -Node $property.Value - } - return - } - if ($Node -is [System.Collections.IEnumerable]) { - foreach ($item in $Node) { - Get-ReceiptStrings -Node $item - } - } -} - -function Test-ReceiptIntegerProperty($Node, [string]$Name) { - if ($Node -isnot [pscustomobject]) { return $false } - $property = $Node.PSObject.Properties[$Name] - if ($null -eq $property) { return $false } - return $property.Value -is [int] -or $property.Value -is [long] -} - -function Test-ReceiptBooleanProperty($Node, [string]$Name) { - if ($Node -isnot [pscustomobject]) { return $false } - $property = $Node.PSObject.Properties[$Name] - return $null -ne $property -and $property.Value -is [bool] -} - -function Assert-SafeReceiptContent($Receipt, [string]$ReceiptText) { - $receiptStrings = @(Get-ReceiptStrings -Node $Receipt) - $absoluteHostPaths = @($receiptStrings | Where-Object { - $_ -match '(?i)(?:^|[\s"(=])(?:[A-Z]:[\\/]|\\\\[^\\/\s]+[\\/]|file:///[A-Z]:/)' - }) - if ($absoluteHostPaths.Count -gt 0) { throw 'Receipt contains absolute host paths' } - - $leakedVariables = @() - foreach ($name in $secretVariables) { - $value = $originalEnvironment[$name] - if ([string]::IsNullOrEmpty($value)) { continue } - $jsonValue = ConvertTo-Json -InputObject $value -Compress - $escapedValue = if ($jsonValue.Length -ge 2) { - $jsonValue.Substring(1, $jsonValue.Length - 2) - } else { $jsonValue } - $leaked = $ReceiptText.Contains($value) -or $ReceiptText.Contains($escapedValue) - foreach ($candidate in $receiptStrings) { - if ($candidate.Contains($value) -or $candidate.Contains($escapedValue)) { $leaked = $true } - if ($candidate.Length -ge 8 -and $candidate.Length % 4 -eq 0 -and - $candidate -match '^[A-Za-z0-9+/]*={0,2}$') { - try { - $decoded = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($candidate)) - if ($decoded.Contains($value) -or $decoded.Contains($escapedValue)) { $leaked = $true } - } catch { - # Not every base64-shaped reporter string is valid base64. - } - } - } - if ($leaked) { $leakedVariables += $name } - } - if ($leakedVariables.Count -gt 0) { - throw ( - 'Unsafe receipt: captured secret values found for environment variables: ' + - (@($leakedVariables | Select-Object -Unique) -join ', ') - ) - } -} - -function Publish-SafeReceipt( - [string]$RawReceiptPath, - [string]$SafeReceiptPath, - [ValidateSet('playwright-summary', 'vitest-summary')][string]$ExpectedKind, - [string]$ExpectedSpec, - [string]$ReceiptLane, - [string[]]$ExpectedHostIds, - [string]$HermesProvider = '', - [string]$HermesModel = '' -) { - $tempProjectionPath = $null - $tempProjectionOwned = $false - try { - if (-not (Test-Path -LiteralPath $RawReceiptPath -PathType Leaf)) { - throw "Expected reporter receipt was not created: $RawReceiptPath" - } - $rawPrefix = [IO.Path]::GetFullPath($rawReceiptRoot).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) + [IO.Path]::DirectorySeparatorChar - $resolvedRaw = [IO.Path]::GetFullPath($RawReceiptPath) - if (-not $resolvedRaw.StartsWith($rawPrefix, [StringComparison]::OrdinalIgnoreCase)) { - throw "Raw reporter receipt escaped the owned run tree: $resolvedRaw" - } - Assert-NoReparsePointInPath -Path $resolvedRaw ` - -FailureMessage 'Raw reporter receipt path contains a reparse point' - $safeParent = [IO.Path]::GetFullPath([IO.Path]::GetDirectoryName($SafeReceiptPath)) - if (-not $safeParent.Equals([IO.Path]::GetFullPath($receiptStagingRoot), [StringComparison]::OrdinalIgnoreCase)) { - throw "Safe receipt escaped the staging directory: $SafeReceiptPath" - } - Assert-NoReparsePointInPath -Path $safeParent ` - -FailureMessage 'Safe receipt path contains a reparse point' - if (Test-Path -LiteralPath $SafeReceiptPath) { - throw "Safe receipt target already exists: $SafeReceiptPath" - } - - try { $rawReceipt = Get-Content -Raw -LiteralPath $resolvedRaw | ConvertFrom-Json } - catch { throw "Invalid reporter receipt JSON: $resolvedRaw" } - $receiptName = [IO.Path]::GetFileNameWithoutExtension($SafeReceiptPath) - if ($receiptName -cne "$ReceiptLane-report") { - throw 'Safe receipt filename did not match its declared lane' - } - $normalizedExpectedSpec = $ExpectedSpec.Replace('\', '/') - $expectedSpecPath = [IO.Path]::GetFullPath((Join-Path $repoRoot $normalizedExpectedSpec)) - if ($ExpectedKind -ceq 'playwright-summary') { - $configProperty = $rawReceipt.PSObject.Properties['config'] - $suitesProperty = $rawReceipt.PSObject.Properties['suites'] - $rootDirProperty = if ($null -ne $configProperty -and $configProperty.Value -is [pscustomobject]) { - $configProperty.Value.PSObject.Properties['rootDir'] - } else { - $null - } - $specEvidence = @( - if ($null -ne $rootDirProperty -and $rootDirProperty.Value -is [string] -and - -not [string]::IsNullOrWhiteSpace($rootDirProperty.Value) -and - $null -ne $suitesProperty) { - $reportRoot = [IO.Path]::GetFullPath($rootDirProperty.Value) - $reportRootPrefix = $reportRoot.TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) + [IO.Path]::DirectorySeparatorChar - if ($expectedSpecPath.StartsWith($reportRootPrefix, [StringComparison]::OrdinalIgnoreCase)) { - $reportRelativeSpec = $expectedSpecPath.Substring($reportRootPrefix.Length).Replace('\', '/') - foreach ($suite in @($suitesProperty.Value)) { - if ($suite -isnot [pscustomobject]) { continue } - $fileProperty = $suite.PSObject.Properties['file'] - if ($null -ne $fileProperty -and $fileProperty.Value -is [string] -and - $fileProperty.Value.Replace('\', '/').Equals( - $reportRelativeSpec, - [StringComparison]::OrdinalIgnoreCase - )) { - $fileProperty.Value - } - } - } - } - ) - } else { - $testResultsProperty = $rawReceipt.PSObject.Properties['testResults'] - if ($null -eq $testResultsProperty -or $testResultsProperty.Value -isnot [array]) { - throw 'Vitest receipt field testResults must be an array' - } - $specEvidence = @( - foreach ($testResult in $testResultsProperty.Value) { - if ($testResult -isnot [pscustomobject]) { - throw 'Vitest receipt testResults entries must be objects' - } - $nameProperty = $testResult.PSObject.Properties['name'] - if ($null -eq $nameProperty -or $nameProperty.Value -isnot [string] -or - [string]::IsNullOrWhiteSpace($nameProperty.Value)) { - throw 'Vitest receipt testResults[].name must be a non-empty string' - } - try { - [IO.Path]::GetFullPath($nameProperty.Value) - } catch { - throw 'Vitest receipt testResults[].name must be a valid path' - } - } - ) - if ($specEvidence.Count -ne 1 -or - -not [string]::Equals( - $specEvidence[0], - $expectedSpecPath, - [StringComparison]::OrdinalIgnoreCase - )) { - throw 'Vitest receipt result-file set did not exactly match the expected specification' - } - } - if ($specEvidence.Count -eq 0) { - throw 'Reporter receipt did not bind the expected test specification' - } - $statsProperty = $rawReceipt.PSObject.Properties['stats'] - $successProperty = $rawReceipt.PSObject.Properties['success'] - $suiteCountProperty = $rawReceipt.PSObject.Properties['numTotalTestSuites'] - if ($null -ne $statsProperty) { - if ($ExpectedKind -cne 'playwright-summary') { - throw 'Reporter receipt kind did not match the declared lane' - } - $stats = $statsProperty.Value - foreach ($name in @('expected', 'unexpected', 'flaky', 'skipped')) { - if (-not (Test-ReceiptIntegerProperty $stats $name)) { - throw "Playwright receipt field $name must be an integer" - } - } - $receipt = [pscustomobject][ordered]@{ - schemaVersion = 1 - kind = 'playwright-summary' - name = $receiptName - lane = $ReceiptLane - spec = $normalizedExpectedSpec - hostIds = @($ExpectedHostIds) - success = ([int]$stats.expected -eq 1 -and [int]$stats.unexpected -eq 0 -and - [int]$stats.flaky -eq 0 -and [int]$stats.skipped -eq 0) - expected = [int]$stats.expected - unexpected = [int]$stats.unexpected - flaky = [int]$stats.flaky - skipped = [int]$stats.skipped - } - } elseif ($null -ne $successProperty -or $null -ne $suiteCountProperty) { - if ($ExpectedKind -cne 'vitest-summary') { - throw 'Reporter receipt kind did not match the declared lane' - } - if (-not (Test-ReceiptBooleanProperty $rawReceipt 'success')) { - throw 'Vitest receipt field success must be a boolean' - } - foreach ($name in @( - 'numTotalTestSuites', 'numPassedTestSuites', 'numFailedTestSuites', 'numPendingTestSuites', - 'numTotalTests', 'numPassedTests', 'numFailedTests', 'numPendingTests', 'numTodoTests' - )) { - if (-not (Test-ReceiptIntegerProperty $rawReceipt $name)) { - throw "Vitest receipt field $name must be an integer" - } - } - $receipt = [pscustomobject][ordered]@{ - schemaVersion = 1 - kind = 'vitest-summary' - name = $receiptName - lane = $ReceiptLane - spec = $normalizedExpectedSpec - hostIds = @($ExpectedHostIds) - hermesProvider = $HermesProvider - hermesModel = $HermesModel - success = ($rawReceipt.success -and - [int]$rawReceipt.numTotalTestSuites -gt 0 -and - [int]$rawReceipt.numPassedTestSuites -eq [int]$rawReceipt.numTotalTestSuites -and - [int]$rawReceipt.numFailedTestSuites -eq 0 -and [int]$rawReceipt.numPendingTestSuites -eq 0 -and - [int]$rawReceipt.numTotalTests -eq 1 -and [int]$rawReceipt.numPassedTests -eq 1 -and - [int]$rawReceipt.numFailedTests -eq 0 -and [int]$rawReceipt.numPendingTests -eq 0 -and - [int]$rawReceipt.numTodoTests -eq 0) - totalTestSuites = [int]$rawReceipt.numTotalTestSuites - passedTestSuites = [int]$rawReceipt.numPassedTestSuites - failedTestSuites = [int]$rawReceipt.numFailedTestSuites - pendingTestSuites = [int]$rawReceipt.numPendingTestSuites - totalTests = [int]$rawReceipt.numTotalTests - passedTests = [int]$rawReceipt.numPassedTests - failedTests = [int]$rawReceipt.numFailedTests - pendingTests = [int]$rawReceipt.numPendingTests - todoTests = [int]$rawReceipt.numTodoTests - } - } else { throw 'Unknown reporter receipt schema' } - if (-not $receipt.success) { throw 'Reporter receipt did not prove an exact successful lane' } - - $receiptText = $receipt | ConvertTo-Json -Depth 4 - Assert-SafeReceiptContent -Receipt $receipt -ReceiptText $receiptText - $tempProjectionPath = "$SafeReceiptPath.partial-$([guid]::NewGuid().ToString('N'))" - $projectionBytes = [Text.UTF8Encoding]::new($false).GetBytes($receiptText) - $projectionStream = [IO.File]::Open( - $tempProjectionPath, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None - ) - $tempProjectionOwned = $true - try { $projectionStream.Write($projectionBytes, 0, $projectionBytes.Length) } - finally { $projectionStream.Dispose() } - [IO.File]::Move($tempProjectionPath, $SafeReceiptPath) - $tempProjectionPath = $null - $tempProjectionOwned = $false - } finally { - if ($tempProjectionOwned -and $tempProjectionPath -and - (Test-Path -LiteralPath $tempProjectionPath -PathType Leaf)) { - Remove-Item -LiteralPath $tempProjectionPath -Force - } - if (Test-Path -LiteralPath $RawReceiptPath -PathType Leaf) { - Remove-Item -LiteralPath $RawReceiptPath -Force - } - } -} - -function Assert-SafeReceiptSet([string]$StagingRoot, [string[]]$ExpectedReceiptNames) { - Assert-NoReparsePointInPath -Path $StagingRoot ` - -FailureMessage 'Receipt staging path contains a reparse point' - $stagingItem = Get-Item -LiteralPath $StagingRoot -Force - if (-not $stagingItem.PSIsContainer -or - ($stagingItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw 'Receipt staging root must be an owned regular directory' - } - $items = @(Get-ChildItem -LiteralPath $StagingRoot -Force) - if (@($items | Where-Object { $_.PSIsContainer -or ($_.Attributes -band [IO.FileAttributes]::ReparsePoint) }).Count -gt 0) { - throw 'Receipt staging contains a directory or reparse point' - } - $actualNames = @($items | ForEach-Object Name | Sort-Object) - $expectedNames = @($ExpectedReceiptNames | Sort-Object) - if (@(Compare-Object -ReferenceObject $expectedNames -DifferenceObject $actualNames -CaseSensitive).Count -gt 0) { - throw "Receipt staging did not contain the exact expected set: $($expectedNames -join ', ')" - } - foreach ($file in $items) { - $text = [IO.File]::ReadAllText($file.FullName) - $receipt = $text | ConvertFrom-Json - if (-not (Test-ReceiptIntegerProperty $receipt 'schemaVersion') -or [int]$receipt.schemaVersion -ne 1 -or - -not (Test-ReceiptBooleanProperty $receipt 'success') -or -not $receipt.success) { - throw "Safe receipt failed final schema validation: $($file.Name)" - } - $contract = switch -CaseSensitive ($file.Name) { - 'hooks-report.json' { - [pscustomobject]@{ - kind = 'playwright-summary' - lane = 'hooks' - spec = 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' - hostIds = @($receiptHostIds) - } - } - 'tools-report.json' { - [pscustomobject]@{ - kind = 'playwright-summary' - lane = 'tools' - spec = 'tests/e2e/launcher-real-tool-lifecycle.spec.ts' - hostIds = @($receiptHostIds) - } - } - 'authenticated-tasks-report.json' { - [pscustomobject]@{ - kind = 'vitest-summary' - lane = 'authenticated-tasks' - spec = 'tests/integration/external-agent-collaboration.live.test.ts' - hostIds = @($authenticatedHostIds) - hermesProvider = 'openai-codex' - hermesModel = 'gpt-5.5' - } - } - default { throw "Safe receipt had no release contract: $($file.Name)" } - } - foreach ($name in @('kind', 'lane', 'spec')) { - $property = $receipt.PSObject.Properties[$name] - if ($null -eq $property -or $property.Value -isnot [string] -or - $property.Value -cne $contract.$name) { - throw "Safe receipt identity mismatch for $($file.Name): $name" - } - } - $hostIdsProperty = $receipt.PSObject.Properties['hostIds'] - $actualHostIds = if ($null -eq $hostIdsProperty) { @() } else { @($hostIdsProperty.Value) } - if ($null -eq $hostIdsProperty -or - @(Compare-Object -ReferenceObject @($contract.hostIds) -DifferenceObject $actualHostIds -CaseSensitive).Count -gt 0 -or - $actualHostIds.Count -ne @($contract.hostIds).Count) { - throw "Safe receipt host roster mismatch for $($file.Name)" - } - if ($file.Name -ceq 'authenticated-tasks-report.json') { - foreach ($name in @('hermesProvider', 'hermesModel')) { - $property = $receipt.PSObject.Properties[$name] - if ($null -eq $property -or $property.Value -isnot [string] -or - $property.Value -cne $contract.$name) { - throw "Safe receipt Hermes identity mismatch for $($file.Name): $name" - } - } - } - Assert-SafeReceiptContent -Receipt $receipt -ReceiptText $text - } -} - -function Publish-ReceiptSet( - [string]$StagingRoot, - [string]$FinalRoot, - [string[]]$ExpectedReceiptNames -) { - if (Test-Path -LiteralPath $FinalRoot) { throw "ReceiptDir became occupied before publication: $FinalRoot" } - Assert-NoReparsePointInPath -Path $StagingRoot ` - -FailureMessage 'Receipt publication staging path contains a reparse point' - Assert-NoReparsePointInPath -Path ([IO.Path]::GetDirectoryName($FinalRoot)) ` - -FailureMessage 'Receipt publication destination path contains a reparse point' - $stagingParent = [IO.Path]::GetFullPath([IO.Path]::GetDirectoryName($StagingRoot)) - $finalParent = [IO.Path]::GetFullPath([IO.Path]::GetDirectoryName($FinalRoot)) - if (-not $stagingParent.Equals($finalParent, [StringComparison]::OrdinalIgnoreCase)) { - throw 'Receipt staging and final directories must be same-volume siblings' - } - Assert-SafeReceiptSet -StagingRoot $StagingRoot -ExpectedReceiptNames $ExpectedReceiptNames - [IO.Directory]::Move($StagingRoot, $FinalRoot) -} - -function Remove-VerifiedReceiptStaging([string]$StagingRoot, [string]$FinalRoot, [bool]$Owned) { - if (-not $Owned -or [string]::IsNullOrWhiteSpace($StagingRoot) -or - -not (Test-Path -LiteralPath $StagingRoot)) { return } - $resolvedStaging = [IO.Path]::GetFullPath($StagingRoot) - $finalParent = [IO.Path]::GetFullPath([IO.Path]::GetDirectoryName($FinalRoot)) - Assert-NoReparsePointInPath -Path $resolvedStaging ` - -FailureMessage 'Receipt cleanup path contains a reparse point' - Assert-NoReparsePointInPath -Path $finalParent ` - -FailureMessage 'Receipt cleanup destination path contains a reparse point' - $stagingParent = [IO.Path]::GetFullPath([IO.Path]::GetDirectoryName($resolvedStaging)) - $expectedPrefix = ".$([IO.Path]::GetFileName($FinalRoot)).staging-" - if (-not $stagingParent.Equals($finalParent, [StringComparison]::OrdinalIgnoreCase) -or - -not [IO.Path]::GetFileName($resolvedStaging).StartsWith($expectedPrefix, [StringComparison]::Ordinal)) { - throw "Refusing receipt staging cleanup outside the owned sibling: $resolvedStaging" - } - $stagingItem = Get-Item -LiteralPath $resolvedStaging -Force - if (-not $stagingItem.PSIsContainer -or - ($stagingItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw "Refusing unsafe receipt staging cleanup: $resolvedStaging" - } - Remove-Item -LiteralPath $resolvedStaging -Recurse -Force -} - -function Get-FreeLoopbackPort { - $listener = [Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, 0) - try { - $listener.Start() - return ([Net.IPEndPoint]$listener.LocalEndpoint).Port - } finally { - $listener.Stop() - } -} - -function Remove-VerifiedTempTree([string]$Target) { - $resolved = [IO.Path]::GetFullPath($Target) - $tempPrefix = $tempBase.TrimEnd([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar) + [IO.Path]::DirectorySeparatorChar - if (-not $resolved.StartsWith($tempPrefix, [StringComparison]::OrdinalIgnoreCase)) { - throw "Refusing recursive cleanup outside the OS temp directory: $resolved" - } - $extended = if ($resolved.StartsWith('\\')) { - '\\?\UNC\' + $resolved.Substring(2) - } else { - '\\?\' + $resolved - } - if (-not [IO.Directory]::Exists($extended)) { return } - - $lastError = $null - for ($attempt = 0; $attempt -lt 40; $attempt += 1) { - try { - $targetItem = Get-Item -LiteralPath $resolved -Force - if (-not $targetItem.PSIsContainer -or - ($targetItem.Attributes -band [IO.FileAttributes]::ReparsePoint)) { - throw "Refusing unsafe temp-tree cleanup: $resolved" - } - [IO.Directory]::Delete($extended, $true) - return - } catch { - if (-not [IO.Directory]::Exists($extended)) { return } - $lastError = $_ - Start-Sleep -Milliseconds 250 - } - } - if (-not [IO.Directory]::Exists($extended)) { return } - throw $lastError -} - -function Invoke-PlaywrightLane([string]$Spec, [string]$DataDir, [string]$ReceiptName) { - $port = Get-FreeLoopbackPort - $rawReceiptPath = $null - $safeReceiptPath = $null - $playwrightOutput = Join-Path $runRoot "playwright-$ReceiptName" - Set-ProcessEnvironment -Name 'WAGGLE_E2E_DATA_DIR' -Value $DataDir - Set-ProcessEnvironment -Name 'WAGGLE_E2E_PORT' -Value ([string]$port) - Set-ProcessEnvironment -Name 'WAGGLE_E2E_BASE_URL' -Value "http://127.0.0.1:$port" - Write-Host "Running $Spec on isolated port $port" - $playwrightArgs = @( - 'test', - $Spec, - '--config=playwright.config.ts', - '--project=chromium', - '--retries=0', - '--output', - $playwrightOutput - ) - if ($null -ne $receiptStagingRoot) { - $rawReceiptPath = Join-Path $rawReceiptRoot "$ReceiptName-report.raw.json" - $safeReceiptPath = Join-Path $receiptStagingRoot "$ReceiptName-report.json" - Set-ProcessEnvironment -Name 'PLAYWRIGHT_JSON_OUTPUT_FILE' -Value $rawReceiptPath - $playwrightArgs += @( - '--reporter=list,json' - ) - } else { - Set-ProcessEnvironment -Name 'PLAYWRIGHT_JSON_OUTPUT_FILE' -Value $null - $playwrightArgs += '--reporter=list' - } - try { - & $script:runnerNodePath $script:playwrightCli @playwrightArgs - $exitCode = $LASTEXITCODE - if ($exitCode -ne 0) { throw "$Spec failed with exit code $exitCode" } - if ($rawReceiptPath) { - Publish-SafeReceipt -RawReceiptPath $rawReceiptPath -SafeReceiptPath $safeReceiptPath ` - -ExpectedKind 'playwright-summary' -ExpectedSpec $Spec -ReceiptLane $ReceiptName ` - -ExpectedHostIds $receiptHostIds - } - } finally { - if ($rawReceiptPath -and (Test-Path -LiteralPath $rawReceiptPath -PathType Leaf)) { - Remove-Item -LiteralPath $rawReceiptPath -Force - } - } -} - -function Invoke-VitestLane( - [string]$Spec, - [string]$ReceiptName, - [string]$HermesProvider, - [string]$HermesModel -) { - $rawReceiptPath = $null - $safeReceiptPath = $null - $vitestArgs = @( - 'run', - $Spec, - '--retry=0', - '--reporter=verbose' - ) - if ($null -ne $receiptStagingRoot) { - $rawReceiptPath = Join-Path $rawReceiptRoot "$ReceiptName-report.raw.json" - $safeReceiptPath = Join-Path $receiptStagingRoot "$ReceiptName-report.json" - $vitestArgs += @( - '--reporter=json', - "--outputFile=$rawReceiptPath" - ) - } - try { - & $script:runnerNodePath $script:vitestCli @vitestArgs - $exitCode = $LASTEXITCODE - if ($exitCode -ne 0) { throw "$Spec failed with exit code $exitCode" } - if ($rawReceiptPath) { - Publish-SafeReceipt -RawReceiptPath $rawReceiptPath -SafeReceiptPath $safeReceiptPath ` - -ExpectedKind 'vitest-summary' -ExpectedSpec $Spec -ReceiptLane $ReceiptName ` - -ExpectedHostIds $receiptHostIds -HermesProvider $HermesProvider -HermesModel $HermesModel - } - } finally { - if ($rawReceiptPath -and (Test-Path -LiteralPath $rawReceiptPath -PathType Leaf)) { - Remove-Item -LiteralPath $rawReceiptPath -Force - } - } -} - -$publicationCompleted = $false -try { - try { - $null = New-Item -ItemType Directory -Path $hookProfile -Force - $null = New-Item -ItemType Directory -Path $rawReceiptRoot -Force - if ($null -ne $receiptRoot) { - if (Test-Path -LiteralPath $receiptRoot) { - throw "ReceiptDir must be a fresh path owned by this run: $receiptRoot" - } - Assert-NoReparsePointInPath -Path $receiptParent ` - -FailureMessage 'ReceiptDir path must not contain a reparse point' - $null = New-Item -ItemType Directory -Path $receiptParent -Force - Assert-NoReparsePointInPath -Path $receiptParent ` - -FailureMessage 'ReceiptDir path must not contain a reparse point' - $null = New-Item -ItemType Directory -Path $receiptStagingRoot - $receiptStagingOwned = $true - } - foreach ($name in $secretVariables) { Set-ProcessEnvironment -Name $name -Value $null } - Set-ProcessEnvironment -Name 'WAGGLE_E2E_SKIP_LITELLM' -Value '1' - Set-ProcessEnvironment -Name 'WAGGLE_E2E_REUSE_EXISTING_SERVER' -Value '0' - Set-ProcessEnvironment -Name 'WAGGLE_E2E_TEMP_ROOT' -Value $runRoot - Set-ProcessEnvironment -Name 'WAGGLE_E2E_HOST_IDS' -Value $requestedHostIds - Set-ProcessEnvironment -Name 'PLAYWRIGHT_JSON_OUTPUT_FILE' -Value $null - - Push-Location $repoRoot - try { - Set-ProcessEnvironment -Name 'USERPROFILE' -Value $hookProfile - Set-ProcessEnvironment -Name 'HOME' -Value $hookProfile - Set-ProcessEnvironment -Name 'HOMEDRIVE' -Value (Split-Path -Qualifier $hookProfile) - Set-ProcessEnvironment -Name 'HOMEPATH' -Value $hookProfile.Substring((Split-Path -Qualifier $hookProfile).Length) - Set-ProcessEnvironment -Name 'APPDATA' -Value (Join-Path $hookProfile 'AppData\Roaming') - Set-ProcessEnvironment -Name 'LOCALAPPDATA' -Value (Join-Path $hookProfile 'AppData\Local') - Set-ProcessEnvironment -Name 'HERMES_HOME' -Value (Join-Path $hookProfile '.hermes') - Set-ProcessEnvironment -Name 'HERMES_PROFILE' -Value $null - Set-ProcessEnvironment -Name 'WAGGLE_E2E_HOOK_HOME' -Value $hookProfile - Set-ProcessEnvironment -Name 'WAGGLE_E2E_REAL_HOOKS' -Value '1' - Set-ProcessEnvironment -Name 'WAGGLE_E2E_REAL_TOOLS' -Value $null - Invoke-PlaywrightLane -Spec 'tests/e2e/launcher-real-hook-lifecycle.spec.ts' -DataDir (Join-Path $runRoot 'hook-data') -ReceiptName 'hooks' - - foreach ($name in $profileVariables) { Restore-ProcessEnvironment -Name $name } - Set-ProcessEnvironment -Name 'WAGGLE_E2E_HOOK_HOME' -Value $null - Set-ProcessEnvironment -Name 'WAGGLE_E2E_REAL_HOOKS' -Value $null - Set-ProcessEnvironment -Name 'WAGGLE_E2E_REAL_TOOLS' -Value '1' - Invoke-PlaywrightLane -Spec 'tests/e2e/launcher-real-tool-lifecycle.spec.ts' -DataDir (Join-Path $runRoot 'tool-data') -ReceiptName 'tools' - if ($AuthenticatedTasks) { - $authenticatedHermesProfile = 'wagglee2e-' + [guid]::NewGuid().ToString('N') - $authenticatedHermesLease = $null - $authenticatedProfileRoot = Join-Path $runRoot 'authenticated-profile' - $authenticatedHermesRoot = Join-Path $authenticatedProfileRoot 'hermes-root' - $hermesProfilesRoot = Join-Path $authenticatedHermesRoot 'profiles' - $null = New-Item -ItemType Directory -Path $authenticatedHermesRoot -Force - Assert-NoReparsePointInPath -Path $authenticatedHermesRoot ` - -FailureMessage 'Hermes authenticated root contains a reparse point' - $originalUserHome = if (-not [string]::IsNullOrWhiteSpace($originalEnvironment['USERPROFILE'])) { - $originalEnvironment['USERPROFILE'] - } elseif (-not [string]::IsNullOrWhiteSpace($originalEnvironment['HOME'])) { - $originalEnvironment['HOME'] - } else { - [Environment]::GetFolderPath([Environment+SpecialFolder]::UserProfile) - } - $sourceClaudeConfig = if (-not [string]::IsNullOrWhiteSpace($originalEnvironment['CLAUDE_CONFIG_DIR'])) { - $originalEnvironment['CLAUDE_CONFIG_DIR'] - } else { - Join-Path $originalUserHome '.claude' - } - $sourceCodexHome = if (-not [string]::IsNullOrWhiteSpace($originalEnvironment['CODEX_HOME'])) { - $originalEnvironment['CODEX_HOME'] - } else { - Join-Path $originalUserHome '.codex' - } - $sourceHermesHome = if (-not [string]::IsNullOrWhiteSpace($originalEnvironment['HERMES_HOME'])) { - $originalEnvironment['HERMES_HOME'] - } else { - Join-Path $originalEnvironment['LOCALAPPDATA'] 'hermes' - } - $sourceClaudeCredentials = Join-Path $sourceClaudeConfig '.credentials.json' - $sourceCodexAuth = Join-Path $sourceCodexHome 'auth.json' - $sourceHermesAuth = Join-Path $sourceHermesHome 'auth.json' - $isolatedClaudeCredentials = Join-Path $authenticatedProfileRoot '.claude\.credentials.json' - $isolatedCodexAuth = Join-Path $authenticatedProfileRoot '.codex\auth.json' - $isolatedHermesAuth = Join-Path ( - Join-Path $hermesProfilesRoot $authenticatedHermesProfile - ) 'auth.json' - $isolatedAuthPaths = @($isolatedClaudeCredentials, $isolatedCodexAuth, $isolatedHermesAuth) - $sourceAuthEvidence = @( - [pscustomobject]@{ Path = $sourceClaudeCredentials; Hash = (Get-FileHash -LiteralPath $sourceClaudeCredentials -Algorithm SHA256).Hash }, - [pscustomobject]@{ Path = $sourceCodexAuth; Hash = (Get-FileHash -LiteralPath $sourceCodexAuth -Algorithm SHA256).Hash }, - [pscustomobject]@{ Path = $sourceHermesAuth; Hash = (Get-FileHash -LiteralPath $sourceHermesAuth -Algorithm SHA256).Hash } - ) - try { - Set-ProcessEnvironment -Name 'HERMES_HOME' -Value $authenticatedHermesRoot - Set-ProcessEnvironment -Name 'HERMES_PROFILE' -Value $null - $authenticatedHermesLease = New-OwnedHermesProfile ` - -ProfilesRoot $hermesProfilesRoot -ProfileName $authenticatedHermesProfile ` - -OwnedRoot $authenticatedProfileRoot - Copy-IsolatedAuthenticationFile ` - -Source $sourceClaudeCredentials ` - -Destination $isolatedClaudeCredentials ` - -OwnedRoot $authenticatedProfileRoot - Copy-IsolatedAuthenticationFile ` - -Source $sourceCodexAuth ` - -Destination $isolatedCodexAuth ` - -OwnedRoot $authenticatedProfileRoot - Copy-IsolatedAuthenticationFile ` - -Source $sourceHermesAuth ` - -Destination $isolatedHermesAuth ` - -OwnedRoot $authenticatedProfileRoot - $codexShim = New-IsolatedCodexShim ` - -ShimRoot (Join-Path $authenticatedProfileRoot 'bin') ` - -CodexHome (Join-Path $authenticatedProfileRoot '.codex') ` - -OwnedRoot $authenticatedProfileRoot - $hermesConfig = @' -model: - default: gpt-5.5 - provider: openai-codex -agent: - reasoning_effort: xhigh -'@ - [IO.File]::WriteAllText( - (Join-Path $authenticatedHermesLease.ProfileHome 'config.yaml'), - $hermesConfig + [Environment]::NewLine, - [Text.UTF8Encoding]::new($false) - ) - $authenticatedDrive = Split-Path -Qualifier $authenticatedProfileRoot - Set-ProcessEnvironment -Name 'USERPROFILE' -Value $authenticatedProfileRoot - Set-ProcessEnvironment -Name 'HOME' -Value $authenticatedProfileRoot - Set-ProcessEnvironment -Name 'HOMEDRIVE' -Value $authenticatedDrive - Set-ProcessEnvironment -Name 'HOMEPATH' -Value $authenticatedProfileRoot.Substring($authenticatedDrive.Length) - Set-ProcessEnvironment -Name 'APPDATA' -Value (Join-Path $authenticatedProfileRoot 'AppData\Roaming') - Set-ProcessEnvironment -Name 'LOCALAPPDATA' -Value (Join-Path $authenticatedProfileRoot 'AppData\Local') - Set-ProcessEnvironment -Name 'CLAUDE_CONFIG_DIR' -Value $null - Set-ProcessEnvironment -Name 'CODEX_HOME' -Value $null - Set-ProcessEnvironment -Name 'PATH' -Value ( - $codexShim.Directory + [IO.Path]::PathSeparator + $originalEnvironment['PATH'] - ) - Set-ProcessEnvironment -Name 'WAGGLE_E2E_REAL_TOOLS' -Value $null - Set-ProcessEnvironment -Name 'WAGGLE_LIVE_EXTERNAL_AGENTS' -Value '1' - Set-ProcessEnvironment -Name 'WAGGLE_LIVE_HERMES_PROVIDER' -Value $null - Set-ProcessEnvironment -Name 'WAGGLE_LIVE_HERMES_MODEL' -Value $null - Set-ProcessEnvironment -Name 'HERMES_HOME' -Value $authenticatedHermesLease.ProfileHome - Invoke-NativePreflight -FilePath 'claude' -ArgumentList @('auth', 'status') ` - -FailureMessage 'Isolated Claude Code authentication preflight failed' - Invoke-NativePreflight -FilePath $script:runnerNodePath ` - -ArgumentList @($codexShim.Launcher, 'login', 'status') ` - -FailureMessage 'Isolated Codex authentication preflight failed' - Invoke-VitestLane -Spec 'tests/integration/external-agent-collaboration.live.test.ts' ` - -ReceiptName 'authenticated-tasks' -HermesProvider 'openai-codex' -HermesModel 'gpt-5.5' - } finally { - Complete-AuthenticatedIsolationCleanup ` - -HermesLease $authenticatedHermesLease ` - -SourceAuthEvidence $sourceAuthEvidence ` - -ProfileVariables $profileVariables ` - -IsolatedAuthPaths $isolatedAuthPaths ` - -OwnedRoot $authenticatedProfileRoot - } - } - } finally { - Pop-Location - } - } finally { - foreach ($name in $environmentToRestore) { Restore-ProcessEnvironment -Name $name } - Remove-VerifiedTempTree -Target $runRoot - } - if ($null -ne $receiptRoot) { - Publish-ReceiptSet -StagingRoot $receiptStagingRoot -FinalRoot $receiptRoot ` - -ExpectedReceiptNames $expectedReceiptNames - $receiptStagingOwned = $false - } - $publicationCompleted = $true -} finally { - if (-not $publicationCompleted -and $null -ne $receiptRoot) { - Remove-VerifiedReceiptStaging -StagingRoot $receiptStagingRoot -FinalRoot $receiptRoot ` - -Owned $receiptStagingOwned - } -} - -$authenticatedMessage = if ($AuthenticatedTasks) { ', authenticated task' } else { '' } -Write-Host "Windows external-agent route, hook$authenticatedMessage lifecycle passed with isolated cleanup." diff --git a/scripts/test-windows-official-auth-canaries.ps1 b/scripts/test-windows-official-auth-canaries.ps1 deleted file mode 100644 index ac4e796..0000000 --- a/scripts/test-windows-official-auth-canaries.ps1 +++ /dev/null @@ -1,2239 +0,0 @@ -[CmdletBinding()] -param( - [Parameter(Mandatory = $true)] - [ValidatePattern('^[0-9a-f]{40}$')] - [string]$ExpectedHead, - - [Parameter(Mandatory = $true)] - [string]$ReceiptDir, - - [string]$ClaudeModel = 'claude-haiku-4-5-20251001', - - [ValidateRange(0.01, 1.00)] - [decimal]$ClaudeMaxUsd = [decimal]0.05, - - [string]$CodexModel = 'gpt-5.5', - - [ValidateSet('openai-codex')] - [string]$HermesProvider = 'openai-codex', - - [string]$HermesModel = 'gpt-5.5', - - [switch]$CodexProofValidatorSelfTest, - - [switch]$StaticPreflightOnly, - - [switch]$Execute, - - [string]$PaidRunAck = '' -) - -$ErrorActionPreference = 'Stop' -$executionAcknowledgement = 'I_ACKNOWLEDGE_3_OFFICIAL_AUTH_CALLS' -$evidenceRoot = [IO.Path]::GetFullPath('C:\tmp\waggle-readiness-evidence') -$profileEnvironmentNames = @( - 'USERPROFILE', - 'HOME', - 'HOMEDRIVE', - 'HOMEPATH', - 'APPDATA', - 'LOCALAPPDATA', - 'CLAUDE_CONFIG_DIR', - 'CODEX_HOME', - 'HERMES_HOME', - 'HERMES_PROFILE' -) -$claudeAlternativeAuthNames = @( - 'ANTHROPIC_API_KEY', - 'ANTHROPIC_AUTH_TOKEN', - 'CLAUDE_CODE_OAUTH_TOKEN', - 'ANTHROPIC_BASE_URL', - 'CLAUDE_CODE_USE_BEDROCK', - 'CLAUDE_CODE_USE_VERTEX', - 'CLAUDE_CODE_USE_FOUNDRY', - 'AWS_ACCESS_KEY_ID', - 'AWS_SECRET_ACCESS_KEY', - 'AWS_SESSION_TOKEN', - 'AWS_PROFILE', - 'AWS_CONFIG_FILE', - 'AWS_SHARED_CREDENTIALS_FILE', - 'GOOGLE_API_KEY', - 'GOOGLE_APPLICATION_CREDENTIALS', - 'CLOUDSDK_CONFIG' -) -$codexAlternativeAuthNames = @( - 'OPENAI_API_KEY', - 'OPENAI_ACCESS_TOKEN', - 'CODEX_ACCESS_TOKEN', - 'OPENAI_BASE_URL', - 'OPENAI_API_BASE' -) -$hermesAlternativeAuthNames = @( - 'OPENAI_API_KEY', - 'OPENAI_ACCESS_TOKEN', - 'CODEX_ACCESS_TOKEN', - 'OPENAI_BASE_URL', - 'OPENAI_API_BASE', - 'OPENROUTER_API_KEY', - 'OPENROUTER_BASE_URL', - 'HERMES_API_KEY', - 'LLM_API_KEY' -) - -function Assert-PlainIdentifier([string]$Value, [string]$Name) { - if ([string]::IsNullOrWhiteSpace($Value) -or $Value.Length -gt 128 -or $Value -match '[\x00-\x1f\x7f]') { - throw "$Name must be a non-empty, single-line identifier of at most 128 characters." - } -} - -function Assert-NoExistingReparsePoint([string]$Path, [string]$FailureMessage) { - $cursor = [IO.Path]::GetFullPath($Path) - while (-not [string]::IsNullOrWhiteSpace($cursor)) { - if (Test-Path -LiteralPath $cursor) { - $item = Get-Item -LiteralPath $cursor -Force - if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { - throw "$FailureMessage`: $cursor" - } - } - $parent = [IO.Path]::GetDirectoryName($cursor) - if ([string]::IsNullOrWhiteSpace($parent) -or $parent -eq $cursor) { break } - $cursor = $parent - } -} - -function Resolve-ReceiptLayout([string]$RequestedPath) { - $rootPrefix = $evidenceRoot.TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) + [IO.Path]::DirectorySeparatorChar - $fullPath = [IO.Path]::GetFullPath($RequestedPath).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) - if (-not $fullPath.StartsWith($rootPrefix, [StringComparison]::OrdinalIgnoreCase)) { - throw "ReceiptDir must be a child of $evidenceRoot." - } - if (Test-Path -LiteralPath $fullPath) { - throw 'ReceiptDir must be a fresh path owned by this run.' - } - Assert-NoExistingReparsePoint -Path ([IO.Path]::GetDirectoryName($fullPath)) ` - -FailureMessage 'ReceiptDir ancestor is a reparse point' - $leaf = [IO.Path]::GetFileName($fullPath) - if ([string]::IsNullOrWhiteSpace($leaf)) { throw 'ReceiptDir must have a final directory name.' } - return [pscustomobject]@{ - Root = $fullPath - Staging = Join-Path ([IO.Path]::GetDirectoryName($fullPath)) ".$leaf.staging-$([guid]::NewGuid().ToString('N'))" - } -} - -function Get-Sha256Text([string]$Value) { - $bytes = [Text.Encoding]::UTF8.GetBytes($Value) - return [Convert]::ToHexString([Security.Cryptography.SHA256]::HashData($bytes)).ToLowerInvariant() -} - -function Assert-JsonBoolean([object]$Value, [bool]$Expected, [string]$Label) { - if (-not ($Value -is [bool]) -or $Value -ne $Expected) { - throw "$Label must be the JSON boolean $($Expected.ToString().ToLowerInvariant())." - } -} - -function Assert-JsonInteger([object]$Value, [long]$Expected, [string]$Label) { - if ((-not ($Value -is [int])) -and (-not ($Value -is [long]))) { - throw "$Label must be a JSON integer." - } - if ([long]$Value -ne $Expected) { throw "$Label must equal $Expected." } -} - -function Assert-Sha256([object]$Value, [string]$Label) { - if (-not ($Value -is [string]) -or [string]$Value -cnotmatch '^[0-9a-f]{64}$') { - throw "$Label must be a lowercase SHA-256 value." - } -} - -function Assert-CodexToolDenialProof( - [object]$Proof, - [string]$ExpectedExecutableSha256, - [string]$ExpectedHelperSha256, - [string]$ExpectedHead, - [string]$ExpectedHiveMindCliSha256, - [string]$ExpectedMarkerSha256, - [string]$ExpectedPowerShellSha256, - [string]$ExpectedSessionIdSha256 -) { - Assert-JsonInteger -Value $Proof.schemaVersion -Expected 1 -Label 'Codex proof schemaVersion' - if (-not ($Proof.kind -is [string]) -or [string]$Proof.kind -cne 'codex-tool-denial-and-official-auth') { - throw 'Codex proof kind is invalid.' - } - Assert-JsonBoolean -Value $Proof.pass -Expected $true -Label 'Codex proof pass' - Assert-JsonInteger -Value $Proof.paidCalls -Expected 1 -Label 'Codex proof paidCalls' - Assert-JsonInteger -Value $Proof.diagnostic.modelCalls -Expected 1 -Label 'Codex proof modelCalls' - Assert-JsonInteger -Value $Proof.diagnostic.turnStartCalls -Expected 1 -Label 'Codex proof turnStartCalls' - Assert-JsonBoolean -Value $Proof.diagnostic.closeClean -Expected $true -Label 'Codex proof closeClean' - if ($null -ne $Proof.diagnostic.failureStage -or $null -ne $Proof.diagnostic.protocolCode -or - [string]$Proof.diagnostic.completedStage -cne 'post-turn-invariants') { - throw 'Codex proof did not complete all paid-turn invariants cleanly.' - } - Assert-JsonBoolean -Value $Proof.proof.executed -Expected $true -Label 'Codex proof-only executed' - Assert-JsonBoolean -Value $Proof.proof.pass -Expected $true -Label 'Codex proof-only pass' - Assert-JsonInteger -Value $Proof.proof.paidCalls -Expected 0 -Label 'Codex proof-only paidCalls' - - if (-not ($Proof.source.expectedHead -is [string]) -or [string]$Proof.source.expectedHead -cne $ExpectedHead) { - throw 'Codex proof expected HEAD did not match the outer harness.' - } - if (-not ($Proof.source.observedHead -is [string]) -or [string]$Proof.source.observedHead -cne $ExpectedHead) { - throw 'Codex proof observed HEAD did not match the outer harness.' - } - foreach ($entry in @( - [pscustomobject]@{ Value = $Proof.source.tree; Label = 'Codex proof source tree' } - [pscustomobject]@{ Value = $Proof.source.scriptBlob; Label = 'Codex proof helper blob' } - )) { - if (-not ($entry.Value -is [string]) -or [string]$entry.Value -cnotmatch '^[0-9a-f]{40}$') { - throw "$($entry.Label) must be a lowercase 40-character Git object id." - } - } - Assert-JsonBoolean -Value $Proof.source.trackedClean -Expected $true -Label 'Codex proof trackedClean' - Assert-JsonBoolean -Value $Proof.source.unchanged -Expected $true -Label 'Codex proof source unchanged' - - Assert-Sha256 -Value $Proof.executable.sha256 -Label 'Codex executable hash' - if ([string]$Proof.executable.sha256 -cne $ExpectedExecutableSha256) { - throw 'Codex executable did not match tool-denial proof.' - } - Assert-JsonBoolean -Value $Proof.executable.unchanged -Expected $true -Label 'Codex executable unchanged' - - foreach ($entry in @( - [pscustomobject]@{ Value = $Proof.modelCatalog.sealedSha256; Label = 'Codex sealed model catalog hash' } - [pscustomobject]@{ Value = $Proof.modelCatalog.controlSha256; Label = 'Codex control model catalog hash' } - [pscustomobject]@{ Value = $Proof.invocation.argumentsSha256; Label = 'Codex invocation arguments hash' } - [pscustomobject]@{ Value = $Proof.invocation.configSha256; Label = 'Codex invocation config hash' } - [pscustomobject]@{ Value = $Proof.invocation.threadParamsSha256; Label = 'Codex thread parameters hash' } - [pscustomobject]@{ Value = $Proof.invocation.turnParamsSha256; Label = 'Codex turn parameters hash' } - [pscustomobject]@{ Value = $Proof.invocation.mcpServerNamesSha256; Label = 'Codex MCP server-name inventory hash' } - [pscustomobject]@{ Value = $Proof.hooks.graphSha256; Label = 'Codex hook graph hash' } - [pscustomobject]@{ Value = $Proof.hooks.denyHookSha256; Label = 'Codex deny-hook hash' } - [pscustomobject]@{ Value = $Proof.hooks.artifactsSha256; Label = 'Codex packaged hook artifact hash' } - [pscustomobject]@{ Value = $Proof.hooks.cliSha256; Label = 'Codex hive-mind CLI hash' } - [pscustomobject]@{ Value = $Proof.green.denialReasonSha256; Label = 'Codex denial reason hash' } - [pscustomobject]@{ Value = $Proof.paidInvocation.argumentsSha256; Label = 'Codex paid invocation hash' } - [pscustomobject]@{ Value = $Proof.paidInvocation.modelCatalogSha256; Label = 'Codex paid model catalog hash' } - [pscustomobject]@{ Value = $Proof.paidInvocation.hookGraphSha256; Label = 'Codex paid hook graph hash' } - [pscustomobject]@{ Value = $Proof.paidInvocation.markerSha256; Label = 'Codex paid marker hash' } - [pscustomobject]@{ Value = $Proof.paidInvocation.sessionIdSha256; Label = 'Codex paid session hash' } - [pscustomobject]@{ Value = $Proof.paidInvocation.threadParamsSha256; Label = 'Codex paid thread parameters hash' } - [pscustomobject]@{ Value = $Proof.paidInvocation.turnParamsSha256; Label = 'Codex paid turn parameters hash' } - [pscustomobject]@{ Value = $Proof.mcpBoundary.namesSha256; Label = 'Codex MCP boundary server-name hash' } - [pscustomobject]@{ Value = $Proof.mcpBoundary.initialSha256; Label = 'Codex initial MCP boundary hash' } - [pscustomobject]@{ Value = $Proof.mcpBoundary.prePaidSha256; Label = 'Codex pre-paid MCP boundary hash' } - [pscustomobject]@{ Value = $Proof.mcpBoundary.postPaidSha256; Label = 'Codex post-paid MCP boundary hash' } - [pscustomobject]@{ Value = $Proof.mcpBoundary.initialConfigSha256; Label = 'Codex initial config hash' } - [pscustomobject]@{ Value = $Proof.mcpBoundary.prePaidConfigSha256; Label = 'Codex pre-paid config hash' } - [pscustomobject]@{ Value = $Proof.mcpBoundary.postPaidConfigSha256; Label = 'Codex post-paid config hash' } - [pscustomobject]@{ Value = $Proof.artifacts.scriptSha256; Label = 'Codex helper script hash' } - [pscustomobject]@{ Value = $Proof.artifacts.windowsPowerShellSha256; Label = 'Codex Windows PowerShell hash' } - [pscustomobject]@{ Value = $Proof.artifacts.windowsSystemDirectorySha256; Label = 'Codex Windows system-directory hash' } - )) { - Assert-Sha256 -Value $entry.Value -Label ([string]$entry.Label) - } - if ((-not ($Proof.invocation.mcpServerCount -is [int])) -and (-not ($Proof.invocation.mcpServerCount -is [long]))) { - throw 'Codex MCP server count must be a JSON integer.' - } - if ([long]$Proof.invocation.mcpServerCount -lt 0) { - throw 'Codex MCP server count must not be negative.' - } - if ((-not ($Proof.mcpBoundary.count -is [int])) -and (-not ($Proof.mcpBoundary.count -is [long]))) { - throw 'Codex MCP boundary count must be a JSON integer.' - } - if ([long]$Proof.mcpBoundary.count -ne [long]$Proof.invocation.mcpServerCount -or - [string]$Proof.mcpBoundary.namesSha256 -cne [string]$Proof.invocation.mcpServerNamesSha256) { - throw 'Codex MCP boundary inventory did not match the invocation inventory.' - } - if ([string]$Proof.mcpBoundary.prePaidSha256 -cne [string]$Proof.mcpBoundary.initialSha256 -or - [string]$Proof.mcpBoundary.postPaidSha256 -cne [string]$Proof.mcpBoundary.initialSha256 -or - [string]$Proof.mcpBoundary.prePaidConfigSha256 -cne [string]$Proof.mcpBoundary.initialConfigSha256 -or - [string]$Proof.mcpBoundary.postPaidConfigSha256 -cne [string]$Proof.mcpBoundary.initialConfigSha256) { - throw 'Codex MCP/config boundary changed around the paid turn.' - } - Assert-JsonBoolean -Value $Proof.mcpBoundary.unchanged -Expected $true -Label 'Codex MCP boundary unchanged' - - Assert-JsonInteger -Value $Proof.hooks.expectedCount -Expected 5 -Label 'Codex hook expectedCount' - Assert-JsonInteger -Value $Proof.hooks.extraCount -Expected 0 -Label 'Codex hook extraCount' - Assert-JsonInteger -Value $Proof.hooks.warnings -Expected 0 -Label 'Codex hook warnings' - Assert-JsonInteger -Value $Proof.hooks.errors -Expected 0 -Label 'Codex hook errors' - Assert-JsonBoolean -Value $Proof.hooks.allTrusted -Expected $true -Label 'Codex hooks allTrusted' - Assert-JsonBoolean -Value $Proof.hooks.unchangedAfterPin -Expected $true -Label 'Codex hooks unchangedAfterPin' - Assert-JsonBoolean -Value $Proof.hooks.artifactsUnchanged -Expected $true -Label 'Codex hook artifacts unchanged' - Assert-JsonBoolean -Value $Proof.artifacts.scriptUnchanged -Expected $true -Label 'Codex helper script unchanged' - if ([string]$Proof.artifacts.scriptSha256 -cne $ExpectedHelperSha256) { - throw 'Codex tool-denial helper did not match the outer harness artifact.' - } - if ([string]$Proof.hooks.cliSha256 -cne $ExpectedHiveMindCliSha256) { - throw 'Codex hive-mind CLI did not match the packaged artifact.' - } - if ([string]$Proof.artifacts.windowsPowerShellSha256 -cne $ExpectedPowerShellSha256) { - throw 'Codex deny-hook PowerShell did not match the OS executable.' - } - - Assert-JsonInteger -Value $Proof.sealed.topLevelToolCount -Expected 0 -Label 'Codex sealed topLevelToolCount' - Assert-JsonInteger -Value $Proof.sealed.additionalToolCount -Expected 0 -Label 'Codex sealed additionalToolCount' - Assert-JsonBoolean -Value $Proof.sealed.emptySchema -Expected $true -Label 'Codex sealed emptySchema' - Assert-JsonBoolean -Value $Proof.sealed.normalTextCompleted -Expected $true -Label 'Codex sealed normalTextCompleted' - - $redTools = @($Proof.red.toolNames) - if ($redTools.Count -ne 1 -or -not ($redTools[0] -is [string]) -or [string]$redTools[0] -cne 'view_image') { - throw 'Codex RED control did not expose exactly view_image.' - } - Assert-JsonBoolean -Value $Proof.red.toolLoopObserved -Expected $true -Label 'Codex RED toolLoopObserved' - if (-not ($Proof.red.readObserved -is [bool])) { - throw 'Codex RED readObserved must be a JSON boolean.' - } - if (-not ($Proof.red.sensitiveDataObserved -is [bool])) { - throw 'Codex RED sensitiveDataObserved must be a JSON boolean.' - } - Assert-JsonBoolean -Value $Proof.green.deniedBeforeRead -Expected $true -Label 'Codex GREEN deniedBeforeRead' - Assert-JsonBoolean -Value $Proof.green.sensitiveDataObserved -Expected $false -Label 'Codex GREEN sensitiveDataObserved' - Assert-JsonBoolean -Value $Proof.green.normalTextCompleted -Expected $true -Label 'Codex GREEN normalTextCompleted' - - Assert-JsonBoolean -Value $Proof.paidInvocation.attempted -Expected $true -Label 'Codex paid attempted' - Assert-JsonBoolean -Value $Proof.paidInvocation.executed -Expected $true -Label 'Codex paid executed' - Assert-JsonBoolean -Value $Proof.paidInvocation.normalTextCompleted -Expected $true -Label 'Codex paid normalTextCompleted' - Assert-JsonInteger -Value $Proof.paidInvocation.toolEventsObserved -Expected 0 -Label 'Codex paid toolEventsObserved' - Assert-JsonInteger -Value $Proof.paidInvocation.unknownEventsObserved -Expected 0 -Label 'Codex paid unknownEventsObserved' - if ([string]$Proof.paidInvocation.hookGraphSha256 -cne [string]$Proof.hooks.graphSha256) { - throw 'Codex paid hook graph did not match the pinned proof.' - } - if ([string]$Proof.paidInvocation.modelCatalogSha256 -cne [string]$Proof.modelCatalog.sealedSha256) { - throw 'Codex paid model catalog did not match the sealed proof.' - } - if ([string]$Proof.paidInvocation.threadParamsSha256 -cne [string]$Proof.invocation.threadParamsSha256 -or - [string]$Proof.paidInvocation.turnParamsSha256 -cne [string]$Proof.invocation.turnParamsSha256) { - throw 'Codex paid turn parameters did not match the sealed proof.' - } - if ([string]$Proof.paidInvocation.markerSha256 -cne $ExpectedMarkerSha256) { - throw 'Codex paid marker hash did not match.' - } - if ([string]$Proof.paidInvocation.sessionIdSha256 -cne $ExpectedSessionIdSha256) { - throw 'Codex paid session identifier hash did not match.' - } -} - -function Assert-SanitizedCodexTurnFailure([object]$Failure) { - if ($null -eq $Failure) { return $null } - $expectedProperties = @('code', 'httpStatusCode', 'status', 'willRetry') - $actualProperties = @($Failure.PSObject.Properties.Name | Sort-Object) - if ([string]::Join(',', $actualProperties) -cne [string]::Join(',', $expectedProperties)) { - throw 'Codex turn failure contains unexpected or missing properties.' - } - if (-not ($Failure.status -is [string]) -or - [string]$Failure.status -cnotin @('failed', 'interrupted')) { - throw 'Codex turn failure status is invalid.' - } - $simpleCodes = @( - 'badRequest', - 'contextWindowExceeded', - 'cyberPolicy', - 'internalServerError', - 'other', - 'sandboxError', - 'serverOverloaded', - 'sessionBudgetExceeded', - 'threadRollbackFailed', - 'unauthorized', - 'usageLimitExceeded' - ) - $httpCodes = @( - 'httpConnectionFailed', - 'responseStreamConnectionFailed', - 'responseStreamDisconnected', - 'responseTooManyFailedAttempts' - ) - $code = $Failure.code - if ($null -ne $code -and (-not ($code -is [string]) -or - ([string]$code -cnotin $simpleCodes -and [string]$code -cnotin $httpCodes -and - [string]$code -cne 'activeTurnNotSteerable'))) { - throw 'Codex turn failure code is invalid.' - } - $httpStatusCode = $Failure.httpStatusCode - if ($null -ne $httpStatusCode -and - ((-not ($httpStatusCode -is [int])) -and (-not ($httpStatusCode -is [long])))) { - throw 'Codex turn failure HTTP status must be a JSON integer or null.' - } - if ($null -ne $httpStatusCode -and - ([long]$httpStatusCode -lt 0 -or [long]$httpStatusCode -gt 65535)) { - throw 'Codex turn failure HTTP status is out of range.' - } - if ($null -ne $httpStatusCode -and [string]$code -cnotin $httpCodes) { - throw 'Only Codex HTTP failure codes may carry an HTTP status.' - } - if ($null -ne $Failure.willRetry -and -not ($Failure.willRetry -is [bool])) { - throw 'Codex turn failure retry flag must be a JSON boolean or null.' - } - return [pscustomobject][ordered]@{ - status = [string]$Failure.status - code = if ($null -eq $code) { $null } else { [string]$code } - httpStatusCode = if ($null -eq $httpStatusCode) { $null } else { [long]$httpStatusCode } - willRetry = if ($null -eq $Failure.willRetry) { $null } else { [bool]$Failure.willRetry } - } -} - -function Assert-CodexFailureStage([object]$Value, [bool]$AllowNull = $false) { - if ($null -eq $Value) { - if ($AllowNull) { return $null } - throw 'Codex failure stage must not be null.' - } - $stages = @( - 'app-server-spawn', - 'initialize', - 'hooks-list', - 'pre-turn-boundary', - 'thread-start', - 'turn-start', - 'turn-completed', - 'event-audit', - 'app-server-close', - 'post-turn-invariants' - ) - if (-not ($Value -is [string]) -or [string]$Value -cnotin $stages) { - throw 'Codex failure stage is invalid.' - } - return [string]$Value -} - -function Assert-NullableProtocolCode([object]$Value) { - if ($null -eq $Value) { return $null } - if ((-not ($Value -is [int])) -and (-not ($Value -is [long]))) { - throw 'Codex protocol code must be a JSON integer or null.' - } - $code = [long]$Value - if ($code -lt [int]::MinValue -or $code -gt [int]::MaxValue) { - throw 'Codex protocol code must fit in a signed 32-bit integer.' - } - return [int]$code -} - -function Assert-SanitizedCodexFailureProjection([object]$Failure) { - $expectedProperties = @('class', 'lane', 'protocolCode', 'stage', 'turn') - $actualProperties = @($Failure.PSObject.Properties.Name | Sort-Object) - if ([string]::Join(',', $actualProperties) -cne [string]::Join(',', $expectedProperties)) { - throw 'Sanitized Codex failure contains unexpected or missing properties.' - } - if (-not ($Failure.class -is [string]) -or [string]$Failure.class -cnotin @( - 'codex-paid-turn-incomplete', - 'codex-post-turn-invariant-failed', - 'codex-proof-incomplete' - )) { - throw 'Sanitized Codex failure class is invalid.' - } - if (-not ($Failure.lane -is [string]) -or [string]$Failure.lane -cne 'codex') { - throw 'Sanitized Codex failure lane is invalid.' - } - $null = Assert-CodexFailureStage ` - -Value $Failure.stage ` - -AllowNull ([string]$Failure.class -ceq 'codex-proof-incomplete') - $null = Assert-NullableProtocolCode -Value $Failure.protocolCode - $null = Assert-SanitizedCodexTurnFailure -Failure $Failure.turn -} - -function Assert-CodexSetupPreflight( - [object]$Summary, - [object]$Report, - [string]$ExpectedHead, - [string]$ExpectedTree, - [string]$ExpectedHelperBlob, - [string]$ExpectedHelperSha256, - [string]$ExpectedExecutableSha256, - [string]$ExpectedReportPath, - [string]$ExpectedReportSha256 -) { - $expectedSummaryProperties = @( - 'closeClean', - 'completedStage', - 'failureStage', - 'markerMatched', - 'modelCalls', - 'paidCalls', - 'pass', - 'protocolCode', - 'reportPath', - 'reportSha256', - 'sessionId', - 'turnFailure', - 'turnStartCalls' - ) - $actualSummaryProperties = @($Summary.PSObject.Properties.Name | Sort-Object) - if ([string]::Join(',', $actualSummaryProperties) -cne - [string]::Join(',', $expectedSummaryProperties)) { - throw 'Codex setup summary contains unexpected or missing properties.' - } - Assert-JsonBoolean -Value $Summary.pass -Expected $true -Label 'Codex setup summary pass' - Assert-JsonInteger -Value $Summary.paidCalls -Expected 0 -Label 'Codex setup summary paidCalls' - Assert-JsonInteger -Value $Summary.modelCalls -Expected 0 -Label 'Codex setup summary modelCalls' - Assert-JsonInteger -Value $Summary.turnStartCalls -Expected 0 -Label 'Codex setup summary turnStartCalls' - Assert-JsonBoolean -Value $Summary.closeClean -Expected $true -Label 'Codex setup summary closeClean' - if (-not ($Summary.markerMatched -is [bool])) { - throw 'Codex setup summary markerMatched must be a JSON boolean.' - } - if ($null -ne $Summary.sessionId -or $null -ne $Summary.turnFailure) { - throw 'Codex setup summary must not contain a session identifier or turn failure.' - } - $null = Assert-CodexFailureStage -Value $Summary.failureStage -AllowNull $true - $null = Assert-NullableProtocolCode -Value $Summary.protocolCode - if ($null -ne $Summary.failureStage -or $null -ne $Summary.protocolCode -or - -not ($Summary.completedStage -is [string]) -or - [string]$Summary.completedStage -cne 'thread-start') { - throw 'Codex setup summary did not stop cleanly at thread-start.' - } - Assert-Sha256 -Value $Summary.reportSha256 -Label 'Codex setup summary report hash' - if (-not ($Summary.reportPath -is [string]) -or - [IO.Path]::GetFullPath([string]$Summary.reportPath) -cne $ExpectedReportPath -or - [string]$Summary.reportSha256 -cne $ExpectedReportSha256) { - throw 'Codex setup summary report binding is invalid.' - } - - Assert-JsonBoolean -Value $Report.pass -Expected $true -Label 'Codex setup report pass' - Assert-JsonInteger -Value $Report.schemaVersion -Expected 1 -Label 'Codex setup report schemaVersion' - if (-not ($Report.kind -is [string]) -or - [string]$Report.kind -cne 'codex-tool-denial-and-official-auth') { - throw 'Codex setup report kind is invalid.' - } - Assert-JsonInteger -Value $Report.paidCalls -Expected 0 -Label 'Codex setup report paidCalls' - Assert-JsonInteger -Value $Report.diagnostic.modelCalls -Expected 0 -Label 'Codex setup report modelCalls' - Assert-JsonInteger -Value $Report.diagnostic.turnStartCalls -Expected 0 -Label 'Codex setup report turnStartCalls' - Assert-JsonBoolean -Value $Report.diagnostic.closeClean -Expected $true -Label 'Codex setup report closeClean' - Assert-JsonBoolean -Value $Report.proof.executed -Expected $false ` - -Label 'Codex setup offline proof executed' - if ($null -ne $Report.proof.pass) { - throw 'Codex setup report must record the skipped offline proof as null.' - } - Assert-JsonInteger -Value $Report.proof.paidCalls -Expected 0 -Label 'Codex setup offline proof paidCalls' - if ($null -ne $Report.diagnostic.failureStage -or $null -ne $Report.diagnostic.protocolCode -or - [string]$Report.diagnostic.completedStage -cne 'thread-start') { - throw 'Codex setup report did not stop cleanly at thread-start.' - } - if ([string]$Report.source.expectedHead -cne $ExpectedHead -or - [string]$Report.source.observedHead -cne $ExpectedHead -or - [string]$Report.source.tree -cne $ExpectedTree -or - [string]$Report.source.scriptBlob -cne $ExpectedHelperBlob) { - throw 'Codex setup report source did not match the sealed revision.' - } - Assert-JsonBoolean -Value $Report.source.trackedClean -Expected $true ` - -Label 'Codex setup report trackedClean' - if ([string]$Report.executable.sha256 -cne $ExpectedExecutableSha256 -or - [string]$Report.artifacts.scriptSha256 -cne $ExpectedHelperSha256) { - throw 'Codex setup report did not match the sealed executable or helper.' - } -} - -function New-SanitizedCodexFailureReceipt( - [object]$TurnFailure, - [object]$FailureStage, - [object]$ProtocolCode, - [object]$ChildResult, - [object]$GitState, - [string]$ExpectedHead, - [string]$ScriptSha256, - [string]$ScriptBlob, - [string]$HelperSha256, - [string]$HelperBlob, - [string]$ExecutableSha256, - [string]$ReportSha256, - [object]$ProofSummary, - [bool]$TemporaryRootRemoved -) { - $turnFailure = Assert-SanitizedCodexTurnFailure -Failure $TurnFailure - $failureClass = if ($null -ne $turnFailure) { - 'codex-paid-turn-incomplete' - } elseif ($ProofSummary.executed -eq $true) { - 'codex-post-turn-invariant-failed' - } else { - 'codex-proof-incomplete' - } - $failureStage = Assert-CodexFailureStage ` - -Value $FailureStage ` - -AllowNull ($failureClass -eq 'codex-proof-incomplete') - $protocolCode = Assert-NullableProtocolCode -Value $ProtocolCode - foreach ($entry in @( - [pscustomobject]@{ Value = $ScriptSha256; Label = 'failure receipt script hash' } - [pscustomobject]@{ Value = $HelperSha256; Label = 'failure receipt helper hash' } - [pscustomobject]@{ Value = $ExecutableSha256; Label = 'failure receipt executable hash' } - [pscustomobject]@{ Value = $ReportSha256; Label = 'failure receipt child report hash' } - [pscustomobject]@{ Value = $ChildResult.StdoutSha256; Label = 'failure receipt stdout hash' } - [pscustomobject]@{ Value = $ChildResult.StderrSha256; Label = 'failure receipt stderr hash' } - )) { - Assert-Sha256 -Value $entry.Value -Label ([string]$entry.Label) - } - foreach ($entry in @( - [pscustomobject]@{ Value = $ScriptBlob; Label = 'failure receipt script blob' } - [pscustomobject]@{ Value = $HelperBlob; Label = 'failure receipt helper blob' } - [pscustomobject]@{ Value = $GitState.Tree; Label = 'failure receipt source tree' } - )) { - if (-not ($entry.Value -is [string]) -or [string]$entry.Value -cnotmatch '^[0-9a-f]{40}$') { - throw "$($entry.Label) must be a lowercase 40-character Git object id." - } - } - if (-not $TemporaryRootRemoved) { throw 'Codex failure receipt requires completed temporary cleanup.' } - $failureProjection = [ordered]@{ - class = $failureClass - lane = 'codex' - protocolCode = $protocolCode - stage = $failureStage - turn = if ($null -eq $turnFailure) { $null } else { [pscustomobject]$turnFailure } - } - Assert-SanitizedCodexFailureProjection -Failure ([pscustomobject]$failureProjection) - return [ordered]@{ - schemaVersion = 1 - kind = 'windows-official-auth-codex-failure' - pass = $false - source = [ordered]@{ - expectedHead = $ExpectedHead - observedHead = [string]$GitState.Head - tree = [string]$GitState.Tree - trackedClean = [bool]$GitState.TrackedClean - scriptSha256 = $ScriptSha256 - scriptBlob = $ScriptBlob - codexHelperSha256 = $HelperSha256 - codexHelperBlob = $HelperBlob - codexExecutableSha256 = $ExecutableSha256 - } - child = [ordered]@{ - exitCode = $ChildResult.ExitCode - timedOut = [bool]$ChildResult.TimedOut - durationMs = [long]$ChildResult.DurationMs - stdoutBytes = [long]$ChildResult.StdoutBytes - stdoutSha256 = [string]$ChildResult.StdoutSha256 - stderrBytes = [long]$ChildResult.StderrBytes - stderrSha256 = [string]$ChildResult.StderrSha256 - reportSha256 = $ReportSha256 - } - failure = $failureProjection - proof = [ordered]@{ - pass = [bool]$ProofSummary.pass - paidCalls = [long]$ProofSummary.paidCalls - offlinePass = [bool]$ProofSummary.offlinePass - offlinePaidCalls = [long]$ProofSummary.offlinePaidCalls - attempted = [bool]$ProofSummary.attempted - executed = [bool]$ProofSummary.executed - normalTextCompleted = [bool]$ProofSummary.normalTextCompleted - toolEventsObserved = [long]$ProofSummary.toolEventsObserved - unknownEventsObserved = [long]$ProofSummary.unknownEventsObserved - mcpBoundaryUnchanged = [bool]$ProofSummary.mcpBoundaryUnchanged - } - cleanup = [ordered]@{ - temporaryRootRemoved = $true - rawOutputsPersisted = $false - childReportPersisted = $false - } - } -} - -function Invoke-CodexProofValidatorSelfTest { - $executableHash = 'a' * 64 - $cliHash = 'b' * 64 - $powerShellHash = 'c' * 64 - $markerHash = 'd' * 64 - $sessionHash = 'e' * 64 - $graphHash = 'f' * 64 - $catalogHash = '1' * 64 - $otherHash = '2' * 64 - $helperHash = '3' * 64 - $expectedHead = '0' * 40 - $gitObject = 'a' * 40 - $validValue = [ordered]@{ - schemaVersion = 1 - kind = 'codex-tool-denial-and-official-auth' - pass = $true - paidCalls = 1 - diagnostic = [ordered]@{ - modelCalls = 1 - turnStartCalls = 1 - failureStage = $null - protocolCode = $null - completedStage = 'post-turn-invariants' - closeClean = $true - } - proof = [ordered]@{ executed = $true; pass = $true; paidCalls = 0 } - source = [ordered]@{ - expectedHead = $expectedHead - observedHead = $expectedHead - tree = $gitObject - scriptBlob = $gitObject - trackedClean = $true - unchanged = $true - } - executable = [ordered]@{ sha256 = $executableHash; unchanged = $true } - modelCatalog = [ordered]@{ sealedSha256 = $catalogHash; controlSha256 = $otherHash } - invocation = [ordered]@{ - argumentsSha256 = $otherHash - configSha256 = $otherHash - threadParamsSha256 = $otherHash - turnParamsSha256 = $otherHash - mcpServerCount = 4 - mcpServerNamesSha256 = $otherHash - } - hooks = [ordered]@{ - expectedCount = 5 - extraCount = 0 - warnings = 0 - errors = 0 - allTrusted = $true - unchangedAfterPin = $true - artifactsUnchanged = $true - graphSha256 = $graphHash - denyHookSha256 = $otherHash - artifactsSha256 = $otherHash - cliSha256 = $cliHash - } - mcpBoundary = [ordered]@{ - count = 4 - namesSha256 = $otherHash - initialSha256 = $otherHash - prePaidSha256 = $otherHash - postPaidSha256 = $otherHash - initialConfigSha256 = $otherHash - prePaidConfigSha256 = $otherHash - postPaidConfigSha256 = $otherHash - unchanged = $true - } - sealed = [ordered]@{ - topLevelToolCount = 0 - additionalToolCount = 0 - emptySchema = $true - normalTextCompleted = $true - } - red = [ordered]@{ - toolNames = @('view_image') - toolLoopObserved = $true - readObserved = $false - sensitiveDataObserved = $false - } - green = [ordered]@{ - deniedBeforeRead = $true - sensitiveDataObserved = $false - normalTextCompleted = $true - denialReasonSha256 = $otherHash - } - paidInvocation = [ordered]@{ - attempted = $true - executed = $true - normalTextCompleted = $true - toolEventsObserved = 0 - unknownEventsObserved = 0 - argumentsSha256 = $otherHash - modelCatalogSha256 = $catalogHash - hookGraphSha256 = $graphHash - markerSha256 = $markerHash - sessionIdSha256 = $sessionHash - threadParamsSha256 = $otherHash - turnParamsSha256 = $otherHash - } - artifacts = [ordered]@{ - scriptSha256 = $helperHash - scriptUnchanged = $true - windowsPowerShellSha256 = $powerShellHash - windowsSystemDirectorySha256 = $otherHash - } - } - $copy = { - param($Value) - return ($Value | ConvertTo-Json -Depth 50 | ConvertFrom-Json -Depth 50) - } - $validate = { - param($Value) - Assert-CodexToolDenialProof -Proof $Value ` - -ExpectedExecutableSha256 $executableHash ` - -ExpectedHelperSha256 $helperHash ` - -ExpectedHead $expectedHead ` - -ExpectedHiveMindCliSha256 $cliHash ` - -ExpectedMarkerSha256 $markerHash ` - -ExpectedPowerShellSha256 $powerShellHash ` - -ExpectedSessionIdSha256 $sessionHash - } - $valid = & $copy $validValue - & $validate $valid - $cases = 1 - $reject = { - param($Fixture, [string]$Label) - $accepted = $true - try { & $validate $Fixture } catch { $accepted = $false } - if ($accepted) { throw "Codex proof validator accepted invalid fixture: $Label" } - $script:codexProofSelfTestCases += 1 - } - $script:codexProofSelfTestCases = $cases - - $fixture = & $copy $validValue; $fixture.kind = 'wrong'; & $reject $fixture 'kind' - $fixture = & $copy $validValue; $fixture.schemaVersion = '1'; & $reject $fixture 'string schemaVersion' - $fixture = & $copy $validValue; $fixture.pass = 1; & $reject $fixture 'numeric pass' - $fixture = & $copy $validValue; $fixture.paidCalls = '1'; & $reject $fixture 'string paidCalls' - $fixture = & $copy $validValue; $fixture.invocation.mcpServerCount = '4'; & $reject $fixture 'string MCP server count' - $fixture = & $copy $validValue; $fixture.invocation.mcpServerNamesSha256 = 'bad'; & $reject $fixture 'MCP server inventory hash' - $fixture = & $copy $validValue; $fixture.hooks.extraCount = 1; & $reject $fixture 'extra hook' - $fixture = & $copy $validValue; $fixture.hooks.warnings = 1; & $reject $fixture 'hook warning' - $fixture = & $copy $validValue; $fixture.hooks.allTrusted = 1; & $reject $fixture 'numeric hook trust' - $fixture = & $copy $validValue; $fixture.sealed.topLevelToolCount = 1; & $reject $fixture 'non-empty tools' - $fixture = & $copy $validValue; $fixture.red.toolLoopObserved = $false; & $reject $fixture 'RED tool loop' - $fixture = & $copy $validValue; $fixture.green.sensitiveDataObserved = $true; & $reject $fixture 'GREEN data leak' - $fixture = & $copy $validValue; $fixture.paidInvocation.toolEventsObserved = 1; & $reject $fixture 'paid tool event' - $fixture = & $copy $validValue; $fixture.paidInvocation.unknownEventsObserved = 1; & $reject $fixture 'unknown event' - $fixture = & $copy $validValue; $fixture.executable.sha256 = $otherHash; & $reject $fixture 'executable mismatch' - $fixture = & $copy $validValue; $fixture.hooks.cliSha256 = $otherHash; & $reject $fixture 'CLI mismatch' - $fixture = & $copy $validValue; $fixture.artifacts.windowsPowerShellSha256 = $otherHash; & $reject $fixture 'PowerShell mismatch' - $fixture = & $copy $validValue; $fixture.paidInvocation.modelCatalogSha256 = $otherHash; & $reject $fixture 'catalog mismatch' - $fixture = & $copy $validValue; $fixture.paidInvocation.hookGraphSha256 = $otherHash; & $reject $fixture 'graph mismatch' - $fixture = & $copy $validValue; $fixture.paidInvocation.markerSha256 = $otherHash; & $reject $fixture 'marker mismatch' - $fixture = & $copy $validValue; $fixture.paidInvocation.sessionIdSha256 = $otherHash; & $reject $fixture 'session mismatch' - $fixture = & $copy $validValue; $fixture.source.expectedHead = 'f' * 40; & $reject $fixture 'source HEAD mismatch' - $fixture = & $copy $validValue; $fixture.artifacts.scriptSha256 = $otherHash; & $reject $fixture 'helper hash mismatch' - $fixture = & $copy $validValue; $fixture.hooks.artifactsUnchanged = $false; & $reject $fixture 'hook artifact drift' - $fixture = & $copy $validValue; $fixture.paidInvocation.threadParamsSha256 = $graphHash; & $reject $fixture 'paid thread mismatch' - $fixture = & $copy $validValue; $fixture.mcpBoundary.prePaidSha256 = $graphHash; & $reject $fixture 'pre-paid MCP drift' - - $validFailure = [pscustomobject][ordered]@{ - status = 'failed' - code = 'unauthorized' - httpStatusCode = $null - willRetry = $false - } - $validChild = [pscustomobject]@{ - ExitCode = 1 - TimedOut = $false - DurationMs = 123 - Stdout = 'C:\secret\auth.json sk-self-test' - Stderr = 'file:///private/detail sess-self-test' - StdoutBytes = 40 - StderrBytes = 40 - StdoutSha256 = $otherHash - StderrSha256 = $catalogHash - } - $validGit = [pscustomobject]@{ - Head = $expectedHead - Tree = $gitObject - TrackedClean = $true - } - $validProofSummary = [pscustomobject]@{ - pass = $false - paidCalls = 1 - offlinePass = $true - offlinePaidCalls = 0 - attempted = $true - executed = $false - normalTextCompleted = $false - toolEventsObserved = 0 - unknownEventsObserved = 0 - mcpBoundaryUnchanged = $false - } - $failureReceipt = New-SanitizedCodexFailureReceipt ` - -TurnFailure $validFailure -FailureStage 'thread-start' -ProtocolCode ([int]-32603) ` - -ChildResult $validChild -GitState $validGit ` - -ExpectedHead $expectedHead -ScriptSha256 $helperHash -ScriptBlob $gitObject ` - -HelperSha256 $helperHash -HelperBlob $gitObject -ExecutableSha256 $executableHash ` - -ReportSha256 $otherHash -ProofSummary $validProofSummary -TemporaryRootRemoved $true - $failureReceiptJson = $failureReceipt | ConvertTo-Json -Depth 20 -Compress - if ($failureReceipt.kind -cne 'windows-official-auth-codex-failure' -or - $failureReceipt.pass -ne $false -or $failureReceiptJson -match '(?i)(?:auth\.json|file:///|sk-|sess-)') { - throw 'Sanitized Codex failure receipt retained untrusted diagnostic text.' - } - $script:codexProofSelfTestCases += 1 - - $normalizedFailure = Assert-SanitizedCodexTurnFailure -Failure $validFailure - $roundTripReceipt = New-SanitizedCodexFailureReceipt ` - -TurnFailure $normalizedFailure -FailureStage 'thread-start' -ProtocolCode ([int]-32603) ` - -ChildResult $validChild -GitState $validGit ` - -ExpectedHead $expectedHead -ScriptSha256 $helperHash -ScriptBlob $gitObject ` - -HelperSha256 $helperHash -HelperBlob $gitObject -ExecutableSha256 $executableHash ` - -ReportSha256 $otherHash -ProofSummary $validProofSummary -TemporaryRootRemoved $true - if ($roundTripReceipt.failure.turn.status -cne 'failed' -or - $roundTripReceipt.failure.turn.code -cne 'unauthorized') { - throw 'Sanitized Codex failure receipt did not accept normalized turn failure.' - } - $script:codexProofSelfTestCases += 1 - $failureReject = { - param($Fixture, [string]$Label) - $accepted = $true - try { $null = Assert-SanitizedCodexTurnFailure -Failure $Fixture } catch { $accepted = $false } - if ($accepted) { throw "Codex turn-failure validator accepted invalid fixture: $Label" } - $script:codexProofSelfTestCases += 1 - } - $fixture = & $copy $validFailure; $fixture.status = 'completed'; & $failureReject $fixture 'completed status' - $fixture = & $copy $validFailure; $fixture.code = 'futureCode'; & $failureReject $fixture 'unknown code' - $fixture = & $copy $validFailure; $fixture.code = 'httpConnectionFailed'; $fixture.httpStatusCode = 70000; & $failureReject $fixture 'HTTP status range' - $fixture = & $copy $validFailure; $fixture.willRetry = 'false'; & $failureReject $fixture 'string retry flag' - $fixture = & $copy $validFailure; $fixture | Add-Member -NotePropertyName message -NotePropertyValue 'C:\secret\auth.json sk-fixture'; & $failureReject $fixture 'message property' - $fixture = & $copy $validFailure; $fixture | Add-Member -NotePropertyName additionalDetails -NotePropertyValue 'file:///private/detail'; & $failureReject $fixture 'additional details property' - $fixture = & $copy $validFailure; $fixture.httpStatusCode = 401; & $failureReject $fixture 'HTTP status on simple code' - - if ([string]$failureReceipt.failure.stage -cne 'thread-start' -or - [int]$failureReceipt.failure.protocolCode -ne -32603 -or - [string]::Join(',', @($failureReceipt.failure.Keys | Sort-Object)) -cne - 'class,lane,protocolCode,stage,turn') { - throw 'Valid staged Codex failure receipt did not retain the safe fixed diagnostics.' - } - $script:codexProofSelfTestCases += 1 - - $proofIncompleteSummary = & $copy $validProofSummary - $proofIncompleteSummary.paidCalls = 0 - $proofIncompleteSummary.attempted = $false - $proofIncompleteSummary.executed = $false - $proofIncompleteReceipt = New-SanitizedCodexFailureReceipt ` - -TurnFailure $null -FailureStage $null -ProtocolCode $null ` - -ChildResult $validChild -GitState $validGit ` - -ExpectedHead $expectedHead -ScriptSha256 $helperHash -ScriptBlob $gitObject ` - -HelperSha256 $helperHash -HelperBlob $gitObject -ExecutableSha256 $executableHash ` - -ReportSha256 $otherHash -ProofSummary $proofIncompleteSummary -TemporaryRootRemoved $true - $proofIncompleteFailure = [pscustomobject]$proofIncompleteReceipt.failure - Assert-SanitizedCodexFailureProjection -Failure $proofIncompleteFailure - if ([string]$proofIncompleteFailure.class -cne 'codex-proof-incomplete' -or - $null -ne $proofIncompleteFailure.stage -or - $null -ne $proofIncompleteFailure.protocolCode) { - throw 'Codex proof-incomplete failure receipt did not preserve safe null lifecycle diagnostics.' - } - $script:codexProofSelfTestCases += 1 - - $postTurnInvariantSummary = & $copy $validProofSummary - $postTurnInvariantSummary.executed = $true - $postTurnAccepted = $true - try { - $null = New-SanitizedCodexFailureReceipt ` - -TurnFailure $null -FailureStage $null -ProtocolCode $null ` - -ChildResult $validChild -GitState $validGit ` - -ExpectedHead $expectedHead -ScriptSha256 $helperHash -ScriptBlob $gitObject ` - -HelperSha256 $helperHash -HelperBlob $gitObject -ExecutableSha256 $executableHash ` - -ReportSha256 $otherHash -ProofSummary $postTurnInvariantSummary -TemporaryRootRemoved $true - } catch { - $postTurnAccepted = $false - } - if ($postTurnAccepted) { - throw 'Codex post-turn invariant failure accepted null lifecycle diagnostics.' - } - $script:codexProofSelfTestCases += 1 - - $stageReject = { - param($Value, [string]$Label) - $accepted = $true - try { $null = Assert-CodexFailureStage -Value $Value } catch { $accepted = $false } - if ($accepted) { throw "Codex stage validator accepted invalid fixture: $Label" } - $script:codexProofSelfTestCases += 1 - } - & $stageReject 'future-stage' 'unknown stage' - - $protocolReject = { - param($Value, [string]$Label) - $accepted = $true - try { $null = Assert-NullableProtocolCode -Value $Value } catch { $accepted = $false } - if ($accepted) { throw "Codex protocol-code validator accepted invalid fixture: $Label" } - $script:codexProofSelfTestCases += 1 - } - & $protocolReject '-32603' 'string code' - & $protocolReject ([double]-32603.5) 'fractional code' - & $protocolReject ([long][int]::MinValue - 1) 'low overflow' - & $protocolReject ([long][int]::MaxValue + 1) 'high overflow' - - $projectionReject = { - param($Fixture, [string]$Label) - $accepted = $true - try { Assert-SanitizedCodexFailureProjection -Failure $Fixture } catch { $accepted = $false } - if ($accepted) { throw "Codex failure projection accepted invalid fixture: $Label" } - $script:codexProofSelfTestCases += 1 - } - $fixture = & $copy $failureReceipt.failure - $fixture | Add-Member -NotePropertyName message -NotePropertyValue 'C:\secret\auth.json' - & $projectionReject $fixture 'vendor message' - $fixture = & $copy $failureReceipt.failure - $fixture | Add-Member -NotePropertyName data -NotePropertyValue 'file:///private/detail' - & $projectionReject $fixture 'vendor data' - - $setupReportPath = [IO.Path]::GetFullPath((Join-Path ([IO.Path]::GetTempPath()) 'codex-setup-selftest\report.json')) - $validSetupSummary = [pscustomobject][ordered]@{ - pass = $true - paidCalls = 0 - markerMatched = $false - reportPath = $setupReportPath - reportSha256 = $otherHash - sessionId = $null - turnFailure = $null - failureStage = $null - protocolCode = $null - completedStage = 'thread-start' - modelCalls = 0 - turnStartCalls = 0 - closeClean = $true - } - $validSetupReport = [pscustomobject][ordered]@{ - schemaVersion = 1 - kind = 'codex-tool-denial-and-official-auth' - pass = $true - paidCalls = 0 - diagnostic = [pscustomobject][ordered]@{ - modelCalls = 0 - turnStartCalls = 0 - failureStage = $null - protocolCode = $null - completedStage = 'thread-start' - closeClean = $true - } - proof = [pscustomobject][ordered]@{ executed = $false; pass = $null; paidCalls = 0 } - source = [pscustomobject][ordered]@{ - expectedHead = $expectedHead - observedHead = $expectedHead - tree = $gitObject - scriptBlob = $gitObject - trackedClean = $true - } - executable = [pscustomobject][ordered]@{ sha256 = $executableHash } - artifacts = [pscustomobject][ordered]@{ scriptSha256 = $helperHash } - } - $validateSetup = { - param($Summary, $Report) - Assert-CodexSetupPreflight -Summary $Summary -Report $Report ` - -ExpectedHead $expectedHead -ExpectedTree $gitObject -ExpectedHelperBlob $gitObject ` - -ExpectedHelperSha256 $helperHash -ExpectedExecutableSha256 $executableHash ` - -ExpectedReportPath $setupReportPath -ExpectedReportSha256 $otherHash - } - & $validateSetup (& $copy $validSetupSummary) (& $copy $validSetupReport) - $script:codexProofSelfTestCases += 1 - $setupReject = { - param($Summary, [string]$Label) - $accepted = $true - try { & $validateSetup $Summary (& $copy $validSetupReport) } catch { $accepted = $false } - if ($accepted) { throw "Codex setup validator accepted invalid fixture: $Label" } - $script:codexProofSelfTestCases += 1 - } - $fixture = & $copy $validSetupSummary; $fixture.paidCalls = 1; & $setupReject $fixture 'paid call' - $fixture = & $copy $validSetupSummary; $fixture.modelCalls = 1; & $setupReject $fixture 'model call' - $fixture = & $copy $validSetupSummary; $fixture.turnStartCalls = 1; & $setupReject $fixture 'turn start call' - $setupReportReject = { - param($Report, [string]$Label) - $accepted = $true - try { & $validateSetup (& $copy $validSetupSummary) $Report } catch { $accepted = $false } - if ($accepted) { throw "Codex setup validator accepted invalid report fixture: $Label" } - $script:codexProofSelfTestCases += 1 - } - $fixture = & $copy $validSetupReport; $fixture.proof.executed = $true - & $setupReportReject $fixture 'offline proof executed' - $fixture = & $copy $validSetupReport; $fixture.proof.pass = $true - & $setupReportReject $fixture 'offline proof pass' - - return [pscustomobject]@{ - pass = $true - paidCalls = 0 - cases = $script:codexProofSelfTestCases - } -} - -function Get-Utf8ByteCount([string]$Value) { - return [Text.Encoding]::UTF8.GetByteCount($Value) -} - -function Get-RegularFileHash([string]$Path, [string]$Label) { - if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "$Label is missing." } - $item = Get-Item -LiteralPath $Path -Force - if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { - throw "$Label must be a regular file, not a reparse point." - } - return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant() -} - -function Resolve-Application([string]$Name) { - $command = Get-Command -Name $Name -CommandType Application -ErrorAction Stop | Select-Object -First 1 - $null = Get-RegularFileHash -Path $command.Source -Label "$Name executable" - return $command.Source -} - -function Get-GitState([string]$RepositoryRoot) { - $head = (& git.exe -C $RepositoryRoot rev-parse HEAD 2>$null).Trim() - if ($LASTEXITCODE -ne 0) { throw 'Could not read repository HEAD.' } - $tree = (& git.exe -C $RepositoryRoot rev-parse 'HEAD^{tree}' 2>$null).Trim() - if ($LASTEXITCODE -ne 0) { throw 'Could not read repository tree.' } - $status = [string]::Join("`n", @(& git.exe -C $RepositoryRoot status --porcelain=v1 --untracked-files=no 2>$null)).Trim() - if ($LASTEXITCODE -ne 0) { throw 'Could not read tracked repository status.' } - return [pscustomobject]@{ Head = $head; Tree = $tree; TrackedClean = $status.Length -eq 0 } -} - -function Get-ScriptProvenance([string]$RepositoryRoot, [string]$ScriptPath) { - $relativePath = [IO.Path]::GetRelativePath($RepositoryRoot, $ScriptPath).Replace('\', '/') - if ($relativePath.StartsWith('../', [StringComparison]::Ordinal) -or - [IO.Path]::IsPathRooted($relativePath)) { - throw 'Official-auth canary script must be inside the repository.' - } - $trackedPath = [string]::Join("`n", @( - & git.exe -C $RepositoryRoot ls-files --error-unmatch -- $relativePath 2>$null - )).Trim() - if ($LASTEXITCODE -ne 0 -or $trackedPath -cne $relativePath) { - throw 'Official-auth canary script must be tracked at ExpectedHead.' - } - $expectedBlob = (& git.exe -C $RepositoryRoot rev-parse "$ExpectedHead`:$relativePath" 2>$null).Trim() - if ($LASTEXITCODE -ne 0 -or $expectedBlob -notmatch '^[0-9a-f]{40}$') { - throw 'Could not resolve the official-auth canary script blob at ExpectedHead.' - } - $workingBlob = (& git.exe -C $RepositoryRoot hash-object "--path=$relativePath" -- $ScriptPath 2>$null).Trim() - if ($LASTEXITCODE -ne 0 -or $workingBlob -notmatch '^[0-9a-f]{40}$') { - throw 'Could not hash the filtered official-auth canary script working file.' - } - if ($workingBlob -cne $expectedBlob) { - throw 'Official-auth canary script working blob does not match ExpectedHead.' - } - return [pscustomobject]@{ - RelativePath = $relativePath - Blob = $expectedBlob - } -} - -function Assert-ExpectedGitState($State) { - if ($State.Head -cne $ExpectedHead) { - throw "Expected HEAD $ExpectedHead but observed $($State.Head)." - } - if (-not $State.TrackedClean) { throw 'Tracked repository state must be clean.' } -} - -function Invoke-CapturedProcess( - [string]$FilePath, - [string[]]$ArgumentList, - [string]$WorkingDirectory, - [hashtable]$EnvironmentOverrides, - [string[]]$BlankEnvironmentNames, - [int]$TimeoutSeconds -) { - $start = [Diagnostics.ProcessStartInfo]::new() - $start.FileName = $FilePath - $start.WorkingDirectory = $WorkingDirectory - $start.UseShellExecute = $false - $start.CreateNoWindow = $true - $start.RedirectStandardOutput = $true - $start.RedirectStandardError = $true - foreach ($argument in $ArgumentList) { $null = $start.ArgumentList.Add($argument) } - foreach ($entry in $EnvironmentOverrides.GetEnumerator()) { - $start.Environment[[string]$entry.Key] = [string]$entry.Value - } - foreach ($name in @($start.Environment.Keys)) { - if ([string]$name -match '(?i)(?:API_KEY|AUTH_TOKEN|ACCESS_TOKEN|_TOKEN)$') { - $start.Environment[[string]$name] = '' - } - } - foreach ($name in $BlankEnvironmentNames) { $start.Environment[$name] = '' } - - $profilePreserved = $true - foreach ($name in $profileEnvironmentNames) { - $parentHas = [Environment]::GetEnvironmentVariables().Contains($name) - $childHas = $start.Environment.ContainsKey($name) - if ($parentHas -ne $childHas) { $profilePreserved = $false; continue } - if ($parentHas -and $start.Environment[$name] -cne [Environment]::GetEnvironmentVariable($name)) { - $profilePreserved = $false - } - } - if (-not $profilePreserved) { throw 'A child process would override a user profile environment variable.' } - - $process = [Diagnostics.Process]::new() - $process.StartInfo = $start - $startedAt = [DateTimeOffset]::UtcNow - try { - if (-not $process.Start()) { throw 'Process did not start.' } - $stdoutTask = $process.StandardOutput.ReadToEndAsync() - $stderrTask = $process.StandardError.ReadToEndAsync() - $timedOut = -not $process.WaitForExit($TimeoutSeconds * 1000) - if ($timedOut) { - try { $process.Kill($true) } catch { } - $process.WaitForExit() - } - $stdout = $stdoutTask.GetAwaiter().GetResult() - $stderr = $stderrTask.GetAwaiter().GetResult() - return [pscustomobject]@{ - ExitCode = if ($timedOut) { $null } else { $process.ExitCode } - TimedOut = $timedOut - DurationMs = [int]([DateTimeOffset]::UtcNow - $startedAt).TotalMilliseconds - Stdout = $stdout - Stderr = $stderr - StdoutBytes = Get-Utf8ByteCount $stdout - StderrBytes = Get-Utf8ByteCount $stderr - StdoutSha256 = Get-Sha256Text $stdout - StderrSha256 = Get-Sha256Text $stderr - ProfileEnvironmentPreserved = $profilePreserved - } - } finally { - $process.Dispose() - } -} - -function Assert-ProcessPassed($Result, [string]$Label) { - if ($Result.TimedOut) { throw "$Label timed out." } - if ($Result.ExitCode -ne 0) { throw "$Label exited non-zero ($($Result.ExitCode))." } -} - -function Remove-OwnedFile([string]$Path, [string]$OwnedRoot) { - if (-not (Test-Path -LiteralPath $Path)) { return } - $resolved = [IO.Path]::GetFullPath($Path) - $prefix = [IO.Path]::GetFullPath($OwnedRoot).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) + [IO.Path]::DirectorySeparatorChar - if (-not $resolved.StartsWith($prefix, [StringComparison]::OrdinalIgnoreCase)) { - throw 'Refusing to remove a file outside the run-owned root.' - } - $item = Get-Item -LiteralPath $resolved -Force - if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { - throw 'Refusing to remove a non-regular run artifact.' - } - [IO.File]::Delete($resolved) -} - -function Remove-OwnedDirectory([string]$Path, [string]$RequiredParent) { - if (-not (Test-Path -LiteralPath $Path)) { return } - $resolved = [IO.Path]::GetFullPath($Path) - $prefix = [IO.Path]::GetFullPath($RequiredParent).TrimEnd( - [IO.Path]::DirectorySeparatorChar, - [IO.Path]::AltDirectorySeparatorChar - ) + [IO.Path]::DirectorySeparatorChar - if (-not $resolved.StartsWith($prefix, [StringComparison]::OrdinalIgnoreCase)) { - throw 'Refusing to remove a directory outside the run-owned parent.' - } - $item = Get-Item -LiteralPath $resolved -Force - if (-not $item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { - throw 'Refusing to remove an unsafe run directory.' - } - $reparse = Get-ChildItem -LiteralPath $resolved -Force -Recurse -Attributes ReparsePoint -ErrorAction SilentlyContinue | - Select-Object -First 1 - if ($null -ne $reparse) { throw 'Refusing cleanup because the run directory contains a reparse point.' } - $lastError = $null - for ($attempt = 0; $attempt -lt 20; $attempt += 1) { - try { - [IO.Directory]::Delete($resolved, $true) - return - } catch { - $lastError = $_ - Start-Sleep -Milliseconds 250 - } - } - throw $lastError -} - -function Write-JsonCreateNew([string]$Path, $Value) { - $json = ConvertTo-Json -InputObject $Value -Depth 20 - $stream = [IO.File]::Open($Path, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None) - try { - $writer = [IO.StreamWriter]::new($stream, [Text.UTF8Encoding]::new($false)) - try { $writer.Write($json); $writer.Write("`n"); $writer.Flush(); $stream.Flush($true) } finally { $writer.Dispose() } - } finally { - $stream.Dispose() - } -} - -function Initialize-IsolatedMindWorkspace([string]$MindRoot, [string]$WorkspaceId) { - $workspaceDir = Join-Path (Join-Path $MindRoot 'workspaces') $WorkspaceId - $null = [IO.Directory]::CreateDirectory($workspaceDir) - Write-JsonCreateNew -Path (Join-Path $workspaceDir 'workspace.json') -Value ([ordered]@{ id = $WorkspaceId }) - return Join-Path $workspaceDir 'workspace.mind' -} - -function Get-MarkerCaptureEvidence( - [string]$NodeExe, - [string]$BetterSqliteEntry, - [string]$MindPath, - [string]$Marker, - [string]$Source, - [string]$SessionId, - [string]$WorkingDirectory, - [hashtable]$EnvironmentOverrides, - [string[]]$BlankEnvironmentNames -) { - $mindSha256 = Get-RegularFileHash -Path $MindPath -Label "$Source isolated workspace mind" - $inspectionSource = @' -const crypto = require('node:crypto'); -const [modulePath, mindPath, marker, source, sessionId] = process.argv.slice(1); -const Database = require(modulePath); -const db = new Database(mindPath, { readonly: true, fileMustExist: true }); -try { - db.pragma('query_only = ON'); - const rows = db.prepare( - 'SELECT content, importance, source FROM memory_frames WHERE instr(content, ?) > 0 ORDER BY id' - ).all(marker); - const belongsToSession = (content) => - content.startsWith(`[hm session:${sessionId} `) && content.includes(` src:${source} event:`); - const matchingSession = rows.filter((row) => belongsToSession(row.content)); - const prompts = rows.filter((row) => - belongsToSession(row.content) - && row.content.includes(` src:${source} event:user-prompt-submit] `) - && row.importance === 'temporary' - && row.source === 'system' - ); - const responses = rows.filter((row) => - belongsToSession(row.content) - && row.content.includes(` src:${source} event:stop] `) - && (row.importance === 'important' || row.importance === 'critical') - && row.source === 'system' - ); - const hash = (value) => crypto.createHash('sha256').update(value, 'utf8').digest('hex'); - process.stdout.write(JSON.stringify({ - markerFrames: rows.length, - matchingSessionFrames: matchingSession.length, - promptFrames: prompts.length, - responseFrames: responses.length, - systemSourceFrames: rows.filter((row) => row.source === 'system').length, - temporaryFrames: rows.filter((row) => row.importance === 'temporary').length, - importantOrCriticalFrames: rows.filter((row) => - row.importance === 'important' || row.importance === 'critical' - ).length, - promptContentSha256: prompts[0] ? hash(prompts[0].content) : null, - responseContentSha256: responses[0] ? hash(responses[0].content) : null, - })); - if (rows.length !== 2 || matchingSession.length !== 2 || - prompts.length !== 1 || responses.length !== 1) process.exitCode = 2; -} finally { - db.close(); -} -'@ - $inspection = Invoke-CapturedProcess -FilePath $NodeExe -ArgumentList @( - '-e', $inspectionSource, '--', $BetterSqliteEntry, $MindPath, $Marker, $Source, $SessionId - ) -WorkingDirectory $WorkingDirectory -EnvironmentOverrides $EnvironmentOverrides ` - -BlankEnvironmentNames $BlankEnvironmentNames -TimeoutSeconds 30 - try { $evidence = $inspection.Stdout | ConvertFrom-Json -Depth 10 } catch { - throw "$Source marker capture inspection did not return valid JSON." - } - if ($inspection.TimedOut -or $inspection.ExitCode -ne 0 -or - [int]$evidence.markerFrames -ne 2 -or [int]$evidence.matchingSessionFrames -ne 2 -or - [int]$evidence.promptFrames -ne 1 -or - [int]$evidence.responseFrames -ne 1 -or - [string]$evidence.promptContentSha256 -notmatch '^[0-9a-f]{64}$' -or - [string]$evidence.responseContentSha256 -notmatch '^[0-9a-f]{64}$') { - throw "$Source marker capture evidence did not satisfy the two-frame contract " + - "(exit=$($inspection.ExitCode), timeout=$($inspection.TimedOut), marker=$($evidence.markerFrames), " + - "session=$($evidence.matchingSessionFrames), prompt=$($evidence.promptFrames), " + - "response=$($evidence.responseFrames), system=$($evidence.systemSourceFrames), " + - "temporary=$($evidence.temporaryFrames), important=$($evidence.importantOrCriticalFrames))." - } - return [pscustomobject]@{ - MindSha256 = $mindSha256 - MarkerFrames = [int]$evidence.markerFrames - PromptFrames = [int]$evidence.promptFrames - ResponseFrames = [int]$evidence.responseFrames - PromptContentSha256 = [string]$evidence.promptContentSha256 - ResponseContentSha256 = [string]$evidence.responseContentSha256 - } -} - -if ($env:OS -ne 'Windows_NT') { throw 'Official-auth canaries are Windows-only.' } -if ($PSVersionTable.PSVersion.Major -lt 7) { throw 'Official-auth canaries require PowerShell 7 or newer.' } -if ($CodexProofValidatorSelfTest) { - Invoke-CodexProofValidatorSelfTest | ConvertTo-Json -Compress - return -} -if ([bool]$StaticPreflightOnly -eq [bool]$Execute) { - throw 'Choose exactly one mode: -StaticPreflightOnly or -Execute.' -} -if ($Execute -and $PaidRunAck -cne $executionAcknowledgement) { - throw "-Execute requires -PaidRunAck $executionAcknowledgement." -} -Assert-PlainIdentifier -Value $ClaudeModel -Name 'ClaudeModel' -Assert-PlainIdentifier -Value $CodexModel -Name 'CodexModel' -Assert-PlainIdentifier -Value $HermesProvider -Name 'HermesProvider' -Assert-PlainIdentifier -Value $HermesModel -Name 'HermesModel' - -$repoRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..')).Path -$receiptLayout = Resolve-ReceiptLayout -RequestedPath $ReceiptDir -$initialGit = Get-GitState -RepositoryRoot $repoRoot -Assert-ExpectedGitState -State $initialGit -$scriptProvenance = Get-ScriptProvenance -RepositoryRoot $repoRoot -ScriptPath $PSCommandPath -$claudeExe = Resolve-Application -Name 'claude' -$codexExe = Resolve-Application -Name 'codex.exe' -$hermesExe = Resolve-Application -Name 'hermes' -$nodeExe = Resolve-Application -Name 'node' -$windowsPowerShell = Join-Path ([Environment]::SystemDirectory) 'WindowsPowerShell\v1.0\powershell.exe' -$null = Get-RegularFileHash -Path $windowsPowerShell -Label 'OS Windows PowerShell' -$scriptSha256 = Get-RegularFileHash -Path $PSCommandPath -Label 'official-auth canary script' -$codexExecutableSha256 = Get-RegularFileHash -Path $codexExe -Label 'Codex executable' -$artifactPaths = [ordered]@{ - claudeInstaller = Join-Path $repoRoot 'packages\hive-mind-hooks-claude-code\dist\bin\claude-code-hooks-cli.js' - claudeSessionStart = Join-Path $repoRoot 'packages\hive-mind-hooks-claude-code\dist\hooks\session-start.js' - claudeUserPromptSubmit = Join-Path $repoRoot 'packages\hive-mind-hooks-claude-code\dist\hooks\user-prompt-submit.js' - claudeStop = Join-Path $repoRoot 'packages\hive-mind-hooks-claude-code\dist\hooks\stop.js' - codexInstaller = Join-Path $repoRoot 'packages\hive-mind-hooks-codex\dist\bin\codex-hooks.js' - codexSessionStart = Join-Path $repoRoot 'packages\hive-mind-hooks-codex\dist\hooks\session-start.js' - codexUserPromptSubmit = Join-Path $repoRoot 'packages\hive-mind-hooks-codex\dist\hooks\user-prompt-submit.js' - codexStop = Join-Path $repoRoot 'packages\hive-mind-hooks-codex\dist\hooks\stop.js' - codexPreCompact = Join-Path $repoRoot 'packages\hive-mind-hooks-codex\dist\hooks\pre-compact.js' - codexToolDenial = Join-Path $repoRoot 'scripts\verify-codex-tool-denial.mjs' - hiveMindCli = Join-Path $repoRoot 'packages\hive-mind-cli\dist\index.js' - hermesInstaller = Join-Path $repoRoot 'packages\hive-mind-hooks-hermes\dist\bin\hermes-hooks.js' - hermesSessionStart = Join-Path $repoRoot 'packages\hive-mind-hooks-hermes\dist\hooks\session-start.js' - hermesUserPromptSubmit = Join-Path $repoRoot 'packages\hive-mind-hooks-hermes\dist\hooks\user-prompt-submit.js' - hermesStop = Join-Path $repoRoot 'packages\hive-mind-hooks-hermes\dist\hooks\stop.js' - betterSqlite3Entry = Join-Path $repoRoot 'node_modules\better-sqlite3\lib\index.js' -} -$artifactHashes = [ordered]@{} -foreach ($entry in $artifactPaths.GetEnumerator()) { - $artifactHashes[$entry.Key] = Get-RegularFileHash -Path $entry.Value -Label $entry.Key -} -$codexHelperProvenance = Get-ScriptProvenance -RepositoryRoot $repoRoot -ScriptPath $artifactPaths.codexToolDenial - -if ($StaticPreflightOnly) { - [pscustomobject]@{ - schemaVersion = 1 - mode = 'static-preflight' - source = [ordered]@{ - head = $initialGit.Head - tree = $initialGit.Tree - trackedClean = $initialGit.TrackedClean - scriptSha256 = $scriptSha256 - scriptBlob = $scriptProvenance.Blob - codexHelperSha256 = $artifactHashes.codexToolDenial - codexHelperBlob = $codexHelperProvenance.Blob - } - receipt = [ordered]@{ freshAllowedPath = $true; created = $false } - clients = [ordered]@{ - claudeResolved = $true - codexResolved = $true - hermesResolved = $true - nodeResolved = $true - claudeExecutableSha256 = Get-RegularFileHash -Path $claudeExe -Label 'Claude executable' - codexExecutableSha256 = $codexExecutableSha256 - hermesExecutableSha256 = Get-RegularFileHash -Path $hermesExe -Label 'Hermes executable' - } - artifacts = $artifactHashes - authCalls = 0 - modelCalls = 0 - } | ConvertTo-Json -Depth 10 - return -} - -$tempParent = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()) -Assert-NoExistingReparsePoint -Path $tempParent -FailureMessage 'Temporary parent is a reparse point' -$tempRoot = Join-Path $tempParent "waggle-official-auth-$([guid]::NewGuid().ToString('N'))" -$stagingOwned = $false -$tempOwned = $false -$published = $false -$hermesUsageRemoved = $false - -try { - $null = [IO.Directory]::CreateDirectory($tempRoot) - $tempOwned = $true - $claudeWorkspace = Join-Path $tempRoot 'claude-workspace' - $codexWorkspace = Join-Path $tempRoot 'codex-workspace' - $hermesWorkspace = Join-Path $tempRoot 'hermes-workspace' - $mindRoot = Join-Path $tempRoot 'mind' - $processTemp = Join-Path $tempRoot 'process-temp' - $null = [IO.Directory]::CreateDirectory($claudeWorkspace) - $null = [IO.Directory]::CreateDirectory($codexWorkspace) - $null = [IO.Directory]::CreateDirectory($hermesWorkspace) - $null = [IO.Directory]::CreateDirectory($mindRoot) - $null = [IO.Directory]::CreateDirectory($processTemp) - $claudeWorkspaceId = "official-auth-claude-$([guid]::NewGuid().ToString('N'))" - $codexWorkspaceId = "official-auth-codex-$([guid]::NewGuid().ToString('N'))" - $hermesWorkspaceId = "official-auth-hermes-$([guid]::NewGuid().ToString('N'))" - $claudeMindPath = Initialize-IsolatedMindWorkspace -MindRoot $mindRoot -WorkspaceId $claudeWorkspaceId - $codexMindPath = Initialize-IsolatedMindWorkspace -MindRoot $mindRoot -WorkspaceId $codexWorkspaceId - $hermesMindPath = Initialize-IsolatedMindWorkspace -MindRoot $mindRoot -WorkspaceId $hermesWorkspaceId - $commonEnvironment = @{ - HIVE_MIND_DATA_DIR = $mindRoot - HIVE_MIND_SCOPES = 'memory:read,memory:write' - HIVE_MIND_SHIM_LOG_LEVEL = 'error' - WAGGLE_SIGNAL_EMIT = '0' - NO_COLOR = '1' - TEMP = $processTemp - TMP = $processTemp - } - $claudeEnvironment = $commonEnvironment.Clone() - $claudeEnvironment.WAGGLE_WORKSPACE_ID = $claudeWorkspaceId - $codexEnvironment = $commonEnvironment.Clone() - $codexEnvironment.WAGGLE_WORKSPACE_ID = $codexWorkspaceId - $hermesEnvironment = $commonEnvironment.Clone() - $hermesEnvironment.WAGGLE_WORKSPACE_ID = $hermesWorkspaceId - - $claudeVersionRaw = Invoke-CapturedProcess -FilePath $claudeExe -ArgumentList @('--version') ` - -WorkingDirectory $claudeWorkspace -EnvironmentOverrides $claudeEnvironment ` - -BlankEnvironmentNames $claudeAlternativeAuthNames -TimeoutSeconds 30 - Assert-ProcessPassed -Result $claudeVersionRaw -Label 'Claude version preflight' - $claudeVersion = ($claudeVersionRaw.Stdout -split '\r?\n' | Where-Object { $_.Trim() } | Select-Object -First 1).Trim() - if ($claudeVersion -notmatch '^2\.') { throw 'Claude version preflight returned an unexpected version.' } - - $codexVersionRaw = Invoke-CapturedProcess -FilePath $codexExe -ArgumentList @('--version') ` - -WorkingDirectory $codexWorkspace -EnvironmentOverrides $codexEnvironment ` - -BlankEnvironmentNames $codexAlternativeAuthNames -TimeoutSeconds 30 - Assert-ProcessPassed -Result $codexVersionRaw -Label 'Codex version preflight' - $codexVersion = (($codexVersionRaw.Stdout + "`n" + $codexVersionRaw.Stderr) -split '\r?\n' | - Where-Object { $_.Trim() } | Select-Object -First 1).Trim() - if ($codexVersion -notmatch '^codex(?:-cli)?\s') { - throw 'Codex version preflight returned an unexpected version.' - } - - $hermesVersionRaw = Invoke-CapturedProcess -FilePath $hermesExe -ArgumentList @('--version') ` - -WorkingDirectory $hermesWorkspace -EnvironmentOverrides $hermesEnvironment ` - -BlankEnvironmentNames $hermesAlternativeAuthNames -TimeoutSeconds 30 - Assert-ProcessPassed -Result $hermesVersionRaw -Label 'Hermes version preflight' - $hermesVersion = ($hermesVersionRaw.Stdout -split '\r?\n' | Where-Object { $_ -match '^Hermes Agent v' } | Select-Object -First 1).Trim() - if ([string]::IsNullOrWhiteSpace($hermesVersion)) { throw 'Hermes version preflight returned an unexpected version.' } - - $hookBlankNames = @( - $claudeAlternativeAuthNames + $codexAlternativeAuthNames + $hermesAlternativeAuthNames | - Select-Object -Unique - ) - $claudeHookVerify = Invoke-CapturedProcess -FilePath $nodeExe ` - -ArgumentList @($artifactPaths.claudeInstaller, 'verify') -WorkingDirectory $repoRoot ` - -EnvironmentOverrides $commonEnvironment -BlankEnvironmentNames $hookBlankNames -TimeoutSeconds 60 - Assert-ProcessPassed -Result $claudeHookVerify -Label 'Claude hook verification' - $claudeHookPasses = [regex]::Matches($claudeHookVerify.Stdout, '(?m)^\s*\[PASS\]').Count - if ($claudeHookPasses -lt 11 -or $claudeHookVerify.Stdout -notmatch 'All checks passed\.') { - throw 'Claude hook verification did not satisfy the 11-check contract.' - } - - $codexHookVerify = Invoke-CapturedProcess -FilePath $nodeExe ` - -ArgumentList @($artifactPaths.codexInstaller, 'verify') -WorkingDirectory $repoRoot ` - -EnvironmentOverrides $commonEnvironment -BlankEnvironmentNames $hookBlankNames -TimeoutSeconds 60 - Assert-ProcessPassed -Result $codexHookVerify -Label 'Codex hook verification' - $codexHookPasses = [regex]::Matches($codexHookVerify.Stdout, '(?m)^\s*\[PASS\]').Count - if ($codexHookPasses -lt 9 -or $codexHookVerify.Stdout -notmatch 'All checks passed\.') { - throw 'Codex hook verification did not satisfy the 9-check contract.' - } - - $hermesHookVerify = Invoke-CapturedProcess -FilePath $nodeExe ` - -ArgumentList @($artifactPaths.hermesInstaller, 'verify') -WorkingDirectory $repoRoot ` - -EnvironmentOverrides $commonEnvironment -BlankEnvironmentNames $hookBlankNames -TimeoutSeconds 60 - Assert-ProcessPassed -Result $hermesHookVerify -Label 'Hermes hook verification' - $hermesHookPasses = [regex]::Matches($hermesHookVerify.Stdout, '(?m)^\s*\[PASS\]').Count - if ($hermesHookPasses -lt 8 -or $hermesHookVerify.Stdout -notmatch 'All checks passed\.') { - throw 'Hermes hook verification did not satisfy the 8-check contract.' - } - - $claudeAuthRaw = Invoke-CapturedProcess -FilePath $claudeExe -ArgumentList @('auth', 'status', '--json') ` - -WorkingDirectory $claudeWorkspace -EnvironmentOverrides $claudeEnvironment ` - -BlankEnvironmentNames $claudeAlternativeAuthNames -TimeoutSeconds 30 - Assert-ProcessPassed -Result $claudeAuthRaw -Label 'Claude official-auth preflight' - try { $claudeAuthJson = $claudeAuthRaw.Stdout | ConvertFrom-Json -Depth 20 } catch { - throw 'Claude official-auth preflight did not return valid JSON.' - } - if ($claudeAuthJson.loggedIn -ne $true -or $claudeAuthJson.authMethod -cne 'claude.ai' -or - $claudeAuthJson.apiProvider -cne 'firstParty') { - throw 'Claude is not authenticated through the required first-party claude.ai client session.' - } - - $codexAuthRaw = Invoke-CapturedProcess -FilePath $codexExe -ArgumentList @('login', 'status') ` - -WorkingDirectory $codexWorkspace -EnvironmentOverrides $codexEnvironment ` - -BlankEnvironmentNames $codexAlternativeAuthNames -TimeoutSeconds 30 - Assert-ProcessPassed -Result $codexAuthRaw -Label 'Codex official-auth preflight' - $codexAuthText = "$($codexAuthRaw.Stdout)`n$($codexAuthRaw.Stderr)" - if ($codexAuthText -notmatch '(?m)^\s*Logged in using ChatGPT\s*$') { - throw 'Codex is not authenticated through the required first-party ChatGPT client session.' - } - - $hermesAuthRaw = Invoke-CapturedProcess -FilePath $hermesExe ` - -ArgumentList @('auth', 'status', $HermesProvider) -WorkingDirectory $hermesWorkspace ` - -EnvironmentOverrides $hermesEnvironment -BlankEnvironmentNames $hermesAlternativeAuthNames -TimeoutSeconds 30 - Assert-ProcessPassed -Result $hermesAuthRaw -Label 'Hermes official-auth preflight' - $escapedHermesProvider = [regex]::Escape($HermesProvider) - if ($hermesAuthRaw.Stdout -notmatch "(?m)^$escapedHermesProvider`:\s+logged in\s*$") { - throw 'Hermes is not logged in to the required provider.' - } - - $codexSetupDir = Join-Path $tempRoot 'codex-setup-preflight' - $codexSetupRaw = Invoke-CapturedProcess -FilePath $nodeExe -ArgumentList @( - $artifactPaths.codexToolDenial, - '--codex-exe', $codexExe, - '--hive-mind-cli', $artifactPaths.hiveMindCli, - '--receipt-dir', $codexSetupDir, - '--expected-head', $ExpectedHead, - '--model', $CodexModel, - '--windows-powershell', $windowsPowerShell, - '--workspace', $codexWorkspace, - '--setup-only' - ) -WorkingDirectory $codexWorkspace -EnvironmentOverrides $codexEnvironment ` - -BlankEnvironmentNames $codexAlternativeAuthNames -TimeoutSeconds 120 - Assert-ProcessPassed -Result $codexSetupRaw -Label 'Codex zero-inference setup preflight' - try { $codexSetupSummary = $codexSetupRaw.Stdout | ConvertFrom-Json -Depth 30 } catch { - throw 'Codex zero-inference setup preflight did not return valid JSON.' - } - $expectedCodexSetupReportPath = [IO.Path]::GetFullPath((Join-Path $codexSetupDir 'report.json')) - if (-not ($codexSetupSummary.reportPath -is [string]) -or - [IO.Path]::GetFullPath([string]$codexSetupSummary.reportPath) -cne $expectedCodexSetupReportPath -or - -not (Test-Path -LiteralPath $expectedCodexSetupReportPath -PathType Leaf)) { - throw 'Codex setup report path was missing or escaped the owned directory.' - } - $codexSetupReportItem = Get-Item -LiteralPath $expectedCodexSetupReportPath -Force - if (($codexSetupReportItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0 -or - $codexSetupReportItem.Length -gt 2MB) { - throw 'Codex setup report is unsafe or too large.' - } - $codexSetupReportSha256 = Get-RegularFileHash ` - -Path $expectedCodexSetupReportPath -Label 'Codex setup report' - $codexSetupReportBytes = [IO.File]::ReadAllBytes($expectedCodexSetupReportPath) - try { - $codexSetupReport = [Text.Encoding]::UTF8.GetString($codexSetupReportBytes) | - ConvertFrom-Json -Depth 50 - } catch { - throw 'Codex zero-inference setup report was not valid JSON.' - } - Assert-CodexSetupPreflight -Summary $codexSetupSummary -Report $codexSetupReport ` - -ExpectedHead $ExpectedHead -ExpectedTree $initialGit.Tree ` - -ExpectedHelperBlob $codexHelperProvenance.Blob ` - -ExpectedHelperSha256 $artifactHashes.codexToolDenial ` - -ExpectedExecutableSha256 $codexExecutableSha256 ` - -ExpectedReportPath $expectedCodexSetupReportPath ` - -ExpectedReportSha256 $codexSetupReportSha256 - - $claudeMarker = "WAGGLE_CLAUDE_OFFICIAL_AUTH_$([Security.Cryptography.RandomNumberGenerator]::GetHexString(24).ToLowerInvariant())" - $claudePrompt = "Return exactly $claudeMarker and nothing else. Do not call tools." - $claudeBudget = $ClaudeMaxUsd.ToString('0.00', [Globalization.CultureInfo]::InvariantCulture) - $claudeRaw = Invoke-CapturedProcess -FilePath $claudeExe -ArgumentList @( - '-p', $claudePrompt, - '--model', $ClaudeModel, - '--max-budget-usd', $claudeBudget, - '--output-format', 'stream-json', - '--include-hook-events', - '--verbose', - '--permission-mode', 'plan', - '--disable-slash-commands', - '--tools', '', - '--no-session-persistence', - '--setting-sources', 'user', - '--no-chrome' - ) -WorkingDirectory $claudeWorkspace -EnvironmentOverrides $claudeEnvironment ` - -BlankEnvironmentNames $claudeAlternativeAuthNames -TimeoutSeconds 180 - Assert-ProcessPassed -Result $claudeRaw -Label 'Claude official-auth canary' - - $claudeEvents = [Collections.Generic.List[object]]::new() - $invalidClaudeLines = 0 - foreach ($line in ($claudeRaw.Stdout -split '\r?\n')) { - if ([string]::IsNullOrWhiteSpace($line)) { continue } - try { $claudeEvents.Add(($line | ConvertFrom-Json -Depth 100)) } catch { $invalidClaudeLines += 1 } - } - if ($invalidClaudeLines -ne 0) { throw 'Claude emitted malformed stream JSON.' } - $claudeResult = @($claudeEvents | Where-Object { $_.type -eq 'result' }) | Select-Object -Last 1 - if ($null -eq $claudeResult -or $claudeResult.is_error -ne $false -or - ([string]$claudeResult.result).Trim() -cne $claudeMarker) { - throw 'Claude official-auth canary did not return the exact marker.' - } - $claudeCost = [decimal]$claudeResult.total_cost_usd - if ($claudeCost -lt 0 -or $claudeCost -gt $ClaudeMaxUsd) { throw 'Claude exceeded the declared cost cap.' } - if ([int]$claudeResult.num_turns -ne 1) { throw 'Claude official-auth canary was not exactly one turn.' } - $claudeHookEvents = @($claudeEvents | Where-Object { - $_.type -eq 'system' -and $_.subtype -in @('hook_started', 'hook_response') - } | ForEach-Object { [string]$_.hook_event } | Where-Object { $_ } | Sort-Object -Unique) - foreach ($requiredHook in @('SessionStart', 'UserPromptSubmit', 'Stop')) { - if ($claudeHookEvents -notcontains $requiredHook) { throw "Claude did not report $requiredHook hook execution." } - } - $claudeSessionId = [string]$claudeResult.session_id - if ([string]::IsNullOrWhiteSpace($claudeSessionId)) { - $claudeSessionId = [string](@($claudeEvents | Where-Object { $_.session_id } | Select-Object -First 1).session_id) - } - if ([string]::IsNullOrWhiteSpace($claudeSessionId)) { throw 'Claude omitted the session identifier.' } - $claudeCapture = Get-MarkerCaptureEvidence -NodeExe $nodeExe ` - -BetterSqliteEntry $artifactPaths.betterSqlite3Entry -MindPath $claudeMindPath ` - -Marker $claudeMarker -Source 'claude-code' -SessionId $claudeSessionId ` - -WorkingDirectory $repoRoot -EnvironmentOverrides $claudeEnvironment ` - -BlankEnvironmentNames $hookBlankNames - - $codexMarker = "WAGGLE_CODEX_OFFICIAL_AUTH_$([Security.Cryptography.RandomNumberGenerator]::GetHexString(24).ToLowerInvariant())" - $codexProofDir = Join-Path $tempRoot 'codex-tool-denial' - $codexRaw = Invoke-CapturedProcess -FilePath $nodeExe -ArgumentList @( - $artifactPaths.codexToolDenial, - '--codex-exe', $codexExe, - '--hive-mind-cli', $artifactPaths.hiveMindCli, - '--receipt-dir', $codexProofDir, - '--expected-head', $ExpectedHead, - '--model', $CodexModel, - '--windows-powershell', $windowsPowerShell, - '--workspace', $codexWorkspace, - '--execute-paid', - '--marker', $codexMarker, - '--ack', 'I_ACKNOWLEDGE_1_CODEX_OFFICIAL_AUTH_CALL' - ) -WorkingDirectory $codexWorkspace -EnvironmentOverrides $codexEnvironment ` - -BlankEnvironmentNames $codexAlternativeAuthNames -TimeoutSeconds 300 - if ($codexRaw.TimedOut -or $codexRaw.ExitCode -ne 0) { - try { $codexFailureSummary = $codexRaw.Stdout | ConvertFrom-Json -Depth 30 } catch { - throw 'Failed Codex child did not return a valid JSON summary.' - } - $summaryProperties = @($codexFailureSummary.PSObject.Properties.Name | Sort-Object) - $expectedSummaryProperties = @( - 'closeClean', - 'completedStage', - 'failureStage', - 'markerMatched', - 'modelCalls', - 'paidCalls', - 'pass', - 'protocolCode', - 'reportPath', - 'reportSha256', - 'sessionId', - 'turnFailure', - 'turnStartCalls' - ) - if ([string]::Join(',', $summaryProperties) -cne [string]::Join(',', $expectedSummaryProperties)) { - throw 'Failed Codex child summary contains unexpected or missing properties.' - } - Assert-JsonBoolean -Value $codexFailureSummary.pass -Expected $false -Label 'failed Codex summary pass' - if ((-not ($codexFailureSummary.paidCalls -is [int])) -and - (-not ($codexFailureSummary.paidCalls -is [long]))) { - throw 'Failed Codex summary paidCalls must be a JSON integer.' - } - if ([long]$codexFailureSummary.paidCalls -lt 0 -or [long]$codexFailureSummary.paidCalls -gt 1) { - throw 'Failed Codex summary paidCalls is out of range.' - } - if (-not ($codexFailureSummary.markerMatched -is [bool])) { - throw 'Failed Codex summary markerMatched must be a JSON boolean.' - } - $summaryFailureStage = Assert-CodexFailureStage ` - -Value $codexFailureSummary.failureStage -AllowNull $true - $summaryProtocolCode = Assert-NullableProtocolCode -Value $codexFailureSummary.protocolCode - $summaryCompletedStage = Assert-CodexFailureStage ` - -Value $codexFailureSummary.completedStage -AllowNull $true - foreach ($entry in @( - [pscustomobject]@{ Value = $codexFailureSummary.modelCalls; Label = 'modelCalls' } - [pscustomobject]@{ Value = $codexFailureSummary.turnStartCalls; Label = 'turnStartCalls' } - )) { - if ((-not ($entry.Value -is [int])) -and (-not ($entry.Value -is [long])) -or - [long]$entry.Value -lt 0 -or [long]$entry.Value -gt 1) { - throw "Failed Codex summary $($entry.Label) must be a JSON integer from zero to one." - } - } - if (-not ($codexFailureSummary.closeClean -is [bool])) { - throw 'Failed Codex summary closeClean must be a JSON boolean.' - } - Assert-Sha256 -Value $codexFailureSummary.reportSha256 -Label 'failed Codex report hash' - if (-not ($codexFailureSummary.reportPath -is [string])) { - throw 'Failed Codex summary reportPath must be a string.' - } - $expectedCodexFailureReportPath = [IO.Path]::GetFullPath((Join-Path $codexProofDir 'report.json')) - $codexFailureReportPath = [IO.Path]::GetFullPath([string]$codexFailureSummary.reportPath) - if ($codexFailureReportPath -cne $expectedCodexFailureReportPath -or - -not (Test-Path -LiteralPath $codexFailureReportPath -PathType Leaf)) { - throw 'Failed Codex child report path was missing or escaped the owned directory.' - } - $codexFailureReportItem = Get-Item -LiteralPath $codexFailureReportPath -Force - if (($codexFailureReportItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0 -or - $codexFailureReportItem.Length -gt 2MB) { - throw 'Failed Codex child report is unsafe or too large.' - } - $codexFailureReportSha256 = Get-RegularFileHash ` - -Path $codexFailureReportPath -Label 'failed Codex child report' - if ($codexFailureReportSha256 -cne [string]$codexFailureSummary.reportSha256) { - throw 'Failed Codex child report hash did not match its summary.' - } - $codexFailureReportBytes = [IO.File]::ReadAllBytes($codexFailureReportPath) - try { - $codexFailureReport = [Text.Encoding]::UTF8.GetString($codexFailureReportBytes) | - ConvertFrom-Json -Depth 50 - } catch { - throw 'Failed Codex child report was not valid JSON.' - } - Assert-JsonInteger -Value $codexFailureReport.schemaVersion -Expected 1 ` - -Label 'failed Codex report schemaVersion' - if (-not ($codexFailureReport.kind -is [string]) -or - [string]$codexFailureReport.kind -cne 'codex-tool-denial-and-official-auth') { - throw 'Failed Codex child report kind is invalid.' - } - Assert-JsonBoolean -Value $codexFailureReport.pass -Expected $false -Label 'failed Codex report pass' - Assert-JsonInteger -Value $codexFailureReport.paidCalls ` - -Expected ([long]$codexFailureSummary.paidCalls) -Label 'failed Codex report paidCalls' - if ([string]$codexFailureReport.source.expectedHead -cne $ExpectedHead -or - [string]$codexFailureReport.source.observedHead -cne $ExpectedHead -or - [string]$codexFailureReport.source.tree -cne $initialGit.Tree -or - [string]$codexFailureReport.source.scriptBlob -cne $codexHelperProvenance.Blob) { - throw 'Failed Codex child report source did not match the sealed revision.' - } - Assert-JsonBoolean -Value $codexFailureReport.source.trackedClean -Expected $true ` - -Label 'failed Codex report trackedClean' - if ([string]$codexFailureReport.executable.sha256 -cne $codexExecutableSha256 -or - [string]$codexFailureReport.artifacts.scriptSha256 -cne $artifactHashes.codexToolDenial) { - throw 'Failed Codex child report did not match the sealed executable or helper.' - } - $summaryTurnFailure = Assert-SanitizedCodexTurnFailure -Failure $codexFailureSummary.turnFailure - $reportTurnFailure = Assert-SanitizedCodexTurnFailure -Failure $codexFailureReport.turnFailure - if (($summaryTurnFailure | ConvertTo-Json -Compress) -cne - ($reportTurnFailure | ConvertTo-Json -Compress)) { - throw 'Failed Codex child summary and report disagree on the terminal status.' - } - $reportFailureStage = Assert-CodexFailureStage ` - -Value $codexFailureReport.diagnostic.failureStage -AllowNull $true - $reportProtocolCode = Assert-NullableProtocolCode -Value $codexFailureReport.diagnostic.protocolCode - $reportCompletedStage = Assert-CodexFailureStage ` - -Value $codexFailureReport.diagnostic.completedStage -AllowNull $true - if ([string]$reportFailureStage -cne [string]$summaryFailureStage -or - $reportProtocolCode -ne $summaryProtocolCode -or - [string]$reportCompletedStage -cne [string]$summaryCompletedStage) { - throw 'Failed Codex child summary and report disagree on lifecycle diagnostics.' - } - Assert-JsonInteger -Value $codexFailureReport.diagnostic.modelCalls ` - -Expected ([long]$codexFailureSummary.modelCalls) -Label 'failed Codex report modelCalls' - Assert-JsonInteger -Value $codexFailureReport.diagnostic.turnStartCalls ` - -Expected ([long]$codexFailureSummary.turnStartCalls) -Label 'failed Codex report turnStartCalls' - Assert-JsonBoolean -Value $codexFailureReport.diagnostic.closeClean ` - -Expected ([bool]$codexFailureSummary.closeClean) -Label 'failed Codex report closeClean' - Assert-JsonBoolean -Value $codexFailureReport.proof.executed -Expected $true ` - -Label 'failed Codex offline proof executed' - Assert-JsonBoolean -Value $codexFailureReport.proof.pass -Expected $true ` - -Label 'failed Codex offline proof pass' - Assert-JsonInteger -Value $codexFailureReport.proof.paidCalls -Expected 0 ` - -Label 'failed Codex offline proof paidCalls' - Assert-JsonBoolean -Value $codexFailureReport.paidInvocation.attempted -Expected $true ` - -Label 'failed Codex paid attempted' - foreach ($entry in @( - [pscustomobject]@{ Value = $codexFailureReport.paidInvocation.executed; Label = 'executed' } - [pscustomobject]@{ Value = $codexFailureReport.paidInvocation.normalTextCompleted; Label = 'normalTextCompleted' } - [pscustomobject]@{ Value = $codexFailureReport.mcpBoundary.unchanged; Label = 'MCP boundary unchanged' } - )) { - if (-not ($entry.Value -is [bool])) { - throw "Failed Codex report $($entry.Label) must be a JSON boolean." - } - } - foreach ($entry in @( - [pscustomobject]@{ Value = $codexFailureReport.paidInvocation.toolEventsObserved; Label = 'tool events' } - [pscustomobject]@{ Value = $codexFailureReport.paidInvocation.unknownEventsObserved; Label = 'unknown events' } - )) { - if ((-not ($entry.Value -is [int])) -and (-not ($entry.Value -is [long])) -or - [long]$entry.Value -lt 0) { - throw "Failed Codex report $($entry.Label) must be a non-negative JSON integer." - } - } - $failureGit = Get-GitState -RepositoryRoot $repoRoot - Assert-ExpectedGitState -State $failureGit - if ($failureGit.Tree -cne $initialGit.Tree) { - throw 'Repository tree changed during the failed Codex canary.' - } - $failureScriptProvenance = Get-ScriptProvenance -RepositoryRoot $repoRoot -ScriptPath $PSCommandPath - $failureHelperProvenance = Get-ScriptProvenance ` - -RepositoryRoot $repoRoot -ScriptPath $artifactPaths.codexToolDenial - if ($failureScriptProvenance.Blob -cne $scriptProvenance.Blob -or - $failureHelperProvenance.Blob -cne $codexHelperProvenance.Blob) { - throw 'Official-auth source provenance changed during the failed Codex canary.' - } - foreach ($entry in $artifactPaths.GetEnumerator()) { - if ((Get-RegularFileHash -Path $entry.Value -Label $entry.Key) -cne - [string]$artifactHashes[$entry.Key]) { - throw "$($entry.Key) changed during the failed Codex canary." - } - } - $failureProofSummary = [pscustomobject]@{ - pass = $false - paidCalls = [long]$codexFailureReport.paidCalls - offlinePass = $true - offlinePaidCalls = 0 - attempted = [bool]$codexFailureReport.paidInvocation.attempted - executed = [bool]$codexFailureReport.paidInvocation.executed - normalTextCompleted = [bool]$codexFailureReport.paidInvocation.normalTextCompleted - toolEventsObserved = [long]$codexFailureReport.paidInvocation.toolEventsObserved - unknownEventsObserved = [long]$codexFailureReport.paidInvocation.unknownEventsObserved - mcpBoundaryUnchanged = [bool]$codexFailureReport.mcpBoundary.unchanged - } - - Remove-OwnedDirectory -Path $tempRoot -RequiredParent $tempParent - $tempOwned = $false - if (Test-Path -LiteralPath $tempRoot) { - throw 'Official-auth temporary root survived failed Codex cleanup.' - } - - $failureReceipt = New-SanitizedCodexFailureReceipt ` - -TurnFailure $summaryTurnFailure -FailureStage $summaryFailureStage ` - -ProtocolCode $summaryProtocolCode -ChildResult $codexRaw -GitState $failureGit ` - -ExpectedHead $ExpectedHead -ScriptSha256 $scriptSha256 -ScriptBlob $scriptProvenance.Blob ` - -HelperSha256 $artifactHashes.codexToolDenial -HelperBlob $codexHelperProvenance.Blob ` - -ExecutableSha256 $codexExecutableSha256 -ReportSha256 $codexFailureReportSha256 ` - -ProofSummary $failureProofSummary -TemporaryRootRemoved $true - $null = [IO.Directory]::CreateDirectory($evidenceRoot) - Assert-NoExistingReparsePoint -Path $evidenceRoot -FailureMessage 'Evidence root is a reparse point' - $null = [IO.Directory]::CreateDirectory($receiptLayout.Staging) - $stagingOwned = $true - $failureReceiptPath = Join-Path $receiptLayout.Staging 'official-auth-failure.json' - Write-JsonCreateNew -Path $failureReceiptPath -Value $failureReceipt - $failureReceiptText = [IO.File]::ReadAllText($failureReceiptPath) - if ($failureReceiptText -match '(?i)(?:[A-Z]:[\\/]|file:///|\\\\[^\\/\s]+[\\/]|WAGGLE_(?:CLAUDE|CODEX|HERMES)_OFFICIAL_AUTH_|(?:sk|sess)-[A-Za-z0-9_-]+)') { - throw 'Sanitized failure receipt contains a forbidden path, marker, session, or credential token.' - } - if ($failureReceiptText -match '(?i)"(?:message|additionalDetails|reportPath|arguments|model|marker|sessionId|error|rawStdout|rawStderr)"\s*:') { - throw 'Sanitized failure receipt contains a forbidden property.' - } - foreach ($rawOutput in @($codexRaw.Stdout, $codexRaw.Stderr)) { - if (-not [string]::IsNullOrWhiteSpace([string]$rawOutput) -and - $failureReceiptText.Contains([string]$rawOutput, [StringComparison]::Ordinal)) { - throw 'Sanitized failure receipt retained raw child output.' - } - } - if (Test-Path -LiteralPath $receiptLayout.Root) { - throw 'Receipt destination became occupied.' - } - [IO.Directory]::Move($receiptLayout.Staging, $receiptLayout.Root) - $stagingOwned = $false - $published = $true - Assert-ProcessPassed -Result $codexRaw -Label 'Codex zero-cost tool-denial proof and official-auth canary' - } - Assert-ProcessPassed -Result $codexRaw -Label 'Codex zero-cost tool-denial proof and official-auth canary' - try { $codexSummary = $codexRaw.Stdout | ConvertFrom-Json -Depth 30 } catch { - throw 'Codex zero-cost tool-denial proof did not return valid JSON.' - } - $codexSummaryProperties = @($codexSummary.PSObject.Properties.Name | Sort-Object) - $expectedCodexSummaryProperties = @( - 'closeClean', - 'completedStage', - 'failureStage', - 'markerMatched', - 'modelCalls', - 'paidCalls', - 'pass', - 'protocolCode', - 'reportPath', - 'reportSha256', - 'sessionId', - 'turnFailure', - 'turnStartCalls' - ) - if ([string]::Join(',', $codexSummaryProperties) -cne - [string]::Join(',', $expectedCodexSummaryProperties)) { - throw 'Codex success summary contains unexpected or missing properties.' - } - Assert-JsonBoolean -Value $codexSummary.pass -Expected $true -Label 'Codex summary pass' - Assert-JsonInteger -Value $codexSummary.paidCalls -Expected 1 -Label 'Codex summary paidCalls' - Assert-JsonInteger -Value $codexSummary.modelCalls -Expected 1 -Label 'Codex summary modelCalls' - Assert-JsonInteger -Value $codexSummary.turnStartCalls -Expected 1 -Label 'Codex summary turnStartCalls' - Assert-JsonBoolean -Value $codexSummary.closeClean -Expected $true -Label 'Codex summary closeClean' - if ($null -ne $codexSummary.failureStage -or $null -ne $codexSummary.protocolCode -or - $null -ne $codexSummary.turnFailure -or - [string]$codexSummary.completedStage -cne 'post-turn-invariants') { - throw 'Codex success summary did not complete all paid-turn invariants cleanly.' - } - Assert-JsonBoolean -Value $codexSummary.markerMatched -Expected $true -Label 'Codex summary markerMatched' - Assert-Sha256 -Value $codexSummary.reportSha256 -Label 'Codex summary report hash' - if (-not ($codexSummary.reportPath -is [string])) { throw 'Codex summary reportPath must be a string.' } - if (-not ($codexSummary.sessionId -is [string])) { throw 'Codex summary sessionId must be a string.' } - $codexSessionId = [string]$codexSummary.sessionId - Assert-PlainIdentifier -Value $codexSessionId -Name 'Codex sessionId' - $expectedCodexReportPath = [IO.Path]::GetFullPath((Join-Path $codexProofDir 'report.json')) - $codexReportPath = [IO.Path]::GetFullPath([string]$codexSummary.reportPath) - if ($codexReportPath -cne $expectedCodexReportPath -or - -not (Test-Path -LiteralPath $codexReportPath -PathType Leaf)) { - throw 'Codex proof report path was missing or escaped the owned directory.' - } - $codexReportSha256 = Get-RegularFileHash -Path $codexReportPath -Label 'Codex tool-denial proof report' - if ($codexReportSha256 -cne [string]$codexSummary.reportSha256) { - throw 'Codex proof report hash did not match the process summary.' - } - $codexReportBytes = [IO.File]::ReadAllBytes($codexReportPath) - try { $codexDenialProof = [Text.Encoding]::UTF8.GetString($codexReportBytes) | ConvertFrom-Json -Depth 50 } catch { - throw 'Codex tool-denial proof report was not valid JSON.' - } - Assert-CodexToolDenialProof -Proof $codexDenialProof ` - -ExpectedExecutableSha256 $codexExecutableSha256 ` - -ExpectedHelperSha256 $artifactHashes.codexToolDenial ` - -ExpectedHead $ExpectedHead ` - -ExpectedHiveMindCliSha256 $artifactHashes.hiveMindCli ` - -ExpectedMarkerSha256 (Get-Sha256Text $codexMarker) ` - -ExpectedPowerShellSha256 (Get-RegularFileHash -Path $windowsPowerShell -Label 'OS Windows PowerShell') ` - -ExpectedSessionIdSha256 (Get-Sha256Text $codexSessionId) - $codexCapture = Get-MarkerCaptureEvidence -NodeExe $nodeExe ` - -BetterSqliteEntry $artifactPaths.betterSqlite3Entry -MindPath $codexMindPath ` - -Marker $codexMarker -Source 'codex' -SessionId $codexSessionId ` - -WorkingDirectory $repoRoot -EnvironmentOverrides $codexEnvironment ` - -BlankEnvironmentNames $hookBlankNames - - $hermesMarker = "WAGGLE_HERMES_OFFICIAL_AUTH_$([Security.Cryptography.RandomNumberGenerator]::GetHexString(24).ToLowerInvariant())" - $hermesPrompt = "Return exactly $hermesMarker and nothing else. Do not call tools." - $hermesUsagePath = Join-Path $tempRoot 'hermes-usage.json' - $hermesRaw = Invoke-CapturedProcess -FilePath $hermesExe -ArgumentList @( - '-z', $hermesPrompt, - '--usage-file', $hermesUsagePath, - '--provider', $HermesProvider, - '-m', $HermesModel, - '--ignore-rules' - ) -WorkingDirectory $hermesWorkspace -EnvironmentOverrides $hermesEnvironment ` - -BlankEnvironmentNames $hermesAlternativeAuthNames -TimeoutSeconds 180 - Assert-ProcessPassed -Result $hermesRaw -Label 'Hermes official-auth canary' - if ($hermesRaw.Stdout.Trim() -cne $hermesMarker) { throw 'Hermes official-auth canary did not return the exact marker.' } - if (-not (Test-Path -LiteralPath $hermesUsagePath -PathType Leaf)) { throw 'Hermes did not write its usage receipt.' } - $hermesUsageBytes = [IO.File]::ReadAllBytes($hermesUsagePath) - $hermesUsageSha256 = [Convert]::ToHexString( - [Security.Cryptography.SHA256]::HashData($hermesUsageBytes) - ).ToLowerInvariant() - try { $hermesUsage = [Text.Encoding]::UTF8.GetString($hermesUsageBytes) | ConvertFrom-Json -Depth 20 } catch { - throw 'Hermes usage receipt was not valid JSON.' - } - if ($hermesUsage.completed -ne $true -or $hermesUsage.failed -eq $true -or [int]$hermesUsage.api_calls -ne 1) { - throw 'Hermes usage receipt did not prove exactly one completed API call.' - } - if ([string]$hermesUsage.provider -cne $HermesProvider -or [string]$hermesUsage.model -cne $HermesModel) { - throw 'Hermes usage receipt did not match the requested provider and model.' - } - $hermesSessionId = [string]$hermesUsage.session_id - if ([string]::IsNullOrWhiteSpace($hermesSessionId)) { throw 'Hermes usage receipt omitted the session identifier.' } - $hermesCapture = Get-MarkerCaptureEvidence -NodeExe $nodeExe ` - -BetterSqliteEntry $artifactPaths.betterSqlite3Entry -MindPath $hermesMindPath ` - -Marker $hermesMarker -Source 'hermes' -SessionId $hermesSessionId ` - -WorkingDirectory $repoRoot -EnvironmentOverrides $hermesEnvironment ` - -BlankEnvironmentNames $hookBlankNames - Remove-OwnedFile -Path $hermesUsagePath -OwnedRoot $tempRoot - $hermesUsageRemoved = -not (Test-Path -LiteralPath $hermesUsagePath) - if (-not $hermesUsageRemoved) { throw 'Hermes raw usage receipt survived cleanup.' } - - $finalGit = Get-GitState -RepositoryRoot $repoRoot - Assert-ExpectedGitState -State $finalGit - if ($finalGit.Tree -cne $initialGit.Tree) { throw 'Repository tree changed during official-auth canaries.' } - $finalScriptProvenance = Get-ScriptProvenance -RepositoryRoot $repoRoot -ScriptPath $PSCommandPath - if ($finalScriptProvenance.Blob -cne $scriptProvenance.Blob) { - throw 'Official-auth canary script provenance changed during execution.' - } - $finalCodexHelperProvenance = Get-ScriptProvenance -RepositoryRoot $repoRoot -ScriptPath $artifactPaths.codexToolDenial - if ($finalCodexHelperProvenance.Blob -cne $codexHelperProvenance.Blob) { - throw 'Codex tool-denial helper provenance changed during execution.' - } - foreach ($entry in $artifactPaths.GetEnumerator()) { - $finalArtifactHash = Get-RegularFileHash -Path $entry.Value -Label $entry.Key - if ($finalArtifactHash -cne [string]$artifactHashes[$entry.Key]) { - throw "$($entry.Key) changed during official-auth canaries." - } - } - - Remove-OwnedDirectory -Path $tempRoot -RequiredParent $tempParent - $tempOwned = $false - if (Test-Path -LiteralPath $tempRoot) { throw 'Official-auth temporary root survived cleanup.' } - - $null = [IO.Directory]::CreateDirectory($evidenceRoot) - Assert-NoExistingReparsePoint -Path $evidenceRoot -FailureMessage 'Evidence root is a reparse point' - $null = [IO.Directory]::CreateDirectory($receiptLayout.Staging) - $stagingOwned = $true - $receipt = [ordered]@{ - schemaVersion = 1 - kind = 'windows-official-auth-canaries' - pass = $true - source = [ordered]@{ - expectedHead = $ExpectedHead - observedHead = $finalGit.Head - tree = $finalGit.Tree - trackedCleanBefore = $initialGit.TrackedClean - trackedCleanAfter = $finalGit.TrackedClean - unchangedDuringRun = $finalGit.Head -ceq $initialGit.Head -and $finalGit.Tree -ceq $initialGit.Tree - scriptSha256 = $scriptSha256 - scriptBlob = $scriptProvenance.Blob - codexHelperSha256 = $artifactHashes.codexToolDenial - codexHelperBlob = $codexHelperProvenance.Blob - } - execution = [ordered]@{ - serialLanes = $true - modelCalls = 3 - authStatusCalls = 3 - freshTemporaryWorkspace = $true - distinctIsolatedMindWorkspaces = $true - captureInspectionReadOnly = $true - rawStdoutPersisted = $false - rawStderrPersisted = $false - rawUsagePersisted = $false - } - credentials = [ordered]@{ - mode = 'user-auth-in-place' - authFilesReadByHarness = 0 - authFilesCopied = 0 - authContentsSerialized = $false - profileEnvironmentOverrides = 0 - profileEnvironmentPreserved = $true - ambientApiTokenEnvironmentNamesBlankedByPattern = $true - claudeAlternativeEnvironmentNamesBlanked = @($claudeAlternativeAuthNames) - codexAlternativeEnvironmentNamesBlanked = @($codexAlternativeAuthNames) - hermesAlternativeEnvironmentNamesBlanked = @($hermesAlternativeAuthNames) - } - clients = [ordered]@{ - claude = [ordered]@{ - version = $claudeVersion - executableSha256 = Get-RegularFileHash -Path $claudeExe -Label 'Claude executable' - auth = [ordered]@{ - loggedIn = $true - authMethod = [string]$claudeAuthJson.authMethod - apiProvider = [string]$claudeAuthJson.apiProvider - subscriptionType = [string]$claudeAuthJson.subscriptionType - outputBytes = $claudeAuthRaw.StdoutBytes - outputSha256 = $claudeAuthRaw.StdoutSha256 - } - model = $ClaudeModel - maxBudgetUsd = $ClaudeMaxUsd - reportedCostUsd = $claudeCost - exitCode = $claudeRaw.ExitCode - durationMs = $claudeRaw.DurationMs - markerSha256 = Get-Sha256Text $claudeMarker - markerMatched = $true - numTurns = [int]$claudeResult.num_turns - sessionIdSha256 = Get-Sha256Text $claudeSessionId - hookEvents = $claudeHookEvents - capture = [ordered]@{ - markerFrames = $claudeCapture.MarkerFrames - promptFrames = $claudeCapture.PromptFrames - responseFrames = $claudeCapture.ResponseFrames - promptContentSha256 = $claudeCapture.PromptContentSha256 - responseContentSha256 = $claudeCapture.ResponseContentSha256 - mindSha256 = $claudeCapture.MindSha256 - } - stdoutBytes = $claudeRaw.StdoutBytes - stdoutSha256 = $claudeRaw.StdoutSha256 - stderrBytes = $claudeRaw.StderrBytes - stderrSha256 = $claudeRaw.StderrSha256 - } - codex = [ordered]@{ - version = $codexVersion - executableSha256 = $codexExecutableSha256 - auth = [ordered]@{ - loggedIn = $true - method = 'chatgpt' - stdoutBytes = $codexAuthRaw.StdoutBytes - stdoutSha256 = $codexAuthRaw.StdoutSha256 - stderrBytes = $codexAuthRaw.StderrBytes - stderrSha256 = $codexAuthRaw.StderrSha256 - } - model = $CodexModel - exitCode = $codexRaw.ExitCode - durationMs = $codexRaw.DurationMs - markerSha256 = Get-Sha256Text $codexMarker - markerMatched = $true - sessionIdSha256 = Get-Sha256Text $codexSessionId - containment = [ordered]@{ - oneShot = $true - timeoutSeconds = 300 - exactMarkerRequired = $true - harnessProcessInvocationsRequired = 1 - paidModelCallsRequired = 1 - usageReceiptRequired = $false - preCallCostCapAvailable = $false - emptyToolSchemaRequired = $true - wildcardPreToolUseBackstopRequired = $true - threadEnvironmentsEmpty = $true - turnEnvironmentsEmpty = $true - dynamicToolsEmpty = $true - selectedCapabilityRootsEmpty = $true - } - toolDenialProof = [ordered]@{ - schemaVersion = [int]$codexDenialProof.schemaVersion - proofPaidCalls = [int]$codexDenialProof.proof.paidCalls - reportSha256 = $codexReportSha256 - executableSha256 = [string]$codexDenialProof.executable.sha256 - modelCatalogSha256 = [string]$codexDenialProof.paidInvocation.modelCatalogSha256 - invocationArgumentsSha256 = [string]$codexDenialProof.invocation.argumentsSha256 - configSha256 = [string]$codexDenialProof.invocation.configSha256 - threadParamsSha256 = [string]$codexDenialProof.invocation.threadParamsSha256 - turnParamsSha256 = [string]$codexDenialProof.invocation.turnParamsSha256 - paidThreadParamsSha256 = [string]$codexDenialProof.paidInvocation.threadParamsSha256 - paidTurnParamsSha256 = [string]$codexDenialProof.paidInvocation.turnParamsSha256 - mcpServerCount = [int]$codexDenialProof.invocation.mcpServerCount - mcpServerNamesSha256 = [string]$codexDenialProof.invocation.mcpServerNamesSha256 - mcpBoundarySha256 = [string]$codexDenialProof.mcpBoundary.postPaidSha256 - mcpConfigSha256 = [string]$codexDenialProof.mcpBoundary.postPaidConfigSha256 - hookGraphSha256 = [string]$codexDenialProof.hooks.graphSha256 - denyHookSha256 = [string]$codexDenialProof.hooks.denyHookSha256 - packagedHookArtifactsSha256 = [string]$codexDenialProof.hooks.artifactsSha256 - hiveMindCliSha256 = [string]$codexDenialProof.hooks.cliSha256 - windowsPowerShellSha256 = [string]$codexDenialProof.artifacts.windowsPowerShellSha256 - windowsSystemDirectorySha256 = [string]$codexDenialProof.artifacts.windowsSystemDirectorySha256 - exactHookCount = [int]$codexDenialProof.hooks.expectedCount - extraHookCount = [int]$codexDenialProof.hooks.extraCount - allHooksTrusted = [bool]$codexDenialProof.hooks.allTrusted - hookGraphUnchanged = [bool]$codexDenialProof.hooks.unchangedAfterPin - packagedArtifactsUnchanged = [bool]$codexDenialProof.hooks.artifactsUnchanged - helperScriptSha256 = [string]$codexDenialProof.artifacts.scriptSha256 - helperScriptBlob = [string]$codexDenialProof.source.scriptBlob - helperScriptUnchanged = [bool]$codexDenialProof.artifacts.scriptUnchanged - emptySchema = [bool]$codexDenialProof.sealed.emptySchema - guardedToolDeniedBeforeExecution = [bool]$codexDenialProof.green.deniedBeforeRead - sensitiveDataObservedAfterDenial = [bool]$codexDenialProof.green.sensitiveDataObserved - normalTextSucceeded = [bool]$codexDenialProof.green.normalTextCompleted - toolEventsObserved = [int]$codexDenialProof.paidInvocation.toolEventsObserved - unknownEventsObserved = [int]$codexDenialProof.paidInvocation.unknownEventsObserved - } - capture = [ordered]@{ - markerFrames = $codexCapture.MarkerFrames - promptFrames = $codexCapture.PromptFrames - responseFrames = $codexCapture.ResponseFrames - promptContentSha256 = $codexCapture.PromptContentSha256 - responseContentSha256 = $codexCapture.ResponseContentSha256 - mindSha256 = $codexCapture.MindSha256 - } - stdoutBytes = $codexRaw.StdoutBytes - stdoutSha256 = $codexRaw.StdoutSha256 - stderrBytes = $codexRaw.StderrBytes - stderrSha256 = $codexRaw.StderrSha256 - } - hermes = [ordered]@{ - version = $hermesVersion - executableSha256 = Get-RegularFileHash -Path $hermesExe -Label 'Hermes executable' - auth = [ordered]@{ - provider = $HermesProvider - loggedIn = $true - outputBytes = $hermesAuthRaw.StdoutBytes - outputSha256 = $hermesAuthRaw.StdoutSha256 - } - provider = $HermesProvider - model = $HermesModel - exitCode = $hermesRaw.ExitCode - durationMs = $hermesRaw.DurationMs - markerSha256 = Get-Sha256Text $hermesMarker - markerMatched = $true - sessionIdSha256 = Get-Sha256Text $hermesSessionId - containment = [ordered]@{ - oneShot = $true - timeoutSeconds = 180 - exactMarkerRequired = $true - apiCallsRequired = 1 - usageReceiptRequired = $true - preCallCostCapAvailable = $false - } - capture = [ordered]@{ - markerFrames = $hermesCapture.MarkerFrames - promptFrames = $hermesCapture.PromptFrames - responseFrames = $hermesCapture.ResponseFrames - promptContentSha256 = $hermesCapture.PromptContentSha256 - responseContentSha256 = $hermesCapture.ResponseContentSha256 - mindSha256 = $hermesCapture.MindSha256 - } - usage = [ordered]@{ - apiCalls = [int]$hermesUsage.api_calls - inputTokens = $hermesUsage.input_tokens - outputTokens = $hermesUsage.output_tokens - reasoningTokens = $hermesUsage.reasoning_tokens - totalTokens = $hermesUsage.total_tokens - estimatedCostUsd = $hermesUsage.estimated_cost_usd - costStatus = $hermesUsage.cost_status - costSource = $hermesUsage.cost_source - serviceTier = $hermesUsage.service_tier - rawBytes = $hermesUsageBytes.Length - rawSha256 = $hermesUsageSha256 - } - stdoutBytes = $hermesRaw.StdoutBytes - stdoutSha256 = $hermesRaw.StdoutSha256 - stderrBytes = $hermesRaw.StderrBytes - stderrSha256 = $hermesRaw.StderrSha256 - } - } - hooks = [ordered]@{ - claude = [ordered]@{ - passedChecks = $claudeHookPasses - minimumChecks = 11 - stdoutSha256 = $claudeHookVerify.StdoutSha256 - stderrSha256 = $claudeHookVerify.StderrSha256 - } - codex = [ordered]@{ - passedChecks = $codexHookPasses - minimumChecks = 9 - stdoutSha256 = $codexHookVerify.StdoutSha256 - stderrSha256 = $codexHookVerify.StderrSha256 - } - hermes = [ordered]@{ - passedChecks = $hermesHookPasses - minimumChecks = 8 - stdoutSha256 = $hermesHookVerify.StdoutSha256 - stderrSha256 = $hermesHookVerify.StderrSha256 - } - artifacts = $artifactHashes - } - cleanup = [ordered]@{ - temporaryRootRemoved = $true - hermesRawUsageRemoved = $hermesUsageRemoved - rawOutputsPersisted = $false - } - } - $receiptPath = Join-Path $receiptLayout.Staging 'official-auth-receipt.json' - Write-JsonCreateNew -Path $receiptPath -Value $receipt - $receiptText = [IO.File]::ReadAllText($receiptPath) - if ($receiptText -match '(?i)(?:[A-Z]:[\\/]|file:///|\\\\[^\\/\s]+[\\/])') { - throw 'Whitelisted receipt unexpectedly contained an absolute path.' - } - if (Test-Path -LiteralPath $receiptLayout.Root) { throw 'Receipt destination became occupied.' } - [IO.Directory]::Move($receiptLayout.Staging, $receiptLayout.Root) - $stagingOwned = $false - $published = $true - Write-Host "Official-auth canaries passed. Receipt: $($receiptLayout.Root)" -} finally { - if ($tempOwned -and (Test-Path -LiteralPath $tempRoot)) { - Remove-OwnedDirectory -Path $tempRoot -RequiredParent $tempParent - } - if ($stagingOwned -and (Test-Path -LiteralPath $receiptLayout.Staging)) { - Remove-OwnedDirectory -Path $receiptLayout.Staging -RequiredParent $evidenceRoot - } - if (-not $published -and (Test-Path -LiteralPath $receiptLayout.Root)) { - throw 'A failed run must not leave a final receipt directory.' - } -} diff --git a/scripts/verify-codex-tool-denial.mjs b/scripts/verify-codex-tool-denial.mjs deleted file mode 100644 index 52ed827..0000000 --- a/scripts/verify-codex-tool-denial.mjs +++ /dev/null @@ -1,2882 +0,0 @@ -#!/usr/bin/env node - -import { spawn, spawnSync } from 'node:child_process'; -import { createHash, randomBytes } from 'node:crypto'; -import { createServer } from 'node:http'; -import { - existsSync, - lstatSync, - readFileSync, - realpathSync, -} from 'node:fs'; -import { - mkdir, - rm, - stat, - writeFile, -} from 'node:fs/promises'; -import { homedir, tmpdir } from 'node:os'; -import { - basename, - dirname, - isAbsolute, - join, - relative, - resolve, - win32, -} from 'node:path'; -import { fileURLToPath } from 'node:url'; - -const SCRIPT_PATH = fileURLToPath(import.meta.url); -const MAX_CAPTURE_BYTES = 2 * 1024 * 1024; -const MAX_REQUEST_BYTES = 2 * 1024 * 1024; -const TURN_TIMEOUT_MS = 180_000; -const PAID_ACK = 'I_ACKNOWLEDGE_1_CODEX_OFFICIAL_AUTH_CALL'; -const DENIAL_REASON = 'Waggle official-auth canary denies every model tool call.'; -const WAGGLE_HOOK_EVENTS = [ - 'preCompact', - 'sessionStart', - 'stop', - 'userPromptSubmit', -]; -const DISABLED_FEATURES = [ - 'shell_tool', - 'unified_exec', - 'apps', - 'browser_use', - 'browser_use_external', - 'browser_use_full_cdp_access', - 'computer_use', - 'image_generation', - 'in_app_browser', - 'multi_agent', - 'multi_agent_v2', - 'goals', - 'skill_search', - 'tool_suggest', - 'workspace_dependencies', - 'skill_mcp_dependency_install', - 'plugins', - 'plugin_sharing', - 'remote_plugin', - 'mentions_v2', -]; -const CONFIG_OVERRIDES = [ - 'approval_policy="never"', - 'sandbox_mode="read-only"', - 'web_search="disabled"', - 'orchestrator.skills.enabled=false', - 'orchestrator.mcp.enabled=false', - 'tools.update_plan.enabled=false', - 'tools.experimental_request_user_input.enabled=false', - 'project_doc_max_bytes=0', - 'history.persistence="none"', - 'analytics.enabled=false', -]; -const DENIAL_RESPONSE = { - hookSpecificOutput: { - hookEventName: 'PreToolUse', - permissionDecision: 'deny', - permissionDecisionReason: DENIAL_REASON, - }, -}; -const CANARY_STAGES = new Set([ - 'app-server-spawn', - 'initialize', - 'hooks-list', - 'pre-turn-boundary', - 'thread-start', - 'turn-start', - 'turn-completed', - 'event-audit', - 'app-server-close', - 'post-turn-invariants', -]); -const EVENT_AUDIT_FAILURE_INVARIANTS = new Set([ - 'failure-notification', - 'wrong-scope', - 'model-reroute', - 'unknown-notification-method', - 'unknown-item-type', - 'forbidden-tool-item', - 'agent-message-cardinality', -]); -const EVENT_AUDIT_FAILURE_KEYS = [ - 'invariant', - 'notificationCount', - 'failureEventsObserved', - 'wrongScopeEventsObserved', - 'rerouteEventsObserved', - 'unknownNotificationEventsObserved', - 'unknownItemEventsObserved', - 'toolEventsObserved', - 'notificationGraphSha256', - 'rejectedMethodSha256', - 'rejectedItemTypeSha256', -]; -const FAIL_CLOSED_PROJECT_TRUST_WARNING = - 'Project-local config, hooks, and exec policies are disabled in the following folders until the project is trusted, but skills still load.'; -const FAILURE_NOTIFICATION_METHODS = new Set([ - 'error', - 'warning', - 'guardianWarning', - 'configWarning', - 'deprecationNotice', -]); -const ALLOWED_NOTIFICATION_METHODS = new Set([ - 'account/updated', - 'account/rateLimits/updated', - 'hook/completed', - 'hook/started', - 'item/agentMessage/delta', - 'item/completed', - 'item/plan/delta', - 'item/reasoning/summaryPartAdded', - 'item/reasoning/summaryTextDelta', - 'item/reasoning/textDelta', - 'item/started', - 'item/updated', - 'mcpServer/startupStatus/updated', - 'model/safetyBuffering/updated', - 'model/verification', - 'remoteControl/status/changed', - 'serverRequest/resolved', - 'thread/name/updated', - 'thread/settings/updated', - 'thread/started', - 'thread/status/changed', - 'thread/tokenUsage/updated', - 'turn/completed', - 'turn/diff/updated', - 'turn/moderationMetadata', - 'turn/plan/updated', - 'turn/started', -]); -const ALLOWED_ITEM_TYPES = new Set([ - 'agentMessage', - 'reasoning', - 'userMessage', -]); -const TERMINAL_TURN_FAILURE_STATUSES = new Set(['failed', 'interrupted']); -const SIMPLE_CODEX_ERROR_CODES = new Set([ - 'badRequest', - 'contextWindowExceeded', - 'cyberPolicy', - 'internalServerError', - 'other', - 'sandboxError', - 'serverOverloaded', - 'sessionBudgetExceeded', - 'threadRollbackFailed', - 'unauthorized', - 'usageLimitExceeded', -]); -const HTTP_CODEX_ERROR_CODES = new Set([ - 'httpConnectionFailed', - 'responseStreamConnectionFailed', - 'responseStreamDisconnected', - 'responseTooManyFailedAttempts', -]); - -function assert(condition, message) { - if (!condition) throw new Error(message); -} - -function sha256(value) { - return createHash('sha256').update(value).digest('hex'); -} - -function sha256File(path) { - return sha256(readFileSync(path)); -} - -function canonicalPath(path) { - return realpathSync.native(path).toLowerCase(); -} - -function stableValue(value) { - if (Array.isArray(value)) return value.map(stableValue); - if (value && typeof value === 'object') { - return Object.fromEntries( - Object.entries(value) - .sort(([left], [right]) => left.localeCompare(right)) - .map(([key, child]) => [key, stableValue(child)]), - ); - } - return value; -} - -function stableJson(value) { - return JSON.stringify(stableValue(value)); -} - -function normalizeCodexErrorInfo(value) { - if (value === null || value === undefined) { - return { code: null, httpStatusCode: null }; - } - if (typeof value === 'string') { - return SIMPLE_CODEX_ERROR_CODES.has(value) - ? { code: value, httpStatusCode: null } - : { code: null, httpStatusCode: null }; - } - if (typeof value !== 'object' || Array.isArray(value)) { - return { code: null, httpStatusCode: null }; - } - const keys = Object.keys(value); - if (keys.length !== 1) return { code: null, httpStatusCode: null }; - const code = keys[0]; - const details = value[code]; - if (code === 'activeTurnNotSteerable') { - return details !== null - && typeof details === 'object' - && !Array.isArray(details) - && Object.keys(details).length === 1 - && ['review', 'compact'].includes(details.turnKind) - ? { code, httpStatusCode: null } - : { code: null, httpStatusCode: null }; - } - if (!HTTP_CODEX_ERROR_CODES.has(code) - || details === null - || typeof details !== 'object' - || Array.isArray(details) - || !Object.keys(details).every((key) => key === 'httpStatusCode')) { - return { code: null, httpStatusCode: null }; - } - const httpStatusCode = details.httpStatusCode ?? null; - if (httpStatusCode !== null - && (!Number.isInteger(httpStatusCode) || httpStatusCode < 0 || httpStatusCode > 65_535)) { - return { code: null, httpStatusCode: null }; - } - return { code, httpStatusCode }; -} - -function sanitizeTurnFailure(turn, errorNotification = null) { - const status = TERMINAL_TURN_FAILURE_STATUSES.has(turn?.status) ? turn.status : null; - const notificationParams = errorNotification?.message?.params ?? null; - const turnInfo = normalizeCodexErrorInfo(turn?.error?.codexErrorInfo); - const notificationInfo = errorNotification?.message?.method === 'error' - ? normalizeCodexErrorInfo(notificationParams?.error?.codexErrorInfo) - : { code: null, httpStatusCode: null }; - // The final turn is authoritative. A disagreeing or malformed notification is - // reduced to the already-sanitized turn result rather than becoming a new error. - const selected = turnInfo.code === null ? notificationInfo : turnInfo; - const willRetry = notificationParams?.willRetry ?? null; - return { - status, - code: selected.code, - httpStatusCode: selected.httpStatusCode, - willRetry: typeof willRetry === 'boolean' ? willRetry : null, - }; -} - -function sanitizeProtocolCode(value) { - return Number.isInteger(value) - && value >= -2_147_483_648 - && value <= 2_147_483_647 - ? value - : null; -} - -function sanitizeCanaryStage(value) { - return CANARY_STAGES.has(value) ? value : null; -} - -function groupSha256(values) { - const counts = new Map(); - for (const value of values) { - if (typeof value !== 'string') continue; - const digest = sha256(value); - counts.set(digest, (counts.get(digest) ?? 0) + 1); - } - return [...counts.entries()] - .map(([digest, count]) => ({ sha256: digest, count })) - .sort((left, right) => left.sha256.localeCompare(right.sha256)); -} - -function sanitizeSha256Counts(value) { - if (!Array.isArray(value) || value.length > 256) return null; - let previous = null; - const sanitized = []; - for (const entry of value) { - if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) return null; - if (Object.keys(entry).sort().join(',') !== 'count,sha256') return null; - if (!/^[0-9a-f]{64}$/.test(entry.sha256)) return null; - if (!Number.isInteger(entry.count) || entry.count < 1 || entry.count > 2_147_483_647) return null; - if (previous !== null && previous >= entry.sha256) return null; - previous = entry.sha256; - sanitized.push({ sha256: entry.sha256, count: entry.count }); - } - return sanitized; -} - -function sanitizeEventAuditFailure(value) { - if (value === null || typeof value !== 'object' || Array.isArray(value)) return null; - if (Object.keys(value).sort().join(',') !== [...EVENT_AUDIT_FAILURE_KEYS].sort().join(',')) return null; - if (!EVENT_AUDIT_FAILURE_INVARIANTS.has(value.invariant)) return null; - const countKeys = [ - 'notificationCount', - 'failureEventsObserved', - 'wrongScopeEventsObserved', - 'rerouteEventsObserved', - 'unknownNotificationEventsObserved', - 'unknownItemEventsObserved', - 'toolEventsObserved', - ]; - for (const key of countKeys) { - if (!Number.isInteger(value[key]) || value[key] < 0 || value[key] > 2_147_483_647) return null; - } - if (!/^[0-9a-f]{64}$/.test(value.notificationGraphSha256)) return null; - const rejectedMethodSha256 = sanitizeSha256Counts(value.rejectedMethodSha256); - const rejectedItemTypeSha256 = sanitizeSha256Counts(value.rejectedItemTypeSha256); - if (rejectedMethodSha256 === null || rejectedItemTypeSha256 === null) return null; - return { - invariant: value.invariant, - notificationCount: value.notificationCount, - failureEventsObserved: value.failureEventsObserved, - wrongScopeEventsObserved: value.wrongScopeEventsObserved, - rerouteEventsObserved: value.rerouteEventsObserved, - unknownNotificationEventsObserved: value.unknownNotificationEventsObserved, - unknownItemEventsObserved: value.unknownItemEventsObserved, - toolEventsObserved: value.toolEventsObserved, - notificationGraphSha256: value.notificationGraphSha256, - rejectedMethodSha256, - rejectedItemTypeSha256, - }; -} - -function selectLifecycleFailure(primaryError, closeError) { - return primaryError ?? closeError ?? null; -} - -function proofStateSatisfied(proof, setupOnly) { - if (proof === null || typeof proof !== 'object' || Array.isArray(proof)) return false; - return setupOnly - ? proof.executed === false && proof.paidCalls === 0 && proof.pass === null - : proof.executed === true && proof.paidCalls === 0 && proof.pass === true; -} - -function parseArgs(argv) { - const values = {}; - const booleanFlags = new Set(['--execute-paid', '--self-test', '--setup-only']); - const valueFlags = new Set([ - '--ack', - '--codex-exe', - '--expected-head', - '--hive-mind-cli', - '--marker', - '--model', - '--receipt-dir', - '--windows-powershell', - '--workspace', - ]); - for (let index = 0; index < argv.length; index += 1) { - const flag = argv[index]; - if (booleanFlags.has(flag)) { - assert(values[flag.slice(2)] === undefined, `duplicate argument: ${flag}`); - values[flag.slice(2)] = true; - continue; - } - assert(valueFlags.has(flag), `unsupported argument: ${flag}`); - assert(values[flag.slice(2)] === undefined, `duplicate argument: ${flag}`); - const value = argv[index + 1]; - assert(value && !value.startsWith('--'), `missing value for ${flag}`); - values[flag.slice(2)] = value; - index += 1; - } - if (values['self-test']) { - assert(Object.keys(values).length === 1, '--self-test cannot be combined with other arguments'); - return values; - } - for (const name of [ - 'codex-exe', - 'expected-head', - 'hive-mind-cli', - 'model', - 'receipt-dir', - 'windows-powershell', - 'workspace', - ]) { - assert(typeof values[name] === 'string', `--${name} is required`); - } - assert(/^[0-9a-f]{40}$/.test(values['expected-head']), '--expected-head must be a lowercase 40-character Git object id'); - assert(values.model.length <= 128 && !/[\x00-\x1f\x7f]/.test(values.model), '--model is invalid'); - assert(!(values['execute-paid'] && values['setup-only']), '--setup-only and --execute-paid are mutually exclusive'); - if (values['execute-paid']) { - assert(typeof values.marker === 'string', '--marker is required with --execute-paid'); - assert(values.ack === PAID_ACK, `--ack must be exactly ${PAID_ACK}`); - assert(values.marker.length <= 256 && !/[\x00-\x1f\x7f]/.test(values.marker), '--marker is invalid'); - } else { - assert(values.marker === undefined && values.ack === undefined, '--marker/--ack require --execute-paid'); - } - return values; -} - -function requireRegularFile(path, label) { - assert(existsSync(path), `${label} is missing`); - const posture = lstatSync(path); - assert(posture.isFile() && !posture.isSymbolicLink(), `${label} must be a regular file`); -} - -function requireDirectory(path, label) { - assert(existsSync(path), `${label} is missing`); - const posture = lstatSync(path); - assert(posture.isDirectory() && !posture.isSymbolicLink(), `${label} must be a regular directory`); -} - -function assertNoExistingReparsePoint(path) { - let cursor = resolve(path); - while (cursor) { - if (existsSync(cursor)) { - const posture = lstatSync(cursor); - assert(!posture.isSymbolicLink(), 'receipt ancestor must not be a symbolic link'); - } - const parent = dirname(cursor); - if (parent === cursor) break; - cursor = parent; - } -} - -function runChecked(command, args, options = {}) { - const result = spawnSync(command, args, { - cwd: options.cwd, - env: options.env ?? process.env, - encoding: 'utf8', - timeout: options.timeoutMs ?? 30_000, - maxBuffer: MAX_CAPTURE_BYTES, - windowsHide: true, - shell: false, - }); - if (result.status !== 0 || result.signal !== null || result.error) { - const code = typeof result.error?.code === 'string' ? result.error.code : null; - throw new Error(`${options.label ?? basename(command)} failed (status=${String(result.status)}, code=${String(code)})`); - } - return { - stdout: String(result.stdout ?? ''), - stderr: String(result.stderr ?? ''), - }; -} - -function verifySourceSnapshot(expectedHead) { - const repositoryRoot = resolve(dirname(SCRIPT_PATH), '..'); - const relativePath = relative(repositoryRoot, SCRIPT_PATH).replaceAll('\\', '/'); - assert(relativePath.length > 0 && !relativePath.startsWith('../') && !isAbsolute(relativePath), 'helper must be inside the repository'); - const git = (args, label) => runChecked('git.exe', ['-C', repositoryRoot, ...args], { - label, - timeoutMs: 30_000, - }).stdout.trim(); - const observedHead = git(['rev-parse', 'HEAD'], 'repository HEAD'); - const tree = git(['rev-parse', 'HEAD^{tree}'], 'repository tree'); - const trackedStatus = git(['status', '--porcelain=v1', '--untracked-files=no'], 'tracked repository status'); - assert(observedHead === expectedHead, `expected HEAD ${expectedHead} but observed ${observedHead}`); - assert(trackedStatus.length === 0, 'tracked repository state must be clean'); - const trackedPath = git(['ls-files', '--error-unmatch', '--', relativePath], 'helper tracked-file lookup'); - assert(trackedPath === relativePath, 'helper must be tracked at expected HEAD'); - const scriptBlob = git(['rev-parse', `${expectedHead}:${relativePath}`], 'helper expected blob'); - const workingBlob = git(['hash-object', `--path=${relativePath}`, SCRIPT_PATH], 'helper working blob'); - assert(/^[0-9a-f]{40}$/.test(tree), 'repository tree object id is invalid'); - assert(/^[0-9a-f]{40}$/.test(scriptBlob), 'helper expected blob object id is invalid'); - assert(workingBlob === scriptBlob, 'helper working content differs from expected HEAD'); - return { - expectedHead, - observedHead, - tree, - scriptBlob, - trackedClean: true, - }; -} - -function appendBounded(chunks, chunk) { - const used = chunks.reduce((sum, item) => sum + item.length, 0); - if (used >= MAX_CAPTURE_BYTES) return; - chunks.push(Buffer.from(chunk).subarray(0, MAX_CAPTURE_BYTES - used)); -} - -function safeError(error, replacements = []) { - let value = error instanceof Error ? error.message : String(error); - for (const replacement of replacements) { - if (replacement) value = value.replaceAll(replacement, ''); - } - return value - .replace(/\b(?:sk|sess)-[A-Za-z0-9_-]{12,}\b/g, '') - .slice(0, 500); -} - -function waitForValue(read, timeoutMs, label) { - const deadline = Date.now() + timeoutMs; - return new Promise((resolveWait, rejectWait) => { - const poll = () => { - const value = read(); - if (value !== undefined) { - resolveWait(value); - return; - } - if (Date.now() >= deadline) { - rejectWait(new Error(`${label} timed out`)); - return; - } - setTimeout(poll, 20); - }; - poll(); - }); -} - -function tomlString(value) { - return JSON.stringify(value); -} - -function powershellLiteral(value) { - return `'${value.replaceAll("'", "''")}'`; -} - -function verifyWindowsPowerShell(powerShell) { - requireRegularFile(powerShell, 'Windows PowerShell'); - const systemDirectory = runChecked(powerShell, [ - '-NoLogo', - '-NoProfile', - '-NonInteractive', - '-Command', - '[Console]::Out.Write([Environment]::SystemDirectory)', - ], { - label: 'Windows system-directory probe', - timeoutMs: 20_000, - }).stdout.trim(); - assert(isAbsolute(systemDirectory), 'Windows system-directory probe returned a non-absolute path'); - assert(basename(systemDirectory).toLowerCase() === 'system32', 'Windows system-directory probe did not return System32'); - const probed = join(systemDirectory, 'WindowsPowerShell', 'v1.0', 'powershell.exe'); - const packageCommandPath = join( - dirname(systemDirectory), - 'System32', - 'WindowsPowerShell', - 'v1.0', - 'powershell.exe', - ); - requireRegularFile(probed, 'OS Windows PowerShell'); - requireRegularFile(packageCommandPath, 'package-command Windows PowerShell'); - assert(canonicalPath(powerShell) === canonicalPath(probed), 'Windows PowerShell is not under the OS system directory'); - assert(canonicalPath(packageCommandPath) === canonicalPath(probed), 'package-command Windows PowerShell is not the probed OS binary'); - return { - path: packageCommandPath, - sha256: sha256File(packageCommandPath), - systemDirectorySha256: sha256(canonicalPath(systemDirectory)), - }; -} - -function buildExpectedWaggleHookCommand(powerShell, scriptPath, cliPath) { - const script = [ - "$ErrorActionPreference = 'Stop'", - 'try {', - ` & ${[ - process.execPath, - scriptPath, - '--cli-path', - cliPath, - ].map(powershellLiteral).join(' ')}`, - ' if ($null -eq $LASTEXITCODE) { exit 1 }', - ' exit $LASTEXITCODE', - '} catch {', - ' [Console]::Error.WriteLine($_.Exception.Message)', - ' exit 1', - '}', - ].join('\r\n'); - const encoded = Buffer.from(script, 'utf16le').toString('base64'); - return `${powerShell} -NoLogo -NoProfile -NonInteractive -EncodedCommand ${encoded}`; -} - -function buildExpectedWaggleHooks(powerShell, cliPath) { - const repoRoot = resolve(dirname(SCRIPT_PATH), '..'); - const packageRoot = join(repoRoot, 'packages', 'hive-mind-hooks-codex', 'dist', 'hooks'); - const scripts = new Map([ - ['preCompact', join(packageRoot, 'pre-compact.js')], - ['sessionStart', join(packageRoot, 'session-start.js')], - ['stop', join(packageRoot, 'stop.js')], - ['userPromptSubmit', join(packageRoot, 'user-prompt-submit.js')], - ]); - const commands = new Map(); - const artifacts = []; - requireRegularFile(cliPath, 'hive-mind CLI'); - artifacts.push({ kind: 'hive-mind-cli', sha256: sha256File(cliPath) }); - for (const [eventName, scriptPath] of scripts) { - requireRegularFile(scriptPath, `${eventName} packaged hook`); - const command = buildExpectedWaggleHookCommand(powerShell, scriptPath, cliPath); - commands.set(eventName, sha256(command)); - artifacts.push({ eventName, sha256: sha256File(scriptPath), commandSha256: sha256(command) }); - } - return { - commands, - artifactsSha256: sha256(stableJson(artifacts)), - cliSha256: sha256File(cliPath), - }; -} - -function buildDenyHookConfig(powerShell) { - const payload = JSON.stringify(DENIAL_RESPONSE); - const encoded = Buffer.from(`[Console]::Out.Write('${payload.replaceAll("'", "''")}')`, 'utf16le') - .toString('base64'); - const command = `${powerShell} -NoLogo -NoProfile -NonInteractive -EncodedCommand ${encoded}`; - assert(!command.includes("'"), 'deny hook command cannot contain a TOML literal quote'); - return { - command, - config: `hooks.PreToolUse=[{matcher='*',hooks=[{type='command',command='false',command_windows='${command}',timeout=5}]}]`, - commandSha256: sha256(command), - }; -} - -function buildChildEnvironment(tempRoot) { - const environment = { ...process.env }; - for (const name of [ - 'ANTHROPIC_API_KEY', - 'CODEX_ACCESS_TOKEN', - 'OPENAI_ACCESS_TOKEN', - 'OPENAI_API_BASE', - 'OPENAI_API_KEY', - 'OPENAI_BASE_URL', - 'OPENROUTER_API_KEY', - ]) { - environment[name] = ''; - } - environment.NO_COLOR = '1'; - environment.HIVE_MIND_DATA_DIR = join(tempRoot, 'mind'); - environment.HIVE_MIND_EMBEDDING_PROVIDER = 'mock'; - environment.HIVE_MIND_NO_SYNTH = '1'; - environment.HIVE_MIND_SCOPES = 'memory:read,memory:write'; - environment.HIVE_MIND_SHIM_LOG_LEVEL = 'error'; - environment.WAGGLE_SIGNAL_EMIT = '0'; - environment.WAGGLE_WORKSPACE_ID = `codex-denial-${randomBytes(12).toString('hex')}`; - environment.TEMP = join(tempRoot, 'process-temp'); - environment.TMP = environment.TEMP; - return environment; -} - -function mutateModel(base, sealed) { - const model = structuredClone(base); - model.apply_patch_tool_type = null; - model.input_modalities = sealed ? ['text'] : ['text', 'image']; - model.supports_image_detail_original = !sealed; - model.supports_search_tool = false; - model.experimental_supported_tools = []; - model.use_responses_lite = false; - return model; -} - -function readBundledModel(codexExecutable, model) { - const raw = runChecked(codexExecutable, ['debug', 'models', '--bundled'], { - label: 'Codex bundled-model catalog', - timeoutMs: 30_000, - }).stdout; - let catalog; - try { - catalog = JSON.parse(raw); - } catch { - throw new Error('Codex bundled-model catalog was not valid JSON'); - } - assert(Array.isArray(catalog?.models), 'Codex bundled-model catalog omitted models'); - const selected = catalog.models.find((entry) => entry?.slug === model); - assert(selected && typeof selected === 'object', `Codex bundled catalog omitted model ${model}`); - return { selected, bundledSha256: sha256(raw) }; -} - -function writeCatalog(path, model) { - const value = `${JSON.stringify({ models: [model] })}\n`; - return writeFile(path, value, 'utf8').then(() => sha256(value)); -} - -function buildMcpInventoryArguments() { - const args = []; - for (const feature of DISABLED_FEATURES) args.push('--disable', feature); - return [...args, 'mcp', 'list', '--json']; -} - -function readConfiguredMcpServerNames(codexExecutable, env) { - const raw = runChecked(codexExecutable, buildMcpInventoryArguments(), { - env, - label: 'Codex MCP server inventory', - timeoutMs: 30_000, - }).stdout; - let value; - try { - value = JSON.parse(raw); - } catch { - throw new Error('Codex MCP server inventory was not valid JSON'); - } - assert(Array.isArray(value), 'Codex MCP server inventory was not an array'); - const names = value.map((entry) => entry?.name); - assert(names.every((name) => typeof name === 'string' && name.length > 0), 'Codex MCP server inventory contained an invalid name'); - assert(names.every((name) => !/[\u0000\r\n']/.test(name)), 'Codex MCP server name contains an unsupported character'); - assert(new Set(names).size === names.length, 'Codex MCP server inventory contained duplicate names'); - return names.sort((left, right) => left.localeCompare(right)); -} - -function captureMcpBoundary(codexExecutable, env, configPath) { - requireRegularFile(configPath, 'user Codex config.toml'); - const names = readConfiguredMcpServerNames(codexExecutable, env); - const namesSha256 = sha256(stableJson(names)); - const configSha256 = sha256File(configPath); - return { - count: names.length, - namesSha256, - configSha256, - boundarySha256: sha256(stableJson({ count: names.length, namesSha256, configSha256 })), - }; -} - -function assertSameMcpBoundary(expected, actual, label) { - assert( - actual.count === expected.count - && actual.namesSha256 === expected.namesSha256 - && actual.configSha256 === expected.configSha256 - && actual.boundarySha256 === expected.boundarySha256, - `Codex MCP/config boundary changed ${label}`, - ); -} - -function responseEvent(event) { - return `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`; -} - -function completedUsage() { - return { - input_tokens: 0, - input_tokens_details: null, - output_tokens: 0, - output_tokens_details: null, - total_tokens: 0, - }; -} - -function messageResponse(text) { - const responseId = `resp_${randomBytes(8).toString('hex')}`; - const itemId = `msg_${randomBytes(8).toString('hex')}`; - const item = { - type: 'message', - role: 'assistant', - id: itemId, - status: 'completed', - content: [{ type: 'output_text', text }], - }; - return [ - responseEvent({ type: 'response.created', response: { id: responseId, status: 'in_progress' } }), - responseEvent({ type: 'response.output_item.done', output_index: 0, item }), - responseEvent({ - type: 'response.completed', - response: { id: responseId, status: 'completed', output: [item], usage: completedUsage() }, - }), - ].join(''); -} - -function functionCallResponse(name, argumentsJson) { - const responseId = `resp_${randomBytes(8).toString('hex')}`; - const itemId = `fc_${randomBytes(8).toString('hex')}`; - const callId = `call_${randomBytes(8).toString('hex')}`; - const item = { - type: 'function_call', - id: itemId, - call_id: callId, - name, - arguments: argumentsJson, - status: 'completed', - }; - return [ - responseEvent({ type: 'response.created', response: { id: responseId, status: 'in_progress' } }), - responseEvent({ type: 'response.output_item.added', output_index: 0, item: { ...item, arguments: '', status: 'in_progress' } }), - responseEvent({ type: 'response.function_call_arguments.done', item_id: itemId, output_index: 0, arguments: argumentsJson }), - responseEvent({ type: 'response.output_item.done', output_index: 0, item }), - responseEvent({ - type: 'response.completed', - response: { id: responseId, status: 'completed', output: [item], usage: completedUsage() }, - }), - ].join(''); -} - -function toolNamesFromRequest(body) { - if (!Array.isArray(body?.tools)) return []; - return body.tools.map((tool) => tool?.name ?? tool?.function?.name ?? null); -} - -function additionalToolsFromRequest(body) { - const values = []; - const visit = (value, insideInput = false) => { - if (Array.isArray(value)) { - value.forEach((child) => visit(child, insideInput)); - return; - } - if (!value || typeof value !== 'object') return; - for (const [key, child] of Object.entries(value)) { - if (insideInput && key === 'additional_tools') { - values.push(...(Array.isArray(child) ? child : [child])); - } - visit(child, insideInput || key === 'input'); - } - }; - visit(body, false); - return values; -} - -function requestContainsImage(body) { - let found = false; - const visit = (value) => { - if (found) return; - if (typeof value === 'string') { - if (value.includes('data:image')) found = true; - return; - } - if (Array.isArray(value)) { - value.forEach(visit); - return; - } - if (!value || typeof value !== 'object') return; - if (value.type === 'input_image') { - found = true; - return; - } - Object.values(value).forEach(visit); - }; - visit(body); - return found; -} - -async function startLoopbackProvider(mode, expectedAcknowledgement, sentinelPath) { - const records = []; - const server = createServer((request, response) => { - const chunks = []; - let bytes = 0; - request.on('data', (chunk) => { - bytes += chunk.length; - if (bytes <= MAX_REQUEST_BYTES) chunks.push(Buffer.from(chunk)); - }); - request.on('end', () => { - const raw = Buffer.concat(chunks).toString('utf8'); - let body = null; - try { body = JSON.parse(raw); } catch { /* rejected below */ } - const record = { - index: records.length, - method: request.method ?? null, - endpointOk: request.url === '/v1/responses', - bytes, - sha256: sha256(raw), - topLevelToolsIsArray: Array.isArray(body?.tools), - toolNames: body ? toolNamesFromRequest(body) : [], - additionalToolCount: body ? additionalToolsFromRequest(body).length : -1, - containsImage: body ? requestContainsImage(body) : false, - }; - records.push(record); - if ( - request.method !== 'POST' - || !record.endpointOk - || body === null - || bytes > MAX_REQUEST_BYTES - ) { - response.writeHead(400, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ error: { message: 'Waggle loopback policy rejected request' } })); - return; - } - let payload; - if (mode === 'sealed') { - payload = records.length === 1 - ? messageResponse(expectedAcknowledgement) - : null; - } else if (records.length === 1) { - payload = functionCallResponse('view_image', JSON.stringify({ path: sentinelPath })); - } else if (records.length === 2) { - payload = messageResponse(expectedAcknowledgement); - } - if (payload === null || payload === undefined) { - response.writeHead(400, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ error: { message: 'Waggle loopback request count exceeded policy' } })); - return; - } - response.writeHead(200, { - 'cache-control': 'no-cache', - 'content-type': 'text/event-stream; charset=utf-8', - }); - response.end(payload); - }); - }); - await new Promise((resolveListen, rejectListen) => { - server.once('error', rejectListen); - server.listen(0, '127.0.0.1', resolveListen); - }); - const address = server.address(); - assert(address && typeof address !== 'string', 'loopback provider did not expose an address'); - return { - baseUrl: `http://127.0.0.1:${address.port}/v1`, - records, - close: () => new Promise((resolveClose, rejectClose) => { - server.close((error) => error ? rejectClose(error) : resolveClose()); - }), - }; -} - -function buildInvocationArguments(options) { - const args = ['app-server', '--stdio', '--strict-config']; - for (const feature of DISABLED_FEATURES) args.push('--disable', feature); - assert(Array.isArray(options.mcpServerNames), 'MCP server inventory is missing'); - const mcpServerEntries = options.mcpServerNames.map((name) => { - assert(!/[\u0000\r\n']/.test(name), 'MCP server name contains an unsupported character'); - return `'${name}'={enabled=false}`; - }); - const hookStateEntries = []; - const hookStateKeys = new Set(); - for (const key of options.hookDisables ?? []) { - assert(!/[\u0000\r\n']/.test(key), 'disabled hook key contains an unsupported character'); - assert(!hookStateKeys.has(key), 'duplicate hook-state key'); - hookStateKeys.add(key); - hookStateEntries.push(`'${key}'={enabled=false}`); - } - for (const { key, currentHash } of options.hookPins) { - assert(!/[\u0000\r\n']/.test(key), 'hook key contains an unsupported character'); - assert(!hookStateKeys.has(key), 'duplicate hook-state key'); - assert(/^sha256:[0-9a-f]{64}$/.test(currentHash), 'hook currentHash is invalid'); - hookStateKeys.add(key); - hookStateEntries.push(`'${key}'={enabled=true,trusted_hash='${currentHash}'}`); - } - const overrides = [ - ...CONFIG_OVERRIDES, - 'model_reasoning_effort="low"', - `mcp_servers={${mcpServerEntries.join(',')}}`, - `model_catalog_json=${tomlString(options.catalogPath)}`, - ...(options.providerBaseUrl ? [ - 'model_provider="waggle_loopback"', - `model_providers.waggle_loopback.name="Waggle zero-cost loopback"`, - `model_providers.waggle_loopback.base_url=${tomlString(options.providerBaseUrl)}`, - 'model_providers.waggle_loopback.wire_api="responses"', - 'model_providers.waggle_loopback.request_max_retries=0', - 'model_providers.waggle_loopback.stream_max_retries=0', - 'model_providers.waggle_loopback.requires_openai_auth=false', - 'model_providers.waggle_loopback.supports_websockets=false', - ] : [ - 'model_provider="openai"', - ]), - ...(options.includeDeny ? [options.denyConfig] : []), - ...(hookStateEntries.length > 0 ? [`hooks.state={${hookStateEntries.join(',')}}`] : []), - ]; - for (const override of overrides) args.push('-c', override); - return args; -} - -function normalizedArguments(args, sensitiveValues) { - return args.map((argument) => { - if (argument.startsWith('hooks.state={')) { - return `hooks.state=`; - } - if (argument.startsWith('mcp_servers={')) { - return `mcp_servers=`; - } - let normalized = argument; - for (const [value, label] of sensitiveValues) { - if (value) normalized = normalized.replaceAll(value, label); - } - return normalized; - }); -} - -class AppServerClient { - constructor(executable, args, cwd, env, replacements) { - this.responses = new Map(); - this.notifications = []; - this.stdoutChunks = []; - this.stderrChunks = []; - this.buffer = ''; - this.sequence = 0; - this.closed = false; - this.error = null; - this.replacements = replacements; - this.child = spawn(executable, args, { - cwd, - env, - stdio: ['pipe', 'pipe', 'pipe'], - windowsHide: true, - shell: false, - }); - this.child.stdout.on('data', (chunk) => { - appendBounded(this.stdoutChunks, chunk); - this.buffer += chunk.toString('utf8'); - while (this.buffer.includes('\n')) { - const newline = this.buffer.indexOf('\n'); - const line = this.buffer.slice(0, newline).trim(); - this.buffer = this.buffer.slice(newline + 1); - this.processLine(line); - } - }); - this.child.stderr.on('data', (chunk) => appendBounded(this.stderrChunks, chunk)); - this.child.on('error', (error) => { - this.error = safeError(error, replacements); - }); - this.child.on('close', () => { - this.closed = true; - this.processLine(this.buffer.trim()); - this.buffer = ''; - }); - } - - processLine(line) { - if (!line) return; - let message; - try { message = JSON.parse(line); } catch { - this.error = this.error ?? 'Codex app-server emitted malformed protocol output'; - return; - } - this.sequence += 1; - if (message?.id !== undefined && typeof message?.method !== 'string') { - this.responses.set(String(message.id), message); - } else if (typeof message?.method === 'string') { - this.notifications.push({ sequence: this.sequence, message }); - } - } - - send(message) { - assert(this.child.stdin.writable, 'Codex app-server stdin is closed'); - this.child.stdin.write(`${JSON.stringify(message)}\n`); - } - - response(id, timeoutMs = 20_000) { - return waitForValue( - () => this.responses.get(String(id)), - timeoutMs, - `Codex app-server response ${id}`, - ); - } - - notification(method, predicate = () => true, timeoutMs = TURN_TIMEOUT_MS) { - return waitForValue( - () => this.notifications.find((entry) => ( - entry.message?.method === method && predicate(entry) - )), - timeoutMs, - `Codex app-server notification ${method}`, - ); - } - - async initialize(clientName) { - this.send({ - method: 'initialize', - id: 1, - params: { - clientInfo: { name: clientName, title: clientName, version: '1.0.0' }, - capabilities: { experimentalApi: true }, - }, - }); - const response = await this.response(1); - assertProtocolSuccess(response, 'Codex app-server initialize failed'); - this.send({ method: 'initialized', params: {} }); - } - - async close() { - try { this.child.stdin.end(); } catch { /* already closed */ } - try { - await waitForValue(() => this.closed ? true : undefined, 5_000, 'Codex app-server close'); - } catch { - if (this.child.pid) { - spawnSync('taskkill.exe', ['/PID', String(this.child.pid), '/T', '/F'], { - encoding: 'utf8', - timeout: 10_000, - windowsHide: true, - }); - } - try { this.child.kill('SIGKILL'); } catch { /* already closed */ } - await waitForValue(() => this.closed ? true : undefined, 5_000, 'forced Codex app-server close'); - } - } - - transcriptDigest() { - return { - stdoutBytes: Buffer.concat(this.stdoutChunks).length, - stdoutSha256: sha256(Buffer.concat(this.stdoutChunks)), - stderrBytes: Buffer.concat(this.stderrChunks).length, - stderrSha256: sha256(Buffer.concat(this.stderrChunks)), - }; - } -} - -function assertProtocolSuccess(response, message) { - if (!response?.error) return; - const error = new Error(message); - error.protocolCode = sanitizeProtocolCode(response.error?.code); - throw error; -} - -function hookGraphFromEntry(entry) { - const hooks = Array.isArray(entry?.hooks) ? entry.hooks : []; - return hooks.map((hook) => ({ - eventName: hook?.eventName ?? null, - sourcePath: hook?.sourcePath ?? null, - sourceCanonical: typeof hook?.sourcePath === 'string' && existsSync(hook.sourcePath) - ? canonicalPath(hook.sourcePath) - : String(hook?.sourcePath ?? '').toLowerCase(), - key: hook?.key ?? null, - currentHash: hook?.currentHash ?? null, - commandSha256: typeof hook?.command === 'string' ? sha256(hook.command) : null, - enabled: hook?.enabled === true, - trustStatus: hook?.trustStatus ?? null, - })); -} - -function assertExactHookGraph( - entry, - codexHome, - includeDeny, - requireTrusted, - expectedDenyCommandSha256, - expectedWaggleCommands, - allowActiveExtras, -) { - const graph = hookGraphFromEntry(entry); - const expectedCount = WAGGLE_HOOK_EVENTS.length + (includeDeny ? 1 : 0); - const warnings = Array.isArray(entry?.warnings) ? entry.warnings.length : -1; - const errors = Array.isArray(entry?.errors) ? entry.errors.length : -1; - assert(warnings === 0, `hooks/list reported ${warnings} warnings`); - assert(errors === 0, `hooks/list reported ${errors} errors`); - const waggleSource = canonicalPath(join(codexHome, 'hooks.json')); - assert(expectedWaggleCommands instanceof Map, 'expected Waggle hook commands are missing'); - const waggle = graph.filter((hook) => ( - hook.sourceCanonical === waggleSource - && hook.commandSha256 === expectedWaggleCommands.get(hook.eventName) - )); - const deny = graph.filter((hook) => ( - hook.eventName === 'preToolUse' - && hook.sourceCanonical === 'c:\\\\config.toml' - && hook.commandSha256 === expectedDenyCommandSha256 - )); - const waggleSourceMatches = graph.filter((hook) => hook.sourceCanonical === waggleSource).length; - const waggleCommandMatches = graph.filter((hook) => ( - hook.commandSha256 === expectedWaggleCommands.get(hook.eventName) - )).length; - assert( - waggle.length === 4, - `hooks/list returned ${waggle.length} Waggle hooks, expected 4 (sourceMatches=${waggleSourceMatches}, commandMatches=${waggleCommandMatches})`, - ); - assert( - JSON.stringify(waggle.map((hook) => hook.eventName).sort()) - === JSON.stringify([...WAGGLE_HOOK_EVENTS].sort()), - 'hooks/list returned an unexpected Waggle event set', - ); - assert(deny.length === (includeDeny ? 1 : 0), 'hooks/list returned an unexpected deny-hook count'); - const expected = [...waggle, ...deny]; - const expectedKeys = new Set(expected.map((hook) => hook.key)); - const extras = graph.filter((hook) => !expectedKeys.has(hook.key)); - assert(graph.every((hook) => ( - typeof hook.key === 'string' - && /^sha256:[0-9a-f]{64}$/.test(hook.currentHash ?? '') - && typeof hook.commandSha256 === 'string' - )), 'hooks/list returned a malformed hook'); - assert(expected.every((hook) => hook.enabled), 'an expected hook is disabled'); - if (!allowActiveExtras) { - assert(extras.every((hook) => !hook.enabled), 'an unapproved hook remains enabled'); - } - if (requireTrusted) { - assert(expected.every((hook) => hook.trustStatus === 'trusted'), 'an expected hook is not hash-trusted'); - } - const sanitized = graph.map((hook) => ({ - eventName: hook.eventName, - sourceKind: waggle.includes(hook) - ? 'waggle-hooks-json' - : deny.includes(hook) ? 'session-flags' : 'disabled-external', - sourcePathSha256: sha256(hook.sourceCanonical), - keySha256: sha256(hook.key), - currentHash: hook.currentHash, - commandSha256: hook.commandSha256, - enabled: hook.enabled, - trustStatus: requireTrusted ? hook.trustStatus : 'pre-pin', - })).sort((left, right) => stableJson(left).localeCompare(stableJson(right))); - const identity = sanitized.map(({ enabled: _enabled, trustStatus: _trustStatus, ...value }) => value); - return { - graph, - expected, - extras, - sanitized, - graphSha256: sha256(stableJson(sanitized)), - identitySha256: sha256(stableJson(identity)), - warnings, - errors, - activeCount: graph.filter((hook) => hook.enabled).length, - extraCount: extras.filter((hook) => hook.enabled).length, - disabledExtraCount: extras.filter((hook) => !hook.enabled).length, - denyHookSha256: includeDeny ? deny[0].currentHash.slice('sha256:'.length) : null, - }; -} - -async function listHooks( - executable, - args, - cwd, - env, - codexHome, - includeDeny, - requireTrusted, - expectedDenyCommandSha256, - expectedWaggleCommands, - allowActiveExtras, - replacements, -) { - const client = new AppServerClient(executable, args, cwd, env, replacements); - try { - await client.initialize('waggle_codex_tool_denial_discovery'); - client.send({ method: 'hooks/list', id: 2, params: { cwds: [cwd] } }); - const response = await client.response(2); - assert(!response.error, 'Codex app-server hooks/list failed'); - const entry = response.result?.data?.[0]; - const result = assertExactHookGraph( - entry, - codexHome, - includeDeny, - requireTrusted, - expectedDenyCommandSha256, - expectedWaggleCommands, - allowActiveExtras, - ); - assert(client.error === null, client.error ?? 'Codex hook discovery failed'); - return { ...result, transcript: client.transcriptDigest() }; - } finally { - await client.close(); - } -} - -function itemType(notification) { - return notification?.message?.params?.item?.type ?? null; -} - -function treeContains(value, needle) { - if (typeof value === 'string') return value.includes(needle); - if (Array.isArray(value)) return value.some((child) => treeContains(child, needle)); - if (value && typeof value === 'object') return Object.values(value).some((child) => treeContains(child, needle)); - return false; -} - -function notificationScope(notification) { - const params = notification?.message?.params ?? {}; - return { - threadIds: [params.threadId, params.thread?.id, params.turn?.threadId] - .filter((value) => typeof value === 'string'), - turnIds: [params.turnId, params.turn?.id, params.item?.turnId] - .filter((value) => typeof value === 'string'), - }; -} - -function isFailClosedProjectTrustWarning(notification) { - if (notification?.message?.method !== 'configWarning') return false; - const params = notification.message?.params; - if (!params || typeof params !== 'object' || Array.isArray(params)) return false; - const allowedKeys = new Set(['details', 'path', 'range', 'summary']); - const keys = Object.keys(params); - if (!keys.includes('summary')) return false; - if (!keys.every((key) => allowedKeys.has(key))) return false; - if (keys.includes('details') && params.details !== null) return false; - if (keys.includes('path') && params.path !== null) return false; - if (keys.includes('range') && params.range !== null) return false; - if (typeof params.summary !== 'string') return false; - const lines = params.summary.split('\n'); - if (lines.at(-1) !== '') return false; - if (lines[0] !== FAIL_CLOSED_PROJECT_TRUST_WARNING) return false; - const warningLines = lines.slice(1, -1); - if (warningLines.length < 2 || warningLines.length % 2 !== 0) return false; - const folderPattern = /^ ([1-9][0-9]*)\. ([A-Za-z]:[\\/].+[\\/]\.codex)$/; - const instructionPattern = /^ To load project-local config, hooks, and exec policies, add ([A-Za-z]:[\\/].+) as a trusted project in ([A-Za-z]:[\\/].+[\\/]\.codex[\\/]config\.toml)\.$/; - const normalizePath = (value) => value.replaceAll('/', '\\').replace(/\\+$/, '').toLowerCase(); - for (let index = 0; index < warningLines.length; index += 2) { - const folderMatch = folderPattern.exec(warningLines[index]); - const instructionMatch = instructionPattern.exec(warningLines[index + 1]); - if (!folderMatch || !instructionMatch) return false; - if (Number(folderMatch[1]) !== (index / 2) + 1) return false; - if (normalizePath(folderMatch[2]) !== `${normalizePath(instructionMatch[1])}\\.codex`) { - return false; - } - } - return true; -} - -function collectEventAudit(notifications, expectedAcknowledgement, threadId, turnId) { - const failureNotifications = notifications.filter((entry) => ( - FAILURE_NOTIFICATION_METHODS.has(entry.message?.method) - && !isFailClosedProjectTrustWarning(entry) - )); - const unknownNotifications = notifications.filter((entry) => ( - !ALLOWED_NOTIFICATION_METHODS.has(entry.message?.method) - && !FAILURE_NOTIFICATION_METHODS.has(entry.message?.method) - )); - const wrongScopeNotifications = notifications.filter((entry) => { - const scope = notificationScope(entry); - return scope.threadIds.some((value) => value !== threadId) - || scope.turnIds.some((value) => value !== turnId); - }); - const rerouteNotifications = notifications.filter((entry) => ( - entry.message?.method === 'model/rerouted' - )); - const itemNotifications = notifications.filter((entry) => ( - entry.message?.method === 'item/started' - || entry.message?.method === 'item/updated' - || entry.message?.method === 'item/completed' - )); - const unknownItems = itemNotifications.filter((entry) => !ALLOWED_ITEM_TYPES.has(itemType(entry))); - const completedMessages = notifications.filter((entry) => ( - entry.message?.method === 'item/completed' - && itemType(entry) === 'agentMessage' - )); - const exactMessages = completedMessages.filter((entry) => ( - entry.message?.params?.item?.text?.trim() === expectedAcknowledgement - )); - const imageItems = itemNotifications.filter((entry) => ( - String(itemType(entry)).toLowerCase().includes('image') - || treeContains(entry.message?.params?.item, 'view_image') - )); - const denyHooks = notifications.filter((entry) => ( - entry.message?.method === 'hook/completed' - && entry.message?.params?.run?.eventName === 'preToolUse' - && treeContains(entry.message?.params?.run, DENIAL_REASON) - )); - const forbiddenToolItems = itemNotifications.filter((entry) => { - const type = String(itemType(entry) ?? '').toLowerCase(); - return [ - 'command', - 'computer', - 'dynamictool', - 'filechange', - 'imagegeneration', - 'mcptool', - 'websearch', - ].some((family) => type.includes(family)); - }); - const notificationGraph = notifications.map((entry, index) => { - const method = entry.message?.method; - const type = itemType(entry); - return { - ordinal: index + 1, - methodSha256: typeof method === 'string' ? sha256(method) : null, - itemTypeSha256: typeof type === 'string' ? sha256(type) : null, - }; - }); - return { - notificationCount: notifications.length, - exactMessageCount: exactMessages.length, - normalTextCompleted: completedMessages.length === 1 && exactMessages.length === 1, - imageItemCount: imageItems.length, - firstImageSequence: imageItems[0]?.sequence ?? null, - denyHookCount: denyHooks.length, - firstDenySequence: denyHooks[0]?.sequence ?? null, - toolEventsObserved: forbiddenToolItems.length, - unknownEventsObserved: unknownNotifications.length + unknownItems.length, - unknownNotificationEventsObserved: unknownNotifications.length, - unknownItemEventsObserved: unknownItems.length, - failureEventsObserved: failureNotifications.length, - wrongScopeEventsObserved: wrongScopeNotifications.length, - rerouteEventsObserved: rerouteNotifications.length, - notificationGraphSha256: sha256(stableJson(notificationGraph)), - rejectionEvidence: { - failureMethodSha256: groupSha256(failureNotifications.map((entry) => entry.message?.method)), - wrongScopeMethodSha256: groupSha256(wrongScopeNotifications.map((entry) => entry.message?.method)), - rerouteMethodSha256: groupSha256(rerouteNotifications.map((entry) => entry.message?.method)), - unknownMethodSha256: groupSha256(unknownNotifications.map((entry) => entry.message?.method)), - unknownItemTypeSha256: groupSha256(unknownItems.map((entry) => itemType(entry))), - toolMethodSha256: groupSha256(forbiddenToolItems.map((entry) => entry.message?.method)), - toolItemTypeSha256: groupSha256(forbiddenToolItems.map((entry) => itemType(entry))), - agentMessageMethodSha256: groupSha256(completedMessages.map((entry) => entry.message?.method)), - agentMessageItemTypeSha256: groupSha256(completedMessages.map((entry) => itemType(entry))), - }, - }; -} - -function throwEventAuditFailure(invariant, audit) { - const evidence = audit.rejectionEvidence; - let rejectedMethodSha256 = []; - let rejectedItemTypeSha256 = []; - if (invariant === 'failure-notification') rejectedMethodSha256 = evidence.failureMethodSha256; - if (invariant === 'wrong-scope') rejectedMethodSha256 = evidence.wrongScopeMethodSha256; - if (invariant === 'model-reroute') rejectedMethodSha256 = evidence.rerouteMethodSha256; - if (invariant === 'unknown-notification-method') rejectedMethodSha256 = evidence.unknownMethodSha256; - if (invariant === 'unknown-item-type') rejectedItemTypeSha256 = evidence.unknownItemTypeSha256; - if (invariant === 'forbidden-tool-item') { - rejectedMethodSha256 = evidence.toolMethodSha256; - rejectedItemTypeSha256 = evidence.toolItemTypeSha256; - } - if (invariant === 'agent-message-cardinality') { - rejectedMethodSha256 = evidence.agentMessageMethodSha256; - rejectedItemTypeSha256 = evidence.agentMessageItemTypeSha256; - } - const error = new Error('Codex event audit rejected a sanitized invariant'); - error.eventAuditFailure = sanitizeEventAuditFailure({ - invariant, - notificationCount: audit.notificationCount, - failureEventsObserved: audit.failureEventsObserved, - wrongScopeEventsObserved: audit.wrongScopeEventsObserved, - rerouteEventsObserved: audit.rerouteEventsObserved, - unknownNotificationEventsObserved: audit.unknownNotificationEventsObserved, - unknownItemEventsObserved: audit.unknownItemEventsObserved, - toolEventsObserved: audit.toolEventsObserved, - notificationGraphSha256: audit.notificationGraphSha256, - rejectedMethodSha256, - rejectedItemTypeSha256, - }); - throw error; -} - -function assertEventAudit(audit, strictAllowlist, requireAcknowledgement = true) { - if (audit.failureEventsObserved !== 0) throwEventAuditFailure('failure-notification', audit); - if (audit.wrongScopeEventsObserved !== 0) throwEventAuditFailure('wrong-scope', audit); - if (audit.rerouteEventsObserved !== 0) throwEventAuditFailure('model-reroute', audit); - if (strictAllowlist) { - if (audit.unknownNotificationEventsObserved !== 0) { - throwEventAuditFailure('unknown-notification-method', audit); - } - if (audit.unknownItemEventsObserved !== 0) throwEventAuditFailure('unknown-item-type', audit); - if (audit.toolEventsObserved !== 0) throwEventAuditFailure('forbidden-tool-item', audit); - } - if (requireAcknowledgement) { - if (!audit.normalTextCompleted) throwEventAuditFailure('agent-message-cardinality', audit); - } -} - -function eventAudit( - notifications, - expectedAcknowledgement, - strictAllowlist, - threadId = null, - turnId = null, - requireAcknowledgement = true, -) { - const audit = collectEventAudit( - notifications, - expectedAcknowledgement, - threadId, - turnId, - ); - assertEventAudit(audit, strictAllowlist, requireAcknowledgement); - return audit; -} - -async function runTurn(options) { - const client = new AppServerClient( - options.executable, - options.args, - options.workspace, - options.env, - options.replacements, - ); - let threadId = null; - let turnId = null; - let result = null; - let primaryError = null; - let closeError = null; - let failureStage = 'app-server-spawn'; - let completedStage = 'app-server-spawn'; - let modelCalls = 0; - let turnStartCalls = 0; - let closeClean = false; - try { - failureStage = 'initialize'; - await client.initialize(`waggle_codex_tool_denial_${options.lane}`); - completedStage = 'initialize'; - failureStage = 'hooks-list'; - client.send({ method: 'hooks/list', id: 2, params: { cwds: [options.workspace] } }); - const hookResponse = await client.response(2); - assertProtocolSuccess(hookResponse, 'Codex app-server hooks/list failed'); - const hooks = assertExactHookGraph( - hookResponse.result?.data?.[0], - options.codexHome, - options.includeDeny, - true, - options.expectedDenyCommandSha256, - options.expectedWaggleCommands, - false, - ); - assert(hooks.graphSha256 === options.expectedHookGraphSha256, 'hook graph changed after hash pinning'); - completedStage = 'hooks-list'; - failureStage = 'pre-turn-boundary'; - if (options.beforeTurn) await options.beforeTurn(); - completedStage = 'pre-turn-boundary'; - - const threadParams = { - model: options.model, - cwd: options.workspace, - approvalPolicy: 'never', - sandbox: 'read-only', - ephemeral: true, - allowProviderModelFallback: false, - ...(options.useDefaultEnvironmentForControl ? {} : { environments: [] }), - selectedCapabilityRoots: [], - dynamicTools: [], - }; - failureStage = 'thread-start'; - client.send({ method: 'thread/start', id: 3, params: threadParams }); - const threadResponse = await client.response(3); - assertProtocolSuccess(threadResponse, 'Codex app-server thread/start failed'); - threadId = threadResponse.result?.thread?.id ?? null; - assert(typeof threadId === 'string' && threadId.length > 0, 'Codex app-server omitted thread id'); - completedStage = 'thread-start'; - - if (options.setupOnly) { - result = { - hooks, - events: null, - threadParamsSha256: sha256(stableJson({ ...threadParams, cwd: '' })), - turnParamsSha256: null, - threadId, - threadIdSha256: sha256(threadId), - turnIdSha256: null, - transcript: client.transcriptDigest(), - modelCalls: 0, - turnStartCalls: 0, - completedStage: 'thread-start', - }; - } else { - const turnParams = { - threadId, - input: [{ type: 'text', text: options.prompt, text_elements: [] }], - model: options.model, - approvalPolicy: 'never', - sandboxPolicy: { type: 'readOnly', networkAccess: false }, - ...(options.useDefaultEnvironmentForControl ? {} : { environments: [] }), - }; - failureStage = 'turn-start'; - turnStartCalls += 1; - client.send({ method: 'turn/start', id: 4, params: turnParams }); - const turnResponse = await client.response(4); - assertProtocolSuccess(turnResponse, 'Codex app-server turn/start failed'); - modelCalls += 1; - turnId = turnResponse.result?.turn?.id ?? null; - assert(typeof turnId === 'string' && turnId.length > 0, 'Codex app-server omitted turn id'); - completedStage = 'turn-start'; - failureStage = 'turn-completed'; - const completed = await client.notification('turn/completed', (entry) => ( - entry.message?.params?.threadId === threadId - && entry.message?.params?.turn?.id === turnId - )); - await new Promise((resolveWait) => setTimeout(resolveWait, 100)); - const completedTurn = completed.message?.params?.turn; - if (completedTurn?.status !== 'completed') { - const errorNotification = [...client.notifications].reverse().find((entry) => ( - entry.message?.method === 'error' - && entry.message?.params?.threadId === threadId - && entry.message?.params?.turnId === turnId - )) ?? null; - const terminalError = new Error('Codex app-server turn did not complete'); - terminalError.turnFailure = sanitizeTurnFailure(completedTurn, errorNotification); - throw terminalError; - } - completedStage = 'turn-completed'; - failureStage = 'event-audit'; - const events = eventAudit( - client.notifications, - options.expectedAcknowledgement, - options.strictAllowlist, - threadId, - turnId, - ); - completedStage = 'event-audit'; - failureStage = 'post-turn-invariants'; - assert(client.error === null, client.error ?? 'Codex app-server protocol failed'); - result = { - hooks, - events, - threadParamsSha256: sha256(stableJson({ ...threadParams, cwd: '' })), - turnParamsSha256: sha256(stableJson({ ...turnParams, threadId: '', input: '' })), - threadId, - threadIdSha256: sha256(threadId), - turnIdSha256: sha256(turnId), - transcript: client.transcriptDigest(), - modelCalls, - turnStartCalls, - completedStage: 'post-turn-invariants', - }; - completedStage = 'post-turn-invariants'; - } - } catch (error) { - primaryError = error; - } - try { - await client.close(); - closeClean = true; - } catch (error) { - closeError = error; - } - if (primaryError === null && closeError === null) { - try { - failureStage = 'event-audit'; - const finalEvents = eventAudit( - client.notifications, - options.setupOnly ? null : options.expectedAcknowledgement, - options.strictAllowlist, - threadId, - options.setupOnly ? null : turnId, - !options.setupOnly, - ); - assert( - client.error === null, - client.error ?? 'Codex app-server protocol failed after close', - ); - result.events = finalEvents; - } catch (error) { - primaryError = error; - } - } - const lifecycleFailure = selectLifecycleFailure(primaryError, closeError); - if (lifecycleFailure !== null) { - const wrapped = new Error(`Codex app-server canary failed at ${sanitizeCanaryStage( - primaryError === null ? 'app-server-close' : failureStage, - ) ?? 'app-server-spawn'}`); - wrapped.turnFailure = lifecycleFailure?.turnFailure ?? null; - wrapped.eventAuditFailure = sanitizeEventAuditFailure(lifecycleFailure?.eventAuditFailure); - wrapped.canaryDiagnostic = { - failureStage: sanitizeCanaryStage(primaryError === null ? 'app-server-close' : failureStage), - protocolCode: sanitizeProtocolCode(lifecycleFailure?.protocolCode), - completedStage: sanitizeCanaryStage(completedStage), - modelCalls, - turnStartCalls, - closeClean, - }; - throw wrapped; - } - return { - ...result, - diagnostic: { - failureStage: null, - protocolCode: null, - completedStage: result.completedStage, - modelCalls, - turnStartCalls, - closeClean, - }, - }; -} - -async function createSentinelPng(path) { - const png = Buffer.from( - 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=', - 'base64', - ); - await writeFile(path, png); - requireRegularFile(path, 'owned sentinel PNG'); - return sha256(png); -} - -async function runValidationSelfTest() { - const root = join(tmpdir(), `waggle-codex-denial-self-test-${randomBytes(12).toString('hex')}`); - const codexHome = join(root, 'codex-home'); - const hooksPath = join(codexHome, 'hooks.json'); - let cases = 0; - const expectThrow = (run, label) => { - let threw = false; - try { run(); } catch { threw = true; } - assert(threw, `negative fixture was accepted: ${label}`); - cases += 1; - }; - const captureEventAuditFailure = (run, label) => { - try { - run(); - } catch (error) { - const diagnostic = sanitizeEventAuditFailure(error?.eventAuditFailure); - assert(diagnostic !== null, `event-audit fixture omitted sanitized diagnostics: ${label}`); - return diagnostic; - } - throw new Error(`event-audit fixture was accepted: ${label}`); - }; - try { - assert( - stableJson([...CANARY_STAGES]) === stableJson([ - 'app-server-spawn', - 'initialize', - 'hooks-list', - 'pre-turn-boundary', - 'thread-start', - 'turn-start', - 'turn-completed', - 'event-audit', - 'app-server-close', - 'post-turn-invariants', - ]), - 'canary stage fixture changed', - ); - cases += 1; - assert( - sanitizeProtocolCode(-2_147_483_648) === -2_147_483_648 - && sanitizeProtocolCode(2_147_483_647) === 2_147_483_647, - 'signed int32 protocol-code fixture failed', - ); - cases += 1; - assert( - sanitizeProtocolCode(-2_147_483_649) === null - && sanitizeProtocolCode(2_147_483_648) === null - && sanitizeProtocolCode(1.5) === null - && sanitizeProtocolCode('1') === null, - 'invalid protocol-code fixture was accepted', - ); - cases += 1; - const inventoryArguments = buildMcpInventoryArguments(); - assert( - inventoryArguments.length === (DISABLED_FEATURES.length * 2) + 3 - && JSON.stringify(inventoryArguments.slice(-3)) === JSON.stringify(['mcp', 'list', '--json']) - && DISABLED_FEATURES.every((feature, index) => ( - inventoryArguments[index * 2] === '--disable' - && inventoryArguments[(index * 2) + 1] === feature - )), - 'MCP inventory did not inherit the app-server feature-disable boundary', - ); - cases += 1; - assert( - win32.join('C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe') - === 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', - 'package-command Windows PowerShell spelling fixture failed', - ); - await mkdir(codexHome, { recursive: true }); - await writeFile(hooksPath, '{}\n', 'utf8'); - const expectedCommands = new Map(); - const hooks = WAGGLE_HOOK_EVENTS.map((eventName, index) => { - const command = `fixture-waggle-command-${index}`; - expectedCommands.set(eventName, sha256(command)); - return { - eventName, - sourcePath: hooksPath, - key: `fixture-waggle-key-${index}`, - currentHash: `sha256:${String(index + 1).padStart(64, '0')}`, - command, - enabled: true, - trustStatus: 'trusted', - }; - }); - const denyCommand = 'fixture-deny-command'; - hooks.push({ - eventName: 'preToolUse', - sourcePath: 'C:\\\\config.toml', - key: 'fixture-deny-key', - currentHash: `sha256:${'f'.repeat(64)}`, - command: denyCommand, - enabled: true, - trustStatus: 'trusted', - }); - const validEntry = { hooks, warnings: [], errors: [] }; - const valid = assertExactHookGraph( - validEntry, - codexHome, - true, - true, - sha256(denyCommand), - expectedCommands, - false, - ); - assert(valid.graph.length === 5, 'valid hook fixture did not pass'); - cases += 1; - - const withExtra = structuredClone(validEntry); - withExtra.hooks.push({ ...withExtra.hooks[0], key: 'extra-key' }); - expectThrow(() => assertExactHookGraph( - withExtra, codexHome, true, true, sha256(denyCommand), expectedCommands, false, - ), 'extra hook'); - - const activeExternal = structuredClone(validEntry); - activeExternal.hooks.push({ - ...activeExternal.hooks[0], - sourcePath: 'C:\\external\\hooks.json', - key: 'external-key', - command: 'external-command', - }); - expectThrow(() => assertExactHookGraph( - activeExternal, codexHome, true, true, sha256(denyCommand), expectedCommands, false, - ), 'active external hook'); - const disabledExternal = structuredClone(activeExternal); - disabledExternal.hooks[5].enabled = false; - const disabledResult = assertExactHookGraph( - disabledExternal, codexHome, true, true, sha256(denyCommand), expectedCommands, false, - ); - assert(disabledResult.extraCount === 0 && disabledResult.disabledExtraCount === 1, - 'disabled external hook fixture did not pass'); - cases += 1; - - const wrongDenySource = structuredClone(validEntry); - wrongDenySource.hooks[4].sourcePath = 'C:\\temp\\config.toml'; - expectThrow(() => assertExactHookGraph( - wrongDenySource, codexHome, true, true, sha256(denyCommand), expectedCommands, false, - ), 'deny source'); - - const wrongDenyCommand = structuredClone(validEntry); - wrongDenyCommand.hooks[4].command = 'different-deny-command'; - expectThrow(() => assertExactHookGraph( - wrongDenyCommand, codexHome, true, true, sha256(denyCommand), expectedCommands, false, - ), 'deny command'); - - const wrongWaggleCommand = structuredClone(validEntry); - wrongWaggleCommand.hooks[0].command = 'different-waggle-command'; - expectThrow(() => assertExactHookGraph( - wrongWaggleCommand, codexHome, true, true, sha256(denyCommand), expectedCommands, false, - ), 'packaged Waggle command'); - - const untrusted = structuredClone(validEntry); - untrusted.hooks[0].trustStatus = 'untrusted'; - expectThrow(() => assertExactHookGraph( - untrusted, codexHome, true, true, sha256(denyCommand), expectedCommands, false, - ), 'untrusted hook'); - - const acknowledgement = 'SELF_TEST_ACK'; - const safeNotification = { - sequence: 1, - message: { - method: 'item/completed', - params: { item: { type: 'agentMessage', text: acknowledgement } }, - }, - }; - assert(eventAudit([safeNotification], acknowledgement, true).normalTextCompleted, 'safe event fixture did not pass'); - cases += 1; - for (const itemTypeValue of [ - 'commandExecution', - 'computerUse', - 'dynamicToolCall', - 'fileChange', - 'imageGeneration', - 'imageView', - 'mcpToolCall', - 'toolSearch', - 'webSearch', - ]) { - const bad = { - sequence: 2, - message: { method: 'item/completed', params: { item: { type: itemTypeValue } } }, - }; - expectThrow(() => eventAudit([safeNotification, bad], acknowledgement, true), itemTypeValue); - } - const unknownNotification = { sequence: 2, message: { method: 'future/tool/event', params: {} } }; - expectThrow( - () => eventAudit([safeNotification, unknownNotification], acknowledgement, true), - 'unknown notification', - ); - const failureNotification = { sequence: 2, message: { method: 'warning', params: {} } }; - expectThrow( - () => eventAudit([safeNotification, failureNotification], acknowledgement, true), - 'failure notification', - ); - const projectTrustWarning = { - sequence: 2, - message: { - method: 'configWarning', - params: { - summary: `${FAIL_CLOSED_PROJECT_TRUST_WARNING}\n` - + ' 1. C:\\fixture\\.codex\n' - + ' To load project-local config, hooks, and exec policies, add c:\\fixture as a trusted project in C:\\Users\\fixture\\.codex\\config.toml.\n', - details: null, - path: null, - range: null, - }, - }, - }; - const remoteControlStatus = { - sequence: 3, - message: { method: 'remoteControl/status/changed', params: { status: 'disconnected' } }, - }; - const failClosedTrustAudit = eventAudit( - [safeNotification, projectTrustWarning, remoteControlStatus], - acknowledgement, - true, - ); - assert( - failClosedTrustAudit.failureEventsObserved === 0 - && failClosedTrustAudit.unknownEventsObserved === 0, - 'fail-closed project trust warning or schema-backed remote-control status was rejected', - ); - cases += 1; - const unsafeTrustWarning = { - ...projectTrustWarning, - message: { - ...projectTrustWarning.message, - params: { ...projectTrustWarning.message.params, details: 'unexpected detail' }, - }, - }; - const unsafeTrustDiagnostic = captureEventAuditFailure( - () => eventAudit([safeNotification, unsafeTrustWarning], acknowledgement, true), - 'unsafe project trust warning', - ); - assert( - unsafeTrustDiagnostic.invariant === 'failure-notification' - && stableJson(unsafeTrustDiagnostic.rejectedMethodSha256) === stableJson([ - { sha256: sha256('configWarning'), count: 1 }, - ]), - 'non-canonical project trust warning was not rejected', - ); - cases += 1; - const appendedTrustWarning = { - ...projectTrustWarning, - message: { - ...projectTrustWarning.message, - params: { - ...projectTrustWarning.message.params, - summary: `${projectTrustWarning.message.params.summary}unexpected suffix\n`, - }, - }, - }; - expectThrow( - () => eventAudit([safeNotification, appendedTrustWarning], acknowledgement, true), - 'appended project trust warning', - ); - const extraParamTrustWarning = { - ...projectTrustWarning, - message: { - ...projectTrustWarning.message, - params: { ...projectTrustWarning.message.params, extra: null }, - }, - }; - expectThrow( - () => eventAudit([safeNotification, extraParamTrustWarning], acknowledgement, true), - 'extra-param project trust warning', - ); - const nearMatchTrustWarning = { - ...projectTrustWarning, - message: { - ...projectTrustWarning.message, - params: { - ...projectTrustWarning.message.params, - summary: projectTrustWarning.message.params.summary.replace( - FAIL_CLOSED_PROJECT_TRUST_WARNING, - `${FAIL_CLOSED_PROJECT_TRUST_WARNING} Additional guidance follows.`, - ), - }, - }, - }; - expectThrow( - () => eventAudit([safeNotification, nearMatchTrustWarning], acknowledgement, true), - 'near-match project trust warning', - ); - const unknownDiagnostic = captureEventAuditFailure( - () => eventAudit([safeNotification, unknownNotification], acknowledgement, true), - 'unknown diagnostic', - ); - assert( - unknownDiagnostic.invariant === 'unknown-notification-method' - && unknownDiagnostic.notificationCount === 2 - && unknownDiagnostic.unknownNotificationEventsObserved === 1 - && stableJson(unknownDiagnostic.rejectedMethodSha256) === stableJson([ - { sha256: sha256('future/tool/event'), count: 1 }, - ]) - && Object.keys(unknownDiagnostic).join(',') === EVENT_AUDIT_FAILURE_KEYS.join(',') - && !stableJson(unknownDiagnostic).includes('future/tool/event'), - 'unknown notification diagnostic was not fixed, hashed, and path-free', - ); - cases += 1; - const secretVariant = { - sequence: 2, - message: { - method: 'future/tool/event', - params: { text: 'sk-private-value', path: 'C:\\private\\auth.json', sessionId: 'sess-private' }, - }, - }; - const secretDiagnostic = captureEventAuditFailure( - () => eventAudit([safeNotification, secretVariant], acknowledgement, true), - 'secret-independent diagnostic', - ); - assert( - stableJson(secretDiagnostic) === stableJson(unknownDiagnostic), - 'event-audit diagnostic changed when only untrusted params changed', - ); - cases += 1; - const precedenceDiagnostic = captureEventAuditFailure( - () => eventAudit([safeNotification, unknownNotification, failureNotification], acknowledgement, true), - 'failure precedence', - ); - assert( - precedenceDiagnostic.invariant === 'failure-notification' - && stableJson(precedenceDiagnostic.rejectedMethodSha256) === stableJson([ - { sha256: sha256('warning'), count: 1 }, - ]), - 'failure notification did not retain precedence and hashed evidence', - ); - cases += 1; - const unknownItem = { - sequence: 2, - message: { method: 'item/completed', params: { item: { type: 'futureItem' } } }, - }; - const unknownItemDiagnostic = captureEventAuditFailure( - () => eventAudit([safeNotification, unknownItem], acknowledgement, true), - 'unknown item type', - ); - assert( - unknownItemDiagnostic.invariant === 'unknown-item-type' - && stableJson(unknownItemDiagnostic.rejectedItemTypeSha256) === stableJson([ - { sha256: sha256('futureItem'), count: 1 }, - ]), - 'unknown item diagnostic did not retain only hashed type evidence', - ); - cases += 1; - assert( - sanitizeEventAuditFailure({ ...unknownDiagnostic, notificationCount: '2' }) === null, - 'event-audit diagnostic accepted a non-integer count', - ); - cases += 1; - assert( - sanitizeEventAuditFailure({ ...unknownDiagnostic, params: { text: 'private' } }) === null, - 'event-audit diagnostic accepted an unexpected raw property', - ); - cases += 1; - const benignNotifications = [ - 'account/updated', - 'mcpServer/startupStatus/updated', - 'model/safetyBuffering/updated', - 'model/verification', - 'thread/name/updated', - 'thread/settings/updated', - 'turn/moderationMetadata', - ].map((method, index) => ({ sequence: index + 2, message: { method, params: {} } })); - assert( - eventAudit([safeNotification, ...benignNotifications], acknowledgement, true).unknownEventsObserved === 0, - 'schema-backed benign notification fixture failed', - ); - cases += 1; - const wrongScope = { - sequence: 2, - message: { - method: 'item/updated', - params: { threadId: 'wrong-thread', turnId: 'turn-1', item: { type: 'reasoning' } }, - }, - }; - expectThrow( - () => eventAudit([safeNotification, wrongScope], acknowledgement, true, 'thread-1', 'turn-1'), - 'cross-turn notification', - ); - const reroute = { sequence: 2, message: { method: 'model/rerouted', params: {} } }; - expectThrow( - () => eventAudit([safeNotification, reroute], acknowledgement, false), - 'model reroute', - ); - const mcpBoundary = { - count: 3, - namesSha256: 'a'.repeat(64), - configSha256: 'b'.repeat(64), - boundarySha256: 'c'.repeat(64), - }; - assertSameMcpBoundary(mcpBoundary, { ...mcpBoundary }, 'self-test'); - cases += 1; - expectThrow( - () => assertSameMcpBoundary(mcpBoundary, { ...mcpBoundary, configSha256: 'd'.repeat(64) }, 'self-test'), - 'MCP boundary drift', - ); - const simpleFailure = sanitizeTurnFailure({ - status: 'failed', - error: { codexErrorInfo: 'unauthorized' }, - }); - assert( - stableJson(simpleFailure) === stableJson({ - status: 'failed', code: 'unauthorized', httpStatusCode: null, willRetry: null, - }), - 'simple terminal failure was not normalized', - ); - cases += 1; - const httpFailure = sanitizeTurnFailure({ status: 'interrupted' }, { - message: { - method: 'error', - params: { - willRetry: false, - error: { - codexErrorInfo: { responseStreamDisconnected: { httpStatusCode: 429 } }, - }, - }, - }, - }); - assert( - stableJson(httpFailure) === stableJson({ - status: 'interrupted', - code: 'responseStreamDisconnected', - httpStatusCode: 429, - willRetry: false, - }), - 'HTTP terminal failure was not normalized', - ); - cases += 1; - const maliciousFailure = sanitizeTurnFailure({ - status: 'failed', - error: { - message: 'C:\\secret\\auth.json sk-fixture', - additionalDetails: 'file:///private/detail', - codexErrorInfo: { activeTurnNotSteerable: { turnKind: 'review' } }, - }, - }); - assert( - Object.keys(maliciousFailure).sort().join(',') === 'code,httpStatusCode,status,willRetry' - && !JSON.stringify(maliciousFailure).includes('secret'), - 'terminal failure retained untrusted text', - ); - cases += 1; - assert(sanitizeTurnFailure({ status: 'completed' }).status === null, 'completed turn was not safely normalized'); - cases += 1; - assert(sanitizeTurnFailure({ status: 'inProgress' }).status === null, 'in-progress turn was not safely normalized'); - cases += 1; - assert(sanitizeTurnFailure({ - status: 'failed', error: { codexErrorInfo: 'futureUnknownCode' }, - }).code === null, 'unknown terminal error code was not safely normalized'); - cases += 1; - assert(sanitizeTurnFailure({ - status: 'failed', - error: { - codexErrorInfo: { - responseStreamDisconnected: { httpStatusCode: 429 }, - httpConnectionFailed: { httpStatusCode: 503 }, - }, - }, - }).code === null, 'multi-key terminal error was not safely normalized'); - cases += 1; - assert(sanitizeTurnFailure({ - status: 'failed', - error: { - codexErrorInfo: { httpConnectionFailed: { httpStatusCode: 70_000 } }, - }, - }).code === null, 'invalid terminal HTTP status was not safely normalized'); - cases += 1; - const disagreeingFailure = sanitizeTurnFailure({ - status: 'failed', - error: { codexErrorInfo: 'unauthorized' }, - }, { - message: { - method: 'error', - params: { error: { codexErrorInfo: 'usageLimitExceeded' }, willRetry: false }, - }, - }); - assert( - disagreeingFailure.code === 'unauthorized' && disagreeingFailure.willRetry === false, - 'final turn was not authoritative over a disagreeing notification', - ); - cases += 1; - const malformedFailure = sanitizeTurnFailure(null, { - message: { method: 'futureError', params: { willRetry: 'yes', error: { codexErrorInfo: 7 } } }, - }); - assert( - stableJson(malformedFailure) === stableJson({ - status: null, code: null, httpStatusCode: null, willRetry: null, - }), - 'malformed terminal failure was not totally normalized', - ); - cases += 1; - const primaryFixture = new Error('primary'); - const closeFixture = new Error('close'); - assert( - selectLifecycleFailure(primaryFixture, closeFixture) === primaryFixture, - 'close failure replaced the primary lifecycle failure', - ); - cases += 1; - const requiredArgs = [ - '--codex-exe', 'C:\\codex.exe', - '--expected-head', 'a'.repeat(40), - '--hive-mind-cli', 'C:\\hive-mind.exe', - '--model', 'fixture-model', - '--receipt-dir', 'C:\\receipt', - '--windows-powershell', 'C:\\powershell.exe', - '--workspace', 'C:\\workspace', - ]; - assert(parseArgs([...requiredArgs, '--setup-only'])['setup-only'] === true, 'setup-only fixture failed'); - cases += 1; - expectThrow( - () => parseArgs([ - ...requiredArgs, - '--setup-only', - '--execute-paid', - '--marker', 'fixture', - '--ack', PAID_ACK, - ]), - 'setup-only paid conflict', - ); - const officialProviderArguments = buildInvocationArguments({ - mcpServerNames: [], - catalogPath: 'C:\\models.json', - providerBaseUrl: null, - includeDeny: false, - hookDisables: [], - hookPins: [], - }); - const officialProviderOverrides = officialProviderArguments.filter((argument) => ( - argument.startsWith('model_provider=') || argument.startsWith('model_providers.') - )); - assert( - stableJson(officialProviderOverrides) === stableJson(['model_provider="openai"']), - 'official provider arguments redefined the reserved built-in OpenAI provider', - ); - cases += 1; - assert(cases === 54, `expected exactly 54 self-test cases, observed ${cases}`); - return { pass: true, paidCalls: 0, cases }; - } finally { - await rm(root, { recursive: true, force: true }); - } -} - -const flags = parseArgs(process.argv.slice(2)); -if (flags['self-test']) { - process.stdout.write(`${JSON.stringify(await runValidationSelfTest())}\n`); - process.exit(0); -} -const codexExecutable = resolve(flags['codex-exe']); -const hiveMindCli = resolve(flags['hive-mind-cli']); -const receiptDir = resolve(flags['receipt-dir']); -const windowsPowerShell = resolve(flags['windows-powershell']); -const workspace = resolve(flags.workspace); -const model = flags.model; -const expectedHead = flags['expected-head']; -const executePaid = flags['execute-paid'] === true; -const setupOnly = flags['setup-only'] === true; -const initialExecutableSha256 = existsSync(codexExecutable) ? sha256File(codexExecutable) : null; -const initialScriptSha256 = sha256File(SCRIPT_PATH); -const report = { - schemaVersion: 1, - kind: 'codex-tool-denial-and-official-auth', - pass: false, - paidCalls: 0, - proof: { executed: false, paidCalls: 0, pass: null }, - source: { - expectedHead, - observedHead: null, - tree: null, - scriptBlob: null, - trackedClean: false, - unchanged: false, - }, - executable: { - version: null, - sha256: initialExecutableSha256, - unchanged: false, - }, - model: { - idSha256: sha256(model), - bundledCatalogSha256: null, - }, - modelCatalog: { - sealedSha256: null, - controlSha256: null, - }, - invocation: { - argumentsSha256: null, - configSha256: sha256(stableJson(CONFIG_OVERRIDES)), - threadParamsSha256: null, - turnParamsSha256: null, - mcpServerCount: null, - mcpServerNamesSha256: null, - }, - hooks: { - expectedCount: 5, - extraCount: null, - artifactsSha256: null, - cliSha256: null, - graphSha256: null, - denyHookSha256: null, - warnings: null, - errors: null, - allTrusted: false, - unchangedAfterPin: false, - artifactsUnchanged: false, - }, - mcpBoundary: { - count: null, - namesSha256: null, - initialSha256: null, - prePaidSha256: null, - postPaidSha256: null, - initialConfigSha256: null, - prePaidConfigSha256: null, - postPaidConfigSha256: null, - unchanged: false, - }, - sealed: null, - red: null, - green: null, - paidInvocation: { - attempted: false, - executed: false, - arguments: [], - argumentsSha256: null, - modelCatalogSha256: null, - hookGraphSha256: null, - markerSha256: null, - sessionIdSha256: null, - threadParamsSha256: null, - turnParamsSha256: null, - normalTextCompleted: false, - toolEventsObserved: 0, - unknownEventsObserved: 0, - }, - artifacts: { - scriptSha256: initialScriptSha256, - scriptUnchanged: false, - windowsPowerShellSha256: null, - windowsSystemDirectorySha256: null, - workspaceSha256: sha256(canonicalPath(workspace)), - reportPathHash: sha256(join(receiptDir, 'report.json').toLowerCase()), - tempRemoved: false, - }, - diagnostic: { - failureStage: null, - protocolCode: null, - completedStage: null, - modelCalls: 0, - turnStartCalls: 0, - closeClean: false, - }, - turnFailure: null, - eventAuditFailure: null, - error: null, -}; - -let rawPaidSessionId = null; -let markerMatched = !executePaid; -let tempRoot = null; -let initialSourceSnapshot = null; -let initialMcpBoundary = null; -const providers = []; -let receiptCreated = false; - -try { - assert(process.platform === 'win32', 'this proof is Windows-only'); - assert(isAbsolute(flags['codex-exe']), '--codex-exe must be absolute'); - assert(isAbsolute(flags['hive-mind-cli']), '--hive-mind-cli must be absolute'); - assert(isAbsolute(flags['receipt-dir']), '--receipt-dir must be absolute'); - assert(isAbsolute(flags['windows-powershell']), '--windows-powershell must be absolute'); - assert(isAbsolute(flags.workspace), '--workspace must be absolute'); - requireRegularFile(codexExecutable, 'Codex executable'); - requireRegularFile(hiveMindCli, 'hive-mind CLI'); - requireDirectory(workspace, 'workspace'); - initialSourceSnapshot = verifySourceSnapshot(expectedHead); - Object.assign(report.source, initialSourceSnapshot); - assert(!existsSync(receiptDir), '--receipt-dir must be fresh'); - assertNoExistingReparsePoint(dirname(receiptDir)); - await mkdir(receiptDir, { recursive: false }); - receiptCreated = true; - - const codexHome = resolve(process.env.CODEX_HOME || join(homedir(), '.codex')); - requireDirectory(codexHome, 'user Codex profile'); - requireRegularFile(join(codexHome, 'hooks.json'), 'user Codex hooks.json'); - const codexConfigPath = join(codexHome, 'config.toml'); - requireRegularFile(codexConfigPath, 'user Codex config.toml'); - const version = runChecked(codexExecutable, ['--version'], { - label: 'Codex version', - timeoutMs: 20_000, - }).stdout.trim(); - assert(/^codex(?:-cli)?\s/.test(version), 'Codex returned an unexpected version'); - report.executable.version = version; - - tempRoot = join(tmpdir(), `waggle-codex-denial-${randomBytes(12).toString('hex')}`); - await mkdir(tempRoot, { recursive: false }); - await mkdir(join(tempRoot, 'mind'), { recursive: true }); - await mkdir(join(tempRoot, 'process-temp'), { recursive: true }); - const env = buildChildEnvironment(tempRoot); - initialMcpBoundary = captureMcpBoundary(codexExecutable, env, codexConfigPath); - const mcpServerNames = readConfiguredMcpServerNames(codexExecutable, env); - assert( - initialMcpBoundary.namesSha256 === sha256(stableJson(mcpServerNames)), - 'initial MCP inventory changed while being captured', - ); - report.invocation.mcpServerCount = mcpServerNames.length; - report.invocation.mcpServerNamesSha256 = sha256(stableJson(mcpServerNames)); - report.mcpBoundary.count = initialMcpBoundary.count; - report.mcpBoundary.namesSha256 = initialMcpBoundary.namesSha256; - report.mcpBoundary.initialSha256 = initialMcpBoundary.boundarySha256; - report.mcpBoundary.initialConfigSha256 = initialMcpBoundary.configSha256; - const sentinelPath = join(tempRoot, 'owned-sentinel.png'); - const sentinelSha256 = await createSentinelPng(sentinelPath); - const { selected, bundledSha256 } = readBundledModel(codexExecutable, model); - report.model.bundledCatalogSha256 = bundledSha256; - const sealedCatalogPath = join(tempRoot, 'sealed-models.json'); - const controlCatalogPath = join(tempRoot, 'control-models.json'); - report.modelCatalog.sealedSha256 = await writeCatalog(sealedCatalogPath, mutateModel(selected, true)); - const controlModel = `waggle-control-${sha256(model).slice(0, 16)}`; - const controlCatalogModel = mutateModel(selected, false); - controlCatalogModel.slug = controlModel; - report.modelCatalog.controlSha256 = await writeCatalog(controlCatalogPath, controlCatalogModel); - - const trustedPowerShell = verifyWindowsPowerShell(windowsPowerShell); - report.artifacts.windowsPowerShellSha256 = trustedPowerShell.sha256; - report.artifacts.windowsSystemDirectorySha256 = trustedPowerShell.systemDirectorySha256; - const expectedWaggleHooks = buildExpectedWaggleHooks(trustedPowerShell.path, hiveMindCli); - const denial = buildDenyHookConfig(trustedPowerShell.path); - const replacements = [ - codexExecutable, - hiveMindCli, - receiptDir, - workspace, - codexHome, - tempRoot, - sentinelPath, - trustedPowerShell.path, - flags.marker, - ]; - const discoveryArgs = buildInvocationArguments({ - mcpServerNames, - catalogPath: sealedCatalogPath, - providerBaseUrl: 'http://127.0.0.1:9/v1', - includeDeny: true, - denyConfig: denial.config, - hookPins: [], - }); - const discovered = await listHooks( - codexExecutable, - discoveryArgs, - workspace, - env, - codexHome, - true, - false, - denial.commandSha256, - expectedWaggleHooks.commands, - true, - replacements, - ); - const allPins = discovered.expected.map(({ key, currentHash }) => ({ key, currentHash })); - const wagglePins = discovered.expected - .filter(({ eventName }) => eventName !== 'preToolUse') - .map(({ key, currentHash }) => ({ key, currentHash })); - const externalHookDisables = discovered.extras.map(({ key }) => key); - - const pinnedDiscoveryArgs = buildInvocationArguments({ - mcpServerNames, - catalogPath: sealedCatalogPath, - providerBaseUrl: 'http://127.0.0.1:9/v1', - includeDeny: true, - denyConfig: denial.config, - hookDisables: externalHookDisables, - hookPins: allPins, - }); - const pinned = await listHooks( - codexExecutable, - pinnedDiscoveryArgs, - workspace, - env, - codexHome, - true, - true, - denial.commandSha256, - expectedWaggleHooks.commands, - false, - replacements, - ); - assert(discovered.identitySha256 === pinned.identitySha256, 'hook graph identity changed while hash-pinning'); - assert(pinned.denyHookSha256 !== null, 'deny hook hash is missing'); - report.hooks = { - expectedCount: 5, - extraCount: pinned.extraCount, - disabledExtraCount: pinned.disabledExtraCount, - artifactsSha256: expectedWaggleHooks.artifactsSha256, - cliSha256: expectedWaggleHooks.cliSha256, - graphSha256: pinned.graphSha256, - denyHookSha256: pinned.denyHookSha256, - warnings: pinned.warnings, - errors: pinned.errors, - allTrusted: pinned.expected.every((hook) => hook.trustStatus === 'trusted'), - unchangedAfterPin: discovered.identitySha256 === pinned.identitySha256, - artifactsUnchanged: false, - }; - - if (!setupOnly) { - const sealedAck = `WAGGLE_SEALED_ACK_${randomBytes(24).toString('hex')}`; - const sealedProvider = await startLoopbackProvider('sealed', sealedAck, sentinelPath); - providers.push(sealedProvider); - const sealedArgs = buildInvocationArguments({ - mcpServerNames, - catalogPath: sealedCatalogPath, - providerBaseUrl: sealedProvider.baseUrl, - includeDeny: true, - denyConfig: denial.config, - hookDisables: externalHookDisables, - hookPins: allPins, - }); - const sealedRun = await runTurn({ - executable: codexExecutable, - args: sealedArgs, - workspace, - env, - replacements, - codexHome, - includeDeny: true, - expectedDenyCommandSha256: denial.commandSha256, - expectedWaggleCommands: expectedWaggleHooks.commands, - expectedHookGraphSha256: pinned.graphSha256, - lane: 'sealed', - model, - prompt: `Return exactly ${sealedAck} and nothing else. Do not call tools.`, - expectedAcknowledgement: sealedAck, - strictAllowlist: true, - }); - await sealedProvider.close(); - providers.splice(providers.indexOf(sealedProvider), 1); - assert(sealedProvider.records.length === 1, `sealed provider saw ${sealedProvider.records.length} requests`); - const sealedRequest = sealedProvider.records[0]; - assert(sealedRequest.topLevelToolsIsArray, 'sealed provider request omitted the top-level tools array'); - assert( - sealedRequest.toolNames.length === 0, - `sealed provider request exposed top-level tools: ${JSON.stringify(sealedRequest.toolNames)}`, - ); - assert(sealedRequest.additionalToolCount === 0, 'sealed provider request exposed input additional_tools'); - report.sealed = { - topLevelToolCount: sealedRequest.toolNames.length, - additionalToolCount: sealedRequest.additionalToolCount, - emptySchema: true, - normalTextCompleted: sealedRun.events.normalTextCompleted, - providerRequestSha256: sealedRequest.sha256, - }; - - const controlAck = `WAGGLE_CONTROL_ACK_${randomBytes(24).toString('hex')}`; - const redProvider = await startLoopbackProvider('red', controlAck, sentinelPath); - providers.push(redProvider); - const redArgs = buildInvocationArguments({ - mcpServerNames, - catalogPath: controlCatalogPath, - providerBaseUrl: redProvider.baseUrl, - includeDeny: false, - denyConfig: denial.config, - hookDisables: externalHookDisables, - hookPins: wagglePins, - }); - const redDiscovery = await listHooks( - codexExecutable, - redArgs, - workspace, - env, - codexHome, - false, - true, - null, - expectedWaggleHooks.commands, - false, - replacements, - ); - const redRun = await runTurn({ - executable: codexExecutable, - args: redArgs, - workspace, - env, - replacements, - codexHome, - includeDeny: false, - expectedDenyCommandSha256: null, - expectedWaggleCommands: expectedWaggleHooks.commands, - expectedHookGraphSha256: redDiscovery.graphSha256, - useDefaultEnvironmentForControl: true, - lane: 'red', - model: controlModel, - prompt: 'Follow the provider tool call, then return only the provider acknowledgement.', - expectedAcknowledgement: controlAck, - strictAllowlist: false, - }); - await redProvider.close(); - providers.splice(providers.indexOf(redProvider), 1); - assert(redProvider.records.length === 2, `RED provider saw ${redProvider.records.length} requests`); - const redTools = redProvider.records[0].toolNames; - assert( - JSON.stringify(redTools) === JSON.stringify(['view_image']), - `RED control catalog did not expose only view_image: ${JSON.stringify(redTools)}`, - ); - const redSensitiveDataObserved = redProvider.records[1]?.containsImage === true; - const redReadObserved = redRun.events.imageItemCount > 0 || redSensitiveDataObserved; - const redToolLoopObserved = redProvider.records.length === 2 && redRun.events.normalTextCompleted; - report.red = { - toolNames: redTools, - recordCount: redProvider.records.length, - toolLoopObserved: redToolLoopObserved, - readObserved: redReadObserved, - sensitiveDataObserved: redSensitiveDataObserved, - imageItemCount: redRun.events.imageItemCount, - normalTextCompleted: redRun.events.normalTextCompleted, - sentinelSha256, - }; - assert(redToolLoopObserved, 'RED control did not prove the provider-routed view_image tool loop'); - - const greenProvider = await startLoopbackProvider('green', controlAck, sentinelPath); - providers.push(greenProvider); - const greenArgs = buildInvocationArguments({ - mcpServerNames, - catalogPath: controlCatalogPath, - providerBaseUrl: greenProvider.baseUrl, - includeDeny: true, - denyConfig: denial.config, - hookDisables: externalHookDisables, - hookPins: allPins, - }); - const greenRun = await runTurn({ - executable: codexExecutable, - args: greenArgs, - workspace, - env, - replacements, - codexHome, - includeDeny: true, - expectedDenyCommandSha256: denial.commandSha256, - expectedWaggleCommands: expectedWaggleHooks.commands, - expectedHookGraphSha256: pinned.graphSha256, - useDefaultEnvironmentForControl: true, - lane: 'green', - model: controlModel, - prompt: 'Follow the provider tool call, then return only the provider acknowledgement.', - expectedAcknowledgement: controlAck, - strictAllowlist: false, - }); - await greenProvider.close(); - providers.splice(providers.indexOf(greenProvider), 1); - assert(greenProvider.records.length === 2, `GREEN provider saw ${greenProvider.records.length} requests`); - assert( - JSON.stringify(greenProvider.records[0].toolNames) === JSON.stringify(['view_image']), - `GREEN control catalog did not expose only view_image: ${JSON.stringify(greenProvider.records[0].toolNames)}`, - ); - const greenImageObserved = greenProvider.records.slice(1).some((record) => record.containsImage); - const deniedBeforeRead = greenRun.events.denyHookCount === 1 - && !greenImageObserved - && ( - greenRun.events.firstImageSequence === null - || greenRun.events.firstDenySequence < greenRun.events.firstImageSequence - ); - assert(deniedBeforeRead, 'GREEN hook did not deny view_image before sentinel access'); - report.green = { - deniedBeforeRead, - sensitiveDataObserved: false, - normalTextCompleted: greenRun.events.normalTextCompleted, - denialReasonSha256: sha256(DENIAL_REASON), - denyHookReceipts: greenRun.events.denyHookCount, - }; - report.invocation.argumentsSha256 = sha256(stableJson(normalizedArguments(greenArgs, [ - [sealedCatalogPath, ''], - [controlCatalogPath, ''], - [greenProvider.baseUrl, ''], - [denial.command, ''], - ]))); - report.invocation.threadParamsSha256 = sealedRun.threadParamsSha256; - report.invocation.turnParamsSha256 = sealedRun.turnParamsSha256; - report.proof = { executed: true, paidCalls: 0, pass: true }; - } - - if (setupOnly) { - const setupArgs = buildInvocationArguments({ - mcpServerNames, - catalogPath: sealedCatalogPath, - providerBaseUrl: null, - includeDeny: true, - denyConfig: denial.config, - hookDisables: externalHookDisables, - hookPins: allPins, - }); - let preSetupMcpBoundary = null; - const setupRun = await runTurn({ - executable: codexExecutable, - args: setupArgs, - workspace, - env, - replacements, - codexHome, - includeDeny: true, - expectedDenyCommandSha256: denial.commandSha256, - expectedWaggleCommands: expectedWaggleHooks.commands, - expectedHookGraphSha256: pinned.graphSha256, - beforeTurn: async () => { - preSetupMcpBoundary = captureMcpBoundary(codexExecutable, env, codexConfigPath); - assertSameMcpBoundary(initialMcpBoundary, preSetupMcpBoundary, 'before setup-only thread/start'); - report.mcpBoundary.prePaidSha256 = preSetupMcpBoundary.boundarySha256; - report.mcpBoundary.prePaidConfigSha256 = preSetupMcpBoundary.configSha256; - }, - lane: 'setup', - model, - setupOnly: true, - strictAllowlist: true, - }); - assert(preSetupMcpBoundary !== null, 'setup-only MCP/config boundary was not captured'); - const postSetupMcpBoundary = captureMcpBoundary(codexExecutable, env, codexConfigPath); - assertSameMcpBoundary(initialMcpBoundary, postSetupMcpBoundary, 'after setup-only thread/start'); - report.mcpBoundary.postPaidSha256 = postSetupMcpBoundary.boundarySha256; - report.mcpBoundary.postPaidConfigSha256 = postSetupMcpBoundary.configSha256; - report.mcpBoundary.unchanged = true; - report.diagnostic = setupRun.diagnostic; - report.invocation.argumentsSha256 = sha256(stableJson(normalizedArguments(setupArgs, [ - [sealedCatalogPath, ''], - [denial.command, ''], - ]))); - report.invocation.threadParamsSha256 = setupRun.threadParamsSha256; - report.invocation.turnParamsSha256 = null; - } else if (executePaid) { - const captureMindRoot = process.env.HIVE_MIND_DATA_DIR; - const captureWorkspaceId = process.env.WAGGLE_WORKSPACE_ID; - assert( - typeof captureMindRoot === 'string' && isAbsolute(captureMindRoot), - 'paid lane requires an absolute HIVE_MIND_DATA_DIR from the parent harness', - ); - requireDirectory(captureMindRoot, 'paid capture mind root'); - assert( - typeof captureWorkspaceId === 'string' - && /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(captureWorkspaceId), - 'paid lane requires a safe WAGGLE_WORKSPACE_ID from the parent harness', - ); - const paidEnv = { - ...env, - HIVE_MIND_DATA_DIR: captureMindRoot, - WAGGLE_WORKSPACE_ID: captureWorkspaceId, - }; - const paidArgs = buildInvocationArguments({ - mcpServerNames, - catalogPath: sealedCatalogPath, - providerBaseUrl: null, - includeDeny: true, - denyConfig: denial.config, - hookDisables: externalHookDisables, - hookPins: allPins, - }); - report.paidCalls = 1; - report.paidInvocation.attempted = true; - let prePaidMcpBoundary = null; - const paidRun = await runTurn({ - executable: codexExecutable, - args: paidArgs, - workspace, - env: paidEnv, - replacements, - codexHome, - includeDeny: true, - expectedDenyCommandSha256: denial.commandSha256, - expectedWaggleCommands: expectedWaggleHooks.commands, - expectedHookGraphSha256: pinned.graphSha256, - beforeTurn: async () => { - prePaidMcpBoundary = captureMcpBoundary(codexExecutable, paidEnv, codexConfigPath); - assertSameMcpBoundary(initialMcpBoundary, prePaidMcpBoundary, 'before the paid turn'); - report.mcpBoundary.prePaidSha256 = prePaidMcpBoundary.boundarySha256; - report.mcpBoundary.prePaidConfigSha256 = prePaidMcpBoundary.configSha256; - }, - lane: 'paid', - model, - prompt: `Return exactly ${flags.marker} and nothing else. Do not call tools.`, - expectedAcknowledgement: flags.marker, - strictAllowlist: true, - }); - report.diagnostic = paidRun.diagnostic; - assert(prePaidMcpBoundary !== null, 'paid MCP/config boundary was not captured'); - const postPaidMcpBoundary = captureMcpBoundary(codexExecutable, paidEnv, codexConfigPath); - assertSameMcpBoundary(initialMcpBoundary, postPaidMcpBoundary, 'after the paid turn'); - report.mcpBoundary.postPaidSha256 = postPaidMcpBoundary.boundarySha256; - report.mcpBoundary.postPaidConfigSha256 = postPaidMcpBoundary.configSha256; - report.mcpBoundary.unchanged = true; - rawPaidSessionId = paidRun.threadId; - markerMatched = paidRun.events.normalTextCompleted; - const paidNormalizedArguments = normalizedArguments(paidArgs, [ - [sealedCatalogPath, ''], - [denial.command, ''], - ]); - report.paidInvocation = { - attempted: true, - executed: true, - arguments: paidNormalizedArguments, - argumentsSha256: sha256(stableJson(paidNormalizedArguments)), - modelCatalogSha256: report.modelCatalog.sealedSha256, - hookGraphSha256: paidRun.hooks.graphSha256, - markerSha256: sha256(flags.marker), - sessionIdSha256: paidRun.threadIdSha256, - threadParamsSha256: paidRun.threadParamsSha256, - turnParamsSha256: paidRun.turnParamsSha256, - normalTextCompleted: paidRun.events.normalTextCompleted, - toolEventsObserved: paidRun.events.toolEventsObserved, - unknownEventsObserved: paidRun.events.unknownEventsObserved, - }; - assert(markerMatched, 'paid Codex lane did not return the exact marker'); - assert(report.paidInvocation.toolEventsObserved === 0, 'paid Codex lane emitted a tool event'); - assert(report.paidInvocation.unknownEventsObserved === 0, 'paid Codex lane emitted an unknown event'); - assert( - report.paidInvocation.threadParamsSha256 === report.invocation.threadParamsSha256, - 'paid Codex thread parameters did not match the sealed proof', - ); - assert( - report.paidInvocation.turnParamsSha256 === report.invocation.turnParamsSha256, - 'paid Codex turn parameters did not match the sealed proof', - ); - } - - assert(sha256File(codexExecutable) === initialExecutableSha256, 'Codex executable changed during proof'); - report.executable.unchanged = true; - assert(sha256File(SCRIPT_PATH) === initialScriptSha256, 'Codex tool-denial helper changed during proof'); - report.artifacts.scriptUnchanged = true; - const finalExpectedWaggleHooks = buildExpectedWaggleHooks(trustedPowerShell.path, hiveMindCli); - assert( - finalExpectedWaggleHooks.artifactsSha256 === expectedWaggleHooks.artifactsSha256 - && finalExpectedWaggleHooks.cliSha256 === expectedWaggleHooks.cliSha256, - 'packaged Codex hook or hive-mind CLI artifact changed during proof', - ); - report.hooks.artifactsUnchanged = true; - const finalSourceSnapshot = verifySourceSnapshot(expectedHead); - assert( - finalSourceSnapshot.observedHead === initialSourceSnapshot.observedHead - && finalSourceSnapshot.tree === initialSourceSnapshot.tree - && finalSourceSnapshot.scriptBlob === initialSourceSnapshot.scriptBlob, - 'repository source snapshot changed during proof', - ); - report.source.unchanged = true; - report.pass = proofStateSatisfied(report.proof, setupOnly) - && report.source.unchanged - && report.artifacts.scriptUnchanged - && report.hooks.artifactsUnchanged - && report.eventAuditFailure === null - && (!setupOnly || ( - report.diagnostic.failureStage === null - && report.diagnostic.protocolCode === null - && report.diagnostic.completedStage === 'thread-start' - && report.diagnostic.modelCalls === 0 - && report.diagnostic.turnStartCalls === 0 - && report.diagnostic.closeClean - && report.mcpBoundary.unchanged - )) - && (!executePaid || ( - report.paidCalls === 1 - && report.mcpBoundary.unchanged - && report.paidInvocation.normalTextCompleted - && report.paidInvocation.toolEventsObserved === 0 - && report.paidInvocation.unknownEventsObserved === 0 - )); -} catch (error) { - report.turnFailure = error?.turnFailure ?? null; - report.eventAuditFailure = sanitizeEventAuditFailure(error?.eventAuditFailure); - if (error?.canaryDiagnostic) { - report.diagnostic = { - failureStage: sanitizeCanaryStage(error.canaryDiagnostic.failureStage), - protocolCode: sanitizeProtocolCode(error.canaryDiagnostic.protocolCode), - completedStage: sanitizeCanaryStage(error.canaryDiagnostic.completedStage), - modelCalls: Number.isInteger(error.canaryDiagnostic.modelCalls) - && error.canaryDiagnostic.modelCalls >= 0 - ? error.canaryDiagnostic.modelCalls - : 0, - turnStartCalls: Number.isInteger(error.canaryDiagnostic.turnStartCalls) - && error.canaryDiagnostic.turnStartCalls >= 0 - ? error.canaryDiagnostic.turnStartCalls - : 0, - closeClean: error.canaryDiagnostic.closeClean === true, - }; - } - report.error = safeError(error, [ - codexExecutable, - hiveMindCli, - receiptDir, - workspace, - windowsPowerShell, - tempRoot, - flags.marker, - ]); -} - -for (const provider of providers.splice(0)) { - try { await provider.close(); } catch { /* best-effort loopback cleanup */ } -} -if (tempRoot !== null) { - try { - await rm(tempRoot, { recursive: true, force: true }); - report.artifacts.tempRemoved = !existsSync(tempRoot); - } catch (error) { - report.error = report.error ?? safeError(error, [tempRoot]); - report.pass = false; - } -} -if (!report.executable.unchanged && existsSync(codexExecutable)) { - report.executable.unchanged = sha256File(codexExecutable) === initialExecutableSha256; -} -if (!report.pass && report.error === null) report.error = 'proof did not satisfy all invariants'; - -let reportPath = null; -let reportSha256 = null; -if (receiptCreated) { - reportPath = join(receiptDir, 'report.json'); - await writeFile(reportPath, `${JSON.stringify(report, null, 2)}\n`, 'utf8'); - const posture = await stat(reportPath); - assert(posture.isFile(), 'report.json is not a regular file'); - reportSha256 = sha256File(reportPath); -} - -const summary = { - pass: report.pass, - paidCalls: report.paidCalls, - markerMatched, - reportPath, - reportSha256, - sessionId: executePaid ? rawPaidSessionId : null, - turnFailure: report.turnFailure, - failureStage: report.diagnostic.failureStage, - protocolCode: report.diagnostic.protocolCode, - completedStage: report.diagnostic.completedStage, - modelCalls: report.diagnostic.modelCalls, - turnStartCalls: report.diagnostic.turnStartCalls, - closeClean: report.diagnostic.closeClean, -}; -process.stdout.write(`${JSON.stringify(summary)}\n`); -if (!report.pass) process.exitCode = 1; diff --git a/tests/e2e/launcher-real-hook-lifecycle.spec.ts b/tests/e2e/launcher-real-hook-lifecycle.spec.ts deleted file mode 100644 index 2c981f4..0000000 --- a/tests/e2e/launcher-real-hook-lifecycle.spec.ts +++ /dev/null @@ -1,221 +0,0 @@ -import { expect, test } from '@playwright/test'; -import fs from 'node:fs'; -import path from 'node:path'; - -type HookEnvelope = { - ok: boolean; - action: 'install' | 'verify' | 'uninstall'; - packageName: string; - stdout: string; - stderr: string; - code: number; - error?: string; -}; - -type HookToolCase = { - id: 'claude-code' | 'claude-desktop' | 'codex' | 'codex-desktop' | 'cursor' | 'hermes' | 'openclaw'; - packageName: string; - configPath: string; - pointerPath: string; - cleanupDirs: string[]; - precreateConfig?: string; - managedHookDir?: string; -}; - -const HOOK_TOOL_CASES: HookToolCase[] = [ - { - id: 'claude-code', - packageName: '@waggle/hive-mind-hooks-claude-code', - configPath: path.join('.claude', 'settings.json'), - pointerPath: path.join('.claude', 'hive-mind-install.json'), - cleanupDirs: ['.claude'], - precreateConfig: '{}\n', - }, - { - id: 'claude-desktop', - packageName: '@waggle/hive-mind-hooks-claude-desktop', - configPath: path.join('AppData', 'Roaming', 'Claude', 'claude_desktop_config.json'), - pointerPath: path.join('.waggle', 'claude-desktop', 'hive-mind-install.json'), - cleanupDirs: [path.join('AppData', 'Roaming', 'Claude'), path.join('.waggle', 'claude-desktop')], - precreateConfig: '{}\n', - }, - { - id: 'codex', - packageName: '@waggle/hive-mind-hooks-codex', - configPath: path.join('.codex', 'hooks.json'), - pointerPath: path.join('.codex', 'hive-mind-install.json'), - cleanupDirs: ['.codex'], - }, - { - id: 'codex-desktop', - packageName: '@waggle/hive-mind-hooks-codex-desktop', - configPath: path.join('.codex', 'hooks.json'), - pointerPath: path.join('.codex', 'hive-mind-install.json'), - cleanupDirs: ['.codex'], - }, - { - id: 'cursor', - packageName: '@waggle/hive-mind-hooks-cursor', - configPath: path.join('.cursor', 'hooks.json'), - pointerPath: path.join('.cursor', 'hive-mind-install.json'), - cleanupDirs: ['.cursor'], - }, - { - id: 'hermes', - packageName: '@waggle/hive-mind-hooks-hermes', - configPath: path.join('.hermes', 'config.yaml'), - pointerPath: path.join('.hermes', 'hive-mind-install.json'), - cleanupDirs: ['.hermes'], - }, - { - id: 'openclaw', - packageName: '@waggle/hive-mind-hooks-openclaw', - configPath: path.join('.openclaw', 'openclaw.json'), - pointerPath: path.join('.openclaw', 'hive-mind-install.json'), - cleanupDirs: ['.openclaw'], - managedHookDir: path.join('.openclaw', 'hooks', 'hive-mind'), - }, -]; - -function selectRequestedHookTools(tools: readonly HookToolCase[]): HookToolCase[] { - const raw = process.env.WAGGLE_E2E_HOST_IDS; - if (raw === undefined) return [...tools]; - - const rawIds = raw.split(','); - if (rawIds.some(id => id.trim().length === 0)) { - throw new Error('Invalid WAGGLE_E2E_HOST_IDS: empty host ID.'); - } - const requestedIds = rawIds.map(id => id.trim()); - const duplicateIds = requestedIds.filter( - (id, index) => requestedIds.indexOf(id) !== index, - ); - if (duplicateIds.length > 0) { - throw new Error(`Duplicate WAGGLE_E2E_HOST_IDS: ${[...new Set(duplicateIds)].join(', ')}`); - } - const availableIds = new Set(tools.map(tool => tool.id)); - const unknownIds = requestedIds.filter( - id => !availableIds.has(id as HookToolCase['id']), - ); - if (unknownIds.length > 0) { - throw new Error(`Unknown WAGGLE_E2E_HOST_IDS: ${unknownIds.join(', ')}`); - } - const requested = new Set(requestedIds); - return tools.filter(tool => requested.has(tool.id)); -} - -function normalized(value: string): string { - return path.resolve(value).toLowerCase(); -} - -function inside(root: string, relativePath: string): string { - const candidate = path.resolve(root, relativePath); - const relative = path.relative(path.resolve(root), candidate); - if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) { - throw new Error(`Refusing path outside isolated hook profile: ${candidate}`); - } - return candidate; -} - -function safeRemove(root: string, relativePath: string): void { - fs.rmSync(inside(root, relativePath), { recursive: true, force: true }); -} - -function assertIsolatedWindowsProfile(hookHome: string): void { - const guardedRoot = process.env.WAGGLE_E2E_TEMP_ROOT; - expect(guardedRoot, 'guarded runner must issue WAGGLE_E2E_TEMP_ROOT').toBeTruthy(); - const relativeToRoot = path.relative(path.resolve(guardedRoot!), path.resolve(hookHome)); - expect(relativeToRoot, 'hook profile must be a child of the guarded temp root').not.toMatch(/^\.\.|^[\\/]/); - expect(relativeToRoot, 'hook profile must not be the guarded temp root itself').not.toBe(''); - expect(normalized(process.env.USERPROFILE ?? ''), 'isolated USERPROFILE').toBe(normalized(hookHome)); - expect(normalized(process.env.HOME ?? ''), 'isolated HOME').toBe(normalized(hookHome)); - expect(normalized(process.env.APPDATA ?? ''), 'isolated APPDATA').toBe( - normalized(path.join(hookHome, 'AppData', 'Roaming')), - ); - expect(normalized(process.env.LOCALAPPDATA ?? ''), 'isolated LOCALAPPDATA').toBe( - normalized(path.join(hookHome, 'AppData', 'Local')), - ); -} - -test.describe('Launcher real Windows hook lifecycle', () => { - test('runs requested packaged hook routes with server-owned CLI wiring and reversible temp-profile cleanup', async ({ request }, testInfo) => { - test.setTimeout(240_000); - test.skip(process.platform !== 'win32', 'This real-host safety lane is Windows-specific.'); - test.skip( - process.env.WAGGLE_E2E_REAL_HOOKS !== '1' || !process.env.WAGGLE_E2E_HOOK_HOME, - 'Use scripts/test-windows-external-agents.ps1 to provide a throwaway Windows profile.', - ); - - const hookToolCases = selectRequestedHookTools(HOOK_TOOL_CASES); - const hookHome = path.resolve(process.env.WAGGLE_E2E_HOOK_HOME!); - assertIsolatedWindowsProfile(hookHome); - fs.mkdirSync(hookHome, { recursive: true }); - const completed: Array<{ id: HookToolCase['id']; actions: string[]; packagedCli: string }> = []; - - const postHook = async (tool: HookToolCase, action: HookEnvelope['action']) => { - const response = await request.post('/api/tools/hooks', { - data: { id: tool.id, action }, - }); - expect(response.status(), await response.text()).toBe(200); - const body = await response.json() as HookEnvelope; - expect(body, body.error ?? body.stderr).toMatchObject({ - ok: true, - action, - packageName: tool.packageName, - code: 0, - }); - return body; - }; - - try { - for (const tool of hookToolCases) { - const configPath = inside(hookHome, tool.configPath); - const pointerPath = inside(hookHome, tool.pointerPath); - const managedHookDir = tool.managedHookDir ? inside(hookHome, tool.managedHookDir) : null; - - await test.step(`${tool.id} hook install -> verify -> uninstall`, async () => { - for (const cleanupDir of tool.cleanupDirs) safeRemove(hookHome, cleanupDir); - if (tool.precreateConfig !== undefined) { - fs.mkdirSync(path.dirname(configPath), { recursive: true }); - fs.writeFileSync(configPath, tool.precreateConfig, 'utf8'); - } - - const install = await postHook(tool, 'install'); - expect(install.stdout).toContain('install'); - expect(fs.existsSync(configPath)).toBe(true); - expect(fs.existsSync(pointerPath)).toBe(true); - if (managedHookDir) expect(fs.existsSync(managedHookDir)).toBe(true); - - const pointer = JSON.parse(fs.readFileSync(pointerPath, 'utf8')) as { cli_path?: unknown }; - expect(pointer.cli_path, 'route pins the packaged hive-mind CLI').toEqual(expect.any(String)); - const packagedCli = path.resolve(String(pointer.cli_path)); - expect(fs.existsSync(packagedCli), `packaged CLI exists: ${packagedCli}`).toBe(true); - expect(packagedCli.replace(/\\/g, '/')).toMatch(/hive-mind-cli\/dist\/index\.js$/); - - const verify = await postHook(tool, 'verify'); - expect(verify.stdout).toContain('All checks passed.'); - - const uninstall = await postHook(tool, 'uninstall'); - expect(uninstall.stdout).toContain('uninstall'); - expect(fs.existsSync(pointerPath)).toBe(false); - if (tool.precreateConfig !== undefined) { - expect(fs.readFileSync(configPath, 'utf8')).toBe(tool.precreateConfig); - } else { - expect(fs.existsSync(configPath)).toBe(false); - } - if (managedHookDir) expect(fs.existsSync(managedHookDir)).toBe(false); - completed.push({ id: tool.id, actions: ['install', 'verify', 'uninstall'], packagedCli }); - }); - } - - await testInfo.attach('windows-hook-route-summary', { - body: Buffer.from(JSON.stringify({ hookHome, completed }, null, 2)), - contentType: 'application/json', - }); - expect(completed.map(item => item.id)).toEqual(hookToolCases.map(tool => tool.id)); - } finally { - for (const tool of hookToolCases) { - for (const cleanupDir of tool.cleanupDirs) safeRemove(hookHome, cleanupDir); - } - } - }); -}); diff --git a/tests/e2e/launcher-real-tool-lifecycle.spec.ts b/tests/e2e/launcher-real-tool-lifecycle.spec.ts deleted file mode 100644 index ca1e3f8..0000000 --- a/tests/e2e/launcher-real-tool-lifecycle.spec.ts +++ /dev/null @@ -1,357 +0,0 @@ -import { expect, test, type APIRequestContext } from '@playwright/test'; -import { SUPPORTED_TOOLS } from '@waggle/shared'; -import { randomUUID } from 'node:crypto'; -import path from 'node:path'; - -const SKIP_PARAMS = 'skipOnboarding=true&skipBoot=true&skipBriefing=true&tier=power'; -type ToolId = typeof SUPPORTED_TOOLS[number]; -const SAFE_VERSION_ARGS: Partial> = { - 'claude-code': ['--version'], - codex: ['--version'], - hermes: ['--version'], - openclaw: ['--version'], -}; -const SECRET_ENV_NAMES = [ - 'ANTHROPIC_API_KEY', - 'ANTHROPIC_AUTH_TOKEN', - 'CLAUDE_CODE_OAUTH_TOKEN', - 'OPENAI_API_KEY', - 'OPENAI_ACCESS_TOKEN', - 'OPENROUTER_API_KEY', - 'GOOGLE_API_KEY', - 'GEMINI_API_KEY', - 'GROQ_API_KEY', - 'XAI_API_KEY', - 'MISTRAL_API_KEY', - 'COHERE_API_KEY', - 'DEEPSEEK_API_KEY', - 'AZURE_OPENAI_API_KEY', - 'AWS_ACCESS_KEY_ID', - 'AWS_SECRET_ACCESS_KEY', - 'AWS_SESSION_TOKEN', - 'AWS_PROFILE', - 'AWS_CONFIG_FILE', - 'AWS_SHARED_CREDENTIALS_FILE', - 'GOOGLE_APPLICATION_CREDENTIALS', - 'CLOUDSDK_CONFIG', - 'AZURE_CONFIG_DIR', - 'KUBECONFIG', - 'DOCKER_CONFIG', - 'DOCKER_HOST', - 'GITHUB_TOKEN', - 'GH_TOKEN', - 'STRIPE_SECRET_KEY', - 'DATABASE_URL', - 'NPM_TOKEN', - 'HF_TOKEN', - 'HUGGING_FACE_HUB_TOKEN', - 'RENDER_API_KEY', - 'SSH_AUTH_SOCK', - 'GIT_ASKPASS', - 'SSH_ASKPASS', - 'GIT_SSH_COMMAND', - 'HTTP_PROXY', - 'HTTPS_PROXY', - 'ALL_PROXY', - 'NODE_OPTIONS', -] as const; - -function selectRequestedToolIds(tools: readonly ToolId[]): ToolId[] { - const raw = process.env.WAGGLE_E2E_HOST_IDS; - if (raw === undefined) return [...tools]; - - const rawIds = raw.split(','); - if (rawIds.some(id => id.trim().length === 0)) { - throw new Error('Invalid WAGGLE_E2E_HOST_IDS: empty host ID.'); - } - const requestedIds = rawIds.map(id => id.trim()); - const duplicateIds = requestedIds.filter( - (id, index) => requestedIds.indexOf(id) !== index, - ); - if (duplicateIds.length > 0) { - throw new Error(`Duplicate WAGGLE_E2E_HOST_IDS: ${[...new Set(duplicateIds)].join(', ')}`); - } - const unknownIds = requestedIds.filter(id => !tools.includes(id as ToolId)); - if (unknownIds.length > 0) { - throw new Error(`Unknown WAGGLE_E2E_HOST_IDS: ${unknownIds.join(', ')}`); - } - const requested = new Set(requestedIds); - return tools.filter(tool => requested.has(tool)); -} - -type DetectedTool = { - id: string; - displayName: string; - installed: boolean; - installedPath: string | null; - launchable?: boolean; - builtin?: boolean; - capabilities?: { - interactiveLaunch: boolean; - headlessTask: boolean; - }; -}; - -type DetectionEnvelope = { - platform: string; - tools: DetectedTool[]; -}; - -type LaunchEnvelope = { - ok: boolean; - pid: number | null; - roomId?: string; - runId?: string; - error?: string; -}; - -type WorkspaceEnvelope = { - id?: string; - name?: string; - storageType?: string; -}; - -type RouteResult = { - id: string; - displayName: string; - status: 'unavailable' | 'safe-version-exit' | 'interactive-only-rejected'; - installedPath: string | null; - exitCode?: number | null; - output?: string; - route?: string; -}; - -function routeWithSkip(route: string): string { - const sep = route.includes('?') ? '&' : '?'; - return `${route}${sep}${SKIP_PARAMS}`; -} - -function assertTemporaryDataDir(): void { - const dataDir = process.env.WAGGLE_E2E_DATA_DIR; - const guardedRoot = process.env.WAGGLE_E2E_TEMP_ROOT; - expect(dataDir, 'WAGGLE_E2E_DATA_DIR must be explicitly isolated').toBeTruthy(); - expect(guardedRoot, 'guarded runner must issue WAGGLE_E2E_TEMP_ROOT').toBeTruthy(); - const relative = path.relative(path.resolve(guardedRoot!), path.resolve(dataDir!)); - expect(relative, 'E2E data dir must be a child of the guarded temp root').not.toMatch(/^\.\.|^[\\/]/); - expect(relative, 'E2E data dir must not be the guarded temp root itself').not.toBe(''); -} - -async function createManagedWorkspace(request: APIRequestContext): Promise { - const createResponse = await request.post('/api/workspaces', { - data: { - name: `Windows external-agent route ${randomUUID().slice(0, 8)}`, - group: 'external-agent-e2e', - icon: 'Terminal', - tone: 'technical', - storageType: 'virtual', - }, - }); - expect(createResponse.status(), await createResponse.text()).toBe(201); - const created = await createResponse.json() as WorkspaceEnvelope; - expect(created.id, 'managed workspace id from POST /api/workspaces').toMatch(/\S/); - - const persistedResponse = await request.get(`/api/workspaces/${encodeURIComponent(created.id!)}`); - expect(persistedResponse.status(), await persistedResponse.text()).toBe(200); - const persisted = await persistedResponse.json() as WorkspaceEnvelope; - expect(persisted).toMatchObject({ id: created.id, name: created.name }); - return { ...created, id: created.id! }; -} - -async function readObservedStream( - baseURL: string, - pid: number, -): Promise<{ lines: string[]; exitCode: number | null | undefined }> { - const controller = new AbortController(); - const timeout = setTimeout(() => controller.abort(), 30_000); - const lines: string[] = []; - let exitCode: number | null | undefined; - - try { - const response = await fetch(new URL(`/api/tools/stream?pid=${pid}`, baseURL), { - signal: controller.signal, - }); - expect(response.ok).toBe(true); - expect(response.body).toBeTruthy(); - - const reader = response.body!.getReader(); - const decoder = new TextDecoder(); - let buffer = ''; - - while (exitCode === undefined) { - const chunk = await reader.read(); - if (chunk.done) break; - buffer += decoder.decode(chunk.value, { stream: true }).replace(/\r\n/g, '\n'); - - let eventEnd = buffer.indexOf('\n\n'); - while (eventEnd >= 0) { - const rawEvent = buffer.slice(0, eventEnd); - buffer = buffer.slice(eventEnd + 2); - eventEnd = buffer.indexOf('\n\n'); - - let event = 'message'; - let data = ''; - for (const line of rawEvent.split('\n')) { - if (line.startsWith('event:')) event = line.slice('event:'.length).trim(); - if (line.startsWith('data:')) data += line.slice('data:'.length).trim(); - } - - if (event === 'line') lines.push((JSON.parse(data) as { line: string }).line); - if (event === 'exit') { - exitCode = (JSON.parse(data) as { code: number | null }).code; - break; - } - } - } - } finally { - clearTimeout(timeout); - } - - return { lines, exitCode }; -} - -async function waitForProcessClear(request: APIRequestContext, pid: number): Promise { - await expect.poll(async () => { - const processesResponse = await request.get('/api/tools/processes'); - expect(processesResponse.ok()).toBe(true); - const body = await processesResponse.json() as { processes: Array<{ pid: number }> }; - return body.processes.some(process => process.pid === pid); - }, { timeout: 10_000 }).toBe(false); -} - -test.describe('Launcher real Windows supported-route lifecycle', () => { - test('covers requested built-in tools without credentials, unsafe GUI launch, or fabricated workspace ids', async ({ baseURL, page, request }, testInfo) => { - test.setTimeout(240_000); - test.skip(process.platform !== 'win32', 'This real-host route lane is Windows-specific.'); - test.skip( - process.env.WAGGLE_E2E_REAL_TOOLS !== '1', - 'Use scripts/test-windows-external-agents.ps1 to run the guarded real-tool lane.', - ); - assertTemporaryDataDir(); - expect( - process.env.WAGGLE_E2E_REUSE_EXISTING_SERVER, - 'guarded runner must forbid reuse of a pre-existing app server', - ).toBe('0'); - expect( - SECRET_ENV_NAMES.filter(name => Boolean(process.env[name])), - 'provider and cloud credentials must be scrubbed by the guarded runner', - ).toEqual([]); - const toolIds = selectRequestedToolIds(SUPPORTED_TOOLS); - const root = baseURL ?? process.env.WAGGLE_E2E_BASE_URL ?? 'http://127.0.0.1:3333'; - - const detectionResponse = await request.get('/api/tools/detect'); - expect(detectionResponse.ok()).toBe(true); - const detection = await detectionResponse.json() as DetectionEnvelope; - expect(detection.platform).toBe('win32'); - expect( - detection.tools.filter(tool => tool.builtin === true).map(tool => tool.id), - ).toEqual(SUPPORTED_TOOLS); - - await page.goto(routeWithSkip('/launcher?watch=1'), { waitUntil: 'domcontentloaded' }); - await expect(page.getByText('Tool Launcher', { exact: true })).toBeVisible({ timeout: 15_000 }); - for (const tool of detection.tools) { - await expect(page.getByText(tool.displayName, { exact: true }).first()).toBeVisible({ timeout: 10_000 }); - } - - const workspace = await createManagedWorkspace(request); - const activePids = new Set(); - const results: RouteResult[] = []; - - try { - for (const toolId of toolIds) { - const tool = detection.tools.find(candidate => candidate.id === toolId)!; - if (!tool.installed || !tool.installedPath) { - results.push({ - id: tool.id, - displayName: tool.displayName, - status: 'unavailable', - installedPath: null, - }); - testInfo.annotations.push({ type: 'tool-unavailable', description: tool.displayName }); - continue; - } - - await test.step(`${tool.displayName} supported route`, async () => { - if (tool.capabilities?.headlessTask !== true) { - const unsupported = await request.post('/api/tools/run', { - data: { - toolId: tool.id, - workspaceIds: [workspace.id], - prompt: 'Do not run. This request must be rejected as interactive-only.', - timeoutMs: 10_000, - }, - }); - expect(unsupported.status(), await unsupported.text()).toBe(409); - expect(await unsupported.json()).toMatchObject({ - error: 'TOOL_NOT_HEADLESS', - toolId: tool.id, - }); - results.push({ - id: tool.id, - displayName: tool.displayName, - status: 'interactive-only-rejected', - installedPath: tool.installedPath, - route: '/api/tools/run', - }); - return; - } - - const args = SAFE_VERSION_ARGS[tool.id as ToolId]; - expect(args, `${tool.displayName} must have an audited no-network version command`).toBeTruthy(); - const launchResponse = await request.post('/api/tools/launch', { - data: { - id: tool.id, - args, - workspaceId: workspace.id, - observe: true, - }, - }); - expect(launchResponse.status(), await launchResponse.text()).toBe(202); - const launch = await launchResponse.json() as LaunchEnvelope; - expect(launch, launch.error).toMatchObject({ ok: true }); - expect(launch.pid).toEqual(expect.any(Number)); - expect(launch.roomId).toMatch(/\S/); - expect(launch.runId).toMatch(/\S/); - activePids.add(launch.pid!); - - const stream = await readObservedStream(root, launch.pid!); - expect(stream.exitCode).toBe(0); - expect(stream.lines.join('\n').trim().length).toBeGreaterThan(0); - await waitForProcessClear(request, launch.pid!); - activePids.delete(launch.pid!); - results.push({ - id: tool.id, - displayName: tool.displayName, - status: 'safe-version-exit', - installedPath: tool.installedPath, - exitCode: stream.exitCode, - output: stream.lines.join('\n').trim(), - route: '/api/tools/launch', - }); - }); - } - - expect(results.map(result => result.id)).toEqual(toolIds); - if (process.env.WAGGLE_E2E_HOST_IDS) { - expect( - results.filter(result => result.status === 'unavailable').map(result => result.id), - 'every explicitly requested host must be installed and healthy', - ).toEqual([]); - } else { - expect(results.some(result => result.status !== 'unavailable'), 'at least one real installed tool route').toBe(true); - } - } finally { - try { - await testInfo.attach('windows-external-tool-route-summary', { - body: Buffer.from(JSON.stringify({ workspace, results }, null, 2)), - contentType: 'application/json', - }); - } finally { - for (const pid of activePids) { - await request.post('/api/tools/kill', { data: { pid } }).catch(() => null); - } - const deleteResponse = await request.delete(`/api/workspaces/${encodeURIComponent(workspace.id)}`).catch(() => null); - if (deleteResponse) expect([204, 404]).toContain(deleteResponse.status()); - } - } - }); -}); diff --git a/tests/integration/external-agent-collaboration.live.test.ts b/tests/integration/external-agent-collaboration.live.test.ts deleted file mode 100644 index 9a45d3f..0000000 --- a/tests/integration/external-agent-collaboration.live.test.ts +++ /dev/null @@ -1,344 +0,0 @@ -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; -import { createHash, randomBytes } from 'node:crypto'; -import fs from 'node:fs'; -import os from 'node:os'; -import path from 'node:path'; -import type { FastifyInstance } from 'fastify'; -import { detectInstalledTools, getToolRegistry, runExternalTool } from '@waggle/agent'; -import { scopeKey, workspaceScope } from '@waggle/memory-contract'; -import { AgentRunRegistry } from '../../packages/server/src/local/agent-run-registry.js'; -import { buildLocalServer } from '../../packages/server/src/local/index.js'; -import { injectWithAuth } from '../../packages/server/tests/test-utils.js'; - -const LIVE = process.env.WAGGLE_LIVE_EXTERNAL_AGENTS === '1'; -const describeLive = LIVE ? describe : describe.skip; -const REQUIRED_TOOLS = ['claude-code', 'codex', 'hermes'] as const; - -async function waitFor( - predicate: () => boolean, - timeoutMs: number, - message: string, -): Promise { - const deadline = Date.now() + timeoutMs; - while (Date.now() < deadline) { - if (predicate()) return; - await new Promise((resolve) => setTimeout(resolve, 250)); - } - throw new Error(message); -} - -describeLive('live external-agent collaboration', () => { - let server: FastifyInstance; - let dataDir: string; - let sourceWorkspaceDir: string; - let sourceWorkspaceId: string; - let synthesisWorkspaceDir: string; - let synthesisWorkspaceId: string; - let expectedCanaryLine: string; - - beforeAll(async () => { - dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-live-collab-')); - sourceWorkspaceDir = path.join(dataDir, 'source-workspace'); - synthesisWorkspaceDir = path.join(dataDir, 'synthesis-workspace'); - fs.mkdirSync(sourceWorkspaceDir, { recursive: true }); - fs.mkdirSync(synthesisWorkspaceDir, { recursive: true }); - expectedCanaryLine = `CANARY=${randomBytes(24).toString('hex')}`; - fs.writeFileSync(path.join(sourceWorkspaceDir, 'CANARY.txt'), `${expectedCanaryLine}\n`, 'utf8'); - fs.writeFileSync(path.join(synthesisWorkspaceDir, 'SENTINEL.txt'), 'workspace must remain unchanged\n', 'utf8'); - server = await buildLocalServer({ dataDir }); - const hermesProvider = process.env.WAGGLE_LIVE_HERMES_PROVIDER?.trim(); - const hermesModel = process.env.WAGGLE_LIVE_HERMES_MODEL?.trim(); - if (Boolean(hermesProvider) !== Boolean(hermesModel)) { - throw new Error('Set both WAGGLE_LIVE_HERMES_PROVIDER and WAGGLE_LIVE_HERMES_MODEL'); - } - server.decorate('externalToolRunner', (request) => { - const task = request.manifest.task; - if (request.manifest.id !== 'hermes' || !task || !hermesProvider || !hermesModel) { - return runExternalTool(request); - } - const override = ['--provider', hermesProvider, '-m', hermesModel]; - return runExternalTool({ - ...request, - manifest: { - ...request.manifest, - task: { - ...task, - argvTemplate: [...task.argvTemplate, ...override], - ...(task.resumeArgvTemplate - ? { resumeArgvTemplate: [...task.resumeArgvTemplate, ...override] } - : {}), - }, - }, - }); - }); - const sourceWorkspace = server.workspaceManager.create({ - name: 'Live Collaboration Source', - group: 'live-test', - directory: sourceWorkspaceDir, - }); - const synthesisWorkspace = server.workspaceManager.create({ - name: 'Live Collaboration Synthesis', - group: 'live-test', - directory: synthesisWorkspaceDir, - }); - sourceWorkspaceId = sourceWorkspace.id; - synthesisWorkspaceId = synthesisWorkspace.id; - await server.listen({ host: '127.0.0.1', port: 0 }); - }, 120_000); - - afterAll(async () => { - if (server) await server.close(); - if (dataDir) { - let lastError: unknown; - let removed = false; - for (let attempt = 0; attempt < 10; attempt++) { - try { - fs.rmSync(dataDir, { recursive: true, force: true }); - removed = true; - break; - } catch (err) { - lastError = err; - await new Promise((resolve) => setTimeout(resolve, 250)); - } - } - if (!removed) throw lastError; - } - }, 90_000); - - it('runs the authenticated supported-agent cohort and delivers peer evidence through WaggleDance', async () => { - const detection = await detectInstalledTools(); - const manifests = new Map(getToolRegistry().map((manifest) => [manifest.id, manifest])); - const unavailable = REQUIRED_TOOLS.filter((id) => !detection.tools.some( - (tool) => tool.id === id && tool.installed && tool.installedPath, - ) || manifests.get(id)?.capabilities?.headlessTask !== true); - expect(unavailable, `Missing authenticated headless tools: ${unavailable.join(', ')}`).toEqual([]); - expect(server.workspaceManager.get(sourceWorkspaceId)?.directory).toBe(sourceWorkspaceDir); - expect(server.workspaceManager.get(synthesisWorkspaceId)?.directory).toBe(synthesisWorkspaceDir); - const sourceDigestBefore = workspaceDigest(sourceWorkspaceDir); - const synthesisDigestBefore = workspaceDigest(synthesisWorkspaceDir); - - const response = await injectWithAuth(server, { - method: 'POST', - url: '/api/tools/run', - payload: { - prompt: [ - 'This is a read-only acceptance test. Do not modify any file.', - 'Look only in the assigned workspace for ./CANARY.txt.', - 'If it exists, copy its entire single line verbatim, including the literal CANARY= prefix, and do not add commentary.', - 'A source-file answer must start with the literal CANARY= prefix followed by exactly 48 lowercase hexadecimal characters.', - 'If it does not exist, return exactly NO_LOCAL_CANARY and never guess its content.', - 'If peer findings are supplied in a later collaboration round, copy the complete matching CANARY= line from those findings verbatim as the source of truth.', - ].join(' '), - participants: [ - { toolId: 'claude-code', workspaceIds: [sourceWorkspaceId], access: 'read-only' }, - { toolId: 'codex', workspaceIds: [sourceWorkspaceId], access: 'read-only' }, - { toolId: 'hermes', workspaceIds: [sourceWorkspaceId], access: 'native' }, - { toolId: 'hermes', workspaceIds: [synthesisWorkspaceId], access: 'native' }, - ], - timeoutMs: 300_000, - }, - }); - expect(response.statusCode).toBe(202); - const body = response.json() as { - roomId: string; - runs: Array<{ runId: string; toolId: string; workspaceId: string }>; - }; - expect(body.runs).toHaveLength(5); - - await waitFor( - () => { - return [body.roomId, ...body.runs.map(({ runId }) => runId)].every((id) => - ['completed', 'failed', 'cancelled', 'interrupted'].includes( - server.agentRunRegistry.get(id)?.status ?? '', - )); - }, - 390_000, - 'live external-agent Room did not settle', - ); - const terminalRoom = server.agentRunRegistry.get(body.roomId); - const terminalRuns = body.runs.map(({ runId }) => server.agentRunRegistry.get(runId)!); - if (terminalRoom?.status === 'completed' && terminalRuns.every((run) => run.status === 'completed')) { - await waitFor( - () => [body.roomId, ...body.runs.map(({ runId }) => runId)].every((id) => - server.agentRunRegistry.get(id)?.memoryRefs.status === 'complete'), - 30_000, - 'live external-agent memory receipts did not settle', - ); - } - - const room = server.agentRunRegistry.get(body.roomId); - const runs = body.runs.map(({ runId }) => server.agentRunRegistry.get(runId)!); - const diagnostic = JSON.stringify({ - room: { status: room?.status, memoryStatus: room?.memoryRefs.status }, - versions: Object.fromEntries(detection.tools - .filter((tool) => REQUIRED_TOOLS.includes(tool.id as typeof REQUIRED_TOOLS[number])) - .map((tool) => [tool.id, tool.version ?? null])), - runs: runs.map((run) => ({ - tool: run.executor.toolId, status: run.status, exitCode: run.result?.exitCode, - memoryStatus: run.memoryRefs.status, - error: run.result?.error, - summary: run.result?.summary.slice(0, 500), - summaryHash: hashText(run.result?.summary ?? ''), - })), - }); - expect(room?.status, diagnostic).toBe('completed'); - expect(room?.memoryRefs.status, diagnostic).toBe('complete'); - for (const run of runs) { - expect(run.status, diagnostic).toBe('completed'); - expect(run.result?.exitCode, diagnostic).toBe(0); - expect(run.memoryRefs.status, diagnostic).toBe('complete'); - expect(run.memoryRefs.personalFrameIds?.length).toBeGreaterThan(0); - expect(run.kind).toBe('worker'); - if (run.kind === 'worker') { - expect(run.memoryRefs.workspaceFrameIds?.[run.workspaceId]?.length).toBeGreaterThan(0); - } - } - const claude = runs.find((run) => run.executor.toolId === 'claude-code'); - const codex = runs.find((run) => run.executor.toolId === 'codex'); - const hermes = runs.find((run) => run.executor.toolId === 'hermes' - && run.workspaceId === sourceWorkspaceId - && !run.title.startsWith('WaggleDance synthesis')); - const hermesNoCanary = runs.find((run) => run.executor.toolId === 'hermes' - && run.workspaceId === synthesisWorkspaceId - && !run.title.startsWith('WaggleDance synthesis')); - const synthesis = runs.find((run) => run.title.startsWith('WaggleDance synthesis')); - for (const run of [claude, codex, hermes]) { - expect(run?.result?.summary.trim(), diagnostic).toBe(expectedCanaryLine); - } - expect(hermesNoCanary?.result?.summary.trim(), diagnostic).toBe('NO_LOCAL_CANARY'); - expect(synthesis?.kind).toBe('worker'); - expect(synthesis?.executor.toolId).toBe('hermes'); - if (synthesis?.kind === 'worker') expect(synthesis.workspaceId).toBe(synthesisWorkspaceId); - expect(synthesis?.result?.summary.trim(), diagnostic).toBe(expectedCanaryLine); - - expect(workspaceDigest(sourceWorkspaceDir)).toBe(sourceDigestBefore); - expect(workspaceDigest(synthesisWorkspaceDir)).toBe(synthesisDigestBefore); - - const durableRegistry = new AgentRunRegistry(path.join(dataDir, 'agent-runs.json')); - for (const id of [body.roomId, ...body.runs.map(({ runId }) => runId)]) { - const durable = durableRegistry.get(id); - expect(durable?.status, diagnostic).toBe('completed'); - expect(durable?.memoryRefs.status, diagnostic).toBe('complete'); - expect(durable?.memoryRefs, diagnostic).toEqual(server.agentRunRegistry.get(id)?.memoryRefs); - } - - for (const run of runs) { - const workspaceId = run.kind === 'worker' ? run.workspaceId : undefined; - expect(Object.keys(run.memoryRefs.workspaceFrameIds), diagnostic).toEqual([workspaceId]); - const personal = await Promise.all( - run.memoryRefs.personalFrameIds.map((id) => server.memory.get(id)), - ); - const workspace = workspaceId - ? await Promise.all( - run.memoryRefs.workspaceFrameIds[workspaceId].map((id) => server.memory.get(id)), - ) - : []; - expect(personal.length, diagnostic).toBeGreaterThan(0); - expect(workspace.length, diagnostic).toBeGreaterThan(0); - for (const memory of personal) { - expect(memory?.scope.kind, diagnostic).toBe('personal'); - } - for (const memory of workspace) { - expect(memory && scopeKey(memory.scope), diagnostic).toBe(scopeKey(workspaceScope(workspaceId!))); - } - for (const memory of [...personal, ...workspace]) { - expect(memory, diagnostic).not.toBeNull(); - const metadata = memory?.metadata ?? {}; - expect(metadata).toMatchObject({ - runId: run.id, - roomId: run.roomId, - workspaceId: run.kind === 'worker' ? run.workspaceId : undefined, - toolId: run.executor.toolId, - }); - expect(memory?.text.includes(run.id), diagnostic).toBe(true); - for (const forbiddenPath of [dataDir, sourceWorkspaceDir, synthesisWorkspaceDir]) { - expect(memory?.text.includes(forbiddenPath), diagnostic).toBe(false); - expect(JSON.stringify(metadata).includes(forbiddenPath), diagnostic).toBe(false); - } - } - const expectedSummary = run.result?.summary.trim() ?? ''; - expect(personal.some((memory) => memory?.text.includes(expectedSummary)), diagnostic).toBe(true); - expect(workspace.some((memory) => memory?.text.includes(expectedSummary)), diagnostic).toBe(true); - } - expect(new Set(room?.memoryRefs.personalFrameIds)).toEqual( - new Set(runs.flatMap((run) => run.memoryRefs.personalFrameIds)), - ); - expect(room?.memoryRefs.workspaceFrameIds).toEqual(Object.fromEntries( - [...new Set(runs.map((run) => run.workspaceId))].map((workspaceId) => [ - workspaceId, - [...new Set(runs.flatMap((run) => run.memoryRefs.workspaceFrameIds[workspaceId] ?? []))], - ]), - )); - - const roomSignals = server.signalBus?.query({ teamId: `room::${body.roomId}`, limit: 1_000 }) ?? []; - const subtypes = roomSignals.map((message) => message.subtype); - expect(subtypes).toEqual(expect.arrayContaining(['task_delegation', 'task_claim', 'routed_share', 'knowledge_match'])); - for (const run of runs) { - const delegation = roomSignals.find((message) => - message.subtype === 'task_delegation' && message.content.runId === run.id); - const claim = roomSignals.find((message) => - message.subtype === 'task_claim' && message.content.runId === run.id); - const share = roomSignals.find((message) => - message.subtype === 'routed_share' && message.content.runId === run.id - && message.content.phase === 'completed'); - expect(delegation, diagnostic).toBeDefined(); - expect(claim?.referenceId, diagnostic).toBe(delegation?.id); - expect(share?.referenceId, diagnostic).toBe(delegation?.id); - } - const synthesisDelegation = roomSignals.find((message) => - message.subtype === 'task_delegation' && message.content.runId === synthesis?.id); - const peerDelivery = roomSignals.find((message) => - message.subtype === 'knowledge_match' && message.content.runId === synthesis?.id); - expect(peerDelivery?.referenceId, diagnostic).toBe(synthesisDelegation?.id); - expect(peerDelivery?.content.tool, diagnostic).toBe('hermes'); - expect(peerDelivery?.content.peerFindings).toEqual(expect.arrayContaining([ - expect.stringContaining(expectedCanaryLine), - expect.stringContaining('NO_LOCAL_CANARY'), - ])); - const initialRunIds = new Set(runs.filter((run) => run.id !== synthesis?.id).map((run) => run.id)); - const expectedSourceMessageIds = new Set(roomSignals.filter((message) => - message.subtype === 'routed_share' - && initialRunIds.has(String(message.content.runId)) - && message.content.phase === 'completed').map((message) => message.id)); - expect(new Set(peerDelivery?.content.sourceMessageIds as string[] | undefined)).toEqual(expectedSourceMessageIds); - expect(new Set(synthesisDelegation?.content.sourceRunIds as string[] | undefined)).toEqual(initialRunIds); - const peerFindings = peerDelivery?.content.peerFindings as string[] | undefined; - expect(peerFindings, diagnostic).toHaveLength(initialRunIds.size); - for (const runId of initialRunIds) { - expect(peerFindings?.filter((finding) => finding.includes(`ยท run ${runId}]`)), diagnostic).toHaveLength(1); - } - expect(new Set(roomSignals.map((message) => message.content.runId))).toEqual( - new Set(body.runs.map((run) => run.runId)), - ); - }, 600_000); -}); - -function hashText(value: string): string { - return createHash('sha256').update(value).digest('hex'); -} - -function workspaceDigest(root: string): string { - const hash = createHash('sha256'); - const visit = (directory: string, relativeDirectory: string): void => { - const entries = fs.readdirSync(directory, { withFileTypes: true }) - .sort((left, right) => left.name.localeCompare(right.name)); - for (const entry of entries) { - const relative = path.join(relativeDirectory, entry.name).replaceAll('\\', '/'); - const absolute = path.join(directory, entry.name); - if (entry.isDirectory()) { - hash.update(`D\0${relative}\0`); - visit(absolute, relative); - } else if (entry.isFile()) { - hash.update(`F\0${relative}\0`); - hash.update(fs.readFileSync(absolute)); - hash.update('\0'); - } else if (entry.isSymbolicLink()) { - hash.update(`L\0${relative}\0${fs.readlinkSync(absolute)}\0`); - } else { - hash.update(`X\0${relative}\0`); - } - } - }; - visit(root, ''); - return hash.digest('hex'); -}