mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-09-30 12:39:42 +02:00
* fix(cleanup): keep .claude-images while a sibling session uses the same dir
cleanupSession() recursively removes {workingDir}/.claude-images. That
directory belongs to the working directory rather than to the session, and
several sessions routinely share one case directory, so closing one session
deleted the pasted images a live sibling still referred to.
The removal now runs only when no other live session has the same working
directory. A session that is itself being cleaned up does not count as live,
so two sessions of one case closed together still remove the dir.
Split out ahead of the exited-agent sweep for Ark0N/Codeman#446, which closes
sessions unattended and would otherwise make the loss routine.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* feat(session): close sessions whose agent exited cleanly (#446)
Part 2 of Ark0N/Codeman#446. Part 1 records an exited agent as
SessionState.paneExit. A session whose agent the user ended with /exit is
now closed through cleanupSession(), the same path the X button takes, so
finished sessions stop piling up on the board. The lifecycle log records
the reason as "agent exited cleanly (status 0)", and the conversation stays
resumable from the Resume list.
shouldCloseCleanlyExitedSession() in the new pure module pane-exit-sweep.ts
holds the rule. It closes a session only when all of these hold:
- The exit status is an explicit numeric 0 with no signal. An absent status
is how a SIGKILL presents on tmux 3.2a, so it counts as unknown and the
row stays. A non-zero status or any signal also keeps the row, with the
exit code on the tab.
- Two authoritative pane reads agreed on that exit.
TmuxManager.getPaneExitReadCount() counts them, and a failed, empty or
skipped read neither confirms nor resets the count.
- No start, attach or relaunch is running for the pane.
Session.paneLifecycleInFlight covers _setupOrAttachMuxSession(), whose
dead-pane branch revives an exited pane on purpose, and restartCli().
setPaneExit() already scopes paneExit to local mux-backed sessions, so
remote, docker and direct-PTY sessions are never closed.
planRebootRestore() now refuses a record whose persisted paneExit is a
clean exit. That covers an agent that exited just before a reboot, before
the sweep reached it. A crashed agent's record stays eligible, like its row.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(web): show "exited" on the phone overview and desktop home rail (#446)
Part 1 of Ark0N/Codeman#446 taught the tab strip and the rich rail rows
to say that a session's agent has exited. The phone overview and the
desktop home rail still said "idle", beside a green or pulsing dot.
_mobileOverviewExit() in mobile-overview.js is now the one rule for all
three surfaces, and _sidebarRichRow() uses it as well. It changes what a
row shows and leaves the row's state alone, because the state still picks
the section and the sort order. An exited row gets an "exited" pill, a
neutral dot and row accent, and a duration measured from when the server
first saw the pane dead. A pending permission prompt or question still
wins, as it does on the tab.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(cleanup): close the gaps review found in the #446 sweep and image guard
Four fixes from a dual review of Ark0N/Codeman#446 part 2.
- The .claude-images guard compares canonical paths, so a sibling that
reaches the same directory through a symlink keeps it. Its comment used to
say that case only missed a deletion; it caused one.
- A detached session counts as a live sibling. DELETE ?killMux=false removes
it from the server's map while its pane keeps running, so the guard now
reads persisted records too, and exempts only sessions being killed rather
than every session in cleaningUp.
- A session being closed refuses startInteractive() and startShell(). The
/interactive route awaits listener setup before the start, and a start
that raced the close could launch a CLI in a tmux session whose record was
then deleted. A failed close clears the mark again.
- The clean-exit sweep tries each exit once, keyed by session id and the
exit's at stamp, so a close that fails is not retried and logged every
two seconds.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(session): keep a clean exit that lands within 10 s of a pane start (#446)
A CLI that prints a startup error ("not logged in", a bad profile, a config
error) and exits 0 used to lose its tab, and the error with it, about 4 s
after launch. The sweep now keeps any clean exit that lands within
CLEAN_EXIT_MIN_PANE_LIFETIME_MS (10 s) of the last start, attach or relaunch
finishing (Session.paneStartedAt, stamped when _withPaneLifecycle ends). The
row stays as "exited (0)" for the user to read and close.
Verified on an isolated instance: a shell that ran `exit 0` 2 s after start
kept its row, one that exited after 13 s was closed.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
166 lines
6.0 KiB
TypeScript
166 lines
6.0 KiB
TypeScript
/**
|
|
* @fileoverview Deleting a session keeps `.claude-images` while a sibling in the
|
|
* same working directory is still live (Ark0N/Codeman#446).
|
|
*
|
|
* `cleanupSession()` removes `{workingDir}/.claude-images` recursively. That
|
|
* dir belongs to the working directory, not to the session, and several
|
|
* sessions routinely share one case directory, so closing one used to delete
|
|
* the pasted images a live sibling still referred to. The exited-agent sweep
|
|
* closes sessions unattended, which turns that from an occasional loss into a
|
|
* routine one.
|
|
*
|
|
* Port: 3188
|
|
*/
|
|
import { existsSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
|
import { WebServer } from '../src/web/server.js';
|
|
import { pasteImageDirInUseByOtherSession } from '../src/web/paste-image-gc.js';
|
|
|
|
const PORT = 3188;
|
|
|
|
describe('pasteImageDirInUseByOtherSession', () => {
|
|
const none = new Set<string>();
|
|
const check = (
|
|
live: Array<{ id: string; workingDir: string }>,
|
|
opts: { persisted?: Array<{ id: string; workingDir: string; status?: string }>; killing?: Set<string> } = {}
|
|
) =>
|
|
pasteImageDirInUseByOtherSession({
|
|
live,
|
|
persisted: opts.persisted ?? [],
|
|
closingId: 'a',
|
|
workingDir: '/work/case',
|
|
killing: opts.killing ?? none,
|
|
});
|
|
|
|
it('finds a live sibling in the same working directory', () => {
|
|
expect(
|
|
check([
|
|
{ id: 'a', workingDir: '/work/case' },
|
|
{ id: 'b', workingDir: '/work/case' },
|
|
])
|
|
).toBe(true);
|
|
});
|
|
|
|
it('ignores the session being closed', () => {
|
|
expect(check([{ id: 'a', workingDir: '/work/case' }])).toBe(false);
|
|
});
|
|
|
|
it('ignores sessions in other directories, including a subdirectory', () => {
|
|
expect(
|
|
check([
|
|
{ id: 'a', workingDir: '/work/case' },
|
|
{ id: 'b', workingDir: '/work/other' },
|
|
{ id: 'c', workingDir: '/work/case/sub' },
|
|
])
|
|
).toBe(false);
|
|
});
|
|
|
|
it('normalises a trailing slash and dot segments', () => {
|
|
expect(check([{ id: 'b', workingDir: '/work/x/../case/' }])).toBe(true);
|
|
});
|
|
|
|
it('does not count a sibling that is being killed too', () => {
|
|
// Two sessions of one case killed together must not each defer to the
|
|
// other, or neither removes the dir.
|
|
expect(check([{ id: 'b', workingDir: '/work/case' }], { killing: new Set(['a', 'b']) })).toBe(false);
|
|
});
|
|
|
|
it('counts a detached session, which left the map but still runs in tmux', () => {
|
|
// `DELETE ?killMux=false` removes the session from the server's map and
|
|
// keeps its persisted record and its pane.
|
|
expect(check([], { persisted: [{ id: 'b', workingDir: '/work/case', status: 'idle' }] })).toBe(true);
|
|
});
|
|
|
|
it('does not count a persisted record demoted to stopped, or the closing session', () => {
|
|
expect(
|
|
check([], {
|
|
persisted: [
|
|
{ id: 'a', workingDir: '/work/case', status: 'idle' },
|
|
{ id: 'b', workingDir: '/work/case', status: 'stopped' },
|
|
],
|
|
})
|
|
).toBe(false);
|
|
});
|
|
|
|
it('matches a sibling that reaches the same directory through a symlink', () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'codeman-paste-link-'));
|
|
try {
|
|
const real = join(root, 'case');
|
|
mkdirSync(real);
|
|
symlinkSync(real, join(root, 'current'));
|
|
expect(
|
|
pasteImageDirInUseByOtherSession({
|
|
live: [{ id: 'b', workingDir: join(root, 'current') }],
|
|
persisted: [],
|
|
closingId: 'a',
|
|
workingDir: real,
|
|
killing: none,
|
|
})
|
|
).toBe(true);
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('deleting a session that shares its working directory', () => {
|
|
let server: WebServer;
|
|
let workingDir: string;
|
|
const base = `http://localhost:${PORT}`;
|
|
|
|
beforeAll(async () => {
|
|
workingDir = mkdtempSync(join(tmpdir(), 'codeman-paste-shared-'));
|
|
server = new WebServer(PORT, false, true);
|
|
await server.start();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await server.stop();
|
|
rmSync(workingDir, { recursive: true, force: true });
|
|
}, 60000);
|
|
|
|
const create = async (): Promise<string> => {
|
|
const res = await fetch(`${base}/api/sessions`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ workingDir }),
|
|
});
|
|
const body = await res.json();
|
|
return body.data.session.id as string;
|
|
};
|
|
|
|
const remove = (id: string) => fetch(`${base}/api/sessions/${id}`, { method: 'DELETE' });
|
|
|
|
it('keeps the images while a sibling is live, and removes them with the last session', async () => {
|
|
const first = await create();
|
|
const second = await create();
|
|
const imageDir = join(workingDir, '.claude-images');
|
|
mkdirSync(imageDir, { recursive: true });
|
|
writeFileSync(join(imageDir, 'paste-1.png'), 'x');
|
|
|
|
expect((await remove(first)).status).toBe(200);
|
|
expect(existsSync(join(imageDir, 'paste-1.png'))).toBe(true);
|
|
|
|
expect((await remove(second)).status).toBe(200);
|
|
expect(existsSync(imageDir)).toBe(false);
|
|
});
|
|
|
|
it('keeps the images while a sibling is only detached, since it still runs in tmux', async () => {
|
|
const detached = await create();
|
|
const deleted = await create();
|
|
// Persisting a new record is debounced, and a detach cancels the pending
|
|
// write, so wait for the record a long-running session would already have.
|
|
const store = (server as unknown as { store: { getSession: (id: string) => unknown } }).store;
|
|
await vi.waitFor(() => expect(store.getSession(detached)).toBeTruthy(), { timeout: 10_000 });
|
|
const imageDir = join(workingDir, '.claude-images');
|
|
mkdirSync(imageDir, { recursive: true });
|
|
writeFileSync(join(imageDir, 'paste-2.png'), 'x');
|
|
|
|
expect((await fetch(`${base}/api/sessions/${detached}?killMux=false`, { method: 'DELETE' })).status).toBe(200);
|
|
expect((await remove(deleted)).status).toBe(200);
|
|
expect(existsSync(join(imageDir, 'paste-2.png'))).toBe(true);
|
|
});
|
|
});
|