@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
package/src/index.ts ADDED
@@ -0,0 +1,229 @@
1
+ #!/usr/bin/env node
2
+ import { parseArgs } from "node:util";
3
+ import { agentById } from "./agents/index.js";
4
+ import { parkAlert } from "./alert.js";
5
+ import { claimSweep, releaseSweep } from "./state.js";
6
+ import { runAnalyze } from "./commands/analyze.js";
7
+ import { runCheck } from "./commands/check.js";
8
+ import { runCommitCommand } from "./commands/commit.js";
9
+ import { runHealth } from "./commands/health.js";
10
+ import { runInit } from "./commands/init.js";
11
+ import { runLogout } from "./commands/logout.js";
12
+ import { runNotes } from "./commands/notes.js";
13
+ import { runSettings } from "./commands/settings.js";
14
+ import { runStatus } from "./commands/status.js";
15
+ import { formatUploadAlert, formatUploadReport, runUpload } from "./commands/upload.js";
16
+ import { errorMessage, logError } from "./log.js";
17
+ import { spawnIsy } from "./spawn.js";
18
+
19
+ const COMMANDS = new Set([
20
+ "init",
21
+ "status",
22
+ "check",
23
+ "health",
24
+ "upload",
25
+ "sweep",
26
+ "commit",
27
+ "logout",
28
+ "analyze",
29
+ "notes",
30
+ "settings",
31
+ ]);
32
+ const USAGE =
33
+ "Usage: isy <init|status|logout> [--silent] [--json] [--token <token>]\n" +
34
+ " isy upload [--silent] [--hook] [--all] [--agent <claude|kimi|codex>]\n" +
35
+ " isy sweep [--silent] [--json]\n" +
36
+ " isy commit [--hook] [--json]\n" +
37
+ " isy check [--ping] [--json] [--hook] [--agent <claude|kimi|codex>]\n" +
38
+ " isy health [--json] [--fix] [--scan]\n" +
39
+ " isy notes [--pr <number>] [--json]\n" +
40
+ " isy settings [set|unset <key> [value]] [--json]\n" +
41
+ " isy analyze <file.jsonl|directory>... [--stage 0] [--json]\n" +
42
+ " [--expect <dir>] [--update]";
43
+
44
+ const silentRequested =
45
+ process.argv.includes("--silent") || process.argv.includes("--hook");
46
+
47
+ function fail(message: string, silent: boolean): never {
48
+ logError(message);
49
+ if (!silent) console.error(`isy: ${message}`);
50
+ process.exit(silent ? 0 : 1);
51
+ }
52
+
53
+ function parse() {
54
+ try {
55
+ return parseArgs({
56
+ allowPositionals: true,
57
+ options: {
58
+ silent: { type: "boolean", default: false },
59
+ json: { type: "boolean", default: false },
60
+ token: { type: "string" },
61
+ stage: { type: "string" },
62
+ expect: { type: "string" },
63
+ update: { type: "boolean", default: false },
64
+ hook: { type: "boolean", default: false },
65
+ ping: { type: "boolean", default: false },
66
+ all: { type: "boolean", default: false },
67
+ agent: { type: "string" },
68
+ fix: { type: "boolean", default: false },
69
+ scan: { type: "boolean", default: false },
70
+ pr: { type: "string" },
71
+ help: { type: "boolean", short: "h", default: false },
72
+ },
73
+ });
74
+ } catch (error) {
75
+ fail(errorMessage(error), silentRequested);
76
+ }
77
+ }
78
+
79
+ async function run(
80
+ command: string,
81
+ values: {
82
+ json: boolean;
83
+ silent: boolean;
84
+ token?: string;
85
+ stage?: string;
86
+ expect?: string;
87
+ update: boolean;
88
+ hook: boolean;
89
+ ping: boolean;
90
+ all: boolean;
91
+ agent?: string;
92
+ fix: boolean;
93
+ scan: boolean;
94
+ pr?: string;
95
+ },
96
+ positionals: string[],
97
+ ): Promise<void> {
98
+ switch (command) {
99
+ case "status":
100
+ return runStatus({ json: values.json }, process.cwd());
101
+ case "check":
102
+ return runCheck({
103
+ json: values.json,
104
+ hook: values.hook,
105
+ ping: values.ping,
106
+ agent: values.agent,
107
+ catchUp: () => spawnIsy(["sweep", "--silent"]),
108
+ });
109
+ case "notes": {
110
+ const pr = values.pr === undefined ? undefined : Number(values.pr);
111
+ if (pr !== undefined && !Number.isInteger(pr)) throw new Error("--pr takes a pull request number");
112
+ return runNotes({ json: values.json, pr }, process.cwd());
113
+ }
114
+ case "settings":
115
+ return runSettings({
116
+ json: values.json,
117
+ action: positionals[1],
118
+ key: positionals[2],
119
+ value: positionals[3],
120
+ });
121
+ case "health":
122
+ return runHealth({ json: values.json, fix: values.fix, scan: values.scan }, process.cwd());
123
+ case "init":
124
+ return runInit({ token: values.token }, process.cwd());
125
+ case "logout":
126
+ return runLogout(process.cwd());
127
+ // post-commit runs this. Git ignores what it returns — the commit is already
128
+ // made — but it must still never throw, or the user sees a stack trace after
129
+ // every commit.
130
+ case "commit": {
131
+ if (values.hook) {
132
+ try {
133
+ await runCommitCommand({ json: values.json, hook: true }, process.cwd());
134
+ } catch {
135
+ return;
136
+ }
137
+ return;
138
+ }
139
+ return runCommitCommand({ json: values.json, hook: false }, process.cwd());
140
+ }
141
+ case "upload": {
142
+ // A SessionEnd hook must not break the session it reports on.
143
+ if (values.hook) {
144
+ try {
145
+ const report = await runUpload(
146
+ { silent: true, agent: values.agent, all: values.all, hook: true },
147
+ process.cwd(),
148
+ );
149
+ const alert = formatUploadAlert(report);
150
+ // Each CLI displays a line its own way; the wording is the same.
151
+ if (alert) await agentById(report.agent).deliver(alert, "SessionEnd");
152
+ } catch {
153
+ return;
154
+ }
155
+ return;
156
+ }
157
+
158
+ const report = await runUpload(
159
+ { silent: values.silent, agent: values.agent, all: values.all },
160
+ process.cwd(),
161
+ );
162
+ if (!values.silent) console.log(formatUploadReport(report));
163
+ return;
164
+ }
165
+ // What stands in for SessionEnd where a CLI has none: every session on the
166
+ // machine, whatever directory it ran in. Run detached from any CLI — by a
167
+ // SessionStart hook, later by a timer — so a line worth showing is parked
168
+ // for the next session start rather than written to a stdout nobody reads.
169
+ case "sweep": {
170
+ // One sweep at a time on a machine: two CLIs opened together would scan
171
+ // the same transcripts and upload each of them twice.
172
+ if (!(await claimSweep())) {
173
+ if (!values.silent) console.log("isy: a sweep is already running");
174
+ return;
175
+ }
176
+
177
+ try {
178
+ const report = await runUpload({ silent: values.silent, sweep: true }, process.cwd());
179
+ if (values.json) {
180
+ console.log(JSON.stringify(report, null, 2));
181
+ return;
182
+ }
183
+ if (values.silent) {
184
+ const alert = formatUploadAlert(report);
185
+ if (alert) await parkAlert(alert);
186
+ return;
187
+ }
188
+ console.log(formatUploadReport(report));
189
+ } catch (error) {
190
+ // Spawned detached with its output discarded, so an uncaught throw would
191
+ // leave nothing anywhere — and `isy health` reads the journal to answer
192
+ // exactly this question.
193
+ logError(`sweep: ${String(error)}`);
194
+ } finally {
195
+ await releaseSweep();
196
+ }
197
+ return;
198
+ }
199
+ case "analyze":
200
+ return runAnalyze({
201
+ files: positionals.slice(1),
202
+ stage: values.stage,
203
+ json: values.json,
204
+ expect: values.expect,
205
+ update: values.update,
206
+ });
207
+ default:
208
+ throw new Error("not implemented yet");
209
+ }
210
+ }
211
+
212
+ const { values, positionals } = parse();
213
+ const silent = values.silent;
214
+ const command = positionals[0] ?? "status";
215
+
216
+ if (values.help) {
217
+ console.log(USAGE);
218
+ process.exit(0);
219
+ }
220
+
221
+ if (!COMMANDS.has(command)) {
222
+ fail(`unknown command '${command}'. ${USAGE}`, silent);
223
+ }
224
+
225
+ try {
226
+ await run(command, values, positionals);
227
+ } catch (error) {
228
+ fail(`${command}: ${errorMessage(error)}`, silent);
229
+ }
package/src/log.ts ADDED
@@ -0,0 +1,113 @@
1
+ import { appendFileSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
2
+ import { isyHome, logPath } from "./paths.js";
3
+
4
+ /**
5
+ * ponytail: past this size the oldest half is dropped in place. A journal
6
+ * nobody rotates fills a disk, and nothing here is worth a rotation scheme.
7
+ */
8
+ const MAX_LOG_BYTES = 512 * 1024;
9
+
10
+ /**
11
+ * Operations that run unattended. A failure in one of these is invisible until
12
+ * something goes looking, which is what `isy health` is for. Failures of the
13
+ * commands a user typed are already on their screen.
14
+ */
15
+ const UNATTENDED = new Set(["upload", "commit", "check"]);
16
+
17
+ export type LogLevel = "info" | "error";
18
+
19
+ export interface JournalEntry {
20
+ at: string;
21
+ level: LogLevel;
22
+ message: string;
23
+ /** The word before the first colon: which operation wrote this, when it says. */
24
+ operation?: string;
25
+ }
26
+
27
+ function trim(): void {
28
+ try {
29
+ if (statSync(logPath()).size <= MAX_LOG_BYTES) return;
30
+ const lines = readFileSync(logPath(), "utf8").split("\n");
31
+ writeFileSync(logPath(), lines.slice(Math.floor(lines.length / 2)).join("\n"), { mode: 0o600 });
32
+ } catch {
33
+ return;
34
+ }
35
+ }
36
+
37
+ function write(level: LogLevel, message: string): void {
38
+ try {
39
+ mkdirSync(isyHome(), { recursive: true, mode: 0o700 });
40
+ trim();
41
+ appendFileSync(logPath(), `${new Date().toISOString()} ${level} ${message}\n`, { mode: 0o600 });
42
+ } catch {
43
+ return;
44
+ }
45
+ }
46
+
47
+ export function logLine(message: string): void {
48
+ write("info", message);
49
+ }
50
+
51
+ /** A failure `isy health` must be able to find long after it happened. */
52
+ export function logError(message: string): void {
53
+ write("error", message);
54
+ }
55
+
56
+ export function errorMessage(error: unknown): string {
57
+ if (error instanceof Error) return error.message;
58
+ return String(error);
59
+ }
60
+
61
+ function operationOf(message: string): string | undefined {
62
+ const colon = message.indexOf(":");
63
+ if (colon <= 0) return undefined;
64
+ const head = message.slice(0, colon);
65
+ return /^[a-z-]+$/.test(head) ? head : undefined;
66
+ }
67
+
68
+ function parseEntry(line: string): JournalEntry {
69
+ const space = line.indexOf(" ");
70
+ const at = space > 0 ? line.slice(0, space) : "";
71
+ const rest = space > 0 ? line.slice(space + 1) : line;
72
+
73
+ // Lines written before levels existed are informational by definition:
74
+ // logError did not exist to write them.
75
+ const levelled = /^(info|error) (.*)$/s.exec(rest);
76
+ const level = (levelled?.[1] ?? "info") as LogLevel;
77
+ const message = levelled?.[2] ?? rest;
78
+
79
+ return { at, level, message, operation: operationOf(message) };
80
+ }
81
+
82
+ export function readJournal(limit = 200): JournalEntry[] {
83
+ let contents: string;
84
+ try {
85
+ contents = readFileSync(logPath(), "utf8");
86
+ } catch {
87
+ return [];
88
+ }
89
+
90
+ const entries = contents
91
+ .split("\n")
92
+ .filter((line) => line.trim().length > 0)
93
+ .map(parseEntry);
94
+
95
+ return entries.slice(-limit);
96
+ }
97
+
98
+ /**
99
+ * Failures of unattended work that no later success has answered for. An upload
100
+ * that failed and then succeeded is history; one that failed after the last
101
+ * success is a machine that is still broken.
102
+ */
103
+ export function unresolvedErrors(lastSuccessAt?: string, entries = readJournal()): JournalEntry[] {
104
+ const since = lastSuccessAt ? Date.parse(lastSuccessAt) : Number.NaN;
105
+
106
+ return entries.filter((entry) => {
107
+ if (entry.level !== "error") return false;
108
+ if (entry.operation !== undefined && !UNATTENDED.has(entry.operation)) return false;
109
+ if (Number.isNaN(since)) return true;
110
+ const at = Date.parse(entry.at);
111
+ return Number.isNaN(at) || at > since;
112
+ });
113
+ }