@litfamily/litclaude 1.0.57 → 1.0.59
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/CHANGELOG.md +42 -0
- package/README.md +8 -8
- package/README_ko-KR.md +8 -8
- package/bin/litclaude-ai.js +31 -3
- package/docs/assets/readme/badge-version.svg +1 -1
- package/package.json +1 -1
- package/plugins/litclaude/.claude-plugin/plugin.json +1 -1
- package/plugins/litclaude/bin/litclaude-ai +2 -0
- package/plugins/litclaude/bin/litclaude-hook.js +87 -654
- package/plugins/litclaude/bin/litclaude-mcp.js +1 -1
- package/plugins/litclaude/hooks/lit-mod.mjs +20 -8
- package/plugins/litclaude/lib/goal-files.mjs +104 -0
- package/plugins/litclaude/lib/interface-probe-gate.mjs +20 -11
- package/plugins/litclaude/lib/lit-plan-persistence.mjs +23 -13
- package/plugins/litclaude/lib/litgoal/state.mjs +36 -16
- package/plugins/litclaude/lib/mod/goal.mjs +4 -0
- package/plugins/litclaude/lib/receipt-gate.mjs +101 -0
- package/plugins/litclaude/lib/receipts.mjs +379 -0
- package/plugins/litclaude/lib/rewind/suggest.mjs +20 -9
- package/plugins/litclaude/lib/route-match.mjs +729 -0
- package/plugins/litclaude/lib/route-why-cli.mjs +33 -0
- package/plugins/litclaude/lib/session-state.mjs +180 -0
- package/plugins/litclaude/lib/workflow-stage.mjs +8 -1
- package/plugins/litclaude/scripts/goal-propose.mjs +29 -18
|
@@ -31,7 +31,7 @@ import { chipStates, litSkillId, noteSkill } from "../lib/mod/skills.mjs";
|
|
|
31
31
|
import { TOAST_GAP_MS, gateToast, statusChanges, statusOf } from "../lib/mod/toasts.mjs";
|
|
32
32
|
import { detectIconFont, readGitBranch, readInvited, readLedgerTail, readLitSnapshot, readPluginVersions } from "../lib/mod/state.mjs";
|
|
33
33
|
import { settledSample } from "../lib/mod/timeline.mjs";
|
|
34
|
-
import { GOAL_DIR, goalStep, transcriptPathFor } from "../lib/mod/goal.mjs";
|
|
34
|
+
import { GOAL_DIR, goalStep, sessionGoalDir, transcriptPathFor } from "../lib/mod/goal.mjs";
|
|
35
35
|
import {
|
|
36
36
|
EMPTY_ACTIVITY,
|
|
37
37
|
EMPTY_LISTS,
|
|
@@ -164,6 +164,15 @@ async function readGoalRecord($, path) {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
// The session's own record, else the legacy shared one, as { value, path }; writes go back to that path.
|
|
168
|
+
async function findGoalRecord($, dirs, name) {
|
|
169
|
+
for (const dir of dirs) {
|
|
170
|
+
const value = await readGoalRecord($, `${dir}/${name}.json`);
|
|
171
|
+
if (value) return { value, path: `${dir}/${name}.json` };
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
|
|
167
176
|
// One idle's goal work: read the records and the transcript, take the step goalStep picks, say it in
|
|
168
177
|
// its fixed status line. Runs after the turn settled (a hook the turn waits on cannot run a command).
|
|
169
178
|
async function goalTick($, live) {
|
|
@@ -171,28 +180,31 @@ async function goalTick($, live) {
|
|
|
171
180
|
live.goalBusy = true;
|
|
172
181
|
try {
|
|
173
182
|
const [cwd, sessionId] = await Promise.all([$.session.cwd(), $.session.id()]);
|
|
174
|
-
const
|
|
175
|
-
const
|
|
183
|
+
const own = sessionGoalDir(sessionId);
|
|
184
|
+
const dirs = own ? [`${cwd}/${own}`, `${cwd}/${GOAL_DIR}`] : [`${cwd}/${GOAL_DIR}`];
|
|
185
|
+
const found = await Promise.all(["intent", "owner", "clear"].map((name) => findGoalRecord($, dirs, name)));
|
|
186
|
+
const [intent, owner, clear] = found.map((record) => record?.value ?? null);
|
|
187
|
+
const [intentPath, ownerPath, clearPath] = found.map((record) => record?.path);
|
|
176
188
|
if ((!intent || intent.consumedAt) && (!owner || owner.closed)) return;
|
|
177
189
|
const transcript = live.configDir ? await $.fs.read(transcriptPathFor(live.configDir, cwd, sessionId)).catch(() => "") : "";
|
|
178
190
|
const step = goalStep({ intent, owner, clear, transcript, sessionId });
|
|
179
191
|
const now = new Date(await $.clock.now()).toISOString();
|
|
180
|
-
if (step.do === "skip-yours" || step.do === "set") await $.fs.write(
|
|
192
|
+
if (step.do === "skip-yours" || step.do === "set") await $.fs.write(intentPath, `${JSON.stringify({ ...intent, consumedAt: now })}\n`);
|
|
181
193
|
if (step.do === "set") {
|
|
182
194
|
try {
|
|
183
195
|
await $.command.run({ command: "goal", args: step.condition });
|
|
184
|
-
await $.fs.write(
|
|
196
|
+
await $.fs.write(intentPath.replace(/intent\.json$/u, "owner.json"), `${JSON.stringify({ version: 1, condition: intent.condition, hash: intent.hash, id: intent.id, plan: intent.plan, sessionId, setAt: now, confirmed: false })}\n`);
|
|
185
197
|
} catch {
|
|
186
198
|
$.ui.toast(`Goal not confirmed. Paste: /goal ${step.condition}`);
|
|
187
199
|
return;
|
|
188
200
|
}
|
|
189
201
|
}
|
|
190
|
-
if (step.do === "confirm") await $.fs.write(
|
|
202
|
+
if (step.do === "confirm") await $.fs.write(ownerPath, `${JSON.stringify({ ...owner, confirmed: true, confirmedAt: now })}\n`);
|
|
191
203
|
if (step.do === "clear") {
|
|
192
204
|
await $.command.run({ command: "goal", args: "clear" }).catch(() => null);
|
|
193
|
-
if (clear) await $.fs.write(
|
|
205
|
+
if (clear) await $.fs.write(clearPath, `${JSON.stringify({ ...clear, consumedAt: now })}\n`);
|
|
194
206
|
}
|
|
195
|
-
if (step.do === "close" || step.do === "clear") await $.fs.write(
|
|
207
|
+
if (step.do === "close" || step.do === "clear") await $.fs.write(ownerPath, `${JSON.stringify({ ...owner, closed: step.why, closedAt: now })}\n`);
|
|
196
208
|
if (step.status) $.ui.toast(step.status);
|
|
197
209
|
} catch {
|
|
198
210
|
// The goal record is best effort; the card keeps drawing.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// The native-goal records the prompt hook and goal-propose keep beside the session's cwd: consent (the
|
|
2
|
+
// question asked), intent (a yes the mod turns into /goal), owner (the goal the mod set) and clear (a
|
|
3
|
+
// request to clear it). Each session writes `.litclaude/sessions/<id>/goal/<name>`; a read falls back to
|
|
4
|
+
// the legacy shared `.litclaude/goal/<name>`, so a record written before the move is still honoured.
|
|
5
|
+
// A missing or unsafe session id keeps the legacy path for reads and writes alike.
|
|
6
|
+
import { readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { dirname, join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import { GOAL_DIR, sessionGoalDir } from "./mod/goal.mjs";
|
|
10
|
+
import { prepareStateDir, STATE_WRITE_FLAG } from "./project-state-root.mjs";
|
|
11
|
+
import { readWithLegacyFallback, sessionStateDir, sessionStatePath } from "./session-state.mjs";
|
|
12
|
+
|
|
13
|
+
const legacyGoalPath = (cwd, name) => join(cwd, GOAL_DIR, name);
|
|
14
|
+
const sessionGoalPath = (cwd, sessionId, name) => {
|
|
15
|
+
const dir = sessionGoalDir(sessionId);
|
|
16
|
+
return dir === null ? null : join(cwd, dir, name);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Where this session writes the record. */
|
|
20
|
+
export const goalWritePath = (cwd, sessionId, name) => sessionGoalPath(cwd, sessionId, name) ?? legacyGoalPath(cwd, name);
|
|
21
|
+
|
|
22
|
+
const goalRecord = (cwd, sessionId, name) => {
|
|
23
|
+
try {
|
|
24
|
+
const found = readWithLegacyFallback(sessionGoalPath(cwd, sessionId, name), legacyGoalPath(cwd, name));
|
|
25
|
+
return found ? { path: found.path, value: JSON.parse(found.text) } : null;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const readJson = (path) => {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const readGoalFile = (cwd, sessionId, name) => goalRecord(cwd, sessionId, name)?.value ?? null;
|
|
40
|
+
|
|
41
|
+
const writeAt = (target, value) => {
|
|
42
|
+
try {
|
|
43
|
+
if (!prepareStateDir(dirname(target))) return false;
|
|
44
|
+
writeFileSync(target, `${JSON.stringify(value)}\n`, { flag: STATE_WRITE_FLAG });
|
|
45
|
+
return true;
|
|
46
|
+
} catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const writeGoalFile = (cwd, sessionId, name, value) => writeAt(goalWritePath(cwd, sessionId, name), value);
|
|
52
|
+
|
|
53
|
+
/** Removes the record a read would return: the session's own, else the legacy one. */
|
|
54
|
+
export const dropGoalFile = (cwd, sessionId, name) => {
|
|
55
|
+
const found = goalRecord(cwd, sessionId, name);
|
|
56
|
+
try {
|
|
57
|
+
if (found) rmSync(found.path, { force: true });
|
|
58
|
+
} catch {
|
|
59
|
+
// Nothing to drop.
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// `lit cancel` may come from any session of the project, so the clear request lands beside every goal
|
|
64
|
+
// owner LitClaude recorded here: each session's and the legacy shared one.
|
|
65
|
+
export const requestGoalClear = (cwd, why) => {
|
|
66
|
+
if (!cwd) return false;
|
|
67
|
+
const dirs = [join(cwd, GOAL_DIR)];
|
|
68
|
+
try {
|
|
69
|
+
for (const name of readdirSync(join(cwd, ".litclaude", "sessions"))) {
|
|
70
|
+
const dir = sessionGoalDir(name);
|
|
71
|
+
if (dir !== null) dirs.push(join(cwd, dir));
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// No session folders yet.
|
|
75
|
+
}
|
|
76
|
+
const owned = dirs.filter((dir) => readJson(join(dir, "owner.json")));
|
|
77
|
+
return owned.map((dir) => writeAt(join(dir, "clear.json"), { version: 1, why, at: new Date().toISOString() })).some(Boolean);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// A lit run's goal question is asked once per session: `sessions/<id>/goal-asked.json` records when it
|
|
81
|
+
// was shown, and a later run asks again only after a goal LitClaude set has closed since then.
|
|
82
|
+
const GOAL_ASKED_FILE = "goal-asked.json";
|
|
83
|
+
|
|
84
|
+
/** Records that this session was shown the goal question; false when it cannot be recorded. */
|
|
85
|
+
export const markGoalAsked = (cwd, sessionId, now = new Date()) => {
|
|
86
|
+
try {
|
|
87
|
+
const dir = sessionStateDir(cwd, sessionId);
|
|
88
|
+
if (dir === null) return false;
|
|
89
|
+
writeFileSync(join(dir, GOAL_ASKED_FILE), `${JSON.stringify({ version: 1, askedAt: now.toISOString() })}\n`, { flag: STATE_WRITE_FLAG });
|
|
90
|
+
return true;
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** True when this session was shown the question and no goal closed after that (a closed owner with no time counts as after). */
|
|
97
|
+
export const goalAskedSinceClose = (cwd, sessionId, owner) => {
|
|
98
|
+
const path = sessionStatePath(cwd, sessionId);
|
|
99
|
+
const askedAt = path === null ? Number.NaN : Date.parse(readJson(join(path, GOAL_ASKED_FILE))?.askedAt ?? "");
|
|
100
|
+
if (!Number.isFinite(askedAt)) return false;
|
|
101
|
+
if (!owner?.closed) return true;
|
|
102
|
+
const closedAt = Date.parse(owner.closedAt ?? "");
|
|
103
|
+
return Number.isFinite(closedAt) && closedAt < askedAt;
|
|
104
|
+
};
|
|
@@ -8,10 +8,11 @@
|
|
|
8
8
|
// anything the hook cannot read are not clean. The block
|
|
9
9
|
// counter is the loop brake: at most INTERFACE_PROBE_MAX_BLOCKS blocks per turn, then the stop
|
|
10
10
|
// passes with a warning. Any unreadable state fails SAFE (allow).
|
|
11
|
-
import {
|
|
11
|
+
import { readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
12
12
|
import { extname, isAbsolute, join } from "node:path";
|
|
13
13
|
|
|
14
14
|
import { requireStateDir, resolveProjectStateRoot, STATE_WRITE_FLAG } from "./project-state-root.mjs";
|
|
15
|
+
import { clearSessionRecord, readWithLegacyFallback, sessionStateDir, sessionStatePath } from "./session-state.mjs";
|
|
15
16
|
|
|
16
17
|
export const INTERFACE_PROBE_MAX_BLOCKS = 2;
|
|
17
18
|
// start-work is left out: its prompt turn must stay side-effect-free, and the plan rows it runs
|
|
@@ -26,12 +27,19 @@ const OUT_ARGUMENT = /interface-probe\.mjs[^\n;&|]*?--out[=\s]+(?:"([^"]+)"|'([^
|
|
|
26
27
|
const MATRIX_ROWS = 7;
|
|
27
28
|
const MAX_PATHS = 5;
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
// Each session keeps its own turn; the shared legacy path is still read when the session has none.
|
|
31
|
+
const TURN_FILE = "interface-probe-turn.json";
|
|
32
|
+
const legacyTurnPath = (cwd) => join(resolveProjectStateRoot(cwd), ".litclaude", "interface-probe", "turn.json");
|
|
30
33
|
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
export const interfaceProbeTurnPath = (cwd = resolveProjectStateRoot(), sessionId = null) => {
|
|
35
|
+
const dir = sessionStatePath(cwd, sessionId);
|
|
36
|
+
return dir === null ? legacyTurnPath(cwd) : join(dir, TURN_FILE);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const readTurn = (cwd, sessionId) => {
|
|
40
|
+
const found = readWithLegacyFallback(interfaceProbeTurnPath(cwd, sessionId), legacyTurnPath(cwd));
|
|
41
|
+
if (!found) return null;
|
|
42
|
+
const parsed = JSON.parse(found.text);
|
|
35
43
|
if (!parsed || typeof parsed !== "object" || typeof parsed.sessionId !== "string" || !Number.isInteger(parsed.blockCount) || parsed.blockCount < 0) {
|
|
36
44
|
throw new Error("invalid interface-probe turn state");
|
|
37
45
|
}
|
|
@@ -39,8 +47,9 @@ const readTurn = (cwd) => {
|
|
|
39
47
|
};
|
|
40
48
|
|
|
41
49
|
const writeTurn = (cwd, state) => {
|
|
42
|
-
const
|
|
43
|
-
|
|
50
|
+
const dir = sessionStateDir(cwd, state.sessionId);
|
|
51
|
+
const target = dir === null ? legacyTurnPath(cwd) : join(dir, TURN_FILE);
|
|
52
|
+
if (dir === null) requireStateDir(join(target, ".."));
|
|
44
53
|
const tmp = `${target}.tmp`;
|
|
45
54
|
writeFileSync(tmp, JSON.stringify(state), { flag: STATE_WRITE_FLAG });
|
|
46
55
|
renameSync(tmp, target);
|
|
@@ -50,7 +59,7 @@ const writeTurn = (cwd, state) => {
|
|
|
50
59
|
// Every prompt re-arms or clears the turn, so the gate never outlives the turn that armed it.
|
|
51
60
|
export const recordInterfaceProbeTurn = (cwd, { sessionId, discipline, now = Date.now() }) => {
|
|
52
61
|
if (!INTERFACE_PROBE_WORKFLOWS.includes(discipline)) {
|
|
53
|
-
|
|
62
|
+
clearSessionRecord(interfaceProbeTurnPath(cwd, sessionId), legacyTurnPath(cwd), sessionId);
|
|
54
63
|
return null;
|
|
55
64
|
}
|
|
56
65
|
return writeTurn(cwd, { sessionId: typeof sessionId === "string" ? sessionId : "", discipline, promptAt: now, editAt: null, paths: [], probeAt: null, issue: null, issueAt: null, blockCount: 0, warned: false });
|
|
@@ -87,7 +96,7 @@ const probeIssue = ({ output, command, workdir, editAt }) => {
|
|
|
87
96
|
export const observeInterfaceProbeTool = (cwd, { input, filePaths, now = Date.now() }) => {
|
|
88
97
|
let turn;
|
|
89
98
|
try {
|
|
90
|
-
turn = readTurn(cwd);
|
|
99
|
+
turn = readTurn(cwd, input.session_id);
|
|
91
100
|
} catch {
|
|
92
101
|
return null;
|
|
93
102
|
}
|
|
@@ -106,7 +115,7 @@ export const observeInterfaceProbeTool = (cwd, { input, filePaths, now = Date.no
|
|
|
106
115
|
};
|
|
107
116
|
|
|
108
117
|
export const evaluateInterfaceProbeStop = ({ cwd, sessionId, now = Date.now() }) => {
|
|
109
|
-
const turn = readTurn(cwd);
|
|
118
|
+
const turn = readTurn(cwd, sessionId);
|
|
110
119
|
if (!turn || turn.sessionId !== sessionId || turn.editAt === null) return { action: "allow", why: "no-interface-edit" };
|
|
111
120
|
if (turn.probeAt !== null && turn.probeAt >= turn.editAt) return { action: "allow", why: "probed" };
|
|
112
121
|
const detail = turn.issue && turn.issueAt >= turn.editAt
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
// lit-plan persistence gate. UserPromptSubmit records a lit-plan turn at
|
|
2
|
-
// `.litclaude/lit-plan
|
|
2
|
+
// `.litclaude/sessions/<session>/lit-plan-turn.json` (the shared `.litclaude/lit-plan/turn.json` is
|
|
3
|
+
// still read when the session has none); the Stop hook then refuses to end the turn until a
|
|
3
4
|
// `plans/*.md` newer than the prompt carries at least one `- [ ] N.` task row. The
|
|
4
5
|
// block counter is the loop brake: at most LIT_PLAN_MAX_BLOCKS blocks per session,
|
|
5
6
|
// after which the stop passes with a warning. Any unreadable state fails SAFE (allow).
|
|
6
|
-
import {
|
|
7
|
+
import { readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
7
8
|
import { join, resolve } from "node:path";
|
|
8
9
|
|
|
9
10
|
import { describeScopePath, uncoveredScopePaths } from "./plan-scope-roots.mjs";
|
|
10
11
|
import { requireStateDir, resolveProjectStateRoot, STATE_WRITE_FLAG } from "./project-state-root.mjs";
|
|
11
12
|
import { findProjectRoot } from "./rules/discovery.mjs";
|
|
13
|
+
import { clearSessionRecord, readWithLegacyFallback, sessionStateDir, sessionStatePath } from "./session-state.mjs";
|
|
12
14
|
|
|
13
15
|
export const LIT_PLAN_MAX_BLOCKS = 2;
|
|
14
16
|
export const LIT_PLAN_BLOCK_REASON = "lit-plan must persist plans/<slug>.md (run scaffold-plan.mjs) before finishing";
|
|
@@ -18,12 +20,19 @@ export const LIT_PLAN_SCOPE_REASON = "lit-plan must grant the outside folders it
|
|
|
18
20
|
const MTIME_TOLERANCE_MS = 1000;
|
|
19
21
|
const TASK_ROW = /^- \[ \] \d+\./mu;
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
const TURN_FILE = "lit-plan-turn.json";
|
|
24
|
+
const legacyTurnPath = (cwd) => join(resolveProjectStateRoot(cwd), ".litclaude", "lit-plan", "turn.json");
|
|
22
25
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
/** The session's turn record; the legacy shared path when the session id is missing or unsafe. */
|
|
27
|
+
export const litPlanTurnPath = (cwd = resolveProjectStateRoot(), sessionId = null) => {
|
|
28
|
+
const dir = sessionStatePath(cwd, sessionId);
|
|
29
|
+
return dir === null ? legacyTurnPath(cwd) : join(dir, TURN_FILE);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const readTurn = (cwd, sessionId) => {
|
|
33
|
+
const found = readWithLegacyFallback(litPlanTurnPath(cwd, sessionId), legacyTurnPath(cwd));
|
|
34
|
+
if (!found) return null;
|
|
35
|
+
const parsed = JSON.parse(found.text);
|
|
27
36
|
if (
|
|
28
37
|
!parsed
|
|
29
38
|
|| typeof parsed !== "object"
|
|
@@ -38,8 +47,9 @@ const readTurn = (cwd) => {
|
|
|
38
47
|
};
|
|
39
48
|
|
|
40
49
|
const writeTurn = (cwd, state) => {
|
|
41
|
-
const
|
|
42
|
-
|
|
50
|
+
const dir = sessionStateDir(cwd, state.sessionId);
|
|
51
|
+
const target = dir === null ? legacyTurnPath(cwd) : join(dir, TURN_FILE);
|
|
52
|
+
if (dir === null) requireStateDir(join(target, ".."));
|
|
43
53
|
const tmp = `${target}.tmp`;
|
|
44
54
|
writeFileSync(tmp, JSON.stringify(state), { flag: STATE_WRITE_FLAG });
|
|
45
55
|
renameSync(tmp, target);
|
|
@@ -49,7 +59,7 @@ const writeTurn = (cwd, state) => {
|
|
|
49
59
|
export const recordLitPlanTurn = (cwd, { sessionId, now = Date.now() }) => {
|
|
50
60
|
let previous = null;
|
|
51
61
|
try {
|
|
52
|
-
previous = readTurn(cwd);
|
|
62
|
+
previous = readTurn(cwd, sessionId);
|
|
53
63
|
} catch {
|
|
54
64
|
previous = null;
|
|
55
65
|
}
|
|
@@ -62,8 +72,8 @@ export const recordLitPlanTurn = (cwd, { sessionId, now = Date.now() }) => {
|
|
|
62
72
|
});
|
|
63
73
|
};
|
|
64
74
|
|
|
65
|
-
export const clearLitPlanTurn = (cwd) => {
|
|
66
|
-
|
|
75
|
+
export const clearLitPlanTurn = (cwd, sessionId = null) => {
|
|
76
|
+
clearSessionRecord(litPlanTurnPath(cwd, sessionId), legacyTurnPath(cwd), sessionId);
|
|
67
77
|
};
|
|
68
78
|
|
|
69
79
|
const newestPlanAfter = (directory, promptAt) => {
|
|
@@ -98,7 +108,7 @@ const planDirectories = (cwd) => {
|
|
|
98
108
|
};
|
|
99
109
|
|
|
100
110
|
export const evaluateLitPlanStop = ({ cwd, sessionId, now = Date.now() }) => {
|
|
101
|
-
const turn = readTurn(cwd);
|
|
111
|
+
const turn = readTurn(cwd, sessionId);
|
|
102
112
|
if (!turn || turn.sessionId !== sessionId) return { action: "allow", why: "no-lit-plan-turn" };
|
|
103
113
|
|
|
104
114
|
let newest = null;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { closeSync, existsSync, fsyncSync, lstatSync, openSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
3
|
import { dirname } from "node:path";
|
|
3
4
|
|
|
5
|
+
import { acquireOwnerLock, OwnerLockError, readOwnerLock, releaseOwnerLock } from "../owner-lock.mjs";
|
|
4
6
|
import { requireStateDir, STATE_WRITE_FLAG } from "../project-state-root.mjs";
|
|
7
|
+
import { directoryIdentity, sameDirectoryIdentity } from "../secure-path-read.mjs";
|
|
5
8
|
|
|
6
9
|
export class LitgoalStateError extends Error {
|
|
7
10
|
constructor(message, status = 65) {
|
|
@@ -42,28 +45,45 @@ export const writeLitgoalState = (path, state) => {
|
|
|
42
45
|
}
|
|
43
46
|
};
|
|
44
47
|
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
// A lock older than this whose owner is dead, or that has no owner record at all, is recovered.
|
|
49
|
+
export const LITGOAL_LOCK_TIMEOUT_MS = 5000;
|
|
50
|
+
|
|
51
|
+
// Locks taken before the owner-lock switch are bare folders with no owner record, so the owner-lock
|
|
52
|
+
// takeover cannot judge them and a crashed run left one that blocked every later litgoal call. An
|
|
53
|
+
// ownerless folder past the timeout is moved aside to `.lock.stale.<id>` (kept, not deleted); if the
|
|
54
|
+
// rename caught a different folder, it is put back.
|
|
55
|
+
const moveOwnerlessLockAside = (lockDir) => {
|
|
56
|
+
const stats = lstatSync(lockDir, { throwIfNoEntry: false });
|
|
57
|
+
if (!stats || stats.isSymbolicLink() || !stats.isDirectory()) return;
|
|
58
|
+
if (Date.now() - stats.mtimeMs < LITGOAL_LOCK_TIMEOUT_MS || readOwnerLock(lockDir)) return;
|
|
59
|
+
const aside = `${lockDir}.stale.${randomUUID()}`;
|
|
60
|
+
try {
|
|
61
|
+
renameSync(lockDir, aside);
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const moved = lstatSync(aside, { throwIfNoEntry: false });
|
|
66
|
+
if (moved && sameDirectoryIdentity(directoryIdentity(moved), directoryIdentity(stats)) && !readOwnerLock(aside)) return;
|
|
67
|
+
try {
|
|
68
|
+
if (!existsSync(lockDir)) renameSync(aside, lockDir);
|
|
69
|
+
} catch {
|
|
70
|
+
// The aside folder stays for inspection.
|
|
71
|
+
}
|
|
47
72
|
};
|
|
48
73
|
|
|
49
74
|
export const withLitgoalLock = (lockDir, fn) => {
|
|
50
75
|
requireStateDir(dirname(lockDir));
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
throw new LitgoalStateError("litgoal state lock timed out");
|
|
59
|
-
}
|
|
60
|
-
sleepSync(20);
|
|
61
|
-
}
|
|
76
|
+
moveOwnerlessLockAside(lockDir);
|
|
77
|
+
let owner;
|
|
78
|
+
try {
|
|
79
|
+
owner = acquireOwnerLock(lockDir, { timeoutMs: LITGOAL_LOCK_TIMEOUT_MS, staleMs: LITGOAL_LOCK_TIMEOUT_MS });
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (error instanceof OwnerLockError) throw new LitgoalStateError(`litgoal ${error.message}`);
|
|
82
|
+
throw error;
|
|
62
83
|
}
|
|
63
|
-
|
|
64
84
|
try {
|
|
65
85
|
return fn();
|
|
66
86
|
} finally {
|
|
67
|
-
|
|
87
|
+
releaseOwnerLock(lockDir, owner);
|
|
68
88
|
}
|
|
69
89
|
};
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
// what the transcript says about goals, and the one step to take at an idle. No Node built-ins, so the
|
|
3
3
|
// mods loader can import it; lib/goal-pipeline.mjs re-exports it for the hook and the CLI.
|
|
4
4
|
export const GOAL_DIR = ".litclaude/goal";
|
|
5
|
+
// Each session keeps its own goal records under `.litclaude/sessions/<id>/goal`; the shared GOAL_DIR
|
|
6
|
+
// above is the legacy place, still read when a session has no record of its own.
|
|
7
|
+
export const SESSION_STATE_ID = /^[A-Za-z0-9-]{1,80}$/u;
|
|
8
|
+
export const sessionGoalDir = (sessionId) => (typeof sessionId === "string" && SESSION_STATE_ID.test(sessionId) ? `.litclaude/sessions/${sessionId}/goal` : null);
|
|
5
9
|
|
|
6
10
|
// Fixed status lines, one per outcome. The mod and the CLI print these and nothing else.
|
|
7
11
|
export const GOAL_STATUS = Object.freeze({
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Stop receipt gate. A session that ran lit-loop, litwork or start-work and changed code should end
|
|
2
|
+
// with a passing check of that code (the receipts in lib/receipts.mjs). A code change is an edit-tool
|
|
3
|
+
// timestamp or a code fingerprint that differs from the one taken when the discipline started. When
|
|
4
|
+
// the fingerprint is available, the passing receipt's codeSha must match the tree at stop; otherwise
|
|
5
|
+
// the receipt has to be newer than the edit. `receiptGate` in
|
|
6
|
+
// LitClaude's config.json picks what happens when it does not: `off`; `shadow` (the default) warns in
|
|
7
|
+
// a systemMessage and logs a line to `sessions/<sid>/receipt-gate.jsonl`; `enforce` blocks the stop
|
|
8
|
+
// naming the check, at most RECEIPT_GATE_MAX_BLOCKS times per prompt_id, then ends the session with
|
|
9
|
+
// `continue: false`. A Stop the host re-entered (`stop_hook_active`) is never blocked. Doc-only
|
|
10
|
+
// sessions and projects with no check stay silent, and any unreadable state lets the stop through:
|
|
11
|
+
// the caller wraps this in a catch, and a block is logged before it is returned.
|
|
12
|
+
import { closeSync, openSync, readFileSync, writeSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
|
|
15
|
+
import { markLitHome, readLitConfigKey, writeLitConfigKey } from "./mark.mjs";
|
|
16
|
+
import { STATE_APPEND_FLAG, resolveProjectStateRoot } from "./project-state-root.mjs";
|
|
17
|
+
import { codeShaOf, readReceiptState } from "./receipts.mjs";
|
|
18
|
+
import { sessionStateDir } from "./session-state.mjs";
|
|
19
|
+
import { readStack } from "./workflow-stack.mjs";
|
|
20
|
+
|
|
21
|
+
export const RECEIPT_GATE_MODES = Object.freeze(["off", "shadow", "enforce"]);
|
|
22
|
+
export const RECEIPT_GATE_MAX_BLOCKS = 2;
|
|
23
|
+
export const RECEIPT_GATE_LOG = "receipt-gate.jsonl";
|
|
24
|
+
const ACTIVATING_SKILLS = new Set(["lit-loop", "litwork", "start-work"]);
|
|
25
|
+
|
|
26
|
+
export const readReceiptGateMode = ({ env = process.env, litHome = markLitHome(env) } = {}) => {
|
|
27
|
+
const stored = readLitConfigKey(litHome, "receiptGate");
|
|
28
|
+
return RECEIPT_GATE_MODES.includes(stored) ? stored : "shadow";
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const writeReceiptGateMode = (litHome, mode) => {
|
|
32
|
+
if (!RECEIPT_GATE_MODES.includes(mode)) throw new Error(`receiptGate takes one of: ${RECEIPT_GATE_MODES.join(", ")}`);
|
|
33
|
+
return writeLitConfigKey(litHome, "receiptGate", mode);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const sessionActivated = (cwd, sessionId) => {
|
|
37
|
+
const { frames, history } = readStack(cwd, sessionId);
|
|
38
|
+
return [...frames, ...history].some((entry) => ACTIVATING_SKILLS.has(entry?.skill));
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const readGateLog = (dir) => {
|
|
42
|
+
try {
|
|
43
|
+
return readFileSync(join(dir, RECEIPT_GATE_LOG), "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line));
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error?.code === "ENOENT") return [];
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const logDecision = (dir, record) => {
|
|
51
|
+
const fd = openSync(join(dir, RECEIPT_GATE_LOG), STATE_APPEND_FLAG, 0o600);
|
|
52
|
+
try {
|
|
53
|
+
writeSync(fd, `${JSON.stringify(record)}\n`);
|
|
54
|
+
} finally {
|
|
55
|
+
closeSync(fd);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `{ action: "none" }`, `{ action: "warn", systemMessage }`, `{ action: "block", reason }` or
|
|
61
|
+
* `{ action: "stop", stopReason }` for one Stop. `detectCheck` names the project's check.
|
|
62
|
+
*/
|
|
63
|
+
export const evaluateReceiptGate = ({ cwd, input, mode, detectCheck, now = new Date() }) => {
|
|
64
|
+
const none = { action: "none" };
|
|
65
|
+
const sessionId = input?.session_id;
|
|
66
|
+
if (mode === "off" || !RECEIPT_GATE_MODES.includes(mode)) return none;
|
|
67
|
+
if (mode === "enforce" && input?.stop_hook_active === true) return none;
|
|
68
|
+
if (!sessionActivated(cwd, sessionId)) return none;
|
|
69
|
+
const { receipts, lastCodeEditAt, baselineCodeSha } = readReceiptState(cwd, sessionId);
|
|
70
|
+
let currentCodeSha = null;
|
|
71
|
+
try {
|
|
72
|
+
currentCodeSha = codeShaOf(cwd);
|
|
73
|
+
} catch {
|
|
74
|
+
currentCodeSha = null;
|
|
75
|
+
}
|
|
76
|
+
const fingerprint = typeof currentCodeSha === "string" && typeof baselineCodeSha === "string";
|
|
77
|
+
const edited = Number.isFinite(lastCodeEditAt);
|
|
78
|
+
const drifted = fingerprint && currentCodeSha !== baselineCodeSha;
|
|
79
|
+
if (!edited && !drifted) return none;
|
|
80
|
+
const passed = fingerprint
|
|
81
|
+
? receipts.some((receipt) => receipt.exit === 0 && receipt.codeSha === currentCodeSha)
|
|
82
|
+
: edited && receipts.some((receipt) => receipt.exit === 0 && Date.parse(receipt.at) > lastCodeEditAt);
|
|
83
|
+
if (passed) return none;
|
|
84
|
+
const check = detectCheck(cwd) ?? detectCheck(resolveProjectStateRoot(cwd)) ?? receipts.findLast((receipt) => typeof receipt.check === "string")?.check ?? null;
|
|
85
|
+
if (check === null) return none;
|
|
86
|
+
const dir = sessionStateDir(cwd, sessionId);
|
|
87
|
+
if (dir === null) return none;
|
|
88
|
+
const promptId = typeof input.prompt_id === "string" ? input.prompt_id : null;
|
|
89
|
+
const at = now.toISOString();
|
|
90
|
+
if (mode === "shadow") {
|
|
91
|
+
logDecision(dir, { at, mode, action: "warn", check, promptId });
|
|
92
|
+
return { action: "warn", systemMessage: `LitClaude: code changed after the last passing ${check} run; no passing check since.` };
|
|
93
|
+
}
|
|
94
|
+
const blocks = readGateLog(dir).filter((entry) => entry.action === "block" && entry.promptId === promptId).length;
|
|
95
|
+
if (blocks < RECEIPT_GATE_MAX_BLOCKS) {
|
|
96
|
+
logDecision(dir, { at, mode, action: "block", check, promptId });
|
|
97
|
+
return { action: "block", reason: `LitClaude receipt gate: code changed after the last passing check. Run \`${check}\` on its own and fix what fails before finishing.` };
|
|
98
|
+
}
|
|
99
|
+
logDecision(dir, { at, mode, action: "stop", check, promptId });
|
|
100
|
+
return { action: "stop", stopReason: `LitClaude: stopped after ${RECEIPT_GATE_MAX_BLOCKS} reminders; code changed and \`${check}\` has not passed since.` };
|
|
101
|
+
};
|