Skip to content

perf(agent): skip session JSONL sanitize when file is unchanged - #76791

Merged
trunk-io[bot] merged 6 commits into
masterfrom
posthog-code/skip-clean-jsonl-sanitize
Aug 21, 2026
Merged

perf(agent): skip session JSONL sanitize when file is unchanged#76791
trunk-io[bot] merged 6 commits into
masterfrom
posthog-code/skip-clean-jsonl-sanitize

Conversation

@arnohillen

Copy link
Copy Markdown
Contributor

Ports PostHog/code#3977 onto products/desktop/ after the desktop import (the code repo's main is frozen). Original commits and authorship are preserved via git am.

Problem

Part 2 of the task re-open latency work (see #76788). Every agent reconnect runs sanitizeSessionJsonl over the native Claude transcript (~/.claude/projects/.../<sessionId>.jsonl): a full read plus a JSON.parse per line. These files grow to tens of MB on long sessions, so every re-open of a cold task pays seconds of parse work before the session can resume, even when the file was already verified clean moments earlier.

Changes

  • Remember the file stat (mtime + size) of the last clean or healed pass in a small bounded module cache (lru-cache, already a workspace dependency), and return early when the current stat matches.
  • The Claude SDK only appends to the JSONL, which changes both size and mtime, so any real change forces a full re-parse. A stale memo can only cause extra work, never a skipped heal. The stat-equality assumption is the same one the existing concurrent-writer guard already relies on.

Compared to the source PR, the only monorepo-specific change is the lockfile: instead of carrying the source pnpm-lock.yaml hunk blindly, the products/desktop/pnpm-lock.yaml importer entry was regenerated for the new packages/agent dependency (same resolution, lru-cache@11.2.5).

How did you test this code?

  • Test proving the skip through the public interface: a dirty file whose stat is restored to the memoized clean-pass stat must not be healed (would fail if the memo check were removed), and a file that grows past a clean pass must be re-sanitized.
  • Verified in the monorepo from products/desktop/: pnpm install --frozen-lockfile, pnpm typecheck (24/24 packages), and the full @posthog/agent vitest suite (100 files, 1771 tests passing).

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Docs update

Not applicable (desktop app internals), skip-inkeep-docs label applied.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

This PR is a port of PostHog/code#3977, recreated here by PostHog Code following the /porting-code-prs skill: patch series applied with git am --directory=products/desktop/ (original authorship and commit messages preserved), source lockfile hunk excluded and re-derived against the monorepo lockfile instead. Part 2 of 3 ported PRs on task re-open latency.


Created with PostHog Code

Every reconnect re-runs sanitizeSessionJsonl, which reads and JSON-parses
every line of the native Claude transcript. For long sessions that file
reaches tens of MB, adding seconds to each resume.

Remember the file stat of the last clean pass and skip the read when it
matches. The SDK only appends, which changes size and mtime, so any real
change forces a re-parse; staleness only ever causes extra work, not a
skipped heal.

Generated-By: PostHog Code
Task-Id: 5d24ea17-aec0-4334-884e-c2639867e260
Same swap as the conversation caches in #3976: lru-cache v11 is already
a workspace dependency, so the hand-rolled capped Map goes away.

Generated-By: PostHog Code
Task-Id: 5d24ea17-aec0-4334-884e-c2639867e260
The healed path recorded the stat of the renamed file, taken after the
rename, so bytes appended by a concurrent writer in that window would
have been certified clean and skipped by every later pass. Record the
tmp file's stat instead: rename preserves it, and any post-rename append
already mismatches it, forcing a re-parse.

Generated-By: PostHog Code
Task-Id: 47cdc119-1c24-42ec-ac36-74c43cdd7f4e
Not run in CI (test globs only match *.test.*). Numbers land in the PR
description; rerun with pnpm vitest bench in packages/agent.

Generated-By: PostHog Code
Task-Id: 47cdc119-1c24-42ec-ac36-74c43cdd7f4e
Regenerated for the new packages/agent dependency; same resolution the
source PR locked (11.2.5).

Generated-By: PostHog Code
Task-Id: 96e133e1-f570-432f-b8ff-bce4202f5b7f
@arnohillen arnohillen added the skip-inkeep-docs Use this label to skip an Inkeep docs PR in posthog.com label Aug 3, 2026
@arnohillen arnohillen self-assigned this Aug 3, 2026
@trunk-io

trunk-io Bot commented Aug 3, 2026

Copy link
Copy Markdown

😎 Merged successfully - details.

@greptile-apps

greptile-apps Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "chore(agent): add lru-cache to the agent..." | Re-trigger Greptile

@trunk-io

trunk-io Bot commented Aug 3, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@scheduled-actions-posthog

Copy link
Copy Markdown
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, please remove the stale label – otherwise this will be closed in another week. If you want to permanently keep it open, use the waiting label.

@github-actions

Copy link
Copy Markdown
Contributor

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 3af0ff6.

@hosthog

hosthog Bot commented Aug 16, 2026

Copy link
Copy Markdown

HostHog preview — posthog-desktop-web

The previews for this PR have been torn down and no longer serve.

@arnohillen

Copy link
Copy Markdown
Contributor Author

/trunk merge

@trunk-io
trunk-io Bot merged commit 16624ed into master Aug 21, 2026
216 checks passed
@trunk-io
trunk-io Bot deleted the posthog-code/skip-clean-jsonl-sanitize branch August 21, 2026 15:20
@deployment-status-posthog

deployment-status-posthog Bot commented Aug 21, 2026

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-08-21 15:50 UTC Run
prod-us ✅ Deployed 2026-08-21 16:14 UTC Run
prod-eu ✅ Deployed 2026-08-21 16:10 UTC Run

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

Labels

skip-inkeep-docs Use this label to skip an Inkeep docs PR in posthog.com

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants