@miadi/tide 0.6.2 → 0.6.4

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.2",
3
+ "version": "0.6.4",
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,8 +15,8 @@
15
15
  "src"
16
16
  ],
17
17
  "dependencies": {
18
- "@miadi/annotate-core": "^0.2.0",
19
- "@miadi/tide-contract": "0.3.4"
18
+ "@miadi/annotate-core": "^0.3.0",
19
+ "@miadi/tide-contract": "0.3.5"
20
20
  },
21
21
  "keywords": [
22
22
  "miadi",
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
@@ -839,3 +839,88 @@ export function buildEpisodeSteerPlan(
839
839
  : buildSendKeysPlan(pane, message, submit)
840
840
  return { ...plan, lane_id: laneId }
841
841
  }
842
+
843
+ // ── Session teams (jgwill/Miadi#817) ───────────────────────────────────────
844
+ //
845
+ // A session's team is declared by the tmux session option `@miadi-team`, the
846
+ // option the binding line already reads (jgwill/miadi-orchestration-kit#56).
847
+ // `sessionTeams` reads every session's declared team and first-pane folder in
848
+ // one tmux call; `setSessionTeam` sets or clears the option on one session.
849
+ // The rules that turn a session into a team live with teams.json, in Miadi's
850
+ // lib/factory-teams.ts.
851
+
852
+ export interface TmuxSessionTeam {
853
+ /** The tmux session name. */
854
+ session: string
855
+ /** The current folder of the session's first pane (lowest window, then lowest pane). */
856
+ cwd: string | null
857
+ /** The `@miadi-team` session option, or null when it is not set. */
858
+ declared: string | null
859
+ }
860
+
861
+ /** A team id the option may hold: a letter or digit, then letters, digits, `.`, `_`, `-`; at most 40. */
862
+ export const SESSION_TEAM_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,39}$/
863
+
864
+ // No tmux server is a host with no sessions, not a failure.
865
+ const NO_TMUX_SERVER = /no server running|error connecting to|failed to connect to server/i
866
+
867
+ /**
868
+ * Every live tmux session with its declared team and the folder of its first
869
+ * pane. `#{@miadi-team}` in a format resolves the session's user option, so one
870
+ * `list-panes -a` answers both; its rows come session by session, window and
871
+ * pane in index order, so the first row of a session is its first pane.
872
+ */
873
+ export async function sessionTeams(opts: TideRunOptions = {}): Promise<TmuxSessionTeam[]> {
874
+ const bin = resolveTmuxBin()
875
+ let stdout: string
876
+ try {
877
+ ;({ stdout } = await pExecFile(
878
+ bin,
879
+ ["list-panes", "-a", "-F", "#{session_name}\t#{@miadi-team}\t#{pane_current_path}"],
880
+ { timeout: opts.timeoutMs ?? 10_000, cwd: opts.cwd, maxBuffer: 4 * 1024 * 1024 },
881
+ ))
882
+ } catch (err) {
883
+ const stderr = String((err as ExecFileFailure).stderr ?? "")
884
+ if (NO_TMUX_SERVER.test(stderr)) return []
885
+ const msg = err instanceof Error ? err.message : String(err)
886
+ throw new TideError(`tmux list-panes failed while reading session teams: ${msg}`, err)
887
+ }
888
+ const sessions = new Map<string, TmuxSessionTeam>()
889
+ for (const line of stdout.split("\n")) {
890
+ // The folder comes last, so a tab inside a path stays part of it.
891
+ const first = line.indexOf("\t")
892
+ const second = first < 0 ? -1 : line.indexOf("\t", first + 1)
893
+ if (first < 1 || second < 0) continue
894
+ const session = line.slice(0, first)
895
+ if (sessions.has(session)) continue
896
+ const declared = line.slice(first + 1, second).trim()
897
+ const cwd = line.slice(second + 1).trim()
898
+ sessions.set(session, { session, cwd: cwd || null, declared: declared || null })
899
+ }
900
+ return [...sessions.values()]
901
+ }
902
+
903
+ /**
904
+ * Declare `session`'s team (`tmux set-option @miadi-team <team>`), or clear the
905
+ * declaration with null (`-u`). The target is `=<session>:`, which tmux matches
906
+ * exactly: a bare name would also reach a session it is the prefix of.
907
+ */
908
+ export async function setSessionTeam(session: string, team: string | null, opts: TideRunOptions = {}): Promise<void> {
909
+ const name = session.trim()
910
+ // tmux keeps `:` and `.` out of session names, so a name holding one is not a session's.
911
+ if (!name || name.length > 256 || /[:.\u0000-\u001f\u007f]/.test(name)) {
912
+ throw new TideError(`"${session}" is not a tmux session name`)
913
+ }
914
+ if (team !== null && !SESSION_TEAM_PATTERN.test(team)) {
915
+ throw new TideError(`"${team}" is not a team id: a letter or digit, then letters, digits, . _ -, at most 40`)
916
+ }
917
+ const target = `=${name}:`
918
+ const args =
919
+ team === null ? ["set-option", "-u", "-t", target, "@miadi-team"] : ["set-option", "-t", target, "@miadi-team", team]
920
+ try {
921
+ await pExecFile(resolveTmuxBin(), args, { timeout: opts.timeoutMs ?? 10_000, cwd: opts.cwd })
922
+ } catch (err) {
923
+ const msg = err instanceof Error ? err.message : String(err)
924
+ throw new TideError(`tmux set-option @miadi-team on session ${name} failed: ${msg}`, err)
925
+ }
926
+ }