claude-code-session-manager 0.126.0 → 0.128.0
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/bin/cli.cjs +71 -9
- package/bin/supervise.cjs +84 -0
- package/dist/assets/{AgentLibrary-bzeuZN4F.js → AgentLibrary-cNQU5YWy.js} +1 -1
- package/dist/assets/{Button-8dWfDDuQ.js → Button-9dIPAOBg.js} +1 -1
- package/dist/assets/{DataModel-D4ybBPZN.js → DataModel-DrDJ6ysw.js} +1 -1
- package/dist/assets/{History-hsztE6yh.js → History-4rrDdWNP.js} +1 -1
- package/dist/assets/{Hooks-CRmZMQaW.js → Hooks-Bj_jF5Pa.js} +1 -1
- package/dist/assets/{Library-Czt3Bojd.js → Library-D-Wsxrh7.js} +1 -1
- package/dist/assets/{ListDetail-Cr6Ct_WX.js → ListDetail-D7Om0H8j.js} +1 -1
- package/dist/assets/{McpServers-CeXJ2bng.js → McpServers-CbIbSd5o.js} +1 -1
- package/dist/assets/{Memory-caujO8O3.js → Memory-DgQlvQrO.js} +1 -1
- package/dist/assets/{Panel-D64i9tGz.js → Panel-DZHiD8Ar.js} +1 -1
- package/dist/assets/{Permissions-DpWOIrvr.js → Permissions-CfzduF4B.js} +1 -1
- package/dist/assets/{Plugins-Bd1ekP5P.js → Plugins-D-AG5nAo.js} +2 -2
- package/dist/assets/{ProvenanceBadge-B96A_uZf.js → ProvenanceBadge-B9JSEjll.js} +1 -1
- package/dist/assets/{SaveBar-D0Y2t-Ao.js → SaveBar-mVKig6hL.js} +1 -1
- package/dist/assets/{Scheduler-Y5_YbNrC.js → Scheduler-DFTCnh60.js} +1 -1
- package/dist/assets/{ScopeSwitcher-BmWA_dP9.js → ScopeSwitcher-s8J-yr7Z.js} +1 -1
- package/dist/assets/{Settings-Bcf65bpL.js → Settings-BTmxRMaP.js} +1 -1
- package/dist/assets/{SkillReferenceGraph-BeSNXloA.js → SkillReferenceGraph-C5lKqf5y.js} +1 -1
- package/dist/assets/{Skills-DwnQKJVc.js → Skills-CxOacq0k.js} +1 -1
- package/dist/assets/{SystemPrompt-Dih6SCmL.js → SystemPrompt-BCWg5FAz.js} +1 -1
- package/dist/assets/{TagLibrary-BZ-Kp10P.js → TagLibrary-BnF0_PkG.js} +1 -1
- package/dist/assets/{TiptapBody-C2nEUmF7.js → TiptapBody-C2Iu6rZd.js} +1 -1
- package/dist/assets/{Toggle-Dw8-zNQo.js → Toggle-C-oD66BG.js} +1 -1
- package/dist/assets/{ViewTabs-CoYPKGN-.js → ViewTabs-Cs4v7vLY.js} +1 -1
- package/dist/assets/{historyTotals-BbKcNpV7.js → historyTotals-CbRSLD8f.js} +1 -1
- package/dist/assets/{index-BnKaYERU.css → index-DtdWBGrI.css} +1 -1
- package/dist/assets/index-DzY_GXYY.js +3081 -0
- package/dist/assets/{settingsSchema-6doG_61l.js → settingsSchema-bSBfHuan.js} +1 -1
- package/dist/assets/{skillFrontmatter-BYMYWDfb.js → skillFrontmatter-BEseWm8r.js} +1 -1
- package/dist/assets/{useAgentsDirWatch-Cqabjieh.js → useAgentsDirWatch-CdyHxKCI.js} +1 -1
- package/dist/index.html +2 -2
- package/dist-web/app.css +1 -1
- package/dist-web/app.js +343 -341
- package/package.json +1 -1
- package/plugins/session-manager-dev/skills/develop/SKILL.md +3 -2
- package/scripts/scheduler-mcp-server.cjs +209 -7
- package/src/main/build-info.json +4 -4
- package/src/main/chatRunner.cjs +37 -4
- package/src/main/crashDiagnostics.cjs +83 -8
- package/src/main/health.cjs +16 -5
- package/src/main/index.cjs +46 -0
- package/src/main/ipcSchemas.cjs +107 -0
- package/src/main/lib/chatBootRecovery.cjs +136 -0
- package/src/main/lib/chatInflight.cjs +88 -0
- package/src/main/lib/claude/agentModelResolve.cjs +26 -7
- package/src/main/lib/claude/personaToolScope.cjs +40 -2
- package/src/main/lib/demo-video-kit/capture.cjs +113 -0
- package/src/main/lib/demo-video-kit/character.cjs +97 -0
- package/src/main/lib/demo-video-kit/checks.cjs +150 -0
- package/src/main/lib/demo-video-kit/frames.cjs +200 -0
- package/src/main/lib/demo-video-kit/seedKit.cjs +52 -0
- package/src/main/lib/demo-video-kit/sound.cjs +312 -0
- package/src/main/lib/epic/epicDelegationStats.cjs +1 -0
- package/src/main/lib/epic/epicSweepValidator.cjs +60 -6
- package/src/main/lib/epic/epicValidationHook.cjs +31 -3
- package/src/main/lib/feedback-remote/macroAdminRoutes.cjs +71 -15
- package/src/main/lib/feedback-remote/projectHomeAdminRoutes.cjs +185 -34
- package/src/main/lib/feedback-remote/remoteIpcDispatch.cjs +15 -0
- package/src/main/lib/mcpToolCatalog.cjs +93 -14
- package/src/main/lib/opsOwnership.cjs +9 -5
- package/src/main/lib/persona-macro/builtinMacros.cjs +30 -5
- package/src/main/lib/persona-macro/macroJobDispatch.cjs +116 -0
- package/src/main/lib/persona-macro/macroJobIpc.cjs +50 -0
- package/src/main/lib/persona-macro/macroJobStatus.cjs +112 -0
- package/src/main/lib/persona-macro/macroLibrary.cjs +73 -3
- package/src/main/lib/persona-macro/shippedPersonaSeeds.cjs +100 -0
- package/src/main/lib/prd/prdCreate.cjs +17 -2
- package/src/main/lib/prd/prdFrontmatter.cjs +7 -2
- package/src/main/lib/process/childWithLog.cjs +38 -95
- package/src/main/lib/process/memoryScope.cjs +140 -0
- package/src/main/lib/process/ozonePlatform.cjs +15 -0
- package/src/main/lib/process/procIdentity.cjs +10 -3
- package/src/main/lib/process/scopeOomPolicy.cjs +69 -0
- package/src/main/lib/project-pages/pagesLibrary.cjs +171 -0
- package/src/main/lib/project-pages/playerBridge.cjs +129 -0
- package/src/main/lib/scheduler/cpuSample.cjs +147 -0
- package/src/main/lib/scheduler/definitionOfDone.cjs +40 -0
- package/src/main/lib/scheduler/jobLeftovers.cjs +204 -0
- package/src/main/lib/scheduler/jobSupervisorRecord.cjs +2 -1
- package/src/main/lib/scheduler/loadGate.cjs +116 -45
- package/src/main/lib/scheduler/queueStore.cjs +5 -2
- package/src/main/lib/scheduler/scheduleJobSchema.cjs +2 -0
- package/src/main/lib/scheduler/schedulerBatch.cjs +10 -3
- package/src/main/lib/scheduler/schedulerConfig.cjs +7 -2
- package/src/main/lib/smfileHeaders.cjs +11 -3
- package/src/main/lib/telemetry/crashTelemetry.cjs +8 -4
- package/src/main/projectPages.cjs +107 -30
- package/src/main/promptSessionsIpc.cjs +131 -0
- package/src/main/scheduler/budget.cjs +31 -0
- package/src/main/scheduler/prdParser.cjs +2 -0
- package/src/main/scheduler/prdPaths.cjs +54 -0
- package/src/main/scheduler/reviewLadder.cjs +17 -0
- package/src/main/scheduler/starvation.cjs +22 -0
- package/src/main/scheduler.cjs +548 -77
- package/src/main/seedSchedulerMcp.cjs +10 -1
- package/src/main/templates/PRD_AUTHORING.md +5 -2
- package/src/main/transcripts.cjs +8 -0
- package/src/main/usage.cjs +4 -2
- package/src/main/webRemote.cjs +3 -1
- package/src/preload/api.d.ts +113 -7
- package/src/preload/apiSurface.json +70 -0
- package/src/preload/index.cjs +13 -0
- package/src/seed/agents/demo-video-builder.md +141 -43
- package/src/seed/agents/project-home-builder.md +26 -13
- package/dist/assets/index-DgPUERhe.js +0 -3079
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Inline postMessage bridge injected next to the CSP so Project Home can drive
|
|
3
|
+
// a sandboxed (no same-origin) demo video. Replies go to event.source only.
|
|
4
|
+
// Must stay clean under validateDemoVideoHtml's NETWORK_CHECKS.
|
|
5
|
+
|
|
6
|
+
const BRIDGE_JS = `(() => {
|
|
7
|
+
var timer = null;
|
|
8
|
+
var flag = false;
|
|
9
|
+
var muted = false;
|
|
10
|
+
const demo = () => { return window.smDemo || null; }
|
|
11
|
+
const num = (v, d) => { return typeof v === 'number' && isFinite(v) ? v : d; }
|
|
12
|
+
const call = (name, arg) => {
|
|
13
|
+
var d = demo();
|
|
14
|
+
if (d && typeof d[name] === 'function') { d[name](arg); return true; }
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const media = () => { return document.querySelectorAll('audio,video'); }
|
|
18
|
+
const duration = () => {
|
|
19
|
+
var d = demo();
|
|
20
|
+
if (d) {
|
|
21
|
+
var v = typeof d.duration === 'function' ? d.duration() : d.duration;
|
|
22
|
+
if (typeof v === 'number' && isFinite(v) && v > 0) return v;
|
|
23
|
+
}
|
|
24
|
+
var m = document.querySelector('meta[name="sm-demo-duration"]');
|
|
25
|
+
if (m) { var n = Number(m.getAttribute('content')); if (isFinite(n) && n > 0) return n; }
|
|
26
|
+
var els = media();
|
|
27
|
+
for (var i = 0; i < els.length; i++) { if (isFinite(els[i].duration) && els[i].duration > 0) return els[i].duration; }
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
const now = () => {
|
|
31
|
+
var d = demo();
|
|
32
|
+
if (d) {
|
|
33
|
+
if (typeof d.getTime === 'function') return num(d.getTime(), 0);
|
|
34
|
+
if (typeof d.t === 'number') return d.t;
|
|
35
|
+
if (typeof d.time === 'number') return d.time;
|
|
36
|
+
}
|
|
37
|
+
var el = media()[0];
|
|
38
|
+
return el ? num(el.currentTime, 0) : 0;
|
|
39
|
+
}
|
|
40
|
+
const playing = () => {
|
|
41
|
+
var d = demo();
|
|
42
|
+
if (d) {
|
|
43
|
+
if (typeof d.isPlaying === 'function') return !!d.isPlaying();
|
|
44
|
+
if (typeof d.playing === 'boolean') return d.playing;
|
|
45
|
+
}
|
|
46
|
+
return flag;
|
|
47
|
+
}
|
|
48
|
+
const scenes = () => {
|
|
49
|
+
var d = demo();
|
|
50
|
+
var out = [];
|
|
51
|
+
var src = d && Array.isArray(d.scenes) ? d.scenes : [];
|
|
52
|
+
for (var i = 0; i < src.length; i++) {
|
|
53
|
+
var s = src[i] || {};
|
|
54
|
+
var t = typeof s.t === 'number' ? s.t : s.start;
|
|
55
|
+
var label = s.label != null ? s.label : s.title;
|
|
56
|
+
if (typeof t === 'number' && isFinite(t)) out.push({ t: t, label: label == null ? '' : String(label) });
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
const snapshot = () => {
|
|
61
|
+
return { smPlayer: 1, type: 'state', duration: duration(), t: now(), playing: playing(), muted: muted, scenes: scenes() };
|
|
62
|
+
}
|
|
63
|
+
const stopTimer = () => { if (timer !== null) { clearInterval(timer); timer = null; } }
|
|
64
|
+
const reply = (src) => {
|
|
65
|
+
if (!src || typeof src.postMessage !== 'function') return;
|
|
66
|
+
var s = snapshot();
|
|
67
|
+
src.postMessage(s, '*');
|
|
68
|
+
if (!s.playing) stopTimer();
|
|
69
|
+
}
|
|
70
|
+
const startTimer = (src) => {
|
|
71
|
+
stopTimer();
|
|
72
|
+
timer = setInterval(() => { reply(src); }, 250);
|
|
73
|
+
}
|
|
74
|
+
const setMuted = (m) => {
|
|
75
|
+
muted = m;
|
|
76
|
+
var els = media();
|
|
77
|
+
for (var i = 0; i < els.length; i++) els[i].muted = m;
|
|
78
|
+
call('setMuted', m);
|
|
79
|
+
}
|
|
80
|
+
window.addEventListener('message', (e) => {
|
|
81
|
+
var data = e && e.data;
|
|
82
|
+
if (!data || typeof data !== 'object' || data.smPlayer !== 1) return;
|
|
83
|
+
var cmd = data.cmd;
|
|
84
|
+
if (cmd === 'play') {
|
|
85
|
+
flag = true;
|
|
86
|
+
if (!call('play')) { var el = media()[0]; if (el && typeof el.play === 'function') el.play(); }
|
|
87
|
+
} else if (cmd === 'pause') {
|
|
88
|
+
flag = false;
|
|
89
|
+
if (!call('pause')) { var el2 = media()[0]; if (el2 && typeof el2.pause === 'function') el2.pause(); }
|
|
90
|
+
} else if (cmd === 'restart') {
|
|
91
|
+
flag = true;
|
|
92
|
+
if (!call('restart')) { call('seek', 0); call('play'); }
|
|
93
|
+
} else if (cmd === 'seek') {
|
|
94
|
+
call('seek', num(data.t, 0));
|
|
95
|
+
} else if (cmd === 'mute') {
|
|
96
|
+
setMuted(true);
|
|
97
|
+
} else if (cmd === 'unmute') {
|
|
98
|
+
setMuted(false);
|
|
99
|
+
} else if (cmd !== 'hello') {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
reply(e.source);
|
|
103
|
+
if (playing() && e.source) startTimer(e.source);
|
|
104
|
+
});
|
|
105
|
+
})();`;
|
|
106
|
+
|
|
107
|
+
const PLAYER_BRIDGE_SCRIPT = `<script data-sm-player-bridge>${BRIDGE_JS}</script>`;
|
|
108
|
+
|
|
109
|
+
const CSP_META_RE = /<meta\b[^>]*\bhttp-equiv\s*=\s*["']Content-Security-Policy["'][^>]*>/i;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Inserts the bridge right after the CSP meta. Idempotent. If no CSP meta is
|
|
113
|
+
* present the bridge goes after a leading doctype (or at the very start).
|
|
114
|
+
* @param {string} html
|
|
115
|
+
* @returns {string}
|
|
116
|
+
*/
|
|
117
|
+
function injectPlayerBridge(html) {
|
|
118
|
+
if (typeof html !== 'string' || html.includes('data-sm-player-bridge')) return html;
|
|
119
|
+
const m = CSP_META_RE.exec(html);
|
|
120
|
+
if (m) {
|
|
121
|
+
const at = m.index + m[0].length;
|
|
122
|
+
return html.slice(0, at) + PLAYER_BRIDGE_SCRIPT + html.slice(at);
|
|
123
|
+
}
|
|
124
|
+
const dt = /^\s*<!DOCTYPE\b[^>]*>/i.exec(html);
|
|
125
|
+
const at = dt ? dt[0].length : 0;
|
|
126
|
+
return html.slice(0, at) + PLAYER_BRIDGE_SCRIPT + html.slice(at);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = { PLAYER_BRIDGE_SCRIPT, injectPlayerBridge };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* cpuSample.cjs — measured machine-wide CPU use over a real window.
|
|
5
|
+
*
|
|
6
|
+
* The 1-minute load average counts parked D-state tasks and short bursts, so
|
|
7
|
+
* it can read high on a mostly idle machine (2026-11-01, 14 CPUs: 25 D-state
|
|
8
|
+
* tasks gave loadavg1 22.39 while the CPU was only 24% busy over 75 s). This
|
|
9
|
+
* helper derives busy/total from cumulative counters over a window of about a
|
|
10
|
+
* minute instead. Pure and injectable (`read`, `now`).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const fs = require('node:fs');
|
|
14
|
+
const os = require('node:os');
|
|
15
|
+
|
|
16
|
+
// Matches the 1-minute load average's horizon.
|
|
17
|
+
const DEFAULT_WINDOW_MS = 60_000;
|
|
18
|
+
// Anything shorter is a snapshot, not a sample: the two readings that started
|
|
19
|
+
// this work were 8 s long and fell in quiet gaps.
|
|
20
|
+
const MIN_WINDOW_MS = 20_000;
|
|
21
|
+
const MAX_WINDOW_MS = 180_000;
|
|
22
|
+
const MIN_SPACING_MS = 5_000;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Parse the aggregate `cpu ` line of /proc/stat into { busy, iowait, total }.
|
|
26
|
+
* Columns: user nice system idle iowait irq softirq steal guest guest_nice.
|
|
27
|
+
* Only the first 8 count (guest time is already inside user).
|
|
28
|
+
* - iowait is not busy: the CPU is idle during it and the load gate judges CPU
|
|
29
|
+
* only. Swap-in waits land there on this host, and the memory gate and swap
|
|
30
|
+
* clamp run before the load gate. It is carried as its own figure. os.cpus()
|
|
31
|
+
* has no such column, so macOS matches.
|
|
32
|
+
* - nice counts as busy: every Session Manager process here runs at nice 12,
|
|
33
|
+
* so nice is the largest column on this box.
|
|
34
|
+
* @param {string} text
|
|
35
|
+
* @returns {{ busy: number, iowait: number, total: number } | null}
|
|
36
|
+
*/
|
|
37
|
+
function parseProcStat(text) {
|
|
38
|
+
if (typeof text !== 'string' || !text) return null;
|
|
39
|
+
const line = text.split('\n').find((l) => l.startsWith('cpu '));
|
|
40
|
+
if (!line) return null;
|
|
41
|
+
const parts = line.trim().split(/\s+/).slice(1, 9);
|
|
42
|
+
if (parts.length < 4) return null;
|
|
43
|
+
const nums = [];
|
|
44
|
+
for (const p of parts) {
|
|
45
|
+
const n = Number(p);
|
|
46
|
+
if (!Number.isFinite(n)) return null;
|
|
47
|
+
nums.push(n);
|
|
48
|
+
}
|
|
49
|
+
while (nums.length < 8) nums.push(0);
|
|
50
|
+
const total = nums.reduce((a, b) => a + b, 0);
|
|
51
|
+
const idle = nums[3];
|
|
52
|
+
const iowait = nums[4];
|
|
53
|
+
return { busy: total - idle - iowait, iowait, total };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {{ platform?: string, readFile?: (p: string) => string, cpus?: () => import('node:os').CpuInfo[] }} [deps]
|
|
58
|
+
* @returns {{ busy: number, iowait: number, total: number } | null}
|
|
59
|
+
*/
|
|
60
|
+
function readCpuTimes({
|
|
61
|
+
platform = process.platform,
|
|
62
|
+
readFile = (p) => fs.readFileSync(p, 'utf8'),
|
|
63
|
+
cpus = () => os.cpus(),
|
|
64
|
+
} = {}) {
|
|
65
|
+
try {
|
|
66
|
+
if (platform === 'linux') return parseProcStat(readFile('/proc/stat'));
|
|
67
|
+
const list = cpus();
|
|
68
|
+
if (!Array.isArray(list) || list.length === 0) return null;
|
|
69
|
+
let busy = 0;
|
|
70
|
+
let idle = 0;
|
|
71
|
+
for (const c of list) {
|
|
72
|
+
const t = c.times;
|
|
73
|
+
busy += t.user + t.nice + t.sys + t.irq;
|
|
74
|
+
idle += t.idle;
|
|
75
|
+
}
|
|
76
|
+
if (![busy, idle].every(Number.isFinite)) return null;
|
|
77
|
+
return { busy, iowait: 0, total: busy + idle };
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {{ read?: () => ({busy:number,iowait?:number,total:number}|null), now?: () => number,
|
|
85
|
+
* windowMs?: number, minWindowMs?: number, maxWindowMs?: number, minSpacingMs?: number }} [opts]
|
|
86
|
+
*/
|
|
87
|
+
function createCpuSampler({
|
|
88
|
+
read = readCpuTimes,
|
|
89
|
+
now = () => Date.now(),
|
|
90
|
+
windowMs = DEFAULT_WINDOW_MS,
|
|
91
|
+
minWindowMs = MIN_WINDOW_MS,
|
|
92
|
+
maxWindowMs = MAX_WINDOW_MS,
|
|
93
|
+
minSpacingMs = MIN_SPACING_MS,
|
|
94
|
+
} = {}) {
|
|
95
|
+
/** @type {{ at: number, busy: number, iowait: number, total: number }[]} */
|
|
96
|
+
let entries = [];
|
|
97
|
+
|
|
98
|
+
function observe() {
|
|
99
|
+
const t = now();
|
|
100
|
+
const r = read();
|
|
101
|
+
if (!r) return;
|
|
102
|
+
const newest = entries[entries.length - 1];
|
|
103
|
+
if (newest && t - newest.at < minSpacingMs) return;
|
|
104
|
+
entries.push({ at: t, busy: r.busy, iowait: r.iowait ?? 0, total: r.total });
|
|
105
|
+
entries = entries.filter((e) => t - e.at <= maxWindowMs);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function sample() {
|
|
109
|
+
const t = now();
|
|
110
|
+
const r = read();
|
|
111
|
+
if (!r) return null;
|
|
112
|
+
let best = null;
|
|
113
|
+
let bestDiff = Infinity;
|
|
114
|
+
for (const e of entries) {
|
|
115
|
+
const age = t - e.at;
|
|
116
|
+
if (age < minWindowMs || age > maxWindowMs) continue;
|
|
117
|
+
const diff = Math.abs(age - windowMs);
|
|
118
|
+
if (diff < bestDiff) {
|
|
119
|
+
best = e;
|
|
120
|
+
bestDiff = diff;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (!best) return null;
|
|
124
|
+
const dBusy = r.busy - best.busy;
|
|
125
|
+
const dTotal = r.total - best.total;
|
|
126
|
+
const dIowait = (r.iowait ?? 0) - best.iowait;
|
|
127
|
+
// Per-CPU idle and iowait counters can step backwards.
|
|
128
|
+
if (dTotal <= 0 || dBusy < 0) return null;
|
|
129
|
+
return {
|
|
130
|
+
busyFraction: Math.min(1, dBusy / dTotal),
|
|
131
|
+
iowaitFraction: Math.min(1, Math.max(0, dIowait / dTotal)),
|
|
132
|
+
windowMs: t - best.at,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { observe, sample };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
parseProcStat,
|
|
141
|
+
readCpuTimes,
|
|
142
|
+
createCpuSampler,
|
|
143
|
+
DEFAULT_WINDOW_MS,
|
|
144
|
+
MIN_WINDOW_MS,
|
|
145
|
+
MAX_WINDOW_MS,
|
|
146
|
+
MIN_SPACING_MS,
|
|
147
|
+
};
|
|
@@ -389,6 +389,45 @@ function resolveGate(prdText) {
|
|
|
389
389
|
return { source: 'absent', sequence: [] };
|
|
390
390
|
}
|
|
391
391
|
|
|
392
|
+
const TIMEOUT_UNIT_MS = { '': 1000, s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 };
|
|
393
|
+
|
|
394
|
+
/** Time one `&&` step may take: every `timeout N` outside quotes, else the default. */
|
|
395
|
+
function stepTimeoutMs(step) {
|
|
396
|
+
const bare = step.replace(/'[^']*'|"[^"]*"/g, ' ');
|
|
397
|
+
let sum = 0;
|
|
398
|
+
for (const m of bare.matchAll(/(?:^|\s)timeout\s+(\d+)([smhd]?)(?=\s|$)/g)) {
|
|
399
|
+
sum += parseInt(m[1], 10) * TIMEOUT_UNIT_MS[m[2]];
|
|
400
|
+
}
|
|
401
|
+
return sum || DEFAULT_GATE_TIMEOUT_MS;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* The most one full pass over a PRD's gate can take: the sum of every step's
|
|
406
|
+
* `timeout`. Not built on resolveGate because that returns an empty sequence
|
|
407
|
+
* for a gate the no-shell parser refuses (Bilko-Game-Academy 872-fix-e2e-no-gpu-suite
|
|
408
|
+
* read as 0 minutes though it is 47.5), yet a validator runs gates through
|
|
409
|
+
* Bash, so that time is real. Returns 0 for a none gate, a PRD with no gate,
|
|
410
|
+
* or non-string/empty input.
|
|
411
|
+
*
|
|
412
|
+
* @param {unknown} prdText
|
|
413
|
+
* @returns {number} milliseconds
|
|
414
|
+
*/
|
|
415
|
+
function gateTimeoutMs(prdText) {
|
|
416
|
+
if (!prdText || typeof prdText !== 'string') return 0;
|
|
417
|
+
const explicit = readExplicitGate(prdText);
|
|
418
|
+
if (explicit) {
|
|
419
|
+
if (explicit.kind === 'none') return 0;
|
|
420
|
+
let total = 0;
|
|
421
|
+
for (const chain of explicit.chains) {
|
|
422
|
+
for (const seg of splitOnAndAnd(chain)) {
|
|
423
|
+
if (seg.trim()) total += stepTimeoutMs(seg.trim());
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return total;
|
|
427
|
+
}
|
|
428
|
+
return resolveGate(prdText).sequence.reduce((sum, step) => sum + step.timeoutMs, 0);
|
|
429
|
+
}
|
|
430
|
+
|
|
392
431
|
/**
|
|
393
432
|
* The gate as an ordered list of no-shell commands: [{argv, timeoutMs}, …].
|
|
394
433
|
* Empty when there is no parseable gate or the PRD opted out with `gate: none`
|
|
@@ -918,6 +957,7 @@ module.exports = {
|
|
|
918
957
|
explainChain,
|
|
919
958
|
isNoneGate,
|
|
920
959
|
resolveGate,
|
|
960
|
+
gateTimeoutMs,
|
|
921
961
|
runGateSequence,
|
|
922
962
|
runIsolatedSequence,
|
|
923
963
|
extractSection,
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* jobLeftovers.cjs — read-only detection of processes that outlived their
|
|
5
|
+
* scheduler job.
|
|
6
|
+
*
|
|
7
|
+
* Why it exists: the post-exit group sweep only reaches the job's own process
|
|
8
|
+
* group. A child that called setsid (pnpm dev trees: concurrently, tsx watch,
|
|
9
|
+
* vite; Playwright web servers) escapes that group, so run metas report
|
|
10
|
+
* leakedDescendants [] while the tree runs for days.
|
|
11
|
+
*
|
|
12
|
+
* Identity is the SM_SCHEDULER_JOB_SLUG environment marker every job process
|
|
13
|
+
* inherits — never cwd or command line (the scheduler's own git/gate commands
|
|
14
|
+
* run in retained worktrees without the marker). Linux /proc only; no signal
|
|
15
|
+
* is sent here.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const fs = require('node:fs');
|
|
19
|
+
const path = require('node:path');
|
|
20
|
+
const { isClaudeArgv } = require('./reaperHelpers.cjs');
|
|
21
|
+
const procIdentity = require('../process/procIdentity.cjs');
|
|
22
|
+
|
|
23
|
+
const LEFTOVER_GRACE_MS = 120000;
|
|
24
|
+
const MARKER = 'SM_SCHEDULER_JOB_SLUG=';
|
|
25
|
+
const LIVE_STATUSES = new Set(['running', 'investigating', 'pending']);
|
|
26
|
+
|
|
27
|
+
function parseStat(raw) {
|
|
28
|
+
const open = raw.indexOf('(');
|
|
29
|
+
const close = raw.lastIndexOf(')');
|
|
30
|
+
if (open === -1 || close === -1 || close < open) return null;
|
|
31
|
+
const f = raw.slice(close + 1).trim().split(/\s+/);
|
|
32
|
+
const ppid = Number(f[1]);
|
|
33
|
+
if (f.length < 20 || !Number.isFinite(ppid)) return null;
|
|
34
|
+
return {
|
|
35
|
+
comm: raw.slice(open + 1, close),
|
|
36
|
+
state: f[0],
|
|
37
|
+
ppid,
|
|
38
|
+
tty: Number(f[4]) || 0,
|
|
39
|
+
utime: Number(f[11]) || 0,
|
|
40
|
+
stime: Number(f[12]) || 0,
|
|
41
|
+
startTicks: Number(f[19]) || 0,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function scanJobProcesses({ fsImpl = fs, procRoot = process.env.SM_PROC_ROOT || '/proc', platform = process.platform } = {}) {
|
|
46
|
+
const empty = () => ({ procs: [], parentOf: new Map() });
|
|
47
|
+
if (platform !== 'linux') return empty();
|
|
48
|
+
let names;
|
|
49
|
+
try {
|
|
50
|
+
names = fsImpl.readdirSync(procRoot);
|
|
51
|
+
} catch {
|
|
52
|
+
return empty();
|
|
53
|
+
}
|
|
54
|
+
let uptime = 0;
|
|
55
|
+
try {
|
|
56
|
+
uptime = Number(String(fsImpl.readFileSync(path.join(procRoot, 'uptime'), 'utf8')).split(/\s+/)[0]) || 0;
|
|
57
|
+
} catch { /* leave 0 */ }
|
|
58
|
+
|
|
59
|
+
const procs = [];
|
|
60
|
+
const parentOf = new Map();
|
|
61
|
+
for (const name of names) {
|
|
62
|
+
if (!/^\d+$/.test(name)) continue;
|
|
63
|
+
const pid = Number(name);
|
|
64
|
+
const dir = path.join(procRoot, name);
|
|
65
|
+
try {
|
|
66
|
+
const st = parseStat(String(fsImpl.readFileSync(path.join(dir, 'stat'), 'utf8')));
|
|
67
|
+
if (!st) continue;
|
|
68
|
+
parentOf.set(pid, st.ppid);
|
|
69
|
+
let env;
|
|
70
|
+
try {
|
|
71
|
+
env = String(fsImpl.readFileSync(path.join(dir, 'environ'), 'utf8')).split('\0');
|
|
72
|
+
} catch { continue; }
|
|
73
|
+
const entry = env.find((e) => e.startsWith(MARKER));
|
|
74
|
+
if (!entry || st.state === 'Z') continue;
|
|
75
|
+
const slug = entry.slice(MARKER.length);
|
|
76
|
+
let argv = [];
|
|
77
|
+
try {
|
|
78
|
+
argv = String(fsImpl.readFileSync(path.join(dir, 'cmdline'), 'utf8')).split('\0');
|
|
79
|
+
if (argv.length && argv[argv.length - 1] === '') argv.pop();
|
|
80
|
+
} catch { /* empty argv */ }
|
|
81
|
+
let cwd = null;
|
|
82
|
+
try {
|
|
83
|
+
cwd = String(fsImpl.readlinkSync(path.join(dir, 'cwd'))).replace(/ \(deleted\)$/, '');
|
|
84
|
+
} catch { /* null */ }
|
|
85
|
+
const etimes = Math.max(0, uptime - st.startTicks / 100);
|
|
86
|
+
const pcpu = etimes > 0 ? (((st.utime + st.stime) / 100) / etimes) * 100 : 0;
|
|
87
|
+
procs.push({ pid, ppid: st.ppid, tty: st.tty, slug, argv, cwd, comm: st.comm, startTicks: st.startTicks, etimes, pcpu });
|
|
88
|
+
} catch { /* pid vanished or denied mid-scan */ }
|
|
89
|
+
}
|
|
90
|
+
return { procs, parentOf };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function rowEndedMs(row) {
|
|
94
|
+
if (row && row.finishedAt) {
|
|
95
|
+
const t = Date.parse(row.finishedAt);
|
|
96
|
+
if (Number.isFinite(t)) return t;
|
|
97
|
+
}
|
|
98
|
+
const h = row && Array.isArray(row.statusHistory) ? row.statusHistory : [];
|
|
99
|
+
if (h.length) {
|
|
100
|
+
const t = Date.parse(h[h.length - 1] && h[h.length - 1].at);
|
|
101
|
+
if (Number.isFinite(t)) return t;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function selectLeftovers({ scan, rows, runningSlugs, now, selfPid } = {}) {
|
|
107
|
+
const procs = (scan && scan.procs) || [];
|
|
108
|
+
const parentOf = (scan && scan.parentOf) || new Map();
|
|
109
|
+
const allRows = Array.isArray(rows) ? rows : [];
|
|
110
|
+
const running = runningSlugs instanceof Set ? runningSlugs : new Set(runningSlugs || []);
|
|
111
|
+
|
|
112
|
+
const bySlug = new Map();
|
|
113
|
+
for (const p of procs) {
|
|
114
|
+
if (!bySlug.has(p.slug)) bySlug.set(p.slug, []);
|
|
115
|
+
bySlug.get(p.slug).push(p);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const ancestors = new Set();
|
|
119
|
+
let cur = selfPid;
|
|
120
|
+
for (let i = 0; i < 64 && cur != null && parentOf.has(cur); i++) {
|
|
121
|
+
cur = parentOf.get(cur);
|
|
122
|
+
if (!cur || ancestors.has(cur)) break;
|
|
123
|
+
ancestors.add(cur);
|
|
124
|
+
}
|
|
125
|
+
const isDescendant = (pid) => {
|
|
126
|
+
let c = pid;
|
|
127
|
+
for (let i = 0; i < 64 && parentOf.has(c); i++) {
|
|
128
|
+
c = parentOf.get(c);
|
|
129
|
+
if (c === selfPid) return true;
|
|
130
|
+
if (!c) return false;
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const out = [];
|
|
136
|
+
for (const [slug, list] of bySlug) {
|
|
137
|
+
if (running.has(slug)) continue;
|
|
138
|
+
if (list.some((p) => isClaudeArgv(p.argv))) continue;
|
|
139
|
+
const slugRows = allRows.filter((r) => r && r.slug === slug);
|
|
140
|
+
if (slugRows.some((r) => LIVE_STATUSES.has(r.status))) continue;
|
|
141
|
+
let ended = true;
|
|
142
|
+
for (const r of slugRows) {
|
|
143
|
+
const t = rowEndedMs(r);
|
|
144
|
+
if (t == null || now - t < LEFTOVER_GRACE_MS) { ended = false; break; }
|
|
145
|
+
}
|
|
146
|
+
if (!ended) continue;
|
|
147
|
+
const keep = list.filter((p) => !(p.tty !== 0 || p.pid === selfPid || ancestors.has(p.pid) || isDescendant(p.pid)));
|
|
148
|
+
if (keep.length) out.push({ slug, procs: keep });
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* stopLeftovers(procs, deps) — SIGTERM each pid, then after graceMs SIGKILL only
|
|
155
|
+
* pids whose live identity is complete and not a different process from the one
|
|
156
|
+
* recorded before SIGTERM (a recycled pid is never killed). Never throws.
|
|
157
|
+
* Returns the pids that were signalled.
|
|
158
|
+
*/
|
|
159
|
+
function stopLeftovers(procs, {
|
|
160
|
+
kill = process.kill.bind(process),
|
|
161
|
+
identityOf = procIdentity.identity,
|
|
162
|
+
isDifferentProcess = procIdentity.isDifferentProcess,
|
|
163
|
+
setTimer = setTimeout,
|
|
164
|
+
graceMs = 5000,
|
|
165
|
+
} = {}) {
|
|
166
|
+
const recorded = new Map();
|
|
167
|
+
const signalled = [];
|
|
168
|
+
for (const p of Array.isArray(procs) ? procs : []) {
|
|
169
|
+
const pid = p && p.pid;
|
|
170
|
+
if (!Number.isInteger(pid) || pid <= 1) continue;
|
|
171
|
+
try { recorded.set(pid, identityOf(pid)); } catch { recorded.set(pid, null); }
|
|
172
|
+
try {
|
|
173
|
+
kill(pid, 'SIGTERM');
|
|
174
|
+
signalled.push(pid);
|
|
175
|
+
} catch { /* already gone */ }
|
|
176
|
+
}
|
|
177
|
+
if (signalled.length === 0) return signalled;
|
|
178
|
+
const timer = setTimer(() => {
|
|
179
|
+
for (const pid of signalled) {
|
|
180
|
+
try {
|
|
181
|
+
const live = identityOf(pid);
|
|
182
|
+
if (!live || live.complete !== true) continue;
|
|
183
|
+
if (isDifferentProcess(recorded.get(pid), live)) continue;
|
|
184
|
+
kill(pid, 'SIGKILL');
|
|
185
|
+
} catch { /* gone */ }
|
|
186
|
+
}
|
|
187
|
+
}, graceMs);
|
|
188
|
+
if (timer && typeof timer.unref === 'function') timer.unref();
|
|
189
|
+
return signalled;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* listJobSurvivors(slug, pid, opts) — every scanned process carrying the job's
|
|
194
|
+
* SM_SCHEDULER_JOB_SLUG marker except `pid` itself, whatever its process group or
|
|
195
|
+
* session. opts is passed to scanJobProcesses (injectable fs/procRoot/platform).
|
|
196
|
+
*/
|
|
197
|
+
function listJobSurvivors(slug, pid, opts) {
|
|
198
|
+
const { procs } = scanJobProcesses(opts);
|
|
199
|
+
return procs
|
|
200
|
+
.filter((p) => p.slug === slug && p.pid !== pid)
|
|
201
|
+
.map((p) => ({ pid: p.pid, pcpu: Math.round(p.pcpu * 10) / 10, etimes: Math.round(p.etimes), comm: p.comm }));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = { scanJobProcesses, listJobSurvivors, selectLeftovers, stopLeftovers, LEFTOVER_GRACE_MS };
|
|
@@ -40,7 +40,7 @@ function recordPath(runDir, slug) {
|
|
|
40
40
|
*/
|
|
41
41
|
function writeSupervisorRecord({
|
|
42
42
|
runDir, slug, cwd, runId, pid, pgid, identity, execCwd, worktreeDir, worktreeBranch,
|
|
43
|
-
sessionId, startedAt, budgetMs, maxDurationMs, idleKillMs, schedulerPid, codeSha, kind,
|
|
43
|
+
sessionId, startedAt, budgetMs, maxDurationMs, idleKillMs, schedulerPid, codeSha, kind, scopeUnit,
|
|
44
44
|
}) {
|
|
45
45
|
if (!runDir || !slug) throw new Error('writeSupervisorRecord: runDir and slug are required');
|
|
46
46
|
const record = {
|
|
@@ -51,6 +51,7 @@ function writeSupervisorRecord({
|
|
|
51
51
|
sessionId: sessionId ?? null, startedAt: startedAt ?? null,
|
|
52
52
|
budgetMs: budgetMs ?? null, maxDurationMs: maxDurationMs ?? null, idleKillMs: idleKillMs ?? null,
|
|
53
53
|
schedulerPid: schedulerPid ?? null, codeSha: codeSha ?? null,
|
|
54
|
+
scopeUnit: scopeUnit ?? null,
|
|
54
55
|
};
|
|
55
56
|
writeJsonAtomicSync(recordPath(runDir, slug), record);
|
|
56
57
|
return record;
|