claude-code-session-manager 0.142.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 +20 -107
- package/dist/assets/{AppSettings-BcZgIvvY.js → AppSettings-BO4jDVGO.js} +1 -1
- package/dist/assets/{History-DpZuJXi1.js → History-DTttbvPb.js} +1 -1
- package/dist/assets/{HomeClaudeConfig-D32ZJtU-.js → HomeClaudeConfig-DADP5bfn.js} +2 -2
- package/dist/assets/{Panel-BjdJjhqt.js → Panel-B_D4Bg7L.js} +1 -1
- package/dist/assets/{ProjectClaudeConfig-BTClYV6i.js → ProjectClaudeConfig-DPnDJKcr.js} +1 -1
- package/dist/assets/{Scheduler-DPobKx1Z.js → Scheduler-CNLjn90m.js} +8 -8
- package/dist/assets/{SkillReferenceGraph-Cqyu4mef.js → SkillReferenceGraph-D1b2aiol.js} +1 -1
- package/dist/assets/{Skills-vv0noNVI.js → Skills-cfV7sgk6.js} +1 -1
- package/dist/assets/{TiptapBody-CXi8Pvut.js → TiptapBody-DVXtEIVy.js} +1 -1
- package/dist/assets/{Toggle-CiYCWLjh.js → Toggle-CYajnSdx.js} +1 -1
- package/dist/assets/{VoiceButton-4NKKd1sC.js → VoiceButton-DJ4gGRuE.js} +1 -1
- package/dist/assets/{historyTotals-DDunb8Ch.js → historyTotals-D3MoHY2U.js} +1 -1
- package/dist/assets/{index-Cn5E9958.js → index-CdTppjKR.js} +436 -436
- package/dist/index.html +1 -1
- package/dist-web/app.js +356 -356
- package/package.json +3 -3
- package/plugins/session-manager-dev/.claude-plugin/plugin.json +1 -1
- package/plugins/session-manager-dev/skills/builder/SKILL.md +5 -0
- package/plugins/session-manager-dev/skills/develop/SKILL.md +9 -3
- package/scripts/README.md +1 -0
- package/scripts/scheduler-mcp-server.cjs +304 -6
- package/src/main/adminRouteRegistry.cjs +10 -0
- package/src/main/build-info.json +4 -4
- package/src/main/chatRunner.cjs +105 -7
- package/src/main/config.cjs +5 -1
- package/src/main/health.cjs +38 -2
- package/src/main/index.cjs +12 -4
- package/src/main/ipcSchemas.cjs +11 -0
- package/src/main/lib/demo-video-kit/cast.md +10 -1
- package/src/main/lib/demo-video-kit/character.cjs +77 -21
- package/src/main/lib/demo-video-kit/kind-tiktok.md +5 -1
- package/src/main/lib/demo-video-kit/projectSounds.cjs +191 -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/epic/activeIndexMerge.cjs +5 -0
- package/src/main/lib/epic/artifactRetention.cjs +55 -9
- package/src/main/lib/epic/artifactStore.cjs +78 -35
- package/src/main/lib/epic/delegationReadiness.cjs +10 -3
- package/src/main/lib/epic/epicEventLog.cjs +45 -1
- package/src/main/lib/epic/epicStateEmitter.cjs +248 -0
- package/src/main/lib/epic/epicValidationHook.cjs +13 -2
- package/src/main/lib/externalContract.cjs +12 -1
- package/src/main/lib/feedback-remote/autoStartedSession.cjs +1 -1
- package/src/main/lib/feedback-remote/crossProjectReply.cjs +96 -8
- package/src/main/lib/feedback-remote/macroAdminRoutes.cjs +30 -0
- package/src/main/lib/feedback-remote/pendingReplies.cjs +18 -0
- package/src/main/lib/feedback-remote/projectHomeAdminRoutes.cjs +94 -4
- package/src/main/lib/feedback-remote/replyClaims.cjs +159 -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 +113 -10
- package/src/main/lib/mcpToolCatalog.cjs +102 -8
- package/src/main/lib/opsOwnership.cjs +113 -28
- package/src/main/lib/persona-macro/shippedPersonaSeeds.cjs +46 -0
- package/src/main/lib/prd/prdCreate.cjs +350 -227
- package/src/main/lib/prd/prdCriteria.cjs +7 -4
- package/src/main/lib/project-pages/videoPressLines.cjs +145 -0
- 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/scheduler/criteriaChecks.cjs +164 -20
- package/src/main/lib/scheduler/heldSchedulerTurns.cjs +238 -0
- package/src/main/lib/scheduler/jobMemoryStats.cjs +13 -2
- package/src/main/lib/scheduler/planRecordHarvest.cjs +120 -52
- package/src/main/lib/scheduler/prdOutcomeRecord.cjs +60 -2
- package/src/main/lib/scheduler/queueStore.cjs +16 -0
- package/src/main/lib/scheduler/rcaReport.cjs +79 -15
- package/src/main/lib/scheduler/scheduleJobSchema.cjs +13 -0
- package/src/main/lib/scheduler/schedulerPaths.cjs +9 -0
- package/src/main/lib/scheduler/slotsRoute.cjs +101 -87
- package/src/main/lib/scheduler/usageHoldRoute.cjs +97 -0
- package/src/main/lib/scheduler/validatorVerdictSchema.cjs +16 -5
- package/src/main/lib/tasks/memoryConsolidation.cjs +14 -2
- package/src/main/lib/tasks/mitigationReview.cjs +21 -1
- package/src/main/lib/tasks/taskAdminRoutes.cjs +78 -3
- package/src/main/lib/tasks/taskLedger.cjs +2 -2
- package/src/main/lib/telemetry/auditEvents.cjs +21 -24
- package/src/main/lib/telemetry/auditLog.cjs +6 -2
- package/src/main/lib/util/atomicFs.cjs +2 -2
- package/src/main/promptSessionEvents.cjs +16 -1
- package/src/main/pty.cjs +11 -2
- package/src/main/queueOps.cjs +1 -1
- package/src/main/runVerify.cjs +1 -1
- package/src/main/scheduler/utilizationHold.cjs +96 -6
- package/src/main/scheduler.cjs +533 -39
- package/src/main/templates/PRD_AUTHORING.md +7 -2
- package/src/preload/api.d.ts +47 -1
- package/src/preload/apiSurface.json +14 -0
- package/src/preload/index.cjs +2 -0
- package/src/seed/agents/demo-video-builder.md +15 -8
- package/src/seed/agents/validator.md +20 -19
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
* criterion can be checked mechanically:
|
|
9
9
|
* - command: spec is one gate command; must pass prdGateFiles.validateGate.
|
|
10
10
|
* - file: spec is a repo-relative path string, or { path, contains? }.
|
|
11
|
-
* - http: spec is a loopback URL string, or { url, status? } (status defaults
|
|
11
|
+
* - http: spec is a loopback URL string, or { url, status?, contains? } (status defaults
|
|
12
|
+
* to 200; contains is one-line text the response body must include).
|
|
12
13
|
* - manual: spec is optional one-line guidance for the human/validator.
|
|
13
14
|
*
|
|
14
15
|
* Pure, no I/O. normalizeCriteria throws a CriteriaError naming the 1-based
|
|
@@ -71,9 +72,9 @@ function normalizeFile(spec, idx) {
|
|
|
71
72
|
function normalizeHttp(spec, idx) {
|
|
72
73
|
const obj = typeof spec === 'string' ? { url: spec } : spec;
|
|
73
74
|
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) {
|
|
74
|
-
throw new CriteriaError(idx, 'http check spec must be a loopback URL or { url, status? }.');
|
|
75
|
+
throw new CriteriaError(idx, 'http check spec must be a loopback URL or { url, status?, contains? }.');
|
|
75
76
|
}
|
|
76
|
-
const extra = Object.keys(obj).filter((k) => k !== 'url' && k !== 'status');
|
|
77
|
+
const extra = Object.keys(obj).filter((k) => k !== 'url' && k !== 'status' && k !== 'contains');
|
|
77
78
|
if (extra.length) throw new CriteriaError(idx, `http check spec has unknown key(s): ${extra.join(', ')}.`);
|
|
78
79
|
const raw = oneLine(obj.url, 2000, 'http check url', idx);
|
|
79
80
|
let url;
|
|
@@ -93,7 +94,9 @@ function normalizeHttp(spec, idx) {
|
|
|
93
94
|
if (!Number.isInteger(status) || status < 100 || status > 599) {
|
|
94
95
|
throw new CriteriaError(idx, 'http check status must be an integer from 100 to 599.');
|
|
95
96
|
}
|
|
96
|
-
|
|
97
|
+
const out = { url: url.href, status };
|
|
98
|
+
if (obj.contains !== undefined) out.contains = oneLine(obj.contains, CONTAINS_MAX_CHARS, 'http check contains', idx);
|
|
99
|
+
return out;
|
|
97
100
|
}
|
|
98
101
|
|
|
99
102
|
function normalizeCheck(check, idx) {
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* videoPressLines.cjs — a scripted Demo Video press: validate its choices and
|
|
5
|
+
* build exactly the prompt lines the Project Home composer sends for the same
|
|
6
|
+
* choices, so a scripted press runs the same job as a human press.
|
|
7
|
+
*
|
|
8
|
+
* Pure: no fs, no IPC. Mirrors renderer composerTemplates.ts (VIDEO_KINDS,
|
|
9
|
+
* videoLengthsFor, voiceLine, videoPromptLines); main may not import renderer
|
|
10
|
+
* code, so __tests__/videoPressLines.test.cjs asserts the two stay identical.
|
|
11
|
+
* Whether a voice exists in the project's library is the route's check, not this one.
|
|
12
|
+
*
|
|
13
|
+
* parseScriptPress(body) -> { ok: true, cfg } | { ok: false, error } (error: one line)
|
|
14
|
+
* body: { kind, target?, length?, style?, character?, voiceId?, focus?, reviewFirst? }
|
|
15
|
+
* cfg (renderer VideoCfg shape): { kind, target, style, length, mascot, review, focus, voice? }
|
|
16
|
+
* pressLines(cfg) -> string[]
|
|
17
|
+
*
|
|
18
|
+
* Review first travels only as cfg.review (the press's reviewStage), never as a
|
|
19
|
+
* line; a 'Review:' or 'Replace entry:' line in target or focus is refused so a
|
|
20
|
+
* script cannot forge an owner approval or overwrite an entry.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const { VOICE_ID_RE } = require('../demo-video-kit/voiceLibrary.cjs');
|
|
24
|
+
|
|
25
|
+
const VIDEO_KINDS = Object.freeze(['Project demo', 'Blog post', 'Feature', 'TikTok', 'Marketing']);
|
|
26
|
+
const KIND_SLUGS = Object.freeze({
|
|
27
|
+
'project-demo': 'Project demo',
|
|
28
|
+
'blog-post': 'Blog post',
|
|
29
|
+
feature: 'Feature',
|
|
30
|
+
tiktok: 'TikTok',
|
|
31
|
+
marketing: 'Marketing',
|
|
32
|
+
});
|
|
33
|
+
const VIDEO_STYLES = Object.freeze(['Auto', 'Professional', 'Fun', 'Animated whiteboard', 'Kinetic typography']);
|
|
34
|
+
const TARGET_REQUIRED_KINDS = Object.freeze(['Blog post', 'Feature']);
|
|
35
|
+
const MAX_TARGET = 500;
|
|
36
|
+
const MAX_FOCUS = 2000;
|
|
37
|
+
const RESERVED_LINE_RE = /^\s*(review|replace entry)\s*:/im;
|
|
38
|
+
const CONTROL_RE = /[\u0000-\u001f\u007f]/;
|
|
39
|
+
const FOCUS_CONTROL_RE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/;
|
|
40
|
+
|
|
41
|
+
/** The length chips a kind offers: 15 s only for TikTok (renderer videoLengthsFor). */
|
|
42
|
+
function videoLengthsFor(kind) {
|
|
43
|
+
return kind === 'TikTok' ? [15, 30, 60] : [30, 60];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function fail(error) {
|
|
47
|
+
return { ok: false, error };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function parseScriptPress(body) {
|
|
51
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) return fail('press body must be an object');
|
|
52
|
+
|
|
53
|
+
const rawKind = body.kind;
|
|
54
|
+
if (typeof rawKind !== 'string') return fail(`kind is required: one of ${VIDEO_KINDS.join(', ')}`);
|
|
55
|
+
const kind = VIDEO_KINDS.includes(rawKind) ? rawKind : Object.prototype.hasOwnProperty.call(KIND_SLUGS, rawKind) ? KIND_SLUGS[rawKind] : null;
|
|
56
|
+
if (!kind) return fail(`unknown kind '${oneLine(rawKind)}': one of ${VIDEO_KINDS.join(', ')} (or its slug)`);
|
|
57
|
+
|
|
58
|
+
let length = 30;
|
|
59
|
+
if (body.length !== undefined) {
|
|
60
|
+
const allowed = videoLengthsFor(kind);
|
|
61
|
+
if (typeof body.length !== 'number' || !allowed.includes(body.length)) {
|
|
62
|
+
return fail(`length for ${kind} must be one of ${allowed.join(', ')}`);
|
|
63
|
+
}
|
|
64
|
+
length = body.length;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let style = 'Auto';
|
|
68
|
+
if (body.style !== undefined) {
|
|
69
|
+
if (typeof body.style !== 'string' || !VIDEO_STYLES.includes(body.style)) {
|
|
70
|
+
return fail(`style must be one of ${VIDEO_STYLES.join(', ')}`);
|
|
71
|
+
}
|
|
72
|
+
style = body.style;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let mascot = true;
|
|
76
|
+
if (body.character !== undefined) {
|
|
77
|
+
if (typeof body.character !== 'boolean') return fail('character must be true or false');
|
|
78
|
+
mascot = body.character;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let review = false;
|
|
82
|
+
if (body.reviewFirst !== undefined) {
|
|
83
|
+
if (typeof body.reviewFirst !== 'boolean') return fail('reviewFirst must be true or false');
|
|
84
|
+
review = body.reviewFirst;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let voice;
|
|
88
|
+
if (body.voiceId !== undefined) {
|
|
89
|
+
if (typeof body.voiceId !== 'string' || !VOICE_ID_RE.test(body.voiceId)) {
|
|
90
|
+
return fail(`voiceId must match ${VOICE_ID_RE}`);
|
|
91
|
+
}
|
|
92
|
+
voice = body.voiceId;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let target = '';
|
|
96
|
+
if (body.target !== undefined) {
|
|
97
|
+
if (typeof body.target !== 'string') return fail('target must be a string');
|
|
98
|
+
if (CONTROL_RE.test(body.target)) return fail('target must be one line with no control characters');
|
|
99
|
+
target = body.target.trim();
|
|
100
|
+
if (target.length > MAX_TARGET) return fail(`target must be at most ${MAX_TARGET} characters`);
|
|
101
|
+
if (RESERVED_LINE_RE.test(target)) return fail("target may not carry a 'Review:' or 'Replace entry:' line");
|
|
102
|
+
}
|
|
103
|
+
if (TARGET_REQUIRED_KINDS.includes(kind) && !target) return fail(`target is required for a ${kind} video`);
|
|
104
|
+
|
|
105
|
+
let focus = '';
|
|
106
|
+
if (body.focus !== undefined) {
|
|
107
|
+
if (typeof body.focus !== 'string') return fail('focus must be a string');
|
|
108
|
+
if (FOCUS_CONTROL_RE.test(body.focus)) return fail('focus may not contain control characters');
|
|
109
|
+
focus = body.focus.trim();
|
|
110
|
+
if (focus.length > MAX_FOCUS) return fail(`focus must be at most ${MAX_FOCUS} characters`);
|
|
111
|
+
if (RESERVED_LINE_RE.test(focus)) return fail("focus may not carry a 'Review:' or 'Replace entry:' line");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const cfg = { kind, target, style, length, mascot, review, focus };
|
|
115
|
+
if (voice) cfg.voice = voice;
|
|
116
|
+
return { ok: true, cfg };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Same lines as renderer videoPromptLines(cfg); a target is named only for a non-demo kind. */
|
|
120
|
+
function pressLines(cfg) {
|
|
121
|
+
const lines = [`Video kind: ${cfg.kind}`];
|
|
122
|
+
const target = (cfg.target || '').trim();
|
|
123
|
+
if (cfg.kind !== 'Project demo' && target) lines.push(`Video target: ${target}`);
|
|
124
|
+
lines.push(`Video style: ${cfg.style}`);
|
|
125
|
+
const voice = typeof cfg.voice === 'string' ? cfg.voice.trim() : '';
|
|
126
|
+
if (voice) lines.push(`Voice: ${voice}`);
|
|
127
|
+
lines.push(`Video length: ${cfg.length}`, `Character: ${cfg.mascot ? 'on' : 'off'}`);
|
|
128
|
+
const focus = (cfg.focus || '').trim();
|
|
129
|
+
if (focus) lines.push(focus);
|
|
130
|
+
return lines;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function oneLine(s) {
|
|
134
|
+
return String(s).replace(/[\u0000-\u001f\u007f]+/g, ' ').slice(0, 80);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = {
|
|
138
|
+
parseScriptPress,
|
|
139
|
+
pressLines,
|
|
140
|
+
VIDEO_KINDS,
|
|
141
|
+
VIDEO_STYLES,
|
|
142
|
+
KIND_SLUGS,
|
|
143
|
+
MAX_TARGET,
|
|
144
|
+
MAX_FOCUS,
|
|
145
|
+
};
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* videoScriptPress.cjs — the per-project human switch 'Scripts may run Demo
|
|
5
|
+
* Video' and the small ledger of scripted Demo Video presses.
|
|
6
|
+
*
|
|
7
|
+
* Two MACHINE-LEVEL files beside task-switches.json (schedulerPaths):
|
|
8
|
+
* videoScriptSwitchesPath() — keyed by absolute project cwd (path.resolve):
|
|
9
|
+
* { "v": 1, "projects": { "/abs/cwd": { "enabled": true, "changedAt": "ISO", "by": {...} } } }
|
|
10
|
+
* `by` records WHO made the last change: { via: 'ui' } or
|
|
11
|
+
* { via: 'agent', claudeSessionId, quote, promptAt }. An entry without a
|
|
12
|
+
* well-formed `by` reads as { via: 'unknown' }; reads never rewrite the file.
|
|
13
|
+
* videoScriptRunsPath() — the scripted-press ledger, newest last, at most
|
|
14
|
+
* MAX_RUNS_PER_PROJECT runs per project (oldest dropped):
|
|
15
|
+
* { "v": 1, "projects": { "/abs/cwd": [ { entryId, epicId, slug, externalRef?, kind, at } ] } }
|
|
16
|
+
* Both live outside every project ops root, so opsOwnership does not govern
|
|
17
|
+
* them (same reason as tasks/taskSwitch.cjs).
|
|
18
|
+
*
|
|
19
|
+
* EXACTLY TWO WRITERS of the switch: it is a human decision, recorded with who made it.
|
|
20
|
+
* - The UI IPC is a human click: by.via 'ui'.
|
|
21
|
+
* - The agent route POST /admin/project-home/videos/script-switch writes
|
|
22
|
+
* by.via 'agent', and only on a verified human quote.
|
|
23
|
+
* (Both land in later PRDs.) Any other write is outside the law and reads as
|
|
24
|
+
* by.via 'unknown'. The switch is a consent gate, not a sandbox.
|
|
25
|
+
* The ledger's ONLY writer is the scripted-press route
|
|
26
|
+
* POST /admin/project-home/videos/run; it refuses presses while the switch is
|
|
27
|
+
* off and reads the ledger to answer a run's status by entry id.
|
|
28
|
+
*
|
|
29
|
+
* Switching off keeps the entry (enabled:false + changedAt). Switch reads are
|
|
30
|
+
* cached by file mtime; a missing or corrupt file reads as all-off / empty
|
|
31
|
+
* and never throws.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
const fs = require('node:fs');
|
|
35
|
+
const path = require('node:path');
|
|
36
|
+
const { videoScriptSwitchesPath, videoScriptRunsPath } = require('../scheduler/schedulerPaths.cjs');
|
|
37
|
+
const atomicFs = require('../util/atomicFs.cjs');
|
|
38
|
+
|
|
39
|
+
const VERSION = 1;
|
|
40
|
+
const MAX_RUNS_PER_PROJECT = 200;
|
|
41
|
+
|
|
42
|
+
/** @type {{ file: string, mtimeMs: number, data: Record<string, { enabled: boolean, changedAt: string, by: object | null }> } | null} */
|
|
43
|
+
let cache = null;
|
|
44
|
+
|
|
45
|
+
function normalizeCwd(cwd) {
|
|
46
|
+
if (typeof cwd !== 'string' || cwd.trim() === '') return null;
|
|
47
|
+
return path.resolve(cwd);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const BY_ERROR = "setVideoScriptSwitch: by must be {via:'ui'} or {via:'agent', claudeSessionId, quote, promptAt}";
|
|
51
|
+
const UNKNOWN_BY = Object.freeze({ via: 'unknown' });
|
|
52
|
+
|
|
53
|
+
function boundedString(v, max) {
|
|
54
|
+
return typeof v === 'string' && v.length >= 1 && v.length <= max;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isIsoish(v) {
|
|
58
|
+
return typeof v === 'string' && v.trim() !== '' && !Number.isNaN(Date.parse(v));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Normalized copy of a well-formed attribution, or null (same rules as taskSwitch). */
|
|
62
|
+
function validBy(by) {
|
|
63
|
+
if (!by || typeof by !== 'object' || Array.isArray(by)) return null;
|
|
64
|
+
if (by.via === 'ui') {
|
|
65
|
+
return Object.keys(by).length === 1 ? { via: 'ui' } : null;
|
|
66
|
+
}
|
|
67
|
+
if (by.via === 'agent') {
|
|
68
|
+
const { claudeSessionId, quote, promptAt } = by;
|
|
69
|
+
if (!boundedString(claudeSessionId, 128)) return null;
|
|
70
|
+
if (!boundedString(quote, 2000)) return null;
|
|
71
|
+
if (!isIsoish(promptAt)) return null;
|
|
72
|
+
return { via: 'agent', claudeSessionId, quote, promptAt };
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function projectsOf(parsed) {
|
|
78
|
+
const projects = parsed && typeof parsed === 'object' ? parsed.projects : null;
|
|
79
|
+
if (!projects || typeof projects !== 'object' || Array.isArray(projects)) return null;
|
|
80
|
+
return projects;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* ── switch ─────────────────────────────────────────────────────────────── */
|
|
84
|
+
|
|
85
|
+
function sanitizeSwitches(parsed) {
|
|
86
|
+
const out = {};
|
|
87
|
+
const projects = projectsOf(parsed);
|
|
88
|
+
if (!projects) return out;
|
|
89
|
+
for (const [cwd, entry] of Object.entries(projects)) {
|
|
90
|
+
if (!entry || typeof entry !== 'object') continue;
|
|
91
|
+
out[path.resolve(cwd)] = {
|
|
92
|
+
enabled: entry.enabled === true,
|
|
93
|
+
changedAt: typeof entry.changedAt === 'string' ? entry.changedAt : '',
|
|
94
|
+
by: validBy(entry.by),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function readSwitches() {
|
|
101
|
+
let file;
|
|
102
|
+
try {
|
|
103
|
+
file = videoScriptSwitchesPath();
|
|
104
|
+
} catch {
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
let mtimeMs;
|
|
108
|
+
try {
|
|
109
|
+
mtimeMs = fs.statSync(file).mtimeMs;
|
|
110
|
+
} catch {
|
|
111
|
+
cache = null;
|
|
112
|
+
return {};
|
|
113
|
+
}
|
|
114
|
+
if (cache && cache.file === file && cache.mtimeMs === mtimeMs) return cache.data;
|
|
115
|
+
const data = sanitizeSwitches(atomicFs.readJsonOrSync(file, null));
|
|
116
|
+
cache = { file, mtimeMs, data };
|
|
117
|
+
return data;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** True only when `cwd`'s switch is explicitly on. Never throws. */
|
|
121
|
+
function isVideoScriptSwitchOn(cwd) {
|
|
122
|
+
try {
|
|
123
|
+
const key = normalizeCwd(cwd);
|
|
124
|
+
if (!key) return false;
|
|
125
|
+
const entry = readSwitches()[key];
|
|
126
|
+
return !!(entry && entry.enabled);
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* One of the two sanctioned writers only (see header). Records
|
|
134
|
+
* { enabled, changedAt, by }; switching off keeps the entry. An invalid `by`
|
|
135
|
+
* throws before any fs call.
|
|
136
|
+
*/
|
|
137
|
+
function setVideoScriptSwitch(cwd, enabled, by) {
|
|
138
|
+
const attribution = validBy(by);
|
|
139
|
+
if (!attribution) throw new Error(BY_ERROR);
|
|
140
|
+
const key = normalizeCwd(cwd);
|
|
141
|
+
if (!key) throw new Error('setVideoScriptSwitch: cwd must be a non-empty string');
|
|
142
|
+
const projects = { ...readSwitches() };
|
|
143
|
+
projects[key] = { enabled: enabled === true, changedAt: new Date().toISOString(), by: attribution };
|
|
144
|
+
const file = videoScriptSwitchesPath();
|
|
145
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
146
|
+
atomicFs.writeJsonAtomicSync(file, { v: VERSION, projects });
|
|
147
|
+
cache = null;
|
|
148
|
+
return { cwd: key, enabled: projects[key].enabled, changedAt: projects[key].changedAt, by: { ...attribution } };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* One project's entry with attribution. changedAt/by null when there is no
|
|
153
|
+
* entry; by { via:'unknown' } for an entry without a well-formed by; null for
|
|
154
|
+
* a non-string or empty cwd. Never throws.
|
|
155
|
+
*/
|
|
156
|
+
function getVideoScriptSwitch(cwd) {
|
|
157
|
+
try {
|
|
158
|
+
const key = normalizeCwd(cwd);
|
|
159
|
+
if (!key) return null;
|
|
160
|
+
const entry = readSwitches()[key];
|
|
161
|
+
if (!entry) return { cwd: key, enabled: false, changedAt: null, by: null };
|
|
162
|
+
return { cwd: key, enabled: entry.enabled, changedAt: entry.changedAt, by: entry.by ? { ...entry.by } : { ...UNKNOWN_BY } };
|
|
163
|
+
} catch {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/* ── ledger ─────────────────────────────────────────────────────────────── */
|
|
169
|
+
|
|
170
|
+
/** Normalized run record, or null when malformed. */
|
|
171
|
+
function validRun(run) {
|
|
172
|
+
if (!run || typeof run !== 'object' || Array.isArray(run)) return null;
|
|
173
|
+
const { entryId, epicId, slug, externalRef, kind, at } = run;
|
|
174
|
+
if (!boundedString(entryId, 200)) return null;
|
|
175
|
+
if (!boundedString(epicId, 200)) return null;
|
|
176
|
+
if (!boundedString(slug, 200)) return null;
|
|
177
|
+
if (!boundedString(kind, 100)) return null;
|
|
178
|
+
if (!isIsoish(at)) return null;
|
|
179
|
+
if (externalRef !== undefined && externalRef !== null && !boundedString(externalRef, 500)) return null;
|
|
180
|
+
const out = { entryId, epicId, slug, kind, at };
|
|
181
|
+
if (typeof externalRef === 'string') out.externalRef = externalRef;
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function readRuns() {
|
|
186
|
+
let file;
|
|
187
|
+
try {
|
|
188
|
+
file = videoScriptRunsPath();
|
|
189
|
+
} catch {
|
|
190
|
+
return {};
|
|
191
|
+
}
|
|
192
|
+
const projects = projectsOf(atomicFs.readJsonOrSync(file, null));
|
|
193
|
+
const out = {};
|
|
194
|
+
if (!projects) return out;
|
|
195
|
+
for (const [cwd, list] of Object.entries(projects)) {
|
|
196
|
+
if (!Array.isArray(list)) continue;
|
|
197
|
+
const runs = list.map(validRun).filter(Boolean);
|
|
198
|
+
if (runs.length) out[path.resolve(cwd)] = runs;
|
|
199
|
+
}
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The scripted-press route's write (see header). Appends the run (an earlier
|
|
205
|
+
* run with the same entryId is replaced), keeps the newest
|
|
206
|
+
* MAX_RUNS_PER_PROJECT for the project, and returns the stored record.
|
|
207
|
+
* Throws on a malformed cwd or run before any write.
|
|
208
|
+
*/
|
|
209
|
+
function recordScriptRun(cwd, run) {
|
|
210
|
+
const key = normalizeCwd(cwd);
|
|
211
|
+
if (!key) throw new Error('recordScriptRun: cwd must be a non-empty string');
|
|
212
|
+
const record = validRun(run);
|
|
213
|
+
if (!record) throw new Error('recordScriptRun: run must be {entryId, epicId, slug, externalRef?, kind, at(ISO)}');
|
|
214
|
+
const projects = readRuns();
|
|
215
|
+
const list = (projects[key] || []).filter((r) => r.entryId !== record.entryId);
|
|
216
|
+
list.push(record);
|
|
217
|
+
projects[key] = list.length > MAX_RUNS_PER_PROJECT ? list.slice(list.length - MAX_RUNS_PER_PROJECT) : list;
|
|
218
|
+
const file = videoScriptRunsPath();
|
|
219
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
220
|
+
atomicFs.writeJsonAtomicSync(file, { v: VERSION, projects });
|
|
221
|
+
return { ...record };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* A project's run by entryId (preferred) or by externalRef (newest match), or
|
|
226
|
+
* null. Never throws.
|
|
227
|
+
*/
|
|
228
|
+
function findScriptRun(cwd, query) {
|
|
229
|
+
try {
|
|
230
|
+
const key = normalizeCwd(cwd);
|
|
231
|
+
if (!key || !query || typeof query !== 'object') return null;
|
|
232
|
+
const list = readRuns()[key];
|
|
233
|
+
if (!list) return null;
|
|
234
|
+
let hit;
|
|
235
|
+
if (typeof query.entryId === 'string' && query.entryId) {
|
|
236
|
+
hit = list.find((r) => r.entryId === query.entryId);
|
|
237
|
+
} else if (typeof query.externalRef === 'string' && query.externalRef) {
|
|
238
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
239
|
+
if (list[i].externalRef === query.externalRef) { hit = list[i]; break; }
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return hit ? { ...hit } : null;
|
|
243
|
+
} catch {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function _resetForTests() {
|
|
249
|
+
cache = null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
module.exports = {
|
|
253
|
+
MAX_RUNS_PER_PROJECT,
|
|
254
|
+
getVideoScriptSwitch,
|
|
255
|
+
setVideoScriptSwitch,
|
|
256
|
+
isVideoScriptSwitchOn,
|
|
257
|
+
recordScriptRun,
|
|
258
|
+
findScriptRun,
|
|
259
|
+
_resetForTests,
|
|
260
|
+
};
|