Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: share pipeline snapshot hashing without changing stale checks (#7870)

[._.]/ Adam Eivydd7504a02584
diff --git a/server/services/pipeline/contentHash.js b/server/services/pipeline/contentHash.js
new file mode 100644
index 0000000000..df9e7acb97
--- /dev/null
+++ b/server/services/pipeline/contentHash.js
@@ -0,0 +1,5 @@
+import { sha256Text } from '../../lib/fileCore.js';
+
+// Pin pipeline snapshots to their source content. Preserve the existing empty
+// fallback so stored hashes keep the same cache and staleness semantics.
+export const contentHash = (text) => sha256Text(text || '');
diff --git a/server/services/pipeline/continuityBible.js b/server/services/pipeline/continuityBible.js
index cc5335c4b9..986b1d7346 100644
--- a/server/services/pipeline/continuityBible.js
+++ b/server/services/pipeline/continuityBible.js
@@ -24,7 +24,7 @@
*/
import { join } from 'path';
-import { createHash } from 'crypto';
+import { contentHash } from './contentHash.js';
import { atomicWrite, readJSONFile } from '../../lib/fileUtils.js';
import { createKeyedFileWriteQueue } from '../../lib/fileWriteQueue.js';
import { createSseRunner } from '../../lib/sseUtils.js';
@@ -74,9 +74,6 @@ const LEDGER_OUTPUT_RESERVE_TOKENS = 6_000;
const nowIso = () => new Date().toISOString();
-// Content hash — pins an input so a later edit flips the ledger to `stale`.
-const contentHash = (text) => createHash('sha256').update(text || '').digest('hex');
-
// Defense-in-depth: refuse path-traversal-shaped ids before they reach the
// on-disk ledger path. Series ids are `ser-<uuid>` — restrict to a safe charset.
function assertValidSeriesId(id) {
diff --git a/server/services/pipeline/editorial/reviewStaleness.js b/server/services/pipeline/editorial/reviewStaleness.js
index d9bff41753..de423035b3 100644
--- a/server/services/pipeline/editorial/reviewStaleness.js
+++ b/server/services/pipeline/editorial/reviewStaleness.js
@@ -7,7 +7,7 @@
* drifting while leaving checkRunner focused on conducting checks.
*/
-import { createHash } from 'crypto';
+import { contentHash as sha256 } from '../contentHash.js';
import {
EDITORIAL_SOURCES,
comicLetteringIssues,
@@ -42,7 +42,6 @@ import { getSeriesCanon } from '../seriesCanon.js';
// load-time guard below ensures a newly declared source cannot silently hash as
// empty.
const HASH_SEP = '\u0000';
-const sha256 = (text) => createHash('sha256').update(text || '').digest('hex');
const SOURCE_RESOLVERS = {
manuscript: ({ manuscript }) => manuscript || '',
canon: ({ canon }) => canonicalStringify(canon ?? null),
diff --git a/server/services/pipeline/editorialAnalysis.js b/server/services/pipeline/editorialAnalysis.js
index 38288636f7..39defb951e 100644
--- a/server/services/pipeline/editorialAnalysis.js
+++ b/server/services/pipeline/editorialAnalysis.js
@@ -16,7 +16,7 @@
*/
import { join } from 'path';
-import { createHash } from 'crypto';
+import { contentHash } from './contentHash.js';
import { PATHS, atomicWrite, ensureDir, tryReadFile, safeJSONParse } from '../../lib/fileUtils.js';
import { runStagedLLM, resolveStageContext } from '../stageRunner.js';
import { manuscriptContentBudgetChars, estimateTokens } from '../../lib/contextBudget.js';
@@ -48,11 +48,6 @@ const ANALYSIS_OUTPUT_RESERVE_TOKENS = 4_000;
const nowIso = () => new Date().toISOString();
-// Snapshot content hash — sourceContentHash pins the analyzed draft so a later
-// edit flips the issue to `stale`. One-liner (matches writersRoom/local.js's
-// contentHash) — not worth a shared lib module + barrel entry.
-const contentHash = (text) => createHash('sha256').update(text || '').digest('hex');
-
// Defense-in-depth: refuse path-traversal-shaped ids before interpolating into
// the on-disk snapshot path. Issue ids are `iss-<uuid>` — restrict to a safe
// charset.
diff --git a/server/services/pipeline/editorialAnalysis.test.js b/server/services/pipeline/editorialAnalysis.test.js
index d0b6d2ad37..d9ae262772 100644
--- a/server/services/pipeline/editorialAnalysis.test.js
+++ b/server/services/pipeline/editorialAnalysis.test.js
@@ -94,7 +94,8 @@ describe('analyzeIssue', () => {
expect(snap.providerId).toBe('mock-provider');
expect(snap.runId).toBe('run-1');
expect(snap.sourceStage).toBe('prose');
- expect(snap.sourceContentHash).toBeTruthy();
+ // Keep existing persisted snapshot hashes compatible across refactors.
+ expect(snap.sourceContentHash).toBe('33d5cbd89afe65179526fec2bf154661bc0ae43e248397ee2421134e8aed35fe');
// Canon must be resolved from the SERIES OBJECT (not the seriesId string) —
// a regression to getSeriesCanon(issue.seriesId) would silently drop the
// character hints in production while a loose mock stayed green.
diff --git a/server/services/pipeline/perspectiveRewrite.js b/server/services/pipeline/perspectiveRewrite.js
index 89ecadf279..32e279d728 100644
--- a/server/services/pipeline/perspectiveRewrite.js
+++ b/server/services/pipeline/perspectiveRewrite.js
@@ -19,7 +19,8 @@
*/
import { join } from 'path';
-import { createHash, randomUUID } from 'crypto';
+import { randomUUID } from 'crypto';
+import { contentHash } from './contentHash.js';
import { PATHS, atomicWrite, ensureDir, tryReadFile, safeJSONParse } from '../../lib/fileUtils.js';
import { createKeyedFileWriteQueue } from '../../lib/fileWriteQueue.js';
import { runStagedLLM, resolveStageContext } from '../stageRunner.js';
@@ -65,10 +66,6 @@ const clampNum = (v, min, max, fallback = 0) => {
return Math.max(min, Math.min(max, Math.round(n)));
};
-// Snapshot content hash — pins the analyzed draft so a later edit flips a
-// rewrite to `stale`. One-liner matching editorialAnalysis.contentHash.
-const contentHash = (text) => createHash('sha256').update(text || '').digest('hex');
-
// Defense-in-depth: refuse path-traversal-shaped ids before interpolating into
// the on-disk path. Issue ids are `iss-<uuid>` — restrict to a safe charset.
function assertValidIssueId(id) {
diff --git a/server/services/pipeline/pipelineJudge.js b/server/services/pipeline/pipelineJudge.js
index e487e1e626..4f26856eee 100644
--- a/server/services/pipeline/pipelineJudge.js
+++ b/server/services/pipeline/pipelineJudge.js
@@ -28,7 +28,7 @@
*/
import { join } from 'path';
-import { createHash } from 'crypto';
+import { contentHash } from './contentHash.js';
import { PATHS, atomicWrite, ensureDir, tryReadFile, safeJSONParse } from '../../lib/fileUtils.js';
import { runStagedLLM, resolveStageContext, resolveJudgeForStage } from '../stageRunner.js';
import { manuscriptContentBudgetChars, estimateTokens } from '../../lib/contextBudget.js';
@@ -82,10 +82,6 @@ const JUDGE_OUTPUT_RESERVE_TOKENS = 2_500;
const nowIso = () => new Date().toISOString();
-// Snapshot content hash — pins the judged draft so a later edit flips `stale`.
-// One-liner (matches editorialAnalysis.js) — not worth a shared lib module.
-const contentHash = (text) => createHash('sha256').update(text || '').digest('hex');
-
// Defense-in-depth: refuse path-traversal-shaped ids before interpolating into
// the on-disk snapshot path (issue ids are `iss-<uuid>`).
function assertValidIssueId(id) {
diff --git a/server/services/pipeline/reverseOutline.js b/server/services/pipeline/reverseOutline.js
index cd386276c0..35832b73a4 100644
--- a/server/services/pipeline/reverseOutline.js
+++ b/server/services/pipeline/reverseOutline.js
@@ -21,7 +21,7 @@
*/
import { join } from 'path';
-import { createHash } from 'crypto';
+import { contentHash } from './contentHash.js';
import { atomicWrite, readJSONFile } from '../../lib/fileUtils.js';
import { createKeyedFileWriteQueue } from '../../lib/fileWriteQueue.js';
import { createSseRunner } from '../../lib/sseUtils.js';
@@ -87,10 +87,6 @@ const UNASSIGNED_PLOTLINE = Object.freeze({ id: '_unassigned', label: 'Unassigne
const nowIso = () => new Date().toISOString();
const colorForIndex = (i) => PLOTLINE_COLORS[i % PLOTLINE_COLORS.length];
-// Content hash — pins the analyzed manuscript so a later edit flips the outline
-// to `stale`. One-liner matching editorialAnalysis.contentHash.
-const contentHash = (text) => createHash('sha256').update(text || '').digest('hex');
-
// Defense-in-depth: refuse path-traversal-shaped ids before they reach the
// on-disk outline path. Series ids are `ser-<uuid>` — restrict to a safe charset.
function assertValidSeriesId(id) {