@suifei/dsh-ai-team 0.4.89 → 0.4.90
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 +37 -0
- package/index.js +71 -49
- 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 +44 -3
- package/lib/runtime/host-agent.js +39 -61
- package/package.json +1 -1
package/lib/ledger/fold.js
CHANGED
|
@@ -43,8 +43,12 @@ 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 看过空看板之后,没有新进展就不再定时推进。
|
|
46
50
|
*/
|
|
47
|
-
export const FOLD_STATE_VERSION =
|
|
51
|
+
export const FOLD_STATE_VERSION = 6;
|
|
48
52
|
|
|
49
53
|
/**
|
|
50
54
|
* 投影封顶:大规模/长迭代账本下,stream 与每个 Thread 的 entries 不随事件数
|
|
@@ -665,9 +669,34 @@ function applyFollow(state, event, payload, following) {
|
|
|
665
669
|
* - 成员在空看板上建卡:有 freeWave 则消耗它(这是 Human 指令的那一批),否则 `pmoRoundsUsed + 1`;
|
|
666
670
|
* - `project/pmo-capped`:本份预算已告知暂停(只告知一次)。
|
|
667
671
|
* 预算只由 Human 续,成员不能给自己续。闸门在 service(createTask / pmoGate)。
|
|
672
|
+
*
|
|
673
|
+
* 新进展(`pmoNews`,pmoTick 的空看板推进只在为 true 时放行):
|
|
674
|
+
* - Human 作者事件、Task 进入终态(accepted / cancelled / deleted)→ true;
|
|
675
|
+
* - PMO 被成功唤醒去看局面(pmo_continue / wave_done / human_message)→ false。
|
|
676
|
+
* r09:两个项目目标完成后,PMO 每 10 分钟被推进一次、拉 5 名成员开会,再发一条
|
|
677
|
+
* 「状态不变」,35 分钟 8 次推进 / 40 次会议唤醒;wave_done 与 pmo_continue 还在同一秒重复。
|
|
668
678
|
*/
|
|
679
|
+
const PMO_NEWS_TASK_EVENTS = new Set(['task/accepted', 'task/cancelled', 'task/deleted']);
|
|
680
|
+
const PMO_SEEN_REASONS = new Set(['pmo_continue', 'wave_done', 'human_message']);
|
|
681
|
+
|
|
669
682
|
export function applyEvent(state, event) {
|
|
670
|
-
const next = applyEventCore(state, event);
|
|
683
|
+
const next = applyBudget(state, event, applyEventCore(state, event));
|
|
684
|
+
if (next === state) return next;
|
|
685
|
+
if (event.actor?.kind === 'human' || PMO_NEWS_TASK_EVENTS.has(event.type)) {
|
|
686
|
+
return next.pmoNews === true ? next : { ...next, pmoNews: true };
|
|
687
|
+
}
|
|
688
|
+
if (event.type === 'wake/delivered' && PMO_SEEN_REASONS.has(event.payload?.reason)) {
|
|
689
|
+
return next.pmoNews === false ? next : { ...next, pmoNews: false };
|
|
690
|
+
}
|
|
691
|
+
return next;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/** PMO 定时推进是否有新进展可看(缺省视为有:老账本与首个空看板)。 */
|
|
695
|
+
export function pmoHasNews(state) {
|
|
696
|
+
return state.pmoNews !== false;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function applyBudget(state, event, next) {
|
|
671
700
|
if (next === state) return next;
|
|
672
701
|
if (event.type === 'project/pmo-capped') return { ...next, pmoCapped: true };
|
|
673
702
|
if (event.actor?.kind === 'human') {
|
|
@@ -861,7 +890,19 @@ function applyEventCore(state, event) {
|
|
|
861
890
|
case 'team/host-bound': {
|
|
862
891
|
const sessionId = stringOrNull(payload.sessionId);
|
|
863
892
|
if (sessionId === null) return advance(state, event);
|
|
864
|
-
|
|
893
|
+
const previous = stringOrNull(state.hostSessionId);
|
|
894
|
+
if (previous === null || previous === sessionId) return advance(state, event, { hostSessionId: sessionId });
|
|
895
|
+
// PMO 会话换了:已有会话的成员是旧 PMO 的子会话,新 PMO 向它们投递会以
|
|
896
|
+
// UNAUTHORIZED 被拒(不在其子树)。标记 routeStale,下次唤醒即在新 PMO 下、
|
|
897
|
+
// 项目工作区里重建一次(与换模型同一条路径);绑定新会话时标记自然清除。
|
|
898
|
+
const members = {};
|
|
899
|
+
for (const [id, member] of Object.entries(state.members ?? {})) {
|
|
900
|
+
members[id] = member?.kind === 'agent' && typeof member.sessionId === 'string' && member.sessionId !== ''
|
|
901
|
+
&& member.status !== MEMBER_STATUS.RETIRED
|
|
902
|
+
? { ...member, routeStale: true }
|
|
903
|
+
: member;
|
|
904
|
+
}
|
|
905
|
+
return advance(state, event, { hostSessionId: sessionId, members });
|
|
865
906
|
}
|
|
866
907
|
|
|
867
908
|
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.90",
|
|
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",
|