@jungjaehoon/mama-core 1.6.0 → 1.7.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 (35) hide show
  1. package/db/migrations/037-create-context-packets.sql +32 -0
  2. package/dist/cases/wiki-page-index.js +6 -2
  3. package/dist/context-compile/boundary-defaults.d.ts +12 -0
  4. package/dist/context-compile/boundary-defaults.js +105 -0
  5. package/dist/context-compile/compiler-policy.d.ts +39 -0
  6. package/dist/context-compile/compiler-policy.js +147 -0
  7. package/dist/context-compile/compiler.d.ts +18 -0
  8. package/dist/context-compile/compiler.js +401 -0
  9. package/dist/context-compile/index.d.ts +9 -0
  10. package/dist/context-compile/index.js +25 -0
  11. package/dist/context-compile/packet-store.d.ts +9 -0
  12. package/dist/context-compile/packet-store.js +255 -0
  13. package/dist/context-compile/ref.d.ts +7 -0
  14. package/dist/context-compile/ref.js +102 -0
  15. package/dist/context-compile/source-readers.d.ts +69 -0
  16. package/dist/context-compile/source-readers.js +707 -0
  17. package/dist/context-compile/types.d.ts +138 -0
  18. package/dist/context-compile/types.js +11 -0
  19. package/dist/context-compile/visibility.d.ts +21 -0
  20. package/dist/context-compile/visibility.js +200 -0
  21. package/dist/db-adapter/node-sqlite-adapter.d.ts +3 -0
  22. package/dist/db-adapter/node-sqlite-adapter.js +76 -0
  23. package/dist/edges/ref-validation.js +55 -16
  24. package/dist/edges/types.d.ts +2 -0
  25. package/dist/embeddings.d.ts +3 -0
  26. package/dist/embeddings.js +37 -10
  27. package/dist/entities/store.d.ts +4 -1
  28. package/dist/entities/store.js +206 -78
  29. package/dist/entities/types.d.ts +5 -1
  30. package/dist/entities/types.js +1 -1
  31. package/dist/index.d.ts +1 -0
  32. package/dist/index.js +1 -0
  33. package/dist/memory/provenance.js +1 -0
  34. package/dist/memory/types.d.ts +1 -0
  35. package/package.json +3 -2
@@ -0,0 +1,32 @@
1
+ CREATE TABLE IF NOT EXISTS context_packets (
2
+ packet_id TEXT PRIMARY KEY,
3
+ task TEXT NOT NULL,
4
+ packet_json TEXT NOT NULL,
5
+ scope_json TEXT NOT NULL,
6
+ scope_hash TEXT NOT NULL,
7
+ envelope_hash TEXT NOT NULL,
8
+ model_run_id TEXT NOT NULL,
9
+ agent_id TEXT NOT NULL,
10
+ input_snapshot_ref TEXT NOT NULL,
11
+ source_refs_json TEXT NOT NULL DEFAULT '[]',
12
+ tenant_id TEXT NOT NULL DEFAULT 'default',
13
+ project_id TEXT NOT NULL,
14
+ memory_scope_kind TEXT NOT NULL CHECK (memory_scope_kind IN ('global', 'user', 'channel', 'project')),
15
+ memory_scope_id TEXT NOT NULL,
16
+ created_at INTEGER NOT NULL CHECK (created_at >= 0)
17
+ );
18
+
19
+ CREATE INDEX IF NOT EXISTS idx_context_packets_model_run
20
+ ON context_packets(model_run_id, created_at DESC);
21
+
22
+ CREATE INDEX IF NOT EXISTS idx_context_packets_envelope
23
+ ON context_packets(envelope_hash, created_at DESC);
24
+
25
+ CREATE INDEX IF NOT EXISTS idx_context_packets_scope
26
+ ON context_packets(tenant_id, project_id, memory_scope_kind, memory_scope_id, created_at DESC);
27
+
28
+ CREATE INDEX IF NOT EXISTS idx_context_packets_scope_hash
29
+ ON context_packets(scope_hash, created_at DESC);
30
+
31
+ INSERT OR IGNORE INTO schema_version (version, description)
32
+ VALUES (37, 'Create context packet append-only store');
@@ -153,7 +153,7 @@ function buildFtsQuery(query) {
153
153
  .match(/[\p{L}\p{N}_]+/gu)
154
154
  ?.filter((token) => token.length > 0);
155
155
  if (!tokens || tokens.length === 0) {
156
- return query;
156
+ return '';
157
157
  }
158
158
  return tokens.map((token) => `"${token.replace(/"/g, '""')}"`).join(' OR ');
159
159
  }
