Skip to content

feat(sdk): expose headless patch generation - #774

Open
rohanpoudel2 wants to merge 10 commits into
openai:mainfrom
rohanpoudel2:feat/sdk-headless-patch
Open

feat(sdk): expose headless patch generation#774
rohanpoudel2 wants to merge 10 commits into
openai:mainfrom
rohanpoudel2:feat/sdk-headless-patch

Conversation

@rohanpoudel2

@rohanpoudel2 rohanpoudel2 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #681.

The TypeScript SDK can scan and validate findings, but generating a verified patch currently requires applications to launch and supervise the CLI. Add CodexSecurity.patch() so approval-gated remediation systems can run the existing fix-finding workflow through the SDK without parsing terminal output or delegating commit and publication control.

The required syntax is patch({ repositoryPath, finding }). Authentication defaults to "auto"; model and reasoning effort default to the constructor configuration and can be overridden per call. The supplied workspace is intentionally mutable. The SDK never commits, pushes, publishes, opens a pull request, or changes remote finding state. Existing Codex project trust is preserved, and a workspace without a saved trust decision is treated as untrusted before the patch thread starts.

Changes

  • Add PatchOptions and a readonly, discriminated PatchResult union for verified, no_change, blocked, and failed outcomes. Successful/no-change outcomes require verification proof; blocked/failed outcomes require a reason. Use a Responses-compatible root object schema with required nullable wire fields, then normalize it back to the discriminated public result.
  • Run the bundled fix-finding skill through the existing SDK runtime, authentication, provider, cancellation, activity, session-event, reconnect, and cost-tracking boundaries.
  • Restrict sandboxed remediation commands to workspace writes with approvals disabled, network access disabled, and web search disabled. Already-trusted repositories retain their project configuration, whose configured MCP servers run with host permissions outside the thread sandbox. Treat string findings as literal data rather than file paths.
  • Resolve the same project root Codex selects from project_root_markers (defaulting to .git), including configured package markers, existing .git markers, and worktree subdirectories. Preserve only that exact root's trusted/untrusted decision through a raw, path-quoted SDK config override; do not inherit trust from an enclosing worktree across a custom marker boundary, and default an unlisted root to untrusted so workingDirectory cannot persist trust and activate repository-local Codex configuration or MCP servers.
  • Validate model-reported changed files as repository-relative across POSIX and Windows path forms before returning them with thread identity and estimated cost. Callers retain responsibility for deriving and approving the authoritative diff and for all commit or delivery operations.
  • Document the API and cover the installed-package type contract, exhaustive result narrowing, immutable changed-file metadata, runtime export, and package archive.

No public CLI commands, arguments, flags, accepted values, environment variables, or defaults change.

Testing

  • bun test --timeout 30000 tests-ts/api-patch.test.ts --seed 12345 — 28 passed, 0 failed.
  • Project-trust and full API run (api, api-patch, and cli-patch-trust, seed 12345) — 178 passed, 2 platform skips, 0 failed.
  • Focused SDK/CLI remediation run (api-patch, api, cli-skills, and cli-patch, seed 12345) — 210 passed, 2 platform skips, 0 failed.
  • The pinned Codex CLI accepted the raw quoted trust override for a repository path containing spaces.
  • pnpm run types — passed, including generated models, MCP typecheck, and SDK TypeScript checks.
  • Strict NodeNext consumer compilation against the locally built package — passed, including exhaustive status narrowing and rejected invalid reasoning effort.
  • pnpm run build and pnpm run format — passed.
  • pnpm run test:mcp — all 23 test files passed.
  • pnpm pack — passed; the resulting archive contained 390 validated entries, and check-package.mjs passed.
  • pnpm run check:plugin-source, python .github/scripts/check_plugin_source_compatibility.py, and git diff --check — passed.
  • pnpm run test --seed 12345 was attempted on unsupported Node 25 and was not used as passing validation. It reached the same seven unchanged codex-review.test.ts fixture failures reproducible on main, then was interrupted after those deterministic failures appeared. The new patch tests had already passed in that run.
  • Installed-package smoke was attempted but npm could not fetch the repository's currently unpublished pinned @openai/codex@0.149.1 dependency (ETARGET). Archive validation and strict local consumer compilation passed instead.

Risk and rollout

This is an additive public SDK method and type surface. Existing scan, validation, CLI patch, authentication, and publication behavior is unchanged. The method can leave partial workspace edits when the model reports failure, the turn is interrupted, or result validation fails; callers must use a disposable or otherwise controlled workspace and inspect or discard its diff. changedFiles is model-reported metadata rather than an authoritative patch artifact. Cost is null when pricing or usage is unavailable.

A resolved project root explicitly marked trusted retains its ability to load project configuration, including configured MCP servers that run with host permissions rather than the patch thread sandbox. Trust is not inherited from an enclosing worktree across a custom marker boundary. Untrusted roots and roots without a saved decision remain untrusted when patch() supplies the working directory. Unix trust keys must match the exact resolved root; Windows canonical path aliases are accepted, and conflicting aliases fail closed to untrusted.

The first SDK surface intentionally excludes patch-risk assessment, commit creation, pull-request creation, publication, and finding closure. Those remain existing CLI or caller-owned workflows.

