mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-17 19:59:33 +01:00
fix: refuse to git fetch while the Docker case list cannot be read (release review)
readDockerCases() reads an unreadable or half-written docker-cases.json as [], so a fetch (which runs repository hooks, and runs unattended on the interval) could run inside a Docker workspace. The fetch paths now read it through a strict reader: the Fetch button answers a failed run (state 'error') and the scheduler skips the tick, undoes its picks and logs once. The status and diff routes are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b89f15c12f
commit
1752d7ee2c
8 files changed
+221
-18
No files matched your search
@@ -130,6 +130,32 @@ export async function readDockerCases(configDir: string): Promise<DockerCase[]>
|
||||
return readJsonArray<DockerCase>(dockerCasesPath(configDir));
|
||||
}
|
||||
|
||||
/**
|
||||
* The Docker cases for a caller that must fail CLOSED (the git fetch paths): a missing file is no cases,
|
||||
* but a file that cannot be read, does not parse or is not an array rejects, where {@link readDockerCases}
|
||||
* reads all of those as `[]`. The writer truncates and rewrites the file in place, so a read can land on an
|
||||
* empty or half-written file, and to a caller deciding where git may run that must never look like "no
|
||||
* Docker workspaces". The parser's own message is not repeated (V8 quotes the text around the fault).
|
||||
*/
|
||||
export async function readDockerCasesOrThrow(configDir: string): Promise<DockerCase[]> {
|
||||
const path = dockerCasesPath(configDir);
|
||||
let raw: string;
|
||||
try {
|
||||
raw = await fs.readFile(path, 'utf-8');
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return [];
|
||||
throw err;
|
||||
}
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = undefined;
|
||||
}
|
||||
if (!Array.isArray(parsed)) throw new Error(`${path} is not a valid JSON array`);
|
||||
return parsed as DockerCase[];
|
||||
}
|
||||
|
||||
export async function writeDockerCases(configDir: string, cases: DockerCase[]): Promise<void> {
|
||||
await writeJsonArray(configDir, dockerCasesPath(configDir), cases);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@
|
||||
* app-wide switch is off — that is what a per-session switch has to mean.
|
||||
* - Remote and Docker sessions are never fetched, exactly as the Git panel excludes them, and the Docker
|
||||
* workspace roots are checked again every tick, so a session whose case became a Docker case stops
|
||||
* fetching at the next tick.
|
||||
* fetching at the next tick. A Docker case list that cannot be read fails CLOSED: the tick fetches
|
||||
* nothing (a fetch runs a repository's hooks, and without the list nothing tells which folders a
|
||||
* container can write to), the folders it picked stay due for the next tick, and it is logged once
|
||||
* until a read succeeds again.
|
||||
* - A folder is fetched by at most one run at a time, and the run belongs to the FOLDER, not to one
|
||||
* session: two sessions open on the same folder are the same clone, so they share one run and both get
|
||||
* its result. State is keyed by `resolve(workingDir)` — deliberately not `realpath`, because `realpath`
|
||||
@@ -91,6 +94,10 @@ export interface GitFetchPorts {
|
||||
getSession(id: string): GitFetchSessionView | undefined;
|
||||
/** Read FRESH every tick; that is the whole point of the tick-driven shape. */
|
||||
readSettings(): Promise<GitFetchSettings>;
|
||||
/**
|
||||
* Host paths of every Docker case workspace. REJECTS when the list exists but cannot be read or parsed; the
|
||||
* tick then fetches nothing and the folders it picked are due again on the next tick.
|
||||
*/
|
||||
dockerWorkspaces(): Promise<string[]>;
|
||||
/**
|
||||
* Bounded probe (rule #516), default options: a bulk scan keeps the stall cap, so this must not be called
|
||||
@@ -134,6 +141,8 @@ export class GitFetchScheduler {
|
||||
private stopped = false;
|
||||
/** The last settings read failed and was logged; cleared by the next read that succeeds. */
|
||||
private settingsUnreadable = false;
|
||||
/** The same for the Docker case list. */
|
||||
private dockerCasesUnreadable = false;
|
||||
|
||||
constructor(ports: GitFetchPorts) {
|
||||
this.ports = ports;
|
||||
@@ -185,6 +194,9 @@ export class GitFetchScheduler {
|
||||
// group ordered [short, long] and a group ordered [long, short] would have behaved differently, and the
|
||||
// run belongs to the folder, so no session in the group should.
|
||||
const due = new Map<string, GroupMember[]>();
|
||||
// What each picked folder's clock said before this tick stamped it, so a tick that ends up fetching
|
||||
// nothing (an unreadable Docker case list, below) can leave the folders due again.
|
||||
const stampedOver = new Map<string, number | undefined>();
|
||||
for (const [folder, group] of groups) {
|
||||
if (this.inFlight.has(folder)) continue;
|
||||
const started = this.lastRunAt.get(folder);
|
||||
@@ -198,13 +210,35 @@ export class GitFetchScheduler {
|
||||
}
|
||||
this.inFlight.add(folder);
|
||||
// The clock starts at the selection, so a slow folder does not shift its own schedule.
|
||||
stampedOver.set(folder, started);
|
||||
this.lastRunAt.set(folder, now);
|
||||
due.set(folder, group);
|
||||
}
|
||||
if (!due.size) return 0;
|
||||
|
||||
// Only now, because nothing above needs it: a quiet tick does not read the Docker case list.
|
||||
const dockerWorkspaces = await this.ports.dockerWorkspaces();
|
||||
let dockerWorkspaces: string[];
|
||||
try {
|
||||
dockerWorkspaces = await this.ports.dockerWorkspaces();
|
||||
} catch (err) {
|
||||
// Fail CLOSED: a fetch runs the repository's hooks, and without the list nothing tells which folders a
|
||||
// container can write to. Nothing ran, so the picks are undone (a stamp a `remove()` already cleared
|
||||
// stays cleared) and the folders are due again on the next tick instead of after their interval.
|
||||
for (const [folder, started] of stampedOver) {
|
||||
this.inFlight.delete(folder);
|
||||
if (this.lastRunAt.get(folder) !== now) continue;
|
||||
if (started === undefined) this.lastRunAt.delete(folder);
|
||||
else this.lastRunAt.set(folder, started);
|
||||
}
|
||||
if (!this.dockerCasesUnreadable) {
|
||||
this.dockerCasesUnreadable = true;
|
||||
this.ports.log?.(
|
||||
`git fetch: Docker case list unreadable, nothing fetched until it reads again: ${getErrorMessage(err)}`
|
||||
);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
this.dockerCasesUnreadable = false;
|
||||
// ONE bound for the git processes of the whole tick, handed to every run so no folder gets its own:
|
||||
// each fetch is one git process, and a folder can hold up to `MAX_REPOS_LIMIT` of them, so a bound per
|
||||
// run would let `FETCH_CONCURRENCY` folders × `MAX_REPOS_LIMIT` processes run at once.
|
||||
|
||||
@@ -17,7 +17,7 @@ import type { FastifyInstance } from 'fastify';
|
||||
import { ApiErrorCode, createErrorResponse, getErrorMessage, type ApiResponse } from '../../types.js';
|
||||
import { redactGitCredentials } from '../../git-clone.js';
|
||||
import { emptyReport, fetchWorkspaceOnce, gitFetchStatusFrom, type GitFetchReport } from '../../git-fetch.js';
|
||||
import { readDockerCases } from '../../docker-hosts.js';
|
||||
import { readDockerCasesOrThrow } from '../../docker-hosts.js';
|
||||
import { getDataDir } from '../../config/instance.js';
|
||||
import { findSessionOrFail, persistAndBroadcastSession } from '../route-helpers.js';
|
||||
import {
|
||||
@@ -38,9 +38,18 @@ import type { EventPort, SessionPort } from '../ports/index.js';
|
||||
* Host paths of every Docker case workspace: repositories at or inside these are never inspected.
|
||||
* Exported so the interval scheduler (src/git-fetch-scheduler.ts) uses the SAME list — the Docker rule has
|
||||
* to be one rule, checked by every path that could run git.
|
||||
*
|
||||
* REJECTS when `docker-cases.json` exists but cannot be read or parsed (its writer truncates and rewrites it
|
||||
* in place, so a read can land mid-write). The fetch paths fail closed on that: a fetch runs a repository's
|
||||
* own hooks, and without the list nothing tells which folders a container can write to. The read-only
|
||||
* status and diff routes read it as no Docker workspaces, as they always have.
|
||||
*/
|
||||
export const defaultDockerWorkspaces = async (): Promise<string[]> =>
|
||||
(await readDockerCases(getDataDir()).catch(() => [])).map((c) => c.hostWorkspacePath).filter(Boolean);
|
||||
(await readDockerCasesOrThrow(getDataDir())).map((c) => c.hostWorkspacePath).filter(Boolean);
|
||||
|
||||
/** The read-only routes' reading of the list: unchanged, an unreadable list is no Docker workspaces. */
|
||||
const dockerWorkspacesOrNone = (dockerWorkspaces: () => Promise<string[]>): Promise<string[]> =>
|
||||
dockerWorkspaces().catch(() => []);
|
||||
|
||||
/**
|
||||
* The repository limit and git timeout a request asks for (Settings → Bottom bar, per device):
|
||||
@@ -78,7 +87,7 @@ export function registerGitStatusRoutes(
|
||||
git,
|
||||
fresh: fresh === '1',
|
||||
...limits,
|
||||
dockerWorkspaces: await dockerWorkspaces(),
|
||||
dockerWorkspaces: await dockerWorkspacesOrNone(dockerWorkspaces),
|
||||
}),
|
||||
};
|
||||
});
|
||||
@@ -102,7 +111,7 @@ export function registerGitStatusRoutes(
|
||||
? await findWorkspaceRepo(session.workingDir, repo, {
|
||||
git,
|
||||
...limits,
|
||||
dockerWorkspaces: await dockerWorkspaces(),
|
||||
dockerWorkspaces: await dockerWorkspacesOrNone(dockerWorkspaces),
|
||||
})
|
||||
: null;
|
||||
const status = repoRoot
|
||||
@@ -150,9 +159,24 @@ export function registerGitStatusRoutes(
|
||||
const session = findSessionOrFail(ctx, id, req);
|
||||
if (session.remote) return { success: true, data: emptyReport('unsupported', { reason: 'remote' }) };
|
||||
if (session.docker) return { success: true, data: emptyReport('unsupported', { reason: 'docker' }) };
|
||||
// Fail CLOSED on a Docker case list that cannot be read: a fetch runs the repository's hooks, so it never
|
||||
// runs without knowing which folders a container can write to. Answered as a failed run (`state: 'error'`),
|
||||
// published like one, so the Session Options Git row shows it.
|
||||
let dockerRoots: string[];
|
||||
try {
|
||||
dockerRoots = await dockerWorkspaces();
|
||||
} catch (err) {
|
||||
console.warn(`[git fetch] Docker case list unreadable, nothing fetched: ${getErrorMessage(err)}`);
|
||||
const report = emptyReport('error', {
|
||||
error:
|
||||
'Nothing was fetched: Codeman could not read its Docker case list (docker-cases.json), so it cannot tell which folders a container can write to.',
|
||||
});
|
||||
if (session.setGitFetchStatus(gitFetchStatusFrom(report, 'click'))) persistAndBroadcastSession(ctx, session);
|
||||
return { success: true, data: report };
|
||||
}
|
||||
const run = await fetchWorkspaceOnce(
|
||||
session.workingDir,
|
||||
{ git, ...limits, dockerWorkspaces: await dockerWorkspaces() },
|
||||
{ git, ...limits, dockerWorkspaces: dockerRoots },
|
||||
'click'
|
||||
);
|
||||
// The response answers the click, and the run is also the session's current truth: what the panel's
|
||||
|
||||
+2
-1
@@ -406,7 +406,8 @@ export class WebServer extends EventEmitter {
|
||||
sessionIds: () => this.sessions.keys(),
|
||||
getSession: (id) => this.sessions.get(id),
|
||||
readSettings: readGitFetchSettings,
|
||||
// The same Docker list the panel's routes use: the rule has to be one rule.
|
||||
// The same Docker list the panel's Fetch button uses: the rule has to be one rule. It rejects on a list
|
||||
// that cannot be read, and the tick then fetches nothing (fail closed).
|
||||
dockerWorkspaces: defaultDockerWorkspaces,
|
||||
// Bounded probe, DEFAULT options (rule #516): a tick is a bulk scan, so it keeps the stall cap and must
|
||||
// never be handed `pastCap`. A folder on a mount that stopped answering is skipped, not fetched.
|
||||
|
||||
Reference in new issue
Block a user