Skip to content

Commit 82b713d

Browse files
Wintermutethomaspblock
andcommitted
fix(projects): make assignment fetch relay-real and partial-publish heal reachable
Round-3 review (wpfleger96) found two defects living in contracts the previous tests abstracted away: 1. Assignment-operation fetch re-created the bounded window it was meant to fix. The relay pushes only kinds/ids/authors/#e/single-#p/NIP-33-#d into SQL; #t/#a are post-filtered in Rust AFTER the SQL LIMIT (filter_to_query_params / filter_fully_pushable in crates/buzz-relay/src/handlers/req.rs). A #t-carrying filter therefore returns the newest 500 kind-1 candidates tag-filtered down — a short (often empty) page falsely signals exhaustion and an assignment older than ~500 unrelated notes is silently lost. The fetch is now keyed per issue via #e (SQL-pushed JSONB containment, issue ids already in hand at both call sites) with assignment labels filtered client-side, so a short page is a true end-of-results signal. The same-second edge (a full page whose oldest timestamp equals the inclusive until cursor) widens once to the relay's 1,000-row clamp and errors beyond that — surfacing a failed assignments section instead of silent loss, since NIP-01 filters cannot express the relay's composite (created_at, id) keyset cursor. Regression stubs now model the production relay: SQL-pushed constraints, LIMIT, then Rust post-filtering — plus the 0/1 buried-assignment case and both same-second densities. 2. The resume-aware dominated-write guard was unreachable for the very retry it exists to allow: after a partial publish the live head is necessarily newer than the cached snapshot (the failed mutation never updated the cache), so the earlier unconditional guard threw 'updated by another session' before the dangling-member probe ran — breaking the error message's own 're-run Add repository to resume' promise. The unconditional guard is deleted; the resume-aware guard provides identical protection for the non-resume case. The regression now exercises the exported mutation itself (guards included) through injected relay/publish seams: heal on both local and owner-agent paths, dominated-write still throws without a dangling member, and resume works with an unmoved head. Co-authored-by: Thomas Petersen <[email protected]> Signed-off-by: Thomas Petersen <[email protected]>
1 parent 265a349 commit 82b713d

7 files changed

Lines changed: 531 additions & 121 deletions

File tree

‎desktop/src/features/projects/assignmentOperationFetch.test.mjs‎

Lines changed: 154 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@ function makeIssue() {
2727
}
2828

