E04 修到零被拒线,harness 补七刀:跨进程租约与广播桥、原子写、引用墙、对话区按序号排

活动侧(E04 2.1.0 → 2.1.9,I01 1.2.2):
- 覆盖闭合成为机检:基座新增 distinct 读数,「计划盖住的客户数 = 候选行数」钉进
  S3 收步 / S4 哨兵 / 放行 L2;人加的候选客户不再被整单无视
- 「关键人待识别」占位句式全链退役:查无联系人先发数据录入卡收人;契约新增 refersTo,
  计划/回执的 contact_code 只许引用真实存在的联系人(安装期核引用目标)
- 台账更新确认摆全落库面:客户档案/联系人档案两张落库预览(touched: 数据源,
  正式现值 + 草稿叠加),可补改、可加行(自增主键由基座续号);姓名升必填
- 更新明细:update_key 位置键 + 「同一处只留一条」L2;依据写人话不带机器键;
  标题旁不摆单号;判据与文案不夹字段名;行标识去掉单标识那一段
- 回执骨架 = 计划行数钉进 S3 收步与 S4 哨兵;重做分支不许删计划凑数
- 阶段刻度对齐文档 7 步;起单样例预填满

基座侧:
- 两个后端连同一个库:task-claim 跨进程租约(迁移 0030)+ Postgres LISTEN/NOTIFY
  广播桥;对话区按任务内记忆序号排,不按墙上时钟;并卡「刚说过话」按记忆序列判紧邻
- 多行 write 整批先验后写(dryRun),被拒 = 一行都没落
- 决策卡意见与本单草稿字面比对,归属以观察进轨迹(note-anchors)
- 括号栈补「补闭括号」两形(少一个 } / 行对象没关直接跟下一行)
- 件上加行:空着的自增主键直进续号,不从别的行借号;未落下的行点名回给前端
- 人答复落库那一刻就把 runtime 翻 running(task.wake),两块同帧变脸
- 试跑单可落真库(land_master,迁移 0031,默认开);数据域按活动分组;
  JSON 列不再是 [object Object];按钮键感 + 在办进度条 + 防抖
- 报价链 A1–A7 从系统卸载(只卸不删源码),种子只装 E04/I01

