Skip to content

refactor: collapse duplicated prune, inject, state hydration and nudge helpers - #649

Open
amnesiaof wants to merge 5 commits into
Tarquinen:masterfrom
amnesiaof:fix/sdk-media-asset-and-dedup
Open

amnesiaof wants to merge 5 commits into
Tarquinen:masterfrom
amnesiaof:fix/sdk-media-asset-and-dedup

Conversation

@amnesiaof

@amnesiaof amnesiaof commented Oct 2, 2026 •

Copy link
Copy Markdown

Result: 16 files, +442 -1154. Pure internal consolidation, no dependency changes and no dependency-related lockfile churn: package.json and package-lock.json are byte-identical to master here.
Everything is derived from a ponytail-audit pass over lib/. Each commit reverts on its own.

Duplication removed

validateConfigTypes was a 440 line typeof ladder next to a 44 entry list of config keys kept in sync by hand with the interfaces, the defaults and the JSON schema. It is a rule table now, and the set of known keys is derived from defaultConfig.
Five config merge functions plus deepCloneConfig became one recursive merge over structuredClone.
pruneToolOutputs, pruneToolInputs and pruneToolErrors were three loops differing by a status check, a tool name check and a replacement string. They are one function now.
loadPruneMessagesState revalidated every field of a file the same module had just written. Shared guards and one block deserializer replace the per field checks.
PersistedPruneMessagesState was declared verbatim in two files.
applyAnchoredNudges made six near identical calls, three per compression mode. It loops over a table now.
buildSessionState duplicated the hydration in ensureSessionInitialized, and the two had already drifted.
extractParameterKey had identical glob and grep branches, and identical websearch and codesearch branches.
Three prompt files held one template string each. PROMPT_KEYS restated seven keys PROMPT_DEFINITIONS already carries. Both are gone now that the templates and the key list live with the definitions.

Behavior changes

Unknown nested keys in dcp.json now reach the merged config instead of being dropped. The unknown key warning still fires.
TUI session state also picks up currentTurn and live turn nudge anchors, which the duplicated path never had.
resetSessionState now derives from a fresh state and carries over idFormat, compressionTiming and the two cached maps, so external references to those maps stay valid.

Also removed

.npmignore does nothing next to files in package.json. When both are present files wins, and files is the one that runs.
pruneFullTool had one call site, commented out.
report() in lib/v2/index.ts was a stub nothing called. reportToLog does the same write directly.

Verification

  • bun test: 122 pass / 2 fail, identical to master, and both failures pre-date this branch (V2 range compression resolves block IDs..., relay preserves text/binary frames...)
  • tsc --noEmit: clean
  • tsc --emitDeclarationOnly: clean
  • tsup build: success
  • scripts/verify-package.mjs: passed, 230 tarball entries
    prettier --check is clean on every changed file when line endings are normalized. Note for anyone re-running it on Windows: core.autocrlf leaves the working tree on CRLF while the committed blobs are LF, so prettier reports style issues in files this branch never touched. tsconfig.json and tui.tsx are byte-identical to master and still get flagged.

Not in this PR

The @opencode/plugin bump and the OpenTUI / solid-js peer move were split out into #651, together with the Asset fix they force. Keeping them separate matters here because #627 edits those same dependency lines in the opposite direction.

So the sequence is #648 (anywhere), then #651, then this one.

validateConfigTypes was about 440 lines of typeof checks, one branch per
config key, beside a hand maintained 44 entry list of dotted key strings
that had to stay in sync with the interfaces, the defaults and the JSON
schema by hand.

It is a declarative rule table now, and the set of known keys is derived
from defaultConfig rather than restated. The three keys that are valid
but absent from the defaults are listed separately.

Five merge functions plus deepCloneConfig became one recursive merge over
structuredClone. getConfigPaths lost three copies of the same
existsSync ternary.
loadPruneMessagesState revalidated every field of a file the same module
had just written. Shared guards and one block deserializer replace the
per field checks.

The explicit restore of activeBlockIds and activeByAnchorMessageId is
gone. The final loop already repopulates both from block.active, which is
where serialization reads them from, so the restore was redundant.

loadSessionState validated and deduped three anchor sets with three near
identical blocks. One loop over the key names now, with the same log
text.

PersistedPruneMessagesState was declared verbatim in two files. It lives
in types.ts now, which both already import from, avoiding a cycle since
persistence.ts imports a function from utils.ts.

buildSessionState duplicated the hydration in ensureSessionInitialized.
The two had already drifted: the TUI copy never picked up currentTurn or
live turn nudge anchors. It calls the shared path now.

resetSessionState restated about 25 field assignments from
createSessionState. It derives from a fresh state and carries over
idFormat, compressionTiming and the two cached maps so external
references to them stay valid.
pruneToolOutputs, pruneToolInputs and pruneToolErrors were three loops
differing by a status check, a tool name check and a replacement string.
One function takes them as arguments.

pruneFullTool is gone. Its only call site was commented out.

applyAnchoredNudges made six near identical calls, three per compression
mode, differing only in the anchor set and the prompt text. It loops
over a table now.

injectMessageIds and injectAnchoredNudge shared a seven line tail that
placed a synthetic text part before the first tool part. That is
prependSyntheticPart now. The two functions stay separate: the user path
uses all text parts and the assistant path uses tool parts, so folding
them together would need parameterization longer than what it removes.

extractParameterKey merged the glob and grep branches, and the websearch
and codesearch branches, which had identical bodies and identical fall
through. The single field tools now share one lookup.
iteration-nudge.ts, turn-nudge.ts and context-limit-nudge.ts each held
one template string. They are constants in store.ts now.

PROMPT_KEYS restated the same seven keys that PROMPT_DEFINITIONS already
carries. It derives from the definitions.
.npmignore does nothing next to the files array in package.json. When
both are present files wins, and the files array is the one that runs.

report() in lib/v2/index.ts was a stub the V2 entry point never called
on a path that reaches a log. reportToLog does the same write directly.
@amnesiaof
amnesiaof force-pushed the fix/sdk-media-asset-and-dedup branch from a12daae to ae1cbb8 Compare October 3, 2026 08:25
@amnesiaof amnesiaof changed the title fix: support the SDK media Asset and collapse duplicated helpers refactor: collapse duplicated prune, inject, state hydration and nudge helpers Oct 3, 2026
@amnesiaof

amnesiaof commented Oct 3, 2026 •

Copy link
Copy Markdown
Author

@Tarquinen whenever you have a moment, could you take a look? This collapses duplicated helpers. Best merged after #651.

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