Skip to content

An explicit edit session owns a focused node's draft and caret, replacing the timer-arbitrated editor bridge #4380

Not planned

Description

@malibio

Overview

The outline text editor is where the frontend's effect-related races live. While a node is focused, its text has two owners, the store and the textarea, with no explicit handoff between them, and the caret is a one-shot command held in global reactive state. Both are arbitrated by flags and timers. This issue replaces that with an explicit edit session that owns the draft and the selection for as long as a node is being edited.

This is a design decision, not a routine fix. The design below is a proposal from a code-reading review. It is not yet recorded in an ADR, and the first step of the work is to write that ADR and have it agreed. Findings were not reproduced by running the app; line numbers must be re-checked.

Problem Statement

Two owners of content. The store pushes content into the textarea from an effect (textarea-controller.svelte.ts, around line 1291). Whether the push is applied rests on three guards in updateContent (around lines 435-468): value equality, a recentEnter flag cleared by a 100 ms timer, and a document.activeElement check. The docs disagree about which side is the source of truth: component-architecture.md says the textarea, frontend-state-and-persistence.md says the store.

The caret is a command stored as state. focusManager.cursorPosition (focus-manager.svelte.ts) is one module-level signal with six command types. It is read in the controller's constructor and initialize() (both inside effects), applied by actions/position-cursor.ts after await tick(), cleared in a requestAnimationFrame (controller, around line 378), re-applied by a 10 ms setTimeout retry (position-cursor.ts, around lines 177 and 197), and doubles as a blur-suppression flag (base-node.svelte, around lines 588-594). Comments in the controller record the failures this produced: another node consuming the signal in the same flush, and scrambled text such as "Hello" becoming "lloHe".

The focused textarea is destroyed and recreated mid-typing. The textarea exists only while a node is being edited, and the row is keyed on node id plus node type (node-row.svelte:98,136), so typing a heading marker remounts the focused textarea. recentEnter (100 ms), justCreated (50 ms) and the node-type-conversion and inherited-type cursor types exist to carry a caret across that remount.

Effect ordering. The effect that initializes the controller (base-node.svelte, around line 737) is a silent no-op unless the effect that creates it (textarea-controller.svelte.ts, around line 1255) has already run. It holds through source order. Inferred, not reproduced.

Store writes issued from reactive contexts. Pattern detection raises nodeTypeConversionDetected from inside an effect (hence untrack around lines 1060 and 1082 of the controller), and placeholder promotion is deferred with tick() in four handlers of base-node-viewer.svelte, each guarded by an isPromoting flag. Between the flag being set and the tick there is an async gap with two writers.

Proposed Solution

  • An EditSession { nodeId, paneId, baseContent, draft, selection } owned by the focus manager, opened when a node enters editing in a pane and closed when it leaves.
  • While a session is open the textarea is uncontrolled and the session owns the draft. Each input updates the draft and writes to the store.
  • The one store-to-textarea push is gated on "no session for this node in this pane", not on timers or activeElement.
  • A remote content change during a session applies when draft === baseContent; otherwise it raises the conflict indicator.
  • The caret is state: session.selection, kept current from selection events. A remounted textarea reads it synchronously on mount. No clear timing, no retry, no blur-suppression cursor type.
  • Decide whether the row stops being keyed on node type, so the type wrapper changes around a stable textarea and nothing has to carry a caret across a remount.
  • Type conversion and placeholder promotion are issued from event handlers or store methods, not from effects.

Documentation

  • ADR-108 Decision items 1-3 and "Negative / trade-offs accepted" - the plain textarea, editing and viewing as separate elements, the focus manager as the one place that knows which node is edited, and the accepted cost of carrying the caret across the edit/view switch "through ... a deferred focus".
  • ADR-049 Decision item 4 - effects push state out to the DOM and do nothing else; imperative sequencing belongs in an event handler or a store method.
  • ADR-023 - placeholder nodes during editing, which promotion must keep to.
  • Keyboard and Navigation - the caret rules for Enter, Backspace, arrows and type conversion that must keep working.
  • Frontend State & Persistence - "Race Conditions & Mitigations", several of which this replaces.
  • Updates: a new ADR for the edit session (below), component-architecture.md and frontend-state-and-persistence.md (one statement of who owns content while editing), keyboard-and-navigation.md.

