@avi2dg/subquo 0.0.0 → 0.1.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/.bun-version +1 -0
- package/.github/actions/setup/action.yml +21 -0
- package/.github/actions/setup/bun-without-unzip.sh +20 -0
- package/.github/workflows/ci.yml +177 -0
- package/.github/workflows/main.yml +108 -0
- package/.github/workflows/mutation.yml +106 -0
- package/.github/workflows/pr-title.yml +43 -0
- package/.github/workflows/release.yml +34 -0
- package/.jscpd.json +5 -0
- package/.no-mistakes.yaml +3 -0
- package/AGENTS.md +15 -0
- package/CHANGELOG.md +21 -0
- package/CLAUDE.md +2 -0
- package/LICENSE +21 -0
- package/README.md +80 -0
- package/VISION.md +39 -0
- package/bun.lock +909 -0
- package/bunfig.toml +3 -0
- package/checks.config.ts +7 -0
- package/docs/accounts.md +55 -0
- package/docs/design.md +122 -0
- package/docs/shadow-run.md +49 -0
- package/docs/stage-1-findings.md +65 -0
- package/knip.config.ts +3 -0
- package/oxlint.config.ts +3 -0
- package/package.json +45 -3
- package/scripts/release-assets.ts +28 -0
- package/src/accounts.ts +62 -0
- package/src/cli.ts +122 -0
- package/src/codex-credentials.ts +1 -0
- package/src/collect.ts +45 -0
- package/src/collectors.ts +210 -0
- package/src/index.ts +146 -0
- package/src/keychain.ts +25 -0
- package/src/launch.ts +21 -0
- package/src/pi-codex.ts +17 -0
- package/src/renewal.ts +158 -0
- package/src/storage.ts +48 -0
- package/src/usage.ts +400 -0
- package/stryker.conf.mjs +3 -0
- package/templates/adr.md +32 -0
- package/templates/agents.md +9 -0
- package/templates/changelog.md +39 -0
- package/templates/claude.md +2 -0
- package/templates/explanation.md +15 -0
- package/templates/how-to.md +33 -0
- package/templates/readme.md +48 -0
- package/templates/reference.md +15 -0
- package/templates/tutorial.md +31 -0
- package/templates/vision.md +16 -0
- package/tests/e2e/cli.test.ts +193 -0
- package/tests/e2e/npm-package.test.ts +30 -0
- package/tests/e2e/release-assets.test.ts +53 -0
- package/tests/fixtures/snapshots/fresh.json +54 -0
- package/tests/fixtures/snapshots/incomplete.json +66 -0
- package/tests/unit/collect.test.ts +51 -0
- package/tests/unit/collectors.test.ts +401 -0
- package/tests/unit/no-mistakes.test.ts +8 -0
- package/tests/unit/renewal.test.ts +17 -0
- package/tests/unit/snapshot.test.ts +169 -0
- package/tsconfig.json +46 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { TestClock } from "effect/testing";
|
|
4
|
+
import { validClaudeKeychainService } from "../../src/accounts";
|
|
5
|
+
import type { FetchImpl } from "../../src/collectors";
|
|
6
|
+
import { requestClaudeReading, requestCodexReading, requestOpenCodeReading } from "../../src/collectors";
|
|
7
|
+
import {
|
|
8
|
+
parseClaudeCredentials,
|
|
9
|
+
parseClaudeUsagePayload,
|
|
10
|
+
parseCodexCredentials,
|
|
11
|
+
parseCodexUsagePayload,
|
|
12
|
+
parseOpenCodeKey,
|
|
13
|
+
parseOpenCodeUsagePayload,
|
|
14
|
+
} from "../../src/usage";
|
|
15
|
+
|
|
16
|
+
const reset = "2030-01-02T00:00:00.000Z";
|
|
17
|
+
const now = Date.parse("2030-01-01T00:00:00.000Z");
|
|
18
|
+
|
|
19
|
+
function jsonResponse(payload: unknown): Response {
|
|
20
|
+
return new Response(JSON.stringify(payload), { status: 200 });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function statusResponse(status: number): Response {
|
|
24
|
+
return new Response("", { status });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function fakeJwt(expSeconds: number): string {
|
|
28
|
+
const payload = Buffer.from(JSON.stringify({ exp: expSeconds }), "utf8").toString("base64url");
|
|
29
|
+
return "header." + payload + ".signature";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
test("a Claude utilization reply leaves the remainder, not the usage, as percent left", async () => {
|
|
34
|
+
const windows = parseClaudeUsagePayload({ five_hour: { utilization: 60, resets_at: reset } });
|
|
35
|
+
expect(windows).toEqual([{ id: "five-hour", scope: "all", percentLeft: 40, resetsAt: reset }]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a Claude reply with an unknown window shape reads as unchecked, not fresh", async () => {
|
|
39
|
+
expect(parseClaudeUsagePayload({ five_hour: { utilization: 160, resets_at: reset } })).toBeUndefined();
|
|
40
|
+
expect(parseClaudeUsagePayload({ limits: [{ percent: 10, group: "mystery" }] })).toBeUndefined();
|
|
41
|
+
expect(parseClaudeUsagePayload({})).toBeUndefined();
|
|
42
|
+
expect(parseClaudeCredentials("not json")).toBeUndefined();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a rejected Claude pass reads as login needed, not unchecked", async () => {
|
|
46
|
+
const fetchImpl: FetchImpl = () => Promise.resolve(statusResponse(401));
|
|
47
|
+
const reading = await Effect.runPromise(requestClaudeReading({ accessToken: "live-token" }, fetchImpl, now));
|
|
48
|
+
expect(reading).toEqual({ code: "auth_required" });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("a rejected pass that expired mid-read does not read as login needed", async () => {
|
|
52
|
+
const fetchImpl: FetchImpl = () => Promise.resolve(statusResponse(401));
|
|
53
|
+
const program = Effect.gen(function*() {
|
|
54
|
+
yield* TestClock.setTime(now + 5000);
|
|
55
|
+
return yield* requestClaudeReading({ accessToken: "live-token", expiresAt: now + 1000 }, fetchImpl, now);
|
|
56
|
+
}).pipe(Effect.provide(TestClock.layer()));
|
|
57
|
+
const reading = await Effect.runPromise(program);
|
|
58
|
+
expect(reading).toEqual({ code: "auth_unverified" });
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a Claude rate limit reads as limited, not unchecked", async () => {
|
|
62
|
+
const fetchImpl: FetchImpl = () => Promise.resolve(statusResponse(429));
|
|
63
|
+
const reading = await Effect.runPromise(requestClaudeReading({ accessToken: "live-token" }, fetchImpl, now));
|
|
64
|
+
expect(reading).toEqual({ code: "rate_limited" });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("an expired Claude pass reads as unchecked without touching the network", async () => {
|
|
68
|
+
const fetchImpl: FetchImpl = () => { throw new Error("network used"); };
|
|
69
|
+
const reading = await Effect.runPromise(
|
|
70
|
+
requestClaudeReading({ accessToken: "stale-token", expiresAt: now - 1000 }, fetchImpl, now),
|
|
71
|
+
);
|
|
72
|
+
expect(reading).toEqual({ code: "auth_unverified" });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("a fresh Claude reply carries its windows with the read time", async () => {
|
|
76
|
+
const fetchImpl: FetchImpl = () => Promise.resolve(jsonResponse({ seven_day: { utilization: 88, resets_at: reset } }));
|
|
77
|
+
const reading = await Effect.runPromise(requestClaudeReading({ accessToken: "live-token" }, fetchImpl, now));
|
|
78
|
+
expect(reading).toEqual({
|
|
79
|
+
observedAt: "2030-01-01T00:00:00.000Z",
|
|
80
|
+
windows: [{ id: "weekly", scope: "all", percentLeft: 12, resetsAt: reset }],
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a scoped Claude reply keeps the model window out of the shared weekly window", async () => {
|
|
85
|
+
const windows = parseClaudeUsagePayload({
|
|
86
|
+
limits: [
|
|
87
|
+
{ kind: "session", group: "session", percent: 22, resets_at: reset, scope: null, is_active: false },
|
|
88
|
+
{ kind: "weekly_all", group: "weekly", percent: 41, resets_at: reset, scope: null, is_active: false },
|
|
89
|
+
{ kind: "weekly_scoped", group: "weekly", percent: 63, resets_at: reset, scope: { model: { id: null, display_name: "Fable" }, surface: null }, is_active: true },
|
|
90
|
+
],
|
|
91
|
+
});
|
|
92
|
+
expect(windows).toEqual([
|
|
93
|
+
{ id: "five-hour", scope: "all", percentLeft: 78, resetsAt: reset },
|
|
94
|
+
{ id: "weekly", scope: "all", percentLeft: 59, resetsAt: reset },
|
|
95
|
+
{ id: "fable-weekly", scope: "fable", percentLeft: 37, resetsAt: reset },
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("a Claude reply with an unmapped scoped limit reads as unchecked rather than dropping that limit", async () => {
|
|
100
|
+
const shared = [
|
|
101
|
+
{ kind: "session", percent: 22, resets_at: reset },
|
|
102
|
+
{ kind: "weekly_all", percent: 41, resets_at: reset },
|
|
103
|
+
];
|
|
104
|
+
for (const scope of [{ model: { id: null, display_name: "Mystery" } }, { model: { id: "claude-opus-sonnet" } }, null]) {
|
|
105
|
+
const payload = { limits: [...shared, { kind: "weekly_scoped", percent: 100, resets_at: reset, scope }] };
|
|
106
|
+
expect(parseClaudeUsagePayload(payload)).toBeUndefined();
|
|
107
|
+
const fetchImpl: FetchImpl = () => Promise.resolve(jsonResponse(payload));
|
|
108
|
+
const reading = await Effect.runPromise(requestClaudeReading({ accessToken: "live-token" }, fetchImpl, now));
|
|
109
|
+
expect(reading).toEqual({ code: "auth_unverified" });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("an unrelated Claude limit kind does not block the model windows", () => {
|
|
114
|
+
const windows = parseClaudeUsagePayload({
|
|
115
|
+
limits: [
|
|
116
|
+
{ kind: "session", percent: 22, resets_at: reset },
|
|
117
|
+
{ kind: "fake_unrelated_kind", percent: 90, resets_at: reset },
|
|
118
|
+
],
|
|
119
|
+
});
|
|
120
|
+
expect(windows).toEqual([{ id: "five-hour", scope: "all", percentLeft: 78, resetsAt: reset }]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("a Claude kind-only reply neither skips the new session limit nor drops the legacy weekly fallback", () => {
|
|
124
|
+
const windows = parseClaudeUsagePayload({
|
|
125
|
+
limits: [{ kind: "session", percent: 25, resets_at: reset }],
|
|
126
|
+
five_hour: { utilization: 99, resets_at: reset },
|
|
127
|
+
seven_day: { utilization: 80, resets_at: reset },
|
|
128
|
+
});
|
|
129
|
+
expect(windows).toEqual([
|
|
130
|
+
{ id: "five-hour", scope: "all", percentLeft: 75, resetsAt: reset },
|
|
131
|
+
{ id: "weekly", scope: "all", percentLeft: 20, resetsAt: reset },
|
|
132
|
+
]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("a malformed refresh expiry does not poison a usable Claude access token", () => {
|
|
136
|
+
expect(parseClaudeCredentials(JSON.stringify({
|
|
137
|
+
claudeAiOauth: { accessToken: "fake-access", expiresAt: now + 2_000, refreshTokenExpiresAt: "not a date" },
|
|
138
|
+
}))).toEqual({ accessToken: "fake-access", expiresAt: now + 2_000 });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("a Claude refresh expiry in epoch seconds, before 2020 or outside the Date range reads as unknown, not as a date", () => {
|
|
142
|
+
const credentials = (refreshTokenExpiresAt: number) => parseClaudeCredentials(JSON.stringify({
|
|
143
|
+
claudeAiOauth: { accessToken: "fake-access", refreshTokenExpiresAt },
|
|
144
|
+
}));
|
|
145
|
+
for (const unusable of [1_900_000_000, Date.UTC(2019, 11, 31, 23, 59, 59, 999), 8.64e15 + 1, now + 0.5]) {
|
|
146
|
+
expect(credentials(unusable)).toEqual({ accessToken: "fake-access" });
|
|
147
|
+
}
|
|
148
|
+
for (const usable of [Date.UTC(2020, 0, 1), 8.64e15]) {
|
|
149
|
+
expect(credentials(usable)).toEqual({ accessToken: "fake-access", refreshExpiresAt: usable });
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("a legacy Claude reply keeps its sonnet window instead of dropping it", async () => {
|
|
154
|
+
const windows = parseClaudeUsagePayload({
|
|
155
|
+
five_hour: { utilization: 18, resets_at: reset },
|
|
156
|
+
seven_day: { utilization: 36, reset_at: reset },
|
|
157
|
+
seven_day_opus: { utilization: 7, resets_at: reset },
|
|
158
|
+
seven_day_sonnet: { utilization: 50, resets_at: reset },
|
|
159
|
+
});
|
|
160
|
+
expect(windows).toEqual([
|
|
161
|
+
{ id: "five-hour", scope: "all", percentLeft: 82, resetsAt: reset },
|
|
162
|
+
{ id: "weekly", scope: "all", percentLeft: 64, resetsAt: reset },
|
|
163
|
+
{ id: "opus-weekly", scope: "opus", percentLeft: 93, resetsAt: reset },
|
|
164
|
+
{ id: "sonnet-weekly", scope: "sonnet", percentLeft: 50, resetsAt: reset },
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("a legacy Claude reply with a missing model bucket keeps the windows it has", async () => {
|
|
169
|
+
const windows = parseClaudeUsagePayload({
|
|
170
|
+
five_hour: { utilization: 18, resets_at: reset },
|
|
171
|
+
seven_day_opus: null,
|
|
172
|
+
});
|
|
173
|
+
expect(windows).toEqual([{ id: "five-hour", scope: "all", percentLeft: 82, resetsAt: reset }]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("a Claude window without a reset keeps a null reset instead of dropping the account", async () => {
|
|
177
|
+
const windows = parseClaudeUsagePayload({ five_hour: { utilization: 60 } });
|
|
178
|
+
expect(windows).toEqual([{ id: "five-hour", scope: "all", percentLeft: 40, resetsAt: null }]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a Codex usage reply keeps code review out of the shared limits", async () => {
|
|
182
|
+
const windows = parseCodexUsagePayload({
|
|
183
|
+
rate_limit: {
|
|
184
|
+
primary_window: { used_percent: 88, reset_at: reset, limit_window_seconds: 18000 },
|
|
185
|
+
secondary_window: { used_percent: 60, reset_at: reset, limit_window_seconds: 604800 },
|
|
186
|
+
},
|
|
187
|
+
code_review_rate_limit: { primary_window: { used_percent: 33, reset_at: reset, limit_window_seconds: 18000 } },
|
|
188
|
+
}, now);
|
|
189
|
+
expect(windows).toEqual([
|
|
190
|
+
{ id: "five-hour", scope: "all", percentLeft: 12, resetsAt: reset },
|
|
191
|
+
{ id: "weekly", scope: "all", percentLeft: 40, resetsAt: reset },
|
|
192
|
+
{ id: "review-five-hour", scope: "code-review", percentLeft: 67, resetsAt: reset },
|
|
193
|
+
]);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("a Codex primary window lasting a week reads as weekly, not five-hour", async () => {
|
|
197
|
+
const windows = parseCodexUsagePayload({
|
|
198
|
+
rate_limit: { primary_window: { used_percent: 29, reset_at: reset, limit_window_seconds: 604800 } },
|
|
199
|
+
}, now);
|
|
200
|
+
expect(windows).toEqual([{ id: "weekly", scope: "all", percentLeft: 71, resetsAt: reset }]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("a Codex review window lasting a week reads as review-weekly, not review-five-hour", async () => {
|
|
204
|
+
const windows = parseCodexUsagePayload({
|
|
205
|
+
code_review_rate_limit: {
|
|
206
|
+
primary_window: { used_percent: 5, reset_at: reset, limit_window_seconds: 604800 },
|
|
207
|
+
secondary_window: { used_percent: 12, reset_at: reset, limit_window_seconds: 18000 },
|
|
208
|
+
},
|
|
209
|
+
}, now);
|
|
210
|
+
expect(windows).toEqual([
|
|
211
|
+
{ id: "review-weekly", scope: "code-review", percentLeft: 95, resetsAt: reset },
|
|
212
|
+
{ id: "review-five-hour", scope: "code-review", percentLeft: 88, resetsAt: reset },
|
|
213
|
+
]);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("a Codex reply with relative resets counts them from the read time", async () => {
|
|
217
|
+
const windows = parseCodexUsagePayload({
|
|
218
|
+
rate_limit: {
|
|
219
|
+
primary_window: { used_percent: 29, reset_at: 1783374300, limit_window_seconds: 18000 },
|
|
220
|
+
secondary_window: { used_percent: 57, reset_after_seconds: 3600 },
|
|
221
|
+
},
|
|
222
|
+
}, now);
|
|
223
|
+
expect(windows).toEqual([
|
|
224
|
+
{ id: "five-hour", scope: "all", percentLeft: 71, resetsAt: new Date(1783374300 * 1000).toISOString() },
|
|
225
|
+
{ id: "weekly", scope: "all", percentLeft: 43, resetsAt: "2030-01-01T01:00:00.000Z" },
|
|
226
|
+
]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("a Codex reply with an exhausted model bucket keeps that bucket, not just the shared windows", async () => {
|
|
230
|
+
const windows = parseCodexUsagePayload({
|
|
231
|
+
rate_limit: {
|
|
232
|
+
primary_window: { used_percent: 15, reset_at: 1783374300, limit_window_seconds: 18000 },
|
|
233
|
+
secondary_window: { used_percent: 48, reset_after_seconds: 3600 },
|
|
234
|
+
},
|
|
235
|
+
code_review_rate_limit: {
|
|
236
|
+
primary_window: { used_percent: 5, reset_after_seconds: 1800 },
|
|
237
|
+
secondary_window: { used_percent: 12, reset_after_seconds: 7200 },
|
|
238
|
+
},
|
|
239
|
+
additional_rate_limits: [
|
|
240
|
+
{
|
|
241
|
+
limit_name: "GPT-Preview-Spark",
|
|
242
|
+
metered_feature: "codex_previewfeature",
|
|
243
|
+
rate_limit: {
|
|
244
|
+
primary_window: { used_percent: 33, reset_after_seconds: 1200 },
|
|
245
|
+
secondary_window: { used_percent: 70, reset_after_seconds: 5400 },
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
}, now);
|
|
250
|
+
expect(windows).toEqual([
|
|
251
|
+
{ id: "five-hour", scope: "all", percentLeft: 85, resetsAt: new Date(1783374300 * 1000).toISOString() },
|
|
252
|
+
{ id: "weekly", scope: "all", percentLeft: 52, resetsAt: "2030-01-01T01:00:00.000Z" },
|
|
253
|
+
{ id: "review-five-hour", scope: "code-review", percentLeft: 95, resetsAt: "2030-01-01T00:30:00.000Z" },
|
|
254
|
+
{ id: "review-weekly", scope: "code-review", percentLeft: 88, resetsAt: "2030-01-01T02:00:00.000Z" },
|
|
255
|
+
{ id: "model-codex_previewfeature-5h", scope: "codex_previewfeature", percentLeft: 67, resetsAt: "2030-01-01T00:20:00.000Z" },
|
|
256
|
+
{ id: "model-codex_previewfeature-7d", scope: "codex_previewfeature", percentLeft: 30, resetsAt: "2030-01-01T01:30:00.000Z" },
|
|
257
|
+
]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("a Codex rejection on every usage address reads as login needed", async () => {
|
|
261
|
+
const calls: string[] = [];
|
|
262
|
+
const fetchImpl: FetchImpl = (url) => {
|
|
263
|
+
calls.push(url);
|
|
264
|
+
return Promise.resolve(statusResponse(401));
|
|
265
|
+
};
|
|
266
|
+
const reading = await Effect.runPromise(requestCodexReading({ accessToken: "dead-token" }, fetchImpl, now));
|
|
267
|
+
expect(reading).toEqual({ code: "auth_required" });
|
|
268
|
+
expect(calls.length).toBe(2);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test("a rejected Codex pass that expired mid-read still probes both addresses before reading unchecked", async () => {
|
|
272
|
+
const calls: string[] = [];
|
|
273
|
+
const fetchImpl: FetchImpl = (url) => {
|
|
274
|
+
calls.push(url);
|
|
275
|
+
return Promise.resolve(statusResponse(401));
|
|
276
|
+
};
|
|
277
|
+
const credentials = parseCodexCredentials(JSON.stringify({ tokens: { access_token: fakeJwt(now / 1000 + 1) } }));
|
|
278
|
+
if (!credentials) throw new Error("test credential rejected");
|
|
279
|
+
expect(credentials.expiresAt).toBe(now + 1000);
|
|
280
|
+
const program = Effect.gen(function*() {
|
|
281
|
+
yield* TestClock.setTime(now + 5000);
|
|
282
|
+
return yield* requestCodexReading(credentials, fetchImpl, now);
|
|
283
|
+
}).pipe(Effect.provide(TestClock.layer()));
|
|
284
|
+
const reading = await Effect.runPromise(program);
|
|
285
|
+
expect(calls.length).toBe(2);
|
|
286
|
+
expect(reading).toEqual({ code: "auth_unverified" });
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test("a rejection followed by a miss does not read as unchecked when the pass was valid", async () => {
|
|
290
|
+
const calls: string[] = [];
|
|
291
|
+
const fetchImpl: FetchImpl = (url) => {
|
|
292
|
+
calls.push(url);
|
|
293
|
+
return Promise.resolve(statusResponse(url.endsWith("/wham/usage") ? 401 : 404));
|
|
294
|
+
};
|
|
295
|
+
const program = Effect.gen(function*() {
|
|
296
|
+
yield* TestClock.setTime(now);
|
|
297
|
+
return yield* requestCodexReading({ accessToken: "live-token", expiresAt: now + 60000 }, fetchImpl, now);
|
|
298
|
+
}).pipe(Effect.provide(TestClock.layer()));
|
|
299
|
+
const reading = await Effect.runPromise(program);
|
|
300
|
+
expect(calls.length).toBe(2);
|
|
301
|
+
expect(reading).toEqual({ code: "auth_required" });
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("a Codex rate limit on the first address stops without trying the second", async () => {
|
|
305
|
+
const calls: string[] = [];
|
|
306
|
+
const fetchImpl: FetchImpl = (url) => {
|
|
307
|
+
calls.push(url);
|
|
308
|
+
return Promise.resolve(statusResponse(429));
|
|
309
|
+
};
|
|
310
|
+
const reading = await Effect.runPromise(requestCodexReading({ accessToken: "live-token" }, fetchImpl, now));
|
|
311
|
+
expect(reading).toEqual({ code: "rate_limited" });
|
|
312
|
+
expect(calls.length).toBe(1);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("an unrelated OpenCode entry does not block the enrolled Go key", async () => {
|
|
316
|
+
expect(parseOpenCodeKey(JSON.stringify({ "opencode-go": { key: "go-key" }, opencode: { key: "other" } }))).toBe("go-key");
|
|
317
|
+
expect(parseOpenCodeKey(JSON.stringify({ "opencode-go": { key: "go-key" } }))).toBe("go-key");
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test("an OpenCode rejection reads as login needed, not unchecked", async () => {
|
|
321
|
+
const fetchImpl: FetchImpl = () => Promise.resolve(statusResponse(403));
|
|
322
|
+
const reading = await Effect.runPromise(requestOpenCodeReading("go-key", fetchImpl, now));
|
|
323
|
+
expect(reading).toEqual({ code: "auth_required" });
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("an OpenCode usage reply maps rolling and weekly to separate windows", async () => {
|
|
327
|
+
const windows = parseOpenCodeUsagePayload({ usage: { rollingUsage: { percentRemaining: 40, resetsAt: reset, windowSeconds: 18000 }, weeklyUsage: { percent: 88, resetsAt: reset } } }, now);
|
|
328
|
+
expect(windows).toEqual([
|
|
329
|
+
{ id: "five-hour", scope: "all", percentLeft: 40, resetsAt: reset },
|
|
330
|
+
{ id: "weekly", scope: "all", percentLeft: 12, resetsAt: reset },
|
|
331
|
+
]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
test("an OpenCode reply without a rolling duration keeps rolling separate from five-hour", async () => {
|
|
335
|
+
const windows = parseOpenCodeUsagePayload({
|
|
336
|
+
rollingUsage: { usagePercent: 18, resetInSec: 3600 },
|
|
337
|
+
weeklyUsage: { usagePercent: 42, resetInSec: 604800 },
|
|
338
|
+
monthlyUsage: { usagePercent: 7, resetInSec: 2592000 },
|
|
339
|
+
}, now);
|
|
340
|
+
expect(windows).toEqual([
|
|
341
|
+
{ id: "rolling", scope: "all", percentLeft: 82, resetsAt: "2030-01-01T01:00:00.000Z" },
|
|
342
|
+
{ id: "weekly", scope: "all", percentLeft: 58, resetsAt: "2030-01-08T00:00:00.000Z" },
|
|
343
|
+
{ id: "monthly", scope: "all", percentLeft: 93, resetsAt: "2030-01-31T00:00:00.000Z" },
|
|
344
|
+
]);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test("a named default Keychain login is not rejected as unverified", async () => {
|
|
348
|
+
expect(validClaudeKeychainService({ config_dir: "/home/user/.claude", service: "Claude Code-credentials" })).toBe(true);
|
|
349
|
+
expect(validClaudeKeychainService({ config_dir: "/home/user/.claude", service: "Claude Code-credentials-deadbeef" })).toBe(false);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test("a Pi Codex login uses its own account and keeps its credentials out of the reading", async () => {
|
|
353
|
+
const seen: Record<string, string>[] = [];
|
|
354
|
+
const fetchImpl: FetchImpl = (_url, init) => {
|
|
355
|
+
seen.push(init.headers);
|
|
356
|
+
return Promise.resolve(jsonResponse({ rate_limit: { primary_window: { used_percent: 60, reset_at: reset } } }));
|
|
357
|
+
};
|
|
358
|
+
const credentials = parseCodexCredentials(JSON.stringify({
|
|
359
|
+
"openai-codex": { type: "oauth", access: "synthetic-access", refresh: "synthetic-refresh", expires: now + 60_000, accountId: "sample-account" },
|
|
360
|
+
}));
|
|
361
|
+
if (!credentials) throw new Error("test credential rejected");
|
|
362
|
+
const reading = await Effect.runPromise(requestCodexReading(credentials, fetchImpl, now));
|
|
363
|
+
expect(seen[0]?.["ChatGPT-Account-Id"]).toBe("sample-account");
|
|
364
|
+
expect(seen[0]?.authorization).toBe("Bearer synthetic-access");
|
|
365
|
+
expect(reading).toEqual({ observedAt: new Date(now).toISOString(), windows: [{ id: "five-hour", scope: "all", percentLeft: 40, resetsAt: reset }] });
|
|
366
|
+
expect(JSON.stringify(reading)).not.toContain("synthetic");
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
test("a rejected expired Pi Codex login is unchecked rather than signed out", async () => {
|
|
370
|
+
const credentials = parseCodexCredentials(JSON.stringify({
|
|
371
|
+
"openai-codex": { type: "oauth", access: "synthetic-access", refresh: "synthetic-refresh", expires: now - 1, accountId: "sample-account" },
|
|
372
|
+
}));
|
|
373
|
+
if (!credentials) throw new Error("test credential rejected");
|
|
374
|
+
const program = Effect.gen(function*() {
|
|
375
|
+
yield* TestClock.setTime(now);
|
|
376
|
+
return yield* requestCodexReading(credentials, () => Promise.resolve(statusResponse(401)), now);
|
|
377
|
+
}).pipe(Effect.provide(TestClock.layer()));
|
|
378
|
+
const reading = await Effect.runPromise(program);
|
|
379
|
+
expect(reading).toEqual({ code: "auth_unverified" });
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test("a malformed Pi login does not fall back to another Codex login", () => {
|
|
383
|
+
const credentials = parseCodexCredentials(JSON.stringify({
|
|
384
|
+
"openai-codex": { type: "api_key", access: "synthetic-access" },
|
|
385
|
+
tokens: { access_token: "other-access" },
|
|
386
|
+
}));
|
|
387
|
+
expect(credentials).toBeUndefined();
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
test("a Codex login keeps its account header while the token stays out of the reading", async () => {
|
|
391
|
+
const seen: Record<string, string>[] = [];
|
|
392
|
+
const fetchImpl: FetchImpl = (_url, init) => {
|
|
393
|
+
seen.push(init.headers);
|
|
394
|
+
return Promise.resolve(jsonResponse({ rate_limit: { primary_window: { used_percent: 60, reset_at: reset } } }));
|
|
395
|
+
};
|
|
396
|
+
const credentials = parseCodexCredentials(JSON.stringify({ tokens: { access_token: "secret-token", account_id: "acct-1" } }));
|
|
397
|
+
if (!credentials) throw new Error("test credential rejected");
|
|
398
|
+
const reading = await Effect.runPromise(requestCodexReading(credentials, fetchImpl, now));
|
|
399
|
+
expect(seen[0]?.["ChatGPT-Account-Id"]).toBe("acct-1");
|
|
400
|
+
expect(JSON.stringify(reading)).not.toContain("secret-token");
|
|
401
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
test("no-mistakes lint does not omit the repository lint or typecheck gates", async () => {
|
|
4
|
+
const config = Bun.YAML.parse(await Bun.file(".no-mistakes.yaml").text());
|
|
5
|
+
expect(config).toMatchObject({
|
|
6
|
+
commands: { lint: "bun run lint && bun run typecheck" },
|
|
7
|
+
});
|
|
8
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { renewalExpiry } from "../../src/renewal";
|
|
3
|
+
|
|
4
|
+
test("Claude access expiry is not guessed from a non-renewable login", () => {
|
|
5
|
+
expect(renewalExpiry("claude", JSON.stringify({ claudeAiOauth: { expiresAt: 1_700_000_000_000 } }))).toBeUndefined();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
test("Codex access expiry is not taken from the identity token or mistaken for milliseconds", () => {
|
|
9
|
+
const accessClaims = btoa(JSON.stringify({ exp: 1_700_000_000 })).replace(/=+$/u, "");
|
|
10
|
+
const identityClaims = btoa(JSON.stringify({ exp: 1_900_000_000 })).replace(/=+$/u, "");
|
|
11
|
+
const json = JSON.stringify({ tokens: {
|
|
12
|
+
access_token: `${crypto.randomUUID()}.${accessClaims}.${crypto.randomUUID()}`,
|
|
13
|
+
id_token: `${crypto.randomUUID()}.${identityClaims}.${crypto.randomUUID()}`,
|
|
14
|
+
refresh_token: crypto.randomUUID(),
|
|
15
|
+
} });
|
|
16
|
+
expect(renewalExpiry("codex", json)).toBe(1_700_000_000_000);
|
|
17
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { Effect, Result } from "effect";
|
|
3
|
+
import { observationAt, parseSnapshot, pickEvidenceAt, serializeSnapshot } from "../../src/index.js";
|
|
4
|
+
|
|
5
|
+
const freshSample = Bun.file(new URL("../fixtures/snapshots/fresh.json", import.meta.url));
|
|
6
|
+
const incompleteSample = Bun.file(new URL("../fixtures/snapshots/incomplete.json", import.meta.url));
|
|
7
|
+
const observedAt = "2030-01-01T00:00:00.000Z";
|
|
8
|
+
const afterOneMinute = Date.parse("2030-01-01T00:01:00.000Z");
|
|
9
|
+
|
|
10
|
+
async function readSample(file: Bun.BunFile) {
|
|
11
|
+
return Effect.runSync(parseSnapshot(await file.text()));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function readObservation(file: Bun.BunFile, id: string) {
|
|
15
|
+
const account = (await readSample(file)).accounts.find((entry) => entry.id === id);
|
|
16
|
+
if (account === undefined) throw new Error("Sample account missing");
|
|
17
|
+
return account.observation;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
test("does not combine quota windows or drop their individual resets", async () => {
|
|
21
|
+
const observation = await readObservation(freshSample, "sample-claude");
|
|
22
|
+
expect(observation).toMatchObject({
|
|
23
|
+
status: "fresh",
|
|
24
|
+
windows: [
|
|
25
|
+
{ id: "five-hour", scope: "account", percentLeft: 40, resetsAt: "2030-01-01T05:00:00.000Z" },
|
|
26
|
+
{ id: "weekly", scope: "account", percentLeft: 12, resetsAt: "2030-01-08T00:00:00.000Z" },
|
|
27
|
+
],
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("does not offer evidence at the two-minute cutoff or discard the stale date", async () => {
|
|
32
|
+
const observation = await readObservation(freshSample, "sample-claude");
|
|
33
|
+
const cutoff = Date.parse("2030-01-01T00:02:00.000Z");
|
|
34
|
+
expect(pickEvidenceAt(observation, cutoff - 1)?.status).toBe("fresh");
|
|
35
|
+
expect(pickEvidenceAt(observation, cutoff)).toBeUndefined();
|
|
36
|
+
expect(observationAt(observation, cutoff)).toMatchObject({ status: "stale", observedAt, code: null });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("does not pick from a reset window even if it is not the first window", async () => {
|
|
40
|
+
const observation = await readObservation(freshSample, "sample-codex");
|
|
41
|
+
expect(pickEvidenceAt(observation, afterOneMinute - 1)?.status).toBe("fresh");
|
|
42
|
+
expect(pickEvidenceAt(observation, afterOneMinute)).toBeUndefined();
|
|
43
|
+
expect(observationAt(observation, afterOneMinute)).toMatchObject({
|
|
44
|
+
status: "stale",
|
|
45
|
+
observedAt: "2030-01-01T00:00:10.000Z",
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("does not treat a failed refresh as fresh even within two minutes", async () => {
|
|
50
|
+
const observation = await readObservation(incompleteSample, "sample-claude");
|
|
51
|
+
expect(pickEvidenceAt(observation, afterOneMinute)).toBeUndefined();
|
|
52
|
+
expect(observationAt(observation, afterOneMinute)).toMatchObject({
|
|
53
|
+
status: "stale",
|
|
54
|
+
observedAt,
|
|
55
|
+
code: "timeout",
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("does not substitute full quota or a known reset for unknown window data", async () => {
|
|
60
|
+
const observation = await readObservation(incompleteSample, "sample-partial");
|
|
61
|
+
expect(pickEvidenceAt(observation, afterOneMinute)).toBeUndefined();
|
|
62
|
+
expect(observationAt(observation, afterOneMinute)).toMatchObject({
|
|
63
|
+
windows: [{ percentLeft: null, resetsAt: null }],
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
function singleAccount(observation: unknown) {
|
|
68
|
+
return {
|
|
69
|
+
version: "subquo.v1",
|
|
70
|
+
writtenAt: observedAt,
|
|
71
|
+
accounts: [{ id: "sample-claude", provider: "claude", observation }],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const windowReading = {
|
|
76
|
+
id: "five-hour",
|
|
77
|
+
scope: "account",
|
|
78
|
+
percentLeft: 40,
|
|
79
|
+
resetsAt: "2030-01-01T05:00:00.000Z",
|
|
80
|
+
};
|
|
81
|
+
const freshReading = {
|
|
82
|
+
status: "fresh",
|
|
83
|
+
source: "provider",
|
|
84
|
+
observedAt,
|
|
85
|
+
validUntil: "2030-01-01T00:02:00.000Z",
|
|
86
|
+
windows: [windowReading],
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
test.each([-1, 101, Infinity, NaN])("does not serialize an impossible percent left: %p", (percentLeft) => {
|
|
90
|
+
const input = singleAccount({ ...freshReading, windows: [{ ...windowReading, percentLeft }] });
|
|
91
|
+
expect(Result.isFailure(Effect.runSync(Effect.result(serializeSnapshot(input))))).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("does not accept a fresh deadline more than two minutes after the observation", () => {
|
|
95
|
+
const input = singleAccount({ ...freshReading, validUntil: "2030-01-01T00:02:00.001Z" });
|
|
96
|
+
expect(Result.isFailure(Effect.runSync(Effect.result(serializeSnapshot(input))))).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("does not mark a reading fresh in a snapshot written at its deadline", () => {
|
|
100
|
+
const input = { ...singleAccount(freshReading), writtenAt: "2030-01-01T00:02:00.000Z" };
|
|
101
|
+
expect(Result.isFailure(Effect.runSync(Effect.result(parseSnapshot(JSON.stringify(input)))))).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("does not accept a fresh deadline that claims time past a window reset", () => {
|
|
105
|
+
const input = singleAccount({
|
|
106
|
+
...freshReading,
|
|
107
|
+
windows: [{ ...windowReading, resetsAt: "2030-01-01T00:01:00.000Z" }],
|
|
108
|
+
});
|
|
109
|
+
expect(Result.isFailure(Effect.runSync(Effect.result(parseSnapshot(JSON.stringify(input)))))).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test.each(["timeout", "rate_limited", "auth_required", "auth_unverified"])(
|
|
113
|
+
"does not offer pick evidence for unavailable quota with code %s",
|
|
114
|
+
(code) => {
|
|
115
|
+
const input = singleAccount({ status: "unavailable", code });
|
|
116
|
+
const snapshot = Effect.runSync(parseSnapshot(JSON.stringify(input)));
|
|
117
|
+
const account = snapshot.accounts.at(0);
|
|
118
|
+
if (account === undefined) throw new Error("Sample account missing");
|
|
119
|
+
expect(account.observation).toEqual({ status: "unavailable", code });
|
|
120
|
+
expect(pickEvidenceAt(account.observation, afterOneMinute)).toBeUndefined();
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
function fakeToken() {
|
|
125
|
+
return [crypto.randomUUID(), crypto.randomUUID()].join(".");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
test("does not copy runtime credential data into decoded output or serialized snapshots", () => {
|
|
129
|
+
const secret = fakeToken();
|
|
130
|
+
const input = {
|
|
131
|
+
...singleAccount({
|
|
132
|
+
...freshReading,
|
|
133
|
+
rawResponse: { accessToken: secret },
|
|
134
|
+
windows: [{ ...windowReading, token: secret }],
|
|
135
|
+
}),
|
|
136
|
+
login: { token: secret },
|
|
137
|
+
[secret]: true,
|
|
138
|
+
};
|
|
139
|
+
const decoded = Effect.runSync(parseSnapshot(JSON.stringify(input)));
|
|
140
|
+
const serialized = Effect.runSync(serializeSnapshot(input));
|
|
141
|
+
for (const output of [JSON.stringify(decoded), serialized]) {
|
|
142
|
+
expect(output.includes(secret)).toBe(false);
|
|
143
|
+
}
|
|
144
|
+
expect(JSON.parse(JSON.stringify(decoded))).toEqual(singleAccount(freshReading));
|
|
145
|
+
expect(JSON.parse(serialized)).toEqual(singleAccount(freshReading));
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("does not carry extra fields or enrolled account IDs in the committed samples", async () => {
|
|
149
|
+
for (const sample of [freshSample, incompleteSample]) {
|
|
150
|
+
const raw = await sample.text();
|
|
151
|
+
const snapshot = Effect.runSync(parseSnapshot(raw));
|
|
152
|
+
expect(JSON.parse(raw)).toEqual(snapshot);
|
|
153
|
+
expect(snapshot.accounts.every(({ id }) => id.startsWith("sample-"))).toBe(true);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("does not echo rejected tokens in JSON errors or turn a raw vendor error into a safe code", () => {
|
|
158
|
+
const secret = fakeToken();
|
|
159
|
+
const input = singleAccount({ status: "unavailable", code: secret });
|
|
160
|
+
for (const boundary of [
|
|
161
|
+
Effect.asVoid(parseSnapshot(JSON.stringify(input))),
|
|
162
|
+
Effect.asVoid(parseSnapshot(`{"${secret}"`)),
|
|
163
|
+
Effect.asVoid(serializeSnapshot(input)),
|
|
164
|
+
]) {
|
|
165
|
+
const result = Effect.runSync(Effect.result(boundary));
|
|
166
|
+
expect(Bun.inspect(result).includes(secret)).toBe(false);
|
|
167
|
+
expect(Result.isFailure(result) && String(result.failure)).toBe("InvalidSnapshot");
|
|
168
|
+
}
|
|
169
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@avi2dg/checks/tsconfig.effect.json",
|
|
3
|
+
"compilerOptions": { "types": ["bun"], "allowImportingTsExtensions": true, "noEmit": true, "plugins": [
|
|
4
|
+
{
|
|
5
|
+
"name": "@effect/language-service",
|
|
6
|
+
"diagnosticSeverity": {
|
|
7
|
+
"anyUnknownInErrorContext": "error",
|
|
8
|
+
"unsafeEffectTypeAssertion": "error",
|
|
9
|
+
"missingEffectError": "error",
|
|
10
|
+
"unknownInEffectCatch": "error",
|
|
11
|
+
"globalErrorInEffectCatch": "error",
|
|
12
|
+
"globalErrorInEffectFailure": "error",
|
|
13
|
+
"catchUnfailableEffect": "error",
|
|
14
|
+
"catchAllToMapError": "error",
|
|
15
|
+
"missingReturnYieldStar": "error",
|
|
16
|
+
"floatingEffect": "error",
|
|
17
|
+
"outdatedApi": "error"
|
|
18
|
+
},
|
|
19
|
+
"overrides": [
|
|
20
|
+
{
|
|
21
|
+
"include": [
|
|
22
|
+
"**/*.ts",
|
|
23
|
+
"**/*.tsx",
|
|
24
|
+
"**/*.mts",
|
|
25
|
+
"**/*.cts"
|
|
26
|
+
],
|
|
27
|
+
"exclude": [
|
|
28
|
+
"tests/**",
|
|
29
|
+
"**/*.test.ts",
|
|
30
|
+
"**/*.test.tsx"
|
|
31
|
+
],
|
|
32
|
+
"options": {
|
|
33
|
+
"diagnosticSeverity": {
|
|
34
|
+
"nodeBuiltinImport": "error",
|
|
35
|
+
"asyncFunction": "error",
|
|
36
|
+
"newPromise": "error",
|
|
37
|
+
"extendsNativeError": "error",
|
|
38
|
+
"processEnv": "error",
|
|
39
|
+
"processEnvInEffect": "error"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
] }
|
|
46
|
+
}
|