@lumenflow/memory 2.9.0 → 2.11.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.
- package/dist/index.js +1 -0
- package/dist/mem-recover-core.js +258 -0
- package/package.json +2 -2
- package/dist/index.d.ts +0 -17
- package/dist/mem-checkpoint-core.d.ts +0 -91
- package/dist/mem-cleanup-core.d.ts +0 -202
- package/dist/mem-create-core.d.ts +0 -93
- package/dist/mem-export-core.d.ts +0 -33
- package/dist/mem-id.d.ts +0 -91
- package/dist/mem-init-core.d.ts +0 -93
- package/dist/mem-ready-core.d.ts +0 -56
- package/dist/mem-signal-core.d.ts +0 -132
- package/dist/mem-start-core.d.ts +0 -76
- package/dist/mem-summarize-core.d.ts +0 -105
- package/dist/mem-triage-core.d.ts +0 -127
- package/dist/memory-schema.d.ts +0 -150
- package/dist/memory-store.d.ts +0 -108
- package/dist/paths.d.ts +0 -28
package/dist/index.js
CHANGED
|
@@ -19,6 +19,7 @@ export * from './mem-index-core.js';
|
|
|
19
19
|
export * from './mem-promote-core.js';
|
|
20
20
|
export * from './mem-profile-core.js';
|
|
21
21
|
export * from './mem-delete-core.js';
|
|
22
|
+
export * from './mem-recover-core.js';
|
|
22
23
|
export * from './memory-schema.js';
|
|
23
24
|
export * from './memory-store.js';
|
|
24
25
|
// WU-1238: Decay scoring and access tracking
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Recovery Core (WU-1390)
|
|
3
|
+
*
|
|
4
|
+
* Generates post-compaction recovery context for agents that have lost
|
|
5
|
+
* their LumenFlow instructions due to context compaction.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Loads last checkpoint from memory layer
|
|
9
|
+
* - Extracts compact constraints from .lumenflow/constraints.md
|
|
10
|
+
* - Provides essential CLI commands reference
|
|
11
|
+
* - Size-limited output (default 2KB) to prevent truncation
|
|
12
|
+
* - Vendor-agnostic (works for any client)
|
|
13
|
+
*
|
|
14
|
+
* @see {@link packages/@lumenflow/cli/src/mem-recover.ts} - CLI wrapper
|
|
15
|
+
* @see {@link packages/@lumenflow/memory/__tests__/mem-recover-core.test.ts} - Tests
|
|
16
|
+
*/
|
|
17
|
+
import fs from 'node:fs/promises';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { loadMemory } from './memory-store.js';
|
|
20
|
+
import { MEMORY_PATTERNS } from './memory-schema.js';
|
|
21
|
+
import { LUMENFLOW_MEMORY_PATHS } from './paths.js';
|
|
22
|
+
/**
|
|
23
|
+
* Default maximum recovery context size in bytes (2KB)
|
|
24
|
+
* Smaller than spawn context to ensure it doesn't get truncated
|
|
25
|
+
*/
|
|
26
|
+
const DEFAULT_MAX_SIZE = 2048;
|
|
27
|
+
/**
|
|
28
|
+
* Node type constant for checkpoint filtering
|
|
29
|
+
* @see {@link MEMORY_NODE_TYPES} in memory-schema.ts
|
|
30
|
+
*/
|
|
31
|
+
const NODE_TYPE_CHECKPOINT = 'checkpoint';
|
|
32
|
+
/**
|
|
33
|
+
* Constraints file name within .lumenflow directory
|
|
34
|
+
*/
|
|
35
|
+
const CONSTRAINTS_FILENAME = 'constraints.md';
|
|
36
|
+
/**
|
|
37
|
+
* Default value for unknown timestamps
|
|
38
|
+
*/
|
|
39
|
+
const TIMESTAMP_UNKNOWN = 'unknown';
|
|
40
|
+
/**
|
|
41
|
+
* Error messages for validation
|
|
42
|
+
*/
|
|
43
|
+
const ERROR_MESSAGES = {
|
|
44
|
+
WU_ID_REQUIRED: 'wuId is required',
|
|
45
|
+
WU_ID_EMPTY: 'wuId cannot be empty',
|
|
46
|
+
WU_ID_INVALID: 'Invalid WU ID format. Expected pattern: WU-XXX (e.g., WU-1234)',
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Section headers for recovery context formatting
|
|
50
|
+
*/
|
|
51
|
+
const SECTION_HEADERS = {
|
|
52
|
+
RECOVERY_TITLE: 'POST-COMPACTION RECOVERY',
|
|
53
|
+
LAST_CHECKPOINT: 'Last Checkpoint',
|
|
54
|
+
CRITICAL_RULES: 'Critical Rules (DO NOT FORGET)',
|
|
55
|
+
CLI_COMMANDS: 'CLI Commands',
|
|
56
|
+
NEXT_ACTION: 'Next Action',
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Essential CLI commands for recovery (hardcoded, ~400 bytes)
|
|
60
|
+
*/
|
|
61
|
+
const ESSENTIAL_CLI_COMMANDS = `| Command | Purpose |
|
|
62
|
+
|---------|---------|
|
|
63
|
+
| pnpm wu:status --id WU-XXX | Check WU status and location |
|
|
64
|
+
| pnpm wu:spawn --id WU-XXX | Generate fresh agent spawn prompt |
|
|
65
|
+
| pnpm gates | Run quality gates before completion |
|
|
66
|
+
| pnpm mem:checkpoint | Save progress checkpoint |`;
|
|
67
|
+
/**
|
|
68
|
+
* Compact constraints (hardcoded fallback, ~800 bytes)
|
|
69
|
+
* Used when constraints.md cannot be loaded
|
|
70
|
+
*/
|
|
71
|
+
const FALLBACK_CONSTRAINTS = `1. **Worktree Discipline**: Work only in worktrees, treat main as read-only
|
|
72
|
+
2. **WUs Are Specs**: Respect code_paths boundaries, no feature creep
|
|
73
|
+
3. **Docs-Only vs Code**: Documentation WUs use --docs-only gates
|
|
74
|
+
4. **LLM-First Inference**: Use LLMs for semantic tasks, no brittle regex
|
|
75
|
+
5. **Gates Required**: Run pnpm gates before wu:done
|
|
76
|
+
6. **Safety & Governance**: No secrets in code, stop-and-ask for sensitive ops
|
|
77
|
+
7. **Test Ratchet**: NEW test failures block, pre-existing show warning only`;
|
|
78
|
+
/**
|
|
79
|
+
* Validates WU ID format
|
|
80
|
+
*/
|
|
81
|
+
function validateWuId(wuId) {
|
|
82
|
+
if (wuId === undefined || wuId === null) {
|
|
83
|
+
throw new Error(ERROR_MESSAGES.WU_ID_REQUIRED);
|
|
84
|
+
}
|
|
85
|
+
if (wuId === '') {
|
|
86
|
+
throw new Error(ERROR_MESSAGES.WU_ID_EMPTY);
|
|
87
|
+
}
|
|
88
|
+
if (!MEMORY_PATTERNS.WU_ID.test(wuId)) {
|
|
89
|
+
throw new Error(ERROR_MESSAGES.WU_ID_INVALID);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Gets the most recent checkpoint for a WU from memory
|
|
94
|
+
*/
|
|
95
|
+
async function getLastCheckpoint(baseDir, wuId) {
|
|
96
|
+
const memoryDir = path.join(baseDir, LUMENFLOW_MEMORY_PATHS.MEMORY_DIR);
|
|
97
|
+
try {
|
|
98
|
+
const memory = await loadMemory(memoryDir);
|
|
99
|
+
const wuNodes = memory.byWu.get(wuId) ?? [];
|
|
100
|
+
// Filter to checkpoint nodes and sort by timestamp (most recent first)
|
|
101
|
+
const checkpoints = wuNodes
|
|
102
|
+
.filter((node) => node.type === NODE_TYPE_CHECKPOINT)
|
|
103
|
+
.sort((a, b) => {
|
|
104
|
+
const aTime = new Date(a.created_at).getTime();
|
|
105
|
+
const bTime = new Date(b.created_at).getTime();
|
|
106
|
+
return bTime - aTime; // Most recent first
|
|
107
|
+
});
|
|
108
|
+
if (checkpoints.length === 0) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const latest = checkpoints[0];
|
|
112
|
+
return {
|
|
113
|
+
content: latest.content,
|
|
114
|
+
timestamp: latest.created_at,
|
|
115
|
+
progress: latest.metadata?.progress,
|
|
116
|
+
nextSteps: latest.metadata?.nextSteps,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// Memory layer not initialized or error loading
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Loads compact constraints from .lumenflow/constraints.md
|
|
126
|
+
* Extracts just the rule summary from each of the 7 sections
|
|
127
|
+
*/
|
|
128
|
+
async function loadCompactConstraints(baseDir) {
|
|
129
|
+
const constraintsPath = path.join(baseDir, LUMENFLOW_MEMORY_PATHS.BASE, CONSTRAINTS_FILENAME);
|
|
130
|
+
try {
|
|
131
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
132
|
+
const content = await fs.readFile(constraintsPath, 'utf-8');
|
|
133
|
+
// Extract section headers and their Rule lines
|
|
134
|
+
const rules = [];
|
|
135
|
+
const sectionPattern = /### (\d+)\. ([^\n]+)\n\n\*\*Rule:\*\* ([^\n]+)/g;
|
|
136
|
+
let match;
|
|
137
|
+
while ((match = sectionPattern.exec(content)) !== null) {
|
|
138
|
+
const [, num, title, rule] = match;
|
|
139
|
+
rules.push(`${num}. **${title}**: ${rule}`);
|
|
140
|
+
}
|
|
141
|
+
if (rules.length > 0) {
|
|
142
|
+
return rules.join('\n');
|
|
143
|
+
}
|
|
144
|
+
// Fallback if parsing failed
|
|
145
|
+
return FALLBACK_CONSTRAINTS;
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// File not found or read error - use fallback
|
|
149
|
+
return FALLBACK_CONSTRAINTS;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Formats the recovery prompt with size budget
|
|
154
|
+
*/
|
|
155
|
+
function formatRecoveryPrompt(wuId, checkpoint, constraints, cliRef, maxSize) {
|
|
156
|
+
const header = `# ${SECTION_HEADERS.RECOVERY_TITLE}
|
|
157
|
+
|
|
158
|
+
You are resuming work after context compaction. Your previous context was lost.
|
|
159
|
+
**WU:** ${wuId}
|
|
160
|
+
|
|
161
|
+
`;
|
|
162
|
+
const checkpointSection = checkpoint
|
|
163
|
+
? `## ${SECTION_HEADERS.LAST_CHECKPOINT}
|
|
164
|
+
- **Progress:** ${checkpoint.content}
|
|
165
|
+
- **Timestamp:** ${checkpoint.timestamp}
|
|
166
|
+
${checkpoint.nextSteps ? `- **Next Steps:** ${checkpoint.nextSteps}` : ''}
|
|
167
|
+
|
|
168
|
+
`
|
|
169
|
+
: `## ${SECTION_HEADERS.LAST_CHECKPOINT}
|
|
170
|
+
No checkpoint found for this WU.
|
|
171
|
+
|
|
172
|
+
`;
|
|
173
|
+
// Only include sections if they have content
|
|
174
|
+
const constraintsSection = constraints
|
|
175
|
+
? `## ${SECTION_HEADERS.CRITICAL_RULES}
|
|
176
|
+
${constraints}
|
|
177
|
+
|
|
178
|
+
`
|
|
179
|
+
: '';
|
|
180
|
+
const cliSection = cliRef
|
|
181
|
+
? `## ${SECTION_HEADERS.CLI_COMMANDS}
|
|
182
|
+
${cliRef}
|
|
183
|
+
|
|
184
|
+
`
|
|
185
|
+
: '';
|
|
186
|
+
const footer = `## ${SECTION_HEADERS.NEXT_ACTION}
|
|
187
|
+
Run \`pnpm wu:spawn --id ${wuId}\` to spawn a fresh agent with full context.
|
|
188
|
+
`;
|
|
189
|
+
// Build sections in priority order
|
|
190
|
+
// Priority: header > checkpoint > constraints > CLI > footer
|
|
191
|
+
// If over budget, truncate checkpoint content first
|
|
192
|
+
const fixedContent = header + constraintsSection + cliSection + footer;
|
|
193
|
+
const fixedSize = Buffer.byteLength(fixedContent, 'utf-8');
|
|
194
|
+
if (fixedSize > maxSize) {
|
|
195
|
+
// Even fixed content exceeds budget - return minimal recovery
|
|
196
|
+
const minimal = `# ${SECTION_HEADERS.RECOVERY_TITLE}
|
|
197
|
+
WU: ${wuId}
|
|
198
|
+
Run: pnpm wu:spawn --id ${wuId}
|
|
199
|
+
`;
|
|
200
|
+
return { context: minimal, truncated: true };
|
|
201
|
+
}
|
|
202
|
+
const remainingBudget = maxSize - fixedSize;
|
|
203
|
+
const checkpointSize = Buffer.byteLength(checkpointSection, 'utf-8');
|
|
204
|
+
if (checkpointSize <= remainingBudget) {
|
|
205
|
+
// Full checkpoint fits
|
|
206
|
+
const fullContext = header + checkpointSection + constraintsSection + cliSection + footer;
|
|
207
|
+
return { context: fullContext, truncated: false };
|
|
208
|
+
}
|
|
209
|
+
// Truncate checkpoint to fit
|
|
210
|
+
const truncatedCheckpoint = `## ${SECTION_HEADERS.LAST_CHECKPOINT}
|
|
211
|
+
- **Progress:** (truncated - run mem:ready --wu ${wuId} for details)
|
|
212
|
+
- **Timestamp:** ${checkpoint?.timestamp ?? TIMESTAMP_UNKNOWN}
|
|
213
|
+
|
|
214
|
+
`;
|
|
215
|
+
const context = header + truncatedCheckpoint + constraintsSection + cliSection + footer;
|
|
216
|
+
return { context, truncated: true };
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Generates post-compaction recovery context for an agent.
|
|
220
|
+
*
|
|
221
|
+
* The recovery context includes:
|
|
222
|
+
* - Last checkpoint for the WU (from memory layer)
|
|
223
|
+
* - Compact constraints (7 rules from constraints.md)
|
|
224
|
+
* - Essential CLI commands reference
|
|
225
|
+
* - Guidance to spawn fresh agent
|
|
226
|
+
*
|
|
227
|
+
* Size is limited to prevent the recovery context itself from being
|
|
228
|
+
* truncated or compacted.
|
|
229
|
+
*
|
|
230
|
+
* @param options - Recovery options
|
|
231
|
+
* @returns Recovery context result
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* const result = await generateRecoveryContext({
|
|
235
|
+
* wuId: 'WU-1390',
|
|
236
|
+
* maxSize: 2048,
|
|
237
|
+
* });
|
|
238
|
+
* console.log(result.context);
|
|
239
|
+
*/
|
|
240
|
+
export async function generateRecoveryContext(options) {
|
|
241
|
+
const { wuId, baseDir = '.', maxSize = DEFAULT_MAX_SIZE, includeConstraints = true, includeCLIRef = true, } = options;
|
|
242
|
+
// Validate WU ID
|
|
243
|
+
validateWuId(wuId);
|
|
244
|
+
// Load checkpoint from memory
|
|
245
|
+
const checkpoint = await getLastCheckpoint(baseDir, wuId);
|
|
246
|
+
// Load constraints (or use empty if disabled)
|
|
247
|
+
const constraints = includeConstraints ? await loadCompactConstraints(baseDir) : '';
|
|
248
|
+
// Get CLI reference (or empty if disabled)
|
|
249
|
+
const cliRef = includeCLIRef ? ESSENTIAL_CLI_COMMANDS : '';
|
|
250
|
+
// Format with size budget
|
|
251
|
+
const { context, truncated } = formatRecoveryPrompt(wuId, checkpoint, constraints, cliRef, maxSize);
|
|
252
|
+
return {
|
|
253
|
+
success: true,
|
|
254
|
+
context,
|
|
255
|
+
size: Buffer.byteLength(context, 'utf-8'),
|
|
256
|
+
truncated,
|
|
257
|
+
};
|
|
258
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumenflow/memory",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.11.0",
|
|
4
4
|
"description": "Memory layer for LumenFlow workflow framework - session tracking, context recovery, and agent coordination",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lumenflow",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"ms": "^2.1.3",
|
|
50
50
|
"yaml": "^2.8.2",
|
|
51
51
|
"zod": "^4.3.5",
|
|
52
|
-
"@lumenflow/core": "2.
|
|
52
|
+
"@lumenflow/core": "2.11.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@vitest/coverage-v8": "^4.0.17",
|
package/dist/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @lumenflow/memory - Session tracking and context recovery
|
|
3
|
-
* @module @lumenflow/memory
|
|
4
|
-
*/
|
|
5
|
-
export * from './mem-checkpoint-core.js';
|
|
6
|
-
export * from './mem-cleanup-core.js';
|
|
7
|
-
export * from './mem-create-core.js';
|
|
8
|
-
export * from './mem-export-core.js';
|
|
9
|
-
export * from './mem-id.js';
|
|
10
|
-
export * from './mem-init-core.js';
|
|
11
|
-
export * from './mem-ready-core.js';
|
|
12
|
-
export * from './mem-signal-core.js';
|
|
13
|
-
export * from './mem-start-core.js';
|
|
14
|
-
export { filterSummarizableNodes, summarizeWu, getCompactionRatio as getSummarizeCompactionRatio, } from './mem-summarize-core.js';
|
|
15
|
-
export * from './mem-triage-core.js';
|
|
16
|
-
export * from './memory-schema.js';
|
|
17
|
-
export * from './memory-store.js';
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Checkpoint Core (WU-1467, WU-1748)
|
|
3
|
-
*
|
|
4
|
-
* Core logic for creating checkpoint nodes for context snapshots.
|
|
5
|
-
* Used before /clear or session handoff to preserve progress state.
|
|
6
|
-
*
|
|
7
|
-
* Features:
|
|
8
|
-
* - Creates checkpoint nodes with progress summary and next steps
|
|
9
|
-
* - Optional linking to sessions and WUs
|
|
10
|
-
* - Supports handoff trigger detection
|
|
11
|
-
* - Auto-initializes memory layer if not present
|
|
12
|
-
* - WU-1748: Persists to wu-events.jsonl for cross-agent visibility
|
|
13
|
-
*
|
|
14
|
-
* @see {@link tools/mem-checkpoint.mjs} - CLI wrapper
|
|
15
|
-
* @see {@link tools/__tests__/mem-checkpoint.test.mjs} - Tests
|
|
16
|
-
* @see {@link tools/lib/memory-schema.mjs} - Schema definitions
|
|
17
|
-
*/
|
|
18
|
-
/**
|
|
19
|
-
* Checkpoint creation options
|
|
20
|
-
*/
|
|
21
|
-
export interface CreateCheckpointOptions {
|
|
22
|
-
/** Checkpoint note/description (required) */
|
|
23
|
-
note: string;
|
|
24
|
-
/** Session ID to link checkpoint to */
|
|
25
|
-
sessionId?: string;
|
|
26
|
-
/** Work Unit ID to link checkpoint to */
|
|
27
|
-
wuId?: string;
|
|
28
|
-
/** Progress summary */
|
|
29
|
-
progress?: string;
|
|
30
|
-
/** Next steps description */
|
|
31
|
-
nextSteps?: string;
|
|
32
|
-
/** Handoff trigger (e.g., 'clear', 'handoff') */
|
|
33
|
-
trigger?: string;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Checkpoint metadata stored in the node
|
|
37
|
-
*/
|
|
38
|
-
interface CheckpointMetadata {
|
|
39
|
-
progress?: string;
|
|
40
|
-
nextSteps?: string;
|
|
41
|
-
trigger?: string;
|
|
42
|
-
[key: string]: unknown;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Memory node structure for checkpoints
|
|
46
|
-
*/
|
|
47
|
-
interface CheckpointNode {
|
|
48
|
-
id: string;
|
|
49
|
-
type: 'checkpoint';
|
|
50
|
-
lifecycle: 'session';
|
|
51
|
-
content: string;
|
|
52
|
-
created_at: string;
|
|
53
|
-
wu_id?: string;
|
|
54
|
-
session_id?: string;
|
|
55
|
-
metadata?: CheckpointMetadata;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Checkpoint creation result
|
|
59
|
-
*/
|
|
60
|
-
export interface CreateCheckpointResult {
|
|
61
|
-
/** Whether the operation succeeded */
|
|
62
|
-
success: boolean;
|
|
63
|
-
/** Created checkpoint node */
|
|
64
|
-
checkpoint: CheckpointNode;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Creates a new checkpoint node for context preservation.
|
|
68
|
-
*
|
|
69
|
-
* Creates a checkpoint-type memory node with:
|
|
70
|
-
* - Unique ID (mem-XXXX format)
|
|
71
|
-
* - User-provided note in content
|
|
72
|
-
* - Optional session and WU linking
|
|
73
|
-
* - Progress summary and next steps in metadata
|
|
74
|
-
*
|
|
75
|
-
* @param baseDir - Base directory containing .lumenflow/memory/
|
|
76
|
-
* @param options - Checkpoint options
|
|
77
|
-
* @returns Result with created checkpoint node
|
|
78
|
-
* @throws If note is missing or WU ID is invalid
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* const result = await createCheckpoint(baseDir, {
|
|
82
|
-
* note: 'Before /clear - completed TDD tests',
|
|
83
|
-
* sessionId: 'session-uuid',
|
|
84
|
-
* wuId: 'WU-1467',
|
|
85
|
-
* progress: 'TDD tests passing, core module implemented',
|
|
86
|
-
* nextSteps: 'Implement CLI wrapper, add package.json script',
|
|
87
|
-
* });
|
|
88
|
-
* console.log(result.checkpoint.id); // 'mem-a1b2'
|
|
89
|
-
*/
|
|
90
|
-
export declare function createCheckpoint(baseDir: string, options: CreateCheckpointOptions): Promise<CreateCheckpointResult>;
|
|
91
|
-
export {};
|
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Cleanup Core (WU-1472, WU-1554)
|
|
3
|
-
*
|
|
4
|
-
* Prune closed memory nodes based on lifecycle policy.
|
|
5
|
-
* Implements compaction to prevent memory bloat.
|
|
6
|
-
*
|
|
7
|
-
* Features:
|
|
8
|
-
* - Remove ephemeral nodes (always discarded)
|
|
9
|
-
* - Remove session nodes when session is closed
|
|
10
|
-
* - Archive summarized nodes (marked with summarized_into)
|
|
11
|
-
* - Respect sensitive:true flag for stricter retention
|
|
12
|
-
* - Support dry-run mode for preview
|
|
13
|
-
* - Report compaction metrics (ratio, bytes freed)
|
|
14
|
-
* - WU-1554: TTL-based expiration for old nodes
|
|
15
|
-
* - WU-1554: Active session protection regardless of age
|
|
16
|
-
*
|
|
17
|
-
* Lifecycle Policy:
|
|
18
|
-
* - ephemeral: Always removed (scratch pad data)
|
|
19
|
-
* - session: Removed when session is closed
|
|
20
|
-
* - wu: Removed when marked with summarized_into (after WU completion)
|
|
21
|
-
* - project: Never removed (architectural knowledge)
|
|
22
|
-
*
|
|
23
|
-
* TTL Policy (WU-1554):
|
|
24
|
-
* - Nodes older than TTL are removed regardless of lifecycle
|
|
25
|
-
* - Active sessions (status: 'active') are never removed
|
|
26
|
-
* - Project and sensitive nodes are protected from TTL removal
|
|
27
|
-
*
|
|
28
|
-
* @see {@link tools/mem-cleanup.mjs} - CLI wrapper
|
|
29
|
-
* @see {@link tools/__tests__/mem-cleanup.test.mjs} - Tests
|
|
30
|
-
*/
|
|
31
|
-
import type { MemoryNode } from './memory-schema.js';
|
|
32
|
-
/**
|
|
33
|
-
* Lifecycle policy definition
|
|
34
|
-
*/
|
|
35
|
-
interface LifecyclePolicyEntry {
|
|
36
|
-
alwaysRemove: boolean;
|
|
37
|
-
requiresSummarized: boolean;
|
|
38
|
-
protected?: boolean;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Lifecycle policy type for indexed access
|
|
42
|
-
*/
|
|
43
|
-
type LifecycleType = 'ephemeral' | 'session' | 'wu' | 'project';
|
|
44
|
-
/**
|
|
45
|
-
* Lifecycle policy definitions
|
|
46
|
-
*
|
|
47
|
-
* Determines which nodes are eligible for cleanup based on lifecycle.
|
|
48
|
-
*/
|
|
49
|
-
export declare const LIFECYCLE_POLICY: Record<LifecycleType, LifecyclePolicyEntry>;
|
|
50
|
-
/**
|
|
51
|
-
* Metadata flag that indicates sensitive data requiring stricter retention
|
|
52
|
-
*/
|
|
53
|
-
export declare const SENSITIVE_FLAG = "sensitive";
|
|
54
|
-
/**
|
|
55
|
-
* Cleanup options for memory pruning
|
|
56
|
-
*/
|
|
57
|
-
export interface CleanupOptions {
|
|
58
|
-
/** If true, preview without modifications */
|
|
59
|
-
dryRun?: boolean;
|
|
60
|
-
/** Session ID to consider closed (removes session lifecycle nodes) */
|
|
61
|
-
sessionId?: string;
|
|
62
|
-
/** TTL duration string (e.g., '30d', '7d', '24h') for age-based cleanup */
|
|
63
|
-
ttl?: string;
|
|
64
|
-
/** TTL in milliseconds (alternative to ttl string) */
|
|
65
|
-
ttlMs?: number;
|
|
66
|
-
/** Current timestamp for testing (defaults to Date.now()) */
|
|
67
|
-
now?: number;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Breakdown of cleanup by lifecycle type
|
|
71
|
-
*/
|
|
72
|
-
interface CleanupBreakdown {
|
|
73
|
-
ephemeral: number;
|
|
74
|
-
session: number;
|
|
75
|
-
wu: number;
|
|
76
|
-
sensitive: number;
|
|
77
|
-
ttlExpired: number;
|
|
78
|
-
activeSessionProtected: number;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Result of cleanup operation
|
|
82
|
-
*/
|
|
83
|
-
export interface CleanupResult {
|
|
84
|
-
/** Whether cleanup succeeded */
|
|
85
|
-
success: boolean;
|
|
86
|
-
/** IDs of removed nodes */
|
|
87
|
-
removedIds: string[];
|
|
88
|
-
/** IDs of retained nodes */
|
|
89
|
-
retainedIds: string[];
|
|
90
|
-
/** Approximate bytes freed (0 if dry-run) */
|
|
91
|
-
bytesFreed: number;
|
|
92
|
-
/** Ratio of removed to total nodes */
|
|
93
|
-
compactionRatio: number;
|
|
94
|
-
/** True if in dry-run mode */
|
|
95
|
-
dryRun?: boolean;
|
|
96
|
-
/** TTL in milliseconds if TTL was provided */
|
|
97
|
-
ttlMs?: number;
|
|
98
|
-
/** Breakdown by lifecycle */
|
|
99
|
-
breakdown: CleanupBreakdown;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Parse a TTL duration string into milliseconds (WU-1554).
|
|
103
|
-
*
|
|
104
|
-
* Uses the `ms` package to parse human-readable duration strings.
|
|
105
|
-
*
|
|
106
|
-
* @param ttlString - TTL string (e.g., '30d', '7d', '24h', '60m')
|
|
107
|
-
* @returns TTL in milliseconds
|
|
108
|
-
* @throws If TTL format is invalid
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* parseTtl('30d'); // 2592000000 (30 days in ms)
|
|
112
|
-
* parseTtl('7d'); // 604800000 (7 days in ms)
|
|
113
|
-
* parseTtl('24h'); // 86400000 (24 hours in ms)
|
|
114
|
-
*/
|
|
115
|
-
export declare function parseTtl(ttlString: string): number;
|
|
116
|
-
/**
|
|
117
|
-
* Check if a node has expired based on TTL (WU-1554).
|
|
118
|
-
*
|
|
119
|
-
* @param node - Memory node to check
|
|
120
|
-
* @param ttlMs - TTL in milliseconds
|
|
121
|
-
* @param now - Current timestamp (defaults to Date.now())
|
|
122
|
-
* @returns True if node is older than TTL
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* // Check if node is older than 30 days
|
|
126
|
-
* const expired = isNodeExpired(node, 30 * 24 * 60 * 60 * 1000);
|
|
127
|
-
*/
|
|
128
|
-
export declare function isNodeExpired(node: MemoryNode, ttlMs: number, now?: number): boolean;
|
|
129
|
-
/**
|
|
130
|
-
* Check if a node should be removed based on lifecycle policy and TTL.
|
|
131
|
-
*
|
|
132
|
-
* Policy rules (checked in order):
|
|
133
|
-
* 1. Active sessions are always retained (WU-1554)
|
|
134
|
-
* 2. Sensitive nodes are always retained
|
|
135
|
-
* 3. Protected lifecycle (project) nodes are never removed
|
|
136
|
-
* 4. TTL expiration removes old nodes (WU-1554)
|
|
137
|
-
* 5. Ephemeral nodes are always removed
|
|
138
|
-
* 6. Session nodes are removed when their session is closed
|
|
139
|
-
* 7. WU nodes are removed only when marked with summarized_into
|
|
140
|
-
*
|
|
141
|
-
* @param {MemoryNode} node - Memory node to check
|
|
142
|
-
* @param {CleanupOptions} options - Cleanup options
|
|
143
|
-
* @returns {{remove: boolean, reason: string}} Removal decision with reason
|
|
144
|
-
*/
|
|
145
|
-
export declare function shouldRemoveNode(node: MemoryNode, options?: CleanupOptions): {
|
|
146
|
-
remove: boolean;
|
|
147
|
-
reason: string;
|
|
148
|
-
};
|
|
149
|
-
/**
|
|
150
|
-
* Calculate approximate byte size of a node when serialized.
|
|
151
|
-
*
|
|
152
|
-
* @param node - Memory node
|
|
153
|
-
* @returns Approximate byte size
|
|
154
|
-
*/
|
|
155
|
-
export declare function estimateNodeBytes(node: MemoryNode): number;
|
|
156
|
-
/**
|
|
157
|
-
* Calculate compaction ratio (removed / total).
|
|
158
|
-
*
|
|
159
|
-
* @param removedCount - Number of removed nodes
|
|
160
|
-
* @param totalCount - Total number of nodes
|
|
161
|
-
* @returns Compaction ratio (0 to 1, or 0 if no nodes)
|
|
162
|
-
*/
|
|
163
|
-
export declare function getCompactionRatio(removedCount: number, totalCount: number): number;
|
|
164
|
-
/**
|
|
165
|
-
* Perform memory cleanup based on lifecycle policy and TTL.
|
|
166
|
-
*
|
|
167
|
-
* Removes nodes according to lifecycle policy:
|
|
168
|
-
* - Active sessions are never removed (WU-1554)
|
|
169
|
-
* - Sensitive nodes are always retained
|
|
170
|
-
* - Project nodes are never removed
|
|
171
|
-
* - TTL-expired nodes are removed (WU-1554)
|
|
172
|
-
* - Ephemeral nodes are always removed
|
|
173
|
-
* - Session nodes are removed when their session is closed
|
|
174
|
-
* - WU nodes are removed only when marked with summarized_into
|
|
175
|
-
*
|
|
176
|
-
* In dry-run mode, no modifications are made but the result shows
|
|
177
|
-
* what would be removed.
|
|
178
|
-
*
|
|
179
|
-
* @param {string} baseDir - Base directory containing .lumenflow/memory/
|
|
180
|
-
* @param {CleanupOptions} options - Cleanup options
|
|
181
|
-
* @returns {Promise<CleanupResult>} Result with removed nodes and metrics
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* // Cleanup with dry-run to preview
|
|
185
|
-
* const preview = await cleanupMemory(baseDir, { dryRun: true });
|
|
186
|
-
* console.log(`Would remove ${preview.removedIds.length} nodes`);
|
|
187
|
-
* console.log(`Would free ${preview.bytesFreed} bytes`);
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* // Cleanup session nodes when session closes
|
|
191
|
-
* const result = await cleanupMemory(baseDir, {
|
|
192
|
-
* sessionId: 'abc-123-def-456',
|
|
193
|
-
* });
|
|
194
|
-
* console.log(`Removed ${result.removedIds.length} nodes`);
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* // WU-1554: TTL-based cleanup
|
|
198
|
-
* const result = await cleanupMemory(baseDir, { ttl: '30d' });
|
|
199
|
-
* console.log(`Removed ${result.breakdown.ttlExpired} expired nodes`);
|
|
200
|
-
*/
|
|
201
|
-
export declare function cleanupMemory(baseDir: string, options?: CleanupOptions): Promise<CleanupResult>;
|
|
202
|
-
export {};
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Create Core (WU-1469)
|
|
3
|
-
*
|
|
4
|
-
* Core logic for creating memory nodes with discovered-from provenance.
|
|
5
|
-
* KEY DIFFERENTIATOR: supports discovered-from relationship for scope-creep
|
|
6
|
-
* forensics. Creates audit trail of WHY work expanded, not just WHAT changed.
|
|
7
|
-
*
|
|
8
|
-
* Features:
|
|
9
|
-
* - Creates all 5 node types: session, discovery, checkpoint, note, summary
|
|
10
|
-
* - Auto-generates hash-based ID using mem-id
|
|
11
|
-
* - Validates node against memory-schema
|
|
12
|
-
* - Supports discovered-from relationship for provenance tracking
|
|
13
|
-
*
|
|
14
|
-
* @see {@link tools/mem-create.mjs} - CLI wrapper
|
|
15
|
-
* @see {@link tools/__tests__/mem-create.test.mjs} - Tests
|
|
16
|
-
* @see {@link tools/lib/memory-schema.mjs} - Schema definitions
|
|
17
|
-
*/
|
|
18
|
-
import { type MemoryNode } from './memory-schema.js';
|
|
19
|
-
/**
|
|
20
|
-
* Memory node creation options
|
|
21
|
-
*/
|
|
22
|
-
export interface CreateMemoryNodeOptions {
|
|
23
|
-
/** Node title/content (required) */
|
|
24
|
-
title: string;
|
|
25
|
-
/** Node type (session, discovery, checkpoint, note, summary) */
|
|
26
|
-
type?: string;
|
|
27
|
-
/** Work Unit ID to link node to */
|
|
28
|
-
wuId?: string;
|
|
29
|
-
/** Session ID to link node to */
|
|
30
|
-
sessionId?: string;
|
|
31
|
-
/** Parent node ID for provenance tracking */
|
|
32
|
-
discoveredFrom?: string;
|
|
33
|
-
/** Tags for categorization */
|
|
34
|
-
tags?: string[];
|
|
35
|
-
/** Priority level (P0, P1, P2, P3) */
|
|
36
|
-
priority?: string;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Relationship between memory nodes
|
|
40
|
-
*/
|
|
41
|
-
interface Relationship {
|
|
42
|
-
from_id: string;
|
|
43
|
-
to_id: string;
|
|
44
|
-
type: string;
|
|
45
|
-
created_at: string;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Memory node creation result
|
|
49
|
-
*/
|
|
50
|
-
export interface CreateMemoryNodeResult {
|
|
51
|
-
/** Whether the operation succeeded */
|
|
52
|
-
success: boolean;
|
|
53
|
-
/** Created memory node */
|
|
54
|
-
node: MemoryNode;
|
|
55
|
-
/** Created relationship (if discoveredFrom provided) */
|
|
56
|
-
relationship?: Relationship;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Creates a new memory node with optional discovered-from provenance.
|
|
60
|
-
*
|
|
61
|
-
* Creates a memory node with:
|
|
62
|
-
* - Unique ID (mem-XXXX format) generated from content hash
|
|
63
|
-
* - User-provided title as content
|
|
64
|
-
* - Type-appropriate lifecycle
|
|
65
|
-
* - Optional discovered-from relationship for provenance tracking
|
|
66
|
-
*
|
|
67
|
-
* @param {string} baseDir - Base directory containing .lumenflow/memory/
|
|
68
|
-
* @param {CreateMemoryNodeOptions} options - Node creation options
|
|
69
|
-
* @returns {Promise<CreateMemoryNodeResult>} Result with created node and optional relationship
|
|
70
|
-
* @throws {Error} If title is missing, type is invalid, or IDs are malformed
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* // Create a simple discovery node
|
|
74
|
-
* const result = await createMemoryNode(baseDir, {
|
|
75
|
-
* title: 'Found relevant file at src/utils.mjs',
|
|
76
|
-
* type: 'discovery',
|
|
77
|
-
* wuId: 'WU-1469',
|
|
78
|
-
* });
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* // Create a node with provenance (scope-creep tracking)
|
|
82
|
-
* const parent = await createMemoryNode(baseDir, {
|
|
83
|
-
* title: 'Found src/components/',
|
|
84
|
-
* type: 'discovery',
|
|
85
|
-
* });
|
|
86
|
-
* const child = await createMemoryNode(baseDir, {
|
|
87
|
-
* title: 'Found src/components/Button.tsx',
|
|
88
|
-
* type: 'discovery',
|
|
89
|
-
* discoveredFrom: parent.node.id, // Track where this came from
|
|
90
|
-
* });
|
|
91
|
-
*/
|
|
92
|
-
export declare function createMemoryNode(baseDir: string, options: CreateMemoryNodeOptions): Promise<CreateMemoryNodeResult>;
|
|
93
|
-
export {};
|