Archived
活动侧(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
106 lines
4.4 KiB
TypeScript
106 lines
4.4 KiB
TypeScript
/**
|
|
* 广播的跨进程桥 —— **别的后端动了同一张单,这边的浏览器也得知道**。
|
|
*
|
|
* 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']);
|
|
});
|
|
});
|