Designing 106 syscalls with an agent: from ad hoc API calls in January to a machine-readable contract in June
The syscall table in the UnoDOS x86 kernel has 106 entries. I want to tell the story of how it got them, because the interesting part is not the number. It is that nobody, in January, was designing an interface that would one day be the definition of the operating system across twenty-odd machines. In January the agent was trying to get a clock app to draw on the screen without crashing. By June the same table had become a file of data that every other port is generated from, and the x86 kernel that had always been the definition was, in the project's own words, "demoted." Neither of those facts was planned from the other.
January: a plan, then a different plan
The syscall layer has a design document before it has any code. On January 23 at 02:30, daa502fb, "Document architectural plan and hybrid system call approach", recorded a decision with the confidence of someone who has read the right books:
Key Architectural Decision: Hybrid System Call Approach - INT 0x80 for discovery (one-time, returns API table pointer) - Far Call Table for execution (frequent, ~30 cycles vs ~70 for INT) - Saves ~9% CPU on graphical workloads at 4.77MHz - Follows Windows 1.x/2.x, GEOS pattern
The companion docs/SYSCALL.md committed alongside it opens with the literal question that produced it ("Explain the benefits and drawbacks of a far call table vs software interrupt (INT 0x80) for system calls when writing an operating system") followed by the answer. That is the honest shape of the method at this point: a prompt and a response checked into the repo as architecture.
Two hours later, 1e124f15, "Implement Foundation 1.1: System Call Infrastructure (v3.3.0)": an INT 0x80 handler whose only job is to return a pointer to a table, and the table itself, "10 stub functions organized by category", six graphics, two memory, two event. The commit message says "Table offsets are the ABI - stable across kernel updates." That sentence was true and is still true; the table at v3.34.0 begins with the same ten entries in the same order, gfx_draw_pixel at slot 0. But the mechanism around it did not survive the week.
Two days later the first real app tried to use it. df74d728, January 25, "Fix INT 0x80 to dispatch API calls for apps (Build 014)":
The clock app was crashing because kernel API functions use near RET but apps were trying to do far CALLs across segments. Changes: - INT 0x80 now dispatches function calls (AH = function index) - AH=0 still returns API table pointer (discovery) - AH=1-24 calls the corresponding API function
So the far-call table, the whole point of the hybrid design, the thing that was going to save nine percent of the CPU, was quietly abandoned on contact with the first caller. The kernel's routines returned with a near RET; an app in another segment calling them far would corrupt the stack. Rather than rewrite every routine, the agent turned INT 0x80 from a discovery mechanism into the dispatcher, with the function number in AH. By March the dispatcher carried the comment "NOTE: AH=0 is gfx_draw_pixel (no longer API discovery)". The discovery call did not merely lose its purpose; it lost its slot.
What followed was the ordinary pain of a real-mode ABI being found rather than designed. df7b7a00 the same evening: "Save caller's DS and ES in kernel variables before API dispatch", fixing a window title that rendered as "SQRMU". 5410e32c, January 26: "INT 0x80 now preserves carry flag from called functions / The IRET was restoring original FLAGS, losing CF set by functions". 383a5aa1, later that day: "INT 0x80 handler used pushf/pop ax to capture FLAGS, which destroyed any return value in AX (like win_create's window handle)." Each of those is a convention being discovered by violating it. Error in the carry flag, handle in AL, function number in AH, strings read from the caller's DS: none of that was in the plan. All of it is in the contract now.
February: the table grows faster than anyone is counting
The table's header carries its own slot count, and tracing that one number through the history is the quickest way to see what kind of object this was. 10 slots on January 23. 25 on January 25. 31 on January 29. 64 on February 18, the day 7f569eda landed "Add 14 new kernel APIs: colored drawing, system, FS, window (Build 247)", numbered 67 through 80 in the message. Read that again: on the day the table was declared to hold 64 slots it was being extended to 81.
The header then stayed at 64 for three weeks while the table kept growing: 90 entries on February 19 when the clipboard and popup-menu calls came in (ec2a02b1), 100 on March 1, 105 on March 3 (fb91d8fd, "Add scaled sprite, screen blit, and read pixel APIs (Build 390)", APIs 102 to 104). The dispatcher's range check said cmp ah, 105, its comment two lines above said "(0-56 valid)", and the header eight kilobytes away said 64. Three counts of the same table in one file, and the only one that mattered at runtime was the cmp, which is why nothing broke. The header was finally corrected on March 12 by c5e2f8dc, "Code review fixes (Build 394)", as item four of seven: "MAINT-4: Update API table header count from 64 to 105".
I do not think this reflects badly on the agent. It reflects what an interface is when it lives only in code: a side effect of whatever the last app needed. The Notepad needed a clipboard, so the kernel got one. A racing game needed scaled sprites, so slot 102 exists. The numbers were assigned by appending. Apps hard-coded them as constants, and on February 11 (ee85404c, Build 106) every app in the tree had them wrong at once: "All app API constants were off - e.g. API_WIN_CREATE was 19 but kernel table has win_create at slot 20 (slot 19 = app_run). This caused the launcher to recursively call app_run instead of win_create, hanging the system." There was no single place where slot 20 was defined as win_create. There was the table, and there were four apps that each remembered the table slightly differently.
The table reached 105 on March 3 and then did not move for three months.
June: the same numbers, written down as facts
The thing that changed in June was not the table. It was that the project had started porting, and each port had re-derived the ABI by reading the x86 assembly. On June 11 a prose specification arrived to stop that: a18b8242, "Add port specification; Mac port strategy now Toolbox-based", creating docs/PORT-SPEC.md, "the platform-independent contract for UnoDOS ports". Its second paragraph contains the sentence that the later design document would hold up as the whole problem:
Extracted from the x86 reference implementation. Where this file and the x86 source disagree, the source wins — report the discrepancy and update this file.
A specification that defers to the code is a description, not a contract. And it drifted immediately. The same afternoon, 4a2fce5b added "API 105 theme_set_palette" and the table became 106. PORT-SPEC kept saying "105 functions"; ARCHITECTURE.md said "106 functions (indices 0-105)". Four days later the two documents disagreed with each other about how many syscalls the OS had, and docs/CONTRACT-ARCH.md named this, along with the FAT12 geometry living "in FIVE places on x86" and a font advance documented as both 12 and 8 pixels, as the reason the project needed something other than prose:
"105 vs 106" becomes
len(syscalls); PORT-SPEC's tables regenerate; the font advance is fixed once.
On June 15 at 23:17, 55d5a7fe, "unodef: Phase 0 — author the first UNODEF contract (no behavior change)". The message is careful about what this is and is not:
Encodes the CURRENT shipping x86 surface as the single machine-readable Contract (Layer 0 of docs/CONTRACT-ARCH.md), transcribed from kernel/kernel.asm > docs/PORT-SPEC.md > docs/API_REFERENCE.md. Additive and non-behavioral: nothing generates from it yet (that's Phase 1).
"Transcribed" is the right word. unodef/unodef.toml does not redesign the syscall surface. It writes down the one that had accreted since January, slot by slot, with "ordinals contiguous 0..105", and it includes a [discrepancies] ledger that resolves the contradictions the prose had accumulated rather than hiding them. The first entry reads, in the file: "Resolved: callgate.count = 106 = len(syscall). The '105' was an off-by-one (last ordinal vs count)." Six minutes later (ae5c9e3e, "transcribe register signatures for all 106 syscalls") every slot had its register signature, twelve of them taken "from their kernel.asm handler-header comments (the source of truth)" because the API reference had never written them up.
Then the part that makes the data trustworthy. ebb1ed9a at 23:28 added unogen, which "reads unodef.toml and emits the SHAPE of the boundary (syscall ordinals, struct offsets/sizes, FAT12 geometry, font metrics, palette, enums) — never bodies or CPU logic". And at 23:38, 02f44e94, "kernel: source the contract surface from unodef/gen/x86 (byte-identical)":
PROOF: rebuilt with
nasm -f bin -Ikernel/(build_info.inc held constant) the kernel is byte-identical to the pre-change binary — SHA256 04b9709a..., matching the committed build/kernel.bin. The generator now IS the source of truth for these constants, validated against the known-good build.
This is the moment the interface stops being a side effect. The kernel still has the same 106 slots in the same order. But the constants it assembles against now come from a generated include, the generated include comes from the TOML, and the proof that the TOML is correct is that the binary did not change by one byte. The x86 kernel, which had been the definition for five months, became the first thing that has to agree with the definition. The next morning the Amiga port consumed the same generator the same way, 245b51c8, "byte-identical" again; the SNES, Genesis, Mac Plus, IIGS, C64 and Apple II followed before lunch. The eleven fresh ports that came after that are the previous essay.
What 106 is, and is not
At v3.34.0, kernel/kernel.asm includes unodef/gen/x86/unodef.inc, whose line 8 reads SYS_COUNT equ 106. The table's header says 106, the table has 106 dw entries, the last of them theme_set_palette at 105, and unodef.toml has 106 [[syscall]] blocks. The number in the title is real and it is consistent in four places at once, which in March it was not.
What the number is not: designed. Nobody chose 106. It is the count of things apps had asked for by June 11, frozen at the moment the project decided to write them down. The contract did not add a syscall or remove one; "no behavior change" was the whole point, because a contract that changes the thing it describes cannot be verified against it. The design decision of January (hybrid far-call table, nine percent CPU) is gone without a trace except in docs/SYSCALL.md. The accidents of January (AH holds the function number, CF carries the error, handles are one byte) are the contract's [callgate.binding.x86] section.
The lesson
The interface you will eventually need to write down is the one you already have, so make it possible to transcribe before you try to redesign it. In January there was no way to know that the syscall table would become the definition of an OS across dozens of CPUs, and trying to design for that would have produced a far-call table nobody called. What made June possible was not foresight. It was that the table, however ad hoc, was a single contiguous list with stable ordinals, and that every fact about it could be checked against a known-good binary. The transcript could be proven correct before a single port depended on it.
The transferable version: you cannot predict which of your internal interfaces will one day have a second implementation. You can keep each one in a state where, on the day it does, an agent can write it down as data and prove the transcription with a byte-identical rebuild instead of an argument. That means one source for each count, numbers that are appended rather than reassigned, and a build you can reproduce exactly. The contract arrived in an evening because those three things happened to be true. The window manager that sits behind eleven of those syscalls was not nearly so tidy, and that is the next essay.
Sources: https://github.com/hmofet/unodos, commits daa502fb, 1e124f15, df74d728, df7b7a00, 5410e32c, 383a5aa1, ee85404c, 7f569eda, ec2a02b1, fb91d8fd, c5e2f8dc (January 23 to March 12); a18b8242, 4a2fce5b, 55d5a7fe, ae5c9e3e, ebb1ed9a, 02f44e94, 245b51c8 (June 11-16); docs/SYSCALL.md, docs/PORT-SPEC.md, docs/CONTRACT-ARCH.md, unodef/README.md, unodef/unodef.toml and unodef/gen/x86/unodef.inc at v3.34.0.