Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: skip Grok's flat prompt_history.jsonl sibling when scanning session dirs

[._.]/ Adam Eivy5c06357f3803
diff --git a/server/services/usageReconciler.js b/server/services/usageReconciler.js
index 6a8bb0e34f..57d0dd6b39 100644
--- a/server/services/usageReconciler.js
+++ b/server/services/usageReconciler.js
@@ -237,6 +237,18 @@ const asRecordList = (records) => (Array.isArray(records) ? records : [records])
/** List a directory, returning [] when it doesn't exist or can't be read. */
const listDir = async (dir) => readdir(dir).catch(() => []);
+/**
+ * Subdirectory names only, returning [] when `dir` doesn't exist or can't be
+ * read. Grok drops a flat `prompt_history.jsonl` file directly inside a
+ * cwd-keyed folder, as a SIBLING of the per-session-id directories — a plain
+ * `listDir` there hands that filename to callers expecting a session id, which
+ * then fails `ENOTDIR` trying to read `<file>/summary.json` (#6218).
+ */
+const listSubdirs = async (dir) => {
+ const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
+ return entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name);
+};
+
/**
* Every date directory (`YYYY/MM/DD`) a run could have written a Codex rollout
* into. A run spanning midnight (or a UTC/local boundary) touches two days, so
@@ -399,7 +411,7 @@ export async function readMeasuredUsage({ workspacePath, startTime, endTime, fam
for (const dirName of await listDir(sessionsRoot)) {
if (!cwdMatches(decodeGrokSessionDir(dirName), workspacePath)) continue;
const cwdDir = join(sessionsRoot, dirName);
- for (const sessionId of await listDir(cwdDir)) {
+ for (const sessionId of await listSubdirs(cwdDir)) {
const sessionDir = join(cwdDir, sessionId);
const updatesPath = join(sessionDir, 'updates.jsonl');
const updatesText = await tryReadFile(updatesPath);
diff --git a/server/services/usageReconciler.test.js b/server/services/usageReconciler.test.js
index 9b9c1b487c..8a5a3c337e 100644
--- a/server/services/usageReconciler.test.js
+++ b/server/services/usageReconciler.test.js
@@ -1071,6 +1071,21 @@ describe('grok sessions', () => {
expect(record.model).toBe(GROK_MODEL);
});
+ it('ignores a flat prompt_history.jsonl file sitting beside the session-id directories', async () => {
+ // Grok drops this file directly in the cwd-keyed folder, as a SIBLING of
+ // the per-session-id directories it also writes there (#6218) — treating
+ // every entry as a session id previously crashed the ENTRIES loop with
+ // ENOTDIR trying to read `<file>/summary.json`.
+ await writeGrokSession({ updates: [grokTurnLine({ ms: IN_WINDOW_MS })] });
+ await writeFile(
+ join(home, '.grok', 'sessions', encodeURIComponent(WORKSPACE), 'prompt_history.jsonl'),
+ 'not a session directory'
+ );
+ const [record] = await reconcileRunUsage(grokRun, { tokensIn: 1, tokensOut: 1 }, { home });
+ expect(record.source).toBe('measured');
+ expect(record.tokensOut).toBe(1_800);
+ });
+
it('estimates from chat history when the run died before any turn completed', async () => {
await writeGrokSession({
summary: {