opencode-agent-orchestration-kit 1.0.27
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/.github/CODEOWNERS +1 -0
- package/.github/ISSUE_TEMPLATE/bug_report.yml +65 -0
- package/.github/ISSUE_TEMPLATE/config.yml +11 -0
- package/.github/ISSUE_TEMPLATE/documentation.yml +22 -0
- package/.github/ISSUE_TEMPLATE/feature_request.yml +43 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +25 -0
- package/.github/workflows/check.yml +47 -0
- package/CODE_OF_CONDUCT.md +24 -0
- package/CONTRIBUTING.md +52 -0
- package/LICENSE +201 -0
- package/NOTICE.md +23 -0
- package/README.md +806 -0
- package/SECURITY.md +17 -0
- package/docker/open-design/.env.example +6 -0
- package/docker/open-design/Dockerfile +50 -0
- package/docker/open-design/docker-compose.yml +18 -0
- package/docker/open-design/opencode-od/agents/od-worker.md +22 -0
- package/docker/open-design/opencode-od/opencode.json +24 -0
- package/docs/agents.md +21 -0
- package/docs/commands.md +16 -0
- package/docs/configuration.md +30 -0
- package/docs/docker-open-design.md +42 -0
- package/docs/impeccable.md +18 -0
- package/docs/installation.md +176 -0
- package/docs/models.md +17 -0
- package/docs/open-design.md +25 -0
- package/docs/quickstart.md +74 -0
- package/docs/releases/v1.0.10.md +46 -0
- package/docs/releases/v1.0.14.md +39 -0
- package/docs/releases/v1.0.27.md +66 -0
- package/docs/security.md +8 -0
- package/docs/superpowers/plans/2026-07-20-safe-installation-lifecycle.md +779 -0
- package/docs/superpowers/plans/2026-07-20-version-identity-and-releases.md +590 -0
- package/docs/superpowers/specs/2026-07-20-safe-installation-lifecycle-design.md +1350 -0
- package/docs/superpowers/specs/2026-07-20-version-identity-and-releases-design.md +557 -0
- package/docs/superpowers.md +34 -0
- package/docs/synology.md +30 -0
- package/docs/troubleshooting.md +54 -0
- package/docs/workflows.md +44 -0
- package/doctor.sh +5 -0
- package/env.example +15 -0
- package/install.sh +5 -0
- package/opencode/AGENTS.md +104 -0
- package/opencode/agents/debugger.md +41 -0
- package/opencode/agents/designer.md +93 -0
- package/opencode/agents/developer.md +180 -0
- package/opencode/agents/evaluator.md +35 -0
- package/opencode/agents/evolver.md +31 -0
- package/opencode/agents/lead.md +251 -0
- package/opencode/agents/researcher.md +76 -0
- package/opencode/agents/review_api.md +34 -0
- package/opencode/agents/review_coordinator.md +111 -0
- package/opencode/agents/review_quality.md +35 -0
- package/opencode/agents/review_security.md +35 -0
- package/opencode/agents/review_tests.md +37 -0
- package/opencode/agents/reviewer.md +116 -0
- package/opencode/agents/scoper.md +75 -0
- package/opencode/agents/specifier.md +116 -0
- package/opencode/commands/code-simplify.md +36 -0
- package/opencode/commands/design.md +27 -0
- package/opencode/commands/evolve.md +165 -0
- package/opencode/commands/feature.md +67 -0
- package/opencode/commands/implement.md +11 -0
- package/opencode/commands/init.md +34 -0
- package/opencode/commands/loop.md +145 -0
- package/opencode/commands/mvp-spec.md +20 -0
- package/opencode/commands/plan.md +74 -0
- package/opencode/commands/research.md +12 -0
- package/opencode/commands/review-orchestrated.md +47 -0
- package/opencode/commands/review-preflight.md +26 -0
- package/opencode/commands/review.md +18 -0
- package/opencode/commands/scope.md +36 -0
- package/opencode/commands/spec.md +12 -0
- package/opencode/commands/test.md +40 -0
- package/opencode/docs/ai/evolution/README.md +135 -0
- package/opencode/docs/ai/evolution/benchmarks/manual-scenarios.md +123 -0
- package/opencode/docs/ai/evolution/benchmarks/router-scenarios.jsonl +8 -0
- package/opencode/docs/ai/evolution/evolution_history.md +83 -0
- package/opencode/docs/ai/evolution/mechanisms.jsonl +7 -0
- package/opencode/docs/ai/evolution/rejected_mechanisms.jsonl +2 -0
- package/opencode/docs/ai/evolution/runs/README.md +15 -0
- package/opencode/docs/ai/evolution/runs/iteration-001-sidecar/analysis/overview.md +12 -0
- package/opencode/docs/ai/evolution/runs/iteration-001-sidecar/change_evaluation.json +108 -0
- package/opencode/docs/ai/evolution/runs/iteration-001-sidecar/change_manifest.json +52 -0
- package/opencode/docs/ai/evolution/runs/iteration-001-sidecar/evaluation.md +19 -0
- package/opencode/docs/ai/evolution/runs/iteration-001-sidecar/post_evaluation.md +12 -0
- package/opencode/docs/ai/evolution/runs/iteration-002-feature-flow-fix/analysis/overview.md +7 -0
- package/opencode/docs/ai/evolution/runs/iteration-002-feature-flow-fix/change_evaluation.json +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-002-feature-flow-fix/evaluation.md +19 -0
- package/opencode/docs/ai/evolution/runs/iteration-003-agent-readable-harness/analysis/overview.md +13 -0
- package/opencode/docs/ai/evolution/runs/iteration-003-agent-readable-harness/change_evaluation.json +66 -0
- package/opencode/docs/ai/evolution/runs/iteration-003-agent-readable-harness/change_manifest.json +82 -0
- package/opencode/docs/ai/evolution/runs/iteration-003-agent-readable-harness/evaluation.md +20 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/analysis/detail/ahe-run-lifecycle-validator.md +40 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/analysis/detail/evolve-evaluation-only-contract.md +15 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/analysis/detail/open-design-tool-contract.md +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/analysis/detail/slash-command-contract-coverage.md +40 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/analysis/overview.md +17 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/analysis/pre_apply_approval.md +15 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/change_evaluation.json +252 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/change_manifest.json +210 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/evaluation.md +25 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/post_apply_evaluation.md +15 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/post_manifest_evaluation.md +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-004-orchestration-audit/pre_apply_evaluation.md +15 -0
- package/opencode/docs/ai/evolution/runs/iteration-005-harness-orchestration-audit/analysis/overview.md +13 -0
- package/opencode/docs/ai/evolution/runs/iteration-005-harness-orchestration-audit/change_evaluation.json +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-005-harness-orchestration-audit/change_manifest.json +30 -0
- package/opencode/docs/ai/evolution/runs/iteration-005-harness-orchestration-audit/evaluation.md +25 -0
- package/opencode/docs/ai/evolution/runs/iteration-006-lead-delegation-audit/analysis/overview.md +13 -0
- package/opencode/docs/ai/evolution/runs/iteration-006-lead-delegation-audit/change_evaluation.json +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-006-lead-delegation-audit/change_manifest.json +29 -0
- package/opencode/docs/ai/evolution/runs/iteration-006-lead-delegation-audit/evaluation.md +18 -0
- package/opencode/docs/ai/evolution/runs/iteration-007-agent-skills-integration/analysis/overview.md +13 -0
- package/opencode/docs/ai/evolution/runs/iteration-007-agent-skills-integration/change_evaluation.json +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-007-agent-skills-integration/change_manifest.json +34 -0
- package/opencode/docs/ai/evolution/runs/iteration-007-agent-skills-integration/evaluation.md +17 -0
- package/opencode/docs/ai/evolution/runs/iteration-008-lead-readonly-shell-permissions/analysis/overview.md +13 -0
- package/opencode/docs/ai/evolution/runs/iteration-008-lead-readonly-shell-permissions/change_evaluation.json +12 -0
- package/opencode/docs/ai/evolution/runs/iteration-008-lead-readonly-shell-permissions/change_manifest.json +28 -0
- package/opencode/docs/ai/evolution/runs/iteration-008-lead-readonly-shell-permissions/evaluation.md +17 -0
- package/opencode/docs/ai/evolution/runs/iteration-009-lead-developer-ownership-loop/analysis/overview.md +9 -0
- package/opencode/docs/ai/evolution/runs/iteration-009-lead-developer-ownership-loop/change_evaluation.json +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-009-lead-developer-ownership-loop/change_manifest.json +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-009-lead-developer-ownership-loop/evaluation.md +18 -0
- package/opencode/docs/ai/evolution/runs/iteration-010-token-plugin-sidebar-content/analysis/overview.md +12 -0
- package/opencode/docs/ai/evolution/runs/iteration-010-token-plugin-sidebar-content/change_evaluation.json +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-010-token-plugin-sidebar-content/change_manifest.json +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-010-token-plugin-sidebar-content/evaluation.md +17 -0
- package/opencode/docs/ai/evolution/runs/iteration-011-lead-cd-which-permissions/analysis/overview.md +10 -0
- package/opencode/docs/ai/evolution/runs/iteration-011-lead-cd-which-permissions/change_evaluation.json +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-011-lead-cd-which-permissions/change_manifest.json +30 -0
- package/opencode/docs/ai/evolution/runs/iteration-011-lead-cd-which-permissions/evaluation.md +17 -0
- package/opencode/docs/ai/evolution/runs/iteration-012-lead-shell-allowlist-boundaries/analysis/overview.md +25 -0
- package/opencode/docs/ai/evolution/runs/iteration-012-lead-shell-allowlist-boundaries/change_evaluation.json +28 -0
- package/opencode/docs/ai/evolution/runs/iteration-012-lead-shell-allowlist-boundaries/change_manifest.json +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-012-lead-shell-allowlist-boundaries/evaluation.md +22 -0
- package/opencode/docs/ai/evolution/runs/iteration-013-doc-gardening-mechanical-checks/analysis/overview.md +27 -0
- package/opencode/docs/ai/evolution/runs/iteration-013-doc-gardening-mechanical-checks/change_evaluation.json +32 -0
- package/opencode/docs/ai/evolution/runs/iteration-013-doc-gardening-mechanical-checks/change_manifest.json +49 -0
- package/opencode/docs/ai/evolution/runs/iteration-013-doc-gardening-mechanical-checks/evaluation.md +92 -0
- package/opencode/docs/ai/evolution/runs/iteration-014-harness-baseline/analysis/detail/token-vs-semantic.md +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-014-harness-baseline/analysis/overview.md +14 -0
- package/opencode/docs/ai/evolution/runs/iteration-014-harness-baseline/evaluation.md +36 -0
- package/opencode/docs/ai/evolution/runs/iteration-015-semantic-checker/analysis/overview.md +13 -0
- package/opencode/docs/ai/evolution/runs/iteration-015-semantic-checker/change_evaluation.json +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-015-semantic-checker/change_manifest.json +47 -0
- package/opencode/docs/ai/evolution/runs/iteration-015-semantic-checker/evaluation.md +27 -0
- package/opencode/docs/ai/evolution/runs/iteration-016-contract-prompt-alignment/analysis/overview.md +32 -0
- package/opencode/docs/ai/evolution/runs/iteration-016-contract-prompt-alignment/change_evaluation.json +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-016-contract-prompt-alignment/change_manifest.json +35 -0
- package/opencode/docs/ai/evolution/runs/iteration-016-contract-prompt-alignment/evaluation.md +31 -0
- package/opencode/docs/ai/evolution/runs/iteration-017-ahe-cycle/analysis/detail/pattern-1-feature-sidecar-false-positive.md +51 -0
- package/opencode/docs/ai/evolution/runs/iteration-017-ahe-cycle/analysis/detail/pattern-2-checker-time-budget.md +52 -0
- package/opencode/docs/ai/evolution/runs/iteration-017-ahe-cycle/analysis/overview.md +99 -0
- package/opencode/docs/ai/evolution/runs/iteration-017-ahe-cycle/change_evaluation.json +61 -0
- package/opencode/docs/ai/evolution/runs/iteration-017-ahe-cycle/change_manifest.json +61 -0
- package/opencode/docs/ai/evolution/runs/iteration-017-ahe-cycle/evaluation.md +448 -0
- package/opencode/docs/ai/evolution/runs/iteration-018-free-message-routing-audit/analysis/detail/intake-gray-zone.md +65 -0
- package/opencode/docs/ai/evolution/runs/iteration-018-free-message-routing-audit/analysis/overview.md +172 -0
- package/opencode/docs/ai/evolution/runs/iteration-018-free-message-routing-audit/evaluation.md +226 -0
- package/opencode/docs/ai/evolution/runs/iteration-019-handoff-routing-audit/analysis/detail/plan-only-gray-zone.md +62 -0
- package/opencode/docs/ai/evolution/runs/iteration-019-handoff-routing-audit/analysis/overview.md +167 -0
- package/opencode/docs/ai/evolution/runs/iteration-019-handoff-routing-audit/evaluation.md +163 -0
- package/opencode/docs/ai/evolution/runs/iteration-020-loop-engineering-command/analysis/overview.md +42 -0
- package/opencode/docs/ai/evolution/runs/iteration-020-loop-engineering-command/change_evaluation.json +46 -0
- package/opencode/docs/ai/evolution/runs/iteration-020-loop-engineering-command/change_manifest.json +57 -0
- package/opencode/docs/ai/evolution/runs/iteration-020-loop-engineering-command/evaluation.md +48 -0
- package/opencode/docs/ai/evolution/runs/iteration-021/analysis/detail/contract-vs-runtime-gap.md +163 -0
- package/opencode/docs/ai/evolution/runs/iteration-021/analysis/detail/evidence-collection-blocker.md +134 -0
- package/opencode/docs/ai/evolution/runs/iteration-021/analysis/overview.md +111 -0
- package/opencode/docs/ai/evolution/runs/iteration-021/change_evaluation.json +52 -0
- package/opencode/docs/ai/evolution/runs/iteration-021/change_manifest.json +50 -0
- package/opencode/docs/ai/evolution/runs/iteration-021/evaluation.md +46 -0
- package/opencode/docs/ai/evolution/runs/iteration-021/manifest-justification.md +48 -0
- package/opencode/docs/ai/evolution/session-sources.md +91 -0
- package/opencode/docs/ai/harness/README.md +32 -0
- package/opencode/docs/ai/harness/agents.md +148 -0
- package/opencode/docs/ai/harness/checks.md +119 -0
- package/opencode/docs/ai/harness/commands.md +391 -0
- package/opencode/docs/ai/harness/evidence.md +85 -0
- package/opencode/docs/ai/harness/init-detection-rules.md +160 -0
- package/opencode/docs/ai/harness/orchestrated-review.md +131 -0
- package/opencode/docs/ai/harness/skill-metadata-schema.md +198 -0
- package/opencode/docs/ai/harness/skill-resolution.md +202 -0
- package/opencode/docs/ai/harness/skill_registry.json +443 -0
- package/opencode/docs/ai/harness/skill_registry.md +43 -0
- package/opencode/docs/ai/project-context-template.md +53 -0
- package/opencode/opencode.json +38 -0
- package/opencode/package-lock.json +1717 -0
- package/opencode/package.json +11 -0
- package/opencode/plugins/token-tree-usage.tsx +206 -0
- package/opencode/references/performance-checklist.md +153 -0
- package/opencode/references/security-checklist.md +134 -0
- package/opencode/references/testing-patterns.md +236 -0
- package/opencode/scripts/check-harness.mjs +1572 -0
- package/opencode/scripts/check-harness.test.mjs +820 -0
- package/opencode/scripts/collect-session-evidence.mjs +612 -0
- package/opencode/scripts/collect-session-evidence.test.mjs +443 -0
- package/opencode/scripts/preflight-audit.mjs +497 -0
- package/opencode/scripts/preflight-audit.test.mjs +363 -0
- package/opencode/scripts/project-capability-snapshot.mjs +407 -0
- package/opencode/scripts/project-capability-snapshot.test.mjs +382 -0
- package/opencode/scripts/review-orchestrated-prepare.mjs +547 -0
- package/opencode/scripts/review-orchestrated-prepare.test.mjs +516 -0
- package/opencode/scripts/skill-resolver.mjs +585 -0
- package/opencode/scripts/skill-resolver.test.mjs +781 -0
- package/opencode/scripts/update-skill-registry.mjs +380 -0
- package/opencode/skills/api-and-interface-design/SKILL.md +315 -0
- package/opencode/skills/autonomous-loops/SKILL.md +312 -0
- package/opencode/skills/code-review-and-quality/SKILL.md +365 -0
- package/opencode/skills/code-simplification/SKILL.md +345 -0
- package/opencode/skills/context-engineering/SKILL.md +252 -0
- package/opencode/skills/debugging-and-error-recovery/SKILL.md +316 -0
- package/opencode/skills/documentation-and-adrs/SKILL.md +298 -0
- package/opencode/skills/doubt-driven-development/SKILL.md +168 -0
- package/opencode/skills/iterative-retrieval/SKILL.md +232 -0
- package/opencode/skills/open-design/SKILL.md +123 -0
- package/opencode/skills/performance-optimization/SKILL.md +365 -0
- package/opencode/skills/security-and-hardening/SKILL.md +369 -0
- package/opencode/skills/source-driven-development/SKILL.md +210 -0
- package/opencode/skills/test-driven-development/SKILL.md +406 -0
- package/opencode/skills/using-agent-skills/SKILL.md +194 -0
- package/opencode/skills/verification-loop/SKILL.md +162 -0
- package/opencode/tools/open_design.ts +361 -0
- package/opencode/tui.json +5 -0
- package/package.json +20 -0
- package/rollback.sh +5 -0
- package/scripts/check.sh +184 -0
- package/scripts/install-smoke.sh +111 -0
- package/scripts/manage-installation.mjs +1498 -0
- package/scripts/manage-installation.test.mjs +1936 -0
- package/scripts/version.mjs +197 -0
- package/scripts/version.test.mjs +195 -0
- package/uninstall.sh +5 -0
- package/upgrade.sh +5 -0
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
|
|
9
|
+
function fail(message) {
|
|
10
|
+
console.error(message);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Stream sqlite3 JSON output via spawn, collecting stdout chunks incrementally.
|
|
16
|
+
* This avoids the 208MB+ buffer accumulation from spawnSync.
|
|
17
|
+
*/
|
|
18
|
+
function streamSqliteJson(dbPath, sql, timeoutMs = 0) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const child = spawn("sqlite3", ["-json", dbPath, sql], { stdio: ["ignore", "pipe", "pipe"] });
|
|
21
|
+
let stdout = "";
|
|
22
|
+
let stderr = "";
|
|
23
|
+
let timer = null;
|
|
24
|
+
|
|
25
|
+
if (timeoutMs > 0) {
|
|
26
|
+
timer = setTimeout(() => {
|
|
27
|
+
child.kill("SIGTERM");
|
|
28
|
+
reject(new Error(`sqlite3 timeout for ${dbPath} after ${timeoutMs}ms`));
|
|
29
|
+
}, timeoutMs);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
child.stdout.on("data", (chunk) => {
|
|
33
|
+
stdout += chunk;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
child.stderr.on("data", (chunk) => {
|
|
37
|
+
stderr += chunk;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
child.on("error", (error) => {
|
|
41
|
+
if (timer) clearTimeout(timer);
|
|
42
|
+
reject(new Error(`sqlite3 spawn error for ${dbPath}: ${error.message}`));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
child.on("close", (code) => {
|
|
46
|
+
if (timer) clearTimeout(timer);
|
|
47
|
+
if (code !== 0) {
|
|
48
|
+
const details = stderr.trim() || stdout.trim() || `sqlite3 failed for ${dbPath}`;
|
|
49
|
+
reject(new Error(`sqlite3 runtime error for ${dbPath}: ${details}`));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
resolve(JSON.parse(stdout || "[]"));
|
|
54
|
+
} catch (error) {
|
|
55
|
+
reject(new Error(`sqlite3 JSON parse error for ${dbPath}: ${error.message}`));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function parseArgs(argv) {
|
|
62
|
+
const args = { sources: [], fullRescan: false };
|
|
63
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
64
|
+
const token = argv[i];
|
|
65
|
+
if (token === "--iteration") {
|
|
66
|
+
args.iteration = argv[++i];
|
|
67
|
+
} else if (token === "--output-dir") {
|
|
68
|
+
args.outputDir = argv[++i];
|
|
69
|
+
} else if (token === "--source") {
|
|
70
|
+
args.sources.push(argv[++i]);
|
|
71
|
+
} else if (token === "--full-rescan") {
|
|
72
|
+
args.fullRescan = true;
|
|
73
|
+
} else {
|
|
74
|
+
fail(`Unknown argument: ${token}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return args;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function defaultSources() {
|
|
81
|
+
const home = os.homedir();
|
|
82
|
+
const sources = [path.join(home, ".local", "share", "opencode", "opencode.db")];
|
|
83
|
+
const envRaw = process.env.RAW_SESSIONS_DIR;
|
|
84
|
+
const localRaw = "/Volumes/ingestion/raw-sessions/opencode";
|
|
85
|
+
const fallbackRaw = "/raw-sessions";
|
|
86
|
+
if (envRaw && fs.existsSync(envRaw)) {
|
|
87
|
+
sources.push(envRaw);
|
|
88
|
+
} else if (fs.existsSync(localRaw)) {
|
|
89
|
+
sources.push(localRaw);
|
|
90
|
+
} else if (fs.existsSync(fallbackRaw)) {
|
|
91
|
+
sources.push(fallbackRaw);
|
|
92
|
+
}
|
|
93
|
+
return sources;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function resolveOutputDir(args) {
|
|
97
|
+
if (args.outputDir) return path.resolve(args.outputDir);
|
|
98
|
+
if (args.iteration) {
|
|
99
|
+
return path.resolve("docs/ai/evolution/runs", args.iteration, "raw");
|
|
100
|
+
}
|
|
101
|
+
fail("Pass --output-dir or --iteration.");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function detectSource(sourcePath) {
|
|
105
|
+
const stat = fs.statSync(sourcePath);
|
|
106
|
+
if (stat.isFile() && path.basename(sourcePath) === "opencode.db") {
|
|
107
|
+
return "opencode-sqlite";
|
|
108
|
+
}
|
|
109
|
+
if (stat.isDirectory()) {
|
|
110
|
+
return "opencode-raw-json";
|
|
111
|
+
}
|
|
112
|
+
throw new Error(`Unsupported source: ${sourcePath}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function parseJson(text) {
|
|
116
|
+
try {
|
|
117
|
+
return { ok: true, value: JSON.parse(text) };
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return { ok: false, error };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function firstTextPart(parts) {
|
|
124
|
+
for (const part of parts) {
|
|
125
|
+
const data = parseJson(part.data);
|
|
126
|
+
if (data.ok && data.value?.type === "text" && typeof data.value.text === "string" && data.value.text.trim()) {
|
|
127
|
+
return data.value.text.trim();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function loadPreviousCursor(iteration, outputDir) {
|
|
134
|
+
if (!iteration) return null;
|
|
135
|
+
const runsDir = path.dirname(path.dirname(outputDir));
|
|
136
|
+
if (!fs.existsSync(runsDir)) return null;
|
|
137
|
+
const priorIterations = fs
|
|
138
|
+
.readdirSync(runsDir, { withFileTypes: true })
|
|
139
|
+
.filter((entry) => entry.isDirectory() && entry.name.startsWith("iteration-") && entry.name < iteration)
|
|
140
|
+
.map((entry) => entry.name)
|
|
141
|
+
.sort()
|
|
142
|
+
.reverse();
|
|
143
|
+
|
|
144
|
+
for (const candidate of priorIterations) {
|
|
145
|
+
const cursorPath = path.join(runsDir, candidate, "raw", "cursor.json");
|
|
146
|
+
if (!fs.existsSync(cursorPath)) continue;
|
|
147
|
+
const parsed = parseJson(fs.readFileSync(cursorPath, "utf8"));
|
|
148
|
+
if (!parsed.ok) continue;
|
|
149
|
+
if (
|
|
150
|
+
parsed.value?.cursor_mode === "execution_tree_incremental" &&
|
|
151
|
+
typeof parsed.value?.cursor_end_time_updated_max === "number" &&
|
|
152
|
+
typeof parsed.value?.cursor_end_root_session_id === "string"
|
|
153
|
+
) {
|
|
154
|
+
return parsed.value;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function summarizeSqlite(dbPath, previousCursor, fullRescan) {
|
|
162
|
+
const cutoff = !fullRescan && previousCursor ? previousCursor.cursor_end_time_updated_max : null;
|
|
163
|
+
const cutoffFilter = cutoff ? `AND time_created > ${Number(cutoff)}` : "";
|
|
164
|
+
|
|
165
|
+
// Session query: always full (lightweight, needed for parent resolution)
|
|
166
|
+
const sessions = await streamSqliteJson(
|
|
167
|
+
dbPath,
|
|
168
|
+
`
|
|
169
|
+
select
|
|
170
|
+
id,
|
|
171
|
+
project_id,
|
|
172
|
+
parent_id,
|
|
173
|
+
slug,
|
|
174
|
+
directory,
|
|
175
|
+
title,
|
|
176
|
+
version,
|
|
177
|
+
time_created,
|
|
178
|
+
time_updated,
|
|
179
|
+
workspace_id,
|
|
180
|
+
path,
|
|
181
|
+
agent,
|
|
182
|
+
model,
|
|
183
|
+
cost,
|
|
184
|
+
tokens_input,
|
|
185
|
+
tokens_output,
|
|
186
|
+
tokens_reasoning,
|
|
187
|
+
tokens_cache_read,
|
|
188
|
+
tokens_cache_write
|
|
189
|
+
from session
|
|
190
|
+
order by time_updated desc, id desc;
|
|
191
|
+
`,
|
|
192
|
+
300_000,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// Message query: extract only role from JSON data (reduces output from ~31MB to ~200KB)
|
|
196
|
+
// Apply time cutoff when not in full-rescan mode
|
|
197
|
+
const messageSql = fullRescan
|
|
198
|
+
? `select id, session_id, time_created, time_updated, data from message order by session_id, time_created, id;`
|
|
199
|
+
: `select id, session_id, time_created, time_updated, json_extract(data, '$.role') as role from message where 1=1 ${cutoffFilter} order by session_id, time_created, id;`;
|
|
200
|
+
const messages = await streamSqliteJson(dbPath, messageSql, 300_000);
|
|
201
|
+
|
|
202
|
+
// Part query: need full data for text extraction, but apply time cutoff to reduce rows scanned
|
|
203
|
+
const partsSql = `select id, message_id, session_id, time_created, time_updated, data from part where 1=1 ${cutoffFilter} order by session_id, time_created, id;`;
|
|
204
|
+
const parts = await streamSqliteJson(dbPath, partsSql, 300_000);
|
|
205
|
+
|
|
206
|
+
const messagesBySession = new Map();
|
|
207
|
+
for (const message of messages) {
|
|
208
|
+
const list = messagesBySession.get(message.session_id) || [];
|
|
209
|
+
list.push(message);
|
|
210
|
+
messagesBySession.set(message.session_id, list);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const partsByMessage = new Map();
|
|
214
|
+
for (const part of parts) {
|
|
215
|
+
const list = partsByMessage.get(part.message_id) || [];
|
|
216
|
+
list.push(part);
|
|
217
|
+
partsByMessage.set(part.message_id, list);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const normalized = [];
|
|
221
|
+
let skipped = 0;
|
|
222
|
+
const skipReasons = {};
|
|
223
|
+
|
|
224
|
+
for (const session of sessions) {
|
|
225
|
+
if (!session.id) {
|
|
226
|
+
skipped += 1;
|
|
227
|
+
skipReasons.missing_session_id = (skipReasons.missing_session_id || 0) + 1;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
let model = null;
|
|
231
|
+
if (typeof session.model === "string" && session.model) {
|
|
232
|
+
const parsed = parseJson(session.model);
|
|
233
|
+
model = parsed.ok ? parsed.value : { raw: session.model };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const sessionMessages = messagesBySession.get(session.id) || [];
|
|
237
|
+
const userMessages = [];
|
|
238
|
+
const assistantMessages = [];
|
|
239
|
+
for (const message of sessionMessages) {
|
|
240
|
+
// In incremental mode, role is extracted directly from SQL; in full-rescan, parse from data JSON.
|
|
241
|
+
// Cache parseJson(message.data) to avoid parsing twice when message.role is falsy (m2).
|
|
242
|
+
const parsedData = parseJson(message.data);
|
|
243
|
+
const role = message.role || (parsedData.ok ? parsedData.value?.role : null);
|
|
244
|
+
if (role === "user") userMessages.push(message);
|
|
245
|
+
if (role === "assistant") assistantMessages.push(message);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const firstUser = userMessages[0];
|
|
249
|
+
const lastAssistant = assistantMessages.at(-1);
|
|
250
|
+
const userPrompt = firstUser ? firstTextPart(partsByMessage.get(firstUser.id) || []) : null;
|
|
251
|
+
const assistantSummary = lastAssistant ? firstTextPart(partsByMessage.get(lastAssistant.id) || []) : null;
|
|
252
|
+
|
|
253
|
+
// M1: in incremental mode (cutoff active), a session created at or before the cursor has its
|
|
254
|
+
// messages/parts dropped by the SQL `time_created > cutoff` filter. The tree can still be
|
|
255
|
+
// re-activated (tree_time_updated_max > cursor) via a child updated after the cursor, so the
|
|
256
|
+
// session loads with empty messages. Mark this explicitly so consumers (debugger, humans) do
|
|
257
|
+
// not trust incomplete content: message_count becomes null (not 0) and content_filtered: true.
|
|
258
|
+
// We use session.time_created <= cutoff as the signal because the filter is on message/part
|
|
259
|
+
// time_created, and messages are created during the session — a session born before the cutoff
|
|
260
|
+
// cannot have reliable content in incremental mode.
|
|
261
|
+
const contentFiltered =
|
|
262
|
+
cutoff !== null && typeof session.time_created === "number" && session.time_created <= cutoff;
|
|
263
|
+
|
|
264
|
+
normalized.push({
|
|
265
|
+
source_label: "local-opencode-db",
|
|
266
|
+
source_format: "opencode-sqlite",
|
|
267
|
+
session_id: session.id,
|
|
268
|
+
parent_session_id: session.parent_id || null,
|
|
269
|
+
title: session.title || null,
|
|
270
|
+
agent: session.agent || null,
|
|
271
|
+
model,
|
|
272
|
+
directory: session.directory || null,
|
|
273
|
+
path: session.path || "",
|
|
274
|
+
version: session.version || null,
|
|
275
|
+
time_created: session.time_created ?? null,
|
|
276
|
+
time_updated: session.time_updated ?? null,
|
|
277
|
+
tokens: {
|
|
278
|
+
input: session.tokens_input ?? 0,
|
|
279
|
+
output: session.tokens_output ?? 0,
|
|
280
|
+
reasoning: session.tokens_reasoning ?? 0,
|
|
281
|
+
cache: {
|
|
282
|
+
read: session.tokens_cache_read ?? 0,
|
|
283
|
+
write: session.tokens_cache_write ?? 0,
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
cost: session.cost ?? 0,
|
|
287
|
+
user_prompt: userPrompt,
|
|
288
|
+
assistant_summary: assistantSummary,
|
|
289
|
+
message_count: contentFiltered ? null : sessionMessages.length,
|
|
290
|
+
content_filtered: contentFiltered,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
source: {
|
|
296
|
+
label: "local-opencode-db",
|
|
297
|
+
path: dbPath,
|
|
298
|
+
format: "opencode-sqlite",
|
|
299
|
+
discovered: sessions.length,
|
|
300
|
+
accepted: normalized.length,
|
|
301
|
+
skipped,
|
|
302
|
+
skip_reasons: skipReasons,
|
|
303
|
+
},
|
|
304
|
+
normalized,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function summarizeRawDir(dirPath) {
|
|
309
|
+
const files = fs
|
|
310
|
+
.readdirSync(dirPath)
|
|
311
|
+
.filter((file) => file.startsWith("ses_") && file.endsWith(".json"))
|
|
312
|
+
.sort();
|
|
313
|
+
|
|
314
|
+
const normalized = [];
|
|
315
|
+
const skipReasons = {};
|
|
316
|
+
let skipped = 0;
|
|
317
|
+
|
|
318
|
+
for (const file of files) {
|
|
319
|
+
const fullPath = path.join(dirPath, file);
|
|
320
|
+
const text = fs.readFileSync(fullPath, "utf8");
|
|
321
|
+
const parsed = parseJson(text);
|
|
322
|
+
if (!parsed.ok) {
|
|
323
|
+
skipped += 1;
|
|
324
|
+
skipReasons.invalid_json = (skipReasons.invalid_json || 0) + 1;
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (Array.isArray(parsed.value) && parsed.value.length === 0) {
|
|
328
|
+
skipped += 1;
|
|
329
|
+
skipReasons.empty_array = (skipReasons.empty_array || 0) + 1;
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const info = !Array.isArray(parsed.value) ? parsed.value?.info : null;
|
|
334
|
+
const sessionId = info?.id || path.basename(file, ".json");
|
|
335
|
+
if (!sessionId) {
|
|
336
|
+
skipped += 1;
|
|
337
|
+
skipReasons.missing_session_id = (skipReasons.missing_session_id || 0) + 1;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
normalized.push({
|
|
342
|
+
source_label: "external-opencode-raw",
|
|
343
|
+
source_format: "opencode-raw-json",
|
|
344
|
+
session_id: sessionId,
|
|
345
|
+
parent_session_id: null,
|
|
346
|
+
title: info?.title || null,
|
|
347
|
+
agent: info?.agent || null,
|
|
348
|
+
model: info?.model || null,
|
|
349
|
+
directory: info?.directory || null,
|
|
350
|
+
path: info?.path || "",
|
|
351
|
+
version: info?.version || null,
|
|
352
|
+
time_created: null,
|
|
353
|
+
time_updated: null,
|
|
354
|
+
tokens: info?.tokens || null,
|
|
355
|
+
cost: info?.cost ?? 0,
|
|
356
|
+
user_prompt: null,
|
|
357
|
+
assistant_summary: null,
|
|
358
|
+
message_count: null,
|
|
359
|
+
raw_path: fullPath,
|
|
360
|
+
raw_shape: Array.isArray(parsed.value) ? "array" : "object",
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return {
|
|
365
|
+
source: {
|
|
366
|
+
label: "external-opencode-raw",
|
|
367
|
+
path: dirPath,
|
|
368
|
+
format: "opencode-raw-json",
|
|
369
|
+
discovered: files.length,
|
|
370
|
+
accepted: normalized.length,
|
|
371
|
+
skipped,
|
|
372
|
+
skip_reasons: skipReasons,
|
|
373
|
+
},
|
|
374
|
+
normalized,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function sortSessions(rows) {
|
|
379
|
+
return [...rows].sort((a, b) => {
|
|
380
|
+
const timeDelta = (a.time_created ?? Number.MAX_SAFE_INTEGER) - (b.time_created ?? Number.MAX_SAFE_INTEGER);
|
|
381
|
+
if (timeDelta !== 0) return timeDelta;
|
|
382
|
+
return a.session_id.localeCompare(b.session_id);
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function resolveRootId(row, sessionMap) {
|
|
387
|
+
if (row.source_format !== "opencode-sqlite") return row.session_id;
|
|
388
|
+
if (!row.parent_session_id) return row.session_id;
|
|
389
|
+
|
|
390
|
+
const seen = new Set([row.session_id]);
|
|
391
|
+
let current = row;
|
|
392
|
+
while (current.parent_session_id) {
|
|
393
|
+
const parent = sessionMap.get(current.parent_session_id);
|
|
394
|
+
if (!parent || parent.source_format !== "opencode-sqlite") {
|
|
395
|
+
return row.session_id;
|
|
396
|
+
}
|
|
397
|
+
if (seen.has(parent.session_id)) {
|
|
398
|
+
return row.session_id;
|
|
399
|
+
}
|
|
400
|
+
seen.add(parent.session_id);
|
|
401
|
+
current = parent;
|
|
402
|
+
}
|
|
403
|
+
return current.session_id;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function treeRepresentativeSummary(sessions) {
|
|
407
|
+
const withSummary = sessions.filter((row) => row.assistant_summary);
|
|
408
|
+
if (withSummary.length === 0) return null;
|
|
409
|
+
return [...withSummary]
|
|
410
|
+
.sort((a, b) => (a.time_updated ?? 0) - (b.time_updated ?? 0) || a.session_id.localeCompare(b.session_id))
|
|
411
|
+
.at(-1).assistant_summary;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function buildExecutionTrees(sqliteRows, rawRows, previousCursor, fullRescan) {
|
|
415
|
+
const sqliteMap = new Map(sqliteRows.map((row) => [row.session_id, row]));
|
|
416
|
+
const rawBySessionId = new Map(rawRows.map((row) => [row.session_id, row]));
|
|
417
|
+
const grouped = new Map();
|
|
418
|
+
|
|
419
|
+
for (const row of sqliteRows) {
|
|
420
|
+
const rootId = resolveRootId(row, sqliteMap);
|
|
421
|
+
const list = grouped.get(rootId) || [];
|
|
422
|
+
list.push(row);
|
|
423
|
+
grouped.set(rootId, list);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const trees = [];
|
|
427
|
+
const skipReasons = {};
|
|
428
|
+
let skipped = 0;
|
|
429
|
+
let unmatchedRaw = 0;
|
|
430
|
+
|
|
431
|
+
for (const [rootId, rows] of grouped.entries()) {
|
|
432
|
+
const sortedRows = sortSessions(rows);
|
|
433
|
+
const rootSession = sqliteMap.get(rootId) || sortedRows[0];
|
|
434
|
+
const childSessions = sortedRows.filter((row) => row.session_id !== rootSession.session_id);
|
|
435
|
+
const participatingAgents = [...new Set(sortedRows.map((row) => row.agent).filter(Boolean))].sort();
|
|
436
|
+
const timeCreatedValues = sortedRows.map((row) => row.time_created).filter((value) => typeof value === "number");
|
|
437
|
+
const timeUpdatedValues = sortedRows.map((row) => row.time_updated).filter((value) => typeof value === "number");
|
|
438
|
+
const matchedRawIds = sortedRows.filter((row) => rawBySessionId.has(row.session_id)).map((row) => row.session_id);
|
|
439
|
+
|
|
440
|
+
const aggregateTokens = sortedRows.reduce(
|
|
441
|
+
(acc, row) => {
|
|
442
|
+
acc.input += row.tokens?.input ?? 0;
|
|
443
|
+
acc.output += row.tokens?.output ?? 0;
|
|
444
|
+
acc.reasoning += row.tokens?.reasoning ?? 0;
|
|
445
|
+
acc.cache.read += row.tokens?.cache?.read ?? 0;
|
|
446
|
+
acc.cache.write += row.tokens?.cache?.write ?? 0;
|
|
447
|
+
return acc;
|
|
448
|
+
},
|
|
449
|
+
{ input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
const tree = {
|
|
453
|
+
execution_tree_id: rootSession.session_id,
|
|
454
|
+
root_session_id: rootSession.session_id,
|
|
455
|
+
root_session: rootSession,
|
|
456
|
+
child_sessions: childSessions,
|
|
457
|
+
root_agent: rootSession.agent || null,
|
|
458
|
+
root_title: rootSession.title || null,
|
|
459
|
+
root_directory: rootSession.directory || null,
|
|
460
|
+
tree_time_created_min: timeCreatedValues.length ? Math.min(...timeCreatedValues) : null,
|
|
461
|
+
tree_time_updated_max: timeUpdatedValues.length ? Math.max(...timeUpdatedValues) : null,
|
|
462
|
+
participating_agents: participatingAgents,
|
|
463
|
+
session_count: sortedRows.length,
|
|
464
|
+
aggregate_tokens: aggregateTokens,
|
|
465
|
+
aggregate_cost: sortedRows.reduce((sum, row) => sum + (row.cost ?? 0), 0),
|
|
466
|
+
representative_user_prompt:
|
|
467
|
+
rootSession.user_prompt || sortedRows.find((row) => typeof row.user_prompt === "string")?.user_prompt || null,
|
|
468
|
+
representative_assistant_summary: treeRepresentativeSummary(sortedRows),
|
|
469
|
+
// M1: a tree is content_filtered when any of its sessions had content dropped by the
|
|
470
|
+
// incremental cutoff. Consumers must not trust representative prompts/summaries as complete.
|
|
471
|
+
content_filtered: sortedRows.some((row) => row.content_filtered === true),
|
|
472
|
+
session_ids: sortedRows.map((row) => row.session_id),
|
|
473
|
+
supplemental_raw_session_ids: matchedRawIds,
|
|
474
|
+
source_formats: [...new Set(["opencode-sqlite", ...matchedRawIds.map(() => "opencode-raw-json")])],
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
if (
|
|
478
|
+
!fullRescan &&
|
|
479
|
+
previousCursor &&
|
|
480
|
+
typeof tree.tree_time_updated_max === "number" &&
|
|
481
|
+
(tree.tree_time_updated_max < previousCursor.cursor_end_time_updated_max ||
|
|
482
|
+
(tree.tree_time_updated_max === previousCursor.cursor_end_time_updated_max &&
|
|
483
|
+
tree.root_session_id.localeCompare(previousCursor.cursor_end_root_session_id) <= 0))
|
|
484
|
+
) {
|
|
485
|
+
skipped += 1;
|
|
486
|
+
skipReasons.before_cursor = (skipReasons.before_cursor || 0) + 1;
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
trees.push(tree);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
for (const rawRow of rawRows) {
|
|
494
|
+
if (!sqliteMap.has(rawRow.session_id)) unmatchedRaw += 1;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
trees.sort((a, b) => {
|
|
498
|
+
const timeDelta = (a.tree_time_updated_max ?? 0) - (b.tree_time_updated_max ?? 0);
|
|
499
|
+
if (timeDelta !== 0) return timeDelta;
|
|
500
|
+
return a.root_session_id.localeCompare(b.root_session_id);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
trees,
|
|
505
|
+
treeSummary: {
|
|
506
|
+
trees_discovered: grouped.size,
|
|
507
|
+
trees_accepted: trees.length,
|
|
508
|
+
trees_skipped: skipped,
|
|
509
|
+
tree_skip_reasons: skipReasons,
|
|
510
|
+
},
|
|
511
|
+
supplementalRawSummary: {
|
|
512
|
+
discovered: rawRows.length,
|
|
513
|
+
accepted: rawRows.length,
|
|
514
|
+
matched: rawRows.length - unmatchedRaw,
|
|
515
|
+
unmatched: unmatchedRaw,
|
|
516
|
+
},
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function makeCursor(previousCursor, treeSummary, trees, supplementalRawSummary, fullRescan) {
|
|
521
|
+
const lastTree = trees.at(-1) || null;
|
|
522
|
+
return {
|
|
523
|
+
cursor_mode: "execution_tree_incremental",
|
|
524
|
+
cursor_start:
|
|
525
|
+
!fullRescan && previousCursor
|
|
526
|
+
? {
|
|
527
|
+
time_updated_max: previousCursor.cursor_end_time_updated_max,
|
|
528
|
+
root_session_id: previousCursor.cursor_end_root_session_id,
|
|
529
|
+
}
|
|
530
|
+
: null,
|
|
531
|
+
cursor_end:
|
|
532
|
+
lastTree && typeof lastTree.tree_time_updated_max === "number"
|
|
533
|
+
? {
|
|
534
|
+
time_updated_max: lastTree.tree_time_updated_max,
|
|
535
|
+
root_session_id: lastTree.root_session_id,
|
|
536
|
+
}
|
|
537
|
+
: !fullRescan && previousCursor
|
|
538
|
+
? {
|
|
539
|
+
time_updated_max: previousCursor.cursor_end_time_updated_max,
|
|
540
|
+
root_session_id: previousCursor.cursor_end_root_session_id,
|
|
541
|
+
}
|
|
542
|
+
: null,
|
|
543
|
+
cursor_end_time_updated_max:
|
|
544
|
+
lastTree && typeof lastTree.tree_time_updated_max === "number"
|
|
545
|
+
? lastTree.tree_time_updated_max
|
|
546
|
+
: previousCursor?.cursor_end_time_updated_max ?? null,
|
|
547
|
+
cursor_end_root_session_id: lastTree?.root_session_id || previousCursor?.cursor_end_root_session_id || null,
|
|
548
|
+
trees_discovered: treeSummary.trees_discovered,
|
|
549
|
+
trees_accepted: treeSummary.trees_accepted,
|
|
550
|
+
trees_skipped: treeSummary.trees_skipped,
|
|
551
|
+
tree_skip_reasons: treeSummary.tree_skip_reasons,
|
|
552
|
+
supplemental_raw_summary: supplementalRawSummary,
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export async function main(argv = process.argv.slice(2)) {
|
|
557
|
+
const args = parseArgs(argv);
|
|
558
|
+
const outputDir = resolveOutputDir(args);
|
|
559
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
560
|
+
|
|
561
|
+
const sourcePaths = args.sources.length > 0 ? args.sources.map((item) => path.resolve(item)) : defaultSources();
|
|
562
|
+
const summaries = [];
|
|
563
|
+
let sqliteRows = [];
|
|
564
|
+
let rawRows = [];
|
|
565
|
+
|
|
566
|
+
const previousCursor = args.fullRescan ? null : loadPreviousCursor(args.iteration, outputDir);
|
|
567
|
+
|
|
568
|
+
for (const sourcePath of sourcePaths) {
|
|
569
|
+
if (!fs.existsSync(sourcePath)) continue;
|
|
570
|
+
const kind = detectSource(sourcePath);
|
|
571
|
+
const result =
|
|
572
|
+
kind === "opencode-sqlite"
|
|
573
|
+
? await summarizeSqlite(sourcePath, previousCursor, args.fullRescan)
|
|
574
|
+
: summarizeRawDir(sourcePath);
|
|
575
|
+
summaries.push(result.source);
|
|
576
|
+
if (kind === "opencode-sqlite") {
|
|
577
|
+
sqliteRows = sqliteRows.concat(result.normalized);
|
|
578
|
+
} else {
|
|
579
|
+
rawRows = rawRows.concat(result.normalized);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const built = buildExecutionTrees(sqliteRows, rawRows, previousCursor, args.fullRescan);
|
|
584
|
+
const acceptedSessionIds = new Set(built.trees.flatMap((tree) => tree.session_ids));
|
|
585
|
+
const filteredSessionRows = sortSessions(sqliteRows.filter((row) => acceptedSessionIds.has(row.session_id)));
|
|
586
|
+
const cursor = makeCursor(previousCursor, built.treeSummary, built.trees, built.supplementalRawSummary, args.fullRescan);
|
|
587
|
+
|
|
588
|
+
fs.writeFileSync(
|
|
589
|
+
path.join(outputDir, "session-sources.summary.json"),
|
|
590
|
+
JSON.stringify({ sources: summaries }, null, 2),
|
|
591
|
+
"utf8",
|
|
592
|
+
);
|
|
593
|
+
fs.writeFileSync(
|
|
594
|
+
path.join(outputDir, "normalized-sessions.jsonl"),
|
|
595
|
+
`${filteredSessionRows.map((row) => JSON.stringify(row)).join("\n")}${filteredSessionRows.length ? "\n" : ""}`,
|
|
596
|
+
"utf8",
|
|
597
|
+
);
|
|
598
|
+
fs.writeFileSync(
|
|
599
|
+
path.join(outputDir, "execution-trees.jsonl"),
|
|
600
|
+
`${built.trees.map((tree) => JSON.stringify(tree)).join("\n")}${built.trees.length ? "\n" : ""}`,
|
|
601
|
+
"utf8",
|
|
602
|
+
);
|
|
603
|
+
fs.writeFileSync(path.join(outputDir, "cursor.json"), JSON.stringify(cursor, null, 2), "utf8");
|
|
604
|
+
console.log(`Collected ${built.trees.length} execution trees from ${summaries.length} sources.`);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
608
|
+
main().catch((error) => {
|
|
609
|
+
console.error(error.message);
|
|
610
|
+
process.exit(1);
|
|
611
|
+
});
|
|
612
|
+
}
|