@suifei/dsh-ai-team 0.4.88 → 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.
@@ -40,8 +40,15 @@ import { isReviewCandidate } from '../domain/policy.js';
40
40
  * 同时更新 ownerId。旧 checkpoint(v2)里仍带着旧语义的投影,必须作废重折,
41
41
  * 否则升级后「前 owner 仍通过 isLead」「接管继任者仍被排除在验收候选外」会
42
42
  * 被零尾放永久保留。
43
+ *
44
+ * v4:新增有界自主投影 `pmoFreeWave` / `pmoRoundsUsed` / `pmoCapped`(见 applyEvent);
45
+ * 旧 checkpoint 没有这些字段,作废重折。
46
+ *
47
+ * v5:`team/host-bound` 换 PMO 会话时把已有会话的成员标为 routeStale(每项目一个 PMO)。
48
+ *
49
+ * v6:新增 `pmoNews`(见 applyEvent):PMO 看过空看板之后,没有新进展就不再定时推进。
43
50
  */
44
- export const FOLD_STATE_VERSION = 3;
51
+ export const FOLD_STATE_VERSION = 6;
45
52
 
46
53
  /**
47
54
  * 投影封顶:大规模/长迭代账本下,stream 与每个 Thread 的 entries 不随事件数
@@ -655,8 +662,62 @@ function applyFollow(state, event, payload, following) {
655
662
 
656
663
  /**
657
664
  * 应用一个事件。未知事件、已应用事件、非法结构一律返回原 state 引用。
665
+ *
666
+ * 有界自主(对齐官方 /goal 的 maxGoalRounds)。一「轮」= 看板清空后由成员开出新一批 Task
667
+ * ——不论是 PMO 定时推进还是主理人回收结果后自己续开,口径只有这一个:
668
+ * - 任何 Human 作者的事件:`pmoFreeWave = true`、`pmoRoundsUsed = 0`、`pmoCapped = false`;
669
+ * - 成员在空看板上建卡:有 freeWave 则消耗它(这是 Human 指令的那一批),否则 `pmoRoundsUsed + 1`;
670
+ * - `project/pmo-capped`:本份预算已告知暂停(只告知一次)。
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 还在同一秒重复。
658
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
+
659
682
  export function applyEvent(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) {
700
+ if (next === state) return next;
701
+ if (event.type === 'project/pmo-capped') return { ...next, pmoCapped: true };
702
+ if (event.actor?.kind === 'human') {
703
+ if (next.pmoFreeWave === true && (next.pmoRoundsUsed ?? 0) === 0 && next.pmoCapped !== true) return next;
704
+ return { ...next, pmoFreeWave: true, pmoRoundsUsed: 0, pmoCapped: false };
705
+ }
706
+ if (event.type === 'task/created' && event.actor?.kind === 'agent' && isBoardIdle(state)) {
707
+ if (state.pmoFreeWave !== false) return { ...next, pmoFreeWave: false };
708
+ return { ...next, pmoRoundsUsed: (state.pmoRoundsUsed ?? 0) + 1 };
709
+ }
710
+ return next;
711
+ }
712
+
713
+ const BOARD_ACTIVE_STATUSES = new Set(['pending', 'in_progress', 'pending_acceptance']);
714
+
715
+ /** 看板上没有进行中的 Task(与 pmoTick 的 active 口径一致)。 */
716
+ export function isBoardIdle(state) {
717
+ return !Object.values(state.tasks ?? {}).some((task) => task.tombstone !== true && BOARD_ACTIVE_STATUSES.has(task.status));
718
+ }
719
+
720
+ function applyEventCore(state, event) {
660
721
  if (!isPlainObject(state)) throw new TypeError('applyEvent: state must be a plain object');
661
722
  if (!isEventLike(event)) return state;
662
723
  if (event.seq <= (Number.isSafeInteger(state.lastSeq) ? state.lastSeq : 0)) return state;
@@ -670,6 +731,10 @@ export function applyEvent(state, event) {
670
731
  return advance(state, event, { projects: { ...state.projects, [project.id]: project } });
671
732
  }
672
733
 
734
+ case 'project/pmo-capped':
735
+ // 计数在 applyEvent 外层;这里只推进 seq,使事件「已应用」。
736
+ return advance(state, event);
737
+
673
738
  case 'project/pmo-updated': {
674
739
  const projectId = textOr(payload.projectId, event.projectId);
675
740
  const project = state.projects?.[projectId];
@@ -825,7 +890,19 @@ export function applyEvent(state, event) {
825
890
  case 'team/host-bound': {
826
891
  const sessionId = stringOrNull(payload.sessionId);
827
892
  if (sessionId === null) return advance(state, event);
828
- return advance(state, event, { hostSessionId: sessionId });
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 });
829
906
  }
830
907
 
831
908
  case 'member/config-updated': {
@@ -1,19 +1,20 @@
1
1
  /**
2
- * Resolve the parent Agent that owns member activations.
2
+ * Resolve one project's PMO session: the parent Agent of that project's members.
3
3
  *
4
- * Preference order:
5
- * 1. an existing live root Agent (the Human's current session);
6
- * 2. a lazily created Team Host Agent (the Human owner has no
7
- * session, e.g. the workbench was opened without a chat).
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
- * The Host Agent is a runtime root, not a product member: it owns no Task,
10
- * has no product decision power.
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 Host Agent must never be archived. Members are subagent
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 the host
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
- if (live !== undefined) {
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: live, created: false, resumed: true, sessionId: options.resumeSessionId, dispose: async () => {} };
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.88",
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",