Skip to content

Fix projected ops mutating the shared create object - #782

Open
draarivpatel-ui wants to merge 2 commits into
share:mainfrom
draarivpatel-ui:fix-projectop-shared-create
Open

draarivpatel-ui wants to merge 2 commits into
share:mainfrom
draarivpatel-ui:fix-projectop-shared-create

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #779

Bug

projectOp() projects op.create in place. Ops emitted from pubsub are shared between every subscribed agent, and Agent._onOp() only makes a shallow copy, so all agents share the same create object. Whichever agent's sanitizeOp projects the create first rewrites create.data for everyone else, so a subscriber on the unprojected collection (or a differently projected one) sends its client projected create data until it fetches again.

Fix

Project a copy of the create object and assign it back onto the op, matching how projectEdit already builds a new component array rather than editing it in place.

Test

Added a regression test that mirrors Agent._onOp: shallow-copy the op, project the copy, and assert the shared create object keeps its original data while the copy is projected. It fails on main (create.data ends up filtered) and passes with this fix. Full suite: 1113 passing.

projectOp() projected op.create in place. Ops from pubsub are shared
between subscribed agents and Agent._onOp only makes a shallow copy,
so whichever agent projected the create first rewrote the create data
for every other subscriber. An agent subscribed to the unprojected
collection could then send the client projected create data.

Project a copy of the create object instead, matching how projectEdit
already returns a new component array.

Fixes share#779
Copilot AI balanced review requested due to automatic review settings November 24, 2026 19:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@alecgibson alecgibson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for picking this up. The fix looks right to me. It's what the contract in Agent._onOp already asks for ("projections and any user middleware copy deep properties as needed when they modify the op"), and it matches what projectEdit already does for op.op. My main ask is about the test: I'd rather it ran the repro from #779 end to end than hand-copy _onOp in a unit test.

Nit (personal preference): test/projections.js:271 — If we keep a unit test, I don't think the shallow copy of op is needed. Projecting op directly and asserting that the original create is untouched checks the same thing ("projectOp doesn't mutate what it's given") without copying _onOp. If the copy does stay, ../lib/util is already required at the top for clone, so shallowCopy could come from there too.


Generated by Claude Code via /alec-review. Patterns derived from Alec's historical review comments — review with that in mind.

Comment thread test/projections.js Outdated
);
});

it('does not mutate the create object on a shallow copy of the op', function() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we test this "deep" in test/client/projections.js instead? This test copies _onOp's shallow copy by hand, so if _onOp ever changes how it copies, this test will keep passing without proving anything. The repro from #779 drops straight in as a client test. I tried it: it fails on main (expected { age: 5 } to deeply equal { age: 5, color: 'spotted' }) and passes with your fix. That file also runs against the other DB adapters, so we get those for free.

it('does not project the create sent to an unprojected subscriber', function(done) {
  var connection = this.connection;
  var summary = this.backend.connect().get('dogs_summary', 'spot');
  var full = this.backend.connect().get('dogs', 'spot');
  summary.subscribe(function(err) {
    if (err) return done(err);
    full.subscribe(function(err) {
      if (err) return done(err);
      full.on('create', function() {
        expect(full.data).eql({age: 5, color: 'spotted'});
        done();
      });
      connection.get('dogs', 'spot').create({age: 5, color: 'spotted'});
    });
  });
});

(The subscription order matters: the projected agent has to sanitize first.)

Comment thread test/projections.js Outdated
Comment on lines +265 to +268
// Pubsub hands the same op object to every subscribed agent, and
// agent._onOp only makes a shallow copy, so the create object is
// shared. Projecting one agent's copy must not rewrite the create
// data seen by other subscribers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need this comment? It mostly repeats the test name and the PR description, and it will drift the moment _onOp changes. A client test like the one above explains itself.

Comment thread lib/projections.js Outdated
Comment on lines +32 to +33
// The op may be a shallow copy of an op object shared with other
// subscribers, so project a copy of the create instead of mutating it

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(Matter of taste.) Do we need this comment either? _onOp already spells out the contract, and the regression test fails if someone "simplifies" the copy away. I'd keep the why in the commit message, where git blame finds it. You've basically already written it in the PR description.

@draarivpatel-ui

Copy link
Copy Markdown
Author

Thanks, updated in de017df. The regression now runs end to end in test/client/projections.js with the reproduction you provided, and the explanatory comments plus the hand-copied unit test are removed. Verified locally with the new targeted mocha test, mocha test/projections.js test/db-memory.js at 398 passing, and eslint on the touched files.

This branch has not been deployed

No deployments
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.

Projecting a create op changes it for other subscribers

3 participants