Repository navigation
fix(skills): copy cached skill summary fields so they stop pinning whole SKILL.md files - #52336
Conversation
[Low impact] The PR appears safe to merge; the returned metadata stays unchanged while copies stop it from retaining larger Markdown strings. SummaryThe PR copies skill names and descriptions before returning them, so cached summaries can release the larger Markdown text.
Reviews (2) · Last reviewed commit: "fix(skills): copy cached skill summary f..." · Reviewed by Greptile |
|
No actionable comments were generated in the recent review. Walkthrough
Severity of issue fixed: Medium Merge Risk:
|
…ole SKILL.md files summarizeSkillMarkdown and declaredSkillMetadata returned V8 sliced strings of the decoded SKILL.md, so every cached name/description kept the whole file text alive. Copy both fields with the shared ownRetainedString helper so the file text can be freed. Fixes stablyai#52334 Co-authored-by: xwc2020-2 <[email protected]>
9240761 to
4a61ffe
Compare
ELI5
When Orca reads a skill file, it keeps only the skill's short name and description. Those short pieces of text were still tied to the whole file behind the scenes, so the entire file stayed in memory as long as the summary did. This change keeps separate copies of the name and description, so the file can be freed. Nothing you see changes. Orca just uses less memory when you have many large skills.
What Changed
summarizeSkillMarkdownanddeclaredSkillMetadatainsrc/shared/skills/discovery/skill-metadata.tsreturnednameanddescriptionbuilt withtrim(),slice, regex groups andsplit. V8 stores these as sliced strings that point back to the full decoded SKILL.md. Each cached summary therefore kept the whole file text alive (up to the 256 KiB native read cap, or the whole file in the Claude command catalog).ownRetainedString(src/shared/text/own-retained-string.ts). The helper makes a flat copy with a UTF-16LE round trip that is checked against a test string first, and it skips strings too short to be slices. The returned text is identical.readSkillSummaryand the scan cache), WSL observation, the Claude command-folder catalog, package and bundle work, and bot skill scans (admittedSkillin bot-skill-projects, bot-own-skills and bot-share-skills).Why
A read-size limit does not stop a sliced string from holding on to the whole decoded document. The original PR added a local Buffer round trip in
skill-discovery-summary.ts, which fixed the native discovery path only. This version reuses the existing shared helper at the producer, so there is no duplicate copier and every consumer is covered.Linked Issue
Fixes #52334
Visual Proof
N/A. There is no UI or interaction change. Only the in-memory representation of identical metadata changes. Heap measurements are under Testing.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
Reproduced on macOS arm64 with
node --expose-gc. 200 SKILL.md files of about 200 KB each, results kept, retained heap measured after GC, main vs this branch:summarizeSkillMarkdownwith plain, quoted, folded>-, heading/paragraph fallback and list fallback frontmatter: 38.6 MiB vs 0.0 to 0.25 MiB. CRLF+BOM was 0.4 MiB on both, because that shape never pinned the file on main.declaredSkillMetadatafollowed byoneLine(same asadmittedSkill): 38.6 MiB vs 0.2 MiB.readSkillSummarycalled 320 times on a 250 KB SKILL.md: 51.5 MiB vs 0.3 MiB.Output text matches main exactly, including lone surrogates, CJK, emoji, quoted values and BOM stripping.
No regression test is committed. A test that fails on revert would need
--expose-gcheap measurement, which is not reliable in vitest. A text-fidelity test was tried and removed because it also passes on main.vitestonsrc/main/skills/discovery,src/shared/skills,src/shared/textandsrc/main/skills/command-catalog: 36 of 38 pass. The 2 failures are inbot-linked-skills-discovery.test.ts, which reads real home skills, and they fail the same way on main with this file reverted.oxlint,
oxfmt --check, the anti-slop and native-plugin oxlint configs, ast-grep, and the changed-code quality gate are clean on the touched file.Platforms: macOS only. The change is platform-neutral string handling in shared code.
Performance
Small, bounded per-call cost on a cold path. Median per call, main vs branch (
summarizeSkillMarkdown):declaredSkillMetadatais similar: typical 0.37 vs 0.65 us, worst case under 0.4 ms.Both functions run once per SKILL.md per scan, after a file read, in main-process async skill scans (cached, 10 s TTL). They are not on a renderer, terminal-stream, polling or startup-critical path. There is no new I/O, subprocess or polling, the work does not grow with the number of open tabs or worktrees, and nothing comes near 16 ms. In exchange, retained memory drops by about 38 to 51 MiB per 200 to 320 large skills.
Risk
Level: Low. The main risk is the copy changing text. The helper checks a round-trip string with lone surrogates before using Buffer, and falls back to a code-unit copier where Buffer is missing. Alias detection and digests run on the same inputs as before. A plain revert backs this out, with no saved data or protocol changes.
Known follow-ups, out of scope and unchanged from main: WSL observation still keeps slices of WSL stdout through the
skillFilePath/canonicalSkillFilePathfields (Windows/WSL only).Review
Original fix by @xwc2020-2 in #52336. This version moves the copy into the shared summarizer and reuses the existing
ownRetainedStringhelper. Authorship credit is kept via aCo-authored-bytrailer.Agent skill upstream boundary
Notes
Security: no new endpoints, execution or access changes. Cross-platform: pure string handling in shared code. Remote SSH and mobile: the payload shape is unchanged, so older paired clients are not affected. Backwards compatibility: no change to saved data.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)