A third decision-engine provider alongside the local SemIf GGUF and the remote Winnow server: `engine.provider == "ollama"` talks to ollama's native `/v1/systemone` route via `semif_agent/ollama_systemone.py`. It reuses the Winnow DecisionRequest -> choice mapping and the same strict distribution validation over exactly the requested option ids, and adds the edges only ollama has: - no `/health`: `warm()` probes `GET /api/version` + `POST /api/show`, which also proves the model is pulled and advertises the `decision` capability; - `engine.ollama.context_tokens: 0` follows the model's real `num_ctx` from `/api/show`; - `keep_alive` rides on the request, criteria descriptions use the ollama subset `string | null`, and a choice question needs >= 2 options; - decision models do not generate text, so `generate()` raises `EngineUnavailable` (use `llm.provider == "ollama"` for chat). Every failure is an `EngineUnavailable` (fatal, like the local engine) and the engine lock still serializes on the single-slot server. `cli.build_engine` selects it, the setup interview and `--answers` accept it, and the install plan skips the engine clone/GGUF/tokenizer for it. Docs, config.example.json, and tests updated.
1344 lines
52 KiB
Python
1344 lines
52 KiB
Python
"""CLI entrypoint: interactive REPL, scripted JSONL mode, and subcommands.
|
|
|
|
Run on a host with SemIf + a GGUF + a local OpenAI-compatible server:
|
|
|
|
python -m semif_agent.cli run # REPL
|
|
python -m semif_agent.cli run --script inputs.jsonl
|
|
python -m semif_agent.cli dream # prediction-observation cost report
|
|
python -m semif_agent.cli skills
|
|
python -m semif_agent.cli status
|
|
python -m semif_agent.cli relabel <id> <outcome>
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import faulthandler
|
|
import json
|
|
import os
|
|
import sys
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from .dream import dream as run_dream
|
|
from .codegen import CodegenClient
|
|
from .config import (
|
|
ConfigError,
|
|
DEFAULT_SECRETS_FILE,
|
|
generate_example_config,
|
|
load_config_file,
|
|
load_secrets,
|
|
missing_secret_refs,
|
|
resolve_secrets,
|
|
validate_config,
|
|
write_split_config,
|
|
)
|
|
from .console import (
|
|
OPENCODE_BASE_URL,
|
|
ConsoleCodegenClient,
|
|
ConsoleLLMClient,
|
|
)
|
|
from .decisions import DecisionRequest, Option
|
|
from .engine import DecisionEngine, EngineConfig, EngineUnavailable, SemIfEngine
|
|
from .ollama_systemone import OllamaSystemOneConfig, OllamaSystemOneEngine
|
|
from .winnow import WinnowConfig, WinnowEngine
|
|
from .llm import LLMClient, SemIfLLMClient
|
|
from .log import DecisionLog
|
|
from .packs import load_packs
|
|
from .scheduler import Scheduler
|
|
from .setup import run_setup as run_bootstrap_setup
|
|
from .skills import tree_summary
|
|
from .trace import TraceLog
|
|
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
DEFAULT_WINNOW_URL = "http://127.0.0.1:8091"
|
|
DEFAULT_OLLAMA_SYSTEMONE_URL = "http://127.0.0.1:11434"
|
|
|
|
_PATH_KEYS = (
|
|
"skill_packs",
|
|
"skill_bodies",
|
|
"category_registry",
|
|
"deleted_skills",
|
|
"log",
|
|
"trace",
|
|
)
|
|
_PATH_DEFAULTS = {
|
|
"skill_packs": "packs",
|
|
"skill_bodies": "data/skills",
|
|
"category_registry": "data/categories.json",
|
|
"deleted_skills": "data/deleted_skills.json",
|
|
"log": "data/decisions.jsonl",
|
|
"trace": "data/runs.jsonl",
|
|
}
|
|
|
|
|
|
def _anchor_path(value: str) -> str:
|
|
path = Path(value)
|
|
return str(path if path.is_absolute() else REPO_ROOT / path)
|
|
|
|
|
|
def _resolve_api_key(cfg: dict) -> str:
|
|
"""The configured API key.
|
|
|
|
Secrets live in the gitignored `secrets.json` and are resolved to a literal
|
|
by `load_config` (a `"!secret <key>"` value becomes its value); a literal key
|
|
in the block also works. There is deliberately no `api_key_env` — secrets.json
|
|
is the one place credentials live.
|
|
"""
|
|
return str(cfg.get("api_key") or "")
|
|
|
|
|
|
def _opt_float(value) -> float | None:
|
|
"""Config number for an opt-in provider parameter.
|
|
|
|
A missing/`null` key yields `None`, which the provider omits from the
|
|
request so the server's model default applies. A present value is coerced
|
|
to `float`.
|
|
"""
|
|
return None if value is None else float(value)
|
|
|
|
|
|
def _required_model(cfg: dict, provider: str) -> str:
|
|
"""The configured model name, or a clear fatal error.
|
|
|
|
There is no default: which model an endpoint serves is deployment-specific,
|
|
so the per-machine config must name it rather than the code guessing a model
|
|
the endpoint may not have. Failing fast turns a missing model into an
|
|
actionable config error instead of a request to a model that isn't there.
|
|
"""
|
|
model = str(cfg.get("model") or "").strip()
|
|
if not model:
|
|
raise SystemExit(
|
|
f"config.json: {provider}.model is required "
|
|
f"(set it to a model your {provider} endpoint serves)"
|
|
)
|
|
return model
|
|
|
|
|
|
def _provider_endpoint(cfg: dict, default_base: str) -> dict:
|
|
"""Endpoint kwargs shared by the llm/codegen clients.
|
|
|
|
`provider` selects the class (`opencode` → the Console client) and the base
|
|
URL default: an explicit `base_url` always wins, else the Console URL for
|
|
`opencode` and `default_base` (ollama) otherwise. Auth comes from `api_key`
|
|
(a literal or a `!secret` value already resolved by `load_config`);
|
|
`extra_headers` passes through for custom schemes.
|
|
"""
|
|
provider = str(cfg.get("provider", "ollama") or "ollama").lower()
|
|
base_url = cfg.get("base_url") or (
|
|
OPENCODE_BASE_URL if provider == "opencode" else default_base
|
|
)
|
|
return {
|
|
"base_url": base_url,
|
|
"api_key": _resolve_api_key(cfg),
|
|
"extra_headers": cfg.get("extra_headers") or None,
|
|
"user_agent": cfg.get("user_agent"),
|
|
}
|
|
|
|
|
|
def load_config(path: str = "config.json") -> dict:
|
|
"""Load the per-machine config and anchor its runtime paths to the checkout.
|
|
|
|
config.json is a thin index (`{"include": ["config/core.json", ...]}`); each
|
|
included fragment is deep-merged in order (later wins). Secrets are written
|
|
as `"!secret <key>"` and resolved from the gitignored secrets file (default
|
|
`secrets.json` beside the index). The config is required: bootstrap seeds it
|
|
once and only a human edits it — there is no in-code fallback.
|
|
|
|
Path-valued keys (`skill_packs`, `skill_bodies`, `category_registry`, `log`,
|
|
`trace`) resolve against REPO_ROOT, not the process cwd, so a fresh clone
|
|
loads its packs and writes runtime artifacts into the checkout no
|
|
matter where the CLI is invoked from. Absolute values pass through untouched.
|
|
"""
|
|
config_path = Path(path)
|
|
try:
|
|
config = load_config_file(config_path)
|
|
except ConfigError as exc:
|
|
raise SystemExit(str(exc))
|
|
secrets_path = config.get("secrets_file") or DEFAULT_SECRETS_FILE
|
|
secrets_path = Path(secrets_path)
|
|
if not secrets_path.is_absolute():
|
|
secrets_path = config_path.parent / secrets_path
|
|
config = resolve_secrets(config, load_secrets(secrets_path))
|
|
for key in _PATH_KEYS:
|
|
value = config.get(key)
|
|
if isinstance(value, list):
|
|
config[key] = [_anchor_path(str(item)) for item in value]
|
|
else:
|
|
config[key] = _anchor_path(str(value or _PATH_DEFAULTS[key]))
|
|
return config
|
|
|
|
|
|
def load_pins(path: str | None = None) -> dict:
|
|
"""Load the committed pin manifest (pins.json) that owns every external ref.
|
|
|
|
Pins are code, not per-machine config: the SemIf engine commit, GGUF
|
|
url+sha256, HF tokenizer revision, and simplex-chat version/url+sha256 live
|
|
in pins.json so a `git pull` bump propagates on the next bootstrap run
|
|
instead of being frozen in each machine's config.json.
|
|
"""
|
|
pins_path = Path(path) if path else REPO_ROOT / "pins.json"
|
|
if not pins_path.is_file():
|
|
raise SystemExit(f"pins.json not found at {pins_path}")
|
|
return json.loads(pins_path.read_text())
|
|
|
|
|
|
def build_engine_config(config: dict, pins: dict | None = None) -> EngineConfig:
|
|
"""Engine settings for a run: per-machine tuning from config.json, pinned
|
|
refs from pins.json.
|
|
|
|
`engine.backend`/`context_tokens`/`threads` are per-machine config. The
|
|
tokenizer source/revision and the GGUF are pins; the GGUF path is derived
|
|
from the pin's filename under `.runtime/models/`, unless config.json sets an
|
|
explicit `engine.gguf` override.
|
|
"""
|
|
pins = load_pins() if pins is None else pins
|
|
eng = config.get("engine", {}) or {}
|
|
pin_eng = pins.get("engine", {}) or {}
|
|
override = eng.get("gguf")
|
|
gguf = _anchor_path(override) if override else str(
|
|
REPO_ROOT / ".runtime" / "models" / os.path.basename(pin_eng.get("gguf_url", ""))
|
|
)
|
|
gpu_device = eng.get("gpu_device")
|
|
return EngineConfig(
|
|
backend=eng.get("backend", "llamacpp"),
|
|
source=pin_eng.get("source", ""),
|
|
revision=pin_eng.get("revision", ""),
|
|
gguf=gguf,
|
|
context_tokens=int(eng.get("context_tokens", 4096)),
|
|
threads=eng.get("threads"),
|
|
gpu_layers=int(eng.get("gpu_layers", 0)),
|
|
gpu_device=None if gpu_device is None else int(gpu_device),
|
|
gpu_split=int(eng.get("gpu_split", 0)),
|
|
)
|
|
|
|
|
|
def build_engine(config: dict, pins: dict | None = None) -> DecisionEngine:
|
|
"""Build the decision engine from the `engine` config block.
|
|
|
|
`provider` selects the implementation, mirroring `llm`/`codegen`:
|
|
|
|
* ``"semif"`` (default) — the local llama.cpp GGUF (`SemIfEngine`), built
|
|
from the per-machine `engine` knobs + the pinned refs in pins.json.
|
|
* ``"winnow"`` — a remote Winnow `/v1/systemone` typed-decision API
|
|
(`WinnowEngine`), configured by the nested `engine.winnow` block
|
|
(`base_url`/`model`/`api_key`/`timeout`/`retries`).
|
|
* ``"ollama"`` — ollama's native System One `/v1/systemone` route
|
|
(`OllamaSystemOneEngine`), configured by the nested `engine.ollama` block
|
|
(`base_url`/`model`/`keep_alive`/`api_key`/`timeout`/`retries`), probed
|
|
through ollama's own `/api/version` + `/api/show` endpoints.
|
|
|
|
The engine is always real: a provider that cannot be reached raises
|
|
`EngineUnavailable` at call time, which the scheduler treats as fatal. The
|
|
`winnow` and `ollama` models are required (no default) exactly as
|
|
`llm.model`/`codegen.model` are, so a misconfigured endpoint fails as an
|
|
actionable config error.
|
|
"""
|
|
eng = config.get("engine", {}) or {}
|
|
provider = str(eng.get("provider", "semif") or "semif").lower()
|
|
if provider == "winnow":
|
|
win = eng.get("winnow", {}) or {}
|
|
endpoint = _provider_endpoint(win, DEFAULT_WINNOW_URL)
|
|
return WinnowEngine(
|
|
WinnowConfig(
|
|
base_url=endpoint["base_url"],
|
|
model=_required_model(win, "engine.winnow"),
|
|
context_tokens=int(win.get("context_tokens", 32768)),
|
|
timeout=float(win.get("timeout", 120.0)),
|
|
retries=int(win.get("retries", 0)),
|
|
api_key=endpoint["api_key"],
|
|
extra_headers=endpoint["extra_headers"],
|
|
user_agent=str(win.get("user_agent") or "semif-agent"),
|
|
)
|
|
)
|
|
if provider == "ollama":
|
|
block = eng.get("ollama", {}) or {}
|
|
endpoint = _provider_endpoint(block, DEFAULT_OLLAMA_SYSTEMONE_URL)
|
|
return OllamaSystemOneEngine(
|
|
OllamaSystemOneConfig(
|
|
base_url=endpoint["base_url"],
|
|
model=_required_model(block, "engine.ollama"),
|
|
context_tokens=int(block.get("context_tokens", 16384)),
|
|
timeout=float(block.get("timeout", 120.0)),
|
|
retries=int(block.get("retries", 0)),
|
|
api_key=endpoint["api_key"],
|
|
extra_headers=endpoint["extra_headers"],
|
|
user_agent=str(block.get("user_agent") or "semif-agent"),
|
|
keep_alive=str(block.get("keep_alive") or ""),
|
|
)
|
|
)
|
|
return SemIfEngine(build_engine_config(config, pins))
|
|
|
|
|
|
def build_codegen_client(config: dict) -> CodegenClient | None:
|
|
"""Build the codegen client from the `codegen` config block.
|
|
|
|
`provider` selects the class (`opencode` -> the Console client, which adds
|
|
bearer auth and a User-Agent) and the endpoint/auth come from
|
|
`_provider_endpoint`. Shared by `build_scheduler` and the integration tests
|
|
so both exercise the configured provider rather than assuming ollama.
|
|
|
|
Returns ``None`` when no `codegen.model` is configured: skill authoring is
|
|
an opt-in capability, so a host that only runs the shipped packs (the
|
|
minimum path) needs no codegen endpoint and the app must not fail without
|
|
one. The scheduler guards every authoring path against a missing client.
|
|
"""
|
|
codegen_cfg = config.get("codegen", {}) or {}
|
|
model = str(codegen_cfg.get("model") or "").strip()
|
|
if not model:
|
|
return None
|
|
deg_cfg = codegen_cfg.get("degeneration", {}) or {}
|
|
provider = str(codegen_cfg.get("provider", "ollama") or "ollama").lower()
|
|
client_cls = ConsoleCodegenClient if provider == "opencode" else CodegenClient
|
|
endpoint = _provider_endpoint(
|
|
codegen_cfg,
|
|
config.get("llm", {}).get("base_url", "http://localhost:11434/v1"),
|
|
)
|
|
return client_cls(
|
|
base_url=endpoint["base_url"],
|
|
model=model,
|
|
timeout=float(codegen_cfg.get("timeout", 1200.0)),
|
|
stream=bool(codegen_cfg.get("stream", False)),
|
|
idle_warn=float(codegen_cfg.get("idle_warn", 60.0)),
|
|
idle_timeout=float(codegen_cfg.get("idle_timeout", 180.0)),
|
|
context_window=float(codegen_cfg.get("context_window", 0.0)),
|
|
smart_limit=int(codegen_cfg.get("smart_limit", 250000)),
|
|
warn_limit=int(codegen_cfg.get("warn_limit", 500000)),
|
|
max_fill_ratio=float(codegen_cfg.get("max_fill_ratio", 0.9)),
|
|
warn_fill_ratio=float(codegen_cfg.get("warn_fill_ratio", 0.7)),
|
|
max_output=float(codegen_cfg.get("max_output", 0.85)),
|
|
chars_per_token=float(codegen_cfg.get("chars_per_token", 4.0)),
|
|
degeneration_interval=int(deg_cfg.get("interval", 8000)),
|
|
degeneration_window=int(deg_cfg.get("window", 2000)),
|
|
degeneration_min_chars=int(deg_cfg.get("min_chars", 4000)),
|
|
temperature=_opt_float(codegen_cfg.get("temperature")),
|
|
top_p=_opt_float(codegen_cfg.get("top_p")),
|
|
presence_penalty=_opt_float(codegen_cfg.get("presence_penalty")),
|
|
frequency_penalty=_opt_float(codegen_cfg.get("frequency_penalty")),
|
|
max_attempts=int(codegen_cfg.get("max_attempts", 3)),
|
|
api_key=endpoint["api_key"],
|
|
extra_headers=endpoint["extra_headers"],
|
|
user_agent=endpoint["user_agent"],
|
|
)
|
|
|
|
|
|
def build_llm_client(config: dict, engine) -> LLMClient | None:
|
|
"""Build the `llm` title/description client from the `llm` config block.
|
|
|
|
`provider` selects the backend: `"semif"` drives the already-loaded SemIf
|
|
engine model in-process (the GGUF is the model — no endpoint or `model` is
|
|
needed), `"opencode"` the hosted Console client, and anything else the
|
|
default ollama-compatible `LLMClient`. Shared by `build_scheduler` and tests
|
|
so both exercise the configured provider rather than assuming ollama.
|
|
|
|
Returns ``None`` when a non-`semif` provider has no `model`: the `llm` client
|
|
is only needed for skill authoring and gateway result cleanup, both optional.
|
|
"""
|
|
llm_cfg = config.get("llm", {}) or {}
|
|
provider = str(llm_cfg.get("provider", "ollama") or "ollama").lower()
|
|
if provider == "semif":
|
|
semif_cfg = llm_cfg.get("semif", {}) or {}
|
|
return SemIfLLMClient(
|
|
engine,
|
|
temperature=float(semif_cfg.get("temperature", 0.0)),
|
|
max_tokens=int(semif_cfg.get("max_tokens", 256)),
|
|
)
|
|
model = str(llm_cfg.get("model") or "").strip()
|
|
if not model:
|
|
return None
|
|
llm_cls = ConsoleLLMClient if provider == "opencode" else LLMClient
|
|
endpoint = _provider_endpoint(llm_cfg, "http://localhost:11434/v1")
|
|
return llm_cls(
|
|
base_url=endpoint["base_url"],
|
|
model=model,
|
|
timeout=float(llm_cfg.get("timeout", 600.0)),
|
|
stream=bool(llm_cfg.get("stream", False)),
|
|
idle_warn=float(llm_cfg.get("idle_warn", 30.0)),
|
|
idle_timeout=float(llm_cfg.get("idle_timeout", 120.0)),
|
|
context_window=float(llm_cfg.get("context_window", 0.0)),
|
|
temperature=_opt_float(llm_cfg.get("temperature")),
|
|
top_p=_opt_float(llm_cfg.get("top_p")),
|
|
presence_penalty=_opt_float(llm_cfg.get("presence_penalty")),
|
|
frequency_penalty=_opt_float(llm_cfg.get("frequency_penalty")),
|
|
disable_thinking=bool(llm_cfg.get("disable_thinking", True)),
|
|
api_key=endpoint["api_key"],
|
|
extra_headers=endpoint["extra_headers"],
|
|
user_agent=endpoint["user_agent"],
|
|
)
|
|
|
|
|
|
def build_scheduler(config: dict) -> tuple[Scheduler, dict]:
|
|
# Bridge listener addresses/tokens are derived from each bridge's own
|
|
# `bridges.<name>` block (host/port/token) — the single source of truth.
|
|
# There is no duplicate top-level *_bridge_url to drift; skills read these
|
|
# from ctx.config and the contract config search finds them here.
|
|
from .bridges.registry import derived_config_vars
|
|
|
|
config = {**config, **derived_config_vars(config)}
|
|
engine = build_engine(config)
|
|
llm = build_llm_client(config, engine)
|
|
log = DecisionLog(config.get("log", "data/decisions.jsonl"))
|
|
trace = TraceLog(config.get("trace", "data/runs.jsonl"))
|
|
codegen_cfg = config.get("codegen", {})
|
|
deg_cfg = codegen_cfg.get("degeneration", {}) or {}
|
|
codegen = build_codegen_client(config)
|
|
if str(config.get("new_skill_creation", "deny") or "deny").lower() != "deny":
|
|
if codegen is None:
|
|
print(
|
|
"WARN: new_skill_creation is enabled but codegen.model is not "
|
|
"set — skill authoring will not run. Set codegen.model (and "
|
|
"base_url) to enable it.",
|
|
file=sys.stderr,
|
|
)
|
|
if llm is None:
|
|
print(
|
|
"WARN: new_skill_creation is enabled but llm.model is not set — "
|
|
"skill authoring will not run. Set llm.model (and base_url), or "
|
|
"llm.provider 'semif' to use the decision model.",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
def make_degeneration_check(run_id: str):
|
|
"""2-option SemIf decision (continue/stop) that aborts a degenerating
|
|
codegen stream: P(stop) >= threshold returns an abort reason. Recorded
|
|
as a trace-only event (kind `codegen`, with probs) — never in the
|
|
decision log. Disabled when no engine (EngineUnavailable at call time
|
|
degrades to "keep going"), which keeps CodegenClient standalone pure."""
|
|
if not bool(deg_cfg.get("enabled", True)):
|
|
return None
|
|
threshold = float(deg_cfg.get("threshold", 0.9))
|
|
|
|
def check(recent: str) -> str | None:
|
|
decision = DecisionRequest(
|
|
state=f"[codegen {codegen.model}] {recent[-2000:]}",
|
|
question="Is this skill-body generation degenerating (looping or repeating instead of converging)?",
|
|
options=[
|
|
Option("continue", "Continue; it is still making progress."),
|
|
Option("stop", "Stop; it is degenerating."),
|
|
],
|
|
)
|
|
try:
|
|
result = engine.call(decision)
|
|
except EngineUnavailable:
|
|
return None
|
|
trace.append(
|
|
"codegen",
|
|
run_id,
|
|
state=decision.state,
|
|
question=decision.question,
|
|
options=[o.id for o in decision.options],
|
|
selected=result.selected,
|
|
probs=result.probs,
|
|
stop_prob=result.prob("stop"),
|
|
)
|
|
if result.prob("stop") >= threshold:
|
|
return (
|
|
f"P(stop)={result.prob('stop'):.2f} >= threshold {threshold}"
|
|
)
|
|
return None
|
|
|
|
return check
|
|
|
|
def make_regen_decision(run_id: str):
|
|
"""2-option SemIf decision on a failing skill test: which artifact to
|
|
regenerate (the body — which carries the contract — or the test; fixture
|
|
data lives inside the test, so a fixture fix is a test regen). Recorded
|
|
as a trace-only event (kind `codegen_regen`) — never in the decision
|
|
log. Engine unavailable at call time degrades to `regen_test`."""
|
|
def decide(reason: str) -> str:
|
|
decision = DecisionRequest(
|
|
state=f"[testgen {codegen.model}] skill test failed. {reason[-1200:]}",
|
|
question="The auto-run test failed. What should be regenerated to fix it?",
|
|
options=[
|
|
Option("regen_code", "Regenerate the skill body code."),
|
|
Option("regen_test", "Regenerate the test only."),
|
|
],
|
|
)
|
|
try:
|
|
result = engine.call(decision)
|
|
except EngineUnavailable:
|
|
return "regen_test"
|
|
trace.append(
|
|
"codegen_regen",
|
|
run_id,
|
|
state=decision.state,
|
|
question=decision.question,
|
|
options=[o.id for o in decision.options],
|
|
selected=result.selected,
|
|
probs=result.probs,
|
|
reason=reason[-2000:],
|
|
)
|
|
return result.selected
|
|
|
|
return decide
|
|
|
|
scheduler = Scheduler(
|
|
engine=engine,
|
|
llm=llm,
|
|
log=log,
|
|
config=config,
|
|
tau=float(config.get("tau", 0.6)),
|
|
max_reentries=int(config.get("max_reentries", 3)),
|
|
trace=trace,
|
|
codegen=codegen,
|
|
degeneration_check_factory=make_degeneration_check,
|
|
regen_decision_factory=make_regen_decision,
|
|
)
|
|
# Bridges ship with their packs; tell the registry which packs this host
|
|
# loaded so `describe_bridges()` (called from codegen with no config) lists
|
|
# the real set rather than a default-root scan.
|
|
from .bridges.registry import set_bridge_packs
|
|
|
|
set_bridge_packs(scheduler.packs)
|
|
return scheduler, config
|
|
|
|
|
|
def try_warm(scheduler: Scheduler) -> str:
|
|
try:
|
|
scheduler.engine.warm()
|
|
return "decision engine loaded."
|
|
except EngineUnavailable as exc:
|
|
return f"decision engine unavailable: {exc}"
|
|
|
|
|
|
def _answer_questions(scheduler: Scheduler) -> None:
|
|
"""Ask any deferred authoring/repair questions at the REPL."""
|
|
while True:
|
|
pending = scheduler.pending_questions()
|
|
if not pending:
|
|
return
|
|
question = pending[0]
|
|
answer = input(
|
|
f"[{question['skill']}] {question['question']}\n"
|
|
"answer (empty to skip): "
|
|
)
|
|
status, detail = scheduler.answer_question(question["id"], answer)
|
|
print(f"[{status}] {detail}")
|
|
|
|
|
|
def _print_repairs(scheduler: Scheduler) -> None:
|
|
for offer in scheduler.pending_repairs():
|
|
print(
|
|
f"[repair] {offer['id']} {offer['category']}.{offer['skill']} "
|
|
f"suggested={offer['selected']} {offer['failure'][:120]}"
|
|
)
|
|
|
|
|
|
def _answer_approvals(scheduler: Scheduler) -> None:
|
|
"""Ask any pending creation approvals at the REPL.
|
|
|
|
Only prompts when a category (or the global default) uses the `ask` mode
|
|
(the scheduler posts nothing otherwise). An empty answer denies.
|
|
"""
|
|
while True:
|
|
pending = scheduler.pending_approvals()
|
|
if not pending:
|
|
return
|
|
item = pending[0]
|
|
target = (
|
|
item["category"]
|
|
if item["kind"] == "category"
|
|
else f"{item['category']}.{item['skill']}"
|
|
)
|
|
answer = input(
|
|
f"[approval] create new {item['kind']} {target}: {item['description']}\n"
|
|
"approve? (y/N): "
|
|
).strip().lower()
|
|
status, detail = scheduler.answer_approval(
|
|
item["id"], answer in ("y", "yes")
|
|
)
|
|
print(f"[{status}] {detail}")
|
|
|
|
|
|
def _print_approvals(scheduler: Scheduler) -> None:
|
|
for item in scheduler.pending_approvals():
|
|
target = (
|
|
item["category"]
|
|
if item["kind"] == "category"
|
|
else f"{item['category']}.{item['skill']}"
|
|
)
|
|
print(f"[approval] {item['id']} new {item['kind']} {target}: {item['description']}")
|
|
|
|
|
|
def _print_reply(reply, show_run_id: bool = False, verbosity: str = "result") -> None:
|
|
"""Print a scheduler reply, honoring the configured verbosity.
|
|
|
|
"result" shows only the final answer of a request (an intermediate chain
|
|
step is hidden); "query" also shows intermediate steps; "decision"
|
|
additionally prints each SemIf decision phase for the run.
|
|
"""
|
|
if getattr(reply, "intermediate", False) and verbosity == "result":
|
|
return
|
|
label = f"[{reply.run_id}] " if (show_run_id and reply.run_id) else ""
|
|
print(f"[{reply.status}] {label}{reply.text}")
|
|
for line in getattr(reply, "decision_lines", ()) or ():
|
|
print(f" {line}")
|
|
|
|
|
|
def _drain_queue(scheduler: Scheduler) -> None:
|
|
"""Run queued work, printing each outcome with its run id.
|
|
|
|
Routes any replies a background drain buffered (a post-authoring re-run, a
|
|
repair retry) before the queued work, so an off-gate completion still prints.
|
|
"""
|
|
verbosity = getattr(scheduler, "verbosity", "result")
|
|
for reply in [*scheduler.drain_outbox(), *scheduler.run_queue()]:
|
|
_print_reply(reply, show_run_id=True, verbosity=verbosity)
|
|
|
|
|
|
def _fatal_exit(scheduler: Scheduler) -> int | None:
|
|
"""If the scheduler hit a fatal engine failure, print it and return non-zero.
|
|
|
|
The decision engine is always real; if it goes unavailable mid-run the app
|
|
cannot make decisions and must exit rather than degrade.
|
|
"""
|
|
if scheduler.fatal is None:
|
|
return None
|
|
print(scheduler.fatal, file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
def _start_timer_printer(scheduler: Scheduler) -> None:
|
|
"""Print fired timers/alarms while the REPL is blocked on `input()`.
|
|
|
|
A background daemon thread polls the scheduler's timer service and prints
|
|
each fired notification, so a timer set during a session is delivered even
|
|
when the prompt is idle. Timers are in-process: they die with this process.
|
|
"""
|
|
|
|
def loop() -> None:
|
|
while True:
|
|
time.sleep(0.5)
|
|
for fired in scheduler.timers.drain():
|
|
print(f"\n[timer] {fired.message}")
|
|
|
|
threading.Thread(target=loop, name="timer-printer", daemon=True).start()
|
|
|
|
|
|
def repl(scheduler: Scheduler, config: dict) -> int:
|
|
scheduler.defer_questions = True
|
|
_start_timer_printer(scheduler)
|
|
print(try_warm(scheduler))
|
|
print(
|
|
"type a request, or one of: busy <text> | idle | status | skills | dream | "
|
|
"relabel <id> <outcome> | restart <category> <skill> | "
|
|
"repairs | repair <offer-id> [action] | approvals | quit"
|
|
)
|
|
print(
|
|
"housekeeping (slash-prefixed so plain requests still route by SemIf): "
|
|
"/delete <category> <skill> | /clear-config <category> <skill> | "
|
|
"/regen <category> <skill> | /cancel-build <category> <skill> | "
|
|
"/mode <category> allow|deny|ask"
|
|
)
|
|
while True:
|
|
_answer_questions(scheduler)
|
|
_answer_approvals(scheduler)
|
|
try:
|
|
line = input("> ").strip()
|
|
except (EOFError, KeyboardInterrupt):
|
|
print()
|
|
break
|
|
if not line:
|
|
continue
|
|
lower = line.lower()
|
|
if lower in ("quit", "exit"):
|
|
break
|
|
if lower == "status":
|
|
print(scheduler.status())
|
|
continue
|
|
if lower == "skills":
|
|
print(tree_summary(scheduler.tree))
|
|
continue
|
|
if lower == "dream":
|
|
print(run_dream(scheduler.log).render())
|
|
continue
|
|
if lower == "repairs":
|
|
_print_repairs(scheduler)
|
|
continue
|
|
if lower == "approvals":
|
|
_print_approvals(scheduler)
|
|
continue
|
|
if lower.startswith(("repair ", "/repair ")):
|
|
parts = line.lstrip("/").split()
|
|
if len(parts) not in (2, 3):
|
|
print("usage: repair <offer-id> [retry|repair_skill|ask_user|no_repair]")
|
|
continue
|
|
status, detail = scheduler.resolve_repair(
|
|
parts[1], parts[2] if len(parts) == 3 else None
|
|
)
|
|
print(f"[{status}] {detail}")
|
|
_answer_questions(scheduler)
|
|
_answer_approvals(scheduler)
|
|
_drain_queue(scheduler)
|
|
continue
|
|
if lower.startswith("relabel "):
|
|
parts = line.split()
|
|
if len(parts) != 3:
|
|
print("usage: relabel <id> <outcome>")
|
|
continue
|
|
ok = scheduler.log.relabel(parts[1], parts[2])
|
|
print("relabeled." if ok else f"no row with id {parts[1]}")
|
|
continue
|
|
if lower.startswith(("restart ", "/restart ")):
|
|
parts = line.lstrip("/").split()
|
|
if len(parts) != 3:
|
|
print("usage: restart <category> <skill>")
|
|
continue
|
|
status, detail = scheduler.restart_skill(parts[1], parts[2])
|
|
print(f"[{status}] {detail}")
|
|
continue
|
|
if lower.startswith("/delete "):
|
|
parts = line.lstrip("/").split()
|
|
if len(parts) != 3:
|
|
print("usage: /delete <category> <skill>")
|
|
continue
|
|
status, detail = scheduler.delete_skill(parts[1], parts[2])
|
|
print(f"[{status}] {detail}")
|
|
continue
|
|
if lower.startswith("/clear-config "):
|
|
parts = line.lstrip("/").split()
|
|
if len(parts) != 3:
|
|
print("usage: /clear-config <category> <skill>")
|
|
continue
|
|
status, detail = scheduler.clear_skill_config(parts[1], parts[2])
|
|
print(f"[{status}] {detail}")
|
|
continue
|
|
if lower.startswith("/regen "):
|
|
parts = line.lstrip("/").split()
|
|
if len(parts) != 3:
|
|
print("usage: /regen <category> <skill>")
|
|
continue
|
|
status, detail = scheduler.regen_skill(parts[1], parts[2])
|
|
print(f"[{status}] {detail}")
|
|
continue
|
|
if lower.startswith("/cancel-build "):
|
|
parts = line.lstrip("/").split()
|
|
if len(parts) != 3:
|
|
print("usage: /cancel-build <category> <skill>")
|
|
continue
|
|
status, detail = scheduler.cancel_skill_build(parts[1], parts[2])
|
|
print(f"[{status}] {detail}")
|
|
continue
|
|
if lower.startswith("/mode "):
|
|
parts = line.lstrip("/").split()
|
|
if len(parts) != 3 or parts[2].lower() not in ("allow", "deny", "ask"):
|
|
print("usage: /mode <category> allow|deny|ask")
|
|
continue
|
|
status, detail = scheduler.set_category_mode(parts[1], parts[2].lower())
|
|
print(f"[{status}] {detail}")
|
|
continue
|
|
if lower == "idle":
|
|
scheduler.idle()
|
|
print("current process cleared.")
|
|
continue
|
|
if lower.startswith("busy "):
|
|
scheduler.busy(line[5:].strip())
|
|
print("current process set (busy).")
|
|
continue
|
|
_print_reply(scheduler.submit(line), verbosity=scheduler.verbosity)
|
|
if _fatal_exit(scheduler) is not None:
|
|
return 1
|
|
while scheduler.pending is not None:
|
|
hint = "" if scheduler.pending.pre_act else " [leave empty to skip]"
|
|
answer = input(f"{scheduler.pending.question}{hint} ")
|
|
_print_reply(scheduler.answer(answer), verbosity=scheduler.verbosity)
|
|
if _fatal_exit(scheduler) is not None:
|
|
return 1
|
|
_drain_queue(scheduler)
|
|
if _fatal_exit(scheduler) is not None:
|
|
return 1
|
|
_answer_questions(scheduler)
|
|
_answer_approvals(scheduler)
|
|
_print_repairs(scheduler)
|
|
return _fatal_exit(scheduler) or 0
|
|
|
|
|
|
def scripted(scheduler: Scheduler, path: str) -> int:
|
|
print(try_warm(scheduler))
|
|
rows = [json.loads(line) for line in Path(path).read_text().splitlines() if line.strip()]
|
|
for row in rows:
|
|
_print_reply(
|
|
scheduler.submit(str(row["text"]), source=row.get("source", "scripted")),
|
|
verbosity=scheduler.verbosity,
|
|
)
|
|
if _fatal_exit(scheduler) is not None:
|
|
return 1
|
|
_drain_queue(scheduler)
|
|
return _fatal_exit(scheduler) or 0
|
|
|
|
|
|
def _gateway_address_callback(adapter):
|
|
"""Return an `on_connected` coroutine that prints the bot's contact link once.
|
|
|
|
A human cannot reach the gateway without the bot's SimpleX contact address,
|
|
and the only thing that surfaced it before was `packs/simplex/scripts/simplex-address.py`
|
|
run once at provision time. Print it on connect, from the operator front end
|
|
(the CLI), through the daemon the gateway already owns — show-or-create, no
|
|
second connection, no new transport surface. The lookup is best-effort: a
|
|
failure is reported once (to stderr) and retried on the next connect, and it
|
|
never takes the socket down.
|
|
"""
|
|
printed = False
|
|
warned = False
|
|
|
|
async def _announce() -> None:
|
|
nonlocal printed, warned
|
|
if printed:
|
|
return
|
|
try:
|
|
link = await adapter.daemon.address()
|
|
except Exception as exc:
|
|
if not warned:
|
|
warned = True
|
|
print(
|
|
f"gateway simplex: could not read contact link: {exc}",
|
|
file=sys.stderr,
|
|
)
|
|
return
|
|
short = (link or {}).get("short_link") or ""
|
|
full = (link or {}).get("full_link") or ""
|
|
if not (short or full):
|
|
if not warned:
|
|
warned = True
|
|
print("gateway simplex: no contact link available", file=sys.stderr)
|
|
return
|
|
printed = True
|
|
print(f"gateway simplex contact link: {short}")
|
|
if full:
|
|
print(full)
|
|
|
|
return _announce
|
|
|
|
|
|
def _lxmf_address_callback(adapter):
|
|
"""Return a sync `on_connected` hook that prints the bot's LXMF address once.
|
|
|
|
Unlike SimpleX there is no daemon round-trip: the router knows its delivery
|
|
address as soon as it is registered, so the hook just prints it once.
|
|
"""
|
|
printed = False
|
|
|
|
def _announce() -> None:
|
|
nonlocal printed
|
|
if printed:
|
|
return
|
|
address = getattr(adapter.daemon, "address", "") or ""
|
|
if not address:
|
|
print("gateway lxmf: no address available", file=sys.stderr)
|
|
return
|
|
printed = True
|
|
print(f"gateway lxmf address: {address}")
|
|
|
|
return _announce
|
|
|
|
|
|
def _attach_address_announcer(adapter) -> None:
|
|
"""Wire the platform's startup address announcement onto its daemon.
|
|
|
|
A human cannot reach a gateway without its address. Print it once on
|
|
connect, from the operator front end (the CLI), through the transport the
|
|
adapter already owns — no second connection, no new transport surface.
|
|
"""
|
|
if adapter.name == "simplex":
|
|
adapter.daemon.on_connected = _gateway_address_callback(adapter)
|
|
elif adapter.name == "lxmf":
|
|
adapter.daemon.on_connected = _lxmf_address_callback(adapter)
|
|
|
|
|
|
def _build_gateway_adapter(platform: str, cfg: dict, trace):
|
|
"""Construct one adapter from its config, or None for an unknown platform."""
|
|
if platform == "simplex":
|
|
from .gateway.simplex import SimplexAdapter
|
|
|
|
return SimplexAdapter(cfg, trace=trace)
|
|
if platform == "lxmf":
|
|
from .gateway.lxmf import LxmfAdapter
|
|
|
|
cfg = dict(cfg or {})
|
|
# Contain Reticulum's config + identity under the checkout's .runtime/.
|
|
cfg.setdefault("config_dir", str(REPO_ROOT / ".runtime" / "lxmf" / "reticulum"))
|
|
cfg.setdefault("storage_path", str(REPO_ROOT / ".runtime" / "lxmf" / "router"))
|
|
# A voice reply's Piper voice lives with the voice gateway's, under the
|
|
# checkout's .runtime/ (bootstrap downloads it there).
|
|
voice_cfg = dict(cfg.get("voice") or {})
|
|
tts_cfg = dict(voice_cfg.get("tts") or {})
|
|
if not tts_cfg.get("voice_dir"):
|
|
tts_cfg["voice_dir"] = str(REPO_ROOT / ".runtime" / "voice" / "tts")
|
|
voice_cfg["tts"] = tts_cfg
|
|
cfg["voice"] = voice_cfg
|
|
return LxmfAdapter(cfg, trace=trace)
|
|
if platform == "voice":
|
|
from .gateway.voice import VoiceAdapter
|
|
|
|
cfg = dict(cfg or {})
|
|
# Contain downloaded wake-word / TTS models under the checkout's
|
|
# .runtime/ (the faster-whisper model lands in the HF cache instead).
|
|
voice_dir = REPO_ROOT / ".runtime" / "voice"
|
|
wake_cfg = dict(cfg.get("wake") or {})
|
|
if not wake_cfg.get("model_dir"):
|
|
wake_cfg["model_dir"] = str(voice_dir / "wake")
|
|
cfg["wake"] = wake_cfg
|
|
tts_cfg = dict(cfg.get("tts") or {})
|
|
if not tts_cfg.get("voice_dir"):
|
|
tts_cfg["voice_dir"] = str(voice_dir / "tts")
|
|
cfg["tts"] = tts_cfg
|
|
return VoiceAdapter(cfg, trace=trace)
|
|
return None
|
|
|
|
|
|
def _resolve_platforms(requested: str, gateway_cfg: dict) -> list[str]:
|
|
"""Turn `--platform` into a list of platform names.
|
|
|
|
`all`/`*` means every **enabled** gateway block in config; a
|
|
comma-separated list is taken as-is (unknown names are kept so the caller
|
|
can report them). Non-platform keys (`_comment`, the global `humanize`
|
|
defaults block) are skipped.
|
|
"""
|
|
requested = (requested or "simplex").strip()
|
|
if requested in ("all", "*"):
|
|
return [
|
|
name
|
|
for name, block in (gateway_cfg or {}).items()
|
|
if isinstance(block, dict)
|
|
and not name.startswith("_")
|
|
and name != "humanize"
|
|
and block.get("enabled", False)
|
|
]
|
|
return [name.strip() for name in requested.split(",") if name.strip()]
|
|
|
|
|
|
def run_gateway(
|
|
scheduler: Scheduler,
|
|
config: dict,
|
|
platform: str = "simplex",
|
|
serve_dashboard: bool = False,
|
|
) -> int:
|
|
"""Run one or more messenger gateways in the foreground.
|
|
|
|
All enabled adapters share a single scheduler/process (see AGENTS.md: never
|
|
run two scheduler processes over one skill store), so each gets its own
|
|
transport thread but they route through one `GatewayService`.
|
|
"""
|
|
# Dump every thread's Python stack on a fatal signal (SIGSEGV/SIGABRT/...):
|
|
# a wedged front end (e.g. a voice loop stuck on the mic) leaves a trace in
|
|
# the journal instead of dying silently. Also enabled by the unit's
|
|
# PYTHONFAULTHANDLER=1; harmless when set twice.
|
|
faulthandler.enable()
|
|
|
|
gateway_cfg = config.get("gateway", {}) or {}
|
|
platforms = _resolve_platforms(platform, gateway_cfg)
|
|
|
|
adapters: dict = {}
|
|
for name in platforms:
|
|
cfg = gateway_cfg.get(name) or {}
|
|
if not isinstance(cfg, dict) or not cfg.get("enabled", False):
|
|
print(f"gateway.{name} is not enabled in config")
|
|
continue
|
|
adapter = _build_gateway_adapter(name, cfg, scheduler.trace)
|
|
if adapter is None:
|
|
print(f"unknown gateway platform {name!r}")
|
|
continue
|
|
ok, hint = adapter.check_requirements()
|
|
if not ok:
|
|
print(f"gateway {name} unavailable: {hint}")
|
|
continue
|
|
_attach_address_announcer(adapter)
|
|
adapters[name] = adapter
|
|
|
|
if not adapters:
|
|
print("no gateway platforms enabled/available")
|
|
return 1
|
|
|
|
# Reticulum installs process signal handlers, which only works on the main
|
|
# thread. Run the LXMF transport's main-thread setup (Reticulum + router)
|
|
# here before the per-adapter transport threads start; `run()` reuses it.
|
|
for name in list(adapters):
|
|
prepare = getattr(adapters[name].daemon, "prepare", None)
|
|
if callable(prepare):
|
|
try:
|
|
prepare()
|
|
except Exception as exc:
|
|
print(f"gateway {name} unavailable: {exc}", file=sys.stderr)
|
|
del adapters[name]
|
|
|
|
if not adapters:
|
|
print("no gateway platforms enabled/available")
|
|
return 1
|
|
|
|
from .gateway.service import GatewayService
|
|
|
|
scheduler.defer_questions = True
|
|
service = GatewayService(scheduler, adapters, config=gateway_cfg)
|
|
scheduler.on_request_requeued = service.on_request_requeued
|
|
service.start()
|
|
|
|
if serve_dashboard:
|
|
from .dashboard import serve
|
|
|
|
dash_cfg = config.get("dashboard", {}) or {}
|
|
threading.Thread(
|
|
target=serve,
|
|
args=(scheduler,),
|
|
kwargs={
|
|
"port": int(dash_cfg.get("port", 8765)),
|
|
"host": str(dash_cfg.get("host", "127.0.0.1")),
|
|
},
|
|
daemon=True,
|
|
).start()
|
|
|
|
threads: list[threading.Thread] = []
|
|
for name, adapter in adapters.items():
|
|
|
|
def _run(adapter=adapter, name=name) -> None:
|
|
try:
|
|
adapter.run(
|
|
lambda msg, _name=name: service.handle_inbound(
|
|
msg, platform=_name
|
|
),
|
|
service.outbound_for(name),
|
|
)
|
|
except Exception as exc: # a dead transport must not kill the rest
|
|
print(f"gateway {name} stopped: {exc}", file=sys.stderr)
|
|
|
|
thread = threading.Thread(target=_run, name=f"gateway-{name}", daemon=True)
|
|
thread.start()
|
|
threads.append(thread)
|
|
endpoint = getattr(adapter, "ws_url", "") or getattr(adapter, "config_dir", "")
|
|
print(f"gateway {name} listening ({endpoint}) (Ctrl-C to stop)")
|
|
|
|
try:
|
|
while any(thread.is_alive() for thread in threads):
|
|
time.sleep(0.5)
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
service.stop()
|
|
for adapter in adapters.values():
|
|
try:
|
|
adapter.close()
|
|
except Exception:
|
|
pass
|
|
for thread in threads:
|
|
thread.join(timeout=2.0)
|
|
return 0
|
|
|
|
|
|
def run_bridge(scheduler: Scheduler, config: dict, name: str | None = None) -> int:
|
|
"""Run standalone third-party API bridges (SimpleX first).
|
|
|
|
Bridges are their own processes so generated skills never touch a system's
|
|
native protocol and the command gateway never grows read/send surface. See
|
|
`semif_agent.bridges`.
|
|
"""
|
|
from .bridges.registry import run_bridges
|
|
|
|
names = [name] if name else None
|
|
return run_bridges(
|
|
config, names=names, trace=scheduler.trace, llm=scheduler.llm
|
|
)
|
|
|
|
|
|
def _anchor_pack_roots(raw: dict) -> list[str]:
|
|
"""The pack roots from a raw (un-anchored) config, resolved against REPO_ROOT."""
|
|
roots = raw.get("skill_packs") or "packs"
|
|
if isinstance(roots, (str, Path)):
|
|
roots = [roots]
|
|
anchored = []
|
|
for root in roots:
|
|
path = Path(str(root))
|
|
anchored.append(str(path if path.is_absolute() else REPO_ROOT / path))
|
|
return anchored
|
|
|
|
|
|
def check_config(path: str = "config.json", write_example: str | None = None) -> int:
|
|
"""Validate the split config against the core + pack schemas.
|
|
|
|
With `write_example`, regenerate the committed `config.example.json` from
|
|
the schemas (the pack manifests + the core schema) instead of validating.
|
|
Validation reports type mismatches, unknown keys, and any `!secret`
|
|
reference missing from the secrets file.
|
|
"""
|
|
config_path = Path(path)
|
|
try:
|
|
raw = load_config_file(config_path)
|
|
except ConfigError as exc:
|
|
print(exc, file=sys.stderr)
|
|
return 1
|
|
packs = load_packs({**raw, "skill_packs": _anchor_pack_roots(raw)})
|
|
if write_example is not None:
|
|
example = generate_example_config(packs)
|
|
Path(write_example).write_text(json.dumps(example, indent=2) + "\n")
|
|
print(f"wrote {write_example} ({len(packs)} packs)")
|
|
return 0
|
|
secrets_path = raw.get("secrets_file") or DEFAULT_SECRETS_FILE
|
|
secrets_path = Path(secrets_path)
|
|
if not secrets_path.is_absolute():
|
|
secrets_path = config_path.parent / secrets_path
|
|
secrets = load_secrets(secrets_path)
|
|
problems = validate_config(raw, packs)
|
|
problems += [
|
|
f"secret {key!r} referenced but missing from {secrets_path}"
|
|
for key in missing_secret_refs(raw, secrets)
|
|
]
|
|
errors = [p for p in problems if not p.startswith("warning: ")]
|
|
for problem in problems:
|
|
print(f"config: {problem}", file=sys.stderr)
|
|
if errors:
|
|
return 1
|
|
print(f"config ok: {len(packs)} packs validated")
|
|
return 0
|
|
|
|
|
|
def config_init(path: str = "config.json") -> int:
|
|
"""Seed a fresh split config + empty secrets file. Never overwrites.
|
|
|
|
Writes ``config/core.json``, ``config/gateway.json``, ``config/bridges.json``,
|
|
``config/packs/*.json`` and the ``config.json`` index from the schemas, plus
|
|
an empty ``secrets.json``. A human then edits the fragments and fills in the
|
|
secrets.
|
|
"""
|
|
config_path = Path(path)
|
|
if config_path.exists():
|
|
print(f"{config_path} already exists; leaving it alone", file=sys.stderr)
|
|
return 1
|
|
packs = load_packs({"skill_packs": _anchor_pack_roots({})})
|
|
example = generate_example_config(packs)
|
|
written = write_split_config(config_path.parent, example, packs)
|
|
secrets_path = config_path.parent / DEFAULT_SECRETS_FILE
|
|
if not secrets_path.exists():
|
|
secrets_path.write_text("{}\n")
|
|
print(
|
|
f"seeded {len(written)} config files under {config_path.parent or '.'}/ "
|
|
f"and {secrets_path.name}; edit them, then run `check-config`"
|
|
)
|
|
return 0
|
|
|
|
|
|
def _setup_command(args) -> int:
|
|
"""Run the first-run interview (or emit the install plan) — see `setup.py`."""
|
|
config_path = Path(args.path)
|
|
if not config_path.is_absolute():
|
|
config_path = REPO_ROOT / config_path
|
|
packs = load_packs({"skill_packs": _anchor_pack_roots({})})
|
|
return run_bootstrap_setup(
|
|
root=config_path.parent,
|
|
path=config_path,
|
|
packs=packs,
|
|
interview=args.interview,
|
|
yes=args.yes,
|
|
answers_file=args.answers,
|
|
plan_only=args.plan_only,
|
|
plan_out=args.plan_out,
|
|
force=args.force,
|
|
answers_out=args.answers_out,
|
|
pack_index=args.pack_index,
|
|
)
|
|
|
|
|
|
def _load_config_tolerant(path: str) -> dict:
|
|
"""Load the config, or ``{}`` when it is missing/invalid.
|
|
|
|
Used for parser defaults (dashboard bind) before the subcommand is known.
|
|
Runtime commands still require a real config: `main` re-loads it fatally
|
|
after the config-independent subcommands return.
|
|
"""
|
|
try:
|
|
return load_config(path)
|
|
except SystemExit:
|
|
return {}
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
argv = list(argv) if argv is not None else list(sys.argv[1:])
|
|
config_path = _extract_config_path(argv)
|
|
defaults = _load_config_tolerant(config_path)
|
|
parser = argparse.ArgumentParser(prog="semif-agent")
|
|
parser.add_argument("--config", default="config.json")
|
|
sub = parser.add_subparsers(dest="command")
|
|
|
|
run_p = sub.add_parser("run", help="interactive REPL or scripted input")
|
|
run_p.add_argument("--script", default=None, help="JSONL file of {\"text\": ...} rows")
|
|
|
|
sub.add_parser("dream", help="compute the prediction-observation cost report")
|
|
sub.add_parser("skills", help="list the skill tree")
|
|
sub.add_parser("status", help="show current process and queue")
|
|
|
|
cc_p = sub.add_parser(
|
|
"check-config", help="validate config/ against the core + pack schemas"
|
|
)
|
|
cc_p.add_argument(
|
|
"--write-example",
|
|
metavar="PATH",
|
|
default=None,
|
|
help="write the generated config.example.json to PATH and exit",
|
|
)
|
|
|
|
ci_p = sub.add_parser(
|
|
"config-init", help="seed a fresh split config/ + secrets.json (never overwrites)"
|
|
)
|
|
ci_p.add_argument("--path", default="config.json", help="config index path to seed")
|
|
|
|
setup_p = sub.add_parser(
|
|
"setup",
|
|
help="first-run interview: generate config/ from your answers",
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog=(
|
|
"answers JSON fields (all optional; --answers FILE):\n"
|
|
" engine_provider (semif|winnow|ollama), winnow_base_url, winnow_model,\n"
|
|
" winnow_api_key, ollama_base_url, ollama_model, ollama_api_key\n"
|
|
" new_skill_creation (deny|ask|allow)\n"
|
|
" llm_provider (semif|ollama|opencode|openai), llm_base_url,\n"
|
|
" llm_model, llm_api_key\n"
|
|
" codegen_provider, codegen_base_url, codegen_model, codegen_api_key\n"
|
|
" packs (list of pack names; the offered set comes from --pack-index)\n"
|
|
" gateways (list of: simplex, lxmf, voice), timezone\n"
|
|
" nextcloud_url, nextcloud_username, nextcloud_app_password,\n"
|
|
" outlook_client_id"
|
|
),
|
|
)
|
|
setup_p.add_argument("--path", default="config.json", help="config index path to seed")
|
|
setup_p.add_argument(
|
|
"--interview", action="store_true", help="ask the setup questions interactively"
|
|
)
|
|
setup_p.add_argument(
|
|
"--yes",
|
|
action="store_true",
|
|
help="non-interactive: take the minimum working defaults",
|
|
)
|
|
setup_p.add_argument(
|
|
"--answers", default=None, metavar="FILE", help="JSON answers file (non-interactive)"
|
|
)
|
|
setup_p.add_argument(
|
|
"--answers-out",
|
|
default=None,
|
|
metavar="FILE",
|
|
help="collect the answers and write them as JSON to FILE, then exit "
|
|
"(no config; bootstrap's pre-clone pass)",
|
|
)
|
|
setup_p.add_argument(
|
|
"--pack-index",
|
|
default=None,
|
|
metavar="FILE",
|
|
help="pack-index JSON whose entries the interview offers "
|
|
"(semif-packs; see scripts/bootstrap.sh)",
|
|
)
|
|
setup_p.add_argument(
|
|
"--plan-out",
|
|
default=None,
|
|
metavar="PATH",
|
|
help="write the install plan JSON to PATH (for scripts/bootstrap.sh)",
|
|
)
|
|
setup_p.add_argument(
|
|
"--plan-only",
|
|
action="store_true",
|
|
help="read the existing config and emit the plan; write nothing",
|
|
)
|
|
setup_p.add_argument(
|
|
"--force", action="store_true", help="overwrite an existing config"
|
|
)
|
|
|
|
relabel_p = sub.add_parser("relabel", help="human override of a decision label")
|
|
relabel_p.add_argument("id")
|
|
relabel_p.add_argument("outcome")
|
|
|
|
dash_p = sub.add_parser("dashboard", help="run the local browser dashboard")
|
|
dash_p.add_argument(
|
|
"--port",
|
|
type=int,
|
|
default=int(defaults.get("dashboard", {}).get("port", 8765)),
|
|
)
|
|
dash_p.add_argument(
|
|
"--host",
|
|
default=str(defaults.get("dashboard", {}).get("host", "127.0.0.1")),
|
|
help="bind address (0.0.0.0 to expose on the LAN)",
|
|
)
|
|
dash_p.add_argument(
|
|
"--replay",
|
|
action="store_true",
|
|
help="replay mode: do not warm the decision engine",
|
|
)
|
|
|
|
gw_p = sub.add_parser("gateway", help="run the messenger gateway (SimpleX, LXMF)")
|
|
gw_p.add_argument(
|
|
"--platform",
|
|
default="simplex",
|
|
help="gateway platform(s): simplex, lxmf, a comma-separated list, or all (default simplex)",
|
|
)
|
|
gw_p.add_argument(
|
|
"--dashboard",
|
|
action="store_true",
|
|
help="also serve the browser dashboard from the same process",
|
|
)
|
|
|
|
bridge_p = sub.add_parser(
|
|
"bridge", help="run standalone third-party API bridges (SimpleX first)"
|
|
)
|
|
bridge_p.add_argument(
|
|
"--name",
|
|
default=None,
|
|
help="bridge service to run (default: all enabled)",
|
|
)
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
if args.command == "check-config":
|
|
return check_config(config_path, write_example=args.write_example)
|
|
if args.command == "config-init":
|
|
return config_init(args.path)
|
|
if args.command == "setup":
|
|
return _setup_command(args)
|
|
|
|
# Runtime commands need a real config; this is fatal when it is missing.
|
|
config = defaults or load_config(config_path)
|
|
scheduler, config = build_scheduler(config)
|
|
|
|
if args.command == "run":
|
|
if args.script:
|
|
return scripted(scheduler, args.script)
|
|
return repl(scheduler, config)
|
|
elif args.command == "dream":
|
|
print(run_dream(scheduler.log).render())
|
|
elif args.command == "skills":
|
|
print(tree_summary(scheduler.tree))
|
|
elif args.command == "status":
|
|
print(scheduler.status())
|
|
elif args.command == "relabel":
|
|
ok = scheduler.log.relabel(args.id, args.outcome)
|
|
print("relabeled." if ok else f"no row with id {args.id}")
|
|
elif args.command == "dashboard":
|
|
from .dashboard import serve
|
|
|
|
scheduler.defer_questions = True
|
|
if args.replay:
|
|
print("replay mode: reading decision log + trace; engine not warmed.")
|
|
serve(scheduler, port=args.port, host=args.host)
|
|
elif args.command == "gateway":
|
|
return run_gateway(
|
|
scheduler,
|
|
config,
|
|
platform=args.platform,
|
|
serve_dashboard=args.dashboard,
|
|
)
|
|
elif args.command == "bridge":
|
|
return run_bridge(scheduler, config, name=args.name)
|
|
else:
|
|
parser.print_help()
|
|
return 0
|
|
|
|
|
|
def _extract_config_path(argv: list[str]) -> str:
|
|
"""Pull --config out of argv before the full parser runs, so the dashboard
|
|
subcommand can read dashboard.port from config for its default."""
|
|
for index, arg in enumerate(argv):
|
|
if arg == "--config" and index + 1 < len(argv):
|
|
return argv[index + 1]
|
|
if arg.startswith("--config="):
|
|
return arg.split("=", 1)[1]
|
|
return "config.json"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |