@a13xu/lucid 1.16.1 → 1.19.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/README.md +1 -1
- package/build/compression/semantic.js +5 -1
- package/build/database.d.ts +51 -0
- package/build/database.js +86 -0
- package/build/guardian/checklist.d.ts +2 -1
- package/build/guardian/checklist.js +20 -1
- package/build/guardian/coding-rules.d.ts +2 -1
- package/build/guardian/coding-rules.js +20 -1
- package/build/guardian/session-tracker.d.ts +34 -0
- package/build/guardian/session-tracker.js +105 -0
- package/build/guardian/truncate-guard.d.ts +54 -0
- package/build/guardian/truncate-guard.js +136 -0
- package/build/index.js +745 -742
- package/build/local-llm/client.d.ts +20 -0
- package/build/local-llm/client.js +140 -0
- package/build/local-llm/config.d.ts +11 -0
- package/build/local-llm/config.js +50 -0
- package/build/local-llm/runtimes.d.ts +16 -0
- package/build/local-llm/runtimes.js +82 -0
- package/build/local-llm/setup-cli.d.ts +5 -0
- package/build/local-llm/setup-cli.js +298 -0
- package/build/local-llm/types.d.ts +34 -0
- package/build/local-llm/types.js +5 -0
- package/build/tools/backup.d.ts +47 -0
- package/build/tools/backup.js +107 -0
- package/build/tools/delegate-local.d.ts +23 -0
- package/build/tools/delegate-local.js +75 -0
- package/build/tools/init.js +124 -2
- package/build/tools/plan.js +2 -2
- package/build/tools/session.d.ts +13 -0
- package/build/tools/session.js +59 -0
- package/package.json +3 -1
- package/skills/lucid-audit/SKILL.md +11 -0
- package/skills/lucid-context/SKILL.md +9 -0
- package/skills/lucid-plan/SKILL.md +9 -0
- package/skills/lucid-security/SKILL.md +9 -0
- package/skills/lucid-start/SKILL.md +9 -0
- package/skills/lucid-webdev/SKILL.md +14 -0
package/README.md
CHANGED
|
@@ -96,7 +96,7 @@ Default DB path: `~/.claude/memory.db`
|
|
|
96
96
|
| `plan_create` | Create a development plan with title, description, and tasks. Returns plan ID. |
|
|
97
97
|
| `plan_list` | List all plans with status summary (total/done/in-progress tasks). |
|
|
98
98
|
| `plan_get` | Get full plan details including all tasks and their status. |
|
|
99
|
-
| `plan_update_task` | Update a task's status (`
|
|
99
|
+
| `plan_update_task` | Update a task's status (`pending` → `in_progress` → `done` \| `blocked`) and optionally add notes. Accepts `task_id` as number or string. |
|
|
100
100
|
|
|
101
101
|
### Reward system
|
|
102
102
|
| Tool | Description |
|
|
@@ -13,7 +13,11 @@
|
|
|
13
13
|
import { join } from "path";
|
|
14
14
|
import { homedir } from "os";
|
|
15
15
|
import { mkdirSync } from "fs";
|
|
16
|
-
|
|
16
|
+
// Microsoft's original repo ships only PyTorch weights, so transformers.js
|
|
17
|
+
// (ONNX Runtime) cannot load it. ldenoue/* mirrors the same checkpoint with
|
|
18
|
+
// pre-built `onnx/model.onnx` (710 MB) and `onnx/model_quantized.onnx` (179 MB),
|
|
19
|
+
// matching the dtype lookups used below.
|
|
20
|
+
const MODEL_ID = "ldenoue/llmlingua-2-bert-base-multilingual-cased-meetingbank";
|
|
17
21
|
const MODELS_DIR = join(homedir(), ".lucid", "models");
|
|
18
22
|
let _pipeline = null;
|
|
19
23
|
let _loadError = null;
|
package/build/database.d.ts
CHANGED
|
@@ -45,6 +45,36 @@ export interface PlanRow {
|
|
|
45
45
|
created_at: number;
|
|
46
46
|
updated_at: number;
|
|
47
47
|
}
|
|
48
|
+
export interface FileBackupRow {
|
|
49
|
+
id: number;
|
|
50
|
+
filepath: string;
|
|
51
|
+
content: Buffer;
|
|
52
|
+
content_hash: string;
|
|
53
|
+
original_size: number;
|
|
54
|
+
compressed_size: number;
|
|
55
|
+
reason: string;
|
|
56
|
+
created_at: number;
|
|
57
|
+
}
|
|
58
|
+
export interface CliSessionRow {
|
|
59
|
+
session_id: string;
|
|
60
|
+
started_at: number;
|
|
61
|
+
last_activity_at: number;
|
|
62
|
+
prompt_count: number;
|
|
63
|
+
last_compact_hint_at: number | null;
|
|
64
|
+
last_clear_hint_at: number | null;
|
|
65
|
+
last_compact_event_at: number | null;
|
|
66
|
+
compact_count: number;
|
|
67
|
+
cwd: string | null;
|
|
68
|
+
}
|
|
69
|
+
export interface TruncateEventRow {
|
|
70
|
+
id: number;
|
|
71
|
+
filepath: string;
|
|
72
|
+
prev_size: number;
|
|
73
|
+
new_size: number;
|
|
74
|
+
shrink_ratio: number;
|
|
75
|
+
blocked: number;
|
|
76
|
+
created_at: number;
|
|
77
|
+
}
|
|
48
78
|
export interface PlanTaskRow {
|
|
49
79
|
id: number;
|
|
50
80
|
plan_id: number;
|
|
@@ -115,6 +145,27 @@ export interface Statements {
|
|
|
115
145
|
countRemainingTasks: Stmt<[number], {
|
|
116
146
|
count: number;
|
|
117
147
|
}>;
|
|
148
|
+
insertBackup: WriteStmt<[string, Buffer, string, number, number, string]>;
|
|
149
|
+
getBackupsByPath: Stmt<[string], FileBackupRow>;
|
|
150
|
+
getLatestBackup: Stmt<[string], FileBackupRow>;
|
|
151
|
+
getBackupById: Stmt<[number], FileBackupRow>;
|
|
152
|
+
deleteOldBackups: WriteStmt<[string, string, number]>;
|
|
153
|
+
countBackups: Stmt<[string], {
|
|
154
|
+
count: number;
|
|
155
|
+
}>;
|
|
156
|
+
insertTruncateEvent: WriteStmt<[string, number, number, number, number]>;
|
|
157
|
+
recentTruncateEvents: Stmt<[number], TruncateEventRow>;
|
|
158
|
+
countRecentTruncates: Stmt<[number], {
|
|
159
|
+
count: number;
|
|
160
|
+
}>;
|
|
161
|
+
getCliSession: Stmt<[string], CliSessionRow>;
|
|
162
|
+
insertCliSession: WriteStmt<[string, number, number, string | null]>;
|
|
163
|
+
tickCliSession: WriteStmt<[number, string]>;
|
|
164
|
+
markCompactHint: WriteStmt<[number, string]>;
|
|
165
|
+
markClearHint: WriteStmt<[number, string]>;
|
|
166
|
+
markCompactEvent: WriteStmt<[number, string]>;
|
|
167
|
+
recentCliSessions: Stmt<[number], CliSessionRow>;
|
|
168
|
+
resetCliSessionCount: WriteStmt<[string]>;
|
|
118
169
|
}
|
|
119
170
|
export declare function prepareStatements(db: Database.Database): Statements;
|
|
120
171
|
export {};
|
package/build/database.js
CHANGED
|
@@ -170,6 +170,46 @@ function createSchema(db) {
|
|
|
170
170
|
|
|
171
171
|
CREATE INDEX IF NOT EXISTS idx_plan_tasks_plan ON plan_tasks(plan_id, seq);
|
|
172
172
|
CREATE INDEX IF NOT EXISTS idx_plans_status ON plans(status);
|
|
173
|
+
|
|
174
|
+
-- Versioned backups of file content (last N kept per file)
|
|
175
|
+
CREATE TABLE IF NOT EXISTS file_backups (
|
|
176
|
+
id INTEGER PRIMARY KEY,
|
|
177
|
+
filepath TEXT NOT NULL,
|
|
178
|
+
content BLOB NOT NULL,
|
|
179
|
+
content_hash TEXT NOT NULL,
|
|
180
|
+
original_size INTEGER NOT NULL,
|
|
181
|
+
compressed_size INTEGER NOT NULL,
|
|
182
|
+
reason TEXT NOT NULL DEFAULT 'manual',
|
|
183
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch())
|
|
184
|
+
);
|
|
185
|
+
CREATE INDEX IF NOT EXISTS idx_fb_path ON file_backups(filepath, created_at DESC);
|
|
186
|
+
CREATE INDEX IF NOT EXISTS idx_fb_created ON file_backups(created_at);
|
|
187
|
+
|
|
188
|
+
-- Claude Code session tracking — drives /compact and /clear hints
|
|
189
|
+
CREATE TABLE IF NOT EXISTS cli_sessions (
|
|
190
|
+
session_id TEXT PRIMARY KEY,
|
|
191
|
+
started_at INTEGER NOT NULL,
|
|
192
|
+
last_activity_at INTEGER NOT NULL,
|
|
193
|
+
prompt_count INTEGER NOT NULL DEFAULT 0,
|
|
194
|
+
last_compact_hint_at INTEGER,
|
|
195
|
+
last_clear_hint_at INTEGER,
|
|
196
|
+
last_compact_event_at INTEGER,
|
|
197
|
+
compact_count INTEGER NOT NULL DEFAULT 0,
|
|
198
|
+
cwd TEXT
|
|
199
|
+
);
|
|
200
|
+
CREATE INDEX IF NOT EXISTS idx_cli_sessions_activity ON cli_sessions(last_activity_at DESC);
|
|
201
|
+
|
|
202
|
+
-- Truncate-event log (drives cascade detection)
|
|
203
|
+
CREATE TABLE IF NOT EXISTS truncate_events (
|
|
204
|
+
id INTEGER PRIMARY KEY,
|
|
205
|
+
filepath TEXT NOT NULL,
|
|
206
|
+
prev_size INTEGER NOT NULL,
|
|
207
|
+
new_size INTEGER NOT NULL,
|
|
208
|
+
shrink_ratio REAL NOT NULL,
|
|
209
|
+
blocked INTEGER NOT NULL DEFAULT 1,
|
|
210
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch())
|
|
211
|
+
);
|
|
212
|
+
CREATE INDEX IF NOT EXISTS idx_te_created ON truncate_events(created_at DESC);
|
|
173
213
|
`);
|
|
174
214
|
}
|
|
175
215
|
export function prepareStatements(db) {
|
|
@@ -261,5 +301,51 @@ export function prepareStatements(db) {
|
|
|
261
301
|
getTaskById: db.prepare("SELECT * FROM plan_tasks WHERE id = ?"),
|
|
262
302
|
updateTaskStatus: db.prepare("UPDATE plan_tasks SET status = ?, notes = ?, updated_at = unixepoch() WHERE id = ?"),
|
|
263
303
|
countRemainingTasks: db.prepare("SELECT COUNT(*) as count FROM plan_tasks WHERE plan_id = ? AND status != 'done'"),
|
|
304
|
+
// file_backups
|
|
305
|
+
insertBackup: db.prepare(`INSERT INTO file_backups (filepath, content, content_hash, original_size, compressed_size, reason)
|
|
306
|
+
VALUES (?, ?, ?, ?, ?, ?)`),
|
|
307
|
+
getBackupsByPath: db.prepare("SELECT * FROM file_backups WHERE filepath = ? ORDER BY created_at DESC, id DESC"),
|
|
308
|
+
getLatestBackup: db.prepare("SELECT * FROM file_backups WHERE filepath = ? ORDER BY created_at DESC, id DESC LIMIT 1"),
|
|
309
|
+
getBackupById: db.prepare("SELECT * FROM file_backups WHERE id = ?"),
|
|
310
|
+
deleteOldBackups: db.prepare(`DELETE FROM file_backups
|
|
311
|
+
WHERE filepath = ?
|
|
312
|
+
AND id NOT IN (
|
|
313
|
+
SELECT id FROM file_backups
|
|
314
|
+
WHERE filepath = ?
|
|
315
|
+
ORDER BY created_at DESC, id DESC
|
|
316
|
+
LIMIT ?
|
|
317
|
+
)`),
|
|
318
|
+
countBackups: db.prepare("SELECT COUNT(*) as count FROM file_backups WHERE filepath = ?"),
|
|
319
|
+
// truncate_events
|
|
320
|
+
insertTruncateEvent: db.prepare(`INSERT INTO truncate_events (filepath, prev_size, new_size, shrink_ratio, blocked)
|
|
321
|
+
VALUES (?, ?, ?, ?, ?)`),
|
|
322
|
+
recentTruncateEvents: db.prepare("SELECT * FROM truncate_events WHERE created_at >= ? ORDER BY created_at DESC"),
|
|
323
|
+
countRecentTruncates: db.prepare("SELECT COUNT(*) as count FROM truncate_events WHERE created_at >= ? AND blocked = 1"),
|
|
324
|
+
// cli_sessions
|
|
325
|
+
getCliSession: db.prepare("SELECT * FROM cli_sessions WHERE session_id = ?"),
|
|
326
|
+
insertCliSession: db.prepare(`INSERT INTO cli_sessions (session_id, started_at, last_activity_at, prompt_count, cwd)
|
|
327
|
+
VALUES (?, ?, ?, 1, ?)
|
|
328
|
+
ON CONFLICT(session_id) DO UPDATE SET
|
|
329
|
+
last_activity_at = excluded.last_activity_at,
|
|
330
|
+
prompt_count = cli_sessions.prompt_count + 1`),
|
|
331
|
+
tickCliSession: db.prepare(`UPDATE cli_sessions SET
|
|
332
|
+
last_activity_at = ?,
|
|
333
|
+
prompt_count = prompt_count + 1
|
|
334
|
+
WHERE session_id = ?`),
|
|
335
|
+
markCompactHint: db.prepare("UPDATE cli_sessions SET last_compact_hint_at = ? WHERE session_id = ?"),
|
|
336
|
+
markClearHint: db.prepare("UPDATE cli_sessions SET last_clear_hint_at = ? WHERE session_id = ?"),
|
|
337
|
+
markCompactEvent: db.prepare(`UPDATE cli_sessions SET
|
|
338
|
+
last_compact_event_at = ?,
|
|
339
|
+
compact_count = compact_count + 1,
|
|
340
|
+
prompt_count = 0,
|
|
341
|
+
last_compact_hint_at = NULL,
|
|
342
|
+
last_clear_hint_at = NULL
|
|
343
|
+
WHERE session_id = ?`),
|
|
344
|
+
recentCliSessions: db.prepare("SELECT * FROM cli_sessions ORDER BY last_activity_at DESC LIMIT ?"),
|
|
345
|
+
resetCliSessionCount: db.prepare(`UPDATE cli_sessions SET
|
|
346
|
+
prompt_count = 0,
|
|
347
|
+
last_compact_hint_at = NULL,
|
|
348
|
+
last_clear_hint_at = NULL
|
|
349
|
+
WHERE session_id = ?`),
|
|
264
350
|
};
|
|
265
351
|
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const ORIGINAL_CHECKLIST = "# Logic Guardian \u2014 Validation Checklist (5 passes)\n\n## Pass 1: Logic Trace\nTrace through the code with CONCRETE values:\n- Happy path \u2192 real values, write each variable state\n- Empty/zero \u2192 null, 0, \"\", []\n- Boundary \u2192 first element, last element, max int, single char\n- Error case \u2192 network down, file missing, permission denied\n\nSTOP if any trace produces unexpected output. Fix before continuing.\n\n## Pass 2: Contract Verification\n- [ ] Preconditions: what must be true BEFORE this runs? Is it checked?\n- [ ] Postconditions: what must be true AFTER? Can you prove it?\n- [ ] Invariants: what must ALWAYS be true? Does the code maintain it?\n- [ ] Return type: does EVERY code path return the expected type?\n- [ ] Side effects: are all side effects intentional?\n\n## Pass 3: Stupid Mistakes Checklist\n\n### Off-by-one\n- [ ] < vs <= \u2014 verify with boundary values\n- [ ] Array indices \u2014 last is length - 1\n- [ ] Loop iterations \u2014 exactly N times?\n\n### Null/Undefined Propagation\n- [ ] Every .property access \u2014 can the object be null?\n- [ ] Every array index \u2014 can the array be empty?\n- [ ] Every map lookup \u2014 can the key be missing?\n\n### Type Confusion\n- [ ] String vs Number comparisons\n- [ ] Integer vs Float division\n- [ ] Boolean coercion edge cases\n\n### Logic Inversions (THE #1 LLM drift pattern)\n- [ ] if/else \u2014 is the condition testing what you THINK?\n- [ ] Early returns \u2014 does the guard return the RIGHT value?\n- [ ] filter/find/some \u2014 keeping the RIGHT elements?\n- [ ] Error handling \u2014 catching and re-throwing correctly?\n\n### State & Mutation\n- [ ] Mutating shared object when you should copy?\n- [ ] Async state read after it might have changed?\n\n### Copy-Paste Drift\n- [ ] ALL variable names updated in copied blocks?\n- [ ] Conditions changed, not just variable names?\n\n## Pass 4: Integration Sanity\n- [ ] Breaks existing callers?\n- [ ] Imports/exports correct?\n- [ ] If async, all callers awaiting it?\n- [ ] If type changed, all usages updated?\n\n## Pass 5: Explain It Test\nIn ONE sentence: what does this code do?\nIf you can't explain it, or the sentence doesn't match the code \u2192 something is wrong.\n\n## Anti-Drift Triggers\nSTOP if you find yourself thinking:\n- \"This is similar to...\" \u2192 You're pattern-matching. TRACE THE LOGIC.\n- \"This should work because the other one does\" \u2192 VERIFY INDEPENDENTLY.\n- \"I'll just copy and change the names\" \u2192 CHECK EVERY DIFFERENCE.\n- \"The error handling is probably fine\" \u2192 TRACE THE ERROR PATH.\n- \"This is standard boilerplate\" \u2192 Verify it fits this context.\n";
|
|
2
|
+
export declare const CHECKLIST: string;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { join } from "path";
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { existsSync, readFileSync } from "fs";
|
|
4
|
+
export const ORIGINAL_CHECKLIST = `# Logic Guardian — Validation Checklist (5 passes)
|
|
2
5
|
|
|
3
6
|
## Pass 1: Logic Trace
|
|
4
7
|
Trace through the code with CONCRETE values:
|
|
@@ -65,3 +68,19 @@ STOP if you find yourself thinking:
|
|
|
65
68
|
- "The error handling is probably fine" → TRACE THE ERROR PATH.
|
|
66
69
|
- "This is standard boilerplate" → Verify it fits this context.
|
|
67
70
|
`;
|
|
71
|
+
// Opt-in: if a pre-compressed copy exists at ~/.lucid/compressed-prompts/checklist.txt
|
|
72
|
+
// (produced by `npm run compress-prompts`), serve that instead. Falls back to the
|
|
73
|
+
// original on any error so this is always safe.
|
|
74
|
+
function loadCompressed() {
|
|
75
|
+
try {
|
|
76
|
+
const p = join(homedir(), ".lucid", "compressed-prompts", "checklist.txt");
|
|
77
|
+
if (!existsSync(p))
|
|
78
|
+
return null;
|
|
79
|
+
const text = readFileSync(p, "utf-8").trim();
|
|
80
|
+
return text.length > 0 ? text : null;
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export const CHECKLIST = loadCompressed() ?? ORIGINAL_CHECKLIST;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const ORIGINAL_CODING_RULES = "# 25 Golden Rules \u2014 Code Quality Checklist\n\n## Section 1: General Quality (Rules 1\u201310)\n\n- [ ] **Rule 1 \u2014 File Size**: File is under 500 lines (components under 300).\n PASS: file has fewer lines. FAIL: file exceeds limit \u2014 split into modules.\n\n- [ ] **Rule 2 \u2014 Function Length**: Every function/method is under 60 lines.\n PASS: function is focused and short. FAIL: function exceeds 60 lines \u2014 break it up.\n\n- [ ] **Rule 3 \u2014 Meaningful Names**: No vague variable names (x, tmp, data, val, obj, foo, bar).\n No vague function names (doSomething, handleStuff, processData, manage, doWork).\n PASS: names explain purpose. FAIL: name is a placeholder \u2014 rename to intent.\n\n- [ ] **Rule 4 \u2014 Single Responsibility**: Functions/methods do exactly ONE thing.\n If the name contains \"And\" or \"Or\", it is doing two things.\n PASS: one verb, one concept. FAIL: split into two functions.\n\n- [ ] **Rule 5 \u2014 No Dead Code**: No commented-out code blocks (3+ consecutive lines with =, (, {, ;).\n PASS: code is live. FAIL: remove dead code \u2014 use version control for history.\n\n- [ ] **Rule 6 \u2014 Explicit Error Handling**: Errors are caught, logged, or re-thrown.\n No silent swallowing (catch {} or except: pass).\n PASS: every error path is handled. FAIL: add logging or re-throw.\n\n- [ ] **Rule 7 \u2014 No Magic Numbers**: Numeric literals are replaced with named constants.\n PASS: constants have descriptive names. FAIL: extract to a named constant.\n\n- [ ] **Rule 8 \u2014 Max 3 Nesting Levels**: Code is not nested more than 3 levels deep.\n PASS: deepest indent is 3 levels. FAIL: extract logic or use early returns.\n\n- [ ] **Rule 9 \u2014 DRY (Don't Repeat Yourself)**: No near-duplicate blocks of code.\n PASS: logic is extracted into a shared function. FAIL: refactor duplicates.\n\n- [ ] **Rule 10 \u2014 Explicit Return Types**: Typed languages declare return types on public functions.\n PASS: all public functions have explicit return types. FAIL: add return type annotation.\n\n## Section 2: Frontend Components (Rules 11\u201318)\n\n- [ ] **Rule 11 \u2014 Component Size**: UI components are under 300 lines.\n PASS: component is focused. FAIL: extract sub-components or custom hooks.\n\n- [ ] **Rule 12 \u2014 No Inline Styles**: No style={{ }} in JSX/Vue templates.\n PASS: styles are in CSS/SCSS/CSS-in-JS. FAIL: move to stylesheet or styled component.\n\n- [ ] **Rule 13 \u2014 Prop Limit**: Components accept at most 8 props.\n PASS: props are few and cohesive. FAIL: group related props into an object.\n\n- [ ] **Rule 14 \u2014 Prefer Composition**: Large components are split into sub-components.\n No \"god components\" handling layout + data + formatting + interaction.\n PASS: each component has one visual/logical role. FAIL: extract a sub-component.\n\n- [ ] **Rule 15 \u2014 No Direct DOM Access**: No document.querySelector / getElementById in components.\n PASS: refs are used (useRef, ref=). FAIL: replace with ref mechanism.\n\n- [ ] **Rule 16 \u2014 Data Fetching in Services/Hooks**: No fetch() or axios calls in component body.\n PASS: data fetching is in a custom hook or service. FAIL: extract to useXxx hook.\n\n- [ ] **Rule 17 \u2014 One Styling System**: File uses only one styling approach\n (Tailwind OR styled-components OR CSS modules \u2014 not all three).\n PASS: consistent styling. FAIL: standardize on one approach.\n\n- [ ] **Rule 18 \u2014 No Nested Ternaries in JSX**: JSX conditionals use if/else or variables, not nested ternaries.\n PASS: conditions are readable. FAIL: extract condition to a variable or early return.\n\n## Section 3: Architecture (Rules 19\u201325)\n\n- [ ] **Rule 19 \u2014 Single Source of Truth**: State is not duplicated across multiple stores or components.\n PASS: one authoritative source for each piece of state. FAIL: remove duplication.\n\n- [ ] **Rule 20 \u2014 UI/Logic Separation**: Business logic is not inside UI components.\n PASS: components call services/hooks; logic lives elsewhere. FAIL: extract to service.\n\n- [ ] **Rule 21 \u2014 Dependency Abstraction**: External APIs/SDKs are wrapped in adapter/service layers.\n PASS: swapping a library touches one file. FAIL: add an abstraction layer.\n\n- [ ] **Rule 22 \u2014 No Circular Imports**: Module dependency graph is a DAG (no cycles).\n PASS: import graph has no cycles. FAIL: restructure modules to remove the cycle.\n\n- [ ] **Rule 23 \u2014 Config in Dedicated Files**: Magic strings and environment-specific values are in config files.\n PASS: config is centralized. FAIL: extract to config.ts or .env.\n\n- [ ] **Rule 24 \u2014 Public API Coverage**: Every exported function has a corresponding test.\n PASS: public API is covered by tests. FAIL: write a test for the new export.\n\n- [ ] **Rule 25 \u2014 No God Objects**: Classes/modules are focused \u2014 no object that knows everything.\n PASS: objects have clear, narrow responsibilities. FAIL: split the god object.\n\n---\n\n## Quick Check Before Marking Done\n\n1. Run `check_code_quality` on modified files \u2014 fix HIGH severity issues.\n2. Run `validate_file` (Logic Guardian) \u2014 fix correctness bugs first.\n3. Verify rules 3, 4, 8 manually (naming and nesting are hard to auto-detect fully).\n4. For frontend work, verify rules 12, 15, 16 \u2014 these are the most common oversights.\n";
|
|
2
|
+
export declare const CODING_RULES: string;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { join } from "path";
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { existsSync, readFileSync } from "fs";
|
|
4
|
+
export const ORIGINAL_CODING_RULES = `# 25 Golden Rules — Code Quality Checklist
|
|
2
5
|
|
|
3
6
|
## Section 1: General Quality (Rules 1–10)
|
|
4
7
|
|
|
@@ -95,3 +98,19 @@ export const CODING_RULES = `# 25 Golden Rules — Code Quality Checklist
|
|
|
95
98
|
3. Verify rules 3, 4, 8 manually (naming and nesting are hard to auto-detect fully).
|
|
96
99
|
4. For frontend work, verify rules 12, 15, 16 — these are the most common oversights.
|
|
97
100
|
`;
|
|
101
|
+
// Opt-in: if a pre-compressed copy exists at ~/.lucid/compressed-prompts/coding-rules.txt
|
|
102
|
+
// (produced by `npm run compress-prompts`), serve that instead. Falls back to the
|
|
103
|
+
// original on any error so this is always safe.
|
|
104
|
+
function loadCompressed() {
|
|
105
|
+
try {
|
|
106
|
+
const p = join(homedir(), ".lucid", "compressed-prompts", "coding-rules.txt");
|
|
107
|
+
if (!existsSync(p))
|
|
108
|
+
return null;
|
|
109
|
+
const text = readFileSync(p, "utf-8").trim();
|
|
110
|
+
return text.length > 0 ? text : null;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export const CODING_RULES = loadCompressed() ?? ORIGINAL_CODING_RULES;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Tracker — emits hints to invoke /compact or /clear when a Claude Code
|
|
3
|
+
* session grows expensive. Driven by UserPromptSubmit and PreCompact hooks.
|
|
4
|
+
*
|
|
5
|
+
* Cost model: Anthropic prompt cache has a 5-minute TTL. Beyond that the next
|
|
6
|
+
* turn re-reads the entire transcript. Long sessions also pay per-turn linearly
|
|
7
|
+
* with transcript length even when warm. Two pressures, two hints:
|
|
8
|
+
*
|
|
9
|
+
* 1) prompt_count >= COMPACT_HINT_AT → suggest /compact (re-emitted every N).
|
|
10
|
+
* 2) prompt_count >= CLEAR_HINT_AT → suggest /clear (one-shot).
|
|
11
|
+
* 3) idle > CACHE_STALE_SECONDS → cache cold; warn next turn re-bills.
|
|
12
|
+
*
|
|
13
|
+
* Emitted hints are written to STDOUT so the UserPromptSubmit hook injects them
|
|
14
|
+
* into Claude's context (Claude Code convention). Empty stdout = no hint.
|
|
15
|
+
*/
|
|
16
|
+
import type { Statements } from "../database.js";
|
|
17
|
+
export interface SessionTickResult {
|
|
18
|
+
session_id: string;
|
|
19
|
+
prompt_count: number;
|
|
20
|
+
hints: string[];
|
|
21
|
+
cache_likely_cold: boolean;
|
|
22
|
+
idle_seconds: number;
|
|
23
|
+
}
|
|
24
|
+
/** Fired once per UserPromptSubmit. Returns hints to surface to Claude. */
|
|
25
|
+
export declare function tickSession(stmts: Statements, sessionId: string, cwd: string | null): SessionTickResult;
|
|
26
|
+
/** Fired by PreCompact hook — resets per-session counters. */
|
|
27
|
+
export declare function markCompactEvent(stmts: Statements, sessionId: string): void;
|
|
28
|
+
export declare const SESSION_TUNABLES: {
|
|
29
|
+
readonly COMPACT_HINT_AT: number;
|
|
30
|
+
readonly COMPACT_HINT_EVERY: number;
|
|
31
|
+
readonly CLEAR_HINT_AT: number;
|
|
32
|
+
readonly CACHE_STALE_SECONDS: number;
|
|
33
|
+
readonly IDLE_HINT_MIN_PROMPTS: 5;
|
|
34
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Tracker — emits hints to invoke /compact or /clear when a Claude Code
|
|
3
|
+
* session grows expensive. Driven by UserPromptSubmit and PreCompact hooks.
|
|
4
|
+
*
|
|
5
|
+
* Cost model: Anthropic prompt cache has a 5-minute TTL. Beyond that the next
|
|
6
|
+
* turn re-reads the entire transcript. Long sessions also pay per-turn linearly
|
|
7
|
+
* with transcript length even when warm. Two pressures, two hints:
|
|
8
|
+
*
|
|
9
|
+
* 1) prompt_count >= COMPACT_HINT_AT → suggest /compact (re-emitted every N).
|
|
10
|
+
* 2) prompt_count >= CLEAR_HINT_AT → suggest /clear (one-shot).
|
|
11
|
+
* 3) idle > CACHE_STALE_SECONDS → cache cold; warn next turn re-bills.
|
|
12
|
+
*
|
|
13
|
+
* Emitted hints are written to STDOUT so the UserPromptSubmit hook injects them
|
|
14
|
+
* into Claude's context (Claude Code convention). Empty stdout = no hint.
|
|
15
|
+
*/
|
|
16
|
+
const num = (envKey, fallback) => {
|
|
17
|
+
const v = process.env[envKey];
|
|
18
|
+
if (!v)
|
|
19
|
+
return fallback;
|
|
20
|
+
const n = Number(v);
|
|
21
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
22
|
+
};
|
|
23
|
+
const COMPACT_HINT_AT = num("LUCID_COMPACT_HINT_AT", 15);
|
|
24
|
+
const COMPACT_HINT_EVERY = num("LUCID_COMPACT_HINT_EVERY", 10);
|
|
25
|
+
const CLEAR_HINT_AT = num("LUCID_CLEAR_HINT_AT", 30);
|
|
26
|
+
const CACHE_STALE_SECONDS = num("LUCID_CACHE_STALE_SECONDS", 300);
|
|
27
|
+
/** Below this many prompts, idle gaps don't matter — short session, cheap. */
|
|
28
|
+
const IDLE_HINT_MIN_PROMPTS = 5;
|
|
29
|
+
/** Fired once per UserPromptSubmit. Returns hints to surface to Claude. */
|
|
30
|
+
export function tickSession(stmts, sessionId, cwd) {
|
|
31
|
+
if (process.env["LUCID_SESSION_HINTS_DISABLED"] === "1") {
|
|
32
|
+
return { session_id: sessionId, prompt_count: 0, hints: [], cache_likely_cold: false, idle_seconds: 0 };
|
|
33
|
+
}
|
|
34
|
+
const now = Math.floor(Date.now() / 1000);
|
|
35
|
+
const existing = stmts.getCliSession.get(sessionId);
|
|
36
|
+
if (!existing) {
|
|
37
|
+
stmts.insertCliSession.run(sessionId, now, now, cwd);
|
|
38
|
+
return { session_id: sessionId, prompt_count: 1, hints: [], cache_likely_cold: false, idle_seconds: 0 };
|
|
39
|
+
}
|
|
40
|
+
const idleSeconds = now - existing.last_activity_at;
|
|
41
|
+
const cacheCold = idleSeconds > CACHE_STALE_SECONDS;
|
|
42
|
+
stmts.tickCliSession.run(now, sessionId);
|
|
43
|
+
const newCount = existing.prompt_count + 1;
|
|
44
|
+
const hints = [];
|
|
45
|
+
// ── /compact hint: at threshold, then every N prompts after ────────────────
|
|
46
|
+
const compactDue = newCount === COMPACT_HINT_AT ||
|
|
47
|
+
(newCount > COMPACT_HINT_AT &&
|
|
48
|
+
(newCount - COMPACT_HINT_AT) % COMPACT_HINT_EVERY === 0);
|
|
49
|
+
if (compactDue) {
|
|
50
|
+
hints.push(formatCompactHint(newCount, existing.compact_count));
|
|
51
|
+
stmts.markCompactHint.run(now, sessionId);
|
|
52
|
+
}
|
|
53
|
+
// ── /clear hint: one-shot at CLEAR_HINT_AT ─────────────────────────────────
|
|
54
|
+
if (newCount >= CLEAR_HINT_AT && existing.last_clear_hint_at === null) {
|
|
55
|
+
hints.push(formatClearHint(newCount));
|
|
56
|
+
stmts.markClearHint.run(now, sessionId);
|
|
57
|
+
}
|
|
58
|
+
// ── Cache-cold hint: only meaningful past warmup ───────────────────────────
|
|
59
|
+
if (cacheCold && newCount >= IDLE_HINT_MIN_PROMPTS) {
|
|
60
|
+
hints.push(formatColdCacheHint(idleSeconds));
|
|
61
|
+
}
|
|
62
|
+
return { session_id: sessionId, prompt_count: newCount, hints, cache_likely_cold: cacheCold, idle_seconds: idleSeconds };
|
|
63
|
+
}
|
|
64
|
+
/** Fired by PreCompact hook — resets per-session counters. */
|
|
65
|
+
export function markCompactEvent(stmts, sessionId) {
|
|
66
|
+
const now = Math.floor(Date.now() / 1000);
|
|
67
|
+
// Insert a placeholder row if hook fires before any UserPromptSubmit
|
|
68
|
+
if (!stmts.getCliSession.get(sessionId)) {
|
|
69
|
+
stmts.insertCliSession.run(sessionId, now, now, null);
|
|
70
|
+
}
|
|
71
|
+
stmts.markCompactEvent.run(now, sessionId);
|
|
72
|
+
}
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
// Hint formatters — kept terse: every word costs tokens once injected.
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
function formatCompactHint(promptCount, prevCompacts) {
|
|
77
|
+
const tail = prevCompacts > 0 ? ` (${prevCompacts} prior /compact in this session)` : "";
|
|
78
|
+
return [
|
|
79
|
+
`[Lucid · session-cost]`,
|
|
80
|
+
`Session is at ${promptCount} prompts${tail}. Per-turn cost grows with transcript length even when cached.`,
|
|
81
|
+
`→ Run /compact mid-task to summarize earlier turns and reduce input tokens for the next ~${COMPACT_HINT_EVERY} prompts.`,
|
|
82
|
+
].join(" ");
|
|
83
|
+
}
|
|
84
|
+
function formatClearHint(promptCount) {
|
|
85
|
+
return [
|
|
86
|
+
`[Lucid · session-cost]`,
|
|
87
|
+
`Session has reached ${promptCount} prompts.`,
|
|
88
|
+
`→ If you are switching to a new task with little overlap, prefer /clear over /compact — fresh context is cheaper than a summary.`,
|
|
89
|
+
].join(" ");
|
|
90
|
+
}
|
|
91
|
+
function formatColdCacheHint(idleSeconds) {
|
|
92
|
+
const mins = Math.round(idleSeconds / 60);
|
|
93
|
+
return [
|
|
94
|
+
`[Lucid · session-cost]`,
|
|
95
|
+
`${mins}m idle: prompt cache (5-min TTL) is cold. This turn rebuilds it from scratch (~3-4× the cached cost).`,
|
|
96
|
+
`→ For long pauses, run /compact before resuming so the rebuilt cache covers a smaller transcript.`,
|
|
97
|
+
].join(" ");
|
|
98
|
+
}
|
|
99
|
+
export const SESSION_TUNABLES = {
|
|
100
|
+
COMPACT_HINT_AT,
|
|
101
|
+
COMPACT_HINT_EVERY,
|
|
102
|
+
CLEAR_HINT_AT,
|
|
103
|
+
CACHE_STALE_SECONDS,
|
|
104
|
+
IDLE_HINT_MIN_PROMPTS,
|
|
105
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncate Guard — prevents data-loss writes (file emptied, drastically shrunk)
|
|
3
|
+
* and detects truncate cascades (≥N truncate attempts within a short window),
|
|
4
|
+
* which signal a runaway loop in automode.
|
|
5
|
+
*
|
|
6
|
+
* Two layers:
|
|
7
|
+
* 1) Per-write check: empty content over non-empty file, or new size < 30% prev.
|
|
8
|
+
* 2) Cascade lock: ≥2 blocked truncates within 60s blocks ALL further Write/Edit
|
|
9
|
+
* until the user clears the lock (clear_truncate_lock tool or env override).
|
|
10
|
+
*/
|
|
11
|
+
import type { Statements } from "../database.js";
|
|
12
|
+
export type TruncateRule = "EMPTY_OVERWRITE" | "WHITESPACE_ONLY" | "MAJOR_SHRINK" | "CASCADE_LOCK";
|
|
13
|
+
export interface TruncateAssessment {
|
|
14
|
+
blocked: boolean;
|
|
15
|
+
rule?: TruncateRule;
|
|
16
|
+
prevSize: number;
|
|
17
|
+
newSize: number;
|
|
18
|
+
shrinkRatio: number;
|
|
19
|
+
reason?: string;
|
|
20
|
+
/** True when CASCADE_LOCK is the active reason. */
|
|
21
|
+
cascade: boolean;
|
|
22
|
+
/** Number of blocked truncates inside the cascade window. */
|
|
23
|
+
cascadeCount: number;
|
|
24
|
+
}
|
|
25
|
+
export declare function isCascadeBlocked(stmts: Statements): {
|
|
26
|
+
blocked: boolean;
|
|
27
|
+
count: number;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Assess whether writing `newContent` to `path` is a destructive truncate.
|
|
31
|
+
* Pure function — does NOT mutate state. Caller decides whether to record.
|
|
32
|
+
*/
|
|
33
|
+
export declare function assessTruncate(path: string, newContent: string | null, stmts: Statements): TruncateAssessment;
|
|
34
|
+
/** Persist a truncate event so cascade detection can see it on the next call. */
|
|
35
|
+
export declare function recordTruncateEvent(stmts: Statements, path: string, prevSize: number, newSize: number, blocked: boolean): void;
|
|
36
|
+
export interface BackupResult {
|
|
37
|
+
saved: boolean;
|
|
38
|
+
reason: string;
|
|
39
|
+
size?: number;
|
|
40
|
+
hash?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Snapshot the current on-disk file content into file_backups, then GC older
|
|
44
|
+
* versions beyond BACKUP_RETENTION. No-op if the file does not exist or is
|
|
45
|
+
* identical to the most recent backup.
|
|
46
|
+
*/
|
|
47
|
+
export declare function backupFile(stmts: Statements, path: string, snapshotReason?: string): BackupResult;
|
|
48
|
+
export declare const TUNABLES: {
|
|
49
|
+
readonly MIN_KEEP_RATIO: 0.3;
|
|
50
|
+
readonly SMALL_FILE_BYTES: 80;
|
|
51
|
+
readonly CASCADE_WINDOW_SECONDS: 60;
|
|
52
|
+
readonly CASCADE_THRESHOLD: 2;
|
|
53
|
+
readonly BACKUP_RETENTION: 10;
|
|
54
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncate Guard — prevents data-loss writes (file emptied, drastically shrunk)
|
|
3
|
+
* and detects truncate cascades (≥N truncate attempts within a short window),
|
|
4
|
+
* which signal a runaway loop in automode.
|
|
5
|
+
*
|
|
6
|
+
* Two layers:
|
|
7
|
+
* 1) Per-write check: empty content over non-empty file, or new size < 30% prev.
|
|
8
|
+
* 2) Cascade lock: ≥2 blocked truncates within 60s blocks ALL further Write/Edit
|
|
9
|
+
* until the user clears the lock (clear_truncate_lock tool or env override).
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, readFileSync, statSync } from "fs";
|
|
12
|
+
import { resolve } from "path";
|
|
13
|
+
import { compress, sha256 } from "../store/content.js";
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Tunables (kept local — no config knob until requested)
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
/** New content must keep ≥ this fraction of previous size, else flagged. */
|
|
18
|
+
const MIN_KEEP_RATIO = 0.30;
|
|
19
|
+
/** Files smaller than this are exempt — too small for ratio to be meaningful. */
|
|
20
|
+
const SMALL_FILE_BYTES = 80;
|
|
21
|
+
/** Cascade window: how far back we look for blocked truncate events. */
|
|
22
|
+
const CASCADE_WINDOW_SECONDS = 60;
|
|
23
|
+
/** Number of blocked truncates within the window that triggers a hard lock. */
|
|
24
|
+
const CASCADE_THRESHOLD = 2;
|
|
25
|
+
/** Backups kept per file (last N). */
|
|
26
|
+
const BACKUP_RETENTION = 10;
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Cascade detection
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
export function isCascadeBlocked(stmts) {
|
|
31
|
+
if (process.env["LUCID_TRUNCATE_OVERRIDE"] === "1") {
|
|
32
|
+
return { blocked: false, count: 0 };
|
|
33
|
+
}
|
|
34
|
+
const since = Math.floor(Date.now() / 1000) - CASCADE_WINDOW_SECONDS;
|
|
35
|
+
const row = stmts.countRecentTruncates.get(since);
|
|
36
|
+
const count = row?.count ?? 0;
|
|
37
|
+
return { blocked: count >= CASCADE_THRESHOLD, count };
|
|
38
|
+
}
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Per-write assessment
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
/**
|
|
43
|
+
* Assess whether writing `newContent` to `path` is a destructive truncate.
|
|
44
|
+
* Pure function — does NOT mutate state. Caller decides whether to record.
|
|
45
|
+
*/
|
|
46
|
+
export function assessTruncate(path, newContent, stmts) {
|
|
47
|
+
const cascade = isCascadeBlocked(stmts);
|
|
48
|
+
const absPath = resolve(path);
|
|
49
|
+
// Resolve previous size: filesystem first (source of truth), DB as fallback.
|
|
50
|
+
let prevSize = 0;
|
|
51
|
+
if (existsSync(absPath)) {
|
|
52
|
+
try {
|
|
53
|
+
prevSize = statSync(absPath).size;
|
|
54
|
+
}
|
|
55
|
+
catch { /* ignore */ }
|
|
56
|
+
}
|
|
57
|
+
if (prevSize === 0) {
|
|
58
|
+
const dbRow = stmts.getFileByPath.get(absPath);
|
|
59
|
+
if (dbRow)
|
|
60
|
+
prevSize = dbRow.original_size;
|
|
61
|
+
}
|
|
62
|
+
const newSize = newContent === null
|
|
63
|
+
? -1 // unknown — only cascade lock can apply
|
|
64
|
+
: Buffer.byteLength(newContent, "utf-8");
|
|
65
|
+
const ratio = prevSize > 0 && newSize >= 0 ? newSize / prevSize : 1;
|
|
66
|
+
// Cascade lock takes precedence — even non-truncating writes are blocked.
|
|
67
|
+
if (cascade.blocked) {
|
|
68
|
+
return {
|
|
69
|
+
blocked: true,
|
|
70
|
+
rule: "CASCADE_LOCK",
|
|
71
|
+
prevSize, newSize, shrinkRatio: ratio,
|
|
72
|
+
cascade: true,
|
|
73
|
+
cascadeCount: cascade.count,
|
|
74
|
+
reason: `Cascade lock active: ${cascade.count} truncate attempts within ${CASCADE_WINDOW_SECONDS}s. ` +
|
|
75
|
+
`Run "lucid guard clear" or set LUCID_TRUNCATE_OVERRIDE=1 to release.`,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
// Nothing to compare against — first-time write, allow.
|
|
79
|
+
if (prevSize === 0 || prevSize <= SMALL_FILE_BYTES || newSize < 0) {
|
|
80
|
+
return { blocked: false, prevSize, newSize, shrinkRatio: ratio, cascade: false, cascadeCount: cascade.count };
|
|
81
|
+
}
|
|
82
|
+
if (newSize === 0) {
|
|
83
|
+
return rule("EMPTY_OVERWRITE", prevSize, newSize, ratio, cascade.count, `Refusing to overwrite ${prevSize}B file with empty content.`);
|
|
84
|
+
}
|
|
85
|
+
if (newContent !== null && newContent.trim().length === 0) {
|
|
86
|
+
return rule("WHITESPACE_ONLY", prevSize, newSize, ratio, cascade.count, `Refusing to overwrite ${prevSize}B file with whitespace-only content (${newSize}B).`);
|
|
87
|
+
}
|
|
88
|
+
if (ratio < MIN_KEEP_RATIO) {
|
|
89
|
+
return rule("MAJOR_SHRINK", prevSize, newSize, ratio, cascade.count, `New content keeps only ${Math.round(ratio * 100)}% of previous size ` +
|
|
90
|
+
`(${newSize}B vs ${prevSize}B). Threshold: ${Math.round(MIN_KEEP_RATIO * 100)}%.`);
|
|
91
|
+
}
|
|
92
|
+
return { blocked: false, prevSize, newSize, shrinkRatio: ratio, cascade: false, cascadeCount: cascade.count };
|
|
93
|
+
}
|
|
94
|
+
function rule(r, prev, next, ratio, cascadeCount, reason) {
|
|
95
|
+
return { blocked: true, rule: r, prevSize: prev, newSize: next,
|
|
96
|
+
shrinkRatio: ratio, cascade: false, cascadeCount, reason };
|
|
97
|
+
}
|
|
98
|
+
/** Persist a truncate event so cascade detection can see it on the next call. */
|
|
99
|
+
export function recordTruncateEvent(stmts, path, prevSize, newSize, blocked) {
|
|
100
|
+
const ratio = prevSize > 0 ? Math.max(0, newSize) / prevSize : 1;
|
|
101
|
+
stmts.insertTruncateEvent.run(resolve(path), prevSize, Math.max(0, newSize), ratio, blocked ? 1 : 0);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Snapshot the current on-disk file content into file_backups, then GC older
|
|
105
|
+
* versions beyond BACKUP_RETENTION. No-op if the file does not exist or is
|
|
106
|
+
* identical to the most recent backup.
|
|
107
|
+
*/
|
|
108
|
+
export function backupFile(stmts, path, snapshotReason = "manual") {
|
|
109
|
+
const absPath = resolve(path);
|
|
110
|
+
if (!existsSync(absPath))
|
|
111
|
+
return { saved: false, reason: `File not found: ${absPath}` };
|
|
112
|
+
let source;
|
|
113
|
+
try {
|
|
114
|
+
source = readFileSync(absPath, "utf-8");
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
return { saved: false, reason: `Could not read file: ${e.message}` };
|
|
118
|
+
}
|
|
119
|
+
const hash = sha256(source);
|
|
120
|
+
const latest = stmts.getLatestBackup.get(absPath);
|
|
121
|
+
if (latest && latest.content_hash === hash) {
|
|
122
|
+
return { saved: false, reason: "Identical to latest backup — skipped", hash, size: latest.original_size };
|
|
123
|
+
}
|
|
124
|
+
const compressed = compress(source);
|
|
125
|
+
stmts.insertBackup.run(absPath, compressed, hash, Buffer.byteLength(source, "utf-8"), compressed.length, snapshotReason);
|
|
126
|
+
// Garbage-collect older versions beyond retention.
|
|
127
|
+
stmts.deleteOldBackups.run(absPath, absPath, BACKUP_RETENTION);
|
|
128
|
+
return { saved: true, reason: `Snapshot stored (${snapshotReason})`, hash, size: Buffer.byteLength(source, "utf-8") };
|
|
129
|
+
}
|
|
130
|
+
export const TUNABLES = {
|
|
131
|
+
MIN_KEEP_RATIO,
|
|
132
|
+
SMALL_FILE_BYTES,
|
|
133
|
+
CASCADE_WINDOW_SECONDS,
|
|
134
|
+
CASCADE_THRESHOLD,
|
|
135
|
+
BACKUP_RETENTION,
|
|
136
|
+
};
|