Repository navigation
feat(core): check a typed collection's write expectation in Stack - #431
Merged
Merged
Conversation
A typed collection must refuse a write to a record outside its Type family, and notice a content write to a record stored at an older version. Doing either from outside Stack costs a read per write, or throws only after the write has landed. Every write verb now accepts an internal expectation under a module-private symbol key: the family, plus an exact typeId checked on a content patch. Stack checks it against the read it already makes; a family mismatch is StackNotFoundError, a version mismatch is a StoredVersionError carrying the stored record. ScopedStack checks the family ahead of its permission gate, so another family's record is not found whatever the requester may do to it, and passes the expectation through. Closes #408 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GomaLzoB7N2ipg547Kr6h4
🦋 Changeset detectedLatest commit: 74c834b The changes in this PR will be included in the next version bump. This PR includes changesets to release 10 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…ugh them - Stack.mutate checks the stored version after the tombstone and ifVersion refusals, so a deleted record is never handed back in a StoredVersionError and a stale caller gets a version conflict first. - ScopedStack skips its content gates when the patch targets another version, and pins ifVersion to its read so Stack must refuse it. - A purge under an expectation is pinned to the version it checked, and purges nothing when the record is missing. ScopedStack pins its delete the same way. - typedMutate passes an exact expectation instead of its own pre-read: one read per write, and no race between the check and the write. - Changeset raised to minor; seedShelf now grants SHARE as documented. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Pzh2Awag1hD5tK4Y2yarfz
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #408. Every write verb (
mutate,patchContent,associate,dissociate,amendAssociations,grantAccess,revokeAccess,amendAccess,delete,undelete,restoreVersion) now accepts an internal write expectation, passed under a module-private symbol key (WRITE_EXPECTATIONinpackages/core/src/write-expectation.ts). The expectation names abaseId, can name an exacttypeId, and can setexactto hold every write (not only a content write) to thattypeId.Stackchecks the expectation against the read each verb already makes, before anything is written.StackNotFoundError.typeIdmismatch on a content patch (or on any write, withexact) throwsStoredVersionError, which carries the stored record. It is checked after the tombstone refusal and theifVersionprecondition, so a deleted record is never handed back and a stale caller learns of the conflict first.ScopedStackchecks the family ahead of its permission gate, so another family's record is "not found" whatever the requester may do to it. When a patch targets another version, it skips its content gates (which read the stored Type) and forwards withifVersionpinned to its own read, soStackmust refuse it. The association and access verbs gained an optional options argument, taking options third andsurfacefourth, asStack's do.ScopedStackpins its delete to its own read the same way.mutate()/patchContent()now pass anexactexpectation instead of making their own read first: one read per write, with no gap between the check and the write.Spec
docs/spec/data-model.md § Type handlesis updated. Observable change: a typed write to a record of another family now throwsStackNotFoundError(it wasStackBadRequestError). A record of the family stored at another version is still refused withStackBadRequestError, and a deleted record or staleifVersionis refused first, as for an untyped write. The changeset isminor.Verification
All passing locally:
Tests in
packages/core/tests/write-expectation.test.tscover bothStackandScopedStack:ifVersionas a version conflict, ahead of the stored version.packages/core/tests/type-handle.test.tspins the typed write: another family is not found, another version throws before writing (content and association-only), and a typed write reads no more often than an untyped one.Notes for reviewers
Stackmakes one extra read when an expectation is present, since a purge otherwise reads nothing first. The returned record still comes from the purge itself.StackVersionConflictError, naming anifVersionthe caller never passed. The pin also only partly guards the case it's meant for: an ID purged and re-created in another family starts again at version 1, so it would still match a pin of 1. It's kept because it's cheap and retrying after a conflict is a reasonable response.restoreVersionchecks the family only. A snapshot is validated against its own stored Type, so the record's current version doesn't affect the write.🤖 Generated with Claude Code
https://claude.ai/code/session_01Pzh2Awag1hD5tK4Y2yarfz