tokenusage-cli 0.5.5 → 0.6.1
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/CHANGELOG.md +44 -0
- package/README.md +7 -6
- package/package.json +1 -1
- package/src/cli.js +32 -5
- package/src/commands/device-login.js +16 -17
- package/src/commands/diagnostics.js +16 -16
- package/src/commands/doctor.js +14 -11
- package/src/commands/pause.js +34 -3
- package/src/commands/project-profiles.js +5 -2
- package/src/commands/sessions.js +18 -30
- package/src/commands/status.js +22 -16
- package/src/commands/sync.js +117 -3614
- package/src/commands/uninstall.js +10 -9
- package/src/commands/upload.js +18 -12
- package/src/lib/ark-agent-plan-limits.js +4 -13
- package/src/lib/ark-coding-plan-limits.js +4 -13
- package/src/lib/cli-flags.js +109 -0
- package/src/lib/commandcode-limits.js +11 -38
- package/src/lib/doctor.js +91 -1
- package/src/lib/fs.js +10 -0
- package/src/lib/grok-limits.js +5 -18
- package/src/lib/limits-core.js +64 -0
- package/src/lib/qoder-limits.js +4 -13
- package/src/lib/queue-backup.js +24 -0
- package/src/lib/queue-drain.js +388 -0
- package/src/lib/runtime-config.js +26 -7
- package/src/lib/usage-limits.js +22 -29
- package/src/lib/workbuddy-ipc.js +191 -0
- package/src/lib/workbuddy-limits.js +656 -0
- package/src/lib/zcode-limits.js +5 -18
- package/src/repairs/claude.js +442 -0
- package/src/repairs/codebuddy.js +246 -0
- package/src/repairs/codex.js +1023 -0
- package/src/repairs/droid.js +266 -0
- package/src/repairs/grok.js +352 -0
- package/src/repairs/legacy-migrations.js +135 -0
- package/src/repairs/mimo.js +161 -0
- package/src/repairs/workbuddy.js +209 -0
- package/src/repairs/zcode.js +647 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// WorkBuddy's authenticated local proxy. The ticket proves both peers; the
|
|
4
|
+
// desktop daemon adds its own access token to relative billing requests.
|
|
5
|
+
const fs = require("node:fs");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
const net = require("node:net");
|
|
8
|
+
const crypto = require("node:crypto");
|
|
9
|
+
|
|
10
|
+
const UNAVAILABLE = "Start WorkBuddy and sign in to read its subscription quota.";
|
|
11
|
+
const MAX_FRAME_BYTES = 1024 * 1024;
|
|
12
|
+
const BILLING_PATHS = new Set([
|
|
13
|
+
"/billing/meter/get-user-resource-summary",
|
|
14
|
+
"/billing/meter/get-user-resource-paid-packages",
|
|
15
|
+
"/billing/meter/get-user-resource-free-packages",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
function readWorkbuddyIpcDiscovery(workbuddyHome) {
|
|
19
|
+
if (!workbuddyHome) return null;
|
|
20
|
+
let fd;
|
|
21
|
+
try {
|
|
22
|
+
fd = fs.openSync(path.join(workbuddyHome, "wbipc", "endpoint.json"),
|
|
23
|
+
fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0));
|
|
24
|
+
const stat = fs.fstatSync(fd);
|
|
25
|
+
if (!stat.isFile() || stat.size > 16384 || (process.platform !== "win32"
|
|
26
|
+
&& (stat.uid !== process.getuid() || (stat.mode & 0o077) !== 0))) {
|
|
27
|
+
throw new Error("Untrusted discovery file.");
|
|
28
|
+
}
|
|
29
|
+
const value = JSON.parse(fs.readFileSync(fd, "utf8"));
|
|
30
|
+
if (typeof value.ticket !== "string" || !/^[\w-]{43}$/.test(value.ticket)
|
|
31
|
+
|| typeof value.endpoint !== "string") throw new Error("Invalid discovery file.");
|
|
32
|
+
if (process.platform === "win32") {
|
|
33
|
+
if (!/^\\\\\.\\pipe\\wbipc-[\w-]+$/.test(value.endpoint)) throw new Error("Invalid pipe.");
|
|
34
|
+
} else {
|
|
35
|
+
if (!path.isAbsolute(value.endpoint)) throw new Error("Invalid socket.");
|
|
36
|
+
const socket = fs.lstatSync(value.endpoint);
|
|
37
|
+
if (!socket.isSocket() || socket.uid !== process.getuid() || (socket.mode & 0o077) !== 0) {
|
|
38
|
+
throw new Error("Untrusted socket.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { endpoint: value.endpoint, ticket: value.ticket };
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error.code === "ENOENT" || error.code === "ENOTDIR") return null;
|
|
44
|
+
// Never expose discovery contents, tickets, paths or JSON parser excerpts.
|
|
45
|
+
throw new Error("WorkBuddy local connection information is unavailable.");
|
|
46
|
+
} finally {
|
|
47
|
+
if (fd !== undefined) fs.closeSync(fd);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function computeProof(ticket, role, endpoint, clientNonce, serverNonce) {
|
|
52
|
+
const parts = [];
|
|
53
|
+
for (const field of [role === "server" ? "wbipc-s" : "wbipc-c", "1", endpoint, clientNonce, serverNonce]) {
|
|
54
|
+
const bytes = Buffer.from(field, "utf8");
|
|
55
|
+
const length = Buffer.alloc(4);
|
|
56
|
+
length.writeUInt32BE(bytes.length);
|
|
57
|
+
parts.push(length, bytes);
|
|
58
|
+
}
|
|
59
|
+
return crypto.createHmac("sha256", ticket).update(Buffer.concat(parts)).digest("base64url");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function openWorkbuddyIpc({ discovery, signal, timeoutMs = 12000, connectImpl = net.createConnection }) {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const { endpoint, ticket } = discovery;
|
|
65
|
+
const clientNonce = crypto.randomBytes(16).toString("base64url");
|
|
66
|
+
const pending = new Map();
|
|
67
|
+
let socket;
|
|
68
|
+
let phase = "challenge";
|
|
69
|
+
let channel;
|
|
70
|
+
let nextId = 1;
|
|
71
|
+
let buffer = Buffer.alloc(0);
|
|
72
|
+
let closed = false;
|
|
73
|
+
const timer = setTimeout(() => fail(new Error("WorkBuddy quota request timed out.")), timeoutMs);
|
|
74
|
+
const onAbort = () => fail(new Error("WorkBuddy quota request was cancelled."));
|
|
75
|
+
|
|
76
|
+
function fail(error) {
|
|
77
|
+
if (closed) return;
|
|
78
|
+
closed = true;
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
signal?.removeEventListener("abort", onAbort);
|
|
81
|
+
reject(error);
|
|
82
|
+
for (const handlers of pending.values()) handlers.reject(error);
|
|
83
|
+
pending.clear();
|
|
84
|
+
socket?.destroy();
|
|
85
|
+
}
|
|
86
|
+
function send(packet) {
|
|
87
|
+
socket.write(JSON.stringify(packet) + "\n");
|
|
88
|
+
}
|
|
89
|
+
function rpc(method, params, mode) {
|
|
90
|
+
if (closed) return Promise.reject(new Error(UNAVAILABLE));
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const id = nextId++;
|
|
93
|
+
pending.set(id, { resolve, reject });
|
|
94
|
+
send({ jsonrpc: "2.0", id, method, params, ...(mode ? { mode } : {}) });
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const client = {
|
|
98
|
+
close: () => fail(new Error("WorkBuddy local connection closed.")),
|
|
99
|
+
async postBilling(apiPath, body) {
|
|
100
|
+
if (!BILLING_PATHS.has(apiPath)) throw new Error("Unsupported WorkBuddy quota request.");
|
|
101
|
+
const response = await rpc(`${channel}/http.fetch`, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
path: apiPath,
|
|
104
|
+
headers: { "content-type": "application/json", accept: "application/json" },
|
|
105
|
+
body_b64: Buffer.from(JSON.stringify(body)).toString("base64"),
|
|
106
|
+
}, "call");
|
|
107
|
+
if (response?.status === 401 || response?.status === 403) {
|
|
108
|
+
const error = new Error("WorkBuddy sign-in expired.");
|
|
109
|
+
error.code = "AUTH_EXPIRED";
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
if (response?.status !== 200) throw new Error("WorkBuddy quota request failed.");
|
|
113
|
+
try {
|
|
114
|
+
if (typeof response.body_b64 !== "string" || !/^[A-Za-z0-9+/]*={0,2}$/.test(response.body_b64)) {
|
|
115
|
+
throw new Error("Invalid body.");
|
|
116
|
+
}
|
|
117
|
+
return JSON.parse(Buffer.from(response.body_b64, "base64").toString("utf8"));
|
|
118
|
+
} catch {
|
|
119
|
+
throw new Error("WorkBuddy quota response was malformed.");
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
function onPacket(packet) {
|
|
124
|
+
if (!packet || typeof packet !== "object") throw new Error("Invalid packet.");
|
|
125
|
+
if (phase === "challenge" && packet.type === "session_challenge" && packet.protocol === 1) {
|
|
126
|
+
if (typeof packet.server_nonce !== "string" || !/^[\w-]{16,128}$/.test(packet.server_nonce)
|
|
127
|
+
|| typeof packet.server_proof !== "string") throw new Error("Invalid challenge.");
|
|
128
|
+
const expected = Buffer.from(computeProof(ticket, "server", endpoint, clientNonce, packet.server_nonce));
|
|
129
|
+
const actual = Buffer.from(packet.server_proof);
|
|
130
|
+
if (expected.length !== actual.length || !crypto.timingSafeEqual(expected, actual)) {
|
|
131
|
+
throw new Error("Invalid server proof.");
|
|
132
|
+
}
|
|
133
|
+
phase = "ack";
|
|
134
|
+
send({ type: "session_prove", client_proof: computeProof(ticket, "client", endpoint, clientNonce, packet.server_nonce) });
|
|
135
|
+
} else if (phase === "ack" && packet.type === "session_hello_ack" && packet.protocol === 1
|
|
136
|
+
&& Array.isArray(packet.pipes) && packet.pipes.includes("wb.request")) {
|
|
137
|
+
phase = "ready";
|
|
138
|
+
rpc("broker/GetPipe", { pipe: "wb.request" }).then((result) => {
|
|
139
|
+
if (closed) return;
|
|
140
|
+
if (result?.channel !== "c:wb.request" || !Array.isArray(result.methods)
|
|
141
|
+
|| !result.methods.includes("http.fetch")) {
|
|
142
|
+
fail(new Error("WorkBuddy billing proxy is unavailable."));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
channel = result.channel;
|
|
146
|
+
resolve(client);
|
|
147
|
+
}, () => fail(new Error(UNAVAILABLE)));
|
|
148
|
+
} else if (phase === "ready" && packet.jsonrpc === "2.0" && pending.has(packet.id)) {
|
|
149
|
+
const handlers = pending.get(packet.id);
|
|
150
|
+
pending.delete(packet.id);
|
|
151
|
+
if (packet.error) handlers.reject(new Error("WorkBuddy local quota request was rejected."));
|
|
152
|
+
else handlers.resolve(packet.result);
|
|
153
|
+
} else {
|
|
154
|
+
throw new Error("Unexpected packet.");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
158
|
+
if (signal?.aborted) { onAbort(); return; }
|
|
159
|
+
try {
|
|
160
|
+
socket = connectImpl(endpoint);
|
|
161
|
+
socket.on("connect", () => send({
|
|
162
|
+
type: "session_hello", protocol_min: 1, protocol_max: 1,
|
|
163
|
+
client_nonce: clientNonce,
|
|
164
|
+
ticket_id: crypto.createHash("sha256").update(ticket, "utf8").digest("hex").slice(0, 16),
|
|
165
|
+
client: { kind: "tokentracker", id: "subscription-quota", version: "1" },
|
|
166
|
+
}));
|
|
167
|
+
socket.on("data", (chunk) => {
|
|
168
|
+
if (closed) return;
|
|
169
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
170
|
+
if (buffer.length > MAX_FRAME_BYTES) { fail(new Error("WorkBuddy quota response was too large.")); return; }
|
|
171
|
+
try {
|
|
172
|
+
let index;
|
|
173
|
+
while ((index = buffer.indexOf(10)) >= 0 && !closed) {
|
|
174
|
+
const line = buffer.subarray(0, index);
|
|
175
|
+
buffer = buffer.subarray(index + 1);
|
|
176
|
+
if (line.length) onPacket(JSON.parse(line.toString("utf8")));
|
|
177
|
+
}
|
|
178
|
+
} catch {
|
|
179
|
+
fail(new Error("WorkBuddy local connection authentication or protocol failed."));
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
socket.on("error", () => fail(new Error(UNAVAILABLE)));
|
|
183
|
+
socket.on("close", () => fail(new Error(UNAVAILABLE)));
|
|
184
|
+
socket.on("end", () => fail(new Error(UNAVAILABLE)));
|
|
185
|
+
} catch {
|
|
186
|
+
fail(new Error(UNAVAILABLE));
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = { UNAVAILABLE, readWorkbuddyIpcDiscovery, openWorkbuddyIpc };
|