@nightloom/isy 1.0.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.
Files changed (223) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -0
  3. package/dist/agents/claude.d.ts +6 -0
  4. package/dist/agents/claude.js +66 -0
  5. package/dist/agents/claude.js.map +1 -0
  6. package/dist/agents/codex-records.d.ts +34 -0
  7. package/dist/agents/codex-records.js +492 -0
  8. package/dist/agents/codex-records.js.map +1 -0
  9. package/dist/agents/codex.d.ts +5 -0
  10. package/dist/agents/codex.js +205 -0
  11. package/dist/agents/codex.js.map +1 -0
  12. package/dist/agents/index.d.ts +25 -0
  13. package/dist/agents/index.js +50 -0
  14. package/dist/agents/index.js.map +1 -0
  15. package/dist/agents/kimi-records.d.ts +24 -0
  16. package/dist/agents/kimi-records.js +252 -0
  17. package/dist/agents/kimi-records.js.map +1 -0
  18. package/dist/agents/kimi-wire.d.ts +8 -0
  19. package/dist/agents/kimi-wire.js +158 -0
  20. package/dist/agents/kimi-wire.js.map +1 -0
  21. package/dist/agents/kimi.d.ts +6 -0
  22. package/dist/agents/kimi.js +332 -0
  23. package/dist/agents/kimi.js.map +1 -0
  24. package/dist/agents/types.d.ts +83 -0
  25. package/dist/agents/types.js +2 -0
  26. package/dist/agents/types.js.map +1 -0
  27. package/dist/alert.d.ts +24 -0
  28. package/dist/alert.js +68 -0
  29. package/dist/alert.js.map +1 -0
  30. package/dist/api.d.ts +103 -0
  31. package/dist/api.js +171 -0
  32. package/dist/api.js.map +1 -0
  33. package/dist/commands/analyze.d.ts +61 -0
  34. package/dist/commands/analyze.js +198 -0
  35. package/dist/commands/analyze.js.map +1 -0
  36. package/dist/commands/check.d.ts +55 -0
  37. package/dist/commands/check.js +180 -0
  38. package/dist/commands/check.js.map +1 -0
  39. package/dist/commands/commit.d.ts +26 -0
  40. package/dist/commands/commit.js +130 -0
  41. package/dist/commands/commit.js.map +1 -0
  42. package/dist/commands/health.d.ts +47 -0
  43. package/dist/commands/health.js +366 -0
  44. package/dist/commands/health.js.map +1 -0
  45. package/dist/commands/init.d.ts +3 -0
  46. package/dist/commands/init.js +173 -0
  47. package/dist/commands/init.js.map +1 -0
  48. package/dist/commands/logout.d.ts +1 -0
  49. package/dist/commands/logout.js +30 -0
  50. package/dist/commands/logout.js.map +1 -0
  51. package/dist/commands/notes.d.ts +11 -0
  52. package/dist/commands/notes.js +49 -0
  53. package/dist/commands/notes.js.map +1 -0
  54. package/dist/commands/scan.d.ts +47 -0
  55. package/dist/commands/scan.js +199 -0
  56. package/dist/commands/scan.js.map +1 -0
  57. package/dist/commands/settings.d.ts +15 -0
  58. package/dist/commands/settings.js +94 -0
  59. package/dist/commands/settings.js.map +1 -0
  60. package/dist/commands/status.d.ts +61 -0
  61. package/dist/commands/status.js +151 -0
  62. package/dist/commands/status.js.map +1 -0
  63. package/dist/commands/upload.d.ts +42 -0
  64. package/dist/commands/upload.js +429 -0
  65. package/dist/commands/upload.js.map +1 -0
  66. package/dist/config.d.ts +4 -0
  67. package/dist/config.js +32 -0
  68. package/dist/config.js.map +1 -0
  69. package/dist/detectors.d.ts +43 -0
  70. package/dist/detectors.js +1026 -0
  71. package/dist/detectors.js.map +1 -0
  72. package/dist/git.d.ts +22 -0
  73. package/dist/git.js +68 -0
  74. package/dist/git.js.map +1 -0
  75. package/dist/githook.d.ts +71 -0
  76. package/dist/githook.js +238 -0
  77. package/dist/githook.js.map +1 -0
  78. package/dist/hook.d.ts +39 -0
  79. package/dist/hook.js +173 -0
  80. package/dist/hook.js.map +1 -0
  81. package/dist/index.d.ts +2 -0
  82. package/dist/index.js +205 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/log.d.ts +19 -0
  85. package/dist/log.js +97 -0
  86. package/dist/log.js.map +1 -0
  87. package/dist/mask-paths.d.ts +75 -0
  88. package/dist/mask-paths.js +364 -0
  89. package/dist/mask-paths.js.map +1 -0
  90. package/dist/pairing.d.ts +60 -0
  91. package/dist/pairing.js +137 -0
  92. package/dist/pairing.js.map +1 -0
  93. package/dist/parser.d.ts +48 -0
  94. package/dist/parser.js +516 -0
  95. package/dist/parser.js.map +1 -0
  96. package/dist/paths.d.ts +41 -0
  97. package/dist/paths.js +181 -0
  98. package/dist/paths.js.map +1 -0
  99. package/dist/plan.d.ts +25 -0
  100. package/dist/plan.js +39 -0
  101. package/dist/plan.js.map +1 -0
  102. package/dist/queue.d.ts +26 -0
  103. package/dist/queue.js +120 -0
  104. package/dist/queue.js.map +1 -0
  105. package/dist/redact.d.ts +29 -0
  106. package/dist/redact.js +254 -0
  107. package/dist/redact.js.map +1 -0
  108. package/dist/spawn.d.ts +13 -0
  109. package/dist/spawn.js +32 -0
  110. package/dist/spawn.js.map +1 -0
  111. package/dist/stage0.d.ts +20 -0
  112. package/dist/stage0.js +90 -0
  113. package/dist/stage0.js.map +1 -0
  114. package/dist/state.d.ts +84 -0
  115. package/dist/state.js +150 -0
  116. package/dist/state.js.map +1 -0
  117. package/dist/test/analyze.test.d.ts +1 -0
  118. package/dist/test/analyze.test.js +408 -0
  119. package/dist/test/analyze.test.js.map +1 -0
  120. package/dist/test/check.test.d.ts +1 -0
  121. package/dist/test/check.test.js +193 -0
  122. package/dist/test/check.test.js.map +1 -0
  123. package/dist/test/cli.test.d.ts +1 -0
  124. package/dist/test/cli.test.js +132 -0
  125. package/dist/test/cli.test.js.map +1 -0
  126. package/dist/test/codex.test.d.ts +1 -0
  127. package/dist/test/codex.test.js +585 -0
  128. package/dist/test/codex.test.js.map +1 -0
  129. package/dist/test/commit.test.d.ts +1 -0
  130. package/dist/test/commit.test.js +430 -0
  131. package/dist/test/commit.test.js.map +1 -0
  132. package/dist/test/git.test.d.ts +1 -0
  133. package/dist/test/git.test.js +75 -0
  134. package/dist/test/git.test.js.map +1 -0
  135. package/dist/test/health.test.d.ts +1 -0
  136. package/dist/test/health.test.js +156 -0
  137. package/dist/test/health.test.js.map +1 -0
  138. package/dist/test/hook.test.d.ts +1 -0
  139. package/dist/test/hook.test.js +144 -0
  140. package/dist/test/hook.test.js.map +1 -0
  141. package/dist/test/kimi.test.d.ts +1 -0
  142. package/dist/test/kimi.test.js +349 -0
  143. package/dist/test/kimi.test.js.map +1 -0
  144. package/dist/test/mask-paths.test.d.ts +1 -0
  145. package/dist/test/mask-paths.test.js +230 -0
  146. package/dist/test/mask-paths.test.js.map +1 -0
  147. package/dist/test/notes.test.d.ts +1 -0
  148. package/dist/test/notes.test.js +41 -0
  149. package/dist/test/notes.test.js.map +1 -0
  150. package/dist/test/pairing.test.d.ts +1 -0
  151. package/dist/test/pairing.test.js +138 -0
  152. package/dist/test/pairing.test.js.map +1 -0
  153. package/dist/test/parser.test.d.ts +1 -0
  154. package/dist/test/parser.test.js +312 -0
  155. package/dist/test/parser.test.js.map +1 -0
  156. package/dist/test/plan.test.d.ts +1 -0
  157. package/dist/test/plan.test.js +34 -0
  158. package/dist/test/plan.test.js.map +1 -0
  159. package/dist/test/redact.test.d.ts +1 -0
  160. package/dist/test/redact.test.js +303 -0
  161. package/dist/test/redact.test.js.map +1 -0
  162. package/dist/test/settings.test.d.ts +1 -0
  163. package/dist/test/settings.test.js +32 -0
  164. package/dist/test/settings.test.js.map +1 -0
  165. package/dist/test/stage0.test.d.ts +1 -0
  166. package/dist/test/stage0.test.js +607 -0
  167. package/dist/test/stage0.test.js.map +1 -0
  168. package/dist/test/sweep.test.d.ts +1 -0
  169. package/dist/test/sweep.test.js +298 -0
  170. package/dist/test/sweep.test.js.map +1 -0
  171. package/dist/test/update.test.d.ts +1 -0
  172. package/dist/test/update.test.js +26 -0
  173. package/dist/test/update.test.js.map +1 -0
  174. package/dist/test/upload.test.d.ts +1 -0
  175. package/dist/test/upload.test.js +331 -0
  176. package/dist/test/upload.test.js.map +1 -0
  177. package/dist/types.d.ts +142 -0
  178. package/dist/types.js +2 -0
  179. package/dist/types.js.map +1 -0
  180. package/dist/update.d.ts +17 -0
  181. package/dist/update.js +46 -0
  182. package/dist/update.js.map +1 -0
  183. package/package.json +60 -0
  184. package/src/agents/claude.ts +89 -0
  185. package/src/agents/codex-records.ts +533 -0
  186. package/src/agents/codex.ts +238 -0
  187. package/src/agents/index.ts +58 -0
  188. package/src/agents/kimi-records.ts +274 -0
  189. package/src/agents/kimi-wire.ts +179 -0
  190. package/src/agents/kimi.ts +390 -0
  191. package/src/agents/types.ts +92 -0
  192. package/src/alert.ts +68 -0
  193. package/src/api.ts +269 -0
  194. package/src/commands/analyze.ts +308 -0
  195. package/src/commands/check.ts +239 -0
  196. package/src/commands/commit.ts +179 -0
  197. package/src/commands/health.ts +444 -0
  198. package/src/commands/init.ts +186 -0
  199. package/src/commands/logout.ts +33 -0
  200. package/src/commands/notes.ts +57 -0
  201. package/src/commands/scan.ts +278 -0
  202. package/src/commands/settings.ts +107 -0
  203. package/src/commands/status.ts +236 -0
  204. package/src/commands/upload.ts +524 -0
  205. package/src/config.ts +33 -0
  206. package/src/detectors.ts +1138 -0
  207. package/src/git.ts +86 -0
  208. package/src/githook.ts +269 -0
  209. package/src/hook.ts +237 -0
  210. package/src/index.ts +229 -0
  211. package/src/log.ts +113 -0
  212. package/src/mask-paths.ts +434 -0
  213. package/src/pairing.ts +190 -0
  214. package/src/parser.ts +581 -0
  215. package/src/paths.ts +191 -0
  216. package/src/plan.ts +56 -0
  217. package/src/queue.ts +145 -0
  218. package/src/redact.ts +335 -0
  219. package/src/spawn.ts +33 -0
  220. package/src/stage0.ts +121 -0
  221. package/src/state.ts +208 -0
  222. package/src/types.ts +165 -0
  223. package/src/update.ts +46 -0
