Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion desktop/src/app-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function AppControl({ projectId, status, onStatus }: { projectId: string;
<button disabled={!config.url} onClick={() => void window.codepilot.openProjectApp(projectId)} className="flex items-center gap-2 rounded-lg border border-white/10 px-4 py-2.5 text-sm text-zinc-300 disabled:opacity-40"><ArrowTopRightOnSquareIcon className="size-4" />Open app</button>
</div>
</section>
<section className="flex min-h-96 flex-col overflow-hidden rounded-2xl border border-white/10 bg-white/[.025]"><header className="border-b border-white/10 p-5 text-sm font-semibold">App output</header><pre className="min-h-0 flex-1 overflow-auto whitespace-pre-wrap bg-black/20 p-5 text-[11px] leading-5 text-zinc-500">{status?.logs.length ? status.logs.join("") : "Start the app to see its output here."}</pre></section>
<section className="flex min-h-96 flex-col overflow-hidden rounded-2xl border border-white/10 bg-white/[.025]"><header className="flex items-center justify-between border-b border-white/10 p-5 text-sm font-semibold"><span>App output</span><span className="font-mono text-[9px] font-normal uppercase tracking-widest text-zinc-600">Persisted locally</span></header><pre className="min-h-0 flex-1 overflow-auto whitespace-pre-wrap bg-black/20 p-5 text-[11px] leading-5 text-zinc-500">{status?.logs.length ? status.logs.join("") : "Start the app to see its output here."}</pre></section>
</div>
</div>;
}
Expand Down
171 changes: 160 additions & 11 deletions desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ import {
} from "../../src/project-onboarding";
import { removeProjectRegistration } from "../../src/project-registry";
import { assertGitHubPublicationAllowed, parseGitHubAuthStatus, type GitHubStatus } from "../../src/github-auth";
import {
AppLogStore,
formatAppLogEntry,
type AppLogStream,
} from "../../src/app-log-store";
declare const MAIN_WINDOW_VITE_DEV_SERVER_URL: string | undefined;
declare const MAIN_WINDOW_VITE_NAME: string;
if (started) app.quit();
Expand All @@ -64,7 +69,16 @@ const pmThreads = new Map<string, string>();
const pmTurns = new KeyedSerialQueue();
const pmActivity = new Map<string, number>();
const appProcesses = new Map<string, ReturnType<typeof spawn>>();
const appProcessState = new Map<string, { startedAt?: string; logs: string[] }>();
type AppProcessRuntime = {
startedAt?: string;
sessionId?: string;
stopping?: boolean;
persistenceError?: boolean;
logs: string[];
};
const appProcessState = new Map<string, AppProcessRuntime>();
const appLogStores = new Map<string, AppLogStore>();
let appIsQuitting = false;
const defaultGlobalSettings: GlobalSettings = { defaultProvider: "codex", scaffold: { mode: "guided", autoDeploy: false, onboardingPrompt: "You are onboarding me into a new project. Begin by asking focused questions about the users, problem, core workflow, constraints, technology preferences, and definition of success. Summarize decisions as we go. Create small implementation tasks only after requirements are agreed, then continue helping me shape and build the project incrementally." }, github: { allowWrites: false } };
const file = () => join(app.getPath("userData"), "state.json");
function load(): DesktopState {
Expand Down Expand Up @@ -324,6 +338,8 @@ function registerIPC() {
state.runs = [];
pmThreads.delete(project.id);
appProcessState.delete(project.id);
appLogStores.get(project.id)?.close();
appLogStores.delete(project.id);
save(state);
return hydrate();
});
Expand Down Expand Up @@ -498,8 +514,70 @@ function saveAppConfig(input: AppConfig, project: Project) {
mkdirSync(dirname(appConfigPath(project)), { recursive: true });
writeFileSync(appConfigPath(project), JSON.stringify({ command, workingDirectory, url }, null, 2));
}
function projectAppLogStore(project: Project): AppLogStore {
const existing = appLogStores.get(project.id);
if (existing) return existing;
const store = new AppLogStore(
join(project.repo, ".codepilot", "codepilot.db"),
);
appLogStores.set(project.id, store);
return store;
}
function persistedAppLogs(project: Project): string[] {
try {
return projectAppLogStore(project).recent(500).map(formatAppLogEntry);
} catch {
return [];
}
}
function appendAppLog(
project: Project,
state: AppProcessRuntime,
stream: AppLogStream,
payload: string,
): void {
const timestamp = new Date().toISOString();
state.logs.push(formatAppLogEntry({ timestamp, stream, payload }));
state.logs = state.logs.slice(-500);
if (!state.sessionId || appIsQuitting) return;
try {
projectAppLogStore(project).append(
state.sessionId,
stream,
payload,
timestamp,
);
} catch (error) {
if (state.persistenceError) return;
state.persistenceError = true;
state.logs.push(
formatAppLogEntry({
timestamp: new Date().toISOString(),
stream: "system",
payload: `App log persistence failed: ${error instanceof Error ? error.message : String(error)}`,
}),
);
}
}
function endAppLogSession(
project: Project,
state: AppProcessRuntime,
input: { exitCode?: number; reason: string },
): void {
if (!state.sessionId) return;
try {
projectAppLogStore(project).endSession(state.sessionId, input);
} catch {
state.persistenceError = true;
}
}
function projectAppStatus(project: Project) {
const process = appProcesses.get(project.id), state = appProcessState.get(project.id) ?? { logs: [] };
const process = appProcesses.get(project.id);
let state = appProcessState.get(project.id);
if (!state) {
state = { logs: persistedAppLogs(project) };
appProcessState.set(project.id, state);
}
return { configured: Boolean(readAppConfig(project).command), running: Boolean(process && process.exitCode === null), ...(process?.pid ? { pid: process.pid } : {}), ...(state.startedAt ? { startedAt: state.startedAt } : {}), config: readAppConfig(project), logs: state.logs };
}
function projectEnvironment(...directories: string[]): NodeJS.ProcessEnv {
Expand Down Expand Up @@ -527,23 +605,77 @@ function startProjectApp(project: Project) {
if (!config.command) throw new Error("Configure a start command first");
const cwd = resolve(project.repo, config.workingDirectory);
if (!existsSync(cwd)) throw new Error("Configured working directory does not exist");
const state = { startedAt: new Date().toISOString(), logs: [] as string[] };
const child = spawn(config.command, { cwd, shell: true, windowsHide: true, env: { ...isolatedAgentEnvironment(), ...projectEnvironment(project.repo, cwd) } });
const append = (prefix: string, chunk: Buffer) => { state.logs.push(`${prefix}${chunk.toString("utf8")}`); state.logs = state.logs.slice(-200); };
child.stdout?.on("data", (chunk: Buffer) => append("", chunk));
child.stderr?.on("data", (chunk: Buffer) => append("[stderr] ", chunk));
child.once("error", (error) => state.logs.push(`[error] ${error.message}`));
child.once("exit", (code) => { state.logs.push(`[codePilot] App exited with code ${code ?? "unknown"}.`); appProcesses.delete(project.id); });
const startedAt = new Date().toISOString();
const journal = projectAppLogStore(project);
const sessionId = journal.startSession({
command: config.command,
workingDirectory: config.workingDirectory,
url: config.url,
startedAt,
});
const state: AppProcessRuntime = {
startedAt,
sessionId,
logs: journal.recent(500).map(formatAppLogEntry),
};
let child: ReturnType<typeof spawn>;
try {
child = spawn(config.command, { cwd, shell: true, windowsHide: true, env: { ...isolatedAgentEnvironment(), ...projectEnvironment(project.repo, cwd) } });
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
journal.append(sessionId, "system", `App could not start: ${message}`);
journal.endSession(sessionId, { reason: "spawn-error" });
throw error;
}
appendAppLog(
project,
state,
"system",
`App started${child.pid ? ` with PID ${child.pid}` : ""}.`,
);
child.stdout?.on("data", (chunk: Buffer) =>
appendAppLog(project, state, "stdout", chunk.toString("utf8")),
);
child.stderr?.on("data", (chunk: Buffer) =>
appendAppLog(project, state, "stderr", chunk.toString("utf8")),
);
child.once("error", (error) => {
if (appIsQuitting) return;
appendAppLog(project, state, "system", `App error: ${error.message}`);
endAppLogSession(project, state, { reason: "spawn-error" });
});
child.once("exit", (code) => {
if (!appIsQuitting && !state.stopping)
appendAppLog(
project,
state,
"system",
`App exited with code ${code ?? "unknown"}.`,
);
if (!appIsQuitting)
endAppLogSession(project, state, {
...(typeof code === "number" ? { exitCode: code } : {}),
reason: state.stopping ? "user-stop" : "process-exit",
});
if (appProcesses.get(project.id) === child)
appProcesses.delete(project.id);
});
appProcesses.set(project.id, child); appProcessState.set(project.id, state);
return projectAppStatus(project);
}
function stopProjectApp(project: Project) {
const child = appProcesses.get(project.id);
if (child?.pid) {
const state = appProcessState.get(project.id);
if (state) {
state.stopping = true;
appendAppLog(project, state, "system", "App stopped by user.");
endAppLogSession(project, state, { reason: "user-stop" });
}
if (process.platform === "win32") { try { execFileSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { windowsHide: true }); } catch { child.kill(); } }
else child.kill("SIGTERM");
appProcesses.delete(project.id);
appProcessState.get(project.id)?.logs.push("[codePilot] App stopped by user.");
if (appProcesses.get(project.id) === child)
appProcesses.delete(project.id);
}
return projectAppStatus(project);
}
Expand Down Expand Up @@ -1302,9 +1434,26 @@ app.on("window-all-closed", () => {
});
app.on("before-quit", () => {
pmServer.stop();
const projects = new Map(load().projects.map((project) => [project.id, project]));
for (const [projectId] of appProcesses) {
const project = projects.get(projectId);
const state = appProcessState.get(projectId);
if (!project || !state) continue;
state.stopping = true;
appendAppLog(
project,
state,
"system",
"App stopped because CodePilot is quitting.",
);
endAppLogSession(project, state, { reason: "codepilot-quit" });
}
appIsQuitting = true;
for (const child of appProcesses.values()) {
if (!child.pid) continue;
if (process.platform === "win32") { try { execFileSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { windowsHide: true }); } catch { child.kill(); } }
else child.kill("SIGTERM");
}
for (const store of appLogStores.values()) store.close();
appLogStores.clear();
});
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ GitHub authentication is owned by the official `gh` CLI. The desktop launches it

