Skip to content

refactor: simplify session command dispatch#87

Open
ALX99 wants to merge 3 commits into
masterfrom
refactor/simplify-session-command-dispatch
Open

refactor: simplify session command dispatch#87
ALX99 wants to merge 3 commits into
masterfrom
refactor/simplify-session-command-dispatch

Conversation

@ALX99

@ALX99 ALX99 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Simplification

Inline the new_session / switch_session bridge dispatch flow and remove the private handleSessionCommand() helper.

Why the current design is overcomplicated

handleSessionCommand() had exactly one caller: the adjacent new_session / switch_session branch inside Bridge.dispatch(). The helper did not provide a separate abstraction boundary, extension point, reuse point, or independently testable behavior. It also forced reviewers to jump between two private methods to understand one command dispatch path.

The session command flow is small and depends directly on dispatch-local context: the parsed command, the originating client sender, runtime session replacement, cancellation handling, rebinding, and the final response. Keeping it inline makes the full branch behavior visible in one place.

Behavioral contract

The following behavior must remain unchanged:

  • new_session still calls runtime.newSession().
  • switch_session still calls runtime.switchSession(cmd.path).
  • Cancelled session changes still respond with { type: "response", id, ok: false, error: "cancelled by extension" }.
  • Successful session changes still call rebind() before responding success.
  • Successful session changes still respond with { type: "response", id, ok: true }.
  • The command parser, invalid-command response, exception-to-error-response handling, prompt/abort/model/thinking/state/session-list/model-list dispatch, client fan-out, subscriptions, disposal, wire format, runtime interface, and session persistence behavior are unchanged.
  • No public API, stored data, configuration contract, logging, metrics, or user-visible behavior changes.

Before and after

Before:

  • Bridge.dispatch() grouped new_session and switch_session, then delegated to this.handleSessionCommand(cmd, replyTo).
  • handleSessionCommand() immediately performed the runtime call, cancellation response, rebind(), and success response.
  • The private helper was not called anywhere else.

After:

  • Bridge.dispatch() handles the grouped new_session / switch_session branch directly.
  • The same runtime call selection, cancellation response, rebind(), and success response remain in the same order.
  • The private handleSessionCommand() method is removed.

Specific evidence:

  • Removed one private single-caller method from pi-web/src/bridge.ts.
  • Removed one extra call boundary in the session command dispatch path.
  • Kept the existing grouped branch for new_session and switch_session.
  • The diff is limited to pi-web/src/bridge.ts and is 1 commit ahead of master with no unrelated files changed.

Validation

Static validation performed in this connector-limited run:

  • Checked open PRs first to avoid overlapping current simplification or bug-fix work.
  • Searched open issues for this specific bridge/session simplification and found no matching open issue.
  • Inspected pi-web/src/bridge.ts before editing and verified handleSessionCommand() had exactly one caller.
  • Compared master...refactor/simplify-session-command-dispatch: result is 1 commit ahead, 0 behind, with only pi-web/src/bridge.ts modified.
  • Verified the inlined branch preserves the previous runtime call selection, cancellation response, rebind() ordering, and success response text/shape.

Commands not run in this automation environment:

  • cd pi-web && npm run build
  • cd pi-web && npm run typecheck
  • cd pi-web && npm test

Existing tests expected to cover this behavior:

  • pi-web/tests/bridge.test.ts covers command dispatch, session rebinding, cancellation paths, response behavior, and client behavior.

Risk assessment

Risk is low because this removes only a private single-caller helper and keeps the same code path, response payloads, runtime calls, and ordering. The behavior-sensitive part is session replacement: cancellation must avoid rebinding and success must rebind before returning success. Both branches are preserved exactly in the inlined flow.

Behavior-sensitive areas examined:

  • new_session vs switch_session runtime call selection,
  • cancellation response shape,
  • rebind() ordering,
  • success response shape,
  • exception handling through the outer handleCommand() try/catch,
  • surrounding command dispatch branches.

Scope

Intentionally not simplified:

  • Other Bridge.dispatch() branches, because they are behavior-dense and already readable.
  • respond() and broadcast helpers, because they are reused side-effect boundaries.
  • Runtime/session interfaces, because changing them would affect the bridge boundary.
  • Open PR refactor: simplify web event guard #82 (pi-web/src/web/bridge.ts) and open PR refactor: simplify agent discovery result handling #74 (subagent discovery), to avoid overlapping work.
  • Shell/profile dotfiles, because behavior preservation is harder to prove without local environment validation.

@ALX99 ALX99 force-pushed the master branch 4 times, most recently from 5cf571d to dd748db Compare July 10, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant