claude-code-session-manager 0.138.0 → 0.140.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 +352 -79
- package/dist/assets/AppSettings-CMFEM68R.js +2 -0
- package/dist/assets/History--JwsFnbn.js +2 -0
- package/dist/assets/HomeClaudeConfig-ijEXs--3.js +3 -0
- package/dist/assets/{Panel-CeDiyuNn.js → Panel-CALyU0pe.js} +1 -1
- package/dist/assets/ProjectClaudeConfig-sdlA-fUb.js +26 -0
- package/dist/assets/Scheduler-YT3j8SUa.js +16 -0
- package/dist/assets/{SkillReferenceGraph-CFMK8NJh.js → SkillReferenceGraph-CxAuwhNN.js} +1 -1
- package/dist/assets/Skills-DtRzghn5.js +67 -0
- package/dist/assets/{TiptapBody-D1kTj-tn.js → TiptapBody-DApeu7x8.js} +60 -54
- package/dist/assets/ViewTabs-B7qLXRAH.js +1 -0
- package/dist/assets/{VoiceButton-CZeyPM31.js → VoiceButton-DtStfv1H.js} +1 -1
- package/dist/assets/{historyTotals-B00l64Jw.js → historyTotals-ydfnwepc.js} +1 -1
- package/dist/assets/index-CtxruO1L.js +3121 -0
- package/dist/assets/{index-62tyjEQB.css → index-DKXdXfka.css} +1 -1
- package/dist/index.html +2 -2
- package/dist-web/app.css +1 -1
- package/dist-web/app.js +467 -451
- package/package.json +17 -6
- package/plugins/AGENTS.md +60 -0
- package/plugins/CLAUDE.md +1 -60
- package/plugins/session-manager-dev/.claude-plugin/plugin.json +1 -1
- package/plugins/session-manager-dev/skills/builder/SKILL.md +12 -4
- package/plugins/session-manager-dev/skills/builder/scripts/publish-preflight.cjs +255 -0
- package/plugins/session-manager-dev/skills/develop/SKILL.md +36 -22
- package/plugins/session-manager-dev/skills/maintaining-md-network/SKILL.md +5 -4
- package/plugins/session-manager-dev/skills/maintaining-md-network/audit.md +4 -2
- package/plugins/session-manager-dev/skills/maintaining-md-network/drift-check.md +16 -12
- package/plugins/session-manager-dev/skills/maintaining-md-network/migrate-to-agents-md.md +15 -0
- package/plugins/session-manager-dev/skills/maintaining-md-network/placement.md +50 -2
- package/plugins/session-manager-dev/skills/maintaining-md-network/plan.md +10 -10
- package/plugins/session-manager-dev/skills/maintaining-md-network/scripts/drift-check.cjs +117 -112
- package/plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdDuplicates.cjs +182 -0
- package/plugins/session-manager-dev/skills/maintaining-md-network/scripts/mdLinks.cjs +340 -0
- package/scripts/README.md +2 -0
- package/scripts/hooks/guard-tool-risk.cjs +91 -0
- package/scripts/hooks/lib/guard-tool-risk-policy.cjs +217 -0
- package/scripts/ops-sweep.cjs +53 -23
- package/scripts/scheduler-mcp-server.cjs +360 -35
- package/src/main/build-info.json +4 -4
- package/src/main/chatRunner.cjs +2 -2
- package/src/main/config.cjs +112 -8
- package/src/main/docEdit.cjs +6 -1
- package/src/main/exchanges.cjs +70 -5
- package/src/main/files.cjs +7 -27
- package/src/main/health.cjs +48 -15
- package/src/main/historyAggregator.cjs +172 -63
- package/src/main/historyDashboard.cjs +87 -17
- package/src/main/index.cjs +78 -4
- package/src/main/ipcSchemas.cjs +233 -20
- package/src/main/lib/AGENTS.md +54 -0
- package/src/main/lib/CLAUDE.md +1 -54
- package/src/main/lib/agent-cli/adapters/codex.cjs +5 -2
- package/src/main/lib/agent-cli/agentCliIpc.cjs +18 -2
- package/src/main/lib/agent-cli/agentCliSessionStore.cjs +1 -0
- package/src/main/lib/agent-cli/agentCliTurn.cjs +20 -2
- package/src/main/lib/agent-cli/geminiContextDefaults.cjs +94 -0
- package/src/main/lib/claude/effectiveModelInfo.cjs +92 -22
- package/src/main/lib/claude/jobSandboxPolicy.cjs +202 -0
- package/src/main/lib/claude/modelPricing.cjs +148 -0
- package/src/main/lib/claude/personaToolScope.cjs +12 -6
- package/src/main/lib/claude/rateLimitDetect.cjs +13 -2
- package/src/main/lib/claude/runClaudeP.cjs +6 -1
- package/src/main/lib/claude/toolRiskIpc.cjs +177 -0
- package/src/main/lib/claude/toolRiskMarks.cjs +373 -0
- package/src/main/lib/demo-video-kit/seedKit.cjs +27 -6
- package/src/main/lib/ephemeralCwd.cjs +37 -2
- package/src/main/lib/epic/delegationReadiness.cjs +42 -9
- package/src/main/lib/epic/epicAdminRoutes.cjs +121 -0
- package/src/main/lib/epic/epicJobReceipt.cjs +87 -0
- package/src/main/lib/epic/epicOutsideRead.cjs +423 -0
- package/src/main/lib/epic/epicOutsideState.cjs +233 -0
- package/src/main/lib/epic/epicSettle.cjs +1 -1
- package/src/main/lib/epic/epicStateWatcher.cjs +157 -0
- package/src/main/lib/epic/epicSweepValidator.cjs +7 -2
- package/src/main/lib/epic/epicValidationHook.cjs +24 -4
- package/src/main/lib/epic/externalRef.cjs +37 -0
- package/src/main/lib/epic/externalRefIndex.cjs +136 -0
- package/src/main/lib/epic/guardSpawnSettings.cjs +57 -1
- package/src/main/lib/epic/restorableSessions.cjs +151 -17
- package/src/main/lib/epic/transcriptUsage.cjs +197 -0
- package/src/main/lib/externalContract.cjs +137 -0
- package/src/main/lib/feedback-remote/crossProjectFeedback.cjs +176 -5
- package/src/main/lib/feedback-remote/crossProjectReply.cjs +186 -9
- package/src/main/lib/feedback-remote/customerFeedbackAdminRoutes.cjs +16 -6
- package/src/main/lib/feedback-remote/customerFeedbackClient.cjs +50 -8
- package/src/main/lib/feedback-remote/customerFeedbackInbox.cjs +218 -4
- package/src/main/lib/feedback-remote/customerFeedbackIpc.cjs +19 -3
- package/src/main/lib/feedback-remote/feedbackImages.cjs +171 -0
- package/src/main/lib/feedback-remote/localAdminHttp.cjs +11 -10
- package/src/main/lib/feedback-remote/macroAdminRoutes.cjs +167 -21
- package/src/main/lib/feedback-remote/needsYouAdminRoutes.cjs +133 -0
- package/src/main/lib/feedback-remote/needsYouIpc.cjs +50 -0
- package/src/main/lib/feedback-remote/needsYouStore.cjs +285 -0
- package/src/main/lib/feedback-remote/projectHomeAdminRoutes.cjs +98 -1
- package/src/main/lib/feedback-remote/remoteIpcDispatch.cjs +5 -0
- package/src/main/lib/git/branchSweep.cjs +209 -4
- package/src/main/lib/git/gitWorktree.cjs +41 -5
- package/src/main/lib/guardShims.cjs +1 -1
- package/src/main/lib/historyRollup.cjs +58 -5
- package/src/main/lib/insideHome.cjs +27 -1
- package/src/main/lib/mcpToolCatalog.cjs +232 -25
- package/src/main/lib/mdDocCensus.cjs +59 -78
- package/src/main/lib/mdDocRefs.cjs +71 -93
- package/src/main/lib/mdNetworkScan.cjs +245 -250
- package/src/main/lib/mdReviewRecord.cjs +22 -9
- package/src/main/lib/memoryMdNetwork.cjs +5 -12
- package/src/main/lib/opsOwnership.cjs +18 -2
- package/src/main/lib/opsRetentionSettings.cjs +28 -4
- package/src/main/lib/persona-macro/agentAdminRoutes.cjs +121 -0
- package/src/main/lib/persona-macro/macroJobDispatch.cjs +90 -20
- package/src/main/lib/persona-macro/macroJobIpc.cjs +144 -46
- package/src/main/lib/persona-macro/macroJobStatus.cjs +87 -9
- package/src/main/lib/persona-macro/macroLibrary.cjs +95 -5
- package/src/main/lib/persona-macro/personaImportHealth.cjs +78 -17
- package/src/main/lib/persona-macro/projectVideoToolCheck.cjs +96 -0
- package/src/main/lib/persona-macro/shippedPersonaSeeds.cjs +11 -0
- package/src/main/lib/prd/depSlugResolve.cjs +104 -4
- package/src/main/lib/prd/prdAdminRoutes.cjs +35 -2
- package/src/main/lib/prd/prdCreate.cjs +351 -23
- package/src/main/lib/prd/prdDisposition.cjs +11 -2
- package/src/main/lib/prd/prdFrontmatter.cjs +96 -3
- package/src/main/lib/process/loginItem.cjs +171 -0
- package/src/main/lib/process/loginItemPlan.cjs +193 -0
- package/src/main/lib/project-pages/playerBridge.cjs +4 -0
- package/src/main/lib/project-pages/videoReview.cjs +268 -0
- package/src/main/lib/project-pages/videoReviewIpc.cjs +229 -0
- package/src/main/lib/promptSessionSchema.cjs +4 -0
- package/src/main/lib/remoteTranscriptTail.cjs +32 -1
- package/src/main/lib/scheduler/cpuSample.cjs +131 -4
- package/src/main/lib/scheduler/fixPlanSlug.cjs +4 -3
- package/src/main/lib/scheduler/loadGate.cjs +24 -6
- package/src/main/lib/scheduler/planRecordHarvest.cjs +108 -0
- package/src/main/lib/scheduler/planRecordRetention.cjs +144 -0
- package/src/main/lib/scheduler/queueStore.cjs +7 -0
- package/src/main/lib/scheduler/rcaReport.cjs +21 -1
- package/src/main/lib/scheduler/reaperHelpers.cjs +35 -5
- package/src/main/lib/scheduler/runLogRetention.cjs +104 -3
- package/src/main/lib/scheduler/scheduleJobSchema.cjs +3 -0
- package/src/main/lib/scheduler/schedulerBatch.cjs +25 -48
- package/src/main/lib/scheduler/schedulerPaths.cjs +12 -0
- package/src/main/lib/scheduler/sessionSlots.cjs +176 -21
- package/src/main/lib/scheduler/validationQuality.cjs +282 -0
- package/src/main/lib/scheduler/validatorVerdictSchema.cjs +254 -0
- package/src/main/lib/tasks/taskAdminRoutes.cjs +641 -0
- package/src/main/lib/tasks/taskIpc.cjs +80 -0
- package/src/main/lib/tasks/taskLedger.cjs +502 -0
- package/src/main/lib/tasks/taskRun.cjs +313 -0
- package/src/main/lib/tasks/taskSwitch.cjs +120 -0
- package/src/main/lib/telemetry/auditEvents.cjs +176 -0
- package/src/main/lib/telemetry/eventsAdminRoutes.cjs +45 -0
- package/src/main/loginItemIpc.cjs +35 -0
- package/src/main/loopEfficiencyHandlers.cjs +11 -2
- package/src/main/projectPages.cjs +6 -15
- package/src/main/promptSessionEvents.cjs +9 -1
- package/src/main/queueOps.cjs +260 -3
- package/src/main/runVerify.cjs +53 -25
- package/src/main/scheduler/AGENTS.md +26 -0
- package/src/main/scheduler/CLAUDE.md +1 -26
- package/src/main/scheduler/budget.cjs +239 -9
- package/src/main/scheduler/cancelDependents.cjs +154 -0
- package/src/main/scheduler/prdParser.cjs +4 -1
- package/src/main/scheduler/starvation.cjs +20 -18
- package/src/main/scheduler.cjs +774 -64
- package/src/main/supervisor.cjs +5 -1
- package/src/main/templates/PRD_AUTHORING.md +7 -1
- package/src/main/transcripts.cjs +62 -7
- package/src/main/validationQualityHandlers.cjs +199 -0
- package/src/main/webRemote.cjs +132 -20
- package/src/preload/api.d.ts +434 -28
- package/src/preload/apiSurface.cjs +7 -0
- package/src/preload/apiSurface.json +485 -81
- package/src/preload/index.cjs +38 -5
- package/src/seed/agents/validator.md +19 -8
- package/dist/assets/AgentLibrary-C1d_D7uQ.js +0 -2
- package/dist/assets/AppSettings-CgDhqrsU.js +0 -2
- package/dist/assets/Button-Bk23rEnZ.js +0 -1
- package/dist/assets/DocumentEditorPane-BqY5Nq6W.js +0 -1
- package/dist/assets/Extensions-CXem-uwI.js +0 -2
- package/dist/assets/History-BwO3coo4.js +0 -2
- package/dist/assets/Library-BDV69fSe.js +0 -32
- package/dist/assets/ListDetail-CD-Fm6mh.js +0 -1
- package/dist/assets/ProjectClaudeConfig-NjdKdwe8.js +0 -25
- package/dist/assets/ProvenanceBadge-Bu8NmI_R.js +0 -1
- package/dist/assets/Scheduler-CkhTvG7C.js +0 -16
- package/dist/assets/Settings-COT34hBF.js +0 -2
- package/dist/assets/Skills-BgexoUkQ.js +0 -29
- package/dist/assets/SourceChip-BXjcdeb1.js +0 -1
- package/dist/assets/SystemPrompt-CeeH3EeW.js +0 -1
- package/dist/assets/Toggle-CENi19iK.js +0 -1
- package/dist/assets/ViewTabs-DDBb0x96.js +0 -1
- package/dist/assets/claude-settings-schema-COigsajx.js +0 -3
- package/dist/assets/index-CgCxpunJ.js +0 -3116
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* videoReview.cjs — the one writer of a video entry's human review step:
|
|
5
|
+
* session-manager-operations/project-pages/videos/<id>/review/ (namespace
|
|
6
|
+
* 'project-pages', owner 'project-home' in lib/opsOwnership.cjs).
|
|
7
|
+
*
|
|
8
|
+
* dispatch.json — written by the app when a review-tool run is queued
|
|
9
|
+
* material.json — + image-1.<ext> .. image-N.<ext>, written by the
|
|
10
|
+
* agent-facing review write tool (writeMaterial)
|
|
11
|
+
* decision.json — written ONLY by the human decision IPC (writeDecision)
|
|
12
|
+
*
|
|
13
|
+
* reviewStatus() derives the state from those three records, purely.
|
|
14
|
+
* Plain Node: no Electron import. Paths come from opsPath; every write goes
|
|
15
|
+
* through assertOpsWrite + util/atomicFs.cjs (tmp+rename).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const fs = require('node:fs');
|
|
19
|
+
const fsp = require('node:fs/promises');
|
|
20
|
+
const path = require('node:path');
|
|
21
|
+
const { opsPath, assertOpsWrite } = require('../opsOwnership.cjs');
|
|
22
|
+
const { writeJsonAtomic, writeTextAtomic, readJsonOr } = require('../util/atomicFs.cjs');
|
|
23
|
+
const { ENTRY_ID_RE } = require('../../ipcSchemas.cjs');
|
|
24
|
+
|
|
25
|
+
const WRITER = 'project-home';
|
|
26
|
+
const REVIEW_MATERIAL_REL = 'session-manager-operations/project-pages/videos/{id}/review/material.json';
|
|
27
|
+
const REVIEW_TEXT_MAX = 32000;
|
|
28
|
+
const REVIEW_IMAGES_MAX = 6;
|
|
29
|
+
const REVIEW_IMAGE_BYTES_MAX = 2 * 1024 * 1024;
|
|
30
|
+
const NOTE_MAX = 2000;
|
|
31
|
+
const AT_MAX = 40;
|
|
32
|
+
const STAGE_MAX = 40;
|
|
33
|
+
const DECISIONS = Object.freeze(['approve', 'changes']);
|
|
34
|
+
const IMAGE_NAME_RE = /^image-\d+\.(png|jpg|webp)$/;
|
|
35
|
+
|
|
36
|
+
/** Strictly increasing ISO stamps, so records written in the same ms still order. */
|
|
37
|
+
let lastStampMs = 0;
|
|
38
|
+
function nowIso() {
|
|
39
|
+
let ms = Date.now();
|
|
40
|
+
if (ms <= lastStampMs) ms = lastStampMs + 1;
|
|
41
|
+
lastStampMs = ms;
|
|
42
|
+
return new Date(ms).toISOString();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function reviewDir(cwd, entryId) {
|
|
46
|
+
return opsPath(cwd, 'project-pages', 'videos', entryId, 'review');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function validEntryId(entryId) {
|
|
50
|
+
return typeof entryId === 'string' && ENTRY_ID_RE.test(entryId);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function writeJson(abs, obj) {
|
|
54
|
+
assertOpsWrite(abs, WRITER);
|
|
55
|
+
await writeJsonAtomic(abs, obj);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** -> 'png' | 'jpg' | 'webp' | null, by magic bytes only. */
|
|
59
|
+
function imageExt(buf) {
|
|
60
|
+
if (!Buffer.isBuffer(buf)) return null;
|
|
61
|
+
if (buf.length >= 8 && buf.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return 'png';
|
|
62
|
+
if (buf.length >= 3 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return 'jpg';
|
|
63
|
+
if (buf.length >= 12 && buf.toString('latin1', 0, 4) === 'RIFF' && buf.toString('latin1', 8, 12) === 'WEBP') return 'webp';
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function readRecords(dir) {
|
|
68
|
+
const [dispatch, material, decision] = await Promise.all([
|
|
69
|
+
readJsonOr(path.join(dir, 'dispatch.json'), null),
|
|
70
|
+
readJsonOr(path.join(dir, 'material.json'), null),
|
|
71
|
+
readJsonOr(path.join(dir, 'decision.json'), null),
|
|
72
|
+
]);
|
|
73
|
+
return { dispatch, material, decision };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const ms = (iso) => {
|
|
77
|
+
const t = Date.parse(iso);
|
|
78
|
+
return Number.isFinite(t) ? t : -Infinity;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* reviewStatus({ dispatch, material, decision }) -> 'building' | 'waiting' |
|
|
83
|
+
* 'approved' | 'changes-requested' | null. Pure. The newest record wins; ties
|
|
84
|
+
* go to the later lifecycle step. A 'final' dispatch never outranks a decision.
|
|
85
|
+
*/
|
|
86
|
+
function reviewStatus(records) {
|
|
87
|
+
const { dispatch = null, material = null, decision = null } = records || {};
|
|
88
|
+
const candidates = [];
|
|
89
|
+
if (dispatch && !(dispatch.stage === 'final' && decision)) {
|
|
90
|
+
candidates.push({ t: ms(dispatch.dispatchedAt), rank: 0, status: 'building' });
|
|
91
|
+
}
|
|
92
|
+
if (material) candidates.push({ t: ms(material.savedAt), rank: 1, status: 'waiting' });
|
|
93
|
+
if (decision) {
|
|
94
|
+
candidates.push({
|
|
95
|
+
t: ms(decision.decidedAt),
|
|
96
|
+
rank: 2,
|
|
97
|
+
status: decision.decision === 'approve' ? 'approved' : 'changes-requested',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (!candidates.length) return null;
|
|
101
|
+
candidates.sort((a, b) => (b.t - a.t) || (b.rank - a.rank));
|
|
102
|
+
return candidates[0].status;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* recordDispatch(cwd, entryId, { macroId, epicId, slug, stage, inputValues, extraInstructions })
|
|
107
|
+
* -> { ok: true, path, dispatch } | { ok: false, error }
|
|
108
|
+
*/
|
|
109
|
+
async function recordDispatch(cwd, entryId, input = {}) {
|
|
110
|
+
if (!validEntryId(entryId)) return { ok: false, error: 'invalid entryId' };
|
|
111
|
+
const { macroId, epicId = null, slug = '', stage, inputValues = {}, extraInstructions = '' } = input || {};
|
|
112
|
+
if (typeof macroId !== 'string' || !macroId) return { ok: false, error: 'macroId required' };
|
|
113
|
+
if (epicId !== null && typeof epicId !== 'string') return { ok: false, error: 'epicId must be a string or null' };
|
|
114
|
+
if (typeof slug !== 'string') return { ok: false, error: 'slug must be a string' };
|
|
115
|
+
if (typeof stage !== 'string' || !stage || stage.length > STAGE_MAX) return { ok: false, error: 'stage required (1-40 chars)' };
|
|
116
|
+
if (!inputValues || typeof inputValues !== 'object' || Array.isArray(inputValues)) {
|
|
117
|
+
return { ok: false, error: 'inputValues must be an object' };
|
|
118
|
+
}
|
|
119
|
+
if (typeof extraInstructions !== 'string') return { ok: false, error: 'extraInstructions must be a string' };
|
|
120
|
+
const dispatch = { entryId, macroId, epicId, slug, stage, inputValues, extraInstructions, dispatchedAt: nowIso() };
|
|
121
|
+
const abs = path.join(reviewDir(cwd, entryId), 'dispatch.json');
|
|
122
|
+
await writeJson(abs, dispatch);
|
|
123
|
+
return { ok: true, path: abs, dispatch };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* writeMaterial(cwd, entryId, { text, images: [{ bytes: Buffer }] })
|
|
128
|
+
* -> { ok: true, path, material } | { ok: false, error }
|
|
129
|
+
* Replaces the previous material and images. Refused without dispatch.json.
|
|
130
|
+
*/
|
|
131
|
+
async function writeMaterial(cwd, entryId, input = {}) {
|
|
132
|
+
if (!validEntryId(entryId)) return { ok: false, error: 'invalid entryId' };
|
|
133
|
+
const { text, images } = input || {};
|
|
134
|
+
if (typeof text !== 'string' || text.length < 1 || text.length > REVIEW_TEXT_MAX) {
|
|
135
|
+
return { ok: false, error: `text must be 1-${REVIEW_TEXT_MAX} chars` };
|
|
136
|
+
}
|
|
137
|
+
if (!Array.isArray(images) || images.length < 1 || images.length > REVIEW_IMAGES_MAX) {
|
|
138
|
+
return { ok: false, error: `images must be 1-${REVIEW_IMAGES_MAX} items` };
|
|
139
|
+
}
|
|
140
|
+
const exts = [];
|
|
141
|
+
for (let i = 0; i < images.length; i++) {
|
|
142
|
+
const bytes = images[i] && images[i].bytes;
|
|
143
|
+
if (!Buffer.isBuffer(bytes)) return { ok: false, error: `image ${i + 1}: bytes must be a Buffer` };
|
|
144
|
+
if (bytes.length > REVIEW_IMAGE_BYTES_MAX) return { ok: false, error: `image ${i + 1}: larger than 2 MiB` };
|
|
145
|
+
const ext = imageExt(bytes);
|
|
146
|
+
if (!ext) return { ok: false, error: `image ${i + 1}: not a PNG, JPEG or WebP` };
|
|
147
|
+
exts.push(ext);
|
|
148
|
+
}
|
|
149
|
+
const dir = reviewDir(cwd, entryId);
|
|
150
|
+
const dispatch = await readJsonOr(path.join(dir, 'dispatch.json'), null);
|
|
151
|
+
if (!dispatch) return { ok: false, error: 'no dispatch for this entry' };
|
|
152
|
+
|
|
153
|
+
const names = exts.map((ext, i) => `image-${i + 1}.${ext}`);
|
|
154
|
+
for (let i = 0; i < names.length; i++) {
|
|
155
|
+
const abs = path.join(dir, names[i]);
|
|
156
|
+
assertOpsWrite(abs, WRITER);
|
|
157
|
+
// atomicFs.writeTextAtomic writes a Buffer verbatim (encoding is ignored for Buffers).
|
|
158
|
+
await writeTextAtomic(abs, images[i].bytes);
|
|
159
|
+
}
|
|
160
|
+
let existing = [];
|
|
161
|
+
try { existing = await fsp.readdir(dir); } catch { /* dir was just created */ }
|
|
162
|
+
for (const name of existing) {
|
|
163
|
+
if (IMAGE_NAME_RE.test(name) && !names.includes(name)) {
|
|
164
|
+
const abs = path.join(dir, name);
|
|
165
|
+
assertOpsWrite(abs, WRITER);
|
|
166
|
+
await fsp.unlink(abs);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const material = { entryId, text, images: names.map((name) => ({ name })), savedAt: nowIso() };
|
|
170
|
+
const abs = path.join(dir, 'material.json');
|
|
171
|
+
await writeJson(abs, material);
|
|
172
|
+
return { ok: true, path: abs, material };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* writeDecision(cwd, entryId, { decision, note, at }) — the human decision.
|
|
177
|
+
* -> { ok: true, path, decision } | { ok: false, error }
|
|
178
|
+
*/
|
|
179
|
+
async function writeDecision(cwd, entryId, input = {}) {
|
|
180
|
+
if (!validEntryId(entryId)) return { ok: false, error: 'invalid entryId' };
|
|
181
|
+
const { decision, note, at } = input || {};
|
|
182
|
+
if (!DECISIONS.includes(decision)) return { ok: false, error: "decision must be 'approve' or 'changes'" };
|
|
183
|
+
const record = { entryId, decision };
|
|
184
|
+
if (decision === 'changes') {
|
|
185
|
+
const n = typeof note === 'string' ? note.replace(/\s*[\r\n]+\s*/g, ' ').trim() : '';
|
|
186
|
+
if (n.length < 1 || n.length > NOTE_MAX) return { ok: false, error: `note must be 1-${NOTE_MAX} chars` };
|
|
187
|
+
record.note = n;
|
|
188
|
+
} else {
|
|
189
|
+
if (typeof at !== 'string' || at.length < 1 || at.length > AT_MAX || /[\r\n]/.test(at)) {
|
|
190
|
+
return { ok: false, error: `at must be a 1-${AT_MAX} char single line` };
|
|
191
|
+
}
|
|
192
|
+
record.at = at;
|
|
193
|
+
}
|
|
194
|
+
const dir = reviewDir(cwd, entryId);
|
|
195
|
+
const { material, decision: prior } = await readRecords(dir);
|
|
196
|
+
if (!material) return { ok: false, error: 'no material to decide on' };
|
|
197
|
+
if (prior && ms(prior.decidedAt) >= ms(material.savedAt)) return { ok: false, error: 'already decided' };
|
|
198
|
+
record.decidedAt = nowIso();
|
|
199
|
+
const abs = path.join(dir, 'decision.json');
|
|
200
|
+
await writeJson(abs, record);
|
|
201
|
+
return { ok: true, path: abs, decision: record };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** readReview(cwd, entryId) -> { entryId, dispatch, material, decision, status } | null */
|
|
205
|
+
async function readReview(cwd, entryId) {
|
|
206
|
+
if (!validEntryId(entryId)) return null;
|
|
207
|
+
const dir = reviewDir(cwd, entryId);
|
|
208
|
+
if (!fs.existsSync(dir)) return null;
|
|
209
|
+
const records = await readRecords(dir);
|
|
210
|
+
let material = records.material;
|
|
211
|
+
if (material) {
|
|
212
|
+
const images = Array.isArray(material.images) ? material.images : [];
|
|
213
|
+
material = {
|
|
214
|
+
...material,
|
|
215
|
+
images: images
|
|
216
|
+
.filter((img) => img && typeof img.name === 'string' && IMAGE_NAME_RE.test(img.name))
|
|
217
|
+
.map((img) => ({ name: img.name, path: path.join(dir, img.name) })),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
entryId,
|
|
222
|
+
dispatch: records.dispatch,
|
|
223
|
+
material,
|
|
224
|
+
decision: records.decision,
|
|
225
|
+
status: reviewStatus(records),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function newestStamp(r) {
|
|
230
|
+
return Math.max(
|
|
231
|
+
r.dispatch ? ms(r.dispatch.dispatchedAt) : -Infinity,
|
|
232
|
+
r.material ? ms(r.material.savedAt) : -Infinity,
|
|
233
|
+
r.decision ? ms(r.decision.decidedAt) : -Infinity,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** listReviews(cwd) -> every entry with a review folder, newest record first. */
|
|
238
|
+
async function listReviews(cwd) {
|
|
239
|
+
const videosDir = opsPath(cwd, 'project-pages', 'videos');
|
|
240
|
+
let names;
|
|
241
|
+
try {
|
|
242
|
+
names = await fsp.readdir(videosDir);
|
|
243
|
+
} catch (e) {
|
|
244
|
+
if (e && e.code === 'ENOENT') return [];
|
|
245
|
+
throw e;
|
|
246
|
+
}
|
|
247
|
+
const out = [];
|
|
248
|
+
for (const name of names) {
|
|
249
|
+
if (!validEntryId(name)) continue;
|
|
250
|
+
const r = await readReview(cwd, name);
|
|
251
|
+
if (r) out.push(r);
|
|
252
|
+
}
|
|
253
|
+
out.sort((a, b) => newestStamp(b) - newestStamp(a));
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
module.exports = {
|
|
258
|
+
REVIEW_MATERIAL_REL,
|
|
259
|
+
REVIEW_TEXT_MAX,
|
|
260
|
+
REVIEW_IMAGES_MAX,
|
|
261
|
+
REVIEW_IMAGE_BYTES_MAX,
|
|
262
|
+
recordDispatch,
|
|
263
|
+
writeMaterial,
|
|
264
|
+
writeDecision,
|
|
265
|
+
readReview,
|
|
266
|
+
listReviews,
|
|
267
|
+
reviewStatus,
|
|
268
|
+
};
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* videoReviewIpc.cjs — IPC handlers `project-videos:review-list`,
|
|
5
|
+
* `project-videos:review-decide` and `project-videos:review-retry`: the human
|
|
6
|
+
* review step of a project Video
|
|
7
|
+
* tool entry. Renderer IPC only — no admin route or MCP tool reaches a
|
|
8
|
+
* decision, and nothing here mints an Epic.
|
|
9
|
+
*
|
|
10
|
+
* approve — starts the tool's final run in the Epic that built the entry
|
|
11
|
+
* (runToolJob, review { decision: 'approved', at }), then writes
|
|
12
|
+
* decision.json with that same `at`.
|
|
13
|
+
* changes — re-runs the review stage with the owner's note, then writes
|
|
14
|
+
* decision.json with the note.
|
|
15
|
+
*
|
|
16
|
+
* retry — an approved entry whose final run failed: re-dispatches the
|
|
17
|
+
* final stage with the recorded approval `at` and inputs; writes
|
|
18
|
+
* no decision; archives the old failed slug (best effort).
|
|
19
|
+
*
|
|
20
|
+
* A decision is written only after an ok dispatch. One decide/retry per entry
|
|
21
|
+
* at a time (module-level in-flight set), so a double click dispatches one run.
|
|
22
|
+
* review-list adds each entry's `job` (jobStatusesBySlug for dispatch.slug, or
|
|
23
|
+
* null) and `canRetry` (retryable()).
|
|
24
|
+
*
|
|
25
|
+
* deps: the same deps registerMacroJobIpc gets ({ schemas, validatePath,
|
|
26
|
+
* readActiveIndex, createPrd, loadJobs, archivePrd, ... }), plus
|
|
27
|
+
* optional readHistoryTail, and videoReview / runToolJob /
|
|
28
|
+
* jobStatusesBySlug / now overrides for tests.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const NOT_RETRYABLE_ERROR = 'only an approved entry whose final run failed can be retried';
|
|
32
|
+
const IN_FLIGHT_ERROR = 'a decision or retry for this entry is already in progress';
|
|
33
|
+
const INACTIVE_ERROR = 'the session that built this entry is no longer active — press the tool again';
|
|
34
|
+
|
|
35
|
+
/** In-flight decides and retries (they share one slot per entry), keyed `${cwd}\0${entryId}`. */
|
|
36
|
+
const inFlight = new Set();
|
|
37
|
+
|
|
38
|
+
function defaultVideoReview() {
|
|
39
|
+
return require('./videoReview.cjs');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function defaultJobStatusesBySlug() {
|
|
43
|
+
return require('../persona-macro/macroJobStatus.cjs').jobStatusesBySlug;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* retryable(review, job) -> boolean. Pure. True only for an approved entry
|
|
48
|
+
* (decision 'approve' with a non-empty at) whose recorded final-stage run's
|
|
49
|
+
* job ended failed. No job row counts as not failed.
|
|
50
|
+
*/
|
|
51
|
+
function retryable(review, job) {
|
|
52
|
+
if (!review || review.status !== 'approved') return false;
|
|
53
|
+
const d = review.decision;
|
|
54
|
+
if (!d || d.decision !== 'approve' || typeof d.at !== 'string' || !d.at.trim()) return false;
|
|
55
|
+
if (!review.dispatch || review.dispatch.stage !== 'final') return false;
|
|
56
|
+
return !!job && job.state === 'failed';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** { [slug]: job | null } for the given reviews' dispatch slugs. */
|
|
60
|
+
async function jobsFor(deps, cwd, reviews) {
|
|
61
|
+
const bySlug = deps.jobStatusesBySlug || defaultJobStatusesBySlug();
|
|
62
|
+
const slugs = reviews.map((r) => r && r.dispatch && r.dispatch.slug).filter((s) => typeof s === 'string' && s);
|
|
63
|
+
if (!slugs.length) return {};
|
|
64
|
+
const io = { loadJobs: deps.loadJobs };
|
|
65
|
+
if (typeof deps.readHistoryTail === 'function') io.readHistoryTail = deps.readHistoryTail;
|
|
66
|
+
try {
|
|
67
|
+
return (await bySlug({ cwd, slugs }, io)) || {};
|
|
68
|
+
} catch {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function jobOf(jobs, review) {
|
|
74
|
+
const slug = review && review.dispatch && review.dispatch.slug;
|
|
75
|
+
return (typeof slug === 'string' && slug && jobs[slug]) || null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function defaultRunToolJob() {
|
|
79
|
+
return require('../persona-macro/macroJobIpc.cjs').runToolJob;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** '2026-11-22 13:05 PST' — the machine's local zone, 24-hour clock. */
|
|
83
|
+
function formatLocalAt(date = new Date()) {
|
|
84
|
+
const parts = new Intl.DateTimeFormat('en-US', {
|
|
85
|
+
year: 'numeric', month: '2-digit', day: '2-digit',
|
|
86
|
+
hour: '2-digit', minute: '2-digit', hourCycle: 'h23',
|
|
87
|
+
timeZoneName: 'short',
|
|
88
|
+
}).formatToParts(date);
|
|
89
|
+
const get = (type) => (parts.find((p) => p.type === type) || {}).value || '';
|
|
90
|
+
return `${get('year')}-${get('month')}-${get('day')} ${get('hour')}:${get('minute')} ${get('timeZoneName')}`.trim();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Drop every 'Replace entry:' line, then name this entry. */
|
|
94
|
+
function replaceEntryInstructions(extra, entryId) {
|
|
95
|
+
const kept = (typeof extra === 'string' ? extra : '')
|
|
96
|
+
.split(/\r?\n/)
|
|
97
|
+
.filter((l) => !/^\s*replace entry:/i.test(l))
|
|
98
|
+
.join('\n')
|
|
99
|
+
.trim();
|
|
100
|
+
return [kept, `Replace entry: ${entryId}`].filter(Boolean).join('\n');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function epicIsActive(readActiveIndex, cwd, epicId) {
|
|
104
|
+
if (typeof epicId !== 'string' || !epicId) return false;
|
|
105
|
+
const index = readActiveIndex(cwd);
|
|
106
|
+
const sessions = index && index.sessions ? index.sessions : {};
|
|
107
|
+
if (!Object.prototype.hasOwnProperty.call(sessions, epicId)) return false;
|
|
108
|
+
const entry = sessions[epicId];
|
|
109
|
+
return !!entry && entry.status === 'active';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** decide(deps, { cwd, entryId, decision, note }) — `cwd` already validated. */
|
|
113
|
+
async function decide(deps, { cwd, entryId, decision, note }) {
|
|
114
|
+
const videoReview = deps.videoReview || defaultVideoReview();
|
|
115
|
+
const runToolJob = deps.runToolJob || defaultRunToolJob();
|
|
116
|
+
const key = `${cwd}\0${entryId}`;
|
|
117
|
+
if (inFlight.has(key)) return { ok: false, error: IN_FLIGHT_ERROR };
|
|
118
|
+
inFlight.add(key);
|
|
119
|
+
try {
|
|
120
|
+
const trimmedNote = typeof note === 'string' ? note.trim() : '';
|
|
121
|
+
if (decision === 'changes' && !trimmedNote) return { ok: false, error: 'a note is required to ask for changes' };
|
|
122
|
+
|
|
123
|
+
const rev = await videoReview.readReview(cwd, entryId);
|
|
124
|
+
if (!rev || rev.status !== 'waiting') {
|
|
125
|
+
return { ok: false, error: `entry ${entryId} is not waiting for review (${rev ? rev.status : 'no review'})` };
|
|
126
|
+
}
|
|
127
|
+
const dispatch = rev.dispatch;
|
|
128
|
+
if (!dispatch || typeof dispatch.macroId !== 'string' || !dispatch.macroId) {
|
|
129
|
+
return { ok: false, error: `entry ${entryId} has no recorded tool run` };
|
|
130
|
+
}
|
|
131
|
+
if (!epicIsActive(deps.readActiveIndex, cwd, dispatch.epicId)) return { ok: false, error: INACTIVE_ERROR };
|
|
132
|
+
|
|
133
|
+
const at = decision === 'approve' ? formatLocalAt(deps.now ? deps.now() : new Date()) : null;
|
|
134
|
+
const review = decision === 'approve'
|
|
135
|
+
? { decision: 'approved', at }
|
|
136
|
+
: { decision: 'changes', note: trimmedNote };
|
|
137
|
+
|
|
138
|
+
const result = await runToolJob(deps, {
|
|
139
|
+
cwd,
|
|
140
|
+
macroId: dispatch.macroId,
|
|
141
|
+
epicId: dispatch.epicId,
|
|
142
|
+
inputValues: dispatch.inputValues && typeof dispatch.inputValues === 'object' ? dispatch.inputValues : {},
|
|
143
|
+
extraInstructions: replaceEntryInstructions(dispatch.extraInstructions, entryId),
|
|
144
|
+
review,
|
|
145
|
+
});
|
|
146
|
+
if (!result || result.ok !== true) {
|
|
147
|
+
return { ok: false, error: (result && result.error) || 'dispatch failed' };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const written = await videoReview.writeDecision(cwd, entryId, decision === 'approve'
|
|
151
|
+
? { decision: 'approve', at }
|
|
152
|
+
: { decision: 'changes', note: trimmedNote });
|
|
153
|
+
if (!written || written.ok !== true) {
|
|
154
|
+
return { ok: false, error: `run queued (${result.slug}) but the decision was not recorded: ${(written && written.error) || 'unknown error'}` };
|
|
155
|
+
}
|
|
156
|
+
return { ok: true, slug: result.slug, stage: result.stage, decision: written.decision };
|
|
157
|
+
} finally {
|
|
158
|
+
inFlight.delete(key);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** retry(deps, { cwd, entryId }) — `cwd` already validated. Writes no decision. */
|
|
163
|
+
async function retry(deps, { cwd, entryId }) {
|
|
164
|
+
const videoReview = deps.videoReview || defaultVideoReview();
|
|
165
|
+
const runToolJob = deps.runToolJob || defaultRunToolJob();
|
|
166
|
+
const key = `${cwd}\0${entryId}`;
|
|
167
|
+
if (inFlight.has(key)) return { ok: false, error: IN_FLIGHT_ERROR };
|
|
168
|
+
inFlight.add(key);
|
|
169
|
+
try {
|
|
170
|
+
const rev = await videoReview.readReview(cwd, entryId);
|
|
171
|
+
const job = rev ? jobOf(await jobsFor(deps, cwd, [rev]), rev) : null;
|
|
172
|
+
if (!retryable(rev, job)) return { ok: false, error: NOT_RETRYABLE_ERROR };
|
|
173
|
+
const dispatch = rev.dispatch;
|
|
174
|
+
if (typeof dispatch.macroId !== 'string' || !dispatch.macroId) return { ok: false, error: NOT_RETRYABLE_ERROR };
|
|
175
|
+
const oldSlug = dispatch.slug;
|
|
176
|
+
|
|
177
|
+
const result = await runToolJob(deps, {
|
|
178
|
+
cwd,
|
|
179
|
+
macroId: dispatch.macroId,
|
|
180
|
+
epicId: dispatch.epicId,
|
|
181
|
+
inputValues: dispatch.inputValues && typeof dispatch.inputValues === 'object' ? dispatch.inputValues : {},
|
|
182
|
+
extraInstructions: replaceEntryInstructions(dispatch.extraInstructions, entryId),
|
|
183
|
+
review: { decision: 'approved', at: rev.decision.at },
|
|
184
|
+
});
|
|
185
|
+
if (result && result.ok === true && typeof deps.archivePrd === 'function' && oldSlug && oldSlug !== result.slug) {
|
|
186
|
+
try { await deps.archivePrd(oldSlug, cwd); } catch { /* best effort */ }
|
|
187
|
+
}
|
|
188
|
+
return result;
|
|
189
|
+
} finally {
|
|
190
|
+
inFlight.delete(key);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function registerVideoReviewIpc(ipcMain, deps = {}) {
|
|
195
|
+
ipcMain.handle('project-videos:review-list', async (_event, rawPayload) => {
|
|
196
|
+
const payload = deps.schemas.projectVideosReviewList.parse(rawPayload);
|
|
197
|
+
const cwd = deps.validatePath(payload.cwd);
|
|
198
|
+
const videoReview = deps.videoReview || defaultVideoReview();
|
|
199
|
+
const reviews = await videoReview.listReviews(cwd);
|
|
200
|
+
const list = Array.isArray(reviews) ? reviews : [];
|
|
201
|
+
const jobs = await jobsFor(deps, cwd, list);
|
|
202
|
+
return list.map((r) => {
|
|
203
|
+
const job = jobOf(jobs, r);
|
|
204
|
+
return { ...r, job, canRetry: retryable(r, job) };
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
ipcMain.handle('project-videos:review-decide', async (_event, rawPayload) => {
|
|
209
|
+
const payload = deps.schemas.projectVideosReviewDecide.parse(rawPayload);
|
|
210
|
+
const cwd = deps.validatePath(payload.cwd);
|
|
211
|
+
return decide(deps, {
|
|
212
|
+
cwd,
|
|
213
|
+
entryId: payload.entryId,
|
|
214
|
+
decision: payload.decision,
|
|
215
|
+
note: payload.note,
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
ipcMain.handle('project-videos:review-retry', async (_event, rawPayload) => {
|
|
220
|
+
const payload = deps.schemas.projectVideosReviewRetry.parse(rawPayload);
|
|
221
|
+
const cwd = deps.validatePath(payload.cwd);
|
|
222
|
+
return retry(deps, { cwd, entryId: payload.entryId });
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
module.exports = {
|
|
227
|
+
registerVideoReviewIpc, formatLocalAt, replaceEntryInstructions, retryable,
|
|
228
|
+
INACTIVE_ERROR, NOT_RETRYABLE_ERROR,
|
|
229
|
+
};
|
|
@@ -45,6 +45,10 @@ const EpicSourceSchema = z.object({
|
|
|
45
45
|
fromEpicId: z.string().optional(),
|
|
46
46
|
// The macro that launched this Epic (new-epic-ui Epics only).
|
|
47
47
|
macroId: z.string().optional(),
|
|
48
|
+
// An outside app's own id for this Epic. Any string here: the strict rule
|
|
49
|
+
// lives in epic/externalRef.cjs at entry points, since a stricter stored
|
|
50
|
+
// shape would make dropInvalidSessions drop a whole row.
|
|
51
|
+
externalRef: z.string().optional(),
|
|
48
52
|
// Zod strips undeclared keys on the merge path; a stricter shape could make dropInvalidSessions (ipcSchemas.cjs) drop a whole row holding an older or malformed value.
|
|
49
53
|
proposal: z.unknown().optional(),
|
|
50
54
|
autoApprove: z.unknown().optional(),
|
|
@@ -28,10 +28,41 @@ function textOf(content) {
|
|
|
28
28
|
.join('\n');
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
const ATTACHED_LINE = /^Attached: .+$/;
|
|
32
|
+
|
|
33
|
+
function capHead(text, max) {
|
|
32
34
|
return text.length > max ? text.slice(0, max - 1) + '…' : text;
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Cap text at `max` chars. A trailing block of `Attached: <path>` lines (image
|
|
39
|
+
* sends append it) survives: only the body before it is cut, so the phone can
|
|
40
|
+
* still render attachment chips. Without such a block this is a plain head cap.
|
|
41
|
+
*/
|
|
42
|
+
function cap(text, max) {
|
|
43
|
+
if (text.length <= max) return text;
|
|
44
|
+
const lines = text.split('\n');
|
|
45
|
+
let i = lines.length;
|
|
46
|
+
while (i > 0 && ATTACHED_LINE.test(lines[i - 1])) i--;
|
|
47
|
+
if (i === lines.length) return capHead(text, max);
|
|
48
|
+
const block = lines.slice(i);
|
|
49
|
+
const blockText = block.join('\n');
|
|
50
|
+
if (blockText.length <= max - 2) {
|
|
51
|
+
const body = lines.slice(0, i).join('\n').trimEnd();
|
|
52
|
+
const budget = max - 2 - blockText.length;
|
|
53
|
+
if (!body || budget < 1) return blockText;
|
|
54
|
+
return capHead(body, budget) + '\n\n' + blockText;
|
|
55
|
+
}
|
|
56
|
+
// Block alone overflows: whole lines from its start, no body.
|
|
57
|
+
let out = '';
|
|
58
|
+
for (const line of block) {
|
|
59
|
+
const next = out ? out + '\n' + line : line;
|
|
60
|
+
if (next.length > max) break;
|
|
61
|
+
out = next;
|
|
62
|
+
}
|
|
63
|
+
return out || capHead(block[0], max);
|
|
64
|
+
}
|
|
65
|
+
|
|
35
66
|
function truncate(text) {
|
|
36
67
|
return cap(text, MAX_TEXT_CHARS);
|
|
37
68
|
}
|