@jungjaehoon/mama-core 1.4.2 → 1.6.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 (181) hide show
  1. package/README.md +48 -10
  2. package/db/migrations/026-create-canonical-entity-tables.sql +99 -0
  3. package/db/migrations/027-create-entity-review-support.sql +44 -0
  4. package/db/migrations/028-create-entity-audit-tables.sql +45 -0
  5. package/db/migrations/029-extend-entity-observations-observation-type.sql +60 -0
  6. package/db/migrations/030-case-first-memory-substrate.sql +901 -0
  7. package/db/migrations/031-wiki-page-index-provenance-columns.sql +2 -0
  8. package/db/migrations/032-add-memory-provenance-columns.sql +21 -0
  9. package/db/migrations/033-create-model-runs-and-tool-traces.sql +53 -0
  10. package/db/migrations/034-add-connector-event-scope-columns.sql +14 -0
  11. package/db/migrations/035-create-twin-edges.sql +63 -0
  12. package/db/migrations/036-create-agent-situation-packets.sql +61 -0
  13. package/dist/agent-graph/alias-write.d.ts +3 -0
  14. package/dist/agent-graph/alias-write.js +231 -0
  15. package/dist/agent-graph/entity-resolve.d.ts +3 -0
  16. package/dist/agent-graph/entity-resolve.js +275 -0
  17. package/dist/agent-graph/errors.d.ts +4 -0
  18. package/dist/agent-graph/errors.js +11 -0
  19. package/dist/agent-graph/graph-query.d.ts +5 -0
  20. package/dist/agent-graph/graph-query.js +359 -0
  21. package/dist/agent-graph/index.d.ts +6 -0
  22. package/dist/agent-graph/index.js +22 -0
  23. package/dist/agent-graph/types.d.ts +162 -0
  24. package/dist/agent-graph/types.js +3 -0
  25. package/dist/agent-situation/builder.d.ts +8 -0
  26. package/dist/agent-situation/builder.js +239 -0
  27. package/dist/agent-situation/cache-key.d.ts +4 -0
  28. package/dist/agent-situation/cache-key.js +64 -0
  29. package/dist/agent-situation/index.d.ts +7 -0
  30. package/dist/agent-situation/index.js +28 -0
  31. package/dist/agent-situation/packet-store.d.ts +27 -0
  32. package/dist/agent-situation/packet-store.js +228 -0
  33. package/dist/agent-situation/ranking-policy.d.ts +18 -0
  34. package/dist/agent-situation/ranking-policy.js +75 -0
  35. package/dist/agent-situation/source-readers.d.ts +52 -0
  36. package/dist/agent-situation/source-readers.js +300 -0
  37. package/dist/agent-situation/types.d.ts +195 -0
  38. package/dist/agent-situation/types.js +3 -0
  39. package/dist/canonicalize.d.ts +38 -0
  40. package/dist/canonicalize.js +106 -0
  41. package/dist/cases/case-links.d.ts +116 -0
  42. package/dist/cases/case-links.js +794 -0
  43. package/dist/cases/composition-overrides.d.ts +90 -0
  44. package/dist/cases/composition-overrides.js +724 -0
  45. package/dist/cases/corrections.d.ts +156 -0
  46. package/dist/cases/corrections.js +1045 -0
  47. package/dist/cases/freshness.d.ts +57 -0
  48. package/dist/cases/freshness.js +283 -0
  49. package/dist/cases/live-state.d.ts +48 -0
  50. package/dist/cases/live-state.js +256 -0
  51. package/dist/cases/membership-explain.d.ts +56 -0
  52. package/dist/cases/membership-explain.js +140 -0
  53. package/dist/cases/membership-matcher.d.ts +23 -0
  54. package/dist/cases/membership-matcher.js +193 -0
  55. package/dist/cases/merge-split.d.ts +61 -0
  56. package/dist/cases/merge-split.js +368 -0
  57. package/dist/cases/role-inference.d.ts +10 -0
  58. package/dist/cases/role-inference.js +85 -0
  59. package/dist/cases/search-rollup.d.ts +27 -0
  60. package/dist/cases/search-rollup.js +309 -0
  61. package/dist/cases/sqlite-transaction.d.ts +10 -0
  62. package/dist/cases/sqlite-transaction.js +62 -0
  63. package/dist/cases/store.d.ts +74 -0
  64. package/dist/cases/store.js +816 -0
  65. package/dist/cases/target-ref.d.ts +25 -0
  66. package/dist/cases/target-ref.js +22 -0
  67. package/dist/cases/timeline-range.d.ts +6 -0
  68. package/dist/cases/timeline-range.js +322 -0
  69. package/dist/cases/tombstone-sweeper.d.ts +13 -0
  70. package/dist/cases/tombstone-sweeper.js +128 -0
  71. package/dist/cases/types.d.ts +244 -0
  72. package/dist/cases/types.js +47 -0
  73. package/dist/cases/wiki-page-index.d.ts +50 -0
  74. package/dist/cases/wiki-page-index.js +350 -0
  75. package/dist/connectors/event-index.d.ts +45 -0
  76. package/dist/connectors/event-index.js +381 -0
  77. package/dist/connectors/raw-query.d.ts +18 -0
  78. package/dist/connectors/raw-query.js +305 -0
  79. package/dist/connectors/types.d.ts +109 -0
  80. package/dist/connectors/types.js +3 -0
  81. package/dist/db-adapter/node-sqlite-adapter.d.ts +6 -0
  82. package/dist/db-adapter/node-sqlite-adapter.js +131 -0
  83. package/dist/db-manager.d.ts +12 -1
  84. package/dist/db-manager.js +60 -5
  85. package/dist/decision-formatter.d.ts +2 -0
  86. package/dist/decision-formatter.js +11 -8
  87. package/dist/edges/ref-validation.d.ts +8 -0
  88. package/dist/edges/ref-validation.js +262 -0
  89. package/dist/edges/store.d.ts +10 -0
  90. package/dist/edges/store.js +343 -0
  91. package/dist/edges/types.d.ts +86 -0
  92. package/dist/edges/types.js +18 -0
  93. package/dist/embedding-server/mobile/daemon.d.ts +1 -0
  94. package/dist/embedding-server/mobile/daemon.js +11 -1
  95. package/dist/embeddings.js +13 -0
  96. package/dist/entities/audit-metrics.d.ts +73 -0
  97. package/dist/entities/audit-metrics.js +128 -0
  98. package/dist/entities/candidate-generator.d.ts +9 -0
  99. package/dist/entities/candidate-generator.js +436 -0
  100. package/dist/entities/entity-impact.d.ts +41 -0
  101. package/dist/entities/entity-impact.js +140 -0
  102. package/dist/entities/entity-linked-decision-counts.d.ts +2 -0
  103. package/dist/entities/entity-linked-decision-counts.js +29 -0
  104. package/dist/entities/entity-list.d.ts +23 -0
  105. package/dist/entities/entity-list.js +99 -0
  106. package/dist/entities/entity-orphan-list.d.ts +26 -0
  107. package/dist/entities/entity-orphan-list.js +91 -0
  108. package/dist/entities/entity-search.d.ts +21 -0
  109. package/dist/entities/entity-search.js +150 -0
  110. package/dist/entities/errors.d.ts +65 -0
  111. package/dist/entities/errors.js +124 -0
  112. package/dist/entities/exact-merge-backfill.d.ts +11 -0
  113. package/dist/entities/exact-merge-backfill.js +161 -0
  114. package/dist/entities/lineage-backfill.d.ts +12 -0
  115. package/dist/entities/lineage-backfill.js +101 -0
  116. package/dist/entities/lineage-store.d.ts +51 -0
  117. package/dist/entities/lineage-store.js +308 -0
  118. package/dist/entities/normalization.d.ts +14 -0
  119. package/dist/entities/normalization.js +63 -0
  120. package/dist/entities/policy-store.d.ts +17 -0
  121. package/dist/entities/policy-store.js +263 -0
  122. package/dist/entities/policy-types.d.ts +72 -0
  123. package/dist/entities/policy-types.js +59 -0
  124. package/dist/entities/projection.d.ts +8 -0
  125. package/dist/entities/projection.js +82 -0
  126. package/dist/entities/provenance-query.d.ts +33 -0
  127. package/dist/entities/provenance-query.js +167 -0
  128. package/dist/entities/read-identity.d.ts +31 -0
  129. package/dist/entities/read-identity.js +264 -0
  130. package/dist/entities/recall-bridge.d.ts +5 -0
  131. package/dist/entities/recall-bridge.js +171 -0
  132. package/dist/entities/resolution-engine.d.ts +15 -0
  133. package/dist/entities/resolution-engine.js +69 -0
  134. package/dist/entities/rollback-preview.d.ts +39 -0
  135. package/dist/entities/rollback-preview.js +235 -0
  136. package/dist/entities/source-locator.d.ts +3 -0
  137. package/dist/entities/source-locator.js +32 -0
  138. package/dist/entities/store.d.ts +75 -0
  139. package/dist/entities/store.js +465 -0
  140. package/dist/entities/types.d.ts +162 -0
  141. package/dist/entities/types.js +30 -0
  142. package/dist/index.d.ts +62 -3
  143. package/dist/index.js +116 -26
  144. package/dist/mama-api.d.ts +53 -35
  145. package/dist/mama-api.js +437 -78
  146. package/dist/memory/api.d.ts +26 -38
  147. package/dist/memory/api.js +772 -68
  148. package/dist/memory/event-store.d.ts +3 -0
  149. package/dist/memory/event-store.js +21 -2
  150. package/dist/memory/provenance-audit.d.ts +22 -0
  151. package/dist/memory/provenance-audit.js +112 -0
  152. package/dist/memory/provenance-query.d.ts +11 -0
  153. package/dist/memory/provenance-query.js +193 -0
  154. package/dist/memory/provenance.d.ts +29 -0
  155. package/dist/memory/provenance.js +136 -0
  156. package/dist/memory/scope-backfill.d.ts +17 -0
  157. package/dist/memory/scope-backfill.js +77 -0
  158. package/dist/memory/types.d.ts +97 -16
  159. package/dist/model-runs/store.d.ts +13 -0
  160. package/dist/model-runs/store.js +457 -0
  161. package/dist/model-runs/tool-trace-store.d.ts +4 -0
  162. package/dist/model-runs/tool-trace-store.js +114 -0
  163. package/dist/model-runs/types.d.ts +69 -0
  164. package/dist/model-runs/types.js +5 -0
  165. package/dist/runtime/trusted-provenance.d.ts +3 -0
  166. package/dist/runtime/trusted-provenance.js +6 -0
  167. package/dist/search/feedback-store.d.ts +59 -0
  168. package/dist/search/feedback-store.js +211 -0
  169. package/dist/search/question-type.d.ts +5 -0
  170. package/dist/search/question-type.js +41 -0
  171. package/dist/search/ranker-features.d.ts +23 -0
  172. package/dist/search/ranker-features.js +104 -0
  173. package/dist/search/ranker-rescore.d.ts +31 -0
  174. package/dist/search/ranker-rescore.js +226 -0
  175. package/dist/search/ranker-trainer.d.ts +94 -0
  176. package/dist/search/ranker-trainer.js +860 -0
  177. package/dist/search/search-quality.d.ts +36 -0
  178. package/dist/search/search-quality.js +29 -0
  179. package/dist/test-utils.d.ts +14 -0
  180. package/dist/test-utils.js +52 -0
  181. package/package.json +28 -3
