@cyber-dash-tech/revela 0.11.0 → 0.13.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.
Files changed (37) hide show
  1. package/README.md +35 -29
  2. package/README.zh-CN.md +35 -29
  3. package/lib/commands/brief.ts +63 -0
  4. package/lib/commands/designs.ts +2 -2
  5. package/lib/commands/domains.ts +2 -2
  6. package/lib/commands/enable.ts +19 -19
  7. package/lib/commands/help.ts +7 -3
  8. package/lib/commands/init.ts +30 -19
  9. package/lib/commands/narrative.ts +160 -0
  10. package/lib/commands/review.ts +115 -1
  11. package/lib/decks-state.ts +46 -3
  12. package/lib/edit/prompt.ts +3 -0
  13. package/lib/inspection-context/compile.ts +159 -5
  14. package/lib/inspection-context/project.ts +20 -0
  15. package/lib/narrative-state/coverage.ts +100 -0
  16. package/lib/narrative-state/display.ts +219 -0
  17. package/lib/narrative-state/executive-brief.ts +246 -0
  18. package/lib/narrative-state/hash.ts +61 -0
  19. package/lib/narrative-state/map-html.ts +348 -0
  20. package/lib/narrative-state/map.ts +282 -0
  21. package/lib/narrative-state/normalize.ts +361 -0
  22. package/lib/narrative-state/project-compat.ts +14 -0
  23. package/lib/narrative-state/queries.ts +433 -0
  24. package/lib/narrative-state/readiness.ts +359 -0
  25. package/lib/narrative-state/render-plan.ts +250 -0
  26. package/lib/narrative-state/research-gaps.ts +191 -0
  27. package/lib/narrative-state/types.ts +172 -0
  28. package/lib/prompt-builder.ts +59 -26
  29. package/lib/workspace-state/evidence-status.ts +21 -1
  30. package/lib/workspace-state/graph.ts +174 -2
  31. package/lib/workspace-state/types.ts +13 -1
  32. package/package.json +1 -1
  33. package/plugin.ts +58 -2
  34. package/skill/NARRATIVE_SKILL.md +64 -0
  35. package/tools/decks.ts +265 -2
  36. package/tools/narrative-view.ts +84 -0
  37. package/tools/workspace-scan.ts +14 -1
