dsh-workflow-canvas 1.6.0 → 1.6.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.
- package/client.js +489 -112
- package/docs/MECHANISM.md +2 -2
- package/docs/MECHANISM.zh.md +2 -2
- package/index.js +238 -1098
- package/lib/graph.js +1068 -0
- package/lib/telemetry.js +250 -0
- package/package.json +2 -1
package/lib/telemetry.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-token run summary snapshots and long-term disk/memory LRU hygiene
|
|
3
|
+
* for dsh-workflow-canvas.
|
|
4
|
+
*/
|
|
5
|
+
import { mkdirSync, readdirSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
|
|
8
|
+
export const MAX_STORED_SESSIONS = 100;
|
|
9
|
+
export const MAX_LAST_RUNS = 50;
|
|
10
|
+
export const MAX_CHILD_SLOTS = 400;
|
|
11
|
+
export const STALE_CANVAS_FILE_MS = 14 * 24 * 60 * 60 * 1000; // 14 days
|
|
12
|
+
const STALE_TMP_FILE_MS = 60 * 60 * 1000; // 1 hour for orphaned atomic-write temps
|
|
13
|
+
const CLEANUP_THROTTLE_MS = 60 * 60 * 1000; // 1 hour per workspace
|
|
14
|
+
|
|
15
|
+
const lastCleanupByDir = new Map();
|
|
16
|
+
let atomicTmpSeq = 0;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Sanitize a sessionId for safe use as a filename stem inside `.dsh/canvas/`.
|
|
20
|
+
* Leaves standard `session-<uuid>` ids untouched while preventing path traversal
|
|
21
|
+
* (`..`, `/`, `\`) and Windows-illegal characters (`:`, `*`, `?`, `"`, `<`, `>`, `|`).
|
|
22
|
+
*/
|
|
23
|
+
export function safeSessionFileBase(sessionId) {
|
|
24
|
+
const raw = String(sessionId || '').trim();
|
|
25
|
+
if (!raw) return '';
|
|
26
|
+
const cleaned = raw.replace(/[^A-Za-z0-9._-]/g, '_').replace(/\.\.+/g, '_').replace(/^\.+/, '_');
|
|
27
|
+
return cleaned.slice(0, 180);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Atomically write UTF-8 text to `targetPath` via a unique same-directory temp file
|
|
32
|
+
* and `renameSync`, preventing torn JSON reads during concurrent polling or crashes.
|
|
33
|
+
* Falls back to direct write only on Windows transient rename locks (`EPERM`/`EBUSY`/`EACCES`),
|
|
34
|
+
* always cleaning up the temp file on failure.
|
|
35
|
+
*/
|
|
36
|
+
export function writeFileAtomic(targetPath, content) {
|
|
37
|
+
const dir = dirname(targetPath);
|
|
38
|
+
mkdirSync(dir, { recursive: true });
|
|
39
|
+
atomicTmpSeq = (atomicTmpSeq + 1) % 1_000_000;
|
|
40
|
+
const tmp = targetPath + '.' + process.pid + '.' + atomicTmpSeq + '.tmp';
|
|
41
|
+
try {
|
|
42
|
+
writeFileSync(tmp, content, 'utf8');
|
|
43
|
+
try {
|
|
44
|
+
renameSync(tmp, targetPath);
|
|
45
|
+
} catch (renameErr) {
|
|
46
|
+
const code = renameErr && renameErr.code;
|
|
47
|
+
try { unlinkSync(tmp); } catch (_u) { /* best-effort cleanup */ }
|
|
48
|
+
if (code === 'EPERM' || code === 'EBUSY' || code === 'EACCES') {
|
|
49
|
+
writeFileSync(targetPath, content, 'utf8');
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
throw renameErr;
|
|
53
|
+
}
|
|
54
|
+
} catch (err) {
|
|
55
|
+
try { unlinkSync(tmp); } catch (_u) { /* best-effort cleanup */ }
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Evict oldest insertion-order entries from a Map until size <= limit. */
|
|
61
|
+
export function pruneMapLRU(map, limit) {
|
|
62
|
+
if (!(map instanceof Map) || !Number.isFinite(limit) || limit < 0) return 0;
|
|
63
|
+
let evicted = 0;
|
|
64
|
+
while (map.size > limit) {
|
|
65
|
+
const oldest = map.keys().next().value;
|
|
66
|
+
if (oldest === undefined) break;
|
|
67
|
+
map.delete(oldest);
|
|
68
|
+
evicted += 1;
|
|
69
|
+
}
|
|
70
|
+
return evicted;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Insert or refresh a key at the tail of a Map and enforce the LRU size cap. */
|
|
74
|
+
export function touchMapLRU(map, key, value, limit) {
|
|
75
|
+
if (!(map instanceof Map)) return;
|
|
76
|
+
if (map.has(key)) map.delete(key);
|
|
77
|
+
map.set(key, value);
|
|
78
|
+
if (Number.isFinite(limit) && limit >= 0) pruneMapLRU(map, limit);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Periodically remove orphaned `.dsh/canvas/*.json` and `*.run.json` files older than
|
|
83
|
+
* `maxAgeMs` so workspaces do not accumulate stale telemetry files over months of use.
|
|
84
|
+
* Throttled to once per hour per directory unless `opts.force === true`.
|
|
85
|
+
*/
|
|
86
|
+
export function cleanupStaleCanvasFiles(dir, opts) {
|
|
87
|
+
if (!dir || typeof dir !== 'string') return 0;
|
|
88
|
+
const o = opts || {};
|
|
89
|
+
const now = typeof o.now === 'number' ? o.now : Date.now();
|
|
90
|
+
const maxAgeMs = typeof o.maxAgeMs === 'number' ? o.maxAgeMs : STALE_CANVAS_FILE_MS;
|
|
91
|
+
const activeIds = o.activeIds instanceof Set ? o.activeIds : new Set();
|
|
92
|
+
if (!o.force) {
|
|
93
|
+
const prev = lastCleanupByDir.get(dir) || 0;
|
|
94
|
+
if (now - prev < CLEANUP_THROTTLE_MS) return 0;
|
|
95
|
+
}
|
|
96
|
+
touchMapLRU(lastCleanupByDir, dir, now, 64);
|
|
97
|
+
let removed = 0;
|
|
98
|
+
try {
|
|
99
|
+
const names = readdirSync(dir);
|
|
100
|
+
for (const name of names) {
|
|
101
|
+
if (name.endsWith('.tmp')) {
|
|
102
|
+
const full = join(dir, name);
|
|
103
|
+
try {
|
|
104
|
+
const st = statSync(full);
|
|
105
|
+
if (st.isFile() && now - st.mtimeMs > STALE_TMP_FILE_MS) {
|
|
106
|
+
unlinkSync(full);
|
|
107
|
+
removed += 1;
|
|
108
|
+
}
|
|
109
|
+
} catch (_e) { /* best-effort per file */ }
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (!name.endsWith('.json')) continue;
|
|
113
|
+
const base = name.endsWith('.run.json')
|
|
114
|
+
? name.slice(0, -'.run.json'.length)
|
|
115
|
+
: name.slice(0, -'.json'.length);
|
|
116
|
+
if (activeIds.has(base)) continue;
|
|
117
|
+
const full = join(dir, name);
|
|
118
|
+
try {
|
|
119
|
+
const st = statSync(full);
|
|
120
|
+
if (st.isFile() && now - st.mtimeMs > maxAgeMs) {
|
|
121
|
+
unlinkSync(full);
|
|
122
|
+
removed += 1;
|
|
123
|
+
}
|
|
124
|
+
} catch (_e) { /* best-effort per file */ }
|
|
125
|
+
}
|
|
126
|
+
} catch (_e) { /* directory absent or unreadable */ }
|
|
127
|
+
return removed;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Build a zero-token structured run summary snapshot from in-memory executor state.
|
|
132
|
+
* Published inside `.run.json` for the sidebar UI (never injected into model prompts).
|
|
133
|
+
*/
|
|
134
|
+
export function buildRunSummarySnapshot(st, opts) {
|
|
135
|
+
if (!st || typeof st !== 'object') return null;
|
|
136
|
+
const o = opts || {};
|
|
137
|
+
const now = typeof o.now === 'number' ? o.now : Date.now();
|
|
138
|
+
const status = typeof o.status === 'string' && o.status ? o.status : 'running';
|
|
139
|
+
const waitingLead = o.waitingLead || st.waitingLead || null;
|
|
140
|
+
const haltedReason = typeof o.haltedReason === 'string' && o.haltedReason
|
|
141
|
+
? o.haltedReason
|
|
142
|
+
: (st.haltedReason || '');
|
|
143
|
+
const isTerminal = status === 'done' || status === 'halted';
|
|
144
|
+
const startedAt = typeof st.startedAt === 'number' ? st.startedAt : now;
|
|
145
|
+
const endedAt = isTerminal ? (typeof st.endedAt === 'number' ? st.endedAt : now) : null;
|
|
146
|
+
const nodeStatsMap = st.nodeStats && typeof st.nodeStats === 'object' ? st.nodeStats : {};
|
|
147
|
+
const prunedSet = st.pruned instanceof Set ? st.pruned : new Set(Array.isArray(st.pruned) ? st.pruned : []);
|
|
148
|
+
const decisions = st.decisions && typeof st.decisions === 'object' ? st.decisions : {};
|
|
149
|
+
const failedMap = st.failed && typeof st.failed === 'object' ? st.failed : {};
|
|
150
|
+
const emptyMap = st.emptyOutputs && typeof st.emptyOutputs === 'object' ? st.emptyOutputs : {};
|
|
151
|
+
const nodeMeta = st.nodeMeta && typeof st.nodeMeta === 'object' ? st.nodeMeta : {};
|
|
152
|
+
const orderIds = Array.isArray(st.orderIds) && st.orderIds.length
|
|
153
|
+
? st.orderIds
|
|
154
|
+
: Object.keys(nodeStatsMap);
|
|
155
|
+
|
|
156
|
+
const nodes = [];
|
|
157
|
+
for (const id of orderIds) {
|
|
158
|
+
const meta = nodeMeta[id] || {};
|
|
159
|
+
const stat = nodeStatsMap[id] || null;
|
|
160
|
+
const dec = decisions[id] || null;
|
|
161
|
+
const fail = failedMap[id] || null;
|
|
162
|
+
const isPruned = prunedSet.has(id);
|
|
163
|
+
const isWaiting = Boolean(status === 'need-lead' && waitingLead && waitingLead.id === id);
|
|
164
|
+
|
|
165
|
+
let entryStatus = 'pending';
|
|
166
|
+
let durationMs = stat && typeof stat.durationMs === 'number' ? stat.durationMs : 0;
|
|
167
|
+
let entryStartedAt = stat && typeof stat.startedAt === 'number' ? stat.startedAt : null;
|
|
168
|
+
let entryEndedAt = stat && typeof stat.endedAt === 'number' ? stat.endedAt : null;
|
|
169
|
+
|
|
170
|
+
if (isPruned && (!stat || stat.status === 'pending' || stat.status === 'pruned')) {
|
|
171
|
+
entryStatus = 'pruned';
|
|
172
|
+
} else if (isWaiting) {
|
|
173
|
+
entryStatus = 'waiting';
|
|
174
|
+
} else if (stat && stat.status) {
|
|
175
|
+
entryStatus = stat.status;
|
|
176
|
+
if (status === 'halted' && (entryStatus === 'running' || entryStatus === 'waiting')) {
|
|
177
|
+
entryStatus = 'halted';
|
|
178
|
+
if (entryStartedAt && !entryEndedAt) {
|
|
179
|
+
durationMs += Math.max(0, now - entryStartedAt);
|
|
180
|
+
entryEndedAt = now;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
} else if (fail) {
|
|
184
|
+
entryStatus = 'failed';
|
|
185
|
+
} else if (emptyMap[id]) {
|
|
186
|
+
entryStatus = 'empty';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
let reason = (stat && typeof stat.reason === 'string' ? stat.reason : '') || (fail && fail.reason) || '';
|
|
190
|
+
if (!reason && dec) {
|
|
191
|
+
const matched = Array.isArray(dec.branchLabels) && dec.branchLabels.length
|
|
192
|
+
? dec.branchLabels.join(', ')
|
|
193
|
+
: (dec.branchLabel || dec.branch || '');
|
|
194
|
+
const decReason = typeof dec.reason === 'string' ? dec.reason.trim() : '';
|
|
195
|
+
reason = matched ? (decReason ? matched + ' — ' + decReason : matched) : decReason;
|
|
196
|
+
}
|
|
197
|
+
if (reason.length > 160) reason = reason.slice(0, 159) + '…';
|
|
198
|
+
|
|
199
|
+
nodes.push({
|
|
200
|
+
id,
|
|
201
|
+
label: (stat && stat.label) || meta.label || (st.labels && st.labels[id]) || id,
|
|
202
|
+
kind: (stat && stat.kind) || meta.kind || 'agent',
|
|
203
|
+
status: entryStatus,
|
|
204
|
+
startedAt: entryStartedAt,
|
|
205
|
+
endedAt: entryEndedAt,
|
|
206
|
+
durationMs,
|
|
207
|
+
runs: stat && typeof stat.runs === 'number' ? stat.runs : 0,
|
|
208
|
+
model: (stat && stat.model) || meta.model || '',
|
|
209
|
+
fallbackUsed: Boolean(stat && stat.fallbackUsed),
|
|
210
|
+
sessionContinued: Boolean(stat && stat.sessionContinued),
|
|
211
|
+
childId: (stat && stat.childId) || '',
|
|
212
|
+
reason,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const loopMeta = Array.isArray(st.loopMeta) ? st.loopMeta : [];
|
|
217
|
+
const loopCounts = st.loopCounts && typeof st.loopCounts === 'object' ? st.loopCounts : {};
|
|
218
|
+
const loopHistory = st.loopHistory && typeof st.loopHistory === 'object' ? st.loopHistory : {};
|
|
219
|
+
const exhaustedKeys = new Set(Array.isArray(st.loopExhausted) ? st.loopExhausted.map((x) => x && x.edgeKey).filter(Boolean) : []);
|
|
220
|
+
const loops = [];
|
|
221
|
+
for (const lm of loopMeta) {
|
|
222
|
+
const used = Number(loopCounts[lm.key]) || 0;
|
|
223
|
+
const rawRounds = Array.isArray(loopHistory[lm.key]) ? loopHistory[lm.key] : [];
|
|
224
|
+
const exhausted = exhaustedKeys.has(lm.key);
|
|
225
|
+
if (used === 0 && rawRounds.length === 0 && !exhausted) continue;
|
|
226
|
+
loops.push({
|
|
227
|
+
fromLabel: lm.fromLabel,
|
|
228
|
+
toLabel: lm.toLabel,
|
|
229
|
+
used,
|
|
230
|
+
max: lm.max,
|
|
231
|
+
exhausted,
|
|
232
|
+
rounds: rawRounds.map((r) => ({
|
|
233
|
+
round: r.round,
|
|
234
|
+
reason: typeof r.reason === 'string' && r.reason.length > 140 ? r.reason.slice(0, 139) + '…' : (r.reason || ''),
|
|
235
|
+
})),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
status,
|
|
241
|
+
startedAt,
|
|
242
|
+
endedAt,
|
|
243
|
+
haltedReason,
|
|
244
|
+
maxParallel: typeof st.maxParallel === 'number' ? st.maxParallel : 4,
|
|
245
|
+
totalLoops: typeof st.totalLoops === 'number' ? st.totalLoops : 0,
|
|
246
|
+
reused: Array.isArray(st.reusedIds) ? st.reusedIds.slice() : [],
|
|
247
|
+
nodes,
|
|
248
|
+
loops,
|
|
249
|
+
};
|
|
250
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-workflow-canvas",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "A visual multi-agent workflow canvas for DeepSeek Harness — parallel subagents, conditional branching, controlled retry loops, Lead gates & zero-token run summaries in the sidebar.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
15
|
"index.js",
|
|
16
|
+
"lib/*.js",
|
|
16
17
|
"client.js",
|
|
17
18
|
"cordis.patch.yml",
|
|
18
19
|
"locale/*.json",
|