Skip to content

🤖 fix: compact devtools.jsonl to the retained DevTools window - #6890

Open
ibetitsmike wants to merge 3 commits into
mainfrom
mike/devtools-jsonl-compaction
Open

ibetitsmike wants to merge 3 commits into
mainfrom
mike/devtools-jsonl-compaction

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

With debug logging on, devtools.jsonl grows with every run and step ever logged, while the DevTools panel replays only a bounded recent window (64 MiB per workspace by default). This PR rewrites the file down to that window once more than twice the budget of it is lines that replay no longer keeps, so the file stays near the window instead of growing without bound.

Background

#4260 bounded what replay keeps in memory and left the file untouched ("compaction is a follow-up"). Debug logs had reached 25 GiB on a dev machine.

Implementation

  • DevToolsService tracks the file length (fileBytes, in the same units as the retention bytes) during replay and after each append.
  • The rewrite runs inside the existing per-workspace write queue. It replays the file itself, not this process's memory, so rows that other backends appended survive and entries still queued behind it are written once. It writes the retained runs and steps (updates merged in) in log order, each run just before its first step, to devtools.jsonl.compacting-<pid>, looping until every byte of each line is written.
  • The replay and that write run outside the session lock. Under the lock (through commitToSessionFileUnlessRemoved, so a removed workspace's directory is not recreated), the rewrite replaces the log only if the log is unchanged since the replay started, and rewrites left by crashed or overtaken backends are removed.
  • If the log changed or the rewrite fails, the full file stays, a warning is logged, and the next attempt waits for another 2x budget of growth.
  • A file that is already oversized is rewritten the first time it loads.
  • readDevtoolsLog (debug CLI replay-verify and cache-audit) now also reads usage, error, and prompt-cache facts from step rows, because compaction merges step-update rows into them.

Validation

  • 11 compaction tests in devToolsService.test.ts and a step-row layout case in replayVerify.test.ts. Each fails with its trigger or guard removed, or on the commit before its fix.
  • Local bench, default budget, 600 steps of 1 MiB each: the file peaked at 190 MiB (600 MiB before) and ended at 88 MiB. The 4 appends that compacted took 0.4 to 0.6 s each (mean 5.5 to 6.7 ms per append), and replaying the result took under 0.3 s.
  • In the same bench, a second process taking the session lock in a loop waited at most 0.15 to 0.21 s (0.13 s in a run too short to compact), against 0.76 s when the rewrite held the lock.

Risks

  • Lines of evicted runs, including runs still streaming, are removed from disk. The panel already does not show them.
  • If another backend keeps appending to the same workspace's log during every rewrite, each attempt is dropped and the file can grow past 3x the budget.
  • The temporary file is created in the session directory outside the lock. A workspace removal at that moment can fail to delete the directory, which removal already handles as an orphan.
  • If xum exits during the rewrite, the original file stays intact, and its temporary file stays until the next rewrite of that log removes it.
  • Replay still runs on the main thread. With the file bounded near the budget it stays short, so this PR does not change it.

Review record

  • Codex round 1 (cb278af): 3 findings. Each reproduced as a failing test and was fixed in ba76b67: rows from another backend erased (P1), steps of overlapping runs reordered (P2), a step queued behind the rewrite written twice (P2).
  • Codex round 2 (ba76b67): 2 findings. Each reproduced as a failing test and was fixed in 9a2a27a: the rewrite held the session lock for its whole run, so another backend's append could time out (P2), and a short write could leave a broken row in the rewrite (P2).

devtools.jsonl was append-only: memory kept only the newest 100 runs or
64 MiB per workspace, but the file kept every run ever logged, so files
reached several GB and each first load after a restart replayed all of it.

Once more than twice the byte budget of the file is lines that replay no
longer keeps (evicted runs, superseded step updates), DevToolsService now
rewrites it as the retained runs and their merged steps (temp file plus
rename, inside the per-workspace write queue). A load that finds an
oversized file compacts it once. A failed rewrite keeps the full file and
waits for another two budgets of growth before it tries again; a partial
replay never compacts.

The debug CLI's log reader now also takes usage, errors and wire facts
from step rows, where a compacted log keeps them.

With the default budget and 600 one-MiB steps, the file peaked at 190 MiB
instead of 600 MiB; the append that compacts took 252 ms (mean 4 ms).
@ibetitsmike

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T20:35:41.941103Z 9a2a27a Manual request
🔒 Security Review ✅ Completed 2026-10-16T20:35:35.318611Z 9a2a27a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: cb278af372

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb278af372

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/node/services/devToolsService.ts
Comment thread src/node/services/devToolsService.ts Outdated
Comment thread src/node/services/devToolsService.ts Outdated
Compaction rewrote the log from this process's memory. That erased rows
another backend appended since this one loaded, wrote entries whose
appends were still queued twice, and grouped steps by run, which changed
the log order the replay audit reads.

It now replays the file inside the session lock and writes what that
replay keeps, with steps in log order and each run before its first step.
@ibetitsmike

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: ba76b67dc9

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ba76b67dc9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/node/services/devToolsService.ts Outdated
Comment thread src/node/services/devToolsService.ts Outdated
…writes

Compaction replayed and rewrote the whole log while it held the session
lock. That lock also guards the workspace's other session files, and
other processes give up on it after 2 s, so a slow rewrite could make
another backend drop the row it was appending.

The replay and the rewrite now run outside the lock. Under it, the
rewrite replaces the log only if the log is unchanged since the replay
started, and rewrites that a crashed or overtaken backend left are
removed. The temporary file name now includes the process id, because
two backends can rewrite the same log at once.

Each rewritten line is written in a loop until all of its bytes are out,
because a nearly full disk can accept part of a write without an error.
@ibetitsmike

Copy link
Copy Markdown
Contributor Author

@codex review

Please take another look.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 9a2a27a702

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 9a2a27a702

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant