Stabilize runtime orchestration and fix flaky CI tests (#488)
Co-authored-by: Claude Opus 4.6 <[email protected]> Co-authored-by: codex <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
47 files changed
+2259
-628
No files matched your search
+125
-3
@@ -5,17 +5,139 @@ T3 Code runs as a **Node.js WebSocket server** that wraps `codex app-server` (JS
|
||||
```
|
||||
┌─────────────────────────────────┐
|
||||
│ Browser (React + Vite) │
|
||||
│ Connected via WebSocket │
|
||||
│ wsTransport (state machine) │
|
||||
│ Typed push decode at boundary │
|
||||
└──────────┬──────────────────────┘
|
||||
│ ws://localhost:3773
|
||||
┌──────────▼──────────────────────┐
|
||||
│ apps/server (Node.js) │
|
||||
│ WebSocket + HTTP static server │
|
||||
│ ProviderManager │
|
||||
│ CodexAppServerManager │
|
||||
│ ServerPushBus (ordered pushes) │
|
||||
│ ServerReadiness (startup gate) │
|
||||
│ OrchestrationEngine │
|
||||
│ ProviderService │
|
||||
│ CheckpointReactor │
|
||||
│ RuntimeReceiptBus │
|
||||
└──────────┬──────────────────────┘
|
||||
│ JSON-RPC over stdio
|
||||
┌──────────▼──────────────────────┐
|
||||
│ codex app-server │
|
||||
└─────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Components
|
||||
|
||||
- **Browser app**: The React app renders session state, owns the client-side WebSocket transport, and treats typed push events as the boundary between server runtime details and UI state.
|
||||
|
||||
- **Server**: `apps/server` is the main coordinator. It serves the web app, accepts WebSocket requests, waits for startup readiness before welcoming clients, and sends all outbound pushes through a single ordered push path.
|
||||
|
||||
- **Provider runtime**: `codex app-server` does the actual provider/session work. The server talks to it over JSON-RPC on stdio and translates those runtime events into the app's orchestration model.
|
||||
|
||||
- **Background workers**: Long-running async flows such as runtime ingestion, command reaction, and checkpoint processing run as queue-backed workers. This keeps work ordered, reduces timing races, and gives tests a deterministic way to wait for the system to go idle.
|
||||
|
||||
- **Runtime signals**: The server emits lightweight typed receipts when important async milestones finish, such as checkpoint capture, diff finalization, or a turn becoming fully quiescent. Tests and orchestration code wait on these signals instead of polling internal state.
|
||||
|
||||
## Event Lifecycle
|
||||
|
||||
### Startup and client connect
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Browser
|
||||
participant Transport as WsTransport
|
||||
participant Server as wsServer
|
||||
participant Layers as serverLayers
|
||||
participant Ready as ServerReadiness
|
||||
participant Push as ServerPushBus
|
||||
|
||||
Browser->>Transport: Load app and open WebSocket
|
||||
Transport->>Server: Connect
|
||||
Server->>Layers: Start runtime services
|
||||
Server->>Ready: Wait for startup barriers
|
||||
Ready-->>Server: Ready
|
||||
Server->>Push: Publish server.welcome
|
||||
Push-->>Transport: Ordered welcome push
|
||||
Transport-->>Browser: Hydrate initial state
|
||||
```
|
||||
|
||||
1. The browser boots [`WsTransport`][1] and registers typed listeners in [`wsNativeApi`][2].
|
||||
2. The server accepts the connection in [`wsServer`][3] and brings up the runtime graph defined in [`serverLayers`][7].
|
||||
3. [`ServerReadiness`][4] waits until the key startup barriers are complete.
|
||||
4. Once the server is ready, [`wsServer`][3] sends `server.welcome` from the contracts in [`ws.ts`][6] through [`ServerPushBus`][5].
|
||||
5. The browser receives that ordered push through [`WsTransport`][1], and [`wsNativeApi`][2] uses it to seed local client state.
|
||||
|
||||
### User turn flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Browser
|
||||
participant Transport as WsTransport
|
||||
participant Server as wsServer
|
||||
participant Provider as ProviderService
|
||||
participant Codex as codex app-server
|
||||
participant Ingest as ProviderRuntimeIngestion
|
||||
participant Engine as OrchestrationEngine
|
||||
participant Push as ServerPushBus
|
||||
|
||||
Browser->>Transport: Send user action
|
||||
Transport->>Server: Typed WebSocket request
|
||||
Server->>Provider: Route request
|
||||
Provider->>Codex: JSON-RPC over stdio
|
||||
Codex-->>Ingest: Provider runtime events
|
||||
Ingest->>Engine: Normalize into orchestration events
|
||||
Engine-->>Server: Domain events
|
||||
Server->>Push: Publish orchestration.domainEvent
|
||||
Push-->>Browser: Typed push
|
||||
```
|
||||
|
||||
1. A user action in the browser becomes a typed request through [`WsTransport`][1] and the browser API layer in [`nativeApi`][12].
|
||||
2. [`wsServer`][3] decodes that request using the shared WebSocket contracts in [`ws.ts`][6] and routes it to the right service.
|
||||
3. [`ProviderService`][8] starts or resumes a session and talks to `codex app-server` over JSON-RPC on stdio.
|
||||
4. Provider-native events are pulled back into the server by [`ProviderRuntimeIngestion`][9], which converts them into orchestration events.
|
||||
5. [`OrchestrationEngine`][10] persists those events, updates the read model, and exposes them as domain events.
|
||||
6. [`wsServer`][3] pushes those updates to the browser through [`ServerPushBus`][5] on channels defined in [`orchestration.ts`][11].
|
||||
|
||||
### Async completion flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Server as wsServer
|
||||
participant Worker as Queue-backed workers
|
||||
participant Cmd as ProviderCommandReactor
|
||||
participant Checkpoint as CheckpointReactor
|
||||
participant Receipt as RuntimeReceiptBus
|
||||
participant Push as ServerPushBus
|
||||
participant Browser
|
||||
|
||||
Server->>Worker: Enqueue follow-up work
|
||||
Worker->>Cmd: Process provider commands
|
||||
Worker->>Checkpoint: Process checkpoint tasks
|
||||
Checkpoint->>Receipt: Publish completion receipt
|
||||
Cmd-->>Server: Produce orchestration changes
|
||||
Checkpoint-->>Server: Produce orchestration changes
|
||||
Server->>Push: Publish resulting state updates
|
||||
Push-->>Browser: User-visible push
|
||||
```
|
||||
|
||||
1. Some work continues after the initial request returns, especially in [`ProviderRuntimeIngestion`][9], [`ProviderCommandReactor`][13], and [`CheckpointReactor`][14].
|
||||
2. These flows run as queue-backed workers using [`DrainableWorker`][16], which helps keep side effects ordered and test synchronization deterministic.
|
||||
3. When a milestone completes, the server emits a typed receipt on [`RuntimeReceiptBus`][15], such as checkpoint completion or turn quiescence.
|
||||
4. Tests and orchestration code wait on those receipts instead of polling git state, projections, or timers.
|
||||
5. Any user-visible state changes produced by that async work still go back through [`wsServer`][3] and [`ServerPushBus`][5].
|
||||
|
||||
[1]: ../apps/web/src/wsTransport.ts
|
||||
[2]: ../apps/web/src/wsNativeApi.ts
|
||||
[3]: ../apps/server/src/wsServer.ts
|
||||
[4]: ../apps/server/src/wsServer/readiness.ts
|
||||
[5]: ../apps/server/src/wsServer/pushBus.ts
|
||||
[6]: ../packages/contracts/src/ws.ts
|
||||
[7]: ../apps/server/src/serverLayers.ts
|
||||
[8]: ../apps/server/src/provider/Layers/ProviderService.ts
|
||||
[9]: ../apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
|
||||
[10]: ../apps/server/src/orchestration/Layers/OrchestrationEngine.ts
|
||||
[11]: ../packages/contracts/src/orchestration.ts
|
||||
[12]: ../apps/web/src/nativeApi.ts
|
||||
[13]: ../apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
|
||||
[14]: ../apps/server/src/orchestration/Layers/CheckpointReactor.ts
|
||||
[15]: ../apps/server/src/orchestration/Layers/RuntimeReceiptBus.ts
|
||||
[16]: ../packages/shared/src/DrainableWorker.ts
|
||||
@@ -3,7 +3,9 @@
|
||||
The web app communicates with the server via WebSocket using a simple JSON-RPC-style protocol:
|
||||
|
||||
- **Request/Response**: `{ id, method, params }` → `{ id, result }` or `{ id, error }`
|
||||
- **Push events**: `{ type: "push", channel, data }` for orchestration read-model updates
|
||||
- **Push events**: typed envelopes with `channel`, `sequence` (monotonic per connection), and channel-specific `data`
|
||||
|
||||
Push channels: `server.welcome`, `server.configUpdated`, `terminal.event`, `orchestration.domainEvent`. Payloads are schema-validated at the transport boundary (`wsTransport.ts`). Decode failures produce structured `WsDecodeDiagnostic` with `code`, `reason`, and path info.
|
||||
|
||||
Methods mirror the `NativeApi` interface defined in `@t3tools/contracts`:
|
||||
|
||||
@@ -12,3 +14,17 @@ Methods mirror the `NativeApi` interface defined in `@t3tools/contracts`:
|
||||
- `shell.openInEditor`, `server.getConfig`
|
||||
|
||||
Codex is the only implemented provider. `claudeCode` is reserved in contracts/UI.
|
||||
|
||||
## Client transport
|
||||
|
||||
`wsTransport.ts` manages connection state: `connecting` → `open` → `reconnecting` → `closed` → `disposed`. Outbound requests are queued while disconnected and flushed on reconnect. Inbound pushes are decoded and validated at the boundary, then cached per channel. Subscribers can opt into `replayLatest` to receive the last push on subscribe.
|
||||
|
||||
## Server-side orchestration layers
|
||||
|
||||
Provider runtime events flow through queue-based workers:
|
||||
|
||||
1. **ProviderRuntimeIngestion** — consumes provider runtime streams, emits orchestration commands
|
||||
2. **ProviderCommandReactor** — reacts to orchestration intent events, dispatches provider calls
|
||||
3. **CheckpointReactor** — captures git checkpoints on turn start/complete, publishes runtime receipts
|
||||
|
||||
All three use `DrainableWorker` internally and expose `drain()` for deterministic test synchronization.
|
||||
@@ -3,4 +3,5 @@
|
||||
- `/apps/server`: Node.js WebSocket server. Wraps Codex app-server, serves the built web app, and opens the browser on start.
|
||||
- `/apps/web`: React + Vite UI. Session control, conversation, and provider event rendering. Connects to the server via WebSocket.
|
||||
- `/apps/desktop`: Electron shell. Spawns a desktop-scoped `t3` backend process and loads the shared web app.
|
||||
- `/packages/contracts`: Shared Zod schemas and TypeScript contracts for provider events, WebSocket protocol, and model/session types.
|
||||
- `/packages/contracts`: Shared effect/Schema schemas and TypeScript contracts for provider events, WebSocket protocol, and model/session types.
|
||||
- `/packages/shared`: Shared runtime utilities consumed by both server and web. Uses explicit subpath exports (e.g. `@t3tools/shared/git`, `@t3tools/shared/DrainableWorker`) — no barrel index.
|
||||
@@ -0,0 +1,3 @@
|
||||
[tools]
|
||||
node = "24.13.1"
|
||||
bun = "1.3.9"
|
||||
@@ -0,0 +1,109 @@
|
||||
# Plan: Provider-Neutral Runtime Determinism and Flake Elimination
|
||||
|
||||
## Summary
|
||||
Replace timing-sensitive websocket and orchestration behavior with explicit typed runtime boundaries, ordered push delivery, and server-owned completion receipts. The cutover is broad and single-shot: no compatibility shim, no mixed old/new transport. The design must reduce flakes without baking Codex-specific lifecycle semantics into generic runtime code.
|
||||
|
||||
## Implementation Status
|
||||
|
||||
All 7 sections are implemented. CI passes (format, lint, typecheck, test, browser test, build). One deferred item remains: the shared `WsTestClient` helper from section 7 — tests use direct transport subscription and receipt-based waits instead.
|
||||
|
||||
### New files
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `packages/shared/src/DrainableWorker.ts` | Queue-based Effect worker with deterministic `drain` signal |
|
||||
| `packages/shared/src/schemaJson.ts` | Two-phase JSON→Schema decode helpers (`decodeJsonResult`, `formatSchemaError`) |
|
||||
| `apps/server/src/wsServer/pushBus.ts` | `ServerPushBus` — ordered typed push pipeline with auto-incrementing sequence |
|
||||
| `apps/server/src/wsServer/readiness.ts` | `ServerReadiness` — Deferred-based barriers for startup sequencing |
|
||||
| `apps/server/src/orchestration/Services/RuntimeReceiptBus.ts` | Receipt schema union: checkpoint captured, diff finalized, turn quiesced |
|
||||
| `apps/server/src/orchestration/Layers/RuntimeReceiptBus.ts` | PubSub-backed receipt bus implementation |
|
||||
| `apps/server/src/watchFileWithStatPolling.ts` | Stat-polling file watcher for containers where `fs.watch` is unreliable |
|
||||
| `apps/server/vitest.config.ts` | Server-specific test config (timeout bumps) |
|
||||
| `apps/server/src/wsServer/pushBus.test.ts` | Push bus serialization and welcome-gating tests |
|
||||
| `packages/shared/src/DrainableWorker.test.ts` | Drainable worker enqueue/drain lifecycle tests |
|
||||
|
||||
### Key modifications
|
||||
|
||||
| File | Change |
|
||||
|------|--------|
|
||||
| `packages/contracts/src/ws.ts` | Channel-indexed `WsPushPayloadByChannel` map, `WsPush` union schema, `WsPushSequence` |
|
||||
| `apps/server/src/wsServer.ts` | Integrated `ServerPushBus` and `ServerReadiness`; welcome gated on readiness |
|
||||
| `apps/server/src/keybindings.ts` | Explicit runtime with `start`/`ready`/`snapshot`; dual `fs.watch` + stat-polling watcher |
|
||||
| `apps/web/src/wsTransport.ts` | Connection state machine (`connecting`→`open`→`reconnecting`→`closed`→`disposed`); two-phase decode at boundary; cached latest push by channel |
|
||||
| `apps/web/src/wsNativeApi.ts` | Removed decode logic; delegates to pre-validated transport messages |
|
||||
| `apps/server/src/orchestration/Layers/CheckpointReactor.ts` | Uses `DrainableWorker`; publishes completion receipts |
|
||||
| `apps/server/src/orchestration/Layers/ProviderCommandReactor.ts` | Uses `DrainableWorker` for command processing |
|
||||
| `apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts` | Uses `DrainableWorker` for event ingestion |
|
||||
| `apps/server/integration/OrchestrationEngineHarness.integration.ts` | Receipt-based waits replace polling loops |
|
||||
|
||||
## Key Changes
|
||||
### 1. Strengthen the generic boundaries, not the Codex boundary — DONE
|
||||
- `ProviderRuntimeEvent` remains the canonical provider event contract; `ProviderService` remains the only cross-provider facade.
|
||||
- Raw Codex payloads and event ordering stay isolated in `CodexAdapter.ts` and `codexAppServerManager.ts`.
|
||||
- `ProviderKind` was not expanded. The runtime stays provider-neutral by contract.
|
||||
|
||||
### 2. Replace loose websocket envelopes with channel-indexed typed pushes — DONE
|
||||
- `packages/contracts/src/ws.ts` now derives push messages from a `WsPushPayloadByChannel` channel-to-schema map. `WsPush` is a union schema replacing `channel: string` + `data: unknown`.
|
||||
- Every server push carries `sequence: number`, auto-incremented in `ServerPushBus`.
|
||||
- `packages/shared/src/schemaJson.ts` provides structured decode diagnostics via `formatSchemaError`.
|
||||
- `packages/contracts/src/ws.test.ts` covers typed push envelope validation and channel/payload mismatch rejection.
|
||||
|
||||
### 3. Introduce explicit server readiness and a single push pipeline — DONE
|
||||
- `apps/server/src/wsServer/pushBus.ts`: `ServerPushBus` with `publishAll` (broadcast) and `publishClient` (targeted) methods, backed by one ordered path. All pushes flow through it.
|
||||
- `apps/server/src/wsServer/readiness.ts`: `ServerReadiness` with Deferred-based barriers for HTTP listening, push bus, keybindings, terminal subscriptions, and orchestration subscriptions.
|
||||
- `server.welcome` is emitted only after connection-scoped and server-scoped readiness is complete.
|
||||
- `wsServer.ts` no longer publishes directly from ad hoc background streams.
|
||||
|
||||
### 4. Turn background watchers into explicit runtimes — DONE
|
||||
- `apps/server/src/keybindings.ts` refactored as explicit `KeybindingsShape` service with `start`, `ready`, `snapshot` semantics.
|
||||
- Initial config load, cache warmup, and dual watcher attachment (`fs.watch` + `watchFileWithStatPolling`) complete before `ready` resolves.
|
||||
- `watchFileWithStatPolling.ts` is the thin adapter for environments where `fs.watch` is unreliable.
|
||||
|
||||
### 5. Replace polling-based orchestration waiting with receipts — DONE
|
||||
- `RuntimeReceiptBus` service defines three receipt types: `CheckpointBaselineCapturedReceipt`, `CheckpointDiffFinalizedReceipt` (with `status: "ready"|"missing"|"error"`), and `TurnProcessingQuiescedReceipt`.
|
||||
- `CheckpointReactor`, `ProviderCommandReactor`, and `ProviderRuntimeIngestion` use `DrainableWorker` and publish receipts on completion.
|
||||
- Integration harness and checkpoint tests await receipts instead of polling snapshots and git refs.
|
||||
|
||||
### 6. Centralize client transport state and decoding — DONE
|
||||
- `apps/web/src/wsTransport.ts` implements an explicit connection state machine: `connecting`, `open`, `reconnecting`, `closed`, `disposed`.
|
||||
- Two-phase decode (JSON parse → Schema validate) happens at the transport boundary. `wsNativeApi.ts` receives pre-validated messages.
|
||||
- Cached latest welcome/config modeled as explicit `latestPushByChannel` state.
|
||||
|
||||
### 7. Replace ad hoc test helpers with semantic test clients — MOSTLY DONE
|
||||
- `DrainableWorker` replaces timing-sensitive `Effect.sleep` with deterministic `drain()` across reactor tests.
|
||||
- Orchestration harness waits on receipts/barriers instead of `waitForThread`, `waitForGitRef`, and retry loops.
|
||||
- Behavioral assertions moved to deterministic unit-style harnesses; narrow integration tests kept for real filesystem/socket behavior.
|
||||
- **Deferred:** Shared `WsTestClient` helper (connect, awaitSemanticWelcome, awaitTypedPush, trackSequence, matchRpcResponseById). Tests use direct transport subscription instead.
|
||||
|
||||
## Provider-Coupling Guardrails
|
||||
- No generic runtime API may depend on Codex-native event names, thread IDs, or request payload shapes.
|
||||
- No readiness barrier may be defined as "Codex has emitted X." Readiness is owned by the server runtime, not by provider event order.
|
||||
- No websocket channel payload may contain raw provider-native payloads unless the channel is explicitly debug/internal.
|
||||
- Any provider-specific divergence must be exposed through provider capabilities from `ProviderService.getCapabilities()`, not `if provider === "codex"` branches in shared runtime code.
|
||||
- Generic tests must use canonical `ProviderRuntimeEvent` fixtures. Codex-specific ordering and translation tests stay in adapter/app-server suites only.
|
||||
- Keep UI/provider-specific knobs such as Codex-only options scoped to provider UX code. Do not pull them into generic transport or orchestration state.
|
||||
|
||||
## Test Plan
|
||||
- Contracts:
|
||||
- schema tests for typed push envelopes and structured decode diagnostics
|
||||
- ordering tests for `sequence`
|
||||
- Server:
|
||||
- readiness tests proving `server.welcome` cannot precede runtime readiness
|
||||
- push bus tests proving terminal/config/orchestration pushes are serialized and typed
|
||||
- keybindings runtime tests with fake watch source plus one real watcher integration test
|
||||
- Orchestration:
|
||||
- receipt tests proving checkpoint refs and projections are complete before completion signals resolve
|
||||
- replacement of polling-based checkpoint/integration waits with receipt-based waits
|
||||
- Web:
|
||||
- transport tests for invalid JSON, invalid envelope, invalid payload, reconnect queue flushing, cached semantic state
|
||||
- Validation gate:
|
||||
- `bun run lint`
|
||||
- `bun run typecheck`
|
||||
- `mise exec -- bun run test`
|
||||
- repeated full-suite run after cutover to confirm flake removal
|
||||
|
||||
## Assumptions and Defaults
|
||||
- This remains a single-provider product during the cutover, but the runtime contracts must stay provider-neutral.
|
||||
- No backward-compatibility layer is required for old websocket push envelopes.
|
||||
- The goal is deterministic runtime behavior first; reducing retries and sleeps in tests is a consequence, not the primary mechanism.
|
||||
- If a completion signal cannot be expressed provider-neutrally, it does not belong in the shared runtime layer and must stay adapter-local.
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
Layer,
|
||||
ManagedRuntime,
|
||||
Option,
|
||||
Ref,
|
||||
Schedule,
|
||||
Schema,
|
||||
Scope,
|
||||
@@ -45,6 +46,7 @@ import { CheckpointReactorLive } from "../src/orchestration/Layers/CheckpointRea
|
||||
import { OrchestrationEngineLive } from "../src/orchestration/Layers/OrchestrationEngine.ts";
|
||||
import { OrchestrationProjectionPipelineLive } from "../src/orchestration/Layers/ProjectionPipeline.ts";
|
||||
import { OrchestrationProjectionSnapshotQueryLive } from "../src/orchestration/Layers/ProjectionSnapshotQuery.ts";
|
||||
import { RuntimeReceiptBusLive } from "../src/orchestration/Layers/RuntimeReceiptBus.ts";
|
||||
import { OrchestrationReactorLive } from "../src/orchestration/Layers/OrchestrationReactor.ts";
|
||||
import { ProviderCommandReactorLive } from "../src/orchestration/Layers/ProviderCommandReactor.ts";
|
||||
import { ProviderRuntimeIngestionLive } from "../src/orchestration/Layers/ProviderRuntimeIngestion.ts";
|
||||
@@ -54,6 +56,10 @@ import {
|
||||
} from "../src/orchestration/Services/OrchestrationEngine.ts";
|
||||
import { OrchestrationReactor } from "../src/orchestration/Services/OrchestrationReactor.ts";
|
||||
import { ProjectionSnapshotQuery } from "../src/orchestration/Services/ProjectionSnapshotQuery.ts";
|
||||
import {
|
||||
RuntimeReceiptBus,
|
||||
type OrchestrationRuntimeReceipt,
|
||||
} from "../src/orchestration/Services/RuntimeReceiptBus.ts";
|
||||
|
||||
import {
|
||||
makeTestProviderAdapterHarness,
|
||||
@@ -114,7 +120,7 @@ function waitFor<A, E>(
|
||||
read: Effect.Effect<A, E>,
|
||||
predicate: (value: A) => boolean,
|
||||
description: string,
|
||||
timeoutMs = 3000,
|
||||
timeoutMs = 10_000,
|
||||
): Effect.Effect<A, never> {
|
||||
const RETRY_SIGNAL = "wait_for_retry";
|
||||
const retryIntervalMs = 10;
|
||||
@@ -185,6 +191,16 @@ export interface OrchestrationIntegrationHarness {
|
||||
},
|
||||
never
|
||||
>;
|
||||
readonly waitForReceipt: {
|
||||
(
|
||||
predicate: (receipt: OrchestrationRuntimeReceipt) => boolean,
|
||||
timeoutMs?: number,
|
||||
): Effect.Effect<OrchestrationRuntimeReceipt, never>;
|
||||
<Receipt extends OrchestrationRuntimeReceipt>(
|
||||
predicate: (receipt: OrchestrationRuntimeReceipt) => receipt is Receipt,
|
||||
timeoutMs?: number,
|
||||
): Effect.Effect<Receipt, never>;
|
||||
};
|
||||
readonly dispose: Effect.Effect<void, never>;
|
||||
}
|
||||
|
||||
@@ -268,6 +284,7 @@ export const makeOrchestrationIntegrationHarness = (
|
||||
ProjectionPendingApprovalRepositoryLive,
|
||||
CheckpointStoreLive,
|
||||
providerLayer,
|
||||
RuntimeReceiptBusLive,
|
||||
);
|
||||
const runtimeIngestionLayer = ProviderRuntimeIngestionLive.pipe(
|
||||
Layer.provideMerge(runtimeServicesLayer),
|
||||
@@ -322,11 +339,18 @@ export const makeOrchestrationIntegrationHarness = (
|
||||
"load ProjectionPendingApprovalRepository service",
|
||||
() => runtime.runPromise(Effect.service(ProjectionPendingApprovalRepository)),
|
||||
).pipe(Effect.orDie);
|
||||
const runtimeReceiptBus = yield* tryRuntimePromise("load RuntimeReceiptBus service", () =>
|
||||
runtime.runPromise(Effect.service(RuntimeReceiptBus)),
|
||||
).pipe(Effect.orDie);
|
||||
|
||||
const scope = yield* Scope.make("sequential");
|
||||
yield* tryRuntimePromise("start OrchestrationReactor", () =>
|
||||
runtime.runPromise(reactor.start.pipe(Scope.provide(scope))),
|
||||
).pipe(Effect.orDie);
|
||||
const receiptHistory = yield* Ref.make<ReadonlyArray<OrchestrationRuntimeReceipt>>([]);
|
||||
yield* Stream.runForEach(runtimeReceiptBus.stream, (receipt) =>
|
||||
Ref.update(receiptHistory, (history) => [...history, receipt]).pipe(Effect.asVoid),
|
||||
).pipe(Effect.forkIn(scope));
|
||||
yield* sleep(10);
|
||||
|
||||
const waitForThread: OrchestrationIntegrationHarness["waitForThread"] = (
|
||||
@@ -398,6 +422,30 @@ export const makeOrchestrationIntegrationHarness = (
|
||||
never
|
||||
>;
|
||||
|
||||
function waitForReceipt(
|
||||
predicate: (receipt: OrchestrationRuntimeReceipt) => boolean,
|
||||
timeoutMs?: number,
|
||||
): Effect.Effect<OrchestrationRuntimeReceipt, never>;
|
||||
function waitForReceipt<Receipt extends OrchestrationRuntimeReceipt>(
|
||||
predicate: (receipt: OrchestrationRuntimeReceipt) => receipt is Receipt,
|
||||
timeoutMs?: number,
|
||||
): Effect.Effect<Receipt, never>;
|
||||
function waitForReceipt(
|
||||
predicate: (receipt: OrchestrationRuntimeReceipt) => boolean,
|
||||
timeoutMs?: number,
|
||||
) {
|
||||
const readMatchingReceipt = Ref.get(receiptHistory).pipe(
|
||||
Effect.map((history) => history.find(predicate)),
|
||||
);
|
||||
|
||||
return waitFor(
|
||||
readMatchingReceipt,
|
||||
(receipt): receipt is OrchestrationRuntimeReceipt => receipt !== undefined,
|
||||
"runtime receipt",
|
||||
timeoutMs,
|
||||
);
|
||||
}
|
||||
|
||||
let disposed = false;
|
||||
const dispose = Effect.gen(function* () {
|
||||
if (disposed) {
|
||||
@@ -443,6 +491,7 @@ export const makeOrchestrationIntegrationHarness = (
|
||||
waitForThread,
|
||||
waitForDomainEvent,
|
||||
waitForPendingApproval,
|
||||
waitForReceipt,
|
||||
dispose,
|
||||
} satisfies OrchestrationIntegrationHarness;
|
||||
});
|
||||
@@ -21,6 +21,10 @@ import {
|
||||
type OrchestrationIntegrationHarness,
|
||||
} from "./OrchestrationEngineHarness.integration.ts";
|
||||
import { checkpointRefForThreadTurn } from "../src/checkpointing/Utils.ts";
|
||||
import type {
|
||||
CheckpointDiffFinalizedReceipt,
|
||||
TurnProcessingQuiescedReceipt,
|
||||
} from "../src/orchestration/Services/RuntimeReceiptBus.ts";
|
||||
|
||||
const asMessageId = (value: string): MessageId => MessageId.makeUnsafe(value);
|
||||
const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
|
||||
@@ -187,6 +191,22 @@ it.live("runs a single turn end-to-end and persists checkpoint state in sqlite +
|
||||
messageId: "msg-user-single",
|
||||
text: "Say hello",
|
||||
});
|
||||
const finalizedReceipt = yield* harness.waitForReceipt(
|
||||
(receipt): receipt is CheckpointDiffFinalizedReceipt =>
|
||||
receipt.type === "checkpoint.diff.finalized" &&
|
||||
receipt.threadId === THREAD_ID &&
|
||||
receipt.checkpointTurnCount === 1,
|
||||
);
|
||||
if (finalizedReceipt.type !== "checkpoint.diff.finalized") {
|
||||
throw new Error("Expected checkpoint.diff.finalized receipt.");
|
||||
}
|
||||
assert.equal(finalizedReceipt.status, "ready");
|
||||
yield* harness.waitForReceipt(
|
||||
(receipt): receipt is TurnProcessingQuiescedReceipt =>
|
||||
receipt.type === "turn.processing.quiesced" &&
|
||||
receipt.threadId === THREAD_ID &&
|
||||
receipt.checkpointTurnCount === 1,
|
||||
);
|
||||
|
||||
const thread = yield* harness.waitForThread(
|
||||
THREAD_ID,
|
||||
@@ -208,8 +228,6 @@ it.live("runs a single turn end-to-end and persists checkpoint state in sqlite +
|
||||
assert.equal(checkpointRows[0]?.status, "ready");
|
||||
assert.deepEqual(checkpointRows[0]?.files, []);
|
||||
|
||||
yield* harness.waitForDomainEvent((event) => event.type === "thread.turn-diff-completed");
|
||||
|
||||
const ref0 = checkpointRefForThreadTurn(THREAD_ID, 0);
|
||||
const ref1 = checkpointRefForThreadTurn(THREAD_ID, 1);
|
||||
assert.equal(gitRefExists(harness.workspaceDir, ref0), true);
|
||||
@@ -367,6 +385,12 @@ it.live("runs multi-turn file edits and persists checkpoint diffs", () =>
|
||||
messageId: "msg-user-multi-1",
|
||||
text: "Make first edit",
|
||||
});
|
||||
yield* harness.waitForReceipt(
|
||||
(receipt): receipt is CheckpointDiffFinalizedReceipt =>
|
||||
receipt.type === "checkpoint.diff.finalized" &&
|
||||
receipt.threadId === THREAD_ID &&
|
||||
receipt.checkpointTurnCount === 1,
|
||||
);
|
||||
|
||||
yield* harness.waitForThread(
|
||||
THREAD_ID,
|
||||
@@ -408,6 +432,22 @@ it.live("runs multi-turn file edits and persists checkpoint diffs", () =>
|
||||
messageId: "msg-user-multi-2",
|
||||
text: "Make second edit",
|
||||
});
|
||||
const secondReceipt = yield* harness.waitForReceipt(
|
||||
(receipt): receipt is CheckpointDiffFinalizedReceipt =>
|
||||
receipt.type === "checkpoint.diff.finalized" &&
|
||||
receipt.threadId === THREAD_ID &&
|
||||
receipt.checkpointTurnCount === 2,
|
||||
);
|
||||
if (secondReceipt.type !== "checkpoint.diff.finalized") {
|
||||
throw new Error("Expected checkpoint.diff.finalized receipt.");
|
||||
}
|
||||
assert.equal(secondReceipt.status, "ready");
|
||||
yield* harness.waitForReceipt(
|
||||
(receipt): receipt is TurnProcessingQuiescedReceipt =>
|
||||
receipt.type === "turn.processing.quiesced" &&
|
||||
receipt.threadId === THREAD_ID &&
|
||||
receipt.checkpointTurnCount === 2,
|
||||
);
|
||||
|
||||
const secondTurnThread = yield* harness.waitForThread(
|
||||
THREAD_ID,
|
||||
|
||||
@@ -654,7 +654,7 @@ const makeGitCore = Effect.gen(function* () {
|
||||
|
||||
const statusDetails: GitCoreShape["statusDetails"] = (cwd) =>
|
||||
Effect.gen(function* () {
|
||||
yield* refreshStatusUpstreamIfStale(cwd).pipe(Effect.catch(() => Effect.void));
|
||||
yield* refreshStatusUpstreamIfStale(cwd).pipe(Effect.ignoreCause({ log: true }));
|
||||
|
||||
const [statusStdout, unstagedNumstatStdout, stagedNumstatStdout] = yield* Effect.all(
|
||||
[
|
||||
@@ -1365,7 +1365,7 @@ const makeGitCore = Effect.gen(function* () {
|
||||
|
||||
// Refresh upstream refs in the background so checkout remains responsive.
|
||||
yield* Effect.forkScoped(
|
||||
refreshCheckedOutBranchUpstream(input.cwd).pipe(Effect.catch(() => Effect.void)),
|
||||
refreshCheckedOutBranchUpstream(input.cwd).pipe(Effect.ignoreCause({ log: true })),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -22,7 +22,9 @@ import {
|
||||
Array,
|
||||
Cache,
|
||||
Cause,
|
||||
Deferred,
|
||||
Effect,
|
||||
Exit,
|
||||
FileSystem,
|
||||
Path,
|
||||
Layer,
|
||||
@@ -33,7 +35,9 @@ import {
|
||||
SchemaGetter,
|
||||
SchemaIssue,
|
||||
SchemaTransformation,
|
||||
Ref,
|
||||
ServiceMap,
|
||||
Scope,
|
||||
Stream,
|
||||
} from "effect";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
@@ -422,6 +426,7 @@ export interface KeybindingsConfigState {
|
||||
}
|
||||
|
||||
export interface KeybindingsChangeEvent {
|
||||
readonly keybindings: ResolvedKeybindingsConfig;
|
||||
readonly issues: readonly ServerConfigIssue[];
|
||||
}
|
||||
|
||||
@@ -468,6 +473,22 @@ function mergeWithDefaultKeybindings(custom: ResolvedKeybindingsConfig): Resolve
|
||||
* KeybindingsShape - Service API for keybinding configuration operations.
|
||||
*/
|
||||
export interface KeybindingsShape {
|
||||
/**
|
||||
* Start the keybindings runtime and attach file watching.
|
||||
*
|
||||
* Safe to call multiple times. The first successful call establishes the
|
||||
* runtime; later calls await the same startup.
|
||||
*/
|
||||
readonly start: Effect.Effect<void, KeybindingsConfigError>;
|
||||
|
||||
/**
|
||||
* Await keybindings runtime readiness.
|
||||
*
|
||||
* Readiness means the config directory exists, the watcher is attached, the
|
||||
* startup sync has completed, and the current snapshot has been loaded.
|
||||
*/
|
||||
readonly ready: Effect.Effect<void, KeybindingsConfigError>;
|
||||
|
||||
/**
|
||||
* Ensure the on-disk keybindings file exists and includes all default
|
||||
* commands so newly-added defaults are backfilled on startup.
|
||||
@@ -480,9 +501,14 @@ export interface KeybindingsShape {
|
||||
readonly loadConfigState: Effect.Effect<KeybindingsConfigState, KeybindingsConfigError>;
|
||||
|
||||
/**
|
||||
* Stream keybindings config change events.
|
||||
* Read the latest keybindings snapshot from cache/disk.
|
||||
*/
|
||||
readonly changes: Stream.Stream<KeybindingsChangeEvent>;
|
||||
readonly getSnapshot: Effect.Effect<KeybindingsConfigState, KeybindingsConfigError>;
|
||||
|
||||
/**
|
||||
* Stream of keybindings config change events.
|
||||
*/
|
||||
readonly streamChanges: Stream.Stream<KeybindingsChangeEvent>;
|
||||
|
||||
/**
|
||||
* Upsert a keybinding rule and persist the resulting configuration.
|
||||
@@ -509,9 +535,12 @@ const makeKeybindings = Effect.gen(function* () {
|
||||
const upsertSemaphore = yield* Semaphore.make(1);
|
||||
const resolvedConfigCacheKey = "resolved" as const;
|
||||
const changesPubSub = yield* PubSub.unbounded<KeybindingsChangeEvent>();
|
||||
|
||||
const emitChange = (issues: readonly ServerConfigIssue[]) =>
|
||||
PubSub.publish(changesPubSub, { issues }).pipe(Effect.asVoid);
|
||||
const startedRef = yield* Ref.make(false);
|
||||
const startedDeferred = yield* Deferred.make<void, KeybindingsConfigError>();
|
||||
const watcherScope = yield* Scope.make("sequential");
|
||||
yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void));
|
||||
const emitChange = (configState: KeybindingsConfigState) =>
|
||||
PubSub.publish(changesPubSub, configState).pipe(Effect.asVoid);
|
||||
|
||||
const readConfigExists = fs.exists(keybindingsConfigPath).pipe(
|
||||
Effect.mapError(
|
||||
@@ -676,43 +705,10 @@ const makeKeybindings = Effect.gen(function* () {
|
||||
Effect.gen(function* () {
|
||||
yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
|
||||
const configState = yield* loadConfigStateFromCacheOrDisk;
|
||||
yield* emitChange(configState.issues);
|
||||
yield* emitChange(configState);
|
||||
}),
|
||||
);
|
||||
|
||||
const keybindingsConfigDir = path.dirname(keybindingsConfigPath);
|
||||
const keybindingsConfigFile = path.basename(keybindingsConfigPath);
|
||||
const keybindingsConfigPathResolved = path.resolve(keybindingsConfigPath);
|
||||
yield* fs
|
||||
.makeDirectory(keybindingsConfigDir, { recursive: true })
|
||||
.pipe(Effect.orElseSucceed(() => undefined));
|
||||
yield* Stream.runForEach(fs.watch(keybindingsConfigDir), (event) => {
|
||||
const isTargetConfigEvent =
|
||||
event.path === keybindingsConfigFile ||
|
||||
event.path === keybindingsConfigPath ||
|
||||
path.resolve(keybindingsConfigDir, event.path) === keybindingsConfigPathResolved;
|
||||
if (!isTargetConfigEvent) {
|
||||
return Effect.void;
|
||||
}
|
||||
return revalidateAndEmit.pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.logWarning("failed to revalidate keybindings config after file update", {
|
||||
path: keybindingsConfigPath,
|
||||
detail: error.detail,
|
||||
cause: error.cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}).pipe(
|
||||
Effect.catch((cause) =>
|
||||
Effect.logWarning("keybindings config watcher stopped unexpectedly", {
|
||||
path: keybindingsConfigPath,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.forkScoped,
|
||||
);
|
||||
|
||||
const syncDefaultKeybindingsOnStartup = upsertSemaphore.withPermits(1)(
|
||||
Effect.gen(function* () {
|
||||
const configExists = yield* readConfigExists;
|
||||
@@ -803,10 +799,68 @@ const makeKeybindings = Effect.gen(function* () {
|
||||
}),
|
||||
);
|
||||
|
||||
const startWatcher = Effect.gen(function* () {
|
||||
const keybindingsConfigDir = path.dirname(keybindingsConfigPath);
|
||||
const keybindingsConfigFile = path.basename(keybindingsConfigPath);
|
||||
const keybindingsConfigPathResolved = path.resolve(keybindingsConfigPath);
|
||||
|
||||
yield* fs.makeDirectory(keybindingsConfigDir, { recursive: true }).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new KeybindingsConfigError({
|
||||
configPath: keybindingsConfigPath,
|
||||
detail: "failed to prepare keybindings config directory",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const revalidateAndEmitSafely = revalidateAndEmit.pipe(Effect.ignoreCause({ log: true }));
|
||||
|
||||
yield* Stream.runForEach(fs.watch(keybindingsConfigDir), (event) => {
|
||||
const isTargetConfigEvent =
|
||||
event.path === keybindingsConfigFile ||
|
||||
event.path === keybindingsConfigPath ||
|
||||
path.resolve(keybindingsConfigDir, event.path) === keybindingsConfigPathResolved;
|
||||
if (!isTargetConfigEvent) {
|
||||
return Effect.void;
|
||||
}
|
||||
return revalidateAndEmitSafely;
|
||||
}).pipe(Effect.ignoreCause({ log: true }), Effect.forkIn(watcherScope), Effect.asVoid);
|
||||
});
|
||||
|
||||
const start = Effect.gen(function* () {
|
||||
const alreadyStarted = yield* Ref.get(startedRef);
|
||||
if (alreadyStarted) {
|
||||
return yield* Deferred.await(startedDeferred);
|
||||
}
|
||||
|
||||
yield* Ref.set(startedRef, true);
|
||||
const startup = Effect.gen(function* () {
|
||||
yield* startWatcher;
|
||||
yield* syncDefaultKeybindingsOnStartup;
|
||||
yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
|
||||
yield* loadConfigStateFromCacheOrDisk;
|
||||
});
|
||||
|
||||
const startupExit = yield* Effect.exit(startup);
|
||||
if (startupExit._tag === "Failure") {
|
||||
yield* Deferred.failCause(startedDeferred, startupExit.cause).pipe(Effect.orDie);
|
||||
return yield* Effect.failCause(startupExit.cause);
|
||||
}
|
||||
|
||||
yield* Deferred.succeed(startedDeferred, undefined).pipe(Effect.orDie);
|
||||
});
|
||||
|
||||
return {
|
||||
start,
|
||||
ready: Deferred.await(startedDeferred),
|
||||
syncDefaultKeybindingsOnStartup,
|
||||
loadConfigState: loadConfigStateFromCacheOrDisk,
|
||||
changes: Stream.fromPubSub(changesPubSub),
|
||||
getSnapshot: loadConfigStateFromCacheOrDisk,
|
||||
get streamChanges() {
|
||||
return Stream.fromPubSub(changesPubSub);
|
||||
},
|
||||
upsertKeybindingRule: (rule) =>
|
||||
upsertSemaphore.withPermits(1)(
|
||||
Effect.gen(function* () {
|
||||
@@ -833,7 +887,10 @@ const makeKeybindings = Effect.gen(function* () {
|
||||
keybindings: nextResolved,
|
||||
issues: [],
|
||||
});
|
||||
yield* emitChange([]);
|
||||
yield* emitChange({
|
||||
keybindings: nextResolved,
|
||||
issues: [],
|
||||
});
|
||||
return nextResolved;
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -22,6 +22,7 @@ import { CheckpointStore } from "../../checkpointing/Services/CheckpointStore.ts
|
||||
import { CheckpointReactorLive } from "./CheckpointReactor.ts";
|
||||
import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
|
||||
import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
|
||||
import { RuntimeReceiptBusLive } from "./RuntimeReceiptBus.ts";
|
||||
import { OrchestrationEventStoreLive } from "../../persistence/Layers/OrchestrationEventStore.ts";
|
||||
import { OrchestrationCommandReceiptRepositoryLive } from "../../persistence/Layers/OrchestrationCommandReceipts.ts";
|
||||
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
|
||||
@@ -112,7 +113,7 @@ async function waitForThread(
|
||||
checkpoints: ReadonlyArray<{ checkpointTurnCount: number }>;
|
||||
activities: ReadonlyArray<{ kind: string }>;
|
||||
}) => boolean,
|
||||
timeoutMs = 2000,
|
||||
timeoutMs = 5000,
|
||||
) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
const poll = async (): Promise<{
|
||||
@@ -137,7 +138,7 @@ async function waitForThread(
|
||||
async function waitForEvent(
|
||||
engine: OrchestrationEngineShape,
|
||||
predicate: (event: { type: string }) => boolean,
|
||||
timeoutMs = 2000,
|
||||
timeoutMs = 5000,
|
||||
) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
const poll = async () => {
|
||||
@@ -188,7 +189,7 @@ function gitShowFileAtRef(cwd: string, ref: string, filePath: string): string {
|
||||
return runGit(cwd, ["show", `${ref}:${filePath}`]);
|
||||
}
|
||||
|
||||
async function waitForGitRefExists(cwd: string, ref: string, timeoutMs = 2000) {
|
||||
async function waitForGitRefExists(cwd: string, ref: string, timeoutMs = 5000) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
const poll = async (): Promise<void> => {
|
||||
if (gitRefExists(cwd, ref)) {
|
||||
@@ -252,6 +253,7 @@ describe("CheckpointReactor", () => {
|
||||
|
||||
const layer = CheckpointReactorLive.pipe(
|
||||
Layer.provideMerge(orchestrationLayer),
|
||||
Layer.provideMerge(RuntimeReceiptBusLive),
|
||||
Layer.provideMerge(Layer.succeed(ProviderService, provider.service)),
|
||||
Layer.provideMerge(CheckpointStoreLive),
|
||||
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
|
||||
@@ -264,7 +266,7 @@ describe("CheckpointReactor", () => {
|
||||
const checkpointStore = await runtime.runPromise(Effect.service(CheckpointStore));
|
||||
scope = await Effect.runPromise(Scope.make("sequential"));
|
||||
await Effect.runPromise(reactor.start.pipe(Scope.provide(scope)));
|
||||
await Effect.runPromise(Effect.sleep("10 millis"));
|
||||
const drain = () => Effect.runPromise(reactor.drain);
|
||||
|
||||
const createdAt = new Date().toISOString();
|
||||
await Effect.runPromise(
|
||||
@@ -321,6 +323,7 @@ describe("CheckpointReactor", () => {
|
||||
engine,
|
||||
provider,
|
||||
cwd,
|
||||
drain,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -449,7 +452,7 @@ describe("CheckpointReactor", () => {
|
||||
payload: { state: "completed" },
|
||||
});
|
||||
|
||||
await Effect.runPromise(Effect.sleep("40 millis"));
|
||||
await harness.drain();
|
||||
const midReadModel = await Effect.runPromise(harness.engine.getReadModel());
|
||||
const midThread = midReadModel.threads.find(
|
||||
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
|
||||
@@ -643,7 +646,7 @@ describe("CheckpointReactor", () => {
|
||||
status: "completed",
|
||||
});
|
||||
|
||||
await Effect.runPromise(Effect.sleep("40 millis"));
|
||||
await harness.drain();
|
||||
const readModel = await Effect.runPromise(harness.engine.getReadModel());
|
||||
const thread = readModel.threads.find((entry) => entry.id === ThreadId.makeUnsafe("thread-1"));
|
||||
expect(thread?.checkpoints.some((checkpoint) => checkpoint.checkpointTurnCount === 3)).toBe(
|
||||
|
||||
@@ -7,7 +7,8 @@ import {
|
||||
type OrchestrationEvent,
|
||||
type ProviderRuntimeEvent,
|
||||
} from "@t3tools/contracts";
|
||||
import { Cause, Effect, Layer, Option, Queue, Stream } from "effect";
|
||||
import { Cause, Effect, Layer, Option, Stream } from "effect";
|
||||
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
|
||||
|
||||
import { parseTurnDiffFilesFromUnifiedDiff } from "../../checkpointing/Diffs.ts";
|
||||
import {
|
||||
@@ -19,6 +20,7 @@ import { CheckpointStore } from "../../checkpointing/Services/CheckpointStore.ts
|
||||
import { ProviderService } from "../../provider/Services/ProviderService.ts";
|
||||
import { CheckpointReactor, type CheckpointReactorShape } from "../Services/CheckpointReactor.ts";
|
||||
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
|
||||
import { RuntimeReceiptBus } from "../Services/RuntimeReceiptBus.ts";
|
||||
import { CheckpointStoreError } from "../../checkpointing/Errors.ts";
|
||||
import { OrchestrationDispatchError } from "../Errors.ts";
|
||||
import { isGitRepository } from "../../git/isRepo.ts";
|
||||
@@ -64,6 +66,7 @@ const make = Effect.gen(function* () {
|
||||
const orchestrationEngine = yield* OrchestrationEngineService;
|
||||
const providerService = yield* ProviderService;
|
||||
const checkpointStore = yield* CheckpointStore;
|
||||
const receiptBus = yield* RuntimeReceiptBus;
|
||||
|
||||
const appendRevertFailureActivity = (input: {
|
||||
readonly threadId: ThreadId;
|
||||
@@ -277,6 +280,22 @@ const make = Effect.gen(function* () {
|
||||
checkpointTurnCount: nextTurnCount,
|
||||
createdAt: now,
|
||||
});
|
||||
yield* receiptBus.publish({
|
||||
type: "checkpoint.diff.finalized",
|
||||
threadId: thread.id,
|
||||
turnId,
|
||||
checkpointTurnCount: nextTurnCount,
|
||||
checkpointRef: targetCheckpointRef,
|
||||
status: checkpointStatusFromRuntime(event.payload.state),
|
||||
createdAt: now,
|
||||
});
|
||||
yield* receiptBus.publish({
|
||||
type: "turn.processing.quiesced",
|
||||
threadId: thread.id,
|
||||
turnId,
|
||||
checkpointTurnCount: nextTurnCount,
|
||||
createdAt: now,
|
||||
});
|
||||
|
||||
yield* orchestrationEngine.dispatch({
|
||||
type: "thread.activity.append",
|
||||
@@ -350,6 +369,13 @@ const make = Effect.gen(function* () {
|
||||
cwd: checkpointCwd,
|
||||
checkpointRef: baselineCheckpointRef,
|
||||
});
|
||||
yield* receiptBus.publish({
|
||||
type: "checkpoint.baseline.captured",
|
||||
threadId: thread.id,
|
||||
checkpointTurnCount: currentTurnCount,
|
||||
checkpointRef: baselineCheckpointRef,
|
||||
createdAt: event.createdAt,
|
||||
});
|
||||
});
|
||||
|
||||
const ensurePreTurnBaselineFromDomainTurnStart = Effect.fnUntraced(function* (
|
||||
@@ -412,6 +438,13 @@ const make = Effect.gen(function* () {
|
||||
cwd: checkpointCwd,
|
||||
checkpointRef: baselineCheckpointRef,
|
||||
});
|
||||
yield* receiptBus.publish({
|
||||
type: "checkpoint.baseline.captured",
|
||||
threadId,
|
||||
checkpointTurnCount: currentTurnCount,
|
||||
checkpointRef: baselineCheckpointRef,
|
||||
createdAt: event.occurredAt,
|
||||
});
|
||||
});
|
||||
|
||||
const handleRevertRequested = Effect.fnUntraced(function* (
|
||||
@@ -603,14 +636,9 @@ const make = Effect.gen(function* () {
|
||||
}),
|
||||
);
|
||||
|
||||
const worker = yield* makeDrainableWorker(processInputSafely);
|
||||
|
||||
const start: CheckpointReactorShape["start"] = Effect.gen(function* () {
|
||||
const queue = yield* Queue.unbounded<ReactorInput>();
|
||||
yield* Effect.addFinalizer(() => Queue.shutdown(queue).pipe(Effect.asVoid));
|
||||
|
||||
yield* Effect.forkScoped(
|
||||
Effect.forever(Queue.take(queue).pipe(Effect.flatMap(processInputSafely))),
|
||||
);
|
||||
|
||||
yield* Effect.forkScoped(
|
||||
Stream.runForEach(orchestrationEngine.streamDomainEvents, (event) => {
|
||||
if (
|
||||
@@ -620,7 +648,7 @@ const make = Effect.gen(function* () {
|
||||
) {
|
||||
return Effect.void;
|
||||
}
|
||||
return Queue.offer(queue, { source: "domain", event }).pipe(Effect.asVoid);
|
||||
return worker.enqueue({ source: "domain", event });
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -629,13 +657,14 @@ const make = Effect.gen(function* () {
|
||||
if (event.type !== "turn.started" && event.type !== "turn.completed") {
|
||||
return Effect.void;
|
||||
}
|
||||
return Queue.offer(queue, { source: "runtime", event }).pipe(Effect.asVoid);
|
||||
return worker.enqueue({ source: "runtime", event });
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
start,
|
||||
drain: worker.drain,
|
||||
} satisfies CheckpointReactorShape;
|
||||
});
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ describe("OrchestrationReactor", () => {
|
||||
start: Effect.sync(() => {
|
||||
started.push("provider-runtime-ingestion");
|
||||
}),
|
||||
drain: Effect.void,
|
||||
}),
|
||||
),
|
||||
Layer.provideMerge(
|
||||
@@ -34,6 +35,7 @@ describe("OrchestrationReactor", () => {
|
||||
start: Effect.sync(() => {
|
||||
started.push("provider-command-reactor");
|
||||
}),
|
||||
drain: Effect.void,
|
||||
}),
|
||||
),
|
||||
Layer.provideMerge(
|
||||
@@ -41,6 +43,7 @@ describe("OrchestrationReactor", () => {
|
||||
start: Effect.sync(() => {
|
||||
started.push("checkpoint-reactor");
|
||||
}),
|
||||
drain: Effect.void,
|
||||
}),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -219,7 +219,7 @@ describe("ProviderCommandReactor", () => {
|
||||
const reactor = await runtime.runPromise(Effect.service(ProviderCommandReactor));
|
||||
scope = await Effect.runPromise(Scope.make("sequential"));
|
||||
await Effect.runPromise(reactor.start.pipe(Scope.provide(scope)));
|
||||
await Effect.runPromise(Effect.sleep("10 millis"));
|
||||
const drain = () => Effect.runPromise(reactor.drain);
|
||||
|
||||
await Effect.runPromise(
|
||||
engine.dispatch({
|
||||
@@ -259,6 +259,7 @@ describe("ProviderCommandReactor", () => {
|
||||
renameBranch,
|
||||
generateBranchName,
|
||||
stateDir,
|
||||
drain,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -577,7 +578,7 @@ describe("ProviderCommandReactor", () => {
|
||||
return thread?.runtimeMode === "approval-required";
|
||||
});
|
||||
await waitFor(() => harness.startSession.mock.calls.length === 2);
|
||||
await Effect.runPromise(Effect.sleep("30 millis"));
|
||||
await harness.drain();
|
||||
|
||||
expect(harness.stopSession.mock.calls.length).toBe(0);
|
||||
expect(harness.sendTurn.mock.calls.length).toBe(1);
|
||||
|
||||
@@ -12,7 +12,8 @@ import {
|
||||
type RuntimeMode,
|
||||
type TurnId,
|
||||
} from "@t3tools/contracts";
|
||||
import { Cache, Cause, Duration, Effect, Layer, Option, Queue, Schema, Stream } from "effect";
|
||||
import { Cache, Cause, Duration, Effect, Layer, Option, Schema, Stream } from "effect";
|
||||
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
|
||||
|
||||
import { resolveThreadWorkspaceCwd } from "../../checkpointing/Utils.ts";
|
||||
import { GitCore } from "../../git/Services/GitCore.ts";
|
||||
@@ -573,16 +574,14 @@ const make = Effect.gen(function* () {
|
||||
})
|
||||
.pipe(
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.gen(function* () {
|
||||
yield* appendProviderFailureActivity({
|
||||
threadId: event.payload.threadId,
|
||||
kind: "provider.user-input.respond.failed",
|
||||
summary: "Provider user input response failed",
|
||||
detail: Cause.pretty(cause),
|
||||
turnId: null,
|
||||
createdAt: event.payload.createdAt,
|
||||
requestId: event.payload.requestId,
|
||||
});
|
||||
appendProviderFailureActivity({
|
||||
threadId: event.payload.threadId,
|
||||
kind: "provider.user-input.respond.failed",
|
||||
summary: "Provider user input response failed",
|
||||
detail: Cause.pretty(cause),
|
||||
turnId: null,
|
||||
createdAt: event.payload.createdAt,
|
||||
requestId: event.payload.requestId,
|
||||
}),
|
||||
),
|
||||
);
|
||||
@@ -665,34 +664,28 @@ const make = Effect.gen(function* () {
|
||||
}),
|
||||
);
|
||||
|
||||
const start: ProviderCommandReactorShape["start"] = Effect.gen(function* () {
|
||||
const queue = yield* Queue.unbounded<ProviderIntentEvent>();
|
||||
yield* Effect.addFinalizer(() => Queue.shutdown(queue).pipe(Effect.asVoid));
|
||||
const worker = yield* makeDrainableWorker(processDomainEventSafely);
|
||||
|
||||
yield* Effect.forkScoped(
|
||||
Effect.forever(Queue.take(queue).pipe(Effect.flatMap(processDomainEventSafely))),
|
||||
);
|
||||
const start: ProviderCommandReactorShape["start"] = Effect.forkScoped(
|
||||
Stream.runForEach(orchestrationEngine.streamDomainEvents, (event) => {
|
||||
if (
|
||||
event.type !== "thread.runtime-mode-set" &&
|
||||
event.type !== "thread.turn-start-requested" &&
|
||||
event.type !== "thread.turn-interrupt-requested" &&
|
||||
event.type !== "thread.approval-response-requested" &&
|
||||
event.type !== "thread.user-input-response-requested" &&
|
||||
event.type !== "thread.session-stop-requested"
|
||||
) {
|
||||
return Effect.void;
|
||||
}
|
||||
|
||||
yield* Effect.forkScoped(
|
||||
Stream.runForEach(orchestrationEngine.streamDomainEvents, (event) => {
|
||||
if (
|
||||
event.type !== "thread.runtime-mode-set" &&
|
||||
event.type !== "thread.turn-start-requested" &&
|
||||
event.type !== "thread.turn-interrupt-requested" &&
|
||||
event.type !== "thread.approval-response-requested" &&
|
||||
event.type !== "thread.user-input-response-requested" &&
|
||||
event.type !== "thread.session-stop-requested"
|
||||
) {
|
||||
return Effect.void;
|
||||
}
|
||||
|
||||
return Queue.offer(queue, event).pipe(Effect.asVoid);
|
||||
}),
|
||||
);
|
||||
});
|
||||
return worker.enqueue(event);
|
||||
}),
|
||||
).pipe(Effect.asVoid);
|
||||
|
||||
return {
|
||||
start,
|
||||
drain: worker.drain,
|
||||
} satisfies ProviderCommandReactorShape;
|
||||
});
|
||||
|
||||
|
||||
@@ -159,7 +159,7 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
const ingestion = await runtime.runPromise(Effect.service(ProviderRuntimeIngestionService));
|
||||
scope = await Effect.runPromise(Scope.make("sequential"));
|
||||
await Effect.runPromise(ingestion.start.pipe(Scope.provide(scope)));
|
||||
await Effect.runPromise(Effect.sleep("10 millis"));
|
||||
const drain = () => Effect.runPromise(ingestion.drain);
|
||||
|
||||
const createdAt = new Date().toISOString();
|
||||
await Effect.runPromise(
|
||||
@@ -209,6 +209,7 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
return {
|
||||
engine,
|
||||
emit: provider.emit,
|
||||
drain,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -377,7 +378,7 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
threadId: asThreadId("thread-1"),
|
||||
});
|
||||
|
||||
await Effect.runPromise(Effect.sleep("40 millis"));
|
||||
await harness.drain();
|
||||
const midReadModel = await Effect.runPromise(harness.engine.getReadModel());
|
||||
const midThread = midReadModel.threads.find(
|
||||
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
|
||||
@@ -430,7 +431,7 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
status: "completed",
|
||||
});
|
||||
|
||||
await Effect.runPromise(Effect.sleep("40 millis"));
|
||||
await harness.drain();
|
||||
const midReadModel = await Effect.runPromise(harness.engine.getReadModel());
|
||||
const midThread = midReadModel.threads.find(
|
||||
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
|
||||
@@ -484,7 +485,7 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
status: "completed",
|
||||
});
|
||||
|
||||
await Effect.runPromise(Effect.sleep("40 millis"));
|
||||
await harness.drain();
|
||||
const midReadModel = await Effect.runPromise(harness.engine.getReadModel());
|
||||
const midThread = midReadModel.threads.find(
|
||||
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
|
||||
@@ -725,7 +726,7 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
},
|
||||
});
|
||||
|
||||
await Effect.runPromise(Effect.sleep("30 millis"));
|
||||
await harness.drain();
|
||||
const midReadModel = await Effect.runPromise(harness.engine.getReadModel());
|
||||
const midThread = midReadModel.threads.find(
|
||||
(entry) => entry.id === ThreadId.makeUnsafe("thread-1"),
|
||||
@@ -784,7 +785,7 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
createdAt: now,
|
||||
}),
|
||||
);
|
||||
await Effect.runPromise(Effect.sleep("30 millis"));
|
||||
await harness.drain();
|
||||
|
||||
harness.emit({
|
||||
type: "turn.started",
|
||||
|
||||
@@ -10,7 +10,8 @@ import {
|
||||
type OrchestrationThreadActivity,
|
||||
type ProviderRuntimeEvent,
|
||||
} from "@t3tools/contracts";
|
||||
import { Cache, Cause, Duration, Effect, Layer, Option, Queue, Ref, Stream } from "effect";
|
||||
import { Cache, Cause, Duration, Effect, Layer, Option, Ref, Stream } from "effect";
|
||||
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
|
||||
|
||||
import { ProviderService } from "../../provider/Services/ProviderService.ts";
|
||||
import { resolveThreadWorkspaceCwd } from "../../checkpointing/Utils.ts";
|
||||
@@ -1118,16 +1119,12 @@ const make = Effect.gen(function* () {
|
||||
}),
|
||||
);
|
||||
|
||||
const start: ProviderRuntimeIngestionShape["start"] = Effect.gen(function* () {
|
||||
const inputQueue = yield* Queue.unbounded<RuntimeIngestionInput>();
|
||||
yield* Effect.addFinalizer(() => Queue.shutdown(inputQueue).pipe(Effect.asVoid));
|
||||
const worker = yield* makeDrainableWorker(processInputSafely);
|
||||
|
||||
yield* Effect.forkScoped(
|
||||
Effect.forever(Queue.take(inputQueue).pipe(Effect.flatMap(processInputSafely))),
|
||||
);
|
||||
const start: ProviderRuntimeIngestionShape["start"] = Effect.gen(function* () {
|
||||
yield* Effect.forkScoped(
|
||||
Stream.runForEach(providerService.streamEvents, (event) =>
|
||||
Queue.offer(inputQueue, { source: "runtime", event }).pipe(Effect.asVoid),
|
||||
worker.enqueue({ source: "runtime", event }),
|
||||
),
|
||||
);
|
||||
yield* Effect.forkScoped(
|
||||
@@ -1135,13 +1132,14 @@ const make = Effect.gen(function* () {
|
||||
if (event.type !== "thread.turn-start-requested") {
|
||||
return Effect.void;
|
||||
}
|
||||
return Queue.offer(inputQueue, { source: "domain", event }).pipe(Effect.asVoid);
|
||||
return worker.enqueue({ source: "domain", event });
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
start,
|
||||
drain: worker.drain,
|
||||
} satisfies ProviderRuntimeIngestionShape;
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Effect, Layer, PubSub, Stream } from "effect";
|
||||
|
||||
import {
|
||||
RuntimeReceiptBus,
|
||||
type RuntimeReceiptBusShape,
|
||||
type OrchestrationRuntimeReceipt,
|
||||
} from "../Services/RuntimeReceiptBus.ts";
|
||||
|
||||
const makeRuntimeReceiptBus = Effect.gen(function* () {
|
||||
const pubSub = yield* PubSub.unbounded<OrchestrationRuntimeReceipt>();
|
||||
|
||||
return {
|
||||
publish: (receipt) => PubSub.publish(pubSub, receipt).pipe(Effect.asVoid),
|
||||
stream: Stream.fromPubSub(pubSub),
|
||||
} satisfies RuntimeReceiptBusShape;
|
||||
});
|
||||
|
||||
export const RuntimeReceiptBusLive = Layer.effect(RuntimeReceiptBus, makeRuntimeReceiptBus);
|
||||
@@ -23,6 +23,12 @@ export interface CheckpointReactorShape {
|
||||
* internal queue.
|
||||
*/
|
||||
readonly start: Effect.Effect<void, never, Scope.Scope>;
|
||||
|
||||
/**
|
||||
* Resolves when the internal processing queue is empty and idle.
|
||||
* Intended for test use to replace timing-sensitive sleeps.
|
||||
*/
|
||||
readonly drain: Effect.Effect<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,12 @@ export interface ProviderCommandReactorShape {
|
||||
* processing.
|
||||
*/
|
||||
readonly start: Effect.Effect<void, never, Scope.Scope>;
|
||||
|
||||
/**
|
||||
* Resolves when the internal processing queue is empty and idle.
|
||||
* Intended for test use to replace timing-sensitive sleeps.
|
||||
*/
|
||||
readonly drain: Effect.Effect<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,12 @@ export interface ProviderRuntimeIngestionShape {
|
||||
* logging warnings.
|
||||
*/
|
||||
readonly start: Effect.Effect<void, never, Scope.Scope>;
|
||||
|
||||
/**
|
||||
* Resolves when the internal processing queue is empty and idle.
|
||||
* Intended for test use to replace timing-sensitive sleeps.
|
||||
*/
|
||||
readonly drain: Effect.Effect<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { CheckpointRef, IsoDateTime, NonNegativeInt, ThreadId, TurnId } from "@t3tools/contracts";
|
||||
import { Schema, ServiceMap } from "effect";
|
||||
import type { Effect, Stream } from "effect";
|
||||
|
||||
export const CheckpointBaselineCapturedReceipt = Schema.Struct({
|
||||
type: Schema.Literal("checkpoint.baseline.captured"),
|
||||
threadId: ThreadId,
|
||||
checkpointTurnCount: NonNegativeInt,
|
||||
checkpointRef: CheckpointRef,
|
||||
createdAt: IsoDateTime,
|
||||
});
|
||||
export type CheckpointBaselineCapturedReceipt = typeof CheckpointBaselineCapturedReceipt.Type;
|
||||
|
||||
export const CheckpointDiffFinalizedReceipt = Schema.Struct({
|
||||
type: Schema.Literal("checkpoint.diff.finalized"),
|
||||
threadId: ThreadId,
|
||||
turnId: TurnId,
|
||||
checkpointTurnCount: NonNegativeInt,
|
||||
checkpointRef: CheckpointRef,
|
||||
status: Schema.Literals(["ready", "missing", "error"]),
|
||||
createdAt: IsoDateTime,
|
||||
});
|
||||
export type CheckpointDiffFinalizedReceipt = typeof CheckpointDiffFinalizedReceipt.Type;
|
||||
|
||||
export const TurnProcessingQuiescedReceipt = Schema.Struct({
|
||||
type: Schema.Literal("turn.processing.quiesced"),
|
||||
threadId: ThreadId,
|
||||
turnId: TurnId,
|
||||
checkpointTurnCount: NonNegativeInt,
|
||||
createdAt: IsoDateTime,
|
||||
});
|
||||
export type TurnProcessingQuiescedReceipt = typeof TurnProcessingQuiescedReceipt.Type;
|
||||
|
||||
export const OrchestrationRuntimeReceipt = Schema.Union([
|
||||
CheckpointBaselineCapturedReceipt,
|
||||
CheckpointDiffFinalizedReceipt,
|
||||
TurnProcessingQuiescedReceipt,
|
||||
]);
|
||||
export type OrchestrationRuntimeReceipt = typeof OrchestrationRuntimeReceipt.Type;
|
||||
|
||||
export interface RuntimeReceiptBusShape {
|
||||
readonly publish: (receipt: OrchestrationRuntimeReceipt) => Effect.Effect<void>;
|
||||
readonly stream: Stream.Stream<OrchestrationRuntimeReceipt>;
|
||||
}
|
||||
|
||||
export class RuntimeReceiptBus extends ServiceMap.Service<
|
||||
RuntimeReceiptBus,
|
||||
RuntimeReceiptBusShape
|
||||
>()("t3/orchestration/Services/RuntimeReceiptBus") {}
|
||||
@@ -292,9 +292,7 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) =>
|
||||
}
|
||||
|
||||
yield* upsertSessionBinding(session, threadId, {
|
||||
...(input.providerOptions !== undefined
|
||||
? { providerOptions: input.providerOptions }
|
||||
: {}),
|
||||
providerOptions: input.providerOptions,
|
||||
});
|
||||
yield* analytics.record("provider.session.started", {
|
||||
provider: session.provider,
|
||||
|
||||
@@ -17,6 +17,7 @@ import { ProviderCommandReactorLive } from "./orchestration/Layers/ProviderComma
|
||||
import { OrchestrationProjectionPipelineLive } from "./orchestration/Layers/ProjectionPipeline";
|
||||
import { OrchestrationProjectionSnapshotQueryLive } from "./orchestration/Layers/ProjectionSnapshotQuery";
|
||||
import { ProviderRuntimeIngestionLive } from "./orchestration/Layers/ProviderRuntimeIngestion";
|
||||
import { RuntimeReceiptBusLive } from "./orchestration/Layers/RuntimeReceiptBus";
|
||||
import { ProviderUnsupportedError } from "./provider/Errors";
|
||||
import { makeCodexAdapterLive } from "./provider/Layers/CodexAdapter";
|
||||
import { ProviderAdapterRegistryLive } from "./provider/Layers/ProviderAdapterRegistry";
|
||||
@@ -87,6 +88,7 @@ export function makeServerRuntimeServicesLayer() {
|
||||
OrchestrationProjectionSnapshotQueryLive,
|
||||
CheckpointStoreLive,
|
||||
checkpointDiffQueryLayer,
|
||||
RuntimeReceiptBusLive,
|
||||
);
|
||||
const runtimeIngestionLayer = ProviderRuntimeIngestionLive.pipe(
|
||||
Layer.provideMerge(runtimeServicesLayer),
|
||||
|
||||
+203
-160
@@ -27,6 +27,8 @@ import {
|
||||
type ServerProviderStatus,
|
||||
type KeybindingsConfig,
|
||||
type ResolvedKeybindingsConfig,
|
||||
type WsPushChannel,
|
||||
type WsPushMessage,
|
||||
type WsPush,
|
||||
} from "@t3tools/contracts";
|
||||
import { compileResolvedKeybindingRule, DEFAULT_KEYBINDINGS } from "./keybindings";
|
||||
@@ -52,13 +54,6 @@ import { GitCommandError, GitManagerError } from "./git/Errors.ts";
|
||||
import { MigrationError } from "@effect/sql-sqlite-bun/SqliteMigrator";
|
||||
import { AnalyticsService } from "./telemetry/Services/AnalyticsService.ts";
|
||||
|
||||
interface PendingMessages {
|
||||
queue: unknown[];
|
||||
waiters: Array<(message: unknown) => void>;
|
||||
}
|
||||
|
||||
const pendingBySocket = new WeakMap<WebSocket, PendingMessages>();
|
||||
|
||||
const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
|
||||
const asProviderItemId = (value: string): ProviderItemId => ProviderItemId.makeUnsafe(value);
|
||||
const asThreadId = (value: string): ThreadId => ThreadId.makeUnsafe(value);
|
||||
@@ -214,44 +209,69 @@ class MockTerminalManager implements TerminalManagerShape {
|
||||
readonly dispose: TerminalManagerShape["dispose"] = Effect.void;
|
||||
}
|
||||
|
||||
function connectWs(port: number, token?: string): Promise<WebSocket> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = token ? `?token=${encodeURIComponent(token)}` : "";
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${port}/${query}`);
|
||||
const pending: PendingMessages = { queue: [], waiters: [] };
|
||||
pendingBySocket.set(ws, pending);
|
||||
// ---------------------------------------------------------------------------
|
||||
// WebSocket test harness
|
||||
//
|
||||
// Incoming messages are split into two channels:
|
||||
// - pushChannel: server push envelopes (type === "push")
|
||||
// - responseChannel: request/response envelopes (have an "id" field)
|
||||
//
|
||||
// This means sendRequest never has to skip push messages and waitForPush
|
||||
// never has to skip response messages, eliminating a class of ordering bugs.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
ws.on("message", (raw) => {
|
||||
const parsed = JSON.parse(String(raw));
|
||||
const waiter = pending.waiters.shift();
|
||||
if (waiter) {
|
||||
waiter(parsed);
|
||||
return;
|
||||
}
|
||||
pending.queue.push(parsed);
|
||||
});
|
||||
|
||||
ws.once("open", () => resolve(ws));
|
||||
ws.once("error", () => reject(new Error("WebSocket connection failed")));
|
||||
});
|
||||
interface MessageChannel<T> {
|
||||
queue: T[];
|
||||
waiters: Array<{
|
||||
resolve: (value: T) => void;
|
||||
reject: (error: Error) => void;
|
||||
timeoutId: ReturnType<typeof setTimeout> | null;
|
||||
}>;
|
||||
}
|
||||
|
||||
function waitForMessage(ws: WebSocket): Promise<unknown> {
|
||||
const pending = pendingBySocket.get(ws);
|
||||
if (!pending) {
|
||||
return Promise.reject(new Error("WebSocket not initialized"));
|
||||
}
|
||||
interface SocketChannels {
|
||||
push: MessageChannel<WsPush>;
|
||||
response: MessageChannel<WebSocketResponse>;
|
||||
}
|
||||
|
||||
const queued = pending.queue.shift();
|
||||
const channelsBySocket = new WeakMap<WebSocket, SocketChannels>();
|
||||
|
||||
function enqueue<T>(channel: MessageChannel<T>, item: T) {
|
||||
const waiter = channel.waiters.shift();
|
||||
if (waiter) {
|
||||
if (waiter.timeoutId !== null) clearTimeout(waiter.timeoutId);
|
||||
waiter.resolve(item);
|
||||
return;
|
||||
}
|
||||
channel.queue.push(item);
|
||||
}
|
||||
|
||||
function dequeue<T>(channel: MessageChannel<T>, timeoutMs: number): Promise<T> {
|
||||
const queued = channel.queue.shift();
|
||||
if (queued !== undefined) {
|
||||
return Promise.resolve(queued);
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
pending.waiters.push(resolve);
|
||||
return new Promise((resolve, reject) => {
|
||||
const waiter = {
|
||||
resolve,
|
||||
reject,
|
||||
timeoutId: setTimeout(() => {
|
||||
const index = channel.waiters.indexOf(waiter);
|
||||
if (index >= 0) channel.waiters.splice(index, 1);
|
||||
reject(new Error(`Timed out waiting for WebSocket message after ${timeoutMs}ms`));
|
||||
}, timeoutMs) as ReturnType<typeof setTimeout>,
|
||||
};
|
||||
channel.waiters.push(waiter);
|
||||
});
|
||||
}
|
||||
|
||||
function isWsPushEnvelope(message: unknown): message is WsPush {
|
||||
if (typeof message !== "object" || message === null) return false;
|
||||
if (!("type" in message) || !("channel" in message)) return false;
|
||||
return (message as { type?: unknown }).type === "push";
|
||||
}
|
||||
|
||||
function asWebSocketResponse(message: unknown): WebSocketResponse | null {
|
||||
if (typeof message !== "object" || message === null) return null;
|
||||
if (!("id" in message)) return null;
|
||||
@@ -260,11 +280,68 @@ function asWebSocketResponse(message: unknown): WebSocketResponse | null {
|
||||
return message as WebSocketResponse;
|
||||
}
|
||||
|
||||
function connectWsOnce(port: number, token?: string): Promise<WebSocket> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = token ? `?token=${encodeURIComponent(token)}` : "";
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${port}/${query}`);
|
||||
const channels: SocketChannels = {
|
||||
push: { queue: [], waiters: [] },
|
||||
response: { queue: [], waiters: [] },
|
||||
};
|
||||
channelsBySocket.set(ws, channels);
|
||||
|
||||
ws.on("message", (raw) => {
|
||||
const parsed = JSON.parse(String(raw));
|
||||
if (isWsPushEnvelope(parsed)) {
|
||||
enqueue(channels.push, parsed);
|
||||
} else {
|
||||
const response = asWebSocketResponse(parsed);
|
||||
if (response) {
|
||||
enqueue(channels.response, response);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.once("open", () => resolve(ws));
|
||||
ws.once("error", () => reject(new Error("WebSocket connection failed")));
|
||||
});
|
||||
}
|
||||
|
||||
async function connectWs(port: number, token?: string, attempts = 5): Promise<WebSocket> {
|
||||
let lastError: unknown = new Error("WebSocket connection failed");
|
||||
|
||||
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
||||
try {
|
||||
return await connectWsOnce(port, token);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt < attempts - 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 25));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
/** Connect and wait for the server.welcome push. Returns [ws, welcomeData]. */
|
||||
async function connectAndAwaitWelcome(
|
||||
port: number,
|
||||
token?: string,
|
||||
): Promise<[WebSocket, WsPushMessage<typeof WS_CHANNELS.serverWelcome>]> {
|
||||
const ws = await connectWs(port, token);
|
||||
const welcome = await waitForPush(ws, WS_CHANNELS.serverWelcome);
|
||||
return [ws, welcome];
|
||||
}
|
||||
|
||||
async function sendRequest(
|
||||
ws: WebSocket,
|
||||
method: string,
|
||||
params?: unknown,
|
||||
): Promise<WebSocketResponse> {
|
||||
const channels = channelsBySocket.get(ws);
|
||||
if (!channels) throw new Error("WebSocket not initialized");
|
||||
|
||||
const id = crypto.randomUUID();
|
||||
const body =
|
||||
method === ORCHESTRATION_WS_METHODS.dispatchCommand
|
||||
@@ -272,44 +349,53 @@ async function sendRequest(
|
||||
: params && typeof params === "object" && !Array.isArray(params)
|
||||
? { _tag: method, ...(params as Record<string, unknown>) }
|
||||
: { _tag: method };
|
||||
const message = JSON.stringify({ id, body });
|
||||
ws.send(message);
|
||||
ws.send(JSON.stringify({ id, body }));
|
||||
|
||||
// Wait for response with matching id
|
||||
// Response channel only contains responses — no push filtering needed
|
||||
while (true) {
|
||||
const parsed = asWebSocketResponse(await waitForMessage(ws));
|
||||
if (!parsed) {
|
||||
continue;
|
||||
}
|
||||
if (parsed.id === id) {
|
||||
return parsed;
|
||||
}
|
||||
if (parsed.id === "unknown") {
|
||||
return parsed;
|
||||
const response = await dequeue(channels.response, 60_000);
|
||||
if (response.id === id || response.id === "unknown") {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForPush(
|
||||
async function waitForPush<C extends WsPushChannel>(
|
||||
ws: WebSocket,
|
||||
channel: string,
|
||||
predicate?: (push: WsPush) => boolean,
|
||||
channel: C,
|
||||
predicate?: (push: WsPushMessage<C>) => boolean,
|
||||
maxMessages = 120,
|
||||
): Promise<WsPush> {
|
||||
const take = async (remaining: number): Promise<WsPush> => {
|
||||
if (remaining <= 0) {
|
||||
throw new Error(`Timed out waiting for push on ${channel}`);
|
||||
idleTimeoutMs = 5_000,
|
||||
): Promise<WsPushMessage<C>> {
|
||||
const channels = channelsBySocket.get(ws);
|
||||
if (!channels) throw new Error("WebSocket not initialized");
|
||||
|
||||
for (let remaining = maxMessages; remaining > 0; remaining--) {
|
||||
const push = await dequeue(channels.push, idleTimeoutMs);
|
||||
if (push.channel !== channel) continue;
|
||||
const typed = push as WsPushMessage<C>;
|
||||
if (!predicate || predicate(typed)) return typed;
|
||||
}
|
||||
throw new Error(`Timed out waiting for push on ${channel}`);
|
||||
}
|
||||
|
||||
async function rewriteKeybindingsAndWaitForPush(
|
||||
ws: WebSocket,
|
||||
keybindingsPath: string,
|
||||
contents: string,
|
||||
predicate: (push: WsPushMessage<typeof WS_CHANNELS.serverConfigUpdated>) => boolean,
|
||||
attempts = 3,
|
||||
): Promise<WsPushMessage<typeof WS_CHANNELS.serverConfigUpdated>> {
|
||||
let lastError: unknown;
|
||||
for (let attempt = 0; attempt < attempts; attempt++) {
|
||||
fs.writeFileSync(keybindingsPath, contents, "utf8");
|
||||
try {
|
||||
return await waitForPush(ws, WS_CHANNELS.serverConfigUpdated, predicate, 20, 3_000);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
}
|
||||
const message = (await waitForMessage(ws)) as WsPush;
|
||||
if (message.type !== "push" || message.channel !== channel) {
|
||||
return take(remaining - 1);
|
||||
}
|
||||
if (!predicate || predicate(message)) {
|
||||
return message;
|
||||
}
|
||||
return take(remaining - 1);
|
||||
};
|
||||
return take(maxMessages);
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async function requestPath(
|
||||
@@ -489,18 +575,15 @@ describe("WebSocket Server", () => {
|
||||
|
||||
it("sends welcome message on connect", async () => {
|
||||
server = await createTestServer({ cwd: "/test/project" });
|
||||
// Get the actual port after listen
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
expect(port).toBeGreaterThan(0);
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws, welcome] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
|
||||
const message = (await waitForMessage(ws)) as WsPush;
|
||||
expect(message.type).toBe("push");
|
||||
expect(message.channel).toBe(WS_CHANNELS.serverWelcome);
|
||||
expect(message.data).toEqual({
|
||||
expect(welcome.type).toBe("push");
|
||||
expect(welcome.data).toEqual({
|
||||
cwd: "/test/project",
|
||||
projectName: "project",
|
||||
});
|
||||
@@ -589,10 +672,8 @@ describe("WebSocket Server", () => {
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
expect(port).toBeGreaterThan(0);
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws, welcome] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
const welcome = (await waitForMessage(ws)) as WsPush; // welcome
|
||||
expect(welcome.channel).toBe(WS_CHANNELS.serverWelcome);
|
||||
expect(welcome.data).toEqual(
|
||||
expect.objectContaining({
|
||||
cwd: "/test/bootstrap-workspace",
|
||||
@@ -666,9 +747,8 @@ describe("WebSocket Server", () => {
|
||||
let port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
expect(port).toBeGreaterThan(0);
|
||||
|
||||
const firstWs = await connectWs(port);
|
||||
const [firstWs, firstWelcome] = await connectAndAwaitWelcome(port);
|
||||
connections.push(firstWs);
|
||||
const firstWelcome = (await waitForMessage(firstWs)) as WsPush;
|
||||
const firstBootstrapProjectId = (firstWelcome.data as { bootstrapProjectId?: string })
|
||||
.bootstrapProjectId;
|
||||
const firstBootstrapThreadId = (firstWelcome.data as { bootstrapThreadId?: string })
|
||||
@@ -690,10 +770,8 @@ describe("WebSocket Server", () => {
|
||||
port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
expect(port).toBeGreaterThan(0);
|
||||
|
||||
const secondWs = await connectWs(port);
|
||||
const [secondWs, secondWelcome] = await connectAndAwaitWelcome(port);
|
||||
connections.push(secondWs);
|
||||
const secondWelcome = (await waitForMessage(secondWs)) as WsPush;
|
||||
expect(secondWelcome.channel).toBe(WS_CHANNELS.serverWelcome);
|
||||
expect(secondWelcome.data).toEqual(
|
||||
expect.objectContaining({
|
||||
cwd,
|
||||
@@ -717,9 +795,8 @@ describe("WebSocket Server", () => {
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
expect(port).toBeGreaterThan(0);
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
expect(
|
||||
logSpy.mock.calls.some(([message]) => {
|
||||
@@ -742,12 +819,9 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
|
||||
// Consume welcome message
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(response.error).toBeUndefined();
|
||||
expect(response.result).toEqual({
|
||||
@@ -770,9 +844,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(response.error).toBeUndefined();
|
||||
@@ -801,9 +874,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(response.error).toBeUndefined();
|
||||
@@ -841,9 +913,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(response.error).toBeUndefined();
|
||||
@@ -885,32 +956,28 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
fs.writeFileSync(keybindingsPath, "{ not-json", "utf8");
|
||||
const malformedPush = await waitForPush(
|
||||
const malformedPush = await rewriteKeybindingsAndWaitForPush(
|
||||
ws,
|
||||
WS_CHANNELS.serverConfigUpdated,
|
||||
keybindingsPath,
|
||||
"{ not-json",
|
||||
(push) =>
|
||||
Array.isArray((push.data as { issues?: unknown[] }).issues) &&
|
||||
Boolean((push.data as { issues: Array<{ kind: string }> }).issues[0]) &&
|
||||
(push.data as { issues: Array<{ kind: string }> }).issues[0]!.kind ===
|
||||
"keybindings.malformed-config",
|
||||
Array.isArray(push.data.issues) &&
|
||||
Boolean(push.data.issues[0]) &&
|
||||
push.data.issues[0]!.kind === "keybindings.malformed-config",
|
||||
);
|
||||
expect(malformedPush.data).toEqual({
|
||||
issues: [{ kind: "keybindings.malformed-config", message: expect.any(String) }],
|
||||
providers: defaultProviderStatuses,
|
||||
});
|
||||
|
||||
fs.writeFileSync(keybindingsPath, "[]", "utf8");
|
||||
const successPush = await waitForPush(
|
||||
const successPush = await rewriteKeybindingsAndWaitForPush(
|
||||
ws,
|
||||
WS_CHANNELS.serverConfigUpdated,
|
||||
(push) =>
|
||||
Array.isArray((push.data as { issues?: unknown[] }).issues) &&
|
||||
(push.data as { issues: unknown[] }).issues.length === 0,
|
||||
keybindingsPath,
|
||||
"[]",
|
||||
(push) => Array.isArray(push.data.issues) && push.data.issues.length === 0,
|
||||
);
|
||||
expect(successPush.data).toEqual({ issues: [], providers: defaultProviderStatuses });
|
||||
});
|
||||
@@ -929,9 +996,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.shellOpenInEditor, {
|
||||
cwd: "/my/workspace",
|
||||
@@ -957,11 +1023,9 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.serverGetConfig);
|
||||
expect(response.error).toBeUndefined();
|
||||
const persistedConfig = JSON.parse(
|
||||
@@ -991,9 +1055,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const upsertResponse = await sendRequest(ws, WS_METHODS.serverUpsertKeybinding, {
|
||||
key: "mod+shift+r",
|
||||
@@ -1033,12 +1096,9 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
|
||||
// Consume welcome push
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, "nonexistent.method");
|
||||
expect(response.error).toBeDefined();
|
||||
expect(response.error!.message).toContain("Invalid request format");
|
||||
@@ -1049,9 +1109,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, ORCHESTRATION_WS_METHODS.getTurnDiff, {
|
||||
threadId: "thread-missing",
|
||||
@@ -1067,9 +1126,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, ORCHESTRATION_WS_METHODS.getTurnDiff, {
|
||||
threadId: "thread-any",
|
||||
@@ -1087,9 +1145,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, ORCHESTRATION_WS_METHODS.getFullThreadDiff, {
|
||||
threadId: "thread-missing",
|
||||
@@ -1104,9 +1161,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const workspaceRoot = makeTempDir("t3code-ws-diff-project-");
|
||||
const createdAt = new Date().toISOString();
|
||||
@@ -1183,9 +1239,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const workspaceRoot = makeTempDir("t3code-ws-project-");
|
||||
const createdAt = new Date().toISOString();
|
||||
@@ -1276,9 +1331,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const open = await sendRequest(ws, WS_METHODS.terminalOpen, {
|
||||
threadId: "thread-1",
|
||||
@@ -1331,8 +1385,13 @@ describe("WebSocket Server", () => {
|
||||
};
|
||||
terminalManager.emitEvent(manualEvent);
|
||||
|
||||
const push = await waitForPush(ws, WS_CHANNELS.terminalEvent);
|
||||
expect((push.data as TerminalEvent).type).toBe("output");
|
||||
const push = await waitForPush(
|
||||
ws,
|
||||
WS_CHANNELS.terminalEvent,
|
||||
(candidate) => (candidate.data as TerminalEvent).type === "output",
|
||||
);
|
||||
expect(push.type).toBe("push");
|
||||
expect(push.channel).toBe(WS_CHANNELS.terminalEvent);
|
||||
});
|
||||
|
||||
it("detaches terminal event listener on stop for injected manager", async () => {
|
||||
@@ -1355,9 +1414,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.terminalOpen, {
|
||||
threadId: "",
|
||||
@@ -1373,21 +1431,17 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
|
||||
// Consume welcome
|
||||
await waitForMessage(ws);
|
||||
|
||||
// Send garbage
|
||||
ws.send("not json at all");
|
||||
|
||||
// Error response goes to the response channel
|
||||
const channels = channelsBySocket.get(ws)!;
|
||||
let response: WebSocketResponse | null = null;
|
||||
for (let attempt = 0; attempt < 5; attempt += 1) {
|
||||
const message = asWebSocketResponse(await waitForMessage(ws));
|
||||
if (!message) {
|
||||
continue;
|
||||
}
|
||||
const message = await dequeue(channels.response, 5_000);
|
||||
if (message.id === "unknown") {
|
||||
response = message;
|
||||
break;
|
||||
@@ -1420,9 +1474,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
@@ -1466,9 +1519,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const listResponse = await sendRequest(ws, WS_METHODS.projectsList);
|
||||
expect(listResponse.result).toBeUndefined();
|
||||
@@ -1503,9 +1555,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.projectsSearchEntries, {
|
||||
cwd: workspace,
|
||||
@@ -1529,9 +1580,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.projectsWriteFile, {
|
||||
cwd: workspace,
|
||||
@@ -1555,9 +1605,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.projectsWriteFile, {
|
||||
cwd: workspace,
|
||||
@@ -1603,9 +1652,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const listResponse = await sendRequest(ws, WS_METHODS.gitListBranches, { cwd: "/repo/path" });
|
||||
expect(listResponse.error).toBeUndefined();
|
||||
@@ -1652,9 +1700,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.gitStatus, {
|
||||
cwd: "/test",
|
||||
@@ -1692,9 +1739,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const resolveResponse = await sendRequest(ws, WS_METHODS.gitResolvePullRequest, {
|
||||
cwd: "/test",
|
||||
@@ -1741,9 +1787,8 @@ describe("WebSocket Server", () => {
|
||||
const addr = server.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
const [ws] = await connectAndAwaitWelcome(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const response = await sendRequest(ws, WS_METHODS.gitRunStackedAction, {
|
||||
cwd: "/test",
|
||||
@@ -1764,9 +1809,7 @@ describe("WebSocket Server", () => {
|
||||
|
||||
await expect(connectWs(port)).rejects.toThrow("WebSocket connection failed");
|
||||
|
||||
const authorizedWs = await connectWs(port, "secret-token");
|
||||
const [authorizedWs] = await connectAndAwaitWelcome(port, "secret-token");
|
||||
connections.push(authorizedWs);
|
||||
const welcome = (await waitForMessage(authorizedWs)) as WsPush;
|
||||
expect(welcome.channel).toBe(WS_CHANNELS.serverWelcome);
|
||||
});
|
||||
});
|
||||
+69
-102
@@ -20,13 +20,12 @@ import {
|
||||
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
|
||||
ProjectId,
|
||||
ThreadId,
|
||||
TerminalEvent,
|
||||
WS_CHANNELS,
|
||||
WS_METHODS,
|
||||
WebSocketRequest,
|
||||
WsPush,
|
||||
type WsResponse as WsResponseMessage,
|
||||
WsResponse,
|
||||
ServerProviderStatus,
|
||||
type WsPushEnvelopeBase,
|
||||
} from "@t3tools/contracts";
|
||||
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
|
||||
import {
|
||||
@@ -37,6 +36,7 @@ import {
|
||||
Layer,
|
||||
Path,
|
||||
Ref,
|
||||
Result,
|
||||
Schema,
|
||||
Scope,
|
||||
ServiceMap,
|
||||
@@ -66,6 +66,7 @@ import {
|
||||
normalizeAttachmentRelativePath,
|
||||
resolveAttachmentRelativePath,
|
||||
} from "./attachmentPaths";
|
||||
|
||||
import {
|
||||
createAttachmentId,
|
||||
resolveAttachmentPath,
|
||||
@@ -74,6 +75,9 @@ import {
|
||||
import { parseBase64DataUrl } from "./imageMime.ts";
|
||||
import { AnalyticsService } from "./telemetry/Services/AnalyticsService.ts";
|
||||
import { expandHomePath } from "./os-jank.ts";
|
||||
import { makeServerPushBus } from "./wsServer/pushBus.ts";
|
||||
import { makeServerReadiness } from "./wsServer/readiness.ts";
|
||||
import { decodeJsonResult, formatSchemaError } from "@t3tools/shared/schemaJson";
|
||||
|
||||
/**
|
||||
* ServerShape - Service API for server lifecycle control.
|
||||
@@ -195,6 +199,9 @@ function stripRequestTag<T extends { _tag: string }>(body: T) {
|
||||
return Struct.omit(body, ["_tag"]);
|
||||
}
|
||||
|
||||
const encodeWsResponse = Schema.encodeEffect(Schema.fromJsonString(WsResponse));
|
||||
const decodeWebSocketRequest = decodeJsonResult(WebSocketRequest);
|
||||
|
||||
export type ServerCoreRuntimeServices =
|
||||
| OrchestrationEngineService
|
||||
| ProjectionSnapshotQuery
|
||||
@@ -261,38 +268,33 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
),
|
||||
);
|
||||
|
||||
const providerStatuses = yield* providerHealth.getStatuses;
|
||||
|
||||
const clients = yield* Ref.make(new Set<WebSocket>());
|
||||
const logger = createLogger("ws");
|
||||
const readiness = yield* makeServerReadiness;
|
||||
|
||||
function logOutgoingPush(push: WsPush, recipients: number) {
|
||||
function logOutgoingPush(push: WsPushEnvelopeBase, recipients: number) {
|
||||
if (!logWebSocketEvents) return;
|
||||
logger.event("outgoing push", {
|
||||
channel: push.channel,
|
||||
sequence: push.sequence,
|
||||
recipients,
|
||||
payload: push.data,
|
||||
});
|
||||
}
|
||||
|
||||
const encodePush = Schema.encodeEffect(Schema.fromJsonString(WsPush));
|
||||
const broadcastPush = Effect.fnUntraced(function* (push: WsPush) {
|
||||
const message = yield* encodePush(push);
|
||||
let recipients = 0;
|
||||
for (const client of yield* Ref.get(clients)) {
|
||||
if (client.readyState === client.OPEN) {
|
||||
client.send(message);
|
||||
recipients += 1;
|
||||
}
|
||||
}
|
||||
logOutgoingPush(push, recipients);
|
||||
});
|
||||
|
||||
const onTerminalEvent = Effect.fnUntraced(function* (event: TerminalEvent) {
|
||||
yield* broadcastPush({
|
||||
type: "push",
|
||||
channel: WS_CHANNELS.terminalEvent,
|
||||
data: event,
|
||||
});
|
||||
const pushBus = yield* makeServerPushBus({
|
||||
clients,
|
||||
logOutgoingPush,
|
||||
});
|
||||
yield* readiness.markPushBusReady;
|
||||
yield* keybindingsManager.start.pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ServerLifecycleError({ operation: "keybindingsRuntimeStart", cause }),
|
||||
),
|
||||
);
|
||||
yield* readiness.markKeybindingsReady;
|
||||
|
||||
const normalizeDispatchCommand = Effect.fnUntraced(function* (input: {
|
||||
readonly command: ClientOrchestrationCommand;
|
||||
@@ -474,7 +476,7 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
}
|
||||
}),
|
||||
).pipe(Effect.exit);
|
||||
if (streamExit._tag === "Failure") {
|
||||
if (Exit.isFailure(streamExit)) {
|
||||
if (!res.destroyed) {
|
||||
res.destroy();
|
||||
}
|
||||
@@ -605,43 +607,19 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
const subscriptionsScope = yield* Scope.make("sequential");
|
||||
yield* Effect.addFinalizer(() => Scope.close(subscriptionsScope, Exit.void));
|
||||
|
||||
// Push updated provider statuses to connected clients once background health checks finish.
|
||||
let providers: ReadonlyArray<ServerProviderStatus> = [];
|
||||
yield* providerHealth.getStatuses.pipe(
|
||||
Effect.flatMap((statuses) => {
|
||||
providers = statuses;
|
||||
return broadcastPush({
|
||||
type: "push",
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: {
|
||||
issues: [],
|
||||
providers: statuses,
|
||||
},
|
||||
});
|
||||
}),
|
||||
Effect.forkIn(subscriptionsScope),
|
||||
);
|
||||
|
||||
yield* Stream.runForEach(orchestrationEngine.streamDomainEvents, (event) =>
|
||||
broadcastPush({
|
||||
type: "push",
|
||||
channel: ORCHESTRATION_WS_CHANNELS.domainEvent,
|
||||
data: event,
|
||||
}),
|
||||
pushBus.publishAll(ORCHESTRATION_WS_CHANNELS.domainEvent, event),
|
||||
).pipe(Effect.forkIn(subscriptionsScope));
|
||||
|
||||
yield* Stream.runForEach(keybindingsManager.changes, (event) =>
|
||||
broadcastPush({
|
||||
type: "push",
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: {
|
||||
issues: event.issues,
|
||||
providers,
|
||||
},
|
||||
yield* Stream.runForEach(keybindingsManager.streamChanges, (event) =>
|
||||
pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, {
|
||||
issues: event.issues,
|
||||
providers: providerStatuses,
|
||||
}),
|
||||
).pipe(Effect.forkIn(subscriptionsScope));
|
||||
|
||||
yield* Scope.provide(orchestrationReactor.start, subscriptionsScope);
|
||||
yield* readiness.markOrchestrationSubscriptionsReady;
|
||||
|
||||
let welcomeBootstrapProjectId: ProjectId | undefined;
|
||||
let welcomeBootstrapThreadId: ThreadId | undefined;
|
||||
@@ -712,23 +690,18 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
const runPromise = Effect.runPromiseWith(runtimeServices);
|
||||
|
||||
const unsubscribeTerminalEvents = yield* terminalManager.subscribe(
|
||||
(event) => void Effect.runPromise(onTerminalEvent(event)),
|
||||
(event) => void Effect.runPromise(pushBus.publishAll(WS_CHANNELS.terminalEvent, event)),
|
||||
);
|
||||
yield* Effect.addFinalizer(() => Effect.sync(() => unsubscribeTerminalEvents()));
|
||||
yield* readiness.markTerminalSubscriptionsReady;
|
||||
|
||||
yield* NodeHttpServer.make(() => httpServer, listenOptions).pipe(
|
||||
Effect.mapError((cause) => new ServerLifecycleError({ operation: "httpServerListen", cause })),
|
||||
);
|
||||
yield* readiness.markHttpListening;
|
||||
|
||||
yield* Effect.addFinalizer(() =>
|
||||
Effect.all([
|
||||
closeAllClients,
|
||||
closeWebSocketServer.pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.logWarning("failed to close web socket server", { cause: error }),
|
||||
),
|
||||
),
|
||||
]),
|
||||
Effect.all([closeAllClients, closeWebSocketServer.pipe(Effect.ignoreCause({ log: true }))]),
|
||||
);
|
||||
|
||||
const routeRequest = Effect.fnUntraced(function* (request: WebSocketRequest) {
|
||||
@@ -900,7 +873,7 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
keybindingsConfigPath,
|
||||
keybindings: keybindingsConfig.keybindings,
|
||||
issues: keybindingsConfig.issues,
|
||||
providers,
|
||||
providers: providerStatuses,
|
||||
availableEditors,
|
||||
};
|
||||
|
||||
@@ -920,44 +893,40 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
});
|
||||
|
||||
const handleMessage = Effect.fnUntraced(function* (ws: WebSocket, raw: unknown) {
|
||||
const encodeResponse = Schema.encodeEffect(Schema.fromJsonString(WsResponse));
|
||||
const sendWsResponse = (response: WsResponseMessage) =>
|
||||
encodeWsResponse(response).pipe(
|
||||
Effect.tap((encodedResponse) => Effect.sync(() => ws.send(encodedResponse))),
|
||||
Effect.asVoid,
|
||||
);
|
||||
|
||||
const messageText = websocketRawToString(raw);
|
||||
if (messageText === null) {
|
||||
const errorResponse = yield* encodeResponse({
|
||||
return yield* sendWsResponse({
|
||||
id: "unknown",
|
||||
error: { message: "Invalid request format: Failed to read message" },
|
||||
});
|
||||
ws.send(errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const request = Schema.decodeExit(Schema.fromJsonString(WebSocketRequest))(messageText);
|
||||
if (request._tag === "Failure") {
|
||||
const errorResponse = yield* encodeResponse({
|
||||
const request = decodeWebSocketRequest(messageText);
|
||||
if (Result.isFailure(request)) {
|
||||
return yield* sendWsResponse({
|
||||
id: "unknown",
|
||||
error: { message: `Invalid request format: ${Cause.pretty(request.cause)}` },
|
||||
error: { message: `Invalid request format: ${formatSchemaError(request.failure)}` },
|
||||
});
|
||||
ws.send(errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const result = yield* Effect.exit(routeRequest(request.value));
|
||||
if (result._tag === "Failure") {
|
||||
const errorResponse = yield* encodeResponse({
|
||||
id: request.value.id,
|
||||
const result = yield* Effect.exit(routeRequest(request.success));
|
||||
if (Exit.isFailure(result)) {
|
||||
return yield* sendWsResponse({
|
||||
id: request.success.id,
|
||||
error: { message: Cause.pretty(result.cause) },
|
||||
});
|
||||
ws.send(errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const response = yield* encodeResponse({
|
||||
id: request.value.id,
|
||||
return yield* sendWsResponse({
|
||||
id: request.success.id,
|
||||
result: result.value,
|
||||
});
|
||||
|
||||
ws.send(response);
|
||||
});
|
||||
|
||||
httpServer.on("upgrade", (request, socket, head) => {
|
||||
@@ -985,30 +954,28 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
});
|
||||
|
||||
wss.on("connection", (ws) => {
|
||||
void runPromise(Ref.update(clients, (clients) => clients.add(ws)));
|
||||
|
||||
const segments = cwd.split(/[/\\]/).filter(Boolean);
|
||||
const projectName = segments[segments.length - 1] ?? "project";
|
||||
|
||||
const welcome: WsPush = {
|
||||
type: "push",
|
||||
channel: WS_CHANNELS.serverWelcome,
|
||||
data: {
|
||||
cwd,
|
||||
projectName,
|
||||
...(welcomeBootstrapProjectId ? { bootstrapProjectId: welcomeBootstrapProjectId } : {}),
|
||||
...(welcomeBootstrapThreadId ? { bootstrapThreadId: welcomeBootstrapThreadId } : {}),
|
||||
},
|
||||
const welcomeData = {
|
||||
cwd,
|
||||
projectName,
|
||||
...(welcomeBootstrapProjectId ? { bootstrapProjectId: welcomeBootstrapProjectId } : {}),
|
||||
...(welcomeBootstrapThreadId ? { bootstrapThreadId: welcomeBootstrapThreadId } : {}),
|
||||
};
|
||||
logOutgoingPush(welcome, 1);
|
||||
ws.send(JSON.stringify(welcome));
|
||||
// Send welcome before adding to broadcast set so publishAll calls
|
||||
// cannot reach this client before the welcome arrives.
|
||||
void runPromise(
|
||||
readiness.awaitServerReady.pipe(
|
||||
Effect.flatMap(() => pushBus.publishClient(ws, WS_CHANNELS.serverWelcome, welcomeData)),
|
||||
Effect.flatMap((delivered) =>
|
||||
delivered ? Ref.update(clients, (clients) => clients.add(ws)) : Effect.void,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
ws.on("message", (raw) => {
|
||||
void runPromise(
|
||||
handleMessage(ws, raw).pipe(
|
||||
Effect.catch((error) => Effect.logError("Error handling message", error)),
|
||||
),
|
||||
);
|
||||
void runPromise(handleMessage(ws, raw).pipe(Effect.ignoreCause({ log: true })));
|
||||
});
|
||||
|
||||
ws.on("close", () => {
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import type { WebSocket } from "ws";
|
||||
import { it } from "@effect/vitest";
|
||||
import { describe, expect } from "vitest";
|
||||
import { Effect, Ref } from "effect";
|
||||
import { WS_CHANNELS } from "@t3tools/contracts";
|
||||
|
||||
import { makeServerPushBus } from "./pushBus";
|
||||
|
||||
class MockWebSocket {
|
||||
static readonly OPEN = 1;
|
||||
|
||||
readonly OPEN = MockWebSocket.OPEN;
|
||||
readyState = MockWebSocket.OPEN;
|
||||
readonly sent: string[] = [];
|
||||
private readonly waiters = new Set<() => void>();
|
||||
|
||||
send(message: string) {
|
||||
this.sent.push(message);
|
||||
for (const waiter of this.waiters) {
|
||||
waiter();
|
||||
}
|
||||
}
|
||||
|
||||
waitForSentCount(count: number): Promise<void> {
|
||||
if (this.sent.length >= count) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const check = () => {
|
||||
if (this.sent.length < count) {
|
||||
return;
|
||||
}
|
||||
this.waiters.delete(check);
|
||||
resolve();
|
||||
};
|
||||
|
||||
this.waiters.add(check);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
describe("makeServerPushBus", () => {
|
||||
it.live("waits for the welcome push before a new client joins broadcast delivery", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const client = new MockWebSocket();
|
||||
const clients = yield* Ref.make(new Set<WebSocket>());
|
||||
const pushBus = yield* makeServerPushBus({
|
||||
clients,
|
||||
logOutgoingPush: () => {},
|
||||
});
|
||||
|
||||
yield* pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, {
|
||||
issues: [{ kind: "keybindings.malformed-config", message: "queued-before-connect" }],
|
||||
providers: [],
|
||||
});
|
||||
|
||||
const delivered = yield* pushBus.publishClient(
|
||||
client as unknown as WebSocket,
|
||||
WS_CHANNELS.serverWelcome,
|
||||
{
|
||||
cwd: "/tmp/project",
|
||||
projectName: "project",
|
||||
},
|
||||
);
|
||||
expect(delivered).toBe(true);
|
||||
|
||||
yield* Ref.update(clients, (current) => current.add(client as unknown as WebSocket));
|
||||
|
||||
yield* pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, {
|
||||
issues: [],
|
||||
providers: [],
|
||||
});
|
||||
|
||||
yield* Effect.promise(() => client.waitForSentCount(2));
|
||||
|
||||
const messages = client.sent.map(
|
||||
(message) => JSON.parse(message) as { channel: string; data: unknown },
|
||||
);
|
||||
|
||||
expect(messages).toHaveLength(2);
|
||||
expect(messages[0]).toEqual({
|
||||
type: "push",
|
||||
sequence: 2,
|
||||
channel: WS_CHANNELS.serverWelcome,
|
||||
data: {
|
||||
cwd: "/tmp/project",
|
||||
projectName: "project",
|
||||
},
|
||||
});
|
||||
expect(messages[1]).toEqual({
|
||||
type: "push",
|
||||
sequence: 3,
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: {
|
||||
issues: [],
|
||||
providers: [],
|
||||
},
|
||||
});
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,114 @@
|
||||
import {
|
||||
WsPush,
|
||||
type WsPushChannel,
|
||||
type WsPushData,
|
||||
type WsPushEnvelopeBase,
|
||||
} from "@t3tools/contracts";
|
||||
import { Deferred, Effect, Queue, Ref, Schema } from "effect";
|
||||
import type { Scope } from "effect";
|
||||
import type { WebSocket } from "ws";
|
||||
|
||||
type PushTarget =
|
||||
| { readonly kind: "all" }
|
||||
| { readonly kind: "client"; readonly client: WebSocket };
|
||||
|
||||
interface PushJob<C extends WsPushChannel = WsPushChannel> {
|
||||
readonly channel: C;
|
||||
readonly data: WsPushData<C>;
|
||||
readonly target: PushTarget;
|
||||
readonly delivered: Deferred.Deferred<boolean> | null;
|
||||
}
|
||||
|
||||
export interface ServerPushBus {
|
||||
readonly publishAll: <C extends WsPushChannel>(
|
||||
channel: C,
|
||||
data: WsPushData<C>,
|
||||
) => Effect.Effect<void>;
|
||||
readonly publishClient: <C extends WsPushChannel>(
|
||||
client: WebSocket,
|
||||
channel: C,
|
||||
data: WsPushData<C>,
|
||||
) => Effect.Effect<boolean>;
|
||||
}
|
||||
|
||||
export const makeServerPushBus = (input: {
|
||||
readonly clients: Ref.Ref<Set<WebSocket>>;
|
||||
readonly logOutgoingPush: (push: WsPushEnvelopeBase, recipients: number) => void;
|
||||
}): Effect.Effect<ServerPushBus, never, Scope.Scope> =>
|
||||
Effect.gen(function* () {
|
||||
const nextSequence = yield* Ref.make(0);
|
||||
const queue = yield* Queue.unbounded<PushJob>();
|
||||
const encodePush = Schema.encodeUnknownEffect(Schema.fromJsonString(WsPush));
|
||||
|
||||
const settleDelivery = (job: PushJob, delivered: boolean) =>
|
||||
job.delivered === null
|
||||
? Effect.void
|
||||
: Deferred.succeed(job.delivered, delivered).pipe(Effect.orDie);
|
||||
|
||||
const send = Effect.fnUntraced(function* (job: PushJob) {
|
||||
const sequence = yield* Ref.updateAndGet(nextSequence, (current) => current + 1);
|
||||
const push: WsPushEnvelopeBase = {
|
||||
type: "push",
|
||||
sequence,
|
||||
channel: job.channel,
|
||||
data: job.data,
|
||||
};
|
||||
const recipients =
|
||||
job.target.kind === "all" ? yield* Ref.get(input.clients) : new Set([job.target.client]);
|
||||
|
||||
return yield* encodePush(push).pipe(
|
||||
Effect.map((message) => {
|
||||
let recipientCount = 0;
|
||||
for (const client of recipients) {
|
||||
if (client.readyState !== client.OPEN) {
|
||||
continue;
|
||||
}
|
||||
client.send(message);
|
||||
recipientCount += 1;
|
||||
}
|
||||
|
||||
input.logOutgoingPush(push, recipientCount);
|
||||
return recipientCount > 0;
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
yield* Effect.forkScoped(
|
||||
Effect.forever(
|
||||
Queue.take(queue).pipe(
|
||||
Effect.flatMap((job) =>
|
||||
send(job).pipe(
|
||||
Effect.tap((delivered) => settleDelivery(job, delivered)),
|
||||
Effect.tapCause(() => settleDelivery(job, false)),
|
||||
Effect.ignoreCause({ log: true }),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const publish =
|
||||
(target: PushTarget) =>
|
||||
<C extends WsPushChannel>(channel: C, data: WsPushData<C>) =>
|
||||
Queue.offer(queue, {
|
||||
channel,
|
||||
data,
|
||||
target,
|
||||
delivered: null,
|
||||
}).pipe(Effect.asVoid);
|
||||
|
||||
return {
|
||||
publishAll: publish({ kind: "all" }),
|
||||
publishClient: (client, channel, data) =>
|
||||
Effect.gen(function* () {
|
||||
const delivered = yield* Deferred.make<boolean>();
|
||||
yield* Queue.offer(queue, {
|
||||
channel,
|
||||
data,
|
||||
target: { kind: "client", client },
|
||||
delivered,
|
||||
}).pipe(Effect.asVoid);
|
||||
return yield* Deferred.await(delivered);
|
||||
}),
|
||||
} satisfies ServerPushBus;
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Deferred, Effect } from "effect";
|
||||
|
||||
export interface ServerReadiness {
|
||||
readonly awaitServerReady: Effect.Effect<void>;
|
||||
readonly markHttpListening: Effect.Effect<void>;
|
||||
readonly markPushBusReady: Effect.Effect<void>;
|
||||
readonly markKeybindingsReady: Effect.Effect<void>;
|
||||
readonly markTerminalSubscriptionsReady: Effect.Effect<void>;
|
||||
readonly markOrchestrationSubscriptionsReady: Effect.Effect<void>;
|
||||
}
|
||||
|
||||
export const makeServerReadiness = Effect.gen(function* () {
|
||||
const httpListening = yield* Deferred.make<void>();
|
||||
const pushBusReady = yield* Deferred.make<void>();
|
||||
const keybindingsReady = yield* Deferred.make<void>();
|
||||
const terminalSubscriptionsReady = yield* Deferred.make<void>();
|
||||
const orchestrationSubscriptionsReady = yield* Deferred.make<void>();
|
||||
|
||||
const complete = (deferred: Deferred.Deferred<void>) =>
|
||||
Deferred.succeed(deferred, undefined).pipe(Effect.orDie);
|
||||
|
||||
return {
|
||||
awaitServerReady: Effect.all([
|
||||
Deferred.await(httpListening),
|
||||
Deferred.await(pushBusReady),
|
||||
Deferred.await(keybindingsReady),
|
||||
Deferred.await(terminalSubscriptionsReady),
|
||||
Deferred.await(orchestrationSubscriptionsReady),
|
||||
]).pipe(Effect.asVoid),
|
||||
markHttpListening: complete(httpListening),
|
||||
markPushBusReady: complete(pushBusReady),
|
||||
markKeybindingsReady: complete(keybindingsReady),
|
||||
markTerminalSubscriptionsReady: complete(terminalSubscriptionsReady),
|
||||
markOrchestrationSubscriptionsReady: complete(orchestrationSubscriptionsReady),
|
||||
} satisfies ServerReadiness;
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
import { defineConfig, mergeConfig } from "vitest/config";
|
||||
|
||||
import baseConfig from "../../vitest.config";
|
||||
|
||||
export default mergeConfig(
|
||||
baseConfig,
|
||||
defineConfig({
|
||||
test: {
|
||||
testTimeout: 15_000,
|
||||
hookTimeout: 15_000,
|
||||
},
|
||||
}),
|
||||
);
|
||||
@@ -25,6 +25,7 @@ import { useStore } from "../store";
|
||||
import { estimateTimelineMessageHeight } from "./timelineHeight";
|
||||
|
||||
const THREAD_ID = "thread-browser-test" as ThreadId;
|
||||
const UUID_ROUTE_RE = /^\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
const PROJECT_ID = "project-1" as ProjectId;
|
||||
const NOW_ISO = "2026-03-04T12:00:00.000Z";
|
||||
const BASE_TIME_MS = Date.parse(NOW_ISO);
|
||||
@@ -85,6 +86,7 @@ interface MountedChatView {
|
||||
cleanup: () => Promise<void>;
|
||||
measureUserRow: (targetMessageId: MessageId) => Promise<UserRowMeasurement>;
|
||||
setViewport: (viewport: ViewportSpec) => Promise<void>;
|
||||
router: ReturnType<typeof getRouter>;
|
||||
}
|
||||
|
||||
function isoAt(offsetSeconds: number): string {
|
||||
@@ -245,6 +247,46 @@ function buildFixture(snapshot: OrchestrationReadModel): TestFixture {
|
||||
};
|
||||
}
|
||||
|
||||
function addThreadToSnapshot(
|
||||
snapshot: OrchestrationReadModel,
|
||||
threadId: ThreadId,
|
||||
): OrchestrationReadModel {
|
||||
return {
|
||||
...snapshot,
|
||||
snapshotSequence: snapshot.snapshotSequence + 1,
|
||||
threads: [
|
||||
...snapshot.threads,
|
||||
{
|
||||
id: threadId,
|
||||
projectId: PROJECT_ID,
|
||||
title: "New thread",
|
||||
model: "gpt-5",
|
||||
interactionMode: "default",
|
||||
runtimeMode: "full-access",
|
||||
branch: "main",
|
||||
worktreePath: null,
|
||||
latestTurn: null,
|
||||
createdAt: NOW_ISO,
|
||||
updatedAt: NOW_ISO,
|
||||
deletedAt: null,
|
||||
messages: [],
|
||||
activities: [],
|
||||
proposedPlans: [],
|
||||
checkpoints: [],
|
||||
session: {
|
||||
threadId,
|
||||
status: "ready",
|
||||
providerName: "codex",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: null,
|
||||
updatedAt: NOW_ISO,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function createDraftOnlySnapshot(): OrchestrationReadModel {
|
||||
const snapshot = createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-draft-target" as MessageId,
|
||||
@@ -361,6 +403,7 @@ const worker = setupWorker(
|
||||
client.send(
|
||||
JSON.stringify({
|
||||
type: "push",
|
||||
sequence: 1,
|
||||
channel: WS_CHANNELS.serverWelcome,
|
||||
data: fixture.welcome,
|
||||
}),
|
||||
@@ -448,6 +491,22 @@ async function waitForElement<T extends Element>(
|
||||
return element;
|
||||
}
|
||||
|
||||
async function waitForURL(
|
||||
router: ReturnType<typeof getRouter>,
|
||||
predicate: (pathname: string) => boolean,
|
||||
errorMessage: string,
|
||||
): Promise<string> {
|
||||
let pathname = "";
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
pathname = router.state.location.pathname;
|
||||
expect(predicate(pathname), errorMessage).toBe(true);
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
return pathname;
|
||||
}
|
||||
|
||||
async function waitForComposerEditor(): Promise<HTMLElement> {
|
||||
return waitForElement(
|
||||
() => document.querySelector<HTMLElement>('[contenteditable="true"]'),
|
||||
@@ -593,6 +652,7 @@ async function mountChatView(options: {
|
||||
await setViewport(viewport);
|
||||
await waitForProductionStyles();
|
||||
},
|
||||
router,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -934,6 +994,60 @@ describe("ChatView timeline estimator parity (full app)", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the new thread selected after clicking the new-thread button", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
snapshot: createSnapshotForTargetUser({
|
||||
targetMessageId: "msg-user-new-thread-test" as MessageId,
|
||||
targetText: "new thread selection test",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
// Wait for the sidebar to render with the project.
|
||||
const newThreadButton = page.getByTestId("new-thread-button");
|
||||
await expect.element(newThreadButton).toBeInTheDocument();
|
||||
|
||||
await newThreadButton.click();
|
||||
|
||||
// The route should change to a new draft thread ID.
|
||||
const newThreadPath = await waitForURL(
|
||||
mounted.router,
|
||||
(path) => UUID_ROUTE_RE.test(path),
|
||||
"Route should have changed to a new draft thread UUID.",
|
||||
);
|
||||
const newThreadId = newThreadPath.slice(1) as ThreadId;
|
||||
|
||||
// The composer editor should be present for the new draft thread.
|
||||
await waitForComposerEditor();
|
||||
|
||||
// Simulate the snapshot sync arriving from the server after the draft
|
||||
// thread has been promoted to a server thread (thread.create + turn.start
|
||||
// succeeded). The snapshot now includes the new thread, and the sync
|
||||
// should clear the draft without disrupting the route.
|
||||
const { syncServerReadModel } = useStore.getState();
|
||||
syncServerReadModel(addThreadToSnapshot(fixture.snapshot, newThreadId));
|
||||
|
||||
// Clear the draft now that the server thread exists (mirrors EventRouter behavior).
|
||||
useComposerDraftStore.getState().clearDraftThread(newThreadId);
|
||||
|
||||
// The route should still be on the new thread — not redirected away.
|
||||
await waitForURL(
|
||||
mounted.router,
|
||||
(path) => path === newThreadPath,
|
||||
"New thread should remain selected after snapshot sync clears the draft.",
|
||||
);
|
||||
|
||||
// The empty thread view and composer should still be visible.
|
||||
await expect
|
||||
.element(page.getByText("Send a message to start the conversation."))
|
||||
.toBeInTheDocument();
|
||||
await expect.element(page.getByTestId("composer-editor")).toBeInTheDocument();
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps long proposed plans lightweight until the user expands them", async () => {
|
||||
const mounted = await mountChatView({
|
||||
viewport: DEFAULT_VIEWPORT,
|
||||
|
||||
@@ -613,7 +613,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
(store) => store.syncPersistedAttachments,
|
||||
);
|
||||
const clearComposerDraftContent = useComposerDraftStore((store) => store.clearComposerContent);
|
||||
const clearDraftThread = useComposerDraftStore((store) => store.clearDraftThread);
|
||||
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
|
||||
const getDraftThreadByProjectId = useComposerDraftStore(
|
||||
(store) => store.getDraftThreadByProjectId,
|
||||
@@ -2788,9 +2787,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
createdAt: messageCreatedAt,
|
||||
});
|
||||
turnStartSucceeded = true;
|
||||
if (isFirstMessage) {
|
||||
clearDraftThread(threadIdForSend);
|
||||
}
|
||||
})().catch(async (err: unknown) => {
|
||||
if (createdServerThreadForLocalDraft && !turnStartSucceeded) {
|
||||
await api.orchestration
|
||||
|
||||
@@ -748,6 +748,7 @@ function ComposerPromptEditorInner({
|
||||
"block max-h-[200px] min-h-17.5 w-full overflow-y-auto whitespace-pre-wrap break-words bg-transparent text-[14px] leading-relaxed text-foreground focus:outline-none",
|
||||
className,
|
||||
)}
|
||||
data-testid="composer-editor"
|
||||
aria-placeholder={placeholder}
|
||||
placeholder={<span />}
|
||||
onPaste={onPaste}
|
||||
|
||||
@@ -0,0 +1,371 @@
|
||||
import "../index.css";
|
||||
|
||||
import {
|
||||
ORCHESTRATION_WS_METHODS,
|
||||
type MessageId,
|
||||
type OrchestrationReadModel,
|
||||
type ProjectId,
|
||||
type ServerConfig,
|
||||
type ThreadId,
|
||||
type WsWelcomePayload,
|
||||
WS_CHANNELS,
|
||||
WS_METHODS,
|
||||
} from "@t3tools/contracts";
|
||||
import { RouterProvider, createMemoryHistory } from "@tanstack/react-router";
|
||||
import { ws, http, HttpResponse } from "msw";
|
||||
import { setupWorker } from "msw/browser";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { render } from "vitest-browser-react";
|
||||
|
||||
import { useComposerDraftStore } from "../composerDraftStore";
|
||||
import { getRouter } from "../router";
|
||||
import { useStore } from "../store";
|
||||
|
||||
const THREAD_ID = "thread-kb-toast-test" as ThreadId;
|
||||
const PROJECT_ID = "project-1" as ProjectId;
|
||||
const NOW_ISO = "2026-03-04T12:00:00.000Z";
|
||||
|
||||
interface TestFixture {
|
||||
snapshot: OrchestrationReadModel;
|
||||
serverConfig: ServerConfig;
|
||||
welcome: WsWelcomePayload;
|
||||
}
|
||||
|
||||
let fixture: TestFixture;
|
||||
let wsClient: { send: (data: string) => void } | null = null;
|
||||
let pushSequence = 1;
|
||||
|
||||
const wsLink = ws.link(/ws(s)?:\/\/.*/);
|
||||
|
||||
function createBaseServerConfig(): ServerConfig {
|
||||
return {
|
||||
cwd: "/repo/project",
|
||||
keybindingsConfigPath: "/repo/project/.t3code-keybindings.json",
|
||||
keybindings: [],
|
||||
issues: [],
|
||||
providers: [
|
||||
{
|
||||
provider: "codex",
|
||||
status: "ready",
|
||||
available: true,
|
||||
authStatus: "authenticated",
|
||||
checkedAt: NOW_ISO,
|
||||
},
|
||||
],
|
||||
availableEditors: [],
|
||||
};
|
||||
}
|
||||
|
||||
function createMinimalSnapshot(): OrchestrationReadModel {
|
||||
return {
|
||||
snapshotSequence: 1,
|
||||
projects: [
|
||||
{
|
||||
id: PROJECT_ID,
|
||||
title: "Project",
|
||||
workspaceRoot: "/repo/project",
|
||||
defaultModel: "gpt-5",
|
||||
scripts: [],
|
||||
createdAt: NOW_ISO,
|
||||
updatedAt: NOW_ISO,
|
||||
deletedAt: null,
|
||||
},
|
||||
],
|
||||
threads: [
|
||||
{
|
||||
id: THREAD_ID,
|
||||
projectId: PROJECT_ID,
|
||||
title: "Test thread",
|
||||
model: "gpt-5",
|
||||
interactionMode: "default",
|
||||
runtimeMode: "full-access",
|
||||
branch: "main",
|
||||
worktreePath: null,
|
||||
latestTurn: null,
|
||||
createdAt: NOW_ISO,
|
||||
updatedAt: NOW_ISO,
|
||||
deletedAt: null,
|
||||
messages: [
|
||||
{
|
||||
id: "msg-1" as MessageId,
|
||||
role: "user",
|
||||
text: "hello",
|
||||
turnId: null,
|
||||
streaming: false,
|
||||
createdAt: NOW_ISO,
|
||||
updatedAt: NOW_ISO,
|
||||
},
|
||||
],
|
||||
activities: [],
|
||||
proposedPlans: [],
|
||||
checkpoints: [],
|
||||
session: {
|
||||
threadId: THREAD_ID,
|
||||
status: "ready",
|
||||
providerName: "codex",
|
||||
runtimeMode: "full-access",
|
||||
activeTurnId: null,
|
||||
lastError: null,
|
||||
updatedAt: NOW_ISO,
|
||||
},
|
||||
},
|
||||
],
|
||||
updatedAt: NOW_ISO,
|
||||
};
|
||||
}
|
||||
|
||||
function buildFixture(): TestFixture {
|
||||
return {
|
||||
snapshot: createMinimalSnapshot(),
|
||||
serverConfig: createBaseServerConfig(),
|
||||
welcome: {
|
||||
cwd: "/repo/project",
|
||||
projectName: "Project",
|
||||
bootstrapProjectId: PROJECT_ID,
|
||||
bootstrapThreadId: THREAD_ID,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function resolveWsRpc(tag: string): unknown {
|
||||
if (tag === ORCHESTRATION_WS_METHODS.getSnapshot) {
|
||||
return fixture.snapshot;
|
||||
}
|
||||
if (tag === WS_METHODS.serverGetConfig) {
|
||||
return fixture.serverConfig;
|
||||
}
|
||||
if (tag === WS_METHODS.gitListBranches) {
|
||||
return {
|
||||
isRepo: true,
|
||||
hasOriginRemote: true,
|
||||
branches: [{ name: "main", current: true, isDefault: true, worktreePath: null }],
|
||||
};
|
||||
}
|
||||
if (tag === WS_METHODS.gitStatus) {
|
||||
return {
|
||||
branch: "main",
|
||||
hasWorkingTreeChanges: false,
|
||||
workingTree: { files: [], insertions: 0, deletions: 0 },
|
||||
hasUpstream: true,
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
pr: null,
|
||||
};
|
||||
}
|
||||
if (tag === WS_METHODS.projectsSearchEntries) {
|
||||
return { entries: [], truncated: false };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
const worker = setupWorker(
|
||||
wsLink.addEventListener("connection", ({ client }) => {
|
||||
wsClient = client;
|
||||
pushSequence = 1;
|
||||
client.send(
|
||||
JSON.stringify({
|
||||
type: "push",
|
||||
sequence: pushSequence++,
|
||||
channel: WS_CHANNELS.serverWelcome,
|
||||
data: fixture.welcome,
|
||||
}),
|
||||
);
|
||||
client.addEventListener("message", (event) => {
|
||||
const rawData = event.data;
|
||||
if (typeof rawData !== "string") return;
|
||||
let request: { id: string; body: { _tag: string; [key: string]: unknown } };
|
||||
try {
|
||||
request = JSON.parse(rawData);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const method = request.body?._tag;
|
||||
if (typeof method !== "string") return;
|
||||
client.send(
|
||||
JSON.stringify({
|
||||
id: request.id,
|
||||
result: resolveWsRpc(method),
|
||||
}),
|
||||
);
|
||||
});
|
||||
}),
|
||||
http.get("*/attachments/:attachmentId", () => new HttpResponse(null, { status: 204 })),
|
||||
http.get("*/api/project-favicon", () => new HttpResponse(null, { status: 204 })),
|
||||
);
|
||||
|
||||
function sendServerConfigUpdatedPush(issues: Array<{ kind: string; message: string }>) {
|
||||
if (!wsClient) throw new Error("WebSocket client not connected");
|
||||
wsClient.send(
|
||||
JSON.stringify({
|
||||
type: "push",
|
||||
sequence: pushSequence++,
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: {
|
||||
issues,
|
||||
providers: fixture.serverConfig.providers,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function queryToastTitles(): string[] {
|
||||
return Array.from(document.querySelectorAll('[data-slot="toast-title"]')).map(
|
||||
(el) => el.textContent ?? "",
|
||||
);
|
||||
}
|
||||
|
||||
async function waitForElement<T extends Element>(
|
||||
query: () => T | null,
|
||||
errorMessage: string,
|
||||
): Promise<T> {
|
||||
let element: T | null = null;
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
element = query();
|
||||
expect(element, errorMessage).toBeTruthy();
|
||||
},
|
||||
{ timeout: 8_000, interval: 16 },
|
||||
);
|
||||
return element!;
|
||||
}
|
||||
|
||||
async function waitForComposerEditor(): Promise<HTMLElement> {
|
||||
return waitForElement(
|
||||
() => document.querySelector<HTMLElement>('[data-testid="composer-editor"]'),
|
||||
"App should render composer editor",
|
||||
);
|
||||
}
|
||||
|
||||
async function waitForToast(title: string, count = 1): Promise<void> {
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
const matches = queryToastTitles().filter((t) => t === title);
|
||||
expect(matches.length, `Expected ${count} "${title}" toast(s)`).toBeGreaterThanOrEqual(count);
|
||||
},
|
||||
{ timeout: 4_000, interval: 16 },
|
||||
);
|
||||
}
|
||||
|
||||
async function waitForNoToast(title: string): Promise<void> {
|
||||
await vi.waitFor(
|
||||
() => {
|
||||
expect(queryToastTitles().filter((t) => t === title)).toHaveLength(0);
|
||||
},
|
||||
{ timeout: 10_000, interval: 50 },
|
||||
);
|
||||
}
|
||||
|
||||
async function mountApp(): Promise<{ cleanup: () => Promise<void> }> {
|
||||
const host = document.createElement("div");
|
||||
host.style.position = "fixed";
|
||||
host.style.inset = "0";
|
||||
host.style.width = "100vw";
|
||||
host.style.height = "100vh";
|
||||
host.style.display = "grid";
|
||||
host.style.overflow = "hidden";
|
||||
document.body.append(host);
|
||||
|
||||
const router = getRouter(createMemoryHistory({ initialEntries: [`/${THREAD_ID}`] }));
|
||||
|
||||
const screen = await render(<RouterProvider router={router} />, { container: host });
|
||||
await waitForComposerEditor();
|
||||
|
||||
return {
|
||||
cleanup: async () => {
|
||||
await screen.unmount();
|
||||
host.remove();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("Keybindings update toast", () => {
|
||||
beforeAll(async () => {
|
||||
fixture = buildFixture();
|
||||
await worker.start({
|
||||
onUnhandledRequest: "bypass",
|
||||
quiet: true,
|
||||
serviceWorker: { url: "/mockServiceWorker.js" },
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await worker.stop();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
document.body.innerHTML = "";
|
||||
pushSequence = 1;
|
||||
useComposerDraftStore.setState({
|
||||
draftsByThreadId: {},
|
||||
draftThreadsByThreadId: {},
|
||||
projectDraftThreadIdByProjectId: {},
|
||||
});
|
||||
useStore.setState({
|
||||
projects: [],
|
||||
threads: [],
|
||||
threadsHydrated: false,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
it("shows a toast for each consecutive keybinding update with no issues", async () => {
|
||||
const mounted = await mountApp();
|
||||
|
||||
try {
|
||||
sendServerConfigUpdatedPush([]);
|
||||
await waitForToast("Keybindings updated", 1);
|
||||
|
||||
// Each server push represents a distinct file change, so it should produce its own toast.
|
||||
sendServerConfigUpdatedPush([]);
|
||||
await waitForToast("Keybindings updated", 2);
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("shows a warning toast when keybinding config has issues", async () => {
|
||||
const mounted = await mountApp();
|
||||
|
||||
try {
|
||||
sendServerConfigUpdatedPush([
|
||||
{ kind: "keybindings.malformed-config", message: "Expected JSON array" },
|
||||
]);
|
||||
await waitForToast("Invalid keybindings configuration");
|
||||
} finally {
|
||||
await mounted.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
it("does not show a toast from the replayed cached value on subscribe", async () => {
|
||||
const mounted = await mountApp();
|
||||
|
||||
try {
|
||||
sendServerConfigUpdatedPush([]);
|
||||
await waitForToast("Keybindings updated");
|
||||
await waitForNoToast("Keybindings updated");
|
||||
|
||||
// Remount the app — onServerConfigUpdated replays the cached value
|
||||
// synchronously on subscribe. This should NOT produce a toast.
|
||||
await mounted.cleanup();
|
||||
const remounted = await mountApp();
|
||||
|
||||
// Give it a moment to process the replayed value
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
const titles = queryToastTitles();
|
||||
expect(
|
||||
titles.filter((t) => t === "Keybindings updated").length,
|
||||
"Replayed cached value should not produce a toast",
|
||||
).toBe(0);
|
||||
|
||||
await remounted.cleanup();
|
||||
} catch (error) {
|
||||
await mounted.cleanup().catch(() => {});
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1477,6 +1477,7 @@ export default function Sidebar() {
|
||||
<button
|
||||
type="button"
|
||||
aria-label={`Create new thread in ${project.name}`}
|
||||
data-testid="new-thread-button"
|
||||
/>
|
||||
}
|
||||
showOnHover
|
||||
|
||||
@@ -1279,3 +1279,20 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
|
||||
export function useComposerThreadDraft(threadId: ThreadId): ComposerThreadDraftState {
|
||||
return useComposerDraftStore((state) => state.draftsByThreadId[threadId] ?? EMPTY_THREAD_DRAFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear draft threads that have been promoted to server threads.
|
||||
*
|
||||
* Call this after a snapshot sync so the route guard in `_chat.$threadId`
|
||||
* sees the server thread before the draft is removed — avoids a redirect
|
||||
* to `/` caused by a gap where neither draft nor server thread exists.
|
||||
*/
|
||||
export function clearPromotedDraftThreads(serverThreadIds: ReadonlySet<ThreadId>): void {
|
||||
const store = useComposerDraftStore.getState();
|
||||
const draftThreadIds = Object.keys(store.draftThreadsByThreadId) as ThreadId[];
|
||||
for (const draftId of draftThreadIds) {
|
||||
if (serverThreadIds.has(draftId)) {
|
||||
store.clearDraftThread(draftId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import { Button } from "../components/ui/button";
|
||||
import { AnchoredToastProvider, ToastProvider, toastManager } from "../components/ui/toast";
|
||||
import { serverConfigQueryOptions, serverQueryKeys } from "../lib/serverReactQuery";
|
||||
import { readNativeApi } from "../nativeApi";
|
||||
import { useComposerDraftStore } from "../composerDraftStore";
|
||||
import { clearPromotedDraftThreads, useComposerDraftStore } from "../composerDraftStore";
|
||||
import { useStore } from "../store";
|
||||
import { useTerminalStateStore } from "../terminalStateStore";
|
||||
import { preferredTerminalEditor } from "../terminal-links";
|
||||
@@ -140,7 +140,6 @@ function EventRouter() {
|
||||
const navigate = useNavigate();
|
||||
const pathname = useRouterState({ select: (state) => state.location.pathname });
|
||||
const pathnameRef = useRef(pathname);
|
||||
const lastConfigIssuesSignatureRef = useRef<string | null>(null);
|
||||
const handledBootstrapThreadIdRef = useRef<string | null>(null);
|
||||
|
||||
pathnameRef.current = pathname;
|
||||
@@ -159,6 +158,7 @@ function EventRouter() {
|
||||
if (disposed) return;
|
||||
latestSequence = Math.max(latestSequence, snapshot.snapshotSequence);
|
||||
syncServerReadModel(snapshot);
|
||||
clearPromotedDraftThreads(new Set(snapshot.threads.map((t) => t.id)));
|
||||
const draftThreadIds = Object.keys(
|
||||
useComposerDraftStore.getState().draftThreadsByThreadId,
|
||||
) as ThreadId[];
|
||||
@@ -255,14 +255,13 @@ function EventRouter() {
|
||||
handledBootstrapThreadIdRef.current = payload.bootstrapThreadId;
|
||||
})().catch(() => undefined);
|
||||
});
|
||||
// onServerConfigUpdated replays the latest cached value synchronously
|
||||
// during subscribe. Skip the toast for that replay so effect re-runs
|
||||
// don't produce duplicate toasts.
|
||||
let subscribed = false;
|
||||
const unsubServerConfigUpdated = onServerConfigUpdated((payload) => {
|
||||
const signature = JSON.stringify(payload.issues);
|
||||
if (lastConfigIssuesSignatureRef.current === signature) {
|
||||
return;
|
||||
}
|
||||
lastConfigIssuesSignatureRef.current = signature;
|
||||
|
||||
void queryClient.invalidateQueries({ queryKey: serverQueryKeys.config() });
|
||||
if (!subscribed) return;
|
||||
const issue = payload.issues.find((entry) => entry.kind.startsWith("keybindings."));
|
||||
if (!issue) {
|
||||
toastManager.add({
|
||||
@@ -297,6 +296,7 @@ function EventRouter() {
|
||||
},
|
||||
});
|
||||
});
|
||||
subscribed = true;
|
||||
return () => {
|
||||
disposed = true;
|
||||
needsProviderInvalidation = false;
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import {
|
||||
CommandId,
|
||||
type ContextMenuItem,
|
||||
EventId,
|
||||
ORCHESTRATION_WS_CHANNELS,
|
||||
ORCHESTRATION_WS_METHODS,
|
||||
type OrchestrationEvent,
|
||||
ProjectId,
|
||||
ThreadId,
|
||||
type WsPushChannel,
|
||||
type WsPushData,
|
||||
type WsPushMessage,
|
||||
WS_CHANNELS,
|
||||
WS_METHODS,
|
||||
type WsPush,
|
||||
type ServerProviderStatus,
|
||||
} from "@t3tools/contracts";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
@@ -19,26 +25,38 @@ const showContextMenuFallbackMock =
|
||||
position?: { x: number; y: number },
|
||||
) => Promise<T | null>
|
||||
>();
|
||||
const channelListeners = new Map<string, Set<(data: unknown) => void>>();
|
||||
const subscribeMock = vi.fn<(channel: string, listener: (data: unknown) => void) => () => void>(
|
||||
(channel, listener) => {
|
||||
const listeners = channelListeners.get(channel) ?? new Set<(data: unknown) => void>();
|
||||
listeners.add(listener);
|
||||
channelListeners.set(channel, listeners);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
if (listeners.size === 0) {
|
||||
channelListeners.delete(channel);
|
||||
}
|
||||
};
|
||||
},
|
||||
);
|
||||
const channelListeners = new Map<string, Set<(message: WsPush) => void>>();
|
||||
const latestPushByChannel = new Map<string, WsPush>();
|
||||
const subscribeMock = vi.fn<
|
||||
(
|
||||
channel: string,
|
||||
listener: (message: WsPush) => void,
|
||||
options?: { replayLatest?: boolean },
|
||||
) => () => void
|
||||
>((channel, listener, options) => {
|
||||
const listeners = channelListeners.get(channel) ?? new Set<(message: WsPush) => void>();
|
||||
listeners.add(listener);
|
||||
channelListeners.set(channel, listeners);
|
||||
const latest = latestPushByChannel.get(channel);
|
||||
if (latest && options?.replayLatest) {
|
||||
listener(latest);
|
||||
}
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
if (listeners.size === 0) {
|
||||
channelListeners.delete(channel);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("./wsTransport", () => {
|
||||
return {
|
||||
WsTransport: class MockWsTransport {
|
||||
request = requestMock;
|
||||
subscribe = subscribeMock;
|
||||
getLatestPush(channel: string) {
|
||||
return latestPushByChannel.get(channel) ?? null;
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
@@ -47,11 +65,20 @@ vi.mock("./contextMenuFallback", () => ({
|
||||
showContextMenuFallback: showContextMenuFallbackMock,
|
||||
}));
|
||||
|
||||
function emitPush(channel: string, data: unknown): void {
|
||||
let nextPushSequence = 1;
|
||||
|
||||
function emitPush<C extends WsPushChannel>(channel: C, data: WsPushData<C>): void {
|
||||
const listeners = channelListeners.get(channel);
|
||||
const message = {
|
||||
type: "push" as const,
|
||||
sequence: nextPushSequence++,
|
||||
channel,
|
||||
data,
|
||||
} as WsPushMessage<C>;
|
||||
latestPushByChannel.set(channel, message);
|
||||
if (!listeners) return;
|
||||
for (const listener of listeners) {
|
||||
listener(data);
|
||||
listener(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +108,8 @@ beforeEach(() => {
|
||||
showContextMenuFallbackMock.mockReset();
|
||||
subscribeMock.mockClear();
|
||||
channelListeners.clear();
|
||||
latestPushByChannel.clear();
|
||||
nextPushSequence = 1;
|
||||
Reflect.deleteProperty(getWindowForTest(), "desktopBridge");
|
||||
});
|
||||
|
||||
@@ -90,7 +119,6 @@ afterEach(() => {
|
||||
|
||||
describe("wsNativeApi", () => {
|
||||
it("delivers and caches valid server.welcome payloads", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const { createWsNativeApi, onServerWelcome } = await import("./wsNativeApi");
|
||||
|
||||
createWsNativeApi();
|
||||
@@ -108,7 +136,6 @@ describe("wsNativeApi", () => {
|
||||
|
||||
expect(lateListener).toHaveBeenCalledTimes(1);
|
||||
expect(lateListener).toHaveBeenCalledWith(expect.objectContaining(payload));
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("preserves bootstrap ids from server.welcome payloads", async () => {
|
||||
@@ -121,8 +148,8 @@ describe("wsNativeApi", () => {
|
||||
emitPush(WS_CHANNELS.serverWelcome, {
|
||||
cwd: "/tmp/workspace",
|
||||
projectName: "t3-code",
|
||||
bootstrapProjectId: "project-1",
|
||||
bootstrapThreadId: "thread-1",
|
||||
bootstrapProjectId: ProjectId.makeUnsafe("project-1"),
|
||||
bootstrapThreadId: ThreadId.makeUnsafe("thread-1"),
|
||||
});
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
@@ -136,31 +163,26 @@ describe("wsNativeApi", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores invalid server.welcome payloads and keeps subscription active", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
it("delivers successive server.welcome payloads to active listeners", async () => {
|
||||
const { createWsNativeApi, onServerWelcome } = await import("./wsNativeApi");
|
||||
|
||||
createWsNativeApi();
|
||||
const listener = vi.fn();
|
||||
onServerWelcome(listener);
|
||||
|
||||
emitPush(WS_CHANNELS.serverWelcome, { cwd: 42, projectName: "t3-code" });
|
||||
emitPush(WS_CHANNELS.serverWelcome, { cwd: "/tmp/one", projectName: "one" });
|
||||
emitPush(WS_CHANNELS.serverWelcome, { cwd: "/tmp/workspace", projectName: "t3-code" });
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ cwd: "/tmp/workspace", projectName: "t3-code" }),
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
expect(listener).toHaveBeenLastCalledWith(
|
||||
expect.objectContaining({
|
||||
cwd: "/tmp/workspace",
|
||||
projectName: "t3-code",
|
||||
}),
|
||||
);
|
||||
expect(warnSpy).toHaveBeenCalledTimes(1);
|
||||
expect(warnSpy).toHaveBeenCalledWith("Dropped inbound WebSocket push payload", {
|
||||
reason: "decode-failed",
|
||||
raw: { cwd: 42, projectName: "t3-code" },
|
||||
issue: expect.stringContaining("SchemaError"),
|
||||
});
|
||||
});
|
||||
|
||||
it("delivers and caches valid server.configUpdated payloads", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const { createWsNativeApi, onServerConfigUpdated } = await import("./wsNativeApi");
|
||||
|
||||
createWsNativeApi();
|
||||
@@ -186,11 +208,9 @@ describe("wsNativeApi", () => {
|
||||
onServerConfigUpdated(lateListener);
|
||||
expect(lateListener).toHaveBeenCalledTimes(1);
|
||||
expect(lateListener).toHaveBeenCalledWith(payload);
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("drops malformed server.configUpdated payloads", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
it("delivers successive server.configUpdated payloads to active listeners", async () => {
|
||||
const { createWsNativeApi, onServerConfigUpdated } = await import("./wsNativeApi");
|
||||
|
||||
createWsNativeApi();
|
||||
@@ -198,24 +218,22 @@ describe("wsNativeApi", () => {
|
||||
onServerConfigUpdated(listener);
|
||||
|
||||
emitPush(WS_CHANNELS.serverConfigUpdated, {
|
||||
issues: [{ kind: "keybindings.invalid-entry", message: "missing index" }],
|
||||
issues: [{ kind: "keybindings.malformed-config", message: "bad json" }],
|
||||
providers: defaultProviders,
|
||||
});
|
||||
emitPush(WS_CHANNELS.serverConfigUpdated, {
|
||||
issues: [{ kind: "keybindings.malformed-config", message: "bad json" }],
|
||||
issues: [],
|
||||
providers: defaultProviders,
|
||||
});
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledWith({
|
||||
issues: [{ kind: "keybindings.malformed-config", message: "bad json" }],
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
expect(listener).toHaveBeenLastCalledWith({
|
||||
issues: [],
|
||||
providers: defaultProviders,
|
||||
});
|
||||
expect(warnSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("forwards valid terminal and orchestration events", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
|
||||
const api = createWsNativeApi();
|
||||
@@ -236,9 +254,9 @@ describe("wsNativeApi", () => {
|
||||
|
||||
const orchestrationEvent = {
|
||||
sequence: 1,
|
||||
eventId: "event-1",
|
||||
eventId: EventId.makeUnsafe("event-1"),
|
||||
aggregateKind: "project",
|
||||
aggregateId: "project-1",
|
||||
aggregateId: ProjectId.makeUnsafe("project-1"),
|
||||
occurredAt: "2026-02-24T00:00:00.000Z",
|
||||
commandId: null,
|
||||
causationEventId: null,
|
||||
@@ -246,7 +264,7 @@ describe("wsNativeApi", () => {
|
||||
metadata: {},
|
||||
type: "project.created",
|
||||
payload: {
|
||||
projectId: "project-1",
|
||||
projectId: ProjectId.makeUnsafe("project-1"),
|
||||
title: "Project",
|
||||
workspaceRoot: "/tmp/workspace",
|
||||
defaultModel: null,
|
||||
@@ -254,61 +272,13 @@ describe("wsNativeApi", () => {
|
||||
createdAt: "2026-02-24T00:00:00.000Z",
|
||||
updatedAt: "2026-02-24T00:00:00.000Z",
|
||||
},
|
||||
} as const;
|
||||
} satisfies Extract<OrchestrationEvent, { type: "project.created" }>;
|
||||
emitPush(ORCHESTRATION_WS_CHANNELS.domainEvent, orchestrationEvent);
|
||||
|
||||
expect(onTerminalEvent).toHaveBeenCalledTimes(1);
|
||||
expect(onTerminalEvent).toHaveBeenCalledWith(terminalEvent);
|
||||
expect(onDomainEvent).toHaveBeenCalledTimes(1);
|
||||
expect(onDomainEvent).toHaveBeenCalledWith(orchestrationEvent);
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("drops malformed terminal and orchestration push payloads", async () => {
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
|
||||
const api = createWsNativeApi();
|
||||
const onTerminalEvent = vi.fn();
|
||||
const onDomainEvent = vi.fn();
|
||||
|
||||
api.terminal.onEvent(onTerminalEvent);
|
||||
api.orchestration.onDomainEvent(onDomainEvent);
|
||||
|
||||
emitPush(WS_CHANNELS.terminalEvent, {
|
||||
threadId: "thread-1",
|
||||
terminalId: "",
|
||||
createdAt: "2026-02-24T00:00:00.000Z",
|
||||
type: "output",
|
||||
data: "hello",
|
||||
});
|
||||
emitPush(ORCHESTRATION_WS_CHANNELS.domainEvent, {
|
||||
sequence: -1,
|
||||
type: "project.created",
|
||||
});
|
||||
|
||||
expect(onTerminalEvent).not.toHaveBeenCalled();
|
||||
expect(onDomainEvent).not.toHaveBeenCalled();
|
||||
expect(warnSpy).toHaveBeenCalledTimes(2);
|
||||
expect(warnSpy).toHaveBeenNthCalledWith(1, "Dropped inbound WebSocket push payload", {
|
||||
reason: "decode-failed",
|
||||
raw: {
|
||||
threadId: "thread-1",
|
||||
terminalId: "",
|
||||
createdAt: "2026-02-24T00:00:00.000Z",
|
||||
type: "output",
|
||||
data: "hello",
|
||||
},
|
||||
issue: expect.stringContaining("SchemaError"),
|
||||
});
|
||||
expect(warnSpy).toHaveBeenNthCalledWith(2, "Dropped inbound WebSocket push payload", {
|
||||
reason: "decode-failed",
|
||||
raw: {
|
||||
sequence: -1,
|
||||
type: "project.created",
|
||||
},
|
||||
issue: expect.stringContaining("SchemaError"),
|
||||
});
|
||||
});
|
||||
|
||||
it("wraps orchestration dispatch commands in the command envelope", async () => {
|
||||
|
||||
+16
-45
@@ -1,16 +1,13 @@
|
||||
import {
|
||||
OrchestrationEvent,
|
||||
ORCHESTRATION_WS_CHANNELS,
|
||||
ORCHESTRATION_WS_METHODS,
|
||||
type ContextMenuItem,
|
||||
type NativeApi,
|
||||
ServerConfigUpdatedPayload,
|
||||
TerminalEvent,
|
||||
WS_CHANNELS,
|
||||
WS_METHODS,
|
||||
WsWelcomePayload,
|
||||
type WsWelcomePayload,
|
||||
} from "@t3tools/contracts";
|
||||
import { Cause, Schema } from "effect";
|
||||
|
||||
import { showContextMenuFallback } from "./contextMenuFallback";
|
||||
import { WsTransport } from "./wsTransport";
|
||||
@@ -18,24 +15,6 @@ import { WsTransport } from "./wsTransport";
|
||||
let instance: { api: NativeApi; transport: WsTransport } | null = null;
|
||||
const welcomeListeners = new Set<(payload: WsWelcomePayload) => void>();
|
||||
const serverConfigUpdatedListeners = new Set<(payload: ServerConfigUpdatedPayload) => void>();
|
||||
let lastWelcome: WsWelcomePayload | null = null;
|
||||
let lastServerConfigUpdated: ServerConfigUpdatedPayload | null = null;
|
||||
|
||||
const decodeAndWarnOnFailure = <T>(
|
||||
schema: Schema.Schema<T> & { readonly DecodingServices: never },
|
||||
raw: unknown,
|
||||
): T | null => {
|
||||
const decoded = Schema.decodeUnknownExit(schema)(raw);
|
||||
if (decoded._tag === "Failure") {
|
||||
console.warn("Dropped inbound WebSocket push payload", {
|
||||
reason: "decode-failed",
|
||||
raw,
|
||||
issue: Cause.pretty(decoded.cause),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
return decoded.value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Subscribe to the server welcome message. If a welcome was already received
|
||||
@@ -45,10 +24,10 @@ const decodeAndWarnOnFailure = <T>(
|
||||
export function onServerWelcome(listener: (payload: WsWelcomePayload) => void): () => void {
|
||||
welcomeListeners.add(listener);
|
||||
|
||||
// Replay cached welcome for late subscribers
|
||||
if (lastWelcome) {
|
||||
const latestWelcome = instance?.transport.getLatestPush(WS_CHANNELS.serverWelcome)?.data ?? null;
|
||||
if (latestWelcome) {
|
||||
try {
|
||||
listener(lastWelcome);
|
||||
listener(latestWelcome);
|
||||
} catch {
|
||||
// Swallow listener errors
|
||||
}
|
||||
@@ -68,9 +47,11 @@ export function onServerConfigUpdated(
|
||||
): () => void {
|
||||
serverConfigUpdatedListeners.add(listener);
|
||||
|
||||
if (lastServerConfigUpdated) {
|
||||
const latestConfig =
|
||||
instance?.transport.getLatestPush(WS_CHANNELS.serverConfigUpdated)?.data ?? null;
|
||||
if (latestConfig) {
|
||||
try {
|
||||
listener(lastServerConfigUpdated);
|
||||
listener(latestConfig);
|
||||
} catch {
|
||||
// Swallow listener errors
|
||||
}
|
||||
@@ -86,12 +67,8 @@ export function createWsNativeApi(): NativeApi {
|
||||
|
||||
const transport = new WsTransport();
|
||||
|
||||
// Listen for server welcome and forward to registered listeners.
|
||||
// Also cache it so late subscribers (React effects) get it immediately.
|
||||
transport.subscribe(WS_CHANNELS.serverWelcome, (data) => {
|
||||
const payload = decodeAndWarnOnFailure(WsWelcomePayload, data);
|
||||
if (!payload) return;
|
||||
lastWelcome = payload;
|
||||
transport.subscribe(WS_CHANNELS.serverWelcome, (message) => {
|
||||
const payload = message.data;
|
||||
for (const listener of welcomeListeners) {
|
||||
try {
|
||||
listener(payload);
|
||||
@@ -100,10 +77,8 @@ export function createWsNativeApi(): NativeApi {
|
||||
}
|
||||
}
|
||||
});
|
||||
transport.subscribe(WS_CHANNELS.serverConfigUpdated, (data) => {
|
||||
const payload = decodeAndWarnOnFailure(ServerConfigUpdatedPayload, data);
|
||||
if (!payload) return;
|
||||
lastServerConfigUpdated = payload;
|
||||
transport.subscribe(WS_CHANNELS.serverConfigUpdated, (message) => {
|
||||
const payload = message.data;
|
||||
for (const listener of serverConfigUpdatedListeners) {
|
||||
try {
|
||||
listener(payload);
|
||||
@@ -134,10 +109,7 @@ export function createWsNativeApi(): NativeApi {
|
||||
restart: (input) => transport.request(WS_METHODS.terminalRestart, input),
|
||||
close: (input) => transport.request(WS_METHODS.terminalClose, input),
|
||||
onEvent: (callback) =>
|
||||
transport.subscribe(WS_CHANNELS.terminalEvent, (data) => {
|
||||
const payload = decodeAndWarnOnFailure(TerminalEvent, data);
|
||||
if (payload) callback(payload);
|
||||
}),
|
||||
transport.subscribe(WS_CHANNELS.terminalEvent, (message) => callback(message.data)),
|
||||
},
|
||||
projects: {
|
||||
searchEntries: (input) => transport.request(WS_METHODS.projectsSearchEntries, input),
|
||||
@@ -199,10 +171,9 @@ export function createWsNativeApi(): NativeApi {
|
||||
replayEvents: (fromSequenceExclusive) =>
|
||||
transport.request(ORCHESTRATION_WS_METHODS.replayEvents, { fromSequenceExclusive }),
|
||||
onDomainEvent: (callback) =>
|
||||
transport.subscribe(ORCHESTRATION_WS_CHANNELS.domainEvent, (data) => {
|
||||
const payload = decodeAndWarnOnFailure(OrchestrationEvent, data);
|
||||
if (payload) callback(payload);
|
||||
}),
|
||||
transport.subscribe(ORCHESTRATION_WS_CHANNELS.domainEvent, (message) =>
|
||||
callback(message.data),
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { WS_CHANNELS } from "@t3tools/contracts";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { WsTransport } from "./wsTransport";
|
||||
@@ -90,18 +91,24 @@ describe("WsTransport", () => {
|
||||
socket.open();
|
||||
|
||||
const listener = vi.fn();
|
||||
transport.subscribe("providers.event", listener);
|
||||
transport.subscribe(WS_CHANNELS.serverConfigUpdated, listener);
|
||||
|
||||
socket.serverMessage(
|
||||
JSON.stringify({
|
||||
type: "push",
|
||||
channel: "providers.event",
|
||||
data: { status: "ok" },
|
||||
sequence: 1,
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: { issues: [], providers: [] },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledWith({ status: "ok" });
|
||||
expect(listener).toHaveBeenCalledWith({
|
||||
type: "push",
|
||||
sequence: 1,
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: { issues: [], providers: [] },
|
||||
});
|
||||
|
||||
transport.dispose();
|
||||
});
|
||||
@@ -137,12 +144,13 @@ describe("WsTransport", () => {
|
||||
socket.open();
|
||||
|
||||
const listener = vi.fn();
|
||||
transport.subscribe("providers.event", listener);
|
||||
transport.subscribe(WS_CHANNELS.serverConfigUpdated, listener);
|
||||
|
||||
socket.serverMessage("{ invalid-json");
|
||||
socket.serverMessage(
|
||||
JSON.stringify({
|
||||
type: "push",
|
||||
sequence: 2,
|
||||
channel: 42,
|
||||
data: { bad: true },
|
||||
}),
|
||||
@@ -150,26 +158,52 @@ describe("WsTransport", () => {
|
||||
socket.serverMessage(
|
||||
JSON.stringify({
|
||||
type: "push",
|
||||
channel: "providers.event",
|
||||
data: { ok: true },
|
||||
sequence: 3,
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: { issues: [], providers: [] },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledWith({ ok: true });
|
||||
expect(listener).toHaveBeenCalledWith({
|
||||
type: "push",
|
||||
sequence: 3,
|
||||
channel: WS_CHANNELS.serverConfigUpdated,
|
||||
data: { issues: [], providers: [] },
|
||||
});
|
||||
expect(warnSpy).toHaveBeenCalledTimes(2);
|
||||
expect(warnSpy).toHaveBeenNthCalledWith(1, "Dropped inbound WebSocket envelope", {
|
||||
reason: "decode-failed",
|
||||
issue:
|
||||
"SchemaError: SyntaxError: Expected property name or '}' in JSON at position 2 (line 1 column 3)",
|
||||
raw: "{ invalid-json",
|
||||
});
|
||||
expect(warnSpy).toHaveBeenNthCalledWith(2, "Dropped inbound WebSocket envelope", {
|
||||
reason: "decode-failed",
|
||||
issue: expect.stringContaining("SchemaError: Expected string, got 42"),
|
||||
raw: '{"type":"push","channel":42,"data":{"bad":true}}',
|
||||
});
|
||||
expect(warnSpy).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"Dropped inbound WebSocket envelope",
|
||||
"SyntaxError: Expected property name or '}' in JSON at position 2 (line 1 column 3)",
|
||||
);
|
||||
expect(warnSpy).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"Dropped inbound WebSocket envelope",
|
||||
expect.stringContaining('Expected "server.configUpdated"'),
|
||||
);
|
||||
|
||||
transport.dispose();
|
||||
});
|
||||
|
||||
it("queues requests until the websocket opens", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
const socket = getSocket();
|
||||
|
||||
const requestPromise = transport.request("projects.list");
|
||||
expect(socket.sent).toHaveLength(0);
|
||||
|
||||
socket.open();
|
||||
expect(socket.sent).toHaveLength(1);
|
||||
const requestEnvelope = JSON.parse(socket.sent[0] ?? "{}") as { id: string };
|
||||
socket.serverMessage(
|
||||
JSON.stringify({
|
||||
id: requestEnvelope.id,
|
||||
result: { projects: [] },
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(requestPromise).resolves.toEqual({ projects: [] });
|
||||
transport.dispose();
|
||||
});
|
||||
});
|
||||
+127
-56
@@ -1,7 +1,15 @@
|
||||
import { WebSocketResponse, WsPush, WsResponse } from "@t3tools/contracts";
|
||||
import { Cause, Schema } from "effect";
|
||||
import {
|
||||
type WsPush,
|
||||
type WsPushChannel,
|
||||
type WsPushMessage,
|
||||
WebSocketResponse,
|
||||
type WsResponse as WsResponseMessage,
|
||||
WsResponse as WsResponseSchema,
|
||||
} from "@t3tools/contracts";
|
||||
import { decodeUnknownJsonResult, formatSchemaError } from "@t3tools/shared/schemaJson";
|
||||
import { Result, Schema } from "effect";
|
||||
|
||||
type PushListener = (data: unknown) => void;
|
||||
type PushListener<C extends WsPushChannel> = (message: WsPushMessage<C>) => void;
|
||||
|
||||
interface PendingRequest {
|
||||
resolve: (result: unknown) => void;
|
||||
@@ -9,12 +17,20 @@ interface PendingRequest {
|
||||
timeout: ReturnType<typeof setTimeout>;
|
||||
}
|
||||
|
||||
interface SubscribeOptions {
|
||||
readonly replayLatest?: boolean;
|
||||
}
|
||||
|
||||
type TransportState = "connecting" | "open" | "reconnecting" | "closed" | "disposed";
|
||||
|
||||
const REQUEST_TIMEOUT_MS = 60_000;
|
||||
const RECONNECT_DELAYS_MS = [500, 1_000, 2_000, 4_000, 8_000];
|
||||
const decodeWsResponseFromJson = Schema.decodeUnknownExit(Schema.fromJsonString(WsResponse));
|
||||
const isWsPushEnvelope = Schema.is(WsPush);
|
||||
const decodeWsResponse = decodeUnknownJsonResult(WsResponseSchema);
|
||||
const isWebSocketResponseEnvelope = Schema.is(WebSocketResponse);
|
||||
|
||||
const isWsPushMessage = (value: WsResponseMessage): value is WsPush =>
|
||||
"type" in value && value.type === "push";
|
||||
|
||||
interface WsRequestEnvelope {
|
||||
id: string;
|
||||
body: {
|
||||
@@ -23,20 +39,28 @@ interface WsRequestEnvelope {
|
||||
};
|
||||
}
|
||||
|
||||
function asError(value: unknown, fallback: string): Error {
|
||||
if (value instanceof Error) {
|
||||
return value;
|
||||
}
|
||||
return new Error(fallback);
|
||||
}
|
||||
|
||||
export class WsTransport {
|
||||
private ws: WebSocket | null = null;
|
||||
private nextId = 1;
|
||||
private readonly pending = new Map<string, PendingRequest>();
|
||||
private readonly listeners = new Map<string, Set<PushListener>>();
|
||||
private readonly listeners = new Map<string, Set<(message: WsPush) => void>>();
|
||||
private readonly latestPushByChannel = new Map<string, WsPush>();
|
||||
private readonly outboundQueue: string[] = [];
|
||||
private reconnectAttempt = 0;
|
||||
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private disposed = false;
|
||||
private state: TransportState = "connecting";
|
||||
private readonly url: string;
|
||||
|
||||
constructor(url?: string) {
|
||||
const bridgeUrl = window.desktopBridge?.getWsUrl();
|
||||
// In dev mode, VITE_WS_URL points to the server's WebSocket endpoint.
|
||||
// In production, the page is served by the WS server on the same host:port.
|
||||
const envUrl = import.meta.env.VITE_WS_URL as string | undefined;
|
||||
this.url =
|
||||
url ??
|
||||
@@ -52,9 +76,11 @@ export class WsTransport {
|
||||
if (typeof method !== "string" || method.length === 0) {
|
||||
throw new Error("Request method is required");
|
||||
}
|
||||
|
||||
const id = String(this.nextId++);
|
||||
const body = params != null ? { ...params, _tag: method } : { _tag: method };
|
||||
const message: WsRequestEnvelope = { id, body };
|
||||
const encoded = JSON.stringify(message);
|
||||
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
@@ -68,28 +94,53 @@ export class WsTransport {
|
||||
timeout,
|
||||
});
|
||||
|
||||
this.send(message);
|
||||
this.send(encoded);
|
||||
});
|
||||
}
|
||||
|
||||
subscribe(channel: string, listener: PushListener): () => void {
|
||||
subscribe<C extends WsPushChannel>(
|
||||
channel: C,
|
||||
listener: PushListener<C>,
|
||||
options?: SubscribeOptions,
|
||||
): () => void {
|
||||
let channelListeners = this.listeners.get(channel);
|
||||
if (!channelListeners) {
|
||||
channelListeners = new Set();
|
||||
channelListeners = new Set<(message: WsPush) => void>();
|
||||
this.listeners.set(channel, channelListeners);
|
||||
}
|
||||
channelListeners.add(listener);
|
||||
|
||||
const wrappedListener = (message: WsPush) => {
|
||||
listener(message as WsPushMessage<C>);
|
||||
};
|
||||
channelListeners.add(wrappedListener);
|
||||
|
||||
if (options?.replayLatest) {
|
||||
const latest = this.latestPushByChannel.get(channel);
|
||||
if (latest) {
|
||||
wrappedListener(latest);
|
||||
}
|
||||
}
|
||||
|
||||
return () => {
|
||||
channelListeners!.delete(listener);
|
||||
if (channelListeners!.size === 0) {
|
||||
channelListeners?.delete(wrappedListener);
|
||||
if (channelListeners?.size === 0) {
|
||||
this.listeners.delete(channel);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getLatestPush<C extends WsPushChannel>(channel: C): WsPushMessage<C> | null {
|
||||
const latest = this.latestPushByChannel.get(channel);
|
||||
return latest ? (latest as WsPushMessage<C>) : null;
|
||||
}
|
||||
|
||||
getState(): TransportState {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.disposed = true;
|
||||
this.state = "disposed";
|
||||
if (this.reconnectTimer !== null) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
this.reconnectTimer = null;
|
||||
@@ -99,18 +150,24 @@ export class WsTransport {
|
||||
pending.reject(new Error("Transport disposed"));
|
||||
}
|
||||
this.pending.clear();
|
||||
this.outboundQueue.length = 0;
|
||||
this.ws?.close();
|
||||
this.ws = null;
|
||||
}
|
||||
|
||||
private connect() {
|
||||
if (this.disposed) return;
|
||||
if (this.disposed) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.state = this.reconnectAttempt > 0 ? "reconnecting" : "connecting";
|
||||
const ws = new WebSocket(this.url);
|
||||
|
||||
ws.addEventListener("open", () => {
|
||||
this.ws = ws;
|
||||
this.state = "open";
|
||||
this.reconnectAttempt = 0;
|
||||
this.flushQueue();
|
||||
});
|
||||
|
||||
ws.addEventListener("message", (event) => {
|
||||
@@ -118,34 +175,37 @@ export class WsTransport {
|
||||
});
|
||||
|
||||
ws.addEventListener("close", () => {
|
||||
this.ws = null;
|
||||
if (this.ws === ws) {
|
||||
this.ws = null;
|
||||
}
|
||||
if (this.disposed) {
|
||||
this.state = "disposed";
|
||||
return;
|
||||
}
|
||||
this.state = "closed";
|
||||
this.scheduleReconnect();
|
||||
});
|
||||
|
||||
ws.addEventListener("error", () => {
|
||||
// close event will fire after error
|
||||
// close will follow
|
||||
});
|
||||
}
|
||||
|
||||
private handleMessage(raw: unknown) {
|
||||
const exit = decodeWsResponseFromJson(raw);
|
||||
if (exit._tag === "Failure") {
|
||||
console.warn("Dropped inbound WebSocket envelope", {
|
||||
reason: "decode-failed",
|
||||
raw,
|
||||
issue: Cause.pretty(exit.cause),
|
||||
});
|
||||
const result = decodeWsResponse(raw);
|
||||
if (Result.isFailure(result)) {
|
||||
console.warn("Dropped inbound WebSocket envelope", formatSchemaError(result.failure));
|
||||
return;
|
||||
}
|
||||
const message = exit.value;
|
||||
|
||||
// Push event
|
||||
if (isWsPushEnvelope(message)) {
|
||||
const message = result.success;
|
||||
if (isWsPushMessage(message)) {
|
||||
this.latestPushByChannel.set(message.channel, message);
|
||||
const channelListeners = this.listeners.get(message.channel);
|
||||
if (channelListeners) {
|
||||
for (const listener of channelListeners) {
|
||||
try {
|
||||
listener(message.data);
|
||||
listener(message);
|
||||
} catch {
|
||||
// Swallow listener errors
|
||||
}
|
||||
@@ -154,57 +214,68 @@ export class WsTransport {
|
||||
return;
|
||||
}
|
||||
|
||||
// Response to a request
|
||||
if (!isWebSocketResponseEnvelope(message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pending = this.pending.get(message.id);
|
||||
if (!pending) return;
|
||||
if (!pending) {
|
||||
return;
|
||||
}
|
||||
|
||||
clearTimeout(pending.timeout);
|
||||
this.pending.delete(message.id);
|
||||
|
||||
if (message.error) {
|
||||
pending.reject(new Error(message.error.message));
|
||||
} else {
|
||||
pending.resolve(message.result);
|
||||
}
|
||||
}
|
||||
|
||||
private send(message: WsRequestEnvelope) {
|
||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(JSON.stringify(message));
|
||||
return;
|
||||
}
|
||||
|
||||
// If not connected, wait for connection
|
||||
const waitForOpen = () => {
|
||||
const check = setInterval(() => {
|
||||
if (this.disposed) {
|
||||
clearInterval(check);
|
||||
return;
|
||||
}
|
||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
||||
clearInterval(check);
|
||||
this.ws.send(JSON.stringify(message));
|
||||
}
|
||||
}, 50);
|
||||
pending.resolve(message.result);
|
||||
}
|
||||
|
||||
// Give up after timeout (the pending request will time out on its own)
|
||||
setTimeout(() => clearInterval(check), REQUEST_TIMEOUT_MS);
|
||||
};
|
||||
waitForOpen();
|
||||
private send(encodedMessage: string) {
|
||||
if (this.disposed) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.outboundQueue.push(encodedMessage);
|
||||
try {
|
||||
this.flushQueue();
|
||||
} catch {
|
||||
// Swallow: flushQueue has queued the message for retry on reconnect
|
||||
}
|
||||
}
|
||||
|
||||
private flushQueue() {
|
||||
if (this.ws?.readyState !== WebSocket.OPEN) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (this.outboundQueue.length > 0) {
|
||||
const message = this.outboundQueue.shift();
|
||||
if (!message) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
this.ws.send(message);
|
||||
} catch (error) {
|
||||
this.outboundQueue.unshift(message);
|
||||
throw asError(error, "Failed to send WebSocket request.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleReconnect() {
|
||||
if (this.disposed) return;
|
||||
if (this.disposed || this.reconnectTimer !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const delay =
|
||||
RECONNECT_DELAYS_MS[Math.min(this.reconnectAttempt, RECONNECT_DELAYS_MS.length - 1)] ??
|
||||
RECONNECT_DELAYS_MS[0]!;
|
||||
|
||||
this.reconnectAttempt++;
|
||||
this.reconnectAttempt += 1;
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.reconnectTimer = null;
|
||||
this.connect();
|
||||
|
||||
@@ -15,7 +15,10 @@ export default mergeConfig(
|
||||
},
|
||||
},
|
||||
test: {
|
||||
include: ["src/components/ChatView.browser.tsx"],
|
||||
include: [
|
||||
"src/components/ChatView.browser.tsx",
|
||||
"src/components/KeybindingsToast.browser.tsx",
|
||||
],
|
||||
browser: {
|
||||
enabled: true,
|
||||
provider: playwright(),
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { Effect, Schema } from "effect";
|
||||
|
||||
import { ORCHESTRATION_WS_METHODS } from "./orchestration";
|
||||
import { WS_METHODS } from "./ws";
|
||||
import { WebSocketRequest } from "./ws";
|
||||
import { ORCHESTRATION_WS_CHANNELS, ORCHESTRATION_WS_METHODS } from "./orchestration";
|
||||
import { WebSocketRequest, WsResponse, WS_CHANNELS, WS_METHODS } from "./ws";
|
||||
|
||||
const decodeWebSocketRequest = Schema.decodeUnknownEffect(WebSocketRequest);
|
||||
const decodeWsResponse = Schema.decodeUnknownEffect(WsResponse);
|
||||
|
||||
it.effect("accepts getTurnDiff requests when fromTurnCount <= toTurnCount", () =>
|
||||
Effect.gen(function* () {
|
||||
@@ -72,3 +72,43 @@ it.effect("accepts git.preparePullRequestThread requests", () =>
|
||||
assert.strictEqual(parsed.body._tag, WS_METHODS.gitPreparePullRequestThread);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("accepts typed websocket push envelopes with sequence", () =>
|
||||
Effect.gen(function* () {
|
||||
const parsed = yield* decodeWsResponse({
|
||||
type: "push",
|
||||
sequence: 1,
|
||||
channel: WS_CHANNELS.serverWelcome,
|
||||
data: {
|
||||
cwd: "/tmp/workspace",
|
||||
projectName: "workspace",
|
||||
},
|
||||
});
|
||||
|
||||
if (!("type" in parsed) || parsed.type !== "push") {
|
||||
assert.fail("expected websocket response to decode as a push envelope");
|
||||
}
|
||||
|
||||
assert.strictEqual(parsed.type, "push");
|
||||
assert.strictEqual(parsed.sequence, 1);
|
||||
assert.strictEqual(parsed.channel, WS_CHANNELS.serverWelcome);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("rejects push envelopes when channel payload does not match the channel schema", () =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* Effect.exit(
|
||||
decodeWsResponse({
|
||||
type: "push",
|
||||
sequence: 2,
|
||||
channel: ORCHESTRATION_WS_CHANNELS.domainEvent,
|
||||
data: {
|
||||
cwd: "/tmp/workspace",
|
||||
projectName: "workspace",
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
assert.strictEqual(result._tag, "Failure");
|
||||
}),
|
||||
);
|
||||
@@ -1,8 +1,10 @@
|
||||
import { Schema, Struct } from "effect";
|
||||
import { ProjectId, ThreadId, TrimmedNonEmptyString } from "./baseSchemas";
|
||||
import { NonNegativeInt, ProjectId, ThreadId, TrimmedNonEmptyString } from "./baseSchemas";
|
||||
|
||||
import {
|
||||
ClientOrchestrationCommand,
|
||||
OrchestrationEvent,
|
||||
ORCHESTRATION_WS_CHANNELS,
|
||||
OrchestrationGetFullThreadDiffInput,
|
||||
ORCHESTRATION_WS_METHODS,
|
||||
OrchestrationGetSnapshotInput,
|
||||
@@ -25,6 +27,7 @@ import {
|
||||
import {
|
||||
TerminalClearInput,
|
||||
TerminalCloseInput,
|
||||
TerminalEvent,
|
||||
TerminalOpenInput,
|
||||
TerminalResizeInput,
|
||||
TerminalRestartInput,
|
||||
@@ -33,6 +36,7 @@ import {
|
||||
import { KeybindingRule } from "./keybindings";
|
||||
import { ProjectSearchEntriesInput, ProjectWriteFileInput } from "./project";
|
||||
import { OpenInEditorInput } from "./editor";
|
||||
import { ServerConfigUpdatedPayload } from "./server";
|
||||
|
||||
// ── WebSocket RPC Method Names ───────────────────────────────────────
|
||||
|
||||
@@ -154,19 +158,8 @@ export const WebSocketResponse = Schema.Struct({
|
||||
});
|
||||
export type WebSocketResponse = typeof WebSocketResponse.Type;
|
||||
|
||||
export const WsPush = Schema.Struct({
|
||||
type: Schema.Literal("push"),
|
||||
channel: TrimmedNonEmptyString,
|
||||
data: Schema.Unknown,
|
||||
});
|
||||
export type WsPush = typeof WsPush.Type;
|
||||
|
||||
// ── Union of all server → client messages ─────────────────────────────
|
||||
|
||||
export const WsResponse = Schema.Union([WebSocketResponse, WsPush]);
|
||||
export type WsResponse = typeof WsResponse.Type;
|
||||
|
||||
// ── Server welcome payload ───────────────────────────────────────────
|
||||
export const WsPushSequence = NonNegativeInt;
|
||||
export type WsPushSequence = typeof WsPushSequence.Type;
|
||||
|
||||
export const WsWelcomePayload = Schema.Struct({
|
||||
cwd: TrimmedNonEmptyString,
|
||||
@@ -175,3 +168,66 @@ export const WsWelcomePayload = Schema.Struct({
|
||||
bootstrapThreadId: Schema.optional(ThreadId),
|
||||
});
|
||||
export type WsWelcomePayload = typeof WsWelcomePayload.Type;
|
||||
|
||||
export interface WsPushPayloadByChannel {
|
||||
readonly [WS_CHANNELS.serverWelcome]: WsWelcomePayload;
|
||||
readonly [WS_CHANNELS.serverConfigUpdated]: typeof ServerConfigUpdatedPayload.Type;
|
||||
readonly [WS_CHANNELS.terminalEvent]: typeof TerminalEvent.Type;
|
||||
readonly [ORCHESTRATION_WS_CHANNELS.domainEvent]: OrchestrationEvent;
|
||||
}
|
||||
|
||||
export type WsPushChannel = keyof WsPushPayloadByChannel;
|
||||
export type WsPushData<C extends WsPushChannel> = WsPushPayloadByChannel[C];
|
||||
|
||||
const makeWsPushSchema = <const Channel extends string, Payload extends Schema.Schema<any>>(
|
||||
channel: Channel,
|
||||
payload: Payload,
|
||||
) =>
|
||||
Schema.Struct({
|
||||
type: Schema.Literal("push"),
|
||||
sequence: WsPushSequence,
|
||||
channel: Schema.Literal(channel),
|
||||
data: payload,
|
||||
});
|
||||
|
||||
export const WsPushServerWelcome = makeWsPushSchema(WS_CHANNELS.serverWelcome, WsWelcomePayload);
|
||||
export const WsPushServerConfigUpdated = makeWsPushSchema(
|
||||
WS_CHANNELS.serverConfigUpdated,
|
||||
ServerConfigUpdatedPayload,
|
||||
);
|
||||
export const WsPushTerminalEvent = makeWsPushSchema(WS_CHANNELS.terminalEvent, TerminalEvent);
|
||||
export const WsPushOrchestrationDomainEvent = makeWsPushSchema(
|
||||
ORCHESTRATION_WS_CHANNELS.domainEvent,
|
||||
OrchestrationEvent,
|
||||
);
|
||||
|
||||
export const WsPushChannelSchema = Schema.Literals([
|
||||
WS_CHANNELS.serverWelcome,
|
||||
WS_CHANNELS.serverConfigUpdated,
|
||||
WS_CHANNELS.terminalEvent,
|
||||
ORCHESTRATION_WS_CHANNELS.domainEvent,
|
||||
]);
|
||||
export type WsPushChannelSchema = typeof WsPushChannelSchema.Type;
|
||||
|
||||
export const WsPush = Schema.Union([
|
||||
WsPushServerWelcome,
|
||||
WsPushServerConfigUpdated,
|
||||
WsPushTerminalEvent,
|
||||
WsPushOrchestrationDomainEvent,
|
||||
]);
|
||||
export type WsPush = typeof WsPush.Type;
|
||||
|
||||
export type WsPushMessage<C extends WsPushChannel> = Extract<WsPush, { channel: C }>;
|
||||
|
||||
export const WsPushEnvelopeBase = Schema.Struct({
|
||||
type: Schema.Literal("push"),
|
||||
sequence: WsPushSequence,
|
||||
channel: WsPushChannelSchema,
|
||||
data: Schema.Unknown,
|
||||
});
|
||||
export type WsPushEnvelopeBase = typeof WsPushEnvelopeBase.Type;
|
||||
|
||||
// ── Union of all server → client messages ─────────────────────────────
|
||||
|
||||
export const WsResponse = Schema.Union([WebSocketResponse, WsPush]);
|
||||
export type WsResponse = typeof WsResponse.Type;
|
||||
@@ -23,6 +23,14 @@
|
||||
"./Net": {
|
||||
"types": "./src/Net.ts",
|
||||
"import": "./src/Net.ts"
|
||||
},
|
||||
"./DrainableWorker": {
|
||||
"types": "./src/DrainableWorker.ts",
|
||||
"import": "./src/DrainableWorker.ts"
|
||||
},
|
||||
"./schemaJson": {
|
||||
"types": "./src/schemaJson.ts",
|
||||
"import": "./src/schemaJson.ts"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { it } from "@effect/vitest";
|
||||
import { describe, expect } from "vitest";
|
||||
import { Deferred, Effect } from "effect";
|
||||
|
||||
import { makeDrainableWorker } from "./DrainableWorker";
|
||||
|
||||
describe("makeDrainableWorker", () => {
|
||||
it.live("waits for work enqueued during active processing before draining", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const processed: string[] = [];
|
||||
const firstStarted = yield* Deferred.make<void>();
|
||||
const releaseFirst = yield* Deferred.make<void>();
|
||||
const secondStarted = yield* Deferred.make<void>();
|
||||
const releaseSecond = yield* Deferred.make<void>();
|
||||
|
||||
const worker = yield* makeDrainableWorker((item: string) =>
|
||||
Effect.gen(function* () {
|
||||
if (item === "first") {
|
||||
yield* Deferred.succeed(firstStarted, undefined).pipe(Effect.orDie);
|
||||
yield* Deferred.await(releaseFirst);
|
||||
}
|
||||
|
||||
if (item === "second") {
|
||||
yield* Deferred.succeed(secondStarted, undefined).pipe(Effect.orDie);
|
||||
yield* Deferred.await(releaseSecond);
|
||||
}
|
||||
|
||||
processed.push(item);
|
||||
}),
|
||||
);
|
||||
|
||||
yield* worker.enqueue("first");
|
||||
yield* Deferred.await(firstStarted);
|
||||
|
||||
const drained = yield* Deferred.make<void>();
|
||||
yield* Effect.forkChild(
|
||||
worker.drain.pipe(
|
||||
Effect.tap(() => Deferred.succeed(drained, undefined).pipe(Effect.orDie)),
|
||||
),
|
||||
);
|
||||
|
||||
yield* worker.enqueue("second");
|
||||
yield* Deferred.succeed(releaseFirst, undefined);
|
||||
yield* Deferred.await(secondStarted);
|
||||
|
||||
expect(yield* Deferred.isDone(drained)).toBe(false);
|
||||
|
||||
yield* Deferred.succeed(releaseSecond, undefined);
|
||||
yield* Deferred.await(drained);
|
||||
|
||||
expect(processed).toEqual(["first", "second"]);
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* DrainableWorker - A queue-based worker that exposes a `drain()` effect.
|
||||
*
|
||||
* Wraps the common `Queue.unbounded` + `Effect.forever` pattern and adds
|
||||
* a signal that resolves when the queue is empty **and** the current item
|
||||
* has finished processing. This lets tests replace timing-sensitive
|
||||
* `Effect.sleep` calls with deterministic `drain()`.
|
||||
*
|
||||
* @module DrainableWorker
|
||||
*/
|
||||
import { Deferred, Effect, Queue, Ref } from "effect";
|
||||
import type { Scope } from "effect";
|
||||
|
||||
export interface DrainableWorker<A> {
|
||||
/**
|
||||
* Enqueue a work item and track it for `drain()`.
|
||||
*
|
||||
* This wraps `Queue.offer` so drain state is updated atomically with the
|
||||
* enqueue path instead of inferring it from queue internals.
|
||||
*/
|
||||
readonly enqueue: (item: A) => Effect.Effect<void>;
|
||||
|
||||
/**
|
||||
* Resolves when the queue is empty and the worker is idle (not processing).
|
||||
*/
|
||||
readonly drain: Effect.Effect<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a drainable worker that processes items from an unbounded queue.
|
||||
*
|
||||
* The worker is forked into the current scope and will be interrupted when
|
||||
* the scope closes. A finalizer shuts down the queue.
|
||||
*
|
||||
* @param process - The effect to run for each queued item.
|
||||
* @returns A `DrainableWorker` with `queue` and `drain`.
|
||||
*/
|
||||
export const makeDrainableWorker = <A, E, R>(
|
||||
process: (item: A) => Effect.Effect<void, E, R>,
|
||||
): Effect.Effect<DrainableWorker<A>, never, Scope.Scope | R> =>
|
||||
Effect.gen(function* () {
|
||||
const queue = yield* Queue.unbounded<A>();
|
||||
const initialIdle = yield* Deferred.make<void>();
|
||||
yield* Deferred.succeed(initialIdle, undefined).pipe(Effect.orDie);
|
||||
const state = yield* Ref.make({
|
||||
outstanding: 0,
|
||||
idle: initialIdle,
|
||||
});
|
||||
|
||||
yield* Effect.addFinalizer(() => Queue.shutdown(queue).pipe(Effect.asVoid));
|
||||
|
||||
const finishOne = Ref.modify(state, (current) => {
|
||||
const remaining = Math.max(0, current.outstanding - 1);
|
||||
return [
|
||||
remaining === 0 ? current.idle : null,
|
||||
{
|
||||
outstanding: remaining,
|
||||
idle: current.idle,
|
||||
},
|
||||
] as const;
|
||||
}).pipe(
|
||||
Effect.flatMap((idle) =>
|
||||
idle === null ? Effect.void : Deferred.succeed(idle, undefined).pipe(Effect.orDie),
|
||||
),
|
||||
);
|
||||
|
||||
yield* Effect.forkScoped(
|
||||
Effect.forever(
|
||||
Queue.take(queue).pipe(
|
||||
Effect.flatMap((item) => process(item).pipe(Effect.ensuring(finishOne))),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const enqueue: DrainableWorker<A>["enqueue"] = (item) =>
|
||||
Effect.gen(function* () {
|
||||
const nextIdle = yield* Deferred.make<void>();
|
||||
yield* Ref.update(state, (current) =>
|
||||
current.outstanding === 0
|
||||
? {
|
||||
outstanding: 1,
|
||||
idle: nextIdle,
|
||||
}
|
||||
: {
|
||||
outstanding: current.outstanding + 1,
|
||||
idle: current.idle,
|
||||
},
|
||||
);
|
||||
|
||||
const accepted = yield* Queue.offer(queue, item);
|
||||
if (!accepted) {
|
||||
yield* finishOne;
|
||||
}
|
||||
});
|
||||
|
||||
const drain: DrainableWorker<A>["drain"] = Ref.get(state).pipe(
|
||||
Effect.flatMap(({ idle }) => Deferred.await(idle)),
|
||||
);
|
||||
|
||||
return { enqueue, drain } satisfies DrainableWorker<A>;
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Cause, Exit, Result, Schema, SchemaIssue } from "effect";
|
||||
|
||||
export const decodeJsonResult = <S extends Schema.Codec<unknown, unknown, never, never>>(
|
||||
schema: S,
|
||||
) => {
|
||||
const decode = Schema.decodeExit(Schema.fromJsonString(schema));
|
||||
return (input: string) => {
|
||||
const result = decode(input);
|
||||
if (Exit.isFailure(result)) {
|
||||
return Result.fail(result.cause);
|
||||
}
|
||||
return Result.succeed(result.value);
|
||||
};
|
||||
};
|
||||
|
||||
export const decodeUnknownJsonResult = <S extends Schema.Codec<unknown, unknown, never, never>>(
|
||||
schema: S,
|
||||
) => {
|
||||
const decode = Schema.decodeUnknownExit(Schema.fromJsonString(schema));
|
||||
return (input: unknown) => {
|
||||
const result = decode(input);
|
||||
if (Exit.isFailure(result)) {
|
||||
return Result.fail(result.cause);
|
||||
}
|
||||
return Result.succeed(result.value);
|
||||
};
|
||||
};
|
||||
|
||||
export const formatSchemaError = (cause: Cause.Cause<Schema.SchemaError>) => {
|
||||
const squashed = Cause.squash(cause);
|
||||
return Schema.isSchemaError(squashed)
|
||||
? SchemaIssue.makeFormatterDefault()(squashed.issue)
|
||||
: Cause.pretty(cause);
|
||||
};
|
||||
Reference in new issue
Block a user