harness-reflex 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +103 -23
- package/USER_GUIDE.md +121 -43
- package/dist/adapters/agy.js +1 -1
- package/dist/adapters/agy.js.map +1 -1
- package/dist/adapters/codex.d.ts +133 -4
- package/dist/adapters/codex.js +325 -2
- package/dist/adapters/codex.js.map +1 -1
- package/dist/backend/defaults.d.ts +31 -0
- package/dist/backend/defaults.js +32 -0
- package/dist/backend/defaults.js.map +1 -0
- package/dist/backend/jev.js +6 -5
- package/dist/backend/jev.js.map +1 -1
- package/dist/backend/mock.d.ts +12 -1
- package/dist/backend/mock.js +34 -0
- package/dist/backend/mock.js.map +1 -1
- package/dist/backend/simple-jev.js +5 -4
- package/dist/backend/simple-jev.js.map +1 -1
- package/dist/backend/types.d.ts +34 -0
- package/dist/backend/types.js.map +1 -1
- package/dist/cli/index.js +5 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/core/closing.d.ts +90 -0
- package/dist/core/closing.js +111 -0
- package/dist/core/closing.js.map +1 -0
- package/dist/core/evidence.d.ts +119 -0
- package/dist/core/evidence.js +209 -0
- package/dist/core/evidence.js.map +1 -0
- package/dist/core/judge.d.ts +221 -0
- package/dist/core/judge.js +251 -0
- package/dist/core/judge.js.map +1 -0
- package/dist/core/orchestrate-closing.d.ts +237 -0
- package/dist/core/orchestrate-closing.js +250 -0
- package/dist/core/orchestrate-closing.js.map +1 -0
- package/dist/core/orchestrate-correction.d.ts +209 -0
- package/dist/core/orchestrate-correction.js +141 -0
- package/dist/core/orchestrate-correction.js.map +1 -0
- package/dist/core/orchestrate-host.d.ts +225 -0
- package/dist/core/orchestrate-host.js +80 -0
- package/dist/core/orchestrate-host.js.map +1 -0
- package/dist/core/orchestrate-stagnation.d.ts +259 -0
- package/dist/core/orchestrate-stagnation.js +270 -0
- package/dist/core/orchestrate-stagnation.js.map +1 -0
- package/dist/core/orchestrate.d.ts +357 -0
- package/dist/core/orchestrate.js +418 -0
- package/dist/core/orchestrate.js.map +1 -0
- package/dist/core/policy.d.ts +122 -0
- package/dist/core/policy.js +102 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/prune-plan.d.ts +141 -0
- package/dist/core/prune-plan.js +390 -0
- package/dist/core/prune-plan.js.map +1 -0
- package/dist/core/score.d.ts +2 -2
- package/dist/core/score.js.map +1 -1
- package/dist/core/text.d.ts +36 -0
- package/dist/core/text.js +66 -0
- package/dist/core/text.js.map +1 -0
- package/dist/hook/cli.d.ts +9 -0
- package/dist/hook/cli.js +52 -0
- package/dist/hook/cli.js.map +1 -0
- package/dist/hook/contract.d.ts +125 -0
- package/dist/hook/contract.js +159 -0
- package/dist/hook/contract.js.map +1 -0
- package/dist/hook/decision.d.ts +97 -0
- package/dist/hook/decision.js +955 -0
- package/dist/hook/decision.js.map +1 -0
- package/dist/hook/memory-store.d.ts +81 -0
- package/dist/hook/memory-store.js +157 -0
- package/dist/hook/memory-store.js.map +1 -0
- package/dist/hook/payload.d.ts +55 -0
- package/dist/hook/payload.js +129 -0
- package/dist/hook/payload.js.map +1 -0
- package/dist/hook/run.d.ts +48 -0
- package/dist/hook/run.js +218 -0
- package/dist/hook/run.js.map +1 -0
- package/dist/hook/store-port.d.ts +72 -0
- package/dist/hook/store-port.js +2 -0
- package/dist/hook/store-port.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/ports/audit.d.ts +18 -0
- package/dist/ports/audit.js +11 -0
- package/dist/ports/audit.js.map +1 -0
- package/dist/ports/clock.d.ts +14 -0
- package/dist/ports/clock.js +11 -0
- package/dist/ports/clock.js.map +1 -0
- package/dist/ports/decision.d.ts +75 -0
- package/dist/ports/decision.js +2 -0
- package/dist/ports/decision.js.map +1 -0
- package/dist/ports/index.d.ts +14 -0
- package/dist/ports/index.js +2 -0
- package/dist/ports/index.js.map +1 -0
- package/dist/ports/log.d.ts +22 -0
- package/dist/ports/log.js +12 -0
- package/dist/ports/log.js.map +1 -0
- package/dist/ports/state.d.ts +46 -0
- package/dist/ports/state.js +13 -0
- package/dist/ports/state.js.map +1 -0
- package/dist/state/clock.d.ts +40 -0
- package/dist/state/clock.js +43 -0
- package/dist/state/clock.js.map +1 -0
- package/dist/state/file-store.d.ts +340 -0
- package/dist/state/file-store.js +849 -0
- package/dist/state/file-store.js.map +1 -0
- package/dist/state/paths.d.ts +41 -0
- package/dist/state/paths.js +73 -0
- package/dist/state/paths.js.map +1 -0
- package/docs/USER_GUIDE.en.md +79 -36
- package/docs/claude-code.md +281 -0
- package/docs/design/v0.2.0-harness-plugins.md +381 -0
- package/docs/design/v0.3.0-orchestration-unification.md +580 -0
- package/docs/dsh-plugin.md +54 -54
- package/docs/release-checklist-v0.1.0.md +32 -21
- package/docs/release-handbook-porting-notes.md +11 -11
- package/docs/release-handbook.md +55 -30
- package/docs/release-notes-v0.1.0.md +27 -14
- package/docs/release-notes-v0.3.0.md +152 -0
- package/docs/research/claude-code-harness.md +607 -0
- package/docs/research/pi-harness.md +621 -0
- package/package.json +16 -7
package/dist/adapters/codex.d.ts
CHANGED
|
@@ -1,10 +1,139 @@
|
|
|
1
|
+
import { type CodexOnlyHookEvent, type HookEvent } from '../hook/contract.js';
|
|
2
|
+
/** One Codex hook event, as the binary spells it in all three notations. */
|
|
3
|
+
export interface CodexHookEventSpec {
|
|
4
|
+
/** The `hook_event_name` value Codex puts in the payload and reads back. */
|
|
5
|
+
readonly pascal: string;
|
|
6
|
+
/** The kebab-case subcommand this runtime accepts for the event. */
|
|
7
|
+
readonly kebab: string;
|
|
8
|
+
/** The `harness-reflex hook <subcommand>` token for the event. */
|
|
9
|
+
readonly subcommand: string;
|
|
10
|
+
/**
|
|
11
|
+
* The canonical event this maps to, or `null` when the runtime has no
|
|
12
|
+
* capability for it and must answer `{}`.
|
|
13
|
+
*/
|
|
14
|
+
readonly canonical: HookEvent | null;
|
|
15
|
+
/**
|
|
16
|
+
* The Codex-only event, when {@link canonical} is `null` and the event is
|
|
17
|
+
* still recognized. `null` on both means a genuinely unsupported event.
|
|
18
|
+
*/
|
|
19
|
+
readonly codexOnly: CodexOnlyHookEvent | null;
|
|
20
|
+
/** Handler timeout in seconds, kept inside both budgets. */
|
|
21
|
+
readonly timeoutSec: number;
|
|
22
|
+
}
|
|
23
|
+
/** The SessionEnd timeout Codex clamps to, discovered from its own warning. */
|
|
24
|
+
export declare const CODEX_SESSION_END_TIMEOUT_SEC = 3;
|
|
25
|
+
/**
|
|
26
|
+
* The twelve Codex hook events and what this runtime does with each.
|
|
27
|
+
*
|
|
28
|
+
* Order follows the binary so a diff against `command_runner.rs` is readable.
|
|
29
|
+
*/
|
|
30
|
+
export declare const CODEX_HOOK_EVENTS: readonly CodexHookEventSpec[];
|
|
31
|
+
/**
|
|
32
|
+
* Resolve a Codex event from any spelling: PascalCase, kebab-case, snake_case,
|
|
33
|
+
* the hook subcommand, or the canonical name.
|
|
34
|
+
* @param raw - the event name to resolve.
|
|
35
|
+
* @returns the matching spec, or `undefined` for an unsupported name.
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseCodexHookEvent(raw: string | undefined): CodexHookEventSpec | undefined;
|
|
38
|
+
/** The spec for one canonical event, for tests and for the reverse mapping. */
|
|
39
|
+
export declare function codexSpecForCanonical(event: HookEvent): CodexHookEventSpec | undefined;
|
|
40
|
+
/** Every Codex-only event the runtime recognizes but cannot decide. */
|
|
41
|
+
export declare function codexOnlyEvents(): readonly CodexOnlyHookEvent[];
|
|
42
|
+
/**
|
|
43
|
+
* Render the whole command Codex should run for one event.
|
|
44
|
+
*
|
|
45
|
+
* Codex does not forward handler `args`, so the event is part of the command
|
|
46
|
+
* string. Two shapes are supported and distinguished by the entry itself:
|
|
47
|
+
*
|
|
48
|
+
* - a **script path** (`/x/dist/cli/index.js`, `./cli.js`) runs under this Node
|
|
49
|
+
* binary, because Codex spawns the command without knowing it is JS;
|
|
50
|
+
* - a **command name** (`harness-reflex`, the installed bin) runs as-is.
|
|
51
|
+
*
|
|
52
|
+
* Anything that looks like a path is quoted, so a directory with spaces works.
|
|
53
|
+
* @param entry - an absolute script path or an installed command name.
|
|
54
|
+
* @param spec - the event to wire.
|
|
55
|
+
* @returns the command string for `hooks.json`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function renderCodexHookCommand(entry: string, spec: CodexHookEventSpec): string;
|
|
58
|
+
/** One matcher group of a Codex `hooks.json` event. */
|
|
59
|
+
export interface CodexHookMatcherGroup {
|
|
60
|
+
matcher?: string;
|
|
61
|
+
hooks: {
|
|
62
|
+
type: 'command';
|
|
63
|
+
command: string;
|
|
64
|
+
timeout: number;
|
|
65
|
+
}[];
|
|
66
|
+
}
|
|
67
|
+
/** The `hooks.json` document Codex loads from `$CODEX_HOME/hooks.json`. */
|
|
68
|
+
export interface CodexHooksConfig {
|
|
69
|
+
hooks: Record<string, CodexHookMatcherGroup[]>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Build the `hooks.json` document for this runtime.
|
|
73
|
+
*
|
|
74
|
+
* `PreToolUse` and `PostToolUse` get a `matcher` that covers both the built-in
|
|
75
|
+
* shell tools and any MCP tool, because the probe showed `matcher: ".*"` is
|
|
76
|
+
* honored and a matcher that only names MCP tools would miss every shell call.
|
|
77
|
+
* @param entryScript - absolute path to `dist/cli/index.js`.
|
|
78
|
+
* @param options - `events` narrows the wired set; `matcher` overrides the tool
|
|
79
|
+
* matcher for the tool-scoped events.
|
|
80
|
+
* @returns the document, ready to serialize.
|
|
81
|
+
*/
|
|
82
|
+
export declare function buildCodexHooksConfig(entryScript: string, options?: {
|
|
83
|
+
events?: readonly CodexHookEventSpec[];
|
|
84
|
+
matcher?: string;
|
|
85
|
+
}): CodexHooksConfig;
|
|
86
|
+
/**
|
|
87
|
+
* Build the `config.toml` fragment that registers the MCP server.
|
|
88
|
+
*
|
|
89
|
+
* Codex reads MCP servers from `config.toml` (`[mcp_servers.<name>]`), which
|
|
90
|
+
* `codex mcp list --json` reports back and which the probe confirmed. The
|
|
91
|
+
* `.codex/config.json` file this repository wrote before that is kept for
|
|
92
|
+
* backwards compatibility but is not what Codex reads.
|
|
93
|
+
* @param entryScript - absolute path to `dist/cli/index.js`.
|
|
94
|
+
* @param options - `name` overrides the server name; `env` overrides the
|
|
95
|
+
* environment table (empty values are dropped, an empty object emits no table).
|
|
96
|
+
* @returns the TOML fragment, without the surrounding config.
|
|
97
|
+
*/
|
|
98
|
+
export declare function renderCodexMcpToml(entryScript: string, options?: {
|
|
99
|
+
name?: string;
|
|
100
|
+
env?: Record<string, string>;
|
|
101
|
+
}): string;
|
|
102
|
+
/** What one Codex configuration run wrote. */
|
|
103
|
+
export interface CodexSetupResult {
|
|
104
|
+
/** `.codex/config.json` — the legacy MCP block, kept for compatibility. */
|
|
105
|
+
configPath: string;
|
|
106
|
+
/** `.codex/hooks.json` — the hook wiring Codex actually loads. */
|
|
107
|
+
hooksPath: string;
|
|
108
|
+
/** `.codex/config.toml` — the MCP block Codex actually reads. */
|
|
109
|
+
tomlPath: string;
|
|
110
|
+
created: boolean;
|
|
111
|
+
}
|
|
1
112
|
export declare class CodexAdapter {
|
|
2
113
|
/**
|
|
3
|
-
* Generates or updates Codex CLI configuration
|
|
114
|
+
* Generates or updates Codex CLI configuration: MCP (both files) and hooks.
|
|
115
|
+
*
|
|
116
|
+
* Codex reads MCP servers from `config.toml`, not `config.json` — the latter
|
|
117
|
+
* is what this repository wrote before and is kept so existing setups keep
|
|
118
|
+
* working. Hooks come from `$CODEX_HOME/hooks.json`, so the same directory is
|
|
119
|
+
* written here; a user who points `CODEX_HOME` at `<workspace>/.codex` gets a
|
|
120
|
+
* complete setup.
|
|
121
|
+
* @param workspaceDir - the directory that holds `.codex/`.
|
|
122
|
+
* @param binaryPath - absolute path to `dist/cli/index.js`.
|
|
123
|
+
*/
|
|
124
|
+
static configureCodex(workspaceDir: string, binaryPath: string): CodexSetupResult;
|
|
125
|
+
/**
|
|
126
|
+
* Write only the hook wiring, leaving any MCP configuration alone.
|
|
127
|
+
* @param workspaceDir - the directory that holds `.codex/`.
|
|
128
|
+
* @param binaryPath - absolute path to `dist/cli/index.js`.
|
|
4
129
|
*/
|
|
5
|
-
static
|
|
6
|
-
|
|
7
|
-
created: boolean;
|
|
130
|
+
static configureCodexHooks(workspaceDir: string, binaryPath: string): {
|
|
131
|
+
hooksPath: string;
|
|
8
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* The `hooks.json` document for every Codex event.
|
|
135
|
+
* @param binaryPath - absolute path to `dist/cli/index.js`.
|
|
136
|
+
*/
|
|
137
|
+
static buildHooksConfig(binaryPath: string): CodexHooksConfig;
|
|
9
138
|
static getCodexInstructions(): string;
|
|
10
139
|
}
|
package/dist/adapters/codex.js
CHANGED
|
@@ -1,8 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Codex adapter: event table, `hooks.json`, `config.toml`, and the setup entry
|
|
3
|
+
* point.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is grounded in codex-cli 0.163.0 rather than in its
|
|
6
|
+
* documentation, because the documentation does not cover hooks yet. The event
|
|
7
|
+
* table was read out of the binary (`hooks/src/engine/command_runner.rs`) and
|
|
8
|
+
* then confirmed by running a real `codex exec` against a probe hook:
|
|
9
|
+
*
|
|
10
|
+
* - **Twelve events**, not ten. `Stop` and `Interrupt` are in the binary's
|
|
11
|
+
* normalized list and `Stop` fires on every completed turn.
|
|
12
|
+
* - **`args` is not passed to the hook process.** A handler declared as
|
|
13
|
+
* `{command: "/path/hook.sh", args: ["session-start"]}` runs with `argv = []`.
|
|
14
|
+
* The event therefore has to ride in the command string itself.
|
|
15
|
+
* - **Codex clamps the SessionEnd timeout to 3 s** and warns when `hooks.json`
|
|
16
|
+
* asks for more, so the table stays under 3.
|
|
17
|
+
* - **The event name is in the payload** (`hook_event_name`, PascalCase), which
|
|
18
|
+
* is what the probe records and what the normalizer trusts over `argv`.
|
|
19
|
+
*
|
|
20
|
+
* The payload normalizer itself lives in `hook/payload` because it describes the
|
|
21
|
+
* stdin shape of every host; an adapter that imported another adapter would
|
|
22
|
+
* break the dependency fence this repository tests.
|
|
23
|
+
*
|
|
24
|
+
* @module adapters/codex
|
|
25
|
+
*/
|
|
1
26
|
import * as fs from 'fs';
|
|
2
27
|
import * as path from 'path';
|
|
28
|
+
import { CODEX_ONLY_HOOK_EVENTS, EVENT_BUDGETS } from '../hook/contract.js';
|
|
29
|
+
/** The SessionEnd timeout Codex clamps to, discovered from its own warning. */
|
|
30
|
+
export const CODEX_SESSION_END_TIMEOUT_SEC = 3;
|
|
31
|
+
/**
|
|
32
|
+
* Convert a host timeout in milliseconds to the whole seconds `hooks.json` takes.
|
|
33
|
+
*
|
|
34
|
+
* Rounded **up**: the host timeout must never be shorter than the internal
|
|
35
|
+
* decision budget, or a decision that would have finished is killed by the host
|
|
36
|
+
* and the event silently fails open.
|
|
37
|
+
*/
|
|
38
|
+
function toHostSeconds(ms) {
|
|
39
|
+
return Math.max(1, Math.ceil(ms / 1000));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The twelve Codex hook events and what this runtime does with each.
|
|
43
|
+
*
|
|
44
|
+
* Order follows the binary so a diff against `command_runner.rs` is readable.
|
|
45
|
+
*/
|
|
46
|
+
export const CODEX_HOOK_EVENTS = [
|
|
47
|
+
{
|
|
48
|
+
pascal: 'PreToolUse',
|
|
49
|
+
kebab: 'pre-tool-use',
|
|
50
|
+
subcommand: 'pretooluse',
|
|
51
|
+
canonical: 'pretooluse',
|
|
52
|
+
codexOnly: null,
|
|
53
|
+
timeoutSec: toHostSeconds(EVENT_BUDGETS.pretooluse.hookTimeoutMs)
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
pascal: 'PermissionRequest',
|
|
57
|
+
kebab: 'permission-request',
|
|
58
|
+
subcommand: 'permissionrequest',
|
|
59
|
+
canonical: null,
|
|
60
|
+
codexOnly: 'permissionrequest',
|
|
61
|
+
timeoutSec: 1
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
pascal: 'PostToolUse',
|
|
65
|
+
kebab: 'post-tool-use',
|
|
66
|
+
subcommand: 'posttooluse',
|
|
67
|
+
canonical: 'posttooluse',
|
|
68
|
+
codexOnly: null,
|
|
69
|
+
timeoutSec: toHostSeconds(EVENT_BUDGETS.posttooluse.hookTimeoutMs)
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
pascal: 'PreCompact',
|
|
73
|
+
kebab: 'pre-compact',
|
|
74
|
+
subcommand: 'precompact',
|
|
75
|
+
canonical: null,
|
|
76
|
+
codexOnly: 'precompact',
|
|
77
|
+
timeoutSec: 1
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
pascal: 'PostCompact',
|
|
81
|
+
kebab: 'post-compact',
|
|
82
|
+
subcommand: 'postcompact',
|
|
83
|
+
canonical: null,
|
|
84
|
+
codexOnly: 'postcompact',
|
|
85
|
+
timeoutSec: 1
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
pascal: 'SessionStart',
|
|
89
|
+
kebab: 'session-start',
|
|
90
|
+
subcommand: 'sessionstart',
|
|
91
|
+
canonical: 'sessionstart',
|
|
92
|
+
codexOnly: null,
|
|
93
|
+
timeoutSec: toHostSeconds(EVENT_BUDGETS.sessionstart.hookTimeoutMs)
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
pascal: 'SessionEnd',
|
|
97
|
+
kebab: 'session-end',
|
|
98
|
+
subcommand: 'sessionend',
|
|
99
|
+
canonical: 'sessionend',
|
|
100
|
+
codexOnly: null,
|
|
101
|
+
// Codex clamps this one to 3 s and warns, so ask for 3 and stay quiet.
|
|
102
|
+
timeoutSec: Math.min(CODEX_SESSION_END_TIMEOUT_SEC, toHostSeconds(EVENT_BUDGETS.sessionend.hookTimeoutMs))
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
pascal: 'UserPromptSubmit',
|
|
106
|
+
kebab: 'user-prompt-submit',
|
|
107
|
+
subcommand: 'userpromptsubmit',
|
|
108
|
+
canonical: null,
|
|
109
|
+
codexOnly: 'userpromptsubmit',
|
|
110
|
+
timeoutSec: 1
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
pascal: 'SubagentStart',
|
|
114
|
+
kebab: 'subagent-start',
|
|
115
|
+
subcommand: 'subagentstart',
|
|
116
|
+
canonical: null,
|
|
117
|
+
codexOnly: 'subagentstart',
|
|
118
|
+
timeoutSec: 1
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
pascal: 'SubagentStop',
|
|
122
|
+
kebab: 'subagent-stop',
|
|
123
|
+
subcommand: 'substop',
|
|
124
|
+
canonical: 'substop',
|
|
125
|
+
codexOnly: null,
|
|
126
|
+
timeoutSec: toHostSeconds(EVENT_BUDGETS.substop.hookTimeoutMs)
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
pascal: 'Stop',
|
|
130
|
+
kebab: 'stop',
|
|
131
|
+
subcommand: 'stop',
|
|
132
|
+
canonical: 'stop',
|
|
133
|
+
codexOnly: null,
|
|
134
|
+
timeoutSec: toHostSeconds(EVENT_BUDGETS.stop.hookTimeoutMs)
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
pascal: 'Interrupt',
|
|
138
|
+
kebab: 'interrupt',
|
|
139
|
+
subcommand: 'interrupt',
|
|
140
|
+
canonical: null,
|
|
141
|
+
codexOnly: 'interrupt',
|
|
142
|
+
timeoutSec: 1
|
|
143
|
+
}
|
|
144
|
+
];
|
|
145
|
+
/** Normalize any spelling of a Codex event to its spec. */
|
|
146
|
+
function normalizeName(raw) {
|
|
147
|
+
return raw.trim().toLowerCase().replace(/[-_\s]/g, '');
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Resolve a Codex event from any spelling: PascalCase, kebab-case, snake_case,
|
|
151
|
+
* the hook subcommand, or the canonical name.
|
|
152
|
+
* @param raw - the event name to resolve.
|
|
153
|
+
* @returns the matching spec, or `undefined` for an unsupported name.
|
|
154
|
+
*/
|
|
155
|
+
export function parseCodexHookEvent(raw) {
|
|
156
|
+
if (!raw)
|
|
157
|
+
return undefined;
|
|
158
|
+
const wanted = normalizeName(raw);
|
|
159
|
+
for (const spec of CODEX_HOOK_EVENTS) {
|
|
160
|
+
if (normalizeName(spec.pascal) === wanted ||
|
|
161
|
+
normalizeName(spec.kebab) === wanted ||
|
|
162
|
+
normalizeName(spec.subcommand) === wanted) {
|
|
163
|
+
return spec;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
/** The spec for one canonical event, for tests and for the reverse mapping. */
|
|
169
|
+
export function codexSpecForCanonical(event) {
|
|
170
|
+
return CODEX_HOOK_EVENTS.find((spec) => spec.canonical === event);
|
|
171
|
+
}
|
|
172
|
+
/** Every Codex-only event the runtime recognizes but cannot decide. */
|
|
173
|
+
export function codexOnlyEvents() {
|
|
174
|
+
return CODEX_ONLY_HOOK_EVENTS;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Render the whole command Codex should run for one event.
|
|
178
|
+
*
|
|
179
|
+
* Codex does not forward handler `args`, so the event is part of the command
|
|
180
|
+
* string. Two shapes are supported and distinguished by the entry itself:
|
|
181
|
+
*
|
|
182
|
+
* - a **script path** (`/x/dist/cli/index.js`, `./cli.js`) runs under this Node
|
|
183
|
+
* binary, because Codex spawns the command without knowing it is JS;
|
|
184
|
+
* - a **command name** (`harness-reflex`, the installed bin) runs as-is.
|
|
185
|
+
*
|
|
186
|
+
* Anything that looks like a path is quoted, so a directory with spaces works.
|
|
187
|
+
* @param entry - an absolute script path or an installed command name.
|
|
188
|
+
* @param spec - the event to wire.
|
|
189
|
+
* @returns the command string for `hooks.json`.
|
|
190
|
+
*/
|
|
191
|
+
export function renderCodexHookCommand(entry, spec) {
|
|
192
|
+
const looksLikeScript = entry.includes('/') || entry.includes('\\') || /\.(?:js|mjs|cjs)$/.test(entry);
|
|
193
|
+
const program = looksLikeScript ? `node ${quote(entry)}` : entry;
|
|
194
|
+
return `${program} hook ${spec.subcommand}`;
|
|
195
|
+
}
|
|
196
|
+
/** Quote one shell word when it contains anything a shell would split on. */
|
|
197
|
+
function quote(word) {
|
|
198
|
+
return /^[A-Za-z0-9_./:@%+-]+$/.test(word) ? word : `"${word.replace(/(["\\$`])/g, '\\$1')}"`;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Build the `hooks.json` document for this runtime.
|
|
202
|
+
*
|
|
203
|
+
* `PreToolUse` and `PostToolUse` get a `matcher` that covers both the built-in
|
|
204
|
+
* shell tools and any MCP tool, because the probe showed `matcher: ".*"` is
|
|
205
|
+
* honored and a matcher that only names MCP tools would miss every shell call.
|
|
206
|
+
* @param entryScript - absolute path to `dist/cli/index.js`.
|
|
207
|
+
* @param options - `events` narrows the wired set; `matcher` overrides the tool
|
|
208
|
+
* matcher for the tool-scoped events.
|
|
209
|
+
* @returns the document, ready to serialize.
|
|
210
|
+
*/
|
|
211
|
+
export function buildCodexHooksConfig(entryScript, options = {}) {
|
|
212
|
+
const events = options.events ?? CODEX_HOOK_EVENTS;
|
|
213
|
+
// `.*` is what was verified against a real exec run; it covers exec_command,
|
|
214
|
+
// apply_patch, and mcp__* alike.
|
|
215
|
+
const matcher = options.matcher ?? '.*';
|
|
216
|
+
const hooks = {};
|
|
217
|
+
for (const spec of events) {
|
|
218
|
+
const handler = {
|
|
219
|
+
type: 'command',
|
|
220
|
+
command: renderCodexHookCommand(entryScript, spec),
|
|
221
|
+
timeout: spec.timeoutSec
|
|
222
|
+
};
|
|
223
|
+
hooks[spec.pascal] =
|
|
224
|
+
spec.pascal === 'PreToolUse' || spec.pascal === 'PostToolUse'
|
|
225
|
+
? [{ matcher, hooks: [handler] }]
|
|
226
|
+
: [{ hooks: [handler] }];
|
|
227
|
+
}
|
|
228
|
+
return { hooks };
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Build the `config.toml` fragment that registers the MCP server.
|
|
232
|
+
*
|
|
233
|
+
* Codex reads MCP servers from `config.toml` (`[mcp_servers.<name>]`), which
|
|
234
|
+
* `codex mcp list --json` reports back and which the probe confirmed. The
|
|
235
|
+
* `.codex/config.json` file this repository wrote before that is kept for
|
|
236
|
+
* backwards compatibility but is not what Codex reads.
|
|
237
|
+
* @param entryScript - absolute path to `dist/cli/index.js`.
|
|
238
|
+
* @param options - `name` overrides the server name; `env` overrides the
|
|
239
|
+
* environment table (empty values are dropped, an empty object emits no table).
|
|
240
|
+
* @returns the TOML fragment, without the surrounding config.
|
|
241
|
+
*/
|
|
242
|
+
export function renderCodexMcpToml(entryScript, options = {}) {
|
|
243
|
+
const name = options.name ?? 'harness-reflex';
|
|
244
|
+
const env = options.env ?? {
|
|
245
|
+
SYSTEM_ONE_BACKEND: process.env.SYSTEM_ONE_BACKEND || process.env.REFLEX_BACKEND || 'mock',
|
|
246
|
+
SYSTEM_ONE_API_KEY: process.env.SYSTEM_ONE_API_KEY || process.env.JEV_API_KEY || '',
|
|
247
|
+
SYSTEM_ONE_BASE_URL: process.env.SYSTEM_ONE_BASE_URL || process.env.JEV_BASE_URL || ''
|
|
248
|
+
};
|
|
249
|
+
const lines = [
|
|
250
|
+
`[mcp_servers.${name}]`,
|
|
251
|
+
'command = "node"',
|
|
252
|
+
`args = [${JSON.stringify(entryScript)}, "mcp"]`,
|
|
253
|
+
'startup_timeout_sec = 20'
|
|
254
|
+
];
|
|
255
|
+
// Emit the env table only when there is at least one non-empty value.
|
|
256
|
+
const entries = Object.entries(env).filter(([, value]) => value !== '');
|
|
257
|
+
if (entries.length > 0) {
|
|
258
|
+
lines.push('');
|
|
259
|
+
lines.push(`[mcp_servers.${name}.env]`);
|
|
260
|
+
for (const [key, value] of entries)
|
|
261
|
+
lines.push(`${key} = ${JSON.stringify(value)}`);
|
|
262
|
+
}
|
|
263
|
+
return `${lines.join('\n')}\n`;
|
|
264
|
+
}
|
|
3
265
|
export class CodexAdapter {
|
|
4
266
|
/**
|
|
5
|
-
* Generates or updates Codex CLI configuration
|
|
267
|
+
* Generates or updates Codex CLI configuration: MCP (both files) and hooks.
|
|
268
|
+
*
|
|
269
|
+
* Codex reads MCP servers from `config.toml`, not `config.json` — the latter
|
|
270
|
+
* is what this repository wrote before and is kept so existing setups keep
|
|
271
|
+
* working. Hooks come from `$CODEX_HOME/hooks.json`, so the same directory is
|
|
272
|
+
* written here; a user who points `CODEX_HOME` at `<workspace>/.codex` gets a
|
|
273
|
+
* complete setup.
|
|
274
|
+
* @param workspaceDir - the directory that holds `.codex/`.
|
|
275
|
+
* @param binaryPath - absolute path to `dist/cli/index.js`.
|
|
6
276
|
*/
|
|
7
277
|
static configureCodex(workspaceDir, binaryPath) {
|
|
8
278
|
const codexConfigDir = path.join(workspaceDir, '.codex');
|
|
@@ -31,7 +301,34 @@ export class CodexAdapter {
|
|
|
31
301
|
}
|
|
32
302
|
};
|
|
33
303
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
|
|
34
|
-
|
|
304
|
+
// `config.toml` is the file Codex actually reads for MCP servers. Merge the
|
|
305
|
+
// managed block in without touching whatever else the user keeps there.
|
|
306
|
+
const tomlPath = path.join(codexConfigDir, 'config.toml');
|
|
307
|
+
const block = renderCodexMcpToml(binaryPath);
|
|
308
|
+
writeManagedTomlBlock(tomlPath, block);
|
|
309
|
+
const hooks = CodexAdapter.buildHooksConfig(binaryPath);
|
|
310
|
+
const hooksPath = path.join(codexConfigDir, 'hooks.json');
|
|
311
|
+
fs.writeFileSync(hooksPath, `${JSON.stringify(hooks, null, 2)}\n`, 'utf8');
|
|
312
|
+
return { configPath, hooksPath, tomlPath, created: true };
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Write only the hook wiring, leaving any MCP configuration alone.
|
|
316
|
+
* @param workspaceDir - the directory that holds `.codex/`.
|
|
317
|
+
* @param binaryPath - absolute path to `dist/cli/index.js`.
|
|
318
|
+
*/
|
|
319
|
+
static configureCodexHooks(workspaceDir, binaryPath) {
|
|
320
|
+
const codexConfigDir = path.join(workspaceDir, '.codex');
|
|
321
|
+
fs.mkdirSync(codexConfigDir, { recursive: true });
|
|
322
|
+
const hooksPath = path.join(codexConfigDir, 'hooks.json');
|
|
323
|
+
fs.writeFileSync(hooksPath, `${JSON.stringify(CodexAdapter.buildHooksConfig(binaryPath), null, 2)}\n`, 'utf8');
|
|
324
|
+
return { hooksPath };
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* The `hooks.json` document for every Codex event.
|
|
328
|
+
* @param binaryPath - absolute path to `dist/cli/index.js`.
|
|
329
|
+
*/
|
|
330
|
+
static buildHooksConfig(binaryPath) {
|
|
331
|
+
return buildCodexHooksConfig(binaryPath, { events: CODEX_HOOK_EVENTS });
|
|
35
332
|
}
|
|
36
333
|
static getCodexInstructions() {
|
|
37
334
|
return `
|
|
@@ -42,4 +339,30 @@ export class CodexAdapter {
|
|
|
42
339
|
`;
|
|
43
340
|
}
|
|
44
341
|
}
|
|
342
|
+
/** The marker that delimits the block this adapter owns inside `config.toml`. */
|
|
343
|
+
const TOML_BLOCK_START = '# >>> harness-reflex (managed) >>>';
|
|
344
|
+
const TOML_BLOCK_END = '# <<< harness-reflex (managed) <<<';
|
|
345
|
+
/**
|
|
346
|
+
* Replace the managed block in a TOML file, leaving every other byte alone.
|
|
347
|
+
* @param tomlPath - the file to update; created when missing.
|
|
348
|
+
* @param block - the block body, without markers.
|
|
349
|
+
*/
|
|
350
|
+
function writeManagedTomlBlock(tomlPath, block) {
|
|
351
|
+
const managed = `${TOML_BLOCK_START}\n${block.trimEnd()}\n${TOML_BLOCK_END}`;
|
|
352
|
+
let existing = '';
|
|
353
|
+
if (fs.existsSync(tomlPath)) {
|
|
354
|
+
existing = fs.readFileSync(tomlPath, 'utf8');
|
|
355
|
+
}
|
|
356
|
+
const start = existing.indexOf(TOML_BLOCK_START);
|
|
357
|
+
const end = existing.indexOf(TOML_BLOCK_END);
|
|
358
|
+
let next;
|
|
359
|
+
if (start >= 0 && end > start) {
|
|
360
|
+
next = `${existing.slice(0, start)}${managed}${existing.slice(end + TOML_BLOCK_END.length)}`;
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
const separator = existing.length === 0 || existing.endsWith('\n\n') ? '' : existing.endsWith('\n') ? '\n' : '\n\n';
|
|
364
|
+
next = `${existing}${separator}${managed}\n`;
|
|
365
|
+
}
|
|
366
|
+
fs.writeFileSync(tomlPath, next, 'utf8');
|
|
367
|
+
}
|
|
45
368
|
//# sourceMappingURL=codex.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codex.js","sourceRoot":"","sources":["../../src/adapters/codex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,MAAM,OAAO,YAAY;IACvB;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,YAAoB,EAAE,UAAkB;QAC5D,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;QACzD,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC;YACnC,EAAE,CAAC,SAAS,CAAC,cAAc,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACpD,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;QAC5D,IAAI,MAAM,GAAQ,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;QAErC,IAAI,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAC9B,IAAI,CAAC;gBACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;gBACzD,IAAI,CAAC,MAAM,CAAC,UAAU;oBAAE,MAAM,CAAC,UAAU,GAAG,EAAE,CAAC;YACjD,CAAC;YAAC,MAAM,CAAC;gBACP,MAAM,GAAG,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;YAC9B,CAAC;QACH,CAAC;QAED,MAAM,CAAC,UAAU,CAAC,gBAAgB,CAAC,GAAG;YACpC,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,CAAC,UAAU,EAAE,KAAK,CAAC;YACzB,GAAG,EAAE;gBACH,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,MAAM;gBAC1F,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,EAAE;gBACnF,mBAAmB,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,EAAE;aACvF;SACF,CAAC;QAEF,EAAE,CAAC,aAAa,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QACtE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IACvC,CAAC;IAED,MAAM,CAAC,oBAAoB;QACzB,OAAO;;;;;CAKV,CAAC;IACA,CAAC;CACF","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\nexport class CodexAdapter {\n /**\n * Generates or updates Codex CLI configuration\n */\n static configureCodex(workspaceDir: string, binaryPath: string): { configPath: string; created: boolean } {\n const codexConfigDir = path.join(workspaceDir, '.codex');\n if (!fs.existsSync(codexConfigDir)) {\n fs.mkdirSync(codexConfigDir, { recursive: true });\n }\n\n const configPath = path.join(codexConfigDir, 'config.json');\n let config: any = { mcpServers: {} };\n\n if (fs.existsSync(configPath)) {\n try {\n config = JSON.parse(fs.readFileSync(configPath, 'utf8'));\n if (!config.mcpServers) config.mcpServers = {};\n } catch {\n config = { mcpServers: {} };\n }\n }\n\n config.mcpServers['harness-reflex'] = {\n command: 'node',\n args: [binaryPath, 'mcp'],\n env: {\n SYSTEM_ONE_BACKEND: process.env.SYSTEM_ONE_BACKEND || process.env.REFLEX_BACKEND || 'mock',\n SYSTEM_ONE_API_KEY: process.env.SYSTEM_ONE_API_KEY || process.env.JEV_API_KEY || '',\n SYSTEM_ONE_BASE_URL: process.env.SYSTEM_ONE_BASE_URL || process.env.JEV_BASE_URL || ''\n }\n };\n\n fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');\n return { configPath, created: true };\n }\n\n static getCodexInstructions(): string {\n return `\n# Codex Harness Reflex Rules\n1. **Pre-flight & Post-flight Gate**: Do not submit PR or mark goal done without passing \\`reflex_gate\\`.\n2. **Context Pruning**: Use \\`reflex_prune\\` on command stdout exceeding 1000 characters.\n3. **Loop Breaker**: When hitting repeated bash exit code > 0, check \\`reflex_detect_stagnation\\`.\n`;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"codex.js","sourceRoot":"","sources":["../../src/adapters/codex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,sBAAsB,EAAE,aAAa,EAA2C,MAAM,qBAAqB,CAAC;AAwBrH,+EAA+E;AAC/E,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,CAAC;AAE/C;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,EAAU;IAC/B,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;AAC3C,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAkC;IAC9D;QACE,MAAM,E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n * Everything here is grounded in codex-cli 0.163.0 rather than in its\n * documentation, because the documentation does not cover hooks yet. The event\n * table was read out of the binary (`hooks/src/engine/command_runner.rs`) and\n * then confirmed by running a real `codex exec` against a probe hook:\n *\n * - **Twelve events**, not ten. `Stop` and `Interrupt` are in the binary's\n * normalized list and `Stop` fires on every completed turn.\n * - **`args` is not passed to the hook process.** A handler declared as\n * `{command: \"/path/hook.sh\", args: [\"session-start\"]}` runs with `argv = []`.\n * The event therefore has to ride in the command string itself.\n * - **Codex clamps the SessionEnd timeout to 3 s** and warns when `hooks.json`\n * asks for more, so the table stays under 3.\n * - **The event name is in the payload** (`hook_event_name`, PascalCase), which\n * is what the probe records and what the normalizer trusts over `argv`.\n *\n * The payload normalizer itself lives in `hook/payload` because it describes the\n * stdin shape of every host; an adapter that imported another adapter would\n * break the dependency fence this repository tests.\n *\n * @module adapters/codex\n */\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { CODEX_ONLY_HOOK_EVENTS, EVENT_BUDGETS, type CodexOnlyHookEvent, type HookEvent } from '../hook/contract.js';\n\n/** One Codex hook event, as the binary spells it in all three notations. */\nexport interface CodexHookEventSpec {\n /** The `hook_event_name` value Codex puts in the payload and reads back. */\n readonly pascal: string;\n /** The kebab-case subcommand this runtime accepts for the event. */\n readonly kebab: string;\n /** The `harness-reflex hook <subcommand>` token for the event. */\n readonly subcommand: string;\n /**\n * The canonical event this maps to, or `null` when the runtime has no\n * capability for it and must answer `{}`.\n */\n readonly canonical: HookEvent | null;\n /**\n * The Codex-only event, when {@link canonical} is `null` and the event is\n * still recognized. `null` on both means a genuinely unsupported event.\n */\n readonly codexOnly: CodexOnlyHookEvent | null;\n /** Handler timeout in seconds, kept inside both budgets. */\n readonly timeoutSec: number;\n}\n\n/** The SessionEnd timeout Codex clamps to, discovered from its own warning. */\nexport const CODEX_SESSION_END_TIMEOUT_SEC = 3;\n\n/**\n * Convert a host timeout in milliseconds to the whole seconds `hooks.json` takes.\n *\n * Rounded **up**: the host timeout must never be shorter than the internal\n * decision budget, or a decision that would have finished is killed by the host\n * and the event silently fails open.\n */\nfunction toHostSeconds(ms: number): number {\n return Math.max(1, Math.ceil(ms / 1000));\n}\n\n/**\n * The twelve Codex hook events and what this runtime does with each.\n *\n * Order follows the binary so a diff against `command_runner.rs` is readable.\n */\nexport const CODEX_HOOK_EVENTS: readonly CodexHookEventSpec[] = [\n {\n pascal: 'PreToolUse',\n kebab: 'pre-tool-use',\n subcommand: 'pretooluse',\n canonical: 'pretooluse',\n codexOnly: null,\n timeoutSec: toHostSeconds(EVENT_BUDGETS.pretooluse.hookTimeoutMs)\n },\n {\n pascal: 'PermissionRequest',\n kebab: 'permission-request',\n subcommand: 'permissionrequest',\n canonical: null,\n codexOnly: 'permissionrequest',\n timeoutSec: 1\n },\n {\n pascal: 'PostToolUse',\n kebab: 'post-tool-use',\n subcommand: 'posttooluse',\n canonical: 'posttooluse',\n codexOnly: null,\n timeoutSec: toHostSeconds(EVENT_BUDGETS.posttooluse.hookTimeoutMs)\n },\n {\n pascal: 'PreCompact',\n kebab: 'pre-compact',\n subcommand: 'precompact',\n canonical: null,\n codexOnly: 'precompact',\n timeoutSec: 1\n },\n {\n pascal: 'PostCompact',\n kebab: 'post-compact',\n subcommand: 'postcompact',\n canonical: null,\n codexOnly: 'postcompact',\n timeoutSec: 1\n },\n {\n pascal: 'SessionStart',\n kebab: 'session-start',\n subcommand: 'sessionstart',\n canonical: 'sessionstart',\n codexOnly: null,\n timeoutSec: toHostSeconds(EVENT_BUDGETS.sessionstart.hookTimeoutMs)\n },\n {\n pascal: 'SessionEnd',\n kebab: 'session-end',\n subcommand: 'sessionend',\n canonical: 'sessionend',\n codexOnly: null,\n // Codex clamps this one to 3 s and warns, so ask for 3 and stay quiet.\n timeoutSec: Math.min(CODEX_SESSION_END_TIMEOUT_SEC, toHostSeconds(EVENT_BUDGETS.sessionend.hookTimeoutMs))\n },\n {\n pascal: 'UserPromptSubmit',\n kebab: 'user-prompt-submit',\n subcommand: 'userpromptsubmit',\n canonical: null,\n codexOnly: 'userpromptsubmit',\n timeoutSec: 1\n },\n {\n pascal: 'SubagentStart',\n kebab: 'subagent-start',\n subcommand: 'subagentstart',\n canonical: null,\n codexOnly: 'subagentstart',\n timeoutSec: 1\n },\n {\n pascal: 'SubagentStop',\n kebab: 'subagent-stop',\n subcommand: 'substop',\n canonical: 'substop',\n codexOnly: null,\n timeoutSec: toHostSeconds(EVENT_BUDGETS.substop.hookTimeoutMs)\n },\n {\n pascal: 'Stop',\n kebab: 'stop',\n subcommand: 'stop',\n canonical: 'stop',\n codexOnly: null,\n timeoutSec: toHostSeconds(EVENT_BUDGETS.stop.hookTimeoutMs)\n },\n {\n pascal: 'Interrupt',\n kebab: 'interrupt',\n subcommand: 'interrupt',\n canonical: null,\n codexOnly: 'interrupt',\n timeoutSec: 1\n }\n];\n\n/** Normalize any spelling of a Codex event to its spec. */\nfunction normalizeName(raw: string): string {\n return raw.trim().toLowerCase().replace(/[-_\\s]/g, '');\n}\n\n/**\n * Resolve a Codex event from any spelling: PascalCase, kebab-case, snake_case,\n * the hook subcommand, or the canonical name.\n * @param raw - the event name to resolve.\n * @returns the matching spec, or `undefined` for an unsupported name.\n */\nexport function parseCodexHookEvent(raw: string | undefined): CodexHookEventSpec | undefined {\n if (!raw) return undefined;\n const wanted = normalizeName(raw);\n for (const spec of CODEX_HOOK_EVENTS) {\n if (\n normalizeName(spec.pascal) === wanted ||\n normalizeName(spec.kebab) === wanted ||\n normalizeName(spec.subcommand) === wanted\n ) {\n return spec;\n }\n }\n return undefined;\n}\n\n/** The spec for one canonical event, for tests and for the reverse mapping. */\nexport function codexSpecForCanonical(event: HookEvent): CodexHookEventSpec | undefined {\n return CODEX_HOOK_EVENTS.find((spec) => spec.canonical === event);\n}\n\n/** Every Codex-only event the runtime recognizes but cannot decide. */\nexport function codexOnlyEvents(): readonly CodexOnlyHookEvent[] {\n return CODEX_ONLY_HOOK_EVENTS;\n}\n\n/**\n * Render the whole command Codex should run for one event.\n *\n * Codex does not forward handler `args`, so the event is part of the command\n * string. Two shapes are supported and distinguished by the entry itself:\n *\n * - a **script path** (`/x/dist/cli/index.js`, `./cli.js`) runs under this Node\n * binary, because Codex spawns the command without knowing it is JS;\n * - a **command name** (`harness-reflex`, the installed bin) runs as-is.\n *\n * Anything that looks like a path is quoted, so a directory with spaces works.\n * @param entry - an absolute script path or an installed command name.\n * @param spec - the event to wire.\n * @returns the command string for `hooks.json`.\n */\nexport function renderCodexHookCommand(entry: string, spec: CodexHookEventSpec): string {\n const looksLikeScript = entry.includes('/') || entry.includes('\\\\') || /\\.(?:js|mjs|cjs)$/.test(entry);\n const program = looksLikeScript ? `node ${quote(entry)}` : entry;\n return `${program} hook ${spec.subcommand}`;\n}\n\n/** Quote one shell word when it contains anything a shell would split on. */\nfunction quote(word: string): string {\n return /^[A-Za-z0-9_./:@%+-]+$/.test(word) ? word : `\"${word.replace(/([\"\\\\$`])/g, '\\\\$1')}\"`;\n}\n\n/** One matcher group of a Codex `hooks.json` event. */\nexport interface CodexHookMatcherGroup {\n matcher?: string;\n hooks: { type: 'command'; command: string; timeout: number }[];\n}\n\n/** The `hooks.json` document Codex loads from `$CODEX_HOME/hooks.json`. */\nexport interface CodexHooksConfig {\n hooks: Record<string, CodexHookMatcherGroup[]>;\n}\n\n/**\n * Build the `hooks.json` document for this runtime.\n *\n * `PreToolUse` and `PostToolUse` get a `matcher` that covers both the built-in\n * shell tools and any MCP tool, because the probe showed `matcher: \".*\"` is\n * honored and a matcher that only names MCP tools would miss every shell call.\n * @param entryScript - absolute path to `dist/cli/index.js`.\n * @param options - `events` narrows the wired set; `matcher` overrides the tool\n * matcher for the tool-scoped events.\n * @returns the document, ready to serialize.\n */\nexport function buildCodexHooksConfig(\n entryScript: string,\n options: { events?: readonly CodexHookEventSpec[]; matcher?: string } = {}\n): CodexHooksConfig {\n const events = options.events ?? CODEX_HOOK_EVENTS;\n // `.*` is what was verified against a real exec run; it covers exec_command,\n // apply_patch, and mcp__* alike.\n const matcher = options.matcher ?? '.*';\n const hooks: Record<string, CodexHookMatcherGroup[]> = {};\n for (const spec of events) {\n const handler = {\n type: 'command' as const,\n command: renderCodexHookCommand(entryScript, spec),\n timeout: spec.timeoutSec\n };\n hooks[spec.pascal] =\n spec.pascal === 'PreToolUse' || spec.pascal === 'PostToolUse'\n ? [{ matcher, hooks: [handler] }]\n : [{ hooks: [handler] }];\n }\n return { hooks };\n}\n\n/**\n * Build the `config.toml` fragment that registers the MCP server.\n *\n * Codex reads MCP servers from `config.toml` (`[mcp_servers.<name>]`), which\n * `codex mcp list --json` reports back and which the probe confirmed. The\n * `.codex/config.json` file this repository wrote before that is kept for\n * backwards compatibility but is not what Codex reads.\n * @param entryScript - absolute path to `dist/cli/index.js`.\n * @param options - `name` overrides the server name; `env` overrides the\n * environment table (empty values are dropped, an empty object emits no table).\n * @returns the TOML fragment, without the surrounding config.\n */\nexport function renderCodexMcpToml(\n entryScript: string,\n options: { name?: string; env?: Record<string, string> } = {}\n): string {\n const name = options.name ?? 'harness-reflex';\n const env = options.env ?? {\n SYSTEM_ONE_BACKEND: process.env.SYSTEM_ONE_BACKEND || process.env.REFLEX_BACKEND || 'mock',\n SYSTEM_ONE_API_KEY: process.env.SYSTEM_ONE_API_KEY || process.env.JEV_API_KEY || '',\n SYSTEM_ONE_BASE_URL: process.env.SYSTEM_ONE_BASE_URL || process.env.JEV_BASE_URL || ''\n };\n const lines = [\n `[mcp_servers.${name}]`,\n 'command = \"node\"',\n `args = [${JSON.stringify(entryScript)}, \"mcp\"]`,\n 'startup_timeout_sec = 20'\n ];\n // Emit the env table only when there is at least one non-empty value.\n const entries = Object.entries(env).filter(([, value]) => value !== '');\n if (entries.length > 0) {\n lines.push('');\n lines.push(`[mcp_servers.${name}.env]`);\n for (const [key, value] of entries) lines.push(`${key} = ${JSON.stringify(value)}`);\n }\n return `${lines.join('\\n')}\\n`;\n}\n\n/** What one Codex configuration run wrote. */\nexport interface CodexSetupResult {\n /** `.codex/config.json` — the legacy MCP block, kept for compatibility. */\n configPath: string;\n /** `.codex/hooks.json` — the hook wiring Codex actually loads. */\n hooksPath: string;\n /** `.codex/config.toml` — the MCP block Codex actually reads. */\n tomlPath: string;\n created: boolean;\n}\n\nexport class CodexAdapter {\n /**\n * Generates or updates Codex CLI configuration: MCP (both files) and hooks.\n *\n * Codex reads MCP servers from `config.toml`, not `config.json` — the latter\n * is what this repository wrote before and is kept so existing setups keep\n * working. Hooks come from `$CODEX_HOME/hooks.json`, so the same directory is\n * written here; a user who points `CODEX_HOME` at `<workspace>/.codex` gets a\n * complete setup.\n * @param workspaceDir - the directory that holds `.codex/`.\n * @param binaryPath - absolute path to `dist/cli/index.js`.\n */\n static configureCodex(workspaceDir: string, binaryPath: string): CodexSetupResult {\n const codexConfigDir = path.join(workspaceDir, '.codex');\n if (!fs.existsSync(codexConfigDir)) {\n fs.mkdirSync(codexConfigDir, { recursive: true });\n }\n\n const configPath = path.join(codexConfigDir, 'config.json');\n let config: any = { mcpServers: {} };\n\n if (fs.existsSync(configPath)) {\n try {\n config = JSON.parse(fs.readFileSync(configPath, 'utf8'));\n if (!config.mcpServers) config.mcpServers = {};\n } catch {\n config = { mcpServers: {} };\n }\n }\n\n config.mcpServers['harness-reflex'] = {\n command: 'node',\n args: [binaryPath, 'mcp'],\n env: {\n SYSTEM_ONE_BACKEND: process.env.SYSTEM_ONE_BACKEND || process.env.REFLEX_BACKEND || 'mock',\n SYSTEM_ONE_API_KEY: process.env.SYSTEM_ONE_API_KEY || process.env.JEV_API_KEY || '',\n SYSTEM_ONE_BASE_URL: process.env.SYSTEM_ONE_BASE_URL || process.env.JEV_BASE_URL || ''\n }\n };\n\n fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');\n\n // `config.toml` is the file Codex actually reads for MCP servers. Merge the\n // managed block in without touching whatever else the user keeps there.\n const tomlPath = path.join(codexConfigDir, 'config.toml');\n const block = renderCodexMcpToml(binaryPath);\n writeManagedTomlBlock(tomlPath, block);\n\n const hooks = CodexAdapter.buildHooksConfig(binaryPath);\n const hooksPath = path.join(codexConfigDir, 'hooks.json');\n fs.writeFileSync(hooksPath, `${JSON.stringify(hooks, null, 2)}\\n`, 'utf8');\n\n return { configPath, hooksPath, tomlPath, created: true };\n }\n\n /**\n * Write only the hook wiring, leaving any MCP configuration alone.\n * @param workspaceDir - the directory that holds `.codex/`.\n * @param binaryPath - absolute path to `dist/cli/index.js`.\n */\n static configureCodexHooks(workspaceDir: string, binaryPath: string): { hooksPath: string } {\n const codexConfigDir = path.join(workspaceDir, '.codex');\n fs.mkdirSync(codexConfigDir, { recursive: true });\n const hooksPath = path.join(codexConfigDir, 'hooks.json');\n fs.writeFileSync(hooksPath, `${JSON.stringify(CodexAdapter.buildHooksConfig(binaryPath), null, 2)}\\n`, 'utf8');\n return { hooksPath };\n }\n\n /**\n * The `hooks.json` document for every Codex event.\n * @param binaryPath - absolute path to `dist/cli/index.js`.\n */\n static buildHooksConfig(binaryPath: string): CodexHooksConfig {\n return buildCodexHooksConfig(binaryPath, { events: CODEX_HOOK_EVENTS });\n }\n\n static getCodexInstructions(): string {\n return `\n# Codex Harness Reflex Rules\n1. **Pre-flight & Post-flight Gate**: Do not submit PR or mark goal done without passing \\`reflex_gate\\`.\n2. **Context Pruning**: Use \\`reflex_prune\\` on command stdout exceeding 1000 characters.\n3. **Loop Breaker**: When hitting repeated bash exit code > 0, check \\`reflex_detect_stagnation\\`.\n`;\n }\n}\n\n/** The marker that delimits the block this adapter owns inside `config.toml`. */\nconst TOML_BLOCK_START = '# >>> harness-reflex (managed) >>>';\nconst TOML_BLOCK_END = '# <<< harness-reflex (managed) <<<';\n\n/**\n * Replace the managed block in a TOML file, leaving every other byte alone.\n * @param tomlPath - the file to update; created when missing.\n * @param block - the block body, without markers.\n */\nfunction writeManagedTomlBlock(tomlPath: string, block: string): void {\n const managed = `${TOML_BLOCK_START}\\n${block.trimEnd()}\\n${TOML_BLOCK_END}`;\n let existing = '';\n if (fs.existsSync(tomlPath)) {\n existing = fs.readFileSync(tomlPath, 'utf8');\n }\n const start = existing.indexOf(TOML_BLOCK_START);\n const end = existing.indexOf(TOML_BLOCK_END);\n let next: string;\n if (start >= 0 && end > start) {\n next = `${existing.slice(0, start)}${managed}${existing.slice(end + TOML_BLOCK_END.length)}`;\n } else {\n const separator = existing.length === 0 || existing.endsWith('\\n\\n') ? '' : existing.endsWith('\\n') ? '\\n' : '\\n\\n';\n next = `${existing}${separator}${managed}\\n`;\n }\n fs.writeFileSync(tomlPath, next, 'utf8');\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The values each System One backend falls back to when neither the
|
|
3
|
+
* deployment configuration nor the environment names one.
|
|
4
|
+
*
|
|
5
|
+
* They live in one place because they are shipped documentation, not
|
|
6
|
+
* implementation detail: the user guide, the settings-panel help text and the
|
|
7
|
+
* backends themselves must agree, and a single source is what makes a drift a
|
|
8
|
+
* compile error rather than a stale sentence. They are deliberately *not* one
|
|
9
|
+
* shared value per variable — the Jev gateway and a self-hosted vLLM differ in
|
|
10
|
+
* every one of them.
|
|
11
|
+
*
|
|
12
|
+
* @module backend/defaults
|
|
13
|
+
*/
|
|
14
|
+
/** Endpoint used when neither configuration nor environment supplies a Jev base URL. */
|
|
15
|
+
export declare const JEV_DEFAULT_ENDPOINT_PATH = "/v1/systemone";
|
|
16
|
+
/** @see JEV_DEFAULT_ENDPOINT_PATH */
|
|
17
|
+
export declare const JEV_DEFAULT_BASE_URL = "https://api.typesafe.ai/v1/systemone";
|
|
18
|
+
/** Model id the Jev gateway serves when nothing overrides it. */
|
|
19
|
+
export declare const JEV_DEFAULT_MODEL = "jev-latest";
|
|
20
|
+
/** Jev has no default key: an empty key is sent as-is and the service rejects it. */
|
|
21
|
+
export declare const JEV_DEFAULT_API_KEY = "";
|
|
22
|
+
/** Per-request timeout of the Jev backend, in milliseconds. */
|
|
23
|
+
export declare const JEV_DEFAULT_TIMEOUT_MS = 10000;
|
|
24
|
+
/** Endpoint used when neither configuration nor environment supplies a logits base URL. */
|
|
25
|
+
export declare const LOGITS_DEFAULT_BASE_URL = "http://localhost:8000";
|
|
26
|
+
/** Model id asked for when nothing overrides it. */
|
|
27
|
+
export declare const LOGITS_DEFAULT_MODEL = "Qwen/Qwen2.5-7B-Instruct";
|
|
28
|
+
/** A local vLLM usually checks no credentials, so a placeholder keeps the header well-formed. */
|
|
29
|
+
export declare const LOGITS_DEFAULT_API_KEY = "dummy";
|
|
30
|
+
/** Per-request timeout of the logits backend, in milliseconds. */
|
|
31
|
+
export declare const LOGITS_DEFAULT_TIMEOUT_MS = 8000;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The values each System One backend falls back to when neither the
|
|
3
|
+
* deployment configuration nor the environment names one.
|
|
4
|
+
*
|
|
5
|
+
* They live in one place because they are shipped documentation, not
|
|
6
|
+
* implementation detail: the user guide, the settings-panel help text and the
|
|
7
|
+
* backends themselves must agree, and a single source is what makes a drift a
|
|
8
|
+
* compile error rather than a stale sentence. They are deliberately *not* one
|
|
9
|
+
* shared value per variable — the Jev gateway and a self-hosted vLLM differ in
|
|
10
|
+
* every one of them.
|
|
11
|
+
*
|
|
12
|
+
* @module backend/defaults
|
|
13
|
+
*/
|
|
14
|
+
/** Endpoint used when neither configuration nor environment supplies a Jev base URL. */
|
|
15
|
+
export const JEV_DEFAULT_ENDPOINT_PATH = '/v1/systemone';
|
|
16
|
+
/** @see JEV_DEFAULT_ENDPOINT_PATH */
|
|
17
|
+
export const JEV_DEFAULT_BASE_URL = `https://api.typesafe.ai${JEV_DEFAULT_ENDPOINT_PATH}`;
|
|
18
|
+
/** Model id the Jev gateway serves when nothing overrides it. */
|
|
19
|
+
export const JEV_DEFAULT_MODEL = 'jev-latest';
|
|
20
|
+
/** Jev has no default key: an empty key is sent as-is and the service rejects it. */
|
|
21
|
+
export const JEV_DEFAULT_API_KEY = '';
|
|
22
|
+
/** Per-request timeout of the Jev backend, in milliseconds. */
|
|
23
|
+
export const JEV_DEFAULT_TIMEOUT_MS = 10_000;
|
|
24
|
+
/** Endpoint used when neither configuration nor environment supplies a logits base URL. */
|
|
25
|
+
export const LOGITS_DEFAULT_BASE_URL = 'http://localhost:8000';
|
|
26
|
+
/** Model id asked for when nothing overrides it. */
|
|
27
|
+
export const LOGITS_DEFAULT_MODEL = 'Qwen/Qwen2.5-7B-Instruct';
|
|
28
|
+
/** A local vLLM usually checks no credentials, so a placeholder keeps the header well-formed. */
|
|
29
|
+
export const LOGITS_DEFAULT_API_KEY = 'dummy';
|
|
30
|
+
/** Per-request timeout of the logits backend, in milliseconds. */
|
|
31
|
+
export const LOGITS_DEFAULT_TIMEOUT_MS = 8000;
|
|
32
|
+
//# sourceMappingURL=defaults.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.js","sourceRoot":"","sources":["../../src/backend/defaults.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,wFAAwF;AACxF,MAAM,CAAC,MAAM,yBAAyB,GAAG,eAAe,CAAC;AACzD,qCAAqC;AACrC,MAAM,CAAC,MAAM,oBAAoB,GAAG,0BAA0B,yBAAyB,EAAE,CAAC;AAC1F,iEAAiE;AACjE,MAAM,CAAC,MAAM,iBAAiB,GAAG,YAAY,CAAC;AAC9C,qFAAqF;AACrF,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,CAAC;AACtC,+DAA+D;AAC/D,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAE7C,2FAA2F;AAC3F,MAAM,CAAC,MAAM,uBAAuB,GAAG,uBAAuB,CAAC;AAC/D,oDAAoD;AACpD,MAAM,CAAC,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAC/D,iGAAiG;AACjG,MAAM,CAAC,MAAM,sBAAsB,GAAG,OAAO,CAAC;AAC9C,kEAAkE;AAClE,MAAM,CAAC,MAAM,yBAAyB,GAAG,IAAI,CAAC","sourcesContent":["/**\n * The values each System One backend falls back to when neither the\n * deployment configuration nor the environment names one.\n *\n * They live in one place because they are shipped documentation, not\n * implementation detail: the user guide, the settings-panel help text and the\n * backends themselves must agree, and a single source is what makes a drift a\n * compile error rather than a stale sentence. They are deliberately *not* one\n * shared value per variable — the Jev gateway and a self-hosted vLLM differ in\n * every one of them.\n *\n * @module backend/defaults\n */\n\n/** Endpoint used when neither configuration nor environment supplies a Jev base URL. */\nexport const JEV_DEFAULT_ENDPOINT_PATH = '/v1/systemone';\n/** @see JEV_DEFAULT_ENDPOINT_PATH */\nexport const JEV_DEFAULT_BASE_URL = `https://api.typesafe.ai${JEV_DEFAULT_ENDPOINT_PATH}`;\n/** Model id the Jev gateway serves when nothing overrides it. */\nexport const JEV_DEFAULT_MODEL = 'jev-latest';\n/** Jev has no default key: an empty key is sent as-is and the service rejects it. */\nexport const JEV_DEFAULT_API_KEY = '';\n/** Per-request timeout of the Jev backend, in milliseconds. */\nexport const JEV_DEFAULT_TIMEOUT_MS = 10_000;\n\n/** Endpoint used when neither configuration nor environment supplies a logits base URL. */\nexport const LOGITS_DEFAULT_BASE_URL = 'http://localhost:8000';\n/** Model id asked for when nothing overrides it. */\nexport const LOGITS_DEFAULT_MODEL = 'Qwen/Qwen2.5-7B-Instruct';\n/** A local vLLM usually checks no credentials, so a placeholder keeps the header well-formed. */\nexport const LOGITS_DEFAULT_API_KEY = 'dummy';\n/** Per-request timeout of the logits backend, in milliseconds. */\nexport const LOGITS_DEFAULT_TIMEOUT_MS = 8000;\n"]}
|
package/dist/backend/jev.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { CURRENT_MODEL_MARK, buildRouteContext } from './route-prompt.js';
|
|
2
|
+
import { JEV_DEFAULT_API_KEY, JEV_DEFAULT_BASE_URL, JEV_DEFAULT_MODEL, JEV_DEFAULT_TIMEOUT_MS } from './defaults.js';
|
|
2
3
|
/** Path of the one endpoint the current gateway exposes. */
|
|
3
4
|
export const SYSTEMONE_ENDPOINT_PATH = '/v1/systemone';
|
|
4
5
|
/** Endpoint used when neither configuration nor environment supplies a base. */
|
|
5
|
-
export const DEFAULT_SYSTEMONE_ENDPOINT =
|
|
6
|
+
export const DEFAULT_SYSTEMONE_ENDPOINT = JEV_DEFAULT_BASE_URL;
|
|
6
7
|
/** Illustration of a choice request, for docs and drift checks. */
|
|
7
8
|
export const CHOICE_REQUEST_EXAMPLE = {
|
|
8
|
-
model:
|
|
9
|
+
model: JEV_DEFAULT_MODEL,
|
|
9
10
|
state: {},
|
|
10
11
|
questions: {
|
|
11
12
|
q: { type: 'choice', instructions: 'Which option is better?', criteria: { a: 'the first option', b: 'the second option' } }
|
|
@@ -906,13 +907,13 @@ export class JevDecisionBackend {
|
|
|
906
907
|
config?.apiKey ||
|
|
907
908
|
process.env.SYSTEM_ONE_API_KEY ||
|
|
908
909
|
process.env.JEV_API_KEY ||
|
|
909
|
-
|
|
910
|
+
JEV_DEFAULT_API_KEY;
|
|
910
911
|
this.model =
|
|
911
912
|
config?.model ||
|
|
912
913
|
process.env.SYSTEM_ONE_MODEL ||
|
|
913
914
|
process.env.JEV_MODEL ||
|
|
914
|
-
|
|
915
|
-
this.timeoutMs = config?.timeoutMs ||
|
|
915
|
+
JEV_DEFAULT_MODEL;
|
|
916
|
+
this.timeoutMs = config?.timeoutMs || JEV_DEFAULT_TIMEOUT_MS;
|
|
916
917
|
this.pruneJudgeMinChars = resolvePruneJudgeMinChars(config?.pruneJudgeMinChars);
|
|
917
918
|
}
|
|
918
919
|
/**
|