@suifei/dsh-ai-team 0.4.89 → 0.4.91
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -0
- package/index.js +107 -49
- package/lib/diaglog.js +145 -143
- package/lib/host/member-tools.js +317 -307
- package/lib/host/service.js +5 -0
- package/lib/host/waker.js +371 -359
- package/lib/ledger/fold.js +60 -4
- package/lib/runtime/host-agent.js +39 -61
- package/package.json +1 -1
package/lib/ledger/fold.js
CHANGED
|
@@ -43,8 +43,15 @@ import { isReviewCandidate } from '../domain/policy.js';
|
|
|
43
43
|
*
|
|
44
44
|
* v4:新增有界自主投影 `pmoFreeWave` / `pmoRoundsUsed` / `pmoCapped`(见 applyEvent);
|
|
45
45
|
* 旧 checkpoint 没有这些字段,作废重折。
|
|
46
|
+
*
|
|
47
|
+
* v5:`team/host-bound` 换 PMO 会话时把已有会话的成员标为 routeStale(每项目一个 PMO)。
|
|
48
|
+
*
|
|
49
|
+
* v6:新增 `pmoNews`(见 applyEvent):PMO 看过空看板之后,没有新进展就不再定时推进。
|
|
50
|
+
*
|
|
51
|
+
* v7:插件内部以 Human 身份记的簿记事件(会话绑定 / 激活失败 / 人格蒸馏)不再算
|
|
52
|
+
* Human 意图——它们曾在每次重启时重置 PMO 预算并触发一次推进。
|
|
46
53
|
*/
|
|
47
|
-
export const FOLD_STATE_VERSION =
|
|
54
|
+
export const FOLD_STATE_VERSION = 7;
|
|
48
55
|
|
|
49
56
|
/**
|
|
50
57
|
* 投影封顶:大规模/长迭代账本下,stream 与每个 Thread 的 entries 不随事件数
|
|
@@ -665,12 +672,49 @@ function applyFollow(state, event, payload, following) {
|
|
|
665
672
|
* - 成员在空看板上建卡:有 freeWave 则消耗它(这是 Human 指令的那一批),否则 `pmoRoundsUsed + 1`;
|
|
666
673
|
* - `project/pmo-capped`:本份预算已告知暂停(只告知一次)。
|
|
667
674
|
* 预算只由 Human 续,成员不能给自己续。闸门在 service(createTask / pmoGate)。
|
|
675
|
+
*
|
|
676
|
+
* 新进展(`pmoNews`,pmoTick 的空看板推进只在为 true 时放行):
|
|
677
|
+
* - Human 作者事件、Task 进入终态(accepted / cancelled / deleted)→ true;
|
|
678
|
+
* - PMO 被成功唤醒去看局面(pmo_continue / wave_done / human_message)→ false。
|
|
679
|
+
* r09:两个项目目标完成后,PMO 每 10 分钟被推进一次、拉 5 名成员开会,再发一条
|
|
680
|
+
* 「状态不变」,35 分钟 8 次推进 / 40 次会议唤醒;wave_done 与 pmo_continue 还在同一秒重复。
|
|
681
|
+
*
|
|
682
|
+
* 「Human 作者事件」指 Human 意图(`isHumanIntent`)。插件激活成员时以 Human 身份记
|
|
683
|
+
* `member/session-bound`(失败记 `member/failed`),反思后记 `member/distilled`;历史账本
|
|
684
|
+
* 里已大量存在。重启即重新激活成员,若把它们算作 Human 动作,每次重启都会重置预算
|
|
685
|
+
* 并推进一次(r10 复验:重启 1 秒内两个已静默项目各开会一次)。
|
|
668
686
|
*/
|
|
687
|
+
const HUMAN_BOOKKEEPING_EVENTS = new Set(['member/session-bound', 'member/failed', 'member/distilled']);
|
|
688
|
+
|
|
689
|
+
/** 事件是否代表 Human 的意图(而不是插件代 Human 记的簿记)。 */
|
|
690
|
+
export function isHumanIntent(event) {
|
|
691
|
+
return event?.actor?.kind === 'human' && !HUMAN_BOOKKEEPING_EVENTS.has(event.type);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const PMO_NEWS_TASK_EVENTS = new Set(['task/accepted', 'task/cancelled', 'task/deleted']);
|
|
695
|
+
const PMO_SEEN_REASONS = new Set(['pmo_continue', 'wave_done', 'human_message']);
|
|
696
|
+
|
|
669
697
|
export function applyEvent(state, event) {
|
|
670
|
-
const next = applyEventCore(state, event);
|
|
698
|
+
const next = applyBudget(state, event, applyEventCore(state, event));
|
|
699
|
+
if (next === state) return next;
|
|
700
|
+
if (isHumanIntent(event) || PMO_NEWS_TASK_EVENTS.has(event.type)) {
|
|
701
|
+
return next.pmoNews === true ? next : { ...next, pmoNews: true };
|
|
702
|
+
}
|
|
703
|
+
if (event.type === 'wake/delivered' && PMO_SEEN_REASONS.has(event.payload?.reason)) {
|
|
704
|
+
return next.pmoNews === false ? next : { ...next, pmoNews: false };
|
|
705
|
+
}
|
|
706
|
+
return next;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/** PMO 定时推进是否有新进展可看(缺省视为有:老账本与首个空看板)。 */
|
|
710
|
+
export function pmoHasNews(state) {
|
|
711
|
+
return state.pmoNews !== false;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
function applyBudget(state, event, next) {
|
|
671
715
|
if (next === state) return next;
|
|
672
716
|
if (event.type === 'project/pmo-capped') return { ...next, pmoCapped: true };
|
|
673
|
-
if (event
|
|
717
|
+
if (isHumanIntent(event)) {
|
|
674
718
|
if (next.pmoFreeWave === true && (next.pmoRoundsUsed ?? 0) === 0 && next.pmoCapped !== true) return next;
|
|
675
719
|
return { ...next, pmoFreeWave: true, pmoRoundsUsed: 0, pmoCapped: false };
|
|
676
720
|
}
|
|
@@ -861,7 +905,19 @@ function applyEventCore(state, event) {
|
|
|
861
905
|
case 'team/host-bound': {
|
|
862
906
|
const sessionId = stringOrNull(payload.sessionId);
|
|
863
907
|
if (sessionId === null) return advance(state, event);
|
|
864
|
-
|
|
908
|
+
const previous = stringOrNull(state.hostSessionId);
|
|
909
|
+
if (previous === null || previous === sessionId) return advance(state, event, { hostSessionId: sessionId });
|
|
910
|
+
// PMO 会话换了:已有会话的成员是旧 PMO 的子会话,新 PMO 向它们投递会以
|
|
911
|
+
// UNAUTHORIZED 被拒(不在其子树)。标记 routeStale,下次唤醒即在新 PMO 下、
|
|
912
|
+
// 项目工作区里重建一次(与换模型同一条路径);绑定新会话时标记自然清除。
|
|
913
|
+
const members = {};
|
|
914
|
+
for (const [id, member] of Object.entries(state.members ?? {})) {
|
|
915
|
+
members[id] = member?.kind === 'agent' && typeof member.sessionId === 'string' && member.sessionId !== ''
|
|
916
|
+
&& member.status !== MEMBER_STATUS.RETIRED
|
|
917
|
+
? { ...member, routeStale: true }
|
|
918
|
+
: member;
|
|
919
|
+
}
|
|
920
|
+
return advance(state, event, { hostSessionId: sessionId, members });
|
|
865
921
|
}
|
|
866
922
|
|
|
867
923
|
case 'member/config-updated': {
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Resolve the parent Agent that
|
|
2
|
+
* Resolve one project's PMO session: the parent Agent of that project's members.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Core model: one PMO per project, resident in the project's workspace; every
|
|
5
|
+
* member is its subagent child, so member sessions live in the same workspace.
|
|
6
|
+
* The PMO session is owned by the plugin — it never borrows the Human's own
|
|
7
|
+
* chat session (doing so parented members under whatever conversation the
|
|
8
|
+
* Human had open, in whatever workspace, and delivered their results into it).
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
10
|
+
* Order: the project's persisted PMO session (live or cold-resumed) when its
|
|
11
|
+
* cwd is the project workspace; otherwise a fresh one created in that cwd.
|
|
11
12
|
*
|
|
12
|
-
* IMPORTANT — the
|
|
13
|
+
* IMPORTANT — the PMO session must never be archived. Members are subagent
|
|
13
14
|
* descendants of it, and `archived-session-gate` (dsh-api-session-controller)
|
|
14
15
|
* rejects every `agent/pre-step` whose Session, or any Session above it in the
|
|
15
16
|
* subagent lineage, is archived. The loop then ends the turn as
|
|
16
|
-
* `{kind:'blocked'}` **without issuing a model request**, so archiving
|
|
17
|
+
* `{kind:'blocked'}` **without issuing a model request**, so archiving it
|
|
17
18
|
* silently disables the whole team (observed: 453 blocked turns, 0 completed).
|
|
18
19
|
* Earlier revisions archived it to hide it from the session list; that is
|
|
19
20
|
* repaired here by unarchiving on resolve.
|
|
@@ -89,62 +90,39 @@ export async function ensureHostAgent(options) {
|
|
|
89
90
|
};
|
|
90
91
|
if (typeof options.resumeSessionId === 'string' && options.resumeSessionId !== '') {
|
|
91
92
|
const live = typeof agents.get === 'function' ? agents.get(options.resumeSessionId) : undefined;
|
|
92
|
-
|
|
93
|
+
let agent = live;
|
|
94
|
+
let release = async () => {};
|
|
95
|
+
if (agent === undefined) {
|
|
96
|
+
try {
|
|
97
|
+
const handle = await agents.resume({ resumeSessionId: options.resumeSessionId });
|
|
98
|
+
agent = handle?.agent;
|
|
99
|
+
release = async () => { try { await handle?.dispose?.(); } catch { /* already gone */ } };
|
|
100
|
+
} catch (error) {
|
|
101
|
+
// A persisted PMO session must be resumed: creating another one would
|
|
102
|
+
// break the existing members' direct-child adjacency.
|
|
103
|
+
throw new HostAgentError('E_HOST_AGENT', `Team Host Agent resume failed: ${error?.message ?? error}`, {
|
|
104
|
+
sessionId: options.resumeSessionId,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// 会话的 cwd 创建后不可变,成员子会话继承它。cwd 不是本项目工作区时不能复用
|
|
109
|
+
// (实机:host 记着插件数据目录,30 个成员全在数据目录里干活、attach 每天被拒上百次;
|
|
110
|
+
// 多项目共用一条 host 时,B 项目成员会落进 A 的工作区)。在线与冷恢复同一判定。
|
|
111
|
+
const expectedCwd = typeof options.cwd === 'string' ? options.cwd.trim() : '';
|
|
112
|
+
const actualCwd = agent?.session?.header?.cwd ?? options.sessions?.get?.(options.resumeSessionId)?.header?.cwd ?? null;
|
|
113
|
+
if (expectedCwd !== '' && typeof actualCwd === 'string' && actualCwd !== '' && samePath(actualCwd, expectedCwd) === false) {
|
|
114
|
+
if (live === undefined) await release();
|
|
115
|
+
reportFail(
|
|
116
|
+
'host.resume.cwd-mismatch.fail',
|
|
117
|
+
{ sessionId: options.resumeSessionId, expectedCwd, actualCwd, live: live !== undefined },
|
|
118
|
+
'[ai-team] PMO session cwd does not match the project workspace; creating a fresh one',
|
|
119
|
+
);
|
|
120
|
+
} else {
|
|
93
121
|
await unarchiveHost(options.resumeSessionId);
|
|
94
122
|
const selection = typeof options.agentDefaultModel?.currentSelection === 'function' ? options.agentDefaultModel.currentSelection() : null;
|
|
95
123
|
persistSelection(options.resumeSessionId, selection);
|
|
96
|
-
return { agent
|
|
124
|
+
return { agent, created: false, resumed: true, sessionId: options.resumeSessionId, dispose: release };
|
|
97
125
|
}
|
|
98
|
-
try {
|
|
99
|
-
const handle = await agents.resume({ resumeSessionId: options.resumeSessionId });
|
|
100
|
-
await unarchiveHost(options.resumeSessionId);
|
|
101
|
-
// 持久化的 host 会话保留着**创建时的 cwd**,而成员子会话会继承它。
|
|
102
|
-
// 实机事故:2026-11-09 建的 host 记的是 `…/.dsh/ai-team/default`,于是
|
|
103
|
-
// 30 个成员会话全在插件数据目录里干活,`workspace.attachSession` 每天被拒
|
|
104
|
-
// 上百次。cwd 不一致时不能复用这条 host——释放它,落到下面的创建分支。
|
|
105
|
-
const expectedCwd = typeof options.cwd === 'string' ? options.cwd.trim() : '';
|
|
106
|
-
const actualCwd = handle?.agent?.session?.header?.cwd ?? options.sessions?.get?.(options.resumeSessionId)?.header?.cwd ?? null;
|
|
107
|
-
if (expectedCwd !== '' && typeof actualCwd === 'string' && actualCwd !== ''
|
|
108
|
-
&& samePath(actualCwd, expectedCwd) === false) {
|
|
109
|
-
try { await handle.dispose?.(); } catch { /* already gone */ }
|
|
110
|
-
reportFail(
|
|
111
|
-
'host.resume.cwd-mismatch.fail',
|
|
112
|
-
{ sessionId: options.resumeSessionId, expectedCwd, actualCwd },
|
|
113
|
-
'[ai-team] Team Host session cwd does not match the project workspace; creating a fresh host',
|
|
114
|
-
);
|
|
115
|
-
} else {
|
|
116
|
-
const selection = typeof options.agentDefaultModel?.currentSelection === 'function' ? options.agentDefaultModel.currentSelection() : null;
|
|
117
|
-
persistSelection(options.resumeSessionId, selection);
|
|
118
|
-
return {
|
|
119
|
-
agent: handle.agent,
|
|
120
|
-
created: false,
|
|
121
|
-
resumed: true,
|
|
122
|
-
sessionId: options.resumeSessionId,
|
|
123
|
-
dispose: async () => { try { await handle.dispose?.(); } catch { /* already gone */ } },
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
} catch (error) {
|
|
127
|
-
// A persisted host must be resumed: creating another one would break the
|
|
128
|
-
// existing members' direct-child adjacency.
|
|
129
|
-
throw new HostAgentError('E_HOST_AGENT', `Team Host Agent resume failed: ${error?.message ?? error}`, {
|
|
130
|
-
sessionId: options.resumeSessionId,
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
const roots = typeof agents.roots === 'function' ? agents.roots() : [];
|
|
135
|
-
if (roots.length > 0) {
|
|
136
|
-
// roots() 只保证注册顺序,不保证某个元素是「Human 的根会话」;优先挑一个
|
|
137
|
-
// 真正没有父会话、且不是 subagent 出来的根,避免把成员自己的父链当成
|
|
138
|
-
// Human 通道(那会让成员成为彼此的 parent)。
|
|
139
|
-
const rootAgent = roots.find((candidate) => {
|
|
140
|
-
const header = candidate?.session?.header;
|
|
141
|
-
if (header === undefined) return true;
|
|
142
|
-
return header.origin !== 'subagent' && header.parentSession === undefined;
|
|
143
|
-
}) ?? roots[0];
|
|
144
|
-
// 被选中的 Human 根会话若已归档,成员血缘会被 gate 永久拒绝——这条快路径
|
|
145
|
-
// 也必须解归档,否则 boot 期的 sweep 由于没有 hostSessionId 看不到它。
|
|
146
|
-
await unarchiveHost(rootAgent.id);
|
|
147
|
-
return { agent: rootAgent, created: false, sessionId: rootAgent.id, dispose: async () => {} };
|
|
148
126
|
}
|
|
149
127
|
|
|
150
128
|
const selection = typeof options.agentDefaultModel?.currentSelection === 'function'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@suifei/dsh-ai-team",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.91",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "文渊阁 (Wenyuan Pavilion) — AI Team mode for DeepSeek Harness: concurrent projects, owned tasks, threads, inbox and an append-only team ledger",
|
|
6
6
|
"license": "MIT",
|