Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import {
migrateV1ToV3,
type RateLimitStateV3,
} from "./storage/migrations.js";
import {
getStoragePathState,
setStoragePathState,
} from "./storage/path-state.js";
import {
findProjectRoot,
getConfigDir,
Expand Down Expand Up @@ -345,31 +349,6 @@ async function ensureGitignore(storagePath: string): Promise<void> {
}
}

type StoragePathState = {
currentStoragePath: string | null;
currentLegacyProjectStoragePath: string | null;
currentLegacyWorktreeStoragePath: string | null;
currentProjectRoot: string | null;
};

let currentStorageState: StoragePathState = {
currentStoragePath: null,
currentLegacyProjectStoragePath: null,
currentLegacyWorktreeStoragePath: null,
currentProjectRoot: null,
};

const storagePathStateContext = new AsyncLocalStorage<StoragePathState>();

function getStoragePathState(): StoragePathState {
return storagePathStateContext.getStore() ?? currentStorageState;
}

function setStoragePathState(state: StoragePathState): void {
currentStorageState = state;
storagePathStateContext.enterWith(state);
}

export function setStorageBackupEnabled(enabled: boolean): void {
storageBackupEnabled = enabled;
}
Expand Down
30 changes: 30 additions & 0 deletions lib/storage/path-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AsyncLocalStorage } from "node:async_hooks";

export type StoragePathState = {
currentStoragePath: string | null;
currentLegacyProjectStoragePath: string | null;
currentLegacyWorktreeStoragePath: string | null;
currentProjectRoot: string | null;
};

const storagePathStateContext = new AsyncLocalStorage<StoragePathState>();

let currentStorageState: StoragePathState = {
currentStoragePath: null,
currentLegacyProjectStoragePath: null,
currentLegacyWorktreeStoragePath: null,
currentProjectRoot: null,
};

export function getStoragePathState(): StoragePathState {
// Keep the last synchronously assigned state as a fallback until enterWith()
// has propagated through the current async chain. This is intentionally a
// best-effort bridge for immediate reads; callers should still set state
// before spawning child work and treat AsyncLocalStorage as the source of truth.
return storagePathStateContext.getStore() ?? currentStorageState;
}

export function setStoragePathState(state: StoragePathState): void {
currentStorageState = state;
storagePathStateContext.enterWith(state);
}