Skip to content

client: delayed form hydration removes a newly received form #56726

Open
RealBhupesh/opencode
#1

Description

@521ox

client: delayed form hydration removes a newly received form

Summary

A delayed session-form list response can overwrite a newer form.created event in createData. A form present in the real client cache disappears when the older empty snapshot commits.

This is specifically the form-cache hydration path. Permission hydration is already tracked by the open, unmerged PR #52074 and is not a new issue in this report.

Environment

  • Windows build 22631; Bun 1.4.2.
  • Executed checkout: custom fork at 72d0dce8ea437ea061e0680110fa19a81aa118fd, client package 2.0.32.
  • Source comparison: the affected behavior also exists at official 63c1a27af5a391d9caa55b82694f496911e1859b. Comparison with dev 60a5a6c found only three AI-file changes and no client changes. Those official revisions were not dynamically tested.
  • Headless Bun test with --conditions=browser; real createData and Promise client, injected in-memory fetch, no plugins or real service involved.

Reproduction

Use the local deferred-fetch/listener fixture pattern from packages/client/test/solid-inbox-race.test.ts:15-35 in a scratch test. It constructs a real createData with an injected Promise-client fetch and an event-listener set; it does not replace the reducer. The helper is not exported, so do not import the test file itself.

Create an existing remembered session and this form:

const form = {
  id: "frm_hydration_audit",
  sessionID,
  title: "Fixture question",
  metadata: { kind: "question" },
  fields: [{ type: "string", key: "answer", title: "Answer", required: true }],
}

Then use this ordering:

  1. Call data.session.form.sync(sessionID).
  2. In the injected fetch, wait until GET /api/session/:sessionID/form arrives, then hold its old empty response with Promise.withResolvers<Response>().
  3. Emit a complete form.created event through the fixture listener, with a unique event ID, created, location: { directory: "/project" }, and data: { form }.
  4. Confirm data.session.form.list(sessionID) contains frm_hydration_audit.
  5. Resolve the held response with Response.json({ data: [] }) and await the original sync.
  6. Read data.session.form.list(sessionID) again. The expected assertion that the form remains present fails.

Control: resolve and await the same empty list response before emitting form.created. The form then remains in the cache. This non-interleaved control passed, validating both hydration and event delivery.

Expected Behavior

A snapshot taken before form.created must not erase that newer form from the client projection. Initial hydration and live form events must converge without requiring an unrelated disconnect.

Actual Behavior

Checkpoint Form IDs in the real client cache
After form.created, before releasing HTTP ["frm_hydration_audit"]
After the old empty HTTP response commits []

The interleaved case used one form GET, with no fixture errors. The non-interleaved control retains the same form. No extra sync was added to repair the observation.

Additional Context

Source references below use checkout 72d0dce:

  • packages/client/src/solid/data.ts:1288-1293: form.created appends the new form.
  • packages/client/src/solid/data.ts:1841-1861: form hydration publishes its response without reconciling form events received during the read. This reproduction exercises the session-owned branch at line 1860, not global forms.
  • packages/client/src/solid/data.ts:1220-1223: replied/cancelled forms also have live removal handlers, which should be considered when defining the hydration reconciliation semantics; no separate form-resurrection result is claimed here.
  • Official 63c1a27 has the session-form replacement at packages/client/src/solid/data.ts:1850.

Related: PR #52074. Its patch adds permissionUpdates for permission hydration, but does not add corresponding form-update reconciliation. This draft reports the independently owned form path only; it does not reopen permission hydration as a new issue.

The experiment confirms loss from the client projection. It did not run a rendered form dock or a real server execution, so a real execution stall is not claimed as measured. Subsequent reconnect/revalidation behavior and production frequency were not tested.

Suggested solution — not implemented

Reconcile form.created and settlement events with in-flight form-list hydration at the data owner, or use a consistent read/version boundary with a bounded revalidation path. Reuse the event-precedence approach already present for inbox/activity, and the related permission work where appropriate. Preserve successful initial hydration: dropping or aborting every overlapping read alone can leave the cache unhydrated. No additional synchronization framework is needed for this proposal.