契约变更(api-report 已刷新):FieldDeclaration.distinct / refersTo,
objectDeclaration.file,landMaster,unkeyed,assetFiles,ledgerByActivityRoute。
1700 测 / typecheck / lint / depcruise / api:check 绿。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EEJX8Z7CHejvwmDQyPQuBX
This commit is contained in:
xcs
2026-08-25 01:23:30 +08:00
co-authored by Claude Fable 5
parent 92dd729c36
commit f3397a60ef
91 changed files with 3919 additions and 350 deletions
@@ -0,0 +1,105 @@
/**
* 广播的跨进程桥 —— **别的后端动了同一张单,这边的浏览器也得知道**。
*
* 2026-08-24 用户报的第三件事:「一直卡在 AI 推进中,刷新后才发现已经推完了」。
* 推送本身没坏 —— 坏在 `createBroadcaster()` 是进程内的一个 Set:那会儿本机与
* 局域网另一台各跑一个后端连同一个库,单子被那一台推着走,这一台的浏览器一个字没收到。
*
* 这里钉两条:①自己推的顺手 NOTIFY 出去;②**别人发的才在本地转发,自己发的不收** ——
* 不然一条推送在两个进程之间来回弹。
*/
import { describe, expect, it } from 'vitest';
import { bridgeBroadcaster, TOUCH_CHANNEL } from '../broadcast-bridge.ts';
import { createBroadcaster } from '../broadcast.ts';
/** 假连接池:记下 NOTIFY 出去的载荷,并留一个口子把「别人的通知」塞进来 */
function fakePool() {
const notified: Array<{ channel: string; payload: string }> = [];
let onNotification: ((m: { channel: string; payload?: string }) => void) | undefined;
const client = {
on(event: string, handler: (arg: never) => void) {
if (event === 'notification') onNotification = handler as never;
},
query: async () => ({ rows: [] }),
release: () => undefined,
};
return {
notified,
/** 模拟别的进程(或自己)发来一条 */
deliver(wire: Record<string, unknown> | string) {
// 字符串原样送 —— 「载荷坏了」那条用例要的是真·不是 JSON 的字节
onNotification?.({ channel: TOUCH_CHANNEL, payload: typeof wire === 'string' ? wire : JSON.stringify(wire) });
},
get armed() {
return onNotification !== undefined;
},
pool: {
query: async (_sql: string, params: unknown[]) => {
notified.push({ channel: String(params[0]), payload: String(params[1]) });
return { rows: [] };
},
connect: async () => client,
},
};
}
const settle = (): Promise<void> => new Promise((r) => setTimeout(r, 0));
describe('bridgeBroadcaster', () => {
it('本地推一条 → 顺手 NOTIFY 到库上(带着「谁发的」)', async () => {
const fake = fakePool();
const bus = createBroadcaster();
bridgeBroadcaster(fake.pool as never, bus);
bus.publish({ kind: 'task', id: 't-1', reason: 'advanced' });
await settle();
expect(fake.notified).toHaveLength(1);
expect(fake.notified[0]?.channel).toBe(TOUCH_CHANNEL);
const wire = JSON.parse(fake.notified[0]?.payload ?? '{}') as Record<string, unknown>;
expect(wire['id']).toBe('t-1');
expect(typeof wire['from']).toBe('string'); // 认「谁发的」靠它
});
it('**别的进程发来的,在本地转发一次** —— 这正是那一台推进、这一台刷新的那条路', async () => {
const fake = fakePool();
const bus = createBroadcaster();
bridgeBroadcaster(fake.pool as never, bus);
await settle();
expect(fake.armed).toBe(true);
const got: string[] = [];
bus.subscribe((t) => got.push(`${t.kind}:${t.id}:${t.reason}`));
fake.deliver({ kind: 'task', id: 't-9', reason: 'advanced', from: '别的机器' });
expect(got).toEqual(['task:t-9:advanced']);
});
it('**自己发的不再收** —— 否则一条推送在两个进程之间来回弹', async () => {
const fake = fakePool();
const bus = createBroadcaster();
bridgeBroadcaster(fake.pool as never, bus);
bus.publish({ kind: 'task', id: 't-1', reason: 'advanced' });
await settle();
const self = (JSON.parse(fake.notified[0]?.payload ?? '{}') as { from?: string }).from;
const got: string[] = [];
bus.subscribe((t) => got.push(t.id));
fake.deliver({ kind: 'task', id: 't-1', reason: 'advanced', from: self });
expect(got).toEqual([]);
});
it('载荷坏了只丢这一条,连接照旧活着', async () => {
const fake = fakePool();
const bus = createBroadcaster();
bridgeBroadcaster(fake.pool as never, bus);
await settle();
const got: string[] = [];
bus.subscribe((t) => got.push(t.id));
expect(() => fake.deliver('这不是 JSON')).not.toThrow();
// 是 JSON 但形状不对的也得丢掉 —— 频道全库公用,谁都能往里发一句话
expect(() => fake.deliver('"光秃秃一个字符串"')).not.toThrow();
expect(() => fake.deliver({ 没有: 'kind 与 id' })).not.toThrow();
fake.deliver({ kind: 'task', id: 't-3', reason: 'advanced', from: '别处' });
expect(got).toEqual(['t-3']);
});
});
@@ -0,0 +1,42 @@
/**
* 对话区按任务内序号排(2026-08-24):两台后端交替推同一单,时钟一偏,
* 上一拍的话就排到下一拍后面 —— 序号与时钟无关,拿它排。
*/
import { describe, expect, it } from 'vitest';
import { orderChat } from '../chat-order.ts';
const H = [
{ id: 'm1', seq: 1, kind: 'narration', at: '2026-08-24T10:00:30.000Z' }, // 另一台机器,钟快 30s
{ id: 'c1', seq: 2, kind: 'card-sent', at: '2026-08-24T10:00:30.100Z', detail: { id: 'card-A' } },
{ id: 'm2', seq: 3, kind: 'narration', at: '2026-08-24T10:00:05.000Z' }, // 本机,钟准
{ id: 'h1', seq: 4, kind: 'human-input', at: '2026-08-24T10:00:06.000Z', detail: { id: 'say-1' } },
];
describe('orderChat', () => {
it('**时钟倒着、序号顺着:按序号排** —— 上一拍的话不再跑到下一拍后面', () => {
const items = [
{ id: 'm2', at: '2026-08-24T10:00:05.000Z', text: '下一拍' },
{ id: 'm1', at: '2026-08-24T10:00:30.000Z', text: '上一拍' },
];
expect(orderChat(items, H).map((i) => i.text)).toEqual(['上一拍', '下一拍']);
});
it('卡按它的 card-sent 落点排;人的自由输入按 human-input 落点排', () => {
const items = [
{ id: 'say-1', at: '2026-08-24T10:00:06.000Z', text: '人说' },
{ id: 'card-A', at: '2026-08-24T10:00:30.050Z', text: '卡' },
{ id: 'm1', at: '2026-08-24T10:00:30.000Z', text: '话' },
];
expect(orderChat(items, H).map((i) => i.text)).toEqual(['话', '卡', '人说']);
});
it('找不到落点的老数据按时刻插位,不炸', () => {
const items = [
{ id: 'zzz', at: '2026-08-24T10:00:05.500Z', text: '老' },
{ id: 'm2', at: '2026-08-24T10:00:05.000Z', text: '下一拍' },
{ id: 'm1', at: '2026-08-24T10:00:30.000Z', text: '上一拍' },
];
// 老条目的时刻在 m2(seq 3)之后、h1 之前 → 插到 seq 3 那一档,时刻再排在 m2 之后
expect(orderChat(items, H).map((i) => i.text)).toEqual(['上一拍', '下一拍', '老']);
});
});
@@ -9,7 +9,7 @@
* 所以归型放在写进草稿那一刻:归得了就归,归不了当场拒(理由喂回下一拍)。
*/
import { describe, expect, it } from 'vitest';
import { coerceToColumn, localStamp, missingReadings, rowReadings, rowKeyList, tallyReadings, toolCallReadings } from '../wiring/engines.ts';
import { coerceToColumn, distinctReadings, localStamp, missingReadings, rowLabel, rowReadings, rowKeyList, tallyReadings, toolCallReadings, touchedRows } from '../wiring/engines.ts';
describe('空值 = 这一列没有值(不写)', () => {
it('空串 / 全空白 / null / undefined 一律不写 —— 「原文没写的不要补」的数据面落点', () => {
@@ -381,3 +381,108 @@ describe('工具调用读数', () => {
]);
});
});
/**
* 不同值计数 `域_对象_distinct_<列>` —— 覆盖闭合判据的落点(2026-08-24 柴浩实测:
* 人在覆盖范围确认卡上加了一家客户,计划行数照样 > 0,「盖没盖住每一家」没有数说得出来)。
*/
describe('distinctReadings —— 这一列有几个不同的值,基座数', () => {
const DECL = [
{
domain: 'crm_coverage',
object: 'plan',
fields: [{ name: 'customer_code', distinct: true }, { name: 'contact_code' }],
},
];
it('计划行数 ≠ 客户数:一家客户几位关键人就几行,distinct 数的是客户', () => {
const rows = [
{ customer_code: 'CU-0142', contact_code: 'CU-0142-01' },
{ customer_code: 'CU-0142', contact_code: 'CU-0142-02' },
{ customer_code: 'cu-1111', contact_code: 'cu-1111-00' },
];
expect(distinctReadings({ 'mine:crm_coverage.plan': rows }, DECL)).toEqual({
crm_coverage_plan_distinct_customer_code: 2,
});
});
it('没标 distinct 的列不出读数;标了的零行也铺 0 —— 未绑定的名字参与比较求值为假', () => {
const out = distinctReadings({ 'mine:crm_coverage.plan': [] }, DECL);
expect(out).toEqual({ crm_coverage_plan_distinct_customer_code: 0 });
});
it('空值不算一种 —— 「没写」不是一个取值', () => {
const rows = [{ customer_code: 'CU-0142' }, { customer_code: '' }, { customer_code: ' ' }, {}];
expect(distinctReadings({ 'mine:crm_coverage.plan': rows }, DECL)).toEqual({
crm_coverage_plan_distinct_customer_code: 1,
});
});
});
/**
* `touched:域.对象` —— 落库预览的数据源(2026-08-24 柴浩实测:绑 mine: 的话
* Itelma 既有的客户类型/行业整片空白,像是数据丢了)。
*/
describe('touchedRows —— 本轮碰过的行,以叠加后全貌摆出来', () => {
const merged = [
{ customer_code: 'CU-0142', name: 'Itelma', cust_type: 'Tier1 OEM', industry: '车规电子' },
{ customer_code: 'CU-0087', name: '深圳精密', cust_type: '模组厂' },
{ customer_code: 'CU-2039', name: '测试11111' },
];
it('按草稿键挑行:碰过的行带着正式现值的全部列,没碰的行不出现', () => {
const out = touchedRows(merged, [{ customer_code: 'CU-0142' }, { customer_code: 'CU-2039' }]);
expect(out).toHaveLength(2);
// 关键在这一列:草稿里没写过 cust_type,叠加行里必须有 —— 这正是回显的来源
expect((out[0] as Record<string, unknown>)['cust_type']).toBe('Tier1 OEM');
expect((out[1] as Record<string, unknown>)['name']).toBe('测试11111');
});
it('草稿新增的行(正式表没有)也在 —— 叠加视图本就含它;键对不上的丢弃不报错', () => {
const withNew = [...merged, { customer_code: 'CU-NEW', contact_code: 'CU-NEW-01', name: '新人' }];
const out = touchedRows(withNew, [
{ customer_code: 'CU-NEW', contact_code: 'CU-NEW-01' },
{ customer_code: '查无此人', contact_code: 'X' },
]);
expect(out).toHaveLength(1);
expect((out[0] as Record<string, unknown>)['name']).toBe('新人');
});
it('数值键与字符串键按字面等值比(pg 回来的是 number,草稿键常是 string)', () => {
const out = touchedRows([{ seq: 3, name: '三号' }], [{ seq: '3' }]);
expect(out).toHaveLength(1);
});
});
/**
* 摆给人看的行标识去掉单标识(2026-08-25 用户:「COV-…/CU-0142-01 只保留斜杠右边」)。
*/
describe('rowLabel —— 主键去掉单号那一段', () => {
it('单据表:round_ref + contact_code → 只留 contact_code', () => {
expect(rowLabel({ round_ref: 'COV-20260825002227', contact_code: 'CU-0142-01' }, 'round_ref')).toBe('CU-0142-01');
});
it('主数据表没有单标识:全部保留', () => {
expect(rowLabel({ customer_code: 'CU-0142', contact_code: 'CU-0142-01' }, undefined)).toBe('CU-0142/CU-0142-01');
});
it('主键只有单号:去完一段都不剩就退回整段,不摆空串', () => {
expect(rowLabel({ round_ref: 'COV-1' }, 'round_ref')).toBe('COV-1');
});
it('缺项名单走同一把尺 —— 红带 / 提交闸 / 拒回都不再带单号', () => {
const out = missingReadings({ 'mine:crm_coverage.receipt': [{ round_ref: 'COV-1', contact_code: 'CU-0142-01' }] }, [
{
domain: 'crm_coverage',
object: 'receipt',
caseKey: 'round_ref',
fields: [
{ name: 'round_ref', primaryKey: true },
{ name: 'contact_code', primaryKey: true },
{ name: 'completion_status', primaryKey: false, required: true, label: '完成状态' },
],
},
]);
expect(out['crm_coverage_receipt_missing_keys']).toBe('CU-0142-01 缺完成状态');
});
});
@@ -0,0 +1,63 @@
/**
* 驳回意见的**归属比对** —— 2026-08-24 柴浩实测:意见「测试111建议动作改成拜访CTO」,
* 计划表里有一行 customer_name=测试111,模型却把改动落到了 Itelma 那一行。
* 归属是纯字符串包含,引擎数得出来的事实别让模型从没标点的话里自己抠。
*/
import { describe, expect, it } from 'vitest';
import { noteAnchors, noteAnchorTitle } from '../note-anchors.ts';
const row = (object: string, key: Record<string, string>, patch: Record<string, unknown>) => ({
domain: 'crm_coverage',
object,
key,
patch,
});
const PLAN = [
row('plan', { round_ref: 'COV-1', contact_code: 'CU-0142-01' }, { customer_name: 'Itelma', contact_name: 'Ekaterina Volkova', action: '拜访采购' }),
row('plan', { round_ref: 'COV-1', contact_code: 'cu-1234-00' }, { customer_name: '测试111', contact_name: '关键人待识别', action: '首访建档' }),
];
describe('noteAnchors —— 意见按字面点名对上了哪几行', () => {
it('**实测那一单**:意见里的「测试111」恰是一行的客户名 —— 命中它,不命中 Itelma', () => {
const hits = noteAnchors('测试111建议动作改成拜访CTO', PLAN);
expect(hits).toHaveLength(1);
expect(hits[0]).toContain('cu-1234-00');
expect(hits[0]).toContain('测试111');
});
it('点的是编码也认得(主键值同样参与比对)', () => {
const hits = noteAnchors('CU-0142-01 的动作太保守,改激进些', PLAN);
expect(hits).toHaveLength(1);
expect(hits[0]).toContain('CU-0142-01');
});
it('一行都没点名 → 空数组(没有依据的时候不说话)', () => {
expect(noteAnchors('整体排序不合理,重新排一遍', PLAN)).toEqual([]);
});
it('纯数字与短值不算点名 —— 「111」「01」撞上纯属巧合', () => {
const rows = [row('plan', { round_ref: 'COV-1', contact_code: 'X' }, { seq: '111', tag: '高' })];
expect(noteAnchors('测试111建议动作改成拜访CTO', rows)).toEqual([]);
});
it('命中太多行(> 5)当没命中 —— 什么都命中的提示是噪音', () => {
const rows = Array.from({ length: 6 }, (_, i) =>
row('plan', { round_ref: 'COV-1', contact_code: `C-${i}` }, { customer_name: 'Itelma' }),
);
expect(noteAnchors('Itelma 的动作都改一下', rows)).toEqual([]);
});
it('同一行几格都命中时只出一次,展示最长的那个名字', () => {
const rows = [row('plan', { round_ref: 'COV-1', contact_code: 'C-1' }, { customer_name: '测试111', short: '测试' })];
const hits = noteAnchors('测试111建议动作改成拜访CTO', rows);
expect(hits).toHaveLength(1);
expect(hits[0]).toContain('「测试111」');
});
it('观察全文带行动指南:先认归属、点名的行缺什么改这一行自己的内容', () => {
const title = noteAnchorTitle(['crm_coverage.plan COV-1/cu-1234-00(命中「测试111」)']);
expect(title).toContain('先认这几行的归属');
expect(title).toContain('不是把改动挪到别的行头上');
});
});
@@ -99,6 +99,30 @@ describe('件上加行:主键凑得齐才落得下', () => {
});
});
it('**空着的自增列不许从别的行借号** —— 加的新联系人不再覆盖该客户已有的草稿行(2026-08-24)', () => {
// 人加一行:customer_code 填了、contact_code(auto)在件上但空着;该客户已有一行草稿联系人
const contactDrafts = [{ key: { customer_code: 'CU-E2E-ADD', contact_code: 'CU-E2E-ADD-01' } }];
const out = resolveRowKey({
keys: ['customer_code', 'contact_code'],
cells: { customer_code: 'CU-E2E-ADD', contact_code: '', name: '测试CTO王工' },
drafts: contactDrafts,
types: { customer_code: 'text', contact_code: 'text' },
autos: ['contact_code'],
});
// 不许把 -01 借来当自己的钥匙(那是覆盖别人);认不出 → 交给 mint 那条路续号
expect(out).toBeUndefined();
// 编码真填了的(编辑既有行)照旧认得出
expect(
resolveRowKey({
keys: ['customer_code', 'contact_code'],
cells: { customer_code: 'CU-E2E-ADD', contact_code: 'CU-E2E-ADD-01', name: '改名' },
drafts: contactDrafts,
types: { customer_code: 'text', contact_code: 'text' },
autos: ['contact_code'],
}),
).toEqual({ customer_code: 'CU-E2E-ADD', contact_code: 'CU-E2E-ADD-01' });
});
it('**新增行:序号空着 → 基座续号(现有最大 + 1)**,不再静默丢掉', () => {
expect(resolveRowKey({ keys: KEYS, cells: { content_type: '案例集' }, drafts: SAME_ROUND, types: TYPES })).toEqual({
round_ref: 'CTB-1',
@@ -438,3 +462,61 @@ describe('parseRowKey —— 件上传来的主键认哪一行', () => {
expect(parseRowKey('X', [])).toBeUndefined();
});
});
/**
* **保存说成功,就得真的都存下了**(2026-08-24 用户实测:
* 「用编辑新增了一个客户,推进到下一步直接就把我手动添加的这一行遗漏掉了」)。
*
* 主键那几格空着就凑不出钥匙 —— 从前基座一句 `continue` 悄悄跳过,接口照回 `{ok:true}`:
* 人看到「保存成功」,东西却不在,而提交闸也拦不住(一行根本没写进台账,谈何缺列)。
* 现在没落下的行要点名说出来,且**只点他填得了的那几列**。
*/
describe('落不下的行要点名 —— 不许悄悄丢', () => {
const keys = ['round_ref', 'customer_code'];
it('主键那一格空着 → 认不出这一行(resolveRowKey 交白卷)', () => {
expect(
resolveRowKey({
keys,
cells: { customer_code: '', customer_name: '忘了填编号的客户' },
drafts: [{ key: { round_ref: 'COV-1', customer_code: 'CU-0142' } }],
types: { round_ref: 'text', customer_code: 'text' },
}),
).toBeUndefined();
});
it('主键填了 → 案号由「全表一致」补齐,行认得出来', () => {
expect(
resolveRowKey({
keys,
cells: { customer_code: 'CU-9527', customer_name: '人工加的客户' },
drafts: [{ key: { round_ref: 'COV-1', customer_code: 'CU-0142' } }],
types: { round_ref: 'text', customer_code: 'text' },
}),
).toEqual({ customer_code: 'CU-9527', round_ref: 'COV-1' });
});
it('**表里只有一行时也不许「继承」老行的编码** —— 那会当场把老行覆盖掉', () => {
// 件上摆着 customer_code、人却空着:一行的表在「全表一致」眼里像个常量,
// 从前新行会拿到 CU-0142(屏上两行、库里一行,而且内容还是新的那份)
expect(
resolveRowKey({
keys,
cells: { customer_code: '', customer_name: '新客户' },
drafts: [{ key: { round_ref: 'COV-1', customer_code: 'CU-0142' } }],
types: { round_ref: 'text', customer_code: 'text' },
}),
).toBeUndefined();
});
it('件上没摆的那把钥匙(案号)照旧由基座按全表一致补 —— 那是他碰不到的列', () => {
expect(
resolveRowKey({
keys,
cells: { customer_code: 'CU-9527' },
drafts: [{ key: { round_ref: 'COV-1', customer_code: 'CU-0142' } }],
types: { round_ref: 'text', customer_code: 'text' },
}),
).toEqual({ customer_code: 'CU-9527', round_ref: 'COV-1' });
});
});