#!/usr/bin/env python3
"""Run the real CLI inventory using the captured real app-server config arguments."""
import json
import os
from pathlib import Path
import subprocess
import sys
root = Path(sys.argv[1]).resolve()
launches = [json.loads(line) for line in (root / 'launches.jsonl').read_text().splitlines()]
report = {}
for label, home, override in [('base', 'shadow-base', True), ('head', 'shadow-head', True),
                              ('explicit', 'shadow-explicit', True), ('base-control', 'shadow-base', False)]:
    launch = [entry for entry in launches
              if entry['CODEX_HOME'] == str(root / home) and entry['args'][0] == 'app-server'
              and ('model_auto_compact_token_limit=250000' in entry['args']) == override][-1]
    result = subprocess.run(['codex', 'plugin', 'list', '--json', *launch['args'][1:]],
                            env=dict(os.environ, CODEX_HOME=launch['CODEX_HOME']),
                            text=True, capture_output=True, check=True)
    report[label] = {'launch': launch, 'pluginListExit': result.returncode,
                     'pluginList': json.loads(result.stdout)}
output = json.dumps(report, indent=2)
for home in ['shadow-base', 'shadow-head', 'shadow-explicit', 'shared']:
    output = output.replace(str(root / home), '$' + home.upper().replace('-', '_'))
print(output)
