Files
R8Forge/AGENTS.md
T
starcells 86a54aa23c Fix the ROT-8 interpreters: biz / rnz test the low bit
Four interpreters here (tests/synth_test.cc, src/derive.cc and the two I added
earlier: tests/tape_run.cc, tests/byte_add_test.cc) implemented '[' and ']' as
a Brainfuck style "is this whole cell zero" test.  The reference implementation
does not:

    ROT-8-viz src/rot8.c
    rot8_biz: if ((*mem & 1) == 0) skip to just past the matching ']'
    rot8_rnz: if ((*mem & 1) == 1) jump back to the matching '['

That one difference is why every byte level program in the library looked like
an endless loop.  It is invisible in the bit level world -- for a cell holding 0
or 1 the low bit test and the zero test agree -- which is why the synth tables,
asm/test.r8asm and full_add never noticed anything.

Cross checked against the reference before touching anything: the tape our
assembler produces for "A = 128, set_0, B = 3, raw_add" prints 128 3 0 131 0 on
ROT-8-viz/build/rot8_test.exe and hung in our derive.

With the fix, measured:

    raw_add   65536/65536 pairs ok  1647 chars
    raw_and   65536/65536            639
    raw_or    65536/65536            575
    raw_xor   65536/65536            463
    raw_inc   512/512               1214
    asm/fibonacci.r8asm prints 1 1 2 3 5 8 13 21 34 55, exactly what its
    comment claims

So the byte level layer was never broken.  Two commits ago it was declared dead
-- set_0 "only converges on 0 and 1", the bits of a packed byte "unreadable",
raw_add "only survives 0+0" -- entirely on the strength of the broken
interpreters.  AGENTS.md and README.md now say the opposite and keep the wrong
diagnosis as a worked example: when byte level code looks hung, check the
interpreter first.

New:
  tests/byte_op_test.cc + drv/raw_{add,and,or,xor,inc}_byte.r8asm
      exhaustive runtime proof for the byte level library, 0.5 s
  tests/tape_run.cc + r8forge_tape_run
      a second interpreter that runs a .rot8 file directly -- cross checking
      derive is now one command

verify_set_0.r8asm now demonstrates what set_0 really does instead of claiming
it hangs: 255 -> 254, 5 -> 4, 128 -> 128 (it clears the low bit, not the cell).

The one bit per cell layer (byte_add / pack_byte) stays as a documented
alternative: it is correct under the real semantics too, just redundant.
2026-11-07 00:01:25 +08:00

9.3 KiB
Raw Blame History

给接手的 agent 的话

这个文件是给下一个会话(或者下一个 agent)看的交接说明。内容都是这个仓库里实测过的结论, 不是推测;每条后面基本都跟着一次失败的尝试,别再踩。

工作方式(上一轮踩到的)

  • 超过 ~30 秒的命令一律丢后台(run_in_background),前台只做短轮询。 一次搜索可以跑 680 秒、缓存预热 885 秒,前台干等会撞上客户端和 API 的传输超时。

  • 别让大段 stderr 进上下文。PowerShell 遇到原生命令出错会吐一整套 NativeCommandError 堆栈;用 2>$null 收干净,判断结果靠 $LASTEXITCODE 和 stdout。

  • 写含中文的文件用编辑工具(UTF-8),不要用 Set-Content -Encoding ascii —— 汉字会变成 ?(examples/full_add_flat*.r8asm 就这么坏过一次)。

  • 构建要用 VS 自带的 cmake:build/ 是 Visual Studio 18 2026 生成器配的, 直接敲 PATH 上的 scoop cmake(4.4)会因为版本不一致重新配置,然后死在 No preprocessor test for "PellesC"。用这个:

    "C:/Program Files/Microsoft Visual Studio/18/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe" \
        --build build --config Release
    
  • 会话很长时传输容易失败,重要结论要落到 commit message 和文件注释里,别只留在对话里。

  • 写结论之前先对虚拟机:这个仓库最贵的一次事故就是虚拟机语义写错,然后在错的东西上 写了一整套"实测结论"(见「字节级运算」)。字节级结论必须能被 ROT-8-viz (C:\Users\peng0\git\ROT-8-viz,权威实现)复现。

