@unerr-ai/unerr 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -45
- package/dist/cli.js +37443 -36022
- package/package.json +2 -1
- package/dist/behaviors/agent-llm-bridge.js +0 -166
- package/dist/behaviors/architecture-guard.js +0 -256
- package/dist/behaviors/auto-doc.js +0 -247
- package/dist/behaviors/cascade-guard.js +0 -289
- package/dist/behaviors/change-narrative.js +0 -270
- package/dist/behaviors/convention-drift.js +0 -290
- package/dist/behaviors/framework.js +0 -235
- package/dist/behaviors/guard-formatter.js +0 -44
- package/dist/behaviors/incomplete-work.js +0 -270
- package/dist/behaviors/loop-breaker.js +0 -300
- package/dist/behaviors/session-continuity.js +0 -208
- package/dist/commands/branches.js +0 -97
- package/dist/commands/check-commit.js +0 -225
- package/dist/commands/compress-output.js +0 -64
- package/dist/commands/config-verify.js +0 -243
- package/dist/commands/daemon.js +0 -905
- package/dist/commands/dashboard.js +0 -52
- package/dist/commands/debug.js +0 -200
- package/dist/commands/enrich.js +0 -184
- package/dist/commands/exec.js +0 -233
- package/dist/commands/gain.js +0 -156
- package/dist/commands/hook.js +0 -88
- package/dist/commands/index.js +0 -88
- package/dist/commands/init.js +0 -74
- package/dist/commands/install.js +0 -505
- package/dist/commands/learn.js +0 -116
- package/dist/commands/manifest.js +0 -193
- package/dist/commands/rewind.js +0 -103
- package/dist/commands/serve.js +0 -19
- package/dist/commands/setup-wizard.js +0 -414
- package/dist/commands/skills.js +0 -64
- package/dist/commands/stats.js +0 -20
- package/dist/commands/status.js +0 -654
- package/dist/commands/timeline.js +0 -139
- package/dist/commands/uninstall.js +0 -230
- package/dist/components/App.js +0 -109
- package/dist/components/Banner.js +0 -12
- package/dist/components/ConfirmPrompt.js +0 -25
- package/dist/components/DriftSummary.js +0 -23
- package/dist/components/GradeBadge.js +0 -15
- package/dist/components/HealthCard.js +0 -18
- package/dist/components/InkSpinner.js +0 -22
- package/dist/components/InputBox.js +0 -17
- package/dist/components/KeyValue.js +0 -13
- package/dist/components/MessageList.js +0 -14
- package/dist/components/ProgressBar.js +0 -26
- package/dist/components/Section.js +0 -16
- package/dist/components/SessionSummaryCard.js +0 -73
- package/dist/components/StartupDisplay.js +0 -24
- package/dist/components/StatusDashboard.js +0 -57
- package/dist/components/StatusLine.js +0 -8
- package/dist/components/StepLine.js +0 -22
- package/dist/components/Theme.js +0 -20
- package/dist/components/ToolProgress.js +0 -8
- package/dist/components/ViolationList.js +0 -21
- package/dist/components/render.js +0 -13
- package/dist/config/agent-registry.js +0 -237
- package/dist/config/claude-settings-hooks.js +0 -304
- package/dist/config/hook-installer.js +0 -65
- package/dist/config/instruction-writer.js +0 -388
- package/dist/config/mcp-config-writer.js +0 -266
- package/dist/config/settings.js +0 -174
- package/dist/config/tool-detector.js +0 -42
- package/dist/config/value-surfacing.js +0 -119
- package/dist/core/context-assembly.js +0 -108
- package/dist/core/conversation.js +0 -33
- package/dist/core/local-chat-provider.js +0 -475
- package/dist/core/provider-factory.js +0 -55
- package/dist/core/providers.js +0 -90
- package/dist/core/query-engine.js +0 -174
- package/dist/daemon/api.js +0 -312
- package/dist/daemon/autostart.js +0 -119
- package/dist/daemon/bootstrap.js +0 -39
- package/dist/daemon/client.js +0 -164
- package/dist/daemon/detect-ci.js +0 -81
- package/dist/daemon/platform-linux.js +0 -146
- package/dist/daemon/platform-macos.js +0 -134
- package/dist/daemon/platform-windows.js +0 -116
- package/dist/daemon/process-manager.js +0 -299
- package/dist/daemon/protocol.js +0 -23
- package/dist/daemon/registry.js +0 -270
- package/dist/daemon/settings-schema.js +0 -72
- package/dist/daemon/system-health.js +0 -134
- package/dist/daemon/version-checker.js +0 -262
- package/dist/daemon/warm-start.js +0 -223
- package/dist/entrypoints/cli.js +0 -1043
- package/dist/entrypoints/daemon.js +0 -380
- package/dist/entrypoints/repl.js +0 -147
- package/dist/hooks/adapters/claude-code.js +0 -90
- package/dist/hooks/adapters/cline.js +0 -100
- package/dist/hooks/adapters/cursor.js +0 -98
- package/dist/hooks/hook-dedup.js +0 -79
- package/dist/hooks/hook-runner.js +0 -113
- package/dist/hooks/navigation-hooks.js +0 -175
- package/dist/hooks/prompt-hooks.js +0 -63
- package/dist/hooks/shell-hooks.js +0 -47
- package/dist/ignore.js +0 -111
- package/dist/intelligence/approach-suggester.js +0 -61
- package/dist/intelligence/ast-extractor.js +0 -2615
- package/dist/intelligence/ast-worker.js +0 -34
- package/dist/intelligence/background-indexer.js +0 -121
- package/dist/intelligence/blast-radius.js +0 -200
- package/dist/intelligence/community-detection.js +0 -691
- package/dist/intelligence/community-detector.js +0 -184
- package/dist/intelligence/computation-scheduler.js +0 -75
- package/dist/intelligence/confidence-propagation.js +0 -47
- package/dist/intelligence/convention-detector.js +0 -242
- package/dist/intelligence/convention-learner.js +0 -205
- package/dist/intelligence/convention-matcher.js +0 -205
- package/dist/intelligence/cozo-schema.js +0 -376
- package/dist/intelligence/decision-point-detector.js +0 -90
- package/dist/intelligence/deep-dive-tools.js +0 -586
- package/dist/intelligence/durability-scorer.js +0 -84
- package/dist/intelligence/exploration-cost.js +0 -204
- package/dist/intelligence/exploration-pattern-tracker.js +0 -61
- package/dist/intelligence/fact-generator.js +0 -322
- package/dist/intelligence/facts-schema.js +0 -90
- package/dist/intelligence/file-intelligence.js +0 -59
- package/dist/intelligence/graph-holder.js +0 -220
- package/dist/intelligence/graph-temporal-joiner.js +0 -238
- package/dist/intelligence/health-grade.js +0 -423
- package/dist/intelligence/health-grader.js +0 -200
- package/dist/intelligence/health-map-data.js +0 -259
- package/dist/intelligence/import-symbols.js +0 -136
- package/dist/intelligence/incremental-indexer.js +0 -658
- package/dist/intelligence/indexer/centrality.js +0 -62
- package/dist/intelligence/indexer/cfg-context.js +0 -95
- package/dist/intelligence/indexer/confidence.js +0 -34
- package/dist/intelligence/indexer/cross-file-resolver.js +0 -104
- package/dist/intelligence/indexer/edge-repair.js +0 -89
- package/dist/intelligence/indexer/entity-key.js +0 -17
- package/dist/intelligence/indexer/export-map.js +0 -132
- package/dist/intelligence/indexer/git-cochange.js +0 -128
- package/dist/intelligence/indexer/graph-patch.js +0 -147
- package/dist/intelligence/indexer/incremental.js +0 -78
- package/dist/intelligence/indexer/ingest.js +0 -160
- package/dist/intelligence/indexer/language-detect.js +0 -226
- package/dist/intelligence/indexer/metadata.js +0 -63
- package/dist/intelligence/indexer/mutation-tracker.js +0 -79
- package/dist/intelligence/indexer/orchestrator.js +0 -155
- package/dist/intelligence/indexer/plugin-interface.js +0 -31
- package/dist/intelligence/indexer/plugins/csharp.js +0 -440
- package/dist/intelligence/indexer/plugins/go.js +0 -335
- package/dist/intelligence/indexer/plugins/java.js +0 -370
- package/dist/intelligence/indexer/plugins/python.js +0 -358
- package/dist/intelligence/indexer/plugins/regex-fallback.js +0 -82
- package/dist/intelligence/indexer/plugins/ruby.js +0 -290
- package/dist/intelligence/indexer/plugins/rust.js +0 -484
- package/dist/intelligence/indexer/plugins/tier2-generic.js +0 -310
- package/dist/intelligence/indexer/plugins/typescript.js +0 -456
- package/dist/intelligence/indexer/resource-monitor.js +0 -93
- package/dist/intelligence/indexer/scip/decoder.js +0 -253
- package/dist/intelligence/indexer/scip/detector.js +0 -232
- package/dist/intelligence/indexer/scip/downloader.js +0 -427
- package/dist/intelligence/indexer/scip/fallback.js +0 -34
- package/dist/intelligence/indexer/scip/merger.js +0 -109
- package/dist/intelligence/indexer/scip/orchestrator.js +0 -433
- package/dist/intelligence/indexer/scip/runner.js +0 -98
- package/dist/intelligence/indexer/snapshot.js +0 -66
- package/dist/intelligence/indexer/test-detector.js +0 -196
- package/dist/intelligence/indexer/watch-integration.js +0 -61
- package/dist/intelligence/indexer/worker.js +0 -85
- package/dist/intelligence/local-convention-detector.js +0 -437
- package/dist/intelligence/local-embeddings.js +0 -190
- package/dist/intelligence/local-graph.js +0 -1946
- package/dist/intelligence/local-indexer.js +0 -1575
- package/dist/intelligence/local-llm.js +0 -163
- package/dist/intelligence/local-rule-generator.js +0 -154
- package/dist/intelligence/local-snapshot.js +0 -213
- package/dist/intelligence/negative-knowledge.js +0 -103
- package/dist/intelligence/persistent-db.js +0 -85
- package/dist/intelligence/query-router.js +0 -2556
- package/dist/intelligence/risk-classifier.js +0 -116
- package/dist/intelligence/rule-evaluator.js +0 -380
- package/dist/intelligence/rule-generator.js +0 -49
- package/dist/intelligence/search-index.js +0 -173
- package/dist/intelligence/semantic/docstring-extractor.js +0 -67
- package/dist/intelligence/semantic/embedding-store.js +0 -52
- package/dist/intelligence/semantic/enrichment-orchestrator.js +0 -48
- package/dist/intelligence/semantic/git-message-miner.js +0 -114
- package/dist/intelligence/semantic/identifier-tokenizer.js +0 -51
- package/dist/intelligence/semantic/node2vec-embeddings.js +0 -71
- package/dist/intelligence/semantic/node2vec-walks.js +0 -103
- package/dist/intelligence/semantic/path-domain-inference.js +0 -112
- package/dist/intelligence/semantic/similarity-engine.js +0 -60
- package/dist/intelligence/semantic/tfidf-vectors.js +0 -88
- package/dist/intelligence/session-brief-builder.js +0 -159
- package/dist/intelligence/session-context.js +0 -221
- package/dist/intelligence/session-health-monitor.js +0 -211
- package/dist/intelligence/session-narrative.js +0 -197
- package/dist/intelligence/session-pattern-analyzer.js +0 -218
- package/dist/intelligence/signal-scorer.js +0 -390
- package/dist/intelligence/signal-show-store.js +0 -182
- package/dist/intelligence/smart-truncate.js +0 -158
- package/dist/intelligence/subgraph-cache.js +0 -88
- package/dist/intelligence/temporal-facts.js +0 -494
- package/dist/intelligence/token-estimator.js +0 -100
- package/dist/intelligence/tool-injector.js +0 -87
- package/dist/intelligence/tree-sitter-loader.js +0 -71
- package/dist/intelligence/worker-pool.js +0 -116
- package/dist/proxy/arg-validator.js +0 -79
- package/dist/proxy/auto-bootstrap.js +0 -167
- package/dist/proxy/bridge.js +0 -147
- package/dist/proxy/budget-enforcer.js +0 -70
- package/dist/proxy/compression-quality-monitor.js +0 -160
- package/dist/proxy/compression-stats.js +0 -51
- package/dist/proxy/context-rot-detector.js +0 -137
- package/dist/proxy/drift-detector.js +0 -139
- package/dist/proxy/efficiency-tracker.js +0 -79
- package/dist/proxy/fact-ranking.js +0 -154
- package/dist/proxy/format-encoder.js +0 -266
- package/dist/proxy/http-transport.js +0 -90
- package/dist/proxy/lifecycle-actor.js +0 -55
- package/dist/proxy/lifecycle-machine.js +0 -187
- package/dist/proxy/log-tailer.js +0 -265
- package/dist/proxy/model-pricing.js +0 -98
- package/dist/proxy/network-firewall.js +0 -141
- package/dist/proxy/nudge-state.js +0 -93
- package/dist/proxy/output-compressor.js +0 -185
- package/dist/proxy/pid-lock.js +0 -291
- package/dist/proxy/proxy-context.js +0 -11
- package/dist/proxy/proxy.js +0 -2633
- package/dist/proxy/response-enrichment.js +0 -32
- package/dist/proxy/response-envelope.js +0 -313
- package/dist/proxy/session-dedup.js +0 -82
- package/dist/proxy/session-legend.js +0 -30
- package/dist/proxy/session-persistence.js +0 -210
- package/dist/proxy/session-resume.js +0 -94
- package/dist/proxy/session-stats.js +0 -513
- package/dist/proxy/shell-classifier.js +0 -1346
- package/dist/proxy/shell-compression-log.js +0 -93
- package/dist/proxy/shell-compressor.js +0 -390
- package/dist/proxy/shell-graph-boost.js +0 -202
- package/dist/proxy/shell-monitor-map.js +0 -18
- package/dist/proxy/shell-stats.js +0 -54
- package/dist/proxy/shell-strategies/cloud.js +0 -215
- package/dist/proxy/shell-strategies/diff.js +0 -159
- package/dist/proxy/shell-strategies/error-diagnostic.js +0 -796
- package/dist/proxy/shell-strategies/filter-dsl.js +0 -358
- package/dist/proxy/shell-strategies/git-status.js +0 -177
- package/dist/proxy/shell-strategies/key-value.js +0 -193
- package/dist/proxy/shell-strategies/log-text.js +0 -154
- package/dist/proxy/shell-strategies/omni.js +0 -188
- package/dist/proxy/shell-strategies/progress.js +0 -55
- package/dist/proxy/shell-strategies/redact.js +0 -76
- package/dist/proxy/shell-strategies/structured.js +0 -241
- package/dist/proxy/shell-strategies/tabular.js +0 -243
- package/dist/proxy/shell-strategies/test-results-types.js +0 -13
- package/dist/proxy/shell-strategies/test-results.js +0 -784
- package/dist/proxy/shell-strategies/tree-paths.js +0 -144
- package/dist/proxy/shell-strategies/yaml.js +0 -182
- package/dist/proxy/shell-tee.js +0 -111
- package/dist/proxy/signal-dedup.js +0 -171
- package/dist/proxy/startup-renderer.js +0 -158
- package/dist/proxy/task-token-display.js +0 -38
- package/dist/proxy/token-counter.js +0 -61
- package/dist/proxy/tool-clusters.js +0 -273
- package/dist/proxy/tool-definitions.js +0 -525
- package/dist/proxy/transport-mux.js +0 -229
- package/dist/proxy/wire-cap.js +0 -268
- package/dist/rules/developer.mozilla.org.json +0 -9
- package/dist/rules/github.com.json +0 -21
- package/dist/schemas/api/skills.js +0 -19
- package/dist/schemas/common/errors.js +0 -7
- package/dist/schemas/common/headers.js +0 -5
- package/dist/schemas/entities/edge.js +0 -25
- package/dist/schemas/entities/entity.js +0 -22
- package/dist/schemas/entities/rule.js +0 -18
- package/dist/schemas/index.js +0 -14
- package/dist/server/event-bus.js +0 -59
- package/dist/server/http.js +0 -156
- package/dist/server/middleware.js +0 -70
- package/dist/server/routes/drift.js +0 -97
- package/dist/server/routes/intelligence.js +0 -1217
- package/dist/server/routes/reasoning-quality.js +0 -444
- package/dist/server/routes/session.js +0 -86
- package/dist/server/routes/stream.js +0 -120
- package/dist/server/routes/system.js +0 -73
- package/dist/server/routes/temporal.js +0 -170
- package/dist/server/routes/timeline.js +0 -232
- package/dist/server/routes/token-flow.js +0 -403
- package/dist/skills/effectiveness-tracker.js +0 -93
- package/dist/skills/local-pack.js +0 -380
- package/dist/skills/resolver.js +0 -495
- package/dist/state-detector.js +0 -83
- package/dist/timeline/intent-detector.js +0 -263
- package/dist/timeline/loop-miner.js +0 -140
- package/dist/timeline/open-threads.js +0 -49
- package/dist/timeline/signal-reinforcer.js +0 -62
- package/dist/timeline/timeline-bootstrap.js +0 -151
- package/dist/timeline/timeline-store.js +0 -618
- package/dist/tools/coding/bash.js +0 -49
- package/dist/tools/coding/file-edit.js +0 -72
- package/dist/tools/coding/file-outline.js +0 -227
- package/dist/tools/coding/file-read-protocol.js +0 -425
- package/dist/tools/coding/file-read.js +0 -35
- package/dist/tools/coding/file-write.js +0 -43
- package/dist/tools/coding/glob-tool.js +0 -109
- package/dist/tools/coding/grep.js +0 -162
- package/dist/tools/coding/index.js +0 -27
- package/dist/tools/intelligence/index.js +0 -269
- package/dist/tools/intelligence/record-fact.js +0 -48
- package/dist/tools/intelligence/timeline-markers.js +0 -130
- package/dist/tools/registry.js +0 -47
- package/dist/tools/types.js +0 -8
- package/dist/tracking/auto-snapshot-triggers.js +0 -246
- package/dist/tracking/branch-context.js +0 -115
- package/dist/tracking/branch-snapshot.js +0 -217
- package/dist/tracking/causal-bridge.js +0 -317
- package/dist/tracking/circuit-breaker.js +0 -147
- package/dist/tracking/commit-watcher.js +0 -114
- package/dist/tracking/context-ledger.js +0 -119
- package/dist/tracking/correction-detector.js +0 -324
- package/dist/tracking/drift-tracker.js +0 -874
- package/dist/tracking/durability-tracker.js +0 -94
- package/dist/tracking/entity-rewind.js +0 -200
- package/dist/tracking/file-hash-state.js +0 -114
- package/dist/tracking/git-attribution.js +0 -132
- package/dist/tracking/git-trailers.js +0 -171
- package/dist/tracking/intelligence-counter.js +0 -46
- package/dist/tracking/intent-correlator.js +0 -202
- package/dist/tracking/intent-encoder.js +0 -52
- package/dist/tracking/intent-token-tracker.js +0 -159
- package/dist/tracking/ledger-archiver.js +0 -94
- package/dist/tracking/ledger-chains.js +0 -245
- package/dist/tracking/metrics-store.js +0 -361
- package/dist/tracking/native-watcher.js +0 -131
- package/dist/tracking/offline-rewind.js +0 -295
- package/dist/tracking/pending-violations.js +0 -74
- package/dist/tracking/persistence-effectiveness.js +0 -167
- package/dist/tracking/prompt-durability.js +0 -202
- package/dist/tracking/quality-signals.js +0 -213
- package/dist/tracking/redactor.js +0 -73
- package/dist/tracking/rewind-engine.js +0 -161
- package/dist/tracking/session-history.js +0 -128
- package/dist/tracking/session-receipt.js +0 -88
- package/dist/tracking/session-summary-writer.js +0 -157
- package/dist/tracking/shadow-ledger.js +0 -321
- package/dist/tracking/stash-manager.js +0 -258
- package/dist/tracking/timeline-fork.js +0 -213
- package/dist/tracking/timeline.js +0 -69
- package/dist/tracking/token-flow.js +0 -276
- package/dist/tracking/turn-segmenter.js +0 -122
- package/dist/tracking/weekly-accumulator.js +0 -179
- package/dist/tracking/working-snapshots.js +0 -188
- package/dist/tracking/workspace-manifest.js +0 -176
- package/dist/transport/http.js +0 -102
- package/dist/utils/counterfactual.js +0 -65
- package/dist/utils/deep-link.js +0 -34
- package/dist/utils/detect.js +0 -193
- package/dist/utils/exec.js +0 -73
- package/dist/utils/file-logger.js +0 -87
- package/dist/utils/format-error.js +0 -29
- package/dist/utils/git.js +0 -181
- package/dist/utils/log.js +0 -57
- package/dist/utils/logger.js +0 -35
- package/dist/utils/mcp-content-json.js +0 -8
- package/dist/utils/session-logger.js +0 -154
- package/dist/utils/startup-log.js +0 -512
- package/dist/utils/ui.js +0 -56
|
@@ -1,618 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Timeline Store — third CozoDB instance at `.unerr/timeline.db`.
|
|
3
|
-
*
|
|
4
|
-
* Parallel to graph.db (Layer 2) and facts.db (Layer 9). Holds:
|
|
5
|
-
* - turns : agent-turn rollups (one per closed turn)
|
|
6
|
-
* - intents : cross-session task groupings (ST-4)
|
|
7
|
-
* - intent_sessions : intent↔session many-to-many (ST-4)
|
|
8
|
-
* - markers : agent-emitted mark_* rows (ST-2)
|
|
9
|
-
* - derived_signals : mined patterns (hot files, loops, co-changes) (ST-3+)
|
|
10
|
-
* - signal_reinforcement : append-only reinforcement events per signal (ST-5)
|
|
11
|
-
*
|
|
12
|
-
* Schema rules (per CLAUDE.md):
|
|
13
|
-
* - Named Datalog syntax for any relation with 4+ columns.
|
|
14
|
-
* - All db.run() calls are async — always await.
|
|
15
|
-
* - Edit in place, no migrations until first public release.
|
|
16
|
-
*
|
|
17
|
-
* Isolation contract: this module NEVER reads or writes graph.db or facts.db.
|
|
18
|
-
* It only opens/owns `.unerr/timeline.db`. Existing Layer 2 / Layer 9 code is
|
|
19
|
-
* untouched.
|
|
20
|
-
*/
|
|
21
|
-
import { existsSync, mkdirSync } from "node:fs";
|
|
22
|
-
import { join } from "node:path";
|
|
23
|
-
const TIMELINE_DB_FILENAME = "timeline.db";
|
|
24
|
-
/**
|
|
25
|
-
* Open or create timeline.db at `{projectRoot}/.unerr/timeline.db`. Mirrors the
|
|
26
|
-
* factory pattern in `openFactsDb()` so the dynamic-import dance stays uniform.
|
|
27
|
-
*/
|
|
28
|
-
export async function openTimelineDb(projectRoot) {
|
|
29
|
-
const unerrDir = join(projectRoot, ".unerr");
|
|
30
|
-
mkdirSync(unerrDir, { recursive: true });
|
|
31
|
-
const dbPath = join(unerrDir, TIMELINE_DB_FILENAME);
|
|
32
|
-
const isNew = !existsSync(dbPath);
|
|
33
|
-
const cozoModule = await import("cozo-node");
|
|
34
|
-
const CozoDbConstructor = cozoModule.default
|
|
35
|
-
? cozoModule.default.CozoDb
|
|
36
|
-
: cozoModule.CozoDb;
|
|
37
|
-
const db = new CozoDbConstructor("sqlite", dbPath);
|
|
38
|
-
return { db, isNew, dbPath };
|
|
39
|
-
}
|
|
40
|
-
async function getExistingRelations(db) {
|
|
41
|
-
const result = await db.run("::relations");
|
|
42
|
-
return new Set(result.rows.map((row) => row[0]));
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Initialise timeline.db schema. Creates only missing relations — safe to call
|
|
46
|
-
* on fresh and existing databases. Schema is intentionally minimal here; the
|
|
47
|
-
* later sprints (ST-3/4/5) add CRUD methods on top.
|
|
48
|
-
*/
|
|
49
|
-
export async function initTimelineSchema(db) {
|
|
50
|
-
const existing = await getExistingRelations(db);
|
|
51
|
-
if (!existing.has("turns")) {
|
|
52
|
-
await db.run(`
|
|
53
|
-
:create turns {
|
|
54
|
-
turn_id: String
|
|
55
|
-
=>
|
|
56
|
-
session_id: String,
|
|
57
|
-
started_at: Float,
|
|
58
|
-
ended_at: Float,
|
|
59
|
-
opened_by: String,
|
|
60
|
-
closed_reason: String,
|
|
61
|
-
tool_count: Int,
|
|
62
|
-
file_count: Int,
|
|
63
|
-
edit_count: Int,
|
|
64
|
-
title: String,
|
|
65
|
-
outcome: String
|
|
66
|
-
}
|
|
67
|
-
`);
|
|
68
|
-
}
|
|
69
|
-
if (!existing.has("intents")) {
|
|
70
|
-
await db.run(`
|
|
71
|
-
:create intents {
|
|
72
|
-
intent_id: String
|
|
73
|
-
=>
|
|
74
|
-
title: String,
|
|
75
|
-
started_at: Float,
|
|
76
|
-
last_active_at: Float,
|
|
77
|
-
file_set: String,
|
|
78
|
-
file_set_hash: String,
|
|
79
|
-
status: String,
|
|
80
|
-
confidence: Float,
|
|
81
|
-
source: String
|
|
82
|
-
}
|
|
83
|
-
`);
|
|
84
|
-
}
|
|
85
|
-
if (!existing.has("intent_sessions")) {
|
|
86
|
-
await db.run(`
|
|
87
|
-
:create intent_sessions {
|
|
88
|
-
intent_id: String,
|
|
89
|
-
session_id: String
|
|
90
|
-
}
|
|
91
|
-
`);
|
|
92
|
-
}
|
|
93
|
-
if (!existing.has("markers")) {
|
|
94
|
-
await db.run(`
|
|
95
|
-
:create markers {
|
|
96
|
-
marker_id: String
|
|
97
|
-
=>
|
|
98
|
-
type: String,
|
|
99
|
-
text: String,
|
|
100
|
-
session_id: String,
|
|
101
|
-
turn_id: String,
|
|
102
|
-
ts: Float,
|
|
103
|
-
blocker_ref: String,
|
|
104
|
-
file_path: String
|
|
105
|
-
}
|
|
106
|
-
`);
|
|
107
|
-
}
|
|
108
|
-
if (!existing.has("derived_signals")) {
|
|
109
|
-
await db.run(`
|
|
110
|
-
:create derived_signals {
|
|
111
|
-
signal_id: String
|
|
112
|
-
=>
|
|
113
|
-
type: String,
|
|
114
|
-
scope: String,
|
|
115
|
-
content: String,
|
|
116
|
-
confidence: Float,
|
|
117
|
-
first_seen_at: Float,
|
|
118
|
-
last_seen_at: Float
|
|
119
|
-
}
|
|
120
|
-
`);
|
|
121
|
-
}
|
|
122
|
-
if (!existing.has("signal_reinforcement")) {
|
|
123
|
-
await db.run(`
|
|
124
|
-
:create signal_reinforcement {
|
|
125
|
-
signal_id: String,
|
|
126
|
-
ts: Float
|
|
127
|
-
=>
|
|
128
|
-
delta: Float,
|
|
129
|
-
source: String
|
|
130
|
-
}
|
|
131
|
-
`);
|
|
132
|
-
}
|
|
133
|
-
if (!existing.has("session_files")) {
|
|
134
|
-
// (session_id, file_path) — records distinct file touches per session for
|
|
135
|
-
// intent stitching (ST-4). Populated by the bootstrap when a turn closes.
|
|
136
|
-
await db.run(`
|
|
137
|
-
:create session_files {
|
|
138
|
-
session_id: String,
|
|
139
|
-
file_path: String
|
|
140
|
-
}
|
|
141
|
-
`);
|
|
142
|
-
}
|
|
143
|
-
if (!existing.has("session_agents")) {
|
|
144
|
-
// UX-1: which agent (Claude Code, Cursor, Codex, …) drove this session.
|
|
145
|
-
// Best-effort — populated when the MCP `initialize` handshake provided a
|
|
146
|
-
// clientInfo.name. Sessions without an identified client get "unknown".
|
|
147
|
-
await db.run(`
|
|
148
|
-
:create session_agents {
|
|
149
|
-
session_id: String
|
|
150
|
-
=>
|
|
151
|
-
agent_name: String,
|
|
152
|
-
first_seen: Float,
|
|
153
|
-
last_seen: Float
|
|
154
|
-
}
|
|
155
|
-
`);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
// ── Store facade ─────────────────────────────────────────────────────────────
|
|
159
|
-
/**
|
|
160
|
-
* Thin async wrapper around the timeline CozoDB instance. ST-1b only ships a
|
|
161
|
-
* minimal CRUD surface (turns + markers + listings). ST-3/4/5 extend this with
|
|
162
|
-
* intent, signal, and reinforcement methods.
|
|
163
|
-
*/
|
|
164
|
-
export class CozoTimelineStore {
|
|
165
|
-
db;
|
|
166
|
-
dbPath;
|
|
167
|
-
isNew;
|
|
168
|
-
constructor(db, dbPath, isNew) {
|
|
169
|
-
this.db = db;
|
|
170
|
-
this.dbPath = dbPath;
|
|
171
|
-
this.isNew = isNew;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Factory — opens the db and runs schema init. Use this instead of `new`.
|
|
175
|
-
*/
|
|
176
|
-
static async create(projectRoot) {
|
|
177
|
-
const { db, dbPath, isNew } = await openTimelineDb(projectRoot);
|
|
178
|
-
await initTimelineSchema(db);
|
|
179
|
-
return new CozoTimelineStore(db, dbPath, isNew);
|
|
180
|
-
}
|
|
181
|
-
/** Underlying handle for tests and advanced consumers. */
|
|
182
|
-
getDb() {
|
|
183
|
-
return this.db;
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Upsert a turn rollup. Called when the segmenter emits a turn-close event.
|
|
187
|
-
*/
|
|
188
|
-
async upsertTurn(turn) {
|
|
189
|
-
await this.db.run(`
|
|
190
|
-
?[turn_id, session_id, started_at, ended_at, opened_by, closed_reason, tool_count, file_count, edit_count, title, outcome] <-
|
|
191
|
-
[[$turn_id, $session_id, $started_at, $ended_at, $opened_by, $closed_reason, $tool_count, $file_count, $edit_count, $title, $outcome]]
|
|
192
|
-
:put turns {
|
|
193
|
-
turn_id
|
|
194
|
-
=>
|
|
195
|
-
session_id, started_at, ended_at, opened_by, closed_reason,
|
|
196
|
-
tool_count, file_count, edit_count, title, outcome
|
|
197
|
-
}
|
|
198
|
-
`, turn);
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* List turns with filters. Newest-first by `started_at`. Supports pagination
|
|
202
|
-
* (offset + limit) and substring search on `title`.
|
|
203
|
-
*/
|
|
204
|
-
async listTurns(opts = {}) {
|
|
205
|
-
const limit = Math.max(1, Math.min(opts.limit ?? 50, 500));
|
|
206
|
-
const offset = Math.max(0, opts.offset ?? 0);
|
|
207
|
-
const { whereClause, params } = buildTurnFilter(opts);
|
|
208
|
-
const query = `
|
|
209
|
-
?[turn_id, session_id, started_at, ended_at, opened_by, closed_reason, tool_count, file_count, edit_count, title, outcome] :=
|
|
210
|
-
*turns{
|
|
211
|
-
turn_id, session_id, started_at, ended_at, opened_by, closed_reason,
|
|
212
|
-
tool_count, file_count, edit_count, title, outcome
|
|
213
|
-
}${whereClause}
|
|
214
|
-
:order -started_at
|
|
215
|
-
:offset ${offset}
|
|
216
|
-
:limit ${limit}
|
|
217
|
-
`;
|
|
218
|
-
const result = await this.db.run(query, params);
|
|
219
|
-
return result.rows.map(rowToTurn);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Total number of turns matching the same filter as `listTurns`. Used for
|
|
223
|
-
* pagination footers ("page X of Y").
|
|
224
|
-
*/
|
|
225
|
-
async countTurns(opts = {}) {
|
|
226
|
-
const { whereClause, params } = buildTurnFilter(opts);
|
|
227
|
-
const result = await this.db.run(`?[count(turn_id)] := *turns{turn_id, session_id, started_at, ended_at, opened_by, closed_reason, tool_count, file_count, edit_count, title, outcome}${whereClause}`, params);
|
|
228
|
-
const row = result.rows[0];
|
|
229
|
-
return row ? Number(row[0]) : 0;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* List distinct sessions touched by `turns`, with aggregate stats and the
|
|
233
|
-
* agent that drove the session (when known). Powers the session + agent
|
|
234
|
-
* filters in the Timeline page.
|
|
235
|
-
*/
|
|
236
|
-
async listSessions(opts = {}) {
|
|
237
|
-
const limit = Math.max(1, Math.min(opts.limit ?? 200, 1000));
|
|
238
|
-
const filters = [];
|
|
239
|
-
const params = {};
|
|
240
|
-
if (typeof opts.fromTs === "number") {
|
|
241
|
-
filters.push("started_at >= $from_ts");
|
|
242
|
-
params.from_ts = opts.fromTs;
|
|
243
|
-
}
|
|
244
|
-
if (typeof opts.toTs === "number") {
|
|
245
|
-
filters.push("started_at <= $to_ts");
|
|
246
|
-
params.to_ts = opts.toTs;
|
|
247
|
-
}
|
|
248
|
-
const whereClause = filters.length > 0 ? `, ${filters.join(", ")}` : "";
|
|
249
|
-
const result = await this.db.run(`?[session_id, min(started_at), max(ended_at), count(turn_id), sum(edit_count), sum(file_count)] :=
|
|
250
|
-
*turns{turn_id, session_id, started_at, ended_at, edit_count, file_count}${whereClause}
|
|
251
|
-
:order -max(ended_at)
|
|
252
|
-
:limit ${limit}`, params);
|
|
253
|
-
const rows = result.rows.map((r) => ({
|
|
254
|
-
session_id: r[0],
|
|
255
|
-
first_seen: r[1],
|
|
256
|
-
last_seen: r[2],
|
|
257
|
-
turn_count: r[3],
|
|
258
|
-
edit_count: r[4],
|
|
259
|
-
file_count: r[5],
|
|
260
|
-
agent_name: "unknown",
|
|
261
|
-
}));
|
|
262
|
-
// Left-join the agent name lookup in a second pass — Cozo's join syntax
|
|
263
|
-
// doesn't compose cleanly with `:order` over aggregates here.
|
|
264
|
-
const agentMap = await this.getSessionAgents(rows.map((r) => r.session_id));
|
|
265
|
-
for (const r of rows) {
|
|
266
|
-
const a = agentMap.get(r.session_id);
|
|
267
|
-
if (a)
|
|
268
|
-
r.agent_name = a;
|
|
269
|
-
}
|
|
270
|
-
return rows;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Record (or refresh) the agent name for a session. Called from the bootstrap
|
|
274
|
-
* on each turn close — first_seen is preserved across calls, last_seen
|
|
275
|
-
* advances. Best-effort; no-op when agent_name is empty.
|
|
276
|
-
*/
|
|
277
|
-
async setSessionAgent(sessionId, agentName, nowMs = Date.now()) {
|
|
278
|
-
if (!agentName || agentName.length === 0)
|
|
279
|
-
return;
|
|
280
|
-
const existing = await this.db.run("?[first_seen] := *session_agents{session_id, first_seen}, session_id = $sid", { sid: sessionId });
|
|
281
|
-
const firstSeen = existing.rows.length > 0 ? existing.rows[0]?.[0] : nowMs;
|
|
282
|
-
await this.db.run(`?[session_id, agent_name, first_seen, last_seen] <-
|
|
283
|
-
[[$sid, $name, $first, $last]]
|
|
284
|
-
:put session_agents {
|
|
285
|
-
session_id => agent_name, first_seen, last_seen
|
|
286
|
-
}`, {
|
|
287
|
-
sid: sessionId,
|
|
288
|
-
name: agentName,
|
|
289
|
-
first: firstSeen,
|
|
290
|
-
last: nowMs,
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
/** Lookup the agent name for one session id. Returns null when unknown. */
|
|
294
|
-
async getSessionAgent(sessionId) {
|
|
295
|
-
const result = await this.db.run("?[agent_name] := *session_agents{session_id, agent_name}, session_id = $sid", { sid: sessionId });
|
|
296
|
-
const row = result.rows[0];
|
|
297
|
-
return row ? row[0] : null;
|
|
298
|
-
}
|
|
299
|
-
/** Batched agent lookup — used by listSessions to avoid N+1 queries. */
|
|
300
|
-
async getSessionAgents(sessionIds) {
|
|
301
|
-
const map = new Map();
|
|
302
|
-
if (sessionIds.length === 0)
|
|
303
|
-
return map;
|
|
304
|
-
// Cozo doesn't take an `IN` list directly; pull all and filter client-side.
|
|
305
|
-
const all = await this.db.run("?[session_id, agent_name] := *session_agents{session_id, agent_name}");
|
|
306
|
-
const wanted = new Set(sessionIds);
|
|
307
|
-
for (const r of all.rows) {
|
|
308
|
-
const sid = r[0];
|
|
309
|
-
if (wanted.has(sid))
|
|
310
|
-
map.set(sid, r[1]);
|
|
311
|
-
}
|
|
312
|
-
return map;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Distinct agent names with session counts. Powers the agent filter
|
|
316
|
-
* dropdown and the "agents over time" KPI.
|
|
317
|
-
*/
|
|
318
|
-
async listAgents() {
|
|
319
|
-
const result = await this.db.run(`?[agent_name, count(session_id), max(last_seen)] :=
|
|
320
|
-
*session_agents{session_id, agent_name, last_seen}
|
|
321
|
-
:order -max(last_seen)`);
|
|
322
|
-
return result.rows.map((r) => ({
|
|
323
|
-
agent_name: r[0],
|
|
324
|
-
session_count: r[1],
|
|
325
|
-
last_seen: r[2],
|
|
326
|
-
}));
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Bucketed activity counts for the heatmap. Returns one row per day in the
|
|
330
|
-
* `[fromTs, toTs]` range (inclusive), with zeros for empty days.
|
|
331
|
-
*/
|
|
332
|
-
async getActivityBuckets(opts) {
|
|
333
|
-
const bucket = opts.bucketMs ?? 24 * 60 * 60_000;
|
|
334
|
-
const turns = await this.listTurns({
|
|
335
|
-
fromTs: opts.fromTs,
|
|
336
|
-
toTs: opts.toTs,
|
|
337
|
-
limit: 5000,
|
|
338
|
-
});
|
|
339
|
-
const map = new Map();
|
|
340
|
-
// Pre-fill the range so empty days still render.
|
|
341
|
-
for (let t = bucketStart(opts.fromTs, bucket); t <= opts.toTs; t += bucket) {
|
|
342
|
-
map.set(t, { ts: t, turns: 0, edits: 0, tools: 0 });
|
|
343
|
-
}
|
|
344
|
-
for (const turn of turns) {
|
|
345
|
-
const b = bucketStart(turn.started_at, bucket);
|
|
346
|
-
const cur = map.get(b) ?? { ts: b, turns: 0, edits: 0, tools: 0 };
|
|
347
|
-
cur.turns += 1;
|
|
348
|
-
cur.edits += turn.edit_count;
|
|
349
|
-
cur.tools += turn.tool_count;
|
|
350
|
-
map.set(b, cur);
|
|
351
|
-
}
|
|
352
|
-
return [...map.values()].sort((a, b) => a.ts - b.ts);
|
|
353
|
-
}
|
|
354
|
-
/** Insert a marker row. Used by ST-2 marker tool handlers. */
|
|
355
|
-
async insertMarker(marker) {
|
|
356
|
-
await this.db.run(`
|
|
357
|
-
?[marker_id, type, text, session_id, turn_id, ts, blocker_ref, file_path] <-
|
|
358
|
-
[[$marker_id, $type, $text, $session_id, $turn_id, $ts, $blocker_ref, $file_path]]
|
|
359
|
-
:put markers {
|
|
360
|
-
marker_id
|
|
361
|
-
=>
|
|
362
|
-
type, text, session_id, turn_id, ts, blocker_ref, file_path
|
|
363
|
-
}
|
|
364
|
-
`, marker);
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* List markers, newest first. ST-3 open-threads uses this to find blockers
|
|
368
|
-
* without resolutions.
|
|
369
|
-
*/
|
|
370
|
-
async listMarkers(opts = {}) {
|
|
371
|
-
const limit = opts.limit ?? 100;
|
|
372
|
-
const filters = [];
|
|
373
|
-
if (opts.sessionId)
|
|
374
|
-
filters.push("session_id = $session_id");
|
|
375
|
-
if (opts.type)
|
|
376
|
-
filters.push("type = $type");
|
|
377
|
-
const filterClause = filters.length > 0 ? `, ${filters.join(", ")}` : "";
|
|
378
|
-
const query = `
|
|
379
|
-
?[marker_id, type, text, session_id, turn_id, ts, blocker_ref, file_path] :=
|
|
380
|
-
*markers{
|
|
381
|
-
marker_id, type, text, session_id, turn_id, ts, blocker_ref, file_path
|
|
382
|
-
}${filterClause}
|
|
383
|
-
:order -ts
|
|
384
|
-
:limit ${Math.max(1, Math.min(limit, 500))}
|
|
385
|
-
`;
|
|
386
|
-
const params = {};
|
|
387
|
-
if (opts.sessionId)
|
|
388
|
-
params.session_id = opts.sessionId;
|
|
389
|
-
if (opts.type)
|
|
390
|
-
params.type = opts.type;
|
|
391
|
-
const result = await this.db.run(query, params);
|
|
392
|
-
return result.rows.map(rowToMarker);
|
|
393
|
-
}
|
|
394
|
-
/**
|
|
395
|
-
* Record distinct file touches for a session. Called by the bootstrap on
|
|
396
|
-
* each turn-close so the intent stitcher (ST-4) has a file set to match on.
|
|
397
|
-
*/
|
|
398
|
-
async recordSessionFiles(sessionId, files) {
|
|
399
|
-
const list = [...new Set([...files].filter((f) => f.length > 0))];
|
|
400
|
-
if (list.length === 0)
|
|
401
|
-
return;
|
|
402
|
-
// CozoDB :put against a many-row relation works one row at a time.
|
|
403
|
-
for (const fp of list) {
|
|
404
|
-
await this.db.run(`?[session_id, file_path] <- [[$session_id, $file_path]]
|
|
405
|
-
:put session_files { session_id, file_path }`, { session_id: sessionId, file_path: fp });
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
async getSessionFiles(sessionId) {
|
|
409
|
-
const result = await this.db.run("?[file_path] := *session_files{session_id: $session_id, file_path}", { session_id: sessionId });
|
|
410
|
-
return result.rows.map((r) => r[0]);
|
|
411
|
-
}
|
|
412
|
-
async upsertIntent(intent) {
|
|
413
|
-
await this.db.run(`?[intent_id, title, started_at, last_active_at, file_set, file_set_hash, status, confidence, source] <-
|
|
414
|
-
[[$intent_id, $title, $started_at, $last_active_at, $file_set, $file_set_hash, $status, $confidence, $source]]
|
|
415
|
-
:put intents {
|
|
416
|
-
intent_id
|
|
417
|
-
=> title, started_at, last_active_at, file_set, file_set_hash, status, confidence, source
|
|
418
|
-
}`, intent);
|
|
419
|
-
}
|
|
420
|
-
async listIntents(opts = {}) {
|
|
421
|
-
const limit = Math.max(1, Math.min(opts.limit ?? 100, 500));
|
|
422
|
-
const filter = opts.status ? ", status = $status" : "";
|
|
423
|
-
const query = `?[intent_id, title, started_at, last_active_at, file_set, file_set_hash, status, confidence, source] :=
|
|
424
|
-
*intents{intent_id, title, started_at, last_active_at, file_set, file_set_hash, status, confidence, source}${filter}
|
|
425
|
-
:order -last_active_at
|
|
426
|
-
:limit ${limit}`;
|
|
427
|
-
const params = {};
|
|
428
|
-
if (opts.status)
|
|
429
|
-
params.status = opts.status;
|
|
430
|
-
const result = await this.db.run(query, params);
|
|
431
|
-
return result.rows.map((r) => ({
|
|
432
|
-
intent_id: r[0],
|
|
433
|
-
title: r[1],
|
|
434
|
-
started_at: r[2],
|
|
435
|
-
last_active_at: r[3],
|
|
436
|
-
file_set: r[4],
|
|
437
|
-
file_set_hash: r[5],
|
|
438
|
-
status: r[6],
|
|
439
|
-
confidence: r[7],
|
|
440
|
-
source: r[8],
|
|
441
|
-
}));
|
|
442
|
-
}
|
|
443
|
-
async attachSession(intentId, sessionId) {
|
|
444
|
-
await this.db.run(`?[intent_id, session_id] <- [[$intent_id, $session_id]]
|
|
445
|
-
:put intent_sessions { intent_id, session_id }`, { intent_id: intentId, session_id: sessionId });
|
|
446
|
-
}
|
|
447
|
-
async listIntentSessions(intentId) {
|
|
448
|
-
const result = await this.db.run("?[session_id] := *intent_sessions{intent_id: $intent_id, session_id}", { intent_id: intentId });
|
|
449
|
-
return result.rows.map((r) => r[0]);
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Reverse lookup: find the intent (if any) attached to a session.
|
|
453
|
-
*/
|
|
454
|
-
async findIntentForSession(sessionId) {
|
|
455
|
-
const result = await this.db.run("?[intent_id] := *intent_sessions{intent_id, session_id: $session_id}", { session_id: sessionId });
|
|
456
|
-
const row = result.rows[0];
|
|
457
|
-
return row ? row[0] : null;
|
|
458
|
-
}
|
|
459
|
-
// ── ST-5: Derived signals + reinforcement ─────────────────────────────
|
|
460
|
-
async upsertSignal(signal) {
|
|
461
|
-
await this.db.run(`?[signal_id, type, scope, content, confidence, first_seen_at, last_seen_at] <-
|
|
462
|
-
[[$signal_id, $type, $scope, $content, $confidence, $first_seen_at, $last_seen_at]]
|
|
463
|
-
:put derived_signals {
|
|
464
|
-
signal_id => type, scope, content, confidence, first_seen_at, last_seen_at
|
|
465
|
-
}`, signal);
|
|
466
|
-
}
|
|
467
|
-
async getSignal(signalId) {
|
|
468
|
-
const result = await this.db.run(`?[signal_id, type, scope, content, confidence, first_seen_at, last_seen_at] :=
|
|
469
|
-
*derived_signals{signal_id, type, scope, content, confidence, first_seen_at, last_seen_at},
|
|
470
|
-
signal_id = $signal_id`, { signal_id: signalId });
|
|
471
|
-
const row = result.rows[0];
|
|
472
|
-
if (!row)
|
|
473
|
-
return null;
|
|
474
|
-
return rowToSignal(row);
|
|
475
|
-
}
|
|
476
|
-
async listSignals(opts = {}) {
|
|
477
|
-
const limit = Math.max(1, Math.min(opts.limit ?? 50, 500));
|
|
478
|
-
const filters = [];
|
|
479
|
-
if (opts.type)
|
|
480
|
-
filters.push("type = $type");
|
|
481
|
-
if (typeof opts.minConfidence === "number")
|
|
482
|
-
filters.push("confidence >= $min_confidence");
|
|
483
|
-
const filterClause = filters.length > 0 ? `, ${filters.join(", ")}` : "";
|
|
484
|
-
const query = `?[signal_id, type, scope, content, confidence, first_seen_at, last_seen_at] :=
|
|
485
|
-
*derived_signals{signal_id, type, scope, content, confidence, first_seen_at, last_seen_at}${filterClause}
|
|
486
|
-
:order -last_seen_at
|
|
487
|
-
:limit ${limit}`;
|
|
488
|
-
const params = {};
|
|
489
|
-
if (opts.type)
|
|
490
|
-
params.type = opts.type;
|
|
491
|
-
if (typeof opts.minConfidence === "number")
|
|
492
|
-
params.min_confidence = opts.minConfidence;
|
|
493
|
-
const result = await this.db.run(query, params);
|
|
494
|
-
return result.rows.map(rowToSignal);
|
|
495
|
-
}
|
|
496
|
-
async appendReinforcement(signalId, ts, delta, source) {
|
|
497
|
-
await this.db.run(`?[signal_id, ts, delta, source] <-
|
|
498
|
-
[[$signal_id, $ts, $delta, $source]]
|
|
499
|
-
:put signal_reinforcement { signal_id, ts => delta, source }`, {
|
|
500
|
-
signal_id: signalId,
|
|
501
|
-
ts,
|
|
502
|
-
delta,
|
|
503
|
-
source,
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
async getReinforcementHistory(signalId, limit = 10) {
|
|
507
|
-
const result = await this.db.run(`?[signal_id, ts, delta, source] :=
|
|
508
|
-
*signal_reinforcement{signal_id, ts, delta, source},
|
|
509
|
-
signal_id = $signal_id
|
|
510
|
-
:order -ts
|
|
511
|
-
:limit ${Math.max(1, Math.min(limit, 100))}`, { signal_id: signalId });
|
|
512
|
-
return result.rows.map((r) => ({
|
|
513
|
-
signal_id: r[0],
|
|
514
|
-
ts: r[1],
|
|
515
|
-
delta: r[2],
|
|
516
|
-
source: r[3],
|
|
517
|
-
}));
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* Delete signals last seen before `cutoffMs`. Returns the number removed.
|
|
521
|
-
* Cascades: matching reinforcement history rows are also removed.
|
|
522
|
-
*/
|
|
523
|
-
async deleteSignalsBefore(cutoffMs) {
|
|
524
|
-
const stale = await this.db.run(`?[signal_id] := *derived_signals{signal_id, last_seen_at},
|
|
525
|
-
last_seen_at < $cutoff`, { cutoff: cutoffMs });
|
|
526
|
-
const ids = stale.rows.map((r) => r[0]);
|
|
527
|
-
for (const id of ids) {
|
|
528
|
-
await this.db.run(`?[signal_id] := *derived_signals{signal_id}, signal_id = $signal_id
|
|
529
|
-
:rm derived_signals { signal_id }`, { signal_id: id });
|
|
530
|
-
await this.db.run(`?[signal_id, ts] := *signal_reinforcement{signal_id, ts}, signal_id = $signal_id
|
|
531
|
-
:rm signal_reinforcement { signal_id, ts }`, { signal_id: id });
|
|
532
|
-
}
|
|
533
|
-
return ids.length;
|
|
534
|
-
}
|
|
535
|
-
/** Cleanly close the underlying CozoDB handle. */
|
|
536
|
-
close() {
|
|
537
|
-
this.db.close?.();
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
function rowToSignal(row) {
|
|
541
|
-
return {
|
|
542
|
-
signal_id: row[0],
|
|
543
|
-
type: row[1],
|
|
544
|
-
scope: row[2],
|
|
545
|
-
content: row[3],
|
|
546
|
-
confidence: row[4],
|
|
547
|
-
first_seen_at: row[5],
|
|
548
|
-
last_seen_at: row[6],
|
|
549
|
-
};
|
|
550
|
-
}
|
|
551
|
-
function rowToTurn(row) {
|
|
552
|
-
return {
|
|
553
|
-
turn_id: row[0],
|
|
554
|
-
session_id: row[1],
|
|
555
|
-
started_at: row[2],
|
|
556
|
-
ended_at: row[3],
|
|
557
|
-
opened_by: row[4],
|
|
558
|
-
closed_reason: row[5],
|
|
559
|
-
tool_count: row[6],
|
|
560
|
-
file_count: row[7],
|
|
561
|
-
edit_count: row[8],
|
|
562
|
-
title: row[9],
|
|
563
|
-
outcome: row[10],
|
|
564
|
-
};
|
|
565
|
-
}
|
|
566
|
-
function rowToMarker(row) {
|
|
567
|
-
return {
|
|
568
|
-
marker_id: row[0],
|
|
569
|
-
type: row[1],
|
|
570
|
-
text: row[2],
|
|
571
|
-
session_id: row[3],
|
|
572
|
-
turn_id: row[4],
|
|
573
|
-
ts: row[5],
|
|
574
|
-
blocker_ref: row[6],
|
|
575
|
-
file_path: row[7],
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* Build the Datalog WHERE-clause fragment + params for the turn-filter family
|
|
580
|
-
* (listTurns, countTurns). Centralised so listTurns and countTurns share the
|
|
581
|
-
* same semantics and can't drift.
|
|
582
|
-
*/
|
|
583
|
-
function buildTurnFilter(opts) {
|
|
584
|
-
const filters = [];
|
|
585
|
-
const params = {};
|
|
586
|
-
if (opts.sessionId) {
|
|
587
|
-
filters.push("session_id = $session_id");
|
|
588
|
-
params.session_id = opts.sessionId;
|
|
589
|
-
}
|
|
590
|
-
if (typeof opts.fromTs === "number") {
|
|
591
|
-
filters.push("started_at >= $from_ts");
|
|
592
|
-
params.from_ts = opts.fromTs;
|
|
593
|
-
}
|
|
594
|
-
if (typeof opts.toTs === "number") {
|
|
595
|
-
filters.push("started_at <= $to_ts");
|
|
596
|
-
params.to_ts = opts.toTs;
|
|
597
|
-
}
|
|
598
|
-
if (opts.query && opts.query.trim().length > 0) {
|
|
599
|
-
// Case-insensitive substring search via Cozo's `regex_matches`. The pattern
|
|
600
|
-
// is regex-escaped so `?` / `.` / `(` / `[` etc. in the user input don't
|
|
601
|
-
// accidentally turn into wildcards.
|
|
602
|
-
filters.push("regex_matches(title, $q_re)");
|
|
603
|
-
params.q_re = `(?i).*${escapeRegex(opts.query.trim())}.*`;
|
|
604
|
-
}
|
|
605
|
-
return {
|
|
606
|
-
whereClause: filters.length > 0 ? `, ${filters.join(", ")}` : "",
|
|
607
|
-
params,
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
function escapeRegex(s) {
|
|
611
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
612
|
-
}
|
|
613
|
-
/** Floor a timestamp to the start of its bucket (e.g. UTC midnight for 24h). */
|
|
614
|
-
function bucketStart(ts, bucketMs) {
|
|
615
|
-
if (!Number.isFinite(ts) || bucketMs <= 0)
|
|
616
|
-
return ts;
|
|
617
|
-
return Math.floor(ts / bucketMs) * bucketMs;
|
|
618
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bash Tool — execute shell commands.
|
|
3
|
-
* Requires user permission for non-trivial commands. Streams output.
|
|
4
|
-
*/
|
|
5
|
-
import { exec } from "../../utils/exec.js";
|
|
6
|
-
export const bashTool = {
|
|
7
|
-
name: "bash",
|
|
8
|
-
description: "Execute a bash command in the project directory. Returns stdout and stderr. " +
|
|
9
|
-
"Use this for running tests, git commands, build scripts, and other shell operations. " +
|
|
10
|
-
"Commands time out after 2 minutes.",
|
|
11
|
-
inputSchema: {
|
|
12
|
-
type: "object",
|
|
13
|
-
properties: {
|
|
14
|
-
command: {
|
|
15
|
-
type: "string",
|
|
16
|
-
description: "The bash command to execute",
|
|
17
|
-
},
|
|
18
|
-
timeout: {
|
|
19
|
-
type: "number",
|
|
20
|
-
description: "Timeout in milliseconds. Default: 120000 (2 minutes)",
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
required: ["command"],
|
|
24
|
-
},
|
|
25
|
-
isReadOnly: false,
|
|
26
|
-
requiresPermission: true,
|
|
27
|
-
async execute(args, ctx) {
|
|
28
|
-
const command = args.command;
|
|
29
|
-
const timeout = Math.min(args.timeout ?? 120_000, 600_000);
|
|
30
|
-
const result = await exec("bash", ["-c", command], {
|
|
31
|
-
cwd: ctx.cwd,
|
|
32
|
-
timeout,
|
|
33
|
-
});
|
|
34
|
-
if (result.exitCode === 0) {
|
|
35
|
-
return { content: result.stdout || "(no output)" };
|
|
36
|
-
}
|
|
37
|
-
const parts = [];
|
|
38
|
-
if (result.stdout)
|
|
39
|
-
parts.push(result.stdout);
|
|
40
|
-
if (result.stderr)
|
|
41
|
-
parts.push(result.stderr);
|
|
42
|
-
if (parts.length === 0)
|
|
43
|
-
parts.push("Command failed");
|
|
44
|
-
return {
|
|
45
|
-
content: `Exit code: ${result.exitCode}\n${parts.join("\n")}`,
|
|
46
|
-
isError: true,
|
|
47
|
-
};
|
|
48
|
-
},
|
|
49
|
-
};
|