1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
|
// Building pardes, for contributors: platforms, options, the patches to
// dependencies, the library, tests, the release gates and sending patches,
// then how the pieces fit: the principle, the tree, the core and its
// frontends, the loops, the tape, the checks, the threads, the daemon and its
// sessions, the 9P engine, the listeners and mounts.
#import "style.typ": key, keys, btn, chord, word, tag, addr, file, cmd, doc, pairs
For contributors: how pardes is built, tested and released, and how its
pieces fit. To install it, see #doc("setup", section: "install").
= Platforms
Zig *0.17.1* (`build.zig.zon` pins `minimum_zig_version`). Dependencies are
fetched and pinned by the manifest; the terminal build needs no system
package. The SDL shell builds SDL3 and FreeType from source. Pictures and
PDFs are addons, each its own step: `zig build image`, `zig build pdf`
(`pdf.so`, over MuPDF, its own FreeType and zlib in it), or `zig build
addons` for both; the binaries hold neither. MuPDF, and the FreeType and
zlib the terminal build takes only for it, are lazy: a build fetches them
only with `-Dpdf`, which is on by itself once MuPDF is fetched, so a fresh
checkout's `zig build -Dplatform=tui` downloads none of them, and its
first `zig build pdf -Dpdf` (or `addons`, `static`, `mupdf-check`, a PDF
bench) fetches MuPDF once; without it those steps say to add `-Dpdf`, and
the tests leave their PDF cases out. (Zig 0.17's configure is handed the
options and never the steps asked for, so a step cannot fetch for itself.)
MuPDF is compiled only for the steps and tests that use `pdf.so`.
#pairs(
[`zig build`], [the terminal shell and the SDL window together, *installed into `~/.local/bin`* (`pardes` and `pardes-gui`)],
[`zig build -Dplatform=tui`], [the terminal shell alone],
[`zig build -Dplatform=gui`], [the SDL3 window alone],
[`zig build -Dplatform=macos`], [an AppKit and CoreText app over a static `libpardes.a` (`docs/macos.md`)],
[`zig build static -Dplatform=tui -Dtarget=x86_64-linux-musl`], [one statically linked `pardes` with the image and PDF addons compiled in (`/addons` lists them `builtin`); musl's, as glibc links only dynamically, and never the default],
[`zig build static -Dplatform=gui`], [`pardes-gui` with the two addons compiled in, linked dynamically (SDL and the GPU take the display's libraries)],
)
Off Linux the addons are always compiled in, as in a static build:
`Addon.zig` loads a library through a Linux memfd, and `Addon load`
elsewhere says `not on this platform`. A gui for macOS built on another
system takes the SDK SDL needs with `-Dmacos-sdk=<path>`.
A bare `zig build` installs into the prefix (`zig-out`, or `--prefix
<dir>`) and copies the binaries into `~/.local/bin`; `-Dhome-install=false`
leaves that copy out. Zig 0.17 keeps the prefix from the build script, so
`--prefix` alone no longer stops it. The `image`, `pdf` and `addons` steps
of a bare `zig build` also install into `$XDG_DATA_HOME/pardes/addons`
(else `~/.local/share/pardes/addons`); `-Dhome-install=false` skips that
copy too. With `-Dplatform` neither is copied, but `-Dhome-install=true`
copies the addons. Test, benchmark and run steps build what they need
without installing. `pardes --version` prints `pardes <version>` (from
`build.zig.zon`), plus the commit for release builds and the `~/.local`
install, and on a second line the build's semantics hash, which a replica
must share with its session; `pardes --help` lists every flag.
== Build options
`zig build --help` lists the options for the selected platform.
#pairs(
[`-Dplatform`], [`tui`, `gui`, `macos`; absent: the tui and SDL shells together, installed into `~/.local`],
[`-Dpdf`], [bool: build the pdf addon, fetching MuPDF the first time; default on once MuPDF is fetched, and off Linux],
[`-Djpx`], [bool, `true`: JPEG 2000, and with it scanned PDFs],
[`-Dgrammars`], [the tree-sitter grammars compiled in: `none` (the runtime alone), `minimal` (C, C++ and Zig) or `full` (all of them; the default)],
[`-Dembed-sources`], [bool, `false`: serve the sources under #file("/src")],
[`-Dhome-install`], [bool: a bare `zig build` also copies the binaries into `~/.local/bin` and the addon steps theirs into `$XDG_DATA_HOME/pardes/addons`, default true; with `-Dplatform` only `true` copies, and the addons alone],
[`-Dstamp-commit`], [bool: the git commit in `--version` and crash records; on for release builds and the `~/.local` install],
[`-Dworkspace-tag`], [bool: draw the workspace tag row; on except for macOS, whose menu bar carries it],
[`-Dmacos-sdk`], [path: the macOS SDK SDL's headers and frameworks come from, for a gui built for macOS elsewhere],
[`-Dprebuilt-shaders`], [bool: embed the committed SPIR-V; on for a bare `zig build`, off with `-Dplatform`; the shaders are sketchbook's (`dev/draw/shaders/`), and its `zig build shaders` refreshes the SPIR-V with glslc],
[`-Dtracy`], [path to a Tracy checkout; off],
[`-Dmacos-identity`], [codesigning identity for `pardes.app`; `-` (ad hoc)],
[`-Dtest-filter`], [run only tests whose name contains it; a filter that matches nothing fails],
[`-Dtest-rebuild`], [bool: fresh Zig test compilation],
[`-Dhelix-harness`], [reference executable for live differential tests; `HX_HARNESS`, else `hx-harness` on `PATH`],
)
== Patches to dependencies <dep-patches>
A fault in a dependency is fixed in the dependency, never guarded in the
core. pardes builds a few with edits of its own, made as each is fetched:
`build/patches.zig` holds, for each patched file, a table of exact edits
(and a file to append, in `build/patches/`), which `tools/patch.zig`
applies at make time, so a fresh fetch is patched the same and a bump that
moves an anchor fails that file's step. Each table says why, edit by edit.
#pairs(
[vaxis], [its input parser (no reply, split read or malformed sequence reaches the editor as typed text), its event queue (every blocked writer woken) and its render (a blank whose ink alone changed is not sent)],
[zstbi], [stb_image and its writer cut out: its resize alone is left, for a terminal PNG's downscale],
[ghostty], [kitty graphics: a path opened only once it is a regular file, shared memory non-blocking, an image no larger than a screen's storage],
[mvzr], [a step budget, a group's backtracking, `$` before `|`, the `**` Zig 0.17 removed, a class's range to `\xff`, and its reads past a pattern cut short; `docs/mvzr-group-backtrack.md` and `docs/mvzr-bounds.md` are the reports for upstream],
)
== As a library <library>
As `build.zig`'s head comment says it, word for word:
A package that depends on pardes is given its syntax, `pardes-syntax`
(`core/lib_syntax.zig`):
```
const pardes = b.dependency("pardes", .{
.target = target,
.optimize = optimize,
.grammars = .minimal,
});
exe.root_module.addImport("pardes-syntax", pardes.module("pardes-syntax"));
```
and, when it asks, the text pane, `pardes-editor`
(`core/lib_editor.zig`), here with a grammar of its own handed in at
run time:
```
const pardes = b.dependency("pardes", .{
.target = target,
.optimize = optimize,
.grammars = .none,
.editor = true,
});
exe.root_module.addImport("pardes-editor", pardes.module("pardes-editor"));
```
`.grammars`: the tree-sitter grammars compiled in: `none` (the runtime
alone), `minimal` (C, C++ and Zig) or `full` (all of them; the default).
`.languages`: instead of `.grammars`, the languages compiled in by name
(`"rust"`, or `&.{ "rust", "go" }`), each with what it needs (markdown
its inline grammar); a name the manifest does not have fails the
configure. `.editor`: exports `pardes-editor` besides `pardes-syntax`,
and with it fetches uucode, mvzr and vaxis.
The manifest, for a package's build script to read at configure time
(`@import("pardes").grammar_manifest.all`: each grammar's name, its
extensions and what it needs), is `grammar_manifest`.
A browser's module, one call a language (`wasmModule`, below):
```
const pardes = @import("pardes");
b.installArtifact(pardes.wasmModule(b, .{ .language = "rust" }));
b.installArtifact(pardes.wasmModule(b, .{})); // the patch view alone
```
Every dependency but tree-sitter's runtime is lazy in the manifest, so
such a package fetches what it asked for and nothing else.
`pardes-editor` has `pardes-syntax` in it as `.syntax`: a file belongs
to one module, so a package imports one of the two. `test/consumer` is
such a package, with every shape; `consumer-test`, which `unit-test` runs,
builds and runs it.
`build.zig.zon` names pardes by its path: `.pardes = .{ .path = "../pardes" }`.
Zig reports an option a dependency does not have, or a value it cannot
read, and builds on with the default (exit code 0): pardes ends the
configure itself (exit code 1, the manifest's names) for a language it
does not have, and for `.grammars` given beside `.languages`.
=== The syntax
It is the editor's own code under the names it has there. A language is
told by a path's extension, as in the editor, and `pathFor` gives a path
for a language's name.
#pairs(
[`highlightFileRange`, `analyzeSource`], [a style class for each byte (`Syn`: keyword, string, number, comment or none) and the declarations the sticky headers show, each with its bytes and lines],
[`parse`, `walkNodes`, `ts`], [a text's syntax tree, helix's walks from a byte range to the node about it, its siblings and children, and the tree-sitter bindings the nodes are read through],
[`theme`, `themes`, `page`, `ink`, `synInk`], [a theme by name and what it draws a page, its text and each class in],
[`lineCount`, `lineStartOffset`, `lineSlice`], [a line's bytes from its number],
[`grammars`, `supportsPath`, `pathFor`], [every grammar, and whether this build has a path's],
[`formatPatch`, `PatchCtx`, `Patch`, `PatchRow`, `PatchSource`], [a unified diff stacked as the patch view shows it (below)],
[`start`, `stop`], [tree-sitter's allocator, and the end of the parsers kept per language],
)
pardes's own build takes `-Dgrammars` too, and asks for what it is
missing in one round. Zig still unpacks a lazy package whose archive its
global cache already holds; `--pkg-dir` (or `ZIG_LOCAL_PKG_DIR`) at
pardes's `zig-pkg` shares the one copy. What a pane measures stays out: a
cell's width is vaxis's and uucode's, and a tab is `config.tab_width`
cells wherever it stands.
=== The patch view
`formatPatch` stacks a unified diff as pardes's +Diff shows it, with
nothing of the editor: each hunk under its `path:line`, what it stands in
above it, its lines behind their markers on their grounds, and the words a
changed line changed emphasised (delta's way). It returns a `Patch`, its
text and a `PatchRow` for each of its lines (row _i_ is line _i_), all in
the arena it is handed. A row's text is its line without its end, the
`\n` and a `\r` before it: a CRLF diff's rows have no `\r` (a reader that
must show one reads it off the diff), and a file handed in as `source` is
compared line by line the same way, so a CRLF file matches its diff.
#pairs(
[`kind`], [`other` (a mailed patch's headers, message, diffstat), `meta` (a section's own words: a mode, a rename, `Binary files ... differ`), `hunk` (its label, a star a declaration it stands in), `declaration` (what it stands in: a declaration's `path:line` and header lines from the file, or git's own words after the `@@`), `context`, `added`, `removed`],
[`path`, `line`], [the file its section names, and the line of it the row points at (0 for none)],
[`old`, `new`], [a hunk line's numbers in the old and the new file, null on the side it is not on],
[`location_end`, `code_start`], [where a label's `path:line` ends and what follows it starts],
[`styles`], [a byte a byte of its text: a `Syn` in `syn_bits` (with colours), `ground_add` or `ground_del` on a changed line, `ground_line` on the marker of one no other line changed, `ground_emph` on a changed word],
[`no_newline`], [the diff's `\ No newline at end of file` follows it],
)
What it takes of the program is a `PatchCtx`: an `arena`, and, each with
a default, `source` (the file a section names, as a `PatchSource`: its
text and path; none by default, so each hunk is numbered by the diff and
stands under git's words), `declarations` (none; `PatchCtx.parsed` reads
them from the file's parse), `colors` (none; `PatchCtx.languages` colours
each hunk as its file's language), `width` (code points; it decides
whether two lines are one changed) and `tslocations`. With no `colors`
and no `declarations` it calls nothing of tree-sitter. A reader with a
commit's two blobs hands the new one in as `source` (or the old one, for a
file the commit deleted): the hunk is found in it on either side.
=== In a browser
`wasmModule(b, .{ .language = "rust" })`, called from a package's build
script after `@import("pardes")` (its dependency named `pardes`), gives
the compile step of one module for a browser: wasm32-wasi, a reactor (no
`main`: `_initialize`, then any export), with that language's grammar and
what it needs, tree-sitter's runtime and pardes's highlighter. With no
`.language` it is the patch view alone, with no tree-sitter in it and no
import at all. `.optimize` is `ReleaseSmall` unless given. A name the
manifest does not have fails the configure. The module is
`pardes-<language>.wasm`, or `pardes-patch.wasm`; its exports
(`core/lib_wasm.zig`) are the same in every module:
#pairs(
[`memory`, `_initialize`], [its memory, and what a reactor runs once before any other call],
[`reserve(len)`, `release(ptr, len)`], [`len` bytes for the page to write an argument into (at least one, so `reserve(0)` is a pointer too), and their return],
[`highlight(path, path_len, text, len)`], [(a module with a language) a style byte for each byte of `text`: 0 none, 1 keyword, 2 string, 3 number, 4 comment, as pardes colours the file `path` names by its extension; all 0 for a path of another language. The module's until the next `highlight`],
[`patch(text, len)`], [the diff `text` stacked as the patch view shows it, with no file and no colours: a pointer to a header. The module's until the next `patch` or `patchFile`],
[`patchFile(text, len, file, file_len)`], [(a module with a language) `patch` with `file`, the text of the file the diff's first section names, the new one's or the old one's: each hunk is found in it and stacks under the declarations it sits in, from the file's parse (`declaration` rows), as the editor's +Diff does; a hunk the text does not hold, and the diff's other files, stack as with no file. The same header and records as `patch`],
)
xit writes its hunks' `@@` with nothing after it; with the file's text a
hunk still gets what it sits in. A page holding a commit's diff of
`src/lib.rs` and the file's new text calls (`put` copies bytes into a
`reserve`d buffer and gives the pointer and length):
```
const [d, dn] = put(diffBytes), [f, fn] = put(fileBytes);
const header = rust.exports.patchFile(d, dn, f, fn); // as patch's
```
and reads the rows as it reads `patch`'s. With the new text the labels and
declarations are numbered as the new file is; with the old text, as the
old one. Its cost is the file's parse: 18 ms against `patch`'s 4 for a
28-hunk diff of a 1,100-line Zig file, under node.
Each returns 0 when its memory runs out. Pointers and lengths are
wasm32's: unsigned 32-bit; in memory they are little-endian. `patch`'s
header is five u32, 20 bytes:
#pairs(
[0 `text`, 4 `text_len`], [the rows' text, a line a row, each ending in a newline; the rows' paths follow it in the same buffer],
[8 `styles`], [a byte for each byte of the text: `ground_add` 0x80 or `ground_del` 0x40 on a changed line, `ground_line` 0x10 on the marker of one no other line changed, `ground_emph` 0x20 on the words a changed line changed],
[12 `rows`, 16 `row_count`], [the rows, a record of nine u32 (36 bytes) each],
)
and a row's record:
#pairs(
[0 `kind`], [0 other, 1 meta, 2 hunk, 3 declaration (git's words after the `@@`; with `patchFile`, a declaration's `path:line`, whose `code_start` is its whole length, and its header's lines), 4 context, 5 added, 6 removed],
[4 `old`, 8 `new`], [its numbers in the old and the new file, from 1; 0 where it has none],
[12 `code_start`], [where its code starts in its text: after a hunk label's `path:line` and stars],
[16 `text_offset`, 20 `text_len`], [its text, without its line end (the `\n`, and a `\r` before it), in the header's `text`],
[24 `path_offset`, 28 `path_len`], [its section's path, in the header's `text` too],
[32 `flags`], [bit 0: the diff has `\ No newline at end of file` after it],
)
A module with a language imports three functions of
`wasi_snapshot_preview1`, `fd_write`, `fd_seek` and `fd_close` (libc's
streams, which tree-sitter's debug output names; `fd_write` is also how a
panic's message reaches stderr); a page stubs each (`fd_write` may write
to the console, the others answer `ENOSYS`, 52). The patch view's module
imports nothing. `test/consumer/wasm.mjs` is such a page, run by node:
`consumer-test` builds both shapes through `wasmModule` and runs them when
`node` is on PATH, and says so when it is not.
=== The editor
`pardes-editor` (`core/lib_editor.zig`) is the text pane as a library:
helix's keys on a text kept in memory, no files, commands, builtins,
prompts or views. Of vaxis it takes the grapheme width alone. With
`.grammars = .none` it has no grammar of pardes's: a program hands its
own in at run time (`Language`, below), linking the grammar's parser
itself, as `test/consumer` does with C's.
The editor is a value of plain data, `Editor(capacity)`: no pointer, no
allocator, every capacity in its type, so a program keeps it where it
keeps its state and it outlives the code that made it. Each call is handed
an allocator it may drop afterwards (a frame's arena), runs the editor's
own normal.zig and edit.zig, and says what came of the key.
#pairs(
[`key`, `run`], [a key as a pane in normal or insert mode takes it, or an action the caller built: `done`, `pending` (a count or a prefix awaits the next), `unbound`, `action` (bound but the caller's: a view, `:`, `/`, `s`, a language server, a macro), or `refused` (what it made does not fit; the value is as it was)],
[`selectRegex`, `setText`, `replace`, `setRegister`], [what a caller does with `s` once it has a pattern, a whole new text as one step to undo, a byte range rewritten as one (or `join`ed to the step before: a drag's frames one undo), the clipboard into `+`],
[`read`, `bytes`, `ranges`, `mode`, `pending`, `register`], [the text, the ranges in document order and the primary, normal, insert or select],
[`Language`, `Syntax`], [a grammar handed in at run time (its `TSLanguage` and query sources, told by an extension), and a text's parse and colours made again when it changes; drop it at a reload. Handed to `key` and `run`, the tree-sitter walks (Alt-o and kin, `]f`, `mif`) run on it; without one they are the caller's],
)
`zig build hxlib` runs helix's cases on it against the goldens, and
`-- --parity` against the file pane.
= Tests
```
zig build unit-test module and shell unit tests (the perf gate runs with it)
zig build test-build compile the unit-test programs without running them
zig build core-test core tests on the core without a terminal (no ghostty,
no C++): a shell boot is a scratch pane, and a test
that needs a terminal skips; the one to iterate on
zig build shell-test the same tests and the native shell's, with the terminal,
without the perf gate
zig build term-test the terminal module's own tests (core/term/)
zig build pane-test pane, output and namespace integration tests
zig build kitty-test kitty graphics conformance of terminal panes, at the core
zig build kitty-harness kitty graphics through a pane end to end (a fake kitty
terminal outside on the tui, the GPU capture on the gui;
kitten icat and chafa too when installed)
zig build syntax deterministic per-byte highlighting snapshots
zig build consumer-test the library, from a package that depends on pardes (test/consumer):
its shapes, a language named alone, the browser's modules
under node; unit-test runs it too
zig build hxlib helix's cases on the library's editor (-- --parity: against the file pane)
zig build behaviour-test behaviour through the 9P tree, in process (test/behaviour/),
and the T1 runner's short run of random 9P requests
zig build replicas the soak: a session and clients in process, on the real
session and client loops, each client typing over a
delayed link (-- [--seeds N] [--clients C] [--rtt 30,80,207])
zig build process-test real sessions over their sockets: mounts, flags, shells (test/process/),
and the tty's own cases, each a pardes in a pty against its golden
(test/process/tty_*.zig, test/snapshots/); -Dplatform=gui runs the
socket cases on the hidden window instead
zig build monkey-gui random input to the hidden window, every effect setting (-Dplatform=gui)
zig build 9p-test freestanding protocol tests
zig build 9p-io-test native 9P transports and client
zig build hxdiff differential suite against helix's own behaviour
(-- --parity: each case in a file pane and a pty pane, which must agree)
zig build hxgolf every helix-golf example, step by step, against helix
zig build perf-gate a gesture on the 50k-line file over 3x its baseline fails
zig build mupdf-check the PDF addon's MuPDF: compile, link, render and search docs/design.pdf
zig build cheatsheet render the cheatsheet PDF (needs typst)
```
- `-Dtest-filter=<text>` applies to every unit-test binary. A failed test's
printed trace can be stale: run it alone with the filter.
- `process-test -- <filter>` runs the cases whose name holds it (`tty
editing:` a family of the tty's, `tty editing: grep` one).
`process-test -- --update <filter>` writes a tty case's captures to its
golden: one case at a time, then a plain run, and read the golden's diff
for moved shell output (a flake) before keeping it. A tty case that fails
runs again alone with a wider settle probe before it counts; what it drew
is kept in `/tmp/pardes-snap/<name>/drew.golden`.
- `behaviour-test -- --monkey <seeds> [--steps M]` types random keys, leader
bursts, mouse presses, drags, wheel turns and tiny resizes at an in-process
session, a seed a child, hunting panics and hangs; a seed always makes the
same input, and a failure is shrunk to the steps that make it. `-- --seed N`
runs the random 9P requests with the documented rules checked after each.
Each crash found gets a fix and a regression case.
- `behaviour-test -- --replay` records each monkey seed's sessions as tapes
and replays them (three replicas and the dump); `process-test -- --replay
[--replay-with=<pardes-gui>]` records and replays the tty cases' real
sessions, their shells' and files' answers on the tape.
- The tutor's practice blocks (`# keys:`, `# before`, `# after` in
`core/tutor.txt`) are typed by hand; nothing runs them. What pins that
behaviour is `hxdiff`, which replays `test/hxcases` against goldens
recorded from a real helix, and its `--parity`, which runs each case in a
file pane and in a shell and demands they agree.
- `zig build hxdiff -- --strict cases.jsonl reference.jsonl [waivers.jsonl]` runs custom differential cases. `docs/helix-keys.md`
tracks helix parity key by key and names the reference helix build;
`docs/selections.md` is the selection model under normal mode.
- Benchmarks (`perf`, `pdf-scroll-bench`, `pdf-pointer-bench`,
`lspbench`, `fs-bench`) take `-- --json`; measure
with `-Doptimize=ReleaseFast`. `perf -- --base old.json` refuses reports
whose build metadata differs.
- `zig build history -- run 'ancestors(@, 2)' DIR -- zig build unit-test`
records a command's output and runtimes across revisions; `history -- compare a.json b.json 1.20` fails past that runtime ratio.
- `python3 -B test/agent_session.py <pardes> --ready 'text' --min-rows N -- command args` drives an interactive command in a private shell and checks
it through 9P.
The optional real-server check
`python3 -B test/real_zls.py GUI_BINARY ZLS_BINARY OUTPUT_DIRECTORY`
uses a private home and a restricted desktop `PATH`. It checks a build-module
import as the first query without retries, standard and relative imports,
paths with spaces, imported symbols,
hover, references, rename previews, diagnostics and completion while editing.
It requires Zig and ZLS; `core-test -Dtest-filter=lsp_client` checks the
protocol client's own tests without installed language servers.
== Release gates
A release passes all of these first: `unit-test` with `-Dplatform=tui` and
with `-Dplatform=gui`, `core-test`, `behaviour-test`, `process-test` (on both:
its tty cases on the tui, the hidden window on the gui), `hxdiff` and its
`--parity`, the replicas harness and the soak (`test/soak.py`), and the
ReleaseSafe tui and ReleaseFast gui builds. The committed
`docs/typ/cheatsheet-a4.pdf` is rendered again when the docs change.
= Sending patches <patches>
Mail patches to the list `~gbrls/[email protected]`; it is the preferred way to contribute:
#cmd("git config format.subjectPrefix \"PATCH pardes\"\ngit send-email --to=~gbrls/[email protected] HEAD^")
#link("https://git-send-email.io")[git-send-email.io] sets `git send-email` up. The mirrors (#doc("setup", section: "install")) are for cloning, as a GitHub one will be; pull requests are not the preferred route.
= The principle <principle>
The owner's rule, in their words: "keep the wire protocol simple and raw,
and the local state process it" (2026-11-19). Every design choice is
measured against it.
What crosses between processes is raw and ordered: a client's keys and
gestures, and the session's steps, each with what the session's machine
answered inside it (a file's bytes, the clock, a directory's listing) as
the machine said it. Each process's own core turns them into state. Nothing
computed is shipped where the input that computes it can be, and bandwidth
is spent before correctness is. What follows from it in the tree today:
- A client is a whole core, loaded once from the session's dump. It applies
its own input at once and replays the session's units over it, rolling
back where the session ordered otherwise; nothing it draws is sent to it.
A picture or a game is drawn by the process that shows it, from its own
addon's code.
- A step that asks the machine asks through the tape (#link(<tape>)[below]):
the session records the answer, a client's replay reads that answer and
not its own machine's. The session's home, shell and directory are state
of the core for the same reason.
- What may wait is what touches the session's machine; the lists that say
so are checked by a test (#link(<may-wait>)[below]).
- Nothing a frame writes is read by a step, and nothing of a core is a
process global, so a core that drew and one that did not, in this process
or another, step alike (#link(<core-checks>)[the checks]).
- The exceptions, each small and named: the dump a client joins from (it
cannot replay from the start); the digests and quiet checks (a check, not
state); and a terminal, which crosses as the session's screen state
(rows and their changes), not as the pty's bytes.
= The tree <tree>
#pairs(
[`core/`], [the editor as a state machine: panes, layout, the frame, the 9P tree, the tape and the replica engine. Module `core`, rooted at `core/root.zig`; the core's struct is `core/Core.zig`, `pardes.Pardes`],
[`dev/`], [the devices: the frontends (`dev/tui`, `dev/gui`, the Mac app's Swift in `dev/macos`), the host's services (`dev/host_io.zig` and the files beside it: `Host`, `Shell`, `Inbox`, `KindJob`, `Lsp`), the file watcher, the fonts, the language servers' client. Module `dev`],
[`runtime/`], [what runs a session: a session's loop (`session.zig`) and a client's (`client.zig`), the daemon and the stub, the upgrade, the crash file, the 9P listener and client (`9p_io.zig`). Module `runtime`; beside it each product's root, a module of its own: `runtime/main.zig` (`pardes`, `pardes-gui`) and `runtime/macos.zig` (the Mac's library)],
[`abi/`], [what crosses a process or a language: the replica stream (`abi/wire.zig`, module `wire`), the records the core and the terminal share (`plain.zig`), the terminal's table (`term.zig`), the addon's header and module, `pardes.h`],
)
The modules name each other. A Zig test program runs the tests of the
module it is rooted at, so `core-test` and `shell-test` run one program a
module (core, dev, runtime, wire, and for `shell-test` `runtime/main.zig`'s);
each module's `root.zig` lists the files whose tests it runs. Beside the
four: `addons/`, `build/` (the build script's parts, the patches), `test/`,
`tools/`, `shaders/`, `vendor/`, `assets/` and `docs/`.
= The core and its frontends
One core, three frontends (tui, gui, macos). The core owns
editing, layout, rendering and the virtual filesystem it serves over 9P; a
frontend turns native input into `pardes.Event`, presents `pardes.Surface`,
and its host performs effects such as spawning processes.
- The core is a state machine: input arrives as an `Event` through
`update`; output leaves as a `Surface` from `render(arena)` and as a ring
of `Effect` values. Effects are fixed-size values with no lifetime ties
into the core; unbounded content is read off the core when an effect is
drained (`save_text` carries a path and the pane's serial, so a reused
slot writes nothing). `emit` refuses when the ring is full and never
evicts.
- Pty bytes leave as 64-byte `.write` effects; overflow parks in a per-pane
buffer that `nextEffect` drains in order. `postEvent` is a 64-entry value
queue for events that borrow no slices.
- `pump` runs: wait for input (the host owns the sleep), flush paused 9P
write batches, drain queued events, drain effects, settle the turn, then
render and present only when a frame is needed and someone is looking.
- `Host` (`dev/Host.zig`) is a context pointer and a table of optional
callbacks; a null method is not an error, and `Host{}` is a complete
in-process pardes that the tests use. Comptime decides what a build has
(`pardes.platform`, `can_attach`); the table decides who serves it.
- The terminal and the window draw from the frame alone: the `Surface`
(cells, regions, layers, placements, cursor, `Chrome`) and one record
beside it, `Facts` (`core/surface.zig`: the view's size, each open
pane's rectangle, tag rows and kind, the settings a frontend draws by),
which `draw.place` fills with the frame. Each reads these, not the core,
to draw and to turn its input into events, so a client draws its own
view with no view of the core made live. A few reads of the core remain, in the window: its
hit test, its close and a session window's font.
- The macOS build is a library whose Swift app owns `main()` and the run
loop (`runtime/macos.zig`); it reads the core beyond the frame (its
theme, a pane's document state) and runs neither loop below.
- The terminal is a module of its own, `term` (`core/term/`): the
emulator (ghostty-vt), kitty images (wuffs, zstbi), the pager's parse
and the replicas' screen state. It never imports the core; the core
reaches it through one table of plain-data entries (`abi/term.zig`,
through `core/Term.zig`), chosen at compile time. The records both name
(cells, colours, keys) are `plain` (`abi/plain.zig`, std only).
Every product is built with the terminal; core-test's core is built
without it, so its compile names no ghostty, wuffs, zstbi or C++.
- `memory.limits` is the one home for the core's capacities (selections,
the tag's bytes, undo, the arenas' regions). Each core
allocator is a DebugAllocator over its arena's reserved region, thread
safe, its checks in Debug builds only.
Code map: `core/panes.zig` and the pane kinds it names (`core/File.zig`,
`core/Term.zig`, `core/Kind.zig`, ...), `core/layout.zig`, `core/fs.zig`
(host access, mounts and resolution), `core/Core.zig` (the core's state
and its entries) and `core/draw.zig` (the frame), with one file per thing
beside them (`edit.zig`, `normal.zig`, `look.zig`, `exec.zig`,
`mouse.zig`, ...). `core/ninep/` is the control tree, `core/tape.zig` the
tape, `core/replica.zig` the replica engine (a client's `Replica` and
`Link`, a session's `Peers`), `runtime/daemon.zig` the per-user daemon and
a terminal's stub, `abi/wire.zig` the replica stream's frames, `core/lsp/`
the language-server client; `test/` holds harnesses, goldens and helix
cases, `build/` the build script's parts.
== The loops <loops>
A session runs `runtime/session.zig`'s `Loop`, a client
`runtime/client.zig`'s, each over the frontend it is given (comptime: no
vtable). The terminal's and the window's own frontend (`Own` in each) and
their client frontends (`Tty`, the window's `Client`) give the calls;
what a loop holds and its order are written once.
- The session's loop holds its allocators and workers, its core (new,
from a dump, or an Upgrade's), the prompt files its shells start with,
the 9P listener and its clients (`Peers`). A turn is a `pump`, the step's
unit to the clients (`Peers.boundary`), the frontend's `sent`, an
Upgrade or a Restore between steps, and the daemon's notes. Its parts end
in one order: the listener, the clients, the frontend's devices, the
prompt files, the core, the workers, the allocators. The devices (ptys,
their readers, watched files, the jobs, the queue they post to) are
still each frontend's (`open`, `start`, `close`).
- A client's loop holds what it keeps across joins (`client.Start`: its
allocators, the addons it distrusts, its frame timings, the blobs it
keeps when far) and runs its joins: the reader thread, a pass at a time,
and a join again after a divergence or an Upgrade. A pass: the
frontend's own work (`own`), the predicted echo's frame, a draw or the
frontend's own motion (`again`), the wait (`deadline`, `wait`), then the
session's units settled, and a yank, paste or link its view asked for
done on this machine (`copy`, `paste`, `open`). An input is sent to the
session before it is applied here.
- The soak, `zig build replicas` (`test/replicas.zig`), runs both: its
session is `session.Loop` over a frontend whose host is the soak's
delayed network, every client a `client.Loop`, so what it tests is what
a session and a client run. The core's own unit tests stand a session in
with `replica.Authority`.
== The tape <tape>
The tape (`core/tape.zig`) records what a step takes in: the entry a host
called and its arguments, and every answer of the machine inside the step.
A session records its steps; a client replays the session's and records
its own speculations, to compare.
- An entry is written once: a private body and, directly under it, its
public name made by `tape.taped`
(`pub const update = pardes.tape.taped(updateInner);`); the tape's
`Input` variant and the replay come from the body's parameters. The few
written by hand (`init`, `deinit`, `pump`, `render`, ...) each say why.
- The tape is the handle a step is given, as an allocator is: a core
holds it (`p.tape`, never null), and a function handed no core takes
`tape: *Tape`. A step's question of the machine goes through it
(`p.tape.value`, `p.tape.site`, `p.tape.authorityOnly`): the session
records the answer, a replay reads it. So far the kinds and addons, a
directory's listing, the file system, the core's own files and the
smaller ones ask it so; beside the field, the process's `tape.on` still
says which core is being stepped.
- A pane's and a view's slot are types where they cross the core's edge
(an entry's argument or answer, an event, an effect, a host call, the
wire's welcome): `Pane.Id` and `View.Id`, each an `enum(u8)` of its own.
The core's own loops keep their integers.
- The session's facts, its home, default shell and directory, are the
core's state (`session_home`, `session_shell`, `session_dir`), read once
at its start through the tape and carried in its dump: a far client's
`~`, #word("Shell") and a word run in the session's directory are the
session's.
== What may wait <may-wait>
A client applies its own input at once; a step that needs the session's
machine stops its speculation, and the input waits a round trip for the
session's unit (`tape.hostCall`). The user's line: motions, edits and
layout are instant; what has a side effect on the system may wait. Two
lists say which, each row with why:
- `keeps_stopping` (`core/tape.zig`): the Host entries that return nothing
and still stop a speculation (`pty_write`, `spawn`, `write_file`, ...).
A client records any other such call as made and goes on; an entry that
answers stops by nature, the answer being the session's.
- `may_wait` (`core/replica.zig`): each word, key or gesture that may wait,
on a kind of pane, and where it stops. A test walks every word of the
registry, every key (and every key after each prefix) and the gestures
between panes, on a file's, a kind's and a shell's pane: a stop not
listed fails it, and a row nothing stopped at is stale. The soak judges
its stops by name against the same list.
== The checks <core-checks>
At the end of `core/root.zig`, run by `core-test`, `shell-test` and
`unit-test`:
- *No process globals:* every container-level `var` in `core/` is on
`allowed_vars`, with why it is not a core's.
- *What the core asks of the machine:* every line of `core/` naming libc,
`std.posix`, `getenv`, a `std.Io` file, directory or socket, `dev` or
`runtime` is counted, a file at a time, against `machine_lines`, with why.
- *What is kept out of the digest:* every field kept out of the digest and
the dump (`.local`, `.derived`) is on `kept_out`, with who writes it and
why a step's read of it is the step's.
Beside them, `core/draw.zig`'s test "a frame changes nothing a step reads"
steps a core that draws after every input and one that never draws, and
their digests agree after every step.
== Threads
The core is single-threaded under a turn mutex, `pardes.turn`. The editor
thread holds the turn and lets go of it while it waits for input and while
it is out in a host syscall mid-step; a 9P connection task takes the turn
in those gaps to answer a request. While a step is out, a request that
would change a pane parks (`Status.again`) and is retried when the editor
rests. 9P requests are not events: they enter through `Pardes.serveFs`, and
only one that changes a pane costs a frame. Language-server and
selection-pipe workers post completions through a bounded mailbox; a
`Lsp.Job` (`dev/Lsp.zig`) owns copies of the source, path and arguments, never
reading the live core, and a request id, the pane's serial and (for an
edit) the file's revision reject a stale reply. A process that never calls
`turn.start` (the tests) has no second thread.
== The daemon and detached sessions
One `pardes --daemon` per user (`runtime/daemon.zig`) listens on
`pardes-daemon.sock` in the runtime directory, its `.lock` flocked for its
life; the first session that finds none starts it (a double fork and an
exec) and it exits a second after it holds nothing. It is nobody's parent:
a session registers on one connection, sends it a dup of every pty master
it forks (so a session killed outright never hangs its shells up, and
#word("Adopt") takes them into another), says whether it has let its own
terminal or window go, and is handed each client's connection through it
(`pardes attach`, below). A daemon that dies is started again by the
sessions: each one's link's reader, woken by the link's end, dials the
next, says the hello again and tells it its masters and its clients.
Messages are `SOCK_SEQPACKET` datagrams: a line of words and the fds it
carries.
`pardes` in a terminal forks its session off before anything starts: the
session, in a session of its own (`setsid`), draws on the terminal it
opened, and the process the shell waits on stays as its stub, passing it
`SIGWINCH`, `SIGTERM`, `SIGINT` and `SIGQUIT` and ending with its status.
#word("Detach"), or the terminal's end (EIO, a hangup), puts
the terminal back and lets it go; the stub ends (exit 0) and the session
runs on with nothing drawn, detached for good: `pardes attach` is a client
of it. A window's #word("Detach") closes the window and keeps the
process, the same. `PARDES_DAEMON=off` (the
process tests) runs a session as before the daemon: in the terminal's
own process, ended by its hangup.
A replica (a client running its own copy of the core, phase 5 of
`docs/next-daemon.md`) connects to `pardes-replica.sock`, a stream socket
beside it that `ssh -L` can forward. `abi/wire.zig` is its
protocol (its `version`): fixed-width little-endian fields, a 5-byte header
(tag, then a u32 length), the hello, a ping, the input events. The hello
names a session and carries the client's semantics hash (the protocol,
release, commit, build options, every dependency's hash and the tape's
shape; the terminal's build and the window's share one, so either joins
the other's sessions); the daemon refuses another build's, or a session
nobody has, in words, and hands an accepted connection to its session
(`SCM_RIGHTS`), which welcomes it and from then on is the one it talks
to: the session sends the dump the client loads, then each step's tape and
its terminals' screens, and the client sends its inputs
(`core/replica.zig`; #doc("running") draws it).
#word("Upgrade") (`runtime/upgrade.zig`) execs the path the session was started
from, at a step boundary, after asking it `--version`: the session's dump
is committed to the state file as `upgrade-<pid>` (the build before wrote
`pardes-upgrade-<pid>.dump`, which this one still reads, for one release), and its pty masters,
terminals, stubs and daemon link stay open across the exec, named in a
record on the new command line (`--upgraded=`) ahead of the arguments it
was started with. The pid stays, so the shells stay its children; the new
process loads the dump with every view and takes the fds back. One that
cannot load it execs the old binary again (kept open from
`/proc/self/exe`), or with none starts afresh with the shells adopted and
the dump left for a Restore. The daemon re-execs the same way, its
registry on its command line. `python3 -B test/daemon.py --upgrade <old>
<new>` (and `--upgrade --gui`) is the end-to-end test.
= 9P
The wire format, the client and server connections, the file-server engine
and the Unix and TCP transports are the `cloud9` package
(`git.sr.ht/~gbrls/cloud9`), pinned in `build.zig.zon` and fetched into
`zig-pkg/`. Re-pin with
`zig fetch --save=cloud9 git+https://git.sr.ht/~gbrls/cloud9#<commit>`, or
use `.cloud9 = .{ .path = "../cloud9" }` while editing both. cloud9's own
`zig build test`, `transport-test`, `fuzz` and `differential` cover the
shared code.
- The engine (`fs.Server`) owns fids, permissions, directory reads,
flushes and what a hangup releases; it allocates nothing and makes no OS
calls. The control tree in `core/ninep/` is its backend: `tree.zig`
(nodes, dispatch), `pane.zig`, `ctl.zig`, `cols.zig`, `addr.zig`,
`pty.zig`, `events.zig` (event and log), `screen.zig`, `sources.zig`.
`runtime/9p.zig` names the editor's engine settings: msize 65536, 256 fids,
128 held reads a connection, names up to 255 bytes.
- A read, write, open, clunk, remove or truncating wstat can park; a walk,
attach, stat, create or rename cannot. Every Rread is clamped to the
count and the msize; Tflush answers the original request first.
- Unix and TCP listeners run on cloud9's `serve.Runner`: an accept task per
listener, a reader and a writer task per connection, 16 connections.
- A body write takes only whole UTF-8 sequences and answers a short count
for the rest. Consecutive writes from one open at the end of the text are
held and applied as one edit, flushed by any other request, the open's
release, 64 MiB, or a 20 ms pause in the editor's step.
== Writes through a mount <writes-through-a-mount>
A mount cuts a big write into pieces of at most one message (msize 65536,
less the header), anywhere, and each command line runs once its newline
comes; nothing is read into a write's size. A last line with no newline
(`printf Save > exec`) runs when the file is closed, as does an #word("Edit")
block never ended, and its failure is then only in the log, as its `err`
record: the close reports no error, and the write that sent it had
already succeeded. So a script that needs a line's result ends it with a
newline. A line over 1 MiB is refused once, and the rest of it, through
its newline, is dropped.
== Listeners <listeners>
`--9p-tcp='tcp!127.0.0.1!5640'` adds TCP. Addresses are numeric IPv4 or IPv6; port
0 picks one; #file("/listeners") reads them back. Every connection has full
session access, #file("/os") included, and TCP is unencrypted: use
loopback. Unix and TCP share 16 connection slots; a 17th client's Tversion
gets `too many connections` (and the log
`err - 9p: too many connections (N turned away)`).
`$XDG_RUNTIME_DIR/9p` is the machine's `/srv`: servers post themselves
there by name, and `9ns --mntgen` mounts the whole registry. A pardes whose
socket is in the runtime directory posts
`$XDG_RUNTIME_DIR/9p/pardes/<name>`, a symlink to its socket, and unposts
it on a clean stop if it is still its own; one whose socket fell back to
`~/.local/state/pardes` posts nothing. Posting first sweeps the group: a
symlink whose socket refuses a connect is removed with its socket. pardes
binds its own socket rather than going through `cloud9.post`, which takes
only flat names. A reader of the registry must `stat` through the symlink.
== Kernel mounts
For Linux v9fs use `version=9p2000,cache=none,access=any`, `trans=unix`
(or `trans=tcp` with `port=`), `uname`, `dfltuid` and `dfltgid` for the
local user, and an empty `aname`. Linux follows `O_TRUNC` with a `Twstat`
of zero length and an `mtime` hint; pardes takes the truncation and drops
the hint.
= Other notes
`docs/` keeps the platform and design notes beside this book:
`macos.md` (the macOS shell; `web.md` says what the browser shell was),
`render-pipeline.md` (a frame, and what the frontends draw on their own), `helix-keys.md` and `selections.md` (helix parity),
`lsp-evaluation.md`, `ui-review.md`, `divergences.md` (bookmarks off
`main`) and `open-questions.md`. `next-steps.txt` is a wishlist and
`transactions.txt` records one open structural gap against helix.
|