Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Cancel the shadow fan-out warm-up when the server is destroyed (#4247)
Nick Gomez41b8d4bdad41
diff --git a/.changeset/shadow-warmup-cancel-on-destroy.md b/.changeset/shadow-warmup-cancel-on-destroy.mdnew file mode 100644index 000000000..3e660c5cc--- /dev/null+++ b/.changeset/shadow-warmup-cancel-on-destroy.md@@ -0,0 +1,9 @@+---+'@inkeep/open-knowledge': patch+---++Shutting the server down now cancels the shadow history warm-up that was still scheduled, so that warm-up no longer issues a git write after shutdown reports it has finished.++Boot schedules a one-shot warm-up three seconds later that rebuilds the shadow repository's fan-out index, writing a git index and loose objects under the project's `.git/ok/`. The handle for that timer was discarded, so shutdown had nothing to cancel: a server destroyed inside those three seconds drained its in-flight shadow work, released the shadow lock, and returned, and the warm-up then fired against a repository that was no longer supposed to be written to. The timer is now held and cleared in the same shutdown phase that clears the other pending timers, before the shadow drain runs, so a warm-up that has not yet fired never starts and one already running is still awaited by the existing drain.++Anything that deletes the project directory as soon as shutdown returns was racing those writes. The write recreated directories under `.git/ok/objects/` while the delete was walking them, which surfaces as an `ENOTEMPTY` failure from the delete rather than from anything the server reports.diff --git a/packages/server/src/server-factory.ts b/packages/server/src/server-factory.tsindex e96a9aefb..b81628a62 100644--- a/packages/server/src/server-factory.ts+++ b/packages/server/src/server-factory.ts@@ -356,6 +356,8 @@ export interface ServerInstance {readonly acpPermissions: AcpPermissionStore;}+export const SHADOW_FANOUT_WARMUP_MS = 3000;+const PARK_SNAPSHOT_ORIGIN = (() => {const ctx = Object.freeze({ origin: 'park-snapshot', paired: true as const });return Object.freeze({@@ -762,6 +764,7 @@ export function createServer(options: ServerOptions): ServerInstance {let bridgeLossReporter: BridgeDeriveLossReporter | undefined;let cc1Broadcaster: CC1Broadcaster | null = null;let inPlaceRescanTimer: ReturnType<typeof setTimeout> | null = null;+ let shadowWarmupTimer: ReturnType<typeof setTimeout> | null = null;const IN_PLACE_RESCAN_DEBOUNCE_MS = 500;let agentFocusBroadcaster: AgentFocusBroadcaster | null = null;let agentPresenceBroadcaster: AgentPresenceBroadcaster | null = null;@@ -2677,6 +2680,10 @@ export function createServer(options: ServerOptions): ServerInstance {clearTimeout(inPlaceRescanTimer);inPlaceRescanTimer = null;}+ if (shadowWarmupTimer) {+ clearTimeout(shadowWarmupTimer);+ shadowWarmupTimer = null;+ }if (headWatcher) {await headWatcher.unsubscribe();headWatcher = null;@@ -2945,14 +2952,16 @@ export function createServer(options: ServerOptions): ServerInstance {}}- if (shadowRef.current) {+ if (shadowRef.current && inflightDestroy === null) {const warmShadow = shadowRef.current;const warmContentRoot = toPosix(relative(projectDir, contentDir)) || '.';- setTimeout(() => {+ shadowWarmupTimer = setTimeout(() => {+ shadowWarmupTimer = null;void buildWipTree(warmShadow, warmContentRoot).catch((e) => {log.debug({ err: e }, '[shadow] fan-out index warm-up failed (non-fatal)');});- }, 3000).unref();+ }, SHADOW_FANOUT_WARMUP_MS);+ shadowWarmupTimer.unref?.();}if (shadowRef.current) {diff --git a/packages/server/src/shadow-drain-on-destroy.test.ts b/packages/server/src/shadow-drain-on-destroy.test.tsindex 5f051da7e..ab4277ec9 100644--- a/packages/server/src/shadow-drain-on-destroy.test.ts+++ b/packages/server/src/shadow-drain-on-destroy.test.ts@@ -1,11 +1,12 @@+import { existsSync, rmSync } from 'node:fs';import { mkdtemp, rm } from 'node:fs/promises';import { tmpdir } from 'node:os';import { join } from 'node:path';import simpleGit from 'simple-git';import { afterEach, beforeEach, describe, expect, test } from 'vitest';-import { createServer, type ServerInstance } from './server-factory.ts';+import { createServer, type ServerInstance, SHADOW_FANOUT_WARMUP_MS } from './server-factory.ts';import { type ShadowOpGate, shadowOpGateFor } from './shadow-op-gate.ts';-import { initShadowRepo, type ShadowHandle } from './shadow-repo.ts';+import { FANOUT_INDEX_NAME, initShadowRepo, type ShadowHandle } from './shadow-repo.ts';describe('createServer() — shadow mutator drain on destroy', () => {let projectDir: string;@@ -79,6 +80,18 @@ describe('createServer() — shadow mutator drain on destroy', () => {expect(gate.activeMutators).toBe(0);});+ test('destroy() cancels the pending fan-out warm-up so no shadow write lands after teardown', async () => {+ const srv = await boot(10_000);+ const fanoutIndex = join(shadow.gitDir, FANOUT_INDEX_NAME);++ await srv.destroy();+ rmSync(fanoutIndex, { force: true });+ await delay(SHADOW_FANOUT_WARMUP_MS + 500);+ await shadowOpGateFor(shadow).drain();++ expect(existsSync(fanoutIndex)).toBe(false);+ });+test('destroy() stays bounded when a shadow mutator never retires', async () => {const srv = await boot(300);diff --git a/packages/server/src/shadow-repo.ts b/packages/server/src/shadow-repo.tsindex 25fc0b53e..2ea79082d 100644--- a/packages/server/src/shadow-repo.ts+++ b/packages/server/src/shadow-repo.ts@@ -357,7 +357,7 @@ async function commitWipInner(}}-const FANOUT_INDEX_NAME = 'index-wip-fanout';+export const FANOUT_INDEX_NAME = 'index-wip-fanout';function sweepOrphanedScratchState(shadow: ShadowHandle): number {let deleted = 0;