@zergai/ztc 0.25.0-beta.1 → 0.26.0-beta.1

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 (38) hide show
  1. package/README.md +70 -0
  2. package/bin/zmux-darwin-arm64 +0 -0
  3. package/bin/ztc-audio-darwin-arm64 +0 -0
  4. package/bin/ztc-capture-darwin-arm64 +0 -0
  5. package/bin/ztc-tui-darwin-arm64 +0 -0
  6. package/bin/zterm-darwin-arm64 +2 -2
  7. package/dist/app/emergency_exit.d.ts +14 -0
  8. package/dist/app/emergency_exit.js +36 -0
  9. package/dist/app/layout_change.d.ts +20 -0
  10. package/dist/app/layout_change.js +14 -0
  11. package/dist/app/workspace_topology.d.ts +8 -0
  12. package/dist/app/workspace_topology.js +8 -0
  13. package/dist/cli_main.js +2 -24
  14. package/dist/cli_ratatui.d.ts +70 -1
  15. package/dist/cli_ratatui.js +259 -53
  16. package/dist/desktop/daily/status_restore.js +6 -2
  17. package/dist/mcp/workspace_server.js +9 -2
  18. package/dist/ui/core/input_segments.d.ts +15 -0
  19. package/dist/ui/core/input_segments.js +22 -0
  20. package/dist/ui/core/layout_yoga.d.ts +7 -0
  21. package/dist/ui/core/layout_yoga.js +16 -0
  22. package/dist/ui/ratatui/owner_ping.d.ts +24 -0
  23. package/dist/ui/ratatui/owner_ping.js +18 -0
  24. package/dist/ui/ratatui/process.d.ts +2 -2
  25. package/dist/ui/ratatui/protocol.d.ts +80 -3
  26. package/dist/ui/ratatui/protocol.js +58 -0
  27. package/dist/ui/scrollback.d.ts +24 -0
  28. package/dist/ui/scrollback.js +53 -18
  29. package/dist/ui/terminal_image.js +17 -1
  30. package/dist/ui/transcript_bound.d.ts +41 -0
  31. package/dist/ui/transcript_bound.js +100 -0
  32. package/dist/ui/transcript_render_cache.d.ts +15 -4
  33. package/dist/ui/transcript_render_cache.js +39 -16
  34. package/dist/ui/views/app.js +3 -2
  35. package/dist/ui/views/input_area.d.ts +1 -0
  36. package/dist/ui/views/input_area.js +34 -8
  37. package/dist/zlm-embed.mjs +1 -1
  38. package/package.json +17 -9
