diff --git a/.github/scripts/build-codex-package-archive.sh b/.github/scripts/build-codex-package-archive.sh index 7b5ac74865..ecac99f533 100644 --- a/.github/scripts/build-codex-package-archive.sh +++ b/.github/scripts/build-codex-package-archive.sh @@ -15,7 +15,7 @@ Usage: build-codex-package-archive.sh \ [--zsh-manifest ] \ [--codex-command-runner-bin ] \ [--codex-windows-sandbox-setup-bin ] \ - [--voice-signed-dir --release-version ] \ + [--voice-release-dir --release-version ] \ [--target-suffixed-entrypoint] EOF } @@ -30,7 +30,7 @@ bwrap_bin_provided="false" code_mode_host_bin_provided="false" command_runner_bin_provided="false" sandbox_setup_bin_provided="false" -voice_signed_dir="" +voice_release_dir="" release_version="" while [[ $# -gt 0 ]]; do @@ -93,8 +93,8 @@ while [[ $# -gt 0 ]]; do target_suffixed_entrypoint="true" shift ;; - --voice-signed-dir) - voice_signed_dir="${2:?--voice-signed-dir requires a value}" + --voice-release-dir) + voice_release_dir="${2:?--voice-release-dir requires a value}" shift 2 ;; --release-version) @@ -117,8 +117,8 @@ if [[ -z "$target" || -z "$bundle" || -z "$entrypoint_dir" || -z "$archive_dir" usage >&2 exit 1 fi -if [[ ( -n "$voice_signed_dir" || -n "$release_version" ) && ( -z "$voice_signed_dir" || -z "$release_version" || "$bundle" != "primary" || "$target" != *-apple-darwin ) ]]; then - echo "Signed voice resources require a primary macOS release package version" >&2 +if [[ ( -n "$voice_release_dir" || -n "$release_version" ) && ( -z "$voice_release_dir" || -z "$release_version" || "$bundle" != "primary" || ( "$target" != *-apple-darwin && "$target" != *-unknown-linux-musl && "$target" != *-pc-windows-msvc ) ) ]]; then + echo "Voice resources require a primary supported release package version" >&2 exit 1 fi @@ -204,7 +204,7 @@ python_args=( --cargo-profile release --package-dir "$package_dir" ) -if [[ -z "$voice_signed_dir" ]]; then +if [[ -z "$voice_release_dir" ]]; then python_args+=(--archive-output "$gzip_archive_path" --archive-output "$zstd_archive_path") fi if ((${#resource_args[@]} > 0)); then @@ -214,14 +214,19 @@ python_args+=(--force) "$python_bin" "${python_args[@]}" -if [[ -n "$voice_signed_dir" ]]; then +if [[ -n "$voice_release_dir" ]]; then + voice_target="$target" + if [[ "$target" == *-unknown-linux-musl ]]; then + voice_target="${target%-musl}-gnu" + fi voice_package="${RUNNER_TEMP:-/tmp}/${archive_stem}-voice-${target}" rm -rf "$voice_package" + voice_helper="${voice_release_dir%/}/codex-voice-host${exe_suffix}" "$python_bin" "${repo_root}/third_party/voice/assemble_package.py" \ --package "$package_dir" \ - --helper "${voice_signed_dir%/}/codex-voice-host" \ - --runtime "${voice_signed_dir%/}/runtime" \ - --voice-target "$target" \ + --helper "$voice_helper" \ + --runtime "${voice_release_dir%/}/runtime" \ + --voice-target "$voice_target" \ --build-commit "$(git -C "$repo_root" rev-parse HEAD)" \ --release-version "$release_version" \ --output "$voice_package" diff --git a/.github/scripts/setup-voice-windows.ps1 b/.github/scripts/setup-voice-windows.ps1 new file mode 100644 index 0000000000..a24d29b1f0 --- /dev/null +++ b/.github/scripts/setup-voice-windows.ps1 @@ -0,0 +1,96 @@ +# CI-only prerequisites. Never install these tools into a Codex package. +param( + [Parameter(Mandatory = $true)][string]$Target, + [Parameter(Mandatory = $true)][string]$SnapshotArchive +) + +$ErrorActionPreference = "Stop" +$PkgHashes = @{ + "x86_64-pc-windows-msvc" = @("x64", "5604cf25ef38bb6a09520cff25ae9f0ecd8c2443053b15e40df4ad1eae0e4405") + "aarch64-pc-windows-msvc" = @("arm64", "d5752ce2ac2296c8abb91fb12c12e75ee99ba8a18b52ce5febf325847b6f062b") +} +if (-not $PkgHashes.ContainsKey($Target)) { throw "Unsupported target: $Target" } +$Root = Join-Path $env:RUNNER_TEMP "voice-windows-build-tools" +$Evidence = Join-Path $Root "evidence" +$Cygwin = Join-Path $Root "cygwin" +$Cache = Join-Path $Root "cache" +New-Item -ItemType Directory -Path $Root, $Evidence | Out-Null +$ManifestPath = Join-Path $PSScriptRoot "voice-cygwin-snapshot.json" +$Manifest = Get-Content -Raw $ManifestPath | ConvertFrom-Json +$SnapshotTool = Join-Path $PSScriptRoot "voice-cygwin-inputs.py" +& python $SnapshotTool extract --archive $SnapshotArchive --directory (Join-Path $Cache $Manifest.cacheDirectory) +if ($LASTEXITCODE -ne 0) { throw "Cygwin snapshot could not be verified and extracted" } +Copy-Item $ManifestPath (Join-Path $Evidence "cygwin-snapshot.json") + +# Official https://cygwin.com/setup/sha512.sum and pkgconf release asset digests. +# The complete package closure is pinned separately in cygwin-snapshot.json. +$Inputs = @( + @{ + url = "https://cygwin.com/setup/setup-2.937.x86_64.exe" + file = "setup.exe" + algorithm = "SHA512" + digest = "6acea47c59781c9e7f544a18d53935d59df6e44d5d52ac95ee165671b8e388820455eebf30ccc7d254b00c3d0eb694269a0f8dc84b17944c1f355dac9c5aafcc" + }, + @{ + url = "https://github.com/pkgconf/pkgconf/releases/download/pkgconf-3.0.6/pkgconf-$($PkgHashes[$Target][0])-3.0.6.msi" + file = "pkgconf.msi" + algorithm = "SHA256" + digest = $PkgHashes[$Target][1] + } +) +foreach ($InputFile in $Inputs) { + $Destination = Join-Path $Root $InputFile.file + Invoke-WebRequest -Uri $InputFile.url -OutFile $Destination + $Actual = (Get-FileHash -Path $Destination -Algorithm $InputFile.algorithm).Hash.ToLowerInvariant() + if ($Actual -ne $InputFile.digest) { throw "Build input digest mismatch: $($InputFile.file)" } +} +$Inputs | ConvertTo-Json | Set-Content (Join-Path $Evidence "bootstrap-inputs.json") +$Packages = $Manifest.requestedPackages -join "," +# Only setup.xz + setup.xz.sig are present: local mode still verifies that +# signature. Never substitute an unsigned setup.ini or enable a network fallback. +$Setup = Start-Process -FilePath (Join-Path $Root "setup.exe") -Wait -PassThru -ArgumentList @( + "--quiet-mode", "--no-admin", "--no-shortcuts", "--only-site", + "--local-install", "--no-version-check", + "--root", "`"$Cygwin`"", "--local-package-dir", "`"$Cache`"", + "--packages", $Packages +) +if ($Setup.ExitCode -ne 0) { throw "Cygwin setup failed: $($Setup.ExitCode)" } +Copy-Item (Join-Path $Cygwin "var/log/setup.log*") $Evidence +Copy-Item (Join-Path $Cygwin "etc/setup/installed.db") $Evidence +& (Join-Path $Cygwin "bin/cygcheck.exe") -cd | Set-Content (Join-Path $Evidence "packages.txt") +if ($LASTEXITCODE -ne 0) { throw "Cannot inventory Cygwin packages" } +& python $SnapshotTool check-installed --inventory (Join-Path $Evidence "packages.txt") +if ($LASTEXITCODE -ne 0) { throw "Installed Cygwin package set differs from the snapshot" } +Get-ChildItem $Cache -File -Recurse | ForEach-Object { + @{ file = [IO.Path]::GetRelativePath($Cache, $_.FullName); bytes = $_.Length; + sha512 = (Get-FileHash $_.FullName -Algorithm SHA512).Hash.ToLowerInvariant() } +} | ConvertTo-Json | Set-Content (Join-Path $Evidence "package-cache.json") + +# Administrative extraction uses TARGETDIR, not a normal MSI installation: +# no product registration or system PATH modification. Fail rather than fall +# back to an ambient executable or ordinary installer if the payload is absent. +$Image = Join-Path $Root "pkgconf-image" +$Extract = Start-Process msiexec.exe -Wait -PassThru -ArgumentList @( + "/a", "`"$(Join-Path $Root 'pkgconf.msi')`"", "/qn", "/norestart", + "TARGETDIR=`"$Image`"", "/L*v", "`"$(Join-Path $Evidence 'pkgconf-extract.log')`"" +) +if ($Extract.ExitCode -ne 0) { throw "pkgconf extraction failed: $($Extract.ExitCode)" } +$Candidates = @(Get-ChildItem $Image -Filter pkgconf.exe -File -Recurse) +if ($Candidates.Count -ne 1) { throw "Expected exactly one native pkgconf executable" } +# Bazel shell-quotes execpaths containing spaces; Cargo executes PKG_CONFIG +# directly. Keep the executable and its support files at a space-free execpath. +$Native = Join-Path $Image "native" +if (Test-Path $Native) { throw "Native pkgconf destination must be fresh" } +Move-Item -LiteralPath $Candidates[0].Directory.FullName -Destination $Native +$PkgConfig = Join-Path $Native "pkgconf.exe" +$Version = & $PkgConfig --version +if ($LASTEXITCODE -ne 0 -or $Version -ne "3.0.6") { throw "Unexpected native pkgconf version" } +@{ target = $Target; cygwin_root = $Cygwin; pkg_config = $PkgConfig; + pkgconf_version = $Version; requested_packages = $Packages } | + ConvertTo-Json | Set-Content (Join-Path $Evidence "tools.json") +# Declare complete installed support trees only after snapshot and tool checks. +& python (Join-Path $PSScriptRoot "voice_windows_tools.py") --root $Root --target $Target --pkg-config $PkgConfig +if ($LASTEXITCODE -ne 0) { throw "Cannot declare Windows Bazel tool inputs" } +"VOICE_WINDOWS_BAZEL_REPOSITORY=$Root" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append +"VOICE_CYGWIN_ROOT=$Cygwin" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append +"VOICE_PKG_CONFIG=$PkgConfig" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append diff --git a/.github/scripts/test_voice_cygwin_inputs.py b/.github/scripts/test_voice_cygwin_inputs.py new file mode 100644 index 0000000000..3b5a4d31ae --- /dev/null +++ b/.github/scripts/test_voice_cygwin_inputs.py @@ -0,0 +1,83 @@ +"""Offline checks for the public Windows build-input bootstrap.""" + +import hashlib +import importlib.util +import io +from pathlib import Path +import tarfile +import tempfile +import unittest + + +SCRIPT = Path(__file__).with_name("voice-cygwin-inputs.py") +SPEC = importlib.util.spec_from_file_location("voice_cygwin_inputs", SCRIPT) +inputs = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(inputs) + + +class VoiceCygwinInputsTests(unittest.TestCase): + def test_input_paths_cannot_escape_the_local_cache(self): + manifest = { + "metadata": [ + {"file": "x86_64/setup.xz"}, + {"file": "x86_64/setup.xz.sig"}, + ], + "packages": [{"file": "../outside.tar.xz"}], + } + with self.assertRaisesRegex(ValueError, "unsafe Cygwin input"): + inputs.records(manifest) + + def test_extract_verifies_archive_and_every_input_before_offline_installation(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + payloads = { + "x86_64/setup.xz": b"metadata", + "x86_64/setup.xz.sig": b"signature", + "x86_64/release/bash/bash.tar.xz": b"package", + } + records = [ + { + "file": name, + "bytes": len(data), + "sha512": hashlib.sha512(data).hexdigest(), + } + for name, data in payloads.items() + ] + archive = root / "inputs.tar.gz" + + def write_archive(contents): + with tarfile.open(archive, "w:gz") as bundle: + for name, data in contents.items(): + info = tarfile.TarInfo(name) + info.size = len(data) + bundle.addfile(info, io.BytesIO(data)) + return { + "bytes": archive.stat().st_size, + "sha256": hashlib.sha256(archive.read_bytes()).hexdigest(), + } + + manifest = { + "metadata": records[:2], + "packages": records[2:], + "archive": write_archive(payloads), + } + destination = root / "cache" + inputs.extract(manifest, archive, destination) + self.assertEqual( + { + record["file"]: (destination / record["file"]).read_bytes() + for record in records + }, + payloads, + ) + + (destination / records[2]["file"]).unlink() + payloads[records[2]["file"]] = b"wrong" + manifest["archive"] = write_archive(payloads) + with self.assertRaisesRegex(ValueError, "Cygwin input mismatch"): + inputs.extract(manifest, archive, root / "rejected") + self.assertFalse((root / "rejected").exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/.github/scripts/voice-cygwin-inputs.py b/.github/scripts/voice-cygwin-inputs.py new file mode 100644 index 0000000000..11b2ebda7d --- /dev/null +++ b/.github/scripts/voice-cygwin-inputs.py @@ -0,0 +1,98 @@ +"""Verify the pinned Cygwin build-input archive before offline installation.""" + +import argparse +import hashlib +import json +from pathlib import Path, PurePosixPath +import shutil +import tarfile + + +def records(manifest): + metadata = manifest["metadata"] + if {item["file"] for item in metadata} != { + "x86_64/setup.xz", + "x86_64/setup.xz.sig", + }: + raise ValueError("signed setup metadata is required") + entries = metadata + manifest["packages"] + expected = {entry["file"]: entry for entry in entries} + if len(expected) != len(entries): + raise ValueError("duplicate Cygwin input") + for name in expected: + path = PurePosixPath(name) + if ( + not path.parts + or path.is_absolute() + or ".." in path.parts + or path.as_posix() != name + or "\\" in name + or ":" in name + ): + raise ValueError(f"unsafe Cygwin input: {name}") + return expected + + +def extract(manifest, archive, destination): + expected = records(manifest) + pin = manifest["archive"] + with archive.open("rb") as stream: + if ( + archive.stat().st_size != pin["bytes"] + or hashlib.file_digest(stream, "sha256").hexdigest() != pin["sha256"] + ): + raise ValueError("Cygwin archive digest or size mismatch") + stream.seek(0) + with tarfile.open(fileobj=stream, mode="r:gz") as bundle: + members = bundle.getmembers() + if len(members) != len(expected) or {m.name for m in members} != set( + expected + ): + raise ValueError("missing, extra, or duplicate Cygwin inputs") + for member in members: + if not member.isfile() or member.issparse(): + raise ValueError(f"non-regular Cygwin input: {member.name}") + with bundle.extractfile(member) as source: + if ( + member.size != expected[member.name]["bytes"] + or hashlib.file_digest(source, "sha512").hexdigest() + != expected[member.name]["sha512"] + ): + raise ValueError(f"Cygwin input mismatch: {member.name}") + destination.mkdir(parents=True) + try: + bundle.extractall(destination, members=members, filter="data") + except BaseException: + shutil.rmtree(destination) + raise + + +def check_installed(manifest, inventory): + installed = sorted( + tuple(line.split()) + for line in inventory.read_text().splitlines() + if len(line.split()) == 2 and not line.startswith("Package ") + ) + expected = sorted((item["name"], item["version"]) for item in manifest["packages"]) + if installed != expected: + raise ValueError("installed Cygwin packages differ from reviewed inputs") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("command", choices=("extract", "check-installed")) + parser.add_argument("--archive", type=Path) + parser.add_argument("--directory", type=Path) + parser.add_argument("--inventory", type=Path) + args = parser.parse_args() + pinned = json.loads( + Path(__file__).with_name("voice-cygwin-snapshot.json").read_text() + ) + if args.command == "extract": + if args.archive is None or args.directory is None: + parser.error("extract requires --archive and --directory") + extract(pinned, args.archive, args.directory) + else: + if args.inventory is None: + parser.error("check-installed requires --inventory") + check_installed(pinned, args.inventory) diff --git a/.github/scripts/voice-cygwin-snapshot.json b/.github/scripts/voice-cygwin-snapshot.json new file mode 100644 index 0000000000..8679adca1e --- /dev/null +++ b/.github/scripts/voice-cygwin-snapshot.json @@ -0,0 +1,145 @@ +{ + "schemaVersion": 1, + "sourceMirror": "https://mirrors.kernel.org/sourceware/cygwin/", + "cacheDirectory": "https%3a%2f%2fmirrors.kernel.org%2fsourceware%2fcygwin%2f", + "requestedPackages": [ + "bash", + "make", + "automake1.18", + "coreutils", + "diffutils", + "findutils", + "grep", + "sed", + "gawk" + ], + "signingFingerprint": "56405CF6FCC81574682A5D561A698DE9E2E56300", + "archive": { + "tag": "voice-cygwin-108b38cf67cbb731", + "name": "cygwin-build-inputs.tar.gz", + "bytes": 62276721, + "sha256": "108b38cf67cbb731801e5ca8d8412b8f349961e4e71d8e3d5a1768f1a8d15c50" + }, + "metadata": [ + { + "file": "x86_64/setup.xz", + "bytes": 3598392, + "sha512": "306bffefa1e86178d9c6ff78a8add2e1af3c96f7cd698d62b58d0237a568e1a9f1b30016ed5891f9fe1d1c8a1e58089c3d9cb8989eb7bed758a928f212078e25" + }, + { + "file": "x86_64/setup.xz.sig", + "bytes": 566, + "sha512": "7c466da6446bbef67762ff4038dd4986018ed09432bd6ee6ca9e33150874dbb9d61c7e4b06bd925f932a55fda345401224689bbaddc135d7300651f67ec8160b" + } + ], + "sourceArchive": { + "name": "cygwin-build-sources.tar", + "bytes": 368824320, + "sha256": "13a42ea4e6465f77c32f91e4162f41bcfeab7c0eca47701bb6a8b9aca1eb99f4" + }, + "packages": [ + {"name": "_autorebase", "version": "001091-1", "file": "noarch/release/_autorebase/_autorebase-001091-1.tar.zst", "bytes": 4696, "sha512": "a9416ce992ca57f3a6208f46517520d21dfac61988ac84cbb5b17b76e053a0845c15368ccb8ca9c483d8935f9e3db81568433c0d08d0265ec5e6916ea30f2831"}, + {"name": "alternatives", "version": "1.31-1", "file": "x86_64/release/alternatives/alternatives-1.31-1.tar.xz", "bytes": 123748, "sha512": "94adf3376e7fcaf937594be718f54a99f2a2b66367eb2bb294b6fd4a933a9515954a9e9f309ed74cc62fbb353adb053d8500bbaf42994e4355f7180664afa62c"}, + {"name": "autoconf", "version": "15-3", "file": "noarch/release/autoconf/autoconf-15-3-noarch.tar.zst", "bytes": 2396, "sha512": "edaa6c97c5b50e138acef4b80406bfdd0988da3360cc622be2541593f6e926a4fbe1088ff608ddadfacf275b3924605d54f59e140cc7c79b31f4885e5878ad6f"}, + {"name": "autoconf2.1", "version": "2.13-12", "file": "noarch/release/autoconf2.1/autoconf2.1-2.13-12.tar.bz2", "bytes": 204911, "sha512": "96f8f592fff4665512c0023d6a7844fdc281ba99dc98dee9e55c37523b3a1279ae8fd31559fa6426edb1da7559a483b3eae99ae7911d50646ec60338ef209c18"}, + {"name": "autoconf2.5", "version": "2.69-5", "file": "noarch/release/autoconf2.5/autoconf2.5-2.69-5.tar.zst", "bytes": 341597, "sha512": "3739719f29ad3ab1c1045b72ccbfe6c58e3f7696ac063c2ff4e09e11e1a9015f289fa8fc267a62b43cb2f6b682b83d3b4b6450040e733f9f41d086dc4a2f36b9"}, + {"name": "autoconf2.7", "version": "2.73-1", "file": "noarch/release/autoconf2.7/autoconf2.7-2.73-1-noarch.tar.zst", "bytes": 906344, "sha512": "0493d68a7ea9cd61e403b6b7086b5302bad5bc1cc35715349f59605a795ac7a9f901c444cb387e5f44d1860de3e5d0fcfceee4ee2726d68904cfc5e2db6e90e9"}, + {"name": "automake1.18", "version": "1.18.1-1", "file": "noarch/release/automake1.18/automake1.18-1.18.1-1-noarch.tar.zst", "bytes": 921189, "sha512": "416ef8041505ecddc23e9e72394ef3a81311c6c378b8b6d348e989584c77924d40aa427b47ab4d3a9a2b83438d13995bb7c6302afb73af3d3af7f2e9061579c0"}, + {"name": "base-cygwin", "version": "3.8-2", "file": "noarch/release/base-cygwin/base-cygwin-3.8-2.tar.xz", "bytes": 1260, "sha512": "e03949b4ae6b692858e1d29fb764afbda990c07a6b70b0eb7219fc12f28dfba3cf7e757a7570c31e315be79f091b012d6a6ddafdb4b0b8961afe9af9f4977fab"}, + {"name": "base-files", "version": "4.3-3", "file": "noarch/release/base-files/base-files-4.3-3.tar.zst", "bytes": 46481, "sha512": "dfafbe28fbe89d9b72a2eeb10901712e04de71d9089a85cfd2b8d45ffd739bdd49dadb7b57d098f01b8902456f5b839e9d4a10dbd6a8001573e035fde6797b63"}, + {"name": "bash", "version": "5.2.21-1", "file": "x86_64/release/bash/bash-5.2.21-1.tar.xz", "bytes": 1650932, "sha512": "c3a5594f1c248dd27a3b5aa10c31fb1651072eae878479d3d8cab09c28354ce4509a2fe230f176ad200a6484a561bc10c2dc9c74c4e9f03164190dac2e683d65"}, + {"name": "bzip2", "version": "1.0.8-2", "file": "x86_64/release/bzip2/bzip2-1.0.8-2-x86_64.tar.xz", "bytes": 36396, "sha512": "86e4b0b21b64d17b9fb60260a787626125102b0cb786de67893cb1d39a109457637bdb93956c992429f1135f64b07def64dd0116682a6190b635ba12f81395d7"}, + {"name": "ca-certificates", "version": "2025.2.80_v9.0.304-1", "file": "noarch/release/ca-certificates/ca-certificates-2025.2.80_v9.0.304-1-noarch.tar.zst", "bytes": 902180, "sha512": "49d08f490db12855f8794cd5cfc721940504d36deffdd3b9aea0fc75909e27471f795d48c739630498fd4baeecb76ee0adb5e5c4cecab4db2ea39b03391a02d0"}, + {"name": "coreutils", "version": "9.0-1", "file": "x86_64/release/coreutils/coreutils-9.0-1.tar.xz", "bytes": 2767296, "sha512": "530f47640d878f5953580f3556c492a93786191abe573322026b70206a46c7f17d05927784db3e7f2c044d1555f35941880316f7288b887eaec770a9eda4c687"}, + {"name": "crypto-policies", "version": "20190218-1", "file": "noarch/release/crypto-policies/crypto-policies-20190218-1.tar.xz", "bytes": 14032, "sha512": "44d50ff08528df786eeb26ec1d312445ec92d77ecc9f448a3b741f7647e187f11eabdc0b38f8ab8de803e21d2e610f67eb44de7554f3fa83f6e8172e609b0054"}, + {"name": "cygutils", "version": "1.4.17-4", "file": "x86_64/release/cygutils/cygutils-1.4.17-4-x86_64.tar.xz", "bytes": 90580, "sha512": "a9c63f8ac86e529fa8809c9637bb80651721003160726e2058c361687e5df6377c4cdbea2d023677b23e1d166bbce37d5a412f7d1135baf075ae2631fdeb77b2"}, + {"name": "cygwin", "version": "3.6.10-1", "file": "x86_64/release/cygwin/cygwin-3.6.10-1-x86_64.tar.xz", "bytes": 1626060, "sha512": "50e5a100f5229eecba279799363a30d599f8c6ad06967eabc4fc8ed9105736576a522539abc250110282cf18da8536d4e408823cc6c0d2abd8692a91d9fd3341"}, + {"name": "dash", "version": "0.5.12-5", "file": "x86_64/release/dash/dash-0.5.12-5.tar.xz", "bytes": 79612, "sha512": "8891165224ca8531ad7b601309bc1a74f8b68dad24721060fd3db5d12ae504e3c96bbd0217006bbcbe0650d13283f04c1f58412bde54c40f19acb5291dfaced9"}, + {"name": "diffutils", "version": "3.12-1", "file": "x86_64/release/diffutils/diffutils-3.12-1-x86_64.tar.xz", "bytes": 431400, "sha512": "2a9d8fa0acd5a6108f74afe515f55a2a7cade6fa7f96368e46a6f33cf3b49d270b50dc3cee0320740c8f1729edcbf8dd3824f4b268261fba7aeed0d75a4e943c"}, + {"name": "editrights", "version": "1.04-1", "file": "x86_64/release/editrights/editrights-1.04-1.tar.xz", "bytes": 7112, "sha512": "aadc188201441ee1ff5710df0e5550c8ca5cb65421d025c5845921510560a2875c317d1f35b6ea8abbb5077172c0de7fc8f2f55ae95dc8d1e59d26c963c93aa7"}, + {"name": "file", "version": "5.46-1", "file": "x86_64/release/file/file-5.46-1-x86_64.tar.xz", "bytes": 840940, "sha512": "e4b52477d353557d36af625a6a55c97a11ae41053ce17602624210df2a6d2683f46250c2b9f6db5a9f9e5fcccb7b0be4b24705a36d196cee677bf47d90dfee99"}, + {"name": "findutils", "version": "4.11.0-1", "file": "x86_64/release/findutils/findutils-4.11.0-1-x86_64.tar.xz", "bytes": 768172, "sha512": "cbb1359fd81c55f98da1485f84f11f905a7a3fe9ce1676424e86da4d7f0effa5fc298546bd5c62c9707e8ddf93c31597d6da01e60be0610c50b9b22ef9475814"}, + {"name": "gawk", "version": "5.4.0-1", "file": "x86_64/release/gawk/gawk-5.4.0-1-x86_64.tar.xz", "bytes": 1496256, "sha512": "3604c99921d0cff387279aec1d3b332e8afcd2746b052304c0511221efdb6ae6d4a3117b36593e44adc43c67b6f6c4c48bccf6b686e2e1a5cac644c89613d829"}, + {"name": "getent", "version": "2.18.90-5", "file": "x86_64/release/getent/getent-2.18.90-5.tar.xz", "bytes": 17864, "sha512": "b860f3d01f32da7312a0ce118bdaa53e2418c53f0ac5cfd52b3e4267f353fa21ae27fd2649eb9c6bff48b3d10a0a9f7bcaa2d644f770f0f3916f5d969beedd65"}, + {"name": "gettext-locale-alias", "version": "0.26-1", "file": "x86_64/release/gettext/gettext-locale-alias/gettext-locale-alias-0.26-1-x86_64.tar.xz", "bytes": 1596, "sha512": "289feafbe00598a2451527b64b0187ab62115577ceaa960774b020270e5a8c5bff4038f389a44c7d9eb870f7e2741c7bb2b27be507843cc8c4ae5c33bc74c8f3"}, + {"name": "grep", "version": "3.12-1", "file": "x86_64/release/grep/grep-3.12-1-x86_64.tar.xz", "bytes": 418788, "sha512": "2ba28b5d93c352bc3f3f0f232e2052e6651b71cc535841f4ff8cedb23e61e9e4c030d7d5516ed9c797074e1a59cbf3a56732ca678cb0e986e86dcbbf25955976"}, + {"name": "groff", "version": "1.24.1-1", "file": "x86_64/release/groff/groff-1.24.1-1-x86_64.tar.zst", "bytes": 5878965, "sha512": "9e8aaab0e6f464df3fec6b96fed6679673091aaeafaa463ed518081cb156ef7441fb5f9341a251f602334862640ea0b0fc1b158746c08a1e2529cceeb37e9802"}, + {"name": "gzip", "version": "1.14-1", "file": "x86_64/release/gzip/gzip-1.14-1-x86_64.tar.xz", "bytes": 164124, "sha512": "57995d2e90bf435ffff8f61cd79c4970ad7260e0c407ab52d4dbacb0540a74c7f8fd0d8bd905316eff31525d9b0367284f9f250950f78f179e5260ce59be3a98"}, + {"name": "hostname", "version": "3.13-1", "file": "x86_64/release/hostname/hostname-3.13-1.tar.bz2", "bytes": 14024, "sha512": "75114326fa4575b8341434658a881feb81a6565e7e0adf8fd100dde87022d3818b5fc3df430bd9b3298d818a1d23f6b6c4bc7ff33efb3ded5f0ea0bf118c1aae"}, + {"name": "info", "version": "7.3-1", "file": "x86_64/release/texinfo/info/info-7.3-1-x86_64.tar.zst", "bytes": 607040, "sha512": "3af0e7c2edc34a068d88974c6c1b0f529663f4bbce145b5a33f3b7e90671fd908fb4a544e033b98a6906d3244c64a18732104c5f1d323c0ccac8a5134e13f411"}, + {"name": "ipc-utils", "version": "1.1-1", "file": "x86_64/release/ipc-utils/ipc-utils-1.1-1.tar.xz", "bytes": 11120, "sha512": "374ef40c871d00eb3b4774c383bb85872443a2e8b9abf74b8b9b9b27984598abfb6284716c2d2cd264181e6d012cf7dc8cd04f6bab91f9b083f72e9b51e6226b"}, + {"name": "less", "version": "704-1", "file": "x86_64/release/less/less-704-1-x86_64.tar.xz", "bytes": 171024, "sha512": "2d1ecf29272e7c1781bb0df5b0f0531b009972edc8d246d63e17edf7d8314109f0a8f38668b371c6f5e1dc1f948d63384eecdca5409eacb927c4f267f89f4bfe"}, + {"name": "libargp", "version": "20250917-2", "file": "x86_64/release/libargp/libargp-20250917-2-x86_64.tar.xz", "bytes": 19508, "sha512": "a83ef7c1809cae1b10a50e228643a640ea14e1be324429b9f53be9a4c8cee03ddd0efb46b24e159493dfa36569e73b636bc897943d61cdb90511e5e4cbb54257"}, + {"name": "libattr1", "version": "2.5.2-2", "file": "x86_64/release/attr/libattr1/libattr1-2.5.2-2-x86_64.tar.xz", "bytes": 6160, "sha512": "345b8f84b9c4df2eeea5e211dbaff5e0382aaee788435e1f4cc69f546e1603bbb45e9baec02a37d935674fcd55fb782d924c3a1540d2f829bde4969c96945ce4"}, + {"name": "libblkid1", "version": "2.40.2-2", "file": "x86_64/release/util-linux/libblkid1/libblkid1-2.40.2-2.tar.xz", "bytes": 120936, "sha512": "d931c47cce5a7caf18799a16ed526e0f0d0f82c0f6d0af952420d50d88f1e0f0ea9d6c07be72f9d0baa06a3299b2db8c3bd3c43d13d94400edc816e7c00da148"}, + {"name": "libbz2_1", "version": "1.0.8-2", "file": "x86_64/release/bzip2/libbz2_1/libbz2_1-1.0.8-2-x86_64.tar.xz", "bytes": 26884, "sha512": "d15d88e8e0fae69f7c4c3025515e748c273d758a0f470312162cec46c4cf0d340d2ef036e3a4f4d03b5edf4508b4927834ffa0f9aa45585a5f3bc3b3163ff675"}, + {"name": "libcrypt2", "version": "4.5.2-1", "file": "x86_64/release/libxcrypt/libcrypt2/libcrypt2-4.5.2-1-x86_64.tar.xz", "bytes": 106104, "sha512": "7668d29aaebba8ca94e48e810d92108db72b067b2786cea8dcf4a049c29f20f0aeb8653a6e9524869c69e0b1df9d4d0ac499a052a86f0d44178e8a8167002c97"}, + {"name": "libdb18.1", "version": "18.1.40-1", "file": "x86_64/release/db/libdb18.1/libdb18.1-18.1.40-1.tar.xz", "bytes": 733872, "sha512": "51955ae95823c54051b5345a7dbe6927521411754dff416ad6af2061bd3c9b2230ca2ea88f9b921b4b09b7654c7dac924b0ae08af708a93604cc193d6a18f4ff"}, + {"name": "libfdisk1", "version": "2.40.2-2", "file": "x86_64/release/util-linux/libfdisk1/libfdisk1-2.40.2-2.tar.xz", "bytes": 160264, "sha512": "0c8e6854eb72bb8cc51fc33565ee0d36a747e520e98e074a1d2400672a45651807b53be0f5a1c97efa0ce34975063f86953c9465312dcfdaba8104edb1298d62"}, + {"name": "libffi8", "version": "3.8.0-1", "file": "x86_64/release/libffi/libffi8/libffi8-3.8.0-1-x86_64.tar.xz", "bytes": 196924, "sha512": "935a0567f740a314d70ecd4adc8c4cf5ad37c6fb43f2d26dee882d4fda71f08efb2646bee10cee6e2000f52e25cce87fa29ce07665ba6d22532e0daa76bc887c"}, + {"name": "libgc1", "version": "8.2.12-1", "file": "x86_64/release/libgc/libgc1/libgc1-8.2.12-1-x86_64.tar.xz", "bytes": 62392, "sha512": "faf226ea2b58949da714e2c50f1aa95738082c3499e2ae4828af5173bdd617248f000bf4868636b31a2b76b3a905057ab307c4f6a0d7112f23422531a97be92d"}, + {"name": "libgcc1", "version": "14.4.0-1", "file": "x86_64/release/gcc/libgcc1/libgcc1-14.4.0-1-x86_64.tar.zst", "bytes": 59166, "sha512": "5ac8a56e5eee285efcd6dbd05a473e795e181021d7194b8a98dfe2fa201a71f0abe2908e0b4c81490eb5ef472efaeac8e56a61a5f289eee4ad6e90e52a844ff8"}, + {"name": "libgdbm6", "version": "1.26-1", "file": "x86_64/release/gdbm/libgdbm6/libgdbm6-1.26-1-x86_64.tar.xz", "bytes": 25436, "sha512": "8ed666a5b978b40f8cfc69d270d47cd674a31eb41bac499f783137eedfde6aa02ba4346de3d5dc3184d9af10ec80f57730c7517a88732616f4c6e0ef637d4bce"}, + {"name": "libgdbm_compat4", "version": "1.26-1", "file": "x86_64/release/gdbm/libgdbm_compat4/libgdbm_compat4-1.26-1-x86_64.tar.xz", "bytes": 3892, "sha512": "fd026dd74c495c1ee67b790269e77e8525ad78d52d4895923443feab55f7bb32dc07b115d490143c7e0277167d9cdbef5dad4707ecc7ca6df13a9f1a5184f8ca"}, + {"name": "libgmp10", "version": "6.3.0-1", "file": "x86_64/release/gmp/libgmp10/libgmp10-6.3.0-1.tar.zst", "bytes": 254680, "sha512": "947cd56e1945ddf180622b453f17e6655b4ee3e79fcc91238370a8826b88cd07c31d939f1ed7b87d91344d46fee90af2560105267a9207dcca800cba72459191"}, + {"name": "libguile3.0_1", "version": "3.0.11-1", "file": "x86_64/release/guile3.0/libguile3.0_1/libguile3.0_1-3.0.11-1-x86_64.tar.xz", "bytes": 6969492, "sha512": "a559914ed5d62d0a701e4ce3810276436cf1ce54d579c72fc72c3b4688f2af71ef93f3cdae7089ad7f85107c7ca5c87f2e497cac6f2550b38df1bc6ba7f16230"}, + {"name": "libiconv2", "version": "1.19-2", "file": "x86_64/release/libiconv/libiconv2/libiconv2-1.19-2-x86_64.tar.xz", "bytes": 555664, "sha512": "a1b23e24e0a495e7163923bcf40a4b933403e0826cfc0d9bde785bc92c909b8c4fb94b1e6b2c0242f4328a723855e72ff3baab9f557ef855b7fe7a7d901b0943"}, + {"name": "libintl8", "version": "0.22.5-1", "file": "x86_64/release/gettext/libintl8/libintl8-0.22.5-1.tar.xz", "bytes": 43256, "sha512": "370a19af888bc7c9ed95cf8ade0d36d5e7cdb634abdebcba260cad61fa9a4cc05b5e748e315c9709f66861e8756b227d6757d529846591189e19ee2b2d00002d"}, + {"name": "liblastlog2", "version": "2.40.2-2", "file": "x86_64/release/util-linux/liblastlog2/liblastlog2-2.40.2-2.tar.xz", "bytes": 4908, "sha512": "89350921fe76c9065662dd9d72dd464d2f404f1690e806210cbb828ae8325ea5f3f9909d8eb04c8342b80417a4e1e22c3569241515762ad081ecd58d56927c02"}, + {"name": "liblz4_1", "version": "1.9.4-1", "file": "x86_64/release/lz4/liblz4_1/liblz4_1-1.9.4-1.tar.xz", "bytes": 53296, "sha512": "70e6b0f3af2c5e04089c955b46c13293ab935ee4683e4690da0cebfffba4ba91614004aede99dd804f54e4def93461159f55d151e7fbb647d1ca97ca29e619cc"}, + {"name": "liblzma5", "version": "5.8.3-1", "file": "x86_64/release/xz/liblzma5/liblzma5-5.8.3-1-x86_64.tar.zst", "bytes": 87321, "sha512": "7399c6d117f23788a7edb11cf7f5eec77724aad2bf7cf8a437001fc1515941f6ca415f42363981be35361290f43cd60cd04e15bb1b01cffc0fd275f3167037e0"}, + {"name": "libmpfr6", "version": "4.2.2-1", "file": "x86_64/release/mpfr/libmpfr6/libmpfr6-4.2.2-1.tar.zst", "bytes": 284416, "sha512": "8203dc61bf646041c71f7fece9fe59422ded96ac28dd24826ffb00a9607f274f3226673f9bee441f32b424a7e26c2f178281bd7836db26b0253d37da208d06a8"}, + {"name": "libncursesw10", "version": "6.5+20240427-1", "file": "x86_64/release/ncurses/libncursesw10/libncursesw10-6.5+20240427-1.tar.xz", "bytes": 393928, "sha512": "08102f7a7083cb5663fecc46d819f2ece4123444a8f94c42300f03f7eebd40395f556c44b22ab58bf57321b44d9b8fb8b19358e535c7500d36e52d48b05dcca8"}, + {"name": "libp11-kit0", "version": "0.26.5-1", "file": "x86_64/release/p11-kit/libp11-kit0/libp11-kit0-0.26.5-1-x86_64.tar.xz", "bytes": 212700, "sha512": "dd64f129fe71233e26e0845b6711db5cf2eb2e6d2d050f07b6aec0d9dd54d980e87390614744fbf022cf57aa2b3f7edbdc30c548c2f9d480188c22bcde0c2eba"}, + {"name": "libpcre1", "version": "8.45-1", "file": "x86_64/release/pcre/libpcre1/libpcre1-8.45-1.tar.zst", "bytes": 166711, "sha512": "85c793ed3622a00b811773b94c45376882513e267b9f1d2a3efa0bc42ff86a4e4bebd8362a193d302e66ae9e5c9b93befc1bb1096c0fd397e2cd0f5632c808dd"}, + {"name": "libpcre2_8_0", "version": "10.47-1", "file": "x86_64/release/pcre2/libpcre2_8_0/libpcre2_8_0-10.47-1-x86_64.tar.zst", "bytes": 237134, "sha512": "a98b38e1591c19404a5794cef488810747d86cc27ce5ee326e706cfa7069cf088d8a64571933f4c1f7a611fd8c232008837271bebe2e81c04a3f6ec571f0009e"}, + {"name": "libpipeline1", "version": "1.5.8-1", "file": "x86_64/release/libpipeline/libpipeline1/libpipeline1-1.5.8-1-x86_64.tar.xz", "bytes": 24880, "sha512": "1d062ac4909d37fbac3ce66b07ac691df0ae5acbd01f08c5019a5ed897cb2bbef95f9e347c5bc71185ae3317d89383306e46f2c59acae5d28a18fa65f66d4e80"}, + {"name": "libpopt-common", "version": "1.19-1", "file": "x86_64/release/popt/libpopt-common/libpopt-common-1.19-1.tar.xz", "bytes": 18536, "sha512": "4d69e6d7e196cdb4caad558c5d7543f706a4c6d7053476be861be07672506e378084126f22903deab8113557f757d2a07578f4a790ae316a2ea50ba612148a43"}, + {"name": "libpopt0", "version": "1.19-1", "file": "x86_64/release/popt/libpopt0/libpopt0-1.19-1.tar.xz", "bytes": 18748, "sha512": "e820a7bac13cdd3bff07673ac54dcb80f342c6a32917035f218ef255bdb707d654ee6a1684242d7ce8439eebf050ade6566f27f5118896fbf8e0171173b5bfee"}, + {"name": "libreadline7", "version": "8.2-2", "file": "x86_64/release/readline/libreadline7/libreadline7-8.2-2.tar.xz", "bytes": 113340, "sha512": "1d27576cf49b056f35f90ad933e39c5d946bc271ddcd109daa98f95b97123b428256827889ff45ca8a76409cdb1afd0146e99c9b70150b302538356512acfe38"}, + {"name": "libreadline8", "version": "8.3-1", "file": "x86_64/release/readline/libreadline8/libreadline8-8.3-1-x86_64.tar.xz", "bytes": 120084, "sha512": "64142c5f305b9836c5be9a4271e4163d8b3e739a0451c0d152373eeb3338342f7cf5691e4437b8e6d0a8404c30ea7a21832e342e7a1ca3b5f3e8e3403051b96e"}, + {"name": "libsmartcols1", "version": "2.40.2-2", "file": "x86_64/release/util-linux/libsmartcols1/libsmartcols1-2.40.2-2.tar.xz", "bytes": 96124, "sha512": "71ded6f02d264343d068b90450d96f4fdeb4087f278b990709aaa814c16123f4ae947714e5f10020260c30a6d78865c2d35d9ffe57a51b038815db52c3274317"}, + {"name": "libsqlite3_0", "version": "3.49.1-1", "file": "x86_64/release/sqlite3/libsqlite3_0/libsqlite3_0-3.49.1-1.tar.xz", "bytes": 686212, "sha512": "cd81686fe5778a94ab91e4aebdd2a229cb963318ce7b393e0a0299e29ed7648c24135733c1978cbebbb926813d7f994b9e38318ede12c72fe79ad404dbfdeaf2"}, + {"name": "libssl3", "version": "3.5.7-1", "file": "x86_64/release/openssl/libssl3/libssl3-3.5.7-1-x86_64.tar.zst", "bytes": 2081790, "sha512": "18161f1292404554742ced4beaa10548c7e1bbfb9398ec0b733e632401dfaa49c33336ad4b1e80e2b7bd77c20f00c10196c5804c327e37f641d49da2288837b8"}, + {"name": "libstdc++6", "version": "14.4.0-1", "file": "x86_64/release/gcc/libstdc++6/libstdc++6-14.4.0-1-x86_64.tar.zst", "bytes": 651767, "sha512": "3d86409193c952618eee7e2dea32bdeb97f22ee800e13cc46f98b5095d4d0b1b31ac4336dc5819abb2d553ad1c2151619e9f6f337dfe921ba80fda1323f6f4e3"}, + {"name": "libtasn1_6", "version": "4.21.0-1", "file": "x86_64/release/libtasn1/libtasn1_6/libtasn1_6-4.21.0-1-x86_64.tar.xz", "bytes": 32072, "sha512": "d28713b27d5d41408b29caed785e29380da4d8b4ccdc1c479ae13906e6765bedc6d6b9e6c1c22fd2111d678fcf1663187d3de27d7ad2e104cd48c38086189e36"}, + {"name": "libuchardet0", "version": "0.0.8-1", "file": "x86_64/release/uchardet/libuchardet0/libuchardet0-0.0.8-1.tar.zst", "bytes": 72637, "sha512": "1fad5711590a82a8386c2492286dd9c0ad7cbe086aab249d07ad27277757327a6a67264e9718aef7d30307515462fe67d64f23582f08f0ced85a19cc8e0afe23"}, + {"name": "libunistring-devel", "version": "1.4.1-1", "file": "x86_64/release/libunistring/libunistring-devel/libunistring-devel-1.4.1-1-x86_64.tar.xz", "bytes": 43332, "sha512": "14e067719e5c0a973950955a05685c18d6fa97df5defa8d0915125ea3af1d01ac921c2a84565a49cc7fb785b15a8be4640ee209f3f0d2e13cb542eb6c75b51f9"}, + {"name": "libunistring5", "version": "1.4.1-1", "file": "x86_64/release/libunistring/libunistring5/libunistring5-1.4.1-1-x86_64.tar.xz", "bytes": 498220, "sha512": "cbd454f5068bfb9291d440a3b03b0c58437aa0342a199007d5cce6fa006b6b5ea4d3ba10c136fb0257ffb0d0199db73484275c632506e4ec31c81e1746667b80"}, + {"name": "libuuid1", "version": "2.40.2-2", "file": "x86_64/release/util-linux/libuuid1/libuuid1-2.40.2-2.tar.xz", "bytes": 12532, "sha512": "3ba0c5d5a401c88796077e9f45164532408882f2f5d9f0ab99b2fdf6eb915917250124cde3653a7c53b648866e091aa3baf3304d0b243337f0d4e254a7631f55"}, + {"name": "libzstd1", "version": "1.5.7-1", "file": "x86_64/release/zstd/libzstd1/libzstd1-1.5.7-1.tar.zst", "bytes": 253786, "sha512": "f89cdaa004487ec8c4796b8f56887c81f6f497873c1495b9cb585ffe6c8d6e6b8ad4b52617d5238075f965f1fe001da287d9a00d121590274b2d87672b415a0f"}, + {"name": "login", "version": "1.13-1", "file": "x86_64/release/login/login-1.13-1.tar.xz", "bytes": 17264, "sha512": "66acc3255962a27af8d701a8c02846f3b9180b7b1d65d946f6fb27cd24aeedc8e6a9a2601b419e981c94b356ef376a5c565ea31a4a220e5add9d70ae501a11d1"}, + {"name": "m4", "version": "1.4.21-1", "file": "x86_64/release/m4/m4-1.4.21-1-x86_64.tar.xz", "bytes": 363204, "sha512": "0d86c88d4bbbfc59e136bba3101ca3714139fba2ab7fea2883bc773ca884c3876f8a8f3a2ea8b05c29fd5280c1fb2d686401346f2761160917edf0818a2b232b"}, + {"name": "make", "version": "4.4.1-2", "file": "x86_64/release/make/make-4.4.1-2.tar.xz", "bytes": 598004, "sha512": "9a89a0f4ceadf5c2d0390029d16274fbed0190d2410ad6beb76e39b7153f38d0d1169ad0abf57c96929e6ab0523a0a76c345868e9ae46f9cd3d708d23f6fa3e6"}, + {"name": "man-db", "version": "2.13.1-1", "file": "x86_64/release/man-db/man-db-2.13.1-1-x86_64.tar.zst", "bytes": 1301518, "sha512": "a1e8a0d6d5a75465e32be83c13e685a73f084fa41abc5500d9437837a2c9de2f897827d3516c1076fa6cdd07a3d1aaaa4751f3b393fcfe64c370ff8f6b8b7f84"}, + {"name": "mintty", "version": "3.8.3-1", "file": "x86_64/release/mintty/mintty-3.8.3-1-x86_64.tar.xz", "bytes": 1190356, "sha512": "79068708c81a7bf6abba3ad1cd0550a2c3eb6f6eea21adade8b0d2b77339cd2519716f528159da9f0972f93bb624867e394d782c3e3b0b7e841a8db985395b92"}, + {"name": "ncurses", "version": "6.5+20240427-1", "file": "x86_64/release/ncurses/ncurses-6.5+20240427-1.tar.xz", "bytes": 111396, "sha512": "e15664bdab3f4d82bac6481deaa66476adbe34404937079983b5165930fa18cec3aa0345b93fbb2c390f8304ef20f041cd47d268552b92d01a51adb6abde0fc5"}, + {"name": "openssl", "version": "3.5.7-1", "file": "x86_64/release/openssl/openssl-3.5.7-1-x86_64.tar.zst", "bytes": 1392875, "sha512": "c50821b61eec47d58d1624a4a3d5a4013c80a8b48cdc803d81a656e6859079d321b34fa0ff97db4c093735a76b71a68c8a7f12184b2c4f0677014e910d1ba9d3"}, + {"name": "p11-kit", "version": "0.26.5-1", "file": "x86_64/release/p11-kit/p11-kit-0.26.5-1-x86_64.tar.xz", "bytes": 332724, "sha512": "1f057de789a5553f04649c80e6f78385135b57de7ba078e9dd51f2db60afee37717bb6de9dcf0a330cf72fce1359e08c0186988632b5f858992ebf95739bb840"}, + {"name": "p11-kit-trust", "version": "0.26.5-1", "file": "x86_64/release/p11-kit/p11-kit-trust/p11-kit-trust-0.26.5-1-x86_64.tar.xz", "bytes": 110096, "sha512": "2f98ae11e42f69fa4626638f7cefe2ba2fed808ae618093c1e6c568f8a8be79e6690b40e39e20df02a8d363c371cca8b8320fea7b288e8561cd299f15cc5b796"}, + {"name": "perl", "version": "5.44.0-1", "file": "x86_64/release/perl/perl-5.44.0-1-x86_64.tar.zst", "bytes": 5129424, "sha512": "42f80064ff94ea778319a24e011d4a7236944c79d9db22e0ed64a7ca7d9b65bc2df5998130eb64c50aa95c173937776cc42665174c5717e84fe6dca523368f27"}, + {"name": "perl-Algorithm-Diff", "version": "1.2010-5", "file": "noarch/release/perl-Algorithm-Diff/perl-Algorithm-Diff-1.2010-5-noarch.tar.zst", "bytes": 31580, "sha512": "d840b605043448ceab8ded77313e12653c81fd7db08062a99010fb19b334429c15628b1a0a805f88110e7aae4dcd3f667b515c18870ba873169533663f4374de"}, + {"name": "perl-Archive-Zip", "version": "1.680.0-1", "file": "noarch/release/perl-Archive-Zip/perl-Archive-Zip-1.680.0-1-noarch.tar.zst", "bytes": 85917, "sha512": "de23da6dac8aa8c8218c141c845723d092f6eaf2f1e67ced96d7b0385206c2e335b150bb7066bd0c7d257125ad1b3fc7691686dc535c56429b4a0f4535b6c73a"}, + {"name": "perl-Class-Inspector", "version": "1.360.0-1", "file": "noarch/release/perl-Class-Inspector/perl-Class-Inspector-1.360.0-1-noarch.tar.zst", "bytes": 21261, "sha512": "299489c9d0bc86fe8ba625c0ec0530f0e6ecacab3f67b2826760413f6bc58252c6c11f45583063cfcf0944f9127ed2bece9168fd76220e735841f374a57943f5"}, + {"name": "perl-Devel-Cycle", "version": "1.120.0-1", "file": "noarch/release/perl-Devel-Cycle/perl-Devel-Cycle-1.120.0-1-noarch.tar.zst", "bytes": 8884, "sha512": "308211af5b57106629b168be340fd682e23670fd97cc7c7f80c3bac48223984c1a226148c99cf5d904b09e352f1008f13bd4645897223a162a22af819aa7af11"}, + {"name": "perl-File-ShareDir", "version": "1.118.0-1", "file": "noarch/release/perl-File-ShareDir/perl-File-ShareDir-1.118.0-1-noarch.tar.zst", "bytes": 19857, "sha512": "7cb0f5fe4c327e9f870fc9c303f5e7e1483d2d2d012dbb5487f4b08f350170b7eb0a345766bd0a809bb4b8fa138788112eed2528b92718e817a5f21a9b82774d"}, + {"name": "perl-Text-Diff", "version": "1.450.0-1", "file": "noarch/release/perl-Text-Diff/perl-Text-Diff-1.450.0-1-noarch.tar.zst", "bytes": 30982, "sha512": "c19c80564d50d1d8efd44b156013aa5620b79205fd291897b938735c49214c84867a1337dfe9e7857f97e6f924ec919f5a0197df2d3bfeae78e0ec9dc8b9a6ac"}, + {"name": "perl-Win32-API", "version": "0.840.0-1", "file": "x86_64/release/perl-Win32-API/perl-Win32-API-0.840.0-1-x86_64.tar.zst", "bytes": 84514, "sha512": "758b1881d55942e91365780bede1dca63838995ad6574f9a242c15f613e7e3211a029e9bd3a3bd24d834ca6838e7a85c2812411783423ba23ccf808d094b6890"}, + {"name": "perl_autorebase", "version": "5.44.0-1", "file": "x86_64/release/perl/perl_autorebase/perl_autorebase-5.44.0-1-x86_64.tar.zst", "bytes": 175, "sha512": "55cd94604cfa3e2491df45e5a3715f32b2dee8298a656ca2da8de2eba4096329c595228a8c950760e9641b2c6097309571350c0ffe1c8d13759eb76ec841a4f8"}, + {"name": "perl_base", "version": "5.44.0-1", "file": "x86_64/release/perl/perl_base/perl_base-5.44.0-1-x86_64.tar.zst", "bytes": 3772189, "sha512": "3039e7a60c950cd682d8372f948ec83f7ebea27e9b425ab7406ed2eff0bebb80f7622423cf82b9ab07e34deb183bdf1b56fe129c8306bd2cfb04e7cd96beb992"}, + {"name": "rebase", "version": "4.6.6-1", "file": "x86_64/release/rebase/rebase-4.6.6-1.tar.xz", "bytes": 248832, "sha512": "9f3751c4687b09f3c286cf5c4522a0d976fce2d79ab471ab37560b15f14af84f7d13162b6102c02d0d60784448ff0586e76e9291675af4fa0c9320a3713633da"}, + {"name": "run", "version": "1.3.4-2", "file": "x86_64/release/run/run-1.3.4-2.tar.xz", "bytes": 34492, "sha512": "7e3156bbddc5f934fde3530879aa5186c9fdccb757ba848fc958e79e96534b336975f2882e413d5c18174628033a422157d25c48f8b5e90f0f1555ff2fa6343f"}, + {"name": "sed", "version": "4.10-1", "file": "x86_64/release/sed/sed-4.10-1-x86_64.tar.xz", "bytes": 359252, "sha512": "da5475344a1693f90fb560773ccca48eaf13af0e6bbcada9dcea658862bf7f46043b2d7365de0e2e235fb9a874fe784ad83bc29eb2fdbf3207cd8ad5e127b8b9"}, + {"name": "tar", "version": "1.35-2", "file": "x86_64/release/tar/tar-1.35-2.tar.zst", "bytes": 997616, "sha512": "007c7df12bc27ca14862a925680e2b2ff616a5149262b153889e6f580f3db492f98da49b5f34a8c4234194ee25021b67e9a84c987c96c5d6baa7ee0b3ea02425"}, + {"name": "terminfo", "version": "6.5+20240427-1", "file": "x86_64/release/ncurses/terminfo/terminfo-6.5+20240427-1.tar.xz", "bytes": 94760, "sha512": "128fb04f188bbf2af3060416193c9879a9cbbf7e5486febf851f35c3d9d00b9d61a0e2bf4be575d2308fd665a224ce5674485dcfd071125c3a8d199a4b88a042"}, + {"name": "texinfo", "version": "7.3-1", "file": "x86_64/release/texinfo/texinfo-7.3-1-x86_64.tar.zst", "bytes": 1853455, "sha512": "a7a0da042d05bbf2978857fcad1887552db9d77bf745fb8a17e156a2616500cf711adce107610f008ddff5556d02823f148d2bf146bd7eaa8a9466f9300cf2e6"}, + {"name": "tzcode", "version": "2026c-1", "file": "x86_64/release/tzcode/tzcode-2026c-1-x86_64.tar.xz", "bytes": 185512, "sha512": "85ab0f5d8d7c02e38dbd945c99ee6841774d84c486f857cd8484605068441b2028eea1f42a7e36c8fd6801c454f8f786a16f9caeb422afb30e5fcf324434878b"}, + {"name": "tzdata", "version": "2026c-1", "file": "noarch/release/tzdata/tzdata-2026c-1-noarch.tar.xz", "bytes": 184092, "sha512": "567adb6da01050a84b3785a1cb518e62a9c838cfe55e8ab33610c6ee64355677486329de5781361d08f03cd355acd64f94c64ec2a2686d48538813cc405b638c"}, + {"name": "util-linux", "version": "2.40.2-2", "file": "x86_64/release/util-linux/util-linux-2.40.2-2.tar.xz", "bytes": 2053568, "sha512": "4f3473472e32bff586401ec31f1423237c9cb112ae1140cdd8b2797040c5c182632f49f5354eaaa409d2dacba7767d5baa8b8926d7638e2725ad80065b46c10b"}, + {"name": "vim-minimal", "version": "9.1.1825-1", "file": "x86_64/release/vim/vim-minimal/vim-minimal-9.1.1825-1-x86_64.tar.xz", "bytes": 794064, "sha512": "6a252210e2ef08a7038d6e5bc8e5312e0c25d8328ab0aefd7f292eb79b7a32dd699f15f3f293cfbb83687e87cb3f68a335e969b117f55e6ff99b34ebdfbc8119"}, + {"name": "which", "version": "2.23-1", "file": "x86_64/release/which/which-2.23-1-x86_64.tar.xz", "bytes": 31108, "sha512": "98dffe802e1137d7b54359314797fd10080e6c261f37668e32e46aa7b1587b4148a1647b4f4a00cd0d23a9e90f153d5c2081d0ffa4ccea657b73aaa31b538d6e"}, + {"name": "xz", "version": "5.8.3-1", "file": "x86_64/release/xz/xz-5.8.3-1-x86_64.tar.zst", "bytes": 767298, "sha512": "8918389508ade8237226170b6dd02104938ec53b2bb15b310f5005dd64f954dfd67a5391757ba8602afe2a423f901894204771d6ae335402095c074d098daa76"}, + {"name": "zlib0", "version": "1.3.2-1", "file": "x86_64/release/zlib/zlib0/zlib0-1.3.2-1-x86_64.tar.zst", "bytes": 46254, "sha512": "d79ba6098cadf6bf2f32f35d9cd54729293c64b3c9daabb9d080f52861a19db71544d5710fdbd61ff45e7c2ae4eecb3f2e3b8a1e3bc05695b8cfe01c7639c53a"}, + {"name": "zstd", "version": "1.5.7-1", "file": "x86_64/release/zstd/zstd-1.5.7-1.tar.zst", "bytes": 394600, "sha512": "5de9a2fdee75345a35592444d467a1b70504f09f3b2ee5aaf8af8a9d9fda1a4e77d37c5c4c4fee9b96995ef78f9708e6c02adea7a70d9f80f1238e507b2bc0fc"} + ] +} diff --git a/.github/scripts/voice_windows_tools.py b/.github/scripts/voice_windows_tools.py new file mode 100644 index 0000000000..7dc26ee3fc --- /dev/null +++ b/.github/scripts/voice_windows_tools.py @@ -0,0 +1,80 @@ +"""Expose verified Windows build-tool installations as local Bazel inputs. + +This declares the installed support trees; the caller authenticates their +inputs and verifies the installed package inventory first. +""" + +import argparse +import itertools +import json +from pathlib import Path +import stat + + +def export_repository(root: Path, target: str, pkg_config: Path): + if target not in ("x86_64-pc-windows-msvc", "aarch64-pc-windows-msvc"): + raise ValueError("unsupported Windows target") + root = root.resolve(strict=True) + pkg_config = pkg_config.resolve(strict=True) + names = { + "shell": "cygwin/bin/bash.exe", + "make": "cygwin/bin/make.exe", + "cygpath": "cygwin/bin/cygpath.exe", + "automake": "cygwin/bin/automake-1.18", + "pkg_config": pkg_config.relative_to(root).as_posix(), + } + if not names["pkg_config"].startswith("pkgconf-image/"): + raise ValueError("pkg-config must belong to the extracted image") + outputs = ("BUILD.bazel", "MODULE.bazel", "voice-tools.json") + if any((root / name).exists() for name in outputs): + raise ValueError("Bazel tool repository outputs must be fresh") + # A nested package boundary would silently remove files from Bazel's glob. + # Reparse points could escape the installation or hide support directories. + for directory in ("cygwin", "pkgconf-image"): + tree = root / directory + if not tree.is_dir(): + raise ValueError(f"missing tool tree: {directory}") + for path in itertools.chain((tree,), tree.rglob("*")): + info = path.lstat() + if ( + stat.S_ISLNK(info.st_mode) + or getattr(info, "st_file_attributes", 0) + & stat.FILE_ATTRIBUTE_REPARSE_POINT + or not (stat.S_ISDIR(info.st_mode) or stat.S_ISREG(info.st_mode)) + or path.name.casefold() in ("build", "build.bazel") + ): + raise ValueError(f"unsupported tool-tree entry: {path}") + if any(not (root / name).is_file() for name in names.values()): + raise ValueError("required Windows build tool missing") + definitions = [ + 'package(default_visibility = ["//visibility:public"])', + 'filegroup(name = "cygwin", srcs = glob(["cygwin/**"], allow_empty = False))', + 'filegroup(name = "pkgconf", srcs = glob(["pkgconf-image/**"], allow_empty = False))', + 'filegroup(name = "tools", srcs = [":cygwin", ":pkgconf", "voice-tools.json"])', + ] + definitions.extend( + f"filegroup(name = {json.dumps(name)}, srcs = [{json.dumps(path)}])" + for name, path in names.items() + ) + metadata = { + "schemaVersion": 1, + "target": target, + "cygwinArchitecture": "x86_64", + "tools": names, + } + for name, contents in ( + ("BUILD.bazel", "\n".join(definitions) + "\n"), + ("MODULE.bazel", 'module(name = "voice_windows_tools")\n'), + ("voice-tools.json", json.dumps(metadata, indent=2) + "\n"), + ): + with (root / name).open("x", encoding="utf-8") as output: + output.write(contents) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--root", type=Path, required=True) + parser.add_argument("--target", required=True) + parser.add_argument("--pkg-config", type=Path, required=True) + args = parser.parse_args() + export_repository(args.root, args.target, args.pkg_config) diff --git a/.github/scripts/watch_voice_bazel.py b/.github/scripts/watch_voice_bazel.py new file mode 100644 index 0000000000..d27b7a2664 --- /dev/null +++ b/.github/scripts/watch_voice_bazel.py @@ -0,0 +1,79 @@ +"""Capture one bounded diagnostic when the ARM64 voice build goes silent.""" + +import os +from pathlib import Path +import shutil +import subprocess +import sys +import threading +import time + + +def diagnose(): + root = Path(os.environ.get("BAZEL_OUTPUT_BASE", "")) + if not root.is_absolute(): + print("Bazel output base unavailable; skipping diagnostics", flush=True) + return + for relative in ("command.log", "server/jvm.out"): + try: + with (root / relative).open("rb") as source: + source.seek(max(0, source.seek(0, 2) - 16384)) + print( + f"Bazel {relative} tail:\n{source.read(16384).decode(errors='replace')}", + flush=True, + ) + except OSError as error: + print(f"Cannot read {relative}: {error}", flush=True) + try: + pid = str(int((root / "server/server.pid.txt").read_text().strip())) + jstack = shutil.which("jstack") + if not jstack: + print("jstack unavailable; log tails retained", flush=True) + return + result = subprocess.run([jstack, pid], capture_output=True, timeout=20) + print( + f"jstack exit {result.returncode}:\n{(result.stdout + result.stderr)[-65536:].decode(errors='replace')}", + flush=True, + ) + except (OSError, ValueError, subprocess.TimeoutExpired) as error: + print(f"Cannot capture JVM stacks: {error}", flush=True) + + +def main(): + with subprocess.Popen( + sys.argv[1:], stdout=subprocess.PIPE, stderr=subprocess.STDOUT + ) as process: + last_output = time.monotonic() + + def forward(): + nonlocal last_output + for line in process.stdout: + last_output = time.monotonic() + sys.stdout.buffer.write(line) + sys.stdout.buffer.flush() + + reader = threading.Thread(target=forward) + reader.start() + captured = False + while process.poll() is None: + if ( + not captured + and os.environ.get("VOICE_ARCH") == "aarch64" + and time.monotonic() - last_output >= 600 + ): + captured = True + print( + "ARM64 Bazel silent for ten minutes; capturing diagnostics", + flush=True, + ) + diagnose() + time.sleep(1) + reader.join() + if process.returncode and not captured: + print("Bazel command failed; capturing diagnostics", flush=True) + diagnose() + return process.returncode + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/workflows/rust-release-windows.yml b/.github/workflows/rust-release-windows.yml index e9a200ff0f..8ec85f3346 100644 --- a/.github/workflows/rust-release-windows.yml +++ b/.github/workflows/rust-release-windows.yml @@ -146,6 +146,134 @@ jobs: path: | ${{ env.CARGO_TARGET_DIR }}/${{ matrix.target }}/release/staged-${{ matrix.bundle }}/* + build-windows-voice: + name: Build Windows voice - ${{ matrix.target }} + runs-on: ${{ matrix.runs_on }} + timeout-minutes: 120 + permissions: + contents: read + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-pc-windows-msvc + arch: x86_64 + python_arch: x64 + runs_on: + group: ${{ github.event.repository.name }}-runners + labels: ${{ github.event.repository.name }}-windows-x64 + - target: aarch64-pc-windows-msvc + arch: aarch64 + python_arch: arm64 + runs_on: + group: ${{ github.event.repository.name }}-runners + labels: ${{ github.event.repository.name }}-windows-arm64 + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + persist-credentials: false + - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: "3.12" + architecture: ${{ matrix.python_arch }} + - name: Prepare Bazel CI + uses: ./.github/actions/prepare-bazel-ci + with: + target: ${{ matrix.target }} + cache-scope: release-voice-windows + - uses: ./.github/actions/setup-msvc-env + with: + target: ${{ matrix.target }} + - name: Refresh Bazel PATH for the selected MSVC target + shell: pwsh + run: ./.github/scripts/compute-bazel-windows-path.ps1 + - name: Download pinned Windows build inputs + shell: pwsh + env: + GH_TOKEN: ${{ github.token }} + run: | + $manifest = Get-Content -Raw .github/scripts/voice-cygwin-snapshot.json | ConvertFrom-Json + $pin = $manifest.archive + $source = $manifest.sourceArchive + $directory = Join-Path $env:RUNNER_TEMP "voice-cygwin-snapshot" + New-Item -ItemType Directory -Path $directory | Out-Null + gh release download $pin.tag --repo "${{ github.repository }}" --pattern $pin.name --pattern $source.name --dir $directory + if ($LASTEXITCODE -ne 0) { throw "Cannot obtain the public Cygwin inputs and source" } + $sourceFile = Join-Path $directory $source.name + if ((Get-Item $sourceFile).Length -ne $source.bytes -or + (Get-FileHash $sourceFile -Algorithm SHA256).Hash.ToLowerInvariant() -ne $source.sha256) { + throw "Public Cygwin source archive mismatch" + } + "VOICE_CYGWIN_ARCHIVE=$(Join-Path $directory $pin.name)" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append + - name: Verify and install pinned public build tools + shell: pwsh + run: | + & ./.github/scripts/setup-voice-windows.ps1 -Target "${{ matrix.target }}" -SnapshotArchive $env:VOICE_CYGWIN_ARCHIVE + if (-not $env:SystemRoot) { throw "Windows SystemRoot is required for native audio actions" } + "VOICE_WINDOWS_SYSTEM_ROOT=$env:SystemRoot" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append + $hostArch = $env:PROCESSOR_ARCHITEW6432 + if (-not $hostArch) { $hostArch = $env:PROCESSOR_ARCHITECTURE } + if (-not $hostArch) { throw "Windows host architecture is required" } + "VOICE_WINDOWS_HOST_ARCH=$hostArch" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append + - name: Build same-commit native runtime and helper + shell: bash + env: + ARCH: ${{ matrix.arch }} + VOICE_ARCH: ${{ matrix.arch }} + # Avoid the observed Java 25 C1 crash in this method on Windows ARM64. + JAVA_TOOL_OPTIONS: ${{ matrix.arch == 'aarch64' && '-XX:CompileCommand=exclude,net.starlark.java.eval.BuiltinFunction::getPositionalOnlyArgumentVector' || '' }} + BUILDBUDDY_API_KEY: "" + run: | + (cd codex-rs && cargo update --workspace) + python .github/scripts/watch_voice_bazel.py "$(cygpath -w "$BASH")" ./.github/scripts/run-bazel-ci.sh \ + --remote-download-toplevel \ + --print-failed-action-summary \ + --windows-msvc-host-platform \ + -- \ + test -c opt \ + --platforms=//:local_windows_msvc \ + --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows,@local_config_cc//:cc-toolchain-arm64_windows,//third_party/voice:windows_pkg_config_toolchain,//third_party/voice:windows_cmake_toolchain \ + --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=0 \ + --repo_env=BAZEL_MSVC_RUNTIME_VISUAL_STUDIO_EULA=1 \ + --inject_repository="voice_windows_tools=$VOICE_WINDOWS_BAZEL_REPOSITORY" \ + --//third_party/voice:windows_installed_tools=@voice_windows_tools//:tools \ + --action_env="SystemRoot=$VOICE_WINDOWS_SYSTEM_ROOT" \ + --host_action_env="SystemRoot=$VOICE_WINDOWS_SYSTEM_ROOT" \ + --action_env="PROCESSOR_ARCHITECTURE=$VOICE_WINDOWS_HOST_ARCH" \ + --host_action_env="PROCESSOR_ARCHITECTURE=$VOICE_WINDOWS_HOST_ARCH" \ + --output_groups=default,receipt,sdk \ + --workspace_status_command=./scripts/workspace-status.cmd \ + --build_metadata=COMMIT_SHA="$GITHUB_SHA" \ + -- \ + "//third_party/voice:native_runtime_windows_$ARCH" \ + @rules_rust//cargo/private/cargo_build_script_runner:test \ + //codex-rs/voice-host:codex-voice-host + - name: Stage verified voice build output + shell: bash + env: + TARGET: ${{ matrix.target }} + ARCH: ${{ matrix.arch }} + run: | + set -euo pipefail + runtime="bazel-bin/third_party/voice/native_runtime_windows_${ARCH}" + PYTHONPATH=third_party/voice python - "$runtime" "$TARGET" <<'PY' + from pathlib import Path + import sys + from package_runtime import runtime_files + runtime_files(Path(sys.argv[1]).resolve(strict=True), sys.argv[2]) + PY + output="voice-unsigned/${TARGET}" + mkdir -p "$output" + cp -R "$runtime" "$output/runtime" + cp bazel-bin/codex-rs/voice-host/codex-voice-host.exe "$output/codex-voice-host.exe" + chmod -R u+w "$output" + tar -C "$output" -czf "voice-unsigned-${TARGET}.tar.gz" runtime codex-voice-host.exe + - uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 + with: + name: voice-${{ matrix.target }}-unsigned + path: voice-unsigned-${{ matrix.target }}.tar.gz + if-no-files-found: error + build-windows-symbols: needs: - build-windows-binaries @@ -193,12 +321,13 @@ jobs: build-windows: needs: - build-windows-binaries + - build-windows-voice name: Build - ${{ matrix.runner }} - ${{ matrix.target }} runs-on: ${{ matrix.runs_on }} environment: name: azure-artifact-signing deployment: false - timeout-minutes: 90 + timeout-minutes: 120 permissions: contents: read id-token: write @@ -265,6 +394,76 @@ jobs: account-name: ${{ secrets.AZURE_ARTIFACT_SIGNING_ACCOUNT_NAME }} certificate-profile-name: ${{ secrets.AZURE_ARTIFACT_SIGNING_CERTIFICATE_PROFILE_NAME }} + - name: Download matching native voice build + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + name: voice-${{ matrix.target }}-unsigned + path: ${{ runner.temp }}/unsigned-voice + + - name: Stage voice files for signing + id: voice_files + shell: bash + run: | + set -euo pipefail + target="${{ matrix.target }}" + unsigned="${RUNNER_TEMP}/unsigned-voice/extracted" + signed="${GITHUB_WORKSPACE}/signed-voice/${target}" + mkdir -p "$unsigned" "$signed" + tar -xzf "${RUNNER_TEMP}/unsigned-voice/voice-unsigned-${target}.tar.gz" -C "$unsigned" + python "${GITHUB_WORKSPACE}/third_party/voice/release_runtime.py" stage \ + --source "$unsigned/runtime" --target "$target" --output "$signed/runtime" + cp "$unsigned/codex-voice-host.exe" "$signed/codex-voice-host.exe" + chmod -R u+w "$signed" + { + echo 'files<> "$GITHUB_OUTPUT" + + - name: Sign Windows voice helper and native DLLs + uses: azure/trusted-signing-action@1d365fec12862c4aa68fcac418143d73f0cea293 # v0.5.11 + with: + endpoint: ${{ secrets.AZURE_ARTIFACT_SIGNING_ENDPOINT }} + trusted-signing-account-name: ${{ secrets.AZURE_ARTIFACT_SIGNING_ACCOUNT_NAME }} + certificate-profile-name: ${{ secrets.AZURE_ARTIFACT_SIGNING_CERTIFICATE_PROFILE_NAME }} + exclude-environment-credential: true + exclude-workload-identity-credential: true + exclude-managed-identity-credential: true + exclude-shared-token-cache-credential: true + exclude-visual-studio-credential: true + exclude-visual-studio-code-credential: true + exclude-azure-cli-credential: false + exclude-azure-powershell-credential: true + exclude-azure-developer-cli-credential: true + exclude-interactive-browser-credential: true + cache-dependencies: false + files: ${{ steps.voice_files.outputs.files }} + + - uses: ./.github/actions/setup-msvc-env + with: + target: ${{ matrix.target }} + + - name: Seal and verify signed voice output + shell: pwsh + run: | + $target = "${{ matrix.target }}" + $signed = Join-Path $env:GITHUB_WORKSPACE "signed-voice/$target" + python (Join-Path $env:GITHUB_WORKSPACE 'third_party/voice/windows_crt.py') --root (Join-Path $signed 'runtime') --target $target --helper (Join-Path $signed 'codex-voice-host.exe') + if ($LASTEXITCODE -ne 0) { throw 'Cannot stage verified Microsoft CRT' } + $files = @(Join-Path $signed "codex-voice-host.exe") + @( + Get-ChildItem (Join-Path $signed "runtime/bin") -Filter *.dll -File | + ForEach-Object FullName + ) + if ($files.Count -lt 8) { throw "Voice DLL closure is incomplete" } + foreach ($file in $files) { + $signature = Get-AuthenticodeSignature $file + if ($signature.Status -ne 'Valid') { throw "Unsigned voice file: $file ($($signature.Status))" } + } + python (Join-Path $env:GITHUB_WORKSPACE 'third_party/voice/release_runtime.py') seal --target $target --output (Join-Path $signed 'runtime') + if ($LASTEXITCODE -ne 0) { throw 'Cannot seal signed voice runtime' } + "VOICE_RELEASE_DIR=$signed" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append + - name: Stage artifacts shell: bash run: | @@ -282,22 +481,34 @@ jobs: set -euo pipefail target="${{ matrix.target }}" archive_script="${GITHUB_WORKSPACE}/.github/scripts/build-codex-package-archive.sh" - temp_root="${RUNNER_TEMP}/codex-package-archives" + voice_args=(--voice-release-dir "$VOICE_RELEASE_DIR") + voice_args+=(--release-version "${GITHUB_REF_NAME#rust-v}") + bash "$archive_script" \ + --target "$target" --bundle primary \ + --entrypoint-dir "$CARGO_TARGET_DIR/$target/release" \ + --archive-dir "dist/$target" "${voice_args[@]}" + bash "$archive_script" \ + --target "$target" --bundle app-server \ + --entrypoint-dir "$CARGO_TARGET_DIR/$target/release" \ + --archive-dir "dist/$target" - # The package helper rewrites cached DotSlash executables. Keep the - # concurrent processes in separate temp roots because Windows cannot - # replace an executable while another process is using it. - mkdir -p "$temp_root/primary" "$temp_root/app-server" - printf '%s\0' primary app-server | - xargs -0 -P0 -I{} env \ - TMPDIR="$temp_root/{}" \ - TMP="$temp_root/{}" \ - TEMP="$temp_root/{}" \ - bash "$archive_script" \ - --target "$target" \ - --bundle "{}" \ - --entrypoint-dir "$CARGO_TARGET_DIR/$target/release" \ - --archive-dir "dist/$target" + - name: Verify packaged Windows voice closure + shell: pwsh + run: | + $target = "${{ matrix.target }}" + $package = Join-Path $env:RUNNER_TEMP "verify-voice-archive-$target" + New-Item -ItemType Directory -Path $package | Out-Null + tar -xzf "dist/$target/codex-package-$target.tar.gz" -C $package + if ($LASTEXITCODE -ne 0) { throw "Cannot extract the final Windows package" } + $voice = Join-Path $package 'codex-resources/voice' + $helper = Join-Path $voice 'bin/codex-voice-host.exe' + if (-not (Test-Path $helper) -or -not (Test-Path (Join-Path $voice 'bin/gstreamer-1.0-0.dll'))) { + throw 'Signed Windows voice files are missing from the canonical package' + } + $signature = Get-AuthenticodeSignature $helper + if ($signature.Status -ne 'Valid') { throw "Packaged helper signature: $($signature.Status)" } + python -c 'import sys; from pathlib import Path; sys.path.insert(0, sys.argv[3]); from package_runtime import runtime_files; runtime_files(Path(sys.argv[1]), sys.argv[2], public_release=True)' $voice $target (Join-Path $env:GITHUB_WORKSPACE 'third_party/voice') + if ($LASTEXITCODE -ne 0) { throw 'Packaged runtime receipt verification failed' } - name: Build Python runtime wheel shell: bash @@ -320,12 +531,25 @@ jobs: python -m venv "${RUNNER_TEMP}/python-runtime-build-venv" "${RUNNER_TEMP}/python-runtime-build-venv/Scripts/python.exe" -m pip install build + # The wheel's Windows support floor predates the native voice + # runtime's verified device/VC++ prerequisites. Keep it voice-free. + wheel_archives="${RUNNER_TEMP}/voice-free-wheel/${{ matrix.target }}" + bash "${GITHUB_WORKSPACE}/.github/scripts/build-codex-package-archive.sh" \ + --target "${{ matrix.target }}" --bundle primary \ + --entrypoint-dir "$CARGO_TARGET_DIR/${{ matrix.target }}/release" \ + --archive-dir "$wheel_archives" + python - "$wheel_archives/codex-package-${{ matrix.target }}.tar.gz" <<'PY' + import sys + import tarfile + with tarfile.open(sys.argv[1]) as archive: + assert not any("codex-resources/voice/" in member.name for member in archive) + PY stage_dir="${RUNNER_TEMP}/openai-codex-cli-bin-${{ matrix.target }}" wheel_dir="${GITHUB_WORKSPACE}/python-runtime-dist/${{ matrix.target }}" python "${GITHUB_WORKSPACE}/sdk/python/scripts/update_sdk_artifacts.py" \ stage-runtime \ "$stage_dir" \ - "dist/${{ matrix.target }}/codex-package-${{ matrix.target }}.tar.gz" \ + "$wheel_archives/codex-package-${{ matrix.target }}.tar.gz" \ --codex-version "${GITHUB_REF_NAME}" \ --platform-tag "$platform_tag" "${RUNNER_TEMP}/python-runtime-build-venv/Scripts/python.exe" -m build --wheel --outdir "$wheel_dir" "$stage_dir" diff --git a/.github/workflows/rust-release.yml b/.github/workflows/rust-release.yml index d351371dfa..293c4bdf79 100644 --- a/.github/workflows/rust-release.yml +++ b/.github/workflows/rust-release.yml @@ -61,9 +61,8 @@ jobs: needs: tag-check name: Build - ${{ matrix.runner }} - ${{ matrix.target }} - ${{ matrix.bundle }} runs-on: ${{ matrix.runs_on || matrix.runner }} - # Release builds can take a long time, so leave some headroom to avoid - # having to restart the full workflow due to a timeout. - timeout-minutes: 90 + # Linux releases also build the native voice runtime in this job. + timeout-minutes: 120 permissions: contents: read id-token: write @@ -292,6 +291,37 @@ jobs: path: codex-rs/symbols-dist/${{ matrix.artifact_name }}/* if-no-files-found: error + - name: Set up Bazel for Linux voice + if: ${{ matrix.bundle == 'primary' && contains(matrix.target, 'linux') }} + uses: bazel-contrib/setup-bazel@c5acdfb288317d0b5c0bbd7a396a3dc868bb0f86 # 0.19.0 + with: + bazelisk-version: 1.28.1 + + - name: Build Linux voice runtime + if: ${{ matrix.bundle == 'primary' && contains(matrix.target, 'linux') }} + shell: bash + env: + APP_TARGET: ${{ matrix.target }} + run: | + set -euo pipefail + cd "$GITHUB_WORKSPACE" + voice_target="${APP_TARGET%-musl}-gnu" + case "$voice_target" in + aarch64-unknown-linux-gnu) prefix=linux_aarch64 ;; + x86_64-unknown-linux-gnu) prefix=linux_x86_64 ;; + *) exit 1 ;; + esac + bazel build -c opt //codex-rs/voice-host:codex-voice-host //third_party/voice:native_runtime + source="bazel-bin/third_party/voice/native_runtime_${prefix}" + output="${RUNNER_TEMP}/signed-voice/${APP_TARGET}" + mkdir -p "$output" + python3 third_party/voice/release_runtime.py stage \ + --target "$voice_target" --source "$source" --output "$output/runtime" + cp bazel-bin/codex-rs/voice-host/codex-voice-host "$output/codex-voice-host" + chmod 0755 "$output/codex-voice-host" + python3 third_party/voice/release_runtime.py seal \ + --target "$voice_target" --output "$output/runtime" + - if: ${{ runner.os == 'macOS' }} name: Stage unsigned macOS artifacts shell: bash @@ -376,12 +406,26 @@ jobs: BUNDLE: ${{ matrix.bundle }} run: | set -euo pipefail + voice_args=() + if [[ "$BUNDLE" == "primary" && "$TARGET" == *-unknown-linux-musl ]]; then + voice_args+=(--voice-release-dir "${RUNNER_TEMP}/signed-voice/${TARGET}") + voice_args+=(--release-version "${GITHUB_REF_NAME#rust-v}") + fi bash "${GITHUB_WORKSPACE}/.github/scripts/build-codex-package-archive.sh" \ --target "$TARGET" \ --bundle "$BUNDLE" \ --entrypoint-dir "target/${TARGET}/release" \ --archive-dir "dist/${TARGET}" \ - --zsh-manifest "${RUNNER_TEMP}/codex-zsh" + --zsh-manifest "${RUNNER_TEMP}/codex-zsh" \ + "${voice_args[@]}" + + - name: Cosign Linux voice package archives + if: ${{ matrix.bundle == 'primary' && contains(matrix.target, 'linux') }} + uses: ./.github/actions/linux-code-sign + with: + target: ${{ matrix.target }} + artifacts-dir: ${{ github.workspace }}/codex-rs/dist/${{ matrix.target }} + binaries: codex-package-${{ matrix.target }}.tar.gz codex-package-${{ matrix.target }}.tar.zst - name: Build Python runtime wheel if: ${{ matrix.bundle == 'primary' && runner.os != 'macOS' }} @@ -413,13 +457,31 @@ jobs: # the Homebrew Python as externally managed under PEP 668. "${RUNNER_TEMP}/python-runtime-build-venv/bin/python" -m pip install build + # Keep the existing manylinux_2_17 wheel compatible with older glibc. + # GNU voice libraries belong in the signed release package archives, + # but their minimum glibc version is not covered by this wheel tag. + wheel_archives="${RUNNER_TEMP}/voice-free-wheel/${{ matrix.target }}" + bash "${GITHUB_WORKSPACE}/.github/scripts/build-codex-package-archive.sh" \ + --target "${{ matrix.target }}" \ + --bundle primary \ + --entrypoint-dir "target/${{ matrix.target }}/release" \ + --archive-dir "$wheel_archives" \ + --zsh-manifest "${RUNNER_TEMP}/codex-zsh" + wheel_archive="${wheel_archives}/codex-package-${{ matrix.target }}.tar.gz" + python3 - "$wheel_archive" <<'PY' + import sys + import tarfile + with tarfile.open(sys.argv[1]) as archive: + assert not any("codex-resources/voice/" in item.name for item in archive) + PY + stage_dir="${RUNNER_TEMP}/openai-codex-cli-bin-${{ matrix.target }}" wheel_dir="${GITHUB_WORKSPACE}/python-runtime-dist/${{ matrix.target }}" stage_runtime_args=( "${GITHUB_WORKSPACE}/sdk/python/scripts/update_sdk_artifacts.py" stage-runtime "$stage_dir" - "dist/${{ matrix.target }}/codex-package-${{ matrix.target }}.tar.gz" + "$wheel_archive" --codex-version "${GITHUB_REF_NAME}" --platform-tag "$platform_tag" ) @@ -915,7 +977,7 @@ jobs: set -euo pipefail voice_args=() if [[ "$BUNDLE" == "primary" ]]; then - voice_args+=(--voice-signed-dir "${RUNNER_TEMP}/signed-voice/${TARGET}") + voice_args+=(--voice-release-dir "${RUNNER_TEMP}/signed-voice/${TARGET}") voice_args+=(--release-version "${GITHUB_REF_NAME#rust-v}") fi bash "${GITHUB_WORKSPACE}/.github/scripts/build-codex-package-archive.sh" \ diff --git a/MODULE.bazel b/MODULE.bazel index 6f74ef307b..7854947538 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -142,6 +142,11 @@ use_repo(osx, "macos_sdk") # Needed to disable xcode... bazel_dep(name = "apple_support", version = "2.1.0") bazel_dep(name = "rules_cc", version = "0.2.18") + +# Windows voice jobs explicitly select the installed MSVC C toolchains. +cc_configure = use_extension("@rules_cc//cc:extensions.bzl", "cc_configure_extension") +use_repo(cc_configure, "local_config_cc") + single_version_override( module_name = "rules_cc", patch_strip = 1, @@ -160,6 +165,7 @@ single_version_override( patches = [ "//patches:rules_foreign_cc_make_cppflags.patch", "//patches:rules_foreign_cc_make_xcompile.patch", + "//patches:rules_foreign_cc_make_msvc_stdint.patch", ], version = "0.15.1", ) @@ -203,6 +209,7 @@ rules_rust.patch( "//patches:rules_rust_windows_process_wrapper_skip_temp_outputs.patch", # Group build-script argument files to avoid Windows command-line limits. "//patches:rules_rust_group_build_script_arg_files.patch", + "//patches:rules_rust_windows_execroot_separators.patch", ], strip = 1, ) diff --git a/codex-rs/codex-home/src/instructions/mod.rs b/codex-rs/codex-home/src/instructions/mod.rs index a1952b26a3..aa3a67267f 100644 --- a/codex-rs/codex-home/src/instructions/mod.rs +++ b/codex-rs/codex-home/src/instructions/mod.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use std::sync::Mutex; use codex_extension_api::Instructions; -use codex_extension_api::LoadUserInstructionsFuture; +use codex_extension_api::LoadInstructionsFuture; use codex_extension_api::LoadedUserInstructions; use codex_extension_api::UserInstructionsProvider; use codex_utils_absolute_path::AbsolutePathBuf; @@ -70,7 +70,7 @@ impl CodexHomeUserInstructionsProvider { return LoadedUserInstructions { instructions: Some(Instructions { text: trimmed.to_string(), - source: path, + source: Some(path), }), warnings, }; @@ -84,7 +84,7 @@ impl CodexHomeUserInstructionsProvider { } impl UserInstructionsProvider for CodexHomeUserInstructionsProvider { - fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_> { + fn load_user_instructions(&self) -> LoadInstructionsFuture<'_> { Box::pin(async move { let mut loaded = self.load_from_codex_home().await; let mut state = self diff --git a/codex-rs/codex-home/src/instructions/tests.rs b/codex-rs/codex-home/src/instructions/tests.rs index bebe897851..bb3e9e50f5 100644 --- a/codex-rs/codex-home/src/instructions/tests.rs +++ b/codex-rs/codex-home/src/instructions/tests.rs @@ -27,8 +27,10 @@ fn expected( LoadedUserInstructions { instructions: Some(Instructions { text: text.to_string(), - source: AbsolutePathBuf::try_from(home.path().join(filename)) - .expect("absolute source path"), + source: Some( + AbsolutePathBuf::try_from(home.path().join(filename)) + .expect("absolute source path"), + ), }), warnings, } diff --git a/codex-rs/core-api/src/lib.rs b/codex-rs/core-api/src/lib.rs index ca2b92cb67..a8facc3ae1 100644 --- a/codex-rs/core-api/src/lib.rs +++ b/codex-rs/core-api/src/lib.rs @@ -85,8 +85,9 @@ pub use codex_exec_server::NoiseRendezvousConnectBundle; pub use codex_exec_server::NoiseRendezvousConnectProvider; pub use codex_extension_api::ExtensionRegistryBuilder; pub use codex_extension_api::Instructions; -pub use codex_extension_api::LoadUserInstructionsFuture; +pub use codex_extension_api::LoadInstructionsFuture; pub use codex_extension_api::LoadedUserInstructions; +pub use codex_extension_api::ThreadInstructionsProvider; pub use codex_extension_api::UserInstructionsProvider; pub use codex_extension_api::empty_extension_registry; pub use codex_features::Feature; diff --git a/codex-rs/core/src/agent/control/spawn.rs b/codex-rs/core/src/agent/control/spawn.rs index 306e770bcd..3da7924509 100644 --- a/codex-rs/core/src/agent/control/spawn.rs +++ b/codex-rs/core/src/agent/control/spawn.rs @@ -557,7 +557,17 @@ impl AgentControl { None, ) }; - // Reserving a slot can evict an idle nested parent. Keep its authority captured above. + let inherited_instructions = if let Some((parent, _)) = parent.as_ref() { + Some(parent.session.inherited_instructions().await) + } else if let Some(parent_thread_id) = parent_thread_id + && let Ok(parent) = state.get_thread(parent_thread_id).await + { + Some(parent.session.inherited_instructions().await) + } else { + None + }; + // Reserving a slot can evict an idle nested parent. Capture its instructions + // alongside its authority so the child does not depend on a later live lookup. let residency_slot = self .reserve_v2_residency_slot(&state, &config, Some(thread_id)) .await?; @@ -571,6 +581,7 @@ impl AgentControl { parent_thread_id, environment_selections, inherited_environments, + inherited_instructions, inherited_exec_policy, client_mcp_extensions, }) @@ -1277,6 +1288,7 @@ impl AgentControl { parent_thread_id, environment_selections: None, inherited_environments, + inherited_instructions: None, inherited_exec_policy, client_mcp_extensions: None, }) diff --git a/codex-rs/core/src/agent/control_tests.rs b/codex-rs/core/src/agent/control_tests.rs index f8cb0e5457..2981b13b0a 100644 --- a/codex-rs/core/src/agent/control_tests.rs +++ b/codex-rs/core/src/agent/control_tests.rs @@ -18,9 +18,15 @@ use crate::thread_manager::StartThreadOptions; use crate::tools::handlers::multi_agents_common::thread_spawn_source; use assert_matches::assert_matches; use codex_extension_api::ExtensionDataInit; +use codex_extension_api::Instructions; +use codex_extension_api::LoadInstructionsFuture; +use codex_extension_api::LoadedUserInstructions; +use codex_extension_api::ThreadInstructionsProvider; use codex_extension_api::empty_extension_registry; use codex_features::Feature; use codex_history::CompactedItem; +use codex_history::InitialHistory; +use codex_history::ResumedHistory; use codex_history::RolloutItem; use codex_login::AuthManager; use codex_login::CodexAuth; @@ -788,6 +794,16 @@ async fn check_v2_agent_reload(route: V2ReloadRoute) { .start_thread(StartThreadOptions { history_mode: Some(ThreadHistoryMode::Paginated), client_mcp_extensions: client_mcp_extensions.clone(), + user_instructions: Some(LoadedUserInstructions { + instructions: Some(Instructions { + text: "global instructions survive parent eviction".to_string(), + source: None, + }), + warnings: Vec::new(), + }), + thread_instructions_provider: Some(Arc::new(StaticThreadInstructionsProvider( + "thread instructions survive parent eviction", + ))), ..StartThreadOptions::new(harness.config.clone()) }) .await @@ -811,6 +827,9 @@ async fn check_v2_agent_reload(route: V2ReloadRoute) { } }; let parent_thread_id = parent_thread.session.thread_id; + let inherited_instructions = parent_thread.session.inherited_instructions().await; + assert!(inherited_instructions.user.is_some()); + assert!(inherited_instructions.thread.is_some()); let mut child_config = harness.config.clone(); child_config.model = Some("gpt-5.6-luna".to_string()); let spawned_agent = @@ -910,6 +929,12 @@ async fn check_v2_agent_reload(route: V2ReloadRoute) { .get_thread(spawned_agent.thread_id) .await .expect("reloaded child thread should exist"); + let reloaded_instructions = reloaded_child.session.inherited_instructions().await; + assert_eq!( + (reloaded_instructions.user, reloaded_instructions.thread), + (inherited_instructions.user, inherited_instructions.thread), + "reloading a child must retain both parent snapshots, even if residency evicts the parent", + ); if matches!(route, V2ReloadRoute::NestedParent) { let reloaded_turn = reloaded_child.session.new_default_turn().await; assert_eq!( @@ -1104,6 +1129,120 @@ async fn resume_agent_from_rollout_does_not_reopen_v2_descendants() { assert_thread_not_loaded(&resumed_manager, sibling_thread_id).await; } +struct StaticThreadInstructionsProvider(&'static str); + +impl ThreadInstructionsProvider for StaticThreadInstructionsProvider { + fn load_thread_instructions(&self) -> LoadInstructionsFuture<'_> { + let instructions = Instructions { + text: self.0.to_string(), + source: None, + }; + Box::pin(async move { + LoadedUserInstructions { + instructions: Some(instructions), + warnings: Vec::new(), + } + }) + } +} + +#[tokio::test] +async fn cold_resume_with_thread_instructions_preserves_lazy_v2_child_inheritance() { + let (home, mut config) = test_config().await; + let _ = config.features.enable(Feature::MultiAgentV2); + let _ = config.features.enable(Feature::Sqlite); + let harness = AgentControlHarness::new_with_config(home, config).await; + let parent = harness + .manager + .start_thread(StartThreadOptions { + thread_instructions_provider: Some(Arc::new(StaticThreadInstructionsProvider( + "initial thread instructions", + ))), + ..StartThreadOptions::new(harness.config.clone()) + }) + .await + .expect("start parent with thread instructions"); + let parent_thread_id = parent.thread_id; + let control = &parent.thread.session.services.agent_control; + let worker_thread_id = + spawn_v2_reload_test_child(control, harness.config.clone(), &parent.thread, "worker") + .await + .thread_id; + let worker = harness + .manager + .get_thread(worker_thread_id) + .await + .expect("worker should be loaded"); + persist_thread_for_tree_resume(&parent.thread, "parent persisted").await; + persist_thread_for_tree_resume(&worker, "worker persisted").await; + wait_for_live_thread_spawn_children(control, parent_thread_id, &[worker_thread_id]).await; + + let stored_parent = parent + .thread + .read_thread( + /*include_archived*/ true, /*include_history*/ true, + ) + .await + .expect("read parent history"); + let initial_history = InitialHistory::Resumed(ResumedHistory { + conversation_id: parent_thread_id, + history: Arc::new(stored_parent.history.expect("parent history").items), + rollout_path: stored_parent.rollout_path, + }); + let report = harness + .manager + .shutdown_all_threads_bounded(Duration::from_secs(5)) + .await; + assert_eq!(report.submit_failed, Vec::::new()); + assert_eq!(report.timed_out, Vec::::new()); + + let resumed_manager = ThreadManager::with_models_provider_home_and_state_for_tests( + CodexAuth::from_api_key("dummy"), + harness.config.model_provider.clone(), + harness.config.codex_home.to_path_buf(), + Arc::new(codex_exec_server::EnvironmentManager::default_for_tests()), + harness.state_db.clone(), + ); + let resumed_parent = resumed_manager + .start_thread(StartThreadOptions { + initial_history, + session_source: Some(SessionSource::Exec), + thread_instructions_provider: Some(Arc::new(StaticThreadInstructionsProvider( + "resumed thread instructions", + ))), + ..StartThreadOptions::new(harness.config.clone()) + }) + .await + .expect("cold resume parent with updated thread instructions"); + let thread_instructions = Instructions { + text: "resumed thread instructions".to_string(), + source: None, + }; + assert_eq!( + resumed_parent + .thread + .session + .inherited_instructions() + .await + .thread, + Some(thread_instructions.clone()), + ); + assert_thread_not_loaded(&resumed_manager, worker_thread_id).await; + + resumed_manager + .ensure_multi_agent_v2_child_loaded(worker_thread_id) + .await + .expect("resume v2 worker on demand"); + let resumed_worker = resumed_manager + .get_thread(worker_thread_id) + .await + .expect("resumed worker should be loaded"); + assert_eq!( + resumed_worker.session.inherited_instructions().await.thread, + Some(thread_instructions), + ); +} + #[tokio::test] async fn spawn_agent_creates_thread_and_sends_prompt() { let harness = AgentControlHarness::new().await; diff --git a/codex-rs/core/src/agents_md.rs b/codex-rs/core/src/agents_md.rs index 37678f3e34..0bcb2903e2 100644 --- a/codex-rs/core/src/agents_md.rs +++ b/codex-rs/core/src/agents_md.rs @@ -286,9 +286,12 @@ fn candidate_filenames(config: &Config) -> Vec<&str> { /// guidance. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct LoadedAgentsMd { - /// Host-provided user instructions. + /// Account- or home-scoped instructions supplied by the host. user_instructions: Option, + /// Thread-scoped instructions supplied by the host. + thread_instructions: Option, + /// Ordered instructions and their provenance. entries: Vec, } @@ -302,8 +305,9 @@ impl LoadedAgentsMd { Self { user_instructions: Some(Instructions { text: contents, - source: path, + source: Some(path), }), + thread_instructions: None, entries: Vec::new(), } } @@ -312,16 +316,20 @@ impl LoadedAgentsMd { Self { user_instructions: user_instructions .filter(|instructions| !instructions.text.trim().is_empty()), + thread_instructions: None, entries: Vec::new(), } } - pub(crate) fn with_user_instructions( + pub(crate) fn with_instructions( mut self, user_instructions: Option, + thread_instructions: Option, ) -> Option { self.user_instructions = user_instructions.filter(|instructions| !instructions.text.trim().is_empty()); + self.thread_instructions = + thread_instructions.filter(|instructions| !instructions.text.trim().is_empty()); (!self.is_empty()).then_some(self) } @@ -336,6 +344,7 @@ impl LoadedAgentsMd { } Self { user_instructions: None, + thread_instructions: None, entries: vec![InstructionEntry { contents, provenance: InstructionProvenance::Internal, @@ -345,6 +354,7 @@ impl LoadedAgentsMd { fn is_empty(&self) -> bool { self.user_instructions.is_none() + && self.thread_instructions.is_none() && self .entries .iter() @@ -364,7 +374,14 @@ impl LoadedAgentsMd { let mut output = String::new(); let mut has_previous = false; let mut previous_was_project = false; - if let Some(instructions) = &self.user_instructions { + for instructions in self + .user_instructions + .iter() + .chain(self.thread_instructions.iter()) + { + if has_previous { + output.push_str("\n\n"); + } output.push_str(&instructions.text); has_previous = true; } @@ -392,7 +409,14 @@ impl LoadedAgentsMd { let mut output = String::new(); let mut has_previous = false; let mut previous_environment: Option<(&str, &PathUri)> = None; - if let Some(instructions) = &self.user_instructions { + for instructions in self + .user_instructions + .iter() + .chain(self.thread_instructions.iter()) + { + if has_previous { + output.push_str("\n\n"); + } output.push_str(&instructions.text); has_previous = true; } @@ -452,7 +476,8 @@ impl LoadedAgentsMd { pub fn sources(&self) -> impl Iterator + '_ { self.user_instructions .iter() - .map(|instructions| PathUri::from_abs_path(&instructions.source)) + .chain(self.thread_instructions.iter()) + .filter_map(|instructions| instructions.source.as_ref().map(PathUri::from_abs_path)) .chain( self.entries .iter() diff --git a/codex-rs/core/src/agents_md_manager.rs b/codex-rs/core/src/agents_md_manager.rs index 3bb500192b..68fdeeeafb 100644 --- a/codex-rs/core/src/agents_md_manager.rs +++ b/codex-rs/core/src/agents_md_manager.rs @@ -1,4 +1,4 @@ -//! Resolves global and repository instructions into one validated snapshot. +//! Resolves global, thread, and repository instructions into one validated snapshot. //! Refreshes are serialized; the state lock is never held while calling providers. use crate::agents_md::LoadedAgentsMd; @@ -6,11 +6,14 @@ use crate::agents_md::load_project_instructions; use crate::config::Config; use crate::environment_selection::TurnEnvironmentSnapshot; use codex_extension_api::Instructions; +use codex_extension_api::ThreadInstructionsProvider; use codex_extension_api::UserInstructionsProvider; use codex_protocol::config_types::TrustLevel; use codex_protocol::error::CodexErr; use codex_protocol::error::Result as CodexResult; use codex_protocol::protocol::TurnEnvironmentSelection; +use codex_utils_string::approx_bytes_for_tokens; +use codex_utils_string::approx_tokens_from_byte_count; use std::sync::Arc; use tokio::sync::Mutex; use tokio::sync::Semaphore; @@ -25,7 +28,9 @@ pub(crate) struct AgentsMdManager { #[derive(Clone, Default)] pub(crate) struct SessionInstructions { pub(crate) user: Option, + pub(crate) thread: Option, pub(crate) user_provider: Option>, + pub(crate) thread_provider: Option>, } struct AgentsMdState { @@ -43,6 +48,7 @@ struct AgentsMdCache { impl AgentsMdManager { pub(crate) fn new(mut instructions: SessionInstructions) -> Self { instructions.user = normalize_instructions(instructions.user); + instructions.thread = normalize_instructions(instructions.thread); Self { refresh_lock: Semaphore::new(/*permits*/ 1), state: Mutex::new(AgentsMdState { @@ -96,11 +102,21 @@ impl AgentsMdManager { instructions.user = normalize_instructions(loaded.instructions); warnings.extend(loaded.warnings); } + if let Some(provider) = &instructions.thread_provider { + let loaded = provider.load_thread_instructions().await; + instructions.thread = normalize_instructions(loaded.instructions); + warnings.extend(loaded.warnings); + } let result = async { + if let Some(input) = &instructions.thread { + validate_thread_instruction_size(input.text.len())?; + } if !refresh_repository { let state = self.state.lock().await; - if state.instructions.user == instructions.user { + if state.instructions.user == instructions.user + && state.instructions.thread == instructions.thread + { return Ok(cached); } } @@ -112,7 +128,7 @@ impl AgentsMdManager { } else { cached.as_deref().cloned().unwrap_or_default() } - .with_user_instructions(instructions.user.clone()) + .with_instructions(instructions.user.clone(), instructions.thread.clone()) .map(Arc::new); let mut state = self.state.lock().await; state.instructions = instructions; @@ -135,11 +151,27 @@ impl AgentsMdManager { let state = self.state.lock().await; SessionInstructions { user: state.instructions.user.clone(), + thread: state.instructions.thread.clone(), ..Default::default() } } } +// Bound the new host-provided contribution independently of project_doc_max_bytes, +// which controls repository discovery. Existing global and combined instruction +// size policy is unchanged; reject oversized thread input rather than truncate it. +const MAX_THREAD_INSTRUCTIONS_TOKENS: usize = 10_000; + +fn validate_thread_instruction_size(bytes: usize) -> CodexResult<()> { + if bytes > approx_bytes_for_tokens(MAX_THREAD_INSTRUCTIONS_TOKENS) { + let estimated_tokens = approx_tokens_from_byte_count(bytes); + return Err(CodexErr::InvalidRequest(format!( + "thread instructions exceed the limit of {MAX_THREAD_INSTRUCTIONS_TOKENS} estimated tokens ({estimated_tokens} estimated tokens provided)" + ))); + } + Ok(()) +} + fn normalize_instructions(instructions: Option) -> Option { instructions.filter(|instructions| !instructions.text.trim().is_empty()) } diff --git a/codex-rs/core/src/agents_md_tests.rs b/codex-rs/core/src/agents_md_tests.rs index bfa1f8768a..d2e8f0644a 100644 --- a/codex-rs/core/src/agents_md_tests.rs +++ b/codex-rs/core/src/agents_md_tests.rs @@ -418,6 +418,7 @@ fn foreign_agents_md_uses_environment_native_paths() { let source_path = cwd.join("AGENTS.md").expect("AGENTS.md URI"); let loaded = LoadedAgentsMd { user_instructions: None, + thread_instructions: None, entries: vec![InstructionEntry { contents: "remote instructions".to_string(), provenance: InstructionProvenance::Project { @@ -451,6 +452,7 @@ fn multi_environment_agents_md_renders_mixed_path_conventions() { .expect("Windows AGENTS.md URI"); let loaded = LoadedAgentsMd { user_instructions: None, + thread_instructions: None, entries: vec![ InstructionEntry { contents: "POSIX instructions".to_string(), @@ -509,7 +511,7 @@ async fn make_config(root: &TempDir, limit: usize, instructions: Option<&str>) - let user_instructions = instructions.map(|text| Instructions { text: text.to_owned(), - source: config.codex_home.join(DEFAULT_AGENTS_MD_FILENAME), + source: Some(config.codex_home.join(DEFAULT_AGENTS_MD_FILENAME)), }); TestConfig { config, @@ -558,7 +560,7 @@ async fn make_config_with_project_root_markers( config.project_doc_max_bytes = limit; let user_instructions = instructions.map(|text| Instructions { text: text.to_owned(), - source: config.codex_home.join(DEFAULT_AGENTS_MD_FILENAME), + source: Some(config.codex_home.join(DEFAULT_AGENTS_MD_FILENAME)), }); TestConfig { config, @@ -608,6 +610,7 @@ fn empty_loaded_instructions_are_empty() { fn loaded_instructions_with_only_empty_or_whitespace_entries_are_empty() { let empty = LoadedAgentsMd { user_instructions: None, + thread_instructions: None, entries: vec![InstructionEntry { contents: String::new(), provenance: InstructionProvenance::Internal, @@ -615,6 +618,7 @@ fn loaded_instructions_with_only_empty_or_whitespace_entries_are_empty() { }; let whitespace = LoadedAgentsMd { user_instructions: None, + thread_instructions: None, entries: vec![InstructionEntry { contents: " \n\t".to_string(), provenance: InstructionProvenance::Internal, @@ -686,6 +690,7 @@ async fn total_byte_limit_truncates_later_project_docs() { let loaded = load_agents_md(&config).await.expect("project instructions"); let expected = LoadedAgentsMd { user_instructions: None, + thread_instructions: None, entries: vec![ InstructionEntry { contents: "root".to_string(), @@ -1100,16 +1105,16 @@ async fn zero_byte_limit_disables_docs() { ); } -/// When both system instructions and AGENTS.md docs are present the two -/// should be concatenated with the separator. +/// User instructions precede project docs without consuming their byte budget. #[tokio::test] async fn merges_existing_instructions_with_agents_md() { let tmp = tempfile::tempdir().expect("tempdir"); - fs::write(tmp.path().join("AGENTS.md"), "proj doc").unwrap(); + fs::write(tmp.path().join("AGENTS.md"), "proj document").unwrap(); const INSTRUCTIONS: &str = "base instructions"; + let limit = "proj doc".len(); - let res = get_user_instructions(&make_config(&tmp, /*limit*/ 4096, Some(INSTRUCTIONS)).await) + let res = get_user_instructions(&make_config(&tmp, limit, Some(INSTRUCTIONS)).await) .await .expect("should produce a combined instruction string"); @@ -1173,11 +1178,13 @@ secondary doc"#, loaded.sources().collect::>(), vec![ PathUri::from_abs_path( - &config + config .user_instructions .as_ref() .expect("global instructions") - .source, + .source + .as_ref() + .expect("global instruction source"), ), PathUri::from_abs_path(&primary.path().join("AGENTS.md").abs()), PathUri::from_abs_path(&primary_nested.join("AGENTS.md").abs()), @@ -1376,6 +1383,7 @@ async fn concatenates_root_and_cwd_docs() { let crate_agents = cfg.cwd.join("AGENTS.md"); let expected = LoadedAgentsMd { user_instructions: None, + thread_instructions: None, entries: vec![ InstructionEntry { contents: "root doc".to_string(), @@ -1513,8 +1521,9 @@ async fn instruction_sources_include_global_before_agents_md_docs() { let expected = LoadedAgentsMd { user_instructions: Some(Instructions { text: "global doc".to_string(), - source: global_agents.clone(), + source: Some(global_agents.clone()), }), + thread_instructions: None, entries: vec![InstructionEntry { contents: "project doc".to_string(), provenance: project_provenance(project_agents.clone(), cfg.cwd.clone()), diff --git a/codex-rs/core/src/guardian/review_session.rs b/codex-rs/core/src/guardian/review_session.rs index 22e1ff5965..f7d38d20c7 100644 --- a/codex-rs/core/src/guardian/review_session.rs +++ b/codex-rs/core/src/guardian/review_session.rs @@ -222,6 +222,7 @@ pub struct GuardianReviewSessionReuseKey { developer_instructions: Option, base_instructions: Option, user_instructions: Option, + thread_instructions: Option, compact_prompt: Option, cwd: PathUri, mcp_servers: Constrained>, @@ -264,6 +265,7 @@ impl GuardianReviewSessionReuseKey { developer_instructions: spawn_config.developer_instructions.clone(), base_instructions: spawn_config.base_instructions.clone(), user_instructions: instructions.user, + thread_instructions: instructions.thread, compact_prompt: spawn_config.compact_prompt.clone(), cwd: PathUri::from_abs_path(&spawn_config.cwd), mcp_servers: spawn_config.mcp_servers.clone(), diff --git a/codex-rs/core/src/guardian/review_session_factory.rs b/codex-rs/core/src/guardian/review_session_factory.rs index c7be2363fd..894ee52c14 100644 --- a/codex-rs/core/src/guardian/review_session_factory.rs +++ b/codex-rs/core/src/guardian/review_session_factory.rs @@ -137,6 +137,7 @@ impl ReviewerSessionFactory for PreparedSession { let inherited = session.inherited_instructions().await; let context = GuardianReviewSessionReuseKey { user_instructions: inherited.user, + thread_instructions: inherited.thread, ..context }; Ok(GuardianReviewSession { diff --git a/codex-rs/core/src/guardian/review_session_tests.rs b/codex-rs/core/src/guardian/review_session_tests.rs index d0239d7e74..987faf87fd 100644 --- a/codex-rs/core/src/guardian/review_session_tests.rs +++ b/codex-rs/core/src/guardian/review_session_tests.rs @@ -282,22 +282,27 @@ async fn spawned_guardian_session_preserves_windows_sandbox_proxy_settings() { #[tokio::test] async fn spawned_guardian_reuse_key_matches_inherited_instructions() { let mut params = test_review_params().await; + let latest = Some(Instructions { + text: "latest thread instructions".to_string(), + source: None, + }); let latest_global = Some(Instructions { text: "latest global instructions".to_string(), - source: params.spawn_config.codex_home.join("AGENTS.md"), + source: None, }); let parent = Arc::get_mut(&mut params.parent_session).expect("unshared parent session"); parent.services.agents_md_manager = Arc::new(AgentsMdManager::new(SessionInstructions { user: latest_global.clone(), + thread: latest.clone(), ..Default::default() })); // Reproduce an update between reuse-key capture and reviewer creation. let stale_key = GuardianReviewSessionReuseKey::from_spawn_config( ¶ms.spawn_config, SessionInstructions { - user: Some(Instructions { - text: "previous global instructions".to_string(), - source: params.spawn_config.codex_home.join("AGENTS.md"), + thread: Some(Instructions { + text: "previous thread instructions".to_string(), + source: None, }), ..Default::default() }, @@ -305,7 +310,8 @@ async fn spawned_guardian_reuse_key_matches_inherited_instructions() { parent.guardian_context_mode, ); let expected_key = GuardianReviewSessionReuseKey { - user_instructions: latest_global.clone(), + user_instructions: latest_global, + thread_instructions: latest.clone(), ..stale_key.clone() }; let prepared = factory::prepare_review(params) @@ -323,10 +329,7 @@ async fn spawned_guardian_reuse_key_matches_inherited_instructions() { .expect("spawn reviewer after instruction update"); assert_eq!(review.reuse_key, expected_key); - assert_eq!( - review.session.inherited_instructions().await.user, - latest_global - ); + assert_eq!(review.session.inherited_instructions().await.thread, latest); review.shutdown().await; } @@ -394,6 +397,17 @@ async fn guardian_review_session_config_change_invalidates_cached_session() { GuardianContextMode::Legacy, ) ); + assert_ne!( + cached_reuse_key.clone(), + GuardianReviewSessionReuseKey { + thread_instructions: Some(Instructions { + text: "updated thread instructions".to_string(), + source: None, + }), + ..cached_reuse_key.clone() + }, + "changing thread instructions must invalidate reviewer history" + ); assert_ne!( cached_reuse_key .clone() @@ -969,7 +983,11 @@ async fn run_review_removes_trunk_when_event_stream_is_broken() { parent.services.agents_md_manager = Arc::new(AgentsMdManager::new(SessionInstructions { user: Some(Instructions { text: "parent global instructions".to_string(), - source: params.spawn_config.codex_home.join("AGENTS.md"), + source: None, + }), + thread: Some(Instructions { + text: "parent thread instructions".to_string(), + source: None, }), ..Default::default() })); diff --git a/codex-rs/core/src/guardian/review_session_threads.rs b/codex-rs/core/src/guardian/review_session_threads.rs index dd3d793140..5d50f0710d 100644 --- a/codex-rs/core/src/guardian/review_session_threads.rs +++ b/codex-rs/core/src/guardian/review_session_threads.rs @@ -5,7 +5,6 @@ use std::sync::Arc; use std::sync::Weak; use codex_async_utils::OrCancelExt; -use codex_extension_api::LoadedUserInstructions; use codex_history::InitialHistory; use codex_protocol::protocol::AskForApproval; use codex_protocol::protocol::InternalSessionSource; @@ -75,28 +74,28 @@ impl ManagedReviewerThreads { thread_source: Some(ThreadSource::GuardianReview), environments: Some(context.environments().to_selections()), inherited_environments: Some(context.environments().clone()), - user_instructions: Some(LoadedUserInstructions { - instructions: parent.inherited_instructions().await.user, - warnings: Vec::new(), - }), client_mcp_extensions: parent.services.client_mcp_extensions.clone(), ..StartThreadOptions::new(config) }; - let spawned = match history.unwrap_or(InitialHistory::New) { - InitialHistory::Forked(history) => manager - .fork_internal_session(parent.thread_id(), options, history) - .or_cancel(&cancel) - .await - .map_err(codex_protocol::error::CodexErr::from)??, - InitialHistory::New | InitialHistory::Cleared => manager - .spawn_internal_session(parent.thread_id(), options) - .or_cancel(&cancel) - .await - .map_err(codex_protocol::error::CodexErr::from)??, + let history = match history.unwrap_or(InitialHistory::New) { + InitialHistory::Forked(history) => InitialHistory::Forked(history), + InitialHistory::New | InitialHistory::Cleared => InitialHistory::New, InitialHistory::Resumed(_) => { anyhow::bail!("guardian review forks cannot resume an existing thread") } }; + // Generic internal sessions start isolated. Reviewers explicitly inherit + // the parent's applied instructions, just like standalone delegates. + let spawned = manager + .spawn_internal_session_with_history( + parent.thread_id(), + options, + history, + Some(parent.inherited_instructions().await), + ) + .or_cancel(&cancel) + .await + .map_err(codex_protocol::error::CodexErr::from)??; let thread = spawned.thread; let session = Arc::clone(&thread.session); let io = forward_session_io( diff --git a/codex-rs/core/src/session/step_activation_tests.rs b/codex-rs/core/src/session/step_activation_tests.rs index 828a5d36b5..dcaa598eb6 100644 --- a/codex-rs/core/src/session/step_activation_tests.rs +++ b/codex-rs/core/src/session/step_activation_tests.rs @@ -18,8 +18,9 @@ use codex_config::ConfigRequirementsWithSources; use codex_config::RequirementSource; use codex_config::Sourced; use codex_extension_api::Instructions; -use codex_extension_api::LoadUserInstructionsFuture; +use codex_extension_api::LoadInstructionsFuture; use codex_extension_api::LoadedUserInstructions; +use codex_extension_api::ThreadInstructionsProvider; use codex_extension_api::UserInstructionsProvider; use codex_http_client::HttpClientFactory; use codex_login::AuthManager; @@ -60,42 +61,54 @@ use tokio_util::sync::CancellationToken; const MODEL_A: &str = "step-activation-a"; const MODEL_B: &str = "step-activation-b"; +struct GatedInstructionsProvider { + instructions: StdMutex>, + block_next_read: AtomicBool, + read_started: Notify, +} + +impl GatedInstructionsProvider { + fn load(&self) -> LoadInstructionsFuture<'_> { + let instructions = self + .instructions + .lock() + .expect("instruction snapshot") + .clone(); + let block = self.block_next_read.swap(/*val*/ false, Ordering::SeqCst); + Box::pin(async move { + if block { + self.read_started.notify_one(); + std::future::pending::<()>().await; + } + LoadedUserInstructions { + instructions, + warnings: Vec::new(), + } + }) + } +} + +impl UserInstructionsProvider for GatedInstructionsProvider { + fn load_user_instructions(&self) -> LoadInstructionsFuture<'_> { + self.load() + } +} + +impl ThreadInstructionsProvider for GatedInstructionsProvider { + fn load_thread_instructions(&self) -> LoadInstructionsFuture<'_> { + self.load() + } +} + #[tokio::test] async fn instruction_refresh_serializes_reads_and_releases_on_cancellation() { - struct GatedInstructionsProvider { - instructions: StdMutex>, - block_next_read: AtomicBool, - read_started: Notify, - } - - impl UserInstructionsProvider for GatedInstructionsProvider { - fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_> { - let instructions = self - .instructions - .lock() - .expect("instruction snapshot") - .clone(); - let block = self.block_next_read.swap(/*val*/ false, Ordering::SeqCst); - Box::pin(async move { - if block { - self.read_started.notify_one(); - std::future::pending::<()>().await; - } - LoadedUserInstructions { - instructions, - warnings: Vec::new(), - } - }) - } - } - let (mut session, turn) = make_session_and_context().await; let turn = Arc::new(turn); let source = turn.config.codex_home.join("AGENTS.md"); let instructions = |text: &str| { Some(Instructions { text: text.to_string(), - source: source.clone(), + source: Some(source.clone()), }) }; let provider = Arc::new(GatedInstructionsProvider { @@ -149,6 +162,91 @@ async fn instruction_refresh_serializes_reads_and_releases_on_cancellation() { ); } +#[test_case(None; "thread only")] +#[test_case(Some("global instructions"); "with global instructions")] +#[tokio::test] +async fn thread_instruction_refresh_serializes_reads_and_releases_on_cancellation( + global_text: Option<&str>, +) { + let (mut session, mut turn) = make_session_and_context().await; + // This tests provider snapshots, not the checkout's repository instructions. + Arc::make_mut(&mut turn.config).project_doc_max_bytes = 0; + let turn = Arc::new(turn); + let global = global_text.map(|text| Instructions { + text: text.to_string(), + source: Some(turn.config.codex_home.join("AGENTS.md")), + }); + let instructions = |text: &str| { + Some(Instructions { + text: text.to_string(), + source: None, + }) + }; + let provider = Arc::new(GatedInstructionsProvider { + instructions: StdMutex::new(instructions("initial")), + block_next_read: AtomicBool::new(/*v*/ false), + read_started: Notify::new(), + }); + session.services.agents_md_manager = Arc::new(AgentsMdManager::new(SessionInstructions { + user: global.clone(), + thread_provider: Some(provider.clone()), + ..Default::default() + })); + let session = Arc::new(session); + session + .services + .agents_md_manager + .refresh(&turn.config, &turn.environments) + .await + .0 + .expect("install initial provider"); + *provider.instructions.lock().expect("instruction snapshot") = instructions("cancelled"); + provider + .block_next_read + .store(/*val*/ true, Ordering::SeqCst); + + let cancellation = CancellationToken::new(); + let first_capture = session.capture_step_context(Arc::clone(&turn), &cancellation); + tokio::pin!(first_capture); + tokio::select! { + _ = &mut first_capture => panic!("capture must wait for provider"), + () = provider.read_started.notified() => {} + } + + *provider.instructions.lock().expect("instruction snapshot") = instructions("latest"); + let next_cancellation = CancellationToken::new(); + let next_capture = session.capture_step_context(turn, &next_cancellation); + tokio::pin!(next_capture); + assert!(futures::poll!(&mut next_capture).is_pending()); + let inherited = session.inherited_instructions().await; + assert_eq!( + (inherited.user, inherited.thread), + (global.clone(), instructions("initial")) + ); + + cancellation.cancel(); + assert!(first_capture.await.is_err()); + let step = next_capture + .await + .expect("cancelled read releases the refresh guard"); + let inherited = session.inherited_instructions().await; + assert_eq!( + (inherited.user, inherited.thread), + (global, instructions("latest")) + ); + let expected = global_text.map_or_else( + || "latest".to_string(), + |global| format!("{global}\n\nlatest"), + ); + assert_eq!( + step.loaded_agents_md + .as_ref() + .expect("captured instructions") + .text(), + expected + ); +} + fn activation_models() -> Vec { let model = bundled_models_response() .expect("bundled models") diff --git a/codex-rs/core/src/test_support.rs b/codex-rs/core/src/test_support.rs index 8ce1f815d7..b93c2ba700 100644 --- a/codex-rs/core/src/test_support.rs +++ b/codex-rs/core/src/test_support.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use std::sync::Arc; use codex_exec_server::EnvironmentManager; -use codex_extension_api::LoadUserInstructionsFuture; +use codex_extension_api::LoadInstructionsFuture; use codex_extension_api::LoadedUserInstructions; use codex_extension_api::UserInstructionsProvider; use codex_http_client::HttpClientFactory; @@ -69,7 +69,7 @@ pub async fn history_with_tool_call_metadata( pub struct EmptyUserInstructionsProvider; impl UserInstructionsProvider for EmptyUserInstructionsProvider { - fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_> { + fn load_user_instructions(&self) -> LoadInstructionsFuture<'_> { Box::pin(async { LoadedUserInstructions::default() }) } } diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 9ce622f540..fe9d373182 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -34,6 +34,7 @@ use codex_exec_server::EnvironmentManager; use codex_extension_api::ExtensionDataInit; use codex_extension_api::ExtensionRegistry; use codex_extension_api::LoadedUserInstructions; +use codex_extension_api::ThreadInstructionsProvider; use codex_extension_api::UserInstructionsProvider; use codex_extension_api::empty_extension_registry; use codex_features::Feature; @@ -231,6 +232,19 @@ pub struct ThreadManager { pub struct StartThreadOptions { pub config: Config, + /// Host-owned provider for this root thread's additional instructions. + /// + /// Core composes these after the global [`UserInstructionsProvider`] + /// snapshot and before repository instructions. `None` or blank provider + /// output clears only the thread contribution. Core retains this provider + /// and reads it when capturing model-request context. Hosts own fetching + /// and caching, and must supply a provider again when cold-resuming a thread + /// or forking an offline source. A live source can supply its applied snapshot + /// to a fork, but its task-bound provider is never inherited by the new root. + /// Registration lasts for the live session. Warm resume ignores this option + /// and reuses the existing provider without loading it; normal model-request + /// boundaries load its latest instructions. + pub thread_instructions_provider: Option>, pub allow_provider_model_fallback: bool, pub initial_history: InitialHistory, pub history_mode: Option, @@ -242,7 +256,7 @@ pub struct StartThreadOptions { pub environments: Option>, /// Existing environment bindings captured by an internal caller. pub inherited_environments: Option, - /// Explicit instructions carried by an internal caller instead of loading them again. + /// Explicit global instructions carried by an internal caller instead of loading them again. pub user_instructions: Option, pub thread_extension_init: ExtensionDataInit, pub client_mcp_extensions: ClientMcpExtensions, @@ -256,6 +270,7 @@ impl StartThreadOptions { pub fn new(config: Config) -> Self { Self { config, + thread_instructions_provider: None, allow_provider_model_fallback: false, initial_history: InitialHistory::New, history_mode: None, @@ -283,6 +298,7 @@ struct ThreadSpawnRequest { forked_from_thread_id: Option, fork_persistence: ForkPersistence, inherited_environments: Option, + inherited_instructions: Option, inherited_exec_policy: Option>, user_shell_override: Option, } @@ -301,6 +317,7 @@ impl ThreadSpawnRequest { forked_from_thread_id: None, fork_persistence: ForkPersistence::Copied, inherited_environments: None, + inherited_instructions: None, inherited_exec_policy: None, user_shell_override: None, } @@ -345,6 +362,7 @@ pub(crate) struct ResumeThreadWithHistoryOptions { pub(crate) parent_thread_id: Option, pub(crate) environment_selections: Option>, pub(crate) inherited_environments: Option, + pub(crate) inherited_instructions: Option, pub(crate) inherited_exec_policy: Option>, pub(crate) client_mcp_extensions: Option, } @@ -978,8 +996,13 @@ impl ThreadManager { parent_thread_id: ThreadId, options: StartThreadOptions, ) -> CodexResult { - self.spawn_internal_session_with_history(parent_thread_id, options, InitialHistory::New) - .await + self.spawn_internal_session_with_history( + parent_thread_id, + options, + InitialHistory::New, + /*inherited_instructions*/ None, + ) + .await } /// Starts an internal session from an explicitly selected, committed history. @@ -996,15 +1019,17 @@ impl ThreadManager { parent_thread_id, options, InitialHistory::Forked(history), + /*inherited_instructions*/ None, ) .await } - async fn spawn_internal_session_with_history( + pub(crate) async fn spawn_internal_session_with_history( &self, parent_thread_id: ThreadId, mut options: StartThreadOptions, history: InitialHistory, + inherited_instructions: Option, ) -> CodexResult { if !matches!(options.session_source, Some(SessionSource::Internal(_))) { return Err(CodexErr::InvalidRequest( @@ -1021,6 +1046,7 @@ impl ThreadManager { ); request.parent_thread_id = Some(parent_thread_id); request.forked_from_thread_id = forked_from_thread_id; + request.inherited_instructions = inherited_instructions; Box::pin(self.state.spawn_thread(request)).await } @@ -1154,14 +1180,6 @@ impl ThreadManager { let (session_source, thread_source) = initial_history .get_resumed_session_sources() .unwrap_or_else(|| (self.state.session_source.clone(), None)); - if let InitialHistory::Resumed(resumed) = &initial_history - && initial_history.get_multi_agent_version() == Some(MultiAgentVersion::V2) - && !session_source.is_non_root_agent() - { - agent_control - .restore_v2_agent_metadata(&config, resumed.conversation_id) - .await; - } let options = StartThreadOptions { initial_history, session_source: Some(session_source), @@ -1319,7 +1337,8 @@ impl ThreadManager { /// Fork an existing thread by snapshotting rollout history according to /// `snapshot` and starting a new thread with identical configuration /// (unless overridden by the caller's options). The new thread has a fresh id. - /// Fork history replaces `options.initial_history`. + /// Fork history replaces `options.initial_history`; hosts can supply the new + /// task's instruction provider through the same options as thread creation. pub async fn fork_thread( &self, snapshot: S, @@ -1420,6 +1439,20 @@ impl ThreadManager { InitialHistory::Forked(_) => history.forked_from_id(), InitialHistory::New | InitialHistory::Cleared => None, }; + // Capture the live source before later startup work can unload it. Keep + // the fork's own providers, not the source's task-bound callback. + let instructions = self + .state + .instructions_for_spawn( + options + .session_source + .as_ref() + .unwrap_or(&self.state.session_source), + /*parent_thread_id*/ None, + source_thread_id, + options.thread_instructions_provider.clone(), + ) + .await; // Capture the source's settings before truncating its model history. options.disabled_plugin_ids = Some(options.disabled_plugin_ids.unwrap_or_else(|| { source_thread_id @@ -1452,6 +1485,7 @@ impl ThreadManager { ThreadSpawnRequest::new(options, Arc::clone(&self.state.auth_manager), agent_control); request.forked_from_thread_id = source_thread_id; request.fork_persistence = fork_persistence; + request.inherited_instructions = Some(instructions); Box::pin(self.state.spawn_thread(request)).await } @@ -1655,35 +1689,41 @@ impl ThreadManagerState { /// Selects instruction sources for a newly spawned runtime without loading providers. /// - /// Fresh roots, cold resumes, and root forks retain the global provider. The - /// session's AgentsMdManager loads it at startup and subsequent context captures. - /// Subagents inherit their live parent's applied snapshot without its provider; - /// if that parent is unavailable, they start without inherited instructions. + /// Fresh roots, cold resumes, and root forks retain the global and any supplied + /// thread provider. The session's AgentsMdManager loads them at startup and + /// subsequent context captures. Subagents inherit only applied parent snapshots. + /// A root fork without a provider preserves the live source's thread snapshot, + /// never its task-bound provider. Hosts must supply a provider for offline forks. /// Warm resumes retain the existing session and do not call this function. async fn instructions_for_spawn( &self, session_source: &SessionSource, parent_thread_id: Option, forked_from_thread_id: Option, + thread_provider: Option>, ) -> SessionInstructions { - if !session_source.is_non_root_agent() { - return SessionInstructions { - user_provider: Some(Arc::clone(&self.user_instructions_provider)), - ..Default::default() - }; - } let inherited_thread_id = match session_source { SessionSource::SubAgent(SubAgentSource::ThreadSpawn { parent_thread_id, .. }) => Some(*parent_thread_id), _ => parent_thread_id.or(forked_from_thread_id), }; - match inherited_thread_id { + let inherited = match inherited_thread_id { Some(thread_id) => match self.get_thread(thread_id).await { Ok(thread) => thread.session.inherited_instructions().await, Err(_) => SessionInstructions::default(), }, None => SessionInstructions::default(), + }; + if session_source.is_non_root_agent() { + inherited + } else { + SessionInstructions { + user_provider: Some(Arc::clone(&self.user_instructions_provider)), + thread_provider, + thread: inherited.thread, + ..Default::default() + } } } @@ -1813,6 +1853,7 @@ impl ThreadManagerState { parent_thread_id, environment_selections, inherited_environments, + inherited_instructions, inherited_exec_policy, client_mcp_extensions, } = options; @@ -1838,6 +1879,7 @@ impl ThreadManagerState { ThreadSpawnRequest::new(options, Arc::clone(&self.auth_manager), agent_control); request.parent_thread_id = parent_thread_id; request.inherited_environments = inherited_environments; + request.inherited_instructions = inherited_instructions; request.inherited_exec_policy = inherited_exec_policy; Box::pin(self.spawn_thread(request)).await } @@ -1901,11 +1943,13 @@ impl ThreadManagerState { forked_from_thread_id, fork_persistence, inherited_environments, + inherited_instructions, inherited_exec_policy, user_shell_override, } = request; let StartThreadOptions { mut config, + thread_instructions_provider, allow_provider_model_fallback, initial_history, history_mode, @@ -1981,10 +2025,19 @@ impl ThreadManagerState { threads.remove(&resumed.conversation_id); } } + // Both resume entry points must restore identities before children can be loaded lazily. + if let InitialHistory::Resumed(resumed) = &initial_history + && initial_history.get_multi_agent_version() == Some(MultiAgentVersion::V2) + && !session_source.is_non_root_agent() + { + agent_control + .restore_v2_agent_metadata(&config, resumed.conversation_id) + .await; + } let (instructions, inherited_exec_policy, extensions, mcp_manager, multi_agent_version) = if isolation == codex_extension_api::SessionIsolation::Isolated { ( - SessionInstructions::default(), + inherited_instructions.unwrap_or_default(), None, empty_extension_registry(), Arc::new(McpManager::new(Arc::clone(&self.plugins_manager))), @@ -1992,12 +2045,18 @@ impl ThreadManagerState { ) } else { ( - self.instructions_for_spawn( - &session_source, - parent_thread_id, - forked_from_thread_id, - ) - .await, + match inherited_instructions { + Some(instructions) => instructions, + None => { + self.instructions_for_spawn( + &session_source, + parent_thread_id, + forked_from_thread_id, + thread_instructions_provider, + ) + .await + } + }, inherited_exec_policy, Arc::clone(&self.extensions), Arc::clone(&self.mcp_manager), diff --git a/codex-rs/core/src/thread_manager_tests.rs b/codex-rs/core/src/thread_manager_tests.rs index 88fe058d5e..131df94c35 100644 --- a/codex-rs/core/src/thread_manager_tests.rs +++ b/codex-rs/core/src/thread_manager_tests.rs @@ -53,6 +53,103 @@ use wiremock::MockServer; const TEST_INSTALLATION_ID: &str = "11111111-1111-4111-8111-111111111111"; +struct ParentInstructionsProvider(codex_extension_api::Instructions); + +impl codex_extension_api::UserInstructionsProvider for ParentInstructionsProvider { + fn load_user_instructions(&self) -> codex_extension_api::LoadInstructionsFuture<'_> { + Box::pin(async move { + codex_extension_api::LoadedUserInstructions { + instructions: Some(self.0.clone()), + warnings: Vec::new(), + } + }) + } +} + +impl codex_extension_api::ThreadInstructionsProvider for ParentInstructionsProvider { + fn load_thread_instructions(&self) -> codex_extension_api::LoadInstructionsFuture<'_> { + codex_extension_api::UserInstructionsProvider::load_user_instructions(self) + } +} + +#[tokio::test] +async fn live_fork_keeps_instructions_when_source_is_unloaded_during_setup() { + let temp_dir = tempdir().expect("tempdir"); + let mut config = test_config().await; + config.codex_home = temp_dir.path().join("codex-home").abs(); + config.cwd = config.codex_home.abs(); + std::fs::create_dir_all(&config.codex_home).expect("create codex home"); + let global = codex_extension_api::Instructions { + text: "global instructions".to_string(), + source: None, + }; + let thread = codex_extension_api::Instructions { + text: "source thread instructions".to_string(), + source: None, + }; + let mut manager = ThreadManager::with_models_provider_and_home_for_tests( + CodexAuth::from_api_key("dummy"), + config.model_provider.clone(), + config.codex_home.to_path_buf(), + Arc::new(codex_exec_server::EnvironmentManager::default_for_tests()), + ); + Arc::get_mut(&mut manager.state) + .expect("unshared manager") + .user_instructions_provider = Arc::new(ParentInstructionsProvider(global.clone())); + let source = manager + .start_thread(StartThreadOptions { + environments: Some(Vec::new()), + thread_instructions_provider: Some(Arc::new(ParentInstructionsProvider( + thread.clone(), + ))), + ..StartThreadOptions::new(config.clone()) + }) + .await + .expect("start source"); + let history = InitialHistory::Resumed(ResumedHistory { + conversation_id: source.thread_id, + history: Arc::new(Vec::new()), + rollout_path: None, + }); + + // Queue removal between the first source lookup and subsequent startup + // lookups. Tokio's fair RwLock makes this ordering deterministic. + let fork = manager.fork_thread_from_history( + ForkSnapshot::Interrupted, + StartThreadOptions { + environments: Some(Vec::new()), + ..StartThreadOptions::new(config) + }, + history, + ); + tokio::pin!(fork); + { + let _guard = manager.state.threads.write().await; + let mut context = std::task::Context::from_waker(std::task::Waker::noop()); + assert!(std::future::Future::poll(fork.as_mut(), &mut context).is_pending()); + } + let removal = manager.remove_thread_if_matches(&source.thread_id, &source.thread); + tokio::pin!(removal); + assert!(futures::poll!(&mut removal).is_pending()); + assert!(futures::poll!(&mut fork).is_pending()); + assert!(removal.await.is_some()); + let fork = fork.await.expect("fork survives source removal"); + let instructions = fork.thread.session.inherited_instructions().await; + assert_eq!( + (instructions.user, instructions.thread), + (Some(global), Some(thread)) + ); + source + .thread + .shutdown_and_wait() + .await + .expect("shutdown source"); + fork.thread + .shutdown_and_wait() + .await + .expect("shutdown fork"); +} + /// A thread opt-out wins over a shared client without disabling its siblings. #[tokio::test] async fn thread_analytics_opt_out_overrides_shared_client() { @@ -1149,19 +1246,6 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history } } - struct ParentInstructionsProvider(codex_extension_api::Instructions); - - impl codex_extension_api::UserInstructionsProvider for ParentInstructionsProvider { - fn load_user_instructions(&self) -> codex_extension_api::LoadUserInstructionsFuture<'_> { - Box::pin(async move { - codex_extension_api::LoadedUserInstructions { - instructions: Some(self.0.clone()), - warnings: Vec::new(), - } - }) - } - } - let temp_dir = tempdir().expect("tempdir"); let mut config = test_config().await; config.codex_home = temp_dir.path().join("codex-home").abs(); @@ -1197,7 +1281,7 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history let parent_instructions = codex_extension_api::Instructions { text: "parent user instructions must not be inherited".to_string(), - source: config.codex_home.join("AGENTS.md"), + source: Some(config.codex_home.join("AGENTS.md")), }; let mut manager = ThreadManager::with_models_provider_and_home_for_tests( CodexAuth::from_api_key("dummy"), @@ -1219,11 +1303,12 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history Arc::clone(&manager_state.extensions), manager_state.mcp_manager.codex_apps_tools_cache(), )); - manager_state.user_instructions_provider = - Arc::new(ParentInstructionsProvider(parent_instructions.clone())); + let parent_provider = Arc::new(ParentInstructionsProvider(parent_instructions.clone())); + manager_state.user_instructions_provider = parent_provider.clone(); let parent = manager .start_thread(StartThreadOptions { metrics_service_name: Some("codex_work_desktop".to_string()), + thread_instructions_provider: Some(parent_provider), ..StartThreadOptions::new(config.clone()) }) .await @@ -1232,9 +1317,10 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history .thread .session .set_multi_agent_version_if_unset(MultiAgentVersion::V2); + let parent_snapshot = parent.thread.session.inherited_instructions().await; assert_eq!( - parent.thread.session.inherited_instructions().await.user, - Some(parent_instructions) + (parent_snapshot.user, parent_snapshot.thread), + (Some(parent_instructions.clone()), Some(parent_instructions)) ); assert_eq!( parent @@ -1317,14 +1403,10 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history Some(parent.thread_id) ); assert_eq!(reviewer.session_configured.forked_from_id, None); - assert!( - reviewer - .thread - .session - .inherited_instructions() - .await - .user - .is_none() + let reviewer_snapshot = reviewer.thread.session.inherited_instructions().await; + assert_eq!( + (reviewer_snapshot.user, reviewer_snapshot.thread), + (None, None) ); assert!( reviewer diff --git a/codex-rs/core/tests/common/test_codex.rs b/codex-rs/core/tests/common/test_codex.rs index 419a4005e5..f81000b450 100644 --- a/codex-rs/core/tests/common/test_codex.rs +++ b/codex-rs/core/tests/common/test_codex.rs @@ -28,7 +28,7 @@ use codex_exec_server::CreateDirectoryOptions; use codex_exec_server::ExecutorFileSystem; use codex_exec_server::RemoveOptions; use codex_extension_api::ExtensionRegistry; -use codex_extension_api::LoadUserInstructionsFuture; +use codex_extension_api::LoadInstructionsFuture; use codex_extension_api::UserInstructionsProvider; use codex_extension_api::empty_extension_registry; use codex_features::Feature; @@ -114,7 +114,7 @@ impl RecordingUserInstructionsProvider { } impl UserInstructionsProvider for RecordingUserInstructionsProvider { - fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_> { + fn load_user_instructions(&self) -> LoadInstructionsFuture<'_> { self.load_count.fetch_add(1, Ordering::SeqCst); self.inner.load_user_instructions() } diff --git a/codex-rs/core/tests/suite/agents_md.rs b/codex-rs/core/tests/suite/agents_md.rs index 4979b9df78..8c0ed2b731 100644 --- a/codex-rs/core/tests/suite/agents_md.rs +++ b/codex-rs/core/tests/suite/agents_md.rs @@ -6,12 +6,19 @@ use codex_core::TurnInputRequest; use codex_exec_server::CreateDirectoryOptions; use codex_exec_server::LOCAL_ENVIRONMENT_ID; use codex_exec_server::REMOTE_ENVIRONMENT_ID; -use codex_extension_api::LoadUserInstructionsFuture; +use codex_extension_api::Instructions; +use codex_extension_api::LoadInstructionsFuture; +use codex_extension_api::LoadedUserInstructions; +use codex_extension_api::ThreadInstructionsProvider; use codex_extension_api::UserInstructionsProvider; use codex_features::Feature; +use codex_history::InitialHistory; +use codex_history::ResumedHistory; use codex_history::RolloutItem; use codex_home::CodexHomeUserInstructionsProvider; +use codex_protocol::ThreadId; use codex_protocol::config_types::TrustLevel; +use codex_protocol::error::CodexErrorDetails; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemAccessMode; use codex_protocol::permissions::FileSystemSandboxEntry; @@ -20,11 +27,19 @@ use codex_protocol::permissions::NetworkSandboxPolicy; use codex_protocol::protocol::EnvironmentConfigState; use codex_protocol::protocol::EventMsg; use codex_protocol::protocol::Op; +use codex_protocol::protocol::ThreadHistoryMode; use codex_protocol::protocol::ThreadSettingsOverrides; use codex_protocol::protocol::TurnEnvironmentSelection; +use codex_protocol::protocol::TurnEnvironmentSelections; +use codex_protocol::request_user_input::RequestUserInputAnswer; +use codex_protocol::request_user_input::RequestUserInputResponse; use codex_protocol::user_input::UserInput; +use codex_thread_store::ForkBoundary; +use codex_thread_store::LoadThreadHistoryParams; +use codex_thread_store::PrepareForkParams; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::PathUri; +use codex_utils_string::approx_bytes_for_tokens; use core_test_support::PathBufExt; use core_test_support::create_directory_symlink; use core_test_support::load_default_config_for_test; @@ -39,6 +54,7 @@ use core_test_support::skip_if_no_remote_env; use core_test_support::skip_if_sandbox; use core_test_support::skip_if_target_windows; use core_test_support::test_codex::RecordingUserInstructionsProvider; +use core_test_support::test_codex::TestCodex; use core_test_support::test_codex::TestCodexBuilder; use core_test_support::test_codex::executor_path_uri; use core_test_support::test_codex::test_codex; @@ -46,9 +62,12 @@ use core_test_support::test_codex::turn_permission_fields; use core_test_support::wait_for_event; use pretty_assertions::assert_eq; use serde_json::json; +use std::collections::HashMap; use std::path::Path; use std::sync::Arc; +use std::sync::Mutex; use std::sync::atomic::AtomicBool; +use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use std::time::Duration; use tempfile::TempDir; @@ -64,6 +83,8 @@ const NEW_PROJECT_INSTRUCTIONS: &str = "new project instructions"; const OLD_GLOBAL_INSTRUCTIONS: &str = "old global instructions"; const PROJECT_INSTRUCTIONS: &str = "project instructions"; const PROJECT_SEPARATOR: &str = "--- project-doc ---"; +const TASK_USER_INSTRUCTIONS: &str = "task user instructions"; +const UPDATED_TASK_USER_INSTRUCTIONS: &str = "updated task user instructions"; const SPAWN_CALL_ID: &str = "spawn-global-instructions-child"; const SPAWN_CHILD_PROMPT: &str = "inspect inherited global instructions"; const SPAWN_FRESH_PARENT_PROMPT: &str = "spawn a child with fresh context"; @@ -77,7 +98,7 @@ struct WarningInstructionsProvider { } impl UserInstructionsProvider for WarningInstructionsProvider { - fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_> { + fn load_user_instructions(&self) -> LoadInstructionsFuture<'_> { Box::pin(async move { let mut loaded = self.inner.load_user_instructions().await; if self.warning_active.load(Ordering::SeqCst) { @@ -88,6 +109,60 @@ impl UserInstructionsProvider for WarningInstructionsProvider { } } +struct RecordingThreadInstructionsProvider { + loaded: Mutex, + load_count: AtomicUsize, +} + +impl RecordingThreadInstructionsProvider { + fn new(instructions: Option) -> Self { + Self { + loaded: Mutex::new(LoadedUserInstructions { + instructions, + warnings: Vec::new(), + }), + load_count: AtomicUsize::new(0), + } + } + + fn with_text(text: impl Into) -> Self { + Self::new(Some(Instructions { + text: text.into(), + source: None, + })) + } + + fn load_count(&self) -> usize { + self.load_count.load(Ordering::SeqCst) + } + + fn set_instructions(&self, instructions: Option) { + self.loaded + .lock() + .expect("instruction snapshot lock") + .instructions = instructions; + } + + fn set_warnings(&self, warnings: Vec) { + self.loaded + .lock() + .expect("instruction snapshot lock") + .warnings = warnings; + } +} + +impl ThreadInstructionsProvider for RecordingThreadInstructionsProvider { + fn load_thread_instructions(&self) -> LoadInstructionsFuture<'_> { + self.load_count.fetch_add(1, Ordering::SeqCst); + let loaded = self + .loaded + .lock() + .expect("instruction snapshot lock") + .clone(); + Box::pin(async move { loaded }) + } +} + async fn agents_instructions(mut builder: TestCodexBuilder) -> Result { let server = start_mock_server().await; let resp_mock = mount_sse_once( @@ -175,6 +250,32 @@ async fn submit_thread_turn(thread: &Arc, prompt: &str) Ok(()) } +async fn persisted_resume_history( + thread: &Arc, +) -> Result<(ThreadId, InitialHistory)> { + thread.ensure_rollout_materialized().await; + thread.flush_rollout().await?; + let stored = thread + .read_thread( + /*include_archived*/ true, /*include_history*/ true, + ) + .await?; + let thread_id = stored.thread_id; + Ok(( + thread_id, + InitialHistory::Resumed(ResumedHistory { + conversation_id: thread_id, + history: Arc::new( + stored + .history + .ok_or_else(|| anyhow!("thread history should be loaded"))? + .items, + ), + rollout_path: stored.rollout_path, + }), + )) +} + fn request_body_contains(request: &wiremock::Request, text: &str) -> bool { let is_zstd = request .headers @@ -919,6 +1020,561 @@ async fn loads_user_instructions_without_a_primary_environment() -> Result<()> { Ok(()) } +struct ThreadInstructionsFixture { + test: TestCodex, + thread: Arc, + provider: Arc, +} + +impl ThreadInstructionsFixture { + async fn new(server: &wiremock::MockServer) -> Result { + let home = Arc::new(TempDir::new()?); + write_global_file(home.as_ref(), GLOBAL_AGENTS_FILENAME, GLOBAL_INSTRUCTIONS)?; + let global_provider = Arc::new(RecordingUserInstructionsProvider::new(Arc::new( + CodexHomeUserInstructionsProvider::new(AbsolutePathBuf::try_from( + home.path().to_path_buf(), + )?), + ))); + let mut builder = test_codex() + .with_home(home) + .with_user_instructions_provider(global_provider.clone()) + .with_config(|config| { + config.project_doc_max_bytes = PROJECT_INSTRUCTIONS.len(); + config + .features + .enable(Feature::DefaultModeRequestUserInput) + .expect("test config should allow request-user-input feature"); + }) + .with_workspace_setup(|cwd, fs| async move { + let project_agents_uri = executor_path_uri(cwd.join(GLOBAL_AGENTS_FILENAME))?; + fs.write_file( + &project_agents_uri, + PROJECT_INSTRUCTIONS.as_bytes().to_vec(), + Default::default(), + /*sandbox*/ None, + ) + .await?; + Ok(()) + }); + let test = builder.build_with_auto_env(server).await?; + assert_eq!(global_provider.load_count(), 1); + let provider = Arc::new(RecordingThreadInstructionsProvider::with_text( + TASK_USER_INSTRUCTIONS, + )); + let thread = test + .thread_manager + .start_thread(StartThreadOptions { + environments: Some(vec![test.executor_environment().selection().clone()]), + thread_instructions_provider: Some(provider.clone()), + ..StartThreadOptions::new(test.config.clone()) + }) + .await? + .thread; + Ok(Self { + test, + thread, + provider, + }) + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn thread_provider_composes_and_clears_only_its_instructions() -> Result<()> { + let server = start_mock_server().await; + let response_mock = responses::mount_sse_sequence( + &server, + ["initial", "unchanged", "cleared", "unchanged-empty"] + .map(|id| sse(vec![ev_response_created(id), ev_completed(id)])) + .to_vec(), + ) + .await; + let fixture = ThreadInstructionsFixture::new(&server).await?; + let sources = vec![ + PathUri::from_abs_path(&fixture.test.config.codex_home.join(GLOBAL_AGENTS_FILENAME)), + fixture.test.workspace_path_uri(GLOBAL_AGENTS_FILENAME)?, + ]; + assert_eq!(fixture.thread.instruction_sources().await, sources); + for prompt in [ + "inspect task instructions", + "inspect unchanged task instructions", + ] { + submit_thread_turn(&fixture.thread, prompt).await?; + } + for instructions in [ + Some(Instructions { + text: String::new(), + source: None, + }), + None, + ] { + fixture.provider.set_instructions(instructions); + submit_thread_turn(&fixture.thread, "inspect cleared task instructions").await?; + } + assert!(fixture.provider.load_count() > 1); + assert_eq!(fixture.thread.instruction_sources().await, sources); + + let cwd = &fixture.test.executor_environment().selection().cwd; + let initial = expected_instruction_fragment( + cwd, + &format!( + "{GLOBAL_INSTRUCTIONS}\n\n{TASK_USER_INSTRUCTIONS}\n\n{PROJECT_SEPARATOR}\n\n{PROJECT_INSTRUCTIONS}" + ), + ); + let cleared = expected_instruction_fragment( + cwd, + &format!( + "These AGENTS.md instructions replace all previously provided AGENTS.md instructions.\n\n{GLOBAL_INSTRUCTIONS}\n\n{PROJECT_SEPARATOR}\n\n{PROJECT_INSTRUCTIONS}" + ), + ); + assert_eq!( + response_mock + .requests() + .iter() + .map(instruction_fragments) + .collect::>(), + vec![ + vec![initial.clone()], + vec![initial.clone()], + vec![initial.clone(), cleared.clone()], + vec![initial, cleared], + ], + ); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn thread_provider_refreshes_at_the_next_step_of_an_active_turn() -> Result<()> { + let server = start_mock_server().await; + let response_mock = responses::mount_sse_sequence( + &server, + vec![ + sse(vec![ + ev_response_created("active-turn"), + responses::ev_function_call( + "pause-for-thread-instructions", + "request_user_input", + &json!({ + "questions": [{ + "id": "continue", + "header": "Continue", + "question": "Continue after updating thread instructions?", + "options": [{ + "label": "Yes (Recommended)", + "description": "Continue the current turn." + }, { + "label": "No", + "description": "Stop the current turn." + }] + }] + }) + .to_string(), + ), + ev_completed("active-turn"), + ]), + sse(vec![ + ev_response_created("updated"), + ev_completed("updated"), + ]), + ], + ) + .await; + let fixture = ThreadInstructionsFixture::new(&server).await?; + fixture + .thread + .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text { + text: "inspect instructions updated during the active turn".to_string(), + text_elements: Vec::new(), + }])) + .await?; + let EventMsg::RequestUserInput(request) = wait_for_event(&fixture.thread, |event| { + matches!(event, EventMsg::RequestUserInput(_)) + }) + .await + else { + unreachable!("wait_for_event should return the request-user-input event") + }; + // Only the host's provider changes; no explicit instruction update or resume call. + fixture.provider.set_instructions(Some(Instructions { + text: UPDATED_TASK_USER_INSTRUCTIONS.to_string(), + source: None, + })); + fixture + .thread + .submit(Op::UserInputAnswer { + id: request.turn_id, + response: RequestUserInputResponse { + answers: HashMap::from([( + "continue".to_string(), + RequestUserInputAnswer { + answers: vec!["Yes (Recommended)".to_string()], + }, + )]), + }, + }) + .await?; + wait_for_event(&fixture.thread, |event| { + matches!(event, EventMsg::TurnComplete(_)) + }) + .await; + + let cwd = &fixture.test.executor_environment().selection().cwd; + let initial = expected_instruction_fragment( + cwd, + &format!( + "{GLOBAL_INSTRUCTIONS}\n\n{TASK_USER_INSTRUCTIONS}\n\n{PROJECT_SEPARATOR}\n\n{PROJECT_INSTRUCTIONS}" + ), + ); + let updated = expected_instruction_fragment( + cwd, + &format!( + "These AGENTS.md instructions replace all previously provided AGENTS.md instructions.\n\n{GLOBAL_INSTRUCTIONS}\n\n{UPDATED_TASK_USER_INSTRUCTIONS}\n\n{PROJECT_SEPARATOR}\n\n{PROJECT_INSTRUCTIONS}" + ), + ); + assert_eq!( + response_mock + .requests() + .iter() + .map(instruction_fragments) + .collect::>(), + vec![vec![initial.clone()], vec![initial, updated]], + ); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn thread_provider_enforces_its_own_limit_before_startup_and_sampling() -> Result<()> { + let server = start_mock_server().await; + let response_mock = responses::mount_sse_sequence( + &server, + ["initial", "full-thread-budget"] + .map(|id| sse(vec![ev_response_created(id), ev_completed(id)])) + .to_vec(), + ) + .await; + let fixture = ThreadInstructionsFixture::new(&server).await?; + let oversized = Instructions { + text: "x".repeat(approx_bytes_for_tokens(/*tokens*/ 10_001)), + source: None, + }; + let error = fixture + .test + .thread_manager + .start_thread(StartThreadOptions { + environments: Some(vec![ + fixture.test.executor_environment().selection().clone(), + ]), + thread_instructions_provider: Some(Arc::new(RecordingThreadInstructionsProvider::new( + Some(oversized.clone()), + ))), + ..StartThreadOptions::new(fixture.test.config.clone()) + }) + .await + .err() + .ok_or_else(|| anyhow!("oversized instructions must fail before thread creation"))?; + assert!(matches!( + error.details(), + CodexErrorDetails::InvalidRequest(_) + )); + assert!(error.to_string().contains("10000 estimated tokens")); + + submit_thread_turn(&fixture.thread, "inspect valid instructions").await?; + fixture.provider.set_instructions(Some(oversized)); + fixture + .provider + .set_warnings(vec!["provider refresh warning".to_string()]); + let request_count = response_mock.requests().len(); + fixture + .thread + .start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text { + text: "reject oversized instructions before sampling".to_string(), + text_elements: Vec::new(), + }])) + .await?; + let EventMsg::Warning(warning) = wait_for_event(&fixture.thread, |event| { + matches!(event, EventMsg::Warning(_) | EventMsg::Error(_)) + }) + .await + else { + panic!("provider warnings must be emitted before validation errors"); + }; + assert_eq!(warning.message, "provider refresh warning"); + let EventMsg::Error(error) = + wait_for_event(&fixture.thread, |event| matches!(event, EventMsg::Error(_))).await + else { + unreachable!(); + }; + assert!(error.message.contains("10000 estimated tokens")); + wait_for_event(&fixture.thread, |event| { + matches!(event, EventMsg::TurnComplete(_)) + }) + .await; + assert_eq!(response_mock.requests().len(), request_count); + fixture.provider.set_warnings(Vec::new()); + + // The full thread budget is independent of global instructions and wrapping. + // Changing environments must also remove the previously selected repository docs. + let host_text = "x".repeat(approx_bytes_for_tokens(/*tokens*/ 10_000)); + let expected = expected_provider_only_instruction_fragment(&format!( + "These AGENTS.md instructions replace all previously provided AGENTS.md instructions.\n\n{GLOBAL_INSTRUCTIONS}\n\n{host_text}" + )); + fixture.provider.set_instructions(Some(Instructions { + text: host_text, + source: None, + })); + fixture + .thread + .start_or_steer_turn( + TurnInputRequest::user_input(vec![UserInput::Text { + text: "inspect instructions without the previous environment".to_string(), + text_elements: Vec::new(), + }]) + .with_thread_settings(ThreadSettingsOverrides { + environments: Some(TurnEnvironmentSelections::new( + fixture.test.config.cwd.clone(), + Vec::new(), + )), + ..Default::default() + }), + ) + .await?; + wait_for_event(&fixture.thread, |event| { + matches!(event, EventMsg::TurnComplete(_)) + }) + .await; + let requests = response_mock.requests(); + assert_eq!(requests.len(), 2); + assert_eq!(instruction_fragments(&requests[1]).last(), Some(&expected)); + Ok(()) +} + +#[derive(Clone, Copy)] +enum InstructionForkSource { + LiveRollout, + OfflineHistory, + OfflinePrepared, +} + +#[test_case::test_case(InstructionForkSource::LiveRollout; "live snapshot")] +#[test_case::test_case(InstructionForkSource::OfflineHistory; "offline history provider")] +#[test_case::test_case(InstructionForkSource::OfflinePrepared; "offline prepared provider")] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn fork_preserves_thread_instructions(source: InstructionForkSource) -> Result<()> { + let server = start_mock_server().await; + let response_mock = responses::mount_sse_sequence( + &server, + ["parent", "fork", "updated-fork"] + .map(|id| sse(vec![ev_response_created(id), ev_completed(id)])) + .to_vec(), + ) + .await; + let history_mode = match source { + InstructionForkSource::LiveRollout | InstructionForkSource::OfflineHistory => { + ThreadHistoryMode::Legacy + } + InstructionForkSource::OfflinePrepared => ThreadHistoryMode::Paginated, + }; + let mut builder = test_codex() + .with_config(|config| { + config + .features + .enable(Feature::Sqlite) + .expect("enable local persistence"); + }) + .with_history_mode(history_mode); + let test = builder.build_with_auto_env(&server).await?; + let parent_provider = Arc::new(RecordingThreadInstructionsProvider::with_text( + TASK_USER_INSTRUCTIONS, + )); + let parent = test + .thread_manager + .start_thread(StartThreadOptions { + environments: Some(Vec::new()), + thread_instructions_provider: Some(parent_provider.clone()), + history_mode: Some(history_mode), + ..StartThreadOptions::new(test.config.clone()) + }) + .await?; + submit_thread_turn(&parent.thread, "persist parent instructions").await?; + parent.thread.ensure_rollout_materialized().await; + parent.thread.flush_rollout().await?; + let parent_id = parent.thread_id; + let rollout_path = parent + .thread + .rollout_path() + .expect("persisted parent rollout"); + let parent_loads = parent_provider.load_count(); + let offline = !matches!(source, InstructionForkSource::LiveRollout); + if offline { + parent.thread.shutdown_and_wait().await?; + test.thread_manager + .remove_thread_if_matches(&parent_id, &parent.thread) + .await; + } + let fork_provider = Arc::new(RecordingThreadInstructionsProvider::with_text( + TASK_USER_INSTRUCTIONS, + )); + let options = StartThreadOptions { + environments: Some(Vec::new()), + thread_instructions_provider: offline + .then(|| fork_provider.clone() as Arc), + ..StartThreadOptions::new(test.config.clone()) + }; + let fork = match source { + InstructionForkSource::LiveRollout => { + test.thread_manager + .fork_thread(ForkSnapshot::Interrupted, options, rollout_path) + .await? + } + InstructionForkSource::OfflineHistory => { + let stored = test + .thread_store + .load_latest_model_context(LoadThreadHistoryParams { + thread_id: parent_id, + include_archived: true, + }) + .await?; + let history = InitialHistory::Resumed(ResumedHistory { + conversation_id: parent_id, + history: Arc::new(stored.items), + rollout_path: None, + }); + test.thread_manager + .fork_thread_from_history(ForkSnapshot::Interrupted, options, history) + .await? + } + InstructionForkSource::OfflinePrepared => { + let prepared = test + .thread_store + .prepare_fork(PrepareForkParams { + thread_id: parent_id, + boundary: ForkBoundary::Latest, + }) + .await?; + test.thread_manager + .fork_prepared_thread(options, prepared) + .await? + } + }; + submit_thread_turn(&fork.thread, "continue with inherited instructions").await?; + // Live forks inherit only a snapshot; offline forks retain their own provider. + // Neither path may call the original task's provider. + for provider in [&parent_provider, &fork_provider] { + provider.set_instructions(Some(Instructions { + text: UPDATED_TASK_USER_INSTRUCTIONS.to_string(), + source: None, + })); + } + submit_thread_turn(&fork.thread, "continue after source update").await?; + assert_eq!(parent_provider.load_count(), parent_loads); + let initial = expected_provider_only_instruction_fragment(TASK_USER_INSTRUCTIONS); + let final_fragments = if offline { + vec![ + initial.clone(), + expected_provider_only_instruction_fragment(&format!( + "These AGENTS.md instructions replace all previously provided AGENTS.md instructions.\n\n{UPDATED_TASK_USER_INSTRUCTIONS}" + )), + ] + } else { + vec![initial.clone()] + }; + assert_eq!( + response_mock + .requests() + .iter() + .map(instruction_fragments) + .collect::>(), + vec![vec![initial.clone()], vec![initial], final_fragments] + ); + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn thread_provider_lives_with_its_session_across_resume() -> Result<()> { + let server = start_mock_server().await; + let response_mock = responses::mount_sse_sequence( + &server, + ["updated-live-session", "cold-session"] + .map(|id| sse(vec![ev_response_created(id), ev_completed(id)])) + .to_vec(), + ) + .await; + let mut builder = test_codex(); + let test = builder.build_with_auto_env(&server).await?; + let provider = Arc::new(RecordingThreadInstructionsProvider::new( + /*instructions*/ None, + )); + let started = test + .thread_manager + .start_thread(StartThreadOptions { + environments: Some(Vec::new()), + thread_instructions_provider: Some(provider.clone()), + ..StartThreadOptions::new(test.config.clone()) + }) + .await?; + assert_eq!(provider.load_count(), 1); + let (_, history) = persisted_resume_history(&started.thread).await?; + let cold_provider = Arc::new(RecordingThreadInstructionsProvider::with_text( + "cold session instructions", + )); + provider.set_instructions(Some(Instructions { + text: UPDATED_TASK_USER_INSTRUCTIONS.to_string(), + source: None, + })); + let same_provider = Arc::new(RecordingThreadInstructionsProvider::with_text( + UPDATED_TASK_USER_INSTRUCTIONS, + )); + for supplied in [ + None, + Some(same_provider.clone() as Arc), + Some(cold_provider.clone() as Arc), + ] { + let resumed = test + .thread_manager + .start_thread(StartThreadOptions { + initial_history: history.clone(), + thread_instructions_provider: supplied, + ..StartThreadOptions::new(test.config.clone()) + }) + .await?; + assert!(Arc::ptr_eq(&resumed.thread, &started.thread)); + assert_eq!(provider.load_count(), 1); + assert_eq!(same_provider.load_count(), 0); + assert_eq!(cold_provider.load_count(), 0); + } + + // Reattachment does no loading; the existing Session pulls at the next model step. + submit_thread_turn(&started.thread, "load instructions added after creation").await?; + assert_eq!(provider.load_count(), 2); + assert_eq!(same_provider.load_count(), 0); + let (_, history) = persisted_resume_history(&started.thread).await?; + started.thread.shutdown_and_wait().await?; + let resumed = test + .thread_manager + .start_thread(StartThreadOptions { + initial_history: history, + environments: Some(Vec::new()), + thread_instructions_provider: Some(cold_provider.clone()), + ..StartThreadOptions::new(test.config.clone()) + }) + .await?; + assert!(!Arc::ptr_eq(&resumed.thread, &started.thread)); + assert_eq!(cold_provider.load_count(), 1); + submit_thread_turn(&resumed.thread, "load cold session instructions").await?; + assert_eq!(provider.load_count(), 2); + let requests = response_mock.requests(); + assert_single_instruction_fragment( + &requests[0], + &expected_provider_only_instruction_fragment(UPDATED_TASK_USER_INSTRUCTIONS), + ); + assert_eq!( + instruction_fragments(&requests[1]).last(), + Some(&expected_provider_only_instruction_fragment( + "These AGENTS.md instructions replace all previously provided AGENTS.md instructions.\n\ncold session instructions", + )), + ); + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn fresh_thread_composes_global_before_project_and_reports_sources() -> Result<()> { // Set up one global source, one project source, and two ordinary model turns. diff --git a/codex-rs/core/tests/suite/agents_md_refresh.rs b/codex-rs/core/tests/suite/agents_md_refresh.rs index dfd6c7f4e6..3155f6a14d 100644 --- a/codex-rs/core/tests/suite/agents_md_refresh.rs +++ b/codex-rs/core/tests/suite/agents_md_refresh.rs @@ -207,7 +207,7 @@ async fn interrupting_a_provider_read_allows_the_next_turn_to_refresh() -> Resul started: Notify, } impl UserInstructionsProvider for GatedProvider { - fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_> { + fn load_user_instructions(&self) -> LoadInstructionsFuture<'_> { Box::pin(async move { if self.block_next.swap(/*val*/ false, Ordering::SeqCst) { self.started.notify_one(); diff --git a/codex-rs/ext/extension-api/src/lib.rs b/codex-rs/ext/extension-api/src/lib.rs index 3daadfe87a..d92e5c6a65 100644 --- a/codex-rs/ext/extension-api/src/lib.rs +++ b/codex-rs/ext/extension-api/src/lib.rs @@ -104,6 +104,7 @@ pub use state::ExtensionData; pub use state::ExtensionDataInit; pub use turn_admission::TurnStartAdmission; pub use user_instructions::Instructions; -pub use user_instructions::LoadUserInstructionsFuture; +pub use user_instructions::LoadInstructionsFuture; pub use user_instructions::LoadedUserInstructions; +pub use user_instructions::ThreadInstructionsProvider; pub use user_instructions::UserInstructionsProvider; diff --git a/codex-rs/ext/extension-api/src/user_instructions.rs b/codex-rs/ext/extension-api/src/user_instructions.rs index 5e262722b3..b6c9a4b36f 100644 --- a/codex-rs/ext/extension-api/src/user_instructions.rs +++ b/codex-rs/ext/extension-api/src/user_instructions.rs @@ -5,17 +5,17 @@ use codex_utils_absolute_path::AbsolutePathBuf; /// Instructions supplied by the host. /// -/// `source` must be an absolute filesystem path because the app-server -/// `instructionSources` API currently exposes instruction sources as -/// `AbsolutePathBuf` values. +/// Filesystem-backed instructions retain their absolute source path for the +/// app-server `instructionSources` API. Other host-provided instructions do +/// not report a filesystem source. // TODO(anp): Replace the absolute path with a more general instruction-source // abstraction when non-filesystem providers need first-class attribution. #[derive(Clone, Debug, Eq, PartialEq)] pub struct Instructions { /// Model-visible instruction text. pub text: String, - /// Absolute filesystem path reported through `instructionSources`. - pub source: AbsolutePathBuf, + /// Absolute filesystem path reported through `instructionSources`, if any. + pub source: Option, } /// Result of loading host-provided user instructions. @@ -28,10 +28,24 @@ pub struct LoadedUserInstructions { pub warnings: Vec, } -/// Future returned by a [`UserInstructionsProvider`]. -pub type LoadUserInstructionsFuture<'a> = +/// Future returned by an instruction provider. +pub type LoadInstructionsFuture<'a> = Pin + Send + 'a>>; +/// Loads host-provided instructions that apply to one root thread. +/// +/// These instructions follow the global [`UserInstructionsProvider`] snapshot +/// and precede repository instructions. A result with no instructions or +/// blank instructions clears only the thread-scoped contribution. Core retains +/// the provider and reads it at startup and when capturing model-request context. +/// Implementations own fetching and caching; repeated reads should be cheap and +/// return a coherent snapshot. On a recoverable fetch failure, return the last +/// usable snapshot with warnings rather than an empty result that clears it. +pub trait ThreadInstructionsProvider: Send + Sync { + /// Loads the current snapshot for the provider's root thread. + fn load_thread_instructions(&self) -> LoadInstructionsFuture<'_>; +} + /// Loads global user instructions shared across root threads. /// /// Core reads this provider at startup and when capturing model-request context. @@ -40,5 +54,5 @@ pub type LoadUserInstructionsFuture<'a> = /// while still returning usable fallback instructions when available. pub trait UserInstructionsProvider: Send + Sync { /// Loads the current global snapshot for a root runtime. - fn load_user_instructions(&self) -> LoadUserInstructionsFuture<'_>; + fn load_user_instructions(&self) -> LoadInstructionsFuture<'_>; } diff --git a/codex-rs/realtime-webrtc/src/client.rs b/codex-rs/realtime-webrtc/src/client.rs index aa56305646..ed2f50e37f 100644 --- a/codex-rs/realtime-webrtc/src/client.rs +++ b/codex-rs/realtime-webrtc/src/client.rs @@ -25,12 +25,28 @@ const RUNTIME_INITIALIZATION_DEADLINE: Duration = Duration::from_secs(/*secs*/ 3 pub enum ConnectionError { NegotiationTimedOut, Failed, + HelperStartup, + RuntimeInitialization, + Transport, + AudioDevices, + AudioControls, + AudioSession, + Shutdown, } impl std::fmt::Display for ConnectionError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { Self::NegotiationTimedOut => "voice negotiation timed out", Self::Failed => "voice connection failed", + Self::HelperStartup => "voice helper could not start", + Self::RuntimeInitialization => "voice audio runtime could not initialize", + Self::Transport => "voice transport could not connect", + Self::AudioDevices => { + "voice audio devices could not open; check microphone and speaker setup" + } + Self::AudioControls => "voice audio controls failed", + Self::AudioSession => "voice audio session stopped unexpectedly", + Self::Shutdown => "voice helper could not shut down cleanly", }) } } @@ -149,6 +165,16 @@ impl VoiceHost { "voice helper must be inside the physical package" ); let environment = child_environment(std::env::vars_os()); + #[cfg(target_os = "linux")] + let environment = { + let mut environment = environment; + if let Some(directory) = + crate::linux_alsa::plugin_directory(crate::linux_alsa::PLUGIN_DIRECTORIES) + { + environment.insert("ALSA_PLUGIN_DIR".to_owned(), directory.to_owned()); + } + environment + }; let SpawnedProcess { session, stdout_rx, diff --git a/codex-rs/realtime-webrtc/src/client_tests.rs b/codex-rs/realtime-webrtc/src/client_tests.rs index 7f77d719ff..5fde5b791f 100644 --- a/codex-rs/realtime-webrtc/src/client_tests.rs +++ b/codex-rs/realtime-webrtc/src/client_tests.rs @@ -14,6 +14,7 @@ fn forwards_only_explicit_device_network_and_os_inputs() { ("DYLD_INSERT_LIBRARIES", "loader"), ("PATH", "project"), ("GST_PLUGIN_PATH", "plugins"), + ("ALSA_PLUGIN_DIR", "untrusted-alsa-plugins"), ("GST_REGISTRY", "untrusted-registry"), ("GST_REGISTRY_FORK", "yes"), ("OPENAI_API_KEY", "secret"), diff --git a/codex-rs/realtime-webrtc/src/lib.rs b/codex-rs/realtime-webrtc/src/lib.rs index 6ce6b1d732..d4776cfcdd 100644 --- a/codex-rs/realtime-webrtc/src/lib.rs +++ b/codex-rs/realtime-webrtc/src/lib.rs @@ -1,5 +1,7 @@ mod client; mod helper_exit; +#[cfg(any(target_os = "linux", test))] +mod linux_alsa; mod message_reader; mod protocol; mod session; diff --git a/codex-rs/realtime-webrtc/src/linux_alsa.rs b/codex-rs/realtime-webrtc/src/linux_alsa.rs new file mode 100644 index 0000000000..bd98648bd5 --- /dev/null +++ b/codex-rs/realtime-webrtc/src/linux_alsa.rs @@ -0,0 +1,28 @@ +//! Locate system ALSA plugins for the statically linked Linux voice helper. +//! +//! Bazel's ALSA defaults to /usr/lib/alsa-lib. Distribution plugins may instead +//! live in a multiarch or lib64 directory. Only fixed system paths are admitted; +//! caller-provided plugin directories and loader search paths remain excluded. + +use std::path::Path; + +#[cfg(target_os = "linux")] +pub(crate) const PLUGIN_DIRECTORIES: &[&str] = &[ + #[cfg(target_arch = "x86_64")] + "/usr/lib/x86_64-linux-gnu/alsa-lib", + #[cfg(target_arch = "aarch64")] + "/usr/lib/aarch64-linux-gnu/alsa-lib", + "/usr/lib64/alsa-lib", + "/usr/lib/alsa-lib", +]; + +pub(crate) fn plugin_directory<'a>(candidates: &[&'a str]) -> Option<&'a str> { + candidates + .iter() + .copied() + .find(|path| Path::new(path).is_dir()) +} + +#[cfg(test)] +#[path = "linux_alsa_tests.rs"] +mod tests; diff --git a/codex-rs/realtime-webrtc/src/linux_alsa_tests.rs b/codex-rs/realtime-webrtc/src/linux_alsa_tests.rs new file mode 100644 index 0000000000..9afd97e5a3 --- /dev/null +++ b/codex-rs/realtime-webrtc/src/linux_alsa_tests.rs @@ -0,0 +1,38 @@ +//! Verify fallback among installed layouts while ignoring non-directory entries. + +use pretty_assertions::assert_eq; + +use super::plugin_directory; + +#[test] +fn discovers_available_layout_without_selecting_a_file() -> std::io::Result<()> { + let root = std::env::temp_dir().join(format!( + "codex-alsa-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + std::fs::create_dir(&root)?; + let multiarch = root.join("multiarch"); + let lib64 = root.join("lib64"); + let generic = root.join("generic"); + let candidates = [ + multiarch.to_str().unwrap(), + lib64.to_str().unwrap(), + generic.to_str().unwrap(), + ]; + + assert_eq!(plugin_directory(&candidates), None); + std::fs::create_dir(&generic)?; + assert_eq!(plugin_directory(&candidates), Some(candidates[2])); + std::fs::write(&lib64, "not a directory")?; + assert_eq!(plugin_directory(&candidates), Some(candidates[2])); + std::fs::remove_file(&lib64)?; + std::fs::create_dir(&lib64)?; + assert_eq!(plugin_directory(&candidates), Some(candidates[1])); + std::fs::create_dir(&multiarch)?; + assert_eq!(plugin_directory(&candidates), Some(candidates[0])); + std::fs::remove_dir_all(root) +} diff --git a/codex-rs/realtime-webrtc/src/session.rs b/codex-rs/realtime-webrtc/src/session.rs index a0f95b4bfe..fee0c39669 100644 --- a/codex-rs/realtime-webrtc/src/session.rs +++ b/codex-rs/realtime-webrtc/src/session.rs @@ -16,6 +16,7 @@ use futures::future::Abortable; use tokio::sync::mpsc; use crate::AudioControls; +use crate::ConnectionError; use crate::SessionDescription; use crate::VoiceHost; @@ -134,30 +135,37 @@ impl RealtimeWebrtcSession { .spawn(move || { let task = async { let host = report_failure( - "connect", + ConnectionError::HelperStartup, VoiceHost::connect(&package, &build_commit).await, )?; - let host = - report_failure("initialize_runtime", host.initialize_runtime().await)?; + let host = report_failure( + ConnectionError::RuntimeInitialization, + host.initialize_runtime().await, + )?; let (host, sdp) = - report_failure("start_transport", host.start_transport().await)?; + report_failure(ConnectionError::Transport, host.start_transport().await)?; offer - .send(sdp.into_sdp()) + .send(Ok(sdp.into_sdp())) .map_err(|_| anyhow::anyhow!("voice startup cancelled"))?; run(host, receiver, &state, &controls).await }; let result = runtime.block_on(Abortable::new(Abortable::new(task, abort), stopped)); - if !matches!(result, Err(_) | Ok(Err(_)) | Ok(Ok(Ok(())))) { + if let Ok(Ok(Err(error))) = result { + let failure = error + .downcast_ref::() + .copied() + .unwrap_or(ConnectionError::Failed); + let _ = offer.try_send(Err(failure)); *state .error .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) = - Some("Voice helper stopped unexpectedly.".into()); + Some(failure.to_string()); } })?; let offer_sdp = result .recv_timeout(STARTUP_WAIT) - .map_err(|_| anyhow::anyhow!("voice startup failed"))?; + .map_err(|_| anyhow::anyhow!("voice startup failed"))??; Ok(StartedRealtimeWebrtcSession { offer_sdp, handle }) } } @@ -267,35 +275,39 @@ async fn run( biased; command = commands.recv() => match command { Some(Command::Answer(sdp, complete)) if !connected => { - host = match host.apply_answer(sdp).await { + let startup = async { + let mut host = report_failure(ConnectionError::Transport, host.apply_answer(sdp).await)?; + host = report_failure(ConnectionError::AudioDevices, host.open_devices().await)?; + let applied = startup_controls(&mut commands, controls, |initial| { + host.begin_audio_controls(initial) + }); + let applied = report_failure(ConnectionError::AudioControls, applied)?; + report_failure(ConnectionError::AudioControls, applied.await)?; + Ok::<_, anyhow::Error>(host) + }.await; + host = match startup { Ok(host) => host, Err(error) => { - let failure = error.downcast_ref::() - .copied().unwrap_or(crate::ConnectionError::Failed); + let failure = error.downcast_ref::() + .copied().unwrap_or(ConnectionError::Failed); let _ = complete.send(Err(failure)); // This failure is delivered by the startup completion only. return Ok(()); } }; - host = report_failure("open_devices", host.open_devices().await)?; - let applied = startup_controls(&mut commands, controls, |initial| { - host.begin_audio_controls(initial) - }); - let applied = report_failure("queue_startup_controls", applied)?; - report_failure("apply_startup_controls", applied.await)?; connected = true; let _ = complete.send(Ok(())); } Some(Command::Controls(next)) => { if connected { - report_failure("set_audio_controls", host.set_audio_controls(next).await)?; + report_failure(ConnectionError::AudioControls, host.set_audio_controls(next).await)?; } } Some(Command::Answer(..)) => anyhow::bail!("voice answer already applied"), - None => return report_failure("close", host.close().await), + None => return report_failure(ConnectionError::Shutdown, host.close().await), }, _ = poll.tick() => { - let audio = report_failure("inspect_audio", host.inspect_audio().await)?; + let audio = report_failure(ConnectionError::AudioSession, host.inspect_audio().await)?; state.microphone.fetch_max(audio.microphone_peak, Ordering::Release); state.speaker.fetch_max(audio.speaker_peak, Ordering::Release); } @@ -304,8 +316,8 @@ async fn run( } // Keep diagnostics bounded and independent of untyped native, SDP, or device error text. -fn report_failure(stage: &'static str, result: Result) -> Result { - result.inspect_err(|error| { +fn report_failure(stage: ConnectionError, result: Result) -> Result { + result.map_err(|error| { let kind = if error.is::() { "timeout" } else if let Some(error) = error.downcast_ref::() { @@ -319,7 +331,13 @@ fn report_failure(stage: &'static str, result: Result) -> Result { } else { "other" }; - tracing::warn!(stage, kind, "voice session operation failed"); + tracing::warn!(?stage, kind, "voice session operation failed"); + let failure = error + .downcast_ref::() + .copied() + .unwrap_or(stage); + // Replace the original error instead of retaining a potentially sensitive source chain. + anyhow::Error::new(failure) }) } diff --git a/codex-rs/realtime-webrtc/src/session_tests.rs b/codex-rs/realtime-webrtc/src/session_tests.rs index a5afc9b912..87227e6516 100644 --- a/codex-rs/realtime-webrtc/src/session_tests.rs +++ b/codex-rs/realtime-webrtc/src/session_tests.rs @@ -198,3 +198,26 @@ async fn startup_completion_preserves_classified_failure() { assert_eq!(handle.take_error(), None); } } + +#[test] +fn classified_errors_discard_sensitive_sources_but_preserve_retry_classification() { + let error = report_failure::<()>( + ConnectionError::AudioDevices, + Err(anyhow::anyhow!("synthetic-secret-sdp-device-path")), + ) + .unwrap_err(); + assert_eq!( + format!("{error:#}"), + ConnectionError::AudioDevices.to_string() + ); + assert!(!format!("{error:?}").contains("synthetic-secret")); + let error = report_failure::<()>( + ConnectionError::Transport, + Err(ConnectionError::NegotiationTimedOut.into()), + ) + .unwrap_err(); + assert_eq!( + error.downcast_ref::(), + Some(&ConnectionError::NegotiationTimedOut) + ); +} diff --git a/codex-rs/realtime-webrtc/tests/common/mod.rs b/codex-rs/realtime-webrtc/tests/common/mod.rs index 4996a90777..63f8884b48 100644 --- a/codex-rs/realtime-webrtc/tests/common/mod.rs +++ b/codex-rs/realtime-webrtc/tests/common/mod.rs @@ -153,6 +153,9 @@ fn helper() -> Result<()> { if root.join("hold-initialization").exists() { wait_for(|| root.join("release").exists())?; } + if root.join("fail-initialization").exists() { + anyhow::bail!("synthetic private runtime diagnostic"); + } stage = Stage::Offer; Message::RuntimeReady {} } @@ -176,6 +179,9 @@ fn helper() -> Result<()> { Message::TransportReady {} } Message::OpenDevices {} if stage == Stage::Devices => { + if root.join("fail-devices").exists() { + anyhow::bail!("synthetic private device diagnostic"); + } stage = Stage::Controls; Message::DevicesOpened {} } diff --git a/codex-rs/realtime-webrtc/tests/session_actor.rs b/codex-rs/realtime-webrtc/tests/session_actor.rs index 44a8917c1b..ded26288ce 100644 --- a/codex-rs/realtime-webrtc/tests/session_actor.rs +++ b/codex-rs/realtime-webrtc/tests/session_actor.rs @@ -67,7 +67,10 @@ fn startup_controls_meters_and_helper_loss() -> Result<()> { error = handle.take_error(); error.is_some() })?; - assert_eq!(error.as_deref(), Some("Voice helper stopped unexpectedly.")); + assert_eq!( + error.as_deref(), + Some("voice audio session stopped unexpectedly") + ); assert_eq!(handle.take_error(), None); Ok(()) } @@ -107,3 +110,47 @@ fn last_owner_drop_reaps_helper() -> Result<()> { drop(started); common::wait_for_helper_reaped(&root) } + +#[test] +fn device_failure_reaches_startup_completion_without_duplicate_error() -> Result<()> { + let Some(root) = + common::package("device_failure_reaches_startup_completion_without_duplicate_error")? + else { + return Ok(()); + }; + fs::write(root.join("fail-devices"), [])?; + fs::write(root.join("release"), [])?; + let (_abort, registration) = AbortHandle::new_pair(); + let started = RealtimeWebrtcSession::start(registration)?; + assert_eq!( + started.handle.apply_answer_sdp("synthetic-answer".into()), + Err(codex_realtime_webrtc::ConnectionError::AudioDevices) + ); + #[cfg(unix)] + common::wait_for_helper_reaped(&root)?; + assert_eq!(started.handle.take_error(), None); + Ok(()) +} + +#[test] +fn runtime_failure_reaches_offer_caller_without_native_error_text() -> Result<()> { + let Some(root) = + common::package("runtime_failure_reaches_offer_caller_without_native_error_text")? + else { + return Ok(()); + }; + fs::write(root.join("fail-initialization"), [])?; + let (_abort, registration) = AbortHandle::new_pair(); + let error = RealtimeWebrtcSession::start(registration).unwrap_err(); + assert_eq!( + error.downcast_ref::(), + Some(&codex_realtime_webrtc::ConnectionError::RuntimeInitialization) + ); + assert_eq!( + format!("{error:#}"), + "voice audio runtime could not initialize" + ); + #[cfg(unix)] + common::wait_for_helper_reaped(&root)?; + Ok(()) +} diff --git a/codex-rs/tui/src/app/agents_overview.rs b/codex-rs/tui/src/app/agents_overview.rs index 336f1a0318..0e78fedba3 100644 --- a/codex-rs/tui/src/app/agents_overview.rs +++ b/codex-rs/tui/src/app/agents_overview.rs @@ -334,8 +334,10 @@ impl App { root_thread_id: ThreadId, ) -> color_eyre::Result { if self.current_displayed_thread_id() == Some(root_thread_id) - && !self.thread_unavailable(root_thread_id) + && (!self.thread_unavailable(root_thread_id) + || self.chat_widget.is_external_writer_view()) { + // Keep the displayed read-only snapshot frozen; R explicitly retries attachment. if let Ok(mut state) = self.agents_overview.view_state.lock() { state.completion = Some(crate::bottom_pane::ViewCompletion::Accepted); } diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index 6f1f808b87..e0257f2dde 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -389,7 +389,7 @@ impl App { return self.archive_current_thread(tui, app_server).await; } AppEvent::DeleteCurrentThread => { - return Ok(self.delete_current_thread(app_server).await); + return self.delete_current_thread(tui, app_server).await; } AppEvent::ForkCurrentSession { name } => { self.session_telemetry.counter( @@ -3540,28 +3540,61 @@ impl App { pub(super) async fn delete_current_thread( &mut self, + tui: &mut tui::Tui, app_server: &mut AppServerSession, - ) -> AppRunControl { + ) -> Result { let Some(thread_id) = self.active_thread_id.or(self.chat_widget.thread_id()) else { self.chat_widget .add_error_message("A thread must start before it can be deleted.".to_string()); - return AppRunControl::Continue; + return Ok(AppRunControl::Continue); }; if self.side_threads.contains_key(&thread_id) { self.chat_widget.add_error_message( "'/delete' is unavailable in side conversations. Press Ctrl+C to return to the main thread first." .to_string(), ); - return AppRunControl::Continue; + return Ok(AppRunControl::Continue); } - match app_server.thread_delete(thread_id).await { - Ok(()) => AppRunControl::Exit(ExitReason::ThreadRemoved), + if !matches!(self.app_server_target, AppServerTarget::Embedded) { + self.shutdown_side_threads(app_server).await; + if !self.side_threads.is_empty() { + return Ok(AppRunControl::Continue); + } + } + + Ok(match app_server.thread_delete(thread_id).await { + Ok(()) if matches!(self.app_server_target, AppServerTarget::Embedded) => { + AppRunControl::Exit(ExitReason::ThreadRemoved) + } + Ok(()) => { + self.track_agents_overview_notification(&ServerNotification::ThreadDeleted( + codex_app_server_protocol::ThreadDeletedNotification { + thread_id: thread_id.to_string(), + }, + )); + self.discard_thread_local_state(thread_id).await; + self.agents_overview.input_states.remove(&thread_id); + self.agents_overview.dispatched_requests.remove(&thread_id); + self.reset_for_thread_switch(tui)?; + self.pending_thread_switch_resets += 1; + self.app_event_tx + .send(AppEvent::ResetTranscriptForThreadSwitch); + self.reset_thread_event_state(); + let init = self.chatwidget_init_for_forked_or_resumed_thread( + tui, + self.config.clone(), + /*initial_user_message*/ None, + ); + self.replace_chat_widget(ChatWidget::new_with_app_event(init)); + self.open_agents_overview(app_server, AgentsOverviewFocus::List); + AppRunControl::Continue + } Err(err) => { self.chat_widget .add_error_message(format!("Failed to delete current thread: {err}")); AppRunControl::Continue } - } + }) } } diff --git a/codex-rs/tui/src/app/input.rs b/codex-rs/tui/src/app/input.rs index b5350192e1..a4cbedb631 100644 --- a/codex-rs/tui/src/app/input.rs +++ b/codex-rs/tui/src/app/input.rs @@ -252,6 +252,13 @@ impl App { && key_event.kind == KeyEventKind::Press { let modifiers = key_event.modifiers; + if key_event.code == KeyCode::Esc + && modifiers == KeyModifiers::NONE + && !matches!(self.app_server_target, AppServerTarget::Embedded) + { + self.open_agents_overview(app_server, AgentsOverviewFocus::List); + return; + } let quit = match key_event.code { KeyCode::Esc => modifiers == KeyModifiers::NONE, KeyCode::Char('q' | 'Q') => { diff --git a/codex-rs/tui/src/app/startup.rs b/codex-rs/tui/src/app/startup.rs index 02356f9181..1faea715d7 100644 --- a/codex-rs/tui/src/app/startup.rs +++ b/codex-rs/tui/src/app/startup.rs @@ -7,6 +7,7 @@ use super::agents_overview_view::AgentsOverviewFocus; use super::reconnect::ReconnectState; use super::*; use crate::session_start::SessionStartAction; +use crate::session_start::SessionStartOutcome; use crate::session_start::cancel_session_start; use crate::session_start::complete_session_start; use crate::unarchive_prompt::run_unarchive_prompt; @@ -375,7 +376,7 @@ impl App { &session_selection, SessionSelection::StartFresh | SessionSelection::Exit ); - let start_in_agents_overview = + let mut start_in_agents_overview = matches!(&session_selection, SessionSelection::AgentsOverview); let mut read_only_thread = false; let (mut chat_widget, initial_started_thread) = match session_selection { @@ -502,14 +503,15 @@ impl App { }; let resumed = if read_only_thread { match resumed { - Ok(resumed) => resumed, + Ok(resumed) => Some(resumed), Err(err) => return shutdown_on_startup_error(app_server, err).await, } } else { let action = SessionStartAction::Resume(model_settings); - let Some(resumed) = complete_session_start( + let resumed = complete_session_start( &mut app_server, &config, + &app_server_target, &target_session, action, resumed, @@ -518,11 +520,14 @@ impl App { run_unarchive_prompt(tui, target_session.thread_id, action).await }, ) - .await? - else { - return Ok(cancel_session_start(app_server).await); - }; - resumed + .await?; + match resumed { + SessionStartOutcome::Started(started) => Some(*started), + SessionStartOutcome::CommandCenter => None, + SessionStartOutcome::Exit => { + return Ok(cancel_session_start(app_server).await); + } + } }; let init = crate::chatwidget::ChatWidgetInit { local_settings: local_settings.clone(), @@ -535,7 +540,8 @@ impl App { initial_images.clone(), // CLI prompt args are plain strings, so they don't provide element ranges. Vec::new(), - ), + ) + .filter(|_| resumed.is_some()), enhanced_keys_supported, has_chatgpt_account, requires_openai_auth, @@ -552,7 +558,7 @@ impl App { .clone(), session_telemetry: session_telemetry.clone(), }; - (ChatWidget::new_with_app_event(init), Some(resumed)) + (ChatWidget::new_with_app_event(init), resumed) } SessionSelection::Fork(target_session) => { let explicit_permission_override = @@ -598,9 +604,10 @@ impl App { Err(err) => return shutdown_on_startup_error(app_server, err).await, }; let action = SessionStartAction::Fork(permission_mode); - let Some(forked) = complete_session_start( + let forked = complete_session_start( &mut app_server, &config, + &app_server_target, &target_session, action, forked, @@ -609,17 +616,23 @@ impl App { run_unarchive_prompt(tui, target_session.thread_id, action).await }, ) - .await? - else { - return Ok(cancel_session_start(app_server).await); + .await?; + let forked = match forked { + SessionStartOutcome::Started(started) => Some(*started), + SessionStartOutcome::CommandCenter => None, + SessionStartOutcome::Exit => { + return Ok(cancel_session_start(app_server).await); + } }; - if let Some(worktree) = managed_worktree.as_ref() - && let Err(err) = worktree.bind(forked.session.thread_id) - { - return shutdown_on_startup_error(app_server, err).await; - } - if config.model_reasoning_effort.is_none() { - config.model_reasoning_effort = forked.session.reasoning_effort.clone(); + if let Some(forked) = &forked { + if let Some(worktree) = managed_worktree.as_ref() + && let Err(err) = worktree.bind(forked.session.thread_id) + { + return shutdown_on_startup_error(app_server, err).await; + } + if config.model_reasoning_effort.is_none() { + config.model_reasoning_effort = forked.session.reasoning_effort.clone(); + } } let init = crate::chatwidget::ChatWidgetInit { local_settings: local_settings.clone(), @@ -632,7 +645,8 @@ impl App { initial_images.clone(), // CLI prompt args are plain strings, so they don't provide element ranges. Vec::new(), - ), + ) + .filter(|_| forked.is_some()), enhanced_keys_supported, has_chatgpt_account, requires_openai_auth, @@ -649,9 +663,15 @@ impl App { .clone(), session_telemetry: session_telemetry.clone(), }; - (ChatWidget::new_with_app_event(init), Some(forked)) + (ChatWidget::new_with_app_event(init), forked) } }; + let startup_session_cancelled = initial_started_thread.is_none() + && !pending_startup_thread_start + && !start_in_agents_overview; + if startup_session_cancelled { + start_in_agents_overview = true; + } chat_widget.note_rendered_width(tui.terminal.last_known_screen_size.width); chat_widget.remote_connection = remote_connection; chat_widget.set_local_worktree_operations(!crate::uses_remote_workspace_or_environment( @@ -896,7 +916,9 @@ See the Codex keymap documentation for supported actions and examples." { return shutdown_on_startup_error(app_server, err).await; } - let mut pending_startup_draft = Some(startup_draft.into_draft()); + // Input for the cancelled resume/fork must not appear in a later selected session. + let mut pending_startup_draft = + (!startup_session_cancelled).then(|| startup_draft.into_draft()); if app_event_rx.is_empty() && !app.has_queued_startup_protected_request() { app.chat_widget .restore_startup_draft_when_ready(&mut pending_startup_draft); diff --git a/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs b/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs index 4698065e7f..0a4d5502e1 100644 --- a/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs +++ b/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs @@ -617,58 +617,130 @@ async fn make_history_test_app() -> Result<(App, tempfile::TempDir)> { } #[tokio::test] -async fn deleting_remote_thread_omits_disconnect_guidance() -> Result<()> { - let (mut app, codex_home) = make_history_test_app().await?; - let thread_id = ThreadId::from_string( - &create_fake_rollout( - codex_home.path(), - "2026-01-01T00-00-00", - "2026-01-01T00:00:00Z", - "Saved user message", - Some(app.config.model_provider_id.as_str()), - /*git_info*/ None, - ) - .expect("create rollout"), - )?; - let (mut server, _, proxy) = start_recording_app_server( - &app.config, - /*blocked_thread_list*/ None, - /*failed_thread_name*/ None, - ) - .await?; - let resumed = server - .resume_thread( - &app.local_settings, - app.config.clone(), - thread_id, - crate::app_server_session::ResumeModelSettings::RestoreFromThread, +async fn delete_current_thread_navigates_only_after_success() -> Result<()> { + let endpoint = crate::resolve_remote_addr("ws://127.0.0.1:4500")?; + for target in [ + AppServerTarget::Embedded, + AppServerTarget::LocalDaemon { + endpoint: endpoint.clone(), + }, + AppServerTarget::Remote { endpoint }, + ] { + let (mut app, _codex_home) = make_history_test_app().await?; + let thread_id = + create_history_rollout(&app.config, ThreadHistoryMode::Legacy, "delete me")?; + let (mut server, requests, proxy) = start_recording_app_server( + &app.config, + /*blocked_thread_list*/ None, + /*failed_thread_name*/ None, ) .await?; - app.app_server_target = AppServerTarget::Remote { - endpoint: crate::resolve_remote_addr("ws://127.0.0.1:4500")?, - }; - app.active_thread_id = Some(thread_id); - app.chat_widget.handle_thread_session(resumed.session); - let mut tui = crate::tui::test_support::make_test_tui()?; - // Keep the large dispatcher future off the Windows test thread's stack. - let AppRunControl::Exit(reason) = - Box::pin(app.handle_event(&mut tui, &mut server, AppEvent::DeleteCurrentThread)).await? - else { - panic!("removing the current thread must exit"); - }; - assert_matches!(reason, ExitReason::ThreadRemoved); - let mut exit_info = app.exit_info(reason); - exit_info.token_usage = TokenUsage { - output_tokens: 2, - total_tokens: 2, - ..Default::default() - }; - assert_eq!( - exit_info.format_exit_messages(/*color_enabled*/ false), - vec!["Token usage: total=2 input=0 output=2".to_string()] - ); - server.shutdown().await?; - proxy.await??; + app.app_server_target = target; + let mut tui = crate::tui::test_support::make_test_tui()?; + let missing_id = ThreadId::new(); + app.active_thread_id = Some(missing_id); + app.chat_widget.insert_str("Retained on failure"); + assert_matches!( + app.delete_current_thread(&mut tui, &mut server).await?, + AppRunControl::Continue + ); + assert_eq!(app.active_thread_id, Some(missing_id)); + assert_eq!( + app.chat_widget.composer_text_with_pending(), + "Retained on failure" + ); + assert!(!app.chat_widget.has_active_view()); + + let resumed = server + .resume_thread( + &app.local_settings, + app.config.clone(), + thread_id, + crate::app_server_session::ResumeModelSettings::RestoreFromThread, + ) + .await?; + app.active_thread_id = Some(thread_id); + app.enqueue_primary_thread_session(resumed.session.clone(), resumed.turns) + .await?; + app.chat_widget.handle_thread_session(resumed.session); + let mut side_config = app.config.clone(); + side_config.ephemeral = true; + let side = server + .fork_side_thread(&app.local_settings, side_config.clone(), thread_id) + .await?; + let side_id = side.session.thread_id; + app.side_threads + .insert(side_id, SideThreadState::new(thread_id)); + if !matches!(app.app_server_target, AppServerTarget::Embedded) { + // The recording proxy rejects the next unsubscribe after a failed fork. + side_config.cwd = side_config.cwd.join("failure"); + assert!( + server + .fork_side_thread(&app.local_settings, side_config, thread_id) + .await + .is_err() + ); + assert_matches!( + app.delete_current_thread(&mut tui, &mut server).await?, + AppRunControl::Continue + ); + assert_eq!(app.active_thread_id, Some(thread_id)); + assert!(app.side_threads.contains_key(&side_id)); + assert!(!app.chat_widget.has_active_view()); + assert_eq!(recorded_params(&requests, "thread/delete").len(), 1); + } + let (tx, mut events) = tokio::sync::mpsc::unbounded_channel(); + app.app_event_tx = AppEventSender::new(tx); + let control = + Box::pin(app.handle_event(&mut tui, &mut server, AppEvent::DeleteCurrentThread)) + .await?; + assert!( + server + .thread_read(thread_id, /*include_turns*/ false) + .await + .is_err() + ); + if matches!(app.app_server_target, AppServerTarget::Embedded) { + assert_matches!(control, AppRunControl::Exit(ExitReason::ThreadRemoved)); + assert!(recorded_params(&requests, "thread/unsubscribe").is_empty()); + } else { + assert_matches!(control, AppRunControl::Continue); + assert!(app.side_threads.is_empty()); + assert_eq!( + recorded_params(&requests, "thread/unsubscribe"), + vec![serde_json::json!({"threadId": side_id.to_string()}); 2] + ); + loop { + let event = tokio::time::timeout(Duration::from_secs(/*secs*/ 5), events.recv()) + .await? + .expect("command center refresh"); + let refreshed = matches!(&event, AppEvent::AgentsOverviewThreadsLoaded { .. }); + Box::pin(app.handle_event(&mut tui, &mut server, event)).await?; + if refreshed { + break; + } + } + assert_eq!( + ( + app.active_thread_id, + app.primary_thread_id, + app.chat_widget.thread_id() + ), + (None, None, None) + ); + assert!(app.chat_widget.composer_is_empty()); + assert!(app.chat_widget.has_active_view()); + } + assert_eq!( + recorded_params(&requests, "thread/delete"), + vec![ + serde_json::json!({"threadId": missing_id.to_string()}), + serde_json::json!({"threadId": thread_id.to_string()}), + ] + ); + server.shutdown().await?; + proxy.await??; + } Ok(()) } @@ -4294,3 +4366,85 @@ fn session_lifecycle_avoids_redundant_subagent_metadata_reads() -> Result<()> { mod new_session_tests; #[path = "startup_defaults_tests.rs"] mod startup_defaults_tests; + +#[tokio::test] +async fn external_writer_escape_preserves_snapshot_and_explicit_quits() -> Result<()> { + let endpoint = crate::resolve_remote_addr("ws://127.0.0.1:4500")?; + for target in [ + AppServerTarget::LocalDaemon { + endpoint: endpoint.clone(), + }, + AppServerTarget::Remote { endpoint }, + ] { + let (mut app, _codex_home) = make_history_test_app().await?; + let thread_id = + create_history_rollout(&app.config, ThreadHistoryMode::Legacy, "owned elsewhere")?; + let (mut server, requests, proxy) = start_recording_app_server( + &app.config, + /*blocked_thread_list*/ None, + /*failed_thread_name*/ None, + ) + .await?; + app.app_server_target = target; + app.active_thread_id = Some(thread_id); + app.primary_thread_id = Some(thread_id); + app.ensure_thread_channel(thread_id).mark_external_writer(); + app.chat_widget.show_external_writer_thread(); + app.chat_widget.insert_str("Retained draft"); + let (tx, mut events) = tokio::sync::mpsc::unbounded_channel(); + app.app_event_tx = AppEventSender::new(tx); + let mut tui = crate::tui::test_support::make_test_tui()?; + for key in [ + KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE), + KeyEvent::new(KeyCode::Char('Q'), KeyModifiers::SHIFT), + KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL), + ] { + app.handle_key_event(&mut tui, &mut server, key).await; + assert_matches!(events.try_recv(), Ok(AppEvent::Exit(ExitMode::Immediate))); + } + app.handle_key_event( + &mut tui, + &mut server, + KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), + ) + .await; + assert!(app.chat_widget.has_active_view()); + loop { + let event = tokio::time::timeout(Duration::from_secs(/*secs*/ 5), events.recv()) + .await? + .expect("command center refresh"); + let refreshed = matches!(&event, AppEvent::AgentsOverviewThreadsLoaded { .. }); + Box::pin(app.handle_event(&mut tui, &mut server, event)).await?; + if refreshed { + break; + } + } + assert_matches!( + Box::pin(app.handle_event( + &mut tui, + &mut server, + AppEvent::SelectAgentsOverviewThread { thread_id }, + )) + .await?, + AppRunControl::Continue + ); + assert!(!app.chat_widget.has_active_view()); + assert_eq!(app.active_thread_id, Some(thread_id)); + assert!(app.chat_widget.is_external_writer_view()); + assert_eq!( + app.chat_widget.composer_text_with_pending(), + "Retained draft" + ); + assert_eq!( + app.thread_event_channels[&thread_id].attachment(), + ThreadEventAttachment::ExternalWriter + ); + assert!(requests.lock().unwrap().iter().all(|request| matches!( + request.method.as_str(), + "thread/read" | "thread/list" | "thread/loaded/list" | "thread/turns/list" + ))); + server.shutdown().await?; + proxy.await??; + } + Ok(()) +} diff --git a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__command_popup__tests__command_popup_default_items.snap b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__command_popup__tests__command_popup_default_items.snap index 0cadee0486..91b583d43e 100644 --- a/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__command_popup__tests__command_popup_default_items.snap +++ b/codex-rs/tui/src/bottom_pane/snapshots/codex_tui__bottom_pane__command_popup__tests__command_popup_default_items.snap @@ -17,7 +17,7 @@ expression: commands /rename - rename the current thread /new - start a new chat during a conversation /archive - archive this session -/delete - permanently delete this session and exit +/delete - permanently delete this session /resume - resume a saved chat /fork - fork the current chat /app - continue this session in the Desktop app diff --git a/codex-rs/tui/src/chatwidget/realtime.rs b/codex-rs/tui/src/chatwidget/realtime.rs index 186c289596..64a50f1df6 100644 --- a/codex-rs/tui/src/chatwidget/realtime.rs +++ b/codex-rs/tui/src/chatwidget/realtime.rs @@ -1599,6 +1599,7 @@ impl ChatWidget { { self.record_realtime_failure(); } + let failed = self.realtime_conversation.failure_recorded; self.reset_realtime_conversation(); if let Some(thread_id) = retry_thread_id { // The old backend is closed. A late peer result belongs to its attempt ID. @@ -1615,6 +1616,7 @@ impl ChatWidget { } if let Some(reason) = reason && reason != "error" + && !(failed && reason == "requested") { self.add_info_message( format!("Voice conversation ended: {reason}"), diff --git a/codex-rs/tui/src/chatwidget/realtime_tests/lifecycle.rs b/codex-rs/tui/src/chatwidget/realtime_tests/lifecycle.rs index fe39a83754..aedbcc2fb1 100644 --- a/codex-rs/tui/src/chatwidget/realtime_tests/lifecycle.rs +++ b/codex-rs/tui/src/chatwidget/realtime_tests/lifecycle.rs @@ -471,3 +471,34 @@ async fn startup_retry_never_retries_twice_or_retries_other_errors_or_active_ses assert!(ops.try_recv().is_err(), "no retry may be queued"); } } + +#[tokio::test] +async fn failure_cleanup_does_not_attribute_stop_to_the_user() { + let (mut chat, _sender, mut events, _ops) = make_chatwidget_manual_with_sender().await; + // A local failure initiates backend cleanup; its acknowledgement is still "requested". + chat.realtime_conversation.phase = RealtimeConversationPhase::Stopping; + chat.realtime_conversation.failure_recorded = true; + chat.on_realtime_error(format!( + "Failed to connect voice mode: {}", + codex_realtime_webrtc::ConnectionError::AudioDevices + )); + chat.on_realtime_conversation_closed(Some("requested".into())); + assert_eq!( + chat.realtime_conversation.phase, + RealtimeConversationPhase::Inactive + ); + let rendered = std::iter::from_fn(|| events.try_recv().ok()) + .filter_map(|event| match event { + AppEvent::InsertHistoryCell(cell) => Some( + cell.display_lines(/*width*/ 80) + .into_iter() + .map(|line| line.to_string()) + .collect::>() + .join("\n"), + ), + _ => None, + }) + .collect::>() + .join("\n"); + insta::assert_snapshot!("voice_device_failure_cleanup", rendered); +} diff --git a/codex-rs/tui/src/chatwidget/realtime_tests/snapshots/codex_tui__chatwidget__realtime__tests__lifecycle__voice_device_failure_cleanup.snap b/codex-rs/tui/src/chatwidget/realtime_tests/snapshots/codex_tui__chatwidget__realtime__tests__lifecycle__voice_device_failure_cleanup.snap new file mode 100644 index 0000000000..a7a017646f --- /dev/null +++ b/codex-rs/tui/src/chatwidget/realtime_tests/snapshots/codex_tui__chatwidget__realtime__tests__lifecycle__voice_device_failure_cleanup.snap @@ -0,0 +1,6 @@ +--- +source: tui/src/chatwidget/realtime_tests/lifecycle.rs +assertion_line: 498 +expression: rendered +--- +■ Failed to connect voice mode: voice audio devices could not open; check microphone and speaker setup diff --git a/codex-rs/tui/src/chatwidget/rendering.rs b/codex-rs/tui/src/chatwidget/rendering.rs index 8163ae2549..d24f11411d 100644 --- a/codex-rs/tui/src/chatwidget/rendering.rs +++ b/codex-rs/tui/src/chatwidget/rendering.rs @@ -13,6 +13,7 @@ use ratatui::widgets::Block; use std::cell::Cell; struct ExternalWriterNotice { + command_center_available: bool, transcript_hint: Option, } @@ -87,19 +88,18 @@ impl ExternalWriterNotice { } fn footer_lines(&self, width: u16) -> Vec> { - let mut items = vec![ - ("r".to_string(), "retry".to_string()), - ( - format!( - "{}/{}/{}", - crate::key_hint::plain(KeyCode::Esc).display_label(), - crate::key_hint::ctrl(KeyCode::Char('c')).display_label(), - crate::key_hint::plain(KeyCode::Char('q')).display_label(), - ) - .replace(" + ", "+"), - "exit".to_string(), - ), + let mut items = vec![("r".to_string(), "retry".to_string())]; + let escape = crate::key_hint::plain(KeyCode::Esc).display_label(); + let mut quit_keys = vec![ + crate::key_hint::ctrl(KeyCode::Char('c')).display_label(), + crate::key_hint::plain(KeyCode::Char('q')).display_label(), ]; + if self.command_center_available { + items.push((escape, "command center".to_string())); + } else { + quit_keys.insert(/*index*/ 0, escape); + } + items.push((quit_keys.join("/").replace(" + ", "+"), "exit".to_string())); if let Some(hint) = self.transcript_hint { items.push(( hint.display_label().replace(" + ", "+"), @@ -193,6 +193,7 @@ impl ChatWidget { } let bottom = if self.external_writer_view && !self.bottom_pane.has_active_view() { RenderableItem::Owned(Box::new(ExternalWriterNotice { + command_center_available: self.remote_connection.is_some(), transcript_hint: self.bottom_pane.transcript_shortcut_hint(), })) } else { diff --git a/codex-rs/tui/src/chatwidget/rendering_tests.rs b/codex-rs/tui/src/chatwidget/rendering_tests.rs index 2d7abb97ae..902bc0ec55 100644 --- a/codex-rs/tui/src/chatwidget/rendering_tests.rs +++ b/codex-rs/tui/src/chatwidget/rendering_tests.rs @@ -367,3 +367,25 @@ async fn removing_active_cell_invalidates_layout_before_reusing_its_identity() { render_frame(&widget, /*width*/ 80); assert_eq!(desired_height_calls.load(Ordering::Relaxed), 2); } + +#[tokio::test] +async fn external_writer_notice_offers_command_center_on_shared_servers() { + let endpoint = crate::resolve_remote_addr("ws://127.0.0.1:4500").unwrap(); + for target in [ + crate::AppServerTarget::LocalDaemon { + endpoint: endpoint.clone(), + }, + crate::AppServerTarget::Remote { endpoint }, + ] { + let (mut widget, _sender, _events, _operations) = + make_chatwidget_manual_with_sender().await; + widget.remote_connection = crate::status::remote_connection::remote_connection_status_value( + &target, /*server_version*/ None, + ); + widget.show_external_writer_thread(); + for width in [60, 100] { + let rendered = crate::chatwidget::tests::render_bottom_popup(&widget, width); + insta::assert_snapshot!(format!("external_writer_command_center_{width}"), rendered); + } + } +} diff --git a/codex-rs/tui/src/chatwidget/slash_dispatch.rs b/codex-rs/tui/src/chatwidget/slash_dispatch.rs index 147b41d1bb..333f435998 100644 --- a/codex-rs/tui/src/chatwidget/slash_dispatch.rs +++ b/codex-rs/tui/src/chatwidget/slash_dispatch.rs @@ -227,7 +227,12 @@ impl ChatWidget { ..Default::default() }, SelectionItem { - name: "Yes, delete and exit".to_string(), + name: if self.remote_connection.is_some() { + "Yes, delete and return to command center" + } else { + "Yes, delete and exit" + } + .to_string(), description: Some("Permanently delete this session now".to_string()), actions: vec![Box::new(|tx| { tx.send(AppEvent::DeleteCurrentThread); diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__rendering__tests__external_writer_command_center_100.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__rendering__tests__external_writer_command_center_100.snap new file mode 100644 index 0000000000..3067a2cc1c --- /dev/null +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__rendering__tests__external_writer_command_center_100.snap @@ -0,0 +1,10 @@ +--- +source: tui/src/chatwidget/rendering_tests.rs +expression: rendered +--- + + + 🔒 This conversation is open in another app R to Retry + Close it there and press R to continue here. + + r retry esc command center ctrl+c/q exit ctrl+t transcript diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__rendering__tests__external_writer_command_center_60.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__rendering__tests__external_writer_command_center_60.snap new file mode 100644 index 0000000000..de6bbf771e --- /dev/null +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__rendering__tests__external_writer_command_center_60.snap @@ -0,0 +1,11 @@ +--- +source: tui/src/chatwidget/rendering_tests.rs +expression: rendered +--- + + + 🔒 This conversation is open in another app R to Retry + Close it there and press R to continue here. + + r retry esc command center ctrl+c/q exit ctrl+t + transcript diff --git a/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__slash_delete_confirmation_command_center.snap b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__slash_delete_confirmation_command_center.snap new file mode 100644 index 0000000000..5ca9c50a18 --- /dev/null +++ b/codex-rs/tui/src/chatwidget/snapshots/codex_tui__chatwidget__tests__slash_delete_confirmation_command_center.snap @@ -0,0 +1,12 @@ +--- +source: tui/src/chatwidget/tests/slash_commands.rs +expression: popup +--- + Delete this session? + Cannot be undone. Subagent threads will also be deleted. + +› 1. No, keep this session Return to the current session + 2. Yes, delete and return to command center Permanently delete this session + now + + Press enter to confirm or esc to go back diff --git a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs index e558424b55..bb39b86708 100644 --- a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs +++ b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs @@ -3108,20 +3108,31 @@ async fn slash_archive_confirmation_requests_current_thread_archive() { #[tokio::test] async fn slash_delete_confirmation_requests_current_thread_delete() { - let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; - - chat.dispatch_command(SlashCommand::Delete); - - assert!(chat.bottom_pane.has_active_view()); - assert_matches!(rx.try_recv(), Err(TryRecvError::Empty)); - - let popup = render_bottom_popup(&chat, /*width*/ 80); - assert_chatwidget_snapshot!("slash_delete_confirmation_popup", popup); - - chat.handle_key_event(KeyEvent::from(KeyCode::Down)); - chat.handle_key_event(KeyEvent::from(KeyCode::Enter)); - - assert_matches!(rx.try_recv(), Ok(AppEvent::DeleteCurrentThread)); + let endpoint = crate::resolve_remote_addr("ws://127.0.0.1:4500").unwrap(); + for target in [ + crate::AppServerTarget::Embedded, + crate::AppServerTarget::LocalDaemon { + endpoint: endpoint.clone(), + }, + crate::AppServerTarget::Remote { endpoint }, + ] { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.remote_connection = crate::status::remote_connection::remote_connection_status_value( + &target, /*server_version*/ None, + ); + chat.dispatch_command(SlashCommand::Delete); + assert!(chat.bottom_pane.has_active_view()); + assert_matches!(rx.try_recv(), Err(TryRecvError::Empty)); + let popup = render_bottom_popup(&chat, /*width*/ 80); + if matches!(target, crate::AppServerTarget::Embedded) { + assert_chatwidget_snapshot!("slash_delete_confirmation_popup", popup); + } else { + assert_chatwidget_snapshot!("slash_delete_confirmation_command_center", popup); + } + chat.handle_key_event(KeyEvent::from(KeyCode::Down)); + chat.handle_key_event(KeyEvent::from(KeyCode::Enter)); + assert_matches!(rx.try_recv(), Ok(AppEvent::DeleteCurrentThread)); + } } #[tokio::test] diff --git a/codex-rs/tui/src/session_start.rs b/codex-rs/tui/src/session_start.rs index 1c562fafb9..045c0e770d 100644 --- a/codex-rs/tui/src/session_start.rs +++ b/codex-rs/tui/src/session_start.rs @@ -53,16 +53,24 @@ impl SessionStartAction { } } +#[derive(Debug)] +pub(crate) enum SessionStartOutcome { + Started(Box), + CommandCenter, + Exit, +} + pub(crate) async fn complete_session_start( app_server: &mut AppServerSession, config: &Config, + app_server_target: &crate::AppServerTarget, target: &SessionTarget, action: SessionStartAction, initial_result: Result, confirm: impl AsyncFnOnce() -> Result, -) -> Result> { +) -> Result { match initial_result { - Ok(started) => return Ok(Some(started)), + Ok(started) => return Ok(SessionStartOutcome::Started(Box::new(started))), Err(err) => { // Match the requested ID as well as the server's archive guidance: an unrelated // startup failure must never cause us to unarchive a session. @@ -75,8 +83,16 @@ pub(crate) async fn complete_session_start( } } - if confirm().await? == UnarchiveChoice::Cancel { - return Ok(None); + match confirm().await? { + UnarchiveChoice::Cancel => { + return Ok(match app_server_target { + crate::AppServerTarget::LocalDaemon { .. } + | crate::AppServerTarget::Remote { .. } => SessionStartOutcome::CommandCenter, + crate::AppServerTarget::Embedded => SessionStartOutcome::Exit, + }); + } + UnarchiveChoice::Quit => return Ok(SessionStartOutcome::Exit), + UnarchiveChoice::Unarchive => {} } app_server @@ -87,7 +103,7 @@ pub(crate) async fn complete_session_start( action .start(app_server, config, target) .await - .map(Some) + .map(|started| SessionStartOutcome::Started(Box::new(started))) .map_err(|err| session_start_error(action.verb(), target, err)) } diff --git a/codex-rs/tui/src/session_start_tests.rs b/codex-rs/tui/src/session_start_tests.rs index 888d0bf237..c05839714b 100644 --- a/codex-rs/tui/src/session_start_tests.rs +++ b/codex-rs/tui/src/session_start_tests.rs @@ -14,9 +14,18 @@ async fn start_session( target: &SessionTarget, action: SessionStartAction, confirm: impl AsyncFnOnce() -> Result, -) -> Result> { +) -> Result { let initial_result = action.start(app_server, config, target).await; - complete_session_start(app_server, config, target, action, initial_result, confirm).await + complete_session_start( + app_server, + config, + &crate::AppServerTarget::Embedded, + target, + action, + initial_result, + confirm, + ) + .await } #[tokio::test] @@ -55,26 +64,61 @@ async fn archived_session_requires_confirmation_before_resume_or_fork() -> Resul let mut app_server = crate::start_embedded_app_server_for_picker(&config).await?; let prompts = Cell::new(0); - let cancelled = start_session(&mut app_server, &config, &target, action, async || { - prompts.set(prompts.get() + 1); - Ok(UnarchiveChoice::Cancel) - }) - .await?; - assert!(cancelled.is_none()); - assert_eq!( - (prompts.get(), archived_path.exists(), active_path.exists()), - (1, true, false) - ); + let endpoint = crate::resolve_remote_addr("ws://127.0.0.1:4500")?; + for server_target in [ + crate::AppServerTarget::LocalDaemon { + endpoint: endpoint.clone(), + }, + crate::AppServerTarget::Remote { endpoint }, + crate::AppServerTarget::Embedded, + ] { + for choice in [UnarchiveChoice::Cancel, UnarchiveChoice::Quit] { + let initial = action.start(&mut app_server, &config, &target).await; + let outcome = complete_session_start( + &mut app_server, + &config, + &server_target, + &target, + action, + initial, + async || { + prompts.set(prompts.get() + 1); + Ok(choice) + }, + ) + .await?; + assert_eq!( + matches!(outcome, SessionStartOutcome::CommandCenter), + choice == UnarchiveChoice::Cancel + && !matches!(server_target, crate::AppServerTarget::Embedded), + ); + assert_eq!( + (archived_path.exists(), active_path.exists()), + (true, false) + ); + assert!( + app_server + .thread_loaded_list(Default::default()) + .await? + .data + .is_empty() + ); + } + } - let started = start_session(&mut app_server, &config, &target, action, async || { - prompts.set(prompts.get() + 1); - Ok(UnarchiveChoice::Unarchive) - }) - .await? - .expect("confirmed session should start"); + assert_eq!(prompts.get(), 6); + let SessionStartOutcome::Started(started) = + start_session(&mut app_server, &config, &target, action, async || { + prompts.set(prompts.get() + 1); + Ok(UnarchiveChoice::Unarchive) + }) + .await? + else { + panic!("confirmed session should start") + }; assert_eq!( (prompts.get(), archived_path.exists(), active_path.exists()), - (2, false, true) + (7, false, true) ); match action { SessionStartAction::Resume(_) => { @@ -92,7 +136,7 @@ async fn archived_session_requires_confirmation_before_resume_or_fork() -> Resul } } - let resumed = start_session( + let SessionStartOutcome::Started(resumed) = start_session( &mut app_server, &config, &target, @@ -100,7 +144,9 @@ async fn archived_session_requires_confirmation_before_resume_or_fork() -> Resul async || panic!("active sessions must not prompt"), ) .await? - .expect("active session should start"); + else { + panic!("active session should start") + }; assert_eq!(resumed.session.thread_id, target.thread_id); let missing = SessionTarget { diff --git a/codex-rs/tui/src/slash_command.rs b/codex-rs/tui/src/slash_command.rs index ecfadd8758..0a3e107b88 100644 --- a/codex-rs/tui/src/slash_command.rs +++ b/codex-rs/tui/src/slash_command.rs @@ -96,7 +96,7 @@ impl SlashCommand { SlashCommand::Rename => "rename the current thread", SlashCommand::Resume => "resume a saved chat", SlashCommand::Archive => "archive this session", - SlashCommand::Delete => "permanently delete this session and exit", + SlashCommand::Delete => "permanently delete this session", SlashCommand::Clear => "clear the terminal and start a new chat", SlashCommand::Fork => "fork the current chat", SlashCommand::Worktree => "start or continue a conversation in a new worktree", diff --git a/codex-rs/tui/src/unarchive_prompt.rs b/codex-rs/tui/src/unarchive_prompt.rs index 3f51822762..8c26fc4767 100644 --- a/codex-rs/tui/src/unarchive_prompt.rs +++ b/codex-rs/tui/src/unarchive_prompt.rs @@ -26,6 +26,7 @@ use tokio_stream::StreamExt; pub(crate) enum UnarchiveChoice { Unarchive, Cancel, + Quit, } pub(crate) async fn run_unarchive_prompt( @@ -44,7 +45,7 @@ pub(crate) async fn run_unarchive_prompt( guard.draw(&screen)?; loop { let Some(event) = events.next().await else { - return Ok(UnarchiveChoice::Cancel); + return Ok(UnarchiveChoice::Quit); }; guard.tui.screen_size_for_event(&event)?; match event { @@ -140,13 +141,13 @@ impl UnarchivePrompt { if key.modifiers.contains(KeyModifiers::CONTROL) && matches!(key.code, KeyCode::Char('c') | KeyCode::Char('d')) { - return Some(UnarchiveChoice::Cancel); + return Some(UnarchiveChoice::Quit); } match key.code { KeyCode::Up | KeyCode::Down | KeyCode::Char('k' | 'j') => { self.highlighted = match self.highlighted { UnarchiveChoice::Unarchive => UnarchiveChoice::Cancel, - UnarchiveChoice::Cancel => UnarchiveChoice::Unarchive, + UnarchiveChoice::Cancel | UnarchiveChoice::Quit => UnarchiveChoice::Unarchive, }; None } diff --git a/codex-rs/tui/src/unarchive_prompt_tests.rs b/codex-rs/tui/src/unarchive_prompt_tests.rs index b3175f6eeb..d2b20bb715 100644 --- a/codex-rs/tui/src/unarchive_prompt_tests.rs +++ b/codex-rs/tui/src/unarchive_prompt_tests.rs @@ -76,11 +76,16 @@ fn confirmation_requires_an_accept_key() { for key in [ KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE), KeyEvent::new(KeyCode::Char('n'), KeyModifiers::NONE), - KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL), - KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL), + KeyEvent::new(KeyCode::Char('2'), KeyModifiers::NONE), ] { assert_eq!(screen.handle_key(key), Some(UnarchiveChoice::Cancel)); } + for key in ['c', 'd'] { + assert_eq!( + screen.handle_key(KeyEvent::new(KeyCode::Char(key), KeyModifiers::CONTROL)), + Some(UnarchiveChoice::Quit), + ); + } assert_eq!( screen.handle_key(KeyEvent::new_with_kind( KeyCode::Enter, diff --git a/codex-rs/voice-host/src/devices.rs b/codex-rs/voice-host/src/devices.rs index 1f1d58f1f1..41d34d4148 100644 --- a/codex-rs/voice-host/src/devices.rs +++ b/codex-rs/voice-host/src/devices.rs @@ -243,9 +243,12 @@ fn bounded_stream_config( if min > max { return Err(io::Error::other("unsupported audio callback size range")); } - // Aim for 10 ms without consuming the queue's service headroom. - // Do not fall back to the backend's potentially much larger default buffer. - let frames = (config.sample_rate / 100).clamp(min, max); + // ALSA allocates two periods. A 20 ms ring can be smaller than one PipeWire + // graph cycle (e.g. 2048 frames at 48 kHz), silently losing capture samples + // every cycle. Give Linux a bounded 100 ms ring instead, while retaining + // 10 ms callbacks elsewhere and rejecting incompatible device ranges below. + let periods_per_second = if cfg!(target_os = "linux") { 20 } else { 100 }; + let frames = (config.sample_rate / periods_per_second).clamp(min, max); let callback_duration = Duration::from_secs_f64(f64::from(frames) / f64::from(config.sample_rate)); // Backends may deliver smaller callbacks than requested. Packing makes queue diff --git a/codex-rs/voice-host/src/devices_tests.rs b/codex-rs/voice-host/src/devices_tests.rs index 2bfcffee77..e9f7b40dab 100644 --- a/codex-rs/voice-host/src/devices_tests.rs +++ b/codex-rs/voice-host/src/devices_tests.rs @@ -72,17 +72,22 @@ fn startup_output_waits_for_service_and_later_overflow_discards_reference() { #[test] fn callback_configuration_fits_supported_range_and_actual_queue() { - for (rate, min, max, frames) in [ - (96_000, 0, u32::MAX, 960), - (96_000, 1, 16, 16), - (384_000, 1, 64, 64), - (8_000, 7_680, 8_192, 7_680), - (384_000, 1, 16, 16), - (96_000, 1, 15, 15), - (48_000, 1, 128, 128), - (48_000, 2_048, 16_384, 2_048), - (384_000, 6_016, 16_384, 6_016), + for (rate, min, max, other_frames, linux_frames) in [ + (96_000, 0, u32::MAX, 960, 4_800), + (96_000, 1, 16, 16, 16), + (384_000, 1, 64, 64, 64), + (8_000, 7_680, 8_192, 7_680, 7_680), + (384_000, 1, 16, 16, 16), + (96_000, 1, 15, 15, 15), + (48_000, 1, 128, 128, 128), + (48_000, 2_048, 16_384, 2_048, 2_400), + (384_000, 6_016, 16_384, 6_016, 8_192), ] { + let frames = if cfg!(target_os = "linux") { + linux_frames + } else { + other_frames + }; let supported = cpal::SupportedStreamConfig::new( /*channels*/ 2, rate, @@ -116,6 +121,77 @@ fn callback_configuration_fits_supported_range_and_actual_queue() { } } +#[test] +#[cfg(target_os = "linux")] +fn pipewire_graph_cycles_support_capture_and_playback() { + let supported = cpal::SupportedStreamConfig::new( + /*channels*/ 2, + /*sample_rate*/ 48_000, + cpal::SupportedBufferSize::Range { + min: 1, + max: 16_384, + }, + cpal::SampleFormat::F32, + ); + let cpal::BufferSize::Fixed(period) = bounded_stream_config(&supported).unwrap().buffer_size + else { + panic!("callback size must be bounded"); + }; + let mut processor = + processing::Processor::new(/*input_rate*/ 48_000, /*output_rate*/ 48_000).unwrap(); + let mut packer = FramePacker::default(); + let buffers = Arc::new(Buffers::new( + /*input_rate*/ 48_000, /*output_rate*/ 48_000, + )); + let start = Instant::now(); + let mut available = 0; + let mut packets = Vec::new(); + // Model measured 2048-frame graph cycles and ALSA's two-period ring. + // Packing and encoding must sustain 20 ms packets despite this cadence. + for cycle in 1..=48 { + available = (available + 2_048).min(2 * period); + let now = start + Duration::from_secs_f64(f64::from(cycle * 2_048) / 48_000.0); + while available >= period { + let captured = now - Duration::from_secs_f64(f64::from(available) / 48_000.0); + packer.discard_capture_gap(captured, /*rate*/ 48_000.0); + for offset in (0..period as usize).step_by(BLOCK) { + let frame = Frame { + samples: [0.25; BLOCK], + len: BLOCK.min(period as usize - offset), + at: captured + Duration::from_secs_f64(offset as f64 / 48_000.0), + generation: 2, + }; + assert!(packer.push(frame, /*rate*/ 48_000.0, &buffers.capture)); + } + available -= period; + while let Some(frame) = buffers.capture.pop() { + packets.extend(processor.capture(&frame, || now).unwrap()); + } + } + } + assert!(packets.len() >= 90, "capture starved Opus"); + + // Queue a full output callback without racing the producer. + Buffers::set_disabled(&buffers.speaker, /*disabled*/ false).unwrap(); + buffers.serviced.store(true, Ordering::Release); + let port = PlaybackPort::new(buffers.clone(), /*rate*/ 48_000); + let writer = port.writer(); + let bytes = 0.25_f32.to_le_bytes().repeat(period as usize); + for chunk in bytes.chunks(BLOCK * 4) { + assert_eq!(writer.write(chunk).unwrap(), chunk.len()); + } + let mut output = vec![0.0_f32; period as usize]; + render_output( + &mut output, + /*channels*/ 1, + /*rate*/ 48_000.0, + Instant::now(), + &buffers, + &mut OutputState::default(), + ); + assert_eq!(output, vec![0.25; period as usize]); +} + #[test] fn callback_configuration_fits_dynamic_queue_with_following_callbacks() { for min in [6_016, 8_192] { diff --git a/codex-rs/voice-host/src/playback.rs b/codex-rs/voice-host/src/playback.rs index 282f32aa5d..2fffbfc6b4 100644 --- a/codex-rs/voice-host/src/playback.rs +++ b/codex-rs/voice-host/src/playback.rs @@ -84,7 +84,11 @@ impl PlaybackWriter { .map_err(|_| "speaker writer failed")?; let deadline = Instant::now() + Duration::from_millis(/*millis*/ 100); let buffers = &self.state.buffers; - let limit = (self.state.rate / 25).min((BLOCK * buffers.playback.capacity()) as u32); + // Linux requests 50 ms callbacks; retain two callbacks of audio so a + // callback can fill without racing the producer. Keep the queue cap. + let windows_per_second = if cfg!(target_os = "linux") { 10 } else { 25 }; + let limit = (self.state.rate / windows_per_second) + .min((BLOCK * buffers.playback.capacity()) as u32); loop { if self.epoch % 2 == 1 || buffers.speaker.load(Ordering::Acquire) != self.epoch diff --git a/codex-rs/voice-host/src/playback_tests.rs b/codex-rs/voice-host/src/playback_tests.rs index a3a45d7db8..70773a1519 100644 --- a/codex-rs/voice-host/src/playback_tests.rs +++ b/codex-rs/voice-host/src/playback_tests.rs @@ -35,7 +35,9 @@ fn partial_writes_account_for_samples_until_the_device_consumes_them() { fn suppression_cancels_a_full_writer_and_old_writers_cannot_resume() { let (buffers, port, writer) = active(/*rate*/ 8000); let bytes = vec![0; BLOCK * 4]; - writer.write(&bytes).unwrap(); + for _ in 0..if cfg!(target_os = "linux") { 3 } else { 1 } { + writer.write(&bytes).unwrap(); + } let waiting = std::thread::spawn(move || writer.write(&bytes)); Buffers::set_disabled(&buffers.speaker, /*disabled*/ true).unwrap(); assert_eq!(waiting.join().unwrap(), Err("speaker writer cancelled")); @@ -129,11 +131,17 @@ fn invalid_samples_fail_but_stalled_consumption_drops_and_resumes() { writer.write(&f32::NAN.to_le_bytes()), Err("invalid speaker sample") ); - writer.write(&vec![0; BLOCK * 4]).unwrap(); + let blocks = if cfg!(target_os = "linux") { 3 } else { 1 }; + for _ in 0..blocks { + writer.write(&vec![0; BLOCK * 4]).unwrap(); + } assert_eq!(writer.write(&vec![0; BLOCK * 4]), Ok(BLOCK * 4)); - assert_eq!(buffers.queued.load(Ordering::Acquire), BLOCK as u32); + assert_eq!( + buffers.queued.load(Ordering::Acquire), + (blocks * BLOCK) as u32 + ); let mut playback = Playback::default(); - for _ in 0..BLOCK { + for _ in 0..blocks * BLOCK { assert!(playback.next(&buffers).is_some()); } assert_eq!(writer.write(&vec![0; BLOCK * 4]), Ok(BLOCK * 4)); diff --git a/patches/BUILD.bazel b/patches/BUILD.bazel index 2afcf05d1e..7b58251d8a 100644 --- a/patches/BUILD.bazel +++ b/patches/BUILD.bazel @@ -6,11 +6,13 @@ exports_files([ "llvm_windows_mingw_compat.patch", "rules_rust_build_script_tools_transition.patch", "rules_rust_group_build_script_arg_files.patch", + "rules_rust_windows_execroot_separators.patch", "rules_rust_windows_msvc_direct_link_args.patch", "rules_rust_windows_process_wrapper_skip_temp_outputs.patch", "rules_cc_rusty_v8_custom_libcxx.patch", "rules_foreign_cc_make_cppflags.patch", "rules_foreign_cc_make_xcompile.patch", + "rules_foreign_cc_make_msvc_stdint.patch", "rules_rs_build_script_deps_annotation.patch", "rules_rs_windows_msvc_linker.patch", "rules_rs_zlib_snapshot_urls.patch", diff --git a/patches/ring_windows_msvc_include_dirs.patch b/patches/ring_windows_msvc_include_dirs.patch index 06f944ad34..7bb49cbc2c 100644 --- a/patches/ring_windows_msvc_include_dirs.patch +++ b/patches/ring_windows_msvc_include_dirs.patch @@ -1,8 +1,7 @@ diff --git a/build.rs b/build.rs -index 9843ad8aa..573075489 100644 --- a/build.rs +++ b/build.rs -@@ -346,7 +346,28 @@ fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) { +@@ -346,7 +346,27 @@ // we want to optimize for minimizing the build tools required: No Perl, // no nasm, etc. let generated_dir = if !is_git { @@ -31,7 +30,22 @@ index 9843ad8aa..573075489 100644 } else { generate_sources_and_preassemble( &out_dir, -@@ -569,6 +591,15 @@ fn configure_cc(c: &mut cc::Build, target: &Target, c_root_dir: &Path, include_d +@@ -561,7 +581,13 @@ + let compiler = c.get_compiler(); + // FIXME: On Windows AArch64 we currently must use Clang to compile C code + let compiler = if target.os == WINDOWS && target.arch == AARCH64 && !compiler.is_like_clang() { +- let _ = c.compiler("clang"); ++ // Keep inherited MSVC flags compatible with the selected Clang driver. ++ let clang = if compiler.is_like_msvc() { ++ "clang-cl" ++ } else { ++ "clang" ++ }; ++ let _ = c.compiler(clang); + c.get_compiler() + } else { + compiler +@@ -569,6 +595,15 @@ let _ = c.include(c_root_dir.join("include")); let _ = c.include(include_dir); diff --git a/patches/rules_foreign_cc_make_msvc_stdint.patch b/patches/rules_foreign_cc_make_msvc_stdint.patch new file mode 100644 index 0000000000..f3af72841a --- /dev/null +++ b/patches/rules_foreign_cc_make_msvc_stdint.patch @@ -0,0 +1,13 @@ +diff --git a/foreign_cc/built_tools/make_build.bzl b/foreign_cc/built_tools/make_build.bzl +--- a/foreign_cc/built_tools/make_build.bzl ++++ b/foreign_cc/built_tools/make_build.bzl +@@ -35,6 +35,9 @@ + build_str += " gcc" + dist_dir = "GccRel" + else: ++ # Modern MSVC provides stdint.h, including the intmax_t types that ++ # Make's legacy Windows config otherwise replaces with macros. ++ build_str = 'export CL="$${CL:-}$$ /DHAVE_STDINT_H=1"\n' + build_str + dist_dir = "WinRel" + + script = [ diff --git a/patches/rules_rs_windows_msvc_linker.patch b/patches/rules_rs_windows_msvc_linker.patch index 66feb78569..5056ade96b 100644 --- a/patches/rules_rs_windows_msvc_linker.patch +++ b/patches/rules_rs_windows_msvc_linker.patch @@ -1,13 +1,14 @@ -# What: use Rust's bundled direct linker for Windows x86_64 MSVC toolchains. -# Scope: standard and bootstrap rules_rs Rust toolchains targeting x86_64 MSVC. +# What: use Rust's bundled direct linker for Windows x86_64 and ARM64 MSVC toolchains. +# Scope: standard and bootstrap rules_rs Rust toolchains targeting x86_64 and ARM64 MSVC. diff --git a/rs/toolchains/declare_rustc_toolchains.bzl b/rs/toolchains/declare_rustc_toolchains.bzl --- a/rs/toolchains/declare_rustc_toolchains.bzl +++ b/rs/toolchains/declare_rustc_toolchains.bzl -@@ -88,6 +88,7 @@ def declare_rustc_toolchains( +@@ -88,6 +88,8 @@ def declare_rustc_toolchains( llvm_cov = "@llvm//tools:llvm-cov", llvm_profdata = "@llvm//tools:llvm-profdata", linker = select({ ++ "@rules_rs//rs/platforms/config:aarch64-pc-windows-msvc": "{}rust-lld".format(rustc_repo_label), + "@rules_rs//rs/platforms/config:x86_64-pc-windows-msvc": "{}rust-lld".format(rustc_repo_label), "@platforms//cpu:wasm32": "{}rust-lld".format(rustc_repo_label), "@platforms//cpu:wasm64": "{}rust-lld".format(rustc_repo_label), diff --git a/patches/rules_rust_windows_execroot_separators.patch b/patches/rules_rust_windows_execroot_separators.patch new file mode 100644 index 0000000000..187b37f48c --- /dev/null +++ b/patches/rules_rust_windows_execroot_separators.patch @@ -0,0 +1,52 @@ +--- a/cargo/private/cargo_build_script_runner/lib.rs ++++ b/cargo/private/cargo_build_script_runner/lib.rs +@@ -337,9 +337,18 @@ + return "${pwd}".to_owned(); + } + +- value ++ let redacted = value + .replace(&format!("{exec_root}/"), "${pwd}/") +- .replace(&format!("{exec_root}\\"), "${pwd}\\") ++ .replace(&format!("{exec_root}\\"), "${pwd}\\"); ++ #[cfg(windows)] ++ let redacted = { ++ // pkg-config can emit forward slashes for the same Windows root. ++ let root = exec_root.replace('\\', "/"); ++ redacted ++ .replace(&format!("{root}/"), "${pwd}/") ++ .replace(&format!("{root}\\"), "${pwd}\\") ++ }; ++ redacted + } + + /// Redact for env vars: uses the generic `${out_dir}` token, resolved +@@ -630,6 +639,28 @@ + link_search_paths: + "-L${pwd}/${bazel-out/cfg/bin/pkg/_bs.out_dir}\n-L${pwd}/other/path".to_owned(), + } ++ ); ++ } ++ ++ #[cfg(windows)] ++ #[test] ++ fn windows_include_metadata_redacts_both_root_spellings() { ++ let root = r"D:\o\execroot\_main"; ++ let forward = root.replace('\\', "/"); ++ let outputs = vec![BuildScriptOutput::DepEnv(format!( ++ "INCLUDE={forward}/include;{root}\\lib;{forward}-other/include" ++ ))]; ++ assert_eq!( ++ BuildScriptOutput::outputs_to_dep_env(&outputs, "glib", root), ++ format!("DEP_GLIB_INCLUDE=${{pwd}}/include;${{pwd}}\\lib;{forward}-other/include") ++ ); ++ let directory = std::env::current_dir().unwrap(); ++ let directory = directory.to_str().unwrap(); ++ let forward = directory.replace('\\', "/"); ++ let outputs = vec![BuildScriptOutput::DepEnv(format!("INCLUDE={forward}/."))]; ++ assert_eq!( ++ BuildScriptOutput::nonhermetic_absolute_paths(&outputs, directory, ""), ++ Vec::::new() + ); + } + diff --git a/patches/rules_rust_windows_msvc_direct_link_args.patch b/patches/rules_rust_windows_msvc_direct_link_args.patch index aa5fb274e1..a8eba2ba6e 100644 --- a/patches/rules_rust_windows_msvc_direct_link_args.patch +++ b/patches/rules_rust_windows_msvc_direct_link_args.patch @@ -4,7 +4,7 @@ use_bpf_linker = toolchain.target_arch in ("bpfeb", "bpfel") and toolchain.linker - if not ld or toolchain.linker_preference == "rust" or use_bpf_linker: + use_windows_msvc_linker = ( -+ toolchain.target_arch == "x86_64" and ++ toolchain.target_arch in ("x86_64", "aarch64") and + toolchain.target_os == "windows" and + toolchain.target_abi == "msvc" and + toolchain.linker != None and diff --git a/third_party/voice/BUILD.bazel b/third_party/voice/BUILD.bazel index 3cf29ebde6..b3c5f0930e 100644 --- a/third_party/voice/BUILD.bazel +++ b/third_party/voice/BUILD.bazel @@ -133,6 +133,30 @@ alias( visibility = ["//visibility:public"], ) +# Explicitly select the installed pkgconf and pinned CMake in Windows voice CI. +native_tool_toolchain( + name = "windows_pkg_config_tool", + env = {"PKG_CONFIG": "$(execpath :pkg_config)"}, + path = "$(execpath :pkg_config)", + target = ":pkg_config", +) + +toolchain( + name = "windows_pkg_config_toolchain", + exec_compatible_with = ["@platforms//os:windows"], + toolchain = ":windows_pkg_config_tool", + toolchain_type = "@rules_foreign_cc//toolchains:pkgconfig_toolchain", + visibility = ["//visibility:public"], +) + +toolchain( + name = "windows_cmake_toolchain", + exec_compatible_with = ["@platforms//os:windows"], + toolchain = "@cmake-3.31.8-windows-x86_64//:cmake_tool", + toolchain_type = "@rules_foreign_cc//toolchains:cmake_toolchain", + visibility = ["//visibility:public"], +) + filegroup( name = "archives", srcs = [ diff --git a/third_party/voice/NOTICE.md b/third_party/voice/NOTICE.md index b6557717f7..3de0cff39b 100644 --- a/third_party/voice/NOTICE.md +++ b/third_party/voice/NOTICE.md @@ -1,6 +1,6 @@ # Native voice libraries in Codex releases -The macOS Codex release package includes dynamically linked GStreamer and GLib +Codex release packages with voice include dynamically linked GStreamer and GLib libraries and selected plugins, plus their native library dependencies. These components have their own copyrights and licenses. Their notices accompany this file in `licenses/`: `LGPL-2.1.txt` for GStreamer and GLib, @@ -12,6 +12,16 @@ The exact upstream versions, source archive URLs and SHA-256 digests are in projection and package scripts are in the public Codex source tree under `third_party/voice/`. The source commit for this package is recorded in `manifest.json`. The native libraries remain separate dynamic libraries in -`lib/` and `plugins/`; replacing them requires compatible binaries and valid -macOS code signatures. Build tools listed in `sources.json` are build inputs, -not bundled runtime libraries. +platform-specific runtime directories. Replacements must be compatible with +the package and, on macOS, have valid code signatures. Build tools listed in +`sources.json` are build inputs, not bundled runtime libraries. + +Windows packages also contain an unmodified Microsoft Visual C++ runtime DLL, +copyright Microsoft Corporation, under Microsoft's applicable software license +terms, separately from the open-source audio libraries. `windows-crt.json` +records its version, official download, hashes, and redistribution references. +The Apache and LGPL licenses for other components do not license this DLL. +Microsoft's runtime terms and separate developer redistribution terms apply +to the Microsoft component; the runtime terms alone do not grant redistribution. +Only retail redistributable files are included; Microsoft signatures are retained. +App-local runtime security updates must be delivered with Codex updates. diff --git a/third_party/voice/README.md b/third_party/voice/README.md index d79e3de68d..6cbddf369f 100644 --- a/third_party/voice/README.md +++ b/third_party/voice/README.md @@ -249,7 +249,23 @@ runtime inspection and SDK export. These targets are MSVC-only. They require native Windows execution of the matching architecture; they are not cross builds. The generic Rust-consumer aliases are connected separately, after these inputs. -Provide the complete installed Cygwin/pkgconf repository explicitly. The default +Provide the complete installed Cygwin/pkgconf repository explicitly. For a public +Windows build, `.github/scripts/setup-voice-windows.ps1 -Target +x86_64-pc-windows-msvc -SnapshotArchive ` (under `public/` in +codex-internal) verifies the pinned archive and every input size and SHA-512 +digest, then runs the offline installer against its signed metadata. ARM64 uses +`aarch64-pc-windows-msvc` and the same Cygwin x64 tools under emulation. The +installed tool tree stays in the CI temporary directory and is never added to +a Codex package. The upstream mirror's signed metadata changes over time, so +the archived snapshot must be supplied separately. Private CI validates this +public bootstrap against its existing pinned archive. Public release CI obtains +the same hash-pinned build inputs from the public `openai/codex` release named +by `voice-cygwin-snapshot.json`. That release also makes the corresponding +upstream source archives available under `cygwin-build-sources.tar`, with its +own size and SHA-256 pin. These Cygwin tools run only on the build runner; +neither archive is included in the user's Codex package. + +The default `windows_installed_tools` label setting is empty and fails if a Windows action needs it. This keeps ordinary public dependency queries independent of private provisioning; it does not silently omit tools from a requested Windows build. @@ -258,11 +274,14 @@ The module does not download that installed tree or accept compiler licenses. After provisioning, a native PowerShell invocation is: ```powershell +$hostArch = $env:PROCESSOR_ARCHITEW6432 +if (-not $hostArch) { $hostArch = $env:PROCESSOR_ARCHITECTURE } bazel build //third_party/voice:native_link_windows_x86_64 ` --platforms=//:local_windows_msvc ` --inject_repository="voice_windows_tools=$env:VOICE_WINDOWS_BAZEL_REPOSITORY" ` --//third_party/voice:windows_installed_tools=@voice_windows_tools//:tools ` - --action_env="SystemRoot=$env:SystemRoot" --host_action_env="SystemRoot=$env:SystemRoot" + --action_env="SystemRoot=$env:SystemRoot" --host_action_env="SystemRoot=$env:SystemRoot" ` + --action_env="PROCESSOR_ARCHITECTURE=$hostArch" --host_action_env="PROCESSOR_ARCHITECTURE=$hostArch" ``` Use `native_link_windows_aarch64` on native ARM64 Windows. Existing MSVC license diff --git a/third_party/voice/assemble_package.py b/third_party/voice/assemble_package.py index af6cc8ca00..a8077d4c5f 100644 --- a/third_party/voice/assemble_package.py +++ b/third_party/voice/assemble_package.py @@ -114,6 +114,7 @@ def assemble( for relative in ( "NOTICE.md", "sources.json", + *(("windows-crt.json",) if suffix else ()), "licenses/LGPL-2.1.txt", "licenses/Opus.txt", "licenses/PCRE2.md", diff --git a/third_party/voice/bazel_windows.py b/third_party/voice/bazel_windows.py index f14941d041..5cd172fb5a 100644 --- a/third_party/voice/bazel_windows.py +++ b/third_party/voice/bazel_windows.py @@ -64,9 +64,13 @@ def main(): environment, _ = build_environment( document, inputs["target"], {"python": Path(sys.executable)} ) - # Keep executor scratch directories, never its developer tool search path. + # Keep host identity and scratch directories, never developer tool search paths. environment.update( - {name: os.environ[name] for name in ("TMP", "TEMP") if name in os.environ} + { + name: os.environ[name] + for name in ("TMP", "TEMP", "PROCESSOR_ARCHITECTURE") + if name in os.environ + } ) home = temporary / "home" home.mkdir() diff --git a/third_party/voice/release_runtime.py b/third_party/voice/release_runtime.py index 6cb0f74efc..a02cce3448 100644 --- a/third_party/voice/release_runtime.py +++ b/third_party/voice/release_runtime.py @@ -1,4 +1,4 @@ -"""Stage verified macOS voice libraries and seal their post-signing release receipt.""" +"""Stage verified voice libraries and seal their public-release receipt.""" import argparse import json @@ -10,8 +10,15 @@ from runtime import digest def stage(source: Path, destination: Path, target: str) -> None: - if target not in {"aarch64-apple-darwin", "x86_64-apple-darwin"}: - raise ValueError("public voice runtime requires a macOS target") + if target not in { + "aarch64-apple-darwin", + "x86_64-apple-darwin", + "aarch64-unknown-linux-gnu", + "x86_64-unknown-linux-gnu", + "aarch64-pc-windows-msvc", + "x86_64-pc-windows-msvc", + }: + raise ValueError("unsupported public release voice runtime target") source = source.resolve(strict=True) files = runtime_files(source, target) destination.mkdir() diff --git a/third_party/voice/test_assemble_package.py b/third_party/voice/test_assemble_package.py index 2eb4999671..a9ac9a3588 100644 --- a/third_party/voice/test_assemble_package.py +++ b/third_party/voice/test_assemble_package.py @@ -276,6 +276,79 @@ class AssembleTests(unittest.TestCase): ) self.assertEqual(manifest["appVersion"], version) + def test_linux_release_pairs_musl_app_with_gnu_voice_runtime(self): + self.commit = "b" * 40 + target = "aarch64-unknown-linux-gnu" + runtime, _ = self.make_runtime(target) + staged = self.root / "staged" + stage(runtime, staged, target) + seal(staged, target) + for version in ("0.154.0-alpha.8", "0.154.0-beta.2", "0.154.0"): + with self.subTest(version=version): + self.metadata["version"] = version + (self.package / "codex-package.json").write_text( + json.dumps(self.metadata) + ) + output = self.root / f"linux-{version}" + assemble( + self.package, + self.helper, + target, + self.commit, + output, + runtime=staged, + release_version=version, + ) + voice = output / "codex-resources/voice" + manifest = json.loads((voice / "manifest.json").read_text()) + self.assertEqual(manifest["appTarget"], "aarch64-unknown-linux-musl") + self.assertEqual(manifest["voiceTarget"], target) + self.assertEqual( + manifest["sha256"]["codex-resources/voice/runtime.json"], + digest(staged / "runtime.json"), + ) + + def test_windows_release_packages_signed_receipt_and_exe_helper(self): + self.commit = "b" * 40 + for target in ("x86_64-pc-windows-msvc", "aarch64-pc-windows-msvc"): + with self.subTest(target=target): + runtime, _ = self.make_runtime(target, "bin/gst{}.dll") + staged = self.root / f"signed-{target}" + stage(runtime, staged, target) + library = staged / "bin/gio-2.0-0.dll" + library.write_bytes(library.read_bytes() + b"signed") + seal(staged, target) + self.metadata.update( + target=target, + entrypoint="bin/codex.exe", + version="0.154.0-beta.2", + ) + (self.package / "bin/codex.exe").write_bytes(b"unchanged app") + (self.package / "codex-package.json").write_text( + json.dumps(self.metadata) + ) + output = self.root / f"windows-{target}" + assemble( + self.package, + self.helper, + target, + self.commit, + output, + runtime=staged, + release_version="0.154.0-beta.2", + ) + voice = output / "codex-resources/voice" + self.assertEqual( + (voice / "bin/codex-voice-host.exe").read_bytes(), + self.helper.read_bytes(), + ) + self.assertEqual( + (voice / "bin/gio-2.0-0.dll").read_bytes(), library.read_bytes() + ) + self.assertTrue( + runtime_files(voice.resolve(), target, public_release=True) + ) + def test_rejects_invalid_runtime_receipts_before_creating_package(self): runtime, original = self.make_runtime() changes = [ diff --git a/third_party/voice/test_bazel_windows.py b/third_party/voice/test_bazel_windows.py index 945c7e270c..1bb4296e62 100644 --- a/third_party/voice/test_bazel_windows.py +++ b/third_party/voice/test_bazel_windows.py @@ -5,13 +5,56 @@ import errno import json from pathlib import Path import tempfile +from types import SimpleNamespace import unittest +from unittest.mock import patch + +import bazel_windows from bazel_copy import copy_payloads from bazel_windows import selected_inputs class WindowsInputsTests(unittest.TestCase): + def test_adapter_preserves_host_architecture_without_developer_path(self): + with tempfile.TemporaryDirectory() as directory: + config = Path(directory) / "action.json" + config.write_text("{}") + for architecture in ("AMD64", "ARM64"): + environment = {"PROCESSOR_ARCHITECTURE": architecture, "PATH": "unsafe"} + with ( + self.subTest(architecture=architecture), + patch.object( + bazel_windows, "os", SimpleNamespace(environ=environment) + ), + patch.object( + bazel_windows.sys, "argv", ["driver", "unknown", str(config)] + ), + patch.object( + bazel_windows, + "selected_inputs", + return_value={"target": "unused"}, + ), + patch.object( + bazel_windows, + "build_environment", + return_value=({"PATH": "declared"}, {}), + ), + ): + with self.assertRaisesRegex( + ValueError, "unknown Windows native action" + ): + bazel_windows.main() + self.assertEqual( + environment, + { + "PROCESSOR_ARCHITECTURE": architecture, + "PATH": "declared", + "HOME": environment["HOME"], + "USERPROFILE": environment["HOME"], + }, + ) + def setUp(self): temporary = tempfile.TemporaryDirectory() self.addCleanup(temporary.cleanup) diff --git a/third_party/voice/windows-crt.json b/third_party/voice/windows-crt.json new file mode 100644 index 0000000000..c198cb9ea4 --- /dev/null +++ b/third_party/voice/windows-crt.json @@ -0,0 +1,18 @@ +{ + "version": "14.50.35719", + "license": "https://visualstudio.microsoft.com/license-terms/vs2026-ga-visualcpp-v14-redist-runtime/", + "developerLicense": "https://visualstudio.microsoft.com/license-terms/vs2026-ga-community/", + "redistribution": "https://learn.microsoft.com/en-us/visualstudio/releases/2026/redistribution", + "x86_64-pc-windows-msvc": { + "url": "https://download.visualstudio.microsoft.com/download/pr/a424e95b-20f6-4af6-844f-9d9a806080a7/a4ab48822362df6e478eb4ed33bf825a07a6faacd6879c1e3d59929cd38b984b/Microsoft.VC.14.50.18.0.CRT.Redist.X64.base.vsix", + "sha256": "a4ab48822362df6e478eb4ed33bf825a07a6faacd6879c1e3d59929cd38b984b", + "member": "Contents/VC/Redist/MSVC/14.50.35710/x64/Microsoft.VC145.CRT/vcruntime140.dll", + "dllSha256": "184146852727a9db4eea06178716bec3cdbb1015c911f6b0f915b184ad7775b2" + }, + "aarch64-pc-windows-msvc": { + "url": "https://download.visualstudio.microsoft.com/download/pr/a424e95b-20f6-4af6-844f-9d9a806080a7/3544badcbcf09e77c6ec32ed866aae59471a39d8a84611d9ae016861ff60a42f/Microsoft.VC.14.50.18.0.CRT.Redist.ARM64.base.vsix", + "sha256": "3544badcbcf09e77c6ec32ed866aae59471a39d8a84611d9ae016861ff60a42f", + "member": "Contents/VC/Redist/MSVC/14.50.35710/arm64/Microsoft.VC145.CRT/vcruntime140.dll", + "dllSha256": "6d987d8cb2a47cff9c29c1fcbb853e7ef273c545f7d822728f7d2d448ca18758" + } +} diff --git a/third_party/voice/windows_crt.py b/third_party/voice/windows_crt.py new file mode 100644 index 0000000000..a927c1b19e --- /dev/null +++ b/third_party/voice/windows_crt.py @@ -0,0 +1,83 @@ +"""Add pinned, unmodified Microsoft retail CRT files to Windows release staging.""" + +import argparse +import hashlib +import io +import json +from pathlib import Path +import re +import subprocess +import urllib.request +import zipfile + +from windows_runtime import EXTERNAL_IMPORTS, inspect + + +def stage(root: Path, target: str, helper: Path): + pin = json.loads(Path(__file__).with_name("windows-crt.json").read_text())[target] + with urllib.request.urlopen(pin["url"], timeout=90) as response: + archive = response.read(6 * 1024 * 1024 + 1) + if hashlib.sha256(archive).hexdigest() != pin["sha256"]: + raise ValueError("Microsoft CRT archive digest mismatch") + with zipfile.ZipFile(io.BytesIO(archive)) as source: + member = source.getinfo(pin["member"]) + if member.file_size > 1024 * 1024: + raise ValueError("CRT member exceeds size limit") + data = source.read(member) + if hashlib.sha256(data).hexdigest() != pin["dllSha256"]: + raise ValueError("Microsoft CRT DLL digest mismatch") + # Extract exactly one retail member, never debug_nonredist or installer files. + with (root / "bin/vcruntime140.dll").open("xb") as output: + output.write(data) + files = list((root / "bin").glob("*.dll")) + bundled = {path.name.lower() for path in files} + imports = set() + for path in files: + if path.name.lower() != "vcruntime140.dll": + imports.update(inspect(path, target).imports) + # Only Microsoft's hash-pinned CRT may use ARM64X rather than plain ARM64. + for path in (helper, root / "bin/vcruntime140.dll"): + result = subprocess.run( + ["dumpbin", "/nologo", "/dependents", str(path)], + check=True, + capture_output=True, + timeout=30, + ) + imports.update( + re.findall( + r"(?mi)^ +([a-z0-9_+.-]+\.dll)\s*$", result.stdout.decode("ascii") + ) + ) + # Additional Windows OS imports used by the Rust helper, not bundled CRTs. + system = (EXTERNAL_IMPORTS - {"vcruntime140.dll"}) | { + "api-ms-win-core-synch-l1-2-0.dll", + "api-ms-win-core-winrt-error-l1-1-0.dll", + "bcrypt.dll", + "bcryptprimitives.dll", + "combase.dll", + "mmdevapi.dll", + "oleaut32.dll", + "ntdll.dll", + "userenv.dll", + "dbghelp.dll", + } + missing = {name.lower() for name in imports} - bundled - system + if missing: + raise ValueError(f"Unbundled Windows imports: {sorted(missing)}") + manifest_path = root / "runtime.json" + manifest = json.loads(manifest_path.read_text()) + if manifest.get("target") != target or manifest.get("developmentOnly") is not True: + raise ValueError("expected staged development runtime") + manifest["libraries"].append( + {"path": "bin/vcruntime140.dll", "sha256": pin["dllSha256"]} + ) + manifest_path.write_text(json.dumps(manifest, indent=2) + "\n") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--root", type=Path, required=True) + parser.add_argument("--target", required=True) + parser.add_argument("--helper", type=Path, required=True) + args = parser.parse_args() + stage(args.root, args.target, args.helper) diff --git a/third_party/voice/windows_native.bzl b/third_party/voice/windows_native.bzl index e1a135319e..51c9968dd5 100644 --- a/third_party/voice/windows_native.bzl +++ b/third_party/voice/windows_native.bzl @@ -25,6 +25,9 @@ def _windows_tools_impl(ctx): system_root = ctx.configuration.default_shell_env.get("SystemRoot") if not system_root: fail("Pass --action_env=SystemRoot= as a fixed value") + host_architecture = ctx.configuration.default_shell_env.get("PROCESSOR_ARCHITECTURE") + if not host_architecture: + fail("Pass --action_env=PROCESSOR_ARCHITECTURE= as a fixed value") msvc = ctx.attr.msvc[DirectoryInfo] sdk = ctx.attr.sdk[DirectoryInfo] tools = { @@ -68,7 +71,11 @@ def _windows_tools_impl(ctx): python = python, manifest = manifests[0], installed_files = installed, - environment = {"SystemRoot": system_root, "SYSTEMROOT": system_root}, + environment = { + "SystemRoot": system_root, + "SYSTEMROOT": system_root, + "PROCESSOR_ARCHITECTURE": host_architecture, + }, inputs = { "schemaVersion": 1, "target": ctx.attr.target,