Public disclosure review

  • No customer, partner, prospect, or user identities, data, or identifying details are included.
  • No credentials, personal data, private source, scan findings, or nonpublic links or tickets are included.
  • I reviewed the branch name, title, description, commits, changes, comments, logs, screenshots, attachments, and links for public disclosure.
@github-actions github-actions Bot added the enhancement New feature or request label Sep 1, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-01T06:54:00.806670Z 42d5d2e Manual request
🔒 Security Review Completed 2026-09-01T07:02:12.490778Z 42d5d2e New commits

Security findings

Blocking findings (2)

ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Security Review

Here are some automated security review suggestions for this pull request.

Reviewed commit: e071faa9b1

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread sdk/typescript/src/api.ts
@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

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

Copy link
Copy Markdown

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: 08cdfc9323

ℹ️ 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".

Comment thread sdk/typescript/src/api.ts Outdated
@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

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

Copy link
Copy Markdown

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: 8f69987795

ℹ️ 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".

Comment thread sdk/typescript/README.md Outdated
Comment thread sdk/typescript/src/api.ts Outdated
@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

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

Copy link
Copy Markdown

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: 9825b0b7a3

ℹ️ 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".

Comment thread sdk/typescript/src/api.ts
@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

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

Copy link
Copy Markdown

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: 55cb677e2a

ℹ️ 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".

Comment thread sdk/typescript/src/api.ts Outdated
@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

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

Copy link
Copy Markdown

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: e4e77a11af

ℹ️ 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".

Comment thread sdk/typescript/src/api.ts Outdated
@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Provided git ref 1d9c787670e152f7b0db572a7d6a1b8b96031716 does not exist
ℹ️ 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".

@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

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

Copy link
Copy Markdown

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: 1d9c787670

ℹ️ 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".

Comment thread sdk/typescript/src/api.ts Outdated
@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

@rohanpoudel2

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 42d5d2eaf9

ℹ️ 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".

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Security Review

Here are some automated security review suggestions for this pull request.

Reviewed commit: 42d5d2eaf9

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread sdk/typescript/src/api.ts
Comment on lines +845 to +848
const projectRoot = await codexProjectRoot(
repository,
session.sessionConfig,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Security: Resolve trust roots from system configuration

Requires a host-wide Codex config with custom project_root_markers and a nested patch target. Fresh evidence beyond the prior marker-root fix is that Codex's own preflight loads the system layer before $CODEX_HOME, while this code resolves only session.sessionConfig; with no constructor override it therefore assumes .git. A contributor can commit the system marker plus a nested .codex/config.toml; the override is applied to the Git root, workingDirectory trusts the actual nested root, and its MCP command runs with host permissions. Resolve the root from every configuration layer Codex will load before starting the thread.

SECURITY.md reference: SECURITY.md:L63-L68


Dismiss this finding: Reply with @codex security dismiss <reason> [context]. Codex will resolve this conversation automatically; GitHub may require a page refresh to show the result.

Valid reasons: false-positive, duplicate, out-of-scope, compensating-control, risk-accepted, or other. Example: @codex security dismiss duplicate Already flagged by another review

What each reason means
  • false-positive — Not a vulnerability
  • duplicate — Already tracked elsewhere
  • out-of-scope — Outside this review's scope
  • compensating-control — Mitigated by another control
  • risk-accepted — Risk intentionally accepted
  • other — Another reason; context required

Useful? React with 👍 / 👎.

@strbbrn

strbbrn commented Sep 1, 2026

Copy link
Copy Markdown

This API now matches our approval-gated remediation use case: patch an isolated workspace, return structured verification, and leave authoritative diff capture, approval, commit creation, PR delivery, and finding closure to the caller.

Before adoption, we need the remaining project-trust issue resolved. An SDK patch operation must fail closed regardless of system-level project_root_markers; an unknown disposable workspace must not activate repository-local MCP servers or other trusted project configuration.

For an embedding-safe default, please consider disabling repository MCP servers for patch() unless the caller explicitly opts in. This would be safer and simpler than relying on exact trust reconstruction across every Codex configuration layer.

assessPatchRisk was included in issue #681's suggested contract but is excluded from this initial API. That is not a blocker for the core patch method, but please track it as a follow-up SDK capability.

@strbbrn

strbbrn commented Sep 1, 2026

Copy link
Copy Markdown

I checked the structured finding path. Object findings are passed through jsonForPrompt() and therefore serialized as JSON text in the Codex turn; this is functionally correct because the thread input is a text prompt, and the tests verify both literal-text and object findings.

One non-blocking SDK contract improvement would help embedding applications: PatchOptions.finding is currently typed as string | object, while the public contract promises a JSON-serializable object. The object type also permits circular values, BigInt, and custom toJSON() results that can fail or disappear only when JSON.stringify() runs.

Please consider exporting a reusable JsonValue/JsonObject type (shared with validate() where appropriate), converting serialization failures into a clear CodexSecurityError, and applying a documented payload-size limit consistent with the CLI. We will pass a canonical finding object containing taxonomy, locations, root cause, code evidence, attack path, remediation guidance, and validation context, so preserving a stable structured-input contract is useful. This is not a blocker for the core patch() API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

2 participants