|
| 1 | +// Covers the canonical worktree-GC owner liveness predicate (#104108). |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, test } from "vitest"; |
| 5 | +import { IDLE_GC_MS } from "../agents/worktrees/service.js"; |
| 6 | +import { resetConfigRuntimeState, setRuntimeConfigSnapshot } from "../config/config.js"; |
| 7 | +import type { OpenClawConfig } from "../config/config.js"; |
| 8 | +import { withStateDirEnv } from "../test-helpers/state-dir-env.js"; |
| 9 | +import { isManagedWorktreeOwnerActive } from "./worktree-owner-activity.js"; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + resetConfigRuntimeState(); |
| 13 | +}); |
| 14 | + |
| 15 | +describe("isManagedWorktreeOwnerActive", () => { |
| 16 | + test("keeps live session owners active and idle-expired ones inactive", async () => { |
| 17 | + await withStateDirEnv("worktree-owner-activity-", async ({ stateDir }) => { |
| 18 | + const sessionsDir = path.join(stateDir, "agents", "main", "sessions"); |
| 19 | + fs.mkdirSync(sessionsDir, { recursive: true }); |
| 20 | + const now = Date.now(); |
| 21 | + fs.writeFileSync( |
| 22 | + path.join(sessionsDir, "sessions.json"), |
| 23 | + JSON.stringify({ |
| 24 | + "agent:main:live": { sessionId: "sess-live", lastInteractionAt: now, updatedAt: now }, |
| 25 | + "agent:main:stale": { |
| 26 | + sessionId: "sess-stale", |
| 27 | + lastInteractionAt: now - IDLE_GC_MS - 60_000, |
| 28 | + updatedAt: now - IDLE_GC_MS - 60_000, |
| 29 | + }, |
| 30 | + }), |
| 31 | + "utf8", |
| 32 | + ); |
| 33 | + const cfg = { |
| 34 | + session: { |
| 35 | + mainKey: "main", |
| 36 | + store: path.join(stateDir, "agents", "{agentId}", "sessions", "sessions.json"), |
| 37 | + }, |
| 38 | + agents: { list: [{ id: "main", default: true }] }, |
| 39 | + } as OpenClawConfig; |
| 40 | + setRuntimeConfigSnapshot(cfg, cfg); |
| 41 | + |
| 42 | + expect(isManagedWorktreeOwnerActive("session", "agent:main:live")).toBe(true); |
| 43 | + expect(isManagedWorktreeOwnerActive("session", "agent:main:stale")).toBe(false); |
| 44 | + // Non-session owners rely on the git-lock fallback inside gc(), never |
| 45 | + // on session activity. |
| 46 | + expect(isManagedWorktreeOwnerActive("manual", "agent:main:live")).toBe(false); |
| 47 | + // Unknown sessions are not treated as live. |
| 48 | + expect(isManagedWorktreeOwnerActive("session", "agent:main:missing")).toBe(false); |
| 49 | + }); |
| 50 | + }); |
| 51 | +}); |
0 commit comments