principles-disciple 1.8.1 → 1.8.2
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/ADVANCED_CONFIG_ZH.md +97 -0
- package/AGENT_INSTALL.md +173 -0
- package/AGENT_INSTALL_EN.md +173 -0
- package/INSTALL.md +256 -0
- package/SKILL.md +63 -0
- package/docs/COMMAND_REFERENCE.md +76 -0
- package/docs/COMMAND_REFERENCE_EN.md +79 -0
- package/esbuild.config.js +75 -0
- package/openclaw.plugin.json +4 -4
- package/package.json +11 -13
- package/scripts/build-web.mjs +46 -0
- package/scripts/install-dependencies.cjs +47 -0
- package/scripts/sync-plugin.mjs +802 -0
- package/scripts/verify-build.mjs +109 -0
- package/src/agents/nocturnal-dreamer.md +152 -0
- package/src/agents/nocturnal-philosopher.md +138 -0
- package/src/agents/nocturnal-reflector.md +126 -0
- package/src/agents/nocturnal-scribe.md +164 -0
- package/src/commands/capabilities.ts +85 -0
- package/{dist/commands/context.js → src/commands/context.ts} +78 -38
- package/src/commands/evolution-status.ts +146 -0
- package/src/commands/export.ts +111 -0
- package/src/commands/focus.ts +533 -0
- package/src/commands/nocturnal-review.ts +311 -0
- package/src/commands/nocturnal-rollout.ts +763 -0
- package/src/commands/nocturnal-train.ts +1002 -0
- package/{dist/commands/pain.js → src/commands/pain.ts} +68 -49
- package/src/commands/principle-rollback.ts +27 -0
- package/{dist/commands/rollback.js → src/commands/rollback.ts} +44 -12
- package/src/commands/samples.ts +60 -0
- package/src/commands/strategy.ts +38 -0
- package/{dist/commands/thinking-os.js → src/commands/thinking-os.ts} +59 -36
- package/src/commands/workflow-debug.ts +128 -0
- package/{dist/config/defaults/runtime.js → src/config/defaults/runtime.ts} +12 -5
- package/src/config/errors.ts +163 -0
- package/{dist/config/index.d.ts → src/config/index.ts} +2 -1
- package/src/constants/diagnostician.ts +66 -0
- package/src/constants/tools.ts +62 -0
- package/src/core/adaptive-thresholds.ts +476 -0
- package/{dist/core/config-service.js → src/core/config-service.ts} +7 -4
- package/{dist/core/config.js → src/core/config.ts} +158 -46
- package/src/core/control-ui-db.ts +435 -0
- package/{dist/core/detection-funnel.js → src/core/detection-funnel.ts} +36 -21
- package/{dist/core/detection-service.js → src/core/detection-service.ts} +7 -4
- package/{dist/core/dictionary-service.js → src/core/dictionary-service.ts} +7 -4
- package/{dist/core/dictionary.js → src/core/dictionary.ts} +57 -34
- package/src/core/empathy-keyword-matcher.ts +327 -0
- package/src/core/empathy-types.ts +218 -0
- package/src/core/event-log.ts +544 -0
- package/src/core/evolution-engine.ts +612 -0
- package/src/core/evolution-logger.ts +353 -0
- package/src/core/evolution-migration.ts +77 -0
- package/src/core/evolution-reducer.ts +731 -0
- package/src/core/evolution-types.ts +456 -0
- package/src/core/external-training-contract.ts +527 -0
- package/src/core/focus-history.ts +1458 -0
- package/src/core/hygiene/tracker.ts +117 -0
- package/{dist/core/init.js → src/core/init.ts} +39 -26
- package/src/core/local-worker-routing.ts +617 -0
- package/{dist/core/migration.js → src/core/migration.ts} +18 -11
- package/src/core/model-deployment-registry.ts +722 -0
- package/src/core/model-training-registry.ts +813 -0
- package/src/core/nocturnal-arbiter.ts +706 -0
- package/src/core/nocturnal-candidate-scoring.ts +392 -0
- package/src/core/nocturnal-compliance.ts +1075 -0
- package/src/core/nocturnal-dataset.ts +668 -0
- package/src/core/nocturnal-executability.ts +428 -0
- package/src/core/nocturnal-export.ts +390 -0
- package/{dist/core/nocturnal-paths.js → src/core/nocturnal-paths.ts} +49 -23
- package/src/core/nocturnal-trajectory-extractor.ts +484 -0
- package/src/core/nocturnal-trinity.ts +1384 -0
- package/src/core/pain.ts +122 -0
- package/{dist/core/path-resolver.js → src/core/path-resolver.ts} +157 -36
- package/{dist/core/paths.js → src/core/paths.ts} +13 -4
- package/src/core/principle-training-state.ts +450 -0
- package/src/core/profile.ts +226 -0
- package/src/core/promotion-gate.ts +822 -0
- package/{dist/core/risk-calculator.js → src/core/risk-calculator.ts} +42 -16
- package/{dist/core/session-tracker.js → src/core/session-tracker.ts} +175 -62
- package/src/core/shadow-observation-registry.ts +534 -0
- package/{dist/core/system-logger.js → src/core/system-logger.ts} +9 -5
- package/src/core/thinking-models.ts +217 -0
- package/src/core/training-program.ts +630 -0
- package/src/core/trajectory-types.ts +243 -0
- package/src/core/trajectory.ts +1673 -0
- package/{dist/core/workspace-context.js → src/core/workspace-context.ts} +57 -32
- package/src/hooks/bash-risk.ts +171 -0
- package/src/hooks/edit-verification.ts +295 -0
- package/src/hooks/gate-block-helper.ts +160 -0
- package/src/hooks/gate.ts +210 -0
- package/src/hooks/gfi-gate.ts +177 -0
- package/src/hooks/lifecycle.ts +326 -0
- package/{dist/hooks/llm.js → src/hooks/llm.ts} +160 -80
- package/src/hooks/message-sanitize.ts +45 -0
- package/src/hooks/pain.ts +384 -0
- package/src/hooks/progressive-trust-gate.ts +174 -0
- package/src/hooks/prompt.ts +920 -0
- package/src/hooks/subagent.ts +207 -0
- package/src/hooks/thinking-checkpoint.ts +73 -0
- package/src/hooks/trajectory-collector.ts +290 -0
- package/src/http/principles-console-route.ts +716 -0
- package/src/i18n/commands.ts +117 -0
- package/src/index.ts +694 -0
- package/src/service/central-database.ts +831 -0
- package/src/service/control-ui-query-service.ts +888 -0
- package/src/service/evolution-query-service.ts +405 -0
- package/src/service/evolution-worker.ts +1646 -0
- package/src/service/health-query-service.ts +836 -0
- package/{dist/service/nocturnal-runtime.js → src/service/nocturnal-runtime.ts} +235 -79
- package/src/service/nocturnal-service.ts +1015 -0
- package/src/service/nocturnal-target-selector.ts +532 -0
- package/src/service/phase3-input-filter.ts +237 -0
- package/src/service/runtime-summary-service.ts +757 -0
- package/src/service/subagent-workflow/deep-reflect-workflow-manager.ts +513 -0
- package/{dist/service/subagent-workflow/empathy-observer-workflow-manager.js → src/service/subagent-workflow/empathy-observer-workflow-manager.ts} +240 -117
- package/src/service/subagent-workflow/index.ts +51 -0
- package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +856 -0
- package/src/service/subagent-workflow/runtime-direct-driver.ts +166 -0
- package/{dist/service/subagent-workflow/types.d.ts → src/service/subagent-workflow/types.ts} +137 -18
- package/src/service/subagent-workflow/workflow-store.ts +328 -0
- package/src/service/trajectory-service.ts +15 -0
- package/{dist/tools/critique-prompt.js → src/tools/critique-prompt.ts} +25 -8
- package/src/tools/deep-reflect.ts +349 -0
- package/{dist/tools/model-index.js → src/tools/model-index.ts} +33 -17
- package/src/types/event-types.ts +453 -0
- package/src/types/hygiene-types.ts +31 -0
- package/src/types/principle-tree-schema.ts +244 -0
- package/src/types/runtime-summary.ts +49 -0
- package/src/types.ts +74 -0
- package/src/utils/file-lock.ts +391 -0
- package/{dist/utils/glob-match.js → src/utils/glob-match.ts} +21 -20
- package/{dist/utils/hashing.js → src/utils/hashing.ts} +6 -4
- package/src/utils/io.ts +110 -0
- package/{dist/utils/nlp.js → src/utils/nlp.ts} +19 -12
- package/{dist/utils/plugin-logger.js → src/utils/plugin-logger.ts} +33 -8
- package/src/utils/subagent-probe.ts +94 -0
- package/templates/langs/zh/skills/pd-diagnostician/SKILL.md +70 -1
- package/templates/pain_settings.json +2 -1
- package/tests/README.md +120 -0
- package/tests/build-artifacts.test.ts +111 -0
- package/tests/commands/evolution-status.test.ts +222 -0
- package/tests/commands/evolver.test.ts +22 -0
- package/tests/commands/export.test.ts +78 -0
- package/tests/commands/nocturnal-review.test.ts +448 -0
- package/tests/commands/nocturnal-train.test.ts +97 -0
- package/tests/commands/pain.test.ts +108 -0
- package/tests/commands/samples.test.ts +65 -0
- package/tests/commands/strategy.test.ts +34 -0
- package/tests/commands/thinking-os.test.ts +88 -0
- package/tests/core/adaptive-thresholds.test.ts +261 -0
- package/tests/core/config-service.test.ts +89 -0
- package/tests/core/config.test.ts +90 -0
- package/tests/core/control-ui-db.test.ts +75 -0
- package/tests/core/core-template-guidance.test.ts +21 -0
- package/tests/core/detection-funnel.test.ts +63 -0
- package/tests/core/detection-service.test.ts +50 -0
- package/tests/core/dictionary-service.test.ts +116 -0
- package/tests/core/dictionary.test.ts +168 -0
- package/tests/core/empathy-keyword-matcher.test.ts +209 -0
- package/tests/core/event-log.test.ts +181 -0
- package/tests/core/evolution-e2e.test.ts +58 -0
- package/tests/core/evolution-engine-gate-integration.test.ts +543 -0
- package/tests/core/evolution-engine.test.ts +562 -0
- package/tests/core/evolution-logger.test.ts +148 -0
- package/tests/core/evolution-migration.test.ts +50 -0
- package/tests/core/evolution-paths.test.ts +21 -0
- package/tests/core/evolution-reducer.detector-metadata.test.ts +602 -0
- package/tests/core/evolution-reducer.test.ts +180 -0
- package/tests/core/evolution-types-loop.test.ts +48 -0
- package/tests/core/evolution-user-stories.e2e.test.ts +249 -0
- package/tests/core/external-training-contract.test.ts +463 -0
- package/tests/core/focus-history.test.ts +682 -0
- package/tests/core/init-flatten.test.ts +69 -0
- package/tests/core/init-refactor.test.ts +87 -0
- package/tests/core/init-v1.3.test.ts +46 -0
- package/tests/core/init.test.ts +190 -0
- package/tests/core/local-worker-routing.test.ts +757 -0
- package/tests/core/migration.test.ts +84 -0
- package/tests/core/model-deployment-registry.test.ts +845 -0
- package/tests/core/model-training-registry.test.ts +889 -0
- package/tests/core/nocturnal-arbiter.test.ts +494 -0
- package/tests/core/nocturnal-candidate-scoring.test.ts +400 -0
- package/tests/core/nocturnal-compliance.test.ts +646 -0
- package/tests/core/nocturnal-dataset.test.ts +892 -0
- package/tests/core/nocturnal-executability.test.ts +357 -0
- package/tests/core/nocturnal-export.test.ts +462 -0
- package/tests/core/nocturnal-reviewed-subset-comparison.test.ts +428 -0
- package/tests/core/nocturnal-trajectory-extractor.test.ts +634 -0
- package/tests/core/nocturnal-trinity.test.ts +953 -0
- package/tests/core/pain.test.ts +33 -0
- package/tests/core/path-resolver.test.ts +57 -0
- package/tests/core/paths-refactor.test.ts +42 -0
- package/tests/core/phase7-rollout-integration.test.ts +477 -0
- package/tests/core/principle-training-state.test.ts +712 -0
- package/tests/core/profile.test.ts +56 -0
- package/tests/core/promotion-gate.test.ts +556 -0
- package/tests/core/risk-calculator.test.ts +168 -0
- package/tests/core/session-tracker.test.ts +191 -0
- package/tests/core/training-program.test.ts +472 -0
- package/tests/core/trajectory.test.ts +265 -0
- package/tests/core/workspace-context-factory.test.ts +18 -0
- package/tests/core/workspace-context.test.ts +134 -0
- package/tests/fixtures/nocturnal-reviewed-subset.json +183 -0
- package/tests/fixtures/production-compatibility.test.ts +147 -0
- package/tests/fixtures/production-mock-generator.ts +282 -0
- package/tests/hooks/bash-risk-integration.test.ts +137 -0
- package/tests/hooks/bash-risk.test.ts +81 -0
- package/tests/hooks/edit-verification.test.ts +678 -0
- package/tests/hooks/gate-edit-verification-p1.test.ts +632 -0
- package/tests/hooks/gate-edit-verification.test.ts +435 -0
- package/tests/hooks/gate-pipeline-integration.test.ts +404 -0
- package/tests/hooks/gate.test.ts +271 -0
- package/tests/hooks/gfi-gate-unit.test.ts +422 -0
- package/tests/hooks/gfi-gate.test.ts +669 -0
- package/tests/hooks/lifecycle.test.ts +248 -0
- package/tests/hooks/llm.test.ts +308 -0
- package/tests/hooks/message-sanitize.test.ts +36 -0
- package/tests/hooks/pain.test.ts +141 -0
- package/tests/hooks/progressive-trust-gate.test.ts +277 -0
- package/tests/hooks/prompt.test.ts +1411 -0
- package/tests/hooks/subagent.test.ts +467 -0
- package/tests/hooks/thinking-gate.test.ts +313 -0
- package/tests/http/principles-console-route.test.ts +140 -0
- package/tests/hygiene-tracker.test.ts +77 -0
- package/tests/index.integration.test.ts +179 -0
- package/tests/index.shadow-routing.integration.test.ts +140 -0
- package/tests/index.test.ts +9 -0
- package/tests/integration/empathy-workflow-integration.test.ts +627 -0
- package/tests/service/control-ui-query-service.test.ts +121 -0
- package/tests/service/empathy-observer-workflow-manager.test.ts +176 -0
- package/tests/service/evolution-worker.test.ts +585 -0
- package/tests/service/nocturnal-runtime.test.ts +470 -0
- package/tests/service/nocturnal-service.test.ts +577 -0
- package/tests/service/nocturnal-target-selector.test.ts +615 -0
- package/tests/service/nocturnal-workflow-manager.test.ts +439 -0
- package/tests/service/phase3-input-filter.test.ts +289 -0
- package/tests/service/runtime-summary-service.test.ts +919 -0
- package/tests/task-compliance.test.ts +166 -0
- package/tests/test-utils.ts +48 -0
- package/tests/tools/critique-prompt.test.ts +260 -0
- package/tests/tools/deep-reflect.test.ts +232 -0
- package/tests/tools/model-index.test.ts +246 -0
- package/tests/ui/app.test.tsx +114 -0
- package/tests/utils/file-lock.test.ts +407 -0
- package/tests/utils/hashing.test.ts +32 -0
- package/tests/utils/io.test.ts +39 -0
- package/tests/utils/nlp.test.ts +53 -0
- package/tests/utils/plugin-logger.test.ts +156 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/ui/src/App.tsx +45 -0
- package/ui/src/api.ts +216 -0
- package/ui/src/charts.tsx +586 -0
- package/ui/src/components/ErrorState.tsx +6 -0
- package/ui/src/components/Loading.tsx +13 -0
- package/ui/src/components/ProtectedRoute.tsx +12 -0
- package/ui/src/components/Shell.tsx +91 -0
- package/ui/src/components/WorkspaceConfig.tsx +146 -0
- package/ui/src/components/index.ts +5 -0
- package/ui/src/context/auth.tsx +80 -0
- package/ui/src/context/theme.tsx +66 -0
- package/ui/src/hooks/useAutoRefresh.ts +39 -0
- package/ui/src/i18n/ui.ts +363 -0
- package/ui/src/main.tsx +16 -0
- package/ui/src/pages/EvolutionPage.tsx +352 -0
- package/ui/src/pages/FeedbackPage.tsx +140 -0
- package/ui/src/pages/GateMonitorPage.tsx +136 -0
- package/ui/src/pages/LoginPage.tsx +88 -0
- package/ui/src/pages/OverviewPage.tsx +238 -0
- package/ui/src/pages/SamplesPage.tsx +174 -0
- package/ui/src/pages/ThinkingModelsPage.tsx +127 -0
- package/ui/src/styles.css +1661 -0
- package/ui/src/types.ts +368 -0
- package/ui/src/utils/format.ts +15 -0
- package/vitest.config.ts +23 -0
- package/dist/commands/capabilities.d.ts +0 -3
- package/dist/commands/capabilities.js +0 -73
- package/dist/commands/context.d.ts +0 -5
- package/dist/commands/evolution-status.d.ts +0 -4
- package/dist/commands/evolution-status.js +0 -117
- package/dist/commands/evolver.d.ts +0 -9
- package/dist/commands/evolver.js +0 -26
- package/dist/commands/export.d.ts +0 -2
- package/dist/commands/export.js +0 -98
- package/dist/commands/focus.d.ts +0 -14
- package/dist/commands/focus.js +0 -457
- package/dist/commands/nocturnal-review.d.ts +0 -24
- package/dist/commands/nocturnal-review.js +0 -265
- package/dist/commands/nocturnal-rollout.d.ts +0 -27
- package/dist/commands/nocturnal-rollout.js +0 -671
- package/dist/commands/nocturnal-train.d.ts +0 -25
- package/dist/commands/nocturnal-train.js +0 -919
- package/dist/commands/pain.d.ts +0 -5
- package/dist/commands/principle-rollback.d.ts +0 -4
- package/dist/commands/principle-rollback.js +0 -22
- package/dist/commands/rollback.d.ts +0 -19
- package/dist/commands/samples.d.ts +0 -2
- package/dist/commands/samples.js +0 -55
- package/dist/commands/strategy.d.ts +0 -3
- package/dist/commands/strategy.js +0 -29
- package/dist/commands/thinking-os.d.ts +0 -2
- package/dist/config/defaults/runtime.d.ts +0 -40
- package/dist/config/errors.d.ts +0 -84
- package/dist/config/errors.js +0 -94
- package/dist/config/index.js +0 -7
- package/dist/constants/diagnostician.d.ts +0 -12
- package/dist/constants/diagnostician.js +0 -56
- package/dist/constants/tools.d.ts +0 -17
- package/dist/constants/tools.js +0 -54
- package/dist/core/adaptive-thresholds.d.ts +0 -186
- package/dist/core/adaptive-thresholds.js +0 -300
- package/dist/core/config-service.d.ts +0 -15
- package/dist/core/config.d.ts +0 -129
- package/dist/core/control-ui-db.d.ts +0 -95
- package/dist/core/control-ui-db.js +0 -292
- package/dist/core/detection-funnel.d.ts +0 -33
- package/dist/core/detection-service.d.ts +0 -15
- package/dist/core/dictionary-service.d.ts +0 -15
- package/dist/core/dictionary.d.ts +0 -38
- package/dist/core/event-log.d.ts +0 -82
- package/dist/core/event-log.js +0 -463
- package/dist/core/evolution-engine.d.ts +0 -118
- package/dist/core/evolution-engine.js +0 -464
- package/dist/core/evolution-logger.d.ts +0 -137
- package/dist/core/evolution-logger.js +0 -256
- package/dist/core/evolution-migration.d.ts +0 -5
- package/dist/core/evolution-migration.js +0 -65
- package/dist/core/evolution-reducer.d.ts +0 -98
- package/dist/core/evolution-reducer.js +0 -465
- package/dist/core/evolution-types.d.ts +0 -287
- package/dist/core/evolution-types.js +0 -78
- package/dist/core/external-training-contract.d.ts +0 -276
- package/dist/core/external-training-contract.js +0 -269
- package/dist/core/focus-history.d.ts +0 -210
- package/dist/core/focus-history.js +0 -1185
- package/dist/core/hygiene/tracker.d.ts +0 -22
- package/dist/core/hygiene/tracker.js +0 -106
- package/dist/core/init.d.ts +0 -12
- package/dist/core/local-worker-routing.d.ts +0 -175
- package/dist/core/local-worker-routing.js +0 -525
- package/dist/core/migration.d.ts +0 -6
- package/dist/core/model-deployment-registry.d.ts +0 -218
- package/dist/core/model-deployment-registry.js +0 -503
- package/dist/core/model-training-registry.d.ts +0 -295
- package/dist/core/model-training-registry.js +0 -475
- package/dist/core/nocturnal-arbiter.d.ts +0 -159
- package/dist/core/nocturnal-arbiter.js +0 -534
- package/dist/core/nocturnal-candidate-scoring.d.ts +0 -137
- package/dist/core/nocturnal-candidate-scoring.js +0 -266
- package/dist/core/nocturnal-compliance.d.ts +0 -175
- package/dist/core/nocturnal-compliance.js +0 -824
- package/dist/core/nocturnal-dataset.d.ts +0 -224
- package/dist/core/nocturnal-dataset.js +0 -443
- package/dist/core/nocturnal-executability.d.ts +0 -85
- package/dist/core/nocturnal-executability.js +0 -331
- package/dist/core/nocturnal-export.d.ts +0 -124
- package/dist/core/nocturnal-export.js +0 -275
- package/dist/core/nocturnal-paths.d.ts +0 -124
- package/dist/core/nocturnal-trajectory-extractor.d.ts +0 -242
- package/dist/core/nocturnal-trajectory-extractor.js +0 -307
- package/dist/core/nocturnal-trinity.d.ts +0 -311
- package/dist/core/nocturnal-trinity.js +0 -880
- package/dist/core/pain.d.ts +0 -4
- package/dist/core/pain.js +0 -70
- package/dist/core/path-resolver.d.ts +0 -46
- package/dist/core/paths.d.ts +0 -65
- package/dist/core/principle-training-state.d.ts +0 -121
- package/dist/core/principle-training-state.js +0 -321
- package/dist/core/profile.d.ts +0 -62
- package/dist/core/profile.js +0 -210
- package/dist/core/promotion-gate.d.ts +0 -238
- package/dist/core/promotion-gate.js +0 -529
- package/dist/core/risk-calculator.d.ts +0 -22
- package/dist/core/session-tracker.d.ts +0 -101
- package/dist/core/shadow-observation-registry.d.ts +0 -217
- package/dist/core/shadow-observation-registry.js +0 -308
- package/dist/core/system-logger.d.ts +0 -8
- package/dist/core/thinking-models.d.ts +0 -38
- package/dist/core/thinking-models.js +0 -170
- package/dist/core/training-program.d.ts +0 -233
- package/dist/core/training-program.js +0 -433
- package/dist/core/trajectory.d.ts +0 -411
- package/dist/core/trajectory.js +0 -1307
- package/dist/core/workspace-context.d.ts +0 -71
- package/dist/hooks/bash-risk.d.ts +0 -57
- package/dist/hooks/bash-risk.js +0 -137
- package/dist/hooks/edit-verification.d.ts +0 -62
- package/dist/hooks/edit-verification.js +0 -256
- package/dist/hooks/gate-block-helper.d.ts +0 -44
- package/dist/hooks/gate-block-helper.js +0 -119
- package/dist/hooks/gate.d.ts +0 -24
- package/dist/hooks/gate.js +0 -173
- package/dist/hooks/gfi-gate.d.ts +0 -40
- package/dist/hooks/gfi-gate.js +0 -113
- package/dist/hooks/lifecycle.d.ts +0 -5
- package/dist/hooks/lifecycle.js +0 -284
- package/dist/hooks/llm.d.ts +0 -13
- package/dist/hooks/message-sanitize.d.ts +0 -3
- package/dist/hooks/message-sanitize.js +0 -37
- package/dist/hooks/pain.d.ts +0 -5
- package/dist/hooks/pain.js +0 -301
- package/dist/hooks/progressive-trust-gate.d.ts +0 -52
- package/dist/hooks/progressive-trust-gate.js +0 -134
- package/dist/hooks/prompt.d.ts +0 -49
- package/dist/hooks/prompt.js +0 -905
- package/dist/hooks/subagent.d.ts +0 -10
- package/dist/hooks/subagent.js +0 -387
- package/dist/hooks/thinking-checkpoint.d.ts +0 -37
- package/dist/hooks/thinking-checkpoint.js +0 -51
- package/dist/hooks/trajectory-collector.d.ts +0 -32
- package/dist/hooks/trajectory-collector.js +0 -256
- package/dist/http/principles-console-route.d.ts +0 -9
- package/dist/http/principles-console-route.js +0 -681
- package/dist/i18n/commands.d.ts +0 -26
- package/dist/i18n/commands.js +0 -116
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -581
- package/dist/service/central-database.d.ts +0 -104
- package/dist/service/central-database.js +0 -649
- package/dist/service/control-ui-query-service.d.ts +0 -221
- package/dist/service/control-ui-query-service.js +0 -543
- package/dist/service/empathy-observer-manager.d.ts +0 -88
- package/dist/service/empathy-observer-manager.js +0 -414
- package/dist/service/evolution-query-service.d.ts +0 -155
- package/dist/service/evolution-query-service.js +0 -258
- package/dist/service/evolution-worker.d.ts +0 -101
- package/dist/service/evolution-worker.js +0 -975
- package/dist/service/health-query-service.d.ts +0 -170
- package/dist/service/health-query-service.js +0 -662
- package/dist/service/nocturnal-runtime.d.ts +0 -183
- package/dist/service/nocturnal-service.d.ts +0 -163
- package/dist/service/nocturnal-service.js +0 -787
- package/dist/service/nocturnal-target-selector.d.ts +0 -145
- package/dist/service/nocturnal-target-selector.js +0 -315
- package/dist/service/phase3-input-filter.d.ts +0 -73
- package/dist/service/phase3-input-filter.js +0 -172
- package/dist/service/runtime-summary-service.d.ts +0 -122
- package/dist/service/runtime-summary-service.js +0 -485
- package/dist/service/subagent-workflow/empathy-observer-workflow-manager.d.ts +0 -48
- package/dist/service/subagent-workflow/index.d.ts +0 -4
- package/dist/service/subagent-workflow/index.js +0 -3
- package/dist/service/subagent-workflow/runtime-direct-driver.d.ts +0 -77
- package/dist/service/subagent-workflow/runtime-direct-driver.js +0 -75
- package/dist/service/subagent-workflow/types.js +0 -11
- package/dist/service/subagent-workflow/workflow-store.d.ts +0 -26
- package/dist/service/subagent-workflow/workflow-store.js +0 -165
- package/dist/service/trajectory-service.d.ts +0 -2
- package/dist/service/trajectory-service.js +0 -15
- package/dist/tools/critique-prompt.d.ts +0 -14
- package/dist/tools/deep-reflect.d.ts +0 -39
- package/dist/tools/deep-reflect.js +0 -350
- package/dist/tools/model-index.d.ts +0 -9
- package/dist/types/event-types.d.ts +0 -306
- package/dist/types/event-types.js +0 -106
- package/dist/types/hygiene-types.d.ts +0 -20
- package/dist/types/hygiene-types.js +0 -12
- package/dist/types/runtime-summary.d.ts +0 -47
- package/dist/types/runtime-summary.js +0 -1
- package/dist/types.d.ts +0 -50
- package/dist/types.js +0 -22
- package/dist/utils/file-lock.d.ts +0 -71
- package/dist/utils/file-lock.js +0 -309
- package/dist/utils/glob-match.d.ts +0 -28
- package/dist/utils/hashing.d.ts +0 -9
- package/dist/utils/io.d.ts +0 -6
- package/dist/utils/io.js +0 -106
- package/dist/utils/nlp.d.ts +0 -9
- package/dist/utils/plugin-logger.d.ts +0 -39
- package/dist/utils/subagent-probe.d.ts +0 -34
- package/dist/utils/subagent-probe.js +0 -81
package/dist/core/trajectory.js
DELETED
|
@@ -1,1307 +0,0 @@
|
|
|
1
|
-
import Database from 'better-sqlite3';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import crypto from 'crypto';
|
|
5
|
-
import { withLock } from '../utils/file-lock.js';
|
|
6
|
-
import { resolvePdPath } from './paths.js';
|
|
7
|
-
import { SampleNotFoundError } from '../config/index.js';
|
|
8
|
-
/**
|
|
9
|
-
* Trajectory database stores HISTORICAL and ANALYTICS data.
|
|
10
|
-
*
|
|
11
|
-
* PURPOSE: Track task outcomes, trust changes, and evolution progress over time.
|
|
12
|
-
* USAGE: Insights, trends, and Phase 3 supporting evidence (where explicitly allowed).
|
|
13
|
-
* NOT FOR: Control decisions, Phase 3 eligibility, or real-time operations.
|
|
14
|
-
*
|
|
15
|
-
* Runtime truth comes from: queue state, workspace trust scorecard, active sessions
|
|
16
|
-
*/
|
|
17
|
-
const DEFAULT_INLINE_THRESHOLD = 16 * 1024;
|
|
18
|
-
const DEFAULT_BUSY_TIMEOUT_MS = 5000;
|
|
19
|
-
const DEFAULT_ORPHAN_BLOB_GRACE_DAYS = 7;
|
|
20
|
-
const SCHEMA_VERSION = 1;
|
|
21
|
-
function nowIso() {
|
|
22
|
-
return new Date().toISOString();
|
|
23
|
-
}
|
|
24
|
-
function safeJson(value) {
|
|
25
|
-
return JSON.stringify(value ?? {});
|
|
26
|
-
}
|
|
27
|
-
function fileSizeIfExists(filePath) {
|
|
28
|
-
try {
|
|
29
|
-
return fs.existsSync(filePath) ? fs.statSync(filePath).size : 0;
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
return 0;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function summarizeForDiff(text) {
|
|
36
|
-
return text.length > 240 ? `${text.slice(0, 237)}...` : text;
|
|
37
|
-
}
|
|
38
|
-
function redactText(text) {
|
|
39
|
-
return text
|
|
40
|
-
.replace(/[A-Za-z]:\\[^\s"'`]+/g, '<WINDOWS_PATH>')
|
|
41
|
-
.replace(/\/(?:[A-Za-z0-9._-]+\/){1,}[A-Za-z0-9._-]+(?:\.[A-Za-z0-9._-]+)?/g, '<PATH>')
|
|
42
|
-
.replace(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/gi, '<EMAIL>')
|
|
43
|
-
.replace(/\b(sk|rk|pk)_[A-Za-z0-9]+\b/g, '<TOKEN>');
|
|
44
|
-
}
|
|
45
|
-
export class TrajectoryDatabase {
|
|
46
|
-
workspaceDir;
|
|
47
|
-
stateDir;
|
|
48
|
-
dbPath;
|
|
49
|
-
blobDir;
|
|
50
|
-
exportDir;
|
|
51
|
-
blobInlineThresholdBytes;
|
|
52
|
-
orphanBlobGraceMs;
|
|
53
|
-
db;
|
|
54
|
-
constructor(opts) {
|
|
55
|
-
this.workspaceDir = path.resolve(opts.workspaceDir);
|
|
56
|
-
this.stateDir = resolvePdPath(this.workspaceDir, 'STATE_DIR');
|
|
57
|
-
this.dbPath = resolvePdPath(this.workspaceDir, 'TRAJECTORY_DB');
|
|
58
|
-
this.blobDir = resolvePdPath(this.workspaceDir, 'TRAJECTORY_BLOBS_DIR');
|
|
59
|
-
this.exportDir = resolvePdPath(this.workspaceDir, 'EXPORTS_DIR');
|
|
60
|
-
this.blobInlineThresholdBytes = opts.blobInlineThresholdBytes ?? DEFAULT_INLINE_THRESHOLD;
|
|
61
|
-
this.orphanBlobGraceMs = Math.max(0, (opts.orphanBlobGraceDays ?? DEFAULT_ORPHAN_BLOB_GRACE_DAYS) * 24 * 60 * 60 * 1000);
|
|
62
|
-
fs.mkdirSync(this.stateDir, { recursive: true });
|
|
63
|
-
fs.mkdirSync(this.blobDir, { recursive: true });
|
|
64
|
-
fs.mkdirSync(this.exportDir, { recursive: true });
|
|
65
|
-
this.db = new Database(this.dbPath);
|
|
66
|
-
this.db.pragma('journal_mode = WAL');
|
|
67
|
-
this.db.pragma('foreign_keys = ON');
|
|
68
|
-
this.db.pragma('synchronous = NORMAL');
|
|
69
|
-
this.db.pragma(`busy_timeout = ${Math.max(0, opts.busyTimeoutMs ?? DEFAULT_BUSY_TIMEOUT_MS)}`);
|
|
70
|
-
this.initSchema();
|
|
71
|
-
this.importLegacyArtifacts();
|
|
72
|
-
this.pruneUnreferencedBlobs();
|
|
73
|
-
}
|
|
74
|
-
dispose() {
|
|
75
|
-
this.db.close();
|
|
76
|
-
}
|
|
77
|
-
recordSession(input) {
|
|
78
|
-
const startedAt = input.startedAt ?? nowIso();
|
|
79
|
-
this.withWrite(() => {
|
|
80
|
-
this.db.prepare(`
|
|
81
|
-
INSERT INTO sessions (session_id, started_at, updated_at)
|
|
82
|
-
VALUES (?, ?, ?)
|
|
83
|
-
ON CONFLICT(session_id) DO UPDATE SET updated_at = excluded.updated_at
|
|
84
|
-
`).run(input.sessionId, startedAt, nowIso());
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
recordAssistantTurn(input) {
|
|
88
|
-
this.recordSession({ sessionId: input.sessionId, startedAt: input.createdAt });
|
|
89
|
-
const rawStorage = this.storeRawText('assistant', input.rawText);
|
|
90
|
-
const createdAt = input.createdAt ?? nowIso();
|
|
91
|
-
return this.withWrite(() => {
|
|
92
|
-
const result = this.db.prepare(`
|
|
93
|
-
INSERT INTO assistant_turns (
|
|
94
|
-
session_id, run_id, provider, model, raw_text, sanitized_text, usage_json,
|
|
95
|
-
empathy_signal_json, blob_ref, raw_excerpt, created_at
|
|
96
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
97
|
-
`).run(input.sessionId, input.runId, input.provider, input.model, rawStorage.inlineText, input.sanitizedText, safeJson(input.usageJson), safeJson(input.empathySignalJson), rawStorage.blobRef, rawStorage.excerpt, createdAt);
|
|
98
|
-
return Number(result.lastInsertRowid);
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
recordUserTurn(input) {
|
|
102
|
-
this.recordSession({ sessionId: input.sessionId, startedAt: input.createdAt });
|
|
103
|
-
const rawStorage = this.storeRawText('user', input.rawText);
|
|
104
|
-
const createdAt = input.createdAt ?? nowIso();
|
|
105
|
-
return this.withWrite(() => {
|
|
106
|
-
const result = this.db.prepare(`
|
|
107
|
-
INSERT INTO user_turns (
|
|
108
|
-
session_id, turn_index, raw_text, blob_ref, raw_excerpt,
|
|
109
|
-
correction_detected, correction_cue, references_assistant_turn_id, created_at
|
|
110
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
111
|
-
`).run(input.sessionId, input.turnIndex, rawStorage.inlineText, rawStorage.blobRef, rawStorage.excerpt, input.correctionDetected ? 1 : 0, input.correctionCue ?? null, input.referencesAssistantTurnId ?? null, createdAt);
|
|
112
|
-
return Number(result.lastInsertRowid);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
recordToolCall(input) {
|
|
116
|
-
this.recordSession({ sessionId: input.sessionId, startedAt: input.createdAt });
|
|
117
|
-
const createdAt = input.createdAt ?? nowIso();
|
|
118
|
-
// Extract filePath from paramsJson if provided and is an object with filePath
|
|
119
|
-
const paramsObj = input.paramsJson;
|
|
120
|
-
const filePath = paramsObj && typeof paramsObj.filePath === 'string' ? paramsObj.filePath : null;
|
|
121
|
-
const rowId = this.withWrite(() => {
|
|
122
|
-
const result = this.db.prepare(`
|
|
123
|
-
INSERT INTO tool_calls (
|
|
124
|
-
session_id, tool_name, outcome, duration_ms, exit_code, error_type, error_message,
|
|
125
|
-
gfi_before, gfi_after, params_json, created_at
|
|
126
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
127
|
-
`).run(input.sessionId, input.toolName, input.outcome, input.durationMs ?? null, input.exitCode ?? null, input.errorType ?? null, input.errorMessage ?? null, input.gfiBefore ?? null, input.gfiAfter ?? null, safeJson(input.paramsJson), createdAt);
|
|
128
|
-
return Number(result.lastInsertRowid);
|
|
129
|
-
});
|
|
130
|
-
if (input.outcome === 'success') {
|
|
131
|
-
this.maybeCreateCorrectionSample(input.sessionId);
|
|
132
|
-
}
|
|
133
|
-
return rowId;
|
|
134
|
-
}
|
|
135
|
-
recordPainEvent(input) {
|
|
136
|
-
this.recordSession({ sessionId: input.sessionId, startedAt: input.createdAt });
|
|
137
|
-
this.withWrite(() => {
|
|
138
|
-
this.db.prepare(`
|
|
139
|
-
INSERT INTO pain_events (
|
|
140
|
-
session_id, source, score, reason, severity, origin, confidence, created_at
|
|
141
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
142
|
-
`).run(input.sessionId, input.source, input.score, input.reason ?? null, input.severity ?? null, input.origin ?? null, input.confidence ?? null, input.createdAt ?? nowIso());
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
recordGateBlock(input) {
|
|
146
|
-
this.withWrite(() => {
|
|
147
|
-
this.db.prepare(`
|
|
148
|
-
INSERT INTO gate_blocks (session_id, tool_name, file_path, reason, plan_status, created_at)
|
|
149
|
-
VALUES (?, ?, ?, ?, ?, ?)
|
|
150
|
-
`).run(input.sessionId ?? null, input.toolName, input.filePath ?? null, input.reason, input.planStatus ?? null, input.createdAt ?? nowIso());
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
recordTrustChange(input) {
|
|
154
|
-
this.withWrite(() => {
|
|
155
|
-
this.db.prepare(`
|
|
156
|
-
INSERT INTO trust_changes (session_id, previous_score, new_score, delta, reason, created_at)
|
|
157
|
-
VALUES (?, ?, ?, ?, ?, ?)
|
|
158
|
-
`).run(input.sessionId ?? null, input.previousScore, input.newScore, input.delta, input.reason, input.createdAt ?? nowIso());
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
recordPrincipleEvent(input) {
|
|
162
|
-
this.withWrite(() => {
|
|
163
|
-
this.db.prepare(`
|
|
164
|
-
INSERT INTO principle_events (principle_id, event_type, payload_json, created_at)
|
|
165
|
-
VALUES (?, ?, ?, ?)
|
|
166
|
-
`).run(input.principleId ?? null, input.eventType, safeJson(input.payload), input.createdAt ?? nowIso());
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
recordTaskOutcome(input) {
|
|
170
|
-
this.withWrite(() => {
|
|
171
|
-
this.db.prepare(`
|
|
172
|
-
INSERT INTO task_outcomes (session_id, task_id, outcome, summary, principle_ids_json, created_at)
|
|
173
|
-
VALUES (?, ?, ?, ?, ?, ?)
|
|
174
|
-
`).run(input.sessionId, input.taskId ?? null, input.outcome, input.summary ?? null, safeJson(input.principleIdsJson), input.createdAt ?? nowIso());
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
recordEvolutionTask(input) {
|
|
178
|
-
const now = nowIso();
|
|
179
|
-
// Cast to V2 to access new fields
|
|
180
|
-
const v2 = input;
|
|
181
|
-
this.withWrite(() => {
|
|
182
|
-
this.db.prepare(`
|
|
183
|
-
INSERT INTO evolution_tasks (
|
|
184
|
-
task_id, trace_id, source, reason, score, status,
|
|
185
|
-
enqueued_at, started_at, completed_at, resolution, created_at, updated_at,
|
|
186
|
-
task_kind, priority, retry_count, max_retries, last_error, result_ref
|
|
187
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
188
|
-
ON CONFLICT(task_id) DO UPDATE SET
|
|
189
|
-
status = excluded.status,
|
|
190
|
-
started_at = excluded.started_at,
|
|
191
|
-
completed_at = excluded.completed_at,
|
|
192
|
-
resolution = excluded.resolution,
|
|
193
|
-
updated_at = excluded.updated_at,
|
|
194
|
-
task_kind = excluded.task_kind,
|
|
195
|
-
priority = excluded.priority,
|
|
196
|
-
retry_count = excluded.retry_count,
|
|
197
|
-
max_retries = excluded.max_retries,
|
|
198
|
-
last_error = excluded.last_error,
|
|
199
|
-
result_ref = excluded.result_ref
|
|
200
|
-
`).run(input.taskId, input.traceId, input.source, input.reason ?? null, input.score ?? 0, input.status ?? 'pending', input.enqueuedAt ?? null, input.startedAt ?? null, input.completedAt ?? null, input.resolution ?? null, input.createdAt ?? now, input.updatedAt ?? now, v2.taskKind ?? null, v2.priority ?? null, v2.retryCount ?? null, v2.maxRetries ?? null, v2.lastError ?? null, v2.resultRef ?? null);
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
updateEvolutionTask(taskId, updates) {
|
|
204
|
-
const now = nowIso();
|
|
205
|
-
// Cast to V2 to access new fields
|
|
206
|
-
const v2Updates = updates;
|
|
207
|
-
this.withWrite(() => {
|
|
208
|
-
const setClauses = ['updated_at = ?'];
|
|
209
|
-
const values = [now];
|
|
210
|
-
if (updates.status !== undefined) {
|
|
211
|
-
setClauses.push('status = ?');
|
|
212
|
-
values.push(updates.status);
|
|
213
|
-
}
|
|
214
|
-
if (updates.startedAt !== undefined) {
|
|
215
|
-
setClauses.push('started_at = ?');
|
|
216
|
-
values.push(updates.startedAt);
|
|
217
|
-
}
|
|
218
|
-
if (updates.completedAt !== undefined) {
|
|
219
|
-
setClauses.push('completed_at = ?');
|
|
220
|
-
values.push(updates.completedAt);
|
|
221
|
-
}
|
|
222
|
-
if (updates.resolution !== undefined) {
|
|
223
|
-
setClauses.push('resolution = ?');
|
|
224
|
-
values.push(updates.resolution);
|
|
225
|
-
}
|
|
226
|
-
if (updates.score !== undefined) {
|
|
227
|
-
setClauses.push('score = ?');
|
|
228
|
-
values.push(updates.score);
|
|
229
|
-
}
|
|
230
|
-
// V2 fields
|
|
231
|
-
if (v2Updates.taskKind !== undefined) {
|
|
232
|
-
setClauses.push('task_kind = ?');
|
|
233
|
-
values.push(v2Updates.taskKind);
|
|
234
|
-
}
|
|
235
|
-
if (v2Updates.priority !== undefined) {
|
|
236
|
-
setClauses.push('priority = ?');
|
|
237
|
-
values.push(v2Updates.priority);
|
|
238
|
-
}
|
|
239
|
-
if (v2Updates.retryCount !== undefined) {
|
|
240
|
-
setClauses.push('retry_count = ?');
|
|
241
|
-
values.push(v2Updates.retryCount);
|
|
242
|
-
}
|
|
243
|
-
if (v2Updates.maxRetries !== undefined) {
|
|
244
|
-
setClauses.push('max_retries = ?');
|
|
245
|
-
values.push(v2Updates.maxRetries);
|
|
246
|
-
}
|
|
247
|
-
if (v2Updates.lastError !== undefined) {
|
|
248
|
-
setClauses.push('last_error = ?');
|
|
249
|
-
values.push(v2Updates.lastError);
|
|
250
|
-
}
|
|
251
|
-
if (v2Updates.resultRef !== undefined) {
|
|
252
|
-
setClauses.push('result_ref = ?');
|
|
253
|
-
values.push(v2Updates.resultRef);
|
|
254
|
-
}
|
|
255
|
-
values.push(taskId);
|
|
256
|
-
this.db.prepare(`
|
|
257
|
-
UPDATE evolution_tasks SET ${setClauses.join(', ')} WHERE task_id = ?
|
|
258
|
-
`).run(...values);
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
recordEvolutionEvent(input) {
|
|
262
|
-
this.withWrite(() => {
|
|
263
|
-
this.db.prepare(`
|
|
264
|
-
INSERT INTO evolution_events (trace_id, task_id, stage, level, message, summary, metadata_json, created_at)
|
|
265
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
266
|
-
`).run(input.traceId, input.taskId ?? null, input.stage, input.level ?? 'info', input.message, input.summary ?? null, safeJson(input.metadata), input.createdAt ?? nowIso());
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* List evolution tasks with optional filtering.
|
|
271
|
-
*
|
|
272
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
273
|
-
* Not: Runtime truth or real-time queue state.
|
|
274
|
-
*/
|
|
275
|
-
listEvolutionTasks(filters = {}) {
|
|
276
|
-
const conditions = [];
|
|
277
|
-
const values = [];
|
|
278
|
-
if (filters.status) {
|
|
279
|
-
conditions.push('status = ?');
|
|
280
|
-
values.push(filters.status);
|
|
281
|
-
}
|
|
282
|
-
if (filters.dateFrom) {
|
|
283
|
-
conditions.push('created_at >= ?');
|
|
284
|
-
values.push(filters.dateFrom);
|
|
285
|
-
}
|
|
286
|
-
if (filters.dateTo) {
|
|
287
|
-
conditions.push('created_at <= ?');
|
|
288
|
-
values.push(filters.dateTo);
|
|
289
|
-
}
|
|
290
|
-
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
291
|
-
const limit = filters.limit ?? 50;
|
|
292
|
-
const offset = filters.offset ?? 0;
|
|
293
|
-
const rows = this.db.prepare(`
|
|
294
|
-
SELECT id, task_id, trace_id, source, reason, score, status,
|
|
295
|
-
enqueued_at, started_at, completed_at, resolution, created_at, updated_at,
|
|
296
|
-
task_kind, priority, retry_count, max_retries, last_error, result_ref
|
|
297
|
-
FROM evolution_tasks
|
|
298
|
-
${whereClause}
|
|
299
|
-
ORDER BY created_at DESC
|
|
300
|
-
LIMIT ? OFFSET ?
|
|
301
|
-
`).all(...values, limit, offset);
|
|
302
|
-
return rows.map((row) => ({
|
|
303
|
-
id: Number(row.id),
|
|
304
|
-
taskId: String(row.task_id),
|
|
305
|
-
traceId: String(row.trace_id),
|
|
306
|
-
source: String(row.source),
|
|
307
|
-
reason: row.reason ? String(row.reason) : null,
|
|
308
|
-
score: Number(row.score ?? 0),
|
|
309
|
-
status: String(row.status),
|
|
310
|
-
enqueuedAt: row.enqueued_at ? String(row.enqueued_at) : null,
|
|
311
|
-
startedAt: row.started_at ? String(row.started_at) : null,
|
|
312
|
-
completedAt: row.completed_at ? String(row.completed_at) : null,
|
|
313
|
-
resolution: row.resolution ? String(row.resolution) : null,
|
|
314
|
-
createdAt: String(row.created_at),
|
|
315
|
-
updatedAt: String(row.updated_at),
|
|
316
|
-
taskKind: row.task_kind ? row.task_kind : null,
|
|
317
|
-
priority: row.priority ? row.priority : null,
|
|
318
|
-
retryCount: row.retry_count != null ? Number(row.retry_count) : null,
|
|
319
|
-
maxRetries: row.max_retries != null ? Number(row.max_retries) : null,
|
|
320
|
-
lastError: row.last_error ? String(row.last_error) : null,
|
|
321
|
-
resultRef: row.result_ref ? String(row.result_ref) : null,
|
|
322
|
-
}));
|
|
323
|
-
}
|
|
324
|
-
/**
|
|
325
|
-
* List evolution events for a trace or globally.
|
|
326
|
-
*
|
|
327
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
328
|
-
* Not: Runtime truth or real-time queue state.
|
|
329
|
-
*/
|
|
330
|
-
listEvolutionEvents(traceId, filters = {}) {
|
|
331
|
-
const limit = filters.limit ?? 100;
|
|
332
|
-
const offset = filters.offset ?? 0;
|
|
333
|
-
let rows;
|
|
334
|
-
if (traceId) {
|
|
335
|
-
rows = this.db.prepare(`
|
|
336
|
-
SELECT id, trace_id, task_id, stage, level, message, summary, metadata_json, created_at
|
|
337
|
-
FROM evolution_events
|
|
338
|
-
WHERE trace_id = ?
|
|
339
|
-
ORDER BY created_at ASC
|
|
340
|
-
LIMIT ? OFFSET ?
|
|
341
|
-
`).all(traceId, limit, offset);
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
rows = this.db.prepare(`
|
|
345
|
-
SELECT id, trace_id, task_id, stage, level, message, summary, metadata_json, created_at
|
|
346
|
-
FROM evolution_events
|
|
347
|
-
ORDER BY created_at DESC
|
|
348
|
-
LIMIT ? OFFSET ?
|
|
349
|
-
`).all(limit, offset);
|
|
350
|
-
}
|
|
351
|
-
return rows.map((row) => ({
|
|
352
|
-
id: Number(row.id),
|
|
353
|
-
traceId: String(row.trace_id),
|
|
354
|
-
taskId: row.task_id ? String(row.task_id) : null,
|
|
355
|
-
stage: String(row.stage),
|
|
356
|
-
level: String(row.level ?? 'info'),
|
|
357
|
-
message: String(row.message),
|
|
358
|
-
summary: row.summary ? String(row.summary) : null,
|
|
359
|
-
metadata: JSON.parse(String(row.metadata_json ?? '{}')),
|
|
360
|
-
createdAt: String(row.created_at),
|
|
361
|
-
}));
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Get evolution task by trace ID.
|
|
365
|
-
*
|
|
366
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
367
|
-
* Not: Runtime truth or real-time queue state.
|
|
368
|
-
*/
|
|
369
|
-
getEvolutionTaskByTraceId(traceId) {
|
|
370
|
-
const row = this.db.prepare(`
|
|
371
|
-
SELECT id, task_id, trace_id, source, reason, score, status,
|
|
372
|
-
enqueued_at, started_at, completed_at, resolution, created_at, updated_at,
|
|
373
|
-
task_kind, priority, retry_count, max_retries, last_error, result_ref
|
|
374
|
-
FROM evolution_tasks
|
|
375
|
-
WHERE trace_id = ?
|
|
376
|
-
LIMIT 1
|
|
377
|
-
`).get(traceId);
|
|
378
|
-
if (!row)
|
|
379
|
-
return null;
|
|
380
|
-
return {
|
|
381
|
-
id: Number(row.id),
|
|
382
|
-
taskId: String(row.task_id),
|
|
383
|
-
traceId: String(row.trace_id),
|
|
384
|
-
source: String(row.source),
|
|
385
|
-
reason: row.reason ? String(row.reason) : null,
|
|
386
|
-
score: Number(row.score ?? 0),
|
|
387
|
-
status: String(row.status),
|
|
388
|
-
enqueuedAt: row.enqueued_at ? String(row.enqueued_at) : null,
|
|
389
|
-
startedAt: row.started_at ? String(row.started_at) : null,
|
|
390
|
-
completedAt: row.completed_at ? String(row.completed_at) : null,
|
|
391
|
-
resolution: row.resolution ? String(row.resolution) : null,
|
|
392
|
-
createdAt: String(row.created_at),
|
|
393
|
-
updatedAt: String(row.updated_at),
|
|
394
|
-
taskKind: row.task_kind ? row.task_kind : null,
|
|
395
|
-
priority: row.priority ? row.priority : null,
|
|
396
|
-
retryCount: row.retry_count != null ? Number(row.retry_count) : null,
|
|
397
|
-
maxRetries: row.max_retries != null ? Number(row.max_retries) : null,
|
|
398
|
-
lastError: row.last_error ? String(row.last_error) : null,
|
|
399
|
-
resultRef: row.result_ref ? String(row.result_ref) : null,
|
|
400
|
-
};
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Get evolution task statistics.
|
|
404
|
-
*
|
|
405
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
406
|
-
* Not: Runtime truth or real-time queue state.
|
|
407
|
-
*/
|
|
408
|
-
getEvolutionStats() {
|
|
409
|
-
const rows = this.db.prepare(`
|
|
410
|
-
SELECT status, COUNT(*) as count FROM evolution_tasks GROUP BY status
|
|
411
|
-
`).all();
|
|
412
|
-
const stats = { total: 0, pending: 0, inProgress: 0, completed: 0, failed: 0 };
|
|
413
|
-
for (const row of rows) {
|
|
414
|
-
stats.total += row.count;
|
|
415
|
-
if (row.status === 'pending')
|
|
416
|
-
stats.pending = row.count;
|
|
417
|
-
else if (row.status === 'in_progress')
|
|
418
|
-
stats.inProgress = row.count;
|
|
419
|
-
else if (row.status === 'completed')
|
|
420
|
-
stats.completed = row.count;
|
|
421
|
-
else if (row.status === 'failed')
|
|
422
|
-
stats.failed = row.count;
|
|
423
|
-
}
|
|
424
|
-
return stats;
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* List recent sessions from the trajectory database.
|
|
428
|
-
*
|
|
429
|
-
* Returns: Recent session records ordered by most recently updated.
|
|
430
|
-
*
|
|
431
|
-
* @param options.limit - Maximum number of sessions to return (default: 20)
|
|
432
|
-
* @param options.dateFrom - Only return sessions updated after this date
|
|
433
|
-
* @param options.dateTo - Only return sessions updated before this date
|
|
434
|
-
*/
|
|
435
|
-
listRecentSessions(options = {}) {
|
|
436
|
-
const conditions = [];
|
|
437
|
-
const values = [];
|
|
438
|
-
if (options.dateFrom) {
|
|
439
|
-
conditions.push('updated_at >= ?');
|
|
440
|
-
values.push(options.dateFrom);
|
|
441
|
-
}
|
|
442
|
-
if (options.dateTo) {
|
|
443
|
-
conditions.push('updated_at <= ?');
|
|
444
|
-
values.push(options.dateTo);
|
|
445
|
-
}
|
|
446
|
-
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
447
|
-
const limit = options.limit ?? 20;
|
|
448
|
-
const rows = this.db.prepare(`
|
|
449
|
-
SELECT session_id, started_at, updated_at
|
|
450
|
-
FROM sessions
|
|
451
|
-
${whereClause}
|
|
452
|
-
ORDER BY updated_at DESC
|
|
453
|
-
LIMIT ?
|
|
454
|
-
`).all(...values, limit);
|
|
455
|
-
return rows.map((row) => ({
|
|
456
|
-
sessionId: String(row.session_id),
|
|
457
|
-
startedAt: String(row.started_at),
|
|
458
|
-
updatedAt: String(row.updated_at),
|
|
459
|
-
}));
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* List assistant turns for a session.
|
|
463
|
-
*
|
|
464
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
465
|
-
* Not: Runtime truth or real-time queue state.
|
|
466
|
-
*/
|
|
467
|
-
listAssistantTurns(sessionId) {
|
|
468
|
-
const rows = this.db.prepare(`
|
|
469
|
-
SELECT id, session_id, run_id, provider, model, raw_text, sanitized_text, blob_ref, created_at
|
|
470
|
-
FROM assistant_turns
|
|
471
|
-
WHERE session_id = ?
|
|
472
|
-
ORDER BY id ASC
|
|
473
|
-
`).all(sessionId);
|
|
474
|
-
return rows.map((row) => ({
|
|
475
|
-
id: Number(row.id),
|
|
476
|
-
sessionId: String(row.session_id),
|
|
477
|
-
runId: String(row.run_id),
|
|
478
|
-
provider: String(row.provider),
|
|
479
|
-
model: String(row.model),
|
|
480
|
-
rawText: this.restoreRawText(row.raw_text, row.blob_ref),
|
|
481
|
-
sanitizedText: String(row.sanitized_text ?? ''),
|
|
482
|
-
blobRef: row.blob_ref ? String(row.blob_ref) : null,
|
|
483
|
-
createdAt: String(row.created_at),
|
|
484
|
-
}));
|
|
485
|
-
}
|
|
486
|
-
/**
|
|
487
|
-
* List tool calls for a session.
|
|
488
|
-
*
|
|
489
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
490
|
-
* Not: Runtime truth or real-time queue state.
|
|
491
|
-
*/
|
|
492
|
-
listToolCallsForSession(sessionId) {
|
|
493
|
-
const rows = this.db.prepare(`
|
|
494
|
-
SELECT id, tool_name, outcome, params_json, duration_ms, exit_code, error_type, error_message,
|
|
495
|
-
gfi_before, gfi_after, created_at
|
|
496
|
-
FROM tool_calls
|
|
497
|
-
WHERE session_id = ?
|
|
498
|
-
ORDER BY id ASC
|
|
499
|
-
`).all(sessionId);
|
|
500
|
-
return rows.map((row) => {
|
|
501
|
-
// Extract filePath from params_json if present
|
|
502
|
-
let filePath = null;
|
|
503
|
-
if (row.params_json && typeof row.params_json === 'string') {
|
|
504
|
-
try {
|
|
505
|
-
const params = JSON.parse(row.params_json);
|
|
506
|
-
if (params && typeof params.filePath === 'string') {
|
|
507
|
-
filePath = params.filePath;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
catch {
|
|
511
|
-
// Ignore malformed JSON
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return {
|
|
515
|
-
id: Number(row.id),
|
|
516
|
-
toolName: String(row.tool_name),
|
|
517
|
-
outcome: String(row.outcome),
|
|
518
|
-
filePath,
|
|
519
|
-
durationMs: row.duration_ms != null ? Number(row.duration_ms) : null,
|
|
520
|
-
exitCode: row.exit_code != null ? Number(row.exit_code) : null,
|
|
521
|
-
errorType: row.error_type ? String(row.error_type) : null,
|
|
522
|
-
errorMessage: row.error_message ? String(row.error_message) : null,
|
|
523
|
-
gfiBefore: row.gfi_before != null ? Number(row.gfi_before) : null,
|
|
524
|
-
gfiAfter: row.gfi_after != null ? Number(row.gfi_after) : null,
|
|
525
|
-
createdAt: String(row.created_at),
|
|
526
|
-
};
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
/**
|
|
530
|
-
* List pain events for a session.
|
|
531
|
-
*
|
|
532
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
533
|
-
* Not: Runtime truth or real-time queue state.
|
|
534
|
-
*/
|
|
535
|
-
listPainEventsForSession(sessionId) {
|
|
536
|
-
const rows = this.db.prepare(`
|
|
537
|
-
SELECT id, source, score, reason, severity, origin, confidence, created_at
|
|
538
|
-
FROM pain_events
|
|
539
|
-
WHERE session_id = ?
|
|
540
|
-
ORDER BY created_at ASC
|
|
541
|
-
`).all(sessionId);
|
|
542
|
-
return rows.map((row) => ({
|
|
543
|
-
id: Number(row.id),
|
|
544
|
-
source: String(row.source),
|
|
545
|
-
score: Number(row.score),
|
|
546
|
-
reason: row.reason ? String(row.reason) : null,
|
|
547
|
-
severity: row.severity ? String(row.severity) : null,
|
|
548
|
-
origin: row.origin ? String(row.origin) : null,
|
|
549
|
-
confidence: row.confidence != null ? Number(row.confidence) : null,
|
|
550
|
-
createdAt: String(row.created_at),
|
|
551
|
-
}));
|
|
552
|
-
}
|
|
553
|
-
/**
|
|
554
|
-
* List user turns for a session.
|
|
555
|
-
* Returns sanitized/reduced fields for nocturnal use — NO raw text.
|
|
556
|
-
*/
|
|
557
|
-
listUserTurnsForSession(sessionId) {
|
|
558
|
-
const rows = this.db.prepare(`
|
|
559
|
-
SELECT id, turn_index, correction_detected, correction_cue, created_at
|
|
560
|
-
FROM user_turns
|
|
561
|
-
WHERE session_id = ?
|
|
562
|
-
ORDER BY turn_index ASC
|
|
563
|
-
`).all(sessionId);
|
|
564
|
-
return rows.map((row) => ({
|
|
565
|
-
id: Number(row.id),
|
|
566
|
-
turnIndex: Number(row.turn_index),
|
|
567
|
-
correctionDetected: Boolean(row.correction_detected),
|
|
568
|
-
correctionCue: row.correction_cue ? String(row.correction_cue) : null,
|
|
569
|
-
createdAt: String(row.created_at),
|
|
570
|
-
}));
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* List correction samples with optional review status filter.
|
|
574
|
-
*
|
|
575
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
576
|
-
* Not: Runtime truth or real-time queue state.
|
|
577
|
-
*/
|
|
578
|
-
listCorrectionSamples(status = 'pending') {
|
|
579
|
-
const rows = this.db.prepare(`
|
|
580
|
-
SELECT sample_id, session_id, bad_assistant_turn_id, user_correction_turn_id,
|
|
581
|
-
recovery_tool_span_json, diff_excerpt, principle_ids_json, quality_score,
|
|
582
|
-
review_status, export_mode, created_at, updated_at
|
|
583
|
-
FROM correction_samples
|
|
584
|
-
WHERE review_status = ?
|
|
585
|
-
ORDER BY created_at DESC
|
|
586
|
-
`).all(status);
|
|
587
|
-
return rows.map((row) => ({
|
|
588
|
-
sampleId: String(row.sample_id),
|
|
589
|
-
sessionId: String(row.session_id),
|
|
590
|
-
badAssistantTurnId: Number(row.bad_assistant_turn_id),
|
|
591
|
-
userCorrectionTurnId: Number(row.user_correction_turn_id),
|
|
592
|
-
recoveryToolSpanJson: String(row.recovery_tool_span_json),
|
|
593
|
-
diffExcerpt: String(row.diff_excerpt ?? ''),
|
|
594
|
-
principleIdsJson: String(row.principle_ids_json ?? '[]'),
|
|
595
|
-
qualityScore: Number(row.quality_score),
|
|
596
|
-
reviewStatus: row.review_status,
|
|
597
|
-
exportMode: row.export_mode,
|
|
598
|
-
createdAt: String(row.created_at),
|
|
599
|
-
updatedAt: String(row.updated_at),
|
|
600
|
-
}));
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* List gate blocks for a session.
|
|
604
|
-
* Returns minimal fields for nocturnal use — no raw text.
|
|
605
|
-
*/
|
|
606
|
-
listGateBlocksForSession(sessionId) {
|
|
607
|
-
const rows = this.db.prepare(`
|
|
608
|
-
SELECT id, tool_name, file_path, reason, plan_status, created_at
|
|
609
|
-
FROM gate_blocks
|
|
610
|
-
WHERE session_id = ?
|
|
611
|
-
ORDER BY id ASC
|
|
612
|
-
`).all(sessionId);
|
|
613
|
-
return rows.map((row) => ({
|
|
614
|
-
id: Number(row.id),
|
|
615
|
-
toolName: String(row.tool_name),
|
|
616
|
-
filePath: row.file_path ? String(row.file_path) : null,
|
|
617
|
-
reason: String(row.reason),
|
|
618
|
-
planStatus: row.plan_status ? String(row.plan_status) : null,
|
|
619
|
-
createdAt: String(row.created_at),
|
|
620
|
-
}));
|
|
621
|
-
}
|
|
622
|
-
reviewCorrectionSample(sampleId, status, note) {
|
|
623
|
-
const updatedAt = nowIso();
|
|
624
|
-
const updated = this.withWrite(() => {
|
|
625
|
-
const updateResult = this.db.prepare(`
|
|
626
|
-
UPDATE correction_samples
|
|
627
|
-
SET review_status = ?, updated_at = ?
|
|
628
|
-
WHERE sample_id = ?
|
|
629
|
-
`).run(status, updatedAt, sampleId);
|
|
630
|
-
if (updateResult.changes === 0) {
|
|
631
|
-
return false;
|
|
632
|
-
}
|
|
633
|
-
this.db.prepare(`
|
|
634
|
-
INSERT INTO sample_reviews (sample_id, review_status, note, created_at)
|
|
635
|
-
VALUES (?, ?, ?, ?)
|
|
636
|
-
`).run(sampleId, status, note ?? null, updatedAt);
|
|
637
|
-
return true;
|
|
638
|
-
});
|
|
639
|
-
if (!updated) {
|
|
640
|
-
throw new SampleNotFoundError(sampleId);
|
|
641
|
-
}
|
|
642
|
-
const record = this.db.prepare(`
|
|
643
|
-
SELECT sample_id, session_id, bad_assistant_turn_id, user_correction_turn_id,
|
|
644
|
-
recovery_tool_span_json, diff_excerpt, principle_ids_json, quality_score,
|
|
645
|
-
review_status, export_mode, created_at, updated_at
|
|
646
|
-
FROM correction_samples
|
|
647
|
-
WHERE sample_id = ?
|
|
648
|
-
`).get(sampleId);
|
|
649
|
-
if (!record) {
|
|
650
|
-
throw new SampleNotFoundError(`${sampleId} (after update)`);
|
|
651
|
-
}
|
|
652
|
-
return {
|
|
653
|
-
sampleId: String(record.sample_id),
|
|
654
|
-
sessionId: String(record.session_id),
|
|
655
|
-
badAssistantTurnId: Number(record.bad_assistant_turn_id),
|
|
656
|
-
userCorrectionTurnId: Number(record.user_correction_turn_id),
|
|
657
|
-
recoveryToolSpanJson: String(record.recovery_tool_span_json),
|
|
658
|
-
diffExcerpt: String(record.diff_excerpt ?? ''),
|
|
659
|
-
principleIdsJson: String(record.principle_ids_json ?? '[]'),
|
|
660
|
-
qualityScore: Number(record.quality_score),
|
|
661
|
-
reviewStatus: record.review_status,
|
|
662
|
-
exportMode: record.export_mode,
|
|
663
|
-
createdAt: String(record.created_at),
|
|
664
|
-
updatedAt: String(record.updated_at),
|
|
665
|
-
};
|
|
666
|
-
}
|
|
667
|
-
/**
|
|
668
|
-
* Export correction samples to JSONL file.
|
|
669
|
-
*
|
|
670
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
671
|
-
* Not: Runtime truth or real-time queue state.
|
|
672
|
-
*/
|
|
673
|
-
exportCorrections(opts) {
|
|
674
|
-
const rows = this.db.prepare(`
|
|
675
|
-
SELECT cs.sample_id, cs.session_id, cs.recovery_tool_span_json, cs.diff_excerpt, cs.quality_score,
|
|
676
|
-
at.raw_text AS assistant_raw_text, at.blob_ref AS assistant_blob_ref, at.sanitized_text,
|
|
677
|
-
ut.raw_text AS user_raw_text, ut.blob_ref AS user_blob_ref, ut.correction_cue
|
|
678
|
-
FROM correction_samples cs
|
|
679
|
-
JOIN assistant_turns at ON at.id = cs.bad_assistant_turn_id
|
|
680
|
-
JOIN user_turns ut ON ut.id = cs.user_correction_turn_id
|
|
681
|
-
WHERE (? = 0 OR cs.review_status = 'approved')
|
|
682
|
-
ORDER BY cs.created_at ASC
|
|
683
|
-
`).all(opts.approvedOnly ? 1 : 0);
|
|
684
|
-
const exportPath = path.join(this.exportDir, `corrections-${Date.now()}-${opts.mode}.jsonl`);
|
|
685
|
-
const lines = rows.map((row) => {
|
|
686
|
-
const assistantRaw = this.restoreRawText(row.assistant_raw_text, row.assistant_blob_ref);
|
|
687
|
-
const userRaw = this.restoreRawText(row.user_raw_text, row.user_blob_ref);
|
|
688
|
-
const assistantText = opts.mode === 'redacted' ? redactText(assistantRaw) : assistantRaw;
|
|
689
|
-
const userText = opts.mode === 'redacted' ? redactText(userRaw) : userRaw;
|
|
690
|
-
return JSON.stringify({
|
|
691
|
-
sample_id: row.sample_id,
|
|
692
|
-
session_id: row.session_id,
|
|
693
|
-
instruction: userText,
|
|
694
|
-
input_context: assistantText,
|
|
695
|
-
bad_attempt_summary: String(row.diff_excerpt ?? ''),
|
|
696
|
-
preferred_response: userText,
|
|
697
|
-
labels: {
|
|
698
|
-
correction_cue: row.correction_cue,
|
|
699
|
-
quality_score: row.quality_score,
|
|
700
|
-
},
|
|
701
|
-
metadata: {
|
|
702
|
-
mode: opts.mode,
|
|
703
|
-
recovery_tool_span_json: row.recovery_tool_span_json,
|
|
704
|
-
},
|
|
705
|
-
});
|
|
706
|
-
});
|
|
707
|
-
fs.writeFileSync(exportPath, `${lines.join('\n')}${lines.length > 0 ? '\n' : ''}`, 'utf8');
|
|
708
|
-
this.recordExportAudit('corrections', opts.mode, opts.approvedOnly, exportPath, rows.length);
|
|
709
|
-
return { filePath: exportPath, count: rows.length, mode: opts.mode };
|
|
710
|
-
}
|
|
711
|
-
/**
|
|
712
|
-
* Export analytics data to JSON file.
|
|
713
|
-
*
|
|
714
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
715
|
-
* Not: Runtime truth or real-time queue state.
|
|
716
|
-
*/
|
|
717
|
-
exportAnalytics() {
|
|
718
|
-
const payload = {
|
|
719
|
-
generatedAt: nowIso(),
|
|
720
|
-
stats: this.getDataStats(),
|
|
721
|
-
dailyMetrics: this.dailyMetrics(),
|
|
722
|
-
errorClusters: this.db.prepare('SELECT * FROM v_error_clusters').all(),
|
|
723
|
-
principleEffectiveness: this.db.prepare('SELECT * FROM v_principle_effectiveness').all(),
|
|
724
|
-
sampleQueue: this.db.prepare('SELECT * FROM v_sample_queue').all(),
|
|
725
|
-
};
|
|
726
|
-
const exportPath = path.join(this.exportDir, `analytics-${Date.now()}.json`);
|
|
727
|
-
fs.writeFileSync(exportPath, JSON.stringify(payload, null, 2), 'utf8');
|
|
728
|
-
this.recordExportAudit('analytics', 'raw', true, exportPath, Array.isArray(payload.dailyMetrics) ? payload.dailyMetrics.length : 0);
|
|
729
|
-
return { filePath: exportPath, count: Array.isArray(payload.dailyMetrics) ? payload.dailyMetrics.length : 0 };
|
|
730
|
-
}
|
|
731
|
-
/**
|
|
732
|
-
* Get trajectory database statistics.
|
|
733
|
-
*
|
|
734
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
735
|
-
* Not: Runtime truth or real-time queue state.
|
|
736
|
-
*/
|
|
737
|
-
getDataStats() {
|
|
738
|
-
const getCount = (table, where) => {
|
|
739
|
-
const sql = where ? `SELECT COUNT(*) as count FROM ${table} WHERE ${where}` : `SELECT COUNT(*) as count FROM ${table}`;
|
|
740
|
-
return Number(this.db.prepare(sql).get().count);
|
|
741
|
-
};
|
|
742
|
-
const lastIngest = this.db.prepare(`
|
|
743
|
-
SELECT MAX(ts) AS ts FROM (
|
|
744
|
-
SELECT MAX(created_at) AS ts FROM assistant_turns
|
|
745
|
-
UNION ALL SELECT MAX(created_at) AS ts FROM user_turns
|
|
746
|
-
UNION ALL SELECT MAX(created_at) AS ts FROM tool_calls
|
|
747
|
-
UNION ALL SELECT MAX(created_at) AS ts FROM pain_events
|
|
748
|
-
)
|
|
749
|
-
`).get();
|
|
750
|
-
return {
|
|
751
|
-
dbPath: this.dbPath,
|
|
752
|
-
dbSizeBytes: fileSizeIfExists(this.dbPath),
|
|
753
|
-
assistantTurns: getCount('assistant_turns'),
|
|
754
|
-
userTurns: getCount('user_turns'),
|
|
755
|
-
toolCalls: getCount('tool_calls'),
|
|
756
|
-
painEvents: getCount('pain_events'),
|
|
757
|
-
pendingSamples: getCount('correction_samples', `review_status = 'pending'`),
|
|
758
|
-
approvedSamples: getCount('correction_samples', `review_status = 'approved'`),
|
|
759
|
-
blobBytes: this.computeBlobBytes(),
|
|
760
|
-
lastIngestAt: lastIngest.ts ?? null,
|
|
761
|
-
};
|
|
762
|
-
}
|
|
763
|
-
cleanupBlobStorage() {
|
|
764
|
-
return this.pruneUnreferencedBlobs();
|
|
765
|
-
}
|
|
766
|
-
initSchema() {
|
|
767
|
-
this.db.exec(`
|
|
768
|
-
CREATE TABLE IF NOT EXISTS schema_version (version INTEGER NOT NULL);
|
|
769
|
-
CREATE TABLE IF NOT EXISTS ingest_checkpoint (
|
|
770
|
-
source_key TEXT PRIMARY KEY,
|
|
771
|
-
imported_at TEXT NOT NULL
|
|
772
|
-
);
|
|
773
|
-
CREATE TABLE IF NOT EXISTS sessions (
|
|
774
|
-
session_id TEXT PRIMARY KEY,
|
|
775
|
-
started_at TEXT NOT NULL,
|
|
776
|
-
updated_at TEXT NOT NULL
|
|
777
|
-
);
|
|
778
|
-
CREATE TABLE IF NOT EXISTS assistant_turns (
|
|
779
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
780
|
-
session_id TEXT NOT NULL,
|
|
781
|
-
run_id TEXT NOT NULL,
|
|
782
|
-
provider TEXT NOT NULL,
|
|
783
|
-
model TEXT NOT NULL,
|
|
784
|
-
raw_text TEXT,
|
|
785
|
-
sanitized_text TEXT NOT NULL,
|
|
786
|
-
usage_json TEXT NOT NULL,
|
|
787
|
-
empathy_signal_json TEXT NOT NULL,
|
|
788
|
-
blob_ref TEXT,
|
|
789
|
-
raw_excerpt TEXT,
|
|
790
|
-
created_at TEXT NOT NULL
|
|
791
|
-
);
|
|
792
|
-
CREATE TABLE IF NOT EXISTS user_turns (
|
|
793
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
794
|
-
session_id TEXT NOT NULL,
|
|
795
|
-
turn_index INTEGER NOT NULL,
|
|
796
|
-
raw_text TEXT,
|
|
797
|
-
blob_ref TEXT,
|
|
798
|
-
raw_excerpt TEXT,
|
|
799
|
-
correction_detected INTEGER NOT NULL DEFAULT 0,
|
|
800
|
-
correction_cue TEXT,
|
|
801
|
-
references_assistant_turn_id INTEGER,
|
|
802
|
-
created_at TEXT NOT NULL
|
|
803
|
-
);
|
|
804
|
-
CREATE TABLE IF NOT EXISTS tool_calls (
|
|
805
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
806
|
-
session_id TEXT NOT NULL,
|
|
807
|
-
tool_name TEXT NOT NULL,
|
|
808
|
-
outcome TEXT NOT NULL,
|
|
809
|
-
duration_ms INTEGER,
|
|
810
|
-
exit_code INTEGER,
|
|
811
|
-
error_type TEXT,
|
|
812
|
-
error_message TEXT,
|
|
813
|
-
gfi_before REAL,
|
|
814
|
-
gfi_after REAL,
|
|
815
|
-
params_json TEXT NOT NULL,
|
|
816
|
-
created_at TEXT NOT NULL
|
|
817
|
-
);
|
|
818
|
-
CREATE TABLE IF NOT EXISTS pain_events (
|
|
819
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
820
|
-
session_id TEXT NOT NULL,
|
|
821
|
-
source TEXT NOT NULL,
|
|
822
|
-
score REAL NOT NULL,
|
|
823
|
-
reason TEXT,
|
|
824
|
-
severity TEXT,
|
|
825
|
-
origin TEXT,
|
|
826
|
-
confidence REAL,
|
|
827
|
-
created_at TEXT NOT NULL
|
|
828
|
-
);
|
|
829
|
-
CREATE TABLE IF NOT EXISTS gate_blocks (
|
|
830
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
831
|
-
session_id TEXT,
|
|
832
|
-
tool_name TEXT NOT NULL,
|
|
833
|
-
file_path TEXT,
|
|
834
|
-
reason TEXT NOT NULL,
|
|
835
|
-
plan_status TEXT,
|
|
836
|
-
created_at TEXT NOT NULL
|
|
837
|
-
);
|
|
838
|
-
CREATE TABLE IF NOT EXISTS trust_changes (
|
|
839
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
840
|
-
session_id TEXT,
|
|
841
|
-
previous_score REAL NOT NULL,
|
|
842
|
-
new_score REAL NOT NULL,
|
|
843
|
-
delta REAL NOT NULL,
|
|
844
|
-
reason TEXT NOT NULL,
|
|
845
|
-
created_at TEXT NOT NULL
|
|
846
|
-
);
|
|
847
|
-
CREATE TABLE IF NOT EXISTS principle_events (
|
|
848
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
849
|
-
principle_id TEXT,
|
|
850
|
-
event_type TEXT NOT NULL,
|
|
851
|
-
payload_json TEXT NOT NULL,
|
|
852
|
-
created_at TEXT NOT NULL
|
|
853
|
-
);
|
|
854
|
-
CREATE TABLE IF NOT EXISTS task_outcomes (
|
|
855
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
856
|
-
session_id TEXT NOT NULL,
|
|
857
|
-
task_id TEXT,
|
|
858
|
-
outcome TEXT NOT NULL,
|
|
859
|
-
summary TEXT,
|
|
860
|
-
principle_ids_json TEXT NOT NULL,
|
|
861
|
-
created_at TEXT NOT NULL
|
|
862
|
-
);
|
|
863
|
-
CREATE TABLE IF NOT EXISTS correction_samples (
|
|
864
|
-
sample_id TEXT PRIMARY KEY,
|
|
865
|
-
session_id TEXT NOT NULL,
|
|
866
|
-
bad_assistant_turn_id INTEGER NOT NULL,
|
|
867
|
-
user_correction_turn_id INTEGER NOT NULL,
|
|
868
|
-
recovery_tool_span_json TEXT NOT NULL,
|
|
869
|
-
diff_excerpt TEXT NOT NULL,
|
|
870
|
-
principle_ids_json TEXT NOT NULL,
|
|
871
|
-
quality_score REAL NOT NULL,
|
|
872
|
-
review_status TEXT NOT NULL,
|
|
873
|
-
export_mode TEXT NOT NULL,
|
|
874
|
-
created_at TEXT NOT NULL,
|
|
875
|
-
updated_at TEXT NOT NULL
|
|
876
|
-
);
|
|
877
|
-
CREATE TABLE IF NOT EXISTS sample_reviews (
|
|
878
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
879
|
-
sample_id TEXT NOT NULL,
|
|
880
|
-
review_status TEXT NOT NULL,
|
|
881
|
-
note TEXT,
|
|
882
|
-
created_at TEXT NOT NULL
|
|
883
|
-
);
|
|
884
|
-
CREATE TABLE IF NOT EXISTS exports_audit (
|
|
885
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
886
|
-
export_kind TEXT NOT NULL,
|
|
887
|
-
mode TEXT NOT NULL,
|
|
888
|
-
approved_only INTEGER NOT NULL,
|
|
889
|
-
file_path TEXT NOT NULL,
|
|
890
|
-
row_count INTEGER NOT NULL,
|
|
891
|
-
created_at TEXT NOT NULL
|
|
892
|
-
);
|
|
893
|
-
CREATE TABLE IF NOT EXISTS evolution_tasks (
|
|
894
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
895
|
-
task_id TEXT UNIQUE NOT NULL,
|
|
896
|
-
trace_id TEXT NOT NULL,
|
|
897
|
-
source TEXT NOT NULL,
|
|
898
|
-
reason TEXT,
|
|
899
|
-
score INTEGER DEFAULT 0,
|
|
900
|
-
status TEXT DEFAULT 'pending',
|
|
901
|
-
enqueued_at TEXT,
|
|
902
|
-
started_at TEXT,
|
|
903
|
-
completed_at TEXT,
|
|
904
|
-
resolution TEXT,
|
|
905
|
-
created_at TEXT NOT NULL,
|
|
906
|
-
updated_at TEXT NOT NULL
|
|
907
|
-
);
|
|
908
|
-
CREATE TABLE IF NOT EXISTS evolution_events (
|
|
909
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
910
|
-
trace_id TEXT NOT NULL,
|
|
911
|
-
task_id TEXT,
|
|
912
|
-
stage TEXT NOT NULL,
|
|
913
|
-
level TEXT DEFAULT 'info',
|
|
914
|
-
message TEXT NOT NULL,
|
|
915
|
-
summary TEXT,
|
|
916
|
-
metadata_json TEXT,
|
|
917
|
-
created_at TEXT NOT NULL
|
|
918
|
-
);
|
|
919
|
-
`);
|
|
920
|
-
// V2 migration: Add V2 columns to evolution_tasks if they don't exist
|
|
921
|
-
// SQLite does not support IF NOT EXISTS for ADD COLUMN, so we must check manually
|
|
922
|
-
// before each ALTER to avoid "duplicate column name" errors on existing DBs
|
|
923
|
-
const v2Columns = [
|
|
924
|
-
{ name: 'task_kind', type: 'TEXT' },
|
|
925
|
-
{ name: 'priority', type: 'TEXT' },
|
|
926
|
-
{ name: 'retry_count', type: 'INTEGER' },
|
|
927
|
-
{ name: 'max_retries', type: 'INTEGER' },
|
|
928
|
-
{ name: 'last_error', type: 'TEXT' },
|
|
929
|
-
{ name: 'result_ref', type: 'TEXT' },
|
|
930
|
-
];
|
|
931
|
-
for (const col of v2Columns) {
|
|
932
|
-
const exists = this.db.prepare(`PRAGMA table_info(evolution_tasks)`).all()
|
|
933
|
-
.some((row) => row.name === col.name);
|
|
934
|
-
if (!exists) {
|
|
935
|
-
this.db.exec(`ALTER TABLE evolution_tasks ADD COLUMN ${col.name} ${col.type}`);
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
this.db.exec(`
|
|
939
|
-
CREATE VIEW IF NOT EXISTS v_error_clusters AS
|
|
940
|
-
SELECT tool_name, COALESCE(error_type, 'unknown') AS error_type, COUNT(*) AS occurrences
|
|
941
|
-
FROM tool_calls
|
|
942
|
-
WHERE outcome = 'failure'
|
|
943
|
-
GROUP BY tool_name, COALESCE(error_type, 'unknown')
|
|
944
|
-
ORDER BY occurrences DESC;
|
|
945
|
-
CREATE VIEW IF NOT EXISTS v_principle_effectiveness AS
|
|
946
|
-
SELECT event_type, COUNT(*) AS total
|
|
947
|
-
FROM principle_events
|
|
948
|
-
GROUP BY event_type
|
|
949
|
-
ORDER BY total DESC;
|
|
950
|
-
CREATE VIEW IF NOT EXISTS v_sample_queue AS
|
|
951
|
-
SELECT review_status, COUNT(*) AS total
|
|
952
|
-
FROM correction_samples
|
|
953
|
-
GROUP BY review_status;
|
|
954
|
-
CREATE INDEX IF NOT EXISTS idx_assistant_turns_session_id ON assistant_turns(session_id);
|
|
955
|
-
CREATE INDEX IF NOT EXISTS idx_assistant_turns_created_at ON assistant_turns(created_at);
|
|
956
|
-
CREATE INDEX IF NOT EXISTS idx_assistant_turns_provider_model ON assistant_turns(provider, model);
|
|
957
|
-
CREATE INDEX IF NOT EXISTS idx_user_turns_session_id ON user_turns(session_id);
|
|
958
|
-
CREATE INDEX IF NOT EXISTS idx_tool_calls_session_id ON tool_calls(session_id);
|
|
959
|
-
CREATE INDEX IF NOT EXISTS idx_tool_calls_created_at ON tool_calls(created_at);
|
|
960
|
-
CREATE INDEX IF NOT EXISTS idx_pain_events_session_id ON pain_events(session_id);
|
|
961
|
-
CREATE INDEX IF NOT EXISTS idx_correction_samples_review_status ON correction_samples(review_status);
|
|
962
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_trace_id ON evolution_tasks(trace_id);
|
|
963
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_status ON evolution_tasks(status);
|
|
964
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_tasks_created_at ON evolution_tasks(created_at);
|
|
965
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_events_trace_id ON evolution_events(trace_id);
|
|
966
|
-
CREATE INDEX IF NOT EXISTS idx_evolution_events_created_at ON evolution_events(created_at);
|
|
967
|
-
`);
|
|
968
|
-
const row = this.db.prepare('SELECT version FROM schema_version LIMIT 1').get();
|
|
969
|
-
this.migrateSchema(row?.version);
|
|
970
|
-
if (!row) {
|
|
971
|
-
this.db.prepare('INSERT INTO schema_version (version) VALUES (?)').run(SCHEMA_VERSION);
|
|
972
|
-
}
|
|
973
|
-
else if (row.version !== SCHEMA_VERSION) {
|
|
974
|
-
this.db.prepare('UPDATE schema_version SET version = ?').run(SCHEMA_VERSION);
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
importLegacyArtifacts() {
|
|
978
|
-
this.importLegacySessions();
|
|
979
|
-
this.importLegacyEvents();
|
|
980
|
-
this.importLegacyEvolution();
|
|
981
|
-
}
|
|
982
|
-
migrateSchema(_fromVersion) {
|
|
983
|
-
this.db.exec(`
|
|
984
|
-
DROP VIEW IF EXISTS v_daily_metrics;
|
|
985
|
-
CREATE VIEW IF NOT EXISTS v_daily_metrics AS
|
|
986
|
-
WITH tool_daily AS (
|
|
987
|
-
SELECT
|
|
988
|
-
substr(created_at, 1, 10) AS day,
|
|
989
|
-
COUNT(*) AS tool_calls,
|
|
990
|
-
SUM(CASE WHEN outcome = 'failure' THEN 1 ELSE 0 END) AS failures
|
|
991
|
-
FROM tool_calls
|
|
992
|
-
GROUP BY substr(created_at, 1, 10)
|
|
993
|
-
),
|
|
994
|
-
correction_daily AS (
|
|
995
|
-
SELECT
|
|
996
|
-
substr(created_at, 1, 10) AS day,
|
|
997
|
-
SUM(CASE WHEN correction_detected = 1 THEN 1 ELSE 0 END) AS user_corrections
|
|
998
|
-
FROM user_turns
|
|
999
|
-
GROUP BY substr(created_at, 1, 10)
|
|
1000
|
-
)
|
|
1001
|
-
SELECT
|
|
1002
|
-
tool_daily.day AS day,
|
|
1003
|
-
tool_daily.tool_calls AS tool_calls,
|
|
1004
|
-
tool_daily.failures AS failures,
|
|
1005
|
-
COALESCE(correction_daily.user_corrections, 0) AS user_corrections
|
|
1006
|
-
FROM tool_daily
|
|
1007
|
-
LEFT JOIN correction_daily ON correction_daily.day = tool_daily.day;
|
|
1008
|
-
`);
|
|
1009
|
-
}
|
|
1010
|
-
/**
|
|
1011
|
-
* Get daily metrics for analytics.
|
|
1012
|
-
*
|
|
1013
|
-
* Returns: Analytics data aggregated from trajectory database.
|
|
1014
|
-
* Not: Runtime truth or real-time queue state.
|
|
1015
|
-
*/
|
|
1016
|
-
dailyMetrics() {
|
|
1017
|
-
return this.db.prepare('SELECT * FROM v_daily_metrics ORDER BY day ASC').all();
|
|
1018
|
-
}
|
|
1019
|
-
importLegacySessions() {
|
|
1020
|
-
const key = 'legacy:sessions';
|
|
1021
|
-
if (this.isImported(key))
|
|
1022
|
-
return;
|
|
1023
|
-
const sessionDir = resolvePdPath(this.workspaceDir, 'SESSION_DIR');
|
|
1024
|
-
if (!fs.existsSync(sessionDir))
|
|
1025
|
-
return;
|
|
1026
|
-
for (const file of fs.readdirSync(sessionDir).filter((entry) => entry.endsWith('.json'))) {
|
|
1027
|
-
try {
|
|
1028
|
-
const content = JSON.parse(fs.readFileSync(path.join(sessionDir, file), 'utf8'));
|
|
1029
|
-
if (content.sessionId) {
|
|
1030
|
-
const startedAt = typeof content.lastActivityAt === 'number'
|
|
1031
|
-
? new Date(content.lastActivityAt).toISOString()
|
|
1032
|
-
: nowIso();
|
|
1033
|
-
this.recordSession({ sessionId: content.sessionId, startedAt });
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
catch {
|
|
1037
|
-
// Ignore malformed legacy sessions.
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
this.markImported(key);
|
|
1041
|
-
}
|
|
1042
|
-
importLegacyEvents() {
|
|
1043
|
-
const key = 'legacy:events';
|
|
1044
|
-
if (this.isImported(key))
|
|
1045
|
-
return;
|
|
1046
|
-
const eventsPath = path.join(this.stateDir, 'logs', 'events.jsonl');
|
|
1047
|
-
if (!fs.existsSync(eventsPath))
|
|
1048
|
-
return;
|
|
1049
|
-
const raw = fs.readFileSync(eventsPath, 'utf8').trim();
|
|
1050
|
-
if (!raw) {
|
|
1051
|
-
this.markImported(key);
|
|
1052
|
-
return;
|
|
1053
|
-
}
|
|
1054
|
-
for (const line of raw.split('\n')) {
|
|
1055
|
-
try {
|
|
1056
|
-
const event = JSON.parse(line);
|
|
1057
|
-
if (event.type === 'pain_signal' && event.sessionId) {
|
|
1058
|
-
this.recordPainEvent({
|
|
1059
|
-
sessionId: event.sessionId,
|
|
1060
|
-
source: String(event.data?.source ?? 'legacy'),
|
|
1061
|
-
score: Number(event.data?.score ?? 0),
|
|
1062
|
-
reason: typeof event.data?.reason === 'string' ? event.data.reason : null,
|
|
1063
|
-
severity: typeof event.data?.severity === 'string' ? event.data.severity : null,
|
|
1064
|
-
origin: typeof event.data?.origin === 'string' ? event.data.origin : null,
|
|
1065
|
-
confidence: typeof event.data?.confidence === 'number' ? event.data.confidence : null,
|
|
1066
|
-
createdAt: event.ts,
|
|
1067
|
-
});
|
|
1068
|
-
}
|
|
1069
|
-
if (event.type === 'trust_change') {
|
|
1070
|
-
this.recordTrustChange({
|
|
1071
|
-
sessionId: event.sessionId,
|
|
1072
|
-
previousScore: Number(event.data?.previousScore ?? 0),
|
|
1073
|
-
newScore: Number(event.data?.newScore ?? 0),
|
|
1074
|
-
delta: Number(event.data?.delta ?? 0),
|
|
1075
|
-
reason: String(event.data?.reason ?? 'legacy'),
|
|
1076
|
-
createdAt: event.ts,
|
|
1077
|
-
});
|
|
1078
|
-
}
|
|
1079
|
-
if (event.type === 'gate_block') {
|
|
1080
|
-
this.recordGateBlock({
|
|
1081
|
-
sessionId: event.sessionId,
|
|
1082
|
-
toolName: String(event.data?.toolName ?? 'unknown'),
|
|
1083
|
-
filePath: typeof event.data?.filePath === 'string' ? event.data.filePath : null,
|
|
1084
|
-
reason: String(event.data?.reason ?? 'legacy'),
|
|
1085
|
-
planStatus: typeof event.data?.planStatus === 'string' ? event.data.planStatus : null,
|
|
1086
|
-
createdAt: event.ts,
|
|
1087
|
-
});
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
catch {
|
|
1091
|
-
// Ignore malformed legacy events.
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
this.markImported(key);
|
|
1095
|
-
}
|
|
1096
|
-
importLegacyEvolution() {
|
|
1097
|
-
const key = 'legacy:evolution';
|
|
1098
|
-
if (this.isImported(key))
|
|
1099
|
-
return;
|
|
1100
|
-
const evolutionPath = resolvePdPath(this.workspaceDir, 'EVOLUTION_STREAM');
|
|
1101
|
-
if (!fs.existsSync(evolutionPath))
|
|
1102
|
-
return;
|
|
1103
|
-
const raw = fs.readFileSync(evolutionPath, 'utf8').trim();
|
|
1104
|
-
if (!raw) {
|
|
1105
|
-
this.markImported(key);
|
|
1106
|
-
return;
|
|
1107
|
-
}
|
|
1108
|
-
for (const line of raw.split('\n')) {
|
|
1109
|
-
try {
|
|
1110
|
-
const event = JSON.parse(line);
|
|
1111
|
-
this.recordPrincipleEvent({
|
|
1112
|
-
principleId: typeof event.data?.principleId === 'string' ? event.data.principleId : null,
|
|
1113
|
-
eventType: String(event.type ?? 'legacy'),
|
|
1114
|
-
payload: event.data ?? {},
|
|
1115
|
-
createdAt: event.ts,
|
|
1116
|
-
});
|
|
1117
|
-
}
|
|
1118
|
-
catch {
|
|
1119
|
-
// Ignore malformed legacy evolution events.
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
this.markImported(key);
|
|
1123
|
-
}
|
|
1124
|
-
markImported(sourceKey) {
|
|
1125
|
-
this.withWrite(() => {
|
|
1126
|
-
this.db.prepare(`
|
|
1127
|
-
INSERT INTO ingest_checkpoint (source_key, imported_at)
|
|
1128
|
-
VALUES (?, ?)
|
|
1129
|
-
ON CONFLICT(source_key) DO UPDATE SET imported_at = excluded.imported_at
|
|
1130
|
-
`).run(sourceKey, nowIso());
|
|
1131
|
-
});
|
|
1132
|
-
}
|
|
1133
|
-
isImported(sourceKey) {
|
|
1134
|
-
const row = this.db.prepare('SELECT source_key FROM ingest_checkpoint WHERE source_key = ?').get(sourceKey);
|
|
1135
|
-
return Boolean(row);
|
|
1136
|
-
}
|
|
1137
|
-
maybeCreateCorrectionSample(sessionId) {
|
|
1138
|
-
const pending = this.db.prepare(`
|
|
1139
|
-
SELECT sample_id FROM correction_samples
|
|
1140
|
-
WHERE session_id = ? AND review_status = 'pending'
|
|
1141
|
-
ORDER BY created_at DESC
|
|
1142
|
-
LIMIT 1
|
|
1143
|
-
`).get(sessionId);
|
|
1144
|
-
if (pending?.sample_id)
|
|
1145
|
-
return;
|
|
1146
|
-
const correctionTurn = this.db.prepare(`
|
|
1147
|
-
SELECT id, references_assistant_turn_id, correction_cue, raw_text, blob_ref
|
|
1148
|
-
FROM user_turns
|
|
1149
|
-
WHERE session_id = ? AND correction_detected = 1
|
|
1150
|
-
ORDER BY id DESC
|
|
1151
|
-
LIMIT 1
|
|
1152
|
-
`).get(sessionId);
|
|
1153
|
-
if (!correctionTurn || !correctionTurn.references_assistant_turn_id)
|
|
1154
|
-
return;
|
|
1155
|
-
const failedCall = this.db.prepare(`
|
|
1156
|
-
SELECT id, tool_name, error_type, error_message
|
|
1157
|
-
FROM tool_calls
|
|
1158
|
-
WHERE session_id = ? AND outcome = 'failure'
|
|
1159
|
-
ORDER BY id DESC
|
|
1160
|
-
LIMIT 1
|
|
1161
|
-
`).get(sessionId);
|
|
1162
|
-
if (!failedCall)
|
|
1163
|
-
return;
|
|
1164
|
-
const successfulCalls = this.db.prepare(`
|
|
1165
|
-
SELECT id, tool_name
|
|
1166
|
-
FROM tool_calls
|
|
1167
|
-
WHERE session_id = ? AND outcome = 'success'
|
|
1168
|
-
ORDER BY id DESC
|
|
1169
|
-
LIMIT 3
|
|
1170
|
-
`).all(sessionId);
|
|
1171
|
-
if (successfulCalls.length === 0)
|
|
1172
|
-
return;
|
|
1173
|
-
const sampleId = `sample_${crypto.createHash('md5').update(`${sessionId}:${correctionTurn.id}:${successfulCalls[0].id}`).digest('hex').slice(0, 12)}`;
|
|
1174
|
-
const userRawText = this.restoreRawText(correctionTurn.raw_text, correctionTurn.blob_ref);
|
|
1175
|
-
const qualityScore = [
|
|
1176
|
-
correctionTurn.references_assistant_turn_id ? 35 : 0,
|
|
1177
|
-
correctionTurn.correction_cue ? 20 : 0,
|
|
1178
|
-
failedCall ? 20 : 0,
|
|
1179
|
-
successfulCalls.length > 0 ? 25 : 0,
|
|
1180
|
-
].reduce((sum, value) => sum + value, 0);
|
|
1181
|
-
this.withWrite(() => {
|
|
1182
|
-
this.db.prepare(`
|
|
1183
|
-
INSERT OR IGNORE INTO correction_samples (
|
|
1184
|
-
sample_id, session_id, bad_assistant_turn_id, user_correction_turn_id,
|
|
1185
|
-
recovery_tool_span_json, diff_excerpt, principle_ids_json, quality_score,
|
|
1186
|
-
review_status, export_mode, created_at, updated_at
|
|
1187
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pending', 'raw', ?, ?)
|
|
1188
|
-
`).run(sampleId, sessionId, Number(correctionTurn.references_assistant_turn_id), Number(correctionTurn.id), safeJson(successfulCalls.map((call) => ({ id: call.id, toolName: call.tool_name }))), summarizeForDiff(userRawText || String(failedCall.error_message ?? failedCall.error_type ?? failedCall.tool_name)), '[]', qualityScore, nowIso(), nowIso());
|
|
1189
|
-
});
|
|
1190
|
-
}
|
|
1191
|
-
recordExportAudit(exportKind, mode, approvedOnly, filePath, rowCount) {
|
|
1192
|
-
this.withWrite(() => {
|
|
1193
|
-
this.db.prepare(`
|
|
1194
|
-
INSERT INTO exports_audit (export_kind, mode, approved_only, file_path, row_count, created_at)
|
|
1195
|
-
VALUES (?, ?, ?, ?, ?, ?)
|
|
1196
|
-
`).run(exportKind, mode, approvedOnly ? 1 : 0, filePath, rowCount, nowIso());
|
|
1197
|
-
});
|
|
1198
|
-
}
|
|
1199
|
-
storeRawText(kind, text) {
|
|
1200
|
-
const excerpt = text.length > 200 ? `${text.slice(0, 197)}...` : text;
|
|
1201
|
-
const bytes = Buffer.byteLength(text, 'utf8');
|
|
1202
|
-
if (bytes <= this.blobInlineThresholdBytes) {
|
|
1203
|
-
return { inlineText: text, blobRef: null, excerpt };
|
|
1204
|
-
}
|
|
1205
|
-
const hash = crypto.createHash('sha256').update(text).digest('hex');
|
|
1206
|
-
const relativePath = `${kind}-${hash}.txt`;
|
|
1207
|
-
const fullPath = path.join(this.blobDir, relativePath);
|
|
1208
|
-
if (!fs.existsSync(fullPath)) {
|
|
1209
|
-
fs.writeFileSync(fullPath, text, 'utf8');
|
|
1210
|
-
}
|
|
1211
|
-
return { inlineText: null, blobRef: relativePath, excerpt };
|
|
1212
|
-
}
|
|
1213
|
-
restoreRawText(inlineText, blobRef) {
|
|
1214
|
-
if (inlineText)
|
|
1215
|
-
return inlineText;
|
|
1216
|
-
if (!blobRef)
|
|
1217
|
-
return '';
|
|
1218
|
-
const fullPath = path.join(this.blobDir, blobRef);
|
|
1219
|
-
return fs.existsSync(fullPath) ? fs.readFileSync(fullPath, 'utf8') : '';
|
|
1220
|
-
}
|
|
1221
|
-
computeBlobBytes() {
|
|
1222
|
-
if (!fs.existsSync(this.blobDir))
|
|
1223
|
-
return 0;
|
|
1224
|
-
return fs.readdirSync(this.blobDir).reduce((sum, file) => sum + fileSizeIfExists(path.join(this.blobDir, file)), 0);
|
|
1225
|
-
}
|
|
1226
|
-
pruneUnreferencedBlobs() {
|
|
1227
|
-
if (!fs.existsSync(this.blobDir)) {
|
|
1228
|
-
return { removedFiles: 0, reclaimedBytes: 0 };
|
|
1229
|
-
}
|
|
1230
|
-
const referenced = new Set();
|
|
1231
|
-
const rows = this.db.prepare(`
|
|
1232
|
-
SELECT blob_ref FROM assistant_turns WHERE blob_ref IS NOT NULL
|
|
1233
|
-
UNION
|
|
1234
|
-
SELECT blob_ref FROM user_turns WHERE blob_ref IS NOT NULL
|
|
1235
|
-
`).all();
|
|
1236
|
-
for (const row of rows) {
|
|
1237
|
-
if (row.blob_ref)
|
|
1238
|
-
referenced.add(String(row.blob_ref));
|
|
1239
|
-
}
|
|
1240
|
-
const now = Date.now();
|
|
1241
|
-
let removedFiles = 0;
|
|
1242
|
-
let reclaimedBytes = 0;
|
|
1243
|
-
for (const entry of fs.readdirSync(this.blobDir)) {
|
|
1244
|
-
if (referenced.has(entry))
|
|
1245
|
-
continue;
|
|
1246
|
-
const fullPath = path.join(this.blobDir, entry);
|
|
1247
|
-
let stat;
|
|
1248
|
-
try {
|
|
1249
|
-
stat = fs.statSync(fullPath);
|
|
1250
|
-
}
|
|
1251
|
-
catch {
|
|
1252
|
-
continue;
|
|
1253
|
-
}
|
|
1254
|
-
if (!stat.isFile())
|
|
1255
|
-
continue;
|
|
1256
|
-
if (this.orphanBlobGraceMs > 0 && now - stat.mtimeMs < this.orphanBlobGraceMs)
|
|
1257
|
-
continue;
|
|
1258
|
-
reclaimedBytes += stat.size;
|
|
1259
|
-
removedFiles += 1;
|
|
1260
|
-
fs.rmSync(fullPath, { force: true });
|
|
1261
|
-
}
|
|
1262
|
-
return { removedFiles, reclaimedBytes };
|
|
1263
|
-
}
|
|
1264
|
-
withWrite(fn) {
|
|
1265
|
-
return withLock(this.dbPath, fn, { lockSuffix: '.trajectory.lock', lockStaleMs: 30000 });
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
export class TrajectoryRegistry {
|
|
1269
|
-
static instances = new Map();
|
|
1270
|
-
static get(workspaceDir, opts = {}) {
|
|
1271
|
-
const normalized = path.resolve(workspaceDir);
|
|
1272
|
-
const existing = this.instances.get(normalized);
|
|
1273
|
-
if (existing)
|
|
1274
|
-
return existing;
|
|
1275
|
-
const created = new TrajectoryDatabase({ workspaceDir: normalized, ...opts });
|
|
1276
|
-
this.instances.set(normalized, created);
|
|
1277
|
-
return created;
|
|
1278
|
-
}
|
|
1279
|
-
static dispose(workspaceDir) {
|
|
1280
|
-
const normalized = path.resolve(workspaceDir);
|
|
1281
|
-
const instance = this.instances.get(normalized);
|
|
1282
|
-
if (instance) {
|
|
1283
|
-
instance.dispose();
|
|
1284
|
-
this.instances.delete(normalized);
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
static clear() {
|
|
1288
|
-
for (const instance of this.instances.values()) {
|
|
1289
|
-
instance.dispose();
|
|
1290
|
-
}
|
|
1291
|
-
this.instances.clear();
|
|
1292
|
-
}
|
|
1293
|
-
static use(workspaceDir, fn, opts = {}) {
|
|
1294
|
-
const normalized = path.resolve(workspaceDir);
|
|
1295
|
-
const existing = this.instances.get(normalized);
|
|
1296
|
-
if (existing) {
|
|
1297
|
-
return fn(existing);
|
|
1298
|
-
}
|
|
1299
|
-
const transient = new TrajectoryDatabase({ workspaceDir: normalized, ...opts });
|
|
1300
|
-
try {
|
|
1301
|
-
return fn(transient);
|
|
1302
|
-
}
|
|
1303
|
-
finally {
|
|
1304
|
-
transient.dispose();
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
}
|