test(gbrain-sync): regression for #1611 timeouts + resume

19 tests across three surfaces:

  - resolveStageTimeoutMs (10 tests): undefined/empty → default; non-numeric,
    zero, negative, below-floor, above-ceiling → warn + default; at-floor,
    at-ceiling, valid mid-range → accepted as-is.

  - decideResume (6 tests): no checkpoint, corrupt JSON, checkpoint + staging
    ok, checkpoint + staging missing, checkpoint with no dir, checkpoint with
    empty dir.

  - SIGTERM staging preservation (3 static invariants): memory-ingest signal
    handler must check stagingDirIsCheckpointed BEFORE cleanup; preserve
    branch must come before cleanup branch (ordering); orchestrator must
    pass GSTACK_INGEST_RESUME_DIR to the grandchild on resume.

Also threads process.env.HOME through readGbrainCheckpoint and
stagingDirIsCheckpointed so tests can redirect home. os.homedir() caches
at process start and ignores later mutation, so the env override is the
only reliable test injection point.

Failing build if the timeout bounds are removed, the resume detection
short-circuits incorrectly, or the SIGTERM handler regresses to
unconditional cleanup.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-05-21 09:45:56 -07:00
parent 700c9a4ff8
commit 64a7bee176
3 changed files with 235 additions and 2 deletions
+5 -1
View File
@@ -137,7 +137,11 @@ interface GbrainCheckpoint {
}
export function readGbrainCheckpoint(): GbrainCheckpoint | null {
const cpPath = join(homedir(), ".gbrain", "import-checkpoint.json");
// Read HOME from env so tests can redirect via process.env.HOME = ...
// (Node/Bun's os.homedir() caches at process start and ignores later
// mutations.)
const home = process.env.HOME || homedir();
const cpPath = join(home, ".gbrain", "import-checkpoint.json");
if (!existsSync(cpPath)) return null;
try {
const raw = readFileSync(cpPath, "utf-8");
+3 -1
View File
@@ -1293,7 +1293,9 @@ let _signalHandlersInstalled = false;
*/
function stagingDirIsCheckpointed(stagingDir: string): boolean {
try {
const cpPath = join(homedir(), ".gbrain", "import-checkpoint.json");
// Read HOME from env so tests can redirect; homedir() caches.
const home = process.env.HOME || homedir();
const cpPath = join(home, ".gbrain", "import-checkpoint.json");
if (!existsSync(cpPath)) return false;
const raw = readFileSync(cpPath, "utf-8");
const cp = JSON.parse(raw) as { dir?: string };