@rkarim08/sia 1.0.0

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.
Files changed (355) hide show
  1. package/.claude-plugin/marketplace.json +35 -0
  2. package/.claude-plugin/plugin.json +27 -0
  3. package/.mcp.json +13 -0
  4. package/CLAUDE.md +226 -0
  5. package/LICENSE +202 -0
  6. package/PLUGIN_README.md +253 -0
  7. package/README.md +1013 -0
  8. package/agents/sia-changelog-writer.md +89 -0
  9. package/agents/sia-code-reviewer.md +86 -0
  10. package/agents/sia-conflict-resolver.md +100 -0
  11. package/agents/sia-convention-enforcer.md +69 -0
  12. package/agents/sia-debug.md +106 -0
  13. package/agents/sia-decision-reviewer.md +101 -0
  14. package/agents/sia-dependency-tracker.md +80 -0
  15. package/agents/sia-explain.md +126 -0
  16. package/agents/sia-feature.md +116 -0
  17. package/agents/sia-knowledge-capture.md +117 -0
  18. package/agents/sia-lead-architecture-advisor.md +93 -0
  19. package/agents/sia-lead-team-health.md +107 -0
  20. package/agents/sia-migration.md +100 -0
  21. package/agents/sia-onboarding.md +115 -0
  22. package/agents/sia-orientation.md +99 -0
  23. package/agents/sia-pm-briefing.md +106 -0
  24. package/agents/sia-pm-risk-advisor.md +82 -0
  25. package/agents/sia-qa-analyst.md +116 -0
  26. package/agents/sia-qa-regression-map.md +94 -0
  27. package/agents/sia-refactor.md +115 -0
  28. package/agents/sia-regression.md +112 -0
  29. package/agents/sia-security-audit.md +125 -0
  30. package/agents/sia-test-advisor.md +91 -0
  31. package/hooks/hooks.json +98 -0
  32. package/migrations/bridge/001_initial.sql +34 -0
  33. package/migrations/episodic/001_initial.sql +35 -0
  34. package/migrations/meta/001_initial.sql +68 -0
  35. package/migrations/semantic/001_initial.sql +292 -0
  36. package/migrations/semantic/002_ontology.sql +89 -0
  37. package/migrations/semantic/003_freshness.sql +63 -0
  38. package/migrations/semantic/004_v5_unified_schema.sql +194 -0
  39. package/migrations/semantic/005_backfill_event_kinds.sql +8 -0
  40. package/migrations/semantic/006_tree_sitter.sql +6 -0
  41. package/migrations/semantic/007_branch_snapshots.sql +22 -0
  42. package/package.json +110 -0
  43. package/scripts/branch-switch.sh +13 -0
  44. package/scripts/build-wasm-grammars.sh +81 -0
  45. package/scripts/post-compact.sh +8 -0
  46. package/scripts/post-tool-use.sh +10 -0
  47. package/scripts/pre-compact.sh +8 -0
  48. package/scripts/session-end.sh +8 -0
  49. package/scripts/session-start.sh +8 -0
  50. package/scripts/start-mcp.ts +45 -0
  51. package/scripts/stop-hook.sh +8 -0
  52. package/scripts/user-prompt-submit.sh +8 -0
  53. package/scripts/viz-server.ts +152 -0
  54. package/skills/sia-brainstorm/SKILL.md +156 -0
  55. package/skills/sia-brainstorm/scripts/frame-template.html +214 -0
  56. package/skills/sia-brainstorm/scripts/helper.js +95 -0
  57. package/skills/sia-brainstorm/scripts/server.cjs +338 -0
  58. package/skills/sia-brainstorm/scripts/start-server.sh +153 -0
  59. package/skills/sia-brainstorm/scripts/stop-server.sh +55 -0
  60. package/skills/sia-brainstorm/spec-document-reviewer-prompt.md +49 -0
  61. package/skills/sia-brainstorm/visual-companion.md +286 -0
  62. package/skills/sia-capture/SKILL.md +64 -0
  63. package/skills/sia-compare/SKILL.md +33 -0
  64. package/skills/sia-conflicts/SKILL.md +38 -0
  65. package/skills/sia-debug-workflow/SKILL.md +120 -0
  66. package/skills/sia-debug-workflow/root-cause-tracing.md +70 -0
  67. package/skills/sia-debug-workflow/scripts/find-polluter.sh +64 -0
  68. package/skills/sia-debug-workflow/temporal-investigation.md +72 -0
  69. package/skills/sia-digest/SKILL.md +23 -0
  70. package/skills/sia-dispatch/SKILL.md +69 -0
  71. package/skills/sia-dispatch/agent-task-template.md +99 -0
  72. package/skills/sia-doctor/SKILL.md +39 -0
  73. package/skills/sia-execute/SKILL.md +70 -0
  74. package/skills/sia-execute-plan/SKILL.md +85 -0
  75. package/skills/sia-export-import/SKILL.md +49 -0
  76. package/skills/sia-export-knowledge/SKILL.md +46 -0
  77. package/skills/sia-finish/SKILL.md +100 -0
  78. package/skills/sia-finish/pr-summary-template.md +54 -0
  79. package/skills/sia-freshness/SKILL.md +38 -0
  80. package/skills/sia-history/SKILL.md +42 -0
  81. package/skills/sia-impact/SKILL.md +70 -0
  82. package/skills/sia-index/SKILL.md +54 -0
  83. package/skills/sia-install/SKILL.md +39 -0
  84. package/skills/sia-lead-compliance/SKILL.md +16 -0
  85. package/skills/sia-lead-drift-report/SKILL.md +16 -0
  86. package/skills/sia-lead-knowledge-map/SKILL.md +16 -0
  87. package/skills/sia-learn/SKILL.md +58 -0
  88. package/skills/sia-plan/SKILL.md +68 -0
  89. package/skills/sia-plan/plan-reviewer-prompt.md +63 -0
  90. package/skills/sia-playbooks/SKILL.md +29 -0
  91. package/skills/sia-playbooks/reference-feature.md +100 -0
  92. package/skills/sia-playbooks/reference-flagging.md +50 -0
  93. package/skills/sia-playbooks/reference-orientation.md +92 -0
  94. package/skills/sia-playbooks/reference-regression.md +115 -0
  95. package/skills/sia-playbooks/reference-review.md +64 -0
  96. package/skills/sia-playbooks/reference-tools.md +239 -0
  97. package/skills/sia-pm-decision-log/SKILL.md +28 -0
  98. package/skills/sia-pm-risk-dashboard/SKILL.md +24 -0
  99. package/skills/sia-pm-sprint-summary/SKILL.md +27 -0
  100. package/skills/sia-prune/SKILL.md +45 -0
  101. package/skills/sia-qa-coverage/SKILL.md +28 -0
  102. package/skills/sia-qa-flaky/SKILL.md +20 -0
  103. package/skills/sia-qa-report/SKILL.md +26 -0
  104. package/skills/sia-reindex/SKILL.md +30 -0
  105. package/skills/sia-review-respond/SKILL.md +88 -0
  106. package/skills/sia-review-respond/pushback-patterns.md +90 -0
  107. package/skills/sia-search/SKILL.md +47 -0
  108. package/skills/sia-setup/SKILL.md +82 -0
  109. package/skills/sia-setup/setup-checklist.md +97 -0
  110. package/skills/sia-stats/SKILL.md +36 -0
  111. package/skills/sia-status/SKILL.md +44 -0
  112. package/skills/sia-sync/SKILL.md +46 -0
  113. package/skills/sia-team/SKILL.md +64 -0
  114. package/skills/sia-test/SKILL.md +92 -0
  115. package/skills/sia-test/testing-anti-patterns.md +104 -0
  116. package/skills/sia-tour/SKILL.md +29 -0
  117. package/skills/sia-upgrade/SKILL.md +43 -0
  118. package/skills/sia-verify/SKILL.md +81 -0
  119. package/skills/sia-visualize/SKILL.md +28 -0
  120. package/skills/sia-visualize-live/SKILL.md +55 -0
  121. package/skills/sia-visualize-live/scripts/graph-template.html +389 -0
  122. package/skills/sia-visualize-live/scripts/start-visualizer.sh +161 -0
  123. package/skills/sia-visualize-live/scripts/stop-visualizer.sh +55 -0
  124. package/skills/sia-visualize-live/scripts/visualizer-server.cjs +264 -0
  125. package/skills/sia-workspace/SKILL.md +57 -0
  126. package/src/agent/claude-md-template-flagging.md +219 -0
  127. package/src/agent/claude-md-template.md +213 -0
  128. package/src/agent/modules/sia-feature.md +100 -0
  129. package/src/agent/modules/sia-flagging.md +50 -0
  130. package/src/agent/modules/sia-orientation.md +92 -0
  131. package/src/agent/modules/sia-regression.md +115 -0
  132. package/src/agent/modules/sia-review.md +64 -0
  133. package/src/agent/modules/sia-tools.md +239 -0
  134. package/src/ast/extractors/c-include.ts +189 -0
  135. package/src/ast/extractors/csharp-project.ts +260 -0
  136. package/src/ast/extractors/prisma-schema.ts +44 -0
  137. package/src/ast/extractors/project-manifest.ts +111 -0
  138. package/src/ast/extractors/sql-schema.ts +67 -0
  139. package/src/ast/extractors/tier-a.ts +423 -0
  140. package/src/ast/extractors/tier-b.ts +289 -0
  141. package/src/ast/extractors/tier-dispatch.ts +247 -0
  142. package/src/ast/index-worker.ts +108 -0
  143. package/src/ast/indexer.ts +484 -0
  144. package/src/ast/languages.ts +408 -0
  145. package/src/ast/pagerank-builder.ts +125 -0
  146. package/src/ast/path-utils.ts +137 -0
  147. package/src/ast/tree-sitter/backends/native.ts +57 -0
  148. package/src/ast/tree-sitter/backends/wasm.ts +39 -0
  149. package/src/ast/tree-sitter/call-walker.ts +44 -0
  150. package/src/ast/tree-sitter/edit-computer.ts +55 -0
  151. package/src/ast/tree-sitter/query-runner.ts +46 -0
  152. package/src/ast/tree-sitter/service.ts +174 -0
  153. package/src/ast/tree-sitter/tree-cache.ts +39 -0
  154. package/src/ast/tree-sitter/types.ts +79 -0
  155. package/src/ast/watcher.ts +322 -0
  156. package/src/capture/chunker.ts +169 -0
  157. package/src/capture/consolidate.ts +127 -0
  158. package/src/capture/edge-inferrer.ts +161 -0
  159. package/src/capture/embedder.ts +166 -0
  160. package/src/capture/embedding-cache.ts +73 -0
  161. package/src/capture/flag-processor.ts +64 -0
  162. package/src/capture/hook.ts +67 -0
  163. package/src/capture/pipeline.ts +450 -0
  164. package/src/capture/prompts/consolidate.ts +25 -0
  165. package/src/capture/prompts/edge-infer.ts +29 -0
  166. package/src/capture/prompts/extract-flagged.ts +36 -0
  167. package/src/capture/prompts/extract.ts +42 -0
  168. package/src/capture/tokenizer.ts +147 -0
  169. package/src/capture/track-a-ast.ts +93 -0
  170. package/src/capture/track-b-llm.ts +149 -0
  171. package/src/capture/types.ts +64 -0
  172. package/src/cli/commands/community.ts +137 -0
  173. package/src/cli/commands/compare.ts +123 -0
  174. package/src/cli/commands/conflicts.ts +41 -0
  175. package/src/cli/commands/digest.ts +197 -0
  176. package/src/cli/commands/disable-flagging.ts +34 -0
  177. package/src/cli/commands/doctor.ts +240 -0
  178. package/src/cli/commands/download-model.ts +161 -0
  179. package/src/cli/commands/enable-flagging.ts +34 -0
  180. package/src/cli/commands/export-knowledge.ts +208 -0
  181. package/src/cli/commands/export.ts +85 -0
  182. package/src/cli/commands/freshness.ts +164 -0
  183. package/src/cli/commands/graph.ts +51 -0
  184. package/src/cli/commands/history.ts +139 -0
  185. package/src/cli/commands/import.ts +335 -0
  186. package/src/cli/commands/install.ts +156 -0
  187. package/src/cli/commands/lead-report.ts +241 -0
  188. package/src/cli/commands/learn.ts +321 -0
  189. package/src/cli/commands/pm-report.ts +413 -0
  190. package/src/cli/commands/prune.ts +75 -0
  191. package/src/cli/commands/qa-report.ts +278 -0
  192. package/src/cli/commands/reindex.ts +104 -0
  193. package/src/cli/commands/rollback.ts +70 -0
  194. package/src/cli/commands/search.ts +103 -0
  195. package/src/cli/commands/server.ts +91 -0
  196. package/src/cli/commands/share.ts +33 -0
  197. package/src/cli/commands/stats.ts +79 -0
  198. package/src/cli/commands/status.ts +176 -0
  199. package/src/cli/commands/sync.ts +96 -0
  200. package/src/cli/commands/team.ts +118 -0
  201. package/src/cli/commands/tour.ts +157 -0
  202. package/src/cli/commands/visualize-live.ts +162 -0
  203. package/src/cli/commands/workspace.ts +117 -0
  204. package/src/cli/index.ts +424 -0
  205. package/src/cli/learn-progress.ts +87 -0
  206. package/src/community/detection-bridge.ts +344 -0
  207. package/src/community/leiden.ts +462 -0
  208. package/src/community/raptor.ts +210 -0
  209. package/src/community/scheduler.ts +74 -0
  210. package/src/community/summarize.ts +115 -0
  211. package/src/decay/archiver.ts +73 -0
  212. package/src/decay/bridge-orphan-cleanup.ts +212 -0
  213. package/src/decay/consolidation-sweep.ts +112 -0
  214. package/src/decay/decay.ts +116 -0
  215. package/src/decay/deep-validator.ts +62 -0
  216. package/src/decay/episodic-promoter.ts +132 -0
  217. package/src/decay/maintenance-scheduler.ts +326 -0
  218. package/src/decay/scheduler.ts +6 -0
  219. package/src/decay/session-sweeper.ts +79 -0
  220. package/src/decay/types.ts +17 -0
  221. package/src/freshness/confidence-decay.ts +122 -0
  222. package/src/freshness/cuckoo-filter.ts +176 -0
  223. package/src/freshness/deep-validation.ts +345 -0
  224. package/src/freshness/dirty-tracker.ts +237 -0
  225. package/src/freshness/file-watcher-layer.ts +119 -0
  226. package/src/freshness/firewall.ts +64 -0
  227. package/src/freshness/git-reconcile-layer.ts +161 -0
  228. package/src/freshness/inverted-index.ts +158 -0
  229. package/src/freshness/stale-read-layer.ts +222 -0
  230. package/src/graph/audit.ts +69 -0
  231. package/src/graph/bridge-db.ts +141 -0
  232. package/src/graph/communities.ts +195 -0
  233. package/src/graph/db-interface.ts +259 -0
  234. package/src/graph/edges.ts +163 -0
  235. package/src/graph/entities.ts +327 -0
  236. package/src/graph/episodic-db.ts +113 -0
  237. package/src/graph/flags.ts +31 -0
  238. package/src/graph/meta-db.ts +200 -0
  239. package/src/graph/semantic-db.ts +101 -0
  240. package/src/graph/session-resume.ts +56 -0
  241. package/src/graph/snapshots.ts +342 -0
  242. package/src/graph/staging.ts +151 -0
  243. package/src/graph/types.ts +128 -0
  244. package/src/hooks/adapters/claude-code.ts +21 -0
  245. package/src/hooks/adapters/cline.ts +43 -0
  246. package/src/hooks/adapters/cursor.ts +65 -0
  247. package/src/hooks/adapters/generic.ts +12 -0
  248. package/src/hooks/agent-detect.ts +34 -0
  249. package/src/hooks/claude-md-directives.ts +32 -0
  250. package/src/hooks/event-router.ts +182 -0
  251. package/src/hooks/extractors/pattern-detector.ts +111 -0
  252. package/src/hooks/handlers/post-compact.ts +30 -0
  253. package/src/hooks/handlers/post-tool-use.ts +403 -0
  254. package/src/hooks/handlers/pre-compact.ts +100 -0
  255. package/src/hooks/handlers/session-end.ts +47 -0
  256. package/src/hooks/handlers/session-start.ts +154 -0
  257. package/src/hooks/handlers/stop.ts +128 -0
  258. package/src/hooks/handlers/user-prompt-submit.ts +68 -0
  259. package/src/hooks/plugin-branch-switch.ts +68 -0
  260. package/src/hooks/plugin-common.ts +47 -0
  261. package/src/hooks/plugin-post-compact.ts +28 -0
  262. package/src/hooks/plugin-post-tool-use.ts +38 -0
  263. package/src/hooks/plugin-pre-compact.ts +37 -0
  264. package/src/hooks/plugin-session-end.ts +37 -0
  265. package/src/hooks/plugin-session-start.ts +75 -0
  266. package/src/hooks/plugin-stop.ts +61 -0
  267. package/src/hooks/plugin-user-prompt-submit.ts +47 -0
  268. package/src/hooks/types.ts +43 -0
  269. package/src/knowledge/discovery.ts +238 -0
  270. package/src/knowledge/external-refs.ts +98 -0
  271. package/src/knowledge/freshness.ts +221 -0
  272. package/src/knowledge/ingest.ts +330 -0
  273. package/src/knowledge/markdown-export.ts +229 -0
  274. package/src/knowledge/markdown-import.ts +359 -0
  275. package/src/knowledge/patterns.ts +74 -0
  276. package/src/knowledge/templates.ts +307 -0
  277. package/src/llm/ai-sdk-adapter.ts +46 -0
  278. package/src/llm/config.ts +88 -0
  279. package/src/llm/cost-tracker.ts +110 -0
  280. package/src/llm/prompts/extraction.ts +55 -0
  281. package/src/llm/prompts/summarization.ts +36 -0
  282. package/src/llm/prompts/validation.ts +37 -0
  283. package/src/llm/provider-registry.ts +68 -0
  284. package/src/llm/reliability.ts +179 -0
  285. package/src/llm/schemas.ts +52 -0
  286. package/src/mcp/freshness-annotator.ts +69 -0
  287. package/src/mcp/server.ts +949 -0
  288. package/src/mcp/tools/sia-ast-query.ts +225 -0
  289. package/src/mcp/tools/sia-at-time.ts +151 -0
  290. package/src/mcp/tools/sia-backlinks.ts +87 -0
  291. package/src/mcp/tools/sia-batch-execute.ts +169 -0
  292. package/src/mcp/tools/sia-by-file.ts +89 -0
  293. package/src/mcp/tools/sia-community.ts +113 -0
  294. package/src/mcp/tools/sia-doctor.ts +73 -0
  295. package/src/mcp/tools/sia-execute-file.ts +122 -0
  296. package/src/mcp/tools/sia-execute.ts +104 -0
  297. package/src/mcp/tools/sia-expand.ts +158 -0
  298. package/src/mcp/tools/sia-fetch-and-index.ts +241 -0
  299. package/src/mcp/tools/sia-flag.ts +65 -0
  300. package/src/mcp/tools/sia-index.ts +111 -0
  301. package/src/mcp/tools/sia-note.ts +134 -0
  302. package/src/mcp/tools/sia-search.ts +105 -0
  303. package/src/mcp/tools/sia-stats.ts +63 -0
  304. package/src/mcp/tools/sia-sync-status.ts +44 -0
  305. package/src/mcp/tools/sia-upgrade.ts +247 -0
  306. package/src/mcp/truncate.ts +231 -0
  307. package/src/native/bridge.ts +167 -0
  308. package/src/native/fallback-ast-diff.ts +144 -0
  309. package/src/native/fallback-graph.ts +325 -0
  310. package/src/ontology/constraints.ts +56 -0
  311. package/src/ontology/errors.ts +8 -0
  312. package/src/ontology/middleware.ts +266 -0
  313. package/src/retrieval/bm25-search.ts +151 -0
  314. package/src/retrieval/context-assembly.ts +76 -0
  315. package/src/retrieval/graph-traversal.ts +168 -0
  316. package/src/retrieval/pagerank.ts +40 -0
  317. package/src/retrieval/query-classifier.ts +106 -0
  318. package/src/retrieval/reranker.ts +156 -0
  319. package/src/retrieval/search.ts +236 -0
  320. package/src/retrieval/throttle.ts +102 -0
  321. package/src/retrieval/vector-search.ts +203 -0
  322. package/src/retrieval/workspace-search.ts +130 -0
  323. package/src/sandbox/context-mode.ts +285 -0
  324. package/src/sandbox/credential-pass.ts +55 -0
  325. package/src/sandbox/executor.ts +235 -0
  326. package/src/security/pattern-detector.ts +127 -0
  327. package/src/security/rule-of-two.ts +50 -0
  328. package/src/security/sanitize.ts +46 -0
  329. package/src/security/semantic-consistency.ts +93 -0
  330. package/src/security/staging-promoter.ts +154 -0
  331. package/src/shared/config.ts +302 -0
  332. package/src/shared/diagnostics.ts +210 -0
  333. package/src/shared/errors.ts +48 -0
  334. package/src/shared/git-utils.ts +143 -0
  335. package/src/shared/llm-client.ts +120 -0
  336. package/src/shared/logger.ts +99 -0
  337. package/src/shared/types.ts +79 -0
  338. package/src/sync/client.ts +43 -0
  339. package/src/sync/conflict.ts +106 -0
  340. package/src/sync/dedup.ts +183 -0
  341. package/src/sync/hlc.ts +117 -0
  342. package/src/sync/keychain.ts +144 -0
  343. package/src/sync/pull.ts +232 -0
  344. package/src/sync/push.ts +131 -0
  345. package/src/types/chokidar.d.ts +23 -0
  346. package/src/visualization/graph-renderer.ts +312 -0
  347. package/src/visualization/subgraph-extract.ts +208 -0
  348. package/src/visualization/views/community-clusters.ts +246 -0
  349. package/src/visualization/views/dependency-map.ts +189 -0
  350. package/src/visualization/views/graph-explorer.ts +364 -0
  351. package/src/visualization/views/timeline.ts +247 -0
  352. package/src/workspace/api-contracts.ts +226 -0
  353. package/src/workspace/cross-repo.ts +61 -0
  354. package/src/workspace/detector.ts +190 -0
  355. package/src/workspace/manifest.ts +141 -0
