mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-17 03:28:57 +08:00
Compare commits
2 Commits
codex/refa
...
dallin/mat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
347ae156d6 | ||
|
|
f60cefac67 |
1
.github/labeler.yml
vendored
1
.github/labeler.yml
vendored
@@ -122,6 +122,7 @@
|
||||
- "docs/concepts/qa-e2e-automation.md"
|
||||
- "docs/concepts/personal-agent-benchmark-pack.md"
|
||||
- "docs/channels/qa-channel.md"
|
||||
- "docs/reference/maturity-tests.md"
|
||||
"channel: signal":
|
||||
- changed-files:
|
||||
- any-glob-to-any-file:
|
||||
|
||||
11
.github/workflows/ci.yml
vendored
11
.github/workflows/ci.yml
vendored
@@ -1358,8 +1358,6 @@ jobs:
|
||||
- check_name: check-additional-boundaries-bcd
|
||||
group: boundaries
|
||||
boundary_shard: 2/4,3/4,4/4
|
||||
- check_name: check-session-accessor-boundary
|
||||
group: session-accessor-boundary
|
||||
- check_name: check-additional-extension-channels
|
||||
group: extension-channels
|
||||
- check_name: check-additional-extension-bundled
|
||||
@@ -1506,15 +1504,6 @@ jobs:
|
||||
boundaries)
|
||||
node scripts/run-additional-boundary-checks.mjs
|
||||
;;
|
||||
session-accessor-boundary)
|
||||
if [ ! -f scripts/check-session-accessor-boundary.mjs ]; then
|
||||
echo "[skip] session accessor boundary check is not present in this checkout"
|
||||
elif ! node -e 'const pkg = require("./package.json"); process.exit(pkg.scripts?.["lint:tmp:session-accessor-boundary"] ? 0 : 1);'; then
|
||||
echo "[skip] session accessor boundary script is not present in package.json"
|
||||
else
|
||||
run_check "lint:tmp:session-accessor-boundary" pnpm run lint:tmp:session-accessor-boundary
|
||||
fi
|
||||
;;
|
||||
extension-channels)
|
||||
run_check "lint:extensions:channels" pnpm run lint:extensions:channels
|
||||
;;
|
||||
|
||||
@@ -783,7 +783,7 @@ jobs:
|
||||
fi
|
||||
|
||||
args=(
|
||||
-f ref="$TARGET_REF"
|
||||
-f ref="$TARGET_SHA"
|
||||
-f expected_sha="$TARGET_SHA"
|
||||
-f provider="$PROVIDER"
|
||||
-f mode="$MODE"
|
||||
|
||||
1
.github/workflows/mantis-telegram-live.yml
vendored
1
.github/workflows/mantis-telegram-live.yml
vendored
@@ -379,6 +379,7 @@ jobs:
|
||||
OPENCLAW_QA_CONVEX_SECRET_CI: ${{ secrets.OPENCLAW_QA_CONVEX_SECRET_CI }}
|
||||
OPENCLAW_QA_CREDENTIAL_ACQUIRE_TIMEOUT_MS: "1800000"
|
||||
OPENCLAW_QA_REDACT_PUBLIC_METADATA: "1"
|
||||
OPENCLAW_QA_TELEGRAM_CAPTURE_CONTENT: "1"
|
||||
CRABBOX_COORDINATOR: ${{ secrets.CRABBOX_COORDINATOR }}
|
||||
CRABBOX_COORDINATOR_TOKEN: ${{ secrets.CRABBOX_COORDINATOR_TOKEN }}
|
||||
OPENCLAW_QA_MANTIS_CRABBOX_COORDINATOR: ${{ secrets.OPENCLAW_QA_MANTIS_CRABBOX_COORDINATOR }}
|
||||
|
||||
1
.github/workflows/npm-telegram-beta-e2e.yml
vendored
1
.github/workflows/npm-telegram-beta-e2e.yml
vendored
@@ -220,6 +220,7 @@ jobs:
|
||||
OPENCLAW_QA_CONVEX_SECRET_CI: ${{ secrets.OPENCLAW_QA_CONVEX_SECRET_CI }}
|
||||
OPENCLAW_QA_CREDENTIAL_ACQUIRE_TIMEOUT_MS: "1800000"
|
||||
OPENCLAW_QA_REDACT_PUBLIC_METADATA: "1"
|
||||
OPENCLAW_QA_TELEGRAM_CAPTURE_CONTENT: "1"
|
||||
INPUT_SCENARIO: ${{ inputs.scenario }}
|
||||
PACKAGE_ARTIFACT_NAME: ${{ inputs.package_artifact_name || '' }}
|
||||
run: |
|
||||
|
||||
25
.github/workflows/openclaw-release-checks.yml
vendored
25
.github/workflows/openclaw-release-checks.yml
vendored
@@ -1181,7 +1181,7 @@ jobs:
|
||||
runtime_tool_coverage_release_checks:
|
||||
name: Enforce QA Lab runtime tool coverage
|
||||
needs: [resolve_target, qa_lab_runtime_parity_release_checks]
|
||||
if: contains(fromJSON('["all","qa","qa-parity"]'), needs.resolve_target.outputs.rerun_group)
|
||||
if: always() && contains(fromJSON('["all","qa","qa-parity"]'), needs.resolve_target.outputs.rerun_group)
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 15
|
||||
permissions:
|
||||
@@ -1204,35 +1204,13 @@ jobs:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
install-bun: "true"
|
||||
|
||||
- name: Download runtime parity status
|
||||
uses: actions/download-artifact@v8
|
||||
with:
|
||||
name: release-check-status-qa-runtime-parity-${{ needs.resolve_target.outputs.revision }}
|
||||
path: .artifacts/release-check-status/
|
||||
|
||||
- name: Verify runtime parity producer status
|
||||
id: verify_runtime_parity_status
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
status_path=".artifacts/release-check-status/qa_lab_runtime_parity_release_checks.env"
|
||||
status="$(sed -n 's/^status=//p' "$status_path" | tail -n 1)"
|
||||
if [[ "$status" != "success" ]]; then
|
||||
echo "Runtime parity producer status is ${status:-missing}; skipping coverage artifact consumer."
|
||||
echo "ready=false" >> "$GITHUB_OUTPUT"
|
||||
exit 0
|
||||
fi
|
||||
echo "ready=true" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Download runtime parity artifacts
|
||||
if: steps.verify_runtime_parity_status.outputs.ready == 'true'
|
||||
uses: actions/download-artifact@v8
|
||||
with:
|
||||
name: release-qa-runtime-parity-${{ needs.resolve_target.outputs.revision }}
|
||||
path: .artifacts/qa-e2e/
|
||||
|
||||
- name: Enforce standard runtime tool coverage
|
||||
if: steps.verify_runtime_parity_status.outputs.ready == 'true'
|
||||
run: |
|
||||
set -euo pipefail
|
||||
pnpm openclaw qa coverage \
|
||||
@@ -1434,6 +1412,7 @@ jobs:
|
||||
OPENCLAW_QA_CONVEX_SECRET_CI: ${{ secrets.OPENCLAW_QA_CONVEX_SECRET_CI }}
|
||||
OPENCLAW_QA_CREDENTIAL_ACQUIRE_TIMEOUT_MS: "1800000"
|
||||
OPENCLAW_QA_REDACT_PUBLIC_METADATA: "1"
|
||||
OPENCLAW_QA_TELEGRAM_CAPTURE_CONTENT: "1"
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
|
||||
@@ -532,6 +532,7 @@ jobs:
|
||||
OPENCLAW_QA_CONVEX_SECRET_CI: ${{ secrets.OPENCLAW_QA_CONVEX_SECRET_CI }}
|
||||
OPENCLAW_QA_CREDENTIAL_ACQUIRE_TIMEOUT_MS: "1800000"
|
||||
OPENCLAW_QA_REDACT_PUBLIC_METADATA: "1"
|
||||
OPENCLAW_QA_TELEGRAM_CAPTURE_CONTENT: "1"
|
||||
INPUT_SCENARIO: ${{ github.event_name == 'workflow_dispatch' && inputs.scenario || '' }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
@@ -2074,204 +2074,6 @@ public struct SessionsCompactionRestoreResult: Codable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionFileBrowserEntry: Codable, Sendable {
|
||||
public let path: String
|
||||
public let name: String
|
||||
public let kind: AnyCodable
|
||||
public let sessionkind: SessionFileRelevance?
|
||||
public let size: Int?
|
||||
public let updatedatms: Int?
|
||||
|
||||
public init(
|
||||
path: String,
|
||||
name: String,
|
||||
kind: AnyCodable,
|
||||
sessionkind: SessionFileRelevance?,
|
||||
size: Int?,
|
||||
updatedatms: Int?)
|
||||
{
|
||||
self.path = path
|
||||
self.name = name
|
||||
self.kind = kind
|
||||
self.sessionkind = sessionkind
|
||||
self.size = size
|
||||
self.updatedatms = updatedatms
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case path
|
||||
case name
|
||||
case kind
|
||||
case sessionkind = "sessionKind"
|
||||
case size
|
||||
case updatedatms = "updatedAtMs"
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionFileBrowserResult: Codable, Sendable {
|
||||
public let path: String
|
||||
public let parentpath: String?
|
||||
public let search: String?
|
||||
public let entries: [SessionFileBrowserEntry]
|
||||
public let truncated: Bool?
|
||||
|
||||
public init(
|
||||
path: String,
|
||||
parentpath: String?,
|
||||
search: String?,
|
||||
entries: [SessionFileBrowserEntry],
|
||||
truncated: Bool?)
|
||||
{
|
||||
self.path = path
|
||||
self.parentpath = parentpath
|
||||
self.search = search
|
||||
self.entries = entries
|
||||
self.truncated = truncated
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case path
|
||||
case parentpath = "parentPath"
|
||||
case search
|
||||
case entries
|
||||
case truncated
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionFileEntry: Codable, Sendable {
|
||||
public let path: String
|
||||
public let name: String
|
||||
public let kind: SessionFileKind
|
||||
public let missing: Bool
|
||||
public let size: Int?
|
||||
public let updatedatms: Int?
|
||||
public let content: String?
|
||||
|
||||
public init(
|
||||
path: String,
|
||||
name: String,
|
||||
kind: SessionFileKind,
|
||||
missing: Bool,
|
||||
size: Int?,
|
||||
updatedatms: Int?,
|
||||
content: String?)
|
||||
{
|
||||
self.path = path
|
||||
self.name = name
|
||||
self.kind = kind
|
||||
self.missing = missing
|
||||
self.size = size
|
||||
self.updatedatms = updatedatms
|
||||
self.content = content
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case path
|
||||
case name
|
||||
case kind
|
||||
case missing
|
||||
case size
|
||||
case updatedatms = "updatedAtMs"
|
||||
case content
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionsFilesListParams: Codable, Sendable {
|
||||
public let sessionkey: String
|
||||
public let agentid: String?
|
||||
public let path: String?
|
||||
public let search: String?
|
||||
|
||||
public init(
|
||||
sessionkey: String,
|
||||
agentid: String? = nil,
|
||||
path: String?,
|
||||
search: String?)
|
||||
{
|
||||
self.sessionkey = sessionkey
|
||||
self.agentid = agentid
|
||||
self.path = path
|
||||
self.search = search
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case sessionkey = "sessionKey"
|
||||
case agentid = "agentId"
|
||||
case path
|
||||
case search
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionsFilesListResult: Codable, Sendable {
|
||||
public let sessionkey: String
|
||||
public let root: String?
|
||||
public let files: [SessionFileEntry]
|
||||
public let browser: SessionFileBrowserResult?
|
||||
|
||||
public init(
|
||||
sessionkey: String,
|
||||
root: String?,
|
||||
files: [SessionFileEntry],
|
||||
browser: SessionFileBrowserResult?)
|
||||
{
|
||||
self.sessionkey = sessionkey
|
||||
self.root = root
|
||||
self.files = files
|
||||
self.browser = browser
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case sessionkey = "sessionKey"
|
||||
case root
|
||||
case files
|
||||
case browser
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionsFilesGetParams: Codable, Sendable {
|
||||
public let sessionkey: String
|
||||
public let path: String
|
||||
public let agentid: String?
|
||||
|
||||
public init(
|
||||
sessionkey: String,
|
||||
path: String,
|
||||
agentid: String? = nil)
|
||||
{
|
||||
self.sessionkey = sessionkey
|
||||
self.path = path
|
||||
self.agentid = agentid
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case sessionkey = "sessionKey"
|
||||
case path
|
||||
case agentid = "agentId"
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionsFilesGetResult: Codable, Sendable {
|
||||
public let sessionkey: String
|
||||
public let root: String?
|
||||
public let file: SessionFileEntry
|
||||
|
||||
public init(
|
||||
sessionkey: String,
|
||||
root: String?,
|
||||
file: SessionFileEntry)
|
||||
{
|
||||
self.sessionkey = sessionkey
|
||||
self.root = root
|
||||
self.file = file
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case sessionkey = "sessionKey"
|
||||
case root
|
||||
case file
|
||||
}
|
||||
}
|
||||
|
||||
public struct SessionsCreateParams: Codable, Sendable {
|
||||
public let key: String?
|
||||
public let agentid: String?
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
b121079a0912b3051a9fc319a675ef920da9db23364ca0c0ccd3c9f0a05a3a49 plugin-sdk-api-baseline.json
|
||||
61a0108da670e0f44ba4b861c002eb6eaa5cf63e392d4e7e7de42044cbe7d115 plugin-sdk-api-baseline.jsonl
|
||||
85c3572e6ed2bfe3df92c7d53cef465b30d2e861ad9529009faa287cdc5aec71 plugin-sdk-api-baseline.json
|
||||
0d7c7e42d04b97d40519c5a23ba96599b05868c71a997eb913b9fccbc5fb2515 plugin-sdk-api-baseline.jsonl
|
||||
|
||||
@@ -943,6 +943,10 @@
|
||||
"source": "Matrix QA",
|
||||
"target": "Matrix QA"
|
||||
},
|
||||
{
|
||||
"source": "Maturity tests",
|
||||
"target": "成熟度测试"
|
||||
},
|
||||
{
|
||||
"source": "Matrix presentation metadata",
|
||||
"target": "Matrix 呈现元数据"
|
||||
|
||||
@@ -311,9 +311,7 @@ $OPENCLAW_STATE_DIR/tasks/runs.sqlite
|
||||
|
||||
The registry loads into memory at gateway start and syncs writes to SQLite for durability across restarts.
|
||||
The Gateway keeps the SQLite write-ahead log bounded by using SQLite's default
|
||||
autocheckpoint threshold plus periodic `PASSIVE` checkpoints. Shutdown and
|
||||
explicit maintenance checkpoints still use `TRUNCATE` so normal closes can
|
||||
reclaim WAL space without making the background sweeper wait on active readers.
|
||||
autocheckpoint threshold plus periodic and shutdown `TRUNCATE` checkpoints.
|
||||
|
||||
### Automatic maintenance
|
||||
|
||||
|
||||
@@ -416,9 +416,7 @@ Enable `dynamicAgentCreation` to automatically create **isolated agent instances
|
||||
This is essential for public bots where you want each user to have their own private AI assistant experience.
|
||||
|
||||
<Note>
|
||||
Dynamic bindings include the normalized Feishu `accountId`, so default and named accounts route each sender to the correct dynamic agent.
|
||||
|
||||
If a named account created an unscoped dynamic agent on an older release, that legacy agent still counts toward `maxAgents`. Confirm that it is not used by the default account before removing it, or temporarily increase `maxAgents`; OpenClaw cannot safely infer which account owns ambiguous legacy state.
|
||||
**Account limitation**: `dynamicAgentCreation` currently works with the **default Feishu account only**. Named/multi-account setups are not yet fully supported — dynamic bindings are created without `accountId`, so messages to named accounts may still route to `agent:main`. Track progress in [Issue #42837](https://github.com/openclaw/openclaw/issues/42837).
|
||||
</Note>
|
||||
|
||||
### Quick setup
|
||||
@@ -449,7 +447,7 @@ If a named account created an unscoped dynamic agent on an older release, that l
|
||||
|
||||
When a new user sends their first DM:
|
||||
|
||||
1. The channel generates a unique `agentId`: `feishu-{user_open_id}` for the default account, or a bounded account-prefixed identity digest for a named account
|
||||
1. The channel generates a unique `agentId` = `feishu-{user_open_id}`
|
||||
2. Creates a new workspace at `workspaceTemplate` path
|
||||
3. Registers the agent and creates a binding for this user
|
||||
4. The workspace helper ensures bootstrap files (`AGENTS.md`, `SOUL.md`, `USER.md`, etc.) on first access
|
||||
@@ -466,23 +464,22 @@ When a new user sends their first DM:
|
||||
|
||||
Template variables:
|
||||
|
||||
- `{agentId}` - the generated agent ID (e.g., `feishu-ou_xxxxxx` or `feishu-support-<identity_digest>`)
|
||||
- `{agentId}` - the generated agent ID (e.g., `feishu-ou_xxxxxx`)
|
||||
- `{userId}` - the sender's Feishu open_id (e.g., `ou_xxxxxx`)
|
||||
|
||||
### Session scope
|
||||
|
||||
`session.dmScope` controls how direct messages are mapped to agent sessions. This is a **global setting** that affects all channels.
|
||||
|
||||
| Value | Behavior | Best for |
|
||||
| ---------------------------- | ------------------------------------------------------------------- | ------------------------------------------------------------------ |
|
||||
| `"main"` | Each user's DM maps to their agent's main session | Single-user bots where you want `USER.md` / `SOUL.md` to auto-load |
|
||||
| `"per-channel-peer"` | Each (channel + user) combination gets a separate session | Public multi-user bots needing stronger isolation |
|
||||
| `"per-account-channel-peer"` | Each (account + channel + user) combination gets a separate session | Multi-account bots needing account-level session isolation |
|
||||
| Value | Behavior | Best for |
|
||||
| -------------------- | --------------------------------------------------------- | ------------------------------------------------------------------ |
|
||||
| `"main"` | Each user's DM maps to their agent's main session | Single-user bots where you want `USER.md` / `SOUL.md` to auto-load |
|
||||
| `"per-channel-peer"` | Each (channel + user) combination gets a separate session | Public multi-user bots needing stronger isolation |
|
||||
|
||||
**Tradeoff**: Using `"main"` enables automatic bootstrap file loading (`USER.md`, `SOUL.md`, `MEMORY.md`), but means all DMs across all channels share the same session key pattern. For public multi-user bots where isolation matters more than bootstrap auto-loading, consider `"per-channel-peer"` and manage bootstrap files manually.
|
||||
|
||||
<Note>
|
||||
Use `"per-account-channel-peer"` when named Feishu accounts should keep separate sessions for the same sender. Dynamic bindings preserve the account scope.
|
||||
`"per-account-channel-peer"` is not recommended with `dynamicAgentCreation` because dynamic bindings are created without `accountId`. Use it only with manual bindings.
|
||||
</Note>
|
||||
|
||||
```json5
|
||||
|
||||
@@ -247,13 +247,12 @@ of only a bot-to-bot Slack transcript.
|
||||
evidence pipeline. It checks out the trusted candidate ref in a separate
|
||||
worktree, runs `pnpm openclaw qa telegram --credential-source convex
|
||||
--credential-role ci`, writes a `mantis-evidence.json` manifest from the
|
||||
Telegram QA summary, `qa-evidence.json`, and report artifacts, renders the
|
||||
redacted evidence HTML through a Crabbox desktop browser, generates a
|
||||
motion-trimmed GIF with `crabbox media preview`, and posts the inline PR
|
||||
evidence comment when a PR number is available. This lane is QA-evidence visual
|
||||
rather than logged-in Telegram Web proof: the Telegram Bot API gives stable live
|
||||
message evidence, but Telegram Web login state is not required for normal Mantis
|
||||
automation.
|
||||
Telegram QA summary and observed-message artifact, renders the redacted
|
||||
transcript HTML through a Crabbox desktop browser, generates a motion-trimmed GIF
|
||||
with `crabbox media preview`, and posts the inline PR evidence comment when a PR
|
||||
number is available. This lane is transcript-visual rather than logged-in
|
||||
Telegram Web proof: the Telegram Bot API gives stable live message evidence, but
|
||||
Telegram Web login state is not required for normal Mantis automation.
|
||||
|
||||
`Mantis Telegram Desktop Proof` is the agentic native Telegram Desktop
|
||||
before/after wrapper. A maintainer can trigger it from a PR comment with
|
||||
@@ -495,8 +494,8 @@ zero:
|
||||
|
||||
- `pnpm openclaw qa discord` already runs a live Discord lane with driver and
|
||||
SUT bots.
|
||||
- The live transport runner already writes reports, QA evidence, and
|
||||
transport-specific artifacts under `.artifacts/qa-e2e/`.
|
||||
- The live transport runner already writes reports and observed-message
|
||||
artifacts under `.artifacts/qa-e2e/`.
|
||||
- Convex credential leases already provide exclusive access to shared live
|
||||
transport credentials.
|
||||
- The browser control service already supports screenshots, snapshots,
|
||||
|
||||
@@ -318,17 +318,17 @@ Matrix has a [dedicated page](/concepts/qa-matrix) because of its scenario count
|
||||
|
||||
These lanes register through `extensions/qa-lab/src/live-transports/shared/live-transport-cli.ts` and accept the same flags:
|
||||
|
||||
| Flag | Default | Description |
|
||||
| ------------------------------------- | -------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `--scenario <id>` | - | Run only this scenario. Repeatable. |
|
||||
| `--output-dir <path>` | `<repo>/.artifacts/qa-e2e/<transport>-<timestamp>` | Where reports, summaries, evidence, transport-specific artifacts, and the output log are written. Relative paths resolve against `--repo-root`. |
|
||||
| `--repo-root <path>` | `process.cwd()` | Repository root when invoking from a neutral cwd. |
|
||||
| `--sut-account <id>` | `sut` | Temporary account id inside the QA gateway config. |
|
||||
| `--provider-mode <mode>` | `live-frontier` | `mock-openai` or `live-frontier` (legacy `live-openai` still works). |
|
||||
| `--model <ref>` / `--alt-model <ref>` | provider default | Primary/alternate model refs. |
|
||||
| `--fast` | off | Provider fast mode where supported. |
|
||||
| `--credential-source <env\|convex>` | `env` | See [Convex credential pool](#convex-credential-pool). |
|
||||
| `--credential-role <maintainer\|ci>` | `ci` in CI, `maintainer` otherwise | Role used when `--credential-source convex`. |
|
||||
| Flag | Default | Description |
|
||||
| ------------------------------------- | -------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
|
||||
| `--scenario <id>` | - | Run only this scenario. Repeatable. |
|
||||
| `--output-dir <path>` | `<repo>/.artifacts/qa-e2e/<transport>-<timestamp>` | Where reports/summary/observed messages and the output log are written. Relative paths resolve against `--repo-root`. |
|
||||
| `--repo-root <path>` | `process.cwd()` | Repository root when invoking from a neutral cwd. |
|
||||
| `--sut-account <id>` | `sut` | Temporary account id inside the QA gateway config. |
|
||||
| `--provider-mode <mode>` | `live-frontier` | `mock-openai` or `live-frontier` (legacy `live-openai` still works). |
|
||||
| `--model <ref>` / `--alt-model <ref>` | provider default | Primary/alternate model refs. |
|
||||
| `--fast` | off | Provider fast mode where supported. |
|
||||
| `--credential-source <env\|convex>` | `env` | See [Convex credential pool](#convex-credential-pool). |
|
||||
| `--credential-role <maintainer\|ci>` | `ci` in CI, `maintainer` otherwise | Role used when `--credential-source convex`. |
|
||||
|
||||
Each lane exits non-zero on any failed scenario. `--allow-failures` writes artifacts without setting a failing exit code.
|
||||
|
||||
@@ -346,6 +346,10 @@ Required env when `--credential-source env`:
|
||||
- `OPENCLAW_QA_TELEGRAM_DRIVER_BOT_TOKEN`
|
||||
- `OPENCLAW_QA_TELEGRAM_SUT_BOT_TOKEN`
|
||||
|
||||
Optional:
|
||||
|
||||
- `OPENCLAW_QA_TELEGRAM_CAPTURE_CONTENT=1` keeps message bodies in observed-message artifacts (default redacts).
|
||||
|
||||
Scenarios (`extensions/qa-lab/src/live-transports/telegram/telegram-live.runtime.ts`):
|
||||
|
||||
- `telegram-canary`
|
||||
@@ -371,26 +375,26 @@ Output artifacts:
|
||||
|
||||
- `telegram-qa-report.md`
|
||||
- `qa-evidence.json` - evidence entries for the live transport checks, including profile, coverage, provider, channel, artifacts, result, and RTT fields.
|
||||
- `telegram-qa-observed-messages.json` - bodies redacted unless `OPENCLAW_QA_TELEGRAM_CAPTURE_CONTENT=1`.
|
||||
|
||||
Package Telegram runs use the same Telegram credential contract. Repeated RTT
|
||||
measurement is part of the normal package Telegram live lane; the RTT
|
||||
distribution is folded into `qa-evidence.json` under `result.timing` for the
|
||||
selected RTT check.
|
||||
Package RTT comparison uses the same Telegram credential contract while keeping
|
||||
its RTT sample controls on the RTT harness path:
|
||||
|
||||
```bash
|
||||
OPENCLAW_QA_CREDENTIAL_SOURCE=convex \
|
||||
pnpm test:docker:npm-telegram-live
|
||||
pnpm rtt openclaw@beta \
|
||||
--credential-source convex \
|
||||
--credential-role maintainer \
|
||||
--samples 20 \
|
||||
--sample-timeout-ms 30000
|
||||
```
|
||||
|
||||
When `OPENCLAW_QA_CREDENTIAL_SOURCE=convex` is set, the package live wrapper
|
||||
leases a `kind: "telegram"` credential, exports the leased group/driver/SUT bot
|
||||
env into the installed-package run, heartbeats the lease, and releases it on
|
||||
shutdown. The package wrapper defaults to 20 RTT checks of
|
||||
`telegram-mentioned-message-reply`, a 30s RTT timeout, and Convex role
|
||||
`maintainer` outside CI when Convex is selected. Override
|
||||
`OPENCLAW_NPM_TELEGRAM_RTT_SAMPLES`, `OPENCLAW_NPM_TELEGRAM_RTT_TIMEOUT_MS`,
|
||||
or `OPENCLAW_NPM_TELEGRAM_RTT_MAX_FAILURES` to tune RTT measurement without
|
||||
creating a separate RTT command or Telegram-specific summary format.
|
||||
When `--credential-source convex` is set, the RTT Docker wrapper leases a
|
||||
`kind: "telegram"` credential, exports the leased group/driver/SUT bot env into
|
||||
the installed-package run, heartbeats the lease, and releases it on shutdown.
|
||||
`--samples` and `--sample-timeout-ms` still feed
|
||||
`OPENCLAW_NPM_TELEGRAM_WARM_SAMPLES` and
|
||||
`OPENCLAW_NPM_TELEGRAM_SAMPLE_TIMEOUT_MS`, so `result.json` remains comparable
|
||||
across env-backed and Convex-backed RTT runs.
|
||||
|
||||
### Discord QA
|
||||
|
||||
@@ -914,6 +918,7 @@ When choosing focused proof for a touched behavior or file path, run `pnpm openc
|
||||
The match report searches scenario metadata, docs refs, code refs, coverage IDs, plugins, and provider requirements, then prints matching `qa suite --scenario ...` targets.
|
||||
Every `qa suite` scenario execution writes a `qa-evidence.json` artifact. Flow scenarios also write `qa-suite-summary.json` for existing suite/report tooling; scenarios that declare `execution.kind: vitest` or `execution.kind: playwright` run the matching test path and write `qa-vitest-report.md` or `qa-playwright-report.md` plus per-scenario logs.
|
||||
Treat it as a discovery aid, not a gate replacement; the selected scenario still needs the right provider mode, live transport, Multipass, Testbox, or release lane for the behavior under test.
|
||||
For scorecard context, see [Maturity tests](/reference/maturity-tests).
|
||||
|
||||
For character and style checks, run the same scenario across multiple live model
|
||||
refs and write a judged Markdown report:
|
||||
@@ -971,6 +976,7 @@ When no `--judge-model` is passed, the judges default to
|
||||
## Related docs
|
||||
|
||||
- [Matrix QA](/concepts/qa-matrix)
|
||||
- [Maturity tests](/reference/maturity-tests)
|
||||
- [Personal agent benchmark pack](/concepts/personal-agent-benchmark-pack)
|
||||
- [QA Channel](/channels/qa-channel)
|
||||
- [Testing](/help/testing)
|
||||
|
||||
@@ -1845,6 +1845,7 @@
|
||||
"pages": [
|
||||
"reference/RELEASING",
|
||||
"reference/full-release-validation",
|
||||
"reference/maturity-tests",
|
||||
"reference/release-performance-sweep",
|
||||
"reference/test",
|
||||
"ci",
|
||||
|
||||
@@ -21,6 +21,7 @@ of Docker runners. This doc is a "how we test" guide:
|
||||
- [QA overview](/concepts/qa-e2e-automation) - architecture, command surface, scenario authoring.
|
||||
- [Matrix QA](/concepts/qa-matrix) - reference for `pnpm openclaw qa matrix`.
|
||||
- [QA channel](/channels/qa-channel) - the synthetic transport plugin used by repo-backed scenarios.
|
||||
- [Maturity tests](/reference/maturity-tests) - how QA evidence maps to scorecard coverage.
|
||||
|
||||
This page covers running the regular test suites and Docker/Parallels runners. The QA-specific runners section below ([QA-specific runners](#qa-specific-runners)) lists the concrete `qa` invocations and points back at the references above.
|
||||
</Note>
|
||||
@@ -218,27 +219,17 @@ inside every shard.
|
||||
`OPENCLAW_NPM_TELEGRAM_PACKAGE_TGZ=/path/to/openclaw-current.tgz` or
|
||||
`OPENCLAW_CURRENT_PACKAGE_TGZ` to test a resolved local tarball instead of
|
||||
installing from the registry.
|
||||
- Emits repeated RTT timing in `qa-evidence.json` by default with
|
||||
`OPENCLAW_NPM_TELEGRAM_RTT_SAMPLES=20`. Override
|
||||
`OPENCLAW_NPM_TELEGRAM_RTT_SAMPLES`,
|
||||
`OPENCLAW_NPM_TELEGRAM_RTT_TIMEOUT_MS`, or
|
||||
`OPENCLAW_NPM_TELEGRAM_RTT_MAX_FAILURES` to tune the RTT run.
|
||||
`OPENCLAW_NPM_TELEGRAM_RTT_CHECKS` accepts a comma-separated list of
|
||||
Telegram QA check IDs to sample; when unset, the default RTT-capable check
|
||||
is `telegram-mentioned-message-reply`.
|
||||
- Uses the same Telegram env credentials or Convex credential source as
|
||||
`pnpm openclaw qa telegram`. For CI/release automation, set
|
||||
`OPENCLAW_NPM_TELEGRAM_CREDENTIAL_SOURCE=convex` plus
|
||||
`OPENCLAW_QA_CONVEX_SITE_URL` and a role secret. If
|
||||
`OPENCLAW_QA_CONVEX_SITE_URL` and the role secret. If
|
||||
`OPENCLAW_QA_CONVEX_SITE_URL` and a Convex role secret are present in CI,
|
||||
the Docker wrapper selects Convex automatically.
|
||||
- The wrapper validates Telegram or Convex credential env on the host before
|
||||
Docker build/install work. Set `OPENCLAW_NPM_TELEGRAM_SKIP_CREDENTIAL_PREFLIGHT=1`
|
||||
only when deliberately debugging pre-credential setup.
|
||||
- `OPENCLAW_NPM_TELEGRAM_CREDENTIAL_ROLE=ci|maintainer` overrides the shared
|
||||
`OPENCLAW_QA_CREDENTIAL_ROLE` for this lane only. When Convex credentials
|
||||
are selected and no role is set, the wrapper uses `ci` in CI and
|
||||
`maintainer` outside CI.
|
||||
`OPENCLAW_QA_CREDENTIAL_ROLE` for this lane only.
|
||||
- GitHub Actions exposes this lane as the manual maintainer workflow
|
||||
`NPM Telegram Beta E2E`. It does not run on merge. The workflow uses the
|
||||
`qa-live-shared` environment and Convex CI credential leases.
|
||||
@@ -354,11 +345,11 @@ gh workflow run package-acceptance.yml --ref main \
|
||||
want artifacts without a failing exit code.
|
||||
- Requires two distinct bots in the same private group, with the SUT bot exposing a Telegram username.
|
||||
- For stable bot-to-bot observation, enable Bot-to-Bot Communication Mode in `@BotFather` for both bots and ensure the driver bot can observe group bot traffic.
|
||||
- Writes a Telegram QA report, summary, and `qa-evidence.json` under `.artifacts/qa-e2e/...`. Replying scenarios include RTT from driver send request to observed SUT reply.
|
||||
- Writes a Telegram QA report, summary, and observed-messages artifact under `.artifacts/qa-e2e/...`. Replying scenarios include RTT from driver send request to observed SUT reply.
|
||||
|
||||
`Mantis Telegram Live` is the PR-evidence wrapper around this lane. It runs the
|
||||
candidate ref with Convex-leased Telegram credentials, renders the redacted QA
|
||||
report/evidence bundle in a Crabbox desktop browser, records MP4 evidence,
|
||||
candidate ref with Convex-leased Telegram credentials, renders the redacted
|
||||
observed-message transcript in a Crabbox desktop browser, records MP4 evidence,
|
||||
generates a motion-trimmed GIF, uploads the artifact bundle, and posts inline PR
|
||||
evidence through the Mantis GitHub App when `pr_number` is set. Maintainers can
|
||||
start it from the Actions UI through `Mantis Scenario` (`scenario_id:
|
||||
|
||||
@@ -143,39 +143,12 @@ The native Codex app-server harness supports context engines that require
|
||||
pre-prompt assembly. Generic CLI backends, including `codex-cli`, do not provide
|
||||
that host capability.
|
||||
|
||||
Codex thread bindings live in OpenClaw's SQLite plugin state and use the stable
|
||||
agent-scoped OpenClaw session key, or an opaque conversation-binding id, as
|
||||
their owner. Physical session ids fence delayed cleanup but may rotate without
|
||||
losing the Codex thread. Context-engine compaction adopts the successor id
|
||||
before continuing native Codex compaction. The bounded store rejects a new
|
||||
binding at its safety limit instead of evicting an existing thread's continuity
|
||||
record.
|
||||
Conversation binds create or resume their Codex thread on the first bound
|
||||
message after channel approval; an abandoned approval consumes no thread row.
|
||||
That first message carries the prepared thread directly into its turn.
|
||||
Subsequent messages use a metadata-only resume to subscribe the shared client,
|
||||
then unsubscribe after the turn completes.
|
||||
The runtime does not poll transcript-adjacent binding files. Upgrades from
|
||||
releases that used `*.jsonl.codex-app-server.json` sidecars migrate them during
|
||||
normal startup preflight. `openclaw doctor --fix` can run the same migration
|
||||
manually.
|
||||
Successfully matched sidecars are archived before the new runtime resumes their
|
||||
threads. Migration imports durable thread ownership only; it does not infer
|
||||
Codex context usage from OpenClaw counters or crawl Codex rollout files. For
|
||||
agent-session harness bindings, the next resume attempts to restore a cached
|
||||
native snapshot when Codex has one, and ongoing turns persist the current-context
|
||||
usage reported by app-server notifications, not the cumulative thread lifetime
|
||||
total. Conversation bindings
|
||||
keep metadata-only resumes and leave continuity and compaction with the native
|
||||
Codex thread. Conflicting or ambiguous sidecars stay in place with a warning for
|
||||
operator review.
|
||||
|
||||
For Codex-backed agents, `/compact` starts native Codex app-server compaction on
|
||||
the bound thread. OpenClaw bounds the request-acceptance RPC but does not wait
|
||||
for compaction completion, restart the shared app-server, or fall back to a
|
||||
context-engine or public OpenAI summarizer. If the native Codex thread binding
|
||||
is missing or stale, the command fails closed so the operator sees the real
|
||||
runtime boundary instead of silently switching compaction backends.
|
||||
the bound thread. OpenClaw does not wait for completion, impose an OpenClaw
|
||||
timeout, restart the shared app-server, or fall back to a context-engine or
|
||||
public OpenAI summarizer. If the native Codex thread binding is missing or
|
||||
stale, the command fails closed so the operator sees the real runtime boundary
|
||||
instead of silently switching compaction backends.
|
||||
|
||||
```json5
|
||||
{
|
||||
|
||||
@@ -515,7 +515,6 @@ API key auth, and dynamic model resolution.
|
||||
|
||||
- `openclaw/plugin-sdk/provider-model-shared` - `ProviderReplayFamily`, `buildProviderReplayFamilyHooks(...)`, and the raw replay builders (`buildOpenAICompatibleReplayPolicy`, `buildAnthropicReplayPolicyForModel`, `buildGoogleGeminiReplayPolicy`, `buildHybridAnthropicOrOpenAIReplayPolicy`). Also exports Gemini replay helpers (`sanitizeGoogleGeminiReplayHistory`, `resolveTaggedReasoningOutputMode`) and endpoint/model helpers (`resolveProviderEndpoint`, `normalizeProviderId`, `normalizeGooglePreviewModelId`).
|
||||
- `openclaw/plugin-sdk/provider-stream` - `ProviderStreamFamily`, `buildProviderStreamFamilyHooks(...)`, `composeProviderStreamWrappers(...)`, plus the shared OpenAI/Codex wrappers (`createOpenAIAttributionHeadersWrapper`, `createOpenAIFastModeWrapper`, `createOpenAIServiceTierWrapper`, `createOpenAIResponsesContextManagementWrapper`, `createCodexNativeWebSearchWrapper`), DeepSeek V4 OpenAI-compatible wrapper (`createDeepSeekV4OpenAICompatibleThinkingWrapper`), Anthropic Messages thinking prefill cleanup (`createAnthropicThinkingPrefillPayloadWrapper`), plain-text tool-call compat (`createPlainTextToolCallCompatWrapper`), and shared proxy/provider wrappers (`createOpenRouterWrapper`, `createToolStreamWrapper`, `createMinimaxFastModeWrapper`).
|
||||
- `openclaw/plugin-sdk/provider-stream-shared` - lightweight payload and event wrappers for hot provider paths, including `createOpenAICompatibleCompletionsThinkingOffWrapper`, `createPayloadPatchStreamWrapper`, and `createPlainTextToolCallCompatWrapper`.
|
||||
- `openclaw/plugin-sdk/provider-tools` - `ProviderToolCompatFamily`, `buildProviderToolCompatFamilyHooks("deepseek" | "gemini" | "openai")`, and underlying provider schema helpers.
|
||||
|
||||
For Gemini-family providers, keep the reasoning-output mode aligned with
|
||||
|
||||
@@ -164,7 +164,7 @@ and pairing-path families.
|
||||
| `plugin-sdk/provider-tools` | `ProviderToolCompatFamily`, `buildProviderToolCompatFamilyHooks`, and DeepSeek/Gemini/OpenAI schema cleanup + diagnostics |
|
||||
| `plugin-sdk/provider-usage` | Provider usage snapshot types, shared usage fetch helpers, and provider fetchers such as `fetchClaudeUsage` |
|
||||
| `plugin-sdk/provider-stream` | `ProviderStreamFamily`, `buildProviderStreamFamilyHooks`, `composeProviderStreamWrappers`, stream wrapper types, plain-text tool-call compat, and shared Anthropic/Bedrock/DeepSeek V4/Google/Kilocode/Moonshot/OpenAI/OpenRouter/Z.A.I/MiniMax/Copilot wrapper helpers |
|
||||
| `plugin-sdk/provider-stream-shared` | Public shared provider stream wrapper helpers including `composeProviderStreamWrappers`, `createOpenAICompatibleCompletionsThinkingOffWrapper`, `createPlainTextToolCallCompatWrapper`, `createPayloadPatchStreamWrapper`, `createToolStreamWrapper`, and Anthropic/DeepSeek/OpenAI-compatible stream utilities |
|
||||
| `plugin-sdk/provider-stream-shared` | Public shared provider stream wrapper helpers including `composeProviderStreamWrappers`, `createPlainTextToolCallCompatWrapper`, `createPayloadPatchStreamWrapper`, `createToolStreamWrapper`, and Anthropic/DeepSeek/OpenAI-compatible stream utilities |
|
||||
| `plugin-sdk/provider-transport-runtime` | Native provider transport helpers such as guarded fetch, transport message transforms, and writable transport event streams |
|
||||
| `plugin-sdk/provider-onboard` | Onboarding config patch helpers |
|
||||
| `plugin-sdk/global-singleton` | Process-local singleton/map/cache helpers |
|
||||
@@ -236,7 +236,6 @@ usage endpoint failed or returned no usable usage data.
|
||||
| `plugin-sdk/config-contracts` | Focused type-only config surface for plugin config shapes such as `OpenClawConfig` and channel/provider config types |
|
||||
| `plugin-sdk/plugin-config-runtime` | Runtime plugin-config lookup helpers such as `requireRuntimeConfig`, `resolvePluginConfigObject`, and `resolveLivePluginConfigObject` |
|
||||
| `plugin-sdk/config-mutation` | Transactional config mutation helpers such as `mutateConfigFile`, `replaceConfigFile`, and `logConfigUpdated` |
|
||||
| `plugin-sdk/message-tool-delivery-hints` | Shared message-tool delivery metadata hint strings |
|
||||
| `plugin-sdk/runtime-config-snapshot` | Current process config snapshot helpers such as `getRuntimeConfig`, `getRuntimeConfigSnapshot`, and test snapshot setters |
|
||||
| `plugin-sdk/telegram-command-config` | Telegram command-name/description normalization and duplicate/conflict checks, even when the bundled Telegram contract surface is unavailable |
|
||||
| `plugin-sdk/text-autolink-runtime` | File-reference autolink detection without the broad text barrel |
|
||||
|
||||
49
docs/reference/maturity-tests.md
Normal file
49
docs/reference/maturity-tests.md
Normal file
@@ -0,0 +1,49 @@
|
||||
---
|
||||
summary: "How OpenClaw maps the maturity scorecard to QA coverage and evidence."
|
||||
read_when:
|
||||
- Reading QA scorecard coverage
|
||||
- Adding coverage IDs to QA scenarios
|
||||
- Finding evidence for a maturity category
|
||||
title: "Maturity tests"
|
||||
---
|
||||
|
||||
Maturity tests are QA evidence linked to the OpenClaw maturity scorecard. They help maintainers see which scorecard categories already have runnable proof and which ones still need coverage.
|
||||
|
||||
The scorecard has two source files:
|
||||
|
||||
- `taxonomy.yaml` defines surfaces, categories, maturity levels, profile membership, and feature coverage IDs.
|
||||
- `docs/maturity-scores.yaml` records the current score snapshot and LTS status.
|
||||
|
||||
QA scenarios connect to the scorecard by using the same coverage IDs:
|
||||
|
||||
- `qa/scenarios/**/*.md` stores `coverage.primary` and `coverage.secondary` IDs.
|
||||
- `extensions/qa-lab` joins scenario coverage to the taxonomy report.
|
||||
- `qa suite` writes `qa-evidence.json` for the scenarios it runs.
|
||||
|
||||
## Find Coverage
|
||||
|
||||
Start with the coverage inventory when a requirement needs a runnable mapping:
|
||||
|
||||
```bash
|
||||
pnpm openclaw qa coverage --match <surface-or-coverage-id>
|
||||
pnpm openclaw qa coverage --json --match <surface-or-coverage-id>
|
||||
```
|
||||
|
||||
The report includes a **Scorecard Taxonomy** section, profile membership, mapped coverage IDs, evidence refs, and matching `qa suite --scenario ...` commands.
|
||||
|
||||
## Add Coverage
|
||||
|
||||
When a category needs new evidence:
|
||||
|
||||
1. Start from the matching `taxonomy.yaml` surface and category.
|
||||
2. Reuse an existing feature `coverageIds` value, or add a broad behavior-shaped ID.
|
||||
3. Add that ID to `coverage.primary` in the scenario that proves it.
|
||||
4. Use `coverage.secondary` only for supporting evidence.
|
||||
5. Add useful `docsRefs` and `codeRefs` to the scenario.
|
||||
6. Run `pnpm openclaw qa coverage --match <coverage-id>` and then run the smallest relevant scenario or test lane.
|
||||
|
||||
## Related docs
|
||||
|
||||
- [QA overview](/concepts/qa-e2e-automation)
|
||||
- [Testing](/help/testing)
|
||||
- [Matrix QA](/concepts/qa-matrix)
|
||||
@@ -20,7 +20,6 @@ Scope includes:
|
||||
- Thinking signature cleanup
|
||||
- Image payload sanitization
|
||||
- Blank text-block cleanup before provider replay
|
||||
- Incomplete reasoning-only length-turn cleanup before provider replay
|
||||
- User-input provenance tagging (for inter-session routed prompts)
|
||||
- Empty assistant error-turn repair for Bedrock Converse replay
|
||||
|
||||
@@ -92,21 +91,6 @@ Implementation:
|
||||
|
||||
---
|
||||
|
||||
## Global rule: incomplete reasoning-only turns
|
||||
|
||||
Assistant turns that hit the provider output limit with only thinking or
|
||||
redacted-thinking content are omitted from the in-memory replay copy. Such turns
|
||||
contain incomplete provider state and may carry a partial thinking signature.
|
||||
|
||||
Empty length turns remain unchanged, as do length turns with visible text, tool
|
||||
calls, or unknown content blocks. Stored transcripts are not rewritten.
|
||||
|
||||
Implementation:
|
||||
|
||||
- `normalizeAssistantReplayContent` in `src/agents/embedded-agent-runner/replay-history.ts`
|
||||
|
||||
---
|
||||
|
||||
## Global rule: inter-session input provenance
|
||||
|
||||
When an agent sends a prompt into another session via `sessions_send` (including
|
||||
|
||||
@@ -13,12 +13,7 @@ CLI, and scripting patterns (snapshots, refs, waits, debug flows).
|
||||
|
||||
## Control API (optional)
|
||||
|
||||
For local integrations only, the Gateway exposes a small loopback HTTP API.
|
||||
This standalone server is opt-in — set the environment variable
|
||||
`OPENCLAW_EAGER_BROWSER_CONTROL_SERVER=1` in the gateway service environment
|
||||
and restart the gateway before the HTTP endpoints become available. Without
|
||||
this variable the browser control runtime still works through the CLI and
|
||||
agent tools, but nothing listens on the loopback control port.
|
||||
For local integrations only, the Gateway exposes a small loopback HTTP API:
|
||||
|
||||
- Status/start/stop: `GET /`, `POST /start`, `POST /stop`
|
||||
- Tabs: `GET /tabs`, `POST /tabs/open`, `POST /tabs/focus`, `DELETE /tabs/:targetId`
|
||||
|
||||
@@ -1,44 +1,6 @@
|
||||
// Codex tests cover doctor contract api plugin behavior.
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { PluginStateKeyedStore } from "openclaw/plugin-sdk/plugin-state-runtime";
|
||||
import {
|
||||
createPluginStateKeyedStoreForTests,
|
||||
resetPluginStateStoreForTests,
|
||||
} from "openclaw/plugin-sdk/plugin-state-test-runtime";
|
||||
import type {
|
||||
OpenKeyedStoreOptions,
|
||||
PluginDoctorStateMigrationContext,
|
||||
} from "openclaw/plugin-sdk/runtime-doctor";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
legacyConfigRules,
|
||||
normalizeCompatibilityConfig,
|
||||
stateMigrations,
|
||||
} from "./doctor-contract-api.js";
|
||||
import {
|
||||
bindingStoreKey,
|
||||
CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
type StoredCodexAppServerBinding,
|
||||
} from "./src/app-server/session-binding.js";
|
||||
import { legacyCodexConversationBindingId } from "./src/conversation-binding-data.js";
|
||||
|
||||
function createDoctorContext(env: NodeJS.ProcessEnv): PluginDoctorStateMigrationContext {
|
||||
return {
|
||||
openPluginStateKeyedStore<T>(options: OpenKeyedStoreOptions) {
|
||||
return createPluginStateKeyedStoreForTests<T>("codex", {
|
||||
...options,
|
||||
env: options.env ?? env,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
resetPluginStateStoreForTests();
|
||||
});
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { legacyConfigRules, normalizeCompatibilityConfig } from "./doctor-contract-api.js";
|
||||
|
||||
describe("codex doctor contract", () => {
|
||||
it("reports the retired dynamic tools profile config key", () => {
|
||||
@@ -80,856 +42,4 @@ describe("codex doctor contract", () => {
|
||||
});
|
||||
expect(original.plugins.entries.codex.config).toHaveProperty("codexDynamicToolsProfile");
|
||||
});
|
||||
|
||||
it("imports shipped binding sidecars under session and legacy conversation identities", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-"));
|
||||
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
|
||||
const sessionsDir = path.join(stateDir, "agents", "main", "sessions");
|
||||
const transcriptPath = path.join(sessionsDir, "session-current.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
const legacyBinding = {
|
||||
schemaVersion: 1,
|
||||
threadId: "thread-1",
|
||||
sessionFile: transcriptPath,
|
||||
updatedAt: "2026-01-01T00:00:00.000Z",
|
||||
};
|
||||
await fs.mkdir(sessionsDir, { recursive: true });
|
||||
await fs.writeFile(transcriptPath, '{"type":"session","id":"session-current"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:session-1": {
|
||||
sessionId: "session-current",
|
||||
sessionFile: "session-current.jsonl",
|
||||
totalTokens: 42_000,
|
||||
totalTokensFresh: true,
|
||||
contextTokens: 258_400,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(sidecarPath, JSON.stringify(legacyBinding), "utf8");
|
||||
const params = {
|
||||
config: {},
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
};
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await expect(migration.detectLegacyState(params)).resolves.toMatchObject({
|
||||
preview: [expect.stringContaining("legacy sidecar")],
|
||||
});
|
||||
await expect(migration.migrateLegacyState(params)).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1")],
|
||||
warnings: [],
|
||||
});
|
||||
|
||||
const store = createDoctorContext(env).openPluginStateKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "session-current",
|
||||
sessionKey: "agent:main:session-1",
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
sessionId: "session-current",
|
||||
binding: { threadId: "thread-1" },
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(transcriptPath),
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
binding: {
|
||||
threadId: "thread-1",
|
||||
cwd: "",
|
||||
historyCoveredThrough: expect.any(String),
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(transcriptPath),
|
||||
}),
|
||||
),
|
||||
).resolves.not.toHaveProperty("binding.nativeContextUsage");
|
||||
await expect(fs.access(`${sidecarPath}.migrated`)).resolves.toBeUndefined();
|
||||
await expect(
|
||||
fs.readFile(path.join(sessionsDir, "sessions.json"), "utf8").then(JSON.parse),
|
||||
).resolves.toMatchObject({
|
||||
"agent:main:session-1": { sessionId: "session-current", agentHarnessId: "codex" },
|
||||
});
|
||||
|
||||
await fs.rm(`${sidecarPath}.migrated`);
|
||||
await fs.writeFile(sidecarPath, JSON.stringify(legacyBinding), "utf8");
|
||||
await expect(migration.migrateLegacyState(params)).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1")],
|
||||
warnings: [],
|
||||
});
|
||||
await expect(fs.access(`${sidecarPath}.migrated`)).resolves.toBeUndefined();
|
||||
|
||||
const resetTranscript = path.join(sessionsDir, "session-before-reset.jsonl");
|
||||
const resetSidecar = `${resetTranscript}.codex-app-server.json`;
|
||||
await fs.writeFile(resetTranscript, '{"type":"session","id":"session-before-reset"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
resetSidecar,
|
||||
JSON.stringify({ schemaVersion: 1, threadId: "thread-before-reset" }),
|
||||
"utf8",
|
||||
);
|
||||
await expect(migration.migrateLegacyState(params)).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1 safe")],
|
||||
warnings: [expect.stringContaining("session owner could not be resolved")],
|
||||
});
|
||||
await expect(fs.access(resetSidecar)).resolves.toBeUndefined();
|
||||
await fs.rm(resetSidecar);
|
||||
|
||||
const conflictingTranscript = path.join(sessionsDir, "session-2.jsonl");
|
||||
const conflictingSidecar = `${conflictingTranscript}.codex-app-server.json`;
|
||||
await fs.writeFile(conflictingTranscript, '{"type":"session","id":"session-2"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
conflictingSidecar,
|
||||
JSON.stringify({ schemaVersion: 1, threadId: "legacy-thread" }),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:session-1": {
|
||||
sessionId: "session-1",
|
||||
sessionFile: "session-1.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
"agent:main:session-2": {
|
||||
sessionId: "session-2",
|
||||
sessionFile: "session-2.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
const conflictingSessionKey = bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "session-2",
|
||||
sessionKey: "agent:main:session-2",
|
||||
});
|
||||
await store.register(conflictingSessionKey, {
|
||||
version: 1,
|
||||
state: "active",
|
||||
binding: {
|
||||
threadId: "legacy-thread",
|
||||
cwd: "/repo",
|
||||
historyCoveredThrough: "2026-01-01T00:00:00.000Z",
|
||||
},
|
||||
});
|
||||
|
||||
await expect(migration.migrateLegacyState(params)).resolves.toMatchObject({
|
||||
changes: [],
|
||||
warnings: [
|
||||
expect.stringContaining(`canonical plugin state changed at ${conflictingSessionKey}`),
|
||||
],
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(conflictingTranscript),
|
||||
}),
|
||||
),
|
||||
).resolves.toBeUndefined();
|
||||
await expect(fs.access(conflictingSidecar)).resolves.toBeUndefined();
|
||||
await fs.rm(conflictingSidecar);
|
||||
|
||||
const inverseTranscript = path.join(sessionsDir, "session-3.jsonl");
|
||||
const inverseSidecar = `${inverseTranscript}.codex-app-server.json`;
|
||||
const inverseConversationKey = bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(inverseTranscript),
|
||||
});
|
||||
await fs.writeFile(inverseTranscript, '{"type":"session","id":"session-3"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:session-3": {
|
||||
sessionId: "session-3",
|
||||
sessionFile: "session-3.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
inverseSidecar,
|
||||
JSON.stringify({ schemaVersion: 1, threadId: "session-thread" }),
|
||||
"utf8",
|
||||
);
|
||||
await store.register(inverseConversationKey, {
|
||||
version: 1,
|
||||
state: "active",
|
||||
binding: { threadId: "conversation-thread", cwd: "/repo" },
|
||||
});
|
||||
|
||||
await expect(migration.migrateLegacyState(params)).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1")],
|
||||
warnings: [],
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "session-3",
|
||||
sessionKey: "agent:main:session-3",
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
sessionId: "session-3",
|
||||
binding: { threadId: "conversation-thread" },
|
||||
});
|
||||
await expect(store.lookup(inverseConversationKey)).resolves.toMatchObject({
|
||||
state: "active",
|
||||
binding: { threadId: "conversation-thread" },
|
||||
});
|
||||
await expect(fs.access(`${inverseSidecar}.migrated`)).resolves.toBeUndefined();
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("does not publish Codex session ownership before every binding row persists", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-order-"));
|
||||
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
|
||||
const sessionsDir = path.join(stateDir, "agents", "main", "sessions");
|
||||
const transcriptPath = path.join(sessionsDir, "session-order.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
const storePath = path.join(sessionsDir, "sessions.json");
|
||||
await fs.mkdir(sessionsDir, { recursive: true });
|
||||
await fs.writeFile(transcriptPath, '{"type":"session","id":"session-order"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
storePath,
|
||||
JSON.stringify({
|
||||
"agent:main:order": {
|
||||
sessionId: "session-order",
|
||||
sessionFile: "session-order.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
sidecarPath,
|
||||
JSON.stringify({ schemaVersion: 1, threadId: "thread-order" }),
|
||||
"utf8",
|
||||
);
|
||||
const store = createPluginStateKeyedStoreForTests<StoredCodexAppServerBinding>("codex", {
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
env,
|
||||
});
|
||||
const registerIfAbsent = store.registerIfAbsent.bind(store);
|
||||
let registerCalls = 0;
|
||||
const failingStore: PluginStateKeyedStore<StoredCodexAppServerBinding> = {
|
||||
...store,
|
||||
async registerIfAbsent(key, value, opts) {
|
||||
registerCalls++;
|
||||
if (registerCalls === 2) {
|
||||
throw new Error("injected session binding write failure");
|
||||
}
|
||||
return await registerIfAbsent(key, value, opts);
|
||||
},
|
||||
};
|
||||
const failingContext: PluginDoctorStateMigrationContext = {
|
||||
openPluginStateKeyedStore<T>() {
|
||||
return failingStore as unknown as PluginStateKeyedStore<T>;
|
||||
},
|
||||
};
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config: {},
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: failingContext,
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1 safe")],
|
||||
warnings: [expect.stringContaining("injected session binding write failure")],
|
||||
});
|
||||
await expect(fs.readFile(storePath, "utf8").then(JSON.parse)).resolves.toMatchObject({
|
||||
"agent:main:order": { sessionId: "session-order" },
|
||||
});
|
||||
expect(
|
||||
(JSON.parse(await fs.readFile(storePath, "utf8")) as Record<string, Record<string, unknown>>)[
|
||||
"agent:main:order"
|
||||
],
|
||||
).not.toHaveProperty("agentHarnessId");
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "session-order",
|
||||
sessionKey: "agent:main:order",
|
||||
}),
|
||||
),
|
||||
).resolves.toBeUndefined();
|
||||
await expect(fs.access(sidecarPath)).resolves.toBeUndefined();
|
||||
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config: {},
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1")],
|
||||
warnings: [],
|
||||
});
|
||||
await expect(fs.readFile(storePath, "utf8").then(JSON.parse)).resolves.toMatchObject({
|
||||
"agent:main:order": {
|
||||
sessionId: "session-order",
|
||||
agentHarnessId: "codex",
|
||||
},
|
||||
});
|
||||
await expect(fs.access(`${sidecarPath}.migrated`)).resolves.toBeUndefined();
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("retains a shipped binding when its session now belongs to another harness", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-owner-"));
|
||||
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
|
||||
const sessionsDir = path.join(stateDir, "agents", "main", "sessions");
|
||||
const transcriptPath = path.join(sessionsDir, "session-foreign.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
await fs.mkdir(sessionsDir, { recursive: true });
|
||||
await fs.writeFile(transcriptPath, '{"type":"session","id":"session-foreign"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:foreign": {
|
||||
sessionId: "session-foreign",
|
||||
sessionFile: "session-foreign.jsonl",
|
||||
agentHarnessId: "openclaw",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
sidecarPath,
|
||||
JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
threadId: "thread-foreign",
|
||||
sessionFile: transcriptPath,
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config: {},
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
changes: [],
|
||||
warnings: [expect.stringContaining("owned by agent harness openclaw")],
|
||||
});
|
||||
await expect(fs.access(sidecarPath)).resolves.toBeUndefined();
|
||||
const store = createDoctorContext(env).openPluginStateKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "session-foreign",
|
||||
sessionKey: "agent:main:foreign",
|
||||
}),
|
||||
),
|
||||
).resolves.toBeUndefined();
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("imports sidecars from the pre-agent session directory before core moves it", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-legacy-"));
|
||||
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
|
||||
const sessionsDir = path.join(stateDir, "sessions");
|
||||
const transcriptPath = path.join(sessionsDir, "legacy-session.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
await fs.mkdir(sessionsDir, { recursive: true });
|
||||
await fs.writeFile(transcriptPath, '{"type":"session","id":"legacy-session"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:legacy": {
|
||||
sessionId: "legacy-session",
|
||||
sessionFile: "legacy-session.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
sidecarPath,
|
||||
JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
threadId: "legacy-thread",
|
||||
sessionFile: transcriptPath,
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
const params = {
|
||||
config: {},
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
};
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await expect(migration.migrateLegacyState(params)).resolves.toMatchObject({ warnings: [] });
|
||||
|
||||
const store = createDoctorContext(env).openPluginStateKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "legacy-session",
|
||||
sessionKey: "agent:main:legacy",
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
sessionId: "legacy-session",
|
||||
binding: { threadId: "legacy-thread" },
|
||||
});
|
||||
await expect(fs.access(`${sidecarPath}.migrated`)).resolves.toBeUndefined();
|
||||
await expect(
|
||||
fs.readFile(path.join(sessionsDir, "sessions.json"), "utf8").then(JSON.parse),
|
||||
).resolves.toMatchObject({
|
||||
"agent:main:legacy": { sessionId: "legacy-session", agentHarnessId: "codex" },
|
||||
});
|
||||
});
|
||||
|
||||
it("uses the session index when a shipped sidecar transcript is missing", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-"));
|
||||
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
|
||||
const sessionsDir = path.join(stateDir, "agents", "main", "sessions");
|
||||
const transcriptPath = path.join(sessionsDir, "missing.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
await fs.mkdir(sessionsDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:missing": {
|
||||
sessionId: "session-missing",
|
||||
sessionFile: "missing.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
sidecarPath,
|
||||
JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
threadId: "thread-legacy-conversation",
|
||||
sessionFile: transcriptPath,
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config: {},
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1")],
|
||||
warnings: [],
|
||||
});
|
||||
|
||||
const store = createDoctorContext(env).openPluginStateKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(transcriptPath),
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
binding: { threadId: "thread-legacy-conversation" },
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "session-missing",
|
||||
sessionKey: "agent:main:missing",
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
sessionId: "session-missing",
|
||||
binding: { threadId: "thread-legacy-conversation" },
|
||||
});
|
||||
await expect(fs.access(`${sidecarPath}.migrated`)).resolves.toBeUndefined();
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("imports a binding without crawling Codex rollout files", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-"));
|
||||
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
|
||||
const sessionsDir = path.join(stateDir, "agents", "main", "sessions");
|
||||
const transcriptPath = path.join(sessionsDir, "session-fresh.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
await fs.mkdir(sessionsDir, { recursive: true });
|
||||
await fs.writeFile(transcriptPath, '{"type":"session","id":"session-fresh"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:fresh": {
|
||||
sessionId: "session-fresh",
|
||||
sessionFile: "session-fresh.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
sidecarPath,
|
||||
JSON.stringify({ schemaVersion: 1, threadId: "thread-without-rollout" }),
|
||||
"utf8",
|
||||
);
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config: {},
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
changes: [expect.stringContaining("Migrated 1")],
|
||||
warnings: [],
|
||||
});
|
||||
|
||||
const store = createDoctorContext(env).openPluginStateKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
});
|
||||
const targetKey = bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(transcriptPath),
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "main",
|
||||
sessionId: "session-fresh",
|
||||
sessionKey: "agent:main:fresh",
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
sessionId: "session-fresh",
|
||||
binding: { threadId: "thread-without-rollout" },
|
||||
});
|
||||
await expect(store.lookup(targetKey)).resolves.toMatchObject({
|
||||
state: "active",
|
||||
binding: { threadId: "thread-without-rollout" },
|
||||
});
|
||||
await expect(fs.access(`${sidecarPath}.migrated`)).resolves.toBeUndefined();
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("retains an ambiguous sidecar and converges after its owner resolves", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-"));
|
||||
const env = { ...process.env, HOME: stateDir, OPENCLAW_STATE_DIR: stateDir };
|
||||
const config = {
|
||||
agents: { list: [{ id: "alpha" }, { id: "beta" }] },
|
||||
session: { store: "~/shared/sessions.json" },
|
||||
};
|
||||
const sessionsDir = path.join(stateDir, "shared");
|
||||
const transcriptPath = path.join(sessionsDir, "ambiguous.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
await fs.mkdir(sessionsDir, { recursive: true });
|
||||
await fs.writeFile(transcriptPath, '{"type":"message"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
sidecarPath,
|
||||
JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
threadId: "thread-ambiguous",
|
||||
sessionFile: transcriptPath,
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config,
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1 safe")],
|
||||
warnings: [expect.stringContaining("session owner could not be resolved")],
|
||||
});
|
||||
|
||||
const store = createDoctorContext(env).openPluginStateKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(transcriptPath),
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({ state: "active", binding: { threadId: "thread-ambiguous" } });
|
||||
await expect(fs.access(sidecarPath)).resolves.toBeUndefined();
|
||||
|
||||
const conversationKey = bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(transcriptPath),
|
||||
});
|
||||
const imported = await store.lookup(conversationKey);
|
||||
if (imported?.state !== "active") {
|
||||
throw new Error("missing imported Codex conversation binding");
|
||||
}
|
||||
await store.register(conversationKey, {
|
||||
...imported,
|
||||
binding: { ...imported.binding, threadId: "thread-recovered" },
|
||||
});
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config,
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
changes: [],
|
||||
warnings: [expect.stringContaining("session owner could not be resolved")],
|
||||
});
|
||||
await expect(store.lookup(conversationKey)).resolves.toMatchObject({
|
||||
state: "active",
|
||||
binding: { threadId: "thread-recovered" },
|
||||
});
|
||||
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:alpha:ambiguous": {
|
||||
sessionId: "session-ambiguous",
|
||||
sessionFile: "ambiguous.jsonl",
|
||||
totalTokens: 12_345,
|
||||
totalTokensFresh: true,
|
||||
contextTokens: 128_000,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await expect(
|
||||
migration.migrateLegacyState({
|
||||
config,
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
changes: [expect.stringContaining("Migrated 1")],
|
||||
warnings: [],
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "alpha",
|
||||
sessionId: "session-ambiguous",
|
||||
sessionKey: "agent:alpha:ambiguous",
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
sessionId: "session-ambiguous",
|
||||
binding: { threadId: "thread-recovered" },
|
||||
});
|
||||
await expect(store.lookup(conversationKey)).resolves.toMatchObject({
|
||||
state: "active",
|
||||
binding: {
|
||||
threadId: "thread-recovered",
|
||||
},
|
||||
});
|
||||
await expect(store.lookup(conversationKey)).resolves.not.toHaveProperty(
|
||||
"binding.nativeContextUsage",
|
||||
);
|
||||
await expect(fs.access(`${sidecarPath}.migrated`)).resolves.toBeUndefined();
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("uses canonical custom-store, agent, and nested transcript path resolution", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-"));
|
||||
const customStoreRoot = await fs.mkdtemp(
|
||||
path.join(os.tmpdir(), "openclaw-codex-custom-store-"),
|
||||
);
|
||||
const env = { ...process.env, HOME: stateDir, OPENCLAW_STATE_DIR: stateDir };
|
||||
const config = {
|
||||
agents: { list: [{ id: "alpha" }] },
|
||||
session: { store: path.join(customStoreRoot, "{agentId}", "sessions.json") },
|
||||
};
|
||||
const sessionsDir = path.join(customStoreRoot, "alpha");
|
||||
const transcriptPath = path.join(sessionsDir, "nested", "session-custom.jsonl");
|
||||
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
|
||||
await fs.mkdir(path.dirname(transcriptPath), { recursive: true });
|
||||
await fs.writeFile(transcriptPath, '{"type":"session","id":"session-custom"}\n', "utf8");
|
||||
await fs.writeFile(
|
||||
path.join(sessionsDir, "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:alpha:custom": {
|
||||
sessionId: "session-custom",
|
||||
sessionFile: "nested/session-custom.jsonl",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
await fs.writeFile(
|
||||
sidecarPath,
|
||||
JSON.stringify({ schemaVersion: 1, threadId: "thread-custom" }),
|
||||
"utf8",
|
||||
);
|
||||
const unrelatedSidecar = path.join(
|
||||
customStoreRoot,
|
||||
"unrelated",
|
||||
`not-a-session.jsonl.codex-app-server.json`,
|
||||
);
|
||||
await fs.mkdir(path.dirname(unrelatedSidecar), { recursive: true });
|
||||
await fs.writeFile(
|
||||
unrelatedSidecar,
|
||||
JSON.stringify({ schemaVersion: 1, threadId: "unrelated-thread" }),
|
||||
"utf8",
|
||||
);
|
||||
const migration = stateMigrations[0];
|
||||
if (!migration) {
|
||||
throw new Error("missing Codex binding migration");
|
||||
}
|
||||
|
||||
await migration.migrateLegacyState({
|
||||
config,
|
||||
env,
|
||||
stateDir,
|
||||
oauthDir: path.join(stateDir, "oauth"),
|
||||
context: createDoctorContext(env),
|
||||
});
|
||||
|
||||
const store = createDoctorContext(env).openPluginStateKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "session",
|
||||
agentId: "alpha",
|
||||
sessionId: "session-custom",
|
||||
sessionKey: "agent:alpha:custom",
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
sessionId: "session-custom",
|
||||
binding: { threadId: "thread-custom" },
|
||||
});
|
||||
await expect(
|
||||
store.lookup(
|
||||
bindingStoreKey({
|
||||
kind: "conversation",
|
||||
bindingId: legacyCodexConversationBindingId(transcriptPath),
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
state: "active",
|
||||
binding: { threadId: "thread-custom" },
|
||||
});
|
||||
await expect(fs.access(unrelatedSidecar)).resolves.toBeUndefined();
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
await fs.rm(customStoreRoot, { recursive: true, force: true });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
/** Doctor contract hooks for Codex config, state migration, and route ownership. */
|
||||
/**
|
||||
* Doctor contract hooks for Codex plugin config migrations and session-route
|
||||
* ownership warnings.
|
||||
*/
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
||||
import type { DoctorSessionRouteStateOwner } from "openclaw/plugin-sdk/runtime-doctor";
|
||||
|
||||
@@ -28,7 +31,9 @@ export const legacyConfigRules: LegacyConfigRule[] = [
|
||||
},
|
||||
];
|
||||
|
||||
/** Removes retired Codex plugin config keys while preserving unrelated config. */
|
||||
/**
|
||||
* Removes retired Codex plugin config keys while preserving unrelated config.
|
||||
*/
|
||||
export function normalizeCompatibilityConfig({ cfg }: { cfg: OpenClawConfig }): {
|
||||
config: OpenClawConfig;
|
||||
changes: string[];
|
||||
@@ -42,9 +47,10 @@ export function normalizeCompatibilityConfig({ cfg }: { cfg: OpenClawConfig }):
|
||||
const nextConfig = structuredClone(cfg) as OpenClawConfig & {
|
||||
plugins?: Record<string, unknown>;
|
||||
};
|
||||
const nextPluginConfig = asRecord(
|
||||
asRecord(asRecord(asRecord(nextConfig.plugins)?.entries)?.codex)?.config,
|
||||
);
|
||||
const nextPlugins = asRecord(nextConfig.plugins);
|
||||
const nextEntries = asRecord(nextPlugins?.entries);
|
||||
const nextEntry = asRecord(nextEntries?.codex);
|
||||
const nextPluginConfig = asRecord(nextEntry?.config);
|
||||
if (!nextPluginConfig) {
|
||||
return { config: cfg, changes: [] };
|
||||
}
|
||||
@@ -69,5 +75,3 @@ export const sessionRouteStateOwners: DoctorSessionRouteStateOwner[] = [
|
||||
authProfilePrefixes: ["codex:", "codex-cli:", "openai-codex:"],
|
||||
},
|
||||
];
|
||||
|
||||
export { stateMigrations } from "./src/migration/session-binding-sidecars.js";
|
||||
|
||||
@@ -1,18 +1,9 @@
|
||||
// Codex tests cover harness plugin behavior.
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createCodexAppServerAgentHarness } from "./harness.js";
|
||||
import {
|
||||
createCodexTestBindingStore,
|
||||
testCodexAppServerBindingStore,
|
||||
} from "./src/app-server/session-binding.test-helpers.js";
|
||||
|
||||
describe("Codex agent harness supports()", () => {
|
||||
const harness = createCodexAppServerAgentHarness({
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
});
|
||||
const harness = createCodexAppServerAgentHarness();
|
||||
|
||||
it("supports the canonical codex virtual provider", () => {
|
||||
expect(harness.supports({ provider: "codex", requestedRuntime: "codex" })).toEqual({
|
||||
@@ -49,149 +40,8 @@ describe("Codex agent harness supports()", () => {
|
||||
});
|
||||
|
||||
it("honors explicit provider id overrides", () => {
|
||||
const narrowHarness = createCodexAppServerAgentHarness({
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
providerIds: ["codex"],
|
||||
});
|
||||
const narrowHarness = createCodexAppServerAgentHarness({ providerIds: ["codex"] });
|
||||
const result = narrowHarness.supports({ provider: "openai", requestedRuntime: "codex" });
|
||||
expect(result.supported).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Codex agent harness reset", () => {
|
||||
it("uses the host agent for global session keys", async () => {
|
||||
const bindingStore = createCodexTestBindingStore();
|
||||
const harness = createCodexAppServerAgentHarness({ bindingStore });
|
||||
const identity = {
|
||||
kind: "session" as const,
|
||||
agentId: "work",
|
||||
sessionId: "session-1",
|
||||
sessionKey: "global",
|
||||
};
|
||||
await bindingStore.mutate(identity, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-work", cwd: "/repo" },
|
||||
});
|
||||
|
||||
await harness.reset?.({
|
||||
agentId: "work",
|
||||
sessionId: "session-1",
|
||||
sessionKey: "global",
|
||||
reason: "reset",
|
||||
});
|
||||
|
||||
await expect(bindingStore.read(identity)).resolves.toBeUndefined();
|
||||
await expect(
|
||||
bindingStore.mutate(identity, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-stale", cwd: "/stale" },
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
const nextIdentity = { ...identity, sessionId: "session-2" };
|
||||
await expect(
|
||||
bindingStore.mutate(nextIdentity, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-next", cwd: "/next" },
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
await expect(
|
||||
bindingStore.mutate(nextIdentity, {
|
||||
kind: "reclaim-generation",
|
||||
expectedPreviousSessionId: identity.sessionId,
|
||||
}),
|
||||
).resolves.toBe(true);
|
||||
await expect(
|
||||
bindingStore.mutate(nextIdentity, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-next", cwd: "/next" },
|
||||
}),
|
||||
).resolves.toBe(true);
|
||||
await expect(bindingStore.read(nextIdentity)).resolves.toMatchObject({
|
||||
threadId: "thread-next",
|
||||
});
|
||||
});
|
||||
|
||||
it("accepts an absent binding but rejects a mismatched reset generation", async () => {
|
||||
const bindingStore = createCodexTestBindingStore();
|
||||
const harness = createCodexAppServerAgentHarness({ bindingStore });
|
||||
const current = {
|
||||
kind: "session" as const,
|
||||
agentId: "main",
|
||||
sessionId: "session-1",
|
||||
sessionKey: "agent:main:main",
|
||||
};
|
||||
|
||||
await expect(
|
||||
harness.reset?.({
|
||||
agentId: "main",
|
||||
sessionId: "missing-session",
|
||||
sessionKey: "agent:main:missing",
|
||||
reason: "reset",
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
await bindingStore.mutate(current, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-1", cwd: "/repo" },
|
||||
});
|
||||
await expect(
|
||||
harness.reset?.({
|
||||
agentId: "main",
|
||||
sessionId: "session-2",
|
||||
sessionKey: current.sessionKey,
|
||||
reason: "reset",
|
||||
}),
|
||||
).rejects.toThrow("binding generation changed");
|
||||
await expect(bindingStore.read(current)).resolves.toMatchObject({ threadId: "thread-1" });
|
||||
});
|
||||
|
||||
it("reclaims a stale generation left while the Codex plugin was unavailable", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-reset-"));
|
||||
const storePath = path.join(stateDir, "sessions.json");
|
||||
const sessionKey = "agent:main:main";
|
||||
await fs.writeFile(
|
||||
storePath,
|
||||
JSON.stringify({
|
||||
[sessionKey]: {
|
||||
sessionId: "session-2",
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
const bindingStore = createCodexTestBindingStore();
|
||||
const harness = createCodexAppServerAgentHarness({
|
||||
bindingStore,
|
||||
resolveConfig: () => ({ session: { store: storePath } }),
|
||||
});
|
||||
const stale = {
|
||||
kind: "session" as const,
|
||||
agentId: "main",
|
||||
sessionId: "session-1",
|
||||
sessionKey,
|
||||
};
|
||||
await bindingStore.mutate(stale, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-stale", cwd: "/repo" },
|
||||
});
|
||||
|
||||
await expect(
|
||||
harness.reset?.({
|
||||
agentId: "main",
|
||||
sessionId: "session-2",
|
||||
sessionKey,
|
||||
reason: "reset",
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
const current = { ...stale, sessionId: "session-2" };
|
||||
await expect(bindingStore.read(current)).resolves.toBeUndefined();
|
||||
await expect(
|
||||
bindingStore.mutate(current, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-delayed", cwd: "/repo" },
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,13 +7,11 @@ import type {
|
||||
AgentHarnessCompactResult,
|
||||
ContextEngineHostCapability,
|
||||
} from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
||||
import type {
|
||||
CodexAppServerListModelsOptions,
|
||||
CodexAppServerModel,
|
||||
CodexAppServerModelListResult,
|
||||
} from "./src/app-server/models.js";
|
||||
import type { CodexAppServerBindingStore } from "./src/app-server/session-binding.js";
|
||||
|
||||
const DEFAULT_CODEX_HARNESS_PROVIDER_IDS = new Set(["codex", "openai"]);
|
||||
const CODEX_APP_SERVER_CONTEXT_ENGINE_HOST_CAPABILITIES = [
|
||||
@@ -39,14 +37,12 @@ type CodexAppServerAgentHarness = AgentHarness & {
|
||||
* Creates the Codex app-server harness used for attempts, side questions,
|
||||
* compaction, reset, and disposal.
|
||||
*/
|
||||
export function createCodexAppServerAgentHarness(options: {
|
||||
export function createCodexAppServerAgentHarness(options?: {
|
||||
id?: string;
|
||||
label?: string;
|
||||
providerIds?: Iterable<string>;
|
||||
pluginConfig?: unknown;
|
||||
resolvePluginConfig?: () => unknown;
|
||||
resolveConfig?: () => OpenClawConfig | undefined;
|
||||
bindingStore: CodexAppServerBindingStore;
|
||||
}): AgentHarness {
|
||||
const providerIds = new Set(
|
||||
[...(options?.providerIds ?? DEFAULT_CODEX_HARNESS_PROVIDER_IDS)].map((id) =>
|
||||
@@ -75,7 +71,6 @@ export function createCodexAppServerAgentHarness(options: {
|
||||
// cold provider catalog reads do not pull in the whole Codex runtime.
|
||||
const { runCodexAppServerAttempt } = await import("./src/app-server/run-attempt.js");
|
||||
return runCodexAppServerAttempt(params, {
|
||||
bindingStore: options.bindingStore,
|
||||
pluginConfig: options?.resolvePluginConfig?.() ?? options?.pluginConfig,
|
||||
nativeHookRelay: { enabled: true },
|
||||
});
|
||||
@@ -83,7 +78,6 @@ export function createCodexAppServerAgentHarness(options: {
|
||||
runSideQuestion: async (params) => {
|
||||
const { runCodexAppServerSideQuestion } = await import("./src/app-server/side-question.js");
|
||||
return runCodexAppServerSideQuestion(params, {
|
||||
bindingStore: options.bindingStore,
|
||||
pluginConfig: options?.resolvePluginConfig?.() ?? options?.pluginConfig,
|
||||
nativeHookRelay: { enabled: true },
|
||||
});
|
||||
@@ -91,43 +85,20 @@ export function createCodexAppServerAgentHarness(options: {
|
||||
compact: async (params) => {
|
||||
const { maybeCompactCodexAppServerSession } = await import("./src/app-server/compact.js");
|
||||
return maybeCompactCodexAppServerSession(params, {
|
||||
bindingStore: options.bindingStore,
|
||||
pluginConfig: options?.resolvePluginConfig?.() ?? options?.pluginConfig,
|
||||
});
|
||||
},
|
||||
compactAfterContextEngine: async (params) => {
|
||||
const { maybeCompactCodexAppServerSession } = await import("./src/app-server/compact.js");
|
||||
return maybeCompactCodexAppServerSession(params, {
|
||||
bindingStore: options.bindingStore,
|
||||
pluginConfig: options?.resolvePluginConfig?.() ?? options?.pluginConfig,
|
||||
allowNonManualNativeRequest: true,
|
||||
});
|
||||
},
|
||||
reset: async (params) => {
|
||||
if (params.sessionId) {
|
||||
const { reclaimCurrentCodexSessionGeneration, sessionBindingIdentity } =
|
||||
await import("./src/app-server/session-binding.js");
|
||||
const identity = sessionBindingIdentity({
|
||||
agentId: params.agentId,
|
||||
sessionId: params.sessionId,
|
||||
sessionKey: params.sessionKey,
|
||||
});
|
||||
let retired = await options.bindingStore.retireSessionGeneration(identity);
|
||||
if (retired === "conflict") {
|
||||
const reclaimed = await reclaimCurrentCodexSessionGeneration({
|
||||
bindingStore: options.bindingStore,
|
||||
identity,
|
||||
config: options.resolveConfig?.(),
|
||||
});
|
||||
if (reclaimed) {
|
||||
retired = await options.bindingStore.retireSessionGeneration(identity);
|
||||
}
|
||||
}
|
||||
if (retired === "conflict") {
|
||||
throw new Error(
|
||||
`Codex binding generation changed before session ${params.sessionId} could reset`,
|
||||
);
|
||||
}
|
||||
if (params.sessionFile) {
|
||||
const { clearCodexAppServerBinding } = await import("./src/app-server/session-binding.js");
|
||||
await clearCodexAppServerBinding(params.sessionFile);
|
||||
}
|
||||
},
|
||||
dispose: async () => {
|
||||
|
||||
@@ -4,30 +4,10 @@ import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createCodexAppServerAgentHarness } from "./harness.js";
|
||||
import plugin from "./index.js";
|
||||
import {
|
||||
createCodexAppServerBindingStore,
|
||||
sessionBindingIdentity,
|
||||
} from "./src/app-server/session-binding.js";
|
||||
import {
|
||||
createCodexTestBindingStateStore,
|
||||
testCodexAppServerBindingStore,
|
||||
} from "./src/app-server/session-binding.test-helpers.js";
|
||||
|
||||
const runCodexAppServerAttemptMock = vi.hoisted(() => vi.fn());
|
||||
const runCodexAppServerSideQuestionMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
function createCodexTestRuntime(
|
||||
current?: () => unknown,
|
||||
stateStore = createCodexTestBindingStateStore(),
|
||||
) {
|
||||
return {
|
||||
...(current ? { config: { current } } : {}),
|
||||
state: {
|
||||
openSyncKeyedStore: () => stateStore,
|
||||
},
|
||||
} as never;
|
||||
}
|
||||
|
||||
vi.mock("./src/app-server/run-attempt.js", () => ({
|
||||
runCodexAppServerAttempt: runCodexAppServerAttemptMock,
|
||||
}));
|
||||
@@ -59,6 +39,7 @@ describe("codex plugin", () => {
|
||||
const registerMigrationProvider = vi.fn();
|
||||
const registerProvider = vi.fn();
|
||||
const on = vi.fn();
|
||||
const onConversationBindingResolved = vi.fn();
|
||||
|
||||
plugin.register(
|
||||
createTestPluginApi({
|
||||
@@ -67,13 +48,14 @@ describe("codex plugin", () => {
|
||||
source: "test",
|
||||
config: {},
|
||||
pluginConfig: {},
|
||||
runtime: createCodexTestRuntime(),
|
||||
runtime: {} as never,
|
||||
registerAgentHarness,
|
||||
registerCommand,
|
||||
registerMediaUnderstandingProvider,
|
||||
registerMigrationProvider,
|
||||
registerProvider,
|
||||
on,
|
||||
onConversationBindingResolved,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -83,6 +65,9 @@ describe("codex plugin", () => {
|
||||
| Record<string, unknown>
|
||||
| undefined;
|
||||
const inboundClaimRegistration = mockCall(on) as [unknown, unknown] | undefined;
|
||||
const bindingResolvedRegistration = mockCall(onConversationBindingResolved) as
|
||||
| [unknown]
|
||||
| undefined;
|
||||
|
||||
expect(providerRegistration.id).toBe("codex");
|
||||
expect(providerRegistration.label).toBe("Codex");
|
||||
@@ -109,12 +94,33 @@ describe("codex plugin", () => {
|
||||
expect(migrationRegistration?.label).toBe("Codex");
|
||||
expect(inboundClaimRegistration?.[0]).toBe("inbound_claim");
|
||||
expect(typeof inboundClaimRegistration?.[1]).toBe("function");
|
||||
expect(typeof bindingResolvedRegistration?.[0]).toBe("function");
|
||||
});
|
||||
|
||||
it("registers with capture APIs that do not expose conversation binding hooks yet", () => {
|
||||
const registerProvider = vi.fn();
|
||||
const api = createTestPluginApi({
|
||||
id: "codex",
|
||||
name: "Codex",
|
||||
source: "test",
|
||||
config: {},
|
||||
pluginConfig: {},
|
||||
runtime: {} as never,
|
||||
registerAgentHarness: vi.fn(),
|
||||
registerCommand: vi.fn(),
|
||||
registerMediaUnderstandingProvider: vi.fn(),
|
||||
registerProvider,
|
||||
on: vi.fn(),
|
||||
});
|
||||
delete (api as { onConversationBindingResolved?: unknown }).onConversationBindingResolved;
|
||||
|
||||
plugin.register(api);
|
||||
expect(registerProvider).toHaveBeenCalledTimes(1);
|
||||
expect((mockCallArg(registerProvider) as { id?: string } | undefined)?.id).toBe("codex");
|
||||
});
|
||||
|
||||
it("claims the Codex routing providers by default", () => {
|
||||
const harness = createCodexAppServerAgentHarness({
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
});
|
||||
const harness = createCodexAppServerAgentHarness();
|
||||
|
||||
expect(harness.deliveryDefaults?.sourceVisibleReplies).toBe("message_tool");
|
||||
expect(
|
||||
@@ -135,196 +141,8 @@ describe("codex plugin", () => {
|
||||
expect(unsupported.supported).toBe(false);
|
||||
});
|
||||
|
||||
it("clears only ended session binding rows in the owning agent scope", async () => {
|
||||
const stateStore = createCodexTestBindingStateStore();
|
||||
const bindingStore = createCodexAppServerBindingStore(stateStore);
|
||||
const on = vi.fn();
|
||||
plugin.register(
|
||||
createTestPluginApi({
|
||||
id: "codex",
|
||||
name: "Codex",
|
||||
source: "test",
|
||||
config: {},
|
||||
pluginConfig: {},
|
||||
runtime: createCodexTestRuntime(undefined, stateStore),
|
||||
registerAgentHarness: vi.fn(),
|
||||
registerCommand: vi.fn(),
|
||||
registerMediaUnderstandingProvider: vi.fn(),
|
||||
registerMigrationProvider: vi.fn(),
|
||||
registerProvider: vi.fn(),
|
||||
on,
|
||||
}),
|
||||
);
|
||||
const sessionEnd = on.mock.calls.find(([name]) => name === "session_end")?.[1] as
|
||||
| ((
|
||||
event: { sessionId: string; sessionKey?: string; reason?: string },
|
||||
ctx: { agentId?: string; sessionId: string; sessionKey?: string },
|
||||
) => Promise<void>)
|
||||
| undefined;
|
||||
if (!sessionEnd) {
|
||||
throw new Error("missing Codex session_end hook");
|
||||
}
|
||||
const identity = sessionBindingIdentity({
|
||||
agentId: "worker",
|
||||
sessionId: "session-1",
|
||||
sessionKey: "agent:worker:session-1",
|
||||
});
|
||||
const setBinding = () =>
|
||||
bindingStore.mutate(identity, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-1", cwd: "/repo" },
|
||||
});
|
||||
|
||||
for (const reason of ["shutdown", "restart", "compaction", "unknown"] as const) {
|
||||
await setBinding();
|
||||
await sessionEnd(
|
||||
{ sessionId: "session-1", sessionKey: "agent:worker:session-1", reason },
|
||||
{ agentId: "worker", sessionId: "session-1" },
|
||||
);
|
||||
await expect(bindingStore.read(identity)).resolves.toMatchObject({
|
||||
threadId: "thread-1",
|
||||
});
|
||||
}
|
||||
for (const reason of ["new", "reset", "idle", "daily", "deleted"] as const) {
|
||||
await setBinding();
|
||||
await sessionEnd(
|
||||
{ sessionId: "session-1", sessionKey: "agent:worker:session-1", reason },
|
||||
{ agentId: "worker", sessionId: "session-1" },
|
||||
);
|
||||
await expect(bindingStore.read(identity)).resolves.toBeUndefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("adopts compaction successors before delayed lifecycle cleanup", async () => {
|
||||
const stateStore = createCodexTestBindingStateStore();
|
||||
const bindingStore = createCodexAppServerBindingStore(stateStore);
|
||||
const on = vi.fn();
|
||||
plugin.register(
|
||||
createTestPluginApi({
|
||||
id: "codex",
|
||||
name: "Codex",
|
||||
source: "test",
|
||||
config: {},
|
||||
pluginConfig: {},
|
||||
runtime: createCodexTestRuntime(undefined, stateStore),
|
||||
registerAgentHarness: vi.fn(),
|
||||
registerCommand: vi.fn(),
|
||||
registerMediaUnderstandingProvider: vi.fn(),
|
||||
registerMigrationProvider: vi.fn(),
|
||||
registerProvider: vi.fn(),
|
||||
on,
|
||||
}),
|
||||
);
|
||||
const afterCompaction = on.mock.calls.find(([name]) => name === "after_compaction")?.[1] as
|
||||
| ((
|
||||
event: {
|
||||
messageCount: number;
|
||||
compactedCount: number;
|
||||
previousSessionId?: string;
|
||||
},
|
||||
ctx: { agentId?: string; sessionId?: string; sessionKey?: string },
|
||||
) => Promise<void>)
|
||||
| undefined;
|
||||
const sessionEnd = on.mock.calls.find(([name]) => name === "session_end")?.[1] as
|
||||
| ((
|
||||
event: { sessionId: string; sessionKey?: string; reason?: string },
|
||||
ctx: { agentId?: string; sessionId: string; sessionKey?: string },
|
||||
) => Promise<void>)
|
||||
| undefined;
|
||||
if (!afterCompaction || !sessionEnd) {
|
||||
throw new Error("missing Codex compaction lifecycle hooks");
|
||||
}
|
||||
const sessionKey = "agent:worker:telegram:chat-1";
|
||||
const previous = sessionBindingIdentity({
|
||||
agentId: "worker",
|
||||
sessionId: "session-1",
|
||||
sessionKey,
|
||||
});
|
||||
const successor = sessionBindingIdentity({
|
||||
agentId: "worker",
|
||||
sessionId: "session-2",
|
||||
sessionKey,
|
||||
});
|
||||
const newest = sessionBindingIdentity({
|
||||
agentId: "worker",
|
||||
sessionId: "session-3",
|
||||
sessionKey,
|
||||
});
|
||||
await bindingStore.mutate(previous, {
|
||||
kind: "set",
|
||||
binding: { threadId: "thread-1", cwd: "/repo" },
|
||||
});
|
||||
|
||||
await afterCompaction(
|
||||
{ messageCount: 1, compactedCount: 1, previousSessionId: "session-1" },
|
||||
{ agentId: "worker", sessionId: "session-2", sessionKey },
|
||||
);
|
||||
await expect(bindingStore.read(previous)).resolves.toBeUndefined();
|
||||
await expect(bindingStore.read(successor)).resolves.toMatchObject({ threadId: "thread-1" });
|
||||
|
||||
await afterCompaction(
|
||||
{ messageCount: 1, compactedCount: 1, previousSessionId: "session-2" },
|
||||
{ agentId: "worker", sessionId: "session-3", sessionKey },
|
||||
);
|
||||
await afterCompaction(
|
||||
{ messageCount: 1, compactedCount: 1, previousSessionId: "session-1" },
|
||||
{ agentId: "worker", sessionId: "session-2", sessionKey },
|
||||
);
|
||||
await expect(bindingStore.read(successor)).resolves.toBeUndefined();
|
||||
await expect(bindingStore.read(newest)).resolves.toMatchObject({ threadId: "thread-1" });
|
||||
|
||||
await sessionEnd(
|
||||
{ sessionId: "session-1", sessionKey, reason: "reset" },
|
||||
{ agentId: "worker", sessionId: "session-1", sessionKey },
|
||||
);
|
||||
await sessionEnd(
|
||||
{ sessionId: "session-2", sessionKey, reason: "compaction" },
|
||||
{ agentId: "worker", sessionId: "session-2", sessionKey },
|
||||
);
|
||||
await expect(bindingStore.read(newest)).resolves.toMatchObject({ threadId: "thread-1" });
|
||||
expect(stateStore.entries()).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("ignores compaction for a session without a Codex binding", async () => {
|
||||
const warn = vi.fn();
|
||||
const on = vi.fn();
|
||||
plugin.register(
|
||||
createTestPluginApi({
|
||||
id: "codex",
|
||||
name: "Codex",
|
||||
source: "test",
|
||||
config: {},
|
||||
pluginConfig: {},
|
||||
logger: { debug: vi.fn(), info: vi.fn(), warn, error: vi.fn() },
|
||||
runtime: createCodexTestRuntime(),
|
||||
registerAgentHarness: vi.fn(),
|
||||
registerCommand: vi.fn(),
|
||||
registerMediaUnderstandingProvider: vi.fn(),
|
||||
registerMigrationProvider: vi.fn(),
|
||||
registerProvider: vi.fn(),
|
||||
on,
|
||||
}),
|
||||
);
|
||||
const afterCompaction = on.mock.calls.find(([name]) => name === "after_compaction")?.[1] as
|
||||
| ((event: object, ctx: { sessionId?: string; sessionKey?: string }) => Promise<void>)
|
||||
| undefined;
|
||||
if (!afterCompaction) {
|
||||
throw new Error("missing Codex after_compaction hook");
|
||||
}
|
||||
|
||||
await afterCompaction(
|
||||
{ previousSessionId: "session-1" },
|
||||
{ sessionId: "session-2", sessionKey: "agent:main:main" },
|
||||
);
|
||||
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("enables the native hook relay for public Codex app-server attempts", async () => {
|
||||
const harness = createCodexAppServerAgentHarness({
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
pluginConfig: { appServer: {} },
|
||||
});
|
||||
const harness = createCodexAppServerAgentHarness({ pluginConfig: { appServer: {} } });
|
||||
const result = { success: true };
|
||||
runCodexAppServerAttemptMock.mockResolvedValueOnce(result);
|
||||
|
||||
@@ -333,7 +151,6 @@ describe("codex plugin", () => {
|
||||
expect(runCodexAppServerAttemptMock).toHaveBeenCalledWith(
|
||||
{ prompt: "hello" },
|
||||
{
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
pluginConfig: { appServer: {} },
|
||||
nativeHookRelay: { enabled: true },
|
||||
},
|
||||
@@ -368,7 +185,11 @@ describe("codex plugin", () => {
|
||||
source: "test",
|
||||
config: {},
|
||||
pluginConfig: { codexPlugins: { enabled: false } },
|
||||
runtime: createCodexTestRuntime(() => liveConfig),
|
||||
runtime: {
|
||||
config: {
|
||||
current: () => liveConfig,
|
||||
},
|
||||
} as never,
|
||||
registerAgentHarness,
|
||||
registerCommand: vi.fn(),
|
||||
registerMediaUnderstandingProvider: vi.fn(),
|
||||
@@ -388,49 +209,14 @@ describe("codex plugin", () => {
|
||||
expect(runCodexAppServerAttemptMock).toHaveBeenCalledWith(
|
||||
{ prompt: "calendar" },
|
||||
{
|
||||
bindingStore: expect.any(Object),
|
||||
pluginConfig: liveConfig.plugins.entries.codex.config,
|
||||
nativeHookRelay: { enabled: true },
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("does not resurrect startup Codex config after the live entry is removed", async () => {
|
||||
const registerAgentHarness = vi.fn();
|
||||
plugin.register(
|
||||
createTestPluginApi({
|
||||
id: "codex",
|
||||
name: "Codex",
|
||||
source: "test",
|
||||
config: {},
|
||||
pluginConfig: { appServer: { mode: "yolo" } },
|
||||
runtime: createCodexTestRuntime(() => ({ plugins: { entries: {} } })),
|
||||
registerAgentHarness,
|
||||
registerCommand: vi.fn(),
|
||||
registerMediaUnderstandingProvider: vi.fn(),
|
||||
registerMigrationProvider: vi.fn(),
|
||||
registerProvider: vi.fn(),
|
||||
on: vi.fn(),
|
||||
}),
|
||||
);
|
||||
const harness = mockCallArg(registerAgentHarness) as ReturnType<
|
||||
typeof createCodexAppServerAgentHarness
|
||||
>;
|
||||
runCodexAppServerAttemptMock.mockResolvedValueOnce({ success: true });
|
||||
|
||||
await harness.runAttempt({ prompt: "default policy" } as never);
|
||||
|
||||
expect(runCodexAppServerAttemptMock).toHaveBeenCalledWith(
|
||||
{ prompt: "default policy" },
|
||||
expect.objectContaining({ pluginConfig: undefined }),
|
||||
);
|
||||
});
|
||||
|
||||
it("enables the native hook relay for public Codex side questions", async () => {
|
||||
const harness = createCodexAppServerAgentHarness({
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
pluginConfig: { appServer: {} },
|
||||
});
|
||||
const harness = createCodexAppServerAgentHarness({ pluginConfig: { appServer: {} } });
|
||||
const runSideQuestion = harness["runSideQuestion"];
|
||||
const result = { text: "ok" };
|
||||
runCodexAppServerSideQuestionMock.mockResolvedValueOnce(result);
|
||||
@@ -443,7 +229,6 @@ describe("codex plugin", () => {
|
||||
expect(runCodexAppServerSideQuestionMock).toHaveBeenCalledWith(
|
||||
{ question: "btw" },
|
||||
{
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
pluginConfig: { appServer: {} },
|
||||
nativeHookRelay: { enabled: true },
|
||||
},
|
||||
|
||||
@@ -4,71 +4,47 @@
|
||||
*/
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
||||
import { mutateConfigFile } from "openclaw/plugin-sdk/config-mutation";
|
||||
import {
|
||||
resolveLivePluginConfigObject,
|
||||
resolvePluginConfigObject,
|
||||
} from "openclaw/plugin-sdk/plugin-config-runtime";
|
||||
import { resolveLivePluginConfigObject } from "openclaw/plugin-sdk/plugin-config-runtime";
|
||||
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
||||
import { createCodexAppServerAgentHarness } from "./harness.js";
|
||||
import { buildCodexMediaUnderstandingProvider } from "./media-understanding-provider.js";
|
||||
import { buildCodexProvider } from "./provider.js";
|
||||
import {
|
||||
CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
createLazyCodexAppServerBindingStore,
|
||||
type StoredCodexAppServerBinding,
|
||||
} from "./src/app-server/session-binding-store.js";
|
||||
import type { CodexPluginsConfigBlock } from "./src/command-plugins-management.js";
|
||||
import { createCodexCommand } from "./src/commands.js";
|
||||
import {
|
||||
handleCodexConversationBindingResolved,
|
||||
handleCodexConversationInboundClaim,
|
||||
} from "./src/conversation-binding.js";
|
||||
import { buildCodexMigrationProvider } from "./src/migration/provider.js";
|
||||
import {
|
||||
createCodexCliSessionNodeHostCommands,
|
||||
createCodexCliSessionNodeInvokePolicies,
|
||||
} from "./src/node-cli-session-registration.js";
|
||||
|
||||
const ENDED_SESSION_REASONS: ReadonlySet<string> = new Set([
|
||||
"new",
|
||||
"reset",
|
||||
"idle",
|
||||
"daily",
|
||||
"deleted",
|
||||
]);
|
||||
listCodexCliSessionsOnNode,
|
||||
resumeCodexCliSessionOnNode,
|
||||
resolveCodexCliSessionForBindingOnNode,
|
||||
} from "./src/node-cli-sessions.js";
|
||||
|
||||
export default definePluginEntry({
|
||||
id: "codex",
|
||||
name: "Codex",
|
||||
description: "Codex app-server harness and Codex-managed GPT model catalog.",
|
||||
register(api) {
|
||||
const runtimeConfigLoader = api.runtime.config?.current
|
||||
? () => api.runtime.config?.current() as OpenClawConfig
|
||||
: undefined;
|
||||
const resolveCurrentConfig = () => runtimeConfigLoader?.();
|
||||
const loadNodeCliSessions = () => import("./src/node-cli-sessions.js");
|
||||
const resolveCurrentConfig = () =>
|
||||
api.runtime.config?.current ? (api.runtime.config.current() as OpenClawConfig) : undefined;
|
||||
const resolveCurrentPluginConfig = () =>
|
||||
// Codex plugin config can change at runtime; resolve from live config for
|
||||
// harness attempts and binding claims instead of keeping startup values.
|
||||
resolveLivePluginConfigObject(
|
||||
runtimeConfigLoader,
|
||||
resolveCurrentConfig,
|
||||
"codex",
|
||||
api.pluginConfig as Record<string, unknown>,
|
||||
);
|
||||
const bindingStore = createLazyCodexAppServerBindingStore(
|
||||
api.runtime.state.openSyncKeyedStore<StoredCodexAppServerBinding>({
|
||||
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
overflowPolicy: "reject-new",
|
||||
}),
|
||||
);
|
||||
) ?? api.pluginConfig;
|
||||
api.registerAgentHarness(
|
||||
createCodexAppServerAgentHarness({
|
||||
bindingStore,
|
||||
resolveConfig: resolveCurrentConfig,
|
||||
resolvePluginConfig: resolveCurrentPluginConfig,
|
||||
}),
|
||||
createCodexAppServerAgentHarness({ resolvePluginConfig: resolveCurrentPluginConfig }),
|
||||
);
|
||||
api.registerProvider(buildCodexProvider({ pluginConfig: api.pluginConfig }));
|
||||
api.registerMediaUnderstandingProvider(
|
||||
buildCodexMediaUnderstandingProvider({ resolvePluginConfig: resolveCurrentPluginConfig }),
|
||||
buildCodexMediaUnderstandingProvider({ pluginConfig: api.pluginConfig }),
|
||||
);
|
||||
api.registerMigrationProvider(buildCodexMigrationProvider({ runtime: api.runtime }));
|
||||
for (const command of createCodexCliSessionNodeHostCommands()) {
|
||||
@@ -79,43 +55,43 @@ export default definePluginEntry({
|
||||
}
|
||||
api.registerCommand(
|
||||
createCodexCommand({
|
||||
resolvePluginConfig: resolveCurrentPluginConfig,
|
||||
pluginConfig: api.pluginConfig,
|
||||
deps: {
|
||||
bindingStore,
|
||||
listCodexCliSessionsOnNode: async (params) =>
|
||||
await (
|
||||
await loadNodeCliSessions()
|
||||
).listCodexCliSessionsOnNode({
|
||||
runtime: api.runtime,
|
||||
...params,
|
||||
}),
|
||||
resolveCodexCliSessionForBindingOnNode: async (params) =>
|
||||
await (
|
||||
await loadNodeCliSessions()
|
||||
).resolveCodexCliSessionForBindingOnNode({
|
||||
runtime: api.runtime,
|
||||
...params,
|
||||
}),
|
||||
listCodexCliSessionsOnNode: (params) =>
|
||||
listCodexCliSessionsOnNode({ runtime: api.runtime, ...params }),
|
||||
resolveCodexCliSessionForBindingOnNode: (params) =>
|
||||
resolveCodexCliSessionForBindingOnNode({ runtime: api.runtime, ...params }),
|
||||
codexPluginsManagementIo: {
|
||||
readConfig: () => {
|
||||
const current = (api.runtime.config?.current?.() ?? {}) as OpenClawConfig;
|
||||
const codexPlugins = resolvePluginConfigObject(current, "codex")?.codexPlugins;
|
||||
if (
|
||||
!codexPlugins ||
|
||||
typeof codexPlugins !== "object" ||
|
||||
Array.isArray(codexPlugins)
|
||||
) {
|
||||
const plugins = (current as Record<string, unknown>).plugins;
|
||||
if (!plugins || typeof plugins !== "object") {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
const block = codexPlugins as Record<string, unknown>;
|
||||
const declared = block.plugins;
|
||||
const entries = (plugins as Record<string, unknown>).entries;
|
||||
if (!entries || typeof entries !== "object") {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
const codexEntry = (entries as Record<string, unknown>).codex;
|
||||
if (!codexEntry || typeof codexEntry !== "object") {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
const config = (codexEntry as Record<string, unknown>).config;
|
||||
if (!config || typeof config !== "object") {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
const codexPlugins = (config as Record<string, unknown>).codexPlugins;
|
||||
if (!codexPlugins || typeof codexPlugins !== "object") {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
const declared = (codexPlugins as Record<string, unknown>).plugins;
|
||||
if (!declared || typeof declared !== "object") {
|
||||
return Promise.resolve({
|
||||
enabled: block.enabled === true,
|
||||
enabled: (codexPlugins as Record<string, unknown>).enabled === true,
|
||||
});
|
||||
}
|
||||
return Promise.resolve({
|
||||
enabled: block.enabled === true,
|
||||
enabled: (codexPlugins as Record<string, unknown>).enabled === true,
|
||||
plugins: declared as Record<string, never>,
|
||||
});
|
||||
},
|
||||
@@ -125,12 +101,17 @@ export default definePluginEntry({
|
||||
// Create the nested plugin config path on demand so codex
|
||||
// plugin commands can enable/update Codex-managed plugins.
|
||||
const root = draft as Record<string, unknown>;
|
||||
const pluginsBlock = (root.plugins ??= {}) as Record<string, unknown>;
|
||||
const entries = (pluginsBlock.entries ??= {}) as Record<string, unknown>;
|
||||
const codexEntry = (entries.codex ??= {}) as Record<string, unknown>;
|
||||
const config = (codexEntry.config ??= {}) as Record<string, unknown>;
|
||||
const codexPlugins = (config.codexPlugins ??= {}) as Record<string, unknown>;
|
||||
codexPlugins.plugins ??= {};
|
||||
root.plugins = (root.plugins ?? {}) as Record<string, unknown>;
|
||||
const pluginsBlock = root.plugins as Record<string, unknown>;
|
||||
pluginsBlock.entries = (pluginsBlock.entries ?? {}) as Record<string, unknown>;
|
||||
const entries = pluginsBlock.entries as Record<string, unknown>;
|
||||
entries.codex = (entries.codex ?? {}) as Record<string, unknown>;
|
||||
const codexEntry = entries.codex as Record<string, unknown>;
|
||||
codexEntry.config = (codexEntry.config ?? {}) as Record<string, unknown>;
|
||||
const config = codexEntry.config as Record<string, unknown>;
|
||||
config.codexPlugins = (config.codexPlugins ?? {}) as Record<string, unknown>;
|
||||
const codexPlugins = config.codexPlugins as Record<string, unknown>;
|
||||
codexPlugins.plugins = (codexPlugins.plugins ?? {}) as Record<string, unknown>;
|
||||
update(codexPlugins as CodexPluginsConfigBlock);
|
||||
},
|
||||
});
|
||||
@@ -139,58 +120,14 @@ export default definePluginEntry({
|
||||
},
|
||||
}),
|
||||
);
|
||||
api.on("inbound_claim", async (event, ctx) => {
|
||||
const { handleCodexConversationInboundClaim } = await import("./src/conversation-binding.js");
|
||||
return await handleCodexConversationInboundClaim(event, ctx, {
|
||||
bindingStore,
|
||||
api.on("inbound_claim", (event, ctx) =>
|
||||
handleCodexConversationInboundClaim(event, ctx, {
|
||||
pluginConfig: resolveCurrentPluginConfig(),
|
||||
config: resolveCurrentConfig(),
|
||||
resumeCodexCliSessionOnNode: async (params) =>
|
||||
await (
|
||||
await loadNodeCliSessions()
|
||||
).resumeCodexCliSessionOnNode({
|
||||
runtime: api.runtime,
|
||||
...params,
|
||||
}),
|
||||
});
|
||||
});
|
||||
api.on("after_compaction", async (event, ctx) => {
|
||||
const previousSessionId = event.previousSessionId?.trim();
|
||||
const sessionId = ctx.sessionId?.trim();
|
||||
if (!previousSessionId || !sessionId || previousSessionId === sessionId) {
|
||||
return;
|
||||
}
|
||||
const config = resolveCurrentConfig();
|
||||
const sessionKey = ctx.sessionKey?.trim();
|
||||
const { sessionBindingIdentity } = await import("./src/app-server/session-binding.js");
|
||||
const identity = sessionBindingIdentity({
|
||||
sessionId,
|
||||
...(sessionKey ? { sessionKey } : {}),
|
||||
...(ctx.agentId ? { agentId: ctx.agentId } : {}),
|
||||
...(config ? { config } : {}),
|
||||
});
|
||||
const adopted = await bindingStore.adoptSessionGeneration(identity, previousSessionId);
|
||||
if (adopted === "conflict") {
|
||||
api.logger.warn?.(
|
||||
`codex: could not adopt compacted session generation ${sessionId} (${adopted}); secondary native compaction will skip`,
|
||||
);
|
||||
}
|
||||
});
|
||||
api.on("session_end", async (event, ctx) => {
|
||||
if (!event.reason || !ENDED_SESSION_REASONS.has(event.reason)) {
|
||||
return;
|
||||
}
|
||||
const sessionKey = event.sessionKey ?? ctx.sessionKey;
|
||||
const config = resolveCurrentConfig();
|
||||
const { sessionBindingIdentity } = await import("./src/app-server/session-binding.js");
|
||||
await bindingStore.retireSessionGeneration(
|
||||
sessionBindingIdentity({
|
||||
sessionId: event.sessionId,
|
||||
...(sessionKey ? { sessionKey } : {}),
|
||||
...(ctx.agentId ? { agentId: ctx.agentId } : {}),
|
||||
...(config ? { config } : {}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
resumeCodexCliSessionOnNode: (params) =>
|
||||
resumeCodexCliSessionOnNode({ runtime: api.runtime, ...params }),
|
||||
}),
|
||||
);
|
||||
api.onConversationBindingResolved?.(handleCodexConversationBindingResolved);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -2,33 +2,8 @@
|
||||
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { buildCodexMediaUnderstandingProvider } from "./media-understanding-provider.js";
|
||||
import { CodexAppServerRpcError, type CodexAppServerClient } from "./src/app-server/client.js";
|
||||
import type { CodexAppServerClient } from "./src/app-server/client.js";
|
||||
import type { CodexServerNotification, JsonValue } from "./src/app-server/protocol.js";
|
||||
import { adaptCodexTestClientFactory } from "./src/app-server/test-support.js";
|
||||
|
||||
const EXPECTED_MEDIA_THREAD_CONFIG = {
|
||||
project_doc_max_bytes: 0,
|
||||
web_search: "disabled",
|
||||
"tools.experimental_request_user_input.enabled": false,
|
||||
"features.hooks": false,
|
||||
"features.multi_agent": false,
|
||||
"features.apps": false,
|
||||
"features.plugins": false,
|
||||
"features.image_generation": false,
|
||||
"features.skill_mcp_dependency_install": false,
|
||||
"features.memories": false,
|
||||
"features.goals": false,
|
||||
"features.code_mode": false,
|
||||
"features.code_mode_only": false,
|
||||
};
|
||||
|
||||
const sharedClientMocks = vi.hoisted(() => ({
|
||||
createIsolatedCodexAppServerClient: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./src/app-server/shared-client.js", () => ({
|
||||
createIsolatedCodexAppServerClient: sharedClientMocks.createIsolatedCodexAppServerClient,
|
||||
}));
|
||||
|
||||
function codexModel(inputModalities: string[] = ["text", "image"]) {
|
||||
return {
|
||||
@@ -102,15 +77,13 @@ function createFakeClient(options?: {
|
||||
inputModalities?: string[];
|
||||
completeWithItems?: boolean;
|
||||
notifyError?: string;
|
||||
approvalRequestMethod?: string;
|
||||
responseText?: string;
|
||||
turnStartError?: Error;
|
||||
preBindNotificationCount?: number;
|
||||
interruptError?: Error;
|
||||
unsubscribeError?: Error;
|
||||
}) {
|
||||
const notifications = new Set<(notification: CodexServerNotification) => void>();
|
||||
const closeHandlers = new Set<() => void>();
|
||||
const requestHandlers = new Set<(request: { method: string }) => JsonValue | undefined>();
|
||||
const requests: Array<{ method: string; params?: JsonValue }> = [];
|
||||
const approvalResponses: JsonValue[] = [];
|
||||
const request = vi.fn(async (method: string, params?: JsonValue) => {
|
||||
requests.push({ method, params });
|
||||
if (method === "model/list") {
|
||||
@@ -123,60 +96,51 @@ function createFakeClient(options?: {
|
||||
return threadStartResult();
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
if (options?.turnStartError) {
|
||||
throw options.turnStartError;
|
||||
}
|
||||
if (options?.preBindNotificationCount) {
|
||||
for (let index = 0; index < options.preBindNotificationCount; index += 1) {
|
||||
for (const notify of notifications) {
|
||||
notify({
|
||||
method: "item/started",
|
||||
params: { threadId: "thread-1", turnId: "turn-1" },
|
||||
});
|
||||
if (options?.approvalRequestMethod) {
|
||||
for (const handler of requestHandlers) {
|
||||
const response = handler({ method: options.approvalRequestMethod });
|
||||
if (response !== undefined) {
|
||||
approvalResponses.push(response);
|
||||
}
|
||||
}
|
||||
return turnStartResult();
|
||||
}
|
||||
const emitTurnNotifications = () => {
|
||||
if (options?.notifyError) {
|
||||
for (const notify of notifications) {
|
||||
notify({
|
||||
method: "error",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
error: {
|
||||
message: options.notifyError,
|
||||
codexErrorInfo: null,
|
||||
additionalDetails: null,
|
||||
},
|
||||
willRetry: false,
|
||||
if (options?.notifyError) {
|
||||
for (const notify of notifications) {
|
||||
notify({
|
||||
method: "error",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
error: {
|
||||
message: options.notifyError,
|
||||
codexErrorInfo: null,
|
||||
additionalDetails: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
} else if (!options?.completeWithItems) {
|
||||
for (const notify of notifications) {
|
||||
notify({
|
||||
method: "item/agentMessage/delta",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
itemId: "msg-1",
|
||||
delta: options?.responseText ?? "A red square.",
|
||||
},
|
||||
});
|
||||
notify({
|
||||
method: "turn/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
turn: turnStartResult("completed").turn,
|
||||
},
|
||||
});
|
||||
}
|
||||
willRetry: false,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
emitTurnNotifications();
|
||||
} else if (!options?.completeWithItems) {
|
||||
for (const notify of notifications) {
|
||||
notify({
|
||||
method: "item/agentMessage/delta",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
itemId: "msg-1",
|
||||
delta: options?.responseText ?? "A red square.",
|
||||
},
|
||||
});
|
||||
notify({
|
||||
method: "turn/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
turn: turnStartResult("completed").turn,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
return turnStartResult(
|
||||
options?.completeWithItems ? "completed" : "inProgress",
|
||||
options?.completeWithItems
|
||||
@@ -192,12 +156,6 @@ function createFakeClient(options?: {
|
||||
: [],
|
||||
);
|
||||
}
|
||||
if (method === "turn/interrupt" && options?.interruptError) {
|
||||
throw options.interruptError;
|
||||
}
|
||||
if (method === "thread/unsubscribe" && options?.unsubscribeError) {
|
||||
throw options.unsubscribeError;
|
||||
}
|
||||
return {};
|
||||
});
|
||||
|
||||
@@ -207,39 +165,26 @@ function createFakeClient(options?: {
|
||||
notifications.add(handler);
|
||||
return () => notifications.delete(handler);
|
||||
},
|
||||
addRequestHandler() {
|
||||
return () => undefined;
|
||||
addRequestHandler(handler: (request: { method: string }) => JsonValue | undefined) {
|
||||
requestHandlers.add(handler);
|
||||
return () => requestHandlers.delete(handler);
|
||||
},
|
||||
addCloseHandler(handler: () => void) {
|
||||
closeHandlers.add(handler);
|
||||
return () => closeHandlers.delete(handler);
|
||||
},
|
||||
close: vi.fn(),
|
||||
} as unknown as CodexAppServerClient;
|
||||
|
||||
return { client, requests };
|
||||
return { client, requests, approvalResponses };
|
||||
}
|
||||
|
||||
describe("codex media understanding provider", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
sharedClientMocks.createIsolatedCodexAppServerClient.mockReset();
|
||||
});
|
||||
|
||||
it("runs image understanding through a bounded Codex app-server turn", async () => {
|
||||
const { client, requests } = createFakeClient();
|
||||
const clientFactory = vi.fn(async () => client);
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(clientFactory),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
const cfg = {
|
||||
auth: {
|
||||
order: {
|
||||
openai: ["openai:work"],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const result = await provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
@@ -249,38 +194,34 @@ describe("codex media understanding provider", () => {
|
||||
model: "gpt-5.4",
|
||||
prompt: "Describe briefly.",
|
||||
timeoutMs: 30_000,
|
||||
cfg,
|
||||
cfg: {},
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
});
|
||||
|
||||
expect(result).toEqual({ text: "A red square.", model: "gpt-5.4" });
|
||||
expect(clientFactory).toHaveBeenCalledWith(
|
||||
expect.any(Object),
|
||||
undefined,
|
||||
"/tmp/openclaw-agent",
|
||||
cfg,
|
||||
expect.objectContaining({ timeoutMs: 30_000 }),
|
||||
);
|
||||
expect(requests.map((entry) => entry.method)).toEqual([
|
||||
"model/list",
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
"thread/unsubscribe",
|
||||
]);
|
||||
expect(requests[0]?.params).toEqual({ limit: 100, cursor: null, includeHidden: true });
|
||||
expect(requests[1]?.params).toEqual({
|
||||
model: "gpt-5.4",
|
||||
modelProvider: "openai",
|
||||
cwd: "/tmp/openclaw-agent/codex-media-home",
|
||||
approvalPolicy: "never",
|
||||
cwd: "/tmp/openclaw-agent",
|
||||
approvalPolicy: "on-request",
|
||||
sandbox: "read-only",
|
||||
serviceName: "OpenClaw",
|
||||
personality: "none",
|
||||
developerInstructions:
|
||||
"You are OpenClaw's bounded image-understanding worker. Describe only the provided image content. Do not call tools, edit files, or ask follow-up questions.",
|
||||
config: EXPECTED_MEDIA_THREAD_CONFIG,
|
||||
config: {
|
||||
"features.code_mode": false,
|
||||
"features.code_mode_only": false,
|
||||
},
|
||||
environments: [],
|
||||
dynamicTools: [],
|
||||
experimentalRawEvents: true,
|
||||
ephemeral: true,
|
||||
persistExtendedHistory: false,
|
||||
});
|
||||
expect(requests[2]?.params).toEqual({
|
||||
threadId: "thread-1",
|
||||
@@ -288,83 +229,19 @@ describe("codex media understanding provider", () => {
|
||||
{ type: "text", text: "Describe briefly.", text_elements: [] },
|
||||
{ type: "image", url: "data:image/png;base64,aW1hZ2UtYnl0ZXM=" },
|
||||
],
|
||||
cwd: "/tmp/openclaw-agent",
|
||||
approvalPolicy: "on-request",
|
||||
model: "gpt-5.4",
|
||||
effort: "low",
|
||||
});
|
||||
});
|
||||
|
||||
it("treats a blank agent directory as absent when starting the app-server", async () => {
|
||||
const { client, requests } = createFakeClient();
|
||||
const clientFactory = vi.fn(async () => client);
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(clientFactory),
|
||||
});
|
||||
const cfg = {
|
||||
agents: { list: [{ id: "main", agentDir: "/tmp/openclaw-default-agent" }] },
|
||||
};
|
||||
|
||||
await provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 30_000,
|
||||
cfg,
|
||||
agentDir: " ",
|
||||
});
|
||||
|
||||
expect(clientFactory).toHaveBeenCalledWith(
|
||||
expect.any(Object),
|
||||
undefined,
|
||||
"/tmp/openclaw-default-agent",
|
||||
cfg,
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(requests[1]?.params).toEqual(
|
||||
expect.objectContaining({ cwd: "/tmp/openclaw-default-agent/codex-media-home" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("passes the scoped auth store into isolated app-server startup", async () => {
|
||||
const { client } = createFakeClient();
|
||||
sharedClientMocks.createIsolatedCodexAppServerClient.mockResolvedValue(client);
|
||||
const provider = buildCodexMediaUnderstandingProvider();
|
||||
const authStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
"openai:scoped": {
|
||||
type: "oauth" as const,
|
||||
provider: "openai",
|
||||
access: "scoped-access",
|
||||
refresh: "scoped-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 30_000,
|
||||
cfg: {},
|
||||
authStore,
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
});
|
||||
|
||||
expect(sharedClientMocks.createIsolatedCodexAppServerClient).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ authProfileStore: authStore }),
|
||||
);
|
||||
});
|
||||
|
||||
it("clamps oversized image understanding turn timeouts", async () => {
|
||||
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
|
||||
try {
|
||||
const { client } = createFakeClient();
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
const result = await provider.describeImage?.({
|
||||
@@ -387,97 +264,33 @@ describe("codex media understanding provider", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("starts the media deadline before client acquisition", async () => {
|
||||
vi.useFakeTimers();
|
||||
it("declines approval requests during image understanding", async () => {
|
||||
const { client, approvalResponses } = createFakeClient({
|
||||
approvalRequestMethod: "item/permissions/requestApproval",
|
||||
});
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(
|
||||
async () => await new Promise<CodexAppServerClient>(() => {}),
|
||||
),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
const description = provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 100,
|
||||
cfg: {},
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
});
|
||||
const rejected = expect(description).rejects.toThrow(
|
||||
"Codex app-server image understanding timed out",
|
||||
);
|
||||
|
||||
await vi.advanceTimersByTimeAsync(100);
|
||||
|
||||
await rejected;
|
||||
});
|
||||
|
||||
it("retires a media client lease that resolves after its deadline", async () => {
|
||||
let resolveLease!: (lease: {
|
||||
client: CodexAppServerClient;
|
||||
release: () => void;
|
||||
abandon: () => Promise<void>;
|
||||
}) => void;
|
||||
const pendingLease = new Promise<{
|
||||
client: CodexAppServerClient;
|
||||
release: () => void;
|
||||
abandon: () => Promise<void>;
|
||||
}>((resolve) => {
|
||||
resolveLease = resolve;
|
||||
});
|
||||
const clientLeaseFactory = vi.fn(async () => await pendingLease);
|
||||
const provider = buildCodexMediaUnderstandingProvider({ clientLeaseFactory });
|
||||
const description = provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 5,
|
||||
cfg: {},
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
});
|
||||
|
||||
await expect(description).rejects.toThrow("Codex app-server image understanding timed out");
|
||||
const { client } = createFakeClient();
|
||||
const release = vi.fn();
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
resolveLease({ client, release, abandon });
|
||||
await vi.waitFor(() => expect(abandon).toHaveBeenCalledOnce());
|
||||
|
||||
expect(release).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("releases the bounded route between isolated media calls", async () => {
|
||||
const { client, requests } = createFakeClient();
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
});
|
||||
const request = {
|
||||
|
||||
await provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
prompt: "Describe briefly.",
|
||||
timeoutMs: 30_000,
|
||||
cfg: {},
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
};
|
||||
});
|
||||
|
||||
const first = await provider.describeImage?.(request);
|
||||
const second = await provider.describeImage?.(request);
|
||||
|
||||
expect(first?.text).toBe("A red square.");
|
||||
expect(second?.text).toBe("A red square.");
|
||||
expect(requests.filter((entry) => entry.method === "model/list")).toHaveLength(2);
|
||||
expect(requests.filter((entry) => entry.method === "thread/start")).toHaveLength(2);
|
||||
expect(approvalResponses).toEqual([{ permissions: {}, scope: "turn" }]);
|
||||
});
|
||||
|
||||
it("extracts text from terminal turn items", async () => {
|
||||
const { client } = createFakeClient({ completeWithItems: true });
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
const result = await provider.describeImages?.({
|
||||
@@ -496,7 +309,7 @@ describe("codex media understanding provider", () => {
|
||||
it("rejects text-only Codex app-server models before starting a turn", async () => {
|
||||
const { client, requests } = createFakeClient({ inputModalities: ["text"] });
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
await expect(
|
||||
@@ -517,7 +330,7 @@ describe("codex media understanding provider", () => {
|
||||
it("surfaces Codex app-server turn errors", async () => {
|
||||
const { client } = createFakeClient({ notifyError: "vision unavailable" });
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
await expect(
|
||||
@@ -534,107 +347,12 @@ describe("codex media understanding provider", () => {
|
||||
).rejects.toThrow("vision unavailable");
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "structured rejection",
|
||||
error: new CodexAppServerRpcError({ message: "turn rejected" }, "turn/start"),
|
||||
abandonCount: 0,
|
||||
},
|
||||
{
|
||||
name: "ambiguous timeout",
|
||||
error: new Error("turn/start timed out"),
|
||||
abandonCount: 1,
|
||||
},
|
||||
])("handles $name with exact media lease ownership", async ({ error, abandonCount }) => {
|
||||
const { client } = createFakeClient({ turnStartError: error });
|
||||
const release = vi.fn();
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: async () => ({ client, release, abandon }),
|
||||
});
|
||||
|
||||
await expect(
|
||||
provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 30_000,
|
||||
cfg: {},
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
}),
|
||||
).rejects.toBe(error);
|
||||
|
||||
expect(abandon).toHaveBeenCalledTimes(abandonCount);
|
||||
expect(release).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("retires the media client when thread cleanup is unconfirmed", async () => {
|
||||
const { client } = createFakeClient({ unsubscribeError: new Error("unsubscribe failed") });
|
||||
const release = vi.fn();
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: async () => ({ client, release, abandon }),
|
||||
});
|
||||
|
||||
await expect(
|
||||
provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 30_000,
|
||||
cfg: {},
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
}),
|
||||
).resolves.toEqual({ text: "A red square.", model: "gpt-5.4" });
|
||||
|
||||
expect(abandon).toHaveBeenCalledOnce();
|
||||
expect(release).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("retires the media client when an accepted turn cannot be interrupted", async () => {
|
||||
const { client, requests } = createFakeClient({
|
||||
preBindNotificationCount: 257,
|
||||
interruptError: new Error("interrupt timeout"),
|
||||
});
|
||||
const release = vi.fn();
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: async () => ({ client, release, abandon }),
|
||||
});
|
||||
|
||||
await expect(
|
||||
provider.describeImage?.({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
fileName: "image.png",
|
||||
mime: "image/png",
|
||||
provider: "codex",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 30_000,
|
||||
cfg: {},
|
||||
agentDir: "/tmp/openclaw-agent",
|
||||
}),
|
||||
).rejects.toThrow("pre-bind notification buffer exceeded 256 entries");
|
||||
|
||||
expect(requests.map((entry) => entry.method)).toEqual([
|
||||
"model/list",
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
"turn/interrupt",
|
||||
]);
|
||||
expect(abandon).toHaveBeenCalledOnce();
|
||||
expect(release).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("runs structured extraction through the same bounded Codex app-server path", async () => {
|
||||
const { client, requests } = createFakeClient({
|
||||
responseText: '{"summary":"red square","tags":["shape"]}',
|
||||
});
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
const result = await provider.extractStructured?.({
|
||||
@@ -675,21 +393,25 @@ describe("codex media understanding provider", () => {
|
||||
"model/list",
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
"thread/unsubscribe",
|
||||
]);
|
||||
expect(requests[1]?.params).toEqual({
|
||||
model: "gpt-5.4",
|
||||
modelProvider: "openai",
|
||||
cwd: "/tmp/openclaw-agent/codex-media-home",
|
||||
approvalPolicy: "never",
|
||||
cwd: "/tmp/openclaw-agent",
|
||||
approvalPolicy: "on-request",
|
||||
sandbox: "read-only",
|
||||
serviceName: "OpenClaw",
|
||||
personality: "none",
|
||||
developerInstructions:
|
||||
"You are OpenClaw's bounded structured-extraction worker. Return only the requested extraction. Do not call tools, edit files, ask follow-up questions, or include secrets.",
|
||||
config: EXPECTED_MEDIA_THREAD_CONFIG,
|
||||
config: {
|
||||
"features.code_mode": false,
|
||||
"features.code_mode_only": false,
|
||||
},
|
||||
environments: [],
|
||||
dynamicTools: [],
|
||||
experimentalRawEvents: true,
|
||||
ephemeral: true,
|
||||
persistExtendedHistory: false,
|
||||
});
|
||||
const turnParams = requests[2]?.params as
|
||||
| {
|
||||
@@ -702,9 +424,9 @@ describe("codex media understanding provider", () => {
|
||||
}
|
||||
| undefined;
|
||||
expect(turnParams?.threadId).toBe("thread-1");
|
||||
expect(turnParams?.approvalPolicy).toBeUndefined();
|
||||
expect(turnParams?.model).toBeUndefined();
|
||||
expect(turnParams?.cwd).toBeUndefined();
|
||||
expect(turnParams?.approvalPolicy).toBe("on-request");
|
||||
expect(turnParams?.model).toBe("gpt-5.4");
|
||||
expect(turnParams?.cwd).toBe("/tmp/openclaw-agent");
|
||||
expect(turnParams?.effort).toBe("low");
|
||||
expect(turnParams?.input).toHaveLength(3);
|
||||
expect(turnParams?.input?.[0]?.type).toBe("text");
|
||||
@@ -727,7 +449,7 @@ describe("codex media understanding provider", () => {
|
||||
responseText: '{"summary":"only text"}',
|
||||
});
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
await expect(
|
||||
@@ -747,7 +469,7 @@ describe("codex media understanding provider", () => {
|
||||
it("returns a controlled error when structured JSON parsing fails", async () => {
|
||||
const { client } = createFakeClient({ responseText: "not json" });
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
await expect(
|
||||
@@ -776,7 +498,7 @@ describe("codex media understanding provider", () => {
|
||||
responseText: '{"summary":123,"tags":["shape"]}',
|
||||
});
|
||||
const provider = buildCodexMediaUnderstandingProvider({
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(async () => client),
|
||||
clientFactory: async () => client,
|
||||
});
|
||||
|
||||
await expect(
|
||||
|
||||
@@ -1,35 +1,538 @@
|
||||
/** Lazy registration facade for Codex-backed media understanding. */
|
||||
import type { MediaUnderstandingProvider } from "openclaw/plugin-sdk/media-understanding";
|
||||
/**
|
||||
* Codex-backed media understanding provider for bounded image description and
|
||||
* structured extraction turns.
|
||||
*/
|
||||
import {
|
||||
type JsonSchemaObject,
|
||||
validateJsonSchemaValue,
|
||||
} from "openclaw/plugin-sdk/json-schema-runtime";
|
||||
import type {
|
||||
ImagesDescriptionRequest,
|
||||
ImagesDescriptionResult,
|
||||
MediaUnderstandingProvider,
|
||||
StructuredExtractionRequest,
|
||||
StructuredExtractionResult,
|
||||
} from "openclaw/plugin-sdk/media-understanding";
|
||||
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
|
||||
import { CODEX_PROVIDER_ID, FALLBACK_CODEX_MODELS } from "./provider-catalog.js";
|
||||
import type { CodexAppServerClientLeaseFactory } from "./src/app-server/shared-client.js";
|
||||
import type { CodexAppServerClientFactory } from "./src/app-server/client-factory.js";
|
||||
import type { CodexAppServerClient } from "./src/app-server/client.js";
|
||||
import { resolveCodexAppServerRuntimeOptions } from "./src/app-server/config.js";
|
||||
import { readModelListResult } from "./src/app-server/models.js";
|
||||
import {
|
||||
assertCodexThreadStartResponse,
|
||||
assertCodexTurnStartResponse,
|
||||
readCodexErrorNotification,
|
||||
readCodexTurnCompletedNotification,
|
||||
} from "./src/app-server/protocol-validators.js";
|
||||
import {
|
||||
isJsonObject,
|
||||
type CodexServerNotification,
|
||||
type CodexThreadItem,
|
||||
type CodexThreadStartParams,
|
||||
type CodexTurn,
|
||||
type CodexTurnStartParams,
|
||||
type CodexUserInput,
|
||||
type JsonObject,
|
||||
type JsonValue,
|
||||
} from "./src/app-server/protocol.js";
|
||||
import { buildCodexRuntimeThreadConfig } from "./src/app-server/thread-lifecycle.js";
|
||||
|
||||
const DEFAULT_CODEX_IMAGE_MODEL =
|
||||
FALLBACK_CODEX_MODELS.find((model) => model.inputModalities.includes("image"))?.id ??
|
||||
FALLBACK_CODEX_MODELS[0]?.id;
|
||||
const DEFAULT_CODEX_IMAGE_PROMPT = "Describe the image.";
|
||||
|
||||
/** Dependencies and plugin config for Codex media-understanding calls. */
|
||||
export type CodexMediaUnderstandingProviderOptions = {
|
||||
pluginConfig?: unknown;
|
||||
resolvePluginConfig?: () => unknown;
|
||||
clientLeaseFactory?: CodexAppServerClientLeaseFactory;
|
||||
clientFactory?: CodexAppServerClientFactory;
|
||||
};
|
||||
|
||||
/** Builds a provider whose app-server implementation loads on first use. */
|
||||
/**
|
||||
* Builds the media-understanding provider that delegates image tasks to an
|
||||
* isolated Codex app-server session.
|
||||
*/
|
||||
export function buildCodexMediaUnderstandingProvider(
|
||||
options: CodexMediaUnderstandingProviderOptions = {},
|
||||
): MediaUnderstandingProvider {
|
||||
let runtime: Promise<typeof import("./src/media-understanding-provider.runtime.js")> | undefined;
|
||||
const load = () => (runtime ??= import("./src/media-understanding-provider.runtime.js"));
|
||||
return {
|
||||
id: CODEX_PROVIDER_ID,
|
||||
capabilities: ["image"],
|
||||
...(DEFAULT_CODEX_IMAGE_MODEL ? { defaultModels: { image: DEFAULT_CODEX_IMAGE_MODEL } } : {}),
|
||||
describeImage: async ({ buffer, fileName, mime, ...request }) =>
|
||||
await (
|
||||
await load()
|
||||
).describeCodexImages({ ...request, images: [{ buffer, fileName, mime }] }, options),
|
||||
describeImages: async (request) => await (await load()).describeCodexImages(request, options),
|
||||
extractStructured: async (request) =>
|
||||
await (await load()).extractCodexStructured(request, options),
|
||||
describeImage: async (req) =>
|
||||
describeCodexImages(
|
||||
{
|
||||
images: [
|
||||
{
|
||||
buffer: req.buffer,
|
||||
fileName: req.fileName,
|
||||
mime: req.mime,
|
||||
},
|
||||
],
|
||||
provider: req.provider,
|
||||
model: req.model,
|
||||
prompt: req.prompt,
|
||||
maxTokens: req.maxTokens,
|
||||
timeoutMs: req.timeoutMs,
|
||||
profile: req.profile,
|
||||
preferredProfile: req.preferredProfile,
|
||||
authStore: req.authStore,
|
||||
agentDir: req.agentDir,
|
||||
cfg: req.cfg,
|
||||
},
|
||||
options,
|
||||
),
|
||||
describeImages: async (req) => describeCodexImages(req, options),
|
||||
extractStructured: async (req) => extractCodexStructured(req, options),
|
||||
};
|
||||
}
|
||||
|
||||
async function describeCodexImages(
|
||||
req: ImagesDescriptionRequest,
|
||||
options: CodexMediaUnderstandingProviderOptions,
|
||||
): Promise<ImagesDescriptionResult> {
|
||||
const model = req.model.trim();
|
||||
if (!model) {
|
||||
throw new Error("Codex image understanding requires model id.");
|
||||
}
|
||||
|
||||
const text = await runBoundedCodexVisionTurn({
|
||||
model,
|
||||
profile: req.profile,
|
||||
timeoutMs: req.timeoutMs,
|
||||
agentDir: req.agentDir,
|
||||
options,
|
||||
taskLabel: "image understanding",
|
||||
developerInstructions:
|
||||
"You are OpenClaw's bounded image-understanding worker. Describe only the provided image content. Do not call tools, edit files, or ask follow-up questions.",
|
||||
input: [
|
||||
{ type: "text", text: buildCodexImagePrompt(req), text_elements: [] },
|
||||
...req.images.map((image) => ({
|
||||
type: "image" as const,
|
||||
url: `data:${image.mime ?? "image/png"};base64,${image.buffer.toString("base64")}`,
|
||||
})),
|
||||
],
|
||||
requiredModalities: ["text", "image"],
|
||||
});
|
||||
return { text, model };
|
||||
}
|
||||
|
||||
type BoundedCodexVisionTurnParams = {
|
||||
model: string;
|
||||
profile?: string;
|
||||
timeoutMs: number;
|
||||
agentDir?: string;
|
||||
options: CodexMediaUnderstandingProviderOptions;
|
||||
taskLabel: string;
|
||||
developerInstructions: string;
|
||||
input: CodexUserInput[];
|
||||
requiredModalities: string[];
|
||||
};
|
||||
|
||||
async function runBoundedCodexVisionTurn(params: BoundedCodexVisionTurnParams): Promise<string> {
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({
|
||||
pluginConfig: params.options.pluginConfig,
|
||||
});
|
||||
const timeoutMs = resolveTimerTimeoutMs(params.timeoutMs, 100, 100);
|
||||
const ownsClient = !params.options.clientFactory;
|
||||
// Tests inject a client factory; production creates an isolated app-server
|
||||
// client so media tasks cannot reuse the interactive attempt session.
|
||||
const client = params.options.clientFactory
|
||||
? await params.options.clientFactory(appServer.start, params.profile)
|
||||
: await import("./src/app-server/shared-client.js").then(
|
||||
({ createIsolatedCodexAppServerClient }) =>
|
||||
createIsolatedCodexAppServerClient({
|
||||
startOptions: appServer.start,
|
||||
timeoutMs,
|
||||
authProfileId: params.profile,
|
||||
}),
|
||||
);
|
||||
const abortController = new AbortController();
|
||||
const timeout = setTimeout(() => abortController.abort("timeout"), timeoutMs);
|
||||
timeout.unref?.();
|
||||
|
||||
try {
|
||||
await assertCodexModelSupportsInput({
|
||||
client,
|
||||
model: params.model,
|
||||
requiredModalities: params.requiredModalities,
|
||||
timeoutMs,
|
||||
signal: abortController.signal,
|
||||
});
|
||||
const thread = assertCodexThreadStartResponse(
|
||||
await client.request<unknown>(
|
||||
"thread/start",
|
||||
{
|
||||
model: params.model,
|
||||
modelProvider: "openai",
|
||||
cwd: params.agentDir || process.cwd(),
|
||||
approvalPolicy: "on-request",
|
||||
sandbox: "read-only",
|
||||
serviceName: "OpenClaw",
|
||||
developerInstructions: params.developerInstructions,
|
||||
// Media workers are bounded read-only turns; native code mode and
|
||||
// dynamic tools stay disabled to avoid side effects while inspecting media.
|
||||
config: buildCodexRuntimeThreadConfig(undefined, { nativeCodeModeEnabled: false }),
|
||||
environments: [],
|
||||
dynamicTools: [],
|
||||
experimentalRawEvents: true,
|
||||
persistExtendedHistory: false,
|
||||
ephemeral: true,
|
||||
} satisfies CodexThreadStartParams,
|
||||
{ timeoutMs, signal: abortController.signal },
|
||||
),
|
||||
);
|
||||
const collector = createCodexTurnCollector(thread.thread.id, params.taskLabel);
|
||||
const cleanup = client.addNotificationHandler(collector.handleNotification);
|
||||
const requestCleanup = client.addRequestHandler(denyCodexImageApprovalRequest);
|
||||
try {
|
||||
const turn = assertCodexTurnStartResponse(
|
||||
await client.request<unknown>(
|
||||
"turn/start",
|
||||
{
|
||||
threadId: thread.thread.id,
|
||||
input: params.input,
|
||||
cwd: params.agentDir || process.cwd(),
|
||||
approvalPolicy: "on-request",
|
||||
model: params.model,
|
||||
effort: "low",
|
||||
} satisfies CodexTurnStartParams,
|
||||
{ timeoutMs, signal: abortController.signal },
|
||||
),
|
||||
);
|
||||
const text = await collector.collect(turn.turn, {
|
||||
timeoutMs,
|
||||
signal: abortController.signal,
|
||||
});
|
||||
return text;
|
||||
} finally {
|
||||
requestCleanup();
|
||||
cleanup();
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
if (ownsClient) {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function extractCodexStructured(
|
||||
req: StructuredExtractionRequest,
|
||||
options: CodexMediaUnderstandingProviderOptions,
|
||||
): Promise<StructuredExtractionResult> {
|
||||
const model = req.model.trim();
|
||||
if (!model) {
|
||||
throw new Error("Codex structured extraction requires model id.");
|
||||
}
|
||||
const instructions = req.instructions.trim();
|
||||
if (!instructions) {
|
||||
throw new Error("Codex structured extraction requires instructions.");
|
||||
}
|
||||
if (req.input.length === 0) {
|
||||
throw new Error("Codex structured extraction requires at least one input.");
|
||||
}
|
||||
if (!req.input.some((entry) => entry.type === "image")) {
|
||||
throw new Error("Codex structured extraction requires at least one image input.");
|
||||
}
|
||||
|
||||
const text = await runBoundedCodexVisionTurn({
|
||||
model,
|
||||
profile: req.profile,
|
||||
timeoutMs: req.timeoutMs,
|
||||
agentDir: req.agentDir,
|
||||
options,
|
||||
taskLabel: "structured extraction",
|
||||
developerInstructions:
|
||||
"You are OpenClaw's bounded structured-extraction worker. Return only the requested extraction. Do not call tools, edit files, ask follow-up questions, or include secrets.",
|
||||
input: buildCodexStructuredInput(req),
|
||||
requiredModalities: requiredStructuredModalities(),
|
||||
});
|
||||
return normalizeStructuredExtractionResult({ text, model, provider: req.provider, req });
|
||||
}
|
||||
|
||||
function denyCodexImageApprovalRequest(request: { method: string }): JsonValue | undefined {
|
||||
if (
|
||||
request.method === "item/commandExecution/requestApproval" ||
|
||||
request.method === "item/fileChange/requestApproval"
|
||||
) {
|
||||
return {
|
||||
decision: "decline",
|
||||
reason: "OpenClaw Codex image understanding does not grant tool or file approvals.",
|
||||
};
|
||||
}
|
||||
if (request.method === "item/permissions/requestApproval") {
|
||||
return { permissions: {}, scope: "turn" };
|
||||
}
|
||||
if (request.method.includes("requestApproval")) {
|
||||
return {
|
||||
decision: "decline",
|
||||
reason: "OpenClaw Codex image understanding does not grant native approvals.",
|
||||
};
|
||||
}
|
||||
if (request.method === "mcpServer/elicitation/request") {
|
||||
return { action: "decline" };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function assertCodexModelSupportsInput(params: {
|
||||
client: CodexAppServerClient;
|
||||
model: string;
|
||||
requiredModalities: string[];
|
||||
timeoutMs: number;
|
||||
signal: AbortSignal;
|
||||
}): Promise<void> {
|
||||
const result = await params.client.request<unknown>(
|
||||
"model/list",
|
||||
{ limit: 100, cursor: null, includeHidden: false },
|
||||
{ timeoutMs: Math.min(params.timeoutMs, 5_000), signal: params.signal },
|
||||
);
|
||||
const listed = readModelListResult(result).models;
|
||||
const match = listed.find((entry) => entry.model === params.model || entry.id === params.model);
|
||||
if (!match) {
|
||||
throw new Error(`Codex app-server model not found: ${params.model}`);
|
||||
}
|
||||
if (params.requiredModalities.includes("image") && !match.inputModalities.includes("image")) {
|
||||
throw new Error(`Codex app-server model does not support images: ${params.model}`);
|
||||
}
|
||||
if (params.requiredModalities.includes("text") && !match.inputModalities.includes("text")) {
|
||||
throw new Error(`Codex app-server model does not support text: ${params.model}`);
|
||||
}
|
||||
}
|
||||
|
||||
function buildCodexImagePrompt(req: ImagesDescriptionRequest): string {
|
||||
const prompt = req.prompt?.trim() || DEFAULT_CODEX_IMAGE_PROMPT;
|
||||
if (req.images.length <= 1) {
|
||||
return prompt;
|
||||
}
|
||||
return `${prompt}\n\nAnalyze all ${req.images.length} images together.`;
|
||||
}
|
||||
|
||||
function requiredStructuredModalities(): string[] {
|
||||
return ["text", "image"];
|
||||
}
|
||||
|
||||
function buildCodexStructuredInput(req: StructuredExtractionRequest): CodexUserInput[] {
|
||||
return [
|
||||
{ type: "text", text: buildStructuredExtractionPrompt(req), text_elements: [] },
|
||||
...req.input.map((entry) => {
|
||||
if (entry.type === "text") {
|
||||
return { type: "text" as const, text: entry.text, text_elements: [] };
|
||||
}
|
||||
return {
|
||||
type: "image" as const,
|
||||
url: `data:${entry.mime ?? "image/png"};base64,${entry.buffer.toString("base64")}`,
|
||||
};
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
function buildStructuredExtractionPrompt(req: StructuredExtractionRequest): string {
|
||||
return [
|
||||
req.instructions.trim(),
|
||||
req.schemaName ? `Schema name: ${req.schemaName}` : undefined,
|
||||
req.jsonSchema ? `JSON schema:\n${JSON.stringify(req.jsonSchema)}` : undefined,
|
||||
req.jsonMode === false
|
||||
? "Return the extraction as concise text."
|
||||
: "Return valid JSON only. Do not wrap the JSON in Markdown fences.",
|
||||
]
|
||||
.filter((part): part is string => Boolean(part))
|
||||
.join("\n\n");
|
||||
}
|
||||
|
||||
function isJsonSchemaObject(value: unknown): value is JsonSchemaObject {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function normalizeStructuredExtractionResult(params: {
|
||||
text: string;
|
||||
model: string;
|
||||
provider: string;
|
||||
req: StructuredExtractionRequest;
|
||||
}): StructuredExtractionResult {
|
||||
const result: StructuredExtractionResult = {
|
||||
text: params.text,
|
||||
model: params.model,
|
||||
provider: params.provider,
|
||||
contentType: params.req.jsonMode === false ? "text" : "json",
|
||||
};
|
||||
if (params.req.jsonMode !== false) {
|
||||
try {
|
||||
result.parsed = JSON.parse(params.text);
|
||||
} catch {
|
||||
throw new Error("Codex structured extraction returned invalid JSON.");
|
||||
}
|
||||
if (isJsonSchemaObject(params.req.jsonSchema)) {
|
||||
const validation = validateJsonSchemaValue({
|
||||
schema: params.req.jsonSchema,
|
||||
cacheKey: "codex.media-understanding.extractStructured",
|
||||
value: result.parsed,
|
||||
cache: false,
|
||||
});
|
||||
if (!validation.ok) {
|
||||
const message = validation.errors.map((error) => error.text).join("; ") || "invalid";
|
||||
throw new Error(`Codex structured extraction JSON did not match schema: ${message}`);
|
||||
}
|
||||
result.parsed = validation.value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function createCodexTurnCollector(threadId: string, taskLabel: string) {
|
||||
let turnId: string | undefined;
|
||||
let completedTurn: CodexTurn | undefined;
|
||||
let promptError: string | undefined;
|
||||
const pending: CodexServerNotification[] = [];
|
||||
const assistantTextByItem = new Map<string, string>();
|
||||
const assistantItemOrder: string[] = [];
|
||||
let resolveCompletion: (() => void) | undefined;
|
||||
const completion = new Promise<void>((resolve) => {
|
||||
resolveCompletion = resolve;
|
||||
});
|
||||
|
||||
const rememberAssistantText = (itemId: string, text: string) => {
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
if (!assistantTextByItem.has(itemId)) {
|
||||
assistantItemOrder.push(itemId);
|
||||
}
|
||||
assistantTextByItem.set(itemId, text);
|
||||
};
|
||||
|
||||
const handleNotification = (notification: CodexServerNotification): void => {
|
||||
const params = isJsonObject(notification.params) ? notification.params : undefined;
|
||||
if (!params || readString(params, "threadId") !== threadId) {
|
||||
return;
|
||||
}
|
||||
if (!turnId) {
|
||||
pending.push(notification);
|
||||
return;
|
||||
}
|
||||
const notificationTurnId = readNotificationTurnId(params);
|
||||
if (notificationTurnId !== turnId) {
|
||||
return;
|
||||
}
|
||||
if (notification.method === "item/agentMessage/delta") {
|
||||
const itemId = readString(params, "itemId") ?? readString(params, "id") ?? "assistant";
|
||||
const delta = readString(params, "delta") ?? "";
|
||||
rememberAssistantText(itemId, `${assistantTextByItem.get(itemId) ?? ""}${delta}`);
|
||||
return;
|
||||
}
|
||||
if (notification.method === "turn/completed") {
|
||||
completedTurn =
|
||||
readCodexTurnCompletedNotification(notification.params)?.turn ?? completedTurn;
|
||||
resolveCompletion?.();
|
||||
return;
|
||||
}
|
||||
if (notification.method === "error") {
|
||||
promptError =
|
||||
readCodexErrorNotification(notification.params)?.error.message ??
|
||||
`codex app-server ${taskLabel} turn failed`;
|
||||
resolveCompletion?.();
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
handleNotification,
|
||||
async collect(
|
||||
startedTurn: CodexTurn,
|
||||
options: { timeoutMs: number; signal: AbortSignal },
|
||||
): Promise<string> {
|
||||
turnId = startedTurn.id;
|
||||
if (isTerminalTurn(startedTurn)) {
|
||||
completedTurn = startedTurn;
|
||||
}
|
||||
for (const notification of pending.splice(0)) {
|
||||
handleNotification(notification);
|
||||
}
|
||||
if (!completedTurn && !promptError) {
|
||||
await waitForTurnCompletion({
|
||||
completion,
|
||||
timeoutMs: options.timeoutMs,
|
||||
signal: options.signal,
|
||||
taskLabel,
|
||||
});
|
||||
}
|
||||
if (promptError) {
|
||||
throw new Error(promptError);
|
||||
}
|
||||
if (completedTurn?.status === "failed") {
|
||||
throw new Error(
|
||||
completedTurn.error?.message ?? `codex app-server ${taskLabel} turn failed`,
|
||||
);
|
||||
}
|
||||
const itemText = collectAssistantTextFromItems(completedTurn?.items);
|
||||
const deltaText = assistantItemOrder
|
||||
.map((itemId) => assistantTextByItem.get(itemId)?.trim())
|
||||
.filter((text): text is string => Boolean(text))
|
||||
.join("\n\n")
|
||||
.trim();
|
||||
const text = (itemText || deltaText).trim();
|
||||
if (!text) {
|
||||
throw new Error(`Codex app-server ${taskLabel} turn returned no text.`);
|
||||
}
|
||||
return text;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function waitForTurnCompletion(params: {
|
||||
completion: Promise<void>;
|
||||
timeoutMs: number;
|
||||
signal: AbortSignal;
|
||||
taskLabel: string;
|
||||
}): Promise<void> {
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined;
|
||||
let cleanupAbort: (() => void) | undefined;
|
||||
try {
|
||||
await Promise.race([
|
||||
params.completion,
|
||||
new Promise<never>((_, reject) => {
|
||||
timeout = setTimeout(
|
||||
() => reject(new Error(`codex app-server ${params.taskLabel} turn timed out`)),
|
||||
params.timeoutMs,
|
||||
);
|
||||
timeout.unref?.();
|
||||
const abortListener = () =>
|
||||
reject(new Error(`codex app-server ${params.taskLabel} turn aborted`));
|
||||
params.signal.addEventListener("abort", abortListener, { once: true });
|
||||
cleanupAbort = () => params.signal.removeEventListener("abort", abortListener);
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
cleanupAbort?.();
|
||||
}
|
||||
}
|
||||
|
||||
function collectAssistantTextFromItems(items: CodexThreadItem[] | undefined): string {
|
||||
return (items ?? [])
|
||||
.filter((item) => item.type === "agentMessage")
|
||||
.map((item) => item.text.trim())
|
||||
.filter(Boolean)
|
||||
.join("\n\n")
|
||||
.trim();
|
||||
}
|
||||
|
||||
function readNotificationTurnId(record: JsonObject): string | undefined {
|
||||
const direct = readString(record, "turnId");
|
||||
if (direct) {
|
||||
return direct;
|
||||
}
|
||||
return isJsonObject(record.turn) ? readString(record.turn, "id") : undefined;
|
||||
}
|
||||
|
||||
function readString(record: JsonObject, key: string): string | undefined {
|
||||
const value = record[key];
|
||||
return typeof value === "string" ? value : undefined;
|
||||
}
|
||||
|
||||
function isTerminalTurn(turn: CodexTurn): boolean {
|
||||
return turn.status === "completed" || turn.status === "interrupted" || turn.status === "failed";
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@ import { CODEX_GPT5_BEHAVIOR_CONTRACT } from "./prompt-overlay.js";
|
||||
import { codexProviderDiscovery } from "./provider-discovery.js";
|
||||
import { buildCodexProvider, buildCodexProviderCatalog } from "./provider.js";
|
||||
import { CodexAppServerClient } from "./src/app-server/client.js";
|
||||
import type { listAllCodexAppServerModels } from "./src/app-server/models.js";
|
||||
import type { listCodexAppServerModels } from "./src/app-server/models.js";
|
||||
import {
|
||||
createIsolatedCodexAppServerClient,
|
||||
leaseSharedCodexAppServerClient,
|
||||
getSharedCodexAppServerClient,
|
||||
resetSharedCodexAppServerClientForTests,
|
||||
} from "./src/app-server/shared-client.js";
|
||||
|
||||
@@ -26,8 +26,7 @@ function createFakeCodexClient(): CodexAppServerClient {
|
||||
return {
|
||||
initialize: vi.fn(async () => undefined),
|
||||
request: vi.fn(async () => ({ data: [] })),
|
||||
addNotificationHandler: vi.fn(() => () => undefined),
|
||||
addRequestHandler: vi.fn(() => () => undefined),
|
||||
setActiveSharedLeaseCountProviderForUnscopedNotifications: vi.fn(),
|
||||
addCloseHandler: vi.fn(() => () => undefined),
|
||||
close: vi.fn(),
|
||||
} as unknown as CodexAppServerClient;
|
||||
@@ -40,7 +39,7 @@ const TEST_CODEX_APP_SERVER_CONFIG = {
|
||||
};
|
||||
|
||||
async function listTestCodexAppServerModels(
|
||||
options: Parameters<typeof listAllCodexAppServerModels>[0] = {},
|
||||
options: Parameters<typeof listCodexAppServerModels>[0] = {},
|
||||
) {
|
||||
expect(options.sharedClient).toBe(false);
|
||||
const client = await createIsolatedCodexAppServerClient({
|
||||
@@ -184,33 +183,45 @@ describe("codex provider", () => {
|
||||
expect(resultProvider?.models.map((model) => model.id)).toEqual(["gpt-5.4"]);
|
||||
});
|
||||
|
||||
it("delegates all-page discovery to one model lister call", async () => {
|
||||
const listModels = vi.fn(async () => ({
|
||||
models: [
|
||||
{
|
||||
id: "gpt-5.4",
|
||||
model: "gpt-5.4",
|
||||
hidden: false,
|
||||
inputModalities: ["text", "image"],
|
||||
supportedReasoningEfforts: ["medium"],
|
||||
},
|
||||
{
|
||||
id: "gpt-5.5",
|
||||
model: "gpt-5.5",
|
||||
hidden: false,
|
||||
inputModalities: ["text"],
|
||||
supportedReasoningEfforts: [],
|
||||
},
|
||||
],
|
||||
}));
|
||||
it("pages through live discovery before building the provider catalog", async () => {
|
||||
const listModels = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
models: [
|
||||
{
|
||||
id: "gpt-5.4",
|
||||
model: "gpt-5.4",
|
||||
hidden: false,
|
||||
inputModalities: ["text", "image"],
|
||||
supportedReasoningEfforts: ["medium"],
|
||||
},
|
||||
],
|
||||
nextCursor: "page-2",
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
models: [
|
||||
{
|
||||
id: "gpt-5.5",
|
||||
model: "gpt-5.5",
|
||||
hidden: false,
|
||||
inputModalities: ["text"],
|
||||
supportedReasoningEfforts: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = await buildCodexProviderCatalog({
|
||||
env: {},
|
||||
listModels,
|
||||
});
|
||||
|
||||
expect(listModels).toHaveBeenCalledTimes(1);
|
||||
expectRecordFields(mockCallArg(listModels, 0), {
|
||||
cursor: undefined,
|
||||
limit: 100,
|
||||
sharedClient: false,
|
||||
});
|
||||
expectRecordFields(mockCallArg(listModels, 1), {
|
||||
cursor: "page-2",
|
||||
limit: 100,
|
||||
sharedClient: false,
|
||||
});
|
||||
@@ -266,7 +277,7 @@ describe("codex provider", () => {
|
||||
.mockReturnValueOnce(activeClient)
|
||||
.mockReturnValueOnce(discoveryClient);
|
||||
|
||||
await leaseSharedCodexAppServerClient({
|
||||
await getSharedCodexAppServerClient({
|
||||
startOptions: {
|
||||
transport: "stdio",
|
||||
command: "/tmp/openclaw-test-codex",
|
||||
|
||||
@@ -18,11 +18,16 @@ import {
|
||||
CODEX_PROVIDER_ID,
|
||||
FALLBACK_CODEX_MODELS,
|
||||
} from "./provider-catalog.js";
|
||||
import type { CodexAppServerStartOptions } from "./src/app-server/config.js";
|
||||
import {
|
||||
type CodexAppServerStartOptions,
|
||||
readCodexPluginConfig,
|
||||
resolveCodexAppServerRuntimeOptions,
|
||||
} from "./src/app-server/config.js";
|
||||
import type {
|
||||
CodexAppServerModel,
|
||||
CodexAppServerModelListResult,
|
||||
} from "./src/app-server/models.js";
|
||||
import { buildCodexAppServerUsageSnapshot } from "./src/app-server/rate-limits.js";
|
||||
|
||||
const DEFAULT_DISCOVERY_TIMEOUT_MS = 2500;
|
||||
const LIVE_DISCOVERY_ENV = "OPENCLAW_CODEX_DISCOVERY_LIVE";
|
||||
@@ -34,6 +39,7 @@ const codexCatalogLog = createSubsystemLogger("codex/catalog");
|
||||
type CodexModelLister = (options: {
|
||||
timeoutMs: number;
|
||||
limit?: number;
|
||||
cursor?: string;
|
||||
startOptions?: CodexAppServerStartOptions;
|
||||
sharedClient?: boolean;
|
||||
}) => Promise<CodexAppServerModelListResult>;
|
||||
@@ -117,11 +123,6 @@ export function buildCodexProvider(options: BuildCodexProviderOptions = {}): Pro
|
||||
}
|
||||
const runtimePluginConfig = resolvePluginConfigObject(ctx.config, CODEX_PROVIDER_ID);
|
||||
const pluginConfig = runtimePluginConfig ?? (ctx.config ? undefined : options.pluginConfig);
|
||||
const [{ resolveCodexAppServerRuntimeOptions }, { buildCodexAppServerUsageSnapshot }] =
|
||||
await Promise.all([
|
||||
import("./src/app-server/config.js"),
|
||||
import("./src/app-server/rate-limits.js"),
|
||||
]);
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig });
|
||||
const rateLimits = await (options.readRateLimits ?? requestCodexAppServerRateLimitsLazy)({
|
||||
timeoutMs: ctx.timeoutMs,
|
||||
@@ -155,15 +156,13 @@ export function buildCodexProvider(options: BuildCodexProviderOptions = {}): Pro
|
||||
export async function buildCodexProviderCatalog(
|
||||
options: BuildCatalogOptions = {},
|
||||
): Promise<{ provider: ModelProviderConfig }> {
|
||||
const { readCodexPluginConfig, resolveCodexAppServerRuntimeOptions } =
|
||||
await import("./src/app-server/config.js");
|
||||
const config = readCodexPluginConfig(options.pluginConfig);
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig: options.pluginConfig });
|
||||
const timeoutMs = normalizeTimeoutMs(config.discovery?.timeoutMs);
|
||||
let discovered: CodexAppServerModel[] = [];
|
||||
if (config.discovery?.enabled !== false && !shouldSkipLiveDiscovery(options.env)) {
|
||||
discovered = await listModelsBestEffort({
|
||||
listModels: options.listModels ?? listAllCodexAppServerModelsLazy,
|
||||
listModels: options.listModels ?? listCodexAppServerModelsLazy,
|
||||
timeoutMs,
|
||||
startOptions: appServer.start,
|
||||
onDiscoveryFailure: options.onDiscoveryFailure,
|
||||
@@ -201,14 +200,22 @@ async function listModelsBestEffort(params: {
|
||||
onDiscoveryFailure?: (error: unknown) => void;
|
||||
}): Promise<CodexAppServerModel[]> {
|
||||
try {
|
||||
// The all-pages helper keeps one app-server client alive across pagination.
|
||||
const result = await params.listModels({
|
||||
timeoutMs: params.timeoutMs,
|
||||
limit: MODEL_DISCOVERY_PAGE_LIMIT,
|
||||
startOptions: params.startOptions,
|
||||
sharedClient: false,
|
||||
});
|
||||
return result.models.filter((model) => !model.hidden);
|
||||
const models: CodexAppServerModel[] = [];
|
||||
let cursor: string | undefined;
|
||||
do {
|
||||
// App-server model listing is paginated; collect every visible model so
|
||||
// aliases and picker rows match the current Codex account.
|
||||
const result = await params.listModels({
|
||||
timeoutMs: params.timeoutMs,
|
||||
limit: MODEL_DISCOVERY_PAGE_LIMIT,
|
||||
cursor,
|
||||
startOptions: params.startOptions,
|
||||
sharedClient: false,
|
||||
});
|
||||
models.push(...result.models.filter((model) => !model.hidden));
|
||||
cursor = result.nextCursor;
|
||||
} while (cursor);
|
||||
return models;
|
||||
} catch (error) {
|
||||
params.onDiscoveryFailure?.(error);
|
||||
codexCatalogLog.debug("codex model discovery failed; using fallback catalog", {
|
||||
@@ -218,14 +225,15 @@ async function listModelsBestEffort(params: {
|
||||
}
|
||||
}
|
||||
|
||||
async function listAllCodexAppServerModelsLazy(options: {
|
||||
async function listCodexAppServerModelsLazy(options: {
|
||||
timeoutMs: number;
|
||||
limit?: number;
|
||||
cursor?: string;
|
||||
startOptions?: CodexAppServerStartOptions;
|
||||
sharedClient?: boolean;
|
||||
}): Promise<CodexAppServerModelListResult> {
|
||||
const { listAllCodexAppServerModels } = await import("./src/app-server/models.js");
|
||||
return listAllCodexAppServerModels(options);
|
||||
const { listCodexAppServerModels } = await import("./src/app-server/models.js");
|
||||
return listCodexAppServerModels(options);
|
||||
}
|
||||
|
||||
async function requestCodexAppServerRateLimitsLazy(options: {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
// Codex tests cover app server policy plugin behavior.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveCodexAppServerForOpenClawToolPolicy } from "./app-server-policy.js";
|
||||
import {
|
||||
resolveCodexAppServerForModelProvider,
|
||||
resolveCodexAppServerForOpenClawToolPolicy,
|
||||
} from "./app-server-policy.js";
|
||||
import { readCodexPluginConfig, resolveCodexAppServerRuntimeOptions } from "./config.js";
|
||||
|
||||
describe("Codex app-server policy", () => {
|
||||
@@ -66,4 +69,143 @@ describe("Codex app-server policy", () => {
|
||||
expect(explicitEnv.approvalPolicy).toBe("never");
|
||||
expect(explicitRequirements.approvalPolicy).toBe("never");
|
||||
});
|
||||
|
||||
it("keeps model-backed reviewers for explicit OpenAI model providers", () => {
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({
|
||||
env: {},
|
||||
requirementsToml: null,
|
||||
execMode: "auto",
|
||||
modelProvider: "openai",
|
||||
});
|
||||
|
||||
expect(
|
||||
resolveCodexAppServerForModelProvider({
|
||||
appServer,
|
||||
provider: "codex",
|
||||
model: "openai/gpt-5.5",
|
||||
}).approvalsReviewer,
|
||||
).toBe("auto_review");
|
||||
expect(
|
||||
resolveCodexAppServerForModelProvider({
|
||||
appServer,
|
||||
provider: "codex",
|
||||
model: "gpt-5.5",
|
||||
}).approvalsReviewer,
|
||||
).toBe("user");
|
||||
expect(
|
||||
resolveCodexAppServerForModelProvider({ appServer, provider: "openai" }).approvalsReviewer,
|
||||
).toBe("auto_review");
|
||||
});
|
||||
|
||||
it("uses human approval for OpenAI-compatible custom endpoints", () => {
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({
|
||||
env: {},
|
||||
requirementsToml: null,
|
||||
execMode: "auto",
|
||||
modelProvider: "openai",
|
||||
model: "gpt-5.5",
|
||||
config: {
|
||||
models: {
|
||||
providers: {
|
||||
openai: {
|
||||
baseUrl: "http://localhost:8080/v1",
|
||||
models: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(appServer.approvalsReviewer).toBe("user");
|
||||
expect(
|
||||
resolveCodexAppServerForModelProvider({
|
||||
appServer,
|
||||
provider: "openai",
|
||||
model: "gpt-5.5",
|
||||
config: {
|
||||
models: {
|
||||
providers: {
|
||||
openai: {
|
||||
baseUrl: "http://localhost:8080/v1",
|
||||
models: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}).approvalsReviewer,
|
||||
).toBe("user");
|
||||
});
|
||||
|
||||
it("uses human approval instead of Codex Guardian for custom model providers", () => {
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({
|
||||
env: {},
|
||||
requirementsToml: null,
|
||||
execMode: "auto",
|
||||
modelProvider: "openai",
|
||||
});
|
||||
|
||||
const resolved = resolveCodexAppServerForModelProvider({
|
||||
appServer,
|
||||
provider: "lmstudio",
|
||||
});
|
||||
const vendorPrefixedModel = resolveCodexAppServerForModelProvider({
|
||||
appServer,
|
||||
provider: "openrouter",
|
||||
model: "openai/gpt-5.5",
|
||||
});
|
||||
|
||||
expect(appServer.approvalsReviewer).toBe("auto_review");
|
||||
expect(resolved.approvalPolicy).toBe("on-request");
|
||||
expect(resolved.sandbox).toBe("workspace-write");
|
||||
expect(resolved.approvalsReviewer).toBe("user");
|
||||
expect(vendorPrefixedModel.approvalsReviewer).toBe("user");
|
||||
});
|
||||
|
||||
it("infers custom providers from provider-qualified model refs", () => {
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({
|
||||
env: {},
|
||||
requirementsToml: null,
|
||||
execMode: "auto",
|
||||
});
|
||||
|
||||
expect(
|
||||
resolveCodexAppServerForModelProvider({
|
||||
appServer,
|
||||
model: "lmstudio/local-model",
|
||||
}).approvalsReviewer,
|
||||
).toBe("user");
|
||||
});
|
||||
|
||||
it("uses provider-qualified model refs to override broad native provider wrappers", () => {
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({
|
||||
env: {},
|
||||
requirementsToml: null,
|
||||
execMode: "auto",
|
||||
});
|
||||
|
||||
expect(
|
||||
resolveCodexAppServerForModelProvider({
|
||||
appServer,
|
||||
provider: "codex",
|
||||
model: "lmstudio/local-model",
|
||||
}).approvalsReviewer,
|
||||
).toBe("user");
|
||||
});
|
||||
|
||||
it("downgrades legacy guardian_subagent for custom model providers", () => {
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({
|
||||
env: {},
|
||||
requirementsToml: null,
|
||||
pluginConfig: {
|
||||
appServer: {
|
||||
mode: "guardian",
|
||||
approvalsReviewer: "guardian_subagent",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(
|
||||
resolveCodexAppServerForModelProvider({ appServer, provider: "local" }).approvalsReviewer,
|
||||
).toBe("user");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
* Policy promotion for Codex app-server runs that can safely use OpenClaw tool
|
||||
* approvals.
|
||||
*/
|
||||
import type {
|
||||
CodexAppServerRuntimeOptions,
|
||||
CodexPluginConfig,
|
||||
OpenClawExecPolicyForCodexAppServer,
|
||||
import {
|
||||
canUseCodexModelBackedApprovalsReviewerForModel,
|
||||
type CodexAppServerRuntimeOptions,
|
||||
type CodexPluginConfig,
|
||||
type OpenClawExecPolicyForCodexAppServer,
|
||||
} from "./config.js";
|
||||
|
||||
/**
|
||||
@@ -44,6 +45,35 @@ export function resolveCodexAppServerForOpenClawToolPolicy(params: {
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveCodexAppServerForModelProvider(params: {
|
||||
appServer: CodexAppServerRuntimeOptions;
|
||||
provider?: string;
|
||||
model?: string;
|
||||
config?: Parameters<typeof canUseCodexModelBackedApprovalsReviewerForModel>[0]["config"];
|
||||
env?: NodeJS.ProcessEnv;
|
||||
agentDir?: string;
|
||||
codexConfigToml?: string | null;
|
||||
}): CodexAppServerRuntimeOptions {
|
||||
const explicitProvider = normalizeModelBackedReviewerProvider(params.provider);
|
||||
if (
|
||||
!isCodexModelBackedApprovalsReviewer(params.appServer.approvalsReviewer) ||
|
||||
canUseCodexModelBackedApprovalsReviewerForModel({
|
||||
modelProvider: explicitProvider,
|
||||
model: params.model,
|
||||
config: params.config,
|
||||
env: params.env,
|
||||
agentDir: params.agentDir,
|
||||
codexConfigToml: params.codexConfigToml,
|
||||
})
|
||||
) {
|
||||
return params.appServer;
|
||||
}
|
||||
return {
|
||||
...params.appServer,
|
||||
approvalsReviewer: "user",
|
||||
};
|
||||
}
|
||||
|
||||
function isCodexAppServerPolicyMode(value: unknown): boolean {
|
||||
return value === "guardian" || value === "yolo";
|
||||
}
|
||||
@@ -53,3 +83,12 @@ function isCodexAppServerApprovalPolicy(value: unknown): boolean {
|
||||
value === "never" || value === "on-request" || value === "on-failure" || value === "untrusted"
|
||||
);
|
||||
}
|
||||
|
||||
function isCodexModelBackedApprovalsReviewer(value: string): boolean {
|
||||
return value === "auto_review" || value === "guardian_subagent";
|
||||
}
|
||||
|
||||
function normalizeModelBackedReviewerProvider(provider: string | undefined): string | undefined {
|
||||
const normalized = provider?.trim().toLowerCase();
|
||||
return normalized || undefined;
|
||||
}
|
||||
|
||||
@@ -285,7 +285,8 @@ function matchesCurrentTurn(
|
||||
if (!requestParams) {
|
||||
return false;
|
||||
}
|
||||
const requestThreadId = readString(requestParams, "threadId");
|
||||
const requestThreadId =
|
||||
readString(requestParams, "threadId") ?? readString(requestParams, "conversationId");
|
||||
const requestTurnId = readString(requestParams, "turnId");
|
||||
return requestThreadId === threadId && requestTurnId === turnId;
|
||||
}
|
||||
|
||||
@@ -2,41 +2,10 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
interruptCodexTurnBestEffort,
|
||||
runCodexTurnStartWithLease,
|
||||
settleCodexAppServerClientLease,
|
||||
unsubscribeCodexThreadBestEffort,
|
||||
validateCodexThreadCreationResponse,
|
||||
} from "./attempt-client-cleanup.js";
|
||||
import { CodexAppServerRpcError } from "./client.js";
|
||||
|
||||
describe("Codex app-server attempt client cleanup", () => {
|
||||
it("keeps the client lease after a structured turn-start rejection", async () => {
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
const error = new CodexAppServerRpcError({ message: "turn rejected" }, "turn/start");
|
||||
|
||||
await expect(
|
||||
runCodexTurnStartWithLease({ abandon } as never, async () => {
|
||||
throw error;
|
||||
}),
|
||||
).rejects.toBe(error);
|
||||
|
||||
expect(abandon).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("abandons only the exact client lease after an ambiguous turn-start timeout", async () => {
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
const otherAbandon = vi.fn(async () => undefined);
|
||||
|
||||
await expect(
|
||||
runCodexTurnStartWithLease({ abandon } as never, async () => {
|
||||
throw new Error("turn/start timed out");
|
||||
}),
|
||||
).rejects.toThrow("turn/start timed out");
|
||||
|
||||
expect(abandon).toHaveBeenCalledTimes(1);
|
||||
expect(otherAbandon).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("interrupts turns with optional request timeout", () => {
|
||||
const request = vi.fn(async () => ({}));
|
||||
|
||||
@@ -53,58 +22,7 @@ describe("Codex app-server attempt client cleanup", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("unsubscribes a retained thread when its create response is malformed", async () => {
|
||||
const request = vi.fn(async () => ({}));
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
const invalidResponse = { thread: { id: "thread-1" } };
|
||||
|
||||
await expect(
|
||||
validateCodexThreadCreationResponse(
|
||||
{ client: { request } as never, abandon },
|
||||
invalidResponse,
|
||||
() => {
|
||||
throw new Error("invalid thread/start response");
|
||||
},
|
||||
),
|
||||
).rejects.toThrow("invalid thread/start response");
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"thread/unsubscribe",
|
||||
{ threadId: "thread-1" },
|
||||
{ timeoutMs: 5_000 },
|
||||
);
|
||||
expect(abandon).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["omits the retained thread id", {}, vi.fn(async () => ({}))],
|
||||
[
|
||||
"cannot confirm unsubscribe",
|
||||
{ thread: { id: "thread-1" } },
|
||||
vi.fn(async () => {
|
||||
throw new Error("connection lost");
|
||||
}),
|
||||
],
|
||||
])(
|
||||
"retires the client when a malformed create response %s",
|
||||
async (_label, response, request) => {
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
|
||||
await expect(
|
||||
validateCodexThreadCreationResponse(
|
||||
{ client: { request } as never, abandon },
|
||||
response,
|
||||
() => {
|
||||
throw new Error("invalid thread/start response");
|
||||
},
|
||||
),
|
||||
).rejects.toThrow("subscription could not be released");
|
||||
|
||||
expect(abandon).toHaveBeenCalledOnce();
|
||||
},
|
||||
);
|
||||
|
||||
it("reports unsubscribe cleanup failures", async () => {
|
||||
it("swallows unsubscribe cleanup failures", async () => {
|
||||
const request = vi.fn(async () => {
|
||||
throw new Error("already gone");
|
||||
});
|
||||
@@ -114,7 +32,7 @@ describe("Codex app-server attempt client cleanup", () => {
|
||||
threadId: "thread-1",
|
||||
timeoutMs: 123,
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"thread/unsubscribe",
|
||||
@@ -122,31 +40,4 @@ describe("Codex app-server attempt client cleanup", () => {
|
||||
{ timeoutMs: 123 },
|
||||
);
|
||||
});
|
||||
|
||||
it("returns leases only after thread cleanup is confirmed", async () => {
|
||||
const release = vi.fn();
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
await settleCodexAppServerClientLease(
|
||||
{ client: { request: vi.fn(async () => ({})) }, release, abandon } as never,
|
||||
{ threadId: "thread-ok", timeoutMs: 123 },
|
||||
);
|
||||
expect(release).toHaveBeenCalledOnce();
|
||||
expect(abandon).not.toHaveBeenCalled();
|
||||
|
||||
release.mockClear();
|
||||
await settleCodexAppServerClientLease(
|
||||
{
|
||||
client: {
|
||||
request: vi.fn(async () => {
|
||||
throw new Error("unsubscribe failed");
|
||||
}),
|
||||
},
|
||||
release,
|
||||
abandon,
|
||||
} as never,
|
||||
{ threadId: "thread-stale", timeoutMs: 123 },
|
||||
);
|
||||
expect(release).not.toHaveBeenCalled();
|
||||
expect(abandon).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,126 +2,14 @@
|
||||
* Best-effort cleanup helpers for timed-out or aborted Codex app-server turns.
|
||||
*/
|
||||
import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import { CodexAppServerRpcError, type CodexAppServerClient } from "./client.js";
|
||||
import { isJsonObject, readCodexThreadCreationResponseId } from "./protocol.js";
|
||||
import type { CodexAppServerClientLease } from "./shared-client.js";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import { retireSharedCodexAppServerClientIfCurrent } from "./shared-client.js";
|
||||
|
||||
/** Timeout for best-effort app-server turn interruption during cleanup. */
|
||||
export const CODEX_APP_SERVER_INTERRUPT_TIMEOUT_MS = 5_000;
|
||||
/** Timeout for best-effort thread unsubscribe during cleanup. */
|
||||
export const CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS = 5_000;
|
||||
|
||||
/** The connection's thread-subscription ownership can no longer be proven. */
|
||||
export class CodexAppServerUnsafeSubscriptionError extends Error {
|
||||
constructor(message: string, options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
this.name = "CodexAppServerUnsafeSubscriptionError";
|
||||
}
|
||||
}
|
||||
|
||||
export function isCodexAppServerUnsafeSubscriptionError(
|
||||
error: unknown,
|
||||
): error is CodexAppServerUnsafeSubscriptionError {
|
||||
return error instanceof CodexAppServerUnsafeSubscriptionError;
|
||||
}
|
||||
|
||||
/** A resume response may only describe the thread this connection retained. */
|
||||
export function assertCodexThreadResumeSubscription(
|
||||
requestedThreadId: string,
|
||||
returnedThreadId: string,
|
||||
): void {
|
||||
if (returnedThreadId !== requestedThreadId) {
|
||||
throw new CodexAppServerUnsafeSubscriptionError(
|
||||
`Codex thread/resume returned ${returnedThreadId} for ${requestedThreadId}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Retires the exact client lease when turn acceptance is ambiguous. */
|
||||
export async function runCodexTurnStartWithLease<T>(
|
||||
lease: CodexAppServerClientLease,
|
||||
startTurn: () => Promise<T>,
|
||||
): Promise<T> {
|
||||
try {
|
||||
return await startTurn();
|
||||
} catch (error) {
|
||||
// Structured RPC rejection happens before Codex accepts the turn. Transport,
|
||||
// timeout, and abort failures may hide an accepted turn with an unknown id.
|
||||
if (!(error instanceof CodexAppServerRpcError)) {
|
||||
await lease.abandon();
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/** Retries once when native work wins the race immediately before turn/start. */
|
||||
export async function runCodexTurnStartWithNativeTurnRetry<T>(params: {
|
||||
startTurn: () => Promise<T>;
|
||||
waitForActiveTurnCompletion: () => Promise<boolean>;
|
||||
afterActiveTurnCompletion?: () => Promise<void>;
|
||||
onRetry?: () => void;
|
||||
}): Promise<T> {
|
||||
try {
|
||||
return await params.startTurn();
|
||||
} catch (error) {
|
||||
if (!isCodexActiveTurnNotSteerableError(error)) {
|
||||
throw error;
|
||||
}
|
||||
params.onRetry?.();
|
||||
if (!(await params.waitForActiveTurnCompletion())) {
|
||||
throw error;
|
||||
}
|
||||
await params.afterActiveTurnCompletion?.();
|
||||
return await params.startTurn();
|
||||
}
|
||||
}
|
||||
|
||||
/** True for Codex's structured rejection when native work already owns the thread. */
|
||||
export function isCodexActiveTurnNotSteerableError(error: unknown): boolean {
|
||||
if (!(error instanceof CodexAppServerRpcError) || !isJsonObject(error.data)) {
|
||||
return false;
|
||||
}
|
||||
const info = error.data.codexErrorInfo;
|
||||
return isJsonObject(info) && isJsonObject(info.activeTurnNotSteerable);
|
||||
}
|
||||
|
||||
/** Validates a create response and retires the client unless cleanup is confirmed. */
|
||||
export async function validateCodexThreadCreationResponse<T>(
|
||||
owner: {
|
||||
client: CodexAppServerClient;
|
||||
abandon: () => Promise<void>;
|
||||
},
|
||||
response: unknown,
|
||||
validate: (value: unknown) => T,
|
||||
): Promise<T> {
|
||||
try {
|
||||
return validate(response);
|
||||
} catch (error) {
|
||||
const threadId = readCodexThreadCreationResponseId(response);
|
||||
const released = threadId
|
||||
? await unsubscribeCodexThreadBestEffort(owner.client, {
|
||||
threadId,
|
||||
timeoutMs: CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
})
|
||||
: false;
|
||||
if (released) {
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
await owner.abandon();
|
||||
} catch (abandonError) {
|
||||
throw new CodexAppServerUnsafeSubscriptionError(
|
||||
"Codex thread creation response was invalid and its client could not be retired",
|
||||
{ cause: abandonError },
|
||||
);
|
||||
}
|
||||
throw new CodexAppServerUnsafeSubscriptionError(
|
||||
"Codex thread creation response was invalid and its subscription could not be released",
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Sends a turn interrupt without blocking abort cleanup on app-server errors. */
|
||||
export function interruptCodexTurnBestEffort(
|
||||
client: CodexAppServerClient,
|
||||
@@ -148,56 +36,28 @@ export function interruptCodexTurnBestEffort(
|
||||
}
|
||||
}
|
||||
|
||||
/** Unsubscribes from a thread and reports whether wire cleanup was confirmed. */
|
||||
/** Unsubscribes from a thread while swallowing cleanup-only failures. */
|
||||
export async function unsubscribeCodexThreadBestEffort(
|
||||
client: CodexAppServerClient,
|
||||
params: {
|
||||
threadId: string;
|
||||
timeoutMs: number;
|
||||
},
|
||||
): Promise<boolean> {
|
||||
): Promise<void> {
|
||||
try {
|
||||
await client.request(
|
||||
"thread/unsubscribe",
|
||||
{ threadId: params.threadId },
|
||||
{ timeoutMs: params.timeoutMs },
|
||||
);
|
||||
return true;
|
||||
} catch (error) {
|
||||
embeddedAgentLog.debug("codex app-server thread unsubscribe cleanup failed", {
|
||||
threadId: params.threadId,
|
||||
error,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns one exact client lease to the pool only after subscription cleanup succeeds. */
|
||||
export async function settleCodexAppServerClientLease(
|
||||
lease: CodexAppServerClientLease,
|
||||
params: {
|
||||
threadId?: string;
|
||||
timeoutMs: number;
|
||||
abandon?: boolean;
|
||||
},
|
||||
): Promise<void> {
|
||||
if (params.abandon) {
|
||||
await lease.abandon();
|
||||
return;
|
||||
}
|
||||
if (
|
||||
params.threadId &&
|
||||
!(await unsubscribeCodexThreadBestEffort(lease.client, {
|
||||
threadId: params.threadId,
|
||||
timeoutMs: params.timeoutMs,
|
||||
}))
|
||||
) {
|
||||
await lease.abandon();
|
||||
return;
|
||||
}
|
||||
lease.release();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retires the shared client after a timed-out turn so later runs do not reuse a
|
||||
* potentially wedged app-server connection.
|
||||
@@ -208,9 +68,10 @@ export async function retireCodexAppServerClientAfterTimedOutTurn(
|
||||
threadId: string;
|
||||
turnId: string;
|
||||
reason: string;
|
||||
abandonClientLease: () => Promise<void>;
|
||||
},
|
||||
): Promise<void> {
|
||||
const retiredSharedClient = retireSharedCodexAppServerClientIfCurrent(client);
|
||||
const detachedSharedClient = Boolean(retiredSharedClient);
|
||||
interruptCodexTurnBestEffort(client, {
|
||||
threadId: params.threadId,
|
||||
turnId: params.turnId,
|
||||
@@ -220,10 +81,28 @@ export async function retireCodexAppServerClientAfterTimedOutTurn(
|
||||
threadId: params.threadId,
|
||||
timeoutMs: CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
});
|
||||
await params.abandonClientLease();
|
||||
let closedClient = retiredSharedClient?.closed ?? false;
|
||||
if (!detachedSharedClient) {
|
||||
const close = (client as { close?: () => void }).close;
|
||||
if (typeof close === "function") {
|
||||
try {
|
||||
close.call(client);
|
||||
closedClient = true;
|
||||
} catch (error) {
|
||||
embeddedAgentLog.debug("codex app-server client close failed during timeout cleanup", {
|
||||
threadId: params.threadId,
|
||||
turnId: params.turnId,
|
||||
error,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
embeddedAgentLog.warn("codex app-server client retired after timed-out turn", {
|
||||
threadId: params.threadId,
|
||||
turnId: params.turnId,
|
||||
reason: params.reason,
|
||||
detachedSharedClient,
|
||||
closedClient,
|
||||
activeSharedClientLeases: retiredSharedClient?.activeLeases ?? 0,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import {
|
||||
} from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import { resolveAgentWorkspaceDir } from "openclaw/plugin-sdk/agent-runtime";
|
||||
import { buildMemorySystemPromptAddition } from "openclaw/plugin-sdk/core";
|
||||
import { MESSAGE_TOOL_DELIVERY_HINTS } from "openclaw/plugin-sdk/message-tool-delivery-hints";
|
||||
import type { CodexDynamicToolSpec, JsonValue } from "./protocol.js";
|
||||
import { isJsonObject } from "./protocol.js";
|
||||
import type { CodexAppServerThreadBinding } from "./session-binding.js";
|
||||
@@ -585,12 +584,17 @@ export function prependCodexOpenClawPromptContext(
|
||||
return [context?.trim(), deliverySection, promptSection].filter(Boolean).join("\n\n");
|
||||
}
|
||||
|
||||
const CODEX_DELIVERY_HINT_LINES = [
|
||||
"Delivery: to send a message, use the `message` tool.",
|
||||
"Delivery: Final assistant text is not automatically delivered in this run. Use the `message` tool to send user-visible output.",
|
||||
] as const;
|
||||
|
||||
function splitLeadingCodexDeliveryHint(prompt: string): {
|
||||
deliveryHint?: string;
|
||||
prompt: string;
|
||||
} {
|
||||
const trimmedStart = prompt.trimStart();
|
||||
const matchedHint = MESSAGE_TOOL_DELIVERY_HINTS.find((hint) => trimmedStart.startsWith(hint));
|
||||
const matchedHint = CODEX_DELIVERY_HINT_LINES.find((hint) => trimmedStart.startsWith(hint));
|
||||
if (!matchedHint) {
|
||||
return { prompt };
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
isFileChangePatchUpdatedNotification,
|
||||
isAssistantCommentaryCompletionNotification,
|
||||
isNativeToolProgressNotification,
|
||||
isNativeResponseStreamDeltaNotification,
|
||||
isPendingOpenClawDynamicToolCompletionNotification,
|
||||
isRawAssistantProgressNotification,
|
||||
isRawReasoningCompletionNotification,
|
||||
@@ -16,6 +17,7 @@ import {
|
||||
isReasoningProgressNotification,
|
||||
isReasoningItemCompletionNotification,
|
||||
isRetryableErrorNotification,
|
||||
isTurnNotification,
|
||||
readCodexNotificationItem,
|
||||
readNotificationItemId,
|
||||
shouldDisarmAssistantCompletionIdleWatch,
|
||||
@@ -23,7 +25,6 @@ import {
|
||||
} from "./attempt-notifications.js";
|
||||
import { CODEX_POST_REASONING_REPLY_IDLE_TIMEOUT_MS } from "./attempt-timeouts.js";
|
||||
import type { CodexAttemptTurnWatchController } from "./attempt-turn-watches.js";
|
||||
import { isCodexNotificationForTurn } from "./notification-correlation.js";
|
||||
import type { CodexServerNotification } from "./protocol.js";
|
||||
|
||||
type CodexExecutionPhase =
|
||||
@@ -69,7 +70,7 @@ export function isTerminalCodexTurnNotificationForTurn(params: {
|
||||
turnId: string;
|
||||
currentPromptTexts: string[];
|
||||
}): boolean {
|
||||
if (!isCodexNotificationForTurn(params.notification.params, params.threadId, params.turnId)) {
|
||||
if (!isTurnNotification(params.notification.params, params.threadId, params.turnId)) {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
@@ -104,15 +105,16 @@ export function applyCodexTurnNotificationState(params: {
|
||||
turnCrossedToolHandoff: boolean;
|
||||
} {
|
||||
const { notification, turnWatches } = params;
|
||||
const isCurrentTurnNotification = isCodexNotificationForTurn(
|
||||
const isCurrentTurnNotification = isTurnNotification(
|
||||
notification.params,
|
||||
params.threadId,
|
||||
params.turnId,
|
||||
);
|
||||
const isTurnCompletion = notification.method === "turn/completed" && isCurrentTurnNotification;
|
||||
const isNativeResponseStreamDelta = isNativeResponseStreamDeltaNotification(notification);
|
||||
let turnCrossedToolHandoff = params.turnCrossedToolHandoff;
|
||||
|
||||
if (isCurrentTurnNotification) {
|
||||
if (isCurrentTurnNotification && !isNativeResponseStreamDelta) {
|
||||
turnWatches.touchActivity(`notification:${notification.method}`, {
|
||||
details: describeNotificationActivity(notification),
|
||||
attemptProgress: true,
|
||||
@@ -248,6 +250,7 @@ export function applyCodexTurnNotificationState(params: {
|
||||
!turnWatches.isCompletionIdleWatchPinnedByTerminalError() &&
|
||||
notification.method !== "turn/completed" &&
|
||||
isCurrentTurnNotification &&
|
||||
!isNativeResponseStreamDelta &&
|
||||
!trackedDynamicToolCompletion &&
|
||||
!rawToolOutputCompletion &&
|
||||
!postToolProgressNeedsTerminalGuard &&
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
/**
|
||||
* Predicates and readers for Codex app-server notification envelopes.
|
||||
*/
|
||||
import { asBoolean } from "openclaw/plugin-sdk/string-coerce-runtime";
|
||||
import {
|
||||
describeCodexNotificationCorrelation,
|
||||
isCodexNotificationForTurn,
|
||||
} from "./notification-correlation.js";
|
||||
import {
|
||||
isJsonObject,
|
||||
type CodexServerNotification,
|
||||
@@ -211,6 +216,13 @@ export function isNativeToolProgressNotification(notification: CodexServerNotifi
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true for raw native response stream delta events. */
|
||||
export function isNativeResponseStreamDeltaNotification(
|
||||
notification: CodexServerNotification,
|
||||
): boolean {
|
||||
return notification.method.startsWith("response.") && notification.method.endsWith(".delta");
|
||||
}
|
||||
|
||||
/** Returns true for file-change patch update notifications. */
|
||||
export function isFileChangePatchUpdatedNotification(
|
||||
notification: CodexServerNotification,
|
||||
@@ -265,9 +277,74 @@ function readRawAssistantTextPreview(item: JsonObject): string | undefined {
|
||||
return text.length > 240 ? `${text.slice(0, 237)}...` : text;
|
||||
}
|
||||
|
||||
/** Returns true when notification params correlate to a specific thread/turn. */
|
||||
export function isTurnNotification(
|
||||
value: JsonValue | undefined,
|
||||
threadId: string,
|
||||
turnId: string,
|
||||
): boolean {
|
||||
return isCodexNotificationForTurn(value, threadId, turnId);
|
||||
}
|
||||
|
||||
/** Returns true when a correlated notification belongs to another active run. */
|
||||
export function isCodexNotificationOutsideActiveRun(
|
||||
correlation: ReturnType<typeof describeCodexNotificationCorrelation>,
|
||||
): boolean {
|
||||
const hasThreadScope = Boolean(correlation.threadId || correlation.nestedTurnThreadId);
|
||||
if (!hasThreadScope) {
|
||||
return false;
|
||||
}
|
||||
if (!correlation.matchesActiveThread) {
|
||||
return true;
|
||||
}
|
||||
const hasTurnScope = Boolean(correlation.turnId || correlation.nestedTurnId);
|
||||
return hasTurnScope && correlation.matchesActiveTurn === false;
|
||||
}
|
||||
|
||||
/** Checks request params that must contain the current thread and turn ids. */
|
||||
export function isCurrentThreadTurnRequestParams(
|
||||
value: JsonValue | undefined,
|
||||
threadId: string,
|
||||
turnId: string,
|
||||
): boolean {
|
||||
if (!isJsonObject(value)) {
|
||||
return false;
|
||||
}
|
||||
return readString(value, "threadId") === threadId && readString(value, "turnId") === turnId;
|
||||
}
|
||||
|
||||
/** Checks approval request params, accepting `conversationId` as thread id. */
|
||||
export function isCurrentApprovalTurnRequestParams(
|
||||
value: JsonValue | undefined,
|
||||
threadId: string,
|
||||
turnId: string,
|
||||
): boolean {
|
||||
if (!isJsonObject(value)) {
|
||||
return false;
|
||||
}
|
||||
const requestThreadId = readString(value, "threadId") ?? readString(value, "conversationId");
|
||||
return requestThreadId === threadId && readString(value, "turnId") === turnId;
|
||||
}
|
||||
|
||||
/** Checks request params where `turnId` may be omitted or null for the thread. */
|
||||
export function isCurrentThreadOptionalTurnRequestParams(
|
||||
value: JsonValue | undefined,
|
||||
threadId: string,
|
||||
turnId: string,
|
||||
): boolean {
|
||||
if (!isJsonObject(value) || readString(value, "threadId") !== threadId) {
|
||||
return false;
|
||||
}
|
||||
const requestTurnId = value.turnId;
|
||||
return requestTurnId === null || requestTurnId === undefined || requestTurnId === turnId;
|
||||
}
|
||||
|
||||
/** Returns true for app-server error notifications that will retry. */
|
||||
export function isRetryableErrorNotification(value: JsonValue | undefined): boolean {
|
||||
return isJsonObject(value) && value.willRetry === true;
|
||||
if (!isJsonObject(value)) {
|
||||
return false;
|
||||
}
|
||||
return readBoolean(value, "willRetry") === true || readBoolean(value, "will_retry") === true;
|
||||
}
|
||||
|
||||
/** Returns true for terminal app-server thread status strings. */
|
||||
@@ -342,6 +419,10 @@ function readString(record: JsonObject, key: string): string | undefined {
|
||||
return typeof value === "string" ? value : undefined;
|
||||
}
|
||||
|
||||
function readBoolean(record: JsonObject, key: string): boolean | undefined {
|
||||
return asBoolean(record[key]);
|
||||
}
|
||||
|
||||
/** Reads a typed Codex item from notification params when id/type are present. */
|
||||
export function readCodexNotificationItem(
|
||||
params: JsonValue | undefined,
|
||||
|
||||
@@ -9,16 +9,13 @@ import type {
|
||||
} from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { startCodexAttemptThread } from "./attempt-startup.js";
|
||||
import { defaultLeasedCodexAppServerClientFactory } from "./client-factory.js";
|
||||
import { CodexAppServerClient } from "./client.js";
|
||||
import { type CodexPluginConfig, resolveCodexAppServerRuntimeOptions } from "./config.js";
|
||||
import { threadStartResult } from "./run-attempt-test-harness.js";
|
||||
import {
|
||||
resetCodexTestBindingStore,
|
||||
testCodexAppServerBindingStore,
|
||||
} from "./session-binding.test-helpers.js";
|
||||
import {
|
||||
leaseSharedCodexAppServerClient,
|
||||
resetSharedCodexAppServerClientForTests,
|
||||
clearSharedCodexAppServerClient,
|
||||
getLeasedSharedCodexAppServerClient,
|
||||
releaseLeasedSharedCodexAppServerClient,
|
||||
} from "./shared-client.js";
|
||||
import { createClientHarness, createCodexTestModel } from "./test-support.js";
|
||||
|
||||
@@ -88,10 +85,12 @@ function startThreadWithHarness(
|
||||
signal = new AbortController().signal,
|
||||
overrides?: {
|
||||
pluginConfig?: CodexPluginConfig;
|
||||
attemptClientFactory?: (
|
||||
harness: ClientHarness,
|
||||
) => Parameters<typeof startCodexAttemptThread>[0]["attemptClientFactory"];
|
||||
harness?: ClientHarness;
|
||||
paths?: AttemptPaths;
|
||||
skipStartSpy?: boolean;
|
||||
onThreadReserved?: Parameters<typeof startCodexAttemptThread>[0]["onThreadReserved"];
|
||||
},
|
||||
) {
|
||||
const harness = overrides?.harness ?? createClientHarness();
|
||||
@@ -102,7 +101,8 @@ function startThreadWithHarness(
|
||||
const effectivePluginConfig = overrides?.pluginConfig ?? pluginConfig;
|
||||
|
||||
const run = startCodexAttemptThread({
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
attemptClientFactory:
|
||||
overrides?.attemptClientFactory?.(harness) ?? defaultLeasedCodexAppServerClientFactory,
|
||||
appServer: resolveCodexAppServerRuntimeOptions({ pluginConfig: effectivePluginConfig }),
|
||||
pluginConfig: effectivePluginConfig,
|
||||
computerUseConfig: effectivePluginConfig.computerUse ?? { enabled: false },
|
||||
@@ -123,11 +123,10 @@ function startThreadWithHarness(
|
||||
sandboxExecServerEnabled: false,
|
||||
sandbox: null,
|
||||
contextEngineProjection: undefined,
|
||||
startupTokenGuard: {},
|
||||
startupTimeoutMs,
|
||||
signal,
|
||||
onStartupTimeout: vi.fn(),
|
||||
onThreadReserved: overrides?.onThreadReserved,
|
||||
spawnedBy: undefined,
|
||||
});
|
||||
|
||||
return { harness, run };
|
||||
@@ -169,13 +168,12 @@ describe("startCodexAttemptThread", () => {
|
||||
vi.useRealTimers();
|
||||
vi.stubEnv("CODEX_API_KEY", "");
|
||||
vi.stubEnv("OPENAI_API_KEY", "");
|
||||
resetCodexTestBindingStore();
|
||||
resetSharedCodexAppServerClientForTests();
|
||||
clearSharedCodexAppServerClient();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
vi.useRealTimers();
|
||||
resetSharedCodexAppServerClientForTests();
|
||||
clearSharedCodexAppServerClient();
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllEnvs();
|
||||
for (const root of tempRoots) {
|
||||
@@ -184,7 +182,7 @@ describe("startCodexAttemptThread", () => {
|
||||
tempRoots.clear();
|
||||
});
|
||||
|
||||
it("keeps the shared app-server reusable after a structured startup rejection", async () => {
|
||||
it("clears the shared app-server when top-level thread startup fails with an app error", async () => {
|
||||
const { harness, run } = startThreadWithHarness(5_000);
|
||||
await answerInitialize(harness);
|
||||
const threadStart = await waitForThreadStart(harness);
|
||||
@@ -194,57 +192,25 @@ describe("startCodexAttemptThread", () => {
|
||||
});
|
||||
|
||||
await expect(run).rejects.toThrow("Invalid bearer token");
|
||||
expect(harness.process.stdin.destroyed).toBe(false);
|
||||
});
|
||||
|
||||
it("retires the client when malformed startup cleanup cannot be confirmed", async () => {
|
||||
const { harness, run } = startThreadWithHarness(5_000);
|
||||
await answerInitialize(harness);
|
||||
const threadStart = await waitForThreadStart(harness);
|
||||
harness.send({ id: threadStart.id, result: { thread: { id: "thread-malformed" } } });
|
||||
const unsubscribe = await waitForRequest(harness, "thread/unsubscribe");
|
||||
harness.send({
|
||||
id: unsubscribe.id,
|
||||
error: { code: -32000, message: "unsubscribe failed" },
|
||||
});
|
||||
|
||||
await expect(run).rejects.toThrow("subscription could not be released");
|
||||
expect(harness.process.stdin.destroyed).toBe(true);
|
||||
});
|
||||
|
||||
it("retires the client when route cleanup cannot release the subscription", async () => {
|
||||
const { harness, run } = startThreadWithHarness(5_000, undefined, {
|
||||
onThreadReserved: () => {
|
||||
throw new Error("route integration failed");
|
||||
},
|
||||
});
|
||||
await answerInitialize(harness);
|
||||
const threadStart = await waitForThreadStart(harness);
|
||||
harness.send({ id: threadStart.id, result: threadStartResult("thread-route-failed") });
|
||||
const unsubscribe = await waitForRequest(harness, "thread/unsubscribe");
|
||||
harness.send({
|
||||
id: unsubscribe.id,
|
||||
error: { code: -32000, message: "unsubscribe failed" },
|
||||
});
|
||||
|
||||
await expect(run).rejects.toThrow("Codex startup subscription cleanup failed");
|
||||
expect(harness.process.stdin.destroyed).toBe(true);
|
||||
});
|
||||
|
||||
it("does not retire a peer-owned client after a structured startup rejection", async () => {
|
||||
it("retires a failed startup client after another active lease releases", async () => {
|
||||
const retained = createClientHarness();
|
||||
const startSpy = vi.spyOn(CodexAppServerClient, "start").mockReturnValue(retained.client);
|
||||
const replacement = createClientHarness();
|
||||
const startSpy = vi
|
||||
.spyOn(CodexAppServerClient, "start")
|
||||
.mockReturnValueOnce(retained.client)
|
||||
.mockReturnValueOnce(replacement.client);
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig });
|
||||
const paths = createAttemptPaths();
|
||||
|
||||
const retainedLeasePromise = leaseSharedCodexAppServerClient({
|
||||
const retainedLease = getLeasedSharedCodexAppServerClient({
|
||||
startOptions: appServer.start,
|
||||
agentDir: paths.agentDir,
|
||||
preparedAuth: {},
|
||||
});
|
||||
await answerInitialize(retained);
|
||||
const retainedLease = await retainedLeasePromise;
|
||||
expect(retainedLease.client).toBe(retained.client);
|
||||
await expect(retainedLease).resolves.toBe(retained.client);
|
||||
|
||||
const { run } = startThreadWithHarness(5_000, new AbortController().signal, {
|
||||
harness: retained,
|
||||
@@ -260,16 +226,17 @@ describe("startCodexAttemptThread", () => {
|
||||
await expect(run).rejects.toThrow("Invalid bearer token");
|
||||
expect(retained.process.stdin.destroyed).toBe(false);
|
||||
|
||||
retainedLease.release();
|
||||
const nextLeasePromise = leaseSharedCodexAppServerClient({
|
||||
expect(releaseLeasedSharedCodexAppServerClient(retained.client)).toBe(true);
|
||||
await vi.waitFor(() => expect(retained.process.stdin.destroyed).toBe(true));
|
||||
|
||||
const replacementLease = getLeasedSharedCodexAppServerClient({
|
||||
startOptions: appServer.start,
|
||||
agentDir: paths.agentDir,
|
||||
preparedAuth: {},
|
||||
});
|
||||
const nextLease = await nextLeasePromise;
|
||||
expect(nextLease.client).toBe(retained.client);
|
||||
expect(startSpy).toHaveBeenCalledTimes(1);
|
||||
nextLease.release();
|
||||
await answerInitialize(replacement);
|
||||
await expect(replacementLease).resolves.toBe(replacement.client);
|
||||
expect(startSpy).toHaveBeenCalledTimes(2);
|
||||
expect(releaseLeasedSharedCodexAppServerClient(replacement.client)).toBe(true);
|
||||
});
|
||||
|
||||
it("clears the shared app-server when startup abandons an in-flight thread request", async () => {
|
||||
@@ -291,20 +258,18 @@ describe("startCodexAttemptThread", () => {
|
||||
expect(harness.stdinDestroyed).toBe(true);
|
||||
});
|
||||
|
||||
it("retires abandoned thread startup even when another lease shares the client", async () => {
|
||||
it("aborts abandoned thread startup when another lease keeps the shared app-server alive", async () => {
|
||||
const retained = createClientHarness();
|
||||
vi.spyOn(CodexAppServerClient, "start").mockReturnValue(retained.client);
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig });
|
||||
const paths = createAttemptPaths();
|
||||
|
||||
const retainedLeasePromise = leaseSharedCodexAppServerClient({
|
||||
const retainedLease = getLeasedSharedCodexAppServerClient({
|
||||
startOptions: appServer.start,
|
||||
agentDir: paths.agentDir,
|
||||
preparedAuth: {},
|
||||
});
|
||||
await answerInitialize(retained);
|
||||
const retainedLease = await retainedLeasePromise;
|
||||
expect(retainedLease.client).toBe(retained.client);
|
||||
await expect(retainedLease).resolves.toBe(retained.client);
|
||||
|
||||
const { run } = startThreadWithHarness(100, new AbortController().signal, {
|
||||
harness: retained,
|
||||
@@ -315,9 +280,11 @@ describe("startCodexAttemptThread", () => {
|
||||
const threadStart = await waitForThreadStart(retained);
|
||||
|
||||
await rejected;
|
||||
expect(threadStart.id).toBeDefined();
|
||||
expect(retained.process.stdin.destroyed).toBe(true);
|
||||
retainedLease.release();
|
||||
expect(retained.process.stdin.destroyed).toBe(false);
|
||||
|
||||
retained.send({ id: threadStart.id, result: { threadId: "late-thread" } });
|
||||
expect(releaseLeasedSharedCodexAppServerClient(retained.client)).toBe(true);
|
||||
await vi.waitFor(() => expect(retained.process.stdin.destroyed).toBe(true));
|
||||
});
|
||||
|
||||
it("closes the shared app-server when startup times out during initialize", async () => {
|
||||
@@ -342,37 +309,45 @@ describe("startCodexAttemptThread", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("releases a late startup lease without retiring a peer-owned initializing client", async () => {
|
||||
const harness = createClientHarness();
|
||||
const startSpy = vi.spyOn(CodexAppServerClient, "start").mockReturnValue(harness.client);
|
||||
const paths = createAttemptPaths();
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig });
|
||||
const peerPromise = leaseSharedCodexAppServerClient({
|
||||
startOptions: appServer.start,
|
||||
agentDir: paths.agentDir,
|
||||
preparedAuth: {},
|
||||
it("closes a startup client that arrives after startup timeout", async () => {
|
||||
let observedFactoryOptions:
|
||||
| {
|
||||
onStartedClient?: (client: CodexAppServerClient) => void;
|
||||
abandonSignal?: AbortSignal;
|
||||
}
|
||||
| undefined;
|
||||
let resolveFactoryDone: () => void = () => undefined;
|
||||
const factoryDone = new Promise<void>((resolve) => {
|
||||
resolveFactoryDone = resolve;
|
||||
});
|
||||
const { run } = startThreadWithHarness(100, new AbortController().signal, {
|
||||
harness,
|
||||
paths,
|
||||
skipStartSpy: true,
|
||||
const { harness, run } = startThreadWithHarness(100, new AbortController().signal, {
|
||||
attemptClientFactory:
|
||||
(factoryHarness) => async (_startOptions, _authProfileId, _agentDir, _config, options) => {
|
||||
try {
|
||||
observedFactoryOptions = options;
|
||||
await new Promise<void>((resolve) => {
|
||||
setTimeout(resolve, 250);
|
||||
});
|
||||
options?.onStartedClient?.(factoryHarness.client);
|
||||
return factoryHarness.client;
|
||||
} finally {
|
||||
resolveFactoryDone();
|
||||
}
|
||||
},
|
||||
});
|
||||
const rejected = expect(run).rejects.toThrow("codex app-server startup timed out");
|
||||
|
||||
await expect(run).rejects.toThrow("codex app-server startup timed out");
|
||||
expect(harness.stdinDestroyed).toBe(false);
|
||||
await answerInitialize(harness);
|
||||
const peer = await peerPromise;
|
||||
expect(peer.client).toBe(harness.client);
|
||||
await new Promise<void>((resolve) => {
|
||||
setImmediate(resolve);
|
||||
await rejected;
|
||||
await factoryDone;
|
||||
await vi.waitFor(() => expect(harness.stdinDestroyed).toBe(true), {
|
||||
interval: 1,
|
||||
timeout: 2_000,
|
||||
});
|
||||
|
||||
expect(startSpy).toHaveBeenCalledTimes(1);
|
||||
expect(
|
||||
readHarnessMessages(harness.writes).some((write) => write.method === "thread/start"),
|
||||
).toBe(false);
|
||||
await peer.abandon();
|
||||
expect(harness.stdinDestroyed).toBe(true);
|
||||
expect(observedFactoryOptions?.onStartedClient).toBeTypeOf("function");
|
||||
expect(observedFactoryOptions?.abandonSignal?.aborted).toBe(true);
|
||||
});
|
||||
|
||||
it("clears the shared app-server when cancellation abandons an in-flight thread request", async () => {
|
||||
|
||||
@@ -11,15 +11,9 @@ import {
|
||||
type resolveSandboxContext,
|
||||
} from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import { defaultCodexAppInventoryCache } from "./app-inventory-cache.js";
|
||||
import {
|
||||
CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
CodexAppServerUnsafeSubscriptionError,
|
||||
isCodexAppServerUnsafeSubscriptionError,
|
||||
unsubscribeCodexThreadBestEffort,
|
||||
} from "./attempt-client-cleanup.js";
|
||||
import { buildCodexPluginThreadConfigEligibilityLogData } from "./attempt-diagnostics.js";
|
||||
import { withCodexStartupTimeout } from "./attempt-timeouts.js";
|
||||
import { ensureCodexAppServerClientRuntime } from "./client-runtime.js";
|
||||
import type { CodexAppServerClientFactory } from "./client-factory.js";
|
||||
import { isCodexAppServerConnectionClosedError, type CodexAppServerClient } from "./client.js";
|
||||
import { ensureCodexComputerUse } from "./computer-use.js";
|
||||
import {
|
||||
@@ -54,23 +48,17 @@ import {
|
||||
releaseCodexSandboxExecServerEnvironment,
|
||||
type CodexSandboxExecEnvironment,
|
||||
} from "./sandbox-exec-server.js";
|
||||
import type { CodexAppServerBindingStore } from "./session-binding.js";
|
||||
import {
|
||||
leaseSharedCodexAppServerClient,
|
||||
type CodexAppServerClientLease,
|
||||
type CodexAppServerClientLeaseFactory,
|
||||
clearSharedCodexAppServerClientIfCurrentAndUnclaimed,
|
||||
clearSharedCodexAppServerClientIfCurrent,
|
||||
releaseLeasedSharedCodexAppServerClient,
|
||||
retireSharedCodexAppServerClientIfCurrent,
|
||||
} from "./shared-client.js";
|
||||
import type { CodexAppServerStartupTokenGuard } from "./startup-binding.js";
|
||||
import {
|
||||
startOrResumeThread,
|
||||
type CodexAppServerThreadLifecycleBinding,
|
||||
type CodexContextEngineThreadBootstrapProjection,
|
||||
} from "./thread-lifecycle.js";
|
||||
import {
|
||||
getCodexAppServerTurnRouter,
|
||||
type CodexAppServerTurnRouter,
|
||||
type CodexThreadRouteReservation,
|
||||
} from "./turn-router.js";
|
||||
|
||||
const CODEX_APP_SERVER_STARTUP_CONNECTION_CLOSE_MAX_ATTEMPTS = 3;
|
||||
|
||||
@@ -78,15 +66,14 @@ type CodexSandboxContext = Awaited<ReturnType<typeof resolveSandboxContext>>;
|
||||
|
||||
/** Resources and bindings returned after a Codex attempt thread starts. */
|
||||
export type StartCodexAttemptThreadResult = {
|
||||
turnRouter: CodexAppServerTurnRouter;
|
||||
turnRoute: CodexThreadRouteReservation;
|
||||
client: CodexAppServerClient;
|
||||
thread: CodexAppServerThreadLifecycleBinding;
|
||||
pluginAppServer: CodexAppServerRuntimeOptions;
|
||||
sandboxEnvironment: CodexSandboxExecEnvironment | undefined;
|
||||
environmentSelection: CodexTurnEnvironmentParams[] | undefined;
|
||||
executionCwd: string;
|
||||
sandboxPolicy: CodexSandboxPolicy | undefined;
|
||||
clientLease: CodexAppServerClientLease;
|
||||
mcpElicitationDelegationRequired: boolean;
|
||||
releaseSharedClientLease: () => void;
|
||||
restartContextEngineCodexThread: () => Promise<CodexAppServerThreadLifecycleBinding>;
|
||||
};
|
||||
|
||||
@@ -95,8 +82,7 @@ export type StartCodexAttemptThreadResult = {
|
||||
* run loop must later release.
|
||||
*/
|
||||
export async function startCodexAttemptThread(params: {
|
||||
bindingStore: CodexAppServerBindingStore;
|
||||
clientLeaseFactory?: CodexAppServerClientLeaseFactory;
|
||||
attemptClientFactory: CodexAppServerClientFactory;
|
||||
appServer: CodexAppServerRuntimeOptions;
|
||||
pluginConfig: CodexPluginConfig;
|
||||
computerUseConfig: CodexComputerUseConfig;
|
||||
@@ -119,26 +105,18 @@ export async function startCodexAttemptThread(params: {
|
||||
sandboxExecServerEnabled: boolean;
|
||||
sandbox: CodexSandboxContext;
|
||||
contextEngineProjection: CodexContextEngineThreadBootstrapProjection | undefined;
|
||||
expectedResumeThreadId?: string;
|
||||
startupTokenGuard: CodexAppServerStartupTokenGuard;
|
||||
startupTimeoutMs: number;
|
||||
signal: AbortSignal;
|
||||
onStartupTimeout: () => void | Promise<void>;
|
||||
onThreadReserved?: (client: CodexAppServerClient, threadId: string) => () => void;
|
||||
spawnedBy: EmbeddedRunAttemptParams["spawnedBy"];
|
||||
}): Promise<StartCodexAttemptThreadResult> {
|
||||
let mcpElicitationDelegationRequired = false;
|
||||
let sharedClientLease: CodexAppServerClientLease | undefined;
|
||||
let pluginAppServer = params.appServer;
|
||||
let releaseSharedClientLease: (() => void) | undefined;
|
||||
let startupClientForAbandonedRequestCleanup: CodexAppServerClient | undefined;
|
||||
let releaseStartupResourcesOnTimeout: (() => Promise<void>) | undefined;
|
||||
let startupAbandoned = false;
|
||||
const startupAbandonController = new AbortController();
|
||||
const abandonStartupAcquire = () => startupAbandonController.abort();
|
||||
const abandonStartupClient = async () => {
|
||||
const lease = sharedClientLease;
|
||||
sharedClientLease = undefined;
|
||||
if (lease) {
|
||||
await lease.abandon();
|
||||
}
|
||||
};
|
||||
params.signal.addEventListener("abort", abandonStartupAcquire, { once: true });
|
||||
try {
|
||||
const startupResult = await withCodexStartupTimeout({
|
||||
@@ -149,7 +127,10 @@ export async function startCodexAttemptThread(params: {
|
||||
startupAbandonController.abort();
|
||||
await params.onStartupTimeout();
|
||||
await releaseStartupResourcesOnTimeout?.();
|
||||
await abandonStartupClient();
|
||||
releaseSharedClientLease?.();
|
||||
releaseSharedClientLease = undefined;
|
||||
await closeAbandonedStartupClient(startupClientForAbandonedRequestCleanup);
|
||||
startupClientForAbandonedRequestCleanup = undefined;
|
||||
},
|
||||
operation: async () => {
|
||||
const threadConfig = mergeCodexThreadConfigs(
|
||||
@@ -180,9 +161,8 @@ export async function startCodexAttemptThread(params: {
|
||||
const resolvedPluginPolicy = pluginThreadConfigRequired
|
||||
? resolveCodexPluginsPolicy(pluginThreadConfigPluginConfig)
|
||||
: undefined;
|
||||
const computerUseMcpElicitationDelegationRequired =
|
||||
params.computerUseConfig.enabled === true;
|
||||
mcpElicitationDelegationRequired =
|
||||
const computerUseMcpElicitationDelegationRequired = params.computerUseConfig.enabled;
|
||||
const mcpElicitationDelegationRequired =
|
||||
resolvedPluginPolicy?.enabled === true || computerUseMcpElicitationDelegationRequired;
|
||||
const enabledPluginConfigKeys = resolvedPluginPolicy
|
||||
? resolvedPluginPolicy.pluginPolicies
|
||||
@@ -204,48 +184,55 @@ export async function startCodexAttemptThread(params: {
|
||||
appServer: params.appServer,
|
||||
}),
|
||||
);
|
||||
const pluginAppServer = mcpElicitationDelegationRequired
|
||||
pluginAppServer = mcpElicitationDelegationRequired
|
||||
? {
|
||||
...params.appServer,
|
||||
approvalPolicy: withMcpElicitationsApprovalPolicy(params.appServer.approvalPolicy),
|
||||
}
|
||||
: params.appServer;
|
||||
|
||||
let attemptedClientAbandoned = false;
|
||||
let attemptedClient: CodexAppServerClient | undefined;
|
||||
const startupAttempt = async () => {
|
||||
let startupClientLease: CodexAppServerClientLease | undefined;
|
||||
let clientWorkStarted = false;
|
||||
attemptedClientAbandoned = false;
|
||||
let startupClientLease: (() => void) | undefined;
|
||||
let startupClient: CodexAppServerClient | undefined;
|
||||
let startupAttemptError: unknown;
|
||||
let startupAttemptSucceeded = false;
|
||||
try {
|
||||
startupClientLease = await (
|
||||
params.clientLeaseFactory ?? leaseSharedCodexAppServerClient
|
||||
)({
|
||||
startOptions: params.appServer.start,
|
||||
authProfileId: params.startupAuthProfileId,
|
||||
agentDir: params.agentDir,
|
||||
config: params.config,
|
||||
preparedAuth: {
|
||||
profileId: params.startupAuthProfileId,
|
||||
cacheKey: params.startupAuthAccountCacheKey ?? params.startupEnvApiKeyCacheKey,
|
||||
startupClient = await params.attemptClientFactory(
|
||||
params.appServer.start,
|
||||
params.startupAuthProfileId,
|
||||
params.agentDir,
|
||||
params.config,
|
||||
{
|
||||
onStartedClient: (client) => {
|
||||
// Timeout cleanup may fire before the client factory resolves;
|
||||
// close any late-arriving client instead of leaking a lease.
|
||||
startupClientForAbandonedRequestCleanup = client;
|
||||
if (startupAbandoned || startupAbandonController.signal.aborted) {
|
||||
void closeAbandonedStartupClient(client);
|
||||
}
|
||||
},
|
||||
abandonSignal: startupAbandonController.signal,
|
||||
},
|
||||
abandonSignal: startupAbandonController.signal,
|
||||
});
|
||||
const activeStartupLease = startupClientLease;
|
||||
const activeStartupClient = activeStartupLease.client;
|
||||
sharedClientLease = startupClientLease;
|
||||
);
|
||||
const activeStartupClient = startupClient;
|
||||
let startupClientLeaseReleased = false;
|
||||
startupClientLease = () => {
|
||||
if (startupClientLeaseReleased) {
|
||||
return;
|
||||
}
|
||||
startupClientLeaseReleased = true;
|
||||
releaseLeasedSharedCodexAppServerClient(activeStartupClient);
|
||||
};
|
||||
releaseSharedClientLease = startupClientLease;
|
||||
attemptedClient = activeStartupClient;
|
||||
startupClientForAbandonedRequestCleanup = activeStartupClient;
|
||||
if (startupAbandoned) {
|
||||
throw new Error("codex app-server startup timed out");
|
||||
}
|
||||
if (startupAbandonController.signal.aborted) {
|
||||
throw new Error("codex app-server startup aborted");
|
||||
}
|
||||
clientWorkStarted = true;
|
||||
ensureCodexAppServerClientRuntime(activeStartupClient, {
|
||||
agentDir: params.agentDir,
|
||||
authProfileId: params.startupAuthProfileId,
|
||||
config: params.config,
|
||||
});
|
||||
const turnRouter = getCodexAppServerTurnRouter(activeStartupClient);
|
||||
await ensureCodexComputerUse({
|
||||
client: activeStartupClient,
|
||||
pluginConfig: params.pluginConfig,
|
||||
@@ -277,6 +264,7 @@ export async function startCodexAttemptThread(params: {
|
||||
: undefined;
|
||||
startupSandboxEnvironmentAcquired = Boolean(startupSandboxEnvironment);
|
||||
if (startupAbandonController.signal.aborted) {
|
||||
await releaseStartupSandboxEnvironment();
|
||||
throw new Error("codex app-server startup aborted");
|
||||
}
|
||||
if (
|
||||
@@ -305,57 +293,9 @@ export async function startCodexAttemptThread(params: {
|
||||
const startupSandboxPolicy = startupSandboxEnvironment
|
||||
? resolveCodexExternalSandboxPolicyForOpenClawSandbox(params.sandbox)
|
||||
: undefined;
|
||||
let startupReservation:
|
||||
| { route: CodexThreadRouteReservation; release: () => void }
|
||||
| undefined;
|
||||
const reserveStartupThread = (threadId: string) => {
|
||||
if (startupReservation) {
|
||||
if (startupReservation.route.threadId !== threadId) {
|
||||
throw new Error(
|
||||
`codex app-server reserved ${startupReservation.route.threadId} but started ${threadId}`,
|
||||
);
|
||||
}
|
||||
return { release: startupReservation.release };
|
||||
}
|
||||
const route = turnRouter.reserveThread({
|
||||
threadId,
|
||||
releaseOn: params.signal,
|
||||
});
|
||||
let releaseIntegration: (() => void) | undefined;
|
||||
try {
|
||||
releaseIntegration = params.onThreadReserved?.(activeStartupClient, threadId);
|
||||
} catch (error) {
|
||||
route.release();
|
||||
throw error;
|
||||
}
|
||||
let released = false;
|
||||
const release = () => {
|
||||
if (released) {
|
||||
return;
|
||||
}
|
||||
released = true;
|
||||
if (startupReservation?.route === route) {
|
||||
startupReservation = undefined;
|
||||
}
|
||||
route.release();
|
||||
releaseIntegration?.();
|
||||
};
|
||||
startupReservation = { route, release };
|
||||
return { release };
|
||||
};
|
||||
const releaseStartupResources = async () => {
|
||||
startupReservation?.release();
|
||||
await releaseStartupSandboxEnvironment();
|
||||
};
|
||||
releaseStartupResourcesOnTimeout = releaseStartupResources;
|
||||
const buildThreadLifecycleParams = (
|
||||
signal: AbortSignal,
|
||||
options: { freshStartOnly?: boolean } = {},
|
||||
) =>
|
||||
const buildThreadLifecycleParams = (signal: AbortSignal) =>
|
||||
({
|
||||
client: activeStartupClient,
|
||||
abandonClient: activeStartupLease.abandon,
|
||||
bindingStore: params.bindingStore,
|
||||
params: params.buildAttemptParams(),
|
||||
agentId: params.sessionAgentId,
|
||||
cwd: startupExecutionCwd,
|
||||
@@ -373,13 +313,7 @@ export async function startCodexAttemptThread(params: {
|
||||
mcpServersFingerprintEvaluated: params.bundleMcpThreadConfig.evaluated,
|
||||
environmentSelection: startupEnvironmentSelection,
|
||||
contextEngineProjection: params.contextEngineProjection,
|
||||
freshStartOnly: options.freshStartOnly,
|
||||
expectedResumeThreadId: options.freshStartOnly
|
||||
? undefined
|
||||
: params.expectedResumeThreadId,
|
||||
signal,
|
||||
reserveResumeThread: options.freshStartOnly ? undefined : reserveStartupThread,
|
||||
startupTokenGuard: params.startupTokenGuard,
|
||||
pluginThreadConfig: pluginThreadConfigRequired
|
||||
? {
|
||||
enabled: true,
|
||||
@@ -403,65 +337,57 @@ export async function startCodexAttemptThread(params: {
|
||||
const startupThread = await startOrResumeThread(
|
||||
buildThreadLifecycleParams(startupAbandonController.signal),
|
||||
);
|
||||
try {
|
||||
reserveStartupThread(startupThread.threadId);
|
||||
} catch (error) {
|
||||
const unsubscribed = await unsubscribeCodexThreadBestEffort(activeStartupClient, {
|
||||
threadId: startupThread.threadId,
|
||||
timeoutMs: CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
});
|
||||
if (!unsubscribed) {
|
||||
throw new CodexAppServerUnsafeSubscriptionError(
|
||||
"Codex startup subscription cleanup failed",
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (startupAbandonController.signal.aborted) {
|
||||
await releaseStartupSandboxEnvironment();
|
||||
throw new Error("codex app-server startup aborted");
|
||||
}
|
||||
if (!startupReservation) {
|
||||
throw new Error("codex app-server startup did not reserve its thread route");
|
||||
}
|
||||
startupSandboxEnvironmentAcquired = false;
|
||||
startupAttemptSucceeded = true;
|
||||
return {
|
||||
turnRouter,
|
||||
turnRoute: startupReservation.route,
|
||||
client: activeStartupClient,
|
||||
thread: startupThread,
|
||||
sandboxEnvironment: startupSandboxEnvironment,
|
||||
environmentSelection: startupEnvironmentSelection,
|
||||
executionCwd: startupExecutionCwd,
|
||||
sandboxPolicy: startupSandboxPolicy,
|
||||
restartContextEngineCodexThread: () =>
|
||||
startOrResumeThread(
|
||||
buildThreadLifecycleParams(params.signal, { freshStartOnly: true }),
|
||||
),
|
||||
startOrResumeThread(buildThreadLifecycleParams(params.signal)),
|
||||
};
|
||||
} catch (error) {
|
||||
await releaseStartupResources();
|
||||
await releaseStartupSandboxEnvironment();
|
||||
throw error;
|
||||
} finally {
|
||||
if (releaseStartupResourcesOnTimeout === releaseStartupResources) {
|
||||
if (releaseStartupResourcesOnTimeout === releaseStartupSandboxEnvironment) {
|
||||
releaseStartupResourcesOnTimeout = undefined;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (sharedClientLease === startupClientLease) {
|
||||
sharedClientLease = undefined;
|
||||
}
|
||||
const shouldAbandonStartupClient =
|
||||
clientWorkStarted &&
|
||||
(startupAbandoned ||
|
||||
params.signal.aborted ||
|
||||
isIndeterminateCodexStartupFailure(error));
|
||||
if (shouldAbandonStartupClient) {
|
||||
attemptedClientAbandoned = true;
|
||||
await startupClientLease?.abandon();
|
||||
} else {
|
||||
startupClientLease?.release();
|
||||
}
|
||||
startupAttemptError = error;
|
||||
throw error;
|
||||
} finally {
|
||||
if (!startupAttemptSucceeded) {
|
||||
if (releaseSharedClientLease === startupClientLease) {
|
||||
releaseSharedClientLease = undefined;
|
||||
}
|
||||
startupClientLease?.();
|
||||
if (startupAbandoned || params.signal.aborted) {
|
||||
if (startupClientForAbandonedRequestCleanup === startupClient) {
|
||||
startupClientForAbandonedRequestCleanup = undefined;
|
||||
}
|
||||
await closeAbandonedStartupClient(startupClient);
|
||||
} else if (
|
||||
shouldClearSharedClientAfterStartupRace(startupAttemptError) ||
|
||||
shouldClearSharedClientAfterStartupFailure({
|
||||
error: startupAttemptError,
|
||||
spawnedBy: params.spawnedBy,
|
||||
})
|
||||
) {
|
||||
if (startupClientForAbandonedRequestCleanup === startupClient) {
|
||||
startupClientForAbandonedRequestCleanup = undefined;
|
||||
}
|
||||
await evictFailedStartupClient(startupClient);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -476,13 +402,18 @@ export async function startCodexAttemptThread(params: {
|
||||
if (params.signal.aborted || !isCodexAppServerConnectionClosedError(error)) {
|
||||
throw error;
|
||||
}
|
||||
const failedClient = attemptedClient;
|
||||
const clearedSharedClient = clearSharedCodexAppServerClientIfCurrent(failedClient);
|
||||
if (startupClientForAbandonedRequestCleanup === failedClient) {
|
||||
startupClientForAbandonedRequestCleanup = undefined;
|
||||
}
|
||||
if (attempt >= CODEX_APP_SERVER_STARTUP_CONNECTION_CLOSE_MAX_ATTEMPTS) {
|
||||
embeddedAgentLog.warn(
|
||||
"codex app-server connection closed during startup; retries exhausted",
|
||||
{
|
||||
attempt,
|
||||
maxAttempts: CODEX_APP_SERVER_STARTUP_CONNECTION_CLOSE_MAX_ATTEMPTS,
|
||||
abandonedSharedClient: attemptedClientAbandoned,
|
||||
clearedSharedClient,
|
||||
error: formatErrorMessage(error),
|
||||
},
|
||||
);
|
||||
@@ -494,7 +425,7 @@ export async function startCodexAttemptThread(params: {
|
||||
attempt,
|
||||
nextAttempt: attempt + 1,
|
||||
maxAttempts: CODEX_APP_SERVER_STARTUP_CONNECTION_CLOSE_MAX_ATTEMPTS,
|
||||
abandonedSharedClient: attemptedClientAbandoned,
|
||||
clearedSharedClient,
|
||||
error: formatErrorMessage(error),
|
||||
},
|
||||
);
|
||||
@@ -503,21 +434,32 @@ export async function startCodexAttemptThread(params: {
|
||||
throw new Error("codex app-server startup retry loop exited unexpectedly");
|
||||
},
|
||||
});
|
||||
const completedSharedClientLease = sharedClientLease;
|
||||
if (!completedSharedClientLease) {
|
||||
startupClientForAbandonedRequestCleanup = undefined;
|
||||
if (!releaseSharedClientLease) {
|
||||
throw new Error("codex app-server startup succeeded without a shared client lease");
|
||||
}
|
||||
sharedClientLease = undefined;
|
||||
return {
|
||||
...startupResult,
|
||||
mcpElicitationDelegationRequired,
|
||||
clientLease: completedSharedClientLease,
|
||||
pluginAppServer,
|
||||
releaseSharedClientLease,
|
||||
};
|
||||
} catch (error) {
|
||||
const shouldAbandonStartupClient =
|
||||
params.signal.aborted || isIndeterminateCodexStartupFailure(error);
|
||||
if (shouldAbandonStartupClient) {
|
||||
await abandonStartupClient();
|
||||
if (params.signal.aborted || shouldClearSharedClientAfterStartupAbandon(error)) {
|
||||
releaseSharedClientLease?.();
|
||||
releaseSharedClientLease = undefined;
|
||||
await closeAbandonedStartupClient(startupClientForAbandonedRequestCleanup);
|
||||
startupClientForAbandonedRequestCleanup = undefined;
|
||||
} else if (
|
||||
shouldClearSharedClientAfterStartupRace(error) ||
|
||||
shouldClearSharedClientAfterStartupFailure({
|
||||
error,
|
||||
spawnedBy: params.spawnedBy,
|
||||
})
|
||||
) {
|
||||
releaseSharedClientLease?.();
|
||||
releaseSharedClientLease = undefined;
|
||||
await evictFailedStartupClient(startupClientForAbandonedRequestCleanup);
|
||||
startupClientForAbandonedRequestCleanup = undefined;
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
@@ -525,13 +467,104 @@ export async function startCodexAttemptThread(params: {
|
||||
}
|
||||
}
|
||||
|
||||
function isIndeterminateCodexStartupFailure(error: unknown): boolean {
|
||||
async function closeAbandonedStartupClient(
|
||||
client: CodexAppServerClient | undefined,
|
||||
): Promise<void> {
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
const unclaimedSharedClient = clearSharedCodexAppServerClientIfCurrentAndUnclaimed(client);
|
||||
if (unclaimedSharedClient.closed) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
return;
|
||||
}
|
||||
if (unclaimedSharedClient.found) {
|
||||
const retired = retireSharedCodexAppServerClientIfCurrent(client);
|
||||
if (retired?.closed) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const retiredSharedClient = retireSharedCodexAppServerClientIfCurrent(client);
|
||||
if (retiredSharedClient) {
|
||||
if (retiredSharedClient.closed) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (clearSharedCodexAppServerClientIfCurrent(client)) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
return;
|
||||
}
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
}
|
||||
|
||||
async function closeClientAndWaitIfAvailable(client: CodexAppServerClient): Promise<void> {
|
||||
const closeable = client as {
|
||||
close?: CodexAppServerClient["close"];
|
||||
closeAndWait?: CodexAppServerClient["closeAndWait"];
|
||||
};
|
||||
if (typeof closeable.closeAndWait === "function") {
|
||||
await closeable.closeAndWait();
|
||||
return;
|
||||
}
|
||||
closeable.close?.();
|
||||
}
|
||||
|
||||
async function evictFailedStartupClient(client: CodexAppServerClient | undefined): Promise<void> {
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
const unclaimedSharedClient = clearSharedCodexAppServerClientIfCurrentAndUnclaimed(client);
|
||||
if (unclaimedSharedClient.closed) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
return;
|
||||
}
|
||||
if (unclaimedSharedClient.found) {
|
||||
const retired = retireSharedCodexAppServerClientIfCurrent(client);
|
||||
if (retired?.closed) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const retiredSharedClient = retireSharedCodexAppServerClientIfCurrent(client);
|
||||
if (retiredSharedClient) {
|
||||
if (retiredSharedClient.closed) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (clearSharedCodexAppServerClientIfCurrent(client)) {
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
return;
|
||||
}
|
||||
await closeClientAndWaitIfAvailable(client);
|
||||
}
|
||||
|
||||
function shouldClearSharedClientAfterStartupAbandon(error: unknown): boolean {
|
||||
return (
|
||||
isCodexAppServerUnsafeSubscriptionError(error) ||
|
||||
isCodexAppServerConnectionClosedError(error) ||
|
||||
(error instanceof Error &&
|
||||
(error.message.endsWith(" timed out") ||
|
||||
error.message.endsWith(" aborted") ||
|
||||
error.message.includes("write EPIPE")))
|
||||
error instanceof Error &&
|
||||
(error.message === "codex app-server startup timed out" ||
|
||||
error.message === "codex app-server startup aborted")
|
||||
);
|
||||
}
|
||||
|
||||
function shouldClearSharedClientAfterStartupRace(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof Error &&
|
||||
(shouldClearSharedClientAfterStartupAbandon(error) || error.message.endsWith(" timed out"))
|
||||
);
|
||||
}
|
||||
|
||||
function shouldClearSharedClientAfterStartupFailure(params: {
|
||||
error: unknown;
|
||||
spawnedBy: EmbeddedRunAttemptParams["spawnedBy"];
|
||||
}): boolean {
|
||||
if (!(params.error instanceof Error)) {
|
||||
return !params.spawnedBy;
|
||||
}
|
||||
if (params.error.message.includes("write EPIPE")) {
|
||||
return true;
|
||||
}
|
||||
return !params.spawnedBy;
|
||||
}
|
||||
|
||||
@@ -159,39 +159,6 @@ describe("Codex app-server attempt timeouts", () => {
|
||||
expect(events).toEqual(["cleanup-start", "cleanup-done"]);
|
||||
});
|
||||
|
||||
it("keeps the timeout result when startup resolves during timeout cleanup", async () => {
|
||||
vi.useFakeTimers();
|
||||
const events: string[] = [];
|
||||
let resolveOperation!: (value: string) => void;
|
||||
let finishCleanup!: () => void;
|
||||
const run = withCodexStartupTimeout({
|
||||
timeoutMs: 10,
|
||||
signal: new AbortController().signal,
|
||||
onTimeout: async () => {
|
||||
events.push("cleanup-start");
|
||||
await new Promise<void>((resolve) => {
|
||||
finishCleanup = resolve;
|
||||
});
|
||||
events.push("cleanup-done");
|
||||
},
|
||||
operation: () =>
|
||||
new Promise<string>((resolve) => {
|
||||
resolveOperation = resolve;
|
||||
}),
|
||||
});
|
||||
const rejected = expect(run).rejects.toThrow("codex app-server startup timed out");
|
||||
|
||||
await vi.advanceTimersByTimeAsync(10);
|
||||
expect(events).toEqual(["cleanup-start"]);
|
||||
resolveOperation("late-ready");
|
||||
await Promise.resolve();
|
||||
expect(events).toEqual(["cleanup-start"]);
|
||||
finishCleanup();
|
||||
|
||||
await rejected;
|
||||
expect(events).toEqual(["cleanup-start", "cleanup-done"]);
|
||||
});
|
||||
|
||||
it("rejects startup timeout when aborted before completion", async () => {
|
||||
vi.useFakeTimers();
|
||||
const controller = new AbortController();
|
||||
|
||||
@@ -52,13 +52,13 @@ export async function withCodexStartupTimeout<T>(params: {
|
||||
};
|
||||
timeout = setTimeout(() => {
|
||||
timeoutError = new Error("codex app-server startup timed out");
|
||||
rejectOnce(timeoutError);
|
||||
timeoutCleanup = Promise.resolve()
|
||||
.then(() => params.onTimeout?.())
|
||||
.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
timeoutCleanup = Promise.resolve(params.onTimeout?.()).then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
void timeoutCleanup.finally(() => {
|
||||
rejectOnce(timeoutError!);
|
||||
});
|
||||
}, params.timeoutMs);
|
||||
const abortListener = () => rejectOnce(new Error("codex app-server startup aborted"));
|
||||
params.signal.addEventListener("abort", abortListener, { once: true });
|
||||
|
||||
@@ -29,7 +29,7 @@ describe("Codex app-server attempt turn watches", () => {
|
||||
const progress: string[] = [];
|
||||
const diagnostics: string[] = [];
|
||||
const controller = createCodexAttemptTurnWatchController({
|
||||
getThreadId: () => "thread-1",
|
||||
threadId: "thread-1",
|
||||
signal: abortController.signal,
|
||||
getTurnId: () => "turn-1",
|
||||
isCompleted: () => completed,
|
||||
|
||||
@@ -29,7 +29,7 @@ export type CodexAttemptTurnWatchController = ReturnType<
|
||||
* notifications and tool handoffs progress.
|
||||
*/
|
||||
export function createCodexAttemptTurnWatchController(params: {
|
||||
getThreadId: () => string;
|
||||
threadId: string;
|
||||
signal: AbortSignal;
|
||||
getTurnId: () => string | undefined;
|
||||
isCompleted: () => boolean;
|
||||
@@ -79,7 +79,6 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
const turnTerminalIdleTimeoutMs = resolveTimerTimeoutMs(params.turnTerminalIdleTimeoutMs, 1);
|
||||
const interruptTimeoutMs = resolveTimerTimeoutMs(params.interruptTimeoutMs, 1);
|
||||
const resolveWatchTimeoutMs = (timeoutMs: number) => resolveTimerTimeoutMs(timeoutMs, 1);
|
||||
const currentThreadId = () => params.getThreadId();
|
||||
|
||||
const clearCompletionIdleTimer = () => {
|
||||
if (completionIdleTimer) {
|
||||
@@ -228,7 +227,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
clearTerminalIdleTimer();
|
||||
const turnId = params.getTurnId();
|
||||
params.onRecordEvent("turn.assistant_completion_idle_release", {
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId,
|
||||
idleMs,
|
||||
timeoutMs: turnAssistantCompletionIdleTimeoutMs,
|
||||
@@ -237,7 +236,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
embeddedAgentLog.warn(
|
||||
"codex app-server turn released after completed assistant item without terminal event",
|
||||
{
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId,
|
||||
idleMs,
|
||||
timeoutMs: turnAssistantCompletionIdleTimeoutMs,
|
||||
@@ -246,7 +245,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
);
|
||||
if (turnId) {
|
||||
params.onInterruptTurn({
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId,
|
||||
timeoutMs: interruptTimeoutMs,
|
||||
});
|
||||
@@ -279,7 +278,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
params.onTimeout(timeout);
|
||||
params.onMarkTimedOut();
|
||||
params.onRecordEvent("turn.progress_idle_timeout", {
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId: params.getTurnId(),
|
||||
idleMs,
|
||||
timeoutMs: timeout.timeoutMs,
|
||||
@@ -287,7 +286,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
...timeout.details,
|
||||
});
|
||||
embeddedAgentLog.warn("codex app-server turn idle timed out waiting for progress", {
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId: params.getTurnId(),
|
||||
idleMs,
|
||||
timeoutMs: timeout.timeoutMs,
|
||||
@@ -332,7 +331,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
params.onTimeout(timeout);
|
||||
params.onMarkTimedOut();
|
||||
params.onRecordEvent("turn.completion_idle_timeout", {
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId: params.getTurnId(),
|
||||
idleMs,
|
||||
timeoutMs,
|
||||
@@ -340,7 +339,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
...timeout.details,
|
||||
});
|
||||
embeddedAgentLog.warn("codex app-server turn idle timed out waiting for completion", {
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId: params.getTurnId(),
|
||||
idleMs,
|
||||
timeoutMs,
|
||||
@@ -375,7 +374,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
params.onTimeout(timeout);
|
||||
params.onMarkTimedOut();
|
||||
params.onRecordEvent("turn.terminal_idle_timeout", {
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId: params.getTurnId(),
|
||||
idleMs,
|
||||
timeoutMs: timeout.timeoutMs,
|
||||
@@ -383,7 +382,7 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
...timeout.details,
|
||||
});
|
||||
embeddedAgentLog.warn("codex app-server turn idle timed out waiting for terminal event", {
|
||||
threadId: currentThreadId(),
|
||||
threadId: params.threadId,
|
||||
turnId: params.getTurnId(),
|
||||
idleMs,
|
||||
timeoutMs: timeout.timeoutMs,
|
||||
@@ -458,11 +457,9 @@ export function createCodexAttemptTurnWatchController(params: {
|
||||
details?: Record<string, unknown>;
|
||||
attemptProgress?: boolean;
|
||||
attemptTimeoutMs?: number;
|
||||
receivedAtMs?: number;
|
||||
},
|
||||
) => {
|
||||
const now = Date.now();
|
||||
completionLastActivityAt = Math.min(now, options?.receivedAtMs ?? now);
|
||||
completionLastActivityAt = Date.now();
|
||||
completionLastActivityReason = `notification:${method}`;
|
||||
if (options?.details !== undefined) {
|
||||
completionLastActivityDetails = options.details;
|
||||
|
||||
@@ -5,7 +5,6 @@ import path from "node:path";
|
||||
import {
|
||||
clearRuntimeAuthProfileStoreSnapshots,
|
||||
loadAuthProfileStoreForSecretsRuntime,
|
||||
replaceRuntimeAuthProfileStoreSnapshots,
|
||||
} from "openclaw/plugin-sdk/agent-runtime";
|
||||
import { upsertAuthProfile } from "openclaw/plugin-sdk/provider-auth";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
@@ -15,7 +14,6 @@ import {
|
||||
refreshCodexAppServerAuthTokens,
|
||||
resolveCodexAppServerAuthAccountCacheKey,
|
||||
resolveCodexAppServerAuthProfileId,
|
||||
resolveCodexAppServerAuthProfileStore,
|
||||
resolveCodexAppServerFallbackApiKeyCacheKey,
|
||||
resolveCodexAppServerHomeDir,
|
||||
resolveCodexAppServerNativeHomeDir,
|
||||
@@ -181,39 +179,6 @@ async function writeCodexCliApiKeyAuthFile(codexHome: string): Promise<void> {
|
||||
}
|
||||
|
||||
describe("bridgeCodexAppServerStartOptions", () => {
|
||||
it("preserves persisted provenance when preparing a supplied base store", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const authProfileStore = { version: 1, profiles: {} };
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
},
|
||||
});
|
||||
|
||||
const prepared = resolveCodexAppServerAuthProfileStore({
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(prepared).not.toBe(authProfileStore);
|
||||
expect(prepared.runtimePersistedProfileIds).toContain("openai:work");
|
||||
expect(prepared.profiles["openai:work"]).toMatchObject({
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("sets agent-owned CODEX_HOME without overriding HOME for local app-server launches", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const startOptions = createStartOptions();
|
||||
@@ -611,603 +576,6 @@ describe("bridgeCodexAppServerStartOptions", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("applies a supplied scoped OAuth profile instead of persisted credentials", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
expires: Date.now() + 24 * 60 * 60_000,
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
});
|
||||
const authProfileStore: AuthProfileStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
"openai:work": {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "scoped-access",
|
||||
refresh: "scoped-refresh",
|
||||
expires: Date.now() + 24 * 60 * 60_000,
|
||||
accountId: "scoped-account",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(request).toHaveBeenCalledWith("account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "scoped-access",
|
||||
chatgptAccountId: "scoped-account",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ name: "without persisted same-id credentials", persistSameId: false },
|
||||
{ name: "with persisted same-id credentials", persistSameId: true },
|
||||
])("refreshes an expired scoped OAuth profile $name", async ({ persistSameId }) => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
oauthMocks.refreshOpenAICodexToken.mockResolvedValueOnce({
|
||||
access: "scoped-refreshed-access",
|
||||
refresh: "scoped-refreshed-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
accountId: "scoped-refreshed-account",
|
||||
});
|
||||
try {
|
||||
if (persistSameId) {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
expires: Date.now() + 24 * 60 * 60_000,
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
});
|
||||
}
|
||||
const authProfileStore: AuthProfileStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
"openai:work": {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "scoped-expired-access",
|
||||
refresh: "scoped-refresh",
|
||||
expires: Date.now() - 60_000,
|
||||
accountId: "scoped-account",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledWith("scoped-refresh");
|
||||
expect(request).toHaveBeenCalledWith("account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "scoped-refreshed-access",
|
||||
chatgptAccountId: "scoped-refreshed-account",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
expect(authProfileStore.profiles["openai:work"]).toMatchObject({
|
||||
access: "scoped-refreshed-access",
|
||||
accountId: "scoped-refreshed-account",
|
||||
});
|
||||
if (persistSameId) {
|
||||
expect(
|
||||
loadAuthProfileStoreForSecretsRuntime(agentDir).profiles["openai:work"],
|
||||
).toMatchObject({
|
||||
access: "persisted-access",
|
||||
accountId: "persisted-account",
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("routes a supplied persisted OAuth clone through canonical refresh", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
oauthMocks.refreshOpenAICodexToken.mockResolvedValueOnce({
|
||||
access: "persisted-refreshed-access",
|
||||
refresh: "persisted-refreshed-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
accountId: "persisted-account",
|
||||
});
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "persisted-expired-access",
|
||||
refresh: "persisted-refresh",
|
||||
expires: Date.now() - 60_000,
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
});
|
||||
const authProfileStore = loadAuthProfileStoreForSecretsRuntime(agentDir);
|
||||
expect(authProfileStore.runtimePersistedProfileIds).toContain("openai:work");
|
||||
|
||||
await applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledWith("persisted-refresh");
|
||||
expect(request).toHaveBeenCalledWith("account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "persisted-refreshed-access",
|
||||
chatgptAccountId: "persisted-account",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
expect(loadAuthProfileStoreForSecretsRuntime(agentDir).profiles["openai:work"]).toMatchObject(
|
||||
{
|
||||
access: "persisted-refreshed-access",
|
||||
refresh: "persisted-refreshed-refresh",
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps a prepared persisted store aligned across rotating refresh tokens", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
oauthMocks.refreshOpenAICodexToken
|
||||
.mockResolvedValueOnce({
|
||||
access: "first-rotated-access",
|
||||
refresh: "first-rotated-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
access: "second-rotated-access",
|
||||
refresh: "second-rotated-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
});
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "initial-access",
|
||||
refresh: "initial-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
},
|
||||
});
|
||||
const authProfileStore = resolveCodexAppServerAuthProfileStore({
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore: { version: 1, profiles: {} },
|
||||
});
|
||||
|
||||
await refreshCodexAppServerAuthTokens({
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
await refreshCodexAppServerAuthTokens({
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken.mock.calls).toEqual([
|
||||
["initial-refresh"],
|
||||
["first-rotated-refresh"],
|
||||
]);
|
||||
expect(authProfileStore.profiles["openai:work"]).toMatchObject({
|
||||
access: "second-rotated-access",
|
||||
refresh: "second-rotated-refresh",
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("does not replace a prepared persisted store changed during refresh", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
let resolveRefresh:
|
||||
| ((value: { access: string; refresh: string; expires: number }) => void)
|
||||
| undefined;
|
||||
oauthMocks.refreshOpenAICodexToken.mockImplementationOnce(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveRefresh = resolve;
|
||||
}),
|
||||
);
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "initial-access",
|
||||
refresh: "initial-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
},
|
||||
});
|
||||
const authProfileStore = resolveCodexAppServerAuthProfileStore({
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore: { version: 1, profiles: {} },
|
||||
});
|
||||
|
||||
const refresh = refreshCodexAppServerAuthTokens({
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
await vi.waitFor(() => expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledTimes(1));
|
||||
authProfileStore.profiles["openai:work"] = {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "replacement-access",
|
||||
refresh: "replacement-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
accountId: "replacement-account",
|
||||
};
|
||||
resolveRefresh?.({
|
||||
access: "rotated-access",
|
||||
refresh: "rotated-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
});
|
||||
|
||||
await refresh;
|
||||
expect(authProfileStore.profiles["openai:work"]).toMatchObject({
|
||||
access: "replacement-access",
|
||||
refresh: "replacement-refresh",
|
||||
accountId: "replacement-account",
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps a runtime-external same-account OAuth profile scoped", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
oauthMocks.refreshOpenAICodexToken.mockResolvedValueOnce({
|
||||
access: "scoped-refreshed-access",
|
||||
refresh: "scoped-refreshed-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
accountId: "shared-account",
|
||||
});
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
expires: Date.now() + 24 * 60 * 60_000,
|
||||
accountId: "shared-account",
|
||||
},
|
||||
});
|
||||
const authProfileStore: AuthProfileStore = {
|
||||
version: 1,
|
||||
runtimeExternalProfileIds: ["openai:work"],
|
||||
runtimeExternalProfileIdsAuthoritative: true,
|
||||
profiles: {
|
||||
"openai:work": {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "scoped-expired-access",
|
||||
refresh: "scoped-refresh",
|
||||
expires: Date.now() - 60_000,
|
||||
accountId: "shared-account",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledWith("scoped-refresh");
|
||||
expect(request).toHaveBeenCalledWith("account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "scoped-refreshed-access",
|
||||
chatgptAccountId: "shared-account",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
expect(loadAuthProfileStoreForSecretsRuntime(agentDir).profiles["openai:work"]).toMatchObject(
|
||||
{
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
accountId: "shared-account",
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps an ambiguous supplied OAuth identity scoped", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
oauthMocks.refreshOpenAICodexToken.mockResolvedValueOnce({
|
||||
access: "scoped-refreshed-access",
|
||||
refresh: "scoped-refreshed-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
});
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
expires: Date.now() + 24 * 60 * 60_000,
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
});
|
||||
const authProfileStore: AuthProfileStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
"openai:work": {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "scoped-expired-access",
|
||||
refresh: "scoped-refresh",
|
||||
expires: Date.now() - 60_000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledWith("scoped-refresh");
|
||||
expect(request).toHaveBeenCalledWith("account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "scoped-refreshed-access",
|
||||
chatgptAccountId: "openai:work",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
expect(loadAuthProfileStoreForSecretsRuntime(agentDir).profiles["openai:work"]).toMatchObject(
|
||||
{
|
||||
access: "persisted-access",
|
||||
refresh: "persisted-refresh",
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("routes a same-identity stale persisted clone through canonical persisted auth", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "stale-access",
|
||||
refresh: "stale-refresh",
|
||||
expires: Date.now() - 60_000,
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
});
|
||||
const authProfileStore = loadAuthProfileStoreForSecretsRuntime(agentDir);
|
||||
expect(authProfileStore.runtimePersistedProfileIds).toContain("openai:work");
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "current-access",
|
||||
refresh: "current-refresh",
|
||||
expires: Date.now() + 24 * 60 * 60_000,
|
||||
accountId: "persisted-account",
|
||||
},
|
||||
});
|
||||
|
||||
await applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken).not.toHaveBeenCalled();
|
||||
expect(request).toHaveBeenCalledWith("account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "current-access",
|
||||
chatgptAccountId: "persisted-account",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps a changed-identity persisted clone scoped", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
oauthMocks.refreshOpenAICodexToken.mockResolvedValueOnce({
|
||||
access: "account-a-refreshed-access",
|
||||
refresh: "account-a-refreshed-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
accountId: "account-a",
|
||||
});
|
||||
try {
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "account-a-expired-access",
|
||||
refresh: "account-a-refresh",
|
||||
expires: Date.now() - 60_000,
|
||||
accountId: "account-a",
|
||||
},
|
||||
});
|
||||
const authProfileStore = loadAuthProfileStoreForSecretsRuntime(agentDir);
|
||||
expect(authProfileStore.runtimePersistedProfileIds).toContain("openai:work");
|
||||
upsertAuthProfile({
|
||||
agentDir,
|
||||
profileId: "openai:work",
|
||||
credential: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "account-b-access",
|
||||
refresh: "account-b-refresh",
|
||||
expires: Date.now() + 24 * 60 * 60_000,
|
||||
accountId: "account-b",
|
||||
},
|
||||
});
|
||||
replaceRuntimeAuthProfileStoreSnapshots([{ agentDir, store: authProfileStore }]);
|
||||
|
||||
await applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledWith("account-a-refresh");
|
||||
expect(request).toHaveBeenCalledWith("account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "account-a-refreshed-access",
|
||||
chatgptAccountId: "account-a",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
expect(loadAuthProfileStoreForSecretsRuntime(agentDir).profiles["openai:work"]).toMatchObject(
|
||||
{
|
||||
access: "account-b-access",
|
||||
refresh: "account-b-refresh",
|
||||
accountId: "account-b",
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("serializes concurrent refreshes of the same scoped OAuth profile", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ type: "chatgptAuthTokens" }));
|
||||
let resolveRefresh:
|
||||
| ((value: { access: string; refresh: string; expires: number; accountId: string }) => void)
|
||||
| undefined;
|
||||
oauthMocks.refreshOpenAICodexToken.mockImplementationOnce(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveRefresh = resolve;
|
||||
}),
|
||||
);
|
||||
const authProfileStore: AuthProfileStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
"openai:work": {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "scoped-expired-access",
|
||||
refresh: "scoped-refresh",
|
||||
expires: Date.now() - 60_000,
|
||||
accountId: "scoped-account",
|
||||
},
|
||||
},
|
||||
};
|
||||
try {
|
||||
const first = applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
const second = applyCodexAppServerAuthProfile({
|
||||
client: { request } as never,
|
||||
agentDir,
|
||||
authProfileId: "openai:work",
|
||||
authProfileStore,
|
||||
});
|
||||
await vi.waitFor(() => expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledTimes(1));
|
||||
|
||||
resolveRefresh?.({
|
||||
access: "scoped-refreshed-access",
|
||||
refresh: "scoped-refreshed-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
accountId: "scoped-refreshed-account",
|
||||
});
|
||||
await Promise.all([first, second]);
|
||||
|
||||
expect(oauthMocks.refreshOpenAICodexToken).toHaveBeenCalledTimes(1);
|
||||
expect(request).toHaveBeenCalledTimes(2);
|
||||
expect(request).toHaveBeenNthCalledWith(1, "account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "scoped-refreshed-access",
|
||||
chatgptAccountId: "scoped-refreshed-account",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
expect(request).toHaveBeenNthCalledWith(2, "account/login/start", {
|
||||
type: "chatgptAuthTokens",
|
||||
accessToken: "scoped-refreshed-access",
|
||||
chatgptAccountId: "scoped-refreshed-account",
|
||||
chatgptPlanType: null,
|
||||
});
|
||||
} finally {
|
||||
resolveRefresh?.({
|
||||
access: "cleanup-access",
|
||||
refresh: "cleanup-refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
accountId: "cleanup-account",
|
||||
});
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves native app-server auth untouched when auth bridging is disabled", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-app-server-"));
|
||||
const request = vi.fn(async () => ({ requiresOpenaiAuth: true }));
|
||||
|
||||
@@ -4,10 +4,9 @@ import fsSync from "node:fs";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import {
|
||||
ensureAuthProfileStore,
|
||||
findPersistedAuthProfileCredential,
|
||||
ensureAuthProfileStoreWithoutExternalProfiles,
|
||||
loadAuthProfileStoreForSecretsRuntime,
|
||||
refreshOAuthCredentialForRuntime,
|
||||
resolveAuthProfileOrder,
|
||||
@@ -19,7 +18,6 @@ import {
|
||||
type AuthProfileStore,
|
||||
type OAuthCredential,
|
||||
} from "openclaw/plugin-sdk/agent-runtime";
|
||||
import { hasUsableOAuthCredential } from "openclaw/plugin-sdk/provider-auth";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import type { CodexAppServerStartOptions } from "./config.js";
|
||||
import type {
|
||||
@@ -50,16 +48,11 @@ const CODEX_AUTH_JSON_FILENAME = "auth.json";
|
||||
const CODEX_HOME_DIRNAME = ".codex";
|
||||
|
||||
type AuthProfileOrderConfig = Parameters<typeof resolveAuthProfileOrder>[0]["cfg"];
|
||||
const scopedOAuthRefreshQueues = new WeakMap<
|
||||
AuthProfileStore,
|
||||
Map<string, Promise<OAuthCredential>>
|
||||
>();
|
||||
|
||||
export async function bridgeCodexAppServerStartOptions(params: {
|
||||
startOptions: CodexAppServerStartOptions;
|
||||
agentDir: string;
|
||||
authProfileId?: string | null;
|
||||
authProfileStore?: AuthProfileStore;
|
||||
config?: AuthProfileOrderConfig;
|
||||
}): Promise<CodexAppServerStartOptions> {
|
||||
if (params.startOptions.transport !== "stdio") {
|
||||
@@ -72,10 +65,9 @@ export async function bridgeCodexAppServerStartOptions(params: {
|
||||
if (params.authProfileId === null) {
|
||||
return isolatedStartOptions;
|
||||
}
|
||||
const store = resolveCodexAppServerAuthProfileStore({
|
||||
const store = ensureCodexAppServerAuthProfileStore({
|
||||
agentDir: params.agentDir,
|
||||
authProfileId: params.authProfileId,
|
||||
authProfileStore: params.authProfileStore,
|
||||
config: params.config,
|
||||
});
|
||||
const authProfileId = resolveCodexAppServerAuthProfileId({
|
||||
@@ -111,15 +103,13 @@ export function resolveCodexAppServerAuthProfileId(params: {
|
||||
|
||||
export function resolveCodexAppServerAuthProfileIdForAgent(params: {
|
||||
authProfileId?: string;
|
||||
authProfileStore?: AuthProfileStore;
|
||||
agentDir?: string;
|
||||
config?: AuthProfileOrderConfig;
|
||||
}): string | undefined {
|
||||
const agentDir = params.agentDir?.trim() || resolveDefaultAgentDir(params.config ?? {});
|
||||
const store = resolveCodexAppServerAuthProfileStore({
|
||||
const store = ensureCodexAppServerAuthProfileStore({
|
||||
agentDir,
|
||||
authProfileId: params.authProfileId,
|
||||
authProfileStore: params.authProfileStore,
|
||||
config: params.config,
|
||||
});
|
||||
return resolveCodexAppServerAuthProfileId({
|
||||
@@ -142,7 +132,7 @@ function ensureCodexAppServerAuthProfileStore(params: {
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveCodexAppServerAuthProfileStore(params: {
|
||||
function resolveCodexAppServerAuthProfileStore(params: {
|
||||
agentDir?: string;
|
||||
authProfileId?: string;
|
||||
authProfileStore?: AuthProfileStore;
|
||||
@@ -173,41 +163,13 @@ export function resolveCodexAppServerAuthProfileStore(params: {
|
||||
...params.authProfileStore.order,
|
||||
}
|
||||
: undefined;
|
||||
const profiles = {
|
||||
...overlaidStore.profiles,
|
||||
...params.authProfileStore.profiles,
|
||||
};
|
||||
const suppliedProfileIds = new Set(Object.keys(params.authProfileStore.profiles));
|
||||
const mergeRuntimeProfileIds = (overlaidIds?: string[], suppliedIds?: string[]) => [
|
||||
...(overlaidIds ?? []).filter((profileId) => !suppliedProfileIds.has(profileId)),
|
||||
...(suppliedIds ?? []),
|
||||
];
|
||||
const runtimePersistedProfileIds = mergeRuntimeProfileIds(
|
||||
overlaidStore.runtimePersistedProfileIds,
|
||||
params.authProfileStore.runtimePersistedProfileIds,
|
||||
).filter((profileId) => profiles[profileId]);
|
||||
const runtimeExternalProfileIds = mergeRuntimeProfileIds(
|
||||
overlaidStore.runtimeExternalProfileIds,
|
||||
params.authProfileStore.runtimeExternalProfileIds,
|
||||
).filter((profileId) => profiles[profileId]);
|
||||
const runtimeExternalProfileIdsAuthoritative =
|
||||
overlaidStore.runtimeExternalProfileIdsAuthoritative === true ||
|
||||
params.authProfileStore.runtimeExternalProfileIdsAuthoritative === true;
|
||||
return {
|
||||
...params.authProfileStore,
|
||||
...(order ? { order } : {}),
|
||||
profiles,
|
||||
...(runtimePersistedProfileIds.length > 0
|
||||
? { runtimePersistedProfileIds: [...new Set(runtimePersistedProfileIds)] }
|
||||
: {}),
|
||||
...(runtimeExternalProfileIds.length > 0 || runtimeExternalProfileIdsAuthoritative
|
||||
? {
|
||||
runtimeExternalProfileIds: [...new Set(runtimeExternalProfileIds)],
|
||||
...(runtimeExternalProfileIdsAuthoritative
|
||||
? { runtimeExternalProfileIdsAuthoritative: true }
|
||||
: {}),
|
||||
}
|
||||
: {}),
|
||||
profiles: {
|
||||
...overlaidStore.profiles,
|
||||
...params.authProfileStore.profiles,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -377,7 +339,6 @@ export async function applyCodexAppServerAuthProfile(params: {
|
||||
client: CodexAppServerClient;
|
||||
agentDir: string;
|
||||
authProfileId?: string | null;
|
||||
authProfileStore?: AuthProfileStore;
|
||||
startOptions?: CodexAppServerStartOptions;
|
||||
config?: AuthProfileOrderConfig;
|
||||
}): Promise<void> {
|
||||
@@ -387,7 +348,6 @@ export async function applyCodexAppServerAuthProfile(params: {
|
||||
const loginParams = await resolveCodexAppServerAuthProfileLoginParams({
|
||||
agentDir: params.agentDir,
|
||||
authProfileId: params.authProfileId,
|
||||
authProfileStore: params.authProfileStore,
|
||||
config: params.config,
|
||||
});
|
||||
if (!loginParams) {
|
||||
@@ -411,7 +371,6 @@ export async function applyCodexAppServerAuthProfile(params: {
|
||||
function resolveCodexAppServerAuthProfileLoginParams(params: {
|
||||
agentDir: string;
|
||||
authProfileId?: string;
|
||||
authProfileStore?: AuthProfileStore;
|
||||
config?: AuthProfileOrderConfig;
|
||||
}): Promise<CodexLoginAccountParams | undefined> {
|
||||
return resolveCodexAppServerAuthProfileLoginParamsInternal(params);
|
||||
@@ -420,7 +379,6 @@ function resolveCodexAppServerAuthProfileLoginParams(params: {
|
||||
export async function refreshCodexAppServerAuthTokens(params: {
|
||||
agentDir: string;
|
||||
authProfileId?: string;
|
||||
authProfileStore?: AuthProfileStore;
|
||||
config?: AuthProfileOrderConfig;
|
||||
}): Promise<CodexChatgptAuthTokensRefreshResponse> {
|
||||
const loginParams = await resolveCodexAppServerAuthProfileLoginParamsInternal({
|
||||
@@ -440,14 +398,12 @@ export async function refreshCodexAppServerAuthTokens(params: {
|
||||
async function resolveCodexAppServerAuthProfileLoginParamsInternal(params: {
|
||||
agentDir: string;
|
||||
authProfileId?: string;
|
||||
authProfileStore?: AuthProfileStore;
|
||||
forceOAuthRefresh?: boolean;
|
||||
config?: AuthProfileOrderConfig;
|
||||
}): Promise<CodexLoginAccountParams | undefined> {
|
||||
const store = resolveCodexAppServerAuthProfileStore({
|
||||
const store = ensureCodexAppServerAuthProfileStore({
|
||||
agentDir: params.agentDir,
|
||||
authProfileId: params.authProfileId,
|
||||
authProfileStore: params.authProfileStore,
|
||||
config: params.config,
|
||||
});
|
||||
const profileId = resolveCodexAppServerAuthProfileId({
|
||||
@@ -469,8 +425,6 @@ async function resolveCodexAppServerAuthProfileLoginParamsInternal(params: {
|
||||
}
|
||||
const loginParams = await resolveLoginParamsForCredential(profileId, credential, {
|
||||
agentDir: params.agentDir,
|
||||
store,
|
||||
preferStoreCredential: Boolean(params.authProfileStore?.profiles[profileId]),
|
||||
forceOAuthRefresh: params.forceOAuthRefresh === true,
|
||||
config: params.config,
|
||||
});
|
||||
@@ -555,22 +509,14 @@ function resolveCodexCliAuthFileApiKeyCacheKey(env: NodeJS.ProcessEnv): string |
|
||||
async function resolveLoginParamsForCredential(
|
||||
profileId: string,
|
||||
credential: AuthProfileCredential,
|
||||
params: {
|
||||
agentDir: string;
|
||||
store: AuthProfileStore;
|
||||
preferStoreCredential: boolean;
|
||||
forceOAuthRefresh: boolean;
|
||||
config?: AuthProfileOrderConfig;
|
||||
},
|
||||
params: { agentDir: string; forceOAuthRefresh: boolean; config?: AuthProfileOrderConfig },
|
||||
): Promise<CodexLoginAccountParams | undefined> {
|
||||
// Runtime honors the persisted auth profile type. Shape-based remediation
|
||||
// belongs at credential entry time so request handling does not preemptively
|
||||
// reject opaque provider credentials.
|
||||
if (credential.type === "api_key") {
|
||||
const resolved = await resolveApiKeyForProfile({
|
||||
store: params.preferStoreCredential
|
||||
? params.store
|
||||
: ensureAuthProfileStore(params.agentDir, { allowKeychainPrompt: false }),
|
||||
store: ensureAuthProfileStore(params.agentDir, { allowKeychainPrompt: false }),
|
||||
profileId,
|
||||
agentDir: params.agentDir,
|
||||
});
|
||||
@@ -579,9 +525,7 @@ async function resolveLoginParamsForCredential(
|
||||
}
|
||||
if (credential.type === "token") {
|
||||
const resolved = await resolveApiKeyForProfile({
|
||||
store: params.preferStoreCredential
|
||||
? params.store
|
||||
: ensureAuthProfileStore(params.agentDir, { allowKeychainPrompt: false }),
|
||||
store: ensureAuthProfileStore(params.agentDir, { allowKeychainPrompt: false }),
|
||||
profileId,
|
||||
agentDir: params.agentDir,
|
||||
});
|
||||
@@ -595,8 +539,6 @@ async function resolveLoginParamsForCredential(
|
||||
}
|
||||
const resolvedCredential = await resolveOAuthCredentialForCodexAppServer(profileId, credential, {
|
||||
agentDir: params.agentDir,
|
||||
store: params.store,
|
||||
preferStoreCredential: params.preferStoreCredential,
|
||||
forceRefresh: params.forceOAuthRefresh,
|
||||
config: params.config,
|
||||
});
|
||||
@@ -609,40 +551,22 @@ async function resolveLoginParamsForCredential(
|
||||
async function resolveOAuthCredentialForCodexAppServer(
|
||||
profileId: string,
|
||||
credential: OAuthCredential,
|
||||
params: {
|
||||
agentDir: string;
|
||||
store: AuthProfileStore;
|
||||
preferStoreCredential: boolean;
|
||||
forceRefresh: boolean;
|
||||
config?: AuthProfileOrderConfig;
|
||||
},
|
||||
params: { agentDir: string; forceRefresh: boolean; config?: AuthProfileOrderConfig },
|
||||
): Promise<OAuthCredential> {
|
||||
const ownerAgentDir = resolvePersistedAuthProfileOwnerAgentDir({
|
||||
agentDir: params.agentDir,
|
||||
profileId,
|
||||
});
|
||||
const persistedCredential = findPersistedAuthProfileCredential({
|
||||
const store = ensureCodexAppServerAuthProfileStore({
|
||||
agentDir: ownerAgentDir,
|
||||
profileId,
|
||||
authProfileId: profileId,
|
||||
config: params.config,
|
||||
});
|
||||
const useScopedCredential =
|
||||
params.preferStoreCredential &&
|
||||
shouldUseScopedOAuthCredential({
|
||||
store: params.store,
|
||||
profileId,
|
||||
persistedCredential,
|
||||
suppliedCredential: credential,
|
||||
config: params.config,
|
||||
});
|
||||
const store = useScopedCredential
|
||||
? params.store
|
||||
: ensureCodexAppServerAuthProfileStore({
|
||||
agentDir: ownerAgentDir,
|
||||
authProfileId: profileId,
|
||||
config: params.config,
|
||||
});
|
||||
const persistedStore = ensureAuthProfileStoreWithoutExternalProfiles(ownerAgentDir, {
|
||||
allowKeychainPrompt: false,
|
||||
});
|
||||
const persistedCredential = persistedStore.profiles[profileId];
|
||||
const persistedOAuthCredential =
|
||||
!useScopedCredential &&
|
||||
persistedCredential?.type === "oauth" &&
|
||||
isCodexAppServerAuthProvider(persistedCredential.provider, params.config)
|
||||
? persistedCredential
|
||||
@@ -653,14 +577,6 @@ async function resolveOAuthCredentialForCodexAppServer(
|
||||
isCodexAppServerAuthProvider(ownerCredential.provider, params.config)
|
||||
? ownerCredential
|
||||
: undefined;
|
||||
if (useScopedCredential && overlaidOAuthCredential) {
|
||||
return await resolveScopedOAuthCredential({
|
||||
store,
|
||||
profileId,
|
||||
credential: overlaidOAuthCredential,
|
||||
forceRefresh: params.forceRefresh,
|
||||
});
|
||||
}
|
||||
if (params.forceRefresh && !persistedOAuthCredential && overlaidOAuthCredential) {
|
||||
const refreshedRuntimeCredential = await refreshOAuthCredentialForRuntime({
|
||||
credential: overlaidOAuthCredential,
|
||||
@@ -677,111 +593,18 @@ async function resolveOAuthCredentialForCodexAppServer(
|
||||
agentDir: ownerAgentDir,
|
||||
forceRefresh: params.forceRefresh && Boolean(persistedOAuthCredential),
|
||||
});
|
||||
const refreshed = useScopedCredential
|
||||
? undefined
|
||||
: loadAuthProfileStoreForSecretsRuntime(ownerAgentDir).profiles[profileId];
|
||||
const refreshedOAuthCredential =
|
||||
const refreshed = loadAuthProfileStoreForSecretsRuntime(ownerAgentDir).profiles[profileId];
|
||||
const storedCredential = store.profiles[profileId];
|
||||
const candidate =
|
||||
refreshed?.type === "oauth" && isCodexAppServerAuthProvider(refreshed.provider, params.config)
|
||||
? refreshed
|
||||
: undefined;
|
||||
if (refreshedOAuthCredential && isDeepStrictEqual(params.store.profiles[profileId], credential)) {
|
||||
// Persisted refreshes rotate refresh tokens. Keep an isolated prepared
|
||||
// store aligned without reverting a concurrent caller-owned replacement.
|
||||
params.store.profiles[profileId] = refreshedOAuthCredential;
|
||||
}
|
||||
const storedCredential = store.profiles[profileId];
|
||||
const candidate = refreshedOAuthCredential
|
||||
? refreshedOAuthCredential
|
||||
: storedCredential?.type === "oauth" &&
|
||||
isCodexAppServerAuthProvider(storedCredential.provider, params.config)
|
||||
? storedCredential
|
||||
: credential;
|
||||
: storedCredential?.type === "oauth" &&
|
||||
isCodexAppServerAuthProvider(storedCredential.provider, params.config)
|
||||
? storedCredential
|
||||
: credential;
|
||||
return resolved?.apiKey ? { ...candidate, access: resolved.apiKey } : candidate;
|
||||
}
|
||||
|
||||
function shouldUseScopedOAuthCredential(params: {
|
||||
store: AuthProfileStore;
|
||||
profileId: string;
|
||||
persistedCredential: AuthProfileCredential | undefined;
|
||||
suppliedCredential: OAuthCredential;
|
||||
config?: AuthProfileOrderConfig;
|
||||
}): boolean {
|
||||
if (!params.store.runtimePersistedProfileIds?.includes(params.profileId)) {
|
||||
return true;
|
||||
}
|
||||
const persisted = params.persistedCredential;
|
||||
if (persisted?.type !== "oauth") {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
resolveProviderIdForAuth(persisted.provider, { config: params.config }) !==
|
||||
resolveProviderIdForAuth(params.suppliedCredential.provider, { config: params.config })
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
!isDeepStrictEqual(persisted, params.suppliedCredential) &&
|
||||
!hasMatchingOAuthIdentity(persisted, params.suppliedCredential)
|
||||
);
|
||||
}
|
||||
|
||||
function hasMatchingOAuthIdentity(persisted: OAuthCredential, supplied: OAuthCredential): boolean {
|
||||
const persistedAccountId = persisted.accountId?.trim();
|
||||
const suppliedAccountId = supplied.accountId?.trim();
|
||||
if (persistedAccountId && suppliedAccountId) {
|
||||
return persistedAccountId === suppliedAccountId;
|
||||
}
|
||||
const persistedEmail = persisted.email?.trim().toLowerCase();
|
||||
const suppliedEmail = supplied.email?.trim().toLowerCase();
|
||||
return Boolean(persistedEmail && suppliedEmail && persistedEmail === suppliedEmail);
|
||||
}
|
||||
|
||||
async function resolveScopedOAuthCredential(params: {
|
||||
store: AuthProfileStore;
|
||||
profileId: string;
|
||||
credential: OAuthCredential;
|
||||
forceRefresh: boolean;
|
||||
}): Promise<OAuthCredential> {
|
||||
const existingRefresh = scopedOAuthRefreshQueues.get(params.store)?.get(params.profileId);
|
||||
if (existingRefresh) {
|
||||
return await existingRefresh;
|
||||
}
|
||||
if (!params.forceRefresh && hasUsableOAuthCredential(params.credential)) {
|
||||
return params.credential;
|
||||
}
|
||||
|
||||
const storeRefreshes = scopedOAuthRefreshQueues.get(params.store) ?? new Map();
|
||||
scopedOAuthRefreshQueues.set(params.store, storeRefreshes);
|
||||
const refresh = (async () => {
|
||||
const current = params.store.profiles[params.profileId];
|
||||
const credential = current?.type === "oauth" ? current : params.credential;
|
||||
if (!params.forceRefresh && hasUsableOAuthCredential(credential)) {
|
||||
return credential;
|
||||
}
|
||||
const refreshed = await refreshOAuthCredentialForRuntime({ credential });
|
||||
if (!refreshed?.access?.trim()) {
|
||||
throw new Error(`Codex app-server auth profile "${params.profileId}" could not refresh.`);
|
||||
}
|
||||
if (!isDeepStrictEqual(params.store.profiles[params.profileId], credential)) {
|
||||
throw new Error(
|
||||
`Codex app-server auth profile "${params.profileId}" changed while refreshing.`,
|
||||
);
|
||||
}
|
||||
params.store.profiles[params.profileId] = refreshed;
|
||||
return refreshed;
|
||||
})();
|
||||
storeRefreshes.set(params.profileId, refresh);
|
||||
try {
|
||||
return await refresh;
|
||||
} finally {
|
||||
// Scoped stores are process-local; serialize their rotating refresh token
|
||||
// and release the queue entry with the refresh that owns it.
|
||||
if (storeRefreshes.get(params.profileId) === refresh) {
|
||||
storeRefreshes.delete(params.profileId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isCodexAppServerAuthProvider(provider: string, config?: AuthProfileOrderConfig): boolean {
|
||||
const resolvedProvider = resolveProviderIdForAuth(provider, { config });
|
||||
return (
|
||||
|
||||
@@ -8,56 +8,37 @@ import {
|
||||
} from "openclaw/plugin-sdk/agent-harness";
|
||||
import { AUTH_PROFILE_RUNTIME_CONTRACT } from "openclaw/plugin-sdk/agent-runtime-test-contracts";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { CodexAppServerClientFactory } from "./client-factory.js";
|
||||
import { runCodexAppServerAttempt as runCodexAppServerAttemptImpl } from "./run-attempt.js";
|
||||
import {
|
||||
readCodexAppServerBinding,
|
||||
registerCodexTestSessionIdentity,
|
||||
resetCodexTestBindingStore,
|
||||
testCodexAppServerBindingStore,
|
||||
writeCodexAppServerBinding,
|
||||
} from "./session-binding.test-helpers.js";
|
||||
import type { CodexAppServerClientLeaseFactory } from "./shared-client.js";
|
||||
import {
|
||||
adaptCodexTestClientFactory,
|
||||
createCodexTestModel,
|
||||
type CodexTestAppServerClientFactory,
|
||||
} from "./test-support.js";
|
||||
import { readCodexAppServerBinding, writeCodexAppServerBinding } from "./session-binding.js";
|
||||
import { createCodexTestModel } from "./test-support.js";
|
||||
|
||||
let codexAppServerClientLeaseFactoryForTest: CodexAppServerClientLeaseFactory | undefined;
|
||||
let codexAppServerClientFactoryForTest: CodexAppServerClientFactory | undefined;
|
||||
|
||||
type RunCodexAppServerAttemptImplOptions = NonNullable<
|
||||
type RunCodexAppServerAttemptOptions = NonNullable<
|
||||
Parameters<typeof runCodexAppServerAttemptImpl>[1]
|
||||
>;
|
||||
type RunCodexAppServerAttemptOptions = Omit<RunCodexAppServerAttemptImplOptions, "bindingStore"> & {
|
||||
bindingStore?: RunCodexAppServerAttemptImplOptions["bindingStore"];
|
||||
};
|
||||
|
||||
function setCodexAppServerClientFactoryForTest(factory: CodexTestAppServerClientFactory): void {
|
||||
codexAppServerClientLeaseFactoryForTest = adaptCodexTestClientFactory(factory);
|
||||
function setCodexAppServerClientFactoryForTest(factory: CodexAppServerClientFactory): void {
|
||||
codexAppServerClientFactoryForTest = factory;
|
||||
}
|
||||
|
||||
function resetCodexAppServerClientFactoryForTest(): void {
|
||||
codexAppServerClientLeaseFactoryForTest = undefined;
|
||||
codexAppServerClientFactoryForTest = undefined;
|
||||
}
|
||||
|
||||
function runCodexAppServerAttempt(
|
||||
params: EmbeddedRunAttemptParams,
|
||||
options: RunCodexAppServerAttemptOptions = {},
|
||||
) {
|
||||
const clientLeaseFactory = options.clientLeaseFactory ?? codexAppServerClientLeaseFactoryForTest;
|
||||
return runCodexAppServerAttemptImpl(params, {
|
||||
...options,
|
||||
bindingStore: options.bindingStore ?? testCodexAppServerBindingStore,
|
||||
...(clientLeaseFactory ? { clientLeaseFactory } : {}),
|
||||
});
|
||||
const clientFactory = options.clientFactory ?? codexAppServerClientFactoryForTest;
|
||||
return runCodexAppServerAttemptImpl(
|
||||
params,
|
||||
clientFactory ? { ...options, clientFactory } : options,
|
||||
);
|
||||
}
|
||||
|
||||
function createParams(sessionFile: string, workspaceDir: string): EmbeddedRunAttemptParams {
|
||||
registerCodexTestSessionIdentity(
|
||||
sessionFile,
|
||||
AUTH_PROFILE_RUNTIME_CONTRACT.sessionId,
|
||||
AUTH_PROFILE_RUNTIME_CONTRACT.sessionKey,
|
||||
);
|
||||
return {
|
||||
prompt: AUTH_PROFILE_RUNTIME_CONTRACT.workspacePrompt,
|
||||
sessionId: AUTH_PROFILE_RUNTIME_CONTRACT.sessionId,
|
||||
@@ -130,8 +111,7 @@ function createCodexAuthProfileHarness(params: { startMethod: "thread/start" | "
|
||||
const seenAuthProfileIds: Array<string | undefined> = [];
|
||||
const seenAgentDirs: Array<string | undefined> = [];
|
||||
const requests: Array<{ method: string; params: unknown }> = [];
|
||||
const notificationHandlers = new Set<(notification: unknown) => Promise<void> | void>();
|
||||
const requestHandlers = new Set<(request: unknown) => unknown>();
|
||||
let notify: (notification: unknown) => Promise<void> = async () => undefined;
|
||||
setCodexAppServerClientFactoryForTest(async (_startOptions, authProfileId, agentDir) => {
|
||||
seenAuthProfileIds.push(authProfileId);
|
||||
seenAgentDirs.push(agentDir);
|
||||
@@ -146,22 +126,13 @@ function createCodexAuthProfileHarness(params: { startMethod: "thread/start" | "
|
||||
}
|
||||
throw new Error(`unexpected method: ${method}`);
|
||||
}),
|
||||
addNotificationHandler: (handler: (notification: unknown) => Promise<void> | void) => {
|
||||
notificationHandlers.add(handler);
|
||||
return () => notificationHandlers.delete(handler);
|
||||
addNotificationHandler: (handler: (notification: unknown) => Promise<void>) => {
|
||||
notify = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
addRequestHandler: (handler: (request: unknown) => unknown) => {
|
||||
requestHandlers.add(handler);
|
||||
return () => requestHandlers.delete(handler);
|
||||
},
|
||||
addCloseHandler: () => () => undefined,
|
||||
addRequestHandler: () => () => undefined,
|
||||
} as never;
|
||||
});
|
||||
const notify = async (notification: unknown) => {
|
||||
await Promise.all(
|
||||
[...notificationHandlers].map((handler) => Promise.resolve(handler(notification))),
|
||||
);
|
||||
};
|
||||
return {
|
||||
seenAuthProfileIds,
|
||||
seenAgentDirs,
|
||||
@@ -187,7 +158,6 @@ describe("Auth profile runtime contract - Codex app-server adapter", () => {
|
||||
let tmpDir: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
resetCodexTestBindingStore();
|
||||
vi.useRealTimers();
|
||||
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-auth-contract-"));
|
||||
});
|
||||
@@ -223,7 +193,6 @@ describe("Auth profile runtime contract - Codex app-server adapter", () => {
|
||||
it("reuses a bound OpenAI Codex auth profile when resume params omit authProfileId", async () => {
|
||||
const harness = createCodexAuthProfileHarness({ startMethod: "thread/resume" });
|
||||
const sessionFile = path.join(tmpDir, "session.jsonl");
|
||||
const params = createParams(sessionFile, tmpDir);
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-auth-contract",
|
||||
cwd: tmpDir,
|
||||
@@ -231,6 +200,7 @@ describe("Auth profile runtime contract - Codex app-server adapter", () => {
|
||||
dynamicToolsFingerprint: "[]",
|
||||
});
|
||||
// authProfileId is intentionally omitted to exercise the resume-bound profile path.
|
||||
const params = createParams(sessionFile, tmpDir);
|
||||
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
await vi.waitFor(
|
||||
@@ -248,13 +218,13 @@ describe("Auth profile runtime contract - Codex app-server adapter", () => {
|
||||
it("prefers an explicit runtime auth profile over a stale persisted binding", async () => {
|
||||
const harness = createCodexAuthProfileHarness({ startMethod: "thread/resume" });
|
||||
const sessionFile = path.join(tmpDir, "session.jsonl");
|
||||
const params = createParams(sessionFile, tmpDir);
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-auth-contract",
|
||||
cwd: tmpDir,
|
||||
authProfileId: "openai:stale",
|
||||
dynamicToolsFingerprint: "[]",
|
||||
});
|
||||
const params = createParams(sessionFile, tmpDir);
|
||||
params.authProfileId = AUTH_PROFILE_RUNTIME_CONTRACT.openAiCodexProfileId;
|
||||
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
|
||||
67
extensions/codex/src/app-server/client-factory.ts
Normal file
67
extensions/codex/src/app-server/client-factory.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Lazy factories for shared and leased Codex app-server clients.
|
||||
*/
|
||||
import type { resolveCodexAppServerAuthProfileIdForAgent } from "./auth-bridge.js";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import type { CodexAppServerStartOptions } from "./config.js";
|
||||
|
||||
type AuthProfileOrderConfig = Parameters<
|
||||
typeof resolveCodexAppServerAuthProfileIdForAgent
|
||||
>[0]["config"];
|
||||
|
||||
/** Factory signature used by Codex attempt startup to acquire a client. */
|
||||
export type CodexAppServerClientFactory = (
|
||||
startOptions?: CodexAppServerStartOptions,
|
||||
authProfileId?: string,
|
||||
agentDir?: string,
|
||||
config?: AuthProfileOrderConfig,
|
||||
options?: {
|
||||
onStartedClient?: (client: CodexAppServerClient) => void;
|
||||
abandonSignal?: AbortSignal;
|
||||
},
|
||||
) => Promise<CodexAppServerClient>;
|
||||
|
||||
let sharedClientModulePromise: Promise<typeof import("./shared-client.js")> | null = null;
|
||||
|
||||
const loadSharedClientModule = async () => {
|
||||
sharedClientModulePromise ??= import("./shared-client.js");
|
||||
return await sharedClientModulePromise;
|
||||
};
|
||||
|
||||
/** Returns the process-shared app-server client for normal attempt reuse. */
|
||||
export const defaultCodexAppServerClientFactory: CodexAppServerClientFactory = (
|
||||
startOptions,
|
||||
authProfileId,
|
||||
agentDir,
|
||||
config,
|
||||
options,
|
||||
) =>
|
||||
loadSharedClientModule().then(({ getSharedCodexAppServerClient }) =>
|
||||
getSharedCodexAppServerClient({
|
||||
startOptions,
|
||||
authProfileId,
|
||||
agentDir,
|
||||
config,
|
||||
onStartedClient: options?.onStartedClient,
|
||||
abandonSignal: options?.abandonSignal,
|
||||
}),
|
||||
);
|
||||
|
||||
/** Returns a leased shared client so startup can release ownership explicitly. */
|
||||
export const defaultLeasedCodexAppServerClientFactory: CodexAppServerClientFactory = (
|
||||
startOptions,
|
||||
authProfileId,
|
||||
agentDir,
|
||||
config,
|
||||
options,
|
||||
) =>
|
||||
loadSharedClientModule().then(({ getLeasedSharedCodexAppServerClient }) =>
|
||||
getLeasedSharedCodexAppServerClient({
|
||||
startOptions,
|
||||
authProfileId,
|
||||
agentDir,
|
||||
config,
|
||||
onStartedClient: options?.onStartedClient,
|
||||
abandonSignal: options?.abandonSignal,
|
||||
}),
|
||||
);
|
||||
@@ -1,78 +0,0 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import { createClientHarness } from "./test-support.js";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
refreshAuth: vi.fn(async () => ({ accessToken: "refreshed", chatgptAccountId: "account" })),
|
||||
mergeRateLimitUpdate: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./auth-bridge.js", () => ({
|
||||
refreshCodexAppServerAuthTokens: mocks.refreshAuth,
|
||||
}));
|
||||
|
||||
vi.mock("./rate-limit-cache.js", () => ({
|
||||
mergeCodexRateLimitsUpdate: mocks.mergeRateLimitUpdate,
|
||||
}));
|
||||
|
||||
const { ensureCodexAppServerClientRuntime } = await import("./client-runtime.js");
|
||||
|
||||
describe("Codex app-server client runtime", () => {
|
||||
const clients: CodexAppServerClient[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const client of clients) {
|
||||
client.close();
|
||||
}
|
||||
clients.length = 0;
|
||||
mocks.refreshAuth.mockClear();
|
||||
mocks.mergeRateLimitUpdate.mockClear();
|
||||
});
|
||||
|
||||
it("installs shared handlers once per physical client", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
const context = {
|
||||
agentDir: "/tmp/agent",
|
||||
authProfileId: "openai:default",
|
||||
config: {},
|
||||
};
|
||||
const updatedContext = {
|
||||
...context,
|
||||
authProfileStore: { version: 1 as const, profiles: {} },
|
||||
config: { models: { mode: "merge" as const } },
|
||||
};
|
||||
const addNotificationHandler = vi.spyOn(harness.client, "addNotificationHandler");
|
||||
const addRequestHandler = vi.spyOn(harness.client, "addRequestHandler");
|
||||
const addCloseHandler = vi.spyOn(harness.client, "addCloseHandler");
|
||||
|
||||
ensureCodexAppServerClientRuntime(harness.client, context);
|
||||
ensureCodexAppServerClientRuntime(harness.client, updatedContext);
|
||||
|
||||
expect(addNotificationHandler).toHaveBeenCalledTimes(1);
|
||||
expect(addRequestHandler).toHaveBeenCalledTimes(1);
|
||||
expect(addCloseHandler).not.toHaveBeenCalled();
|
||||
harness.send({
|
||||
method: "account/rateLimits/updated",
|
||||
params: { rateLimits: { primary: { usedPercent: 12 } } },
|
||||
});
|
||||
harness.send({
|
||||
id: "refresh-1",
|
||||
method: "account/chatgptAuthTokens/refresh",
|
||||
params: { reason: "expired" },
|
||||
});
|
||||
|
||||
await vi.waitFor(() => expect(mocks.mergeRateLimitUpdate).toHaveBeenCalledTimes(1));
|
||||
await vi.waitFor(() => expect(mocks.refreshAuth).toHaveBeenCalledTimes(1));
|
||||
expect(mocks.refreshAuth).toHaveBeenCalledWith(updatedContext);
|
||||
expect(mocks.mergeRateLimitUpdate).toHaveBeenCalledWith(harness.client, {
|
||||
rateLimits: { primary: { usedPercent: 12 } },
|
||||
});
|
||||
await vi.waitFor(() =>
|
||||
expect(harness.writes.map((line) => JSON.parse(line) as unknown)).toContainEqual({
|
||||
id: "refresh-1",
|
||||
result: { accessToken: "refreshed", chatgptAccountId: "account" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,50 +0,0 @@
|
||||
/** Client-scoped Codex auth and account observers. */
|
||||
import { refreshCodexAppServerAuthTokens } from "./auth-bridge.js";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import type { JsonValue } from "./protocol.js";
|
||||
import { mergeCodexRateLimitsUpdate } from "./rate-limit-cache.js";
|
||||
import type { CodexAppServerAuthProfileLookup } from "./session-binding.js";
|
||||
|
||||
type ClientRuntimeContext = Omit<CodexAppServerAuthProfileLookup, "agentDir"> & {
|
||||
agentDir: string;
|
||||
};
|
||||
|
||||
type ClientRuntime = {
|
||||
context: ClientRuntimeContext;
|
||||
};
|
||||
|
||||
const configuredClients = new WeakMap<CodexAppServerClient, ClientRuntime>();
|
||||
|
||||
/** Installs one auth-refresh handler and one rate-limit observer per physical client. */
|
||||
export function ensureCodexAppServerClientRuntime(
|
||||
client: CodexAppServerClient,
|
||||
context: ClientRuntimeContext,
|
||||
): void {
|
||||
const existing = configuredClients.get(client);
|
||||
if (existing) {
|
||||
// Shared-client keys already isolate agent/auth identity. Keep config fresh
|
||||
// without installing another physical-client handler set.
|
||||
existing.context = context;
|
||||
return;
|
||||
}
|
||||
const runtime: ClientRuntime = { context };
|
||||
configuredClients.set(client, runtime);
|
||||
client.addRequestHandler(async (request) => {
|
||||
if (request.method !== "account/chatgptAuthTokens/refresh") {
|
||||
return undefined;
|
||||
}
|
||||
return (await refreshCodexAppServerAuthTokens({
|
||||
agentDir: runtime.context.agentDir,
|
||||
authProfileId: runtime.context.authProfileId,
|
||||
...(runtime.context.authProfileStore
|
||||
? { authProfileStore: runtime.context.authProfileStore }
|
||||
: {}),
|
||||
config: runtime.context.config,
|
||||
})) as unknown as JsonValue;
|
||||
});
|
||||
client.addNotificationHandler((notification) => {
|
||||
if (notification.method === "account/rateLimits/updated") {
|
||||
mergeCodexRateLimitsUpdate(client, notification.params);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -50,78 +50,6 @@ describe("CodexAppServerClient", () => {
|
||||
expect(outbound.method).toBe("model/list");
|
||||
});
|
||||
|
||||
it("keeps a shared thread subscribed until every local owner releases it", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
|
||||
const firstResume = harness.client.request("thread/resume", { threadId: "thread-1" });
|
||||
const secondResume = harness.client.request("thread/resume", { threadId: "thread-1" });
|
||||
const [firstRequest, secondRequest] = harness.writes.map((line) => JSON.parse(line)) as Array<{
|
||||
id: number;
|
||||
}>;
|
||||
const resumeResult = {
|
||||
thread: { id: "thread-1", cwd: "/tmp", status: { type: "idle" } },
|
||||
model: "gpt-5.5",
|
||||
};
|
||||
harness.send({ id: firstRequest?.id, result: resumeResult });
|
||||
harness.send({ id: secondRequest?.id, result: resumeResult });
|
||||
await Promise.all([firstResume, secondResume]);
|
||||
|
||||
await expect(
|
||||
harness.client.request("thread/unsubscribe", { threadId: "thread-1" }),
|
||||
).resolves.toEqual({ status: "unsubscribed" });
|
||||
expect(harness.writes).toHaveLength(2);
|
||||
|
||||
const finalRelease = harness.client.request("thread/unsubscribe", {
|
||||
threadId: "thread-1",
|
||||
});
|
||||
const releaseRequest = JSON.parse(harness.writes[2] ?? "{}") as { id?: number };
|
||||
harness.send({ id: releaseRequest.id, result: { status: "unsubscribed" } });
|
||||
await expect(finalRelease).resolves.toEqual({ status: "unsubscribed" });
|
||||
expect(harness.writes).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("pairs written resume failures without retaining pre-aborted requests", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
|
||||
const firstResume = harness.client.request("thread/resume", { threadId: "thread-1" });
|
||||
const firstRequest = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
|
||||
harness.send({
|
||||
id: firstRequest.id,
|
||||
result: {
|
||||
thread: { id: "thread-1", cwd: "/tmp", status: { type: "idle" } },
|
||||
model: "gpt-5.5",
|
||||
},
|
||||
});
|
||||
await firstResume;
|
||||
|
||||
const failedResume = harness.client.request("thread/resume", { threadId: "thread-1" });
|
||||
const failedRequest = JSON.parse(harness.writes[1] ?? "{}") as { id?: number };
|
||||
harness.send({ id: failedRequest.id, error: { code: -32000, message: "resume failed" } });
|
||||
await expect(failedResume).rejects.toThrow("resume failed");
|
||||
|
||||
await expect(
|
||||
harness.client.request("thread/unsubscribe", { threadId: "thread-1" }),
|
||||
).resolves.toEqual({ status: "unsubscribed" });
|
||||
expect(harness.writes).toHaveLength(2);
|
||||
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
await expect(
|
||||
harness.client.request(
|
||||
"thread/resume",
|
||||
{ threadId: "thread-1" },
|
||||
{ signal: controller.signal },
|
||||
),
|
||||
).rejects.toThrow("thread/resume aborted");
|
||||
const unsubscribe = harness.client.request("thread/unsubscribe", { threadId: "thread-1" });
|
||||
expect(harness.writes).toHaveLength(3);
|
||||
const unsubscribeRequest = JSON.parse(harness.writes[2] ?? "{}") as { id?: number };
|
||||
harness.send({ id: unsubscribeRequest.id, result: { status: "unsubscribed" } });
|
||||
await expect(unsubscribe).resolves.toEqual({ status: "unsubscribed" });
|
||||
});
|
||||
|
||||
it("removes unpaired surrogate code units from outbound JSON-RPC strings", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
@@ -142,9 +70,9 @@ describe("CodexAppServerClient", () => {
|
||||
expect(outbound.params?.nested).toEqual(["lowend", "emoji 🙈 ok"]);
|
||||
harness.send({
|
||||
id: JSON.parse(harness.writes[0] ?? "{}").id,
|
||||
result: { thread: { id: "thread-1" } },
|
||||
result: { threadId: "thread-1" },
|
||||
});
|
||||
await expect(request).resolves.toEqual({ thread: { id: "thread-1" } });
|
||||
await expect(request).resolves.toEqual({ threadId: "thread-1" });
|
||||
});
|
||||
|
||||
it("logs a redacted preview for malformed app-server messages", async () => {
|
||||
@@ -212,30 +140,6 @@ describe("CodexAppServerClient", () => {
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("contains synchronous notification handler failures and continues fanout", async () => {
|
||||
const warn = vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
const laterHandler = vi.fn();
|
||||
harness.client.addNotificationHandler(() => {
|
||||
throw new Error("handler exploded");
|
||||
});
|
||||
harness.client.addNotificationHandler(laterHandler);
|
||||
|
||||
expect(() =>
|
||||
harness.send({
|
||||
method: "item/commandExecution/outputDelta",
|
||||
params: { delta: "still routed" },
|
||||
}),
|
||||
).not.toThrow();
|
||||
|
||||
await vi.waitFor(() => expect(laterHandler).toHaveBeenCalledTimes(1));
|
||||
expect(warn).toHaveBeenCalledWith(
|
||||
"codex app-server notification handler failed",
|
||||
expect.objectContaining({ error: expect.any(Error) }),
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves JSON-RPC error codes", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
@@ -316,95 +220,6 @@ describe("CodexAppServerClient", () => {
|
||||
expect(harness.writes).toHaveLength(1);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
method: "thread/start" as const,
|
||||
params: {},
|
||||
abandonment: "timeout" as const,
|
||||
expectedError: "thread/start timed out",
|
||||
},
|
||||
{
|
||||
method: "thread/fork" as const,
|
||||
params: { threadId: "parent-thread" },
|
||||
abandonment: "abort" as const,
|
||||
expectedError: "thread/fork aborted",
|
||||
},
|
||||
])("unsubscribes a late successful $method after local $abandonment", async (testCase) => {
|
||||
vi.useFakeTimers();
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
const controller = new AbortController();
|
||||
const options =
|
||||
testCase.abandonment === "timeout" ? { timeoutMs: 1 } : { signal: controller.signal };
|
||||
const request = harness.client.request(testCase.method, testCase.params, options);
|
||||
const outbound = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
|
||||
const rejected = expect(request).rejects.toThrow(testCase.expectedError);
|
||||
|
||||
if (testCase.abandonment === "timeout") {
|
||||
await vi.advanceTimersByTimeAsync(100);
|
||||
} else {
|
||||
controller.abort();
|
||||
}
|
||||
await rejected;
|
||||
|
||||
harness.send({ id: outbound.id, result: { thread: { id: "late-thread" } } });
|
||||
expect(JSON.parse(harness.writes[1] ?? "{}")).toEqual({
|
||||
id: expect.any(Number),
|
||||
method: "thread/unsubscribe",
|
||||
params: { threadId: "late-thread" },
|
||||
});
|
||||
});
|
||||
|
||||
it("closes when a late thread creation subscription cannot be released", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
const controller = new AbortController();
|
||||
const request = harness.client.request("thread/start", {}, { signal: controller.signal });
|
||||
const outbound = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
|
||||
const rejected = expect(request).rejects.toThrow("thread/start aborted");
|
||||
|
||||
controller.abort();
|
||||
await rejected;
|
||||
harness.send({ id: outbound.id, result: { thread: { id: "late-thread" } } });
|
||||
const unsubscribe = JSON.parse(harness.writes[1] ?? "{}") as { id?: number };
|
||||
harness.send({
|
||||
id: unsubscribe.id,
|
||||
error: { code: -32_000, message: "unsubscribe failed" },
|
||||
});
|
||||
|
||||
await vi.waitFor(() => expect(harness.stdinDestroyed).toBe(true));
|
||||
});
|
||||
|
||||
it("does not unsubscribe a late rejected thread creation", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
const controller = new AbortController();
|
||||
const request = harness.client.request("thread/start", {}, { signal: controller.signal });
|
||||
const outbound = JSON.parse(harness.writes[0] ?? "{}") as { id?: number };
|
||||
const rejected = expect(request).rejects.toThrow("thread/start aborted");
|
||||
|
||||
controller.abort();
|
||||
await rejected;
|
||||
harness.send({ id: outbound.id, error: { code: -32000, message: "start failed" } });
|
||||
|
||||
expect(harness.writes).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("closes after the bounded late-creation cleanup ledger fills", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
|
||||
for (let index = 0; index < 129; index += 1) {
|
||||
const controller = new AbortController();
|
||||
const request = harness.client.request("thread/start", {}, { signal: controller.signal });
|
||||
const rejected = expect(request).rejects.toThrow("thread/start aborted");
|
||||
controller.abort();
|
||||
await rejected;
|
||||
}
|
||||
|
||||
expect(harness.stdinDestroyed).toBe(true);
|
||||
});
|
||||
|
||||
it("initializes with the required client version", async () => {
|
||||
const { harness, initializing, outbound } = startInitialize();
|
||||
harness.send({
|
||||
@@ -701,26 +516,6 @@ describe("CodexAppServerClient", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["execCommandApproval", "applyPatchApproval"])(
|
||||
"fails closed for unhandled legacy %s requests",
|
||||
async (method) => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
|
||||
harness.send({
|
||||
id: "legacy-approval-1",
|
||||
method,
|
||||
params: { conversationId: "thread-1" },
|
||||
});
|
||||
await vi.waitFor(() => expect(harness.writes.length).toBe(1));
|
||||
|
||||
expect(JSON.parse(harness.writes[0] ?? "{}")).toEqual({
|
||||
id: "legacy-approval-1",
|
||||
result: { decision: "denied" },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("fails closed for unhandled native app-server approvals", async () => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
@@ -738,41 +533,6 @@ describe("CodexAppServerClient", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
"item/tool/call",
|
||||
{
|
||||
contentItems: [
|
||||
{
|
||||
type: "inputText",
|
||||
text: "OpenClaw did not register a handler for this app-server tool call.",
|
||||
},
|
||||
],
|
||||
success: false,
|
||||
},
|
||||
],
|
||||
["item/permissions/requestApproval", { permissions: {}, scope: "turn" }],
|
||||
["mcpServer/elicitation/request", { action: "decline" }],
|
||||
[
|
||||
"item/future/requestApproval",
|
||||
{
|
||||
decision: "decline",
|
||||
reason: "OpenClaw codex app-server bridge does not grant unknown native approvals.",
|
||||
},
|
||||
],
|
||||
])("fails closed for an unhandled %s request", async (method, expected) => {
|
||||
const harness = createClientHarness();
|
||||
clients.push(harness.client);
|
||||
|
||||
harness.send({ id: "unhandled-1", method, params: { threadId: "thread-1" } });
|
||||
await vi.waitFor(() => expect(harness.writes.length).toBe(1));
|
||||
|
||||
expect(JSON.parse(harness.writes[0] ?? "{}")).toEqual({
|
||||
id: "unhandled-1",
|
||||
result: expected,
|
||||
});
|
||||
});
|
||||
|
||||
it("only treats known Codex app-server approval methods as approvals", () => {
|
||||
expect(isCodexAppServerApprovalRequest("item/commandExecution/requestApproval")).toBe(true);
|
||||
expect(isCodexAppServerApprovalRequest("item/fileChange/requestApproval")).toBe(true);
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
type CodexInitializeParams,
|
||||
type CodexInitializeResponse,
|
||||
isRpcResponse,
|
||||
readCodexThreadCreationResponseId,
|
||||
type CodexServerNotification,
|
||||
type JsonValue,
|
||||
type RpcMessage,
|
||||
@@ -35,8 +34,6 @@ const CODEX_APP_SERVER_PARSE_BUFFER_MAX = 1_000_000;
|
||||
const CODEX_APP_SERVER_PARSE_BUFFER_MAX_LINES = 1_000;
|
||||
const CODEX_DYNAMIC_TOOL_SERVER_REQUEST_TIMEOUT_MS = 600_000;
|
||||
const CODEX_APP_SERVER_STDERR_TAIL_MAX = 2_000;
|
||||
const CODEX_APP_SERVER_ABANDONED_THREAD_CREATION_MAX = 128;
|
||||
const CODEX_APP_SERVER_LATE_THREAD_CLEANUP_TIMEOUT_MS = 5_000;
|
||||
const UNPAIRED_SURROGATE_RE =
|
||||
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
|
||||
|
||||
@@ -114,10 +111,7 @@ export class CodexAppServerClient {
|
||||
private readonly requestHandlers = new Set<CodexServerRequestHandler>();
|
||||
private readonly notificationHandlers = new Set<CodexServerNotificationHandler>();
|
||||
private readonly closeHandlers = new Set<(client: CodexAppServerClient) => void>();
|
||||
private readonly threadSubscriptionOwners = new Map<string, number>();
|
||||
// Codex may finish a locally abandoned create request. Remember its RPC id
|
||||
// until response/close so the unknown thread subscription can be released.
|
||||
private readonly abandonedThreadCreationRequestIds = new Set<number | string>();
|
||||
private activeSharedLeaseCountProvider: (() => number | undefined) | undefined;
|
||||
private nextId = 1;
|
||||
private initialized = false;
|
||||
private closed = false;
|
||||
@@ -231,27 +225,11 @@ export class CodexAppServerClient {
|
||||
if (options.signal?.aborted) {
|
||||
return Promise.reject(new Error(`${method} aborted`));
|
||||
}
|
||||
const requestedThreadId = readRequestThreadId(params);
|
||||
if (
|
||||
method === "thread/unsubscribe" &&
|
||||
requestedThreadId &&
|
||||
this.releaseThreadSubscriptionOwner(requestedThreadId)
|
||||
) {
|
||||
// Codex subscriptions are connection-wide sets. A logical owner can
|
||||
// release without silencing another turn on the same physical client.
|
||||
return Promise.resolve({ status: "unsubscribed" } as unknown as T);
|
||||
}
|
||||
if (method === "thread/resume" && requestedThreadId) {
|
||||
// Every resume attempt owns one release, even if the response times out
|
||||
// or aborts: Codex may have subscribed before OpenClaw saw the outcome.
|
||||
this.retainThreadSubscriptionOwner(requestedThreadId);
|
||||
}
|
||||
const id = this.nextId++;
|
||||
const message: RpcRequest = { id, method, params: params as JsonValue | undefined };
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined;
|
||||
let cleanupAbort: (() => void) | undefined;
|
||||
let requestWritten = false;
|
||||
const cleanup = () => {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
@@ -260,37 +238,23 @@ export class CodexAppServerClient {
|
||||
cleanupAbort?.();
|
||||
cleanupAbort = undefined;
|
||||
};
|
||||
const rejectPending = (error: Error, rememberLateThreadCreation = false) => {
|
||||
const rejectPending = (error: Error) => {
|
||||
if (!this.pending.has(id)) {
|
||||
return;
|
||||
}
|
||||
this.pending.delete(id);
|
||||
if (rememberLateThreadCreation && isThreadCreationRequest(method)) {
|
||||
if (
|
||||
this.abandonedThreadCreationRequestIds.size >=
|
||||
CODEX_APP_SERVER_ABANDONED_THREAD_CREATION_MAX
|
||||
) {
|
||||
// Lost create responses can hide server subscriptions. Once the
|
||||
// bounded cleanup ledger fills, closing is the only safe release.
|
||||
this.closeWithError(
|
||||
new Error("codex app-server abandoned thread creation limit exceeded"),
|
||||
);
|
||||
} else {
|
||||
this.abandonedThreadCreationRequestIds.add(id);
|
||||
}
|
||||
}
|
||||
cleanup();
|
||||
reject(error);
|
||||
};
|
||||
if (options.timeoutMs && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0) {
|
||||
timeout = setTimeout(
|
||||
() => rejectPending(new Error(`${method} timed out`), true),
|
||||
() => rejectPending(new Error(`${method} timed out`)),
|
||||
Math.max(100, options.timeoutMs),
|
||||
);
|
||||
timeout.unref?.();
|
||||
}
|
||||
if (options.signal) {
|
||||
const abortListener = () => rejectPending(new Error(`${method} aborted`), requestWritten);
|
||||
const abortListener = () => rejectPending(new Error(`${method} aborted`));
|
||||
options.signal.addEventListener("abort", abortListener, { once: true });
|
||||
cleanupAbort = () => options.signal?.removeEventListener("abort", abortListener);
|
||||
}
|
||||
@@ -298,12 +262,6 @@ export class CodexAppServerClient {
|
||||
method,
|
||||
resolve: (value) => {
|
||||
cleanup();
|
||||
if (method === "thread/start" || method === "thread/fork") {
|
||||
const threadId = readCodexThreadCreationResponseId(value);
|
||||
if (threadId) {
|
||||
this.retainThreadSubscriptionOwner(threadId);
|
||||
}
|
||||
}
|
||||
resolve(value as T);
|
||||
},
|
||||
reject: (error) => {
|
||||
@@ -317,7 +275,6 @@ export class CodexAppServerClient {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
requestWritten = true;
|
||||
this.writeMessage(message, (error) => rejectPending(error));
|
||||
} catch (error) {
|
||||
rejectPending(error instanceof Error ? error : new Error(String(error)));
|
||||
@@ -342,6 +299,18 @@ export class CodexAppServerClient {
|
||||
return () => this.notificationHandlers.delete(handler);
|
||||
}
|
||||
|
||||
/** Installs a lease-count provider used to route unscoped notifications. */
|
||||
setActiveSharedLeaseCountProviderForUnscopedNotifications(
|
||||
provider: (() => number | undefined) | undefined,
|
||||
): void {
|
||||
this.activeSharedLeaseCountProvider = provider;
|
||||
}
|
||||
|
||||
/** Reads the active shared-client lease count when available. */
|
||||
getActiveSharedLeaseCountForUnscopedNotifications(): number | undefined {
|
||||
return this.activeSharedLeaseCountProvider?.();
|
||||
}
|
||||
|
||||
/** Registers a close handler and returns its disposer. */
|
||||
addCloseHandler(handler: (client: CodexAppServerClient) => void): () => void {
|
||||
this.closeHandlers.add(handler);
|
||||
@@ -460,15 +429,6 @@ export class CodexAppServerClient {
|
||||
}
|
||||
|
||||
private handleResponse(response: RpcResponse): void {
|
||||
if (this.abandonedThreadCreationRequestIds.delete(response.id)) {
|
||||
if (!response.error) {
|
||||
const threadId = readCodexThreadCreationResponseId(response.result);
|
||||
if (threadId) {
|
||||
this.unsubscribeLateThreadCreation(threadId);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const pending = this.pending.get(response.id);
|
||||
if (!pending) {
|
||||
return;
|
||||
@@ -546,14 +506,7 @@ export class CodexAppServerClient {
|
||||
|
||||
private handleNotification(notification: CodexServerNotification): void {
|
||||
for (const handler of this.notificationHandlers) {
|
||||
let result: Promise<void> | void;
|
||||
try {
|
||||
result = handler(notification);
|
||||
} catch (error) {
|
||||
embeddedAgentLog.warn("codex app-server notification handler failed", { error });
|
||||
continue;
|
||||
}
|
||||
Promise.resolve(result).catch((error: unknown) => {
|
||||
Promise.resolve(handler(notification)).catch((error: unknown) => {
|
||||
embeddedAgentLog.warn("codex app-server notification handler failed", { error });
|
||||
});
|
||||
}
|
||||
@@ -571,54 +524,11 @@ export class CodexAppServerClient {
|
||||
}
|
||||
this.closed = true;
|
||||
this.closeError = error;
|
||||
this.threadSubscriptionOwners.clear();
|
||||
this.abandonedThreadCreationRequestIds.clear();
|
||||
this.lines.close();
|
||||
this.rejectPendingRequests(error);
|
||||
return true;
|
||||
}
|
||||
|
||||
private unsubscribeLateThreadCreation(threadId: string): void {
|
||||
// This late response never registered a local owner. Track the wire
|
||||
// release anyway; an unconfirmed cleanup makes this client unsafe to pool.
|
||||
void this.request(
|
||||
"thread/unsubscribe",
|
||||
{ threadId },
|
||||
{ timeoutMs: CODEX_APP_SERVER_LATE_THREAD_CLEANUP_TIMEOUT_MS },
|
||||
).catch((error: unknown) => {
|
||||
embeddedAgentLog.debug("codex app-server late thread unsubscribe failed", {
|
||||
threadId,
|
||||
error,
|
||||
});
|
||||
this.closeWithError(
|
||||
new Error(`Codex late thread subscription could not be released: ${threadId}`, {
|
||||
cause: error,
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private retainThreadSubscriptionOwner(threadId: string): void {
|
||||
this.threadSubscriptionOwners.set(
|
||||
threadId,
|
||||
(this.threadSubscriptionOwners.get(threadId) ?? 0) + 1,
|
||||
);
|
||||
}
|
||||
|
||||
/** Returns true when another local owner still needs the wire subscription. */
|
||||
private releaseThreadSubscriptionOwner(threadId: string): boolean {
|
||||
const owners = this.threadSubscriptionOwners.get(threadId);
|
||||
if (owners === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (owners > 1) {
|
||||
this.threadSubscriptionOwners.set(threadId, owners - 1);
|
||||
return true;
|
||||
}
|
||||
this.threadSubscriptionOwners.delete(threadId);
|
||||
return false;
|
||||
}
|
||||
|
||||
private rejectPendingRequests(error: Error): void {
|
||||
for (const pending of this.pending.values()) {
|
||||
pending.cleanup();
|
||||
@@ -631,17 +541,6 @@ export class CodexAppServerClient {
|
||||
}
|
||||
}
|
||||
|
||||
function readRequestThreadId(value: unknown): string | undefined {
|
||||
if (!isJsonObject(value) || typeof value.threadId !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
return value.threadId.trim() || undefined;
|
||||
}
|
||||
|
||||
function isThreadCreationRequest(method: string): boolean {
|
||||
return method === "thread/start" || method === "thread/fork";
|
||||
}
|
||||
|
||||
function defaultServerRequestResponse(
|
||||
request: Required<Pick<RpcRequest, "id" | "method">> & { params?: JsonValue },
|
||||
): JsonValue {
|
||||
@@ -656,9 +555,6 @@ function defaultServerRequestResponse(
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
if (request.method === "execCommandApproval" || request.method === "applyPatchApproval") {
|
||||
return { decision: "denied" };
|
||||
}
|
||||
if (
|
||||
request.method === "item/commandExecution/requestApproval" ||
|
||||
request.method === "item/fileChange/requestApproval"
|
||||
@@ -674,12 +570,6 @@ function defaultServerRequestResponse(
|
||||
reason: "OpenClaw codex app-server bridge does not grant native approvals yet.",
|
||||
};
|
||||
}
|
||||
if (request.method.includes("requestApproval")) {
|
||||
return {
|
||||
decision: "decline",
|
||||
reason: "OpenClaw codex app-server bridge does not grant unknown native approvals.",
|
||||
};
|
||||
}
|
||||
if (request.method === "item/tool/requestUserInput") {
|
||||
return {
|
||||
answers: {},
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,396 +7,145 @@ import {
|
||||
type EmbeddedAgentCompactResult,
|
||||
} from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import {
|
||||
CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
isCodexAppServerUnsafeSubscriptionError,
|
||||
settleCodexAppServerClientLease,
|
||||
} from "./attempt-client-cleanup.js";
|
||||
import { readCodexNotificationItem } from "./attempt-notifications.js";
|
||||
import { resolveCodexTurnTerminalIdleTimeoutMs } from "./attempt-timeouts.js";
|
||||
import { CodexAppServerRpcError } from "./client.js";
|
||||
defaultLeasedCodexAppServerClientFactory,
|
||||
type CodexAppServerClientFactory,
|
||||
} from "./client-factory.js";
|
||||
import { resolveCodexAppServerRuntimeOptions } from "./config.js";
|
||||
import { isJsonObject, type JsonObject, type JsonValue } from "./protocol.js";
|
||||
import type { JsonObject } from "./protocol.js";
|
||||
import { resolveCodexNativeExecutionBlock } from "./sandbox-guard.js";
|
||||
import {
|
||||
CODEX_APP_SERVER_BINDING_GUARDED_REQUEST_TIMEOUT_MS,
|
||||
sessionBindingIdentity,
|
||||
type CodexAppServerBindingIdentity,
|
||||
type CodexAppServerBindingStore,
|
||||
readCodexAppServerBinding,
|
||||
withCodexAppServerBindingLock,
|
||||
writeCodexAppServerBinding,
|
||||
type CodexAppServerThreadBinding,
|
||||
} from "./session-binding.js";
|
||||
import {
|
||||
leaseSharedCodexAppServerClient,
|
||||
type CodexAppServerClientLease,
|
||||
type CodexAppServerClientLeaseFactory,
|
||||
type CodexAppServerClientOptions,
|
||||
} from "./shared-client.js";
|
||||
import { resumeCodexAppServerThread } from "./thread-resume.js";
|
||||
import { withTimeout } from "./timeout.js";
|
||||
import {
|
||||
getCodexAppServerTurnRouter,
|
||||
isCodexTerminalTurnNotification,
|
||||
type CodexNativeTurnCompletionWatch,
|
||||
type CodexThreadRouteReservation,
|
||||
} from "./turn-router.js";
|
||||
import { releaseLeasedSharedCodexAppServerClient } from "./shared-client.js";
|
||||
|
||||
const warnedIgnoredCompactionOverrides = new Set<string>();
|
||||
type CodexAppServerCompactOptions = {
|
||||
bindingStore: CodexAppServerBindingStore;
|
||||
pluginConfig?: unknown;
|
||||
clientLeaseFactory?: CodexAppServerClientLeaseFactory;
|
||||
clientFactory?: CodexAppServerClientFactory;
|
||||
allowNonManualNativeRequest?: boolean;
|
||||
};
|
||||
|
||||
class CodexNativeTurnBindingChangedError extends Error {}
|
||||
|
||||
type CodexNativeTurnRequest = {
|
||||
bindingStore: CodexAppServerBindingStore;
|
||||
bindingIdentity: CodexAppServerBindingIdentity;
|
||||
expectedBinding: CodexAppServerThreadBinding;
|
||||
pluginConfig?: unknown;
|
||||
authProfileId?: string;
|
||||
agentDir?: string;
|
||||
config?: CodexAppServerClientOptions["config"];
|
||||
abortSignal?: AbortSignal;
|
||||
clientLeaseFactory?: CodexAppServerClientLeaseFactory;
|
||||
};
|
||||
|
||||
export type CodexNativeTurnKind = "compact" | "review";
|
||||
|
||||
/** Starts one native Codex turn and retains its app-server owner through completion. */
|
||||
export async function requestCodexNativeTurnForBinding(
|
||||
params: CodexNativeTurnRequest,
|
||||
kind: CodexNativeTurnKind,
|
||||
): Promise<void> {
|
||||
const isCompaction = kind === "compact";
|
||||
const label = isCompaction ? "compaction" : "review";
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig: params.pluginConfig });
|
||||
const requestTimeoutMs = Math.min(
|
||||
appServer.requestTimeoutMs,
|
||||
CODEX_APP_SERVER_BINDING_GUARDED_REQUEST_TIMEOUT_MS,
|
||||
);
|
||||
await params.bindingStore.withLease(params.bindingIdentity, async () => {
|
||||
const currentBinding = await params.bindingStore.read(params.bindingIdentity);
|
||||
if (!currentBinding || !isSameNativeTurnBinding(currentBinding, params.expectedBinding)) {
|
||||
throw new CodexNativeTurnBindingChangedError(
|
||||
`Codex thread binding changed before native ${label}`,
|
||||
);
|
||||
}
|
||||
const clientLease = await (params.clientLeaseFactory ?? leaseSharedCodexAppServerClient)({
|
||||
startOptions: appServer.start,
|
||||
authProfileId: params.authProfileId ?? currentBinding.authProfileId,
|
||||
agentDir: params.agentDir,
|
||||
config: params.config,
|
||||
abandonSignal: params.abortSignal,
|
||||
timeoutMs: appServer.requestTimeoutMs,
|
||||
});
|
||||
const client = clientLease.client;
|
||||
let subscribedThreadId: string | undefined;
|
||||
let abandonClient = false;
|
||||
let lifecycleTransferred = false;
|
||||
let awaitingNativeTurnStart = false;
|
||||
const terminalTurnsBeforeWatch = new Set<string>();
|
||||
let route: CodexThreadRouteReservation | undefined;
|
||||
let completionWatch: CodexNativeTurnCompletionWatch | undefined;
|
||||
let observedContextCompaction = false;
|
||||
let bindingInvalidated = false;
|
||||
let resolveNativeTurnStarted!: () => void;
|
||||
const nativeTurnStarted = new Promise<void>((resolve) => {
|
||||
resolveNativeTurnStarted = resolve;
|
||||
});
|
||||
try {
|
||||
const router = getCodexAppServerTurnRouter(client);
|
||||
route = router.reserveThread({
|
||||
threadId: currentBinding.threadId,
|
||||
onNotificationReceived: (notification, scope) => {
|
||||
const contextCompactionStarted =
|
||||
isCompaction &&
|
||||
Boolean(scope.turnId) &&
|
||||
notification.method === "item/started" &&
|
||||
readCodexNotificationItem(notification.params)?.type === "contextCompaction";
|
||||
if (contextCompactionStarted) {
|
||||
observedContextCompaction = true;
|
||||
}
|
||||
if (!awaitingNativeTurnStart || !scope.turnId) {
|
||||
return;
|
||||
}
|
||||
if (isCodexTerminalTurnNotification(notification)) {
|
||||
terminalTurnsBeforeWatch.add(scope.turnId);
|
||||
}
|
||||
if (contextCompactionStarted) {
|
||||
completionWatch ??= router.watchNativeTurnCompletion({
|
||||
threadId: currentBinding.threadId,
|
||||
turnId: scope.turnId,
|
||||
timeoutMs: resolveCodexTurnTerminalIdleTimeoutMs(undefined),
|
||||
});
|
||||
resolveNativeTurnStarted();
|
||||
}
|
||||
},
|
||||
onNotification: () => undefined,
|
||||
});
|
||||
throwIfCodexNativeTurnAborted(params.abortSignal, kind);
|
||||
let resumed;
|
||||
try {
|
||||
subscribedThreadId = currentBinding.threadId;
|
||||
resumed = await resumeCodexAppServerThread({
|
||||
client,
|
||||
abandonClient: clientLease.abandon,
|
||||
request: {
|
||||
threadId: currentBinding.threadId,
|
||||
excludeTurns: true,
|
||||
persistExtendedHistory: true,
|
||||
},
|
||||
timeoutMs: requestTimeoutMs,
|
||||
signal: params.abortSignal,
|
||||
});
|
||||
} catch (error) {
|
||||
abandonClient = isCodexAppServerUnsafeSubscriptionError(error);
|
||||
throw error;
|
||||
}
|
||||
const invalidateNativeContextBinding = async () => {
|
||||
if (bindingInvalidated) {
|
||||
return;
|
||||
}
|
||||
const invalidated = await params.bindingStore.mutate(params.bindingIdentity, {
|
||||
kind: "invalidate-native-context",
|
||||
threadId: currentBinding.threadId,
|
||||
...(isCompaction ? { invalidateContextEngineProjection: true as const } : {}),
|
||||
});
|
||||
if (!invalidated) {
|
||||
throw new CodexNativeTurnBindingChangedError(
|
||||
`Codex thread binding changed before native ${label}`,
|
||||
);
|
||||
}
|
||||
bindingInvalidated = true;
|
||||
};
|
||||
if (isCompaction && observedContextCompaction) {
|
||||
await invalidateNativeContextBinding();
|
||||
}
|
||||
if (resumed.thread.status?.type === "active") {
|
||||
throw new Error(
|
||||
`Codex thread already has an active turn; retry ${label} after it finishes`,
|
||||
);
|
||||
}
|
||||
throwIfCodexNativeTurnAborted(params.abortSignal, kind);
|
||||
await invalidateNativeContextBinding();
|
||||
awaitingNativeTurnStart = true;
|
||||
let requestResult: JsonValue | undefined;
|
||||
try {
|
||||
requestResult = await client.request(
|
||||
isCompaction ? "thread/compact/start" : "review/start",
|
||||
isCompaction
|
||||
? { threadId: currentBinding.threadId }
|
||||
: { threadId: currentBinding.threadId, target: { type: "uncommittedChanges" } },
|
||||
{ timeoutMs: requestTimeoutMs },
|
||||
);
|
||||
} catch (error) {
|
||||
const requestRejected = error instanceof CodexAppServerRpcError;
|
||||
if (requestRejected) {
|
||||
// A structured rejection proves this request did not start a native
|
||||
// turn. Preserve only compaction already observed on the same thread.
|
||||
completionWatch?.cancel();
|
||||
completionWatch = undefined;
|
||||
if (!isCompaction || !observedContextCompaction) {
|
||||
const restored = await params.bindingStore.mutate(params.bindingIdentity, {
|
||||
kind: "set",
|
||||
binding: currentBinding,
|
||||
});
|
||||
if (!restored) {
|
||||
throw new Error(`Codex thread binding changed after native ${label} was rejected`, {
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (completionWatch) {
|
||||
embeddedAgentLog.debug(`codex app-server ${kind} request failed after startup`, {
|
||||
threadId: currentBinding.threadId,
|
||||
error,
|
||||
});
|
||||
} else {
|
||||
abandonClient = true;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
if (!isCompaction) {
|
||||
try {
|
||||
const review = assertCodexReviewStartResponse(requestResult);
|
||||
if (review.reviewThreadId !== currentBinding.threadId) {
|
||||
throw new Error(
|
||||
`Codex review/start returned ${review.reviewThreadId} for inline review on ${currentBinding.threadId}`,
|
||||
);
|
||||
}
|
||||
completionWatch = terminalTurnsBeforeWatch.has(review.turnId)
|
||||
? { completion: Promise.resolve(true), cancel: () => undefined }
|
||||
: router.watchNativeTurnCompletion({
|
||||
threadId: currentBinding.threadId,
|
||||
turnId: review.turnId,
|
||||
timeoutMs: resolveCodexTurnTerminalIdleTimeoutMs(undefined),
|
||||
});
|
||||
} catch (error) {
|
||||
abandonClient = true;
|
||||
throw error;
|
||||
}
|
||||
} else if (!completionWatch) {
|
||||
try {
|
||||
await waitForCodexNativeTurnStart({
|
||||
started: nativeTurnStarted,
|
||||
routeSignal: route.signal,
|
||||
timeoutMs: requestTimeoutMs,
|
||||
threadId: currentBinding.threadId,
|
||||
kind,
|
||||
});
|
||||
} catch (error) {
|
||||
// Codex accepted Op::Compact, so missing startup confirmation is
|
||||
// ambiguous. Keep facts invalidated and retire this connection.
|
||||
abandonClient = true;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
awaitingNativeTurnStart = false;
|
||||
route.release();
|
||||
route = undefined;
|
||||
const transferredWatch = completionWatch;
|
||||
if (!transferredWatch) {
|
||||
abandonClient = true;
|
||||
throw new Error(
|
||||
`codex app-server ${kind} turn started without a turn id for thread ${currentBinding.threadId}`,
|
||||
);
|
||||
}
|
||||
completionWatch = undefined;
|
||||
lifecycleTransferred = true;
|
||||
monitorCodexNativeTurn({
|
||||
completionWatch: transferredWatch,
|
||||
clientLease,
|
||||
subscribedThreadId,
|
||||
threadId: currentBinding.threadId,
|
||||
kind,
|
||||
});
|
||||
} finally {
|
||||
if (!lifecycleTransferred) {
|
||||
completionWatch?.cancel();
|
||||
route?.release();
|
||||
await settleCodexAppServerClientLease(clientLease, {
|
||||
threadId: subscribedThreadId,
|
||||
timeoutMs: CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
abandon: abandonClient,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function assertCodexReviewStartResponse(value: JsonValue | undefined): {
|
||||
turnId: string;
|
||||
reviewThreadId: string;
|
||||
} {
|
||||
if (
|
||||
!isJsonObject(value) ||
|
||||
!isJsonObject(value.turn) ||
|
||||
typeof value.turn.id !== "string" ||
|
||||
!value.turn.id.trim() ||
|
||||
typeof value.reviewThreadId !== "string" ||
|
||||
!value.reviewThreadId.trim()
|
||||
) {
|
||||
throw new Error("invalid Codex review/start response");
|
||||
}
|
||||
return { turnId: value.turn.id, reviewThreadId: value.reviewThreadId };
|
||||
}
|
||||
|
||||
function monitorCodexNativeTurn(params: {
|
||||
completionWatch: CodexNativeTurnCompletionWatch;
|
||||
clientLease: CodexAppServerClientLease;
|
||||
subscribedThreadId?: string;
|
||||
threadId: string;
|
||||
kind: CodexNativeTurnKind;
|
||||
}): void {
|
||||
void (async () => {
|
||||
const completed = await params.completionWatch.completion;
|
||||
await settleCodexAppServerClientLease(params.clientLease, {
|
||||
threadId: params.subscribedThreadId,
|
||||
timeoutMs: CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
abandon: !completed,
|
||||
});
|
||||
if (!completed) {
|
||||
embeddedAgentLog.warn(`codex app-server ${params.kind} turn lost terminal confirmation`, {
|
||||
threadId: params.threadId,
|
||||
});
|
||||
}
|
||||
})().catch(async (error: unknown) => {
|
||||
await params.clientLease.abandon().catch(() => undefined);
|
||||
embeddedAgentLog.warn(`codex app-server ${params.kind} turn cleanup failed`, {
|
||||
threadId: params.threadId,
|
||||
error,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function throwIfCodexNativeTurnAborted(
|
||||
signal: AbortSignal | undefined,
|
||||
kind: CodexNativeTurnKind,
|
||||
): void {
|
||||
if (!signal?.aborted) {
|
||||
return;
|
||||
}
|
||||
if (signal.reason instanceof Error) {
|
||||
throw signal.reason;
|
||||
}
|
||||
throw new Error(`codex app-server ${kind} aborted before native turn startup`, {
|
||||
cause: signal.reason,
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForCodexNativeTurnStart(params: {
|
||||
started: Promise<void>;
|
||||
routeSignal: AbortSignal;
|
||||
timeoutMs: number;
|
||||
threadId: string;
|
||||
kind: CodexNativeTurnKind;
|
||||
}): Promise<void> {
|
||||
const signal = params.routeSignal;
|
||||
let removeAbort: (() => void) | undefined;
|
||||
const aborted = new Promise<never>((_resolve, reject) => {
|
||||
const onAbort = () => reject(asNativeTurnAbortError(signal));
|
||||
signal.addEventListener("abort", onAbort, { once: true });
|
||||
removeAbort = () => signal.removeEventListener("abort", onAbort);
|
||||
if (signal.aborted) {
|
||||
onAbort();
|
||||
}
|
||||
});
|
||||
try {
|
||||
await withTimeout(
|
||||
Promise.race([params.started, aborted]),
|
||||
params.timeoutMs,
|
||||
`codex app-server ${params.kind} turn did not start for thread ${params.threadId}`,
|
||||
);
|
||||
} finally {
|
||||
removeAbort?.();
|
||||
}
|
||||
}
|
||||
|
||||
function asNativeTurnAbortError(signal: AbortSignal): Error {
|
||||
return signal.reason instanceof Error
|
||||
? signal.reason
|
||||
: new Error("codex app-server native turn startup aborted", { cause: signal.reason });
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts native Codex compaction for a manually requested bound session, or
|
||||
* reports why Codex-owned automatic compaction should handle the trigger.
|
||||
*/
|
||||
export async function maybeCompactCodexAppServerSession(
|
||||
params: CompactEmbeddedAgentSessionParams,
|
||||
options: CodexAppServerCompactOptions,
|
||||
options: CodexAppServerCompactOptions = {},
|
||||
): Promise<EmbeddedAgentCompactResult | undefined> {
|
||||
warnIfIgnoringOpenClawCompactionOverrides(params);
|
||||
// Codex owns automatic context-pressure compaction for Codex runtime sessions.
|
||||
// This entry point starts native Codex compaction for the bound thread and
|
||||
// returns immediately; Codex applies the compaction inside its app-server.
|
||||
return compactCodexNativeThread(params, options);
|
||||
}
|
||||
|
||||
function warnIfIgnoringOpenClawCompactionOverrides(
|
||||
params: CompactEmbeddedAgentSessionParams,
|
||||
): void {
|
||||
const ignoredConfig = readIgnoredCompactionOverridePaths(params);
|
||||
if (ignoredConfig.length === 0) {
|
||||
return;
|
||||
}
|
||||
const warningKey = ignoredConfig.join("\0");
|
||||
if (warnedIgnoredCompactionOverrides.has(warningKey)) {
|
||||
return;
|
||||
}
|
||||
warnedIgnoredCompactionOverrides.add(warningKey);
|
||||
embeddedAgentLog.warn(
|
||||
"ignoring OpenClaw compaction overrides for Codex app-server compaction; Codex uses native server-side compaction",
|
||||
{
|
||||
sessionId: params.sessionId,
|
||||
sessionKey: params.sessionKey,
|
||||
ignoredConfig,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function readIgnoredCompactionOverridePaths(params: CompactEmbeddedAgentSessionParams): string[] {
|
||||
const ignored = new Set<string>();
|
||||
for (const entry of readCompactionOverrideEntries(params)) {
|
||||
const localProvider =
|
||||
typeof entry.record.provider === "string" ? entry.record.provider.trim() : "";
|
||||
const inheritedProvider =
|
||||
!localProvider && typeof entry.inheritedRecord?.provider === "string"
|
||||
? entry.inheritedRecord.provider.trim()
|
||||
: "";
|
||||
const providerPath = localProvider
|
||||
? `${entry.path}.compaction.provider`
|
||||
: inheritedProvider && entry.inheritedPath
|
||||
? `${entry.inheritedPath}.compaction.provider`
|
||||
: undefined;
|
||||
if (typeof entry.record.model === "string" && entry.record.model.trim()) {
|
||||
ignored.add(`${entry.path}.compaction.model`);
|
||||
}
|
||||
if (providerPath) {
|
||||
ignored.add(providerPath);
|
||||
}
|
||||
}
|
||||
return [...ignored];
|
||||
}
|
||||
|
||||
function readCompactionOverrideEntries(params: CompactEmbeddedAgentSessionParams): Array<{
|
||||
path: string;
|
||||
record: Record<string, unknown>;
|
||||
inheritedRecord?: Record<string, unknown>;
|
||||
inheritedPath?: string;
|
||||
}> {
|
||||
const entries: Array<{
|
||||
path: string;
|
||||
record: Record<string, unknown>;
|
||||
inheritedRecord?: Record<string, unknown>;
|
||||
inheritedPath?: string;
|
||||
}> = [];
|
||||
const defaultCompaction = readRecord(readRecord(params.config?.agents)?.defaults)?.compaction;
|
||||
const defaultRecord = readRecord(defaultCompaction);
|
||||
if (defaultRecord) {
|
||||
entries.push({ path: "agents.defaults", record: defaultRecord });
|
||||
}
|
||||
const agentId = readAgentIdFromSessionKey(params.sessionKey ?? params.sandboxSessionKey);
|
||||
if (!agentId) {
|
||||
return entries;
|
||||
}
|
||||
const agents = Array.isArray(params.config?.agents?.list) ? params.config.agents.list : [];
|
||||
const activeAgent = agents.find((agent) => {
|
||||
const id = typeof agent?.id === "string" ? agent.id.trim().toLowerCase() : "";
|
||||
return id === agentId;
|
||||
});
|
||||
const agentCompaction = readRecord(activeAgent)?.compaction;
|
||||
const agentRecord = readRecord(agentCompaction);
|
||||
if (agentRecord) {
|
||||
entries.push({
|
||||
path: `agents.list.${agentId}`,
|
||||
record: agentRecord,
|
||||
inheritedRecord: defaultRecord,
|
||||
inheritedPath: "agents.defaults",
|
||||
});
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
function readAgentIdFromSessionKey(sessionKey: string | undefined): string | undefined {
|
||||
const parts = sessionKey?.trim().toLowerCase().split(":").filter(Boolean) ?? [];
|
||||
if (parts.length < 3 || parts[0] !== "agent") {
|
||||
return undefined;
|
||||
}
|
||||
return parts[1]?.trim() || undefined;
|
||||
}
|
||||
|
||||
function readRecord(value: unknown): Record<string, unknown> | undefined {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
async function compactCodexNativeThread(
|
||||
params: CompactEmbeddedAgentSessionParams,
|
||||
options: CodexAppServerCompactOptions,
|
||||
options: CodexAppServerCompactOptions = {},
|
||||
): Promise<EmbeddedAgentCompactResult | undefined> {
|
||||
if (params.trigger !== "manual" && !options.allowNonManualNativeRequest) {
|
||||
embeddedAgentLog.info("skipping codex app-server compaction for non-manual trigger", {
|
||||
@@ -423,7 +172,6 @@ async function compactCodexNativeThread(
|
||||
}
|
||||
const nativeExecutionBlock = resolveCodexNativeExecutionBlock({
|
||||
config: params.config,
|
||||
agentId: params.agentId,
|
||||
sessionKey: params.sandboxSessionKey ?? params.sessionKey,
|
||||
sessionId: params.sessionId,
|
||||
surface: "native compaction",
|
||||
@@ -431,20 +179,17 @@ async function compactCodexNativeThread(
|
||||
if (nativeExecutionBlock) {
|
||||
return { ok: false, compacted: false, reason: nativeExecutionBlock };
|
||||
}
|
||||
const bindingIdentity: CodexAppServerBindingIdentity = sessionBindingIdentity({
|
||||
sessionId: params.sessionId,
|
||||
sessionKey: params.sessionKey,
|
||||
agentId: params.agentId,
|
||||
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig: options.pluginConfig });
|
||||
const initialBinding = await readCodexAppServerBinding(params.sessionFile, {
|
||||
config: params.config,
|
||||
});
|
||||
const initialBinding = await options.bindingStore.read(bindingIdentity);
|
||||
if (!initialBinding?.threadId) {
|
||||
return failedCodexThreadBindingCompactionResult(params, {
|
||||
reason: "no codex app-server thread binding",
|
||||
recovery: "missing_thread_binding",
|
||||
});
|
||||
}
|
||||
const binding = initialBinding;
|
||||
let binding = initialBinding;
|
||||
const requestedAuthProfileId = params.authProfileId?.trim() || undefined;
|
||||
if (
|
||||
requestedAuthProfileId &&
|
||||
@@ -455,42 +200,85 @@ async function compactCodexNativeThread(
|
||||
// with another profile risks operating on a different Codex account.
|
||||
return { ok: false, compacted: false, reason: "auth profile mismatch for session binding" };
|
||||
}
|
||||
if (options.allowNonManualNativeRequest && params.abortSignal?.aborted) {
|
||||
const currentBinding = await options.bindingStore.read(bindingIdentity);
|
||||
return skippedCodexNativeCompactionResult(params, {
|
||||
reason: "codex app-server compaction aborted before native compaction",
|
||||
code: "aborted_before_native_compaction",
|
||||
expectedThreadId: binding.threadId,
|
||||
currentThreadId: currentBinding?.threadId,
|
||||
});
|
||||
}
|
||||
const shouldReleaseDefaultLease = !options.clientFactory;
|
||||
const clientFactory = options.clientFactory ?? defaultLeasedCodexAppServerClientFactory;
|
||||
const client = await clientFactory(
|
||||
appServer.start,
|
||||
requestedAuthProfileId ?? binding.authProfileId,
|
||||
params.agentDir,
|
||||
params.config,
|
||||
);
|
||||
try {
|
||||
await requestCodexNativeTurnForBinding(
|
||||
{
|
||||
bindingIdentity,
|
||||
bindingStore: options.bindingStore,
|
||||
expectedBinding: binding,
|
||||
pluginConfig: options.pluginConfig,
|
||||
authProfileId: requestedAuthProfileId,
|
||||
agentDir: params.agentDir,
|
||||
config: params.config,
|
||||
abortSignal: params.abortSignal,
|
||||
clientLeaseFactory: options.clientLeaseFactory,
|
||||
},
|
||||
"compact",
|
||||
);
|
||||
if (options.allowNonManualNativeRequest) {
|
||||
const guardedResult = await withCodexAppServerBindingLock(params.sessionFile, async () => {
|
||||
const currentBinding = await readCodexAppServerBinding(params.sessionFile, {
|
||||
config: params.config,
|
||||
});
|
||||
if (params.abortSignal?.aborted) {
|
||||
return {
|
||||
started: false as const,
|
||||
result: skippedCodexNativeCompactionResult(params, {
|
||||
reason: "codex app-server compaction aborted before native compaction",
|
||||
code: "aborted_before_native_compaction",
|
||||
expectedThreadId: binding.threadId,
|
||||
currentThreadId: currentBinding?.threadId,
|
||||
}),
|
||||
};
|
||||
}
|
||||
if (!currentBinding || !isSameNativeCompactionBinding(currentBinding, binding)) {
|
||||
embeddedAgentLog.warn(
|
||||
"skipping codex app-server compaction because the thread binding changed",
|
||||
{
|
||||
sessionId: params.sessionId,
|
||||
sessionKey: params.sessionKey,
|
||||
expectedThreadId: binding.threadId,
|
||||
currentThreadId: currentBinding?.threadId,
|
||||
},
|
||||
);
|
||||
return {
|
||||
started: false as const,
|
||||
result: skippedCodexNativeCompactionResult(params, {
|
||||
reason: "codex app-server binding changed before native compaction",
|
||||
code: "binding_changed_before_native_compaction",
|
||||
expectedThreadId: binding.threadId,
|
||||
currentThreadId: currentBinding?.threadId,
|
||||
}),
|
||||
};
|
||||
}
|
||||
binding = currentBinding;
|
||||
await clearContextEngineProjectionBeforeNativeCompaction({
|
||||
sessionId: params.sessionId,
|
||||
sessionFile: params.sessionFile,
|
||||
binding,
|
||||
config: params.config,
|
||||
});
|
||||
await client.request(
|
||||
"thread/compact/start",
|
||||
{
|
||||
threadId: binding.threadId,
|
||||
},
|
||||
{
|
||||
timeoutMs: Math.min(
|
||||
appServer.requestTimeoutMs,
|
||||
CODEX_APP_SERVER_BINDING_GUARDED_REQUEST_TIMEOUT_MS,
|
||||
),
|
||||
},
|
||||
);
|
||||
return { started: true as const };
|
||||
});
|
||||
if (!guardedResult.started) {
|
||||
return guardedResult.result;
|
||||
}
|
||||
} else {
|
||||
await client.request("thread/compact/start", {
|
||||
threadId: binding.threadId,
|
||||
});
|
||||
}
|
||||
embeddedAgentLog.info("started codex app-server compaction", {
|
||||
sessionId: params.sessionId,
|
||||
threadId: binding.threadId,
|
||||
});
|
||||
} catch (error) {
|
||||
if (
|
||||
options.allowNonManualNativeRequest &&
|
||||
error instanceof CodexNativeTurnBindingChangedError
|
||||
) {
|
||||
const latestBinding = await options.bindingStore.read(bindingIdentity);
|
||||
return skippedBindingChangeResult(params, binding.threadId, latestBinding?.threadId);
|
||||
}
|
||||
if (isCodexThreadNotFoundError(error)) {
|
||||
return failedCodexThreadBindingCompactionResult(params, {
|
||||
threadId: binding.threadId,
|
||||
@@ -509,6 +297,10 @@ async function compactCodexNativeThread(
|
||||
compacted: false,
|
||||
reason: formatCompactionError(error),
|
||||
};
|
||||
} finally {
|
||||
if (shouldReleaseDefaultLease) {
|
||||
releaseLeasedSharedCodexAppServerClient(client);
|
||||
}
|
||||
}
|
||||
const resultDetails: JsonObject = {
|
||||
backend: "codex-app-server",
|
||||
@@ -534,25 +326,6 @@ async function compactCodexNativeThread(
|
||||
};
|
||||
}
|
||||
|
||||
function skippedBindingChangeResult(
|
||||
params: CompactEmbeddedAgentSessionParams,
|
||||
expectedThreadId: string,
|
||||
currentThreadId: string | undefined,
|
||||
): EmbeddedAgentCompactResult {
|
||||
embeddedAgentLog.warn("skipping codex app-server compaction because the thread binding changed", {
|
||||
sessionId: params.sessionId,
|
||||
sessionKey: params.sessionKey,
|
||||
expectedThreadId,
|
||||
currentThreadId,
|
||||
});
|
||||
return skippedCodexNativeCompactionResult(params, {
|
||||
reason: "codex app-server binding changed before native compaction",
|
||||
code: "binding_changed_before_native_compaction",
|
||||
expectedThreadId,
|
||||
currentThreadId,
|
||||
});
|
||||
}
|
||||
|
||||
function skippedCodexNativeCompactionResult(
|
||||
params: CompactEmbeddedAgentSessionParams,
|
||||
skipped: {
|
||||
@@ -609,7 +382,39 @@ function failedCodexThreadBindingCompactionResult(
|
||||
};
|
||||
}
|
||||
|
||||
function isSameNativeTurnBinding(
|
||||
async function clearContextEngineProjectionBeforeNativeCompaction(params: {
|
||||
sessionId: string;
|
||||
sessionFile: string;
|
||||
binding: CodexAppServerThreadBinding;
|
||||
config: CompactEmbeddedAgentSessionParams["config"];
|
||||
}): Promise<void> {
|
||||
const contextEngineBinding = params.binding.contextEngine;
|
||||
if (!contextEngineBinding?.projection) {
|
||||
return;
|
||||
}
|
||||
// Native Codex compaction mutates the thread history outside the projection
|
||||
// guard. Clear only the projection marker so the next turn reprojects context.
|
||||
await writeCodexAppServerBinding(
|
||||
params.sessionFile,
|
||||
{
|
||||
...params.binding,
|
||||
contextEngine: {
|
||||
...contextEngineBinding,
|
||||
projection: undefined,
|
||||
},
|
||||
createdAt: params.binding.createdAt,
|
||||
},
|
||||
{ config: params.config },
|
||||
);
|
||||
embeddedAgentLog.info("cleared codex context-engine projection before native compaction", {
|
||||
sessionId: params.sessionId,
|
||||
threadId: params.binding.threadId,
|
||||
previousEpoch: contextEngineBinding.projection.epoch,
|
||||
previousFingerprint: contextEngineBinding.projection.fingerprint,
|
||||
});
|
||||
}
|
||||
|
||||
function isSameNativeCompactionBinding(
|
||||
current: CodexAppServerThreadBinding,
|
||||
expected: CodexAppServerThreadBinding,
|
||||
): boolean {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
// Codex tests cover config plugin behavior.
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
@@ -308,6 +306,7 @@ describe("Codex app-server config", () => {
|
||||
const switchedLocalModel = resolveCodexModelBackedReviewerPolicyContext({
|
||||
model: "lmstudio/local-model",
|
||||
bindingModel: "gpt-5.5",
|
||||
nativeAuthProfile: true,
|
||||
});
|
||||
expect(switchedLocalModel).toEqual({
|
||||
modelProvider: "lmstudio",
|
||||
@@ -494,39 +493,6 @@ describe("Codex app-server config", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("reloads Codex config.toml policy when Codex can reload it", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-config-"));
|
||||
const codexHome = path.join(agentDir, "codex-home");
|
||||
const configPath = path.join(codexHome, "config.toml");
|
||||
await fs.mkdir(codexHome);
|
||||
try {
|
||||
await fs.writeFile(configPath, 'openai_base_url = "http://localhost:8080/v1"\n');
|
||||
const context = { modelProvider: "openai", model: "gpt-5.5", agentDir };
|
||||
expect(canUseCodexModelBackedApprovalsReviewerForModel(context)).toBe(false);
|
||||
|
||||
await fs.writeFile(configPath, 'openai_base_url = "https://api.openai.com/v1"\n');
|
||||
expect(canUseCodexModelBackedApprovalsReviewerForModel(context)).toBe(true);
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("observes a Codex config.toml created after the first policy check", async () => {
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-config-"));
|
||||
const codexHome = path.join(agentDir, "codex-home");
|
||||
const configPath = path.join(codexHome, "config.toml");
|
||||
await fs.mkdir(codexHome);
|
||||
try {
|
||||
const context = { modelProvider: "openai", model: "gpt-5.5", agentDir };
|
||||
expect(canUseCodexModelBackedApprovalsReviewerForModel(context)).toBe(true);
|
||||
|
||||
await fs.writeFile(configPath, 'openai_base_url = "http://localhost:8080/v1"\n');
|
||||
expect(canUseCodexModelBackedApprovalsReviewerForModel(context)).toBe(false);
|
||||
} finally {
|
||||
await fs.rm(agentDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("forces prompting when explicit no-prompt config cannot use model-backed review", () => {
|
||||
const runtime = resolveRuntimeForTest({
|
||||
pluginConfig: {
|
||||
@@ -724,8 +690,8 @@ allowed_sandbox_modes = ["read-only", "workspace-write"]
|
||||
env: {},
|
||||
modelProvider: "openai",
|
||||
requirementsPath: "/custom/codex/requirements.toml",
|
||||
readRequirementsFile: (requirementsPath) => {
|
||||
readPaths.push(requirementsPath);
|
||||
readRequirementsFile: (path) => {
|
||||
readPaths.push(path);
|
||||
return 'allowed_sandbox_modes = ["read-only", "workspace-write"]\n';
|
||||
},
|
||||
});
|
||||
@@ -745,8 +711,8 @@ allowed_sandbox_modes = ["read-only", "workspace-write"]
|
||||
env: { ProgramData: "D:\\ManagedData" },
|
||||
modelProvider: "openai",
|
||||
platform: "win32",
|
||||
readRequirementsFile: (requirementsPath) => {
|
||||
readPaths.push(requirementsPath);
|
||||
readRequirementsFile: (path) => {
|
||||
readPaths.push(path);
|
||||
return 'allowed_sandbox_modes = ["read-only", "workspace-write"]\n';
|
||||
},
|
||||
});
|
||||
|
||||
@@ -150,11 +150,6 @@ export type CodexAppServerRuntimeOptions = {
|
||||
serviceTier?: CodexServiceTier;
|
||||
};
|
||||
|
||||
export type CodexAppServerRuntimeResolution = {
|
||||
appServer: CodexAppServerRuntimeOptions;
|
||||
modelBackedReviewerAvailable: boolean;
|
||||
};
|
||||
|
||||
export type CodexModelBackedReviewerContext = {
|
||||
modelProvider?: string;
|
||||
model?: string;
|
||||
@@ -410,34 +405,25 @@ export function resolveCodexPluginsPolicy(pluginConfig?: unknown): ResolvedCodex
|
||||
};
|
||||
}
|
||||
|
||||
type CodexAppServerRuntimeParams = {
|
||||
pluginConfig?: unknown;
|
||||
execMode?: OpenClawExecMode;
|
||||
execPolicy?: OpenClawExecPolicyForCodexAppServer;
|
||||
modelProvider?: string;
|
||||
model?: string;
|
||||
config?: ProviderAuthAliasConfig;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
agentDir?: string;
|
||||
codexConfigToml?: string | null;
|
||||
requirementsToml?: string | null;
|
||||
requirementsPath?: string;
|
||||
readRequirementsFile?: (path: string) => string | undefined;
|
||||
platform?: NodeJS.Platform;
|
||||
hostName?: string;
|
||||
openClawSandboxActive?: boolean;
|
||||
};
|
||||
|
||||
export function resolveCodexAppServerRuntimeOptions(
|
||||
params: CodexAppServerRuntimeParams = {},
|
||||
params: {
|
||||
pluginConfig?: unknown;
|
||||
execMode?: OpenClawExecMode;
|
||||
execPolicy?: OpenClawExecPolicyForCodexAppServer;
|
||||
modelProvider?: string;
|
||||
model?: string;
|
||||
config?: ProviderAuthAliasConfig;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
agentDir?: string;
|
||||
codexConfigToml?: string | null;
|
||||
requirementsToml?: string | null;
|
||||
requirementsPath?: string;
|
||||
readRequirementsFile?: (path: string) => string | undefined;
|
||||
platform?: NodeJS.Platform;
|
||||
hostName?: string;
|
||||
openClawSandboxActive?: boolean;
|
||||
} = {},
|
||||
): CodexAppServerRuntimeOptions {
|
||||
return resolveCodexAppServerRuntime(params).appServer;
|
||||
}
|
||||
|
||||
/** Resolves runtime options and the model-policy fact computed with them. */
|
||||
export function resolveCodexAppServerRuntime(
|
||||
params: CodexAppServerRuntimeParams = {},
|
||||
): CodexAppServerRuntimeResolution {
|
||||
const env = params.env ?? process.env;
|
||||
const config = readCodexPluginConfig(params.pluginConfig).appServer ?? {};
|
||||
const transport = resolveTransport(config.transport);
|
||||
@@ -561,46 +547,43 @@ export function resolveCodexAppServerRuntime(
|
||||
: "implicit";
|
||||
|
||||
return {
|
||||
modelBackedReviewerAvailable: canUseModelBackedReviewer,
|
||||
appServer: {
|
||||
start: {
|
||||
transport,
|
||||
command,
|
||||
commandSource,
|
||||
args: args.length > 0 ? args : ["app-server", "--listen", "stdio://"],
|
||||
...(url ? { url } : {}),
|
||||
...(authToken ? { authToken } : {}),
|
||||
headers,
|
||||
...(transport === "stdio" && clearEnv.length > 0 ? { clearEnv } : {}),
|
||||
},
|
||||
codeModeOnly: config.codeModeOnly === true,
|
||||
requestTimeoutMs: normalizePositiveNumber(config.requestTimeoutMs, 60_000),
|
||||
turnCompletionIdleTimeoutMs: normalizePositiveNumber(
|
||||
config.turnCompletionIdleTimeoutMs,
|
||||
60_000,
|
||||
),
|
||||
...(config.postToolRawAssistantCompletionIdleTimeoutMs !== undefined
|
||||
? {
|
||||
postToolRawAssistantCompletionIdleTimeoutMs: normalizePositiveNumber(
|
||||
config.postToolRawAssistantCompletionIdleTimeoutMs,
|
||||
60_000,
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
approvalPolicy: forcedPolicy?.approvalPolicy ?? approvalPolicy,
|
||||
approvalPolicySource,
|
||||
sandbox:
|
||||
forcedPolicy?.sandbox ??
|
||||
configuredSandbox ??
|
||||
defaultPolicy?.sandbox ??
|
||||
(policyMode === "guardian" ? "workspace-write" : "danger-full-access"),
|
||||
approvalsReviewer:
|
||||
forcedPolicy?.approvalsReviewer ??
|
||||
explicitApprovalsReviewer ??
|
||||
defaultPolicy?.approvalsReviewer ??
|
||||
(policyMode === "guardian" ? "auto_review" : "user"),
|
||||
...(serviceTier ? { serviceTier } : {}),
|
||||
start: {
|
||||
transport,
|
||||
command,
|
||||
commandSource,
|
||||
args: args.length > 0 ? args : ["app-server", "--listen", "stdio://"],
|
||||
...(url ? { url } : {}),
|
||||
...(authToken ? { authToken } : {}),
|
||||
headers,
|
||||
...(transport === "stdio" && clearEnv.length > 0 ? { clearEnv } : {}),
|
||||
},
|
||||
codeModeOnly: config.codeModeOnly === true,
|
||||
requestTimeoutMs: normalizePositiveNumber(config.requestTimeoutMs, 60_000),
|
||||
turnCompletionIdleTimeoutMs: normalizePositiveNumber(
|
||||
config.turnCompletionIdleTimeoutMs,
|
||||
60_000,
|
||||
),
|
||||
...(config.postToolRawAssistantCompletionIdleTimeoutMs !== undefined
|
||||
? {
|
||||
postToolRawAssistantCompletionIdleTimeoutMs: normalizePositiveNumber(
|
||||
config.postToolRawAssistantCompletionIdleTimeoutMs,
|
||||
60_000,
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
approvalPolicy: forcedPolicy?.approvalPolicy ?? approvalPolicy,
|
||||
approvalPolicySource,
|
||||
sandbox:
|
||||
forcedPolicy?.sandbox ??
|
||||
configuredSandbox ??
|
||||
defaultPolicy?.sandbox ??
|
||||
(policyMode === "guardian" ? "workspace-write" : "danger-full-access"),
|
||||
approvalsReviewer:
|
||||
forcedPolicy?.approvalsReviewer ??
|
||||
explicitApprovalsReviewer ??
|
||||
defaultPolicy?.approvalsReviewer ??
|
||||
(policyMode === "guardian" ? "auto_review" : "user"),
|
||||
...(serviceTier ? { serviceTier } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -672,6 +655,7 @@ export function resolveCodexModelBackedReviewerPolicyContext(params: {
|
||||
model?: string;
|
||||
bindingModelProvider?: string;
|
||||
bindingModel?: string;
|
||||
nativeAuthProfile?: boolean;
|
||||
}): CodexModelBackedReviewerContext {
|
||||
const provider = params.provider?.trim();
|
||||
if (provider && provider.toLowerCase() !== "codex") {
|
||||
@@ -703,7 +687,7 @@ export function resolveCodexModelBackedReviewerPolicyContext(params: {
|
||||
};
|
||||
}
|
||||
return {
|
||||
modelProvider: undefined,
|
||||
modelProvider: params.nativeAuthProfile === true ? "openai" : undefined,
|
||||
model: params.model ?? params.bindingModel,
|
||||
};
|
||||
}
|
||||
@@ -770,7 +754,6 @@ export function codexAppServerStartOptionsKey(
|
||||
options: CodexAppServerStartOptions,
|
||||
params: {
|
||||
authProfileId?: string;
|
||||
authAccountCacheKey?: string;
|
||||
agentDir?: string;
|
||||
fallbackApiKeyCacheKey?: string;
|
||||
} = {},
|
||||
@@ -790,7 +773,6 @@ export function codexAppServerStartOptionsKey(
|
||||
.map(([key, value]) => [key, hashSecretForKey(value, `env:${key}`)]),
|
||||
clearEnv: [...(options.clearEnv ?? [])].toSorted(),
|
||||
authProfileId: params.authProfileId ?? null,
|
||||
authAccountCacheKey: params.authAccountCacheKey ?? null,
|
||||
agentDir: params.agentDir ?? null,
|
||||
fallbackApiKeyCacheKey: params.fallbackApiKeyCacheKey ?? null,
|
||||
});
|
||||
|
||||
@@ -9,17 +9,15 @@ import {
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
addSandboxShellDynamicToolsIfAvailable,
|
||||
buildDynamicTools,
|
||||
filterCodexDynamicToolsForAllowlist,
|
||||
hasWildcardCodexToolsAllow,
|
||||
includeForcedCodexDynamicToolAllow,
|
||||
prepareDynamicToolCatalog,
|
||||
resetOpenClawCodingToolsFactoryForTests,
|
||||
resolveOpenClawCodingToolsSessionKeys,
|
||||
resolveCodexMessageToolProvider,
|
||||
setOpenClawCodingToolsFactoryForTests,
|
||||
shouldEnableCodexAppServerNativeToolSurface,
|
||||
shouldForceMessageTool,
|
||||
type OpenClawCodingToolsFactory,
|
||||
} from "./dynamic-tool-build.js";
|
||||
import {
|
||||
filterCodexDynamicTools,
|
||||
@@ -101,13 +99,13 @@ function createRuntimeDynamicTool(name: string): RuntimeDynamicToolForTest {
|
||||
async function buildDynamicToolsForTest(
|
||||
params: EmbeddedRunAttemptParams,
|
||||
workspaceDir: string,
|
||||
options: Partial<Parameters<typeof prepareDynamicToolCatalog>[0]> = {},
|
||||
options: Partial<Parameters<typeof buildDynamicTools>[0]> = {},
|
||||
) {
|
||||
const sandboxSessionKey = params.sessionKey;
|
||||
if (!sandboxSessionKey) {
|
||||
throw new Error("createParams must provide a sessionKey for Codex dynamic tool tests.");
|
||||
}
|
||||
const catalog = await prepareDynamicToolCatalog({
|
||||
return buildDynamicTools({
|
||||
params,
|
||||
resolvedWorkspace: workspaceDir,
|
||||
effectiveWorkspace: workspaceDir,
|
||||
@@ -120,7 +118,6 @@ async function buildDynamicToolsForTest(
|
||||
onYieldDetected: () => undefined,
|
||||
...options,
|
||||
});
|
||||
return catalog.tools;
|
||||
}
|
||||
|
||||
describe("Codex app-server dynamic tool build", () => {
|
||||
@@ -135,15 +132,6 @@ describe("Codex app-server dynamic tool build", () => {
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("uses the message tool channel before a differing ingress provider", () => {
|
||||
expect(
|
||||
resolveCodexMessageToolProvider({
|
||||
messageChannel: "discord",
|
||||
messageProvider: "discord-voice",
|
||||
}),
|
||||
).toBe("discord");
|
||||
});
|
||||
|
||||
it("filters Codex-native dynamic tools from app-server tool exposure", () => {
|
||||
const tools = [
|
||||
"read",
|
||||
@@ -171,53 +159,6 @@ describe("Codex app-server dynamic tool build", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("prepares runtime and durable tool views from one OpenClaw catalog", async () => {
|
||||
const messageTool = createRuntimeDynamicTool("message");
|
||||
const webSearchTool = createRuntimeDynamicTool("web_search");
|
||||
const heartbeatTool = createRuntimeDynamicTool("heartbeat_respond");
|
||||
const factory = vi.fn<OpenClawCodingToolsFactory>((options) => [
|
||||
messageTool,
|
||||
webSearchTool,
|
||||
...(options?.enableHeartbeatTool ? [heartbeatTool] : []),
|
||||
]);
|
||||
setOpenClawCodingToolsFactoryForTests(factory);
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.disableTools = false;
|
||||
const runtimePlan = createCodexRuntimePlanFixture();
|
||||
params.runtimePlan = {
|
||||
...runtimePlan,
|
||||
tools: {
|
||||
normalize: (tools: Array<{ name: string }>) =>
|
||||
tools.filter((tool) => tool.name === "message"),
|
||||
logDiagnostics: () => undefined,
|
||||
},
|
||||
} as unknown as NonNullable<EmbeddedRunAttemptParams["runtimePlan"]>;
|
||||
|
||||
const catalog = await prepareDynamicToolCatalog({
|
||||
params,
|
||||
resolvedWorkspace: workspaceDir,
|
||||
effectiveWorkspace: workspaceDir,
|
||||
sandboxSessionKey: params.sessionKey ?? "agent:main:session-1",
|
||||
sandbox: { enabled: false, backendId: "docker" } as never,
|
||||
nativeToolSurfaceEnabled: true,
|
||||
runAbortController: new AbortController(),
|
||||
sessionAgentId: "main",
|
||||
pluginConfig: {},
|
||||
onYieldDetected: () => undefined,
|
||||
});
|
||||
|
||||
expect(factory).toHaveBeenCalledTimes(1);
|
||||
expect(factory.mock.calls[0]?.[0]?.enableHeartbeatTool).toBe(true);
|
||||
expect(catalog.tools.map((tool) => tool.name)).toEqual(["message"]);
|
||||
expect(catalog.registeredTools.map((tool) => tool.name)).toEqual([
|
||||
"message",
|
||||
"web_search",
|
||||
"heartbeat_respond",
|
||||
]);
|
||||
});
|
||||
|
||||
it("applies additional Codex dynamic tool excludes without exposing Codex-native tools", () => {
|
||||
const tools = ["read", "exec", "message", "custom_tool"].map((name) => ({ name }));
|
||||
|
||||
@@ -608,28 +549,6 @@ describe("Codex app-server dynamic tool build", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("passes native and routable channel targets into Codex dynamic tools", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.disableTools = false;
|
||||
params.currentChannelId = "D123";
|
||||
params.currentMessagingTarget = "user:U123";
|
||||
params.runtimePlan = createCodexRuntimePlanFixture();
|
||||
const factoryOptions: unknown[] = [];
|
||||
setOpenClawCodingToolsFactoryForTests((options) => {
|
||||
factoryOptions.push(options);
|
||||
return [];
|
||||
});
|
||||
|
||||
await buildDynamicToolsForTest(params, workspaceDir, { sandbox: null as never });
|
||||
|
||||
expect(factoryOptions[0]).toMatchObject({
|
||||
currentChannelId: "D123",
|
||||
currentMessagingTarget: "user:U123",
|
||||
});
|
||||
});
|
||||
|
||||
it("passes runtime config into Codex exec dynamic tool construction", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
|
||||
@@ -38,9 +38,6 @@ type OpenClawCodingToolsOptions = NonNullable<
|
||||
export type OpenClawCodingToolsFactory =
|
||||
(typeof import("openclaw/plugin-sdk/agent-harness"))["createOpenClawCodingTools"];
|
||||
type OpenClawDynamicTool = ReturnType<OpenClawCodingToolsFactory>[number];
|
||||
type OpenClawDynamicToolProjection = ReturnType<
|
||||
typeof filterProviderNormalizableTools<OpenClawDynamicTool>
|
||||
>;
|
||||
type OpenClawSandboxContext = Awaited<ReturnType<typeof resolveSandboxContext>>;
|
||||
type CodexDynamicToolBuildEvent = Parameters<
|
||||
NonNullable<EmbeddedRunAttemptParams["onAgentEvent"]>
|
||||
@@ -55,7 +52,6 @@ const CODEX_NATIVE_SANDBOX_TOOL_REQUIREMENTS = [
|
||||
"apply_patch",
|
||||
] as const;
|
||||
const CODEX_MEMORY_FLUSH_DYNAMIC_TOOL_ALLOW = new Set(["read", "write"]);
|
||||
const CODEX_HEARTBEAT_DYNAMIC_TOOL_NAME = "heartbeat_respond";
|
||||
|
||||
/** Runtime inputs needed to derive the exact Codex dynamic tool surface for a turn. */
|
||||
export type DynamicToolBuildParams = {
|
||||
@@ -70,6 +66,8 @@ export type DynamicToolBuildParams = {
|
||||
sessionAgentId: string;
|
||||
pluginConfig: CodexPluginConfig;
|
||||
profilerEnabled?: boolean;
|
||||
forceHeartbeatTool?: boolean;
|
||||
ignoreRuntimePlan?: boolean;
|
||||
onYieldDetected: () => void;
|
||||
onCodexAppServerEvent?: (event: CodexDynamicToolBuildEvent) => void;
|
||||
};
|
||||
@@ -98,13 +96,6 @@ export function resolveOpenClawCodingToolsSessionKeys(
|
||||
};
|
||||
}
|
||||
|
||||
/** Returns the canonical channel used for Codex message routing and receipts. */
|
||||
export function resolveCodexMessageToolProvider(
|
||||
params: Pick<EmbeddedRunAttemptParams, "messageChannel" | "messageProvider">,
|
||||
): string | undefined {
|
||||
return params.messageChannel ?? params.messageProvider;
|
||||
}
|
||||
|
||||
/** Resolves the channel id that hook events should target for this Codex app-server turn. */
|
||||
export function resolveCodexAppServerHookChannelId(
|
||||
params: EmbeddedRunAttemptParams,
|
||||
@@ -130,11 +121,6 @@ type CodexDynamicToolBuildStageSummary = {
|
||||
stages: CodexDynamicToolBuildStageTiming[];
|
||||
};
|
||||
|
||||
type CodexDynamicToolBuildStageTracker = {
|
||||
mark: (name: string) => void;
|
||||
snapshot: () => CodexDynamicToolBuildStageSummary;
|
||||
};
|
||||
|
||||
const CODEX_DYNAMIC_TOOL_BUILD_WARN_TOTAL_MS = 1_000;
|
||||
const CODEX_DYNAMIC_TOOL_BUILD_WARN_STAGE_MS = 500;
|
||||
|
||||
@@ -196,42 +182,17 @@ export function formatCodexDynamicToolBuildStageSummary(
|
||||
: "none";
|
||||
}
|
||||
|
||||
/** Builds the turn-visible and durable registration views from one OpenClaw tool catalog. */
|
||||
export async function prepareDynamicToolCatalog(input: DynamicToolBuildParams): Promise<{
|
||||
tools: OpenClawDynamicTool[];
|
||||
registeredTools: OpenClawDynamicTool[];
|
||||
}> {
|
||||
/** Builds, filters, and normalizes Codex-compatible runtime tools for a single turn. */
|
||||
export async function buildDynamicTools(input: DynamicToolBuildParams) {
|
||||
const { params } = input;
|
||||
if (params.disableTools || !supportsModelTools(params.model)) {
|
||||
return { tools: [], registeredTools: [] };
|
||||
return [];
|
||||
}
|
||||
// Dynamic tool construction is on the reply hot path, so per-stage
|
||||
// Date.now/span bookkeeping runs only when the Codex profiler flag is set.
|
||||
const toolBuildStages = createCodexDynamicToolBuildStageTracker({
|
||||
enabled: input.profilerEnabled,
|
||||
});
|
||||
// The durable schema must include heartbeat_respond across normal and heartbeat
|
||||
// turns. Build that superset once, then hide it only from normal turn exposure.
|
||||
const allTools = await buildOpenClawDynamicToolSource(input, toolBuildStages);
|
||||
const readableTools = filterProviderNormalizableTools(allTools);
|
||||
toolBuildStages.mark("provider-normalization");
|
||||
const tools = projectDynamicTools(input, readableTools, toolBuildStages, {
|
||||
excludeHeartbeatTool: params.trigger !== "heartbeat",
|
||||
phase: "runtime-tools",
|
||||
stagePrefix: "runtime",
|
||||
});
|
||||
const registeredTools = projectDynamicTools(input, readableTools, toolBuildStages, {
|
||||
ignoreRuntimePlan: true,
|
||||
phase: "registered-tools",
|
||||
reportDiagnostics: false,
|
||||
stagePrefix: "registered",
|
||||
});
|
||||
return { tools, registeredTools };
|
||||
}
|
||||
|
||||
async function buildOpenClawDynamicToolSource(
|
||||
input: DynamicToolBuildParams,
|
||||
toolBuildStages: CodexDynamicToolBuildStageTracker,
|
||||
): Promise<OpenClawDynamicTool[]> {
|
||||
const { params } = input;
|
||||
const modelHasVision = params.model.input?.includes("image") ?? false;
|
||||
const agentDir = params.agentDir ?? resolveAgentDir(params.config ?? {}, input.sessionAgentId);
|
||||
const createOpenClawCodingTools =
|
||||
@@ -248,8 +209,7 @@ async function buildOpenClawDynamicToolSource(
|
||||
elevated: params.bashElevated,
|
||||
},
|
||||
sandbox: input.sandbox,
|
||||
messageProvider: resolveCodexMessageToolProvider(params),
|
||||
toolPolicyMessageProvider: params.messageProvider ?? params.messageChannel,
|
||||
messageProvider: params.messageChannel ?? params.messageProvider,
|
||||
agentAccountId: params.agentAccountId,
|
||||
messageTo: params.messageTo,
|
||||
messageThreadId: params.messageThreadId,
|
||||
@@ -298,7 +258,6 @@ async function buildOpenClawDynamicToolSource(
|
||||
),
|
||||
suppressManagedWebSearch: false,
|
||||
currentChannelId: params.currentChannelId,
|
||||
currentMessagingTarget: params.currentMessagingTarget,
|
||||
hookChannelId: resolveCodexAppServerHookChannelId(params, input.sandboxSessionKey),
|
||||
currentThreadTs: params.currentThreadTs,
|
||||
currentMessageId: params.currentMessageId,
|
||||
@@ -310,8 +269,8 @@ async function buildOpenClawDynamicToolSource(
|
||||
sourceReplyDeliveryMode: params.sourceReplyDeliveryMode,
|
||||
disableMessageTool: params.disableMessageTool,
|
||||
forceMessageTool: shouldForceMessageTool(params),
|
||||
enableHeartbeatTool: true,
|
||||
forceHeartbeatTool: true,
|
||||
enableHeartbeatTool: params.trigger === "heartbeat" || input.forceHeartbeatTool === true,
|
||||
forceHeartbeatTool: params.trigger === "heartbeat" || input.forceHeartbeatTool === true,
|
||||
onYield: (message) => {
|
||||
input.onYieldDetected();
|
||||
input.onCodexAppServerEvent?.({
|
||||
@@ -324,30 +283,10 @@ async function buildOpenClawDynamicToolSource(
|
||||
},
|
||||
});
|
||||
toolBuildStages.mark("create-openclaw-coding-tools");
|
||||
return allTools;
|
||||
}
|
||||
|
||||
function projectDynamicTools(
|
||||
input: DynamicToolBuildParams,
|
||||
source: OpenClawDynamicToolProjection,
|
||||
toolBuildStages: CodexDynamicToolBuildStageTracker,
|
||||
options: {
|
||||
excludeHeartbeatTool?: boolean;
|
||||
ignoreRuntimePlan?: boolean;
|
||||
phase?: "runtime-tools" | "registered-tools";
|
||||
reportDiagnostics?: boolean;
|
||||
stagePrefix?: string;
|
||||
} = {},
|
||||
): OpenClawDynamicTool[] {
|
||||
const { params } = input;
|
||||
const markStage = (name: string) =>
|
||||
toolBuildStages.mark(options.stagePrefix ? `${options.stagePrefix}-${name}` : name);
|
||||
const preNormalizationDiagnostics: RuntimeToolSchemaDiagnostic[] = [...source.diagnostics];
|
||||
const readableAllTools = [...source.tools].filter(
|
||||
(tool) =>
|
||||
!options.excludeHeartbeatTool ||
|
||||
normalizeCodexDynamicToolName(tool.name) !== CODEX_HEARTBEAT_DYNAMIC_TOOL_NAME,
|
||||
);
|
||||
const preNormalizationDiagnostics: RuntimeToolSchemaDiagnostic[] = [];
|
||||
const readableAllToolProjection = filterProviderNormalizableTools(allTools);
|
||||
preNormalizationDiagnostics.push(...readableAllToolProjection.diagnostics);
|
||||
const readableAllTools = [...readableAllToolProjection.tools];
|
||||
const codexFilteredTools = addNodeShellDynamicToolsIfNeeded(
|
||||
addSandboxShellDynamicToolsIfAvailable(
|
||||
isCodexMemoryFlushRun(params)
|
||||
@@ -359,18 +298,17 @@ function projectDynamicTools(
|
||||
readableAllTools,
|
||||
input,
|
||||
);
|
||||
markStage("codex-filtering");
|
||||
const modelHasVision = params.model.input?.includes("image") ?? false;
|
||||
toolBuildStages.mark("codex-filtering");
|
||||
const visionFilteredTools = filterToolsForVisionInputs(codexFilteredTools, {
|
||||
modelHasVision,
|
||||
hasInboundImages: (params.images?.length ?? 0) > 0,
|
||||
});
|
||||
markStage("vision-filtering");
|
||||
toolBuildStages.mark("vision-filtering");
|
||||
const toolsAllow = includeForcedCodexDynamicToolAllow(params.toolsAllow, params);
|
||||
const filteredTools = filterCodexDynamicToolsForAllowlist(visionFilteredTools, toolsAllow);
|
||||
markStage("allowlist-filter");
|
||||
toolBuildStages.mark("allowlist-filter");
|
||||
const normalizedTools = normalizeAgentRuntimeTools({
|
||||
runtimePlan: options.ignoreRuntimePlan ? undefined : params.runtimePlan,
|
||||
runtimePlan: input.ignoreRuntimePlan ? undefined : params.runtimePlan,
|
||||
tools: filteredTools,
|
||||
provider: params.provider,
|
||||
config: params.config,
|
||||
@@ -379,14 +317,11 @@ function projectDynamicTools(
|
||||
modelId: params.modelId,
|
||||
modelApi: params.model.api,
|
||||
model: params.model,
|
||||
// Registration is a projection of the already-prepared catalog. Never
|
||||
// activate another provider runtime while constructing its durable schema.
|
||||
allowProviderRuntimePluginLoad: options.ignoreRuntimePlan ? false : undefined,
|
||||
onPreNormalizationSchemaDiagnostics: (diagnostics) =>
|
||||
preNormalizationDiagnostics.push(...diagnostics),
|
||||
});
|
||||
markStage("runtime-normalization");
|
||||
if (options.reportDiagnostics !== false && preNormalizationDiagnostics.length > 0) {
|
||||
toolBuildStages.mark("runtime-normalization");
|
||||
if (preNormalizationDiagnostics.length > 0) {
|
||||
embeddedAgentLog.warn(
|
||||
`codex app-server quarantined ${preNormalizationDiagnostics.length} unsupported runtime tool schema${preNormalizationDiagnostics.length === 1 ? "" : "s"} before dynamic tool registration`,
|
||||
{
|
||||
@@ -403,7 +338,7 @@ function projectDynamicTools(
|
||||
}
|
||||
const summary = toolBuildStages.snapshot();
|
||||
if (shouldWarnCodexDynamicToolBuildStageSummary(summary)) {
|
||||
const phase = options.phase ?? "runtime-tools";
|
||||
const phase = input.forceHeartbeatTool ? "registered-tools" : "runtime-tools";
|
||||
embeddedAgentLog.warn(
|
||||
`codex app-server dynamic tool build timings runId=${params.runId} sessionId=${params.sessionId} phase=${phase} totalMs=${summary.totalMs} stages=${formatCodexDynamicToolBuildStageSummary(summary)}`,
|
||||
{
|
||||
@@ -417,7 +352,8 @@ function projectDynamicTools(
|
||||
visionFilteredToolCount: visionFilteredTools.length,
|
||||
filteredToolCount: filteredTools.length,
|
||||
normalizedToolCount: normalizedTools.length,
|
||||
ignoreRuntimePlan: options.ignoreRuntimePlan === true,
|
||||
forceHeartbeatTool: input.forceHeartbeatTool === true,
|
||||
ignoreRuntimePlan: input.ignoreRuntimePlan === true,
|
||||
nativeToolSurfaceEnabled: input.nativeToolSurfaceEnabled === true,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -18,7 +18,6 @@ import {
|
||||
import {
|
||||
createEmptyPluginRegistry,
|
||||
createMockPluginRegistry,
|
||||
createTestRegistry,
|
||||
setActivePluginRegistry,
|
||||
} from "openclaw/plugin-sdk/plugin-test-runtime";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
@@ -799,163 +798,6 @@ describe("createCodexDynamicToolBridge", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("records the current provider and transport thread for implicit message sends", async () => {
|
||||
const hasRepliedRef = { value: false };
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "slack",
|
||||
plugin: {
|
||||
id: "slack",
|
||||
messaging: { normalizeTarget: (raw: string) => raw.trim().toLowerCase() },
|
||||
threading: {
|
||||
resolveAutoThreadId: ({
|
||||
to,
|
||||
toolContext,
|
||||
}: {
|
||||
to: string;
|
||||
toolContext?: {
|
||||
currentChannelId?: string;
|
||||
currentMessagingTarget?: string;
|
||||
currentThreadTs?: string;
|
||||
replyToMode?: "off" | "first" | "all" | "batched";
|
||||
hasRepliedRef?: { value: boolean };
|
||||
};
|
||||
}) => {
|
||||
if (
|
||||
to !== toolContext?.currentMessagingTarget &&
|
||||
to !== toolContext?.currentChannelId
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
if (
|
||||
(toolContext?.replyToMode === "first" ||
|
||||
toolContext?.replyToMode === "batched") &&
|
||||
!toolContext.hasRepliedRef?.value
|
||||
) {
|
||||
return toolContext.currentThreadTs;
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
},
|
||||
},
|
||||
source: "test",
|
||||
},
|
||||
]),
|
||||
);
|
||||
const bridge = createCodexDynamicToolBridge({
|
||||
tools: [
|
||||
createTool({
|
||||
name: "message",
|
||||
execute: vi.fn(async () => {
|
||||
hasRepliedRef.value = true;
|
||||
return textToolResult("Sent.");
|
||||
}),
|
||||
}),
|
||||
],
|
||||
signal: new AbortController().signal,
|
||||
hookContext: {
|
||||
currentChannelProvider: "slack",
|
||||
currentChannelId: "D1",
|
||||
currentMessagingTarget: "user:u1",
|
||||
currentThreadId: "171.222",
|
||||
replyToMode: "first",
|
||||
hasRepliedRef,
|
||||
},
|
||||
});
|
||||
|
||||
await handleMessageToolCall(bridge, {
|
||||
action: "send",
|
||||
to: "user:U1",
|
||||
text: "hello from Codex",
|
||||
});
|
||||
|
||||
expect(bridge.telemetry.messagingToolSentTargets).toEqual([
|
||||
{
|
||||
tool: "message",
|
||||
provider: "slack",
|
||||
to: "user:u1",
|
||||
threadId: "171.222",
|
||||
threadImplicit: true,
|
||||
text: "hello from Codex",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("records the provider-confirmed route for successful message sends", async () => {
|
||||
const registry = createTestRegistry([
|
||||
{
|
||||
pluginId: "mattermost",
|
||||
plugin: {
|
||||
id: "mattermost",
|
||||
messaging: { normalizeTarget: (raw: string) => raw.trim().toLowerCase() },
|
||||
actions: {
|
||||
extractToolSend: ({ args }: { args: Record<string, unknown> }) =>
|
||||
args.action === "send" && typeof args.to === "string"
|
||||
? { to: args.to, threadImplicit: true }
|
||||
: null,
|
||||
extractToolSendResult: ({ result }: { result: unknown }) => {
|
||||
const details = requireRecord(
|
||||
requireRecord(result, "message result").details,
|
||||
"message details",
|
||||
);
|
||||
const toolSend = requireRecord(details.toolSend, "tool send details");
|
||||
return {
|
||||
to: String(toolSend.to),
|
||||
threadId: String(toolSend.threadId),
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
source: "test",
|
||||
},
|
||||
]);
|
||||
const middleware = vi.fn(async (event: { result: AgentToolResult<unknown> }) => {
|
||||
const details = requireRecord(event.result.details, "middleware details");
|
||||
const toolSend = requireRecord(details.toolSend, "middleware tool send");
|
||||
toolSend.to = "channel:corrupted";
|
||||
toolSend.threadId = "corrupted-root";
|
||||
return undefined;
|
||||
});
|
||||
registry.agentToolResultMiddlewares.push({
|
||||
pluginId: "route-details-stripper",
|
||||
pluginName: "Route details stripper",
|
||||
rawHandler: middleware,
|
||||
handler: middleware,
|
||||
runtimes: ["codex"],
|
||||
source: "test",
|
||||
});
|
||||
setActivePluginRegistry(registry);
|
||||
const bridge = createBridgeWithToolResult(
|
||||
"message",
|
||||
textToolResult("Sent.", {
|
||||
toolSend: {
|
||||
to: "channel:resolved-id",
|
||||
threadId: "root-post-id",
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await handleMessageToolCall(bridge, {
|
||||
action: "send",
|
||||
provider: "mattermost",
|
||||
to: "town-square",
|
||||
text: "hello from Codex",
|
||||
});
|
||||
|
||||
expect(bridge.telemetry.messagingToolSentTargets).toEqual([
|
||||
{
|
||||
tool: "message",
|
||||
provider: "mattermost",
|
||||
to: "channel:resolved-id",
|
||||
threadId: "root-post-id",
|
||||
threadImplicit: undefined,
|
||||
threadSuppressed: undefined,
|
||||
text: "hello from Codex",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("records message tool media attachment aliases as delivery evidence", async () => {
|
||||
const toolResult = {
|
||||
content: [{ type: "text", text: "Sent." }],
|
||||
|
||||
@@ -6,8 +6,6 @@ import type { AgentToolResult } from "openclaw/plugin-sdk/agent-core";
|
||||
import {
|
||||
createAgentToolResultMiddlewareRunner,
|
||||
createCodexAppServerToolResultExtensionRunner,
|
||||
extractMessagingToolSend,
|
||||
extractMessagingToolSendResult,
|
||||
extractToolResultMediaArtifact,
|
||||
filterToolResultMediaUrls,
|
||||
HEARTBEAT_RESPONSE_TOOL_NAME,
|
||||
@@ -53,22 +51,10 @@ type CodexDynamicToolHookContext = {
|
||||
sessionKey?: string;
|
||||
runId?: string;
|
||||
channelId?: string;
|
||||
currentChannelProvider?: string;
|
||||
currentChannelId?: string;
|
||||
currentMessagingTarget?: string;
|
||||
currentThreadId?: string;
|
||||
replyToMode?: "off" | "first" | "all" | "batched";
|
||||
hasRepliedRef?: { value: boolean };
|
||||
};
|
||||
|
||||
type CodexToolResultHookContext = Omit<CodexDynamicToolHookContext, "config">;
|
||||
|
||||
type AgentToolResultObserver = (event: {
|
||||
toolName: string;
|
||||
result: unknown;
|
||||
isError: boolean;
|
||||
}) => void;
|
||||
|
||||
type ProjectedCodexDynamicTool = {
|
||||
tool: AnyAgentTool;
|
||||
name: string;
|
||||
@@ -81,22 +67,6 @@ type CodexDynamicToolSchemaQuarantine = {
|
||||
violations: readonly string[];
|
||||
};
|
||||
|
||||
function applyCurrentMessageProvider(
|
||||
toolName: string,
|
||||
args: Record<string, unknown>,
|
||||
currentProvider: string | undefined,
|
||||
): Record<string, unknown> {
|
||||
const hasProvider =
|
||||
typeof args.provider === "string" && args.provider.trim().length > 0
|
||||
? true
|
||||
: typeof args.channel === "string" && args.channel.trim().length > 0;
|
||||
const provider = currentProvider?.trim();
|
||||
if (toolName !== "message" || hasProvider || !provider) {
|
||||
return args;
|
||||
}
|
||||
return { ...args, provider };
|
||||
}
|
||||
|
||||
/** Runtime bridge returned to Codex app-server attempt code. */
|
||||
export type CodexDynamicToolBridge = {
|
||||
availableSpecs: CodexDynamicToolSpec[];
|
||||
@@ -105,7 +75,7 @@ export type CodexDynamicToolBridge = {
|
||||
params: CodexDynamicToolCallParams,
|
||||
options?: {
|
||||
signal?: AbortSignal;
|
||||
onAgentToolResult?: AgentToolResultObserver;
|
||||
onAgentToolResult?: EmbeddedRunAttemptParams["onAgentToolResult"];
|
||||
},
|
||||
) => Promise<CodexDynamicToolCallResponse>;
|
||||
telemetry: {
|
||||
@@ -243,30 +213,9 @@ export function createCodexDynamicToolBridge(params: {
|
||||
// Prepare before marking side-effect evidence; argument preparation can
|
||||
// fail without the target tool actually starting.
|
||||
const preparedArgs = tool.prepareArguments ? tool.prepareArguments(args) : args;
|
||||
const telemetryArgs = isRecord(preparedArgs) ? preparedArgs : args;
|
||||
const messagingTelemetryArgs = applyCurrentMessageProvider(
|
||||
toolName,
|
||||
telemetryArgs,
|
||||
params.hookContext?.currentChannelProvider,
|
||||
);
|
||||
const messagingTarget =
|
||||
isMessagingTool(toolName) && isMessagingToolSendAction(toolName, telemetryArgs)
|
||||
? extractMessagingToolSend(toolName, messagingTelemetryArgs, {
|
||||
config: params.hookContext?.config,
|
||||
currentChannelId: params.hookContext?.currentChannelId,
|
||||
currentMessagingTarget: params.hookContext?.currentMessagingTarget,
|
||||
currentThreadId: params.hookContext?.currentThreadId,
|
||||
replyToMode: params.hookContext?.replyToMode,
|
||||
hasRepliedRef: params.hookContext?.hasRepliedRef,
|
||||
})
|
||||
: undefined;
|
||||
didStartExecution = true;
|
||||
const rawResult = await tool.execute(call.callId, preparedArgs, signal);
|
||||
const rawIsError = isCodexToolResultError(rawResult);
|
||||
const confirmedMessagingTarget =
|
||||
!rawIsError && messagingTarget
|
||||
? extractMessagingToolSendResult(messagingTarget, rawResult)
|
||||
: messagingTarget;
|
||||
const middlewareResult = await middlewareRunner.applyToolResultMiddleware({
|
||||
threadId: call.threadId,
|
||||
turnId: call.turnId,
|
||||
@@ -288,12 +237,11 @@ export function createCodexDynamicToolBridge(params: {
|
||||
notifyAgentToolResult(options?.onAgentToolResult, toolName, result, resultIsError);
|
||||
collectToolTelemetry({
|
||||
toolName,
|
||||
args: telemetryArgs,
|
||||
args,
|
||||
result,
|
||||
mediaTrustResult: rawResult,
|
||||
telemetry,
|
||||
isError: resultIsError,
|
||||
messagingTarget: confirmedMessagingTarget,
|
||||
});
|
||||
void runAgentHarnessAfterToolCallHook({
|
||||
toolName,
|
||||
@@ -375,7 +323,7 @@ export function createCodexDynamicToolBridge(params: {
|
||||
}
|
||||
|
||||
function notifyAgentToolResult(
|
||||
observer: AgentToolResultObserver | undefined,
|
||||
observer: EmbeddedRunAttemptParams["onAgentToolResult"] | undefined,
|
||||
toolName: string,
|
||||
result: unknown,
|
||||
isError: boolean,
|
||||
@@ -683,7 +631,6 @@ function collectToolTelemetry(params: {
|
||||
mediaTrustResult?: AgentToolResult<unknown>;
|
||||
telemetry: CodexDynamicToolBridge["telemetry"];
|
||||
isError: boolean;
|
||||
messagingTarget?: MessagingToolSend;
|
||||
}): void {
|
||||
if (params.isError) {
|
||||
return;
|
||||
@@ -736,13 +683,11 @@ function collectToolTelemetry(params: {
|
||||
const mediaUrls = collectMediaUrls(params.args);
|
||||
params.telemetry.messagingToolSentMediaUrls.push(...mediaUrls);
|
||||
params.telemetry.messagingToolSentTargets.push({
|
||||
...(params.messagingTarget ?? {
|
||||
tool: params.toolName,
|
||||
provider: readFirstString(params.args, ["provider", "channel"]) ?? params.toolName,
|
||||
accountId: readFirstString(params.args, ["accountId", "account_id"]),
|
||||
to: readFirstString(params.args, ["to", "target", "recipient"]),
|
||||
threadId: readFirstString(params.args, ["threadId", "thread_id", "messageThreadId"]),
|
||||
}),
|
||||
tool: params.toolName,
|
||||
provider: readFirstString(params.args, ["provider", "channel"]) ?? params.toolName,
|
||||
accountId: readFirstString(params.args, ["accountId", "account_id"]),
|
||||
to: readFirstString(params.args, ["to", "target", "recipient"]),
|
||||
threadId: readFirstString(params.args, ["threadId", "thread_id", "messageThreadId"]),
|
||||
...(text ? { text } : {}),
|
||||
...(mediaUrls.length > 0 ? { mediaUrls } : {}),
|
||||
});
|
||||
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
type CodexAppServerEventProjectorOptions,
|
||||
type CodexAppServerToolTelemetry,
|
||||
} from "./event-projector.js";
|
||||
import { rememberCodexRateLimits, resetCodexRateLimitCacheForTests } from "./rate-limit-cache.js";
|
||||
import { createCodexTestModel } from "./test-support.js";
|
||||
|
||||
const THREAD_ID = "thread-1";
|
||||
@@ -107,6 +108,7 @@ afterEach(async () => {
|
||||
resetAgentEventsForTest();
|
||||
resetDiagnosticEventsForTest();
|
||||
resetGlobalHookRunner();
|
||||
resetCodexRateLimitCacheForTests();
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllEnvs();
|
||||
for (const tempDir of tempDirs) {
|
||||
@@ -859,11 +861,10 @@ describe("CodexAppServerEventProjector", () => {
|
||||
});
|
||||
|
||||
it("uses Codex rate-limit resets for usage-limit app-server errors", async () => {
|
||||
const projector = await createProjector();
|
||||
const resetsAt = Math.ceil(Date.now() / 1000) + 120;
|
||||
const projector = await createProjector(undefined, {
|
||||
readRecentRateLimits: () => rateLimitsUpdated(resetsAt).params,
|
||||
});
|
||||
|
||||
await projector.handleNotification(rateLimitsUpdated(resetsAt));
|
||||
await projector.handleNotification(
|
||||
forCurrentTurn("error", {
|
||||
error: {
|
||||
@@ -884,11 +885,10 @@ describe("CodexAppServerEventProjector", () => {
|
||||
});
|
||||
|
||||
it("uses Codex rate-limit resets for failed turns", async () => {
|
||||
const projector = await createProjector();
|
||||
const resetsAt = Math.ceil(Date.now() / 1000) + 120;
|
||||
const projector = await createProjector(undefined, {
|
||||
readRecentRateLimits: () => rateLimitsUpdated(resetsAt).params,
|
||||
});
|
||||
|
||||
await projector.handleNotification(rateLimitsUpdated(resetsAt));
|
||||
await projector.handleNotification(
|
||||
forCurrentTurn("turn/completed", {
|
||||
turn: {
|
||||
@@ -912,8 +912,9 @@ describe("CodexAppServerEventProjector", () => {
|
||||
});
|
||||
|
||||
it("uses a recent Codex rate-limit snapshot when failed turns omit reset details", async () => {
|
||||
const projector = await createProjector();
|
||||
const resetsAt = Math.ceil(Date.now() / 1000) + 120;
|
||||
const rateLimits = {
|
||||
rememberCodexRateLimits({
|
||||
rateLimits: {
|
||||
limitId: "codex",
|
||||
limitName: "Codex",
|
||||
@@ -924,9 +925,6 @@ describe("CodexAppServerEventProjector", () => {
|
||||
rateLimitReachedType: "rate_limit_reached",
|
||||
},
|
||||
rateLimitsByLimitId: null,
|
||||
};
|
||||
const projector = await createProjector(undefined, {
|
||||
readRecentRateLimits: () => rateLimits,
|
||||
});
|
||||
|
||||
await projector.handleNotification(
|
||||
@@ -978,19 +976,19 @@ describe("CodexAppServerEventProjector", () => {
|
||||
expect(result.promptErrorSource).toBe("prompt");
|
||||
});
|
||||
|
||||
it("normalizes current app-server token usage", async () => {
|
||||
it("normalizes snake_case current token usage fields", async () => {
|
||||
const projector = await createProjector();
|
||||
|
||||
await projector.handleNotification(agentMessageDelta("done"));
|
||||
await projector.handleNotification(
|
||||
forCurrentTurn("thread/tokenUsage/updated", {
|
||||
tokenUsage: {
|
||||
total: { totalTokens: 1_000_000 },
|
||||
last: {
|
||||
totalTokens: 17,
|
||||
inputTokens: 8,
|
||||
cachedInputTokens: 3,
|
||||
outputTokens: 9,
|
||||
total: { total_tokens: 1_000_000 },
|
||||
last_token_usage: {
|
||||
total_tokens: 17,
|
||||
input_tokens: 8,
|
||||
cached_input_tokens: 3,
|
||||
output_tokens: 9,
|
||||
},
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -26,7 +26,10 @@ import type { AssistantMessage, Usage } from "openclaw/plugin-sdk/llm";
|
||||
import { saveMediaBuffer } from "openclaw/plugin-sdk/media-store";
|
||||
import { asDateTimestampMs } from "openclaw/plugin-sdk/number-runtime";
|
||||
import { resolveCodexLocalRuntimeAttribution } from "./local-runtime-attribution.js";
|
||||
import { isCodexNotificationForTurn } from "./notification-correlation.js";
|
||||
import {
|
||||
readCodexNotificationThreadId,
|
||||
readCodexNotificationTurnId,
|
||||
} from "./notification-correlation.js";
|
||||
import { readCodexTurn } from "./protocol-validators.js";
|
||||
import {
|
||||
isJsonObject,
|
||||
@@ -37,6 +40,7 @@ import {
|
||||
type JsonObject,
|
||||
type JsonValue,
|
||||
} from "./protocol.js";
|
||||
import { readRecentCodexRateLimits, rememberCodexRateLimits } from "./rate-limit-cache.js";
|
||||
import { formatCodexUsageLimitErrorMessage } from "./rate-limits.js";
|
||||
import { readCodexMirroredSessionHistoryMessages } from "./session-history.js";
|
||||
import {
|
||||
@@ -61,7 +65,6 @@ export type CodexAppServerToolTelemetry = {
|
||||
|
||||
export type CodexAppServerEventProjectorOptions = {
|
||||
nativePostToolUseRelayEnabled?: boolean;
|
||||
readRecentRateLimits?: () => JsonValue | undefined;
|
||||
trajectoryRecorder?: CodexTrajectoryRecorder | null;
|
||||
};
|
||||
|
||||
@@ -89,6 +92,22 @@ const ZERO_USAGE: Usage = {
|
||||
},
|
||||
};
|
||||
|
||||
const CURRENT_TOKEN_USAGE_KEYS = [
|
||||
"last",
|
||||
"current",
|
||||
"lastCall",
|
||||
"lastCallUsage",
|
||||
"lastTokenUsage",
|
||||
"last_token_usage",
|
||||
] as const;
|
||||
|
||||
const CODEX_PROMPT_TOTAL_INPUT_KEYS = [
|
||||
"inputTokens",
|
||||
"input_tokens",
|
||||
"promptTokens",
|
||||
"prompt_tokens",
|
||||
] as const;
|
||||
|
||||
const MAX_TOOL_OUTPUT_DELTA_MESSAGES_PER_ITEM = 20;
|
||||
const TOOL_TRANSCRIPT_OUTPUT_MAX_CHARS = 12_000;
|
||||
const MISSING_TOOL_RESULT_ERROR =
|
||||
@@ -179,6 +198,8 @@ export class CodexAppServerEventProjector {
|
||||
private tokenUsage: ReturnType<typeof normalizeUsage>;
|
||||
private guardianReviewCount = 0;
|
||||
private completedCompactionCount = 0;
|
||||
private latestRateLimits: JsonValue | undefined;
|
||||
|
||||
constructor(
|
||||
private readonly params: EmbeddedRunAttemptParams,
|
||||
private readonly threadId: string,
|
||||
@@ -200,6 +221,11 @@ export class CodexAppServerEventProjector {
|
||||
if (!params) {
|
||||
return;
|
||||
}
|
||||
if (notification.method === "account/rateLimits/updated") {
|
||||
this.latestRateLimits = params;
|
||||
rememberCodexRateLimits(params);
|
||||
return;
|
||||
}
|
||||
if (isHookNotificationMethod(notification.method)) {
|
||||
if (!this.isHookNotificationForCurrentThread(params)) {
|
||||
return;
|
||||
@@ -252,7 +278,7 @@ export class CodexAppServerEventProjector {
|
||||
await this.handleRawResponseItemCompleted(params);
|
||||
break;
|
||||
case "error":
|
||||
if (params.willRetry === true) {
|
||||
if (readBooleanAlias(params, ["willRetry", "will_retry"]) === true) {
|
||||
break;
|
||||
}
|
||||
this.promptError = this.formatCodexErrorMessage(params) ?? "codex app-server error";
|
||||
@@ -645,7 +671,9 @@ export class CodexAppServerEventProjector {
|
||||
|
||||
private handleTokenUsage(params: JsonObject): void {
|
||||
const tokenUsage = isJsonObject(params.tokenUsage) ? params.tokenUsage : undefined;
|
||||
const current = tokenUsage && isJsonObject(tokenUsage.last) ? tokenUsage.last : undefined;
|
||||
const current =
|
||||
(tokenUsage ? readFirstJsonObject(tokenUsage, CURRENT_TOKEN_USAGE_KEYS) : undefined) ??
|
||||
readFirstJsonObject(params, CURRENT_TOKEN_USAGE_KEYS);
|
||||
if (!current) {
|
||||
return;
|
||||
}
|
||||
@@ -716,7 +744,7 @@ export class CodexAppServerEventProjector {
|
||||
formatCodexUsageLimitErrorMessage({
|
||||
message: turn.error?.message,
|
||||
codexErrorInfo: turn.error?.codexErrorInfo as JsonValue | null | undefined,
|
||||
rateLimits: this.options.readRecentRateLimits?.(),
|
||||
rateLimits: this.latestRateLimits ?? readRecentCodexRateLimits(),
|
||||
}) ??
|
||||
turn.error?.message ??
|
||||
"codex app-server turn failed";
|
||||
@@ -1583,7 +1611,7 @@ export class CodexAppServerEventProjector {
|
||||
formatCodexUsageLimitErrorMessage({
|
||||
message: error ? readString(error, "message") : undefined,
|
||||
codexErrorInfo: error?.codexErrorInfo,
|
||||
rateLimits: this.options.readRecentRateLimits?.(),
|
||||
rateLimits: this.latestRateLimits ?? readRecentCodexRateLimits(),
|
||||
}) ?? readCodexErrorNotificationMessage(params)
|
||||
);
|
||||
}
|
||||
@@ -1758,7 +1786,9 @@ export class CodexAppServerEventProjector {
|
||||
}
|
||||
|
||||
private isNotificationForTurn(params: JsonObject): boolean {
|
||||
return isCodexNotificationForTurn(params, this.threadId, this.turnId);
|
||||
const threadId = readCodexNotificationThreadId(params);
|
||||
const turnId = readNotificationTurnId(params);
|
||||
return threadId === this.threadId && turnId === this.turnId;
|
||||
}
|
||||
|
||||
private isHookNotificationForCurrentThread(params: JsonObject): boolean {
|
||||
@@ -1772,6 +1802,10 @@ function isHookNotificationMethod(method: string): method is "hook/started" | "h
|
||||
return method === "hook/started" || method === "hook/completed";
|
||||
}
|
||||
|
||||
function readNotificationTurnId(record: JsonObject): string | undefined {
|
||||
return readCodexNotificationTurnId(record);
|
||||
}
|
||||
|
||||
function readString(record: JsonObject, key: string): string | undefined {
|
||||
const value = record[key];
|
||||
return typeof value === "string" ? value : undefined;
|
||||
@@ -1861,6 +1895,21 @@ function readNonNegativeInteger(record: JsonObject, key: string): number | undef
|
||||
return value !== undefined && Number.isInteger(value) && value >= 0 ? value : undefined;
|
||||
}
|
||||
|
||||
function readBoolean(record: JsonObject, key: string): boolean | undefined {
|
||||
const value = record[key];
|
||||
return typeof value === "boolean" ? value : undefined;
|
||||
}
|
||||
|
||||
function readBooleanAlias(record: JsonObject, keys: readonly string[]): boolean | undefined {
|
||||
for (const key of keys) {
|
||||
const value = readBoolean(record, key);
|
||||
if (value !== undefined) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function readCodexErrorNotificationMessage(record: JsonObject): string | undefined {
|
||||
const error = record.error;
|
||||
if (isJsonObject(error)) {
|
||||
@@ -1888,19 +1937,52 @@ function readHookOutputEntries(
|
||||
});
|
||||
}
|
||||
|
||||
function readFirstJsonObject(record: JsonObject, keys: readonly string[]): JsonObject | undefined {
|
||||
for (const key of keys) {
|
||||
const value = record[key];
|
||||
if (isJsonObject(value)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function readNumberAlias(record: JsonObject, keys: readonly string[]): number | undefined {
|
||||
for (const key of keys) {
|
||||
const value = readNumber(record, key);
|
||||
if (value !== undefined) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function normalizeCodexTokenUsage(record: JsonObject): ReturnType<typeof normalizeUsage> {
|
||||
const promptTotalInput = readNumber(record, "inputTokens");
|
||||
const cacheRead = readNumber(record, "cachedInputTokens");
|
||||
const promptTotalInput = readNumberAlias(record, CODEX_PROMPT_TOTAL_INPUT_KEYS);
|
||||
const cacheRead = readNumberAlias(record, [
|
||||
"cachedInputTokens",
|
||||
"cached_input_tokens",
|
||||
"cacheRead",
|
||||
"cache_read",
|
||||
"cache_read_input_tokens",
|
||||
"cached_tokens",
|
||||
]);
|
||||
const input =
|
||||
promptTotalInput !== undefined && cacheRead !== undefined
|
||||
? Math.max(0, promptTotalInput - cacheRead)
|
||||
: promptTotalInput;
|
||||
: (promptTotalInput ?? readNumber(record, "input"));
|
||||
|
||||
return normalizeUsage({
|
||||
input,
|
||||
output: readNumber(record, "outputTokens"),
|
||||
output: readNumberAlias(record, ["outputTokens", "output_tokens", "output"]),
|
||||
cacheRead,
|
||||
total: readNumber(record, "totalTokens"),
|
||||
cacheWrite: readNumberAlias(record, [
|
||||
"cacheWrite",
|
||||
"cache_write",
|
||||
"cacheCreationInputTokens",
|
||||
"cache_creation_input_tokens",
|
||||
]),
|
||||
total: readNumberAlias(record, ["totalTokens", "total_tokens", "total"]),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,6 @@ import type { CodexAppServerClient } from "./client.js";
|
||||
import type { CodexAppServerStartOptions } from "./config.js";
|
||||
import { readCodexModelListResponse } from "./protocol-validators.js";
|
||||
import type { CodexModel, CodexReasoningEffortOption } from "./protocol.js";
|
||||
import {
|
||||
createIsolatedCodexAppServerClient,
|
||||
leaseSharedCodexAppServerClient,
|
||||
} from "./shared-client.js";
|
||||
|
||||
/** Normalized model metadata returned by the Codex app-server model listing helper. */
|
||||
export type CodexAppServerModel = {
|
||||
@@ -40,11 +36,10 @@ export type CodexAppServerListModelsOptions = {
|
||||
includeHidden?: boolean;
|
||||
timeoutMs?: number;
|
||||
startOptions?: CodexAppServerStartOptions;
|
||||
authProfileId?: string | null;
|
||||
authProfileId?: string;
|
||||
agentDir?: string;
|
||||
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
|
||||
sharedClient?: boolean;
|
||||
signal?: AbortSignal;
|
||||
};
|
||||
|
||||
/** Lists one Codex app-server model page using the configured auth/client options. */
|
||||
@@ -59,37 +54,27 @@ export async function listCodexAppServerModels(
|
||||
/** Walks Codex app-server model pages until exhaustion or the max-page guard. */
|
||||
export async function listAllCodexAppServerModels(
|
||||
options: CodexAppServerListModelsOptions & { maxPages?: number } = {},
|
||||
): Promise<CodexAppServerModelListResult> {
|
||||
return await withCodexAppServerModelClient(options, async ({ client, timeoutMs }) =>
|
||||
listAllCodexAppServerModelsWithClient(client, { ...options, timeoutMs }),
|
||||
);
|
||||
}
|
||||
|
||||
/** Walks all model pages on an already-owned physical app-server client. */
|
||||
export async function listAllCodexAppServerModelsWithClient(
|
||||
client: CodexAppServerClient,
|
||||
options: CodexAppServerListModelsOptions & { maxPages?: number } = {},
|
||||
): Promise<CodexAppServerModelListResult> {
|
||||
const maxPages = normalizeMaxPages(options.maxPages);
|
||||
const timeoutMs = options.timeoutMs ?? 2500;
|
||||
const models: CodexAppServerModel[] = [];
|
||||
let cursor = options.cursor;
|
||||
let nextCursor: string | undefined;
|
||||
for (let page = 0; page < maxPages; page += 1) {
|
||||
options.signal?.throwIfAborted();
|
||||
const result = await requestModelListPage(client, {
|
||||
...options,
|
||||
timeoutMs,
|
||||
cursor,
|
||||
});
|
||||
models.push(...result.models);
|
||||
nextCursor = result.nextCursor;
|
||||
if (!nextCursor) {
|
||||
return { models };
|
||||
return await withCodexAppServerModelClient(options, async ({ client, timeoutMs }) => {
|
||||
const models: CodexAppServerModel[] = [];
|
||||
let cursor = options.cursor;
|
||||
let nextCursor: string | undefined;
|
||||
for (let page = 0; page < maxPages; page += 1) {
|
||||
const result = await requestModelListPage(client, {
|
||||
...options,
|
||||
timeoutMs,
|
||||
cursor,
|
||||
});
|
||||
models.push(...result.models);
|
||||
nextCursor = result.nextCursor;
|
||||
if (!nextCursor) {
|
||||
return { models };
|
||||
}
|
||||
cursor = nextCursor;
|
||||
}
|
||||
cursor = nextCursor;
|
||||
}
|
||||
return { models, nextCursor, truncated: true };
|
||||
return { models, nextCursor, truncated: true };
|
||||
});
|
||||
}
|
||||
|
||||
async function withCodexAppServerModelClient<T>(
|
||||
@@ -98,32 +83,33 @@ async function withCodexAppServerModelClient<T>(
|
||||
): Promise<T> {
|
||||
const timeoutMs = options.timeoutMs ?? 2500;
|
||||
const useSharedClient = options.sharedClient !== false;
|
||||
const clientLease = useSharedClient
|
||||
? await leaseSharedCodexAppServerClient({
|
||||
const {
|
||||
createIsolatedCodexAppServerClient,
|
||||
getLeasedSharedCodexAppServerClient,
|
||||
releaseLeasedSharedCodexAppServerClient,
|
||||
} = await import("./shared-client.js");
|
||||
const client = useSharedClient
|
||||
? await getLeasedSharedCodexAppServerClient({
|
||||
startOptions: options.startOptions,
|
||||
timeoutMs,
|
||||
authProfileId: options.authProfileId,
|
||||
agentDir: options.agentDir,
|
||||
config: options.config,
|
||||
abandonSignal: options.signal,
|
||||
})
|
||||
: undefined;
|
||||
const client =
|
||||
clientLease?.client ??
|
||||
(await createIsolatedCodexAppServerClient({
|
||||
startOptions: options.startOptions,
|
||||
timeoutMs,
|
||||
authProfileId: options.authProfileId,
|
||||
agentDir: options.agentDir,
|
||||
config: options.config,
|
||||
}));
|
||||
: await createIsolatedCodexAppServerClient({
|
||||
startOptions: options.startOptions,
|
||||
timeoutMs,
|
||||
authProfileId: options.authProfileId,
|
||||
agentDir: options.agentDir,
|
||||
config: options.config,
|
||||
});
|
||||
try {
|
||||
return await run({ client, timeoutMs });
|
||||
} finally {
|
||||
if (useSharedClient) {
|
||||
clientLease?.release();
|
||||
releaseLeasedSharedCodexAppServerClient(client);
|
||||
} else {
|
||||
await client.closeAndWait({ exitTimeoutMs: 2_000, forceKillDelayMs: 250 });
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -139,7 +125,7 @@ async function requestModelListPage(
|
||||
cursor: options.cursor ?? null,
|
||||
includeHidden: options.includeHidden ?? null,
|
||||
},
|
||||
{ timeoutMs: options.timeoutMs, signal: options.signal },
|
||||
{ timeoutMs: options.timeoutMs },
|
||||
);
|
||||
return readModelListResult(response);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,7 @@
|
||||
*/
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
||||
import { resolveSandboxRuntimeStatus } from "openclaw/plugin-sdk/sandbox";
|
||||
import {
|
||||
loadSessionStore,
|
||||
resolveSessionStoreEntry,
|
||||
resolveStorePath,
|
||||
type SessionEntry,
|
||||
} from "openclaw/plugin-sdk/session-store-runtime";
|
||||
import { getSessionEntry, type SessionEntry } from "openclaw/plugin-sdk/session-store-runtime";
|
||||
|
||||
type ExecHost = "sandbox" | "gateway" | "node";
|
||||
type ExecTarget = "auto" | ExecHost;
|
||||
@@ -49,21 +44,20 @@ export function resolveCodexNativeExecutionPolicy(params: {
|
||||
}): CodexNativeExecutionPolicy {
|
||||
const config = params.config ?? {};
|
||||
const sessionKey = params.sessionKey?.trim() || params.sessionId?.trim() || undefined;
|
||||
const agentId = resolvePolicyAgentId({ config, sessionKey, agentId: params.agentId });
|
||||
const sessionEntry =
|
||||
params.sessionEntry ??
|
||||
(params.readRuntimeSessionEntry && sessionKey
|
||||
? readRuntimeSessionEntryBestEffort(config, sessionKey, agentId)
|
||||
? readRuntimeSessionEntryBestEffort(sessionKey)
|
||||
: undefined);
|
||||
const sandboxAvailable =
|
||||
params.sandboxAvailable ??
|
||||
(sessionKey
|
||||
? resolveSandboxRuntimeStatus({
|
||||
cfg: config,
|
||||
agentId,
|
||||
sessionKey,
|
||||
}).sandboxed
|
||||
: false);
|
||||
const agentId = resolvePolicyAgentId({ config, sessionKey, agentId: params.agentId });
|
||||
const agentExec = resolvePolicyAgentExec({ config, agentId });
|
||||
const globalExec = config.tools?.exec;
|
||||
const requestedExecHost =
|
||||
@@ -200,17 +194,9 @@ function resolveEffectiveExecHost(params: {
|
||||
return params.requestedExecHost;
|
||||
}
|
||||
|
||||
function readRuntimeSessionEntryBestEffort(
|
||||
config: OpenClawConfig,
|
||||
sessionKey: string,
|
||||
agentId: string,
|
||||
): SessionEntry | undefined {
|
||||
function readRuntimeSessionEntryBestEffort(sessionKey: string): SessionEntry | undefined {
|
||||
try {
|
||||
const storePath = resolveStorePath(config.session?.store, { agentId });
|
||||
return resolveSessionStoreEntry({
|
||||
store: loadSessionStore(storePath, { skipCache: true }),
|
||||
sessionKey,
|
||||
}).existing;
|
||||
return getSessionEntry({ sessionKey, hydrateSkillPromptRefs: false });
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
addTimerTimeoutGraceMs,
|
||||
finiteSecondsToTimerSafeMilliseconds,
|
||||
} from "openclaw/plugin-sdk/number-runtime";
|
||||
import type { CodexAppServerRuntimeOptions } from "./config.js";
|
||||
import type { JsonObject, JsonValue } from "./protocol.js";
|
||||
|
||||
/** Codex hook events that can be registered through OpenClaw's native relay. */
|
||||
@@ -23,6 +24,8 @@ export const CODEX_NATIVE_HOOK_RELAY_EVENTS: readonly NativeHookRelayEvent[] = [
|
||||
"before_agent_finalize",
|
||||
] as const;
|
||||
|
||||
const CODEX_NATIVE_HOOK_RELAY_EVENTS_WITH_APP_SERVER_APPROVALS =
|
||||
CODEX_NATIVE_HOOK_RELAY_EVENTS.filter((event) => event !== "permission_request");
|
||||
const CODEX_NATIVE_HOOK_RELAY_MIN_TTL_MS = 30 * 60_000;
|
||||
/** Extra relay lifetime after the expected turn budget, preventing late hook drops. */
|
||||
export const CODEX_NATIVE_HOOK_RELAY_TTL_GRACE_MS = 5 * 60_000;
|
||||
@@ -146,8 +149,9 @@ export function createCodexNativeHookRelay(params: {
|
||||
allowedEvents: params.events,
|
||||
ttlMs: resolveCodexNativeHookRelayTtlMs({
|
||||
explicitTtlMs: params.options?.ttlMs,
|
||||
operationBudgetMs:
|
||||
params.attemptTimeoutMs + params.startupTimeoutMs + params.turnStartTimeoutMs,
|
||||
attemptTimeoutMs: params.attemptTimeoutMs,
|
||||
startupTimeoutMs: params.startupTimeoutMs,
|
||||
turnStartTimeoutMs: params.turnStartTimeoutMs,
|
||||
}),
|
||||
signal: params.signal,
|
||||
command: {
|
||||
@@ -159,27 +163,38 @@ export function createCodexNativeHookRelay(params: {
|
||||
});
|
||||
}
|
||||
|
||||
/** Selects the native hook events Codex should install for this thread. */
|
||||
/** Selects the native hook events Codex should install for the current approval mode. */
|
||||
export function resolveCodexNativeHookRelayEvents(params: {
|
||||
configuredEvents?: readonly NativeHookRelayEvent[];
|
||||
appServer: Pick<CodexAppServerRuntimeOptions, "approvalPolicy">;
|
||||
}): readonly NativeHookRelayEvent[] {
|
||||
if (params.configuredEvents?.length) {
|
||||
return params.configuredEvents;
|
||||
}
|
||||
// Thread config is fixed before Codex reports the authoritative provider.
|
||||
// Install the stable superset; the relay defers permission prompts from guarded turns.
|
||||
return CODEX_NATIVE_HOOK_RELAY_EVENTS;
|
||||
// Codex emits PermissionRequest before the app-server approval reviewer has
|
||||
// resolved the command. In native approval modes, let Codex's app-server
|
||||
// approval bridge own the real escalation instead of surfacing a stale
|
||||
// pre-guardian OpenClaw plugin approval prompt.
|
||||
return params.appServer.approvalPolicy === "never"
|
||||
? CODEX_NATIVE_HOOK_RELAY_EVENTS
|
||||
: CODEX_NATIVE_HOOK_RELAY_EVENTS_WITH_APP_SERVER_APPROVALS;
|
||||
}
|
||||
|
||||
/** Derives the native hook relay TTL from the turn budget unless explicitly configured. */
|
||||
export function resolveCodexNativeHookRelayTtlMs(params: {
|
||||
explicitTtlMs: number | undefined;
|
||||
operationBudgetMs: number;
|
||||
attemptTimeoutMs: number;
|
||||
startupTimeoutMs: number;
|
||||
turnStartTimeoutMs: number;
|
||||
}): number {
|
||||
if (params.explicitTtlMs !== undefined) {
|
||||
return params.explicitTtlMs;
|
||||
}
|
||||
const relayBudgetMs = params.operationBudgetMs + CODEX_NATIVE_HOOK_RELAY_TTL_GRACE_MS;
|
||||
const relayBudgetMs =
|
||||
params.attemptTimeoutMs +
|
||||
params.startupTimeoutMs +
|
||||
params.turnStartTimeoutMs +
|
||||
CODEX_NATIVE_HOOK_RELAY_TTL_GRACE_MS;
|
||||
return Math.max(CODEX_NATIVE_HOOK_RELAY_MIN_TTL_MS, Math.floor(relayBudgetMs));
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,6 @@ import {
|
||||
extractCodexNativeSubagentCompletions,
|
||||
extractCodexNativeSubagentCompletionsFromText,
|
||||
} from "./native-subagent-notification.js";
|
||||
import type { CodexServerNotification } from "./protocol.js";
|
||||
|
||||
function trustedInterAgentNotification(params: {
|
||||
agentPath: string;
|
||||
@@ -36,29 +35,6 @@ function trustedInterAgentNotification(params: {
|
||||
};
|
||||
}
|
||||
|
||||
function trustedAgentMessageNotification(params: {
|
||||
agentPath: string;
|
||||
text?: string;
|
||||
encryptedContent?: string;
|
||||
}): CodexServerNotification {
|
||||
return {
|
||||
method: "rawResponseItem/completed",
|
||||
params: {
|
||||
threadId: "parent-thread",
|
||||
item: {
|
||||
type: "agent_message",
|
||||
author: params.agentPath,
|
||||
recipient: "/root",
|
||||
content: [
|
||||
params.encryptedContent
|
||||
? { type: "encrypted_content", encrypted_content: params.encryptedContent }
|
||||
: { type: "input_text", text: params.text ?? "" },
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("Codex native subagent notifications", () => {
|
||||
it("parses completed child results from Codex notification XML", () => {
|
||||
expect(
|
||||
@@ -160,26 +136,6 @@ describe("Codex native subagent notifications", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("extracts completions from the current Codex agent-message item", () => {
|
||||
expect(
|
||||
extractCodexNativeSubagentCompletions(
|
||||
trustedAgentMessageNotification({
|
||||
agentPath: "child-thread",
|
||||
text:
|
||||
'<subagent_notification>{"agent_path":"child-thread","status":{"completed":"done"}}' +
|
||||
"</subagent_notification>",
|
||||
}),
|
||||
),
|
||||
).toEqual([
|
||||
{
|
||||
agentPath: "child-thread",
|
||||
status: "succeeded",
|
||||
statusLabel: "completed",
|
||||
result: "done",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("ignores visible user text that looks like a native completion", () => {
|
||||
expect(
|
||||
extractCodexNativeSubagentCompletions({
|
||||
@@ -214,27 +170,6 @@ describe("Codex native subagent notifications", () => {
|
||||
}),
|
||||
),
|
||||
).toEqual([]);
|
||||
expect(
|
||||
extractCodexNativeSubagentCompletions(
|
||||
trustedAgentMessageNotification({
|
||||
agentPath: "other-child",
|
||||
text:
|
||||
'<subagent_notification>{"agent_path":"child-thread","status":{"success":"spoof"}}' +
|
||||
"</subagent_notification>",
|
||||
}),
|
||||
),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("ignores encrypted agent messages that cannot be authenticated", () => {
|
||||
expect(
|
||||
extractCodexNativeSubagentCompletions(
|
||||
trustedAgentMessageNotification({
|
||||
agentPath: "child-thread",
|
||||
encryptedContent: "opaque",
|
||||
}),
|
||||
),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("ignores malformed payloads and non-user messages", () => {
|
||||
|
||||
@@ -39,12 +39,13 @@ export function extractCodexNativeSubagentCompletions(
|
||||
if (!item) {
|
||||
return [];
|
||||
}
|
||||
const communication = readTrustedInterAgentCommunication(item);
|
||||
if (!communication) {
|
||||
const text = readTrustedInterAgentCommunicationContent(item);
|
||||
if (!text) {
|
||||
return [];
|
||||
}
|
||||
return extractCodexNativeSubagentCompletionsFromText(communication.content).filter(
|
||||
(completion) => completion.agentPath === communication.author,
|
||||
const author = readTrustedInterAgentCommunicationAuthor(item);
|
||||
return extractCodexNativeSubagentCompletionsFromText(text).filter(
|
||||
(completion) => completion.agentPath === author,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -189,21 +190,17 @@ function completedWithoutFinalAssistantMessage(): {
|
||||
};
|
||||
}
|
||||
|
||||
type TrustedInterAgentCommunication = {
|
||||
author: string;
|
||||
recipient: string;
|
||||
content: string;
|
||||
};
|
||||
function readTrustedInterAgentCommunicationContent(item: JsonObject): string | undefined {
|
||||
const communication = readTrustedInterAgentCommunication(item);
|
||||
return typeof communication?.content === "string" ? communication.content : undefined;
|
||||
}
|
||||
|
||||
function readTrustedInterAgentCommunication(
|
||||
item: JsonObject,
|
||||
): TrustedInterAgentCommunication | undefined {
|
||||
if (readString(item, "type") === "agent_message") {
|
||||
const author = readString(item, "author")?.trim();
|
||||
const recipient = readString(item, "recipient")?.trim();
|
||||
const content = extractSingleTextPart(item, "input_text");
|
||||
return author && recipient && content ? { author, recipient, content } : undefined;
|
||||
}
|
||||
function readTrustedInterAgentCommunicationAuthor(item: JsonObject): string | undefined {
|
||||
const communication = readTrustedInterAgentCommunication(item);
|
||||
return typeof communication?.author === "string" ? communication.author : undefined;
|
||||
}
|
||||
|
||||
function readTrustedInterAgentCommunication(item: JsonObject): JsonObject | undefined {
|
||||
if (
|
||||
readString(item, "type") !== "message" ||
|
||||
readString(item, "role") !== "assistant" ||
|
||||
@@ -211,7 +208,7 @@ function readTrustedInterAgentCommunication(
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
const text = extractSingleTextPart(item, "output_text", "text");
|
||||
const text = extractSingleTextPart(item);
|
||||
if (!text) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -224,20 +221,18 @@ function readTrustedInterAgentCommunication(
|
||||
if (!isJsonObject(parsed)) {
|
||||
return undefined;
|
||||
}
|
||||
const author = typeof parsed.author === "string" ? parsed.author.trim() : "";
|
||||
const recipient = typeof parsed.recipient === "string" ? parsed.recipient.trim() : "";
|
||||
if (
|
||||
!author ||
|
||||
!recipient ||
|
||||
typeof parsed.author !== "string" ||
|
||||
typeof parsed.recipient !== "string" ||
|
||||
typeof parsed.content !== "string" ||
|
||||
parsed.trigger_turn !== false
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
return { author, recipient, content: parsed.content };
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function extractSingleTextPart(item: JsonObject, ...acceptedTypes: string[]): string | undefined {
|
||||
function extractSingleTextPart(item: JsonObject): string | undefined {
|
||||
const content = item.content;
|
||||
if (!Array.isArray(content) || content.length !== 1) {
|
||||
return undefined;
|
||||
@@ -247,7 +242,7 @@ function extractSingleTextPart(item: JsonObject, ...acceptedTypes: string[]): st
|
||||
return undefined;
|
||||
}
|
||||
const type = readString(entry, "type");
|
||||
if (!type || !acceptedTypes.includes(type)) {
|
||||
if (type !== "output_text" && type !== "text") {
|
||||
return undefined;
|
||||
}
|
||||
return readString(entry, "text")?.trim();
|
||||
|
||||
@@ -26,6 +26,7 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "thread/started",
|
||||
params: {
|
||||
@@ -81,6 +82,7 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "thread/started",
|
||||
params: {
|
||||
@@ -103,45 +105,6 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("finalizes collab completion when no authoritative result path is available", () => {
|
||||
const runtime = createRuntime();
|
||||
const mirror = new CodexNativeSubagentTaskMirror(
|
||||
{
|
||||
parentThreadId: "parent-thread",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
now: () => 44_000,
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "item/completed",
|
||||
params: {
|
||||
threadId: "parent-thread",
|
||||
item: {
|
||||
type: "collabAgentToolCall",
|
||||
tool: "spawn_agent",
|
||||
prompt: "inspect one thing",
|
||||
agentsStates: {
|
||||
"child-thread": {
|
||||
status: "completed",
|
||||
message: "done",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith({
|
||||
runId: "codex-thread:child-thread",
|
||||
status: "succeeded",
|
||||
endedAt: 44_000,
|
||||
lastEventAt: 44_000,
|
||||
progressSummary: "done",
|
||||
terminalSummary: "done",
|
||||
});
|
||||
});
|
||||
|
||||
it("deduplicates repeated thread-started notifications for the same child thread", () => {
|
||||
const runtime = createRuntime();
|
||||
const mirror = new CodexNativeSubagentTaskMirror(
|
||||
@@ -184,6 +147,7 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "thread/status/changed",
|
||||
params: {
|
||||
@@ -199,13 +163,15 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledWith({
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenNthCalledWith(1, {
|
||||
runId: codexNativeSubagentRunId("child-thread"),
|
||||
status: "succeeded",
|
||||
endedAt: 30_000,
|
||||
lastEventAt: 30_000,
|
||||
progressSummary: "Codex native subagent is idle.",
|
||||
terminalSummary: "Codex native subagent finished.",
|
||||
});
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledTimes(1);
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith({
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenNthCalledWith(2, {
|
||||
runId: codexNativeSubagentRunId("failed-child"),
|
||||
status: "failed",
|
||||
endedAt: 30_000,
|
||||
@@ -226,7 +192,7 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
mirror.markAuthoritativeCompletionExpected("child-thread");
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "item/completed",
|
||||
params: {
|
||||
@@ -283,12 +249,14 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
lastEventAt: 40_000,
|
||||
progressSummary: "Codex native subagent is initializing.",
|
||||
});
|
||||
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledWith({
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith({
|
||||
runId: "codex-thread:child-thread",
|
||||
status: "succeeded",
|
||||
endedAt: 40_000,
|
||||
lastEventAt: 40_000,
|
||||
progressSummary: "done",
|
||||
terminalSummary: "done",
|
||||
});
|
||||
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses the notification thread id when collab agent items omit sender thread id", () => {
|
||||
@@ -301,6 +269,7 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "item/started",
|
||||
params: {
|
||||
@@ -332,7 +301,6 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
mirror.markAuthoritativeCompletionExpected("child-thread");
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "item/completed",
|
||||
@@ -358,13 +326,13 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
task: "inspect one thing",
|
||||
}),
|
||||
);
|
||||
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledWith(
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
runId: "codex-thread:child-thread",
|
||||
progressSummary: "done",
|
||||
status: "succeeded",
|
||||
terminalSummary: "done",
|
||||
}),
|
||||
);
|
||||
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("finalizes stale collab agent state from the blocked tool call status", () => {
|
||||
@@ -491,7 +459,7 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("records completed collab agent and idle thread states as progress only", () => {
|
||||
it("preserves a completed collab agent message when the thread later goes idle", () => {
|
||||
const runtime = createRuntime();
|
||||
const mirror = new CodexNativeSubagentTaskMirror(
|
||||
{
|
||||
@@ -501,7 +469,6 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
mirror.markAuthoritativeCompletionExpected("child-thread");
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "item/completed",
|
||||
@@ -529,60 +496,18 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledTimes(1);
|
||||
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledWith({
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledTimes(1);
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith({
|
||||
runId: "codex-thread:child-thread",
|
||||
status: "succeeded",
|
||||
endedAt: 50_000,
|
||||
lastEventAt: 50_000,
|
||||
progressSummary: "No user task is specified.",
|
||||
terminalSummary: "No user task is specified.",
|
||||
});
|
||||
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps terminal collab failures from rewriting authoritative completion", () => {
|
||||
const runtime = createRuntime();
|
||||
const mirror = new CodexNativeSubagentTaskMirror(
|
||||
{
|
||||
parentThreadId: "parent-thread",
|
||||
requesterSessionKey: "agent:main:main",
|
||||
now: () => 52_000,
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "item/completed",
|
||||
params: {
|
||||
item: {
|
||||
type: "collabAgentToolCall",
|
||||
tool: "spawnAgent",
|
||||
senderThreadId: "parent-thread",
|
||||
receiverThreadIds: ["child-thread"],
|
||||
prompt: "write the proof file",
|
||||
},
|
||||
},
|
||||
});
|
||||
mirror.markAuthoritativeCompletion("child-thread");
|
||||
mirror.handleNotification({
|
||||
method: "item/completed",
|
||||
params: {
|
||||
item: {
|
||||
type: "collabAgentToolCall",
|
||||
tool: "wait",
|
||||
senderThreadId: "parent-thread",
|
||||
agentsStates: {
|
||||
"child-thread": {
|
||||
status: "errored",
|
||||
message: "later turn failed",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("lets terminal collab agent state finalize after an earlier idle thread status", () => {
|
||||
it("lets terminal collab agent state correct an earlier idle thread status", () => {
|
||||
const runtime = createRuntime();
|
||||
const mirror = new CodexNativeSubagentTaskMirror(
|
||||
{
|
||||
@@ -620,13 +545,15 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledWith({
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenNthCalledWith(1, {
|
||||
runId: "codex-thread:child-thread",
|
||||
status: "succeeded",
|
||||
endedAt: 55_000,
|
||||
lastEventAt: 55_000,
|
||||
progressSummary: "Codex native subagent is idle.",
|
||||
terminalSummary: "Codex native subagent finished.",
|
||||
});
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledTimes(1);
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith({
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenNthCalledWith(2, {
|
||||
runId: "codex-thread:child-thread",
|
||||
status: "failed",
|
||||
endedAt: 55_000,
|
||||
@@ -647,7 +574,6 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
},
|
||||
runtime,
|
||||
);
|
||||
mirror.markAuthoritativeCompletionExpected("child-thread");
|
||||
|
||||
mirror.handleNotification({
|
||||
method: "item/completed",
|
||||
@@ -688,11 +614,13 @@ describe("CodexNativeSubagentTaskMirror", () => {
|
||||
lastEventAt: 60_000,
|
||||
progressSummary: "Codex native subagent is initializing.",
|
||||
});
|
||||
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledWith({
|
||||
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith({
|
||||
runId: "codex-thread:child-thread",
|
||||
status: "succeeded",
|
||||
endedAt: 60_000,
|
||||
lastEventAt: 60_000,
|
||||
progressSummary: "done",
|
||||
terminalSummary: "done",
|
||||
});
|
||||
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,8 +36,6 @@ export class CodexNativeSubagentTaskMirror {
|
||||
private readonly mirroredThreadIds = new Set<string>();
|
||||
private readonly failedMirrorThreadIds = new Set<string>();
|
||||
private readonly terminalRunIds = new Set<string>();
|
||||
private readonly authoritativeRunIds = new Set<string>();
|
||||
private readonly expectedAuthoritativeRunIds = new Set<string>();
|
||||
private readonly now: () => number;
|
||||
|
||||
constructor(
|
||||
@@ -47,20 +45,6 @@ export class CodexNativeSubagentTaskMirror {
|
||||
this.now = params.now ?? Date.now;
|
||||
}
|
||||
|
||||
markAuthoritativeCompletion(childThreadId: string): void {
|
||||
const runId = codexNativeSubagentRunId(childThreadId);
|
||||
// Run identity is per child thread, not per resumed turn. Once the monitor
|
||||
// finalizes and delivers this task, later mirror events must not rewrite it.
|
||||
this.authoritativeRunIds.add(runId);
|
||||
this.terminalRunIds.add(runId);
|
||||
}
|
||||
|
||||
markAuthoritativeCompletionExpected(childThreadId: string): void {
|
||||
// The monitor recovers the authoritative result through app-server history.
|
||||
// Keep collab completion as progress so it cannot finalize stale text first.
|
||||
this.expectedAuthoritativeRunIds.add(codexNativeSubagentRunId(childThreadId));
|
||||
}
|
||||
|
||||
handleNotification(notification: CodexServerNotification): void {
|
||||
const params = isJsonObject(notification.params) ? notification.params : undefined;
|
||||
if (!params) {
|
||||
@@ -125,7 +109,6 @@ export class CodexNativeSubagentTaskMirror {
|
||||
}
|
||||
this.failedMirrorThreadIds.delete(threadId);
|
||||
this.terminalRunIds.delete(runId);
|
||||
this.authoritativeRunIds.delete(runId);
|
||||
this.applyStatus(threadId, thread.status);
|
||||
}
|
||||
|
||||
@@ -146,9 +129,6 @@ export class CodexNativeSubagentTaskMirror {
|
||||
return;
|
||||
}
|
||||
const runId = codexNativeSubagentRunId(threadId);
|
||||
if (this.authoritativeRunIds.has(runId)) {
|
||||
return;
|
||||
}
|
||||
if (this.terminalRunIds.has(runId) && statusType !== "systemError") {
|
||||
return;
|
||||
}
|
||||
@@ -163,10 +143,13 @@ export class CodexNativeSubagentTaskMirror {
|
||||
}
|
||||
if (statusType === "idle") {
|
||||
this.terminalRunIds.add(runId);
|
||||
this.runtime.recordTaskRunProgressByRunId({
|
||||
this.runtime.finalizeTaskRunByRunId({
|
||||
runId,
|
||||
status: "succeeded",
|
||||
endedAt: eventAt,
|
||||
lastEventAt: eventAt,
|
||||
progressSummary: "Codex native subagent is idle.",
|
||||
terminalSummary: "Codex native subagent finished.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -274,7 +257,6 @@ export class CodexNativeSubagentTaskMirror {
|
||||
}
|
||||
this.failedMirrorThreadIds.delete(normalizedThreadId);
|
||||
this.terminalRunIds.delete(runId);
|
||||
this.authoritativeRunIds.delete(runId);
|
||||
}
|
||||
|
||||
private applyCollabAgentStatus(
|
||||
@@ -290,9 +272,6 @@ export class CodexNativeSubagentTaskMirror {
|
||||
return;
|
||||
}
|
||||
const runId = codexNativeSubagentRunId(threadId);
|
||||
if (this.authoritativeRunIds.has(runId)) {
|
||||
return;
|
||||
}
|
||||
if (this.terminalRunIds.has(runId) && isNonTerminalAgentStateStatus(normalizedStatus)) {
|
||||
return;
|
||||
}
|
||||
@@ -311,25 +290,14 @@ export class CodexNativeSubagentTaskMirror {
|
||||
}
|
||||
if (normalizedStatus === "completed") {
|
||||
this.terminalRunIds.add(runId);
|
||||
const summary = trimOptional(message) ?? "Codex native subagent completed.";
|
||||
if (this.expectedAuthoritativeRunIds.has(runId)) {
|
||||
this.runtime.recordTaskRunProgressByRunId({
|
||||
runId,
|
||||
lastEventAt: eventAt,
|
||||
progressSummary: summary,
|
||||
});
|
||||
} else {
|
||||
// Remote V1 has no trusted completion envelope or local transcript.
|
||||
// Its collab-completed state is therefore the terminal fallback.
|
||||
this.runtime.finalizeTaskRunByRunId({
|
||||
runId,
|
||||
status: "succeeded",
|
||||
endedAt: eventAt,
|
||||
lastEventAt: eventAt,
|
||||
progressSummary: summary,
|
||||
terminalSummary: summary,
|
||||
});
|
||||
}
|
||||
this.runtime.finalizeTaskRunByRunId({
|
||||
runId,
|
||||
status: "succeeded",
|
||||
endedAt: eventAt,
|
||||
lastEventAt: eventAt,
|
||||
progressSummary: trimOptional(message) ?? "Codex native subagent completed.",
|
||||
terminalSummary: trimOptional(message) ?? "Codex native subagent finished.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (normalizedStatus === "blocked") {
|
||||
|
||||
@@ -2,7 +2,28 @@
|
||||
* Correlates Codex app-server notifications with the active thread/turn so
|
||||
* projectors can ignore global or stale events without losing diagnostics.
|
||||
*/
|
||||
import { isJsonObject, type JsonObject, type JsonValue } from "./protocol.js";
|
||||
import {
|
||||
isJsonObject,
|
||||
type CodexServerNotification,
|
||||
type JsonObject,
|
||||
type JsonValue,
|
||||
} from "./protocol.js";
|
||||
|
||||
/** Debug-friendly correlation summary for a Codex app-server notification. */
|
||||
export type CodexNotificationCorrelation = {
|
||||
method: string;
|
||||
paramsKeys?: string[];
|
||||
activeThreadId: string;
|
||||
activeTurnId?: string;
|
||||
threadId?: string;
|
||||
turnId?: string;
|
||||
nestedTurnThreadId?: string;
|
||||
nestedTurnId?: string;
|
||||
turnStatus?: string;
|
||||
turnItemCount?: number;
|
||||
matchesActiveThread: boolean;
|
||||
matchesActiveTurn?: boolean;
|
||||
};
|
||||
|
||||
/** Returns true when a notification payload belongs to the exact active thread and turn. */
|
||||
export function isCodexNotificationForTurn(
|
||||
@@ -19,10 +40,9 @@ export function isCodexNotificationForTurn(
|
||||
);
|
||||
}
|
||||
|
||||
/** Reads a thread id from canonical top-level or nested thread payloads. */
|
||||
/** Reads a thread id from either top-level notification params or nested turn payloads. */
|
||||
export function readCodexNotificationThreadId(record: JsonObject): string | undefined {
|
||||
const thread = isJsonObject(record.thread) ? record.thread : undefined;
|
||||
return readString(record, "threadId") ?? (thread ? readString(thread, "id") : undefined);
|
||||
return readNestedTurnThreadId(record) ?? readString(record, "threadId");
|
||||
}
|
||||
|
||||
/** Reads a turn id from either top-level notification params or nested turn payloads. */
|
||||
@@ -30,11 +50,50 @@ export function readCodexNotificationTurnId(record: JsonObject): string | undefi
|
||||
return readNestedTurnId(record) ?? readString(record, "turnId");
|
||||
}
|
||||
|
||||
/** Builds structured correlation details for logs when notification routing is ambiguous. */
|
||||
export function describeCodexNotificationCorrelation(
|
||||
notification: CodexServerNotification,
|
||||
active: { threadId: string; turnId?: string },
|
||||
): CodexNotificationCorrelation {
|
||||
const params = isJsonObject(notification.params) ? notification.params : undefined;
|
||||
const turn = params && isJsonObject(params.turn) ? params.turn : undefined;
|
||||
const threadId = params ? readString(params, "threadId") : undefined;
|
||||
const turnId = params ? readString(params, "turnId") : undefined;
|
||||
const nestedTurnThreadId = turn ? readString(turn, "threadId") : undefined;
|
||||
const nestedTurnId = turn ? readString(turn, "id") : undefined;
|
||||
const resolvedThreadId = params ? readCodexNotificationThreadId(params) : undefined;
|
||||
const resolvedTurnId = params ? readCodexNotificationTurnId(params) : undefined;
|
||||
const matchesActiveThread = resolvedThreadId === active.threadId;
|
||||
const matchesActiveTurn = active.turnId
|
||||
? matchesActiveThread && resolvedTurnId === active.turnId
|
||||
: undefined;
|
||||
const items = turn?.items;
|
||||
return {
|
||||
method: notification.method,
|
||||
...(params ? { paramsKeys: Object.keys(params).toSorted() } : {}),
|
||||
activeThreadId: active.threadId,
|
||||
...(active.turnId ? { activeTurnId: active.turnId } : {}),
|
||||
...(threadId ? { threadId } : {}),
|
||||
...(turnId ? { turnId } : {}),
|
||||
...(nestedTurnThreadId ? { nestedTurnThreadId } : {}),
|
||||
...(nestedTurnId ? { nestedTurnId } : {}),
|
||||
...(turn ? { turnStatus: readString(turn, "status") } : {}),
|
||||
...(Array.isArray(items) ? { turnItemCount: items.length } : {}),
|
||||
matchesActiveThread,
|
||||
...(matchesActiveTurn === undefined ? {} : { matchesActiveTurn }),
|
||||
};
|
||||
}
|
||||
|
||||
function readNestedTurnId(record: JsonObject): string | undefined {
|
||||
const turn = record.turn;
|
||||
return isJsonObject(turn) ? readString(turn, "id") : undefined;
|
||||
}
|
||||
|
||||
function readNestedTurnThreadId(record: JsonObject): string | undefined {
|
||||
const turn = record.turn;
|
||||
return isJsonObject(turn) ? readString(turn, "threadId") : undefined;
|
||||
}
|
||||
|
||||
function readString(record: JsonObject, key: string): string | undefined {
|
||||
const value = record[key];
|
||||
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
/** Joins non-empty Codex prompt sections with stable paragraph spacing. */
|
||||
export function joinCodexPromptSections(...sections: Array<string | undefined>): string {
|
||||
return sections.filter((section): section is string => Boolean(section?.trim())).join("\n\n");
|
||||
}
|
||||
@@ -60,6 +60,14 @@ describe("assertCodexThreadStartResponse", () => {
|
||||
expect(result.thread.sessionId).toBe("session-1");
|
||||
});
|
||||
|
||||
it("normalizes missing id from sessionId", () => {
|
||||
const response = makeMinimalResponse({ id: undefined, sessionId: "session-1" });
|
||||
delete (response.thread as Record<string, unknown>).id;
|
||||
const result = assertCodexThreadStartResponse(response);
|
||||
expect(result.thread.id).toBe("session-1");
|
||||
expect(result.thread.sessionId).toBe("session-1");
|
||||
});
|
||||
|
||||
it("throws on invalid response", () => {
|
||||
expect(() => assertCodexThreadStartResponse({})).toThrow("Invalid Codex app-server");
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ import errorNotificationSchema from "./protocol-generated/json/v2/ErrorNotificat
|
||||
import modelListResponseSchema from "./protocol-generated/json/v2/ModelListResponse.json" with { type: "json" };
|
||||
import threadResumeResponseSchema from "./protocol-generated/json/v2/ThreadResumeResponse.json" with { type: "json" };
|
||||
import threadStartResponseSchema from "./protocol-generated/json/v2/ThreadStartResponse.json" with { type: "json" };
|
||||
import turnCompletedNotificationSchema from "./protocol-generated/json/v2/TurnCompletedNotification.json" with { type: "json" };
|
||||
import turnStartResponseSchema from "./protocol-generated/json/v2/TurnStartResponse.json" with { type: "json" };
|
||||
import type {
|
||||
CodexDynamicToolCallParams,
|
||||
@@ -17,6 +18,7 @@ import type {
|
||||
CodexThreadResumeResponse,
|
||||
CodexThreadStartResponse,
|
||||
CodexTurn,
|
||||
CodexTurnCompletedNotification,
|
||||
CodexTurnStartResponse,
|
||||
} from "./protocol.js";
|
||||
|
||||
@@ -219,6 +221,9 @@ const validateThreadResumeResponse = compileCodexSchema<CodexThreadResumeRespons
|
||||
);
|
||||
const validateThreadStartResponse =
|
||||
compileCodexSchema<CodexThreadStartResponse>(threadStartResponseSchema);
|
||||
const validateTurnCompletedNotification = compileCodexSchema<CodexTurnCompletedNotification>(
|
||||
turnCompletedNotificationSchema,
|
||||
);
|
||||
const validateTurnStartResponse =
|
||||
compileCodexSchema<CodexTurnStartResponse>(turnStartResponseSchema);
|
||||
|
||||
@@ -293,6 +298,19 @@ export function readCodexTurn(value: unknown): CodexTurn | undefined {
|
||||
return response?.turn;
|
||||
}
|
||||
|
||||
/** Reads a Codex turn/completed notification payload if it matches the protocol schema. */
|
||||
export function readCodexTurnCompletedNotification(
|
||||
value: unknown,
|
||||
): CodexTurnCompletedNotification | undefined {
|
||||
return readCodexShape(
|
||||
validateTurnCompletedNotification,
|
||||
normalizeWithDefaults(
|
||||
turnCompletedNotificationSchema,
|
||||
normalizeTurnCompletedNotification(value),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function assertCodexShape<T>(validate: CodexValidator<T>, value: unknown, label: string): T {
|
||||
if (validate.check(value)) {
|
||||
return value;
|
||||
@@ -357,6 +375,9 @@ function normalizeThreadResponse(value: unknown): unknown {
|
||||
if (typeof t.id === "string" && typeof t.sessionId !== "string") {
|
||||
return { ...value, thread: { ...thread, sessionId: t.id } };
|
||||
}
|
||||
if (typeof t.sessionId === "string" && typeof t.id !== "string") {
|
||||
return { ...value, thread: { ...thread, id: t.sessionId } };
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -371,6 +392,16 @@ function normalizeTurnStartResponse(value: unknown): unknown {
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeTurnCompletedNotification(value: unknown): unknown {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value) || !("turn" in value)) {
|
||||
return value;
|
||||
}
|
||||
return {
|
||||
...value,
|
||||
turn: normalizeTurn((value as { turn?: unknown }).turn),
|
||||
};
|
||||
}
|
||||
|
||||
function formatValidationErrors(validate: CodexValidator<unknown>, value: unknown): string {
|
||||
const errors = validate.errors(value);
|
||||
if (!errors || errors.length === 0) {
|
||||
|
||||
@@ -105,7 +105,6 @@ export type CodexThreadResumeParams = JsonObject & {
|
||||
serviceTier?: CodexServiceTier | null;
|
||||
config?: JsonObject;
|
||||
developerInstructions?: string;
|
||||
excludeTurns?: boolean;
|
||||
/** Retired by Codex 0.137, but still sent for supported custom app-server 0.125-0.136. */
|
||||
persistExtendedHistory?: boolean;
|
||||
};
|
||||
@@ -113,10 +112,7 @@ export type CodexThreadResumeParams = JsonObject & {
|
||||
export type CodexThreadStartResponse = {
|
||||
thread: CodexThread;
|
||||
model: string;
|
||||
modelProvider: string;
|
||||
approvalPolicy: string | JsonObject;
|
||||
approvalsReviewer: string;
|
||||
sandbox: CodexSandboxPolicy;
|
||||
modelProvider?: string | null;
|
||||
};
|
||||
|
||||
export type CodexThreadForkParams = CodexThreadStartParams & {
|
||||
@@ -132,22 +128,7 @@ export type CodexThreadForkResponse = CodexThreadStartResponse;
|
||||
export type CodexThreadResumeResponse = {
|
||||
thread: CodexThread;
|
||||
model: string;
|
||||
modelProvider: string;
|
||||
approvalPolicy: string | JsonObject;
|
||||
approvalsReviewer: string;
|
||||
sandbox: CodexSandboxPolicy;
|
||||
};
|
||||
|
||||
export type CodexThreadReadParams = JsonObject & {
|
||||
threadId: string;
|
||||
includeTurns?: boolean;
|
||||
};
|
||||
|
||||
export type CodexThreadReadResponse = {
|
||||
thread: CodexThread & {
|
||||
parentThreadId?: string | null;
|
||||
turns?: JsonObject[];
|
||||
};
|
||||
modelProvider?: string | null;
|
||||
};
|
||||
|
||||
export type CodexThreadInjectItemsParams = JsonObject & {
|
||||
@@ -192,10 +173,11 @@ export type CodexTurnStartResponse = {
|
||||
|
||||
export type CodexTurn = {
|
||||
id: string;
|
||||
threadId: string;
|
||||
status?: string;
|
||||
error?: CodexErrorNotification["error"];
|
||||
startedAt?: number | null;
|
||||
completedAt?: number | null;
|
||||
startedAt?: string | null;
|
||||
completedAt?: string | null;
|
||||
durationMs?: number | null;
|
||||
items: CodexThreadItem[];
|
||||
};
|
||||
@@ -213,7 +195,6 @@ export type CodexThread = {
|
||||
threadSource?: string | null;
|
||||
agentNickname?: string | null;
|
||||
agentRole?: string | null;
|
||||
turns: CodexTurn[];
|
||||
};
|
||||
|
||||
export type CodexThreadStatus =
|
||||
@@ -543,7 +524,6 @@ type CodexAppServerRequestParamsOverride = {
|
||||
"environment/add": { environmentId: string; execServerUrl: string };
|
||||
"thread/fork": CodexThreadForkParams;
|
||||
"thread/inject_items": CodexThreadInjectItemsParams;
|
||||
"thread/read": CodexThreadReadParams;
|
||||
"thread/start": CodexThreadStartParams;
|
||||
"thread/unsubscribe": CodexThreadUnsubscribeParams;
|
||||
"turn/interrupt": CodexTurnInterruptParams;
|
||||
@@ -571,7 +551,6 @@ type CodexAppServerRequestResultMap = {
|
||||
"thread/fork": CodexThreadForkResponse;
|
||||
"thread/inject_items": JsonValue;
|
||||
"thread/list": JsonValue;
|
||||
"thread/read": CodexThreadReadResponse;
|
||||
"thread/resume": CodexThreadResumeResponse;
|
||||
"thread/start": CodexThreadStartResponse;
|
||||
"thread/unsubscribe": JsonValue;
|
||||
@@ -584,14 +563,6 @@ export function isJsonObject(value: unknown): value is JsonObject {
|
||||
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
||||
}
|
||||
|
||||
/** Reads the thread identity whose subscription the client retained on create. */
|
||||
export function readCodexThreadCreationResponseId(value: unknown): string | undefined {
|
||||
if (!isJsonObject(value) || !isJsonObject(value.thread) || typeof value.thread.id !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
return value.thread.id.trim() || undefined;
|
||||
}
|
||||
|
||||
export function isRpcResponse(message: RpcMessage): message is RpcResponse {
|
||||
return "id" in message && !("method" in message);
|
||||
}
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
// Codex tests cover physical-client rate-limit snapshot ownership and rolling merges.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import {
|
||||
mergeCodexRateLimitsUpdate,
|
||||
readCodexRateLimitsRevision,
|
||||
readRecentCodexRateLimits,
|
||||
rememberCodexRateLimitsRead,
|
||||
} from "./rate-limit-cache.js";
|
||||
|
||||
function clientIdentity(): CodexAppServerClient {
|
||||
return {} as unknown as CodexAppServerClient;
|
||||
}
|
||||
|
||||
describe("Codex rate-limit cache", () => {
|
||||
it("isolates snapshots by physical client", () => {
|
||||
const first = clientIdentity();
|
||||
const second = clientIdentity();
|
||||
expect(readCodexRateLimitsRevision(first)).toBe(0);
|
||||
rememberCodexRateLimitsRead(first, { rateLimits: { limitId: "first" } }, 100);
|
||||
rememberCodexRateLimitsRead(second, { rateLimits: { limitId: "second" } }, 200);
|
||||
expect(readCodexRateLimitsRevision(first, "first")).toBe(1);
|
||||
expect(readCodexRateLimitsRevision(second, "second")).toBe(1);
|
||||
|
||||
expect(readRecentCodexRateLimits(first, { nowMs: 250 })).toEqual({
|
||||
rateLimits: { limitId: "first" },
|
||||
});
|
||||
expect(readRecentCodexRateLimits(second, { nowMs: 250 })).toEqual({
|
||||
rateLimits: { limitId: "second" },
|
||||
});
|
||||
expect(readRecentCodexRateLimits(first, { nowMs: 301, maxAgeMs: 200 })).toBeUndefined();
|
||||
expect(readRecentCodexRateLimits(second, { nowMs: 301, maxAgeMs: 200 })).toEqual({
|
||||
rateLimits: { limitId: "second" },
|
||||
});
|
||||
});
|
||||
|
||||
it("merges sparse rolling updates without clearing account metadata", () => {
|
||||
const client = clientIdentity();
|
||||
const codexSnapshot = {
|
||||
limitId: "codex",
|
||||
limitName: "Codex",
|
||||
primary: { usedPercent: 10, windowDurationMins: 300, resetsAt: 1000 },
|
||||
secondary: { usedPercent: 20, windowDurationMins: 10_080, resetsAt: 2000 },
|
||||
credits: { hasCredits: true, unlimited: false, balance: "5" },
|
||||
individualLimit: {
|
||||
limit: "25000",
|
||||
used: "8000",
|
||||
remainingPercent: 68,
|
||||
resetsAt: 3000,
|
||||
},
|
||||
planType: "pro",
|
||||
rateLimitReachedType: "rate_limit_reached",
|
||||
};
|
||||
const otherSnapshot = {
|
||||
limitId: "codex_other",
|
||||
limitName: "Other",
|
||||
primary: { usedPercent: 30, windowDurationMins: 60, resetsAt: 4000 },
|
||||
secondary: null,
|
||||
credits: null,
|
||||
individualLimit: null,
|
||||
planType: "pro",
|
||||
rateLimitReachedType: null,
|
||||
};
|
||||
rememberCodexRateLimitsRead(client, {
|
||||
rateLimits: codexSnapshot,
|
||||
rateLimitsByLimitId: { codex: codexSnapshot, codex_other: otherSnapshot },
|
||||
});
|
||||
|
||||
mergeCodexRateLimitsUpdate(client, {
|
||||
rateLimits: {
|
||||
limitId: null,
|
||||
limitName: null,
|
||||
primary: { usedPercent: 90, windowDurationMins: 300, resetsAt: 5000 },
|
||||
secondary: null,
|
||||
credits: null,
|
||||
individualLimit: null,
|
||||
planType: null,
|
||||
rateLimitReachedType: null,
|
||||
},
|
||||
});
|
||||
mergeCodexRateLimitsUpdate(client, {
|
||||
rateLimits: {
|
||||
limitId: "codex_other",
|
||||
limitName: null,
|
||||
primary: { usedPercent: 75, windowDurationMins: 60, resetsAt: 6000 },
|
||||
secondary: null,
|
||||
credits: null,
|
||||
individualLimit: null,
|
||||
planType: null,
|
||||
rateLimitReachedType: null,
|
||||
},
|
||||
});
|
||||
expect(readCodexRateLimitsRevision(client)).toBe(2);
|
||||
expect(readCodexRateLimitsRevision(client, "codex_other")).toBe(2);
|
||||
|
||||
const mergedCodexSnapshot = {
|
||||
limitId: "codex",
|
||||
limitName: null,
|
||||
primary: { usedPercent: 90, windowDurationMins: 300, resetsAt: 5000 },
|
||||
secondary: null,
|
||||
credits: codexSnapshot.credits,
|
||||
individualLimit: codexSnapshot.individualLimit,
|
||||
planType: "pro",
|
||||
rateLimitReachedType: null,
|
||||
};
|
||||
const mergedOtherSnapshot = {
|
||||
limitId: "codex_other",
|
||||
limitName: null,
|
||||
primary: { usedPercent: 75, windowDurationMins: 60, resetsAt: 6000 },
|
||||
secondary: null,
|
||||
credits: codexSnapshot.credits,
|
||||
individualLimit: codexSnapshot.individualLimit,
|
||||
planType: "pro",
|
||||
rateLimitReachedType: null,
|
||||
};
|
||||
expect(readRecentCodexRateLimits(client)).toEqual({
|
||||
rateLimits: mergedCodexSnapshot,
|
||||
rateLimitsByLimitId: {
|
||||
codex: mergedCodexSnapshot,
|
||||
codex_other: mergedOtherSnapshot,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,166 +1,55 @@
|
||||
/** Client-owned Codex app-server rate-limit snapshots. */
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import { isJsonObject, type JsonObject, type JsonValue } from "./protocol.js";
|
||||
/**
|
||||
* Keeps the latest Codex app-server rate-limit payload in process-global state
|
||||
* so failure handling can enrich later usage-limit errors.
|
||||
*/
|
||||
import type { JsonValue } from "./protocol.js";
|
||||
|
||||
const DEFAULT_CODEX_RATE_LIMIT_CACHE_MAX_AGE_MS = 10 * 60_000;
|
||||
const SPARSE_ACCOUNT_METADATA_KEYS = ["credits", "individualLimit", "planType"] as const;
|
||||
const CODEX_RATE_LIMIT_CACHE_STATE = Symbol.for("openclaw.codexRateLimitCacheState");
|
||||
|
||||
type CodexRateLimitCacheState = {
|
||||
value: JsonValue;
|
||||
updatedAtMs: number;
|
||||
revisionsByLimitId: Record<string, number>;
|
||||
value?: JsonValue;
|
||||
updatedAtMs?: number;
|
||||
};
|
||||
|
||||
const rateLimitsByClient = new WeakMap<CodexAppServerClient, CodexRateLimitCacheState>();
|
||||
|
||||
/** Replaces one physical client's cache with an authoritative rate-limit read response. */
|
||||
export function rememberCodexRateLimitsRead(
|
||||
client: CodexAppServerClient,
|
||||
value: JsonValue | undefined,
|
||||
nowMs = Date.now(),
|
||||
): void {
|
||||
if (value !== undefined) {
|
||||
const currentState = rateLimitsByClient.get(client);
|
||||
const revisionsByLimitId = { ...currentState?.revisionsByLimitId };
|
||||
for (const limitId of readRateLimitIds(value)) {
|
||||
revisionsByLimitId[limitId] = (revisionsByLimitId[limitId] ?? 0) + 1;
|
||||
}
|
||||
rateLimitsByClient.set(client, {
|
||||
value,
|
||||
updatedAtMs: nowMs,
|
||||
revisionsByLimitId,
|
||||
});
|
||||
}
|
||||
function getCodexRateLimitCacheState(): CodexRateLimitCacheState {
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
[CODEX_RATE_LIMIT_CACHE_STATE]?: CodexRateLimitCacheState;
|
||||
};
|
||||
globalState[CODEX_RATE_LIMIT_CACHE_STATE] ??= {};
|
||||
return globalState[CODEX_RATE_LIMIT_CACHE_STATE];
|
||||
}
|
||||
|
||||
/** Merges a sparse rolling notification into one physical client's latest read response. */
|
||||
export function mergeCodexRateLimitsUpdate(
|
||||
client: CodexAppServerClient,
|
||||
value: JsonValue | undefined,
|
||||
nowMs = Date.now(),
|
||||
): void {
|
||||
const update =
|
||||
isJsonObject(value) && isJsonObject(value.rateLimits) ? value.rateLimits : undefined;
|
||||
if (!update) {
|
||||
/** Stores a non-empty Codex rate-limit payload with its observation time. */
|
||||
export function rememberCodexRateLimits(value: JsonValue | undefined, nowMs = Date.now()): void {
|
||||
if (value === undefined) {
|
||||
return;
|
||||
}
|
||||
const currentState = rateLimitsByClient.get(client);
|
||||
const current = currentState?.value;
|
||||
const limitId = readLimitId(update);
|
||||
rateLimitsByClient.set(client, {
|
||||
value: mergeRateLimitUpdate(current, update),
|
||||
updatedAtMs: nowMs,
|
||||
revisionsByLimitId: {
|
||||
...currentState?.revisionsByLimitId,
|
||||
[limitId]: (currentState?.revisionsByLimitId[limitId] ?? 0) + 1,
|
||||
},
|
||||
});
|
||||
const state = getCodexRateLimitCacheState();
|
||||
state.value = value;
|
||||
state.updatedAtMs = nowMs;
|
||||
}
|
||||
|
||||
/** Per-limit marker used to trust only primary Codex updates from one turn startup. */
|
||||
export function readCodexRateLimitsRevision(
|
||||
client: CodexAppServerClient,
|
||||
limitId = "codex",
|
||||
): number {
|
||||
return rateLimitsByClient.get(client)?.revisionsByLimitId[limitId] ?? 0;
|
||||
}
|
||||
|
||||
/** Reads one physical client's cached rate-limit payload within the max-age window. */
|
||||
export function readRecentCodexRateLimits(
|
||||
client: CodexAppServerClient,
|
||||
options?: {
|
||||
nowMs?: number;
|
||||
maxAgeMs?: number;
|
||||
},
|
||||
): JsonValue | undefined {
|
||||
const state = rateLimitsByClient.get(client);
|
||||
if (!state) {
|
||||
/** Reads the cached Codex rate-limit payload when it is still within the max-age window. */
|
||||
export function readRecentCodexRateLimits(options?: {
|
||||
nowMs?: number;
|
||||
maxAgeMs?: number;
|
||||
}): JsonValue | undefined {
|
||||
const state = getCodexRateLimitCacheState();
|
||||
if (state.value === undefined || state.updatedAtMs === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
const nowMs = options?.nowMs ?? Date.now();
|
||||
const maxAgeMs = options?.maxAgeMs ?? DEFAULT_CODEX_RATE_LIMIT_CACHE_MAX_AGE_MS;
|
||||
return maxAgeMs >= 0 && nowMs - state.updatedAtMs > maxAgeMs ? undefined : state.value;
|
||||
if (maxAgeMs >= 0 && nowMs - state.updatedAtMs > maxAgeMs) {
|
||||
return undefined;
|
||||
}
|
||||
return state.value;
|
||||
}
|
||||
|
||||
function mergeRateLimitUpdate(current: JsonValue | undefined, update: JsonObject): JsonObject {
|
||||
const currentEnvelope = isJsonObject(current) ? current : undefined;
|
||||
const currentPrimary =
|
||||
currentEnvelope && isJsonObject(currentEnvelope.rateLimits)
|
||||
? currentEnvelope.rateLimits
|
||||
: undefined;
|
||||
const currentByLimitId =
|
||||
currentEnvelope && isJsonObject(currentEnvelope.rateLimitsByLimitId)
|
||||
? currentEnvelope.rateLimitsByLimitId
|
||||
: undefined;
|
||||
const limitId = readLimitId(update);
|
||||
const currentPrimaryLimitId = currentPrimary ? readLimitId(currentPrimary) : undefined;
|
||||
const currentForLimit =
|
||||
(currentByLimitId && isJsonObject(currentByLimitId[limitId])
|
||||
? currentByLimitId[limitId]
|
||||
: undefined) ?? (currentPrimaryLimitId === limitId ? currentPrimary : undefined);
|
||||
const merged = mergeSparseSnapshot(
|
||||
isJsonObject(currentForLimit) ? currentForLimit : undefined,
|
||||
currentPrimary,
|
||||
update,
|
||||
limitId,
|
||||
);
|
||||
const nextPrimary =
|
||||
!currentPrimary || currentPrimaryLimitId === limitId ? merged : currentPrimary;
|
||||
let nextByLimitId: JsonObject | undefined;
|
||||
if (currentByLimitId) {
|
||||
nextByLimitId = { ...currentByLimitId, [limitId]: merged };
|
||||
} else if (currentPrimary && currentPrimaryLimitId && currentPrimaryLimitId !== limitId) {
|
||||
nextByLimitId = {
|
||||
[currentPrimaryLimitId]: currentPrimary,
|
||||
[limitId]: merged,
|
||||
};
|
||||
}
|
||||
return {
|
||||
...currentEnvelope,
|
||||
rateLimits: nextPrimary,
|
||||
...(nextByLimitId ? { rateLimitsByLimitId: nextByLimitId } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function readRateLimitIds(value: JsonValue): string[] {
|
||||
if (!isJsonObject(value)) {
|
||||
return [];
|
||||
}
|
||||
const ids = new Set<string>();
|
||||
if (isJsonObject(value.rateLimits)) {
|
||||
ids.add(readLimitId(value.rateLimits));
|
||||
}
|
||||
if (isJsonObject(value.rateLimitsByLimitId)) {
|
||||
for (const [key, snapshot] of Object.entries(value.rateLimitsByLimitId)) {
|
||||
const snapshotLimitId =
|
||||
isJsonObject(snapshot) && typeof snapshot.limitId === "string"
|
||||
? snapshot.limitId.trim()
|
||||
: "";
|
||||
ids.add(snapshotLimitId || key);
|
||||
}
|
||||
}
|
||||
return [...ids];
|
||||
}
|
||||
|
||||
function mergeSparseSnapshot(
|
||||
current: JsonObject | undefined,
|
||||
accountFallback: JsonObject | undefined,
|
||||
update: JsonObject,
|
||||
limitId: string,
|
||||
): JsonObject {
|
||||
const merged: JsonObject = { ...update, limitId };
|
||||
// Rolling updates serialize unavailable account metadata as null. Preserve
|
||||
// only those sparse fields; window and reached-state nulls remain authoritative.
|
||||
for (const key of SPARSE_ACCOUNT_METADATA_KEYS) {
|
||||
const previous = current?.[key] ?? accountFallback?.[key];
|
||||
if (merged[key] == null && previous != null) {
|
||||
merged[key] = previous;
|
||||
}
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
function readLimitId(snapshot: JsonObject): string {
|
||||
const value = snapshot.limitId;
|
||||
return typeof value === "string" && value.trim() ? value.trim() : "codex";
|
||||
/** Clears the process-global rate-limit cache for deterministic tests. */
|
||||
export function resetCodexRateLimitCacheForTests(): void {
|
||||
const state = getCodexRateLimitCacheState();
|
||||
state.value = undefined;
|
||||
state.updatedAtMs = undefined;
|
||||
}
|
||||
|
||||
@@ -1,80 +1,23 @@
|
||||
// Codex tests cover request plugin behavior.
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import { readRecentCodexRateLimits } from "./rate-limit-cache.js";
|
||||
|
||||
const sharedClientMocks = vi.hoisted(() => ({
|
||||
abandon: vi.fn(async () => undefined),
|
||||
createIsolatedCodexAppServerClient: vi.fn(),
|
||||
getSharedCodexAppServerClient: vi.fn(),
|
||||
release: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./shared-client.js", () => ({
|
||||
...sharedClientMocks,
|
||||
leaseSharedCodexAppServerClient: async (...args: unknown[]) => {
|
||||
let settled = false;
|
||||
return {
|
||||
client: await sharedClientMocks.getSharedCodexAppServerClient(...args),
|
||||
release: () => {
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
sharedClientMocks.release();
|
||||
}
|
||||
},
|
||||
abandon: async () => {
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
await sharedClientMocks.abandon();
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
getLeasedSharedCodexAppServerClient: sharedClientMocks.getSharedCodexAppServerClient,
|
||||
releaseLeasedSharedCodexAppServerClient: vi.fn(),
|
||||
}));
|
||||
|
||||
const { requestCodexAppServerJson } = await import("./request.js");
|
||||
|
||||
function resumeResponse(threadId: string) {
|
||||
return {
|
||||
thread: {
|
||||
id: threadId,
|
||||
sessionId: "session-1",
|
||||
forkedFromId: null,
|
||||
preview: "",
|
||||
ephemeral: false,
|
||||
modelProvider: "openai",
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
status: { type: "idle" },
|
||||
path: null,
|
||||
cwd: "/repo",
|
||||
cliVersion: "0.139.0",
|
||||
source: "unknown",
|
||||
agentNickname: null,
|
||||
agentRole: null,
|
||||
gitInfo: null,
|
||||
name: null,
|
||||
turns: [],
|
||||
},
|
||||
model: "gpt-5.5-codex",
|
||||
modelProvider: "openai",
|
||||
serviceTier: null,
|
||||
cwd: "/repo",
|
||||
instructionSources: [],
|
||||
approvalPolicy: "never",
|
||||
approvalsReviewer: "user",
|
||||
sandbox: { type: "dangerFullAccess" },
|
||||
permissionProfile: null,
|
||||
reasoningEffort: null,
|
||||
};
|
||||
}
|
||||
|
||||
describe("requestCodexAppServerJson sandbox guard", () => {
|
||||
beforeEach(() => {
|
||||
sharedClientMocks.createIsolatedCodexAppServerClient.mockReset();
|
||||
sharedClientMocks.getSharedCodexAppServerClient.mockReset();
|
||||
sharedClientMocks.release.mockClear();
|
||||
sharedClientMocks.abandon.mockClear();
|
||||
});
|
||||
|
||||
it("fails closed before raw app-server bypass methods in sandboxed sessions", async () => {
|
||||
@@ -92,29 +35,6 @@ describe("requestCodexAppServerJson sandbox guard", () => {
|
||||
expect(sharedClientMocks.getSharedCodexAppServerClient).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses the explicit agent sandbox for globally scoped session keys", async () => {
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "command/exec",
|
||||
requestParams: { command: ["sh", "-lc", "id"] },
|
||||
config: {
|
||||
agents: {
|
||||
list: [
|
||||
{ id: "main", default: true, sandbox: { mode: "off" } },
|
||||
{ id: "work", sandbox: { mode: "all" } },
|
||||
],
|
||||
},
|
||||
},
|
||||
agentId: "work",
|
||||
sessionKey: "global-session",
|
||||
}),
|
||||
).rejects.toThrow(
|
||||
"Codex-native app-server method `command/exec` is unavailable because OpenClaw sandboxing is active for this session.",
|
||||
);
|
||||
|
||||
expect(sharedClientMocks.getSharedCodexAppServerClient).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails closed before raw app-server bypass methods when exec host=node is active", async () => {
|
||||
for (const method of ["command/exec", "process/spawn"]) {
|
||||
await expect(
|
||||
@@ -145,31 +65,7 @@ describe("requestCodexAppServerJson sandbox guard", () => {
|
||||
}),
|
||||
).resolves.toEqual({ ok: true });
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"thread/list",
|
||||
{ limit: 10 },
|
||||
expect.objectContaining({
|
||||
timeoutMs: expect.any(Number),
|
||||
signal: expect.any(AbortSignal),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("records full rate-limit reads on the physical control client", async () => {
|
||||
const snapshot = { rateLimits: { limitId: "codex", primary: { usedPercent: 12 } } };
|
||||
const client = {
|
||||
request: vi.fn(async () => snapshot),
|
||||
} as unknown as CodexAppServerClient;
|
||||
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue(client);
|
||||
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "account/rateLimits/read",
|
||||
requestParams: undefined,
|
||||
}),
|
||||
).resolves.toEqual(snapshot);
|
||||
|
||||
expect(readRecentCodexRateLimits(client)).toEqual(snapshot);
|
||||
expect(request).toHaveBeenCalledWith("thread/list", { limit: 10 }, { timeoutMs: 60_000 });
|
||||
});
|
||||
|
||||
it("fails closed for config-level exec host=node even without a session key", async () => {
|
||||
@@ -213,125 +109,11 @@ describe("requestCodexAppServerJson sandbox guard", () => {
|
||||
}),
|
||||
).resolves.toEqual({ ok: true });
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"thread/list",
|
||||
{ limit: 10 },
|
||||
expect.objectContaining({
|
||||
timeoutMs: expect.any(Number),
|
||||
signal: expect.any(AbortSignal),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("unsubscribes owned resumes but abandons a mismatched response", async () => {
|
||||
const request = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(resumeResponse("thread-1"))
|
||||
.mockResolvedValueOnce({})
|
||||
.mockRejectedValueOnce(new Error("resume response lost"))
|
||||
.mockResolvedValueOnce(resumeResponse("wrong-thread"));
|
||||
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue({
|
||||
request,
|
||||
addNotificationHandler: vi.fn(() => () => undefined),
|
||||
});
|
||||
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "thread/resume",
|
||||
requestParams: { threadId: "thread-1" },
|
||||
}),
|
||||
).resolves.toMatchObject({ thread: { id: "thread-1" } });
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "thread/resume",
|
||||
requestParams: { threadId: "thread-2" },
|
||||
}),
|
||||
).rejects.toThrow("resume response lost");
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "thread/resume",
|
||||
requestParams: { threadId: "thread-3" },
|
||||
}),
|
||||
).rejects.toThrow("Codex thread/resume returned wrong-thread for thread-3");
|
||||
|
||||
expect(request.mock.calls.map(([method, params]) => [method, params])).toEqual([
|
||||
["thread/resume", { threadId: "thread-1" }],
|
||||
["thread/unsubscribe", { threadId: "thread-1" }],
|
||||
["thread/resume", { threadId: "thread-2" }],
|
||||
["thread/resume", { threadId: "thread-3" }],
|
||||
]);
|
||||
expect(sharedClientMocks.abandon).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("does not release a thread owner when the request deadline expires before resume", async () => {
|
||||
const request = vi.fn();
|
||||
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue({ request });
|
||||
const now = vi.spyOn(Date, "now").mockReturnValueOnce(0).mockReturnValue(10);
|
||||
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "thread/resume",
|
||||
requestParams: { threadId: "thread-1" },
|
||||
timeoutMs: 1,
|
||||
}),
|
||||
).rejects.toThrow("codex app-server thread/resume timed out");
|
||||
|
||||
expect(request).not.toHaveBeenCalled();
|
||||
expect(sharedClientMocks.release).toHaveBeenCalledOnce();
|
||||
expect(sharedClientMocks.abandon).not.toHaveBeenCalled();
|
||||
now.mockRestore();
|
||||
});
|
||||
|
||||
it("retires an isolated client that resolves after the end-to-end deadline", async () => {
|
||||
let resolveClient!: (client: CodexAppServerClient) => void;
|
||||
sharedClientMocks.createIsolatedCodexAppServerClient.mockImplementationOnce(
|
||||
async () =>
|
||||
await new Promise<CodexAppServerClient>((resolve) => {
|
||||
resolveClient = resolve;
|
||||
}),
|
||||
);
|
||||
const request = vi.fn();
|
||||
const closeAndWait = vi.fn(async () => undefined);
|
||||
const response = requestCodexAppServerJson({
|
||||
method: "model/list",
|
||||
requestParams: { limit: 10 },
|
||||
isolated: true,
|
||||
timeoutMs: 5,
|
||||
});
|
||||
|
||||
await expect(response).rejects.toThrow("codex app-server model/list timed out");
|
||||
resolveClient({ request, closeAndWait } as unknown as CodexAppServerClient);
|
||||
await vi.waitFor(() => expect(closeAndWait).toHaveBeenCalledOnce());
|
||||
|
||||
expect(request).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not let isolated teardown extend the caller deadline", async () => {
|
||||
const request = vi.fn(async () => ({ data: [] }));
|
||||
const closeAndWait = vi.fn(async () => await new Promise<void>(() => {}));
|
||||
sharedClientMocks.createIsolatedCodexAppServerClient.mockResolvedValue({
|
||||
request,
|
||||
closeAndWait,
|
||||
});
|
||||
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "model/list",
|
||||
requestParams: { limit: 10 },
|
||||
isolated: true,
|
||||
timeoutMs: 5,
|
||||
}),
|
||||
).rejects.toThrow("codex app-server model/list timed out");
|
||||
|
||||
expect(request).toHaveBeenCalledOnce();
|
||||
expect(closeAndWait).toHaveBeenCalledOnce();
|
||||
expect(request).toHaveBeenCalledWith("thread/list", { limit: 10 }, { timeoutMs: 60_000 });
|
||||
});
|
||||
|
||||
it("allows sandbox-pinned thread starts in sandboxed sessions", async () => {
|
||||
const request = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ thread: { id: "thread-1" }, model: "gpt-5.5" })
|
||||
.mockResolvedValueOnce({});
|
||||
const request = vi.fn(async () => ({ thread: { id: "thread-1" }, model: "gpt-5.5" }));
|
||||
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue({ request });
|
||||
const params = {
|
||||
cwd: "/workspace",
|
||||
@@ -347,44 +129,7 @@ describe("requestCodexAppServerJson sandbox guard", () => {
|
||||
}),
|
||||
).resolves.toEqual({ thread: { id: "thread-1" }, model: "gpt-5.5" });
|
||||
|
||||
expect(request.mock.calls).toEqual([
|
||||
[
|
||||
"thread/start",
|
||||
params,
|
||||
expect.objectContaining({
|
||||
timeoutMs: expect.any(Number),
|
||||
signal: expect.any(AbortSignal),
|
||||
}),
|
||||
],
|
||||
["thread/unsubscribe", { threadId: "thread-1" }, { timeoutMs: 5_000 }],
|
||||
]);
|
||||
});
|
||||
|
||||
it("unsubscribes one-shot shared thread forks", async () => {
|
||||
const request = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ thread: { id: "child-thread" } })
|
||||
.mockResolvedValueOnce({});
|
||||
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue({ request });
|
||||
|
||||
await expect(
|
||||
requestCodexAppServerJson({
|
||||
method: "thread/fork",
|
||||
requestParams: { threadId: "parent-thread" },
|
||||
}),
|
||||
).resolves.toEqual({ thread: { id: "child-thread" } });
|
||||
|
||||
expect(request.mock.calls).toEqual([
|
||||
[
|
||||
"thread/fork",
|
||||
{ threadId: "parent-thread" },
|
||||
expect.objectContaining({
|
||||
timeoutMs: expect.any(Number),
|
||||
signal: expect.any(AbortSignal),
|
||||
}),
|
||||
],
|
||||
["thread/unsubscribe", { threadId: "child-thread" }, { timeoutMs: 5_000 }],
|
||||
]);
|
||||
expect(request).toHaveBeenCalledWith("thread/start", params, { timeoutMs: 60_000 });
|
||||
});
|
||||
|
||||
it("blocks thread starts with sandbox environments when exec host=node is active", async () => {
|
||||
|
||||
@@ -1,30 +1,21 @@
|
||||
import {
|
||||
CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
CodexAppServerUnsafeSubscriptionError,
|
||||
settleCodexAppServerClientLease,
|
||||
} from "./attempt-client-cleanup.js";
|
||||
/**
|
||||
* Sends typed JSON-RPC requests to the Codex app-server with sandbox guard
|
||||
* checks, shared-client leasing, and isolated-client shutdown handling.
|
||||
*/
|
||||
import type { resolveCodexAppServerAuthProfileIdForAgent } from "./auth-bridge.js";
|
||||
import type { CodexAppServerStartOptions } from "./config.js";
|
||||
import {
|
||||
isJsonObject,
|
||||
readCodexThreadCreationResponseId,
|
||||
type CodexAppServerRequestMethod,
|
||||
type CodexAppServerRequestParams,
|
||||
type CodexAppServerRequestResult,
|
||||
type CodexThreadResumeParams,
|
||||
type JsonValue,
|
||||
import type {
|
||||
CodexAppServerRequestMethod,
|
||||
CodexAppServerRequestParams,
|
||||
CodexAppServerRequestResult,
|
||||
JsonValue,
|
||||
} from "./protocol.js";
|
||||
import { rememberCodexRateLimitsRead } from "./rate-limit-cache.js";
|
||||
import { resolveCodexAppServerDirectSandboxBypassBlock } from "./sandbox-guard.js";
|
||||
import {
|
||||
createIsolatedCodexAppServerClient,
|
||||
leaseSharedCodexAppServerClient,
|
||||
getLeasedSharedCodexAppServerClient,
|
||||
releaseLeasedSharedCodexAppServerClient,
|
||||
} from "./shared-client.js";
|
||||
import { resumeCodexAppServerThread } from "./thread-resume.js";
|
||||
import { withTimeout } from "./timeout.js";
|
||||
|
||||
/** Sends a typed Codex app-server request and returns the method-specific response shape. */
|
||||
@@ -34,7 +25,6 @@ export async function requestCodexAppServerJson<M extends CodexAppServerRequestM
|
||||
timeoutMs?: number;
|
||||
startOptions?: CodexAppServerStartOptions;
|
||||
authProfileId?: string | null;
|
||||
agentId?: string;
|
||||
agentDir?: string;
|
||||
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
|
||||
sessionKey?: string;
|
||||
@@ -47,7 +37,6 @@ export async function requestCodexAppServerJson<T = JsonValue | undefined>(param
|
||||
timeoutMs?: number;
|
||||
startOptions?: CodexAppServerStartOptions;
|
||||
authProfileId?: string | null;
|
||||
agentId?: string;
|
||||
agentDir?: string;
|
||||
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
|
||||
sessionKey?: string;
|
||||
@@ -60,7 +49,6 @@ export async function requestCodexAppServerJson<T = JsonValue | undefined>(param
|
||||
timeoutMs?: number;
|
||||
startOptions?: CodexAppServerStartOptions;
|
||||
authProfileId?: string | null;
|
||||
agentId?: string;
|
||||
agentDir?: string;
|
||||
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
|
||||
sessionKey?: string;
|
||||
@@ -71,7 +59,6 @@ export async function requestCodexAppServerJson<T = JsonValue | undefined>(param
|
||||
method: params.method,
|
||||
requestParams: params.requestParams,
|
||||
config: params.config,
|
||||
agentId: params.agentId,
|
||||
sessionKey: params.sessionKey,
|
||||
sessionId: params.sessionId,
|
||||
});
|
||||
@@ -79,112 +66,33 @@ export async function requestCodexAppServerJson<T = JsonValue | undefined>(param
|
||||
throw new Error(sandboxBlock);
|
||||
}
|
||||
const timeoutMs = params.timeoutMs ?? 60_000;
|
||||
const timeoutMessage = `codex app-server ${params.method} timed out`;
|
||||
const abortController = new AbortController();
|
||||
const operation = (async () => {
|
||||
const startedAt = Date.now();
|
||||
const clientOptions = {
|
||||
startOptions: params.startOptions,
|
||||
timeoutMs,
|
||||
authProfileId: params.authProfileId,
|
||||
agentDir: params.agentDir,
|
||||
config: params.config,
|
||||
abandonSignal: abortController.signal,
|
||||
};
|
||||
const clientLease = params.isolated
|
||||
? undefined
|
||||
: await leaseSharedCodexAppServerClient(clientOptions);
|
||||
const client = clientLease?.client ?? (await createIsolatedCodexAppServerClient(clientOptions));
|
||||
const requestedThreadId =
|
||||
params.method === "thread/resume" && isJsonObject(params.requestParams)
|
||||
? typeof params.requestParams.threadId === "string"
|
||||
? params.requestParams.threadId
|
||||
: undefined
|
||||
: undefined;
|
||||
let subscribedThreadId: string | undefined;
|
||||
let abandonClient = false;
|
||||
try {
|
||||
abortController.signal.throwIfAborted();
|
||||
const requestTimeoutMs = remainingRequestTimeoutMs(startedAt, timeoutMs, params.method);
|
||||
let response: T;
|
||||
if (params.method === "thread/resume" && requestedThreadId) {
|
||||
subscribedThreadId = requestedThreadId;
|
||||
response = (await resumeCodexAppServerThread({
|
||||
client,
|
||||
abandonClient: clientLease
|
||||
? clientLease.abandon
|
||||
: async () =>
|
||||
await client.closeAndWait({ exitTimeoutMs: 2_000, forceKillDelayMs: 250 }),
|
||||
request: params.requestParams as CodexThreadResumeParams,
|
||||
timeoutMs: requestTimeoutMs,
|
||||
signal: abortController.signal,
|
||||
})) as T;
|
||||
} else {
|
||||
response = await client.request<T>(params.method, params.requestParams, {
|
||||
timeoutMs: requestTimeoutMs,
|
||||
signal: abortController.signal,
|
||||
});
|
||||
}
|
||||
if (params.method === "account/rateLimits/read") {
|
||||
rememberCodexRateLimitsRead(client, response as JsonValue | undefined);
|
||||
}
|
||||
if (isThreadSubscriptionMethod(params.method)) {
|
||||
const returnedThreadId = readCodexThreadCreationResponseId(response);
|
||||
if (!returnedThreadId) {
|
||||
abandonClient = true;
|
||||
throw new CodexAppServerUnsafeSubscriptionError(
|
||||
`Codex ${params.method} response omitted its thread id`,
|
||||
);
|
||||
}
|
||||
if (params.method === "thread/resume") {
|
||||
if (!requestedThreadId) {
|
||||
abandonClient = true;
|
||||
throw new CodexAppServerUnsafeSubscriptionError(
|
||||
"Codex thread/resume succeeded without a requested thread id",
|
||||
);
|
||||
}
|
||||
return await withTimeout(
|
||||
(async () => {
|
||||
const client = await (
|
||||
params.isolated ? createIsolatedCodexAppServerClient : getLeasedSharedCodexAppServerClient
|
||||
)({
|
||||
startOptions: params.startOptions,
|
||||
timeoutMs,
|
||||
authProfileId: params.authProfileId,
|
||||
agentDir: params.agentDir,
|
||||
config: params.config,
|
||||
});
|
||||
try {
|
||||
return await client.request<T>(params.method, params.requestParams, { timeoutMs });
|
||||
} finally {
|
||||
if (params.isolated) {
|
||||
// Wait for the child to actually exit (with a SIGKILL fallback) so
|
||||
// the parent process doesn't hang on an orphaned codex app-server.
|
||||
// The stdio bin shim does not always propagate stdin EOF to the
|
||||
// underlying codex binary, so the unref'd close() path can leave
|
||||
// the child running and keep the parent's event loop alive.
|
||||
await client.closeAndWait({ exitTimeoutMs: 2_000, forceKillDelayMs: 250 });
|
||||
} else {
|
||||
subscribedThreadId = returnedThreadId;
|
||||
releaseLeasedSharedCodexAppServerClient(client);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
abandonClient ||= error instanceof CodexAppServerUnsafeSubscriptionError;
|
||||
throw error;
|
||||
} finally {
|
||||
if (params.isolated) {
|
||||
// Cleanup may outlive the caller's end-to-end deadline, but the outer
|
||||
// timeout aborts all work and returns without orphaning the child.
|
||||
await client.closeAndWait({ exitTimeoutMs: 2_000, forceKillDelayMs: 250 });
|
||||
} else if (clientLease) {
|
||||
await settleCodexAppServerClientLease(clientLease, {
|
||||
threadId: subscribedThreadId,
|
||||
timeoutMs: CODEX_APP_SERVER_UNSUBSCRIBE_TIMEOUT_MS,
|
||||
abandon: abandonClient,
|
||||
});
|
||||
}
|
||||
}
|
||||
})();
|
||||
try {
|
||||
return await withTimeout(operation, timeoutMs, timeoutMessage);
|
||||
} catch (error) {
|
||||
abortController.abort(error);
|
||||
void operation.catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function remainingRequestTimeoutMs(startedAt: number, timeoutMs: number, method: string): number {
|
||||
if (timeoutMs <= 0) {
|
||||
return timeoutMs;
|
||||
}
|
||||
const remaining = timeoutMs - (Date.now() - startedAt);
|
||||
if (remaining <= 0) {
|
||||
throw new Error(`codex app-server ${method} timed out`);
|
||||
}
|
||||
return Math.max(1, remaining);
|
||||
}
|
||||
|
||||
function isThreadSubscriptionMethod(method: string): boolean {
|
||||
return method === "thread/start" || method === "thread/fork" || method === "thread/resume";
|
||||
})(),
|
||||
timeoutMs,
|
||||
`codex app-server ${params.method} timed out`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -15,29 +15,19 @@ import { clearPluginCommands } from "openclaw/plugin-sdk/plugin-runtime";
|
||||
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
|
||||
import { afterEach, beforeEach, expect, vi } from "vitest";
|
||||
import { defaultCodexAppInventoryCache } from "./app-inventory-cache.js";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import type { CodexAppServerClientFactory } from "./client-factory.js";
|
||||
import { createCodexDynamicToolBridge } from "./dynamic-tools.js";
|
||||
import type { CodexServerNotification, CodexThread } from "./protocol.js";
|
||||
import type { CodexServerNotification } from "./protocol.js";
|
||||
import { resetCodexRateLimitCacheForTests } from "./rate-limit-cache.js";
|
||||
import {
|
||||
runCodexAppServerAttempt as runCodexAppServerAttemptImpl,
|
||||
testing,
|
||||
} from "./run-attempt.js";
|
||||
import { closeCodexSandboxExecServersForTests } from "./sandbox-exec-server.js";
|
||||
import {
|
||||
registerCodexTestSessionIdentity,
|
||||
resetCodexTestBindingStore,
|
||||
testCodexAppServerBindingStore,
|
||||
} from "./session-binding.test-helpers.js";
|
||||
import type { CodexAppServerClientLeaseFactory } from "./shared-client.js";
|
||||
import {
|
||||
adaptCodexTestClientFactory,
|
||||
createCodexTestModel,
|
||||
type CodexTestAppServerClientFactory,
|
||||
} from "./test-support.js";
|
||||
import { createCodexTestModel } from "./test-support.js";
|
||||
|
||||
export let tempDir: string;
|
||||
let codexAppServerClientLeaseFactoryForTest: CodexAppServerClientLeaseFactory | undefined;
|
||||
const multiplexedTestClients = new WeakSet<CodexAppServerClient>();
|
||||
let codexAppServerClientFactoryForTest: CodexAppServerClientFactory | undefined;
|
||||
export const fastWait = { interval: 1, timeout: 5_000 } as const;
|
||||
const appServerHarnessWait = { interval: 1, timeout: 120_000 } as const;
|
||||
const activeAppServerAttemptsForTest = new Set<{
|
||||
@@ -47,12 +37,9 @@ const activeAppServerAttemptsForTest = new Set<{
|
||||
sessionKey?: string;
|
||||
}>();
|
||||
|
||||
type RunCodexAppServerAttemptImplOptions = NonNullable<
|
||||
type RunCodexAppServerAttemptOptions = NonNullable<
|
||||
Parameters<typeof runCodexAppServerAttemptImpl>[1]
|
||||
>;
|
||||
type RunCodexAppServerAttemptOptions = Omit<RunCodexAppServerAttemptImplOptions, "bindingStore"> & {
|
||||
bindingStore?: RunCodexAppServerAttemptImplOptions["bindingStore"];
|
||||
};
|
||||
|
||||
export function queueActiveRunMessageForTest(
|
||||
...args: Parameters<typeof queueAgentHarnessMessage>
|
||||
@@ -60,66 +47,19 @@ export function queueActiveRunMessageForTest(
|
||||
return queueAgentHarnessMessage(...args);
|
||||
}
|
||||
|
||||
export function setCodexAppServerClientFactoryForTest(
|
||||
factory: CodexTestAppServerClientFactory,
|
||||
): void {
|
||||
codexAppServerClientLeaseFactoryForTest = adaptCodexTestClientFactory(async (...args) => {
|
||||
const client = await factory(...args);
|
||||
const testClient = client as unknown as {
|
||||
addCloseHandler?: (handler: () => void) => () => void;
|
||||
};
|
||||
// Narrow test doubles still need the client lifecycle hook installed by
|
||||
// the keyed router, even when the test never simulates transport closure.
|
||||
testClient.addCloseHandler ??= () => () => undefined;
|
||||
multiplexTestClientHandlers(client);
|
||||
return client;
|
||||
});
|
||||
}
|
||||
|
||||
function multiplexTestClientHandlers(client: CodexAppServerClient): void {
|
||||
if (multiplexedTestClients.has(client)) {
|
||||
return;
|
||||
}
|
||||
multiplexedTestClients.add(client);
|
||||
const notificationHandlers = new Set<
|
||||
Parameters<CodexAppServerClient["addNotificationHandler"]>[0]
|
||||
>();
|
||||
const requestHandlers = new Set<Parameters<CodexAppServerClient["addRequestHandler"]>[0]>();
|
||||
const addNotificationHandler = client.addNotificationHandler.bind(client);
|
||||
const addRequestHandler = client.addRequestHandler.bind(client);
|
||||
addNotificationHandler(async (notification) => {
|
||||
await Promise.all(
|
||||
[...notificationHandlers].map((handler) => Promise.resolve(handler(notification))),
|
||||
);
|
||||
});
|
||||
addRequestHandler(async (request) => {
|
||||
for (const handler of requestHandlers) {
|
||||
const result = await handler(request);
|
||||
if (result !== undefined) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
client.addNotificationHandler = (handler) => {
|
||||
notificationHandlers.add(handler);
|
||||
return () => notificationHandlers.delete(handler);
|
||||
};
|
||||
client.addRequestHandler = (handler) => {
|
||||
requestHandlers.add(handler);
|
||||
return () => requestHandlers.delete(handler);
|
||||
};
|
||||
export function setCodexAppServerClientFactoryForTest(factory: CodexAppServerClientFactory): void {
|
||||
codexAppServerClientFactoryForTest = factory;
|
||||
}
|
||||
|
||||
function resetCodexAppServerClientFactoryForTest(): void {
|
||||
codexAppServerClientLeaseFactoryForTest = undefined;
|
||||
codexAppServerClientFactoryForTest = undefined;
|
||||
}
|
||||
|
||||
export function runCodexAppServerAttempt(
|
||||
params: EmbeddedRunAttemptParams,
|
||||
options: RunCodexAppServerAttemptOptions = {},
|
||||
) {
|
||||
const clientLeaseFactory = options.clientLeaseFactory ?? codexAppServerClientLeaseFactoryForTest;
|
||||
const clientFactory = options.clientFactory ?? codexAppServerClientFactoryForTest;
|
||||
const abortController = params.abortSignal ? undefined : new AbortController();
|
||||
const trackedParams = abortController
|
||||
? ({ ...params, abortSignal: abortController.signal } as EmbeddedRunAttemptParams)
|
||||
@@ -130,11 +70,10 @@ export function runCodexAppServerAttempt(
|
||||
sessionId: params.sessionId,
|
||||
sessionKey: params.sessionKey,
|
||||
};
|
||||
const promise = runCodexAppServerAttemptImpl(trackedParams, {
|
||||
...options,
|
||||
bindingStore: options.bindingStore ?? testCodexAppServerBindingStore,
|
||||
...(clientLeaseFactory ? { clientLeaseFactory } : {}),
|
||||
}).finally(() => {
|
||||
const promise = runCodexAppServerAttemptImpl(
|
||||
trackedParams,
|
||||
clientFactory ? { ...options, clientFactory } : options,
|
||||
).finally(() => {
|
||||
activeAppServerAttemptsForTest.delete(entry);
|
||||
});
|
||||
entry.promise = promise;
|
||||
@@ -182,7 +121,6 @@ async function drainActiveAppServerAttemptsForTest(): Promise<void> {
|
||||
}
|
||||
|
||||
export function createParams(sessionFile: string, workspaceDir: string): EmbeddedRunAttemptParams {
|
||||
registerCodexTestSessionIdentity(sessionFile, "session-1", "agent:main:session-1");
|
||||
return {
|
||||
prompt: "hello",
|
||||
sessionId: "session-1",
|
||||
@@ -285,7 +223,7 @@ export function threadStartResult(threadId = "thread-1") {
|
||||
agentRole: null,
|
||||
gitInfo: null,
|
||||
name: null,
|
||||
turns: [] as CodexThread["turns"],
|
||||
turns: [],
|
||||
},
|
||||
model: "gpt-5.4-codex",
|
||||
modelProvider: "openai",
|
||||
@@ -348,73 +286,61 @@ export function createAppServerHarness(
|
||||
} = {},
|
||||
) {
|
||||
const requests: Array<{ method: string; params: unknown }> = [];
|
||||
const notificationHandlers = new Set<
|
||||
(notification: CodexServerNotification) => Promise<void> | void
|
||||
>();
|
||||
const serverRequestHandlers = new Set<AppServerRequestHandler>();
|
||||
let notifyHandler: ((notification: CodexServerNotification) => Promise<void>) | undefined;
|
||||
let handleServerRequest: AppServerRequestHandler | undefined;
|
||||
const closeHandlers = new Set<() => void>();
|
||||
const request = vi.fn(async (method: string, params?: unknown, requestOptions?: unknown) => {
|
||||
requests.push({ method, params });
|
||||
return requestImpl(method, params, requestOptions as { signal?: AbortSignal } | undefined);
|
||||
});
|
||||
|
||||
const client = {
|
||||
getServerVersion: () => "0.132.0",
|
||||
request,
|
||||
addNotificationHandler: (
|
||||
handler: (notification: CodexServerNotification) => Promise<void> | void,
|
||||
) => {
|
||||
notificationHandlers.add(handler);
|
||||
return () => notificationHandlers.delete(handler);
|
||||
},
|
||||
addRequestHandler: (handler: AppServerRequestHandler) => {
|
||||
serverRequestHandlers.add(handler);
|
||||
return () => serverRequestHandlers.delete(handler);
|
||||
},
|
||||
addCloseHandler: (handler: () => void) => {
|
||||
closeHandlers.add(handler);
|
||||
return () => closeHandlers.delete(handler);
|
||||
},
|
||||
} as unknown as CodexAppServerClient;
|
||||
setCodexAppServerClientFactoryForTest(async (_startOptions, authProfileId, agentDir) => {
|
||||
options.onStart?.(authProfileId, agentDir);
|
||||
return client;
|
||||
return {
|
||||
getServerVersion: () => "0.132.0",
|
||||
request,
|
||||
addNotificationHandler: (
|
||||
handler: (notification: CodexServerNotification) => Promise<void>,
|
||||
) => {
|
||||
notifyHandler = handler;
|
||||
return () => {
|
||||
if (notifyHandler === handler) {
|
||||
notifyHandler = undefined;
|
||||
}
|
||||
};
|
||||
},
|
||||
addRequestHandler: (handler: AppServerRequestHandler) => {
|
||||
handleServerRequest = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
addCloseHandler: (handler: () => void) => {
|
||||
closeHandlers.add(handler);
|
||||
return () => closeHandlers.delete(handler);
|
||||
},
|
||||
} as never;
|
||||
});
|
||||
|
||||
const waitForServerRequestHandler = async () => {
|
||||
await vi.waitFor(() => expect(serverRequestHandlers.size).toBeGreaterThan(0), {
|
||||
await vi.waitFor(() => expect(handleServerRequest).toBeTypeOf("function"), {
|
||||
interval: 1,
|
||||
timeout: appServerHarnessWait.timeout,
|
||||
});
|
||||
return async (requestLocal: Parameters<AppServerRequestHandler>[0]) => {
|
||||
for (const handler of serverRequestHandlers) {
|
||||
const result = await handler(requestLocal);
|
||||
if (result !== undefined) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
return handleServerRequest!;
|
||||
};
|
||||
|
||||
const waitForNotificationHandler = async () => {
|
||||
await vi.waitFor(() => expect(notificationHandlers.size).toBeGreaterThan(0), {
|
||||
await vi.waitFor(() => expect(notifyHandler).toBeTypeOf("function"), {
|
||||
interval: 1,
|
||||
timeout: appServerHarnessWait.timeout,
|
||||
});
|
||||
return async (notification: CodexServerNotification) => {
|
||||
await Promise.all(
|
||||
[...notificationHandlers].map((handler) => Promise.resolve(handler(notification))),
|
||||
);
|
||||
};
|
||||
return notifyHandler!;
|
||||
};
|
||||
const sendNotification = async (notification: CodexServerNotification) => {
|
||||
const handler = await waitForNotificationHandler();
|
||||
const handler = notifyHandler ?? (await waitForNotificationHandler());
|
||||
await handler(notification);
|
||||
};
|
||||
|
||||
return {
|
||||
client,
|
||||
request,
|
||||
requests,
|
||||
waitForMethod: async (method: string, timeoutMs: number = appServerHarnessWait.timeout) => {
|
||||
@@ -487,10 +413,9 @@ export function createStartedThreadHarness(
|
||||
}
|
||||
|
||||
export function createResumeHarness() {
|
||||
return createAppServerHarness(async (method, params) => {
|
||||
return createAppServerHarness(async (method) => {
|
||||
if (method === "thread/resume") {
|
||||
const threadId = (params as { threadId?: unknown }).threadId;
|
||||
return threadStartResult(typeof threadId === "string" ? threadId : "thread-existing");
|
||||
return threadStartResult("thread-existing");
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
return turnStartResult();
|
||||
@@ -574,7 +499,6 @@ export function setupRunAttemptTestHooks(): void {
|
||||
clearMemoryPluginState();
|
||||
resetAgentEventsForTest();
|
||||
resetDiagnosticEventsForTest();
|
||||
resetCodexTestBindingStore();
|
||||
vi.stubEnv("OPENCLAW_TRAJECTORY", "0");
|
||||
vi.stubEnv("CODEX_API_KEY", "");
|
||||
vi.stubEnv("OPENAI_API_KEY", "");
|
||||
@@ -588,6 +512,7 @@ export function setupRunAttemptTestHooks(): void {
|
||||
testing.resetOpenClawCodingToolsFactoryForTests();
|
||||
testing.resetEnsuredCodexWorkspaceDirsForTests();
|
||||
testing.clearPendingCodexNativeHookRelayUnregistersForTests();
|
||||
resetCodexRateLimitCacheForTests();
|
||||
nativeHookRelayTesting.clearNativeHookRelaysForTests();
|
||||
clearMemoryPluginState();
|
||||
clearPluginCommands();
|
||||
|
||||
@@ -7,35 +7,10 @@ import {
|
||||
type EmbeddedRunAttemptParams,
|
||||
} from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { CodexAppServerRpcError } from "./client.js";
|
||||
import type { CodexAppServerClientFactory } from "./client-factory.js";
|
||||
import type { CodexServerNotification } from "./protocol.js";
|
||||
import { runCodexAppServerAttempt } from "./run-attempt.js";
|
||||
import { createCodexTestBindingStore } from "./session-binding.test-helpers.js";
|
||||
import {
|
||||
adaptCodexTestClientFactory,
|
||||
createCodexTestModel,
|
||||
type CodexTestAppServerClientFactory,
|
||||
} from "./test-support.js";
|
||||
|
||||
const configRuntimeMock = vi.hoisted(() => ({ rejectedProvider: undefined as string | undefined }));
|
||||
|
||||
vi.mock("./config.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("./config.js")>();
|
||||
return {
|
||||
...actual,
|
||||
resolveCodexAppServerRuntime: (
|
||||
params: Parameters<typeof actual.resolveCodexAppServerRuntime>[0],
|
||||
) => {
|
||||
if (
|
||||
configRuntimeMock.rejectedProvider &&
|
||||
params?.modelProvider === configRuntimeMock.rejectedProvider
|
||||
) {
|
||||
throw new Error(`rejected active provider: ${params.modelProvider}`);
|
||||
}
|
||||
return actual.resolveCodexAppServerRuntime(params);
|
||||
},
|
||||
};
|
||||
});
|
||||
import { createCodexTestModel } from "./test-support.js";
|
||||
|
||||
let tempDir: string;
|
||||
|
||||
@@ -115,7 +90,6 @@ describe("Codex app-server main thread cleanup", () => {
|
||||
vi.stubEnv("OPENCLAW_TRAJECTORY", "0");
|
||||
vi.stubEnv("CODEX_API_KEY", "");
|
||||
vi.stubEnv("OPENAI_API_KEY", "");
|
||||
configRuntimeMock.rejectedProvider = undefined;
|
||||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-run-cleanup-"));
|
||||
});
|
||||
|
||||
@@ -131,9 +105,7 @@ describe("Codex app-server main thread cleanup", () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const requests: Array<{ method: string; params: unknown }> = [];
|
||||
const notificationHandlers = new Set<
|
||||
(notification: CodexServerNotification) => Promise<void> | void
|
||||
>();
|
||||
let notify: (notification: CodexServerNotification) => Promise<void> = async () => undefined;
|
||||
const request = vi.fn(async (method: string, params?: unknown) => {
|
||||
requests.push({ method, params });
|
||||
if (method === "thread/start") {
|
||||
@@ -145,36 +117,32 @@ describe("Codex app-server main thread cleanup", () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
const clientFactory: CodexTestAppServerClientFactory = async () => {
|
||||
const clientFactory: CodexAppServerClientFactory = async () => {
|
||||
return {
|
||||
request,
|
||||
addNotificationHandler: (handler: (notification: CodexServerNotification) => void) => {
|
||||
notificationHandlers.add(handler);
|
||||
return () => notificationHandlers.delete(handler);
|
||||
addNotificationHandler: (handler: typeof notify) => {
|
||||
notify = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
addRequestHandler: () => () => undefined,
|
||||
addCloseHandler: () => () => undefined,
|
||||
} as never;
|
||||
};
|
||||
|
||||
const run = runCodexAppServerAttempt(createParams(sessionFile, workspaceDir), {
|
||||
bindingStore: createCodexTestBindingStore(),
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(clientFactory),
|
||||
clientFactory,
|
||||
});
|
||||
await vi.waitFor(() => expect(requests.map((entry) => entry.method)).toContain("turn/start"), {
|
||||
interval: 1,
|
||||
timeout: 5_000,
|
||||
});
|
||||
for (const handler of notificationHandlers) {
|
||||
await handler({
|
||||
method: "turn/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
turn: { id: "turn-1", status: "completed" },
|
||||
},
|
||||
});
|
||||
}
|
||||
await notify({
|
||||
method: "turn/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
turn: { id: "turn-1", status: "completed" },
|
||||
},
|
||||
});
|
||||
|
||||
const result = await run;
|
||||
expect(result.aborted).toBe(false);
|
||||
@@ -205,27 +173,19 @@ describe("Codex app-server main thread cleanup", () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
const clientFactory: CodexTestAppServerClientFactory = async () => {
|
||||
const clientFactory: CodexAppServerClientFactory = async () => {
|
||||
return {
|
||||
request,
|
||||
addNotificationHandler: () => () => undefined,
|
||||
addRequestHandler: () => () => undefined,
|
||||
addCloseHandler: () => () => undefined,
|
||||
} as never;
|
||||
};
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
|
||||
await expect(
|
||||
runCodexAppServerAttempt(createParams(sessionFile, workspaceDir), {
|
||||
bindingStore: createCodexTestBindingStore(),
|
||||
clientLeaseFactory: async () => ({
|
||||
client: await clientFactory(),
|
||||
release: () => undefined,
|
||||
abandon,
|
||||
}),
|
||||
clientFactory,
|
||||
}),
|
||||
).rejects.toThrow("turn start exploded");
|
||||
expect(abandon).toHaveBeenCalledOnce();
|
||||
expect(requests.map((entry) => entry.method)).toEqual([
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
@@ -237,162 +197,4 @@ describe("Codex app-server main thread cleanup", () => {
|
||||
{ timeoutMs: 5_000 },
|
||||
);
|
||||
});
|
||||
|
||||
it("releases startup ownership when authoritative provider policy rejects", async () => {
|
||||
const sessionFile = path.join(tempDir, "session-policy-rejection.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace-policy-rejection");
|
||||
const request = vi.fn(async (method: string) => {
|
||||
if (method === "thread/start") {
|
||||
const response = threadStartResult();
|
||||
return {
|
||||
...response,
|
||||
thread: { ...response.thread, modelProvider: "lmstudio" },
|
||||
model: "local-model",
|
||||
modelProvider: "lmstudio",
|
||||
};
|
||||
}
|
||||
if (method === "thread/unsubscribe") {
|
||||
return {};
|
||||
}
|
||||
throw new Error(`unexpected method: ${method}`);
|
||||
});
|
||||
const release = vi.fn();
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
configRuntimeMock.rejectedProvider = "lmstudio";
|
||||
|
||||
await expect(
|
||||
runCodexAppServerAttempt(createParams(sessionFile, workspaceDir), {
|
||||
bindingStore: createCodexTestBindingStore(),
|
||||
clientLeaseFactory: async () => ({
|
||||
client: {
|
||||
request,
|
||||
addNotificationHandler: () => () => undefined,
|
||||
addRequestHandler: () => () => undefined,
|
||||
addCloseHandler: () => () => undefined,
|
||||
} as never,
|
||||
release,
|
||||
abandon,
|
||||
}),
|
||||
}),
|
||||
).rejects.toThrow("rejected active provider: lmstudio");
|
||||
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"thread/unsubscribe",
|
||||
{ threadId: "thread-1" },
|
||||
{ timeoutMs: 5_000 },
|
||||
);
|
||||
expect(release).toHaveBeenCalledOnce();
|
||||
expect(abandon).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps the main client reusable after a structured turn rejection", async () => {
|
||||
const sessionFile = path.join(tempDir, "session-rpc-rejection.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace-rpc-rejection");
|
||||
const request = vi.fn(async (method: string) => {
|
||||
if (method === "thread/start") {
|
||||
return threadStartResult();
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
throw new CodexAppServerRpcError({ code: -32000, message: "turn rejected" }, method);
|
||||
}
|
||||
return {};
|
||||
});
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
|
||||
await expect(
|
||||
runCodexAppServerAttempt(createParams(sessionFile, workspaceDir), {
|
||||
bindingStore: createCodexTestBindingStore(),
|
||||
clientLeaseFactory: async () => ({
|
||||
client: {
|
||||
request,
|
||||
addNotificationHandler: () => () => undefined,
|
||||
addRequestHandler: () => () => undefined,
|
||||
addCloseHandler: () => () => undefined,
|
||||
} as never,
|
||||
release: () => undefined,
|
||||
abandon,
|
||||
}),
|
||||
}),
|
||||
).rejects.toThrow("turn rejected");
|
||||
|
||||
expect(abandon).not.toHaveBeenCalled();
|
||||
expect(request).toHaveBeenCalledWith(
|
||||
"thread/unsubscribe",
|
||||
{ threadId: "thread-1" },
|
||||
{ timeoutMs: 5_000 },
|
||||
);
|
||||
});
|
||||
|
||||
it("reuses one client router after each attempt releases its thread route", async () => {
|
||||
const sessionFile = path.join(tempDir, "session-reused.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace-reused");
|
||||
const bindingStore = createCodexTestBindingStore();
|
||||
const notificationHandlers = new Set<
|
||||
(notification: CodexServerNotification) => Promise<void> | void
|
||||
>();
|
||||
const requestHandlers = new Set<(request: unknown) => unknown>();
|
||||
let turnIndex = 0;
|
||||
const request = vi.fn(async (method: string) => {
|
||||
if (method === "thread/start" || method === "thread/resume") {
|
||||
return threadStartResult();
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
turnIndex += 1;
|
||||
return turnStartResult(`turn-${turnIndex}`);
|
||||
}
|
||||
return {};
|
||||
});
|
||||
const addNotificationHandler = vi.fn(
|
||||
(handler: (notification: CodexServerNotification) => Promise<void> | void) => {
|
||||
notificationHandlers.add(handler);
|
||||
return () => notificationHandlers.delete(handler);
|
||||
},
|
||||
);
|
||||
const addRequestHandler = vi.fn((handler: (request: unknown) => unknown) => {
|
||||
requestHandlers.add(handler);
|
||||
return () => requestHandlers.delete(handler);
|
||||
});
|
||||
const client = {
|
||||
request,
|
||||
addNotificationHandler,
|
||||
addRequestHandler,
|
||||
addCloseHandler: () => () => undefined,
|
||||
};
|
||||
const clientFactory: CodexTestAppServerClientFactory = async () => client as never;
|
||||
|
||||
const runAttempt = async (turnId: string, expectedTurnStartCount: number) => {
|
||||
const run = runCodexAppServerAttempt(createParams(sessionFile, workspaceDir), {
|
||||
bindingStore,
|
||||
clientLeaseFactory: adaptCodexTestClientFactory(clientFactory),
|
||||
});
|
||||
await vi.waitFor(
|
||||
() =>
|
||||
expect(request.mock.calls.filter(([method]) => method === "turn/start")).toHaveLength(
|
||||
expectedTurnStartCount,
|
||||
),
|
||||
{ interval: 1, timeout: 5_000 },
|
||||
);
|
||||
for (const handler of notificationHandlers) {
|
||||
void handler({
|
||||
method: "turn/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId,
|
||||
turn: { id: turnId, threadId: "thread-1", status: "completed" },
|
||||
},
|
||||
});
|
||||
}
|
||||
return await run;
|
||||
};
|
||||
|
||||
await expect(runAttempt("turn-1", 1)).resolves.toMatchObject({ aborted: false });
|
||||
const notificationHandlerCount = addNotificationHandler.mock.calls.length;
|
||||
const requestHandlerCount = addRequestHandler.mock.calls.length;
|
||||
await expect(runAttempt("turn-2", 2)).resolves.toMatchObject({ aborted: false });
|
||||
|
||||
expect(addNotificationHandler).toHaveBeenCalledTimes(notificationHandlerCount);
|
||||
expect(addRequestHandler).toHaveBeenCalledTimes(requestHandlerCount);
|
||||
expect(notificationHandlers.size).toBe(notificationHandlerCount);
|
||||
expect(requestHandlers.size).toBe(requestHandlerCount);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,63 +11,39 @@ import {
|
||||
import { SessionManager } from "openclaw/plugin-sdk/agent-sessions";
|
||||
import { registerSandboxBackend } from "openclaw/plugin-sdk/sandbox";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { CodexAppServerClient } from "./client.js";
|
||||
import type { CodexAppServerClientFactory } from "./client-factory.js";
|
||||
import type { CodexServerNotification } from "./protocol.js";
|
||||
import { runCodexAppServerAttempt as runCodexAppServerAttemptImpl } from "./run-attempt.js";
|
||||
import {
|
||||
clearCodexAppServerBinding,
|
||||
readCodexAppServerBinding,
|
||||
registerCodexTestSessionIdentity,
|
||||
resetCodexTestBindingStore,
|
||||
testCodexAppServerBindingStore,
|
||||
writeCodexAppServerBinding as writeStoredCodexAppServerBinding,
|
||||
} from "./session-binding.test-helpers.js";
|
||||
import type { CodexAppServerClientLeaseFactory } from "./shared-client.js";
|
||||
import {
|
||||
adaptCodexTestClientFactory,
|
||||
createCodexTestModel,
|
||||
type CodexTestAppServerClientFactory,
|
||||
} from "./test-support.js";
|
||||
import { readCodexAppServerBinding, writeCodexAppServerBinding } from "./session-binding.js";
|
||||
import { createCodexTestModel } from "./test-support.js";
|
||||
|
||||
let tempDir: string;
|
||||
let codexAppServerClientLeaseFactoryForTest: CodexAppServerClientLeaseFactory | undefined;
|
||||
let codexAppServerClientFactoryForTest: CodexAppServerClientFactory | undefined;
|
||||
|
||||
type RunCodexAppServerAttemptImplOptions = NonNullable<
|
||||
type RunCodexAppServerAttemptOptions = NonNullable<
|
||||
Parameters<typeof runCodexAppServerAttemptImpl>[1]
|
||||
>;
|
||||
type RunCodexAppServerAttemptOptions = Omit<RunCodexAppServerAttemptImplOptions, "bindingStore"> & {
|
||||
bindingStore?: RunCodexAppServerAttemptImplOptions["bindingStore"];
|
||||
};
|
||||
|
||||
function setCodexAppServerClientFactoryForTest(factory: CodexTestAppServerClientFactory): void {
|
||||
codexAppServerClientLeaseFactoryForTest = adaptCodexTestClientFactory(factory);
|
||||
function setCodexAppServerClientFactoryForTest(factory: CodexAppServerClientFactory): void {
|
||||
codexAppServerClientFactoryForTest = factory;
|
||||
}
|
||||
|
||||
function resetCodexAppServerClientFactoryForTest(): void {
|
||||
codexAppServerClientLeaseFactoryForTest = undefined;
|
||||
}
|
||||
|
||||
async function writeCodexAppServerBinding(
|
||||
...args: Parameters<typeof writeStoredCodexAppServerBinding>
|
||||
): Promise<void> {
|
||||
registerCodexTestSessionIdentity(args[0], "session-1", "agent:main:session-1");
|
||||
await writeStoredCodexAppServerBinding(...args);
|
||||
codexAppServerClientFactoryForTest = undefined;
|
||||
}
|
||||
|
||||
function runCodexAppServerAttempt(
|
||||
params: EmbeddedRunAttemptParams,
|
||||
options: RunCodexAppServerAttemptOptions = {},
|
||||
) {
|
||||
const clientLeaseFactory = options.clientLeaseFactory ?? codexAppServerClientLeaseFactoryForTest;
|
||||
return runCodexAppServerAttemptImpl(params, {
|
||||
...options,
|
||||
bindingStore: options.bindingStore ?? testCodexAppServerBindingStore,
|
||||
...(clientLeaseFactory ? { clientLeaseFactory } : {}),
|
||||
});
|
||||
const clientFactory = options.clientFactory ?? codexAppServerClientFactoryForTest;
|
||||
return runCodexAppServerAttemptImpl(
|
||||
params,
|
||||
clientFactory ? { ...options, clientFactory } : options,
|
||||
);
|
||||
}
|
||||
|
||||
function createParams(sessionFile: string, workspaceDir: string): EmbeddedRunAttemptParams {
|
||||
registerCodexTestSessionIdentity(sessionFile, "session-1", "agent:main:session-1");
|
||||
return {
|
||||
prompt: "hello",
|
||||
sessionId: "session-1",
|
||||
@@ -185,10 +161,7 @@ function createStartedThreadHarness(
|
||||
requestImpl: (method: string, params: unknown) => Promise<unknown> = async () => undefined,
|
||||
) {
|
||||
const requests: Array<{ method: string; params: unknown }> = [];
|
||||
const notificationHandlers = new Set<
|
||||
(notification: CodexServerNotification) => Promise<void> | void
|
||||
>();
|
||||
const requestHandlers = new Set<(request: unknown) => unknown>();
|
||||
let notify: (notification: CodexServerNotification) => Promise<void> = async () => undefined;
|
||||
const request = vi.fn(async (method: string, params?: unknown) => {
|
||||
requests.push({ method, params });
|
||||
const override = await requestImpl(method, params);
|
||||
@@ -204,37 +177,20 @@ function createStartedThreadHarness(
|
||||
return {};
|
||||
});
|
||||
|
||||
const client = {
|
||||
request,
|
||||
addNotificationHandler: (
|
||||
handler: (notification: CodexServerNotification) => Promise<void> | void,
|
||||
) => {
|
||||
notificationHandlers.add(handler);
|
||||
return () => notificationHandlers.delete(handler);
|
||||
},
|
||||
addRequestHandler: (handler: (request: unknown) => unknown) => {
|
||||
requestHandlers.add(handler);
|
||||
return () => requestHandlers.delete(handler);
|
||||
},
|
||||
addCloseHandler: () => () => undefined,
|
||||
} as unknown as CodexAppServerClient;
|
||||
setCodexAppServerClientFactoryForTest(async () => client);
|
||||
|
||||
const notify = async (notification: CodexServerNotification) => {
|
||||
await Promise.all(
|
||||
[...notificationHandlers].map((handler) => Promise.resolve(handler(notification))),
|
||||
);
|
||||
};
|
||||
setCodexAppServerClientFactoryForTest(
|
||||
async () =>
|
||||
({
|
||||
request,
|
||||
addNotificationHandler: (handler: typeof notify) => {
|
||||
notify = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
addRequestHandler: () => () => undefined,
|
||||
}) as never,
|
||||
);
|
||||
|
||||
return {
|
||||
client,
|
||||
requests,
|
||||
async handleServerRequest(serverRequest: unknown) {
|
||||
const responses = await Promise.all(
|
||||
[...requestHandlers].map((handler) => Promise.resolve(handler(serverRequest))),
|
||||
);
|
||||
return responses[0];
|
||||
},
|
||||
async waitForMethod(method: string) {
|
||||
await vi.waitFor(() => expect(requests.map((entry) => entry.method)).toContain(method), {
|
||||
interval: 1,
|
||||
@@ -350,7 +306,6 @@ function getRequestInputTextAt(
|
||||
|
||||
describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
beforeEach(async () => {
|
||||
resetCodexTestBindingStore();
|
||||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-context-engine-"));
|
||||
});
|
||||
|
||||
@@ -593,14 +548,13 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
await secondRun;
|
||||
});
|
||||
|
||||
it("resumes a matching thread-bootstrap binding without a native usage snapshot", async () => {
|
||||
it("resumes a matching thread-bootstrap binding even when the bootstrap turn exceeded the opt-in native byte guard", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const agentDir = path.join(tempDir, "agent");
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-bootstrapped",
|
||||
cwd: workspaceDir,
|
||||
model: "gpt-5.4-codex",
|
||||
modelProvider: "openai",
|
||||
dynamicToolsFingerprint: "[]",
|
||||
contextEngine: {
|
||||
schemaVersion: 1,
|
||||
@@ -614,6 +568,21 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
await fs.writeFile(
|
||||
path.join(path.dirname(sessionFile), "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:session-1": {
|
||||
sessionFile,
|
||||
totalTokens: 12_000,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
||||
await fs.mkdir(rolloutDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(rolloutDir, "rollout-thread-bootstrapped.jsonl"),
|
||||
"x".repeat(2_000),
|
||||
);
|
||||
const contextEngine = createContextEngine({
|
||||
assemble: vi.fn(async ({ prompt }) => ({
|
||||
messages: [
|
||||
@@ -625,11 +594,28 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
contextProjection: { mode: "thread_bootstrap" as const, epoch: "epoch-1" },
|
||||
})),
|
||||
});
|
||||
const harness = createStartedThreadHarness(async (method) =>
|
||||
method === "thread/resume" ? threadStartResult("thread-bootstrapped") : undefined,
|
||||
);
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method === "thread/resume") {
|
||||
return threadStartResult("thread-bootstrapped");
|
||||
}
|
||||
if (method === "thread/start") {
|
||||
return threadStartResult("thread-fresh");
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.agentDir = agentDir;
|
||||
params.contextEngine = contextEngine;
|
||||
params.config = {
|
||||
agents: {
|
||||
defaults: {
|
||||
compaction: {
|
||||
truncateAfterCompaction: true,
|
||||
maxActiveTranscriptBytes: 1_000,
|
||||
},
|
||||
},
|
||||
},
|
||||
} as EmbeddedRunAttemptParams["config"];
|
||||
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
await harness.waitForMethod("turn/start");
|
||||
@@ -647,263 +633,14 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
await run;
|
||||
});
|
||||
|
||||
it("projects assembled context when the binding changes during startup", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-old",
|
||||
cwd: workspaceDir,
|
||||
dynamicToolsFingerprint: "[]",
|
||||
contextEngine: {
|
||||
schemaVersion: 1,
|
||||
engineId: "lossless-claw",
|
||||
policyFingerprint:
|
||||
'{"schemaVersion":1,"engineId":"lossless-claw","ownsCompaction":true,"projectionMaxChars":24000}',
|
||||
projection: {
|
||||
schemaVersion: 1,
|
||||
mode: "thread_bootstrap",
|
||||
epoch: "epoch-1",
|
||||
},
|
||||
},
|
||||
});
|
||||
const contextEngine = createContextEngine({
|
||||
assemble: vi.fn(async ({ prompt }) => {
|
||||
// Simulate a concurrent /codex resume or reset after the initial read.
|
||||
await clearCodexAppServerBinding(sessionFile);
|
||||
return {
|
||||
messages: [
|
||||
assistantMessage("assembled startup context", 10),
|
||||
userMessage(prompt ?? "", 11),
|
||||
],
|
||||
estimatedTokens: 42,
|
||||
systemPromptAddition: "context-engine system",
|
||||
contextProjection: { mode: "thread_bootstrap" as const, epoch: "epoch-1" },
|
||||
};
|
||||
}),
|
||||
});
|
||||
const harness = createStartedThreadHarness(async (method) =>
|
||||
method === "thread/start" ? threadStartResult("thread-fresh") : undefined,
|
||||
);
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.contextEngine = contextEngine;
|
||||
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
await harness.waitForMethod("turn/start");
|
||||
|
||||
expect(harness.requests.map((request) => request.method)).toEqual([
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
]);
|
||||
expectRequestInputTextContains(harness, "assembled startup context");
|
||||
await harness.completeTurn("completed", "thread-fresh");
|
||||
await run;
|
||||
await expect(readCodexAppServerBinding(sessionFile)).resolves.toMatchObject({
|
||||
threadId: "thread-fresh",
|
||||
contextEngine: {
|
||||
projection: { mode: "thread_bootstrap", epoch: "epoch-1" },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("awaits accepted-turn interruption and retires the client when it cannot be confirmed", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const contextEngine = createContextEngine({
|
||||
assemble: vi.fn(async ({ prompt }) => ({
|
||||
messages: [assistantMessage("projected context", 10), userMessage(prompt ?? "", 11)],
|
||||
estimatedTokens: 42,
|
||||
contextProjection: { mode: "thread_bootstrap" as const, epoch: "epoch-1" },
|
||||
})),
|
||||
});
|
||||
let rejectTurnInterrupt!: (reason?: unknown) => void;
|
||||
const turnInterrupt = new Promise<unknown>((_resolve, reject) => {
|
||||
rejectTurnInterrupt = reject;
|
||||
});
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method === "thread/start") {
|
||||
return threadStartResult("thread-fresh");
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
return turnStartResult("turn-fresh");
|
||||
}
|
||||
if (method === "turn/interrupt") {
|
||||
return await turnInterrupt;
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
let releaseProjectionCommit!: () => void;
|
||||
let markProjectionCommitStarted!: () => void;
|
||||
const projectionCommitStarted = new Promise<void>((resolve) => {
|
||||
markProjectionCommitStarted = resolve;
|
||||
});
|
||||
const projectionCommitReleased = new Promise<void>((resolve) => {
|
||||
releaseProjectionCommit = resolve;
|
||||
});
|
||||
const bindingStore: RunCodexAppServerAttemptImplOptions["bindingStore"] = {
|
||||
...testCodexAppServerBindingStore,
|
||||
async mutate(identity, mutation) {
|
||||
if (
|
||||
mutation.kind === "patch" &&
|
||||
mutation.patch.contextEngine?.projection?.epoch === "epoch-1"
|
||||
) {
|
||||
markProjectionCommitStarted();
|
||||
await projectionCommitReleased;
|
||||
return false;
|
||||
}
|
||||
return await testCodexAppServerBindingStore.mutate(identity, mutation);
|
||||
},
|
||||
};
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.contextEngine = contextEngine;
|
||||
const onAgentEvent = vi.fn();
|
||||
params.onAgentEvent = onAgentEvent;
|
||||
const releaseClient = vi.fn();
|
||||
const abandonClient = vi.fn(async () => undefined);
|
||||
|
||||
const run = runCodexAppServerAttempt(params, {
|
||||
bindingStore,
|
||||
clientLeaseFactory: async () => ({
|
||||
client: harness.client,
|
||||
release: releaseClient,
|
||||
abandon: abandonClient,
|
||||
}),
|
||||
});
|
||||
await projectionCommitStarted;
|
||||
const bufferedToolRequest = harness.handleServerRequest({
|
||||
id: "tool-after-accepted-turn",
|
||||
method: "item/tool/call",
|
||||
params: {
|
||||
threadId: "thread-fresh",
|
||||
turnId: "turn-fresh",
|
||||
callId: "call-1",
|
||||
namespace: null,
|
||||
tool: "python",
|
||||
arguments: { code: "print('must not run')" },
|
||||
},
|
||||
});
|
||||
releaseProjectionCommit();
|
||||
await harness.waitForMethod("turn/interrupt");
|
||||
const runSettled = vi.fn();
|
||||
void run.then(runSettled, runSettled);
|
||||
await new Promise((resolve) => {
|
||||
setImmediate(resolve);
|
||||
});
|
||||
expect(runSettled).not.toHaveBeenCalled();
|
||||
expect(abandonClient).not.toHaveBeenCalled();
|
||||
|
||||
rejectTurnInterrupt(new Error("interrupt response lost"));
|
||||
await expect(run).rejects.toThrow("binding changed before context projection commit");
|
||||
await expect(bufferedToolRequest).resolves.toBeUndefined();
|
||||
expect(abandonClient).toHaveBeenCalledOnce();
|
||||
expect(releaseClient).not.toHaveBeenCalled();
|
||||
expect(onAgentEvent).not.toHaveBeenCalledWith(expect.objectContaining({ stream: "tool" }));
|
||||
expect(
|
||||
harness.requests.some(
|
||||
(request) =>
|
||||
request.method === "turn/interrupt" &&
|
||||
(request.params as { threadId?: string; turnId?: string }).threadId === "thread-fresh" &&
|
||||
(request.params as { threadId?: string; turnId?: string }).turnId === "turn-fresh",
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("invalidates the projection and preserves fresh usage after native compaction", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-bootstrapped",
|
||||
cwd: workspaceDir,
|
||||
model: "gpt-5.4-codex",
|
||||
modelProvider: "openai",
|
||||
dynamicToolsFingerprint: "[]",
|
||||
nativeContextUsage: { currentTokens: 220_000 },
|
||||
modelContextWindow: 258_400,
|
||||
contextEngine: {
|
||||
schemaVersion: 1,
|
||||
engineId: "lossless-claw",
|
||||
policyFingerprint:
|
||||
'{"schemaVersion":1,"engineId":"lossless-claw","ownsCompaction":true,"projectionMaxChars":24000}',
|
||||
projection: {
|
||||
schemaVersion: 1,
|
||||
mode: "thread_bootstrap",
|
||||
epoch: "epoch-1",
|
||||
fingerprint: "fingerprint-1",
|
||||
},
|
||||
},
|
||||
});
|
||||
const contextEngine = createContextEngine({
|
||||
assemble: vi.fn(async ({ prompt }) => ({
|
||||
messages: [userMessage(prompt ?? "", 11)],
|
||||
estimatedTokens: 42,
|
||||
systemPromptAddition: "context-engine system",
|
||||
contextProjection: {
|
||||
mode: "thread_bootstrap" as const,
|
||||
epoch: "epoch-1",
|
||||
fingerprint: "fingerprint-1",
|
||||
},
|
||||
})),
|
||||
});
|
||||
const harness = createStartedThreadHarness(async (method) =>
|
||||
method === "thread/resume" ? threadStartResult("thread-bootstrapped") : undefined,
|
||||
);
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.contextEngine = contextEngine;
|
||||
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
await harness.waitForMethod("turn/start");
|
||||
await harness.notify({
|
||||
method: "item/started",
|
||||
params: {
|
||||
threadId: "thread-bootstrapped",
|
||||
turnId: "turn-1",
|
||||
item: { id: "compact-1", type: "contextCompaction" },
|
||||
},
|
||||
});
|
||||
const startedBinding = await readCodexAppServerBinding(sessionFile);
|
||||
expect(startedBinding).not.toHaveProperty("nativeContextUsage");
|
||||
expect(startedBinding?.contextEngine).not.toHaveProperty("projection");
|
||||
await harness.notify({
|
||||
method: "thread/tokenUsage/updated",
|
||||
params: {
|
||||
threadId: "thread-bootstrapped",
|
||||
turnId: "turn-1",
|
||||
tokenUsage: {
|
||||
total: { totalTokens: 900_000 },
|
||||
last: { totalTokens: 12_000 },
|
||||
modelContextWindow: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
await harness.notify({
|
||||
method: "item/completed",
|
||||
params: {
|
||||
threadId: "thread-bootstrapped",
|
||||
turnId: "turn-1",
|
||||
item: { id: "compact-1", type: "contextCompaction" },
|
||||
},
|
||||
});
|
||||
const compactedBinding = await readCodexAppServerBinding(sessionFile);
|
||||
expect(compactedBinding).not.toHaveProperty("nativeContextUsage");
|
||||
await harness.completeTurn("completed", "thread-bootstrapped");
|
||||
await run;
|
||||
|
||||
const binding = await readCodexAppServerBinding(sessionFile);
|
||||
expect(binding?.contextEngine?.projection).toBeUndefined();
|
||||
expect(binding?.nativeContextUsage).toEqual({
|
||||
currentTokens: 12_000,
|
||||
});
|
||||
expect(binding?.modelContextWindow).toBe(258_400);
|
||||
});
|
||||
|
||||
it("starts a fresh thread instead of resuming a token-pressured thread-bootstrap binding", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const agentDir = path.join(tempDir, "agent");
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-bootstrapped",
|
||||
cwd: workspaceDir,
|
||||
dynamicToolsFingerprint: "[]",
|
||||
nativeContextUsage: { currentTokens: 241_198 },
|
||||
modelContextWindow: 258_400,
|
||||
contextEngine: {
|
||||
schemaVersion: 1,
|
||||
engineId: "lossless-claw",
|
||||
@@ -916,6 +653,31 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
await fs.writeFile(
|
||||
path.join(path.dirname(sessionFile), "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:session-1": {
|
||||
sessionFile,
|
||||
totalTokens: 12_000,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
||||
await fs.mkdir(rolloutDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(rolloutDir, "rollout-thread-bootstrapped.jsonl"),
|
||||
`${JSON.stringify({
|
||||
payload: {
|
||||
type: "token_count",
|
||||
info: {
|
||||
last_token_usage: {
|
||||
total_tokens: 241_198,
|
||||
},
|
||||
model_context_window: 258_400,
|
||||
},
|
||||
},
|
||||
})}\n`,
|
||||
);
|
||||
const contextEngine = createContextEngine({
|
||||
assemble: vi.fn(async ({ prompt }) => ({
|
||||
messages: [assistantMessage("reprojected context", 10), userMessage(prompt ?? "", 11)],
|
||||
@@ -934,14 +696,13 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
return undefined;
|
||||
});
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.agentDir = agentDir;
|
||||
params.contextEngine = contextEngine;
|
||||
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
await harness.waitForMethod("turn/start");
|
||||
|
||||
expect(harness.requests.map((request) => request.method)).toEqual([
|
||||
"thread/resume",
|
||||
"thread/unsubscribe",
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
]);
|
||||
@@ -956,6 +717,7 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
it("does not inject mirrored history when a stale thread-bootstrap binding has no active context engine", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const agentDir = path.join(tempDir, "agent");
|
||||
const sessionManager = SessionManager.open(sessionFile);
|
||||
sessionManager.appendMessage(
|
||||
userMessage("previous stale-bootstrap request", Date.now()) as never,
|
||||
@@ -967,7 +729,6 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
threadId: "thread-stale-bootstrap",
|
||||
cwd: workspaceDir,
|
||||
dynamicToolsFingerprint: "[]",
|
||||
nativeContextUsage: { currentTokens: 300_000 },
|
||||
contextEngine: {
|
||||
schemaVersion: 1,
|
||||
engineId: "lossless-claw",
|
||||
@@ -980,6 +741,30 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
await fs.writeFile(
|
||||
path.join(path.dirname(sessionFile), "sessions.json"),
|
||||
JSON.stringify({
|
||||
"agent:main:session-1": {
|
||||
sessionFile,
|
||||
totalTokens: 12_000,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const rolloutDir = path.join(agentDir, "codex-home", "sessions");
|
||||
await fs.mkdir(rolloutDir, { recursive: true });
|
||||
await fs.writeFile(
|
||||
path.join(rolloutDir, "rollout-thread-stale-bootstrap.jsonl"),
|
||||
`${JSON.stringify({
|
||||
payload: {
|
||||
type: "token_count",
|
||||
info: {
|
||||
last_token_usage: {
|
||||
total_tokens: 300_000,
|
||||
},
|
||||
},
|
||||
},
|
||||
})}\n`,
|
||||
);
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method === "thread/resume") {
|
||||
return threadStartResult("thread-stale-bootstrap");
|
||||
@@ -990,6 +775,17 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
return undefined;
|
||||
});
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.agentDir = agentDir;
|
||||
params.config = {
|
||||
agents: {
|
||||
defaults: {
|
||||
compaction: {
|
||||
truncateAfterCompaction: true,
|
||||
maxActiveTranscriptBytes: "1mb",
|
||||
},
|
||||
},
|
||||
},
|
||||
} as EmbeddedRunAttemptParams["config"];
|
||||
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
await harness.waitForMethod("turn/start");
|
||||
@@ -1137,7 +933,7 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
sessionId: "session-1",
|
||||
threadId: "thread-new",
|
||||
engineId: "lossless-claw",
|
||||
projectionPending: true,
|
||||
epoch: "epoch-new",
|
||||
action: "rotated",
|
||||
}),
|
||||
);
|
||||
@@ -1149,8 +945,6 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-old",
|
||||
cwd: workspaceDir,
|
||||
model: "gpt-5.4-codex",
|
||||
modelProvider: "openai",
|
||||
dynamicToolsFingerprint: "[]",
|
||||
contextEngine: {
|
||||
schemaVersion: 1,
|
||||
@@ -1317,12 +1111,6 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
},
|
||||
});
|
||||
await run;
|
||||
await expect(readCodexAppServerBinding(sessionFile)).resolves.toMatchObject({
|
||||
threadId: "thread-old",
|
||||
contextEngine: {
|
||||
projection: { mode: "thread_bootstrap", epoch: "epoch-1" },
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
restoreSandboxBackend();
|
||||
}
|
||||
@@ -1404,11 +1192,7 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-old",
|
||||
cwd: workspaceDir,
|
||||
model: "gpt-5.4-codex",
|
||||
modelProvider: "openai",
|
||||
dynamicToolsFingerprint: "[]",
|
||||
nativeContextUsage: { currentTokens: 120_000 },
|
||||
modelContextWindow: 258_400,
|
||||
contextEngine: {
|
||||
schemaVersion: 1,
|
||||
engineId: "lossless-claw",
|
||||
@@ -1455,13 +1239,7 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
throw new Error("Codex ran out of room in the model's context window");
|
||||
}
|
||||
if (method === "thread/start") {
|
||||
const response = threadStartResult("thread-fresh");
|
||||
return {
|
||||
...response,
|
||||
thread: { ...response.thread, modelProvider: "lmstudio" },
|
||||
model: "local-model",
|
||||
modelProvider: "lmstudio",
|
||||
};
|
||||
return threadStartResult("thread-fresh");
|
||||
}
|
||||
if (method === "turn/start" && request.threadId === "thread-fresh") {
|
||||
return turnStartResult("turn-fresh");
|
||||
@@ -1472,30 +1250,15 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
params.contextEngine = contextEngine;
|
||||
params.contextTokenBudget = 400_000;
|
||||
|
||||
const run = runCodexAppServerAttempt(params, {
|
||||
pluginConfig: { appServer: { mode: "guardian" } },
|
||||
});
|
||||
const run = runCodexAppServerAttempt(params);
|
||||
await vi.waitFor(() =>
|
||||
expect(harness.requests.map((request) => request.method)).toEqual([
|
||||
"thread/resume",
|
||||
"turn/start",
|
||||
"thread/unsubscribe",
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
]),
|
||||
);
|
||||
await harness.notify({
|
||||
method: "thread/tokenUsage/updated",
|
||||
params: {
|
||||
threadId: "thread-fresh",
|
||||
turnId: "turn-fresh",
|
||||
tokenUsage: {
|
||||
total: { totalTokens: 900_000 },
|
||||
last: { totalTokens: 12_000 },
|
||||
modelContextWindow: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
await harness.notify({
|
||||
method: "turn/completed",
|
||||
params: {
|
||||
@@ -1511,36 +1274,15 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
const result = await run;
|
||||
|
||||
expect(result.assistantTexts).toContain("fresh answer");
|
||||
const turnStarts = harness.requests.filter((request) => request.method === "turn/start");
|
||||
expect(turnStarts[0]?.params).toMatchObject({ approvalsReviewer: "auto_review" });
|
||||
expect(turnStarts[1]?.params).toMatchObject({
|
||||
model: "local-model",
|
||||
approvalsReviewer: "user",
|
||||
approvalPolicy: "on-request",
|
||||
});
|
||||
expect(compact).not.toHaveBeenCalled();
|
||||
expect(assemble).toHaveBeenCalledTimes(1);
|
||||
const retryInputText = getRequestInputTextAt(harness, -1);
|
||||
expect(retryInputText).toContain("context epoch-before");
|
||||
expect(retryInputText).toContain("hello");
|
||||
expect(retryInputText).toBe("hello");
|
||||
expect(retryInputText).not.toContain("successor compacted context");
|
||||
const savedBinding = await readCodexAppServerBinding(sessionFile);
|
||||
expect(savedBinding?.threadId).toBe("thread-fresh");
|
||||
expect(savedBinding?.contextEngine?.engineId).toBe("lossless-claw");
|
||||
expect(savedBinding?.contextEngine?.projection).toEqual({
|
||||
schemaVersion: 1,
|
||||
mode: "thread_bootstrap",
|
||||
epoch: "epoch-before",
|
||||
});
|
||||
expect(savedBinding?.nativeContextUsage).toEqual({
|
||||
currentTokens: 12_000,
|
||||
});
|
||||
expect(savedBinding?.modelContextWindow).toBeUndefined();
|
||||
expect(
|
||||
harness.requests
|
||||
.filter((request) => request.method === "thread/unsubscribe")
|
||||
.map((request) => request.params),
|
||||
).toEqual([{ threadId: "thread-old" }, { threadId: "thread-fresh" }]);
|
||||
expect(savedBinding?.contextEngine?.projection).toBeUndefined();
|
||||
});
|
||||
|
||||
it("preserves a newer context-engine binding when a stale resumed thread overflows", async () => {
|
||||
@@ -1831,7 +1573,6 @@ describe("runCodexAppServerAttempt context-engine lifecycle", () => {
|
||||
expect(harness.requests.map((request) => request.method)).toEqual([
|
||||
"thread/resume",
|
||||
"turn/start",
|
||||
"thread/unsubscribe",
|
||||
"thread/start",
|
||||
"turn/start",
|
||||
]),
|
||||
|
||||
@@ -120,8 +120,6 @@ describe("runCodexAppServerAttempt hooks and model diagnostics", () => {
|
||||
phase?: string;
|
||||
startedAt?: number;
|
||||
text?: string;
|
||||
threadId?: string;
|
||||
turnId?: string;
|
||||
};
|
||||
stream: string;
|
||||
}>;
|
||||
@@ -129,11 +127,6 @@ describe("runCodexAppServerAttempt hooks and model diagnostics", () => {
|
||||
(event) => event.stream === "lifecycle" && event.data.phase === "start",
|
||||
);
|
||||
expect(typeof lifecycleStart?.data.startedAt).toBe("number");
|
||||
const turnAccepted = agentEvents.find(
|
||||
(event) =>
|
||||
event.stream === "codex_app_server.lifecycle" && event.data.phase === "turn_accepted",
|
||||
);
|
||||
expect(turnAccepted?.data).toMatchObject({ threadId: "thread-1", turnId: "turn-1" });
|
||||
const assistantEvent = agentEvents.find((event) => event.stream === "assistant");
|
||||
expect(assistantEvent?.data).toEqual({ text: "hello back" });
|
||||
const lifecycleEnd = agentEvents.find(
|
||||
@@ -145,16 +138,10 @@ describe("runCodexAppServerAttempt hooks and model diagnostics", () => {
|
||||
(event) => event.stream === "lifecycle" && event.data.phase === "start",
|
||||
);
|
||||
const assistantIndex = agentEvents.findIndex((event) => event.stream === "assistant");
|
||||
const acceptedIndex = agentEvents.findIndex(
|
||||
(event) =>
|
||||
event.stream === "codex_app_server.lifecycle" && event.data.phase === "turn_accepted",
|
||||
);
|
||||
const endIndex = agentEvents.findIndex(
|
||||
(event) => event.stream === "lifecycle" && event.data.phase === "end",
|
||||
);
|
||||
expect(startIndex).toBeGreaterThanOrEqual(0);
|
||||
expect(acceptedIndex).toBeGreaterThanOrEqual(0);
|
||||
expect(startIndex).toBeGreaterThan(acceptedIndex);
|
||||
expect(assistantIndex).toBeGreaterThan(startIndex);
|
||||
expect(endIndex).toBeGreaterThan(assistantIndex);
|
||||
const globalAssistantEvent = globalAgentEvents.find((event) => event.stream === "assistant");
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
} from "openclaw/plugin-sdk/agent-harness-runtime";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import * as approvalBridge from "./approval-bridge.js";
|
||||
import { CodexAppServerRpcError } from "./client.js";
|
||||
import {
|
||||
createParams,
|
||||
createResumeHarness,
|
||||
@@ -17,21 +16,9 @@ import {
|
||||
runCodexAppServerAttempt,
|
||||
setupRunAttemptTestHooks,
|
||||
tempDir,
|
||||
threadStartResult,
|
||||
} from "./run-attempt-test-harness.js";
|
||||
import { testing } from "./run-attempt.js";
|
||||
import {
|
||||
readCodexAppServerBinding,
|
||||
registerCodexTestSessionIdentity,
|
||||
writeCodexAppServerBinding as writeStoredCodexAppServerBinding,
|
||||
} from "./session-binding.test-helpers.js";
|
||||
|
||||
async function writeCodexAppServerBinding(
|
||||
...args: Parameters<typeof writeStoredCodexAppServerBinding>
|
||||
): Promise<void> {
|
||||
registerCodexTestSessionIdentity(args[0], "session-1", "agent:main:session-1");
|
||||
await writeStoredCodexAppServerBinding(...args);
|
||||
}
|
||||
import { readCodexAppServerBinding, writeCodexAppServerBinding } from "./session-binding.js";
|
||||
|
||||
setupRunAttemptTestHooks();
|
||||
|
||||
@@ -265,7 +252,7 @@ describe("runCodexAppServerAttempt native hook relay", () => {
|
||||
expect(nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayId)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("installs policy-stable native hook relay events before thread policy is known", async () => {
|
||||
it("lets Codex app-server approval modes own native permission requests by default", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const harness = createStartedThreadHarness();
|
||||
@@ -286,16 +273,11 @@ describe("runCodexAppServerAttempt native hook relay", () => {
|
||||
expect(Array.isArray(startConfig?.["hooks.PreToolUse"])).toBe(true);
|
||||
expect(startConfig?.["hooks.PostToolUse"]).toEqual([]);
|
||||
expect(startConfig?.["hooks.Stop"]).toEqual([]);
|
||||
const permissionRequestHooks = startConfig?.["hooks.PermissionRequest"] as
|
||||
| Array<{ hooks?: Array<{ command?: string }> }>
|
||||
| undefined;
|
||||
expect(permissionRequestHooks?.[0]?.hooks?.[0]?.command).toContain(
|
||||
"--event permission_request",
|
||||
);
|
||||
expect(startConfig).not.toHaveProperty("hooks.PermissionRequest");
|
||||
const relayId = extractRelayIdFromThreadRequest(startRequest?.params);
|
||||
expect(
|
||||
nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayId)?.allowedEvents,
|
||||
).toEqual(["pre_tool_use", "post_tool_use", "permission_request", "before_agent_finalize"]);
|
||||
).toEqual(["pre_tool_use", "post_tool_use", "before_agent_finalize"]);
|
||||
|
||||
await harness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
|
||||
await run;
|
||||
@@ -303,68 +285,6 @@ describe("runCodexAppServerAttempt native hook relay", () => {
|
||||
expect(nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(relayId)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("defers permission hooks after Codex returns a provider with guarded policy", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method !== "thread/start") {
|
||||
return undefined;
|
||||
}
|
||||
const response = threadStartResult();
|
||||
return {
|
||||
...response,
|
||||
thread: { ...response.thread, modelProvider: "lmstudio" },
|
||||
model: "local-model",
|
||||
modelProvider: "lmstudio",
|
||||
};
|
||||
});
|
||||
const approvalRequester = vi.fn(async () => "allow" as const);
|
||||
nativeHookRelayTesting.setNativeHookRelayPermissionApprovalRequesterForTests(approvalRequester);
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.modelId = "openai/gpt-5.4-codex";
|
||||
|
||||
const run = runCodexAppServerAttempt(params, {
|
||||
pluginConfig: {
|
||||
appServer: {
|
||||
mode: "yolo",
|
||||
approvalsReviewer: "auto_review",
|
||||
},
|
||||
},
|
||||
nativeHookRelay: { enabled: true },
|
||||
});
|
||||
await harness.waitForMethod("turn/start");
|
||||
|
||||
const startRequest = harness.requests.find((request) => request.method === "thread/start");
|
||||
const startParams = startRequest?.params as
|
||||
| { approvalPolicy?: unknown; config?: Record<string, unknown> }
|
||||
| undefined;
|
||||
expect(startParams?.approvalPolicy).toBe("never");
|
||||
expect(Array.isArray(startParams?.config?.["hooks.PermissionRequest"])).toBe(true);
|
||||
const turnStartRequest = harness.requests.find((request) => request.method === "turn/start");
|
||||
expect(
|
||||
(turnStartRequest?.params as { approvalPolicy?: unknown } | undefined)?.approvalPolicy,
|
||||
).toBe("on-request");
|
||||
const relayId = extractRelayIdFromThreadRequest(startRequest?.params);
|
||||
await expect(
|
||||
invokeNativeHookRelay({
|
||||
provider: "codex",
|
||||
relayId,
|
||||
event: "permission_request",
|
||||
rawPayload: {
|
||||
hook_event_name: "PermissionRequest",
|
||||
permission_mode: "default",
|
||||
tool_name: "Bash",
|
||||
tool_input: { command: "git push" },
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual({ stdout: "", stderr: "", exitCode: 0 });
|
||||
expect(approvalRequester).not.toHaveBeenCalled();
|
||||
|
||||
await harness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
|
||||
await run;
|
||||
testing.flushPendingCodexNativeHookRelayUnregistersForTests();
|
||||
});
|
||||
|
||||
it("preserves explicit native permission request relay events in app-server approval modes", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
@@ -512,7 +432,7 @@ describe("runCodexAppServerAttempt native hook relay", () => {
|
||||
"run-2",
|
||||
);
|
||||
|
||||
await secondHarness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
|
||||
await secondHarness.completeTurn({ threadId: "thread-existing", turnId: "turn-1" });
|
||||
await secondRun;
|
||||
expect(nativeHookRelayTesting.getNativeHookRelayRegistrationForTests(firstRelayId)?.runId).toBe(
|
||||
"run-2",
|
||||
@@ -564,7 +484,7 @@ describe("runCodexAppServerAttempt native hook relay", () => {
|
||||
expect(extractRelayIdFromThreadRequest(resumeRequest?.params)).toBe(firstRelayId);
|
||||
expect(extractGenerationFromThreadRequest(resumeRequest?.params)).toBe(firstGeneration);
|
||||
|
||||
await secondHarness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
|
||||
await secondHarness.completeTurn({ threadId: "thread-existing", turnId: "turn-1" });
|
||||
await secondRun;
|
||||
testing.flushPendingCodexNativeHookRelayUnregistersForTests();
|
||||
});
|
||||
@@ -693,7 +613,7 @@ describe("runCodexAppServerAttempt native hook relay", () => {
|
||||
});
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method === "thread/resume") {
|
||||
throw new CodexAppServerRpcError({ code: -32000, message: "resume failed" }, method);
|
||||
throw new Error("resume failed");
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -36,10 +36,10 @@ import {
|
||||
} from "./run-attempt-test-harness.js";
|
||||
import { testing } from "./run-attempt.js";
|
||||
import {
|
||||
createCodexTestBindingStore,
|
||||
readCodexAppServerBinding,
|
||||
resolveCodexAppServerBindingPath,
|
||||
writeCodexAppServerBinding,
|
||||
} from "./session-binding.test-helpers.js";
|
||||
} from "./session-binding.js";
|
||||
|
||||
setupRunAttemptTestHooks();
|
||||
|
||||
@@ -51,7 +51,7 @@ describe("createCodexAttemptTurnWatchController", () => {
|
||||
const onTimeout = vi.fn();
|
||||
const onAbort = vi.fn();
|
||||
const controller = createCodexAttemptTurnWatchController({
|
||||
getThreadId: () => "thread-1",
|
||||
threadId: "thread-1",
|
||||
signal: new AbortController().signal,
|
||||
getTurnId: () => "turn-1",
|
||||
isCompleted: () => false,
|
||||
@@ -80,7 +80,7 @@ describe("createCodexAttemptTurnWatchController", () => {
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 20);
|
||||
});
|
||||
controller.noteNotificationReceived("item/fileChange/patchUpdated", {
|
||||
controller.noteNotificationReceived("response.output_text.delta", {
|
||||
attemptProgress: true,
|
||||
attemptTimeoutMs: 40,
|
||||
});
|
||||
@@ -93,7 +93,7 @@ describe("createCodexAttemptTurnWatchController", () => {
|
||||
expect.objectContaining({
|
||||
kind: "progress",
|
||||
timeoutMs: 40,
|
||||
lastActivityReason: "notification:item/fileChange/patchUpdated",
|
||||
lastActivityReason: "notification:response.output_text.delta",
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
@@ -138,6 +138,8 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
path.join(tempDir, "workspace"),
|
||||
);
|
||||
params.timeoutMs = 200;
|
||||
const bindingPath = resolveCodexAppServerBindingPath(params.sessionFile);
|
||||
|
||||
const run = runCodexAppServerAttempt(params, {
|
||||
pluginConfig: { appServer: { turnCompletionIdleTimeoutMs: 5 } },
|
||||
postToolRawAssistantCompletionIdleTimeoutMs: 5,
|
||||
@@ -183,7 +185,7 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
),
|
||||
{ interval: 1 },
|
||||
);
|
||||
await expect(readCodexAppServerBinding(params.sessionFile)).resolves.toBeUndefined();
|
||||
await expect(fs.stat(bindingPath)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
expect(queueActiveRunMessageForTest("session-1", "after timeout")).toBe(false);
|
||||
});
|
||||
|
||||
@@ -401,7 +403,8 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
expect(result.promptTimeoutOutcome).toBeUndefined();
|
||||
});
|
||||
|
||||
it("unsubscribes the app-server client when the active turn goes idle past the attempt timeout", async () => {
|
||||
it("unsubscribes and closes the app-server client when the active turn goes idle past the attempt timeout", async () => {
|
||||
const close = vi.fn();
|
||||
const request = vi.fn(async (method: string) => {
|
||||
if (method === "thread/start") {
|
||||
return threadStartResult("thread-1");
|
||||
@@ -418,6 +421,7 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
async () =>
|
||||
({
|
||||
request,
|
||||
close,
|
||||
addNotificationHandler: () => () => undefined,
|
||||
addRequestHandler: () => () => undefined,
|
||||
}) as never,
|
||||
@@ -450,6 +454,7 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
},
|
||||
{ timeoutMs: 5_000 },
|
||||
);
|
||||
expect(close).toHaveBeenCalledTimes(1);
|
||||
expect(queueActiveRunMessageForTest("session-1", "after timeout")).toBe(false);
|
||||
});
|
||||
|
||||
@@ -1850,6 +1855,118 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
expect(completionWarnData?.timeoutMs).toBe(100);
|
||||
});
|
||||
|
||||
it("counts native response deltas as post-tool raw assistant activity", async () => {
|
||||
let notify: (notification: CodexServerNotification) => Promise<void> = async () => undefined;
|
||||
let handleRequest:
|
||||
| ((request: { id: string; method: string; params?: unknown }) => Promise<unknown>)
|
||||
| undefined;
|
||||
const request = vi.fn(async (method: string) => {
|
||||
if (method === "thread/start") {
|
||||
return threadStartResult("thread-1");
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
return turnStartResult("turn-1", "inProgress");
|
||||
}
|
||||
return {};
|
||||
});
|
||||
setCodexAppServerClientFactoryForTest(
|
||||
async () =>
|
||||
({
|
||||
request,
|
||||
addNotificationHandler: (handler: typeof notify) => {
|
||||
notify = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
addRequestHandler: (
|
||||
handler: (request: {
|
||||
id: string;
|
||||
method: string;
|
||||
params?: unknown;
|
||||
}) => Promise<unknown>,
|
||||
) => {
|
||||
handleRequest = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
}) as never,
|
||||
);
|
||||
const params = createParams(
|
||||
path.join(tempDir, "session.jsonl"),
|
||||
path.join(tempDir, "workspace"),
|
||||
);
|
||||
params.timeoutMs = 60_000;
|
||||
|
||||
let settled = false;
|
||||
const run = runCodexAppServerAttempt(params, {
|
||||
turnCompletionIdleTimeoutMs: 500,
|
||||
turnAssistantCompletionIdleTimeoutMs: 5,
|
||||
postToolRawAssistantCompletionIdleTimeoutMs: 50,
|
||||
turnTerminalIdleTimeoutMs: 500,
|
||||
}).finally(() => {
|
||||
settled = true;
|
||||
});
|
||||
await vi.waitFor(() => expect(handleRequest).toBeTypeOf("function"), fastWait);
|
||||
|
||||
const toolResult = (await handleRequest?.({
|
||||
id: "request-tool-1",
|
||||
method: "item/tool/call",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
callId: "call-1",
|
||||
namespace: null,
|
||||
tool: "message",
|
||||
arguments: { action: "send", text: "already sent" },
|
||||
},
|
||||
})) as { success?: boolean };
|
||||
expect(toolResult.success).toBe(false);
|
||||
await notify({
|
||||
method: "rawResponseItem/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
item: {
|
||||
type: "message",
|
||||
id: "raw-status-1",
|
||||
role: "assistant",
|
||||
content: [{ type: "output_text", text: "I'm writing a large patch now." }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 30);
|
||||
});
|
||||
// This covers the future-compatible path for raw response deltas if Codex
|
||||
// app-server exposes them directly; current Codex primarily emits
|
||||
// rawResponseItem/completed for the raw-event surface.
|
||||
await notify({
|
||||
method: "response.custom_tool_call_input.delta",
|
||||
params: {
|
||||
item_id: "ctc-large-edit-1",
|
||||
output_index: 0,
|
||||
delta: '{"cmd":"apply_patch","patch":"large chunk"}',
|
||||
},
|
||||
});
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 30);
|
||||
});
|
||||
expect(settled).toBe(false);
|
||||
|
||||
await notify({
|
||||
method: "turn/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
turn: { id: "turn-1", status: "completed" },
|
||||
},
|
||||
});
|
||||
|
||||
const result = await run;
|
||||
expect(result.aborted).toBe(false);
|
||||
expect(result.timedOut).toBe(false);
|
||||
expect(result.promptError).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps the post-tool guard armed for patch update snapshots", async () => {
|
||||
let notify: (notification: CodexServerNotification) => Promise<void> = async () => undefined;
|
||||
let handleRequest:
|
||||
@@ -1960,6 +2077,212 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
expect(completionWarnData?.lastNotificationMethod).toBe("item/fileChange/patchUpdated");
|
||||
});
|
||||
|
||||
it("keeps the post-tool guard armed for scoped native response deltas", async () => {
|
||||
let notify: (notification: CodexServerNotification) => Promise<void> = async () => undefined;
|
||||
let handleRequest:
|
||||
| ((request: { id: string; method: string; params?: unknown }) => Promise<unknown>)
|
||||
| undefined;
|
||||
const request = vi.fn(async (method: string) => {
|
||||
if (method === "thread/start") {
|
||||
return threadStartResult("thread-1");
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
return turnStartResult("turn-1", "inProgress");
|
||||
}
|
||||
return {};
|
||||
});
|
||||
setCodexAppServerClientFactoryForTest(
|
||||
async () =>
|
||||
({
|
||||
request,
|
||||
addNotificationHandler: (handler: typeof notify) => {
|
||||
notify = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
addRequestHandler: (
|
||||
handler: (request: {
|
||||
id: string;
|
||||
method: string;
|
||||
params?: unknown;
|
||||
}) => Promise<unknown>,
|
||||
) => {
|
||||
handleRequest = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
}) as never,
|
||||
);
|
||||
const params = createParams(
|
||||
path.join(tempDir, "session-scoped-delta-timeout.jsonl"),
|
||||
path.join(tempDir, "workspace-scoped-delta-timeout"),
|
||||
);
|
||||
params.timeoutMs = 2_000;
|
||||
|
||||
const run = runCodexAppServerAttempt(params, {
|
||||
turnCompletionIdleTimeoutMs: 500,
|
||||
turnAssistantCompletionIdleTimeoutMs: 5,
|
||||
postToolRawAssistantCompletionIdleTimeoutMs: 50,
|
||||
turnTerminalIdleTimeoutMs: 500,
|
||||
});
|
||||
await vi.waitFor(() => expect(handleRequest).toBeTypeOf("function"), fastWait);
|
||||
|
||||
await handleRequest?.({
|
||||
id: "request-tool-1",
|
||||
method: "item/tool/call",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
callId: "call-1",
|
||||
namespace: null,
|
||||
tool: "message",
|
||||
arguments: { action: "send", text: "already sent" },
|
||||
},
|
||||
});
|
||||
await notify({
|
||||
method: "rawResponseItem/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
item: {
|
||||
type: "message",
|
||||
id: "raw-status-1",
|
||||
role: "assistant",
|
||||
content: [{ type: "output_text", text: "I'm writing a large patch now." }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 30);
|
||||
});
|
||||
await notify({
|
||||
method: "response.custom_tool_call_input.delta",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
item_id: "ctc-large-edit-1",
|
||||
output_index: 0,
|
||||
delta: '{"cmd":"apply_patch","patch":"large chunk"}',
|
||||
},
|
||||
});
|
||||
|
||||
const result = await run;
|
||||
expect(result.timedOut).toBe(true);
|
||||
expect(result.promptError).toBe(
|
||||
"codex app-server turn idle timed out waiting for turn/completed",
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores unscoped native response deltas while another turn leases the client", async () => {
|
||||
let notify: (notification: CodexServerNotification) => Promise<void> = async () => undefined;
|
||||
let handleRequest:
|
||||
| ((request: { id: string; method: string; params?: unknown }) => Promise<unknown>)
|
||||
| undefined;
|
||||
const warn = vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
|
||||
const request = vi.fn(async (method: string) => {
|
||||
if (method === "thread/start") {
|
||||
return threadStartResult("thread-1");
|
||||
}
|
||||
if (method === "turn/start") {
|
||||
return turnStartResult("turn-1", "inProgress");
|
||||
}
|
||||
return {};
|
||||
});
|
||||
setCodexAppServerClientFactoryForTest(
|
||||
async () =>
|
||||
({
|
||||
request,
|
||||
getActiveSharedLeaseCountForUnscopedNotifications: () => 2,
|
||||
addNotificationHandler: (handler: typeof notify) => {
|
||||
notify = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
addRequestHandler: (
|
||||
handler: (request: {
|
||||
id: string;
|
||||
method: string;
|
||||
params?: unknown;
|
||||
}) => Promise<unknown>,
|
||||
) => {
|
||||
handleRequest = handler;
|
||||
return () => undefined;
|
||||
},
|
||||
}) as never,
|
||||
);
|
||||
const params = createParams(
|
||||
path.join(tempDir, "session.jsonl"),
|
||||
path.join(tempDir, "workspace"),
|
||||
);
|
||||
params.timeoutMs = 60_000;
|
||||
|
||||
let settled = false;
|
||||
const run = runCodexAppServerAttempt(params, {
|
||||
turnCompletionIdleTimeoutMs: 500,
|
||||
turnAssistantCompletionIdleTimeoutMs: 5,
|
||||
postToolRawAssistantCompletionIdleTimeoutMs: 80,
|
||||
turnTerminalIdleTimeoutMs: 500,
|
||||
}).finally(() => {
|
||||
settled = true;
|
||||
});
|
||||
await vi.waitFor(() => expect(handleRequest).toBeTypeOf("function"), fastWait);
|
||||
|
||||
await handleRequest?.({
|
||||
id: "request-tool-1",
|
||||
method: "item/tool/call",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
callId: "call-1",
|
||||
namespace: null,
|
||||
tool: "message",
|
||||
arguments: { action: "send", text: "already sent" },
|
||||
},
|
||||
});
|
||||
await notify({
|
||||
method: "rawResponseItem/completed",
|
||||
params: {
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
item: {
|
||||
type: "message",
|
||||
id: "raw-status-1",
|
||||
role: "assistant",
|
||||
content: [{ type: "output_text", text: "I'm writing a large patch now." }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 40);
|
||||
});
|
||||
await notify({
|
||||
method: "response.custom_tool_call_input.delta",
|
||||
params: {
|
||||
item_id: "foreign-large-edit-1",
|
||||
output_index: 0,
|
||||
delta: '{"cmd":"apply_patch","patch":"other turn"}',
|
||||
},
|
||||
});
|
||||
await vi.waitFor(() => expect(settled).toBe(true), fastWait);
|
||||
|
||||
const result = await run;
|
||||
expect(result.aborted).toBe(true);
|
||||
expect(result.timedOut).toBe(true);
|
||||
expect(result.promptError).toBe(
|
||||
"codex app-server turn idle timed out waiting for turn/completed",
|
||||
);
|
||||
const completionWarnCall = warn.mock.calls.find(
|
||||
([message]) => message === "codex app-server turn idle timed out waiting for completion",
|
||||
);
|
||||
const completionWarnData = completionWarnCall?.[1] as
|
||||
| {
|
||||
lastActivityReason?: string;
|
||||
lastNotificationMethod?: string;
|
||||
}
|
||||
| undefined;
|
||||
expect(completionWarnData?.lastActivityReason).toBe("notification:rawResponseItem/completed");
|
||||
expect(completionWarnData?.lastNotificationMethod).toBe("rawResponseItem/completed");
|
||||
});
|
||||
|
||||
it("times out post-native-tool raw assistant progress after the post-tool timeout", async () => {
|
||||
let notify: (notification: CodexServerNotification) => Promise<void> = async () => undefined;
|
||||
const request = vi.fn(async (method: string) => {
|
||||
@@ -2657,47 +2980,6 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("retires a timed-out client even when binding cleanup fails", async () => {
|
||||
vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
|
||||
const baseBindingStore = createCodexTestBindingStore();
|
||||
const bindingStore = {
|
||||
...baseBindingStore,
|
||||
mutate: vi.fn(async (...args: Parameters<typeof baseBindingStore.mutate>) => {
|
||||
if (args[1].kind === "clear") {
|
||||
throw new Error("binding store unavailable");
|
||||
}
|
||||
return await baseBindingStore.mutate(...args);
|
||||
}),
|
||||
};
|
||||
const harness = createStartedThreadHarness();
|
||||
const release = vi.fn();
|
||||
const abandon = vi.fn(async () => undefined);
|
||||
const params = createParams(
|
||||
path.join(tempDir, "session-retirement.jsonl"),
|
||||
path.join(tempDir, "workspace-retirement"),
|
||||
);
|
||||
params.timeoutMs = 200;
|
||||
|
||||
const result = await runCodexAppServerAttempt(params, {
|
||||
bindingStore,
|
||||
turnCompletionIdleTimeoutMs: 15,
|
||||
clientLeaseFactory: async () => ({ client: harness.client, release, abandon }),
|
||||
});
|
||||
|
||||
expect(result.timedOut).toBe(true);
|
||||
expect(bindingStore.mutate).toHaveBeenCalledWith(
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ kind: "clear", threadId: "thread-1" }),
|
||||
);
|
||||
expect(harness.request).toHaveBeenCalledWith(
|
||||
"thread/unsubscribe",
|
||||
{ threadId: "thread-1" },
|
||||
{ timeoutMs: 5_000 },
|
||||
);
|
||||
expect(abandon).toHaveBeenCalledOnce();
|
||||
expect(release).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clears the thread binding after a completion-idle timeout so the next turn starts fresh", async () => {
|
||||
// Regression for openclaw#89974. The "user interrupted the previous turn on
|
||||
// purpose" wording is Codex's generic <turn_aborted> rollout marker, written
|
||||
@@ -2709,7 +2991,6 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
|
||||
const sessionFile = path.join(tempDir, "session-89974.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace-89974");
|
||||
const firstParams = createParams(sessionFile, workspaceDir);
|
||||
await writeCodexAppServerBinding(sessionFile, {
|
||||
threadId: "thread-existing",
|
||||
cwd: workspaceDir,
|
||||
@@ -2720,6 +3001,7 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
|
||||
// Turn 1: resume an existing thread, then never deliver turn/completed.
|
||||
const firstHarness = createResumeHarness();
|
||||
const firstParams = createParams(sessionFile, workspaceDir);
|
||||
firstParams.timeoutMs = 200;
|
||||
const firstRun = runCodexAppServerAttempt(firstParams, { turnCompletionIdleTimeoutMs: 15 });
|
||||
await firstHarness.waitForMethod("turn/start");
|
||||
@@ -2761,9 +3043,9 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
const processing = harness.notify(notification);
|
||||
await Promise.resolve();
|
||||
|
||||
expect(readRecentCodexRateLimits(harness.client)).toBeUndefined();
|
||||
expect(readRecentCodexRateLimits()).toBeUndefined();
|
||||
await processing;
|
||||
expect(readRecentCodexRateLimits(harness.client)).toEqual(notification.params);
|
||||
expect(readRecentCodexRateLimits()).toEqual(notification.params);
|
||||
|
||||
await harness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
|
||||
await expect(run).resolves.toMatchObject({ aborted: false, timedOut: false });
|
||||
@@ -3804,8 +4086,9 @@ describe("runCodexAppServerAttempt turn watches", () => {
|
||||
);
|
||||
|
||||
await harness.waitForMethod("turn/start");
|
||||
await harness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
|
||||
const completed = harness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
|
||||
harness.close();
|
||||
await completed;
|
||||
|
||||
const result = await run;
|
||||
expect(result.promptError ?? undefined).toBeUndefined();
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// Codex tests cover run attempt.usage limits plugin behavior.
|
||||
import path from "node:path";
|
||||
import { saveAuthProfileStore } from "openclaw/plugin-sdk/agent-runtime";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readCodexRateLimitsRevision, rememberCodexRateLimitsRead } from "./rate-limit-cache.js";
|
||||
import { rememberCodexRateLimits } from "./rate-limit-cache.js";
|
||||
import {
|
||||
createParams,
|
||||
createStartedThreadHarness,
|
||||
@@ -26,11 +25,7 @@ describe("runCodexAppServerAttempt usage limits", () => {
|
||||
if (!harnessRef.current) {
|
||||
throw new Error("Expected Codex app-server harness to be initialized");
|
||||
}
|
||||
const revisionBeforeUpdate = readCodexRateLimitsRevision(harnessRef.current.client);
|
||||
await harnessRef.current.notify(rateLimitsUpdated(resetsAt));
|
||||
expect(readCodexRateLimitsRevision(harnessRef.current.client)).toBe(
|
||||
revisionBeforeUpdate + 1,
|
||||
);
|
||||
void harnessRef.current.notify(rateLimitsUpdated(resetsAt));
|
||||
throw Object.assign(new Error("You've reached your usage limit."), {
|
||||
data: { codexErrorInfo: "usageLimitExceeded" },
|
||||
});
|
||||
@@ -41,7 +36,6 @@ describe("runCodexAppServerAttempt usage limits", () => {
|
||||
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.authProfileId = authProfileId;
|
||||
params.agentDir = path.join(tempDir, "agents", "main", "agent");
|
||||
params.authProfileStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
@@ -54,13 +48,11 @@ describe("runCodexAppServerAttempt usage limits", () => {
|
||||
},
|
||||
},
|
||||
};
|
||||
saveAuthProfileStore(params.authProfileStore, params.agentDir);
|
||||
|
||||
const result = await runCodexAppServerAttempt(params);
|
||||
expect(result.promptErrorSource).toBe("prompt");
|
||||
expect(result.promptError).toContain("You've reached your Codex subscription usage limit.");
|
||||
expect(result.promptError).toContain("Next reset in");
|
||||
expect(params.authProfileStore.usageStats?.[authProfileId]?.blockedUntil).toBe(resetsAt * 1000);
|
||||
});
|
||||
|
||||
it("uses a recent Codex rate-limit snapshot when turn/start omits reset details", async () => {
|
||||
@@ -68,15 +60,7 @@ describe("runCodexAppServerAttempt usage limits", () => {
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const resetsAt = Math.ceil(Date.now() / 1000) + 120;
|
||||
const authProfileId = "openai:work";
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method === "turn/start") {
|
||||
throw Object.assign(new Error("You've reached your usage limit."), {
|
||||
data: { codexErrorInfo: "usageLimitExceeded" },
|
||||
});
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
rememberCodexRateLimitsRead(harness.client, {
|
||||
rememberCodexRateLimits({
|
||||
rateLimits: {
|
||||
limitId: "codex",
|
||||
limitName: "Codex",
|
||||
@@ -88,6 +72,14 @@ describe("runCodexAppServerAttempt usage limits", () => {
|
||||
},
|
||||
rateLimitsByLimitId: null,
|
||||
});
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method === "turn/start") {
|
||||
throw Object.assign(new Error("You've reached your usage limit."), {
|
||||
data: { codexErrorInfo: "usageLimitExceeded" },
|
||||
});
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.authProfileId = authProfileId;
|
||||
@@ -114,62 +106,6 @@ describe("runCodexAppServerAttempt usage limits", () => {
|
||||
expect(params.authProfileStore.usageStats?.[authProfileId]?.blockedUntil).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not trust an unrelated in-turn rate-limit update for profile blocking", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
const resetsAt = Math.ceil(Date.now() / 1000) + 120;
|
||||
const authProfileId = "openai:work";
|
||||
const harnessRef: { current?: ReturnType<typeof createStartedThreadHarness> } = {};
|
||||
const harness = createStartedThreadHarness(async (method) => {
|
||||
if (method === "turn/start") {
|
||||
if (!harnessRef.current) {
|
||||
throw new Error("Expected Codex app-server harness to be initialized");
|
||||
}
|
||||
await harnessRef.current.notify({
|
||||
method: "account/rateLimits/updated",
|
||||
params: {
|
||||
rateLimits: {
|
||||
limitId: "codex_other",
|
||||
primary: { usedPercent: 100, windowDurationMins: 60, resetsAt: resetsAt + 60 },
|
||||
rateLimitReachedType: "rate_limit_reached",
|
||||
},
|
||||
},
|
||||
});
|
||||
throw Object.assign(new Error("You've reached your usage limit."), {
|
||||
data: { codexErrorInfo: "usageLimitExceeded" },
|
||||
});
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
harnessRef.current = harness;
|
||||
rememberCodexRateLimitsRead(harness.client, {
|
||||
rateLimits: {
|
||||
limitId: "codex",
|
||||
primary: { usedPercent: 100, windowDurationMins: 300, resetsAt },
|
||||
rateLimitReachedType: "rate_limit_reached",
|
||||
},
|
||||
});
|
||||
const params = createParams(sessionFile, workspaceDir);
|
||||
params.authProfileId = authProfileId;
|
||||
params.authProfileStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
[authProfileId]: {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "access",
|
||||
refresh: "refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const result = await runCodexAppServerAttempt(params);
|
||||
|
||||
expect(result.promptError).toContain("Next reset in");
|
||||
expect(params.authProfileStore.usageStats?.[authProfileId]?.blockedUntil).toBeUndefined();
|
||||
});
|
||||
|
||||
it("refreshes Codex account rate limits when turn/start omits reset details", async () => {
|
||||
const sessionFile = path.join(tempDir, "session.jsonl");
|
||||
const workspaceDir = path.join(tempDir, "workspace");
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/** Small runtime-only Codex thread config boundary shared by isolated turns. */
|
||||
import type { JsonObject } from "./protocol.js";
|
||||
|
||||
// Stream structured patch snapshots so large generated edits keep the turn active.
|
||||
const CODEX_CODE_MODE_THREAD_CONFIG: JsonObject = {
|
||||
"features.code_mode": true,
|
||||
"features.code_mode_only": false,
|
||||
"features.apply_patch_streaming_events": true,
|
||||
};
|
||||
|
||||
const CODEX_CODE_MODE_DISABLED_THREAD_CONFIG: JsonObject = {
|
||||
"features.code_mode": false,
|
||||
"features.code_mode_only": false,
|
||||
};
|
||||
|
||||
/** Applies native code-mode policy without loading the full thread lifecycle. */
|
||||
export function buildCodexRuntimeThreadConfig(
|
||||
config: JsonObject | undefined,
|
||||
options: { nativeCodeModeEnabled?: boolean; nativeCodeModeOnlyEnabled?: boolean } = {},
|
||||
): JsonObject {
|
||||
const codeModeConfig: JsonObject = {
|
||||
...CODEX_CODE_MODE_THREAD_CONFIG,
|
||||
"features.code_mode_only": options.nativeCodeModeOnlyEnabled === true,
|
||||
};
|
||||
if (options.nativeCodeModeEnabled === false) {
|
||||
const disabledConfig = { ...config, ...CODEX_CODE_MODE_DISABLED_THREAD_CONFIG };
|
||||
// Patch streaming belongs to native code mode; omit it when that tool surface is disabled.
|
||||
delete disabledConfig["features.apply_patch_streaming_events"];
|
||||
return disabledConfig;
|
||||
}
|
||||
if (options.nativeCodeModeOnlyEnabled === true) {
|
||||
return { ...codeModeConfig, ...config, "features.code_mode_only": true };
|
||||
}
|
||||
return { ...codeModeConfig, ...config };
|
||||
}
|
||||
@@ -74,7 +74,6 @@ export function resolveCodexAppServerDirectSandboxBypassBlock(params: {
|
||||
method: string;
|
||||
requestParams?: unknown;
|
||||
config?: OpenClawConfig;
|
||||
agentId?: string;
|
||||
sessionKey?: string;
|
||||
sessionId?: string;
|
||||
}): string | undefined {
|
||||
@@ -82,7 +81,6 @@ export function resolveCodexAppServerDirectSandboxBypassBlock(params: {
|
||||
if (NODE_EXEC_BLOCKED_CONTROL_PLANE_METHODS.has(params.method)) {
|
||||
const nodeExecBlock = resolveCodexNativeNodeExecBlock({
|
||||
config: params.config,
|
||||
agentId: params.agentId,
|
||||
sessionKey: params.sessionKey,
|
||||
sessionId: params.sessionId,
|
||||
surface: `app-server method \`${params.method}\``,
|
||||
@@ -96,7 +94,6 @@ export function resolveCodexAppServerDirectSandboxBypassBlock(params: {
|
||||
}
|
||||
const nodeExecBlock = resolveCodexNativeNodeExecBlock({
|
||||
config: params.config,
|
||||
agentId: params.agentId,
|
||||
sessionKey: params.sessionKey,
|
||||
sessionId: params.sessionId,
|
||||
surface: `app-server method \`${params.method}\``,
|
||||
@@ -110,7 +107,6 @@ export function resolveCodexAppServerDirectSandboxBypassBlock(params: {
|
||||
}
|
||||
const sandboxBlock = resolveCodexNativeSandboxBlock({
|
||||
config: params.config,
|
||||
agentId: params.agentId,
|
||||
sessionKey,
|
||||
surface: `app-server method \`${params.method}\``,
|
||||
});
|
||||
@@ -129,7 +125,6 @@ export function resolveCodexAppServerDirectSandboxBypassBlock(params: {
|
||||
/** Resolves the generic native-execution block for sandboxed or node-hosted sessions. */
|
||||
export function resolveCodexNativeExecutionBlock(params: {
|
||||
config?: OpenClawConfig;
|
||||
agentId?: string;
|
||||
sessionKey?: string;
|
||||
sessionId?: string;
|
||||
surface: string;
|
||||
@@ -140,7 +135,6 @@ export function resolveCodexNativeExecutionBlock(params: {
|
||||
/** Returns a block message when native Codex execution cannot honor active sandboxing. */
|
||||
export function resolveCodexNativeSandboxBlock(params: {
|
||||
config?: OpenClawConfig;
|
||||
agentId?: string;
|
||||
sessionKey?: string;
|
||||
sessionId?: string;
|
||||
surface: string;
|
||||
@@ -151,7 +145,6 @@ export function resolveCodexNativeSandboxBlock(params: {
|
||||
}
|
||||
const runtime = resolveSandboxRuntimeStatus({
|
||||
cfg: params.config,
|
||||
agentId: params.agentId,
|
||||
sessionKey,
|
||||
});
|
||||
if (!runtime.sandboxed) {
|
||||
@@ -204,7 +197,6 @@ function formatCodexNativeSandboxBlock(params: { surface: string }): string {
|
||||
|
||||
function resolveCodexNativeNodeExecBlock(params: {
|
||||
config?: OpenClawConfig;
|
||||
agentId?: string;
|
||||
sessionKey?: string;
|
||||
sessionId?: string;
|
||||
surface: string;
|
||||
@@ -212,7 +204,6 @@ function resolveCodexNativeNodeExecBlock(params: {
|
||||
const sessionKey = params.sessionKey?.trim() || params.sessionId?.trim();
|
||||
const policy = resolveCodexNativeExecutionPolicy({
|
||||
config: params.config,
|
||||
agentId: params.agentId,
|
||||
sessionKey,
|
||||
readRuntimeSessionEntry: Boolean(sessionKey),
|
||||
});
|
||||
|
||||
@@ -10,31 +10,8 @@ import {
|
||||
} from "openclaw/plugin-sdk/agent-runtime-test-contracts";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { CodexThreadStartParams } from "./protocol.js";
|
||||
import {
|
||||
resetCodexTestBindingStore,
|
||||
registerCodexTestSessionIdentity,
|
||||
testCodexAppServerBindingStore,
|
||||
} from "./session-binding.test-helpers.js";
|
||||
import { createCodexTestModel, ensureCodexTestClientNotificationSurface } from "./test-support.js";
|
||||
import { startOrResumeThread as startOrResumeThreadImpl } from "./thread-lifecycle.js";
|
||||
|
||||
function startOrResumeThread(
|
||||
params: Omit<Parameters<typeof startOrResumeThreadImpl>[0], "bindingStore" | "abandonClient"> & {
|
||||
abandonClient?: () => Promise<void>;
|
||||
},
|
||||
) {
|
||||
registerCodexTestSessionIdentity(
|
||||
params.params.sessionFile,
|
||||
params.params.sessionId,
|
||||
params.params.sessionKey,
|
||||
);
|
||||
return startOrResumeThreadImpl({
|
||||
...params,
|
||||
client: ensureCodexTestClientNotificationSurface(params.client),
|
||||
abandonClient: params.abandonClient ?? (async () => undefined),
|
||||
bindingStore: testCodexAppServerBindingStore,
|
||||
});
|
||||
}
|
||||
import { createCodexTestModel } from "./test-support.js";
|
||||
import { startOrResumeThread } from "./thread-lifecycle.js";
|
||||
|
||||
let tempDir: string;
|
||||
|
||||
@@ -112,7 +89,6 @@ function threadStartResult(threadId = "thread-1", serviceTier: string | null = n
|
||||
|
||||
describe("Codex app-server dynamic tool schema boundary contract", () => {
|
||||
beforeEach(async () => {
|
||||
resetCodexTestBindingStore();
|
||||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-schema-contract-"));
|
||||
});
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
/** Process-stable plugin-state metadata for Codex app-server bindings. */
|
||||
export const CODEX_APP_SERVER_BINDING_NAMESPACE = "app-server-thread-bindings";
|
||||
export const CODEX_APP_SERVER_BINDING_MAX_ENTRIES = 50_000;
|
||||
@@ -1,31 +0,0 @@
|
||||
/** Lazy store facade that keeps binding schema/auth code off plugin startup. */
|
||||
import type { PluginStateSyncKeyedStore } from "openclaw/plugin-sdk/plugin-state-runtime";
|
||||
import {
|
||||
CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
||||
CODEX_APP_SERVER_BINDING_NAMESPACE,
|
||||
} from "./session-binding-meta.js";
|
||||
import type { CodexAppServerBindingStore, StoredCodexAppServerBinding } from "./session-binding.js";
|
||||
|
||||
export { CODEX_APP_SERVER_BINDING_MAX_ENTRIES, CODEX_APP_SERVER_BINDING_NAMESPACE };
|
||||
export type { StoredCodexAppServerBinding } from "./session-binding.js";
|
||||
|
||||
/** Defers schema compilation and auth loading until the first binding operation. */
|
||||
export function createLazyCodexAppServerBindingStore(
|
||||
state: Pick<PluginStateSyncKeyedStore<StoredCodexAppServerBinding>, "lookup" | "update">,
|
||||
): CodexAppServerBindingStore {
|
||||
let resolved: Promise<CodexAppServerBindingStore> | undefined;
|
||||
const store = () =>
|
||||
(resolved ??= import("./session-binding.js").then(({ createCodexAppServerBindingStore }) =>
|
||||
createCodexAppServerBindingStore(state),
|
||||
));
|
||||
return {
|
||||
read: async (identity) => (await store()).read(identity),
|
||||
mutate: async (identity, mutation) => (await store()).mutate(identity, mutation),
|
||||
prepareSessionGenerationReclaim: async (identity) =>
|
||||
(await store()).prepareSessionGenerationReclaim(identity),
|
||||
adoptSessionGeneration: async (identity, previousSessionId) =>
|
||||
(await store()).adoptSessionGeneration(identity, previousSessionId),
|
||||
retireSessionGeneration: async (identity) => (await store()).retireSessionGeneration(identity),
|
||||
withLease: async (identity, run) => (await store()).withLease(identity, run),
|
||||
};
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
/** In-memory binding store helpers for Codex app-server tests. */
|
||||
import type { PluginStateSyncKeyedStore } from "openclaw/plugin-sdk/plugin-state-runtime";
|
||||
import {
|
||||
bindingStoreKey,
|
||||
createCodexAppServerBindingStore,
|
||||
type CodexAppServerBindingStore,
|
||||
type CodexAppServerThreadBinding,
|
||||
type StoredCodexAppServerBinding,
|
||||
} from "./session-binding.js";
|
||||
|
||||
export function createCodexTestBindingStateStore(): PluginStateSyncKeyedStore<StoredCodexAppServerBinding> {
|
||||
const values = new Map<string, StoredCodexAppServerBinding>();
|
||||
return {
|
||||
register(key, value) {
|
||||
values.set(key, value);
|
||||
},
|
||||
registerIfAbsent(key, value) {
|
||||
if (values.has(key)) {
|
||||
return false;
|
||||
}
|
||||
values.set(key, value);
|
||||
return true;
|
||||
},
|
||||
update(key, updateValue) {
|
||||
const next = updateValue(values.get(key));
|
||||
if (next === undefined) {
|
||||
return false;
|
||||
}
|
||||
values.set(key, next);
|
||||
return true;
|
||||
},
|
||||
lookup: (key) => values.get(key),
|
||||
consume(key) {
|
||||
const value = values.get(key);
|
||||
values.delete(key);
|
||||
return value;
|
||||
},
|
||||
delete: (key) => values.delete(key),
|
||||
entries: () => [...values].map(([key, value]) => ({ key, value, createdAt: 0 })),
|
||||
clear: () => values.clear(),
|
||||
};
|
||||
}
|
||||
|
||||
export function createCodexTestBindingStore(): CodexAppServerBindingStore {
|
||||
return createCodexAppServerBindingStore(createCodexTestBindingStateStore());
|
||||
}
|
||||
|
||||
const sharedStateStore = createCodexTestBindingStateStore();
|
||||
export const testCodexAppServerBindingStore = createCodexAppServerBindingStore(sharedStateStore);
|
||||
const testSessionIdentities = new Map<
|
||||
string,
|
||||
{ agentId: string; sessionId: string; sessionKey?: string }
|
||||
>();
|
||||
|
||||
export function resetCodexTestBindingStore(): void {
|
||||
sharedStateStore.clear();
|
||||
testSessionIdentities.clear();
|
||||
}
|
||||
|
||||
export function registerCodexTestSessionIdentity(
|
||||
locator: string,
|
||||
sessionId: string,
|
||||
sessionKey?: string,
|
||||
agentId = "main",
|
||||
): void {
|
||||
testSessionIdentities.set(locator, {
|
||||
agentId,
|
||||
sessionId,
|
||||
...(sessionKey ? { sessionKey } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
export function seedCodexTestBinding(locator: string, binding: CodexAppServerThreadBinding): void {
|
||||
sharedStateStore.register(bindingStoreKey(testIdentity(locator)), {
|
||||
version: 1,
|
||||
state: "active",
|
||||
binding,
|
||||
});
|
||||
}
|
||||
|
||||
function testIdentity(locator: string) {
|
||||
const identity = testSessionIdentities.get(locator);
|
||||
return {
|
||||
kind: "session" as const,
|
||||
agentId: identity?.agentId ?? "main",
|
||||
sessionId: identity?.sessionId ?? locator,
|
||||
...(identity?.sessionKey ? { sessionKey: identity.sessionKey } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export async function readCodexAppServerBinding(
|
||||
sessionId: string,
|
||||
): Promise<CodexAppServerThreadBinding | undefined> {
|
||||
return await testCodexAppServerBindingStore.read(testIdentity(sessionId));
|
||||
}
|
||||
|
||||
export async function writeCodexAppServerBinding(
|
||||
sessionId: string,
|
||||
binding: CodexAppServerThreadBinding,
|
||||
): Promise<void> {
|
||||
await testCodexAppServerBindingStore.mutate(testIdentity(sessionId), { kind: "set", binding });
|
||||
}
|
||||
|
||||
export async function clearCodexAppServerBinding(sessionId: string): Promise<void> {
|
||||
await testCodexAppServerBindingStore.mutate(testIdentity(sessionId), { kind: "clear" });
|
||||
}
|
||||
|
||||
export async function clearCodexAppServerBindingForThread(
|
||||
sessionId: string,
|
||||
threadId: string,
|
||||
): Promise<boolean> {
|
||||
return await testCodexAppServerBindingStore.mutate(testIdentity(sessionId), {
|
||||
kind: "clear",
|
||||
threadId,
|
||||
});
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user