2929
/** Owner-signed assignment operation (kind:1, `t: assignment`). */
30-
function makeAssignment(id, createdAt) {
30+
function makeAssignment(id, createdAt, issueId = ISSUE_ID) {
3131
return {
3232
id,
3333
kind: 1,
3434
pubkey: REPO_OWNER,
3535
created_at: createdAt,
3636
content: "Assigned",
3737
tags: [
38-
["e", ISSUE_ID, "", "root"],
38+
["e", issueId, "", "root"],
3939
["a", REPO_ADDRESS],
4040
["p", ASSIGNEE],
4141
["t", "assignment"],
4242
],
4343
};
4444
}
4545

46-
function makeComment(id, createdAt) {
46+
function makeComment(id, createdAt, issueId = ISSUE_ID) {
4747
return {
4848
id,
4949
kind: 1,
5050
pubkey: REPO_OWNER,
5151
created_at: createdAt,
5252
content: `Comment ${id.slice(0, 4)}`,
5353
tags: [
54-
["e", ISSUE_ID, "", "root"],
54+
["e", issueId, "", "root"],
5555
["a", REPO_ADDRESS],
5656
],
5757
};
@@ -61,62 +61,174 @@ function eventId(index) {
6161
return index.toString(16).padStart(64, "0");
6262
}
6363

64-
// ── fetchAssignmentOperationEvents: pagination to exhaustion ────────────────
65-
66-
test("fetchAssignmentOperationEvents paginates past the page limit", async () => {
67-
// 1,203 operations — three pages at the 500-event page limit. The stub
68-
// honors `until` (inclusive) and `limit` like a relay: newest first.
69-
const operations = Array.from({ length: 1_203 }, (_, index) =>
70-
makeAssignment(eventId(index + 1), 1_000_000 + index),
71-
);
64+
/**
65+
* Relay model matching production semantics (`filter_to_query_params` +
66+
* `filter_fully_pushable` in `crates/buzz-relay/src/handlers/req.rs`):
67+
*
68+
* 1. SQL applies kinds / `#e` / `since` / `until` (inclusive), orders
69+
* `(created_at DESC, id ASC)`, and cuts to `LIMIT` — clamped to 1,000.
70+
* 2. `#t` / `#a` are applied in Rust AFTER the SQL LIMIT.
71+
*
72+
* Step 2 is the trap the round-3 review caught: a filter that carries `#t`
73+
* gets the newest N kind-1 candidates first, then loses tag mismatches, so a
74+
* short page does NOT mean exhaustion. Any regression back to `#t`-reliant
75+
* fetching fails these tests.
76+
*/
77+
function makeRelayModel(events) {
7278
const calls = [];
7379
const fetchEvents = async (filter) => {
7480
calls.push(filter);
75-
assert.deepEqual(filter["#t"], ["assignment", "unassignment"]);
76-
const until = filter.until ?? Number.POSITIVE_INFINITY;
77-
return operations
78-
.filter((event) => event.created_at <= until)
79-
.sort((left, right) => right.created_at - left.created_at)
80-
.slice(0, filter.limit);
81+
const limit = Math.min(filter.limit ?? 1_000, 1_000);
82+
// SQL phase: pushed constraints only.
83+
const candidates = events
84+
.filter((event) => {
85+
if (filter.kinds && !filter.kinds.includes(event.kind)) return false;
86+
if (
87+
filter["#e"] &&
88+
!event.tags.some(
89+
(tag) => tag[0] === "e" && filter["#e"].includes(tag[1]),
90+
)
91+
) {
92+
return false;
93+
}
94+
if (filter.until !== undefined && event.created_at > filter.until) {
95+
return false;
96+
}
97+
if (filter.since !== undefined && event.created_at < filter.since) {
98+
return false;
99+
}
100+
return true;
101+
})
102+
.sort((left, right) =>
103+
right.created_at !== left.created_at
104+
? right.created_at - left.created_at
105+
: left.id < right.id
106+
? -1
107+
: 1,
108+
)
109+
.slice(0, limit);
110+
// Rust post-filter phase: tag constraints applied AFTER the LIMIT.
111+
return candidates.filter((event) => {
112+
if (
113+
filter["#t"] &&
114+
!event.tags.some(
115+
(tag) => tag[0] === "t" && filter["#t"].includes(tag[1]),
116+
)
117+
) {
118+
return false;
119+
}
120+
if (
121+
filter["#a"] &&
122+
!event.tags.some(
123+
(tag) => tag[0] === "a" && filter["#a"].includes(tag[1]),
124+
)
125+
) {
126+
return false;
127+
}
128+
return true;
129+
});
81130
};
131+
return { calls, fetchEvents };
132+
}
82133

83-
const events = await fetchAssignmentOperationEvents(
84-
[REPO_ADDRESS],
85-
fetchEvents,
134+
// ── fetchAssignmentOperationEvents vs. real relay query semantics ────────────
135+
136+
test("finds an assignment buried behind 600 newer comments on the real relay model", async () => {
137+
// The round-3 adversarial case: one old assignment, then 600 newer comments
138+
// on the same issue. A `#t`-carrying filter sees the newest 500 candidates
139+
// post-filtered to zero and falsely declares exhaustion (0/1). The
140+
// `#e`-keyed walk must return 1/1.
141+
const assignment = makeAssignment(eventId(9_999), 200);
142+
const comments = Array.from({ length: 600 }, (_, index) =>
143+
makeComment(eventId(index + 1), 1_000 + index),
86144
);
145+
const { calls, fetchEvents } = makeRelayModel([assignment, ...comments]);
146+
147+
const events = await fetchAssignmentOperationEvents([ISSUE_ID], fetchEvents);
148+
149+
assert.deepEqual(
150+
events.map((event) => event.id),
151+
[eventId(9_999)],
152+
"the buried assignment must survive the bounded page",
153+
);
154+
assert.ok(calls.length >= 2, "must page past the first bounded window");
155+
for (const filter of calls) {
156+
assert.equal(
157+
filter["#t"],
158+
undefined,
159+
"the filter must carry only SQL-pushed constraints — `#t` is post-filtered after LIMIT",
160+
);
161+
assert.equal(filter["#a"], undefined, "`#a` is post-filtered after LIMIT");
162+
}
163+
});
164+
165+
test("paginates to exhaustion across several full pages", async () => {
166+
// 1,203 operations spread across distinct seconds — three pages at the
167+
// 500-event window.
168+
const operations = Array.from({ length: 1_203 }, (_, index) =>
169+
makeAssignment(eventId(index + 1), 1_000_000 + index),
170+
);
171+
const { calls, fetchEvents } = makeRelayModel(operations);
172+
173+
const events = await fetchAssignmentOperationEvents([ISSUE_ID], fetchEvents);
87174

88175
assert.equal(events.length, 1_203, "every operation must be loaded");
89176
assert.ok(calls.length >= 3, "must page past the 500-event window");
90177
});
91178

92-
test("fetchAssignmentOperationEvents stops when a page adds nothing unseen", async () => {
93-
// A degenerate relay that always returns the same full page must not loop
94-
// forever: a page with zero unseen events terminates pagination.
95-
const page = Array.from({ length: 500 }, (_, index) =>
179+
test("escapes a second denser than one page by widening to the relay clamp", async () => {
180+
// 700 externally signed operations sharing one created_at second: an
181+
// inclusive `until` cursor alone can never advance past the first 500.
182+
// The loop must widen to the relay's 1,000-row clamp and load all 700.
183+
const operations = Array.from({ length: 700 }, (_, index) =>
96184
makeAssignment(eventId(index + 1), 1_000),
97185
);
98-
let callCount = 0;
99-
const fetchEvents = async () => {
100-
callCount += 1;
101-
return page;
102-
};
186+
const { fetchEvents } = makeRelayModel(operations);
103187

104-
const events = await fetchAssignmentOperationEvents(
105-
[REPO_ADDRESS],
106-
fetchEvents,
188+
const events = await fetchAssignmentOperationEvents([ISSUE_ID], fetchEvents);
189+
190+
assert.equal(events.length, 700, "same-second density must not drop events");
191+
});
192+
193+
test("reports a second denser than the relay clamp instead of silently dropping", async () => {
194+
// 1,100 events in one second exceeds the relay's 1,000-row page clamp —
195+
// unreachable through NIP-01 filters. That must surface as an error (the
196+
// caller shows a failed assignments section), never as silent loss.
197+
const operations = Array.from({ length: 1_100 }, (_, index) =>
198+
makeAssignment(eventId(index + 1), 1_000),
107199
);
200+
const { fetchEvents } = makeRelayModel(operations);
108201

109-
assert.equal(events.length, 500);
110-
assert.equal(callCount, 2, "second identical page must terminate the loop");
202+
await assert.rejects(
203+
fetchAssignmentOperationEvents([ISSUE_ID], fetchEvents),
204+
/full relay page/,
205+
);
111206
});
112207

113-
test("fetchAssignmentOperationEvents skips the relay for zero repositories", async () => {
208+
test("skips the relay for zero issues", async () => {
114209
const events = await fetchAssignmentOperationEvents([], async () => {
115210
throw new Error("must not query the relay");
116211
});
117212
assert.deepEqual(events, []);
118213
});
119214

215+
test("chunks large issue sets and dedupes operations across chunks", async () => {
216+
// 150 issues → two `#e` chunks at the 100-id chunk size.
217+
const issueIds = Array.from({ length: 150 }, (_, index) =>
218+
eventId(5_000 + index),
219+
);
220+
const operations = issueIds.map((issueId, index) =>
221+
makeAssignment(eventId(index + 1), 2_000 + index, issueId),
222+
);
223+
const { calls, fetchEvents } = makeRelayModel(operations);
224+
225+
const events = await fetchAssignmentOperationEvents(issueIds, fetchEvents);
226+
227+
assert.equal(events.length, 150);
228+
assert.equal(calls.length, 2, "150 issues must fan out as two #e chunks");
229+
assert.ok(calls.every((filter) => filter["#e"].length <= 100));
230+
});
231+
120232
test("mergeEventsById drops duplicates and keeps both sources", () => {
121233
const shared = makeAssignment(eventId(1), 10);
122234
const merged = mergeEventsById(
@@ -134,29 +246,16 @@ test("mergeEventsById drops duplicates and keeps both sources", () => {
134246
// The reduction in projectIssues.mjs is only as complete as the events it is
135247
// handed. The general comment fetch is bounded (2,000 shared across repos in
136248
// fetchProjectsWorkItems), so an old assignment operation can be evicted by
137-
// newer unrelated comments. The dedicated `#t`-filtered exhaustive query must
138-
// restore it.
249+
// newer unrelated comments. The dedicated issue-keyed exhaustive query must
250+
// restore it — against the real relay's query semantics.
139251

140-
test("an assignment older than 500+ newer comments still reduces to an assignee", async () => {
252+
test("an assignment older than 600 newer comments still reduces to an assignee", async () => {
141253
const issue = makeIssue();
142-
// Operation at t=200, then 600 newer comments that fill the bounded window.
143254
const assignment = makeAssignment(eventId(9_999), 200);
144255
const comments = Array.from({ length: 600 }, (_, index) =>
145-
makeComment(eventId(index + 1), 300 + index),
256+
makeComment(eventId(index + 1), 1_000 + index),
146257
);
147-
148-
const fetchEvents = async (filter) => {
149-
if (filter.kinds?.includes(1621)) return [issue];
150-
if (filter["#t"]) return [assignment];
151-
if (filter.kinds?.includes(1)) {
152-
// Bounded comment window: newest first, capped — the assignment
153-
// operation has been pushed out.
154-
return comments
155-
.sort((left, right) => right.created_at - left.created_at)
156-
.slice(0, 500);
157-
}
158-
return [];
159-
};
258+
const { fetchEvents } = makeRelayModel([issue, assignment, ...comments]);
160259

161260
const result = await fetchProjectsWorkItems(
162261
[{ repositories: [{ repoAddress: REPO_ADDRESS }] }],
@@ -175,7 +274,7 @@ test("a failed assignment query surfaces as a failed section instead of silent l
175274
const issue = makeIssue();
176275
const fetchEvents = async (filter) => {
177276
if (filter.kinds?.includes(1621)) return [issue];
178-
if (filter["#t"]) throw new Error("relay hiccup");
277+
if (filter["#e"]) throw new Error("relay hiccup");
179278
return [];
180279
};
181280

0 commit comments

Comments
 (0)