feat: add repository_dispatch event support

- Add new progress MCP server for reporting task status via API
- Support repository_dispatch events with task description and progress endpoint
- Introduce isDispatch flag to unify dispatch event handling
- Make GitHub data optional for dispatch events without issues/PRs
- Update prompt generation with dispatch-specific instructions

Enables triggering Claude via repository_dispatch with:
{
  "event_type": "claude_task",
  "client_payload": {
    "description": "Task description",
    "progress_endpoint": "https://api.example.com/progress"
  }
}
This commit is contained in:
Ashwin Bhat
2025-06-17 14:54:03 -07:00
parent b39377f9bc
commit 52c2f5881b
22 changed files with 1762 additions and 58 deletions

2
.npmrc
View File

@@ -1,2 +0,0 @@
engine-strict=true
registry=https://registry.npmjs.org/

View File

@@ -162,6 +162,10 @@ runs:
ADDITIONAL_PERMISSIONS: ${{ inputs.additional_permissions }}
USE_COMMIT_SIGNING: ${{ inputs.use_commit_signing }}
# Authentication for remote-agent mode
ANTHROPIC_API_KEY: ${{ inputs.anthropic_api_key }}
CLAUDE_CODE_OAUTH_TOKEN: ${{ inputs.claude_code_oauth_token }}
- name: Install Base Action Dependencies
if: steps.prepare.outputs.contains_trigger == 'true'
shell: bash
@@ -172,7 +176,7 @@ runs:
echo "Base-action dependencies installed"
cd -
# Install Claude Code globally
bun install -g @anthropic-ai/claude-code@1.0.67
bun install -g @anthropic-ai/claude-code
- name: Setup Network Restrictions
if: steps.prepare.outputs.contains_trigger == 'true' && inputs.experimental_allowed_domains != ''
@@ -188,7 +192,6 @@ runs:
if: steps.prepare.outputs.contains_trigger == 'true'
shell: bash
run: |
# Run the base-action
bun run ${GITHUB_ACTION_PATH}/base-action/src/index.ts
env:
@@ -206,16 +209,17 @@ runs:
INPUT_CLAUDE_ENV: ${{ inputs.claude_env }}
INPUT_FALLBACK_MODEL: ${{ inputs.fallback_model }}
INPUT_EXPERIMENTAL_SLASH_COMMANDS_DIR: ${{ github.action_path }}/slash-commands
INPUT_STREAM_CONFIG: ${{ steps.prepare.outputs.stream_config }}
# Model configuration
ANTHROPIC_MODEL: ${{ inputs.model || inputs.anthropic_model }}
ANTHROPIC_MODEL: ${{ steps.prepare.outputs.anthropic_model || inputs.model || inputs.anthropic_model }}
GITHUB_TOKEN: ${{ steps.prepare.outputs.GITHUB_TOKEN }}
NODE_VERSION: ${{ env.NODE_VERSION }}
DETAILED_PERMISSION_MESSAGES: "1"
# Provider configuration
ANTHROPIC_API_KEY: ${{ inputs.anthropic_api_key }}
CLAUDE_CODE_OAUTH_TOKEN: ${{ inputs.claude_code_oauth_token }}
CLAUDE_CODE_OAUTH_TOKEN: ${{ steps.prepare.outputs.claude_code_oauth_token || inputs.claude_code_oauth_token }}
ANTHROPIC_BASE_URL: ${{ env.ANTHROPIC_BASE_URL }}
CLAUDE_CODE_USE_BEDROCK: ${{ inputs.use_bedrock == 'true' && '1' || '' }}
CLAUDE_CODE_USE_VERTEX: ${{ inputs.use_vertex == 'true' && '1' || '' }}
@@ -238,6 +242,17 @@ runs:
VERTEX_REGION_CLAUDE_3_5_SONNET: ${{ env.VERTEX_REGION_CLAUDE_3_5_SONNET }}
VERTEX_REGION_CLAUDE_3_7_SONNET: ${{ env.VERTEX_REGION_CLAUDE_3_7_SONNET }}
- name: Report Claude completion
if: steps.prepare.outputs.contains_trigger == 'true' && always()
shell: bash
run: |
bun run ${GITHUB_ACTION_PATH}/src/entrypoints/report-claude-complete.ts
env:
MODE: ${{ inputs.mode }}
STREAM_CONFIG: ${{ steps.prepare.outputs.stream_config }}
CLAUDE_CONCLUSION: ${{ steps.claude-code.outputs.conclusion }}
CLAUDE_START_TIME: ${{ steps.prepare.outputs.claude_start_time }}
- name: Update comment with job link
if: steps.prepare.outputs.contains_trigger == 'true' && steps.prepare.outputs.claude_comment_id && always()
shell: bash

View File

@@ -102,7 +102,7 @@ runs:
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # https://github.com/actions/setup-node/releases/tag/v4.4.0
with:
node-version: ${{ env.NODE_VERSION || '18.x' }}
node-version: ${{ env.NODE_VERSION || '22.x' }}
cache: ${{ inputs.use_node_cache == 'true' && 'npm' || '' }}
- name: Install Bun
@@ -118,7 +118,9 @@ runs:
- name: Install Claude Code
shell: bash
run: bun install -g @anthropic-ai/claude-code@1.0.67
run: |
# Install Claude Code
bun install -g @anthropic-ai/claude-code
- name: Run Claude Code Action
shell: bash

View File

