mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-06 23:49:41 +02:00
feat(agent-cases): tag agent-spawned case dirs and sweep their leftovers
A long orchestration creates one case directory per worker and deleting the sessions never removed them, so ~/codeman-cases accumulated scratch folders that were indistinguishable from real projects. They are now labelled and have a cleanup path. - src/agent-case-marker.ts: a case dir quick-start CREATES for an agent-driven spawn gets a .codeman-agent-case.json marker (when, by whom, parent session, mode). Only the create branch writes it, so a linked case, a cloned repo or any pre-existing path is never labelled; reading is total, so a malformed marker means "not agent-created" rather than a half-trusted entry. - The signal is the new X-Codeman-Agent-Origin header the skill preamble sets on its shared curl (preamble bumped to 1.22.0), or an agentOrigin body field, falling back to a resolved parentSessionId so a worker spawned by a stale skill copy is still labelled. - GET /api/cases publishes it as agentCreated; GET /api/cases/agent-created is a read-only cleanup listing adding inUse and modifiedAt; Add Case -> Manage badges each case and offers a review-then-delete sweep that names every directory in its confirm and skips any case a live session is working in. Removal stays on the existing DELETE /api/cases/:name. - Agent preamble caches are collected too: ~/.cache/codeman-agent-<id>.sh was written per claude session and never removed (236 leftovers measured on a working machine). Now deleted with the session and swept at boot, guarded by a live-session keep set plus a 7-day age floor. Verified end to end on an isolated instance: marker written for header, body and lineage-only spawns, absent with no agent signal and for a pre-existing directory; inUse flipping on session end; badge, sticky bar, confirm and sweep driven in a browser; preamble seeded on create, removed on delete, boot sweep taking only the aged orphans. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtemp, rm, mkdir, writeFile, readFile, symlink, readdir, stat } from 'node:fs/promises';
|
||||
import { mkdtemp, rm, mkdir, writeFile, readFile, symlink, readdir, stat, utimes } from 'node:fs/promises';
|
||||
import { existsSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir, homedir } from 'node:os';
|
||||
@@ -21,10 +21,25 @@ import {
|
||||
removeAgentSkillFrom,
|
||||
refreshUserAgentSkill,
|
||||
seedAgentSessionPreamble,
|
||||
removeAgentSessionPreamble,
|
||||
pruneAgentSessionPreambles,
|
||||
} from '../src/hooks-config.js';
|
||||
|
||||
const MARKER_PREFIX = '<!-- codeman-managed-agent-skill';
|
||||
|
||||
/** Run `fn` against a throwaway XDG cache dir, restoring the env afterwards. */
|
||||
async function withCacheDir(fn: (cacheDir: string) => Promise<void>): Promise<void> {
|
||||
const prevXdg = process.env.XDG_CACHE_HOME;
|
||||
const cacheDir = join(casePath, 'xdg-cache');
|
||||
process.env.XDG_CACHE_HOME = cacheDir;
|
||||
try {
|
||||
await fn(cacheDir);
|
||||
} finally {
|
||||
if (prevXdg === undefined) delete process.env.XDG_CACHE_HOME;
|
||||
else process.env.XDG_CACHE_HOME = prevXdg;
|
||||
}
|
||||
}
|
||||
|
||||
let casePath: string;
|
||||
const skillDir = () => join(casePath, '.claude', 'skills', 'codeman');
|
||||
|
||||
@@ -165,6 +180,57 @@ describe('preamble single-source (seed + §0 heredoc parity)', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('removeAgentSessionPreamble drops one session cache and shrugs at a missing one', async () => {
|
||||
// Seeding writes one file per claude session and nothing used to remove them
|
||||
// (236 leftovers measured on a working machine); session teardown calls this.
|
||||
await withCacheDir(async (cacheDir) => {
|
||||
await seedAgentSessionPreamble('gone-session');
|
||||
expect(existsSync(join(cacheDir, 'codeman-agent-gone-session.sh'))).toBe(true);
|
||||
|
||||
await removeAgentSessionPreamble('gone-session');
|
||||
expect(existsSync(join(cacheDir, 'codeman-agent-gone-session.sh'))).toBe(false);
|
||||
|
||||
await expect(removeAgentSessionPreamble('never-existed')).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it('pruneAgentSessionPreambles takes only aged caches with no live session behind them', async () => {
|
||||
await withCacheDir(async (cacheDir) => {
|
||||
const aged = (name: string) => join(cacheDir, name);
|
||||
for (const id of ['live-old', 'dead-old', 'dead-fresh']) {
|
||||
await seedAgentSessionPreamble(id);
|
||||
}
|
||||
// Age two of them past the cutoff; `dead-fresh` stays new.
|
||||
const old = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
||||
await utimes(aged('codeman-agent-live-old.sh'), old, old);
|
||||
await utimes(aged('codeman-agent-dead-old.sh'), old, old);
|
||||
// An unrelated file in the same cache dir must be invisible to the sweep.
|
||||
await writeFile(aged('someone-elses-file.sh'), 'not ours\n');
|
||||
|
||||
const removed = await pruneAgentSessionPreambles(['live-old']);
|
||||
|
||||
expect(removed).toBe(1);
|
||||
expect(existsSync(aged('codeman-agent-dead-old.sh'))).toBe(false);
|
||||
// A live session's cache is load-bearing: the skill's two-line loader reads it
|
||||
// mid-run, so age alone must never take it.
|
||||
expect(existsSync(aged('codeman-agent-live-old.sh'))).toBe(true);
|
||||
// And a recently-seeded one belongs to a session this process may not know about.
|
||||
expect(existsSync(aged('codeman-agent-dead-fresh.sh'))).toBe(true);
|
||||
expect(existsSync(aged('someone-elses-file.sh'))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('pruneAgentSessionPreambles reports 0 rather than throwing when there is no cache dir', async () => {
|
||||
const prevXdg = process.env.XDG_CACHE_HOME;
|
||||
process.env.XDG_CACHE_HOME = join(casePath, 'no-such-cache');
|
||||
try {
|
||||
expect(await pruneAgentSessionPreambles([])).toBe(0);
|
||||
} finally {
|
||||
if (prevXdg === undefined) delete process.env.XDG_CACHE_HOME;
|
||||
else process.env.XDG_CACHE_HOME = prevXdg;
|
||||
}
|
||||
});
|
||||
|
||||
it('seedAgentSessionPreamble falls back to ~/.cache when XDG_CACHE_HOME is unset', async () => {
|
||||
const prevXdg = process.env.XDG_CACHE_HOME;
|
||||
delete process.env.XDG_CACHE_HOME;
|
||||
|
||||
Reference in New Issue
Block a user