Q-07: Toolchain / KOS Contract
Status: ANSWERED Agent: opencode/ext-agent Timestamp UTC: 2026-05-11T03:25:00Z Claim: CLAIM | opencode/ext-agent | 2026-05-11T03:12:00Z
Prior Answers Checked
- Q-01 (pi-teams-fit): pi-teams has no built-in KOS integration. Toolchain must be exposed via prompt discipline + documentation.
- Q-06 (runtime-container-shape): Single Docker container with bind mounts. KOS toolchain is a mount question.
- Q-00 (architecture-synthesis): Recommends pi-teams, LLM-wiki, T0-T7 lifecycle. Did not specify toolchain contract.
- BUILD_STATUS.md: Current blocker is asset extraction, not toolchain. KOS build works once toolchain path is set.
- REALIGNMENT_BRIEF: Previous agents broke builds by drifting into CDI/ISO/IP.BIN repair loops instead of the validated KOS ELF path.
Short Answer
Expose KOS as a documented command contract with a fixed toolchain path, not as an installed service. The KOS toolchain already exists on relik-pi4 at /mnt/kitchen/collab/d3-tui/toolchains/kos/ (80MB, including kos-cc and sh-elf-* tools). The primary issue is that agents do not know this path -- the build script's detect_kos() function searches 4 locations but none match the actual install. Fix: set KOS_BASE once in the Docker container or a .env file, bind-mount the toolchain at /opt/toolchains/dc/kos, document the contract in LLM-wiki, and create a smoke test.
Evidence
1. KOS Toolchain Locations on relik-pi4 (verified 2026-05-11)
| Path | Contents | Status |
|---|---|---|
/mnt/kitchen/collab/d3-tui/toolchains/kos/ |
Full KOS tree (bin/, include/, lib/, environ_base.sh, addons/) | Working toolchain -- 80MB |
/mnt/kitchen/from-house/archive/d3-tui/pre-realign-20260510T194241Z/toolchains/kos/ |
Full KOS tree + environ_dreamcast.sh | Archive only -- do not use for builds |
/opt/toolchains/dc/kos/ |
Does not exist | Search path #3 in build script |
$HOME/.pearl/staging/dc-sdk/dc/kos/ |
Directory exists, no kos/ inside | Search path #4 in build script |
$KOS_BASE env var |
Not set | Not in PATH, not exported |
kos-cc in PATH |
Not found | which kos-cc returns nothing |
The collab path at /mnt/kitchen/collab/d3-tui/toolchains/kos/bin/ contains:
kos-cc, kos-c++, makeip, scramble,
sh-elf-addr2line, sh-elf-ar, sh-elf-as, sh-elf-c++filt, sh-elf-cpp, sh-elf-elfedit, ...
Critical gap: This path is NOT in the build script's detect_kos() search order. The build script searches:
1. $KOS_BASE env var (not set)
2. $ROOT_DIR/toolchains/kos/ (bundled with repo -- does not exist in current repo)
3. /opt/toolchains/dc/kos/ (does not exist)
4. $HOME/.pearl/staging/dc-sdk/dc/kos/ (dc/kos/ subdir missing)
The build script would fail: ERROR: KallistiOS not found.
The fix requires:
- export KOS_BASE=/mnt/kitchen/collab/d3-tui/toolchains/kos
- export KOS_CC_BASE=$KOS_BASE
- Or bind-mount: /mnt/kitchen/collab/d3-tui/toolchains/kos -> /opt/toolchains/dc/kos inside container
2. What D3-TUI Actually Needs from KOS
From src/d3tui_main.c and build-dreamcast.sh:
| Component | What It Is | How D3-TUI Uses It |
|---|---|---|
kos-cc |
sh-elf-gcc wrapper with KOS headers/libs | Compiles all .c files |
<kos.h> |
KOS main header | #include in d3tui_main.c |
<dc/biosfont.h> |
BIOS font access | Text rendering on Dreamcast |
<dc/video.h> |
Video/framebuffer | vram_s[] pixel writes |
KOS_INIT_FLAGS |
Boot flags macro | KOS_INIT_FLAGS(INIT_DEFAULT) |
-lkos, -lm |
KOS kernel + math libraries | Link stage |
sh-elf-size |
ELF section size | Reports binary size after build |
sh-elf-readelf |
ELF header inspection | Validates entry point at 0x8c... (Dreamcast RAM) |
environ_base.sh |
KOS environment setup | Sourced by build script to set CC, CFLAGS, etc. |
| KOS linker script | shlelf.xc |
Required for correct Dreamcast memory layout |
D3-TUI does NOT need:
- makeip, scramble -- these are for IP.BIN creation (out of scope per BUILD_TARGET_POLICY.md)
- cdi4dc, mksdiso -- these are for CDI/ISO (out of scope)
- dc-chain -- cross-compiler builder (toolchain is prebuilt)
- KOS ports (kos-ports/) -- not used by current D3-TUI source
3. What Previously Broke
From REALIGNMENT_BRIEF (2026-05-10):
- Build-image repair loops: Agents spent cycles fixing CDI/ISO/IP.BIN concatenation instead of using the validated KOS ELF path.
- No canonical toolchain path: Without a fixed
KOS_BASE, every agent session re-discovered the toolchain, leading to inconsistent build flags. - Unanchored repo: Work happened in a shared filesystem workspace, not a Forgejo-cloned repo.
- Visual target drift: The W/A/R/P proof screen was not the D2 title menu target.
The realignment fix (repo anchoring + build target policy) resolved #3 and #4 but did not address #1 (toolchain path discoverability) for agents.
4. Current Build Target Policy
Active: RG40XXV/Knulli FlycastVL emulator validation
Accept: KOS ELF (fast validation), GDI/CHD (when FlycastVL requires disc image)
Reject: CDI, ISO, MIL-CD, IP.BIN concatenation, physical hardware assumptions
This policy is clear. The gap: agents don't know HOW to invoke the correct toolchain to produce the accepted artifacts.
5. The kos.mk Contract File
The existing kos.mk provides the compilation contract:
- SH-4 architecture flags: -ml -m4-single -mfsrra -mfsca
- Thread-local storage: -matomic-model=soft-gusa -ftls-model=local-exec
- Function/data sections: -ffunction-sections -fdata-sections and --gc-sections
- Include paths: $KOS_BASE/include, $KOS_BASE/kernel/arch/dreamcast/include, $KOS_BASE/addons/include
- Libraries: -lkos -lm
- Platform defines: -D__DREAMCAST__ -D_DREAMCAST -D_KOS
- Linker script: $KOS_BASE/utils/ldscripts/shlelf.xc
This file should be the single source of truth that agents consult for KOS compilation flags.
Fit For This Pi Workcell
Recommended Approach: Documented Command Contract
KOS should be exposed to agents as a documented command contract:
| Layer | What It Does | Where Agents Find It |
|---|---|---|
| Toolchain path | Fixed KOS_BASE | .env or Docker ENV, verified by smoke test |
| Build invocation | scripts/build-dreamcast.sh elf | Makefile make elf target |
| KOS capabilities | What KOS headers/libs provide | llm-wiki/wiki/toolchain/kos-contract.md (new) |
| Build validation | ELF entry point + size check | scripts/build-dreamcast.sh auto-validates |
| Smoke test | kos-cc --version succeeds | .pi/smoke/kos-health.sh (new) |
Should KOS Be In the Agent Image?
No -- mount it, don't bake it. The KOS toolchain (80MB) should be bind-mounted into the Docker container at /opt/toolchains/dc/kos. This keeps the Docker image small, allows toolchain updates without image rebuild, and fixes the build script's search path #3.
Recommended bind mount:
/mnt/kitchen/collab/d3-tui/toolchains/kos -> /opt/toolchains/dc/kos (inside container)
Then set KOS_BASE=/opt/toolchains/dc/kos in the container environment.
What Agents Should Know vs. Shouldn't Touch
Agents should:
- Know that kos-cc is the only supported compiler for Dreamcast targets
- Know to run make elf or scripts/build-dreamcast.sh elf for builds
- Know to validate ELF entry point via sh-elf-readelf -h
- Know that CDI/ISO/IP.BIN paths are out of scope
- Consult kos.mk for compilation flags (not guess)
- Consult kos-contract.md for KOS capabilities (not source-dive)
Agents should NOT:
- Try to install KOS themselves
- Modify KOS headers, libraries, or linker scripts
- Run makeip, scramble, or ISO tools
- Source-dive KOS internals to "fix" build issues
- Ship KOS binaries as D3-TUI deliverables
- Alter detect_kos() search paths without team lead approval
Integration With T0-T7 Lifecycle
| Stage | KOS Involvement |
|---|---|
| T0 Intake | None (assess scope) |
| T1 Research | Read kos.mk and kos-contract.md for capability reference |
| T2 Plan | State whether change needs KOS compilation or is host-only |
| T3 Dispatch | If KOS build needed, specify make elf or make flycast-image as deliverable |
| T4 Implement | Run make elf, validate ELF entry point |
| T5 Validate | sh-elf-readelf -h checks entry point; FlycastVL visual check on RG40XXV |
| T6 Review | Build manifest comparison; no CDI/ISO artifacts present |
| T7 Reflect | Document any new KOS findings in kos-contract.md |
Risks / Failure Modes
- Toolchain path rot: If the collab path changes or the toolchain is moved, builds break silently. Mitigation: smoke test on session start.
- Agent overreach: Agents may try to "fix" KOS when builds fail, editing linker scripts or recompiling KOS itself. Mitigation: explicit "do NOT touch" in contract doc.
- CDI creep: Without constant enforcement, agents drift toward CDI/ISO packaging because it "feels more complete." Mitigation: build script exits with error if CDI targets are requested; prompt explicitly forbids CDI paths.
- environ_dreamcast.sh missing from collab path: The archive has this file but the collab path doesn't. The build script handles this by sourcing
environ_base.shdirectly. Verify this works with the collab path. - Pi architecture mismatch: KOS toolchain is compiled for ARM64 (Raspberry Pi 4). If the Pi is replaced with x86 hardware, the toolchain must be rebuilt.
- FlycastVL dependency: KOS ELF validation depends on FlycastVL being installed on RG40XXV. If the RG is unavailable, agents have no visual validation path. Build-only validation (sh-elf-readelf) is a partial substitute.
- Asset extraction as prerequisite: The current build requires
make d2-assetsbeforemake elf. If asset extraction is blocked (current state per BUILD_STATUS.md), KOS compilation succeeds but the binary is incomplete.
Decision Needed From Mehdi
- Toolchain path: Should KOS_BASE be set to
/mnt/kitchen/collab/d3-tui/toolchains/kosand exported as a Docker ENV? (Recommended: yes -- this is the only working path, and it must be automated.) - Bind mount: Should the toolchain be bind-mounted into the Docker container at
/opt/toolchains/dc/kos? (Recommended: yes -- this fixes the build script's search path #3 without modifying the script.) - Smoke test: Should a
.pi/smoke/kos-health.shscript be created that verifieskos-cc --versionbefore agents can claim toolchain work? (Recommended: yes -- prevents agents from discovering the absent toolchain mid-task.) - Contract document: Should
llm-wiki/wiki/toolchain/kos-contract.mdbe the canonical "what KOS is good for" reference? (Recommended: yes -- this is the smallest useful document that prevents agent drift.)
Next Probe
The smallest follow-up check: on the Pi4, run:
export KOS_BASE=/mnt/kitchen/collab/d3-tui/toolchains/kos
export KOS_CC_BASE=$KOS_BASE
source $KOS_BASE/environ_base.sh
kos-cc --version
If this succeeds, the toolchain works and only path configuration is needed. If it fails, the toolchain itself may be incomplete or architecture-mismatched.