@lastweek-work/cli 0.3.1 → 0.3.2
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/bursts.js +14 -3
- package/dist/ingest.js +5 -1
- package/dist/state.js +49 -2
- package/dist/tasks.js +13 -7
- package/package.json +1 -1
package/dist/bursts.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync,
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { applyCall, burstToEvent, isIdle } from "./hook.js";
|
|
4
4
|
import { appendSpool } from "./spool.js";
|
|
5
|
-
import { ensureStateDir, stateDir } from "./state.js";
|
|
5
|
+
import { ensureStateDir, stateDir, writeAtomic } from "./state.js";
|
|
6
6
|
/** One file per Claude Code session holding its open burst. Survives restarts; nothing is lost if the Mac dies. */
|
|
7
7
|
const dir = () => join(stateDir(), "sessions");
|
|
8
8
|
const file = (session) => join(dir(), `${session.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 100)}.json`);
|
|
@@ -19,6 +19,17 @@ export function openBursts() {
|
|
|
19
19
|
return [];
|
|
20
20
|
const out = [];
|
|
21
21
|
for (const f of readdirSync(dir())) {
|
|
22
|
+
if (!f.endsWith(".json")) {
|
|
23
|
+
// Another process's temp file mid-write, or one a killed process left: the latter goes after 5 minutes.
|
|
24
|
+
try {
|
|
25
|
+
if (Date.now() - statSync(join(dir(), f)).mtimeMs > 5 * 60_000)
|
|
26
|
+
rmSync(join(dir(), f), { force: true });
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// renamed or removed meanwhile
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
22
33
|
try {
|
|
23
34
|
out.push(JSON.parse(readFileSync(join(dir(), f), "utf8")));
|
|
24
35
|
}
|
|
@@ -33,7 +44,7 @@ export function recordCall(call, now) {
|
|
|
33
44
|
ensureStateDir();
|
|
34
45
|
mkdirSync(dir(), { recursive: true });
|
|
35
46
|
const { burst, done } = applyCall(load(call.session), call, now);
|
|
36
|
-
|
|
47
|
+
writeAtomic(file(call.session), JSON.stringify(burst));
|
|
37
48
|
const finished = done ? [done] : [];
|
|
38
49
|
return { spooled: finished.length + spoolIdle(now, call.session, finished) };
|
|
39
50
|
}
|
package/dist/ingest.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/** The server's text limits (ingest EventSchema): a long commit subject is cut here instead of refused there. */
|
|
2
2
|
export function clip(e) {
|
|
3
|
+
// Reflog subjects can carry a remote URL with credentials ("pull https://user:TOKEN@host/repo",
|
|
4
|
+
// "https://TOKEN@host"): never send those.
|
|
5
|
+
const summary = e.summary?.replace(/\/\/[^\s/@]+@/g, "//"); // also token-only: https://ghp_x@github.com
|
|
3
6
|
return {
|
|
4
7
|
...e,
|
|
5
|
-
...(
|
|
8
|
+
...(summary !== e.summary ? { summary } : {}),
|
|
9
|
+
...(summary && summary.length > 1000 ? { summary: summary.slice(0, 1000) } : {}),
|
|
6
10
|
...(e.ref && e.ref.length > 256 ? { ref: e.ref.slice(0, 256) } : {}),
|
|
7
11
|
};
|
|
8
12
|
}
|
package/dist/state.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { preferLegacy } from "./paths.js";
|
|
@@ -37,5 +37,52 @@ export function readLastScan() {
|
|
|
37
37
|
return Number.isNaN(Date.parse(v)) ? null : v;
|
|
38
38
|
}
|
|
39
39
|
export function writeLastScan(iso) {
|
|
40
|
-
|
|
40
|
+
writeAtomic(join(ensureStateDir(), "last_scan"), `${iso}\n`);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Replaces a file in one step (write a temp file, then rename): a reader in another hook or MCP process sees the old
|
|
44
|
+
* content or the new one, never half a file. Temp names end in ".tmp" so folder listings can skip them.
|
|
45
|
+
*/
|
|
46
|
+
export function writeAtomic(path, content) {
|
|
47
|
+
const tmp = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
48
|
+
try {
|
|
49
|
+
writeFileSync(tmp, content);
|
|
50
|
+
renameSync(tmp, path);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
rmSync(tmp, { force: true }); // a full disk mustn't leave summaries lying around
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Runs `fn` while holding `<path>.lock` (exclusive create), so read-change-write of one file by several processes
|
|
59
|
+
* (one MCP server per coding session) loses nobody's change. A lock older than 10 s belongs to a process that died.
|
|
60
|
+
*/
|
|
61
|
+
export function withFileLock(path, fn) {
|
|
62
|
+
const lock = `${path}.lock`;
|
|
63
|
+
const sleep = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
64
|
+
for (let tries = 0;; tries++) {
|
|
65
|
+
try {
|
|
66
|
+
closeSync(openSync(lock, "wx"));
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
try {
|
|
71
|
+
if (Date.now() - statSync(lock).mtimeMs > 10_000)
|
|
72
|
+
rmSync(lock, { force: true });
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// gone meanwhile: try again
|
|
76
|
+
}
|
|
77
|
+
if (tries > 200)
|
|
78
|
+
throw new Error("state file busy");
|
|
79
|
+
sleep(10);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
return fn();
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
rmSync(lock, { force: true });
|
|
87
|
+
}
|
|
41
88
|
}
|
package/dist/tasks.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { existsSync, readFileSync
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
-
import { ensureStateDir, stateDir } from "./state.js";
|
|
4
|
+
import { ensureStateDir, stateDir, withFileLock, writeAtomic } from "./state.js";
|
|
5
5
|
/** The server refuses longer intervals (ingest MAX_SPAN_MS); checked here too so the model is told at once. */
|
|
6
6
|
export const MAX_SPAN_MS = 8 * 3_600_000;
|
|
7
7
|
const MAX_SKEW_MS = 5 * 60_000;
|
|
@@ -21,11 +21,12 @@ const path = () => join(stateDir(), "tasks.json");
|
|
|
21
21
|
const read = () => (existsSync(path()) ? JSON.parse(readFileSync(path(), "utf8")) : []);
|
|
22
22
|
const write = (tasks) => {
|
|
23
23
|
ensureStateDir();
|
|
24
|
-
|
|
24
|
+
writeAtomic(path(), JSON.stringify(tasks, null, 2));
|
|
25
25
|
};
|
|
26
26
|
export function startTask(summary, repo, now, agent) {
|
|
27
27
|
const task = { id: randomUUID().slice(0, 8), summary, repo, started_at: now.toISOString(), agent };
|
|
28
|
-
|
|
28
|
+
ensureStateDir();
|
|
29
|
+
withFileLock(path(), () => write([...read(), task])); // two sessions starting at once both keep their task
|
|
29
30
|
return task;
|
|
30
31
|
}
|
|
31
32
|
export function openTasks() {
|
|
@@ -36,11 +37,16 @@ export function openTasks() {
|
|
|
36
37
|
* The summary given at the end, if any, replaces the one from the start ("outcome, not intent").
|
|
37
38
|
*/
|
|
38
39
|
export function endTask(id, summary, now) {
|
|
39
|
-
|
|
40
|
-
const task =
|
|
40
|
+
ensureStateDir();
|
|
41
|
+
const task = withFileLock(path(), () => {
|
|
42
|
+
const tasks = read();
|
|
43
|
+
const t = id ? tasks.find((x) => x.id === id) : tasks.at(-1);
|
|
44
|
+
if (t)
|
|
45
|
+
write(tasks.filter((x) => x !== t));
|
|
46
|
+
return t;
|
|
47
|
+
});
|
|
41
48
|
if (!task)
|
|
42
49
|
return null;
|
|
43
|
-
write(tasks.filter((t) => t !== task));
|
|
44
50
|
const end = now.toISOString();
|
|
45
51
|
return {
|
|
46
52
|
source: "mcp",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lastweek-work/cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "The lastweek agent: collects git and coding-agent evidence on your machine and sends it to your lastweek account.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"homepage": "https://lastweek.work",
|