Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

142 KB
Raw Blame
import { safeReviewBucketPath } from '../../../work-view/automatic-deliverable-reviews.js';
import type express from 'express';
import type { Request, Response, NextFunction } from 'express';
import { createHash, randomBytes, randomUUID } from 'node:crypto';
import { appendFileSync, existsSync, mkdirSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { basename, dirname, extname, join } from 'node:path';
import { z } from 'zod';
import { Database } from 'bun:sqlite';
import { CLAUDE_CONFIG_DIR, DATA_DIR, DB_PATH, getPackageRoot, USER_SETTINGS_PATH } from '../../../../shared/paths.js';
import { currentContextGeneration, onContextInvalidation } from '../../../../shared/context-invalidation.js';
import { isSharedWorkList } from '../../../work-view/shared-records.mjs';
import { isConversationKey } from '../../../work-view/request-projection.mjs';
import { SettingsDefaultsManager } from '../../../../shared/SettingsDefaultsManager.js';
import { logger } from '../../../../utils/logger.js';
import { hasForwardedClientHeaders, hasLoopbackHostHeader, isLocalhost } from '../../../../server/middleware/request-auth-helpers.js';
import { WorkStateSource, type WorkViewSnapshot } from '../../../work-view/data-source.mjs';
import { WorkViewReadWorkerClient } from '../../../work-view/WorkViewReadWorkerClient.js';
import { boundedDurationMs, type WorkViewReadPurpose } from '../../../work-view/WorkViewReadProtocol.js';
import type { ViewerSyncStatus } from '../../../../shared/viewer-sync-status.js';
import { workViewOverview, workViewRequestDetail } from '../../../work-view/overview.js';
import { FILE_CONTENT_TYPES } from '../../../work-view/work-product.mjs';
import { contentFingerprint, priorContentFingerprint, LocalReviewStore } from '../../../work-view/review-store.js';
import { MY_RULES_LIST, renderRulesFile, restoredSuggestionRowFields, ruleRowFields, type SavedRule } from '../../../work-view/my-rules.mjs';
import { claudeCodeImportLine, hasImportLine, homeRelativePath, RULES_AND_TOOLS_FILENAME } from '../../../../shared/claude-code-rules-import.js';
import { ensureRulesAndToolsFile } from '../../../context/RulesAndToolsFile.js';
import { focusAreaRowFields, MY_FOCUS_LIST, navWordIn, OTHER_FOCUS_AREA_ID, ownerGoalsAreaRows, placementRowFields, PROPOSAL_KINDS, readFocus, routerFocusRows, seedFocusRows, seedUndoRows, SUB_FOCUS_MIN_REQUESTS, subFocusPlacementRowFields, subFocusRowFields, type FocusArea, type RouterFocusBody } from '../../../work-view/my-focus.mjs';
import { MAX_WORK_STATE_FIELDS_JSON_CHARS } from '../../../work-view/work-state-limits.mjs';
import { AGENT_LIFECYCLE_LIST, agentStopFields } from '../../../work-view/agent-lifecycle.mjs';
import { getProjectContext } from '../../../../utils/project-name.js';
import { chatCompletionsHumanCopyProvider, HumanCopyWorker } from '../../../work-view/human-copy-worker.mjs';
import { resolveOpenRouterConfig } from '../../OpenRouterProvider.js';
import { resolveOpenAICompatConfig } from '../../OpenAICompatProvider.js';
import { resolveOpenAICompatReasoning } from '../../reasoning-budget.js';
import { AutoRefreshWorker, OWNER_REFRESH_REASON, refreshIntervalMs } from '../../../work-view/auto-refresh-worker.mjs';
import { buildRefreshTask } from '../../../work-view/refresh-task.js';
import { HostedIntent } from '../../../work-view/hosted-intent.js';
import { FocusShareError, FocusShares } from '../../../work-view/focus-share.js';
import { FocusSharePublisher } from '../../../work-view/focus-share-publisher.js';
import { cmemProOrigin } from '../../../../shared/cmem-gateway.js';
import { isOpenRouterApiUrl } from '../../../../shared/openrouter-base-url.js';
import { FOCUS_CHANGED_MESSAGE, focusChangeVersion } from '../../../../shared/focus-change-version.js';
import { REFRESH_START_REFUSAL } from '../../../../shared/work-view-refresh-reasons.js';
import { runAgentTask } from '../../../agent-runner/run.js';
import { ledgerSignature, readLedger } from '../../../work-view/request-groups.mjs';
import { configuredJudge, decideSuggestion, jevClientFromSettings } from '../../../work-view/request-judge.mjs';
import { labelOpenWorkState } from '../../../work-view/work-state-labels.mjs';
import { isReportedReadyForCheck } from '../../../work-view/review-readiness.js';
export { isReportedReadyForCheck } from '../../../work-view/review-readiness.js';
import { configuredFocusJudge } from '../../../work-view/focus-judge.mjs';
import { BaseRouteHandler } from '../BaseRouteHandler.js';
import { captureEvent } from '../../../telemetry/telemetry.js';
import { ownMemorySynced } from '../../../sync/own-memory-synced.js';
import { isForeignLoopbackBrowserWrite } from './SettingsRoutes.js';
import { validateBody } from '../middleware/validateBody.js';
import type { SetupScan, SetupScanState } from '../../../prompt-import/setup-scan.js';
import { readHistoryCounts, type HistoryImport } from '../../../setup/history-import.js';
import { createSetupTelemetry } from '../../../setup/telemetry.js';
import { createSetupInstallerResumer } from '../../../setup/resume-installer.js';
import { hasActiveSetupSession, hasHostedObserver, hasProConfiguration, readSetupState, setupStatePath, writeSetupState, type SetupCheckout, type SetupCheckoutServed, type SetupStateFile, type SetupStateView } from '../../../../shared/setup-state.js';
import { historyImportRunning, SETUP_BRANCHES, SETUP_CLOUD_SYNC_OFFERS, SETUP_DEVICE_CODE_PATTERN, SETUP_INSTALLER_STAGES, SETUP_MEMORY_BILLINGS, setupCloudSyncOffer } from '../../../../shared/setup-funnel.js';
import { hasPendingInstallSetup, SETUP_DSH_PROFILE_MAX_CHARACTERS, SETUP_INSTALL_NOTE_MAX_CHARACTERS, SETUP_INSTALL_STATUSES } from '../../../../shared/setup-install.js';
import {
  CONTINUABLE_PLATFORMS, CONVERSATION_SESSION_UUID_PATTERN, OPEN_IN_APP_CLICK_ID_PATTERN, type ConversationContinuation, type ConversationContinuationNotice,
  type ConversationOpenedInApp,
} from '../../../../shared/conversation-continuation.js';
import type { ConversationContinuationRunner } from '../../../work-view/conversation-continuation-runner.js';
import { WORK_VIEW_USAGE_VIEWS } from '../../../../shared/work-view-pages.js';
import { WORK_VIEW_FOCUS_ACTIONS } from '../../../../shared/work-view-focus-actions.js';

/** True while a process with this pid exists (signal 0 checks without signalling). */
function processIsAlive(pid: number): boolean {
  if (!Number.isInteger(pid) || pid <= 0) return false;
  try { process.kill(pid, 0); return true; } catch { return false; }
}

/** Recorded files plus the text kinds an agent attaches as a deliverable, served so a browser shows them rather than downloads them. */
const DELIVERABLE_CONTENT_TYPES: Readonly<Record<string, string>> = {
  ...FILE_CONTENT_TYPES,
  '.md': 'text/plain; charset=utf-8', '.markdown': 'text/plain; charset=utf-8', '.txt': 'text/plain; charset=utf-8',
  '.rst': 'text/plain; charset=utf-8', '.json': 'text/plain; charset=utf-8',
};

const reviewBody = z.object({
  requestId: z.string().min(1).max(2000),
  expectedFingerprint: z.string().regex(/^[a-f0-9]{64}$/),
  expectedReviewHash: z.string().min(1).max(128).nullable().optional(),
  checked: z.boolean(),
}).strict();

// The viewer is local even when another worker surface is explicitly exposed.
// Match the existing request-auth helpers, including rejecting proxy headers.
export function workViewLoopbackOnly(req: Request, res: Response, next: NextFunction): void {
  if (!isLocalhost(req) || !hasLoopbackHostHeader(req) || hasForwardedClientHeaders(req)) {
    res.status(403).json({ error: 'Work view is available only on this computer.' });
    return;
  }
  next();
}

// Browser writes must come from the viewer's own page. The Origin check is main's
// shared settings guard (isForeignLoopbackBrowserWrite); the work view also rejects
// cross-site fetch metadata and non-JSON bodies.
export function requireWorkViewOrigin(req: Request, res: Response, next: NextFunction): void {
  const valid = req.get('sec-fetch-site') !== 'cross-site' && !isForeignLoopbackBrowserWrite(req);
  if (!valid || !req.is('application/json')) {
    res.status(403).json({ error: 'Review changes must come from the local work viewer.' });
    return;
  }
  next();
}

// Reads that return share links or comments: the viewer's own page only. Worker CORS lets other local pages read
// responses, and a cross-origin fetch carries its Origin. `refusal` words the 403 for what the route serves.
function workViewReadOrigin(refusal: string) {
  return (req: Request, res: Response, next: NextFunction): void => {
    if (req.get('sec-fetch-site') === 'cross-site' || isForeignLoopbackBrowserWrite(req)) {
      res.status(403).json({ error: refusal });
      return;
    }
    next();
  };
}
export const requireWorkViewReadOrigin = workViewReadOrigin('Sharing is available only from the local work viewer.');
const requireSetupPageReadOrigin = workViewReadOrigin('The device code is available only from the local setup page.');

const ruleBody = z.object({
  action: z.enum(['approve', 'edit', 'turn-off', 'turn-on', 'dismiss', 'add', 'replace', 'restore-suggestion']),
  ruleId: z.string().min(1).max(80).optional(),
  rule: z.object({
    scopeTier: z.enum(['everywhere', 'project', 'situation']),
    scopeName: z.string().max(80),
    when: z.string().max(300),
    then: z.string().trim().min(1).max(600),
  }).strict().optional(),
}).strict();

// Area ids are short generated tokens, bounded like rule ids. A name is a nav label and a goal one sentence:
// 80 and 300 characters leave room for the evidence inside one 2000-character work-state row.
// A request id is a JSON key, bounded like reviewBody's.
const focusAreaIdField = z.string().min(1).max(80);
// A sub focus id is a generated token like an area id, so it takes the area id's bound.
const subFocusIdField = focusAreaIdField;
const focusBody = z.object({
  action: z.enum(WORK_VIEW_FOCUS_ACTIONS),
  focusAreaId: focusAreaIdField.optional(),
  /** rename-sub-focus and merge-sub-focus: the sub focus changed. move-to-sub-focus: the one to move into; null for none. */
  subFocusId: subFocusIdField.nullable().optional(),
  intoSubFocusId: subFocusIdField.optional(),
  name: z.string().trim().min(1).max(80).optional(),
  goal: z.string().trim().min(1).max(300).optional(),
  intoFocusAreaId: focusAreaIdField.optional(),
  /** reorder: every area in the owner's order, first = highest priority. */
  focusAreaIds: z.array(focusAreaIdField).min(1).optional(),
  requestId: z.string().min(1).max(2000).optional(),
  expectedVersion: z.string().min(1).optional(),
}).strict();

// The focus areas owner-goals mined from the owner's prompts (save_goals.py). Same name and goal bounds as
// focusBody. The evidence is one line pointing at the owner-goals item that holds the quotes; 300 characters
// keeps the whole area inside one 2000-character work-state row. No count cap: the owner has as many areas as they have.
const ownerGoalsFocusBody = z.object({
  save_key: z.string().regex(/^[a-f0-9]{24}:[a-f0-9]{64}$/).optional(),
  areas: z.array(z.object({
    name: z.string().trim().min(1).max(80),
    goal: z.string().trim().min(1).max(300),
    rank: z.number().int().positive(),
    /** The model's judgment: the saved area this one already is, or null for a new one. Never a name match. */
    matches: focusAreaIdField.nullable(),
    evidence: z.string().trim().min(1).max(300),
  }).strict()),
}).strict();

// One checked focus-step answer (request-groups.mjs record-focus): router placements and areas proposed from Other.
// Same name, sentence and request-id bounds as focusBody and reviewBody; a basis is focusBasis's 16 hex characters.
// No count caps: every request is placed, and the owner has as many areas as their work holds.
const anchorField = z.string().min(1).max(2000);
// focusBasis and the Other pass hash are 16 hex characters.
const basisField = z.string().regex(/^[0-9a-f]{16}$/);
const routerFocusBody = z.object({
  placements: z.array(z.object({
    anchorConversationKey: anchorField, focusAreaId: focusAreaIdField, basis: basisField, evidence: z.string().trim().min(1).max(300),
  }).strict()),
  proposals: z.array(z.object({
    kind: z.enum(PROPOSAL_KINDS as unknown as ['new', 'promote']),
    name: z.string().trim().min(1).max(80), goal: z.string().trim().min(1).max(300), about: z.string().trim().min(1).max(300),
    evidence: z.string().trim().min(1).max(300),
    sameAs: focusAreaIdField.nullable(),
    requests: z.array(z.object({ anchorConversationKey: anchorField, basis: basisField }).strict()).min(1),
  }).strict()),
  otherPassHash: basisField.nullable().optional(),
  // One sub focus answer for one kept area (request-groups.mjs validateSubFocusAnswer). A name is bounded like an area's;
  // a new sub focus holds SUB_FOCUS_MIN_REQUESTS or more, since a group of one is not a group. The pass hash is 16 hex, and so
  // is a failed pass's (the refresh's record of an area whose pass failed on that set of requests).
  subFocus: z.object({
    focusAreaId: focusAreaIdField,
    newSubFocuses: z.array(z.object({ name: z.string().trim().min(1).max(80), anchorConversationKeys: z.array(anchorField).min(SUB_FOCUS_MIN_REQUESTS) }).strict()),
    placements: z.array(z.object({ anchorConversationKey: anchorField, subFocusId: subFocusIdField.nullable() }).strict()),
    passHash: basisField.nullable(),
    failedPassHash: basisField.nullable().optional(),
  }).strict().nullable().optional(),
}).strict();

// The one-time seed (scripts/seed-focus-areas.mjs): suggested `source: seed` areas and router placements, or the undo of
// one seed from its note. Same name, sentence, id and anchor bounds as the other focus bodies; an area's `about` is the
// seed line's whole description, bounded only by the one work-state row it must fit (focusAreaRowFields checks the row).
// A confidence is the recorded probability, 0 to 1. No count caps: the seed covers every line and every routed request.
// The database the script read and expects this worker to write (a path; 4096 is PATH_MAX on macOS and Linux). A worker
// serving another data folder refuses, so a copy's settings pointing at the live port cannot write the live database.
const seedDatabasePathField = z.string().min(1).max(4096);
const seedFocusBody = z.discriminatedUnion('action', [
  z.object({
    action: z.literal('apply'),
    areas: z.array(z.object({
      key: z.string().min(1).max(80), name: z.string().trim().min(1).max(80), goal: z.string().trim().min(1).max(300),
      about: z.string().trim().min(1).max(MAX_WORK_STATE_FIELDS_JSON_CHARS), priorityRank: z.number().int().positive(),
      evidence: z.string().trim().min(1).max(300), matches: focusAreaIdField.nullable().optional(),
    }).strict()),
    placements: z.array(z.object({
      anchorConversationKey: anchorField, area: z.string().min(1).max(80), confidence: z.number().min(0).max(1),
      basis: basisField, evidence: z.string().trim().min(1).max(300),
    }).strict()),
    databasePath: seedDatabasePathField,
  }).strict(),
  z.object({
    action: z.literal('undo'),
    focusAreaIds: z.array(focusAreaIdField),
    placements: z.array(z.object({ anchorConversationKey: anchorField, focusAreaId: focusAreaIdField, basis: basisField }).strict()),
    databasePath: seedDatabasePathField,
  }).strict(),
]);

const markAgentFinishedBody = z.object({
  agentGroupId: z.string().min(1).max(2000),
}).strict();

// Continuing a conversation from My Work. The platforms and the session id pattern are the shared module's.
const conversationRef = z.object({
  platform: z.enum(CONTINUABLE_PLATFORMS),
  conversationSessionId: z.string().regex(CONVERSATION_SESSION_UUID_PATTERN),
}).strict();
// No .max() on the message: its bound is the worker's 5 MB JSON body limit (express.json in createMiddleware,
// middleware.ts), which is under Claude Code's own 10 MB stdin cap (headless docs, "Pipe data through Claude").
const continueConversationBody = conversationRef.extend({ ownerMessage: z.string().trim().min(1) }).strict();
// An open in its app and its stop name the click they belong to: the id the page made for it (OPEN_IN_APP_CLICK_ID_PATTERN).
const openInAppClickBody = conversationRef.extend({ openInAppClickId: z.string().regex(OPEN_IN_APP_CLICK_ID_PATTERN) }).strict();
// The read's query. Only a tool this cannot continue is refused here. Any other id is answered with its target, whose
// sentence says why it can't be continued, such as an id that is not a UUID. No .max() on the id: it is only matched
// against the UUID pattern before any lookup, and the URL carrying it is bounded by the HTTP server's header limit.
// Not strict, because in local API token mode a read may carry the token as ?token= (createLocalApiTokenGuard).
const conversationContinuationQuery = z.object({
  platform: z.enum(CONTINUABLE_PLATFORMS),
  conversationSessionId: z.string().min(1),
});
const CONVERSATION_CONTINUATION_UNAVAILABLE = 'Continuing a conversation is unavailable on this worker.';

// Shared focus pages. cmem.ai checks the page's shape (src/lib/focus-share/snapshot.ts there); here it only has to be an object.
const focusShareAreaBody = z.object({ focusAreaId: focusAreaIdField }).strict();
const focusShareCommentBody = z.object({ focusAreaId: focusAreaIdField, body: z.string().trim().min(1).max(4000) }).strict();

/** The owner's answer to the judge's "Looks like part of <request>" on a card. */
const suggestionBody = z.object({
  conversationKey: z.string().min(1).max(2000),
  action: z.enum(['join', 'not-this']),
  expectedSuggestionHash: z.string().regex(/^[a-f0-9]{64}$/).nullable().optional(),
}).strict();

// The viewer's page ids (src/ui/my-work/src/live/usage.ts `UsageView`: setup plus App.tsx `View`): a closed list, so a
// usage report can carry nothing else. A focus area is My Work filtered to that area, so it counts as `work`, never by name.
// `view_hosted_plans` is the "See plans and pricing" click on Work View's hosted offer, with the Cloud Sync plan setup
// kept, if any (the same closed values the setup funnel's action carries). It comes after setup, when the installer's
// setup session (and so the setup event route) is gone, so it is counted here.
const usageBody = z.discriminatedUnion('event', [
  z.object({ event: z.literal('opened'), view: z.enum(WORK_VIEW_USAGE_VIEWS) }).strict(),
  z.object({ event: z.literal('switched'), view: z.enum(WORK_VIEW_USAGE_VIEWS), previousView: z.enum(WORK_VIEW_USAGE_VIEWS) }).strict(),
  z.object({ event: z.literal('view_hosted_plans'), cloudSyncOffer: z.enum(SETUP_CLOUD_SYNC_OFFERS).optional() }).strict(),
]);

/** The line that makes Claude Code load the approved rules with the owner's user-level instructions.
 * It names the rules file the worker actually writes, so a custom data folder is still the one loaded. */
export function myRulesImportLine(rulesFilePath: string): string {
  return claudeCodeImportLine(rulesFilePath);
}

interface WorkStateWrite { project: string; listName: string; fields: Record<string, string | number | boolean | null> }

interface WorkViewOptions {
  saveSharedReview?: (input: { descriptor: import('../../../work-view/shared-reviews.mjs').SharedReviewDescriptor; checked: boolean; expectedHash: string | null }) => { changed: number };
  prepareSharedOwnerAction?: () => Promise<void>;
  answerSharedSuggestion?: (conversationKey: string, action: 'join' | 'not-this', expectedHash: string | null) => unknown;
  persistSharedCopy?: (requestId: string, entry: { fingerprint: string; copy: unknown; generatedAt: number }) => void;
  saveSharedLedgerFile?: (name: string, value: unknown) => void;
  getSharedStatus?: () => Record<string, unknown>;
  getSyncStatus?: () => ViewerSyncStatus;
  updateViewerActivity?: (viewerId: string, visible: boolean, sequence: number) => boolean;
  databasePath?: string;
  reviewPath?: string;
  pollIntervalMs?: number;
  getLocalDeviceId?: () => string;
  humanCopy?: HumanCopyWorker | null;
  /** Runs owner-goals and request-progress when something is due. Omitted: on for the default ledger only. */
  autoRefresh?: AutoRefreshWorker | null;
  /** Overall-request ledger written by the request-progress skill; null disables it. */
  ledgerDir?: string | null;
  /** Saves one work-state row (rule changes). Without it, rules are read-only. */
  appendWorkStateEntry?: ((entry: WorkStateWrite) => void) | null;
  /** The project rule rows are saved under: the home folder's, like owner-goals. */
  homeProject?: string;
  /** <data dir>/MY-RULES.md: approved rules, rebuilt after every rule change, local or synced. */
  rulesFilePath?: string;
  /** <Claude config dir>/CLAUDE.md: the user-level file that imports MY-RULES.md. Never a project's file. */
  claudeInstructionsPath?: string;
  /** The install funnel's prompt scan. Without it the setup scan endpoints answer 503. */
  setupScan?: SetupScan | null;
  /** The install funnel's per-machine state file (choice, installer stage, completion). */
  setupStatePath?: string;
  /** Starts the installed setup executor when a person opens deferred onboarding. */
  resumeSetup?: (retry?: boolean) => Promise<void>;
  setupTelemetry?: ReturnType<typeof createSetupTelemetry>;
  /** Read for `alreadyPro`; defaults to ~/.claude-mem/settings.json. */
  settingsPath?: string;
  /** Finish Setup's history upload to CMEM Pro. Without it POST /api/setup/import and /api/setup/import/stop answer 503. */
  historyImport?: HistoryImport | null;
  /** Setup extraction only. Ongoing automatic refresh continues to use the local agent runner. */
  hostedIntent?: Pick<HostedIntent, 'run' | 'state'> | null;
  /** Shared focus pages on cmem.ai. Defaults to the cloud sync credential and focus-shares.json beside settings.json. */
  focusShares?: Pick<FocusShares, 'connected' | 'list' | 'publish' | 'stop' | 'comments' | 'comment'> | null;
  /** How often shared focus pages are republished from this worker; 0 stops the timer (tests). */
  focusSharePublishEveryMs?: number;
  /**
   * Sends the owner's message from My Work to a Claude Code or Codex conversation as one turn. Without it the
   * conversation routes answer 503. Its onChange should be publishConversationContinuation, as the worker's is.
   */
  conversationContinuationRunner?: ConversationContinuationRunner;
}

/** One overall request on the Reveal, read from the request-group ledger so new installs (prompts only) have work too. */
export interface SetupWorkItem { groupId: string; title: string; platform: string | null; lastActiveAt: string | null; progressPercent: number | null }
const SETUP_WORK_SHOWN = 5;

const setupScanBody = z.object({ improveProduct: z.boolean().optional() }).strict();
const setupScanConsentBody = z.object({ upload: z.boolean(), agents: z.array(z.enum(['claude-code', 'codex', 'gemini-cli', 'antigravity-cli', 'opencode'])).optional(), improveProduct: z.boolean().optional() }).strict();
const setupScanPreferencesBody = z.object({ improveProduct: z.boolean() }).strict();
// Request-size and abuse caps on loopback writes, set about twice what the installer offers today (measured
// 2026-11-11: 19 agents in ide-detection.ts, 30 bundled skills, the longest id 17 characters). The email cap is RFC 5321's 254-character path
// limit; the legacy signup note's is SETUP_INSTALL_NOTE_MAX_CHARACTERS (setup-install.ts), the
// same 2,000 characters as the installer's error reports below.
const setupInstallId = z.string().min(1).max(80);
const setupInstallSelectionBody = z.object({
  agents: z.array(setupInstallId).min(1).max(40),
  skills: z.array(setupInstallId).max(200),
  installClaudeCode: z.boolean(),
  disableAutoMemory: z.boolean(),
  telemetryEnabled: z.boolean(),
  email: z.union([z.literal(''), z.email().max(254)]).optional(),
  note: z.string().max(SETUP_INSTALL_NOTE_MAX_CHARACTERS).optional(),
  emailProvided: z.boolean().optional(),
  claudeModel: z.string().trim().min(1).max(200).optional(),
  dshProfile: z.string().trim().min(1).max(SETUP_DSH_PROFILE_MAX_CHARACTERS).optional(),
}).strict();
const setupInstallCatalogBody = z.object({
  agents: z.array(z.object({ id: setupInstallId, label: z.string().max(200), hint: z.string().max(1000), detected: z.boolean() }).strict()).min(1).max(40),
  skills: z.array(z.object({ id: setupInstallId, label: z.string().max(200), description: z.string().max(2000), required: z.boolean().optional() }).strict()).max(200),
  defaults: setupInstallSelectionBody,
  reinstall: z.boolean(),
  telemetryLocked: z.boolean().optional(),
  skillsLocked: z.boolean().optional(),
  emailEnabled: z.boolean().optional(),
  signupSaved: z.boolean().optional(),
}).strict();
const setupInstallReportBody = z.object({
  status: z.enum(SETUP_INSTALL_STATUSES),
  catalog: setupInstallCatalogBody.optional(),
  error: z.string().max(2000).nullable().optional(),
}).strict();
/**
 * Localhost pages can read the worker's setup state (GET /api/setup/state and the `setup-state` event frame), and
 * the signup email and note are data no such page may read (SettingsRoutes keeps them out of /api/settings too).
 * Only the installer reads them, from the state file.
 */
function redactSetupInstall(install: SetupStateFile['install']): SetupStateFile['install'] {
  if (!install?.selection) return install;
  const { email, note: _note, ...selection } = install.selection;
  return { ...install, selection: email ? { ...selection, emailProvided: true } : selection };
}

/**
 * The device code is no page's to read either: typed into someone else's signed-in browser, it would connect this
 * device to their account. The served state says only that one waits; the setup page reads it from the origin-guarded
 * GET /api/setup/checkout/device-code.
 */
function redactSetupCheckout(checkout: SetupCheckout | null): SetupCheckoutServed | null {
  if (!checkout) return null;
  const { deviceCode, ...served } = checkout;
  return deviceCode && checkout.state === 'waiting' ? { ...served, deviceCodeWaiting: true } : served;
}

// Cloud Sync can use the existing provider. An explicit offer never changes a local or legacy hosted choice.
const setupChoiceBody = z.object({
  branch: z.enum(SETUP_BRANCHES),
  memoryBilling: z.enum(SETUP_MEMORY_BILLINGS),
  cloudSyncOffer: z.enum(SETUP_CLOUD_SYNC_OFFERS).optional(),
  shareWorkView: z.literal(true).optional(),
  openCheckout: z.literal(true).optional(),
}).strict().refine(choice => !choice.cloudSyncOffer || (choice.branch === 'go' && choice.memoryBilling === 'existing'), {
  message: 'A Cloud Sync offer requires On The Go with your existing memory provider.',
}).refine(choice => !choice.shareWorkView || !!choice.cloudSyncOffer, {
  message: 'Work View sharing requires an explicit standalone Cloud Sync offer.',
}).refine(choice => !choice.openCheckout || !!choice.cloudSyncOffer, {
  message: 'Select a Cloud Sync offer to open its checkout.',
});
const setupInstallerBody = z.object({
  stage: z.enum(SETUP_INSTALLER_STAGES),
  error: z.string().max(2000).nullable().optional(),
  /** The installer mirrors the pairing checkout it is running (funnel Phase 8). */
  checkout: z.enum(['waiting', 'paid', 'failed']).optional(),
  /** The pairing's code, sent with `waiting` so the setup page can show it; paid and failed clear it. */
  deviceCode: z.object({
    userCode: z.string().regex(SETUP_DEVICE_CODE_PATTERN),
    // The installer sends Date#toISOString(); nothing else is a time this field takes.
    expiresAt: z.string().max(40).refine(value => !Number.isNaN(Date.parse(value)) && new Date(value).toISOString() === value).nullable(),
  }).strict().optional(),
}).strict().refine(body => !body.deviceCode || body.checkout === 'waiting', {
  message: 'A device code is reported only while checkout waits.',
});
const setupCheckoutBody = z.object({}).strict();
const setupResumeBody = z.object({ retry: z.boolean().optional() }).strict();
const setupCompleteBody = z.object({
  /** Finish Setup's picked tools; the installer sets up the local ones it has not installed. */
  // Abuse cap: Finish Setup lists the selected agents (19 offered today) plus 3 cloud readers, so 40 leaves room.
  tools: z.array(z.string().min(1).max(40)).max(40).optional(),
  // Checked separately so a bad analytics payload cannot block real setup.
  telemetry: z.unknown().optional(),
}).strict();
const setupImportBody = z.object({}).strict();

type HumanCopyProvider = ReturnType<typeof chatCompletionsHumanCopyProvider> & {
  configKey?: string;
  isOff?: () => boolean;
  getConfigKey?: () => string | null;
};

function withHumanCopyConfigKey(provider: ReturnType<typeof chatCompletionsHumanCopyProvider>, config: unknown[]): HumanCopyProvider {
  return Object.assign(provider, { configKey: createHash('sha256').update(JSON.stringify(config)).digest('hex') });
}

/** Resolve the explicitly selected copy endpoint, or follow today's primary-provider behavior. */
function resolveConfiguredHumanCopyProvider(settingsPath: string): HumanCopyProvider | null {
  const settings = SettingsDefaultsManager.loadFromFile(settingsPath, true, { readOnly: true });
  // Unset keeps today's behaviour: only an OpenRouter primary writes card wording. Any other endpoint, an
  // OpenAI-compatible primary included, spends tokens on it only once selected here.
  const selection = settings.CLAUDE_MEM_WORK_VIEW_COPY_PROVIDER?.trim() || (settings.CLAUDE_MEM_PROVIDER === 'openrouter' ? 'openrouter' : '');
  const modelOverride = settings.CLAUDE_MEM_WORK_VIEW_COPY_MODEL?.trim();

  if (selection === 'openrouter') {
    const config = resolveOpenRouterConfig(settingsPath);
    const model = modelOverride || config.model;
    if (!config.apiUrl || !config.apiKey || !model) return null;
    const fallbackModels = !modelOverride && isOpenRouterApiUrl(config.apiUrl) ? config.fallbackModels : [];
    return withHumanCopyConfigKey(chatCompletionsHumanCopyProvider({
      url: config.apiUrl, apiKey: config.apiKey, model, ...(fallbackModels.length ? { fallbackModels } : {}),
    }), ['openrouter', config.apiUrl, config.apiKey, model, fallbackModels]);
  }
  if (selection === 'openai-compatible') {
    const config = resolveOpenAICompatConfig(settingsPath);
    const model = modelOverride || config.model;
    if (!config.apiUrl || !model || (config.requiresApiKey && !config.apiKey)) return null;
    // The observer's reasoning setting for this endpoint applies to card wording too, and a strict server that
    // rejects the field gets one resend without it (the observer's own rule, OpenAICompatProvider).
    const sendReasoningOff = resolveOpenAICompatReasoning(settings.CLAUDE_MEM_OPENAI_COMPAT_REASONING) !== 'on';
    return withHumanCopyConfigKey(chatCompletionsHumanCopyProvider({ url: config.apiUrl, apiKey: config.apiKey, model, sendReasoningOff }),
      ['openai-compatible', config.apiUrl, config.apiKey, model, sendReasoningOff]);
  }
  return null;
}

/**
 * My Work summaries follow an OpenRouter primary provider by default, as before.
 * Any other configured endpoint is used only when selected here;
 * the observer's quota-fallback key is never activated merely because it exists.
 * Settings and credentials are resolved for each availability check/request so
 * saves take effect without rebuilding the routes.
 */
export function configuredHumanCopyProvider(settingsPath = USER_SETTINGS_PATH): HumanCopyProvider {
  const provider = (async (input: unknown) => {
    const resolved = resolveConfiguredHumanCopyProvider(settingsPath);
    if (!resolved) throw new Error('No supported My Work summary provider is configured.');
    return resolved(input);
  }) as HumanCopyProvider & { isAvailable: () => boolean; isOff: () => boolean; getConfigKey: () => string | null };
  provider.isAvailable = () => {
    try { return resolveConfiguredHumanCopyProvider(settingsPath) !== null; }
    catch { return false; }
  };
  provider.isOff = () => {
    try { return SettingsDefaultsManager.loadFromFile(settingsPath, true, { readOnly: true }).CLAUDE_MEM_WORK_VIEW_COPY_PROVIDER?.trim() === 'off'; }
    catch { return false; }
  };
  provider.getConfigKey = () => {
    try { return resolveConfiguredHumanCopyProvider(settingsPath)?.configKey ?? null; }
    catch { return null; }
  };
  return provider;
}

/** My Work's automatic refresh on the user's own logged-in agent, unless
 * CLAUDE_MEM_WORK_VIEW_AUTO_REFRESH is 'false' (checked on every tick). */
export function defaultAutoRefresh(ledgerDir: string, settingsPath = USER_SETTINGS_PATH, setupPath = setupStatePath(), setupActive = () => false,
  localDeviceId: () => string, saveFocus: (body: RouterFocusBody) => { placed: number; focusAreaIds: string[] } | Promise<{ placed: number; focusAreaIds: string[] }>): AutoRefreshWorker {
  const openDatabase = () => {
    const database = new Database(DB_PATH, { readonly: true, create: false });
    database.exec('PRAGMA busy_timeout = 1500');
    return database;
  };
  // Scheduled and manual refresh wait while the setup scan uses the worker.
  const setupRunning = () => {
    const state = readSetupState(setupPath);
    return setupActive() || hasPendingInstallSetup(state) || (state.status !== 'complete' && hasActiveSetupSession(setupPath));
  };
  return new AutoRefreshWorker({
    dir: ledgerDir,
    openDatabase,
    setupRunning,
    checkMs: refreshIntervalMs(SettingsDefaultsManager.loadFromFile(settingsPath, true, { readOnly: true }).CLAUDE_MEM_WORK_VIEW_REFRESH_INTERVAL_MIN),
    enabled: () => !setupRunning()
      && SettingsDefaultsManager.loadFromFile(settingsPath, true, { readOnly: true }).CLAUDE_MEM_WORK_VIEW_AUTO_REFRESH !== 'false',
    runJob: async ({ needs, reason }) => {
      // The judges are read from settings per run, so a provider change applies to the next refresh.
      const setup = reason === 'setup-scan';
      const task = buildRefreshTask({ needs, reason, dataDir: DATA_DIR, ledgerDir, pluginRoot: getPackageRoot(), openDatabase, localDeviceId, saveFocus,
        judge: setup ? null : configuredJudge(settingsPath), focusJudge: setup ? null : configuredFocusJudge(settingsPath) });
      return { ...(await runAgentTask(task)), steps: task.steps, groupsLeftWithoutReview: task.groupsLeftWithoutReview };
    },
    // Jev labels open work-state items on every check; read from settings each time, like the judges.
    labelWorkState: async () => {
      const client = jevClientFromSettings(SettingsDefaultsManager.loadFromFile(settingsPath, true, { readOnly: true }));
      if (!client) return null;
      const database = openDatabase();
      try { return await labelOpenWorkState({ dir: ledgerDir, database, client, localDeviceId: localDeviceId() }); }
      finally { database.close(); }
    },
    log: (level, message, data) => (level === 'info' ? logger.info : logger.warn).call(logger, 'WORKER', message, data),
    localDeviceId,
  });
}


/**
 * How long a background read may reuse the last activity signature (observations, prompts, sessions) instead of
 * recomputing it. 120 s is the window measured in #5124: on a 2.85 GB database with 4 live sessions, recomputing
 * activity at most every 120 s kept /api/health at a 7 ms median (85 probes, none above 0.2 s) with an event stream
 * open, versus 30 s stalls when it ran every 1 s tick. The owner asked for this window plus that health-probe
 * measurement as the regression check. Work-state, title-cache and ledger changes always bypass it.
 */
const ACTIVITY_REFRESH_WINDOW_MS = 120_000;

export class WorkViewRoutes extends BaseRouteHandler {
  private readonly saveSharedReview: WorkViewOptions['saveSharedReview'];
  private readonly prepareSharedOwnerAction: WorkViewOptions['prepareSharedOwnerAction'];
  private readonly answerSharedSuggestion: WorkViewOptions['answerSharedSuggestion'];
  private saveSharedLedgerFile: WorkViewOptions['saveSharedLedgerFile'];
  private readonly getSharedStatus: WorkViewOptions['getSharedStatus'];
  private readonly getSyncStatus: (() => ViewerSyncStatus) | undefined;
  private readonly updateViewerActivity: WorkViewOptions['updateViewerActivity'];
  private source: WorkStateSource;
  private sourceReader: WorkViewReadWorkerClient;
  private lastSnapshotDiagnostic: { purpose: WorkViewReadPurpose; postprocessMs: number } | null = null;
  private lastResponseDiagnostic: { transport: 'http' | 'sse'; writeMs: number; bytes: number } | null = null;
  private pendingBackgroundSnapshot: Promise<WorkViewSnapshot> | null = null;
  private ownerEditsDone: Promise<unknown> = Promise.resolve();
  private freshPublishReading = false;
  private freshPublishWanted = false;
  private reviews: LocalReviewStore;
  private clients = new Set<Response>();
  private overviewClients = new Set<Response>();
  private pendingBootstrap: { fence: string; promise: Promise<WorkViewSnapshot> } | null = null;
  private ownerReadFence = 0;
  private ownerEditActive = false;
  private overviewSignature = '';
  private poll: ReturnType<typeof setInterval> | null = null;
  private heartbeat: ReturnType<typeof setInterval> | null = null;
  private signature = '';
  private reviewStackWorkRevision?: WorkViewSnapshot['revision'];
  private closed = false;
  private pollIntervalMs: number;
  private getLocalDeviceId: () => string;
  private humanCopy: HumanCopyWorker | null;
  private autoRefresh: AutoRefreshWorker | null;
  private appendWorkStateEntry: ((entry: WorkStateWrite) => void) | null;
  private homeProject: string | undefined;
  private rulesFilePath: string;
  private claudeInstructionsPath: string;
  private setupScan: SetupScan | null;
  private setupStatePath: string;
  private setupReadExhausted = false;
  private setupTelemetry: ReturnType<typeof createSetupTelemetry> | null = null;
  private settingsPath: string;
  private unsubscribeSetupScan: (() => void) | null = null;
  private ledgerDir: string | null;
  private databasePath: string;
  private setupWorkCache: { signature: string; work: SetupWorkItem[] | null } | null = null;
  private historyImport: HistoryImport | null;
  private unsubscribeHistoryImport: (() => void) | null = null;
  private hostedIntent: Pick<HostedIntent, 'run' | 'state'> | null;
  private focusShares: NonNullable<WorkViewOptions['focusShares']> | null;
  private focusSharePublisher: FocusSharePublisher | null;
  private resumeSetup: (retry?: boolean) => Promise<void>;
  private readonly conversationContinuationRunner: ConversationContinuationRunner | null;

  constructor(options: WorkViewOptions = {}) {
    super();
    this.saveSharedReview = options.saveSharedReview;
    this.prepareSharedOwnerAction = options.prepareSharedOwnerAction;
    this.answerSharedSuggestion = options.answerSharedSuggestion;
    this.saveSharedLedgerFile = options.saveSharedLedgerFile;
    this.getSharedStatus = options.getSharedStatus;
    this.getSyncStatus = options.getSyncStatus;
    this.updateViewerActivity = options.updateViewerActivity;
    this.conversationContinuationRunner = options.conversationContinuationRunner ?? null;
    this.pollIntervalMs = options.pollIntervalMs ?? 1000;
    this.getLocalDeviceId = options.getLocalDeviceId ?? (() => '');
    const ledgerDir = options.ledgerDir === undefined ? join(DATA_DIR, 'work-view') : options.ledgerDir;
    this.ledgerDir = ledgerDir;
    this.databasePath = options.databasePath ?? DB_PATH;
    this.source = new WorkStateSource({
      databasePath: options.databasePath ?? DB_PATH,
      pollIntervalMs: this.pollIntervalMs,
      ledgerDir,
    });
    this.sourceReader = new WorkViewReadWorkerClient({ databasePath: options.databasePath ?? DB_PATH, ledgerDir, pollIntervalMs: this.pollIntervalMs });
    this.reviews = new LocalReviewStore(options.reviewPath ?? join(DATA_DIR, 'work-view-reviews.db'));
    this.humanCopy = options.humanCopy === undefined
      ? new HumanCopyWorker({ provider: configuredHumanCopyProvider(), cachePath: join(DATA_DIR, 'work-view', 'human-copy.json'), persistSharedCopy: options.persistSharedCopy,
        warn: message => logger.warn('WORKER', message) })
      : options.humanCopy;
    this.humanCopy?.pauseWhile(() => this.setupIsActive());
    if (this.humanCopy) void this.humanCopy.load().then(() => this.humanCopy?.start());
    // A test or demo with its own ledger never starts an agent unless it passes one in.
    this.autoRefresh = options.autoRefresh === undefined
      ? (options.ledgerDir === undefined && ledgerDir ? defaultAutoRefresh(ledgerDir, options.settingsPath, options.setupStatePath,
        () => this.setupIsActive(), () => this.getLocalDeviceId(), body => this.recordRouterFocus(body)) : null)
      : options.autoRefresh;
    this.autoRefresh?.start();
    this.appendWorkStateEntry = options.appendWorkStateEntry ?? null;
    this.homeProject = options.homeProject;
    this.rulesFilePath = options.rulesFilePath ?? join(DATA_DIR, 'MY-RULES.md');
    this.claudeInstructionsPath = options.claudeInstructionsPath ?? join(CLAUDE_CONFIG_DIR, 'CLAUDE.md');
    this.setupScan = options.setupScan ?? null;
    this.setupStatePath = options.setupStatePath ?? setupStatePath();
    this.resumeSetup = options.resumeSetup ?? createSetupInstallerResumer(this.setupStatePath, {
      onStateChange: () => this.broadcast('setup-state', this.setupStateView()),
    });
    this.setupTelemetry = options.setupTelemetry ?? null;
    this.settingsPath = options.settingsPath ?? USER_SETTINGS_PATH;
    this.historyImport = options.historyImport ?? null;
    this.hostedIntent = options.hostedIntent === undefined
      ? (this.setupScan && ledgerDir && this.appendWorkStateEntry ? new HostedIntent({
        origin: () => cmemProOrigin(),
        intentConfigPath: join(dirname(this.settingsPath), 'intent-install.json'),
        credentials: () => {
          const settings = SettingsDefaultsManager.loadFromFile(this.settingsPath, true, { readOnly: true });
          return { token: settings.CLAUDE_MEM_CLOUD_SYNC_TOKEN?.trim() ?? '', userId: settings.CLAUDE_MEM_CLOUD_SYNC_USER_ID?.trim() ?? '' };
        },
        ledgerDir,
        openDatabase: () => new Database(this.databasePath, { readonly: true, create: false }),
        appendWorkStateEntry: entry => this.appendWorkStateEntry!(entry),
        homeProject: () => this.homeProject ?? getProjectContext(homedir()).primary,
        localDeviceId: () => this.getLocalDeviceId(),
      }) : null)
      : options.hostedIntent;
    this.focusShares = options.focusShares === undefined
      ? new FocusShares({
        origin: () => cmemProOrigin(),
        registryPath: join(dirname(this.settingsPath), 'focus-shares.json'),
        credentials: () => {
          const settings = SettingsDefaultsManager.loadFromFile(this.settingsPath, true, { readOnly: true });
          return { token: settings.CLAUDE_MEM_CLOUD_SYNC_TOKEN?.trim() ?? '', userId: settings.CLAUDE_MEM_CLOUD_SYNC_USER_ID?.trim() ?? '' };
        },
      })
      : options.focusShares;
    // Shared pages keep updating with My Work closed (R17.3): the worker publishes them on its own timer.
    this.focusSharePublisher = this.focusShares
      ? new FocusSharePublisher({ shares: this.focusShares, snapshot: () => this.snapshot(0, 'shared-focus'), everyMs: options.focusSharePublishEveryMs })
      : null;
    this.focusSharePublisher?.start();
    if (this.autoRefresh) this.setupScan?.setLocalExtractor(async () => {
      const result = await this.autoRefresh!.runNow({ reason: 'setup-scan' });
      if (result === 'ok' || result === 'idle') return true;
      if (result === 'waiting-for-agent' || result === 'failed') return false;
      throw new Error('Another refresh is running. Try again when it finishes.');
    });
    if (this.hostedIntent) this.setupScan?.setExtractor((prompts, improveProduct) => this.hostedIntent!.run(prompts, improveProduct));
    this.unsubscribeHistoryImport = this.historyImport?.subscribe(state => this.broadcast('setup-import', state)) ?? null;
    // Hosted extraction is awaited inside the scan, so failures stay in the scan screen's error slot.
    this.unsubscribeSetupScan = this.setupScan?.subscribe(state => {
      this.publish();
      this.broadcast('setup-scan', state);
    }) ?? null;
    // Rule rows pulled from another device change what Claude Code should load; rebuild the file then too.
    this.stopRulesSync = onContextInvalidation(({ reason, kind, changes }) => {
      if (kind === 'removal' || reason.startsWith('SyncApply.') || changes?.kinds.some(kind => ['observation', 'summary', 'prompt', 'work_state', 'mutation'].includes(kind))
        || ['storeObservations', 'storeSummary', 'importObservation', 'importSessionSummary'].includes(reason)) {
        this.sourceReader.invalidateActivity(kind === 'removal' || (!changes && reason.startsWith('SyncApply.'))
          || Boolean(changes?.kinds.some(kind => kind === 'work_state' || kind === 'mutation')));
        if (!this.ownerEditActive) this.publish();
      }
      if (!reason.startsWith('SyncApply.')) return;
      // A remote commit can complete an earlier page of reserved rows. Announce it after SQLite commits.
      if (!changes || changes.kinds.includes('mutation') ||
        (changes.kinds.includes('work_state') && (!changes.workStateLists || changes.workStateLists.some(isSharedWorkList)))) {
        this.publishReviewStackChanged();
      }
      // Older senders have no details, so keep their conservative rebuild. Known
      // observation/prompt pages cannot change MY-RULES.md.
      if (!changes || changes.kinds.includes('mutation')
        || (changes.kinds.includes('work_state') && (!changes.workStateLists || changes.workStateLists.includes(MY_RULES_LIST)))) {
        this.scheduleRulesFileRebuild();
      }
    });
    // A MY-RULES.md written by an older version lacks the RULES-AND-TOOLS.md import; bring it up to date
    // once. Never creates the file: only someone who loaded My Rules in Claude Code has one. A file that
    // already has the import costs no read of the saved rules.
    if (this.rulesFileLacksRulesAndToolsImport()) this.scheduleRulesFileRebuild();
  }

  private stopRulesSync: () => void;
  private rulesRebuildScheduled = false;
  private rulesRebuildWanted = false;

  private setupIsActive(): boolean {
    const statePath = this.setupStatePath ?? setupStatePath();
    const scanPhase = this.setupScan?.state().phase ?? 'idle';
    // Waiting for the upload answer is not active: a closed tab never answers, and that must not hold local work
    // paused. The installer's session below still covers a setup in progress; reopening the page rejoins the question.
    if (!['idle', 'done', 'failed', 'awaiting-consent'].includes(scanPhase)) return true;
    try {
      const state = readSetupState(statePath);
      const active = hasPendingInstallSetup(state) || (state.status !== 'complete' && hasActiveSetupSession(statePath));
      this.setupReadExhausted = false;
      return active;
    } catch (error) {
      const code = (error as NodeJS.ErrnoException).code;
      if (code !== 'EMFILE' && code !== 'ENFILE') throw error;
      // The machine is out of file descriptors, not the setup files. Unknown setup keeps local paid work paused.
      // A failed open holds no descriptor, so every tick reads again; the warning is logged once per episode.
      if (!this.setupReadExhausted) logger.warn('WORKER', 'Could not read setup state: no file descriptors are free. Local work stays paused until a read succeeds.', { code });
      this.setupReadExhausted = true;
      return true;
    }
  }

  private scheduleRulesFileRebuild(): void {
    if (this.closed) return;
    if (this.rulesRebuildScheduled) { this.rulesRebuildWanted = true; return; }
    this.rulesRebuildScheduled = true;
    setImmediate(() => {
      if (this.closed) { this.rulesRebuildScheduled = false; return; }
      void this.rebuildRulesFileFromSavedRules()
        .catch(error => logger.warn('HTTP', 'Could not rebuild MY-RULES.md after sync', { error: error instanceof Error ? error.message : String(error) }))
        .finally(() => {
          this.rulesRebuildScheduled = false;
          if (!this.rulesRebuildWanted) return;
          this.rulesRebuildWanted = false;
          this.scheduleRulesFileRebuild();
        });
    });
  }

  /** Rewrites MY-RULES.md when the saved rules render differently. Never from an unreadable source, which reads as no rules. */
  rebuildRulesFileFromSavedRules(): Promise<boolean> {
    return this.inOwnerEditOrder(async () => {
      const view = await this.sourceReader.readRules(this.getLocalDeviceId());
      if (!view.source.connected) return false;
      const markdown = this.renderRulesMarkdown((view.rules ?? []) as SavedRule[]);
      let current: string | null = null;
      try { current = readFileSync(this.rulesFilePath, 'utf8'); }
      catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
      if (current === markdown) return false;
      this.writeRulesFile((view.rules ?? []) as SavedRule[]);
      return true;
    });
  }

  /**
   * Runs a read-check-write of the owner's state after every edit before it. The read now happens off this thread, so
   * without this two edits could both check the same view and both save (A merged into B and B into A), or a read from
   * before a save could rewrite MY-RULES.md after it. A failed edit does not stop the next one.
   */
  private inOwnerEditOrder<T>(edit: () => T | Promise<T>): Promise<T> {
    this.ownerReadFence++;
    const result = this.ownerEditsDone.then(async () => {
      this.ownerEditActive = true;
      try { return await edit(); }
      finally {
        // A bootstrap may start while this asynchronous edit is still reading.
        // Fence that read again when the edit settles, before the next caller
        // can reuse a projection from before the write.
        this.ownerReadFence++;
        this.ownerEditActive = false;
      }
    });
    this.ownerEditsDone = result.catch(() => undefined);
    return result;
  }

  private ownerEdit(handler: (req: Request, res: Response) => Promise<void>) {
    return (req: Request, res: Response) => this.inOwnerEditOrder(() => handler(req, res));
  }

  setupRoutes(app: express.Application): void {
    const setupTelemetry = this.setupTelemetry ?? createSetupTelemetry(this.setupStatePath);
    this.setupTelemetry = setupTelemetry;
    app.get('/api/setup/telemetry', workViewLoopbackOnly, requireWorkViewReadOrigin, this.wrapHandler((_req, res) => {
      res.setHeader('Cache-Control', 'no-store');
      res.json({ context: setupTelemetry.context() });
    }));
    app.post('/api/setup/telemetry', workViewLoopbackOnly, requireWorkViewOrigin, this.wrapHandler((req, res) => {
      res.setHeader('Cache-Control', 'no-store');
      // Opt-out is intentionally indistinguishable from an accepted event to the UI.
      setupTelemetry.receive(req.body);
      res.status(204).end();
    }));
    // The owner-goals script already writes through this loopback worker. Its
    // story update commits shared facts before the compatibility file changes.
    app.post('/api/work-view/shared-ledger', workViewLoopbackOnly, requireWorkViewOrigin,
      validateBody(z.object({ file: z.literal('story.json'), value: z.record(z.string(), z.unknown()) }).strict()),
      this.wrapHandler(this.ownerEdit(async (req, res) => {
        if (!this.saveSharedLedgerFile) { res.status(503).json({ error: 'Shared work persistence is unavailable.' }); return; }
        this.saveSharedLedgerFile(req.body.file, req.body.value);
        res.json({ saved: true });
      })));
    app.post('/api/work-view/activity', workViewLoopbackOnly, requireWorkViewOrigin,
      validateBody(z.object({ viewerId: z.string().min(1).max(128), visible: z.boolean(), sequence: z.number().int().min(0).max(Number.MAX_SAFE_INTEGER) }).strict()),
      this.wrapHandler((req, res) => {
        if (!this.updateViewerActivity) { res.status(204).end(); return; }
        if (!this.updateViewerActivity(req.body.viewerId, req.body.visible, req.body.sequence)) { res.status(429).json({ error: 'Too many active viewer leases.' }); return; }
        res.status(204).end();
      }));
    app.get('/api/work-view', workViewLoopbackOnly, this.wrapHandler(async (_req, res) => {
      res.setHeader('Cache-Control', 'no-store');
      const snapshot = await this.snapshot(0, 'http');
      const startedAt = process.hrtime.bigint();
      res.json(snapshot);
      // Measure the existing serialization/send, not a second JSON pass for diagnostics.
      const bytes = Number(res.getHeader('Content-Length'));
      this.lastResponseDiagnostic = { transport: 'http', writeMs: boundedDurationMs(Number(process.hrtime.bigint() - startedAt) / 1e6),
        bytes: Number.isSafeInteger(bytes) && bytes >= 0 ? bytes : 0 };
    }));
    app.get('/api/work-view/overview', workViewLoopbackOnly, this.wrapHandler(async (_req, res) => {
      res.setHeader('Cache-Control', 'no-store');
      res.json(workViewOverview(await this.readWorkSnapshot()));
    }));
    app.get('/api/work-view/requests/:id', workViewLoopbackOnly, this.wrapHandler(async (req, res) => {
      res.setHeader('Cache-Control', 'no-store');
      const id = this.toStringParam(req.params.id);
      if (Object.keys(req.query).some(key => key !== 'fingerprint' && key !== 'revision')
        || (req.query.fingerprint !== undefined && typeof req.query.fingerprint !== 'string')
        || (req.query.revision !== undefined && typeof req.query.revision !== 'string')) {
        this.badRequest(res, 'Invalid request fingerprint or revision.'); return;
      }
      const fingerprint = req.query.fingerprint as string | undefined;
      const revision = req.query.revision as string | undefined;
      if (!id || id.length > 2048 || (fingerprint && !/^(?:[a-f0-9]{24}|[a-f0-9]{64})$/.test(fingerprint))) {
        this.badRequest(res, 'Invalid request identity or fingerprint.'); return;
      }
      const snapshot = await this.snapshot(0, 'request-detail');
      const detail = workViewRequestDetail(snapshot, id);
      if (!detail) { this.notFound(res, 'That request is no longer available.'); return; }
      if ((fingerprint && detail.request.detailRevision !== fingerprint && detail.request.contentFingerprint !== fingerprint) || (revision && detail.revision !== revision)) {
        res.status(409).json({ error: 'That request changed. Read its current version.', overview: workViewOverview(snapshot) }); return;
      }
      res.json(detail);
    }));
    app.get('/api/work-view/diagnostics', workViewLoopbackOnly, this.wrapHandler((_req, res) => {
      res.setHeader('Cache-Control', 'no-store');
      res.json({ reader: this.sourceReader.getDiagnostics(), snapshot: this.lastSnapshotDiagnostic, response: this.lastResponseDiagnostic });
    }));
    app.get('/api/work-view/events', workViewLoopbackOnly, this.wrapHandler(this.events));
    app.get('/api/work-view/evidence/:kind/:id', workViewLoopbackOnly, this.wrapHandler((req, res) => {
      const id = this.parseIntParam(req, res, 'id');
      if (id === null) return;
      const evidence = this.source.evidence(this.toStringParam(req.params.kind), id);
      res.setHeader('Cache-Control', 'no-store');
      if (!evidence) { this.notFound(res, 'Recorded evidence not found.'); return; }
      res.json(evidence);
    }));
    // A recorded work-product file, found through the record that names it.
    // Pages and images render sandboxed so they cannot act as the viewer.
    // It reads real files, so another local page (which worker CORS lets read responses) is refused:
    // a cross-origin fetch carries its Origin, while the viewer opens files from its own origin.
    app.get('/api/work-view/file/:kind/:id/:index', workViewLoopbackOnly, this.wrapHandler((req, res) => {
      if (req.get('sec-fetch-site') === 'cross-site' || isForeignLoopbackBrowserWrite(req)) {
        res.status(403).json({ error: 'Recorded files open only from the local work viewer.' });
        return;
      }
      const id = this.parseIntParam(req, res, 'id');
      if (id === null) return;
      const index = this.parseIntParam(req, res, 'index');
      if (index === null) return;
      const path = this.source.file(this.toStringParam(req.params.kind), id, index);
      if (!path) { this.notFound(res, 'Recorded file not found.'); return; }
      res.setHeader('Cache-Control', 'no-store');
      res.setHeader('X-Content-Type-Options', 'nosniff');
      res.setHeader('Content-Security-Policy', 'sandbox allow-scripts');
      // A token-bearing navigation downloads the original. Its scripts must never see the viewer credential.
      if (req.query.token) res.attachment(basename(path));
      res.type(FILE_CONTENT_TYPES[extname(path).toLowerCase()] ?? 'application/octet-stream');
      res.sendFile(path, { dotfiles: 'allow' });
    }));
    // A deliverable the Stop hook copied into the project bucket (~/.claude-mem/deliverables/<project>/), found
    // through its record; the filename in the URL must be the copy's own, so a link never names another file.
    // Same sandbox and same-origin rules as a recorded file. Text and markdown open inline as plain text.
    app.get('/api/deliverables/:id/:filename', workViewLoopbackOnly, this.wrapHandler((req, res) => {
      if (req.get('sec-fetch-site') === 'cross-site' || isForeignLoopbackBrowserWrite(req)) {
        res.status(403).json({ error: 'Deliverables open only from the local work viewer.' });
        return;
      }
      const id = this.parseIntParam(req, res, 'id');
      if (id === null) return;
      const deliverable = this.source.deliverable(id);
      if (!deliverable || basename(deliverable.path) !== this.toStringParam(req.params.filename) || !safeReviewBucketPath(deliverable.path)) {
        this.notFound(res, 'Deliverable not found.');
        return;
      }
      res.setHeader('Cache-Control', 'no-store');
      res.setHeader('X-Content-Type-Options', 'nosniff');
      res.setHeader('Content-Security-Policy', 'sandbox allow-scripts');
      if (req.query.token) res.attachment(basename(deliverable.path));
      res.type(DELIVERABLE_CONTENT_TYPES[extname(deliverable.path).toLowerCase()] ?? 'application/octet-stream');
      res.sendFile(deliverable.path, { dotfiles: 'allow' });
    }));
    app.post('/api/work-view/review', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(reviewBody), this.wrapHandler(this.ownerEdit(this.review)));
    app.post('/api/work-view/rules', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(ruleBody), this.wrapHandler(this.ownerEdit(this.saveRule)));
    app.post('/api/work-view/focus', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(focusBody), this.wrapHandler(this.ownerEdit(this.saveFocus)));
    // System-written suggestions only: every row is decided_by system, status suggested, under a new id, so
    // nothing posted here can stand in for an owner decision or change an area the owner decided.
    app.post('/api/work-view/focus/owner-goals', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(ownerGoalsFocusBody), this.wrapHandler(this.ownerEdit(this.suggestOwnerGoalsAreas)));
    // The focus step's answer (request-groups.mjs record-focus run by the request-progress skill): router placements
    // and suggested areas only, so nothing posted here can stand in for an owner decision.
    app.post('/api/work-view/focus/router', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(routerFocusBody), this.wrapHandler(this.saveRouterFocus));
    // The one-time seed and its undo: system rows only (suggested seed areas, router placements, system withdrawals and
    // cleared router placements), so nothing posted here can stand in for or change an owner decision.
    app.post('/api/work-view/focus/seed', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(seedFocusBody), this.wrapHandler(this.ownerEdit(this.saveSeedFocus)));
    app.post('/api/work-view/agents/mark-finished', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(markAgentFinishedBody), this.wrapHandler(this.ownerEdit(this.markAgentFinished)));
    app.post('/api/work-view/suggestions', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(suggestionBody), this.wrapHandler(this.ownerEdit(this.answerSuggestion)));
    app.post('/api/work-view/rules/load-in-claude-code', workViewLoopbackOnly, requireWorkViewOrigin, this.wrapHandler(this.ownerEdit(this.loadRulesInClaudeCode)));
    app.post('/api/work-view/refresh', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(z.object({}).strict()), this.wrapHandler(this.startManualRefresh));
    // Continuing a conversation from My Work. These read, start and stop a real turn in the owner's own conversation,
    // and on the owner's computer that turn can run with full tool access (their own permission mode). So each keeps
    // every work-view guard: this computer only and through no proxy, the viewer's own page, and JSON for the writes.
    // None is in OPEN_ROUTES (shared/local-api-token.ts), so local API token mode (#5141) covers them as well.
    app.get('/api/work-view/conversations/continuation', workViewLoopbackOnly, requireWorkViewReadOrigin, this.wrapHandler(this.readConversationContinuation));
    app.post('/api/work-view/conversations/continue', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(continueConversationBody), this.wrapHandler(this.continueConversation));
    app.post('/api/work-view/conversations/stop', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(conversationRef), this.wrapHandler(this.stopConversationContinuation));
    // Opens the owner's conversation in its tool's app on this computer's screen. It starts no turn, and keeps the same guards.
    app.post('/api/work-view/conversations/open-in-app', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(openInAppClickBody), this.wrapHandler(this.openConversationInApp));
    app.post('/api/work-view/conversations/open-in-app/stop', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(openInAppClickBody), this.wrapHandler(this.stopOpeningConversationInApp));
    app.post('/api/work-view/usage', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(usageBody), this.wrapHandler(this.recordUsage));
    // Shared focus pages: this worker signs the cmem.ai calls, so the sync credential never reaches the page.
    app.get('/api/work-view/focus/shares', workViewLoopbackOnly, requireWorkViewReadOrigin, this.wrapHandler(this.listFocusShares));
    app.post('/api/work-view/focus/share', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(focusShareAreaBody), this.wrapHandler(this.publishFocusShare));
    app.post('/api/work-view/focus/share/stop', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(focusShareAreaBody), this.wrapHandler(this.stopFocusShare));
    app.get('/api/work-view/focus/share/comments', workViewLoopbackOnly, requireWorkViewReadOrigin, this.wrapHandler(this.focusShareComments));
    app.post('/api/work-view/focus/share/comments', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(focusShareCommentBody), this.wrapHandler(this.commentOnFocusShare));
    app.get('/api/setup/scan', workViewLoopbackOnly, this.wrapHandler((_req, res) => {
      if (!this.setupScan) { res.status(503).json({ error: 'The prompt scan is unavailable on this worker.' }); return; }
      res.setHeader('Cache-Control', 'no-store');
      res.json(this.setupScan.state());
    }));
    app.post('/api/setup/scan', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupScanBody), this.wrapHandler((req, res) => {
      if (!this.setupScan) { res.status(503).json({ error: 'The prompt scan is unavailable on this worker.' }); return; }
      res.setHeader('Cache-Control', 'no-store');
      res.status(202).json(this.setupScan.start(req.body.improveProduct));
    }));
    // The page's Continue at 98%: ends the wait for the owner's own AI at once; the refresh keeps running.
    app.post('/api/setup/scan/continue', workViewLoopbackOnly, requireWorkViewOrigin, this.wrapHandler(async (_req, res) => {
      if (!this.setupScan) { res.status(503).json({ error: 'The prompt scan is unavailable on this worker.' }); return; }
      res.setHeader('Cache-Control', 'no-store');
      res.json(await this.setupScan.continueNow());
    }));
    app.post('/api/setup/scan/consent', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupScanConsentBody), this.wrapHandler(async (req, res) => {
      if (!this.setupScan) { res.status(503).json({ error: 'The prompt scan is unavailable on this worker.' }); return; }
      res.setHeader('Cache-Control', 'no-store');
      try { res.json(await this.setupScan.confirmUpload(req.body.upload, req.body.agents, req.body.improveProduct)); } catch (error) {
        res.status(409).json({ error: error instanceof Error ? error.message : String(error) });
      }
    }));
    app.post('/api/setup/scan/preferences', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupScanPreferencesBody), this.wrapHandler((req, res) => {
      if (!this.setupScan) { res.status(503).json({ error: 'The prompt scan is unavailable on this worker.' }); return; }
      if (this.setupScan.state().phase === 'uploading') { res.status(409).json({ error: 'The setup upload has already started.' }); return; }
      res.setHeader('Cache-Control', 'no-store');
      res.json(this.setupScan.setImproveProduct(req.body.improveProduct));
    }));
    // The funnel hand-off. The page posts the storage choice and the finish; the installer polls the state and
    // reports what it did with the choice. Node's fetch sends no Origin, so the installer passes the same guard.
    app.get('/api/setup/state', workViewLoopbackOnly, this.wrapHandler((_req, res) => {
      res.setHeader('Cache-Control', 'no-store');
      res.json(this.setupStateView());
    }));
    app.post('/api/setup/choice', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupChoiceBody), this.wrapHandler(this.saveSetupChoice));
    app.post('/api/setup/install', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupInstallSelectionBody), this.wrapHandler(this.configureSetupInstall));
    app.post('/api/setup/install/installer', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupInstallReportBody), this.wrapHandler(this.reportSetupInstall));
    app.post('/api/setup/resume', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupResumeBody), this.wrapHandler(async (req, res) => {
      const state = readSetupState(this.setupStatePath);
      if (state.status !== 'pending' || !state.install) { res.status(409).json({ error: 'There is no pending installation to resume.' }); return; }
      try { await this.resumeSetup(req.body.retry); } catch (error) {
        res.status(503).json({ error: error instanceof Error ? error.message : String(error) }); return;
      }
      res.status(202).json(this.setupStateView());
    }));
    app.post('/api/setup/installer', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupInstallerBody), this.wrapHandler(this.saveInstallerStage));
    app.post('/api/setup/checkout', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupCheckoutBody), this.wrapHandler(this.requestCheckout));
    // The code shown beside "Waiting for this device to connect…": the setup page's own origin only (redactSetupCheckout).
    app.get('/api/setup/checkout/device-code', workViewLoopbackOnly, requireSetupPageReadOrigin, this.wrapHandler((_req, res) => {
      const checkout = readSetupState(this.setupStatePath).checkout;
      res.setHeader('Cache-Control', 'no-store');
      if (checkout?.state !== 'waiting' || !checkout.deviceCode) { res.status(404).json({ error: 'No device code is waiting.' }); return; }
      res.json(checkout.deviceCode);
    }));
    app.post('/api/setup/import', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupImportBody), this.wrapHandler(this.startHistoryImport));
    // Finish Setup's "Stop import": the same guards. It only ends a run; the person's own POST above starts one again.
    app.post('/api/setup/import/stop', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupImportBody), this.wrapHandler(this.stopHistoryImport));
    app.post('/api/setup/complete', workViewLoopbackOnly, requireWorkViewOrigin, validateBody(setupCompleteBody), this.wrapHandler(this.completeSetup));
  }

  private setupStateView(state: SetupStateFile = readSetupState(this.setupStatePath)): SetupStateView {
    const settings = SettingsDefaultsManager.loadFromFile(this.settingsPath, true, { readOnly: true }) as unknown as Record<string, unknown>;
    const historyImport = this.historyImport?.state() ?? null;
    const { history, memorySynced } = this.localMemoryFacts();
    return { ...state, install: redactSetupInstall(state.install), checkout: redactSetupCheckout(state.checkout), alreadyPro: hasProConfiguration(settings), hostedObserver: hasHostedObserver(settings), history, memorySynced, historyImport, backgroundWorkRunning: this.backgroundWorkRunning({ countScanWaitingForUploadAnswer: true, failOnUnreadableRefresh: false }) };
  }

  /**
   * Long work this worker is doing now that a restart would kill (SetupStateView.backgroundWorkRunning): the
   * extraction run and the history import. GET /api/setup/state reports it, and the installer waits for it before
   * any restart (browser-setup.ts waitForBackgroundWorkToFinish); the auto-updater counts it as busy
   * (worker-service.ts runningBackgroundJobNames). Reads refresh.json.
   *
   * failOnUnreadableRefresh: what a refresh.json that cannot be read means. The auto-updater throws, so its quiet
   * check counts the unknown run as busy. The setup state view reads it as no run: a throw there failed every setup
   * state request, the installer's and My Work's, until the file was replaced. The next scheduled check replaces it.
   *
   * countScanWaitingForUploadAnswer: whether a setup scan held at the "upload your prompts?" question
   * ('awaiting-consent') counts as the extraction run. The installer's view counts it, as it always has. The
   * auto-updater does not: the held scan runs nothing and waits in memory with no time limit (a closed tab never
   * answers), and a restart drops it anyway, so counting it would read busy until the worker restarts.
   * setupIsActive leaves that phase out for the same reason.
   */
  backgroundWorkRunning({ countScanWaitingForUploadAnswer, failOnUnreadableRefresh }: { countScanWaitingForUploadAnswer: boolean; failOnUnreadableRefresh: boolean }): NonNullable<SetupStateView['backgroundWorkRunning']> {
    const backgroundWorkRunning: NonNullable<SetupStateView['backgroundWorkRunning']> = [];
    const refresh = failOnUnreadableRefresh ? this.autoRefresh?.status() ?? null : this.readableRefreshStatus();
    // A 'running' record whose process is gone was cut off; only a live run counts.
    // The setup scan counts too: when it finishes it starts the extraction run.
    const scanPhase = this.setupScan?.state().phase ?? 'idle';
    const scanning = !['idle', 'done', 'failed'].includes(scanPhase)
      && (countScanWaitingForUploadAnswer || scanPhase !== 'awaiting-consent');
    if (scanning || (refresh?.state === 'running' && processIsAlive(Number((refresh as { pid?: number }).pid)))) backgroundWorkRunning.push('extraction');
    const importState = this.historyImport?.state().state;
    if (importState && historyImportRunning(importState)) backgroundWorkRunning.push('history-import');
    return backgroundWorkRunning;
  }

  /**
   * My Work's card-wording requests running now (HumanCopyWorker): chat
   * completions of up to 90 s each (human-copy-worker.mjs sends with
   * AbortSignal.timeout(90000)), whose spend is lost when the worker stops
   * first. The auto-updater counts them as busy (worker-service.ts
   * runningBackgroundJobNames). In memory; no I/O.
   */
  cardWordingRequestsRunning(): number {
    return this.humanCopy?.status().generating ?? 0;
  }

  /** This machine's own observations (Finish Setup's import offer) and whether the sync hub has ever acknowledged one of
   * its memories (Work View's hosted offer, ownMemorySynced), from one read-only open of the database. A missing
   * database is a new install: nothing to import, nothing synced. */
  private localMemoryFacts(): { history: { projects: number; sessions: number; observations: number } | null; memorySynced: boolean } {
    if (!existsSync(this.databasePath)) return { history: { projects: 0, sessions: 0, observations: 0 }, memorySynced: false };
    const database = new Database(this.databasePath, { readonly: true, create: false });
    try { return { history: readHistoryCounts(database), memorySynced: ownMemorySynced(database) }; }
    finally { database.close(); }
  }

  /** Finish Setup's "Import full history" (On The Go only, after checkout). */
  private startHistoryImport = (_req: Request, res: Response): void => {
    if (!this.historyImport) { res.status(503).json({ error: 'Bringing your memory is unavailable on this worker.' }); return; }
    const state = readSetupState(this.setupStatePath);
    if (state.choice?.branch !== 'go') { res.status(409).json({ error: 'Bringing your memory to CMEM is part of On The Go.' }); return; }
    try {
      const started = this.historyImport.start();
      res.setHeader('Cache-Control', 'no-store');
      res.status(202).json(started);
    } catch (error) {
      res.status(409).json({ error: error instanceof Error ? error.message : String(error) });
    }
  };

  /**
   * Finish Setup's "Stop import" (HistoryImport.stop): the upload ends at once and no request to cmem.ai starts after
   * this answers. 200 with the stopped run (what was sent, and the batch whose answer never came); 409 with a sentence
   * when no upload is running. What already reached cmem.ai stays there; nothing is deleted on either side.
   */
  private stopHistoryImport = (_req: Request, res: Response): void => {
    if (!this.historyImport) { res.status(503).json({ error: 'Bringing your memory is unavailable on this worker.' }); return; }
    const stopped = this.historyImport.stop();
    if (!stopped) { res.status(409).json({ error: 'No import is running, so there is nothing to stop.' }); return; }
    res.setHeader('Cache-Control', 'no-store');
    res.json(stopped);
  };

  private saveSetupState(state: SetupStateFile, res: Response): void {
    writeSetupState(state, this.setupStatePath);
    const view = this.setupStateView(state);
    this.broadcast('setup-state', view);
    res.setHeader('Cache-Control', 'no-store');
    res.json(view);
  }

  /** Only choices from this installer's catalog can become filesystem actions. */
  private configureSetupInstall = async (req: Request, res: Response): Promise<void> => {
    const selection = req.body as z.infer<typeof setupInstallSelectionBody>;
    const state = readSetupState(this.setupStatePath);
    const install = state.install;
    if (state.status === 'complete' || !install || (install.status !== 'waiting' && install.status !== 'failed')) {
      res.status(409).json({ error: 'The installer is not waiting for installation choices.' }); return;
    }
    const unique = (values: string[]) => new Set(values).size === values.length;
    if (!unique(selection.agents) || !unique(selection.skills)
      || selection.agents.some(id => !install.catalog.agents.some(agent => agent.id === id))
      || selection.skills.some(id => !install.catalog.skills.some(skill => skill.id === id))
      || install.catalog.skills.some(skill => skill.required && !selection.skills.includes(skill.id))) {
      res.status(400).json({ error: 'Choose agents and skills offered by this installer, including required skills.' }); return;
    }
    if (install.catalog.telemetryLocked && selection.telemetryEnabled !== install.catalog.defaults.telemetryEnabled) {
      res.status(400).json({ error: 'Usage sharing is controlled by your existing preference.' }); return;
    }
    if (install.catalog.skillsLocked && (selection.skills.length !== install.catalog.defaults.skills.length
      || selection.skills.some(id => !install.catalog.defaults.skills.includes(id)))) {
      res.status(400).json({ error: 'Skill selection is controlled by your environment.' }); return;
    }
    if (install.catalog.emailEnabled === false && (selection.email || selection.note)) {
      res.status(400).json({ error: 'Email signup is unavailable for this installation.' }); return;
    }
    // Served views drop the email, so a retry that still wants the stored signup says so instead of resending it.
    const { emailProvided, ...chosen } = selection;
    const keepStoredEmail = emailProvided === true && !chosen.email && install.status === 'failed' && !!install.selection?.email;
    const stored = keepStoredEmail ? { ...chosen, email: install.selection!.email, note: chosen.note ?? install.selection!.note } : chosen;
    writeSetupState({ ...state, install: { ...install, selection: stored, status: 'installing', error: null } }, this.setupStatePath);
    try { await this.resumeSetup(true); } catch (error) {
      res.status(503).json({ error: error instanceof Error ? error.message : String(error) }); return;
    }
    this.saveSetupState(readSetupState(this.setupStatePath), res);
  };

  private reportSetupInstall = (req: Request, res: Response): void => {
    const { status, catalog, error } = req.body as z.infer<typeof setupInstallReportBody>;
    const state = readSetupState(this.setupStatePath);
    if (state.status === 'complete') { res.status(409).json({ error: 'Setup is already complete.' }); return; }
    if (status === 'waiting') {
      if (!catalog) { res.status(400).json({ error: 'Installation choices require a catalog.' }); return; }
      this.saveSetupState({ ...state, install: { status, catalog, selection: null, error: null } }, res);
      return;
    }
    if (!state.install?.selection || state.install.status !== 'installing' || status === 'installing') {
      res.status(409).json({ error: 'Choose installation options before reporting the result.' }); return;
    }
    // The signup has been applied once the install is ready; a failed run keeps it on disk for the explicit retry.
    const { email: _email, note: _note, ...withoutSignup } = state.install.selection;
    const selection = status === 'ready' ? withoutSignup : state.install.selection;
    this.saveSetupState({ ...state, install: { ...state.install, selection, status, error: error ?? null } }, res);
  };

  /** The choice can change until the installer acts on it; after that the installer holds the result. */
  private saveSetupChoice = (req: Request, res: Response): void => {
    const { branch, memoryBilling, cloudSyncOffer, shareWorkView, openCheckout } = req.body as z.infer<typeof setupChoiceBody>;
    const offer = setupCloudSyncOffer({ branch, memoryBilling, cloudSyncOffer });
    const state = readSetupState(this.setupStatePath);
    if (state.status === 'complete') { res.status(409).json({ error: 'Setup is already complete.', state: this.setupStateView(state) }); return; }
    if (state.install && state.install.status !== 'ready') { res.status(409).json({ error: 'Finish installing your agents before choosing memory storage.' }); return; }
    const applied = state.installer && state.installer.stage !== 'waiting-for-choice' && state.installer.stage !== 'failed';
    // The first checkout request belongs to the plan saved in this same write. A repeat
    // or a second browser tab cannot replace that plan while the terminal sets up pairing.
    const sameChoice = state.choice?.branch === branch && state.choice.memoryBilling === memoryBilling
      && setupCloudSyncOffer(state.choice) === offer && state.choice.shareWorkView === shareWorkView;
    const locked = applied || (!!state.checkout && state.installer?.stage !== 'failed');
    if (locked && !sameChoice) {
      res.status(409).json({ error: 'The installer already applied the earlier choice.', state: this.setupStateView(state) });
      return;
    }
    const now = new Date().toISOString();
    // Older installers can have an applied choice with no checkout request yet.
    let checkout = sameChoice ? state.checkout : null;
    if (openCheckout && !checkout && !this.setupStateView(state).alreadyPro) {
      checkout = { state: 'requested', requests: 1, error: null, updatedAt: now };
    }
    if (locked && checkout === state.checkout) { res.setHeader('Cache-Control', 'no-store'); res.json(this.setupStateView(state)); return; }
    const choice = locked ? state.choice : {
      branch, memoryBilling, chosenAt: now,
      ...(shareWorkView ? { shareWorkView } : {}),
      ...(offer ? { cloudSyncOffer: offer } : {}),
    };
    this.saveSetupState({ ...state, choice, checkout }, res);
  };

  private saveInstallerStage = (req: Request, res: Response): void => {
    const { stage, error, checkout, deviceCode } = req.body as z.infer<typeof setupInstallerBody>;
    const state = readSetupState(this.setupStatePath);
    const now = new Date().toISOString();
    // A new installer run starts a fresh hand-off: a choice or checkout left by an earlier, interrupted run is not applied.
    const fresh = stage === 'waiting-for-choice';
    const choice = fresh ? null : state.choice;
    const checkoutState = fresh ? null : checkout
      // Each report replaces the code: only a waiting checkout carries one (the body check), so paid and failed clear it.
      ? { state: checkout, requests: state.checkout?.requests ?? 0, error: checkout === 'failed' ? error ?? null : null, updatedAt: now, ...(deviceCode ? { deviceCode } : {}) }
      : state.checkout;
    this.saveSetupState({ ...state, choice, checkout: checkoutState, installer: { stage, error: checkout ? state.installer?.error ?? null : error ?? null, updatedAt: now } }, res);
  };

  /** The page's Continue on Checkout: the installer holding the On The Go pairing opens the checkout (again, on a repeat). */
  private requestCheckout = (_req: Request, res: Response): void => {
    const state = readSetupState(this.setupStatePath);
    if (state.checkout?.state === 'paid') { res.setHeader('Cache-Control', 'no-store'); res.json(this.setupStateView(state)); return; }
    if (state.choice?.branch !== 'go' || state.installer?.stage !== 'go-awaiting-checkout') {
      res.status(409).json({ error: 'Checkout opens once the terminal has started your CMEM Pro sign-in.', state: this.setupStateView(state) });
      return;
    }
    const requests = (state.checkout?.requests ?? 0) + 1;
    this.saveSetupState({ ...state, checkout: { state: 'requested', requests, error: null, updatedAt: new Date().toISOString() } }, res);
  };

  private completeSetup = (req: Request, res: Response): void => {
    const { tools } = req.body as z.infer<typeof setupCompleteBody>;
    const state = readSetupState(this.setupStatePath);
    if (state.status === 'complete') { res.setHeader('Cache-Control', 'no-store'); res.json(this.setupStateView(state)); return; }
    if (state.install && state.install.status !== 'ready') { res.status(409).json({ error: 'Finish installing your agents before completing setup.' }); return; }
    if (!state.choice) { res.status(409).json({ error: 'Choose where to keep your memory before finishing setup.', state: this.setupStateView(state) }); return; }
    const providerReady = state.choice.branch === 'home'
      ? state.installer?.stage === 'home-ready'
      : state.installer?.stage === 'go-kept-existing-pro'
        || (state.installer?.stage === 'go-awaiting-checkout' && state.checkout?.state === 'paid');
    if (!providerReady) {
      res.status(409).json({ error: 'Finish setting up your memory provider before finishing setup.', state: this.setupStateView(state) });
      return;
    }
    const captureCompletion = this.setupTelemetry?.prepareCompletion(req.body.telemetry);
    this.saveSetupState({ ...state, status: 'complete', tools: tools ?? state.tools, completedAt: new Date().toISOString() }, res);
    // Synchronous enqueue after the successful write, before the installer can
    // remove its session marker or restart this worker.
    captureCompletion?.();
  };

  private broadcast(event: string, value: unknown): void {
    if (this.closed) return;
    for (const client of this.clients) this.send(client, event, value);
  }

  /**
   * The view a rules or focus-area write checks against before it writes. Rules and focus areas come only from
   * work-state rows and the ledger, which every read checks fresh, so the activity signature may be reused inside
   * ACTIVITY_REFRESH_WINDOW_MS: with live sessions committing every few seconds, a fresh read here rebuilt the whole
   * view once before the write and again after it (#5124). The view each route answers with is still read fresh.
   * Not for checks against requests, agents or evidence (move-request, mark-finished, review): those read fresh.
   */
  private ownerStateSnapshot(): Promise<WorkViewSnapshot> {
    return this.snapshot(ACTIVITY_REFRESH_WINDOW_MS, 'owner-state');
  }

  /**
   * The event stream's tick and heartbeat, the only reads that may share a result: nothing they send promises to show
   * a write. Every other read posts its own request, so it starts after any write that came before it (the reader
   * answers strictly in order). That order also means a tick's older read can never arrive after a newer publish.
   */
  private backgroundSnapshot(): Promise<WorkViewSnapshot> {
    this.pendingBackgroundSnapshot ??= this.snapshot(ACTIVITY_REFRESH_WINDOW_MS, 'background')
      .finally(() => { this.pendingBackgroundSnapshot = null; });
    return this.pendingBackgroundSnapshot;
  }

  /** Only simultaneous read-only bootstrap callers share this work. Owner edits
   * and every committed context change fence it off; write responses always use
   * their own fresh ordered read. No completed snapshot is cached here. */
  readWorkSnapshot(): Promise<WorkViewSnapshot> {
    // File-writing agents and the copy worker can finish outside an HTTP edit.
    // Their cheap signatures participate without scanning the source database.
    const fence = JSON.stringify([currentContextGeneration(), this.ownerReadFence,
      this.ledgerDir ? ledgerSignature(this.ledgerDir) : null, this.humanCopy?.revision ?? 0]);
    if (this.pendingBootstrap?.fence === fence) return this.pendingBootstrap.promise;
    const promise = this.snapshot(0, 'overview');
    this.pendingBootstrap = { fence, promise };
    void promise.finally(() => {
      if (this.pendingBootstrap?.promise === promise) this.pendingBootstrap = null;
    }).catch(() => undefined);
    return promise;
  }

  private async snapshot(maxActivityAgeMs = 0, purpose: WorkViewReadPurpose = 'snapshot'): Promise<WorkViewSnapshot> {
    const read = await this.sourceReader.read(maxActivityAgeMs, this.getLocalDeviceId(), purpose);
    const startedAt = process.hrtime.bigint();
    this.source.agentRevision = read.agentRevision;
    // A coalesced bootstrap may have multiple consumers. Enrichment must never
    // mutate the reader's cached or shared raw projection.
    const snapshot = structuredClone(read.agentView);
    const sharedEnabled = this.getSharedStatus?.().enabled === true;
    for (const request of snapshot.humanRequests) {
      const legacyFingerprint = contentFingerprint(request);
      request.legacyContentFingerprint = legacyFingerprint;
      request.contentFingerprint = request.sharedReviewDescriptor?.complete ? request.sharedReviewDescriptor.fingerprint : legacyFingerprint;
      const shared = request.sharedReview;
      if (sharedEnabled) {
        request.review = shared ?? { checked: false, checkedAtEpoch: null, actor: null, available: false, mode: 'shared' };
      } else {
        let override = this.reviews.readOverride(request.id, request.contentFingerprint, shared?.hash ?? null);
        let legacy = this.reviews.read(request.id, legacyFingerprint);
        if (request.sourceProductHash && (!legacy.checked || !override)) {
          const prior = priorContentFingerprint(request);
          const priorReview = this.reviews.read(request.id, prior);
          const priorOverride = this.reviews.readOverride(request.id, prior, shared?.hash ?? null);
          const productsAt = request.sourceProductRecordedAtEpoch;
          // Products can arrive after the old check but before the first
          // upgraded read. Unknown or equal timestamps cannot prove order.
          const productsPredateChecks = [priorReview, priorOverride].every(review => !review?.checked ||
            (typeof productsAt === 'number' && Number.isFinite(productsAt) &&
              typeof review.checkedAtEpoch === 'number' && productsAt < review.checkedAtEpoch));
          if ((priorReview.checked || priorOverride) && productsPredateChecks) {
            this.reviews.adoptFingerprint(request.id, prior, legacyFingerprint);
            legacy = this.reviews.read(request.id, legacyFingerprint);
            override = this.reviews.readOverride(request.id, request.contentFingerprint, shared?.hash ?? null);
          }
        }
        const accepted = override ?? (shared?.matches || shared?.conflict ? shared : legacy);
        request.review = { ...accepted, mode: 'local', legacy: !override && accepted === legacy && legacy.checked };
      }
    }
    const reviewRevision = createHash('sha256').update(JSON.stringify(snapshot.humanRequests.map(request => [request.id, request.contentFingerprint, request.review]))).digest('hex').slice(0, 16);
    // Generated wording is display only: it never changes what was reviewed.
    const view = this.humanCopy ? this.humanCopy.observe(snapshot) as WorkViewSnapshot : snapshot;
    const result = { ...view,
      sharedState: { ...((view.sharedState && typeof view.sharedState === 'object') ? view.sharedState : {}),
        ...(this.getSharedStatus?.() ?? { enabled: false }) },
      ...(this.saveSharedLedgerFile ? { sharedPersistenceVersion: 1 } : {}),
      rulesFile: this.rulesFileStatus(snapshot), setup: this.setupView(view), revision: `${snapshot.revision}:${reviewRevision}:${this.humanCopy?.revision ?? 0}` };
    this.lastSnapshotDiagnostic = { purpose, postprocessMs: boundedDurationMs(Number(process.hrtime.bigint() - startedAt) / 1e6) };
    return this.getSyncStatus ? { ...result, sync: this.getSyncStatus() } : result;
  }

  /** refresh.json, or null when its bytes are not JSON, so a view shows no run; the next scheduled check replaces the file. */
  private readableRefreshStatus() {
    try { return this.autoRefresh?.status() ?? null; }
    catch (error) { if (error instanceof SyntaxError) return null; throw error; }
  }

  /**
   * The funnel's view of setup: the scan and the extraction run (the automatic refresh's status). `step` is what is
   * being extracted now, read from what has landed: goals first, then work (request groups), then rules (the story).
   */
  private setupView(snapshot: WorkViewSnapshot) {
    const scan = this.setupScan?.state() ?? null;
    const hosted = this.hostedIntent?.state();
    const ranHosted = scan && (scan.uploadedAgents !== null || (scan.done && scan.agents.every(agent => !agent.prompts)));
    if (ranHosted && hosted && hosted.state !== 'idle') return { scan, work: this.setupWork(), extraction: hosted };
    const refresh = this.readableRefreshStatus();
    const view = snapshot as unknown as { goals?: unknown; story?: { groups?: number; story?: unknown } | null };
    const step = refresh?.state !== 'running' ? null : !view.goals ? 'goals' : !view.story?.groups ? 'work' : 'rules';
    return {
      scan,
      work: this.setupWork(),
      extraction: refresh ? {
        state: refresh.state, reason: refresh.reason ?? null, step, agent: refresh.agent ?? null,
        startedAt: refresh.startedAt ?? null, finishedAt: refresh.finishedAt ?? null, error: refresh.error ?? null,
      } : null,
    };
  }

  /**
   * The newest overall requests in the ledger, with the latest prompt time of their conversations. `null` until the
   * grouping has run. A review without evidence (state `unknown`, every new install) gives no percent, so no bar.
   * Re-read only when the ledger changes.
   */
  private setupWork(): SetupWorkItem[] | null {
    if (!this.ledgerDir) return null;
    const signature = ledgerSignature(this.ledgerDir);
    if (this.setupWorkCache?.signature === signature) return this.setupWorkCache.work;
    const ledger = readLedger(this.ledgerDir);
    const groupIds = Object.keys(ledger.groups.groups);
    let work: SetupWorkItem[] | null = null;
    if (groupIds.length) {
      const conversationsOf = new Map<string, Array<[string, string]>>();
      for (const [key, groupId] of Object.entries(ledger.groups.conversations as Record<string, string>)) {
        // A key that is not a conversation key names no conversation, the way an
        // anchor that is not one names no agent below. The groups file is read
        // as JSON with no shape check, and a shared member arrives from another
        // device, so one such key must not throw the whole snapshot away.
        if (!isConversationKey(key)) continue;
        const [, platform, contentSessionId] = JSON.parse(key) as [string, string, string];
        if (!conversationsOf.has(groupId)) conversationsOf.set(groupId, []);
        conversationsOf.get(groupId)!.push([platform, contentSessionId]);
      }
      const database = existsSync(this.databasePath) ? new Database(this.databasePath, { readonly: true, create: false }) : null;
      try {
        const lastPrompt = database?.prepare(`SELECT MAX(up.created_at_epoch) AS at FROM user_prompts up JOIN sdk_sessions s ON s.id = up.session_db_id
          WHERE s.platform_source = ? AND s.content_session_id = ?`);
        work = groupIds.map((groupId) => {
          const group = ledger.groups.groups[groupId];
          const lastEpoch = Math.max(0, ...(conversationsOf.get(groupId) ?? []).map(([platform, id]) => Number((lastPrompt?.get(platform, id) as { at: number | null } | null)?.at ?? 0)), group.anchorAsk?.atEpoch ?? 0);
          const review = ledger.progress[groupId];
          let platform: string | null = null;
          try { platform = (JSON.parse(group.anchor) as string[])[1] ?? null; } catch { /* An anchor that is not a conversation key names no agent. */ }
          return {
            groupId, title: String(group.title), platform, lastActiveAt: lastEpoch ? new Date(lastEpoch).toISOString() : null,
            progressPercent: review && review.state !== 'unknown' && Number.isFinite(review.percent) ? review.percent : null,
          };
        }).sort((left, right) => String(right.lastActiveAt ?? '').localeCompare(String(left.lastActiveAt ?? ''))).slice(0, SETUP_WORK_SHOWN);
      } finally { database?.close(); }
    }
    this.setupWorkCache = { signature, work };
    return work;
  }

  private rulesFileStatus(snapshot: WorkViewSnapshot) {
    const rules = (snapshot.rules ?? []) as SavedRule[];
    let instructions = '';
    try { instructions = readFileSync(this.claudeInstructionsPath, 'utf8'); }
    catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
    return {
      path: homeRelativePath(this.rulesFilePath),
      instructionsPath: homeRelativePath(this.claudeInstructionsPath),
      approvedCount: renderRulesFile(rules).approvedCount,
      // Any spelling Claude Code loads counts (`@~/…` or an absolute `/` path), not only the one this worker writes.
      loadedInClaudeCode: hasImportLine(instructions, this.rulesFilePath),
    };
  }

  /** RULES-AND-TOOLS.md sits beside MY-RULES.md; MY-RULES.md imports it so Claude Code loads both through one line. */
  private get rulesAndToolsPath(): string {
    return join(dirname(this.rulesFilePath), RULES_AND_TOOLS_FILENAME);
  }

  private rulesFileLacksRulesAndToolsImport(): boolean {
    let markdown: string;
    try { markdown = readFileSync(this.rulesFilePath, 'utf8'); }
    catch (error) {
      // An unreadable file must not stop the worker from starting; the next rule change rewrites it.
      if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
        logger.warn('HTTP', 'Could not read MY-RULES.md to check its Rules + Tools import', { error: error instanceof Error ? error.message : String(error) });
      }
      return false;
    }
    const importLine = claudeCodeImportLine(this.rulesAndToolsPath);
    return !markdown.split('\n').some(line => line.trim() === importLine);
  }

  private renderRulesMarkdown(rules: SavedRule[]): string {
    return renderRulesFile(rules, Date.now(), { importLine: claudeCodeImportLine(this.rulesAndToolsPath) }).markdown;
  }

  /** Rewrites MY-RULES.md through a temp file beside it, so a reader never sees half a file. */
  private writeRulesFile(rules: SavedRule[]): void {
    mkdirSync(dirname(this.rulesFilePath), { recursive: true });
    // The import must never point at a missing file: Claude Code skips it without a word. SessionStart
    // keeps its text current (mode, runtime, settings), so only a missing file is written here.
    if (!existsSync(this.rulesAndToolsPath)) ensureRulesAndToolsFile(true, this.rulesAndToolsPath);
    const temp = join(dirname(this.rulesFilePath), `.MY-RULES.md.${randomUUID()}.tmp`);
    try {
      writeFileSync(temp, this.renderRulesMarkdown(rules), { mode: 0o644, flag: 'wx' });
      renameSync(temp, this.rulesFilePath);
    } catch (error) {
      // A failed write or rename (Windows EPERM/EBUSY while the target is held)
      // would otherwise leave this randomly named temp behind on every attempt.
      try { unlinkSync(temp); } catch { /* the temp may not exist */ }
      throw error;
    }
  }

  private saveRule = async (req: Request, res: Response): Promise<void> => {
    const { action, ruleId, rule } = req.body as z.infer<typeof ruleBody>;
    if (!this.appendWorkStateEntry) { res.status(503).json({ error: 'Saving rules is unavailable on this worker.' }); return; }
    const snapshot = await this.ownerStateSnapshot();
    if (!snapshot.source.connected) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before changing rules.' }); return; }
    const rules = (snapshot.rules ?? []) as SavedRule[];
    const current = ruleId ? rules.find(entry => entry.ruleId === ruleId) : undefined;
    if (action !== 'add' && !current) { this.notFound(res, 'That rule is not in your rules.'); return; }
    if (['edit', 'add', 'replace'].includes(action) && !rule) { this.badRequest(res, 'Write the rule before saving it.'); return; }
    if (rule && rule.scopeTier !== 'everywhere' && !rule.scopeName.trim()) { this.badRequest(res, 'Name the project or situation this rule is for.'); return; }
    const written = rule ? {
      scopeTier: rule.scopeTier, scopeName: rule.scopeTier === 'everywhere' ? 'Everywhere' : rule.scopeName.trim(),
      when: rule.when.trim(), then: rule.then.trim(),
    } : {};
    const now = new Date().toISOString();
    const ownRule = (): SavedRule => ({
      ruleId: `own_${randomBytes(6).toString('hex')}`, scopeId: '', scopeTier: 'everywhere', scopeName: 'Everywhere', when: '', then: '',
      ...written, setBy: 'owner', status: 'on', kind: 'directive', source: 'you', timesSeen: 1, latestAt: now,
      quotes: [], replacedBy: null, replacedAt: null, savedAt: null,
    });
    const rows: Record<string, string | number | boolean | null>[] = [];
    if (action === 'add') rows.push(ruleRowFields(ownRule()));
    else if (action === 'replace') {
      const replacement = ownRule();
      rows.push(ruleRowFields(replacement), ruleRowFields({ ...current!, status: 'replaced', replacedBy: replacement.ruleId, replacedAt: now }));
    } else if (action === 'approve') rows.push(ruleRowFields({ ...current!, ...written, setBy: current!.setBy === 'learned' ? 'approved' : current!.setBy, status: 'on' }));
    else if (action === 'edit') rows.push(ruleRowFields({ ...current!, ...written }));
    else if (action === 'turn-on' || action === 'turn-off') {
      if (current!.status !== 'on' && current!.status !== 'off') { res.status(409).json({ error: 'Only a rule you set or approved can be turned on or off.', snapshot }); return; }
      rows.push(ruleRowFields({ ...current!, status: action === 'turn-on' ? 'on' : 'off' }));
    } else if (action === 'dismiss') rows.push(ruleRowFields({ ...current!, status: 'dismissed' }));
    else {
      // Back to the learned suggestion, with its own words: only its evidence is cleared, for the story's to show again.
      if (!current!.ruleId.startsWith('pref_')) { res.status(409).json({ error: 'Only a learned rule can go back to being a suggestion.', snapshot }); return; }
      rows.push(restoredSuggestionRowFields(current!));
    }
    const project = this.homeProject ?? getProjectContext(homedir()).primary;
    for (const fields of rows) this.appendWorkStateEntry({ project, listName: MY_RULES_LIST, fields });
    const updated = await this.snapshot();
    this.writeRulesFile((updated.rules ?? []) as SavedRule[]);
    logger.debug('HTTP', 'My Rules updated', { action });
    res.setHeader('Cache-Control', 'no-store');
    // The first row is the rule this change saved, so the page can name it without guessing from the snapshot.
    res.json({ ...updated, savedRuleId: rows[0].rule_id });
    this.publish(updated);
  };

  /** The owner's focus decisions, saved as my-focus rows. Owner rows are never overridden by a refresh. */
  private saveFocus = async (req: Request, res: Response): Promise<void> => {
    const { action, focusAreaId, subFocusId, intoSubFocusId, name, goal, intoFocusAreaId, focusAreaIds, requestId, expectedVersion } = req.body as z.infer<typeof focusBody>;
    if (!this.appendWorkStateEntry) { res.status(503).json({ error: 'Saving focus areas is unavailable on this worker.' }); return; }
    // Moving a request looks the request up, and a request can be minutes old: that check reads fresh.
    const snapshot = await (action === 'move-request' || action === 'move-to-sub-focus' ? this.snapshot() : this.ownerStateSnapshot());
    if (!snapshot.source.connected) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before changing focus areas.' }); return; }
    const areas = (snapshot.focusAreas ?? []) as FocusArea[];
    // ownerEdit serializes this fresh read, version check and every row below with other owner writes.
    if (expectedVersion !== undefined && expectedVersion !== focusChangeVersion(req.body, areas, snapshot.humanRequests)) {
      res.status(409).json({ error: FOCUS_CHANGED_MESSAGE, snapshot }); return;
    }
    const areaById = new Map(areas.map(area => [area.focusAreaId, area]));
    const ownRow = (area: FocusArea, change: Partial<FocusArea>) => focusAreaRowFields({ ...area, ...change, decidedBy: 'owner' });
    const rows: Record<string, string | number | boolean | null>[] = [];

    if (action === 'reorder') {
      if (!focusAreaIds) { this.badRequest(res, 'List the focus areas in the order you want.'); return; }
      if (new Set(focusAreaIds).size !== focusAreaIds.length) { this.badRequest(res, 'Each focus area can appear once in the order.'); return; }
      const missing = focusAreaIds.find(id => !areaById.has(id));
      if (missing) { this.notFound(res, 'That focus area is not in your focus areas.'); return; }
      // The whole order of the kept areas at once, so no two share a rank. Only kept areas: an owner row on a suggestion
      // would freeze it against later refreshes, and done, dismissed and merged areas are not in the rail.
      const kept = areas.filter(area => area.status === 'kept' && !area.mergedInto).map(area => area.focusAreaId);
      if (focusAreaIds.length !== kept.length || !kept.every(id => focusAreaIds.includes(id))) {
        this.badRequest(res, 'List every kept focus area, each once, in the order you want.'); return;
      }
      focusAreaIds.forEach((id, index) => rows.push(ownRow(areaById.get(id)!, { ownerRank: index + 1 })));
    } else if (action === 'move-request') {
      if (!requestId || !focusAreaId) { this.badRequest(res, 'Choose the request and the focus area to move it to.'); return; }
      const request = snapshot.humanRequests.find(item => item.id === requestId);
      if (!request) { this.notFound(res, 'That request is not in your work.'); return; }
      if (!request.anchorConversationKey) { res.status(409).json({ error: 'This request has no conversation to file it under.', snapshot }); return; }
      if (focusAreaId !== OTHER_FOCUS_AREA_ID) {
        const target = areaById.get(focusAreaId);
        if (!target) { this.notFound(res, 'That focus area is not in your focus areas.'); return; }
        if (target.status === 'dismissed' || target.mergedInto) { res.status(409).json({ error: 'Move it to an area that is still in your focus.', snapshot }); return; }
      }
      rows.push(placementRowFields({ anchorConversationKey: request.anchorConversationKey, focusAreaId, placedBy: 'owner', evidence: 'Moved by you in My Work.' }));
    } else if (action === 'rename-sub-focus' || action === 'merge-sub-focus') {
      // A live sub focus of a live area: a merged one forwards to its target, and an area that is gone shows none.
      const area = areas.find(entry => entry.status !== 'dismissed' && !entry.mergedInto && entry.subFocuses.some(subFocus => subFocus.subFocusId === subFocusId));
      const subFocus = area?.subFocuses.find(entry => entry.subFocusId === subFocusId);
      if (!area || !subFocus) { this.notFound(res, 'That sub focus is not in your focus areas.'); return; }
      const ownSubFocusRow = (change: { name?: string; mergedInto?: string | null }) => subFocusRowFields({
        subFocusId: subFocus.subFocusId, focusAreaId: area.focusAreaId, name: subFocus.name, mergedInto: null, ...change, decidedBy: 'owner',
      });
      if (action === 'rename-sub-focus') {
        if (!name) { this.badRequest(res, 'Write the new name before saving it.'); return; }
        rows.push(ownSubFocusRow({ name }));
      } else {
        if (!intoSubFocusId) { this.badRequest(res, 'Choose the sub focus to merge it into.'); return; }
        if (intoSubFocusId === subFocus.subFocusId) { this.badRequest(res, 'A sub focus cannot be merged into itself.'); return; }
        // Only into a live sub focus of the same area: a request's sub focus never takes it out of its area.
        if (!area.subFocuses.some(entry => entry.subFocusId === intoSubFocusId)) { res.status(409).json({ error: 'Merge it into a sub focus of the same focus area.', snapshot }); return; }
        rows.push(ownSubFocusRow({ mergedInto: intoSubFocusId }));
      }
    } else if (action === 'move-to-sub-focus') {
      if (!requestId || subFocusId === undefined) { this.badRequest(res, 'Choose the request and the sub focus to move it to.'); return; }
      const request = snapshot.humanRequests.find(item => item.id === requestId);
      if (!request) { this.notFound(res, 'That request is not in your work.'); return; }
      if (!request.anchorConversationKey) { res.status(409).json({ error: 'This request has no conversation to file it under.', snapshot }); return; }
      // The request's own area as served (merges followed); Other and a request not sorted yet have no sub focuses.
      const area = request.focusAreaId ? areaById.get(request.focusAreaId) : undefined;
      if (!area) { res.status(409).json({ error: 'Put this request in a focus area before choosing its sub focus.', snapshot }); return; }
      if (subFocusId !== null && !area.subFocuses.some(entry => entry.subFocusId === subFocusId)) {
        res.status(409).json({ error: 'Move it to a sub focus of the area it is in.', snapshot }); return;
      }
      rows.push(subFocusPlacementRowFields({ anchorConversationKey: request.anchorConversationKey, subFocusId, focusAreaId: area.focusAreaId, placedBy: 'owner' }));
    } else {
      const area = focusAreaId ? areaById.get(focusAreaId) : undefined;
      if (!area) { this.notFound(res, 'That focus area is not in your focus areas.'); return; }
      if (area.mergedInto && action !== 'restore-suggestion') { res.status(409).json({ error: 'This area was merged into another. Undo the merge first.', snapshot }); return; }
      if (action === 'keep') rows.push(ownRow(area, { status: 'kept' }));
      else if (action === 'dismiss') rows.push(ownRow(area, { status: 'dismissed' }));
      else if (action === 'done') rows.push(ownRow(area, { status: 'done' }));
      else if (action === 'rename') {
        if (!name) { this.badRequest(res, 'Write the new name before saving it.'); return; }
        const navWord = navWordIn(name);
        if (navWord) { this.badRequest(res, `"${name}" reuses the nav word "${navWord}". Pick a name that means only this area.`); return; }
        rows.push(ownRow(area, { name }));
      } else if (action === 'edit-goal') {
        if (!goal) { this.badRequest(res, 'Write the goal before saving it.'); return; }
        rows.push(ownRow(area, { goal }));
      } else if (action === 'merge') {
        if (!intoFocusAreaId) { this.badRequest(res, 'Choose the area to merge it into.'); return; }
        if (intoFocusAreaId === area.focusAreaId) { this.badRequest(res, 'An area cannot be merged into itself.'); return; }
        const target = areaById.get(intoFocusAreaId);
        if (!target) { this.notFound(res, 'That focus area is not in your focus areas.'); return; }
        // A target that is itself merged or dismissed could close a merge loop or hide the requests.
        if (target.status === 'dismissed' || target.mergedInto) { res.status(409).json({ error: 'Merge it into an area that is still in your focus.', snapshot }); return; }
        rows.push(ownRow(area, { mergedInto: target.focusAreaId }));
      } else {
        // Back to a suggestion: the undo for keep, merge and dismiss. An area the owner wrote was never a suggestion.
        if (area.source === 'owner') { res.status(409).json({ error: 'Only a suggested area can go back to being a suggestion.', snapshot }); return; }
        // owner-restore hands the area back: later refresh rows apply to it again.
        rows.push(focusAreaRowFields({ ...area, status: 'suggested', mergedInto: null, decidedBy: 'owner-restore' }));
      }
    }
    const project = this.homeProject ?? getProjectContext(homedir()).primary;
    for (const fields of rows) this.appendWorkStateEntry({ project, listName: MY_FOCUS_LIST, fields });
    const updated = await this.snapshot();
    logger.debug('HTTP', 'My Focus updated', { action });
    res.setHeader('Cache-Control', 'no-store');
    res.json(updated);
    this.publish(updated);
  };

  /** Owner-goals' mined focus areas, as my-focus suggestions. Answers each mined area's id, in order. */
  private suggestOwnerGoalsAreas = async (req: Request, res: Response): Promise<void> => {
    const { areas, save_key } = req.body as z.infer<typeof ownerGoalsFocusBody>;
    if (!this.appendWorkStateEntry) { res.status(503).json({ error: 'Saving focus areas is unavailable on this worker.' }); return; }
    const snapshot = await this.ownerStateSnapshot();
    if (!snapshot.source.connected) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before saving focus areas.' }); return; }
    let planned: ReturnType<typeof ownerGoalsAreaRows>;
    try {
      const scope = JSON.stringify([this.homeProject ?? getProjectContext(homedir()).primary, this.getLocalDeviceId()]);
      planned = ownerGoalsAreaRows(areas, (snapshot.focusAreas ?? []) as FocusArea[], undefined, save_key ? { key: save_key, scope } : undefined);
    }
    catch (error) { this.badRequest(res, error instanceof Error ? error.message : String(error)); return; }
    const project = this.homeProject ?? getProjectContext(homedir()).primary;
    for (const fields of planned.rows) this.appendWorkStateEntry({ project, listName: MY_FOCUS_LIST, fields });
    logger.debug('HTTP', 'Owner-goals focus areas saved', { mined: areas.length, rows: planned.rows.length });
    res.setHeader('Cache-Control', 'no-store');
    res.json({ focusAreaIds: planned.focusAreaIds });
    if (planned.rows.length) this.publish();
  };

  /**
   * The focus step's answer as my-focus rows: router placements and `suggested` areas, written as the system. The
   * whole answer is checked against the saved areas before the first row is written, so a refusal (TypeError)
   * writes nothing. The automatic refresh calls this in-process; the skill's CLI reaches it through saveRouterFocus.
   */
  recordRouterFocus = (body: RouterFocusBody): Promise<{ placed: number; focusAreaIds: string[] }> => this.inOwnerEditOrder(async () => {
    if (!this.appendWorkStateEntry) throw new Error('Saving focus areas is unavailable on this worker.');
    const snapshot = await this.ownerStateSnapshot();
    if (!snapshot.source.connected) throw new Error('Local work is unavailable. Reconnect before placing requests.');
    let planned: ReturnType<typeof routerFocusRows>;
    // Every row is built and size-checked before the first write: a refusal here (an unknown or dismissed area, a row too
    // long to save) wrote nothing, so it is a TypeError the refresh may answer again.
    try { planned = routerFocusRows(body, (snapshot.focusAreas ?? []) as FocusArea[]); }
    catch (error) { throw error instanceof RangeError ? new TypeError(error.message) : error; }
    const project = this.homeProject ?? getProjectContext(homedir()).primary;
    let written = 0;
    try {
      for (const fields of planned.rows) { this.appendWorkStateEntry({ project, listName: MY_FOCUS_LIST, fields }); written++; }
    } catch (error) {
      // A failure after a write is never a refusal: asking again would suggest the same areas twice.
      if (written) this.publish();
      throw new Error(`Saving the focus step stopped after ${written} of ${planned.rows.length} rows: ${error instanceof Error ? error.message : String(error)}`);
    }
    logger.debug('HTTP', 'Focus step saved', { placed: planned.placed, proposed: planned.focusAreaIds.length });
    if (planned.rows.length) this.publish();
    return { placed: planned.placed, focusAreaIds: planned.focusAreaIds };
  });

  private saveRouterFocus = async (req: Request, res: Response): Promise<void> => {
    let saved: { placed: number; focusAreaIds: string[] };
    try { saved = await this.recordRouterFocus(req.body as z.infer<typeof routerFocusBody>); }
    catch (error) {
      if (error instanceof TypeError) { this.badRequest(res, error.message); return; }
      res.status(503).json({ error: error instanceof Error ? error.message : String(error) });
      return;
    }
    res.setHeader('Cache-Control', 'no-store');
    res.json(saved);
  };

  /** The seed's rows, checked against the saved areas and placements before the first write (a refusal writes nothing). */
  private saveSeedFocus = async (req: Request, res: Response): Promise<void> => {
    const body = req.body as z.infer<typeof seedFocusBody>;
    if (!this.appendWorkStateEntry) { res.status(503).json({ error: 'Saving focus areas is unavailable on this worker.' }); return; }
    if (!(await this.ownerStateSnapshot()).source.connected) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before seeding focus areas.' }); return; }
    const served = existsSync(this.databasePath) ? realpathSync(this.databasePath) : this.databasePath;
    const expected = existsSync(body.databasePath) ? realpathSync(body.databasePath) : body.databasePath;
    if (served !== expected) { res.status(409).json({ error: `This worker serves ${served}, not ${expected}. Nothing was written.` }); return; }
    const database = new Database(this.databasePath, { readonly: true, create: false });
    let planned: ReturnType<typeof seedFocusRows> | ReturnType<typeof seedUndoRows>;
    try {
      const focus = readFocus(database, { localDeviceId: this.getLocalDeviceId() });
      planned = body.action === 'apply' ? seedFocusRows(body, focus) : seedUndoRows(body, focus);
    } catch (error) {
      if (error instanceof TypeError || error instanceof RangeError) { this.badRequest(res, error.message); return; }
      throw error;
    } finally { database.close(); }
    const project = this.homeProject ?? getProjectContext(homedir()).primary;
    let written = 0;
    try {
      for (const fields of planned.rows) { this.appendWorkStateEntry({ project, listName: MY_FOCUS_LIST, fields }); written++; }
    } finally { if (written) this.publish(); }
    logger.debug('HTTP', 'Focus seed saved', { action: body.action, rows: written });
    const { rows: _rows, ...result } = planned;
    res.setHeader('Cache-Control', 'no-store');
    res.json({ action: body.action, rows: written, ...result });
  };

  /**
   * The owner says a subagent finished when no recorded stop covers it (one that
   * finished before the SubagentStop hook existed). Saved like a hook stop, with
   * its own source, so a later tool call from the agent still reopens it.
   */
  /** Counts the viewer opening on a page, switching page, or opening the hosted offer's plans, through the worker's own
   * consent-gated, whitelist-scrubbed telemetry (telemetry.ts), so the page loads no analytics SDK. The report names the
   * page (or the plan setup kept) only — never what it showed. */
  private viewerHostname(req: Request): string {
    const host = (req.hostname || '').trim().toLowerCase();
    return /^[a-z0-9.-]{1,253}$/.test(host) ? host : 'unknown';
  }

  private recordUsage = (req: Request, res: Response): void => {
    const report = req.body as z.infer<typeof usageBody>;
    const where = { viewer_host: 'local', hostname: this.viewerHostname(req) };
    if (report.event === 'opened') captureEvent('work_view_opened', { view: report.view, ...where });
    else if (report.event === 'switched') captureEvent('work_view_switched', { view: report.view, previous_view: report.previousView, ...where });
    else captureEvent('work_view_hosted_plans_viewed', { ...(report.cloudSyncOffer ? { cloud_sync_offer: report.cloudSyncOffer } : {}), ...where });
    res.status(204).end();
  };

  /** Which areas this install shares, by id, with their links. No credentials and no page content. */
  private listFocusShares = (_req: Request, res: Response): void => {
    const shares = this.focusShares;
    if (!shares) { res.json({ connected: false, shares: {} }); return; }
    const listed = Object.fromEntries(Object.entries(shares.list()).map(([id, entry]) => [id, {
      url: entry.url, sharedAt: entry.sharedAt, publishedAt: entry.publishedAt, publishError: this.focusSharePublisher?.errorFor(id) ?? null,
    }]));
    res.json({ connected: shares.connected(), shares: listed });
  };

  private focusShareRefusal(res: Response, error: unknown): void {
    if (error instanceof FocusShareError) { res.status(error.status).json({ error: error.message }); return; }
    throw error;
  }

  /** Shares an area (first call) or publishes its page now. The worker builds the page from its own work view. */
  private publishFocusShare = async (req: Request, res: Response): Promise<void> => {
    const { focusAreaId } = req.body as z.infer<typeof focusShareAreaBody>;
    if (!this.focusSharePublisher) { res.status(503).json({ error: 'Sharing is unavailable on this worker.' }); return; }
    try {
      const { entry, sent } = await this.focusSharePublisher.publishArea(focusAreaId);
      res.json({ url: entry.url, sharedAt: entry.sharedAt, publishedAt: entry.publishedAt, sent });
    } catch (error) { this.focusShareRefusal(res, error); }
  };

  private stopFocusShare = async (req: Request, res: Response): Promise<void> => {
    const { focusAreaId } = req.body as z.infer<typeof focusShareAreaBody>;
    if (!this.focusSharePublisher) { res.status(503).json({ error: 'Sharing is unavailable on this worker.' }); return; }
    try {
      await this.focusSharePublisher.stopArea(focusAreaId);
      res.json({ stopped: true });
    } catch (error) { this.focusShareRefusal(res, error); }
  };

  private focusShareComments = async (req: Request, res: Response): Promise<void> => {
    const focusAreaId = typeof req.query.focusAreaId === 'string' ? req.query.focusAreaId : '';
    if (!focusAreaIdField.safeParse(focusAreaId).success) { res.status(400).json({ error: 'focusAreaId is required.' }); return; }
    if (!this.focusShares) { res.status(503).json({ error: 'Sharing is unavailable on this worker.' }); return; }
    try {
      res.json(await this.focusShares.comments(focusAreaId));
    } catch (error) { this.focusShareRefusal(res, error); }
  };

  private commentOnFocusShare = async (req: Request, res: Response): Promise<void> => {
    const { focusAreaId, body } = req.body as z.infer<typeof focusShareCommentBody>;
    if (!this.focusShares) { res.status(503).json({ error: 'Sharing is unavailable on this worker.' }); return; }
    try {
      res.status(201).json({ comment: await this.focusShares.comment(focusAreaId, body) });
    } catch (error) { this.focusShareRefusal(res, error); }
  };

  private markAgentFinished = async (req: Request, res: Response): Promise<void> => {
    const { agentGroupId } = req.body as z.infer<typeof markAgentFinishedBody>;
    if (!this.appendWorkStateEntry) { res.status(503).json({ error: 'Saving agent state is unavailable on this worker.' }); return; }
    const snapshot = await this.snapshot();
    if (!snapshot.source.connected) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before changing an agent.' }); return; }
    const agent = (snapshot.agents as Array<{ id: string; isSubagent?: boolean; canMarkFinished?: boolean; agentId: string | null; agentType: string | null; platformSource: string | null; contentSessionId: string | null; project: string }>)
      .find(entry => entry.id === agentGroupId);
    if (!agent) { this.notFound(res, 'That agent is not in the loaded work.'); return; }
    if (!agent.isSubagent || !agent.agentId || !agent.contentSessionId) { res.status(409).json({ error: 'Only a subagent can be marked finished.', snapshot }); return; }
    if (!agent.canMarkFinished) { res.status(409).json({ error: 'This agent is already recorded as finished.', snapshot }); return; }
    const stoppedAtEpoch = Date.now();
    this.appendWorkStateEntry({
      project: agent.project,
      listName: AGENT_LIFECYCLE_LIST,
      fields: agentStopFields({
        platformSource: agent.platformSource, contentSessionId: agent.contentSessionId, agentId: agent.agentId,
        agentType: agent.agentType, stoppedAtEpoch, stopSource: 'owner-marked-finished',
      }),
    });
    logger.debug('HTTP', 'Agent marked finished by the owner');
    const updated = await this.snapshot();
    res.setHeader('Cache-Control', 'no-store');
    res.json(updated);
    this.publish(updated);
  };

  /**
   * The owner's Join / Not this on a judge suggestion, written to the request ledger through the same
   * decisions the request-progress skill's `apply` takes (request-judge.mjs decideSuggestion). The
   * database is only read: projects and first asks for the decision.
   */
  private answerSuggestion = async (req: Request, res: Response): Promise<void> => {
    const { conversationKey, action, expectedSuggestionHash } = req.body as z.infer<typeof suggestionBody>;
    if (!this.ledgerDir) { res.status(503).json({ error: 'Request suggestions are unavailable on this worker.' }); return; }
    if (!existsSync(this.databasePath)) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before answering a suggestion.' }); return; }
    if (this.getSharedStatus?.().enabled === true) {
      if (!this.answerSharedSuggestion || !this.prepareSharedOwnerAction) { res.status(503).json({ error: 'Shared suggestions are unavailable on this worker.' }); return; }
      if (!Object.hasOwn(req.body, 'expectedSuggestionHash')) { res.status(409).json({ error: 'Reload this suggestion before answering it.' }); return; }
      let pendingLocalMaterialization = false;
      try {
        await this.prepareSharedOwnerAction();
        const outcome = this.answerSharedSuggestion(conversationKey, action, expectedSuggestionHash ?? null);
        if (!outcome) { res.status(409).json({ error: 'This suggestion changed. Read its latest state before answering it.', snapshot: await this.snapshot() }); return; }
        pendingLocalMaterialization = (outcome as { materialization?: string }).materialization === 'pending';
      } catch (error) {
        if (!/changed since read|conflict|SQLITE_BUSY/i.test(String(error))) throw error;
        res.status(409).json({ error: 'This suggestion changed or has a concurrent decision. Read its latest state before answering it.', snapshot: await this.snapshot() }); return;
      }
      const updated = { ...await this.snapshot(), save: { scope: 'shared', state: 'queued',
        ...(pendingLocalMaterialization ? { localMaterialization: 'pending' } : {}) } };
      res.setHeader('Cache-Control', 'no-store'); res.json(updated); this.publish(updated); return;
    }
    const database = new Database(this.databasePath, { readonly: true, create: false });
    let outcome: ReturnType<typeof decideSuggestion>;
    try { outcome = decideSuggestion({ dir: this.ledgerDir, database, conversationKey, action, now: Date.now() }); }
    finally { database.close(); }
    if (!outcome) { this.notFound(res, 'That suggestion is not waiting for an answer.'); return; }
    logger.debug('HTTP', 'Request suggestion answered', { action, status: outcome.status });
    const updated = { ...await this.snapshot(), save: { scope: 'local', state: 'saved' } };
    res.setHeader('Cache-Control', 'no-store');
    res.json(updated);
    this.publish(updated);
  };

  /** Starts an on-demand refresh and answers before the run ends, which can take many minutes. */
  private startManualRefresh = async (_req: Request, res: Response): Promise<void> => {
    if (!this.autoRefresh) { res.status(503).json({ error: REFRESH_START_REFUSAL.unavailable }); return; }
    const started = this.autoRefresh.runNow({ reason: OWNER_REFRESH_REASON });
    // runNow decides before its first await: busy now means a run started, and any other answer is already settled.
    if (this.autoRefresh.busy) {
      void started.catch(error => logger.warn('HTTP', 'Manual work-view refresh failed', { error: error instanceof Error ? error.message : String(error) }));
      res.status(202).json({ state: 'running' });
      return;
    }
    const state = await started;
    if (state === 'setup-running') { res.status(409).json({ error: REFRESH_START_REFUSAL.setupRunning }); return; }
    if (state === 'closed') { res.status(503).json({ error: REFRESH_START_REFUSAL.shuttingDown }); return; }
    res.status(state === 'running' ? 202 : 200).json({ state });
  };

  /** Whether a conversation can be continued from here and where its turn would run, and what was sent to it from here. */
  private readConversationContinuation = (req: Request, res: Response): void => {
    const query = conversationContinuationQuery.safeParse(req.query);
    if (!query.success) {
      this.badRequest(res, `Name the conversation with platform (${CONTINUABLE_PLATFORMS.join(' or ')}) and conversationSessionId.`);
      return;
    }
    const runner = this.conversationContinuationRunner;
    if (!runner) { res.status(503).json({ error: CONVERSATION_CONTINUATION_UNAVAILABLE }); return; }
    const { platform, conversationSessionId } = query.data;
    res.setHeader('Cache-Control', 'no-store');
    res.json({ target: runner.target(platform, conversationSessionId), exchanges: runner.exchanges(platform, conversationSessionId) });
  };

  /**
   * Sends the owner's message to the conversation as one turn, and answers 202 with the running exchange without waiting
   * for the turn. Every open window hears that it started and that it ended through a conversation-continuation notice,
   * and reads how through the guarded read. A refusal starts nothing.
   */
  private continueConversation = async (req: Request, res: Response): Promise<void> => {
    const runner = this.conversationContinuationRunner;
    if (!runner) { res.status(503).json({ error: CONVERSATION_CONTINUATION_UNAVAILABLE }); return; }
    const { platform, conversationSessionId, ownerMessage } = req.body as z.infer<typeof continueConversationBody>;
    const started = await runner.start(platform, conversationSessionId, ownerMessage);
    if ('refusalText' in started) { res.status(409).json({ error: started.refusalText }); return; }
    // The runner reports an exchange only when it ends, so its start is pushed here, for every other open window. The
    // pushed state is the runner's current one, so a turn that has ended already is never pushed as running after it.
    const { exchangeId } = started.exchange;
    const exchange = runner.exchanges(platform, conversationSessionId).find(candidate => candidate.exchangeId === exchangeId);
    // The runner keeps every exchange it starts for the worker's life. Without it there is no current state to push, and
    // the copy start answered with could be pushed as running after the turn ended, so this is a broken invariant.
    if (!exchange) throw new Error(`The conversation runner does not hold exchange ${exchangeId}, which it has just started.`);
    this.publishConversationContinuation(exchange);
    res.setHeader('Cache-Control', 'no-store');
    res.status(202).json({ exchange });
  };

  /** Asks the running turn to end. Its exchange stays running until the CLI has exited, and its end is pushed then. */
  private stopConversationContinuation = (req: Request, res: Response): void => {
    const runner = this.conversationContinuationRunner;
    if (!runner) { res.status(503).json({ error: CONVERSATION_CONTINUATION_UNAVAILABLE }); return; }
    const { platform, conversationSessionId } = req.body as z.infer<typeof conversationRef>;
    const stopped = runner.stop(platform, conversationSessionId);
    if ('refusalText' in stopped) { res.status(409).json({ error: stopped.refusalText }); return; }
    res.setHeader('Cache-Control', 'no-store');
    res.json({ exchange: stopped.exchange });
  };

  /**
   * Opens the conversation in its tool's own app: Claude Code's `claude --desktop --resume <session uuid>`. It starts no
   * turn. Answers once the command has exited, with no deadline: 200 with what it printed when the app opened it, else
   * 409 with the sentence saying why, which holds everything the command printed when it ran. While one open of a
   * conversation runs, another is refused (409), and the stop route below ends it when it names this open's click.
   */
  private openConversationInApp = async (req: Request, res: Response): Promise<void> => {
    const runner = this.conversationContinuationRunner;
    if (!runner) { res.status(503).json({ error: CONVERSATION_CONTINUATION_UNAVAILABLE }); return; }
    const { platform, conversationSessionId, openInAppClickId } = req.body as z.infer<typeof openInAppClickBody>;
    const opened = await runner.openInApp(platform, conversationSessionId, openInAppClickId);
    if ('refusalText' in opened) { res.status(409).json({ error: opened.refusalText }); return; }
    const answer: ConversationOpenedInApp = { printedText: opened.printedText };
    res.setHeader('Cache-Control', 'no-store');
    res.json(answer);
  };

  /**
   * Stops the command opening a conversation in its app for the click the body names. The page asks for it as it is
   * unloaded while its open still runs (a reload, a closed tab): the worker cannot see that page's request go away
   * (stopOpeningInApp says why). 200 once the stop is sent, which the open's own answer then reports; 409 with a sentence
   * when no open of that click is running, or when the stop could not be sent.
   */
  private stopOpeningConversationInApp = (req: Request, res: Response): void => {
    const runner = this.conversationContinuationRunner;
    if (!runner) { res.status(503).json({ error: CONVERSATION_CONTINUATION_UNAVAILABLE }); return; }
    const { platform, conversationSessionId, openInAppClickId } = req.body as z.infer<typeof openInAppClickBody>;
    const stopping = runner.stopOpeningInApp(platform, conversationSessionId, openInAppClickId);
    if ('refusalText' in stopping) { res.status(409).json({ error: stopping.refusalText }); return; }
    res.setHeader('Cache-Control', 'no-store');
    res.json({});
  };

  /** Adds the MY-RULES.md import to the user-level CLAUDE.md once. Never touches a project's CLAUDE.md or AGENTS.md. */
  private loadRulesInClaudeCode = async (_req: Request, res: Response): Promise<void> => {
    const snapshot = await this.ownerStateSnapshot();
    if (!snapshot.source.connected) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before loading rules.' }); return; }
    // Always from the saved rules: an existing file can predate rule changes received through sync.
    this.writeRulesFile((snapshot.rules ?? []) as SavedRule[]);
    if (!(snapshot.rulesFile as { loadedInClaudeCode: boolean }).loadedInClaudeCode) {
      let instructions = '';
      try { instructions = readFileSync(this.claudeInstructionsPath, 'utf8'); }
      catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
      const separator = instructions === '' || instructions.endsWith('\n') ? '' : '\n';
      mkdirSync(dirname(this.claudeInstructionsPath), { recursive: true });
      appendFileSync(this.claudeInstructionsPath, `${separator}${myRulesImportLine(this.rulesFilePath)}\n`);
    }
    const updated = await this.snapshot();
    res.setHeader('Cache-Control', 'no-store');
    res.json(updated);
    this.publish(updated);
  };

  private review = async (req: Request, res: Response): Promise<void> => {
    const { requestId, expectedFingerprint, expectedReviewHash, checked } = req.body as z.infer<typeof reviewBody>;
    const sharedEnabled = this.getSharedStatus?.().enabled === true;
    if (sharedEnabled) {
      try { await this.prepareSharedOwnerAction?.(); }
      catch (error) {
        if (!/changed since read|conflict|SQLITE_BUSY/i.test(String(error))) throw error;
        res.status(409).json({ error: 'The shared state has a conflicting change. Reload before reviewing.', snapshot: await this.snapshot() }); return;
      }
    }
    const snapshot = await this.snapshot();
    if (!snapshot.source.connected) { res.status(503).json({ error: 'Local work is unavailable. Reconnect before reviewing.' }); return; }
    const request = snapshot.humanRequests.find(item => item.id === requestId);
    if (!request) { this.notFound(res, 'Request is outside the loaded work.'); return; }
    if (request.review?.available === false) { res.status(503).json({ error: sharedEnabled ? 'Complete shared review evidence is unavailable. Reload after syncing.' : 'Local review storage is unavailable. Your source work is unchanged.' }); return; }
    if (request.contentFingerprint !== expectedFingerprint) {
      res.status(409).json({ error: 'This work changed. Read the latest result before checking it.', snapshot });
      return;
    }
    if (checked && !isReportedReadyForCheck(request)) { res.status(409).json({ error: 'This request has not been reported complete.', snapshot }); return; }
    let save: { scope: 'local' | 'shared'; state: 'saved' | 'queued' | 'unchanged' };
    if (sharedEnabled) {
      if (!this.saveSharedReview || !request.sharedReviewDescriptor?.complete) { res.status(503).json({ error: 'Shared review persistence is unavailable.' }); return; }
      if (!Object.hasOwn(req.body, 'expectedReviewHash') || expectedReviewHash !== request.sharedReview?.hash) {
        res.status(409).json({ error: 'This review changed. Reload before saving a decision.', snapshot }); return;
      }
      try {
        const result = this.saveSharedReview({ descriptor: request.sharedReviewDescriptor, checked, expectedHash: expectedReviewHash ?? null });
        save = { scope: 'shared', state: result.changed ? 'queued' : 'unchanged' };
      } catch (error) {
        if (!/changed since read|conflict|SQLITE_BUSY/i.test(String(error))) throw error;
        res.status(409).json({ error: 'This review changed or has a concurrent decision. Reload before saving.', snapshot: await this.snapshot() }); return;
      }
    } else {
      this.reviews.setOverride(requestId, expectedFingerprint, checked, request.sharedReview?.hash ?? null);
      // Keep the exact legacy sidecar behavior for older readers without ever
      // exporting their acknowledgements to shared state.
      this.reviews.set(requestId, request.legacyContentFingerprint ?? expectedFingerprint, checked);
      save = { scope: 'local', state: 'saved' };
    }
    logger.debug('HTTP', 'Work review updated', { checked, scope: save.scope });
    const updated = { ...await this.snapshot(), save };
    res.setHeader('Cache-Control', 'no-store');
    res.json(updated);
    this.publish(updated);
  };

  private events = async (req: Request, res: Response): Promise<void> => {
    res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
    res.setHeader('Cache-Control', 'no-cache, no-transform');
    res.setHeader('Connection', 'keep-alive');
    res.setHeader('X-Accel-Buffering', 'no');
    res.flushHeaders();
    res.write('retry: 1000\n\n');
    this.clients.add(res);
    const overview = req.query.view === 'overview';
    if (overview) this.overviewClients.add(res);
    const socket = res.socket;
    const cleanup = () => {
      this.clients.delete(res);
      this.overviewClients.delete(res);
      req.off('close', cleanup);
      res.off('close', cleanup);
      socket?.off('close', cleanup);
      if (!this.clients.size) this.stopTimers();
    };
    req.on('close', cleanup);
    res.on('close', cleanup);
    socket?.on('close', cleanup);
    res.on('error', () => { cleanup(); res.destroy(); });
    let initial: WorkViewSnapshot;
    try { initial = await (overview ? this.readWorkSnapshot() : this.snapshot(0, 'event-initial')); }
    catch (error) {
      // An open stream with no first view would look connected forever. Ending it makes the page reconnect.
      logger.warn('HTTP', 'Could not read work view for a new event stream', { error: error instanceof Error ? error.message : String(error) });
      cleanup();
      res.end();
      return;
    }
    if (!this.clients.has(res) || res.destroyed || res.writableEnded) return;
    this.send(res, overview ? 'work-view-overview' : 'work-view', overview ? workViewOverview(initial) : initial);
    const scan: SetupScanState | undefined = this.setupScan?.state();
    if (scan && scan.phase !== 'idle') this.send(res, 'setup-scan', scan);
    // A reconnecting setup page may have missed checkout being paid or an import finishing: send where setup stands.
    // A setup state that cannot be read is logged and leaves the work feed running.
    try {
      const setupState = readSetupState(this.setupStatePath);
      if (setupState.status === 'pending') this.send(res, 'setup-state', this.setupStateView(setupState));
    } catch (error) {
      logger.error('HTTP', 'Setup state could not be read for a new event stream', {}, error as Error);
    }
    // Activity can change on every observer write. Coalesce these rebuilds;
    // work-state, title-cache and ledger changes still appear on the next tick.
    if (!this.poll) this.poll = setInterval(() => {
      void this.backgroundSnapshot().then(snapshot => this.publish(snapshot))
        .catch(error => logger.warn('HTTP', 'Could not read work view for event stream', { error: error instanceof Error ? error.message : String(error) }));
    }, this.pollIntervalMs);
    if (!this.heartbeat) this.heartbeat = setInterval(() => {
      void this.backgroundSnapshot().then(snapshot => {
        for (const client of this.clients) this.send(client, 'heartbeat', { at: new Date().toISOString(), source: snapshot.source, ...(this.getSyncStatus ? { sync: this.getSyncStatus() } : {}) });
      }).catch(error => logger.warn('HTTP', 'Could not read work view for heartbeat', { error: error instanceof Error ? error.message : String(error) }));
    }, 15000);
    this.poll?.unref(); this.heartbeat?.unref();
  };

  private send(res: Response, event: string, value: unknown): void {
    if (res.destroyed || res.writableEnded || res.writableLength > 4 * 1024 * 1024) {
      this.clients.delete(res); this.overviewClients.delete(res); res.destroy(); return;
    }
    try {
      const startedAt = process.hrtime.bigint();
      const frame = `event: ${event}\ndata: ${JSON.stringify(value)}\n\n`;
      res.write(frame);
      this.lastResponseDiagnostic = { transport: 'sse', writeMs: boundedDurationMs(Number(process.hrtime.bigint() - startedAt) / 1e6), bytes: Buffer.byteLength(frame) };
    }
    catch { this.clients.delete(res); this.overviewClients.delete(res); res.destroy(); }
  }

  private publish(snapshot?: WorkViewSnapshot): void {
    // With no event stream open there is no one to send to: skip the rebuild. A stream that opens later
    // gets a fresh snapshot first, before any publish.
    if (this.closed || !this.clients.size) return;
    if (!snapshot) { this.publishFreshRead(); return; }
    // rulesFile changes without a new revision when the rules are loaded in Claude Code; other tabs must still see it.
    const signature = JSON.stringify([snapshot.revision, snapshot.source.connected, snapshot.source.error, snapshot.setup, snapshot.rulesFile]);
    if (signature !== this.signature) {
      this.signature = signature;
      for (const client of this.clients) if (!this.overviewClients.has(client)) this.send(client, 'work-view', snapshot);
    }
    // Mapped time labels age once a minute even while the underlying activity
    // snapshot is cached; this does not renew its source data freshness.
    // A new deliverable or owner move changes the swipe deck without a stack write.
    if (snapshot.revision !== this.reviewStackWorkRevision) {
      this.reviewStackWorkRevision = snapshot.revision;
      this.publishReviewStackChanged();
    }
    const overviewSignature = `${signature}:${Math.floor(Date.now() / 60_000)}`;
    if (this.overviewClients.size && overviewSignature !== this.overviewSignature) {
      this.overviewSignature = overviewSignature;
      const overview = workViewOverview(snapshot);
      for (const client of this.overviewClients) this.send(client, 'work-view-overview', overview);
    }
  }

  /**
   * Publishes from a read that starts after this call. A setup scan asks every 200 ms and a read on a large database
   * can take longer, so while one read is out, further asks collapse into one more read after it.
   */
  private publishFreshRead(): void {
    if (this.freshPublishReading) { this.freshPublishWanted = true; return; }
    this.freshPublishReading = true;
    void this.snapshot(0, 'publish').then(snapshot => this.publish(snapshot))
      .catch(error => logger.warn('HTTP', 'Could not read work view for event stream', { error: error instanceof Error ? error.message : String(error) }))
      .finally(() => {
        this.freshPublishReading = false;
        if (!this.freshPublishWanted) return;
        this.freshPublishWanted = false;
        this.publish();
      });
  }

  private stopTimers(): void {
    if (this.poll) clearInterval(this.poll);
    if (this.heartbeat) clearInterval(this.heartbeat);
    this.poll = null; this.heartbeat = null;
  }

  getClientCount(): number { return this.clients.size; }

  publishSyncStatus(sync: ViewerSyncStatus): void {
    this.broadcast('sync_status', { sync });
  }

  publishReviewStackChanged(): void {
    // The shared event stream carries no cards, IDs or decisions. Readers use the guarded private route.
    this.broadcast('review-stack-changed', {});
  }

  /**
   * Tells every open window that an exchange sent from My Work started (the continue route) or ended (the runner's
   * onChange), by its id, its conversation and its state only. The event stream checks no origin and the worker's CORS
   * lets any localhost page read it, so the message, the reply, the failure text and the folder stay behind the guarded
   * read, which the page asks when the notice arrives.
   */
  publishConversationContinuation({ exchangeId, platform, conversationSessionId, state }: ConversationContinuation): void {
    const notice: ConversationContinuationNotice = { exchangeId, platform, conversationSessionId, state };
    this.broadcast('conversation-continuation', notice);
  }

  close(): void {
    this.closed = true;
    // Stops every turn and open started from My Work, and refuses new ones (ConversationContinuationRunner.close).
    this.conversationContinuationRunner?.close();
    this.unsubscribeSetupScan?.();
    this.unsubscribeHistoryImport?.();
    this.stopRulesSync();
    this.stopTimers();
    this.focusSharePublisher?.close();
    for (const client of this.clients) client.end();
    this.clients.clear();
    this.overviewClients.clear();
    this.source.close();
    void this.sourceReader.close();
    this.reviews.close();
    this.humanCopy?.close();
    this.autoRefresh?.close();
  }
}