Activity

  1. opencode-agent commented on Oct 18, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report. I reproduced this on the latest dev (60a5a6c); the client form code is the same there as in v2.0.32.

    The script below is a headless Bun test that uses the real createData and Promise client with an injected fetch:

    1. Call data.session.form.sync(sessionID) and hold the GET /api/session/:id/form response.
    2. Emit form.created for frm_hydration_audit. form.list(sessionID) now shows ["frm_hydration_audit"].
    3. Release the held { data: [] } response. form.list(sessionID) becomes [].

    The control case passes: when the empty response arrives before form.created, the form stays. The session branch of session.form.sync replaces the cached list with the response and doesn't merge in form events received during the read.

    Related:

    Reproduction script (form-hydration-race.repro.test.ts)
    // Reproduction for anomalyco/opencode#56726
    // Run from packages/client: bun test --conditions=browser test/form-hydration-race.repro.test.ts
    import { expect, test } from "bun:test"
    import { createRoot } from "solid-js"
    import { createData, type CreateDataInput } from "../src/solid"
    import { OpenCode, type OpenCodeEvent } from "../src/promise"
    
    function fixture(fetch: (request: Request) => Promise<Response>) {
      const listeners = new Set<Parameters<CreateDataInput["event"]["listen"]>[0]>()
      const api = OpenCode.make({
        baseUrl: "http://opencode.local",
        fetch: (input, init) => fetch(input instanceof Request ? input : new Request(input, init)),
      })
      return createRoot((dispose) => ({
        data: createData({
          api: () => api,
          directory: "/project",
          event: {
            on: () => () => {},
            listen(handler) {
              listeners.add(handler)
              return () => listeners.delete(handler)
            },
          },
        }),
        emit: (event: OpenCodeEvent) => listeners.forEach((listener) => listener({ name: event.type, details: event })),
        dispose,
      }))
    }
    
    const sessionID = "ses_form_race"
    const form = {
      id: "frm_hydration_audit",
      sessionID,
      title: "Fixture question",
      metadata: { kind: "question" },
      fields: [{ type: "string", key: "answer", title: "Answer", required: true }],
    }
    const created = {
      id: "evt_form_created",
      type: "form.created",
      created: 1,
      location: { directory: "/project" },
      data: { form },
    } as unknown as OpenCodeEvent
    
    const ids = (setup: ReturnType<typeof fixture>) => (setup.data.session.form.list(sessionID) ?? []).map((f) => f.id)
    
    test("control: form.created after the list response is kept", async () => {
      const setup = fixture(async () => Response.json({ data: [] }))
      try {
        await setup.data.session.form.sync(sessionID)
        setup.emit(created)
        expect(ids(setup)).toEqual([form.id])
      } finally {
        setup.dispose()
      }
    })
    
    test("a delayed empty form list must not erase a form.created received during the read", async () => {
      const response = Promise.withResolvers<Response>()
      const requested = Promise.withResolvers<string>()
      const setup = fixture((request) => {
        requested.resolve(`${request.method} ${new URL(request.url).pathname}`)
        return response.promise
      })
      try {
        const reading = setup.data.session.form.sync(sessionID)
        console.log("request:", await requested.promise)
        setup.emit(created)
        console.log("after form.created:", ids(setup))
        expect(ids(setup)).toEqual([form.id])
        response.resolve(Response.json({ data: [] }))
        await reading
        console.log("after delayed list response:", ids(setup))
        expect(ids(setup)).toEqual([form.id])
      } finally {
        setup.dispose()
      }
    })

    Run it: bun form-hydration-race.repro.test.ts

  2. added a commit that references this issue on Oct 18, 2026
    ca72246
  3. vladyslavarkavenko commented on Oct 18, 2026

    @vladyslavarkavenko

    I opened #56876 for this. It records form events while the session form list is loading and applies them to the response, the same way inbox hydration already works. The tests cover a form created during the read, and a form replied to or cancelled during the read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions