Skip to content
Open
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
653 changes: 649 additions & 4 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@
],
"license": "MIT",
"dependencies": {
"@fastify/cors": "^11.2.0",
"commander": "^14.0.0",
"cron-parser": "^5.5.0",
"execa": "^9.5.2",
"fastify": "^5.7.4",
"nanoid": "^5.1.5",
"proper-lockfile": "^4.1.2",
"write-file-atomic": "^7.0.0",
"yaml": "^2.7.1"
},
"devDependencies": {
"@types/node": "^22.13.4",
"@types/node": "^22.19.13",
"@types/proper-lockfile": "^4.1.4",
"tsup": "^8.4.0",
"tsx": "^4.19.3",
Expand Down
85 changes: 23 additions & 62 deletions src/commands/restart.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import fs from 'node:fs/promises';
import { requireManifest, updateManifest, findAgent } from '../core/manifest.js';
import { requireManifest, findAgent } from '../core/manifest.js';
import { loadConfig, resolveAgentConfig } from '../core/config.js';
import { spawnAgent, killAgent } from '../core/agent.js';
import { restartAgent } from '../core/agent.js';
import { getRepoRoot } from '../core/worktree.js';
import * as tmux from '../core/tmux.js';
import { openTerminalWindow } from '../core/terminal.js';
import { agentId as genAgentId, sessionId as genSessionId } from '../lib/id.js';
import { agentPromptFile } from '../lib/paths.js';
import { PpgError, AgentNotFoundError } from '../lib/errors.js';
import { output, success, info } from '../lib/output.js';
import { renderTemplate, type TemplateContext } from '../core/template.js';

export interface RestartOptions {
prompt?: string;
Expand All @@ -29,12 +26,6 @@ export async function restartCommand(agentRef: string, options: RestartOptions):

const { worktree: wt, agent: oldAgent } = found;

// Kill old agent if still running
if (oldAgent.status === 'running') {
info(`Killing existing agent ${oldAgent.id}`);
await killAgent(oldAgent);
}

// Read original prompt from prompt file, or use override
let promptText: string;
if (options.prompt) {
Expand All @@ -51,73 +42,43 @@ export async function restartCommand(agentRef: string, options: RestartOptions):
}
}

// Resolve agent config
const agentConfig = resolveAgentConfig(config, options.agent ?? oldAgent.agentType);

// Ensure tmux session
await tmux.ensureSession(manifest.sessionName);

// Create new tmux window in same worktree
const newAgentId = genAgentId();
const windowTarget = await tmux.createWindow(manifest.sessionName, `${wt.name}-restart`, wt.path);

// Render template vars
const ctx: TemplateContext = {
WORKTREE_PATH: wt.path,
BRANCH: wt.branch,
AGENT_ID: newAgentId,
PROJECT_ROOT: projectRoot,
TASK_NAME: wt.name,
PROMPT: promptText,
};
const renderedPrompt = renderTemplate(promptText, ctx);
if (oldAgent.status === 'running') {
info(`Killing existing agent ${oldAgent.id}`);
}

const newSessionId = genSessionId();
const agentEntry = await spawnAgent({
agentId: newAgentId,
agentConfig,
prompt: renderedPrompt,
worktreePath: wt.path,
tmuxTarget: windowTarget,
const result = await restartAgent({
projectRoot,
branch: wt.branch,
sessionId: newSessionId,
});

// Update manifest: mark old agent as gone, add new agent
await updateManifest(projectRoot, (m) => {
const mWt = m.worktrees[wt.id];
if (mWt) {
const mOldAgent = mWt.agents[oldAgent.id];
if (mOldAgent && mOldAgent.status === 'running') {
mOldAgent.status = 'gone';
}
mWt.agents[newAgentId] = agentEntry;
}
return m;
agentId: oldAgent.id,
worktree: wt,
oldAgent,
sessionName: manifest.sessionName,
agentConfig,
promptText,
});

// Only open Terminal window when explicitly requested via --open (fire-and-forget)
if (options.open === true) {
openTerminalWindow(manifest.sessionName, windowTarget, `${wt.name}-restart`).catch(() => {});
openTerminalWindow(manifest.sessionName, result.tmuxTarget, `${wt.name}-restart`).catch(() => {});
}

if (options.json) {
output({
success: true,
oldAgentId: oldAgent.id,
oldAgentId: result.oldAgentId,
newAgent: {
id: newAgentId,
tmuxTarget: windowTarget,
sessionId: newSessionId,
worktreeId: wt.id,
worktreeName: wt.name,
branch: wt.branch,
path: wt.path,
id: result.newAgentId,
tmuxTarget: result.tmuxTarget,
sessionId: result.sessionId,
worktreeId: result.worktreeId,
worktreeName: result.worktreeName,
branch: result.branch,
path: result.path,
},
}, true);
} else {
success(`Restarted agent ${oldAgent.id} → ${newAgentId} in worktree ${wt.name}`);
info(` New agent ${newAgentId} → ${windowTarget}`);
success(`Restarted agent ${result.oldAgentId} → ${result.newAgentId} in worktree ${wt.name}`);
info(` New agent ${result.newAgentId} → ${result.tmuxTarget}`);
}
}
5 changes: 3 additions & 2 deletions src/commands/spawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { spawnAgent } from '../core/agent.js';
import { getRepoRoot } from '../core/worktree.js';
import { agentId, sessionId } from '../lib/id.js';
import * as tmux from '../core/tmux.js';
import type { Manifest } from '../types/manifest.js';

vi.mock('node:fs/promises', async () => {
const actual = await vi.importActual<typeof import('node:fs/promises')>('node:fs/promises');
Expand Down Expand Up @@ -79,7 +80,7 @@ const mockedEnsureSession = vi.mocked(tmux.ensureSession);
const mockedCreateWindow = vi.mocked(tmux.createWindow);
const mockedSplitPane = vi.mocked(tmux.splitPane);

function createManifest(tmuxWindow = '') {
function createManifest(tmuxWindow = ''): Manifest {
return {
version: 1 as const,
projectRoot: '/tmp/repo',
Expand All @@ -103,7 +104,7 @@ function createManifest(tmuxWindow = '') {
}

describe('spawnCommand', () => {
let manifestState = createManifest();
let manifestState: Manifest = createManifest();
let nextAgent = 1;
let nextSession = 1;

Expand Down
86 changes: 85 additions & 1 deletion src/core/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { agentPromptFile, agentPromptsDir } from '../lib/paths.js';
import { getPaneInfo, listSessionPanes, type PaneInfo } from './tmux.js';
import { updateManifest } from './manifest.js';
import { PpgError } from '../lib/errors.js';
import type { AgentEntry, AgentStatus } from '../types/manifest.js';
import { agentId as genAgentId, sessionId as genSessionId } from '../lib/id.js';
import { renderTemplate, type TemplateContext } from './template.js';
import type { AgentEntry, AgentStatus, WorktreeEntry } from '../types/manifest.js';
import type { AgentConfig } from '../types/config.js';
import * as tmux from './tmux.js';

Expand Down Expand Up @@ -242,6 +244,88 @@ export async function killAgents(agents: AgentEntry[]): Promise<void> {
}));
}

export interface RestartAgentOptions {
projectRoot: string;
agentId: string;
worktree: WorktreeEntry;
oldAgent: AgentEntry;
sessionName: string;
agentConfig: AgentConfig;
promptText: string;
}

export interface RestartAgentResult {
oldAgentId: string;
newAgentId: string;
tmuxTarget: string;
sessionId: string;
worktreeId: string;
worktreeName: string;
branch: string;
path: string;
}

/**
* Restart an agent: kill old, spawn new in a fresh tmux window, update manifest.
*/
export async function restartAgent(opts: RestartAgentOptions): Promise<RestartAgentResult> {
const { projectRoot, worktree: wt, oldAgent, sessionName, agentConfig, promptText } = opts;

// Kill old agent if still running
if (oldAgent.status === 'running') {
await killAgent(oldAgent);
}

await tmux.ensureSession(sessionName);
const newAgentId = genAgentId();
const windowTarget = await tmux.createWindow(sessionName, `${wt.name}-restart`, wt.path);

const ctx: TemplateContext = {
WORKTREE_PATH: wt.path,
BRANCH: wt.branch,
AGENT_ID: newAgentId,
PROJECT_ROOT: projectRoot,
TASK_NAME: wt.name,
PROMPT: promptText,
};
const renderedPrompt = renderTemplate(promptText, ctx);

const newSessionId = genSessionId();
const agentEntry = await spawnAgent({
agentId: newAgentId,
agentConfig,
prompt: renderedPrompt,
worktreePath: wt.path,
tmuxTarget: windowTarget,
projectRoot,
branch: wt.branch,
sessionId: newSessionId,
});

await updateManifest(projectRoot, (m) => {
const mWt = m.worktrees[wt.id];
if (mWt) {
const mOldAgent = mWt.agents[oldAgent.id];
if (mOldAgent && mOldAgent.status === 'running') {
mOldAgent.status = 'gone';
}
mWt.agents[newAgentId] = agentEntry;
}
return m;
});

return {
oldAgentId: oldAgent.id,
newAgentId,
tmuxTarget: windowTarget,
sessionId: newSessionId,
worktreeId: wt.id,
worktreeName: wt.name,
branch: wt.branch,
path: wt.path,
};
}

async function fileExists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
Expand Down
8 changes: 8 additions & 0 deletions src/lib/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ export function worktreeBaseDir(projectRoot: string): string {
export function worktreePath(projectRoot: string, id: string): string {
return path.join(worktreeBaseDir(projectRoot), id);
}

export function serveStatePath(projectRoot: string): string {
return path.join(ppgDir(projectRoot), 'serve.state.json');
}

export function servePidPath(projectRoot: string): string {
return path.join(ppgDir(projectRoot), 'serve.pid');
}
Loading