# Branch 1 — Operator Cockpit: make Miadi consume `tide` correctly

**Date:** 2026-05-29 · **Scope:** Branch 1 operator-cockpit (not Ava-specific). Grounded in
`synthesis.md` (Decision C) + `runtime-integration-and-api-evolution.md`. ep023 / session `20260529_195626_49fead`.

> Medicine-wheel framing; each direction ≤55 words.

## 🌅 EAST — Desired end-state (≤55 words)
Miadi consumes `tide` through one typed `@miadi/tide` client boundary: no hardcoded binary
paths, no shell-built command strings. The three API routes call typed verbs
(`getContext` / `peek` / `steerProposal` / `send`) over a resolver + CLI fallback. The
daemon/Unix-socket transport is documented as the next layer, not yet built.

## 🔥 SOUTH — Context / layers to construct (≤55 words)
A `packages/tide` skeleton: `types.ts` (ContextSnapshot, TerminalContext — confidence is
0–100 int), `client.ts` (`resolveTideBin`: `TIDE_BIN`→`which tide`→legacy path; `runTideJson`
via `execFile` arg-arrays; typed verbs; `TideError`), `index.ts`. One tsconfig `@miadi/tide`
path alias so the app resolves source without lockfile churn.

## 🌊 WEST — Validation / accountability gates (≤55 words)
Targeted `tsc --noEmit --incremental false` → zero errors in `packages/tide` and
`app/api/tide`. Behavior preserved on Gaia via legacy-path fallback. `execFile` arg-arrays
remove shell-injection surface. No daemon overbuild. No commit/push, no install, no unrelated
dirty files. Research packet preserved; this plan added additively.

## ❄️ NORTH — Immediate implementation sequence (≤55 words)
(1) Write `packages/tide/{package.json, src/types.ts, src/client.ts, src/index.ts, README.md}`.
(2) Add tsconfig `@miadi/tide` path alias. (3) Rewrite the three routes to import typed verbs;
drop hardcoded paths + manual escaping. (4) Update `packages/tide/AGENTS.md`. (5) Targeted
type-check. (6) Report changed files + next slice.

---

## Exact files to change

**Create**
- `packages/tide/package.json` — `@miadi/tide` (private, source-entry skeleton)
- `packages/tide/src/types.ts` — typed `tide` contract (context first; peek/steer/send loose pending schema)
- `packages/tide/src/client.ts` — binary resolver + `runTideJson` + verbs + `TideError`
- `packages/tide/src/index.ts` — re-exports
- `packages/tide/README.md` — boundary doc; daemon transport documented-not-built

**Modify**
- `tsconfig.json` — add `"@miadi/tide": ["./packages/tide/src/index.ts"]` path
- `app/api/tide/context/route.ts` — `getContext()`
- `app/api/tide/peek/route.ts` — `peek(pane, lines)`
- `app/api/tide/steer/route.ts` — `send()` / `steerProposal()`; drop manual shell escaping
- `packages/tide/AGENTS.md` — reflect the now-real skeleton

## First safe slice (implement now)
All of the above. It is low-risk: additive package + one config line + three drop-in route
refactors; legacy-path fallback preserves current Gaia behavior; no transport rewrite, no
install, no lockfile/buildinfo churn.

## Slice 2 — Daemon Unix-socket transport ✅ (2026-05-29, Ref jgwill/Miadi#373)
Implemented behind the same verbs. `packages/tide/src/socket.ts` speaks the runtime's
`hermes.navigator.daemon.v1` protocol (newline-delimited JSON over the Unix socket at
`${HNAVIG_HOME|HERMES_HOME|~/.miadi/navigator}/daemon.sock`, override `TIDE_SOCKET`).
`getContext()` is now `auto`: daemon socket when up, CLI fallback on absence **or** daemon error;
`socket`/`cli` modes available. `peek`/`steerProposal`/`send` stay CLI (no daemon action for them).
Added `pingDaemon()` + `describeTransport()` diagnostics. Verified by `packages/tide/test/socket-transport.test.ts`
(7 tests: fake daemon + fake `tide` bin, no live daemon) via `npm test`.

## Next unimplemented slice (Slice 3)
1. **`GET /api/tide/health`** — the operator-cockpit surface: return `describeTransport()` +
   `pingDaemon()` so a Hermes/Ava/Miadi session sees *which transport is live and whether the
   daemon answers* before reading context or proposing a steer.
2. **Schema-first contract** — `tide-api.schema.json` as the single source of truth → generated
   `types.ts` (`openapi-typescript`/`json-schema-to-ts`); CI fails on schema/type drift.
3. **Daemon actions for operator verbs** — once the runtime exposes `peek`/`steer`/`send` as socket
   actions, move them off CLI behind the unchanged verb signatures.
4. **Workspace dep + dist build** — replace the tsconfig alias with `@miadi/tide: workspace:*`.

UI convergence (feed the `navigator` structure with live data) remains a separate branch.
