长期任务的上下文结构:为什么这样设计Context structure for long-running work: why it's built this way
这是「运行机制」那篇的前传:机制是怎么跑的,这里讲为什么要这么拆。案例是多渠道广告投放,方法适用于任何要跑几个月、每天有状态变化的 AI 任务。The prequel to "How the loop runs": that page shows the mechanics, this one explains the reasoning. The case is multi-channel paid media; the method applies to any AI-run task that lasts months and changes state daily.
01重构前的问题The problems before
通读了所有规则文件、操作记录和记忆之后,问题集中在八处。它们互相放大:规则越长越没人维护,越没人维护副本越多,副本越多漂移越快。After reading every rule file, log and memory, the problems clustered into eight. They compound: the longer the rules, the less they're maintained; the less maintained, the more copies; the more copies, the faster they drift.
| 问题Problem | 表现Symptom | 后果Cost |
|---|---|---|
| 规则文件混装四种寿命的内容Rule files mix four lifetimes | 流程、参考事实、研究叙事、案例全在一个文件,500 行级,每次会话全加载;已推翻的研究仍常驻Process, facts, research notes and cases in one 500-line file, loaded every session; superseded research still resident | 一次日常检查起手 ~50k token~50k tokens before any work |
| 只有流水没有快照Log without snapshot | 操作记录 300 行只追加;「现在管着什么」要从头读到尾脑内合并;待办散在各条末尾A 300-line append-only log; "what's live" must be reconstructed by reading all of it; follow-ups buried in entries | 每次重新理解状态;待办过期无人知State re-derived every time; overdue items unnoticed |
| 同一概念两种格式One concept, two formats | 一个渠道用 JSON 记操作,另一个用散文One channel logs in JSON, another in prose | 机器读不了一个,人读不动另一个Machines can't read one; people can't read the other |
| 同一规则 6 份以上副本6+ copies of the same rule | 口径、成熟条件、表格列定义散在根规则 / 任务规则 / 技能 / 记忆Metric definitions scattered across root rules, task rules, skills, memory | 已漂移:三套判定规则互斥、标的清单三个版本、12 处死链Already drifted: three contradictory rule sets, three target lists, 12 dead links |
| 记忆与任务目录双真相源Memory and task dirs both claim truth | 十几条记忆与任务规则讲同一件事,各自演进A dozen memories duplicate task rules and evolve separately | 索引里有已被翻案的结论The index carries overturned conclusions |
| 跨渠道结论困在各目录Cross-channel lessons stuck in silos | 两个渠道独立得出同一结论,没有一处把它们放一起Two channels reached the same conclusion independently; nowhere held both | 经验不复用Experience not reused |
| 缓存无清单Cache without manifest | 几十 MB 数据靠文件名日期识别Tens of MB identified by filename dates | 「复用缓存」靠猜"Reuse the cache" works by guessing |
| 动作→效果链路断Action→outcome link broken | 记录只记改动,效果只存在于当天对话The log records changes; outcomes exist only in that day's chat | 下次动同一旋钮不知道上次结果Next time the same knob moves, last time's result is unknown |
02几个渠道其实是同一个循环Several channels, one loop
把各渠道的优化流程摆在一起:标的在哪、主判据、输出的第一张表、动作协议——完全一样。真正因渠道而异的只有两处:前端数据怎么拉,以及能动哪些设置。Laid side by side, every channel's optimization loop is identical: where the targets live, the primary metric, the first table in the output, the action protocol. Only two things vary by channel: how platform-side data is pulled, and which settings can be changed.
| 环节Step | 跨渠道Across channels | 落到哪Where it lives |
|---|---|---|
| 标的 / 最近改动 / 待办Targets / last change / follow-ups | 同一件事,之前五种写法Same thing, previously five formats | state.json · todo.jsonl · ops.jsonl |
| 主判据(后端双口径)Primary metric (backend, two calibers) | 完全一样Identical | 一处真相源 + 一个脚本One source of truth + one script |
| 输出第一块:分日表First output block: daily table | 完全一样Identical | daily_review.py |
| 前端辅助数据Platform-side data | 渠道特有Channel-specific | adapteradapter |
| 能动的设置Available settings | 渠道特有Channel-specific | 各渠道规则文件Per-channel rule file |
| 动作协议Action protocol | 完全一样Identical | record.py |
03分层原则Layering principles
常驻的越少越好,按需的越全越好。每一层只回答一个问题。Keep what's always loaded tiny and what's on demand complete. Each layer answers one question.
L0 根规则(≤90 行) 铁律 + 指针,每次会话加载 L1 任务通用规则(≤60 行) 循环 + 四条保证 + 渠道入口 L1 渠道规则(≤90 行) 能动的设置、特有红线、知识页索引 L2 state / todo / outcomes hook 按需注入 ≤40 行 L3 wiki/ + 跨渠道结论 按需读;页头标 status: 已验证 | 部分推翻 | 存档 L4 ops.jsonl / 操作记录 流水,只在追因果时翻 L5 数据缓存 + 清单
- 快照与流水分离。状态覆盖写,流水只追加;日常只读状态,不读流水。Snapshot and log are separate. State is overwritten; the log is append-only; daily work reads state, not the log.
- 被推翻的结论不删,标状态。失败记录是资产,但要放在按需层,不能常驻。Overturned conclusions stay, labeled. Failures are assets — kept on the on-demand layer, never resident.
- 口径只有一处定义、一个实现。改口径改那一处,其余只引用;散文里不再出现公式。One definition and one implementation of every metric. Change it there; everything else references it; no formulas in prose.
- 规则文件超过 90 行就该拆。这条由脚本检查,不由人自觉。A rule file over 90 lines gets split. Enforced by script, not by discipline.
04用生命周期代替「任务」做组织单元Lifecycle, not "task", as the unit
长期跟的对象是一条条广告系列,不是「渠道」。它们有阶段,不同阶段该看的东西、该看的频率不一样。把阶段和下次检查日期写进快照,日常检查就从「固定看几条」变成「扫今天到期的」。What you actually track over months is individual campaigns, not "channels". They have stages, and each stage needs different checks at different cadences. Put stage and next-check date in the snapshot, and the daily check turns from "look at these fixed targets" into "scan whatever is due today".
| 阶段Stage | 频率Cadence | 看什么What to check |
|---|---|---|
| 创建Created | 一次Once | 配置读回:转化目标、设备、版位Config read-back: goal, device, placement |
| 冷启动(≤7 天)Cold start (≤7d) | 每日Daily | 吃量、版位分布、审核Spend ramp, placement mix, approvals |
| 稳态Steady | 每日Daily | 分日表 + 前端趋势Daily table + platform trends |
| 衰退Decay | 触发式Triggered | CTR 连降、成本抬头 → 换素材CTR falling, cost rising → new creative |
| 关停Stopped | 一次Once | 归因长尾(零消耗仍有转化)Attribution tail (conversions with zero spend) |
05参考了哪些方案,哪些是自己推的What was borrowed, what was derived
| 做法Practice | 来源Source | 对应Fit |
|---|---|---|
| 规则文件极短,细节按需加载Tiny rule files, details on demand | Anthropic · Skill best practices(progressive disclosure) (progressive disclosure) | 直接对应Direct |
| 快照 + 流水分离Snapshot + log split | Anthropic · Effective context engineering(structured note-taking); (structured note-taking); Cline Memory Bank | 思路一致,拆法借自事件溯源Same idea; split borrowed from event sourcing |
| 待办用 jsonl + 到期扫描Follow-ups as jsonl with due dates | Beads; Manus · todo.md recitation | 直接对应Direct |
| 知识页带状态头、推翻的保留、lintLabeled knowledge pages, kept failures, lint | Karpathy · LLM Wiki(raw / wiki / lint);Manus「把错误留在上下文里」 (raw / wiki / lint); Manus "keep the wrong stuff in" | 直接对应Direct |
| 表格代码化,AI 只做解释Tables in code, AI explains | Anthropic · just-in-time retrievalAnthropic · just-in-time retrieval | 思路一致Same idea |
| 内核 + 适配器;生命周期状态机;动作→效果闭环Kernel + adapters; lifecycle state machine; action→outcome loop | 自行推导。开源广告 agent 项目多止步于「适配器 + 审批门」Derived here. Open-source ad agents mostly stop at "adapter + approval gate" | 无现成模板No template |
06结果Result
| 前Before | 后After | |
|---|---|---|
| 一次检查起手上下文Context at the start of one check | ~50k | ~10k |
| 常驻规则Always-loaded rules | ~1,200 行~1,200 lines | 230 行230 lines |
| 口径副本Copies of metric rules | ≥6 | 1 + 代码code |
| 上次动作的效果Effect of the last action | 无None | 自动计算、自动注入Computed and injected |