mokner99's picture
Release validated November 18 expansion and measured usage receipts
ff376c5 verified
Raw History Blame Contribute Delete
7.75 kB
"""The same recipe contract in a local UI or headless workflow."""
import argparse
import json
import os
import sqlite3
from pathlib import Path
from .database import backup, inspect
from .engine import audit_expansion, build, dump
from .recipe import recommend, starter
from .spec import Recipe
def main():
from .config import load_project_env
load_project_env()
parser = argparse.ArgumentParser(prog="expander")
commands = parser.add_subparsers(dest="command", required=True)
serve = commands.add_parser("serve", help="Open the local recipe workbench")
serve.add_argument(
"--host", default="127.0.0.1", help="Bind address; use your LAN IP for network access"
)
serve.add_argument(
"--allow-host",
action="append",
default=[],
help="Additional trusted Host header, e.g. your LAN IP or hostname",
)
serve.add_argument("--backend", choices=["grok", "claude", "gateway"], default="grok")
serve.add_argument("--port", type=int, default=8765)
serve.add_argument("--root", type=Path, default=Path("runs/workbench"))
serve.add_argument(
"--ptc-run", type=Path, default=Path("runs/ptc-live"), help="Saved run for /ptc.html"
)
serve.add_argument("--benchmark-run", type=Path, default=Path("runs/benchmark-retail-114"))
imp = commands.add_parser(
"import", help="Snapshot a SQLite prototype and create a starter recipe"
)
imp.add_argument("database", type=Path)
imp.add_argument("destination", type=Path)
imp.add_argument("--docs", type=Path)
imp.add_argument("--tools", type=Path)
rec = commands.add_parser("recommend", help="Recommend a recipe through the selected backend")
rec.add_argument("--backend", choices=["grok", "claude", "gateway"], default="grok")
rec.add_argument("environment", type=Path)
rec.add_argument("output", type=Path)
rec.add_argument("--add-rows", type=int, help="Requested additions per table")
rec.add_argument("--guidance", default="", help="Generation goals and workload descriptions")
gen = commands.add_parser("build", help="Expand an environment using a saved recipe")
gen.add_argument("environment", type=Path)
gen.add_argument("recipe", type=Path)
gen.add_argument("--output", type=Path, default=Path("runs/builds"))
gen.add_argument("--no-model", action="store_true")
check = commands.add_parser(
"audit", help="Check an expanded SQLite database against a seed and recipe"
)
check.add_argument("environment", type=Path)
check.add_argument("recipe", type=Path)
check.add_argument("database", type=Path)
gen.add_argument(
"--cache",
type=Path,
help="Reuse validated model responses for repeatable builds and restart recovery",
)
ptc = commands.add_parser(
"ptc-demo", help="Expand car-rental data and run six checked PTC tasks"
)
ptc.add_argument("destination", type=Path, help="New directory for demo artifacts")
ptc.add_argument("--backend", choices=["grok", "claude", "gateway"], default="grok")
ptc.add_argument(
"--replay", type=Path, help="Replay programs from a saved model run without model calls"
)
ptc.add_argument(
"--scripted", action="store_true", help="Use labeled reference programs without model calls"
)
example = commands.add_parser("example", help="Create a runnable expansion example")
from .examples import NAMES
example.add_argument("name", choices=NAMES)
example.add_argument("destination", type=Path)
for command in (rec, gen):
command.add_argument(
"--key-file", type=Path, help="Read the gateway key from a private file"
)
command.add_argument("--base-url", help="Gateway HTTPS base URL")
command.add_argument("--model", help="Gateway model name")
args = parser.parse_args()
if getattr(args, "key_file", None):
os.environ["EXPANDER_GATEWAY_API_KEY"] = args.key_file.read_text().strip()
if getattr(args, "base_url", None):
os.environ["EXPANDER_GATEWAY_BASE_URL"] = args.base_url
if getattr(args, "model", None):
os.environ["EXPANDER_GATEWAY_MODEL"] = args.model
if args.command == "ptc-demo":
from .ptc.demo import run
result = run(args.destination, args.backend, args.scripted, args.replay)
print(json.dumps(result, indent=2))
if result["passed"] != result["tasks"]:
raise SystemExit(1)
elif args.command == "serve":
import uvicorn
from .server import create_app
hosts = args.allow_host + ([args.host] if args.host not in {"0.0.0.0", "::"} else [])
uvicorn.run(
create_app(
args.root,
allowed_hosts=hosts,
backend=args.backend,
ptc_run=args.ptc_run,
benchmark_run=args.benchmark_run,
),
host=args.host,
port=args.port,
)
elif args.command == "example":
from .examples import create_example
try:
print(create_example(args.name, args.destination) / "recipe.json")
except ValueError as exc:
parser.error(str(exc))
elif args.command == "import":
if args.destination.exists():
parser.error("Destination already exists; choose a new directory")
args.destination.mkdir(parents=True)
backup(args.database, args.destination / "seed.sqlite")
schema = inspect(args.destination / "seed.sqlite")
(args.destination / "docs.md").write_text(args.docs.read_text() if args.docs else "")
dump(
args.destination / "tools.json",
json.loads(args.tools.read_text()) if args.tools else {},
)
dump(args.destination / "recipe.json", starter(schema).model_dump())
print(args.destination / "recipe.json")
elif args.command == "recommend":
schema = inspect(args.environment / "seed.sqlite")
if args.add_rows is not None and args.add_rows < 0:
parser.error("--add-rows must be nonnegative")
targets = (
{
t["name"]: t["count"]
+ (
0
if any(c["opaque"] and not c["hidden"] for c in t["columns"])
else args.add_rows
)
for t in schema["tables"]
}
if args.add_rows is not None
else None
)
result = recommend(
schema,
(args.environment / "docs.md").read_text(),
json.loads((args.environment / "tools.json").read_text()),
args.environment,
backend=args.backend,
targets=targets,
guidance=args.guidance,
)
dump(args.output, result.model_dump())
print(args.output)
elif args.command == "audit":
try:
result = audit_expansion(
args.environment, Recipe.model_validate_json(args.recipe.read_text()), args.database
)
except (ValueError, sqlite3.Error) as exc:
parser.error(str(exc))
print(json.dumps(result, indent=2))
if not result["hard_pass"]:
raise SystemExit(1)
else:
try:
result = build(
args.environment,
Recipe.model_validate_json(args.recipe.read_text()),
args.output,
not args.no_model,
cache_dir=args.cache,
)
except (ValueError, sqlite3.Error) as exc:
parser.error(str(exc))
print(json.dumps(result, indent=2))
if __name__ == "__main__":
main()