Skip to content

feat(sdk): route chat.agent transcript persistence through a TranscriptStorage seam - #4893

Open
ericallam wants to merge 1 commit into
feat/transcript-snapshot-v2-tri-13667from
feat/transcript-storage-seam-tri-13667
Open

feat(sdk): route chat.agent transcript persistence through a TranscriptStorage seam#4893
ericallam wants to merge 1 commit into
feat/transcript-snapshot-v2-tri-13667from
feat/transcript-storage-seam-tri-13667

Conversation

@ericallam

@ericallam ericallam commented Sep 5, 2026

Copy link
Copy Markdown
Member

Summary

Introduces the TranscriptStorage seam inside chat.agent and makes the built-in snapshot writer its default implementation. No public option yet; the default path behaves as before, apart from the blob now being written as version 2.

Design

A storage has load (called once when a run boots to continue a conversation) and save (called after every turn, failed turn and history-changing action). save receives a changeset of id-addressed operations: put (upsert by message id), remove, truncateAfter and state, plus the stream cursors the next boot resumes from.

The runtime keeps a shadow of the transcript it last handed to save (ids in order plus a fingerprint per message) and diffs the accumulator against it. A changed message in the common prefix is an in-place put; anything past the prefix is one truncateAfter on the last common id followed by puts in order. Applying the result reproduces the accumulator exactly for any edit, and the common cases come out as the natural operations: a turn is two puts, an undo is one truncateAfter, a regenerate is a truncateAfter and a put. The shadow only advances when save resolves, so a failed save is folded into the next changeset; every operation is idempotent, so a retried changeset converges.

