Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag

### Changes

- **图片消息 id 按载荷寻址(#3751)**:图片 core 的身份种子增加媒体指纹槽——数据图取其字节的 sha256(URL 引用图取 URL 字符串),一张图变化只重铸它自己那条 core,不再把全族 `_N` 序号整体位移(该位移曾是每轮断缓存与 #3748 写放大事故的喂数机)。`_N` 序号回归本职:仅区分字节完全相同的重复图。文本/工具消息种子不变,id 逐字节保持;跨协议同图同 id、responses legacy 单图形状与拆分形状同 id(金桩钉死)。存量图片会话一次性 re-key,由 #3641 判决分类与 #3756 快照渲染兜底吸收。

- **Fork 子会话不再因前缀漂移永久 409(#3741,owner 决策:门降 advisory)**:dsh/客户端 fork 出的子会话,其首个请求如果与继承的有序前缀不匹配(升级换代重排消息 id、宿主改写 body 等),过去直接 409 且被拒请求不计数——重试永远撞同一扇门,子会话确定性卡死、无任何自愈或清除路径。现在改为**照常收养**:session-id 绑定本来就是真正的唤醒路由,折叠覆盖差异由 reconcile(#3641 order-over-hash)兜底修复;漂移首请求付出一次性未折叠 token 峰,正确性无损。每次收养打一条带分类的 warn(reason 沿用 #3743 诊断:id-drift / hash-drift-under-same-id / prefix-longer-than-incoming 等),`publicForkPrefix` 在漂移时保持 false,side-lane 降级启发照常发言。测试:三种漂移形态全部 200+转发+父会话不动+恰一条 warn(one-shot:首请求后 stats.requests>0 绕过检查)。
- **快照渲染:proxy 车道的出站前缀改由服务端正史渲染,存量会话对内核升级免疫(#3756,根治 #3752/#3751)。** 折叠覆盖此前锚定在"客户端本轮重发的字节"上——消息 id 由内容哈希现算,身份函数一变(协议切换 #3640、#3642 的一次性 re-key、dsh 图片 offload #3588、宿主自查点)覆盖名单就与历史失联:轻则整段历史因 MAX_ANCHORS=16384 封顶失锚(#3752),重则图片族 `_N` 后缀全体位移重铸、每轮 fold 断缓存(#3751)。新引擎 `src/server/snapshot-render.ts`(灰度开关 `BILI_ROLLOUT_SNAPSHOT_RENDER`,默认 0%):当会话有活跃折叠块且统一存储可读时,prepare 层的出站消息序列 = 服务端快照行前缀(覆盖区由块的 seq/signature 覆盖集自拥抑制 + `acp_summary_*` 锚原位重渲,身份跟随块而非客户端字节)+ 客户端尾巴(分歧点 join 之上逐字保留,rewind 裁剪不复活)。链回溯用 id 快路径 + 内容签名兜底(#3658 canonical 种子同构,升级后同内容仍可链上);任何环节失步整轮降级回 legacy 路径,零行为面风险。验收:身份函数原地翻盐后出站前缀逐字节不变(老病当场钉死)、协议中途切换会话不分叉、客户端任意删/checkpoint 覆盖区不复活(一轮采用滞后,模块头有注释)、稳态轮轮前缀字节稳。观测闩 `metadata.snapshotRenderTurns`;kernel 加 `__setMessageIdSaltForTests` 测试缝(kernel 0.0.134,仅测试可达,生产零调用)。

Expand Down
21 changes: 21 additions & 0 deletions MESSAGE-IDENTITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ the lanes where D is off, unavailable, or degraded — and the observation
channel (`reconcileFoldCoverage`) still watches the CLIENT view (render and
observation are deliberately separate inputs).

## Images are payload-addressed, not family-numbered (#3751)

The `_N` instance discipline above assumes the base id already separates
*which content* this is. Images broke that assumption until #3751: every
image core carried the same canonical text (`[image]`), so all images in a
conversation hashed to ONE shared base and the `_N` ordinals did all the
separating — positionally. One image changing (offload placeholder, re-encode,
delete) shifted every sibling's ordinal, re-minting the whole family on every
turn (the #3748 write-amplification feeder).

The fix keeps the doctrine and closes the gap: the canonical seed grows a
sixth slot, but only for media cores — `hash(imageBase64)` for data images
(the bytes both codecs and the content CAS already hold), the URL string for
URL-referenced images (no bytes on the wire — the URL is the only identity
available), and the ordered join of part fingerprints for the rare legacy
plural-sidecar core. Byte-identical images sharing a base id is intended:
`_N` stays as the ordinal for *true duplicates* (the #459/#463 instance
discipline, applied to what it was designed for). One image changing now
re-mints only its own core. Text cores never gain the slot, so non-image ids
stay byte-identical across the upgrade.

## Known cost of the hash axiom — and how it was fixed

Same bytes ⇒ same identity has a cost face: #1476 — a user re-sends a short
Expand Down
17 changes: 17 additions & 0 deletions MESSAGE-IDENTITY.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ bili 的应对遵循上文的失败模式公理:**不猜** old→new 配对(猜

#3641 的 reconcile/判决层全部保留:它们仍是 D 关闭/不可用/降级时的安全网;观测通道(`reconcileFoldCoverage`)继续观察**客户端**视图(渲染与观测刻意分离)。

## 图片按载荷寻址,不再按家族编号(#3751)

上面的 `_N` 实例纪律有一个前提:base id 本身已经区分了「这是哪份内容」。
图片在 #3751 之前打破了这个前提——每条图片 core 的规范文本都是同一个
`[image]` 标记,一个会话里所有图片哈希出**同一个共享 base**,全靠 `_N`
序号做区分——而序号是**位置敏感**的。一张图变化(offload 占位、重编码、
删除)会把全族序号整体位移,每轮把整个家族的 id 重铸一遍(正是 #3748
写放大事故的喂数机)。

修法保持教义、补上缺口:规范种子增加第六个槽位,但**只有媒体 core 才有**
——数据图取 `hash(imageBase64)`(字节在 codec 与内容 CAS 两边都已经有),
URL 图取 URL 字符串(wire 上没有字节,URL 是唯一可用的身份),罕见的
legacy 复数 sidecar core 取各部件指纹的有序串。字节相同的图片共享 base
id 是有意的:`_N` 保留给**真正的重复图**(回到 #459/#463 实例纪律的本职)。
一张图变化现在只重铸它自己那条 core。文本 core 永不加该槽位,非图片 id
跨本次升级逐字节不变。

## hash 公理的已知代价——以及修复方向

同字节 ⇒ 同身份有代价面:#1476——用户原样重发与已折叠内容相同的短文本;裸 `h_` id 重新派生;`isCovered()` 吞掉新消息。注意**修复方向**:kernel #459 加实例重编号(`_1/_2…`),#463 加 `lastPassIds` 快照区分折叠后**回声**(id ∈ covered ∧ ∈ 上一轮 → 保持 id,prune 吞掉)与真**新实例**(id ∈ covered ∧ ∉ 上一轮 → 重编号到空闲 `_k`)。content hash 始终是基座,只在其上叠加实例判别——连 hash 公理自己的 bug 都靠**保留 hash** 修,而不是退回位置 id。
Expand Down
2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.135",
"version": "0.0.136",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
23 changes: 13 additions & 10 deletions kernel/src/wire/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CANONICAL_REDACTED_THINKING_TEXT,
canonicalToolArgs,
deriveCanonicalMessageId,
imageMediaFingerprint,
} from "./identity.js";
import { hashId } from "./util.js";
import { ClusterCounter } from "./message-id.js";
Expand Down Expand Up @@ -192,20 +193,11 @@ export function anthropicToCore(body: AnthropicRequestBody): Flat {
break;
}
case "image": {
const base = deriveCanonicalMessageId(
m.role,
"text",
CANONICAL_IMAGE_TEXT,
);
// #3642: parse the base64 source into the shared media sidecars so
// every dialect presents the same image core (marker text +
// imageMediaType/imageBase64); the raw block keeps verbatim
// round-trip. Other source shapes keep the raw-only sidecar.
const source =
b.source &&
typeof b.source === "object" &&
!Array.isArray(b.source)
? (b.source as { type?: unknown; media_type?: unknown; data?: unknown })
? (b.source as { type?: unknown; media_type?: unknown; data?: unknown; url?: unknown })
: undefined;
const mediaSidecar =
source?.type === "base64" &&
Expand All @@ -216,6 +208,17 @@ export function anthropicToCore(body: AnthropicRequestBody): Flat {
imageBase64: source.data,
}
: {};
const base = deriveCanonicalMessageId(m.role, "text", CANONICAL_IMAGE_TEXT, {
// #3751: payload-addressed ids — base64 source hashes its bytes,
// URL source degrades to the URL string, unknown shapes keep the
// shared legacy base.
mediaFingerprint:
source?.type === "base64" && typeof source.data === "string"
? imageMediaFingerprint(source.data)
: typeof source?.url === "string"
? imageMediaFingerprint(source.url)
: undefined,
});
msgs.push({
id: clusters.next(base),
role: m.role,
Expand Down
5 changes: 5 additions & 0 deletions kernel/src/wire/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CANONICAL_IMAGE_TEXT,
canonicalToolArgs,
deriveCanonicalMessageId,
imageMediaFingerprint,
} from "./identity.js";
import type { BiliMessage } from "./bili-message.js";

Expand Down Expand Up @@ -216,6 +217,10 @@ export function googleToCore(body: GoogleRequestBody): GoogleFlat {
isModel ? "assistant" : "user",
"text",
CANONICAL_IMAGE_TEXT,
{
// #3751: payload-addressed ids — inlineData hashes its base64 payload.
mediaFingerprint: imageMediaFingerprint(inline.data),
},
);
msgs.push({
id: clusters.next(base),
Expand Down
24 changes: 22 additions & 2 deletions kernel/src/wire/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,32 @@ export function deriveCanonicalMessageId(
role: string,
contentType: string,
text: string,
options: { toolName?: string } = {},
options: { toolName?: string; mediaFingerprint?: string } = {},
): string {
const seed = `${idSalt ? idSalt + "|" : ""}${role}|${contentType}||${options.toolName ?? ""}|${text}`;
const seed =
`${idSalt ? idSalt + "|" : ""}${role}|${contentType}||${options.toolName ?? ""}|${text}` +
(options.mediaFingerprint !== undefined ? `|${options.mediaFingerprint}` : "");
return "h_" + hashId(seed);
}

/** #3751 — media fingerprint for image-bearing cores.
*
* Pre-#3751 every image core shared the same canonical text ("[image]"), so
* all images in a conversation hashed to ONE base id and were told apart
* only by the ClusterCounter's positional `_N` ordinals — one image changing
* (offload placeholder, re-encode, delete) shifted the whole family's
* ordinals and re-minted every sibling id on every turn.
*
* The fingerprint makes the id content-addressed per image: data images hash
* their base64 payload; URL-referenced images (no bytes on the wire) degrade
* to the URL string; the rare legacy plural-sidecar core (pre-#3642 shape)
* joins its parts' fingerprints in order. Byte-identical images sharing a
* base id is INTENDED — `_N` stays as the ordinal for true duplicates only.
* Non-image ids never gain the sixth slot, so they stay byte-identical. */
export function imageMediaFingerprint(base64OrUrl: string): string {
return hashId(base64OrUrl);
}

/** Stable seed for opaque wire items (responses reasoning items without
* text, computer/file/web/image/code/mcp call items): hash the item with
* volatile lifecycle fields stripped — `id` (provider-minted, changes on
Expand Down
37 changes: 31 additions & 6 deletions kernel/src/wire/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@ import {
CANONICAL_IMAGE_TEXT,
canonicalToolArgs,
deriveCanonicalMessageId,
imageMediaFingerprint,
} from "./identity.js";
import { hashId } from "./util.js";
import { ClusterCounter } from "./message-id.js";
import { parseDataUrl, type BiliMessage } from "./bili-message.js";

/** #3751: ordered image fingerprints for a legacy plural-sidecar part list —
* data-URL images hash their base64 payload, URL images hash the URL,
* non-image parts contribute nothing (the legacy id of an image-free opaque
* core is preserved verbatim). */
function imageFingerprintsOfParts(parts: unknown[]): string[] {
const out: string[] = [];
for (const part of parts) {
if (!part || typeof part !== "object") continue;
const p = part as { type?: unknown; image_url?: { url?: unknown } };
if (p.type !== "image_url") continue;
const url = typeof p.image_url?.url === "string" ? p.image_url.url : undefined;
if (!url) continue;
const parsed = parseDataUrl(url);
out.push(imageMediaFingerprint(parsed ? parsed.base64 : url));
}
return out;
}

export type OpenAIContentPart =
| { type: "text"; text: string }
| { type: "image_url"; image_url: { url: string } }
Expand Down Expand Up @@ -145,11 +164,13 @@ export function openaiToCore(body: OpenAIRequestBody): Flat {
(part as { image_url?: { url?: unknown } }).image_url?.url;
const parsed =
typeof url === "string" ? parseDataUrl(url) : undefined;
const base = deriveCanonicalMessageId(
"user",
"text",
CANONICAL_IMAGE_TEXT,
);
const base = deriveCanonicalMessageId("user", "text", CANONICAL_IMAGE_TEXT, {
// #3751: payload-addressed ids — data images hash their bytes,
// URL images degrade to the URL string.
mediaFingerprint: imageMediaFingerprint(
parsed ? parsed.base64 : typeof url === "string" ? url : "",
),
});
msgs.push({
id: clusters.next(base),
role: "user",
Expand Down Expand Up @@ -196,7 +217,11 @@ export function openaiToCore(body: OpenAIRequestBody): Flat {
...(host.rawOpenaiContentParts ?? []),
];
} else {
const base = deriveCanonicalMessageId("user", "text", "");
const base = deriveCanonicalMessageId("user", "text", "", {
// #3751: the legacy plural core gets the ordered join of its
// image fingerprints (if any) — zero images keeps the legacy id.
mediaFingerprint: imageFingerprintsOfParts(leadingOpaque).join(",") || undefined,
});
msgs.push({
id: clusters.next(base),
role: "user",
Expand Down
33 changes: 31 additions & 2 deletions kernel/src/wire/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
canonicalOpaqueSeed,
canonicalToolArgs,
deriveCanonicalMessageId,
imageMediaFingerprint,
} from "./identity.js";
import type { ConversationIdentity } from "./util.js";
import { hashId } from "./util.js";
Expand Down Expand Up @@ -309,12 +310,18 @@ export function responsesToCore(
part.type === "input_image" &&
typeof part.image_url === "string"
) {
const image = parseDataUrl(part.image_url);
const base = deriveCanonicalMessageId(
"user",
"text",
CANONICAL_IMAGE_TEXT,
{
// #3751: payload-addressed ids — base64 payload when present, else URL.
mediaFingerprint: imageMediaFingerprint(
image ? image.base64 : part.image_url,
),
},
);
const image = parseDataUrl(part.image_url);
msgs.push({
id: clusters.next(base),
role: "user",
Expand Down Expand Up @@ -374,8 +381,30 @@ export function responsesToCore(
role === "user"
? userDisplayText(message.content, effText)
: effText;
// #3751: payload-addressed ids — every input_image in the merged
// shape contributes its fingerprint in order (single image ⇒ one
// fingerprint, byte-identical to the split path's image core so
// both forms of one logical message share one id; text-only legacy
// cores keep their ids verbatim).
const mediaFingerprint = Array.isArray(message.content)
? message.content
.flatMap((part) => {
if (
part?.type === "input_image" &&
typeof part.image_url === "string"
) {
const raw = part.image_url;
const decoded = parseDataUrl(raw);
return [imageMediaFingerprint(decoded ? decoded.base64 : raw)];
}
return [];
})
.join(",") || undefined
: undefined;
coreId = clusters.next(
deriveCanonicalMessageId(role, "text", displayText),
deriveCanonicalMessageId(role, "text", displayText, {
mediaFingerprint,
}),
);
const imageUrl = Array.isArray(message.content)
? message.content.find(
Expand Down
Loading
Loading