WorldTUI:微调3.6亿参数模型模拟终端交互
WorldTUI: I trained a 360M model to pretend to be a terminal
世界模型工程实践干货,详细拆解了从数据合成到自我纠错的训练全流程,Agent开发者可参考其状态管理与纠错思路。
TL;DR: I fine-tuned a 360M-parameter language model to act as a terminal. There's no shell or filesystem behind it: every keypress goes in, and the model predicts how the screen changes. It can create files, run vim and tmux, play snake, and usually remembers what you wrote a few commands later. The biggest wins were teaching it to repair its own mistakes and decoding screen rows in parallel. Try it in your browser (208 MB) and type pythoon to see that it's not a real shell.
TL;DR:我微调了一个拥有3.6亿参数的语言模型,使其充当终端。它背后没有Shell或文件系统:每个按键输入后,模型预测屏幕的变化。它可以创建文件、运行vim和tmux、玩贪吃蛇游戏,并且通常能记住几条命令之前你写的内容。最大的突破在于教会它修复自身的错误以及并行解码屏幕行。在浏览器中尝试(208 MB),输入pythoon以查看它并非真正的Shell。
The demo is designed for a desktop browser with WebGPU and a physical keyboard. Chrome or Edge is the best place to try it. It should work on a modern phone, but painfully slowly.
该演示专为支持WebGPU和物理键盘的桌面浏览器设计。Chrome或Edge是最佳的尝试平台。它在现代手机上应该也能运行,但速度会非常缓慢。
When I first saw Genie 3 last year, I started imagining a future where applications aren't written in code but learned. Instead of a game engine rendering the scene, a model predicts the next most likely frame from the user's inputs.
去年我第一次看到Genie 3时,就开始想象一个应用程序不再通过代码编写而是通过学习获得未来的场景。不再是游戏引擎渲染场景,而是一个模型根据用户的输入预测最可能的下一帧画面。
Naturally I wanted to train my own world model. But I don't have a massive GPU cluster or a pile of video data, so I scaled down my ambition: instead of a game world, a terminal.
自然我也想训练自己的世界模型。但我没有庞大的GPU集群或大量的视频数据,所以我降低了野心:不是游戏世界,而是终端。
A terminal turned out to be a good fit. It's just a grid of characters rather than pixels, and I can generate as much perfectly labelled training data as I want by running real shells. A pretrained language model also already has a pretty good idea of what ls or cat should print.
事实证明,终端是一个很好的适配对象。它只是字符网格而非像素,而且我可以通过运行真实的Shell生成任意数量的完美标注的训练数据。预训练的语言模型也已经对ls或cat应该打印什么有了相当好的理解。
What I really wanted to know was whether it could maintain a consistent world, or just generate convincing terminal output.
我真正想知道的是,它能否维持一个一致的世界,还是仅仅生成令人信服的终端输出。
The final model is based on Qwen2.5-Coder-0.5B, fine-tuned on terminal sessions recorded from real programs running inside Docker. Jump to example videos
最终模型基于Qwen2.5-Coder-0.5B,并在Docker内部运行的真实程序所记录的终端会话上进行了微调。跳转至示例视频
How it sees a terminal
它是如何观察终端的
Standard LLMs take a prompt and then append tokens to the end, one at a time. That doesn't map well to a terminal. A single keypress can do something on its own, like esc in vim, and output doesn't always go to the bottom: clear wipes the screen and sends the cursor back to the top. So instead of predicting a stream of text, I need to predict changes to the screen.
标准的LLM接收提示词,然后一次一个地追加令牌。这并不很好地映射到终端上。单个按键可以独立执行操作,例如vim中的esc键,而且输出并不总是出现在底部:clear会清除屏幕并将光标移回顶部。因此,与其预测文本流,不如预测屏幕的变化。
I treat the terminal as a grid of characters, one per cell.1 The model doesn't see the grid directly, but a sequence of tokens describing its contents and the changes made to it.
我将终端视为字符网格,每个单元格一个字符。¹ 模型不直接看到网格,而是看到描述其内容及其所做更改的令牌序列。
A session
一个会话
Every session starts with a snapshot: the screen size, its contents and where the cursor is.
每个会话都以快照开始:屏幕大小、其内容以及光标位置。
The size is part of the input, not fixed at 32×96. In the demo, you can set the rows and columns when creating a custom starting screen.
大小是输入的一部分,并非固定为32×96。在演示中,您可以在创建自定义起始屏幕时设置行数和列数。
BOS SCREEN_START 32 96
ROW_0 0 'simedw@laptop:~$ '
CURSOR 0 17 1
SCREEN_ENDThis also lets me start partway through an application. Give it a bash prompt, vim buffer or a game of snake, and the model tries to continue from there. (It does better from bash.)
这也让我能够从一个应用程序的中间部分开始。给它一个 bash 提示符、vim 缓冲区或贪吃蛇游戏,模型会尝试从那里继续。(它在 bash 下表现更好。)
After that, each keypress goes in as the raw bytes a real terminal would send. Letters are themselves, Enter is \r, Escape is 0x1b and ← is ESC [ D and so on.
之后,每次按键都作为真实终端发送的原始字节输入。字母本身保持不变,Enter 是 \r,Escape 是 0x1b,← 是 ESC [ D,依此类推。
INPUT_START 'l' INPUT_ENDThe model answers with which rows to rewrite and where the cursor ends up:
模型回答需要重写哪些行以及光标最终停留在哪里:
OUTPUT_START
ROW_0 17 'l'
CURSOR 0 18 1
OUTPUT_ENDIn words: on row 0, starting at column 17, write l, then move the cursor to row 0, column 18. The last 1 means the cursor is visible, since full-screen apps sometimes hide it.
用文字描述:在第 0 行,从第 17 列开始写入 l,然后将光标移动到第 0 行、第 18 列。最后的 1 表示光标可见,因为全屏应用有时会隐藏它。
Nothing gets cleared first. New characters are simply drawn over the old ones.
首先不会清除任何内容。新字符只是简单地绘制在旧字符之上。
I didn't need new tokens for characters. Qwen already has tokens for all 256 byte values, which I reuse for characters, coordinates and other small numbers. So a coordinate like 17 takes one token, not two.
我不需要为字符创建新的 token。Qwen 已经拥有所有 256 个字节值的 token,我将其复用于字符、坐标和其他小数字。因此像 17 这样的坐标只需要一个 token,而不是两个。
Time is an input too. If nothing is pressed, the model gets a WAIT token with a duration instead of a key, which is how snake keeps moving on its own.
时间也是一个输入。如果没有按下任何键,模型会收到一个带有持续时间的 WAIT token 而不是按键,这就是贪吃蛇能够自行移动的原因。
Copying rows
复制行
When you reach the bottom of the terminal window and press enter, every line has to move up by one. I could just ask the model to output every row again in its new place, but that's around 3,000 tokens for a single keypress.
当你到达终端窗口底部并按下回车时,每一行都必须向上移动一行。我可以要求模型再次输出每一行在其新位置,但这对于单次按键来说大约需要 3,000 个 token。
So I added a COPY operation instead:
所以我添加了一个 COPY 操作:
ROW_0 COPY 1
ROW_1 COPY 2
ROW_2 COPY 3
...Each operation copies a row from the previous screen. In this example, the old row 1 becomes row 0, row 2 becomes row 1, and so on.
每个操作都会从上一屏复制一行。在这个例子中,旧的第 1 行变成第 0 行,第 2 行变成第 1 行,依此类推。
A scroll now costs three tokens per row instead of roughly 96.
现在滚动每行只需三个 token,而不是大约 96 个。
The same trick covers clearing the screen, inserting and deleting lines in vim, and navigating man pages. You can also copy a row and then overwrite part of it.
同样的技巧也适用于清屏、在 vim 中插入和删除行以及浏览 man 页面。你也可以复制一行然后覆盖其中的一部分。
Altogether, the model uses a tiny vocabulary of 333 tokens: 256 byte tokens, 64 row tokens and a handful of special operations. Decoding is grammar-constrained, so every prediction is a valid screen update.
总的来说,模型使用了一个只有 333 个 token 的小型词汇表:256 个字节 token、64 个行 token 和一些特殊操作。解码受语法约束,因此每个预测都是有效的屏幕更新。
That tiny vocabulary comes back later, when I squeeze the model into a browser.
这个小型词汇表稍后会在我将模型压缩到浏览器中时派上用场。
Generating the training data
生成训练数据
I initially looked for existing datasets of terminal recordings, including asciinema, but couldn't find one with the precise keystroke-to-screen alignment I needed.
我最初寻找现有的终端录制数据集,包括 asciinema,但找不到具有我所需要的精确按键到屏幕对齐的数据集。
Luckily it wasn't too hard to create my own.
幸运的是,创建自己的数据集并不难。
The basic idea was to generate believable terminal sessions by running real commands inside Docker containers, recording every keypress and the screen after each one.
基本思路是通过在 Docker 容器中运行真实命令来生成逼真的终端会话,记录每次按键及其后的屏幕状态。
The synthetic recipe was:
合成配方如下:
- Randomize usernames, hostnames, prompt styles, terminal sizes.
- Randomize directories and files based on a fictional persona (developer, sysadmin, etc.).
- Pick 6–24 weighted activities (modify files, vim, python, tmux, etc.).
- Track state, so sessions mostly open files that exist.
- Type like a person: one keypress at a time, with tab completion etc. I set the typo rate to 1.2%, which is generous to me.
- 35% of sessions start partway through, forcing the model to learn to work from any state.
- 随机化用户名、主机名、提示符样式和终端大小。
- 基于虚构的人物角色(如开发者、系统管理员等)对目录和文件进行随机化。
- 选择 6–24 项加权活动(修改文件、vim、python、tmux 等)。
- 跟踪状态,因此会话中打开的文件大多真实存在。
- 像人一样打字:一次敲击一个键,并包含 Tab 补全等功能。我将错字率设置为 1.2%,这对我相当宽容。
- 35% 的会话从中途开始,迫使模型学会从任意状态开展工作。
The terminal size was mostly 32×96, with some 24×80 and 40×120, so the model doesn't overfit to one size.
终端尺寸主要为 32×96,部分为 24×80 和 40×120,因此模型不会过度拟合单一尺寸。
I kept expanding the dataset as the model became more capable, and finally landed on 336k episodes, which corresponds to roughly 2B tokens. For comparison, the base model saw around 18T tokens during training.
随着模型能力的提升,我不断扩充数据集,最终确定了 336k 个 episode,对应大约 2B tokens。作为对比,基础模型在训练期间看到了约 18T tokens。
Training
训练
I used Qwen2.5-Coder-0.5B as the starting point. I needed a small model to get close to realtime performance. Being a coding model, it had also already seen plenty of CLI usage.
我以 Qwen2.5-Coder-0.5B 作为起点。我需要一个小型模型以实现接近实时的性能。由于它是一个代码模型,它此前已经见过大量的 CLI 使用场景。
I started off by doing straightforward supervised fine-tuning: give a screen history and a keypress, predict the screen delta produced by the real terminal.
起初,我进行了直接的监督微调:提供屏幕历史和按键,预测由真实终端产生的屏幕变化量。
Ordinary typing was learned quickly: a letter appears, backspace removes it, the cursor moves.
普通打字被快速掌握:出现一个字母,退格键将其删除,光标移动。
In longer sessions, though, errors started to compound: a mistake in a filename propagated through the rest of the session.
然而,在较长的会话中,错误开始累积:文件名中的一个错误会蔓延至整个会话的其余部分。
Learning from its own mistakes
从自身的错误中学习
To address this I took a page from DAgger, where a model learns from the states it visits rather than just the ideal demonstrations.2
为了解决这个问题,我借鉴了 DAgger 的做法,让模型从其访问的状态中学习,而不仅仅是理想演示。2
For each training example:
对于每个训练示例:
- Pick a point in a recorded session, usually close to the command that produces the output.
- Let the model predict the next 1-4 events.
- Compare the resulting screen with the real recorded screen.
- If the difference is small enough, construct a correction that repairs the model's screen on the next keypress.
- Continue training on the corrected frame and a few subsequent real events.
- 在已记录的会话中选择一个点,通常靠近产生输出的命令。
- 让模型预测接下来的 1-4 个事件。
- 将生成的屏幕与真实记录的屏幕进行比较。
- 如果差异足够小,则构建一个修正方案,以便在下一个按键时修复模型的屏幕显示。
- 继续在修正后的帧及其后几个真实事件上进行训练。
The delta takes the model's incorrect screen to the correct next screen. In essence, it learns to "repair" its own mistakes.
变化量(delta)将模型错误的屏幕引导至正确的下一个屏幕。本质上,它学会了“修复”自己的错误。
There's one important restriction: the model must have enough information to infer the correction. Any words introduced by a correction must have appeared on an earlier real screen or in the user's typing, not just in the model's own predictions.
这里有一个重要的限制:模型必须拥有足够的信息来推断出修正方案。任何由修正引入的单词必须出现在之前的真实屏幕或用户的输入中,而不能仅存在于模型自身的预测中。
For example, if the model misspells a filename that appeared earlier, I can teach it to fix that. But if it lists a filename it has never seen, I can't expect it to get it right.
例如,如果模型拼写了一个之前出现过的文件名错误,我可以教它修正这个错误。但如果它列出了一个从未见过的文件名,我就不能指望它能正确识别。
Training on those examples would actually reward hallucinations, which I really don't need more of.
在这些样本上进行训练实际上会奖励幻觉,而我真的不需要更多这样的东西。
About a third of the sampled sessions produced useful correction examples. I repeated this in three rounds, each time collecting mistakes from the latest model.
大约三分之一的采样会话产生了有用的纠正样本。我在三轮中重复了这一过程,每次从最新的模型中收集错误。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力