@@ -0,0 +1,524 @@
1
+ import { stat } from "node:fs/promises";
2
+ import { detectAgent, presentAgents } from "../agents/index.js";
3
+ import type { Agent, AgentId, HookInput } from "../agents/index.js";
4
+ import { DEFAULT_API_BASE_URL, buildTranscriptFields, uploadSession } from "../api.js";
5
+ import type { ApiOptions, UploadPayload } from "../api.js";
6
+ import { readConfig, updateConfig } from "../config.js";
7
+ import { collectGitMetadata } from "../git.js";
8
+ import { autoInstallGitHook } from "../githook.js";
9
+ import type { GitMetadata, GitResult } from "../git.js";
10
+ import type { DiscoveredSession, IsyConfig } from "../types.js";
11
+ import { logError, logLine } from "../log.js";
12
+ import { parseLines } from "../parser.js";
13
+ import { packageVersion } from "../paths.js";
14
+ import { MAX_ATTEMPTS, abandon, drop, enqueue, isAbandoned, listPending, recordFailure } from "../queue.js";
15
+ import { updateNotice } from "../update.js";
16
+ import { planNotice } from "../plan.js";
17
+ import type { PlanStatus } from "../plan.js";
18
+ import { looksUnchanged, readState, updateSession } from "../state.js";
19
+ import type { ClientState, CommitMark } from "../state.js";
20
+
21
+ export const TOTAL_BUDGET_MS = 30_000;
22
+
23
+ export type { HookInput } from "../agents/index.js";
24
+
25
+ export interface UploadReport {
26
+ agent: AgentId;
27
+ sent: number;
28
+ /** Of those sent, how many the server had not already seen. */
29
+ fresh: number;
30
+ queued: number;
31
+ abandoned: number;
32
+ drained: number;
33
+ deduplicated: boolean;
34
+ skipped?: string;
35
+ /** Set when the server expects a newer client than the one that ran. */
36
+ updateNotice?: string;
37
+ /** What a SessionEnd run did about this repository's git hooks, when it did anything. */
38
+ gitHook?: "installed" | "shared-hooks";
39
+ /** Set when the plan has nothing left: the next pull request will not be analysed. */
40
+ planNotice?: string;
41
+ /** Sessions a sweep looked at, across every CLI and every directory. */
42
+ scanned?: number;
43
+ /** The server's hourly upload allowance ran out; nothing was lost by it. */
44
+ throttled?: boolean;
45
+ }
46
+
47
+ export async function readHookInput(
48
+ stream: NodeJS.ReadStream = process.stdin,
49
+ ): Promise<HookInput | undefined> {
50
+ if (stream.isTTY) return undefined;
51
+
52
+ const chunks: Buffer[] = [];
53
+ const guard = setTimeout(() => stream.destroy(), 2000);
54
+ try {
55
+ for await (const chunk of stream) chunks.push(chunk as Buffer);
56
+ } catch {
57
+ return undefined;
58
+ } finally {
59
+ clearTimeout(guard);
60
+ }
61
+
62
+ const text = Buffer.concat(chunks).toString("utf8").trim();
63
+ if (text.length === 0) return undefined;
64
+
65
+ try {
66
+ const parsed: unknown = JSON.parse(text);
67
+ if (typeof parsed !== "object" || parsed === null) return undefined;
68
+ return parsed as HookInput;
69
+ } catch {
70
+ return undefined;
71
+ }
72
+ }
73
+
74
+ export async function buildPayload(
75
+ agent: Agent,
76
+ transcriptPath: string,
77
+ git: GitMetadata,
78
+ options: { extraPatterns?: readonly string[]; cwd?: string; commits?: CommitMark[] },
79
+ fallbackSessionId: string,
80
+ ): Promise<UploadPayload | undefined> {
81
+ // Whatever the CLI's native format, this comes back Claude-record shaped:
82
+ // the server re-parses the upload with the same parseLines (cascade.ts).
83
+ const lines = await agent.redactedLines(transcriptPath, options);
84
+ // Codex writes a rollout before the first turn, so a session opened and
85
+ // closed again is a file with no turn in it. Nothing to analyse, and no
86
+ // ledger mark either: once it grows a turn it is sent like any other.
87
+ if (lines.length === 0) return undefined;
88
+ const session = parseLines(lines);
89
+ const { contentHash, transcript } = await buildTranscriptFields(lines);
90
+ const now = new Date().toISOString();
91
+
92
+ return {
93
+ sessionId: session.sessionId ?? fallbackSessionId,
94
+ agent: agent.id,
95
+ contentHash,
96
+ startedAt: session.meta.startedAt ?? now,
97
+ endedAt: session.meta.endedAt ?? now,
98
+ claudeVersion: session.meta.claudeVersion ?? "unknown",
99
+ isyVersion: await packageVersion(),
100
+ git,
101
+ transcript,
102
+ ...(options.commits && options.commits.length > 0 ? { commits: options.commits } : {}),
103
+ };
104
+ }
105
+
106
+ /** The server's answer says which client it expects; remember it either way. */
107
+ async function noteClientVersion(
108
+ version: string | undefined,
109
+ current: string,
110
+ report: UploadReport,
111
+ ): Promise<void> {
112
+ if (!version) return;
113
+ report.updateNotice = updateNotice(version, current);
114
+ // Kept so `isy status` and `isy check` can say it too, without a call.
115
+ await updateConfig({ latestVersion: version });
116
+ }
117
+
118
+ /**
119
+ * What the answer said about the plan. Stored like the client version, and for
120
+ * the same reason: the upload is the only call a hook makes, so every other
121
+ * command reads this rather than asking again.
122
+ */
123
+ async function notePlan(plan: PlanStatus | undefined, report: UploadReport): Promise<void> {
124
+ if (!plan) return;
125
+ report.planNotice = planNotice(plan);
126
+ await updateConfig({ plan });
127
+ }
128
+
129
+ async function drainQueue(api: ApiOptions, deadline: number, report: UploadReport): Promise<void> {
130
+ for (const item of await listPending()) {
131
+ if (Date.now() >= deadline) return;
132
+
133
+ const outcome = await uploadSession(item.entry.payload, api);
134
+ if (outcome.status === "ok") {
135
+ await noteClientVersion(outcome.clientVersion, item.entry.payload.isyVersion, report);
136
+ await notePlan(outcome.plan, report);
137
+ await drop(item);
138
+ report.drained += 1;
139
+ continue;
140
+ }
141
+
142
+ // The rest would get the same answer. They wait in the queue, attempts untouched.
143
+ if (outcome.status === "throttled") {
144
+ report.throttled = true;
145
+ logLine("upload: rate limited, the queue keeps its place for the next run");
146
+ return;
147
+ }
148
+
149
+ if (outcome.status === "permanent") {
150
+ await abandon(item, outcome.error);
151
+ report.abandoned += 1;
152
+ logError(`upload: abandoned queued session ${item.entry.sessionId}: ${outcome.error}`);
153
+ continue;
154
+ }
155
+
156
+ const result = await recordFailure(item, outcome.error);
157
+ if (result === "abandoned") {
158
+ report.abandoned += 1;
159
+ logError(`upload: gave up on queued session ${item.entry.sessionId}: ${outcome.error}`);
160
+ }
161
+ }
162
+ }
163
+
164
+ interface Target {
165
+ agent: Agent;
166
+ path: string;
167
+ /** Set when the target came from a scan, which already knows size and mtime. */
168
+ file?: DiscoveredSession;
169
+ /**
170
+ * Where this session ran. Only a sweep sets it: every other path knows the
171
+ * directory before it knows the session, because a hook named it.
172
+ */
173
+ cwd?: string;
174
+ }
175
+
176
+ /**
177
+ * Every session recorded for this directory whose bytes are not already on the
178
+ * server, across every CLI installed here.
179
+ *
180
+ * Size and mtime both matching the last accepted upload means the content
181
+ * matches, so an unchanged transcript is never read — and a session that ended
182
+ * without a commit is still picked up here, however long ago it ended.
183
+ */
184
+ async function changedSessions(cwd: string, state: ClientState): Promise<Target[]> {
185
+ const targets: Target[] = [];
186
+
187
+ for (const agent of await presentAgents()) {
188
+ for (const file of await agent.sessionsIn(cwd)) {
189
+ if (looksUnchanged(state[file.path] ?? {}, file)) continue;
190
+ targets.push({ agent, path: file.path, file });
191
+ }
192
+ }
193
+
194
+ return targets;
195
+ }
196
+
197
+ /**
198
+ * How far back a sweep looks. A sweep has no baseline to start from — every
199
+ * session it has never seen reads as changed — so without a window the first
200
+ * one on a machine would upload years of transcripts from every directory the
201
+ * CLI was ever used in. Nothing older than this can reach an analysis anyway:
202
+ * sessions are matched to a pull request by commit SHA, and a transcript
203
+ * untouched for a fortnight belongs to commits that were pushed long ago.
204
+ *
205
+ * ponytail: a fixed window, not a high-water mark. A machine that was offline
206
+ * longer than this loses the tail; move the boundary to a `lastSweepAt` in the
207
+ * config if that ever happens to anyone.
208
+ */
209
+ const SWEEP_WINDOW_MS = 14 * 24 * 60 * 60 * 1000;
210
+
211
+ /**
212
+ * Every recently changed session on this machine, whatever directory it ran in.
213
+ *
214
+ * What a sweep starts from, and the answer to a CLI with no clear end to a
215
+ * session: a desktop app fires no SessionEnd hook, so there is no moment that
216
+ * names a working directory and no transcript path handed to us. The directory
217
+ * is read back out of the transcript instead (`Agent.cwdOf`), and only for the
218
+ * sessions that actually grew — size and mtime settle that with a `stat`, so a
219
+ * machine with hundreds of sessions costs a `stat` each and nothing more.
220
+ *
221
+ * `scanned` counts what was looked at rather than what was sent: it is the one
222
+ * number that tells a quiet sweep apart from a sweep that found no sessions.
223
+ */
224
+ async function sweptSessions(state: ClientState, report: UploadReport): Promise<Target[]> {
225
+ const targets: Target[] = [];
226
+ const oldest = Date.now() - SWEEP_WINDOW_MS;
227
+ let rootless = 0;
228
+ report.scanned = 0;
229
+
230
+ for (const agent of await presentAgents()) {
231
+ for (const file of await agent.allSessions()) {
232
+ report.scanned += 1;
233
+ if (file.modifiedAt.getTime() < oldest) continue;
234
+ if (looksUnchanged(state[file.path] ?? {}, file)) continue;
235
+
236
+ // A session whose directory cannot be read is one nothing can be said
237
+ // about: without it there is no repository, no branch and no root to make
238
+ // the transcript's paths relative to. Kimi's listing already knows.
239
+ const cwd = file.cwd ?? (await agent.cwdOf(file.path));
240
+ if (!cwd) {
241
+ rootless += 1;
242
+ continue;
243
+ }
244
+
245
+ targets.push({ agent, path: file.path, file, cwd });
246
+ }
247
+ }
248
+
249
+ // Counted, not listed. A sweep runs at every session start and the same
250
+ // sessions are skipped every time; one line each would push everything worth
251
+ // reading out of the journal `isy health` shows.
252
+ if (rootless > 0) logLine(`upload: ${rootless} swept session(s) record no working directory`);
253
+
254
+ // Smallest first. The run budget is checked between sessions, and the session
255
+ // still open in the CLI that started this sweep is the biggest and always reads
256
+ // as changed: listed first, it spent the budget on every start and the sessions
257
+ // behind it never went out.
258
+ return targets.sort((a, b) => a.file!.sizeBytes - b.file!.sizeBytes);
259
+ }
260
+
261
+ /** Size and mtime of a transcript, for the "did it change" check on the next run. */
262
+ async function fingerprint(target: Target): Promise<{ sizeBytes: number; modifiedMs: number }> {
263
+ if (target.file) {
264
+ return { sizeBytes: target.file.sizeBytes, modifiedMs: target.file.modifiedAt.getTime() };
265
+ }
266
+ const info = await stat(target.path);
267
+ return { sizeBytes: info.size, modifiedMs: info.mtime.getTime() };
268
+ }
269
+
270
+ async function sendOne(
271
+ target: Target,
272
+ git: GitMetadata,
273
+ state: ClientState,
274
+ config: IsyConfig,
275
+ api: ApiOptions,
276
+ workingDir: string,
277
+ fallbackSessionId: string,
278
+ report: UploadReport,
279
+ ): Promise<void> {
280
+ const payload = await buildPayload(
281
+ target.agent,
282
+ target.path,
283
+ git,
284
+ {
285
+ extraPatterns: config.extraRedactPatterns,
286
+ // A swept session ran where the transcript says it ran, not where this
287
+ // process happens to stand: masking is rooted on it.
288
+ cwd: target.cwd ?? workingDir,
289
+ commits: state[target.path]?.commits,
290
+ },
291
+ fallbackSessionId,
292
+ );
293
+ if (!payload) return;
294
+
295
+ if (await isAbandoned(payload.sessionId, payload.contentHash)) {
296
+ report.skipped = `session ${payload.sessionId} was given up on after ${MAX_ATTEMPTS} attempts`;
297
+ logError(`upload: ${report.skipped}`);
298
+ return;
299
+ }
300
+
301
+ const outcome = await uploadSession(payload, api);
302
+ if (outcome.status === "ok") {
303
+ report.sent += 1;
304
+ await noteClientVersion(outcome.clientVersion, payload.isyVersion, report);
305
+ await notePlan(outcome.plan, report);
306
+ if (outcome.deduplicated !== true) report.fresh += 1;
307
+ // Recorded only once the server has it: a failed send must stay "changed"
308
+ // so the next commit tries again rather than skipping it forever.
309
+ const seen = await fingerprint(target);
310
+ await updateSession(target.path, (current) => ({
311
+ ...current,
312
+ uploadedHash: payload.contentHash,
313
+ uploadedCommit: payload.commits?.at(-1)?.sha,
314
+ ...seen,
315
+ }));
316
+ await updateConfig({ lastUploadAt: new Date().toISOString() });
317
+ } else if (outcome.status === "throttled") {
318
+ // Neither queued nor failed: the transcript still reads as changed, so the
319
+ // next sweep or hook picks it up once the hour turns over, and a queue would
320
+ // only burn its attempts.
321
+ report.throttled = true;
322
+ logLine(`upload: ${payload.sessionId} not sent, this hour's uploads are spent`);
323
+ } else if (outcome.status === "retry") {
324
+ await enqueue(payload, outcome.error);
325
+ report.queued += 1;
326
+ logLine(`upload: queued ${payload.sessionId} for retry: ${outcome.error}`);
327
+ } else {
328
+ report.abandoned += 1;
329
+ logError(`upload: dropped ${payload.sessionId}: ${outcome.error}`);
330
+ }
331
+ }
332
+
333
+ export async function runUpload(
334
+ options: { silent: boolean; agent?: string; all?: boolean; hook?: boolean; sweep?: boolean },
335
+ cwd: string,
336
+ ): Promise<UploadReport> {
337
+ const report: UploadReport = {
338
+ agent: "claude",
339
+ sent: 0,
340
+ fresh: 0,
341
+ queued: 0,
342
+ abandoned: 0,
343
+ drained: 0,
344
+ deduplicated: false,
345
+ };
346
+ const deadline = Date.now() + TOTAL_BUDGET_MS;
347
+
348
+ const config = await readConfig();
349
+ if (!config.token) {
350
+ report.skipped = "no token configured, run: isy init";
351
+ logLine(`upload: ${report.skipped}`);
352
+ return report;
353
+ }
354
+
355
+ const api: ApiOptions = {
356
+ baseUrl: config.apiBaseUrl ?? DEFAULT_API_BASE_URL,
357
+ token: config.token,
358
+ // A hook is the run a CLI may be waiting on: Claude Code and Kimi hold
359
+ // their exit for it, Codex detaches it, and the hook cannot tell which.
360
+ ...(options.hook ? { deadline } : {}),
361
+ };
362
+
363
+ const hook = await readHookInput();
364
+ const agent = detectAgent(hook, options.agent);
365
+ report.agent = agent.id;
366
+
367
+ const workingDir = hook?.cwd ?? cwd;
368
+ const state = await readState();
369
+
370
+ const targets = options.sweep
371
+ ? await sweptSessions(state, report)
372
+ : options.all
373
+ ? await changedSessions(workingDir, state)
374
+ : await (async (): Promise<Target[]> => {
375
+ const path = await agent.transcriptFor(hook, workingDir);
376
+ return path ? [{ agent, path }] : [];
377
+ })();
378
+
379
+ if (targets.length === 0) {
380
+ report.skipped = options.sweep
381
+ ? `nothing new to upload from ${report.scanned ?? 0} session(s) on this machine`
382
+ : options.all
383
+ ? `nothing new to upload for ${workingDir}`
384
+ : `no ${agent.label} transcript found for ${workingDir}`;
385
+ logLine(`upload: ${report.skipped}`);
386
+ await drainQueue(api, deadline, report);
387
+ return report;
388
+ }
389
+
390
+ // One git call per directory, however many sessions ran in it.
391
+ const repositories = new Map<string, GitResult>();
392
+ const gitFor = async (directory: string): Promise<GitResult> => {
393
+ const known = repositories.get(directory);
394
+ if (known) return known;
395
+ const result = await collectGitMetadata(directory);
396
+ repositories.set(directory, result);
397
+ return result;
398
+ };
399
+
400
+ // A sweep finds sessions from directories that were never repositories, and
401
+ // that is not a fault worth reporting: it skips them one by one below. Every
402
+ // other caller was pointed at one directory and deserves to hear why nothing
403
+ // came of it.
404
+ if (!options.sweep) {
405
+ const git = await gitFor(workingDir);
406
+ if (!git.ok) {
407
+ report.skipped = `${workingDir} is not usable: ${git.reason}`;
408
+ logLine(`upload: ${report.skipped}`);
409
+ await drainQueue(api, deadline, report);
410
+ return report;
411
+ }
412
+ }
413
+
414
+ // Only on a session's own hook: that is a repository the developer works in,
415
+ // and `isy init` already asked them once whether isy may hook into it. The
416
+ // flag, not stdin, says so: a Codex hook installed before the payload went
417
+ // over fd 3 still runs with stdin on /dev/null.
418
+ if (options.hook) {
419
+ try {
420
+ const outcome = await autoInstallGitHook(workingDir);
421
+ if (outcome === "installed" || outcome === "shared-hooks") {
422
+ report.gitHook = outcome;
423
+ logLine(`upload: git hooks ${outcome} in ${workingDir}`);
424
+ }
425
+ } catch (error) {
426
+ logError(`upload: git hooks not installed in ${workingDir}: ${String(error)}`);
427
+ }
428
+ }
429
+
430
+ // Sessions whose directory is no repository. Counted for one line at the end
431
+ // rather than one line each, for the same reason as the rootless ones above:
432
+ // a directory that is not a repository today is skipped at every sweep, and
433
+ // could be `git init`ed tomorrow.
434
+ let outside = 0;
435
+
436
+ for (const target of targets) {
437
+ // The budget covers the whole run, queue included: a directory holding a
438
+ // dozen stale sessions must not turn one commit into a minute of network.
439
+ if (Date.now() >= deadline) {
440
+ logLine(`upload: out of budget with ${targets.length - report.sent} session(s) left`);
441
+ break;
442
+ }
443
+ // One "throttled" answers for every session in the run.
444
+ if (report.throttled) break;
445
+
446
+ const git = await gitFor(target.cwd ?? workingDir);
447
+ if (!git.ok) {
448
+ outside += 1;
449
+ continue;
450
+ }
451
+
452
+ await sendOne(
453
+ target,
454
+ git.metadata,
455
+ state,
456
+ config,
457
+ api,
458
+ workingDir,
459
+ // Per target, not per run: a run covers many sessions, and one fallback
460
+ // shared between them would file them all under the same id — which the
461
+ // server keys on, so each would replace the last.
462
+ target.file?.sessionId ?? hook?.session_id ?? "unknown",
463
+ report,
464
+ );
465
+ }
466
+
467
+ if (outside > 0) logLine(`upload: ${outside} changed session(s) ran outside a repository`);
468
+
469
+ // "Nothing new to analyse" only when every session sent was already known.
470
+ report.deduplicated = report.sent > 0 && report.fresh === 0;
471
+
472
+ await drainQueue(api, deadline, report);
473
+ return report;
474
+ }
475
+
476
+ export function formatUploadReport(report: UploadReport): string {
477
+ if (report.skipped) {
478
+ const nothing = `isy: nothing uploaded (${report.skipped})`;
479
+ return report.updateNotice ? `${nothing}\nisy: ${report.updateNotice}` : nothing;
480
+ }
481
+
482
+ const parts: string[] = [];
483
+ if (report.sent > 0) parts.push("session uploaded");
484
+ if (report.throttled) parts.push("rate limited, the rest go out on the next run");
485
+ if (report.queued > 0) parts.push("session queued for retry");
486
+ if (report.drained > 0) parts.push(`${report.drained} queued session(s) sent`);
487
+ if (report.abandoned > 0) parts.push(`${report.abandoned} session(s) given up on`);
488
+
489
+ const line = parts.length > 0 ? `isy: ${parts.join(", ")}` : "isy: nothing to upload";
490
+ // Its own line rather than another clause: this one is about the account, and
491
+ // it is the only part of the summary the reader has to act on.
492
+ const tail = [report.planNotice, report.updateNotice].filter((notice) => notice !== undefined);
493
+ return [line, ...tail.map((notice) => `isy: ${notice}`)].join("\n");
494
+ }
495
+
496
+ // The client never sees the analysis itself: a job is created when a pull request
497
+ // webhook matches this session by commit SHA, which can be minutes later or
498
+ // never. The alert says what actually happened, not what we hope happens next.
499
+ export function formatUploadAlert(report: UploadReport): string | undefined {
500
+ // An update notice is worth the line even when there was nothing to upload:
501
+ // the end of a session is the moment the developer is not mid-thought.
502
+ if (report.skipped) return report.updateNotice ? `ISY: ${report.updateNotice}` : undefined;
503
+
504
+ const parts: string[] = [];
505
+ if (report.sent > 0) {
506
+ parts.push(
507
+ report.deduplicated
508
+ ? "session already uploaded, nothing new to analyse"
509
+ : "session uploaded, analysis runs when it reaches a pull request",
510
+ );
511
+ }
512
+ if (report.drained > 0) parts.push(`${report.drained} queued session(s) sent`);
513
+ if (report.queued > 0) parts.push("upload failed, queued for retry");
514
+ if (report.throttled) parts.push("rate limited, the rest upload on the next run");
515
+ if (report.abandoned > 0) parts.push(`${report.abandoned} session(s) given up on`);
516
+ if (report.gitHook === "installed") parts.push("commit and push hooks added to this repository");
517
+ if (report.gitHook === "shared-hooks") {
518
+ parts.push("no git hooks here: they live in a shared directory, run isy init to add them");
519
+ }
520
+ if (report.planNotice) parts.push(report.planNotice);
521
+ if (report.updateNotice) parts.push(report.updateNotice);
522
+
523
+ return parts.length > 0 ? `ISY: ${parts.join(" · ")}` : undefined;
524
+ }
package/src/config.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
2
+ import { configPath, isyHome } from "./paths.js";
3
+ import type { IsyConfig } from "./types.js";
4
+
5
+ export async function readConfig(): Promise<IsyConfig> {
6
+ let contents: string;
7
+ try {
8
+ contents = await readFile(configPath(), "utf8");
9
+ } catch {
10
+ return {};
11
+ }
12
+
13
+ try {
14
+ const parsed: unknown = JSON.parse(contents);
15
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return {};
16
+ return parsed as IsyConfig;
17
+ } catch {
18
+ throw new Error(`config at ${configPath()} is not valid JSON`);
19
+ }
20
+ }
21
+
22
+ export async function writeConfig(config: IsyConfig): Promise<void> {
23
+ const target = configPath();
24
+ await mkdir(isyHome(), { recursive: true, mode: 0o700 });
25
+ await writeFile(target, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
26
+ await chmod(target, 0o600);
27
+ }
28
+
29
+ export async function updateConfig(patch: Partial<IsyConfig>): Promise<IsyConfig> {
30
+ const merged = { ...(await readConfig()), ...patch };
31
+ await writeConfig(merged);
32
+ return merged;
33
+ }