GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Subagent sessions fail to project first message due to race in commitSyncEvent seq assignment (v1.16.2 and earlier) · Issue #31072 · anomalyco/opencode Closed as not planned Closed as not planned Copy link DescriptionTitleSubagent sessions fail to project first message due to race in SummaryWhen a subagent session is created, the first message event for that aggregate often fails to project. The result is that the subagent Root cause: The error does not bubble up: Reproduction (v1.16.2)On any v1.16.2 install that has been used with parallel subagents, count orphan subagent sessions: SELECT count(*) AS orphan_subagents FROM session WHERE parent_id IS NOT NULL AND id NOT IN (SELECT aggregate_id FROM event_sequence); In one real production DB (Windows, 466 sessions, 333 of which are subagents), this returned 331 — i.e. 99.4% of subagent sessions never got their first message projected. The two subagents that DID succeed ( Root cause// packages/core/src/event.ts (v1.16.2, around line 308) const row = yield* db .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id }) .from(EventSequenceTable) .where(eq(EventSequenceTable.aggregate_id, aggregateID)) .get() const latest = row?.seq ?? -1 // ... validation ... const seq = input?.seq ?? latest + 1 // <-- computed in JS, race window // ... commitGuards, projectors with `seq` ... yield* db .insert(EventSequenceTable) .values([{ aggregate_id: aggregateID, seq, ... }]) // <-- static value .onConflictDoUpdate({ target: EventSequenceTable.aggregate_id, set: { seq, ... }, // <-- sets to the same computed value, not +1 }) .run() Two concurrent writers can both read Suggested fixCollapse the read + compute + upsert into a single atomic statement that increments server-side. The fresh-publish path (the racing one) becomes: // Fresh publish: no caller-specified seq, so we need the server to // hand out the next seq atomically. const upserted = yield* db .insert(EventSequenceTable) .values([{ aggregate_id: aggregateID, seq: 0 }]) .onConflictDoUpdate({ target: EventSequenceTable.aggregate_id, set: { seq: sql`${EventSequenceTable.seq} + 1` }, }) .returning({ seq: EventSequenceTable.seq }) .get() .pipe(Effect.orDie) if (!upserted) { return yield* Effect.die(/* unexpected: upsert returned no row */) } const seq = upserted.seq The replay path (caller specifies
SQLite serializes the upsert at the statement level under WAL mode, so concurrent writers always receive distinct Impact
Proposed testA new test in it.effect("assigns unique seq to concurrent publishes for the same aggregate", () => Effect.gen(function* () { const events = yield* EventV2.Service const { db } = yield* Database.Service const aggregateID = EventV2.ID.create() yield* Effect.all( Array.from({ length: 10 }, (_, i) => events.publish(SyncMessage, { id: aggregateID, text: `concurrent-${i}` }), ), { concurrency: 10, discard: true }, ) const eventRows = yield* db .select({ seq: EventTable.seq }) .from(EventTable) .where(eq(EventTable.aggregate_id, aggregateID)) .all() const seqs = eventRows.map((r) => r.seq).sort((a, b) => a - b) expect(seqs).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) const seqRow = yield* db .select({ seq: EventSequenceTable.seq }) .from(EventSequenceTable) .where(eq(EventSequenceTable.aggregate_id, aggregateID)) .get() expect(seqRow?.seq).toBe(9) }), ) On the unpatched code, this test fails with one of: a Environment
Activity
MetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |