teamsmith 0.0.0-stage → 0.1.2

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.
Files changed (245) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +158 -2
  3. package/bin/team.mjs +59 -0
  4. package/install.sh +94 -0
  5. package/package.json +37 -4
  6. package/skills/teamsmith/CHANGELOG.md +763 -0
  7. package/skills/teamsmith/SKILL.md +312 -0
  8. package/skills/teamsmith/extension/team-bg.ts +435 -0
  9. package/skills/teamsmith/extension/team-inbox-watch.ts +1412 -0
  10. package/skills/teamsmith/extension/team-notify.ts +663 -0
  11. package/skills/teamsmith/references/agent-adapters.md +643 -0
  12. package/skills/teamsmith/references/config.md +477 -0
  13. package/skills/teamsmith/references/meeting.md +124 -0
  14. package/skills/teamsmith/references/memory.md +133 -0
  15. package/skills/teamsmith/references/migration.md +227 -0
  16. package/skills/teamsmith/references/openspec.md +222 -0
  17. package/skills/teamsmith/references/philosophy.md +95 -0
  18. package/skills/teamsmith/references/protocol.md +712 -0
  19. package/skills/teamsmith/references/troubleshooting.md +1384 -0
  20. package/skills/teamsmith/references/workflows.md +448 -0
  21. package/skills/teamsmith/scripts/lib/cmd-agents.sh +1709 -0
  22. package/skills/teamsmith/scripts/lib/cmd-bg.sh +284 -0
  23. package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +311 -0
  24. package/skills/teamsmith/scripts/lib/cmd-config.sh +1156 -0
  25. package/skills/teamsmith/scripts/lib/cmd-death.sh +562 -0
  26. package/skills/teamsmith/scripts/lib/cmd-docs.sh +149 -0
  27. package/skills/teamsmith/scripts/lib/cmd-draft.sh +124 -0
  28. package/skills/teamsmith/scripts/lib/cmd-init.sh +212 -0
  29. package/skills/teamsmith/scripts/lib/cmd-meeting.sh +904 -0
  30. package/skills/teamsmith/scripts/lib/cmd-outbox.sh +196 -0
  31. package/skills/teamsmith/scripts/lib/cmd-project.sh +642 -0
  32. package/skills/teamsmith/scripts/lib/cmd-review.sh +1393 -0
  33. package/skills/teamsmith/scripts/lib/cmd-status.sh +1399 -0
  34. package/skills/teamsmith/scripts/lib/cmd-update.sh +191 -0
  35. package/skills/teamsmith/scripts/lib/cmd-watch.sh +1681 -0
  36. package/skills/teamsmith/scripts/lib/common.sh +4964 -0
  37. package/skills/teamsmith/scripts/lib/draft-entry.sh +51 -0
  38. package/skills/teamsmith/scripts/lib/outbox.sh +2042 -0
  39. package/skills/teamsmith/scripts/lib/tmux-argv.sh +165 -0
  40. package/skills/teamsmith/scripts/monitor.mjs +616 -0
  41. package/skills/teamsmith/scripts/panel/README.md +192 -0
  42. package/skills/teamsmith/scripts/panel/build.sh +47 -0
  43. package/skills/teamsmith/scripts/panel/bun.lock +109 -0
  44. package/skills/teamsmith/scripts/panel/draft-send.sh +68 -0
  45. package/skills/teamsmith/scripts/panel/package.json +24 -0
  46. package/skills/teamsmith/scripts/panel/panel.js +388 -0
  47. package/skills/teamsmith/scripts/panel/src/App.tsx +2150 -0
  48. package/skills/teamsmith/scripts/panel/src/clipboard.ts +154 -0
  49. package/skills/teamsmith/scripts/panel/src/compose.ts +691 -0
  50. package/skills/teamsmith/scripts/panel/src/data.ts +555 -0
  51. package/skills/teamsmith/scripts/panel/src/format.ts +50 -0
  52. package/skills/teamsmith/scripts/panel/src/layout.ts +2562 -0
  53. package/skills/teamsmith/scripts/panel/src/main.tsx +894 -0
  54. package/skills/teamsmith/scripts/panel/src/markdown.ts +340 -0
  55. package/skills/teamsmith/scripts/panel/src/sanitize.ts +42 -0
  56. package/skills/teamsmith/scripts/panel/src/settings.ts +154 -0
  57. package/skills/teamsmith/scripts/panel/src/strings/en.ts +416 -0
  58. package/skills/teamsmith/scripts/panel/src/strings/index.ts +65 -0
  59. package/skills/teamsmith/scripts/panel/src/strings/types.ts +9 -0
  60. package/skills/teamsmith/scripts/panel/src/strings/zh.ts +414 -0
  61. package/skills/teamsmith/scripts/panel/src/theme.ts +116 -0
  62. package/skills/teamsmith/scripts/panel/src/types.ts +652 -0
  63. package/skills/teamsmith/scripts/panel/src/width.ts +105 -0
  64. package/skills/teamsmith/scripts/panel/tsconfig.json +16 -0
  65. package/skills/teamsmith/scripts/shim/signal-gate +156 -0
  66. package/skills/teamsmith/scripts/shim/tmux +403 -0
  67. package/skills/teamsmith/scripts/team +143 -0
  68. package/skills/teamsmith/templates/AGENTS.section.md.tmpl +147 -0
  69. package/skills/teamsmith/templates/BOARD.md.tmpl +18 -0
  70. package/skills/teamsmith/templates/DECISIONS.md.tmpl +22 -0
  71. package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +25 -0
  72. package/skills/teamsmith/templates/PROTOCOL.md.tmpl +84 -0
  73. package/skills/teamsmith/templates/ROADMAP.md.tmpl +29 -0
  74. package/skills/teamsmith/templates/config.sh.tmpl +106 -0
  75. package/skills/teamsmith/templates/memory-seed.md.tmpl +46 -0
  76. package/skills/teamsmith/templates/pm-prompt.md.tmpl +110 -0
  77. package/skills/teamsmith/templates/report.md.tmpl +36 -0
  78. package/skills/teamsmith/templates/task.md.tmpl +65 -0
  79. package/skills/teamsmith/templates/threads-README.md +17 -0
  80. package/skills/teamsmith/tests/bg-ledger-harness.mjs +682 -0
  81. package/skills/teamsmith/tests/bg-ledger.sh +307 -0
  82. package/skills/teamsmith/tests/checkout-shape-probe.sh +800 -0
  83. package/skills/teamsmith/tests/config-cli.sh +1562 -0
  84. package/skills/teamsmith/tests/container-tmux.sh +493 -0
  85. package/skills/teamsmith/tests/coverage-inventory.sh +173 -0
  86. package/skills/teamsmith/tests/death-cause.sh +564 -0
  87. package/skills/teamsmith/tests/delivery-truth.sh +1231 -0
  88. package/skills/teamsmith/tests/fake-tui.py +450 -0
  89. package/skills/teamsmith/tests/fixtures/delivery-truth-real/README.md +60 -0
  90. package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-second.py +139 -0
  91. package/skills/teamsmith/tests/fixtures/delivery-truth-real/mock-server.py +29 -0
  92. package/skills/teamsmith/tests/fixtures/delivery-truth-real/observe.ts +20 -0
  93. package/skills/teamsmith/tests/fixtures/delivery-truth-real/run-case.sh +79 -0
  94. package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +175 -0
  95. package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +229 -0
  96. package/skills/teamsmith/tests/fixtures/p71-incident/README.md +22 -0
  97. package/skills/teamsmith/tests/fixtures/p71-incident/dev2.seen +8 -0
  98. package/skills/teamsmith/tests/fixtures/p71-incident/dev2.wake +9 -0
  99. package/skills/teamsmith/tests/fixtures/p71-incident/pm-nudges.wake +3 -0
  100. package/skills/teamsmith/tests/fixtures/signal-lint-cases.txt +40 -0
  101. package/skills/teamsmith/tests/flip-glue.sh +162 -0
  102. package/skills/teamsmith/tests/flip-m12-26c.sh +129 -0
  103. package/skills/teamsmith/tests/flip-m12.sh +185 -0
  104. package/skills/teamsmith/tests/flip-m16.sh +166 -0
  105. package/skills/teamsmith/tests/flip-m17.sh +204 -0
  106. package/skills/teamsmith/tests/flip-m33.sh +284 -0
  107. package/skills/teamsmith/tests/flip-m34.sh +96 -0
  108. package/skills/teamsmith/tests/flip-m37.sh +185 -0
  109. package/skills/teamsmith/tests/flip-m4.3.sh +375 -0
  110. package/skills/teamsmith/tests/flip-m43.sh +128 -0
  111. package/skills/teamsmith/tests/flip-m44.sh +262 -0
  112. package/skills/teamsmith/tests/flip-m45.sh +244 -0
  113. package/skills/teamsmith/tests/flip-m46.sh +238 -0
  114. package/skills/teamsmith/tests/flip-m48.sh +304 -0
  115. package/skills/teamsmith/tests/flip-m6.2.sh +205 -0
  116. package/skills/teamsmith/tests/flip-m6.3.sh +283 -0
  117. package/skills/teamsmith/tests/flip-m6.4.sh +267 -0
  118. package/skills/teamsmith/tests/flip-m6.5.sh +113 -0
  119. package/skills/teamsmith/tests/flip-m7.1.sh +135 -0
  120. package/skills/teamsmith/tests/flip-m7.2.sh +321 -0
  121. package/skills/teamsmith/tests/flip-p144.sh +112 -0
  122. package/skills/teamsmith/tests/flip-p156.sh +133 -0
  123. package/skills/teamsmith/tests/flip-p158.sh +301 -0
  124. package/skills/teamsmith/tests/flip-p160.sh +182 -0
  125. package/skills/teamsmith/tests/flip-p171.sh +249 -0
  126. package/skills/teamsmith/tests/flip-p18.1.sh +95 -0
  127. package/skills/teamsmith/tests/flip-p20.sh +286 -0
  128. package/skills/teamsmith/tests/flip-p201.sh +249 -0
  129. package/skills/teamsmith/tests/flip-p210.sh +239 -0
  130. package/skills/teamsmith/tests/flip-p211.sh +152 -0
  131. package/skills/teamsmith/tests/flip-p217.sh +288 -0
  132. package/skills/teamsmith/tests/flip-p22.sh +123 -0
  133. package/skills/teamsmith/tests/flip-p23.sh +239 -0
  134. package/skills/teamsmith/tests/flip-p25.sh +165 -0
  135. package/skills/teamsmith/tests/flip-p71.sh +184 -0
  136. package/skills/teamsmith/tests/flip-p72.sh +202 -0
  137. package/skills/teamsmith/tests/frames/README.md +176 -0
  138. package/skills/teamsmith/tests/frames/p78-conversation-rule-below-box.txt +6 -0
  139. package/skills/teamsmith/tests/frames/p78-cursor-mid-draft.txt +6 -0
  140. package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor-line.txt +6 -0
  141. package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor.txt +6 -0
  142. package/skills/teamsmith/tests/frames/p78-draft-rule-blank-region.txt +6 -0
  143. package/skills/teamsmith/tests/frames/p78-draft-rule-only.txt +5 -0
  144. package/skills/teamsmith/tests/frames/p78-spinner-row-below-cursor.txt +6 -0
  145. package/skills/teamsmith/tests/frames/p78-wider-rule-below-cursor.txt +6 -0
  146. package/skills/teamsmith/tests/frames/p86-f1-mixed-width-disjoint-box.txt +8 -0
  147. package/skills/teamsmith/tests/frames/p86-f1-narrower-width-disjoint-box.txt +8 -0
  148. package/skills/teamsmith/tests/frames/p86-f1-spinner-top-disjoint-box.txt +8 -0
  149. package/skills/teamsmith/tests/frames/pi-0.85.1-update-banner.txt +30 -0
  150. package/skills/teamsmith/tests/frames/pi-0.87.0-draft-half-sentence.txt +30 -0
  151. package/skills/teamsmith/tests/frames/pi-0.87.0-empty-box.txt +30 -0
  152. package/skills/teamsmith/tests/frames/pi-0.87.0-one-line-draft.txt +30 -0
  153. package/skills/teamsmith/tests/frames/pi-0.87.0-project-trust-prompt.txt +30 -0
  154. package/skills/teamsmith/tests/frames/pi-0.99.2-empty-editor.txt +32 -0
  155. package/skills/teamsmith/tests/frames/pi-0.99.2-human-draft.txt +32 -0
  156. package/skills/teamsmith/tests/frames/pi-1.0.0-git-error-in-box.txt +32 -0
  157. package/skills/teamsmith/tests/gate-guard.sh +112 -0
  158. package/skills/teamsmith/tests/guard-matrix.sh +233 -0
  159. package/skills/teamsmith/tests/install-shape.sh +731 -0
  160. package/skills/teamsmith/tests/ledger-ban.sh +612 -0
  161. package/skills/teamsmith/tests/lib/box-judge.sh +68 -0
  162. package/skills/teamsmith/tests/lib/checkout-shape.sh +129 -0
  163. package/skills/teamsmith/tests/lib/loop-scan.awk +114 -0
  164. package/skills/teamsmith/tests/lib/pty-wait.sh +676 -0
  165. package/skills/teamsmith/tests/lib/section-guard.sh +855 -0
  166. package/skills/teamsmith/tests/lib/shell-lex.pl +261 -0
  167. package/skills/teamsmith/tests/lib/tmp-root.sh +528 -0
  168. package/skills/teamsmith/tests/lib/tmux-cap.sh +35 -0
  169. package/skills/teamsmith/tests/lib/tmux-iso-call-probe.sh +81 -0
  170. package/skills/teamsmith/tests/lib/tmux-iso-probe.sh +46 -0
  171. package/skills/teamsmith/tests/lib/tmux-iso.sh +381 -0
  172. package/skills/teamsmith/tests/load-experiment.sh +290 -0
  173. package/skills/teamsmith/tests/loop-inventory.tsv +84 -0
  174. package/skills/teamsmith/tests/panel-b2-slow-stub.sh +19 -0
  175. package/skills/teamsmith/tests/panel-b2.sh +1183 -0
  176. package/skills/teamsmith/tests/panel-b3-pty-mouse.py +240 -0
  177. package/skills/teamsmith/tests/panel-b3-pty-tmux-mouse.py +138 -0
  178. package/skills/teamsmith/tests/panel-b3-stub.sh +279 -0
  179. package/skills/teamsmith/tests/panel-b3.sh +1301 -0
  180. package/skills/teamsmith/tests/panel-choices.sh +572 -0
  181. package/skills/teamsmith/tests/panel-clipboard-model.ts +187 -0
  182. package/skills/teamsmith/tests/panel-compose-model.ts +290 -0
  183. package/skills/teamsmith/tests/panel-contrast.mjs +81 -0
  184. package/skills/teamsmith/tests/panel-cpu-premise.sh +173 -0
  185. package/skills/teamsmith/tests/panel-cpu.sh +391 -0
  186. package/skills/teamsmith/tests/panel-flip-m54.sh +207 -0
  187. package/skills/teamsmith/tests/panel-flip-p123.sh +278 -0
  188. package/skills/teamsmith/tests/panel-flip-p125.sh +318 -0
  189. package/skills/teamsmith/tests/panel-flip-p128.sh +259 -0
  190. package/skills/teamsmith/tests/panel-keyprobe-pty.py +99 -0
  191. package/skills/teamsmith/tests/panel-keyprobe-stub.sh +41 -0
  192. package/skills/teamsmith/tests/panel-keyprobe.sh +82 -0
  193. package/skills/teamsmith/tests/panel-keyprobe.tsx +116 -0
  194. package/skills/teamsmith/tests/panel-knobs.sh +83 -0
  195. package/skills/teamsmith/tests/panel-p21.sh +2181 -0
  196. package/skills/teamsmith/tests/panel-snapshots.sh +802 -0
  197. package/skills/teamsmith/tests/panel-strings.mjs +195 -0
  198. package/skills/teamsmith/tests/perf.sh +740 -0
  199. package/skills/teamsmith/tests/pm-box-real.sh +350 -0
  200. package/skills/teamsmith/tests/pulse-nudge-key.sh +563 -0
  201. package/skills/teamsmith/tests/routes.sh +1652 -0
  202. package/skills/teamsmith/tests/section-budgets.tsv +150 -0
  203. package/skills/teamsmith/tests/section-guard.sh +231 -0
  204. package/skills/teamsmith/tests/section-needs-audit.sh +94 -0
  205. package/skills/teamsmith/tests/section-paths.tsv +142 -0
  206. package/skills/teamsmith/tests/section-select.sh +655 -0
  207. package/skills/teamsmith/tests/signal-gate.sh +366 -0
  208. package/skills/teamsmith/tests/signal-lint-legacy.txt +14 -0
  209. package/skills/teamsmith/tests/signal-lint.pl +416 -0
  210. package/skills/teamsmith/tests/skill-load.mjs +65 -0
  211. package/skills/teamsmith/tests/smoke.sh +19711 -0
  212. package/skills/teamsmith/tests/snapshots/zh-dark-120-detail.txt +32 -0
  213. package/skills/teamsmith/tests/snapshots/zh-dark-120-p4.txt +32 -0
  214. package/skills/teamsmith/tests/snapshots/zh-dark-120.txt +32 -0
  215. package/skills/teamsmith/tests/snapshots/zh-dark-160-p4.txt +32 -0
  216. package/skills/teamsmith/tests/snapshots/zh-dark-160.txt +32 -0
  217. package/skills/teamsmith/tests/snapshots/zh-dark-59-detail.txt +32 -0
  218. package/skills/teamsmith/tests/snapshots/zh-dark-59-p4.txt +32 -0
  219. package/skills/teamsmith/tests/snapshots/zh-dark-59.txt +32 -0
  220. package/skills/teamsmith/tests/snapshots/zh-dark-99-p4.txt +32 -0
  221. package/skills/teamsmith/tests/snapshots/zh-dark-99.txt +32 -0
  222. package/skills/teamsmith/tests/snapshots/zh-light-120-detail.txt +32 -0
  223. package/skills/teamsmith/tests/snapshots/zh-light-120-p4.txt +32 -0
  224. package/skills/teamsmith/tests/snapshots/zh-light-120.txt +32 -0
  225. package/skills/teamsmith/tests/snapshots/zh-light-160-p4.txt +32 -0
  226. package/skills/teamsmith/tests/snapshots/zh-light-160.txt +32 -0
  227. package/skills/teamsmith/tests/snapshots/zh-light-59-detail.txt +32 -0
  228. package/skills/teamsmith/tests/snapshots/zh-light-59-p4.txt +32 -0
  229. package/skills/teamsmith/tests/snapshots/zh-light-59.txt +32 -0
  230. package/skills/teamsmith/tests/snapshots/zh-light-99-p4.txt +32 -0
  231. package/skills/teamsmith/tests/snapshots/zh-light-99.txt +32 -0
  232. package/skills/teamsmith/tests/spec-ledger-refs.tsv +78 -0
  233. package/skills/teamsmith/tests/spec-refs.sh +826 -0
  234. package/skills/teamsmith/tests/task-header-model.sh +267 -0
  235. package/skills/teamsmith/tests/team-bg-flip.sh +239 -0
  236. package/skills/teamsmith/tests/team-bg-harness.mjs +392 -0
  237. package/skills/teamsmith/tests/team-bg-stop.sh +434 -0
  238. package/skills/teamsmith/tests/team-inbox-watch-flip.sh +191 -0
  239. package/skills/teamsmith/tests/team-inbox-watch-harness.mjs +1559 -0
  240. package/skills/teamsmith/tests/tmp-hygiene.sh +1190 -0
  241. package/skills/teamsmith/tests/tmux-lint-legacy.txt +31 -0
  242. package/skills/teamsmith/tests/tmux-lint.pl +604 -0
  243. package/skills/teamsmith-init/SKILL.md +100 -0
  244. package/skills/teamsmith-init/references/bootstrap.md +74 -0
  245. package/skills/teamsmith-init/templates/bootstrap-prompt.md.tmpl +46 -0
