How-toSelected

DeepSeek Harness source shows 735 lines of loop code keep agents stable

Why we picked it

The real takeaway is that agent reliability is an engineering problem in the harness layer, so teams debugging flaky agents should audit their loop code before blaming or upgrading the model. Developers building skills or workflows get a concrete pattern: enforce checks before the model call and at teardown rather than relying on prompt instructions. The code is open source, making it a usable reference implementation alongside memory approaches like MemT 1.0 Pro.

A viral breakdown of DeepSeek Harness's open-source code argues that agent instability—dropped messages, mid-run freezes, runaway token spend—is usually caused by the outer loop code, not by the model getting dumber. The core file agent.ts spans about 735 lines and implements four control stages between a user pressing enter and the model's reply.

Body

First, incoming messages enter a queue, so rapid follow-up messages never clobber a running task; a handoff note keeps pending requests intact between rounds. Second, before any model call, messages, rules and tools pass through a plugin checkpoint that can block the call entirely, saving tokens. Third, while the model streams output, the code checks for a stop signal at each chunk, preserving partial content into a ledger instead of aborting abruptly. Fourth, at completion it asks plugins whether new work should continue, and always writes an end record to the ledger whether the run finished, errored or was stopped.

The author's practical point: when agents miss messages or ignore rules, the fix is usually a hard pre-call and post-run check in the harness, not repeated prompts or swapping models. The full 735-line source is public on GitHub for direct inspection.

Original · Lxfater

用 AI 干活老半路卡死、漏消息,很多人第一反应就是模型变笨了,天天追着换新模型

但翻开 DeepSeek Harness 的源码才看明白 真正决定它稳不稳的,是包在模型外面那 735 行循环代码

写个循环调接口,几十行不就完事了,对不对?

写个玩具确实够了 但真用起来,你刚发完一句又手快补一句,或者跑一半发现不对赶紧点停 玩具循环一碰这些直接抓瞎,上一条活给顶没了,点停了后台还在偷偷烧额度

DeepSeek 核心文件 agent.ts 里,从你敲回车到回完话 实打实卡了 4 道关(见图 1):

1. 进门先排队,防连发撞车 消息先存进排队区再开工,上一轮没完绝不硬开新任务 新消息先蹲排队区,收尾间隙还会留张接力纸条 等上一轮退场立刻接上,连发两条不打架也不丢单(见图 2)

2. 调模型前先过插件安检 调模型前先从排队区领消息,凑齐规矩和工具交给插件查 觉得不行直接挡门外,压根不调模型,一分钱额度都不白烧

3. 吐字时逐段盯急停 模型每吐一小段,代码就看一眼你有没有点停 真叫停了也不直接拔电源,稳稳停住把半截内容存进账本,省得下次聊直接断片

4. 收尾先问续不续跑,最后死保记完账 快结束时还会问插件要不要再塞新活接着跑 最后不管干完、报错还是被叫停,都会硬往账本补条结束记录,绝不留烂摊子!

不自己写框架的人,看懂这 4 道关有啥用?

以后 AI 老漏消息或写死规矩它照样不听,先别急着骂模型降智(见图 3) 配 Skill、写工作流也一样,在聊天框里靠嘴叮嘱十遍 真不如在调模型前和收尾时卡死一道硬检查管用

外层靠这种循环框架记账本防断片,底层靠昨晚聊的 MemT 1.0 Pro 把长记忆压进模型省算力(见下面原帖),Agent 想真正跑稳,这两头都得补

这 735 行源码全开源,想对照看细节的直接翻👇 https://t.co/SkEZevlnNg

Industry Coordinates & Source Verification

This report is categorized under AITOP's "How-to" desk, focusing on China's AI industry and domestic foundation model ecosystem. Within our cross-regional analytical framework, it directly maps to AI developer toolchains, coding agents, and engineering workflows. Key entities and technical topics under continuous tracking include DeepSeek, Agent, prompt engineering, MCP and tools.

Source attribution: curated from Lxfater (@lxfater).