### Persistence

One SQLite database owns runs, tasks, attempts, bounded events, worker results, leases, and desktop project-manager records. Initialization is centralized and schema version is recorded with SQLite `user_version`. Interrupted running tasks are marked failed and their runs require review on desktop restart; automatic resume is not yet implemented.
One SQLite database owns runs, tasks, attempts, bounded worker events, worker results, leases, desktop project-manager records, and App Core runtime history. Each app launch creates a session with its command, working directory, URL, start/end times, exit code, and stop reason. Timestamped stdout, stderr, and system events are hydrated after desktop restarts. Runtime retention is bounded to the latest 100 sessions and 20,000 events per project, while the App Core pane renders the latest 500 events. Initialization is centralized and schema version is recorded with SQLite `user_version`. Interrupted running tasks are marked failed and their runs require review on desktop restart; automatic resume is not yet implemented.

## Current limitations

Expand Down
1 change: 1 addition & 0 deletions docs/threat-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ codePilot v0.1 is local developer tooling for repositories, objectives, provider
- Validation commands use a shell and can execute arbitrary operator-supplied text.
- Cooperative file leases do not prevent a provider from ignoring the protocol.
- SQLite state is local and is not tamper-evident.
- App Core stdout and stderr are retained locally in the project database. Applications should not print credentials or other secrets; retained logs must be inspected before sharing `.codepilot/` data.
- The experimental Docker files do not yet represent an enforced manager-owned sandbox.
- Explicit GitHub publication uses the current user's authenticated `gh` and Git credentials to push the generated branch and open a pull request. Repository permissions are enforced by GitHub, not codePilot.
- Explicit repository cloning runs Git with the current user's network access and credentials; the cloned repository remains untrusted until the operator reviews it.
Expand Down
Loading