@lastweek-work/cli 0.0.0-stage → 0.1.1
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 +87 -2
- package/dist/bursts.js +47 -0
- package/dist/cli.js +329 -0
- package/dist/config.js +45 -0
- package/dist/connect.js +285 -0
- package/dist/git.js +184 -0
- package/dist/hook.js +59 -0
- package/dist/ingest.js +22 -0
- package/dist/mcp.js +61 -0
- package/dist/paths.js +6 -0
- package/dist/schedule.js +79 -0
- package/dist/spool.js +88 -0
- package/dist/state.js +27 -0
- package/dist/tasks.js +39 -0
- package/package.json +50 -4
package/dist/schedule.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { stateDir } from "./state.js";
|
|
6
|
+
/** macOS launchd job that runs `lastweek scan` regularly, so nobody has to remember it. */
|
|
7
|
+
export const LABEL = "work.lastweek.scan";
|
|
8
|
+
/** The job's name before the rename; installing or uninstalling removes it too, so it never runs twice. */
|
|
9
|
+
const LEGACY_LABEL = "dev.rewind.scan";
|
|
10
|
+
const plistFor = (label) => join(homedir(), "Library", "LaunchAgents", `${label}.plist`);
|
|
11
|
+
export const plistPath = () => plistFor(LABEL);
|
|
12
|
+
const esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
13
|
+
export function plist(args) {
|
|
14
|
+
const program = [args.node, args.cli, "scan"].map((a) => ` <string>${esc(a)}</string>`).join("\n");
|
|
15
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
16
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
17
|
+
<plist version="1.0">
|
|
18
|
+
<dict>
|
|
19
|
+
<key>Label</key>
|
|
20
|
+
<string>${LABEL}</string>
|
|
21
|
+
<key>ProgramArguments</key>
|
|
22
|
+
<array>
|
|
23
|
+
${program}
|
|
24
|
+
</array>
|
|
25
|
+
<key>StartInterval</key>
|
|
26
|
+
<integer>${args.everySeconds}</integer>
|
|
27
|
+
<key>RunAtLoad</key>
|
|
28
|
+
<true/>
|
|
29
|
+
<key>EnvironmentVariables</key>
|
|
30
|
+
<dict>
|
|
31
|
+
<key>PATH</key>
|
|
32
|
+
<string>${esc(args.path)}</string>
|
|
33
|
+
</dict>
|
|
34
|
+
<key>StandardOutPath</key>
|
|
35
|
+
<string>${esc(join(args.logDir, "scan.log"))}</string>
|
|
36
|
+
<key>StandardErrorPath</key>
|
|
37
|
+
<string>${esc(join(args.logDir, "scan.log"))}</string>
|
|
38
|
+
</dict>
|
|
39
|
+
</plist>
|
|
40
|
+
`;
|
|
41
|
+
}
|
|
42
|
+
const uid = () => String(process.getuid?.() ?? "");
|
|
43
|
+
export function installSchedule(cli, everyMinutes) {
|
|
44
|
+
if (process.platform !== "darwin")
|
|
45
|
+
throw new Error("lastweek schedule uses launchd and only works on macOS");
|
|
46
|
+
mkdirSync(join(homedir(), "Library", "LaunchAgents"), { recursive: true });
|
|
47
|
+
mkdirSync(stateDir(), { recursive: true });
|
|
48
|
+
uninstallSchedule(); // replace any previous version
|
|
49
|
+
writeFileSync(plistPath(),
|
|
50
|
+
// launchd starts jobs with a minimal PATH; git must be findable.
|
|
51
|
+
plist({
|
|
52
|
+
node: process.execPath,
|
|
53
|
+
cli,
|
|
54
|
+
everySeconds: everyMinutes * 60,
|
|
55
|
+
logDir: stateDir(),
|
|
56
|
+
path: process.env.PATH ?? "/usr/bin:/bin",
|
|
57
|
+
}));
|
|
58
|
+
execFileSync("launchctl", ["bootstrap", `gui/${uid()}`, plistPath()]);
|
|
59
|
+
return plistPath();
|
|
60
|
+
}
|
|
61
|
+
export function uninstallSchedule() {
|
|
62
|
+
let removed = false;
|
|
63
|
+
for (const label of [LABEL, LEGACY_LABEL]) {
|
|
64
|
+
if (!existsSync(plistFor(label)))
|
|
65
|
+
continue;
|
|
66
|
+
try {
|
|
67
|
+
execFileSync("launchctl", ["bootout", `gui/${uid()}/${label}`], { stdio: "ignore" });
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// not loaded
|
|
71
|
+
}
|
|
72
|
+
rmSync(plistFor(label), { force: true });
|
|
73
|
+
removed = true;
|
|
74
|
+
}
|
|
75
|
+
return removed;
|
|
76
|
+
}
|
|
77
|
+
/** The plist that is actually there (the old job counts until the next install replaces it), or null. */
|
|
78
|
+
export const installedPlist = () => [plistPath(), plistFor(LEGACY_LABEL)].find((p) => existsSync(p)) ?? null;
|
|
79
|
+
export const scheduleInstalled = () => installedPlist() !== null;
|
package/dist/spool.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { stateDir } from "./state.js";
|
|
4
|
+
/**
|
|
5
|
+
* Local outbox. Events are appended to spool.jsonl before any network call, so being offline or getting a 5xx never
|
|
6
|
+
* loses evidence. Flushing is lock-free: the spool is atomically renamed to a per-process batch file, sent, and only
|
|
7
|
+
* put back on failure. Concurrent appends land in a fresh spool.jsonl. Re-sending is safe: the server dedupes.
|
|
8
|
+
*/
|
|
9
|
+
const SPOOL = "spool.jsonl";
|
|
10
|
+
const STALE_BATCH_MS = 5 * 60_000; // a batch file this old belongs to a flush that died
|
|
11
|
+
const spoolPath = () => join(stateDir(), SPOOL);
|
|
12
|
+
export function appendSpool(events) {
|
|
13
|
+
if (events.length === 0)
|
|
14
|
+
return;
|
|
15
|
+
mkdirSync(stateDir(), { recursive: true });
|
|
16
|
+
// One write per call; O_APPEND keeps concurrent writers' lines intact.
|
|
17
|
+
appendFileSync(spoolPath(), events.map((e) => `${JSON.stringify(e)}\n`).join(""));
|
|
18
|
+
}
|
|
19
|
+
export function spoolSize() {
|
|
20
|
+
let n = 0;
|
|
21
|
+
for (const f of spoolFiles(Number.POSITIVE_INFINITY))
|
|
22
|
+
n += readLines(f).length;
|
|
23
|
+
if (existsSync(spoolPath()))
|
|
24
|
+
n += readLines(spoolPath()).length;
|
|
25
|
+
return n;
|
|
26
|
+
}
|
|
27
|
+
function readLines(path) {
|
|
28
|
+
const out = [];
|
|
29
|
+
for (const line of readFileSync(path, "utf8").split("\n")) {
|
|
30
|
+
if (!line.trim())
|
|
31
|
+
continue;
|
|
32
|
+
try {
|
|
33
|
+
out.push(JSON.parse(line));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// A torn line from a crash mid-write: skip it rather than block the whole spool.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
/** Batch files left behind by flushes that died at least `minAgeMs` ago. */
|
|
42
|
+
function spoolFiles(minAgeMs) {
|
|
43
|
+
if (!existsSync(stateDir()))
|
|
44
|
+
return [];
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
return readdirSync(stateDir())
|
|
47
|
+
.filter((f) => f.startsWith("spool.") && f.endsWith(".sending"))
|
|
48
|
+
.map((f) => join(stateDir(), f))
|
|
49
|
+
.filter((p) => minAgeMs === Number.POSITIVE_INFINITY || now - statSync(p).mtimeMs >= minAgeMs);
|
|
50
|
+
}
|
|
51
|
+
/** Sends everything spooled. On failure the events go back into the spool for the next attempt. */
|
|
52
|
+
export async function flushSpool(send) {
|
|
53
|
+
const result = { sent: 0, inserted: 0, duplicates: 0, failed: 0 };
|
|
54
|
+
const batches = spoolFiles(STALE_BATCH_MS);
|
|
55
|
+
if (existsSync(spoolPath())) {
|
|
56
|
+
const mine = join(stateDir(), `spool.${process.pid}.${Date.now()}.sending`);
|
|
57
|
+
try {
|
|
58
|
+
renameSync(spoolPath(), mine);
|
|
59
|
+
batches.push(mine);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// Another process renamed it first; it will send those events.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
for (const batch of batches) {
|
|
66
|
+
let events;
|
|
67
|
+
try {
|
|
68
|
+
events = readLines(batch);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
continue; // claimed and removed by a concurrent flush
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const r = events.length ? await send(events) : { inserted: 0, duplicates: 0 };
|
|
75
|
+
result.sent += events.length;
|
|
76
|
+
result.inserted += r.inserted;
|
|
77
|
+
result.duplicates += r.duplicates;
|
|
78
|
+
rmSync(batch, { force: true });
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
appendSpool(events);
|
|
82
|
+
rmSync(batch, { force: true });
|
|
83
|
+
result.failed += events.length;
|
|
84
|
+
result.error = e instanceof Error ? e.message : String(e);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { preferLegacy } from "./paths.js";
|
|
5
|
+
/**
|
|
6
|
+
* Local agent state (never synced). A state folder from before the rename (…/rewind) keeps being used, so the last
|
|
7
|
+
* scan and the spool survive the update. `LASTWEEK_STATE_DIR` (or the old `REWIND_STATE_DIR`) overrides for tests.
|
|
8
|
+
*/
|
|
9
|
+
export function stateDir() {
|
|
10
|
+
const env = process.env.LASTWEEK_STATE_DIR ?? process.env.REWIND_STATE_DIR;
|
|
11
|
+
if (env)
|
|
12
|
+
return env;
|
|
13
|
+
const base = process.env.XDG_STATE_HOME ?? join(homedir(), ".local", "state");
|
|
14
|
+
return preferLegacy(join(base, "lastweek"), join(base, "rewind"));
|
|
15
|
+
}
|
|
16
|
+
/** ISO timestamp of the start of the last scan whose events were all ingested, or null. */
|
|
17
|
+
export function readLastScan() {
|
|
18
|
+
const p = join(stateDir(), "last_scan");
|
|
19
|
+
if (!existsSync(p))
|
|
20
|
+
return null;
|
|
21
|
+
const v = readFileSync(p, "utf8").trim();
|
|
22
|
+
return Number.isNaN(Date.parse(v)) ? null : v;
|
|
23
|
+
}
|
|
24
|
+
export function writeLastScan(iso) {
|
|
25
|
+
mkdirSync(stateDir(), { recursive: true });
|
|
26
|
+
writeFileSync(join(stateDir(), "last_scan"), `${iso}\n`);
|
|
27
|
+
}
|
package/dist/tasks.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { stateDir } from "./state.js";
|
|
5
|
+
const path = () => join(stateDir(), "tasks.json");
|
|
6
|
+
const read = () => (existsSync(path()) ? JSON.parse(readFileSync(path(), "utf8")) : []);
|
|
7
|
+
const write = (tasks) => {
|
|
8
|
+
mkdirSync(stateDir(), { recursive: true });
|
|
9
|
+
writeFileSync(path(), JSON.stringify(tasks, null, 2));
|
|
10
|
+
};
|
|
11
|
+
export function startTask(summary, repo, now, agent) {
|
|
12
|
+
const task = { id: randomUUID().slice(0, 8), summary, repo, started_at: now.toISOString(), agent };
|
|
13
|
+
write([...read(), task]);
|
|
14
|
+
return task;
|
|
15
|
+
}
|
|
16
|
+
export function openTasks() {
|
|
17
|
+
return read();
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Ends a task (the given id, else the most recently started one) and returns its block event.
|
|
21
|
+
* The summary given at the end, if any, replaces the one from the start ("outcome, not intent").
|
|
22
|
+
*/
|
|
23
|
+
export function endTask(id, summary, now) {
|
|
24
|
+
const tasks = read();
|
|
25
|
+
const task = id ? tasks.find((t) => t.id === id) : tasks.at(-1);
|
|
26
|
+
if (!task)
|
|
27
|
+
return null;
|
|
28
|
+
write(tasks.filter((t) => t !== task));
|
|
29
|
+
const end = now.toISOString();
|
|
30
|
+
return {
|
|
31
|
+
source: "mcp",
|
|
32
|
+
occurred_at: task.started_at,
|
|
33
|
+
ended_at: end > task.started_at ? end : undefined,
|
|
34
|
+
repo: task.repo,
|
|
35
|
+
summary: summary?.trim() || task.summary,
|
|
36
|
+
payload: { kind: "task", agent: task.agent, task: task.id },
|
|
37
|
+
dedupe_key: `task:${task.id}:${task.started_at}`,
|
|
38
|
+
};
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lastweek-work/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "The lastweek agent: collects git and coding-agent evidence on your machine and sends it to your lastweek account.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"homepage": "https://lastweek.work",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/graphicfox/rewind.git",
|
|
10
|
+
"directory": "packages/agent"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"timesheet",
|
|
14
|
+
"time-tracking",
|
|
15
|
+
"git",
|
|
16
|
+
"claude-code",
|
|
17
|
+
"mcp",
|
|
18
|
+
"freelance"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"bin": {
|
|
22
|
+
"lastweek": "./dist/cli.js",
|
|
23
|
+
"rewind": "./dist/cli.js"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"!dist/**/*.test.js"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
38
|
+
"prepublishOnly": "npm run clean && npm run build && npm test",
|
|
39
|
+
"dev": "tsx src/cli.ts",
|
|
40
|
+
"test": "vitest run --passWithNoTests"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
44
|
+
"zod": "^3.23.8"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^22.0.0",
|
|
48
|
+
"tsx": "^4.19.0",
|
|
49
|
+
"typescript": "^5.6.0",
|
|
50
|
+
"vitest": "^4.1.12"
|
|
51
|
+
}
|
|
52
|
+
}
|