feat(sdk): optional saveDelta hook on SessionStore#95
feat(sdk): optional saveDelta hook on SessionStore#95ketankhairnar wants to merge 2 commits intowithastro:mainfrom
Conversation
4c8b41a to
6094054
Compare
|
Quick housekeeping update:
Scope unchanged — still no tests in this PR, still no recipe addendum (the runnable example will land alongside #87 where the Postgres recipe lives — that's the natural home for it, and avoids cross-PR coupling). Ready when you are. |
|
Dry-run update for this branch: Verified against refreshed What passed:
Notes:
Remaining review concern from the dry run: this proves build/type health, but it does not resolve the persistence-contract questions around |
|
Follow-up fix pushed to What changed after the review pass:
Verification run:
Also noting the paired persistence recipe validation from #87 was not dry-run-only:
The live Postgres/D1 runs validate the store/recipe surface that #95 is meant to support conceptually; the |
5963a93 to
01d3447
Compare
Adds `SessionDelta` interface and optional `saveDelta?` method on
`SessionStore`. Adapters that implement it persist O(delta) per turn
instead of O(history). Existing implementations (in-memory, Cloudflare
DO) work unchanged because the method is optional.
The hook is non-breaking: if `saveDelta` is absent, the SDK falls
through to the existing `save(full)` path. If present, the SDK passes
only entries appended since the last save.
`SessionDelta` carries `{ newEntries, leafId, metadata }`. No
`supersedes` field in v1 — `appendCompaction` is append-only (it pushes
a new `CompactionEntry` without mutating prior history) so the
recipe-side adapter recovers prior entries from its own delta records on
load(). A `supersedes` field can be added later non-breakingly if a
future entry kind needs it.
Closes the SDK-side ask in withastro#89 (the recipe-side restructure landed as
part of withastro#87).
01d3447 to
622861d
Compare
Summary
Adds an optional
saveDelta?method onSessionStore, with a pairedSessionDeltainterface, so persistence adapters can persist O(delta) per turn instead of O(history).This is the SDK-side follow-up to #89. The recipe-side restructure (
category: "persist"+ Postgres/D1 connectors) is in #87.What's in the PR
packages/sdk/src/types.ts— addsSessionDeltainterface and optionalsaveDelta?onSessionStore.packages/sdk/src/session.ts— trackslastSavedEntryCount; insave(), branches ontypeof store.saveDelta === 'function'and calls the delta path when available, falls through tosave(full)otherwise.packages/sdk/src/session-history.ts— adds two small helpers,getEntryCount()andgetEntriesSince(index), used by the SDK to compute the slice.Non-breaking by design
saveDelta?is optional.InMemorySessionStore, the Cloudflare DO store, and the Postgres/D1 recipes from docs: add Postgres + D1 session persistence guides #87 all keep working with zero changes.save()see the existing call path.saveDelta?get only newly appended entries per turn.load(id)still returns fullSessionData— the adapter is responsible for reconstructing it from its delta records (this is natural for append-log shapes; documented on the JSDoc).What's NOT in the PR (deliberately)
mainyet; the test-infra work is on a different branch (feat/test-infra). Adding vitest as part of this PR would double its scope and conflict with the in-flight test infra. Test plan to land in a follow-up PR after both this and the test infra merge:save()— backward-compat round-trip.saveDelta?— assert payloads contain only new entries; reload + further appends work.saveDelta?(notsave).CompactionEntryand prior entries are not re-emitted.connectors/persist--postgres.md(in docs: add Postgres + D1 session persistence guides #87) will get an "Optional: append-log shape withsaveDelta" section in a follow-up PR after both this and docs: add Postgres + D1 session persistence guides #87 land.Design notes
SessionDeltacarries{ newEntries, leafId, metadata }only. Nosupersedesfield in v1:appendCompactionis append-only (it pushes a newCompactionEntrywithout mutating prior entries), so adapters recover prior history from their own delta records onload(). Asupersedesfield can be added later non-breakingly if a future entry kind needs it.Refs
Test plan
pnpm check:typespasses on thepackages/sdkworkspace.