@@ -0,0 +1,340 @@
1
+ // The read-only markdown renderer for the detail view (P18/B3, design §7).
2
+ //
3
+ // A narrow, self-written subset — no new dependency, because the bundle must stay a single file
4
+ // that runs with no install. The fidelity target is *readable structure*, pinned by the spec's
5
+ // "Markdown structure survives the render" scenario; CommonMark conformance is a non-goal.
6
+ //
7
+ // The subset: ATX headings, fenced code blocks (verbatim, dim), pipe tables (display-width
8
+ // aligned), bullet/numbered lists, blockquotes, `---` rules, and inline bold / code / links as
9
+ // tone spans. Anything outside the subset renders as its source text — never dropped, never an
10
+ // error. The renderer is pure, so `src/**` stays testable without a terminal.
11
+
12
+ import { dispWidth } from './width.js'
13
+ import type { Line, PlacedLine, Segment, Tone } from './types.js'
14
+
15
+ const seg = (text: string, tone: Tone = 'text'): Segment => ({ text: String(text), tone })
16
+ const ln = (...parts: (Segment | null)[]): Line => parts.filter((p): p is Segment => p !== null)
17
+
18
+ /** A tone-styled word (or a single space) the wrapper lays out. */
19
+ interface Word {
20
+ text: string
21
+ tone: Tone
22
+ }
23
+
24
+ /** Code points of `text` that fit in `w` display columns (at least one, always something). */
25
+ function takeWidth(text: string, w: number): string {
26
+ let out = ''
27
+ let used = 0
28
+ for (const ch of text) {
29
+ const cw = dispWidth(ch)
30
+ if (used + cw > w) break
31
+ used += cw
32
+ out += ch
33
+ }
34
+ return out || [...text][0] || ''
35
+ }
36
+
37
+ /**
38
+ * Greedy word wrap over tone spans. `first`/`rest` are the line prefixes (a list's bullet on the
39
+ * first line, a quote bar on every line, the hanging indent on continuations); a word that cannot
40
+ * fit on a line of its own is hard-broken, so no row ever overflows the width.
41
+ */
42
+ function wrapWords(words: Word[], width: number, first = '', rest = first): Line[] {
43
+ const maxW = Math.max(1, width - Math.max(dispWidth(first), dispWidth(rest)))
44
+ const chunks: Word[] = []
45
+ for (const word of words) {
46
+ if (/^\s+$/.test(word.text)) {
47
+ chunks.push({ text: ' ', tone: word.tone })
48
+ continue
49
+ }
50
+ let text = word.text
51
+ while (dispWidth(text) > maxW) {
52
+ const head = takeWidth(text, maxW)
53
+ chunks.push({ text: head, tone: word.tone })
54
+ text = text.slice(head.length)
55
+ }
56
+ if (text) chunks.push({ text, tone: word.tone })
57
+ }
58
+ const out: Line[] = []
59
+ let line: Line = first ? [seg(first, 'dim')] : []
60
+ let w = dispWidth(first)
61
+ let has = false
62
+ for (const chunk of chunks) {
63
+ if (chunk.text === ' ') continue // spaces are re-inserted between words, never doubled
64
+ const dw = dispWidth(chunk.text)
65
+ if (has && w + 1 + dw > width) {
66
+ out.push(line)
67
+ line = rest ? [seg(rest, 'dim')] : []
68
+ w = dispWidth(rest)
69
+ has = false
70
+ }
71
+ if (has) {
72
+ line.push(seg(' ', chunk.tone))
73
+ w += 1
74
+ }
75
+ line.push(seg(chunk.text, chunk.tone))
76
+ w += dw
77
+ has = true
78
+ }
79
+ out.push(line)
80
+ return out
81
+ }
82
+
83
+ /**
84
+ * Flatten a tone-styled line into words. Adjacent spans with no whitespace between them stay one
85
+ * word (a link's `(url).` keeps its punctuation attached); whitespace collapses to single spaces.
86
+ * A word carries the tone of the span it started in.
87
+ */
88
+ function wordsOf(line: Line): Word[] {
89
+ const out: Word[] = []
90
+ let cur: Word | null = null
91
+ const flush = (): void => {
92
+ if (cur && cur.text) out.push(cur)
93
+ cur = null
94
+ }
95
+ for (const span of line) {
96
+ for (const part of String(span.text).split(/(\s+)/)) {
97
+ if (part === '') continue
98
+ if (/^\s+$/.test(part)) {
99
+ flush()
100
+ out.push({ text: ' ', tone: span.tone })
101
+ continue
102
+ }
103
+ if (cur) cur.text += part
104
+ else cur = { text: part, tone: span.tone }
105
+ }
106
+ }
107
+ flush()
108
+ return out
109
+ }
110
+
111
+ /**
112
+ * Inline spans: `**bold**` (heading tone), `` `code` `` (accent), `[text](url)` (accent text
113
+ * followed by a dim parenthesised URL). Everything else is plain text, marker and all.
114
+ */
115
+ function inline(text: string, base: Tone = 'text'): Line {
116
+ const out: Line = []
117
+ const re = /(\*\*([^*]+)\*\*)|(`([^`]+)`)|(\[([^\]]+)\]\(([^)\s]+)\))/g
118
+ let last = 0
119
+ for (let m = re.exec(text); m; m = re.exec(text)) {
120
+ if (m.index > last) out.push(seg(text.slice(last, m.index), base))
121
+ if (m[2] !== undefined) out.push(seg(m[2], 'heading'))
122
+ else if (m[4] !== undefined) out.push(seg(m[4], 'accent'))
123
+ else if (m[6] !== undefined) {
124
+ // The URL is one span so its punctuation (`(url).`) stays attached to the link word.
125
+ out.push(seg(m[6], 'accent'), seg(` (${m[7]})`, 'dim'))
126
+ }
127
+ last = m.index + m[0].length
128
+ }
129
+ if (last < text.length) out.push(seg(text.slice(last), base))
130
+ return out.length ? out : [seg(text, base)]
131
+ }
132
+
133
+ /** The plain text of a table cell (inline markers stripped — alignment wins over tone here). */
134
+ function cellText(text: string): string {
135
+ return String(text)
136
+ .replace(/\*\*([^*]+)\*\*/g, '$1')
137
+ .replace(/`([^`]+)`/g, '$1')
138
+ .replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, '$1 ($2)')
139
+ .trim()
140
+ }
141
+
142
+ function splitRow(row: string): string[] {
143
+ let text = row.trim()
144
+ if (text.startsWith('|')) text = text.slice(1)
145
+ if (text.endsWith('|')) text = text.slice(0, -1)
146
+ const cells: string[] = []
147
+ let cur = ''
148
+ for (let i = 0; i < text.length; i++) {
149
+ const ch = text[i]
150
+ if (ch === '\\' && text[i + 1] === '|') {
151
+ cur += '|'
152
+ i++
153
+ continue
154
+ }
155
+ if (ch === '|') {
156
+ cells.push(cur)
157
+ cur = ''
158
+ continue
159
+ }
160
+ cur += ch
161
+ }
162
+ cells.push(cur)
163
+ return cells
164
+ }
165
+
166
+ const ALIGN_RE = /^\s*\|?\s*:?-{1,}:?\s*(\|\s*:?-{1,}:?\s*)*\|?\s*$/
167
+
168
+ function isSeparatorRow(row: string): boolean {
169
+ return row.includes('|') && ALIGN_RE.test(row) && /-/.test(row)
170
+ }
171
+
172
+ type Align = 'left' | 'right' | 'center'
173
+
174
+ function alignOf(marker: string): Align {
175
+ const t = marker.trim()
176
+ if (t.startsWith(':') && t.endsWith(':')) return 'center'
177
+ if (t.endsWith(':')) return 'right'
178
+ return 'left'
179
+ }
180
+
181
+ /** One pipe table: header + separator + body, columns aligned by display width. */
182
+ function tableBlock(rows: string[], width: number): Line[] {
183
+ const header = splitRow(rows[0]).map(cellText)
184
+ const aligns = splitRow(rows[1]).map(alignOf)
185
+ const body = rows.slice(2).map((r) => splitRow(r).map(cellText))
186
+ const cols = Math.max(header.length, ...body.map((r) => r.length), 1)
187
+ const widths: number[] = []
188
+ for (let c = 0; c < cols; c++) {
189
+ let w = dispWidth(header[c] ?? '')
190
+ for (const row of body) w = Math.max(w, dispWidth(row[c] ?? ''))
191
+ widths.push(w)
192
+ }
193
+ const render = (cells: string[], base: Tone): Line => {
194
+ const line: Line = [seg(' ')]
195
+ for (let c = 0; c < cols; c++) {
196
+ if (c > 0) line.push(seg(' │ ', 'dim'))
197
+ const text = cells[c] ?? ''
198
+ const pad = Math.max(0, widths[c] - dispWidth(text))
199
+ const align = aligns[c] ?? 'left'
200
+ const left = align === 'right' ? pad : align === 'center' ? Math.floor(pad / 2) : 0
201
+ const right = pad - left
202
+ line.push(seg(' '.repeat(left)), seg(text, base), seg(' '.repeat(right)))
203
+ }
204
+ return line
205
+ }
206
+ const out: Line[] = [render(header, 'heading')]
207
+ out.push(
208
+ ln(
209
+ seg(' '),
210
+ ...widths.map((w, c) => (c > 0 ? [seg('─┼─', 'dim')] : []).concat([seg('─'.repeat(w), 'dim')])).flat(),
211
+ ),
212
+ )
213
+ for (const row of body) out.push(render(row, 'text'))
214
+ return out.map((l) => (dispWidth(l.map((s) => s.text).join('')) > width ? clip(l, width) : l))
215
+ }
216
+
217
+ /** Hard clip a line to `width` columns (the frame's own truncation, applied early for tables). */
218
+ function clip(line: Line, width: number): Line {
219
+ const out: Line = []
220
+ let used = 0
221
+ for (const span of line) {
222
+ if (used >= width) break
223
+ const text = takeWidth(span.text, width - used)
224
+ if (text) out.push({ text, tone: span.tone })
225
+ used += dispWidth(text)
226
+ }
227
+ return out
228
+ }
229
+
230
+ /**
231
+ * Render a markdown document into placed rows at `width`. Pure: same input → same rows (the memo
232
+ * below is an optimisation, never a semantic difference).
233
+ */
234
+ export function markdownToLines(source: string, width: number): PlacedLine[] {
235
+ const w = Math.max(4, Math.floor(width) || 4)
236
+ const src = String(source ?? '').replace(/\r\n?/g, '\n').split('\n')
237
+ const out: PlacedLine[] = []
238
+ const push = (lines: Line[]): void => {
239
+ for (const line of lines) out.push({ line: clip(line, w) })
240
+ }
241
+ let i = 0
242
+ while (i < src.length) {
243
+ const raw = src[i]
244
+ // Fenced code: the body verbatim in the dim tone; the fence lines themselves are the syntax.
245
+ const fence = /^\s{0,3}(```|~~~)/.exec(raw)
246
+ if (fence) {
247
+ const mark = fence[1]
248
+ const close = new RegExp(`^\\s{0,3}${mark}\\s*$`)
249
+ i++
250
+ while (i < src.length && !close.test(src[i])) {
251
+ out.push({ line: [{ text: src[i], tone: 'dim' }] })
252
+ i++
253
+ }
254
+ if (i < src.length) i++
255
+ continue
256
+ }
257
+ // Blank line.
258
+ if (!raw.trim()) {
259
+ out.push({ line: [] })
260
+ i++
261
+ continue
262
+ }
263
+ // A pipe table: a header row followed by a separator row.
264
+ if (raw.includes('|') && i + 1 < src.length && isSeparatorRow(src[i + 1])) {
265
+ const rows = [raw, src[i + 1]]
266
+ i += 2
267
+ while (i < src.length && src[i].trim() && src[i].includes('|')) {
268
+ rows.push(src[i])
269
+ i++
270
+ }
271
+ push(tableBlock(rows, w))
272
+ continue
273
+ }
274
+ // ATX heading.
275
+ const heading = /^\s{0,3}(#{1,6})\s+(.*?)\s*#*\s*$/.exec(raw)
276
+ if (heading) {
277
+ push(wrapWords([{ text: heading[2], tone: 'heading' }], w))
278
+ i++
279
+ continue
280
+ }
281
+ // Horizontal rule (`---`, `***`, `___` — at least three markers, nothing else).
282
+ if (/^\s{0,3}([-*_])(\s*\1){2,}\s*$/.test(raw)) {
283
+ out.push({ line: [{ text: '─'.repeat(w), tone: 'dim' }] })
284
+ i++
285
+ continue
286
+ }
287
+ // Blockquote: one line at a time, the bar dim, the body readable.
288
+ const quote = /^\s{0,3}>\s?(.*)$/.exec(raw)
289
+ if (quote) {
290
+ push(wrapWords(wordsOf(inline(quote[1])), w, '│ ', '│ '))
291
+ i++
292
+ continue
293
+ }
294
+ // Bullet or numbered list item (nested items keep their indentation as a hanging indent).
295
+ const item = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/.exec(raw)
296
+ if (item) {
297
+ const indent = ' '.repeat(Math.min(8, item[1].length))
298
+ const marker = /^[-*+]$/.test(item[2]) ? '•' : item[2]
299
+ const prefix = `${indent}${marker} `
300
+ push(wrapWords(wordsOf(inline(item[3])), w, prefix, ' '.repeat(dispWidth(prefix))))
301
+ i++
302
+ continue
303
+ }
304
+ // Anything else is a paragraph (its inline spans are the only parsing).
305
+ push(wrapWords(wordsOf(inline(raw)), w))
306
+ i++
307
+ }
308
+ return out
309
+ }
310
+
311
+ /** FNV-1a over a bounded prefix plus the length: a cheap content fingerprint for the memo. */
312
+ function fingerprint(text: string): string {
313
+ let hash = 2166136261
314
+ const head = text.slice(0, 4096)
315
+ for (let i = 0; i < head.length; i++) {
316
+ hash ^= head.charCodeAt(i)
317
+ hash = Math.imul(hash, 16777619)
318
+ }
319
+ return `${head.length}:${text.length}:${hash >>> 0}`
320
+ }
321
+
322
+ const MEMO = new Map<string, PlacedLine[]>()
323
+
324
+ /**
325
+ * The layout's memo: the same file content at the same width yields the same rows, so a repaint
326
+ * (the data cadence, a tab switch back) does not re-parse the document. Keyed by path + width +
327
+ * content fingerprint; bounded so a long session cannot grow it without limit.
328
+ */
329
+ export function markdownMemo(path: string, source: string, width: number): PlacedLine[] {
330
+ const key = `${path}\u0000${width}\u0000${fingerprint(source)}`
331
+ const hit = MEMO.get(key)
332
+ if (hit) return hit
333
+ const rows = markdownToLines(source, width)
334
+ MEMO.set(key, rows)
335
+ if (MEMO.size > 6) {
336
+ const oldest = MEMO.keys().next().value
337
+ if (oldest !== undefined) MEMO.delete(oldest)
338
+ }
339
+ return rows
340
+ }
@@ -0,0 +1,42 @@
1
+ // Display safety for the panel's own strings.
2
+ //
3
+ // `monitor.mjs` sanitizes the data layer's output (its contract, unchanged). The panel reads a
4
+ // second set of strings itself — branch names, task ids, state file values, queue entry names —
5
+ // and those never pass through the data layer. This is the panel's own pass, applied to every
6
+ // string before it is laid out and to every string before it is printed as JSON.
7
+ //
8
+ // Two properties the contract pins, both measured by the hostile-payload fixture:
9
+ // * no ESC, BEL, CR or C1 byte survives, in the TUI as well as in text;
10
+ // * the visible text survives: control bytes are *removed*, not used as a truncation point
11
+ // (that is the exact reason E4 dropped blessed and OpenTUI and kept Ink).
12
+
13
+ const OSC_RE = /\u001B\][^\u0007\u001B]*(?:\u0007|\u001B\\)/g
14
+ const CSI_RE = /\u001B\[[0-?]*[ -/]*[@-~]/g
15
+ const C1_CSI_RE = /\u009B[0-?]*[ -/]*[@-~]/g
16
+ const ESC2_RE = /\u001B[@-Z\\-_]/g
17
+ const CTRL_RE =
18
+ /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u200B\u200E\u200F\u202A-\u202E\u2060\u2066-\u2069\uFEFF]/g
19
+
20
+ /** Strip control sequences but keep the text around them. */
21
+ export function sanitize(value: unknown): string {
22
+ return String(value ?? '')
23
+ .replace(OSC_RE, '')
24
+ .replace(CSI_RE, '')
25
+ .replace(C1_CSI_RE, '')
26
+ .replace(ESC2_RE, '')
27
+ .replace(CTRL_RE, '')
28
+ }
29
+
30
+ /** Sanitize every string in a value (keys included), leaving numbers/booleans/null alone. */
31
+ export function sanitizeDeep<T>(value: T): T {
32
+ if (typeof value === 'string') return sanitize(value) as unknown as T
33
+ if (Array.isArray(value)) return value.map((v) => sanitizeDeep(v)) as unknown as T
34
+ if (value && typeof value === 'object') {
35
+ const out: Record<string, unknown> = {}
36
+ for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
37
+ out[sanitize(k)] = sanitizeDeep(v)
38
+ }
39
+ return out as unknown as T
40
+ }
41
+ return value
42
+ }
@@ -0,0 +1,154 @@
1
+ // `state/panel.conf` and `state/panel-page`: the console's own preferences (runtime state).
2
+ //
3
+ // Boundaries (design §6): this file is NOT the project contract (`.pi/team/config.sh`); it is read
4
+ // by the TUI only — `--print`/`--json` never look at it, which is what keeps the machine exits
5
+ // byte-stable under any preference. A missing, unreadable or corrupt file falls back to the
6
+ // defaults; a partial or unknown-key file keeps the keys it does declare. `theme` is a preference
7
+ // but not an overlay item (the overlay has exactly five). The page file is separate and trivial:
8
+ // the last page the human looked at, restored on the next start.
9
+
10
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
11
+ import { dirname } from 'node:path'
12
+ import { BOARD_LANES } from './types.js'
13
+ import type { Density, PageId, ThemeChoice } from './types.js'
14
+ import { isLang, type Lang } from './strings/index.js'
15
+
16
+ export const PANEL_CONF_FILE = 'panel.conf'
17
+ export const PANEL_PAGE_FILE = 'panel-page'
18
+
19
+ export interface Settings {
20
+ lang: Lang
21
+ defaultPage: PageId
22
+ activity: boolean
23
+ mouse: boolean
24
+ density: Density
25
+ theme: ThemeChoice
26
+ /** Board lanes folded explicitly (P123), in the lane legend's order. */
27
+ boardFold: string[]
28
+ /** Board lanes kept unfolded explicitly (P123): they stay unfolded even while empty. */
29
+ boardShow: string[]
30
+ /** Empty lanes fold by default (P123); `false` renders them boxed with the dim empty marker. */
31
+ boardEmptyFold: boolean
32
+ }
33
+
34
+ export const DEFAULT_SETTINGS: Settings = {
35
+ lang: 'zh',
36
+ defaultPage: 1,
37
+ activity: true,
38
+ mouse: true,
39
+ density: 'comfortable',
40
+ theme: 'auto',
41
+ boardFold: [],
42
+ boardShow: [],
43
+ boardEmptyFold: true,
44
+ }
45
+
46
+ function boolOf(value: string): boolean | null {
47
+ const v = value.trim().toLowerCase()
48
+ if (v === '1' || v === 'true' || v === 'on' || v === 'yes') return true
49
+ if (v === '0' || v === 'false' || v === 'off' || v === 'no') return false
50
+ return null
51
+ }
52
+
53
+ function pageOf(value: string): PageId | null {
54
+ const v = value.trim()
55
+ if (v === '1' || v === '2' || v === '3' || v === '4') return Number(v) as PageId
56
+ return null
57
+ }
58
+
59
+ /**
60
+ * A `boardFold`/`boardShow` value (P123): comma list of lane names. Unknown names are dropped,
61
+ * duplicates dedupe, and the survivors come out in the lane legend's order — the same order
62
+ * `serializePanelConf` writes, so a hand-edited list normalises on the first save.
63
+ */
64
+ function lanesOf(value: string): string[] {
65
+ const named = new Set(value.split(',').map((v) => v.trim()))
66
+ return BOARD_LANES.filter((lane) => named.has(lane))
67
+ }
68
+
69
+ /** Parse a `panel.conf` body; unknown keys and unusable values are ignored (defaults survive). */
70
+ export function parsePanelConf(text: string): Settings {
71
+ const out: Settings = { ...DEFAULT_SETTINGS }
72
+ if (/[\u0000-\u0008\u000e-\u001f]/.test(text)) return out // binary garbage → defaults
73
+ for (const raw of String(text).split('\n')) {
74
+ const line = raw.trim()
75
+ if (!line || line.startsWith('#')) continue
76
+ const eq = line.indexOf('=')
77
+ if (eq <= 0) continue
78
+ const key = line.slice(0, eq).trim()
79
+ const value = line.slice(eq + 1).trim()
80
+ if (key === 'lang' && isLang(value)) out.lang = value
81
+ else if (key === 'page') {
82
+ const p = pageOf(value)
83
+ if (p) out.defaultPage = p
84
+ } else if (key === 'activity' || key === 'mouse') {
85
+ const b = boolOf(value)
86
+ if (b !== null) out[key] = b
87
+ } else if (key === 'density' && (value === 'comfortable' || value === 'compact')) out.density = value
88
+ else if (key === 'theme' && (value === 'dark' || value === 'light' || value === 'auto')) out.theme = value
89
+ else if (key === 'boardFold') out.boardFold = lanesOf(value)
90
+ else if (key === 'boardShow') out.boardShow = lanesOf(value)
91
+ else if (key === 'boardEmptyFold') {
92
+ const b = boolOf(value)
93
+ if (b !== null) out.boardEmptyFold = b
94
+ }
95
+ }
96
+ // A lane named by both lists folds: the explicit hide is the stronger statement.
97
+ out.boardShow = out.boardShow.filter((lane) => !out.boardFold.includes(lane))
98
+ return out
99
+ }
100
+
101
+ export function serializePanelConf(s: Settings): string {
102
+ return [
103
+ `lang=${s.lang}`,
104
+ `page=${s.defaultPage}`,
105
+ `activity=${s.activity ? 1 : 0}`,
106
+ `mouse=${s.mouse ? 1 : 0}`,
107
+ `density=${s.density}`,
108
+ `theme=${s.theme}`,
109
+ `boardFold=${BOARD_LANES.filter((lane) => s.boardFold.includes(lane)).join(',')}`,
110
+ `boardShow=${BOARD_LANES.filter((lane) => s.boardShow.includes(lane)).join(',')}`,
111
+ `boardEmptyFold=${s.boardEmptyFold ? 1 : 0}`,
112
+ '',
113
+ ].join('\n')
114
+ }
115
+
116
+ /** The settings the TUI runs with: the file when readable, the defaults otherwise. */
117
+ export function readSettings(file: string): Settings {
118
+ try {
119
+ if (!existsSync(file)) return { ...DEFAULT_SETTINGS }
120
+ return parsePanelConf(readFileSync(file, 'utf8'))
121
+ } catch {
122
+ return { ...DEFAULT_SETTINGS }
123
+ }
124
+ }
125
+
126
+ export function writeSettings(file: string, s: Settings): boolean {
127
+ try {
128
+ mkdirSync(dirname(file), { recursive: true })
129
+ writeFileSync(file, serializePanelConf(s))
130
+ return true
131
+ } catch {
132
+ return false // a state directory the console cannot write must not kill the panel
133
+ }
134
+ }
135
+
136
+ /** The page remembered from the last run, or the fallback when there is none/its content is junk. */
137
+ export function readPage(file: string, fallback: PageId): PageId {
138
+ try {
139
+ if (!existsSync(file)) return fallback
140
+ return pageOf(readFileSync(file, 'utf8')) ?? fallback
141
+ } catch {
142
+ return fallback
143
+ }
144
+ }
145
+
146
+ export function writePage(file: string, page: PageId): boolean {
147
+ try {
148
+ mkdirSync(dirname(file), { recursive: true })
149
+ writeFileSync(file, `${page}\n`)
150
+ return true
151
+ } catch {
152
+ return false
153
+ }
154
+ }