@@ -0,0 +1,359 @@
1
+ import type { DecksState } from "../decks-state"
2
+ import { recordWorkspaceAction } from "../workspace-state/actions"
3
+ import { computeNarrativeHash, stableHash } from "./hash"
4
+ import { normalizeNarrativeState } from "./normalize"
5
+ import type {
6
+ NarrativeApproval,
7
+ NarrativeClaim,
8
+ NarrativeReadinessIssue,
9
+ NarrativeReadinessResult,
10
+ NarrativeReadinessStatus,
11
+ NarrativeStateV1,
12
+ } from "./types"
13
+
14
+ interface NarrativeApprovalState {
15
+ current: boolean
16
+ stale: boolean
17
+ latest?: NarrativeApproval
18
+ }
19
+
20
+ export interface ReviewNarrativeOptions {
21
+ now?: string
22
+ }
23
+
24
+ export interface ApproveNarrativeOptions {
25
+ now?: string
26
+ approvedBy?: "user" | "override"
27
+ scope?: "narrative" | "render_override"
28
+ note?: string
29
+ }
30
+
31
+ export interface ApproveNarrativeResult {
32
+ approved: boolean
33
+ skipped: boolean
34
+ reason?: string
35
+ narrativeHash: string
36
+ approval?: NarrativeApproval
37
+ readiness: NarrativeReadinessResult
38
+ }
39
+
40
+ export function reviewNarrativeState(state: DecksState, options: ReviewNarrativeOptions = {}): { state: DecksState; result: NarrativeReadinessResult } {
41
+ const next: DecksState = { ...state, narrative: normalizeNarrativeState(state) }
42
+ const result = computeNarrativeReadiness(next.narrative!, next, options)
43
+ next.narrative = { ...next.narrative!, status: narrativeStatusFromReadiness(result.status), updatedAt: options.now ?? next.narrative!.updatedAt }
44
+ return { state: next, result }
45
+ }
46
+
47
+ export function approveNarrativeState(state: DecksState, options: ApproveNarrativeOptions = {}): { state: DecksState; result: ApproveNarrativeResult } {
48
+ const now = options.now ?? new Date().toISOString()
49
+ const reviewed = reviewNarrativeState(state, { now })
50
+ const narrative = reviewed.state.narrative!
51
+ const scope = options.scope ?? "narrative"
52
+ const approvedBy = options.approvedBy ?? "user"
53
+ const override = approvedBy === "override" || scope === "render_override"
54
+ const blocking = reviewed.result.issues.filter((issue) => issue.severity === "blocker")
55
+ const incomplete = blocking.some((issue) => issue.type !== "approval_missing" && issue.type !== "approval_stale")
56
+
57
+ if (incomplete && !override) {
58
+ return {
59
+ state: reviewed.state,
60
+ result: {
61
+ approved: false,
62
+ skipped: true,
63
+ reason: "narrative has unresolved readiness blockers; use an explicit override to record a render override",
64
+ narrativeHash: reviewed.result.narrativeHash,
65
+ readiness: reviewed.result,
66
+ },
67
+ }
68
+ }
69
+
70
+ const approval: NarrativeApproval = {
71
+ id: `approval:${stableHash(`${reviewed.result.narrativeHash}:${now}:${scope}:${approvedBy}`)}`,
72
+ narrativeHash: reviewed.result.narrativeHash,
73
+ approvedAt: now,
74
+ approvedBy,
75
+ scope,
76
+ note: clean(options.note),
77
+ }
78
+ const approvals = dedupeApprovals([...(narrative.approvals ?? []), approval])
79
+ const updatedNarrative: NarrativeStateV1 = {
80
+ ...narrative,
81
+ approvals,
82
+ status: scope === "narrative" && approvedBy === "user" ? "approved" : narrative.status,
83
+ updatedAt: now,
84
+ }
85
+ const next: DecksState = { ...reviewed.state, narrative: updatedNarrative }
86
+ const readiness = computeNarrativeReadiness(updatedNarrative, next, { now })
87
+ next.narrative = { ...updatedNarrative, status: narrativeStatusFromReadiness(readiness.status) }
88
+ return {
89
+ state: next,
90
+ result: {
91
+ approved: true,
92
+ skipped: false,
93
+ narrativeHash: reviewed.result.narrativeHash,
94
+ approval,
95
+ readiness,
96
+ },
97
+ }
98
+ }
99
+
100
+ export function recordNarrativeReviewAction(state: DecksState, result: NarrativeReadinessResult): void {
101
+ recordWorkspaceAction(state, {
102
+ type: "review.performed",
103
+ actor: "revela-decks",
104
+ inputs: { kind: "narrative", narrativeId: state.narrative?.id },
105
+ outputs: {
106
+ kind: "narrative",
107
+ status: result.status,
108
+ narrativeHash: result.narrativeHash,
109
+ blockerCount: result.blockers.length,
110
+ warningCount: result.warnings.length,
111
+ issueCount: result.issues.length,
112
+ approvalCurrent: result.approval?.current ?? false,
113
+ approvalStale: result.approval?.stale ?? false,
114
+ },
115
+ status: "success",
116
+ summary: `Reviewed narrative readiness: ${result.status}.`,
117
+ nodeIds: state.narrative ? [state.narrative.id] : [],
118
+ })
119
+ }
120
+
121
+ export function recordNarrativeApprovalAction(state: DecksState, result: ApproveNarrativeResult): void {
122
+ recordWorkspaceAction(state, {
123
+ type: "narrative.approved",
124
+ actor: "revela-decks",
125
+ inputs: { narrativeId: state.narrative?.id, approvedBy: result.approval?.approvedBy, scope: result.approval?.scope },
126
+ outputs: {
127
+ approved: result.approved,
128
+ skipped: result.skipped,
129
+ reason: result.reason,
130
+ narrativeHash: result.narrativeHash,
131
+ approvalId: result.approval?.id,
132
+ },
133
+ status: result.skipped ? "skipped" : "success",
134
+ summary: result.skipped ? `Skipped narrative approval: ${result.reason ?? "not approved"}.` : `Recorded narrative ${result.approval?.scope ?? "narrative"} approval.`,
135
+ nodeIds: [state.narrative?.id, result.approval?.id].filter((item): item is string => Boolean(item)),
136
+ })
137
+ }
138
+
139
+ function computeNarrativeReadiness(narrative: NarrativeStateV1, state: DecksState, options: ReviewNarrativeOptions): NarrativeReadinessResult {
140
+ const now = options.now ?? new Date().toISOString()
141
+ const narrativeHash = computeNarrativeHash(narrative)
142
+ const issues: NarrativeReadinessIssue[] = []
143
+ const add = (issue: NarrativeReadinessIssue) => issues.push(issue)
144
+
145
+ if (!narrative.audience.primary) add(blocker("missing_audience", "Primary audience is missing.", "Define the primary audience before reviewing the narrative."))
146
+ if (!narrative.audience.beliefBefore || !narrative.audience.beliefAfter) add(blocker("missing_belief_shift", "Audience belief shift is incomplete.", "Add both beliefBefore and beliefAfter so the narrative has a persuasion target."))
147
+ if (!narrative.decision.action) add(blocker("missing_decision", "Decision or action is missing.", "Define the decision, approval, alignment, or action this narrative should drive."))
148
+ if (isDecisionOriented(narrative) && !narrative.thesis?.statement) add(blocker("missing_thesis", "Decision-oriented narrative has no thesis.", "Add a compact thesis that carries the recommendation and evidence boundary."))
149
+
150
+ const centralClaims = narrative.claims.filter((claim) => claim.importance === "central")
151
+ if (isDecisionOriented(narrative) && centralClaims.length === 0) add(blocker("claim_chain_gap", "Decision-oriented narrative has no central claims.", "Add one to three central claims that the narrative must prove."))
152
+ if (centralClaims.length > 4) add(warning("claim_chain_gap", "Narrative has many central claims.", "Tighten the claim chain to the few claims the audience must believe."))
153
+ if (needsClaimRelations(narrative)) add(warning("claim_chain_gap", "Narrative claim progression is not explicit.", "Add claimRelations so the narrative flow shows how claims lead to, support, constrain, or answer each other."))
154
+ for (const relationIssue of claimRelationIssues(narrative)) add(relationIssue)
155
+
156
+ for (const claim of narrative.claims) {
157
+ if (!claim.evidenceRequired) continue
158
+ if (claim.evidenceStatus === "missing" && claim.importance === "central") add(claimIssue("missing_evidence", "blocker", claim, "Central claim lacks evidence.", "Bind source-backed evidence or revise the claim scope before approval."))
159
+ else if (claim.evidenceStatus === "missing") add(claimIssue("missing_evidence", "warning", claim, "Supporting claim lacks evidence.", "Bind evidence or mark the claim as not evidence-required if it is purely framing."))
160
+ else if (claim.evidenceStatus === "weak" || claim.evidenceStatus === "partial") add(claimIssue("weak_evidence", "warning", claim, `Claim evidence is ${claim.evidenceStatus}.`, "Add stronger source trace, caveats, or narrow the claim to the supported scope."))
161
+ if (claim.unsupportedScope) add(claimIssue("unsupported_scope", "warning", claim, "Claim has unsupported scope.", "Keep unsupported scope visible or revise the claim before rendering."))
162
+ }
163
+
164
+ for (const binding of narrative.evidenceBindings) {
165
+ if (binding.unsupportedScope) {
166
+ const claim = narrative.claims.find((item) => item.id === binding.claimId)
167
+ add({
168
+ type: "unsupported_scope",
169
+ severity: "warning",
170
+ message: "Evidence binding records unsupported scope.",
171
+ suggestedAction: "Preserve the unsupported scope caveat or add separate evidence before expanding the claim.",
172
+ claimId: binding.claimId,
173
+ claimText: claim?.text,
174
+ source: binding.source,
175
+ })
176
+ }
177
+ }
178
+
179
+ if (hasRecommendation(narrative) && !hasRiskHandling(narrative)) add(blocker("missing_risk", "Recommendation narrative lacks risk, assumption, or caveat handling.", "Add a risk, assumption, caveat, or tradeoff before approval."))
180
+ for (const objection of narrative.objections) {
181
+ if (objection.priority === "high" && !objection.response) add({
182
+ type: "unhandled_objection",
183
+ severity: "blocker",
184
+ message: "High-priority objection has no response.",
185
+ suggestedAction: "Add a response, evidence boundary, or fallback framing for this objection.",
186
+ claimId: objection.claimId,
187
+ claimText: objection.text,
188
+ })
189
+ }
190
+
191
+ for (const action of state.actions ?? []) {
192
+ if (action.type !== "research.findings_saved") continue
193
+ const path = typeof action.outputs?.path === "string" ? action.outputs.path : undefined
194
+ if (!path) continue
195
+ const attached = Object.values(state.decks ?? {}).some((deck) => deck.researchPlan.some((axis) => axis.findingsFile === path))
196
+ const boundToNarrative = narrative.evidenceBindings.some((binding) => binding.findingsFile === path)
197
+ if (!attached && !boundToNarrative && !isVisualOrMediaFindings(action.inputs?.axis, path)) add({
198
+ type: "research_findings_unattached",
199
+ severity: "warning",
200
+ message: `Research findings are saved but not attached: ${path}`,
201
+ suggestedAction: "Attach the findings to a research axis or bind specific evidence before treating them as canonical support.",
202
+ source: path,
203
+ })
204
+ }
205
+
206
+ for (const gap of narrative.researchGaps ?? []) {
207
+ if (gap.status === "closed" || gap.status === "evidence_bound") continue
208
+ add({
209
+ type: "research_gap_open",
210
+ severity: "warning",
211
+ message: `Research gap is ${gap.status}: ${gap.question}`,
212
+ suggestedAction: gap.status === "open" || gap.status === "in_progress"
213
+ ? "Save findings, attach them to the research plan, and bind evidence before closing the gap."
214
+ : "Bind specific evidence from the findings before treating the gap as resolved.",
215
+ claimId: gap.targetType === "claim" ? gap.targetId : undefined,
216
+ source: gap.findingsFile,
217
+ })
218
+ }
219
+
220
+ const approval = approvalState(narrative, narrativeHash)
221
+ if (!approval.current) add({
222
+ type: approval.stale ? "approval_stale" : "approval_missing",
223
+ severity: "warning",
224
+ message: approval.stale ? "Latest narrative approval is stale." : "Narrative is not approved yet.",
225
+ suggestedAction: approval.stale ? "Review changes and approve the current narrative hash." : "Ask the user to approve the narrative before deck handoff.",
226
+ })
227
+
228
+ const blockers = issues.filter((issue) => issue.severity === "blocker").map((issue) => issue.message)
229
+ const warnings = issues.filter((issue) => issue.severity === "warning").map((issue) => issue.message)
230
+ const status = readinessStatus(issues, approval.current)
231
+ return {
232
+ status,
233
+ narrativeHash,
234
+ reviewedAt: now,
235
+ blockers,
236
+ warnings,
237
+ issues,
238
+ approval,
239
+ nextActions: nextActions(issues, approval.current),
240
+ }
241
+ }
242
+
243
+ function readinessStatus(issues: NarrativeReadinessIssue[], approvalCurrent: boolean): NarrativeReadinessStatus {
244
+ const blockers = issues.filter((issue) => issue.severity === "blocker")
245
+ if (blockers.some((issue) => issue.type === "missing_evidence" || issue.type === "unsupported_scope")) return "needs_research"
246
+ if (blockers.length > 0) return "blocked"
247
+ if (issues.some((issue) => issue.type === "missing_audience" || issue.type === "missing_belief_shift" || issue.type === "missing_decision" || issue.type === "missing_thesis" || issue.type === "claim_chain_gap")) return "needs_user_confirmation"
248
+ return approvalCurrent ? "approved" : "ready_for_approval"
249
+ }
250
+
251
+ function narrativeStatusFromReadiness(status: NarrativeReadinessStatus): NarrativeStateV1["status"] {
252
+ if (status === "blocked") return "needs_user_confirmation"
253
+ if (status === "needs_research") return "needs_research"
254
+ return status
255
+ }
256
+
257
+ function approvalState(narrative: NarrativeStateV1, narrativeHash: string): NarrativeApprovalState {
258
+ const narrativeApprovals = [...(narrative.approvals ?? [])].filter((approval) => approval.scope === "narrative" && approval.approvedBy === "user")
259
+ const latest = narrativeApprovals[narrativeApprovals.length - 1]
260
+ return { current: Boolean(latest && latest.narrativeHash === narrativeHash), stale: Boolean(latest && latest.narrativeHash !== narrativeHash), latest }
261
+ }
262
+
263
+ function isVisualOrMediaFindings(axis: unknown, path: string): boolean {
264
+ const value = `${typeof axis === "string" ? axis : ""} ${path}`.toLowerCase()
265
+ return /(^|[-_/\s])(image|images|media|asset|assets|visual|visuals|logo|logos|screenshot|screenshots)([-_/\s.]|$)/.test(value)
266
+ }
267
+
268
+ function nextActions(issues: NarrativeReadinessIssue[], approvalCurrent: boolean): string[] {
269
+ const blockers = issues.filter((issue) => issue.severity === "blocker")
270
+ if (blockers.length > 0) return unique(blockers.map((issue) => issue.suggestedAction))
271
+ const approvalIssue = issues.find((issue) => issue.type === "approval_missing" || issue.type === "approval_stale")
272
+ if (!approvalCurrent && approvalIssue) return [approvalIssue.suggestedAction]
273
+ return unique(issues.slice(0, 3).map((issue) => issue.suggestedAction))
274
+ }
275
+
276
+ function isDecisionOriented(narrative: NarrativeStateV1): boolean {
277
+ return Boolean(narrative.decision.action || narrative.decision.decisionType && narrative.decision.decisionType !== "understand" || narrative.claims.some((claim) => claim.kind === "recommendation" || claim.kind === "ask"))
278
+ }
279
+
280
+ function hasRecommendation(narrative: NarrativeStateV1): boolean {
281
+ return narrative.claims.some((claim) => claim.kind === "recommendation" || claim.kind === "ask") || /recommend|approve|invest|prioriti[sz]e|建议|批准|投资|优先/i.test(narrative.decision.action)
282
+ }
283
+
284
+ function needsClaimRelations(narrative: NarrativeStateV1): boolean {
285
+ if (!isDecisionOriented(narrative)) return false
286
+ if ((narrative.claimRelations ?? []).length > 0) return false
287
+ const flowClaims = narrative.claims.filter((claim) => claim.importance === "central" || claim.kind === "recommendation" || claim.kind === "ask")
288
+ return flowClaims.length > 1
289
+ }
290
+
291
+ function claimRelationIssues(narrative: NarrativeStateV1): NarrativeReadinessIssue[] {
292
+ const issues: NarrativeReadinessIssue[] = []
293
+ const claimsById = new Map(narrative.claims.map((claim) => [claim.id, claim]))
294
+ for (const relation of narrative.claimRelations ?? []) {
295
+ const fromClaim = claimsById.get(relation.fromClaimId)
296
+ const toClaim = claimsById.get(relation.toClaimId)
297
+ if (!relation.rationale?.trim()) {
298
+ issues.push({
299
+ type: "claim_chain_gap",
300
+ severity: "warning",
301
+ message: "Claim relation lacks objective causal rationale.",
302
+ suggestedAction: "Add a factual causal bridge explaining what the source claim establishes and why the target claim follows within the evidence boundary.",
303
+ claimId: relation.toClaimId,
304
+ claimText: toClaim?.text,
305
+ })
306
+ }
307
+ if (relationMayOverextendEvidence(relation.relation) && fromClaim && toClaim && weakSourceForCausalBridge(fromClaim, toClaim)) {
308
+ issues.push({
309
+ type: "claim_chain_gap",
310
+ severity: "warning",
311
+ message: "Claim relation may overextend the source claim's evidence boundary.",
312
+ suggestedAction: "Narrow the target claim, add stronger evidence, or rewrite the relation rationale so it objectively reflects the supported scope.",
313
+ claimId: relation.toClaimId,
314
+ claimText: toClaim?.text,
315
+ })
316
+ }
317
+ }
318
+ return issues
319
+ }
320
+
321
+ function relationMayOverextendEvidence(relation: string): boolean {
322
+ return relation === "supports" || relation === "leads_to" || relation === "depends_on"
323
+ }
324
+
325
+ function weakSourceForCausalBridge(fromClaim: NarrativeClaim, toClaim: NarrativeClaim): boolean {
326
+ if (!fromClaim.evidenceRequired) return false
327
+ const targetNeedsObjectiveBridge = toClaim.importance === "central" || toClaim.kind === "recommendation" || toClaim.kind === "ask"
328
+ if (!targetNeedsObjectiveBridge) return false
329
+ return fromClaim.evidenceStatus === "missing" || fromClaim.evidenceStatus === "weak" || fromClaim.evidenceStatus === "partial" || Boolean(fromClaim.unsupportedScope)
330
+ }
331
+
332
+ function hasRiskHandling(narrative: NarrativeStateV1): boolean {
333
+ return narrative.risks.length > 0 || narrative.claims.some((claim) => claim.kind === "risk" || claim.kind === "assumption" || claim.caveats?.length || claim.unsupportedScope) || narrative.evidenceBindings.some((binding) => binding.caveat || binding.unsupportedScope)
334
+ }
335
+
336
+ function blocker(type: NarrativeReadinessIssue["type"], message: string, suggestedAction: string): NarrativeReadinessIssue {
337
+ return { type, severity: "blocker", message, suggestedAction }
338
+ }
339
+
340
+ function warning(type: NarrativeReadinessIssue["type"], message: string, suggestedAction: string): NarrativeReadinessIssue {
341
+ return { type, severity: "warning", message, suggestedAction }
342
+ }
343
+
344
+ function claimIssue(type: NarrativeReadinessIssue["type"], severity: "blocker" | "warning", claim: NarrativeClaim, message: string, suggestedAction: string): NarrativeReadinessIssue {
345
+ return { type, severity, message, suggestedAction, claimId: claim.id, claimText: claim.text }
346
+ }
347
+
348
+ function dedupeApprovals(approvals: NarrativeApproval[]): NarrativeApproval[] {
349
+ return [...new Map(approvals.map((approval) => [approval.id, approval])).values()]
350
+ }
351
+
352
+ function unique(items: string[]): string[] {
353
+ return [...new Set(items.filter(Boolean))]
354
+ }
355
+
356
+ function clean(value: string | undefined): string | undefined {
357
+ const trimmed = value?.trim()
358
+ return trimmed || undefined
359
+ }
@@ -0,0 +1,250 @@
1
+ import { upsertDeck, upsertSlides, type DecksState, type EvidenceRef, type RequiredInputs, type SlideSpec } from "../decks-state"
2
+ import { ensureActiveHtmlDeckRenderTarget } from "../workspace-state/render-targets"
3
+ import { getClaimSlideRefs } from "./queries"
4
+ import { computeNarrativeHash } from "./hash"
5
+ import { normalizeNarrativeState } from "./normalize"
6
+ import { narrativeToBrief } from "./project-compat"
7
+ import type { NarrativeClaim, NarrativeEvidenceBinding, NarrativeStateV1 } from "./types"
8
+
9
+ export interface CompileDeckPlanOptions {
10
+ now?: string
11
+ }
12
+
13
+ export interface CompileDeckPlanResult {
14
+ compiled: boolean
15
+ skipped: boolean
16
+ reason?: string
17
+ narrativeHash: string
18
+ slideCount: number
19
+ slides: SlideSpec[]
20
+ }
21
+
22
+ export function compileDeckPlanFromNarrative(state: DecksState, options: CompileDeckPlanOptions = {}): { state: DecksState; result: CompileDeckPlanResult } {
23
+ const narrative = normalizeNarrativeState(state)
24
+ const narrativeHash = computeNarrativeHash(narrative)
25
+ const approval = hasCurrentApprovalOrOverride(narrative, narrativeHash)
26
+ if (!approval) {
27
+ return {
28
+ state: { ...state, narrative },
29
+ result: {
30
+ compiled: false,
31
+ skipped: true,
32
+ reason: "narrative must be approved or explicitly overridden before compiling a deck plan",
33
+ narrativeHash,
34
+ slideCount: 0,
35
+ slides: [],
36
+ },
37
+ }
38
+ }
39
+
40
+ const deckKey = state.activeDeck ?? Object.keys(state.decks)[0]
41
+ const deck = deckKey ? state.decks[deckKey] : undefined
42
+ const slug = deck?.slug ?? state.activeDeck ?? "deck"
43
+ const slides = buildSlides(narrative)
44
+ const requiredInputs: Partial<RequiredInputs> = {
45
+ topicClarified: true,
46
+ audienceClarified: Boolean(narrative.audience.primary),
47
+ languageDecided: Boolean(deck?.language),
48
+ sourceMaterialsIdentified: (state.workspace.sourceMaterials ?? []).length > 0 || narrative.evidenceBindings.length > 0,
49
+ researchNeedAssessed: true,
50
+ researchFindingsRead: narrative.evidenceBindings.some((binding) => Boolean(binding.findingsFile)),
51
+ slidePlanConfirmed: false,
52
+ designLayoutsFetched: false,
53
+ }
54
+ let next = upsertDeck({ ...state, narrative }, {
55
+ ...deck,
56
+ slug,
57
+ goal: deck?.goal || narrative.thesis?.statement || narrative.decision.action,
58
+ audience: narrative.audience.primary || deck?.audience,
59
+ outputPath: deck?.outputPath,
60
+ narrativeBrief: narrativeToBrief(narrative),
61
+ requiredInputs: {
62
+ ...(deck?.requiredInputs ?? {}),
63
+ ...requiredInputs,
64
+ } as RequiredInputs,
65
+ writeReadiness: deck?.writeReadiness ?? { status: "blocked" as const, blockers: [] },
66
+ })
67
+ next = upsertSlides(next, slug, slides)
68
+ next.narrative = { ...narrative, updatedAt: options.now ?? narrative.updatedAt }
69
+ const htmlTarget = ensureActiveHtmlDeckRenderTarget(next)
70
+ if (htmlTarget) {
71
+ htmlTarget.data = {
72
+ ...(htmlTarget.data ?? {}),
73
+ narrativeId: narrative.id,
74
+ narrativeHash,
75
+ claimSlideRefs: getClaimSlideRefs(next).map((ref) => ({
76
+ claimId: ref.claimId,
77
+ claimText: ref.claimText,
78
+ slideIndex: ref.slideIndex,
79
+ slideTitle: ref.slideTitle,
80
+ match: ref.match,
81
+ role: ref.role,
82
+ location: ref.location,
83
+ })),
84
+ }
85
+ }
86
+
87
+ return {
88
+ state: next,
89
+ result: {
90
+ compiled: true,
91
+ skipped: false,
92
+ narrativeHash,
93
+ slideCount: slides.length,
94
+ slides,
95
+ },
96
+ }
97
+ }
98
+
99
+ function buildSlides(narrative: NarrativeStateV1): SlideSpec[] {
100
+ const slides: SlideSpec[] = []
101
+ const centralClaims = narrative.claims.filter((claim) => claim.importance === "central")
102
+ const supportingClaims = narrative.claims.filter((claim) => claim.importance !== "central")
103
+ const evidenceByClaim = new Map<string, NarrativeEvidenceBinding[]>()
104
+ for (const binding of narrative.evidenceBindings) {
105
+ const list = evidenceByClaim.get(binding.claimId) ?? []
106
+ list.push(binding)
107
+ evidenceByClaim.set(binding.claimId, list)
108
+ }
109
+
110
+ slides.push({
111
+ index: slides.length + 1,
112
+ title: "Decision Context",
113
+ purpose: "Frame the audience belief shift and decision required before presenting the recommendation.",
114
+ narrativeRole: "context",
115
+ layout: "cover",
116
+ qa: false,
117
+ components: [],
118
+ content: {
119
+ headline: narrative.thesis?.statement || narrative.decision.action || "Narrative context",
120
+ body: [
121
+ narrative.audience.beliefBefore ? `Before: ${narrative.audience.beliefBefore}` : "Before belief needs confirmation.",
122
+ narrative.audience.beliefAfter ? `After: ${narrative.audience.beliefAfter}` : "After belief needs confirmation.",
123
+ ],
124
+ bullets: narrative.decision.action ? [`Decision: ${narrative.decision.action}`] : [],
125
+ },
126
+ evidence: [],
127
+ status: "planned",
128
+ })
129
+
130
+ for (const claim of centralClaims) slides.push(claimSlide(slides.length + 1, claim, evidenceByClaim.get(claim.id) ?? []))
131
+ if (supportingClaims.length > 0) {
132
+ const supportingBindings = supportingClaims.flatMap((claim) => evidenceByClaim.get(claim.id) ?? [])
133
+ slides.push({
134
+ index: slides.length + 1,
135
+ title: "Supporting Logic",
136
+ purpose: "Connect supporting claims to the central recommendation without overloading the main proof slides.",
137
+ narrativeRole: "evidence",
138
+ layout: "card-grid",
139
+ qa: true,
140
+ components: ["card"],
141
+ claimIds: supportingClaims.map((claim) => claim.id),
142
+ claimRefs: supportingClaims.map((claim) => ({ claimId: claim.id, role: "supporting" as const })),
143
+ evidenceBindingIds: supportingBindings.map((binding) => binding.id),
144
+ content: {
145
+ headline: "Supporting claims and boundaries",
146
+ bullets: supportingClaims.slice(0, 5).map((claim) => claim.text),
147
+ },
148
+ evidence: supportingBindings.map(evidenceRefFromBinding),
149
+ status: "planned",
150
+ })
151
+ }
152
+
153
+ if (narrative.risks.length > 0 || narrative.objections.length > 0) {
154
+ const challengedClaimRefs = [
155
+ ...narrative.risks.map((risk) => risk.claimId ? { claimId: risk.claimId, role: "risk" as const } : undefined).filter((ref): ref is { claimId: string; role: "risk" } => Boolean(ref)),
156
+ ...narrative.objections.map((objection) => objection.claimId ? { claimId: objection.claimId, role: "objection" as const } : undefined).filter((ref): ref is { claimId: string; role: "objection" } => Boolean(ref)),
157
+ ]
158
+ const challengedClaimIds = [...new Set(challengedClaimRefs.map((ref) => ref.claimId))]
159
+ slides.push({
160
+ index: slides.length + 1,
161
+ title: "Risks And Objections",
162
+ purpose: "Make caveats and stakeholder objections visible before asking for a decision.",
163
+ narrativeRole: "risk",
164
+ layout: "two-col",
165
+ qa: true,
166
+ components: ["card"],
167
+ claimIds: challengedClaimIds,
168
+ claimRefs: dedupeClaimRefs(challengedClaimRefs),
169
+ content: {
170
+ headline: "What could break the recommendation",
171
+ bullets: [
172
+ ...narrative.risks.slice(0, 3).map((risk) => risk.mitigation ? `${risk.text} Mitigation: ${risk.mitigation}` : risk.text),
173
+ ...narrative.objections.slice(0, 3).map((objection) => objection.response ? `${objection.text} Response: ${objection.response}` : objection.text),
174
+ ],
175
+ },
176
+ evidence: [],
177
+ status: "planned",
178
+ })
179
+ }
180
+
181
+ slides.push({
182
+ index: slides.length + 1,
183
+ title: "Decision Ask",
184
+ purpose: "Close with the explicit decision or action requested from the audience.",
185
+ narrativeRole: "ask",
186
+ layout: "closing",
187
+ qa: false,
188
+ components: [],
189
+ content: {
190
+ headline: narrative.decision.action || "Confirm the decision",
191
+ bullets: narrative.decision.consequenceOfNoDecision ? [`If no decision: ${narrative.decision.consequenceOfNoDecision}`] : [],
192
+ },
193
+ evidence: [],
194
+ status: "planned",
195
+ })
196
+
197
+ return slides
198
+ }
199
+
200
+ function claimSlide(index: number, claim: NarrativeClaim, bindings: NarrativeEvidenceBinding[]): SlideSpec {
201
+ return {
202
+ index,
203
+ title: titleFromClaim(claim),
204
+ purpose: `Prove or bound this ${claim.importance} ${claim.kind} claim for the audience.`,
205
+ narrativeRole: claim.kind === "risk" || claim.kind === "assumption" ? "risk" : claim.kind === "ask" ? "ask" : claim.kind === "recommendation" ? "recommendation" : "evidence",
206
+ layout: "two-col",
207
+ qa: true,
208
+ components: ["card"],
209
+ claimIds: [claim.id],
210
+ claimRefs: [{ claimId: claim.id, role: "primary" }],
211
+ evidenceBindingIds: bindings.map((binding) => binding.id),
212
+ content: {
213
+ headline: claim.text,
214
+ bullets: [claim.supportedScope, claim.unsupportedScope ? `Unsupported scope: ${claim.unsupportedScope}` : undefined, ...(claim.caveats ?? [])].filter((item): item is string => Boolean(item)),
215
+ },
216
+ evidence: bindings.map(evidenceRefFromBinding),
217
+ status: "planned",
218
+ }
219
+ }
220
+
221
+ function dedupeClaimRefs<T extends { claimId: string; role: "risk" | "objection" }>(refs: T[]): T[] {
222
+ const seen = new Set<string>()
223
+ return refs.filter((ref) => {
224
+ const key = `${ref.claimId}:${ref.role}`
225
+ if (seen.has(key)) return false
226
+ seen.add(key)
227
+ return true
228
+ })
229
+ }
230
+
231
+ function evidenceRefFromBinding(binding: NarrativeEvidenceBinding): EvidenceRef {
232
+ return {
233
+ source: binding.source,
234
+ quote: binding.quote,
235
+ url: binding.url,
236
+ sourcePath: binding.sourcePath,
237
+ location: binding.location,
238
+ findingsFile: binding.findingsFile,
239
+ caveat: binding.caveat || binding.unsupportedScope,
240
+ }
241
+ }
242
+
243
+ function titleFromClaim(claim: NarrativeClaim): string {
244
+ const words = claim.text.split(/\s+/).filter(Boolean).slice(0, 6).join(" ")
245
+ return words || claim.kind
246
+ }
247
+
248
+ function hasCurrentApprovalOrOverride(narrative: NarrativeStateV1, narrativeHash: string): boolean {
249
+ return narrative.approvals.some((approval) => approval.narrativeHash === narrativeHash && (approval.scope === "narrative" && approval.approvedBy === "user" || approval.scope === "render_override" || approval.approvedBy === "override"))
250
+ }