dsh-taskbridge 1.7.1 → 1.8.1

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +21 -21
  3. package/README.md +733 -877
  4. package/THIRD_PARTY_NOTICES.txt +23 -23
  5. package/antigravity/agents/dsh-taskbridge.md +32 -0
  6. package/antigravity/skills/dsh-taskbridge/SKILL.md +51 -0
  7. package/codex/dsh-taskbridge.toml +181 -631
  8. package/cordis.patch.yml +6 -6
  9. package/docs/INTRODUCTION.md +122 -295
  10. package/docs/INTRODUCTION.zh.md +134 -279
  11. package/docs/antigravity-integration.md +42 -0
  12. package/docs/auth-contract.md +138 -138
  13. package/docs/claude-code.md +6 -11
  14. package/docs/codex-integration.md +2 -2
  15. package/docs/contracts-stage-b.md +443 -443
  16. package/docs/conversation-placement.md +7 -6
  17. package/docs/plugin-market.md +30 -30
  18. package/docs/redaction-contract.md +170 -170
  19. package/docs/releases/1.7.0.zh.md +11 -11
  20. package/docs/releases/1.8.0.zh.md +16 -0
  21. package/docs/releases/1.8.1.zh.md +24 -0
  22. package/docs/runtime-compatibility.md +1 -1
  23. package/docs/taskbridge-upgrade.md +28 -28
  24. package/docs/taskdispatch-upgrade.md +25 -25
  25. package/docs/workspace-contract.md +538 -538
  26. package/docs/workspace-grouping.md +126 -126
  27. package/icon.svg +1 -1
  28. package/lib/client.d.ts +804 -699
  29. package/lib/client.js +7 -7
  30. package/locale/en.json +1 -1
  31. package/locale/zh.json +1 -1
  32. package/package.json +150 -144
  33. package/src/antigravity/installer.js +1068 -0
  34. package/src/antigravity/seam.js +142 -0
  35. package/src/auth/fence.js +361 -361
  36. package/src/auth/grants.js +789 -789
  37. package/src/client/api.js +266 -3
  38. package/src/client/index.jsx +224 -11
  39. package/src/client/sidebar.jsx +862 -862
  40. package/src/client/styles.js +624 -624
  41. package/src/client/task-cleanup.js +429 -304
  42. package/src/client/task-records-table.jsx +217 -217
  43. package/src/client/task-view.js +307 -307
  44. package/src/client/workspace-groups.js +482 -482
  45. package/src/client/workspaces-browser.jsx +770 -770
  46. package/src/codex/agent-ownership.js +270 -270
  47. package/src/codex/installer.js +6 -0
  48. package/src/codex/toml.js +784 -784
  49. package/src/config/defaults.js +11 -15
  50. package/src/config/redact.js +245 -245
  51. package/src/config/store.js +186 -186
  52. package/src/credentials/seam.js +202 -202
  53. package/src/http/respond.js +6 -5
  54. package/src/http/user-plane.js +83 -1
  55. package/src/index.js +25 -0
  56. package/src/jobs/diagnostics.js +15 -15
  57. package/src/jobs/engine.js +1 -0
  58. package/src/jobs/history.js +1 -1
  59. package/src/jobs/plan-store.js +1103 -1020
  60. package/src/jobs/record-cleanup.js +452 -7
  61. package/src/jobs/review-note.js +63 -63
  62. package/src/jobs/task-descriptions.js +82 -82
  63. package/src/jobs/task-display.js +73 -73
  64. package/src/jobs/task-tree.js +786 -786
  65. package/src/jobs/title.js +134 -134
  66. package/src/mcp/continuation.js +317 -317
  67. package/src/mcp/server.js +34 -27
  68. package/src/mcp/wait.js +34 -15
  69. package/src/model/connectivity.js +371 -371
  70. package/src/model/discovery.js +582 -582
  71. package/src/model/native-catalog.js +68 -68
  72. package/src/model/openai-adapter.js +671 -671
  73. package/src/model/probe.js +264 -264
  74. package/src/model/providers.js +756 -756
  75. package/src/model/routing.js +321 -321
  76. package/src/runtime/agent-runner.js +1 -0
  77. package/src/runtime/permissions.js +1619 -1619
  78. package/src/runtime/question-call.js +162 -162
  79. package/src/runtime/questions.js +909 -909
  80. package/src/runtime/session-placement.js +36 -1
  81. package/src/runtime/worker-prompt.js +61 -55
  82. package/src/workspace/audit.js +775 -775
  83. package/src/workspace/preflight.js +424 -424
  84. package/src/workspace/worktrees.js +833 -679
