@lastweek-work/cli 0.3.4 → 0.3.5

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 CHANGED
@@ -43,6 +43,13 @@ lastweek scan --dry-run # print events as JSON, send nothing, don'
43
43
  - Sends in chunks of 500. Exits non-zero on any failure; `last_scan` (`~/.local/state/lastweek/last_scan`,
44
44
  override with `LASTWEEK_STATE_DIR`) only advances after everything was ingested. Reruns are safe (server dedupes).
45
45
 
46
+ ## Claude Code session logs (0.3.5)
47
+
48
+ `lastweek scan` also reads Claude Code's own logs in `~/.claude/projects` (the first scan the last 30 days, then
49
+ what changed since the last scan): timestamps, session id, working folder, tool names and edited file paths, folded
50
+ into the same bursts as the hook below. Prompt text, shell commands and tool output are never kept or sent. A burst
51
+ the hook already sent has the same key and is not sent twice.
52
+
46
53
  ## Claude Code hook
47
54
 
48
55
  `lastweek agents` adds this to `~/.claude/settings.json` (merged with existing hooks). Fires on every tool call and every
package/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawn } from "node:child_process";
3
- import { appendFileSync } from "node:fs";
3
+ import { appendFileSync, existsSync } from "node:fs";
4
4
  import { homedir } from "node:os";
5
5
  import { join, resolve } from "node:path";
6
6
  import { parseArgs } from "node:util";
@@ -15,8 +15,9 @@ import { runMcp } from "./mcp.js";
15
15
  import { knownRepos, resolveCwd } from "./resolve.js";
16
16
  import { installedPlist, installSchedule, scheduleInstalled, uninstallSchedule } from "./schedule.js";
17
17
  import { flushSpool, spoolSize } from "./spool.js";
18
- import { ensureStateDir, readLastScan, stateDir, writeLastScan } from "./state.js";
18
+ import { ensureStateDir, readLastScan, stateDir, writeAtomic, writeLastScan } from "./state.js";
19
19
  import { openTasks } from "./tasks.js";
