2026-02-28 19:12:45 +01:00
/**
* @fileoverview Case management routes.
* Handles CRUD for cases (directories under ~/codeman-cases and linked folders),
2026-08-09 16:33:01 +02:00
* cloning a repository into a new case (`/api/cases/clone` + `/clone-preflight`,
* issue #236 — the URL-safety rules live in `src/git-clone.ts`), fix-plan reading,
* and ralph-wizard file serving.
2026-02-28 19:12:45 +01:00
*/
import { FastifyInstance } from 'fastify' ;
2026-08-10 00:07:01 +02:00
import { existsSync , lstatSync , mkdirSync , writeFileSync , readdirSync , readFileSync , createReadStream } from 'node:fs' ;
2026-07-19 15:28:44 +02:00
import { exec } from 'node:child_process' ;
2026-02-28 19:12:45 +01:00
import fs from 'node:fs/promises' ;
2026-07-19 17:38:01 +02:00
import { join , resolve , basename } from 'node:path' ;
import { fileURLToPath } from 'node:url' ;
2026-02-28 19:12:45 +01:00
import { homedir } from 'node:os' ;
2026-09-07 19:09:24 +02:00
import type {
AgentCaseSummary ,
ApiResponse ,
CaseInfo ,
DockerHost ,
RemoteSessionInfo ,
SessionDocker ,
SessionMode ,
} from '../../types.js' ;
2026-02-28 19:12:45 +01:00
import { ApiErrorCode , createErrorResponse , getErrorMessage } from '../../types.js' ;
2026-06-08 10:40:56 +00:00
import {
CreateCaseSchema ,
2026-08-09 16:33:01 +02:00
CloneCaseSchema ,
ClonePreflightSchema ,
2026-06-08 10:40:56 +00:00
LinkCaseSchema ,
CaseOrderSchema ,
RemoteCaseLinkSchema ,
RemoteHostSchema ,
2026-07-19 15:28:44 +02:00
DockerCaseLinkSchema ,
2026-08-29 21:02:19 -07:00
DockerCaseAdoptSchema ,
DockerAdoptPreflightSchema ,
2026-08-29 23:31:28 -07:00
DockerBrowseSchema ,
2026-07-19 15:28:44 +02:00
DockerHostSchema ,
2026-07-19 17:38:01 +02:00
DockerExportSchema ,
DockerImportSchema ,
2026-07-19 21:35:38 +02:00
DockerQuickCreateSchema ,
2026-06-08 10:40:56 +00:00
} from '../schemas.js' ;
2026-07-19 17:38:01 +02:00
import { exportDockerCase , importDockerBundle , listDockerExports , exportBundleName } from '../../docker-export.js' ;
2026-08-09 16:33:01 +02:00
import {
cloneRepository ,
isGitAvailable ,
isSafeGitRef ,
parseGitRepositoryUrl ,
probeGitRemote ,
} from '../../git-clone.js' ;
import type { GitRemoteProbe , GitUrlParse } from '../../git-clone.js' ;
2026-02-28 19:12:45 +01:00
import { generateClaudeMd } from '../../templates/claude-md.js' ;
2026-10-03 13:54:16 +08:00
import { prepareNewCasePath } from '../case-path.js' ;
2026-10-05 20:00:03 +02:00
import { boundedPathExists , describeUnknownPath , probePath } from '../../utils/index.js' ;
2026-09-07 19:09:24 +02:00
import { readAgentCaseMarker , type AgentCaseMarker } from '../../agent-case-marker.js' ;
2026-08-10 00:07:01 +02:00
import { settingsWriteBlocker , writeHooksConfig } from '../../hooks-config.js' ;
2026-07-20 04:02:46 +02:00
import {
canAccessOwned ,
getAuthUser ,
isAdmin ,
2026-07-20 12:33:12 +02:00
isWorkingDirAllowed ,
2026-07-20 04:02:46 +02:00
ownerFor ,
resolveCasesDir ,
SETTINGS_PATH ,
validatePathWithinBase ,
parseBody ,
readJsonConfig ,
} from '../route-helpers.js' ;
2026-07-20 12:33:12 +02:00
import { isMultiUserMode } from '../../config/multiuser.js' ;
2026-07-20 04:02:46 +02:00
import type { AuthUser } from '../../types.js' ;
2026-03-03 22:40:18 +01:00
import { SseEvent } from '../sse-events.js' ;
2026-07-20 02:36:02 +02:00
import type { EventPort , ConfigPort , SessionPort } from '../ports/index.js' ;
2026-07-20 04:02:46 +02:00
import type { FastifyRequest } from 'fastify' ;
2026-06-06 03:53:54 +02:00
import { dataPath , getDataDir } from '../../config/instance.js' ;
2026-07-19 15:28:44 +02:00
import {
checkDockerAvailable ,
2026-07-20 01:28:53 +02:00
checkDockerImagePresent ,
2026-07-19 15:28:44 +02:00
checkDockerTmuxAvailable ,
2026-07-20 01:28:53 +02:00
ensureAgentBaseImage ,
2026-07-19 19:28:13 +02:00
DEFAULT_AGENT_IMAGE ,
2026-07-19 15:28:44 +02:00
dockerContainerName ,
dockerDisplayPath ,
2026-08-29 21:02:19 -07:00
probeAdoptableContainer ,
2026-09-01 05:05:13 -07:00
classifyAdoptContainerConflict ,
2026-08-29 21:02:19 -07:00
listDockerContainers ,
2026-08-29 23:31:28 -07:00
browseInContainer ,
2026-09-05 16:21:51 +02:00
dockerAdoptProbeModes ,
2026-07-19 15:28:44 +02:00
readDockerCases ,
readDockerHosts ,
2026-07-20 02:36:02 +02:00
removeDockerContainer ,
2026-07-19 15:28:44 +02:00
toSessionDocker ,
writeDockerCases ,
writeDockerHosts ,
} from '../../docker-hosts.js' ;
2026-08-29 23:31:28 -07:00
import type { AdoptedContainerProbe , DockerBrowseResult , DockerContainerInfo } from '../../docker-hosts.js' ;
2026-07-19 15:28:44 +02:00
import { buildDockerRemoveCommand } from '../../tmux-manager.js' ;
2026-06-08 10:40:56 +00:00
import {
2026-07-12 19:49:39 +02:00
checkRemoteTmuxAvailable ,
2026-06-13 11:04:18 -04:00
listRemoteCodemanSessions ,
2026-06-08 10:40:56 +00:00
readRemoteCases ,
readRemoteHosts ,
remoteDisplayPath ,
writeRemoteCases ,
writeRemoteHosts ,
} from '../../remote-hosts.js' ;
2026-02-28 19:12:45 +01:00
2026-06-06 03:53:54 +02:00
const LINKED_CASES_FILE = dataPath ( 'linked-cases.json' );
2026-06-08 10:40:56 +00:00
const CODEMAN_CONFIG_DIR = getDataDir ();
2026-04-03 03:58:29 +02:00
const SAFE_CASE_NAME = /^[a-zA-Z0-9_-]+$/ ;
2026-07-19 17:38:01 +02:00
const DOCKER_EXPORTS_DIR = dataPath ( 'docker-exports' );
2026-07-19 19:28:13 +02:00
/** Auto-created host profile for the one-click "Run in Docker" case flow. */
const DEFAULT_DOCKER_HOST_ID = 'default' ;
2026-07-19 17:38:01 +02:00
/** App version for export manifests (best-effort read of package.json). */
const APP_VERSION = (() => {
try {
const pkgPath = fileURLToPath ( new URL ( '../../../package.json' , import . meta . url ));
return ( JSON . parse ( readFileSync ( pkgPath , 'utf-8' )). version as string ) || 'unknown' ;
} catch {
return 'unknown' ;
}
})();
2026-03-15 03:47:18 +01:00
2026-08-09 16:33:01 +02:00
/**
* Refusal text for a `local`-transport clone by a non-admin in multi-user mode.
* Per-user case spaces live inside one $HOME, so cloning from an absolute path
* would copy another user's workspace into the caller's own (the same escape
* `/api/cases/link` is admin-only for).
*/
const LOCAL_CLONE_ADMIN_ONLY =
'Cloning from a local path is admin-only in multi-user mode. Use a repository URL instead.' ;
2026-09-23 14:44:26 +08:00
/**
* Whether a clone or preflight must run with git's credential helpers cleared:
* a non-admin in multi-user mode. Every user's git runs as the one server
* account, so its helpers (the Docker image's opt-in `gh`/`az` ones, or any
* `gh auth setup-git`) would otherwise read a private repository with the
* signed-in admin's credentials, the same boundary the local-transport rule
* above guards. Admins and single-user mode keep the account's own helpers.
*/
export function cloneWithoutCredentialHelpers ( req : FastifyRequest ) : boolean {
return isMultiUserMode () && ! isAdmin ( req );
}
2026-08-09 16:33:01 +02:00
/**
* The one line of git's stderr worth appending to an error message.
*
* NOT the first line: `git clone` opens with "Cloning into '<dest>'…", so a naive
* first-line pick reported the destination path as the reason a bad branch failed
* (observed against a real remote). Prefer the LAST diagnostic line
* (`fatal:`/`error:`/`remote:`), which is where git puts the actual cause.
*/
function gitDiagnosticLine ( stderr : string ) : string {
const lines = stderr
. split ( '\n' )
. map (( l ) => l . trim ())
. filter ( Boolean );
const line = [... lines ]. reverse (). find (( l ) => /^(fatal|error|remote|warning):/i . test ( l )) ?? lines . at ( - 1 ) ?? '' ;
return line . length > 200 ? ` ${ line . slice ( 0 , 200 ) } …` : line ;
}
/**
* Does the freshly cloned tree carry its own Claude settings? Those can contain
* hooks, which run on the user's machine when a session starts in the case, so
* the clone response says so out loud instead of silently merging into them.
*/
function repoShipsClaudeSettings ( casePath : string ) : boolean {
2026-10-04 20:30:40 -04:00
// Deliberately NOT the bounded path probe: the tree was just cloned into the
// local case space (and lstat'ed synchronously moments ago), so a bound protects
// nothing here, while a probe answering "unknown" could silently drop this warning.
2026-08-09 16:33:01 +02:00
return [ 'settings.json' , 'settings.local.json' ]. some (( file ) => existsSync ( join ( casePath , '.claude' , file )));
}
2026-09-07 19:09:24 +02:00
/**
* Project a case's marker onto the wire shape `CaseInfo.agentCreated` carries.
* `owner` stays server-side: the listings are already owner-scoped, and it is not
* something the case list needs to publish.
*/
function agentCreatedInfo ( marker : AgentCaseMarker ) : NonNullable < CaseInfo [ 'agentCreated' ] > {
return {
createdAt : marker.createdAt ,
createdBy : marker.createdBy ,
...( marker . parentSessionId ? { parentSessionId : marker.parentSessionId } : {}),
...( marker . parentSessionName ? { parentSessionName : marker.parentSessionName } : {}),
...( marker . mode ? { mode : marker.mode } : {}),
};
}
2026-03-15 03:47:18 +01:00
/** Read and parse linked-cases.json, returning empty object on missing/invalid file. */
async function readLinkedCases () : Promise < Record < string , string >> {
2026-03-25 23:32:28 +01:00
return readJsonConfig < Record < string , string >>( LINKED_CASES_FILE , 'linked cases' , {});
2026-03-15 03:47:18 +01:00
}
2026-07-20 04:02:46 +02:00
/**
* Resolve a case name to its directory path, checking linked cases first, then the
* user's case space (per-user in multi-user mode, the shared CASES_DIR otherwise).
*/
async function resolveCasePath ( name : string , user? : AuthUser ) : Promise < string > {
2026-03-15 03:47:18 +01:00
const linkedCases = await readLinkedCases ();
2026-07-20 12:33:12 +02:00
// Linked cases carry no owner (legacy/admin-only registry): a non-admin must not
// resolve arbitrary linked paths by name in multi-user mode (path-escape guard).
if ( linkedCases [ name ] && ( ! isMultiUserMode () || user ? . role === 'admin' )) return linkedCases [ name ];
2026-07-20 04:02:46 +02:00
return join ( resolveCasesDir ( user ), name );
2026-03-15 03:47:18 +01:00
}
2026-07-20 01:28:53 +02:00
/**
* Gate a docker case on its base image, AUTO-BUILDING the default image on first
* use so a missing image is never a blocker (the user's ask: "create it when it's
* used for the first time"). Present image → verify tmux (hard prerequisite).
* Default image missing → kick off a BACKGROUND build with SSE progress and return
* `imageBuilding: true` (the case is created regardless; first launch awaits the
* same dedup'd build). Custom image missing → a real error (we can't build a
* foreign ref, and `--pull=never` forbids pulling).
*/
async function ensureCaseImage (
broadcast : EventPort [ 'broadcast' ],
sessionDocker : SessionDocker ,
name : string
) : Promise < { ok : true ; imageBuilding : boolean } | { ok : false ; error : string } > {
2026-07-20 02:36:02 +02:00
if ( await checkDockerImagePresent ( sessionDocker , sessionDocker . image )) {
2026-07-20 01:28:53 +02:00
const tmuxCheck = await checkDockerTmuxAvailable ( sessionDocker );
if ( ! tmuxCheck . ok ) return { ok : false , error : tmuxCheck.error || 'base image is missing tmux' };
return { ok : true , imageBuilding : false };
}
if ( sessionDocker . image !== DEFAULT_AGENT_IMAGE ) {
return {
ok : false ,
error : `base image ${ sessionDocker . image } not present; only ${ DEFAULT_AGENT_IMAGE } is auto-built. Build or pull it first.` ,
};
}
broadcast ( SseEvent . DockerImageBuildStarted , { name , image : sessionDocker.image });
2026-07-20 02:36:02 +02:00
void ensureAgentBaseImage ( sessionDocker , sessionDocker . image , {
2026-07-20 01:28:53 +02:00
onProgress : ( line ) => broadcast ( SseEvent . DockerImageBuildProgress , { name , line }),
})
. then (( r ) =>
broadcast ( r . ok ? SseEvent.DockerImageBuildComplete : SseEvent.DockerImageBuildFailed , {
name ,
image : sessionDocker.image ,
error : r.error ,
})
)
. catch (( err ) =>
broadcast ( SseEvent . DockerImageBuildFailed , { name , image : sessionDocker.image , error : getErrorMessage ( err ) })
);
return { ok : true , imageBuilding : true };
}
2026-07-20 02:36:02 +02:00
export function registerCaseRoutes ( app : FastifyInstance , ctx : EventPort & ConfigPort & SessionPort ) : void {
2026-03-03 22:40:18 +01:00
// ═══════════════════════════════════════════════════════════════
// Case CRUD (list, create, link, detail, fix-plan)
// ═══════════════════════════════════════════════════════════════
// ========== List Cases ==========
2026-02-28 19:12:45 +01:00
2026-07-20 04:02:46 +02:00
app . get ( '/api/cases' , async ( req ) : Promise < CaseInfo [] > => {
2026-02-28 19:12:45 +01:00
const cases : CaseInfo [] = [];
2026-07-20 04:02:46 +02:00
const user = getAuthUser ( req );
const admin = isAdmin ( req );
// Non-admins enumerate their OWN case space; admins see the shared CASES_DIR.
const listBase = resolveCasesDir ( user );
2026-02-28 19:12:45 +01:00
2026-07-20 04:02:46 +02:00
// Get cases from the user's (or shared) cases dir
2026-02-28 19:12:45 +01:00
try {
2026-07-20 04:02:46 +02:00
const entries = await fs . readdir ( listBase , { withFileTypes : true });
2026-02-28 19:12:45 +01:00
for ( const e of entries ) {
2026-04-03 03:58:29 +02:00
if ( e . isDirectory () && SAFE_CASE_NAME . test ( e . name )) {
2026-09-07 19:09:24 +02:00
const casePath = join ( listBase , e . name );
// Only a directory Codeman scaffolded for an agent spawn carries a marker,
// so this stays absent for every human-created, linked or cloned case.
const marker = await readAgentCaseMarker ( casePath );
2026-02-28 19:12:45 +01:00
cases . push ({
name : e.name ,
2026-09-07 19:09:24 +02:00
path : casePath ,
2026-10-01 13:58:55 -04:00
hasClaudeMd : await boundedPathExists ( join ( casePath , 'CLAUDE.md' )),
2026-06-08 10:40:56 +00:00
location : 'local' ,
2026-09-07 19:09:24 +02:00
...( marker ? { agentCreated : agentCreatedInfo ( marker ) } : {}),
2026-02-28 19:12:45 +01:00
});
}
}
} catch {
2026-07-20 04:02:46 +02:00
// dir may not exist yet
2026-02-28 19:12:45 +01:00
}
2026-07-20 04:02:46 +02:00
// Linked cases (v1 registry has no owner) are admin-only in multi-user mode.
2026-03-15 03:47:18 +01:00
const linkedCases = await readLinkedCases ();
const existingNames = new Set ( cases . map (( c ) => c . name ));
2026-07-20 04:02:46 +02:00
if ( admin ) {
for ( const [ name , path ] of Object . entries ( linkedCases )) {
2026-10-04 20:30:40 -04:00
if ( existingNames . has ( name ) || ! SAFE_CASE_NAME . test ( name )) continue ;
const state = await probePath ( path );
if ( state === 'absent' ) continue ;
// An unreachable linked case (a dead network mount) stays listed and says
// so: dropping it would read as "deleted" and invite a same-name local case.
cases . push ({
name ,
path ,
hasClaudeMd : state === 'present' && ( await boundedPathExists ( join ( path , 'CLAUDE.md' ))),
linked : true ,
location : 'linked-local' ,
...( state === 'unknown' ? { unreachable : true } : {}),
});
2026-02-28 19:12:45 +01:00
}
}
2026-07-20 04:02:46 +02:00
// Get remote cases (owner-scoped; legacy no-owner = admin-only)
2026-06-08 10:40:56 +00:00
const remoteHosts = await readRemoteHosts ( CODEMAN_CONFIG_DIR );
const remoteHostMap = new Map ( remoteHosts . map (( host ) => [ host . id , host ]));
for ( const remoteCase of await readRemoteCases ( CODEMAN_CONFIG_DIR )) {
const host = remoteHostMap . get ( remoteCase . hostId );
2026-06-11 18:43:39 +00:00
if ( ! host || ! SAFE_CASE_NAME . test ( remoteCase . name )) continue ;
2026-07-20 04:02:46 +02:00
if ( ! admin && ! canAccessOwned ( user , remoteCase . owner )) continue ;
2026-06-08 10:40:56 +00:00
existingNames . add ( remoteCase . name );
2026-06-11 18:43:39 +00:00
const remoteCaseInfo : CaseInfo = {
2026-06-08 10:40:56 +00:00
name : remoteCase.name ,
path : remoteDisplayPath ({ username : host.username , host : host.host , path : remoteCase.remotePath }),
hasClaudeMd : false ,
location : 'remote' ,
remote : {
hostId : host.id ,
host : host.host ,
username : host.username ,
path : remoteCase.remotePath ,
},
2026-06-11 18:43:39 +00:00
};
const existingIndex = cases . findIndex (( item ) => item . name === remoteCase . name );
if ( existingIndex === - 1 ) {
cases . push ( remoteCaseInfo );
} else {
cases [ existingIndex ] = remoteCaseInfo ;
}
2026-06-08 10:40:56 +00:00
}
2026-07-19 15:28:44 +02:00
// Get docker cases
const dockerHosts = await readDockerHosts ( CODEMAN_CONFIG_DIR );
const dockerHostMap = new Map ( dockerHosts . map (( host ) => [ host . id , host ]));
for ( const dockerCase of await readDockerCases ( CODEMAN_CONFIG_DIR )) {
const host = dockerHostMap . get ( dockerCase . hostId );
if ( ! host || ! SAFE_CASE_NAME . test ( dockerCase . name )) continue ;
2026-07-20 04:02:46 +02:00
if ( ! admin && ! canAccessOwned ( user , dockerCase . owner )) continue ;
2026-07-19 15:28:44 +02:00
existingNames . add ( dockerCase . name );
const container = dockerCase . container ?? dockerContainerName ( dockerCase . name );
const dockerCaseInfo : CaseInfo = {
name : dockerCase.name ,
path : dockerDisplayPath ({ container , path : dockerCase.hostWorkspacePath }),
2026-10-01 13:58:55 -04:00
hasClaudeMd : await boundedPathExists ( join ( dockerCase . hostWorkspacePath , 'CLAUDE.md' )),
2026-07-19 15:28:44 +02:00
location : 'docker' ,
docker : {
hostId : host.id ,
container ,
image : host.image ,
path : dockerCase.hostWorkspacePath ,
2026-09-01 20:45:13 -07:00
containerWorkdir : dockerCase.containerWorkdir ?? dockerCase . hostWorkspacePath ,
2026-07-19 15:28:44 +02:00
network : host.network ?? 'bridge' ,
2026-08-29 21:02:19 -07:00
...( dockerCase . availableModes ? { availableModes : dockerCase.availableModes } : {}),
2026-09-05 16:22:54 +02:00
...( dockerCase . owned === false ? { owned : false } : {}),
2026-07-19 15:28:44 +02:00
},
};
const existingIndex = cases . findIndex (( item ) => item . name === dockerCase . name );
if ( existingIndex === - 1 ) {
cases . push ( dockerCaseInfo );
} else {
cases [ existingIndex ] = dockerCaseInfo ;
}
}
2026-04-03 09:45:19 +08:00
// Sort by persisted caseOrder from settings.json
const settings = await readJsonConfig < Record < string , unknown >>( SETTINGS_PATH , 'settings' , {});
const caseOrder = Array . isArray ( settings . caseOrder ) ? ( settings . caseOrder as string []) : [];
if ( caseOrder . length > 0 ) {
const orderMap = new Map ( caseOrder . map (( name , idx ) => [ name , idx ]));
cases . sort (( a , b ) => {
const ai = orderMap . get ( a . name ) ?? Number . MAX_SAFE_INTEGER ;
const bi = orderMap . get ( b . name ) ?? Number . MAX_SAFE_INTEGER ;
return ai - bi ;
});
}
2026-02-28 19:12:45 +01:00
return cases ;
});
2026-09-07 19:09:24 +02:00
// ========== Agent-created cases (cleanup listing) ==========
/**
* The scratch workspaces agent workers left behind, newest first.
*
* A long orchestration creates one case directory per worker, and deleting the
* sessions does not remove them, so without this the only way to tell an agent's
* `alpha`/`beta` from a real project was to remember which was which. Reads the same
* marker `GET /api/cases` exposes and adds the two facts a human needs before
* deleting a directory: whether a live session is still working in it, and when it
* was last touched.
*
* ⚠️ Read-only on purpose: removal goes through the existing `DELETE /api/cases/:name`,
* one name at a time, so this file keeps exactly one recursive-delete path. Scoped by
* construction — it only ever walks the caller's own case space.
*/
app . get ( '/api/cases/agent-created' , async ( req ) : Promise < ApiResponse < { cases : AgentCaseSummary [] }> > => {
const user = getAuthUser ( req );
const listBase = resolveCasesDir ( user );
const inUsePaths = new Set (
Array . from ( ctx . sessions . values ())
. filter (( session ) => canAccessOwned ( user , session . owner ))
. map (( session ) => session . workingDir )
);
let entries ;
try {
entries = await fs . readdir ( listBase , { withFileTypes : true });
} catch {
return { success : true , data : { cases : [] } }; // case space not created yet
}
const summaries : AgentCaseSummary [] = [];
for ( const entry of entries ) {
if ( ! entry . isDirectory () || ! SAFE_CASE_NAME . test ( entry . name )) continue ;
const casePath = join ( listBase , entry . name );
const marker = await readAgentCaseMarker ( casePath );
if ( ! marker ) continue ;
const modifiedAt = await fs
. stat ( casePath )
. then (( stat ) => stat . mtime . toISOString ())
. catch (() => undefined );
summaries . push ({
name : entry.name ,
path : casePath ,
... agentCreatedInfo ( marker ),
inUse : inUsePaths.has ( casePath ),
...( modifiedAt ? { modifiedAt } : {}),
});
}
summaries . sort (( a , b ) => b . createdAt . localeCompare ( a . createdAt ));
return { success : true , data : { cases : summaries } };
});
2026-10-03 13:54:16 +08:00
/**
* `POST /api/cases` with a `path`: create the folder (or fill an EMPTY existing one), scaffold it
* exactly like a normal case, and register it in the linked-cases registry so it lists, resolves
* and deletes (unlinks, never removes files) like any linked case. Everything is judged before
* anything is written; a failure after the first write undoes what this call created.
*/
async function createCaseInCustomFolder (
name : string ,
description : string | undefined ,
customPath : string ,
req : FastifyRequest ,
reply : { code : ( n : number ) => unknown }
) : Promise < ApiResponse < { case : { name : string ; path : string } }> > {
2026-10-05 14:48:45 +02:00
const ownCasesDir = resolveCasesDir ( getAuthUser ( req ));
if ( existsSync ( join ( ownCasesDir , name ))) {
2026-10-03 13:54:16 +08:00
reply . code ( 409 );
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'A case with this name already exists in codeman-cases.' );
}
const linkedCases = await readLinkedCases ();
if ( linkedCases [ name ]) {
reply . code ( 409 );
return createErrorResponse (
ApiErrorCode . ALREADY_EXISTS ,
`Case " ${ name } " is already linked to ${ linkedCases [ name ] } `
);
}
2026-10-05 14:48:45 +02:00
// The caller's own cases dir and the shared one (the same folder outside multi-user mode).
const casesDirs = [... new Set ([ ownCasesDir , resolveCasesDir ()])];
const prepared = await prepareNewCasePath ( customPath , { home : homedir (), dataDir : getDataDir (), casesDirs });
2026-10-03 13:54:16 +08:00
if ( ! prepared . ok ) {
2026-10-05 20:00:03 +02:00
// A parent that did not answer is not a bad request: OPERATION_FAILED (422), like
// POST /api/sessions for a workingDir on a dead mount.
if ( prepared . code === 'UNREACHABLE' ) {
reply . code ( 422 );
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , prepared . reason );
}
2026-10-03 13:54:16 +08:00
const status = prepared . code === 'NOT_FOUND' ? 404 : prepared.code === 'EXISTS' ? 409 : 400 ;
reply . code ( status );
const code =
prepared . code === 'NOT_FOUND'
? ApiErrorCode.NOT_FOUND
: prepared.code === 'EXISTS'
? ApiErrorCode.ALREADY_EXISTS
: ApiErrorCode.INVALID_INPUT ;
return createErrorResponse ( code , prepared . reason );
}
const casePath = prepared . path ;
const alreadyAs = Object . entries ( linkedCases ). find (([, p ]) => p === casePath ) ? .[ 0 ];
if ( alreadyAs ) {
reply . code ( 409 );
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , `That folder is already the case " ${ alreadyAs } "` );
}
const made : string [] = [];
try {
if ( ! prepared . existedEmpty ) {
mkdirSync ( casePath );
made . push ( casePath );
}
mkdirSync ( join ( casePath , 'src' ));
made . push ( join ( casePath , 'src' ));
const templatePath = await ctx . getDefaultClaudeMdPath ();
writeFileSync ( join ( casePath , 'CLAUDE.md' ), generateClaudeMd ( name , description || '' , templatePath ));
made . push ( join ( casePath , 'CLAUDE.md' ));
made . push ( join ( casePath , '.claude' )); // before the write, so a half-written one is undone too
await writeHooksConfig ( casePath );
const codemanDir = getDataDir ();
if ( ! existsSync ( codemanDir )) mkdirSync ( codemanDir , { recursive : true });
2026-10-05 14:48:45 +02:00
// Re-read right before writing, so a case linked since the check above is not dropped. This only
// narrows the window: like POST /api/cases/link, the registry write is not serialized, and two
// requests that both read before either writes can still lose one entry.
2026-10-03 13:54:16 +08:00
const fresh = await readLinkedCases ();
if ( fresh [ name ]) throw Object . assign ( new Error ( `Case " ${ name } " was just linked` ), { conflict : true });
fresh [ name ] = casePath ;
await fs . writeFile ( LINKED_CASES_FILE , JSON . stringify ( fresh , null , 2 ));
ctx . broadcast ( SseEvent . CaseCreated , { name , path : casePath });
return { success : true , data : { case : { name , path : casePath } } };
} catch ( err ) {
// Undo only what this call made. The whole folder if we created it, otherwise the scaffold
// entries inside the empty folder the user picked; never anything else.
for ( const p of made . reverse ()) await fs . rm ( p , { recursive : true , force : true }). catch (() => undefined );
if (( err as { conflict? : boolean }). conflict ) {
reply . code ( 409 );
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , getErrorMessage ( err ));
}
reply . code ( 500 );
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , getErrorMessage ( err ));
}
}
app . post ( '/api/cases' , async ( req , reply ) : Promise < ApiResponse < { case : { name : string ; path : string } }> > => {
const { name , description , path : customPath } = parseBody ( CreateCaseSchema , req . body );
// A custom folder writes outside the cases directory and registers the path in the shared,
// ownerless linked-cases registry, so it carries the same bar as POST /api/cases/link.
if ( customPath !== undefined ) {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
return createCaseInCustomFolder ( name , description , customPath , req , reply );
}
2026-02-28 19:12:45 +01:00
2026-07-20 04:02:46 +02:00
const casePath = validatePathWithinBase ( name , resolveCasesDir ( getAuthUser ( req )));
2026-03-11 19:13:49 +01:00
if ( ! casePath ) {
2026-02-28 19:12:45 +01:00
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case path' );
}
if ( existsSync ( casePath )) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Case already exists' );
}
try {
mkdirSync ( casePath , { recursive : true });
mkdirSync ( join ( casePath , 'src' ), { recursive : true });
// Read settings to get custom template path
const templatePath = await ctx . getDefaultClaudeMdPath ();
const claudeMd = generateClaudeMd ( name , description || '' , templatePath );
writeFileSync ( join ( casePath , 'CLAUDE.md' ), claudeMd );
// Write .claude/settings.local.json with hooks for desktop notifications
await writeHooksConfig ( casePath );
2026-03-03 22:40:18 +01:00
ctx . broadcast ( SseEvent . CaseCreated , { name , path : casePath });
2026-02-28 19:12:45 +01:00
return { success : true , data : { case : { name , path : casePath } } };
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , getErrorMessage ( err ));
}
});
2026-08-09 16:33:01 +02:00
// ========== Clone a repository as a case (issue #236) ==========
/**
* Ask a remote what it has, without cloning anything.
*
* Two jobs: tell the user whether the URL they typed can be cloned *anonymously*
* (Codeman supplies no credentials, so "private" and "typo" both have to be
* distinguishable from "fine"), and hand back the branch/tag lists so the ref
* field is a picker instead of a guess.
*
* Always 200 with `reachable: false` on a dead remote — an unreachable URL is a
* normal answer to a preflight, not a server error, and the UI renders the reason.
*/
app . post (
'/api/cases/clone-preflight' ,
async (
req ,
reply
) : Promise < ApiResponse < { parse : GitUrlParse ; remote ?: GitRemoteProbe ; gitAvailable : boolean }> > => {
const { repository } = parseBody ( ClonePreflightSchema , req . body );
const parsed = parseGitRepositoryUrl ( repository );
if ( ! parsed . cloneable ) {
return { success : true , data : { parse : parsed , gitAvailable : isGitAvailable () } };
}
if ( parsed . transport === 'local' && isMultiUserMode () && ! isAdmin ( req )) {
reply . code ( 403 );
return createErrorResponse ( ApiErrorCode . FORBIDDEN , LOCAL_CLONE_ADMIN_ONLY );
}
if ( ! isGitAvailable ()) {
return { success : true , data : { parse : parsed , gitAvailable : false } };
}
2026-09-23 14:44:26 +08:00
const remote = await probeGitRemote ( parsed . repository , undefined , {
withoutCredentialHelpers : cloneWithoutCredentialHelpers ( req ),
});
2026-08-09 16:33:01 +02:00
return { success : true , data : { parse : parsed , remote , gitAvailable : true } };
}
);
/**
* Clone a repository into the caller's case space and register it as a normal
* local case (issue #236).
*
* SYNCHRONOUS by design for v1: the request stays open for the whole clone
* (bounded by `GIT_CLONE_TIMEOUT_MS`), so there is no job store, no polling and
* no cancellation surface to get wrong. The `case:created` broadcast is what
* makes that safe behind a proxy with its own idle timeout — a client whose
* request died mid-clone still sees the case appear over SSE when git finishes.
*
* Deliberately NOT admin-gated in multi-user mode: unlike `/api/cases/link`,
2026-09-23 14:44:26 +08:00
* this writes only inside the caller's own `resolveCasesDir`. Two things would
* otherwise read through that boundary: a `local`-transport source (refused for
* non-admins) and the server account's git credential helpers, which every user
* shares (cleared for non-admins, see `cloneWithoutCredentialHelpers`).
2026-08-09 16:33:01 +02:00
*
* Repository contents win over scaffolding: an existing CLAUDE.md is left
* alone, and hooks are MERGED into whatever `.claude/settings.local.json` the
* repo ships (`writeHooksConfig` preserves non-Codeman handlers). A repo that
* ships its own hooks is reported back as a warning, because those run on the
* user's machine the moment a session starts in the case.
*/
app . post (
'/api/cases/clone' ,
async (
req ,
reply
) : Promise <
ApiResponse < {
case : { name : string ; path : string };
repository : string ;
ref? : string ;
provider : string ;
warnings : string [];
} >
> => {
const { name , repository , ref , shallow , description } = parseBody ( CloneCaseSchema , req . body );
const user = getAuthUser ( req );
const parsed = parseGitRepositoryUrl ( repository );
if ( ! parsed . cloneable ) return createErrorResponse ( ApiErrorCode . INVALID_INPUT , parsed . message );
if ( ref && ! isSafeGitRef ( ref )) {
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid branch or tag name' );
}
if ( parsed . transport === 'local' && isMultiUserMode () && ! isAdmin ( req )) {
reply . code ( 403 );
return createErrorResponse ( ApiErrorCode . FORBIDDEN , LOCAL_CLONE_ADMIN_ONLY );
}
if ( ! isGitAvailable ()) {
return createErrorResponse (
ApiErrorCode . OPERATION_FAILED ,
'git is not installed on this machine (or not on the server’ s PATH).'
);
}
const casesDir = resolveCasesDir ( user );
const casePath = validatePathWithinBase ( name , casesDir );
if ( ! casePath ) return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case path' );
// Reject a duplicate name across EVERY case kind before invoking git, so a
// clone can never be the thing that discovers the collision (it would have
// spent minutes of network first, and git's own error is about a directory).
const linkedCases = await readLinkedCases ();
const dockerCases = await readDockerCases ( CODEMAN_CONFIG_DIR );
const remoteCases = await readRemoteCases ( CODEMAN_CONFIG_DIR );
if (
existsSync ( casePath ) ||
linkedCases [ name ] ||
dockerCases . some (( item ) => item . name === name ) ||
remoteCases . some (( item ) => item . name === name )
) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Case already exists' );
}
// git creates the leaf, not necessarily the case space above it.
try {
mkdirSync ( casesDir , { recursive : true });
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , getErrorMessage ( err ));
}
const clone = await cloneRepository ({
repository : parsed.repository ,
destination : casePath ,
2026-09-23 14:44:26 +08:00
withoutCredentialHelpers : cloneWithoutCredentialHelpers ( req ),
2026-08-09 16:33:01 +02:00
...( ref ? { ref } : {}),
...( shallow ? { shallow : true } : {}),
});
if ( ! clone . ok ) {
const code =
clone . failure . code === 'NOT_FOUND'
? ApiErrorCode.NOT_FOUND
: clone.failure.code === 'DESTINATION_EXISTS'
? ApiErrorCode.ALREADY_EXISTS
: clone.failure.code === 'REF_NOT_FOUND'
? ApiErrorCode.INVALID_INPUT
2026-08-10 00:07:01 +02:00
: clone.failure.code === 'BUSY'
? ApiErrorCode.RATE_LIMITED
: ApiErrorCode.OPERATION_FAILED ;
2026-08-09 16:33:01 +02:00
const detail = clone . failure . stderr
? ` ${ clone . failure . message } ( ${ gitDiagnosticLine ( clone . failure . stderr ) } )`
: clone . failure . message ;
return createErrorResponse ( code , detail );
}
2026-08-10 00:07:01 +02:00
// Scaffold WITHOUT overwriting anything the repository shipped, and
// WITHOUT writing through anything it shipped as a symlink.
2026-08-09 16:33:01 +02:00
const warnings = [... parsed . warnings ];
try {
2026-08-10 00:07:01 +02:00
// Presence via lstat, not existsSync: a repo-shipped CLAUDE.md SYMLINK
// counts as "the repository ships its own" even when the link is
// broken (existsSync follows links and reports a broken one as
// absent), because writeFileSync would write THROUGH it to a
// repository-chosen path outside the case.
if ( ! lstatSync ( join ( casePath , 'CLAUDE.md' ), { throwIfNoEntry : false })) {
2026-08-09 16:33:01 +02:00
const templatePath = await ctx . getDefaultClaudeMdPath ();
const summary = description || `Cloned from ${ parsed . repository } ` ;
writeFileSync ( join ( casePath , 'CLAUDE.md' ), generateClaudeMd ( name , summary , templatePath ));
} else {
warnings . push ( 'Kept the repository’ s own CLAUDE.md.' );
}
if ( repoShipsClaudeSettings ( casePath )) {
warnings . push (
'This repository ships its own .claude/settings files. Codeman merged its hooks alongside them without removing anything — review them before starting a session, since repo-supplied hooks run on this machine.'
);
}
2026-08-10 00:07:01 +02:00
// A repository can ship `.claude` (or the settings file) as a symlink
// pointing anywhere on this machine; writeHooksConfig itself refuses
// to write through those (settingsWriteBlocker in hooks-config.ts).
// Checking here too turns that refusal into a user-visible warning.
const hooksBlocker = await settingsWriteBlocker ( casePath );
if ( hooksBlocker ) {
warnings . push (
`Codeman hooks were NOT installed: ${ hooksBlocker } . Codeman refuses to write through repository-controlled links; replace the link with a real file or directory if you want hooks in this case.`
);
} else {
await writeHooksConfig ( casePath );
}
2026-08-09 16:33:01 +02:00
} catch ( err ) {
// The clone itself succeeded: keep the case and report the scaffolding
// problem, rather than deleting a tree the user just waited for.
warnings . push ( `Case scaffolding was incomplete: ${ getErrorMessage ( err ) } ` );
}
ctx . broadcast ( SseEvent . CaseCreated , { name , path : casePath });
return {
success : true ,
data : {
case : { name , path : casePath },
repository : parsed.repository ,
...( ref ? { ref } : {}),
provider : parsed.provider ,
warnings ,
},
};
}
);
2026-07-20 12:33:12 +02:00
// Hosts are machine-level infra config (ssh users/identity paths): non-admins get an
// empty list in multi-user mode, matching the admin-only write side. No-op otherwise.
app . get ( '/api/remote-hosts' , async ( req ) =>
isMultiUserMode () && ! isAdmin ( req ) ? [] : readRemoteHosts ( CODEMAN_CONFIG_DIR )
);
2026-06-08 10:40:56 +00:00
2026-07-20 04:02:46 +02:00
// Hosts are machine-level resources: only admins may define them in multi-user mode.
const adminOnly = ( req : FastifyRequest , reply : { code : ( n : number ) => unknown }) : ApiResponse < never > | null =>
isAdmin ( req )
? null
: ( reply . code ( 403 ), createErrorResponse ( ApiErrorCode . FORBIDDEN , 'Admin only in multi-user mode' ));
2026-06-08 10:40:56 +00:00
2026-06-13 11:04:18 -04:00
// COD-105 — discover `codeman-*` tmux sessions already running on a remote
// host (created by the remote's own Codeman, another instance, or this one)
// so the operator can attach to one this Codeman didn't launch. Explicit
// trigger only (Decision A): the frontend calls this on a "Discover" click,
// never automatically on host select. listRemoteCodemanSessions never throws
// (returns [] on unreachable/no-tmux/no-sessions) and is ssh-guarded under test.
2026-07-20 14:27:02 +02:00
// Hosts are admin-only infra in multi-user mode, so discovery is too.
2026-06-13 11:04:18 -04:00
app . get (
'/api/remote-hosts/:hostId/sessions' ,
2026-07-20 14:27:02 +02:00
async ( req , reply ) : Promise < ApiResponse < { sessions : RemoteSessionInfo [] }> > => {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-06-13 11:04:18 -04:00
const { hostId } = req . params as { hostId : string };
const host = ( await readRemoteHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Remote host not found' );
const sessions = await listRemoteCodemanSessions ( host );
return { success : true , data : { sessions } };
}
);
2026-07-20 04:02:46 +02:00
app . post ( '/api/remote-hosts' , async ( req , reply ) : Promise < ApiResponse < { host : unknown }> > => {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-06-08 10:40:56 +00:00
const host = parseBody ( RemoteHostSchema , req . body );
const hosts = await readRemoteHosts ( CODEMAN_CONFIG_DIR );
if ( hosts . some (( item ) => item . id === host . id )) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Remote host already exists' );
}
await writeRemoteHosts ( CODEMAN_CONFIG_DIR , [... hosts , host ]);
return { success : true , data : { host } };
});
2026-07-20 04:02:46 +02:00
app . put ( '/api/remote-hosts/:id' , async ( req , reply ) : Promise < ApiResponse < { host : unknown }> > => {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-06-08 10:40:56 +00:00
const { id } = req . params as { id : string };
const host = parseBody ( RemoteHostSchema , { ...( req . body as object ), id });
const hosts = await readRemoteHosts ( CODEMAN_CONFIG_DIR );
const index = hosts . findIndex (( item ) => item . id === id );
if ( index === - 1 ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Remote host not found' );
const next = [... hosts ];
next [ index ] = host ;
await writeRemoteHosts ( CODEMAN_CONFIG_DIR , next );
return { success : true , data : { host } };
});
2026-07-20 04:02:46 +02:00
app . delete ( '/api/remote-hosts/:id' , async ( req , reply ) : Promise < ApiResponse < { id : string }> > => {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-06-08 10:40:56 +00:00
const { id } = req . params as { id : string };
const cases = await readRemoteCases ( CODEMAN_CONFIG_DIR );
if ( cases . some (( item ) => item . hostId === id )) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , 'Remote host is still used by remote cases' );
}
const hosts = await readRemoteHosts ( CODEMAN_CONFIG_DIR );
await writeRemoteHosts (
CODEMAN_CONFIG_DIR ,
hosts . filter (( item ) => item . id !== id )
);
return { success : true , data : { id } };
});
app . post ( '/api/cases/remote-link' , async ( req ) : Promise < ApiResponse < { case : unknown }> > => {
2026-07-20 04:02:46 +02:00
const remoteCase = { ... parseBody ( RemoteCaseLinkSchema , req . body ), type : 'remote' as const , owner : ownerFor ( req ) };
2026-06-08 10:40:56 +00:00
const hosts = await readRemoteHosts ( CODEMAN_CONFIG_DIR );
const host = hosts . find (( item ) => item . id === remoteCase . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Remote host not found' );
const linkedCases = await readLinkedCases ();
const remoteCases = await readRemoteCases ( CODEMAN_CONFIG_DIR );
if (
remoteCases . some (( item ) => item . name === remoteCase . name ) ||
linkedCases [ remoteCase . name ] ||
2026-07-20 04:02:46 +02:00
existsSync ( join ( resolveCasesDir ( getAuthUser ( req )), remoteCase . name ))
2026-06-08 10:40:56 +00:00
) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Case already exists' );
}
2026-07-12 19:49:39 +02:00
// Courtesy validation: tmux is a hard prerequisite for durable remote sessions.
// Verify it up-front so linking surfaces a clear error now instead of a dead pane
// at first launch (also confirms the SSH connection actually works).
const tmuxCheck = await checkRemoteTmuxAvailable ( host );
if ( ! tmuxCheck . ok ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , tmuxCheck . error || 'remote host is missing tmux' );
}
2026-06-08 10:40:56 +00:00
await writeRemoteCases ( CODEMAN_CONFIG_DIR , [... remoteCases , remoteCase ]);
ctx . broadcast ( SseEvent . CaseLinked , { name : remoteCase.name , path : remoteCase.remotePath , type : 'remote' });
return { success : true , data : { case : remoteCase } };
});
2026-07-19 15:28:44 +02:00
// ========== Docker hosts + docker cases (COD-Docker) ==========
2026-07-20 12:33:12 +02:00
// Hosts are machine-level infra config (images/mounts/env): non-admins get an empty
// list in multi-user mode, matching the admin-only write side. No-op otherwise.
app . get ( '/api/docker-hosts' , async ( req ) =>
isMultiUserMode () && ! isAdmin ( req ) ? [] : readDockerHosts ( CODEMAN_CONFIG_DIR )
);
2026-07-19 15:28:44 +02:00
2026-07-20 04:02:46 +02:00
app . post ( '/api/docker-hosts' , async ( req , reply ) : Promise < ApiResponse < { host : unknown }> > => {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-07-19 15:28:44 +02:00
const host = parseBody ( DockerHostSchema , req . body );
const hosts = await readDockerHosts ( CODEMAN_CONFIG_DIR );
if ( hosts . some (( item ) => item . id === host . id )) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Docker host already exists' );
}
await writeDockerHosts ( CODEMAN_CONFIG_DIR , [... hosts , host ]);
return { success : true , data : { host } };
});
2026-07-20 04:02:46 +02:00
app . put ( '/api/docker-hosts/:id' , async ( req , reply ) : Promise < ApiResponse < { host : unknown }> > => {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-07-19 15:28:44 +02:00
const { id } = req . params as { id : string };
const host = parseBody ( DockerHostSchema , { ...( req . body as object ), id });
const hosts = await readDockerHosts ( CODEMAN_CONFIG_DIR );
const index = hosts . findIndex (( item ) => item . id === id );
if ( index === - 1 ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const next = [... hosts ];
next [ index ] = host ;
await writeDockerHosts ( CODEMAN_CONFIG_DIR , next );
return { success : true , data : { host } };
});
2026-07-20 04:02:46 +02:00
app . delete ( '/api/docker-hosts/:id' , async ( req , reply ) : Promise < ApiResponse < { id : string }> > => {
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-07-19 15:28:44 +02:00
const { id } = req . params as { id : string };
const cases = await readDockerCases ( CODEMAN_CONFIG_DIR );
if ( cases . some (( item ) => item . hostId === id )) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , 'Docker host is still used by docker cases' );
}
const hosts = await readDockerHosts ( CODEMAN_CONFIG_DIR );
await writeDockerHosts (
CODEMAN_CONFIG_DIR ,
hosts . filter (( item ) => item . id !== id )
);
return { success : true , data : { id } };
});
app . post (
'/api/cases/docker-link' ,
2026-07-20 01:28:53 +02:00
async (
req
) : Promise <
ApiResponse < { case : unknown ; capsEnforced? : boolean ; isDesktop? : boolean ; imageBuilding? : boolean } >
> => {
2026-07-20 04:02:46 +02:00
const dockerCase = {
... parseBody ( DockerCaseLinkSchema , req . body ),
type : 'docker' as const ,
owner : ownerFor ( req ),
};
2026-07-19 15:28:44 +02:00
const hosts = await readDockerHosts ( CODEMAN_CONFIG_DIR );
const host = hosts . find (( item ) => item . id === dockerCase . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const linkedCases = await readLinkedCases ();
const dockerCases = await readDockerCases ( CODEMAN_CONFIG_DIR );
if (
dockerCases . some (( item ) => item . name === dockerCase . name ) ||
linkedCases [ dockerCase . name ] ||
2026-07-20 04:02:46 +02:00
existsSync ( join ( resolveCasesDir ( getAuthUser ( req )), dockerCase . name ))
2026-07-19 15:28:44 +02:00
) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Case already exists' );
}
2026-07-20 12:33:12 +02:00
// Confine the bind-mounted workspace to the caller's own space BEFORE creating it
// (also removes the arbitrary-dir-creation primitive). No-op for admins/single-user.
if ( ! isWorkingDirAllowed ( getAuthUser ( req ), dockerCase . hostWorkspacePath )) {
return createErrorResponse ( ApiErrorCode . FORBIDDEN , 'hostWorkspacePath is outside your workspace' );
}
2026-07-19 15:28:44 +02:00
// The workspace is a REAL host directory (bind-mounted into the container), so
// create it now if missing. Scaffolding (.claude/settings.local.json + CLAUDE.md)
// is written by quick-start on first launch, matching local-case behaviour.
if ( ! existsSync ( dockerCase . hostWorkspacePath )) {
try {
mkdirSync ( dockerCase . hostWorkspacePath , { recursive : true });
} catch ( err ) {
return createErrorResponse (
ApiErrorCode . OPERATION_FAILED ,
`Could not create workspace: ${ getErrorMessage ( err ) } `
);
}
}
2026-07-20 01:28:53 +02:00
// Courtesy validation: docker daemon must be reachable. The base image is
// auto-built on first use (default image) rather than being a link-time
// blocker, so a missing image kicks off a background build instead of erroring.
2026-07-19 15:28:44 +02:00
const availability = await checkDockerAvailable ( host . engine );
if ( ! availability . ok ) {
return createErrorResponse (
ApiErrorCode . OPERATION_FAILED ,
availability . error || 'docker daemon is not available'
);
}
2026-07-20 01:28:53 +02:00
const imageGate = await ensureCaseImage ( ctx . broadcast , toSessionDocker ( host , dockerCase ), dockerCase . name );
if ( ! imageGate . ok ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , imageGate . error );
2026-07-19 15:28:44 +02:00
}
await writeDockerCases ( CODEMAN_CONFIG_DIR , [... dockerCases , dockerCase ]);
ctx . broadcast ( SseEvent . CaseLinked , {
name : dockerCase.name ,
path : dockerCase.hostWorkspacePath ,
type : 'docker' ,
});
return {
success : true ,
2026-07-20 01:28:53 +02:00
data : {
case : dockerCase ,
capsEnforced : availability.capsEnforced ,
isDesktop : availability.isDesktop ,
imageBuilding : imageGate.imageBuilding ,
},
2026-07-19 19:28:13 +02:00
};
}
);
2026-08-29 21:02:19 -07:00
/**
* ADOPT an already-running container (`owned: false`). The mirror of the
* remote-SSH attach path: Codeman execs into a container the user built and
* runs, and never creates, starts, stops, restarts or removes it.
*
* Everything here is read-only toward the container. The preflight refuses at
* LINK time — missing, stopped, or no tmux inside — because the alternative is
* failing at session launch, where the only ways out would be a dead pane or
* starting a container we do not own. There is no image gate and no
* `ensureCaseImage`: adoption never runs `docker create`, so the container's
* image is the user's business.
*/
app . post (
'/api/cases/docker-adopt' ,
2026-09-05 16:22:54 +02:00
async ( req , reply ) : Promise < ApiResponse < { case : unknown ; image ?: string ; availableModes ?: SessionMode [] }> > => {
// ⚠️ Admin-only in multi-user mode, unlike `docker-link` right above. Linking
// creates OUR container, whose only bind mount is a workspace `isWorkingDirAllowed`
// has already confined. Adoption names a container someone else built, and its
// mounts are whatever its owner gave it — a container mounting `/` hands the
// adopter a shell over the whole host, which is exactly the workspace scoping this
// mode exists to enforce. Same machine-level reasoning as the docker HOST routes.
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-08-29 21:02:19 -07:00
const dockerCase = {
... parseBody ( DockerCaseAdoptSchema , req . body ),
type : 'docker' as const ,
owner : ownerFor ( req ),
owned : false as const ,
};
const host = ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === dockerCase . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const linkedCases = await readLinkedCases ();
const dockerCases = await readDockerCases ( CODEMAN_CONFIG_DIR );
if (
dockerCases . some (( item ) => item . name === dockerCase . name ) ||
linkedCases [ dockerCase . name ] ||
existsSync ( join ( resolveCasesDir ( getAuthUser ( req )), dockerCase . name ))
) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Case already exists' );
}
2026-09-01 05:05:13 -07:00
// One container may back SEVERAL adopted cases, each pointing at a different
// directory inside it. What still blocks it, and why, lives in
// classifyAdoptContainerConflict — note that none of it is about the shared
// in-container tmux server, which is safe precisely because sessions there
// are named per SESSION id (`codeman-dkr-<id8>`), never per case.
2026-08-29 21:02:19 -07:00
const container = dockerCase . container ;
2026-09-01 05:05:13 -07:00
const conflict = classifyAdoptContainerConflict ({
container ,
containerWorkdir : dockerCase.containerWorkdir ?? dockerCase . hostWorkspacePath ,
existing : dockerCases ,
canAccess : ( owner ) => canAccessOwned ( getAuthUser ( req ), owner ),
});
if ( conflict ? . kind === 'owned-case' ) {
return createErrorResponse (
ApiErrorCode . ALREADY_EXISTS ,
`Container " ${ container } " belongs to case " ${ conflict . caseName } ", which Codeman created and whose lifecycle it manages. Adopt a container you started yourself, or open that case directly.`
);
}
if ( conflict ? . kind === 'other-owner' ) {
return createErrorResponse (
ApiErrorCode . FORBIDDEN ,
`Container " ${ container } " is already adopted by another user.`
);
}
if ( conflict ? . kind === 'duplicate' ) {
return createErrorResponse (
ApiErrorCode . ALREADY_EXISTS ,
`Case " ${ conflict . caseName } " already adopts " ${ container } " at that same directory. Point this one at another directory inside the container.`
);
2026-08-29 21:02:19 -07:00
}
if ( ! isWorkingDirAllowed ( getAuthUser ( req ), dockerCase . hostWorkspacePath )) {
return createErrorResponse ( ApiErrorCode . FORBIDDEN , 'hostWorkspacePath is outside your workspace' );
}
// The workspace must ALREADY exist: it mirrors a path inside a container we
// did not create, so silently mkdir-ing it would invent a host directory that
// does not correspond to whatever is actually mounted there.
if ( ! existsSync ( dockerCase . hostWorkspacePath )) {
return createErrorResponse (
ApiErrorCode . INVALID_INPUT ,
'hostWorkspacePath does not exist. Adoption mirrors an existing container, so point this at the real host directory already mounted into it.'
);
}
const availability = await checkDockerAvailable ( host . engine );
if ( ! availability . ok ) {
return createErrorResponse (
ApiErrorCode . OPERATION_FAILED ,
availability . error || 'docker daemon is not available'
);
}
2026-08-29 21:02:19 -07:00
// The container workdir is validated INSIDE the container. It defaults to
// hostWorkspacePath only because that is what an owned container's bind
// mount guarantees; adoption mounts nothing, so the probe has to prove it.
const adoptDocker = toSessionDocker ( host , dockerCase );
2026-09-05 16:21:51 +02:00
const probe = await probeAdoptableContainer ( adoptDocker , dockerAdoptProbeModes (), adoptDocker . containerWorkdir );
2026-08-29 21:02:19 -07:00
if ( ! probe . ok ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , probe . error || 'container is not adoptable' );
}
2026-08-29 21:02:19 -07:00
// Persist what the container actually has: the run-mode picker gates on
// HOST CLIs, which is the wrong question for a case whose agents run inside
// a container the host knows nothing about.
const adoptedCase = { ... dockerCase , availableModes : probe.availableModes };
await writeDockerCases ( CODEMAN_CONFIG_DIR , [... dockerCases , adoptedCase ]);
2026-08-29 21:02:19 -07:00
ctx . broadcast ( SseEvent . CaseLinked , {
2026-08-29 21:02:19 -07:00
name : adoptedCase.name ,
path : adoptedCase.hostWorkspacePath ,
2026-08-29 21:02:19 -07:00
type : 'docker' ,
});
return {
success : true ,
2026-08-29 21:02:19 -07:00
data : { case : adoptedCase , image : probe.image , availableModes : probe.availableModes },
2026-08-29 21:02:19 -07:00
};
}
);
/**
* Preflight an existing container WITHOUT linking anything, so the UI can tell
* the user "not running" / "no tmux" / "codex present, claude missing" before
* they commit to a case name. Read-only; never touches container lifecycle.
*/
2026-08-29 21:02:19 -07:00
/**
* Containers on the host's engine, for the adoption picker. Read-only and
* best-effort (mirror of the remote `:hostId/sessions` discovery route): an
* unreachable daemon yields an empty list rather than an error, because the
* container name is a free-text field the user can always type by hand.
*/
app . get (
'/api/docker-hosts/:hostId/containers' ,
2026-09-05 16:22:54 +02:00
async ( req , reply ) : Promise < ApiResponse < { containers : DockerContainerInfo [] }> > => {
// Enumerating every container on the engine is machine-level information (names,
// images, uptime), so it follows the docker-host policy rather than the case one.
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-08-29 21:02:19 -07:00
const { hostId } = req . params as { hostId : string };
const host = ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const containers = await listDockerContainers ({
engine : host.engine ?? 'docker' ,
context : host.context ,
daemonHost : host.daemonHost ,
});
return { success : true , data : { containers } };
}
);
2026-08-29 23:31:28 -07:00
/**
* Browse a directory INSIDE a container, for the adoption form's
* container-workdir picker. The host picker cannot answer this: for an adopted
* container nothing is mounted at a matching host path, so the field would
* otherwise be typed blind. Read-only — one `ls` through `docker exec`.
*/
2026-09-05 16:22:54 +02:00
app . post ( '/api/docker-cases/browse' , async ( req , reply ) : Promise < ApiResponse < DockerBrowseResult >> => {
// Reads a directory listing inside an ARBITRARY named container, so it is gated with
// the adopt flow it serves rather than with the (owner-scoped) case file routes.
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-08-29 23:31:28 -07:00
const body = parseBody ( DockerBrowseSchema , req . body );
const host = ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === body . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const result = await browseInContainer (
{
engine : host.engine ?? 'docker' ,
context : host.context ,
daemonHost : host.daemonHost ,
containerName : body.container ,
},
body . path || '/'
);
return { success : true , data : result };
});
2026-09-05 16:22:54 +02:00
app . post ( '/api/docker-cases/adopt-preflight' , async ( req , reply ) : Promise < ApiResponse < AdoptedContainerProbe >> => {
2026-08-29 21:02:19 -07:00
const body = parseBody ( DockerAdoptPreflightSchema , req . body );
2026-09-05 16:22:54 +02:00
const dockerCases = await readDockerCases ( CODEMAN_CONFIG_DIR );
// ⚠️ NOT plain `adminOnly`, unlike the two routes above: the run menu probes this for
// every docker case to learn which CLIs the CONTAINER has, so an admin-only gate would
// hide every agent mode from a non-admin's own docker case. A non-admin may therefore
// probe a container ALREADY linked to a case they can access — never an arbitrary one,
// which is the adopt-time question and stays admin-only with the rest of that flow.
if ( ! isAdmin ( req )) {
const owns = dockerCases . some (
( item ) =>
( item . container ?? dockerContainerName ( item . name )) === body . container &&
canAccessOwned ( getAuthUser ( req ), item . owner )
);
if ( ! owns ) {
reply . code ( 403 );
return createErrorResponse ( ApiErrorCode . FORBIDDEN , 'Admin only in multi-user mode' );
}
}
2026-08-29 21:02:19 -07:00
const host = ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === body . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const probe = await probeAdoptableContainer (
{
engine : host.engine ?? 'docker' ,
context : host.context ,
daemonHost : host.daemonHost ,
containerName : body.container ,
},
2026-09-05 16:21:51 +02:00
dockerAdoptProbeModes (),
2026-08-29 21:02:19 -07:00
body . containerWorkdir
2026-08-29 21:02:19 -07:00
);
return { success : true , data : probe };
});
2026-07-19 19:28:13 +02:00
// One-click "Run in Docker": create a NORMAL case (folder in CASES_DIR, scaffolded)
// AND link it to a hardened container with default settings, auto-provisioning a
// shared `default` docker host so the user never touches host/image/network fields.
app . post (
'/api/cases/docker-quickcreate' ,
2026-07-20 01:28:53 +02:00
async (
req
) : Promise <
ApiResponse < { case : unknown ; capsEnforced? : boolean ; isDesktop? : boolean ; imageBuilding? : boolean } >
> => {
2026-07-19 21:35:38 +02:00
const body = parseBody ( DockerQuickCreateSchema , req . body );
const { name , description } = body ;
2026-07-20 04:02:46 +02:00
const casePath = validatePathWithinBase ( name , resolveCasesDir ( getAuthUser ( req )));
2026-07-19 19:28:13 +02:00
if ( ! casePath ) return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case path' );
// Collision across every case kind.
const linkedCases = await readLinkedCases ();
const dockerCases = await readDockerCases ( CODEMAN_CONFIG_DIR );
const remoteCases = await readRemoteCases ( CODEMAN_CONFIG_DIR );
if (
existsSync ( casePath ) ||
dockerCases . some (( item ) => item . name === name ) ||
remoteCases . some (( item ) => item . name === name ) ||
linkedCases [ name ]
) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Case already exists' );
}
2026-07-19 21:35:38 +02:00
// The checkbox alone (no overrides) uses the shared `default` host; any tweaked
// setting gets a dedicated per-case host so it never mutates the shared default.
const hasOverrides = !! (
body . image ||
body . network ||
body . networkName ||
body . memory ||
body . cpus ||
body . gpus ||
body . mountCredentials !== undefined
);
const resources : { memory? : string ; cpus? : string } = {};
if ( body . memory ) resources . memory = body . memory ;
if ( body . cpus ) resources . cpus = body . cpus ;
const desiredHost : DockerHost = {
id : hasOverrides ? `q- ${ name } ` : DEFAULT_DOCKER_HOST_ID ,
label : hasOverrides ? `Case: ${ name } ` : 'Default' ,
image : body.image || DEFAULT_AGENT_IMAGE ,
network : body.network || 'bridge' ,
...( body . networkName ? { networkName : body.networkName } : {}),
...( Object . keys ( resources ). length ? { resources } : {}),
...( body . gpus ? { gpus : body.gpus } : {}),
mountCredentials : body.mountCredentials ?? true ,
resumeOnStart : true ,
hooksEnabled : true ,
};
2026-07-19 19:28:13 +02:00
const hosts = await readDockerHosts ( CODEMAN_CONFIG_DIR );
2026-07-19 21:35:38 +02:00
const existing = hosts . find (( item ) => item . id === desiredHost . id );
// Reuse the shared default if present; create/refresh a per-case host for overrides.
const host = existing && ! hasOverrides ? existing : desiredHost ;
if ( ! existing ) {
await writeDockerHosts ( CODEMAN_CONFIG_DIR , [... hosts , desiredHost ]);
} else if ( hasOverrides ) {
await writeDockerHosts (
CODEMAN_CONFIG_DIR ,
hosts . map (( h ) => ( h . id === desiredHost . id ? desiredHost : h ))
);
2026-07-19 19:28:13 +02:00
}
2026-07-20 01:28:53 +02:00
// Probe the daemon BEFORE scaffolding so a missing docker surfaces a clear
// error instead of leaving an orphaned case folder. The base image is NOT a
// blocker: a missing default image auto-builds in the background on first use.
2026-07-19 19:28:13 +02:00
const availability = await checkDockerAvailable ( host . engine );
if ( ! availability . ok ) {
return createErrorResponse (
ApiErrorCode . OPERATION_FAILED ,
availability . error || 'docker daemon is not available'
);
}
2026-07-20 04:02:46 +02:00
const dockerCase = {
name ,
type : 'docker' as const ,
hostId : host.id ,
hostWorkspacePath : casePath ,
owner : ownerFor ( req ),
};
2026-07-20 01:28:53 +02:00
const imageGate = await ensureCaseImage ( ctx . broadcast , toSessionDocker ( host , dockerCase ), name );
if ( ! imageGate . ok ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , imageGate . error );
2026-07-19 19:28:13 +02:00
}
// Scaffold the case folder exactly like a normal case.
try {
mkdirSync ( casePath , { recursive : true });
mkdirSync ( join ( casePath , 'src' ), { recursive : true });
const templatePath = await ctx . getDefaultClaudeMdPath ();
writeFileSync ( join ( casePath , 'CLAUDE.md' ), generateClaudeMd ( name , description || '' , templatePath ));
await writeHooksConfig ( casePath );
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , `Failed to create case: ${ getErrorMessage ( err ) } ` );
}
await writeDockerCases ( CODEMAN_CONFIG_DIR , [... dockerCases , dockerCase ]);
ctx . broadcast ( SseEvent . CaseCreated , { name , path : casePath });
ctx . broadcast ( SseEvent . CaseLinked , { name , path : casePath , type : 'docker' });
return {
success : true ,
2026-07-20 01:28:53 +02:00
data : {
case : dockerCase ,
capsEnforced : availability.capsEnforced ,
isDesktop : availability.isDesktop ,
imageBuilding : imageGate.imageBuilding ,
},
2026-07-19 15:28:44 +02:00
};
}
);
2026-07-19 17:38:01 +02:00
// ========== Docker export / import ==========
// Export a docker case to a portable bundle. Runs in the BACKGROUND (a full image
// save can take minutes) and broadcasts docker:exportComplete / docker:exportFailed.
app . post ( '/api/docker-cases/:name/export' , async ( req ) : Promise < ApiResponse < { started : true ; bundle : string }> > => {
const { name } = req . params as { name : string };
const { mode = 'full' } = parseBody ( DockerExportSchema , req . body ?? {});
const dockerCase = ( await readDockerCases ( CODEMAN_CONFIG_DIR )). find (( item ) => item . name === name );
if ( ! dockerCase ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker case not found' );
const host = ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === dockerCase . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const sessionDocker = toSessionDocker ( host , dockerCase );
2026-09-05 16:22:54 +02:00
// A full export `docker commit`s the container into an image. For an ADOPTED
// container that means packaging someone else's container — with whatever
// credentials its owner logged in with — into a bundle Codeman then hands out,
// and it is the one export step that touches the container at all. The
// workspace-only export is a plain host-directory tar and stays available.
if ( mode === 'full' && dockerCase . owned === false ) {
return createErrorResponse (
ApiErrorCode . FORBIDDEN ,
`Case " ${ name } " adopted an existing container. Codeman does not own it and will not commit it to an image — use a workspace-only export, or build the image yourself.`
);
}
2026-07-19 17:38:01 +02:00
if ( mode === 'full' && ! sessionDocker . mountCredentials ) {
return createErrorResponse (
ApiErrorCode . INVALID_INPUT ,
'full-image export is refused for a sealed container (its in-container login would ride the committed layer). Use a workspace-only export.'
);
}
const timestamp = Date . now ();
const bundle = exportBundleName ( name , timestamp , mode );
// Fire-and-forget: the client watches for the SSE completion event.
void exportDockerCase ({
docker : sessionDocker ,
caseName : name ,
timestamp ,
exportsDir : DOCKER_EXPORTS_DIR ,
mode ,
codemanVersion : APP_VERSION ,
})
. then (( result ) => {
ctx . broadcast ( SseEvent . DockerExportComplete , {
name ,
bundle : basename ( result . bundlePath ),
sizeBytes : result.sizeBytes ,
mode ,
});
})
. catch (( err ) => {
ctx . broadcast ( SseEvent . DockerExportFailed , { name , mode , error : getErrorMessage ( err ) });
});
return { success : true , data : { started : true , bundle } };
});
app . get ( '/api/docker-exports' , async () : Promise < ApiResponse < { exports : unknown [] }> > => {
return { success : true , data : { exports : await listDockerExports ( DOCKER_EXPORTS_DIR ) } };
});
// Download an export bundle (filename resolved WITHIN the exports dir — no traversal).
app . get ( '/api/docker-exports/:filename' , async ( req , reply ) => {
const { filename } = req . params as { filename : string };
if ( ! /^[a-zA-Z0-9._-]+\.tgz$/ . test ( filename )) {
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid bundle filename' );
}
const full = join ( DOCKER_EXPORTS_DIR , filename );
if ( ! existsSync ( full )) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Export not found' );
reply . header ( 'Content-Type' , 'application/gzip' );
reply . header ( 'Content-Disposition' , `attachment; filename=" ${ filename } "` );
reply . header ( 'X-Content-Type-Options' , 'nosniff' );
return reply . send ( createReadStream ( full ));
});
app . delete ( '/api/docker-exports/:filename' , async ( req ) : Promise < ApiResponse < { filename : string }> > => {
const { filename } = req . params as { filename : string };
if ( ! /^[a-zA-Z0-9._-]+\.tgz$/ . test ( filename )) {
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid bundle filename' );
}
const full = join ( DOCKER_EXPORTS_DIR , filename );
if ( ! existsSync ( full )) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Export not found' );
await fs . rm ( full , { force : true });
return { success : true , data : { filename } };
});
// Import a bundle (already present in the exports dir) into a NEW docker case.
app . post ( '/api/docker-cases/import' , async ( req ) : Promise < ApiResponse < { case : unknown }> > => {
const { bundle , newCaseName , destWorkspacePath } = parseBody ( DockerImportSchema , req . body );
const bundlePath = join ( DOCKER_EXPORTS_DIR , bundle );
if ( ! existsSync ( bundlePath )) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Bundle not found in exports dir' );
// Name-collision guard across ALL case kinds.
const linkedCases = await readLinkedCases ();
const dockerCases = await readDockerCases ( CODEMAN_CONFIG_DIR );
if (
dockerCases . some (( item ) => item . name === newCaseName ) ||
linkedCases [ newCaseName ] ||
2026-07-20 04:02:46 +02:00
existsSync ( join ( resolveCasesDir ( getAuthUser ( req )), newCaseName ))
2026-07-19 17:38:01 +02:00
) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'Case already exists' );
}
2026-07-20 12:33:12 +02:00
// Import extracts a tar into destWorkspacePath (later becomes Session.workingDir):
// confine it to the caller's own space. No-op for admins/single-user.
if ( ! isWorkingDirAllowed ( getAuthUser ( req ), destWorkspacePath )) {
return createErrorResponse ( ApiErrorCode . FORBIDDEN , 'destWorkspacePath is outside your workspace' );
}
2026-07-19 17:38:01 +02:00
const timestamp = Date . now ();
let result ;
try {
2026-07-20 02:36:02 +02:00
result = await importDockerBundle ({
bundlePath ,
destWorkspace : destWorkspacePath ,
engine : 'docker' ,
timestamp ,
newCaseName ,
});
2026-07-19 17:38:01 +02:00
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , `Import failed: ${ getErrorMessage ( err ) } ` );
}
2026-07-20 02:36:02 +02:00
// Create (or REFRESH) the dedicated docker host pointing at the quarantined
// imported image (full mode) or the manifest's base image (workspace-only).
// Refresh matters: after a case-delete + re-import of the same name, a stale
// `imported-<name>` host would silently pin the PREVIOUS import's image tag.
2026-07-19 17:38:01 +02:00
const hostId = `imported- ${ newCaseName } ` ;
const hosts = await readDockerHosts ( CODEMAN_CONFIG_DIR );
2026-07-20 02:36:02 +02:00
const importedHost = {
id : hostId ,
label : `Imported: ${ newCaseName } ` ,
engine : result.manifest.engine ,
image : result.importedImage ?? result . manifest . image ,
network : ([ 'bridge' , 'none' , 'custom' ]. includes ( result . manifest . network ) ? result . manifest . network : 'bridge' ) as
2026-08-29 23:31:28 -07:00
| 'bridge'
| 'none'
| 'custom' ,
2026-07-20 02:36:02 +02:00
};
await writeDockerHosts (
CODEMAN_CONFIG_DIR ,
hosts . some (( h ) => h . id === hostId )
? hosts . map (( h ) => ( h . id === hostId ? { ... h , ... importedHost } : h ))
: [... hosts , importedHost ]
);
2026-07-19 17:38:01 +02:00
const newCase = {
name : newCaseName ,
type : 'docker' as const ,
hostId ,
hostWorkspacePath : destWorkspacePath ,
containerWorkdir : result.manifest.containerWorkdir ,
2026-07-20 04:02:46 +02:00
owner : ownerFor ( req ),
2026-07-19 17:38:01 +02:00
};
await writeDockerCases ( CODEMAN_CONFIG_DIR , [... dockerCases , newCase ]);
ctx . broadcast ( SseEvent . DockerImportComplete , { name : newCaseName , path : destWorkspacePath , type : 'docker' });
return { success : true , data : { case : newCase } };
});
2026-07-20 02:36:02 +02:00
// Recreate-on-drift confirm (docs/docker-cases-plan.md §4): remove the case
// container so the next launch recreates it with the CURRENT host config. The
// workspace + transcripts ride bind mounts and survive; the conversation resumes
// via the case's lastClaudeSessionId. Refused while sessions of the case are live
// (removal would yank the container out from under their panes).
app . post (
'/api/docker-cases/:name/recreate' ,
async ( req ) : Promise < ApiResponse < { name : string ; container : string }> > => {
const { name } = req . params as { name : string };
2026-08-29 21:02:19 -07:00
// Ownership gate: recreate DESTROYS a container, so it must be scoped like
// delete is (`canAccessOwned`). Without it any user could rebuild another
// user's container by name.
const dockerCase = ( await readDockerCases ( CODEMAN_CONFIG_DIR )). find (
( item ) => item . name === name && canAccessOwned ( getAuthUser ( req ), item . owner )
);
2026-07-20 02:36:02 +02:00
if ( ! dockerCase ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker case not found' );
2026-08-29 21:02:19 -07:00
// An ADOPTED container is the user's own: there is nothing to recreate it
// from (no create-config, no image gate) and destroying it is exactly what
// adoption promises never to do.
if ( dockerCase . owned === false ) {
return createErrorResponse (
ApiErrorCode . FORBIDDEN ,
`Case " ${ name } " adopted an existing container. Codeman does not own its lifecycle and will not recreate it — rebuild it yourself, or unlink the case.`
);
}
2026-07-20 02:36:02 +02:00
const host = ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === dockerCase . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const sessionDocker = toSessionDocker ( host , dockerCase );
for ( const session of ctx . sessions . values ()) {
if ( session . docker ? . containerName === sessionDocker . containerName && session . pid ) {
return createErrorResponse (
ApiErrorCode . CONFLICT ,
`Sessions of case " ${ name } " are still running — stop them first, then recreate the container.`
);
}
}
try {
await removeDockerContainer ( sessionDocker );
} catch ( err ) {
return createErrorResponse (
ApiErrorCode . OPERATION_FAILED ,
`Failed to remove container: ${ getErrorMessage ( err ) } `
);
}
// Drop the per-container claude-config seed; it is regenerated at next launch.
await fs
. rm ( join ( dataPath ( 'docker-seeds' ), ` ${ sessionDocker . containerName } .json` ), { force : true })
. catch (() => {});
ctx . broadcast ( SseEvent . DockerContainerRecreated , { name , container : sessionDocker.containerName });
return { success : true , data : { name , container : sessionDocker.containerName } };
}
);
2026-02-28 19:12:45 +01:00
// Link an existing folder as a case
2026-07-20 12:33:12 +02:00
app . post ( '/api/cases/link' , async ( req , reply ) : Promise < ApiResponse < { case : { name : string ; path : string } }> > => {
// Linking writes an arbitrary absolute path into the shared ownerless registry:
// admin-only in multi-user mode (mirrors host CRUD + the admin-only GET listing).
const denied = adminOnly ( req , reply );
if ( denied ) return denied ;
2026-03-22 23:55:39 +01:00
const { name , path : folderPath } = parseBody ( LinkCaseSchema , req . body , 'Invalid request body' );
2026-02-28 19:12:45 +01:00
// Expand ~ to home directory
const expandedPath = folderPath . startsWith ( '~' ) ? join ( homedir (), folderPath . slice ( 1 )) : folderPath ;
// Validate the folder exists
if ( ! existsSync ( expandedPath )) {
return createErrorResponse ( ApiErrorCode . NOT_FOUND , `Folder not found: ${ expandedPath } ` );
}
2026-03-01 03:26:08 +01:00
// Check if case name already exists in CASES_DIR
2026-07-20 04:02:46 +02:00
const casePath = join ( resolveCasesDir ( getAuthUser ( req )), name );
2026-02-28 19:12:45 +01:00
if ( existsSync ( casePath )) {
return createErrorResponse ( ApiErrorCode . ALREADY_EXISTS , 'A case with this name already exists in codeman-cases.' );
}
// Load existing linked cases
2026-03-15 03:47:18 +01:00
const linkedCases = await readLinkedCases ();
2026-02-28 19:12:45 +01:00
// Check if name is already linked
if ( linkedCases [ name ]) {
return createErrorResponse (
ApiErrorCode . ALREADY_EXISTS ,
`Case " ${ name } " is already linked to ${ linkedCases [ name ] } `
);
}
// Save the linked case
linkedCases [ name ] = expandedPath ;
try {
2026-06-06 03:53:54 +02:00
const codemanDir = getDataDir ();
2026-02-28 19:12:45 +01:00
if ( ! existsSync ( codemanDir )) {
mkdirSync ( codemanDir , { recursive : true });
}
2026-03-15 03:47:18 +01:00
await fs . writeFile ( LINKED_CASES_FILE , JSON . stringify ( linkedCases , null , 2 ));
2026-03-03 22:40:18 +01:00
ctx . broadcast ( SseEvent . CaseLinked , { name , path : expandedPath });
2026-02-28 19:12:45 +01:00
return { success : true , data : { case : { name , path : expandedPath } } };
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , getErrorMessage ( err ));
}
});
2026-04-03 09:45:19 +08:00
// ========== Delete / Unlink Case ==========
app . delete ( '/api/cases/:name' , async ( req ) : Promise < ApiResponse < { name : string }> > => {
const { name } = req . params as { name : string };
2026-07-20 12:33:12 +02:00
const user = getAuthUser ( req );
2026-04-03 09:45:19 +08:00
2026-07-20 12:33:12 +02:00
if ( ! validatePathWithinBase ( name , resolveCasesDir ( user ))) {
2026-04-03 09:45:19 +08:00
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case name' );
}
2026-07-20 12:33:12 +02:00
// Fold ownership INTO the match (don't early-return): a non-owned same-named remote/
// docker case is skipped so control falls through to the caller's own local delete.
// canAccessOwned is all-true for admins/single-user, so flag-OFF stays byte-identical.
2026-06-08 10:40:56 +00:00
const remoteCases = await readRemoteCases ( CODEMAN_CONFIG_DIR );
2026-07-20 12:33:12 +02:00
if ( remoteCases . some (( item ) => item . name === name && canAccessOwned ( user , item . owner ))) {
2026-06-08 10:40:56 +00:00
await writeRemoteCases (
CODEMAN_CONFIG_DIR ,
2026-07-20 12:33:12 +02:00
remoteCases . filter (( item ) => ! ( item . name === name && canAccessOwned ( user , item . owner )))
2026-06-08 10:40:56 +00:00
);
ctx . broadcast ( SseEvent . CaseDeleted , { name , type : 'remote-unlinked' });
return { success : true , data : { name } };
}
2026-07-19 15:28:44 +02:00
const dockerCases = await readDockerCases ( CODEMAN_CONFIG_DIR );
2026-07-20 12:33:12 +02:00
const dockerCase = dockerCases . find (( item ) => item . name === name && canAccessOwned ( user , item . owner ));
2026-07-19 15:28:44 +02:00
if ( dockerCase ) {
await writeDockerCases (
CODEMAN_CONFIG_DIR ,
2026-07-20 12:33:12 +02:00
dockerCases . filter (( item ) => item !== dockerCase )
2026-07-19 15:28:44 +02:00
);
// Best-effort `docker rm -f` the per-case container (case-delete is the
// explicit teardown that removes it; the bind-mounted workspace survives).
2026-08-29 21:02:19 -07:00
// An ADOPTED container is skipped entirely: unlinking the case must leave
// the user's own container running and untouched. The seed file is skipped
// with it — adoption never wrote one.
const host =
dockerCase . owned === false
? undefined
: ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === dockerCase . hostId );
2026-07-19 15:28:44 +02:00
if ( host ) {
2026-07-20 01:28:53 +02:00
const sessionDocker = toSessionDocker ( host , dockerCase );
2026-07-19 15:28:44 +02:00
try {
2026-07-20 01:28:53 +02:00
exec ( buildDockerRemoveCommand ( sessionDocker ), { timeout : 15_000 }, () => {});
2026-07-19 15:28:44 +02:00
} catch {
/* best-effort — never blocks the unlink */
}
2026-07-20 01:28:53 +02:00
// Remove the per-container claude-config seed file (account metadata copy).
await fs
. rm ( join ( dataPath ( 'docker-seeds' ), ` ${ sessionDocker . containerName } .json` ), { force : true })
. catch (() => {});
2026-07-19 15:28:44 +02:00
}
ctx . broadcast ( SseEvent . CaseDeleted , { name , type : 'docker-unlinked' });
return { success : true , data : { name } };
}
2026-07-20 12:33:12 +02:00
// Check linked cases first — unlink only, don't delete the actual directory.
// Linked cases carry no owner (admin-only WRITE in multi-user mode), so a non-admin
// must not unlink one either; skip so control falls through to their local delete.
2026-04-03 09:45:19 +08:00
const linkedCases = await readLinkedCases ();
2026-07-20 12:33:12 +02:00
if ( linkedCases [ name ] && ( ! isMultiUserMode () || isAdmin ( req ))) {
2026-04-03 09:45:19 +08:00
delete linkedCases [ name ];
try {
await fs . writeFile ( LINKED_CASES_FILE , JSON . stringify ( linkedCases , null , 2 ));
ctx . broadcast ( SseEvent . CaseDeleted , { name , type : 'unlinked' });
return { success : true , data : { name } };
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , getErrorMessage ( err ));
}
}
// Case in CASES_DIR — delete the entire directory
2026-07-20 04:02:46 +02:00
const casePath = join ( resolveCasesDir ( getAuthUser ( req )), name );
2026-04-03 09:45:19 +08:00
if ( ! existsSync ( casePath )) {
return createErrorResponse ( ApiErrorCode . NOT_FOUND , `Case " ${ name } " not found` );
}
try {
await fs . rm ( casePath , { recursive : true , force : true });
ctx . broadcast ( SseEvent . CaseDeleted , { name , type : 'deleted' });
return { success : true , data : { name } };
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , getErrorMessage ( err ));
}
});
// ========== Reorder Cases ==========
app . put ( '/api/cases/order' , async ( req ) : Promise < ApiResponse < { order : string [] }> > => {
const { order } = parseBody ( CaseOrderSchema , req . body , 'Invalid order data' );
try {
2026-06-06 03:53:54 +02:00
const dir = getDataDir ();
2026-04-03 09:45:19 +08:00
if ( ! existsSync ( dir )) {
mkdirSync ( dir , { recursive : true });
}
let existing : Record < string , unknown > = {};
try {
existing = JSON . parse ( await fs . readFile ( SETTINGS_PATH , 'utf-8' ));
} catch {
/* ignore */
}
const merged = { ... existing , caseOrder : order };
await fs . writeFile ( SETTINGS_PATH , JSON . stringify ( merged , null , 2 ));
ctx . broadcast ( SseEvent . CaseOrderChanged , { order });
return { success : true , data : { order } };
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , getErrorMessage ( err ));
}
});
2026-02-28 19:12:45 +01:00
app . get ( '/api/cases/:name' , async ( req ) => {
const { name } = req . params as { name : string };
2026-07-20 04:02:46 +02:00
if ( ! validatePathWithinBase ( name , resolveCasesDir ( getAuthUser ( req )))) {
2026-03-01 17:26:31 +01:00
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case name' );
}
2026-07-20 12:33:12 +02:00
// Fold ownership INTO the match (don't early-return): a non-owned same-named remote/
// docker case is skipped so control falls through to the caller's own LOCAL case
// (remote/docker names are globally unique, local names per-user). No metadata is
// disclosed for a foreign case. canAccessOwned is allow-all for admins/single-user.
2026-06-11 18:43:39 +00:00
const remoteCases = await readRemoteCases ( CODEMAN_CONFIG_DIR );
2026-07-20 12:33:12 +02:00
const remoteCase = remoteCases . find (( item ) => item . name === name && canAccessOwned ( getAuthUser ( req ), item . owner ));
2026-06-11 18:43:39 +00:00
if ( remoteCase ) {
const host = ( await readRemoteHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === remoteCase . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Remote host not found' );
return {
name ,
path : remoteDisplayPath ({ username : host.username , host : host.host , path : remoteCase.remotePath }),
hasClaudeMd : false ,
location : 'remote' ,
remote : {
hostId : host.id ,
host : host.host ,
username : host.username ,
path : remoteCase.remotePath ,
},
};
}
2026-07-20 12:33:12 +02:00
const dockerCase = ( await readDockerCases ( CODEMAN_CONFIG_DIR )). find (
( item ) => item . name === name && canAccessOwned ( getAuthUser ( req ), item . owner )
);
2026-07-19 15:28:44 +02:00
if ( dockerCase ) {
const host = ( await readDockerHosts ( CODEMAN_CONFIG_DIR )). find (( item ) => item . id === dockerCase . hostId );
if ( ! host ) return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Docker host not found' );
const container = dockerCase . container ?? dockerContainerName ( dockerCase . name );
return {
name ,
path : dockerDisplayPath ({ container , path : dockerCase.hostWorkspacePath }),
2026-10-01 13:58:55 -04:00
hasClaudeMd : await boundedPathExists ( join ( dockerCase . hostWorkspacePath , 'CLAUDE.md' )),
2026-07-19 15:28:44 +02:00
location : 'docker' ,
docker : {
hostId : host.id ,
container ,
image : host.image ,
path : dockerCase.hostWorkspacePath ,
2026-09-01 20:45:13 -07:00
containerWorkdir : dockerCase.containerWorkdir ?? dockerCase . hostWorkspacePath ,
2026-07-19 15:28:44 +02:00
network : host.network ?? 'bridge' ,
2026-09-14 23:48:10 +02:00
...( dockerCase . owned === false ? { owned : false } : {}),
2026-07-19 15:28:44 +02:00
},
};
}
2026-07-20 04:02:46 +02:00
const casePath = await resolveCasePath ( name , getAuthUser ( req ));
2026-10-04 20:30:40 -04:00
const linked = casePath !== join ( resolveCasesDir ( getAuthUser ( req )), name );
2026-02-28 19:12:45 +01:00
2026-10-04 20:30:40 -04:00
// NOT_FOUND means DEFINITELY absent: the Run button creates a case on it, so
// a path that merely did not answer (a dead network mount) must never get it.
// One path, asked for explicitly: probe it even while unrelated mounts are dead.
const state = await probePath ( casePath , { pastCap : true });
if ( state === 'absent' ) {
2026-02-28 19:12:45 +01:00
return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Case not found' );
}
2026-10-04 20:30:40 -04:00
if ( state === 'unknown' ) {
// The linked registry knows where the case lives, so say where, and that
// it is not answering. A local case has no such record to fall back on.
if ( ! linked ) {
return createErrorResponse (
ApiErrorCode . OPERATION_FAILED ,
2026-10-05 20:00:03 +02:00
describeUnknownPath ( 'Case folder' , casePath , { pastCap : true })
2026-10-04 20:30:40 -04:00
);
}
return { name , path : casePath , hasClaudeMd : false , linked : true , unreachable : true };
}
2026-02-28 19:12:45 +01:00
return {
name ,
path : casePath ,
2026-10-05 09:51:27 -04:00
// Probed like the folder above, or a healthy case reads as having no CLAUDE.md under the cap.
hasClaudeMd : ( await probePath ( join ( casePath , 'CLAUDE.md' ), { pastCap : true })) === 'present' ,
2026-03-15 03:47:18 +01:00
...( linked && { linked : true }),
2026-02-28 19:12:45 +01:00
};
});
// Read @fix_plan.md from a case directory (for wizard to detect existing plans)
app . get ( '/api/cases/:name/fix-plan' , async ( req ) => {
const { name } = req . params as { name : string };
2026-07-20 04:02:46 +02:00
if ( ! validatePathWithinBase ( name , resolveCasesDir ( getAuthUser ( req )))) {
2026-03-01 17:26:31 +01:00
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case name' );
}
2026-03-01 03:26:08 +01:00
// Get case path (check linked cases first, then CASES_DIR)
2026-07-20 04:02:46 +02:00
const casePath = await resolveCasePath ( name , getAuthUser ( req ));
2026-02-28 19:12:45 +01:00
const fixPlanPath = join ( casePath , '@fix_plan.md' );
2026-10-04 20:30:40 -04:00
const fixPlanState = await probePath ( fixPlanPath , { pastCap : true });
if ( fixPlanState === 'unknown' ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , 'Case folder is not responding or not readable' );
}
if ( fixPlanState === 'absent' ) {
2026-06-10 01:30:43 +02:00
return { exists : false , content : null , todos : [] };
2026-02-28 19:12:45 +01:00
}
try {
const content = await fs . readFile ( fixPlanPath , 'utf-8' );
// Parse todos from the content (similar to ralph-tracker's importFixPlanMarkdown)
const todos : Array < {
content : string ;
status : 'pending' | 'in_progress' | 'completed' ;
priority : string | null ;
} > = [];
const todoPattern = /^-\s*\[([ xX-])\]\s*(.+)$/ ;
const p0HeaderPattern = /^##\s*(High Priority|Critical|P0|Critical Path)/i ;
const p1HeaderPattern = /^##\s*(Standard|P1|Medium Priority)/i ;
const p2HeaderPattern = /^##\s*(Nice to Have|P2|Low Priority)/i ;
const completedHeaderPattern = /^##\s*Completed/i ;
let currentPriority : string | null = null ;
let inCompletedSection = false ;
for ( const line of content . split ( '\n' )) {
const trimmed = line . trim ();
if ( p0HeaderPattern . test ( trimmed )) {
currentPriority = 'P0' ;
inCompletedSection = false ;
continue ;
}
if ( p1HeaderPattern . test ( trimmed )) {
currentPriority = 'P1' ;
inCompletedSection = false ;
continue ;
}
if ( p2HeaderPattern . test ( trimmed )) {
currentPriority = 'P2' ;
inCompletedSection = false ;
continue ;
}
if ( completedHeaderPattern . test ( trimmed )) {
inCompletedSection = true ;
continue ;
}
const match = trimmed . match ( todoPattern );
if ( match ) {
const [, checkboxState , taskContent ] = match ;
let status : 'pending' | 'in_progress' | 'completed' ;
if ( inCompletedSection || checkboxState === 'x' || checkboxState === 'X' ) {
status = 'completed' ;
} else if ( checkboxState === '-' ) {
status = 'in_progress' ;
} else {
status = 'pending' ;
}
todos . push ({
content : taskContent.trim (),
status ,
priority : inCompletedSection ? null : currentPriority ,
});
}
}
// Calculate stats in a single pass for better performance
let pending = 0 ,
inProgress = 0 ,
completed = 0 ;
for ( const t of todos ) {
if ( t . status === 'pending' ) pending ++ ;
else if ( t . status === 'in_progress' ) inProgress ++ ;
else if ( t . status === 'completed' ) completed ++ ;
}
const stats = { total : todos.length , pending , inProgress , completed };
return {
exists : true ,
content ,
todos ,
stats ,
};
} catch ( err ) {
return createErrorResponse ( ApiErrorCode . OPERATION_FAILED , `Failed to read @fix_plan.md: ${ err } ` );
}
});
2026-03-03 22:40:18 +01:00
// ═══════════════════════════════════════════════════════════════
// Ralph Wizard Files (per-case prompt/result serving)
// ═══════════════════════════════════════════════════════════════
// ========== List Wizard Files ==========
2026-02-28 19:12:45 +01:00
app . get ( '/api/cases/:caseName/ralph-wizard/files' , async ( req ) => {
const { caseName } = req . params as { caseName : string };
2026-07-20 04:02:46 +02:00
if ( ! validatePathWithinBase ( caseName , resolveCasesDir ( getAuthUser ( req )))) {
2026-02-28 19:12:45 +01:00
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case name' );
}
2026-07-20 04:02:46 +02:00
const casePath = await resolveCasePath ( caseName , getAuthUser ( req ));
2026-02-28 19:12:45 +01:00
const wizardDir = join ( casePath , 'ralph-wizard' );
if ( ! existsSync ( wizardDir )) {
return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'Ralph wizard directory not found' );
}
// List all subdirectories and their files
const files : Array < { agentType : string ; promptFile? : string ; resultFile? : string } > = [];
const entries = readdirSync ( wizardDir , { withFileTypes : true });
for ( const entry of entries ) {
if ( entry . isDirectory ()) {
const agentDir = join ( wizardDir , entry . name );
const agentFiles : { agentType : string ; promptFile? : string ; resultFile? : string } = {
agentType : entry.name ,
};
if ( existsSync ( join ( agentDir , 'prompt.md' ))) {
agentFiles . promptFile = ` ${ entry . name } /prompt.md` ;
}
if ( existsSync ( join ( agentDir , 'result.json' ))) {
agentFiles . resultFile = ` ${ entry . name } /result.json` ;
}
if ( agentFiles . promptFile || agentFiles . resultFile ) {
files . push ( agentFiles );
}
}
}
return { success : true , data : { files , caseName } };
});
// Read a specific ralph-wizard file
// Cache disabled to ensure fresh prompts when starting new plan generations
app . get ( '/api/cases/:caseName/ralph-wizard/file/:filePath' , async ( req , reply ) => {
const { caseName , filePath } = req . params as { caseName : string ; filePath : string };
2026-07-20 04:02:46 +02:00
if ( ! validatePathWithinBase ( caseName , resolveCasesDir ( getAuthUser ( req )))) {
2026-03-11 19:13:49 +01:00
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid case name' );
}
2026-02-28 19:12:45 +01:00
// Prevent browser caching - prompts change between plan generations
reply . header ( 'Cache-Control' , 'no-store, no-cache, must-revalidate' );
reply . header ( 'Pragma' , 'no-cache' );
reply . header ( 'Expires' , '0' );
2026-07-20 04:02:46 +02:00
const casePath = await resolveCasePath ( caseName , getAuthUser ( req ));
2026-02-28 19:12:45 +01:00
const wizardDir = join ( casePath , 'ralph-wizard' );
// Decode the file path (it may be URL encoded)
const decodedPath = decodeURIComponent ( filePath );
const fullPath = join ( wizardDir , decodedPath );
// Security: ensure path is within wizard directory
const resolvedPath = resolve ( fullPath );
const resolvedWizard = resolve ( wizardDir );
if ( ! resolvedPath . startsWith ( resolvedWizard )) {
return createErrorResponse ( ApiErrorCode . INVALID_INPUT , 'Invalid file path' );
}
let content : string ;
try {
content = await fs . readFile ( fullPath , 'utf-8' );
} catch ( err ) {
if (( err as NodeJS . ErrnoException ). code === 'ENOENT' ) {
return createErrorResponse ( ApiErrorCode . NOT_FOUND , 'File not found' );
}
throw err ;
}
const isJson = filePath . endsWith ( '.json' );
// Parse JSON content safely (may contain invalid JSON or unescaped control characters)
let parsed : unknown = null ;
if ( isJson ) {
try {
parsed = JSON . parse ( content );
} catch {
// Try repairing common JSON issues (unescaped control characters, trailing commas)
try {
let repaired = content ;
// Fix trailing commas before closing brackets
repaired = repaired . replace ( /,(\s*[\]}])/g , '$1' );
// Fix unescaped control characters within JSON strings
repaired = repaired . replace ( /"([^"\\]|\\.)*"/g , ( match ) => {
return match
. replace ( /\n/g , '\\n' )
. replace ( /\r/g , '\\r' )
. replace ( /\t/g , '\\t' )
. replace (
// eslint-disable-next-line no-control-regex
/[\x00-\x1f]/g ,
( c ) => `\\u ${ c . charCodeAt ( 0 ). toString ( 16 ). padStart ( 4 , '0' ) } `
);
});
parsed = JSON . parse ( repaired );
} catch {
// Still invalid - return null for parsed, content available as raw string
}
}
}
return {
success : true ,
data : {
content ,
filePath : decodedPath ,
isJson ,
parsed ,
},
};
});
}