afyx-engineering-kit 0.0.0-stage → 0.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.afyx-source-revision +1 -0
- package/LICENSE +21 -0
- package/README.md +377 -2
- package/SECURITY.md +74 -0
- package/afyx-context/LICENSE +21 -0
- package/afyx-context/README.md +155 -0
- package/afyx-context/afyx-context.json +30 -0
- package/afyx-context/contracts/integration-v1.schema.json +34 -0
- package/afyx-context/package-lock.json +53 -0
- package/afyx-context/package.json +34 -0
- package/afyx-context/scripts/benchmark.mjs +39 -0
- package/afyx-context/scripts/build.mjs +15 -0
- package/afyx-context/scripts/comparative-benchmark.mjs +342 -0
- package/afyx-context/scripts/comparative-worker.mjs +74 -0
- package/afyx-context/scripts/graph-integration-e2e.mjs +188 -0
- package/afyx-context/scripts/integrated-dogfood.mjs +309 -0
- package/afyx-context/src/budget.js +166 -0
- package/afyx-context/src/cli.js +110 -0
- package/afyx-context/src/coordination.js +343 -0
- package/afyx-context/src/engine.js +198 -0
- package/afyx-context/src/errors.js +14 -0
- package/afyx-context/src/graph-integration-worker.js +70 -0
- package/afyx-context/src/graph-integration.js +373 -0
- package/afyx-context/src/graph-mcp-bridge.js +103 -0
- package/afyx-context/src/index.js +25 -0
- package/afyx-context/src/integrations.js +87 -0
- package/afyx-context/src/memory.js +255 -0
- package/afyx-context/src/storage.js +364 -0
- package/afyx-context/test-fixtures/coordination-worker.mjs +52 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/__init__.py +1 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/__manifest__.py +4 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/models/__init__.py +1 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/models/sale_order.py +5 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/__init__.py +1 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/__manifest__.py +10 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/__init__.py +2 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/sale_order.py +20 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/sale_order_audit.py +11 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/security/ir.model.access.csv +2 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/views/sale_order_views.xml +14 -0
- package/afyx-forge/LICENSE +55 -0
- package/afyx-forge/README.md +126 -0
- package/afyx-forge/afyx-forge.json +28 -0
- package/afyx-forge/bin/afyx-forge +4 -0
- package/afyx-forge/bin/afyx-forge.cmd +3 -0
- package/afyx-forge/bin/afyx-forge.js +16 -0
- package/afyx-forge/contracts/forge-plan-v1.schema.json +172 -0
- package/afyx-forge/package.json +26 -0
- package/afyx-forge/src/classifier.js +190 -0
- package/afyx-forge/src/cli.js +111 -0
- package/afyx-forge/src/doctor.js +105 -0
- package/afyx-forge/src/guidance.js +120 -0
- package/afyx-forge/src/index.js +273 -0
- package/afyx-forge/src/planning.js +107 -0
- package/afyx-forge/src/policy.js +109 -0
- package/afyx-forge/src/workflow-refiner.js +151 -0
- package/afyx-graph/afyx-graph.json +20 -0
- package/afyx-graph/engine/LICENSE +21 -0
- package/afyx-graph/engine/README.md +104 -0
- package/afyx-graph/engine/release/RELEASE-MANIFEST.json +21 -0
- package/afyx-graph/engine/release/SHA256SUMS +2 -0
- package/afyx-graph/engine/release/afyx-graph-linux-x64.tar.gz +0 -0
- package/afyx-graph/engine/release/afyx-graph-win32-x64.zip +0 -0
- package/afyx-graph/engine/scripts/distribution-contract.mjs +212 -0
- package/afyx-graph/engine/scripts/distribution-product.json +33 -0
- package/bin/afyx-engineering-kit.mjs +61 -0
- package/install.ps1 +1053 -0
- package/install.sh +617 -0
- package/package.json +66 -4
- package/scripts/afyx-capability-orchestrator.mjs +752 -0
- package/scripts/afyx-claude-integration.mjs +547 -0
- package/scripts/afyx-context-artifact.mjs +105 -0
- package/scripts/afyx-credential-broker.mjs +75 -0
- package/scripts/afyx-doctor.ps1 +152 -0
- package/scripts/afyx-doctor.sh +146 -0
- package/scripts/afyx-forge-artifact.mjs +183 -0
- package/scripts/afyx-global-routing-policy.md +12 -0
- package/scripts/afyx-global-routing.mjs +168 -0
- package/scripts/afyx-graph-artifact.mjs +178 -0
- package/scripts/afyx-host-adapters.mjs +543 -0
- package/scripts/afyx-host-bridge-lifecycle.mjs +174 -0
- package/scripts/afyx-host-bridge.mjs +288 -0
- package/scripts/afyx-integration-contracts.mjs +825 -0
- package/scripts/afyx-mcp-integration.mjs +528 -0
- package/scripts/afyx-modular-integration.mjs +361 -0
- package/scripts/afyx-modular-runtime.mjs +451 -0
- package/scripts/afyx-native-skill-host.mjs +124 -0
- package/scripts/afyx-npm-installer.mjs +486 -0
- package/scripts/afyx-project.ps1 +205 -0
- package/scripts/afyx-project.sh +207 -0
- package/scripts/afyx-provider-backup.mjs +1206 -0
- package/scripts/afyx-provider-integration.mjs +724 -0
- package/scripts/afyx-provider-inventory.mjs +147 -0
- package/scripts/afyx-security-auditor.mjs +224 -0
- package/scripts/afyx-skill-engineering.mjs +354 -0
- package/scripts/afyx-tool-family-routing.mjs +64 -0
- package/scripts/afyx-universal-runtime.mjs +530 -0
- package/scripts/alpha_acceptance_preflight.py +240 -0
- package/scripts/benchmark-graph-memory.mjs +96 -0
- package/scripts/benchmark_capability_orchestrator.mjs +64 -0
- package/scripts/build-windows-installer.ps1 +259 -0
- package/scripts/build_forge_synthetic_image.py +328 -0
- package/scripts/check-reference-staleness.py +71 -0
- package/scripts/components.json +254 -0
- package/scripts/contracts/afyx-capability-request-v1.schema.json +189 -0
- package/scripts/contracts/afyx-capability-result-v1.schema.json +32 -0
- package/scripts/dogfood_engineering_comparison.mjs +303 -0
- package/scripts/engineering-assurance.json +28 -0
- package/scripts/install-afyx-context.ps1 +142 -0
- package/scripts/install-afyx-context.sh +126 -0
- package/scripts/install-afyx-forge.ps1 +265 -0
- package/scripts/install-afyx-forge.sh +273 -0
- package/scripts/install-afyx-graph.ps1 +571 -0
- package/scripts/install-afyx-graph.sh +438 -0
- package/scripts/install-codex-usage-tracker.ps1 +172 -0
- package/scripts/lib/AfyxComponents.psm1 +399 -0
- package/scripts/lib/AfyxSkillLifecycle.psm1 +289 -0
- package/scripts/lib/AfyxTerminal.psm1 +838 -0
- package/scripts/lib/afyx-components.sh +256 -0
- package/scripts/lib/afyx-skill-lifecycle.sh +271 -0
- package/scripts/link-global-dev.ps1 +193 -0
- package/scripts/link-global-dev.sh +130 -0
- package/scripts/phase8-paired-evidence.mjs +195 -0
- package/scripts/phase8-representative-workload.mjs +771 -0
- package/scripts/phase8-session-recorder.mjs +157 -0
- package/scripts/phase8_event_ledger.py +248 -0
- package/scripts/phase8_evidence_discovery.py +189 -0
- package/scripts/prepare-g6-package.mjs +349 -0
- package/scripts/resolve-git-release-artifacts.mjs +321 -0
- package/scripts/stage-npm-release.mjs +94 -0
- package/scripts/validate-afyx-graph.py +203 -0
- package/scripts/validate-components.py +194 -0
- package/scripts/validate-engineering-assurance.py +281 -0
- package/scripts/verify-graph-archive.mjs +64 -0
- package/skills/afyx-context/SKILL.md +26 -0
- package/skills/afyx-engineering-expert/README.md +42 -0
- package/skills/afyx-engineering-expert/SKILL.md +70 -0
- package/skills/afyx-engineering-expert/assets/result-template.md +15 -0
- package/skills/afyx-engineering-expert/references/ai-ml-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/business-analyst.md +17 -0
- package/skills/afyx-engineering-expert/references/clean-code-engineer.md +26 -0
- package/skills/afyx-engineering-expert/references/code-reviewer.md +24 -0
- package/skills/afyx-engineering-expert/references/cybersecurity-devsecops.md +17 -0
- package/skills/afyx-engineering-expert/references/data-analyst-bi.md +24 -0
- package/skills/afyx-engineering-expert/references/data-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/database-administrator.md +17 -0
- package/skills/afyx-engineering-expert/references/debugging-specialist.md +24 -0
- package/skills/afyx-engineering-expert/references/embedded-iot-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/erp-technical-specialist.md +17 -0
- package/skills/afyx-engineering-expert/references/hardware-diagnostics-auditor.md +24 -0
- package/skills/afyx-engineering-expert/references/hardware-electronics-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/integration-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/legacy-modernization-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/llmops-prompt-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/network-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/performance-benchmark-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/platform-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/product-manager.md +17 -0
- package/skills/afyx-engineering-expert/references/qa-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/site-reliability-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/software-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/solutions-architect.md +17 -0
- package/skills/afyx-engineering-expert/references/specialists-index.md +33 -0
- package/skills/afyx-engineering-expert/references/subject-matter-expert.md +17 -0
- package/skills/afyx-engineering-expert/references/system-analyst.md +17 -0
- package/skills/afyx-engineering-expert/references/technical-project-manager.md +24 -0
- package/skills/afyx-engineering-expert/references/technical-writer.md +24 -0
- package/skills/afyx-engineering-expert/references/ui-ux-designer.md +17 -0
- package/skills/afyx-engineering-expert/workflows/ai-data-erp.md +20 -0
- package/skills/afyx-engineering-expert/workflows/debugging.md +10 -0
- package/skills/afyx-engineering-expert/workflows/discovery-design.md +11 -0
- package/skills/afyx-engineering-expert/workflows/implementation.md +12 -0
- package/skills/afyx-engineering-expert/workflows/incident-reliability.md +12 -0
- package/skills/afyx-engineering-expert/workflows/review-audit.md +13 -0
- package/skills/afyx-forge/SKILL.md +43 -0
- package/skills/afyx-forge/references/adaptive-policy.md +19 -0
- package/skills/efficient-coding/SKILL.md +33 -0
- package/skills/efficient-coding/references/investigation.md +27 -0
- package/skills/efficient-coding/references/token-efficiency.md +25 -0
- package/skills/efficient-coding/references/tool-routing.md +13 -0
- package/skills/odoo-engineering/SKILL.md +33 -0
- package/skills/odoo-engineering/references/common.md +9 -0
- package/skills/odoo-engineering/references/odoo-10.md +32 -0
- package/skills/odoo-engineering/references/odoo-11.md +26 -0
- package/skills/odoo-engineering/references/odoo-12.md +31 -0
- package/skills/odoo-engineering/references/odoo-13.md +27 -0
- package/skills/odoo-engineering/references/odoo-14.md +31 -0
- package/skills/odoo-engineering/references/odoo-15.md +34 -0
- package/skills/odoo-engineering/references/odoo-16.md +37 -0
- package/skills/odoo-engineering/references/odoo-17.md +37 -0
- package/skills/odoo-engineering/references/odoo-18.md +48 -0
- package/skills/odoo-engineering/references/odoo-19.md +43 -0
- package/skills/odoo-engineering/references/odoo-20.md +39 -0
- package/skills/odoo-engineering/references/reference-schema.md +16 -0
- package/skills/odoo-engineering/references/version-detection.md +13 -0
- package/tools/codex-usage/CodexUsage.psm1 +496 -0
- package/tools/codex-usage/codex-usage-doctor.ps1 +150 -0
- package/tools/codex-usage/codex-usage-pricing.json +25 -0
- package/tools/codex-usage/codex-usage-stop.ps1 +167 -0
- package/tools/codex-usage/codex-usage-watch.ps1 +250 -0
- package/tools/codex-usage/vscode-user-task.json +31 -0
- package/tools/windows-setup/SetupBootstrapper.cs +258 -0
- package/uninstall.ps1 +189 -0
- package/uninstall.sh +167 -0
- package/update.ps1 +95 -0
- package/update.sh +67 -0
- package/verify.ps1 +411 -0
- package/verify.sh +265 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
[CmdletBinding()]
|
|
2
|
+
param(
|
|
3
|
+
[string]$CodexHome = $(if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $env:USERPROFILE '.codex' }),
|
|
4
|
+
[string]$HooksPath,
|
|
5
|
+
[string]$SessionRoot,
|
|
6
|
+
[string]$PricingPath = (Join-Path $PSScriptRoot 'codex-usage-pricing.json'),
|
|
7
|
+
[ValidateRange(1, 99)] [int]$PowerShellMajor = $PSVersionTable.PSVersion.Major
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
Set-StrictMode -Version Latest
|
|
11
|
+
$ErrorActionPreference = 'Stop'
|
|
12
|
+
if (-not $HooksPath) { $HooksPath = Join-Path $CodexHome 'hooks.json' }
|
|
13
|
+
if (-not $SessionRoot) { $SessionRoot = Join-Path $CodexHome 'sessions' }
|
|
14
|
+
|
|
15
|
+
$failures = [System.Collections.Generic.List[string]]::new()
|
|
16
|
+
function Write-Check {
|
|
17
|
+
param([bool]$Ok, [string]$Name, [string]$Detail)
|
|
18
|
+
$state = if ($Ok) { 'OK' } else { 'FAIL' }
|
|
19
|
+
$suffix = if ($Detail) { " — $Detail" } else { '' }
|
|
20
|
+
Write-Host "[$state] $Name$suffix"
|
|
21
|
+
if (-not $Ok) { $script:failures.Add($Name) }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
Write-Host 'Afyx Codex Usage Doctor'
|
|
25
|
+
Write-Host ''
|
|
26
|
+
$powerShellOk = $PowerShellMajor -ge 7
|
|
27
|
+
Write-Check $powerShellOk 'PowerShell 7+' $(if ($powerShellOk) { "$($PSVersionTable.PSVersion)" } else { 'Usage Tracker requires PowerShell 7+; Windows PowerShell 5.1 is unsupported' })
|
|
28
|
+
if (-not $powerShellOk) {
|
|
29
|
+
Write-Host ''
|
|
30
|
+
Write-Host 'Hook display path: UNKNOWN'
|
|
31
|
+
Write-Host 'NOT READY'
|
|
32
|
+
exit 1
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
$requiredFiles = @(
|
|
36
|
+
'CodexUsage.psm1',
|
|
37
|
+
'codex-usage-stop.ps1',
|
|
38
|
+
'codex-usage-watch.ps1',
|
|
39
|
+
'codex-usage-pricing.json',
|
|
40
|
+
'codex-usage-doctor.ps1'
|
|
41
|
+
)
|
|
42
|
+
$missingFiles = @($requiredFiles | Where-Object { -not (Test-Path -LiteralPath (Join-Path $PSScriptRoot $_) -PathType Leaf) })
|
|
43
|
+
Write-Check ($missingFiles.Count -eq 0) 'Tracker runtime files' $(if ($missingFiles.Count) { 'missing: ' + ($missingFiles -join ', ') } else { 'complete' })
|
|
44
|
+
|
|
45
|
+
$hooksDocument = $null
|
|
46
|
+
$hooksJsonOk = $false
|
|
47
|
+
if (Test-Path -LiteralPath $HooksPath -PathType Leaf) {
|
|
48
|
+
try {
|
|
49
|
+
$hooksDocument = Get-Content -Raw -LiteralPath $HooksPath | ConvertFrom-Json -Depth 50 -ErrorAction Stop
|
|
50
|
+
$hooksJsonOk = $true
|
|
51
|
+
} catch { }
|
|
52
|
+
}
|
|
53
|
+
Write-Check $hooksJsonOk 'Hooks JSON' $(if ($hooksJsonOk) { 'parseable' } else { 'missing or invalid' })
|
|
54
|
+
|
|
55
|
+
$expectedStopPath = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot 'codex-usage-stop.ps1'))
|
|
56
|
+
$afyxHandler = $null
|
|
57
|
+
if ($hooksJsonOk -and $hooksDocument.PSObject.Properties['hooks'] -and $hooksDocument.hooks.PSObject.Properties['Stop']) {
|
|
58
|
+
$afyxHandler = @($hooksDocument.hooks.Stop | ForEach-Object { $_.hooks } | Where-Object {
|
|
59
|
+
($_.PSObject.Properties['statusMessage'] -and [string]$_.statusMessage -eq 'Afyx Codex Usage Tracking') -or
|
|
60
|
+
($_.PSObject.Properties['commandWindows'] -and [string]$_.commandWindows -match '(?i)codex-usage-stop\.ps1') -or
|
|
61
|
+
($_.PSObject.Properties['command'] -and [string]$_.command -match '(?i)codex-usage-stop\.ps1')
|
|
62
|
+
} | Select-Object -First 1)
|
|
63
|
+
if ($afyxHandler.Count) { $afyxHandler = $afyxHandler[0] } else { $afyxHandler = $null }
|
|
64
|
+
}
|
|
65
|
+
Write-Check ($null -ne $afyxHandler) 'Stop hook configured' $(if ($afyxHandler) { 'Afyx handler found' } else { 'Afyx handler missing' })
|
|
66
|
+
$hookCommandOk = $false
|
|
67
|
+
if ($null -ne $afyxHandler) {
|
|
68
|
+
$command = if ($afyxHandler.PSObject.Properties['commandWindows']) { [string]$afyxHandler.commandWindows } else { [string]$afyxHandler.command }
|
|
69
|
+
$hookCommandOk = $command -match [regex]::Escape($expectedStopPath) -and (Test-Path -LiteralPath $expectedStopPath -PathType Leaf)
|
|
70
|
+
}
|
|
71
|
+
Write-Check $hookCommandOk 'Stop hook script' $(if ($hookCommandOk) { 'command resolves to installed script' } else { 'command path missing or unexpected' })
|
|
72
|
+
|
|
73
|
+
$debugLogPath = Join-Path $CodexHome 'tools\logs\codex-usage-debug.log'
|
|
74
|
+
$observedStage = 'HOOK_NOT_OBSERVED'
|
|
75
|
+
if (Test-Path -LiteralPath $debugLogPath -PathType Leaf) {
|
|
76
|
+
try {
|
|
77
|
+
$debugRecords = @(Get-Content -LiteralPath $debugLogPath -Tail 100 | ForEach-Object { $_ | ConvertFrom-Json -ErrorAction Stop })
|
|
78
|
+
if (@($debugRecords | Where-Object { $_.stage -eq 'HOOK_OBSERVED' }).Count -gt 0) {
|
|
79
|
+
$observedStage = [string]$debugRecords[-1].stage
|
|
80
|
+
}
|
|
81
|
+
} catch { $observedStage = 'DEBUG_LOG_INVALID' }
|
|
82
|
+
}
|
|
83
|
+
Write-Host "[INFO] Hook observation — $observedStage"
|
|
84
|
+
|
|
85
|
+
$sessionDirectoryOk = Test-Path -LiteralPath $SessionRoot -PathType Container
|
|
86
|
+
Write-Check $sessionDirectoryOk 'Sessions directory' $(if ($sessionDirectoryOk) { 'found' } else { 'missing' })
|
|
87
|
+
$latest = if ($sessionDirectoryOk) {
|
|
88
|
+
Get-ChildItem -LiteralPath $SessionRoot -Recurse -Filter 'rollout-*.jsonl' -File -ErrorAction SilentlyContinue |
|
|
89
|
+
Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1
|
|
90
|
+
} else { $null }
|
|
91
|
+
Write-Check ($null -ne $latest) 'Latest rollout' $(if ($latest) { 'found and readable' } else { 'not found' })
|
|
92
|
+
|
|
93
|
+
$pricing = $null
|
|
94
|
+
$pricingOk = $false
|
|
95
|
+
try {
|
|
96
|
+
Import-Module (Join-Path $PSScriptRoot 'CodexUsage.psm1') -Force -ErrorAction Stop | Out-Null
|
|
97
|
+
$pricing = Import-CodexPricing -Path $PricingPath
|
|
98
|
+
$pricingOk = $pricing.ContainsKey('unit_tokens') -and $pricing.ContainsKey('models')
|
|
99
|
+
} catch { }
|
|
100
|
+
Write-Check $pricingOk 'Pricing configuration' $(if ($pricingOk) { 'valid' } else { 'missing or invalid' })
|
|
101
|
+
|
|
102
|
+
$tokenEventFound = $false
|
|
103
|
+
$lastCompletion = $null
|
|
104
|
+
if ($latest -and $pricingOk) {
|
|
105
|
+
try {
|
|
106
|
+
$state = New-CodexUsageParserState
|
|
107
|
+
$stream = [IO.File]::Open($latest.FullName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::ReadWrite -bor [IO.FileShare]::Delete)
|
|
108
|
+
try {
|
|
109
|
+
$reader = [IO.StreamReader]::new($stream, [Text.Encoding]::UTF8, $true, 4096, $true)
|
|
110
|
+
try {
|
|
111
|
+
while (-not $reader.EndOfStream) {
|
|
112
|
+
$line = $reader.ReadLine()
|
|
113
|
+
if ($line -match '"type"\s*:\s*"token_usage_record"') { $tokenEventFound = $true }
|
|
114
|
+
$event = ConvertFrom-CodexTelemetryLine -Line $line
|
|
115
|
+
if ($null -eq $event) { continue }
|
|
116
|
+
$completion = Update-CodexUsageState -State $state -Event $event -Pricing $pricing -SessionFile $latest.FullName
|
|
117
|
+
if ($null -ne $completion) { $lastCompletion = $completion }
|
|
118
|
+
}
|
|
119
|
+
} finally { $reader.Dispose() }
|
|
120
|
+
} finally { $stream.Dispose() }
|
|
121
|
+
} catch { }
|
|
122
|
+
}
|
|
123
|
+
Write-Check $tokenEventFound 'Token usage event' $(if ($tokenEventFound) { 'detected' } else { 'not detected' })
|
|
124
|
+
Write-Check ($null -ne $lastCompletion) 'Latest completed turn' $(if ($lastCompletion) { 'parseable' } else { 'not parseable' })
|
|
125
|
+
|
|
126
|
+
$summaryOk = $false
|
|
127
|
+
if ($null -ne $lastCompletion) {
|
|
128
|
+
try { $summaryOk = -not [string]::IsNullOrWhiteSpace((Format-CodexUsageUiMessage -Completion $lastCompletion)) } catch { }
|
|
129
|
+
}
|
|
130
|
+
Write-Check $summaryOk 'Usage summary' $(if ($summaryOk) { 'generated' } else { 'not generated' })
|
|
131
|
+
|
|
132
|
+
$watcherReplayOk = $false
|
|
133
|
+
if ($latest -and $pricingOk -and (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'codex-usage-watch.ps1') -PathType Leaf)) {
|
|
134
|
+
try {
|
|
135
|
+
$output = & (Get-Command pwsh).Source -NoLogo -NoProfile -File (Join-Path $PSScriptRoot 'codex-usage-watch.ps1') `
|
|
136
|
+
-SessionRoot $SessionRoot -PricingPath $PricingPath -ReplayLatestCompletedTurns 1 -NoSnapshot 2>$null
|
|
137
|
+
$watcherReplayOk = $LASTEXITCODE -eq 0 -and (($output -join "`n") -match 'Codex Turn Usage')
|
|
138
|
+
} catch { }
|
|
139
|
+
}
|
|
140
|
+
Write-Check $watcherReplayOk 'Watcher replay' $(if ($watcherReplayOk) { 'latest completed turn replayed' } else { 'replay failed' })
|
|
141
|
+
|
|
142
|
+
Write-Host ''
|
|
143
|
+
$displayPath = if ($summaryOk -and $hookCommandOk) { 'CLIENT DISPLAY NOT VERIFIED' } else { 'UNKNOWN' }
|
|
144
|
+
Write-Host "Hook display path: $displayPath"
|
|
145
|
+
if ($failures.Count -eq 0) {
|
|
146
|
+
Write-Host 'READY'
|
|
147
|
+
exit 0
|
|
148
|
+
}
|
|
149
|
+
Write-Host 'NOT READY'
|
|
150
|
+
exit 1
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schema_version": 1,
|
|
3
|
+
"currency": "USD",
|
|
4
|
+
"unit_tokens": 1000000,
|
|
5
|
+
"service_tier": "standard",
|
|
6
|
+
"verified_at": "2026-09-24",
|
|
7
|
+
"source_url": "https://developers.openai.com/api/docs/pricing",
|
|
8
|
+
"models": {
|
|
9
|
+
"gpt-5.6-sol": {
|
|
10
|
+
"long_context_threshold_tokens": 272000,
|
|
11
|
+
"short": {
|
|
12
|
+
"input": 4.0,
|
|
13
|
+
"cached": 0.4,
|
|
14
|
+
"cache_write": 5.0,
|
|
15
|
+
"output": 20.0
|
|
16
|
+
},
|
|
17
|
+
"long": {
|
|
18
|
+
"input": 8.0,
|
|
19
|
+
"cached": 0.8,
|
|
20
|
+
"cache_write": 10.0,
|
|
21
|
+
"output": 30.0
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
[CmdletBinding()]
|
|
2
|
+
param(
|
|
3
|
+
[string]$CodexHome = $(if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $env:USERPROFILE '.codex' }),
|
|
4
|
+
[string]$PricingPath = (Join-Path $PSScriptRoot 'codex-usage-pricing.json'),
|
|
5
|
+
[string]$DebugLogPath,
|
|
6
|
+
[ValidateRange(0, 10)] [int]$RetryCount = 3,
|
|
7
|
+
[ValidateRange(0, 1000)] [int]$RetryMilliseconds = 150
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
Set-StrictMode -Version Latest
|
|
11
|
+
$ErrorActionPreference = 'Stop'
|
|
12
|
+
[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)
|
|
13
|
+
|
|
14
|
+
$debugEnabled = [string]$env:AFYX_CODEX_USAGE_DEBUG -match '^(?i:1|true|yes|on)$'
|
|
15
|
+
if (-not $DebugLogPath) { $DebugLogPath = Join-Path $CodexHome 'tools\logs\codex-usage-debug.log' }
|
|
16
|
+
$debugContext = [ordered]@{ hook_event = $null; session_id = $null; turn_id = $null }
|
|
17
|
+
$lastStage = 'HOOK_NOT_OBSERVED'
|
|
18
|
+
|
|
19
|
+
function Write-DebugStage {
|
|
20
|
+
param(
|
|
21
|
+
[Parameter(Mandatory)] [string]$Stage,
|
|
22
|
+
[string]$ErrorType,
|
|
23
|
+
[string]$Message
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
$script:lastStage = $Stage
|
|
27
|
+
if (-not $script:debugEnabled) { return }
|
|
28
|
+
try {
|
|
29
|
+
$parent = Split-Path -Parent $script:DebugLogPath
|
|
30
|
+
if ($parent) { [IO.Directory]::CreateDirectory($parent) | Out-Null }
|
|
31
|
+
if ((Test-Path -LiteralPath $script:DebugLogPath -PathType Leaf) -and
|
|
32
|
+
(Get-Item -LiteralPath $script:DebugLogPath).Length -ge 1MB) {
|
|
33
|
+
$rotated = "$script:DebugLogPath.1"
|
|
34
|
+
if (Test-Path -LiteralPath $rotated -PathType Leaf) { Remove-Item -LiteralPath $rotated -Force }
|
|
35
|
+
Move-Item -LiteralPath $script:DebugLogPath -Destination $rotated
|
|
36
|
+
}
|
|
37
|
+
$record = [ordered]@{
|
|
38
|
+
timestamp = [DateTime]::UtcNow.ToString('o')
|
|
39
|
+
stage = $Stage
|
|
40
|
+
hook_event = $script:debugContext.hook_event
|
|
41
|
+
session_id = $script:debugContext.session_id
|
|
42
|
+
turn_id = $script:debugContext.turn_id
|
|
43
|
+
error_type = $ErrorType
|
|
44
|
+
message = $Message
|
|
45
|
+
}
|
|
46
|
+
$line = ($record | ConvertTo-Json -Compress -Depth 5) + "`n"
|
|
47
|
+
[IO.File]::AppendAllText($script:DebugLogPath, $line, [Text.UTF8Encoding]::new($false))
|
|
48
|
+
} catch {
|
|
49
|
+
# Diagnostics are best-effort and must never affect the hook protocol.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function Write-HookResult {
|
|
54
|
+
param([string]$SystemMessage)
|
|
55
|
+
|
|
56
|
+
$result = [ordered]@{ continue = $true }
|
|
57
|
+
if ($SystemMessage) { $result.systemMessage = $SystemMessage }
|
|
58
|
+
[Console]::Out.WriteLine(($result | ConvertTo-Json -Compress -Depth 5))
|
|
59
|
+
[Console]::Out.Flush()
|
|
60
|
+
if ($SystemMessage) { Write-DebugStage -Stage 'MESSAGE_EMITTED' }
|
|
61
|
+
else { Write-DebugStage -Stage 'SAFE_RESPONSE_EMITTED' -Message $script:lastStage }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
Write-DebugStage -Stage 'HOOK_OBSERVED'
|
|
66
|
+
# last_assistant_message is intentionally never accessed, copied, logged, or hashed.
|
|
67
|
+
$rawEvent = [Console]::In.ReadToEnd()
|
|
68
|
+
if ([string]::IsNullOrWhiteSpace($rawEvent)) {
|
|
69
|
+
Write-DebugStage -Stage 'PAYLOAD_INVALID' -Message 'empty hook payload'
|
|
70
|
+
Write-HookResult
|
|
71
|
+
exit 0
|
|
72
|
+
}
|
|
73
|
+
try { $hookEvent = $rawEvent | ConvertFrom-Json -Depth 20 -ErrorAction Stop }
|
|
74
|
+
catch {
|
|
75
|
+
Write-DebugStage -Stage 'PAYLOAD_INVALID' -ErrorType $_.Exception.GetType().FullName -Message 'hook payload is not valid JSON'
|
|
76
|
+
Write-HookResult
|
|
77
|
+
exit 0
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
$debugContext.hook_event = if ($hookEvent.PSObject.Properties['hook_event_name']) { [string]$hookEvent.hook_event_name } else { $null }
|
|
81
|
+
if ($debugContext.hook_event -ne 'Stop') {
|
|
82
|
+
Write-DebugStage -Stage 'PAYLOAD_INVALID' -Message 'hook_event_name is not Stop'
|
|
83
|
+
Write-HookResult
|
|
84
|
+
exit 0
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
$turnId = if ($hookEvent.PSObject.Properties['turn_id']) { [string]$hookEvent.turn_id } else { $null }
|
|
88
|
+
$sessionId = if ($hookEvent.PSObject.Properties['session_id']) { [string]$hookEvent.session_id } else { $null }
|
|
89
|
+
$transcriptPath = if ($hookEvent.PSObject.Properties['transcript_path']) { [string]$hookEvent.transcript_path } else { $null }
|
|
90
|
+
$debugContext.session_id = $sessionId
|
|
91
|
+
$debugContext.turn_id = $turnId
|
|
92
|
+
if (-not $turnId) {
|
|
93
|
+
Write-DebugStage -Stage 'TURN_ID_MISSING' -Message 'turn_id is absent'
|
|
94
|
+
Write-HookResult
|
|
95
|
+
exit 0
|
|
96
|
+
}
|
|
97
|
+
if (-not $transcriptPath) {
|
|
98
|
+
Write-DebugStage -Stage 'TRANSCRIPT_NOT_FOUND' -Message 'transcript_path is absent'
|
|
99
|
+
Write-HookResult
|
|
100
|
+
exit 0
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
$sessionRoot = [IO.Path]::GetFullPath((Join-Path $CodexHome 'sessions'))
|
|
105
|
+
$resolvedTranscript = [IO.Path]::GetFullPath($transcriptPath)
|
|
106
|
+
} catch {
|
|
107
|
+
Write-DebugStage -Stage 'TRANSCRIPT_REJECTED' -ErrorType $_.Exception.GetType().FullName -Message 'transcript path is invalid'
|
|
108
|
+
Write-HookResult
|
|
109
|
+
exit 0
|
|
110
|
+
}
|
|
111
|
+
$rootPrefix = $sessionRoot.TrimEnd([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar) + [IO.Path]::DirectorySeparatorChar
|
|
112
|
+
if (-not $resolvedTranscript.StartsWith($rootPrefix, [StringComparison]::OrdinalIgnoreCase) -or
|
|
113
|
+
(-not $sessionId) -or (Split-Path -Leaf $resolvedTranscript) -notmatch [regex]::Escape($sessionId)) {
|
|
114
|
+
Write-DebugStage -Stage 'TRANSCRIPT_REJECTED' -Message 'transcript/session correlation rejected'
|
|
115
|
+
Write-HookResult
|
|
116
|
+
exit 0
|
|
117
|
+
}
|
|
118
|
+
if (-not (Test-Path -LiteralPath $resolvedTranscript -PathType Leaf)) {
|
|
119
|
+
Write-DebugStage -Stage 'TRANSCRIPT_NOT_FOUND' -Message 'correlated transcript does not exist'
|
|
120
|
+
Write-HookResult
|
|
121
|
+
exit 0
|
|
122
|
+
}
|
|
123
|
+
Write-DebugStage -Stage 'TRANSCRIPT_RESOLVED'
|
|
124
|
+
|
|
125
|
+
try { Import-Module (Join-Path $PSScriptRoot 'CodexUsage.psm1') -Force -ErrorAction Stop | Out-Null }
|
|
126
|
+
catch {
|
|
127
|
+
Write-DebugStage -Stage 'USAGE_PARSE_FAILED' -ErrorType $_.Exception.GetType().FullName -Message 'usage module import failed'
|
|
128
|
+
Write-HookResult
|
|
129
|
+
exit 0
|
|
130
|
+
}
|
|
131
|
+
try { $pricing = Import-CodexPricing -Path $PricingPath }
|
|
132
|
+
catch {
|
|
133
|
+
Write-DebugStage -Stage 'PRICING_FAILED' -ErrorType $_.Exception.GetType().FullName -Message 'pricing configuration could not be loaded'
|
|
134
|
+
Write-HookResult
|
|
135
|
+
exit 0
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
$completion = $null
|
|
139
|
+
$failureStage = $null
|
|
140
|
+
for ($attempt = 0; $attempt -le $RetryCount; $attempt++) {
|
|
141
|
+
$failureStage = $null
|
|
142
|
+
$failureStageReference = [ref]$failureStage
|
|
143
|
+
$completion = Get-CodexTurnCompletion -TranscriptPath $resolvedTranscript -TurnId $turnId -Pricing $pricing -FailureStage $failureStageReference
|
|
144
|
+
if ($null -ne $completion) { break }
|
|
145
|
+
if ($attempt -lt $RetryCount -and $RetryMilliseconds -gt 0) { Start-Sleep -Milliseconds $RetryMilliseconds }
|
|
146
|
+
}
|
|
147
|
+
if ($null -eq $completion) {
|
|
148
|
+
if ($failureStage -notin @('TURN_NOT_FOUND', 'USAGE_EVENT_NOT_FOUND', 'USAGE_PARSE_FAILED')) { $failureStage = 'USAGE_PARSE_FAILED' }
|
|
149
|
+
Write-DebugStage -Stage $failureStage -Message 'no safe completion could be produced'
|
|
150
|
+
Write-HookResult
|
|
151
|
+
exit 0
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
Write-DebugStage -Stage 'TURN_RESOLVED'
|
|
155
|
+
Write-DebugStage -Stage 'USAGE_EVENT_FOUND'
|
|
156
|
+
$message = Format-CodexUsageUiMessage -Completion $completion
|
|
157
|
+
if ([string]::IsNullOrWhiteSpace($message)) {
|
|
158
|
+
Write-DebugStage -Stage 'USAGE_PARSE_FAILED' -Message 'usage message formatter returned empty output'
|
|
159
|
+
Write-HookResult
|
|
160
|
+
exit 0
|
|
161
|
+
}
|
|
162
|
+
Write-DebugStage -Stage 'MESSAGE_GENERATED'
|
|
163
|
+
Write-HookResult -SystemMessage $message
|
|
164
|
+
} catch {
|
|
165
|
+
Write-DebugStage -Stage 'USAGE_PARSE_FAILED' -ErrorType $_.Exception.GetType().FullName -Message 'unexpected tracker failure'
|
|
166
|
+
Write-HookResult
|
|
167
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
[CmdletBinding()]
|
|
2
|
+
param(
|
|
3
|
+
[string]$CodexHome = $(if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $env:USERPROFILE '.codex' }),
|
|
4
|
+
[string]$SessionRoot,
|
|
5
|
+
[string]$PricingPath = (Join-Path $PSScriptRoot 'codex-usage-pricing.json'),
|
|
6
|
+
[string]$LatestUsagePath,
|
|
7
|
+
[ValidateRange(100, 60000)] [int]$PollMilliseconds = 750,
|
|
8
|
+
[ValidateRange(0, 100)] [int]$ReplayLatestCompletedTurns = 0,
|
|
9
|
+
[ValidateRange(0, 100)] [int]$ExitAfterCompletions = 0,
|
|
10
|
+
[switch]$NoSnapshot
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
Set-StrictMode -Version Latest
|
|
14
|
+
$ErrorActionPreference = 'Stop'
|
|
15
|
+
[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)
|
|
16
|
+
|
|
17
|
+
Import-Module (Join-Path $PSScriptRoot 'CodexUsage.psm1') -Force
|
|
18
|
+
$pricing = Import-CodexPricing -Path $PricingPath
|
|
19
|
+
|
|
20
|
+
if (-not $SessionRoot) { $SessionRoot = Join-Path $CodexHome 'sessions' }
|
|
21
|
+
if (-not $LatestUsagePath) { $LatestUsagePath = Join-Path $CodexHome 'tools\codex-usage-latest.json' }
|
|
22
|
+
|
|
23
|
+
function Find-LatestRollout {
|
|
24
|
+
if (-not (Test-Path -LiteralPath $SessionRoot -PathType Container)) { return $null }
|
|
25
|
+
return Get-ChildItem -LiteralPath $SessionRoot -Recurse -Filter 'rollout-*.jsonl' -File -ErrorAction SilentlyContinue |
|
|
26
|
+
Sort-Object LastWriteTimeUtc -Descending |
|
|
27
|
+
Select-Object -First 1
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function Read-AppendedTelemetry {
|
|
31
|
+
param([Parameter(Mandatory)] [hashtable]$Tracker)
|
|
32
|
+
|
|
33
|
+
$stream = [IO.File]::Open(
|
|
34
|
+
$Tracker.Path,
|
|
35
|
+
[IO.FileMode]::Open,
|
|
36
|
+
[IO.FileAccess]::Read,
|
|
37
|
+
[IO.FileShare]::ReadWrite -bor [IO.FileShare]::Delete
|
|
38
|
+
)
|
|
39
|
+
try {
|
|
40
|
+
if ($stream.Length -lt [long]$Tracker.Offset) {
|
|
41
|
+
return [pscustomobject]@{ Truncated = $true; Lines = @() }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
$remaining = $stream.Length - [long]$Tracker.Offset
|
|
45
|
+
if ($remaining -eq 0) {
|
|
46
|
+
return [pscustomobject]@{ Truncated = $false; Lines = @() }
|
|
47
|
+
}
|
|
48
|
+
if ($remaining -gt [int]::MaxValue) {
|
|
49
|
+
throw "Telemetry append is too large to process safely: $remaining bytes"
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
[void]$stream.Seek([long]$Tracker.Offset, [IO.SeekOrigin]::Begin)
|
|
53
|
+
$bytes = [byte[]]::new([int]$remaining)
|
|
54
|
+
$read = 0
|
|
55
|
+
while ($read -lt $bytes.Length) {
|
|
56
|
+
$count = $stream.Read($bytes, $read, $bytes.Length - $read)
|
|
57
|
+
if ($count -eq 0) { break }
|
|
58
|
+
$read += $count
|
|
59
|
+
}
|
|
60
|
+
$Tracker.Offset += $read
|
|
61
|
+
if ($read -eq 0) {
|
|
62
|
+
return [pscustomobject]@{ Truncated = $false; Lines = @() }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
$text = $Tracker.Buffer + [Text.Encoding]::UTF8.GetString($bytes, 0, $read)
|
|
66
|
+
$parts = [regex]::Split($text, "`n")
|
|
67
|
+
if ($text.EndsWith("`n", [StringComparison]::Ordinal)) {
|
|
68
|
+
$Tracker.Buffer = ''
|
|
69
|
+
$complete = if ($parts.Count -gt 1) { $parts[0..($parts.Count - 2)] } else { @() }
|
|
70
|
+
} else {
|
|
71
|
+
$Tracker.Buffer = $parts[-1]
|
|
72
|
+
$complete = if ($parts.Count -gt 1) { $parts[0..($parts.Count - 2)] } else { @() }
|
|
73
|
+
}
|
|
74
|
+
return [pscustomobject]@{
|
|
75
|
+
Truncated = $false
|
|
76
|
+
Lines = @($complete | ForEach-Object { $_.TrimEnd("`r") })
|
|
77
|
+
}
|
|
78
|
+
} finally {
|
|
79
|
+
$stream.Dispose()
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function Process-TelemetryLines {
|
|
84
|
+
param(
|
|
85
|
+
[Parameter(Mandatory)] [hashtable]$Tracker,
|
|
86
|
+
[Parameter(Mandatory)] [AllowEmptyCollection()] [string[]]$Lines
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
$completions = [System.Collections.Generic.List[object]]::new()
|
|
90
|
+
foreach ($line in $Lines) {
|
|
91
|
+
$event = ConvertFrom-CodexTelemetryLine -Line $line
|
|
92
|
+
if ($null -eq $event) { continue }
|
|
93
|
+
$completion = Update-CodexUsageState -State $Tracker.Parser -Event $event -Pricing $pricing -SessionFile $Tracker.Path
|
|
94
|
+
if ($null -ne $completion) { $completions.Add($completion) }
|
|
95
|
+
}
|
|
96
|
+
return $completions.ToArray()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function Initialize-RolloutTracker {
|
|
100
|
+
param([Parameter(Mandatory)] [string]$Path)
|
|
101
|
+
|
|
102
|
+
$tracker = @{
|
|
103
|
+
Path = [IO.Path]::GetFullPath($Path)
|
|
104
|
+
Offset = [long]0
|
|
105
|
+
Buffer = ''
|
|
106
|
+
Parser = New-CodexUsageParserState
|
|
107
|
+
}
|
|
108
|
+
$batch = Read-AppendedTelemetry -Tracker $tracker
|
|
109
|
+
$completions = Process-TelemetryLines -Tracker $tracker -Lines $batch.Lines
|
|
110
|
+
return [pscustomobject]@{ Tracker = $tracker; Completions = @($completions) }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function Write-LatestUsageSnapshot {
|
|
114
|
+
param([Parameter(Mandatory)] [object]$Completion)
|
|
115
|
+
|
|
116
|
+
if ($NoSnapshot) { return }
|
|
117
|
+
|
|
118
|
+
$parent = Split-Path -Parent $LatestUsagePath
|
|
119
|
+
if ($parent) { [IO.Directory]::CreateDirectory($parent) | Out-Null }
|
|
120
|
+
$snapshot = [ordered]@{
|
|
121
|
+
schema_version = 1
|
|
122
|
+
completed_at = $Completion.CompletedAt
|
|
123
|
+
model = $Completion.Model
|
|
124
|
+
input_tokens = $Completion.InputTokens
|
|
125
|
+
cached_input_tokens = $Completion.CachedInputTokens
|
|
126
|
+
cache_write_input_tokens = $Completion.CacheWriteInputTokens
|
|
127
|
+
uncached_input_tokens = $Completion.UncachedInputTokens
|
|
128
|
+
output_tokens = $Completion.OutputTokens
|
|
129
|
+
reasoning_tokens = if ($Completion.ReasoningAvailable) { $Completion.ReasoningTokens } else { $null }
|
|
130
|
+
total_tokens = $Completion.TotalTokens
|
|
131
|
+
api_equivalent_currency = if ($Completion.ApiEquivalentAvailable) { $pricing.currency } else { $null }
|
|
132
|
+
api_equivalent_cost = if ($Completion.ApiEquivalentAvailable) { $Completion.ApiEquivalentCost } else { $null }
|
|
133
|
+
usage_source = $Completion.UsageSource
|
|
134
|
+
session_file = $Completion.SessionFile
|
|
135
|
+
}
|
|
136
|
+
$json = $snapshot | ConvertTo-Json -Depth 5
|
|
137
|
+
$temporary = "$LatestUsagePath.tmp-$PID"
|
|
138
|
+
[IO.File]::WriteAllText($temporary, $json, [Text.UTF8Encoding]::new($false))
|
|
139
|
+
Move-Item -Force -LiteralPath $temporary -Destination $LatestUsagePath
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function Show-Completion {
|
|
143
|
+
param([Parameter(Mandatory)] [object]$Completion)
|
|
144
|
+
|
|
145
|
+
Format-CodexUsageSummary -Completion $Completion | ForEach-Object { Write-Host $_ }
|
|
146
|
+
Write-LatestUsageSnapshot -Completion $Completion
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
$latest = Find-LatestRollout
|
|
150
|
+
if ($null -eq $latest) {
|
|
151
|
+
throw "No Codex rollout files found under $SessionRoot"
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
$initial = Initialize-RolloutTracker -Path $latest.FullName
|
|
155
|
+
if ($ReplayLatestCompletedTurns -gt 0) {
|
|
156
|
+
$selected = @($initial.Completions | Select-Object -Last $ReplayLatestCompletedTurns)
|
|
157
|
+
foreach ($completion in $selected) { Show-Completion $completion }
|
|
158
|
+
if ($selected.Count -eq 0) {
|
|
159
|
+
Write-Host 'No completed Codex turns found in the latest rollout file.'
|
|
160
|
+
}
|
|
161
|
+
exit 0
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
$trackers = @{}
|
|
165
|
+
$trackers[$initial.Tracker.Path] = $initial.Tracker
|
|
166
|
+
$activePath = $initial.Tracker.Path
|
|
167
|
+
$emitted = 0
|
|
168
|
+
$watcherStartedUtc = [DateTime]::UtcNow
|
|
169
|
+
$lastFallbackDiscovery = [DateTime]::UtcNow
|
|
170
|
+
|
|
171
|
+
$fileWatcher = [IO.FileSystemWatcher]::new($SessionRoot, '*')
|
|
172
|
+
$fileWatcher.IncludeSubdirectories = $true
|
|
173
|
+
$fileWatcher.NotifyFilter = [IO.NotifyFilters]::FileName -bor [IO.NotifyFilters]::DirectoryName -bor [IO.NotifyFilters]::LastWrite -bor [IO.NotifyFilters]::Size
|
|
174
|
+
$fileWatcher.EnableRaisingEvents = $true
|
|
175
|
+
|
|
176
|
+
Write-Host "Codex usage watcher ready. Monitoring $SessionRoot"
|
|
177
|
+
Write-Host 'Only token metadata, model, timestamps, and turn boundaries are processed.'
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
while ($true) {
|
|
181
|
+
$change = $fileWatcher.WaitForChanged(
|
|
182
|
+
[IO.WatcherChangeTypes]::Changed -bor [IO.WatcherChangeTypes]::Created -bor [IO.WatcherChangeTypes]::Renamed,
|
|
183
|
+
$PollMilliseconds
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
$pathsToRead = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
|
|
187
|
+
$newFileCompletions = [System.Collections.Generic.List[object]]::new()
|
|
188
|
+
$discoverLatest = $false
|
|
189
|
+
if (-not $change.TimedOut -and $change.Name) {
|
|
190
|
+
$candidate = [IO.Path]::GetFullPath((Join-Path $SessionRoot $change.Name))
|
|
191
|
+
if ((Split-Path -Leaf $candidate) -like 'rollout-*.jsonl' -and (Test-Path -LiteralPath $candidate -PathType Leaf)) {
|
|
192
|
+
if (-not $trackers.ContainsKey($candidate)) {
|
|
193
|
+
$created = Initialize-RolloutTracker -Path $candidate
|
|
194
|
+
$trackers[$candidate] = $created.Tracker
|
|
195
|
+
if ((Get-Item -LiteralPath $candidate).CreationTimeUtc -ge $watcherStartedUtc.AddSeconds(-1)) {
|
|
196
|
+
foreach ($completion in $created.Completions) { $newFileCompletions.Add($completion) }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
$activePath = $candidate
|
|
200
|
+
[void]$pathsToRead.Add($candidate)
|
|
201
|
+
} else {
|
|
202
|
+
$discoverLatest = $true
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
[void]$pathsToRead.Add($activePath)
|
|
207
|
+
if ($discoverLatest -or ([DateTime]::UtcNow - $lastFallbackDiscovery).TotalSeconds -ge 2) {
|
|
208
|
+
$fallback = Find-LatestRollout
|
|
209
|
+
$lastFallbackDiscovery = [DateTime]::UtcNow
|
|
210
|
+
if ($null -ne $fallback) {
|
|
211
|
+
$fallbackPath = [IO.Path]::GetFullPath($fallback.FullName)
|
|
212
|
+
if (-not $trackers.ContainsKey($fallbackPath)) {
|
|
213
|
+
$created = Initialize-RolloutTracker -Path $fallbackPath
|
|
214
|
+
$trackers[$fallbackPath] = $created.Tracker
|
|
215
|
+
if ($fallback.CreationTimeUtc -ge $watcherStartedUtc.AddSeconds(-1)) {
|
|
216
|
+
foreach ($completion in $created.Completions) { $newFileCompletions.Add($completion) }
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
$activePath = $fallbackPath
|
|
220
|
+
[void]$pathsToRead.Add($fallbackPath)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
foreach ($completion in $newFileCompletions) {
|
|
225
|
+
Show-Completion $completion
|
|
226
|
+
$emitted++
|
|
227
|
+
if ($ExitAfterCompletions -gt 0 -and $emitted -ge $ExitAfterCompletions) { return }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
foreach ($path in $pathsToRead) {
|
|
231
|
+
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { continue }
|
|
232
|
+
$tracker = $trackers[$path]
|
|
233
|
+
$batch = Read-AppendedTelemetry -Tracker $tracker
|
|
234
|
+
if ($batch.Truncated) {
|
|
235
|
+
Write-Warning "Telemetry file was truncated or recreated; rebuilding baseline for $(Split-Path -Leaf $path)."
|
|
236
|
+
$rebuilt = Initialize-RolloutTracker -Path $path
|
|
237
|
+
$trackers[$path] = $rebuilt.Tracker
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
240
|
+
$completions = @(Process-TelemetryLines -Tracker $tracker -Lines $batch.Lines)
|
|
241
|
+
foreach ($completion in $completions) {
|
|
242
|
+
Show-Completion $completion
|
|
243
|
+
$emitted++
|
|
244
|
+
if ($ExitAfterCompletions -gt 0 -and $emitted -ge $ExitAfterCompletions) { return }
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} finally {
|
|
249
|
+
$fileWatcher.Dispose()
|
|
250
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "2.0.0",
|
|
3
|
+
"tasks": [
|
|
4
|
+
{
|
|
5
|
+
"label": "Codex: Watch Token Usage",
|
|
6
|
+
"type": "process",
|
|
7
|
+
"command": "pwsh",
|
|
8
|
+
"args": [
|
|
9
|
+
"-NoLogo",
|
|
10
|
+
"-NoProfile",
|
|
11
|
+
"-ExecutionPolicy",
|
|
12
|
+
"Bypass",
|
|
13
|
+
"-File",
|
|
14
|
+
"${env:USERPROFILE}\\.codex\\tools\\codex-usage-watch.ps1"
|
|
15
|
+
],
|
|
16
|
+
"isBackground": true,
|
|
17
|
+
"problemMatcher": [],
|
|
18
|
+
"presentation": {
|
|
19
|
+
"echo": false,
|
|
20
|
+
"reveal": "always",
|
|
21
|
+
"focus": false,
|
|
22
|
+
"panel": "dedicated",
|
|
23
|
+
"showReuseMessage": false,
|
|
24
|
+
"clear": true
|
|
25
|
+
},
|
|
26
|
+
"runOptions": {
|
|
27
|
+
"instanceLimit": 1
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|