mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #991] [MERGED] feat(claude-subscription): swap transcript fold for SDK resume #4878
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#4878
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/991
Author: @aeriondyseti
Created: 5/19/2026
Status: ✅ Merged
Merged: 5/22/2026
Merged by: @cha1latte
Base:
staging← Head:feat/claude-subscription-jsonl-resume📝 Commits (10+)
1cdf046feat(claude-subscription): add synthetic JSONL session builder + lifecycle7543bfbfeat(claude-subscription): swap transcript fold for SDK resume445fe9cchore(claude-subscription): address review feedback on resume path342d85cfeat(claude-subscription): honor CLAUDE_CONFIG_DIR + pin docker default90f0077wip(claude-subscription): empty-history skip + context strip + SDK bump9f599d6test(claude-subscription): emit text delta from fake SDK8a73ea3fix(claude-subscription): address resume-branch review feedback9f6033ctest(claude-subscription): make server test suite Windows-safe7ed9ba5test(claude-subscription): skip resume-path wiring suite on win328a17947fix(claude-subscription): isolate account connectors, add observability📊 Changes
17 files changed (+2086 additions, -89 deletions)
View changed files
📝
.gitignore(+4 -0)➕
.nvmrc(+1 -0)📝
Dockerfile(+5 -0)📝
Dockerfile.lite(+5 -0)📝
docs/CONFIGURATION.md(+2 -0)📝
package.json(+0 -16)📝
packages/server/package.json(+2 -2)➕
packages/server/scripts/run-tests.mjs(+53 -0)📝
packages/server/src/config/runtime-config.ts(+14 -0)➕
packages/server/src/services/llm/providers/__tests__/claude-subscription.provider.test.ts(+461 -0)📝
packages/server/src/services/llm/providers/claude-subscription.provider.ts(+319 -29)➕
packages/server/src/services/llm/providers/claude-subscription/__tests__/jsonl-entries.test.ts(+516 -0)➕
packages/server/src/services/llm/providers/claude-subscription/__tests__/session-store.test.ts(+49 -0)➕
packages/server/src/services/llm/providers/claude-subscription/jsonl-entries.ts(+514 -0)➕
packages/server/src/services/llm/providers/claude-subscription/session-store.ts(+83 -0)📝
pnpm-lock.yaml(+42 -42)📝
pnpm-workspace.yaml(+16 -0)📄 Description
Linked issue
Closes #990.
Why this change
Claude subscription connections become substantially cheaper on long chats after this lands — prompt caching actually works. The previous transcript-fold approach folded the entire chat history into one string per request, so the cache key changed every turn and we paid full input-token cost on every message. The new path replays prior turns as a real resumed session via the Claude Agent SDK, which preserves the cache prefix across turns.
Two pre-existing silent bugs in the same provider are also fixed incidentally:
renderTranscript()dropped assistant turns with emptycontent— exactly what atool_calls-only turn looks like — and relabelledrole: "tool"messages as"Assistant: ...". Agentic flows (lorebook-maker / character-maker / encounter / scene / knowledge-retrieval) on aclaude_subscriptionconnection saw tool results without the calls that produced them. The resume path preservestool_use/tool_resultblocks natively.ChatMessage.images; this one ignored it. Images now flow through both the resumed history and the SDK's structured prompt.What changed
Prior turns reach the model through the Claude Agent SDK's
sessionStoreadapter (a documented, typed,@alphapublic API — its shape was byte-identical across0.2.141→0.3.145):splitHistoryForResume(messages)decides how the history is shaped:assistantPrefillfromgenerate.routes.ts) → history = all (prefill stays in history); prompt = synthetic(continue)user turn[Start]user turnResumeSessionStore— a one-shot in-processSessionStore. The provider mints a fresh UUID per request, builds the JSONL entries withassembleEntries(), and registers them under that UUID. The SDK callssessionStore.load()once before subprocess spawn, materializes the transcript to its own temp file, resumes from it, and reaps it.load()matches onsessionIdalone and ignoresprojectKey— so the provider never has to replicate the SDK's private cwd→projectKey encoding.resume+cwd+sessionStoreare reserved SDK option keys, written AFTERapplyCustomParametersso connection-levelcustomParameterscan't forge them (an integration test fires all three forged to confirm).resumeScratchCwd()— a dedicateddata/claude-subscription-scratchworking directory.sessionStorecannot be combined withpersistSession: false, so the SDK still writes each live turn to a real Claude Code session file; pointingcwdhere keeps those out of the user's realclaudeCLI session list for the repo. They are reaped by Claude Code's owncleanupPeriodDays.CLAUDE_SUBSCRIPTION_USE_RESUME=falsekill switch and the degradation target if scratch-dir creation fails. Not legacy code; permanent coexistence.This supersedes an earlier hand-rolled approach (in prior commits on this branch) that wrote a synthetic JSONL into
~/.claude/projects/<cwd-as-dashes>/and reaped it with a boot-time sweep. That required replicating Claude Code's cwd→project-dir encoding, which had no verified Windows form. Handing the filesystem to the SDK deletes that encoding, the orphan sweep, the.mrsidecar scheme, and the EACCES cooldown — and the resume path now has zero platform branches (net +441 / −1035 lines).Validation
pnpm checkpasses locallypnpm --filter @marinara-engine/server run testpassesCONTRIBUTING.mdManual verification notes
The automated tests use an injected fake SDK — they verify Marinara's wiring (correct options passed, the store serves
load()), not that the real SDK materializes and resumes a session. The following need a real run withclaude logincompleted:cache_read_input_tokensshould be non-zero on turn 2+tool_use/tool_resulthistory intact end-to-endCLAUDE_SUBSCRIPTION_USE_RESUME=falsefalls back to transcript-fold cleanlyassistantPrefillset — trailing assistant seeds the continuationdata/claude-subscription-scratch's project bucket, not the repo'sDocs and release impact
Server-only internal change — no user-facing config or behavior surface, no docs (README / CONTRIBUTING / android/README / CHANGELOG) require updating. No version/release files touched.
UI evidence (if applicable)
N/A — server-only change. No UI surface affected.
Known limitations
promptis user-only; it has no surface to author a trailing assistant in the outbound API call. Both the fold path and the resume path degrade native Anthropic prefill to a heuristic(continue)continuation. A loopback-passthrough that rewrites outbound API bodies (out of scope) would unlock native prefill.sessionStoreis@alpha. It is a documented, typed, exported SDK API and its shape held byte-identical across0.2.141→0.3.145, but it carries no stability guarantee. TheSDK_VERSIONpin test is the tripwire.Test coverage
node:testviatsx --test, zero new devDependencies. 50 tests, all passing;tsc --noEmitclean.jsonl-entries.ts— block builders,assembleEntries,splitHistoryForResume,currentToSdkUserMessage,SDK_VERSIONResumeSessionStore—load()matches on sessionId / ignores projectKey / returns null for unknown ids;append()no-op__setSdkForTesting— resume/cwd/sessionStore wiring, history fed viaload(), image flow on current + historical turns, prefill→continuation,customParameterscannot forge the reserved keys, single-turn skips resume, concurrent calls get distinct stores, systemPrompt parity (resume vs fold), auto-context strip, and resume engages onwin32(the inverse of the deleted win32 fold-path test — pins that the platform gate is gone)Files touched
packages/server/src/config/runtime-config.ts— kill-switch doc commentpackages/server/src/index.ts— boot-time orphan sweep removedpackages/server/src/services/llm/providers/claude-subscription.provider.ts— resume/fold dispatcher rewired ontosessionStore; cooldown state +win32gates removedpackages/server/src/services/llm/providers/claude-subscription/jsonl-entries.ts—assembleEntriesmoved in;serializeEntriesremovedpackages/server/src/services/llm/providers/claude-subscription/session-store.ts— new —ResumeSessionStore+resumeScratchCwdpackages/server/src/services/llm/providers/claude-subscription/synthetic-session.ts— deletedsynthetic-session.test.tsdeleted;session-store.test.tsadded;jsonl-entries.test.ts+claude-subscription.provider.test.tsupdatedSummary by CodeRabbit
New Features
Documentation
Tests
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.