@@ -0,0 +1,72 @@
1
+ export declare const ENTITY_POLICY_ROLES: readonly ["viewer", "operator", "admin"];
2
+ export type EntityRole = (typeof ENTITY_POLICY_ROLES)[number];
3
+ export declare const ENTITY_POLICY_KINDS: readonly ["entity_kind", "merge_guardrails", "review_thresholds", "connector_policy"];
4
+ export type EntityPolicyKind = (typeof ENTITY_POLICY_KINDS)[number];
5
+ export declare const ENTITY_POLICY_PROPOSAL_STATUSES: readonly ["pending", "approved", "rejected"];
6
+ export type EntityPolicyProposalStatus = (typeof ENTITY_POLICY_PROPOSAL_STATUSES)[number];
7
+ export interface EntityPolicyRow {
8
+ policy_key: string;
9
+ policy_kind: EntityPolicyKind;
10
+ value_json: string;
11
+ value: Record<string, unknown>;
12
+ version: number;
13
+ created_at: number;
14
+ updated_at: number;
15
+ }
16
+ export interface EntityRoleBinding {
17
+ actor_id: string;
18
+ role: EntityRole;
19
+ created_at: number;
20
+ updated_at: number;
21
+ }
22
+ export interface EntityRoleBindingInput {
23
+ actor_id: string;
24
+ role: EntityRole;
25
+ }
26
+ export interface EntityPolicyProposalRow {
27
+ proposal_id: string;
28
+ policy_key: string;
29
+ policy_kind: EntityPolicyKind;
30
+ proposed_value_json: string;
31
+ proposed_value: Record<string, unknown>;
32
+ proposer_actor: string;
33
+ approver_actor: string | null;
34
+ reason: string;
35
+ status: EntityPolicyProposalStatus;
36
+ created_at: number;
37
+ approved_at: number | null;
38
+ }
39
+ export interface EntityPolicyProposalInput {
40
+ policy_key: string;
41
+ policy_kind: EntityPolicyKind;
42
+ proposed_value: Record<string, unknown>;
43
+ proposer_actor: string;
44
+ reason: string;
45
+ proposal_id?: string;
46
+ created_at?: number;
47
+ }
48
+ export interface EntityPolicyApprovalInput {
49
+ proposal_id: string;
50
+ approver_actor: string;
51
+ approved_at?: number;
52
+ }
53
+ export interface EntityPolicyBootstrapPolicy {
54
+ policy_key: string;
55
+ policy_kind: EntityPolicyKind;
56
+ value: Record<string, unknown>;
57
+ }
58
+ export interface EntityPolicyBootstrapRoleBinding {
59
+ actor_id: string;
60
+ role: EntityRole;
61
+ }
62
+ export interface EntityPolicyBootstrapDocument {
63
+ version: number;
64
+ policies: EntityPolicyBootstrapPolicy[];
65
+ role_bindings: EntityPolicyBootstrapRoleBinding[];
66
+ }
67
+ export declare const ENTITY_ROLE_ORDER: Record<EntityRole, number>;
68
+ export declare function isEntityRole(value: string): value is EntityRole;
69
+ export declare function isEntityPolicyKind(value: string): value is EntityPolicyKind;
70
+ export declare function isEntityPolicyProposalStatus(value: string): value is EntityPolicyProposalStatus;
71
+ export declare function createDefaultEntityPolicyBootstrap(adminActor?: string): EntityPolicyBootstrapDocument;
72
+ //# sourceMappingURL=policy-types.d.ts.map
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ENTITY_ROLE_ORDER = exports.ENTITY_POLICY_PROPOSAL_STATUSES = exports.ENTITY_POLICY_KINDS = exports.ENTITY_POLICY_ROLES = void 0;
4
+ exports.isEntityRole = isEntityRole;
5
+ exports.isEntityPolicyKind = isEntityPolicyKind;
6
+ exports.isEntityPolicyProposalStatus = isEntityPolicyProposalStatus;
7
+ exports.createDefaultEntityPolicyBootstrap = createDefaultEntityPolicyBootstrap;
8
+ exports.ENTITY_POLICY_ROLES = ['viewer', 'operator', 'admin'];
9
+ exports.ENTITY_POLICY_KINDS = [
10
+ 'entity_kind',
11
+ 'merge_guardrails',
12
+ 'review_thresholds',
13
+ 'connector_policy',
14
+ ];
15
+ exports.ENTITY_POLICY_PROPOSAL_STATUSES = ['pending', 'approved', 'rejected'];
16
+ exports.ENTITY_ROLE_ORDER = {
17
+ viewer: 0,
18
+ operator: 1,
19
+ admin: 2,
20
+ };
21
+ function isEntityRole(value) {
22
+ return exports.ENTITY_POLICY_ROLES.includes(value);
23
+ }
24
+ function isEntityPolicyKind(value) {
25
+ return exports.ENTITY_POLICY_KINDS.includes(value);
26
+ }
27
+ function isEntityPolicyProposalStatus(value) {
28
+ return exports.ENTITY_POLICY_PROPOSAL_STATUSES.includes(value);
29
+ }
30
+ function createDefaultEntityPolicyBootstrap(adminActor = 'local:viewer') {
31
+ return {
32
+ version: 1,
33
+ policies: [
34
+ {
35
+ policy_key: 'merge_guardrails.default',
36
+ policy_kind: 'merge_guardrails',
37
+ value: {
38
+ max_false_merge_rate: 0.02,
39
+ require_review_for_cross_scope: true,
40
+ },
41
+ },
42
+ {
43
+ policy_key: 'review_thresholds.default',
44
+ policy_kind: 'review_thresholds',
45
+ value: {
46
+ approve_score_min: 0.92,
47
+ defer_score_min: 0.78,
48
+ },
49
+ },
50
+ ],
51
+ role_bindings: [
52
+ {
53
+ actor_id: adminActor,
54
+ role: 'admin',
55
+ },
56
+ ],
57
+ };
58
+ }
59
+ //# sourceMappingURL=policy-types.js.map
@@ -0,0 +1,8 @@
1
+ import type { EntityAlias, EntityNode, EntityTimelineEvent } from './types.js';
2
+ import type { MemoryRecord } from '../memory/types.js';
3
+ interface ProjectionOptions {
4
+ nodeLookup?: Record<string, EntityNode>;
5
+ }
6
+ export declare function projectEntityToRecallSummary(node: EntityNode, aliases: EntityAlias[], latestEvent: EntityTimelineEvent | null, options?: ProjectionOptions): MemoryRecord;
7
+ export {};
8
+ //# sourceMappingURL=projection.d.ts.map
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.projectEntityToRecallSummary = projectEntityToRecallSummary;
4
+ const errors_js_1 = require("./errors.js");
5
+ function formatTimelineEventForRecall(latestEvent) {
6
+ if (!latestEvent) {
7
+ return null;
8
+ }
9
+ const parts = [];
10
+ const eventType = latestEvent.event_type.trim();
11
+ const summary = latestEvent.summary.trim();
12
+ const details = latestEvent.details?.trim() ?? '';
13
+ if (eventType.length > 0) {
14
+ parts.push(eventType);
15
+ }
16
+ if (summary.length > 0) {
17
+ parts.push(summary);
18
+ }
19
+ if (details.length > 0 && details !== summary) {
20
+ try {
21
+ const parsed = JSON.parse(details);
22
+ if (parsed && typeof parsed === 'object') {
23
+ parts.push(JSON.stringify(parsed));
24
+ }
25
+ else {
26
+ parts.push(details);
27
+ }
28
+ }
29
+ catch {
30
+ parts.push(details);
31
+ }
32
+ }
33
+ return parts.length > 0 ? parts.join('\n') : null;
34
+ }
35
+ function assertNoCircularMerge(node, options) {
36
+ if (!options?.nodeLookup) {
37
+ return;
38
+ }
39
+ const seen = new Set();
40
+ let current = options.nodeLookup[node.id] ?? node;
41
+ while (current?.merged_into) {
42
+ if (seen.has(current.id)) {
43
+ throw new errors_js_1.EntityLabelMissingError({
44
+ entity_id: node.id,
45
+ reason: 'circular_merged_into_chain',
46
+ });
47
+ }
48
+ seen.add(current.id);
49
+ current = options.nodeLookup[current.merged_into];
50
+ }
51
+ }
52
+ function projectEntityToRecallSummary(node, aliases, latestEvent, options) {
53
+ if (!node.preferred_label.trim()) {
54
+ throw new errors_js_1.EntityLabelMissingError({
55
+ entity_id: node.id,
56
+ reason: 'missing_preferred_label',
57
+ });
58
+ }
59
+ assertNoCircularMerge(node, options);
60
+ const aliasText = aliases.map((alias) => alias.label).join(', ');
61
+ const detailParts = [formatTimelineEventForRecall(latestEvent), aliasText].filter((value) => Boolean(value));
62
+ const scopeKind = node.scope_kind ?? 'global';
63
+ const scopeId = node.scope_kind === null ? 'global' : (node.scope_id ?? 'global');
64
+ return {
65
+ id: node.id,
66
+ topic: `entity/${node.id}`,
67
+ kind: 'fact',
68
+ summary: node.preferred_label,
69
+ details: detailParts.join('\n'),
70
+ confidence: 0.9,
71
+ status: 'active',
72
+ scopes: [{ kind: scopeKind, id: scopeId }],
73
+ source: {
74
+ package: 'mama-core',
75
+ source_type: 'entity_canonical',
76
+ project_id: node.scope_id ?? undefined,
77
+ },
78
+ created_at: node.created_at,
79
+ updated_at: node.updated_at,
80
+ };
81
+ }
82
+ //# sourceMappingURL=projection.js.map
@@ -0,0 +1,33 @@
1
+ import { type SourceLocatorKind } from './source-locator.js';
2
+ import type { MemoryRecord } from '../memory/types.js';
3
+ export interface ProvenanceObservationSummary {
4
+ id: string;
5
+ observation_type: string;
6
+ entity_kind_hint: string | null;
7
+ surface_form: string;
8
+ normalized_form: string;
9
+ lang: string | null;
10
+ script: string | null;
11
+ context_summary: string | null;
12
+ scope_kind: string;
13
+ scope_id: string | null;
14
+ extractor_version: string;
15
+ embedding_model_version: string | null;
16
+ source_connector: string;
17
+ source_locator: string | null;
18
+ source_locator_kind: SourceLocatorKind;
19
+ source_raw_record_id: string;
20
+ created_at: number;
21
+ }
22
+ export interface MemoryProvenanceResult {
23
+ status: 'legacy' | 'manual' | 'dropped' | 'resolved';
24
+ memory: MemoryRecord;
25
+ observations: ProvenanceObservationSummary[];
26
+ audit: {
27
+ event_type: 'provenance.empty_batch' | 'provenance.link_write_failed';
28
+ reason: string | null;
29
+ created_at: number;
30
+ } | null;
31
+ }
32
+ export declare function queryProvenanceForMemory(memoryId: string): Promise<MemoryProvenanceResult>;
33
+ //# sourceMappingURL=provenance-query.d.ts.map
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.queryProvenanceForMemory = queryProvenanceForMemory;
4
+ const db_manager_js_1 = require("../db-manager.js");
5
+ const source_locator_js_1 = require("./source-locator.js");
6
+ function parseSource(trustContext) {
7
+ if (typeof trustContext !== 'string' || trustContext.length === 0) {
8
+ return { package: 'mama-core', source_type: 'db' };
9
+ }
10
+ try {
11
+ const parsed = JSON.parse(trustContext);
12
+ if (parsed?.source && typeof parsed.source === 'object') {
13
+ return parsed.source;
14
+ }
15
+ }
16
+ catch {
17
+ // ignore malformed trust_context
18
+ }
19
+ return { package: 'mama-core', source_type: 'db' };
20
+ }
21
+ function mapMemoryRecord(row, scopes) {
22
+ return {
23
+ id: row.id,
24
+ topic: row.topic,
25
+ kind: (row.kind ?? 'decision'),
26
+ summary: row.summary ?? row.decision,
27
+ details: row.reasoning ?? row.decision,
28
+ confidence: row.confidence ?? 0.5,
29
+ status: (row.status ?? 'active'),
30
+ scopes,
31
+ source: parseSource(row.trust_context),
32
+ created_at: row.created_at,
33
+ updated_at: row.updated_at ?? row.created_at,
34
+ event_date: row.event_date,
35
+ };
36
+ }
37
+ function loadScopes(memoryId) {
38
+ const adapter = (0, db_manager_js_1.getAdapter)();
39
+ const rows = adapter
40
+ .prepare(`
41
+ SELECT msb.memory_id, ms.kind, ms.external_id
42
+ FROM memory_scope_bindings msb
43
+ JOIN memory_scopes ms ON ms.id = msb.scope_id
44
+ WHERE msb.memory_id = ?
45
+ ORDER BY msb.is_primary DESC
46
+ `)
47
+ .all(memoryId);
48
+ return rows.map((row) => ({
49
+ kind: row.kind,
50
+ id: row.external_id,
51
+ }));
52
+ }
53
+ function loadObservations(memoryId) {
54
+ const adapter = (0, db_manager_js_1.getAdapter)();
55
+ const rows = adapter
56
+ .prepare(`
57
+ SELECT
58
+ o.id,
59
+ o.observation_type,
60
+ o.entity_kind_hint,
61
+ o.surface_form,
62
+ o.normalized_form,
63
+ o.lang,
64
+ o.script,
65
+ o.context_summary,
66
+ o.scope_kind,
67
+ o.scope_id,
68
+ o.extractor_version,
69
+ o.embedding_model_version,
70
+ o.source_connector,
71
+ o.source_locator,
72
+ o.source_raw_record_id,
73
+ o.created_at
74
+ FROM decision_entity_sources des
75
+ JOIN entity_observations o ON o.id = des.entity_observation_id
76
+ WHERE des.decision_id = ?
77
+ ORDER BY des.created_at ASC, o.created_at ASC
78
+ `)
79
+ .all(memoryId);
80
+ return rows.map((row) => ({
81
+ id: row.id,
82
+ observation_type: row.observation_type,
83
+ entity_kind_hint: row.entity_kind_hint,
84
+ surface_form: row.surface_form,
85
+ normalized_form: row.normalized_form,
86
+ lang: row.lang,
87
+ script: row.script,
88
+ context_summary: row.context_summary,
89
+ scope_kind: row.scope_kind,
90
+ scope_id: row.scope_id,
91
+ extractor_version: row.extractor_version,
92
+ embedding_model_version: row.embedding_model_version,
93
+ source_connector: row.source_connector,
94
+ source_locator: row.source_locator,
95
+ source_locator_kind: (0, source_locator_js_1.detectSourceLocatorKind)(row.source_locator),
96
+ source_raw_record_id: row.source_raw_record_id,
97
+ created_at: row.created_at,
98
+ }));
99
+ }
100
+ function loadProvenanceAudit(memoryId) {
101
+ const adapter = (0, db_manager_js_1.getAdapter)();
102
+ const row = adapter
103
+ .prepare(`
104
+ SELECT event_type, reason, created_at
105
+ FROM memory_events
106
+ WHERE memory_id = ?
107
+ AND event_type IN ('provenance.empty_batch', 'provenance.link_write_failed')
108
+ ORDER BY created_at DESC
109
+ LIMIT 1
110
+ `)
111
+ .get(memoryId);
112
+ if (!row) {
113
+ return null;
114
+ }
115
+ return {
116
+ event_type: row.event_type,
117
+ reason: row.reason,
118
+ created_at: row.created_at,
119
+ };
120
+ }
121
+ async function queryProvenanceForMemory(memoryId) {
122
+ await (0, db_manager_js_1.initDB)();
123
+ const adapter = (0, db_manager_js_1.getAdapter)();
124
+ const row = adapter
125
+ .prepare(`
126
+ SELECT
127
+ id,
128
+ topic,
129
+ decision,
130
+ reasoning,
131
+ confidence,
132
+ created_at,
133
+ updated_at,
134
+ trust_context,
135
+ kind,
136
+ status,
137
+ summary,
138
+ event_date
139
+ FROM decisions
140
+ WHERE id = ?
141
+ `)
142
+ .get(memoryId);
143
+ if (!row) {
144
+ throw new Error(`Decision not found: ${memoryId}`);
145
+ }
146
+ const scopes = loadScopes(memoryId);
147
+ const memory = mapMemoryRecord(row, scopes);
148
+ const observations = loadObservations(memoryId);
149
+ const audit = loadProvenanceAudit(memoryId);
150
+ let status = 'legacy';
151
+ if (observations.length > 0) {
152
+ status = 'resolved';
153
+ }
154
+ else if (audit?.event_type === 'provenance.link_write_failed') {
155
+ status = 'dropped';
156
+ }
157
+ else if (audit?.event_type === 'provenance.empty_batch') {
158
+ status = 'manual';
159
+ }
160
+ return {
161
+ status,
162
+ memory,
163
+ observations,
164
+ audit,
165
+ };
166
+ }
167
+ //# sourceMappingURL=provenance-query.js.map
@@ -0,0 +1,31 @@
1
+ import type { MemoryRecord } from '../memory/types.js';
2
+ export interface CanonicalReadEntity {
3
+ id: string;
4
+ label: string;
5
+ kind: string;
6
+ }
7
+ export type ReadIdentity = {
8
+ kind: 'canonical';
9
+ entities: CanonicalReadEntity[];
10
+ primaryEntity: CanonicalReadEntity;
11
+ legacyTopic: string | null;
12
+ shadowConflict: boolean;
13
+ displaySubject?: string | null;
14
+ displayTopic?: string | null;
15
+ displaySuffix?: string | null;
16
+ } | {
17
+ kind: 'topic';
18
+ topic: string;
19
+ displaySubject?: string | null;
20
+ displayTopic?: string | null;
21
+ displaySuffix?: string | null;
22
+ } | {
23
+ kind: 'raw';
24
+ label: string;
25
+ displaySubject?: string | null;
26
+ displayTopic?: string | null;
27
+ displaySuffix?: string | null;
28
+ };
29
+ export declare function resolveReadIdentity(record: Pick<MemoryRecord, 'topic' | 'summary'>, entities: CanonicalReadEntity[]): ReadIdentity;
30
+ export declare function loadDecisionReadIdentityIndex(decisionIds: string[]): Promise<Map<string, CanonicalReadEntity[]>>;
31
+ //# sourceMappingURL=read-identity.d.ts.map