@@ -30,7 +30,8 @@ async function run() {
appendSystemPrompt: process.env.INPUT_APPEND_SYSTEM_PROMPT,
claudeEnv: process.env.INPUT_CLAUDE_ENV,
fallbackModel: process.env.INPUT_FALLBACK_MODEL,
model: process.env.ANTHROPIC_MODEL,
resumeEndpoint: process.env.INPUT_RESUME_ENDPOINT,
streamConfig: process.env.INPUT_STREAM_CONFIG,
});
} catch (error) {
core.setFailed(`Action failed with error: ${error}`);

View File

@@ -4,6 +4,7 @@ import { promisify } from "util";
import { unlink, writeFile, stat } from "fs/promises";
import { createWriteStream } from "fs";
import { spawn } from "child_process";
import { StreamHandler } from "./stream-handler";
const execAsync = promisify(exec);
@@ -21,7 +22,15 @@ export type ClaudeOptions = {
claudeEnv?: string;
fallbackModel?: string;
timeoutMinutes?: string;
model?: string;
resumeEndpoint?: string;
streamConfig?: string;
};
export type StreamConfig = {
progress_endpoint?: string;
headers?: Record<string, string>;
resume_endpoint?: string;
session_id?: string;
};
type PreparedConfig = {
@@ -95,9 +104,6 @@ export function prepareRunConfig(
if (options.fallbackModel) {
claudeArgs.push("--fallback-model", options.fallbackModel);
}
if (options.model) {
claudeArgs.push("--model", options.model);
}
if (options.timeoutMinutes) {
const timeoutMinutesNum = parseInt(options.timeoutMinutes, 10);
if (isNaN(timeoutMinutesNum) || timeoutMinutesNum <= 0) {
@@ -106,6 +112,25 @@ export function prepareRunConfig(
);
}
}
if (options.resumeEndpoint) {
claudeArgs.push("--teleport", options.resumeEndpoint);
}
// Parse stream config for session_id and resume_endpoint
if (options.streamConfig) {
try {
const streamConfig: StreamConfig = JSON.parse(options.streamConfig);
// Add --session-id if session_id is provided
if (streamConfig.session_id) {
claudeArgs.push("--session-id", streamConfig.session_id);
}
// Only add --teleport if we have both session_id AND resume_endpoint
if (streamConfig.session_id && streamConfig.resume_endpoint) {
claudeArgs.push("--teleport", streamConfig.session_id);
}
} catch (e) {
console.error("Failed to parse stream_config JSON:", e);
}
}
// Parse custom environment variables
const customEnv = parseCustomEnvVars(options.claudeEnv);
@@ -120,6 +145,34 @@ export function prepareRunConfig(
export async function runClaude(promptPath: string, options: ClaudeOptions) {
const config = prepareRunConfig(promptPath, options);
// Set up streaming if endpoint is provided in stream config
let streamHandler: StreamHandler | null = null;
let streamConfig: StreamConfig | null = null;
if (options.streamConfig) {
try {
streamConfig = JSON.parse(options.streamConfig);
if (streamConfig?.progress_endpoint) {
const customHeaders = streamConfig.headers || {};
console.log("parsed headers", customHeaders);
Object.keys(customHeaders).forEach((key) => {
console.log(`Custom header: ${key} = ${customHeaders[key]}`);
});
streamHandler = new StreamHandler(
streamConfig.progress_endpoint,
customHeaders,
);
console.log(`Streaming output to: ${streamConfig.progress_endpoint}`);
if (Object.keys(customHeaders).length > 0) {
console.log(
`Custom streaming headers: ${Object.keys(customHeaders).join(", ")}`,
);
}
}
} catch (e) {
console.error("Failed to parse stream_config JSON:", e);
}
}
// Create a named pipe
try {
await unlink(PIPE_PATH);
@@ -162,12 +215,31 @@ export async function runClaude(promptPath: string, options: ClaudeOptions) {
pipeStream.destroy();
});
// Prepare environment variables
const processEnv = {
...process.env,
...config.env,
};
// If both session_id and resume_endpoint are provided, set environment variables
if (streamConfig?.session_id && streamConfig?.resume_endpoint) {
processEnv.TELEPORT_RESUME_URL = streamConfig.resume_endpoint;
console.log(
`Setting TELEPORT_RESUME_URL to: ${streamConfig.resume_endpoint}`,
);
if (streamConfig.headers && Object.keys(streamConfig.headers).length > 0) {
processEnv.TELEPORT_HEADERS = JSON.stringify(streamConfig.headers);
console.log(`Setting TELEPORT_HEADERS for resume endpoint`);
}
}
// Log the full Claude command being executed
console.log(`Running Claude with args: ${config.claudeArgs.join(" ")}`);
const claudeProcess = spawn("claude", config.claudeArgs, {
stdio: ["pipe", "pipe", "inherit"],
env: {
...process.env,
...config.env,
},
env: processEnv,
});
// Handle Claude process errors
@@ -178,32 +250,51 @@ export async function runClaude(promptPath: string, options: ClaudeOptions) {
// Capture output for parsing execution metrics
let output = "";
claudeProcess.stdout.on("data", (data) => {
let lineBuffer = ""; // Buffer for incomplete lines
claudeProcess.stdout.on("data", async (data) => {
const text = data.toString();
output += text;
// Try to parse as JSON and pretty print if it's on a single line
const lines = text.split("\n");
lines.forEach((line: string, index: number) => {
if (line.trim() === "") return;
// Add new data to line buffer
lineBuffer += text;
// Split into lines - the last element might be incomplete
const lines = lineBuffer.split("\n");
// The last element is either empty (if text ended with \n) or incomplete
lineBuffer = lines.pop() || "";
// Process complete lines
for (let index = 0; index < lines.length; index++) {
const line = lines[index];
if (!line || line.trim() === "") continue;
// Try to parse as JSON and pretty print if it's on a single line
try {
// Check if this line is a JSON object
const parsed = JSON.parse(line);
const prettyJson = JSON.stringify(parsed, null, 2);
process.stdout.write(prettyJson);
if (index < lines.length - 1 || text.endsWith("\n")) {
process.stdout.write("\n");
process.stdout.write("\n");
// Send valid JSON to stream handler if available
if (streamHandler) {
try {
// Send the original line (which is valid JSON) with newline for proper splitting
const dataToSend = line + "\n";
await streamHandler.addOutput(dataToSend);
} catch (error) {
core.warning(`Failed to stream output: ${error}`);
}
}
} catch (e) {
// Not a JSON object, print as is
process.stdout.write(line);
if (index < lines.length - 1 || text.endsWith("\n")) {
process.stdout.write("\n");
}
process.stdout.write("\n");
// Don't send non-JSON lines to stream handler
}
});
output += text;
}
});
// Handle stdout errors
@@ -257,8 +348,33 @@ export async function runClaude(promptPath: string, options: ClaudeOptions) {
}
}, timeoutMs);
claudeProcess.on("close", (code) => {
claudeProcess.on("close", async (code) => {
if (!resolved) {
// Process any remaining data in the line buffer
if (lineBuffer.trim()) {
// Try to parse and print the remaining line
try {
const parsed = JSON.parse(lineBuffer);
const prettyJson = JSON.stringify(parsed, null, 2);
process.stdout.write(prettyJson);
process.stdout.write("\n");
// Send valid JSON to stream handler if available
if (streamHandler) {
try {
const dataToSend = lineBuffer + "\n";
await streamHandler.addOutput(dataToSend);
} catch (error) {
core.warning(`Failed to stream final output: ${error}`);
}
}
} catch (e) {
process.stdout.write(lineBuffer);
process.stdout.write("\n");
// Don't send non-JSON lines to stream handler
}
}
clearTimeout(timeoutId);
resolved = true;
resolve(code || 0);
@@ -275,6 +391,15 @@ export async function runClaude(promptPath: string, options: ClaudeOptions) {
});
});
// Clean up streaming
if (streamHandler) {
try {
await streamHandler.close();
} catch (error) {
core.warning(`Failed to close stream handler: ${error}`);
}
}
// Clean up processes
try {
catProcess.kill("SIGTERM");

View File

@@ -0,0 +1,152 @@
import * as core from "@actions/core";
export function parseStreamHeaders(
headersInput?: string,
): Record<string, string> {
if (!headersInput || headersInput.trim() === "") {
return {};
}
try {
return JSON.parse(headersInput);
} catch (e) {
console.error("Failed to parse stream headers as JSON:", e);
return {};
}
}
export type TokenGetter = (audience: string) => Promise<string>;
export class StreamHandler {
private endpoint: string;
private customHeaders: Record<string, string>;
private tokenGetter: TokenGetter;
private token: string | null = null;
private tokenFetchTime: number = 0;
private buffer: string[] = [];
private flushTimer: NodeJS.Timeout | null = null;
private isClosed = false;
private readonly TOKEN_LIFETIME_MS = 4 * 60 * 1000; // 4 minutes
private readonly BATCH_SIZE = 10;
private readonly BATCH_TIMEOUT_MS = 1000;
private readonly REQUEST_TIMEOUT_MS = 5000;
constructor(
endpoint: string,
customHeaders: Record<string, string> = {},
tokenGetter?: TokenGetter,
) {
this.endpoint = endpoint;
this.customHeaders = customHeaders;
this.tokenGetter = tokenGetter || ((audience) => core.getIDToken(audience));
}
async addOutput(data: string): Promise<void> {
if (this.isClosed) return;
// Split by newlines and add to buffer
const lines = data.split("\n").filter((line) => line.length > 0);
this.buffer.push(...lines);
// Check if we should flush
if (this.buffer.length >= this.BATCH_SIZE) {
await this.flush();
} else {
// Set or reset the timer
this.resetFlushTimer();
}
}
private resetFlushTimer(): void {
if (this.flushTimer) {
clearTimeout(this.flushTimer);
}
this.flushTimer = setTimeout(() => {
this.flush().catch((err) => {
core.warning(`Failed to flush stream buffer: ${err}`);
});
}, this.BATCH_TIMEOUT_MS);
}
private async getToken(): Promise<string> {
const now = Date.now();
// Check if we need a new token
if (!this.token || now - this.tokenFetchTime >= this.TOKEN_LIFETIME_MS) {
try {
this.token = await this.tokenGetter("claude-code-github-action");
this.tokenFetchTime = now;
core.debug("Fetched new OIDC token for streaming");
} catch (error) {
throw new Error(`Failed to get OIDC token: ${error}`);
}
}
return this.token;
}
private async flush(): Promise<void> {
if (this.buffer.length === 0) return;
// Clear the flush timer
if (this.flushTimer) {
clearTimeout(this.flushTimer);
this.flushTimer = null;
}
// Get the current buffer and clear it
const output = [...this.buffer];
this.buffer = [];
try {
const token = await this.getToken();
const payload = {
timestamp: new Date().toISOString(),
output: output,
};
// Create an AbortController for timeout
const controller = new AbortController();
const timeoutId = setTimeout(
() => controller.abort(),
this.REQUEST_TIMEOUT_MS,
);
try {
await fetch(this.endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
...this.customHeaders,
},
body: JSON.stringify(payload),
signal: controller.signal,
});
} finally {
clearTimeout(timeoutId);
}
} catch (error) {
// Log but don't throw - we don't want to interrupt Claude's execution
core.warning(`Failed to stream output: ${error}`);
}
}
async close(): Promise<void> {
// Clear any pending timer
if (this.flushTimer) {
clearTimeout(this.flushTimer);
this.flushTimer = null;
}
// Flush any remaining output
if (this.buffer.length > 0) {
await this.flush();
}
// Mark as closed after flushing
this.isClosed = true;
}
}

View File

@@ -0,0 +1,97 @@
import { describe, it, expect } from "bun:test";
import { prepareRunConfig } from "../src/run-claude";
describe("resume endpoint functionality", () => {
it("should add --teleport flag when both session_id and resume_endpoint are provided", () => {
const streamConfig = JSON.stringify({
session_id: "12345",
resume_endpoint: "https://example.com/resume/12345",
});
const config = prepareRunConfig("/path/to/prompt", {
streamConfig,
});
expect(config.claudeArgs).toContain("--teleport");
expect(config.claudeArgs).toContain("12345");
});
it("should not add --teleport flag when no streamConfig is provided", () => {
const config = prepareRunConfig("/path/to/prompt", {
allowedTools: "Edit",
});
expect(config.claudeArgs).not.toContain("--teleport");
});
it("should not add --teleport flag when only session_id is provided without resume_endpoint", () => {
const streamConfig = JSON.stringify({
session_id: "12345",
// No resume_endpoint
});
const config = prepareRunConfig("/path/to/prompt", {
streamConfig,
});
expect(config.claudeArgs).not.toContain("--teleport");
});
it("should not add --teleport flag when only resume_endpoint is provided without session_id", () => {
const streamConfig = JSON.stringify({
resume_endpoint: "https://example.com/resume/12345",
// No session_id
});
const config = prepareRunConfig("/path/to/prompt", {
streamConfig,
});
expect(config.claudeArgs).not.toContain("--teleport");
});
it("should maintain order of arguments with session_id", () => {
const streamConfig = JSON.stringify({
session_id: "12345",
resume_endpoint: "https://example.com/resume/12345",
});
const config = prepareRunConfig("/path/to/prompt", {
allowedTools: "Edit",
streamConfig,
maxTurns: "5",
});
const teleportIndex = config.claudeArgs.indexOf("--teleport");
const maxTurnsIndex = config.claudeArgs.indexOf("--max-turns");
expect(teleportIndex).toBeGreaterThan(-1);
expect(maxTurnsIndex).toBeGreaterThan(-1);
});
it("should handle progress_endpoint and headers in streamConfig", () => {
const streamConfig = JSON.stringify({
progress_endpoint: "https://example.com/progress",
headers: { "X-Test": "value" },
});
const config = prepareRunConfig("/path/to/prompt", {
streamConfig,
});
// This test just verifies parsing doesn't fail - actual streaming logic
// is tested elsewhere as it requires environment setup
expect(config.claudeArgs).toBeDefined();
});
it("should handle session_id with resume_endpoint and headers", () => {
const streamConfig = JSON.stringify({
session_id: "abc123",
resume_endpoint: "https://example.com/resume/abc123",
headers: { Authorization: "Bearer token" },
progress_endpoint: "https://example.com/progress",
});
const config = prepareRunConfig("/path/to/prompt", {
streamConfig,
});
expect(config.claudeArgs).toContain("--teleport");
expect(config.claudeArgs).toContain("abc123");
// Note: Environment variable setup (TELEPORT_RESUME_URL, TELEPORT_HEADERS) is tested in integration tests
});
});

View File

@@ -0,0 +1,364 @@
import { describe, it, expect, beforeEach, mock } from "bun:test";
import {
StreamHandler,
parseStreamHeaders,
type TokenGetter,
} from "../src/stream-handler";
describe("parseStreamHeaders", () => {
it("should return empty object for empty input", () => {
expect(parseStreamHeaders("")).toEqual({});
expect(parseStreamHeaders(undefined)).toEqual({});
expect(parseStreamHeaders(" ")).toEqual({});
});
it("should parse single header", () => {
const result = parseStreamHeaders('{"X-Correlation-Id": "12345"}');
expect(result).toEqual({ "X-Correlation-Id": "12345" });
});
it("should parse multiple headers", () => {
const headers = JSON.stringify({
"X-Correlation-Id": "12345",
"X-Custom-Header": "custom-value",
Authorization: "Bearer token123",
});
const result = parseStreamHeaders(headers);
expect(result).toEqual({
"X-Correlation-Id": "12345",
"X-Custom-Header": "custom-value",
Authorization: "Bearer token123",
});
});
it("should handle headers with spaces", () => {
const headers = JSON.stringify({
"X-Header-One": "value with spaces",
"X-Header-Two": "another value",
});
const result = parseStreamHeaders(headers);
expect(result).toEqual({
"X-Header-One": "value with spaces",
"X-Header-Two": "another value",
});
});
it("should skip empty lines and comments", () => {
const headers = JSON.stringify({
"X-Header-One": "value1",
"X-Header-Two": "value2",
"X-Header-Three": "value3",
});
const result = parseStreamHeaders(headers);
expect(result).toEqual({
"X-Header-One": "value1",
"X-Header-Two": "value2",
"X-Header-Three": "value3",
});
});
it("should skip lines without colons", () => {
const headers = JSON.stringify({
"X-Header-One": "value1",
"X-Header-Two": "value2",
});
const result = parseStreamHeaders(headers);
expect(result).toEqual({
"X-Header-One": "value1",
"X-Header-Two": "value2",
});
});
it("should handle headers with colons in values", () => {
const headers = JSON.stringify({
"X-URL": "https://example.com:8080/path",
"X-Time": "10:30:45",
});
const result = parseStreamHeaders(headers);
expect(result).toEqual({
"X-URL": "https://example.com:8080/path",
"X-Time": "10:30:45",
});
});
});
describe("StreamHandler", () => {
let handler: StreamHandler;
let mockFetch: ReturnType<typeof mock>;
let mockTokenGetter: TokenGetter;
const mockEndpoint = "https://test.example.com/stream";
const mockToken = "mock-oidc-token";
beforeEach(() => {
// Mock fetch
mockFetch = mock(() => Promise.resolve({ ok: true }));
global.fetch = mockFetch as any;
// Mock token getter
mockTokenGetter = mock(() => Promise.resolve(mockToken));
});
describe("basic functionality", () => {
it("should batch lines up to BATCH_SIZE", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
// Add 9 lines (less than batch size of 10)
for (let i = 1; i <= 9; i++) {
await handler.addOutput(`line ${i}\n`);
}
// Should not have sent anything yet
expect(mockFetch).not.toHaveBeenCalled();
// Add the 10th line to trigger flush
await handler.addOutput("line 10\n");
// Should have sent the batch
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(mockEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${mockToken}`,
},
body: expect.stringContaining(
'"output":["line 1","line 2","line 3","line 4","line 5","line 6","line 7","line 8","line 9","line 10"]',
),
signal: expect.any(AbortSignal),
});
});
it("should flush on timeout", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
// Add a few lines
await handler.addOutput("line 1\n");
await handler.addOutput("line 2\n");
// Should not have sent anything yet
expect(mockFetch).not.toHaveBeenCalled();
// Wait for the timeout to trigger
await new Promise((resolve) => setTimeout(resolve, 1100));
// Should have sent the batch
expect(mockFetch).toHaveBeenCalledTimes(1);
const call = mockFetch.mock.calls[0];
expect(call).toBeDefined();
const body = JSON.parse(call![1].body);
expect(body.output).toEqual(["line 1", "line 2"]);
});
it("should include custom headers", async () => {
const customHeaders = {
"X-Correlation-Id": "12345",
"X-Custom": "value",
};
handler = new StreamHandler(mockEndpoint, customHeaders, mockTokenGetter);
// Trigger a batch
for (let i = 1; i <= 10; i++) {
await handler.addOutput(`line ${i}\n`);
}
expect(mockFetch).toHaveBeenCalledWith(mockEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${mockToken}`,
"X-Correlation-Id": "12345",
"X-Custom": "value",
},
body: expect.any(String),
signal: expect.any(AbortSignal),
});
});
it("should include timestamp in payload", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
const beforeTime = new Date().toISOString();
// Trigger a batch
for (let i = 1; i <= 10; i++) {
await handler.addOutput(`line ${i}\n`);
}
const afterTime = new Date().toISOString();
const call = mockFetch.mock.calls[0];
expect(call).toBeDefined();
const body = JSON.parse(call![1].body);
expect(body).toHaveProperty("timestamp");
expect(new Date(body.timestamp).toISOString()).toBe(body.timestamp);
expect(body.timestamp >= beforeTime).toBe(true);
expect(body.timestamp <= afterTime).toBe(true);
});
});
describe("token management", () => {
it("should fetch token on first request", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
// Trigger a flush
for (let i = 1; i <= 10; i++) {
await handler.addOutput(`line ${i}\n`);
}
expect(mockTokenGetter).toHaveBeenCalledWith("claude-code-github-action");
expect(mockTokenGetter).toHaveBeenCalledTimes(1);
});
it("should reuse token within 4 minutes", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
// First batch
for (let i = 1; i <= 10; i++) {
await handler.addOutput(`line ${i}\n`);
}
// Second batch immediately (within 4 minutes)
for (let i = 11; i <= 20; i++) {
await handler.addOutput(`line ${i}\n`);
}
// Should have only fetched token once
expect(mockTokenGetter).toHaveBeenCalledTimes(1);
});
it("should handle token fetch errors", async () => {
const errorTokenGetter = mock(() =>
Promise.reject(new Error("Token fetch failed")),
);
handler = new StreamHandler(mockEndpoint, {}, errorTokenGetter);
// Try to send data
for (let i = 1; i <= 10; i++) {
await handler.addOutput(`line ${i}\n`);
}
// Should not have made fetch request
expect(mockFetch).not.toHaveBeenCalled();
});
});
describe("error handling", () => {
it("should handle fetch errors gracefully", async () => {
mockFetch.mockImplementation(() =>
Promise.reject(new Error("Network error")),
);
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
// Send data - should not throw
for (let i = 1; i <= 10; i++) {
await handler.addOutput(`line ${i}\n`);
}
// Should have attempted to fetch
expect(mockFetch).toHaveBeenCalledTimes(1);
});
it("should continue processing after errors", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
// First batch - make it fail
let callCount = 0;
mockFetch.mockImplementation(() => {
callCount++;
if (callCount === 1) {
return Promise.reject(new Error("First batch failed"));
}
return Promise.resolve({ ok: true });
});
for (let i = 1; i <= 10; i++) {
await handler.addOutput(`line ${i}\n`);
}
// Second batch - should work
for (let i = 11; i <= 20; i++) {
await handler.addOutput(`line ${i}\n`);
}
// Should have attempted both batches
expect(mockFetch).toHaveBeenCalledTimes(2);
});
});
describe("close functionality", () => {
it("should flush remaining data on close", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
// Add some data but not enough to trigger batch
await handler.addOutput("line 1\n");
await handler.addOutput("line 2\n");
expect(mockFetch).not.toHaveBeenCalled();
// Close should flush
await handler.close();
expect(mockFetch).toHaveBeenCalledTimes(1);
const call = mockFetch.mock.calls[0];
expect(call).toBeDefined();
const body = JSON.parse(call![1].body);
expect(body.output).toEqual(["line 1", "line 2"]);
});
it("should not accept new data after close", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
await handler.close();
// Try to add data after close
await handler.addOutput("should not be sent\n");
// Should not have sent anything
expect(mockFetch).not.toHaveBeenCalled();
});
});
describe("data handling", () => {
it("should filter out empty lines", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
await handler.addOutput("line 1\n\n\nline 2\n\n");
await handler.close();
const call = mockFetch.mock.calls[0];
expect(call).toBeDefined();
const body = JSON.parse(call![1].body);
expect(body.output).toEqual(["line 1", "line 2"]);
});
it("should handle data without newlines", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
await handler.addOutput("single line");
await handler.close();
const call = mockFetch.mock.calls[0];
expect(call).toBeDefined();
const body = JSON.parse(call![1].body);
expect(body.output).toEqual(["single line"]);
});
it("should handle multi-line input correctly", async () => {
handler = new StreamHandler(mockEndpoint, {}, mockTokenGetter);
await handler.addOutput("line 1\nline 2\nline 3");
await handler.close();
const call = mockFetch.mock.calls[0];
expect(call).toBeDefined();
const body = JSON.parse(call![1].body);
expect(body.output).toEqual(["line 1", "line 2", "line 3"]);
});
});
});

