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
86 changes: 86 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/agents/copilot-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,45 @@ describe("runCopilotCliAgent", () => {
expect(sdkArgs.model).toBeUndefined();
expect(result.meta?.agentMeta?.model).toBe("default");
});

it("passes infiniteSessions config enabled by default", async () => {
checkCopilotAvailableMock.mockReturnValue({ available: true });
runCopilotAgentMock.mockResolvedValueOnce({
text: "ok",
sessionId: "sid-inf",
});

await runCopilotCliAgent({
sessionId: "s1",
sessionFile: "/tmp/session.jsonl",
workspaceDir: "/tmp",
prompt: "hi",
timeoutMs: 5_000,
runId: "run-7",
});

const sdkArgs = runCopilotAgentMock.mock.calls[0]?.[0];
expect(sdkArgs.infiniteSessions).toBeDefined();
expect(sdkArgs.infiniteSessions.enabled).toBe(true);
});

it("includes workspacePath in result meta when returned by SDK", async () => {
checkCopilotAvailableMock.mockReturnValue({ available: true });
runCopilotAgentMock.mockResolvedValueOnce({
text: "ok",
sessionId: "sid-ws",
workspacePath: "/tmp/copilot-workspace/session-ws",
});

const result = await runCopilotCliAgent({
sessionId: "s1",
sessionFile: "/tmp/session.jsonl",
workspaceDir: "/tmp",
prompt: "hi",
timeoutMs: 5_000,
runId: "run-8",
});

expect(result.meta?.workspacePath).toBe("/tmp/copilot-workspace/session-ws");
});
});
15 changes: 15 additions & 0 deletions src/agents/copilot-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,27 @@ export async function runCopilotCliAgent(params: {
try {
log.info(`copilot-cli exec: model=${modelId} promptChars=${params.prompt.length}`);

// Enable infinite sessions by default for copilot-cli runs (they can be long-running).
// Thresholds are configurable via cli backend config overrides.
const infiniteSessionsConfig = backendConfig?.config as Record<string, unknown> | undefined;
const infiniteSessions = {
enabled: true,
...(typeof infiniteSessionsConfig?.backgroundCompactionThreshold === "number"
? { backgroundCompactionThreshold: infiniteSessionsConfig.backgroundCompactionThreshold }
: {}),
...(typeof infiniteSessionsConfig?.bufferExhaustionThreshold === "number"
? { bufferExhaustionThreshold: infiniteSessionsConfig.bufferExhaustionThreshold }
: {}),
};

const result = await runCopilotAgent({
prompt: params.prompt,
model: modelId === "default" ? undefined : modelId,
workspaceDir: resolvedWorkspace,
systemPrompt,
timeoutMs: params.timeoutMs,
sessionId: params.cliSessionId,
infiniteSessions,
});

const text = result.text?.trim();
Expand All @@ -135,6 +149,7 @@ export async function runCopilotCliAgent(params: {
provider: "copilot-cli",
model: modelId,
},
workspacePath: result.workspacePath,
},
};
} catch (err) {
Expand Down
57 changes: 57 additions & 0 deletions src/agents/copilot-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const mockSession = {
sendAndWait: vi.fn(),
destroy: vi.fn(),
sessionId: "mock-session-id",
workspacePath: undefined as string | undefined,
};
const mockClient = {
stop: vi.fn(),
Expand Down Expand Up @@ -199,6 +200,62 @@ describe("copilot-sdk", () => {
content: "You are a helpful assistant.",
});
});

it("passes infiniteSessions config to session", async () => {
mockSession.sendAndWait.mockResolvedValueOnce({
data: { content: "ok" },
});

const { runCopilotAgent } = await import("./copilot-sdk.js");
await runCopilotAgent({
prompt: "hi",
timeoutMs: 5_000,
infiniteSessions: {
enabled: true,
backgroundCompactionThreshold: 0.75,
bufferExhaustionThreshold: 0.9,
},
});

const sessionConfig = mockClient.createSession.mock.calls[0]?.[0];
expect(sessionConfig.infiniteSessions).toEqual({
enabled: true,
backgroundCompactionThreshold: 0.75,
bufferExhaustionThreshold: 0.9,
});
});

it("does not set infiniteSessions when not provided", async () => {
mockSession.sendAndWait.mockResolvedValueOnce({
data: { content: "ok" },
});

const { runCopilotAgent } = await import("./copilot-sdk.js");
await runCopilotAgent({
prompt: "hi",
timeoutMs: 5_000,
});

const sessionConfig = mockClient.createSession.mock.calls[0]?.[0];
expect(sessionConfig.infiniteSessions).toBeUndefined();
});

it("returns workspacePath when available", async () => {
mockSession.workspacePath = "/tmp/copilot-workspace/session-123";
mockSession.sendAndWait.mockResolvedValueOnce({
data: { content: "ok" },
});

const { runCopilotAgent } = await import("./copilot-sdk.js");
const result = await runCopilotAgent({
prompt: "hi",
timeoutMs: 5_000,
infiniteSessions: { enabled: true },
});

expect(result.workspacePath).toBe("/tmp/copilot-workspace/session-123");
mockSession.workspacePath = undefined;
});
});

describe("listCopilotModels", () => {
Expand Down
18 changes: 17 additions & 1 deletion src/agents/copilot-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
CopilotClient,
CopilotClientOptions,
CopilotSession,
InfiniteSessionConfig,
ModelInfo,
SessionConfig,
} from "@github/copilot-sdk";
Expand Down Expand Up @@ -116,11 +117,17 @@ export type CopilotAgentRunOptions = {
systemPrompt?: string;
timeoutMs?: number;
sessionId?: string;
infiniteSessions?: {
enabled?: boolean;
backgroundCompactionThreshold?: number;
bufferExhaustionThreshold?: number;
};
};

export type CopilotAgentRunResult = {
text: string;
sessionId: string;
workspacePath?: string;
};

/**
Expand Down Expand Up @@ -151,6 +158,10 @@ export async function runCopilotAgent(
}),
};

if (options.infiniteSessions) {
sessionConfig.infiniteSessions = options.infiniteSessions as InfiniteSessionConfig;
}

if (options.systemPrompt) {
sessionConfig.systemMessage = {
mode: "append",
Expand All @@ -169,14 +180,19 @@ export async function runCopilotAgent(

const text = response?.data?.content ?? "";
const sessionId = session.sessionId;
const workspacePath = session.workspacePath;

if (workspacePath) {
log.info("infinite session workspace", { workspacePath, sessionId });
}

log.info(`copilot agent run completed`, {
sessionId,
model: options.model,
responseLength: text.length,
});

return { text, sessionId };
return { text, sessionId, workspacePath };
} finally {
if (session) {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/agents/pi-embedded-runner/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export type EmbeddedPiRunMeta = {
name: string;
arguments: string;
}>;
/** Workspace path for Copilot SDK infinite session state persistence. */
workspacePath?: string;
};

export type EmbeddedPiRunResult = {
Expand Down
Loading