@@ -1,482 +1,482 @@
1
- // dsh-delegate — the model behind the TaskBridge section of the sidebar.
2
- //
3
- // WHAT THE TREE IS
4
- // ----------------
5
- // The requirement is one row per DISPATCHED CHAIN, not one row per source directory:
6
- //
7
- // TaskBridge section — collapsible, no native record
8
- // └─ 项目(真实 SOURCE) project — collapsible
9
- // └─ 任务工作区(该链的 Workspace) task workspace — collapsible
10
- // └─ 该任务的原生对话 conversation — real (workspaceId, sessionId)
11
- //
12
- // So two independent new dispatches in the same project are TWO task workspaces, side by side;
13
- // a continuation keeps its parent's Workspace and Session and is therefore the SAME node with
14
- // one conversation row (deduplicated), never a second one.
15
- //
16
- // The tree is derived from what the plugin can PROVE:
17
- //
18
- // * the durable identity map (`/session-map` → `SessionStore`), read once per mount, and
19
- // * the task records the page already holds (`/tasks`), which name the chain of a run
20
- // (`continueFromTaskId`) and, for a dispatch the map has not written yet, its placement.
21
- //
22
- // Ownership is proof, never appearance: a conversation is the plugin's when the map (or its own
23
- // record) names that exact (session, workspace) pair. A plain Workspace, an empty one, the
24
- // managed worktree root, or a "mixed" one whose other conversations nobody recorded is NEVER
25
- // claimed by this tree — the native browser below it already shows them, with their own names,
26
- // order, menus and actions.
27
- //
28
- // This module is pure: no host call, no render, no clock. The same inputs always give the same
29
- // tree, which is what makes it testable without a browser.
30
-
31
- /** The plugin's own managed-worktree root directory name, below its data directory. */
32
- const MANAGED_ROOT_SEGMENT = 'worktrees';
33
-
34
- /** Sections this model can produce. */
35
- export const GROUP_KINDS = Object.freeze({
36
- /** The TaskBridge container: projects nest inside it. */
37
- container: 'container',
38
- /** One real source project. */
39
- project: 'project',
40
- });
41
-
42
- /**
43
- * Canonical key of one directory, for grouping only.
44
- *
45
- * The same rule `sessionPathKey` applies (trim, strip trailing separators, fold case on Windows)
46
- * and deliberately not a richer normaliser: two spellings this folds together must be two
47
- * spellings the session layer treats as one directory, or the tree would merge two projects the
48
- * harness keeps apart.
49
- *
50
- * The platform is read through {@link clientPlatform}, never through `process` directly: a
51
- * browser bundle has no `process`, so reading it would silently disable the fold.
52
- * @param {unknown} value - candidate path.
53
- * @returns {string} canonical key, or '' when the value is not a usable path.
54
- */
55
- export function sourcePathKey(value) {
56
- if (typeof value !== 'string') return '';
57
- const trimmed = value.trim();
58
- if (trimmed === '') return '';
59
- const stripped = trimmed.length > 1 ? trimmed.replace(/[\\/]+$/u, '') : trimmed;
60
- return clientPlatform() === 'win32' ? stripped.toLowerCase() : stripped;
61
- }
62
-
63
- /**
64
- * The platform string esbuild substitutes at build time (`--define`), as a NAMESPACED member
65
- * expression so the minifier cannot rename a local binding out of the substitution.
66
- */
67
- export const CLIENT_PLATFORM = globalThis.__DSH_TASKBRIDGE_CLIENT_PLATFORM__;
68
-
69
- /**
70
- * The platform this bundle runs on.
71
- * @returns {'win32' | 'posix'} the platform.
72
- */
73
- function clientPlatform() {
74
- if (typeof CLIENT_PLATFORM === 'string' && CLIENT_PLATFORM !== '') {
75
- return CLIENT_PLATFORM === 'win32' ? 'win32' : 'posix';
76
- }
77
- return typeof process !== 'undefined' && process.platform === 'win32' ? 'win32' : 'posix';
78
- }
79
-
80
- /**
81
- * Whether a path is the plugin's registered managed-worktree root itself.
82
- * @param {unknown} path - Workspace path.
83
- * @returns {boolean} true when it is the managed root.
84
- */
85
- export function isManagedRootPath(path) {
86
- if (typeof path !== 'string' || path.trim() === '') return false;
87
- const parts = path.trim().split(/[\\/]+/u).filter((part) => part !== '');
88
- return parts.length >= 3 && parts[parts.length - 1].toLowerCase() === MANAGED_ROOT_SEGMENT;
89
- }
90
-
91
- /**
92
- * A short, readable label for one path.
93
- * @param {string} path - directory path.
94
- * @returns {string} label (its last one or two segments).
95
- */
96
- export function projectNameOf(path) {
97
- const parts = String(path ?? '').split(/[\\/]+/u).filter((part) => part !== '');
98
- if (parts.length === 0) return '未命名项目';
99
- if (parts.length === 1) return parts[0];
100
- return parts.slice(-2).join('/');
101
- }
102
-
103
- /**
104
- * Read the host Workspaces snapshot, or `null` when it cannot be read synchronously.
105
- * @param {object | null | undefined} workspaces - the host `workspaces` service.
106
- * @returns {object | null} snapshot.
107
- */
108
- export function readWorkspacesSnapshot(workspaces) {
109
- const source = workspaces?.list;
110
- if (source === null || source === undefined || typeof source.getSnapshot !== 'function') return null;
111
- try {
112
- const snapshot = source.getSnapshot();
113
- if (snapshot === null || typeof snapshot !== 'object' || Array.isArray(snapshot)) return null;
114
- return snapshot;
115
- } catch {
116
- return null;
117
- }
118
- }
119
-
120
- /**
121
- * The host's Workspaces, normalized to the fields this model consumes.
122
- * @param {object | null} snapshot - `readWorkspacesSnapshot` result.
123
- * @returns {{ ready: boolean, items: object[], archivedSessionIds: string[], ungroupedSessionIds: string[] }} normalized view.
124
- */
125
- export function workspaceItems(snapshot) {
126
- if (snapshot === null || typeof snapshot !== 'object') {
127
- return { ready: false, items: [], archivedSessionIds: [], ungroupedSessionIds: [] };
128
- }
129
- const archived = new Set((Array.isArray(snapshot.archivedSessionIds) ? snapshot.archivedSessionIds : [])
130
- .filter((id) => typeof id === 'string' && id !== ''));
131
- const items = [];
132
- const placed = new Set();
133
- for (const entry of Array.isArray(snapshot.items) ? snapshot.items : []) {
134
- if (entry === null || typeof entry !== 'object') continue;
135
- const workspaceId = typeof entry.workspaceId === 'string' ? entry.workspaceId : '';
136
- if (workspaceId === '') continue;
137
- const sessionIds = (Array.isArray(entry.sessionIds) ? entry.sessionIds : [])
138
- .filter((id) => typeof id === 'string' && id !== '');
139
- for (const id of sessionIds) placed.add(id);
140
- items.push({
141
- workspaceId,
142
- title: typeof entry.title === 'string' ? entry.title : '',
143
- path: typeof entry.path === 'string' ? entry.path : '',
144
- sessionIds,
145
- });
146
- }
147
- const ungrouped = [];
148
- for (const id of Array.isArray(snapshot.ungroupedSessionIds) ? snapshot.ungroupedSessionIds : []) {
149
- if (typeof id === 'string' && id !== '' && !placed.has(id) && !ungrouped.includes(id)) ungrouped.push(id);
150
- }
151
- return {
152
- ready: snapshot.phase === 'ready',
153
- items,
154
- archivedSessionIds: [...archived],
155
- ungroupedSessionIds: ungrouped,
156
- };
157
- }
158
-
159
- /**
160
- * The identity map, narrowed to what grouping needs. A row that cannot name both a Session and
161
- * a Workspace proves nothing, so it is dropped here as well as on the host.
162
- * @param {unknown} rows - `GET /session-map` `sessions` array.
163
- * @returns {object[]} usable rows.
164
- */
165
- export function identityRows(rows) {
166
- const out = [];
167
- for (const row of Array.isArray(rows) ? rows : []) {
168
- if (row === null || typeof row !== 'object') continue;
169
- const sessionId = typeof row.sessionId === 'string' ? row.sessionId.trim() : '';
170
- const workspaceId = typeof row.workspaceId === 'string' ? row.workspaceId.trim() : '';
171
- if (sessionId === '' || workspaceId === '') continue;
172
- out.push({
173
- taskId: typeof row.taskId === 'string' ? row.taskId.trim() : '',
174
- sessionId,
175
- workspaceId,
176
- sourcePath: typeof row.sourcePath === 'string' ? row.sourcePath.trim() : '',
177
- executionPath: typeof row.executionPath === 'string' ? row.executionPath.trim() : '',
178
- isolation: typeof row.isolation === 'string' ? row.isolation : null,
179
- updatedAt: Number.isFinite(row.updatedAt) ? row.updatedAt : 0,
180
- });
181
- }
182
- return out;
183
- }
184
-
185
- /**
186
- * Task records indexed by the pair they prove, by task id, and by chain.
187
- *
188
- * Only a record that states BOTH a session and a workspace proves a placement; the rest are
189
- * display-only (a title). `chainOf` maps each task to the root of its continuation chain from
190
- * the records' own `continueFromTaskId` links, which is the identity the tree uses to keep one
191
- * task workspace per chain.
192
- * @param {object[]} tasks - `/tasks` rows.
193
- * @returns {{ titles, pairs, byTask, chainOf }} indexes.
194
- */
195
- export function sessionIndex(tasks) {
196
- const titles = new Map();
197
- const pairs = new Map();
198
- const byTask = new Map();
199
- const parentOf = new Map();
200
- const taskIds = new Set();
201
- for (const task of Array.isArray(tasks) ? tasks : []) {
202
- if (task === null || typeof task !== 'object') continue;
203
- const taskId = typeof task.taskId === 'string' ? task.taskId.trim() : '';
204
- if (taskId !== '') taskIds.add(taskId);
205
- const parent = typeof task.continueFromTaskId === 'string' ? task.continueFromTaskId.trim() : '';
206
- if (taskId !== '' && parent !== '' && parent !== taskId) parentOf.set(taskId, parent);
207
- const sessionId = [task.sessionId, task.execution?.sessionId, task.conversation?.sessionId]
208
- .find((value) => typeof value === 'string' && value.trim() !== '');
209
- const workspaceId = [task.execution?.workspaceId, task.workspaceId, task.conversation?.workspaceId]
210
- .find((value) => typeof value === 'string' && value.trim() !== '');
211
- const title = typeof task.title === 'string' ? task.title.trim() : '';
212
- const sourcePath = [task.sourcePath, task.execution?.sourcePath, task.conversation?.sourcePath]
213
- .find((value) => typeof value === 'string' && value.trim() !== '');
214
- const session = typeof sessionId === 'string' ? sessionId.trim() : '';
215
- if (session !== '' && typeof workspaceId === 'string' && workspaceId.trim() !== '') {
216
- const pair = {
217
- taskId,
218
- sessionId: session,
219
- workspaceId: workspaceId.trim(),
220
- sourcePath: typeof sourcePath === 'string' ? sourcePath.trim() : '',
221
- isolation: typeof task.isolation === 'string' ? task.isolation : null,
222
- // Records are the newest evidence available for a session the durable map has not
223
- // recorded yet.
224
- updatedAt: Number.isFinite(task.updatedAt) ? task.updatedAt : Number.MAX_SAFE_INTEGER,
225
- fromRecord: true,
226
- };
227
- const key = `${pair.workspaceId}\u0000${pair.sessionId}`;
228
- const previous = pairs.get(key);
229
- if (previous === undefined || pair.updatedAt >= previous.updatedAt) pairs.set(key, pair);
230
- if (taskId !== '') byTask.set(taskId, pair);
231
- }
232
- if (session !== '' && title !== '' && !titles.has(session)) titles.set(session, title);
233
- }
234
- const rootOf = (taskId) => {
235
- let current = taskId;
236
- const seen = new Set();
237
- while (parentOf.has(current) && !seen.has(current)) {
238
- seen.add(current);
239
- current = parentOf.get(current);
240
- }
241
- return current;
242
- };
243
- const chainOf = new Map();
244
- for (const taskId of taskIds) chainOf.set(taskId, rootOf(taskId));
245
- return { titles, pairs, byTask, chainOf };
246
- }
247
-
248
- /**
249
- * Display titles for sessions, derived from the task records.
250
- * @param {object[]} tasks - `/tasks` rows.
251
- * @returns {Map<string, string>} session id → title.
252
- */
253
- export function sessionTitles(tasks) {
254
- return sessionIndex(tasks).titles;
255
- }
256
-
257
- /**
258
- * Build the TaskBridge section.
259
- *
260
- * @param {object} input - `snapshot`, `identities`, `titles`, `tasks`.
261
- * @returns {{ ready, groups, standalone, ungrouped, archived, duplicates, ownCount,
262
- * conversationCount, workspaceCount, mixedCount, standaloneCount }} the model.
263
- */
264
- export function workspaceGroups(input = {}) {
265
- const host = workspaceItems(input.snapshot ?? null);
266
- const tasks = Array.isArray(input.tasks) ? input.tasks : [];
267
- const records = sessionIndex(tasks);
268
- const titles = input.titles instanceof Map ? input.titles : records.titles;
269
-
270
- // (Session, Workspace) → the newest proof. The durable map wins for a pair it names (it carries
271
- // the recorded source); a record fills a pair the map has not written yet.
272
- const byPair = new Map();
273
- for (const row of identityRows(input.identities)) {
274
- const key = `${row.workspaceId}\u0000${row.sessionId}`;
275
- const previous = byPair.get(key);
276
- if (previous === undefined || row.updatedAt >= previous.updatedAt) byPair.set(key, row);
277
- }
278
- for (const [key, pair] of records.pairs) {
279
- if (!byPair.has(key)) byPair.set(key, pair);
280
- }
281
- const bySession = new Map();
282
- for (const row of byPair.values()) {
283
- const previous = bySession.get(row.sessionId);
284
- if (previous === undefined || row.updatedAt > previous.updatedAt) bySession.set(row.sessionId, row);
285
- }
286
-
287
- /** Host Workspace by id, for a pair whose Workspace the snapshot may no longer list. */
288
- const hostById = new Map(host.items.map((item) => [item.workspaceId, item]));
289
-
290
- // ---- one node per (project, chain) ---------------------------------------
291
- // Keyed by project + chain root: two independent dispatches of one project are two nodes, and
292
- // every round of one chain is the same node.
293
- const nodes = new Map();
294
- /** Workspaces whose conversation set is fully explained by this plugin. */
295
- const fullyProven = new Set();
296
- for (const item of host.items) {
297
- const owned = item.sessionIds.filter((sessionId) => byPair.has(`${item.workspaceId}\u0000${sessionId}`));
298
- if (owned.length === 0) continue;
299
- if (owned.length === item.sessionIds.length && !isManagedRootPath(item.path)) fullyProven.add(item.workspaceId);
300
- if (isManagedRootPath(item.path) && owned.length > 0) fullyProven.add(item.workspaceId);
301
- }
302
- for (const item of host.items) {
303
- for (const sessionId of item.sessionIds) {
304
- const identity = byPair.get(`${item.workspaceId}\u0000${sessionId}`);
305
- if (identity === undefined) continue;
306
- const source = sourceOf(identity, item);
307
- const taskId = typeof identity.taskId === 'string' ? identity.taskId : '';
308
- // A pair the records do not index (a historical row) is its own chain: its task identity
309
- // is what the map recorded, and nothing joins it to another round.
310
- const chainId = (taskId !== '' && records.chainOf.get(taskId)) || (taskId !== '' ? taskId : `${source}\u0000${sessionId}`);
311
- const key = `${source}\u0000${chainId}`;
312
- if (!nodes.has(key)) {
313
- nodes.set(key, {
314
- key,
315
- chainId,
316
- source,
317
- projectName: projectNameOf(source),
318
- workspaceId: item.workspaceId,
319
- workspaceTitle: item.title !== '' ? item.title : projectNameOf(item.path),
320
- workspacePath: item.path,
321
- conversations: [],
322
- });
323
- }
324
- nodes.get(key).conversations.push(sessionRow(identity, titles, false));
325
- }
326
- }
327
-
328
- // ---- projects ------------------------------------------------------------
329
- const projects = new Map();
330
- for (const node of nodes.values()) {
331
- if (!projects.has(node.source)) {
332
- projects.set(node.source, {
333
- kind: GROUP_KINDS.project,
334
- key: node.source,
335
- name: node.projectName,
336
- path: node.source,
337
- workspaces: [],
338
- workspaceCount: 0,
339
- conversationCount: 0,
340
- });
341
- }
342
- const project = projects.get(node.source);
343
- project.workspaces.push({
344
- workspaceId: node.workspaceId,
345
- chainId: node.chainId,
346
- title: node.workspaceTitle,
347
- path: node.workspacePath,
348
- // A Workspace that holds a conversation nobody recorded is shown by the NATIVE browser
349
- // below; renaming or removing it as a whole would act on those conversations too.
350
- sharedWithOrdinary: !fullyProven.has(node.workspaceId),
351
- conversations: node.conversations
352
- .sort((left, right) => right.updatedAt - left.updatedAt),
353
- });
354
- }
355
-
356
- // One row per conversation across the whole section: a continuation keeps its parent's
357
- // Workspace AND Session, so this collapses it onto the node it already lives in.
358
- const seen = new Set();
359
- const duplicates = [];
360
- for (const project of projects.values()) {
361
- for (const workspace of project.workspaces) {
362
- const kept = [];
363
- for (const conversation of workspace.conversations) {
364
- if (seen.has(conversation.sessionId)) {
365
- duplicates.push({ sessionId: conversation.sessionId, workspaceId: workspace.workspaceId });
366
- continue;
367
- }
368
- seen.add(conversation.sessionId);
369
- kept.push(conversation);
370
- }
371
- workspace.conversations = kept;
372
- }
373
- // Stable order: task workspaces by their newest conversation, so the run just started is
374
- // the first row an operator sees.
375
- project.workspaces.sort((left, right) => {
376
- const leftAt = left.conversations[0]?.updatedAt ?? 0;
377
- const rightAt = right.conversations[0]?.updatedAt ?? 0;
378
- if (leftAt !== rightAt) return rightAt - leftAt;
379
- return left.workspaceId < right.workspaceId ? -1 : left.workspaceId > right.workspaceId ? 1 : 0;
380
- });
381
- project.workspaceCount = project.workspaces.length;
382
- project.conversationCount = project.workspaces.reduce((total, workspace) => total + workspace.conversations.length, 0);
383
- }
384
-
385
- const projectNodes = [...projects.values()].sort((left, right) => {
386
- const byName = String(left.name).localeCompare(String(right.name), 'zh-Hans-CN');
387
- return byName !== 0 ? byName : (left.key < right.key ? -1 : left.key > right.key ? 1 : 0);
388
- });
389
-
390
- // The section exists only when it holds a project: an empty TaskBridge row above the native
391
- // browser would be a claim the identity map does not make.
392
- const groups = [];
393
- if (projectNodes.length > 0) {
394
- groups.push({
395
- kind: GROUP_KINDS.container,
396
- key: 'taskbridge',
397
- name: 'TaskBridge',
398
- workspaceId: null,
399
- projects: projectNodes,
400
- workspaceCount: projectNodes.reduce((total, project) => total + project.workspaceCount, 0),
401
- conversationCount: projectNodes.reduce((total, project) => total + project.conversationCount, 0),
402
- });
403
- }
404
-
405
- // ---- what the native browser keeps --------------------------------------
406
- // Reported for measurement and for tests. The native browser shows these itself; this section
407
- // never repeats them, and never hides one.
408
- const standalone = host.items
409
- .filter((item) => !fullyProven.has(item.workspaceId))
410
- .map((item) => ({ workspaceId: item.workspaceId, title: item.title, path: item.path }));
411
-
412
- const ungrouped = host.ungroupedSessionIds
413
- .filter((sessionId) => !seen.has(sessionId))
414
- .map((sessionId) => ({ sessionId, title: titles.get(sessionId) ?? '', owned: bySession.has(sessionId) }));
415
- for (const entry of ungrouped) seen.add(entry.sessionId);
416
-
417
- const archived = host.archivedSessionIds
418
- .map((sessionId) => ({ sessionId, identity: bySession.get(sessionId) ?? null }))
419
- .filter((entry) => entry.identity !== null)
420
- .filter((entry) => !seen.has(entry.sessionId))
421
- .map((entry) => ({
422
- sessionId: entry.sessionId,
423
- title: titles.get(entry.sessionId) ?? '',
424
- workspaceId: entry.identity.workspaceId,
425
- taskId: entry.identity.taskId !== '' ? entry.identity.taskId : null,
426
- }));
427
- for (const entry of archived) seen.add(entry.sessionId);
428
-
429
- return {
430
- ready: host.ready,
431
- groups,
432
- standalone,
433
- ungrouped,
434
- archived,
435
- duplicates,
436
- /** Dispatched conversations shown in the section (one row each). */
437
- conversationCount: projectNodes.reduce((total, project) => total + project.conversationCount, 0),
438
- /** Task workspaces shown in the section. */
439
- workspaceCount: projectNodes.reduce((total, project) => total + project.workspaceCount, 0),
440
- /** Native Workspaces whose conversations are all this plugin's. */
441
- ownCount: projectNodes.length > 0 ? projectNodes.reduce((total, project) => total + project.workspaceCount, 0) : 0,
442
- /** Native Workspaces the section does NOT claim (they also hold unrecorded conversations). */
443
- mixedCount: standalone.filter((entry) => hostById.has(entry.workspaceId) && byPair.size > 0
444
- && (hostById.get(entry.workspaceId)?.sessionIds ?? []).some((sessionId) => bySession.has(sessionId))).length,
445
- standaloneCount: standalone.length,
446
- };
447
- }
448
-
449
- /**
450
- * The real source project of one dispatched conversation: the recorded SOURCE path, else the
451
- * directory the run really worked in, else the Workspace's own path.
452
- * @param {object} identity - the proof row.
453
- * @param {object} item - the host Workspace it lives in.
454
- * @returns {string} canonical project key.
455
- */
456
- function sourceOf(identity, item) {
457
- const source = sourcePathKey(identity.sourcePath);
458
- if (source !== '') return source;
459
- const execution = sourcePathKey(identity.executionPath);
460
- if (execution !== '') return execution;
461
- const fallback = sourcePathKey(item.path);
462
- return fallback === '' ? `workspace:${item.workspaceId}` : fallback;
463
- }
464
-
465
- /**
466
- * One conversation row.
467
- * @param {object} identity - the proof row (native ids included).
468
- * @param {Map<string, string>} titles - session titles.
469
- * @param {boolean} archived - whether DSH archived it.
470
- * @returns {object} conversation row.
471
- */
472
- function sessionRow(identity, titles, archived) {
473
- return {
474
- sessionId: identity.sessionId,
475
- workspaceId: identity.workspaceId,
476
- taskId: typeof identity.taskId === 'string' && identity.taskId !== '' ? identity.taskId : null,
477
- title: titles.get(identity.sessionId) ?? (typeof identity.title === 'string' ? identity.title : ''),
478
- isolation: identity.isolation,
479
- updatedAt: identity.updatedAt,
480
- archived,
481
- };
482
- }
1
+ // dsh-delegate — the model behind the TaskBridge section of the sidebar.
2
+ //
3
+ // WHAT THE TREE IS
4
+ // ----------------
5
+ // The requirement is one row per DISPATCHED CHAIN, not one row per source directory:
6
+ //
7
+ // TaskBridge section — collapsible, no native record
8
+ // └─ 项目(真实 SOURCE) project — collapsible
9
+ // └─ 任务工作区(该链的 Workspace) task workspace — collapsible
10
+ // └─ 该任务的原生对话 conversation — real (workspaceId, sessionId)
11
+ //
12
+ // So two independent new dispatches in the same project are TWO task workspaces, side by side;
13
+ // a continuation keeps its parent's Workspace and Session and is therefore the SAME node with
14
+ // one conversation row (deduplicated), never a second one.
15
+ //
16
+ // The tree is derived from what the plugin can PROVE:
17
+ //
18
+ // * the durable identity map (`/session-map` → `SessionStore`), read once per mount, and
19
+ // * the task records the page already holds (`/tasks`), which name the chain of a run
20
+ // (`continueFromTaskId`) and, for a dispatch the map has not written yet, its placement.
21
+ //
22
+ // Ownership is proof, never appearance: a conversation is the plugin's when the map (or its own
23
+ // record) names that exact (session, workspace) pair. A plain Workspace, an empty one, the
24
+ // managed worktree root, or a "mixed" one whose other conversations nobody recorded is NEVER
25
+ // claimed by this tree — the native browser below it already shows them, with their own names,
26
+ // order, menus and actions.
27
+ //
28
+ // This module is pure: no host call, no render, no clock. The same inputs always give the same
29
+ // tree, which is what makes it testable without a browser.
30
+
31
+ /** The plugin's own managed-worktree root directory name, below its data directory. */
32
+ const MANAGED_ROOT_SEGMENT = 'worktrees';
33
+
34
+ /** Sections this model can produce. */
35
+ export const GROUP_KINDS = Object.freeze({
36
+ /** The TaskBridge container: projects nest inside it. */
37
+ container: 'container',
38
+ /** One real source project. */
39
+ project: 'project',
40
+ });
41
+
42
+ /**
43
+ * Canonical key of one directory, for grouping only.
44
+ *
45
+ * The same rule `sessionPathKey` applies (trim, strip trailing separators, fold case on Windows)
46
+ * and deliberately not a richer normaliser: two spellings this folds together must be two
47
+ * spellings the session layer treats as one directory, or the tree would merge two projects the
48
+ * harness keeps apart.
49
+ *
50
+ * The platform is read through {@link clientPlatform}, never through `process` directly: a
51
+ * browser bundle has no `process`, so reading it would silently disable the fold.
52
+ * @param {unknown} value - candidate path.
53
+ * @returns {string} canonical key, or '' when the value is not a usable path.
54
+ */
55
+ export function sourcePathKey(value) {
56
+ if (typeof value !== 'string') return '';
57
+ const trimmed = value.trim();
58
+ if (trimmed === '') return '';
59
+ const stripped = trimmed.length > 1 ? trimmed.replace(/[\\/]+$/u, '') : trimmed;
60
+ return clientPlatform() === 'win32' ? stripped.toLowerCase() : stripped;
61
+ }
62
+
63
+ /**
64
+ * The platform string esbuild substitutes at build time (`--define`), as a NAMESPACED member
65
+ * expression so the minifier cannot rename a local binding out of the substitution.
66
+ */
67
+ export const CLIENT_PLATFORM = globalThis.__DSH_TASKBRIDGE_CLIENT_PLATFORM__;
68
+
69
+ /**
70
+ * The platform this bundle runs on.
71
+ * @returns {'win32' | 'posix'} the platform.
72
+ */
73
+ function clientPlatform() {
74
+ if (typeof CLIENT_PLATFORM === 'string' && CLIENT_PLATFORM !== '') {
75
+ return CLIENT_PLATFORM === 'win32' ? 'win32' : 'posix';
76
+ }
77
+ return typeof process !== 'undefined' && process.platform === 'win32' ? 'win32' : 'posix';
78
+ }
79
+
80
+ /**
81
+ * Whether a path is the plugin's registered managed-worktree root itself.
82
+ * @param {unknown} path - Workspace path.
83
+ * @returns {boolean} true when it is the managed root.
84
+ */
85
+ export function isManagedRootPath(path) {
86
+ if (typeof path !== 'string' || path.trim() === '') return false;
87
+ const parts = path.trim().split(/[\\/]+/u).filter((part) => part !== '');
88
+ return parts.length >= 3 && parts[parts.length - 1].toLowerCase() === MANAGED_ROOT_SEGMENT;
89
+ }
90
+
91
+ /**
92
+ * A short, readable label for one path.
93
+ * @param {string} path - directory path.
94
+ * @returns {string} label (its last one or two segments).
95
+ */
96
+ export function projectNameOf(path) {
97
+ const parts = String(path ?? '').split(/[\\/]+/u).filter((part) => part !== '');
98
+ if (parts.length === 0) return '未命名项目';
99
+ if (parts.length === 1) return parts[0];
100
+ return parts.slice(-2).join('/');
101
+ }
102
+
103
+ /**
104
+ * Read the host Workspaces snapshot, or `null` when it cannot be read synchronously.
105
+ * @param {object | null | undefined} workspaces - the host `workspaces` service.
106
+ * @returns {object | null} snapshot.
107
+ */
108
+ export function readWorkspacesSnapshot(workspaces) {
109
+ const source = workspaces?.list;
110
+ if (source === null || source === undefined || typeof source.getSnapshot !== 'function') return null;
111
+ try {
112
+ const snapshot = source.getSnapshot();
113
+ if (snapshot === null || typeof snapshot !== 'object' || Array.isArray(snapshot)) return null;
114
+ return snapshot;
115
+ } catch {
116
+ return null;
117
+ }
118
+ }
119
+
120
+ /**
121
+ * The host's Workspaces, normalized to the fields this model consumes.
122
+ * @param {object | null} snapshot - `readWorkspacesSnapshot` result.
123
+ * @returns {{ ready: boolean, items: object[], archivedSessionIds: string[], ungroupedSessionIds: string[] }} normalized view.
124
+ */
125
+ export function workspaceItems(snapshot) {
126
+ if (snapshot === null || typeof snapshot !== 'object') {
127
+ return { ready: false, items: [], archivedSessionIds: [], ungroupedSessionIds: [] };
128
+ }
129
+ const archived = new Set((Array.isArray(snapshot.archivedSessionIds) ? snapshot.archivedSessionIds : [])
130
+ .filter((id) => typeof id === 'string' && id !== ''));
131
+ const items = [];
132
+ const placed = new Set();
133
+ for (const entry of Array.isArray(snapshot.items) ? snapshot.items : []) {
134
+ if (entry === null || typeof entry !== 'object') continue;
135
+ const workspaceId = typeof entry.workspaceId === 'string' ? entry.workspaceId : '';
136
+ if (workspaceId === '') continue;
137
+ const sessionIds = (Array.isArray(entry.sessionIds) ? entry.sessionIds : [])
138
+ .filter((id) => typeof id === 'string' && id !== '');
139
+ for (const id of sessionIds) placed.add(id);
140
+ items.push({
141
+ workspaceId,
142
+ title: typeof entry.title === 'string' ? entry.title : '',
143
+ path: typeof entry.path === 'string' ? entry.path : '',
144
+ sessionIds,
145
+ });
146
+ }
147
+ const ungrouped = [];
148
+ for (const id of Array.isArray(snapshot.ungroupedSessionIds) ? snapshot.ungroupedSessionIds : []) {
149
+ if (typeof id === 'string' && id !== '' && !placed.has(id) && !ungrouped.includes(id)) ungrouped.push(id);
150
+ }
151
+ return {
152
+ ready: snapshot.phase === 'ready',
153
+ items,
154
+ archivedSessionIds: [...archived],
155
+ ungroupedSessionIds: ungrouped,
156
+ };
157
+ }
158
+
159
+ /**
160
+ * The identity map, narrowed to what grouping needs. A row that cannot name both a Session and
161
+ * a Workspace proves nothing, so it is dropped here as well as on the host.
162
+ * @param {unknown} rows - `GET /session-map` `sessions` array.
163
+ * @returns {object[]} usable rows.
164
+ */
165
+ export function identityRows(rows) {
166
+ const out = [];
167
+ for (const row of Array.isArray(rows) ? rows : []) {
168
+ if (row === null || typeof row !== 'object') continue;
169
+ const sessionId = typeof row.sessionId === 'string' ? row.sessionId.trim() : '';
170
+ const workspaceId = typeof row.workspaceId === 'string' ? row.workspaceId.trim() : '';
171
+ if (sessionId === '' || workspaceId === '') continue;
172
+ out.push({
173
+ taskId: typeof row.taskId === 'string' ? row.taskId.trim() : '',
174
+ sessionId,
175
+ workspaceId,
176
+ sourcePath: typeof row.sourcePath === 'string' ? row.sourcePath.trim() : '',
177
+ executionPath: typeof row.executionPath === 'string' ? row.executionPath.trim() : '',
178
+ isolation: typeof row.isolation === 'string' ? row.isolation : null,
179
+ updatedAt: Number.isFinite(row.updatedAt) ? row.updatedAt : 0,
180
+ });
181
+ }
182
+ return out;
183
+ }
184
+
185
+ /**
186
+ * Task records indexed by the pair they prove, by task id, and by chain.
187
+ *
188
+ * Only a record that states BOTH a session and a workspace proves a placement; the rest are
189
+ * display-only (a title). `chainOf` maps each task to the root of its continuation chain from
190
+ * the records' own `continueFromTaskId` links, which is the identity the tree uses to keep one
191
+ * task workspace per chain.
192
+ * @param {object[]} tasks - `/tasks` rows.
193
+ * @returns {{ titles, pairs, byTask, chainOf }} indexes.
194
+ */
195
+ export function sessionIndex(tasks) {
196
+ const titles = new Map();
197
+ const pairs = new Map();
198
+ const byTask = new Map();
199
+ const parentOf = new Map();
200
+ const taskIds = new Set();
201
+ for (const task of Array.isArray(tasks) ? tasks : []) {
202
+ if (task === null || typeof task !== 'object') continue;
203
+ const taskId = typeof task.taskId === 'string' ? task.taskId.trim() : '';
204
+ if (taskId !== '') taskIds.add(taskId);
205
+ const parent = typeof task.continueFromTaskId === 'string' ? task.continueFromTaskId.trim() : '';
206
+ if (taskId !== '' && parent !== '' && parent !== taskId) parentOf.set(taskId, parent);
207
+ const sessionId = [task.sessionId, task.execution?.sessionId, task.conversation?.sessionId]
208
+ .find((value) => typeof value === 'string' && value.trim() !== '');
209
+ const workspaceId = [task.execution?.workspaceId, task.workspaceId, task.conversation?.workspaceId]
210
+ .find((value) => typeof value === 'string' && value.trim() !== '');
211
+ const title = typeof task.title === 'string' ? task.title.trim() : '';
212
+ const sourcePath = [task.sourcePath, task.execution?.sourcePath, task.conversation?.sourcePath]
213
+ .find((value) => typeof value === 'string' && value.trim() !== '');
214
+ const session = typeof sessionId === 'string' ? sessionId.trim() : '';
215
+ if (session !== '' && typeof workspaceId === 'string' && workspaceId.trim() !== '') {
216
+ const pair = {
217
+ taskId,
218
+ sessionId: session,
219
+ workspaceId: workspaceId.trim(),
220
+ sourcePath: typeof sourcePath === 'string' ? sourcePath.trim() : '',
221
+ isolation: typeof task.isolation === 'string' ? task.isolation : null,
222
+ // Records are the newest evidence available for a session the durable map has not
223
+ // recorded yet.
224
+ updatedAt: Number.isFinite(task.updatedAt) ? task.updatedAt : Number.MAX_SAFE_INTEGER,
225
+ fromRecord: true,
226
+ };
227
+ const key = `${pair.workspaceId}\u0000${pair.sessionId}`;
228
+ const previous = pairs.get(key);
229
+ if (previous === undefined || pair.updatedAt >= previous.updatedAt) pairs.set(key, pair);
230
+ if (taskId !== '') byTask.set(taskId, pair);
231
+ }
232
+ if (session !== '' && title !== '' && !titles.has(session)) titles.set(session, title);
233
+ }
234
+ const rootOf = (taskId) => {
235
+ let current = taskId;
236
+ const seen = new Set();
237
+ while (parentOf.has(current) && !seen.has(current)) {
238
+ seen.add(current);
239
+ current = parentOf.get(current);
240
+ }
241
+ return current;
242
+ };
243
+ const chainOf = new Map();
244
+ for (const taskId of taskIds) chainOf.set(taskId, rootOf(taskId));
245
+ return { titles, pairs, byTask, chainOf };
246
+ }
247
+
248
+ /**
249
+ * Display titles for sessions, derived from the task records.
250
+ * @param {object[]} tasks - `/tasks` rows.
251
+ * @returns {Map<string, string>} session id → title.
252
+ */
253
+ export function sessionTitles(tasks) {
254
+ return sessionIndex(tasks).titles;
255
+ }
256
+
257
+ /**
258
+ * Build the TaskBridge section.
259
+ *
260
+ * @param {object} input - `snapshot`, `identities`, `titles`, `tasks`.
261
+ * @returns {{ ready, groups, standalone, ungrouped, archived, duplicates, ownCount,
262
+ * conversationCount, workspaceCount, mixedCount, standaloneCount }} the model.
263
+ */
264
+ export function workspaceGroups(input = {}) {
265
+ const host = workspaceItems(input.snapshot ?? null);
266
+ const tasks = Array.isArray(input.tasks) ? input.tasks : [];
267
+ const records = sessionIndex(tasks);
268
+ const titles = input.titles instanceof Map ? input.titles : records.titles;
269
+
270
+ // (Session, Workspace) → the newest proof. The durable map wins for a pair it names (it carries
271
+ // the recorded source); a record fills a pair the map has not written yet.
272
+ const byPair = new Map();
273
+ for (const row of identityRows(input.identities)) {
274
+ const key = `${row.workspaceId}\u0000${row.sessionId}`;
275
+ const previous = byPair.get(key);
276
+ if (previous === undefined || row.updatedAt >= previous.updatedAt) byPair.set(key, row);
277
+ }
278
+ for (const [key, pair] of records.pairs) {
279
+ if (!byPair.has(key)) byPair.set(key, pair);
280
+ }
281
+ const bySession = new Map();
282
+ for (const row of byPair.values()) {
283
+ const previous = bySession.get(row.sessionId);
284
+ if (previous === undefined || row.updatedAt > previous.updatedAt) bySession.set(row.sessionId, row);
285
+ }
286
+
287
+ /** Host Workspace by id, for a pair whose Workspace the snapshot may no longer list. */
288
+ const hostById = new Map(host.items.map((item) => [item.workspaceId, item]));
289
+
290
+ // ---- one node per (project, chain) ---------------------------------------
291
+ // Keyed by project + chain root: two independent dispatches of one project are two nodes, and
292
+ // every round of one chain is the same node.
293
+ const nodes = new Map();
294
+ /** Workspaces whose conversation set is fully explained by this plugin. */
295
+ const fullyProven = new Set();
296
+ for (const item of host.items) {
297
+ const owned = item.sessionIds.filter((sessionId) => byPair.has(`${item.workspaceId}\u0000${sessionId}`));
298
+ if (owned.length === 0) continue;
299
+ if (owned.length === item.sessionIds.length && !isManagedRootPath(item.path)) fullyProven.add(item.workspaceId);
300
+ if (isManagedRootPath(item.path) && owned.length > 0) fullyProven.add(item.workspaceId);
301
+ }
302
+ for (const item of host.items) {
303
+ for (const sessionId of item.sessionIds) {
304
+ const identity = byPair.get(`${item.workspaceId}\u0000${sessionId}`);
305
+ if (identity === undefined) continue;
306
+ const source = sourceOf(identity, item);
307
+ const taskId = typeof identity.taskId === 'string' ? identity.taskId : '';
308
+ // A pair the records do not index (a historical row) is its own chain: its task identity
309
+ // is what the map recorded, and nothing joins it to another round.
310
+ const chainId = (taskId !== '' && records.chainOf.get(taskId)) || (taskId !== '' ? taskId : `${source}\u0000${sessionId}`);
311
+ const key = `${source}\u0000${chainId}`;
312
+ if (!nodes.has(key)) {
313
+ nodes.set(key, {
314
+ key,
315
+ chainId,
316
+ source,
317
+ projectName: projectNameOf(source),
318
+ workspaceId: item.workspaceId,
319
+ workspaceTitle: item.title !== '' ? item.title : projectNameOf(item.path),
320
+ workspacePath: item.path,
321
+ conversations: [],
322
+ });
323
+ }
324
+ nodes.get(key).conversations.push(sessionRow(identity, titles, false));
325
+ }
326
+ }
327
+
328
+ // ---- projects ------------------------------------------------------------
329
+ const projects = new Map();
330
+ for (const node of nodes.values()) {
331
+ if (!projects.has(node.source)) {
332
+ projects.set(node.source, {
333
+ kind: GROUP_KINDS.project,
334
+ key: node.source,
335
+ name: node.projectName,
336
+ path: node.source,
337
+ workspaces: [],
338
+ workspaceCount: 0,
339
+ conversationCount: 0,
340
+ });
341
+ }
342
+ const project = projects.get(node.source);
343
+ project.workspaces.push({
344
+ workspaceId: node.workspaceId,
345
+ chainId: node.chainId,
346
+ title: node.workspaceTitle,
347
+ path: node.workspacePath,
348
+ // A Workspace that holds a conversation nobody recorded is shown by the NATIVE browser
349
+ // below; renaming or removing it as a whole would act on those conversations too.
350
+ sharedWithOrdinary: !fullyProven.has(node.workspaceId),
351
+ conversations: node.conversations
352
+ .sort((left, right) => right.updatedAt - left.updatedAt),
353
+ });
354
+ }
355
+
356
+ // One row per conversation across the whole section: a continuation keeps its parent's
357
+ // Workspace AND Session, so this collapses it onto the node it already lives in.
358
+ const seen = new Set();
359
+ const duplicates = [];
360
+ for (const project of projects.values()) {
361
+ for (const workspace of project.workspaces) {
362
+ const kept = [];
363
+ for (const conversation of workspace.conversations) {
364
+ if (seen.has(conversation.sessionId)) {
365
+ duplicates.push({ sessionId: conversation.sessionId, workspaceId: workspace.workspaceId });
366
+ continue;
367
+ }
368
+ seen.add(conversation.sessionId);
369
+ kept.push(conversation);
370
+ }
371
+ workspace.conversations = kept;
372
+ }
373
+ // Stable order: task workspaces by their newest conversation, so the run just started is
374
+ // the first row an operator sees.
375
+ project.workspaces.sort((left, right) => {
376
+ const leftAt = left.conversations[0]?.updatedAt ?? 0;
377
+ const rightAt = right.conversations[0]?.updatedAt ?? 0;
378
+ if (leftAt !== rightAt) return rightAt - leftAt;
379
+ return left.workspaceId < right.workspaceId ? -1 : left.workspaceId > right.workspaceId ? 1 : 0;
380
+ });
381
+ project.workspaceCount = project.workspaces.length;
382
+ project.conversationCount = project.workspaces.reduce((total, workspace) => total + workspace.conversations.length, 0);
383
+ }
384
+
385
+ const projectNodes = [...projects.values()].sort((left, right) => {
386
+ const byName = String(left.name).localeCompare(String(right.name), 'zh-Hans-CN');
387
+ return byName !== 0 ? byName : (left.key < right.key ? -1 : left.key > right.key ? 1 : 0);
388
+ });
389
+
390
+ // The section exists only when it holds a project: an empty TaskBridge row above the native
391
+ // browser would be a claim the identity map does not make.
392
+ const groups = [];
393
+ if (projectNodes.length > 0) {
394
+ groups.push({
395
+ kind: GROUP_KINDS.container,
396
+ key: 'taskbridge',
397
+ name: 'TaskBridge',
398
+ workspaceId: null,
399
+ projects: projectNodes,
400
+ workspaceCount: projectNodes.reduce((total, project) => total + project.workspaceCount, 0),
401
+ conversationCount: projectNodes.reduce((total, project) => total + project.conversationCount, 0),
402
+ });
403
+ }
404
+
405
+ // ---- what the native browser keeps --------------------------------------
406
+ // Reported for measurement and for tests. The native browser shows these itself; this section
407
+ // never repeats them, and never hides one.
408
+ const standalone = host.items
409
+ .filter((item) => !fullyProven.has(item.workspaceId))
410
+ .map((item) => ({ workspaceId: item.workspaceId, title: item.title, path: item.path }));
411
+
412
+ const ungrouped = host.ungroupedSessionIds
413
+ .filter((sessionId) => !seen.has(sessionId))
414
+ .map((sessionId) => ({ sessionId, title: titles.get(sessionId) ?? '', owned: bySession.has(sessionId) }));
415
+ for (const entry of ungrouped) seen.add(entry.sessionId);
416
+
417
+ const archived = host.archivedSessionIds
418
+ .map((sessionId) => ({ sessionId, identity: bySession.get(sessionId) ?? null }))
419
+ .filter((entry) => entry.identity !== null)
420
+ .filter((entry) => !seen.has(entry.sessionId))
421
+ .map((entry) => ({
422
+ sessionId: entry.sessionId,
423
+ title: titles.get(entry.sessionId) ?? '',
424
+ workspaceId: entry.identity.workspaceId,
425
+ taskId: entry.identity.taskId !== '' ? entry.identity.taskId : null,
426
+ }));
427
+ for (const entry of archived) seen.add(entry.sessionId);
428
+
429
+ return {
430
+ ready: host.ready,
431
+ groups,
432
+ standalone,
433
+ ungrouped,
434
+ archived,
435
+ duplicates,
436
+ /** Dispatched conversations shown in the section (one row each). */
437
+ conversationCount: projectNodes.reduce((total, project) => total + project.conversationCount, 0),
438
+ /** Task workspaces shown in the section. */
439
+ workspaceCount: projectNodes.reduce((total, project) => total + project.workspaceCount, 0),
440
+ /** Native Workspaces whose conversations are all this plugin's. */
441
+ ownCount: projectNodes.length > 0 ? projectNodes.reduce((total, project) => total + project.workspaceCount, 0) : 0,
442
+ /** Native Workspaces the section does NOT claim (they also hold unrecorded conversations). */
443
+ mixedCount: standalone.filter((entry) => hostById.has(entry.workspaceId) && byPair.size > 0
444
+ && (hostById.get(entry.workspaceId)?.sessionIds ?? []).some((sessionId) => bySession.has(sessionId))).length,
445
+ standaloneCount: standalone.length,
446
+ };
447
+ }
448
+
449
+ /**
450
+ * The real source project of one dispatched conversation: the recorded SOURCE path, else the
451
+ * directory the run really worked in, else the Workspace's own path.
452
+ * @param {object} identity - the proof row.
453
+ * @param {object} item - the host Workspace it lives in.
454
+ * @returns {string} canonical project key.
455
+ */
456
+ function sourceOf(identity, item) {
457
+ const source = sourcePathKey(identity.sourcePath);
458
+ if (source !== '') return source;
459
+ const execution = sourcePathKey(identity.executionPath);
460
+ if (execution !== '') return execution;
461
+ const fallback = sourcePathKey(item.path);
462
+ return fallback === '' ? `workspace:${item.workspaceId}` : fallback;
463
+ }
464
+
465
+ /**
466
+ * One conversation row.
467
+ * @param {object} identity - the proof row (native ids included).
468
+ * @param {Map<string, string>} titles - session titles.
469
+ * @param {boolean} archived - whether DSH archived it.
470
+ * @returns {object} conversation row.
471
+ */
472
+ function sessionRow(identity, titles, archived) {
473
+ return {
474
+ sessionId: identity.sessionId,
475
+ workspaceId: identity.workspaceId,
476
+ taskId: typeof identity.taskId === 'string' && identity.taskId !== '' ? identity.taskId : null,
477
+ title: titles.get(identity.sessionId) ?? (typeof identity.title === 'string' ? identity.title : ''),
478
+ isolation: identity.isolation,
479
+ updatedAt: identity.updatedAt,
480
+ archived,
481
+ };
482
+ }