@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.
- package/LICENSE +21 -0
- package/README.md +69 -0
- package/dist/agents/claude.d.ts +6 -0
- package/dist/agents/claude.js +66 -0
- package/dist/agents/claude.js.map +1 -0
- package/dist/agents/codex-records.d.ts +34 -0
- package/dist/agents/codex-records.js +492 -0
- package/dist/agents/codex-records.js.map +1 -0
- package/dist/agents/codex.d.ts +5 -0
- package/dist/agents/codex.js +205 -0
- package/dist/agents/codex.js.map +1 -0
- package/dist/agents/index.d.ts +25 -0
- package/dist/agents/index.js +50 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/kimi-records.d.ts +24 -0
- package/dist/agents/kimi-records.js +252 -0
- package/dist/agents/kimi-records.js.map +1 -0
- package/dist/agents/kimi-wire.d.ts +8 -0
- package/dist/agents/kimi-wire.js +158 -0
- package/dist/agents/kimi-wire.js.map +1 -0
- package/dist/agents/kimi.d.ts +6 -0
- package/dist/agents/kimi.js +332 -0
- package/dist/agents/kimi.js.map +1 -0
- package/dist/agents/types.d.ts +83 -0
- package/dist/agents/types.js +2 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/alert.d.ts +24 -0
- package/dist/alert.js +68 -0
- package/dist/alert.js.map +1 -0
- package/dist/api.d.ts +103 -0
- package/dist/api.js +171 -0
- package/dist/api.js.map +1 -0
- package/dist/commands/analyze.d.ts +61 -0
- package/dist/commands/analyze.js +198 -0
- package/dist/commands/analyze.js.map +1 -0
- package/dist/commands/check.d.ts +55 -0
- package/dist/commands/check.js +180 -0
- package/dist/commands/check.js.map +1 -0
- package/dist/commands/commit.d.ts +26 -0
- package/dist/commands/commit.js +130 -0
- package/dist/commands/commit.js.map +1 -0
- package/dist/commands/health.d.ts +47 -0
- package/dist/commands/health.js +366 -0
- package/dist/commands/health.js.map +1 -0
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.js +173 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/logout.d.ts +1 -0
- package/dist/commands/logout.js +30 -0
- package/dist/commands/logout.js.map +1 -0
- package/dist/commands/notes.d.ts +11 -0
- package/dist/commands/notes.js +49 -0
- package/dist/commands/notes.js.map +1 -0
- package/dist/commands/scan.d.ts +47 -0
- package/dist/commands/scan.js +199 -0
- package/dist/commands/scan.js.map +1 -0
- package/dist/commands/settings.d.ts +15 -0
- package/dist/commands/settings.js +94 -0
- package/dist/commands/settings.js.map +1 -0
- package/dist/commands/status.d.ts +61 -0
- package/dist/commands/status.js +151 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/upload.d.ts +42 -0
- package/dist/commands/upload.js +429 -0
- package/dist/commands/upload.js.map +1 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.js +32 -0
- package/dist/config.js.map +1 -0
- package/dist/detectors.d.ts +43 -0
- package/dist/detectors.js +1026 -0
- package/dist/detectors.js.map +1 -0
- package/dist/git.d.ts +22 -0
- package/dist/git.js +68 -0
- package/dist/git.js.map +1 -0
- package/dist/githook.d.ts +71 -0
- package/dist/githook.js +238 -0
- package/dist/githook.js.map +1 -0
- package/dist/hook.d.ts +39 -0
- package/dist/hook.js +173 -0
- package/dist/hook.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +205 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +19 -0
- package/dist/log.js +97 -0
- package/dist/log.js.map +1 -0
- package/dist/mask-paths.d.ts +75 -0
- package/dist/mask-paths.js +364 -0
- package/dist/mask-paths.js.map +1 -0
- package/dist/pairing.d.ts +60 -0
- package/dist/pairing.js +137 -0
- package/dist/pairing.js.map +1 -0
- package/dist/parser.d.ts +48 -0
- package/dist/parser.js +516 -0
- package/dist/parser.js.map +1 -0
- package/dist/paths.d.ts +41 -0
- package/dist/paths.js +181 -0
- package/dist/paths.js.map +1 -0
- package/dist/plan.d.ts +25 -0
- package/dist/plan.js +39 -0
- package/dist/plan.js.map +1 -0
- package/dist/queue.d.ts +26 -0
- package/dist/queue.js +120 -0
- package/dist/queue.js.map +1 -0
- package/dist/redact.d.ts +29 -0
- package/dist/redact.js +254 -0
- package/dist/redact.js.map +1 -0
- package/dist/spawn.d.ts +13 -0
- package/dist/spawn.js +32 -0
- package/dist/spawn.js.map +1 -0
- package/dist/stage0.d.ts +20 -0
- package/dist/stage0.js +90 -0
- package/dist/stage0.js.map +1 -0
- package/dist/state.d.ts +84 -0
- package/dist/state.js +150 -0
- package/dist/state.js.map +1 -0
- package/dist/test/analyze.test.d.ts +1 -0
- package/dist/test/analyze.test.js +408 -0
- package/dist/test/analyze.test.js.map +1 -0
- package/dist/test/check.test.d.ts +1 -0
- package/dist/test/check.test.js +193 -0
- package/dist/test/check.test.js.map +1 -0
- package/dist/test/cli.test.d.ts +1 -0
- package/dist/test/cli.test.js +132 -0
- package/dist/test/cli.test.js.map +1 -0
- package/dist/test/codex.test.d.ts +1 -0
- package/dist/test/codex.test.js +585 -0
- package/dist/test/codex.test.js.map +1 -0
- package/dist/test/commit.test.d.ts +1 -0
- package/dist/test/commit.test.js +430 -0
- package/dist/test/commit.test.js.map +1 -0
- package/dist/test/git.test.d.ts +1 -0
- package/dist/test/git.test.js +75 -0
- package/dist/test/git.test.js.map +1 -0
- package/dist/test/health.test.d.ts +1 -0
- package/dist/test/health.test.js +156 -0
- package/dist/test/health.test.js.map +1 -0
- package/dist/test/hook.test.d.ts +1 -0
- package/dist/test/hook.test.js +144 -0
- package/dist/test/hook.test.js.map +1 -0
- package/dist/test/kimi.test.d.ts +1 -0
- package/dist/test/kimi.test.js +349 -0
- package/dist/test/kimi.test.js.map +1 -0
- package/dist/test/mask-paths.test.d.ts +1 -0
- package/dist/test/mask-paths.test.js +230 -0
- package/dist/test/mask-paths.test.js.map +1 -0
- package/dist/test/notes.test.d.ts +1 -0
- package/dist/test/notes.test.js +41 -0
- package/dist/test/notes.test.js.map +1 -0
- package/dist/test/pairing.test.d.ts +1 -0
- package/dist/test/pairing.test.js +138 -0
- package/dist/test/pairing.test.js.map +1 -0
- package/dist/test/parser.test.d.ts +1 -0
- package/dist/test/parser.test.js +312 -0
- package/dist/test/parser.test.js.map +1 -0
- package/dist/test/plan.test.d.ts +1 -0
- package/dist/test/plan.test.js +34 -0
- package/dist/test/plan.test.js.map +1 -0
- package/dist/test/redact.test.d.ts +1 -0
- package/dist/test/redact.test.js +303 -0
- package/dist/test/redact.test.js.map +1 -0
- package/dist/test/settings.test.d.ts +1 -0
- package/dist/test/settings.test.js +32 -0
- package/dist/test/settings.test.js.map +1 -0
- package/dist/test/stage0.test.d.ts +1 -0
- package/dist/test/stage0.test.js +607 -0
- package/dist/test/stage0.test.js.map +1 -0
- package/dist/test/sweep.test.d.ts +1 -0
- package/dist/test/sweep.test.js +298 -0
- package/dist/test/sweep.test.js.map +1 -0
- package/dist/test/update.test.d.ts +1 -0
- package/dist/test/update.test.js +26 -0
- package/dist/test/update.test.js.map +1 -0
- package/dist/test/upload.test.d.ts +1 -0
- package/dist/test/upload.test.js +331 -0
- package/dist/test/upload.test.js.map +1 -0
- package/dist/types.d.ts +142 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/update.d.ts +17 -0
- package/dist/update.js +46 -0
- package/dist/update.js.map +1 -0
- package/package.json +60 -0
- package/src/agents/claude.ts +89 -0
- package/src/agents/codex-records.ts +533 -0
- package/src/agents/codex.ts +238 -0
- package/src/agents/index.ts +58 -0
- package/src/agents/kimi-records.ts +274 -0
- package/src/agents/kimi-wire.ts +179 -0
- package/src/agents/kimi.ts +390 -0
- package/src/agents/types.ts +92 -0
- package/src/alert.ts +68 -0
- package/src/api.ts +269 -0
- package/src/commands/analyze.ts +308 -0
- package/src/commands/check.ts +239 -0
- package/src/commands/commit.ts +179 -0
- package/src/commands/health.ts +444 -0
- package/src/commands/init.ts +186 -0
- package/src/commands/logout.ts +33 -0
- package/src/commands/notes.ts +57 -0
- package/src/commands/scan.ts +278 -0
- package/src/commands/settings.ts +107 -0
- package/src/commands/status.ts +236 -0
- package/src/commands/upload.ts +524 -0
- package/src/config.ts +33 -0
- package/src/detectors.ts +1138 -0
- package/src/git.ts +86 -0
- package/src/githook.ts +269 -0
- package/src/hook.ts +237 -0
- package/src/index.ts +229 -0
- package/src/log.ts +113 -0
- package/src/mask-paths.ts +434 -0
- package/src/pairing.ts +190 -0
- package/src/parser.ts +581 -0
- package/src/paths.ts +191 -0
- package/src/plan.ts +56 -0
- package/src/queue.ts +145 -0
- package/src/redact.ts +335 -0
- package/src/spawn.ts +33 -0
- package/src/stage0.ts +121 -0
- package/src/state.ts +208 -0
- package/src/types.ts +165 -0
- package/src/update.ts +46 -0
package/src/paths.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { basename, join } from "node:path";
|
|
6
|
+
import type { SessionFile } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export async function packageVersion(): Promise<string> {
|
|
9
|
+
try {
|
|
10
|
+
const contents = await readFile(join(import.meta.dirname, "..", "package.json"), "utf8");
|
|
11
|
+
const parsed: unknown = JSON.parse(contents);
|
|
12
|
+
if (typeof parsed === "object" && parsed !== null && "version" in parsed) {
|
|
13
|
+
const version = (parsed as { version: unknown }).version;
|
|
14
|
+
if (typeof version === "string") return version;
|
|
15
|
+
}
|
|
16
|
+
} catch {
|
|
17
|
+
return "0.0.0";
|
|
18
|
+
}
|
|
19
|
+
return "0.0.0";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isyHome(): string {
|
|
23
|
+
return process.env.ISY_HOME ?? join(homedir(), ".isy");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function configPath(): string {
|
|
27
|
+
return join(isyHome(), "config.json");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function logPath(): string {
|
|
31
|
+
return join(isyHome(), "isy.log");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function queueDir(): string {
|
|
35
|
+
return join(isyHome(), "queue");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Where an alert waits when it could not be shown at the moment it happened.
|
|
40
|
+
* Kimi discards hook stdout on its lifecycle events, so an alert raised as a
|
|
41
|
+
* session ends is parked here and drained at the next prompt.
|
|
42
|
+
*/
|
|
43
|
+
export function pendingAlertPath(): string {
|
|
44
|
+
return join(isyHome(), "pending-alert");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function claudeConfigDir(): string {
|
|
48
|
+
return process.env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function claudeProjectsDir(): string {
|
|
52
|
+
return join(claudeConfigDir(), "projects");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function claudeSettingsPath(): string {
|
|
56
|
+
return join(claudeConfigDir(), "settings.json");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function projectSlug(cwd: string): string {
|
|
60
|
+
return cwd.replace(/[^a-zA-Z0-9]/g, "-");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function projectDir(cwd: string): string {
|
|
64
|
+
return join(claudeProjectsDir(), projectSlug(cwd));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The transcripts in one project folder, unsorted: the callers below sort. */
|
|
68
|
+
async function sessionsInDir(dir: string): Promise<SessionFile[]> {
|
|
69
|
+
let entries: string[];
|
|
70
|
+
try {
|
|
71
|
+
entries = await readdir(dir);
|
|
72
|
+
} catch {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const sessions: SessionFile[] = [];
|
|
77
|
+
for (const entry of entries) {
|
|
78
|
+
if (!entry.endsWith(".jsonl")) continue;
|
|
79
|
+
const path = join(dir, entry);
|
|
80
|
+
try {
|
|
81
|
+
const info = await stat(path);
|
|
82
|
+
if (!info.isFile()) continue;
|
|
83
|
+
sessions.push({
|
|
84
|
+
sessionId: basename(entry, ".jsonl"),
|
|
85
|
+
path,
|
|
86
|
+
sizeBytes: info.size,
|
|
87
|
+
modifiedAt: info.mtime,
|
|
88
|
+
});
|
|
89
|
+
} catch {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return sessions;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function newestFirst(sessions: SessionFile[]): SessionFile[] {
|
|
98
|
+
return sessions.sort((a, b) => b.modifiedAt.getTime() - a.modifiedAt.getTime());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function findSessions(cwd: string): Promise<SessionFile[]> {
|
|
102
|
+
return newestFirst(await sessionsInDir(projectDir(cwd)));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function latestSession(cwd: string): Promise<SessionFile | undefined> {
|
|
106
|
+
const sessions = await findSessions(cwd);
|
|
107
|
+
return sessions[0];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Every Claude session on this machine, newest first, without opening a single
|
|
112
|
+
* transcript: a scan that runs on a timer must cost `readdir` and `stat`, not a
|
|
113
|
+
* read per session. Which directory each one ran in is `firstCwd`'s job, asked
|
|
114
|
+
* only about the sessions that turned out to have changed.
|
|
115
|
+
*/
|
|
116
|
+
export async function allClaudeSessions(): Promise<SessionFile[]> {
|
|
117
|
+
const root = claudeProjectsDir();
|
|
118
|
+
|
|
119
|
+
let projects: string[];
|
|
120
|
+
try {
|
|
121
|
+
projects = await readdir(root);
|
|
122
|
+
} catch {
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const sessions: SessionFile[] = [];
|
|
127
|
+
for (const project of projects) sessions.push(...(await sessionsInDir(join(root, project))));
|
|
128
|
+
|
|
129
|
+
return newestFirst(sessions);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* How far into a transcript to look for the working directory. Claude opens a
|
|
134
|
+
* session with records that carry none — a resumed session starts with a
|
|
135
|
+
* `summary` — so the first line alone is not enough, and reading the whole file
|
|
136
|
+
* to answer one question is too much.
|
|
137
|
+
*/
|
|
138
|
+
const CWD_SCAN_LINES = 40;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Where a Claude session ran, read from the transcript rather than derived from
|
|
142
|
+
* the folder it sits in: `projectSlug` replaces every non-alphanumeric character
|
|
143
|
+
* with a dash, so the directory cannot be recovered from the name. This is the
|
|
144
|
+
* same field `mask-paths` roots its rewriting on.
|
|
145
|
+
*/
|
|
146
|
+
export async function firstCwd(path: string, limit = CWD_SCAN_LINES): Promise<string | undefined> {
|
|
147
|
+
const input = createReadStream(path, { encoding: "utf8" });
|
|
148
|
+
const reader = createInterface({ input, crlfDelay: Infinity });
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
let seen = 0;
|
|
152
|
+
for await (const line of reader) {
|
|
153
|
+
if (seen++ >= limit) return undefined;
|
|
154
|
+
if (line.length === 0) continue;
|
|
155
|
+
try {
|
|
156
|
+
const parsed: unknown = JSON.parse(line);
|
|
157
|
+
if (typeof parsed !== "object" || parsed === null) continue;
|
|
158
|
+
const cwd = (parsed as { cwd?: unknown }).cwd;
|
|
159
|
+
if (typeof cwd === "string" && cwd.length > 0) return cwd;
|
|
160
|
+
} catch {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
} finally {
|
|
166
|
+
reader.close();
|
|
167
|
+
input.destroy();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The first line of a file, without reading the rest: transcripts get large. */
|
|
172
|
+
export async function firstLine(path: string): Promise<string> {
|
|
173
|
+
const input = createReadStream(path, { encoding: "utf8" });
|
|
174
|
+
const reader = createInterface({ input, crlfDelay: Infinity });
|
|
175
|
+
try {
|
|
176
|
+
for await (const line of reader) return line;
|
|
177
|
+
return "";
|
|
178
|
+
} finally {
|
|
179
|
+
reader.close();
|
|
180
|
+
input.destroy();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* What this client already did with each session: what was uploaded, what was
|
|
186
|
+
* printed, which commits it spans. A cache — losing it costs one repeated
|
|
187
|
+
* upload the server deduplicates, never correctness.
|
|
188
|
+
*/
|
|
189
|
+
export function sessionStatePath(): string {
|
|
190
|
+
return join(isyHome(), "sessions.json");
|
|
191
|
+
}
|
package/src/plan.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the server said about the account's plan on the last upload, and the one
|
|
3
|
+
* line it is worth printing.
|
|
4
|
+
*
|
|
5
|
+
* The CLI cannot see an analysis: a run starts when a pull request webhook
|
|
6
|
+
* matches the session, minutes later or never. So the one thing it can
|
|
7
|
+
* usefully say is that the next match will be refused — otherwise a plan that
|
|
8
|
+
* ran out looks exactly like a pull request that has not arrived yet.
|
|
9
|
+
*/
|
|
10
|
+
export interface PlanStatus {
|
|
11
|
+
tier: string;
|
|
12
|
+
/** Paid analyses left in the cycle. Zero means the gate is closed. */
|
|
13
|
+
analysesLeft: number;
|
|
14
|
+
/** Bought analyses on top of the allowance, one credit each. */
|
|
15
|
+
credits: number;
|
|
16
|
+
/** ISO timestamp the cycle rolls over at. */
|
|
17
|
+
resetsAt?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isNumber(value: unknown): value is number {
|
|
21
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Never throws: an older or newer server that sends something else has no plan block. */
|
|
25
|
+
export function parsePlan(value: unknown): PlanStatus | undefined {
|
|
26
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
27
|
+
const record = value as Record<string, unknown>;
|
|
28
|
+
if (typeof record.tier !== "string") return undefined;
|
|
29
|
+
if (!isNumber(record.analysesLeft) || !isNumber(record.credits)) return undefined;
|
|
30
|
+
return {
|
|
31
|
+
tier: record.tier,
|
|
32
|
+
analysesLeft: record.analysesLeft,
|
|
33
|
+
credits: record.credits,
|
|
34
|
+
...(typeof record.resetsAt === "string" ? { resetsAt: record.resetsAt } : {}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** "1 Oct" — enough to answer "when", short enough to sit in a one-line alert. */
|
|
39
|
+
function resetDay(iso: string | undefined): string | undefined {
|
|
40
|
+
if (!iso) return undefined;
|
|
41
|
+
const date = new Date(iso);
|
|
42
|
+
if (Number.isNaN(date.getTime())) return undefined;
|
|
43
|
+
return date.toLocaleDateString("en-GB", { day: "numeric", month: "short", timeZone: "UTC" });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The line, or nothing while there is still room. Credits count as room: they
|
|
48
|
+
* are what the gate spends once the allowance is gone (`billing.ts:checkQuota`).
|
|
49
|
+
*/
|
|
50
|
+
export function planNotice(plan: PlanStatus | undefined): string | undefined {
|
|
51
|
+
if (!plan || plan.analysesLeft > 0 || plan.credits > 0) return undefined;
|
|
52
|
+
const day = resetDay(plan.resetsAt);
|
|
53
|
+
return `plan limit reached on ${plan.tier} — pull requests are not analysed${
|
|
54
|
+
day ? ` until ${day}` : ""
|
|
55
|
+
}. Add credits or upgrade the plan.`;
|
|
56
|
+
}
|
package/src/queue.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { access, chmod, mkdir, readFile, readdir, rename, unlink, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { queueDir } from "./paths.js";
|
|
4
|
+
import type { UploadPayload } from "./api.js";
|
|
5
|
+
|
|
6
|
+
export const MAX_ATTEMPTS = 3;
|
|
7
|
+
const FAILED_SUFFIX = ".failed";
|
|
8
|
+
|
|
9
|
+
export interface QueueEntry {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
contentHash: string;
|
|
12
|
+
attempts: number;
|
|
13
|
+
queuedAt: string;
|
|
14
|
+
lastError?: string;
|
|
15
|
+
payload: UploadPayload;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface QueueItem {
|
|
19
|
+
path: string;
|
|
20
|
+
entry: QueueEntry;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function entryName(sessionId: string, contentHash: string): string {
|
|
24
|
+
const safeSession = sessionId.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
25
|
+
const digest = contentHash.replace(/^sha256:/, "").slice(0, 12);
|
|
26
|
+
return `${safeSession}.${digest}.json`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function isAbandoned(sessionId: string, contentHash: string): Promise<boolean> {
|
|
30
|
+
const path = join(queueDir(), `${entryName(sessionId, contentHash)}${FAILED_SUFFIX}`);
|
|
31
|
+
try {
|
|
32
|
+
await access(path);
|
|
33
|
+
return true;
|
|
34
|
+
} catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function enqueue(payload: UploadPayload, lastError?: string): Promise<string> {
|
|
40
|
+
const dir = queueDir();
|
|
41
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
42
|
+
|
|
43
|
+
const path = join(dir, entryName(payload.sessionId, payload.contentHash));
|
|
44
|
+
const existing = await readEntry(path);
|
|
45
|
+
|
|
46
|
+
const entry: QueueEntry = {
|
|
47
|
+
sessionId: payload.sessionId,
|
|
48
|
+
contentHash: payload.contentHash,
|
|
49
|
+
attempts: existing ? existing.attempts : 0,
|
|
50
|
+
queuedAt: existing ? existing.queuedAt : new Date().toISOString(),
|
|
51
|
+
lastError,
|
|
52
|
+
payload,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
await writeFile(path, JSON.stringify(entry), { mode: 0o600 });
|
|
56
|
+
await chmod(path, 0o600);
|
|
57
|
+
return path;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function readEntry(path: string): Promise<QueueEntry | undefined> {
|
|
61
|
+
try {
|
|
62
|
+
const parsed: unknown = JSON.parse(await readFile(path, "utf8"));
|
|
63
|
+
if (typeof parsed !== "object" || parsed === null) return undefined;
|
|
64
|
+
return parsed as QueueEntry;
|
|
65
|
+
} catch {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function listBySuffix(suffix: string): Promise<QueueItem[]> {
|
|
71
|
+
let names: string[];
|
|
72
|
+
try {
|
|
73
|
+
names = await readdir(queueDir());
|
|
74
|
+
} catch {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const items: QueueItem[] = [];
|
|
79
|
+
for (const name of names) {
|
|
80
|
+
// ".json" also matches ".json.failed", so the pending list excludes it.
|
|
81
|
+
if (!name.endsWith(suffix)) continue;
|
|
82
|
+
if (suffix === ".json" && name.endsWith(FAILED_SUFFIX)) continue;
|
|
83
|
+
const path = join(queueDir(), name);
|
|
84
|
+
const entry = await readEntry(path);
|
|
85
|
+
if (entry) items.push({ path, entry });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
items.sort((a, b) => a.entry.queuedAt.localeCompare(b.entry.queuedAt));
|
|
89
|
+
return items;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function listPending(): Promise<QueueItem[]> {
|
|
93
|
+
return listBySuffix(".json");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Uploads that ran out of attempts. Their `lastError` is the only record of why. */
|
|
97
|
+
export async function listFailed(): Promise<QueueItem[]> {
|
|
98
|
+
return listBySuffix(FAILED_SUFFIX);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function recordFailure(
|
|
102
|
+
item: QueueItem,
|
|
103
|
+
error: string,
|
|
104
|
+
): Promise<"retained" | "abandoned"> {
|
|
105
|
+
const attempts = item.entry.attempts + 1;
|
|
106
|
+
const updated: QueueEntry = { ...item.entry, attempts, lastError: error };
|
|
107
|
+
await writeFile(item.path, JSON.stringify(updated), { mode: 0o600 });
|
|
108
|
+
|
|
109
|
+
if (attempts >= MAX_ATTEMPTS) {
|
|
110
|
+
await rename(item.path, `${item.path}${FAILED_SUFFIX}`);
|
|
111
|
+
return "abandoned";
|
|
112
|
+
}
|
|
113
|
+
return "retained";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function drop(item: QueueItem): Promise<void> {
|
|
117
|
+
try {
|
|
118
|
+
await unlink(item.path);
|
|
119
|
+
} catch {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function abandon(item: QueueItem, error: string): Promise<void> {
|
|
125
|
+
const updated: QueueEntry = { ...item.entry, attempts: MAX_ATTEMPTS, lastError: error };
|
|
126
|
+
await writeFile(item.path, JSON.stringify(updated), { mode: 0o600 });
|
|
127
|
+
await rename(item.path, `${item.path}${FAILED_SUFFIX}`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export async function countQueue(): Promise<{ pending: number; failed: number }> {
|
|
131
|
+
let entries: string[];
|
|
132
|
+
try {
|
|
133
|
+
entries = await readdir(queueDir());
|
|
134
|
+
} catch {
|
|
135
|
+
return { pending: 0, failed: 0 };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let pending = 0;
|
|
139
|
+
let failed = 0;
|
|
140
|
+
for (const entry of entries) {
|
|
141
|
+
if (entry.endsWith(FAILED_SUFFIX)) failed += 1;
|
|
142
|
+
else if (entry.endsWith(".json")) pending += 1;
|
|
143
|
+
}
|
|
144
|
+
return { pending, failed };
|
|
145
|
+
}
|