Experience-Driven Black-Box Cost Control for Coding Agents
Python agentic-aiagentic-codingai-codingai-coding-assistantanthropicartificial-intelligenceclaude-codecodexcoding-agentcoding-agentscost-optimizationcost-reductiondeveloper-toolsgithub-copilotllmopen-sourceopenaiproductivitytoken-optimization
codex quote parsing fixed
Aarond20578d4a8be
diff --git a/plugin/scripts/te_bootstrap.py b/plugin/scripts/te_bootstrap.pyindex f7fc535..83dd420 100644--- a/plugin/scripts/te_bootstrap.py+++ b/plugin/scripts/te_bootstrap.py@@ -31,6 +31,28 @@ import sysMIN_VERSION = (3, 10)+# `source = ...` in Codex's config.toml: a TOML basic string ("...", with escapes) or a literal+# one ('...', as written), which Codex writes for a path with backslashes (0.163.0, Windows).+_SOURCE = re.compile(r'''source\s*=\s*(?:"((?:[^"\\]|\\.)*)"|'([^']*)')''')+++def _toml_string(basic, literal):+ r"""A `source` value: a literal string as written; a basic string with its `\\` and `\"`+ undone, any other backslash kept, as a path pasted between double quotes has them."""+ if literal is not None:+ return literal+ return re.sub(r'\\([\\"])', r'\1', basic)+++def _without_long_prefix(path):+ r"""`path` without the extended-length prefix Codex puts on a local source on Windows:+ `\\?\C:\src` is `C:\src`, and `\\?\UNC\server\share` is `\\server\share`."""+ if path.startswith("\\\\?\\UNC\\"):+ return "\\\\" + path[8:]+ if path.startswith("\\\\?\\"):+ return path[4:]+ return path+def _marketplace_sources(plugin_root):"""For a Codex install copy (`<home>/plugins/cache/<marketplace>/<plugin>/<version>`), the@@ -63,11 +85,13 @@ def _marketplace_sources(plugin_root):for line in text.splitlines():line = line.strip()if line.startswith("["):- section = line.strip("[]").strip().replace('"', "")+ section = line.strip("[]").strip().replace('"', "").replace("'", "")continue- m = re.match(r'source\s*=\s*"([^"]+)"', line)+ m = _SOURCE.match(line)if m and section == "marketplaces." + market:- out.append(m.group(1))+ source = _without_long_prefix(_toml_string(m.group(1), m.group(2)))+ if source:+ out.append(source)return outdiff --git a/tests/test_plugin_package.py b/tests/test_plugin_package.pyindex 4c719fd..d9b64d3 100644--- a/tests/test_plugin_package.py+++ b/tests/test_plugin_package.py@@ -352,16 +352,21 @@ def test_a_codex_install_copy_finds_the_core_in_the_marketplace_it_came_from(tmpignore=shutil.ignore_patterns("__pycache__", "*.pyc", "lib"))(copy / "lib").mkdir()- def te(source):+ def te(source, literal=False):+ # As Codex writes it: a TOML basic string (a JSON string is one), or on Windows, for a+ # path with backslashes, a literal one (0.163.0).+ value = f"'{source}'" if literal else json.dumps(source, ensure_ascii=False)(home / "config.toml").write_text('check_for_update_on_startup = false\n\n[marketplaces.tokeneconomizer]\n'- f'source_type = "local"\nsource = "{source}"\n')+ f'source_type = "local"\nsource = {value}\n')return subprocess.run(SH + [str(copy / "scripts" / "te"), "doctor"], capture_output=True,text=True, timeout=60, cwd=str(tmp_path),env=dict(os.environ, TOKENECONOMIZER_HOME=str(tmp_path / "h"),TE_CORE_PATH="", PYTHONPATH=""))out = te(REPO)assert out.returncode == 0 and "python" in out.stdout, out.stderr[-500:]+ out = te(REPO, literal=True)+ assert out.returncode == 0 and "python" in out.stdout, out.stderr[-500:]out = te(str(tmp_path / "nowhere"))assert out.returncode == 1 and "core package was not found" in out.stderrassert "was not found for the plugin at" in (tmp_path / "h" / "errors.log").read_text()@@ -376,6 +381,32 @@ def test_a_codex_install_copy_finds_the_core_in_the_marketplace_it_came_from(tmpassert out.returncode == 0 and "python" in out.stdout, out.stderr[-500:]+def test_the_codex_marketplace_source_is_read_in_either_toml_string_form(tmp_path):+ r"""Codex on Windows writes a local marketplace's `source` as a TOML literal string, with the+ extended-length prefix (0.163.0): `source = '\\?\C:\src\TokenEconomizer'`. A basic string+ has its escapes undone, and another marketplace's `source` is not this one's."""+ sys.path.insert(0, os.path.join(PLUGIN, "scripts"))+ try:+ import te_bootstrap+ finally:+ sys.path.pop(0)+ home = tmp_path / "codex"+ home.mkdir()+ copy = home / "plugins" / "cache" / "tokeneconomizer" / "tokeneconomizer" / "0.1.0"++ def source(line):+ (home / "config.toml").write_text(+ "[marketplaces.other]\nsource = '/elsewhere'\n\n"+ f'[marketplaces.tokeneconomizer]\nsource_type = "local"\n{line}\n', encoding="utf-8")+ return te_bootstrap._marketplace_sources(str(copy))[1:] # after Codex's git checkout++ assert source(r"source = '\\?\C:\src\TokenEconomizer'") == [r"C:\src\TokenEconomizer"]+ assert source(r"source = '\\?\UNC\server\share\TokenEconomizer'") == [+ r"\\server\share\TokenEconomizer"]+ assert source(r'source = "C:\\src\\Token \"Economizer\""') == [r'C:\src\Token "Economizer"']+ assert source('source = "/home/me/TokenEconomizer"') == ["/home/me/TokenEconomizer"]++def test_a_claude_install_copy_without_symlinks_finds_the_core_in_its_marketplace(tmp_path):"""On a checkout without symlinks (Windows), `lib/tokeneconomizer` is a file holding thelink's target, and Claude Code's install copy (`~/.claude/plugins/cache/...`) carries that