View File

@@ -12,7 +12,6 @@ import { createOctokit } from "../github/api/client";
import { parseGitHubContext, isEntityContext } from "../github/context";
import { getMode, isValidMode, DEFAULT_MODE } from "../modes/registry";
import type { ModeName } from "../modes/types";
import { prepare } from "../prepare";
async function run() {
try {
@@ -60,7 +59,19 @@ async function run() {
}
// Step 4: Get mode and check trigger conditions
const mode = getMode(validatedMode, context);
let mode;
// TEMPORARY HACK: Always use remote-agent mode for repository_dispatch events
// This ensures backward compatibility while we transition
if (context.eventName === "repository_dispatch") {
console.log(
"🔧 TEMPORARY HACK: Forcing remote-agent mode for repository_dispatch event",
);
mode = getMode("remote-agent", context);
} else {
mode = getMode(context.inputs.mode, context);
}
const containsTrigger = mode.shouldTrigger(context);
// Set output for action.yml to check
@@ -72,10 +83,9 @@ async function run() {
}
// Step 5: Use the new modular prepare function
const result = await prepare({
const result = await mode.prepare({
context,
octokit,
mode,
githubToken,
});

View File

@@ -0,0 +1,81 @@
#!/usr/bin/env bun
import * as core from "@actions/core";
import { reportClaudeComplete } from "../modes/remote-agent/system-progress-handler";
import type { SystemProgressConfig } from "../modes/remote-agent/progress-types";
import type { StreamConfig } from "../types/stream-config";
async function run() {
try {
// Only run if we're in remote-agent mode
const mode = process.env.MODE;
if (mode !== "remote-agent") {
console.log(
"Not in remote-agent mode, skipping Claude completion reporting",
);
return;
}
// Check if we have stream config with system progress endpoint
const streamConfigStr = process.env.STREAM_CONFIG;
if (!streamConfigStr) {
console.log(
"No stream config available, skipping Claude completion reporting",
);
return;
}
let streamConfig: StreamConfig;
try {
streamConfig = JSON.parse(streamConfigStr);
} catch (e) {
console.error("Failed to parse stream config:", e);
return;
}
if (!streamConfig.system_progress_endpoint) {
console.log(
"No system progress endpoint in stream config, skipping Claude completion reporting",
);
return;
}
// Extract the system progress config
const systemProgressConfig: SystemProgressConfig = {
endpoint: streamConfig.system_progress_endpoint,
headers: streamConfig.headers || {},
};
// Get the OIDC token from Authorization header
const authHeader = systemProgressConfig.headers?.["Authorization"];
if (!authHeader || !authHeader.startsWith("Bearer ")) {
console.error("No valid Authorization header in stream config");
return;
}
const oidcToken = authHeader.substring(7); // Remove "Bearer " prefix
// Get Claude execution status
const claudeConclusion = process.env.CLAUDE_CONCLUSION || "failure";
const exitCode = claudeConclusion === "success" ? 0 : 1;
// Calculate duration if possible
const startTime = process.env.CLAUDE_START_TIME;
let durationMs = 0;
if (startTime) {
durationMs = Date.now() - parseInt(startTime, 10);
}
// Report Claude completion
console.log(
`Reporting Claude completion: exitCode=${exitCode}, duration=${durationMs}ms`,
);
reportClaudeComplete(systemProgressConfig, oidcToken, exitCode, durationMs);
} catch (error) {
// Don't fail the action if reporting fails
core.warning(`Failed to report Claude completion: ${error}`);
}
}
if (import.meta.main) {
run();
}

View File

@@ -6,6 +6,7 @@ import type {
PullRequestEvent,
PullRequestReviewEvent,
PullRequestReviewCommentEvent,
RepositoryDispatchEvent,
} from "@octokit/webhooks-types";
// Custom types for GitHub Actions events that aren't webhooks
export type WorkflowDispatchEvent = {
@@ -46,7 +47,11 @@ const ENTITY_EVENT_NAMES = [
"pull_request_review_comment",
] as const;
const AUTOMATION_EVENT_NAMES = ["workflow_dispatch", "schedule"] as const;
const AUTOMATION_EVENT_NAMES = [
"workflow_dispatch",
"schedule",
"repository_dispatch",
] as const;
// Derive types from constants for better maintainability
type EntityEventName = (typeof ENTITY_EVENT_NAMES)[number];
@@ -62,6 +67,17 @@ type BaseContext = {
full_name: string;
};
actor: string;
payload:
| IssuesEvent
| IssueCommentEvent
| PullRequestEvent
| PullRequestReviewEvent
| PullRequestReviewCommentEvent
| RepositoryDispatchEvent
| WorkflowDispatchEvent
| ScheduleEvent;
entityNumber?: number;
isPR?: boolean;
inputs: {
mode: ModeName;
triggerPhrase: string;
@@ -78,6 +94,14 @@ type BaseContext = {
additionalPermissions: Map<string, string>;
useCommitSigning: boolean;
};
progressTracking?: {
headers?: Record<string, string>;
resumeEndpoint?: string;
sessionId?: string;
progressEndpoint: string;
systemProgressEndpoint?: string;
oauthTokenEndpoint?: string;
};
};
// Context for entity-based events (issues, PRs, comments)
@@ -96,7 +120,7 @@ export type ParsedGitHubContext = BaseContext & {
// Context for automation events (workflow_dispatch, schedule)
export type AutomationContext = BaseContext & {
eventName: AutomationEventName;
payload: WorkflowDispatchEvent | ScheduleEvent;
payload: WorkflowDispatchEvent | ScheduleEvent | RepositoryDispatchEvent;
};
// Union type for all contexts
@@ -190,6 +214,66 @@ export function parseGitHubContext(): GitHubContext {
isPR: true,
};
}
case "repository_dispatch": {
const payload = context.payload as RepositoryDispatchEvent;
// Extract task description from client_payload
const clientPayload = payload.client_payload as {
prompt?: string;
stream_endpoint?: string;
headers?: Record<string, string>;
resume_endpoint?: string;
session_id?: string;
endpoints?: {
resume?: string;
progress?: string;
system_progress?: string;
oauth_endpoint?: string;
};
overrideInputs?: {
model?: string;
base_branch?: string;
};
};
// Override directPrompt with the prompt
if (clientPayload.prompt) {
commonFields.inputs.directPrompt = clientPayload.prompt;
}
// Apply input overrides
if (clientPayload.overrideInputs) {
if (clientPayload.overrideInputs.base_branch) {
commonFields.inputs.baseBranch =
clientPayload.overrideInputs.base_branch;
}
}
// Set up progress tracking - prioritize endpoints object if available, fallback to individual fields
let progressTracking: ParsedGitHubContext["progressTracking"] = undefined;
if (clientPayload.endpoints?.progress || clientPayload.stream_endpoint) {
progressTracking = {
progressEndpoint:
clientPayload.endpoints?.progress ||
clientPayload.stream_endpoint ||
"",
headers: clientPayload.headers,
resumeEndpoint:
// clientPayload.endpoints?.resume || clientPayload.resume_endpoint,
clientPayload.resume_endpoint,
sessionId: clientPayload.session_id,
systemProgressEndpoint: clientPayload.endpoints?.system_progress,
oauthTokenEndpoint: clientPayload.endpoints?.oauth_endpoint,
};
}
return {
...commonFields,
eventName: "repository_dispatch",
payload: payload,
progressTracking,
};
}
case "workflow_dispatch": {
return {
...commonFields,
@@ -287,3 +371,9 @@ export function isAutomationContext(
context.eventName as AutomationEventName,
);
}
export function isRepositoryDispatchEvent(
context: GitHubContext,
): context is GitHubContext & { payload: RepositoryDispatchEvent } {
return context.eventName === "repository_dispatch";
}

View File

@@ -8,7 +8,7 @@
import { $ } from "bun";
import * as core from "@actions/core";
import type { ParsedGitHubContext } from "../context";
import type { GitHubContext } from "../context";
import type { GitHubPullRequest } from "../types";
import type { Octokits } from "../api/client";
import type { FetchDataResult } from "../data/fetcher";
@@ -21,15 +21,15 @@ export type BranchInfo = {
export async function setupBranch(
octokits: Octokits,
githubData: FetchDataResult,
context: ParsedGitHubContext,
githubData: FetchDataResult | null,
context: GitHubContext,
): Promise<BranchInfo> {
const { owner, repo } = context.repository;
const entityNumber = context.entityNumber;
const { baseBranch, branchPrefix } = context.inputs;
const isPR = context.isPR;
if (isPR) {
if (isPR && githubData) {
const prData = githubData.contextData as GitHubPullRequest;
const prState = prData.state;
@@ -84,19 +84,27 @@ export async function setupBranch(
sourceBranch = repoResponse.data.default_branch;
}
// Generate branch name for either an issue or closed/merged PR
const entityType = isPR ? "pr" : "issue";
// Generate branch name for either an issue, closed/merged PR, or repository_dispatch event
let branchName: string;
// Create Kubernetes-compatible timestamp: lowercase, hyphens only, shorter format
const now = new Date();
const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`;
if (context.eventName === "repository_dispatch") {
// For repository_dispatch events, use run ID for uniqueness since there's no entity number
const now = new Date();
const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`;
branchName = `${branchPrefix}dispatch-${context.runId}-${timestamp}`;
} else {
// For issues and PRs, use the existing logic
const entityType = isPR ? "pr" : "issue";
const now = new Date();
const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`;
branchName = `${branchPrefix}${entityType}-${entityNumber}-${timestamp}`;
}
// Ensure branch name is Kubernetes-compatible:
// - Lowercase only
// - Alphanumeric with hyphens
// - No underscores
// - Max 50 chars (to allow for prefixes)
const branchName = `${branchPrefix}${entityType}-${entityNumber}-${timestamp}`;
const newBranch = branchName.toLowerCase().substring(0, 50);
try {
@@ -132,8 +140,18 @@ export async function setupBranch(
}
// For non-signing case, create and checkout the branch locally only
const entityType =
context.eventName === "repository_dispatch"
? "dispatch"
: isPR
? "pr"
: "issue";
const entityId =
context.eventName === "repository_dispatch"
? context.runId
: entityNumber!.toString();
console.log(
`Creating local branch ${newBranch} for ${entityType} #${entityNumber} from source branch: ${sourceBranch}...`,
`Creating local branch ${newBranch} for ${entityType} ${entityId} from source branch: ${sourceBranch}...`,
);
// Fetch and checkout the source branch first to ensure we branch from the correct base

View File

@@ -6,7 +6,7 @@
*/
import { $ } from "bun";
import type { ParsedGitHubContext } from "../context";
import type { GitHubContext } from "../context";
import { GITHUB_SERVER_URL } from "../api/config";
type GitUser = {
@@ -16,7 +16,7 @@ type GitUser = {
export async function configureGitAuth(
githubToken: string,
context: ParsedGitHubContext,
context: GitHubContext,
user: GitUser | null,
) {
console.log("Configuring git authentication for non-signing mode");

View File

@@ -1,6 +1,6 @@
import * as core from "@actions/core";
import { GITHUB_API_URL, GITHUB_SERVER_URL } from "../github/api/config";
import type { ParsedGitHubContext } from "../github/context";
import type { GitHubContext } from "../github/context";
import { Octokit } from "@octokit/rest";
type PrepareConfigParams = {
@@ -12,7 +12,7 @@ type PrepareConfigParams = {
additionalMcpConfig?: string;
claudeCommentId?: string;
allowedTools: string[];
context: ParsedGitHubContext;
context: GitHubContext;
};
async function checkActionsReadPermission(

View File

@@ -16,9 +16,15 @@ import { agentMode } from "./agent";
import { reviewMode } from "./review";
import type { GitHubContext } from "../github/context";
import { isAutomationContext } from "../github/context";
import { remoteAgentMode } from "./remote-agent";
export const DEFAULT_MODE = "tag" as const;
export const VALID_MODES = ["tag", "agent", "experimental-review"] as const;
export const VALID_MODES = [
"tag",
"agent",
"remote-agent",
"experimental-review",
] as const;
/**
* All available modes.
@@ -28,6 +34,7 @@ const modes = {
tag: tagMode,
agent: agentMode,
"experimental-review": reviewMode,
"remote-agent": remoteAgentMode,
} as const satisfies Record<ModeName, Mode>;
/**
@@ -49,7 +56,13 @@ export function getMode(name: ModeName, context: GitHubContext): Mode {
// Validate mode can handle the event type
if (name === "tag" && isAutomationContext(context)) {
throw new Error(
`Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events.`,
`Tag mode cannot handle ${context.eventName} events. Use 'agent' mode for automation events or 'remote-agent' mode for repository_dispatch events.`,
);
}
if (name === "remote-agent" && context.eventName !== "repository_dispatch") {
throw new Error(
`Remote agent mode can only handle repository_dispatch events. Use 'tag' mode for @claude mentions or 'agent' mode for other automation events.`,
);
}

View File

@@ -0,0 +1,463 @@
import * as core from "@actions/core";
import { mkdir, writeFile } from "fs/promises";
import type { Mode, ModeOptions, ModeResult } from "../types";
import { isRepositoryDispatchEvent } from "../../github/context";
import type { GitHubContext } from "../../github/context";
import { setupBranch } from "../../github/operations/branch";
import { configureGitAuth } from "../../github/operations/git-config";
import { prepareMcpConfig } from "../../mcp/install-mcp-server";
import { GITHUB_SERVER_URL } from "../../github/api/config";
import {
buildAllowedToolsString,
buildDisallowedToolsString,
type PreparedContext,
} from "../../create-prompt";
import {
reportWorkflowInitialized,
reportClaudeStarting,
reportWorkflowFailed,
} from "./system-progress-handler";
import type { SystemProgressConfig } from "./progress-types";
import { fetchUserDisplayName } from "../../github/data/fetcher";
import { createOctokit } from "../../github/api/client";
import type { StreamConfig } from "../../types/stream-config";
/**
* Fetches a Claude Code OAuth token from the specified endpoint using OIDC authentication
*/
async function fetchClaudeCodeOAuthToken(
oauthTokenEndpoint: string,
oidcToken?: string,
sessionId?: string,
): Promise<string> {
console.log(`Fetching Claude Code OAuth token from: ${oauthTokenEndpoint}`);
try {
if (!oidcToken) {
throw new Error("OIDC token is required for OAuth authentication");
}
// Make request to OAuth token endpoint
const response = await fetch(oauthTokenEndpoint, {
method: "POST",
headers: {
Authorization: `Bearer ${oidcToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
...(sessionId && { session_id: sessionId }),
}),
});
if (!response.ok) {
throw new Error(
`OAuth token request failed: ${response.status} ${response.statusText}`,
);
}
const data = (await response.json()) as {
oauth_token?: string;
message?: string;
};
if (!data.oauth_token) {
const message = data.message || "Unknown error";
throw new Error(`OAuth token request failed: ${message}`);
}
console.log("Successfully fetched Claude Code OAuth token");
return data.oauth_token;
} catch (error) {
console.error("Failed to fetch Claude Code OAuth token:", error);
throw error;
}
}
/**
* Remote Agent mode implementation.
*
* This mode is specifically designed for repository_dispatch events triggered by external APIs.
* It bypasses the standard trigger checking, comment tracking, and GitHub data fetching used by tag mode,
* making it ideal for automated tasks triggered via API calls with custom payloads.
*/
export const remoteAgentMode: Mode = {
name: "remote-agent",
description: "Remote automation mode for repository_dispatch events",
shouldTrigger(context) {
// Only trigger for repository_dispatch events
return isRepositoryDispatchEvent(context);
},
prepareContext(context, data) {
// Remote agent mode uses minimal context
return {
mode: "remote-agent",
githubContext: context,
baseBranch: data?.baseBranch,
claudeBranch: data?.claudeBranch,
};
},
getAllowedTools() {
return [];
},
getDisallowedTools() {
return [];
},
shouldCreateTrackingComment() {
return false;
},
async prepare({
context,
octokit,
githubToken,
}: ModeOptions): Promise<ModeResult> {
// Remote agent mode handles repository_dispatch events only
if (!isRepositoryDispatchEvent(context)) {
throw new Error(
"Remote agent mode can only handle repository_dispatch events",
);
}
// Extract task details from client_payload
const payload = context.payload;
const clientPayload = payload.client_payload as {
prompt?: string;
stream_endpoint?: string;
headers?: Record<string, string>;
resume_endpoint?: string;
session_id?: string;
endpoints?: {
stream?: string;
progress?: string;
systemProgress?: string;
oauthToken?: string;
};
overrideInputs?: {
model?: string;
base_branch?: string;
};
};
// Get OIDC token for streaming and potential OAuth token fetching
let oidcToken: string;
try {
oidcToken = await core.getIDToken("claude-code-github-action");
} catch (error) {
console.error("Failed to get OIDC token:", error);
throw new Error(
`OIDC token required for remote-agent mode. Please add 'id-token: write' to your workflow permissions. Error: ${error}`,
);
}
// Set up system progress config if endpoint is provided
let systemProgressConfig: SystemProgressConfig | null = null;
if (context.progressTracking?.systemProgressEndpoint) {
systemProgressConfig = {
endpoint: context.progressTracking.systemProgressEndpoint,
headers: context.progressTracking.headers,
};
}
// Handle authentication - fetch OAuth token if needed
const anthropicApiKey = process.env.ANTHROPIC_API_KEY;
const claudeCodeOAuthToken = process.env.CLAUDE_CODE_OAUTH_TOKEN;
if (!anthropicApiKey && !claudeCodeOAuthToken) {
const oauthTokenEndpoint = context.progressTracking?.oauthTokenEndpoint;
if (oauthTokenEndpoint) {
console.log(
"No API key or OAuth token found, fetching OAuth token from endpoint",
);
try {
const fetchedToken = await fetchClaudeCodeOAuthToken(
oauthTokenEndpoint,
oidcToken,
context.progressTracking?.sessionId,
);
core.setOutput("claude_code_oauth_token", fetchedToken);
console.log(
"Successfully fetched and set OAuth token for Claude Code",
);
} catch (error) {
console.error("Failed to fetch OAuth token:", error);
throw new Error(
`Authentication failed: No API key or OAuth token available, and OAuth token fetching failed: ${error}`,
);
}
} else {
throw new Error(
"No authentication available: Missing ANTHROPIC_API_KEY, CLAUDE_CODE_OAUTH_TOKEN, and no OAuth token endpoint provided",
);
}
} else {
console.log("Using existing authentication (API key or OAuth token)");
}
const taskDescription =
clientPayload.prompt ||
context.inputs.directPrompt ||
"No task description provided";
// Setup branch for work isolation
let branchInfo;
try {
branchInfo = await setupBranch(octokit, null, context);
} catch (error) {
// Report failure if we have system progress config
if (systemProgressConfig) {
reportWorkflowFailed(
systemProgressConfig,
oidcToken,
"initialization",
error as Error,
"branch_setup_failed",
);
}
throw error;
}
// Configure git authentication if not using commit signing
if (!context.inputs.useCommitSigning) {
try {
// Force Claude bot as git user
await configureGitAuth(githubToken, context, {
login: "claude[bot]",
id: 209825114,
});
} catch (error) {
console.error("Failed to configure git authentication:", error);
// Report failure if we have system progress config
if (systemProgressConfig) {
reportWorkflowFailed(
systemProgressConfig,
oidcToken,
"initialization",
error as Error,
"git_config_failed",
);
}
throw error;
}
}
// Report workflow initialized
if (systemProgressConfig) {
reportWorkflowInitialized(
systemProgressConfig,
oidcToken,
branchInfo.claudeBranch || branchInfo.currentBranch,
branchInfo.baseBranch,
context.progressTracking?.sessionId,
);
}
// Create prompt directory
await mkdir(`${process.env.RUNNER_TEMP}/claude-prompts`, {
recursive: true,
});
// Fetch trigger user display name from context.actor
let triggerDisplayName: string | null | undefined;
if (context.actor) {
try {
const octokits = createOctokit(githubToken);
triggerDisplayName = await fetchUserDisplayName(
octokits,
context.actor,
);
} catch (error) {
console.warn(
`Failed to fetch user display name for ${context.actor}:`,
error,
);
}
}
// Generate dispatch-specific prompt (just the task description)
const promptContent = generateDispatchPrompt(taskDescription);
// Write the prompt file
await writeFile(
`${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`,
promptContent,
);
// Set stream configuration for repository_dispatch events
if (context.progressTracking) {
const streamConfig: StreamConfig = {};
if (context.progressTracking.resumeEndpoint) {
streamConfig.resume_endpoint = context.progressTracking.resumeEndpoint;
}
if (context.progressTracking.sessionId) {
streamConfig.session_id = context.progressTracking.sessionId;
}
if (context.progressTracking.progressEndpoint) {
streamConfig.progress_endpoint =
context.progressTracking.progressEndpoint;
}
if (context.progressTracking.systemProgressEndpoint) {
streamConfig.system_progress_endpoint =
context.progressTracking.systemProgressEndpoint;
}
// Merge provided headers with OIDC token
const headers: Record<string, string> = {
...(context.progressTracking.headers || {}),
};
// Use existing OIDC token for streaming
headers["Authorization"] = `Bearer ${oidcToken}`;
if (Object.keys(headers).length > 0) {
streamConfig.headers = headers;
}
console.log("Setting stream config:", streamConfig);
core.setOutput("stream_config", JSON.stringify(streamConfig));
}
// Export tool environment variables for remote agent mode
// Check if we have actions:read permission for CI tools
const hasActionsReadPermission =
context.inputs.additionalPermissions.get("actions") === "read";
const allowedToolsString = buildAllowedToolsString(
context.inputs.allowedTools,
hasActionsReadPermission,
context.inputs.useCommitSigning,
);
const disallowedToolsString = buildDisallowedToolsString(
context.inputs.disallowedTools,
);
core.exportVariable("ALLOWED_TOOLS", allowedToolsString);
core.exportVariable("DISALLOWED_TOOLS", disallowedToolsString);
// Handle model override from repository_dispatch payload
if (clientPayload.overrideInputs?.model) {
core.setOutput("anthropic_model", clientPayload.overrideInputs.model);
}
// Get minimal MCP configuration for remote agent mode
const additionalMcpConfig = process.env.MCP_CONFIG || "";
const mcpConfig = await prepareMcpConfig({
githubToken,
owner: context.repository.owner,
repo: context.repository.repo,
branch: branchInfo.claudeBranch || branchInfo.currentBranch,
baseBranch: branchInfo.baseBranch,
additionalMcpConfig,
claudeCommentId: "", // No comment ID for remote agent mode
allowedTools: context.inputs.allowedTools,
context,
});
core.setOutput("mcp_config", mcpConfig);
// Report Claude is starting
if (systemProgressConfig) {
reportClaudeStarting(systemProgressConfig, oidcToken);
}
// Track Claude start time for duration calculation
core.setOutput("claude_start_time", Date.now().toString());
// Export system prompt for remote agent mode
const systemPrompt = generateDispatchSystemPrompt(
context,
branchInfo.baseBranch,
branchInfo.claudeBranch,
context.actor,
triggerDisplayName,
);
core.exportVariable("APPEND_SYSTEM_PROMPT", systemPrompt);
return {
commentId: undefined, // No comment tracking for remote agent mode
branchInfo,
mcpConfig,
};
},
generatePrompt(context: PreparedContext): string {
// TODO: update this to generate a more meaningful prompt
return `Repository: ${context.repository}`;
},
};
/**
* Generates a task-focused prompt for repository_dispatch events
*/
function generateDispatchPrompt(taskDescription: string): string {
return taskDescription;
}
/**
* Generates the system prompt portion for repository_dispatch events
*/
function generateDispatchSystemPrompt(
context: GitHubContext,
baseBranch: string,
claudeBranch: string | undefined,
triggerUsername?: string,
triggerDisplayName?: string | null,
): string {
const { repository } = context;
const coAuthorLine =
triggerUsername && (triggerDisplayName || triggerUsername !== "Unknown")
? `Co-authored-by: ${triggerDisplayName ?? triggerUsername} <${triggerUsername}@users.noreply.github.com>`
: "";
let commitInstructions = "";
if (context.inputs.useCommitSigning) {
commitInstructions = `- Use mcp__github_file_ops__commit_files and mcp__github_file_ops__delete_files to commit and push changes`;
if (coAuthorLine) {
commitInstructions += `
- When pushing changes, include a Co-authored-by trailer in the commit message
- Use: "${coAuthorLine}"`;
}
} else {
commitInstructions = `- Use git commands via the Bash tool to commit and push your changes:
- Stage files: Bash(git add <files>)
- Commit with a descriptive message: Bash(git commit -m "<message>")`;
if (coAuthorLine) {
commitInstructions += `
- When committing, include a Co-authored-by trailer:
Bash(git commit -m "<message>\\n\\n${coAuthorLine}")`;
}
commitInstructions += `
- Be sure to follow your commit message guidelines
- Push to the remote: Bash(git push origin HEAD)`;
}
return `You are Claude, an AI assistant designed to help with GitHub issues and pull requests. Think carefully as you analyze the context and respond appropriately. Here's the context for your current task:
Your task is to complete the request described in the task description.
Instructions:
1. For questions: Research the codebase and provide a detailed answer
2. For implementations: Make the requested changes, commit, and push
Key points:
- You're already on a new branch - don't create another
${commitInstructions}
${
claudeBranch
? `- After completing your work, provide a URL to create a PR in this format:
${GITHUB_SERVER_URL}/${repository.owner}/${repository.repo}/compare/${baseBranch}...${claudeBranch}?quick_pull=1`
: ""
}`;
}

View File

@@ -0,0 +1,78 @@
/**
* System progress tracking types for remote agent mode
*/
/**
* Base event structure
*/
type BaseProgressEvent = {
timestamp: string; // ISO 8601
};
/**
* Workflow initializing event
*/
export type WorkflowInitializingEvent = BaseProgressEvent & {
event_type: "workflow_initializing";
data: {
branch: string;
base_branch: string;
session_id?: string;
};
};
/**
* Claude starting event
*/
export type ClaudeStartingEvent = BaseProgressEvent & {
event_type: "claude_starting";
data: Record<string, never>; // No data needed
};
/**
* Claude complete event
*/
export type ClaudeCompleteEvent = BaseProgressEvent & {
event_type: "claude_complete";
data: {
exit_code: number;
duration_ms: number;
};
};
/**
* Workflow failed event
*/
export type WorkflowFailedEvent = BaseProgressEvent & {
event_type: "workflow_failed";
data: {
error: {
phase: "initialization" | "claude_execution";
message: string;
code: string;
};
};
};
/**
* Discriminated union of all progress events
*/
export type ProgressEvent =
| WorkflowInitializingEvent
| ClaudeStartingEvent
| ClaudeCompleteEvent
| WorkflowFailedEvent;
/**
* Payload sent to the system progress endpoint
*/
export type SystemProgressPayload = ProgressEvent;
/**
* Configuration for system progress reporting
*/
export type SystemProgressConfig = {
endpoint: string;
headers?: Record<string, string>;
timeout_ms?: number; // Default: 5000
};

View File

@@ -0,0 +1,149 @@
import * as core from "@actions/core";
import type {
ProgressEvent,
SystemProgressPayload,
SystemProgressConfig,
WorkflowInitializingEvent,
ClaudeStartingEvent,
ClaudeCompleteEvent,
WorkflowFailedEvent,
} from "./progress-types";
/**
* Send a progress event to the system progress endpoint (fire-and-forget)
*/
function sendProgressEvent(
event: ProgressEvent,
config: SystemProgressConfig,
oidcToken: string,
): void {
const payload: SystemProgressPayload = event;
console.log(
`Sending system progress event: ${event.event_type}`,
JSON.stringify(payload, null, 2),
);
// Fire and forget - don't await
Promise.resolve().then(async () => {
try {
// Create an AbortController for timeout
const controller = new AbortController();
const timeoutId = setTimeout(
() => controller.abort(),
config.timeout_ms || 5000,
);
try {
const response = await fetch(config.endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${oidcToken}`,
...config.headers,
},
body: JSON.stringify(payload),
signal: controller.signal,
});
if (!response.ok) {
console.error(
`System progress endpoint returned ${response.status}: ${response.statusText}`,
);
}
} finally {
clearTimeout(timeoutId);
}
} catch (error) {
// Log but don't throw - we don't want progress reporting to interrupt the workflow
core.warning(`Failed to send system progress event: ${error}`);
}
});
}
/**
* Report workflow initialization complete
*/
export function reportWorkflowInitialized(
config: SystemProgressConfig,
oidcToken: string,
branch: string,
baseBranch: string,
sessionId?: string,
): void {
const event: WorkflowInitializingEvent = {
timestamp: new Date().toISOString(),
event_type: "workflow_initializing",
data: {
branch,
base_branch: baseBranch,
...(sessionId && { session_id: sessionId }),
},
};
sendProgressEvent(event, config, oidcToken);
}
/**
* Report Claude is starting
*/
export function reportClaudeStarting(
config: SystemProgressConfig,
oidcToken: string,
): void {
const event: ClaudeStartingEvent = {
timestamp: new Date().toISOString(),
event_type: "claude_starting",
data: {},
};
sendProgressEvent(event, config, oidcToken);
}
/**
* Report Claude completed
*/
export function reportClaudeComplete(
config: SystemProgressConfig,
oidcToken: string,
exitCode: number,
durationMs: number,
): void {
const event: ClaudeCompleteEvent = {
timestamp: new Date().toISOString(),
event_type: "claude_complete",
data: {
exit_code: exitCode,
duration_ms: durationMs,
},
};
sendProgressEvent(event, config, oidcToken);
}
/**
* Report workflow failed
*/
export function reportWorkflowFailed(
config: SystemProgressConfig,
oidcToken: string,
phase: "initialization" | "claude_execution",
error: Error | string,
code: string,
): void {
const errorMessage = error instanceof Error ? error.message : error;
const event: WorkflowFailedEvent = {
timestamp: new Date().toISOString(),
event_type: "workflow_failed",
data: {
error: {
phase,
message: errorMessage,
code,
},
},
};
sendProgressEvent(event, config, oidcToken);
}

View File

@@ -3,7 +3,7 @@ import type { PreparedContext } from "../create-prompt/types";
import type { FetchDataResult } from "../github/data/fetcher";
import type { Octokits } from "../github/api/client";
export type ModeName = "tag" | "agent" | "experimental-review";
export type ModeName = "tag" | "agent" | "remote-agent" | "experimental-review";
export type ModeContext = {
mode: ModeName;

View File

@@ -0,0 +1,19 @@
/**
* Configuration for streaming and progress tracking
*/
export type StreamConfig = {
/** Endpoint for streaming Claude execution progress */
progress_endpoint?: string;
/** Endpoint for system-level progress reporting (workflow lifecycle events) */
system_progress_endpoint?: string;
/** Resume endpoint for teleport functionality */
resume_endpoint?: string;
/** Session ID for tracking */
session_id?: string;
/** Headers to include with streaming requests (includes Authorization) */
headers?: Record<string, string>;
};

View File

@@ -39,13 +39,13 @@ describe("Mode Registry", () => {
test("getMode throws error for tag mode with workflow_dispatch event", () => {
expect(() => getMode("tag", mockWorkflowDispatchContext)).toThrow(
"Tag mode cannot handle workflow_dispatch events. Use 'agent' mode for automation events.",
"Tag mode cannot handle workflow_dispatch events. Use 'agent' mode for automation events or 'remote-agent' mode for repository_dispatch events.",
);
});
test("getMode throws error for tag mode with schedule event", () => {
expect(() => getMode("tag", mockScheduleContext)).toThrow(
"Tag mode cannot handle schedule events. Use 'agent' mode for automation events.",
"Tag mode cannot handle schedule events. Use 'agent' mode for automation events or 'remote-agent' mode for repository_dispatch events.",
);
});
@@ -64,7 +64,7 @@ describe("Mode Registry", () => {
test("getMode throws error for invalid mode", () => {
const invalidMode = "invalid" as unknown as ModeName;
expect(() => getMode(invalidMode, mockContext)).toThrow(
"Invalid mode 'invalid'. Valid modes are: 'tag', 'agent', 'experimental-review'. Please check your workflow configuration.",
"Invalid mode 'invalid'. Valid modes are: 'tag', 'agent', 'remote-agent', 'experimental-review'. Please check your workflow configuration.",
);
});
@@ -72,6 +72,7 @@ describe("Mode Registry", () => {
expect(isValidMode("tag")).toBe(true);
expect(isValidMode("agent")).toBe(true);
expect(isValidMode("experimental-review")).toBe(true);
expect(isValidMode("remote-agent")).toBe(true);
});
test("isValidMode returns false for invalid mode", () => {

View File

@@ -0,0 +1,28 @@
import { describe, test, expect } from "bun:test";
import type { StreamConfig } from "../src/types/stream-config";
describe("report-claude-complete", () => {
test("StreamConfig type should include system_progress_endpoint", () => {
const config: StreamConfig = {
progress_endpoint: "https://example.com/progress",
system_progress_endpoint: "https://example.com/system-progress",
resume_endpoint: "https://example.com/resume",
session_id: "test-session",
headers: {
Authorization: "Bearer test-token",
},
};
expect(config.system_progress_endpoint).toBe(
"https://example.com/system-progress",
);
});
test("StreamConfig type should allow optional fields", () => {
const config: StreamConfig = {};
expect(config.system_progress_endpoint).toBeUndefined();
expect(config.progress_endpoint).toBeUndefined();
expect(config.headers).toBeUndefined();
});
});