Learning / 长期任务的上下文结构Context structure for long-running work

长期任务的上下文结构:为什么这样设计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 formatsstate.json · todo.jsonl · ops.jsonl
主判据(后端双口径)Primary metric (backend, two calibers)完全一样Identical一处真相源 + 一个脚本One source of truth + one script
输出第一块:分日表First output block: daily table完全一样Identicaldaily_review.py
前端辅助数据Platform-side data渠道特有Channel-specificadapteradapter
能动的设置Available settings渠道特有Channel-specific各渠道规则文件Per-channel rule file
动作协议Action protocol完全一样Identicalrecord.py
结论:渠道无关的内核 + 渠道适配器。之前的目录按渠道切,等于把内核抄了五遍。Conclusion: a channel-agnostic kernel plus per-channel adapters. The old directory split by channel — five copies of the kernel.

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  数据缓存 + 清单

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触发式TriggeredCTR 连降、成本抬头 → 换素材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 demandAnthropic · Skill best practices(progressive disclosure) (progressive disclosure)直接对应Direct
快照 + 流水分离Snapshot + log splitAnthropic · 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 datesBeads; Manus · todo.md recitation直接对应Direct
知识页带状态头、推翻的保留、lintLabeled knowledge pages, kept failures, lintKarpathy · LLM Wiki(raw / wiki / lint);Manus「把错误留在上下文里」 (raw / wiki / lint); Manus "keep the wrong stuff in"直接对应Direct
表格代码化,AI 只做解释Tables in code, AI explainsAnthropic · 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

BeforeAfter
一次检查起手上下文Context at the start of one check~50k~10k
常驻规则Always-loaded rules~1,200 行~1,200 lines230 行230 lines
口径副本Copies of metric rules≥61 + 代码code
上次动作的效果Effect of the last actionNone自动计算、自动注入Computed and injected
机制层面的实现见「运行机制」。For the mechanics, see "How the loop runs".