fix: give the reviewer and arbiter their own paired-room rules
Codex sessions always received owner-common-paired-room.md ("You are the
owner", branch rules) whatever the role, so the Codex reviewer never saw the
reviewer rules and answered with PROCEED/REVISE (26x/8x in 30 days).
The Claude arbiter runs with CLAUDE_CONFIG_DIR pointed at a directory that is
wiped before every run, and its CLAUDE.md was only written on the unsafe-host
path. In normal mode it ran with no rules at all, so 20 of 23 arbiter verdicts
in 30 days were free-form text the verdict parser could not read.
Pick the paired-room prompt by role (owner / reviewer / arbiter) for Codex
AGENTS.md, and write the role CLAUDE.md into the reviewer/arbiter config dir
in normal mode as well. The readonly-session builder now shares that code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -67,6 +67,7 @@ vi.mock('./token-rotation.js', () => ({
|
||||
vi.mock('./platform-prompts.js', () => ({
|
||||
readPlatformPrompt: vi.fn(() => 'platform prompt'),
|
||||
readPairedRoomPrompt: vi.fn(() => 'paired room prompt'),
|
||||
readArbiterPrompt: vi.fn(() => 'arbiter prompt'),
|
||||
}));
|
||||
|
||||
vi.mock('./service-routing.js', () => ({
|
||||
@@ -115,6 +116,7 @@ vi.mock('os', async () => {
|
||||
import {
|
||||
prepareReadonlySessionEnvironment,
|
||||
prepareGroupEnvironment,
|
||||
writeReadonlyRoleClaudeMd,
|
||||
} from './agent-runner-environment.js';
|
||||
import * as config from './config.js';
|
||||
import * as serviceRouting from './service-routing.js';
|
||||
@@ -496,6 +498,42 @@ describe('prepareGroupEnvironment prompt stacks', () => {
|
||||
|
||||
expect(segments).toEqual(['platform prompt']);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['owner', 'owner common paired prompt'],
|
||||
['reviewer', 'paired room prompt'],
|
||||
['arbiter', 'arbiter prompt'],
|
||||
] as const)(
|
||||
'gives the Codex %s session its own paired-room rules',
|
||||
(roomRole, expectedRules) => {
|
||||
vi.mocked(config.isReviewService).mockReturnValue(false);
|
||||
vi.mocked(serviceRouting.hasReviewerLease).mockReturnValue(true);
|
||||
mockReadEnvFile.mockReturnValue({});
|
||||
const promptsDir = path.join(tempRoot, 'prompts');
|
||||
fs.mkdirSync(promptsDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(promptsDir, 'owner-common-paired-room.md'),
|
||||
'owner common paired prompt\n',
|
||||
);
|
||||
|
||||
prepareGroupEnvironment(group, false, 'dc:test', { roomRole });
|
||||
|
||||
const agents = fs.readFileSync(
|
||||
path.join(
|
||||
tempRoot,
|
||||
'sessions',
|
||||
group.folder,
|
||||
'services',
|
||||
'codex-main',
|
||||
'.codex',
|
||||
'AGENTS.md',
|
||||
),
|
||||
'utf-8',
|
||||
);
|
||||
const segments = agents.trim().split('\n\n---\n\n');
|
||||
expect(segments).toEqual(['platform prompt', expectedRules]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('prepareGroupEnvironment Codex MCP room role env', () => {
|
||||
@@ -948,3 +986,43 @@ args = ["other.js"]
|
||||
expect(toml.match(/\[mcp_servers\.ejclaw\.env\]/g)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('writeReadonlyRoleClaudeMd', () => {
|
||||
let tempRoot: string;
|
||||
let previousCwd: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempRoot = fs.mkdtempSync(path.join('/tmp', 'ejclaw-role-md-'));
|
||||
previousCwd = process.cwd();
|
||||
process.chdir(tempRoot);
|
||||
vi.mocked(serviceRouting.hasReviewerLease).mockReturnValue(true);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.chdir(previousCwd);
|
||||
fs.rmSync(tempRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it.each([
|
||||
['arbiter', 'arbiter prompt'],
|
||||
['reviewer', 'paired room prompt'],
|
||||
] as const)(
|
||||
'writes the %s rules into the role config dir (normal mode)',
|
||||
(role, expectedRules) => {
|
||||
const sessionDir = path.join(tempRoot, `room-${role}`);
|
||||
writeReadonlyRoleClaudeMd({
|
||||
sessionDir,
|
||||
chatJid: 'dc:test',
|
||||
isMain: false,
|
||||
role,
|
||||
memoryBriefing: 'memory briefing',
|
||||
});
|
||||
const md = fs.readFileSync(path.join(sessionDir, 'CLAUDE.md'), 'utf-8');
|
||||
const segments = md.trim().split('\n\n---\n\n');
|
||||
expect(segments[0]).toBe('platform prompt');
|
||||
expect(segments[1]).toBe(expectedRules);
|
||||
expect(segments).toContain('memory briefing');
|
||||
expect(md).not.toContain('owner common paired prompt');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -406,6 +406,94 @@ function prepareClaudeEnvironment(args: {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Paired-room rules for the role that is about to run. The owner gets the
|
||||
* owner rules; the reviewer and arbiter must get THEIR rules — handing them the
|
||||
* owner rules ("You are the owner") made the Codex reviewer answer with
|
||||
* arbiter/owner tokens, and an arbiter with no rules answered in free form
|
||||
* that the verdict parser could not read.
|
||||
*/
|
||||
function readPairedRolePrompt(
|
||||
projectRoot: string,
|
||||
role: PairedRoomRole | undefined,
|
||||
): string | undefined {
|
||||
if (role === 'reviewer') return readPairedRoomPrompt('claude-code', projectRoot);
|
||||
if (role === 'arbiter') return readArbiterPrompt(projectRoot);
|
||||
return readOptionalPromptFile(projectRoot, 'owner-common-paired-room.md');
|
||||
}
|
||||
|
||||
function buildReadonlyRoleClaudeMd(args: {
|
||||
projectRoot: string;
|
||||
chatJid: string;
|
||||
isMain: boolean;
|
||||
role: 'reviewer' | 'arbiter';
|
||||
memoryBriefing?: string;
|
||||
}): {
|
||||
content: string;
|
||||
hasPlatform: boolean;
|
||||
hasPairedRoom: boolean;
|
||||
hasGlobalMemory: boolean;
|
||||
} {
|
||||
const claudePlatformPrompt = readPlatformPrompt(
|
||||
'claude-code',
|
||||
args.projectRoot,
|
||||
);
|
||||
const claudePairedRoomPrompt = hasReviewerLease(args.chatJid)
|
||||
? readPairedRolePrompt(args.projectRoot, args.role)
|
||||
: undefined;
|
||||
const globalClaudeMdPath = path.join(GROUPS_DIR, 'global', 'CLAUDE.md');
|
||||
const globalClaudeMemory =
|
||||
!args.isMain && fs.existsSync(globalClaudeMdPath)
|
||||
? fs.readFileSync(globalClaudeMdPath, 'utf-8').trim()
|
||||
: undefined;
|
||||
const content = [
|
||||
claudePlatformPrompt,
|
||||
claudePairedRoomPrompt,
|
||||
globalClaudeMemory,
|
||||
args.memoryBriefing,
|
||||
]
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.join('\n\n---\n\n')
|
||||
.trim();
|
||||
return {
|
||||
content,
|
||||
hasPlatform: !!claudePlatformPrompt,
|
||||
hasPairedRoom: !!claudePairedRoomPrompt,
|
||||
hasGlobalMemory: !!globalClaudeMemory,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the reviewer/arbiter CLAUDE.md into the role's own Claude config dir.
|
||||
*
|
||||
* Reviewer and arbiter runs point CLAUDE_CONFIG_DIR at a per-role directory
|
||||
* (the arbiter's is wiped before every run), so the owner CLAUDE.md written by
|
||||
* prepareGroupEnvironment never reaches them. Only the unsafe-host path used to
|
||||
* fill that directory, which left the normal-mode arbiter with no rules at all.
|
||||
*/
|
||||
export function writeReadonlyRoleClaudeMd(args: {
|
||||
sessionDir: string;
|
||||
chatJid: string;
|
||||
isMain: boolean;
|
||||
role: 'reviewer' | 'arbiter';
|
||||
memoryBriefing?: string;
|
||||
}): void {
|
||||
const { content } = buildReadonlyRoleClaudeMd({
|
||||
projectRoot: process.cwd(),
|
||||
chatJid: args.chatJid,
|
||||
isMain: args.isMain,
|
||||
role: args.role,
|
||||
memoryBriefing: args.memoryBriefing,
|
||||
});
|
||||
fs.mkdirSync(args.sessionDir, { recursive: true });
|
||||
const target = path.join(args.sessionDir, 'CLAUDE.md');
|
||||
if (content) {
|
||||
fs.writeFileSync(target, content + '\n');
|
||||
} else if (fs.existsSync(target)) {
|
||||
fs.unlinkSync(target);
|
||||
}
|
||||
}
|
||||
|
||||
function prepareCodexSessionEnvironment(args: {
|
||||
env: Record<string, string>;
|
||||
envVars: Record<string, string>;
|
||||
@@ -488,10 +576,7 @@ function prepareCodexSessionEnvironment(args: {
|
||||
: [
|
||||
readPlatformPrompt('codex', args.projectRoot),
|
||||
args.isPairedRoom
|
||||
? readOptionalPromptFile(
|
||||
args.projectRoot,
|
||||
'owner-common-paired-room.md',
|
||||
)
|
||||
? readPairedRolePrompt(args.projectRoot, args.roomRole)
|
||||
: undefined,
|
||||
args.memoryBriefing,
|
||||
]
|
||||
@@ -827,28 +912,18 @@ export function prepareReadonlySessionEnvironment(args: {
|
||||
}
|
||||
|
||||
// Build CLAUDE.md with role-appropriate prompts (reviewer or arbiter)
|
||||
const claudePlatformPrompt = readPlatformPrompt('claude-code', projectRoot);
|
||||
const claudePairedRoomPrompt = hasReviewerLease(chatJid)
|
||||
? role === 'arbiter'
|
||||
? readArbiterPrompt(projectRoot)
|
||||
: readPairedRoomPrompt('claude-code', projectRoot)
|
||||
: undefined;
|
||||
const globalDir = path.join(GROUPS_DIR, 'global');
|
||||
const globalClaudeMdPath = path.join(globalDir, 'CLAUDE.md');
|
||||
const globalClaudeMemory =
|
||||
!isMain && fs.existsSync(globalClaudeMdPath)
|
||||
? fs.readFileSync(globalClaudeMdPath, 'utf-8').trim()
|
||||
: undefined;
|
||||
|
||||
const sessionClaudeMd = [
|
||||
claudePlatformPrompt,
|
||||
claudePairedRoomPrompt,
|
||||
globalClaudeMemory,
|
||||
const {
|
||||
content: sessionClaudeMd,
|
||||
hasPlatform,
|
||||
hasPairedRoom,
|
||||
hasGlobalMemory,
|
||||
} = buildReadonlyRoleClaudeMd({
|
||||
projectRoot,
|
||||
chatJid,
|
||||
isMain,
|
||||
role,
|
||||
memoryBriefing,
|
||||
]
|
||||
.filter((value): value is string => Boolean(value))
|
||||
.join('\n\n---\n\n')
|
||||
.trim();
|
||||
});
|
||||
|
||||
const sessionClaudeMdPath = path.join(sessionDir, 'CLAUDE.md');
|
||||
if (sessionClaudeMd) {
|
||||
@@ -892,9 +967,9 @@ export function prepareReadonlySessionEnvironment(args: {
|
||||
{
|
||||
sessionDir,
|
||||
claudeMdSize: sessionClaudeMd.length,
|
||||
hasPlatform: !!claudePlatformPrompt,
|
||||
hasPairedRoom: !!claudePairedRoomPrompt,
|
||||
hasGlobalMemory: !!globalClaudeMemory,
|
||||
hasPlatform,
|
||||
hasPairedRoom,
|
||||
hasGlobalMemory,
|
||||
hasMemoryBriefing: !!memoryBriefing,
|
||||
},
|
||||
'Readonly session CLAUDE.md written',
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
import {
|
||||
prepareReadonlySessionEnvironment,
|
||||
prepareGroupEnvironment,
|
||||
writeReadonlyRoleClaudeMd,
|
||||
type PreparedCodexSessionAuth,
|
||||
} from './agent-runner-environment.js';
|
||||
import {
|
||||
@@ -227,6 +228,21 @@ export async function runAgentProcess(
|
||||
env.HOME = readonlySession.codexHomeDir;
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
envOverrides?.CLAUDE_CONFIG_DIR &&
|
||||
(group.agentType || 'claude-code') === 'claude-code' &&
|
||||
(input.roomRoleContext?.role === 'reviewer' ||
|
||||
input.roomRoleContext?.role === 'arbiter')
|
||||
) {
|
||||
// Normal (safe) mode: the role runs with its own CLAUDE_CONFIG_DIR, so it
|
||||
// needs its own rules file there or it runs with no paired-room rules.
|
||||
writeReadonlyRoleClaudeMd({
|
||||
sessionDir: envOverrides.CLAUDE_CONFIG_DIR,
|
||||
chatJid: input.chatJid,
|
||||
isMain: input.isMain,
|
||||
role: input.roomRoleContext.role,
|
||||
memoryBriefing: input.memoryBriefing,
|
||||
});
|
||||
}
|
||||
if (input.runId) {
|
||||
env[EJCLAW_ENV.runId] = input.runId;
|
||||
|
||||
Reference in New Issue
Block a user