20
+ import { scanTranscripts } from "./transcripts.js";
20
21
  const USAGE = `usage:
21
22
  lastweek connect <CODE> --server <url> [--name <device>] [--yes] [--no-agents] [--no-schedule] [--no-scan]
22
23
  set everything up with a code from the web app's setup tour
@@ -99,12 +100,19 @@ async function runScan(cfg, since) {
99
100
  const warning = authorWarning(stats);
100
101
  if (warning)
101
102
  console.error(warning);
103
+ // Claude Code's session logs (Phase 32a); the first time, the last 30 days (what Claude Code keeps by default).
104
+ const marker = join(stateDir(), "transcripts_backfilled");
105
+ const backfilled = existsSync(marker);
106
+ const sessions = scanTranscripts(backfilled ? since : new Date(Date.now() - 30 * 86400e3).toISOString(), Date.now(), homedir(), { knownRepos });
107
+ events.push(...sessions);
102
108
  // Hook/MCP events waiting in the spool go out with every scan (and with the scheduled one).
103
109
  const spooled = await flushNow();
104
110
  if (spooled.failed)
105
111
  console.error(`spool: ${spooled.failed} events kept for later (${spooled.error})`);
106
112
  const r = await ingest(cfg, events);
107
113
  writeLastScan(startedAt);
114
+ if (!backfilled)
115
+ writeAtomic(marker, `${startedAt}\n`);
108
116
  return { repos: repos.length, events: events.length, since, ...r, spoolSent: spooled.sent };
109
117
  }
110
118
  /** The setup tour's one command. Every step explains itself; nothing secret is printed. */
@@ -0,0 +1,108 @@
1
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { settle } from "./bursts.js";
5
+ import { applyCall, burstToEvent, callFromInput } from "./hook.js";
6
+ import { resolveCwd } from "./resolve.js";
7
+ // Phase 32a: Claude Code's own session logs as evidence. Covers sessions before the hook was installed (or while it
8
+ // only saw edits), subagents and worktrees. Read per line: timestamp, session, cwd, tool names and the file path of a
9
+ // tool call, whether a line is a prompt. Prompt text, commands and tool output are never kept or sent.
10
+ // The same burst logic and dedupe keys as the hook, so a burst the hook already sent is not sent twice.
11
+ /** Newer calls are left to the live hook (its burst may still be growing). */
12
+ export const SETTLE_MS = 20 * 60_000;
13
+ /** The calls in one log file's text (pure). */
14
+ export function callsOf(text, fromMs, toMs) {
15
+ const out = [];
16
+ for (const raw of text.split("\n")) {
17
+ if (!raw.includes('"timestamp"'))
18
+ continue;
19
+ let l;
20
+ try {
21
+ l = JSON.parse(raw);
22
+ }
23
+ catch {
24
+ continue;
25
+ }
26
+ const at = Date.parse(l.timestamp ?? "");
27
+ if (!(at >= fromMs && at < toMs) || !l.sessionId || !l.cwd || l.isMeta)
28
+ continue;
29
+ // Subagents run beside the main thread (often in their own worktree): their own burst stream.
30
+ const key = `${l.sessionId}:${l.agentId ?? "main"}`;
31
+ const base = { session_id: l.sessionId, cwd: l.cwd };
32
+ const content = l.message?.content;
33
+ if (l.type === "assistant" && Array.isArray(content)) {
34
+ for (const c of content)
35
+ if (c?.type === "tool_use" && c.name)
36
+ out.push({ at, key, input: { ...base, tool_name: c.name, tool_input: c.input } });
37
+ }
38
+ else if (l.type === "user" && l.message?.role === "user") {
39
+ // A typed prompt: plain text, not a tool result handed back to the model.
40
+ const toolResult = Array.isArray(content) && content.some((c) => c?.type === "tool_result");
41
+ if (!toolResult)
42
+ out.push({ at, key, input: { ...base, tool_name: "UserPromptSubmit" } });
43
+ }
44
+ }
45
+ return out;
46
+ }
47
+ /** Calls → finished bursts → events (pure apart from `resolve`). */
48
+ export function eventsOf(calls, resolve) {
49
+ const byKey = new Map();
50
+ for (const c of calls)
51
+ byKey.set(c.key, [...(byKey.get(c.key) ?? []), c]);
52
+ const cache = new Map();
53
+ const repoOf = (cwd) => cache.get(cwd) ?? cache.set(cwd, resolve(cwd)).get(cwd);
54
+ const events = [];
55
+ for (const list of byKey.values()) {
56
+ list.sort((a, b) => a.at - b.at);
57
+ let current = null;
58
+ const finish = (b) => {
59
+ if (b)
60
+ events.push(burstToEvent(settle(b)));
61
+ };
62
+ for (const c of list) {
63
+ const r = repoOf(c.input.cwd);
64
+ const call = {
65
+ ...callFromInput(c.input, r.repo),
66
+ cwd: c.input.cwd,
67
+ ...(r.via ? { via: r.via } : {}),
68
+ ...(r.open ? { open: true } : {}),
69
+ };
70
+ const { burst, done } = applyCall(current, call, c.at);
71
+ finish(done);
72
+ current = burst;
73
+ }
74
+ finish(current);
75
+ }
76
+ return events;
77
+ }
78
+ /** Claude Code session logs changed since `since`, as evidence events. Never throws: no logs means no events. */
79
+ export function scanTranscripts(since, now = Date.now(), home = homedir(), deps = {}) {
80
+ const root = join(home, ".claude", "projects");
81
+ if (!existsSync(root))
82
+ return [];
83
+ // shortcut: a burst crossing the window's start is cut there (an extra, overlapping event; the engine merges
84
+ // overlaps, hours don't change). Exact would need the previous scan's open bursts.
85
+ // From SETTLE_MS before `since`: what the previous scan left to the hook is read now (dedupe keeps it single).
86
+ const fromMs = Date.parse(since) - SETTLE_MS;
87
+ const toMs = now - SETTLE_MS;
88
+ const calls = [];
89
+ try {
90
+ for (const rel of readdirSync(root, { recursive: true, encoding: "utf8" })) {
91
+ if (!rel.endsWith(".jsonl"))
92
+ continue;
93
+ const path = join(root, rel);
94
+ try {
95
+ if (statSync(path).mtimeMs < fromMs)
96
+ continue; // untouched since the last scan
97
+ calls.push(...callsOf(readFileSync(path, "utf8"), fromMs, toMs));
98
+ }
99
+ catch {
100
+ // a log being rotated or unreadable: skip it
101
+ }
102
+ }
103
+ }
104
+ catch {
105
+ return [];
106
+ }
107
+ return eventsOf(calls, (cwd) => resolveCwd(cwd, deps));
108
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lastweek-work/cli",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
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",