Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/goal-tick-provider-env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
id: goal-tick-provider-env
tasks: [T13884]
kind: feat
summary: the Stop dispatcher passes CLEO_HOOK_PROVIDER (claude-code or codex) to the goal tick
---

`cleo hook stop --provider <p>` now spawns the goal tick with
`CLEO_HOOK_PROVIDER=<p>` added to the inherited environment, so `cleo-goal tick`
can tell which harness's Stop it is answering. The name is exported as
`GOAL_TICK_PROVIDER_ENV` from `@cleocode/contracts/goal-runtime.js`.
10 changes: 10 additions & 0 deletions packages/cleo/src/cli/__tests__/stop-dispatch-entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @task T13796
*/

import { GOAL_TICK_PROVIDER_ENV } from '@cleocode/contracts/goal-runtime.js';
import { describe, expect, it, vi } from 'vitest';
import {
askEnforceOptedOut,
Expand Down Expand Up @@ -216,6 +217,15 @@ describe('stopDispatch (T13796)', () => {
expect(r.stdout).not.toContain('pwned');
});

it('the tick child sees CLEO_HOOK_PROVIDER set to the dispatching harness (T13884)', async () => {
const echo = `node -e "process.stdout.write(JSON.stringify({p: process.env.${GOAL_TICK_PROVIDER_ENV}, h: process.env.HOME ?? null}))"`;
for (const provider of ['claude-code', 'codex'] as const) {
const run = await runTickKillable(echo, STOP, 5000, provider);
expect(run.status).toBe(0);
expect(JSON.parse(run.stdout)).toEqual({ p: provider, h: process.env.HOME ?? null });
}
});

it('a tick that traps SIGTERM is SIGKILLed with its group at the deadline, and fails open', async () => {
const marker = `cleo-t13796-${process.pid}-${Date.now()}`;
const trap = `node -e "process.on('SIGTERM',()=>{});setInterval(()=>{},1000)" ${marker}-trap`;
Expand Down
9 changes: 8 additions & 1 deletion packages/cleo/src/cli/stop-dispatch-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { spawn, spawnSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import type { HookJsonValue } from '@cleocode/contracts';
import { GOAL_TICK_PROVIDER_ENV } from '@cleocode/contracts/goal-runtime.js';

/** Harnesses `cleo hook stop` dispatches for. */
export const STOP_DISPATCH_PROVIDERS = ['claude-code', 'codex'] as const;
Expand Down Expand Up @@ -231,7 +232,7 @@ export function processStopDispatchDeps(
return goalTickCommand({ ...process.env, ...env });
},
hasGoalWorkspace: (input) => hasGoalWorkspace(input, env),
runTick: runTickKillable,
runTick: (command, input, timeoutMs) => runTickKillable(command, input, timeoutMs, provider),
endSession: () => {
const cli = process.argv[1];
spawnSync(
Expand Down Expand Up @@ -285,12 +286,15 @@ export function hasGoalWorkspace(
* @param command - The tick command line.
* @param input - The Stop JSON for its stdin.
* @param timeoutMs - Deadline.
* @param provider - The harness whose Stop this is, passed to the tick as
* {@link GOAL_TICK_PROVIDER_ENV}; the rest of the environment is inherited.
* @returns What the tick did.
*/
export function runTickKillable(
command: string,
input: string,
timeoutMs: number,
provider?: StopDispatchProvider,
): Promise<TickRun> {
return new Promise((done) => {
let stdout = '';
Expand All @@ -305,6 +309,9 @@ export function runTickKillable(
shell: true,
detached: true,
stdio: ['pipe', 'pipe', 'ignore'],
...(provider === undefined
? {}
: { env: { ...process.env, [GOAL_TICK_PROVIDER_ENV]: provider } }),
});
const timer = setTimeout(() => {
try {
Expand Down
7 changes: 7 additions & 0 deletions packages/contracts/src/goal-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
* @epic T13791
*/

/**
* Environment variable naming the harness whose Stop the dispatcher is
* answering (`claude-code` or `codex`), set on the `cleo-goal tick` child so
* the tick can tell harnesses apart (T13884).
*/
export const GOAL_TICK_PROVIDER_ENV = 'CLEO_HOOK_PROVIDER';

/** The only `schemaVersion` this CLEO understands for every shape below. */
export const GOAL_RUNTIME_SCHEMA_VERSION = 1;

Expand Down
Loading