@openai/codex-security 0.8.2 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -61
- package/_bundled_plugin/.app.json +4 -0
- package/_bundled_plugin/.mcp.json +1 -0
- 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/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/permission-profile-preflight.mjs +46 -12
- 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/references/artifact-storage.md +1 -1
- package/_bundled_plugin/references/core-scan.md +14 -5
- package/_bundled_plugin/references/default-severity-rubric.md +28 -0
- package/_bundled_plugin/references/finding-detail-fields.md +1 -0
- package/_bundled_plugin/references/threat-model.md +1 -0
- package/_bundled_plugin/scripts/deep_scan_workbench.py +23 -112
- package/_bundled_plugin/scripts/finalize_scan_contract.py +167 -153
- package/_bundled_plugin/scripts/finding_preview.py +5 -29
- package/_bundled_plugin/scripts/posix_scan_local_files.py +187 -0
- package/_bundled_plugin/scripts/report_projection.py +87 -136
- package/_bundled_plugin/scripts/validate_scan_contract.py +2 -6
- package/_bundled_plugin/scripts/windows_scan_local_files.py +212 -55
- package/_bundled_plugin/scripts/workbench/retained_coverage.py +8 -24
- package/_bundled_plugin/scripts/workbench/saved_coverage.py +143 -231
- package/_bundled_plugin/scripts/workbench_cli.py +102 -13
- package/_bundled_plugin/scripts/workbench_constants.py +9 -0
- package/_bundled_plugin/scripts/workbench_db.py +186 -360
- package/_bundled_plugin/scripts/workbench_finding_workflows.py +86 -4
- package/_bundled_plugin/scripts/workbench_native_indexes.py +17 -21
- package/_bundled_plugin/scripts/workbench_remediation.py +495 -11
- package/_bundled_plugin/scripts/workbench_scan_history.py +22 -25
- package/_bundled_plugin/scripts/workbench_scan_start.py +1 -1
- package/_bundled_plugin/scripts/workbench_scan_usage.py +25 -52
- package/_bundled_plugin/scripts/workbench_schema.py +1 -1
- package/_bundled_plugin/scripts/workbench_setup.py +239 -0
- package/_bundled_plugin/scripts/workbench_source_scopes.py +15 -23
- package/_bundled_plugin/scripts/workbench_target.py +213 -20
- package/_bundled_plugin/scripts/workbench_validation.py +1 -9
- package/_bundled_plugin/shared/workbench-history-repairs.json +7 -0
- package/_bundled_plugin/shared/workbench-migrations.json +19 -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 +2 -0
- package/_bundled_plugin/skills/fix-finding/SKILL.md +18 -1
- package/_bundled_plugin/skills/publish-findings/SKILL.md +64 -35
- package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +2 -21
- package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +14 -8
- 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 +6 -4
- package/dist/api.d.ts +6 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +411 -160
- package/dist/api.js.map +1 -1
- package/dist/auth.js +6 -1
- package/dist/auth.js.map +1 -1
- package/dist/blackduck-findings.d.ts +8 -0
- package/dist/blackduck-findings.d.ts.map +1 -0
- package/dist/blackduck-findings.js +305 -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/classify-severity-records.d.ts +5 -0
- package/dist/classify-severity-records.d.ts.map +1 -1
- package/dist/classify-severity-records.js +6 -8
- 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 +28 -9
- package/dist/classify-severity.js.map +1 -1
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +608 -258
- package/dist/cli.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.map +1 -1
- package/dist/component-scan.js +6 -5
- 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 +162 -40
- 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 +157 -0
- package/dist/continuum-findings.js.map +1 -0
- package/dist/contract-values.d.ts +28 -0
- package/dist/contract-values.d.ts.map +1 -0
- package/dist/contract-values.js +75 -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.map +1 -1
- package/dist/cost-model.js +1 -4
- package/dist/cost-model.js.map +1 -1
- package/dist/cost.d.ts +19 -1
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +2120 -39
- package/dist/cost.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.map +1 -1
- package/dist/deduplication/local.js +3 -8
- 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/deep-scan.d.ts +11 -0
- package/dist/deep-scan.d.ts.map +1 -1
- package/dist/deep-scan.js +235 -4
- 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.map +1 -1
- package/dist/endor-findings.js +7 -37
- package/dist/endor-findings.js.map +1 -1
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/external-findings-import.d.ts.map +1 -1
- package/dist/external-findings-import.js +5 -11
- package/dist/external-findings-import.js.map +1 -1
- package/dist/external-findings-local.d.ts +9 -0
- package/dist/external-findings-local.d.ts.map +1 -1
- package/dist/external-findings-local.js +325 -179
- package/dist/external-findings-local.js.map +1 -1
- package/dist/external-findings-publish.d.ts +12 -0
- package/dist/external-findings-publish.d.ts.map +1 -1
- package/dist/external-findings-publish.js +1202 -214
- package/dist/external-findings-publish.js.map +1 -1
- package/dist/external-findings-read.d.ts +8 -0
- package/dist/external-findings-read.d.ts.map +1 -1
- package/dist/external-findings-read.js +65 -54
- package/dist/external-findings-read.js.map +1 -1
- package/dist/external-findings.d.ts +2 -0
- package/dist/external-findings.d.ts.map +1 -1
- package/dist/external-findings.js +111 -0
- package/dist/external-findings.js.map +1 -1
- package/dist/external-import-contract.d.ts +29 -1
- package/dist/external-import-contract.d.ts.map +1 -1
- package/dist/external-import-contract.js +314 -44
- 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/finding-normalization.d.ts +1 -1
- package/dist/finding-normalization.d.ts.map +1 -1
- package/dist/finding-normalization.js +46 -11
- package/dist/finding-normalization.js.map +1 -1
- 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/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 -29
- package/dist/github-advisories.js.map +1 -1
- package/dist/github-dependabot-findings.d.ts.map +1 -1
- package/dist/github-dependabot-findings.js +7 -16
- package/dist/github-dependabot-findings.js.map +1 -1
- package/dist/github-findings.d.ts.map +1 -1
- package/dist/github-findings.js +344 -56
- 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-scan.d.ts +15 -0
- package/dist/hosted-scan.d.ts.map +1 -1
- package/dist/hosted-scan.js +55 -33
- package/dist/hosted-scan.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/inspector-findings.d.ts.map +1 -1
- package/dist/inspector-findings.js +13 -18
- package/dist/inspector-findings.js.map +1 -1
- package/dist/knowledge-base.js +18 -5
- 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 +131 -0
- package/dist/local-git-metadata.js.map +1 -0
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +22 -12
- package/dist/multiscan.js.map +1 -1
- package/dist/opengrep-findings.d.ts.map +1 -1
- package/dist/opengrep-findings.js +34 -20
- package/dist/opengrep-findings.js.map +1 -1
- 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/private-finding-import.d.ts +246 -0
- package/dist/private-finding-import.d.ts.map +1 -0
- package/dist/private-finding-import.js +387 -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 +10 -2
- package/dist/provider-profile.d.ts.map +1 -1
- package/dist/provider-profile.js +72 -10
- package/dist/provider-profile.js.map +1 -1
- package/dist/publication.js +7 -1
- package/dist/publication.js.map +1 -1
- package/dist/reader-selection.d.ts +13 -0
- package/dist/reader-selection.d.ts.map +1 -0
- package/dist/reader-selection.js +81 -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 +5 -0
- package/dist/remediation.d.ts.map +1 -1
- package/dist/remediation.js +30 -0
- package/dist/remediation.js.map +1 -1
- package/dist/result.d.ts +2 -0
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +3 -1
- package/dist/result.js.map +1 -1
- package/dist/runtime.d.ts +2 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +95 -52
- package/dist/runtime.js.map +1 -1
- package/dist/scan-activity.js +40 -17
- package/dist/scan-activity.js.map +1 -1
- package/dist/scan-comparison.d.ts +1 -1
- package/dist/scan-comparison.d.ts.map +1 -1
- package/dist/scan-comparison.js +11 -13
- package/dist/scan-comparison.js.map +1 -1
- package/dist/scan-dashboard.d.ts +1 -0
- package/dist/scan-dashboard.d.ts.map +1 -1
- package/dist/scan-dashboard.js +16 -37
- package/dist/scan-dashboard.js.map +1 -1
- package/dist/scan-executor.d.ts +2 -0
- package/dist/scan-executor.d.ts.map +1 -1
- package/dist/scan-executor.js.map +1 -1
- package/dist/scan-history-renderer.d.ts.map +1 -1
- package/dist/scan-history-renderer.js +22 -9
- package/dist/scan-history-renderer.js.map +1 -1
- package/dist/scan-logs.d.ts.map +1 -1
- package/dist/scan-logs.js +5 -5
- package/dist/scan-logs.js.map +1 -1
- package/dist/scanner-import.d.ts +46 -6
- package/dist/scanner-import.d.ts.map +1 -1
- package/dist/scanner-import.js +208 -42
- package/dist/scanner-import.js.map +1 -1
- package/dist/scanner-repository-url.d.ts +14 -0
- package/dist/scanner-repository-url.d.ts.map +1 -0
- package/dist/scanner-repository-url.js +67 -0
- package/dist/scanner-repository-url.js.map +1 -0
- package/dist/semgrep-findings.d.ts.map +1 -1
- package/dist/semgrep-findings.js +20 -48
- package/dist/semgrep-findings.js.map +1 -1
- package/dist/semgrep-platform-findings.d.ts +12 -0
- package/dist/semgrep-platform-findings.d.ts.map +1 -0
- package/dist/semgrep-platform-findings.js +198 -0
- package/dist/semgrep-platform-findings.js.map +1 -0
- 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/snyk-findings.d.ts.map +1 -1
- package/dist/snyk-findings.js +21 -20
- package/dist/snyk-findings.js.map +1 -1
- package/dist/sonar-collector.d.ts +16 -0
- package/dist/sonar-collector.d.ts.map +1 -0
- package/dist/sonar-collector.js +383 -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 +279 -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/targets.d.ts +3 -0
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +18 -12
- package/dist/targets.js.map +1 -1
- 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/validation-records.d.ts +0 -3
- package/dist/validation-records.d.ts.map +1 -1
- package/dist/validation-records.js +0 -16
- package/dist/validation-records.js.map +1 -1
- package/dist/veracode-findings.d.ts +7 -0
- package/dist/veracode-findings.d.ts.map +1 -0
- package/dist/veracode-findings.js +371 -0
- package/dist/veracode-findings.js.map +1 -0
- package/dist/windows-path.d.ts.map +1 -1
- package/dist/windows-path.js +1 -0
- 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 +10 -2
- package/dist/wiz-findings.d.ts.map +1 -1
- package/dist/wiz-findings.js +88 -49
- package/dist/wiz-findings.js.map +1 -1
- package/docs/classify-severity-records.md +1 -1
- package/docs/cli.md +310 -477
- package/docs/dedupe-records.md +10 -0
- package/docs/findings-service.md +21 -8
- package/docs/hosted-scans.md +16 -1
- package/package.json +8 -9
- package/schemas/external-findings.schema.json +12 -1
- package/schemas/hosted-scan-v2.fixture.json +16 -1
- 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/cost.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { parseJson } from "./value.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
import { open, readdir, stat } from "node:fs/promises";
|
|
3
4
|
import { join } from "node:path";
|
|
4
5
|
import { StringDecoder } from "node:string_decoder";
|
|
@@ -29,6 +30,51 @@ const rateLimitSnapshot = z.object({
|
|
|
29
30
|
function createSessionReasoning(id, text = "") {
|
|
30
31
|
return { id, text, raw: false, activity: null };
|
|
31
32
|
}
|
|
33
|
+
function savedProgress(source) {
|
|
34
|
+
return {
|
|
35
|
+
progress: source.update,
|
|
36
|
+
position: source.position,
|
|
37
|
+
markerIndex: source.markerIndex,
|
|
38
|
+
order: source.order ?? 0,
|
|
39
|
+
source,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// A live parent keeps authority over an unresolved saved prefix. Once an exact
|
|
43
|
+
// anchor bounds that prefix, source order also governs admission and display.
|
|
44
|
+
// Independent workers and legacy projections still use observation order.
|
|
45
|
+
function compareProgressOrder(left, right) {
|
|
46
|
+
const source = compareSourceOrder(left, right);
|
|
47
|
+
if (source !== undefined)
|
|
48
|
+
return source;
|
|
49
|
+
// A display preference is not source proof. Keep current live authority over
|
|
50
|
+
// an unresolved prefix, while retaining that prefix for later reconciliation.
|
|
51
|
+
if (left.live?.after !== undefined && right.position !== undefined)
|
|
52
|
+
return 1;
|
|
53
|
+
if (right.live?.after !== undefined && left.position !== undefined)
|
|
54
|
+
return -1;
|
|
55
|
+
const observedOrder = (candidate) => {
|
|
56
|
+
const marker = candidate.source?.projection?.markers[candidate.markerIndex ?? 0];
|
|
57
|
+
// A complete source projection retains its originating live order even
|
|
58
|
+
// when polling observes that copy after a newer unpositioned phase.
|
|
59
|
+
return marker !== undefined &&
|
|
60
|
+
progressKey(marker.progress) === progressKey(candidate.source.update)
|
|
61
|
+
? marker.order
|
|
62
|
+
: candidate.order;
|
|
63
|
+
};
|
|
64
|
+
return observedOrder(left) - observedOrder(right);
|
|
65
|
+
}
|
|
66
|
+
function compareSourceOrder(left, right) {
|
|
67
|
+
const leftPosition = left.position ?? left.live?.position ?? undefined;
|
|
68
|
+
const rightPosition = right.position ?? right.live?.position ?? undefined;
|
|
69
|
+
if (leftPosition !== undefined && rightPosition !== undefined)
|
|
70
|
+
return (leftPosition - rightPosition ||
|
|
71
|
+
(left.markerIndex ?? 0) - (right.markerIndex ?? 0));
|
|
72
|
+
if (leftPosition !== undefined && right.live?.after !== undefined)
|
|
73
|
+
return leftPosition >= right.live.after ? 1 : undefined;
|
|
74
|
+
if (rightPosition !== undefined && left.live?.after !== undefined)
|
|
75
|
+
return rightPosition >= left.live.after ? -1 : undefined;
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
32
78
|
const COST_POLL_INTERVAL_MS = 100;
|
|
33
79
|
const SESSION_READ_SIZE = 64 * 1_024;
|
|
34
80
|
function createSessionUsage(observeUsage) {
|
|
@@ -53,6 +99,14 @@ function createSessionUsage(observeUsage) {
|
|
|
53
99
|
calls: new Map(),
|
|
54
100
|
activities: [],
|
|
55
101
|
progress: [],
|
|
102
|
+
position: 0,
|
|
103
|
+
completions: [],
|
|
104
|
+
latestWorkerProgress: null,
|
|
105
|
+
pendingWorkerProgress: new Map(),
|
|
106
|
+
messageProjections: new Map(),
|
|
107
|
+
messageProgressOrder: 0,
|
|
108
|
+
messageProgressFrontiers: { native: 0, event: 0, response: 0 },
|
|
109
|
+
messageProgressProjections: new Map(),
|
|
56
110
|
filesCompleted: 0,
|
|
57
111
|
filesTotal: null,
|
|
58
112
|
prose: new Set(),
|
|
@@ -66,7 +120,37 @@ export class ScanCostTracker {
|
|
|
66
120
|
#receipts = new Map();
|
|
67
121
|
#workers = new Map();
|
|
68
122
|
#workerProgress = new Map();
|
|
69
|
-
#
|
|
123
|
+
#liveParentProgress = new Set();
|
|
124
|
+
#liveParentItems = [];
|
|
125
|
+
#liveParentEvents = new WeakMap();
|
|
126
|
+
#acceptedParent = null;
|
|
127
|
+
// Global coverage admitted from either the live or saved parent projection.
|
|
128
|
+
#parentProgress = null;
|
|
129
|
+
#parentFacts = new Map();
|
|
130
|
+
#parentInventory = null;
|
|
131
|
+
#parentInventoryBoundary = null;
|
|
132
|
+
// A bound excludes inventories at or after it without locating the review.
|
|
133
|
+
#parentReviewBefore = null;
|
|
134
|
+
#readyParentMarkers = new Map();
|
|
135
|
+
#inventoryFrontier = {
|
|
136
|
+
position: 0,
|
|
137
|
+
markerIndex: -1,
|
|
138
|
+
order: 0,
|
|
139
|
+
progress: undefined,
|
|
140
|
+
};
|
|
141
|
+
#progressOrder = 0;
|
|
142
|
+
#displayedProgress = null;
|
|
143
|
+
#matchedParentItems = 0;
|
|
144
|
+
#parentCompletionCursor = 0;
|
|
145
|
+
#indexedParentCompletions = 0;
|
|
146
|
+
#indexedParentItems = 0;
|
|
147
|
+
#parentCompletionKeys = new Map();
|
|
148
|
+
#parentItemKeys = new Map();
|
|
149
|
+
#parentMatches = [];
|
|
150
|
+
#pendingParentMatches = new Set();
|
|
151
|
+
#parentSession = null;
|
|
152
|
+
#parentTurnStartKnown = true;
|
|
153
|
+
#parentFrontier = { position: 0, markerIndex: -1 };
|
|
70
154
|
#threadId = null;
|
|
71
155
|
#observingWorkers = true;
|
|
72
156
|
#timer = null;
|
|
@@ -77,13 +161,1460 @@ export class ScanCostTracker {
|
|
|
77
161
|
#lastObservation = null;
|
|
78
162
|
#highestFilesCompleted = 0;
|
|
79
163
|
#expectedFilesTotal;
|
|
164
|
+
#parentTurnFilesTotal;
|
|
165
|
+
#inventoryNeedsReconciliation = false;
|
|
80
166
|
constructor(options) {
|
|
81
167
|
this.#options = options;
|
|
82
|
-
|
|
168
|
+
if (options.expectedFilesTotal !== undefined)
|
|
169
|
+
this.setExpectedFilesTotal(options.expectedFilesTotal);
|
|
83
170
|
}
|
|
84
171
|
setExpectedFilesTotal(filesTotal) {
|
|
172
|
+
this.#parentTurnFilesTotal ??= filesTotal;
|
|
173
|
+
if (this.#expectedFilesTotal !== undefined &&
|
|
174
|
+
this.#expectedFilesTotal !== filesTotal)
|
|
175
|
+
this.#inventoryNeedsReconciliation = true;
|
|
85
176
|
this.#expectedFilesTotal = filesTotal;
|
|
86
177
|
}
|
|
178
|
+
/** Called before launching a turn, after loading any resumed history. */
|
|
179
|
+
beginParentTurn(resumed = false) {
|
|
180
|
+
this.#parentTurnFilesTotal = this.#expectedFilesTotal;
|
|
181
|
+
this.#parentTurnStartKnown = this.#parentSession !== null || !resumed;
|
|
182
|
+
for (const live of this.#liveParentItems) {
|
|
183
|
+
delete live.after;
|
|
184
|
+
}
|
|
185
|
+
this.#liveParentItems.length = 0;
|
|
186
|
+
this.#liveParentEvents = new WeakMap();
|
|
187
|
+
this.#acceptedParent = null;
|
|
188
|
+
this.#parentInventory = null;
|
|
189
|
+
this.#parentReviewBefore = null;
|
|
190
|
+
// Historical logs may omit projection kinds emitted by the new turn.
|
|
191
|
+
this.#parentSession?.messageProjections.clear();
|
|
192
|
+
if (this.#parentSession !== null) {
|
|
193
|
+
this.#parentSession.messageProgressProjections.clear();
|
|
194
|
+
this.#parentSession.messageProgressOrder = 0;
|
|
195
|
+
this.#parentSession.messageProgressFrontiers = {
|
|
196
|
+
native: 0,
|
|
197
|
+
event: 0,
|
|
198
|
+
response: 0,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
// A new turn retires unresolved hypotheses from the previous turn; admitted
|
|
202
|
+
// coverage and source summaries remain available for resumed scans.
|
|
203
|
+
if (this.#parentSession !== null)
|
|
204
|
+
this.#parentSession.progress.length = 0;
|
|
205
|
+
for (const completion of this.#parentSession?.completions ?? []) {
|
|
206
|
+
completion.item = null;
|
|
207
|
+
delete completion.kind;
|
|
208
|
+
delete completion.lastCommandProgress;
|
|
209
|
+
delete completion.projection;
|
|
210
|
+
}
|
|
211
|
+
this.#readyParentMarkers.clear();
|
|
212
|
+
this.#matchedParentItems = 0;
|
|
213
|
+
this.#parentCompletionCursor = this.#parentSession?.completions.length ?? 0;
|
|
214
|
+
this.#indexedParentCompletions = this.#parentCompletionCursor;
|
|
215
|
+
this.#indexedParentItems = 0;
|
|
216
|
+
this.#parentCompletionKeys.clear();
|
|
217
|
+
this.#parentItemKeys.clear();
|
|
218
|
+
this.#parentMatches.length = 0;
|
|
219
|
+
this.#pendingParentMatches.clear();
|
|
220
|
+
this.#liveParentProgress.clear();
|
|
221
|
+
}
|
|
222
|
+
/** Observe every supported completion so repeated output keeps its occurrence. */
|
|
223
|
+
recordParentItem(event) {
|
|
224
|
+
const item = event["item"];
|
|
225
|
+
if (event["type"] !== "item.completed" ||
|
|
226
|
+
!isRecord(item) ||
|
|
227
|
+
(item["type"] !== "command_execution" &&
|
|
228
|
+
item["type"] !== "agent_message") ||
|
|
229
|
+
this.#liveParentEvents.has(event))
|
|
230
|
+
return;
|
|
231
|
+
const live = {
|
|
232
|
+
markers: [],
|
|
233
|
+
item: streamedProgressItem(event),
|
|
234
|
+
position: null,
|
|
235
|
+
};
|
|
236
|
+
this.#liveParentItems.push(live);
|
|
237
|
+
this.#liveParentEvents.set(event, live);
|
|
238
|
+
this.#matchParentCompletions();
|
|
239
|
+
}
|
|
240
|
+
/** Observe source identity before applying display/count guards. */
|
|
241
|
+
recordParentProgress(progress, event) {
|
|
242
|
+
this.recordParentItem(event);
|
|
243
|
+
const live = this.#liveParentEvents.get(event);
|
|
244
|
+
if (live === undefined)
|
|
245
|
+
return true;
|
|
246
|
+
// Parsed rows were observed before this live marker even when their
|
|
247
|
+
// file.close is still pending. Do not give that old snapshot a later order.
|
|
248
|
+
for (const session of this.#sessions.values())
|
|
249
|
+
if (session.threadId === this.#threadId)
|
|
250
|
+
for (const saved of session.progress)
|
|
251
|
+
saved.order ??= ++this.#progressOrder;
|
|
252
|
+
const marker = {
|
|
253
|
+
progress,
|
|
254
|
+
index: live.markers.length,
|
|
255
|
+
order: ++this.#progressOrder,
|
|
256
|
+
accepted: false,
|
|
257
|
+
};
|
|
258
|
+
live.markers.push(marker);
|
|
259
|
+
if (isInventoryProgress(progress))
|
|
260
|
+
this.#parentInventory = marker;
|
|
261
|
+
if (live.position !== null)
|
|
262
|
+
this.#readyParentMarkers.set(marker, live);
|
|
263
|
+
const current = isInventoryProgress(progress)
|
|
264
|
+
? this.#retainedInventory()
|
|
265
|
+
: this.#parentFacts.get(progress.filesTotal);
|
|
266
|
+
return (live.position === null ||
|
|
267
|
+
this.#afterParentFrontier(live.position, marker.index) ||
|
|
268
|
+
// Polling can admit this completion before live delivery. Its live copy
|
|
269
|
+
// restores progress after worker output or an unmatched live completion;
|
|
270
|
+
// older proved positions and earlier markers remain stale.
|
|
271
|
+
((this.#displayedProgress?.owner === "worker" ||
|
|
272
|
+
(this.#displayedProgress?.owner === "live" &&
|
|
273
|
+
this.#displayedProgress.position === undefined)) &&
|
|
274
|
+
((live.position === this.#parentFrontier.position &&
|
|
275
|
+
marker.index === this.#parentFrontier.markerIndex) ||
|
|
276
|
+
(current?.position === this.#parentFrontier.position &&
|
|
277
|
+
current.markerIndex === this.#parentFrontier.markerIndex &&
|
|
278
|
+
progressKey(current.progress) === progressKey(progress)))));
|
|
279
|
+
}
|
|
280
|
+
/** Source acceptance is independent of duplicate observer notifications. */
|
|
281
|
+
deferParentInventory(event) {
|
|
282
|
+
const marker = this.#liveParentEvents.get(event)?.markers.at(-1);
|
|
283
|
+
// Parent inventory can invalidate provisional worker coverage. Reconcile
|
|
284
|
+
// it before displaying anything, so still-eligible coverage never drops.
|
|
285
|
+
if (marker !== undefined &&
|
|
286
|
+
marker === this.#parentInventory &&
|
|
287
|
+
(!this.#parentIsEligible(this.#parentProgress) ||
|
|
288
|
+
this.#parentProgress.progress.filesCompleted === 0))
|
|
289
|
+
marker.accepted = true;
|
|
290
|
+
}
|
|
291
|
+
acceptParentProgress(progress, event) {
|
|
292
|
+
const live = this.#liveParentEvents.get(event);
|
|
293
|
+
if (live === undefined)
|
|
294
|
+
return;
|
|
295
|
+
const marker = live.markers.at(-1);
|
|
296
|
+
if (marker === undefined)
|
|
297
|
+
return;
|
|
298
|
+
marker.accepted = true;
|
|
299
|
+
if (progress.filesCompleted > 0)
|
|
300
|
+
live.reviewMarker ??= marker.index;
|
|
301
|
+
this.#acceptedParent = live;
|
|
302
|
+
const provenance = {
|
|
303
|
+
order: marker.order,
|
|
304
|
+
live,
|
|
305
|
+
markerIndex: marker.index,
|
|
306
|
+
...(live.position === null ? {} : { position: live.position }),
|
|
307
|
+
};
|
|
308
|
+
this.#parentProgress = { progress, ...provenance };
|
|
309
|
+
this.#rememberParentFact(this.#parentProgress);
|
|
310
|
+
const update = {
|
|
311
|
+
...progress,
|
|
312
|
+
filesTotal: this.#expectedFilesTotal ?? progress.filesTotal,
|
|
313
|
+
};
|
|
314
|
+
this.#displayedProgress = { update, owner: "live", ...provenance };
|
|
315
|
+
this.#highestFilesCompleted = Math.max(this.#highestFilesCompleted, progress.filesCompleted);
|
|
316
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
317
|
+
this.#recordParentBoundary(progress, live.position ?? undefined, marker.index, true, undefined, live);
|
|
318
|
+
if (live.position !== null)
|
|
319
|
+
this.#advanceParentFrontier(live.position, marker.index);
|
|
320
|
+
}
|
|
321
|
+
#rememberParentFact(candidate) {
|
|
322
|
+
// Unbound live markers stay in the existing completion queue until their
|
|
323
|
+
// saved occurrence resolves. Inventory markers have their own frontier.
|
|
324
|
+
if (candidate.position === undefined ||
|
|
325
|
+
!this.#parentSourceIsEligible(candidate) ||
|
|
326
|
+
isInventoryProgress(candidate.progress))
|
|
327
|
+
return;
|
|
328
|
+
const total = candidate.progress.filesTotal;
|
|
329
|
+
const previous = this.#parentFacts.get(total);
|
|
330
|
+
if (previous === undefined ||
|
|
331
|
+
!this.#parentSourceIsEligible(previous) ||
|
|
332
|
+
candidate.progress.filesCompleted > previous.progress.filesCompleted ||
|
|
333
|
+
(candidate.progress.filesCompleted === previous.progress.filesCompleted &&
|
|
334
|
+
compareProgressOrder(candidate, previous) > 0))
|
|
335
|
+
this.#parentFacts.set(total, candidate);
|
|
336
|
+
}
|
|
337
|
+
#parentSourceIsEligible(candidate) {
|
|
338
|
+
const marker = candidate.live?.markers[candidate.markerIndex ?? 0];
|
|
339
|
+
return (
|
|
340
|
+
// Inventory establishes scope; only coverage is bounded by its prefix.
|
|
341
|
+
isInventoryProgress(candidate.progress) ||
|
|
342
|
+
candidate.progress.filesTotal <=
|
|
343
|
+
(candidate.source?.scopeFilesTotal ??
|
|
344
|
+
marker?.scopeFilesTotal ??
|
|
345
|
+
Number.MAX_SAFE_INTEGER));
|
|
346
|
+
}
|
|
347
|
+
#parentIsEligible(candidate) {
|
|
348
|
+
return (candidate !== null &&
|
|
349
|
+
this.#parentSourceIsEligible(candidate) &&
|
|
350
|
+
candidate.progress.filesTotal <=
|
|
351
|
+
(this.#expectedFilesTotal ?? Number.MAX_SAFE_INTEGER));
|
|
352
|
+
}
|
|
353
|
+
#parentInventoryWitness() {
|
|
354
|
+
const accepted = this.#acceptedParent;
|
|
355
|
+
if (accepted === null)
|
|
356
|
+
return undefined;
|
|
357
|
+
let witness;
|
|
358
|
+
// Compute one proof for the accepted live prefix. Later unpositioned items
|
|
359
|
+
// keep their predecessors' ordering evidence without gaining a position.
|
|
360
|
+
for (const live of this.#liveParentItems) {
|
|
361
|
+
for (const marker of live.markers) {
|
|
362
|
+
if (isInventoryProgress(marker.progress))
|
|
363
|
+
continue;
|
|
364
|
+
const position = live.position ?? marker.projection?.position;
|
|
365
|
+
if (position === undefined)
|
|
366
|
+
continue;
|
|
367
|
+
const candidate = {
|
|
368
|
+
position,
|
|
369
|
+
markerIndex: live.position === null
|
|
370
|
+
? marker.projection.markerIndex
|
|
371
|
+
: marker.index,
|
|
372
|
+
order: marker.order,
|
|
373
|
+
live,
|
|
374
|
+
};
|
|
375
|
+
if (witness === undefined ||
|
|
376
|
+
(compareSourceOrder(candidate, witness) ?? 0) > 0)
|
|
377
|
+
witness = candidate;
|
|
378
|
+
}
|
|
379
|
+
if (live === accepted)
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
return witness;
|
|
383
|
+
}
|
|
384
|
+
#retainedInventory() {
|
|
385
|
+
const { progress, ...source } = this.#inventoryFrontier;
|
|
386
|
+
return progress === undefined ? undefined : { progress, ...source };
|
|
387
|
+
}
|
|
388
|
+
#acceptedInventory() {
|
|
389
|
+
let accepted;
|
|
390
|
+
let precedingAnchor;
|
|
391
|
+
for (const live of this.#liveParentItems) {
|
|
392
|
+
for (const marker of live.markers) {
|
|
393
|
+
if (!marker.accepted ||
|
|
394
|
+
!isInventoryProgress(marker.progress) ||
|
|
395
|
+
(live.position !== null &&
|
|
396
|
+
!this.#inventoryPrecedesReview(live.position, marker.index, live)) ||
|
|
397
|
+
// An unresolved command follows the whole preceding exact completion.
|
|
398
|
+
// That lower bound can exclude it without assigning it a position.
|
|
399
|
+
(live.position === null &&
|
|
400
|
+
precedingAnchor !== undefined &&
|
|
401
|
+
!this.#inventoryPrecedesReview(precedingAnchor.position, precedingAnchor.markers.length)))
|
|
402
|
+
continue;
|
|
403
|
+
const candidate = {
|
|
404
|
+
progress: marker.progress,
|
|
405
|
+
marker,
|
|
406
|
+
live,
|
|
407
|
+
position: live.position ?? undefined,
|
|
408
|
+
markerIndex: marker.index,
|
|
409
|
+
order: marker.order,
|
|
410
|
+
};
|
|
411
|
+
if ((this.#inventoryReviewOrder(candidate) ?? -1) >= 0)
|
|
412
|
+
continue;
|
|
413
|
+
if (accepted === undefined ||
|
|
414
|
+
compareProgressOrder(candidate, accepted) > 0)
|
|
415
|
+
accepted = candidate;
|
|
416
|
+
}
|
|
417
|
+
if (live.position !== null)
|
|
418
|
+
precedingAnchor = live;
|
|
419
|
+
}
|
|
420
|
+
return accepted;
|
|
421
|
+
}
|
|
422
|
+
#savedInventorySupersedes(saved, live, native = this.#parentSession?.completions.some((completion) => completion.kind === "command")) {
|
|
423
|
+
const source = this.#inventorySourceOrder(saved, live);
|
|
424
|
+
if (source !== undefined) {
|
|
425
|
+
return source >= 0;
|
|
426
|
+
}
|
|
427
|
+
else if (live.live.after === undefined && !native) {
|
|
428
|
+
// Older logs omit command completions. Their later phase receipts still
|
|
429
|
+
// provide the existing best-effort evidence for a saved inventory.
|
|
430
|
+
const phase = this.#parentFacts.get(saved.progress.filesTotal);
|
|
431
|
+
const row = this.#parentSession?.progress.find((row) => row.position === saved.position &&
|
|
432
|
+
row.markerIndex === saved.markerIndex);
|
|
433
|
+
const evidence = phase !== undefined && (compareSourceOrder(phase, saved) ?? 0) > 0
|
|
434
|
+
? phase
|
|
435
|
+
: row === undefined || !this.#unboundParentCopy(row)
|
|
436
|
+
? saved
|
|
437
|
+
: undefined;
|
|
438
|
+
if (evidence !== undefined && compareProgressOrder(evidence, live) >= 0) {
|
|
439
|
+
// Keep admitted relative authority when a later anchor only narrows
|
|
440
|
+
// an unknown occurrence. Exact source order can still supersede it.
|
|
441
|
+
saved.afterLive = live.live;
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
#inventorySourceOrder(saved, live) {
|
|
448
|
+
const source = compareSourceOrder(saved, live);
|
|
449
|
+
if (source !== undefined)
|
|
450
|
+
return source;
|
|
451
|
+
const projection = saved.source?.projection;
|
|
452
|
+
const marker = projection?.markers[saved.markerIndex];
|
|
453
|
+
// A complete bounded projection preserves live inventory order without
|
|
454
|
+
// identifying the saved occurrence. Count rejection cannot erase that proof.
|
|
455
|
+
if (projection !== undefined &&
|
|
456
|
+
marker !== undefined &&
|
|
457
|
+
progressKey(marker.progress) === progressKey(saved.progress))
|
|
458
|
+
return (compareSourceOrder({ order: marker.order, markerIndex: marker.index, live: projection }, live) ?? marker.order - live.order);
|
|
459
|
+
return saved.afterLive === live.live ? 1 : undefined;
|
|
460
|
+
}
|
|
461
|
+
#projectParentInventory() {
|
|
462
|
+
const saved = this.#retainedInventory();
|
|
463
|
+
const live = this.#acceptedInventory();
|
|
464
|
+
const selected = saved !== undefined &&
|
|
465
|
+
(live === undefined || this.#savedInventorySupersedes(saved, live))
|
|
466
|
+
? saved
|
|
467
|
+
: live;
|
|
468
|
+
if (selected === saved && saved !== undefined) {
|
|
469
|
+
if (saved.source !== undefined)
|
|
470
|
+
saved.source.inventoryAdmitted = true;
|
|
471
|
+
if (saved.afterLive !== undefined)
|
|
472
|
+
this.#inventoryFrontier.afterLive = saved.afterLive;
|
|
473
|
+
}
|
|
474
|
+
const invalidated = selected === undefined &&
|
|
475
|
+
this.#liveParentItems.some((item) => item.markers.some((marker) => marker.accepted && isInventoryProgress(marker.progress)));
|
|
476
|
+
// Empty source history does not override a caller's explicit scope update.
|
|
477
|
+
// Roll back only when observed inventory authority has lost eligibility.
|
|
478
|
+
const filesTotal = selected?.progress.filesTotal ??
|
|
479
|
+
(invalidated ? this.#parentTurnFilesTotal : undefined);
|
|
480
|
+
if (filesTotal !== undefined) {
|
|
481
|
+
// Scope recovered here has not necessarily reached the API, including
|
|
482
|
+
// its first assignment when registration supplied no file count.
|
|
483
|
+
if (filesTotal !== this.#expectedFilesTotal)
|
|
484
|
+
this.#inventoryNeedsReconciliation = true;
|
|
485
|
+
this.setExpectedFilesTotal(filesTotal);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
#recordParentBoundary(progress, position, markerIndex, admitted = false, filesTotal, projection) {
|
|
489
|
+
let reviewed = progress.filesCompleted > 0;
|
|
490
|
+
filesTotal ??= projection?.markers[markerIndex]?.scopeFilesTotal;
|
|
491
|
+
if (position === undefined) {
|
|
492
|
+
if (reviewed &&
|
|
493
|
+
admitted &&
|
|
494
|
+
projection?.after !== undefined &&
|
|
495
|
+
progress.filesTotal <= (filesTotal ?? Number.MAX_SAFE_INTEGER)) {
|
|
496
|
+
const marker = projection.markers[markerIndex];
|
|
497
|
+
const previous = this.#parentReviewBefore;
|
|
498
|
+
if (previous === null ||
|
|
499
|
+
projection.after < previous.live.after ||
|
|
500
|
+
(projection.after === previous.live.after &&
|
|
501
|
+
marker.order < previous.order))
|
|
502
|
+
this.#parentReviewBefore = {
|
|
503
|
+
order: marker.order,
|
|
504
|
+
markerIndex,
|
|
505
|
+
live: projection,
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
// Eligible source progress can start review without winning the display.
|
|
511
|
+
// Merely locating a rejected oversized marker cannot fix that cutoff.
|
|
512
|
+
if (reviewed &&
|
|
513
|
+
progress.filesTotal > (filesTotal ?? Number.MAX_SAFE_INTEGER)) {
|
|
514
|
+
const boundary = this.#parentInventoryBoundary;
|
|
515
|
+
if (boundary?.reviewed === true &&
|
|
516
|
+
boundary.position === position &&
|
|
517
|
+
boundary.markerIndex === markerIndex)
|
|
518
|
+
// Retire the contradicted cutoff while preserving its source position
|
|
519
|
+
// as an ordering witness for preceding inventory.
|
|
520
|
+
boundary.reviewed = false;
|
|
521
|
+
if (projection === undefined)
|
|
522
|
+
return;
|
|
523
|
+
// A bounded current completion still orders its preceding inventory.
|
|
524
|
+
// Scope-ineligible progress supplies that witness, not a reviewed cutoff.
|
|
525
|
+
reviewed = false;
|
|
526
|
+
}
|
|
527
|
+
if (isInventoryProgress(progress))
|
|
528
|
+
return;
|
|
529
|
+
const boundary = this.#parentInventoryBoundary;
|
|
530
|
+
const order = boundary === null
|
|
531
|
+
? 0
|
|
532
|
+
: position - boundary.position || markerIndex - boundary.markerIndex;
|
|
533
|
+
// Before review starts, each later phase can establish its preceding
|
|
534
|
+
// inventory. Once coverage starts, preserve the first reviewed boundary.
|
|
535
|
+
if (boundary === null ||
|
|
536
|
+
(reviewed
|
|
537
|
+
? !boundary.reviewed || order < 0
|
|
538
|
+
: !boundary.reviewed && order > 0))
|
|
539
|
+
this.#parentInventoryBoundary = {
|
|
540
|
+
position,
|
|
541
|
+
markerIndex,
|
|
542
|
+
reviewed,
|
|
543
|
+
projection: projection === undefined
|
|
544
|
+
? reviewed
|
|
545
|
+
? undefined
|
|
546
|
+
: boundary?.projection
|
|
547
|
+
: {
|
|
548
|
+
position,
|
|
549
|
+
markerIndex,
|
|
550
|
+
order: projection.markers[markerIndex].order,
|
|
551
|
+
live: projection,
|
|
552
|
+
},
|
|
553
|
+
};
|
|
554
|
+
else if (order === 0 && projection !== undefined)
|
|
555
|
+
boundary.projection = {
|
|
556
|
+
position,
|
|
557
|
+
markerIndex,
|
|
558
|
+
order: projection.markers[markerIndex].order,
|
|
559
|
+
live: projection,
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
#recordParentSourceBoundaries(command) {
|
|
563
|
+
const session = this.#parentSession;
|
|
564
|
+
if (session === null)
|
|
565
|
+
return;
|
|
566
|
+
const native = session.completions.some((completion) => completion.kind === "command");
|
|
567
|
+
const projectedItems = new Set();
|
|
568
|
+
for (const completion of session.completions)
|
|
569
|
+
if (completion.projection !== undefined)
|
|
570
|
+
projectedItems.add(completion.projection);
|
|
571
|
+
this.#parentReviewBefore = null;
|
|
572
|
+
for (const live of this.#liveParentItems) {
|
|
573
|
+
for (const marker of live.markers)
|
|
574
|
+
delete marker.projection;
|
|
575
|
+
if (live.position === null &&
|
|
576
|
+
live.after !== undefined &&
|
|
577
|
+
!projectedItems.has(live))
|
|
578
|
+
// Complete projections get their eligibility from the ordered rows
|
|
579
|
+
// below. Only omitted admitted reviews contribute this upper bound.
|
|
580
|
+
for (const marker of live.markers)
|
|
581
|
+
if (marker.accepted && marker.progress.filesCompleted > 0)
|
|
582
|
+
this.#recordParentBoundary(marker.progress, undefined, marker.index, true, undefined, live);
|
|
583
|
+
}
|
|
584
|
+
const authority = this.#acceptedInventory();
|
|
585
|
+
// Legacy logs without command positions keep the established live scope.
|
|
586
|
+
// An old copied preview cannot replace it merely by being read later.
|
|
587
|
+
let filesTotal = !native &&
|
|
588
|
+
authority?.live.position === null &&
|
|
589
|
+
authority.live.after === undefined
|
|
590
|
+
? authority.progress.filesTotal
|
|
591
|
+
: this.#parentTurnFilesTotal;
|
|
592
|
+
let inventory;
|
|
593
|
+
let savedInventory;
|
|
594
|
+
const scopeAt = (candidate, completion) => {
|
|
595
|
+
if (!native ||
|
|
596
|
+
inventory !== undefined ||
|
|
597
|
+
authority?.live.position !== null ||
|
|
598
|
+
Number.isFinite(authority.live.after))
|
|
599
|
+
return filesTotal;
|
|
600
|
+
if (candidate.markerIndex < authority.marker.index &&
|
|
601
|
+
completion?.item?.kind === "command" &&
|
|
602
|
+
completion.item.outputDigest === authority.live.item?.outputDigest)
|
|
603
|
+
// Complete output agreement proves marker layout, not occurrence.
|
|
604
|
+
// A command's later inventory cannot justify its own earlier coverage.
|
|
605
|
+
return filesTotal;
|
|
606
|
+
// An initial estimate is not source proof against an admitted inventory
|
|
607
|
+
// missing from persistence. Keep it for rows known or observed before
|
|
608
|
+
// that live authority; unresolved later rows retain the admitted scope.
|
|
609
|
+
const order = this.#inventorySourceOrder(candidate, authority) ??
|
|
610
|
+
(candidate.order === 0 ? 1 : candidate.order - authority.order);
|
|
611
|
+
return order < 0 ? filesTotal : authority.progress.filesTotal;
|
|
612
|
+
};
|
|
613
|
+
let liveIndex = 0;
|
|
614
|
+
const applyBounds = (position) => {
|
|
615
|
+
while (liveIndex < this.#liveParentItems.length) {
|
|
616
|
+
const live = this.#liveParentItems[liveIndex];
|
|
617
|
+
if (live.position !== null || live.after === undefined) {
|
|
618
|
+
liveIndex++;
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (live.after > position)
|
|
622
|
+
break;
|
|
623
|
+
liveIndex++;
|
|
624
|
+
// Every marker remains available. A rejected trailing zero must not
|
|
625
|
+
// replace inventory whose order still proves it precedes review.
|
|
626
|
+
for (const marker of live.markers) {
|
|
627
|
+
if (!isInventoryProgress(marker.progress) ||
|
|
628
|
+
// A bound orders a lossy inventory but does not admit its scope.
|
|
629
|
+
// Unaccepted inventory needs a complete projection or corroboration.
|
|
630
|
+
(!marker.accepted && !projectedItems.has(live)))
|
|
631
|
+
continue;
|
|
632
|
+
const bounded = {
|
|
633
|
+
progress: marker.progress,
|
|
634
|
+
marker,
|
|
635
|
+
live,
|
|
636
|
+
markerIndex: marker.index,
|
|
637
|
+
order: marker.order,
|
|
638
|
+
};
|
|
639
|
+
if (
|
|
640
|
+
// An upper bound does not place this inventory at the anchor.
|
|
641
|
+
// Unknown relative order cannot revoke already admitted scope.
|
|
642
|
+
(this.#inventoryReviewOrder(bounded) ?? -1) < 0 &&
|
|
643
|
+
(inventory === undefined ||
|
|
644
|
+
("marker" in inventory
|
|
645
|
+
? compareProgressOrder(inventory, bounded) < 0
|
|
646
|
+
: !this.#savedInventorySupersedes(inventory, bounded, native)))) {
|
|
647
|
+
inventory = bounded;
|
|
648
|
+
filesTotal = bounded.progress.filesTotal;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
const recordInventory = (candidate) => {
|
|
654
|
+
const retained = this.#retainedInventory();
|
|
655
|
+
inventory =
|
|
656
|
+
retained?.position === candidate.position &&
|
|
657
|
+
retained.markerIndex === candidate.markerIndex
|
|
658
|
+
? retained
|
|
659
|
+
: candidate;
|
|
660
|
+
savedInventory = inventory;
|
|
661
|
+
filesTotal = candidate.progress.filesTotal;
|
|
662
|
+
};
|
|
663
|
+
let completionIndex = 0;
|
|
664
|
+
const remainingMarkers = (completion) => {
|
|
665
|
+
applyBounds(completion.position);
|
|
666
|
+
const live = completion.projection;
|
|
667
|
+
if (completion.kind !== "command" ||
|
|
668
|
+
live?.position !== completion.position)
|
|
669
|
+
return;
|
|
670
|
+
// Exact command identity also locates markers omitted by persistence.
|
|
671
|
+
// Finish that command before examining the next saved source position.
|
|
672
|
+
for (let index = (completion.lastCommandProgress?.markerIndex ?? -1) + 1; index < live.markers.length; index++) {
|
|
673
|
+
const marker = live.markers[index];
|
|
674
|
+
const candidate = {
|
|
675
|
+
progress: marker.progress,
|
|
676
|
+
position: completion.position,
|
|
677
|
+
markerIndex: marker.index,
|
|
678
|
+
order: marker.order,
|
|
679
|
+
live,
|
|
680
|
+
};
|
|
681
|
+
marker.scopeFilesTotal = scopeAt(candidate, completion);
|
|
682
|
+
if (isInventoryProgress(marker.progress)) {
|
|
683
|
+
if (this.#inventoryPrecedesReview(completion.position, marker.index, live))
|
|
684
|
+
recordInventory(candidate);
|
|
685
|
+
}
|
|
686
|
+
else
|
|
687
|
+
this.#recordParentBoundary(marker.progress, completion.position, marker.index, marker.accepted, marker.scopeFilesTotal);
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
for (const saved of session.progress) {
|
|
691
|
+
while (completionIndex < session.completions.length &&
|
|
692
|
+
session.completions[completionIndex].position < saved.position)
|
|
693
|
+
remainingMarkers(session.completions[completionIndex++]);
|
|
694
|
+
const completion = session.completions[completionIndex];
|
|
695
|
+
applyBounds(saved.position);
|
|
696
|
+
saved.projection =
|
|
697
|
+
completion?.position === saved.position
|
|
698
|
+
? completion.projection
|
|
699
|
+
: undefined;
|
|
700
|
+
const projected = saved.projection?.markers[saved.markerIndex];
|
|
701
|
+
if (saved.duplicate)
|
|
702
|
+
continue;
|
|
703
|
+
const candidate = savedProgress(saved);
|
|
704
|
+
const progress = candidate.progress;
|
|
705
|
+
saved.scopeFilesTotal = scopeAt(candidate, completion?.position === saved.position ? completion : undefined);
|
|
706
|
+
const exact = completion?.projection?.position === saved.position;
|
|
707
|
+
const nativeOccurrence = this.#parentTurnStartKnown && saved.completion;
|
|
708
|
+
if (projected !== undefined) {
|
|
709
|
+
projected.scopeFilesTotal = saved.scopeFilesTotal;
|
|
710
|
+
if (progressKey(projected.progress) === progressKey(progress))
|
|
711
|
+
projected.projection = {
|
|
712
|
+
position: saved.position,
|
|
713
|
+
markerIndex: saved.markerIndex,
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
if (isInventoryProgress(progress)) {
|
|
717
|
+
// Source eligibility follows the inventory at this position, not the
|
|
718
|
+
// display scope at the start or end of a poll. The retained rows make
|
|
719
|
+
// this ordered pass identical for every partition of the same input.
|
|
720
|
+
if (this.#inventoryPrecedesReview(saved.position, saved.markerIndex, saved.projection, saved)) {
|
|
721
|
+
if (exact || nativeOccurrence || !this.#unboundParentCopy(saved))
|
|
722
|
+
recordInventory(candidate);
|
|
723
|
+
else if (authority !== undefined &&
|
|
724
|
+
progressKey(progress) === progressKey(authority.progress) &&
|
|
725
|
+
(compareSourceOrder(candidate, authority) ?? 0) >= 0) {
|
|
726
|
+
// A duplicate can restate the admitted legacy scope without proving
|
|
727
|
+
// an occurrence or becoming eligible for another notification.
|
|
728
|
+
inventory = authority;
|
|
729
|
+
filesTotal = authority.progress.filesTotal;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
if (projected !== undefined ||
|
|
735
|
+
// Native current-turn review rows have their own source evidence. Their
|
|
736
|
+
// eligibility does not depend on the provisional display frontier.
|
|
737
|
+
(nativeOccurrence && progress.filesCompleted > 0) ||
|
|
738
|
+
(this.#savedParentProgressIsCurrent(saved) &&
|
|
739
|
+
(command === undefined ||
|
|
740
|
+
command.live !== this.#acceptedParent ||
|
|
741
|
+
command.live.position !== null ||
|
|
742
|
+
saved.position >= command.after)) ||
|
|
743
|
+
(!saved.receipt &&
|
|
744
|
+
progress.filesCompleted > 0 &&
|
|
745
|
+
this.#liveParentProgress.has(progressKey(progress))))
|
|
746
|
+
this.#recordParentBoundary(progress, saved.position, saved.markerIndex, exact && (projected?.accepted ?? false), saved.scopeFilesTotal, projected === undefined ? undefined : completion.projection);
|
|
747
|
+
}
|
|
748
|
+
while (completionIndex < session.completions.length)
|
|
749
|
+
remainingMarkers(session.completions[completionIndex++]);
|
|
750
|
+
return savedInventory;
|
|
751
|
+
}
|
|
752
|
+
#precedesParentBoundary(position, markerIndex) {
|
|
753
|
+
const first = this.#parentInventoryBoundary;
|
|
754
|
+
return (first !== null &&
|
|
755
|
+
(position < first.position ||
|
|
756
|
+
(position === first.position && markerIndex < first.markerIndex)));
|
|
757
|
+
}
|
|
758
|
+
#inventoryAllowedInCompletion(live, markerIndex) {
|
|
759
|
+
return (live?.reviewMarker === undefined ||
|
|
760
|
+
markerIndex < live.reviewMarker ||
|
|
761
|
+
live.markers[markerIndex]?.accepted !== false);
|
|
762
|
+
}
|
|
763
|
+
#inventoryPrecedesReview(position, markerIndex, live, source) {
|
|
764
|
+
return ((source?.inventoryAdmitted === true ||
|
|
765
|
+
this.#inventoryAllowedInCompletion(live, markerIndex)) &&
|
|
766
|
+
position < (this.#parentReviewBefore?.live.after ?? Infinity) &&
|
|
767
|
+
(this.#parentInventoryBoundary?.reviewed !== true ||
|
|
768
|
+
this.#precedesParentBoundary(position, markerIndex)));
|
|
769
|
+
}
|
|
770
|
+
#inventoryReviewOrder(inventory) {
|
|
771
|
+
const projection = inventory.live?.markers[inventory.markerIndex ?? 0]?.projection;
|
|
772
|
+
if (
|
|
773
|
+
// A complete projection can exclude this inventory even though its live
|
|
774
|
+
// occurrence remains unknown. A mere upper bound cannot do so.
|
|
775
|
+
(projection !== undefined &&
|
|
776
|
+
!this.#inventoryPrecedesReview(projection.position, projection.markerIndex)) ||
|
|
777
|
+
!this.#inventoryAllowedInCompletion(inventory.live, inventory.markerIndex ?? 0))
|
|
778
|
+
return 1;
|
|
779
|
+
const boundary = this.#parentInventoryBoundary;
|
|
780
|
+
const reviews = [];
|
|
781
|
+
if (boundary?.reviewed === true)
|
|
782
|
+
reviews.push({
|
|
783
|
+
position: boundary.position,
|
|
784
|
+
markerIndex: boundary.markerIndex,
|
|
785
|
+
live: boundary.projection?.live,
|
|
786
|
+
order: boundary.projection?.order ?? 0,
|
|
787
|
+
});
|
|
788
|
+
if (this.#parentReviewBefore !== null)
|
|
789
|
+
reviews.push(this.#parentReviewBefore);
|
|
790
|
+
let order = -1;
|
|
791
|
+
for (const review of reviews) {
|
|
792
|
+
const relation = compareSourceOrder(inventory, review) ??
|
|
793
|
+
// A resolved projection or bounded omitted review preserves the live
|
|
794
|
+
// sequence, including marker order within one command. It does not
|
|
795
|
+
// assign either completion an otherwise unknown saved occurrence.
|
|
796
|
+
(inventory.live !== undefined && review.live !== undefined
|
|
797
|
+
? inventory.order - review.order
|
|
798
|
+
: undefined);
|
|
799
|
+
if (relation !== undefined && relation >= 0)
|
|
800
|
+
return relation;
|
|
801
|
+
if (relation === undefined)
|
|
802
|
+
order = undefined;
|
|
803
|
+
}
|
|
804
|
+
return order;
|
|
805
|
+
}
|
|
806
|
+
#afterParentFrontier(position, index) {
|
|
807
|
+
return (position > this.#parentFrontier.position ||
|
|
808
|
+
(position === this.#parentFrontier.position &&
|
|
809
|
+
index > this.#parentFrontier.markerIndex));
|
|
810
|
+
}
|
|
811
|
+
#advanceParentFrontier(position, markerIndex) {
|
|
812
|
+
if (this.#afterParentFrontier(position, markerIndex))
|
|
813
|
+
this.#parentFrontier = { position, markerIndex };
|
|
814
|
+
}
|
|
815
|
+
#unresolvedParentCommand() {
|
|
816
|
+
const live = this.#acceptedParent;
|
|
817
|
+
if (live?.position !== null ||
|
|
818
|
+
live.after === undefined ||
|
|
819
|
+
!live.markers.some((marker) => marker.accepted))
|
|
820
|
+
return undefined;
|
|
821
|
+
return { live, after: live.after };
|
|
822
|
+
}
|
|
823
|
+
#boundParentCommands(session, start = 0, end = this.#liveParentItems.length, completionIndex = 0) {
|
|
824
|
+
// Exact-only intervals require neither source scans nor alias indexes.
|
|
825
|
+
while (start < end && this.#liveParentItems[start].position !== null)
|
|
826
|
+
start++;
|
|
827
|
+
if (start === end ||
|
|
828
|
+
!this.#parentTurnStartKnown ||
|
|
829
|
+
!session.completions.some((completion) => completion.kind === "command"))
|
|
830
|
+
return;
|
|
831
|
+
while (start < end) {
|
|
832
|
+
if (this.#liveParentItems[start].position !== null) {
|
|
833
|
+
start++;
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
let stop = start;
|
|
837
|
+
while (stop < end && this.#liveParentItems[stop].position === null)
|
|
838
|
+
stop++;
|
|
839
|
+
const before = this.#liveParentItems[start - 1]?.position ?? 0;
|
|
840
|
+
const after = this.#liveParentItems[stop]?.position ?? Infinity;
|
|
841
|
+
const saved = new Map();
|
|
842
|
+
while (completionIndex < session.completions.length &&
|
|
843
|
+
session.completions[completionIndex].position < after) {
|
|
844
|
+
const completion = session.completions[completionIndex++];
|
|
845
|
+
if (completion.position <= before ||
|
|
846
|
+
completion.item?.kind !== "command")
|
|
847
|
+
continue;
|
|
848
|
+
// Bounded projections are derived from this interval, not occurrences.
|
|
849
|
+
// Rebuild them when a later completion provides a tighter bound.
|
|
850
|
+
if (completion.projection?.position === null)
|
|
851
|
+
delete completion.projection;
|
|
852
|
+
const key = completion.item.outputDigest;
|
|
853
|
+
const occurrences = saved.get(key);
|
|
854
|
+
if (occurrences === undefined)
|
|
855
|
+
saved.set(key, [completion]);
|
|
856
|
+
else
|
|
857
|
+
occurrences.push(completion);
|
|
858
|
+
}
|
|
859
|
+
const observed = new Map();
|
|
860
|
+
const groups = new Map();
|
|
861
|
+
for (let index = start; index < stop; index++) {
|
|
862
|
+
const live = this.#liveParentItems[index];
|
|
863
|
+
if (live.item?.kind !== "command")
|
|
864
|
+
continue;
|
|
865
|
+
const keys = [
|
|
866
|
+
...new Set([live.item.outputDigest, live.item.persistedOutputDigest]),
|
|
867
|
+
]
|
|
868
|
+
.filter((key) => saved.has(key))
|
|
869
|
+
.sort();
|
|
870
|
+
for (const key of keys) {
|
|
871
|
+
const occurrences = observed.get(key);
|
|
872
|
+
if (occurrences === undefined)
|
|
873
|
+
observed.set(key, [index]);
|
|
874
|
+
else
|
|
875
|
+
occurrences.push(index);
|
|
876
|
+
}
|
|
877
|
+
if (live.markers.length === 0)
|
|
878
|
+
continue;
|
|
879
|
+
const key = keys.join(":");
|
|
880
|
+
const group = groups.get(key);
|
|
881
|
+
if (group === undefined)
|
|
882
|
+
groups.set(key, { keys, members: [index] });
|
|
883
|
+
else
|
|
884
|
+
group.members.push(index);
|
|
885
|
+
}
|
|
886
|
+
const evidence = new Map();
|
|
887
|
+
for (const { keys, members } of groups.values()) {
|
|
888
|
+
const queues = keys.map((key) => ({
|
|
889
|
+
key,
|
|
890
|
+
saved: saved.get(key),
|
|
891
|
+
observed: observed.get(key),
|
|
892
|
+
reserved: 0,
|
|
893
|
+
next: 0,
|
|
894
|
+
}));
|
|
895
|
+
let reservations = 0;
|
|
896
|
+
for (const index of members) {
|
|
897
|
+
// Reserve preceding compatible live items once for this signature
|
|
898
|
+
// group. Advancing ordered cursors is equivalent to consuming the
|
|
899
|
+
// earliest compatible item for each saved occurrence, without splice.
|
|
900
|
+
while (true) {
|
|
901
|
+
const previous = Math.min(...queues.map((queue) => queue.observed[queue.next] ?? Infinity));
|
|
902
|
+
if (previous >= index)
|
|
903
|
+
break;
|
|
904
|
+
const item = this.#liveParentItems[previous].item;
|
|
905
|
+
let selected;
|
|
906
|
+
for (const queue of queues) {
|
|
907
|
+
if (queue.observed[queue.next] === previous)
|
|
908
|
+
queue.next++;
|
|
909
|
+
if ((queue.key === item.outputDigest ||
|
|
910
|
+
queue.key === item.persistedOutputDigest) &&
|
|
911
|
+
queue.reserved < queue.saved.length &&
|
|
912
|
+
(selected === undefined ||
|
|
913
|
+
queue.saved[queue.reserved].position <
|
|
914
|
+
selected.saved[selected.reserved].position))
|
|
915
|
+
selected = queue;
|
|
916
|
+
}
|
|
917
|
+
if (selected !== undefined) {
|
|
918
|
+
selected.reserved++;
|
|
919
|
+
reservations++;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
evidence.set(index, {
|
|
923
|
+
candidates: queues.map((queue) => ({
|
|
924
|
+
saved: queue.saved,
|
|
925
|
+
first: queue.reserved,
|
|
926
|
+
})),
|
|
927
|
+
reserved: reservations > 0,
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
// Later complete projections constrain earlier compatible copies. A
|
|
932
|
+
// bound narrows the interval without assigning a live occurrence.
|
|
933
|
+
let bound = after;
|
|
934
|
+
for (let index = stop - 1; index >= start; index--) {
|
|
935
|
+
const live = this.#liveParentItems[index];
|
|
936
|
+
bound = Math.min(bound, live.after ?? Infinity);
|
|
937
|
+
const progress = live.markers.at(-1)?.progress;
|
|
938
|
+
if (live.item?.kind !== "command" || progress === undefined) {
|
|
939
|
+
if (bound !== Infinity)
|
|
940
|
+
live.after = bound;
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
const { candidates, reserved } = evidence.get(index);
|
|
944
|
+
let latest;
|
|
945
|
+
for (const { saved, first } of candidates) {
|
|
946
|
+
let lower = first;
|
|
947
|
+
let upper = saved.length;
|
|
948
|
+
while (lower < upper) {
|
|
949
|
+
const middle = Math.floor((lower + upper) / 2);
|
|
950
|
+
if (saved[middle].position < bound)
|
|
951
|
+
lower = middle + 1;
|
|
952
|
+
else
|
|
953
|
+
upper = middle;
|
|
954
|
+
}
|
|
955
|
+
const candidate = lower > first ? saved[lower - 1] : undefined;
|
|
956
|
+
if (candidate !== undefined &&
|
|
957
|
+
(latest === undefined || candidate.position > latest.position))
|
|
958
|
+
latest = candidate;
|
|
959
|
+
}
|
|
960
|
+
if (latest === undefined &&
|
|
961
|
+
!reserved &&
|
|
962
|
+
bound === Infinity &&
|
|
963
|
+
!isInventoryProgress(progress))
|
|
964
|
+
continue;
|
|
965
|
+
const visible = latest?.lastCommandProgress;
|
|
966
|
+
const fullProjection = bound !== Infinity &&
|
|
967
|
+
visible !== undefined &&
|
|
968
|
+
visible.markerIndex === live.markers.length - 1 &&
|
|
969
|
+
progressKey(visible.update) === progressKey(progress);
|
|
970
|
+
if (fullProjection)
|
|
971
|
+
latest.projection = live;
|
|
972
|
+
live.after = Math.min(live.after ?? Infinity, fullProjection ? visible.position + 1 : bound);
|
|
973
|
+
// A preceding completion ends before this projection's inclusive edge.
|
|
974
|
+
bound = Math.min(bound, fullProjection ? visible.position : live.after);
|
|
975
|
+
}
|
|
976
|
+
start = stop + 1;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
#matchParentCompletions() {
|
|
980
|
+
if (this.#parentSession === null || !this.#parentTurnStartKnown)
|
|
981
|
+
return;
|
|
982
|
+
const completions = this.#parentSession.completions;
|
|
983
|
+
// Ordinary live completions can be dropped by native queue backpressure.
|
|
984
|
+
// Match ordered signatures, not list indexes. Identical output signatures
|
|
985
|
+
// remain ambiguous; use the earliest compatible occurrence after our anchor.
|
|
986
|
+
const add = (index, key, queues) => {
|
|
987
|
+
let queue = queues.get(key);
|
|
988
|
+
if (queue === undefined) {
|
|
989
|
+
queue = { indexes: [], next: 0 };
|
|
990
|
+
queues.set(key, queue);
|
|
991
|
+
}
|
|
992
|
+
queue.indexes.push(index);
|
|
993
|
+
this.#queueParentMatch(key);
|
|
994
|
+
};
|
|
995
|
+
while (this.#indexedParentCompletions < completions.length) {
|
|
996
|
+
const index = this.#indexedParentCompletions++;
|
|
997
|
+
const item = completions[index].item;
|
|
998
|
+
if (item !== null)
|
|
999
|
+
add(index, progressItemKey(item), this.#parentCompletionKeys);
|
|
1000
|
+
}
|
|
1001
|
+
while (this.#indexedParentItems < this.#liveParentItems.length) {
|
|
1002
|
+
const index = this.#indexedParentItems++;
|
|
1003
|
+
const item = this.#liveParentItems[index].item;
|
|
1004
|
+
if (item !== null)
|
|
1005
|
+
for (const key of progressItemKeys(item))
|
|
1006
|
+
add(index, key, this.#parentItemKeys);
|
|
1007
|
+
}
|
|
1008
|
+
while (this.#parentMatches.length > 0) {
|
|
1009
|
+
const index = takeFirstIndex(this.#parentMatches);
|
|
1010
|
+
this.#pendingParentMatches.delete(index);
|
|
1011
|
+
if (index < this.#matchedParentItems)
|
|
1012
|
+
continue;
|
|
1013
|
+
const live = this.#liveParentItems[index];
|
|
1014
|
+
if (live.item === null)
|
|
1015
|
+
continue;
|
|
1016
|
+
let matched;
|
|
1017
|
+
for (const key of progressItemKeys(live.item)) {
|
|
1018
|
+
const queue = this.#parentCompletionKeys.get(key);
|
|
1019
|
+
const savedIndex = queue?.indexes[queue.next];
|
|
1020
|
+
if (savedIndex !== undefined &&
|
|
1021
|
+
(matched === undefined || savedIndex < matched))
|
|
1022
|
+
matched = savedIndex;
|
|
1023
|
+
}
|
|
1024
|
+
if (matched === undefined)
|
|
1025
|
+
continue;
|
|
1026
|
+
live.position = completions[matched].position;
|
|
1027
|
+
completions[matched].projection = live;
|
|
1028
|
+
for (const marker of live.markers)
|
|
1029
|
+
this.#readyParentMarkers.set(marker, live);
|
|
1030
|
+
this.#boundParentCommands(this.#parentSession, this.#matchedParentItems, index + 1, this.#parentCompletionCursor);
|
|
1031
|
+
const changed = new Set();
|
|
1032
|
+
const remove = (key, queues) => {
|
|
1033
|
+
const queue = queues.get(key);
|
|
1034
|
+
if (++queue.next === queue.indexes.length)
|
|
1035
|
+
queues.delete(key);
|
|
1036
|
+
changed.add(key);
|
|
1037
|
+
};
|
|
1038
|
+
// A compatible later anchor lets both prefixes advance. Without an
|
|
1039
|
+
// anchor, retain compact unmatched signatures: reads can precede writes
|
|
1040
|
+
// or native persistence can fail and later retry. Neither blocks other
|
|
1041
|
+
// compatible signatures, and each occurrence is indexed/consumed once.
|
|
1042
|
+
while (this.#matchedParentItems <= index) {
|
|
1043
|
+
const previous = this.#liveParentItems[this.#matchedParentItems++];
|
|
1044
|
+
if (previous.position === null) {
|
|
1045
|
+
if (previous.item?.kind !== "command") {
|
|
1046
|
+
previous.after = Math.min(previous.after ?? Infinity, live.position);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (previous.item !== null) {
|
|
1050
|
+
for (const key of progressItemKeys(previous.item))
|
|
1051
|
+
remove(key, this.#parentItemKeys);
|
|
1052
|
+
if (previous.position !== null)
|
|
1053
|
+
previous.item = null;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
while (this.#parentCompletionCursor <= matched) {
|
|
1057
|
+
const saved = completions[this.#parentCompletionCursor++];
|
|
1058
|
+
if (saved.item !== null) {
|
|
1059
|
+
remove(progressItemKey(saved.item), this.#parentCompletionKeys);
|
|
1060
|
+
if (saved.position === live.position) {
|
|
1061
|
+
saved.item = null;
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
for (const key of changed)
|
|
1066
|
+
this.#queueParentMatch(key);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
#queueParentMatch(key) {
|
|
1070
|
+
if (!this.#parentCompletionKeys.has(key))
|
|
1071
|
+
return;
|
|
1072
|
+
const queue = this.#parentItemKeys.get(key);
|
|
1073
|
+
const index = queue?.indexes[queue.next];
|
|
1074
|
+
if (index === undefined || this.#pendingParentMatches.has(index))
|
|
1075
|
+
return;
|
|
1076
|
+
this.#pendingParentMatches.add(index);
|
|
1077
|
+
insertIndex(this.#parentMatches, index);
|
|
1078
|
+
}
|
|
1079
|
+
#corroboratedInventory(live, marker, inventoryWitness) {
|
|
1080
|
+
if (!this.#parentTurnStartKnown ||
|
|
1081
|
+
marker.progress.filesCompleted === 0 ||
|
|
1082
|
+
live.position === null)
|
|
1083
|
+
return undefined;
|
|
1084
|
+
let inventory;
|
|
1085
|
+
let pending;
|
|
1086
|
+
for (let index = this.#liveParentItems.indexOf(live) - 1; index >= 0; index--) {
|
|
1087
|
+
const candidate = this.#liveParentItems[index];
|
|
1088
|
+
inventory = candidate.markers.findLast((observed) => (observed.accepted || observed === this.#parentInventory) &&
|
|
1089
|
+
isInventoryProgress(observed.progress) &&
|
|
1090
|
+
observed.progress.filesTotal === marker.progress.filesTotal);
|
|
1091
|
+
if (inventory !== undefined) {
|
|
1092
|
+
pending = candidate;
|
|
1093
|
+
break;
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
if (inventory === undefined ||
|
|
1097
|
+
pending === undefined ||
|
|
1098
|
+
!this.#inventoryAllowedInCompletion(pending, inventory.index))
|
|
1099
|
+
return undefined;
|
|
1100
|
+
let position = pending.position;
|
|
1101
|
+
if (position === null) {
|
|
1102
|
+
const item = pending.item;
|
|
1103
|
+
if (item?.kind !== "command")
|
|
1104
|
+
return undefined;
|
|
1105
|
+
for (const completion of this.#parentSession?.completions ?? []) {
|
|
1106
|
+
if (completion.item?.kind !== "command" ||
|
|
1107
|
+
completion.item.outputDigest !== item.persistedOutputDigest)
|
|
1108
|
+
continue;
|
|
1109
|
+
if (position !== null)
|
|
1110
|
+
return undefined;
|
|
1111
|
+
position = completion.position;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
// A retained source row may have supplied scope before its lossy live copy
|
|
1115
|
+
// arrived. Reusing that receipt does not locate the live command.
|
|
1116
|
+
const source = this.#inventoryFrontier.source;
|
|
1117
|
+
const admittedSource = source?.inventoryAdmitted === true &&
|
|
1118
|
+
source.position === position &&
|
|
1119
|
+
source.markerIndex === inventory.index &&
|
|
1120
|
+
progressKey(source.update) === progressKey(inventory.progress);
|
|
1121
|
+
// A matched review can corroborate an accepted inventory or admit a lost
|
|
1122
|
+
// inventory without assigning its command a guessed occurrence. A later
|
|
1123
|
+
// inventory still wins, and duplicate lossy output remains ambiguous.
|
|
1124
|
+
const bounded = position !== null &&
|
|
1125
|
+
(position > this.#inventoryFrontier.position ||
|
|
1126
|
+
(position === this.#inventoryFrontier.position &&
|
|
1127
|
+
((pending.position !== null &&
|
|
1128
|
+
inventory.index >= this.#inventoryFrontier.markerIndex) ||
|
|
1129
|
+
admittedSource))) &&
|
|
1130
|
+
(position < live.position ||
|
|
1131
|
+
(pending.position !== null &&
|
|
1132
|
+
position === live.position &&
|
|
1133
|
+
inventory.index < marker.index));
|
|
1134
|
+
if (!bounded ||
|
|
1135
|
+
!this.#inventoryPrecedesReview(position, pending.position === null ? 0 : inventory.index))
|
|
1136
|
+
return undefined;
|
|
1137
|
+
const later = this.#parentSession?.progress.findLast((saved) => !saved.duplicate &&
|
|
1138
|
+
isInventoryProgress(saved.update) &&
|
|
1139
|
+
(saved.position > position ||
|
|
1140
|
+
(pending.position !== null &&
|
|
1141
|
+
saved.position === position &&
|
|
1142
|
+
saved.markerIndex > inventory.index)) &&
|
|
1143
|
+
(saved.position < live.position ||
|
|
1144
|
+
(saved.position === live.position &&
|
|
1145
|
+
saved.markerIndex < marker.index)) &&
|
|
1146
|
+
this.#parentInventoryAdmission(saved, undefined, {
|
|
1147
|
+
live: pending,
|
|
1148
|
+
after: {
|
|
1149
|
+
position: position,
|
|
1150
|
+
markerIndex: inventory.index,
|
|
1151
|
+
order: inventory.order,
|
|
1152
|
+
},
|
|
1153
|
+
}, inventoryWitness) !== "retained");
|
|
1154
|
+
// Corroboration admits scope without relocating the inventory to the review.
|
|
1155
|
+
// Its original live order continues to participate alongside newer scopes.
|
|
1156
|
+
if (later === undefined && pending.position === null) {
|
|
1157
|
+
inventory.accepted = true;
|
|
1158
|
+
return undefined;
|
|
1159
|
+
}
|
|
1160
|
+
return later === undefined
|
|
1161
|
+
? {
|
|
1162
|
+
progress: inventory.progress,
|
|
1163
|
+
position: pending.position,
|
|
1164
|
+
markerIndex: inventory.index,
|
|
1165
|
+
order: inventory.order,
|
|
1166
|
+
}
|
|
1167
|
+
: {
|
|
1168
|
+
progress: later.update,
|
|
1169
|
+
position: later.position,
|
|
1170
|
+
markerIndex: later.markerIndex,
|
|
1171
|
+
order: (later.order ??= ++this.#progressOrder),
|
|
1172
|
+
afterLive: pending,
|
|
1173
|
+
source: later,
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
#reconcileParentProgress() {
|
|
1177
|
+
const ready = [...this.#readyParentMarkers];
|
|
1178
|
+
this.#readyParentMarkers.clear();
|
|
1179
|
+
for (const [marker, live] of ready) {
|
|
1180
|
+
if (live.position !== null) {
|
|
1181
|
+
this.#rememberParentFact({
|
|
1182
|
+
progress: marker.progress,
|
|
1183
|
+
order: marker.order,
|
|
1184
|
+
position: live.position,
|
|
1185
|
+
markerIndex: marker.index,
|
|
1186
|
+
live,
|
|
1187
|
+
});
|
|
1188
|
+
if (marker.accepted)
|
|
1189
|
+
this.#recordParentBoundary(marker.progress, live.position, marker.index, true, marker.scopeFilesTotal, live);
|
|
1190
|
+
}
|
|
1191
|
+
if (marker.accepted && live.position !== null) {
|
|
1192
|
+
const source = { position: live.position, markerIndex: marker.index };
|
|
1193
|
+
if (this.#parentProgress?.order === marker.order)
|
|
1194
|
+
Object.assign(this.#parentProgress, source);
|
|
1195
|
+
if (this.#displayedProgress?.owner !== "worker" &&
|
|
1196
|
+
this.#displayedProgress?.order === marker.order)
|
|
1197
|
+
Object.assign(this.#displayedProgress, source);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
if (this.#parentSession !== null)
|
|
1201
|
+
this.#boundParentCommands(this.#parentSession);
|
|
1202
|
+
const unresolvedCommand = this.#unresolvedParentCommand();
|
|
1203
|
+
// Source proof and its eligible review cutoff precede inventory admission.
|
|
1204
|
+
// A count-rejected review can invalidate a later provisional inventory.
|
|
1205
|
+
const sourceInventory = this.#recordParentSourceBoundaries(unresolvedCommand);
|
|
1206
|
+
let witnessParent = this.#acceptedParent;
|
|
1207
|
+
let inventoryWitness = this.#parentInventoryWitness();
|
|
1208
|
+
if (!this.#inventoryPrecedesReview(this.#inventoryFrontier.position, this.#inventoryFrontier.markerIndex, this.#inventoryFrontier.live ??
|
|
1209
|
+
this.#inventoryFrontier.source?.projection, this.#inventoryFrontier.source))
|
|
1210
|
+
this.#inventoryFrontier = { position: 0, markerIndex: -1, order: 0 };
|
|
1211
|
+
const acceptedInventory = this.#acceptedInventory();
|
|
1212
|
+
let corrected = null;
|
|
1213
|
+
for (const [marker, live] of ready) {
|
|
1214
|
+
const position = live.position;
|
|
1215
|
+
if (position === null)
|
|
1216
|
+
continue;
|
|
1217
|
+
const corroborated = this.#corroboratedInventory(live, marker, inventoryWitness);
|
|
1218
|
+
const progress = corroborated?.progress ?? marker.progress;
|
|
1219
|
+
if (marker === this.#parentInventory &&
|
|
1220
|
+
!marker.accepted &&
|
|
1221
|
+
this.#parentProgress !== null &&
|
|
1222
|
+
this.#parentProgress.progress.filesCompleted > 0 &&
|
|
1223
|
+
this.#parentIsEligible(this.#parentProgress) &&
|
|
1224
|
+
this.#parentInventoryBoundary?.reviewed !== true) {
|
|
1225
|
+
// A read can reach EOF before the next completion is appended, then
|
|
1226
|
+
// receive both live items while file.close is pending. Keep the compact
|
|
1227
|
+
// inventory proof until the admitted review's source position resolves.
|
|
1228
|
+
this.#readyParentMarkers.set(marker, live);
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
if ((corroborated !== undefined ||
|
|
1232
|
+
marker === acceptedInventory?.marker ||
|
|
1233
|
+
(marker === this.#parentInventory &&
|
|
1234
|
+
this.#inventoryPrecedesReview(position, marker.index, live))) &&
|
|
1235
|
+
(corroborated !== undefined ||
|
|
1236
|
+
this.#inventoryPrecedesReview(position, marker.index, live)) &&
|
|
1237
|
+
isInventoryProgress(progress) &&
|
|
1238
|
+
(position > this.#inventoryFrontier.position ||
|
|
1239
|
+
(position === this.#inventoryFrontier.position &&
|
|
1240
|
+
marker.index >= this.#inventoryFrontier.markerIndex))) {
|
|
1241
|
+
// A saved occurrence remains authoritative when its live copy arrives
|
|
1242
|
+
// after polling consumed it, even if provisional coverage rejects it.
|
|
1243
|
+
// Inventory may precede already-admitted review even when its full live
|
|
1244
|
+
// marker arrives later. For lossy output, the real matched review marks
|
|
1245
|
+
// the scope-proof boundary; the missing inventory position stays unknown.
|
|
1246
|
+
this.#inventoryFrontier = corroborated ?? {
|
|
1247
|
+
position,
|
|
1248
|
+
markerIndex: marker.index,
|
|
1249
|
+
order: marker.order,
|
|
1250
|
+
progress,
|
|
1251
|
+
live,
|
|
1252
|
+
};
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
if (acceptedInventory !== undefined &&
|
|
1256
|
+
acceptedInventory.marker === this.#parentInventory &&
|
|
1257
|
+
acceptedInventory.live.position === null &&
|
|
1258
|
+
(!this.#parentIsEligible(this.#parentProgress) ||
|
|
1259
|
+
this.#parentProgress.progress.filesCompleted === 0) &&
|
|
1260
|
+
acceptedInventory.order > (this.#parentProgress?.order ?? 0)) {
|
|
1261
|
+
// This live inventory was accepted for deferred scope reconciliation;
|
|
1262
|
+
// its saved occurrence remains unknown until source matching resolves it.
|
|
1263
|
+
const progress = acceptedInventory.marker.progress;
|
|
1264
|
+
this.#acceptedParent = acceptedInventory.live;
|
|
1265
|
+
this.#parentProgress = {
|
|
1266
|
+
progress,
|
|
1267
|
+
order: acceptedInventory.order,
|
|
1268
|
+
live: acceptedInventory.live,
|
|
1269
|
+
markerIndex: acceptedInventory.marker.index,
|
|
1270
|
+
};
|
|
1271
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
1272
|
+
corrected = { ...this.#parentProgress, owner: "live" };
|
|
1273
|
+
witnessParent = this.#acceptedParent;
|
|
1274
|
+
inventoryWitness = this.#parentInventoryWitness();
|
|
1275
|
+
}
|
|
1276
|
+
// Recover eligible retained source authority before a baseline rollback
|
|
1277
|
+
// could temporarily revive an out-of-scope parent or worker phase.
|
|
1278
|
+
if (this.#retainedInventory() === undefined &&
|
|
1279
|
+
this.#acceptedInventory() === undefined &&
|
|
1280
|
+
sourceInventory?.source !== undefined) {
|
|
1281
|
+
const admission = this.#parentInventoryAdmission(sourceInventory.source, unresolvedCommand, undefined, inventoryWitness);
|
|
1282
|
+
if (admission === "current" || admission === "proven")
|
|
1283
|
+
this.#inventoryFrontier = sourceInventory;
|
|
1284
|
+
}
|
|
1285
|
+
const inventory = this.#retainedInventory();
|
|
1286
|
+
this.#projectParentInventory();
|
|
1287
|
+
const inventoryChanged = this.#inventoryNeedsReconciliation;
|
|
1288
|
+
this.#inventoryNeedsReconciliation = false;
|
|
1289
|
+
const parentIneligible = this.#parentProgress !== null &&
|
|
1290
|
+
!this.#parentIsEligible(this.#parentProgress);
|
|
1291
|
+
const inventoryReconciled = inventoryChanged || parentIneligible;
|
|
1292
|
+
if (inventoryReconciled) {
|
|
1293
|
+
let retainedWorker = null;
|
|
1294
|
+
// Reconsider compact worker receipts under the proven inventory. Keep an
|
|
1295
|
+
// oversized receipt available without counting it as reviewed coverage.
|
|
1296
|
+
for (const session of this.#sessions.values()) {
|
|
1297
|
+
if (session.threadId === null || session.threadId === this.#threadId)
|
|
1298
|
+
continue;
|
|
1299
|
+
for (const [total, pending] of session.pendingWorkerProgress) {
|
|
1300
|
+
const progress = pending.progress;
|
|
1301
|
+
if (total > this.#expectedFilesTotal ||
|
|
1302
|
+
(session.filesTotal !== null && total !== session.filesTotal) ||
|
|
1303
|
+
progress.filesCompleted < session.filesCompleted)
|
|
1304
|
+
continue;
|
|
1305
|
+
session.filesTotal = total;
|
|
1306
|
+
session.filesCompleted = progress.filesCompleted;
|
|
1307
|
+
const latest = session.latestWorkerProgress;
|
|
1308
|
+
if (latest === null ||
|
|
1309
|
+
progress.filesCompleted > latest.progress.filesCompleted ||
|
|
1310
|
+
pending.order > latest.order)
|
|
1311
|
+
session.latestWorkerProgress = pending;
|
|
1312
|
+
session.pendingWorkerProgress.delete(total);
|
|
1313
|
+
if (corrected === null || pending.order > corrected.order)
|
|
1314
|
+
corrected = { ...pending, owner: "worker" };
|
|
1315
|
+
}
|
|
1316
|
+
if (session.filesTotal !== null &&
|
|
1317
|
+
session.filesTotal <= this.#expectedFilesTotal) {
|
|
1318
|
+
this.#workerProgress.set(session.threadId, session.filesCompleted);
|
|
1319
|
+
const latest = session.latestWorkerProgress;
|
|
1320
|
+
if (latest !== null &&
|
|
1321
|
+
(retainedWorker === null || latest.order > retainedWorker.order))
|
|
1322
|
+
retainedWorker = latest;
|
|
1323
|
+
if (parentIneligible &&
|
|
1324
|
+
latest !== null &&
|
|
1325
|
+
(corrected === null || latest.order > corrected.order))
|
|
1326
|
+
corrected = { ...latest, owner: "worker" };
|
|
1327
|
+
}
|
|
1328
|
+
else
|
|
1329
|
+
this.#workerProgress.delete(session.threadId);
|
|
1330
|
+
}
|
|
1331
|
+
const parentCount = this.#parentIsEligible(this.#parentProgress)
|
|
1332
|
+
? this.#parentProgress.progress.filesCompleted
|
|
1333
|
+
: 0;
|
|
1334
|
+
// Project the phase from the greatest eligible coverage. A zero-count
|
|
1335
|
+
// inventory establishes scope but cannot mask restored worker progress.
|
|
1336
|
+
const workerCoverage = Math.min(this.#expectedFilesTotal, this.#workerProgress.values().reduce((sum, count) => sum + count, 0));
|
|
1337
|
+
if (retainedWorker !== null &&
|
|
1338
|
+
workerCoverage > parentCount &&
|
|
1339
|
+
(corrected === null ||
|
|
1340
|
+
(corrected.owner !== "worker" &&
|
|
1341
|
+
workerCoverage > corrected.progress.filesCompleted)))
|
|
1342
|
+
corrected = { ...retainedWorker, owner: "worker" };
|
|
1343
|
+
this.#highestFilesCompleted = Math.max(parentCount, workerCoverage);
|
|
1344
|
+
if (this.#parentSession !== null) {
|
|
1345
|
+
this.#parentSession.filesCompleted = parentCount;
|
|
1346
|
+
this.#parentSession.filesTotal = this.#expectedFilesTotal;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
const workerCoverage = Math.min(this.#expectedFilesTotal ?? Number.MAX_SAFE_INTEGER, this.#workerProgress.values().reduce((sum, count) => sum + count, 0));
|
|
1350
|
+
for (const currentCompletion of this.#liveParentItems) {
|
|
1351
|
+
if (currentCompletion.position !== null ||
|
|
1352
|
+
// Unfamiliar projections supply no source identity. Keep the existing
|
|
1353
|
+
// recovery of an admitted completion's remaining parsed markers.
|
|
1354
|
+
(currentCompletion.item === null &&
|
|
1355
|
+
!currentCompletion.markers.some((marker) => marker.accepted)))
|
|
1356
|
+
continue;
|
|
1357
|
+
// A bound orders other completions; it does not retire later markers
|
|
1358
|
+
// inside any retained completion. Scope can make those markers eligible
|
|
1359
|
+
// after provisional worker coverage prevented their live admission.
|
|
1360
|
+
for (const marker of currentCompletion.markers) {
|
|
1361
|
+
const progress = marker.progress;
|
|
1362
|
+
const candidate = {
|
|
1363
|
+
progress,
|
|
1364
|
+
order: marker.order,
|
|
1365
|
+
live: currentCompletion,
|
|
1366
|
+
markerIndex: marker.index,
|
|
1367
|
+
owner: "live",
|
|
1368
|
+
};
|
|
1369
|
+
if (marker.accepted ||
|
|
1370
|
+
!this.#parentSourceIsEligible(candidate) ||
|
|
1371
|
+
// Inventory has its own admission path. A rejected inventory cannot
|
|
1372
|
+
// become a phase merely because coverage later falls to zero.
|
|
1373
|
+
isInventoryProgress(progress) ||
|
|
1374
|
+
progress.filesTotal >
|
|
1375
|
+
(this.#expectedFilesTotal ?? Number.MAX_SAFE_INTEGER) ||
|
|
1376
|
+
progress.filesCompleted < this.#highestFilesCompleted ||
|
|
1377
|
+
(this.#parentProgress !== null &&
|
|
1378
|
+
compareProgressOrder(candidate, this.#parentProgress) < 0))
|
|
1379
|
+
continue;
|
|
1380
|
+
marker.accepted = true;
|
|
1381
|
+
this.#acceptedParent = currentCompletion;
|
|
1382
|
+
this.#recordParentBoundary(progress, undefined, marker.index, true, undefined, currentCompletion);
|
|
1383
|
+
this.#parentProgress = candidate;
|
|
1384
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
1385
|
+
this.#highestFilesCompleted = progress.filesCompleted;
|
|
1386
|
+
const correctedCoverage = corrected?.owner === "worker"
|
|
1387
|
+
? workerCoverage
|
|
1388
|
+
: (corrected?.progress.filesCompleted ?? -1);
|
|
1389
|
+
if (progress.filesCompleted > correctedCoverage ||
|
|
1390
|
+
(progress.filesCompleted === correctedCoverage &&
|
|
1391
|
+
corrected !== null &&
|
|
1392
|
+
compareProgressOrder(candidate, corrected) > 0))
|
|
1393
|
+
corrected = candidate;
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
for (const [marker, live] of ready) {
|
|
1397
|
+
const position = live.position;
|
|
1398
|
+
const progress = marker.progress;
|
|
1399
|
+
const deferredCoverage = !marker.accepted &&
|
|
1400
|
+
progress.filesCompleted > this.#highestFilesCompleted;
|
|
1401
|
+
if (position === null ||
|
|
1402
|
+
(!deferredCoverage &&
|
|
1403
|
+
!this.#afterParentFrontier(position, marker.index) &&
|
|
1404
|
+
!(inventoryChanged &&
|
|
1405
|
+
marker === this.#parentInventory &&
|
|
1406
|
+
position === this.#parentFrontier.position &&
|
|
1407
|
+
marker.index === this.#parentFrontier.markerIndex)))
|
|
1408
|
+
continue;
|
|
1409
|
+
const candidate = {
|
|
1410
|
+
progress,
|
|
1411
|
+
order: marker.order,
|
|
1412
|
+
position,
|
|
1413
|
+
markerIndex: marker.index,
|
|
1414
|
+
live,
|
|
1415
|
+
owner: "live",
|
|
1416
|
+
};
|
|
1417
|
+
const selectedCoverage = corrected?.owner === "worker"
|
|
1418
|
+
? workerCoverage
|
|
1419
|
+
: (corrected?.progress.filesCompleted ?? -1);
|
|
1420
|
+
// Do not let a newer, lower-coverage worker candidate mask a valid parent
|
|
1421
|
+
// correction before the final display guards can consider it.
|
|
1422
|
+
const preferCandidate = corrected === null ||
|
|
1423
|
+
progress.filesCompleted > selectedCoverage ||
|
|
1424
|
+
(progress.filesCompleted === selectedCoverage &&
|
|
1425
|
+
compareProgressOrder(candidate, corrected) > 0);
|
|
1426
|
+
if (!marker.accepted) {
|
|
1427
|
+
if ((this.#expectedFilesTotal !== undefined &&
|
|
1428
|
+
progress.filesTotal > this.#expectedFilesTotal) ||
|
|
1429
|
+
progress.filesTotal >
|
|
1430
|
+
(marker.scopeFilesTotal ?? Number.MAX_SAFE_INTEGER) ||
|
|
1431
|
+
progress.filesCompleted < this.#highestFilesCompleted ||
|
|
1432
|
+
(isInventoryProgress(progress) &&
|
|
1433
|
+
!this.#inventoryPrecedesReview(position, marker.index, live))) {
|
|
1434
|
+
// Saved inventory can admit the live receipt or invalidate provisional
|
|
1435
|
+
// worker coverage. Keep only the parsed fact until it is eligible.
|
|
1436
|
+
this.#readyParentMarkers.set(marker, live);
|
|
1437
|
+
continue;
|
|
1438
|
+
}
|
|
1439
|
+
marker.accepted = true;
|
|
1440
|
+
this.#acceptedParent = live;
|
|
1441
|
+
const selectedParent = this.#parentProgress;
|
|
1442
|
+
if (!this.#parentIsEligible(selectedParent) ||
|
|
1443
|
+
progress.filesCompleted !== selectedParent.progress.filesCompleted ||
|
|
1444
|
+
compareProgressOrder(candidate, selectedParent) >= 0)
|
|
1445
|
+
this.#parentProgress = {
|
|
1446
|
+
progress,
|
|
1447
|
+
order: marker.order,
|
|
1448
|
+
position,
|
|
1449
|
+
markerIndex: marker.index,
|
|
1450
|
+
live,
|
|
1451
|
+
};
|
|
1452
|
+
this.#recordParentBoundary(progress, position, marker.index, false, marker.scopeFilesTotal);
|
|
1453
|
+
this.#liveParentProgress.add(progressKey(progress));
|
|
1454
|
+
this.#highestFilesCompleted = progress.filesCompleted;
|
|
1455
|
+
if (preferCandidate)
|
|
1456
|
+
corrected = candidate;
|
|
1457
|
+
}
|
|
1458
|
+
else if (this.#parentIsEligible(candidate) &&
|
|
1459
|
+
this.#displayedProgress?.owner === "parent" &&
|
|
1460
|
+
compareProgressOrder(candidate, this.#displayedProgress) > 0 &&
|
|
1461
|
+
preferCandidate) {
|
|
1462
|
+
corrected = candidate;
|
|
1463
|
+
}
|
|
1464
|
+
this.#advanceParentFrontier(position, marker.index);
|
|
1465
|
+
}
|
|
1466
|
+
if (witnessParent !== this.#acceptedParent)
|
|
1467
|
+
inventoryWitness = this.#parentInventoryWitness();
|
|
1468
|
+
// Parent facts outlive display admission and queue draining. Preserve the
|
|
1469
|
+
// greatest eligible coverage for each original inventory, then source order.
|
|
1470
|
+
let eligible = this.#parentIsEligible(this.#parentProgress)
|
|
1471
|
+
? this.#parentProgress
|
|
1472
|
+
: null;
|
|
1473
|
+
for (const candidate of [
|
|
1474
|
+
...this.#parentFacts.values(),
|
|
1475
|
+
...(inventory === undefined ? [] : [inventory]),
|
|
1476
|
+
]) {
|
|
1477
|
+
// Apply eligible preceding inventories before promoting a retained phase.
|
|
1478
|
+
// Retained copies and unresolved alternatives are not unapplied work.
|
|
1479
|
+
if (this.#parentSession?.progress.some((saved) => {
|
|
1480
|
+
const admission = this.#parentInventoryAdmission(saved, unresolvedCommand, undefined, inventoryWitness);
|
|
1481
|
+
return ((admission === "current" || admission === "proven") &&
|
|
1482
|
+
(saved.position < candidate.position ||
|
|
1483
|
+
(saved.position === candidate.position &&
|
|
1484
|
+
saved.markerIndex < candidate.markerIndex)));
|
|
1485
|
+
}))
|
|
1486
|
+
continue;
|
|
1487
|
+
if (!this.#parentIsEligible(candidate) ||
|
|
1488
|
+
candidate.progress.filesCompleted < workerCoverage)
|
|
1489
|
+
continue;
|
|
1490
|
+
if (eligible === null ||
|
|
1491
|
+
candidate.progress.filesCompleted > eligible.progress.filesCompleted ||
|
|
1492
|
+
(candidate.progress.filesCompleted ===
|
|
1493
|
+
eligible.progress.filesCompleted &&
|
|
1494
|
+
compareProgressOrder(candidate, eligible) > 0))
|
|
1495
|
+
eligible = candidate;
|
|
1496
|
+
}
|
|
1497
|
+
if (eligible !== null) {
|
|
1498
|
+
this.#parentProgress = eligible;
|
|
1499
|
+
this.#advanceParentFrontier(eligible.position, eligible.markerIndex);
|
|
1500
|
+
this.#recordParentBoundary(eligible.progress, eligible.position, eligible.markerIndex, false, eligible.source?.scopeFilesTotal ??
|
|
1501
|
+
eligible.live?.markers[eligible.markerIndex ?? 0]?.scopeFilesTotal, eligible.live);
|
|
1502
|
+
this.#highestFilesCompleted = Math.max(workerCoverage, eligible.progress.filesCompleted);
|
|
1503
|
+
if (this.#parentSession !== null) {
|
|
1504
|
+
this.#parentSession.filesCompleted = eligible.progress.filesCompleted;
|
|
1505
|
+
this.#parentSession.filesTotal =
|
|
1506
|
+
this.#expectedFilesTotal ?? eligible.progress.filesTotal;
|
|
1507
|
+
}
|
|
1508
|
+
const correctedCoverage = corrected?.owner === "worker"
|
|
1509
|
+
? workerCoverage
|
|
1510
|
+
: (corrected?.progress.filesCompleted ?? -1);
|
|
1511
|
+
if (corrected === null ||
|
|
1512
|
+
eligible.progress.filesCompleted > correctedCoverage ||
|
|
1513
|
+
(eligible.progress.filesCompleted === correctedCoverage &&
|
|
1514
|
+
compareProgressOrder(eligible, corrected) > 0))
|
|
1515
|
+
corrected = { ...eligible, owner: "parent" };
|
|
1516
|
+
}
|
|
1517
|
+
const displayed = this.#displayedProgress;
|
|
1518
|
+
const displayedParentIneligible = displayed !== null &&
|
|
1519
|
+
displayed.owner !== "worker" &&
|
|
1520
|
+
!this.#parentIsEligible({
|
|
1521
|
+
...displayed,
|
|
1522
|
+
// Scope recovery can replace the parent before changing the display.
|
|
1523
|
+
// Requalify the receipt that actually supplied the displayed phase.
|
|
1524
|
+
progress: displayed.live?.markers[displayed.markerIndex ?? 0]?.progress ??
|
|
1525
|
+
displayed.source?.update ??
|
|
1526
|
+
displayed.update,
|
|
1527
|
+
});
|
|
1528
|
+
if (!inventoryReconciled && corrected === null)
|
|
1529
|
+
return inventoryWitness;
|
|
1530
|
+
const correctedCoverage = corrected?.owner === "worker"
|
|
1531
|
+
? workerCoverage
|
|
1532
|
+
: (corrected?.progress.filesCompleted ?? 0);
|
|
1533
|
+
const replacePhase = corrected !== null &&
|
|
1534
|
+
correctedCoverage >= this.#highestFilesCompleted &&
|
|
1535
|
+
(displayed === null ||
|
|
1536
|
+
displayedParentIneligible ||
|
|
1537
|
+
correctedCoverage > displayed.update.filesCompleted ||
|
|
1538
|
+
compareProgressOrder(corrected, displayed) >= 0);
|
|
1539
|
+
const selected = replacePhase ? corrected : displayed;
|
|
1540
|
+
const parent = this.#parentProgress;
|
|
1541
|
+
const parentAtEqualCoverage = selected?.owner === "worker" &&
|
|
1542
|
+
this.#parentIsEligible(parent) &&
|
|
1543
|
+
parent.progress.filesCompleted === this.#highestFilesCompleted;
|
|
1544
|
+
const phase = parentAtEqualCoverage
|
|
1545
|
+
? parent.progress.phase
|
|
1546
|
+
: replacePhase
|
|
1547
|
+
? corrected.progress.phase
|
|
1548
|
+
: (displayed?.update.phase ?? "discovery");
|
|
1549
|
+
const progress = {
|
|
1550
|
+
phase,
|
|
1551
|
+
filesCompleted: this.#highestFilesCompleted,
|
|
1552
|
+
filesTotal: this.#expectedFilesTotal ??
|
|
1553
|
+
corrected?.progress.filesTotal ??
|
|
1554
|
+
displayed.update.filesTotal,
|
|
1555
|
+
};
|
|
1556
|
+
const key = progressKey(progress);
|
|
1557
|
+
// An initial zero-count discovery already reported this inventory. Other
|
|
1558
|
+
// equal raw keys can still require synchronization of the API's file total.
|
|
1559
|
+
const inventoryReported = isInventoryProgress(progress);
|
|
1560
|
+
const duplicate = displayed !== null &&
|
|
1561
|
+
progressKey(displayed.update) === key &&
|
|
1562
|
+
(!inventoryReconciled || inventoryReported);
|
|
1563
|
+
const displaySource = parentAtEqualCoverage
|
|
1564
|
+
? { ...parent, owner: "parent" }
|
|
1565
|
+
: selected;
|
|
1566
|
+
this.#displayedProgress = {
|
|
1567
|
+
update: progress,
|
|
1568
|
+
owner: displaySource?.owner ?? "live",
|
|
1569
|
+
order: displaySource?.order ?? 0,
|
|
1570
|
+
live: displaySource?.live,
|
|
1571
|
+
source: displaySource?.source,
|
|
1572
|
+
markerIndex: displaySource?.markerIndex,
|
|
1573
|
+
...(displaySource?.position === undefined
|
|
1574
|
+
? {}
|
|
1575
|
+
: {
|
|
1576
|
+
position: displaySource.position,
|
|
1577
|
+
}),
|
|
1578
|
+
};
|
|
1579
|
+
if (duplicate)
|
|
1580
|
+
return inventoryWitness;
|
|
1581
|
+
this.#options.onProgress?.(progress, {
|
|
1582
|
+
reconciled: true,
|
|
1583
|
+
...(displaySource?.owner === "worker" ? { worker: true } : {}),
|
|
1584
|
+
// Recovered prefix scope can invalidate coverage even when a later
|
|
1585
|
+
// inventory restores the same total. Synchronize the API's count too.
|
|
1586
|
+
...(inventoryReconciled ? { inventory: true } : {}),
|
|
1587
|
+
});
|
|
1588
|
+
return inventoryWitness;
|
|
1589
|
+
}
|
|
1590
|
+
#savedParentProgressIsCurrent(saved) {
|
|
1591
|
+
if (!this.#afterParentFrontier(saved.position, saved.markerIndex))
|
|
1592
|
+
return false;
|
|
1593
|
+
// An outer tool result has its own completion order. Native output does not
|
|
1594
|
+
// identify whether its text was copied from a nested command or emitted
|
|
1595
|
+
// directly, so text equality and call-start time cannot establish a copy.
|
|
1596
|
+
// Treat it as a receipt; a copied truncated preview can consequently hide
|
|
1597
|
+
// omitted same-count phases from its earlier command. This is best effort.
|
|
1598
|
+
if (saved.receipt)
|
|
1599
|
+
return true;
|
|
1600
|
+
const live = this.#acceptedParent;
|
|
1601
|
+
if (live === null)
|
|
1602
|
+
return true;
|
|
1603
|
+
// A later saved occurrence may legitimately repeat an earlier live tuple,
|
|
1604
|
+
// including progress recovered only from the original multipart response.
|
|
1605
|
+
if (live.position !== null)
|
|
1606
|
+
return saved.position > live.position;
|
|
1607
|
+
if (live.after !== undefined)
|
|
1608
|
+
return saved.position >= live.after;
|
|
1609
|
+
return !this.#unboundParentCopy(saved);
|
|
1610
|
+
}
|
|
1611
|
+
#unboundParentCopy(saved) {
|
|
1612
|
+
const live = this.#acceptedParent;
|
|
1613
|
+
return (!saved.receipt &&
|
|
1614
|
+
live?.position === null &&
|
|
1615
|
+
live.after === undefined &&
|
|
1616
|
+
this.#liveParentProgress.has(progressKey(saved.update)));
|
|
1617
|
+
}
|
|
87
1618
|
recordUsage(usage, threadId = this.#threadId) {
|
|
88
1619
|
const normalized = tokenUsage(usage);
|
|
89
1620
|
if (threadId !== null) {
|
|
@@ -162,6 +1693,7 @@ export class ScanCostTracker {
|
|
|
162
1693
|
const repository = this.#options.onActivity === undefined
|
|
163
1694
|
? undefined
|
|
164
1695
|
: this.#options.repository;
|
|
1696
|
+
const progressThread = this.#options.onProgress === undefined ? undefined : this.#threadId;
|
|
165
1697
|
const unreadable = [];
|
|
166
1698
|
for await (const path of sessionFiles(join(this.#options.codexHome, "sessions"))) {
|
|
167
1699
|
let session = this.#sessions.get(path);
|
|
@@ -170,7 +1702,7 @@ export class ScanCostTracker {
|
|
|
170
1702
|
this.#sessions.set(path, session);
|
|
171
1703
|
}
|
|
172
1704
|
try {
|
|
173
|
-
await readSessionUsage(path, session, repository);
|
|
1705
|
+
await readSessionUsage(path, session, repository, progressThread);
|
|
174
1706
|
}
|
|
175
1707
|
catch (error) {
|
|
176
1708
|
if (session.threadId === null)
|
|
@@ -211,18 +1743,23 @@ export class ScanCostTracker {
|
|
|
211
1743
|
throw error;
|
|
212
1744
|
}
|
|
213
1745
|
const usages = new Map(this.#receipts);
|
|
214
|
-
for (const [path,
|
|
215
|
-
const threadId =
|
|
1746
|
+
for (const [path, session] of this.#sessions) {
|
|
1747
|
+
const threadId = session.threadId;
|
|
216
1748
|
if (threadId === null || !included.has(threadId))
|
|
217
1749
|
continue;
|
|
218
|
-
let session = tracked;
|
|
219
1750
|
if (this.#options.onSessionEvent !== undefined &&
|
|
220
1751
|
session.events === undefined) {
|
|
221
|
-
// Replay
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
1752
|
+
// Replay event history through the already-read snapshot without
|
|
1753
|
+
// replacing its authoritative progress, usage, or partial-line decoder.
|
|
1754
|
+
const replay = createSessionUsage(false);
|
|
1755
|
+
replay.events = [];
|
|
1756
|
+
await readSessionUsage(path, replay, undefined, undefined, session.offset);
|
|
1757
|
+
if (replay.offset === session.offset) {
|
|
1758
|
+
session.events = replay.events;
|
|
1759
|
+
// At stop there is no next poll to read usage appended during replay.
|
|
1760
|
+
if (this.#timer === null)
|
|
1761
|
+
await readSessionUsage(path, session, repository, progressThread, "file-size");
|
|
1762
|
+
}
|
|
226
1763
|
}
|
|
227
1764
|
let worker;
|
|
228
1765
|
if (threadId !== this.#threadId) {
|
|
@@ -250,7 +1787,30 @@ export class ScanCostTracker {
|
|
|
250
1787
|
worker,
|
|
251
1788
|
});
|
|
252
1789
|
}
|
|
253
|
-
|
|
1790
|
+
}
|
|
1791
|
+
let unresolvedCommand;
|
|
1792
|
+
let inventoryWitness;
|
|
1793
|
+
if (threadId === this.#threadId) {
|
|
1794
|
+
this.#parentSession = session;
|
|
1795
|
+
for (const saved of session.progress)
|
|
1796
|
+
if (saved.order !== undefined && !saved.duplicate)
|
|
1797
|
+
this.#rememberParentFact(savedProgress(saved));
|
|
1798
|
+
this.#matchParentCompletions();
|
|
1799
|
+
inventoryWitness = this.#reconcileParentProgress();
|
|
1800
|
+
unresolvedCommand = this.#unresolvedParentCommand();
|
|
1801
|
+
}
|
|
1802
|
+
const reviewWasUnpositioned = this.#parentInventoryBoundary?.reviewed !== true;
|
|
1803
|
+
this.#reportSessionProgress(session, threadId === this.#threadId, unresolvedCommand, inventoryWitness);
|
|
1804
|
+
// Fresh saved facts are observed at their source-loop position, so initial
|
|
1805
|
+
// inventory notifications keep their order. Recover after the batch too:
|
|
1806
|
+
// a later accepted parent may have hidden an eligible earlier receipt.
|
|
1807
|
+
if (threadId === this.#threadId) {
|
|
1808
|
+
// A newly admitted review locates inventories parked earlier in this
|
|
1809
|
+
// batch. Reconsider them before the final refresh returns.
|
|
1810
|
+
if (reviewWasUnpositioned &&
|
|
1811
|
+
this.#parentInventoryBoundary?.reviewed === true)
|
|
1812
|
+
this.#reportSessionProgress(session, true, unresolvedCommand, inventoryWitness);
|
|
1813
|
+
this.#reconcileParentProgress();
|
|
254
1814
|
}
|
|
255
1815
|
const receipt = usages.get(threadId);
|
|
256
1816
|
// A discovered thread without a receipt has unknown usage, not zero.
|
|
@@ -327,38 +1887,230 @@ export class ScanCostTracker {
|
|
|
327
1887
|
this.#lastObservation = observation;
|
|
328
1888
|
this.#options.onUsageObservation?.(observation);
|
|
329
1889
|
}
|
|
330
|
-
#
|
|
1890
|
+
#parentInventoryAdmission(saved, command, corroboration, inventoryWitness) {
|
|
1891
|
+
if (!isInventoryProgress(saved.update))
|
|
1892
|
+
return undefined;
|
|
1893
|
+
const newer = saved.position > this.#inventoryFrontier.position ||
|
|
1894
|
+
(saved.position === this.#inventoryFrontier.position &&
|
|
1895
|
+
saved.markerIndex > this.#inventoryFrontier.markerIndex);
|
|
1896
|
+
if (saved.duplicate || !newer)
|
|
1897
|
+
return "retained";
|
|
1898
|
+
if (!this.#inventoryPrecedesReview(saved.position, saved.markerIndex, saved.projection, saved))
|
|
1899
|
+
return "retained";
|
|
1900
|
+
const authority = this.#acceptedInventory();
|
|
1901
|
+
const source = savedProgress(saved);
|
|
1902
|
+
// A matched review can order an inventory after a compatible truncated
|
|
1903
|
+
// prefix without identifying the live command's occurrence. That proof
|
|
1904
|
+
// applies to its own inventory, not a newer independent live authority.
|
|
1905
|
+
const relation = authority === undefined
|
|
1906
|
+
? 0
|
|
1907
|
+
: (this.#inventorySourceOrder(source, authority) ??
|
|
1908
|
+
(corroboration?.live === authority.live
|
|
1909
|
+
? compareSourceOrder(source, corroboration.after)
|
|
1910
|
+
: undefined) ??
|
|
1911
|
+
(authority.live.after === undefined ? 0 : -1));
|
|
1912
|
+
if (this.#precedesParentBoundary(saved.position, saved.markerIndex) &&
|
|
1913
|
+
(this.#parentInventoryBoundary?.reviewed === true ||
|
|
1914
|
+
!this.#parentIsEligible(this.#parentProgress) ||
|
|
1915
|
+
this.#parentProgress.progress.filesCompleted === 0 ||
|
|
1916
|
+
(inventoryWitness !== undefined &&
|
|
1917
|
+
(compareSourceOrder(source, inventoryWitness) ?? 0) < 0)) &&
|
|
1918
|
+
relation >= 0)
|
|
1919
|
+
return "proven";
|
|
1920
|
+
if ((this.#parentIsEligible(this.#parentProgress) &&
|
|
1921
|
+
this.#parentProgress.progress.filesCompleted > 0) ||
|
|
1922
|
+
!this.#savedParentProgressIsCurrent(saved) ||
|
|
1923
|
+
(command !== undefined &&
|
|
1924
|
+
command.live === this.#acceptedParent &&
|
|
1925
|
+
command.live.position === null &&
|
|
1926
|
+
this.#parentProgress !== null &&
|
|
1927
|
+
saved.position < command.after &&
|
|
1928
|
+
this.#afterParentFrontier(saved.position, saved.markerIndex)))
|
|
1929
|
+
return "retained";
|
|
1930
|
+
return "current";
|
|
1931
|
+
}
|
|
1932
|
+
#reportSessionProgress(session, parent, command, inventoryWitness) {
|
|
331
1933
|
if (this.#options.onProgress === undefined || session.threadId === null) {
|
|
332
1934
|
return;
|
|
333
1935
|
}
|
|
334
|
-
|
|
1936
|
+
const live = this.#acceptedParent;
|
|
1937
|
+
const current = this.#parentProgress;
|
|
1938
|
+
const unresolvedCommandState = parent &&
|
|
1939
|
+
command?.live === live &&
|
|
1940
|
+
live?.position === null &&
|
|
1941
|
+
current !== null;
|
|
1942
|
+
// Parent rows are source evidence, not a notification queue. Admission or
|
|
1943
|
+
// temporary scope exclusion cannot discard an unresolved occurrence.
|
|
1944
|
+
// Worker receipts are independent and retain their compact summaries below.
|
|
1945
|
+
for (const saved of parent
|
|
1946
|
+
? session.progress
|
|
1947
|
+
: session.progress.splice(0)) {
|
|
1948
|
+
const progress = saved.update;
|
|
1949
|
+
const inventory = parent
|
|
1950
|
+
? this.#parentInventoryAdmission(saved, command, undefined, inventoryWitness)
|
|
1951
|
+
: undefined;
|
|
1952
|
+
if (inventory === "retained")
|
|
1953
|
+
continue;
|
|
1954
|
+
const inventoryMarker = inventory !== undefined;
|
|
1955
|
+
const provenInventory = inventory === "proven";
|
|
1956
|
+
// Source order governs equal-count phases. Fresh and parked receipts can
|
|
1957
|
+
// both add coverage after a scope correction makes that coverage eligible.
|
|
1958
|
+
const greaterCoverage = progress.filesCompleted > this.#highestFilesCompleted;
|
|
1959
|
+
if (parent &&
|
|
1960
|
+
!saved.duplicate &&
|
|
1961
|
+
!inventoryMarker &&
|
|
1962
|
+
!this.#unboundParentCopy(saved)) {
|
|
1963
|
+
saved.order ??= ++this.#progressOrder;
|
|
1964
|
+
this.#rememberParentFact(savedProgress(saved));
|
|
1965
|
+
}
|
|
1966
|
+
if (unresolvedCommandState &&
|
|
1967
|
+
saved.position < (command?.after ?? Infinity) &&
|
|
1968
|
+
this.#afterParentFrontier(saved.position, saved.markerIndex) &&
|
|
1969
|
+
!provenInventory &&
|
|
1970
|
+
progress.filesCompleted <= this.#highestFilesCompleted) {
|
|
1971
|
+
// Keep lossy-output ambiguity across marker-free anchors; a current
|
|
1972
|
+
// state or greater coverage must supply the missing progress evidence.
|
|
1973
|
+
continue;
|
|
1974
|
+
}
|
|
1975
|
+
if (parent &&
|
|
1976
|
+
!provenInventory &&
|
|
1977
|
+
!greaterCoverage &&
|
|
1978
|
+
!this.#savedParentProgressIsCurrent(saved))
|
|
1979
|
+
continue;
|
|
1980
|
+
if (saved.duplicate) {
|
|
1981
|
+
if (parent && saved.completion)
|
|
1982
|
+
this.#advanceParentFrontier(saved.position, saved.markerIndex);
|
|
1983
|
+
continue;
|
|
1984
|
+
}
|
|
335
1985
|
const expectedFilesTotal = this.#expectedFilesTotal;
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
progress.
|
|
1986
|
+
const parentInventory = inventoryMarker;
|
|
1987
|
+
const order = (saved.order ??= ++this.#progressOrder);
|
|
1988
|
+
if ((!parent &&
|
|
1989
|
+
session.filesTotal !== null &&
|
|
1990
|
+
progress.filesTotal !== session.filesTotal) ||
|
|
1991
|
+
(!parentInventory && progress.filesCompleted < session.filesCompleted))
|
|
1992
|
+
continue;
|
|
1993
|
+
if (!parentInventory &&
|
|
1994
|
+
((expectedFilesTotal !== undefined &&
|
|
1995
|
+
progress.filesTotal > expectedFilesTotal) ||
|
|
1996
|
+
(parent &&
|
|
1997
|
+
progress.filesTotal >
|
|
1998
|
+
(saved.scopeFilesTotal ?? Number.MAX_SAFE_INTEGER)))) {
|
|
1999
|
+
if (!parent) {
|
|
2000
|
+
const pending = session.pendingWorkerProgress.get(progress.filesTotal);
|
|
2001
|
+
if (pending === undefined ||
|
|
2002
|
+
progress.filesCompleted >= pending.progress.filesCompleted)
|
|
2003
|
+
session.pendingWorkerProgress.set(progress.filesTotal, {
|
|
2004
|
+
progress,
|
|
2005
|
+
order,
|
|
2006
|
+
});
|
|
2007
|
+
}
|
|
341
2008
|
continue;
|
|
342
2009
|
}
|
|
343
2010
|
session.filesTotal = progress.filesTotal;
|
|
344
2011
|
session.filesCompleted = progress.filesCompleted;
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
2012
|
+
if (!parent)
|
|
2013
|
+
session.latestWorkerProgress = { progress, order };
|
|
2014
|
+
if (parentInventory && this.#highestFilesCompleted > 0) {
|
|
2015
|
+
// An inventory behind admitted review changes scope without replaying
|
|
2016
|
+
// its earlier phase. At zero coverage it follows normal source-ordered
|
|
2017
|
+
// phase admission below, including when the inventory total changes.
|
|
2018
|
+
if (!this.#parentIsEligible(this.#parentProgress) ||
|
|
2019
|
+
this.#parentProgress.progress.filesCompleted === 0) {
|
|
2020
|
+
const candidate = {
|
|
2021
|
+
progress,
|
|
2022
|
+
order,
|
|
2023
|
+
position: saved.position,
|
|
2024
|
+
markerIndex: saved.markerIndex,
|
|
2025
|
+
};
|
|
2026
|
+
if (this.#parentProgress === null ||
|
|
2027
|
+
this.#parentProgress.progress.phase !== "discovery" ||
|
|
2028
|
+
compareProgressOrder(candidate, this.#parentProgress) >= 0)
|
|
2029
|
+
this.#parentProgress = candidate;
|
|
2030
|
+
}
|
|
2031
|
+
this.#advanceParentFrontier(saved.position, saved.markerIndex);
|
|
2032
|
+
this.#inventoryFrontier = savedProgress(saved);
|
|
2033
|
+
this.#projectParentInventory();
|
|
350
2034
|
continue;
|
|
351
|
-
|
|
2035
|
+
}
|
|
2036
|
+
// Parent markers already describe global coverage, including its workers.
|
|
2037
|
+
// Only worker-local counts belong in the delegated shard sum.
|
|
2038
|
+
if (!parent)
|
|
2039
|
+
this.#workerProgress.set(session.threadId, progress.filesCompleted);
|
|
2040
|
+
const filesCompleted = parent
|
|
2041
|
+
? progress.filesCompleted
|
|
2042
|
+
: Math.min(expectedFilesTotal ?? Number.MAX_SAFE_INTEGER, this.#workerProgress
|
|
2043
|
+
.values()
|
|
2044
|
+
.reduce((total, reviewed) => total + reviewed, 0));
|
|
2045
|
+
if (filesCompleted < this.#highestFilesCompleted) {
|
|
2046
|
+
// A provisional worker can exceed this global parent receipt, then
|
|
2047
|
+
// become ineligible when the parent's earlier inventory is recovered.
|
|
2048
|
+
// Keep the parsed parent fact for that correction, without displaying
|
|
2049
|
+
// a count decrease while the worker coverage remains eligible.
|
|
2050
|
+
continue;
|
|
2051
|
+
}
|
|
2052
|
+
// A worker-local phase cannot replace eligible global parent authority
|
|
2053
|
+
// when the aggregate count adds no reviewed files. Counts remain above.
|
|
2054
|
+
if (!parent &&
|
|
2055
|
+
this.#parentIsEligible(this.#parentProgress) &&
|
|
2056
|
+
filesCompleted === this.#parentProgress.progress.filesCompleted)
|
|
2057
|
+
continue;
|
|
2058
|
+
if (parent)
|
|
2059
|
+
this.#recordParentBoundary(progress, saved.position, saved.markerIndex, false, saved.scopeFilesTotal);
|
|
2060
|
+
// Known parent positions outrank observation order: polling can park a
|
|
2061
|
+
// newer completion before an earlier live completion is displayed.
|
|
2062
|
+
const displayed = this.#displayedProgress;
|
|
2063
|
+
const provenance = {
|
|
2064
|
+
order,
|
|
2065
|
+
position: saved.position,
|
|
2066
|
+
markerIndex: saved.markerIndex,
|
|
2067
|
+
source: saved,
|
|
2068
|
+
};
|
|
2069
|
+
const newerProgress = parent &&
|
|
2070
|
+
displayed !== null &&
|
|
2071
|
+
displayed.owner !== "worker" &&
|
|
2072
|
+
filesCompleted === displayed.update.filesCompleted &&
|
|
2073
|
+
compareProgressOrder(provenance, displayed) < 0
|
|
2074
|
+
? displayed
|
|
2075
|
+
: null;
|
|
2076
|
+
const update = newerProgress?.update ?? {
|
|
352
2077
|
...progress,
|
|
353
2078
|
filesCompleted,
|
|
354
|
-
filesTotal:
|
|
2079
|
+
filesTotal: parent
|
|
2080
|
+
? progress.filesTotal
|
|
2081
|
+
: (expectedFilesTotal ??
|
|
2082
|
+
Math.max(progress.filesTotal, filesCompleted)),
|
|
355
2083
|
};
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
2084
|
+
// Retaining a newer parent phase retains that whole receipt, including
|
|
2085
|
+
// its actual inventory. Mixing an older total with its phase would discard
|
|
2086
|
+
// valid parent coverage when a later inventory correction arrives.
|
|
2087
|
+
if (parent && newerProgress === null)
|
|
2088
|
+
this.#parentProgress = { progress, ...provenance };
|
|
2089
|
+
const key = progressKey(update);
|
|
2090
|
+
if (parent) {
|
|
2091
|
+
// An original multipart response can expose progress absent from its
|
|
2092
|
+
// concatenated native projection. Its admitted source position still
|
|
2093
|
+
// prevents an older live completion from replaying over that progress.
|
|
2094
|
+
this.#advanceParentFrontier(saved.position, saved.markerIndex);
|
|
2095
|
+
if (parentInventory) {
|
|
2096
|
+
if (newerProgress === null)
|
|
2097
|
+
saved.inventoryAdmitted = true;
|
|
2098
|
+
this.#inventoryFrontier = savedProgress(saved);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
360
2101
|
this.#highestFilesCompleted = update.filesCompleted;
|
|
361
|
-
this.#
|
|
2102
|
+
this.#displayedProgress =
|
|
2103
|
+
newerProgress !== null
|
|
2104
|
+
? { ...newerProgress, update }
|
|
2105
|
+
: {
|
|
2106
|
+
update,
|
|
2107
|
+
owner: parent ? "parent" : "worker",
|
|
2108
|
+
...(parent ? provenance : { order }),
|
|
2109
|
+
};
|
|
2110
|
+
// Distinct equal-valued receipts still advance source ownership/order.
|
|
2111
|
+
if (displayed !== null && progressKey(displayed.update) === key)
|
|
2112
|
+
continue;
|
|
2113
|
+
this.#options.onProgress(update, parent ? { reconciled: true } : undefined);
|
|
362
2114
|
}
|
|
363
2115
|
}
|
|
364
2116
|
#reportCost(cost, complete = true) {
|
|
@@ -402,13 +2154,16 @@ export async function* sessionFiles(directory, compressed = false) {
|
|
|
402
2154
|
}
|
|
403
2155
|
}
|
|
404
2156
|
}
|
|
405
|
-
async function readSessionUsage(path, session, repository) {
|
|
2157
|
+
async function readSessionUsage(path, session, repository, progressThread, throughOffset) {
|
|
406
2158
|
if (session.unreadable)
|
|
407
2159
|
return;
|
|
408
2160
|
let file;
|
|
409
2161
|
try {
|
|
410
|
-
|
|
2162
|
+
const size = (await stat(path)).size;
|
|
2163
|
+
if (size === session.offset)
|
|
411
2164
|
return;
|
|
2165
|
+
if (throughOffset === "file-size")
|
|
2166
|
+
throughOffset = size;
|
|
412
2167
|
file = await open(path, "r");
|
|
413
2168
|
}
|
|
414
2169
|
catch (error) {
|
|
@@ -418,8 +2173,10 @@ async function readSessionUsage(path, session, repository) {
|
|
|
418
2173
|
}
|
|
419
2174
|
try {
|
|
420
2175
|
const buffer = Buffer.alloc(SESSION_READ_SIZE);
|
|
421
|
-
while (
|
|
422
|
-
const { bytesRead } = await file.read(buffer, 0,
|
|
2176
|
+
while (throughOffset === undefined || session.offset < throughOffset) {
|
|
2177
|
+
const { bytesRead } = await file.read(buffer, 0, throughOffset === undefined
|
|
2178
|
+
? buffer.length
|
|
2179
|
+
: Math.min(buffer.length, throughOffset - session.offset), session.offset);
|
|
423
2180
|
if (bytesRead === 0)
|
|
424
2181
|
return;
|
|
425
2182
|
session.offset += bytesRead;
|
|
@@ -429,7 +2186,7 @@ async function readSessionUsage(path, session, repository) {
|
|
|
429
2186
|
.split("\n");
|
|
430
2187
|
session.pendingLine.push(lines[0]);
|
|
431
2188
|
for (const line of lines.slice(1)) {
|
|
432
|
-
readSessionEvent(session.pendingLine.join(""), session, repository);
|
|
2189
|
+
readSessionEvent(session.pendingLine.join(""), session, repository, progressThread);
|
|
433
2190
|
session.pendingLine = [line];
|
|
434
2191
|
}
|
|
435
2192
|
}
|
|
@@ -444,7 +2201,7 @@ async function readSessionUsage(path, session, repository) {
|
|
|
444
2201
|
await file.close();
|
|
445
2202
|
}
|
|
446
2203
|
}
|
|
447
|
-
function readSessionEvent(line, session, repository) {
|
|
2204
|
+
function readSessionEvent(line, session, repository, progressThread) {
|
|
448
2205
|
if (line.length === 0)
|
|
449
2206
|
return;
|
|
450
2207
|
const event = parseJson(() => line);
|
|
@@ -485,6 +2242,7 @@ function readSessionEvent(line, session, repository) {
|
|
|
485
2242
|
return;
|
|
486
2243
|
}
|
|
487
2244
|
session.events?.push(event);
|
|
2245
|
+
const position = ++session.position;
|
|
488
2246
|
if (event["type"] === "token_usage_record") {
|
|
489
2247
|
const responseId = payload["response_id"];
|
|
490
2248
|
if (payload["thread_id"] !== session.threadId ||
|
|
@@ -511,7 +2269,34 @@ function readSessionEvent(line, session, repository) {
|
|
|
511
2269
|
return;
|
|
512
2270
|
}
|
|
513
2271
|
if (event["type"] === "response_item") {
|
|
514
|
-
|
|
2272
|
+
const receipt = payload["type"] === "function_call_output" ||
|
|
2273
|
+
payload["type"] === "custom_tool_call_output" ||
|
|
2274
|
+
payload["type"] === "local_shell_call_output";
|
|
2275
|
+
const message = payload["type"] === "message" &&
|
|
2276
|
+
payload["role"] === "assistant" &&
|
|
2277
|
+
Array.isArray(payload["content"])
|
|
2278
|
+
? payload["content"]
|
|
2279
|
+
.filter(isRecord)
|
|
2280
|
+
.map((part) => typeof part["text"] === "string" ? part["text"] : "")
|
|
2281
|
+
.join("")
|
|
2282
|
+
: null;
|
|
2283
|
+
const updates = sessionProgressUpdates(payload);
|
|
2284
|
+
const duplicateMessage = message !== null &&
|
|
2285
|
+
repeatedMessageProjection(session, digest(message), "response", updates);
|
|
2286
|
+
// Code-mode notifications and the final output can share a call ID.
|
|
2287
|
+
// Each appended output record remains a distinct completion receipt.
|
|
2288
|
+
for (const [markerIndex, update] of (duplicateMessage
|
|
2289
|
+
? []
|
|
2290
|
+
: updates).entries()) {
|
|
2291
|
+
session.progress.push({
|
|
2292
|
+
update,
|
|
2293
|
+
position,
|
|
2294
|
+
completion: false,
|
|
2295
|
+
markerIndex,
|
|
2296
|
+
receipt,
|
|
2297
|
+
duplicate: false,
|
|
2298
|
+
});
|
|
2299
|
+
}
|
|
515
2300
|
if (repository === undefined)
|
|
516
2301
|
return;
|
|
517
2302
|
if (payload["type"] === "reasoning" &&
|
|
@@ -578,6 +2363,44 @@ function readSessionEvent(line, session, repository) {
|
|
|
578
2363
|
}
|
|
579
2364
|
return;
|
|
580
2365
|
}
|
|
2366
|
+
if (session.threadId === progressThread &&
|
|
2367
|
+
event["type"] === "event_msg" &&
|
|
2368
|
+
payload["type"] === "item_completed") {
|
|
2369
|
+
const item = nativeProgressItem(payload["item"]);
|
|
2370
|
+
const signature = item?.signature;
|
|
2371
|
+
const value = payload["item"];
|
|
2372
|
+
const duplicateCompletion = signature?.kind === "message" &&
|
|
2373
|
+
repeatedMessageProjection(session, signature.outputDigest, "native");
|
|
2374
|
+
let completion;
|
|
2375
|
+
if (isRecord(value) &&
|
|
2376
|
+
(value["type"] === "CommandExecution" ||
|
|
2377
|
+
value["type"] === "AgentMessage") &&
|
|
2378
|
+
!duplicateCompletion) {
|
|
2379
|
+
completion = {
|
|
2380
|
+
position,
|
|
2381
|
+
item: signature ?? null,
|
|
2382
|
+
kind: signature?.kind,
|
|
2383
|
+
};
|
|
2384
|
+
session.completions.push(completion);
|
|
2385
|
+
}
|
|
2386
|
+
if (item !== null) {
|
|
2387
|
+
const duplicate = item.signature.kind === "message" &&
|
|
2388
|
+
repeatedMessageProjection(session, item.signature.outputDigest, "native", item.updates);
|
|
2389
|
+
for (const [markerIndex, update] of item.updates.entries()) {
|
|
2390
|
+
const saved = {
|
|
2391
|
+
update,
|
|
2392
|
+
position,
|
|
2393
|
+
completion: true,
|
|
2394
|
+
markerIndex,
|
|
2395
|
+
receipt: false,
|
|
2396
|
+
duplicate,
|
|
2397
|
+
};
|
|
2398
|
+
session.progress.push(saved);
|
|
2399
|
+
if (completion !== undefined && item.signature.kind === "command")
|
|
2400
|
+
completion.lastCommandProgress = saved;
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
}
|
|
581
2404
|
if (event["type"] === "event_msg" &&
|
|
582
2405
|
(payload["type"] === "agent_reasoning" ||
|
|
583
2406
|
payload["type"] === "agent_reasoning_delta" ||
|
|
@@ -586,7 +2409,29 @@ function readSessionEvent(line, session, repository) {
|
|
|
586
2409
|
payload["type"] === "agent_message")) {
|
|
587
2410
|
if (payload["type"] === "agent_message" &&
|
|
588
2411
|
typeof payload["message"] === "string") {
|
|
589
|
-
|
|
2412
|
+
const signature = progressSignature("message", payload["message"]);
|
|
2413
|
+
const updates = scanProgressUpdatesFromText(payload["message"]);
|
|
2414
|
+
const duplicate = repeatedMessageProjection(session, signature.outputDigest, "event", updates);
|
|
2415
|
+
// Legacy rollouts persist the finalized message instead of the native
|
|
2416
|
+
// item. It provides the same compact output signature for live matching.
|
|
2417
|
+
if (session.threadId === progressThread &&
|
|
2418
|
+
!repeatedMessageProjection(session, signature.outputDigest, "event"))
|
|
2419
|
+
session.completions.push({
|
|
2420
|
+
position,
|
|
2421
|
+
item: signature,
|
|
2422
|
+
});
|
|
2423
|
+
for (const [markerIndex, update] of (duplicate
|
|
2424
|
+
? []
|
|
2425
|
+
: updates).entries()) {
|
|
2426
|
+
session.progress.push({
|
|
2427
|
+
update,
|
|
2428
|
+
position,
|
|
2429
|
+
completion: false,
|
|
2430
|
+
markerIndex,
|
|
2431
|
+
receipt: false,
|
|
2432
|
+
duplicate: false,
|
|
2433
|
+
});
|
|
2434
|
+
}
|
|
590
2435
|
}
|
|
591
2436
|
if (repository === undefined)
|
|
592
2437
|
return;
|
|
@@ -696,6 +2541,242 @@ function recordReasoningActivity(session, activity) {
|
|
|
696
2541
|
session.prose.add(`${activity.kind}:${activity.description}`);
|
|
697
2542
|
session.activities.push(activity);
|
|
698
2543
|
}
|
|
2544
|
+
const PERSISTED_OUTPUT_GAP = "\n... command output truncated for persistence ...\n";
|
|
2545
|
+
// Native Codex 0.162.1 persists command output with a 64 KiB UTF-8 budget.
|
|
2546
|
+
// This signature mirrors that representation; emitted diagnostics stay intact.
|
|
2547
|
+
const PERSISTED_COMMAND_OUTPUT_BYTES = 64 * 1024;
|
|
2548
|
+
const PERSISTED_OUTPUT_GAP_BYTES = Buffer.byteLength(PERSISTED_OUTPUT_GAP);
|
|
2549
|
+
const PERSISTED_COMMAND_PREFIX_BYTES = Math.floor((PERSISTED_COMMAND_OUTPUT_BYTES - PERSISTED_OUTPUT_GAP_BYTES) / 2);
|
|
2550
|
+
const PERSISTED_COMMAND_SUFFIX_BYTES = PERSISTED_COMMAND_OUTPUT_BYTES -
|
|
2551
|
+
PERSISTED_OUTPUT_GAP_BYTES -
|
|
2552
|
+
PERSISTED_COMMAND_PREFIX_BYTES;
|
|
2553
|
+
function digest(text) {
|
|
2554
|
+
return createHash("sha256").update(text).digest("hex");
|
|
2555
|
+
}
|
|
2556
|
+
function persistedOutputDigest(text, fullDigest) {
|
|
2557
|
+
if (Buffer.byteLength(text) <= PERSISTED_COMMAND_OUTPUT_BYTES)
|
|
2558
|
+
return fullDigest;
|
|
2559
|
+
const bytes = Buffer.from(text);
|
|
2560
|
+
let prefixEnd = PERSISTED_COMMAND_PREFIX_BYTES;
|
|
2561
|
+
let suffixStart = bytes.length - PERSISTED_COMMAND_SUFFIX_BYTES;
|
|
2562
|
+
while ((bytes[prefixEnd] & 0xc0) === 0x80)
|
|
2563
|
+
prefixEnd--;
|
|
2564
|
+
while ((bytes[suffixStart] & 0xc0) === 0x80)
|
|
2565
|
+
suffixStart++;
|
|
2566
|
+
return createHash("sha256")
|
|
2567
|
+
.update(bytes.subarray(0, prefixEnd))
|
|
2568
|
+
.update(PERSISTED_OUTPUT_GAP)
|
|
2569
|
+
.update(bytes.subarray(suffixStart))
|
|
2570
|
+
.digest("hex");
|
|
2571
|
+
}
|
|
2572
|
+
function isInventoryProgress(progress) {
|
|
2573
|
+
return progress.phase === "discovery" && progress.filesCompleted === 0;
|
|
2574
|
+
}
|
|
2575
|
+
function progressSignature(kind, text, command) {
|
|
2576
|
+
const outputDigest = digest(text);
|
|
2577
|
+
return {
|
|
2578
|
+
kind,
|
|
2579
|
+
outputDigest,
|
|
2580
|
+
persistedOutputDigest: kind === "command"
|
|
2581
|
+
? persistedOutputDigest(text, outputDigest)
|
|
2582
|
+
: outputDigest,
|
|
2583
|
+
...(command === undefined
|
|
2584
|
+
? {}
|
|
2585
|
+
: { commandDigest: digest(JSON.stringify(command)) }),
|
|
2586
|
+
};
|
|
2587
|
+
}
|
|
2588
|
+
function repeatedMessageProjection(session, textDigest, projection, updates) {
|
|
2589
|
+
if (updates?.length === 0)
|
|
2590
|
+
return false;
|
|
2591
|
+
// Completion matching counts every native/legacy message, even without
|
|
2592
|
+
// progress. Keep those occurrences separate from parsed progress copies:
|
|
2593
|
+
// multipart responses can recover updates absent from their native copy.
|
|
2594
|
+
const key = updates === undefined
|
|
2595
|
+
? textDigest
|
|
2596
|
+
: `${textDigest}:${digest(JSON.stringify(updates))}`;
|
|
2597
|
+
if (updates !== undefined) {
|
|
2598
|
+
let queue = session.messageProgressProjections.get(key);
|
|
2599
|
+
if (queue === undefined) {
|
|
2600
|
+
queue = { occurrences: [], next: { native: 0, event: 0, response: 0 } };
|
|
2601
|
+
session.messageProgressProjections.set(key, queue);
|
|
2602
|
+
}
|
|
2603
|
+
// Copies retain their order within each projection. A missing earlier copy
|
|
2604
|
+
// cannot consume an identical message after that projection has advanced.
|
|
2605
|
+
// These ordinals identify progress copies only, never completion positions.
|
|
2606
|
+
while (queue.next[projection] < queue.occurrences.length) {
|
|
2607
|
+
const order = queue.occurrences[queue.next[projection]++];
|
|
2608
|
+
if (order <= session.messageProgressFrontiers[projection])
|
|
2609
|
+
continue;
|
|
2610
|
+
session.messageProgressFrontiers[projection] = order;
|
|
2611
|
+
return true;
|
|
2612
|
+
}
|
|
2613
|
+
const order = ++session.messageProgressOrder;
|
|
2614
|
+
queue.occurrences.push(order);
|
|
2615
|
+
queue.next[projection] = queue.occurrences.length;
|
|
2616
|
+
session.messageProgressFrontiers[projection] = order;
|
|
2617
|
+
return false;
|
|
2618
|
+
}
|
|
2619
|
+
let counts = session.messageProjections.get(key);
|
|
2620
|
+
if (counts === undefined) {
|
|
2621
|
+
counts = { native: 0, event: 0, response: 0 };
|
|
2622
|
+
session.messageProjections.set(key, counts);
|
|
2623
|
+
}
|
|
2624
|
+
const previous = Math.max(counts.native, counts.event, counts.response);
|
|
2625
|
+
counts[projection]++;
|
|
2626
|
+
return counts[projection] <= previous;
|
|
2627
|
+
}
|
|
2628
|
+
function progressKey(progress) {
|
|
2629
|
+
return `${progress.phase}:${progress.filesCompleted}:${progress.filesTotal}`;
|
|
2630
|
+
}
|
|
2631
|
+
function streamedProgressItem(event) {
|
|
2632
|
+
const item = event["item"];
|
|
2633
|
+
if (!isRecord(item))
|
|
2634
|
+
return null;
|
|
2635
|
+
if (item["type"] === "agent_message" && typeof item["text"] === "string")
|
|
2636
|
+
return progressSignature("message", item["text"]);
|
|
2637
|
+
if (item["type"] !== "command_execution" ||
|
|
2638
|
+
typeof item["aggregated_output"] !== "string" ||
|
|
2639
|
+
typeof item["command"] !== "string")
|
|
2640
|
+
return null;
|
|
2641
|
+
const command = splitCommand(item["command"]);
|
|
2642
|
+
return command === null
|
|
2643
|
+
? null
|
|
2644
|
+
: progressSignature("command", item["aggregated_output"], command);
|
|
2645
|
+
}
|
|
2646
|
+
function nativeProgressItem(value) {
|
|
2647
|
+
if (!isRecord(value))
|
|
2648
|
+
return null;
|
|
2649
|
+
let kind;
|
|
2650
|
+
let text;
|
|
2651
|
+
let command;
|
|
2652
|
+
if (value["type"] === "AgentMessage" && Array.isArray(value["content"])) {
|
|
2653
|
+
kind = "message";
|
|
2654
|
+
text = value["content"]
|
|
2655
|
+
.filter((part) => isRecord(part) &&
|
|
2656
|
+
part["type"] === "Text" &&
|
|
2657
|
+
typeof part["text"] === "string")
|
|
2658
|
+
.map((part) => part.text)
|
|
2659
|
+
.join("");
|
|
2660
|
+
}
|
|
2661
|
+
else if (value["type"] === "CommandExecution" &&
|
|
2662
|
+
typeof value["aggregated_output"] === "string" &&
|
|
2663
|
+
Array.isArray(value["command"]) &&
|
|
2664
|
+
value["command"].every((arg) => typeof arg === "string")) {
|
|
2665
|
+
kind = "command";
|
|
2666
|
+
text = value["aggregated_output"];
|
|
2667
|
+
command = value["command"];
|
|
2668
|
+
}
|
|
2669
|
+
else
|
|
2670
|
+
return null;
|
|
2671
|
+
let progressText = text;
|
|
2672
|
+
if (kind === "command") {
|
|
2673
|
+
const bytes = Buffer.from(text);
|
|
2674
|
+
// Native UTF-8 boundary adjustment can shorten each fragment by up to
|
|
2675
|
+
// three bytes. An earlier literal sentinel is ordinary command output.
|
|
2676
|
+
const gap = bytes.indexOf(PERSISTED_OUTPUT_GAP, PERSISTED_COMMAND_PREFIX_BYTES - 3);
|
|
2677
|
+
const suffixBytes = bytes.length - gap - PERSISTED_OUTPUT_GAP_BYTES;
|
|
2678
|
+
if (gap !== -1 &&
|
|
2679
|
+
gap <= PERSISTED_COMMAND_PREFIX_BYTES &&
|
|
2680
|
+
suffixBytes >= PERSISTED_COMMAND_SUFFIX_BYTES - 3 &&
|
|
2681
|
+
suffixBytes <= PERSISTED_COMMAND_SUFFIX_BYTES) {
|
|
2682
|
+
// Complete original prefix lines have known fence state. The gap inserts
|
|
2683
|
+
// its own newline, so the partial prefix line and tail remain unproven.
|
|
2684
|
+
// Literal text at these exact boundaries is indistinguishable on disk.
|
|
2685
|
+
const prefix = bytes.subarray(0, gap).toString("utf8");
|
|
2686
|
+
progressText = prefix.slice(0, prefix.lastIndexOf("\n") + 1);
|
|
2687
|
+
}
|
|
2688
|
+
}
|
|
2689
|
+
return {
|
|
2690
|
+
signature: progressSignature(kind, text, command),
|
|
2691
|
+
updates: scanProgressUpdatesFromText(progressText),
|
|
2692
|
+
};
|
|
2693
|
+
}
|
|
2694
|
+
function progressItemKey(item, persisted = false) {
|
|
2695
|
+
return `${item.kind}:${item.commandDigest ?? ""}:${persisted ? item.persistedOutputDigest : item.outputDigest}`;
|
|
2696
|
+
}
|
|
2697
|
+
function progressItemKeys(item) {
|
|
2698
|
+
const keys = [progressItemKey(item)];
|
|
2699
|
+
if (item.outputDigest !== item.persistedOutputDigest)
|
|
2700
|
+
keys.push(progressItemKey(item, true));
|
|
2701
|
+
return keys;
|
|
2702
|
+
}
|
|
2703
|
+
// Min-heap keeps compatible live occurrences ordered without rescanning the
|
|
2704
|
+
// unmatched prefix or sorting every completion batch.
|
|
2705
|
+
function insertIndex(heap, index) {
|
|
2706
|
+
let child = heap.length;
|
|
2707
|
+
heap.push(index);
|
|
2708
|
+
while (child > 0) {
|
|
2709
|
+
const parent = (child - 1) >>> 1;
|
|
2710
|
+
if (heap[parent] <= index)
|
|
2711
|
+
break;
|
|
2712
|
+
heap[child] = heap[parent];
|
|
2713
|
+
child = parent;
|
|
2714
|
+
}
|
|
2715
|
+
heap[child] = index;
|
|
2716
|
+
}
|
|
2717
|
+
function takeFirstIndex(heap) {
|
|
2718
|
+
const first = heap[0];
|
|
2719
|
+
const last = heap.pop();
|
|
2720
|
+
if (heap.length === 0)
|
|
2721
|
+
return first;
|
|
2722
|
+
let parent = 0;
|
|
2723
|
+
while (parent * 2 + 1 < heap.length) {
|
|
2724
|
+
let child = parent * 2 + 1;
|
|
2725
|
+
if (child + 1 < heap.length && heap[child + 1] < heap[child])
|
|
2726
|
+
child++;
|
|
2727
|
+
if (last <= heap[child])
|
|
2728
|
+
break;
|
|
2729
|
+
heap[parent] = heap[child];
|
|
2730
|
+
parent = child;
|
|
2731
|
+
}
|
|
2732
|
+
heap[parent] = last;
|
|
2733
|
+
return first;
|
|
2734
|
+
}
|
|
2735
|
+
/** Decode the quoted argv rendering emitted by native command completions. */
|
|
2736
|
+
function splitCommand(command) {
|
|
2737
|
+
const words = [];
|
|
2738
|
+
let word = "";
|
|
2739
|
+
let started = false;
|
|
2740
|
+
let quote = null;
|
|
2741
|
+
for (let index = 0; index < command.length; index++) {
|
|
2742
|
+
const character = command[index];
|
|
2743
|
+
if (character === "\\" && quote !== "'") {
|
|
2744
|
+
const next = command[++index];
|
|
2745
|
+
if (next === undefined)
|
|
2746
|
+
return null;
|
|
2747
|
+
if (quote === '"' && !["$", "`", '"', "\\", "\n"].includes(next))
|
|
2748
|
+
word += "\\";
|
|
2749
|
+
if (next !== "\n")
|
|
2750
|
+
word += next;
|
|
2751
|
+
started = true;
|
|
2752
|
+
}
|
|
2753
|
+
else if (quote !== null) {
|
|
2754
|
+
if (character === quote)
|
|
2755
|
+
quote = null;
|
|
2756
|
+
else
|
|
2757
|
+
word += character;
|
|
2758
|
+
}
|
|
2759
|
+
else if (character === "'" || character === '"') {
|
|
2760
|
+
quote = character;
|
|
2761
|
+
started = true;
|
|
2762
|
+
}
|
|
2763
|
+
else if (" \t\r\n".includes(character)) {
|
|
2764
|
+
if (started)
|
|
2765
|
+
words.push(word);
|
|
2766
|
+
word = "";
|
|
2767
|
+
started = false;
|
|
2768
|
+
}
|
|
2769
|
+
else {
|
|
2770
|
+
word += character;
|
|
2771
|
+
started = true;
|
|
2772
|
+
}
|
|
2773
|
+
}
|
|
2774
|
+
if (quote !== null)
|
|
2775
|
+
return null;
|
|
2776
|
+
if (started)
|
|
2777
|
+
words.push(word);
|
|
2778
|
+
return words;
|
|
2779
|
+
}
|
|
699
2780
|
function sessionProgressUpdates(payload) {
|
|
700
2781
|
if (payload["type"] === "message" && payload["role"] === "assistant") {
|
|
701
2782
|
return scanProgressUpdatesFromText(sessionContentText(payload["content"]));
|