@@ -0,0 +1,292 @@
1
+ -- graph.db initial schema (ARCHI v4.1 section 2.4)
2
+ -- Full bi-temporal model for entities and edges.
3
+
4
+ -- -----------------------------------------------------------------
5
+ -- ENTITIES
6
+ -- Full bi-temporal model: both t_valid_from and t_valid_until apply
7
+ -- to entities, not just to edges. When a Decision entity is
8
+ -- superseded, it is invalidated (t_valid_until set) rather than
9
+ -- soft-deleted (archived_at set). archived_at is reserved for
10
+ -- low-importance entities that have simply decayed out of relevance.
11
+ -- -----------------------------------------------------------------
12
+ CREATE TABLE entities (
13
+ id TEXT PRIMARY KEY, -- UUID v4
14
+
15
+ -- Classification
16
+ type TEXT NOT NULL,
17
+ -- 'CodeEntity' | 'Concept' | 'Decision' | 'Bug'
18
+ -- 'Solution' | 'Convention' | 'Community'
19
+ name TEXT NOT NULL,
20
+ content TEXT NOT NULL, -- Full description (max ~500 words)
21
+ summary TEXT NOT NULL, -- One sentence (max 20 words)
22
+
23
+ -- Monorepo package scoping
24
+ package_path TEXT, -- e.g. 'packages/frontend'; NULL for standalone
25
+
26
+ -- Tags and file associations
27
+ tags TEXT NOT NULL DEFAULT '[]', -- JSON string[]
28
+ file_paths TEXT NOT NULL DEFAULT '[]', -- JSON string[] (relative paths)
29
+
30
+ -- Trust and confidence
31
+ trust_tier INTEGER NOT NULL DEFAULT 3,
32
+ -- 1=UserDirect(x1.00) 2=CodeAnalysis(x0.90) 3=LLMInferred(x0.70) 4=External(x0.50)
33
+ confidence REAL NOT NULL DEFAULT 0.7,
34
+ base_confidence REAL NOT NULL DEFAULT 0.7,
35
+
36
+ -- Importance (retrieval ranking + decay)
37
+ importance REAL NOT NULL DEFAULT 0.5,
38
+ base_importance REAL NOT NULL DEFAULT 0.5,
39
+ access_count INTEGER NOT NULL DEFAULT 0,
40
+ edge_count INTEGER NOT NULL DEFAULT 0, -- denormalized; maintained by trigger
41
+ last_accessed INTEGER NOT NULL, -- Unix ms
42
+ created_at INTEGER NOT NULL, -- Unix ms
43
+
44
+ -- *** FULL BI-TEMPORAL METADATA ON ENTITIES ***
45
+ t_created INTEGER NOT NULL, -- Unix ms: when Sia recorded this entity
46
+ t_expired INTEGER, -- Unix ms: when Sia invalidated it
47
+ t_valid_from INTEGER, -- Unix ms: when the fact became true in the world
48
+ t_valid_until INTEGER, -- Unix ms: when it stopped being true (NULL = still true)
49
+
50
+ -- Team visibility
51
+ visibility TEXT NOT NULL DEFAULT 'private', -- 'private' | 'team' | 'project'
52
+ created_by TEXT NOT NULL, -- developer_id from sync_config
53
+ workspace_scope TEXT, -- workspace_id when visibility='project'
54
+
55
+ -- Sync metadata
56
+ hlc_created INTEGER,
57
+ hlc_modified INTEGER,
58
+ synced_at INTEGER, -- NULL = not yet pushed to server
59
+
60
+ -- Conflict tracking
61
+ conflict_group_id TEXT,
62
+
63
+ -- Provenance
64
+ source_episode TEXT, -- episodic.episodes.id (cross-db ref, not enforced)
65
+ extraction_method TEXT,
66
+ -- 'tree-sitter' | 'spacy' | 'llm-haiku' | 'user-direct' | 'manifest'
67
+ extraction_model TEXT, -- model version string if LLM-extracted
68
+
69
+ -- Embedding (384-dim from all-MiniLM-L6-v2)
70
+ embedding BLOB,
71
+
72
+ -- Soft delete for low-importance, disconnected, decayed entities
73
+ archived_at INTEGER -- NULL = active
74
+ );
75
+
76
+ -- -----------------------------------------------------------------
77
+ -- FTS5 content table -- kept in sync via triggers
78
+ -- -----------------------------------------------------------------
79
+ CREATE VIRTUAL TABLE entities_fts USING fts5(
80
+ name, content, summary, tags,
81
+ content=entities,
82
+ content_rowid=rowid
83
+ );
84
+
85
+ -- Triggers to keep entities_fts in sync
86
+ CREATE TRIGGER entities_ai AFTER INSERT ON entities BEGIN
87
+ INSERT INTO entities_fts(rowid, name, content, summary, tags)
88
+ VALUES (new.rowid, new.name, new.content, new.summary, new.tags);
89
+ END;
90
+ CREATE TRIGGER entities_ad AFTER DELETE ON entities BEGIN
91
+ INSERT INTO entities_fts(entities_fts, rowid, name, content, summary, tags)
92
+ VALUES ('delete', old.rowid, old.name, old.content, old.summary, old.tags);
93
+ END;
94
+ CREATE TRIGGER entities_au AFTER UPDATE ON entities BEGIN
95
+ INSERT INTO entities_fts(entities_fts, rowid, name, content, summary, tags)
96
+ VALUES ('delete', old.rowid, old.name, old.content, old.summary, old.tags);
97
+ INSERT INTO entities_fts(rowid, name, content, summary, tags)
98
+ VALUES (new.rowid, new.name, new.content, new.summary, new.tags);
99
+ END;
100
+
101
+ -- entities_vss (sqlite-vss) created at runtime when extension is loaded
102
+
103
+ -- -----------------------------------------------------------------
104
+ -- Entity indexes
105
+ -- -----------------------------------------------------------------
106
+ CREATE INDEX idx_entities_type ON entities(type) WHERE archived_at IS NULL AND t_valid_until IS NULL;
107
+ CREATE INDEX idx_entities_package ON entities(package_path) WHERE archived_at IS NULL;
108
+ CREATE INDEX idx_entities_importance ON entities(importance DESC) WHERE archived_at IS NULL AND t_valid_until IS NULL;
109
+ CREATE INDEX idx_entities_trust ON entities(trust_tier, confidence);
110
+ CREATE INDEX idx_entities_visibility ON entities(visibility, synced_at);
111
+ CREATE INDEX idx_entities_accessed ON entities(last_accessed DESC);
112
+ CREATE INDEX idx_entities_temporal ON entities(t_valid_from, t_valid_until);
113
+ CREATE INDEX idx_entities_conflict ON entities(conflict_group_id) WHERE conflict_group_id IS NOT NULL;
114
+
115
+ -- -----------------------------------------------------------------
116
+ -- EDGES (bi-temporal, typed, weighted)
117
+ -- -----------------------------------------------------------------
118
+ CREATE TABLE edges (
119
+ id TEXT PRIMARY KEY,
120
+ from_id TEXT NOT NULL REFERENCES entities(id),
121
+ to_id TEXT NOT NULL REFERENCES entities(id),
122
+ type TEXT NOT NULL,
123
+ -- Structural (from AST):
124
+ -- 'calls' | 'imports' | 'inherits_from' | 'contains' | 'depends_on'
125
+ -- Semantic (from LLM extraction):
126
+ -- 'relates_to' | 'solves' | 'caused_by' | 'supersedes'
127
+ -- 'elaborates' | 'contradicts' | 'used_in'
128
+ -- Community (from Leiden):
129
+ -- 'member_of' | 'summarized_by'
130
+ weight REAL NOT NULL DEFAULT 1.0,
131
+ confidence REAL NOT NULL DEFAULT 0.7,
132
+ trust_tier INTEGER NOT NULL DEFAULT 3,
133
+
134
+ -- Bi-temporal metadata
135
+ t_created INTEGER NOT NULL,
136
+ t_expired INTEGER,
137
+ t_valid_from INTEGER,
138
+ t_valid_until INTEGER, -- NULL = still active
139
+
140
+ -- Sync metadata
141
+ hlc_created INTEGER,
142
+ hlc_modified INTEGER,
143
+
144
+ source_episode TEXT,
145
+ extraction_method TEXT
146
+ );
147
+
148
+ CREATE INDEX idx_edges_from ON edges(from_id) WHERE t_valid_until IS NULL;
149
+ CREATE INDEX idx_edges_to ON edges(to_id) WHERE t_valid_until IS NULL;
150
+ CREATE INDEX idx_edges_type ON edges(type);
151
+ CREATE INDEX idx_edges_temporal ON edges(t_valid_from, t_valid_until);
152
+
153
+ -- -----------------------------------------------------------------
154
+ -- Edge count triggers
155
+ -- -----------------------------------------------------------------
156
+
157
+ -- Insert trigger: increment edge_count on both endpoints when a new active edge is created
158
+ CREATE TRIGGER edges_ai_count AFTER INSERT ON edges
159
+ WHEN new.t_valid_until IS NULL
160
+ BEGIN
161
+ UPDATE entities SET edge_count = edge_count + 1 WHERE id = new.from_id;
162
+ UPDATE entities SET edge_count = edge_count + 1 WHERE id = new.to_id;
163
+ END;
164
+
165
+ -- Invalidation trigger: decrement when edge goes from active -> invalidated
166
+ CREATE TRIGGER edges_au_count_invalidate
167
+ AFTER UPDATE OF t_valid_until ON edges
168
+ WHEN old.t_valid_until IS NULL AND new.t_valid_until IS NOT NULL
169
+ BEGIN
170
+ UPDATE entities SET edge_count = edge_count - 1 WHERE id = new.from_id;
171
+ UPDATE entities SET edge_count = edge_count - 1 WHERE id = new.to_id;
172
+ END;
173
+
174
+ -- Reactivation trigger: increment when edge goes from invalidated -> active
175
+ CREATE TRIGGER edges_au_count_reactivate
176
+ AFTER UPDATE OF t_valid_until ON edges
177
+ WHEN old.t_valid_until IS NOT NULL AND new.t_valid_until IS NULL
178
+ BEGIN
179
+ UPDATE entities SET edge_count = edge_count + 1 WHERE id = new.from_id;
180
+ UPDATE entities SET edge_count = edge_count + 1 WHERE id = new.to_id;
181
+ END;
182
+
183
+ -- -----------------------------------------------------------------
184
+ -- COMMUNITIES AND SUMMARY TREE
185
+ -- -----------------------------------------------------------------
186
+ CREATE TABLE communities (
187
+ id TEXT PRIMARY KEY,
188
+ level INTEGER NOT NULL, -- 0=fine, 1=medium, 2=coarse
189
+ parent_id TEXT REFERENCES communities(id),
190
+ summary TEXT,
191
+ summary_hash TEXT, -- SHA-256 of sorted member entity IDs
192
+ member_count INTEGER DEFAULT 0,
193
+ last_summary_member_count INTEGER DEFAULT 0, -- member_count at time of last summary generation
194
+ package_path TEXT, -- NULL=whole repo; set=monorepo package
195
+ created_at INTEGER NOT NULL,
196
+ updated_at INTEGER NOT NULL
197
+ );
198
+
199
+ CREATE TABLE community_members (
200
+ community_id TEXT NOT NULL REFERENCES communities(id),
201
+ entity_id TEXT NOT NULL REFERENCES entities(id),
202
+ level INTEGER NOT NULL,
203
+ PRIMARY KEY (community_id, entity_id)
204
+ );
205
+
206
+ CREATE TABLE summary_tree (
207
+ id TEXT PRIMARY KEY,
208
+ level INTEGER NOT NULL, -- 0=raw entity, 1=entity summary, 2=module, 3=architectural
209
+ scope_id TEXT NOT NULL, -- entity_id (levels 0-1) | community_id (levels 2-3)
210
+ content TEXT NOT NULL,
211
+ content_hash TEXT NOT NULL,
212
+ token_count INTEGER NOT NULL,
213
+ created_at INTEGER NOT NULL,
214
+ expires_at INTEGER -- NULL = valid; set when source facts change
215
+ );
216
+
217
+ -- -----------------------------------------------------------------
218
+ -- SECURITY STAGING
219
+ -- Physically isolated: no FK relationships to entities or edges.
220
+ -- -----------------------------------------------------------------
221
+ CREATE TABLE memory_staging (
222
+ id TEXT PRIMARY KEY,
223
+ source_episode TEXT,
224
+ proposed_type TEXT NOT NULL,
225
+ proposed_name TEXT NOT NULL,
226
+ proposed_content TEXT NOT NULL,
227
+ proposed_tags TEXT NOT NULL DEFAULT '[]',
228
+ proposed_file_paths TEXT NOT NULL DEFAULT '[]',
229
+ trust_tier INTEGER NOT NULL DEFAULT 4,
230
+ raw_confidence REAL NOT NULL,
231
+ validation_status TEXT NOT NULL DEFAULT 'pending',
232
+ -- 'pending' | 'passed' | 'rejected' | 'quarantined'
233
+ rejection_reason TEXT,
234
+ created_at INTEGER NOT NULL,
235
+ expires_at INTEGER NOT NULL -- created_at + (7 * 86400000)
236
+ );
237
+
238
+ -- -----------------------------------------------------------------
239
+ -- SESSION FLAGS AND AUDIT LOG
240
+ -- -----------------------------------------------------------------
241
+ CREATE TABLE session_flags (
242
+ id TEXT PRIMARY KEY,
243
+ session_id TEXT NOT NULL,
244
+ reason TEXT NOT NULL, -- sanitized, max 100 chars
245
+ transcript_position INTEGER,
246
+ created_at INTEGER NOT NULL,
247
+ consumed INTEGER NOT NULL DEFAULT 0 -- 1 = pipeline processed this flag
248
+ );
249
+ CREATE INDEX idx_flags_session ON session_flags(session_id, consumed);
250
+
251
+ CREATE TABLE audit_log (
252
+ id TEXT PRIMARY KEY,
253
+ ts INTEGER NOT NULL,
254
+ hlc INTEGER,
255
+ operation TEXT NOT NULL,
256
+ -- 'ADD' | 'UPDATE' | 'INVALIDATE' | 'NOOP'
257
+ -- 'STAGE' | 'PROMOTE' | 'QUARANTINE'
258
+ -- 'SYNC_RECV' | 'SYNC_SEND'
259
+ -- 'ARCHIVE' | 'VSS_REFRESH'
260
+ entity_id TEXT,
261
+ edge_id TEXT,
262
+ source_episode TEXT,
263
+ trust_tier INTEGER,
264
+ extraction_method TEXT,
265
+ source_hash TEXT, -- SHA-256 of raw source content
266
+ developer_id TEXT,
267
+ snapshot_id TEXT
268
+ );
269
+
270
+ -- -----------------------------------------------------------------
271
+ -- DEDUPLICATION LOGS
272
+ -- Two separate tables for two separate processes (Issue #21).
273
+ -- -----------------------------------------------------------------
274
+
275
+ -- local_dedup_log: nightly consolidation sweep (intra-developer, local graph only)
276
+ CREATE TABLE local_dedup_log (
277
+ entity_a_id TEXT NOT NULL,
278
+ entity_b_id TEXT NOT NULL,
279
+ decision TEXT NOT NULL, -- 'merged' | 'different' | 'related' | 'pending'
280
+ checked_at INTEGER NOT NULL,
281
+ PRIMARY KEY (entity_a_id, entity_b_id)
282
+ );
283
+
284
+ -- sync_dedup_log: post-sync deduplication (cross-developer; peer_id disambiguates source)
285
+ CREATE TABLE sync_dedup_log (
286
+ entity_a_id TEXT NOT NULL, -- local entity
287
+ entity_b_id TEXT NOT NULL, -- peer entity
288
+ peer_id TEXT NOT NULL, -- which teammate this came from
289
+ decision TEXT NOT NULL, -- 'merged' | 'different' | 'related' | 'pending'
290
+ checked_at INTEGER NOT NULL,
291
+ PRIMARY KEY (entity_a_id, entity_b_id, peer_id)
292
+ );
@@ -0,0 +1,89 @@
1
+ -- Migration: 002_ontology.sql
2
+ -- Ontology Constraint Layer
3
+ -- Adds the edge_constraints metadata table declaring all valid (source_type, edge_type, target_type) triples.
4
+ -- Application-layer middleware uses this table to validate edges before insertion.
5
+
6
+ CREATE TABLE IF NOT EXISTS edge_constraints (
7
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
8
+ source_type TEXT NOT NULL,
9
+ edge_type TEXT NOT NULL,
10
+ target_type TEXT NOT NULL,
11
+ description TEXT,
12
+ cardinality TEXT DEFAULT 'many-to-many',
13
+ required INTEGER DEFAULT 0,
14
+ UNIQUE(source_type, edge_type, target_type)
15
+ );
16
+
17
+ -- Seed the constraints table with all valid triples from the Sia v5 ontology.
18
+
19
+ -- Structural edges
20
+ INSERT OR IGNORE INTO edge_constraints (source_type, edge_type, target_type, description) VALUES
21
+ ('FileNode', 'defines', 'CodeEntity', 'File defines a function/class/module'),
22
+ ('CodeEntity', 'imports', 'CodeEntity', 'Symbol imports another symbol'),
23
+ ('CodeEntity', 'calls', 'CodeEntity', 'Symbol calls another symbol'),
24
+ ('CodeEntity', 'inherits_from', 'CodeEntity', 'Class inherits from parent'),
25
+ ('PackageNode', 'contains', 'FileNode', 'Package contains file'),
26
+ ('PackageNode', 'depends_on', 'PackageNode', 'Package depends on another package'),
27
+ ('Community', 'contains', 'CodeEntity', 'Community contains symbol'),
28
+ ('Community', 'contains', 'FileNode', 'Community contains file');
29
+
30
+ -- Semantic edges
31
+ INSERT OR IGNORE INTO edge_constraints (source_type, edge_type, target_type, description) VALUES
32
+ ('Decision', 'pertains_to', 'CodeEntity', 'Decision concerns a code symbol'),
33
+ ('Decision', 'pertains_to', 'FileNode', 'Decision concerns a file'),
34
+ ('Decision', 'pertains_to', 'PackageNode', 'Decision concerns a package'),
35
+ ('Convention', 'pertains_to', 'CodeEntity', 'Convention governs a code symbol'),
36
+ ('Convention', 'pertains_to', 'FileNode', 'Convention governs a file'),
37
+ ('Convention', 'pertains_to', 'PackageNode', 'Convention governs a package'),
38
+ ('Bug', 'caused_by', 'CodeEntity', 'Bug caused by a code symbol'),
39
+ ('Bug', 'caused_by', 'FileNode', 'Bug caused by code in a file'),
40
+ ('Solution', 'solves', 'Bug', 'Solution resolves a bug'),
41
+ ('Solution', 'pertains_to', 'CodeEntity', 'Solution modifies a code symbol'),
42
+ ('Solution', 'pertains_to', 'FileNode', 'Solution modifies a file'),
43
+ ('Concept', 'pertains_to', 'CodeEntity', 'Concept relates to a symbol'),
44
+ ('Concept', 'pertains_to', 'FileNode', 'Concept relates to a file'),
45
+ ('Concept', 'pertains_to', 'PackageNode', 'Concept relates to a package'),
46
+ ('Concept', 'elaborates', 'Decision', 'Concept elaborates a decision'),
47
+ ('Decision', 'elaborates', 'Convention', 'Decision elaborates a convention');
48
+
49
+ -- Supersession edges (same-type only — enforced at application layer)
50
+ INSERT OR IGNORE INTO edge_constraints (source_type, edge_type, target_type, description) VALUES
51
+ ('Decision', 'supersedes', 'Decision', 'New decision supersedes old'),
52
+ ('Convention', 'supersedes', 'Convention', 'New convention supersedes old'),
53
+ ('Solution', 'supersedes', 'Solution', 'New solution supersedes old'),
54
+ ('Concept', 'supersedes', 'Concept', 'New concept supersedes old');
55
+
56
+ -- Contradiction edges
57
+ INSERT OR IGNORE INTO edge_constraints (source_type, edge_type, target_type, description) VALUES
58
+ ('Decision', 'contradicts', 'Decision', 'Two decisions contradict'),
59
+ ('Convention', 'contradicts', 'Convention', 'Two conventions contradict');
60
+
61
+ -- Generic relationship edges (used by capture pipeline and consolidation)
62
+ INSERT OR IGNORE INTO edge_constraints (source_type, edge_type, target_type, description) VALUES
63
+ ('Concept', 'relates_to', 'Concept', 'Concept relates to concept'),
64
+ ('Concept', 'relates_to', 'CodeEntity', 'Concept relates to code'),
65
+ ('Concept', 'relates_to', 'FileNode', 'Concept relates to file'),
66
+ ('Decision', 'relates_to', 'Decision', 'Decision relates to decision'),
67
+ ('Decision', 'relates_to', 'CodeEntity', 'Decision relates to code'),
68
+ ('Convention', 'relates_to', 'Convention', 'Convention relates to convention'),
69
+ ('Bug', 'relates_to', 'Bug', 'Bug relates to bug'),
70
+ ('Bug', 'relates_to', 'CodeEntity', 'Bug relates to code'),
71
+ ('Solution', 'relates_to', 'CodeEntity', 'Solution relates to code'),
72
+ ('CodeEntity', 'relates_to', 'CodeEntity', 'Code symbol relates to another'),
73
+ ('FileNode', 'relates_to', 'FileNode', 'File relates to file'),
74
+ ('Dependency', 'relates_to', 'CodeEntity', 'Dependency relates to code');
75
+
76
+ -- Documentation edges (Phase 14 additions)
77
+ INSERT OR IGNORE INTO edge_constraints (source_type, edge_type, target_type, description) VALUES
78
+ ('ContentChunk', 'child_of', 'FileNode', 'Chunk belongs to document'),
79
+ ('FileNode', 'references', 'FileNode', 'Document links to another document'),
80
+ ('ContentChunk', 'references', 'CodeEntity', 'Chunk mentions a code symbol'),
81
+ ('ContentChunk', 'references', 'FileNode', 'Chunk mentions a file'),
82
+ ('ContentChunk', 'references', 'Decision', 'Chunk references a decision'),
83
+ ('ContentChunk', 'references', 'Convention', 'Chunk references a convention');
84
+
85
+ -- Community membership edges
86
+ INSERT OR IGNORE INTO edge_constraints (source_type, edge_type, target_type, description) VALUES
87
+ ('CodeEntity', 'member_of', 'Community', 'Symbol belongs to community'),
88
+ ('FileNode', 'member_of', 'Community', 'File belongs to community'),
89
+ ('Community', 'summarized_by', 'ContentChunk', 'Community summarized by chunk');
@@ -0,0 +1,63 @@
1
+ -- Phase 15: Freshness Engine — source_deps inverted index + current_entities shadow table
2
+
3
+ -- Maps each source file to every graph node derived from it.
4
+ -- When file X changes, SELECT node_id FROM source_deps WHERE source_path = X
5
+ -- returns the exact set of nodes that may be stale.
6
+ CREATE TABLE IF NOT EXISTS source_deps (
7
+ source_path TEXT NOT NULL,
8
+ node_id TEXT NOT NULL REFERENCES entities(id),
9
+ dep_type TEXT NOT NULL,
10
+ source_mtime INTEGER NOT NULL,
11
+ PRIMARY KEY (source_path, node_id)
12
+ );
13
+
14
+ CREATE INDEX IF NOT EXISTS idx_source_deps_path ON source_deps(source_path);
15
+ CREATE INDEX IF NOT EXISTS idx_source_deps_node ON source_deps(node_id);
16
+
17
+ -- Shadow table: contains ONLY active, non-archived entities.
18
+ -- Eliminates the temporal predicate from the most common query pattern.
19
+ CREATE TABLE IF NOT EXISTS current_entities AS
20
+ SELECT * FROM entities
21
+ WHERE t_valid_until IS NULL AND archived_at IS NULL;
22
+
23
+ CREATE INDEX IF NOT EXISTS idx_current_type ON current_entities(type);
24
+ CREATE INDEX IF NOT EXISTS idx_current_importance ON current_entities(importance DESC);
25
+
26
+ -- Shadow maintenance triggers
27
+ CREATE TRIGGER IF NOT EXISTS shadow_invalidate
28
+ AFTER UPDATE OF t_valid_until ON entities
29
+ WHEN new.t_valid_until IS NOT NULL AND old.t_valid_until IS NULL
30
+ BEGIN
31
+ DELETE FROM current_entities WHERE id = new.id;
32
+ END;
33
+
34
+ CREATE TRIGGER IF NOT EXISTS shadow_archive
35
+ AFTER UPDATE OF archived_at ON entities
36
+ WHEN new.archived_at IS NOT NULL AND old.archived_at IS NULL
37
+ BEGIN
38
+ DELETE FROM current_entities WHERE id = new.id;
39
+ END;
40
+
41
+ CREATE TRIGGER IF NOT EXISTS shadow_insert
42
+ AFTER INSERT ON entities
43
+ WHEN new.t_valid_until IS NULL AND new.archived_at IS NULL
44
+ BEGIN
45
+ INSERT INTO current_entities SELECT * FROM entities WHERE id = new.id;
46
+ END;
47
+
48
+ CREATE TRIGGER IF NOT EXISTS shadow_reactivate
49
+ AFTER UPDATE OF t_valid_until ON entities
50
+ WHEN new.t_valid_until IS NULL AND old.t_valid_until IS NOT NULL
51
+ BEGIN
52
+ INSERT OR REPLACE INTO current_entities SELECT * FROM entities WHERE id = new.id;
53
+ END;
54
+
55
+ -- Partial indexes for hot-path queries
56
+ CREATE INDEX IF NOT EXISTS idx_entities_type_active ON entities(type)
57
+ WHERE archived_at IS NULL AND t_valid_until IS NULL;
58
+ CREATE INDEX IF NOT EXISTS idx_entities_importance_active ON entities(importance DESC)
59
+ WHERE archived_at IS NULL AND t_valid_until IS NULL;
60
+ CREATE INDEX IF NOT EXISTS idx_edges_from_active ON edges(from_id)
61
+ WHERE t_valid_until IS NULL;
62
+ CREATE INDEX IF NOT EXISTS idx_edges_to_active ON edges(to_id)
63
+ WHERE t_valid_until IS NULL;
@@ -0,0 +1,194 @@
1
+ -- Migration: 004_v5_unified_schema.sql
2
+ -- Rename entities->graph_nodes and edges->graph_edges,
3
+ -- add v5 columns, session tables, and rebuild FTS5 + triggers.
4
+
5
+ -- -----------------------------------------------------------------
6
+ -- Step A: Rename tables
7
+ -- -----------------------------------------------------------------
8
+ ALTER TABLE entities RENAME TO graph_nodes;
9
+ ALTER TABLE edges RENAME TO graph_edges;
10
+
11
+ -- -----------------------------------------------------------------
12
+ -- Step B: Add new v5 columns to graph_nodes
13
+ -- -----------------------------------------------------------------
14
+ ALTER TABLE graph_nodes ADD COLUMN kind TEXT;
15
+ ALTER TABLE graph_nodes ADD COLUMN priority_tier INTEGER DEFAULT 3;
16
+ ALTER TABLE graph_nodes ADD COLUMN session_id TEXT;
17
+ ALTER TABLE graph_nodes ADD COLUMN properties TEXT DEFAULT '{}';
18
+
19
+ -- -----------------------------------------------------------------
20
+ -- Step C: Backfill kind from type
21
+ -- -----------------------------------------------------------------
22
+ UPDATE graph_nodes SET kind = type WHERE kind IS NULL;
23
+
24
+ -- -----------------------------------------------------------------
25
+ -- Step D: Add session_resume table
26
+ -- -----------------------------------------------------------------
27
+ CREATE TABLE IF NOT EXISTS session_resume (
28
+ session_id TEXT PRIMARY KEY,
29
+ subgraph_json TEXT NOT NULL,
30
+ last_prompt TEXT,
31
+ budget_used INTEGER DEFAULT 0,
32
+ created_at INTEGER NOT NULL,
33
+ updated_at INTEGER NOT NULL
34
+ );
35
+
36
+ -- -----------------------------------------------------------------
37
+ -- Step E: Add search_throttle table
38
+ -- -----------------------------------------------------------------
39
+ CREATE TABLE IF NOT EXISTS search_throttle (
40
+ session_id TEXT NOT NULL,
41
+ tool_name TEXT NOT NULL,
42
+ call_count INTEGER NOT NULL DEFAULT 1,
43
+ last_called_at INTEGER NOT NULL,
44
+ PRIMARY KEY (session_id, tool_name)
45
+ );
46
+
47
+ -- -----------------------------------------------------------------
48
+ -- Step F: Add DELETE edge_count trigger
49
+ -- -----------------------------------------------------------------
50
+ CREATE TRIGGER IF NOT EXISTS trg_edge_count_delete
51
+ AFTER DELETE ON graph_edges
52
+ WHEN OLD.t_valid_until IS NULL
53
+ BEGIN
54
+ UPDATE graph_nodes SET edge_count = MAX(0, edge_count - 1) WHERE id = OLD.from_id;
55
+ UPDATE graph_nodes SET edge_count = MAX(0, edge_count - 1) WHERE id = OLD.to_id;
56
+ END;
57
+
58
+ -- -----------------------------------------------------------------
59
+ -- Step G: Recreate FTS5 for graph_nodes
60
+ -- Drop old FTS5 triggers first (they reference 'entities')
61
+ -- -----------------------------------------------------------------
62
+ DROP TRIGGER IF EXISTS entities_ai;
63
+ DROP TRIGGER IF EXISTS entities_ad;
64
+ DROP TRIGGER IF EXISTS entities_au;
65
+
66
+ -- Drop old FTS5 virtual table
67
+ DROP TABLE IF EXISTS entities_fts;
68
+
69
+ -- Create new FTS5 for graph_nodes
70
+ CREATE VIRTUAL TABLE graph_nodes_fts USING fts5(
71
+ name, content, summary, tags,
72
+ content='graph_nodes',
73
+ content_rowid='rowid'
74
+ );
75
+
76
+ -- Rebuild FTS from existing data
77
+ INSERT INTO graph_nodes_fts(graph_nodes_fts) VALUES ('rebuild');
78
+
79
+ -- New FTS sync triggers
80
+ CREATE TRIGGER graph_nodes_fts_ai AFTER INSERT ON graph_nodes BEGIN
81
+ INSERT INTO graph_nodes_fts(rowid, name, content, summary, tags)
82
+ VALUES (NEW.rowid, NEW.name, NEW.content, NEW.summary, NEW.tags);
83
+ END;
84
+
85
+ CREATE TRIGGER graph_nodes_fts_ad AFTER DELETE ON graph_nodes BEGIN
86
+ INSERT INTO graph_nodes_fts(graph_nodes_fts, rowid, name, content, summary, tags)
87
+ VALUES ('delete', OLD.rowid, OLD.name, OLD.content, OLD.summary, OLD.tags);
88
+ END;
89
+
90
+ CREATE TRIGGER graph_nodes_fts_au AFTER UPDATE ON graph_nodes BEGIN
91
+ INSERT INTO graph_nodes_fts(graph_nodes_fts, rowid, name, content, summary, tags)
92
+ VALUES ('delete', OLD.rowid, OLD.name, OLD.content, OLD.summary, OLD.tags);
93
+ INSERT INTO graph_nodes_fts(rowid, name, content, summary, tags)
94
+ VALUES (NEW.rowid, NEW.name, NEW.content, NEW.summary, NEW.tags);
95
+ END;
96
+
97
+ -- -----------------------------------------------------------------
98
+ -- Step H: Recreate edge_count triggers for graph_nodes/graph_edges
99
+ -- Drop old triggers that reference 'entities'/'edges'
100
+ -- -----------------------------------------------------------------
101
+ DROP TRIGGER IF EXISTS edges_ai_count;
102
+ DROP TRIGGER IF EXISTS edges_au_count_invalidate;
103
+ DROP TRIGGER IF EXISTS edges_au_count_reactivate;
104
+ DROP TRIGGER IF EXISTS trg_edge_count_insert;
105
+ DROP TRIGGER IF EXISTS trg_edge_count_invalidate;
106
+ DROP TRIGGER IF EXISTS trg_edge_count_reactivate;
107
+
108
+ CREATE TRIGGER trg_edge_count_insert AFTER INSERT ON graph_edges
109
+ WHEN NEW.t_valid_until IS NULL
110
+ BEGIN
111
+ UPDATE graph_nodes SET edge_count = edge_count + 1 WHERE id = NEW.from_id;
112
+ UPDATE graph_nodes SET edge_count = edge_count + 1 WHERE id = NEW.to_id;
113
+ END;
114
+
115
+ CREATE TRIGGER trg_edge_count_invalidate AFTER UPDATE OF t_valid_until ON graph_edges
116
+ WHEN OLD.t_valid_until IS NULL AND NEW.t_valid_until IS NOT NULL
117
+ BEGIN
118
+ UPDATE graph_nodes SET edge_count = MAX(0, edge_count - 1) WHERE id = NEW.from_id;
119
+ UPDATE graph_nodes SET edge_count = MAX(0, edge_count - 1) WHERE id = NEW.to_id;
120
+ END;
121
+
122
+ CREATE TRIGGER trg_edge_count_reactivate AFTER UPDATE OF t_valid_until ON graph_edges
123
+ WHEN OLD.t_valid_until IS NOT NULL AND NEW.t_valid_until IS NULL
124
+ BEGIN
125
+ UPDATE graph_nodes SET edge_count = edge_count + 1 WHERE id = NEW.from_id;
126
+ UPDATE graph_nodes SET edge_count = edge_count + 1 WHERE id = NEW.to_id;
127
+ END;
128
+
129
+ -- -----------------------------------------------------------------
130
+ -- Step I: Drop and recreate triggers from 003_freshness.sql that
131
+ -- reference the old 'entities' table name.
132
+ -- Also fix the source_deps FK reference and current_entities shadow.
133
+ -- -----------------------------------------------------------------
134
+
135
+ -- Drop old shadow maintenance triggers (they reference 'entities')
136
+ DROP TRIGGER IF EXISTS shadow_invalidate;
137
+ DROP TRIGGER IF EXISTS shadow_archive;
138
+ DROP TRIGGER IF EXISTS shadow_insert;
139
+ DROP TRIGGER IF EXISTS shadow_reactivate;
140
+
141
+ -- Drop old current_entities shadow table (was built from entities)
142
+ DROP TABLE IF EXISTS current_entities;
143
+
144
+ -- Drop old source_deps (FK referenced entities(id), now graph_nodes)
145
+ DROP TABLE IF EXISTS source_deps;
146
+
147
+ -- Recreate source_deps referencing graph_nodes
148
+ CREATE TABLE IF NOT EXISTS source_deps (
149
+ source_path TEXT NOT NULL,
150
+ node_id TEXT NOT NULL REFERENCES graph_nodes(id),
151
+ dep_type TEXT NOT NULL,
152
+ source_mtime INTEGER NOT NULL,
153
+ PRIMARY KEY (source_path, node_id)
154
+ );
155
+
156
+ CREATE INDEX IF NOT EXISTS idx_source_deps_path ON source_deps(source_path);
157
+ CREATE INDEX IF NOT EXISTS idx_source_deps_node ON source_deps(node_id);
158
+
159
+ -- Recreate current_entities shadow as current_nodes from graph_nodes
160
+ CREATE TABLE IF NOT EXISTS current_nodes AS
161
+ SELECT * FROM graph_nodes
162
+ WHERE t_valid_until IS NULL AND archived_at IS NULL;
163
+
164
+ CREATE INDEX IF NOT EXISTS idx_current_type ON current_nodes(type);
165
+ CREATE INDEX IF NOT EXISTS idx_current_importance ON current_nodes(importance DESC);
166
+
167
+ -- Recreate shadow maintenance triggers referencing graph_nodes / current_nodes
168
+ CREATE TRIGGER IF NOT EXISTS shadow_invalidate
169
+ AFTER UPDATE OF t_valid_until ON graph_nodes
170
+ WHEN NEW.t_valid_until IS NOT NULL AND OLD.t_valid_until IS NULL
171
+ BEGIN
172
+ DELETE FROM current_nodes WHERE id = NEW.id;
173
+ END;
174
+
175
+ CREATE TRIGGER IF NOT EXISTS shadow_archive
176
+ AFTER UPDATE OF archived_at ON graph_nodes
177
+ WHEN NEW.archived_at IS NOT NULL AND OLD.archived_at IS NULL
178
+ BEGIN
179
+ DELETE FROM current_nodes WHERE id = NEW.id;
180
+ END;
181
+
182
+ CREATE TRIGGER IF NOT EXISTS shadow_insert
183
+ AFTER INSERT ON graph_nodes
184
+ WHEN NEW.t_valid_until IS NULL AND NEW.archived_at IS NULL
185
+ BEGIN
186
+ INSERT INTO current_nodes SELECT * FROM graph_nodes WHERE id = NEW.id;
187
+ END;
188
+
189
+ CREATE TRIGGER IF NOT EXISTS shadow_reactivate
190
+ AFTER UPDATE OF t_valid_until ON graph_nodes
191
+ WHEN NEW.t_valid_until IS NULL AND OLD.t_valid_until IS NOT NULL
192
+ BEGIN
193
+ INSERT OR REPLACE INTO current_nodes SELECT * FROM graph_nodes WHERE id = NEW.id;
194
+ END;
@@ -0,0 +1,8 @@
1
+ -- Migration 005: Backfill event node kinds
2
+ -- Reclassifies existing CodeEntity nodes whose names follow event prefixes
3
+ -- into purpose-specific kind values for better semantic retrieval.
4
+
5
+ UPDATE graph_nodes SET kind = 'EditEvent' WHERE kind = 'CodeEntity' AND name LIKE 'Edit: %';
6
+ UPDATE graph_nodes SET kind = 'ExecutionEvent' WHERE kind = 'CodeEntity' AND name LIKE 'Bash: %';
7
+ UPDATE graph_nodes SET kind = 'GitEvent' WHERE kind = 'CodeEntity' AND name LIKE 'Git: %';
8
+ UPDATE graph_nodes SET kind = 'ErrorEvent' WHERE kind = 'CodeEntity' AND name LIKE 'Error: %';
@@ -0,0 +1,6 @@
1
+ -- Migration 006: tree-sitter extraction backend tracking
2
+ -- Note: entities was renamed to graph_nodes in migration 004.
3
+ -- current_nodes is a shadow table maintained by triggers (SELECT * FROM graph_nodes),
4
+ -- so it must also get the new column to keep column counts in sync.
5
+ ALTER TABLE graph_nodes ADD COLUMN extraction_backend TEXT;
6
+ ALTER TABLE current_nodes ADD COLUMN extraction_backend TEXT;
@@ -0,0 +1,22 @@
1
+ -- Migration 007: branch-keyed snapshots for fast branch switching
2
+ --
3
+ -- Stores graph snapshots per branch so that switching back to a
4
+ -- previously-analyzed branch restores its state without a full rebuild.
5
+ -- Each branch gets exactly one snapshot (UPSERT on branch_name).
6
+
7
+ CREATE TABLE IF NOT EXISTS branch_snapshots (
8
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
9
+ branch_name TEXT NOT NULL UNIQUE,
10
+ commit_hash TEXT NOT NULL,
11
+ node_count INTEGER NOT NULL DEFAULT 0,
12
+ edge_count INTEGER NOT NULL DEFAULT 0,
13
+ snapshot_data TEXT NOT NULL,
14
+ created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000),
15
+ updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
16
+ );
17
+
18
+ CREATE INDEX IF NOT EXISTS idx_branch_snapshots_branch
19
+ ON branch_snapshots(branch_name);
20
+
21
+ CREATE INDEX IF NOT EXISTS idx_branch_snapshots_updated
22
+ ON branch_snapshots(updated_at);