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
12 changes: 12 additions & 0 deletions .changeset/hook-stop-benign-session-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
id: hook-stop-benign-session-end
tasks: [T13890]
kind: fix
summary: cleo hook stop no longer warns when session end finds no session to end (exit 24 or 31)
---

A Stop with no bound session makes `cleo session end` exit with
`E_SESSION_UNBOUND` (24) or `E_SESSION_NOT_FOUND` (31). The Stop dispatcher
printed "session end failed … (exit 24)" for that expected case. Those two
exits are now benign (`SESSION_END_BENIGN_EXITS`). Every other non-zero exit
still prints one warning line.
19 changes: 19 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 { ExitCode } from '@cleocode/contracts/exit-codes.js';
import { GOAL_TICK_PROVIDER_ENV } from '@cleocode/contracts/goal-runtime.js';
import { describe, expect, it, vi } from 'vitest';
import {
Expand All @@ -19,6 +20,7 @@ import {
processStopDispatchDeps,
runBounded,
runTickKillable,
SESSION_END_BENIGN_EXITS,
type StopDispatchDeps,
stopDispatch,
type TickRun,
Expand Down Expand Up @@ -286,6 +288,23 @@ describe('stopDispatch (T13796)', () => {
const failed = vi.fn();
await runBounded(process.execPath, ['-e', 'process.exit(3)'], 5000, 'session end', failed);
expect(failed.mock.calls[0]?.[0]).toMatch(/session end failed after \d+ ms \(exit 3\)/);
// A Stop with no bound session: session end's no-session exits are benign.
const quiet = vi.fn();
for (const code of SESSION_END_BENIGN_EXITS) {
await runBounded(
process.execPath,
['-e', `process.exit(${code})`],
5000,
'session end',
quiet,
SESSION_END_BENIGN_EXITS,
);
}
expect(SESSION_END_BENIGN_EXITS).toEqual([
ExitCode.SESSION_UNBOUND,
ExitCode.SESSION_NOT_FOUND,
]);
expect(quiet).not.toHaveBeenCalled();
} finally {
const { execFileSync } = await import('node:child_process');
try {
Expand Down
15 changes: 14 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 } from 'node:child_process';
import { existsSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import type { HookJsonValue } from '@cleocode/contracts';
import { ExitCode } from '@cleocode/contracts/exit-codes.js';
import { GOAL_TICK_PROVIDER_ENV } from '@cleocode/contracts/goal-runtime.js';

/** Harnesses `cleo hook stop` dispatches for. */
Expand Down Expand Up @@ -292,12 +293,22 @@ export function processStopDispatchDeps(
SESSION_END_WAIT_MS,
'session end',
warn,
SESSION_END_BENIGN_EXITS,
);
},
warn,
};
}

/**
* `cleo session end` exits that only mean there was no session to end (a Stop
* with no bound session): expected, so they print no warning.
*/
export const SESSION_END_BENIGN_EXITS: readonly number[] = [
ExitCode.SESSION_UNBOUND,
ExitCode.SESSION_NOT_FOUND,
];

/**
* Run a detached process and wait for it at most `waitMs`. A non-zero exit or
* a spawn error gets one `warn` line with the duration. Past the wait the
Expand All @@ -308,6 +319,7 @@ export function processStopDispatchDeps(
* @param waitMs - Longest wait.
* @param label - Name used in the warning.
* @param warn - Receives the diagnostic line.
* @param benignExits - Non-zero exit codes that are expected and get no warning.
* @returns Resolves on exit or at the deadline, whichever comes first.
*/
export function runBounded(
Expand All @@ -316,6 +328,7 @@ export function runBounded(
waitMs: number,
label: string,
warn: (line: string) => void,
benignExits: readonly number[] = [],
): Promise<void> {
return new Promise((done) => {
const started = Date.now();
Expand All @@ -340,7 +353,7 @@ export function runBounded(
if (settled) return;
settled = true;
clearTimeout(timer);
if (code !== 0) {
if (code !== 0 && (code === null || !benignExits.includes(code))) {
warn(
`[cleo hook] ${label} failed after ${Date.now() - started} ms (${signal ?? `exit ${code}`})\n`,
);
Expand Down
Loading