[GH-ISSUE #990] [Feature]: X Claude Subscription #481

Closed
opened 2026-10-20 17:29:19 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @aeriondyseti on GitHub (May 19, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/990

What problem does this solve?

The claude_subscription provider folds the whole chat history into one big User: ... / Assistant: ... string prompt, then sends that as the request. This kills prompt caching — every turn, that string is slightly different (new user message at the end), so the cache key changes and we pay full input-token cost every time. With real multi-turn messages we'd actually get cache hits on the stable prefix.

Two other things break as side effects:

  1. Tool messages get mangled. renderTranscript() drops assistant turns with empty content (line 64), which is exactly what a tool_calls-only turn looks like. Tool-result messages (role: "tool") get relabelled as "Assistant: ". Anything routed through agent-executor.ts (lorebook-maker, character-maker, encounter, scene, knowledge-retrieval) on a claude_subscription connection ends up showing the model tool results without the calls that produced them.
  2. Images get silently dropped. Other providers (Anthropic, OpenAI, Google) read ChatMessage.images for attachments. This one doesn't. The image just disappears at the provider boundary.

Proposed solution

Stop folding. Use the Claude Agent SDK's resume option — it's exactly built for this.

Before each query() call, write a temp JSONL file at ~/.claude/projects/{cwd-as-dashes}/{uuid}.jsonl with one entry per prior turn (proper tool_use / tool_result / image blocks). Pass resume: {uuid} in SDK options. Send only the current user message as prompt. Delete the file in finally. Sweep orphans on server boot.

Two new modules under packages/server/src/services/llm/providers/claude-subscription/:

  • jsonl-entries.ts — pure builder for the synthetic entries
  • synthetic-session.ts — write / cleanup / orphan sweep

Kill switch: env var CLAUDE_SUBSCRIPTION_USE_RESUME (default false to make this opt-in). Set to false to go back to the current fold path if anything explodes. On Windows or if ~/.claude/projects/ isn't writable, falls back automatically with a one-time warn. (I don't have access to a Windows machine to see how the project directories are constructed.)

Alternatives considered

  • Sidecar HTTP process that fronts the SDK and gets called over HTTP. Cleanest separation but breaks the single-process deploy.
  • Loopback passthrough in-process to also strip the PII the SDK injects on the wire (account UUID, email, "you are a Claude agent" preamble). Bigger change; doesn't need to ride along with this one.
  • Just patch the fold. Keep the empty-content filter from dropping tool_calls turns, plumb images through the string. Doesn't fix caching, accumulates band-aids on a serialization that's lossy by design.

Additional context

  • SDK resume is documented at https://docs.anthropic.com/en/docs/claude-code/sdk. It reads JSONL from
    ~/.claude/projects/{cwd-with-slashes-as-dashes}/{sessionId}.jsonl. We just write the file ourselves before calling.
  • Entry schema verified against real CC sessions on SDK v2.1.126 (the envelope, content blocks, stop_reason, parent-uuid chain).
  • Related PRs: #243 (provider was added), #549 (silent model identity loss), #294 (helper routes), #362 (clear API key on switch), #246 (baseUrl gate).
  • There doesn't appear to be any automated test suite. This PR adds a narrow one — node:test via tsx --test, no new devDependencies, 34 unit tests for the new modules only. Doesn't touch pnpm check. I can remove this if you'd like
  • Things to manually verify if this lands: (a) multi-turn chat actually preserves prior turns on regen/branch swap, (b) lorebook-maker (or similar agent route) on Claude Subscription doesn't lose tool turns anymore, (c) an attached image actually reaches the model, (d) CLAUDE_SUBSCRIPTION_USE_RESUME=false falls back to the old fold cleanly.

Template check

  • Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked.
Originally created by @aeriondyseti on GitHub (May 19, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/990 ## What problem does this solve? The claude_subscription provider folds the whole chat history into one big User: ... / Assistant: ... string prompt, then sends that as the request. This kills prompt caching — every turn, that string is slightly different (new user message at the end), so the cache key changes and we pay full input-token cost every time. With real multi-turn messages we'd actually get cache hits on the stable prefix. Two other things break as side effects: 1. Tool messages get mangled. renderTranscript() drops assistant turns with empty content (line 64), which is exactly what a tool_calls-only turn looks like. Tool-result messages (role: "tool") get relabelled as "Assistant: <result>". Anything routed through agent-executor.ts (lorebook-maker, character-maker, encounter, scene, knowledge-retrieval) on a claude_subscription connection ends up showing the model tool results without the calls that produced them. 2. Images get silently dropped. Other providers (Anthropic, OpenAI, Google) read ChatMessage.images for attachments. This one doesn't. The image just disappears at the provider boundary. ## Proposed solution Stop folding. Use the Claude Agent SDK's resume option — it's exactly built for this. Before each query() call, write a temp JSONL file at ~/.claude/projects/{cwd-as-dashes}/{uuid}.jsonl with one entry per prior turn (proper tool_use / tool_result / image blocks). Pass resume: {uuid} in SDK options. Send only the current user message as prompt. Delete the file in finally. Sweep orphans on server boot. Two new modules under packages/server/src/services/llm/providers/claude-subscription/: - jsonl-entries.ts — pure builder for the synthetic entries - synthetic-session.ts — write / cleanup / orphan sweep Kill switch: env var CLAUDE_SUBSCRIPTION_USE_RESUME (default false to make this opt-in). Set to false to go back to the current fold path if anything explodes. On Windows or if ~/.claude/projects/ isn't writable, falls back automatically with a one-time warn. (I don't have access to a Windows machine to see how the project directories are constructed.) ## Alternatives considered - Sidecar HTTP process that fronts the SDK and gets called over HTTP. Cleanest separation but breaks the single-process deploy. - Loopback passthrough in-process to also strip the PII the SDK injects on the wire (account UUID, email, "you are a Claude agent" preamble). Bigger change; doesn't need to ride along with this one. - Just patch the fold. Keep the empty-content filter from dropping tool_calls turns, plumb images through the string. Doesn't fix caching, accumulates band-aids on a serialization that's lossy by design. ## Additional context - SDK resume is documented at https://docs.anthropic.com/en/docs/claude-code/sdk. It reads JSONL from ~/.claude/projects/{cwd-with-slashes-as-dashes}/{sessionId}.jsonl. We just write the file ourselves before calling. - Entry schema verified against real CC sessions on SDK v2.1.126 (the envelope, content blocks, stop_reason, parent-uuid chain). - Related PRs: #243 (provider was added), #549 (silent model identity loss), #294 (helper routes), #362 (clear API key on switch), #246 (baseUrl gate). - There doesn't appear to be any automated test suite. This PR adds a narrow one — `node:test via tsx --test`, **no new devDependencies**, 34 unit tests for the new modules only. Doesn't touch pnpm check. *I can remove this if you'd like* - Things to manually verify if this lands: (a) multi-turn chat actually preserves prior turns on regen/branch swap, (b) lorebook-maker (or similar agent route) on Claude Subscription doesn't lose tool turns anymore, (c) an attached image actually reaches the model, (d) CLAUDE_SUBSCRIPTION_USE_RESUME=false falls back to the old fold cleanly. ## Template check - [ ] Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked.
Author
Owner

@aeriondyseti commented on GitHub (May 19, 2026):

Picking this up on feat/claude-subscription-jsonl-resume (work-in-progress fork: https://github.com/aeriondyseti/Marinara-Engine/tree/feat/claude-subscription-jsonl-resume).

Current status: pure JSONL builder + FS lifecycle modules landed on the branch with 34 unit tests passing. Provider rewiring (the actual swap from transcript-fold to SDK resume) is the next commit. Draft PR will go up against staging once that lands.

<!-- gh-comment-id:4483893165 --> @aeriondyseti commented on GitHub (May 19, 2026): Picking this up on `feat/claude-subscription-jsonl-resume` (work-in-progress fork: https://github.com/aeriondyseti/Marinara-Engine/tree/feat/claude-subscription-jsonl-resume). Current status: pure JSONL builder + FS lifecycle modules landed on the branch with 34 unit tests passing. Provider rewiring (the actual swap from transcript-fold to SDK `resume`) is the next commit. Draft PR will go up against `staging` once that lands.
Author
Owner

@cha1latte commented on GitHub (May 19, 2026):

Triage note: labeling this as scope-risk because the request replaces Claude Subscription transcript folding with SDK resume plus JSONL session lifecycle, cleanup behavior, and tool/image history preservation.

The least risky next step is to keep this tied to the active draft PR and require focused provider verification before it is marked ready.

<!-- gh-comment-id:4489007079 --> @cha1latte commented on GitHub (May 19, 2026): Triage note: labeling this as `scope-risk` because the request replaces Claude Subscription transcript folding with SDK resume plus JSONL session lifecycle, cleanup behavior, and tool/image history preservation. The least risky next step is to keep this tied to the active draft PR and require focused provider verification before it is marked ready.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#481
No description provided.