tokenusage-cli 0.5.5 → 0.6.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/CHANGELOG.md +32 -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/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,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Legacy history migrations, split out of commands/sync.js: the Cursor
|
|
4
|
+
// unknown-bucket purge and the rollout cumulative-delta reparse. Both only
|
|
5
|
+
// rewrite local aggregation state once (keyed by cursors.migrations) so the
|
|
6
|
+
// forward reader starts from a clean baseline.
|
|
7
|
+
|
|
8
|
+
const fs = require("node:fs/promises");
|
|
9
|
+
const path = require("node:path");
|
|
10
|
+
|
|
11
|
+
const { ensureDir } = require("../lib/fs");
|
|
12
|
+
|
|
13
|
+
const CURSOR_UNKNOWN_MIGRATION_KEY = "cursorUnknownPurge_2026_04";
|
|
14
|
+
const ROLLOUT_CUMULATIVE_DELTA_MIGRATION_KEY = "rolloutCumulativeDeltaReparse_2026_05";
|
|
15
|
+
|
|
16
|
+
async function migrateCursorUnknownBuckets({ cursors, queuePath }) {
|
|
17
|
+
if (!cursors || typeof cursors !== "object") return;
|
|
18
|
+
cursors.migrations = cursors.migrations || {};
|
|
19
|
+
if (cursors.migrations[CURSOR_UNKNOWN_MIGRATION_KEY]) return;
|
|
20
|
+
|
|
21
|
+
const buckets = cursors.hourly?.buckets;
|
|
22
|
+
if (!buckets || typeof buckets !== "object") {
|
|
23
|
+
cursors.migrations[CURSOR_UNKNOWN_MIGRATION_KEY] = new Date().toISOString();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const retractions = [];
|
|
28
|
+
for (const key of Object.keys(buckets)) {
|
|
29
|
+
if (!key.startsWith("cursor|unknown|")) continue;
|
|
30
|
+
const hourStart = key.split("|").slice(2).join("|");
|
|
31
|
+
retractions.push(
|
|
32
|
+
JSON.stringify({
|
|
33
|
+
source: "cursor",
|
|
34
|
+
model: "unknown",
|
|
35
|
+
hour_start: hourStart,
|
|
36
|
+
input_tokens: 0,
|
|
37
|
+
cached_input_tokens: 0,
|
|
38
|
+
cache_creation_input_tokens: 0,
|
|
39
|
+
output_tokens: 0,
|
|
40
|
+
reasoning_output_tokens: 0,
|
|
41
|
+
total_tokens: 0,
|
|
42
|
+
conversation_count: 0,
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
delete buckets[key];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (retractions.length > 0) {
|
|
49
|
+
await ensureDir(path.dirname(queuePath));
|
|
50
|
+
await fs.appendFile(queuePath, retractions.join("\n") + "\n");
|
|
51
|
+
if (cursors.cursorApi) {
|
|
52
|
+
cursors.cursorApi.lastRecordTimestamp = null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
cursors.migrations[CURSOR_UNKNOWN_MIGRATION_KEY] = new Date().toISOString();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function migrateRolloutCumulativeDeltaBuckets({ cursors, queuePath, rolloutFiles }) {
|
|
60
|
+
if (!cursors || typeof cursors !== "object") return;
|
|
61
|
+
cursors.migrations = cursors.migrations || {};
|
|
62
|
+
if (cursors.migrations[ROLLOUT_CUMULATIVE_DELTA_MIGRATION_KEY]) return;
|
|
63
|
+
|
|
64
|
+
const rolloutPathSources = new Map();
|
|
65
|
+
for (const entry of Array.isArray(rolloutFiles) ? rolloutFiles : []) {
|
|
66
|
+
const filePath = typeof entry === "string" ? entry : entry?.path;
|
|
67
|
+
const source = typeof entry === "string" ? "codex" : String(entry?.source || "codex");
|
|
68
|
+
if (!filePath) continue;
|
|
69
|
+
if (source === "codex" || source === "every-code") {
|
|
70
|
+
rolloutPathSources.set(filePath, source);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (cursors.files && typeof cursors.files === "object") {
|
|
75
|
+
for (const filePath of rolloutPathSources.keys()) {
|
|
76
|
+
delete cursors.files[filePath];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// The migration clears Codex buckets and reparses the discovered corpus from
|
|
80
|
+
// byte zero. Persisted event keys belong to the cleared buckets, so retaining
|
|
81
|
+
// them can suppress the rebuild when a moved session still has an old path
|
|
82
|
+
// cursor. Rebuild the hash inventory together with the buckets. Counted
|
|
83
|
+
// compaction ids (#652) belong to the cleared buckets the same way.
|
|
84
|
+
cursors.codexHashes = [];
|
|
85
|
+
cursors.codexCompactionResponseIds = [];
|
|
86
|
+
|
|
87
|
+
const buckets = cursors.hourly?.buckets;
|
|
88
|
+
const retractions = [];
|
|
89
|
+
if (buckets && typeof buckets === "object") {
|
|
90
|
+
for (const key of Object.keys(buckets)) {
|
|
91
|
+
const [source, model, ...hourParts] = key.split("|");
|
|
92
|
+
if (source !== "codex" && source !== "every-code") continue;
|
|
93
|
+
const hourStart = hourParts.join("|");
|
|
94
|
+
retractions.push(
|
|
95
|
+
JSON.stringify({
|
|
96
|
+
source,
|
|
97
|
+
model: model || "unknown",
|
|
98
|
+
hour_start: hourStart,
|
|
99
|
+
input_tokens: 0,
|
|
100
|
+
cached_input_tokens: 0,
|
|
101
|
+
cache_creation_input_tokens: 0,
|
|
102
|
+
output_tokens: 0,
|
|
103
|
+
reasoning_output_tokens: 0,
|
|
104
|
+
total_tokens: 0,
|
|
105
|
+
billable_total_tokens: 0,
|
|
106
|
+
conversation_count: 0,
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
delete buckets[key];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const groupQueued = cursors.hourly?.groupQueued;
|
|
114
|
+
if (groupQueued && typeof groupQueued === "object") {
|
|
115
|
+
for (const key of Object.keys(groupQueued)) {
|
|
116
|
+
if (key.startsWith("codex|") || key.startsWith("every-code|")) {
|
|
117
|
+
delete groupQueued[key];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (retractions.length > 0) {
|
|
123
|
+
await ensureDir(path.dirname(queuePath));
|
|
124
|
+
await fs.appendFile(queuePath, retractions.join("\n") + "\n");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
cursors.migrations[ROLLOUT_CUMULATIVE_DELTA_MIGRATION_KEY] = new Date().toISOString();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = {
|
|
131
|
+
CURSOR_UNKNOWN_MIGRATION_KEY,
|
|
132
|
+
ROLLOUT_CUMULATIVE_DELTA_MIGRATION_KEY,
|
|
133
|
+
migrateCursorUnknownBuckets,
|
|
134
|
+
migrateRolloutCumulativeDeltaBuckets,
|
|
135
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Mimo mislabel repair (0.57.0), split out of commands/sync.js. That release
|
|
4
|
+
// read mimocode.db without the providerID filter, so the Claude/claude-mem
|
|
5
|
+
// mirror was ingested a second time as source=mimo. The repair purges every
|
|
6
|
+
// source=mimo row and cursor bucket so the next scan rebuilds it correctly;
|
|
7
|
+
// cloud orphans are cleaned server-side.
|
|
8
|
+
|
|
9
|
+
const path = require("node:path");
|
|
10
|
+
const fs = require("node:fs/promises");
|
|
11
|
+
|
|
12
|
+
const { ensureDir } = require("../lib/fs");
|
|
13
|
+
const { backupExistingFile } = require("../lib/queue-backup");
|
|
14
|
+
|
|
15
|
+
const MIMO_PROVIDER_REPAIR_KEY = "mimoClaudeMislabelRepair_2026_06";
|
|
16
|
+
|
|
17
|
+
async function resetUploadOffsetForMimoRepair(queueStatePath) {
|
|
18
|
+
if (typeof queueStatePath !== "string" || !queueStatePath) return false;
|
|
19
|
+
let state = {};
|
|
20
|
+
try {
|
|
21
|
+
state = JSON.parse(await fs.readFile(queueStatePath, "utf8"));
|
|
22
|
+
} catch (_e) {
|
|
23
|
+
state = {};
|
|
24
|
+
}
|
|
25
|
+
state.offset = 0;
|
|
26
|
+
state.updatedAt = new Date().toISOString();
|
|
27
|
+
state.note = "reset_after_mimo_claude_mislabel_repair_2026_06";
|
|
28
|
+
await ensureDir(path.dirname(queueStatePath));
|
|
29
|
+
await fs.writeFile(queueStatePath, JSON.stringify(state, null, 2) + "\n", "utf8");
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Remove every source=mimo row from a queue file (atomic rewrite, backed up
|
|
34
|
+
// first). Returns the number of rows removed. Non-JSON lines are preserved.
|
|
35
|
+
async function dropMimoQueueRows(filePath) {
|
|
36
|
+
let raw;
|
|
37
|
+
try {
|
|
38
|
+
raw = await fs.readFile(filePath, "utf8");
|
|
39
|
+
} catch (e) {
|
|
40
|
+
if (e?.code === "ENOENT") return 0;
|
|
41
|
+
throw e;
|
|
42
|
+
}
|
|
43
|
+
const kept = [];
|
|
44
|
+
let removed = 0;
|
|
45
|
+
for (const line of raw.split("\n")) {
|
|
46
|
+
if (!line.trim()) continue;
|
|
47
|
+
let row;
|
|
48
|
+
try {
|
|
49
|
+
row = JSON.parse(line);
|
|
50
|
+
} catch (_e) {
|
|
51
|
+
kept.push(line);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (row && row.source === "mimo") {
|
|
55
|
+
removed += 1;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
kept.push(line);
|
|
59
|
+
}
|
|
60
|
+
if (removed === 0) return 0;
|
|
61
|
+
await backupExistingFile(filePath);
|
|
62
|
+
const tmp = `${filePath}.mimorepair.${process.pid}.${Date.now()}`;
|
|
63
|
+
await fs.writeFile(tmp, kept.length ? kept.join("\n") + "\n" : "", "utf8");
|
|
64
|
+
await fs.rename(tmp, filePath);
|
|
65
|
+
return removed;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// One-time repair for the 0.57.0 mimo mislabel bug. Purges all source=mimo data
|
|
69
|
+
// (the mislabeled Claude/claude-mem mirror) from the local queues and cursor
|
|
70
|
+
// state, so the next sync re-parses mimocode.db with the providerID-filtered
|
|
71
|
+
// reader and rebuilds source=mimo from scratch — correct mimo-auto only. Cloud
|
|
72
|
+
// orphans (mimo rows already uploaded) are cleaned server-side separately.
|
|
73
|
+
async function repairMimoClaudeMislabel({
|
|
74
|
+
cursors,
|
|
75
|
+
queuePath,
|
|
76
|
+
queueStatePath,
|
|
77
|
+
projectQueuePath,
|
|
78
|
+
projectQueueStatePath,
|
|
79
|
+
} = {}) {
|
|
80
|
+
if (!cursors || typeof cursors !== "object") return false;
|
|
81
|
+
const migrations = (cursors.migrations ||= {});
|
|
82
|
+
if (migrations[MIMO_PROVIDER_REPAIR_KEY]) return false;
|
|
83
|
+
|
|
84
|
+
const hourly = cursors.hourly && typeof cursors.hourly === "object" ? cursors.hourly : null;
|
|
85
|
+
const hasMimoBucket =
|
|
86
|
+
hourly && hourly.buckets
|
|
87
|
+
? Object.keys(hourly.buckets).some((k) => k.startsWith("mimo|"))
|
|
88
|
+
: false;
|
|
89
|
+
|
|
90
|
+
// Nothing mimo-related anywhere → mark done so we don't re-scan every sync.
|
|
91
|
+
let mainRaw = null;
|
|
92
|
+
try {
|
|
93
|
+
mainRaw = await fs.readFile(queuePath, "utf8");
|
|
94
|
+
} catch (e) {
|
|
95
|
+
if (e?.code !== "ENOENT") throw e;
|
|
96
|
+
}
|
|
97
|
+
const hasMimoRow =
|
|
98
|
+
typeof mainRaw === "string" &&
|
|
99
|
+
mainRaw.split("\n").some((l) => {
|
|
100
|
+
if (!l.trim()) return false;
|
|
101
|
+
try {
|
|
102
|
+
return JSON.parse(l).source === "mimo";
|
|
103
|
+
} catch (_e) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (!hasMimoBucket && !hasMimoRow && !cursors.mimo) {
|
|
109
|
+
migrations[MIMO_PROVIDER_REPAIR_KEY] = new Date().toISOString();
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 1. Drop source=mimo rows from the main + project queues.
|
|
114
|
+
const removedMain = await dropMimoQueueRows(queuePath);
|
|
115
|
+
const removedProject =
|
|
116
|
+
typeof projectQueuePath === "string" && projectQueuePath
|
|
117
|
+
? await dropMimoQueueRows(projectQueuePath)
|
|
118
|
+
: 0;
|
|
119
|
+
|
|
120
|
+
// 2. Clear stale mimo buckets from the aggregation state (keys are
|
|
121
|
+
// `source|model|hour` for hourly, `projectKey|source|hour` for project).
|
|
122
|
+
if (hourly && hourly.buckets) {
|
|
123
|
+
for (const k of Object.keys(hourly.buckets)) {
|
|
124
|
+
if (k.startsWith("mimo|")) delete hourly.buckets[k];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (hourly && hourly.groupQueued) {
|
|
128
|
+
for (const k of Object.keys(hourly.groupQueued)) {
|
|
129
|
+
if (k.startsWith("mimo|")) delete hourly.groupQueued[k];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const projectHourly =
|
|
133
|
+
cursors.projectHourly && typeof cursors.projectHourly === "object"
|
|
134
|
+
? cursors.projectHourly
|
|
135
|
+
: null;
|
|
136
|
+
if (projectHourly && projectHourly.buckets) {
|
|
137
|
+
for (const k of Object.keys(projectHourly.buckets)) {
|
|
138
|
+
if (k.includes("|mimo|")) delete projectHourly.buckets[k];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 3. Reset the mimo message index so the next sync re-parses the DB fresh.
|
|
143
|
+
delete cursors.mimo;
|
|
144
|
+
|
|
145
|
+
// 4. Reset upload offsets — the queue rewrite changed byte offsets, so a full
|
|
146
|
+
// replay is required (cloud keeps latest per key; orphan mimo rows already
|
|
147
|
+
// uploaded are removed server-side).
|
|
148
|
+
if (removedMain > 0) await resetUploadOffsetForMimoRepair(queueStatePath);
|
|
149
|
+
if (removedProject > 0) await resetUploadOffsetForMimoRepair(projectQueueStatePath);
|
|
150
|
+
|
|
151
|
+
migrations[MIMO_PROVIDER_REPAIR_KEY] = {
|
|
152
|
+
appliedAt: new Date().toISOString(),
|
|
153
|
+
removedMain,
|
|
154
|
+
removedProject,
|
|
155
|
+
};
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
module.exports = {
|
|
159
|
+
MIMO_PROVIDER_REPAIR_KEY,
|
|
160
|
+
repairMimoClaudeMislabel,
|
|
161
|
+
};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// WorkBuddy context-usage repair, split out of commands/sync.js. The 0.87.9
|
|
4
|
+
// SQLite fallback counted the bounded context snapshot as cumulative input;
|
|
5
|
+
// this rebuilds WorkBuddy buckets from JSONL/trace sources.
|
|
6
|
+
|
|
7
|
+
const fs = require("node:fs/promises");
|
|
8
|
+
const path = require("node:path");
|
|
9
|
+
|
|
10
|
+
const { ensureDir } = require("../lib/fs");
|
|
11
|
+
const { bucketKey, parseWorkbuddyIncremental } = require("../lib/rollout");
|
|
12
|
+
|
|
13
|
+
const WORKBUDDY_CONTEXT_USAGE_REPAIR_KEY = "workbuddyContextUsageRepair_2026_08";
|
|
14
|
+
|
|
15
|
+
async function repairWorkbuddyContextUsage({
|
|
16
|
+
cursors,
|
|
17
|
+
queuePath,
|
|
18
|
+
queueStatePath,
|
|
19
|
+
workbuddyFiles,
|
|
20
|
+
env = process.env,
|
|
21
|
+
} = {}) {
|
|
22
|
+
if (!cursors || typeof cursors !== "object") return false;
|
|
23
|
+
const migrations = (cursors.migrations ||= {});
|
|
24
|
+
const prior = migrations[WORKBUDDY_CONTEXT_USAGE_REPAIR_KEY];
|
|
25
|
+
if (prior && !(typeof prior === "object" && prior.skipped)) return false;
|
|
26
|
+
|
|
27
|
+
const hourly = cursors.hourly && typeof cursors.hourly === "object" ? cursors.hourly : {};
|
|
28
|
+
const liveHourlyKeys = new Set(
|
|
29
|
+
Object.keys(hourly.buckets || {}).filter((key) => key.startsWith("workbuddy|")),
|
|
30
|
+
);
|
|
31
|
+
let queueRaw = "";
|
|
32
|
+
try {
|
|
33
|
+
queueRaw = await fs.readFile(queuePath, "utf8");
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e?.code !== "ENOENT") throw e;
|
|
36
|
+
}
|
|
37
|
+
const queueWorkbuddyKeys = new Set();
|
|
38
|
+
const keptQueueLines = [];
|
|
39
|
+
for (const line of queueRaw.split("\n")) {
|
|
40
|
+
if (!line.trim()) continue;
|
|
41
|
+
let row;
|
|
42
|
+
try { row = JSON.parse(line); } catch (_e) {
|
|
43
|
+
keptQueueLines.push(line);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (row && row.source === "workbuddy" && typeof row.model === "string" && typeof row.hour_start === "string") {
|
|
47
|
+
queueWorkbuddyKeys.add(bucketKey("workbuddy", row.model || "unknown", row.hour_start));
|
|
48
|
+
} else {
|
|
49
|
+
keptQueueLines.push(line);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const liveKeys = new Set([...liveHourlyKeys, ...queueWorkbuddyKeys]);
|
|
53
|
+
if (liveKeys.size === 0) {
|
|
54
|
+
migrations[WORKBUDDY_CONTEXT_USAGE_REPAIR_KEY] = {
|
|
55
|
+
status: "noop",
|
|
56
|
+
appliedAt: new Date().toISOString(),
|
|
57
|
+
reason: "no-workbuddy-history",
|
|
58
|
+
};
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const files = Array.isArray(workbuddyFiles) ? workbuddyFiles : [];
|
|
63
|
+
const discoveredPaths = new Set(
|
|
64
|
+
files.map((entry) => typeof entry === "string" ? entry : entry?.path).filter(Boolean),
|
|
65
|
+
);
|
|
66
|
+
const priorFiles = cursors.workbuddy?.fileOffsets && typeof cursors.workbuddy.fileOffsets === "object"
|
|
67
|
+
? Object.keys(cursors.workbuddy.fileOffsets)
|
|
68
|
+
: [];
|
|
69
|
+
if (files.length === 0 || priorFiles.some((filePath) => !discoveredPaths.has(filePath))) {
|
|
70
|
+
migrations[WORKBUDDY_CONTEXT_USAGE_REPAIR_KEY] = {
|
|
71
|
+
skipped: true,
|
|
72
|
+
reason: files.length === 0 ? "no-rebuild-sources" : "workbuddy_file_unreproducible",
|
|
73
|
+
at: new Date().toISOString(),
|
|
74
|
+
};
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const tmpQueue = `${queuePath}.workbuddyrebuild.${process.pid}.${Date.now()}`;
|
|
79
|
+
const tmpCursors = {
|
|
80
|
+
version: 1,
|
|
81
|
+
hourly: { buckets: {}, groupQueued: {} },
|
|
82
|
+
workbuddy: {},
|
|
83
|
+
};
|
|
84
|
+
let rebuiltRows = [];
|
|
85
|
+
try {
|
|
86
|
+
await parseWorkbuddyIncremental({
|
|
87
|
+
projectFiles: files,
|
|
88
|
+
cursors: tmpCursors,
|
|
89
|
+
queuePath: tmpQueue,
|
|
90
|
+
env,
|
|
91
|
+
});
|
|
92
|
+
let rebuiltRaw = "";
|
|
93
|
+
try { rebuiltRaw = await fs.readFile(tmpQueue, "utf8"); } catch (e) {
|
|
94
|
+
if (e?.code !== "ENOENT") throw e;
|
|
95
|
+
}
|
|
96
|
+
rebuiltRows = rebuiltRaw.split("\n").filter((line) => line.trim());
|
|
97
|
+
} catch (e) {
|
|
98
|
+
console.error("[sync] WorkBuddy context-usage repair: rebuild failed, leaving history untouched:", e?.message || e);
|
|
99
|
+
return false;
|
|
100
|
+
} finally {
|
|
101
|
+
await fs.rm(tmpQueue, { force: true }).catch(() => {});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const rebuiltKeys = new Set();
|
|
105
|
+
const validRebuiltRows = [];
|
|
106
|
+
for (const line of rebuiltRows) {
|
|
107
|
+
let row;
|
|
108
|
+
try { row = JSON.parse(line); } catch (_e) { continue; }
|
|
109
|
+
if (!row || row.source !== "workbuddy" || typeof row.model !== "string" || typeof row.hour_start !== "string") continue;
|
|
110
|
+
rebuiltKeys.add(bucketKey("workbuddy", row.model || "unknown", row.hour_start));
|
|
111
|
+
validRebuiltRows.push(line);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const staleRetractions = [];
|
|
115
|
+
for (const key of liveKeys) {
|
|
116
|
+
if (rebuiltKeys.has(key)) continue;
|
|
117
|
+
const [, model, ...hourParts] = key.split("|");
|
|
118
|
+
staleRetractions.push(JSON.stringify({
|
|
119
|
+
source: "workbuddy",
|
|
120
|
+
model: model || "unknown",
|
|
121
|
+
hour_start: hourParts.join("|"),
|
|
122
|
+
input_tokens: 0,
|
|
123
|
+
cached_input_tokens: 0,
|
|
124
|
+
cache_creation_input_tokens: 0,
|
|
125
|
+
output_tokens: 0,
|
|
126
|
+
reasoning_output_tokens: 0,
|
|
127
|
+
total_tokens: 0,
|
|
128
|
+
billable_total_tokens: 0,
|
|
129
|
+
conversation_count: 0,
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const nextQueueLines = keptQueueLines.concat(staleRetractions, validRebuiltRows);
|
|
134
|
+
await ensureDir(path.dirname(queuePath));
|
|
135
|
+
const queueTmp = `${queuePath}.tmp.${process.pid}.${Date.now()}`;
|
|
136
|
+
await fs.writeFile(queueTmp, nextQueueLines.length ? `${nextQueueLines.join("\n")}\n` : "", "utf8");
|
|
137
|
+
await fs.rename(queueTmp, queuePath);
|
|
138
|
+
|
|
139
|
+
const nextHourly = (cursors.hourly ||= { buckets: {}, groupQueued: {} });
|
|
140
|
+
nextHourly.buckets ||= {};
|
|
141
|
+
nextHourly.groupQueued ||= {};
|
|
142
|
+
for (const key of Object.keys(nextHourly.buckets)) {
|
|
143
|
+
if (key.startsWith("workbuddy|")) delete nextHourly.buckets[key];
|
|
144
|
+
}
|
|
145
|
+
for (const key of Object.keys(nextHourly.groupQueued)) {
|
|
146
|
+
if (key.startsWith("workbuddy|")) delete nextHourly.groupQueued[key];
|
|
147
|
+
}
|
|
148
|
+
for (const [key, bucket] of Object.entries(tmpCursors.hourly.buckets || {})) {
|
|
149
|
+
if (key.startsWith("workbuddy|")) nextHourly.buckets[key] = bucket;
|
|
150
|
+
}
|
|
151
|
+
for (const [key, value] of Object.entries(tmpCursors.hourly.groupQueued || {})) {
|
|
152
|
+
if (key.startsWith("workbuddy|")) nextHourly.groupQueued[key] = value;
|
|
153
|
+
}
|
|
154
|
+
// This migration rebuilds totals from a temp queue using empty cursors, so
|
|
155
|
+
// `tmpCursors.workbuddy` carries no project state. Replacing the whole object
|
|
156
|
+
// would drop the project cursor while leaving the published project buckets
|
|
157
|
+
// (cursors.projectHourly and the real project queue) untouched, and the next
|
|
158
|
+
// parse would re-attribute every historical session on top of them. The
|
|
159
|
+
// migration only changes totals, so keep the project cursor as it is.
|
|
160
|
+
const previousWorkbuddyState =
|
|
161
|
+
cursors.workbuddy && typeof cursors.workbuddy === "object" ? cursors.workbuddy : {};
|
|
162
|
+
cursors.workbuddy = {
|
|
163
|
+
...(tmpCursors.workbuddy || {}),
|
|
164
|
+
...(previousWorkbuddyState.projectFileOffsets
|
|
165
|
+
? { projectFileOffsets: previousWorkbuddyState.projectFileOffsets }
|
|
166
|
+
: {}),
|
|
167
|
+
...(Array.isArray(previousWorkbuddyState.projectSeenIds)
|
|
168
|
+
? { projectSeenIds: previousWorkbuddyState.projectSeenIds }
|
|
169
|
+
: {}),
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
let uploadState = {};
|
|
173
|
+
try { uploadState = JSON.parse(await fs.readFile(queueStatePath, "utf8")); } catch (_e) {}
|
|
174
|
+
uploadState.offset = 0;
|
|
175
|
+
uploadState.updatedAt = new Date().toISOString();
|
|
176
|
+
uploadState.note = "reset_after_workbuddy_context_usage_repair_2026_08";
|
|
177
|
+
await ensureDir(path.dirname(queueStatePath));
|
|
178
|
+
await fs.writeFile(queueStatePath, JSON.stringify(uploadState, null, 2) + "\n", "utf8");
|
|
179
|
+
|
|
180
|
+
migrations[WORKBUDDY_CONTEXT_USAGE_REPAIR_KEY] = {
|
|
181
|
+
status: "applied",
|
|
182
|
+
appliedAt: new Date().toISOString(),
|
|
183
|
+
previousBuckets: liveKeys.size,
|
|
184
|
+
rebuiltBuckets: rebuiltKeys.size,
|
|
185
|
+
retractedBuckets: staleRetractions.length,
|
|
186
|
+
};
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// One-time repair (#187): rebuild codex hourly buckets that the inode-keyed
|
|
191
|
+
// re-scan double-counted before the codexHashes event-dedup landed, and push
|
|
192
|
+
// the corrected values to the cloud. Runs BEFORE the codex parse in the same
|
|
193
|
+
// sync: it clears codex hourly state + codexHashes so the parse rebuilds clean,
|
|
194
|
+
// atomically strips the inflated codex rows from queue.jsonl, and resets the
|
|
195
|
+
// upload offset so the re-uploaded clean rows overwrite the cloud (with no
|
|
196
|
+
// stale-high codex rows left in the queue, the ingest's within-batch MAX keeps
|
|
197
|
+
// nothing larger, so its overwrite-upsert replaces the inflated cloud rows).
|
|
198
|
+
//
|
|
199
|
+
// GUARDED against the v6 ground-truth-repair data-loss incident: a clear+reparse
|
|
200
|
+
// rebuilds codex buckets ONLY from the codex files this sync re-parses (now both
|
|
201
|
+
// sessions/ AND archived_sessions/), so if any codex file that previously
|
|
202
|
+
// contributed is gone from disk (genuinely deleted — Codex-Manager log rotation
|
|
203
|
+
// or user cleanup) the migration is skipped entirely — the forward dedup fix
|
|
204
|
+
// still prevents new double-counting; only this historical correction is deferred.
|
|
205
|
+
|
|
206
|
+
module.exports = {
|
|
207
|
+
WORKBUDDY_CONTEXT_USAGE_REPAIR_KEY,
|
|
208
|
+
repairWorkbuddyContextUsage,
|
|
209
|
+
};
|