Architecture Impact

  • Extends ADR-108. ADR-108 says the textarea value is the single source of truth for a node's text while it is edited, and accepts a deferred focus to carry the caret across the edit/view switch. The session keeps the textarea as the editing surface but makes the handoff with the store explicit and turns the caret from a deferred command into state. If rows stop remounting on type change, ADR-108's trade-off about type conversion carrying the caret no longer applies and must be amended.
  • Implements ADR-049 item 4 for the editor bridge.
  • A companion nodespace-docs change is required and comes first: a new ADR recording the edit session (ownership while editing, the remote-update rule, caret as state, the row-key decision), pushed to nodespace-docs main before implementation starts, plus the doc updates listed above in the same change as the code.

Acceptance Criteria

  • An ADR for the edit session is written, agreed and on nodespace-docs main before implementation
  • One object holds the draft and selection for a node being edited in a pane; nothing else holds a copy of either
  • The store-to-textarea push is gated on the absence of a session, with no timer and no document.activeElement check
  • recentEnter, justCreated, the requestAnimationFrame clear and the 10 ms retry are deleted
  • focusManager.cursorPosition as a consumed one-shot command is gone; the caret is read from session state on mount
  • Controller creation and initialization do not depend on the order of two effects
  • No effect raises an event or calls a method that writes the store; the tick() deferrals and the isPromoting flag are gone or justified one by one in the ADR
  • The row-key decision is made and recorded; if rows no longer remount on type change, the node-type-conversion and inherited-type cursor types are deleted
  • Browser tests cover: Enter splitting a node keeps the caret; typing a heading marker keeps the caret and typed text in order; Backspace merge places the caret at the join; arrow navigation between nodes; a remote update to the focused node with and without a local draft; the same node edited in one pane and shown in another
  • component-architecture.md and frontend-state-and-persistence.md agree on who owns content while a node is edited
  • bun run test:changed and bun run test:browser pass; bun run quality:fix is clean

Technical Specifications

Files to Modify

  • packages/desktop-app/src/lib/services/focus-manager.svelte.ts
  • packages/desktop-app/src/lib/actions/position-cursor.ts
  • packages/desktop-app/src/lib/design/components/textarea-controller.svelte.ts
  • packages/desktop-app/src/lib/design/components/base-node.svelte
  • packages/desktop-app/src/lib/design/components/base-node-viewer.svelte
  • packages/desktop-app/src/lib/design/components/node-row.svelte
  • packages/desktop-app/src/lib/commands/keyboard/create-node.command.ts, navigate-up.command.ts, navigate-down.command.ts

Implementation Details

  • Focus and selection are real-browser behaviour: test with bun run test:browser, not Happy-DOM.
  • The remote-update rule interacts with how the store treats updates to a node being edited (it currently reads focusManager and drops them). Keep the two consistent; the store-side change is its own issue.
  • No timing assertions in correctness tests.
  • No compatibility path: delete the old caret command types and flags in the same change.

Dependencies

Related Issues

Non-Goals

  • Splitting the store's confirmed and optimistic state.
  • Replacing the textarea with another editor.

Activity

  1. malibio commented on Nov 28, 2026

    @malibio
    CollaboratorAuthor

    Moved to NodeSpace: the task "An explicit edit session owns a focused node's draft and caret, replacing the timer-arbitrated editor bridge" (bd285a37-31fd-4c94-90cc-516392015a2e) in the NodeSpace workspace. The team's work is tracked there, so this GitHub copy is closed to keep its status in one place.

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