Every changeset also carries the whole transcript as it stands after the changes (entries with a final flag, plus the runtime's state). A row-per-message store applies the changes; a store that keeps the conversation as one document writes the transcript as-is and needs no state of its own between saves. The default storage is the second kind: it serialises the transcript and rewrites the blob, so a turn still costs one PUT and no GET, and nothing is held in memory between saves. The snapshot read and write helpers move to their own module so the storage can import them without a cycle; the test seams keep their import path.

@changeset-bot

changeset-bot Bot commented Sep 5, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a075eb1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Walkthrough

The change adds transcript storage with reducers, shadow-based diffs, pagination, cursors, and version 2 snapshot persistence. It adds injectable and production snapshot I/O paths with failure handling. Chat agent persistence now uses transcript changesets for loading, completed turns, actions, and errors. Mocks and integration tests support version 1 compatibility and version 2 message envelopes.

Merge Risk: 🟡 Moderate · up to a075e

Continuation sessions can overwrite saved transcript state and mark interrupted responses complete, while snapshot downloads may expose signing credentials to an unintended destination. These persistence and security issues should be resolved before merge.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 63.16% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 11 files. (1 skipped:… Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The description gives a detailed and relevant explanation of the TranscriptStorage design and runtime behavior, but it omits the required issue closure, checklist, Testing, Changelog, and Screenshots … Add the required template sections. Include the linked issue, complete the checklist, document test commands and results, add a short changelog entry, and provide screenshots or state that screenshots are not applicable.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely identifies the main change: routing chat.agent transcript persistence through the new TranscriptStorage seam.
Full details: Docstring Coverage

Explanation

Docstring coverage is 63.16% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 11 files. (1 skipped: 1 too large.)

Full details: Description check

Explanation

The description gives a detailed and relevant explanation of the TranscriptStorage design and runtime behavior, but it omits the required issue closure, checklist, Testing, Changelog, and Screenshots sections from the repository template.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/transcript-storage-seam-tri-13667

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch from 4c91a95 to a7e6626 Compare September 5, 2026 20:29
@pkg-pr-new

pkg-pr-new Bot commented Sep 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trigger.dev/build

npm i https://pkg.pr.new/@trigger.dev/build@a075eb1

trigger.dev

npm i https://pkg.pr.new/trigger.dev@a075eb1

@trigger.dev/core

npm i https://pkg.pr.new/@trigger.dev/core@a075eb1

@trigger.dev/python

npm i https://pkg.pr.new/@trigger.dev/python@a075eb1

@trigger.dev/react-hooks

npm i https://pkg.pr.new/@trigger.dev/react-hooks@a075eb1

@trigger.dev/redis-worker

npm i https://pkg.pr.new/@trigger.dev/redis-worker@a075eb1

@trigger.dev/rsc

npm i https://pkg.pr.new/@trigger.dev/rsc@a075eb1

@trigger.dev/schema-to-json

npm i https://pkg.pr.new/@trigger.dev/schema-to-json@a075eb1

@trigger.dev/sdk

npm i https://pkg.pr.new/@trigger.dev/sdk@a075eb1

commit: a075eb1

@ericallam
ericallam marked this pull request as ready for review September 5, 2026 20:33
devin-ai-integration[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch 2 times, most recently from 49acce9 to f5c6372 Compare September 6, 2026 05:42
devin-ai-integration[bot]

This comment was marked as resolved.

@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch from f5c6372 to 15f04f2 Compare September 6, 2026 05:54
devin-ai-integration[bot]

This comment was marked as resolved.

@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch from 15f04f2 to 8ead6fd Compare September 6, 2026 06:18
devin-ai-integration[bot]

This comment was marked as resolved.

@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch from 8ead6fd to 85f3ff1 Compare September 7, 2026 07:48
devin-ai-integration[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch from 85f3ff1 to 627899d Compare September 7, 2026 08:16
@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch from 627899d to 5b764c7 Compare September 7, 2026 10:22
…ptStorage seam

Introduces the TranscriptStorage interface (load/save over id-addressed changes: put, remove, truncateAfter, state) and makes the built-in snapshot writer its default implementation. The runtime keeps a shadow of the transcript it last saved and hands the storage the diff after every turn, failed turn and history-changing action, together with the stream cursors the next boot resumes from. The default storage reduces each changeset onto an in-memory copy and rewrites the blob as version 2, so a turn still costs one PUT and no GET, and the boot read goes through load.

The snapshot read/write helpers move to their own module so the storage can import them without a cycle; the test seams keep their import path. The mock chat agent harness now reports version 2 snapshots and accepts either version as a seed.
@ericallam
ericallam force-pushed the feat/transcript-storage-seam-tri-13667 branch from 5b764c7 to a075eb1 Compare September 8, 2026 09:48

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Devin Review

turn,
trigger: storageTrigger(currentWirePayload.trigger),
clientData: turnClientData,
lastOutEventId: lastSnapshotOutEventId,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Failed turns persist stale replay cursors

After a turn fails, saveTranscript receives the prior successful turn's cursor. Continuations replay already-superseded output, and later actions preserve that stale position.

Prompt for agents
In packages/trigger-sdk/src/v3/ai.ts, the error path saves lastSnapshotOutEventId even though writeTurnCompleteChunk produced errorTurnCompleteResult.lastEventId. Persist the failed turn's completion cursor and update the shared lastSnapshotOutEventId holder so any later cursor-neutral action retains it. Preserve the previous cursor only when the error completion write returned no event ID. The existing error-snapshot-cursor.test.ts describes and exercises this invariant.
Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
packages/trigger-sdk/src/v3/transcriptStorage.ts (1)

273-280: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Return nonFinalIds from snapshotTranscriptStorage.load. Version-2 entries persist final, but load maps them to messages and omits IDs where final: false. If continuation boot passes this result to createTranscriptShadow, the next unchanged save can mark a persisted partial message as final. Collect and return those IDs.

packages/trigger-sdk/src/v3/test/mock-chat-agent.ts (1)

813-813: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Keep the seeded input cursor monotonic.

TestSessionStreamManager.setLastSeqNum overwrites the cursor. If live input advances it to 5 and seedSessionInTail seeds two messages, the cursor becomes 2. The next automatic input receives sequence 3 and reuses an existing number. This can break replay and ordering assertions. Preserve the maximum, or reset the manager and seeded sequence numbers together.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 48a0c9ab-2982-4e39-a28f-151ae684d3fd

📥 Commits

Reviewing files that changed from the base of the PR and between 85f3ff1 and a075eb1.

📒 Files selected for processing (2)
  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📜 Review details
⏰ Context from checks skipped due to timeout. (27)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (12, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (16, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (19, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (24, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (9, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (14, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (20, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (21, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (18, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (15, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (17, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (11, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (13, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (10, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (7, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (1, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (6, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (8, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (2, 24)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (5, 24)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (1, 3)
  • GitHub Check: e2e-webapp / 🧪 E2E Tests: Webapp (2, 2)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (2, 3)
  • GitHub Check: internal / 🧪 Unit Tests: Internal
  • GitHub Check: e2e-webapp / 🧪 E2E Tests: Webapp (1, 2)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (3, 3)
  • GitHub Check: Analyze (javascript-typescript)
🧰 Additional context used
📓 Path-based instructions (7)
Always import from `@trigger.dev/sdk`.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts
**Prefer static imports over dynamic imports.**

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts
Add crumbs as you write code — not just when debugging.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts
In the Trigger.dev SDK (packages/trigger-sdk), prefer isomorphic code like fetch and ReadableStream instead of Node.js-specific code

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Files:

  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts
Use function declarations instead of default exports

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Files:

  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts
Use types over interfaces for TypeScript Avoid using enums; prefer string unions or const objects instead

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Files:

  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts
When creating or editing OTEL metrics (counters, histograms, gauges), ensure metric attributes have low cardinality by using only enums, booleans, bounded error codes, or bounded shard IDs Do not use high-cardinality attributes in OTEL metr...

📄 CodeRabbit inference engine (.cursor/rules/otel-metrics.mdc)

Files:

  • packages/trigger-sdk/src/v3/ai.ts
  • packages/trigger-sdk/src/v3/test/mock-chat-agent.ts
🪛 ast-grep (0.45.2)
packages/trigger-sdk/src/v3/ai.ts

[warning] 7119-7122: Avoid logging sensitive data
Context: logger.warn("chat.agent: transcript load failed; continuing from the stream tail", {
error: error instanceof Error ? error.message : String(error),
sessionId: sessionIdForSnapshot,
})
Note: [CWE-532] Insertion of Sensitive Information into Log File.

(log-sensitive-data-typescript)

🔇 Additional comments (8)
packages/trigger-sdk/src/v3/test/mock-chat-agent.ts (2)

3-3: LGTM!

Also applies to: 104-104, 240-240, 314-314, 443-444, 453-456


414-419: 🗄️ Data Integrity & Integration

No change needed. drivers.sessions.in.send accepts (sessionId, data, io, metadata) and forwards io and metadata.seqNum to the test session stream manager.

packages/trigger-sdk/src/v3/ai.ts (6)

7902-7902: 🗄️ Data Integrity & Integration | ⚡ Quick win

Fix the stale clientData fallback used on the error-persistence path.

turnClientData is declared inside the turn loop as payload.metadata, where payload is the run's original boot payload captured once at function entry. This value is used only as the fallback for the turn-error transcript save when parseClientData(wireMetadata) throws before clientData is assigned at Line 7933.

For any turn after turn 0, currentWirePayload (the current wire message) differs from payload (the boot payload). If parsing fails on turn > 0, the error-path saveTranscript call records the turn-0 boot metadata instead of the current turn's metadata. A custom TranscriptStorage implementation that partitions or routes by clientData receives incorrect data for that changeset.

Seed the fallback from the current turn's wire payload instead of the fixed boot payload.

🐛 Proposed fix
-          let turnClientData: unknown = payload.metadata;
+          let turnClientData: unknown = currentWirePayload.metadata;

41-41: LGTM!

Also applies to: 79-86, 125-126, 133-133, 263-268


6951-6963: LGTM!

Also applies to: 6975-7031, 7043-7057, 7109-7130


7249-7253: LGTM!

Also applies to: 7257-7257, 8071-8073, 8442-8473, 8489-8489, 8501-8501, 9328-9339, 9711-9719


2283-2283: LGTM!

Also applies to: 2314-2324, 2335-2337, 3177-3180, 5857-5868, 6798-6798, 8627-8632, 9378-9389, 9736-9738, 10385-10461, 10505-10536, 11469-11473, 11690-11690, 11800-11802, 11921-11922, 13123-13166


12471-12471: LGTM!

Also applies to: 12515-12515, 12538-12542, 12624-12631, 12658-12661, 12677-12682, 12717-12717, 12754-12764, 12776-12777, 12786-12791

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.

2 participants