From eefd5ee2f20475c3aa8454307d6d5629b2c13cf3 Mon Sep 17 00:00:00 2001 From: Autumn-27 <53844290+Autumn-27@users.noreply.github.com> Date: Wed, 9 Sep 2026 12:04:17 -0400 Subject: [PATCH] =?UTF-8?q?fix(digest):=20=E5=A4=9A=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E4=BD=93=E8=AF=84=E5=AE=A1=E4=BF=AE=E6=AD=A3=20=E2=80=94=20?= =?UTF-8?q?=E8=B5=84=E4=BA=A7=E7=B4=A2=E5=BC=95=E6=94=B6=E6=95=9B=20+=2060?= =?UTF-8?q?cap=20=E4=BF=9D=20hot=20+=20=E6=96=87=E6=A1=A3=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对照 cold-digest-spec 的对抗式评审(20 项发现)逐条核对实现,两处真实代码问题已修: - 资产索引膨胀(评审 §6.2 高危):早前把一个 digest 落进它成员命中的【每个】asset 桶, 跨资产 digest 会复制进几十个桶、顶层条目不降反升(真实图 asset_ids 约 144 个)。改为 每个 digest 只归一个【代表资产】桶(成员锚定众数、并列取最小 id;无锚定 → 0 号兜底桶), 条目数 ≤ digest 数。 - 60 兜底截断误砍活跃上下文(评审 §6.3 高危):hot(live 前沿祖先链 / live 意图下新事实) 是活跃推理上下文,绝不能截。改为 hot 全保、60-cap 只约束【冷但未折】残余;冷区两侧 均衡保留。graph_overview 每轮计算 render 期 hot 集(兼用于复活检查)。 其余 16 项评审发现经核对,本实现已由设计选择规避/处理:全量扫描维护(静默变冷/传递翻转 盖章)、签名含锚节点、minor+major 统一走同一 group()(§3.1 在两处都生效、major 不会拆散 同父组)、提交时活跃度复核(不折入复活成员)、content_version 列+状态变更 bump、渲染期索引 (无 KindIndex 陈旧)、cold_digests 与索引并列(body 仍在顶层)、singleton 只进 recent。 文档 §6.2 同步上述解决口径(body 归属、一 digest 一桶、singleton 归 recent)。 --- agent/tools.go | 57 ++++++++++++++++++++++++++++--------------- agent/tools_digest.go | 34 +++++++++++++++----------- 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/agent/tools.go b/agent/tools.go index 94754fe..ad80fcb 100644 --- a/agent/tools.go +++ b/agent/tools.go @@ -425,11 +425,13 @@ func (t *ToolSet) graphOverviewData() map[string]any { // only when it is covered AND still cold. covered_members (built from hidden) lets a // parents/yields id pointing into a live fold stay resolvable (§6.5 dangling lineage). covered, _ := t.ts.CoveredMembers() + // Hot set at render time serves two §6 needs: (1) a covered member that revived + // (now hot) must reappear this round; (2) the 60-cap must never truncate hot + // (active-context) nodes — only the cold-but-unfolded region is cappable (§6.3). + // Computed every round (cheap for real graph sizes); nil map degrades safely. var hotAtRender map[int64]bool - if len(covered) > 0 { - if cg, _, err := loadColdGraph(t.ts); err == nil { - hotAtRender = cg.hotSet() - } + if cg, _, err := loadColdGraph(t.ts); err == nil { + hotAtRender = cg.hotSet() } hidden := func(id int64) bool { _, c := covered[id]; return c && !hotAtRender[id] } fr, _ := t.ts.Frontier(100) @@ -448,7 +450,8 @@ func (t *ToolSet) graphOverviewData() map[string]any { } } out["running_intents"] = compactIntents(running, parentsOf, yieldsOf) - out["recent_done_intents"] = compactIntents(recentDone, parentsOf, yieldsOf) + // recent_done_intents is finalized further below (after facts are known) so the + // 60-cap can balance the two lists and protect hot nodes (§6.3). // done_intents_total:已结束意图(done/blocked/exhausted)总数,与 recent_done_intents // 平行命名——后者只是它的最新窗口(≤15)截断视图。两键并排即自描述:"看到的是 N/总数", // 让 planner 去重时别把"没显示"当成"没派过",无需在提示词里另行解释。 @@ -521,7 +524,9 @@ func (t *ToolSet) graphOverviewData() map[string]any { findingList = append(findingList, m) } out["finding_list"] = findingList - recentFacts := make([]map[string]any, 0, len(factNodes)) + // Build facts, split hot (active context — a fact under a live intent) from cold + // (not-yet-folded). Hidden (folded & still cold) ones are surfaced via cold_digests. + var recentFactsHot, recentFactsCold []map[string]any for _, n := range factNodes { if hidden(n.ID) { continue // in a cold_digest and still cold — surfaced via cold_digests (§6.2) @@ -538,22 +543,34 @@ func (t *ToolSet) graphOverviewData() map[string]any { m["confidence"] = c } } - recentFacts = append(recentFacts, m) - } - // §6.3 兜底截断:未折的 settled/cold 区(recent_done_intents + recent_facts)总量 > - // cap 时按最新优先截——live(open/running) 在独立字段、永不受影响。两边【均衡】保留: - // 较少的一侧全留,较多的一侧填剩余额度,各自至少保底 cap/2,绝不把某一侧饿到 0 - // (深探索链下 hot 的 settled 意图很多,天真的"意图优先"会把 facts 挤没)。正常由压缩 - // 阈值约束、不触发;触发即标注,说明压缩滞后。 - const unfoldedCap = 60 - if doneList, ok := out["recent_done_intents"].([]map[string]any); ok { - if keepDone, keepFacts, truncated := balancedCap(len(doneList), len(recentFacts), unfoldedCap); truncated { - out["recent_done_intents"] = doneList[:keepDone] - recentFacts = recentFacts[:keepFacts] - out["unfolded_truncated"] = true // 正常不触发;触发说明压缩滞后 + if hotAtRender[n.ID] { + recentFactsHot = append(recentFactsHot, m) + } else { + recentFactsCold = append(recentFactsCold, m) } } - out["recent_facts"] = recentFacts // 最近事实的 {id, summary, from_intent, confidence?},详情/证据用 node_detail(id) + // Split the settled intents the same way: hot = ancestor of a live intent (active + // context); cold = not-yet-folded settled. + var doneHot, doneCold []*db.Node + for _, n := range recentDone { + if hotAtRender[n.ID] { + doneHot = append(doneHot, n) + } else { + doneCold = append(doneCold, n) + } + } + // §6.3 兜底截断:hot(活跃探索上下文——live 前沿的祖先链、live 意图下的新事实)【永不】 + // 被截;60-cap 只约束【冷但未折】的残余(压缩滞后时才增长)。live(open/running) 另有字段、 + // 同样不受影响。冷区两侧均衡保留:较少一侧全留、较多一侧填余额,各自保底 cap/2,绝不饿到 0。 + const unfoldedCap = 60 + keepColdDone, keepColdFacts, truncated := balancedCap(len(doneCold), len(recentFactsCold), unfoldedCap) + if truncated { + out["unfolded_truncated"] = true // 正常不触发;触发说明压缩滞后 + } + out["recent_done_intents"] = append( + compactIntents(doneHot, parentsOf, yieldsOf), + compactIntents(doneCold[:keepColdDone], parentsOf, yieldsOf)...) + out["recent_facts"] = append(recentFactsHot, recentFactsCold[:keepColdFacts]...) // {id, summary, from_intent, confidence?};详情用 node_detail(id) // cold-digest §6.1/§6.2: the folded cold region + the per-asset directory that // collapses independent directions, plus the dangling-lineage resolver map. if cds, cidx := t.coldDigestOverview(); len(cds) > 0 { diff --git a/agent/tools_digest.go b/agent/tools_digest.go index ccf8611..e83e832 100644 --- a/agent/tools_digest.go +++ b/agent/tools_digest.go @@ -48,28 +48,34 @@ func (t *ToolSet) coldDigestOverview() (digests []map[string]any, index []map[st }) } - // index (§6.2): asset → digests touching it. A digest with no anchored asset - // falls into the 0 bucket ("(未锚定资产)"). One digest may list under >1 asset. + // index (§6.2): asset → digests. Each digest lands in EXACTLY ONE bucket — its + // representative asset = the asset anchored on the most of its members (mode; + // tie-break lowest id). This is what makes the index converge: bucketing a digest + // into every asset its members touch would duplicate it across dozens of buckets + // and blow up the top-level count instead of shrinking it (asset_ids are fine- + // grained — a real task has ~144 of them). A digest whose members anchor no asset + // falls into the 0 bucket ("(未锚定资产)"). byAsset := map[int64]map[int64]bool{} // asset id → set of digest ids assetSet := map[int64]bool{} for dID, ms := range memByDigest { - touched := map[int64]bool{} + counts := map[int64]int{} for _, m := range ms { for _, a := range assetsByNode[m] { - touched[a] = true + counts[a]++ } } - if len(touched) == 0 { - touched[0] = true + rep, best := int64(0), 0 + for a, c := range counts { + if c > best || (c == best && (rep == 0 || a < rep)) { + rep, best = a, c + } } - for a := range touched { - if byAsset[a] == nil { - byAsset[a] = map[int64]bool{} - } - byAsset[a][dID] = true - if a != 0 { - assetSet[a] = true - } + if byAsset[rep] == nil { + byAsset[rep] = map[int64]bool{} + } + byAsset[rep][dID] = true + if rep != 0 { + assetSet[rep] = true } } labels := map[int64]string{}