fix: consolidate VSCodium wrapper with all fixes (text=True, messages check, stdin forwarder)

Consolidates duplicate wrapper generation code paths in cli.py and applies all
critical fixes for VS Codium integration:
- Replace deprecated universal_newlines with text=True
- Check for both 'journal' and 'messages' in filename patterns
- Add forward_stdin() function with stdin forwarder thread
- Properly forward stdin from VSCodium to vibe-acp binary
- Improve error handling with proper exception messages
- Restructure monitor to return observer instead of using globals

This ensures cost-tracker lines appear after every turn in VSCodium.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
nvaldez
2026-11-04 10:30:27 +01:00
parent 777a804125
commit 5f985db696
+73 -37
View File
@@ -143,16 +143,25 @@ def integrate_hooks(harness_name):
"cli": "mistral-vibe-cli",
"vscodium": "mistral-vibe-code",
"code": "mistral-vibe-code",
"hermes": "hermes",
"hermes-agent": "hermes",
}
canonical_name = harness_aliases.get(harness_name, harness_name)
if canonical_name == "hermes":
# Hermes integration is a plugin, not a hook script: one call
# copies the plugin, ships pricing.json, enables it, and
# restarts a running gateway daemon so it re-imports it.
from agent_cost_estimator.integration.hermes.install import main as hermes_install
return hermes_install([])
supported_harnesses = ["mistral-vibe-cli", "mistral-vibe-code"]
if canonical_name not in supported_harnesses:
print(f"error: unsupported harness '{harness_name}'. "
f"Supported harnesses: {', '.join(supported_harnesses + list(harness_aliases.keys()))}",
file=sys.stderr)
return 1
# Get the hook script path (from the package's modular structure)
hook_script_src = pathlib.Path(__file__).parent / "hooks" / canonical_name / "post_turn.py"
@@ -330,9 +339,15 @@ def remove_integrate_hooks(harness_name):
"cli": "mistral-vibe-cli",
"vscodium": "mistral-vibe-code",
"code": "mistral-vibe-code",
"hermes": "hermes",
"hermes-agent": "hermes",
}
canonical_name = harness_aliases.get(harness_name, harness_name)
if canonical_name == "hermes":
from agent_cost_estimator.integration.hermes.install import main as hermes_install
return hermes_install(["--uninstall"])
supported_harnesses = ["mistral-vibe-cli", "mistral-vibe-code"]
if canonical_name not in supported_harnesses:
print(f"error: unsupported harness '{harness_name}'. "
@@ -462,6 +477,20 @@ vibe_acp_process = None
stop_observer = False
def forward_stdin():
# Forward data from our stdin to vibe-acp's stdin.
# This is crucial for VSCodium to communicate with the real vibe-acp.
# Without this, VSCodium hangs during initialization.
global vibe_acp_process
try:
for line in sys.stdin:
if vibe_acp_process and vibe_acp_process.stdin:
vibe_acp_process.stdin.write(line)
vibe_acp_process.stdin.flush()
except Exception as e:
print(f"stdin forwarder error: {{e}}", file=sys.stderr)
class TurnCompletionHandler(FileSystemEventHandler):
def on_created(self, event):
self._check_journal(event)
@@ -470,59 +499,52 @@ class TurnCompletionHandler(FileSystemEventHandler):
self._check_journal(event)
def _check_journal(self, event):
global vibe_acp_process
if stop_observer:
return
if not event.is_directory:
path = pathlib.Path(event.src_path)
if "journal" in str(path) and path.suffix == ".jsonl":
self._run_hooks(path.parent.parent.name)
if not event.is_directory and ("journal" in str(event.src_path) or "messages" in str(event.src_path)) and event.src_path.endswith(".jsonl"):
session_dir = pathlib.Path(event.src_path).parent.parent
self._run_hooks(session_dir)
def _run_hooks(self, session_id):
def _run_hooks(self, session_dir):
global vibe_acp_process
hook_script = pathlib.Path(VIBE_HOME) / "hooks" / "post_turn.py"
if not hook_script.exists():
return
try:
payload = {{"session_id": session_id, "hook_event_name": "post_agent"}}
payload = {{"session_id": session_dir.name, "hook_event_name": "post_agent"}}
result = subprocess.run(
"python ~/.vibe/hooks/post_turn.py",
shell=True,
[sys.executable, str(hook_script)],
input=json.dumps(payload),
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0 and result.stdout:
if result.stdout:
# Send hook output to vibe-acp's stdin instead of printing to stdout
# This prevents breaking the ACP protocol
try:
if vibe_acp_process and vibe_acp_process.stdin:
vibe_acp_process.stdin.write(result.stdout + "\\n")
vibe_acp_process.stdin.write(result.stdout)
vibe_acp_process.stdin.flush()
except Exception:
pass
except Exception:
pass
# If we can't write to stdin, print to stderr for debugging
print(result.stdout, end='', flush=True, file=sys.stderr)
except Exception as e:
print(f"Hook error: {{e}}", file=sys.stderr)
def start_monitor():
global stop_observer
def monitor_thread():
global stop_observer
observer = Observer()
handler = TurnCompletionHandler()
for d in ["unified", "active"]:
p = pathlib.Path(VIBE_HOME) / "logs" / "session" / d
p.mkdir(parents=True, exist_ok=True)
observer.schedule(handler, str(p), recursive=True)
observer.start()
while not stop_observer:
import time
time.sleep(0.1)
observer.stop()
observer.join()
threading.Thread(target=monitor_thread, daemon=True).start()
observer = Observer()
handler = TurnCompletionHandler()
for d in ["unified", "active"]:
p = pathlib.Path(VIBE_HOME) / "logs" / "session" / d
p.mkdir(parents=True, exist_ok=True)
observer.schedule(handler, str(p), recursive=True)
observer.start()
return observer
def main():
global vibe_acp_process, stop_observer
start_monitor()
observer = start_monitor()
try:
vibe_acp_process = subprocess.Popen(
[REAL_ACP] + sys.argv[1:],
@@ -530,17 +552,31 @@ def main():
stdout=sys.stdout,
stderr=sys.stderr,
bufsize=1,
universal_newlines=True
text=True
)
# Start stdin forwarder thread - CRITICAL for ACP protocol
# Without this, VSCodium cannot communicate with vibe-acp
stdin_thread = threading.Thread(
target=forward_stdin,
name="vibe-acp-stdin-forwarder",
daemon=True
)
stdin_thread.start()
# Wait for vibe-acp to complete (or until VSCodium terminates us)
vibe_acp_process.wait()
sys.exit(vibe_acp_process.returncode)
except KeyboardInterrupt:
stop_observer = True
if vibe_acp_process:
vibe_acp_process.terminate()
observer.stop()
sys.exit(0)
finally:
stop_observer = True
observer.stop()
observer.join()
if vibe_acp_process:
try:
vibe_acp_process.stdin.close()