Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

Support Claude Code on Windows

Luc Lin371c39e9d4fc
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..e916ab7
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,4 @@
+# Shell launchers must keep LF endings, or sh fails on Windows checkouts with core.autocrlf=true.
+plugin/scripts/te text eol=lf
+plugin/scripts/te-hook text eol=lf
+*.sh text eol=lf
diff --git a/docs/PLUGIN.md b/docs/PLUGIN.md
index 27fdd4c..3cc809d 100644
--- a/docs/PLUGIN.md
+++ b/docs/PLUGIN.md
@@ -588,6 +588,7 @@ open-vocabulary. On a repository you do not trust, leave micro-skills off (the d
* `refresh-catalog` is manual, once per machine and agent; until it runs, evidence pruning
sees only the tools sessions used (plus, on Claude Code, the deferred ones). Running it at
an agent's first session on a machine would close that gap.
-* Windows (the launcher is POSIX `sh`), Python 3.9, and compressed Codex rollouts (`.zst`).
+* Windows beyond Claude Code (Codex, the Copilot CLI and Pi there are untested; Copilot's `-p` is
+ not told apart, having no `ps`), Python 3.9, and compressed Codex rollouts (`.zst`).
* Transcript formats are internal to each agent and change between versions; the readers are
pinned by tests against the shapes observed on the versions above.
diff --git a/plugin/README.md b/plugin/README.md
index fa1519f..f1aba44 100644
--- a/plugin/README.md
+++ b/plugin/README.md
@@ -45,6 +45,12 @@ claude plugin list # should show tokeneconomizer@tokeneconomizer ... enab
(Inside Claude Code the same thing is `/plugin marketplace add …` and `/plugin install …`.)
+On Windows, the hooks run under Git for Windows' `sh`, which Claude Code uses for its shell.
+Give the marketplace as an absolute path with forward slashes (`C:/path/to/TokenEconomizer`)
+and keep the clone in place: a checkout without symlinks has no package in the install copy,
+which finds it in that clone. Python is taken as `python3`, `python`, then `py -3`; the
+Microsoft Store alias is skipped when it runs nothing.
+
**2. Check it.** Start Claude Code in any project (`cd my-project && claude`) and type
`/tokeneconomizer:status`. It should print a short status block, not an error. Claude Code may ask
permission to run the plugin's script; allow it always.
diff --git a/plugin/scripts/te b/plugin/scripts/te
old mode 100755
new mode 100644
index 0010305..d50251c
--- a/plugin/scripts/te
+++ b/plugin/scripts/te
@@ -1,10 +1,19 @@
#!/bin/sh
# TokenEconomizer command line (`te status`, `te toolprune show`, `te config`, ...).
-dir=$(CDPATH= cd -- "${0%/*}" && pwd)
-for py in "${TE_PYTHON:-}" python3 python; do
- if [ -n "$py" ] && command -v "$py" >/dev/null 2>&1; then
- exec "$py" "$dir/te_bootstrap.py" cli "$@"
+# Windows (Git Bash): see te-hook for the backslashes in $0 and the Microsoft Store alias.
+self=$0
+case $self in *\\*) self=$(printf '%s' "$self" | tr '\\' '/') ;; esac
+dir=$(CDPATH= cd -- "${self%/*}" && pwd)
+# Session logs are UTF-8; without this, Python on Windows reads them in the ANSI code page.
+export PYTHONUTF8=1
+for py in "${TE_PYTHON:-}" python3 python py; do
+ [ -n "$py" ] || continue
+ where=$(command -v "$py" 2>/dev/null) || continue
+ case $where in *WindowsApps*) "$py" -c "" </dev/null >/dev/null 2>&1 || continue ;; esac
+ if [ "$py" = py ]; then
+ exec py -3 "$dir/te_bootstrap.py" cli "$@"
fi
+ exec "$py" "$dir/te_bootstrap.py" cli "$@"
done
echo "te: no python3 on PATH (set TE_PYTHON)" >&2
exit 1
diff --git a/plugin/scripts/te-hook b/plugin/scripts/te-hook
old mode 100755
new mode 100644
index af3d45d..ce12f03
--- a/plugin/scripts/te-hook
+++ b/plugin/scripts/te-hook
@@ -1,11 +1,22 @@
#!/bin/sh
# TokenEconomizer hook launcher: run the hook dispatcher under the first Python 3 on PATH.
# Fails open -- with no usable Python the hook prints an empty object, which changes nothing.
-dir=$(CDPATH= cd -- "${0%/*}" && pwd)
-for py in "${TE_PYTHON:-}" python3 python; do
- if [ -n "$py" ] && command -v "$py" >/dev/null 2>&1; then
- exec "$py" "$dir/te_bootstrap.py" hook "$@"
+# On Windows (Git Bash), $0 can arrive with backslashes, and `python3` can be the Microsoft Store
+# alias under WindowsApps, which is on PATH but runs nothing: such an alias is tried before use,
+# and the `py` launcher is the last candidate.
+self=$0
+case $self in *\\*) self=$(printf '%s' "$self" | tr '\\' '/') ;; esac
+dir=$(CDPATH= cd -- "${self%/*}" && pwd)
+# Session logs are UTF-8; without this, Python on Windows reads them in the ANSI code page.
+export PYTHONUTF8=1
+for py in "${TE_PYTHON:-}" python3 python py; do
+ [ -n "$py" ] || continue
+ where=$(command -v "$py" 2>/dev/null) || continue
+ case $where in *WindowsApps*) "$py" -c "" </dev/null >/dev/null 2>&1 || continue ;; esac
+ if [ "$py" = py ]; then
+ exec py -3 "$dir/te_bootstrap.py" hook "$@"
fi
+ exec "$py" "$dir/te_bootstrap.py" hook "$@"
done
# Drain stdin with a builtin (no `cat` when PATH is empty), then answer "no change".
while IFS= read -r _line; do :; done
diff --git a/plugin/scripts/te_bootstrap.py b/plugin/scripts/te_bootstrap.py
index 25b6986..1ebd83b 100644
--- a/plugin/scripts/te_bootstrap.py
+++ b/plugin/scripts/te_bootstrap.py
@@ -41,6 +41,19 @@ def _marketplace_sources(plugin_root):
return []
home, market = os.sep.join(parts[:-5]), parts[-3]
out = [os.path.join(home, ".tmp", "marketplaces", market)]
+ # Claude Code keeps the same cache layout under `~/.claude/plugins/cache/`; on a checkout
+ # without symlinks (Windows) the copy has no `lib`, so the package is found in the clone
+ # its `known_marketplaces.json` records for this marketplace.
+ try:
+ import json
+ known = json.load(open(os.path.join(home, "plugins", "known_marketplaces.json")))
+ entry = known.get(market) or {}
+ source = entry.get("source") or {}
+ for cand in (source.get("path"), entry.get("installLocation")):
+ if cand:
+ out.append(cand)
+ except (IOError, OSError, ValueError, AttributeError):
+ pass
try:
text = open(os.path.join(home, "config.toml")).read()
except (IOError, OSError):
@@ -57,10 +70,24 @@ def _marketplace_sources(plugin_root):
return out
+def _symlink_stub(plugin_root):
+ """Where `lib/tokeneconomizer` points when git checked the symlink out as a plain file
+ holding its target (Windows without symlink support): the directory holding the package."""
+ stub = os.path.join(plugin_root, "lib", "tokeneconomizer")
+ try:
+ if not os.path.isfile(stub) or os.path.getsize(stub) > 1024:
+ return None
+ target = open(stub).read().strip()
+ except (IOError, OSError):
+ return None
+ return os.path.dirname(os.path.normpath(os.path.join(plugin_root, "lib", target)))
+
+
def core_path():
plugin_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
for cand in ([os.environ.get("TE_CORE_PATH"), os.path.join(plugin_root, "lib"),
- os.path.dirname(plugin_root)] + _marketplace_sources(plugin_root)):
+ _symlink_stub(plugin_root), os.path.dirname(plugin_root)]
+ + _marketplace_sources(plugin_root)):
if cand and os.path.isfile(os.path.join(cand, "tokeneconomizer", "plugin", "hook.py")):
return cand
return None
diff --git a/tests/test_live_core.py b/tests/test_live_core.py
index b5d666e..8574a19 100644
--- a/tests/test_live_core.py
+++ b/tests/test_live_core.py
@@ -230,6 +230,7 @@ def test_a_compressed_rollout_is_reported_unreadable(tmp_path):
# ---- hosts ---------------------------------------------------------------------------------
[email protected](os.name == "nt", reason="no `ps` on Windows: Copilot -p is not told apart")
def test_host_detection():
assert hosts.detect({"turn_id": "t"}).name == "codex"
assert hosts.detect({"transcript_path": "/h/.codex/sessions/2026/rollout-a.jsonl"}).name == "codex"
@@ -845,6 +846,7 @@ def test_copilot_side_calls_name_a_model_a_byok_provider_can_serve():
"COPILOT_PROVIDER_TYPE": wire}) == "anthropic/claude-haiku-4.5"
[email protected](os.name == "nt", reason="the probe trap is made of sh scripts")
def test_the_probe_repository_carries_its_trap(tmp_path):
import subprocess
from tokeneconomizer.plugin import probe
diff --git a/tests/test_live_hooks.py b/tests/test_live_hooks.py
index 4f27e18..5f2b8e7 100644
--- a/tests/test_live_hooks.py
+++ b/tests/test_live_hooks.py
@@ -815,8 +815,8 @@ def test_a_fold_that_cannot_take_the_lock_skips_and_the_session_is_still_folded_
env, repo, tmp_path, monkeypatch):
"""A paid fold holds the lock through its side call; a second fold of the same session that
cannot take it skips, leaving the session for the holder or a later start: it is folded once."""
- import fcntl
from tokeneconomizer.plugin import fold, hosts
+ from tokeneconomizer.plugin.store import _try_lock, _unlock
from tokeneconomizer.plugin.context import LiveContext
t = trap_session(str(tmp_path / "s.jsonl"), repo, 0)
base = {"session_id": "s", "transcript_path": t, "cwd": repo}
@@ -824,7 +824,7 @@ def test_a_fold_that_cannot_take_the_lock_skips_and_the_session_is_still_folded_
st = store_for(env, repo)
monkeypatch.setattr(fold, "FOLD_LOCK_WAIT_S", 0.2)
holder = open(st.path(".fold.lock"), "a+") # another fold, mid side call
- fcntl.flock(holder.fileno(), fcntl.LOCK_EX)
+ _try_lock(holder)
try:
call(env, dict(base, hook_event_name="SessionEnd", reason="other")) # folds inline
assert st.read_lines("ledger.jsonl") == []
@@ -832,7 +832,7 @@ def test_a_fold_that_cannot_take_the_lock_skips_and_the_session_is_still_folded_
ctx = LiveContext.build(hosts.HOSTS["claude"](), repo, env)
assert "s" in fold.pending_sessions(ctx) # left for the holder, or a later start
finally:
- fcntl.flock(holder.fileno(), fcntl.LOCK_UN)
+ _unlock(holder)
holder.close()
assert fold.fold_session(ctx, "s") is not None
assert fold.fold_session(ctx, "s") is None
diff --git a/tests/test_live_repomap.py b/tests/test_live_repomap.py
index 62282d9..e6d135d 100644
--- a/tests/test_live_repomap.py
+++ b/tests/test_live_repomap.py
@@ -8,6 +8,8 @@ import sys
import pytest
+pytestmark = pytest.mark.skipif(os.name == "nt", reason="the stand-in Python is a sh script")
+
from tokeneconomizer.plugin import repomap
from tokeneconomizer.plugin.config import LiveConfig
from tests.test_live_hooks import call, env, repo # noqa: F401
diff --git a/tests/test_live_repos.py b/tests/test_live_repos.py
index c49a7b0..c82d945 100644
--- a/tests/test_live_repos.py
+++ b/tests/test_live_repos.py
@@ -615,7 +615,8 @@ def test_an_end_with_no_start_and_no_log_records_nothing(tmp_path, env, host):
assert st.read_lines("ledger.jsonl") == []
[email protected](os.geteuid() == 0, reason="root writes through read-only permissions")
[email protected](os.name == "nt" or os.geteuid() == 0,
+ reason="root writes through read-only permissions; Windows ignores chmod on directories")
def test_a_change_where_the_state_is_read_only_is_refused_whole(tmp_path, env, monkeypatch,
capsys):
"""Codex's default sandbox makes ~/.tokeneconomizer read-only. A change must be refused
diff --git a/tests/test_live_safety.py b/tests/test_live_safety.py
index e7cacd4..b1168de 100644
--- a/tests/test_live_safety.py
+++ b/tests/test_live_safety.py
@@ -81,6 +81,7 @@ def test_a_symlinked_exclude_file_is_not_written_through(repo, tmp_path):
assert victim.read_text() == "keep\n"
[email protected](os.name == "nt", reason="POSIX permission bits: Windows keeps a profile private by ACL")
def test_a_loose_state_root_is_made_private_and_the_launcher_is_owner_only(tmp_path):
env = {"TOKENECONOMIZER_HOME": str(tmp_path / "home"), "TE_PLUGIN_ROOT": PLUGIN}
os.makedirs(env["TOKENECONOMIZER_HOME"], mode=0o775)
@@ -90,6 +91,7 @@ def test_a_loose_state_root_is_made_private_and_the_launcher_is_owner_only(tmp_p
assert _mode(ensure_launcher(env)) == 0o700
[email protected](os.name == "nt", reason="POSIX permission bits: Windows keeps a profile private by ACL")
def test_the_command_line_creates_its_state_private(repo, tmp_path):
env = dict(os.environ, TOKENECONOMIZER_HOME=str(tmp_path / "home"))
subprocess.run(["sh", os.path.join(PLUGIN, "scripts", "te"), "--cwd", repo, "config", "set",
diff --git a/tests/test_plugin_package.py b/tests/test_plugin_package.py
index 437c791..3ac38f7 100644
--- a/tests/test_plugin_package.py
+++ b/tests/test_plugin_package.py
@@ -17,6 +17,8 @@ import pytest
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PLUGIN = os.path.join(REPO, "plugin")
+# Windows runs no shebang: the launchers go through Git Bash's `sh`.
+SH = ["sh"] if os.name == "nt" else []
EVENTS = {"SessionStart", "UserPromptSubmit", "PostToolUse", "Stop", "SessionEnd"}
@@ -82,7 +84,7 @@ def test_no_bin_directory():
def _run_hook(launcher, payload, env):
- p = subprocess.run([launcher], input=json.dumps(payload), capture_output=True, text=True,
+ p = subprocess.run(SH + [launcher], input=json.dumps(payload), capture_output=True, text=True,
env=env, timeout=60)
return p.returncode, p.stdout, p.stderr
@@ -209,6 +211,8 @@ def test_the_mechanisms_never_import_the_plugin_runtime():
assert out.stdout.strip() == "[]", out.stdout + out.stderr
[email protected](not os.path.islink(os.path.join(PLUGIN, "lib", "tokeneconomizer")),
+ reason="checkout without symlinks (Windows): see the known_marketplaces test")
def test_the_plugin_ships_and_loads_no_benchmark_code():
"""The benchmark adapters, the facade, the workloads, the baselines and the benchmark-only
controllers live with the experiments (`experiments/te_bench/`). The plugin's library is the
@@ -251,6 +255,7 @@ def test_the_package_is_stdlib_only_and_never_touches_the_benchmark_harness():
"tokeneconomizer", "tomllib"), f"{path} imports non-stdlib {mod}"
[email protected](os.name == "nt", reason="the Pi extension is not ported to Windows")
def test_a_resumed_pi_conversation_keeps_the_section_it_carries():
"""Pi rebuilds a conversation's system message from its system entries, so a resumed one is
given back the facts section it carries, byte for byte: unwrapped from the `<name>` tags Pi
@@ -311,6 +316,8 @@ def test_the_plugin_directory_is_also_a_pi_package():
("package-lock.json", "npm-shrinkwrap.json", "bun.lock", "bun.lockb"))
[email protected](not os.path.islink(os.path.join(PLUGIN, "lib", "tokeneconomizer")),
+ reason="checkout without symlinks (Windows): see the known_marketplaces test")
def test_the_plugin_carries_the_core_as_symlinks_an_install_copy_follows(tmp_path):
"""Claude Code installs a COPY of plugin/ into its cache, even from a local checkout, so the
core must be reachable from inside plugin/."""
@@ -323,7 +330,7 @@ def test_the_plugin_carries_the_core_as_symlinks_an_install_copy_follows(tmp_pat
copy = str(tmp_path / "cache" / "tokeneconomizer" / "0.1.0")
shutil.copytree(PLUGIN, copy, symlinks=False,
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
- out = subprocess.run([os.path.join(copy, "scripts", "te"), "doctor"], capture_output=True,
+ out = subprocess.run(SH + [os.path.join(copy, "scripts", "te"), "doctor"], capture_output=True,
text=True, env=dict(os.environ, TOKENECONOMIZER_HOME=str(tmp_path / "h")),
timeout=60)
assert out.returncode == 0 and "python" in out.stdout, out.stderr[-500:]
@@ -343,7 +350,7 @@ def test_a_codex_install_copy_finds_the_core_in_the_marketplace_it_came_from(tmp
(home / "config.toml").write_text(
'check_for_update_on_startup = false\n\n[marketplaces.tokeneconomizer]\n'
f'source_type = "local"\nsource = "{source}"\n')
- return subprocess.run([str(copy / "scripts" / "te"), "doctor"], capture_output=True,
+ 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=""))
@@ -355,11 +362,40 @@ def test_a_codex_install_copy_finds_the_core_in_the_marketplace_it_came_from(tmp
# A git marketplace (`codex plugin marketplace add owner/repo`): config.toml names the URL,
# and Codex keeps its checkout under .tmp/marketplaces (seen on 0.153.1).
(home / ".tmp" / "marketplaces").mkdir(parents=True)
- os.symlink(REPO, str(home / ".tmp" / "marketplaces" / "tokeneconomizer"))
+ try:
+ os.symlink(REPO, str(home / ".tmp" / "marketplaces" / "tokeneconomizer"))
+ except OSError:
+ pytest.skip("symlinks need privileges on Windows")
out = te("https://github.com/microsoft/TokenEconomizer.git")
assert out.returncode == 0 and "python" in out.stdout, out.stderr[-500:]
+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 the
+ link's target, and Claude Code's install copy (`~/.claude/plugins/cache/...`) carries that
+ file, not the package. The copy finds the core through the marketplace Claude Code recorded
+ in `plugins/known_marketplaces.json`."""
+ home = tmp_path / "claude-home"
+ copy = home / "plugins" / "cache" / "tokeneconomizer" / "tokeneconomizer" / "0.1.0"
+ shutil.copytree(PLUGIN, str(copy), symlinks=True,
+ ignore=shutil.ignore_patterns("__pycache__", "*.pyc", "lib"))
+ (copy / "lib").mkdir()
+ (copy / "lib" / "tokeneconomizer").write_text("../../tokeneconomizer")
+
+ def te(path):
+ (home / "plugins" / "known_marketplaces.json").write_text(json.dumps(
+ {"tokeneconomizer": {"source": {"source": "directory", "path": path},
+ "installLocation": path}}))
+ 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(str(tmp_path / "nowhere"))
+ assert out.returncode == 1 and "core package was not found" in out.stderr
+
+
def test_codex_gets_its_commands_as_plugin_skills_and_no_one_else_does():
"""Codex has no plugin slash commands; its plugins carry skills. Ours live outside `skills/`,
which Claude Code and Copilot would load beside their slash commands. Codex lists a plugin's
diff --git a/tokeneconomizer/microskills/observe.py b/tokeneconomizer/microskills/observe.py
index d720ebd..0b46964 100644
--- a/tokeneconomizer/microskills/observe.py
+++ b/tokeneconomizer/microskills/observe.py
@@ -11,6 +11,7 @@ repository. `observe_task` returns everything the environment-fact families need
session.
"""
import os
+import posixpath
import re
from dataclasses import dataclass
@@ -252,22 +253,42 @@ def _cd_target(cur, target, root=None, top=None):
there."""
if not target or target[0] in "~$-":
return None
- if os.path.isabs(target):
+ # The command is shell text (Git Bash on Windows too), so its paths are compared in one
+ # POSIX spelling: `C:\r`, `C:/r` and `/c/r` are the same directory.
+ target, cur = _shell_path(target), _shell_path(cur or ".")
+ if _shell_isabs(target):
if not root:
return None
- rel = os.path.relpath(os.path.normpath(target), os.path.normpath(root))
+ rel = posixpath.relpath(posixpath.normpath(target), posixpath.normpath(_shell_path(root)))
else:
- rel = os.path.normpath(os.path.join(cur or ".", target))
+ rel = posixpath.normpath(posixpath.join(cur, target))
if rel == ".":
return ""
if rel == ".." or rel.startswith("../"):
if not (top and root):
return None
- at = os.path.normpath(os.path.join(root, rel))
- return "" if at == os.path.normpath(top) else None
+ at = posixpath.normpath(posixpath.join(_shell_path(root), rel))
+ return "" if at == posixpath.normpath(_shell_path(top)) else None
return rel
+_DRIVE_RE = re.compile(r"^/([A-Za-z])(?=/|$)|^([A-Za-z]):(?=/|$)")
+
+
+def _shell_path(p):
+ """`p` with forward slashes and a Windows drive spelled `c:` (from a backslashed `C:`, `C:/`
+ or `/c/`); a POSIX path is returned as it is."""
+ p = p.replace("\\", "/") if os.sep == "\\" else p
+ m = _DRIVE_RE.match(p)
+ if m and os.sep == "\\":
+ p = (m.group(1) or m.group(2)).lower() + ":" + p[m.end():]
+ return p
+
+
+def _shell_isabs(p):
+ return p.startswith("/") or bool(re.match(r"^[a-z]:(/|$)", p))
+
+
def classify_command(cmd, call_index=0, vocab=None, cwd="", root=None, top=None):
"""Observations for one shell command (possibly compound). Activation prefixes carry
across the segments of one command, which is how `source activate X && python ...`
diff --git a/tokeneconomizer/plugin/context.py b/tokeneconomizer/plugin/context.py
index ec64057..0b09a41 100644
--- a/tokeneconomizer/plugin/context.py
+++ b/tokeneconomizer/plugin/context.py
@@ -82,6 +82,9 @@ class LiveContext:
cmd = [sys.executable, "-m", "tokeneconomizer.plugin.cli", *argv,
"--host", self.host.name, "--cwd", self.repo.root]
self.store.ensure()
+ if os.name == "nt":
+ from . import winspawn
+ return winspawn.spawn(cmd, self.repo.root, env, self.store.path(log_name))
log = open(self.store.path(log_name), "a")
try:
subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=log, stderr=log,
diff --git a/tokeneconomizer/plugin/hosts.py b/tokeneconomizer/plugin/hosts.py
index 51e2528..b142f62 100644
--- a/tokeneconomizer/plugin/hosts.py
+++ b/tokeneconomizer/plugin/hosts.py
@@ -421,6 +421,8 @@ def _ancestor_args(limit=4):
"""The command lines of this process's nearest ancestors, nearest first."""
import subprocess
out, pid = [], os.getppid()
+ if os.name == "nt":
+ return out # no `ps` there; Copilot's `-p` is then not told apart (a notice is kept)
for _ in range(limit):
if pid <= 1:
break
diff --git a/tokeneconomizer/plugin/store.py b/tokeneconomizer/plugin/store.py
index 9acf686..7389d75 100644
--- a/tokeneconomizer/plugin/store.py
+++ b/tokeneconomizer/plugin/store.py
@@ -6,8 +6,9 @@ therefore happens under an exclusive lock on a sibling lock file, and every writ
temporary file and `os.replace`, so a reader never sees half a document. Per-session documents are
written by one session only and need no lock.
-Locking uses `fcntl.flock`. Where it is unavailable (Windows) the lock degrades to a no-op rather
-than failing the hook: a lost update is recoverable, a crashed hook is not.
+Locking uses `fcntl.flock`, and `msvcrt.locking` on Windows. Where neither is available the lock
+degrades to a no-op rather than failing the hook: a lost update is recoverable, a crashed hook is
+not.
"""
import contextlib
import json
@@ -19,6 +20,28 @@ try:
except ImportError: # pragma: no cover - Windows
fcntl = None
+try:
+ import msvcrt
+except ImportError: # pragma: no cover - POSIX
+ msvcrt = None
+
+
+def _try_lock(fh):
+ """Take an exclusive lock on `fh` without blocking; raise OSError when it is held."""
+ if fcntl is not None:
+ fcntl.flock(fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
+ else:
+ fh.seek(0)
+ msvcrt.locking(fh.fileno(), msvcrt.LK_NBLCK, 1)
+
+
+def _unlock(fh):
+ if fcntl is not None:
+ fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
+ else:
+ fh.seek(0)
+ msvcrt.locking(fh.fileno(), msvcrt.LK_UNLCK, 1)
+
class Store:
"""One directory of JSON documents and append-only JSONL logs."""
@@ -78,7 +101,7 @@ class Store:
`timeout` seconds -- a hook must never hang the agent, and a hook's caller proceeds
either way. A caller that must not run unlocked (the fold) checks the yielded flag."""
os.makedirs(self.base, exist_ok=True)
- if fcntl is None:
+ if fcntl is None and msvcrt is None:
yield True
return
fh = open(self.path(f".{name}.lock"), "a+")
@@ -87,7 +110,7 @@ class Store:
try:
while True:
try:
- fcntl.flock(fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
+ _try_lock(fh)
got = True
break
except OSError:
@@ -97,5 +120,5 @@ class Store:
yield got
finally:
if got:
- fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
+ _unlock(fh)
fh.close()
diff --git a/tokeneconomizer/plugin/winspawn.py b/tokeneconomizer/plugin/winspawn.py
new file mode 100644
index 0000000..d81f4b6
--- /dev/null
+++ b/tokeneconomizer/plugin/winspawn.py
@@ -0,0 +1,77 @@
+"""Detached processes on Windows, where a hook's children die with it.
+
+An agent runs each hook inside a Windows job object that kills every process in it when the hook
+returns, so `start_new_session` (a no-op on Windows) and `DETACHED_PROCESS` alone do not keep a
+spawned `te` alive. Two ways out, tried in order:
+
+ 1. `CREATE_BREAKAWAY_FROM_JOB`, which works when the job allows breakaway;
+ 2. otherwise WMI's `Win32_Process.Create`, whose process is a child of the WMI service and so
+ outside the job by construction. WMI passes neither the environment nor redirections, so
+ the command, its environment and its log are written to a spec file that this module, run
+ as a script, reads back (`python winspawn.py <spec>`).
+
+Run as a script, this file imports nothing from the package: the package may not be on
+`sys.path` until the spec's `PYTHONPATH` is applied.
+"""
+import json
+import os
+import subprocess
+import sys
+import tempfile
+
+_FLAGS = (0x00000008 # DETACHED_PROCESS
+ | 0x00000200 # CREATE_NEW_PROCESS_GROUP
+ | 0x08000000) # CREATE_NO_WINDOW
+_BREAKAWAY = 0x01000000 # CREATE_BREAKAWAY_FROM_JOB
+
+
+def spawn(cmd, cwd, env, log_path):
+ """Start `cmd` so that it outlives the hook that started it. Returns whether it started."""
+ try:
+ with open(log_path, "a") as log:
+ subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=log, stderr=log, cwd=cwd,
+ env=env, creationflags=_FLAGS | _BREAKAWAY, close_fds=True)
+ return True
+ except OSError:
+ pass # the job forbids breakaway: go through WMI
+ return _spawn_wmi(cmd, cwd, env, log_path)
+
+
+def _spawn_wmi(cmd, cwd, env, log_path):
+ fd, spec = tempfile.mkstemp(prefix="te-spawn-", suffix=".json",
+ dir=os.path.dirname(log_path))
+ with os.fdopen(fd, "w") as fh:
+ json.dump({"cmd": cmd, "cwd": cwd, "env": env, "log": log_path}, fh)
+ line = subprocess.list2cmdline([sys.executable, os.path.abspath(__file__), spec])
+ ps = ("$r = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments "
+ "@{ CommandLine = '%s'; CurrentDirectory = '%s' }; exit $r.ReturnValue"
+ % (line.replace("'", "''"), cwd.replace("'", "''")))
+ try:
+ done = subprocess.run(["powershell", "-NoProfile", "-NonInteractive", "-Command", ps],
+ stdin=subprocess.DEVNULL, capture_output=True, timeout=15,
+ creationflags=0x08000000)
+ except (OSError, subprocess.SubprocessError):
+ done = None
+ if done is None or done.returncode != 0:
+ try:
+ os.remove(spec)
+ except OSError:
+ pass
+ return False
+ return True
+
+
+def _run(spec_path):
+ with open(spec_path) as fh:
+ spec = json.load(fh)
+ try:
+ os.remove(spec_path)
+ except OSError:
+ pass
+ with open(spec["log"], "a") as log:
+ return subprocess.call(spec["cmd"], stdin=subprocess.DEVNULL, stdout=log, stderr=log,
+ cwd=spec["cwd"], env=spec["env"], creationflags=0x08000000)
+
+
+if __name__ == "__main__":
+ sys.exit(_run(sys.argv[1]))