Skip to content

backends: pi keeps in-process copies of core's brain writers that production never runs #2523

Open

Description

@schlessera

Context

The pi backend still carries in-process copies of brain write operations that production never calls. This issue is the sole owner of deleting them: #1398 migrates only the internal imports that survive this deletion, and #1434 no longer repairs these writers.

  • BrainAccess.update is a near-verbatim copy of core's updateDocumentFields, and it has drifted:
    • The copy is update at packages/ui-backend-pi/src/brain-access.ts:283-344; core's original is updateDocumentFields at packages/core/src/lib/ops/write.ts:77-122.
    • The copy writes with plain writeFileSync (brain-access.ts:335). Core writes through writeFileSafely, a staged rename (packages/core/src/lib/ops/write.ts:118).
  • add and archive duplicate core too. BrainAccess.add (brain-access.ts:260) and archive (:346) duplicate core's addDocument (write.ts:42) and archiveWithReindex (write.ts:125).
  • None of them runs in production.
    • The worker always installs a bridge with applyBrain (packages/ui-backend-pi/src/worker-entry.ts:196-201, commented "No missing-route fallback may execute a legacy brain writer").
    • The routed application tools take the legacy body only when applyBrain is missing (if (!bridge?.applyBrain), packages/ui-backend-pi/src/tools.ts:872-874).
    • The only way to reach them is through the sessionFactory test seam.
    • The same holds for the local write_file/edit_file bodies when they are reached as the application-tool fallback. Those bodies themselves stay: with directWrites the routed list omits write/edit (tools.ts:858) and governed native writes run them (tools.ts:498, :535). Their atomic-write repair belongs to backends, modules: document writes outside core still truncate before writing #1434.

Tests that exercise brain_add/brain_update/brain_archive through a fake session therefore check code that will never write a user's brain. Deleting the writers first removes that part of #1398's migration, which is why #1398 is blocked by this issue.

Scope

  • Remove the legacy fallback from the routed application tools in tools.ts. With no applyBrain, they refuse with the existing "No authoritative server application route is available for this turn." error.
  • Delete BrainAccess.add, update and archive, the local execute paths of brain_add/brain_update/brain_archive that call them (tools.ts:601, :738, :818; the routed tool reads only legacy?.label from them, which must survive), and any helpers and @schlessera/brain/internal imports left unused afterwards (for example ingest, archiveDocument, updateDocument, relevanceOnArchive if nothing else uses them).
  • Point the affected tests at a fake applyBrain that records the request, so they assert what pi sends to the server route.

Out of scope

Acceptance criteria

  • grep -nE "async (add|update|archive)\(" packages/ui-backend-pi/src/brain-access.ts returns nothing.
  • grep -n "legacy.execute" packages/ui-backend-pi/src/tools.ts returns nothing.
  • A test calls brain_update with no applyBrain on the bridge and asserts the refusal, with no file written under a temporary fictional brain. It fails on the tree before the change, where the legacy writer edits the file.
  • Tests for brain_add, brain_update and brain_archive assert the exact applyBrain request (operation, path, expected base hash).
  • Governed native write_file/edit_file with directWrites still write through their own bodies, shown by an existing or added test.
  • The pi package suite and root tests/pi-worker-runtime.test.ts pass.

Verification

bun run test packages/ui-backend-pi
bun run test tests/pi-worker-runtime.test.ts
bun run typecheck

Contract impact

None. Production behaviour is unchanged, because the removed paths are unreachable there. A patch changeset is needed only if the @schlessera/brain-backend-pi declaration surface changes.

Found by a repository review on 2026-10-22.

2026-10-24: made this issue the sole owner of deleting pi's unused application writers (removed from #1398 and #1434), kept the native direct-write bodies out of scope, and refreshed citations to a6b945b.

Activity

  1. added
    type: refactor
    area: backends
    priority: p3
    agent-ready
    on Oct 21, 2026
  2. added
    priority: p2
    and removed
    priority: p3
    on Oct 23, 2026
  3. schlessera commented on Oct 23, 2026

    @schlessera
    OwnerAuthor

    2026-10-24 restructuring: raised to p2. It now solely owns deleting pi's unused application writers, and #1398 (p2) is sequenced after it, so the blocker must carry at least that priority.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: backendspriority: p2type: refactor

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions