Skip to content

Prune backups by age, and keep an extension's own field spelling - #2

Closed
CaffreySun wants to merge 3 commits into
dncore:mainfrom
CaffreySun:fix-backup-pruning-and-capture-keys
Closed

CaffreySun wants to merge 3 commits into
dncore:mainfrom
CaffreySun:fix-backup-pruning-and-capture-keys

Conversation

@CaffreySun

Copy link
Copy Markdown
Contributor

The two fixes from #1 that do not depend on the Oh My Pi feature — split out so they can land on main while the feature PR is rebased, as suggested there.

Both are live on main today, now that several integrations share the runtime's directories.

1. Backups were pruned by file name

ConfigTransaction.pruneBackups() sorted by lastPathComponent, and a backup is named <file>.<timestamp>.<fingerprint>. That is chronological only while every backup in the directory comes from the same file. With two integrations writing there, one integration's backups sort ahead of the other's however new they are — so the backup taken during a call was the first one that call pruned, and the path the caller had just been handed was gone.

Age now comes from .contentModificationDate, with the name breaking ties inside a clock tick. Regression test: take a backup under a second file name, assert the path the call reported still exists. Against the old ordering it fails with exactly that path already removed.

2. The capture allowlist knew only one agent's spelling

EventCapture.capturableKeys had session_id / tool_name, and nothing else. The in-process extensions send the camelCase names their own API uses — Pi's extension already sends toolName beside a snake-case session id — so those fields were dropped on the way to disk.

Dropping the tool name costs a label; dropping the session id costs the row: an event spooled while the app was down replays with no session, falls back to the shim's parent pid, and draws a second row for a session already on screen under its real one. Both spellings are kept now, and the list stays hand-written — deriving it from the profiles would put UserPromptSubmit's prompt on disk.

Regression test: spool a camelCase payload, assert the session id and tool name survive while tool arguments and prompt text do not. Without the fix the record reads:

{"cwd":"/tmp/project","_omitted_keys":["args","sessionId","text","toolName"]}

Verification

  • swift build --build-tests, swift test --parallel on current main → 511 tests, 81 suites, 0 failures.
  • Both new tests verified as negative controls: with the old ordering / without the added keys they fail, and pass with the fix.
  • git diff touches four files: two sources, their two test files. No version, workflow or release metadata.

The backup directory is shared by every integration, and a backup's name
starts with the name of the file it came from:

    settings.json.1789440066.d62d57de
    agentpet.ts.1789446695.aa1d698b

Sorting those names is chronological only while every backup in the
directory comes from the same file. With two integrations writing here —
which is the case on main today — one integration's backups sort ahead of
the other's however new they are, so the backup taken during a call was the
first one that call pruned, and the path the caller had just been handed was
already gone.

Age now comes from the filesystem (.contentModificationDate), with the name
settling ties inside a single clock tick. The new test takes a backup under a
second name and asserts the reported path still exists afterwards; against
the old ordering it fails with that path already removed.
The capture and spool allowlist knew Claude Code's `session_id` and
`tool_name` only. The in-process extensions send the camelCase names their own
API uses — Pi's extension already sends `toolName` beside a snake-case session
id — so those fields were dropped on the way to disk.

Dropping the tool name costs a label. Dropping the session id costs the row:
an event spooled while the app was down replays with no session, falls back to
the shim's parent pid, and draws a second row for a session that is already on
screen under its real one.

Both spellings are kept. The list stays hand-written rather than derived from
the profiles: a profile's `summaryField` is the user's own prompt on
`UserPromptSubmit`, and an allowlist that followed profiles would follow
prompts onto disk.

The test spools a camelCase payload and asserts the session id and the tool
name survive while tool arguments and prompt text do not; without the fix the
record reads {"cwd":…,"_omitted_keys":["args","sessionId","text","toolName"]}.
Its comment credited the loss to "an in-process extension", which is not
where the camelCase spelling comes from: Pi's extension sends `session_id`
and only its tool name is camelCase, so nothing of ours has ever lost a
session this way. What the test covers is a payload that spells the session
id only in camelCase; the redaction path does not care which agent sent it,
so the envelope's agent is `generic-cli` rather than a named one whose wire
someone might go and check.

Found while reviewing the rebase of the Oh My Pi branch, which carries the
same test and got the same correction.

511 tests.
@CaffreySun

Copy link
Copy Markdown
Contributor Author

One wording fix on top, from the review of the Oh My Pi branch (which carries the same test):

1a0bad5 — the capture test's comment credited the lost session to "an in-process extension", which is not where the camelCase spelling comes from: Pi's extension sends session_id, and only its tool name is camelCase, so nothing of ours has ever lost a session that way. The test covers a payload that spells the session id only in camelCase; the redaction path does not care which agent sent it, so the envelope's agent is now generic-cli instead of a named one whose wire someone might go and check. 511 tests still green.

Nothing else here changed.

@CaffreySun

Copy link
Copy Markdown
Contributor Author

Superseded by main: f865c95 (pruning by age) and 55194e9 (the camelCase capture keys) landed the two fixes with their regression tests, and #1's shim work followed as 1cb66a6. Nothing here is left over, so I'm closing it — thanks for porting them rather than making them wait on the rebase.

(For the record, the third commit on this branch, 1a0bad5, was a wording fix to the capture test's comment from the review of #1. Main's version of that test came from 55194e9, so it does not apply here.)

@CaffreySun CaffreySun closed this Sep 16, 2026
dncore added a commit that referenced this pull request Sep 16, 2026
The capture comment credited the camelCase session id to in-process
extensions, and that is wrong twice over: Grok's captured wire carries
both spellings of every core field, and the extensions we ship report
`session_id` with a camelCase `toolName`. Antigravity's wire is camelCase
throughout, which the old wording ("hooks use session_id") also missed.

The spool test was named after a wire that does not spell it that way;
the redaction path does not care which agent sent the payload, so the
envelope is agent-neutral now.

Wording fix ported from #2 (1a0bad5; author CaffreySun), whose branch
was closed as superseded before this part of it landed.

Co-Authored-By: CaffreySun <sunhongwei0913@163.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
@CaffreySun
CaffreySun deleted the fix-backup-pruning-and-capture-keys branch September 16, 2026 02:49
dncore added a commit that referenced this pull request Sep 21, 2026
The capture comment credited the camelCase session id to in-process
extensions, and that is wrong twice over: Grok's captured wire carries
both spellings of every core field, and the extensions we ship report
`session_id` with a camelCase `toolName`. Antigravity's wire is camelCase
throughout, which the old wording ("hooks use session_id") also missed.

The spool test was named after a wire that does not spell it that way;
the redaction path does not care which agent sent the payload, so the
envelope is agent-neutral now.

Wording fix ported from #2 (1a0bad5; author CaffreySun), whose branch
was closed as superseded before this part of it landed.

Co-Authored-By: CaffreySun <sunhongwei0913@163.com>
Co-Authored-By: Claude Code <noreply@anthropic.com>
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