Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Cache rename-path's tracked-file lookup (PRD-9066) (#5792)

Andrew Mikofalvy2a841ce6757a
diff --git a/.changeset/prd-9066-cache-tracked-path-lookup.md b/.changeset/prd-9066-cache-tracked-path-lookup.md
new file mode 100644
index 000000000..0e02b616a
--- /dev/null
+++ b/.changeset/prd-9066-cache-tracked-path-lookup.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Moving many files that git does not track yet, one after another, is faster, because a rename no longer asks git about each of those files separately.
diff --git a/knip.config.ts b/knip.config.ts
index d3638888e..65f0e8807 100644
--- a/knip.config.ts
+++ b/knip.config.ts
@@ -147,15 +147,11 @@ export default {
'packages/server': {
entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],
project: 'src/**',
- ignoreDependencies: [
- '@types/shell-quote',
- ],
+ ignoreDependencies: ['@types/shell-quote'],
},
'packages/cli': {
entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],
- ignoreDependencies: [
- 'yjs',
- ],
+ ignoreDependencies: ['yjs'],
},
'packages/desktop': {
entry: [
diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
index 4549c74b3..a50b00123 100644
--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
@@ -1,4 +1,3 @@
-
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
index 4fc2c11f9..6fcdc23f2 100644
--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { normalizeBridge } from '@inkeep/open-knowledge-core';
import { updateYFragment } from '@tiptap/y-tiptap';
diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.ts
index 9f2f6feb6..40b5cdfd6 100644
--- a/packages/app/tests/integration/interior-edit-freshness.test.ts
+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
index 2a085742b..93666af70 100644
--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.ts
index 878d5196b..867598d34 100644
--- a/packages/app/tests/integration/producer-guard-wiring.test.ts
+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
index 4ada3cf26..0970ac12f 100644
--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterEach, describe, expect, test } from 'vitest';
import { agentWriteMd, createTestServer, type TestServer } from './test-harness';
diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
index cbe2b705f..b632185e1 100644
--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
@@ -1,4 +1,3 @@
-
import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { join } from 'node:path';
diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
index 89bc9b4d9..c530ce4ba 100644
--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';
import type * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.ts
index a78aeb0e4..d3767adc9 100644
--- a/packages/app/tests/integration/undo-after-rollback.test.ts
+++ b/packages/app/tests/integration/undo-after-rollback.test.ts
@@ -1,4 +1,3 @@
-
import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.ts
index e7cc69c3f..b076f955c 100644
--- a/packages/app/tests/integration/undo-recycle-reset.test.ts
+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts
@@ -1,4 +1,3 @@
-
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.ts
index 9ddcd0730..7935e35f8 100644
--- a/packages/app/tests/integration/undo-rederive-survival.test.ts
+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter } from '@inkeep/open-knowledge-core';
import { Plugin, PluginKey } from '@tiptap/pm/state';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.ts
index 2a597ec3d..f4cc8a286 100644
--- a/packages/core/src/bridge/compose.ts
+++ b/packages/core/src/bridge/compose.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';
export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';
diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.ts
index 01ef76a9f..8fb428b90 100644
--- a/packages/core/src/bridge/doc-boundary-space.ts
+++ b/packages/core/src/bridge/doc-boundary-space.ts
@@ -1,4 +1,3 @@
-
import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';
import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';
diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.ts
index fb0ba5a7c..fc267cbea 100644
--- a/packages/core/src/markdown/comment-promoter.ts
+++ b/packages/core/src/markdown/comment-promoter.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.ts
index fa3cfcd86..bfd7e3109 100644
--- a/packages/core/src/markdown/merged-walker.ts
+++ b/packages/core/src/markdown/merged-walker.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Parent, Root } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.ts
index 8be632ba4..4d3250d3a 100644
--- a/packages/core/src/markdown/safe-url.ts
+++ b/packages/core/src/markdown/safe-url.ts
@@ -1,4 +1,3 @@
-
export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;
const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');
diff --git a/packages/server/src/api-extension.ts b/packages/server/src/api-extension.ts
index c1e95ad9f..942a36628 100644
--- a/packages/server/src/api-extension.ts
+++ b/packages/server/src/api-extension.ts
@@ -253,6 +253,7 @@ import {
tracedWriteFileSync,
} from './fs-traced.ts';
import { withParentLock } from './git-handle.ts';
+import { isPathTrackedInGit } from './git-tracked-paths.ts';
import { type ApiRouteTable, createApiRequestPipeline } from './http/api-pipeline.ts';
import { createAssetRoutes } from './http/asset-routes.ts';
import { createCommentRoutes } from './http/comment-routes.ts';
@@ -1227,11 +1228,14 @@ export async function renameTrackedPathInGit(
return await withParentLock(async () => {
const pg = simpleGit({ baseDir: projectDir, timeout: { block: 15_000 } });
- let tracked = '';
+ let tracked: boolean;
try {
- tracked = (await pg.raw('ls-files', ...pathspecArgs([sourceRel]))).trim();
+ tracked = await isPathTrackedInGit(pg, projectDir, sourceRel);
} catch (err) {
- log.warn({ err }, '[renameTrackedPathInGit] git ls-files failed, falling back to fs rename');
+ log.warn(
+ { err, projectDir, path: sourceRel },
+ '[renameTrackedPathInGit] tracked-path lookup failed, falling back to fs rename',
+ );
return false;
}
if (!tracked) return false;
diff --git a/packages/server/src/git-tracked-paths.test.ts b/packages/server/src/git-tracked-paths.test.ts
new file mode 100644
index 000000000..5c3b163c5
--- /dev/null
+++ b/packages/server/src/git-tracked-paths.test.ts
@@ -0,0 +1,219 @@
+import { execFileSync } from 'node:child_process';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join } from 'node:path';
+import { pathspecArgs } from '@inkeep/open-knowledge-core';
+import simpleGit, { type SimpleGit } from 'simple-git';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import { configureTestGitRepository } from '../../../test-support/configure-git-fixture.test-helper.ts';
+import { isPathTrackedInGit } from './git-tracked-paths.ts';
+
+const fixtures: string[] = [];
+
+beforeEach(() => {
+ vi.useFakeTimers({ toFake: ['Date'] });
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+ for (const fixture of fixtures.splice(0)) rmSync(fixture, { recursive: true, force: true });
+});
+
+function git(dir: string, args: string[]): string {
+ return execFileSync('git', args, { cwd: dir, encoding: 'utf8' });
+}
+
+function write(dir: string, relPath: string): void {
+ mkdirSync(dirname(join(dir, relPath)), { recursive: true });
+ writeFileSync(join(dir, relPath), `${relPath}\n`);
+}
+
+function createRepo(): string {
+ const repo = mkdtempSync(join(tmpdir(), 'ok-tracked-paths-'));
+ fixtures.push(repo);
+ git(repo, ['init', '-q', '-b', 'main']);
+ configureTestGitRepository(repo);
+ return repo;
+}
+
+function settleIndex(): void {
+ vi.setSystemTime(Date.now() + 60_000);
+}
+
+function observedGit(dir: string): { git: SimpleGit; queries: () => string[][] } {
+ const queries: string[][] = [];
+ const runner = simpleGit({ baseDir: dir }).outputHandler((_command, _stdout, _stderr, args) => {
+ if (args[0] === 'ls-files')
+ queries.push(args.filter((arg) => arg !== 'ls-files' && arg !== '-z'));
+ });
+ return { git: runner, queries: () => queries };
+}
+
+const askedAbout = (path: string): string[] => pathspecArgs([path]);
+const listedFolder = (scope: string): string[] => (scope === '' ? [] : pathspecArgs([scope]));
+
+describe('isPathTrackedInGit', () => {
+ const answerRows = [
+ { path: 'notes/tracked.md', scope: 'notes', tracked: true },
+ { path: 'notes', scope: '', tracked: true },
+ { path: 'notes/deep', scope: 'notes', tracked: true },
+ { path: 'notes/untracked.md', scope: 'notes', tracked: false },
+ { path: 'drafts', scope: '', tracked: false },
+ { path: 'drafts/loose.md', scope: 'drafts', tracked: false },
+ { path: ':colon/tracked.md', scope: ':colon', tracked: true },
+ { path: 'top.md', scope: '', tracked: true },
+ ];
+
+ function seedAnswerRepo(): string {
+ const repo = createRepo();
+ for (const file of ['notes/tracked.md', 'notes/deep/inner.md', ':colon/tracked.md', 'top.md']) {
+ write(repo, file);
+ }
+ git(repo, ['add', '--', 'notes', ':(literal):colon', 'top.md']);
+ write(repo, 'notes/untracked.md');
+ write(repo, 'drafts/loose.md');
+ return repo;
+ }
+
+ test.each(answerRows)(
+ 'answers $tracked for $path when asked alone',
+ async ({ path, tracked }) => {
+ const repo = seedAnswerRepo();
+ const { git: runner, queries } = observedGit(repo);
+
+ expect(await isPathTrackedInGit(runner, repo, path)).toBe(tracked);
+ expect(queries()).toEqual([askedAbout(path)]);
+ },
+ );
+
+ test.each(answerRows)(
+ 'answers $tracked for $path from a folder listing',
+ async ({ path, scope, tracked }) => {
+ const repo = seedAnswerRepo();
+ settleIndex();
+ const { git: runner, queries } = observedGit(repo);
+ const sibling = scope === '' ? 'absent.md' : `${scope}/absent.md`;
+
+ expect(await isPathTrackedInGit(runner, repo, sibling)).toBe(false);
+ expect(await isPathTrackedInGit(runner, repo, path)).toBe(tracked);
+ expect(queries()).toEqual([askedAbout(sibling), listedFolder(scope)]);
+ },
+ );
+
+ test('answers from a project directory below the repository root', async () => {
+ const repo = createRepo();
+ write(repo, 'project/notes/tracked.md');
+ git(repo, ['add', '--', 'project']);
+ write(repo, 'project/notes/untracked.md');
+ settleIndex();
+ const projectDir = join(repo, 'project');
+ const { git: runner, queries } = observedGit(projectDir);
+
+ expect(await isPathTrackedInGit(runner, projectDir, 'notes/tracked.md')).toBe(true);
+ expect(await isPathTrackedInGit(runner, projectDir, 'notes/untracked.md')).toBe(false);
+ expect(queries()).toEqual([askedAbout('notes/tracked.md'), listedFolder('notes')]);
+ });
+
+ test('answers a run of lookups in one folder from one listing', async () => {
+ const repo = createRepo();
+ const names = ['a', 'b', 'c', 'd', 'e'];
+ for (const name of names) write(repo, `notes/${name}.md`);
+ git(repo, ['add', '--', 'notes/a.md']);
+ settleIndex();
+ const { git: runner, queries } = observedGit(repo);
+
+ const answers = [];
+ for (const name of names)
+ answers.push(await isPathTrackedInGit(runner, repo, `notes/${name}.md`));
+
+ expect(answers).toEqual([true, false, false, false, false]);
+ expect(queries()).toEqual([askedAbout('notes/a.md'), listedFolder('notes')]);
+ });
+
+ test.each(['notes', ''])(
+ 'asks about each path alone in a run of tracked moves out of "%s"',
+ async (folder) => {
+ const repo = createRepo();
+ const paths = ['a', 'b', 'c'].map((name) =>
+ folder === '' ? `${name}.md` : `${folder}/${name}.md`,
+ );
+ for (const path of paths) write(repo, path);
+ git(repo, ['add', '--', ...paths]);
+ mkdirSync(join(repo, 'moved'));
+ const { git: runner, queries } = observedGit(repo);
+
+ const answers = [];
+ for (const path of paths) {
+ settleIndex();
+ answers.push(await isPathTrackedInGit(runner, repo, path));
+ git(repo, ['mv', '--', path, `moved/${path.replaceAll('/', '-')}`]);
+ }
+
+ expect(answers).toEqual([true, true, true]);
+ expect(queries()).toEqual(paths.map(askedAbout));
+ },
+ );
+
+ test('answers a lookup in another folder from that folder', async () => {
+ const repo = createRepo();
+ for (const path of ['notes/a.md', 'notes/b.md', 'other/tracked.md']) write(repo, path);
+ git(repo, ['add', '--', 'other/tracked.md']);
+ settleIndex();
+ const { git: runner } = observedGit(repo);
+
+ expect(await isPathTrackedInGit(runner, repo, 'notes/a.md')).toBe(false);
+ expect(await isPathTrackedInGit(runner, repo, 'notes/b.md')).toBe(false);
+ expect(await isPathTrackedInGit(runner, repo, 'other/tracked.md')).toBe(true);
+ });
+
+ test('answers as git does for a name spelled in another Unicode normalization form', async () => {
+ const repo = createRepo();
+ const composed = 'notes/café.md';
+ const decomposed = composed.normalize('NFD');
+ write(repo, 'notes/a.md');
+ write(repo, composed);
+ git(repo, ['add', ...pathspecArgs([composed])]);
+ settleIndex();
+ const { git: runner, queries } = observedGit(repo);
+ const gitAnswer = git(repo, ['ls-files', ...pathspecArgs([decomposed])]) !== '';
+
+ expect(await isPathTrackedInGit(runner, repo, 'notes/a.md')).toBe(false);
+ expect(await isPathTrackedInGit(runner, repo, decomposed)).toBe(gitAnswer);
+ expect(queries()).toEqual([askedAbout('notes/a.md'), askedAbout(decomposed)]);
+ });
+
+ test('answers from the index once another writer changes it', async () => {
+ const repo = createRepo();
+ write(repo, 'notes/seed.md');
+ git(repo, ['add', '--', 'notes/seed.md']);
+ write(repo, 'notes/later.md');
+ settleIndex();
+ const { git: runner } = observedGit(repo);
+
+ expect(await isPathTrackedInGit(runner, repo, 'notes/seed.md')).toBe(true);
+ expect(await isPathTrackedInGit(runner, repo, 'notes/later.md')).toBe(false);
+ git(repo, ['add', '--', 'notes/later.md']);
+ settleIndex();
+ expect(await isPathTrackedInGit(runner, repo, 'notes/later.md')).toBe(true);
+ });
+
+ test('asks about each path alone while the index was written within the timestamp granularity', async () => {
+ const repo = createRepo();
+ for (const name of ['a', 'b', 'c']) write(repo, `notes/${name}.md`);
+ git(repo, ['add', '--', 'notes/a.md']);
+ const { git: runner, queries } = observedGit(repo);
+
+ for (const name of ['a', 'b', 'c']) await isPathTrackedInGit(runner, repo, `notes/${name}.md`);
+
+ expect(queries()).toEqual(['a', 'b', 'c'].map((name) => askedAbout(`notes/${name}.md`)));
+ });
+
+ test('answers untracked without asking git when the repository has no index', async () => {
+ const repo = createRepo();
+ write(repo, 'notes/loose.md');
+ const { git: runner, queries } = observedGit(repo);
+
+ expect(await isPathTrackedInGit(runner, repo, 'notes/loose.md')).toBe(false);
+ expect(queries()).toEqual([]);
+ });
+});
diff --git a/packages/server/src/git-tracked-paths.ts b/packages/server/src/git-tracked-paths.ts
new file mode 100644
index 000000000..fc93d33f9
--- /dev/null
+++ b/packages/server/src/git-tracked-paths.ts
@@ -0,0 +1,81 @@
+import { statSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { pathspecArgs } from '@inkeep/open-knowledge-core';
+import type { SimpleGit } from 'simple-git';
+import { listNames } from './git-paths.ts';
+import { isRacyStat, sampleWallClockNs, statSignature } from './stat-signature.ts';
+
+interface IndexObservation {
+ signature: string;
+ racy: boolean;
+}
+
+interface LastLookup {
+ indexSignature: string;
+ scope: string;
+ listing: ReadonlySet<string> | undefined;
+}
+
+const PRINTABLE_ASCII = /^[ -~]*$/;
+
+const indexPaths = new Map<string, string>();
+const lastLookups = new Map<string, LastLookup>();
+
+function parentScope(relPath: string): string {
+ const slash = relPath.lastIndexOf('/');
+ return slash === -1 ? '' : relPath.slice(0, slash);
+}
+
+function observeIndex(indexPath: string): IndexObservation | null {
+ const sampledAtNs = sampleWallClockNs();
+ try {
+ const stats = statSync(indexPath, { bigint: true });
+ return { signature: statSignature(stats), racy: isRacyStat(stats, sampledAtNs) };
+ } catch (err) {
+ if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null;
+ throw err;
+ }
+}
+
+function withAncestorDirectories(files: readonly string[]): Set<string> {
+ const paths = new Set<string>();
+ for (const file of files) {
+ let path = file;
+ while (path !== '' && !paths.has(path)) {
+ paths.add(path);
+ path = parentScope(path);
+ }
+ }
+ return paths;
+}
+
+export async function isPathTrackedInGit(
+ git: SimpleGit,
+ projectDir: string,
+ relPath: string,
+): Promise<boolean> {
+ const projectKey = resolve(projectDir);
+ let indexPath = indexPaths.get(projectKey);
+ if (indexPath === undefined) {
+ indexPath = resolve(projectKey, (await git.raw('rev-parse', '--git-path', 'index')).trim());
+ indexPaths.set(projectKey, indexPath);
+ }
+ const index = observeIndex(indexPath);
+ if (index === null) {
+ lastLookups.delete(projectKey);
+ return false;
+ }
+ const scope = parentScope(relPath);
+ const last = lastLookups.get(projectKey);
+ const listable = !index.racy && PRINTABLE_ASCII.test(relPath);
+ if (listable && last?.indexSignature === index.signature && last.scope === scope) {
+ last.listing ??= withAncestorDirectories(
+ await listNames(git, ['ls-files', ...(scope === '' ? [] : pathspecArgs([scope]))]),
+ );
+ return last.listing.has(relPath);
+ }
+ if (listable) {
+ lastLookups.set(projectKey, { indexSignature: index.signature, scope, listing: undefined });
+ }
+ return (await listNames(git, ['ls-files', ...pathspecArgs([relPath])])).length > 0;
+}
diff --git a/packages/server/src/polled-path-watcher.ts b/packages/server/src/polled-path-watcher.ts
index e3152f8ac..e389e501c 100644
--- a/packages/server/src/polled-path-watcher.ts
+++ b/packages/server/src/polled-path-watcher.ts
@@ -3,13 +3,10 @@ import type { BigIntStats } from 'node:fs';
import { readFile, stat } from 'node:fs/promises';
import { errnoCode } from './http/handler-utils.ts';
import { getLogger } from './logger.ts';
+import { isRacyStat, sampleWallClockNs, statSignature } from './stat-signature.ts';
const POLL_INTERVAL_MS = 200;
-const TIMESTAMP_GRANULARITY_BOUND_NS = 3_000_000_000n;
-
-const NS_PER_MS = 1_000_000n;
-
type PathEvent = 'add' | 'change' | 'unlink';
interface PolledPathWatcherOptions {
@@ -30,14 +27,6 @@ const readsAsAbsent = (err: unknown): boolean => {
return code === 'ENOENT' || code === 'ENOTDIR';
};
-const signatureOf = (stats: BigIntStats): string =>
- `${stats.dev}:${stats.ino}:${stats.size}:${stats.mtimeNs}:${stats.ctimeNs}`;
-
-const isRacy = (stats: BigIntStats, sampledAtNs: bigint): boolean =>
- stats.ctimeNs + TIMESTAMP_GRANULARITY_BOUND_NS > sampledAtNs ||
- (stats.mtimeNs + TIMESTAMP_GRANULARITY_BOUND_NS > sampledAtNs &&
- stats.mtimeNs <= sampledAtNs + TIMESTAMP_GRANULARITY_BOUND_NS);
-
const unchanged = (known: Observation, observed: Observation): boolean =>
known.signature === observed.signature && (!known.racy || known.digest === observed.digest);
@@ -71,11 +60,11 @@ async function observe(
known: Observation | undefined,
isClosed: () => boolean,
): Promise<Observation | undefined> {
- const sampledAtNs = BigInt(Date.now()) * NS_PER_MS;
+ const sampledAtNs = sampleWallClockNs();
const first = await fileStats(path);
if (first === undefined) return undefined;
- if (!known?.racy && !isRacy(first, sampledAtNs)) {
- return { size: first.size, signature: signatureOf(first), racy: false, digest: undefined };
+ if (!known?.racy && !isRacyStat(first, sampledAtNs)) {
+ return { size: first.size, signature: statSignature(first), racy: false, digest: undefined };
}
if (isClosed()) return undefined;
const digest = await contentDigest(path);
@@ -84,8 +73,8 @@ async function observe(
if (second === undefined) return undefined;
return {
size: second.size,
- signature: signatureOf(second),
- racy: isRacy(second, sampledAtNs),
+ signature: statSignature(second),
+ racy: isRacyStat(second, sampledAtNs),
digest,
};
}
diff --git a/packages/server/src/stat-signature.ts b/packages/server/src/stat-signature.ts
new file mode 100644
index 000000000..90d9035aa
--- /dev/null
+++ b/packages/server/src/stat-signature.ts
@@ -0,0 +1,15 @@
+import type { BigIntStats } from 'node:fs';
+
+const TIMESTAMP_GRANULARITY_BOUND_NS = 3_000_000_000n;
+
+const NS_PER_MS = 1_000_000n;
+
+export const sampleWallClockNs = (): bigint => BigInt(Date.now()) * NS_PER_MS;
+
+export const statSignature = (stats: BigIntStats): string =>
+ `${stats.dev}:${stats.ino}:${stats.size}:${stats.mtimeNs}:${stats.ctimeNs}`;
+
+export const isRacyStat = (stats: BigIntStats, sampledAtNs: bigint): boolean =>
+ stats.ctimeNs + TIMESTAMP_GRANULARITY_BOUND_NS > sampledAtNs ||
+ (stats.mtimeNs + TIMESTAMP_GRANULARITY_BOUND_NS > sampledAtNs &&
+ stats.mtimeNs <= sampledAtNs + TIMESTAMP_GRANULARITY_BOUND_NS);