claude-code-session-manager 0.141.0 → 0.143.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/.claude-plugin/marketplace.json +1 -1
- package/README.md +76 -153
- package/bin/cli.cjs +11 -0
- package/dist/assets/AppSettings-BO4jDVGO.js +2 -0
- package/dist/assets/{History-DJMiuXyK.js → History-DTttbvPb.js} +1 -1
- package/dist/assets/HomeClaudeConfig-DADP5bfn.js +3 -0
- package/dist/assets/{Panel-Bf1Cf8UL.js → Panel-B_D4Bg7L.js} +1 -1
- package/dist/assets/{ProjectClaudeConfig-BWPpypf9.js → ProjectClaudeConfig-DPnDJKcr.js} +7 -7
- package/dist/assets/Scheduler-CNLjn90m.js +16 -0
- package/dist/assets/{SkillReferenceGraph-Ba4TkX2C.js → SkillReferenceGraph-D1b2aiol.js} +1 -1
- package/dist/assets/{Skills-C1v91GEO.js → Skills-cfV7sgk6.js} +5 -5
- package/dist/assets/{TiptapBody-BrcWVpiW.js → TiptapBody-DVXtEIVy.js} +1 -1
- package/dist/assets/Toggle-CYajnSdx.js +1 -0
- package/dist/assets/{VoiceButton-qivnDV6K.js → VoiceButton-DJ4gGRuE.js} +1 -1
- package/dist/assets/{historyTotals-CMCPTKqC.js → historyTotals-D3MoHY2U.js} +1 -1
- package/dist/assets/index-CdTppjKR.js +3133 -0
- package/dist/assets/{index-DKXdXfka.css → index-DVHIHoJf.css} +1 -1
- package/dist/index.html +2 -2
- package/dist-web/app.css +1 -1
- package/dist-web/app.js +496 -484
- package/package.json +6 -4
- package/plugins/AGENTS.md +4 -3
- package/plugins/session-manager-dev/.claude-plugin/plugin.json +1 -1
- package/plugins/session-manager-dev/hooks/hooks.json +15 -0
- package/plugins/session-manager-dev/hooks/residue-check.cjs +241 -0
- package/plugins/session-manager-dev/skills/builder/SKILL.md +5 -0
- package/plugins/session-manager-dev/skills/develop/SKILL.md +58 -18
- package/plugins/session-manager-dev/skills/develop/standards.md +3 -2
- package/scripts/README.md +2 -0
- package/scripts/hooks/guard-tool-risk.cjs +5 -1
- package/scripts/hooks/lib/guard-tool-risk-policy.cjs +158 -1
- package/scripts/scheduler-mcp-server.cjs +1032 -112
- package/src/main/adminRouteRegistry.cjs +69 -0
- package/src/main/agentLibrary.cjs +72 -4
- package/src/main/agentsIpc.cjs +3 -2
- package/src/main/build-info.json +4 -4
- package/src/main/chatRunner.cjs +336 -23
- package/src/main/config.cjs +35 -1
- package/src/main/docEdit.cjs +4 -2
- package/src/main/health.cjs +129 -2
- package/src/main/index.cjs +110 -52
- package/src/main/ipcSchemas.cjs +172 -8
- package/src/main/lib/AGENTS.md +1 -1
- package/src/main/lib/chatBootRecovery.cjs +2 -0
- package/src/main/lib/claude/agentModelResolve.cjs +51 -25
- package/src/main/lib/claude/agentSnapshot.cjs +186 -0
- package/src/main/lib/claude/environmentProfiles.cjs +242 -0
- package/src/main/lib/claude/jobSandboxPolicy.cjs +241 -33
- package/src/main/lib/claude/personaToolScope.cjs +82 -31
- package/src/main/lib/claude/runClaudeP.cjs +4 -2
- package/src/main/lib/claude/toolRiskMarks.cjs +41 -1
- package/src/main/lib/claude/usageCircuit.cjs +49 -21
- package/src/main/lib/claude/vaultRefs.cjs +277 -0
- package/src/main/lib/demo-video-kit/capture.cjs +282 -12
- package/src/main/lib/demo-video-kit/cast.md +52 -0
- package/src/main/lib/demo-video-kit/character.cjs +778 -30
- package/src/main/lib/demo-video-kit/checks.cjs +120 -1
- package/src/main/lib/demo-video-kit/export.cjs +218 -0
- package/src/main/lib/demo-video-kit/frames.cjs +104 -9
- package/src/main/lib/demo-video-kit/kind-marketing.md +121 -0
- package/src/main/lib/demo-video-kit/kind-tiktok.md +103 -0
- package/src/main/lib/demo-video-kit/kokoroSay.cjs +205 -0
- package/src/main/lib/demo-video-kit/project-home-html.md +43 -0
- package/src/main/lib/demo-video-kit/projectSounds.cjs +191 -0
- package/src/main/lib/demo-video-kit/resumableRun.cjs +298 -0
- package/src/main/lib/demo-video-kit/seedKit.cjs +3 -3
- package/src/main/lib/demo-video-kit/sound.cjs +166 -9
- package/src/main/lib/demo-video-kit/standalone.cjs +231 -0
- package/src/main/lib/demo-video-kit/voiceLane.cjs +233 -0
- package/src/main/lib/demo-video-kit/voiceLibrary.cjs +312 -0
- package/src/main/lib/epic/activeIndexMerge.cjs +5 -0
- package/src/main/lib/epic/artifactAdminRoutes.cjs +211 -0
- package/src/main/lib/epic/artifactIpc.cjs +153 -0
- package/src/main/lib/epic/artifactNotice.cjs +72 -0
- package/src/main/lib/epic/artifactRetention.cjs +356 -0
- package/src/main/lib/epic/artifactStore.cjs +435 -0
- package/src/main/lib/epic/delegationReadiness.cjs +83 -2
- package/src/main/lib/epic/epicAgentRefresh.cjs +97 -0
- package/src/main/lib/epic/epicEventLog.cjs +258 -0
- package/src/main/lib/epic/epicMint.cjs +43 -1
- package/src/main/lib/epic/epicOutsideRead.cjs +1 -0
- package/src/main/lib/epic/epicOutsideState.cjs +18 -4
- package/src/main/lib/epic/epicPurge.cjs +347 -0
- package/src/main/lib/epic/epicReopen.cjs +5 -2
- package/src/main/lib/epic/epicSettle.cjs +4 -13
- package/src/main/lib/epic/epicSpawnPlan.cjs +57 -4
- package/src/main/lib/epic/epicStateEmitter.cjs +248 -0
- package/src/main/lib/epic/epicStatusMirror.cjs +34 -1
- package/src/main/lib/epic/epicTransitions.cjs +139 -0
- package/src/main/lib/epic/epicValidationHook.cjs +94 -4
- package/src/main/lib/epic/guardSpawnSettings.cjs +3 -2
- package/src/main/lib/externalContract.cjs +48 -4
- package/src/main/lib/feedback-remote/autoStartedSession.cjs +1 -1
- package/src/main/lib/feedback-remote/crossProjectReply.cjs +478 -127
- package/src/main/lib/feedback-remote/customerFeedbackAdminRoutes.cjs +16 -3
- package/src/main/lib/feedback-remote/customerFeedbackClient.cjs +11 -6
- package/src/main/lib/feedback-remote/customerFeedbackInbox.cjs +26 -1
- package/src/main/lib/feedback-remote/customerFeedbackIpc.cjs +5 -2
- package/src/main/lib/feedback-remote/feedbackBoxDrain.cjs +409 -0
- package/src/main/lib/feedback-remote/feedbackBoxKeys.cjs +218 -0
- package/src/main/lib/feedback-remote/feedbackEnvelope.cjs +163 -0
- package/src/main/lib/feedback-remote/feedbackRelayClient.cjs +164 -0
- package/src/main/lib/feedback-remote/feedbackReport.cjs +234 -0
- package/src/main/lib/feedback-remote/feedbackSeal.cjs +217 -0
- package/src/main/lib/feedback-remote/localAdminHttp.cjs +108 -2
- package/src/main/lib/feedback-remote/macroAdminRoutes.cjs +32 -1
- package/src/main/lib/feedback-remote/needsYouIpc.cjs +17 -0
- package/src/main/lib/feedback-remote/needsYouStore.cjs +177 -6
- package/src/main/lib/feedback-remote/pendingReplies.cjs +207 -0
- package/src/main/lib/feedback-remote/projectHomeAdminRoutes.cjs +282 -14
- package/src/main/lib/feedback-remote/replyClaims.cjs +159 -0
- package/src/main/lib/feedback-remote/serviceClient.cjs +429 -0
- package/src/main/lib/feedback-remote/serviceMcpSession.cjs +252 -0
- package/src/main/lib/feedback-remote/serviceMcpTools.cjs +284 -0
- package/src/main/lib/feedback-remote/servicePairings.cjs +313 -0
- package/src/main/lib/feedback-remote/serviceStdioBridge.cjs +290 -0
- package/src/main/lib/feedback-remote/siteFeedbackAdminRoutes.cjs +295 -0
- package/src/main/lib/feedback-remote/siteFeedbackInbox.cjs +598 -0
- package/src/main/lib/feedback-remote/siteFeedbackIpc.cjs +86 -0
- package/src/main/lib/feedback-remote/siteFeedbackService.cjs +477 -0
- package/src/main/lib/git/gitWorktree.cjs +8 -0
- package/src/main/lib/git/runtimeStateUntrack.cjs +15 -1
- package/src/main/lib/git/worktreeDeps.cjs +182 -4
- package/src/main/lib/mcpToolCatalog.cjs +399 -29
- package/src/main/lib/opsOwnership.cjs +141 -28
- package/src/main/lib/opsRetentionSettings.cjs +27 -5
- package/src/main/lib/persona-macro/builtinMacros.cjs +7 -2
- package/src/main/lib/persona-macro/macroJobDispatch.cjs +52 -8
- package/src/main/lib/persona-macro/macroJobIpc.cjs +16 -6
- package/src/main/lib/persona-macro/macroLibrary.cjs +49 -7
- package/src/main/lib/persona-macro/personaFrontmatterSchema.cjs +83 -0
- package/src/main/lib/persona-macro/projectVideoToolCheck.cjs +3 -0
- package/src/main/lib/persona-macro/shippedPersonaSeeds.cjs +309 -0
- package/src/main/lib/phaseProjection.cjs +103 -0
- package/src/main/lib/prd/depSlugResolve.cjs +4 -13
- package/src/main/lib/prd/prdBodyPatch.cjs +12 -17
- package/src/main/lib/prd/prdCreate.cjs +387 -228
- package/src/main/lib/prd/prdCriteria.cjs +228 -0
- package/src/main/lib/prd/prdFrontmatter.cjs +43 -3
- package/src/main/lib/prd/prdGateFiles.cjs +125 -49
- package/src/main/lib/prd/prdSizing.cjs +3 -1
- package/src/main/lib/process/cleanEnv.cjs +52 -1
- package/src/main/lib/process/leftoverRenice.cjs +178 -0
- package/src/main/lib/process/memoryScope.cjs +31 -3
- package/src/main/lib/project-pages/pagesLibrary.cjs +72 -6
- package/src/main/lib/project-pages/videoPressLines.cjs +145 -0
- package/src/main/lib/project-pages/videoReviewIpc.cjs +3 -2
- package/src/main/lib/project-pages/videoScriptPress.cjs +260 -0
- package/src/main/lib/project-pages/videoScriptRunRoute.cjs +351 -0
- package/src/main/lib/project-pages/videoScriptSwitchIpc.cjs +49 -0
- package/src/main/lib/project-pages/videoScriptSwitchRoute.cjs +173 -0
- package/src/main/lib/project-pages/voiceAdminRoutes.cjs +175 -0
- package/src/main/lib/project-pages/voicePreview.cjs +196 -0
- package/src/main/lib/project-pages/voicesIpc.cjs +186 -0
- package/src/main/lib/promptSessionSchema.cjs +6 -0
- package/src/main/lib/scheduler/cancelledRow.cjs +57 -0
- package/src/main/lib/scheduler/cpuSample.cjs +133 -42
- package/src/main/lib/scheduler/criteriaChecks.cjs +313 -0
- package/src/main/lib/scheduler/definitionOfDone.cjs +44 -5
- package/src/main/lib/scheduler/heldSchedulerTurns.cjs +238 -0
- package/src/main/lib/scheduler/jobMemoryStats.cjs +324 -0
- package/src/main/lib/scheduler/loadGate.cjs +82 -4
- package/src/main/lib/scheduler/planRecordHarvest.cjs +152 -54
- package/src/main/lib/scheduler/prdOutcomeRecord.cjs +191 -0
- package/src/main/lib/scheduler/queueStore.cjs +16 -0
- package/src/main/lib/scheduler/rcaReport.cjs +79 -15
- package/src/main/lib/scheduler/runLogRetention.cjs +42 -1
- package/src/main/lib/scheduler/scheduleJobSchema.cjs +18 -0
- package/src/main/lib/scheduler/scheduleJobTransitions.cjs +47 -0
- package/src/main/lib/scheduler/schedulerConfig.cjs +47 -0
- package/src/main/lib/scheduler/schedulerPaths.cjs +9 -3
- package/src/main/lib/scheduler/schedulerRuntimeState.cjs +50 -1
- package/src/main/lib/scheduler/sessionSlots.cjs +5 -1
- package/src/main/lib/scheduler/slotsRoute.cjs +251 -0
- package/src/main/lib/scheduler/toolClearance.cjs +258 -0
- package/src/main/lib/scheduler/usageHoldRoute.cjs +97 -0
- package/src/main/lib/scheduler/validatorVerdictSchema.cjs +152 -11
- package/src/main/lib/tasks/memoryConsolidation.cjs +312 -0
- package/src/main/lib/tasks/mitigationReview.cjs +314 -0
- package/src/main/lib/tasks/taskAdminRoutes.cjs +390 -25
- package/src/main/lib/tasks/taskIpc.cjs +22 -18
- package/src/main/lib/tasks/taskLedger.cjs +147 -25
- package/src/main/lib/tasks/taskRun.cjs +186 -17
- package/src/main/lib/tasks/taskSwitch.cjs +85 -12
- package/src/main/lib/tasks/taskSwitchRoute.cjs +172 -0
- package/src/main/lib/telemetry/auditEvents.cjs +204 -6
- package/src/main/lib/telemetry/auditLog.cjs +6 -2
- package/src/main/lib/telemetry/eventsAdminRoutes.cjs +198 -6
- package/src/main/lib/telemetry/webhookDelivery.cjs +205 -0
- package/src/main/lib/telemetry/webhookRegistry.cjs +281 -0
- package/src/main/lib/toolUseClassify.cjs +181 -0
- package/src/main/lib/util/atomicFs.cjs +2 -2
- package/src/main/lib/util/smfileRange.cjs +77 -0
- package/src/main/projectPages.cjs +3 -1
- package/src/main/promptSessionEvents.cjs +21 -1
- package/src/main/pty.cjs +77 -0
- package/src/main/queueOps.cjs +33 -14
- package/src/main/runVerify.cjs +74 -9
- package/src/main/scheduler/AGENTS.md +1 -0
- package/src/main/scheduler/budget.cjs +6 -5
- package/src/main/scheduler/prdParser.cjs +46 -13
- package/src/main/scheduler/reviewLadder.cjs +9 -25
- package/src/main/scheduler/utilizationHold.cjs +191 -0
- package/src/main/scheduler.cjs +1932 -213
- package/src/main/templates/PRD_AUTHORING.md +11 -3
- package/src/main/webRemote.cjs +463 -6
- package/src/preload/api.d.ts +576 -39
- package/src/preload/apiSurface.json +223 -7
- package/src/preload/index.cjs +48 -1
- package/src/seed/agents/architect.md +6 -1
- package/src/seed/agents/blender-artist.md +2 -1
- package/src/seed/agents/competitive-analyst.md +3 -1
- package/src/seed/agents/competitive-researcher.md +2 -1
- package/src/seed/agents/demo-video-builder.md +98 -87
- package/src/seed/agents/dev-lead.md +3 -3
- package/src/seed/agents/project-home-builder.md +9 -31
- package/src/seed/agents/validator.md +23 -23
- package/dist/assets/AppSettings-DExeJhqQ.js +0 -2
- package/dist/assets/HomeClaudeConfig-CTp2tkZ4.js +0 -3
- package/dist/assets/Scheduler-cyr61UAC.js +0 -16
- package/dist/assets/ViewTabs-DXhOWFG0.js +0 -1
- package/dist/assets/index-B2TQz3nd.js +0 -3121
- package/src/main/lib/epic/epicStateWatcher.cjs +0 -157
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* voicesIpc.cjs — the Voices screen's IPC (Project Home ▸ Video ▸ Voices).
|
|
5
|
+
* Renderer IPC only: no admin HTTP route or MCP tool reaches the writers here,
|
|
6
|
+
* so changing an approved voice always takes a human click in the app.
|
|
7
|
+
*
|
|
8
|
+
* voices:list {cwd} -> { voices, errors, lane }
|
|
9
|
+
* each voice = voiceLibrary.listVoices entry + lastPreview
|
|
10
|
+
* {at, wallMs, audioSeconds} | null (newest 'preview' usage
|
|
11
|
+
* line for this project, voiceLane.readUsage) + inLane
|
|
12
|
+
* {lane, position} | null (voiceLane.laneSnapshot).
|
|
13
|
+
* voices:preview {cwd, voiceId, text} -> previewVoice result, bytes as a Uint8Array.
|
|
14
|
+
* voices:approve {cwd, voiceId} -> writes <voicesHome>/approvals.json
|
|
15
|
+
* each approval as {pin, approvedAt (ISO)}, pin = voiceLibrary.pinOf;
|
|
16
|
+
* project/machine voices only.
|
|
17
|
+
* voices:unapprove {cwd, voiceId} -> removes that approval.
|
|
18
|
+
* voices:delete {cwd, voiceId} -> deletes the effective project or
|
|
19
|
+
* machine voice file; refuses an approved voice and a built-in.
|
|
20
|
+
*
|
|
21
|
+
* This module is the ONLY writer of approvals.json and of voice deletions
|
|
22
|
+
* (voiceLibrary.cjs only reads approvals). Approval keys come from
|
|
23
|
+
* voiceLibrary.approvalKey — the reader's own function, never a copy.
|
|
24
|
+
*
|
|
25
|
+
* deps: { schemas, validatePath } plus optional test overrides
|
|
26
|
+
* { resolveProjectRoot, voiceLibrary, voiceLane, previewVoice, now }.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const fs = require('node:fs');
|
|
30
|
+
const path = require('node:path');
|
|
31
|
+
const { writeJsonAtomicSync } = require('../util/atomicFs.cjs');
|
|
32
|
+
|
|
33
|
+
const BUILTIN_ERROR = 'built-in voices cannot be approved, unapproved or deleted';
|
|
34
|
+
const APPROVED_DELETE_ERROR = 'this voice is approved — unapprove it first';
|
|
35
|
+
|
|
36
|
+
function libOf(deps) {
|
|
37
|
+
return deps.voiceLibrary || require('../demo-video-kit/voiceLibrary.cjs');
|
|
38
|
+
}
|
|
39
|
+
function laneOf(deps) {
|
|
40
|
+
return deps.voiceLane || require('../demo-video-kit/voiceLane.cjs');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** cwd -> validated, resolved project root. Throws on an invalid cwd. */
|
|
44
|
+
function projectRootOf(deps, cwd) {
|
|
45
|
+
const real = deps.validatePath(cwd);
|
|
46
|
+
const resolve = deps.resolveProjectRoot || require('../opsOwnership.cjs').resolveProjectRoot;
|
|
47
|
+
return path.resolve(resolve(real));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The one approval-key format, shared with the reader so the two can never drift.
|
|
51
|
+
const { approvalKey } = require('../demo-video-kit/voiceLibrary.cjs');
|
|
52
|
+
|
|
53
|
+
function approvalsPath(lib) {
|
|
54
|
+
return path.join(lib.voicesHome(), 'approvals.json');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The effective (highest-scope) library entry for id, or null. */
|
|
58
|
+
function effectiveEntry(lib, root, voiceId) {
|
|
59
|
+
const { voices } = lib.listVoices(root);
|
|
60
|
+
return voices.find((v) => v.id === voiceId) || null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function writeApprovals(lib, approvals) {
|
|
64
|
+
writeJsonAtomicSync(approvalsPath(lib), { version: 1, approvals });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function listWithStats(deps, root) {
|
|
68
|
+
const lib = libOf(deps);
|
|
69
|
+
const lane = laneOf(deps);
|
|
70
|
+
const { voices, errors } = lib.listVoices(root);
|
|
71
|
+
let usage = [];
|
|
72
|
+
try { usage = lane.readUsage({ projectRoot: root, limit: Infinity }) || []; } catch { usage = []; }
|
|
73
|
+
/** @type {Map<string, any>} */
|
|
74
|
+
const last = new Map();
|
|
75
|
+
for (const row of usage) {
|
|
76
|
+
if (!row || row.kind !== 'preview' || typeof row.voiceId !== 'string' || last.has(row.voiceId)) continue;
|
|
77
|
+
last.set(row.voiceId, {
|
|
78
|
+
at: typeof row.at === 'string' ? row.at : null,
|
|
79
|
+
wallMs: Number.isFinite(row.wallMs) ? row.wallMs : null,
|
|
80
|
+
audioSeconds: Number.isFinite(row.audioSeconds) ? row.audioSeconds : null,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
let snapshot = {};
|
|
84
|
+
try { snapshot = lane.laneSnapshot() || {}; } catch { snapshot = {}; }
|
|
85
|
+
/** @type {Map<string, {lane: string, position: number}>} */
|
|
86
|
+
const inLane = new Map();
|
|
87
|
+
for (const [name, tickets] of Object.entries(snapshot)) {
|
|
88
|
+
for (const t of Array.isArray(tickets) ? tickets : []) {
|
|
89
|
+
if (!t || typeof t.voiceId !== 'string' || inLane.has(t.voiceId)) continue;
|
|
90
|
+
if (t.projectRoot && t.projectRoot !== root) continue;
|
|
91
|
+
inLane.set(t.voiceId, { lane: name, position: t.position });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
voices: voices.map((v) => ({ ...v, lastPreview: last.get(v.id) || null, inLane: inLane.get(v.id) || null })),
|
|
96
|
+
errors,
|
|
97
|
+
lane: snapshot,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function preview(deps, root, voiceId, text) {
|
|
102
|
+
const run = deps.previewVoice || require('./voicePreview.cjs').previewVoice;
|
|
103
|
+
const r = await run({ projectRoot: root, voiceId, text });
|
|
104
|
+
if (!r || r.ok !== true) return { ok: false, error: (r && r.error) || 'voice preview failed' };
|
|
105
|
+
return { ok: true, cached: !!r.cached, wavPath: r.wavPath, bytes: new Uint8Array(r.bytes) };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function approve(deps, root, voiceId) {
|
|
109
|
+
const lib = libOf(deps);
|
|
110
|
+
const entry = effectiveEntry(lib, root, voiceId);
|
|
111
|
+
if (!entry) return { ok: false, error: `unknown voice '${voiceId}'` };
|
|
112
|
+
if (entry.scope === 'builtin') return { ok: false, error: BUILTIN_ERROR };
|
|
113
|
+
if (entry.scope !== 'project' && entry.scope !== 'machine') return { ok: false, error: `voice '${voiceId}' has no approvable scope` };
|
|
114
|
+
const { approvals } = lib.readApprovals();
|
|
115
|
+
const key = approvalKey(entry.scope, root, voiceId);
|
|
116
|
+
const pin = lib.pinOf(entry, root);
|
|
117
|
+
const approvedAt = (deps.now ? deps.now() : new Date()).toISOString();
|
|
118
|
+
writeApprovals(lib, { ...approvals, [key]: { pin, approvedAt } });
|
|
119
|
+
return { ok: true, voiceId, scope: entry.scope, pin, approvedAt };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function unapprove(deps, root, voiceId) {
|
|
123
|
+
const lib = libOf(deps);
|
|
124
|
+
const entry = effectiveEntry(lib, root, voiceId);
|
|
125
|
+
if (entry && entry.scope === 'builtin') return { ok: false, error: BUILTIN_ERROR };
|
|
126
|
+
const { approvals } = lib.readApprovals();
|
|
127
|
+
// The effective entry's key; with no file left (removed outside the app), any
|
|
128
|
+
// project/machine approval of this id so the human can clear the refusal.
|
|
129
|
+
const keys = entry
|
|
130
|
+
? [approvalKey(entry.scope, root, voiceId)]
|
|
131
|
+
: [approvalKey('project', root, voiceId), approvalKey('machine', root, voiceId)];
|
|
132
|
+
const present = keys.filter((k) => Object.prototype.hasOwnProperty.call(approvals, k));
|
|
133
|
+
if (!present.length) return { ok: false, error: `voice '${voiceId}' is not approved` };
|
|
134
|
+
const next = { ...approvals };
|
|
135
|
+
for (const k of present) delete next[k];
|
|
136
|
+
writeApprovals(lib, next);
|
|
137
|
+
return { ok: true, voiceId };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function deleteVoice(deps, root, voiceId) {
|
|
141
|
+
const lib = libOf(deps);
|
|
142
|
+
const entry = effectiveEntry(lib, root, voiceId);
|
|
143
|
+
if (!entry) return { ok: false, error: `unknown voice '${voiceId}'` };
|
|
144
|
+
if (entry.scope === 'builtin') return { ok: false, error: BUILTIN_ERROR };
|
|
145
|
+
const { approvals } = lib.readApprovals();
|
|
146
|
+
if (Object.prototype.hasOwnProperty.call(approvals, approvalKey(entry.scope, root, voiceId))) {
|
|
147
|
+
return { ok: false, error: APPROVED_DELETE_ERROR };
|
|
148
|
+
}
|
|
149
|
+
if (typeof entry.file !== 'string' || !entry.file) return { ok: false, error: `voice '${voiceId}' has no file` };
|
|
150
|
+
try {
|
|
151
|
+
fs.unlinkSync(entry.file);
|
|
152
|
+
} catch (e) {
|
|
153
|
+
return { ok: false, error: `could not delete voice '${voiceId}': ${e && e.message ? e.message : String(e)}` };
|
|
154
|
+
}
|
|
155
|
+
return { ok: true, voiceId, scope: entry.scope, file: entry.file };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function registerVoicesIpc(ipcMain, deps = {}) {
|
|
159
|
+
ipcMain.handle('voices:list', async (_event, raw) => {
|
|
160
|
+
const p = deps.schemas.voicesList.parse(raw);
|
|
161
|
+
return listWithStats(deps, projectRootOf(deps, p.cwd));
|
|
162
|
+
});
|
|
163
|
+
ipcMain.handle('voices:preview', async (_event, raw) => {
|
|
164
|
+
const p = deps.schemas.voicesPreview.parse(raw);
|
|
165
|
+
return preview(deps, projectRootOf(deps, p.cwd), p.voiceId, p.text);
|
|
166
|
+
});
|
|
167
|
+
ipcMain.handle('voices:approve', async (_event, raw) => {
|
|
168
|
+
const p = deps.schemas.voicesApprove.parse(raw);
|
|
169
|
+
return approve(deps, projectRootOf(deps, p.cwd), p.voiceId);
|
|
170
|
+
});
|
|
171
|
+
ipcMain.handle('voices:unapprove', async (_event, raw) => {
|
|
172
|
+
const p = deps.schemas.voicesUnapprove.parse(raw);
|
|
173
|
+
return unapprove(deps, projectRootOf(deps, p.cwd), p.voiceId);
|
|
174
|
+
});
|
|
175
|
+
ipcMain.handle('voices:delete', async (_event, raw) => {
|
|
176
|
+
const p = deps.schemas.voicesDelete.parse(raw);
|
|
177
|
+
return deleteVoice(deps, projectRootOf(deps, p.cwd), p.voiceId);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
module.exports = {
|
|
182
|
+
registerVoicesIpc,
|
|
183
|
+
BUILTIN_ERROR,
|
|
184
|
+
APPROVED_DELETE_ERROR,
|
|
185
|
+
CHANNELS: Object.freeze(['voices:list', 'voices:preview', 'voices:approve', 'voices:unapprove', 'voices:delete']),
|
|
186
|
+
};
|
|
@@ -115,6 +115,12 @@ const PromptSessionSchema = z.object({
|
|
|
115
115
|
// Unknown ids are kept (bounded strings, never an enum) so dropInvalidSessions (ipcSchemas.cjs)
|
|
116
116
|
// never drops a row for a pack id a newer build ships and this one does not know.
|
|
117
117
|
packs: z.array(z.string().min(1).max(64)).max(16).optional(),
|
|
118
|
+
// Content id (sha256 hex) of the AgentSnapshot (claude/agentSnapshot.cjs) frozen at mint:
|
|
119
|
+
// every chat turn builds its persona body + model from it, so a later persona edit reaches
|
|
120
|
+
// only new Epics. Absent on Epics minted before pinning (they keep the live persona read)
|
|
121
|
+
// and on non-Claude / agentType-less Epics. A bounded string, not a hex regex, so
|
|
122
|
+
// dropInvalidSessions (ipcSchemas.cjs) never drops a whole row over it.
|
|
123
|
+
agentSnapshotId: z.string().min(1).max(128).optional(),
|
|
118
124
|
// true = this Epic adopted an existing CLI session as its claudeSessionId
|
|
119
125
|
// (ensureEpic's restoreSessionId) instead of a fresh one; absent otherwise.
|
|
120
126
|
// Only true is ever written: a false would fail here, and dropInvalidSessions
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// @ts-check
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* cancelledRow — the ONE definition of "did a human cancel this queue row?"
|
|
6
|
+
* Pure, requires nothing, so every layer (scheduler, queueOps, lib/epic,
|
|
7
|
+
* lib/prd) can import it without dragging in scheduler deps.
|
|
8
|
+
*
|
|
9
|
+
* The rule: a row is cancelled when its status is a cancel status AND at
|
|
10
|
+
* least one cancel marker holds.
|
|
11
|
+
*
|
|
12
|
+
* Why two statuses: remote.cancelJob (scheduler.cjs) parks a cancelled row as
|
|
13
|
+
* 'failed', except when the killed run's landedCommit resolves — then it parks
|
|
14
|
+
* 'needs_review' so the landed work is verified before anyone trusts it. Both
|
|
15
|
+
* are a human cancel. Any other status (pending, running, completed, …) is
|
|
16
|
+
* never cancelled, even if it still carries a stale marker.
|
|
17
|
+
*
|
|
18
|
+
* Why three markers (any one is enough):
|
|
19
|
+
* 1. `cancelledAt` (non-empty string) — stamped by remote.cancelJob (and its
|
|
20
|
+
* dependents cascade) since 2026-10-19; the primary marker.
|
|
21
|
+
* 2. The newest statusHistory entry whose `to` equals the row's current
|
|
22
|
+
* status has source 'remote:cancelJob' — covers rows cancelled before
|
|
23
|
+
* `cancelledAt` existed. Only the newest matching entry counts, so a row
|
|
24
|
+
* cancelled, reset, then failed for real reads as a real failure.
|
|
25
|
+
* 3. `error` starts with 'cancelled via admin API' — the oldest rows, which
|
|
26
|
+
* carry neither of the above.
|
|
27
|
+
*
|
|
28
|
+
* A revived row never reads as cancelled: resetJobFields deletes `cancelledAt`
|
|
29
|
+
* and overwrites `error`, and the reset moves status off failed/needs_review.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/** Statuses remote.cancelJob writes for a cancelled row. */
|
|
33
|
+
const CANCEL_STATUSES = new Set(['failed', 'needs_review']);
|
|
34
|
+
/** statusHistory source remote.cancelJob stamps. */
|
|
35
|
+
const CANCEL_SOURCE = 'remote:cancelJob';
|
|
36
|
+
/** Prefix of every error string remote.cancelJob writes. */
|
|
37
|
+
const CANCEL_ERROR_PREFIX = 'cancelled via admin API';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {unknown} row a queue row (ScheduleJob-shaped) or anything else
|
|
41
|
+
* @returns {boolean} true only for a human-cancelled row
|
|
42
|
+
*/
|
|
43
|
+
function isCancelledRow(row) {
|
|
44
|
+
if (!row || typeof row !== 'object') return false;
|
|
45
|
+
const r = /** @type {Record<string, any>} */ (row);
|
|
46
|
+
if (!CANCEL_STATUSES.has(r.status)) return false;
|
|
47
|
+
if (typeof r.cancelledAt === 'string' && r.cancelledAt.length > 0) return true;
|
|
48
|
+
if (typeof r.error === 'string' && r.error.startsWith(CANCEL_ERROR_PREFIX)) return true;
|
|
49
|
+
const hist = Array.isArray(r.statusHistory) ? r.statusHistory : [];
|
|
50
|
+
for (let i = hist.length - 1; i >= 0; i--) {
|
|
51
|
+
const h = hist[i];
|
|
52
|
+
if (h && h.to === r.status) return h.source === CANCEL_SOURCE;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = { isCancelledRow, CANCEL_STATUSES, CANCEL_SOURCE, CANCEL_ERROR_PREFIX };
|
|
@@ -18,6 +18,15 @@
|
|
|
18
18
|
* processes — they carry no `--type=` flag — so they stay in the net figure.
|
|
19
19
|
* /proc/stat and /proc/<pid>/stat are both in USER_HZ clock ticks, so the
|
|
20
20
|
* deltas subtract directly. Off Linux the own reading is null and net = raw.
|
|
21
|
+
*
|
|
22
|
+
* Idle-priority exclusion (PRD 4021): 2026-11-23 6:20 PM PST the gate held all
|
|
23
|
+
* 163 pending jobs with 0 running while two local voice renders used ~13.5 of
|
|
24
|
+
* 14 cores; one ran at nice 19. A nice-19 process yields to anything at a
|
|
25
|
+
* higher priority, jobs included, so it is not load the gate should defend.
|
|
26
|
+
* The same bounded /proc scan that finds the own tree also collects every
|
|
27
|
+
* process at nice 19 (field 19 of /proc/<pid>/stat) that is not an own
|
|
28
|
+
* process; its ticks are subtracted from the net figure and reported as
|
|
29
|
+
* idleNiceShare. Nice 12 and below still count as busy.
|
|
21
30
|
*/
|
|
22
31
|
|
|
23
32
|
const fs = require('node:fs');
|
|
@@ -30,6 +39,12 @@ const DEFAULT_WINDOW_MS = 60_000;
|
|
|
30
39
|
const MIN_WINDOW_MS = 20_000;
|
|
31
40
|
const MAX_WINDOW_MS = 180_000;
|
|
32
41
|
const MIN_SPACING_MS = 5_000;
|
|
42
|
+
// One /proc scan reads at most this many pids, so a pid-storm host cannot turn
|
|
43
|
+
// a sample into thousands of file reads every tick.
|
|
44
|
+
const MAX_PROC_SCAN = 4096;
|
|
45
|
+
// The lowest scheduling priority Linux offers: such a process only gets CPU
|
|
46
|
+
// time no one else wants.
|
|
47
|
+
const IDLE_NICE = 19;
|
|
33
48
|
|
|
34
49
|
/**
|
|
35
50
|
* Parse the aggregate `cpu ` line of /proc/stat into { busy, iowait, total }.
|
|
@@ -91,24 +106,36 @@ function readCpuTimes({
|
|
|
91
106
|
}
|
|
92
107
|
|
|
93
108
|
/**
|
|
94
|
-
* Parse one /proc/<pid>/stat line → { pid, ppid, ticks: utime+stime }.
|
|
109
|
+
* Parse one /proc/<pid>/stat line → { pid, ppid, ticks: utime+stime, nice }.
|
|
95
110
|
* comm (field 2) may contain spaces and parens, so split after the LAST ')'.
|
|
111
|
+
* nice is null when the line is too short to carry field 19.
|
|
96
112
|
* @param {string} text
|
|
97
|
-
* @returns {{ pid: number, ppid: number, ticks: number } | null}
|
|
113
|
+
* @returns {{ pid: number, ppid: number, ticks: number, nice: number | null } | null}
|
|
98
114
|
*/
|
|
99
|
-
function
|
|
115
|
+
function parsePidStatFields(text) {
|
|
100
116
|
if (typeof text !== 'string' || !text) return null;
|
|
101
117
|
const close = text.lastIndexOf(')');
|
|
102
118
|
const open = text.indexOf('(');
|
|
103
119
|
if (close < 0 || open < 0) return null;
|
|
104
120
|
const pid = Number(text.slice(0, open).trim());
|
|
105
|
-
// After ')': state(3) ppid(4) ... utime(14) stime(15) → indices 0,1,...,11,12.
|
|
121
|
+
// After ')': state(3) ppid(4) ... utime(14) stime(15) ... nice(19) → indices 0,1,...,11,12,...,16.
|
|
106
122
|
const rest = text.slice(close + 1).trim().split(/\s+/);
|
|
107
123
|
const ppid = Number(rest[1]);
|
|
108
124
|
const utime = Number(rest[11]);
|
|
109
125
|
const stime = Number(rest[12]);
|
|
110
126
|
if (![pid, ppid, utime, stime].every(Number.isFinite)) return null;
|
|
111
|
-
|
|
127
|
+
const nice = rest.length > 16 && rest[16] !== '' ? Number(rest[16]) : NaN;
|
|
128
|
+
return { pid, ppid, ticks: utime + stime, nice: Number.isFinite(nice) ? nice : null };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Parse one /proc/<pid>/stat line → { pid, ppid, ticks: utime+stime }.
|
|
133
|
+
* @param {string} text
|
|
134
|
+
* @returns {{ pid: number, ppid: number, ticks: number } | null}
|
|
135
|
+
*/
|
|
136
|
+
function parsePidStat(text) {
|
|
137
|
+
const f = parsePidStatFields(text);
|
|
138
|
+
return f ? { pid: f.pid, ppid: f.ppid, ticks: f.ticks } : null;
|
|
112
139
|
}
|
|
113
140
|
|
|
114
141
|
/**
|
|
@@ -123,63 +150,87 @@ function isElectronHelperCmdline(cmd) {
|
|
|
123
150
|
}
|
|
124
151
|
|
|
125
152
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
153
|
+
* One bounded /proc scan (at most maxPids pids) → { own, idleNice }.
|
|
154
|
+
* - own: the app's own process tree as Map<pid, cumulative utime+stime ticks>:
|
|
155
|
+
* the root (Electron main) plus every descendant reached only through
|
|
156
|
+
* processes whose cmdline carries an Electron `--type=` flag. A helper must
|
|
157
|
+
* also run the same executable as the root (when /proc/<pid>/exe is
|
|
158
|
+
* readable), so a job whose prompt text happens to contain "--type=" never
|
|
159
|
+
* counts as own. null when the root is not in the scan.
|
|
160
|
+
* - idleNice: Map<pid, ticks> of every process at nice 19 that is not own
|
|
161
|
+
* (own ticks are already subtracted, so they never count twice).
|
|
162
|
+
* Linux only; both null elsewhere or when /proc cannot be listed. Never throws.
|
|
133
163
|
* @param {{ rootPid?: number, platform?: string, readdir?: (p: string) => string[],
|
|
134
|
-
* readFile?: (p: string) => string, readlink?: (p: string) => string }} [deps]
|
|
135
|
-
* @returns {Map<number, number> | null}
|
|
164
|
+
* readFile?: (p: string) => string, readlink?: (p: string) => string, maxPids?: number }} [deps]
|
|
165
|
+
* @returns {{ own: Map<number, number> | null, idleNice: Map<number, number> | null }}
|
|
136
166
|
*/
|
|
137
|
-
function
|
|
167
|
+
function readProcessTicks({
|
|
138
168
|
rootPid = process.pid,
|
|
139
169
|
platform = process.platform,
|
|
140
170
|
readdir = (p) => fs.readdirSync(p),
|
|
141
171
|
readFile = (p) => fs.readFileSync(p, 'utf8'),
|
|
142
172
|
readlink = (p) => fs.readlinkSync(p),
|
|
173
|
+
maxPids = MAX_PROC_SCAN,
|
|
143
174
|
} = {}) {
|
|
144
|
-
if (platform !== 'linux') return null;
|
|
175
|
+
if (platform !== 'linux') return { own: null, idleNice: null };
|
|
145
176
|
try {
|
|
146
|
-
/** @type {Map<number, { ppid: number, ticks: number }>} */
|
|
177
|
+
/** @type {Map<number, { ppid: number, ticks: number, nice: number | null }>} */
|
|
147
178
|
const procs = new Map();
|
|
148
179
|
/** @type {Map<number, number[]>} */
|
|
149
180
|
const children = new Map();
|
|
181
|
+
let scanned = 0;
|
|
150
182
|
for (const name of readdir('/proc')) {
|
|
151
183
|
if (!/^\d+$/.test(name)) continue;
|
|
184
|
+
if (scanned >= maxPids) break;
|
|
185
|
+
scanned += 1;
|
|
152
186
|
let st;
|
|
153
|
-
try { st =
|
|
187
|
+
try { st = parsePidStatFields(readFile(`/proc/${name}/stat`)); } catch { continue; }
|
|
154
188
|
if (!st) continue;
|
|
155
|
-
procs.set(st.pid, { ppid: st.ppid, ticks: st.ticks });
|
|
189
|
+
procs.set(st.pid, { ppid: st.ppid, ticks: st.ticks, nice: st.nice });
|
|
156
190
|
const list = children.get(st.ppid);
|
|
157
191
|
if (list) list.push(st.pid); else children.set(st.ppid, [st.pid]);
|
|
158
192
|
}
|
|
193
|
+
/** @type {Map<number, number> | null} */
|
|
194
|
+
let own = null;
|
|
159
195
|
const root = procs.get(rootPid);
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
196
|
+
if (root) {
|
|
197
|
+
const exeOf = (/** @type {number} */ pid) => { try { return readlink(`/proc/${pid}/exe`); } catch { return null; } };
|
|
198
|
+
const rootExe = exeOf(rootPid);
|
|
199
|
+
own = new Map([[rootPid, root.ticks]]);
|
|
200
|
+
const queue = [rootPid];
|
|
201
|
+
while (queue.length) {
|
|
202
|
+
const parent = /** @type {number} */ (queue.shift());
|
|
203
|
+
for (const child of children.get(parent) || []) {
|
|
204
|
+
let cmd = '';
|
|
205
|
+
try { cmd = readFile(`/proc/${child}/cmdline`); } catch { continue; }
|
|
206
|
+
if (!isElectronHelperCmdline(cmd)) continue;
|
|
207
|
+
if (rootExe !== null && exeOf(child) !== rootExe) continue;
|
|
208
|
+
own.set(child, /** @type {{ticks:number}} */ (procs.get(child)).ticks);
|
|
209
|
+
queue.push(child);
|
|
210
|
+
}
|
|
175
211
|
}
|
|
176
212
|
}
|
|
177
|
-
|
|
213
|
+
/** @type {Map<number, number>} */
|
|
214
|
+
const idleNice = new Map();
|
|
215
|
+
for (const [pid, p] of procs) {
|
|
216
|
+
if (p.nice === IDLE_NICE && !(own && own.has(pid))) idleNice.set(pid, p.ticks);
|
|
217
|
+
}
|
|
218
|
+
return { own, idleNice };
|
|
178
219
|
} catch {
|
|
179
|
-
return null;
|
|
220
|
+
return { own: null, idleNice: null };
|
|
180
221
|
}
|
|
181
222
|
}
|
|
182
223
|
|
|
224
|
+
/**
|
|
225
|
+
* The app's own process tree (see readProcessTicks). Linux only; null
|
|
226
|
+
* elsewhere, when /proc cannot be listed, or when the root is not found.
|
|
227
|
+
* @param {Parameters<typeof readProcessTicks>[0]} [deps]
|
|
228
|
+
* @returns {Map<number, number> | null}
|
|
229
|
+
*/
|
|
230
|
+
function readOwnProcessTicks(deps) {
|
|
231
|
+
return readProcessTicks(deps).own;
|
|
232
|
+
}
|
|
233
|
+
|
|
183
234
|
/**
|
|
184
235
|
* Own-process ticks consumed between two readings. A pid only in `before`
|
|
185
236
|
* exited (its last ticks are lost, count 0); a pid only in `after` started in
|
|
@@ -199,20 +250,49 @@ function ownTicksDelta(before, after) {
|
|
|
199
250
|
}
|
|
200
251
|
|
|
201
252
|
/**
|
|
253
|
+
* Nice-19 ticks consumed between two readings, counting only pids at nice 19
|
|
254
|
+
* in BOTH. Unlike own processes, a pid absent from `before` may have run the
|
|
255
|
+
* whole window at a higher priority and only now been reniced (its cumulative
|
|
256
|
+
* ticks would then wipe out real load), so it waits for the next window. Erring
|
|
257
|
+
* that way keeps the gate holding rather than launching onto a busy CPU.
|
|
258
|
+
* @param {Map<number, number> | null} before
|
|
259
|
+
* @param {Map<number, number> | null} after
|
|
260
|
+
* @returns {number | null}
|
|
261
|
+
*/
|
|
262
|
+
function idleNiceTicksDelta(before, after) {
|
|
263
|
+
if (!before || !after) return null;
|
|
264
|
+
let d = 0;
|
|
265
|
+
for (const [pid, ticks] of after) {
|
|
266
|
+
const prev = before.get(pid);
|
|
267
|
+
if (prev !== undefined) d += Math.max(0, ticks - prev);
|
|
268
|
+
}
|
|
269
|
+
return d;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* `readProcs` returns { own, idleNice } from one /proc scan (default
|
|
274
|
+
* readProcessTicks). Injecting `readOwn` and/or `readIdleNice` instead builds
|
|
275
|
+
* it from those (a missing one reads null).
|
|
202
276
|
* @param {{ read?: () => ({busy:number,iowait?:number,total:number}|null),
|
|
203
|
-
*
|
|
277
|
+
* readProcs?: () => ({ own: Map<number, number>|null, idleNice: Map<number, number>|null }),
|
|
278
|
+
* readOwn?: () => (Map<number, number>|null), readIdleNice?: () => (Map<number, number>|null),
|
|
279
|
+
* now?: () => number,
|
|
204
280
|
* windowMs?: number, minWindowMs?: number, maxWindowMs?: number, minSpacingMs?: number }} [opts]
|
|
205
281
|
*/
|
|
206
282
|
function createCpuSampler({
|
|
207
283
|
read = readCpuTimes,
|
|
208
|
-
readOwn
|
|
284
|
+
readOwn,
|
|
285
|
+
readIdleNice,
|
|
286
|
+
readProcs = (readOwn || readIdleNice)
|
|
287
|
+
? () => ({ own: readOwn ? readOwn() : null, idleNice: readIdleNice ? readIdleNice() : null })
|
|
288
|
+
: () => readProcessTicks(),
|
|
209
289
|
now = () => Date.now(),
|
|
210
290
|
windowMs = DEFAULT_WINDOW_MS,
|
|
211
291
|
minWindowMs = MIN_WINDOW_MS,
|
|
212
292
|
maxWindowMs = MAX_WINDOW_MS,
|
|
213
293
|
minSpacingMs = MIN_SPACING_MS,
|
|
214
294
|
} = {}) {
|
|
215
|
-
/** @type {{ at: number, busy: number, iowait: number, total: number, own: Map<number, number> | null }[]} */
|
|
295
|
+
/** @type {{ at: number, busy: number, iowait: number, total: number, own: Map<number, number> | null, idleNice: Map<number, number> | null }[]} */
|
|
216
296
|
let entries = [];
|
|
217
297
|
|
|
218
298
|
function observe() {
|
|
@@ -221,7 +301,8 @@ function createCpuSampler({
|
|
|
221
301
|
if (!r) return;
|
|
222
302
|
const newest = entries[entries.length - 1];
|
|
223
303
|
if (newest && t - newest.at < minSpacingMs) return;
|
|
224
|
-
|
|
304
|
+
const p = readProcs() || { own: null, idleNice: null };
|
|
305
|
+
entries.push({ at: t, busy: r.busy, iowait: r.iowait ?? 0, total: r.total, own: p.own ?? null, idleNice: p.idleNice ?? null });
|
|
225
306
|
entries = entries.filter((e) => t - e.at <= maxWindowMs);
|
|
226
307
|
}
|
|
227
308
|
|
|
@@ -246,12 +327,17 @@ function createCpuSampler({
|
|
|
246
327
|
const dIowait = (r.iowait ?? 0) - best.iowait;
|
|
247
328
|
// Per-CPU idle and iowait counters can step backwards.
|
|
248
329
|
if (dTotal <= 0 || dBusy < 0) return null;
|
|
249
|
-
const
|
|
330
|
+
const p = readProcs() || { own: null, idleNice: null };
|
|
331
|
+
const dOwnRaw = ownTicksDelta(best.own, p.own ?? null);
|
|
250
332
|
const dOwn = dOwnRaw === null ? null : Math.min(dBusy, dOwnRaw);
|
|
333
|
+
// Clamped so net never drops below 0.
|
|
334
|
+
const dIdleRaw = idleNiceTicksDelta(best.idleNice, p.idleNice ?? null);
|
|
335
|
+
const dIdle = dIdleRaw === null ? null : Math.min(dBusy - (dOwn ?? 0), dIdleRaw);
|
|
251
336
|
return {
|
|
252
337
|
busyFraction: Math.min(1, dBusy / dTotal),
|
|
253
338
|
ownFraction: dOwn === null ? null : dOwn / dTotal,
|
|
254
|
-
|
|
339
|
+
idleNiceShare: dIdle === null ? null : dIdle / dTotal,
|
|
340
|
+
netBusyFraction: Math.min(1, (dBusy - (dOwn ?? 0) - (dIdle ?? 0)) / dTotal),
|
|
255
341
|
iowaitFraction: Math.min(1, Math.max(0, dIowait / dTotal)),
|
|
256
342
|
windowMs: t - best.at,
|
|
257
343
|
};
|
|
@@ -263,12 +349,17 @@ function createCpuSampler({
|
|
|
263
349
|
module.exports = {
|
|
264
350
|
parseProcStat,
|
|
265
351
|
parsePidStat,
|
|
352
|
+
parsePidStatFields,
|
|
353
|
+
readProcessTicks,
|
|
266
354
|
readOwnProcessTicks,
|
|
267
355
|
ownTicksDelta,
|
|
356
|
+
idleNiceTicksDelta,
|
|
268
357
|
readCpuTimes,
|
|
269
358
|
createCpuSampler,
|
|
270
359
|
DEFAULT_WINDOW_MS,
|
|
271
360
|
MIN_WINDOW_MS,
|
|
272
361
|
MAX_WINDOW_MS,
|
|
273
362
|
MIN_SPACING_MS,
|
|
363
|
+
MAX_PROC_SCAN,
|
|
364
|
+
IDLE_NICE,
|
|
274
365
|
};
|