@@ -264,6 +264,10 @@ function ftsSearchWikiPages(adapter, query, limit) {
264
264
  if (!searchTableExists(adapter, 'wiki_pages_fts')) {
265
265
  return [];
266
266
  }
267
+ const ftsQuery = buildFtsQuery(query);
268
+ if (!ftsQuery) {
269
+ return [];
270
+ }
267
271
  const schema = readSchema(adapter);
268
272
  const rows = adapter
269
273
  .prepare(`
@@ -273,7 +277,7 @@ function ftsSearchWikiPages(adapter, query, limit) {
273
277
  ORDER BY raw
274
278
  LIMIT ?
275
279
  `)
276
- .all(buildFtsQuery(query), boundedLimit);
280
+ .all(ftsQuery, boundedLimit);
277
281
  const hits = [];
278
282
  for (const row of rows) {
279
283
  const record = selectByFtsPageId(adapter, schema, row.page_id);
@@ -0,0 +1,12 @@
1
+ import type { ContextBoundary, ContextProjectRef, ContextRange } from './types.js';
2
+ import type { MemoryScopeRef } from '../memory/types.js';
3
+ export type ContextBoundaryReadableInput = {
4
+ scopes?: MemoryScopeRef[];
5
+ connectors?: string[];
6
+ project_refs?: ContextProjectRef[];
7
+ tenant_id?: string | null;
8
+ range?: ContextRange;
9
+ as_of?: string | number | null;
10
+ };
11
+ export declare function applyContextBoundaryReadDefaults<T extends ContextBoundaryReadableInput>(input: T, boundary: ContextBoundary | undefined): T;
12
+ //# sourceMappingURL=boundary-defaults.d.ts.map
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyContextBoundaryReadDefaults = applyContextBoundaryReadDefaults;
4
+ function parseBoundaryTimeMs(value, field) {
5
+ if (value === null || value === undefined) {
6
+ return null;
7
+ }
8
+ if (typeof value === 'number' && Number.isFinite(value)) {
9
+ return Math.floor(value);
10
+ }
11
+ if (typeof value === 'string') {
12
+ const trimmed = value.trim();
13
+ if (trimmed.length === 0) {
14
+ throw new Error(`Invalid context boundary ${field}: ${String(value)}`);
15
+ }
16
+ const numeric = Number(trimmed);
17
+ if (Number.isFinite(numeric)) {
18
+ return Math.floor(numeric);
19
+ }
20
+ const parsed = Date.parse(trimmed);
21
+ if (Number.isFinite(parsed)) {
22
+ return parsed;
23
+ }
24
+ }
25
+ throw new Error(`Invalid context boundary ${field}: ${String(value)}`);
26
+ }
27
+ function rangeBoundaryMs(value, field) {
28
+ if (value === undefined) {
29
+ return null;
30
+ }
31
+ if (typeof value === 'number' && Number.isFinite(value)) {
32
+ return Math.floor(value);
33
+ }
34
+ throw new Error(`Invalid context boundary ${field}: ${String(value)}`);
35
+ }
36
+ function intersectRange(range, boundaryRange) {
37
+ if (!boundaryRange) {
38
+ return range;
39
+ }
40
+ const requestedStartMs = rangeBoundaryMs(range?.start_ms, 'range.start_ms');
41
+ const requestedEndMs = rangeBoundaryMs(range?.end_ms, 'range.end_ms');
42
+ const boundaryStartMs = rangeBoundaryMs(boundaryRange.start_ms, 'boundary.range.start_ms');
43
+ const boundaryEndMs = rangeBoundaryMs(boundaryRange.end_ms, 'boundary.range.end_ms');
44
+ const startMs = requestedStartMs === null
45
+ ? boundaryStartMs
46
+ : boundaryStartMs === null
47
+ ? requestedStartMs
48
+ : Math.max(requestedStartMs, boundaryStartMs);
49
+ const endMs = requestedEndMs === null
50
+ ? boundaryEndMs
51
+ : boundaryEndMs === null
52
+ ? requestedEndMs
53
+ : Math.min(requestedEndMs, boundaryEndMs);
54
+ if (startMs === null && endMs === null) {
55
+ return undefined;
56
+ }
57
+ if (startMs !== null && endMs !== null && startMs > endMs) {
58
+ throw new RangeError(`Context boundary range is empty after intersection: start_ms ${startMs} > end_ms ${endMs}`);
59
+ }
60
+ return {
61
+ ...(startMs !== null ? { start_ms: startMs } : {}),
62
+ ...(endMs !== null ? { end_ms: endMs } : {}),
63
+ };
64
+ }
65
+ function clampAsOf(requested, boundaryAsOf) {
66
+ const requestedMs = parseBoundaryTimeMs(requested, 'as_of');
67
+ const boundaryMs = parseBoundaryTimeMs(boundaryAsOf, 'boundary.as_of');
68
+ if (requestedMs === null) {
69
+ return boundaryAsOf ?? null;
70
+ }
71
+ if (boundaryMs === null) {
72
+ return requested ?? null;
73
+ }
74
+ return requestedMs <= boundaryMs ? (requested ?? null) : (boundaryAsOf ?? null);
75
+ }
76
+ function applyContextBoundaryReadDefaults(input, boundary) {
77
+ if (!boundary) {
78
+ return input;
79
+ }
80
+ const requestedTenantId = input.tenant_id;
81
+ const boundaryTenantId = boundary.tenant_id;
82
+ if (requestedTenantId !== undefined &&
83
+ requestedTenantId !== null &&
84
+ boundaryTenantId !== undefined &&
85
+ boundaryTenantId !== null &&
86
+ requestedTenantId !== boundaryTenantId) {
87
+ throw new Error('Requested tenant is outside the context boundary');
88
+ }
89
+ const scopedInput = {
90
+ ...input,
91
+ scopes: input.scopes === undefined ? boundary.scopes : input.scopes,
92
+ connectors: input.connectors === undefined ? boundary.connectors : input.connectors,
93
+ project_refs: input.project_refs === undefined ? boundary.project_refs : input.project_refs,
94
+ range: intersectRange(input.range, boundary.range),
95
+ as_of: clampAsOf(input.as_of, boundary.as_of),
96
+ };
97
+ if (boundaryTenantId !== undefined || requestedTenantId !== undefined) {
98
+ return {
99
+ ...scopedInput,
100
+ tenant_id: boundaryTenantId !== undefined ? boundaryTenantId : requestedTenantId,
101
+ };
102
+ }
103
+ return scopedInput;
104
+ }
105
+ //# sourceMappingURL=boundary-defaults.js.map
@@ -0,0 +1,39 @@
1
+ import type { ContextEvidence, ContextRef } from './types.js';
2
+ import type { ContextCandidate, HiddenCandidateAggregate } from './source-readers.js';
3
+ export interface ContextCompilerPolicyInput {
4
+ task: string;
5
+ candidates: ContextCandidate[];
6
+ hidden: HiddenCandidateAggregate;
7
+ limit?: number;
8
+ strictness?: 'recall' | 'balanced' | 'strict' | 'low' | 'medium' | 'high';
9
+ max_tokens?: number;
10
+ }
11
+ export interface ContextCompilerPolicyResult {
12
+ selected_evidence: ContextEvidence[];
13
+ source_refs: ContextRef[];
14
+ evidence_clusters: unknown[];
15
+ related_decisions: Array<{
16
+ memory_id: string;
17
+ title: string;
18
+ }>;
19
+ rejected_refs: ContextRef[];
20
+ rejected_summary: string[];
21
+ missing_context: string[];
22
+ caveats: string[];
23
+ retrieval_diagnostics: {
24
+ candidate_count: number;
25
+ selected_count: number;
26
+ rejected_count: number;
27
+ hidden: HiddenCandidateAggregate;
28
+ deduplicated_count: number;
29
+ strict_vector_only_rejected_count: number;
30
+ token_budget_rejected_count: number;
31
+ limit_rejected_count: number;
32
+ truncated_by_tokens: boolean;
33
+ estimated_tokens: number;
34
+ };
35
+ estimated_tokens: number;
36
+ }
37
+ export declare function estimateEvidenceTokens(evidence: Pick<ContextEvidence, 'title' | 'excerpt'>): number;
38
+ export declare function applyContextCompilerPolicy(input: ContextCompilerPolicyInput): ContextCompilerPolicyResult;
39
+ //# sourceMappingURL=compiler-policy.d.ts.map
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.estimateEvidenceTokens = estimateEvidenceTokens;
4
+ exports.applyContextCompilerPolicy = applyContextCompilerPolicy;
5
+ const ref_js_1 = require("./ref.js");
6
+ function normalizeLimit(limit) {
7
+ return Math.max(0, Math.min(100, Math.floor(limit ?? 10)));
8
+ }
9
+ function estimateEvidenceTokens(evidence) {
10
+ const text = `${evidence.title ?? ''}\n${evidence.excerpt ?? ''}`;
11
+ return Math.max(1, Math.ceil(text.length / 4));
12
+ }
13
+ function sortedCandidates(candidates) {
14
+ return [...candidates].sort((left, right) => {
15
+ const scoreDiff = right.score - left.score;
16
+ if (scoreDiff !== 0) {
17
+ return scoreDiff;
18
+ }
19
+ return (right.timestamp_ms ?? 0) - (left.timestamp_ms ?? 0);
20
+ });
21
+ }
22
+ function shouldRejectVectorOnly(candidate, strictness) {
23
+ return (strictness === 'strict' &&
24
+ candidate.support.is_vector_only === true &&
25
+ candidate.support.confirmation_signals.length === 0);
26
+ }
27
+ function evidenceFromCandidate(candidate) {
28
+ return {
29
+ ref: candidate.ref,
30
+ title: candidate.title,
31
+ excerpt: candidate.excerpt,
32
+ score: candidate.score,
33
+ reasons: [
34
+ ...(candidate.support.confirmation_signals ?? []),
35
+ ...(candidate.support.graph_expanded ? ['graph_expanded'] : []),
36
+ ],
37
+ ...(candidate.retrieval_diagnostics
38
+ ? { retrieval_diagnostics: candidate.retrieval_diagnostics }
39
+ : {}),
40
+ };
41
+ }
42
+ function addCountSummary(parts, label, count) {
43
+ if (count > 0) {
44
+ parts.push(`${label}: ${count}`);
45
+ }
46
+ }
47
+ function normalizeStrictness(strictness) {
48
+ switch (strictness) {
49
+ case 'low':
50
+ case 'recall':
51
+ return 'recall';
52
+ case 'high':
53
+ case 'strict':
54
+ return 'strict';
55
+ case 'medium':
56
+ case 'balanced':
57
+ case undefined:
58
+ return 'balanced';
59
+ }
60
+ return 'balanced';
61
+ }
62
+ function applyContextCompilerPolicy(input) {
63
+ const limit = normalizeLimit(input.limit);
64
+ const strictness = normalizeStrictness(input.strictness);
65
+ const maxTokens = typeof input.max_tokens === 'number' && Number.isFinite(input.max_tokens)
66
+ ? Math.max(0, Math.floor(input.max_tokens))
67
+ : null;
68
+ const seen = new Set();
69
+ const selected = [];
70
+ const sourceRefs = [];
71
+ const rejectedRefs = [];
72
+ const counters = {
73
+ deduplicated: 0,
74
+ strictVectorOnly: 0,
75
+ tokenBudget: 0,
76
+ limit: 0,
77
+ };
78
+ let estimatedTokens = 0;
79
+ for (const candidate of sortedCandidates(input.candidates)) {
80
+ if (!candidate.visible) {
81
+ continue;
82
+ }
83
+ const key = (0, ref_js_1.serializeContextRefForProvenance)(candidate.ref);
84
+ if (seen.has(key)) {
85
+ counters.deduplicated += 1;
86
+ rejectedRefs.push(candidate.ref);
87
+ continue;
88
+ }
89
+ if (shouldRejectVectorOnly(candidate, strictness)) {
90
+ counters.strictVectorOnly += 1;
91
+ rejectedRefs.push(candidate.ref);
92
+ continue;
93
+ }
94
+ if (selected.length >= limit) {
95
+ counters.limit += 1;
96
+ rejectedRefs.push(candidate.ref);
97
+ continue;
98
+ }
99
+ const evidence = evidenceFromCandidate(candidate);
100
+ const nextTokens = estimateEvidenceTokens(evidence);
101
+ if (maxTokens !== null && estimatedTokens + nextTokens > maxTokens) {
102
+ counters.tokenBudget += 1;
103
+ rejectedRefs.push(candidate.ref);
104
+ continue;
105
+ }
106
+ selected.push(evidence);
107
+ sourceRefs.push(candidate.ref);
108
+ seen.add(key);
109
+ estimatedTokens += nextTokens;
110
+ }
111
+ const rejectedSummary = [];
112
+ addCountSummary(rejectedSummary, 'deduplicated duplicate candidates', counters.deduplicated);
113
+ addCountSummary(rejectedSummary, 'strictness rejected vector-only candidates', counters.strictVectorOnly);
114
+ addCountSummary(rejectedSummary, 'token budget rejected candidates', counters.tokenBudget);
115
+ addCountSummary(rejectedSummary, 'limit rejected candidates', counters.limit);
116
+ const caveats = [];
117
+ if (input.hidden.total > 0) {
118
+ caveats.push(`hidden candidates omitted: ${input.hidden.total}`);
119
+ }
120
+ const relatedDecisions = selected.flatMap((evidence) => evidence.ref.kind === 'memory'
121
+ ? [{ memory_id: evidence.ref.id, title: evidence.title ?? evidence.ref.id }]
122
+ : []);
123
+ return {
124
+ selected_evidence: selected,
125
+ source_refs: sourceRefs,
126
+ evidence_clusters: [],
127
+ related_decisions: relatedDecisions,
128
+ rejected_refs: rejectedRefs,
129
+ rejected_summary: rejectedSummary,
130
+ missing_context: selected.length === 0 ? [`No visible evidence selected for task: ${input.task}`] : [],
131
+ caveats,
132
+ retrieval_diagnostics: {
133
+ candidate_count: input.candidates.length,
134
+ selected_count: selected.length,
135
+ rejected_count: rejectedRefs.length,
136
+ hidden: input.hidden,
137
+ deduplicated_count: counters.deduplicated,
138
+ strict_vector_only_rejected_count: counters.strictVectorOnly,
139
+ token_budget_rejected_count: counters.tokenBudget,
140
+ limit_rejected_count: counters.limit,
141
+ truncated_by_tokens: counters.tokenBudget > 0,
142
+ estimated_tokens: estimatedTokens,
143
+ },
144
+ estimated_tokens: estimatedTokens,
145
+ };
146
+ }
147
+ //# sourceMappingURL=compiler-policy.js.map
@@ -0,0 +1,18 @@
1
+ import type { DatabaseAdapter } from '../db-manager.js';
2
+ import type { ContextBoundary, ContextCompileInput, ContextPacket, ContextRef } from './types.js';
3
+ import { type ContextSourceReadInput, type ContextSourceReadResult } from './source-readers.js';
4
+ type ContextCompilerAdapter = Pick<DatabaseAdapter, 'prepare'>;
5
+ export interface ContextCompilerDeps {
6
+ adapter?: ContextCompilerAdapter;
7
+ boundary?: ContextBoundary;
8
+ signal?: AbortSignal;
9
+ deadlineMs?: number;
10
+ now?: () => number;
11
+ packetId?: () => string;
12
+ readMemoryCandidates?: (input: ContextSourceReadInput) => Promise<ContextSourceReadResult> | ContextSourceReadResult;
13
+ readRawCandidates?: (input: ContextSourceReadInput) => Promise<ContextSourceReadResult> | ContextSourceReadResult;
14
+ readGraphCandidates?: (input: ContextSourceReadInput, visibleRefs: readonly ContextRef[]) => Promise<ContextSourceReadResult> | ContextSourceReadResult;
15
+ }
16
+ export declare function compileContext(input: ContextCompileInput, deps?: ContextCompilerDeps): Promise<ContextPacket>;
17
+ export {};
18
+ //# sourceMappingURL=compiler.d.ts.map