tui-transcript-view.test.ts 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. import { pathToFileURL } from 'node:url';
  2. import { stripVTControlCharacters } from 'node:util';
  3. import { visibleWidth } from '../../src/tui/rendering/text.js';
  4. import { getCapabilities, setCapabilities } from '../../src/tui/engine/public.js';
  5. import { describe, expect, it } from 'vitest';
  6. import { TranscriptView, resolveTuiDecisionColor } from '../../src/tui/transcript/view.js';
  7. import { createTranscriptCell } from '../../src/tui/transcript/model.js';
  8. import { TranscriptStore } from '../../src/tui/transcript/store.js';
  9. import { sanitizeTerminalText } from '../../src/tui/rendering/terminal-text.js';
  10. import {
  11. applyTuiRenderTheme,
  12. createTuiChalk,
  13. getTuiThemeSnapshot,
  14. tuiColors,
  15. } from '../../src/tui/theme/runtime.js';
  16. import { MINIMAX_CODE_DARK_THEME, MINIMAX_CODE_LIGHT_THEME } from '../../src/tui/theme/palettes.js';
  17. const previewDisplayModes = {
  18. revision: 0,
  19. resolveMainDisplayMode: () => 'preview' as const,
  20. };
  21. describe('TranscriptView', () => {
  22. it.each(['assistant', 'assistant-preamble', 'thinking'] as const)(
  23. 'removes control strings from %s content without changing stored model output',
  24. (kind) => {
  25. const control = '\x1b]52;c;U1lOVEhFVElD\x07';
  26. const content = `**Visible**\n\n\`\`\`text\n${control}\x1b[8mSafe code\x1b[0m\n\`\`\``;
  27. const cell = createTranscriptCell({
  28. id: 'untrusted-markdown',
  29. kind,
  30. status: 'running',
  31. content,
  32. createdAtMs: 1,
  33. });
  34. const view = new TranscriptView(() => [cell], {
  35. displayModes: { revision: 0, resolveMainDisplayMode: () => 'expanded' },
  36. });
  37. const rendered = view.render(100).join('\n');
  38. expect(rendered).not.toContain(control);
  39. expect(rendered).not.toContain('\x1b[8m');
  40. expect(stripVTControlCharacters(rendered)).toContain('Safe code');
  41. expect(cell.content).toBe(content);
  42. },
  43. );
  44. it('safely renders every streaming prefix including C1 and unterminated control strings', () => {
  45. const content = 'Visible\x9d52;c;U1lOVEhFVElD\x9c\x1bPprivate payload\x1b\\\x1b[8mtext\x1b[0m';
  46. for (let end = 1; end <= content.length; end++) {
  47. const text = sanitizeTerminalText(content.slice(0, end));
  48. expect(text).not.toMatch(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]/u);
  49. expect(text).not.toContain('private payload');
  50. expect(text).not.toContain('U1lOVEhFVElD');
  51. }
  52. expect(sanitizeTerminalText('中文🙂\ttext\r\nnext\rother')).toBe('中文🙂\ttext\nnext other');
  53. });
  54. it.each(['running', 'succeeded'] as const)(
  55. 'does not emit model terminal controls from a %s assistant cell',
  56. (status) => {
  57. const controls = ['\x1b]52;c;U1lOVEhFVElD\x07', '\x1b]2;FORGED_TITLE\x07', '\x1b[8m'];
  58. const view = new TranscriptView(() => [
  59. createTranscriptCell({
  60. id: 'untrusted-controls',
  61. kind: 'assistant',
  62. status,
  63. content: `Visible ${controls.join('')}model text\x1b[0m`,
  64. createdAtMs: 1,
  65. }),
  66. ]);
  67. const rendered = view.render(100).join('\n');
  68. for (const control of controls) expect(rendered).not.toContain(control);
  69. expect(stripVTControlCharacters(rendered)).toContain('Visible model text');
  70. },
  71. );
  72. it('renders a Markdown question receipt (Plan Review) as formatted prose', () => {
  73. const view = new TranscriptView(() => [
  74. createTranscriptCell({
  75. id: 'question:plan-review-view',
  76. kind: 'question',
  77. status: 'blocked',
  78. title: 'Plan Review',
  79. content: '# Implementation plan\n\n1. Add Runtime projection\n2. Add TUI controls',
  80. contentFormat: 'markdown',
  81. ephemeral: true,
  82. createdAtMs: 1,
  83. }),
  84. ]);
  85. const rendered = view.render(80).join('\n');
  86. // Heading marker + title, and the plan body rendered (heading hash stripped by Markdown).
  87. expect(rendered).toContain('Plan Review');
  88. expect(rendered).toContain('Implementation plan');
  89. expect(rendered).toContain('Add Runtime projection');
  90. expect(rendered).not.toContain('# Implementation plan');
  91. });
  92. it('renders a resolved Markdown question receipt with the decision detail', () => {
  93. const view = new TranscriptView(() => [
  94. createTranscriptCell({
  95. id: 'question:plan-review-resolved-view',
  96. kind: 'question',
  97. status: 'resolved',
  98. title: 'Plan Review',
  99. content: '# Plan\n\nShip it.',
  100. contentFormat: 'markdown',
  101. detail: 'Approved · implementation queued',
  102. ephemeral: true,
  103. createdAtMs: 1,
  104. }),
  105. ]);
  106. const rendered = view.render(80).join('\n');
  107. expect(rendered).toContain('Ship it.');
  108. expect(rendered).toContain('Approved · implementation queued');
  109. });
  110. it('marks user and Assistant blocks as semantic terminal zones', () => {
  111. const view = new TranscriptView(() => [
  112. createTranscriptCell({
  113. id: 'user-semantic-zone',
  114. kind: 'user',
  115. status: 'succeeded',
  116. content: 'Inspect the renderer',
  117. createdAtMs: 1,
  118. }),
  119. createTranscriptCell({
  120. id: 'assistant-semantic-zone',
  121. kind: 'assistant',
  122. status: 'succeeded',
  123. content: 'The renderer is stable.',
  124. createdAtMs: 2,
  125. }),
  126. ]);
  127. const lines = view.render(60);
  128. expect(lines.filter((line) => line.startsWith('\x1b]133;A\x07'))).toHaveLength(2);
  129. expect(lines.filter((line) => line.includes('\x1b]133;B\x07\x1b]133;C\x07'))).toHaveLength(2);
  130. });
  131. it('attaches a pending steer to the active flow as the next instruction', () => {
  132. const width = 60;
  133. const view = new TranscriptView(() => [
  134. createTranscriptCell({
  135. id: 'active-tool',
  136. kind: 'tool',
  137. status: 'running',
  138. title: 'bash',
  139. content: JSON.stringify({ command: 'pnpm test' }),
  140. turnId: 'turn-active',
  141. createdAtMs: 1,
  142. }),
  143. createTranscriptCell({
  144. id: 'pending-steer',
  145. kind: 'user',
  146. status: 'pending',
  147. content: 'Only inspect the login flow',
  148. userPresentation: 'pending-steer',
  149. turnId: 'turn-active',
  150. createdAtMs: 2,
  151. }),
  152. ]);
  153. const lines = view.render(width).map((line) => stripVTControlCharacters(line).trimEnd());
  154. const toolIndex = lines.findIndex((line) => line.includes('pnpm test'));
  155. const steerIndex = lines.findIndex((line) => line.includes('Only inspect the login flow'));
  156. expect(lines[steerIndex]).toBe(' ↳ Next · Only inspect the login flow');
  157. expect(steerIndex).toBe(toolIndex + 1);
  158. expect(lines.every((line) => visibleWidth(line) <= width)).toBe(true);
  159. });
  160. it.each([0, 1, 2, 3, 4, 7])(
  161. 'keeps the pending steer annotation within a %i-column viewport',
  162. (width) => {
  163. const view = new TranscriptView(() => [
  164. createTranscriptCell({
  165. id: 'pending-steer-narrow',
  166. kind: 'user',
  167. status: 'pending',
  168. content: 'Steer',
  169. userPresentation: 'pending-steer',
  170. createdAtMs: 1,
  171. }),
  172. ]);
  173. const lines = view.render(width).map(stripVTControlCharacters);
  174. expect(lines.every((line) => visibleWidth(line) <= width)).toBe(true);
  175. },
  176. );
  177. it('aligns wrapped pending steer content beneath the instruction text', () => {
  178. const view = new TranscriptView(() => [
  179. createTranscriptCell({
  180. id: 'pending-steer-wrapped',
  181. kind: 'user',
  182. status: 'pending',
  183. content: 'Compare this implementation with the other repositories before changing it',
  184. userPresentation: 'pending-steer',
  185. createdAtMs: 1,
  186. }),
  187. ]);
  188. const lines = view.render(32).map((line) => stripVTControlCharacters(line).trimEnd());
  189. expect(lines[0]).toMatch(/^ {2}↳ Next · /u);
  190. expect(lines.length).toBeGreaterThan(1);
  191. expect(lines.slice(1).every((line) => line.startsWith(' '.repeat(11)))).toBe(true);
  192. expect(lines.every((line) => visibleWidth(line) <= 32)).toBe(true);
  193. });
  194. it.each([
  195. ['succeeded', 'Not applied'],
  196. ['blocked', 'Not applied'],
  197. ['cancelled', 'Not applied'],
  198. ['failed', 'May not have applied'],
  199. ] as const)('settles an unconsumed %s steer annotation', (status, label) => {
  200. const view = new TranscriptView(() => [
  201. createTranscriptCell({
  202. id: `pending-steer-${status}`,
  203. kind: 'user',
  204. status,
  205. content: 'Steer',
  206. userPresentation: 'pending-steer',
  207. createdAtMs: 1,
  208. }),
  209. ]);
  210. const rendered = view.render(60).map(stripVTControlCharacters).join('\n');
  211. expect(rendered).toContain(label);
  212. expect(rendered).not.toContain('Next ·');
  213. });
  214. it.each([
  215. [25_400, '25s'],
  216. [60_000, '1min0s'],
  217. [7_770_000, '2h9min30s'],
  218. ])('renders completed Run duration of %s ms as %s', (durationMs, expected) => {
  219. const view = new TranscriptView(() => [
  220. createTranscriptCell({
  221. id: 'turn-duration-1',
  222. kind: 'turn-duration',
  223. status: 'succeeded',
  224. content: '',
  225. durationMs,
  226. createdAtMs: 1,
  227. }),
  228. ]);
  229. expect(view.render(60).join('\n')).toContain(`└ Completed in ${expected}`);
  230. });
  231. it('distinguishes an interrupted Run from a completed response', () => {
  232. const view = new TranscriptView(() => [
  233. createTranscriptCell({
  234. id: 'turn-duration-cancelled',
  235. kind: 'turn-duration',
  236. status: 'cancelled',
  237. content: '',
  238. durationMs: 1_250,
  239. createdAtMs: 1,
  240. }),
  241. ]);
  242. expect(view.render(60).join('\n')).toContain('└ Interrupted after 1s');
  243. });
  244. it.each([
  245. ['running', 'Compacting context', '•'],
  246. ['succeeded', 'Context compacted', '•'],
  247. ['failed', 'Context compaction failed', '×'],
  248. ] as const)(
  249. 'renders Runtime-owned compaction %s as a title-only state row',
  250. (status, title, marker) => {
  251. const view = new TranscriptView(() => [
  252. createTranscriptCell({
  253. id: 'compaction-1',
  254. kind: 'compaction',
  255. status,
  256. title,
  257. content: 'summary must stay hidden',
  258. createdAtMs: 1,
  259. }),
  260. ]);
  261. const rendered = view.render(60).join('\n');
  262. expect(rendered).toContain(marker);
  263. expect(rendered).toContain(title);
  264. expect(rendered).not.toContain('summary must stay hidden');
  265. },
  266. );
  267. it('renders a completed compaction as one compact token-delta row', () => {
  268. const view = new TranscriptView(() => [
  269. createTranscriptCell({
  270. id: 'compaction-1',
  271. kind: 'compaction',
  272. status: 'succeeded',
  273. content: '',
  274. tokensBefore: 14_200,
  275. tokensAfter: 5_100,
  276. createdAtMs: 1,
  277. }),
  278. ]);
  279. expect(view.render(60).join('\n')).toContain('• Context compacted · 14.2k → 5.1k tokens');
  280. });
  281. it('reuses an unchanged transcript frame and invalidates it when a cell changes', () => {
  282. let cells = [
  283. createTranscriptCell({
  284. id: 'assistant-1',
  285. kind: 'assistant',
  286. status: 'succeeded',
  287. content: 'Stable answer',
  288. createdAtMs: 1,
  289. }),
  290. ];
  291. const view = new TranscriptView(() => cells);
  292. const first = view.render(50);
  293. expect(view.render(50)).toBe(first);
  294. const original = cells[0];
  295. if (!original) throw new Error('missing transcript fixture');
  296. cells = [{ ...original, content: 'Updated answer', updatedAtMs: 2 }];
  297. const updated = view.render(50);
  298. expect(updated).not.toBe(first);
  299. expect(updated.join('\n')).toContain('Updated answer');
  300. });
  301. it('renders rich delivery markup as a compact terminal file receipt', () => {
  302. const view = new TranscriptView(() => [
  303. createTranscriptCell({
  304. id: 'assistant-assets',
  305. kind: 'assistant',
  306. status: 'succeeded',
  307. content: [
  308. 'Done.',
  309. '<deliver-assets>',
  310. '<media src="/workspace/chart.png" caption="Chart" />',
  311. '</deliver-assets>',
  312. ].join('\n'),
  313. createdAtMs: 1,
  314. }),
  315. ]);
  316. const rendered = stripVTControlCharacters(view.render(80).join('\n'));
  317. expect(rendered).toContain('Created chart.png ↗ · Chart');
  318. expect(rendered).not.toContain('/workspace/chart.png');
  319. expect(rendered).not.toContain('Chart · /workspace/chart.png');
  320. expect(rendered).not.toContain('<deliver-assets>');
  321. expect(rendered).not.toContain('<media');
  322. });
  323. it('keeps leaked caption text and absolute paths out of the compact receipt', () => {
  324. const path = '/Users/example/workspace/sample-project/output/mavis-architecture.html';
  325. const view = new TranscriptView(() => [
  326. createTranscriptCell({
  327. id: 'assistant-long-asset',
  328. kind: 'assistant',
  329. status: 'succeeded',
  330. content: [
  331. 'Done.',
  332. '<deliver-assets>',
  333. `<media src="${path}" caption="Mavis architecture overview · ${path}RunTurnInput leaked text" />`,
  334. '</deliver-assets>',
  335. ].join('\n'),
  336. createdAtMs: 1,
  337. }),
  338. ]);
  339. const lines = view.render(36).map((line) => stripVTControlCharacters(line).trimEnd());
  340. const receiptLine = lines.findIndex((line) => line.includes('mavis-architecture.html ↗'));
  341. const normalized = lines.join(' ').replace(/\s+/gu, ' ');
  342. expect(receiptLine).toBeGreaterThan(-1);
  343. expect(normalized).toContain('Mavis architecture overview');
  344. expect(normalized).not.toContain('leaked text');
  345. expect(normalized).not.toContain('/Users/example');
  346. });
  347. it('makes the generated file label a highlighted OSC 8 file link when supported', () => {
  348. const originalCapabilities = getCapabilities();
  349. const originalTheme = getTuiThemeSnapshot();
  350. const palette =
  351. originalTheme.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  352. setCapabilities({ ...originalCapabilities, hyperlinks: true });
  353. applyTuiRenderTheme(palette, 3);
  354. try {
  355. const view = new TranscriptView(() => [
  356. createTranscriptCell({
  357. id: 'assistant-linked-asset',
  358. kind: 'assistant',
  359. status: 'succeeded',
  360. content:
  361. '<deliver-assets><media src="/workspace/hello-world.html" caption="hello-world.html" /></deliver-assets>',
  362. createdAtMs: 1,
  363. }),
  364. ]);
  365. const rendered = view.render(80).join('\n');
  366. expect(rendered).toContain(`\x1b]8;;${pathToFileURL('/workspace/hello-world.html').href}\x1b\\`);
  367. expect(rendered).toContain('\x1b[4m');
  368. expect(stripVTControlCharacters(rendered)).toContain('Created hello-world.html ↗');
  369. } finally {
  370. setCapabilities(originalCapabilities);
  371. applyTuiRenderTheme(palette, originalTheme.colorLevel);
  372. }
  373. });
  374. it('aligns assistant prose and preambles on one large dot per segment', () => {
  375. const view = new TranscriptView(() => [
  376. createTranscriptCell({
  377. id: 'assistant-preamble-anchor',
  378. kind: 'assistant-preamble',
  379. status: 'succeeded',
  380. content: 'I will inspect the transcript renderer before changing it.',
  381. createdAtMs: 1,
  382. }),
  383. createTranscriptCell({
  384. id: 'assistant-answer-anchor',
  385. kind: 'assistant',
  386. status: 'succeeded',
  387. content: 'The renderer now keeps wrapped continuation lines aligned with the answer.',
  388. createdAtMs: 2,
  389. }),
  390. ]);
  391. const lines = view
  392. .render(28)
  393. .map((line) => stripVTControlCharacters(line).trimEnd())
  394. .filter(Boolean);
  395. expect(lines.filter((line) => line.startsWith('● '))).toHaveLength(2);
  396. expect(lines.filter((line) => line.includes('●'))).toHaveLength(2);
  397. expect(lines.filter((line) => !line.includes('●')).every((line) => line.startsWith(' '))).toBe(
  398. true,
  399. );
  400. });
  401. it('renders assistant dots in the same tone as the body text', () => {
  402. const original = getTuiThemeSnapshot();
  403. const originalPalette =
  404. original.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  405. applyTuiRenderTheme(MINIMAX_CODE_DARK_THEME, 3);
  406. try {
  407. const view = new TranscriptView(() => [
  408. createTranscriptCell({
  409. id: 'assistant-tone-anchor',
  410. kind: 'assistant',
  411. status: 'succeeded',
  412. content: 'Same tone',
  413. createdAtMs: 1,
  414. }),
  415. ]);
  416. const line = view.render(28)[0] ?? '';
  417. const color = createTuiChalk({ colorLevel: 3 });
  418. expect(line).toContain(color.hex(MINIMAX_CODE_DARK_THEME.colors.text)('●'));
  419. expect(line).not.toContain(color.hex(MINIMAX_CODE_DARK_THEME.colors.signal)('●'));
  420. } finally {
  421. applyTuiRenderTheme(originalPalette, original.colorLevel);
  422. }
  423. });
  424. it('keeps assistant dots and Markdown within narrow terminal widths', () => {
  425. const view = new TranscriptView(() => [
  426. createTranscriptCell({
  427. id: 'assistant-narrow-anchor',
  428. kind: 'assistant',
  429. status: 'succeeded',
  430. content: [
  431. 'A compact answer with a long line that wraps.',
  432. '',
  433. '```ts',
  434. 'const value = 1;',
  435. '```',
  436. ].join('\n'),
  437. createdAtMs: 1,
  438. }),
  439. ]);
  440. const lines = view.render(28);
  441. const plainLines = lines.map((line) => stripVTControlCharacters(line).trimEnd());
  442. expect(plainLines[0]).toMatch(/^● /u);
  443. expect(plainLines.slice(1).every((line) => !line.includes('●'))).toBe(true);
  444. expect(plainLines.join('\n')).toContain('const value = 1;');
  445. expect(lines.every((line) => visibleWidth(line) <= 28)).toBe(true);
  446. for (const width of [0, 1, 2, 3, 4, 8]) {
  447. expect(view.render(width).every((line) => visibleWidth(line) <= width)).toBe(true);
  448. }
  449. });
  450. it('renders assistant code blocks as compact highlighted content without chrome', () => {
  451. const view = new TranscriptView(() => [
  452. createTranscriptCell({
  453. id: 'assistant-code',
  454. kind: 'assistant',
  455. status: 'succeeded',
  456. content:
  457. "```ts title=policy\nconst startupExecutionPolicy = 'quarantined-with-a-long-suffix';\n```",
  458. createdAtMs: 1,
  459. }),
  460. ]);
  461. const lines = view.render(28);
  462. const plainLines = lines.map((line) => stripVTControlCharacters(line).trimEnd());
  463. expect(plainLines.join('\n')).not.toContain('```');
  464. expect(plainLines.join('\n')).not.toMatch(/[╭╮╰╯│]/u);
  465. expect(plainLines.join('\n')).toContain('startupExecutionPolicy');
  466. expect(plainLines.join('\n')).toContain('quarantined-with-a-');
  467. expect(lines.every((line) => visibleWidth(line) <= 28)).toBe(true);
  468. });
  469. it('invalidates a streaming frame when content changes at the same Runtime timestamp', () => {
  470. let cells = [
  471. createTranscriptCell({
  472. id: 'thinking-live',
  473. kind: 'thinking',
  474. status: 'running',
  475. content: 'First chunk',
  476. createdAtMs: 1,
  477. updatedAtMs: 10,
  478. expanded: true,
  479. }),
  480. ];
  481. const view = new TranscriptView(() => cells);
  482. const first = view.render(50);
  483. const current = cells[0];
  484. if (!current) throw new Error('missing transcript fixture');
  485. cells = [{ ...current, content: 'First chunk\nSecond chunk' }];
  486. const second = view.render(50);
  487. expect(second).not.toBe(first);
  488. expect(second.join('\n')).toContain('Second chunk');
  489. });
  490. it('renders Pi Markdown tables, nested blocks, and partial closing fences', () => {
  491. const view = new TranscriptView(() => [
  492. createTranscriptCell({
  493. id: 'assistant-markdown',
  494. kind: 'assistant',
  495. status: 'running',
  496. content: [
  497. '| Name | State |',
  498. '| --- | --- |',
  499. '| Markdown | **ready** |',
  500. '',
  501. '> Notes',
  502. '> - nested `code`',
  503. '',
  504. '```ts',
  505. 'const value = 1;',
  506. '``',
  507. ].join('\n'),
  508. createdAtMs: 1,
  509. }),
  510. ]);
  511. const rendered = view.render(60).join('\n');
  512. expect(rendered).toContain('│ Name');
  513. expect(rendered).toContain('│ Notes');
  514. expect(rendered).toContain('- nested code');
  515. expect(rendered).toContain('const value = 1;');
  516. expect(rendered.match(/^\s*``\s*$/gmu)).toBeNull();
  517. });
  518. it('renders local inspection reports as a complete bordered table', () => {
  519. const view = new TranscriptView(() => [
  520. createTranscriptCell({
  521. id: 'inspection-status',
  522. kind: 'inspection',
  523. status: 'succeeded',
  524. content: 'MCode status\nAccount: Connected with MiniMax',
  525. inspection: {
  526. title: 'MCode status',
  527. badge: { label: 'READY', tone: 'success' },
  528. sections: [
  529. {
  530. title: 'Account',
  531. rows: [
  532. { label: 'Account', value: 'Connected with MiniMax', tone: 'success' },
  533. { label: 'Provider', value: 'minimax' },
  534. ],
  535. },
  536. {
  537. title: 'Model',
  538. rows: [{ label: 'Active', value: 'minimax/MiniMax-M3' }],
  539. },
  540. ],
  541. footer: 'Read only',
  542. },
  543. createdAtMs: 1,
  544. }),
  545. ]);
  546. const rendered = stripVTControlCharacters(view.render(60).join('\n'));
  547. expect(rendered).toContain('╭─');
  548. expect(rendered).toMatch(/│ MCode status\s+● READY │/u);
  549. expect(rendered).toContain('READY');
  550. expect(rendered).toContain('├─ Account ');
  551. expect(rendered).toMatch(/│ Account\s+│ Connected with MiniMax\s+│/u);
  552. expect(rendered).toContain('├─ Model ');
  553. expect(rendered).toMatch(/│ Read only\s+│/u);
  554. expect(rendered).toContain('╰─');
  555. expect(rendered.split('\n').every((line) => visibleWidth(line) === 60)).toBe(true);
  556. });
  557. it('stacks inspection labels and values on narrow terminals', () => {
  558. const view = new TranscriptView(() => [
  559. createTranscriptCell({
  560. id: 'inspection-config',
  561. kind: 'inspection',
  562. status: 'succeeded',
  563. content: 'Effective configuration',
  564. inspection: {
  565. title: 'Effective configuration',
  566. badge: { label: 'READ ONLY', tone: 'accent' },
  567. sections: [
  568. {
  569. title: 'Location',
  570. rows: [
  571. {
  572. label: 'Source',
  573. value: '/home/dev/.minimax/config.yaml',
  574. },
  575. ],
  576. },
  577. ],
  578. },
  579. createdAtMs: 1,
  580. }),
  581. ]);
  582. const rendered = stripVTControlCharacters(view.render(28).join('\n'));
  583. expect(rendered).toMatch(/│ Source\s+│/u);
  584. expect(rendered).toContain('│ /home/dev/.minimax');
  585. expect(rendered).toContain('│ /config.yaml');
  586. expect(rendered).not.toMatch(/│ Source\s+│?\s*\/home/u);
  587. expect(rendered).toContain('╰─');
  588. expect(rendered.split('\n').every((line) => visibleWidth(line) === 28)).toBe(true);
  589. });
  590. it('routes usage inspection reports through the compact usage visualization', () => {
  591. const view = new TranscriptView(() => [
  592. createTranscriptCell({
  593. id: 'inspection-usage',
  594. kind: 'inspection',
  595. status: 'succeeded',
  596. content: 'Session usage',
  597. inspection: {
  598. title: 'Session usage',
  599. badge: { label: 'HIDDEN', tone: 'accent' },
  600. sections: [{ title: 'Internal details', rows: [{ label: 'Turns', value: '9' }] }],
  601. footer: 'Runtime source',
  602. visualization: {
  603. kind: 'usage',
  604. model: 'minimax-cn-coding-plan/MiniMax-M3',
  605. inputTokens: 145_000,
  606. outputTokens: 1_400,
  607. totalTokens: 146_400,
  608. context: null,
  609. },
  610. },
  611. createdAtMs: 1,
  612. }),
  613. ]);
  614. const rendered = stripVTControlCharacters(view.render(60).join('\n'));
  615. expect(rendered).toContain('Usage');
  616. expect(rendered).not.toContain('HIDDEN');
  617. expect(rendered).not.toContain('Internal details');
  618. expect(rendered).not.toContain('Runtime source');
  619. expect(rendered.split('\n').every((line) => visibleWidth(line) <= 60)).toBe(true);
  620. });
  621. it('reuses unaffected item renders when Ctrl+O changes detail presentation', () => {
  622. const view = new TranscriptView(() => [
  623. createTranscriptCell({
  624. id: 'assistant-before-details',
  625. kind: 'assistant',
  626. status: 'succeeded',
  627. content: 'Stable answer',
  628. createdAtMs: 1,
  629. }),
  630. createTranscriptCell({
  631. id: 'tool-details',
  632. kind: 'tool',
  633. status: 'succeeded',
  634. title: 'bash',
  635. content: '{"command":"pnpm test"}',
  636. detail: 'result line 1\nresult line 2',
  637. createdAtMs: 2,
  638. }),
  639. createTranscriptCell({
  640. id: 'summary-after-details',
  641. kind: 'final-summary',
  642. status: 'succeeded',
  643. content: 'Stable summary',
  644. createdAtMs: 3,
  645. }),
  646. ]);
  647. view.render(60);
  648. view.toggleDetailMode();
  649. const detailed = view.render(60).join('\n');
  650. const performance = view.getPerformanceSnapshot();
  651. expect(detailed).toContain('result line 1');
  652. expect(performance.unitCacheHits).toBe(2);
  653. expect(performance.unitCacheMisses).toBe(1);
  654. });
  655. it('rerenders only the active item while detailed content streams', () => {
  656. const store = new TranscriptStore([
  657. createTranscriptCell({
  658. id: 'settled-before-stream',
  659. kind: 'assistant',
  660. status: 'succeeded',
  661. content: 'Stable answer',
  662. createdAtMs: 1,
  663. }),
  664. createTranscriptCell({
  665. id: 'active-thinking-stream',
  666. kind: 'thinking',
  667. status: 'running',
  668. content: 'Inspecting',
  669. createdAtMs: 2,
  670. }),
  671. ]);
  672. const view = new TranscriptView(store);
  673. view.toggleDetailMode();
  674. view.render(60);
  675. store.upsert({
  676. id: 'active-thinking-stream',
  677. content: 'Inspecting the renderer',
  678. updatedAtMs: 3,
  679. });
  680. const rendered = view.render(60).join('\n');
  681. const performance = view.getPerformanceSnapshot();
  682. expect(rendered).toContain('Inspecting the renderer');
  683. expect(performance.unitCacheHits).toBe(1);
  684. expect(performance.unitCacheMisses).toBe(1);
  685. });
  686. it('uses the generic fallback for tools without a built-in definition', () => {
  687. const view = new TranscriptView(() => [
  688. createTranscriptCell({
  689. id: 'deploy-1',
  690. kind: 'tool',
  691. status: 'succeeded',
  692. title: 'deploy',
  693. content: 'preview',
  694. createdAtMs: 1,
  695. }),
  696. ]);
  697. expect(view.render(50).join('\n')).toContain('Deploy (preview)');
  698. });
  699. it('renders product Web Tool summaries and hides output in compact mode', () => {
  700. const view = new TranscriptView(() => [
  701. createTranscriptCell({
  702. id: 'web-search',
  703. kind: 'tool',
  704. status: 'succeeded',
  705. title: 'mcp__matrix__web_search',
  706. content: '{"query":"AI news today 2026-08-07"}',
  707. detail: 'private search result',
  708. createdAtMs: 1,
  709. }),
  710. createTranscriptCell({
  711. id: 'web-fetch',
  712. kind: 'tool',
  713. status: 'succeeded',
  714. title: 'web_fetch',
  715. content: '{"url":"https://example.com/article"}',
  716. detail: 'private fetched body',
  717. createdAtMs: 2,
  718. }),
  719. ]);
  720. const compact = stripVTControlCharacters(view.render(90).join('\n'));
  721. expect(compact).toContain('Used WebSearch (AI news today 2026-08-07)');
  722. expect(compact).toContain('Used WebFetch (https://example.com/article)');
  723. expect(compact).not.toContain('matrix.web_search');
  724. expect(compact).not.toContain('private search result');
  725. expect(compact).not.toContain('private fetched body');
  726. view.toggleDetailMode();
  727. const detailed = stripVTControlCharacters(view.render(90).join('\n'));
  728. expect(detailed).toContain('private search result');
  729. expect(detailed).toContain('private fetched body');
  730. });
  731. it('keeps non-Web MCP identities while hiding their output in compact mode', () => {
  732. const view = new TranscriptView(() => [
  733. createTranscriptCell({
  734. id: 'mcp-1',
  735. kind: 'tool',
  736. status: 'succeeded',
  737. title: 'mcp__jira__create_issue',
  738. content: '{"description":"Broken checkout"}',
  739. detail: 'issue-123',
  740. createdAtMs: 1,
  741. }),
  742. ]);
  743. const compact = stripVTControlCharacters(view.render(72).join('\n'));
  744. expect(compact).toContain('Called jira.create_issue (Broken checkout)');
  745. expect(compact).not.toContain('issue-123');
  746. view.toggleDetailMode();
  747. expect(stripVTControlCharacters(view.render(72).join('\n'))).toContain('issue-123');
  748. });
  749. it('keeps failed Web Tool errors hidden until detailed mode is requested', () => {
  750. const view = new TranscriptView(() => [
  751. createTranscriptCell({
  752. id: 'web-fetch-failed',
  753. kind: 'tool',
  754. status: 'failed',
  755. title: 'web_fetch',
  756. content: '{"url":"https://example.com/missing"}',
  757. detail: 'HTTP 404 response body',
  758. createdAtMs: 1,
  759. }),
  760. ]);
  761. const compact = stripVTControlCharacters(view.render(72).join('\n'));
  762. expect(compact).toContain('WebFetch failed (https://example.com/missing)');
  763. expect(compact).not.toContain('HTTP 404 response body');
  764. view.toggleDetailMode();
  765. expect(stripVTControlCharacters(view.render(72).join('\n'))).toContain(
  766. 'HTTP 404 response body',
  767. );
  768. });
  769. it('hides structured changes until Tool details are requested', () => {
  770. const view = new TranscriptView(() => [
  771. createTranscriptCell({
  772. id: 'edit-1',
  773. kind: 'tool',
  774. status: 'succeeded',
  775. title: 'edit',
  776. content: '{"path":"src/greeting.ts"}',
  777. structuredPreview: {
  778. schemaVersion: 1,
  779. state: 'applied',
  780. blocks: [
  781. {
  782. kind: 'diff',
  783. path: 'src/greeting.ts',
  784. diff: '- old greeting\n+ new greeting',
  785. addedLines: 1,
  786. removedLines: 1,
  787. truncated: false,
  788. },
  789. ],
  790. },
  791. createdAtMs: 1,
  792. turnId: 'turn-1',
  793. }),
  794. ]);
  795. const compact = stripVTControlCharacters(view.render(60).join('\n'));
  796. expect(compact).toContain('Edited (src/greeting.ts)');
  797. expect(compact).not.toContain('Applied');
  798. expect(compact).not.toContain('- old greeting');
  799. expect(compact).not.toContain('+ new greeting');
  800. view.toggleDetailMode();
  801. const detailed = stripVTControlCharacters(view.render(60).join('\n'));
  802. expect(detailed).toContain('Applied');
  803. expect(detailed).toContain('- old greeting');
  804. expect(detailed).toContain('+ new greeting');
  805. });
  806. it.each([{ width: 80 }, { width: 40 }, { width: 28 }])(
  807. 'keeps Tool rows within the terminal width at $width columns',
  808. ({ width }) => {
  809. const view = new TranscriptView(() => [
  810. createTranscriptCell({
  811. id: `responsive-tool-${width}`,
  812. kind: 'tool',
  813. status: 'succeeded',
  814. title: 'bash',
  815. content: JSON.stringify({ command: 'pnpm test' }),
  816. detail: 'All tests passed',
  817. createdAtMs: 1,
  818. }),
  819. ]);
  820. const lines = view.render(width);
  821. expect(lines.every((line) => visibleWidth(line) <= width)).toBe(true);
  822. },
  823. );
  824. it('avoids duplicate structured success output but keeps the failure reason', () => {
  825. const preview = {
  826. schemaVersion: 1 as const,
  827. state: 'not-applied' as const,
  828. blocks: [
  829. {
  830. kind: 'diff' as const,
  831. path: 'src/greeting.ts',
  832. diff: '- old greeting\n+ new greeting',
  833. addedLines: 1,
  834. removedLines: 1,
  835. truncated: false,
  836. },
  837. ],
  838. };
  839. const succeeded = createTranscriptCell({
  840. id: 'edit-succeeded',
  841. kind: 'tool',
  842. status: 'succeeded',
  843. title: 'edit',
  844. content: '{"path":"src/greeting.ts"}',
  845. detail: 'duplicate raw success payload',
  846. structuredPreview: { ...preview, state: 'applied' },
  847. createdAtMs: 1,
  848. });
  849. const failed = createTranscriptCell({
  850. ...succeeded,
  851. id: 'edit-failed',
  852. status: 'failed',
  853. detail: 'Permission denied',
  854. structuredPreview: preview,
  855. });
  856. const successView = new TranscriptView(() => [succeeded]);
  857. const failureView = new TranscriptView(() => [failed]);
  858. successView.toggleDetailMode();
  859. failureView.toggleDetailMode();
  860. const successOutput = stripVTControlCharacters(successView.render(64).join('\n'));
  861. const failureOutput = stripVTControlCharacters(failureView.render(64).join('\n'));
  862. expect(successOutput).toContain('Applied');
  863. expect(successOutput).not.toContain('duplicate raw success payload');
  864. expect(failureOutput).toContain('Not applied');
  865. expect(failureOutput).toContain('Permission denied');
  866. });
  867. it('keeps completed Diff previews hidden until inline details are requested', () => {
  868. const view = new TranscriptView(() => [
  869. createTranscriptCell({
  870. id: 'edit-large',
  871. kind: 'tool',
  872. status: 'succeeded',
  873. title: 'edit',
  874. content: '{"path":"src/large.ts"}',
  875. structuredPreview: {
  876. schemaVersion: 1,
  877. state: 'applied',
  878. blocks: [
  879. {
  880. kind: 'diff',
  881. path: 'src/large.ts',
  882. diff: Array.from({ length: 8 }, (_, index) => `+ line ${index + 1}`).join('\n'),
  883. addedLines: 8,
  884. removedLines: 0,
  885. truncated: false,
  886. },
  887. ],
  888. },
  889. createdAtMs: 1,
  890. }),
  891. ]);
  892. const compact = stripVTControlCharacters(view.render(60).join('\n'));
  893. expect(compact).toContain('Edited (src/large.ts)');
  894. expect(compact).not.toContain('Applied');
  895. expect(compact).not.toContain('lines hidden');
  896. expect(compact).not.toContain('+ line 8');
  897. expect(view.toggleDetailMode()).toBe('detailed');
  898. const detailed = stripVTControlCharacters(view.render(60).join('\n'));
  899. expect(detailed).toContain('+ line 8');
  900. expect(detailed).not.toContain('lines hidden');
  901. });
  902. it('renders intent and execution as one compact visual rail', () => {
  903. const view = new TranscriptView(() => [
  904. createTranscriptCell({
  905. id: 'user-1',
  906. kind: 'user',
  907. status: 'succeeded',
  908. content: 'Please **inspect** this.',
  909. createdAtMs: 1,
  910. turnId: 'turn-1',
  911. }),
  912. createTranscriptCell({
  913. id: 'thinking-1',
  914. kind: 'thinking',
  915. status: 'running',
  916. content: 'Reading the repository',
  917. createdAtMs: 2,
  918. turnId: 'turn-1',
  919. expanded: true,
  920. }),
  921. createTranscriptCell({
  922. id: 'tool-1',
  923. kind: 'tool',
  924. status: 'running',
  925. title: 'read',
  926. content: 'README.md',
  927. createdAtMs: 3,
  928. turnId: 'turn-1',
  929. }),
  930. createTranscriptCell({
  931. id: 'assistant-1',
  932. kind: 'assistant',
  933. status: 'running',
  934. content: 'I found `one` result.',
  935. createdAtMs: 4,
  936. turnId: 'turn-1',
  937. }),
  938. createTranscriptCell({
  939. id: 'error-1',
  940. kind: 'error',
  941. status: 'failed',
  942. content: 'provider unavailable',
  943. createdAtMs: 5,
  944. }),
  945. ]);
  946. const lines = view.render(50);
  947. const rendered = lines.join('\n');
  948. expect(rendered).toContain('› Please inspect this.');
  949. expect(rendered).not.toContain('YOU');
  950. expect(rendered).toContain('├ • Thinking…');
  951. expect(rendered).toContain('│ Reading the repository');
  952. expect(rendered).toContain('└ • Reading (README.md)');
  953. expect(rendered).toContain('I found one result.');
  954. expect(rendered).toContain('× Error provider unavailable');
  955. const thinkingIndex = lines.findIndex((line) => line.includes('Reading the repository'));
  956. expect(lines[thinkingIndex + 1]).toContain('Reading (README.md)');
  957. });
  958. it('renders user messages as a vertically padded full-width prompt band', () => {
  959. const original = getTuiThemeSnapshot();
  960. const originalPalette =
  961. original.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  962. applyTuiRenderTheme(MINIMAX_CODE_LIGHT_THEME, 3);
  963. try {
  964. const view = new TranscriptView(() => [
  965. createTranscriptCell({
  966. id: 'user-band',
  967. kind: 'user',
  968. status: 'succeeded',
  969. content: 'Short message',
  970. createdAtMs: 1,
  971. }),
  972. ]);
  973. const lines = view.render(24);
  974. expect(lines).toHaveLength(3);
  975. expect(lines.map(stripVTControlCharacters)).toEqual([
  976. ' '.repeat(24),
  977. ' › Short message'.padEnd(24),
  978. ' '.repeat(24),
  979. ]);
  980. expect(lines.join('\n')).not.toContain('YOU');
  981. expect(lines.every((line) => line.includes('\u001B[48;2;245;245;245m'))).toBe(true);
  982. expect(lines.every((line) => visibleWidth(line) === 24)).toBe(true);
  983. } finally {
  984. applyTuiRenderTheme(originalPalette, original.colorLevel);
  985. }
  986. });
  987. it('renders structured attachment cards before user text and omits an empty text band', () => {
  988. const withText = new TranscriptView(() => [
  989. createTranscriptCell({
  990. id: 'user-with-attachments',
  991. kind: 'user',
  992. status: 'succeeded',
  993. content: 'Inspect these files',
  994. attachments: [
  995. {
  996. type: 'image',
  997. fileName: 'diagram.png',
  998. mimeType: 'image/png',
  999. sizeBytes: 2048,
  1000. },
  1001. {
  1002. type: 'file',
  1003. fileName: 'notes.md',
  1004. mimeType: 'text/markdown',
  1005. },
  1006. {
  1007. type: 'file',
  1008. fileName: 'walkthrough.mov',
  1009. mimeType: 'video/quicktime',
  1010. sizeBytes: 4096,
  1011. },
  1012. ],
  1013. createdAtMs: 1,
  1014. }),
  1015. ]);
  1016. const rendered = withText.render(48).map(stripVTControlCharacters);
  1017. const imageIndex = rendered.findIndex((line) => line.includes('Image diagram.png'));
  1018. const fileIndex = rendered.findIndex((line) => line.includes('File notes.md'));
  1019. const videoIndex = rendered.findIndex((line) => line.includes('Video walkthrough.mov'));
  1020. const textIndex = rendered.findIndex((line) => line.includes('› Inspect these files'));
  1021. expect(imageIndex).toBeGreaterThanOrEqual(0);
  1022. expect(fileIndex).toBeGreaterThan(imageIndex);
  1023. expect(videoIndex).toBeGreaterThan(fileIndex);
  1024. expect(textIndex).toBeGreaterThan(videoIndex);
  1025. expect(rendered.join('\n')).not.toContain('Attachments:');
  1026. const attachmentOnly = new TranscriptView(() => [
  1027. createTranscriptCell({
  1028. id: 'attachment-only',
  1029. kind: 'user',
  1030. status: 'succeeded',
  1031. content: '',
  1032. attachments: [
  1033. {
  1034. type: 'image',
  1035. fileName: 'only.png',
  1036. mimeType: 'image/png',
  1037. sizeBytes: 1024,
  1038. },
  1039. ],
  1040. createdAtMs: 1,
  1041. }),
  1042. ]);
  1043. const attachmentOnlyLines = attachmentOnly.render(48).map(stripVTControlCharacters);
  1044. expect(attachmentOnlyLines.join('\n')).toContain('Image only.png');
  1045. expect(attachmentOnlyLines.every((line) => !line.includes('›'))).toBe(true);
  1046. });
  1047. it('shows a bounded result preview below a completed tool', () => {
  1048. const view = new TranscriptView(
  1049. () => [
  1050. createTranscriptCell({
  1051. id: 'tool-1',
  1052. kind: 'tool',
  1053. status: 'succeeded',
  1054. title: 'bash',
  1055. content: 'pnpm test',
  1056. detail: ['114 tests passed', '0 failed', 'completed in 1.4s', 'coverage 92%'].join('\n'),
  1057. createdAtMs: 1,
  1058. }),
  1059. ],
  1060. { displayModes: previewDisplayModes },
  1061. );
  1062. const lines = view.render(40);
  1063. const rendered = lines.join('\n');
  1064. expect(rendered).toContain('└ • Ran pnpm test · 4 output lines');
  1065. expect(rendered).toContain('$ pnpm test');
  1066. expect(rendered).toContain('114 tests passed');
  1067. expect(rendered).toContain('1 line hidden');
  1068. expect(rendered).toContain('coverage 92%');
  1069. expect(rendered).not.toContain('completed in 1.4s');
  1070. });
  1071. it('distinguishes a running background bash dispatch from a foreground command', () => {
  1072. const view = new TranscriptView(
  1073. () => [
  1074. createTranscriptCell({
  1075. id: 'tool-background-running',
  1076. kind: 'tool',
  1077. status: 'running',
  1078. title: 'bash',
  1079. content: JSON.stringify({ command: 'sleep 30', run_in_background: true }),
  1080. createdAtMs: 1,
  1081. }),
  1082. ],
  1083. { displayModes: previewDisplayModes },
  1084. );
  1085. const rendered = view.render(80).map(stripVTControlCharacters).join('\n');
  1086. expect(rendered).toContain('Starting background task');
  1087. expect(rendered).toContain('$ sleep 30');
  1088. expect(rendered).not.toContain('Running ');
  1089. });
  1090. it('shows the task id after a background bash dispatch starts', () => {
  1091. const view = new TranscriptView(
  1092. () => [
  1093. createTranscriptCell({
  1094. id: 'tool-background-started',
  1095. kind: 'tool',
  1096. status: 'succeeded',
  1097. title: 'bash',
  1098. content: JSON.stringify({ command: 'sleep 30', run_in_background: true }),
  1099. detail:
  1100. '<bash_background task_id="bg_abc123">\nStarted local background bash task bg_abc123.\n</bash_background>',
  1101. createdAtMs: 1,
  1102. }),
  1103. ],
  1104. { displayModes: previewDisplayModes },
  1105. );
  1106. const rendered = view.render(80).map(stripVTControlCharacters).join('\n');
  1107. expect(rendered).toContain('Started background task bg_abc123');
  1108. expect(rendered).toContain('$ sleep 30');
  1109. expect(rendered).not.toContain('3 output lines');
  1110. expect(rendered).not.toContain('<bash_background');
  1111. });
  1112. it('recognizes replayed background bash history from its result envelope', () => {
  1113. const view = new TranscriptView(() => [
  1114. createTranscriptCell({
  1115. id: 'tool-background-replayed',
  1116. kind: 'tool',
  1117. status: 'succeeded',
  1118. title: 'bash',
  1119. content: JSON.stringify({ command: 'sleep 30' }),
  1120. detail:
  1121. '<bash_background task_id="bg_replayed">\nStarted local background bash task bg_replayed.\n</bash_background>',
  1122. createdAtMs: 1,
  1123. }),
  1124. ]);
  1125. const rendered = view.render(80).map(stripVTControlCharacters).join('\n');
  1126. expect(rendered).toContain('Started background task bg_replayed');
  1127. expect(rendered).not.toContain('Ran 3 output lines');
  1128. });
  1129. it('keeps multiline shell commands out of the single-line execution summary', () => {
  1130. const command = [
  1131. "git commit -q -F - <<'EOF'",
  1132. 'fix(tui): keep execution rows aligned',
  1133. '',
  1134. 'Keep multiline commit messages on the detail rail.',
  1135. 'EOF',
  1136. ].join('\n');
  1137. const view = new TranscriptView(() => [
  1138. createTranscriptCell({
  1139. id: 'tool-multiline-shell',
  1140. kind: 'tool',
  1141. status: 'succeeded',
  1142. title: 'bash',
  1143. content: JSON.stringify({ command }),
  1144. createdAtMs: 1,
  1145. }),
  1146. ]);
  1147. const compact = view.render(80).map(stripVTControlCharacters);
  1148. expect(compact).toEqual(["└ • Ran git commit -q -F - <<'EOF'… · no output"]);
  1149. expect(compact.join('\n')).not.toContain('fix(tui): keep execution rows aligned');
  1150. view.toggleDetailMode();
  1151. const detailed = view.render(80).map(stripVTControlCharacters);
  1152. expect(detailed[0]).toBe("└ • Ran git commit -q -F - <<'EOF'… · no output");
  1153. expect(detailed[1]).toBe(" $ git commit -q -F - <<'EOF'");
  1154. expect(detailed[2]).toBe(' fix(tui): keep execution rows aligned');
  1155. });
  1156. it('shows the shell command alongside the output count on the collapsed row', () => {
  1157. const view = new TranscriptView(() => [
  1158. createTranscriptCell({
  1159. id: 'tool-shell-summary',
  1160. kind: 'tool',
  1161. status: 'succeeded',
  1162. title: 'bash',
  1163. content: JSON.stringify({ command: 'git merge origin/preview_train' }),
  1164. detail: 'Updating a1b2c3d..e4f5g6h\nFast-forward',
  1165. createdAtMs: 1,
  1166. }),
  1167. ]);
  1168. const rendered = view.render(80).map(stripVTControlCharacters);
  1169. expect(rendered[0]).toBe('└ • Ran git merge origin/preview_train · 2 output lines');
  1170. });
  1171. it('syntax-highlights executable commands on the collapsed shell row', () => {
  1172. const original = getTuiThemeSnapshot();
  1173. const originalPalette =
  1174. original.appearance === 'light' ? MINIMAX_CODE_LIGHT_THEME : MINIMAX_CODE_DARK_THEME;
  1175. applyTuiRenderTheme(MINIMAX_CODE_DARK_THEME, 3);
  1176. try {
  1177. const command = 'ls packages/tui/src/ && echo "---" && ls packages/tui/src/tui/ 2>/dev/null';
  1178. const view = new TranscriptView(() => [
  1179. createTranscriptCell({
  1180. id: 'tool-shell-highlighted-summary',
  1181. kind: 'tool',
  1182. status: 'succeeded',
  1183. title: 'bash',
  1184. content: JSON.stringify({ command }),
  1185. detail: 'account\nacp',
  1186. createdAtMs: 1,
  1187. }),
  1188. ]);
  1189. const rendered = view.render(140)[0] ?? '';
  1190. const ansi = createTuiChalk({ colorLevel: 3 });
  1191. expect(stripVTControlCharacters(rendered)).toBe(`└ • Ran ${command} · 2 output lines`);
  1192. expect(rendered).toContain(ansi.bold.hex(tuiColors.accent)('ls'));
  1193. expect(rendered).toContain(ansi.bold.hex(tuiColors.accent)('echo'));
  1194. } finally {
  1195. applyTuiRenderTheme(originalPalette, original.colorLevel);
  1196. }
  1197. });
  1198. it('trims the shell command but keeps the output count when the row is narrow', () => {
  1199. const view = new TranscriptView(() => [
  1200. createTranscriptCell({
  1201. id: 'tool-shell-narrow',
  1202. kind: 'tool',
  1203. status: 'succeeded',
  1204. title: 'bash',
  1205. content: JSON.stringify({
  1206. command: 'pnpm --filter @mavis/ui exec vitest run packages/ui/test/unit/example.test.tsx',
  1207. }),
  1208. detail: 'ok',
  1209. createdAtMs: 1,
  1210. }),
  1211. ]);
  1212. const rendered = view.render(56).map(stripVTControlCharacters);
  1213. expect(rendered[0]).toContain('1 output line');
  1214. expect(rendered[0]).toContain('…');
  1215. expect(rendered[0]?.length).toBeLessThanOrEqual(56);
  1216. });
  1217. it('keeps failed search queries on a single execution-summary row', () => {
  1218. const view = new TranscriptView(() => [
  1219. createTranscriptCell({
  1220. id: 'tool-multiline-search',
  1221. kind: 'tool',
  1222. status: 'failed',
  1223. title: 'search',
  1224. content: JSON.stringify({ query: 'compact",\noutput_mode' }),
  1225. detail: 'No matches found',
  1226. createdAtMs: 1,
  1227. }),
  1228. ]);
  1229. const compact = view.render(80).map(stripVTControlCharacters);
  1230. expect(compact[0]).toBe('└ × Search failed (compact",)');
  1231. expect(compact[0]).not.toContain('\n');
  1232. });
  1233. it('labels an invalid grep regex separately from a generic search failure', () => {
  1234. const view = new TranscriptView(() => [
  1235. createTranscriptCell({
  1236. id: 'tool-invalid-regex',
  1237. kind: 'tool',
  1238. status: 'failed',
  1239. title: 'grep',
  1240. content: JSON.stringify({ pattern: '(?:fork: async|fork(input)' }),
  1241. detail: 'Invalid search pattern: unclosed group.',
  1242. toolErrorCode: 'invalid_regex',
  1243. createdAtMs: 1,
  1244. }),
  1245. ]);
  1246. const compact = view.render(100).map(stripVTControlCharacters);
  1247. expect(compact[0]).toBe('└ × Invalid search pattern ((?:fork: async|fork(input))');
  1248. });
  1249. it('shows live Thinking detail and fully collapses it on completion', () => {
  1250. const running = createTranscriptCell({
  1251. id: 'thinking-running',
  1252. kind: 'thinking',
  1253. status: 'running',
  1254. content: 'Inspecting the repository',
  1255. createdAtMs: 1_000,
  1256. updatedAtMs: 1_600,
  1257. });
  1258. const completed = createTranscriptCell({
  1259. ...running,
  1260. id: 'thinking-completed',
  1261. status: 'succeeded',
  1262. durationMs: 1_600,
  1263. updatedAtMs: 2_600,
  1264. });
  1265. const live = new TranscriptView(() => [running]).render(60).join('\n');
  1266. const settled = new TranscriptView(() => [completed]).render(60).join('\n');
  1267. expect(live).toContain('Thinking…');
  1268. expect(live).toContain('Inspecting the repository');
  1269. expect(settled).toContain('Thought for 1.6s');
  1270. expect(settled).not.toContain('Inspecting the repository');
  1271. });
  1272. it('keeps live Thinking to a tail preview until the user expands it', () => {
  1273. const running = createTranscriptCell({
  1274. id: 'thinking-running-preview',
  1275. kind: 'thinking',
  1276. status: 'running',
  1277. content: ['one', 'two', 'three', 'four', 'five'].join('\n'),
  1278. createdAtMs: 1,
  1279. });
  1280. const expanded = createTranscriptCell({
  1281. ...running,
  1282. id: 'thinking-running-expanded',
  1283. expanded: true,
  1284. });
  1285. const preview = new TranscriptView(() => [running]).render(60).join('\n');
  1286. expect(preview).not.toContain('one');
  1287. expect(preview).not.toContain('two');
  1288. expect(preview).toContain('2 earlier lines');
  1289. expect(preview).toContain('three');
  1290. expect(preview).toContain('five');
  1291. const fullView = new TranscriptView(() => [expanded]);
  1292. fullView.toggleDetailMode();
  1293. const full = fullView.render(60).join('\n');
  1294. expect(full).toContain('one');
  1295. expect(full).toContain('five');
  1296. });
  1297. it('uses tool-specific bounded previews for live output and errors', () => {
  1298. const detail = Array.from({ length: 14 }, (_, index) => `output-${index + 1}`).join('\n');
  1299. const running = createTranscriptCell({
  1300. id: 'tool-bash-running-preview',
  1301. kind: 'tool',
  1302. status: 'running',
  1303. title: 'bash',
  1304. content: 'pnpm test',
  1305. detail,
  1306. createdAtMs: 1,
  1307. });
  1308. const failed = createTranscriptCell({
  1309. ...running,
  1310. id: 'tool-bash-failed-preview',
  1311. status: 'failed',
  1312. });
  1313. const live = new TranscriptView(() => [running], {
  1314. displayModes: previewDisplayModes,
  1315. })
  1316. .render(80)
  1317. .join('\n');
  1318. expect(live).not.toContain('output-1\n');
  1319. expect(live).toContain('11 earlier lines');
  1320. expect(live).toContain('output-14');
  1321. const error = new TranscriptView(() => [failed], {
  1322. displayModes: previewDisplayModes,
  1323. })
  1324. .render(80)
  1325. .join('\n');
  1326. expect(error).toContain('output-14');
  1327. expect(error).not.toContain('output-1\n');
  1328. });
  1329. it('bounds previews by rendered rows when one physical line wraps', () => {
  1330. const view = new TranscriptView(
  1331. () => [
  1332. createTranscriptCell({
  1333. id: 'tool-read-wrapped-preview',
  1334. kind: 'tool',
  1335. status: 'running',
  1336. title: 'read',
  1337. content: 'README.md',
  1338. detail: Array.from({ length: 80 }, (_, index) => `word-${index + 1}`).join(' '),
  1339. createdAtMs: 1,
  1340. }),
  1341. ],
  1342. { displayModes: previewDisplayModes },
  1343. );
  1344. const detailLines = view.render(30).slice(1);
  1345. expect(detailLines.length).toBeLessThanOrEqual(4);
  1346. expect(detailLines.join('\n')).toContain('earlier lines');
  1347. expect(detailLines.join('\n')).toContain('word-80');
  1348. });
  1349. it('hides a running tool result until the user requests details', () => {
  1350. const running = createTranscriptCell({
  1351. id: 'tool-running',
  1352. kind: 'tool',
  1353. status: 'running',
  1354. title: 'bash',
  1355. content: 'pnpm test',
  1356. detail: 'streaming output',
  1357. createdAtMs: 1,
  1358. });
  1359. const view = new TranscriptView(() => [running]);
  1360. expect(view.render(60).join('\n')).not.toContain('streaming output');
  1361. view.toggleDetailMode();
  1362. expect(view.render(60).join('\n')).toContain('streaming output');
  1363. });
  1364. it('keeps completed Thinking collapsed while live Thinking remains visible', () => {
  1365. const completed = createTranscriptCell({
  1366. id: 'thinking-completed',
  1367. kind: 'thinking',
  1368. status: 'succeeded',
  1369. content: ['First decision', 'Second decision', 'Third decision', 'Final detail'].join('\n'),
  1370. createdAtMs: 1,
  1371. });
  1372. const running = createTranscriptCell({
  1373. id: 'thinking-running',
  1374. kind: 'thinking',
  1375. status: 'running',
  1376. content: 'Inspecting the code',
  1377. createdAtMs: 2,
  1378. });
  1379. const completedView = new TranscriptView(() => [completed]);
  1380. const compact = completedView.render(40).join('\n');
  1381. expect(compact).toContain('Thought');
  1382. expect(compact).not.toContain('First decision');
  1383. expect(compact).not.toContain('Final detail');
  1384. expect(new TranscriptView(() => [running]).render(40).join('\n')).toContain(
  1385. 'Inspecting the code',
  1386. );
  1387. completedView.toggleDetailMode();
  1388. expect(completedView.render(40).join('\n')).toContain('First decision');
  1389. expect(completedView.render(40).join('\n')).toContain('Final detail');
  1390. });
  1391. it('extracts the useful target from structured tool arguments', () => {
  1392. const view = new TranscriptView(() => [
  1393. createTranscriptCell({
  1394. id: 'tool-structured',
  1395. kind: 'tool',
  1396. status: 'running',
  1397. title: 'read',
  1398. content: JSON.stringify({ path: 'src/cart/price.ts', offset: 120 }),
  1399. createdAtMs: 1,
  1400. }),
  1401. ]);
  1402. const rendered = view.render(50).join('\n');
  1403. expect(rendered).toContain('Reading (src/cart/price.ts)');
  1404. expect(rendered).not.toContain('{"path"');
  1405. });
  1406. it('collapses grouped read targets in compact mode and reveals them in detailed mode', () => {
  1407. const view = new TranscriptView(() => [
  1408. createTranscriptCell({
  1409. id: 'thinking-1',
  1410. kind: 'thinking',
  1411. status: 'running',
  1412. content: 'Inspecting the implementation',
  1413. createdAtMs: 1,
  1414. turnId: 'turn-1',
  1415. }),
  1416. createTranscriptCell({
  1417. id: 'read-1',
  1418. kind: 'tool',
  1419. status: 'succeeded',
  1420. title: 'read',
  1421. content: JSON.stringify({ path: 'src/app.ts' }),
  1422. createdAtMs: 2,
  1423. turnId: 'turn-1',
  1424. }),
  1425. createTranscriptCell({
  1426. id: 'read-2',
  1427. kind: 'tool',
  1428. status: 'running',
  1429. title: 'read',
  1430. content: JSON.stringify({ path: 'src/runtime.ts' }),
  1431. createdAtMs: 3,
  1432. turnId: 'turn-1',
  1433. }),
  1434. createTranscriptCell({
  1435. id: 'read-3',
  1436. kind: 'tool',
  1437. status: 'failed',
  1438. title: 'read',
  1439. content: JSON.stringify({ path: 'src/missing.ts' }),
  1440. createdAtMs: 4,
  1441. turnId: 'turn-1',
  1442. }),
  1443. createTranscriptCell({
  1444. id: 'read-4',
  1445. kind: 'tool',
  1446. status: 'succeeded',
  1447. title: 'read',
  1448. content: JSON.stringify({ path: 'src/app.ts' }),
  1449. createdAtMs: 5,
  1450. turnId: 'turn-1',
  1451. }),
  1452. createTranscriptCell({
  1453. id: 'bash-1',
  1454. kind: 'tool',
  1455. status: 'running',
  1456. title: 'bash',
  1457. content: JSON.stringify({ command: 'pnpm test' }),
  1458. createdAtMs: 6,
  1459. turnId: 'turn-1',
  1460. }),
  1461. ]);
  1462. const compact = view.render(64).join('\n');
  1463. expect(compact).toContain('Reading 3 files');
  1464. expect(compact).toContain('1 failed');
  1465. expect(compact).not.toContain('src/app.ts');
  1466. expect(compact).not.toContain('src/runtime.ts');
  1467. expect(compact).not.toContain('src/missing.ts');
  1468. expect(compact).toContain('└ • Running');
  1469. expect(compact).toContain('pnpm test');
  1470. view.toggleDetailMode();
  1471. const detailed = view.render(64).join('\n');
  1472. expect(detailed).toContain('src/app.ts');
  1473. expect(detailed).toContain('src/runtime.ts');
  1474. expect(detailed).toContain('reading');
  1475. expect(detailed).toContain('src/missing.ts');
  1476. expect(detailed).toContain('failed');
  1477. expect(detailed).toContain('$ pnpm test');
  1478. expect(detailed.match(/src\/app\.ts/gu)).toHaveLength(2);
  1479. expect(compact).toContain('4 calls');
  1480. expect(detailed.match(/\bRead {2}src\//gu)).toBeNull();
  1481. });
  1482. it('keeps read groups turn-scoped and preserves expanded details', () => {
  1483. const view = new TranscriptView(() => [
  1484. createTranscriptCell({
  1485. id: 'read-1',
  1486. kind: 'tool',
  1487. status: 'succeeded',
  1488. title: 'read',
  1489. content: 'src/a.ts',
  1490. detail: 'const a = 1;',
  1491. expanded: true,
  1492. createdAtMs: 1,
  1493. turnId: 'turn-1',
  1494. }),
  1495. createTranscriptCell({
  1496. id: 'read-2',
  1497. kind: 'tool',
  1498. status: 'succeeded',
  1499. title: 'read',
  1500. content: 'src/b.ts',
  1501. createdAtMs: 2,
  1502. turnId: 'turn-1',
  1503. }),
  1504. createTranscriptCell({
  1505. id: 'read-3',
  1506. kind: 'tool',
  1507. status: 'succeeded',
  1508. title: 'read',
  1509. content: 'src/c.ts',
  1510. createdAtMs: 3,
  1511. turnId: 'turn-2',
  1512. }),
  1513. ]);
  1514. const compact = view.render(50).join('\n');
  1515. expect(compact).toContain('Read 2 files');
  1516. expect(compact).not.toContain('const a = 1;');
  1517. expect(compact).toContain('Read (src/c.ts)');
  1518. view.toggleDetailMode();
  1519. expect(view.render(50).join('\n')).toContain('const a = 1;');
  1520. });
  1521. it('labels a single image read as Read Image', () => {
  1522. const view = new TranscriptView(() => [
  1523. createTranscriptCell({
  1524. id: 'read-image',
  1525. kind: 'tool',
  1526. status: 'succeeded',
  1527. title: 'read',
  1528. content: JSON.stringify({ path: 'figures/reference_overview.png' }),
  1529. detail: 'Read image file [image/png]',
  1530. createdAtMs: 1,
  1531. }),
  1532. ]);
  1533. const rendered = stripVTControlCharacters(view.render(80).join('\n'));
  1534. expect(rendered).toContain('Read Image (figures/reference_overview.png)');
  1535. });
  1536. it('collapses grouped search and path targets while preserving the concrete action', () => {
  1537. const view = new TranscriptView(() => [
  1538. createTranscriptCell({
  1539. id: 'search-1',
  1540. kind: 'tool',
  1541. status: 'succeeded',
  1542. title: 'grep',
  1543. content: JSON.stringify({ query: 'watchStream' }),
  1544. createdAtMs: 1,
  1545. turnId: 'turn-1',
  1546. }),
  1547. createTranscriptCell({
  1548. id: 'list-1',
  1549. kind: 'tool',
  1550. status: 'succeeded',
  1551. title: 'glob',
  1552. content: JSON.stringify({ pattern: '**/*.ts' }),
  1553. createdAtMs: 2,
  1554. turnId: 'turn-1',
  1555. }),
  1556. ]);
  1557. const compact = view.render(64).join('\n');
  1558. expect(compact).toContain('Explored 2 operations');
  1559. expect(compact).not.toContain('watchStream');
  1560. expect(compact).not.toContain('**/*.ts');
  1561. expect(compact).not.toContain('searching');
  1562. expect(compact).not.toContain('listing');
  1563. view.toggleDetailMode();
  1564. const detailed = view.render(64).join('\n');
  1565. expect(detailed).toContain('watchStream');
  1566. expect(detailed).toContain('**/*.ts');
  1567. });
  1568. it('keeps unanswered prompts in the picker and shows compact answer progress and receipts', () => {
  1569. const view = new TranscriptView(() => [
  1570. createTranscriptCell({
  1571. id: 'permission-1',
  1572. kind: 'permission',
  1573. status: 'blocked',
  1574. title: 'bash',
  1575. content: 'Run the unit tests',
  1576. createdAtMs: 1,
  1577. }),
  1578. createTranscriptCell({
  1579. id: 'permission-2',
  1580. kind: 'permission',
  1581. status: 'resolved',
  1582. title: 'write_file',
  1583. content: 'Update the changelog',
  1584. detail: 'Allowed for this conversation · write_file',
  1585. createdAtMs: 2,
  1586. }),
  1587. createTranscriptCell({
  1588. id: 'question-1',
  1589. kind: 'question',
  1590. status: 'blocked',
  1591. title: 'Input required',
  1592. content: '',
  1593. createdAtMs: 3,
  1594. }),
  1595. createTranscriptCell({
  1596. id: 'question-2',
  1597. kind: 'question',
  1598. status: 'blocked',
  1599. title: 'Release setup',
  1600. content: 'Release channel Stable',
  1601. detail: '1 of 2 answered',
  1602. createdAtMs: 4,
  1603. }),
  1604. createTranscriptCell({
  1605. id: 'question-3',
  1606. kind: 'question',
  1607. status: 'pending',
  1608. title: 'Answers sent',
  1609. content: 'Release channel Stable\nCollaboration Mixed mode',
  1610. detail: 'Sending your answer…',
  1611. createdAtMs: 5,
  1612. }),
  1613. createTranscriptCell({
  1614. id: 'question-4',
  1615. kind: 'question',
  1616. status: 'resolved',
  1617. title: 'Answers sent',
  1618. content: 'Release channel Stable\nCollaboration Mixed mode',
  1619. detail: 'MCode is continuing…',
  1620. createdAtMs: 6,
  1621. }),
  1622. ]);
  1623. const rendered = view.render(60).join('\n');
  1624. expect(rendered).not.toContain('Approval needed');
  1625. expect(rendered).not.toContain('Run the unit tests');
  1626. expect(rendered).toContain('✓ Allowed for this conversation · write_file');
  1627. expect(rendered).not.toContain('Update the changelog');
  1628. expect(rendered).not.toContain('decision panel');
  1629. expect(rendered).toContain('◆ Answers so far');
  1630. expect(rendered).toContain('1 of 2 answered');
  1631. expect(rendered).toContain('● Sending answers…');
  1632. expect(rendered).toContain('✓ Answers sent');
  1633. expect(rendered).toContain('Release channel Stable');
  1634. expect(rendered).toContain('Collaboration Mixed mode');
  1635. expect(rendered).toContain('MCode is continuing');
  1636. expect(rendered).not.toContain('/allow');
  1637. expect(rendered).not.toContain('/q1');
  1638. });
  1639. it('uses a denied lifecycle marker for resolved permission receipts', () => {
  1640. const view = new TranscriptView(() => [
  1641. createTranscriptCell({
  1642. id: 'permission-denied',
  1643. kind: 'permission',
  1644. status: 'resolved',
  1645. title: 'bash',
  1646. content: 'Run a command',
  1647. detail: 'Denied · bash',
  1648. createdAtMs: 1,
  1649. }),
  1650. ]);
  1651. expect(view.render(48).join('\n')).toContain('× Denied · bash');
  1652. });
  1653. it('uses readable action labels and never exposes generic JSON as a tool summary', () => {
  1654. const view = new TranscriptView(() => [
  1655. createTranscriptCell({
  1656. id: 'memory-1',
  1657. kind: 'tool',
  1658. status: 'succeeded',
  1659. title: 'Memory',
  1660. content: JSON.stringify({
  1661. target: 'user',
  1662. operation: 'append',
  1663. content: 'large private implementation payload',
  1664. }),
  1665. createdAtMs: 1,
  1666. }),
  1667. createTranscriptCell({
  1668. id: 'write-1',
  1669. kind: 'tool',
  1670. status: 'succeeded',
  1671. title: 'write_file',
  1672. content: JSON.stringify({ file_path: 'src/app.ts', contents: '...' }),
  1673. createdAtMs: 2,
  1674. }),
  1675. createTranscriptCell({
  1676. id: 'task-1',
  1677. kind: 'tool',
  1678. status: 'running',
  1679. title: 'task',
  1680. content: JSON.stringify({
  1681. agent_name: 'verifier',
  1682. description: 'Review recent CLI changes',
  1683. }),
  1684. createdAtMs: 3,
  1685. }),
  1686. ]);
  1687. const rendered = view.render(80).join('\n');
  1688. expect(rendered).toContain('Updated memory (user · append)');
  1689. expect(rendered).toContain('Wrote (src/app.ts)');
  1690. expect(rendered).not.toContain('large private implementation payload');
  1691. expect(rendered).not.toContain('{"target"');
  1692. expect(rendered).not.toContain('Write_file');
  1693. expect(rendered).toContain('Delegating (verifier · Review recent CLI changes)');
  1694. });
  1695. it('uses blue for Ask User, gold for permission confirmation, and green after resolution', () => {
  1696. expect(resolveTuiDecisionColor('question', false)).toBe(tuiColors.signal);
  1697. expect(resolveTuiDecisionColor('permission', false)).toBe(tuiColors.warning);
  1698. expect(resolveTuiDecisionColor('question', true)).toBe(tuiColors.success);
  1699. });
  1700. it('keeps every rail row inside a narrow terminal', () => {
  1701. const view = new TranscriptView(() => [
  1702. createTranscriptCell({
  1703. id: 'user-narrow',
  1704. kind: 'user',
  1705. status: 'succeeded',
  1706. content: 'Inspect this deliberately long request before changing anything.',
  1707. createdAtMs: 1,
  1708. }),
  1709. createTranscriptCell({
  1710. id: 'tool-narrow',
  1711. kind: 'tool',
  1712. status: 'running',
  1713. title: 'read',
  1714. content: 'src/a-deliberately-long-file-name.ts',
  1715. detail: 'Waiting for a deliberately long result from the runtime.',
  1716. createdAtMs: 2,
  1717. }),
  1718. ]);
  1719. const lines = view.render(28);
  1720. expect(lines.every((line) => visibleWidth(line) <= 28)).toBe(true);
  1721. expect(lines.some((line) => line.includes('Read') && line.includes('…'))).toBe(true);
  1722. });
  1723. it('keeps a stable turn-window anchor as new cells append', () => {
  1724. let cells = Array.from({ length: 80 }, (_, index) =>
  1725. createTranscriptCell({
  1726. id: `cell-${index + 1}`,
  1727. turnId: `turn-${Math.floor(index / 2) + 1}`,
  1728. kind: 'final-summary',
  1729. status: 'succeeded',
  1730. content: `message ${index + 1}`,
  1731. createdAtMs: index + 1,
  1732. }),
  1733. );
  1734. const view = new TranscriptView(() => cells, { maxInitialTurns: 10 });
  1735. const first = view.render(80).join('\n');
  1736. cells = [
  1737. ...cells,
  1738. createTranscriptCell({
  1739. id: 'cell-81',
  1740. turnId: 'turn-41',
  1741. kind: 'final-summary',
  1742. status: 'succeeded',
  1743. content: 'message 81',
  1744. createdAtMs: 81,
  1745. }),
  1746. ];
  1747. const appended = view.render(80).join('\n');
  1748. expect(first).toContain('30 earlier turns omitted');
  1749. expect(first).toContain('message 61');
  1750. expect(first).not.toContain('message 60');
  1751. expect(appended).toContain('message 61');
  1752. expect(appended).toContain('message 81');
  1753. });
  1754. it('folds the oldest cells inside one oversized turn while preserving its request and tail', () => {
  1755. const cells = [
  1756. createTranscriptCell({
  1757. id: 'user-1',
  1758. turnId: 'turn-1',
  1759. kind: 'user',
  1760. status: 'succeeded',
  1761. content: 'Inspect the repository',
  1762. createdAtMs: 1,
  1763. }),
  1764. ...Array.from({ length: 200 }, (_, index) =>
  1765. createTranscriptCell({
  1766. id: `tool-${index + 1}`,
  1767. turnId: 'turn-1',
  1768. kind: 'tool',
  1769. status: 'succeeded',
  1770. title: 'read',
  1771. content: `src/file-${index + 1}.ts`,
  1772. createdAtMs: index + 2,
  1773. }),
  1774. ),
  1775. createTranscriptCell({
  1776. id: 'assistant-final',
  1777. turnId: 'turn-1',
  1778. kind: 'assistant',
  1779. status: 'succeeded',
  1780. content: 'Final conclusion',
  1781. createdAtMs: 202,
  1782. }),
  1783. ];
  1784. const view = new TranscriptView(() => cells, { maxCellsPerTurn: 40 });
  1785. const rendered = view.render(80).join('\n');
  1786. expect(rendered).toContain('Inspect the repository');
  1787. expect(rendered).toContain('earlier steps folded');
  1788. expect(rendered).toContain('Final conclusion');
  1789. expect(view.getPerformanceSnapshot()).toMatchObject({
  1790. sourceUnits: 202,
  1791. projectedCells: 41,
  1792. foldedCells: 162,
  1793. });
  1794. });
  1795. it('renders Todo progress as a compact read-only checklist', () => {
  1796. const view = new TranscriptView(() => [
  1797. createTranscriptCell({
  1798. id: 'todo-turn-1',
  1799. kind: 'todo',
  1800. status: 'running',
  1801. content: '',
  1802. todoItems: [
  1803. { content: 'Inspect competitors', status: 'completed' },
  1804. { content: 'Add Todo display', status: 'in_progress' },
  1805. { content: 'Run focused tests', status: 'pending' },
  1806. { content: 'Discard obsolete draft', status: 'cancelled' },
  1807. ],
  1808. createdAtMs: 1,
  1809. }),
  1810. ]);
  1811. const rendered = stripVTControlCharacters(view.render(48).join('\n'));
  1812. expect(rendered).toContain('Todo list 1/4 · 1 cancelled');
  1813. expect(rendered).toContain('✓ Inspect competitors');
  1814. expect(rendered).toContain('● Add Todo display');
  1815. expect(rendered).toContain('○ Run focused tests');
  1816. expect(rendered).toContain('× Discard obsolete draft');
  1817. expect(rendered).not.toContain('todowrite');
  1818. });
  1819. it('wraps long Todo items within a narrow terminal', () => {
  1820. const view = new TranscriptView(() => [
  1821. createTranscriptCell({
  1822. id: 'todo-turn-1',
  1823. kind: 'todo',
  1824. status: 'running',
  1825. content: '',
  1826. todoItems: [
  1827. {
  1828. content: 'Implement a deliberately long task description that must wrap',
  1829. status: 'in_progress',
  1830. },
  1831. ],
  1832. createdAtMs: 1,
  1833. }),
  1834. ]);
  1835. const lines = view.render(24);
  1836. expect(stripVTControlCharacters(lines.join('\n'))).toContain('Todo list 0/1');
  1837. expect(lines.length).toBeGreaterThan(2);
  1838. expect(lines.every((line) => visibleWidth(line) <= 24)).toBe(true);
  1839. });
  1840. });