optz: Goal orchestrator

This commit is contained in:
thanhnv
2026-07-11 12:18:59 +09:00
parent b56f7d357e
commit 4fc72332f5
34 changed files with 850 additions and 452 deletions
@@ -117,11 +117,17 @@ def scan(text: str, mode: str):
output = os.path.join(directory, "output.txt")
with open(source, "w", encoding="utf-8") as handle:
handle.write(text)
environment = os.environ.copy()
# The goal workflow already records and enforces its H4 boundary. Keep
# deterministic injection/secret/PII checks active, but do not turn a
# temporary semantic-classifier outage into a false-positive block.
environment["CASAN_SECURITY_STRICT"] = "0"
result = subprocess.run(
["bash", SECURITY, source, output, mode],
cwd=ROOT,
capture_output=True,
text=True,
env=environment,
timeout=60,
)
safe = ""
@@ -141,6 +147,11 @@ def call_model(model: str, prompt: str, cloud: bool):
handle.write(prompt)
environment = os.environ.copy()
environment["CASAN_PREFLIGHT"] = "1" if cloud else "0"
# Long Markdown objectives need enough time for a local 9B model to
# ingest the brief and produce a bounded plan. The outer goal timeout
# remains the hard ceiling; this only raises the router's 60s default.
environment.setdefault("CASAN_MODEL_TIMEOUT_SEC", os.environ.get("CASAN_GOAL_LOCAL_TIMEOUT_SEC", "240") if not cloud else "120")
environment.setdefault("CASAN_MODEL_GENERATE_MAX_TOKENS", os.environ.get("CASAN_GOAL_MAX_OUTPUT_TOKENS", "1400"))
try:
result = subprocess.run(
["bash", MODEL_ROUTER, prompt_path, output_path, "--role", "generate", "--model", model],
@@ -292,7 +303,7 @@ def run(job_path: str) -> int:
if not ok:
stage(job_path, "local-worker", "error", reason, job.get("local_provider", ""), local_model)
emit(goal_id, "H2-tool", "error", "Local worker failed", {"reason": reason})
raise RuntimeError("local_worker_failed")
raise RuntimeError(f"local_worker_failed:{reason}")
allowed, safe_local = scan(local_draft, "output")
if not allowed:
emit(goal_id, "H4-security", "blocked", "Local worker output rejected")
@@ -40,14 +40,26 @@ start_auth_bridge() {
openssl rand -hex 32 > "$AUTH_BRIDGE_TOKEN_FILE"
chmod 600 "$AUTH_BRIDGE_TOKEN_FILE"
fi
if [[ -f "$AUTH_BRIDGE_PID_FILE" ]] && kill -0 "$(cat "$AUTH_BRIDGE_PID_FILE")" 2>/dev/null; then
if curl -fsS -m 2 "http://127.0.0.1:20130/healthz" >/dev/null 2>&1; then
return 0
fi
if [[ -f "$AUTH_BRIDGE_PID_FILE" ]]; then
local existing_pid
existing_pid="$(cat "$AUTH_BRIDGE_PID_FILE")"
# A PID can be stale or reused by an unrelated process. Health is the
# authoritative signal; clean the stale state before starting the bridge.
kill "$existing_pid" 2>/dev/null || true
rm -f "$AUTH_BRIDGE_PID_FILE"
fi
[[ -f "$AUTH_BRIDGE" ]] || { echo "CASAN_AUTH_BRIDGE_MISSING" >&2; return 1; }
nohup python3 "$AUTH_BRIDGE" --bind 0.0.0.0 --port 20130 --token-file "$AUTH_BRIDGE_TOKEN_FILE" --audit-log "$AUTH_BRIDGE_AUDIT" > "$AUTH_BRIDGE_LOG" 2>&1 &
echo "$!" > "$AUTH_BRIDGE_PID_FILE"
chmod 600 "$AUTH_BRIDGE_PID_FILE" "$AUTH_BRIDGE_LOG" "$AUTH_BRIDGE_AUDIT" 2>/dev/null || true
wait_url "http://127.0.0.1:20130/healthz"
if ! wait_url "http://127.0.0.1:20130/healthz"; then
kill "$(cat "$AUTH_BRIDGE_PID_FILE")" 2>/dev/null || true
rm -f "$AUTH_BRIDGE_PID_FILE"
return 1
fi
}
stop_auth_bridge() {
@@ -112,8 +124,8 @@ case "$CMD" in
bash "$INFRA" status
echo "=== control panel ==="
cp_compose ps
if [[ -f "$AUTH_BRIDGE_PID_FILE" ]] && kill -0 "$(cat "$AUTH_BRIDGE_PID_FILE")" 2>/dev/null; then
echo "provider_auth_bridge=running pid=$(cat "$AUTH_BRIDGE_PID_FILE")"
if curl -fsS -m 2 "http://127.0.0.1:20130/healthz" >/dev/null 2>&1; then
echo "provider_auth_bridge=running"
else
echo "provider_auth_bridge=stopped"
fi
@@ -171,6 +171,11 @@ def call_ollama(model_name, prompt, role):
# small budget is consumed by reasoning and `response` comes back empty.
body["think"] = False
body["options"]["num_predict"] = 16 # terse final answer + fast
else:
# Prevent an unconstrained local generation from consuming the whole
# request window. Goal Orchestrator can tune this without weakening the
# shorter classifier/judge budgets.
body["options"]["num_predict"] = max(64, int(os.environ.get("CASAN_MODEL_GENERATE_MAX_TOKENS", "1400")))
data = json.dumps(body).encode()
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
t0 = time.time()
@@ -322,7 +322,7 @@ def runtime_env(provider_id: str, model_id: str) -> int:
parsed = urlparse(endpoint)
env.update({
"CASAN_OLLAMA_HOST": parsed.netloc,
"OLLAMA_HOST": parsed.netloc,
"OLLAMA_HOST": endpoint,
"CASAN_ALLOW_DOCKER_HOST_OLLAMA": "1" if parsed.hostname == "host.docker.internal" else "0",
})
print(json.dumps({"success": True, "env": env}, ensure_ascii=False))
@@ -28,6 +28,10 @@ mkdir -p "$(dirname "$PIN_FILE")"
CMD="${1:-verify}"
MODEL="${2:-${CASAN_MODEL:-ornith:9b}}"
OLLAMA="${OLLAMA_HOST:-127.0.0.1:11434}"
case "$OLLAMA" in
http://*|https://*) OLLAMA_BASE="${OLLAMA%/}" ;;
*) OLLAMA_BASE="http://${OLLAMA%/}" ;;
esac
current_digest() {
# 1) explicit override (deterministic for CI/tests) — DISABLED in enforced mode.
@@ -44,7 +48,7 @@ current_digest() {
fi
# 2) live Ollama
local d
d="$(curl -sf "http://$OLLAMA/api/tags" 2>/dev/null | \
d="$(curl -sf "$OLLAMA_BASE/api/tags" 2>/dev/null | \
python3 -c "import json,sys
m=sys.argv[1]
try: d=json.load(sys.stdin)