@openai/codex-security 0.8.2 → 0.9.1
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/README.md +201 -178
- package/_bundled_plugin/.app.json +4 -0
- package/_bundled_plugin/.mcp.json +6 -1
- package/_bundled_plugin/examples/completed-scan/findings.json +16 -4
- package/_bundled_plugin/examples/completed-scan/report.md +11 -8
- package/_bundled_plugin/examples/completed-scan/scan-manifest.json +4 -2
- package/_bundled_plugin/mcp/fzstd-LICENSE.txt +21 -0
- package/_bundled_plugin/mcp/helpers.mjs +2 -11
- package/_bundled_plugin/mcp/helpers.mjs.br +0 -0
- package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +3 -210
- package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
- package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
- package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
- package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
- package/_bundled_plugin/mcp/package.json +1 -0
- package/_bundled_plugin/mcp/permission-profile-preflight.mjs +51 -14
- package/_bundled_plugin/mcp/server.mjs +2 -11
- package/_bundled_plugin/mcp/server.mjs.br +0 -0
- package/_bundled_plugin/mcp/session-decompression.mjs +712 -0
- package/_bundled_plugin/references/artifact-storage.md +1 -1
- package/_bundled_plugin/references/core-scan.md +14 -5
- package/_bundled_plugin/references/default-severity-rubric.md +33 -0
- package/_bundled_plugin/references/final-report.md +5 -3
- package/_bundled_plugin/references/finding-detail-fields.md +19 -18
- package/_bundled_plugin/references/sarif-adapter.md +8 -2
- package/_bundled_plugin/references/scan-artifacts.md +21 -12
- package/_bundled_plugin/references/scan-contract.md +27 -33
- package/_bundled_plugin/references/threat-model.md +1 -0
- package/_bundled_plugin/schemas/tools/scan-draft.schema.json +2 -2
- package/_bundled_plugin/scripts/check_deep_scan_target.py +97 -0
- package/_bundled_plugin/scripts/codex_profile.mjs +7 -1
- package/_bundled_plugin/scripts/deep_scan_config.py +20 -1
- package/_bundled_plugin/scripts/deep_scan_workbench.py +188 -454
- package/_bundled_plugin/scripts/filesystem_identity.py +1 -0
- package/_bundled_plugin/scripts/finalize_scan_contract.py +317 -377
- package/_bundled_plugin/scripts/finding_preview.py +25 -29
- package/_bundled_plugin/scripts/launch_codex_security_mcp +1 -0
- package/_bundled_plugin/scripts/legacy_findings.py +148 -0
- package/_bundled_plugin/scripts/posix_scan_local_files.py +198 -0
- package/_bundled_plugin/scripts/read_codex_session.mjs +21 -0
- package/_bundled_plugin/scripts/report_projection.py +167 -178
- package/_bundled_plugin/scripts/review_state.py +8 -2
- package/_bundled_plugin/scripts/servicenow_avit.py +20 -0
- package/_bundled_plugin/scripts/snapshot_sqlite.py +1 -0
- package/_bundled_plugin/scripts/threat_model_projection.py +22 -26
- package/_bundled_plugin/scripts/validate_scan_contract.py +9 -7
- package/_bundled_plugin/scripts/windows_scan_local_files.py +600 -72
- package/_bundled_plugin/scripts/workbench/__init__.py +1 -1
- package/_bundled_plugin/scripts/workbench/handoff.py +47 -0
- package/_bundled_plugin/scripts/workbench/retained_coverage.py +38 -75
- package/_bundled_plugin/scripts/workbench/retained_records.py +18 -30
- package/_bundled_plugin/scripts/workbench/runtime_toml.py +4 -1
- package/_bundled_plugin/scripts/workbench/saved_coverage.py +152 -248
- package/_bundled_plugin/scripts/workbench/saved_outputs.py +9 -0
- package/_bundled_plugin/scripts/workbench/saved_sources.py +47 -40
- package/_bundled_plugin/scripts/workbench/storage.py +71 -7
- package/_bundled_plugin/scripts/workbench_cli.py +121 -14
- package/_bundled_plugin/scripts/workbench_constants.py +19 -0
- package/_bundled_plugin/scripts/workbench_coverage_receipts.py +3 -3
- package/_bundled_plugin/scripts/workbench_db.py +550 -492
- package/_bundled_plugin/scripts/workbench_feedback.py +8 -7
- package/_bundled_plugin/scripts/workbench_finding_index.py +1 -0
- package/_bundled_plugin/scripts/workbench_finding_workflows.py +90 -4
- package/_bundled_plugin/scripts/workbench_native_indexes.py +29 -33
- package/_bundled_plugin/scripts/workbench_progress.py +10 -1
- package/_bundled_plugin/scripts/workbench_publication.py +14 -10
- package/_bundled_plugin/scripts/workbench_remediation.py +508 -11
- package/_bundled_plugin/scripts/workbench_saved_results.py +132 -77
- package/_bundled_plugin/scripts/workbench_scan_history.py +139 -80
- package/_bundled_plugin/scripts/workbench_scan_start.py +84 -25
- package/_bundled_plugin/scripts/workbench_scan_usage.py +666 -117
- package/_bundled_plugin/scripts/workbench_schema.py +44 -55
- package/_bundled_plugin/scripts/workbench_setup.py +251 -0
- package/_bundled_plugin/scripts/workbench_source_excerpt.py +13 -0
- package/_bundled_plugin/scripts/workbench_source_scopes.py +33 -23
- package/_bundled_plugin/scripts/workbench_target.py +330 -66
- package/_bundled_plugin/scripts/workbench_target_state.py +3 -0
- package/_bundled_plugin/scripts/workbench_validation.py +44 -12
- package/_bundled_plugin/shared/workbench-history-repairs.json +19 -16
- package/_bundled_plugin/shared/workbench-migrations.json +53 -0
- package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -1
- package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +4 -68
- package/_bundled_plugin/skills/check-skills/SKILL.md +43 -0
- package/_bundled_plugin/skills/check-skills/agents/openai.yaml +4 -0
- package/_bundled_plugin/skills/cloud-findings/SKILL.md +6 -4
- package/_bundled_plugin/skills/deep-security-scan/SKILL.md +7 -50
- package/_bundled_plugin/skills/fix-finding/SKILL.md +19 -2
- package/_bundled_plugin/skills/publish-findings/SKILL.md +81 -45
- package/_bundled_plugin/skills/publish-findings/references/checkmarx-import.md +37 -0
- package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +5 -24
- package/_bundled_plugin/skills/publish-findings/scripts/collect_checkmarx.py +464 -0
- package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +36 -9
- package/_bundled_plugin/skills/track-findings/SKILL.md +5 -3
- package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +22 -68
- package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +12 -6
- package/dist/api.d.ts +45 -5
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +1144 -411
- package/dist/api.js.map +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +10 -7
- package/dist/auth.js.map +1 -1
- package/dist/bedrock-oss-pricing.d.ts +6 -0
- package/dist/bedrock-oss-pricing.d.ts.map +1 -0
- package/dist/bedrock-oss-pricing.js +58 -0
- package/dist/bedrock-oss-pricing.js.map +1 -0
- package/dist/bedrock-pricing.d.ts +15 -0
- package/dist/bedrock-pricing.d.ts.map +1 -0
- package/dist/bedrock-pricing.js +117 -0
- package/dist/bedrock-pricing.js.map +1 -0
- package/dist/blackduck-findings.d.ts +8 -0
- package/dist/blackduck-findings.d.ts.map +1 -0
- package/dist/blackduck-findings.js +320 -0
- package/dist/blackduck-findings.js.map +1 -0
- package/dist/bulk-scan-discovery.d.ts.map +1 -1
- package/dist/bulk-scan-discovery.js +51 -36
- package/dist/bulk-scan-discovery.js.map +1 -1
- package/dist/checkmarx-evidence.d.ts +19 -0
- package/dist/checkmarx-evidence.d.ts.map +1 -0
- package/dist/checkmarx-evidence.js +36 -0
- package/dist/checkmarx-evidence.js.map +1 -0
- package/dist/checkmarx-findings.d.ts +4 -0
- package/dist/checkmarx-findings.d.ts.map +1 -0
- package/dist/checkmarx-findings.js +27 -0
- package/dist/checkmarx-findings.js.map +1 -0
- package/dist/checkmarx-one.d.ts +4 -0
- package/dist/checkmarx-one.d.ts.map +1 -0
- package/dist/checkmarx-one.js +53 -0
- package/dist/checkmarx-one.js.map +1 -0
- package/dist/checkmarx-repository.d.ts +12 -0
- package/dist/checkmarx-repository.d.ts.map +1 -0
- package/dist/checkmarx-repository.js +181 -0
- package/dist/checkmarx-repository.js.map +1 -0
- package/dist/checkmarx-xml.d.ts +12 -0
- package/dist/checkmarx-xml.d.ts.map +1 -0
- package/dist/checkmarx-xml.js +196 -0
- package/dist/checkmarx-xml.js.map +1 -0
- package/dist/classify-severity-records.d.ts +6 -9
- package/dist/classify-severity-records.d.ts.map +1 -1
- package/dist/classify-severity-records.js +9 -21
- package/dist/classify-severity-records.js.map +1 -1
- package/dist/classify-severity.d.ts +4 -1
- package/dist/classify-severity.d.ts.map +1 -1
- package/dist/classify-severity.js +30 -13
- package/dist/classify-severity.js.map +1 -1
- package/dist/cli.d.ts +14 -5
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +2112 -1266
- package/dist/cli.js.map +1 -1
- package/dist/cloud-publish.js +2 -2
- package/dist/cloud-publish.js.map +1 -1
- package/dist/codex-prompt.d.ts +2 -0
- package/dist/codex-prompt.d.ts.map +1 -1
- package/dist/codex-prompt.js +6 -0
- package/dist/codex-prompt.js.map +1 -1
- package/dist/component-scan.d.ts +16 -3
- package/dist/component-scan.d.ts.map +1 -1
- package/dist/component-scan.js +32 -6
- package/dist/component-scan.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +221 -65
- package/dist/config.js.map +1 -1
- package/dist/continuum-findings.d.ts +7 -0
- package/dist/continuum-findings.d.ts.map +1 -0
- package/dist/continuum-findings.js +225 -0
- package/dist/continuum-findings.js.map +1 -0
- package/dist/contract-values.d.ts +30 -0
- package/dist/contract-values.d.ts.map +1 -0
- package/dist/contract-values.js +78 -0
- package/dist/contract-values.js.map +1 -0
- package/dist/contract.d.ts +2 -20
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +58 -669
- package/dist/contract.js.map +1 -1
- package/dist/cost-model.d.ts +13 -1
- package/dist/cost-model.d.ts.map +1 -1
- package/dist/cost-model.js +66 -115
- package/dist/cost-model.js.map +1 -1
- package/dist/cost.d.ts +61 -4
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +2560 -100
- package/dist/cost.js.map +1 -1
- package/dist/custom-publish.d.ts +4 -0
- package/dist/custom-publish.d.ts.map +1 -1
- package/dist/custom-publish.js +40 -3
- package/dist/custom-publish.js.map +1 -1
- package/dist/custom-validation-prompt.d.ts +1 -1
- package/dist/custom-validation-prompt.d.ts.map +1 -1
- package/dist/custom-validation-prompt.js +5 -0
- package/dist/custom-validation-prompt.js.map +1 -1
- package/dist/custom-validation.d.ts +2 -0
- package/dist/custom-validation.d.ts.map +1 -1
- package/dist/custom-validation.js +4 -0
- package/dist/custom-validation.js.map +1 -1
- package/dist/deduplication/deduplication-prompts.d.ts +4 -4
- package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
- package/dist/deduplication/deduplication-prompts.js +2 -2
- package/dist/deduplication/deduplication-prompts.js.map +1 -1
- package/dist/deduplication/deduplication-reviewer.d.ts +14 -11
- package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
- package/dist/deduplication/deduplication-reviewer.js +11 -4
- package/dist/deduplication/deduplication-reviewer.js.map +1 -1
- package/dist/deduplication/deduplication.js.map +1 -1
- package/dist/deduplication/finding-schema.d.ts +19 -1
- package/dist/deduplication/finding-schema.d.ts.map +1 -1
- package/dist/deduplication/finding-schema.js +25 -0
- package/dist/deduplication/finding-schema.js.map +1 -1
- package/dist/deduplication/local.d.ts +2 -2
- package/dist/deduplication/local.d.ts.map +1 -1
- package/dist/deduplication/local.js +5 -10
- package/dist/deduplication/local.js.map +1 -1
- package/dist/deduplication/records.d.ts +8 -2
- package/dist/deduplication/records.d.ts.map +1 -1
- package/dist/deduplication/records.js +36 -8
- package/dist/deduplication/records.js.map +1 -1
- package/dist/deduplication/refusal.d.ts.map +1 -1
- package/dist/deduplication/refusal.js +25 -5
- package/dist/deduplication/refusal.js.map +1 -1
- package/dist/deduplication/scan.d.ts +3 -0
- package/dist/deduplication/scan.d.ts.map +1 -1
- package/dist/deduplication/scan.js +1 -1
- package/dist/deduplication/scan.js.map +1 -1
- package/dist/deep-config.d.ts +0 -1
- package/dist/deep-config.d.ts.map +1 -1
- package/dist/deep-config.js +22 -33
- package/dist/deep-config.js.map +1 -1
- package/dist/deep-scan-defaults.d.ts +2 -0
- package/dist/deep-scan-defaults.d.ts.map +1 -1
- package/dist/deep-scan-defaults.js +28 -1
- package/dist/deep-scan-defaults.js.map +1 -1
- package/dist/deep-scan.d.ts +13 -0
- package/dist/deep-scan.d.ts.map +1 -1
- package/dist/deep-scan.js +262 -7
- package/dist/deep-scan.js.map +1 -1
- package/dist/default-severity-rubric.d.ts +1 -2
- package/dist/default-severity-rubric.d.ts.map +1 -1
- package/dist/default-severity-rubric.js +2 -66
- package/dist/default-severity-rubric.js.map +1 -1
- package/dist/endor-findings.d.ts +3 -1
- package/dist/endor-findings.d.ts.map +1 -1
- package/dist/endor-findings.js +108 -80
- package/dist/endor-findings.js.map +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +9 -2
- package/dist/errors.js.map +1 -1
- package/dist/external-findings-import.d.ts +5 -3
- package/dist/external-findings-import.d.ts.map +1 -1
- package/dist/external-findings-import.js +11 -16
- package/dist/external-findings-import.js.map +1 -1
- package/dist/external-findings-local.d.ts +15 -4
- package/dist/external-findings-local.d.ts.map +1 -1
- package/dist/external-findings-local.js +619 -225
- package/dist/external-findings-local.js.map +1 -1
- package/dist/external-findings-publish.d.ts +20 -6
- package/dist/external-findings-publish.d.ts.map +1 -1
- package/dist/external-findings-publish.js +2356 -308
- package/dist/external-findings-publish.js.map +1 -1
- package/dist/external-findings-read.d.ts +28 -2
- package/dist/external-findings-read.d.ts.map +1 -1
- package/dist/external-findings-read.js +174 -81
- package/dist/external-findings-read.js.map +1 -1
- package/dist/external-findings.d.ts +17 -3
- package/dist/external-findings.d.ts.map +1 -1
- package/dist/external-findings.js +153 -1
- package/dist/external-findings.js.map +1 -1
- package/dist/external-import-contract.d.ts +33 -1
- package/dist/external-import-contract.d.ts.map +1 -1
- package/dist/external-import-contract.js +335 -51
- package/dist/external-import-contract.js.map +1 -1
- package/dist/external-import-models.d.ts +1 -1
- package/dist/external-import-models.d.ts.map +1 -1
- package/dist/external-import-models.js.map +1 -1
- package/dist/feedback.d.ts.map +1 -1
- package/dist/feedback.js +5 -4
- package/dist/feedback.js.map +1 -1
- package/dist/finding-prompt.d.ts +5 -0
- package/dist/finding-prompt.d.ts.map +1 -0
- package/dist/finding-prompt.js +32 -0
- package/dist/finding-prompt.js.map +1 -0
- package/dist/finding-retrieval.d.ts +3 -3
- package/dist/finding-retrieval.d.ts.map +1 -1
- package/dist/finding-retrieval.js.map +1 -1
- package/dist/finding-workflow.d.ts +6 -1
- package/dist/finding-workflow.d.ts.map +1 -1
- package/dist/finding-workflow.js +2 -2
- package/dist/finding-workflow.js.map +1 -1
- package/dist/findings-client.d.ts +1 -0
- package/dist/findings-client.d.ts.map +1 -1
- package/dist/findings-client.js +27 -3
- package/dist/findings-client.js.map +1 -1
- package/dist/git-remote.d.ts +4 -0
- package/dist/git-remote.d.ts.map +1 -1
- package/dist/git-remote.js +20 -0
- package/dist/git-remote.js.map +1 -1
- package/dist/github-advisories.d.ts +3 -12
- package/dist/github-advisories.d.ts.map +1 -1
- package/dist/github-advisories.js +9 -31
- package/dist/github-advisories.js.map +1 -1
- package/dist/github-dependabot-findings.d.ts.map +1 -1
- package/dist/github-dependabot-findings.js +22 -20
- package/dist/github-dependabot-findings.js.map +1 -1
- package/dist/github-findings.d.ts +4 -1
- package/dist/github-findings.d.ts.map +1 -1
- package/dist/github-findings.js +392 -79
- package/dist/github-findings.js.map +1 -1
- package/dist/github.d.ts +17 -7
- package/dist/github.d.ts.map +1 -1
- package/dist/github.js +43 -30
- package/dist/github.js.map +1 -1
- package/dist/host-protocol.d.ts +2 -2
- package/dist/host-protocol.d.ts.map +1 -1
- package/dist/host-protocol.js.map +1 -1
- package/dist/host-records-protocol.d.ts +10 -11
- package/dist/host-records-protocol.d.ts.map +1 -1
- package/dist/host-records-protocol.js +69 -9
- package/dist/host-records-protocol.js.map +1 -1
- package/dist/hosted-git-remotes.d.ts +5 -0
- package/dist/hosted-git-remotes.d.ts.map +1 -0
- package/dist/hosted-git-remotes.js +285 -0
- package/dist/hosted-git-remotes.js.map +1 -0
- package/dist/hosted-git-repository.d.ts +28 -0
- package/dist/hosted-git-repository.d.ts.map +1 -0
- package/dist/hosted-git-repository.js +222 -0
- package/dist/hosted-git-repository.js.map +1 -0
- package/dist/hosted-scan-protocol.js +2 -2
- package/dist/hosted-scan-protocol.js.map +1 -1
- package/dist/hosted-scan.d.ts +15 -0
- package/dist/hosted-scan.d.ts.map +1 -1
- package/dist/hosted-scan.js +71 -44
- package/dist/hosted-scan.js.map +1 -1
- package/dist/hosted-scanner-http.d.ts +21 -0
- package/dist/hosted-scanner-http.d.ts.map +1 -0
- package/dist/hosted-scanner-http.js +204 -0
- package/dist/hosted-scanner-http.js.map +1 -0
- package/dist/hosted-scanner-v1.d.ts +6 -0
- package/dist/hosted-scanner-v1.d.ts.map +1 -0
- package/dist/hosted-scanner-v1.js +60 -0
- package/dist/hosted-scanner-v1.js.map +1 -0
- package/dist/import-scan.d.ts.map +1 -1
- package/dist/import-scan.js +3 -0
- package/dist/import-scan.js.map +1 -1
- package/dist/index.d.ts +24 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -1
- package/dist/index.js.map +1 -1
- package/dist/inspector-findings.d.ts.map +1 -1
- package/dist/inspector-findings.js +58 -81
- package/dist/inspector-findings.js.map +1 -1
- package/dist/knowledge-base.d.ts.map +1 -1
- package/dist/knowledge-base.js +86 -10
- package/dist/knowledge-base.js.map +1 -1
- package/dist/local-git-metadata.d.ts +5 -0
- package/dist/local-git-metadata.d.ts.map +1 -0
- package/dist/local-git-metadata.js +130 -0
- package/dist/local-git-metadata.js.map +1 -0
- package/dist/multiscan.d.ts +6 -4
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +160 -77
- package/dist/multiscan.js.map +1 -1
- package/dist/native-json-findings.d.ts +2 -1
- package/dist/native-json-findings.d.ts.map +1 -1
- package/dist/native-json-findings.js +12 -2
- package/dist/native-json-findings.js.map +1 -1
- package/dist/opengrep-findings.d.ts +1 -0
- package/dist/opengrep-findings.d.ts.map +1 -1
- package/dist/opengrep-findings.js +49 -31
- package/dist/opengrep-findings.js.map +1 -1
- package/dist/operation-telemetry.d.ts +104 -0
- package/dist/operation-telemetry.d.ts.map +1 -0
- package/dist/operation-telemetry.js +293 -0
- package/dist/operation-telemetry.js.map +1 -0
- package/dist/patch-tui.d.ts.map +1 -1
- package/dist/patch-tui.js +90 -35
- package/dist/patch-tui.js.map +1 -1
- package/dist/plugin-python-probe.d.ts +4 -0
- package/dist/plugin-python-probe.d.ts.map +1 -0
- package/dist/plugin-python-probe.js +10 -0
- package/dist/plugin-python-probe.js.map +1 -0
- package/dist/private-finding-import.d.ts +341 -0
- package/dist/private-finding-import.d.ts.map +1 -0
- package/dist/private-finding-import.js +411 -0
- package/dist/private-finding-import.js.map +1 -0
- package/dist/project-config.d.ts.map +1 -1
- package/dist/project-config.js +2 -2
- package/dist/project-config.js.map +1 -1
- package/dist/provider-profile.d.ts +11 -3
- package/dist/provider-profile.d.ts.map +1 -1
- package/dist/provider-profile.js +80 -11
- package/dist/provider-profile.js.map +1 -1
- package/dist/publication-telemetry.d.ts +45 -0
- package/dist/publication-telemetry.d.ts.map +1 -0
- package/dist/publication-telemetry.js +50 -0
- package/dist/publication-telemetry.js.map +1 -0
- package/dist/publication.js +7 -1
- package/dist/publication.js.map +1 -1
- package/dist/publish.d.ts +4 -0
- package/dist/publish.d.ts.map +1 -1
- package/dist/publish.js +42 -2
- package/dist/publish.js.map +1 -1
- package/dist/reader-selection.d.ts +15 -0
- package/dist/reader-selection.d.ts.map +1 -0
- package/dist/reader-selection.js +89 -0
- package/dist/reader-selection.js.map +1 -0
- package/dist/record.d.ts +6 -1
- package/dist/record.d.ts.map +1 -1
- package/dist/record.js +53 -2
- package/dist/record.js.map +1 -1
- package/dist/remediation.d.ts +3 -1
- package/dist/remediation.d.ts.map +1 -1
- package/dist/remediation.js +28 -2
- package/dist/remediation.js.map +1 -1
- package/dist/request-metadata.d.ts +3 -0
- package/dist/request-metadata.d.ts.map +1 -1
- package/dist/request-metadata.js +9 -0
- package/dist/request-metadata.js.map +1 -1
- package/dist/result.d.ts +6 -0
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +3 -1
- package/dist/result.js.map +1 -1
- package/dist/retry-windows-rename.d.ts +8 -0
- package/dist/retry-windows-rename.d.ts.map +1 -0
- package/dist/retry-windows-rename.js +24 -0
- package/dist/retry-windows-rename.js.map +1 -0
- package/dist/runtime.d.ts +22 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +408 -162
- package/dist/runtime.js.map +1 -1
- package/dist/saved-scan-bootstrap.d.ts +1 -0
- package/dist/saved-scan-bootstrap.d.ts.map +1 -1
- package/dist/saved-scan-bootstrap.js +18 -15
- package/dist/saved-scan-bootstrap.js.map +1 -1
- package/dist/scan-activity.d.ts.map +1 -1
- package/dist/scan-activity.js +172 -64
- package/dist/scan-activity.js.map +1 -1
- package/dist/scan-comparison.d.ts +10 -5
- package/dist/scan-comparison.d.ts.map +1 -1
- package/dist/scan-comparison.js +68 -25
- package/dist/scan-comparison.js.map +1 -1
- package/dist/scan-dashboard.d.ts +7 -6
- package/dist/scan-dashboard.d.ts.map +1 -1
- package/dist/scan-dashboard.js +91 -79
- package/dist/scan-dashboard.js.map +1 -1
- package/dist/scan-executor.d.ts +12 -1
- package/dist/scan-executor.d.ts.map +1 -1
- package/dist/scan-executor.js +22 -2
- package/dist/scan-executor.js.map +1 -1
- package/dist/scan-history-renderer.d.ts.map +1 -1
- package/dist/scan-history-renderer.js +31 -28
- package/dist/scan-history-renderer.js.map +1 -1
- package/dist/scan-logs.d.ts +12 -0
- package/dist/scan-logs.d.ts.map +1 -1
- package/dist/scan-logs.js +106 -77
- package/dist/scan-logs.js.map +1 -1
- package/dist/scan-measurement.d.ts +26 -0
- package/dist/scan-measurement.d.ts.map +1 -0
- package/dist/scan-measurement.js +77 -0
- package/dist/scan-measurement.js.map +1 -0
- package/dist/scan-progress.d.ts +1 -0
- package/dist/scan-progress.d.ts.map +1 -1
- package/dist/scan-progress.js +3 -0
- package/dist/scan-progress.js.map +1 -1
- package/dist/scan-sessions.d.ts.map +1 -1
- package/dist/scan-sessions.js +4 -1
- package/dist/scan-sessions.js.map +1 -1
- package/dist/scan-settings.d.ts +1 -1
- package/dist/scan-settings.d.ts.map +1 -1
- package/dist/scan-settings.js +1 -24
- package/dist/scan-settings.js.map +1 -1
- package/dist/scanner-import.d.ts +64 -6
- package/dist/scanner-import.d.ts.map +1 -1
- package/dist/scanner-import.js +406 -48
- package/dist/scanner-import.js.map +1 -1
- package/dist/scanner-repository-url.d.ts +17 -0
- package/dist/scanner-repository-url.d.ts.map +1 -0
- package/dist/scanner-repository-url.js +71 -0
- package/dist/scanner-repository-url.js.map +1 -0
- package/dist/security-policy.d.ts +1 -1
- package/dist/security-policy.d.ts.map +1 -1
- package/dist/security-policy.js.map +1 -1
- package/dist/semgrep-findings.d.ts +6 -3
- package/dist/semgrep-findings.d.ts.map +1 -1
- package/dist/semgrep-findings.js +124 -88
- package/dist/semgrep-findings.js.map +1 -1
- package/dist/semgrep-guardian-findings.d.ts +38 -0
- package/dist/semgrep-guardian-findings.d.ts.map +1 -0
- package/dist/semgrep-guardian-findings.js +165 -0
- package/dist/semgrep-guardian-findings.js.map +1 -0
- package/dist/semgrep-platform-collector.d.ts +26 -0
- package/dist/semgrep-platform-collector.d.ts.map +1 -0
- package/dist/semgrep-platform-collector.js +108 -0
- package/dist/semgrep-platform-collector.js.map +1 -0
- package/dist/semgrep-platform-findings.d.ts +213 -0
- package/dist/semgrep-platform-findings.d.ts.map +1 -0
- package/dist/semgrep-platform-findings.js +1078 -0
- package/dist/semgrep-platform-findings.js.map +1 -0
- package/dist/semgrep-platform-v1.d.ts +6 -0
- package/dist/semgrep-platform-v1.d.ts.map +1 -0
- package/dist/semgrep-platform-v1.js +83 -0
- package/dist/semgrep-platform-v1.js.map +1 -0
- package/dist/server/api.d.ts +1 -1
- package/dist/server/api.d.ts.map +1 -1
- package/dist/server/api.js.map +1 -1
- package/dist/server/embedding-tokenizer.cdx.json +33 -0
- package/dist/server/embedding-tokenizer.d.ts +2 -0
- package/dist/server/embedding-tokenizer.d.ts.map +1 -0
- package/dist/server/embedding-tokenizer.js +322 -0
- package/dist/server/embedding-tokenizer.js.map +6 -0
- package/dist/server/embeddings.d.ts +16 -2
- package/dist/server/embeddings.d.ts.map +1 -1
- package/dist/server/embeddings.js +35 -5
- package/dist/server/embeddings.js.map +1 -1
- package/dist/session-lines.d.ts +5 -0
- package/dist/session-lines.d.ts.map +1 -0
- package/dist/session-lines.js +60 -0
- package/dist/session-lines.js.map +1 -0
- package/dist/severity-review.d.ts +3 -1
- package/dist/severity-review.d.ts.map +1 -1
- package/dist/severity-review.js +3 -2
- package/dist/severity-review.js.map +1 -1
- package/dist/severity-rubric.d.ts +20 -0
- package/dist/severity-rubric.d.ts.map +1 -0
- package/dist/severity-rubric.js +73 -0
- package/dist/severity-rubric.js.map +1 -0
- package/dist/severity-store.js +15 -5
- package/dist/severity-store.js.map +1 -1
- package/dist/skill-authentication.d.ts +1 -0
- package/dist/skill-authentication.d.ts.map +1 -1
- package/dist/skill-authentication.js +1 -0
- package/dist/skill-authentication.js.map +1 -1
- package/dist/snyk-findings.d.ts +7 -2
- package/dist/snyk-findings.d.ts.map +1 -1
- package/dist/snyk-findings.js +108 -84
- package/dist/snyk-findings.js.map +1 -1
- package/dist/snyk-rest-findings.d.ts +76 -0
- package/dist/snyk-rest-findings.d.ts.map +1 -0
- package/dist/snyk-rest-findings.js +1051 -0
- package/dist/snyk-rest-findings.js.map +1 -0
- package/dist/snyk-rest-v1.d.ts +28 -0
- package/dist/snyk-rest-v1.d.ts.map +1 -0
- package/dist/snyk-rest-v1.js +132 -0
- package/dist/snyk-rest-v1.js.map +1 -0
- package/dist/snyk-token-origin.d.ts +4 -0
- package/dist/snyk-token-origin.d.ts.map +1 -0
- package/dist/snyk-token-origin.js +82 -0
- package/dist/snyk-token-origin.js.map +1 -0
- package/dist/sonar-collector.d.ts +16 -0
- package/dist/sonar-collector.d.ts.map +1 -0
- package/dist/sonar-collector.js +380 -0
- package/dist/sonar-collector.js.map +1 -0
- package/dist/sonar-findings.d.ts +49 -0
- package/dist/sonar-findings.d.ts.map +1 -0
- package/dist/sonar-findings.js +288 -0
- package/dist/sonar-findings.js.map +1 -0
- package/dist/sonar-import.d.ts +111 -0
- package/dist/sonar-import.d.ts.map +1 -0
- package/dist/sonar-import.js +277 -0
- package/dist/sonar-import.js.map +1 -0
- package/dist/suggest-owners.d.ts.map +1 -1
- package/dist/suggest-owners.js +2 -1
- package/dist/suggest-owners.js.map +1 -1
- package/dist/target-drift.d.ts +9 -0
- package/dist/target-drift.d.ts.map +1 -0
- package/dist/target-drift.js +27 -0
- package/dist/target-drift.js.map +1 -0
- package/dist/targets.d.ts +5 -0
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +73 -31
- package/dist/targets.js.map +1 -1
- package/dist/tenable-findings.d.ts +9 -0
- package/dist/tenable-findings.d.ts.map +1 -0
- package/dist/tenable-findings.js +122 -0
- package/dist/tenable-findings.js.map +1 -0
- package/dist/terminal-columns.d.ts +4 -0
- package/dist/terminal-columns.d.ts.map +1 -0
- package/dist/terminal-columns.js +19 -0
- package/dist/terminal-columns.js.map +1 -0
- package/dist/terminal-stream.d.ts +13 -0
- package/dist/terminal-stream.d.ts.map +1 -0
- package/dist/terminal-stream.js +35 -0
- package/dist/terminal-stream.js.map +1 -0
- package/dist/trivy-findings.d.ts +19 -0
- package/dist/trivy-findings.d.ts.map +1 -0
- package/dist/trivy-findings.js +435 -0
- package/dist/trivy-findings.js.map +1 -0
- package/dist/trivy-import.d.ts +19 -0
- package/dist/trivy-import.d.ts.map +1 -0
- package/dist/trivy-import.js +151 -0
- package/dist/trivy-import.js.map +1 -0
- package/dist/validation-records.d.ts +0 -3
- package/dist/validation-records.d.ts.map +1 -1
- package/dist/validation-records.js +1 -17
- package/dist/validation-records.js.map +1 -1
- package/dist/veracode-findings.d.ts +9 -0
- package/dist/veracode-findings.d.ts.map +1 -0
- package/dist/veracode-findings.js +409 -0
- package/dist/veracode-findings.js.map +1 -0
- package/dist/vertex-pricing.d.ts +8 -0
- package/dist/vertex-pricing.d.ts.map +1 -0
- package/dist/vertex-pricing.js +20 -0
- package/dist/vertex-pricing.js.map +1 -0
- package/dist/windows-path.d.ts +1 -0
- package/dist/windows-path.d.ts.map +1 -1
- package/dist/windows-path.js +27 -1
- package/dist/windows-path.js.map +1 -1
- package/dist/wiz-csv.d.ts +2 -2
- package/dist/wiz-csv.d.ts.map +1 -1
- package/dist/wiz-csv.js +1 -3
- package/dist/wiz-csv.js.map +1 -1
- package/dist/wiz-findings.d.ts +14 -2
- package/dist/wiz-findings.d.ts.map +1 -1
- package/dist/wiz-findings.js +143 -50
- package/dist/wiz-findings.js.map +1 -1
- package/docs/classify-severity-records.md +28 -13
- package/docs/cli.md +623 -843
- package/docs/dedupe-records.md +54 -24
- package/docs/findings-service.md +87 -47
- package/docs/hosted-remediation.md +14 -7
- package/docs/hosted-scans.md +64 -41
- package/docs/telemetry.md +236 -0
- package/docs/validate-records.md +78 -44
- package/package.json +19 -17
- package/schemas/external-findings.schema.json +14 -1
- package/schemas/hosted-scan-v2.fixture.json +16 -1
- package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
- package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
- package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +0 -96
- package/_bundled_plugin/scripts/validate_tracking_source.py +0 -82
- package/_bundled_plugin/scripts/workbench_local_dedupe.py +0 -136
- package/dist/deduplication/records-protocol.d.ts +0 -4
- package/dist/deduplication/records-protocol.d.ts.map +0 -1
- package/dist/deduplication/records-protocol.js +0 -49
- package/dist/deduplication/records-protocol.js.map +0 -1
- package/dist/external-imports-feature.d.ts +0 -6
- package/dist/external-imports-feature.d.ts.map +0 -1
- package/dist/external-imports-feature.js +0 -51
- package/dist/external-imports-feature.js.map +0 -1
- package/dist/finding-normalization.d.ts +0 -5
- package/dist/finding-normalization.d.ts.map +0 -1
- package/dist/finding-normalization.js +0 -120
- package/dist/finding-normalization.js.map +0 -1
package/dist/cost.js
CHANGED
|
@@ -1,15 +1,72 @@
|
|
|
1
|
-
import { parseJson } from "./value.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { isSafeNonNegativeInteger, parseJson } from "./value.js";
|
|
2
|
+
import { workbenchArgs } from "./workbench-arguments.js";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { lstat, open, readFile, readdir, realpath, stat, } from "node:fs/promises";
|
|
5
|
+
import { join, relative, resolve } from "node:path";
|
|
4
6
|
import { StringDecoder } from "node:string_decoder";
|
|
5
7
|
import { isDeepStrictEqual } from "node:util";
|
|
6
8
|
import { z } from "zod";
|
|
7
9
|
import { isRecord } from "./record.js";
|
|
10
|
+
import { errorMessage } from "./errors.js";
|
|
11
|
+
import { sessionLines } from "./session-lines.js";
|
|
8
12
|
import { estimateScanCost, tokenUsage, } from "./cost-model.js";
|
|
9
13
|
import { scanActivityFromSessionEvent, sessionContentText, } from "./scan-activity.js";
|
|
10
14
|
import { isScanArtifactDirectory, sessionOwnsTurn, sessionParentThreadId, sessionStartedAt, } from "./scan-sessions.js";
|
|
11
15
|
import { scanProgressUpdatesFromText, } from "./worker-progress.js";
|
|
12
16
|
export { estimateScanCost, formatUsd } from "./cost-model.js";
|
|
17
|
+
export async function readScanBudgetUsage(workbench, scanId, threadId, validationThreadId, requireWorkerUsage = false) {
|
|
18
|
+
const result = await workbench(workbenchArgs("read-scan-usage", { scanId, threadId, validationThreadId }));
|
|
19
|
+
const counts = {
|
|
20
|
+
input_tokens: result["inputTokens"],
|
|
21
|
+
cached_input_tokens: result["cachedInputTokens"],
|
|
22
|
+
cache_write_input_tokens: result["cacheWriteInputTokens"],
|
|
23
|
+
output_tokens: result["outputTokens"],
|
|
24
|
+
reasoning_output_tokens: result["reasoningOutputTokens"],
|
|
25
|
+
};
|
|
26
|
+
const usage = Object.values(counts).every(Number.isSafeInteger)
|
|
27
|
+
? tokenUsage(counts)
|
|
28
|
+
: null;
|
|
29
|
+
const scope = result["coverageScope"];
|
|
30
|
+
const warnings = result["warnings"];
|
|
31
|
+
const ownership = {
|
|
32
|
+
...(Array.isArray(warnings) &&
|
|
33
|
+
warnings.length > 0 &&
|
|
34
|
+
warnings.every((item) => typeof item === "string")
|
|
35
|
+
? { warnings }
|
|
36
|
+
: {}),
|
|
37
|
+
...(isSafeNonNegativeInteger(result["threadCount"])
|
|
38
|
+
? { threadCount: result["threadCount"] }
|
|
39
|
+
: {}),
|
|
40
|
+
...(isSafeNonNegativeInteger(result["missingThreadCount"])
|
|
41
|
+
? { missingThreadCount: result["missingThreadCount"] }
|
|
42
|
+
: {}),
|
|
43
|
+
...(Array.isArray(scope) &&
|
|
44
|
+
scope.every((item) => typeof item === "string")
|
|
45
|
+
? { coverageScope: scope }
|
|
46
|
+
: {}),
|
|
47
|
+
};
|
|
48
|
+
const roles = result["byRole"];
|
|
49
|
+
const workers = isRecord(roles) ? roles["workers"] : undefined;
|
|
50
|
+
const coverage = result["coverage"];
|
|
51
|
+
if ((coverage !== "complete" && coverage !== "partial") ||
|
|
52
|
+
usage === null ||
|
|
53
|
+
usage.total_tokens !== result["totalTokens"])
|
|
54
|
+
return { ...ownership, usage: null, complete: false };
|
|
55
|
+
const hasWorkers = isRecord(workers) &&
|
|
56
|
+
typeof workers["threadCount"] === "number" &&
|
|
57
|
+
workers["threadCount"] > 0;
|
|
58
|
+
return {
|
|
59
|
+
...ownership,
|
|
60
|
+
// Older workbenches sum cache writes without tracking omitted counters.
|
|
61
|
+
usage: result["cacheWriteInputTokensReported"] === true
|
|
62
|
+
? usage
|
|
63
|
+
: { ...usage, cache_write_input_tokens_reported: false },
|
|
64
|
+
complete: coverage === "complete" && (!requireWorkerUsage || hasWorkers),
|
|
65
|
+
...(requireWorkerUsage && !hasWorkers
|
|
66
|
+
? { missingThreadCount: Math.max(1, ownership.missingThreadCount ?? 0) }
|
|
67
|
+
: {}),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
13
70
|
const rateLimitWindow = z
|
|
14
71
|
.object({
|
|
15
72
|
used_percent: z.number(),
|
|
@@ -29,6 +86,54 @@ const rateLimitSnapshot = z.object({
|
|
|
29
86
|
function createSessionReasoning(id, text = "") {
|
|
30
87
|
return { id, text, raw: false, activity: null };
|
|
31
88
|
}
|
|
89
|
+
function savedProgress(source) {
|
|
90
|
+
return {
|
|
91
|
+
progress: source.update,
|
|
92
|
+
position: source.position,
|
|
93
|
+
markerIndex: source.markerIndex,
|
|
94
|
+
order: source.order ?? 0,
|
|
95
|
+
source,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
// A live parent keeps authority over an unresolved saved prefix. Once an exact
|
|
99
|
+
// anchor bounds that prefix, source order also governs admission and display.
|
|
100
|
+
// Independent workers and legacy projections still use observation order.
|
|
101
|
+
function compareProgressOrder(left, right) {
|
|
102
|
+
const source = compareSourceOrder(left, right);
|
|
103
|
+
if (source !== undefined)
|
|
104
|
+
return source;
|
|
105
|
+
// A display preference is not source proof. Keep current live authority over
|
|
106
|
+
// an unresolved prefix, while retaining that prefix for later reconciliation.
|
|
107
|
+
if (left.live?.after !== undefined && right.position !== undefined)
|
|
108
|
+
return 1;
|
|
109
|
+
if (right.live?.after !== undefined && left.position !== undefined)
|
|
110
|
+
return -1;
|
|
111
|
+
const observedOrder = (candidate) => {
|
|
112
|
+
const marker = candidate.source?.projection?.markers[candidate.markerIndex ?? 0];
|
|
113
|
+
// A complete source projection retains its originating live order even
|
|
114
|
+
// when polling observes that copy after a newer unpositioned phase.
|
|
115
|
+
return marker !== undefined &&
|
|
116
|
+
progressKey(marker.progress) === progressKey(candidate.source.update)
|
|
117
|
+
? marker.order
|
|
118
|
+
: candidate.order;
|
|
119
|
+
};
|
|
120
|
+
return observedOrder(left) - observedOrder(right);
|
|
121
|
+
}
|
|
122
|
+
function compareSourceOrder(left, right) {
|
|
123
|
+
const leftPosition = left.position ?? left.live?.position ?? undefined;
|
|
124
|
+
const rightPosition = right.position ?? right.live?.position ?? undefined;
|
|
125
|
+
if (leftPosition !== undefined && rightPosition !== undefined)
|
|
126
|
+
return (leftPosition - rightPosition ||
|
|
127
|
+
(left.markerIndex ?? 0) - (right.markerIndex ?? 0));
|
|
128
|
+
if (leftPosition !== undefined && right.live?.after !== undefined)
|
|
129
|
+
return leftPosition >= right.live.after ? 1 : undefined;
|
|
130
|
+
if (rightPosition !== undefined && left.live?.after !== undefined)
|
|
131
|
+
return rightPosition >= left.live.after ? -1 : undefined;
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
function sessionObservation(session, value) {
|
|
135
|
+
return { position: session.eventPosition, value };
|
|
136
|
+
}
|
|
32
137
|
const COST_POLL_INTERVAL_MS = 100;
|
|
33
138
|
const SESSION_READ_SIZE = 64 * 1_024;
|
|
34
139
|
function createSessionUsage(observeUsage) {
|
|
@@ -37,6 +142,7 @@ function createSessionUsage(observeUsage) {
|
|
|
37
142
|
decoder: new StringDecoder("utf8"),
|
|
38
143
|
pendingLine: [],
|
|
39
144
|
unreadable: false,
|
|
145
|
+
compressedRead: false,
|
|
40
146
|
threadId: null,
|
|
41
147
|
parentThreadId: null,
|
|
42
148
|
workingDirectory: null,
|
|
@@ -50,9 +156,18 @@ function createSessionUsage(observeUsage) {
|
|
|
50
156
|
: null,
|
|
51
157
|
responseUsage: null,
|
|
52
158
|
responseIds: new Set(),
|
|
159
|
+
eventPosition: 0,
|
|
53
160
|
calls: new Map(),
|
|
54
161
|
activities: [],
|
|
55
162
|
progress: [],
|
|
163
|
+
position: 0,
|
|
164
|
+
completions: [],
|
|
165
|
+
latestWorkerProgress: null,
|
|
166
|
+
pendingWorkerProgress: new Map(),
|
|
167
|
+
messageProjections: new Map(),
|
|
168
|
+
messageProgressOrder: 0,
|
|
169
|
+
messageProgressFrontiers: { native: 0, event: 0, response: 0 },
|
|
170
|
+
messageProgressProjections: new Map(),
|
|
56
171
|
filesCompleted: 0,
|
|
57
172
|
filesTotal: null,
|
|
58
173
|
prose: new Set(),
|
|
@@ -62,28 +177,1520 @@ function createSessionUsage(observeUsage) {
|
|
|
62
177
|
}
|
|
63
178
|
export class ScanCostTracker {
|
|
64
179
|
#options;
|
|
180
|
+
#lastBudgetPoll = -Infinity;
|
|
65
181
|
#sessions = new Map();
|
|
66
182
|
#receipts = new Map();
|
|
183
|
+
#workerReceipts = new Map();
|
|
67
184
|
#workers = new Map();
|
|
68
185
|
#workerProgress = new Map();
|
|
69
|
-
#
|
|
186
|
+
#reportedSessionPositions = new Map();
|
|
187
|
+
#liveParentProgress = new Set();
|
|
188
|
+
#liveParentItems = [];
|
|
189
|
+
#liveParentEvents = new WeakMap();
|
|
190
|
+
#acceptedParent = null;
|
|
191
|
+
// Global coverage admitted from either the live or saved parent projection.
|
|
192
|
+
#parentProgress = null;
|
|
193
|
+
#parentFacts = new Map();
|
|
194
|
+
#parentInventory = null;
|
|
195
|
+
#parentInventoryBoundary = null;
|
|
196
|
+
// A bound excludes inventories at or after it without locating the review.
|
|
197
|
+
#parentReviewBefore = null;
|
|
198
|
+
#readyParentMarkers = new Map();
|
|
199
|
+
#inventoryFrontier = {
|
|
200
|
+
position: 0,
|
|
201
|
+
markerIndex: -1,
|
|
202
|
+
order: 0,
|
|
203
|
+
progress: undefined,
|
|
204
|
+
};
|
|
205
|
+
#progressOrder = 0;
|
|
206
|
+
#displayedProgress = null;
|
|
207
|
+
#matchedParentItems = 0;
|
|
208
|
+
#parentCompletionCursor = 0;
|
|
209
|
+
#indexedParentCompletions = 0;
|
|
210
|
+
#indexedParentItems = 0;
|
|
211
|
+
#parentCompletionKeys = new Map();
|
|
212
|
+
#parentItemKeys = new Map();
|
|
213
|
+
#parentMatches = [];
|
|
214
|
+
#pendingParentMatches = new Set();
|
|
215
|
+
#parentSession = null;
|
|
216
|
+
#parentTurnStartKnown = true;
|
|
217
|
+
#parentFrontier = { position: 0, markerIndex: -1 };
|
|
70
218
|
#threadId = null;
|
|
71
219
|
#observingWorkers = true;
|
|
72
220
|
#timer = null;
|
|
73
221
|
#pending = Promise.resolve();
|
|
74
|
-
#snapshot = {
|
|
222
|
+
#snapshot = {
|
|
223
|
+
usage: null,
|
|
224
|
+
cost: null,
|
|
225
|
+
measurement: {
|
|
226
|
+
usage: null,
|
|
227
|
+
cost: null,
|
|
228
|
+
coverage: "unavailable",
|
|
229
|
+
observedAt: new Date().toISOString(),
|
|
230
|
+
threadCount: 0,
|
|
231
|
+
missingThreadCount: 0,
|
|
232
|
+
},
|
|
233
|
+
};
|
|
75
234
|
#lastCost = null;
|
|
76
235
|
#lastUsage = null;
|
|
236
|
+
#lastMeasurement = null;
|
|
77
237
|
#lastObservation = null;
|
|
78
238
|
#highestFilesCompleted = 0;
|
|
79
239
|
#expectedFilesTotal;
|
|
240
|
+
#parentTurnFilesTotal;
|
|
241
|
+
#inventoryNeedsReconciliation = false;
|
|
80
242
|
constructor(options) {
|
|
81
243
|
this.#options = options;
|
|
82
|
-
|
|
244
|
+
if (options.expectedFilesTotal !== undefined)
|
|
245
|
+
this.setExpectedFilesTotal(options.expectedFilesTotal);
|
|
83
246
|
}
|
|
84
247
|
setExpectedFilesTotal(filesTotal) {
|
|
248
|
+
this.#parentTurnFilesTotal ??= filesTotal;
|
|
249
|
+
if (this.#expectedFilesTotal !== undefined &&
|
|
250
|
+
this.#expectedFilesTotal !== filesTotal)
|
|
251
|
+
this.#inventoryNeedsReconciliation = true;
|
|
85
252
|
this.#expectedFilesTotal = filesTotal;
|
|
86
253
|
}
|
|
254
|
+
/** Called before launching a turn, after loading any resumed history. */
|
|
255
|
+
beginParentTurn(resumed = false) {
|
|
256
|
+
this.#parentTurnFilesTotal = this.#expectedFilesTotal;
|
|
257
|
+
this.#parentTurnStartKnown = this.#parentSession !== null || !resumed;
|
|
258
|
+
for (const live of this.#liveParentItems) {
|
|
259
|
+
delete live.after;
|
|
260
|
+
}
|
|
261
|
+
this.#liveParentItems.length = 0;
|
|
262
|
+
this.#liveParentEvents = new WeakMap();
|
|
263
|
+
this.#acceptedParent = null;
|
|
264
|
+
this.#parentInventory = null;
|
|
265
|
+
this.#parentReviewBefore = null;
|
|
266
|
+
// Historical logs may omit projection kinds emitted by the new turn.
|
|
267
|
+
this.#parentSession?.messageProjections.clear();
|
|
268
|
+
if (this.#parentSession !== null) {
|
|
269
|
+
this.#parentSession.messageProgressProjections.clear();
|
|
270
|
+
this.#parentSession.messageProgressOrder = 0;
|
|
271
|
+
this.#parentSession.messageProgressFrontiers = {
|
|
272
|
+
native: 0,
|
|
273
|
+
event: 0,
|
|
274
|
+
response: 0,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
// A new turn retires unresolved hypotheses from the previous turn; admitted
|
|
278
|
+
// coverage and source summaries remain available for resumed scans.
|
|
279
|
+
if (this.#parentSession !== null)
|
|
280
|
+
this.#parentSession.progress.length = 0;
|
|
281
|
+
for (const completion of this.#parentSession?.completions ?? []) {
|
|
282
|
+
completion.item = null;
|
|
283
|
+
delete completion.kind;
|
|
284
|
+
delete completion.lastCommandProgress;
|
|
285
|
+
delete completion.projection;
|
|
286
|
+
}
|
|
287
|
+
this.#readyParentMarkers.clear();
|
|
288
|
+
this.#matchedParentItems = 0;
|
|
289
|
+
this.#parentCompletionCursor = this.#parentSession?.completions.length ?? 0;
|
|
290
|
+
this.#indexedParentCompletions = this.#parentCompletionCursor;
|
|
291
|
+
this.#indexedParentItems = 0;
|
|
292
|
+
this.#parentCompletionKeys.clear();
|
|
293
|
+
this.#parentItemKeys.clear();
|
|
294
|
+
this.#parentMatches.length = 0;
|
|
295
|
+
this.#pendingParentMatches.clear();
|
|
296
|
+
this.#liveParentProgress.clear();
|
|
297
|
+
}
|
|
298
|
+
/** Observe every supported completion so repeated output keeps its occurrence. */
|
|
299
|
+
recordParentItem(event) {
|
|
300
|
+
const item = event["item"];
|
|
301
|
+
if (event["type"] !== "item.completed" ||
|
|
302
|
+
!isRecord(item) ||
|
|
303
|
+
(item["type"] !== "command_execution" &&
|
|
304
|
+
item["type"] !== "agent_message") ||
|
|
305
|
+
this.#liveParentEvents.has(event))
|
|
306
|
+
return;
|
|
307
|
+
const live = {
|
|
308
|
+
markers: [],
|
|
309
|
+
item: streamedProgressItem(event),
|
|
310
|
+
position: null,
|
|
311
|
+
};
|
|
312
|
+
this.#liveParentItems.push(live);
|
|
313
|
+
this.#liveParentEvents.set(event, live);
|
|
314
|
+
this.#matchParentCompletions();
|
|
315
|
+
}
|
|
316
|
+
/** Observe source identity before applying display/count guards. */
|
|
317
|
+
recordParentProgress(progress, event) {
|
|
318
|
+
this.recordParentItem(event);
|
|
319
|
+
const live = this.#liveParentEvents.get(event);
|
|
320
|
+
if (live === undefined)
|
|
321
|
+
return true;
|
|
322
|
+
// Parsed rows were observed before this live marker even when their
|
|
323
|
+
// file.close is still pending. Do not give that old snapshot a later order.
|
|
324
|
+
for (const session of this.#sessions.values())
|
|
325
|
+
if (session.threadId === this.#threadId)
|
|
326
|
+
for (const saved of session.progress)
|
|
327
|
+
saved.order ??= ++this.#progressOrder;
|
|
328
|
+
const marker = {
|
|
329
|
+
progress,
|
|
330
|
+
index: live.markers.length,
|
|
331
|
+
order: ++this.#progressOrder,
|
|
332
|
+
accepted: false,
|
|
333
|
+
};
|
|
334
|
+
live.markers.push(marker);
|
|
335
|
+
if (isInventoryProgress(progress))
|
|
336
|
+
this.#parentInventory = marker;
|
|
337
|
+
if (live.position !== null)
|
|
338
|
+
this.#readyParentMarkers.set(marker, live);
|
|
339
|
+
const current = isInventoryProgress(progress)
|
|
340
|
+
? this.#retainedInventory()
|
|
341
|
+
: this.#parentFacts.get(progress.filesTotal);
|
|
342
|
+
return (live.position === null ||
|
|
343
|
+
this.#afterParentFrontier(live.position, marker.index) ||
|
|
344
|
+
// Polling can admit this completion before live delivery. Its live copy
|
|
345
|
+
// restores progress after worker output or an unmatched live completion;
|
|
346
|
+
// older proved positions and earlier markers remain stale.
|
|
347
|
+
((this.#displayedProgress?.owner === "worker" ||
|
|
348
|
+
(this.#displayedProgress?.owner === "live" &&
|
|
349
|
+
this.#displayedProgress.position === undefined)) &&
|
|
350
|
+
((live.position === this.#parentFrontier.position &&
|
|
351
|
+
marker.index === this.#parentFrontier.markerIndex) ||
|
|
352
|
+
(current?.position === this.#parentFrontier.position &&
|
|
353
|
+
current.markerIndex === this.#parentFrontier.markerIndex &&
|
|
354
|
+
progressKey(current.progress) === progressKey(progress)))));
|
|
355
|
+
}
|
|
356
|
+
/** Source acceptance is independent of duplicate observer notifications. */
|
|
357
|
+
deferParentInventory(event) {
|
|
358
|
+
const marker = this.#liveParentEvents.get(event)?.markers.at(-1);
|
|
359
|
+
// Parent inventory can invalidate provisional worker coverage. Reconcile
|
|
360
|
+
// it before displaying anything, so still-eligible coverage never drops.
|
|
361
|
+
if (marker !== undefined &&
|
|
362
|
+
marker === this.#parentInventory &&
|
|
363
|
+
(!this.#parentIsEligible(this.#parentProgress) ||
|
|
364
|
+
this.#parentProgress.progress.filesCompleted === 0))
|
|
365
|
+
marker.accepted = true;
|
|
366
|
+
}
|
|
367
|
+
acceptParentProgress(progress, event) {
|
|
368
|
+
const live = this.#liveParentEvents.get(event);
|
|
369
|
+
if (live === undefined)
|
|
370
|
+
return;
|
|
371
|
+
const marker = live.markers.at(-1);
|
|
372
|
+
if (marker === undefined)
|
|
373
|
+
return;
|
|
374
|
+
marker.accepted = true;
|
|
375
|
+
if (progress.filesCompleted > 0)
|
|
376
|
+
live.reviewMarker ??= marker.index;
|
|
377
|
+
this.#acceptedParent = live;
|
|
378
|
+
const provenance = {
|
|
379
|
+
order: marker.order,
|
|
380
|
+
live,
|
|
381
|
+
markerIndex: marker.index,
|
|
382
|
+
...(live.position === null ? {} : { position: live.position }),
|
|
383
|
+
};
|
|
384
|
+
this.#parentProgress = { progress, ...provenance };
|
|
385
|
+
this.#rememberParentFact(this.#parentProgress);
|
|
386
|
+
const update = {
|
|
387
|
+
...progress,
|
|
388
|
+
filesTotal: this.#expectedFilesTotal ?? progress.filesTotal,
|
|
389
|
+
};
|
|
390
|
+
this.#displayedProgress = { update, owner: "live", ...provenance };
|
|
391
|
+
this.#highestFilesCompleted = Math.max(this.#highestFilesCompleted, progress.filesCompleted);
|
|
392
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
393
|
+
this.#recordParentBoundary(progress, live.position ?? undefined, marker.index, true, undefined, live);
|
|
394
|
+
if (live.position !== null)
|
|
395
|
+
this.#advanceParentFrontier(live.position, marker.index);
|
|
396
|
+
}
|
|
397
|
+
#rememberParentFact(candidate) {
|
|
398
|
+
// Unbound live markers stay in the existing completion queue until their
|
|
399
|
+
// saved occurrence resolves. Inventory markers have their own frontier.
|
|
400
|
+
if (candidate.position === undefined ||
|
|
401
|
+
!this.#parentSourceIsEligible(candidate) ||
|
|
402
|
+
isInventoryProgress(candidate.progress))
|
|
403
|
+
return;
|
|
404
|
+
const total = candidate.progress.filesTotal;
|
|
405
|
+
const previous = this.#parentFacts.get(total);
|
|
406
|
+
if (previous === undefined ||
|
|
407
|
+
!this.#parentSourceIsEligible(previous) ||
|
|
408
|
+
candidate.progress.filesCompleted > previous.progress.filesCompleted ||
|
|
409
|
+
(candidate.progress.filesCompleted === previous.progress.filesCompleted &&
|
|
410
|
+
compareProgressOrder(candidate, previous) > 0))
|
|
411
|
+
this.#parentFacts.set(total, candidate);
|
|
412
|
+
}
|
|
413
|
+
#parentSourceIsEligible(candidate) {
|
|
414
|
+
const marker = candidate.live?.markers[candidate.markerIndex ?? 0];
|
|
415
|
+
return (
|
|
416
|
+
// Inventory establishes scope; only coverage is bounded by its prefix.
|
|
417
|
+
isInventoryProgress(candidate.progress) ||
|
|
418
|
+
candidate.progress.filesTotal <=
|
|
419
|
+
(candidate.source?.scopeFilesTotal ??
|
|
420
|
+
marker?.scopeFilesTotal ??
|
|
421
|
+
Number.MAX_SAFE_INTEGER));
|
|
422
|
+
}
|
|
423
|
+
#parentIsEligible(candidate) {
|
|
424
|
+
return (candidate !== null &&
|
|
425
|
+
this.#parentSourceIsEligible(candidate) &&
|
|
426
|
+
candidate.progress.filesTotal <=
|
|
427
|
+
(this.#expectedFilesTotal ?? Number.MAX_SAFE_INTEGER));
|
|
428
|
+
}
|
|
429
|
+
#parentInventoryWitness() {
|
|
430
|
+
const accepted = this.#acceptedParent;
|
|
431
|
+
if (accepted === null)
|
|
432
|
+
return undefined;
|
|
433
|
+
let witness;
|
|
434
|
+
// Compute one proof for the accepted live prefix. Later unpositioned items
|
|
435
|
+
// keep their predecessors' ordering evidence without gaining a position.
|
|
436
|
+
for (const live of this.#liveParentItems) {
|
|
437
|
+
for (const marker of live.markers) {
|
|
438
|
+
if (isInventoryProgress(marker.progress))
|
|
439
|
+
continue;
|
|
440
|
+
const position = live.position ?? marker.projection?.position;
|
|
441
|
+
if (position === undefined)
|
|
442
|
+
continue;
|
|
443
|
+
const candidate = {
|
|
444
|
+
position,
|
|
445
|
+
markerIndex: live.position === null
|
|
446
|
+
? marker.projection.markerIndex
|
|
447
|
+
: marker.index,
|
|
448
|
+
order: marker.order,
|
|
449
|
+
live,
|
|
450
|
+
};
|
|
451
|
+
if (witness === undefined ||
|
|
452
|
+
(compareSourceOrder(candidate, witness) ?? 0) > 0)
|
|
453
|
+
witness = candidate;
|
|
454
|
+
}
|
|
455
|
+
if (live === accepted)
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
return witness;
|
|
459
|
+
}
|
|
460
|
+
#retainedInventory() {
|
|
461
|
+
const { progress, ...source } = this.#inventoryFrontier;
|
|
462
|
+
return progress === undefined ? undefined : { progress, ...source };
|
|
463
|
+
}
|
|
464
|
+
#acceptedInventory() {
|
|
465
|
+
let accepted;
|
|
466
|
+
let precedingAnchor;
|
|
467
|
+
for (const live of this.#liveParentItems) {
|
|
468
|
+
for (const marker of live.markers) {
|
|
469
|
+
if (!marker.accepted ||
|
|
470
|
+
!isInventoryProgress(marker.progress) ||
|
|
471
|
+
(live.position !== null &&
|
|
472
|
+
!this.#inventoryPrecedesReview(live.position, marker.index, live)) ||
|
|
473
|
+
// An unresolved command follows the whole preceding exact completion.
|
|
474
|
+
// That lower bound can exclude it without assigning it a position.
|
|
475
|
+
(live.position === null &&
|
|
476
|
+
precedingAnchor !== undefined &&
|
|
477
|
+
!this.#inventoryPrecedesReview(precedingAnchor.position, precedingAnchor.markers.length)))
|
|
478
|
+
continue;
|
|
479
|
+
const candidate = {
|
|
480
|
+
progress: marker.progress,
|
|
481
|
+
marker,
|
|
482
|
+
live,
|
|
483
|
+
position: live.position ?? undefined,
|
|
484
|
+
markerIndex: marker.index,
|
|
485
|
+
order: marker.order,
|
|
486
|
+
};
|
|
487
|
+
if ((this.#inventoryReviewOrder(candidate) ?? -1) >= 0)
|
|
488
|
+
continue;
|
|
489
|
+
if (accepted === undefined ||
|
|
490
|
+
compareProgressOrder(candidate, accepted) > 0)
|
|
491
|
+
accepted = candidate;
|
|
492
|
+
}
|
|
493
|
+
if (live.position !== null)
|
|
494
|
+
precedingAnchor = live;
|
|
495
|
+
}
|
|
496
|
+
return accepted;
|
|
497
|
+
}
|
|
498
|
+
#savedInventorySupersedes(saved, live, native = this.#parentSession?.completions.some((completion) => completion.kind === "command")) {
|
|
499
|
+
const source = this.#inventorySourceOrder(saved, live);
|
|
500
|
+
if (source !== undefined) {
|
|
501
|
+
return source >= 0;
|
|
502
|
+
}
|
|
503
|
+
else if (live.live.after === undefined && !native) {
|
|
504
|
+
// Older logs omit command completions. Their later phase receipts still
|
|
505
|
+
// provide the existing best-effort evidence for a saved inventory.
|
|
506
|
+
const phase = this.#parentFacts.get(saved.progress.filesTotal);
|
|
507
|
+
const row = this.#parentSession?.progress.find((row) => row.position === saved.position &&
|
|
508
|
+
row.markerIndex === saved.markerIndex);
|
|
509
|
+
const evidence = phase !== undefined && (compareSourceOrder(phase, saved) ?? 0) > 0
|
|
510
|
+
? phase
|
|
511
|
+
: row === undefined || !this.#unboundParentCopy(row)
|
|
512
|
+
? saved
|
|
513
|
+
: undefined;
|
|
514
|
+
if (evidence !== undefined && compareProgressOrder(evidence, live) >= 0) {
|
|
515
|
+
// Keep admitted relative authority when a later anchor only narrows
|
|
516
|
+
// an unknown occurrence. Exact source order can still supersede it.
|
|
517
|
+
saved.afterLive = live.live;
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
#inventorySourceOrder(saved, live) {
|
|
524
|
+
const source = compareSourceOrder(saved, live);
|
|
525
|
+
if (source !== undefined)
|
|
526
|
+
return source;
|
|
527
|
+
const projection = saved.source?.projection;
|
|
528
|
+
const marker = projection?.markers[saved.markerIndex];
|
|
529
|
+
// A complete bounded projection preserves live inventory order without
|
|
530
|
+
// identifying the saved occurrence. Count rejection cannot erase that proof.
|
|
531
|
+
if (projection !== undefined &&
|
|
532
|
+
marker !== undefined &&
|
|
533
|
+
progressKey(marker.progress) === progressKey(saved.progress))
|
|
534
|
+
return (compareSourceOrder({ order: marker.order, markerIndex: marker.index, live: projection }, live) ?? marker.order - live.order);
|
|
535
|
+
return saved.afterLive === live.live ? 1 : undefined;
|
|
536
|
+
}
|
|
537
|
+
#projectParentInventory() {
|
|
538
|
+
const saved = this.#retainedInventory();
|
|
539
|
+
const live = this.#acceptedInventory();
|
|
540
|
+
const selected = saved !== undefined &&
|
|
541
|
+
(live === undefined || this.#savedInventorySupersedes(saved, live))
|
|
542
|
+
? saved
|
|
543
|
+
: live;
|
|
544
|
+
if (selected === saved && saved !== undefined) {
|
|
545
|
+
if (saved.source !== undefined)
|
|
546
|
+
saved.source.inventoryAdmitted = true;
|
|
547
|
+
if (saved.afterLive !== undefined)
|
|
548
|
+
this.#inventoryFrontier.afterLive = saved.afterLive;
|
|
549
|
+
}
|
|
550
|
+
const invalidated = selected === undefined &&
|
|
551
|
+
this.#liveParentItems.some((item) => item.markers.some((marker) => marker.accepted && isInventoryProgress(marker.progress)));
|
|
552
|
+
// Empty source history does not override a caller's explicit scope update.
|
|
553
|
+
// Roll back only when observed inventory authority has lost eligibility.
|
|
554
|
+
const filesTotal = selected?.progress.filesTotal ??
|
|
555
|
+
(invalidated ? this.#parentTurnFilesTotal : undefined);
|
|
556
|
+
if (filesTotal !== undefined) {
|
|
557
|
+
// Scope recovered here has not necessarily reached the API, including
|
|
558
|
+
// its first assignment when registration supplied no file count.
|
|
559
|
+
if (filesTotal !== this.#expectedFilesTotal)
|
|
560
|
+
this.#inventoryNeedsReconciliation = true;
|
|
561
|
+
this.setExpectedFilesTotal(filesTotal);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
#recordParentBoundary(progress, position, markerIndex, admitted = false, filesTotal, projection) {
|
|
565
|
+
let reviewed = progress.filesCompleted > 0;
|
|
566
|
+
filesTotal ??= projection?.markers[markerIndex]?.scopeFilesTotal;
|
|
567
|
+
if (position === undefined) {
|
|
568
|
+
if (reviewed &&
|
|
569
|
+
admitted &&
|
|
570
|
+
projection?.after !== undefined &&
|
|
571
|
+
progress.filesTotal <= (filesTotal ?? Number.MAX_SAFE_INTEGER)) {
|
|
572
|
+
const marker = projection.markers[markerIndex];
|
|
573
|
+
const previous = this.#parentReviewBefore;
|
|
574
|
+
if (previous === null ||
|
|
575
|
+
projection.after < previous.live.after ||
|
|
576
|
+
(projection.after === previous.live.after &&
|
|
577
|
+
marker.order < previous.order))
|
|
578
|
+
this.#parentReviewBefore = {
|
|
579
|
+
order: marker.order,
|
|
580
|
+
markerIndex,
|
|
581
|
+
live: projection,
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
// Eligible source progress can start review without winning the display.
|
|
587
|
+
// Merely locating a rejected oversized marker cannot fix that cutoff.
|
|
588
|
+
if (reviewed &&
|
|
589
|
+
progress.filesTotal > (filesTotal ?? Number.MAX_SAFE_INTEGER)) {
|
|
590
|
+
const boundary = this.#parentInventoryBoundary;
|
|
591
|
+
if (boundary?.reviewed === true &&
|
|
592
|
+
boundary.position === position &&
|
|
593
|
+
boundary.markerIndex === markerIndex)
|
|
594
|
+
// Retire the contradicted cutoff while preserving its source position
|
|
595
|
+
// as an ordering witness for preceding inventory.
|
|
596
|
+
boundary.reviewed = false;
|
|
597
|
+
if (projection === undefined)
|
|
598
|
+
return;
|
|
599
|
+
// A bounded current completion still orders its preceding inventory.
|
|
600
|
+
// Scope-ineligible progress supplies that witness, not a reviewed cutoff.
|
|
601
|
+
reviewed = false;
|
|
602
|
+
}
|
|
603
|
+
if (isInventoryProgress(progress))
|
|
604
|
+
return;
|
|
605
|
+
const boundary = this.#parentInventoryBoundary;
|
|
606
|
+
const order = boundary === null
|
|
607
|
+
? 0
|
|
608
|
+
: position - boundary.position || markerIndex - boundary.markerIndex;
|
|
609
|
+
// Before review starts, each later phase can establish its preceding
|
|
610
|
+
// inventory. Once coverage starts, preserve the first reviewed boundary.
|
|
611
|
+
if (boundary === null ||
|
|
612
|
+
(reviewed
|
|
613
|
+
? !boundary.reviewed || order < 0
|
|
614
|
+
: !boundary.reviewed && order > 0))
|
|
615
|
+
this.#parentInventoryBoundary = {
|
|
616
|
+
position,
|
|
617
|
+
markerIndex,
|
|
618
|
+
reviewed,
|
|
619
|
+
projection: projection === undefined
|
|
620
|
+
? reviewed
|
|
621
|
+
? undefined
|
|
622
|
+
: boundary?.projection
|
|
623
|
+
: {
|
|
624
|
+
position,
|
|
625
|
+
markerIndex,
|
|
626
|
+
order: projection.markers[markerIndex].order,
|
|
627
|
+
live: projection,
|
|
628
|
+
},
|
|
629
|
+
};
|
|
630
|
+
else if (order === 0 && projection !== undefined)
|
|
631
|
+
boundary.projection = {
|
|
632
|
+
position,
|
|
633
|
+
markerIndex,
|
|
634
|
+
order: projection.markers[markerIndex].order,
|
|
635
|
+
live: projection,
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
#recordParentSourceBoundaries(command) {
|
|
639
|
+
const session = this.#parentSession;
|
|
640
|
+
if (session === null)
|
|
641
|
+
return;
|
|
642
|
+
const native = session.completions.some((completion) => completion.kind === "command");
|
|
643
|
+
const projectedItems = new Set();
|
|
644
|
+
for (const completion of session.completions)
|
|
645
|
+
if (completion.projection !== undefined)
|
|
646
|
+
projectedItems.add(completion.projection);
|
|
647
|
+
this.#parentReviewBefore = null;
|
|
648
|
+
for (const live of this.#liveParentItems) {
|
|
649
|
+
for (const marker of live.markers)
|
|
650
|
+
delete marker.projection;
|
|
651
|
+
if (live.position === null &&
|
|
652
|
+
live.after !== undefined &&
|
|
653
|
+
!projectedItems.has(live))
|
|
654
|
+
// Complete projections get their eligibility from the ordered rows
|
|
655
|
+
// below. Only omitted admitted reviews contribute this upper bound.
|
|
656
|
+
for (const marker of live.markers)
|
|
657
|
+
if (marker.accepted && marker.progress.filesCompleted > 0)
|
|
658
|
+
this.#recordParentBoundary(marker.progress, undefined, marker.index, true, undefined, live);
|
|
659
|
+
}
|
|
660
|
+
const authority = this.#acceptedInventory();
|
|
661
|
+
// Legacy logs without command positions keep the established live scope.
|
|
662
|
+
// An old copied preview cannot replace it merely by being read later.
|
|
663
|
+
let filesTotal = !native &&
|
|
664
|
+
authority?.live.position === null &&
|
|
665
|
+
authority.live.after === undefined
|
|
666
|
+
? authority.progress.filesTotal
|
|
667
|
+
: this.#parentTurnFilesTotal;
|
|
668
|
+
let inventory;
|
|
669
|
+
let savedInventory;
|
|
670
|
+
const scopeAt = (candidate, completion) => {
|
|
671
|
+
if (!native ||
|
|
672
|
+
inventory !== undefined ||
|
|
673
|
+
authority?.live.position !== null ||
|
|
674
|
+
Number.isFinite(authority.live.after))
|
|
675
|
+
return filesTotal;
|
|
676
|
+
if (candidate.markerIndex < authority.marker.index &&
|
|
677
|
+
completion?.item?.kind === "command" &&
|
|
678
|
+
completion.item.outputDigest === authority.live.item?.outputDigest)
|
|
679
|
+
// Complete output agreement proves marker layout, not occurrence.
|
|
680
|
+
// A command's later inventory cannot justify its own earlier coverage.
|
|
681
|
+
return filesTotal;
|
|
682
|
+
// An initial estimate is not source proof against an admitted inventory
|
|
683
|
+
// missing from persistence. Keep it for rows known or observed before
|
|
684
|
+
// that live authority; unresolved later rows retain the admitted scope.
|
|
685
|
+
const order = this.#inventorySourceOrder(candidate, authority) ??
|
|
686
|
+
(candidate.order === 0 ? 1 : candidate.order - authority.order);
|
|
687
|
+
return order < 0 ? filesTotal : authority.progress.filesTotal;
|
|
688
|
+
};
|
|
689
|
+
let liveIndex = 0;
|
|
690
|
+
const applyBounds = (position) => {
|
|
691
|
+
while (liveIndex < this.#liveParentItems.length) {
|
|
692
|
+
const live = this.#liveParentItems[liveIndex];
|
|
693
|
+
if (live.position !== null || live.after === undefined) {
|
|
694
|
+
liveIndex++;
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
697
|
+
if (live.after > position)
|
|
698
|
+
break;
|
|
699
|
+
liveIndex++;
|
|
700
|
+
// Every marker remains available. A rejected trailing zero must not
|
|
701
|
+
// replace inventory whose order still proves it precedes review.
|
|
702
|
+
for (const marker of live.markers) {
|
|
703
|
+
if (!isInventoryProgress(marker.progress) ||
|
|
704
|
+
// A bound orders a lossy inventory but does not admit its scope.
|
|
705
|
+
// Unaccepted inventory needs a complete projection or corroboration.
|
|
706
|
+
(!marker.accepted && !projectedItems.has(live)))
|
|
707
|
+
continue;
|
|
708
|
+
const bounded = {
|
|
709
|
+
progress: marker.progress,
|
|
710
|
+
marker,
|
|
711
|
+
live,
|
|
712
|
+
markerIndex: marker.index,
|
|
713
|
+
order: marker.order,
|
|
714
|
+
};
|
|
715
|
+
if (
|
|
716
|
+
// An upper bound does not place this inventory at the anchor.
|
|
717
|
+
// Unknown relative order cannot revoke already admitted scope.
|
|
718
|
+
(this.#inventoryReviewOrder(bounded) ?? -1) < 0 &&
|
|
719
|
+
(inventory === undefined ||
|
|
720
|
+
("marker" in inventory
|
|
721
|
+
? compareProgressOrder(inventory, bounded) < 0
|
|
722
|
+
: !this.#savedInventorySupersedes(inventory, bounded, native)))) {
|
|
723
|
+
inventory = bounded;
|
|
724
|
+
filesTotal = bounded.progress.filesTotal;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
const recordInventory = (candidate) => {
|
|
730
|
+
const retained = this.#retainedInventory();
|
|
731
|
+
inventory =
|
|
732
|
+
retained?.position === candidate.position &&
|
|
733
|
+
retained.markerIndex === candidate.markerIndex
|
|
734
|
+
? retained
|
|
735
|
+
: candidate;
|
|
736
|
+
savedInventory = inventory;
|
|
737
|
+
filesTotal = candidate.progress.filesTotal;
|
|
738
|
+
};
|
|
739
|
+
let completionIndex = 0;
|
|
740
|
+
const remainingMarkers = (completion) => {
|
|
741
|
+
applyBounds(completion.position);
|
|
742
|
+
const live = completion.projection;
|
|
743
|
+
if (completion.kind !== "command" ||
|
|
744
|
+
live?.position !== completion.position)
|
|
745
|
+
return;
|
|
746
|
+
// Exact command identity also locates markers omitted by persistence.
|
|
747
|
+
// Finish that command before examining the next saved source position.
|
|
748
|
+
for (let index = (completion.lastCommandProgress?.markerIndex ?? -1) + 1; index < live.markers.length; index++) {
|
|
749
|
+
const marker = live.markers[index];
|
|
750
|
+
const candidate = {
|
|
751
|
+
progress: marker.progress,
|
|
752
|
+
position: completion.position,
|
|
753
|
+
markerIndex: marker.index,
|
|
754
|
+
order: marker.order,
|
|
755
|
+
live,
|
|
756
|
+
};
|
|
757
|
+
marker.scopeFilesTotal = scopeAt(candidate, completion);
|
|
758
|
+
if (isInventoryProgress(marker.progress)) {
|
|
759
|
+
if (this.#inventoryPrecedesReview(completion.position, marker.index, live))
|
|
760
|
+
recordInventory(candidate);
|
|
761
|
+
}
|
|
762
|
+
else
|
|
763
|
+
this.#recordParentBoundary(marker.progress, completion.position, marker.index, marker.accepted, marker.scopeFilesTotal);
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
for (const saved of session.progress) {
|
|
767
|
+
while (completionIndex < session.completions.length &&
|
|
768
|
+
session.completions[completionIndex].position < saved.position)
|
|
769
|
+
remainingMarkers(session.completions[completionIndex++]);
|
|
770
|
+
const completion = session.completions[completionIndex];
|
|
771
|
+
applyBounds(saved.position);
|
|
772
|
+
saved.projection =
|
|
773
|
+
completion?.position === saved.position
|
|
774
|
+
? completion.projection
|
|
775
|
+
: undefined;
|
|
776
|
+
const projected = saved.projection?.markers[saved.markerIndex];
|
|
777
|
+
if (saved.duplicate)
|
|
778
|
+
continue;
|
|
779
|
+
const candidate = savedProgress(saved);
|
|
780
|
+
const progress = candidate.progress;
|
|
781
|
+
saved.scopeFilesTotal = scopeAt(candidate, completion?.position === saved.position ? completion : undefined);
|
|
782
|
+
const exact = completion?.projection?.position === saved.position;
|
|
783
|
+
const nativeOccurrence = this.#parentTurnStartKnown && saved.completion;
|
|
784
|
+
if (projected !== undefined) {
|
|
785
|
+
projected.scopeFilesTotal = saved.scopeFilesTotal;
|
|
786
|
+
if (progressKey(projected.progress) === progressKey(progress))
|
|
787
|
+
projected.projection = {
|
|
788
|
+
position: saved.position,
|
|
789
|
+
markerIndex: saved.markerIndex,
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
if (isInventoryProgress(progress)) {
|
|
793
|
+
// Source eligibility follows the inventory at this position, not the
|
|
794
|
+
// display scope at the start or end of a poll. The retained rows make
|
|
795
|
+
// this ordered pass identical for every partition of the same input.
|
|
796
|
+
if (this.#inventoryPrecedesReview(saved.position, saved.markerIndex, saved.projection, saved)) {
|
|
797
|
+
if (exact || nativeOccurrence || !this.#unboundParentCopy(saved))
|
|
798
|
+
recordInventory(candidate);
|
|
799
|
+
else if (authority !== undefined &&
|
|
800
|
+
progressKey(progress) === progressKey(authority.progress) &&
|
|
801
|
+
(compareSourceOrder(candidate, authority) ?? 0) >= 0) {
|
|
802
|
+
// A duplicate can restate the admitted legacy scope without proving
|
|
803
|
+
// an occurrence or becoming eligible for another notification.
|
|
804
|
+
inventory = authority;
|
|
805
|
+
filesTotal = authority.progress.filesTotal;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
if (projected !== undefined ||
|
|
811
|
+
// Native current-turn review rows have their own source evidence. Their
|
|
812
|
+
// eligibility does not depend on the provisional display frontier.
|
|
813
|
+
(nativeOccurrence && progress.filesCompleted > 0) ||
|
|
814
|
+
(this.#savedParentProgressIsCurrent(saved) &&
|
|
815
|
+
(command === undefined ||
|
|
816
|
+
command.live !== this.#acceptedParent ||
|
|
817
|
+
command.live.position !== null ||
|
|
818
|
+
saved.position >= command.after)) ||
|
|
819
|
+
(!saved.receipt &&
|
|
820
|
+
progress.filesCompleted > 0 &&
|
|
821
|
+
this.#liveParentProgress.has(progressKey(progress))))
|
|
822
|
+
this.#recordParentBoundary(progress, saved.position, saved.markerIndex, exact && (projected?.accepted ?? false), saved.scopeFilesTotal, projected === undefined ? undefined : completion.projection);
|
|
823
|
+
}
|
|
824
|
+
while (completionIndex < session.completions.length)
|
|
825
|
+
remainingMarkers(session.completions[completionIndex++]);
|
|
826
|
+
return savedInventory;
|
|
827
|
+
}
|
|
828
|
+
#precedesParentBoundary(position, markerIndex) {
|
|
829
|
+
const first = this.#parentInventoryBoundary;
|
|
830
|
+
return (first !== null &&
|
|
831
|
+
(position < first.position ||
|
|
832
|
+
(position === first.position && markerIndex < first.markerIndex)));
|
|
833
|
+
}
|
|
834
|
+
#inventoryAllowedInCompletion(live, markerIndex) {
|
|
835
|
+
return (live?.reviewMarker === undefined ||
|
|
836
|
+
markerIndex < live.reviewMarker ||
|
|
837
|
+
live.markers[markerIndex]?.accepted !== false);
|
|
838
|
+
}
|
|
839
|
+
#inventoryPrecedesReview(position, markerIndex, live, source) {
|
|
840
|
+
return ((source?.inventoryAdmitted === true ||
|
|
841
|
+
this.#inventoryAllowedInCompletion(live, markerIndex)) &&
|
|
842
|
+
position < (this.#parentReviewBefore?.live.after ?? Infinity) &&
|
|
843
|
+
(this.#parentInventoryBoundary?.reviewed !== true ||
|
|
844
|
+
this.#precedesParentBoundary(position, markerIndex)));
|
|
845
|
+
}
|
|
846
|
+
#inventoryReviewOrder(inventory) {
|
|
847
|
+
const projection = inventory.live?.markers[inventory.markerIndex ?? 0]?.projection;
|
|
848
|
+
if (
|
|
849
|
+
// A complete projection can exclude this inventory even though its live
|
|
850
|
+
// occurrence remains unknown. A mere upper bound cannot do so.
|
|
851
|
+
(projection !== undefined &&
|
|
852
|
+
!this.#inventoryPrecedesReview(projection.position, projection.markerIndex)) ||
|
|
853
|
+
!this.#inventoryAllowedInCompletion(inventory.live, inventory.markerIndex ?? 0))
|
|
854
|
+
return 1;
|
|
855
|
+
const boundary = this.#parentInventoryBoundary;
|
|
856
|
+
const reviews = [];
|
|
857
|
+
if (boundary?.reviewed === true)
|
|
858
|
+
reviews.push({
|
|
859
|
+
position: boundary.position,
|
|
860
|
+
markerIndex: boundary.markerIndex,
|
|
861
|
+
live: boundary.projection?.live,
|
|
862
|
+
order: boundary.projection?.order ?? 0,
|
|
863
|
+
});
|
|
864
|
+
if (this.#parentReviewBefore !== null)
|
|
865
|
+
reviews.push(this.#parentReviewBefore);
|
|
866
|
+
let order = -1;
|
|
867
|
+
for (const review of reviews) {
|
|
868
|
+
const relation = compareSourceOrder(inventory, review) ??
|
|
869
|
+
// A resolved projection or bounded omitted review preserves the live
|
|
870
|
+
// sequence, including marker order within one command. It does not
|
|
871
|
+
// assign either completion an otherwise unknown saved occurrence.
|
|
872
|
+
(inventory.live !== undefined && review.live !== undefined
|
|
873
|
+
? inventory.order - review.order
|
|
874
|
+
: undefined);
|
|
875
|
+
if (relation !== undefined && relation >= 0)
|
|
876
|
+
return relation;
|
|
877
|
+
if (relation === undefined)
|
|
878
|
+
order = undefined;
|
|
879
|
+
}
|
|
880
|
+
return order;
|
|
881
|
+
}
|
|
882
|
+
#afterParentFrontier(position, index) {
|
|
883
|
+
return (position > this.#parentFrontier.position ||
|
|
884
|
+
(position === this.#parentFrontier.position &&
|
|
885
|
+
index > this.#parentFrontier.markerIndex));
|
|
886
|
+
}
|
|
887
|
+
#advanceParentFrontier(position, markerIndex) {
|
|
888
|
+
if (this.#afterParentFrontier(position, markerIndex))
|
|
889
|
+
this.#parentFrontier = { position, markerIndex };
|
|
890
|
+
}
|
|
891
|
+
#unresolvedParentCommand() {
|
|
892
|
+
const live = this.#acceptedParent;
|
|
893
|
+
if (live?.position !== null ||
|
|
894
|
+
live.after === undefined ||
|
|
895
|
+
!live.markers.some((marker) => marker.accepted))
|
|
896
|
+
return undefined;
|
|
897
|
+
return { live, after: live.after };
|
|
898
|
+
}
|
|
899
|
+
#boundParentCommands(session, start = 0, end = this.#liveParentItems.length, completionIndex = 0) {
|
|
900
|
+
// Exact-only intervals require neither source scans nor alias indexes.
|
|
901
|
+
while (start < end && this.#liveParentItems[start].position !== null)
|
|
902
|
+
start++;
|
|
903
|
+
if (start === end ||
|
|
904
|
+
!this.#parentTurnStartKnown ||
|
|
905
|
+
!session.completions.some((completion) => completion.kind === "command"))
|
|
906
|
+
return;
|
|
907
|
+
while (start < end) {
|
|
908
|
+
if (this.#liveParentItems[start].position !== null) {
|
|
909
|
+
start++;
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
let stop = start;
|
|
913
|
+
while (stop < end && this.#liveParentItems[stop].position === null)
|
|
914
|
+
stop++;
|
|
915
|
+
const before = this.#liveParentItems[start - 1]?.position ?? 0;
|
|
916
|
+
const after = this.#liveParentItems[stop]?.position ?? Infinity;
|
|
917
|
+
const saved = new Map();
|
|
918
|
+
while (completionIndex < session.completions.length &&
|
|
919
|
+
session.completions[completionIndex].position < after) {
|
|
920
|
+
const completion = session.completions[completionIndex++];
|
|
921
|
+
if (completion.position <= before ||
|
|
922
|
+
completion.item?.kind !== "command")
|
|
923
|
+
continue;
|
|
924
|
+
// Bounded projections are derived from this interval, not occurrences.
|
|
925
|
+
// Rebuild them when a later completion provides a tighter bound.
|
|
926
|
+
if (completion.projection?.position === null)
|
|
927
|
+
delete completion.projection;
|
|
928
|
+
const key = completion.item.outputDigest;
|
|
929
|
+
const occurrences = saved.get(key);
|
|
930
|
+
if (occurrences === undefined)
|
|
931
|
+
saved.set(key, [completion]);
|
|
932
|
+
else
|
|
933
|
+
occurrences.push(completion);
|
|
934
|
+
}
|
|
935
|
+
const observed = new Map();
|
|
936
|
+
const groups = new Map();
|
|
937
|
+
for (let index = start; index < stop; index++) {
|
|
938
|
+
const live = this.#liveParentItems[index];
|
|
939
|
+
if (live.item?.kind !== "command")
|
|
940
|
+
continue;
|
|
941
|
+
const keys = [
|
|
942
|
+
...new Set([live.item.outputDigest, live.item.persistedOutputDigest]),
|
|
943
|
+
]
|
|
944
|
+
.filter((key) => saved.has(key))
|
|
945
|
+
.sort();
|
|
946
|
+
for (const key of keys) {
|
|
947
|
+
const occurrences = observed.get(key);
|
|
948
|
+
if (occurrences === undefined)
|
|
949
|
+
observed.set(key, [index]);
|
|
950
|
+
else
|
|
951
|
+
occurrences.push(index);
|
|
952
|
+
}
|
|
953
|
+
if (live.markers.length === 0)
|
|
954
|
+
continue;
|
|
955
|
+
const key = keys.join(":");
|
|
956
|
+
const group = groups.get(key);
|
|
957
|
+
if (group === undefined)
|
|
958
|
+
groups.set(key, { keys, members: [index] });
|
|
959
|
+
else
|
|
960
|
+
group.members.push(index);
|
|
961
|
+
}
|
|
962
|
+
const evidence = new Map();
|
|
963
|
+
for (const { keys, members } of groups.values()) {
|
|
964
|
+
const queues = keys.map((key) => ({
|
|
965
|
+
key,
|
|
966
|
+
saved: saved.get(key),
|
|
967
|
+
observed: observed.get(key),
|
|
968
|
+
reserved: 0,
|
|
969
|
+
next: 0,
|
|
970
|
+
}));
|
|
971
|
+
let reservations = 0;
|
|
972
|
+
for (const index of members) {
|
|
973
|
+
// Reserve preceding compatible live items once for this signature
|
|
974
|
+
// group. Advancing ordered cursors is equivalent to consuming the
|
|
975
|
+
// earliest compatible item for each saved occurrence, without splice.
|
|
976
|
+
while (true) {
|
|
977
|
+
const previous = Math.min(...queues.map((queue) => queue.observed[queue.next] ?? Infinity));
|
|
978
|
+
if (previous >= index)
|
|
979
|
+
break;
|
|
980
|
+
const item = this.#liveParentItems[previous].item;
|
|
981
|
+
let selected;
|
|
982
|
+
for (const queue of queues) {
|
|
983
|
+
if (queue.observed[queue.next] === previous)
|
|
984
|
+
queue.next++;
|
|
985
|
+
if ((queue.key === item.outputDigest ||
|
|
986
|
+
queue.key === item.persistedOutputDigest) &&
|
|
987
|
+
queue.reserved < queue.saved.length &&
|
|
988
|
+
(selected === undefined ||
|
|
989
|
+
queue.saved[queue.reserved].position <
|
|
990
|
+
selected.saved[selected.reserved].position))
|
|
991
|
+
selected = queue;
|
|
992
|
+
}
|
|
993
|
+
if (selected !== undefined) {
|
|
994
|
+
selected.reserved++;
|
|
995
|
+
reservations++;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
evidence.set(index, {
|
|
999
|
+
candidates: queues.map((queue) => ({
|
|
1000
|
+
saved: queue.saved,
|
|
1001
|
+
first: queue.reserved,
|
|
1002
|
+
})),
|
|
1003
|
+
reserved: reservations > 0,
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
// Later complete projections constrain earlier compatible copies. A
|
|
1008
|
+
// bound narrows the interval without assigning a live occurrence.
|
|
1009
|
+
let bound = after;
|
|
1010
|
+
for (let index = stop - 1; index >= start; index--) {
|
|
1011
|
+
const live = this.#liveParentItems[index];
|
|
1012
|
+
bound = Math.min(bound, live.after ?? Infinity);
|
|
1013
|
+
const progress = live.markers.at(-1)?.progress;
|
|
1014
|
+
if (live.item?.kind !== "command" || progress === undefined) {
|
|
1015
|
+
if (bound !== Infinity)
|
|
1016
|
+
live.after = bound;
|
|
1017
|
+
continue;
|
|
1018
|
+
}
|
|
1019
|
+
const { candidates, reserved } = evidence.get(index);
|
|
1020
|
+
let latest;
|
|
1021
|
+
for (const { saved, first } of candidates) {
|
|
1022
|
+
let lower = first;
|
|
1023
|
+
let upper = saved.length;
|
|
1024
|
+
while (lower < upper) {
|
|
1025
|
+
const middle = Math.floor((lower + upper) / 2);
|
|
1026
|
+
if (saved[middle].position < bound)
|
|
1027
|
+
lower = middle + 1;
|
|
1028
|
+
else
|
|
1029
|
+
upper = middle;
|
|
1030
|
+
}
|
|
1031
|
+
const candidate = lower > first ? saved[lower - 1] : undefined;
|
|
1032
|
+
if (candidate !== undefined &&
|
|
1033
|
+
(latest === undefined || candidate.position > latest.position))
|
|
1034
|
+
latest = candidate;
|
|
1035
|
+
}
|
|
1036
|
+
if (latest === undefined &&
|
|
1037
|
+
!reserved &&
|
|
1038
|
+
bound === Infinity &&
|
|
1039
|
+
!isInventoryProgress(progress))
|
|
1040
|
+
continue;
|
|
1041
|
+
const visible = latest?.lastCommandProgress;
|
|
1042
|
+
const fullProjection = bound !== Infinity &&
|
|
1043
|
+
visible !== undefined &&
|
|
1044
|
+
visible.markerIndex === live.markers.length - 1 &&
|
|
1045
|
+
progressKey(visible.update) === progressKey(progress);
|
|
1046
|
+
if (fullProjection)
|
|
1047
|
+
latest.projection = live;
|
|
1048
|
+
live.after = Math.min(live.after ?? Infinity, fullProjection ? visible.position + 1 : bound);
|
|
1049
|
+
// A preceding completion ends before this projection's inclusive edge.
|
|
1050
|
+
bound = Math.min(bound, fullProjection ? visible.position : live.after);
|
|
1051
|
+
}
|
|
1052
|
+
start = stop + 1;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
#matchParentCompletions() {
|
|
1056
|
+
if (this.#parentSession === null || !this.#parentTurnStartKnown)
|
|
1057
|
+
return;
|
|
1058
|
+
const completions = this.#parentSession.completions;
|
|
1059
|
+
// Ordinary live completions can be dropped by native queue backpressure.
|
|
1060
|
+
// Match ordered signatures, not list indexes. Identical output signatures
|
|
1061
|
+
// remain ambiguous; use the earliest compatible occurrence after our anchor.
|
|
1062
|
+
const add = (index, key, queues) => {
|
|
1063
|
+
let queue = queues.get(key);
|
|
1064
|
+
if (queue === undefined) {
|
|
1065
|
+
queue = { indexes: [], next: 0 };
|
|
1066
|
+
queues.set(key, queue);
|
|
1067
|
+
}
|
|
1068
|
+
queue.indexes.push(index);
|
|
1069
|
+
this.#queueParentMatch(key);
|
|
1070
|
+
};
|
|
1071
|
+
while (this.#indexedParentCompletions < completions.length) {
|
|
1072
|
+
const index = this.#indexedParentCompletions++;
|
|
1073
|
+
const item = completions[index].item;
|
|
1074
|
+
if (item !== null)
|
|
1075
|
+
add(index, progressItemKey(item), this.#parentCompletionKeys);
|
|
1076
|
+
}
|
|
1077
|
+
while (this.#indexedParentItems < this.#liveParentItems.length) {
|
|
1078
|
+
const index = this.#indexedParentItems++;
|
|
1079
|
+
const item = this.#liveParentItems[index].item;
|
|
1080
|
+
if (item !== null)
|
|
1081
|
+
for (const key of progressItemKeys(item))
|
|
1082
|
+
add(index, key, this.#parentItemKeys);
|
|
1083
|
+
}
|
|
1084
|
+
while (this.#parentMatches.length > 0) {
|
|
1085
|
+
const index = takeFirstIndex(this.#parentMatches);
|
|
1086
|
+
this.#pendingParentMatches.delete(index);
|
|
1087
|
+
if (index < this.#matchedParentItems)
|
|
1088
|
+
continue;
|
|
1089
|
+
const live = this.#liveParentItems[index];
|
|
1090
|
+
if (live.item === null)
|
|
1091
|
+
continue;
|
|
1092
|
+
let matched;
|
|
1093
|
+
for (const key of progressItemKeys(live.item)) {
|
|
1094
|
+
const queue = this.#parentCompletionKeys.get(key);
|
|
1095
|
+
const savedIndex = queue?.indexes[queue.next];
|
|
1096
|
+
if (savedIndex !== undefined &&
|
|
1097
|
+
(matched === undefined || savedIndex < matched))
|
|
1098
|
+
matched = savedIndex;
|
|
1099
|
+
}
|
|
1100
|
+
if (matched === undefined)
|
|
1101
|
+
continue;
|
|
1102
|
+
live.position = completions[matched].position;
|
|
1103
|
+
completions[matched].projection = live;
|
|
1104
|
+
for (const marker of live.markers)
|
|
1105
|
+
this.#readyParentMarkers.set(marker, live);
|
|
1106
|
+
this.#boundParentCommands(this.#parentSession, this.#matchedParentItems, index + 1, this.#parentCompletionCursor);
|
|
1107
|
+
const changed = new Set();
|
|
1108
|
+
const remove = (key, queues) => {
|
|
1109
|
+
const queue = queues.get(key);
|
|
1110
|
+
if (++queue.next === queue.indexes.length)
|
|
1111
|
+
queues.delete(key);
|
|
1112
|
+
changed.add(key);
|
|
1113
|
+
};
|
|
1114
|
+
// A compatible later anchor lets both prefixes advance. Without an
|
|
1115
|
+
// anchor, retain compact unmatched signatures: reads can precede writes
|
|
1116
|
+
// or native persistence can fail and later retry. Neither blocks other
|
|
1117
|
+
// compatible signatures, and each occurrence is indexed/consumed once.
|
|
1118
|
+
while (this.#matchedParentItems <= index) {
|
|
1119
|
+
const previous = this.#liveParentItems[this.#matchedParentItems++];
|
|
1120
|
+
if (previous.position === null) {
|
|
1121
|
+
if (previous.item?.kind !== "command") {
|
|
1122
|
+
previous.after = Math.min(previous.after ?? Infinity, live.position);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
if (previous.item !== null) {
|
|
1126
|
+
for (const key of progressItemKeys(previous.item))
|
|
1127
|
+
remove(key, this.#parentItemKeys);
|
|
1128
|
+
if (previous.position !== null)
|
|
1129
|
+
previous.item = null;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
while (this.#parentCompletionCursor <= matched) {
|
|
1133
|
+
const saved = completions[this.#parentCompletionCursor++];
|
|
1134
|
+
if (saved.item !== null) {
|
|
1135
|
+
remove(progressItemKey(saved.item), this.#parentCompletionKeys);
|
|
1136
|
+
if (saved.position === live.position) {
|
|
1137
|
+
saved.item = null;
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
for (const key of changed)
|
|
1142
|
+
this.#queueParentMatch(key);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
#queueParentMatch(key) {
|
|
1146
|
+
if (!this.#parentCompletionKeys.has(key))
|
|
1147
|
+
return;
|
|
1148
|
+
const queue = this.#parentItemKeys.get(key);
|
|
1149
|
+
const index = queue?.indexes[queue.next];
|
|
1150
|
+
if (index === undefined || this.#pendingParentMatches.has(index))
|
|
1151
|
+
return;
|
|
1152
|
+
this.#pendingParentMatches.add(index);
|
|
1153
|
+
insertIndex(this.#parentMatches, index);
|
|
1154
|
+
}
|
|
1155
|
+
#corroboratedInventory(live, marker, inventoryWitness) {
|
|
1156
|
+
if (!this.#parentTurnStartKnown ||
|
|
1157
|
+
marker.progress.filesCompleted === 0 ||
|
|
1158
|
+
live.position === null)
|
|
1159
|
+
return undefined;
|
|
1160
|
+
let inventory;
|
|
1161
|
+
let pending;
|
|
1162
|
+
for (let index = this.#liveParentItems.indexOf(live) - 1; index >= 0; index--) {
|
|
1163
|
+
const candidate = this.#liveParentItems[index];
|
|
1164
|
+
inventory = candidate.markers.findLast((observed) => (observed.accepted || observed === this.#parentInventory) &&
|
|
1165
|
+
isInventoryProgress(observed.progress) &&
|
|
1166
|
+
observed.progress.filesTotal === marker.progress.filesTotal);
|
|
1167
|
+
if (inventory !== undefined) {
|
|
1168
|
+
pending = candidate;
|
|
1169
|
+
break;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
if (inventory === undefined ||
|
|
1173
|
+
pending === undefined ||
|
|
1174
|
+
!this.#inventoryAllowedInCompletion(pending, inventory.index))
|
|
1175
|
+
return undefined;
|
|
1176
|
+
let position = pending.position;
|
|
1177
|
+
if (position === null) {
|
|
1178
|
+
const item = pending.item;
|
|
1179
|
+
if (item?.kind !== "command")
|
|
1180
|
+
return undefined;
|
|
1181
|
+
for (const completion of this.#parentSession?.completions ?? []) {
|
|
1182
|
+
if (completion.item?.kind !== "command" ||
|
|
1183
|
+
completion.item.outputDigest !== item.persistedOutputDigest)
|
|
1184
|
+
continue;
|
|
1185
|
+
if (position !== null)
|
|
1186
|
+
return undefined;
|
|
1187
|
+
position = completion.position;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
// A retained source row may have supplied scope before its lossy live copy
|
|
1191
|
+
// arrived. Reusing that receipt does not locate the live command.
|
|
1192
|
+
const source = this.#inventoryFrontier.source;
|
|
1193
|
+
const admittedSource = source?.inventoryAdmitted === true &&
|
|
1194
|
+
source.position === position &&
|
|
1195
|
+
source.markerIndex === inventory.index &&
|
|
1196
|
+
progressKey(source.update) === progressKey(inventory.progress);
|
|
1197
|
+
// A matched review can corroborate an accepted inventory or admit a lost
|
|
1198
|
+
// inventory without assigning its command a guessed occurrence. A later
|
|
1199
|
+
// inventory still wins, and duplicate lossy output remains ambiguous.
|
|
1200
|
+
const bounded = position !== null &&
|
|
1201
|
+
(position > this.#inventoryFrontier.position ||
|
|
1202
|
+
(position === this.#inventoryFrontier.position &&
|
|
1203
|
+
((pending.position !== null &&
|
|
1204
|
+
inventory.index >= this.#inventoryFrontier.markerIndex) ||
|
|
1205
|
+
admittedSource))) &&
|
|
1206
|
+
(position < live.position ||
|
|
1207
|
+
(pending.position !== null &&
|
|
1208
|
+
position === live.position &&
|
|
1209
|
+
inventory.index < marker.index));
|
|
1210
|
+
if (!bounded ||
|
|
1211
|
+
!this.#inventoryPrecedesReview(position, pending.position === null ? 0 : inventory.index))
|
|
1212
|
+
return undefined;
|
|
1213
|
+
const later = this.#parentSession?.progress.findLast((saved) => !saved.duplicate &&
|
|
1214
|
+
isInventoryProgress(saved.update) &&
|
|
1215
|
+
(saved.position > position ||
|
|
1216
|
+
(pending.position !== null &&
|
|
1217
|
+
saved.position === position &&
|
|
1218
|
+
saved.markerIndex > inventory.index)) &&
|
|
1219
|
+
(saved.position < live.position ||
|
|
1220
|
+
(saved.position === live.position &&
|
|
1221
|
+
saved.markerIndex < marker.index)) &&
|
|
1222
|
+
this.#parentInventoryAdmission(saved, undefined, {
|
|
1223
|
+
live: pending,
|
|
1224
|
+
after: {
|
|
1225
|
+
position: position,
|
|
1226
|
+
markerIndex: inventory.index,
|
|
1227
|
+
order: inventory.order,
|
|
1228
|
+
},
|
|
1229
|
+
}, inventoryWitness) !== "retained");
|
|
1230
|
+
// Corroboration admits scope without relocating the inventory to the review.
|
|
1231
|
+
// Its original live order continues to participate alongside newer scopes.
|
|
1232
|
+
if (later === undefined && pending.position === null) {
|
|
1233
|
+
inventory.accepted = true;
|
|
1234
|
+
return undefined;
|
|
1235
|
+
}
|
|
1236
|
+
return later === undefined
|
|
1237
|
+
? {
|
|
1238
|
+
progress: inventory.progress,
|
|
1239
|
+
position: pending.position,
|
|
1240
|
+
markerIndex: inventory.index,
|
|
1241
|
+
order: inventory.order,
|
|
1242
|
+
}
|
|
1243
|
+
: {
|
|
1244
|
+
progress: later.update,
|
|
1245
|
+
position: later.position,
|
|
1246
|
+
markerIndex: later.markerIndex,
|
|
1247
|
+
order: (later.order ??= ++this.#progressOrder),
|
|
1248
|
+
afterLive: pending,
|
|
1249
|
+
source: later,
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
#reconcileParentProgress() {
|
|
1253
|
+
const ready = [...this.#readyParentMarkers];
|
|
1254
|
+
this.#readyParentMarkers.clear();
|
|
1255
|
+
for (const [marker, live] of ready) {
|
|
1256
|
+
if (live.position !== null) {
|
|
1257
|
+
this.#rememberParentFact({
|
|
1258
|
+
progress: marker.progress,
|
|
1259
|
+
order: marker.order,
|
|
1260
|
+
position: live.position,
|
|
1261
|
+
markerIndex: marker.index,
|
|
1262
|
+
live,
|
|
1263
|
+
});
|
|
1264
|
+
if (marker.accepted)
|
|
1265
|
+
this.#recordParentBoundary(marker.progress, live.position, marker.index, true, marker.scopeFilesTotal, live);
|
|
1266
|
+
}
|
|
1267
|
+
if (marker.accepted && live.position !== null) {
|
|
1268
|
+
const source = { position: live.position, markerIndex: marker.index };
|
|
1269
|
+
if (this.#parentProgress?.order === marker.order)
|
|
1270
|
+
Object.assign(this.#parentProgress, source);
|
|
1271
|
+
if (this.#displayedProgress?.owner !== "worker" &&
|
|
1272
|
+
this.#displayedProgress?.order === marker.order)
|
|
1273
|
+
Object.assign(this.#displayedProgress, source);
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
if (this.#parentSession !== null)
|
|
1277
|
+
this.#boundParentCommands(this.#parentSession);
|
|
1278
|
+
const unresolvedCommand = this.#unresolvedParentCommand();
|
|
1279
|
+
// Source proof and its eligible review cutoff precede inventory admission.
|
|
1280
|
+
// A count-rejected review can invalidate a later provisional inventory.
|
|
1281
|
+
const sourceInventory = this.#recordParentSourceBoundaries(unresolvedCommand);
|
|
1282
|
+
let witnessParent = this.#acceptedParent;
|
|
1283
|
+
let inventoryWitness = this.#parentInventoryWitness();
|
|
1284
|
+
if (!this.#inventoryPrecedesReview(this.#inventoryFrontier.position, this.#inventoryFrontier.markerIndex, this.#inventoryFrontier.live ??
|
|
1285
|
+
this.#inventoryFrontier.source?.projection, this.#inventoryFrontier.source))
|
|
1286
|
+
this.#inventoryFrontier = { position: 0, markerIndex: -1, order: 0 };
|
|
1287
|
+
const acceptedInventory = this.#acceptedInventory();
|
|
1288
|
+
let corrected = null;
|
|
1289
|
+
for (const [marker, live] of ready) {
|
|
1290
|
+
const position = live.position;
|
|
1291
|
+
if (position === null)
|
|
1292
|
+
continue;
|
|
1293
|
+
const corroborated = this.#corroboratedInventory(live, marker, inventoryWitness);
|
|
1294
|
+
const progress = corroborated?.progress ?? marker.progress;
|
|
1295
|
+
if (marker === this.#parentInventory &&
|
|
1296
|
+
!marker.accepted &&
|
|
1297
|
+
this.#parentProgress !== null &&
|
|
1298
|
+
this.#parentProgress.progress.filesCompleted > 0 &&
|
|
1299
|
+
this.#parentIsEligible(this.#parentProgress) &&
|
|
1300
|
+
this.#parentInventoryBoundary?.reviewed !== true) {
|
|
1301
|
+
// A read can reach EOF before the next completion is appended, then
|
|
1302
|
+
// receive both live items while file.close is pending. Keep the compact
|
|
1303
|
+
// inventory proof until the admitted review's source position resolves.
|
|
1304
|
+
this.#readyParentMarkers.set(marker, live);
|
|
1305
|
+
continue;
|
|
1306
|
+
}
|
|
1307
|
+
if ((corroborated !== undefined ||
|
|
1308
|
+
marker === acceptedInventory?.marker ||
|
|
1309
|
+
(marker === this.#parentInventory &&
|
|
1310
|
+
this.#inventoryPrecedesReview(position, marker.index, live))) &&
|
|
1311
|
+
(corroborated !== undefined ||
|
|
1312
|
+
this.#inventoryPrecedesReview(position, marker.index, live)) &&
|
|
1313
|
+
isInventoryProgress(progress) &&
|
|
1314
|
+
(position > this.#inventoryFrontier.position ||
|
|
1315
|
+
(position === this.#inventoryFrontier.position &&
|
|
1316
|
+
marker.index >= this.#inventoryFrontier.markerIndex))) {
|
|
1317
|
+
// A saved occurrence remains authoritative when its live copy arrives
|
|
1318
|
+
// after polling consumed it, even if provisional coverage rejects it.
|
|
1319
|
+
// Inventory may precede already-admitted review even when its full live
|
|
1320
|
+
// marker arrives later. For lossy output, the real matched review marks
|
|
1321
|
+
// the scope-proof boundary; the missing inventory position stays unknown.
|
|
1322
|
+
this.#inventoryFrontier = corroborated ?? {
|
|
1323
|
+
position,
|
|
1324
|
+
markerIndex: marker.index,
|
|
1325
|
+
order: marker.order,
|
|
1326
|
+
progress,
|
|
1327
|
+
live,
|
|
1328
|
+
};
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
if (acceptedInventory !== undefined &&
|
|
1332
|
+
acceptedInventory.marker === this.#parentInventory &&
|
|
1333
|
+
acceptedInventory.live.position === null &&
|
|
1334
|
+
(!this.#parentIsEligible(this.#parentProgress) ||
|
|
1335
|
+
this.#parentProgress.progress.filesCompleted === 0) &&
|
|
1336
|
+
acceptedInventory.order > (this.#parentProgress?.order ?? 0)) {
|
|
1337
|
+
// This live inventory was accepted for deferred scope reconciliation;
|
|
1338
|
+
// its saved occurrence remains unknown until source matching resolves it.
|
|
1339
|
+
const progress = acceptedInventory.marker.progress;
|
|
1340
|
+
this.#acceptedParent = acceptedInventory.live;
|
|
1341
|
+
this.#parentProgress = {
|
|
1342
|
+
progress,
|
|
1343
|
+
order: acceptedInventory.order,
|
|
1344
|
+
live: acceptedInventory.live,
|
|
1345
|
+
markerIndex: acceptedInventory.marker.index,
|
|
1346
|
+
};
|
|
1347
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
1348
|
+
corrected = { ...this.#parentProgress, owner: "live" };
|
|
1349
|
+
witnessParent = this.#acceptedParent;
|
|
1350
|
+
inventoryWitness = this.#parentInventoryWitness();
|
|
1351
|
+
}
|
|
1352
|
+
// Recover eligible retained source authority before a baseline rollback
|
|
1353
|
+
// could temporarily revive an out-of-scope parent or worker phase.
|
|
1354
|
+
if (this.#retainedInventory() === undefined &&
|
|
1355
|
+
this.#acceptedInventory() === undefined &&
|
|
1356
|
+
sourceInventory?.source !== undefined) {
|
|
1357
|
+
const admission = this.#parentInventoryAdmission(sourceInventory.source, unresolvedCommand, undefined, inventoryWitness);
|
|
1358
|
+
if (admission === "current" || admission === "proven")
|
|
1359
|
+
this.#inventoryFrontier = sourceInventory;
|
|
1360
|
+
}
|
|
1361
|
+
const inventory = this.#retainedInventory();
|
|
1362
|
+
this.#projectParentInventory();
|
|
1363
|
+
const inventoryChanged = this.#inventoryNeedsReconciliation;
|
|
1364
|
+
this.#inventoryNeedsReconciliation = false;
|
|
1365
|
+
const parentIneligible = this.#parentProgress !== null &&
|
|
1366
|
+
!this.#parentIsEligible(this.#parentProgress);
|
|
1367
|
+
const inventoryReconciled = inventoryChanged || parentIneligible;
|
|
1368
|
+
if (inventoryReconciled) {
|
|
1369
|
+
let retainedWorker = null;
|
|
1370
|
+
// Reconsider compact worker receipts under the proven inventory. Keep an
|
|
1371
|
+
// oversized receipt available without counting it as reviewed coverage.
|
|
1372
|
+
for (const { source: session, } of this.#reportedSessionPositions.values()) {
|
|
1373
|
+
if (session.threadId === null || session.threadId === this.#threadId)
|
|
1374
|
+
continue;
|
|
1375
|
+
for (const [total, pending] of session.pendingWorkerProgress) {
|
|
1376
|
+
const progress = pending.progress;
|
|
1377
|
+
if (total > this.#expectedFilesTotal ||
|
|
1378
|
+
(session.filesTotal !== null && total !== session.filesTotal) ||
|
|
1379
|
+
progress.filesCompleted < session.filesCompleted)
|
|
1380
|
+
continue;
|
|
1381
|
+
session.filesTotal = total;
|
|
1382
|
+
session.filesCompleted = progress.filesCompleted;
|
|
1383
|
+
const latest = session.latestWorkerProgress;
|
|
1384
|
+
if (latest === null ||
|
|
1385
|
+
progress.filesCompleted > latest.progress.filesCompleted ||
|
|
1386
|
+
pending.order > latest.order)
|
|
1387
|
+
session.latestWorkerProgress = pending;
|
|
1388
|
+
session.pendingWorkerProgress.delete(total);
|
|
1389
|
+
if (corrected === null || pending.order > corrected.order)
|
|
1390
|
+
corrected = { ...pending, owner: "worker" };
|
|
1391
|
+
}
|
|
1392
|
+
if (session.filesTotal !== null &&
|
|
1393
|
+
session.filesTotal <= this.#expectedFilesTotal) {
|
|
1394
|
+
this.#workerProgress.set(session.threadId, session.filesCompleted);
|
|
1395
|
+
const latest = session.latestWorkerProgress;
|
|
1396
|
+
if (latest !== null &&
|
|
1397
|
+
(retainedWorker === null || latest.order > retainedWorker.order))
|
|
1398
|
+
retainedWorker = latest;
|
|
1399
|
+
if (parentIneligible &&
|
|
1400
|
+
latest !== null &&
|
|
1401
|
+
(corrected === null || latest.order > corrected.order))
|
|
1402
|
+
corrected = { ...latest, owner: "worker" };
|
|
1403
|
+
}
|
|
1404
|
+
else
|
|
1405
|
+
this.#workerProgress.delete(session.threadId);
|
|
1406
|
+
}
|
|
1407
|
+
const parentCount = this.#parentIsEligible(this.#parentProgress)
|
|
1408
|
+
? this.#parentProgress.progress.filesCompleted
|
|
1409
|
+
: 0;
|
|
1410
|
+
// Project the phase from the greatest eligible coverage. A zero-count
|
|
1411
|
+
// inventory establishes scope but cannot mask restored worker progress.
|
|
1412
|
+
const workerCoverage = Math.min(this.#expectedFilesTotal, this.#workerProgress.values().reduce((sum, count) => sum + count, 0));
|
|
1413
|
+
if (retainedWorker !== null &&
|
|
1414
|
+
workerCoverage > parentCount &&
|
|
1415
|
+
(corrected === null ||
|
|
1416
|
+
(corrected.owner !== "worker" &&
|
|
1417
|
+
workerCoverage > corrected.progress.filesCompleted)))
|
|
1418
|
+
corrected = { ...retainedWorker, owner: "worker" };
|
|
1419
|
+
this.#highestFilesCompleted = Math.max(parentCount, workerCoverage);
|
|
1420
|
+
if (this.#parentSession !== null) {
|
|
1421
|
+
this.#parentSession.filesCompleted = parentCount;
|
|
1422
|
+
this.#parentSession.filesTotal = this.#expectedFilesTotal;
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
const workerCoverage = Math.min(this.#expectedFilesTotal ?? Number.MAX_SAFE_INTEGER, this.#workerProgress.values().reduce((sum, count) => sum + count, 0));
|
|
1426
|
+
for (const currentCompletion of this.#liveParentItems) {
|
|
1427
|
+
if (currentCompletion.position !== null ||
|
|
1428
|
+
// Unfamiliar projections supply no source identity. Keep the existing
|
|
1429
|
+
// recovery of an admitted completion's remaining parsed markers.
|
|
1430
|
+
(currentCompletion.item === null &&
|
|
1431
|
+
!currentCompletion.markers.some((marker) => marker.accepted)))
|
|
1432
|
+
continue;
|
|
1433
|
+
// A bound orders other completions; it does not retire later markers
|
|
1434
|
+
// inside any retained completion. Scope can make those markers eligible
|
|
1435
|
+
// after provisional worker coverage prevented their live admission.
|
|
1436
|
+
for (const marker of currentCompletion.markers) {
|
|
1437
|
+
const progress = marker.progress;
|
|
1438
|
+
const candidate = {
|
|
1439
|
+
progress,
|
|
1440
|
+
order: marker.order,
|
|
1441
|
+
live: currentCompletion,
|
|
1442
|
+
markerIndex: marker.index,
|
|
1443
|
+
owner: "live",
|
|
1444
|
+
};
|
|
1445
|
+
if (marker.accepted ||
|
|
1446
|
+
!this.#parentSourceIsEligible(candidate) ||
|
|
1447
|
+
// Inventory has its own admission path. A rejected inventory cannot
|
|
1448
|
+
// become a phase merely because coverage later falls to zero.
|
|
1449
|
+
isInventoryProgress(progress) ||
|
|
1450
|
+
progress.filesTotal >
|
|
1451
|
+
(this.#expectedFilesTotal ?? Number.MAX_SAFE_INTEGER) ||
|
|
1452
|
+
progress.filesCompleted < this.#highestFilesCompleted ||
|
|
1453
|
+
(this.#parentProgress !== null &&
|
|
1454
|
+
compareProgressOrder(candidate, this.#parentProgress) < 0))
|
|
1455
|
+
continue;
|
|
1456
|
+
marker.accepted = true;
|
|
1457
|
+
this.#acceptedParent = currentCompletion;
|
|
1458
|
+
this.#recordParentBoundary(progress, undefined, marker.index, true, undefined, currentCompletion);
|
|
1459
|
+
this.#parentProgress = candidate;
|
|
1460
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
1461
|
+
this.#highestFilesCompleted = progress.filesCompleted;
|
|
1462
|
+
const correctedCoverage = corrected?.owner === "worker"
|
|
1463
|
+
? workerCoverage
|
|
1464
|
+
: (corrected?.progress.filesCompleted ?? -1);
|
|
1465
|
+
if (progress.filesCompleted > correctedCoverage ||
|
|
1466
|
+
(progress.filesCompleted === correctedCoverage &&
|
|
1467
|
+
corrected !== null &&
|
|
1468
|
+
compareProgressOrder(candidate, corrected) > 0))
|
|
1469
|
+
corrected = candidate;
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
for (const [marker, live] of ready) {
|
|
1473
|
+
const position = live.position;
|
|
1474
|
+
const progress = marker.progress;
|
|
1475
|
+
const deferredCoverage = !marker.accepted &&
|
|
1476
|
+
progress.filesCompleted > this.#highestFilesCompleted;
|
|
1477
|
+
if (position === null ||
|
|
1478
|
+
(!deferredCoverage &&
|
|
1479
|
+
!this.#afterParentFrontier(position, marker.index) &&
|
|
1480
|
+
!(inventoryChanged &&
|
|
1481
|
+
marker === this.#parentInventory &&
|
|
1482
|
+
position === this.#parentFrontier.position &&
|
|
1483
|
+
marker.index === this.#parentFrontier.markerIndex)))
|
|
1484
|
+
continue;
|
|
1485
|
+
const candidate = {
|
|
1486
|
+
progress,
|
|
1487
|
+
order: marker.order,
|
|
1488
|
+
position,
|
|
1489
|
+
markerIndex: marker.index,
|
|
1490
|
+
live,
|
|
1491
|
+
owner: "live",
|
|
1492
|
+
};
|
|
1493
|
+
const selectedCoverage = corrected?.owner === "worker"
|
|
1494
|
+
? workerCoverage
|
|
1495
|
+
: (corrected?.progress.filesCompleted ?? -1);
|
|
1496
|
+
// Do not let a newer, lower-coverage worker candidate mask a valid parent
|
|
1497
|
+
// correction before the final display guards can consider it.
|
|
1498
|
+
const preferCandidate = corrected === null ||
|
|
1499
|
+
progress.filesCompleted > selectedCoverage ||
|
|
1500
|
+
(progress.filesCompleted === selectedCoverage &&
|
|
1501
|
+
compareProgressOrder(candidate, corrected) > 0);
|
|
1502
|
+
if (!marker.accepted) {
|
|
1503
|
+
if ((this.#expectedFilesTotal !== undefined &&
|
|
1504
|
+
progress.filesTotal > this.#expectedFilesTotal) ||
|
|
1505
|
+
progress.filesTotal >
|
|
1506
|
+
(marker.scopeFilesTotal ?? Number.MAX_SAFE_INTEGER) ||
|
|
1507
|
+
progress.filesCompleted < this.#highestFilesCompleted ||
|
|
1508
|
+
(isInventoryProgress(progress) &&
|
|
1509
|
+
!this.#inventoryPrecedesReview(position, marker.index, live))) {
|
|
1510
|
+
// Saved inventory can admit the live receipt or invalidate provisional
|
|
1511
|
+
// worker coverage. Keep only the parsed fact until it is eligible.
|
|
1512
|
+
this.#readyParentMarkers.set(marker, live);
|
|
1513
|
+
continue;
|
|
1514
|
+
}
|
|
1515
|
+
marker.accepted = true;
|
|
1516
|
+
this.#acceptedParent = live;
|
|
1517
|
+
const selectedParent = this.#parentProgress;
|
|
1518
|
+
if (!this.#parentIsEligible(selectedParent) ||
|
|
1519
|
+
progress.filesCompleted !== selectedParent.progress.filesCompleted ||
|
|
1520
|
+
compareProgressOrder(candidate, selectedParent) >= 0)
|
|
1521
|
+
this.#parentProgress = {
|
|
1522
|
+
progress,
|
|
1523
|
+
order: marker.order,
|
|
1524
|
+
position,
|
|
1525
|
+
markerIndex: marker.index,
|
|
1526
|
+
live,
|
|
1527
|
+
};
|
|
1528
|
+
this.#recordParentBoundary(progress, position, marker.index, false, marker.scopeFilesTotal);
|
|
1529
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
1530
|
+
this.#highestFilesCompleted = progress.filesCompleted;
|
|
1531
|
+
if (preferCandidate)
|
|
1532
|
+
corrected = candidate;
|
|
1533
|
+
}
|
|
1534
|
+
else if (this.#parentIsEligible(candidate) &&
|
|
1535
|
+
this.#displayedProgress?.owner === "parent" &&
|
|
1536
|
+
compareProgressOrder(candidate, this.#displayedProgress) > 0 &&
|
|
1537
|
+
preferCandidate) {
|
|
1538
|
+
corrected = candidate;
|
|
1539
|
+
}
|
|
1540
|
+
this.#advanceParentFrontier(position, marker.index);
|
|
1541
|
+
}
|
|
1542
|
+
if (witnessParent !== this.#acceptedParent)
|
|
1543
|
+
inventoryWitness = this.#parentInventoryWitness();
|
|
1544
|
+
// Parent facts outlive display admission and queue draining. Preserve the
|
|
1545
|
+
// greatest eligible coverage for each original inventory, then source order.
|
|
1546
|
+
let eligible = this.#parentIsEligible(this.#parentProgress)
|
|
1547
|
+
? this.#parentProgress
|
|
1548
|
+
: null;
|
|
1549
|
+
for (const candidate of [
|
|
1550
|
+
...this.#parentFacts.values(),
|
|
1551
|
+
...(inventory === undefined ? [] : [inventory]),
|
|
1552
|
+
]) {
|
|
1553
|
+
// Apply eligible preceding inventories before promoting a retained phase.
|
|
1554
|
+
// Retained copies and unresolved alternatives are not unapplied work.
|
|
1555
|
+
if (this.#parentSession?.progress.some((saved) => {
|
|
1556
|
+
const admission = this.#parentInventoryAdmission(saved, unresolvedCommand, undefined, inventoryWitness);
|
|
1557
|
+
return ((admission === "current" || admission === "proven") &&
|
|
1558
|
+
(saved.position < candidate.position ||
|
|
1559
|
+
(saved.position === candidate.position &&
|
|
1560
|
+
saved.markerIndex < candidate.markerIndex)));
|
|
1561
|
+
}))
|
|
1562
|
+
continue;
|
|
1563
|
+
if (!this.#parentIsEligible(candidate) ||
|
|
1564
|
+
candidate.progress.filesCompleted < workerCoverage)
|
|
1565
|
+
continue;
|
|
1566
|
+
if (eligible === null ||
|
|
1567
|
+
candidate.progress.filesCompleted > eligible.progress.filesCompleted ||
|
|
1568
|
+
(candidate.progress.filesCompleted ===
|
|
1569
|
+
eligible.progress.filesCompleted &&
|
|
1570
|
+
compareProgressOrder(candidate, eligible) > 0))
|
|
1571
|
+
eligible = candidate;
|
|
1572
|
+
}
|
|
1573
|
+
if (eligible !== null) {
|
|
1574
|
+
this.#parentProgress = eligible;
|
|
1575
|
+
this.#advanceParentFrontier(eligible.position, eligible.markerIndex);
|
|
1576
|
+
this.#recordParentBoundary(eligible.progress, eligible.position, eligible.markerIndex, false, eligible.source?.scopeFilesTotal ??
|
|
1577
|
+
eligible.live?.markers[eligible.markerIndex ?? 0]?.scopeFilesTotal, eligible.live);
|
|
1578
|
+
this.#highestFilesCompleted = Math.max(workerCoverage, eligible.progress.filesCompleted);
|
|
1579
|
+
if (this.#parentSession !== null) {
|
|
1580
|
+
this.#parentSession.filesCompleted = eligible.progress.filesCompleted;
|
|
1581
|
+
this.#parentSession.filesTotal =
|
|
1582
|
+
this.#expectedFilesTotal ?? eligible.progress.filesTotal;
|
|
1583
|
+
}
|
|
1584
|
+
const correctedCoverage = corrected?.owner === "worker"
|
|
1585
|
+
? workerCoverage
|
|
1586
|
+
: (corrected?.progress.filesCompleted ?? -1);
|
|
1587
|
+
if (corrected === null ||
|
|
1588
|
+
eligible.progress.filesCompleted > correctedCoverage ||
|
|
1589
|
+
(eligible.progress.filesCompleted === correctedCoverage &&
|
|
1590
|
+
compareProgressOrder(eligible, corrected) > 0))
|
|
1591
|
+
corrected = { ...eligible, owner: "parent" };
|
|
1592
|
+
}
|
|
1593
|
+
const displayed = this.#displayedProgress;
|
|
1594
|
+
const displayedParentIneligible = displayed !== null &&
|
|
1595
|
+
displayed.owner !== "worker" &&
|
|
1596
|
+
!this.#parentIsEligible({
|
|
1597
|
+
...displayed,
|
|
1598
|
+
// Scope recovery can replace the parent before changing the display.
|
|
1599
|
+
// Requalify the receipt that actually supplied the displayed phase.
|
|
1600
|
+
progress: displayed.live?.markers[displayed.markerIndex ?? 0]?.progress ??
|
|
1601
|
+
displayed.source?.update ??
|
|
1602
|
+
displayed.update,
|
|
1603
|
+
});
|
|
1604
|
+
if (!inventoryReconciled && corrected === null)
|
|
1605
|
+
return inventoryWitness;
|
|
1606
|
+
const correctedCoverage = corrected?.owner === "worker"
|
|
1607
|
+
? workerCoverage
|
|
1608
|
+
: (corrected?.progress.filesCompleted ?? 0);
|
|
1609
|
+
const replacePhase = corrected !== null &&
|
|
1610
|
+
correctedCoverage >= this.#highestFilesCompleted &&
|
|
1611
|
+
(displayed === null ||
|
|
1612
|
+
displayedParentIneligible ||
|
|
1613
|
+
correctedCoverage > displayed.update.filesCompleted ||
|
|
1614
|
+
compareProgressOrder(corrected, displayed) >= 0);
|
|
1615
|
+
const selected = replacePhase ? corrected : displayed;
|
|
1616
|
+
const parent = this.#parentProgress;
|
|
1617
|
+
const parentAtEqualCoverage = selected?.owner === "worker" &&
|
|
1618
|
+
this.#parentIsEligible(parent) &&
|
|
1619
|
+
parent.progress.filesCompleted === this.#highestFilesCompleted;
|
|
1620
|
+
const phase = parentAtEqualCoverage
|
|
1621
|
+
? parent.progress.phase
|
|
1622
|
+
: replacePhase
|
|
1623
|
+
? corrected.progress.phase
|
|
1624
|
+
: (displayed?.update.phase ?? "discovery");
|
|
1625
|
+
const progress = {
|
|
1626
|
+
phase,
|
|
1627
|
+
filesCompleted: this.#highestFilesCompleted,
|
|
1628
|
+
filesTotal: this.#expectedFilesTotal ??
|
|
1629
|
+
corrected?.progress.filesTotal ??
|
|
1630
|
+
displayed.update.filesTotal,
|
|
1631
|
+
};
|
|
1632
|
+
const key = progressKey(progress);
|
|
1633
|
+
// An initial zero-count discovery already reported this inventory. Other
|
|
1634
|
+
// equal raw keys can still require synchronization of the API's file total.
|
|
1635
|
+
const inventoryReported = isInventoryProgress(progress);
|
|
1636
|
+
const duplicate = displayed !== null &&
|
|
1637
|
+
progressKey(displayed.update) === key &&
|
|
1638
|
+
(!inventoryReconciled || inventoryReported);
|
|
1639
|
+
const displaySource = parentAtEqualCoverage
|
|
1640
|
+
? { ...parent, owner: "parent" }
|
|
1641
|
+
: selected;
|
|
1642
|
+
this.#displayedProgress = {
|
|
1643
|
+
update: progress,
|
|
1644
|
+
owner: displaySource?.owner ?? "live",
|
|
1645
|
+
order: displaySource?.order ?? 0,
|
|
1646
|
+
live: displaySource?.live,
|
|
1647
|
+
source: displaySource?.source,
|
|
1648
|
+
markerIndex: displaySource?.markerIndex,
|
|
1649
|
+
...(displaySource?.position === undefined
|
|
1650
|
+
? {}
|
|
1651
|
+
: {
|
|
1652
|
+
position: displaySource.position,
|
|
1653
|
+
}),
|
|
1654
|
+
};
|
|
1655
|
+
if (duplicate)
|
|
1656
|
+
return inventoryWitness;
|
|
1657
|
+
this.#options.onProgress?.(progress, {
|
|
1658
|
+
reconciled: true,
|
|
1659
|
+
...(displaySource?.owner === "worker" ? { worker: true } : {}),
|
|
1660
|
+
// Recovered prefix scope can invalidate coverage even when a later
|
|
1661
|
+
// inventory restores the same total. Synchronize the API's count too.
|
|
1662
|
+
...(inventoryReconciled ? { inventory: true } : {}),
|
|
1663
|
+
});
|
|
1664
|
+
return inventoryWitness;
|
|
1665
|
+
}
|
|
1666
|
+
#savedParentProgressIsCurrent(saved) {
|
|
1667
|
+
if (!this.#afterParentFrontier(saved.position, saved.markerIndex))
|
|
1668
|
+
return false;
|
|
1669
|
+
// An outer tool result has its own completion order. Native output does not
|
|
1670
|
+
// identify whether its text was copied from a nested command or emitted
|
|
1671
|
+
// directly, so text equality and call-start time cannot establish a copy.
|
|
1672
|
+
// Treat it as a receipt; a copied truncated preview can consequently hide
|
|
1673
|
+
// omitted same-count phases from its earlier command. This is best effort.
|
|
1674
|
+
if (saved.receipt)
|
|
1675
|
+
return true;
|
|
1676
|
+
const live = this.#acceptedParent;
|
|
1677
|
+
if (live === null)
|
|
1678
|
+
return true;
|
|
1679
|
+
// A later saved occurrence may legitimately repeat an earlier live tuple,
|
|
1680
|
+
// including progress recovered only from the original multipart response.
|
|
1681
|
+
if (live.position !== null)
|
|
1682
|
+
return saved.position > live.position;
|
|
1683
|
+
if (live.after !== undefined)
|
|
1684
|
+
return saved.position >= live.after;
|
|
1685
|
+
return !this.#unboundParentCopy(saved);
|
|
1686
|
+
}
|
|
1687
|
+
#unboundParentCopy(saved) {
|
|
1688
|
+
const live = this.#acceptedParent;
|
|
1689
|
+
return (!saved.receipt &&
|
|
1690
|
+
live?.position === null &&
|
|
1691
|
+
live.after === undefined &&
|
|
1692
|
+
this.#liveParentProgress.has(progressKey(saved.update)));
|
|
1693
|
+
}
|
|
87
1694
|
recordUsage(usage, threadId = this.#threadId) {
|
|
88
1695
|
const normalized = tokenUsage(usage);
|
|
89
1696
|
if (threadId !== null) {
|
|
@@ -97,6 +1704,7 @@ export class ScanCostTracker {
|
|
|
97
1704
|
if (this.#options.maxCostUsd === undefined &&
|
|
98
1705
|
this.#options.onCost === undefined &&
|
|
99
1706
|
this.#options.onUsage === undefined &&
|
|
1707
|
+
this.#options.onMeasurement === undefined &&
|
|
100
1708
|
this.#options.onUsageObservation === undefined &&
|
|
101
1709
|
this.#options.onActivity === undefined &&
|
|
102
1710
|
this.#options.onProgress === undefined &&
|
|
@@ -112,7 +1720,7 @@ export class ScanCostTracker {
|
|
|
112
1720
|
return;
|
|
113
1721
|
}
|
|
114
1722
|
polling = true;
|
|
115
|
-
void this.refresh()
|
|
1723
|
+
void this.refresh(true)
|
|
116
1724
|
.catch((error) => {
|
|
117
1725
|
this.#options.onError?.(error);
|
|
118
1726
|
})
|
|
@@ -128,14 +1736,43 @@ export class ScanCostTracker {
|
|
|
128
1736
|
this.#timer.unref();
|
|
129
1737
|
poll();
|
|
130
1738
|
}
|
|
131
|
-
async refresh() {
|
|
1739
|
+
async refresh(polling = false) {
|
|
132
1740
|
const update = this.#pending.then(async () => {
|
|
133
|
-
|
|
1741
|
+
try {
|
|
1742
|
+
await this.#refreshUsage(polling);
|
|
1743
|
+
}
|
|
1744
|
+
catch (error) {
|
|
1745
|
+
const previous = this.#snapshot.measurement;
|
|
1746
|
+
this.#setSnapshot(previous.usage, previous.cost, false, previous.threadCount, Math.max(1, previous.missingThreadCount), previous);
|
|
1747
|
+
throw error;
|
|
1748
|
+
}
|
|
134
1749
|
});
|
|
135
1750
|
this.#pending = update.catch(() => { });
|
|
136
1751
|
await update;
|
|
137
1752
|
return this.#snapshot;
|
|
138
1753
|
}
|
|
1754
|
+
async #refreshUsage(polling) {
|
|
1755
|
+
const { readBudgetUsage } = this.#options;
|
|
1756
|
+
if (readBudgetUsage === undefined)
|
|
1757
|
+
return this.#readSessions();
|
|
1758
|
+
if (this.#threadId === null)
|
|
1759
|
+
return;
|
|
1760
|
+
if (this.#options.onActivity ||
|
|
1761
|
+
this.#options.onProgress ||
|
|
1762
|
+
this.#options.onWorkerEvent ||
|
|
1763
|
+
this.#options.onSessionEvent ||
|
|
1764
|
+
this.#options.onUsageObservation) {
|
|
1765
|
+
await this.#readSessions().catch((error) => this.#options.onObservationError?.(error));
|
|
1766
|
+
}
|
|
1767
|
+
// Reading the indexed graph is more expensive than tailing session activity.
|
|
1768
|
+
// Always get fresh usage when stopping, even after a recent live poll.
|
|
1769
|
+
if (polling && performance.now() - this.#lastBudgetPoll < 1_000)
|
|
1770
|
+
return;
|
|
1771
|
+
const measured = await readBudgetUsage(this.#threadId);
|
|
1772
|
+
this.#lastBudgetPoll = performance.now();
|
|
1773
|
+
this.#updateCost(measured.usage, measured.complete, measured.threadCount ?? (measured.usage === null ? 0 : 1), measured.missingThreadCount ??
|
|
1774
|
+
(measured.complete || measured.warnings?.length ? 0 : 1), measured);
|
|
1775
|
+
}
|
|
139
1776
|
async stop(fallbackUsage) {
|
|
140
1777
|
clearInterval(this.#timer ?? undefined);
|
|
141
1778
|
this.#timer = null;
|
|
@@ -147,11 +1784,15 @@ export class ScanCostTracker {
|
|
|
147
1784
|
finally {
|
|
148
1785
|
this.#observingWorkers = false;
|
|
149
1786
|
}
|
|
150
|
-
if (this.#
|
|
1787
|
+
if (this.#options.readBudgetUsage ||
|
|
1788
|
+
this.#receipts.size > 0 ||
|
|
1789
|
+
this.#snapshot.measurement.usage !== null ||
|
|
1790
|
+
this.#snapshot.measurement.missingThreadCount > 0)
|
|
151
1791
|
return this.#snapshot;
|
|
152
|
-
const cost = estimateScanCost(this.#options.model, fallbackUsage);
|
|
153
|
-
|
|
154
|
-
this.#
|
|
1792
|
+
const cost = estimateScanCost(this.#options.model, fallbackUsage, this.#options.pricingContext);
|
|
1793
|
+
const usage = tokenUsage(fallbackUsage);
|
|
1794
|
+
this.#setSnapshot(usage, cost, usage !== null, usage === null ? 0 : 1, 0);
|
|
1795
|
+
this.#reportUsage(usage);
|
|
155
1796
|
this.#reportCost(cost);
|
|
156
1797
|
return this.#snapshot;
|
|
157
1798
|
}
|
|
@@ -162,23 +1803,56 @@ export class ScanCostTracker {
|
|
|
162
1803
|
const repository = this.#options.onActivity === undefined
|
|
163
1804
|
? undefined
|
|
164
1805
|
: this.#options.repository;
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
this.#
|
|
171
|
-
|
|
1806
|
+
const collectionErrors = [];
|
|
1807
|
+
const progressThread = this.#options.onProgress === undefined ? undefined : this.#threadId;
|
|
1808
|
+
const observeHealthySessions = this.#options.maxCostUsd === undefined &&
|
|
1809
|
+
(this.#options.onActivity !== undefined ||
|
|
1810
|
+
this.#options.onProgress !== undefined ||
|
|
1811
|
+
this.#options.onSessionEvent !== undefined ||
|
|
1812
|
+
this.#options.onWorkerEvent !== undefined);
|
|
1813
|
+
const unreadable = new Map();
|
|
1814
|
+
let blockingReadError;
|
|
1815
|
+
directories: for (const directory of ["sessions", "archived_sessions"]) {
|
|
172
1816
|
try {
|
|
173
|
-
await
|
|
1817
|
+
for await (const path of sessionFiles(join(this.#options.codexHome, directory), true)) {
|
|
1818
|
+
let session = this.#sessions.get(path);
|
|
1819
|
+
if (session === undefined) {
|
|
1820
|
+
session = createSessionUsage(observeUsage);
|
|
1821
|
+
this.#sessions.set(path, session);
|
|
1822
|
+
}
|
|
1823
|
+
try {
|
|
1824
|
+
await readSessionUsage(path, session, repository, progressThread, undefined, true);
|
|
1825
|
+
}
|
|
1826
|
+
catch (error) {
|
|
1827
|
+
if (session.threadId !== null)
|
|
1828
|
+
unreadable.set(session, error);
|
|
1829
|
+
else if (isRecord(error) && typeof error["syscall"] === "string") {
|
|
1830
|
+
blockingReadError ??= error;
|
|
1831
|
+
if (!observeHealthySessions || !isBlockedLogRead(error))
|
|
1832
|
+
break directories;
|
|
1833
|
+
}
|
|
1834
|
+
else if (directory !== "archived_sessions" ||
|
|
1835
|
+
!path.endsWith(".zst")) {
|
|
1836
|
+
collectionErrors.push(error);
|
|
1837
|
+
}
|
|
1838
|
+
// A damaged cold archive with no readable identity cannot be
|
|
1839
|
+
// attributed to this scan. Registered threads still require usage.
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
174
1842
|
}
|
|
175
1843
|
catch (error) {
|
|
176
|
-
if
|
|
177
|
-
|
|
178
|
-
|
|
1844
|
+
// Retain the blocking read if traversal also stops on a later entry.
|
|
1845
|
+
if (blockingReadError === undefined)
|
|
1846
|
+
collectionErrors.push(error);
|
|
179
1847
|
}
|
|
180
1848
|
}
|
|
181
|
-
const
|
|
1849
|
+
const workerReceipts = await readWorkerReceipts(this.#options.workerReceipts, this.#workerReceipts);
|
|
1850
|
+
collectionErrors.push(...workerReceipts.errors);
|
|
1851
|
+
const included = new Set([
|
|
1852
|
+
this.#threadId,
|
|
1853
|
+
...this.#receipts.keys(),
|
|
1854
|
+
...workerReceipts.usages.keys(),
|
|
1855
|
+
]);
|
|
182
1856
|
if (this.#options.scanDirectory !== undefined) {
|
|
183
1857
|
const scanStartedAt = [...this.#sessions.values()].find((session) => session.threadId === this.#threadId)?.startedAt ?? null;
|
|
184
1858
|
for (const session of this.#sessions.values()) {
|
|
@@ -206,24 +1880,93 @@ export class ScanCostTracker {
|
|
|
206
1880
|
}
|
|
207
1881
|
}
|
|
208
1882
|
} while (included.size !== previousSize);
|
|
209
|
-
for (const
|
|
210
|
-
if (included.has(session.threadId))
|
|
211
|
-
|
|
1883
|
+
for (const [session, error] of unreadable) {
|
|
1884
|
+
if (session.threadId !== null && included.has(session.threadId)) {
|
|
1885
|
+
if (isBlockedLogRead(error))
|
|
1886
|
+
blockingReadError ??= error;
|
|
1887
|
+
else
|
|
1888
|
+
collectionErrors.push(error);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
if (blockingReadError !== undefined)
|
|
1892
|
+
collectionErrors.unshift(blockingReadError);
|
|
1893
|
+
const usages = new Map([...included].map((threadId) => [
|
|
1894
|
+
threadId,
|
|
1895
|
+
this.#receipts.get(threadId) ?? null,
|
|
1896
|
+
]));
|
|
1897
|
+
for (const [threadId, receipt] of workerReceipts.usages) {
|
|
1898
|
+
const current = usages.get(threadId);
|
|
1899
|
+
if (receipt !== null &&
|
|
1900
|
+
receipt.total_tokens > (current?.total_tokens ?? -1))
|
|
1901
|
+
usages.set(threadId, receipt);
|
|
212
1902
|
}
|
|
213
|
-
|
|
1903
|
+
// SDK totals retain measured usage when a rollout is unavailable.
|
|
1904
|
+
// Native rollouts also establish descendant coverage.
|
|
1905
|
+
const incomplete = new Set(workerReceipts.usages.keys());
|
|
214
1906
|
for (const [path, tracked] of this.#sessions) {
|
|
215
1907
|
const threadId = tracked.threadId;
|
|
216
1908
|
if (threadId === null || !included.has(threadId))
|
|
217
1909
|
continue;
|
|
218
1910
|
let session = tracked;
|
|
219
|
-
|
|
1911
|
+
const observe = !unreadable.has(tracked) &&
|
|
1912
|
+
(blockingReadError === undefined || observeHealthySessions);
|
|
1913
|
+
if (observe && path.endsWith(".zst") && !tracked.compressedRead) {
|
|
1914
|
+
// The metadata pass associates this archive with the scan. A full read
|
|
1915
|
+
// starts at the beginning, including when retrying a partial decode.
|
|
1916
|
+
const archive = createSessionUsage(observeUsage);
|
|
1917
|
+
if (this.#options.onSessionEvent !== undefined)
|
|
1918
|
+
archive.events = [];
|
|
1919
|
+
try {
|
|
1920
|
+
await readSessionUsage(path, archive, repository, progressThread);
|
|
1921
|
+
}
|
|
1922
|
+
catch (error) {
|
|
1923
|
+
collectionErrors.push(error);
|
|
1924
|
+
}
|
|
1925
|
+
if (archive.threadId === threadId &&
|
|
1926
|
+
archive.eventPosition >= tracked.eventPosition) {
|
|
1927
|
+
session = archive;
|
|
1928
|
+
this.#sessions.set(path, archive);
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
else if (observe &&
|
|
1932
|
+
this.#options.onSessionEvent !== undefined &&
|
|
220
1933
|
session.events === undefined) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
1934
|
+
const replay = createSessionUsage(false);
|
|
1935
|
+
replay.events = [];
|
|
1936
|
+
try {
|
|
1937
|
+
// Replay raw events only through the tracked snapshot. Keep its progress,
|
|
1938
|
+
// usage and decoder so a failed read cannot replace authoritative state.
|
|
1939
|
+
await readSessionUsage(path, replay, undefined, undefined, session.offset);
|
|
1940
|
+
if (replay.offset === session.offset) {
|
|
1941
|
+
session.events = replay.events;
|
|
1942
|
+
if (this.#timer === null)
|
|
1943
|
+
await readSessionUsage(path, session, repository, progressThread, "file-size");
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
catch (error) {
|
|
1947
|
+
collectionErrors.push(error);
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
const receipt = usages.get(threadId);
|
|
1951
|
+
if (session.usage !== null &&
|
|
1952
|
+
session.usage.total_tokens >=
|
|
1953
|
+
(workerReceipts.usages.get(threadId)?.total_tokens ?? 0))
|
|
1954
|
+
incomplete.delete(threadId);
|
|
1955
|
+
// A discovered thread without a receipt has unknown usage, not zero.
|
|
1956
|
+
// Retain it so a parent-only subtotal cannot appear to be complete.
|
|
1957
|
+
if (receipt === undefined)
|
|
1958
|
+
usages.set(threadId, null);
|
|
1959
|
+
if (session.usage !== null &&
|
|
1960
|
+
(session.usage.total_tokens > (receipt?.total_tokens ?? -1) ||
|
|
1961
|
+
// Worker receipts retain field presence; the SDK may default to zero.
|
|
1962
|
+
(session.usage.total_tokens === receipt?.total_tokens &&
|
|
1963
|
+
(receipt.cache_write_input_tokens_reported === false ||
|
|
1964
|
+
(receipt === this.#receipts.get(threadId) &&
|
|
1965
|
+
receipt.cache_write_input_tokens === 0))))) {
|
|
1966
|
+
usages.set(threadId, session.usage);
|
|
226
1967
|
}
|
|
1968
|
+
if (!observe)
|
|
1969
|
+
continue;
|
|
227
1970
|
let worker;
|
|
228
1971
|
if (threadId !== this.#threadId) {
|
|
229
1972
|
worker = this.#workers.get(threadId);
|
|
@@ -234,7 +1977,19 @@ export class ScanCostTracker {
|
|
|
234
1977
|
this.#options.onWorkerEvent?.({ kind: "observed", worker });
|
|
235
1978
|
}
|
|
236
1979
|
}
|
|
237
|
-
|
|
1980
|
+
// Archives and compressed copies retain the same append-only event order.
|
|
1981
|
+
// A new rollout for the same thread has a new initial metadata timestamp.
|
|
1982
|
+
const eventStream = JSON.stringify([threadId, session.startedAt]);
|
|
1983
|
+
const reported = this.#reportedSessionPositions.get(eventStream) ?? {
|
|
1984
|
+
events: 0,
|
|
1985
|
+
activities: 0,
|
|
1986
|
+
progress: 0,
|
|
1987
|
+
source: session,
|
|
1988
|
+
};
|
|
1989
|
+
for (const { position, value: event } of session.events?.splice(0) ??
|
|
1990
|
+
[]) {
|
|
1991
|
+
if (position <= reported.events)
|
|
1992
|
+
continue;
|
|
238
1993
|
this.#options.onSessionEvent?.({
|
|
239
1994
|
threadId,
|
|
240
1995
|
parentThreadId: session.parentThreadId,
|
|
@@ -242,50 +1997,136 @@ export class ScanCostTracker {
|
|
|
242
1997
|
event,
|
|
243
1998
|
});
|
|
244
1999
|
}
|
|
2000
|
+
// A failed replay can leave activity/progress available without raw events.
|
|
2001
|
+
if (session.events !== undefined)
|
|
2002
|
+
reported.events = Math.max(reported.events, session.eventPosition);
|
|
245
2003
|
if (worker !== undefined) {
|
|
246
|
-
for (const activity of session.activities.splice(0)) {
|
|
2004
|
+
for (const { position, value: activity } of session.activities.splice(0)) {
|
|
2005
|
+
if (position <= reported.activities)
|
|
2006
|
+
continue;
|
|
247
2007
|
this.#options.onActivity?.({
|
|
248
2008
|
...activity,
|
|
249
2009
|
id: `${threadId}:${activity.id}`,
|
|
250
2010
|
worker,
|
|
251
2011
|
});
|
|
252
2012
|
}
|
|
253
|
-
|
|
2013
|
+
reported.activities = Math.max(reported.activities, session.eventPosition);
|
|
254
2014
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
2015
|
+
if (session === reported.source || session.position > reported.progress) {
|
|
2016
|
+
if (session !== reported.source) {
|
|
2017
|
+
// An archive can continue an existing rollout. Keep admitted marker
|
|
2018
|
+
// state and process only the suffix unique to this copy.
|
|
2019
|
+
const prior = reported.source;
|
|
2020
|
+
session.progress = prior.progress
|
|
2021
|
+
.filter((marker) => marker.position <= reported.progress)
|
|
2022
|
+
.concat(session.progress.filter((marker) => marker.position > reported.progress));
|
|
2023
|
+
session.completions = prior.completions
|
|
2024
|
+
.filter((completion) => completion.position <= reported.progress)
|
|
2025
|
+
.concat(session.completions.filter((completion) => completion.position > reported.progress));
|
|
2026
|
+
session.latestWorkerProgress = prior.latestWorkerProgress;
|
|
2027
|
+
session.pendingWorkerProgress = prior.pendingWorkerProgress;
|
|
2028
|
+
session.filesCompleted = prior.filesCompleted;
|
|
2029
|
+
session.filesTotal = prior.filesTotal;
|
|
2030
|
+
reported.source = session;
|
|
2031
|
+
}
|
|
2032
|
+
reported.progress = session.position;
|
|
2033
|
+
let unresolvedCommand;
|
|
2034
|
+
let inventoryWitness;
|
|
2035
|
+
if (threadId === this.#threadId) {
|
|
2036
|
+
this.#parentSession = session;
|
|
2037
|
+
for (const saved of session.progress)
|
|
2038
|
+
if (saved.order !== undefined && !saved.duplicate)
|
|
2039
|
+
this.#rememberParentFact(savedProgress(saved));
|
|
2040
|
+
this.#matchParentCompletions();
|
|
2041
|
+
inventoryWitness = this.#reconcileParentProgress();
|
|
2042
|
+
unresolvedCommand = this.#unresolvedParentCommand();
|
|
2043
|
+
}
|
|
2044
|
+
const reviewWasUnpositioned = this.#parentInventoryBoundary?.reviewed !== true;
|
|
2045
|
+
this.#reportSessionProgress(session, threadId === this.#threadId, unresolvedCommand, inventoryWitness);
|
|
2046
|
+
// Fresh saved facts are observed at their source-loop position, so initial
|
|
2047
|
+
// inventory notifications keep their order. Recover after the batch too:
|
|
2048
|
+
// a later accepted parent may have hidden an eligible earlier receipt.
|
|
2049
|
+
if (threadId === this.#threadId) {
|
|
2050
|
+
// A newly admitted review locates inventories parked earlier in this
|
|
2051
|
+
// batch. Reconsider them before the final refresh returns.
|
|
2052
|
+
if (reviewWasUnpositioned &&
|
|
2053
|
+
this.#parentInventoryBoundary?.reviewed === true)
|
|
2054
|
+
this.#reportSessionProgress(session, true, unresolvedCommand, inventoryWitness);
|
|
2055
|
+
this.#reconcileParentProgress();
|
|
2056
|
+
}
|
|
266
2057
|
}
|
|
2058
|
+
this.#reportedSessionPositions.set(eventStream, reported);
|
|
267
2059
|
}
|
|
268
2060
|
let usage = null;
|
|
269
|
-
let
|
|
270
|
-
|
|
2061
|
+
let missingThreadCount = workerReceipts.unknownAttempts;
|
|
2062
|
+
let threadCount = 0;
|
|
2063
|
+
for (const [threadId, value] of usages) {
|
|
271
2064
|
if (value === null) {
|
|
272
|
-
|
|
2065
|
+
missingThreadCount += 1;
|
|
273
2066
|
continue;
|
|
274
2067
|
}
|
|
2068
|
+
threadCount += 1;
|
|
2069
|
+
if (incomplete.has(threadId))
|
|
2070
|
+
missingThreadCount += 1;
|
|
275
2071
|
usage = addTokenUsage(usage, value);
|
|
276
2072
|
}
|
|
2073
|
+
if (collectionErrors.length > 0)
|
|
2074
|
+
missingThreadCount = Math.max(1, missingThreadCount);
|
|
2075
|
+
const complete = missingThreadCount === 0 && usage !== null;
|
|
277
2076
|
if (observeUsage)
|
|
278
2077
|
this.#reportUsageObservation(usages, complete);
|
|
279
|
-
if (
|
|
280
|
-
this.#
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
2078
|
+
if (this.#options.readBudgetUsage === undefined)
|
|
2079
|
+
this.#updateCost(usage, complete, threadCount, missingThreadCount, {
|
|
2080
|
+
coverageScope: collectionErrors.length === 0
|
|
2081
|
+
? [
|
|
2082
|
+
...[...included].map((id) => `thread:${id}`),
|
|
2083
|
+
...[...workerReceipts.attemptIds].map((id) => `worker-attempt:${id}`),
|
|
2084
|
+
].sort()
|
|
2085
|
+
: undefined,
|
|
2086
|
+
},
|
|
2087
|
+
// A blocking read leaves a native-only subtotal unconfirmed until the retry.
|
|
2088
|
+
blockingReadError === undefined ||
|
|
2089
|
+
this.#receipts.size > 0 ||
|
|
2090
|
+
workerReceipts.usages.size > 0);
|
|
2091
|
+
// Preserve measured usage and every diagnostic when several logs fail together.
|
|
2092
|
+
if (collectionErrors.length === 1)
|
|
2093
|
+
throw collectionErrors[0];
|
|
2094
|
+
if (collectionErrors.length > 1)
|
|
2095
|
+
throw new AggregateError(collectionErrors, collectionErrors.map(errorMessage).join("\n"));
|
|
2096
|
+
}
|
|
2097
|
+
#updateCost(usage, complete, threadCount, missingThreadCount, details, reportCost = true) {
|
|
2098
|
+
const cost = estimateScanCost(this.#options.model, usage, this.#options.pricingContext);
|
|
2099
|
+
this.#setSnapshot(usage, cost, complete, threadCount, missingThreadCount, details);
|
|
2100
|
+
if (complete)
|
|
286
2101
|
this.#reportUsage(usage);
|
|
287
|
-
|
|
288
|
-
|
|
2102
|
+
if (reportCost)
|
|
2103
|
+
this.#reportCost(cost, complete);
|
|
2104
|
+
}
|
|
2105
|
+
#setSnapshot(usage, cost, complete, threadCount, missingThreadCount, details) {
|
|
2106
|
+
const data = {
|
|
2107
|
+
usage,
|
|
2108
|
+
cost,
|
|
2109
|
+
coverage: usage === null ? "unavailable" : complete ? "complete" : "partial",
|
|
2110
|
+
threadCount,
|
|
2111
|
+
missingThreadCount,
|
|
2112
|
+
...(details?.coverageScope === undefined
|
|
2113
|
+
? {}
|
|
2114
|
+
: { coverageScope: details.coverageScope }),
|
|
2115
|
+
...(details?.warnings === undefined
|
|
2116
|
+
? {}
|
|
2117
|
+
: { warnings: details.warnings }),
|
|
2118
|
+
};
|
|
2119
|
+
const measurement = { ...data, observedAt: new Date().toISOString() };
|
|
2120
|
+
this.#snapshot = {
|
|
2121
|
+
usage: complete ? usage : null,
|
|
2122
|
+
cost: complete ? cost : null,
|
|
2123
|
+
measurement,
|
|
2124
|
+
};
|
|
2125
|
+
const signature = JSON.stringify(data);
|
|
2126
|
+
if (signature === this.#lastMeasurement)
|
|
2127
|
+
return;
|
|
2128
|
+
this.#lastMeasurement = signature;
|
|
2129
|
+
this.#options.onMeasurement?.(measurement);
|
|
289
2130
|
}
|
|
290
2131
|
#reportUsageObservation(usages, complete) {
|
|
291
2132
|
const limits = new Map();
|
|
@@ -304,8 +2145,8 @@ export class ScanCostTracker {
|
|
|
304
2145
|
tokensUpdatedAt = session.tokensUpdatedAt;
|
|
305
2146
|
}
|
|
306
2147
|
}
|
|
307
|
-
if (!complete) {
|
|
308
|
-
//
|
|
2148
|
+
if (!complete && this.#lastUsage !== null) {
|
|
2149
|
+
// Keep the last complete total's time while a worker is unmeasured.
|
|
309
2150
|
tokensUpdatedAt = this.#lastObservation?.tokensUpdatedAt ?? null;
|
|
310
2151
|
}
|
|
311
2152
|
else if (usages
|
|
@@ -327,38 +2168,230 @@ export class ScanCostTracker {
|
|
|
327
2168
|
this.#lastObservation = observation;
|
|
328
2169
|
this.#options.onUsageObservation?.(observation);
|
|
329
2170
|
}
|
|
330
|
-
#
|
|
2171
|
+
#parentInventoryAdmission(saved, command, corroboration, inventoryWitness) {
|
|
2172
|
+
if (!isInventoryProgress(saved.update))
|
|
2173
|
+
return undefined;
|
|
2174
|
+
const newer = saved.position > this.#inventoryFrontier.position ||
|
|
2175
|
+
(saved.position === this.#inventoryFrontier.position &&
|
|
2176
|
+
saved.markerIndex > this.#inventoryFrontier.markerIndex);
|
|
2177
|
+
if (saved.duplicate || !newer)
|
|
2178
|
+
return "retained";
|
|
2179
|
+
if (!this.#inventoryPrecedesReview(saved.position, saved.markerIndex, saved.projection, saved))
|
|
2180
|
+
return "retained";
|
|
2181
|
+
const authority = this.#acceptedInventory();
|
|
2182
|
+
const source = savedProgress(saved);
|
|
2183
|
+
// A matched review can order an inventory after a compatible truncated
|
|
2184
|
+
// prefix without identifying the live command's occurrence. That proof
|
|
2185
|
+
// applies to its own inventory, not a newer independent live authority.
|
|
2186
|
+
const relation = authority === undefined
|
|
2187
|
+
? 0
|
|
2188
|
+
: (this.#inventorySourceOrder(source, authority) ??
|
|
2189
|
+
(corroboration?.live === authority.live
|
|
2190
|
+
? compareSourceOrder(source, corroboration.after)
|
|
2191
|
+
: undefined) ??
|
|
2192
|
+
(authority.live.after === undefined ? 0 : -1));
|
|
2193
|
+
if (this.#precedesParentBoundary(saved.position, saved.markerIndex) &&
|
|
2194
|
+
(this.#parentInventoryBoundary?.reviewed === true ||
|
|
2195
|
+
!this.#parentIsEligible(this.#parentProgress) ||
|
|
2196
|
+
this.#parentProgress.progress.filesCompleted === 0 ||
|
|
2197
|
+
(inventoryWitness !== undefined &&
|
|
2198
|
+
(compareSourceOrder(source, inventoryWitness) ?? 0) < 0)) &&
|
|
2199
|
+
relation >= 0)
|
|
2200
|
+
return "proven";
|
|
2201
|
+
if ((this.#parentIsEligible(this.#parentProgress) &&
|
|
2202
|
+
this.#parentProgress.progress.filesCompleted > 0) ||
|
|
2203
|
+
!this.#savedParentProgressIsCurrent(saved) ||
|
|
2204
|
+
(command !== undefined &&
|
|
2205
|
+
command.live === this.#acceptedParent &&
|
|
2206
|
+
command.live.position === null &&
|
|
2207
|
+
this.#parentProgress !== null &&
|
|
2208
|
+
saved.position < command.after &&
|
|
2209
|
+
this.#afterParentFrontier(saved.position, saved.markerIndex)))
|
|
2210
|
+
return "retained";
|
|
2211
|
+
return "current";
|
|
2212
|
+
}
|
|
2213
|
+
#reportSessionProgress(session, parent, command, inventoryWitness) {
|
|
331
2214
|
if (this.#options.onProgress === undefined || session.threadId === null) {
|
|
332
2215
|
return;
|
|
333
2216
|
}
|
|
334
|
-
|
|
2217
|
+
const live = this.#acceptedParent;
|
|
2218
|
+
const current = this.#parentProgress;
|
|
2219
|
+
const unresolvedCommandState = parent &&
|
|
2220
|
+
command?.live === live &&
|
|
2221
|
+
live?.position === null &&
|
|
2222
|
+
current !== null;
|
|
2223
|
+
// Parent rows are source evidence, not a notification queue. Admission or
|
|
2224
|
+
// temporary scope exclusion cannot discard an unresolved occurrence.
|
|
2225
|
+
// Worker receipts are independent and retain their compact summaries below.
|
|
2226
|
+
for (const saved of parent
|
|
2227
|
+
? session.progress
|
|
2228
|
+
: session.progress.splice(0)) {
|
|
2229
|
+
const progress = saved.update;
|
|
2230
|
+
const inventory = parent
|
|
2231
|
+
? this.#parentInventoryAdmission(saved, command, undefined, inventoryWitness)
|
|
2232
|
+
: undefined;
|
|
2233
|
+
if (inventory === "retained")
|
|
2234
|
+
continue;
|
|
2235
|
+
const inventoryMarker = inventory !== undefined;
|
|
2236
|
+
const provenInventory = inventory === "proven";
|
|
2237
|
+
// Source order governs equal-count phases. Fresh and parked receipts can
|
|
2238
|
+
// both add coverage after a scope correction makes that coverage eligible.
|
|
2239
|
+
const greaterCoverage = progress.filesCompleted > this.#highestFilesCompleted;
|
|
2240
|
+
if (parent &&
|
|
2241
|
+
!saved.duplicate &&
|
|
2242
|
+
!inventoryMarker &&
|
|
2243
|
+
!this.#unboundParentCopy(saved)) {
|
|
2244
|
+
saved.order ??= ++this.#progressOrder;
|
|
2245
|
+
this.#rememberParentFact(savedProgress(saved));
|
|
2246
|
+
}
|
|
2247
|
+
if (unresolvedCommandState &&
|
|
2248
|
+
saved.position < (command?.after ?? Infinity) &&
|
|
2249
|
+
this.#afterParentFrontier(saved.position, saved.markerIndex) &&
|
|
2250
|
+
!provenInventory &&
|
|
2251
|
+
progress.filesCompleted <= this.#highestFilesCompleted) {
|
|
2252
|
+
// Keep lossy-output ambiguity across marker-free anchors; a current
|
|
2253
|
+
// state or greater coverage must supply the missing progress evidence.
|
|
2254
|
+
continue;
|
|
2255
|
+
}
|
|
2256
|
+
if (parent &&
|
|
2257
|
+
!provenInventory &&
|
|
2258
|
+
!greaterCoverage &&
|
|
2259
|
+
!this.#savedParentProgressIsCurrent(saved))
|
|
2260
|
+
continue;
|
|
2261
|
+
if (saved.duplicate) {
|
|
2262
|
+
if (parent && saved.completion)
|
|
2263
|
+
this.#advanceParentFrontier(saved.position, saved.markerIndex);
|
|
2264
|
+
continue;
|
|
2265
|
+
}
|
|
335
2266
|
const expectedFilesTotal = this.#expectedFilesTotal;
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
progress.
|
|
2267
|
+
const parentInventory = inventoryMarker;
|
|
2268
|
+
const order = (saved.order ??= ++this.#progressOrder);
|
|
2269
|
+
if ((!parent &&
|
|
2270
|
+
session.filesTotal !== null &&
|
|
2271
|
+
progress.filesTotal !== session.filesTotal) ||
|
|
2272
|
+
(!parentInventory && progress.filesCompleted < session.filesCompleted))
|
|
2273
|
+
continue;
|
|
2274
|
+
if (!parentInventory &&
|
|
2275
|
+
((expectedFilesTotal !== undefined &&
|
|
2276
|
+
progress.filesTotal > expectedFilesTotal) ||
|
|
2277
|
+
(parent &&
|
|
2278
|
+
progress.filesTotal >
|
|
2279
|
+
(saved.scopeFilesTotal ?? Number.MAX_SAFE_INTEGER)))) {
|
|
2280
|
+
if (!parent) {
|
|
2281
|
+
const pending = session.pendingWorkerProgress.get(progress.filesTotal);
|
|
2282
|
+
if (pending === undefined ||
|
|
2283
|
+
progress.filesCompleted >= pending.progress.filesCompleted)
|
|
2284
|
+
session.pendingWorkerProgress.set(progress.filesTotal, {
|
|
2285
|
+
progress,
|
|
2286
|
+
order,
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
341
2289
|
continue;
|
|
342
2290
|
}
|
|
343
2291
|
session.filesTotal = progress.filesTotal;
|
|
344
2292
|
session.filesCompleted = progress.filesCompleted;
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
2293
|
+
if (!parent)
|
|
2294
|
+
session.latestWorkerProgress = { progress, order };
|
|
2295
|
+
if (parentInventory && this.#highestFilesCompleted > 0) {
|
|
2296
|
+
// An inventory behind admitted review changes scope without replaying
|
|
2297
|
+
// its earlier phase. At zero coverage it follows normal source-ordered
|
|
2298
|
+
// phase admission below, including when the inventory total changes.
|
|
2299
|
+
if (!this.#parentIsEligible(this.#parentProgress) ||
|
|
2300
|
+
this.#parentProgress.progress.filesCompleted === 0) {
|
|
2301
|
+
const candidate = {
|
|
2302
|
+
progress,
|
|
2303
|
+
order,
|
|
2304
|
+
position: saved.position,
|
|
2305
|
+
markerIndex: saved.markerIndex,
|
|
2306
|
+
};
|
|
2307
|
+
if (this.#parentProgress === null ||
|
|
2308
|
+
this.#parentProgress.progress.phase !== "discovery" ||
|
|
2309
|
+
compareProgressOrder(candidate, this.#parentProgress) >= 0)
|
|
2310
|
+
this.#parentProgress = candidate;
|
|
2311
|
+
}
|
|
2312
|
+
this.#advanceParentFrontier(saved.position, saved.markerIndex);
|
|
2313
|
+
this.#inventoryFrontier = savedProgress(saved);
|
|
2314
|
+
this.#projectParentInventory();
|
|
2315
|
+
continue;
|
|
2316
|
+
}
|
|
2317
|
+
// Parent markers already describe global coverage, including its workers.
|
|
2318
|
+
// Only worker-local counts belong in the delegated shard sum.
|
|
2319
|
+
if (!parent)
|
|
2320
|
+
this.#workerProgress.set(session.threadId, progress.filesCompleted);
|
|
2321
|
+
const filesCompleted = parent
|
|
2322
|
+
? progress.filesCompleted
|
|
2323
|
+
: Math.min(expectedFilesTotal ?? Number.MAX_SAFE_INTEGER, this.#workerProgress
|
|
2324
|
+
.values()
|
|
2325
|
+
.reduce((total, reviewed) => total + reviewed, 0));
|
|
2326
|
+
if (filesCompleted < this.#highestFilesCompleted) {
|
|
2327
|
+
// A provisional worker can exceed this global parent receipt, then
|
|
2328
|
+
// become ineligible when the parent's earlier inventory is recovered.
|
|
2329
|
+
// Keep the parsed parent fact for that correction, without displaying
|
|
2330
|
+
// a count decrease while the worker coverage remains eligible.
|
|
2331
|
+
continue;
|
|
2332
|
+
}
|
|
2333
|
+
// A worker-local phase cannot replace eligible global parent authority
|
|
2334
|
+
// when the aggregate count adds no reviewed files. Counts remain above.
|
|
2335
|
+
if (!parent &&
|
|
2336
|
+
this.#parentIsEligible(this.#parentProgress) &&
|
|
2337
|
+
filesCompleted === this.#parentProgress.progress.filesCompleted)
|
|
350
2338
|
continue;
|
|
351
|
-
|
|
2339
|
+
if (parent)
|
|
2340
|
+
this.#recordParentBoundary(progress, saved.position, saved.markerIndex, false, saved.scopeFilesTotal);
|
|
2341
|
+
// Known parent positions outrank observation order: polling can park a
|
|
2342
|
+
// newer completion before an earlier live completion is displayed.
|
|
2343
|
+
const displayed = this.#displayedProgress;
|
|
2344
|
+
const provenance = {
|
|
2345
|
+
order,
|
|
2346
|
+
position: saved.position,
|
|
2347
|
+
markerIndex: saved.markerIndex,
|
|
2348
|
+
source: saved,
|
|
2349
|
+
};
|
|
2350
|
+
const newerProgress = parent &&
|
|
2351
|
+
displayed !== null &&
|
|
2352
|
+
displayed.owner !== "worker" &&
|
|
2353
|
+
filesCompleted === displayed.update.filesCompleted &&
|
|
2354
|
+
compareProgressOrder(provenance, displayed) < 0
|
|
2355
|
+
? displayed
|
|
2356
|
+
: null;
|
|
2357
|
+
const update = newerProgress?.update ?? {
|
|
352
2358
|
...progress,
|
|
353
2359
|
filesCompleted,
|
|
354
|
-
filesTotal:
|
|
2360
|
+
filesTotal: parent
|
|
2361
|
+
? progress.filesTotal
|
|
2362
|
+
: (expectedFilesTotal ??
|
|
2363
|
+
Math.max(progress.filesTotal, filesCompleted)),
|
|
355
2364
|
};
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
2365
|
+
// Retaining a newer parent phase retains that whole receipt, including
|
|
2366
|
+
// its actual inventory. Mixing an older total with its phase would discard
|
|
2367
|
+
// valid parent coverage when a later inventory correction arrives.
|
|
2368
|
+
if (parent && newerProgress === null)
|
|
2369
|
+
this.#parentProgress = { progress, ...provenance };
|
|
2370
|
+
const key = progressKey(update);
|
|
2371
|
+
if (parent) {
|
|
2372
|
+
// An original multipart response can expose progress absent from its
|
|
2373
|
+
// concatenated native projection. Its admitted source position still
|
|
2374
|
+
// prevents an older live completion from replaying over that progress.
|
|
2375
|
+
this.#advanceParentFrontier(saved.position, saved.markerIndex);
|
|
2376
|
+
if (parentInventory) {
|
|
2377
|
+
if (newerProgress === null)
|
|
2378
|
+
saved.inventoryAdmitted = true;
|
|
2379
|
+
this.#inventoryFrontier = savedProgress(saved);
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
360
2382
|
this.#highestFilesCompleted = update.filesCompleted;
|
|
361
|
-
this.#
|
|
2383
|
+
this.#displayedProgress =
|
|
2384
|
+
newerProgress !== null
|
|
2385
|
+
? { ...newerProgress, update }
|
|
2386
|
+
: {
|
|
2387
|
+
update,
|
|
2388
|
+
owner: parent ? "parent" : "worker",
|
|
2389
|
+
...(parent ? provenance : { order }),
|
|
2390
|
+
};
|
|
2391
|
+
// Distinct equal-valued receipts still advance source ownership/order.
|
|
2392
|
+
if (displayed !== null && progressKey(displayed.update) === key)
|
|
2393
|
+
continue;
|
|
2394
|
+
this.#options.onProgress(update, parent ? { reconciled: true } : undefined);
|
|
362
2395
|
}
|
|
363
2396
|
}
|
|
364
2397
|
#reportCost(cost, complete = true) {
|
|
@@ -380,6 +2413,76 @@ export class ScanCostTracker {
|
|
|
380
2413
|
this.#options.onUsage?.(usage);
|
|
381
2414
|
}
|
|
382
2415
|
}
|
|
2416
|
+
async function readWorkerReceipts(store, retained) {
|
|
2417
|
+
const usages = new Map();
|
|
2418
|
+
const attempts = new Set();
|
|
2419
|
+
const result = {
|
|
2420
|
+
usages,
|
|
2421
|
+
unknownAttempts: 0,
|
|
2422
|
+
attemptIds: attempts,
|
|
2423
|
+
errors: [],
|
|
2424
|
+
};
|
|
2425
|
+
if (store === undefined)
|
|
2426
|
+
return result;
|
|
2427
|
+
const { directory, scanId } = store;
|
|
2428
|
+
try {
|
|
2429
|
+
const directoryInfo = await lstat(directory);
|
|
2430
|
+
if (!directoryInfo.isDirectory() ||
|
|
2431
|
+
relative(resolve(directory), await realpath(directory)) !== "")
|
|
2432
|
+
throw new Error(`Worker usage directory must use a canonical directory: ${directory}`);
|
|
2433
|
+
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
2434
|
+
if (!entry.isFile() || !entry.name.endsWith(".json"))
|
|
2435
|
+
continue;
|
|
2436
|
+
let receipt;
|
|
2437
|
+
try {
|
|
2438
|
+
receipt = JSON.parse(await readFile(join(directory, entry.name), "utf8"));
|
|
2439
|
+
}
|
|
2440
|
+
catch (error) {
|
|
2441
|
+
if (isMissingFile(error))
|
|
2442
|
+
continue;
|
|
2443
|
+
throw error;
|
|
2444
|
+
}
|
|
2445
|
+
if (!isRecord(receipt) ||
|
|
2446
|
+
receipt["schemaVersion"] !== 1 ||
|
|
2447
|
+
receipt["source"] !== "codex_sdk" ||
|
|
2448
|
+
receipt["usageScope"] !== "thread_cumulative" ||
|
|
2449
|
+
receipt["scanId"] !== scanId ||
|
|
2450
|
+
typeof receipt["attemptId"] !== "string" ||
|
|
2451
|
+
entry.name !== `${receipt["attemptId"]}.json` ||
|
|
2452
|
+
attempts.has(receipt["attemptId"]))
|
|
2453
|
+
continue;
|
|
2454
|
+
attempts.add(receipt["attemptId"]);
|
|
2455
|
+
retained.set(receipt["attemptId"], {
|
|
2456
|
+
threadId: typeof receipt["threadId"] === "string" ? receipt["threadId"] : null,
|
|
2457
|
+
usage: tokenUsage(receipt["usage"]),
|
|
2458
|
+
});
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
catch (error) {
|
|
2462
|
+
if (!isMissingFile(error))
|
|
2463
|
+
result.errors.push(error);
|
|
2464
|
+
}
|
|
2465
|
+
// As with native sessions, a later read failure cannot erase observed usage.
|
|
2466
|
+
// Keep attempts separate so a worker's initial unknown identity can resolve.
|
|
2467
|
+
for (const [attemptId, { threadId, usage }] of retained) {
|
|
2468
|
+
attempts.add(attemptId);
|
|
2469
|
+
if (threadId === null) {
|
|
2470
|
+
result.unknownAttempts += 1;
|
|
2471
|
+
continue;
|
|
2472
|
+
}
|
|
2473
|
+
const existing = usages.get(threadId) ?? null;
|
|
2474
|
+
// Native SDK completions include earlier turns of a resumed thread.
|
|
2475
|
+
usages.set(threadId, usage !== null &&
|
|
2476
|
+
(existing === null ||
|
|
2477
|
+
usage.total_tokens > existing.total_tokens ||
|
|
2478
|
+
(usage.total_tokens === existing.total_tokens &&
|
|
2479
|
+
existing.cache_write_input_tokens_reported === false &&
|
|
2480
|
+
usage.cache_write_input_tokens_reported !== false))
|
|
2481
|
+
? usage
|
|
2482
|
+
: existing);
|
|
2483
|
+
}
|
|
2484
|
+
return result;
|
|
2485
|
+
}
|
|
383
2486
|
export async function* sessionFiles(directory, compressed = false) {
|
|
384
2487
|
let entries;
|
|
385
2488
|
try {
|
|
@@ -402,13 +2505,36 @@ export async function* sessionFiles(directory, compressed = false) {
|
|
|
402
2505
|
}
|
|
403
2506
|
}
|
|
404
2507
|
}
|
|
405
|
-
async function readSessionUsage(path, session, repository) {
|
|
2508
|
+
async function readSessionUsage(path, session, repository, progressThread, throughOffset, metadataOnly = false) {
|
|
406
2509
|
if (session.unreadable)
|
|
407
2510
|
return;
|
|
2511
|
+
if (path.endsWith(".zst")) {
|
|
2512
|
+
if (session.compressedRead)
|
|
2513
|
+
return;
|
|
2514
|
+
if (metadataOnly && session.threadId !== null)
|
|
2515
|
+
return;
|
|
2516
|
+
try {
|
|
2517
|
+
for await (const line of sessionLines(path)) {
|
|
2518
|
+
readSessionEvent(line, session, repository, progressThread);
|
|
2519
|
+
if (metadataOnly && session.threadId !== null)
|
|
2520
|
+
return;
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
catch (error) {
|
|
2524
|
+
if (isMissingFile(error))
|
|
2525
|
+
return;
|
|
2526
|
+
throw error;
|
|
2527
|
+
}
|
|
2528
|
+
session.compressedRead = true;
|
|
2529
|
+
return;
|
|
2530
|
+
}
|
|
408
2531
|
let file;
|
|
409
2532
|
try {
|
|
410
|
-
|
|
2533
|
+
const metadata = await stat(path);
|
|
2534
|
+
if (metadata.isFile() && metadata.size === session.offset)
|
|
411
2535
|
return;
|
|
2536
|
+
if (throughOffset === "file-size")
|
|
2537
|
+
throughOffset = metadata.size;
|
|
412
2538
|
file = await open(path, "r");
|
|
413
2539
|
}
|
|
414
2540
|
catch (error) {
|
|
@@ -418,8 +2544,10 @@ async function readSessionUsage(path, session, repository) {
|
|
|
418
2544
|
}
|
|
419
2545
|
try {
|
|
420
2546
|
const buffer = Buffer.alloc(SESSION_READ_SIZE);
|
|
421
|
-
while (
|
|
422
|
-
const { bytesRead } = await file.read(buffer, 0,
|
|
2547
|
+
while (throughOffset === undefined || session.offset < throughOffset) {
|
|
2548
|
+
const { bytesRead } = await file.read(buffer, 0, throughOffset === undefined
|
|
2549
|
+
? buffer.length
|
|
2550
|
+
: Math.min(buffer.length, throughOffset - session.offset), session.offset);
|
|
423
2551
|
if (bytesRead === 0)
|
|
424
2552
|
return;
|
|
425
2553
|
session.offset += bytesRead;
|
|
@@ -429,7 +2557,7 @@ async function readSessionUsage(path, session, repository) {
|
|
|
429
2557
|
.split("\n");
|
|
430
2558
|
session.pendingLine.push(lines[0]);
|
|
431
2559
|
for (const line of lines.slice(1)) {
|
|
432
|
-
readSessionEvent(session.pendingLine.join(""), session, repository);
|
|
2560
|
+
readSessionEvent(session.pendingLine.join(""), session, repository, progressThread);
|
|
433
2561
|
session.pendingLine = [line];
|
|
434
2562
|
}
|
|
435
2563
|
}
|
|
@@ -444,18 +2572,19 @@ async function readSessionUsage(path, session, repository) {
|
|
|
444
2572
|
await file.close();
|
|
445
2573
|
}
|
|
446
2574
|
}
|
|
447
|
-
function readSessionEvent(line, session, repository) {
|
|
2575
|
+
function readSessionEvent(line, session, repository, progressThread) {
|
|
448
2576
|
if (line.length === 0)
|
|
449
2577
|
return;
|
|
450
2578
|
const event = parseJson(() => line);
|
|
451
2579
|
if (!isRecord(event) || !isRecord(event["payload"]))
|
|
452
2580
|
return;
|
|
2581
|
+
session.eventPosition += 1;
|
|
453
2582
|
const payload = event["payload"];
|
|
454
2583
|
if (event["type"] === "session_meta") {
|
|
455
2584
|
if (session.threadId !== null) {
|
|
456
2585
|
session.replaying = payload["id"] !== session.threadId;
|
|
457
2586
|
if (!session.replaying)
|
|
458
|
-
session.events?.push(event);
|
|
2587
|
+
session.events?.push(sessionObservation(session, event));
|
|
459
2588
|
return;
|
|
460
2589
|
}
|
|
461
2590
|
if (typeof payload["id"] === "string") {
|
|
@@ -466,7 +2595,7 @@ function readSessionEvent(line, session, repository) {
|
|
|
466
2595
|
}
|
|
467
2596
|
session.startedAt = sessionStartedAt(payload["timestamp"]);
|
|
468
2597
|
session.parentThreadId = sessionParentThreadId(payload);
|
|
469
|
-
session.events?.push(event);
|
|
2598
|
+
session.events?.push(sessionObservation(session, event));
|
|
470
2599
|
return;
|
|
471
2600
|
}
|
|
472
2601
|
if (session.replaying) {
|
|
@@ -480,11 +2609,12 @@ function readSessionEvent(line, session, repository) {
|
|
|
480
2609
|
if (payload["type"] === "task_started" &&
|
|
481
2610
|
sessionOwnsTurn(session, payload)) {
|
|
482
2611
|
session.replaying = false;
|
|
483
|
-
session.events?.push(event);
|
|
2612
|
+
session.events?.push(sessionObservation(session, event));
|
|
484
2613
|
}
|
|
485
2614
|
return;
|
|
486
2615
|
}
|
|
487
|
-
session.events?.push(event);
|
|
2616
|
+
session.events?.push(sessionObservation(session, event));
|
|
2617
|
+
const position = ++session.position;
|
|
488
2618
|
if (event["type"] === "token_usage_record") {
|
|
489
2619
|
const responseId = payload["response_id"];
|
|
490
2620
|
if (payload["thread_id"] !== session.threadId ||
|
|
@@ -511,7 +2641,34 @@ function readSessionEvent(line, session, repository) {
|
|
|
511
2641
|
return;
|
|
512
2642
|
}
|
|
513
2643
|
if (event["type"] === "response_item") {
|
|
514
|
-
|
|
2644
|
+
const receipt = payload["type"] === "function_call_output" ||
|
|
2645
|
+
payload["type"] === "custom_tool_call_output" ||
|
|
2646
|
+
payload["type"] === "local_shell_call_output";
|
|
2647
|
+
const message = payload["type"] === "message" &&
|
|
2648
|
+
payload["role"] === "assistant" &&
|
|
2649
|
+
Array.isArray(payload["content"])
|
|
2650
|
+
? payload["content"]
|
|
2651
|
+
.filter(isRecord)
|
|
2652
|
+
.map((part) => typeof part["text"] === "string" ? part["text"] : "")
|
|
2653
|
+
.join("")
|
|
2654
|
+
: null;
|
|
2655
|
+
const updates = sessionProgressUpdates(payload);
|
|
2656
|
+
const duplicateMessage = message !== null &&
|
|
2657
|
+
repeatedMessageProjection(session, digest(message), "response", updates);
|
|
2658
|
+
// Code-mode notifications and the final output can share a call ID.
|
|
2659
|
+
// Each appended output record remains a distinct completion receipt.
|
|
2660
|
+
for (const [markerIndex, update] of (duplicateMessage
|
|
2661
|
+
? []
|
|
2662
|
+
: updates).entries()) {
|
|
2663
|
+
session.progress.push({
|
|
2664
|
+
update,
|
|
2665
|
+
position,
|
|
2666
|
+
completion: false,
|
|
2667
|
+
markerIndex,
|
|
2668
|
+
receipt,
|
|
2669
|
+
duplicate: false,
|
|
2670
|
+
});
|
|
2671
|
+
}
|
|
515
2672
|
if (repository === undefined)
|
|
516
2673
|
return;
|
|
517
2674
|
if (payload["type"] === "reasoning" &&
|
|
@@ -561,7 +2718,7 @@ function readSessionEvent(line, session, repository) {
|
|
|
561
2718
|
if (activity.status === "running") {
|
|
562
2719
|
session.calls.set(activity.id, activity);
|
|
563
2720
|
}
|
|
564
|
-
session.activities.push(activity);
|
|
2721
|
+
session.activities.push(sessionObservation(session, activity));
|
|
565
2722
|
return;
|
|
566
2723
|
}
|
|
567
2724
|
if ((payload["type"] === "function_call_output" ||
|
|
@@ -569,15 +2726,53 @@ function readSessionEvent(line, session, repository) {
|
|
|
569
2726
|
typeof payload["call_id"] === "string") {
|
|
570
2727
|
const call = session.calls.get(payload["call_id"]);
|
|
571
2728
|
if (call !== undefined) {
|
|
572
|
-
session.activities.push({
|
|
2729
|
+
session.activities.push(sessionObservation(session, {
|
|
573
2730
|
...call,
|
|
574
2731
|
status: payload["status"] === "failed" ? "failed" : "completed",
|
|
575
|
-
});
|
|
2732
|
+
}));
|
|
576
2733
|
session.calls.delete(call.id);
|
|
577
2734
|
}
|
|
578
2735
|
}
|
|
579
2736
|
return;
|
|
580
2737
|
}
|
|
2738
|
+
if (session.threadId === progressThread &&
|
|
2739
|
+
event["type"] === "event_msg" &&
|
|
2740
|
+
payload["type"] === "item_completed") {
|
|
2741
|
+
const item = nativeProgressItem(payload["item"]);
|
|
2742
|
+
const signature = item?.signature;
|
|
2743
|
+
const value = payload["item"];
|
|
2744
|
+
const duplicateCompletion = signature?.kind === "message" &&
|
|
2745
|
+
repeatedMessageProjection(session, signature.outputDigest, "native");
|
|
2746
|
+
let completion;
|
|
2747
|
+
if (isRecord(value) &&
|
|
2748
|
+
(value["type"] === "CommandExecution" ||
|
|
2749
|
+
value["type"] === "AgentMessage") &&
|
|
2750
|
+
!duplicateCompletion) {
|
|
2751
|
+
completion = {
|
|
2752
|
+
position,
|
|
2753
|
+
item: signature ?? null,
|
|
2754
|
+
kind: signature?.kind,
|
|
2755
|
+
};
|
|
2756
|
+
session.completions.push(completion);
|
|
2757
|
+
}
|
|
2758
|
+
if (item !== null) {
|
|
2759
|
+
const duplicate = item.signature.kind === "message" &&
|
|
2760
|
+
repeatedMessageProjection(session, item.signature.outputDigest, "native", item.updates);
|
|
2761
|
+
for (const [markerIndex, update] of item.updates.entries()) {
|
|
2762
|
+
const saved = {
|
|
2763
|
+
update,
|
|
2764
|
+
position,
|
|
2765
|
+
completion: true,
|
|
2766
|
+
markerIndex,
|
|
2767
|
+
receipt: false,
|
|
2768
|
+
duplicate,
|
|
2769
|
+
};
|
|
2770
|
+
session.progress.push(saved);
|
|
2771
|
+
if (completion !== undefined && item.signature.kind === "command")
|
|
2772
|
+
completion.lastCommandProgress = saved;
|
|
2773
|
+
}
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
581
2776
|
if (event["type"] === "event_msg" &&
|
|
582
2777
|
(payload["type"] === "agent_reasoning" ||
|
|
583
2778
|
payload["type"] === "agent_reasoning_delta" ||
|
|
@@ -586,7 +2781,29 @@ function readSessionEvent(line, session, repository) {
|
|
|
586
2781
|
payload["type"] === "agent_message")) {
|
|
587
2782
|
if (payload["type"] === "agent_message" &&
|
|
588
2783
|
typeof payload["message"] === "string") {
|
|
589
|
-
|
|
2784
|
+
const signature = progressSignature("message", payload["message"]);
|
|
2785
|
+
const updates = scanProgressUpdatesFromText(payload["message"]);
|
|
2786
|
+
const duplicate = repeatedMessageProjection(session, signature.outputDigest, "event", updates);
|
|
2787
|
+
// Legacy rollouts persist the finalized message instead of the native
|
|
2788
|
+
// item. It provides the same compact output signature for live matching.
|
|
2789
|
+
if (session.threadId === progressThread &&
|
|
2790
|
+
!repeatedMessageProjection(session, signature.outputDigest, "event"))
|
|
2791
|
+
session.completions.push({
|
|
2792
|
+
position,
|
|
2793
|
+
item: signature,
|
|
2794
|
+
});
|
|
2795
|
+
for (const [markerIndex, update] of (duplicate
|
|
2796
|
+
? []
|
|
2797
|
+
: updates).entries()) {
|
|
2798
|
+
session.progress.push({
|
|
2799
|
+
update,
|
|
2800
|
+
position,
|
|
2801
|
+
completion: false,
|
|
2802
|
+
markerIndex,
|
|
2803
|
+
receipt: false,
|
|
2804
|
+
duplicate: false,
|
|
2805
|
+
});
|
|
2806
|
+
}
|
|
590
2807
|
}
|
|
591
2808
|
if (repository === undefined)
|
|
592
2809
|
return;
|
|
@@ -599,7 +2816,7 @@ function readSessionEvent(line, session, repository) {
|
|
|
599
2816
|
if (activity !== null &&
|
|
600
2817
|
!session.prose.has(`${activity.kind}:${activity.description}`)) {
|
|
601
2818
|
session.prose.add(`${activity.kind}:${activity.description}`);
|
|
602
|
-
session.activities.push(activity);
|
|
2819
|
+
session.activities.push(sessionObservation(session, activity));
|
|
603
2820
|
}
|
|
604
2821
|
return;
|
|
605
2822
|
}
|
|
@@ -694,7 +2911,243 @@ function recordReasoningActivity(session, activity) {
|
|
|
694
2911
|
}
|
|
695
2912
|
reasoning.activity = activity;
|
|
696
2913
|
session.prose.add(`${activity.kind}:${activity.description}`);
|
|
697
|
-
session.activities.push(activity);
|
|
2914
|
+
session.activities.push(sessionObservation(session, activity));
|
|
2915
|
+
}
|
|
2916
|
+
const PERSISTED_OUTPUT_GAP = "\n... command output truncated for persistence ...\n";
|
|
2917
|
+
// Native Codex 0.162.1 persists command output with a 64 KiB UTF-8 budget.
|
|
2918
|
+
// This signature mirrors that representation; emitted diagnostics stay intact.
|
|
2919
|
+
const PERSISTED_COMMAND_OUTPUT_BYTES = 64 * 1024;
|
|
2920
|
+
const PERSISTED_OUTPUT_GAP_BYTES = Buffer.byteLength(PERSISTED_OUTPUT_GAP);
|
|
2921
|
+
const PERSISTED_COMMAND_PREFIX_BYTES = Math.floor((PERSISTED_COMMAND_OUTPUT_BYTES - PERSISTED_OUTPUT_GAP_BYTES) / 2);
|
|
2922
|
+
const PERSISTED_COMMAND_SUFFIX_BYTES = PERSISTED_COMMAND_OUTPUT_BYTES -
|
|
2923
|
+
PERSISTED_OUTPUT_GAP_BYTES -
|
|
2924
|
+
PERSISTED_COMMAND_PREFIX_BYTES;
|
|
2925
|
+
function digest(text) {
|
|
2926
|
+
return createHash("sha256").update(text).digest("hex");
|
|
2927
|
+
}
|
|
2928
|
+
function persistedOutputDigest(text, fullDigest) {
|
|
2929
|
+
if (Buffer.byteLength(text) <= PERSISTED_COMMAND_OUTPUT_BYTES)
|
|
2930
|
+
return fullDigest;
|
|
2931
|
+
const bytes = Buffer.from(text);
|
|
2932
|
+
let prefixEnd = PERSISTED_COMMAND_PREFIX_BYTES;
|
|
2933
|
+
let suffixStart = bytes.length - PERSISTED_COMMAND_SUFFIX_BYTES;
|
|
2934
|
+
while ((bytes[prefixEnd] & 0xc0) === 0x80)
|
|
2935
|
+
prefixEnd--;
|
|
2936
|
+
while ((bytes[suffixStart] & 0xc0) === 0x80)
|
|
2937
|
+
suffixStart++;
|
|
2938
|
+
return createHash("sha256")
|
|
2939
|
+
.update(bytes.subarray(0, prefixEnd))
|
|
2940
|
+
.update(PERSISTED_OUTPUT_GAP)
|
|
2941
|
+
.update(bytes.subarray(suffixStart))
|
|
2942
|
+
.digest("hex");
|
|
2943
|
+
}
|
|
2944
|
+
function isInventoryProgress(progress) {
|
|
2945
|
+
return progress.phase === "discovery" && progress.filesCompleted === 0;
|
|
2946
|
+
}
|
|
2947
|
+
function progressSignature(kind, text, command) {
|
|
2948
|
+
const outputDigest = digest(text);
|
|
2949
|
+
return {
|
|
2950
|
+
kind,
|
|
2951
|
+
outputDigest,
|
|
2952
|
+
persistedOutputDigest: kind === "command"
|
|
2953
|
+
? persistedOutputDigest(text, outputDigest)
|
|
2954
|
+
: outputDigest,
|
|
2955
|
+
...(command === undefined
|
|
2956
|
+
? {}
|
|
2957
|
+
: { commandDigest: digest(JSON.stringify(command)) }),
|
|
2958
|
+
};
|
|
2959
|
+
}
|
|
2960
|
+
function repeatedMessageProjection(session, textDigest, projection, updates) {
|
|
2961
|
+
if (updates?.length === 0)
|
|
2962
|
+
return false;
|
|
2963
|
+
// Completion matching counts every native/legacy message, even without
|
|
2964
|
+
// progress. Keep those occurrences separate from parsed progress copies:
|
|
2965
|
+
// multipart responses can recover updates absent from their native copy.
|
|
2966
|
+
const key = updates === undefined
|
|
2967
|
+
? textDigest
|
|
2968
|
+
: `${textDigest}:${digest(JSON.stringify(updates))}`;
|
|
2969
|
+
if (updates !== undefined) {
|
|
2970
|
+
let queue = session.messageProgressProjections.get(key);
|
|
2971
|
+
if (queue === undefined) {
|
|
2972
|
+
queue = { occurrences: [], next: { native: 0, event: 0, response: 0 } };
|
|
2973
|
+
session.messageProgressProjections.set(key, queue);
|
|
2974
|
+
}
|
|
2975
|
+
// Copies retain their order within each projection. A missing earlier copy
|
|
2976
|
+
// cannot consume an identical message after that projection has advanced.
|
|
2977
|
+
// These ordinals identify progress copies only, never completion positions.
|
|
2978
|
+
while (queue.next[projection] < queue.occurrences.length) {
|
|
2979
|
+
const order = queue.occurrences[queue.next[projection]++];
|
|
2980
|
+
if (order <= session.messageProgressFrontiers[projection])
|
|
2981
|
+
continue;
|
|
2982
|
+
session.messageProgressFrontiers[projection] = order;
|
|
2983
|
+
return true;
|
|
2984
|
+
}
|
|
2985
|
+
const order = ++session.messageProgressOrder;
|
|
2986
|
+
queue.occurrences.push(order);
|
|
2987
|
+
queue.next[projection] = queue.occurrences.length;
|
|
2988
|
+
session.messageProgressFrontiers[projection] = order;
|
|
2989
|
+
return false;
|
|
2990
|
+
}
|
|
2991
|
+
let counts = session.messageProjections.get(key);
|
|
2992
|
+
if (counts === undefined) {
|
|
2993
|
+
counts = { native: 0, event: 0, response: 0 };
|
|
2994
|
+
session.messageProjections.set(key, counts);
|
|
2995
|
+
}
|
|
2996
|
+
const previous = Math.max(counts.native, counts.event, counts.response);
|
|
2997
|
+
counts[projection]++;
|
|
2998
|
+
return counts[projection] <= previous;
|
|
2999
|
+
}
|
|
3000
|
+
function progressKey(progress) {
|
|
3001
|
+
return `${progress.phase}:${progress.filesCompleted}:${progress.filesTotal}`;
|
|
3002
|
+
}
|
|
3003
|
+
function streamedProgressItem(event) {
|
|
3004
|
+
const item = event["item"];
|
|
3005
|
+
if (!isRecord(item))
|
|
3006
|
+
return null;
|
|
3007
|
+
if (item["type"] === "agent_message" && typeof item["text"] === "string")
|
|
3008
|
+
return progressSignature("message", item["text"]);
|
|
3009
|
+
if (item["type"] !== "command_execution" ||
|
|
3010
|
+
typeof item["aggregated_output"] !== "string" ||
|
|
3011
|
+
typeof item["command"] !== "string")
|
|
3012
|
+
return null;
|
|
3013
|
+
const command = splitCommand(item["command"]);
|
|
3014
|
+
return command === null
|
|
3015
|
+
? null
|
|
3016
|
+
: progressSignature("command", item["aggregated_output"], command);
|
|
3017
|
+
}
|
|
3018
|
+
function nativeProgressItem(value) {
|
|
3019
|
+
if (!isRecord(value))
|
|
3020
|
+
return null;
|
|
3021
|
+
let kind;
|
|
3022
|
+
let text;
|
|
3023
|
+
let command;
|
|
3024
|
+
if (value["type"] === "AgentMessage" && Array.isArray(value["content"])) {
|
|
3025
|
+
kind = "message";
|
|
3026
|
+
text = value["content"]
|
|
3027
|
+
.filter((part) => isRecord(part) &&
|
|
3028
|
+
part["type"] === "Text" &&
|
|
3029
|
+
typeof part["text"] === "string")
|
|
3030
|
+
.map((part) => part.text)
|
|
3031
|
+
.join("");
|
|
3032
|
+
}
|
|
3033
|
+
else if (value["type"] === "CommandExecution" &&
|
|
3034
|
+
typeof value["aggregated_output"] === "string" &&
|
|
3035
|
+
Array.isArray(value["command"]) &&
|
|
3036
|
+
value["command"].every((arg) => typeof arg === "string")) {
|
|
3037
|
+
kind = "command";
|
|
3038
|
+
text = value["aggregated_output"];
|
|
3039
|
+
command = value["command"];
|
|
3040
|
+
}
|
|
3041
|
+
else
|
|
3042
|
+
return null;
|
|
3043
|
+
let progressText = text;
|
|
3044
|
+
if (kind === "command") {
|
|
3045
|
+
const bytes = Buffer.from(text);
|
|
3046
|
+
// Native UTF-8 boundary adjustment can shorten each fragment by up to
|
|
3047
|
+
// three bytes. An earlier literal sentinel is ordinary command output.
|
|
3048
|
+
const gap = bytes.indexOf(PERSISTED_OUTPUT_GAP, PERSISTED_COMMAND_PREFIX_BYTES - 3);
|
|
3049
|
+
const suffixBytes = bytes.length - gap - PERSISTED_OUTPUT_GAP_BYTES;
|
|
3050
|
+
if (gap !== -1 &&
|
|
3051
|
+
gap <= PERSISTED_COMMAND_PREFIX_BYTES &&
|
|
3052
|
+
suffixBytes >= PERSISTED_COMMAND_SUFFIX_BYTES - 3 &&
|
|
3053
|
+
suffixBytes <= PERSISTED_COMMAND_SUFFIX_BYTES) {
|
|
3054
|
+
// Complete original prefix lines have known fence state. The gap inserts
|
|
3055
|
+
// its own newline, so the partial prefix line and tail remain unproven.
|
|
3056
|
+
// Literal text at these exact boundaries is indistinguishable on disk.
|
|
3057
|
+
const prefix = bytes.subarray(0, gap).toString("utf8");
|
|
3058
|
+
progressText = prefix.slice(0, prefix.lastIndexOf("\n") + 1);
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
return {
|
|
3062
|
+
signature: progressSignature(kind, text, command),
|
|
3063
|
+
updates: scanProgressUpdatesFromText(progressText),
|
|
3064
|
+
};
|
|
3065
|
+
}
|
|
3066
|
+
function progressItemKey(item, persisted = false) {
|
|
3067
|
+
return `${item.kind}:${item.commandDigest ?? ""}:${persisted ? item.persistedOutputDigest : item.outputDigest}`;
|
|
3068
|
+
}
|
|
3069
|
+
function progressItemKeys(item) {
|
|
3070
|
+
const keys = [progressItemKey(item)];
|
|
3071
|
+
if (item.outputDigest !== item.persistedOutputDigest)
|
|
3072
|
+
keys.push(progressItemKey(item, true));
|
|
3073
|
+
return keys;
|
|
3074
|
+
}
|
|
3075
|
+
// Min-heap keeps compatible live occurrences ordered without rescanning the
|
|
3076
|
+
// unmatched prefix or sorting every completion batch.
|
|
3077
|
+
function insertIndex(heap, index) {
|
|
3078
|
+
let child = heap.length;
|
|
3079
|
+
heap.push(index);
|
|
3080
|
+
while (child > 0) {
|
|
3081
|
+
const parent = (child - 1) >>> 1;
|
|
3082
|
+
if (heap[parent] <= index)
|
|
3083
|
+
break;
|
|
3084
|
+
heap[child] = heap[parent];
|
|
3085
|
+
child = parent;
|
|
3086
|
+
}
|
|
3087
|
+
heap[child] = index;
|
|
3088
|
+
}
|
|
3089
|
+
function takeFirstIndex(heap) {
|
|
3090
|
+
const first = heap[0];
|
|
3091
|
+
const last = heap.pop();
|
|
3092
|
+
if (heap.length === 0)
|
|
3093
|
+
return first;
|
|
3094
|
+
let parent = 0;
|
|
3095
|
+
while (parent * 2 + 1 < heap.length) {
|
|
3096
|
+
let child = parent * 2 + 1;
|
|
3097
|
+
if (child + 1 < heap.length && heap[child + 1] < heap[child])
|
|
3098
|
+
child++;
|
|
3099
|
+
if (last <= heap[child])
|
|
3100
|
+
break;
|
|
3101
|
+
heap[parent] = heap[child];
|
|
3102
|
+
parent = child;
|
|
3103
|
+
}
|
|
3104
|
+
heap[parent] = last;
|
|
3105
|
+
return first;
|
|
3106
|
+
}
|
|
3107
|
+
/** Decode the quoted argv rendering emitted by native command completions. */
|
|
3108
|
+
function splitCommand(command) {
|
|
3109
|
+
const words = [];
|
|
3110
|
+
let word = "";
|
|
3111
|
+
let started = false;
|
|
3112
|
+
let quote = null;
|
|
3113
|
+
for (let index = 0; index < command.length; index++) {
|
|
3114
|
+
const character = command[index];
|
|
3115
|
+
if (character === "\\" && quote !== "'") {
|
|
3116
|
+
const next = command[++index];
|
|
3117
|
+
if (next === undefined)
|
|
3118
|
+
return null;
|
|
3119
|
+
if (quote === '"' && !["$", "`", '"', "\\", "\n"].includes(next))
|
|
3120
|
+
word += "\\";
|
|
3121
|
+
if (next !== "\n")
|
|
3122
|
+
word += next;
|
|
3123
|
+
started = true;
|
|
3124
|
+
}
|
|
3125
|
+
else if (quote !== null) {
|
|
3126
|
+
if (character === quote)
|
|
3127
|
+
quote = null;
|
|
3128
|
+
else
|
|
3129
|
+
word += character;
|
|
3130
|
+
}
|
|
3131
|
+
else if (character === "'" || character === '"') {
|
|
3132
|
+
quote = character;
|
|
3133
|
+
started = true;
|
|
3134
|
+
}
|
|
3135
|
+
else if (" \t\r\n".includes(character)) {
|
|
3136
|
+
if (started)
|
|
3137
|
+
words.push(word);
|
|
3138
|
+
word = "";
|
|
3139
|
+
started = false;
|
|
3140
|
+
}
|
|
3141
|
+
else {
|
|
3142
|
+
word += character;
|
|
3143
|
+
started = true;
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
if (quote !== null)
|
|
3147
|
+
return null;
|
|
3148
|
+
if (started)
|
|
3149
|
+
words.push(word);
|
|
3150
|
+
return words;
|
|
698
3151
|
}
|
|
699
3152
|
function sessionProgressUpdates(payload) {
|
|
700
3153
|
if (payload["type"] === "message" && payload["role"] === "assistant") {
|
|
@@ -714,7 +3167,7 @@ function sessionProgressUpdates(payload) {
|
|
|
714
3167
|
}
|
|
715
3168
|
return scanProgressUpdatesFromText(output);
|
|
716
3169
|
}
|
|
717
|
-
function addTokenUsage(previous, next) {
|
|
3170
|
+
export function addTokenUsage(previous, next) {
|
|
718
3171
|
if (previous === null)
|
|
719
3172
|
return next;
|
|
720
3173
|
return {
|
|
@@ -743,6 +3196,13 @@ function subtractTokenUsage(usage, inherited) {
|
|
|
743
3196
|
reasoning_output_tokens: usage.reasoning_output_tokens - inherited.reasoning_output_tokens,
|
|
744
3197
|
});
|
|
745
3198
|
}
|
|
3199
|
+
function isBlockedLogRead(error) {
|
|
3200
|
+
return (isRecord(error) &&
|
|
3201
|
+
(error["code"] === "EACCES" ||
|
|
3202
|
+
error["code"] === "EPERM" ||
|
|
3203
|
+
error["code"] === "EIO") &&
|
|
3204
|
+
(error["syscall"] === "open" || error["syscall"] === "read"));
|
|
3205
|
+
}
|
|
746
3206
|
function isMissingFile(error) {
|
|
747
3207
|
return isRecord(error) && error["code"] === "ENOENT";
|
|
748
3208
|
}
|