wendkeep 0.73.0 → 0.75.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 (38) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.en.md +13 -9
  3. package/README.md +13 -9
  4. package/docs/en/commands/memory.md +16 -1
  5. package/docs/en/commands/observer.md +29 -11
  6. package/docs/en/commands/operating-profiles.md +1 -1
  7. package/docs/pt-BR/commands/memory.md +16 -1
  8. package/docs/pt-BR/commands/observer.md +28 -11
  9. package/docs/pt-BR/commands/operating-profiles.md +1 -1
  10. package/hooks/brain-core.mjs +2 -0
  11. package/hooks/brain-recall.mjs +5 -1
  12. package/hooks/evidence-context.mjs +41 -0
  13. package/hooks/evidence-recall.mjs +1 -0
  14. package/hooks/memory-scope.mjs +1 -0
  15. package/package.json +2 -2
  16. package/packages/cli/src/index.mjs +2 -2
  17. package/packages/integrations/src/host-hooks.mjs +1 -0
  18. package/packages/vault/src/evidence-recall.mjs +343 -0
  19. package/packages/vault/src/index.mjs +2 -0
  20. package/packages/vault/src/memory-handoff.mjs +58 -3
  21. package/packages/vault/src/memory-schema.mjs +12 -2
  22. package/packages/vault/src/memory-scope.mjs +119 -0
  23. package/packages/vault/src/memory-store.mjs +86 -24
  24. package/schema/observer/004-evidence-recall.sql +25 -0
  25. package/schema/observer/005-project-scoped-identities.sql +217 -0
  26. package/src/change.mjs +41 -1
  27. package/src/doctor.mjs +5 -0
  28. package/src/init.mjs +2 -2
  29. package/src/memory.mjs +95 -2
  30. package/src/note.mjs +8 -1
  31. package/src/observer-publish.mjs +9 -34
  32. package/src/observer-server.mjs +38 -63
  33. package/src/observer-sql-migrate.mjs +1 -1
  34. package/src/observer-sql-publish.mjs +372 -12
  35. package/src/observer-sql-store.mjs +248 -32
  36. package/src/observer-store.mjs +15 -3
  37. package/src/observer.mjs +104 -14
  38. package/src/taxonomy.mjs +4 -0
@@ -0,0 +1,119 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ export const MEMORY_SCOPE_TYPES = Object.freeze([
4
+ 'project',
5
+ 'work_session',
6
+ 'change',
7
+ 'branch',
8
+ 'worktree',
9
+ ]);
10
+
11
+ const SCOPE_TYPES = new Set(MEMORY_SCOPE_TYPES);
12
+ const REGISTER_PATTERNS = [
13
+ /^git\.local-head$/,
14
+ /^handoff\.latest$/,
15
+ /^quality\.latest-(?:sensors|verdict)$/,
16
+ /^change\.[A-Za-z0-9._-]+\.status$/,
17
+ ];
18
+
19
+ function clean(value) {
20
+ return String(value ?? '').trim().replace(/[\r\n\t]+/g, ' ').slice(0, 240);
21
+ }
22
+
23
+ function digest(value) {
24
+ return createHash('sha256').update(clean(value)).digest('hex').slice(0, 16);
25
+ }
26
+
27
+ export function normalizeMemoryScope(scope, { projectId = '' } = {}) {
28
+ if (!scope || typeof scope !== 'object' || Array.isArray(scope)) return null;
29
+ const type = clean(scope.type);
30
+ const id = clean(scope.id);
31
+ if (!SCOPE_TYPES.has(type) || !id) return null;
32
+ if (type === 'project' && projectId && id !== projectId) return null;
33
+ return { type, id };
34
+ }
35
+
36
+ function changeSlug(memoryKey, context) {
37
+ const fromKey = String(memoryKey || '').match(/^change\.([A-Za-z0-9._-]+)\.status$/)?.[1];
38
+ return clean(fromKey || context.changeSlug || context.change_slug);
39
+ }
40
+
41
+ function workSession(context) {
42
+ return clean(
43
+ context.workSessionId || context.work_session_id
44
+ || context.canonicalSessionId || context.canonical_session_id
45
+ || context.sessionId || context.session_id,
46
+ );
47
+ }
48
+
49
+ /** Deterministic scope policy for operational keys. It never uses an absolute local path. */
50
+ export function scopeForMemoryKey(memoryKey, context = {}) {
51
+ const key = clean(memoryKey);
52
+ const projectId = clean(context.projectId || context.project_id) || 'unknown-project';
53
+ if (key === 'handoff.latest') {
54
+ return { type: 'work_session', id: workSession(context) || `legacy:${projectId}` };
55
+ }
56
+ if (key === 'git.local-head') {
57
+ const branch = clean(context.branch || context.branchName || context.branch_name);
58
+ const worktree = clean(context.worktreeId || context.worktree_id);
59
+ const repository = clean(context.repositoryId || context.repository_id);
60
+ if (branch) {
61
+ return {
62
+ type: 'branch',
63
+ id: [repository && `repo:${repository}`, worktree && `worktree:${worktree}`, `branch:${branch}`]
64
+ .filter(Boolean).join('|'),
65
+ };
66
+ }
67
+ const lineage = workSession(context) || clean(context.activation_id) || clean(context.event_id);
68
+ return { type: 'branch', id: `legacy:${projectId}:${digest(lineage || key)}` };
69
+ }
70
+ if (/^quality\.latest-(?:sensors|verdict)$/.test(key)) {
71
+ const slug = changeSlug(key, context) || `legacy:${digest(workSession(context) || key)}`;
72
+ const proof = clean(context.tasksHash || context.tasks_hash || context.specHash || context.spec_hash);
73
+ return { type: 'change', id: proof ? `${slug}|proof:${proof}` : slug };
74
+ }
75
+ if (/^change\.[A-Za-z0-9._-]+\.status$/.test(key)) {
76
+ return { type: 'change', id: changeSlug(key, context) };
77
+ }
78
+ if (/^(?:decision|adr)\b/.test(key)) {
79
+ const slug = changeSlug(key, context);
80
+ return slug ? { type: 'change', id: slug } : { type: 'project', id: projectId };
81
+ }
82
+ if (/^(?:constraint|restriction)\b/.test(key)) {
83
+ const session = workSession(context);
84
+ return session ? { type: 'work_session', id: session } : { type: 'project', id: projectId };
85
+ }
86
+ return { type: 'project', id: projectId };
87
+ }
88
+
89
+ export function effectiveMemoryScope(event = {}) {
90
+ // Ledger rows written before scoped registers existed remain project-scoped until an
91
+ // explicit append-only rescope migration supersedes them. This preserves historic replay.
92
+ return normalizeMemoryScope(event.scope, { projectId: event.project_id })
93
+ || { type: 'project', id: clean(event.project_id) || 'unknown-project' };
94
+ }
95
+
96
+ export function memoryScopeKey(scope) {
97
+ const normalized = normalizeMemoryScope(scope);
98
+ return normalized ? `${normalized.type}:${normalized.id}` : 'project:unknown-project';
99
+ }
100
+
101
+ export function sameMemoryScope(left, right) {
102
+ return memoryScopeKey(effectiveMemoryScope(left)) === memoryScopeKey(effectiveMemoryScope(right));
103
+ }
104
+
105
+ /** Project-scoped keys retain their historic public name; narrower registers are qualified. */
106
+ export function memoryRecordKey(event) {
107
+ const scope = effectiveMemoryScope(event);
108
+ return scope.type === 'project'
109
+ ? String(event.memory_key)
110
+ : `${event.memory_key}@${memoryScopeKey(scope)}`;
111
+ }
112
+
113
+ export function isRegisterMemoryKey(memoryKey) {
114
+ return REGISTER_PATTERNS.some((pattern) => pattern.test(String(memoryKey || '')));
115
+ }
116
+
117
+ export function isHumanCuratedMemoryKey(memoryKey) {
118
+ return /^(?:decision|adr|constraint|restriction|block|blocker)\b/.test(String(memoryKey || ''));
119
+ }
@@ -14,6 +14,12 @@ import {
14
14
  sanitizeMemoryText,
15
15
  validateMemoryEvent,
16
16
  } from './memory-schema.mjs';
