@miadi/tide 0.6.3 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miadi/tide",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "Typed client boundary for the ironsilk `tide` runtime. Daemon Unix-socket transport with CLI fallback.",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -15,7 +15,7 @@
15
15
  "src"
16
16
  ],
17
17
  "dependencies": {
18
- "@miadi/annotate-core": "^0.2.0",
18
+ "@miadi/annotate-core": "^0.3.1",
19
19
  "@miadi/tide-contract": "0.3.5"
20
20
  },
21
21
  "keywords": [
package/src/annotate.ts CHANGED
@@ -720,6 +720,12 @@ export async function exportReview(doc: string): Promise<ReviewExport> {
720
720
  /** One mark as the store holds it. Marks made before attribution, or in the terminal TUI, answer null for their author. */
721
721
  export type MarkRecord = StoredMark
722
722
 
723
+ /** The block index plannotator-tui stored with a mark (`anchor.plannotator_tui.block`), or null. */
724
+ function blockIndexOf(anchor: unknown): number | null {
725
+ const block = (anchor as { plannotator_tui?: { block?: unknown } } | undefined)?.plannotator_tui?.block
726
+ return typeof block === "number" && Number.isSafeInteger(block) && block >= 0 ? block : null
727
+ }
728
+
723
729
  /** The marks on a document with their authors, oldest first, so a reader can see whose each one is. */
724
730
  export async function listMarks(doc: string, storeRoot?: string): Promise<MarkRecord[]> {
725
731
  const file = await findStoreFile(doc, storeRoot)
@@ -736,6 +742,8 @@ export async function listMarks(doc: string, storeRoot?: string): Promise<MarkRe
736
742
  author_by_name: typeof a.author_by_name === "string" && a.author_by_name ? a.author_by_name : null,
737
743
  text: text(a.body),
738
744
  quote: text((a.anchor as { originalText?: unknown } | undefined)?.originalText),
745
+ // plannotator-tui records the block it anchored on; the words alone repeat across blocks.
746
+ block: blockIndexOf(a.anchor),
739
747
  created_at: typeof a.created_at === "string" ? a.created_at : null,
740
748
  }))
741
749
  .filter(m => m.id)
package/src/client.ts CHANGED
@@ -616,6 +616,59 @@ export function buildSendKeysPlan(
616
616
  }
617
617
  }
618
618
 
