#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
/**
* External store for real backend policy runs (auto-run on upload).
*
* The auto-run controller fires a backend run for each enabled policy × each
* newly-uploaded file and records it here; the detail view's activity feed reads
* from it. `dispatched` keys (`policyKey:fileId`) ensure a given file is only
* ever run once per policy, surviving remounts via localStorage.
*
* Read with {@code useSyncExternalStore}; mutated by the controller.
*/
import { useSyncExternalStore } from "react";
import type {
PolicyExecutionTarget,
PolicyRunStatus,
} from "@editor/services/policyPipeline";
export interface PolicyRunRecord {
runId: string;
policyKey: string;
fileId: string;
fileName: string;
fileSize: number;
target: PolicyExecutionTarget;
status: PolicyRunStatus;
/** Pipeline progress reported by the run-status endpoint: the 1-based step
* currently running, and the total step count. Drive the "step X/Y" label
* while a run is in flight. Absent until the first status report. */
currentStep?: number;
stepCount?: number;
/** Output files (downloadable via /api/v1/general/files/{id}) once done. */
outputs: { fileId: string; fileName: string }[];
/** Snapshotted by the server: external results must never enter the workspace. */
externalOutput?: boolean;
/** True once delivery has been handled. */
imported?: boolean;
/** Output fileIds already imported — tracked per-file so a partial failure
* retries only the missing ones and never re-adds the ones that succeeded. */
importedFileIds?: string[];
/** Workspace fileIds of the imported output files (the versioned child for
* "new version", or the added file for "new file"). Drives the policy badge,
* which marks the policy's OUTPUT — not the input it ran on. */
outputFileIds?: string[];
error: string | null;
/** Stable backend failure code (e.g. an entitlement sentinel) when FAILED; null otherwise. */
errorCode?: string | null;
/** Set while an auto-retry is pending after a transient (queue-full) rejection, so the activity
* feed shows a soft "busy" row instead of a hard failure during the backoff window. */
retrying?: boolean;
/** A run computed entirely in the browser - it has no server run behind it,
* so it must never be polled for status (a status poll 404s and would flip a
* succeeded run to FAILED) or reconciled against the server. */
browserLocal?: boolean;
/** Epoch ms when the run was dispatched. */
startedAt: number;
}
/** Statuses of a run that is still executing (not yet settled). */
export const POLICY_IN_FLIGHT_STATUSES: readonly PolicyRunStatus[] = [
"PENDING",
"RUNNING",
"WAITING_FOR_INPUT",
];
/** Latest definitive result per policy/file, retained independently of the activity log. */
export interface PolicyRunOutcome {
policyKey: string;
fileId: string;
fileName?: string;
error?: string | null;
status: "COMPLETED" | "FAILED";
startedAt: number;
}
interface RunState {
runs: PolicyRunRecord[];
dispatched: string[];
outcomes: Record<string, PolicyRunOutcome>;
/** startedAt of the run that began the current processing "wave" — a burst of
* runs with no idle gap. Reset whenever a run is recorded while nothing is in
* flight. The panel's progress counts (X of Y processed) scope to this so they
* reflect the CURRENT upload, not the whole persisted history. */
waveStartedAt: number;
}
const IN_FLIGHT_STATUSES: ReadonlySet<PolicyRunStatus> = new Set([
"PENDING",
"RUNNING",
"WAITING_FOR_INPUT",
]);
/** True while a run is still working: dispatched/running, in a retry backoff, or
* done on the backend but not yet imported into the workspace. Shared so the
* wave tracking, the panel progress, and the file-row spinner all agree. */
export function isRunInFlight(run: PolicyRunRecord): boolean {
if (run.retrying) return true;
if (IN_FLIGHT_STATUSES.has(run.status)) return true;
return run.status === "COMPLETED" && !run.imported;
}
const STORAGE_KEY = "stirling-policy-runs";
/** Soft cap on stored runs so the activity log can't grow without bound. Only
* TERMINAL runs are ever evicted (see {@link capRuns}); in-flight runs are always
* kept, so a large upload batch (more files than this cap) still polls + imports
* every one — and the panel's processing count stays accurate. */
const MAX_RUNS = 200;
const TERMINAL: ReadonlySet<PolicyRunStatus> = new Set([
"COMPLETED",
"FAILED",
"CANCELLED",
]);
/**
* Trim the run list toward MAX_RUNS by dropping the OLDEST terminal runs first,
* never in-flight ones. Runs are newest-first, so we walk from the tail. If more
* than MAX_RUNS runs are still in flight (a very large batch) they're all kept —
* dropping a live run would orphan its polling/import and undercount progress.
*/
function capRuns(runs: PolicyRunRecord[]): PolicyRunRecord[] {
if (runs.length <= MAX_RUNS) return runs;
const trimmed = [...runs];
for (let i = trimmed.length - 1; i >= 0 && trimmed.length > MAX_RUNS; i--) {
const run = trimmed[i];
if (TERMINAL.has(run.status) && !isRunInFlight(run)) trimmed.splice(i, 1);
}
return trimmed;
}
function recordOutcome(
outcomes: Record<string, PolicyRunOutcome>,
run: PolicyRunRecord,
): Record<string, PolicyRunOutcome> {
// Success releases a failed input only after its replacement or labels have been imported.
if (
!run.fileId ||
run.browserLocal ||
(run.status !== "FAILED" && !(run.status === "COMPLETED" && run.imported))
)
return outcomes;
const key = dispatchKey(run.policyKey, run.fileId);
const previous = outcomes[key];
if (
previous &&
(previous.startedAt > run.startedAt ||
(previous.startedAt === run.startedAt && previous.status === run.status))
)
return outcomes;
return {
...outcomes,
[key]: {
policyKey: run.policyKey,
fileId: run.fileId,
fileName: run.fileName,
error: run.error,
status: run.status,
startedAt: run.startedAt,
},
};
}
function read(): RunState {
try {
const raw =
typeof localStorage !== "undefined"
? localStorage.getItem(STORAGE_KEY)
: null;
if (raw) {
const parsed = JSON.parse(raw) as Partial<RunState>;
const runs: PolicyRunRecord[] =
// Normalise older persisted records (which predate the `outputs` field)
// so consumers can always rely on `outputs` being an array.
Array.isArray(parsed.runs)
? parsed.runs.map((r) => ({
...r,
// Records written before the rename carry the key as `categoryId`. Without this
// their policy is undefined, so badges vanish and a retry re-runs the whole chain.
policyKey:
r.policyKey ?? (r as { categoryId?: string }).categoryId ?? "",
outputs: Array.isArray(r.outputs) ? r.outputs : [],
importedFileIds: Array.isArray(r.importedFileIds)
? r.importedFileIds
: [],
// Records predating per-run targets all executed on SaaS.
target: r.target === "local" ? "local" : "saas",
}))
: [];
return {
runs,
// Seed older caches from their remaining history; newer caches retain outcomes after pruning.
outcomes: runs.reduce(recordOutcome, parsed.outcomes ?? {}),
dispatched: Array.isArray(parsed.dispatched) ? parsed.dispatched : [],
waveStartedAt:
typeof parsed.waveStartedAt === "number" ? parsed.waveStartedAt : 0,
};
}
} catch {
// Corrupt/unavailable storage — start empty.
}
return { runs: [], dispatched: [], outcomes: {}, waveStartedAt: 0 };
}
let state: RunState = read();
const listeners = new Set<() => void>();
function notifyListeners() {
for (const l of listeners) l();
}
function emit() {
try {
if (typeof localStorage !== "undefined") {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
}
} catch {
// Best-effort persistence.
}
notifyListeners();
}
// Sync in-memory state when another tab (or a test via page.evaluate + dispatchEvent)
// writes to the same localStorage key.
if (typeof window !== "undefined") {
window.addEventListener("storage", (e) => {
if (e.key === STORAGE_KEY) {
state = read();
notifyListeners();
}
});
}
function subscribe(listener: () => void) {
listeners.add(listener);
return () => listeners.delete(listener);
}
function getSnapshot(): RunState {
return state;
}
const SERVER_SNAPSHOT: RunState = {
runs: [],
dispatched: [],
outcomes: {},
waveStartedAt: 0,
};
function getServerSnapshot(): RunState {
return SERVER_SNAPSHOT;
}
/** Non-hook check for background work that should yield while a policy wave is
* running (e.g. the sidebar's category backfill) — reads the store without
* subscribing, so callers don't re-render on every poll tick. */
export function hasInFlightPolicyRuns(): boolean {
return state.runs.some(isRunInFlight);
}
/** Key identifying a single (policy, file) run attempt. */
export function dispatchKey(policyKey: string, fileId: string): string {
return `${policyKey}:${fileId}`;
}
/** Whether this (policy, file) pair has already been dispatched. */
export function isDispatched(policyKey: string, fileId: string): boolean {
return state.dispatched.includes(dispatchKey(policyKey, fileId));
}
/** Walked back through this document's lineage. Only a COMPLETED server run counts as applied. */
export function appliedPoliciesFor(fileId: string): Set<string> {
const applied = new Set<string>();
let cursor: string | null = fileId;
// A lineage cannot outrun the recorded runs, and the bound also breaks a hand-edited cycle.
for (let step = 0; step < state.runs.length; step++) {
if (cursor === null) break;
const child: string = cursor;
cursor = null;
for (const run of state.runs) {
// A local first pass is not the policy's run: counting it would skip the escalation.
if (run.status !== "COMPLETED" || run.browserLocal) continue;
if (!(run.outputFileIds ?? []).includes(child)) continue;
applied.add(run.policyKey);
// An annotating run names its input as its own output, so it adds no lineage step.
if (run.fileId !== child) cursor = run.fileId;
}
}
return applied;
}
/** Record a newly-dispatched run (marks it dispatched + adds the record). */
export function recordRunStart(record: PolicyRunRecord) {
const key = dispatchKey(record.policyKey, record.fileId);
// A run recorded while nothing else is in flight begins a fresh wave, so the
// progress counts reset to this upload instead of accumulating across every
// past upload persisted in localStorage.
const waveStartedAt = state.runs.some(isRunInFlight)
? state.waveStartedAt
: record.startedAt;
state = {
runs: capRuns([record, ...state.runs]),
outcomes: recordOutcome(state.outcomes, record),
dispatched: state.dispatched.includes(key)
? state.dispatched
: [...state.dispatched, key],
waveStartedAt,
};
emit();
}
/**
* Add a run discovered on the backend that this client has no local record of (e.g. it was
* started before a refresh recorded it). Unlike {@link recordRunStart} this adds no dispatch key:
* the run already exists server-side, so re-dispatch isn't the concern; we only want it polled and
* its outputs imported. No-op if the run is already known (reconcile patches those via updateRun).
*/
export function addReconciledRun(record: PolicyRunRecord) {
if (state.runs.some((r) => r.runId === record.runId)) return;
state = {
...state,
runs: capRuns([record, ...state.runs]),
outcomes: recordOutcome(state.outcomes, record),
};
emit();
}
/** Mark a (policy, file) pair handled without a run, such as a file with no remaining bytes. */
export function markDispatched(policyKey: string, fileId: string) {
const key = dispatchKey(policyKey, fileId);
if (state.dispatched.includes(key)) return;
state = { ...state, dispatched: [...state.dispatched, key] };
emit();
}
/** Patch an in-flight run's status/outputs/error as it progresses. */
export function updateRun(runId: string, patch: Partial<PolicyRunRecord>) {
let changed = false;
let outcomes = state.outcomes;
const runs = state.runs.map((r) => {
if (r.runId !== runId) return r;
changed = true;
const updated = { ...r, ...patch };
outcomes = recordOutcome(outcomes, updated);
return updated;
});
if (!changed) return;
state = { ...state, runs, outcomes };
emit();
}
/** The current record for a run id, if any. */
export function getRun(runId: string): PolicyRunRecord | undefined {
return state.runs.find((r) => r.runId === runId);
}
/** Drop a run record (leaving its dispatched key intact). Used when retrying a
* queue-rejected run in place, so the replacement run doesn't stack a second row. */
export function removeRun(runId: string) {
if (!state.runs.some((r) => r.runId === runId)) return;
state = { ...state, runs: state.runs.filter((r) => r.runId !== runId) };
emit();
}
/** Reset the store — used by tests to isolate it. */
export function resetPolicyRuns() {
state = { runs: [], dispatched: [], outcomes: {}, waveStartedAt: 0 };
emit();
}
/** Synchronous enforcement state, available before the editor's effects mount. */
export function getPolicyRunOutcomes(): Readonly<
Record<string, PolicyRunOutcome>
> {
return state.outcomes;
}
export function usePolicyRuns(): PolicyRunRecord[] {
return useSyncExternalStore(
subscribe,
() => getSnapshot().runs,
() => getServerSnapshot().runs,
);
}
/** startedAt of the current processing wave (see {@link RunState.waveStartedAt}).
* The panel scopes its progress counts to runs at/after this. */
export function usePolicyWaveStart(): number {
return useSyncExternalStore(
subscribe,
() => getSnapshot().waveStartedAt,
() => getServerSnapshot().waveStartedAt,
);
}