17
+ import {
18
+ effectiveMemoryScope,
19
+ isRegisterMemoryKey,
20
+ memoryRecordKey,
21
+ sameMemoryScope,
22
+ } from './memory-scope.mjs';
17
23
  import {
18
24
  assertVaultPathSafe, assertVaultPathsSafe, mkdirVaultPath, unlinkVaultFile,
19
25
  VAULT_LOCK_BUSY, withVaultPathLock, writeVaultFileAtomic,
@@ -376,6 +382,7 @@ function sameCompleteCausalLineage(left, right) {
376
382
  }
377
383
 
378
384
  function comparable(left, right) {
385
+ if (left?.project_id !== right?.project_id || !sameMemoryScope(left, right)) return false;
379
386
  if (sameCausalActivation(left, right)) return true;
380
387
  const leftSupersedes = left.supersedes_event_id || left.supersedes;
381
388
  const rightSupersedes = right.supersedes_event_id || right.supersedes;
@@ -387,7 +394,7 @@ function comparable(left, right) {
387
394
  function conflictGroupKey(event) {
388
395
  if (event.operation !== 'replace') return null;
389
396
  if (!Number.isInteger(event.base_revision) || typeof event.base_value_hash !== 'string') return null;
390
- return `${event.memory_key}\u0000${event.base_revision}\u0000${event.base_value_hash}`;
397
+ return `${memoryRecordKey(event)}\u0000${event.base_revision}\u0000${event.base_value_hash}`;
391
398
  }
392
399
 
393
400
  function candidateId(reason, memoryKey, eventIds) {
@@ -421,6 +428,7 @@ function blockedByCoreCandidate(event, coreValue) {
421
428
  reason: 'blocked_by_core',
422
429
  status: 'blocked_by_core',
423
430
  memory_key: event.memory_key,
431
+ ...(event.scope ? { scope: effectiveMemoryScope(event), record_key: memoryRecordKey(event) } : {}),
424
432
  event_ids: [event.event_id],
425
433
  proposed_value: event.value,
426
434
  core_value: coreValue,
@@ -442,6 +450,10 @@ function conflictCandidate(memoryKey, events, currentEvent = null) {
442
450
  candidate_id: candidateId('conflict', memoryKey, eventIds),
443
451
  reason: 'conflict',
444
452
  memory_key: memoryKey,
453
+ ...((ordered[0] || currentEvent)?.scope ? {
454
+ scope: effectiveMemoryScope(ordered[0] || currentEvent || {}),
455
+ record_key: memoryRecordKey(ordered[0] || currentEvent || { memory_key: memoryKey }),
456
+ } : {}),
445
457
  event_ids: eventIds,
446
458
  values: eventIds.map((id) => byId.get(id).value),
447
459
  base_revision: ordered[0]?.base_revision ?? currentEvent?.revision ?? 0,
@@ -459,6 +471,7 @@ function conflictReviewEvent(candidate, candidateCount = 1) {
459
471
  event_id: `mem-review-${candidate.candidate_id}`,
460
472
  project_id: source.project_id || '',
461
473
  memory_key: candidate.memory_key,
474
+ scope: candidate.scope,
462
475
  operation: 'assert',
463
476
  value: `[revisão pendente: ${memoryKey}; candidates: ${candidateCount}; events: ${eventCount}]`,
464
477
  authority: 'candidate',
@@ -508,7 +521,9 @@ function isCausallyOlder(event, current) {
508
521
  if (sameCausalActivation(event, current)) {
509
522
  return Number(event.turn_sequence) < Number(current.turn_sequence);
510
523
  }
511
- if (Number.isInteger(event.activation_epoch) && Number.isInteger(current.activation_epoch)
524
+ if (event.canonical_session_id && event.canonical_session_id === current.canonical_session_id
525
+ && sameMemoryScope(event, current)
526
+ && Number.isInteger(event.activation_epoch) && Number.isInteger(current.activation_epoch)
512
527
  && event.activation_epoch !== current.activation_epoch) {
513
528
  return event.activation_epoch < current.activation_epoch;
514
529
  }
@@ -518,6 +533,30 @@ function isCausallyOlder(event, current) {
518
533
  && eventEffective < currentEffective;
519
534
  }
520
535
 
536
+ const AUTHORITY_RANK = Object.freeze({ candidate: 0, reported: 1, verified: 2 });
537
+
538
+ function sameRegisterLineage(left, right) {
539
+ return Boolean(left?.canonical_session_id)
540
+ && left.canonical_session_id === right?.canonical_session_id
541
+ && left.project_id === right?.project_id
542
+ && sameMemoryScope(left, right);
543
+ }
544
+
545
+ /** Positive means `incoming` is a safe successor; null means human comparison is required. */
546
+ function registerPrecedence(incoming, current) {
547
+ if (!incoming?.scope || !current?.scope
548
+ || !isRegisterMemoryKey(incoming?.memory_key) || !sameRegisterLineage(incoming, current)) return null;
549
+ const epoch = Number(incoming.activation_epoch ?? -1) - Number(current.activation_epoch ?? -1);
550
+ if (epoch) return epoch;
551
+ const turn = Number(incoming.turn_sequence ?? -1) - Number(current.turn_sequence ?? -1);
552
+ if (turn) return turn;
553
+ const authority = (AUTHORITY_RANK[incoming.authority] ?? -1) - (AUTHORITY_RANK[current.authority] ?? -1);
554
+ if (authority) return authority;
555
+ const observed = String(incoming.observed_at || '').localeCompare(String(current.observed_at || ''));
556
+ if (observed) return observed;
557
+ return String(incoming.event_id || '').localeCompare(String(current.event_id || ''));
558
+ }
559
+
521
560
  /**
522
561
  * Pure deterministic reducer. It pre-detects incomparable scalar siblings so replay order
523
562
  * never turns one concurrent writer into an accidental winner.
@@ -537,7 +576,21 @@ export function reduceMemoryEvents(inputEvents = [], {
537
576
  }
538
577
  if (!existing) unique.set(event.event_id, event);
539
578
  }
540
- const events = [...unique.values()].sort(eventOrder);
579
+ const rescopeTargets = new Set(
580
+ [...unique.values()].flatMap((item) => [
581
+ item.rescopes_event_id,
582
+ ...(Array.isArray(item.rescopes_event_ids) ? item.rescopes_event_ids : []),
583
+ ]).filter(Boolean),
584
+ );
585
+ const projectIds = new Set([...unique.values()].map((item) => item.project_id).filter(Boolean));
586
+ if (projectIds.size > 1) {
587
+ const error = new TypeError('Memory reducer cannot compare events from different projects.');
588
+ error.code = 'MEMORY_PROJECT_MIXED';
589
+ throw error;
590
+ }
591
+ const events = [...unique.values()]
592
+ .filter((item) => !rescopeTargets.has(item.event_id))
593
+ .sort(eventOrder);
541
594
  const candidateDecisions = new Map();
542
595
  for (const item of events) {
543
596
  const decision = item.candidate_decision;
@@ -604,7 +657,8 @@ export function reduceMemoryEvents(inputEvents = [], {
604
657
  continue;
605
658
  }
606
659
 
607
- const current = records.get(item.memory_key);
660
+ const recordKey = memoryRecordKey(item);
661
+ const current = records.get(recordKey);
608
662
  const currentSource = current?.source;
609
663
  if (isCausallyOlder(item, currentSource)) {
610
664
  superseded.push({ event_id: item.event_id, by_event_id: currentSource.event_id });
@@ -613,10 +667,12 @@ export function reduceMemoryEvents(inputEvents = [], {
613
667
 
614
668
  if (item.operation === 'assert') {
615
669
  if (current && hashMemoryValue(current.value) !== hashMemoryValue(item.value)) {
616
- if (sameCausalActivation(item, currentSource)
617
- && Number(item.turn_sequence) > Number(currentSource.turn_sequence)) {
618
- records.set(item.memory_key, { value: item.value, revision: current.revision + 1, source: item });
619
- tombstones.delete(item.memory_key);
670
+ const precedence = registerPrecedence(item, currentSource);
671
+ if ((sameCausalActivation(item, currentSource)
672
+ && Number(item.turn_sequence) > Number(currentSource.turn_sequence))
673
+ || precedence > 0) {
674
+ records.set(recordKey, { value: item.value, revision: current.revision + 1, source: item });
675
+ tombstones.delete(recordKey);
620
676
  superseded.push({ event_id: currentSource.event_id, by_event_id: item.event_id });
621
677
  revision += 1;
622
678
  appliedEventIds.push(item.event_id);
@@ -628,8 +684,8 @@ export function reduceMemoryEvents(inputEvents = [], {
628
684
  continue;
629
685
  }
630
686
  if (!current) {
631
- records.set(item.memory_key, { value: item.value, revision: 1, source: item });
632
- tombstones.delete(item.memory_key);
687
+ records.set(recordKey, { value: item.value, revision: 1, source: item });
688
+ tombstones.delete(recordKey);
633
689
  revision += 1;
634
690
  }
635
691
  appliedEventIds.push(item.event_id);
@@ -642,8 +698,8 @@ export function reduceMemoryEvents(inputEvents = [], {
642
698
  const byHash = new Map(oldValues.map((value) => [hashMemoryValue(value), value]));
643
699
  additions.forEach((value) => byHash.set(hashMemoryValue(value), value));
644
700
  const value = [...byHash.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([, entry]) => entry);
645
- records.set(item.memory_key, { value, revision: (current?.revision || 0) + 1, source: item });
646
- tombstones.delete(item.memory_key);
701
+ records.set(recordKey, { value, revision: (current?.revision || 0) + 1, source: item });
702
+ tombstones.delete(recordKey);
647
703
  revision += 1;
648
704
  appliedEventIds.push(item.event_id);
649
705
  continue;
@@ -664,13 +720,13 @@ export function reduceMemoryEvents(inputEvents = [], {
664
720
  if (item.value !== null && item.value !== undefined && Array.isArray(current.value)) {
665
721
  const removalHash = hashMemoryValue(item.value);
666
722
  const value = current.value.filter((entry) => hashMemoryValue(entry) !== removalHash);
667
- records.set(item.memory_key, { value, revision: current.revision + 1, source: item });
668
- tombstones.set(`${item.memory_key}:${removalHash}`, {
723
+ records.set(recordKey, { value, revision: current.revision + 1, source: item });
724
+ tombstones.set(`${recordKey}:${removalHash}`, {
669
725
  event_id: item.event_id, removed_event_id: current.source.event_id, value_hash: removalHash,
670
726
  });
671
727
  } else {
672
- records.delete(item.memory_key);
673
- tombstones.set(item.memory_key, {
728
+ records.delete(recordKey);
729
+ tombstones.set(recordKey, {
674
730
  event_id: item.event_id,
675
731
  removed_event_id: current.source.event_id,
676
732
  value_hash: hashMemoryValue(current.value),
@@ -691,8 +747,8 @@ export function reduceMemoryEvents(inputEvents = [], {
691
747
  candidates.push(conflictCandidate(item.memory_key, current ? [currentEventFromRecord(current), item] : [item]));
692
748
  continue;
693
749
  }
694
- records.set(item.memory_key, { value: item.value, revision: current.revision + 1, source: item });
695
- tombstones.delete(item.memory_key);
750
+ records.set(recordKey, { value: item.value, revision: current.revision + 1, source: item });
751
+ tombstones.delete(recordKey);
696
752
  const explicitlySupersededIds = new Set(
697
753
  Array.isArray(item.supersedes)
698
754
  ? item.supersedes
@@ -732,16 +788,16 @@ export function reduceMemoryEvents(inputEvents = [], {
732
788
  advanced = false;
733
789
  for (const pending of deferredAsserts) {
734
790
  if (resolvedCandidateIds.has(pending.candidate.candidate_id)) continue;
735
- const current = records.get(pending.event.memory_key);
791
+ const current = records.get(memoryRecordKey(pending.event));
736
792
  const currentSource = current?.source;
737
793
  if (!sameCompleteCausalLineage(pending.event, currentSource)) continue;
738
794
  if (pending.event.turn_sequence > currentSource.turn_sequence) {
739
- records.set(pending.event.memory_key, {
795
+ records.set(memoryRecordKey(pending.event), {
740
796
  value: pending.event.value,
741
797
  revision: current.revision + 1,
742
798
  source: pending.event,
743
799
  });
744
- tombstones.delete(pending.event.memory_key);
800
+ tombstones.delete(memoryRecordKey(pending.event));
745
801
  superseded.push({ event_id: currentSource.event_id, by_event_id: pending.event.event_id });
746
802
  revision += 1;
747
803
  appliedEventIds.push(pending.event.event_id);
@@ -762,7 +818,7 @@ export function reduceMemoryEvents(inputEvents = [], {
762
818
  .map((candidate) => {
763
819
  const pending = pendingByCandidateId.get(candidate.candidate_id);
764
820
  if (!pending) return candidate;
765
- const finalSource = currentEventFromRecord(records.get(candidate.memory_key));
821
+ const finalSource = currentEventFromRecord(records.get(candidate.record_key || candidate.memory_key));
766
822
  const previousSource = candidate.events?.find(
767
823
  (event) => event.event_id !== pending.event.event_id,
768
824
  );
@@ -791,10 +847,16 @@ export function reduceMemoryEvents(inputEvents = [], {
791
847
  superseded.sort((left, right) => left.event_id.localeCompare(right.event_id));
792
848
  const eventCursor = events.at(-1)?.event_id || 'none';
793
849
  const stateHash = hashMemoryValue({ state, tombstones: tombstoneObject });
850
+ const ambiguousRecordKeys = new Set(
851
+ unresolvedCandidates.map((candidate) => candidate.record_key || candidate.memory_key),
852
+ );
794
853
  const activeEvents = [
795
- ...Object.entries(recordObject).map(([memoryKey, record]) => ({
854
+ ...Object.entries(recordObject)
855
+ .filter(([recordKey]) => !ambiguousRecordKeys.has(recordKey))
856
+ .map(([recordKey, record]) => ({
796
857
  ...record.source,
797
- memory_key: memoryKey,
858
+ memory_key: record.source.memory_key,
859
+ projection_key: recordKey,
798
860
  operation: 'assert',
799
861
  value: record.value,
800
862
  })),
@@ -0,0 +1,25 @@
1
+ CREATE TABLE IF NOT EXISTS document_chunks (
2
+ chunk_id TEXT PRIMARY KEY,
3
+ project_id TEXT NOT NULL REFERENCES projects(project_id) ON DELETE CASCADE,
4
+ logical_path TEXT NOT NULL,
5
+ title TEXT NOT NULL DEFAULT '',
6
+ heading TEXT NOT NULL DEFAULT '',
7
+ entity_type TEXT NOT NULL DEFAULT 'document',
8
+ change_slug TEXT NOT NULL DEFAULT '',
9
+ session_id TEXT NOT NULL DEFAULT '',
10
+ work_session_id TEXT NOT NULL DEFAULT '',
11
+ authority TEXT NOT NULL DEFAULT 'candidate',
12
+ observed_at TEXT NOT NULL,
13
+ validity TEXT NOT NULL DEFAULT 'active',
14
+ content_hash TEXT NOT NULL,
15
+ ordinal INTEGER NOT NULL DEFAULT 0,
16
+ content TEXT NOT NULL,
17
+ FOREIGN KEY(project_id, logical_path) REFERENCES documents(project_id, logical_path) ON DELETE CASCADE,
18
+ UNIQUE(project_id, logical_path, ordinal)
19
+ );
20
+
21
+ CREATE INDEX IF NOT EXISTS idx_document_chunks_project_path
22
+ ON document_chunks(project_id, logical_path, ordinal);
23
+
24
+ CREATE INDEX IF NOT EXISTS idx_document_chunks_project_type
25
+ ON document_chunks(project_id, entity_type, validity, observed_at);
@@ -0,0 +1,217 @@
1
+ -- wendkeep:structural
2
+ -- Rebuild the operational identity tables so external identifiers are scoped by project.
3
+ -- Internal primary keys remain deterministic and opaque to the public API.
4
+
5
+ PRAGMA defer_foreign_keys = ON;
6
+
7
+ CREATE TABLE sessions_v5 (
8
+ session_pk TEXT PRIMARY KEY,
9
+ project_id TEXT NOT NULL REFERENCES projects(project_id) ON DELETE CASCADE,
10
+ session_id TEXT NOT NULL,
11
+ provider TEXT NOT NULL DEFAULT '',
12
+ status TEXT NOT NULL DEFAULT 'unknown',
13
+ summary TEXT NOT NULL DEFAULT '',
14
+ change_slug TEXT NOT NULL DEFAULT '',
15
+ started_at TEXT,
16
+ ended_at TEXT,
17
+ updated_at TEXT NOT NULL,
18
+ metadata_json TEXT NOT NULL DEFAULT '{}',
19
+ UNIQUE(project_id, session_id)
20
+ );
21
+
22
+ INSERT INTO sessions_v5
23
+ SELECT project_id || char(31) || session_id, project_id, session_id, provider, status,
24
+ summary, change_slug, started_at, ended_at, updated_at, metadata_json
25
+ FROM sessions;
26
+
27
+ INSERT OR IGNORE INTO sessions_v5(session_pk, project_id, session_id, updated_at)
28
+ SELECT project_id || char(31) || session_id, project_id, session_id,
29
+ COALESCE(MAX(occurred_at), CURRENT_TIMESTAMP)
30
+ FROM (
31
+ SELECT project_id, session_id, occurred_at FROM usage_rollups
32
+ UNION ALL SELECT project_id, session_id, occurred_at FROM llm_calls
33
+ UNION ALL SELECT project_id, session_id, occurred_at FROM transcripts
34
+ ) GROUP BY project_id, session_id;
35
+
36
+ INSERT OR IGNORE INTO sessions_v5(session_pk, project_id, session_id, updated_at)
37
+ SELECT project_id || char(31) || session_id, project_id, session_id, CURRENT_TIMESTAMP
38
+ FROM agent_runs;
39
+
40
+ CREATE TABLE agent_runs_v5 (
41
+ agent_pk TEXT PRIMARY KEY,
42
+ project_id TEXT NOT NULL REFERENCES projects(project_id) ON DELETE CASCADE,
43
+ agent_id TEXT NOT NULL,
44
+ session_id TEXT NOT NULL,
45
+ parent_agent_id TEXT,
46
+ role TEXT NOT NULL DEFAULT 'main',
47
+ agent_name TEXT NOT NULL DEFAULT '',
48
+ agent_type TEXT NOT NULL DEFAULT '',
49
+ workflow TEXT NOT NULL DEFAULT '',
50
+ status TEXT NOT NULL DEFAULT 'unknown',
51
+ model TEXT NOT NULL DEFAULT '',
52
+ effort TEXT NOT NULL DEFAULT '',
53
+ started_at TEXT,
54
+ ended_at TEXT,
55
+ metadata_json TEXT NOT NULL DEFAULT '{}',
56
+ UNIQUE(project_id, agent_id),
57
+ FOREIGN KEY(project_id, session_id) REFERENCES sessions_v5(project_id, session_id) ON DELETE CASCADE,
58
+ FOREIGN KEY(project_id, parent_agent_id) REFERENCES agent_runs_v5(project_id, agent_id) ON DELETE SET NULL
59
+ );
60
+
61
+ INSERT INTO agent_runs_v5
62
+ SELECT a.project_id || char(31) || a.agent_id, a.project_id, a.agent_id, a.session_id,
63
+ CASE WHEN EXISTS (
64
+ SELECT 1 FROM agent_runs parent
65
+ WHERE parent.project_id = a.project_id AND parent.agent_id = a.parent_agent_id
66
+ ) THEN a.parent_agent_id ELSE NULL END,
67
+ a.role, a.agent_name, a.agent_type, a.workflow, a.status, a.model, a.effort,
68
+ a.started_at, a.ended_at, a.metadata_json
69
+ FROM agent_runs a;
70
+
71
+ INSERT OR IGNORE INTO agent_runs_v5(
72
+ agent_pk, project_id, agent_id, session_id, role, status
73
+ )
74
+ SELECT project_id || char(31) || agent_id, project_id, agent_id, session_id, role, 'unknown'
75
+ FROM (
76
+ SELECT project_id, agent_id, session_id, role FROM usage_rollups
77
+ UNION ALL SELECT project_id, agent_id, session_id, role FROM llm_calls
78
+ UNION ALL SELECT project_id, agent_id, session_id, 'main' AS role FROM transcripts
79
+ );
80
+
81
+ CREATE TABLE usage_rollups_v5 (
82
+ rollup_pk TEXT PRIMARY KEY,
83
+ rollup_key TEXT NOT NULL,
84
+ project_id TEXT NOT NULL REFERENCES projects(project_id) ON DELETE CASCADE,
85
+ session_id TEXT NOT NULL,
86
+ agent_id TEXT NOT NULL,
87
+ role TEXT NOT NULL DEFAULT 'main',
88
+ provider TEXT NOT NULL DEFAULT '',
89
+ model_provider TEXT NOT NULL DEFAULT '',
90
+ model TEXT NOT NULL DEFAULT '',
91
+ effort TEXT NOT NULL DEFAULT '',
92
+ calls INTEGER NOT NULL DEFAULT 0,
93
+ tokens_input INTEGER NOT NULL DEFAULT 0,
94
+ tokens_cache_write INTEGER NOT NULL DEFAULT 0,
95
+ tokens_cache_read INTEGER NOT NULL DEFAULT 0,
96
+ tokens_output INTEGER NOT NULL DEFAULT 0,
97
+ tokens_reasoning INTEGER NOT NULL DEFAULT 0,
98
+ tokens_total INTEGER NOT NULL DEFAULT 0,
99
+ cost_usd REAL NOT NULL DEFAULT 0,
100
+ cost_status TEXT NOT NULL DEFAULT 'unknown',
101
+ pricing_source TEXT NOT NULL DEFAULT '',
102
+ pricing_version TEXT NOT NULL DEFAULT '',
103
+ wasted_usd REAL NOT NULL DEFAULT 0,
104
+ revision INTEGER NOT NULL DEFAULT 1,
105
+ occurred_at TEXT NOT NULL,
106
+ source_event_id TEXT NOT NULL REFERENCES ingest_events(event_id) ON DELETE CASCADE,
107
+ metadata_json TEXT NOT NULL DEFAULT '{}',
108
+ UNIQUE(project_id, rollup_key),
109
+ FOREIGN KEY(project_id, session_id) REFERENCES sessions_v5(project_id, session_id) ON DELETE CASCADE,
110
+ FOREIGN KEY(project_id, agent_id) REFERENCES agent_runs_v5(project_id, agent_id) ON DELETE CASCADE
111
+ );
112
+
113
+ INSERT INTO usage_rollups_v5
114
+ SELECT project_id || char(31) || rollup_key, rollup_key, project_id, session_id,
115
+ agent_id, role, provider, model_provider, model, effort, calls, tokens_input,
116
+ tokens_cache_write, tokens_cache_read, tokens_output, tokens_reasoning,
117
+ tokens_total, cost_usd, cost_status, pricing_source, pricing_version,
118
+ wasted_usd, revision, occurred_at, source_event_id, metadata_json
119
+ FROM usage_rollups;
120
+
121
+ CREATE TABLE llm_calls_v5 (
122
+ call_pk TEXT PRIMARY KEY,
123
+ call_id TEXT NOT NULL,
124
+ project_id TEXT NOT NULL REFERENCES projects(project_id) ON DELETE CASCADE,
125
+ session_id TEXT NOT NULL,
126
+ agent_id TEXT NOT NULL,
127
+ role TEXT NOT NULL DEFAULT 'main',
128
+ provider TEXT NOT NULL DEFAULT '',
129
+ model_provider TEXT NOT NULL DEFAULT '',
130
+ model TEXT NOT NULL DEFAULT '',
131
+ effort TEXT NOT NULL DEFAULT '',
132
+ sequence INTEGER NOT NULL DEFAULT 0,
133
+ occurred_at TEXT NOT NULL,
134
+ tokens_input INTEGER NOT NULL DEFAULT 0,
135
+ tokens_cache_write INTEGER NOT NULL DEFAULT 0,
136
+ tokens_cache_read INTEGER NOT NULL DEFAULT 0,
137
+ tokens_output INTEGER NOT NULL DEFAULT 0,
138
+ tokens_reasoning INTEGER NOT NULL DEFAULT 0,
139
+ tokens_total INTEGER NOT NULL DEFAULT 0,
140
+ cost_usd REAL NOT NULL DEFAULT 0,
141
+ cost_status TEXT NOT NULL DEFAULT 'unknown',
142
+ transcript_id TEXT,
143
+ prompt_text TEXT NOT NULL DEFAULT '',
144
+ response_text TEXT NOT NULL DEFAULT '',
145
+ status TEXT NOT NULL DEFAULT 'complete',
146
+ metadata_json TEXT NOT NULL DEFAULT '{}',
147
+ UNIQUE(project_id, call_id),
148
+ FOREIGN KEY(project_id, session_id) REFERENCES sessions_v5(project_id, session_id) ON DELETE CASCADE,
149
+ FOREIGN KEY(project_id, agent_id) REFERENCES agent_runs_v5(project_id, agent_id) ON DELETE CASCADE
150
+ );
151
+
152
+ INSERT INTO llm_calls_v5
153
+ SELECT project_id || char(31) || call_id, call_id, project_id, session_id, agent_id,
154
+ role, provider, model_provider, model, effort, sequence, occurred_at,
155
+ tokens_input, tokens_cache_write, tokens_cache_read, tokens_output,
156
+ tokens_reasoning, tokens_total, cost_usd, cost_status, transcript_id,
157
+ prompt_text, response_text, status, metadata_json
158
+ FROM llm_calls;
159
+
160
+ CREATE TABLE transcripts_v5 (
161
+ transcript_pk TEXT PRIMARY KEY,
162
+ transcript_id TEXT NOT NULL,
163
+ project_id TEXT NOT NULL REFERENCES projects(project_id) ON DELETE CASCADE,
164
+ session_id TEXT NOT NULL,
165
+ agent_id TEXT NOT NULL,
166
+ coverage TEXT NOT NULL DEFAULT 'summary_only',
167
+ codec TEXT NOT NULL DEFAULT 'gzip',
168
+ content_gzip BLOB NOT NULL,
169
+ content_sha256 TEXT NOT NULL,
170
+ original_bytes INTEGER NOT NULL,
171
+ compressed_bytes INTEGER NOT NULL,
172
+ source TEXT NOT NULL DEFAULT '',
173
+ occurred_at TEXT NOT NULL,
174
+ metadata_json TEXT NOT NULL DEFAULT '{}',
175
+ UNIQUE(project_id, transcript_id),
176
+ FOREIGN KEY(project_id, session_id) REFERENCES sessions_v5(project_id, session_id) ON DELETE CASCADE,
177
+ FOREIGN KEY(project_id, agent_id) REFERENCES agent_runs_v5(project_id, agent_id) ON DELETE CASCADE
178
+ );
179
+
180
+ INSERT INTO transcripts_v5
181
+ SELECT project_id || char(31) || transcript_id, transcript_id, project_id, session_id,
182
+ agent_id, coverage, codec, content_gzip, content_sha256, original_bytes,
183
+ compressed_bytes, source, occurred_at, metadata_json
184
+ FROM transcripts;
185
+
186
+ DROP TABLE llm_calls;
187
+ DROP TABLE transcripts;
188
+ DROP TABLE usage_rollups;
189
+ DROP TABLE agent_runs;
190
+ DROP TABLE sessions;
191
+
192
+ ALTER TABLE sessions_v5 RENAME TO sessions;
193
+ ALTER TABLE agent_runs_v5 RENAME TO agent_runs;
194
+ ALTER TABLE usage_rollups_v5 RENAME TO usage_rollups;
195
+ ALTER TABLE llm_calls_v5 RENAME TO llm_calls;
196
+ ALTER TABLE transcripts_v5 RENAME TO transcripts;
197
+
198
+ CREATE INDEX idx_sessions_project_time ON sessions(project_id, started_at, ended_at);
199
+ CREATE INDEX idx_agent_runs_project_session ON agent_runs(project_id, session_id, role);
200
+ CREATE INDEX idx_usage_rollups_project_time ON usage_rollups(project_id, occurred_at);
201
+ CREATE INDEX idx_usage_rollups_project_agent ON usage_rollups(project_id, agent_id, role);
202
+ CREATE INDEX idx_usage_rollups_project_model ON usage_rollups(project_id, model_provider, model);
203
+ CREATE INDEX idx_llm_calls_project_time ON llm_calls(project_id, occurred_at);
204
+ CREATE INDEX idx_llm_calls_project_agent ON llm_calls(project_id, agent_id, sequence);
205
+ CREATE INDEX idx_llm_calls_project_model ON llm_calls(project_id, model_provider, model);
206
+ CREATE INDEX idx_transcripts_project_session ON transcripts(project_id, session_id, occurred_at);
207
+ CREATE INDEX idx_transcripts_project_coverage ON transcripts(project_id, coverage);
208
+
209
+ CREATE TABLE project_snapshots (
210
+ project_id TEXT PRIMARY KEY REFERENCES projects(project_id) ON DELETE CASCADE,
211
+ event_id TEXT NOT NULL,
212
+ captured_at TEXT NOT NULL,
213
+ snapshot_json TEXT NOT NULL,
214
+ UNIQUE(project_id, event_id)
215
+ );
216
+
217
+ CREATE INDEX idx_project_snapshots_captured ON project_snapshots(captured_at);
package/src/change.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // `wendkeep change <sub>` — native change lifecycle CLI (Pilar B).
2
- import { readFileSync } from 'node:fs';
2
+ import { readFileSync, readdirSync } from 'node:fs';
3
3
  import { isAbsolute, join, resolve } from 'node:path';
4
4
  import {
5
5
  newChange,
@@ -22,6 +22,22 @@ import { evaluateGate, requiredSensors } from '../hooks/sensors-core.mjs';
22
22
  import { buildEffectiveRequirementPackage, evaluateVerdict, formatOrphanReqs, tasksHashOf, parseSpecsList, parseDelta, parseRequirements, applyDelta, validateSpecImpact } from '../hooks/spec-core.mjs';
23
23
  import { getNextAdrNumber, readControl, readSessionRegistry, upsertSessionRegistry } from '../hooks/obsidian-common.mjs';
24
24
  import { getLocale } from '../hooks/locale.mjs';
25
+ import { enqueueObserverDocumentChange } from './observer-sql-publish.mjs';
26
+ import { readProjectForValidation } from '../packages/vault/src/validate-memory.mjs';
27
+
28
+ function observerMarkdownUnder(vaultBase, relativeRoot) {
29
+ const output = [];
30
+ const walk = (absolute, relative) => {
31
+ for (const entry of readdirSync(absolute, { withFileTypes: true })) {
32
+ const nextAbsolute = join(absolute, entry.name);
33
+ const nextRelative = join(relative, entry.name);
34
+ if (entry.isDirectory()) walk(nextAbsolute, nextRelative);
35
+ else if (entry.isFile() && entry.name.endsWith('.md')) output.push(nextRelative);
36
+ }
37
+ };
38
+ try { walk(join(vaultBase, relativeRoot), relativeRoot); } catch { /* reconcile recupera */ }
39
+ return output;
40
+ }
25
41
 
26
42
  function resolveVault(argv) {
27
43
  let vault;
@@ -299,6 +315,30 @@ export function runChange(argv) {
299
315
  process.stderr.write(`change archive BLOCKED (gate): ${r.failing.join('; ')}\n`);
300
316
  process.exit(1);
301
317
  }
318
+ try {
319
+ const project = readProjectForValidation(vaultBase);
320
+ if (project.ok && project.projectId) {
321
+ const loc = getLocale(vaultBase);
322
+ for (const archivedPath of observerMarkdownUnder(vaultBase, r.archivedRel)) {
323
+ enqueueObserverDocumentChange({ vaultBase, projectId: project.projectId, logicalPath: archivedPath });
324
+ const suffix = archivedPath.slice(String(r.archivedRel).length).replace(/^[\\/]+/, '');
325
+ enqueueObserverDocumentChange({
326
+ vaultBase,
327
+ projectId: project.projectId,
328
+ logicalPath: join(loc.folders.changes, slug, suffix),
329
+ deleted: true,
330
+ });
331
+ }
332
+ if (r.adrRel) enqueueObserverDocumentChange({ vaultBase, projectId: project.projectId, logicalPath: r.adrRel });
333
+ for (const capability of r.promoted || []) {
334
+ enqueueObserverDocumentChange({
335
+ vaultBase,
336
+ projectId: project.projectId,
337
+ logicalPath: join(loc.folders.specs, capability, 'spec.md'),
338
+ });
339
+ }
340
+ }
341
+ } catch { /* Observer é fail-open; reconcile recupera qualquer enqueue perdido. */ }
302
342
  process.stdout.write(`archived: ${r.archivedRel}${r.adrRel ? `; ADR: ${r.adrRel}` : '; GUIDE compacta: sem ADR'}\n`);
303
343
  if (r.promoted && r.promoted.length) process.stdout.write(`specs promovidas: ${r.promoted.join(', ')}\n`);
304
344
  if (r.specWarnings && r.specWarnings.length) for (const w of r.specWarnings) process.stderr.write(` aviso spec: ${w}\n`);