@scrylog/cli 0.1.4 → 0.3.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/README.md +46 -7
- package/package.json +13 -10
- package/src/api/docs/sessions.ts +3 -2
- package/src/api/docs/system.ts +3 -0
- package/src/api/docs/usage.ts +2 -2
- package/src/api/routes/sessions.ts +6 -2
- package/src/api/routes/system.ts +4 -1
- package/src/api/routes/webhooks/claude.ts +9 -21
- package/src/api/routes/webhooks/opencode.ts +6 -1
- package/src/api/routes.ts +1 -1
- package/src/api/schemas/common.ts +8 -0
- package/src/api/schemas/sessions.ts +15 -1
- package/src/api/schemas/usage.ts +1 -0
- package/src/cli/commands.ts +170 -26
- package/src/cli/index.ts +18 -0
- package/src/cli/program.ts +175 -0
- package/src/daemon/collectors/claude/collector.ts +11 -1
- package/src/daemon/collectors/claude/discovery.ts +2 -23
- package/src/daemon/collectors/codex/collector.ts +3 -3
- package/src/daemon/collectors/git.ts +60 -2
- package/src/daemon/collectors/herdr/collector.ts +128 -0
- package/src/daemon/collectors/herdr/index.ts +2 -0
- package/src/daemon/collectors/herdr/normalization.ts +111 -0
- package/src/daemon/collectors/opencode/collector.ts +4 -2
- package/src/daemon/collectors/opencode-db.ts +17 -5
- package/src/daemon/collectors/tmux/collector.ts +21 -1
- package/src/daemon/collectors/tmux/enrichment.ts +1 -0
- package/src/daemon/collectors/usage.ts +115 -49
- package/src/daemon/index.ts +2 -0
- package/src/daemon/store.ts +109 -14
- package/src/postinstall.cjs +11 -0
- package/src/shared/config.ts +33 -0
- package/src/shared/session-exclusions.ts +5 -0
- package/src/shared/session-view.ts +67 -6
- package/src/shared/terminal-capabilities.ts +130 -0
- package/src/shared/types.ts +33 -2
- package/src/shared/version.ts +44 -0
- package/src/tui/App.tsx +240 -0
- package/src/tui/assets/providers/SOURCES.md +21 -0
- package/src/tui/assets/providers/claude-terminal.png +0 -0
- package/src/tui/assets/providers/claude-terminal.svg +62 -0
- package/src/tui/assets/providers/claude.png +0 -0
- package/src/tui/assets/providers/copilot.png +0 -0
- package/src/tui/assets/providers/copilot.svg +44 -0
- package/src/tui/assets/providers/openai-terminal.png +0 -0
- package/src/tui/assets/providers/openai-terminal.svg +62 -0
- package/src/tui/assets/providers/openai.png +0 -0
- package/src/tui/assets/providers/opencode-terminal.png +0 -0
- package/src/tui/assets/providers/opencode-terminal.svg +62 -0
- package/src/tui/assets/providers/opencode.png +0 -0
- package/src/tui/assets/providers/shell.png +0 -0
- package/src/tui/assets/providers/shell.svg +23 -0
- package/src/tui/assets/providers/unknown.png +0 -0
- package/src/tui/assets/providers/unknown.svg +23 -0
- package/src/tui/client.ts +22 -8
- package/src/tui/components/BindingsModal.tsx +82 -0
- package/src/tui/components/DashboardHeader.tsx +256 -0
- package/src/tui/components/KeyBar.tsx +67 -0
- package/src/tui/components/SessionDetailModal.tsx +169 -0
- package/src/tui/components/SessionsView.tsx +184 -0
- package/src/tui/components/TabBar.tsx +40 -0
- package/src/tui/components/UsageSummaryPane.tsx +272 -0
- package/src/tui/components/UsageView.tsx +242 -0
- package/src/tui/components/tool-icons.ts +24 -0
- package/src/tui/index.tsx +106 -0
- package/src/tui/keybindings.ts +49 -0
- package/src/tui/model.ts +295 -0
- package/src/tui/state.ts +77 -0
- package/src/tui/theme.ts +110 -0
- package/src/usage/README.md +3 -3
- package/src/usage/clients/anthropic-oauth-usage.ts +100 -20
- package/src/usage/clients/anthropic-web-usage.ts +133 -49
- package/src/usage/clients/claude-cli-probe.ts +458 -132
- package/src/usage/clients/codex-app-server.ts +354 -0
- package/src/usage/clients/codex-oauth-usage.ts +143 -16
- package/src/usage/clients/opencode-go-api.ts +100 -0
- package/src/usage/clients/opencode-go-usage.ts +163 -27
- package/src/usage/errors.ts +33 -0
- package/src/usage/index.ts +2 -1
- package/src/usage/local/opencode-go-local.ts +194 -0
- package/src/usage/mappers.ts +24 -15
- package/src/usage/planner.ts +89 -25
- package/src/usage/providers/claude.ts +203 -83
- package/src/usage/providers/codex.ts +92 -11
- package/src/usage/providers/opencode-go.ts +94 -16
- package/src/usage/resolvers/browser-cookies.ts +679 -69
- package/src/usage/resolvers/claude-auth.ts +150 -41
- package/src/usage/resolvers/claude-desktop-auth.ts +170 -0
- package/src/usage/resolvers/claude-web.ts +72 -6
- package/src/usage/resolvers/codex-auth.ts +74 -17
- package/src/usage/resolvers/keychain-coordinator.ts +397 -0
- package/src/usage/resolvers/opencode-go-web.ts +47 -6
- package/src/usage/types.ts +37 -2
- package/src/usage/web-session-cache.ts +37 -0
- package/src/neo-blessed.d.ts +0 -4
- package/src/tui/app.ts +0 -920
- package/src/tui/index.ts +0 -193
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { UsageError } from "../errors";
|
|
3
|
+
|
|
4
|
+
export interface KeychainProcessResult {
|
|
5
|
+
stdout: string;
|
|
6
|
+
code: number | null;
|
|
7
|
+
killed: boolean;
|
|
8
|
+
signal: string | null;
|
|
9
|
+
spawnErrorCode?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type KeychainRun = (
|
|
13
|
+
file: string,
|
|
14
|
+
args: string[],
|
|
15
|
+
options: { timeoutMs: number; signal?: AbortSignal },
|
|
16
|
+
) => Promise<KeychainProcessResult>;
|
|
17
|
+
|
|
18
|
+
export interface KeychainCoordinatorOptions {
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
cooldownMs?: number;
|
|
21
|
+
timeoutCooldownMs?: number;
|
|
22
|
+
now?: () => number;
|
|
23
|
+
run?: KeychainRun;
|
|
24
|
+
allowPrompts?: () => boolean;
|
|
25
|
+
onFailure?: (failure: KeychainFailure) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const USER_CANCELED_EXIT = 51;
|
|
29
|
+
const ACCESS_DENIED_EXIT = 45;
|
|
30
|
+
const ITEM_NOT_FOUND_EXIT = 44;
|
|
31
|
+
/** errSecInteractionNotAllowed: the process cannot prompt for authorization. */
|
|
32
|
+
const INTERACTION_NOT_ALLOWED_EXIT = 36;
|
|
33
|
+
|
|
34
|
+
export type KeychainFailureCode =
|
|
35
|
+
| "interaction-not-allowed"
|
|
36
|
+
| "access-denied"
|
|
37
|
+
| "user-canceled"
|
|
38
|
+
| "prompt-unanswered"
|
|
39
|
+
| "cooldown"
|
|
40
|
+
| "item-not-found"
|
|
41
|
+
| "spawn-failure"
|
|
42
|
+
| "read-failure";
|
|
43
|
+
|
|
44
|
+
export interface KeychainFailure {
|
|
45
|
+
code: KeychainFailureCode;
|
|
46
|
+
service: string;
|
|
47
|
+
account?: string;
|
|
48
|
+
at: number;
|
|
49
|
+
cooldownUntil?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Safe Storage items whose cooperation decrypts Claude-side credential caches. */
|
|
53
|
+
export const CLAUDE_SAFE_STORAGE_ITEMS = [{ service: "Claude Safe Storage", account: "Claude" }] as const;
|
|
54
|
+
|
|
55
|
+
const FAILURE_READABLE: Record<KeychainFailureCode, string> = {
|
|
56
|
+
"interaction-not-allowed": "requires user interaction which is not allowed in this session",
|
|
57
|
+
"access-denied": "access was denied",
|
|
58
|
+
"user-canceled": "was canceled by the user",
|
|
59
|
+
"prompt-unanswered": "authorization prompt went unanswered",
|
|
60
|
+
cooldown: "is cooling down after a failed read",
|
|
61
|
+
"item-not-found": "does not exist",
|
|
62
|
+
"spawn-failure": "could not be launched",
|
|
63
|
+
"read-failure": "read failed",
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** Long enough for a human to read and answer the macOS authorization dialog. */
|
|
67
|
+
const DEFAULT_PROMPT_TIMEOUT_MS = 90_000;
|
|
68
|
+
/** Suppresses prompt re-issuing after an unanswered dialog (e.g. user is away). */
|
|
69
|
+
const DEFAULT_TIMEOUT_COOLDOWN_MS = 10 * 60 * 1_000;
|
|
70
|
+
|
|
71
|
+
export const defaultRun: KeychainRun = (file, args, options) =>
|
|
72
|
+
new Promise((resolve) => {
|
|
73
|
+
execFile(
|
|
74
|
+
file,
|
|
75
|
+
args,
|
|
76
|
+
{
|
|
77
|
+
timeout: options.timeoutMs,
|
|
78
|
+
killSignal: "SIGKILL",
|
|
79
|
+
signal: options.signal,
|
|
80
|
+
encoding: "utf8",
|
|
81
|
+
},
|
|
82
|
+
(error, stdout) => {
|
|
83
|
+
// execFile reports success as a null error, so the exit code must be
|
|
84
|
+
// derived from the error only for non-zero exits; a null error means
|
|
85
|
+
// the child exited 0 with the secret on stdout.
|
|
86
|
+
const err = error as (Error & { code?: string | number; killed?: boolean; signal?: string }) | null;
|
|
87
|
+
if (!err) {
|
|
88
|
+
resolve({
|
|
89
|
+
stdout: typeof stdout === "string" ? stdout : "",
|
|
90
|
+
code: 0,
|
|
91
|
+
killed: false,
|
|
92
|
+
signal: null,
|
|
93
|
+
});
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
resolve({
|
|
97
|
+
stdout: typeof stdout === "string" ? stdout : "",
|
|
98
|
+
code: typeof err.code === "number" ? err.code : null,
|
|
99
|
+
killed: Boolean(err.killed),
|
|
100
|
+
signal: err.signal ?? null,
|
|
101
|
+
spawnErrorCode: typeof err.code === "string" ? err.code : undefined,
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
export class KeychainCoordinator {
|
|
108
|
+
private readonly timeoutMs: number;
|
|
109
|
+
private readonly cooldownMs: number;
|
|
110
|
+
private readonly timeoutCooldownMs: number;
|
|
111
|
+
private readonly now: () => number;
|
|
112
|
+
private readonly run: KeychainRun;
|
|
113
|
+
private allowPrompts: () => boolean;
|
|
114
|
+
private onFailure?: (failure: KeychainFailure) => void;
|
|
115
|
+
private readonly inFlight = new Map<string, Promise<string | null>>();
|
|
116
|
+
private readonly cooldownUntil = new Map<string, number>();
|
|
117
|
+
private readonly values = new Map<string, string>();
|
|
118
|
+
/** Most recent blocking failure per item (cleared on success). */
|
|
119
|
+
private readonly latestFailures = new Map<string, KeychainFailure>();
|
|
120
|
+
|
|
121
|
+
constructor(options: KeychainCoordinatorOptions = {}) {
|
|
122
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_PROMPT_TIMEOUT_MS;
|
|
123
|
+
this.cooldownMs = options.cooldownMs ?? 6 * 60 * 60 * 1_000;
|
|
124
|
+
this.timeoutCooldownMs = options.timeoutCooldownMs ?? DEFAULT_TIMEOUT_COOLDOWN_MS;
|
|
125
|
+
this.now = options.now ?? Date.now;
|
|
126
|
+
this.run = options.run ?? defaultRun;
|
|
127
|
+
this.allowPrompts = options.allowPrompts ?? (() => true);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
configure(configureOptions: Pick<KeychainCoordinatorOptions, "allowPrompts" | "onFailure">): void {
|
|
131
|
+
if (configureOptions.allowPrompts) this.allowPrompts = configureOptions.allowPrompts;
|
|
132
|
+
if (configureOptions.onFailure !== undefined) this.onFailure = configureOptions.onFailure;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
isCoolingDown(service: string, account?: string): boolean {
|
|
136
|
+
const until = this.cooldownUntil.get(this.keyFor(service, account));
|
|
137
|
+
return until !== undefined && this.now() < until;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
clearCooldown(service: string, account?: string): void {
|
|
141
|
+
this.cooldownUntil.delete(this.keyFor(service, account));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Clears the memoized value for one item so the next read spawns a query again. */
|
|
145
|
+
clearMemo(service: string, account?: string): void {
|
|
146
|
+
const key = this.keyFor(service, account);
|
|
147
|
+
if (this.values.delete(key)) this.debugMemo("cleared", key);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Clears every memoized keychain value (daemon restart semantics, tests). */
|
|
151
|
+
clearAllMemo(): void {
|
|
152
|
+
this.values.clear();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
isMemoized(service: string, account?: string): boolean {
|
|
156
|
+
return this.values.has(this.keyFor(service, account));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
resetForTesting(): void {
|
|
160
|
+
this.inFlight.clear();
|
|
161
|
+
this.cooldownUntil.clear();
|
|
162
|
+
this.values.clear();
|
|
163
|
+
this.latestFailures.clear();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async read(service: string, account?: string, options?: { signal?: AbortSignal }): Promise<string | null> {
|
|
167
|
+
const key = this.keyFor(service, account);
|
|
168
|
+
// A cooldown established by a denied read wins over a memo from an earlier
|
|
169
|
+
// success: the item was later rejected by the user, so its value must not
|
|
170
|
+
// be served until the cooldown expires.
|
|
171
|
+
if (this.isCoolingDown(service, account)) {
|
|
172
|
+
const original = this.latestFailures.get(key);
|
|
173
|
+
this.recordFailure(
|
|
174
|
+
{
|
|
175
|
+
code: "cooldown",
|
|
176
|
+
service,
|
|
177
|
+
account,
|
|
178
|
+
at: this.now(),
|
|
179
|
+
cooldownUntil: this.cooldownUntil.get(key),
|
|
180
|
+
},
|
|
181
|
+
original,
|
|
182
|
+
);
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const memoized = this.values.get(key);
|
|
186
|
+
if (memoized !== undefined) {
|
|
187
|
+
this.debugMemo("hit", key);
|
|
188
|
+
return memoized;
|
|
189
|
+
}
|
|
190
|
+
if (!this.allowPrompts()) return null;
|
|
191
|
+
|
|
192
|
+
const existing = this.inFlight.get(key);
|
|
193
|
+
if (existing) return this.joinWithSignal(existing, options?.signal);
|
|
194
|
+
|
|
195
|
+
const attempt = this.readOnce(service, account, key, options).finally(() => {
|
|
196
|
+
if (this.inFlight.get(key) === attempt) this.inFlight.delete(key);
|
|
197
|
+
});
|
|
198
|
+
this.inFlight.set(key, attempt);
|
|
199
|
+
return this.joinWithSignal(attempt, options?.signal);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private async readOnce(
|
|
203
|
+
service: string,
|
|
204
|
+
account: string | undefined,
|
|
205
|
+
key: string,
|
|
206
|
+
options?: { signal?: AbortSignal },
|
|
207
|
+
): Promise<string | null> {
|
|
208
|
+
if (options?.signal?.aborted) throw new UsageError("cancelled", "Keychain read cancelled");
|
|
209
|
+
|
|
210
|
+
const args = ["find-generic-password", "-s", service];
|
|
211
|
+
if (account) args.push("-a", account);
|
|
212
|
+
args.push("-w");
|
|
213
|
+
const result = await this.run("/usr/bin/security", args, {
|
|
214
|
+
timeoutMs: this.timeoutMs,
|
|
215
|
+
signal: options?.signal,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
if (options?.signal?.aborted) throw new UsageError("cancelled", "Keychain read cancelled");
|
|
219
|
+
|
|
220
|
+
if (result.code === 0 && result.stdout.trim()) {
|
|
221
|
+
this.cooldownUntil.delete(key);
|
|
222
|
+
this.latestFailures.delete(key);
|
|
223
|
+
const value = result.stdout.trim();
|
|
224
|
+
this.values.set(key, value);
|
|
225
|
+
this.debugMemo("stored", key);
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
if (result.code === USER_CANCELED_EXIT || result.code === ACCESS_DENIED_EXIT) {
|
|
229
|
+
this.cooldownUntil.set(key, this.now() + this.cooldownMs);
|
|
230
|
+
this.values.delete(key);
|
|
231
|
+
this.debugMemo("cleared", key);
|
|
232
|
+
this.recordFailure(
|
|
233
|
+
{
|
|
234
|
+
code: result.code === USER_CANCELED_EXIT ? "user-canceled" : "access-denied",
|
|
235
|
+
service,
|
|
236
|
+
account,
|
|
237
|
+
at: this.now(),
|
|
238
|
+
cooldownUntil: this.cooldownUntil.get(key),
|
|
239
|
+
},
|
|
240
|
+
);
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
// A timed-out (killed) read means the authorization dialog went unanswered:
|
|
244
|
+
// re-prompting every poll would hammer the user, so apply the short
|
|
245
|
+
// timeout cooldown instead of leaving the item unbounded.
|
|
246
|
+
if (result.killed) {
|
|
247
|
+
this.cooldownUntil.set(key, this.now() + this.timeoutCooldownMs);
|
|
248
|
+
this.values.delete(key);
|
|
249
|
+
this.recordFailure(
|
|
250
|
+
{
|
|
251
|
+
code: "prompt-unanswered",
|
|
252
|
+
service,
|
|
253
|
+
account,
|
|
254
|
+
at: this.now(),
|
|
255
|
+
cooldownUntil: this.cooldownUntil.get(key),
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
if (result.code === ITEM_NOT_FOUND_EXIT || result.spawnErrorCode !== undefined) {
|
|
261
|
+
// Item-not-found and spawn failures leave no user-visible trace, so
|
|
262
|
+
// without a cooldown the daemon would re-spawn security on every poll
|
|
263
|
+
// for an item that does not exist yet (created e.g. after a later login).
|
|
264
|
+
this.cooldownUntil.set(key, this.now() + this.timeoutCooldownMs);
|
|
265
|
+
this.values.delete(key);
|
|
266
|
+
this.debugReadFailure(key, result);
|
|
267
|
+
this.recordFailure(
|
|
268
|
+
{
|
|
269
|
+
code: result.spawnErrorCode !== undefined ? "spawn-failure" : "item-not-found",
|
|
270
|
+
service,
|
|
271
|
+
account,
|
|
272
|
+
at: this.now(),
|
|
273
|
+
cooldownUntil: this.cooldownUntil.get(key),
|
|
274
|
+
},
|
|
275
|
+
);
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
if (result.code === INTERACTION_NOT_ALLOWED_EXIT) {
|
|
279
|
+
this.recordFailure(
|
|
280
|
+
{
|
|
281
|
+
code: "interaction-not-allowed",
|
|
282
|
+
service,
|
|
283
|
+
account,
|
|
284
|
+
at: this.now(),
|
|
285
|
+
},
|
|
286
|
+
);
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
this.debugReadFailure(key, result);
|
|
290
|
+
this.recordFailure(
|
|
291
|
+
{
|
|
292
|
+
code: "read-failure",
|
|
293
|
+
service,
|
|
294
|
+
account,
|
|
295
|
+
at: this.now(),
|
|
296
|
+
},
|
|
297
|
+
);
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Describes the latest blocking failure of one keychain item (cleared after
|
|
303
|
+
* a successful read), if that failure is actionable. Benign states — the
|
|
304
|
+
* item does not exist at all — are not surfaced as diagnostics.
|
|
305
|
+
*/
|
|
306
|
+
describeService(service: string, account?: string): string | null {
|
|
307
|
+
const failure = this.latestFailures.get(this.keyFor(service, account));
|
|
308
|
+
if (!failure || failure.code === "item-not-found") return null;
|
|
309
|
+
return this.renderFailure(failure);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* First actionable failure among the given items, or null when none of them
|
|
314
|
+
* has a blocking failure (missing items and other benign states are ignored).
|
|
315
|
+
*/
|
|
316
|
+
describeBlockingFailure(
|
|
317
|
+
items: ReadonlyArray<{ service: string; account?: string }>,
|
|
318
|
+
): string | null {
|
|
319
|
+
for (const { service, account } of items) {
|
|
320
|
+
const rendered = this.describeService(service, account);
|
|
321
|
+
if (rendered) return rendered;
|
|
322
|
+
}
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** Renders a failure without any credential material. */
|
|
327
|
+
private renderFailure(failure: KeychainFailure): string {
|
|
328
|
+
let text = `Keychain item "${failure.service}" ${FAILURE_READABLE[failure.code]}`;
|
|
329
|
+
if (failure.code === "interaction-not-allowed") {
|
|
330
|
+
text +=
|
|
331
|
+
" — the daemon is running in a non-interactive launch session (background session, pane multiplexer, or service). Start the daemon from a GUI terminal session, or add the daemon binary to the item's access list in Keychain Access";
|
|
332
|
+
}
|
|
333
|
+
if (failure.cooldownUntil) {
|
|
334
|
+
text += ` (cooling down until ${new Date(failure.cooldownUntil).toISOString()})`;
|
|
335
|
+
}
|
|
336
|
+
return text;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private recordFailure(failure: KeychainFailure, baseForCooldown?: KeychainFailure): void {
|
|
340
|
+
const key = this.keyFor(failure.service, failure.account);
|
|
341
|
+
// When a cooldown gate re-reports, carry the original blocking cause so
|
|
342
|
+
// diagnostics name the root failure, not just the cooldown.
|
|
343
|
+
const recorded: KeychainFailure = baseForCooldown && failure.code === "cooldown"
|
|
344
|
+
? { ...baseForCooldown, at: failure.at, cooldownUntil: failure.cooldownUntil }
|
|
345
|
+
: failure;
|
|
346
|
+
if (recorded.code === "cooldown") {
|
|
347
|
+
recorded.code = baseForCooldown?.code ?? "cooldown";
|
|
348
|
+
}
|
|
349
|
+
this.latestFailures.set(key, recorded);
|
|
350
|
+
this.onFailure?.(recorded);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private debugReadFailure(key: string, result: KeychainProcessResult): void {
|
|
354
|
+
if (process.env.SCRYLOG_DEBUG_AUTH) {
|
|
355
|
+
const [service, account] = key.split("\u0000");
|
|
356
|
+
process.stderr.write(
|
|
357
|
+
`[scrylog:keychain] read failed: ${service}${account ? ` (account: ${account})` : ""}` +
|
|
358
|
+
` code=${result.code} killed=${result.killed}` +
|
|
359
|
+
`${result.spawnErrorCode ? ` spawnError=${result.spawnErrorCode}` : ""}\n`,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private debugMemo(action: "stored" | "hit" | "cleared", key: string): void {
|
|
365
|
+
if (process.env.SCRYLOG_DEBUG_AUTH) {
|
|
366
|
+
const [service, account] = key.split("\u0000");
|
|
367
|
+
process.stderr.write(
|
|
368
|
+
`[scrylog:keychain] memo ${action}: ${service}${account ? ` (account: ${account})` : ""}\n`,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private joinWithSignal(promise: Promise<string | null>, signal?: AbortSignal): Promise<string | null> {
|
|
374
|
+
if (!signal) return promise;
|
|
375
|
+
if (signal.aborted) return Promise.reject(new UsageError("cancelled", "Keychain read cancelled"));
|
|
376
|
+
return new Promise<string | null>((resolve, reject) => {
|
|
377
|
+
const onAbort = () => reject(new UsageError("cancelled", "Keychain read cancelled"));
|
|
378
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
379
|
+
promise.then(
|
|
380
|
+
(value) => {
|
|
381
|
+
signal.removeEventListener("abort", onAbort);
|
|
382
|
+
resolve(value);
|
|
383
|
+
},
|
|
384
|
+
(error) => {
|
|
385
|
+
signal.removeEventListener("abort", onAbort);
|
|
386
|
+
reject(error);
|
|
387
|
+
},
|
|
388
|
+
);
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private keyFor(service: string, account?: string): string {
|
|
393
|
+
return `${service}\u0000${account ?? ""}`;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export const keychainCoordinator = new KeychainCoordinator();
|
|
@@ -1,11 +1,52 @@
|
|
|
1
1
|
import type { TokenResolver } from "../resolver";
|
|
2
|
-
import {
|
|
2
|
+
import { BrowserCookieSourceCursor, buildCookieHeader } from "./browser-cookies";
|
|
3
3
|
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export interface OpenCodeGoWebCandidate {
|
|
5
|
+
source: string;
|
|
6
|
+
cookies: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface OpenCodeGoWebCredentials {
|
|
10
|
+
candidates: OpenCodeGoWebCandidate[];
|
|
11
|
+
/** Demand-driven cursor advancement after a classified auth rejection. */
|
|
12
|
+
importNext?: () => Promise<OpenCodeGoWebCandidate | null>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const OPENCODE_GO_EXCLUDED_BROWSERS: ReadonlySet<string> = new Set(["claude-desktop"]);
|
|
16
|
+
|
|
17
|
+
export class OpenCodeGoWebResolver implements TokenResolver<OpenCodeGoWebCredentials> {
|
|
18
|
+
constructor(
|
|
19
|
+
private readonly cursorFactory: () => BrowserCookieSourceCursor = () =>
|
|
20
|
+
new BrowserCookieSourceCursor("opencode.ai", ["auth", "__Host-auth", "__Host-console_session"], {
|
|
21
|
+
excludeBrowsers: OPENCODE_GO_EXCLUDED_BROWSERS,
|
|
22
|
+
}),
|
|
23
|
+
) {}
|
|
24
|
+
|
|
25
|
+
resolve(): Promise<OpenCodeGoWebCredentials | null> {
|
|
26
|
+
return Promise.resolve().then(() => this.buildCredentials());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private async buildCredentials(): Promise<OpenCodeGoWebCredentials | null> {
|
|
30
|
+
const cursor = this.cursorFactory();
|
|
31
|
+
const first = await cursor.next();
|
|
32
|
+
if (!first) return null;
|
|
33
|
+
const firstCandidate: OpenCodeGoWebCandidate = {
|
|
34
|
+
source: `${first.browser}:${first.profile}:${first.database}`,
|
|
35
|
+
cookies: buildCookieHeader(first.cookies),
|
|
36
|
+
};
|
|
37
|
+
if (!firstCandidate.cookies) return null;
|
|
38
|
+
return {
|
|
39
|
+
candidates: [firstCandidate],
|
|
40
|
+
importNext: async () => {
|
|
41
|
+
const next = await cursor.next();
|
|
42
|
+
if (!next) return null;
|
|
43
|
+
const candidate: OpenCodeGoWebCandidate = {
|
|
44
|
+
source: `${next.browser}:${next.profile}:${next.database}`,
|
|
45
|
+
cookies: buildCookieHeader(next.cookies),
|
|
46
|
+
};
|
|
47
|
+
return candidate.cookies ? candidate : null;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
9
50
|
}
|
|
10
51
|
|
|
11
52
|
async isAvailable(): Promise<boolean> {
|
package/src/usage/types.ts
CHANGED
|
@@ -28,6 +28,8 @@ export interface ProviderCostSnapshot {
|
|
|
28
28
|
currency: string;
|
|
29
29
|
/** Billing period label, e.g. "Monthly" */
|
|
30
30
|
period: string;
|
|
31
|
+
/** Remaining prepaid balance, independent from used/limit */
|
|
32
|
+
balance?: number;
|
|
31
33
|
/** Reset timestamp (ISO 8601) */
|
|
32
34
|
resetsAt?: string | null;
|
|
33
35
|
/** Last update timestamp (ISO 8601) */
|
|
@@ -97,13 +99,15 @@ export interface ClaudeUsageSnapshot {
|
|
|
97
99
|
updatedAt: Date;
|
|
98
100
|
accountEmail: string | null;
|
|
99
101
|
accountOrganization: string | null;
|
|
102
|
+
/** Organization UUID when the source exposes one (Web membership, OAuth profile). */
|
|
103
|
+
accountOrganizationId: string | null;
|
|
100
104
|
loginMethod: string | null;
|
|
101
105
|
subscriptionType: string | null;
|
|
102
106
|
rateLimitTier: string | null;
|
|
103
107
|
}
|
|
104
108
|
|
|
105
109
|
export interface OpenCodeGoUsageSnapshot {
|
|
106
|
-
source: "web";
|
|
110
|
+
source: "web" | "local" | "api";
|
|
107
111
|
hasMonthlyUsage: boolean;
|
|
108
112
|
rollingUsagePercent: number;
|
|
109
113
|
weeklyUsagePercent: number;
|
|
@@ -146,6 +150,11 @@ export interface ClaudeOAuthCredentials {
|
|
|
146
150
|
accessToken: string;
|
|
147
151
|
refreshToken?: string;
|
|
148
152
|
expiresAt?: string;
|
|
153
|
+
/** Internal credential authority; never exposed through the usage API. */
|
|
154
|
+
owner?: "claude" | "environment";
|
|
155
|
+
/** Internal source identity used to preserve owner recovery rules. */
|
|
156
|
+
credentialSource?: "keychain" | "credentials-file" | "claude-desktop" | "environment";
|
|
157
|
+
isStale?: boolean;
|
|
149
158
|
scopes?: string[];
|
|
150
159
|
rateLimitTier?: string;
|
|
151
160
|
subscriptionType?: string;
|
|
@@ -155,6 +164,8 @@ export interface CodexOAuthCredentials {
|
|
|
155
164
|
accessToken: string;
|
|
156
165
|
refreshToken?: string;
|
|
157
166
|
expiresAt?: string;
|
|
167
|
+
tokenExpiresAt?: number;
|
|
168
|
+
lastRefreshAt?: number;
|
|
158
169
|
accountEmail?: string | null;
|
|
159
170
|
accountId?: string | null;
|
|
160
171
|
chatgptBaseUrl?: string;
|
|
@@ -162,6 +173,15 @@ export interface CodexOAuthCredentials {
|
|
|
162
173
|
|
|
163
174
|
export interface ClaudeWebSession {
|
|
164
175
|
cookieHeader: string;
|
|
176
|
+
/** Ordered, isolated browser/profile candidates for one bounded recovery attempt. */
|
|
177
|
+
cookieCandidates?: string[];
|
|
178
|
+
/** Revision observed in the daemon-lifetime in-memory cache. */
|
|
179
|
+
cacheRevision?: number;
|
|
180
|
+
/**
|
|
181
|
+
* Demand-driven cursor advancement: imports the next browser source only
|
|
182
|
+
* after a classified authentication rejection. Never called during planning.
|
|
183
|
+
*/
|
|
184
|
+
importNext?: () => Promise<string | null>;
|
|
165
185
|
}
|
|
166
186
|
|
|
167
187
|
export type ClaudeDataSource = "oauth" | "web" | "cli" | "auto";
|
|
@@ -170,12 +190,19 @@ export interface ClaudePlannerDecision {
|
|
|
170
190
|
primary: ClaudeDataSource;
|
|
171
191
|
fallback: ClaudeDataSource[];
|
|
172
192
|
webExtras: boolean;
|
|
193
|
+
routes: ClaudePlannedRoute[];
|
|
194
|
+
webExtrasCredentials?: ClaudeWebSession;
|
|
173
195
|
}
|
|
174
196
|
|
|
197
|
+
export type ClaudePlannedRoute =
|
|
198
|
+
| { type: "oauth"; credentials: ClaudeOAuthCredentials }
|
|
199
|
+
| { type: "web"; credentials: ClaudeWebSession }
|
|
200
|
+
| { type: "cli"; credentials: string };
|
|
201
|
+
|
|
175
202
|
export type CodexDataSource = "oauth" | "auto";
|
|
176
203
|
|
|
177
204
|
export interface CodexUsageSnapshot {
|
|
178
|
-
source: "oauth";
|
|
205
|
+
source: "oauth" | "cli";
|
|
179
206
|
planType: string | null;
|
|
180
207
|
primaryWindow: RateWindowLegacy | null;
|
|
181
208
|
secondaryWindow: RateWindowLegacy | null;
|
|
@@ -184,6 +211,14 @@ export interface CodexUsageSnapshot {
|
|
|
184
211
|
unlimited: boolean;
|
|
185
212
|
balance: number | null;
|
|
186
213
|
} | null;
|
|
214
|
+
/** Monthly extra-usage credit cap when exposed by Codex. */
|
|
215
|
+
individualLimit?: {
|
|
216
|
+
used: number;
|
|
217
|
+
limit: number;
|
|
218
|
+
resetsAt: Date | null;
|
|
219
|
+
} | null;
|
|
220
|
+
/** Whether the OAuth response announced managed spend controls. */
|
|
221
|
+
spendControlPresent?: boolean;
|
|
187
222
|
updatedAt: Date;
|
|
188
223
|
accountEmail: string | null;
|
|
189
224
|
accountId: string | null;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface WebSessionObservation {
|
|
2
|
+
value: string | null;
|
|
3
|
+
revision: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface WebSessionEntry {
|
|
7
|
+
value: string;
|
|
8
|
+
revision: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class WebSessionCache {
|
|
12
|
+
private readonly entries = new Map<string, WebSessionEntry>();
|
|
13
|
+
private nextRevision = 1;
|
|
14
|
+
|
|
15
|
+
observe(key: string): WebSessionObservation {
|
|
16
|
+
const entry = this.entries.get(key);
|
|
17
|
+
return entry ? { ...entry } : { value: null, revision: 0 };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
store(key: string, value: string, expectedRevision?: number): WebSessionObservation | null {
|
|
21
|
+
const current = this.observe(key);
|
|
22
|
+
if (expectedRevision !== undefined && current.revision !== expectedRevision) return null;
|
|
23
|
+
|
|
24
|
+
const entry = { value, revision: this.nextRevision++ };
|
|
25
|
+
this.entries.set(key, entry);
|
|
26
|
+
return { ...entry };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
clear(key: string, expectedRevision: number): boolean {
|
|
30
|
+
const current = this.observe(key);
|
|
31
|
+
if (current.revision !== expectedRevision) return false;
|
|
32
|
+
if (current.revision === 0) return true;
|
|
33
|
+
return this.entries.delete(key);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const usageWebSessionCache = new WebSessionCache();
|
package/src/neo-blessed.d.ts
DELETED