Skip to content

Add generic process lifecycle hooks - #31852

Closed
kmbroai wants to merge 2 commits into
dev/kyleb/plugin-script-analytics-contract-v3from
dev/kyleb/generic-process-lifecycle-hooks-v3
Closed

kmbroai wants to merge 2 commits into
dev/kyleb/plugin-script-analytics-contract-v3from
dev/kyleb/generic-process-lifecycle-hooks-v3

Conversation

@kmbroai

@kmbroai kmbroai commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

What

  • Add plugin-agnostic spawn, cancellation, and terminal-process lifecycle callbacks.
  • Thread the callbacks through local PTY, remote exec-server, and Windows sandbox execution paths.
  • Classify short-lived and background sandbox denials before emitting terminal lifecycle success.
  • Add focused tests for spawn and terminal callback behavior.

Why

Plugin script lifecycle metrics need a reliable execution-backend hook without coupling generic process execution to plugin-specific analytics. Terminal callbacks must report denials as failures before idempotent downstream consumers observe success.

Enhancement design: Codex Plugin Metrics

Stacked on #31851.

How

  • Keep lifecycle hooks no-op by default.
  • Invoke after_spawn only after successful child creation.
  • Notify cancellation and terminal completion from existing unified exec process/watcher paths.
  • Materialize sandbox-denial failure state before the first terminal callback.
  • Forward the spawn callback through Windows sandbox launch paths.

I tested this

  • just fmt — passed
  • just fix -p codex-core-plugins -p codex-plugin -p codex-analytics -p codex-core -p codex-features -p codex-app-server -p codex-windows-sandbox — passed (stack-wide scoped lint gate)
  • just argument-comment-lint — passed
  • just test -p codex-windows-sandbox — passed
  • just test -p codex-core unified_exec — 113/114 passed; both relevant short-lived/background network-denial tests passed, and the sole failure is the known unrelated local unified_exec_runs_under_sandbox harness assertion
  • just test — attempted; the local workspace build stopped before tests because the runner lacks system libcap for unrelated codex-bwrap. Exact-head CI provides the full Linux/macOS/Windows matrix.

@kmbroai
kmbroai force-pushed the dev/kyleb/plugin-script-analytics-contract-v3 branch from 8aa0023 to 1be315c Compare July 9, 2026 19:24
@kmbroai
kmbroai force-pushed the dev/kyleb/generic-process-lifecycle-hooks-v3 branch from 3af7e95 to 933dfab Compare July 9, 2026 19:24
@kmbroai
kmbroai force-pushed the dev/kyleb/plugin-script-analytics-contract-v3 branch from 1be315c to 0e6aeaa Compare July 9, 2026 22:28
@kmbroai
kmbroai force-pushed the dev/kyleb/generic-process-lifecycle-hooks-v3 branch from 933dfab to d58e174 Compare July 9, 2026 22:28
@kmbroai
kmbroai force-pushed the dev/kyleb/plugin-script-analytics-contract-v3 branch from 0e6aeaa to cf93b45 Compare July 9, 2026 22:54
@kmbroai
kmbroai force-pushed the dev/kyleb/generic-process-lifecycle-hooks-v3 branch from d58e174 to 2e37597 Compare July 9, 2026 22:54
@kmbroai
kmbroai marked this pull request as ready for review July 9, 2026 23:14
@kmbroai
kmbroai requested a review from a team as a code owner July 9, 2026 23:14

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e37597834

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}
let exit_code = process.exit_code();
let exit = exit_code.unwrap_or(-1);
process.notify_lifecycle_finished(exit_code, /*failed*/ false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check sandbox denial before reporting lifecycle success

For a sandboxed command that exits quickly, this new lifecycle notification runs before check_for_sandbox_denial_with_text() below can convert the exited process into a sandbox-denied failure. An idempotent lifecycle implementation (as required by the trait) will likely keep this first failed=false terminal result, so plugin/process metrics record a sandbox-denied execution as successful. Move the sandbox-denial check ahead of the success notification, or notify only after the terminal outcome is known.

Useful? React with 👍 / 👎.

.await;
} else {
let exit_code = process.exit_code().unwrap_or(-1);
process.notify_lifecycle_finished(Some(exit_code), /*failed*/ false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat background sandbox denials as lifecycle failures

When a stored/background sandboxed process exits after the initial call returns, the output task records ProcessState::sandbox_denied but does not set failure_message, so this watcher falls into the success branch and reports failed=false; there is no later poll required to correct the lifecycle result. This makes sandbox-denied background executions look successful to the new lifecycle metrics. Check sandbox_denied (or run the existing denial check against the transcript) before issuing the success finish notification.

Useful? React with 👍 / 👎.

if state.failure_message.is_none() {
let _ = self.state_tx.send_replace(state.failed(message));
}
self.notify_lifecycle_finished(/*exit_code*/ None, /*failed*/ true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Defer lifecycle finish until termination is observed

When a live process is failed by orchestration, such as the network-denial path calling fail_and_terminate, this reports the lifecycle as finished before terminate() has even been requested or any exit status can be observed. Because lifecycle implementations are expected to make terminal emission idempotent, the first (None, true) result can become the permanent terminal record even if the process later exits with a real status/output; mark the failure state here, but let the watcher/drop path finish the lifecycle after termination is observed.

Useful? React with 👍 / 👎.

if state.failure_message.is_none() {
let _ = self.state_tx.send_replace(state.failed(message));
}
self.notify_lifecycle_finished(/*exit_code*/ None, /*failed*/ true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mark cancellation before terminating failed live processes

When a live process is stopped through fail_and_terminate (for example after a deferred network denial), this path records a terminal failure and then terminates the process, but it never calls mark_cancelled; because state.failed(...) also makes has_exited() true, the later Drop path will skip cancellation as well. Lifecycle consumers that track orchestration-stopped processes will therefore miss these cancellations even though the process was still live when Codex killed it.

Useful? React with 👍 / 👎.

@kmbroai
kmbroai force-pushed the dev/kyleb/plugin-script-analytics-contract-v3 branch from cf93b45 to ef993e5 Compare July 10, 2026 01:10
@kmbroai
kmbroai force-pushed the dev/kyleb/generic-process-lifecycle-hooks-v3 branch from 2e37597 to 6bd7bd6 Compare July 10, 2026 01:10

kmbroai commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by openai/codex-internal#1387 after the Codex source-of-truth migration. The internal replacement preserves this stack position and carries the validated review fixes.

@kmbroai kmbroai closed this Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant