runtime: split ipc queue and runner helpers

This commit is contained in:
ejclaw
2026-04-11 05:49:40 +09:00
parent 30dda74621
commit 88f8b3006c
11 changed files with 1498 additions and 1389 deletions

View File

@@ -16,20 +16,11 @@
import fs from 'fs';
import path from 'path';
import { query } from '@anthropic-ai/claude-agent-sdk';
import {
query,
HookCallback,
PreCompactHookInput,
PreToolUseHookInput,
} from '@anthropic-ai/claude-agent-sdk';
import {
extractImageTagPaths,
IPC_CLOSE_SENTINEL,
IPC_INPUT_SUBDIR,
IPC_POLL_MS,
normalizePublicTextOutput,
type RunnerStructuredOutput,
writeProtocolOutput,
} from 'ejclaw-runners-shared';
import { fileURLToPath } from 'url';
@@ -44,11 +35,24 @@ import {
getClaudeReadonlySandboxMode,
isArbiterRuntimeEnvEnabled,
isClaudeReadonlyReviewerRuntime,
isReviewerMutatingShellCommand,
isReviewerRuntime,
isReviewerRuntimeEnvEnabled,
} from './reviewer-runtime.js';
import { selectCompactMemoriesFromSummary } from './memory-selection.js';
import { drainIpcInput, shouldClose } from './ipc-input.js';
import {
MessageStream,
buildMultimodalContent,
extractAssistantText,
normalizeStructuredOutput,
readStdin,
type RunnerOutput,
writeOutput,
} from './output-protocol.js';
import {
createPreCompactHook,
createReviewerBashGuardHook,
createSanitizeBashHook,
} from './runner-hooks.js';
interface RunnerInput {
prompt: string;
@@ -62,53 +66,6 @@ interface RunnerInput {
roomRoleContext?: RoomRoleContext;
}
/** Mirrors AgentOutput in src/agent-runner.ts (separate package, can't import directly). */
interface RunnerOutput {
status: 'success' | 'error';
phase?: 'progress' | 'final' | 'tool-activity' | 'intermediate';
agentId?: string;
agentLabel?: string;
agentDone?: boolean;
result: string | null;
output?: RunnerStructuredOutput;
newSessionId?: string;
error?: string;
}
interface SessionEntry {
sessionId: string;
fullPath: string;
summary: string;
firstPrompt: string;
}
interface SessionsIndex {
entries: SessionEntry[];
}
type ContentBlock =
| { type: 'text'; text: string }
| {
type: 'image';
source: {
type: 'base64';
media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
data: string;
};
};
interface SDKUserMessage {
type: 'user';
message: { role: 'user'; content: string | ContentBlock[] };
parent_tool_use_id: null;
session_id: string;
}
interface AssistantContentBlock {
type?: string;
text?: string;
}
// Paths configurable via env vars.
const GROUP_DIR = process.env.EJCLAW_GROUP_DIR || '/workspace/group';
const IPC_DIR = process.env.EJCLAW_IPC_DIR || '/workspace/ipc';
@@ -120,116 +77,6 @@ const GROUP_FOLDER = process.env.EJCLAW_GROUP_FOLDER || '';
const IPC_INPUT_DIR = path.join(IPC_DIR, IPC_INPUT_SUBDIR);
const IPC_INPUT_CLOSE_SENTINEL = path.join(IPC_INPUT_DIR, IPC_CLOSE_SENTINEL);
const HOST_TASKS_DIR = path.join(HOST_IPC_DIR, 'tasks');
const MIME_TYPES: Record<string, string> = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.webp': 'image/webp',
};
/**
* Parse [Image: /absolute/path] tags from text and build multimodal content.
* Returns a plain string if no images found, or ContentBlock[] with text + image blocks.
*/
function buildMultimodalContent(text: string): string | ContentBlock[] {
const { cleanText, imagePaths } = extractImageTagPaths(text);
if (imagePaths.length === 0) return text;
const blocks: ContentBlock[] = [];
if (cleanText) {
blocks.push({ type: 'text', text: cleanText });
}
for (const imgPath of imagePaths) {
try {
if (!fs.existsSync(imgPath)) {
log(`Image not found, skipping: ${imgPath}`);
continue;
}
const data = fs.readFileSync(imgPath).toString('base64');
const ext = path.extname(imgPath).toLowerCase();
const mediaType = (MIME_TYPES[ext] || 'image/png') as
| 'image/jpeg'
| 'image/png'
| 'image/gif'
| 'image/webp';
blocks.push({
type: 'image',
source: { type: 'base64', media_type: mediaType, data },
});
log(`Added image block: ${imgPath} (${mediaType})`);
} catch (err) {
log(
`Failed to read image ${imgPath}: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
return blocks.length > 0 ? blocks : text;
}
/**
* Push-based async iterable for streaming user messages to the SDK.
* Keeps the iterable alive until end() is called, preventing isSingleUserTurn.
*/
class MessageStream {
private queue: SDKUserMessage[] = [];
private waiting: (() => void) | null = null;
private done = false;
push(text: string): void {
const content = buildMultimodalContent(text);
this.queue.push({
type: 'user',
message: { role: 'user', content },
parent_tool_use_id: null,
session_id: '',
});
this.waiting?.();
}
end(): void {
this.done = true;
this.waiting?.();
}
async *[Symbol.asyncIterator](): AsyncGenerator<SDKUserMessage> {
while (true) {
while (this.queue.length > 0) {
yield this.queue.shift()!;
}
if (this.done) return;
await new Promise<void>((r) => {
this.waiting = r;
});
this.waiting = null;
}
}
}
async function readStdin(): Promise<string> {
return new Promise((resolve, reject) => {
let data = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (chunk) => {
data += chunk;
});
process.stdin.on('end', () => resolve(data));
process.stdin.on('error', reject);
});
}
function writeOutput(output: RunnerOutput): void {
writeProtocolOutput(output);
}
function normalizeStructuredOutput(result: string | null): {
result: string | null;
output?: RunnerOutput['output'];
} {
return normalizePublicTextOutput(result);
}
function log(message: string): void {
console.error(`[agent-runner] ${message}`);
@@ -242,349 +89,6 @@ process.on('SIGTERM', () => {
agentAbortController.abort();
});
function extractAssistantText(message: unknown): string | null {
const assistant = message as {
message?: {
content?: AssistantContentBlock[];
};
};
const blocks = assistant.message?.content;
if (!Array.isArray(blocks)) return null;
const text = blocks
.filter((block) => block?.type === 'text' && typeof block.text === 'string')
.map((block) => block.text!.trim())
.filter(Boolean)
.join('\n\n')
.trim();
return text || null;
}
function getSessionSummary(
sessionId: string,
transcriptPath: string,
): string | null {
const projectDir = path.dirname(transcriptPath);
const indexPath = path.join(projectDir, 'sessions-index.json');
if (!fs.existsSync(indexPath)) {
log(`Sessions index not found at ${indexPath}`);
return null;
}
try {
const index: SessionsIndex = JSON.parse(
fs.readFileSync(indexPath, 'utf-8'),
);
const entry = index.entries.find((e) => e.sessionId === sessionId);
if (entry?.summary) {
return entry.summary;
}
} catch (err) {
log(
`Failed to read sessions index: ${err instanceof Error ? err.message : String(err)}`,
);
}
return null;
}
function trimSummary(summary: string, maxChars: number): string {
if (summary.length <= maxChars) return summary;
return summary.slice(0, Math.max(0, maxChars - 1)).trimEnd() + '…';
}
function writeHostTaskIpcFile(data: object): string {
fs.mkdirSync(HOST_TASKS_DIR, { recursive: true });
const filename = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}.json`;
const filepath = path.join(HOST_TASKS_DIR, filename);
const tempPath = `${filepath}.tmp`;
fs.writeFileSync(tempPath, JSON.stringify(data, null, 2));
fs.renameSync(tempPath, filepath);
return filename;
}
async function persistCompactMemory(
summary: string,
sessionId: string,
): Promise<void> {
const normalized = summary.trim();
if (!normalized || !GROUP_FOLDER) return;
try {
const scopeKey = `room:${GROUP_FOLDER}`;
const selected = selectCompactMemoriesFromSummary(normalized, scopeKey);
if (selected.length === 0) {
log('Skipped compact memory persist - no salient room memory found');
return;
}
for (const memory of selected) {
const file = writeHostTaskIpcFile({
type: 'persist_memory',
scopeKind: 'room',
scopeKey,
content: trimSummary(memory.content, 300),
keywords: memory.keywords,
memory_kind: memory.memoryKind,
source_kind: 'compact',
source_ref: sessionId ? `compact:${sessionId}` : null,
timestamp: new Date().toISOString(),
});
log(`Persisted compact memory via IPC (${file})`);
}
} catch (err) {
log(
`Failed to persist compact memory via IPC: ${
err instanceof Error ? err.message : String(err)
}`,
);
}
}
/**
* Archive the full transcript to conversations/ before compaction.
*/
function createPreCompactHook(assistantName?: string): HookCallback {
return async (input, _toolUseId, _context) => {
const preCompact = input as PreCompactHookInput;
const transcriptPath = preCompact.transcript_path;
const sessionId = preCompact.session_id;
const trigger = preCompact.trigger || 'auto';
// Show compact status in chat so users know it's not just slow loading
writeOutput({
status: 'success',
phase: 'progress',
result: trigger === 'auto' ? '대화 요약 중...' : '컴팩트 중...',
});
if (!transcriptPath || !fs.existsSync(transcriptPath)) {
log('No transcript found for archiving');
return {};
}
try {
const content = fs.readFileSync(transcriptPath, 'utf-8');
const messages = parseTranscript(content);
if (messages.length === 0) {
log('No messages to archive');
return {};
}
const summary = getSessionSummary(sessionId, transcriptPath);
const name = summary ? sanitizeFilename(summary) : generateFallbackName();
const conversationsDir = path.join(GROUP_DIR, 'conversations');
fs.mkdirSync(conversationsDir, { recursive: true });
const date = new Date().toISOString().split('T')[0];
const filename = `${date}-${name}.md`;
const filePath = path.join(conversationsDir, filename);
const markdown = formatTranscriptMarkdown(
messages,
summary,
assistantName,
);
fs.writeFileSync(filePath, markdown);
log(`Archived conversation to ${filePath}`);
if (summary) {
await persistCompactMemory(summary, sessionId);
}
} catch (err) {
log(
`Failed to archive transcript: ${err instanceof Error ? err.message : String(err)}`,
);
}
return {};
};
}
// Secrets to strip from Bash tool subprocess environments.
// These are needed by claude-code for API auth but should never
// be visible to commands Kit runs.
const SECRET_ENV_VARS = ['ANTHROPIC_API_KEY', 'CLAUDE_CODE_OAUTH_TOKEN'];
function createSanitizeBashHook(): HookCallback {
return async (input, _toolUseId, _context) => {
const preInput = input as PreToolUseHookInput;
const command = (preInput.tool_input as { command?: string })?.command;
if (!command) return {};
const unsetPrefix = `unset ${SECRET_ENV_VARS.join(' ')} 2>/dev/null; `;
return {
hookSpecificOutput: {
hookEventName: 'PreToolUse',
updatedInput: {
...(preInput.tool_input as Record<string, unknown>),
command: unsetPrefix + command,
},
},
};
};
}
function createReviewerBashGuardHook(): HookCallback {
return async (input, _toolUseId, _context) => {
const preInput = input as PreToolUseHookInput;
const command = (preInput.tool_input as { command?: string })?.command;
if (!command || !isReviewerMutatingShellCommand(command)) {
return {};
}
return {
hookSpecificOutput: {
hookEventName: 'PreToolUse',
permissionDecision: 'deny',
permissionDecisionReason:
'EJClaw reviewer runtime blocks mutating shell commands in paired review mode.',
},
};
};
}
function sanitizeFilename(summary: string): string {
return summary
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.slice(0, 50);
}
function generateFallbackName(): string {
const time = new Date();
return `conversation-${time.getHours().toString().padStart(2, '0')}${time.getMinutes().toString().padStart(2, '0')}`;
}
interface ParsedMessage {
role: 'user' | 'assistant';
content: string;
}
function parseTranscript(content: string): ParsedMessage[] {
const messages: ParsedMessage[] = [];
for (const line of content.split('\n')) {
if (!line.trim()) continue;
try {
const entry = JSON.parse(line);
if (entry.type === 'user' && entry.message?.content) {
const text =
typeof entry.message.content === 'string'
? entry.message.content
: entry.message.content
.map((c: { text?: string }) => c.text || '')
.join('');
if (text) messages.push({ role: 'user', content: text });
} else if (entry.type === 'assistant' && entry.message?.content) {
const textParts = entry.message.content
.filter((c: { type: string }) => c.type === 'text')
.map((c: { text: string }) => c.text);
const text = textParts.join('');
if (text) messages.push({ role: 'assistant', content: text });
}
} catch {}
}
return messages;
}
function formatTranscriptMarkdown(
messages: ParsedMessage[],
title?: string | null,
assistantName?: string,
): string {
const now = new Date();
const formatDateTime = (d: Date) =>
d.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true,
});
const lines: string[] = [];
lines.push(`# ${title || 'Conversation'}`);
lines.push('');
lines.push(`Archived: ${formatDateTime(now)}`);
lines.push('');
lines.push('---');
lines.push('');
for (const msg of messages) {
const sender = msg.role === 'user' ? 'User' : assistantName || 'Assistant';
const content =
msg.content.length > 2000
? msg.content.slice(0, 2000) + '...'
: msg.content;
lines.push(`**${sender}**: ${content}`);
lines.push('');
}
return lines.join('\n');
}
/**
* Check for _close sentinel.
*/
function shouldClose(): boolean {
if (fs.existsSync(IPC_INPUT_CLOSE_SENTINEL)) {
try {
fs.unlinkSync(IPC_INPUT_CLOSE_SENTINEL);
} catch {
/* ignore */
}
return true;
}
return false;
}
/**
* Drain all pending IPC input messages.
* Returns messages found, or empty array.
*/
function drainIpcInput(): string[] {
try {
fs.mkdirSync(IPC_INPUT_DIR, { recursive: true });
const files = fs
.readdirSync(IPC_INPUT_DIR)
.filter((f) => f.endsWith('.json'))
.sort();
const messages: string[] = [];
for (const file of files) {
const filePath = path.join(IPC_INPUT_DIR, file);
try {
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
fs.unlinkSync(filePath);
if (data.type === 'message' && data.text) {
messages.push(data.text);
}
} catch (err) {
log(
`Failed to process input file ${file}: ${err instanceof Error ? err.message : String(err)}`,
);
try {
fs.unlinkSync(filePath);
} catch {
/* ignore */
}
}
}
return messages;
} catch (err) {
log(`IPC drain error: ${err instanceof Error ? err.message : String(err)}`);
return [];
}
}
/**
* Run a single query and stream results via writeOutput.
* Uses MessageStream (AsyncIterable) to keep isSingleUserTurn=false,
@@ -605,7 +109,7 @@ async function runQuery(
closedDuringQuery: boolean;
terminalResultObserved: boolean;
}> {
const stream = new MessageStream();
const stream = new MessageStream((text) => buildMultimodalContent(text, log));
stream.push(prompt);
// Poll IPC for follow-up messages and _close sentinel during the query
@@ -613,7 +117,7 @@ async function runQuery(
let closedDuringQuery = false;
const pollIpcDuringQuery = () => {
if (!ipcPolling) return;
if (shouldClose()) {
if (shouldClose(IPC_INPUT_CLOSE_SENTINEL)) {
log('Close sentinel detected during query, ending stream');
// Flush any buffered text before closing — the for-await loop may not
// reach the post-loop flush code after stream.end().
@@ -635,7 +139,7 @@ async function runQuery(
ipcPolling = false;
return;
}
const messages = drainIpcInput();
const messages = drainIpcInput(IPC_INPUT_DIR, log);
for (const text of messages) {
log(`Piping IPC message into active query (${text.length} chars)`);
stream.push(text);
@@ -794,7 +298,18 @@ async function runQuery(
},
hooks: {
PreCompact: [
{ hooks: [createPreCompactHook(runnerInput.assistantName)] },
{
hooks: [
createPreCompactHook({
assistantName: runnerInput.assistantName,
groupDir: GROUP_DIR,
groupFolder: GROUP_FOLDER,
hostTasksDir: HOST_TASKS_DIR,
log,
writeOutput,
}),
],
},
],
PreToolUse: [
{
@@ -1163,7 +678,7 @@ async function main(): Promise<void> {
if (runnerInput.isScheduledTask) {
prompt = `[SCHEDULED TASK - The following message was sent automatically and is not coming directly from the user or group.]\n\n${prompt}`;
}
const pending = drainIpcInput();
const pending = drainIpcInput(IPC_INPUT_DIR, log);
if (pending.length > 0) {
log(`Draining ${pending.length} pending IPC messages into initial prompt`);
prompt += '\n' + pending.join('\n');
@@ -1202,7 +717,18 @@ async function main(): Promise<void> {
abortController: agentAbortController,
hooks: {
PreCompact: [
{ hooks: [createPreCompactHook(runnerInput.assistantName)] },
{
hooks: [
createPreCompactHook({
assistantName: runnerInput.assistantName,
groupDir: GROUP_DIR,
groupFolder: GROUP_FOLDER,
hostTasksDir: HOST_TASKS_DIR,
log,
writeOutput,
}),
],
},
],
},
},

View File

@@ -0,0 +1,52 @@
import fs from 'fs';
import path from 'path';
export function shouldClose(closeSentinelPath: string): boolean {
if (fs.existsSync(closeSentinelPath)) {
try {
fs.unlinkSync(closeSentinelPath);
} catch {
// Ignore cleanup errors on shutdown signal files.
}
return true;
}
return false;
}
export function drainIpcInput(
inputDir: string,
log: (message: string) => void,
): string[] {
try {
fs.mkdirSync(inputDir, { recursive: true });
const files = fs
.readdirSync(inputDir)
.filter((file) => file.endsWith('.json'))
.sort();
const messages: string[] = [];
for (const file of files) {
const filePath = path.join(inputDir, file);
try {
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
fs.unlinkSync(filePath);
if (data.type === 'message' && data.text) {
messages.push(data.text);
}
} catch (err) {
log(
`Failed to process input file ${file}: ${err instanceof Error ? err.message : String(err)}`,
);
try {
fs.unlinkSync(filePath);
} catch {
// Ignore best-effort cleanup failures.
}
}
}
return messages;
} catch (err) {
log(`IPC drain error: ${err instanceof Error ? err.message : String(err)}`);
return [];
}
}

View File

@@ -0,0 +1,174 @@
import fs from 'fs';
import path from 'path';
import {
extractImageTagPaths,
normalizePublicTextOutput,
type RunnerStructuredOutput,
writeProtocolOutput,
} from 'ejclaw-runners-shared';
export interface RunnerOutput {
status: 'success' | 'error';
phase?: 'progress' | 'final' | 'tool-activity' | 'intermediate';
agentId?: string;
agentLabel?: string;
agentDone?: boolean;
result: string | null;
output?: RunnerStructuredOutput;
newSessionId?: string;
error?: string;
}
type ContentBlock =
| { type: 'text'; text: string }
| {
type: 'image';
source: {
type: 'base64';
media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
data: string;
};
};
interface SDKUserMessage {
type: 'user';
message: { role: 'user'; content: string | ContentBlock[] };
parent_tool_use_id: null;
session_id: string;
}
interface AssistantContentBlock {
type?: string;
text?: string;
}
export type LogFn = (message: string) => void;
type StreamContent = string | ContentBlock[];
const MIME_TYPES: Record<string, string> = {
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.webp': 'image/webp',
};
export function buildMultimodalContent(
text: string,
log: LogFn,
): StreamContent {
const { cleanText, imagePaths } = extractImageTagPaths(text);
if (imagePaths.length === 0) return text;
const blocks: ContentBlock[] = [];
if (cleanText) {
blocks.push({ type: 'text', text: cleanText });
}
for (const imgPath of imagePaths) {
try {
if (!fs.existsSync(imgPath)) {
log(`Image not found, skipping: ${imgPath}`);
continue;
}
const data = fs.readFileSync(imgPath).toString('base64');
const ext = path.extname(imgPath).toLowerCase();
const mediaType = (MIME_TYPES[ext] || 'image/png') as
| 'image/jpeg'
| 'image/png'
| 'image/gif'
| 'image/webp';
blocks.push({
type: 'image',
source: { type: 'base64', media_type: mediaType, data },
});
log(`Added image block: ${imgPath} (${mediaType})`);
} catch (err) {
log(
`Failed to read image ${imgPath}: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
return blocks.length > 0 ? blocks : text;
}
export class MessageStream {
private queue: SDKUserMessage[] = [];
private waiting: (() => void) | null = null;
private done = false;
constructor(private readonly buildContent: (text: string) => StreamContent) {}
push(text: string): void {
const content = this.buildContent(text);
this.queue.push({
type: 'user',
message: { role: 'user', content },
parent_tool_use_id: null,
session_id: '',
});
this.waiting?.();
}
end(): void {
this.done = true;
this.waiting?.();
}
async *[Symbol.asyncIterator](): AsyncGenerator<SDKUserMessage> {
while (true) {
while (this.queue.length > 0) {
yield this.queue.shift()!;
}
if (this.done) return;
await new Promise<void>((resolve) => {
this.waiting = resolve;
});
this.waiting = null;
}
}
}
export async function readStdin(): Promise<string> {
return new Promise((resolve, reject) => {
let data = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (chunk) => {
data += chunk;
});
process.stdin.on('end', () => resolve(data));
process.stdin.on('error', reject);
});
}
export function writeOutput(output: RunnerOutput): void {
writeProtocolOutput(output);
}
export function normalizeStructuredOutput(result: string | null): {
result: string | null;
output?: RunnerOutput['output'];
} {
return normalizePublicTextOutput(result);
}
export function extractAssistantText(message: unknown): string | null {
const assistant = message as {
message?: {
content?: AssistantContentBlock[];
};
};
const blocks = assistant.message?.content;
if (!Array.isArray(blocks)) return null;
const text = blocks
.filter((block) => block?.type === 'text' && typeof block.text === 'string')
.map((block) => block.text!.trim())
.filter(Boolean)
.join('\n\n')
.trim();
return text || null;
}

View File

@@ -0,0 +1,304 @@
import fs from 'fs';
import path from 'path';
import {
HookCallback,
PreCompactHookInput,
PreToolUseHookInput,
} from '@anthropic-ai/claude-agent-sdk';
import { selectCompactMemoriesFromSummary } from './memory-selection.js';
import type { RunnerOutput } from './output-protocol.js';
import { isReviewerMutatingShellCommand } from './reviewer-runtime.js';
interface SessionEntry {
sessionId: string;
fullPath: string;
summary: string;
firstPrompt: string;
}
interface SessionsIndex {
entries: SessionEntry[];
}
interface ParsedMessage {
role: 'user' | 'assistant';
content: string;
}
interface PreCompactHookOptions {
assistantName?: string;
groupDir: string;
groupFolder: string;
hostTasksDir: string;
log: (message: string) => void;
writeOutput: (output: RunnerOutput) => void;
}
const SECRET_ENV_VARS = ['ANTHROPIC_API_KEY', 'CLAUDE_CODE_OAUTH_TOKEN'];
function getSessionSummary(
sessionId: string,
transcriptPath: string,
log: (message: string) => void,
): string | null {
const projectDir = path.dirname(transcriptPath);
const indexPath = path.join(projectDir, 'sessions-index.json');
if (!fs.existsSync(indexPath)) {
log(`Sessions index not found at ${indexPath}`);
return null;
}
try {
const index: SessionsIndex = JSON.parse(
fs.readFileSync(indexPath, 'utf-8'),
);
const entry = index.entries.find(
(current) => current.sessionId === sessionId,
);
if (entry?.summary) {
return entry.summary;
}
} catch (err) {
log(
`Failed to read sessions index: ${err instanceof Error ? err.message : String(err)}`,
);
}
return null;
}
function trimSummary(summary: string, maxChars: number): string {
if (summary.length <= maxChars) return summary;
return summary.slice(0, Math.max(0, maxChars - 1)).trimEnd() + '…';
}
function writeHostTaskIpcFile(hostTasksDir: string, data: object): string {
fs.mkdirSync(hostTasksDir, { recursive: true });
const filename = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}.json`;
const filepath = path.join(hostTasksDir, filename);
const tempPath = `${filepath}.tmp`;
fs.writeFileSync(tempPath, JSON.stringify(data, null, 2));
fs.renameSync(tempPath, filepath);
return filename;
}
async function persistCompactMemory(
summary: string,
sessionId: string,
options: Pick<PreCompactHookOptions, 'groupFolder' | 'hostTasksDir' | 'log'>,
): Promise<void> {
const normalized = summary.trim();
if (!normalized || !options.groupFolder) return;
try {
const scopeKey = `room:${options.groupFolder}`;
const selected = selectCompactMemoriesFromSummary(normalized, scopeKey);
if (selected.length === 0) {
options.log(
'Skipped compact memory persist - no salient room memory found',
);
return;
}
for (const memory of selected) {
const file = writeHostTaskIpcFile(options.hostTasksDir, {
type: 'persist_memory',
scopeKind: 'room',
scopeKey,
content: trimSummary(memory.content, 300),
keywords: memory.keywords,
memory_kind: memory.memoryKind,
source_kind: 'compact',
source_ref: sessionId ? `compact:${sessionId}` : null,
timestamp: new Date().toISOString(),
});
options.log(`Persisted compact memory via IPC (${file})`);
}
} catch (err) {
options.log(
`Failed to persist compact memory via IPC: ${
err instanceof Error ? err.message : String(err)
}`,
);
}
}
function sanitizeFilename(summary: string): string {
return summary
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.slice(0, 50);
}
function generateFallbackName(): string {
const time = new Date();
return `conversation-${time.getHours().toString().padStart(2, '0')}${time.getMinutes().toString().padStart(2, '0')}`;
}
function parseTranscript(content: string): ParsedMessage[] {
const messages: ParsedMessage[] = [];
for (const line of content.split('\n')) {
if (!line.trim()) continue;
try {
const entry = JSON.parse(line);
if (entry.type === 'user' && entry.message?.content) {
const text =
typeof entry.message.content === 'string'
? entry.message.content
: entry.message.content
.map((current: { text?: string }) => current.text || '')
.join('');
if (text) messages.push({ role: 'user', content: text });
} else if (entry.type === 'assistant' && entry.message?.content) {
const textParts = entry.message.content
.filter((current: { type: string }) => current.type === 'text')
.map((current: { text: string }) => current.text);
const text = textParts.join('');
if (text) messages.push({ role: 'assistant', content: text });
}
} catch {
// Ignore malformed transcript rows and keep best-effort archive behavior.
}
}
return messages;
}
function formatTranscriptMarkdown(
messages: ParsedMessage[],
summary: string | null,
assistantName?: string,
): string {
const now = new Date();
const formatDateTime = (date: Date) =>
date.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true,
});
const lines: string[] = [];
lines.push(`# ${summary || 'Conversation'}`);
lines.push('');
lines.push(`Archived: ${formatDateTime(now)}`);
lines.push('');
lines.push('---');
lines.push('');
for (const message of messages) {
const sender =
message.role === 'user' ? 'User' : assistantName || 'Assistant';
const content =
message.content.length > 2000
? message.content.slice(0, 2000) + '...'
: message.content;
lines.push(`**${sender}**: ${content}`);
lines.push('');
}
return lines.join('\n');
}
export function createPreCompactHook(
options: PreCompactHookOptions,
): HookCallback {
return async (input, _toolUseId, _context) => {
const preCompact = input as PreCompactHookInput;
const transcriptPath = preCompact.transcript_path;
const sessionId = preCompact.session_id;
const trigger = preCompact.trigger || 'auto';
options.writeOutput({
status: 'success',
phase: 'progress',
result: trigger === 'auto' ? '대화 요약 중...' : '컴팩트 중...',
});
if (!transcriptPath || !fs.existsSync(transcriptPath)) {
options.log('No transcript found for archiving');
return {};
}
try {
const content = fs.readFileSync(transcriptPath, 'utf-8');
const messages = parseTranscript(content);
if (messages.length === 0) {
options.log('No messages to archive');
return {};
}
const summary = getSessionSummary(sessionId, transcriptPath, options.log);
const name = summary ? sanitizeFilename(summary) : generateFallbackName();
const conversationsDir = path.join(options.groupDir, 'conversations');
fs.mkdirSync(conversationsDir, { recursive: true });
const date = new Date().toISOString().split('T')[0];
const filename = `${date}-${name}.md`;
const filePath = path.join(conversationsDir, filename);
const markdown = formatTranscriptMarkdown(
messages,
summary,
options.assistantName,
);
fs.writeFileSync(filePath, markdown);
options.log(`Archived conversation to ${filePath}`);
if (summary) {
await persistCompactMemory(summary, sessionId, options);
}
} catch (err) {
options.log(
`Failed to archive transcript: ${err instanceof Error ? err.message : String(err)}`,
);
}
return {};
};
}
export function createSanitizeBashHook(): HookCallback {
return async (input, _toolUseId, _context) => {
const preInput = input as PreToolUseHookInput;
const command = (preInput.tool_input as { command?: string })?.command;
if (!command) return {};
const unsetPrefix = `unset ${SECRET_ENV_VARS.join(' ')} 2>/dev/null; `;
return {
hookSpecificOutput: {
hookEventName: 'PreToolUse',
updatedInput: {
...(preInput.tool_input as Record<string, unknown>),
command: unsetPrefix + command,
},
},
};
};
}
export function createReviewerBashGuardHook(): HookCallback {
return async (input, _toolUseId, _context) => {
const preInput = input as PreToolUseHookInput;
const command = (preInput.tool_input as { command?: string })?.command;
if (!command || !isReviewerMutatingShellCommand(command)) {
return {};
}
return {
hookSpecificOutput: {
hookEventName: 'PreToolUse',
permissionDecision: 'deny',
permissionDecisionReason:
'EJClaw reviewer runtime blocks mutating shell commands in paired review mode.',
},
};
};
}