619
+ /**
620
+ * The keys a person may press in a pane from the cockpit, one at a time: enough to
621
+ * answer an agent's question (move through its choices, confirm one). Nothing else
622
+ * is reachable this way. Tab and Shift-Tab in particular stay out: in a Claude
623
+ * prompt they change the permission mode (jgwill/Miadi#817).
624
+ */
625
+ export const PANE_KEYS = ["Up", "Down", "Enter"] as const
626
+ export type PaneKey = (typeof PANE_KEYS)[number]
627
+
628
+ const TMUX_KEY_NAME: Record<PaneKey, string> = { Up: "Up", Down: "Down", Enter: "C-m" }
629
+
630
+ export function isPaneKey(value: unknown): value is PaneKey {
631
+ return typeof value === "string" && (PANE_KEYS as readonly string[]).includes(value)
632
+ }
633
+
634
+ /** The exact `tmux send-keys` argv for one key, without executing it. */
635
+ export function buildPaneKeyPlan(pane: string, key: PaneKey): { target: string; key: PaneKey; command: string[] } {
636
+ const target = normalizePaneTarget(pane)
637
+ return { target, key, command: ["send-keys", "-t", target, TMUX_KEY_NAME[key]] }
638
+ }
639
+
640
+ /**
641
+ * Press one key in a tmux pane, exactly once: no literal text and no repeated
642
+ * Enter (sendKeys repeats its Enter for a pasted message; for a menu that would
643
+ * answer the next question too). Steer-gated like sendKeys. tmux only.
644
+ */
645
+ export async function sendPaneKey(
646
+ pane: string,
647
+ key: PaneKey,
648
+ opts: Pick<SendKeysOptions, "timeoutMs" | "cwd" | "gate"> = {},
649
+ ): Promise<{ target: string; key: PaneKey; command: string[]; sent_at: string }> {
650
+ if (!isPaneKey(key)) throw new TideError(`key must be one of ${PANE_KEYS.join(", ")}`)
651
+ if (HERDR_PANE_ID_PATTERN.test(pane.trim())) {
652
+ throw new TideError(`"${pane.trim()}" is a herdr pane id: keys are sent to tmux panes only`)
653
+ }
654
+ const plan = buildPaneKeyPlan(pane, key)
655
+ const bin = resolveTmuxBin()
656
+ const runOpts = { timeout: opts.timeoutMs ?? 10_000, cwd: opts.cwd }
657
+ const gate = acceptGateOverrides(opts.gate)
658
+ await requireSteerGate({
659
+ ...gate,
660
+ target: plan.target,
661
+ resolveTarget: gate.resolveTarget ?? (() => tmuxTargetResolved(plan.target, bin, runOpts)),
662
+ })
663
+ try {
664
+ await pExecFile(bin, plan.command, runOpts)
665
+ } catch (err) {
666
+ const msg = err instanceof Error ? err.message : String(err)
667
+ throw new TideError(`tmux send-keys ${plan.command[3]} to ${plan.target} failed: ${msg}`, err)
668
+ }
669
+ return { ...plan, sent_at: new Date().toISOString() }
670
+ }
671
+
619
672
  /**
620
673
  * Operator cockpit — steer a pane by typing `message` into it via `tmux send-keys`.
621
674
  * Robust and simple: no shell, no Python spawn, no reviewed-proposal artifact.
@@ -839,3 +892,88 @@ export function buildEpisodeSteerPlan(
839
892
  : buildSendKeysPlan(pane, message, submit)
840
893
  return { ...plan, lane_id: laneId }
841
894
  }
895
+
896
+ // ── Session teams (jgwill/Miadi#817) ───────────────────────────────────────
897
+ //
898
+ // A session's team is declared by the tmux session option `@miadi-team`, the
899
+ // option the binding line already reads (jgwill/miadi-orchestration-kit#56).
900
+ // `sessionTeams` reads every session's declared team and first-pane folder in
901
+ // one tmux call; `setSessionTeam` sets or clears the option on one session.
902
+ // The rules that turn a session into a team live with teams.json, in Miadi's
903
+ // lib/factory-teams.ts.
904
+
905
+ export interface TmuxSessionTeam {
906
+ /** The tmux session name. */
907
+ session: string
908
+ /** The current folder of the session's first pane (lowest window, then lowest pane). */
909
+ cwd: string | null
910
+ /** The `@miadi-team` session option, or null when it is not set. */
911
+ declared: string | null
912
+ }
913
+
914
+ /** A team id the option may hold: a letter or digit, then letters, digits, `.`, `_`, `-`; at most 40. */
915
+ export const SESSION_TEAM_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,39}$/
916
+
917
+ // No tmux server is a host with no sessions, not a failure.
918
+ const NO_TMUX_SERVER = /no server running|error connecting to|failed to connect to server/i
919
+
920
+ /**
921
+ * Every live tmux session with its declared team and the folder of its first
922
+ * pane. `#{@miadi-team}` in a format resolves the session's user option, so one
923
+ * `list-panes -a` answers both; its rows come session by session, window and
924
+ * pane in index order, so the first row of a session is its first pane.
925
+ */
926
+ export async function sessionTeams(opts: TideRunOptions = {}): Promise<TmuxSessionTeam[]> {
927
+ const bin = resolveTmuxBin()
928
+ let stdout: string
929
+ try {
930
+ ;({ stdout } = await pExecFile(
931
+ bin,
932
+ ["list-panes", "-a", "-F", "#{session_name}\t#{@miadi-team}\t#{pane_current_path}"],
933
+ { timeout: opts.timeoutMs ?? 10_000, cwd: opts.cwd, maxBuffer: 4 * 1024 * 1024 },
934
+ ))
935
+ } catch (err) {
936
+ const stderr = String((err as ExecFileFailure).stderr ?? "")
937
+ if (NO_TMUX_SERVER.test(stderr)) return []
938
+ const msg = err instanceof Error ? err.message : String(err)
939
+ throw new TideError(`tmux list-panes failed while reading session teams: ${msg}`, err)
940
+ }
941
+ const sessions = new Map<string, TmuxSessionTeam>()
942
+ for (const line of stdout.split("\n")) {
943
+ // The folder comes last, so a tab inside a path stays part of it.
944
+ const first = line.indexOf("\t")
945
+ const second = first < 0 ? -1 : line.indexOf("\t", first + 1)
946
+ if (first < 1 || second < 0) continue
947
+ const session = line.slice(0, first)
948
+ if (sessions.has(session)) continue
949
+ const declared = line.slice(first + 1, second).trim()
950
+ const cwd = line.slice(second + 1).trim()
951
+ sessions.set(session, { session, cwd: cwd || null, declared: declared || null })
952
+ }
953
+ return [...sessions.values()]
954
+ }
955
+
956
+ /**
957
+ * Declare `session`'s team (`tmux set-option @miadi-team <team>`), or clear the
958
+ * declaration with null (`-u`). The target is `=<session>:`, which tmux matches
959
+ * exactly: a bare name would also reach a session it is the prefix of.
960
+ */
961
+ export async function setSessionTeam(session: string, team: string | null, opts: TideRunOptions = {}): Promise<void> {
962
+ const name = session.trim()
963
+ // tmux keeps `:` and `.` out of session names, so a name holding one is not a session's.
964
+ if (!name || name.length > 256 || /[:.\u0000-\u001f\u007f]/.test(name)) {
965
+ throw new TideError(`"${session}" is not a tmux session name`)
966
+ }
967
+ if (team !== null && !SESSION_TEAM_PATTERN.test(team)) {
968
+ throw new TideError(`"${team}" is not a team id: a letter or digit, then letters, digits, . _ -, at most 40`)
969
+ }
970
+ const target = `=${name}:`
971
+ const args =
972
+ team === null ? ["set-option", "-u", "-t", target, "@miadi-team"] : ["set-option", "-t", target, "@miadi-team", team]
973
+ try {
974
+ await pExecFile(resolveTmuxBin(), args, { timeout: opts.timeoutMs ?? 10_000, cwd: opts.cwd })
975
+ } catch (err) {
976
+ const msg = err instanceof Error ? err.message : String(err)
977
+ throw new TideError(`tmux set-option @miadi-team on session ${name} failed: ${msg}`, err)
978
+ }
979
+ }