@@ -0,0 +1,100 @@
1
+ /**
2
+ * What ZTC ships its renderer of Main's transcript (robustness spec VR2, RZ1, RX1).
3
+ *
4
+ * ZTerm and zmux keep the last 10,000 rows of the transcript (ZTerm's SCROLLBACK_LINES, zmux's MAX_SCROLLBACK) and
5
+ * drop any command line over 64 MiB with `too_long`. Shipping the whole transcript in every rebuild took 24 MB for a
6
+ * day-long session with its tool outputs expanded, and past the cap every rebuild was refused while Main stopped
7
+ * updating. So ZTC ships only the rows the renderer keeps, with `head_line_offset` saying how many rows of the first
8
+ * one's message it left out, as the renderer would have trimmed them itself; and at most TRANSCRIPT_LINE_BUDGET bytes
9
+ * of them, a quarter of the renderers' cap. Rows the budget leaves out that the renderer would have kept are never
10
+ * left out silently: the first row shipped says how many.
11
+ */
12
+ import { TERMINAL_THEME } from './views/theme.js';
13
+ /** The rows of Main's transcript ZTerm and zmux keep: ZTerm's SCROLLBACK_LINES and zmux's MAX_SCROLLBACK. */
14
+ export const RENDERER_TRANSCRIPT_ROWS = 10_000;
15
+ /**
16
+ * The most bytes ZTC ships of its transcript in one command line: a quarter of ZTerm's and zmux's 64 MiB line cap.
17
+ * The last 10,000 rows of a measured day-long session serialize to at most 5.1 MB; the rows that pass the budget are
18
+ * rows of images, 3.4 KB each at most.
19
+ */
20
+ export const TRANSCRIPT_LINE_BUDGET = 16 * 1024 * 1024;
21
+ /** The smallest budget a renderer's too_long refusals halve ZTC's to. */
22
+ export const MIN_TRANSCRIPT_LINE_BUDGET = 64 * 1024;
23
+ /** What the command line holds besides the rows, the notice among them, with room to spare. */
24
+ const ENVELOPE_ROOM = 1024;
25
+ /** `bytes` as people read sizes: MiB, KiB or bytes. */
26
+ export function sizeName(bytes) {
27
+ if (bytes >= 1024 * 1024)
28
+ return `${Math.round((bytes / (1024 * 1024)) * 10) / 10} MiB`;
29
+ if (bytes >= 1024)
30
+ return `${Math.round(bytes / 1024)} KiB`;
31
+ return `${bytes} bytes`;
32
+ }
33
+ /** A row's bytes in the command line, its comma included. */
34
+ function rowBytes(row) {
35
+ return Buffer.byteLength(JSON.stringify(row), 'utf8') + 1;
36
+ }
37
+ /**
38
+ * At least a row's bytes in the command line, without writing it: JSON writes no UTF-16 unit as more than 6 bytes,
39
+ * and a row's other fields (a colour, a dim flag, a column) fit in 256. A badge's texts are written out.
40
+ */
41
+ function rowBytesAtMost(row) {
42
+ if (row.style?.badge)
43
+ return rowBytes(row);
44
+ return 6 * (row.text.length + (row.message_id?.length ?? 0) + (row.style?.color?.length ?? 0)) + 256;
45
+ }
46
+ /** How many rows of `lines[start]`'s message come before it. A message's rows are consecutive. */
47
+ function rowsOfMessageBefore(lines, start) {
48
+ const id = lines[start]?.message_id;
49
+ if (id === undefined)
50
+ return 0;
51
+ let count = 0;
52
+ for (let index = start - 1; index >= 0 && lines[index].message_id === id; index--)
53
+ count++;
54
+ return count;
55
+ }
56
+ /** The row that takes the place of the rows the budget left out. */
57
+ function budgetNotice(rows, budget, messageId) {
58
+ return {
59
+ text: `··· ${rows.toLocaleString('en-US')} earlier rows aren't shown here: ZTC sends this window at most the newest ${sizeName(budget)} of the transcript`,
60
+ style: { color: TERMINAL_THEME.muted, dimColor: true },
61
+ ...(messageId !== undefined ? { message_id: messageId } : {}),
62
+ };
63
+ }
64
+ /**
65
+ * The part of `lines` to ship a renderer that keeps `rows` rows, in a command line of at most `bytes` bytes: the newest
66
+ * `rows` rows, less the oldest of them while the line would pass `bytes`, those then told of by a first row of their
67
+ * own. That row takes the place of the last row left out of its message, so the message's rows keep their count.
68
+ */
69
+ export function boundTranscript(lines, limits) {
70
+ let start = Math.max(0, lines.length - limits.rows);
71
+ const room = limits.bytes - ENVELOPE_ROOM;
72
+ let overBudget = 0;
73
+ let atMost = 0;
74
+ for (let index = start; index < lines.length && atMost <= room; index++)
75
+ atMost += rowBytesAtMost(lines[index]);
76
+ if (atMost > room) {
77
+ // The newest rows that fit.
78
+ let used = 0;
79
+ let cut = start;
80
+ for (let index = lines.length - 1; index >= start; index--) {
81
+ const bytes = rowBytes(lines[index]);
82
+ if (used + bytes > room) {
83
+ cut = index + 1;
84
+ break;
85
+ }
86
+ used += bytes;
87
+ }
88
+ overBudget = cut - start;
89
+ start = cut;
90
+ }
91
+ let headLineOffset = rowsOfMessageBefore(lines, start);
92
+ const kept = lines.slice(start);
93
+ if (overBudget === 0)
94
+ return { lines: kept, headLineOffset, overBudget };
95
+ // The notice is a row of the first message's when rows of it were left out, in place of the last of them.
96
+ const id = headLineOffset > 0 ? lines[start].message_id : undefined;
97
+ if (headLineOffset > 0)
98
+ headLineOffset -= 1;
99
+ return { lines: [budgetNotice(overBudget, limits.bytes, id), ...kept], headLineOffset, overBudget };
100
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Per-message transcript render cache.
3
3
  *
4
- * A transcript rebuild renders every message in the session (markdown, tables,
4
+ * A transcript rebuild renders the session's messages (markdown, tables,
5
5
  * syntax highlighting, word wrap). Sessions routinely run to thousands of
6
6
  * messages and multiple megabytes, and a rebuild is scheduled whenever any
7
7
  * message mutates — a streaming delta, a tool result, an expand toggle — plus
@@ -18,6 +18,13 @@
18
18
  *
19
19
  * Entries are bounded by a total rendered-line budget; the oldest entries are
20
20
  * dropped first. A dropped message simply re-renders on its next rebuild.
21
+ *
22
+ * A rebuild renders from the newest message back only as far as the rows the
23
+ * renderer keeps (10,000), fewer than the budget, so at most one message it
24
+ * renders can be larger than the whole budget: the oldest, a pasted list or
25
+ * a run of tool calls of 150,000 rows. That one entry is kept beside the
26
+ * budget, not in it, until another such message takes its place, so a
27
+ * rebuild while an answer streams below it renders the answer, not the list.
21
28
  */
22
29
  import type { Message } from '../types.js';
23
30
  import type { ScrollbackLine } from './scrollback.js';
@@ -38,14 +45,18 @@ export declare class TranscriptRenderCache {
38
45
  private readonly renderMessage;
39
46
  private readonly maxLines;
40
47
  private readonly entries;
48
+ /** The lines of the entries within the budget: all but the oversized one. */
41
49
  private cachedLines;
50
+ /** The one entry larger than the whole budget, kept beside it, if any. */
51
+ private oversized;
42
52
  constructor(renderMessage: (message: Message) => ScrollbackLine[], maxLines?: number);
43
53
  /** Rendered lines for `message` under `configKey`, re-rendering only when
44
54
  * one of its rendering inputs changed since the last render. */
45
55
  render(message: Message, configKey: string): CachedTranscriptRender;
56
+ /** Drops `id`'s entry, from the budget or from beside it. */
57
+ private forget;
46
58
  private matches;
47
- /** Drop oldest entries until the line budget can absorb `adding` lines. A
48
- * single render larger than the whole budget still lands (bounded by that
49
- * message) and is evicted by the next insert. */
59
+ /** Drop the oldest entries within the budget until it can absorb `adding`
60
+ * lines. The oversized entry beside it stays. */
50
61
  private evictFor;
51
62
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Per-message transcript render cache.
3
3
  *
4
- * A transcript rebuild renders every message in the session (markdown, tables,
4
+ * A transcript rebuild renders the session's messages (markdown, tables,
5
5
  * syntax highlighting, word wrap). Sessions routinely run to thousands of
6
6
  * messages and multiple megabytes, and a rebuild is scheduled whenever any
7
7
  * message mutates — a streaming delta, a tool result, an expand toggle — plus
@@ -18,6 +18,13 @@
18
18
  *
19
19
  * Entries are bounded by a total rendered-line budget; the oldest entries are
20
20
  * dropped first. A dropped message simply re-renders on its next rebuild.
21
+ *
22
+ * A rebuild renders from the newest message back only as far as the rows the
23
+ * renderer keeps (10,000), fewer than the budget, so at most one message it
24
+ * renders can be larger than the whole budget: the oldest, a pasted list or
25
+ * a run of tool calls of 150,000 rows. That one entry is kept beside the
26
+ * budget, not in it, until another such message takes its place, so a
27
+ * rebuild while an answer streams below it renders the answer, not the list.
21
28
  */
22
29
  export const TRANSCRIPT_RENDER_CACHE_MAX_LINES = 40_000;
23
30
  const FNV_OFFSET = 2166136261;
@@ -53,7 +60,10 @@ export class TranscriptRenderCache {
53
60
  renderMessage;
54
61
  maxLines;
55
62
  entries = new Map();
63
+ /** The lines of the entries within the budget: all but the oversized one. */
56
64
  cachedLines = 0;
65
+ /** The one entry larger than the whole budget, kept beside it, if any. */
66
+ oversized = null;
57
67
  constructor(renderMessage, maxLines = TRANSCRIPT_RENDER_CACHE_MAX_LINES) {
58
68
  this.renderMessage = renderMessage;
59
69
  this.maxLines = maxLines;
@@ -82,14 +92,31 @@ export class TranscriptRenderCache {
82
92
  toolOutput: toolOutputIdentity(message),
83
93
  attachments: message.attachments,
84
94
  };
85
- const replaced = this.entries.get(message.id);
86
- if (replaced !== undefined)
87
- this.cachedLines -= replaced.lines.length;
88
- this.evictFor(lines.length);
95
+ this.forget(message.id);
96
+ if (this.maxLines > 0 && lines.length > this.maxLines) {
97
+ // Larger than the whole budget: kept beside it, in place of any other such entry.
98
+ if (this.oversized !== null)
99
+ this.forget(this.oversized);
100
+ this.oversized = message.id;
101
+ }
102
+ else {
103
+ this.evictFor(lines.length);
104
+ this.cachedLines += lines.length;
105
+ }
89
106
  this.entries.set(message.id, next);
90
- this.cachedLines += lines.length;
91
107
  return next;
92
108
  }
109
+ /** Drops `id`'s entry, from the budget or from beside it. */
110
+ forget(id) {
111
+ const entry = this.entries.get(id);
112
+ if (entry === undefined)
113
+ return;
114
+ this.entries.delete(id);
115
+ if (this.oversized === id)
116
+ this.oversized = null;
117
+ else
118
+ this.cachedLines -= entry.lines.length;
119
+ }
93
120
  matches(entry, message, configKey) {
94
121
  if (entry.configKey !== configKey)
95
122
  return false;
@@ -120,20 +147,16 @@ export class TranscriptRenderCache {
120
147
  }
121
148
  return true;
122
149
  }
123
- /** Drop oldest entries until the line budget can absorb `adding` lines. A
124
- * single render larger than the whole budget still lands (bounded by that
125
- * message) and is evicted by the next insert. */
150
+ /** Drop the oldest entries within the budget until it can absorb `adding`
151
+ * lines. The oversized entry beside it stays. */
126
152
  evictFor(adding) {
127
153
  if (this.maxLines <= 0)
128
154
  return;
129
- while (this.cachedLines + adding > this.maxLines && this.entries.size > 0) {
130
- const oldest = this.entries.keys().next();
131
- if (oldest.done)
155
+ for (const id of this.entries.keys()) {
156
+ if (this.cachedLines + adding <= this.maxLines)
132
157
  return;
133
- const entry = this.entries.get(oldest.value);
134
- this.entries.delete(oldest.value);
135
- if (entry !== undefined)
136
- this.cachedLines -= entry.lines.length;
158
+ if (id !== this.oversized)
159
+ this.forget(id);
137
160
  }
138
161
  }
139
162
  }
@@ -1,4 +1,5 @@
1
1
  import { normalizeInputMode } from '../../types.js';
2
+ import { countLines } from '../core/input_segments.js';
2
3
  import { box, text } from '../core/index.js';
3
4
  import { buildHeaderView } from './header.js';
4
5
  import { buildMessageListView } from './message_list.js';
@@ -14,8 +15,8 @@ function estimateBadgePreviewLines(state) {
14
15
  if (!segment || segment.type === 'text')
15
16
  return 0;
16
17
  if (segment.type === 'paste') {
17
- const lines = segment.text.split('\n');
18
- return Math.min(3, lines.length) + (lines.length > 3 ? 1 : 0);
18
+ const lines = countLines(segment.text, 4);
19
+ return Math.min(3, lines) + (lines > 3 ? 1 : 0);
19
20
  }
20
21
  if (segment.type === 'image') {
21
22
  return 16;
@@ -31,6 +31,7 @@ export interface RenderToken {
31
31
  badge?: {
32
32
  type: 'paste' | 'file' | 'image';
33
33
  preview: string;
34
+ /** A file's or an image's path. Empty for a paste, whose text the composer keeps (see badgePreview). */
34
35
  full: string;
35
36
  path?: string;
36
37
  };
@@ -2,14 +2,17 @@ import { box, text } from '../core/index.js';
2
2
  import { commandCompletionView } from '../../app/selection_completion.js';
3
3
  import { buildCompletionMenu } from './completion_menu.js';
4
4
  import { displayWidth } from '../core/text_width.js';
5
- import { getExpandablePasteIndex, getInputShortcutHint } from '../core/input_segments.js';
5
+ import { countLines, getExpandablePasteIndex, getInputShortcutHint } from '../core/input_segments.js';
6
6
  import { PLAN_ACCENT } from './theme.js';
7
7
  const promptPrefix = '❯ ';
8
8
  const promptWidth = promptPrefix.length;
9
+ /** The lines of a paste a badge's preview shows, and the most characters of each. */
10
+ const PASTE_PREVIEW_LINES = 3;
11
+ const PASTE_PREVIEW_LINE_CHARS = 200;
9
12
  function badgeLabel(segment) {
10
13
  switch (segment.type) {
11
14
  case 'paste': {
12
- const lines = segment.text.split('\n').length;
15
+ const lines = countLines(segment.text);
13
16
  return `[pasted ${lines} line${lines === 1 ? '' : 's'}]`;
14
17
  }
15
18
  case 'file':
@@ -20,14 +23,37 @@ function badgeLabel(segment) {
20
23
  return '';
21
24
  }
22
25
  }
26
+ /** `line`, cut to `max` characters with an ellipsis, never inside a surrogate pair. */
27
+ function cutLine(line, max) {
28
+ if (line.length <= max)
29
+ return line;
30
+ let end = max - 1;
31
+ const last = line.charCodeAt(end - 1);
32
+ if (last >= 0xd800 && last <= 0xdbff)
33
+ end -= 1;
34
+ return `${line.slice(0, end)}…`;
35
+ }
36
+ /** A paste's first lines, each cut short, and an ellipsis when more follow, read without splitting the rest of it. */
37
+ function pastePreview(text) {
38
+ const lines = [];
39
+ let start = 0;
40
+ while (lines.length < PASTE_PREVIEW_LINES) {
41
+ const end = text.indexOf('\n', start);
42
+ lines.push(cutLine(text.slice(start, end === -1 ? text.length : end), PASTE_PREVIEW_LINE_CHARS));
43
+ if (end === -1)
44
+ return lines.join('\n');
45
+ start = end + 1;
46
+ }
47
+ return `${lines.join('\n')}\n…`;
48
+ }
23
49
  function badgePreview(segment) {
24
50
  switch (segment.type) {
25
- case 'paste': {
26
- const lines = segment.text.split('\n');
27
- const previewLines = lines.slice(0, 3).join('\n');
28
- const suffix = lines.length > 3 ? '\n…' : '';
29
- return { type: 'paste', preview: previewLines + suffix, full: segment.text };
30
- }
51
+ case 'paste':
52
+ // Every render frame carries its tokens, so a paste's badge carries what the composer shows of it, never the
53
+ // paste: the composer keeps the text, and sends it when the person does. With the text in `full`, a render with a
54
+ // paste of 13 MiB in the composer took 69 MB and was refused (LS2). `full` stays, empty, as the renderers'
55
+ // protocol requires it; none of them reads the badge.
56
+ return { type: 'paste', preview: pastePreview(segment.text), full: '' };
31
57
  case 'image':
32
58
  return { type: 'image', preview: `Image: ${segment.path}`, full: segment.path, path: segment.path };
33
59
  case 'file':
@@ -47404,7 +47404,7 @@ var invokeSkillTool = makeInvokeSkillTool(resolveAmbientSkills);
47404
47404
  // src/zlm.ts
47405
47405
  var ZLM_EMBED_API_VERSION = "zerglang.ztc-embed/1";
47406
47406
  var ZLM_STDIO_PROTOCOL = "zerglang.ztc-host/1";
47407
- var ZLM_ZTC_VERSION = true ? "0.25.0-beta.1" : "development";
47407
+ var ZLM_ZTC_VERSION = true ? "0.26.0-beta.1" : "development";
47408
47408
  var ZLM_MAX_HOST_TOOLS = 64;
47409
47409
  var ZLM_MAX_SYSTEM_PROMPT_BYTES = 128 * 1024;
47410
47410
  var ZLM_HOST_APPROVAL_CAPABILITIES = Object.freeze([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zergai/ztc",
3
- "version": "0.25.0-beta.1",
3
+ "version": "0.26.0-beta.1",
4
4
  "description": "Zerg Terminal Client - CLI agent for continual AI systems",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "homepage": "https://zergai.com/docs/ztc",
@@ -285,18 +285,26 @@
285
285
  "zergRelease": {
286
286
  "branch": "main",
287
287
  "channel": "next",
288
- "sourceCommit": "0ce51c097abf97a57a15730443ac8fc401dc14ec",
288
+ "sourceCommit": "f5a73b0d43721158734afe4beb672388cb70aa14",
289
289
  "sourceVersion": "0.2.0-canary.2",
290
290
  "whatsNew": [
291
- "Images that MCP and other tools return now reach models that take images; others get a short placeholder instead of pages of text.",
292
- "Each request sends only your latest 3 tool images, so a long session's screenshots don't pile up in every request.",
293
- "The desktop app now finds updates on zergai.com, so new releases no longer wait on GitHub Pages; older apps need one update by hand."
291
+ "Arrange the desktop app's tabs and panes by dragging, from a pane's menu or with ⌥⇧⌘ keys; resuming the session brings the layout back.",
292
+ "In the desktop app, ⌘-click a URL in Main or any ZTC pane to open it, even one wrapped over several rows outside code blocks and tables.",
293
+ "If ZTC crashes or hangs, the desktop app keeps your panes running, and a banner can end a hung ZTC without closing them.",
294
+ "Very long sessions and huge messages no longer freeze or crash ZTC, and those sessions resume; a 128K-character paste is now a badge too."
294
295
  ],
295
296
  "changes": [
296
- "A model that takes images now sees the images your tools return.",
297
- "A tool's image no longer reaches the model as text.",
298
- "The desktop app finds new releases without GitHub Pages.",
299
- "The desktop app also takes its downloads from zergai.com."
297
+ "A session holding one huge message resumes, and long sessions keep Main updating.",
298
+ "The desktop app, zmux and ztc-tui keep up when a window or a terminal stops reading, and say what they dropped.",
299
+ "Panes no longer inherit the terminal that started ZTC.",
300
+ "Moving panes in the desktop app never leaves a workspace that won't save.",
301
+ "The desktop app keeps your panes when ZTC crashes, and offers to end a ZTC that hangs.",
302
+ "⌘-click a URL in Main, or any ZTC pane, to open it.",
303
+ "Arrange the desktop app's panes.",
304
+ "Arrange the desktop app's tabs.",
305
+ "ZTC saves the desktop app's arrangement as soon as it changes.",
306
+ "A paste of 128K characters or more is a badge in the composer, even on one line.",
307
+ "zmux's mirror takes 16 viewers and lets an idle one go after two minutes."
300
308
  ]
301
309
  },
302
310
  "publishConfig": {