构建与自检

# MSVC Release
cmake -S . -B build -G "Visual Studio 18 2026" -A x64
cmake --build build --config Release

# clang-cl(scoop 的 llvm,比 MSVC 严格,能抓出模板推导问题)
clang-cl /nologo /std:c++20 /EHsc /O2 /I include src/r8asm.cc src/synth.cc tests/asm_test.cc /Fe:r8forge.exe

自检:

r8forge asm/test.r8asm                            # 应 exit 0,1666 字符,7/7 命中缓存
r8forge_synth_test examples/full_add.r8asm        # all eight combinations ok
r8forge verify_raw_add_nc.r8asm                   # 4 cell 加法切片驱动
r8forge_byte_op_test                              # 字节级库:65536×4 + 512 全过,0.5 秒
r8forge_byte_add_test                             # 一位一 cell 的备选层:65536 + 256 全过

r8forge_byte_op_test 是唯一按返回值判断字节级对错的;前三个只证明"汇编得出来"或 "位级正确"。

r8forge_derive 是验证工具:

r8forge_derive <file.r8asm> 4 0 2         # 按输入组合跑,打印 stat
r8forge_derive <file.r8asm> -1 0=100 1=50 # 指定 cell 初值跑一遍,打印 out
r8forge_derive <file.r8asm> -2 0=100      # 同上,另外倒出所有非零 cell

注意 -1/-2 是拿汇编源码跑的(内部会先 import_file),不能直接喂 .rot8 纸带。 r8forge_tape_run <file.rot8> [addr=value...] 是第二个实现,直接喂纸带,用来交叉验证 derive(两个实现同时写错的可能性比一个低)。

指令集语义(这些搞错会浪费一整轮)

'-'  flb  翻转最低位        cells[dp] ^= 1
'+'  ror  右旋整个 cell
'['  biz  最低位为 0 -> 跳到匹配的 ']' 之后
']'  rnz  最低位为 1 -> 跳回匹配的 '['

最后两条的关键在"最低位"三个字:biz/rnz 判的是 cell & 1,不是"整格是否为零"。 权威实现是 ROT-8-viz/src/rot8.c:

void rot8_biz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 0) { ... } }
void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } }

由此推出两条关键约束:

  1. set_0 = [-] 清的是最低位,不是整个 cell。 set_0 把 255 变成 254、把 128 原样留着、把 5 变成 4。要清整个字节就 %rep _CELL_SIZE { set_0; ror }(erase 就是这么写的)。一个字节的位能读: ror 若干次把目标位转到最低位,再 biz。
  2. 合成的程序只碰每个 cell 的低 K 位(K = 参与运算的输入位数),高位不追踪。 所以 %synth 出来的块只适用于"每格一位"的世界。

曾经这里还写过第三条("多字节 cell 的位读不出来")—— 那是把 biz 当成 Brainfuck 的 整格零判断推出来的,错的,已删。

字节级运算(一个 cell 一个字节)

结论:库里的字节级宏是对的,穷举全过。r8forge_byte_op_test 实测(0.5 秒):

宏 驱动 纸带 结果
raw_add(a+b,含进位) drv/raw_add_byte.r8asm 1647 字符 65536/65536
raw_and drv/raw_and_byte.r8asm 639 字符 65536/65536
raw_or drv/raw_or_byte.r8asm 575 字符 65536/65536
raw_xor drv/raw_xor_byte.r8asm 463 字符 65536/65536
raw_inc(a+进位) drv/raw_inc_byte.r8asm 1214 字符 512/512
asm/fibonacci.r8asm — 8761 字符 跑出 1 1 2 3 5 8 13 21 34 55,和文件注释一致

顺带纠正一个流传下来的说法:raw_add 不吃掉操作数。实测 5+3 → A=5 B=3 S=8, 255+255 → A=255 B=255 S=254 Co=1。asm/fibonacci.r8asm 里"每圈都要重新复印"的注释 是过度保守(它确实复印了,但不复印也对)。

这次差点被写进文档的错误诊断(别再犯)

