-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcodex_cli_ab_proof.ps1
More file actions
150 lines (131 loc) · 4.77 KB
/
Copy pathcodex_cli_ab_proof.ps1
File metadata and controls
150 lines (131 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
param(
[string]$Model = "",
[string]$PricePerMillionInput = "1.50",
[string]$OutDir = "codex-ab-proof"
)
$ErrorActionPreference = "Stop"
$ScriptRoot = if ($PSScriptRoot) { $PSScriptRoot } else { (Get-Location).Path }
$ProofDir = if ([System.IO.Path]::IsPathRooted($OutDir)) { $OutDir } else { Join-Path $ScriptRoot $OutDir }
New-Item -ItemType Directory -Force -Path $ProofDir | Out-Null
$rawPrompt = Join-Path $ProofDir "without-sage-raw-prompt.txt"
$sagePrompt = Join-Path $ProofDir "with-sage-compressed-prompt.txt"
$rawJsonl = Join-Path $ProofDir "without-sage-codex.jsonl"
$sageJsonl = Join-Path $ProofDir "with-sage-codex.jsonl"
$reportPath = Join-Path $ProofDir "codex-ab-report.json"
$lines = New-Object System.Collections.Generic.List[string]
$lines.Add("Reply with exactly OK. Do not run tools. Terminal output follows:")
$lines.Add("pytest tests/test_payments.py::test_checkout_flow FAILED")
for ($i = 0; $i -lt 1800; $i++) {
$lineNumber = 120 + ($i % 30)
$lines.Add(("E AssertionError: expected status 200 got 500 | request_id=req_{0:D4} | stack frame app/payments.py:{1}" -f $i, $lineNumber))
}
[IO.File]::WriteAllLines($rawPrompt, $lines)
$compressed = @"
Reply with exactly OK. Do not run tools. Terminal output follows:
Tests: passed=0 failed=1 skipped=0
Summary: E AssertionError: expected status 200 got 500 | request_id=req_1799 | stack frame app/payments.py:149
Failed:
- pytest tests/test_payments.py::test_checkout_flow FAILED
"@
[IO.File]::WriteAllText($sagePrompt, $compressed)
function Invoke-CodexProofRun {
param(
[string]$PromptPath,
[string]$OutputPath
)
$args = @("exec", "--json", "--skip-git-repo-check", "--sandbox", "read-only")
if ($Model.Trim()) {
$args += @("--model", $Model)
}
$args += @("-")
Get-Content -Raw -LiteralPath $PromptPath | & codex @args | Tee-Object -FilePath $OutputPath
}
function Read-Jsonl {
param([string]$Path)
$events = @()
if (-not (Test-Path -LiteralPath $Path)) {
return $events
}
foreach ($line in Get-Content -LiteralPath $Path) {
if (-not $line.Trim()) { continue }
try {
$events += ($line | ConvertFrom-Json)
} catch {
$events += [pscustomobject]@{ type = "non_json"; text = $line }
}
}
return $events
}
function Get-UsageTokenFields {
param([array]$Events)
$hits = @()
foreach ($event in $Events) {
if ($event.type -ne "turn.completed" -or -not $event.usage) {
continue
}
foreach ($prop in $event.usage.PSObject.Properties) {
if ($prop.Name -match "(?i)token|usage") {
$hits += [pscustomobject]@{
path = "turn.completed.usage.$($prop.Name)"
value = $prop.Value
}
}
}
}
return $hits
}
function Get-UsageNumber {
param(
[array]$Events,
[string]$Name
)
foreach ($event in $Events) {
if ($event.type -ne "turn.completed" -or -not $event.usage) {
continue
}
$prop = $event.usage.PSObject.Properties[$Name]
if ($prop -and "$($prop.Value)" -match "^\d+$") {
return [int64]$prop.Value
}
}
return $null
}
Write-Host "Running Codex raw-output A/B leg..."
Invoke-CodexProofRun -PromptPath $rawPrompt -OutputPath $rawJsonl | Out-Host
Write-Host "Running Codex SAGE-compressed A/B leg..."
Invoke-CodexProofRun -PromptPath $sagePrompt -OutputPath $sageJsonl | Out-Host
$rawEvents = Read-Jsonl -Path $rawJsonl
$sageEvents = Read-Jsonl -Path $sageJsonl
$rawHits = Get-UsageTokenFields -Events $rawEvents
$sageHits = Get-UsageTokenFields -Events $sageEvents
$rawInput = Get-UsageNumber -Events $rawEvents -Name "input_tokens"
$sageInput = Get-UsageNumber -Events $sageEvents -Name "input_tokens"
$saved = $null
$savedUsd = $null
$reduction = $null
if ($null -ne $rawInput -and $null -ne $sageInput -and $rawInput -gt 0) {
$saved = $rawInput - $sageInput
$reduction = [math]::Round(($saved / $rawInput) * 100, 2)
$savedUsd = [math]::Round(($saved / 1000000) * [double]$PricePerMillionInput, 6)
}
$report = [ordered]@{
provider = "codex-cli"
model = $(if ($Model.Trim()) { $Model } else { "codex-config-default" })
price_per_million_input_usd = [double]$PricePerMillionInput
files = [ordered]@{
raw_prompt = (Resolve-Path $rawPrompt).Path
sage_prompt = (Resolve-Path $sagePrompt).Path
raw_jsonl = (Resolve-Path $rawJsonl).Path
sage_jsonl = (Resolve-Path $sageJsonl).Path
}
raw_token_fields = $rawHits
sage_token_fields = $sageHits
raw_input_tokens = $rawInput
sage_input_tokens = $sageInput
provider_input_tokens_saved = $saved
provider_input_reduction_percent = $reduction
estimated_input_savings_usd = $savedUsd
note = "If raw_input_tokens or sage_input_tokens is null, this Codex CLI JSON stream did not expose provider usage fields."
}
$report | ConvertTo-Json -Depth 100 | Tee-Object -FilePath $reportPath
Write-Host "Report written to $reportPath"