有一轮里,raw_add / raw_and / copy / fibonacci 全被判定"运行时是坏的", 理由是 set_0 对多字节 cell 不收敛、纸带跑到步数上限。那是虚拟机写错了: 仓库里有三份解释器(tests/synth_test.cc、src/derive.cc、tests/tape_run.cc) 把 biz/rnz 实现成了"整格是否为零"(Brainfuck 直觉),于是每个字节级程序 都变成死循环。改回 & 1 之后全部通过。

为什么之前没被发现:位级程序在两种语义下行为完全一样(0/1 的最低位就是它自己), %synth 的表、asm/test.r8asm、full_add 全都不受影响;只有字节级驱动能区分。 所以以后凡是"字节级看起来跑不动",第一个怀疑对象是虚拟机,不是库。

验证方法:拿 ROT-8-viz/build/rot8_test.exe 跑同一条纸带对答案。 tests/tape_run.cc 现在是正确语义(& 1),可以直接对。

另一位一 cell 的实现(备选,不是主线)

asm/byte_add.r8asm + asm/pack_byte.r8asm:每 bit 一个 cell(32 cell), byte_add 把 A 当累加器(A += B,可以连着加),pack_byte 把八个 flag 压回一个字节。 在正确语义下也是对的(r8forge_byte_add_test:65536 + 256 全过,1308 / 3142 字符), 但和 raw_add 功能重复、cell 多得多。留着当备选。

%synth 相关

  • 语法:params N + mode tape|layers + metric chars|flat|blocks + layout ...(可选)
    • 目标表 stat ... 或 map ...。stat 的项数必须是 2^params(3 参数要 8 项)。 参数是输入位 %1..%K、输出位、剩余草稿位(草稿位必须结束为 0,语义见 compress_stat_space)。
  • metric flat:不计 .tp 行走代价 → 结果与 cell 布局无关、缓存可跨调用点复用。 metric chars 才是按实际布局最优。库里的块统一用 flat。
  • 缓存:.r8forge_cache/(已入库,克隆下来直接命中)。键 = 模型版本 + params + mode + metric + 解析后的整张目标表(+ layout,仅 chars)。改指令模型时要 bump kModelVersion(src/synth.cc)。
  • 缓存代价参考:bit_raw_add 的 flat 搜索 990,636,137 状态 / 680 秒 / ~22 GB 内存。 层数上限现在是 8(原来是 5,5 会把一些可行的表误判成不可能)。

已经验证可行的东西

东西 结论
库里的 7 个位运算块(and/or/xor/inc/add) 合成版比手写短:asm/test.r8asm 1714 → 1666 字符
bit_raw_add_nc(4 cell 忽略进位切片) flat 代价 27,完整全加器切片是 38;8/8 组合正确
bit_raw_add(6 cell 全加器切片) 保留 A、B;字节级 raw_add 1647 字符,65536 组合全对
raw_and / raw_or / raw_xor / raw_inc 639 / 575 / 463 / 1214 字符,各 65536(inc 512)全对
asm/fibonacci.r8asm 8761 字符,跑出 10 项斐波那契,和注释一致
byte_add + pack_byte(一位一 cell) 备选层:32 cell,1308 字符,65536 组合全对

已经确认走不通的(别重试)

  • 3 个 cell 做加法切片:层数上限 5 和 8 都报 no program up to N layers。
  • 让 %synth 合成的块自己保留操作数(没有草稿 cell):轮转要求 A、B 保留, 而合成的程序只会吃掉操作数;把表改成"保留 A、B"后同样报 no program up to 8 layers。 只有 6 cell 的 bit_raw_add(有草稿 cell)能做到。
  • 字节级(2^16 项真值表)交给 %synth:搜索空间不可行。

命名与注释格式(asm/lib.r8asm 的约定)

;   Macro bit_raw_add_nc
;   %1: CellBit A
;   %2: CellBit B
  • 暴露 cell 作为参数的宏,名字里要有 raw;块级别的宏(Cell 参数)不带。
  • %synth 块名 = <宏名>_synth,包装的 %macro 用不带 _synth 的名字。
  • 宏里的注释沿用上面那种 ; Macro / ; %N: <类型> <名字>。