packs: add PACKGEN.md contract + packgen skill
PACKGEN.md is the authoritative spec for a new pack, the sibling of CODEGEN.md (body) and TESTGEN.md (test): the pack.json manifest, its config schema and how it maps onto bridges.<name>/packs.<name>, the enable switch, referencing a bridge by name, authoring a new bridge (BridgeService + CATALOG + build_bridge + the optional-dep 503 rule), acceptance criteria, a worked example, and a checklist. Add a thin OpenCode skill (.opencode/skills/packgen/SKILL.md) that walks the workflow and points at PACKGEN.md, and cross-link the contract from AGENTS.md, README.md, CODEGEN.md, and TESTGEN.md.
This commit is contained in:
1 parent
711dba2f03
commit
f379c0a746
6 files changed
+449
-1
No files matched your search
+379
@@ -0,0 +1,379 @@
|
||||
# PACKGEN.md — the contract for new packs
|
||||
|
||||
This file is the authoritative spec for what a **pack** is and how to author a
|
||||
new one. It is the third sibling of the skill contracts: `CODEGEN.md` governs a
|
||||
skill's runnable **body**, `TESTGEN.md` governs its **test**, and this file
|
||||
governs the **pack** that bundles a concern, its skills, its bridge reference,
|
||||
and its settings into one thing.
|
||||
|
||||
Read this before adding anything under `packs/`. A pack is what makes a new
|
||||
product/concern a first-class part of the agent instead of scattered edits to
|
||||
`skills.py` and `config.py`.
|
||||
|
||||
## What a pack is
|
||||
|
||||
Semif-agent ships a set of starter integrations. Each is a **pack**: a folder
|
||||
`packs/<name>/` holding a `pack.json` manifest plus a `skills/` store of leaves
|
||||
in the exact generated-skill format. The manifest binds the pieces that would
|
||||
otherwise be scattered by convention:
|
||||
|
||||
- the **concern** (what product/job this is) and its **title**;
|
||||
- its **categories** (the tree buckets it owns, each with a scope description);
|
||||
- the **bridge(s)** it talks to, referenced by name (bridge *code* stays in
|
||||
`semif_agent/bridges` — see "Authoring a new bridge");
|
||||
- its **runtime requirements** (optional heavy deps);
|
||||
- its **enable flag**; and
|
||||
- its **config schema** (the defaults and validation for its settings).
|
||||
|
||||
A pack is loaded by `semif_agent/packs.py` (`PackRegistry` discovers packs,
|
||||
`merge_packs` loads every **enabled** pack into the tree at startup) and its
|
||||
`config` schema is read by `semif_agent/config.py` to generate
|
||||
`config.example.json` and to validate a live config.
|
||||
|
||||
Three boundaries must stay crisp:
|
||||
|
||||
- A **skill** is one leaf action (`packs/<name>/skills/<category>/<leaf>/`),
|
||||
governed by `CODEGEN.md`/`TESTGEN.md`. A pack contains skills; it is not one.
|
||||
- A **bridge** is a standalone process in front of a third-party system
|
||||
(`semif_agent/bridges/`). A pack *references* a bridge by name; it never
|
||||
contains bridge code.
|
||||
- A pack is **declarative structure** and is committed. Per-machine values are
|
||||
the split config; credentials live in `secrets.json`. A committed pack never
|
||||
holds a secret.
|
||||
|
||||
## Folder layout
|
||||
|
||||
```
|
||||
packs/<name>/
|
||||
pack.json # the manifest (this file's subject)
|
||||
skills/<category>/<leaf>/
|
||||
skill.py # the body — CODEGEN.md
|
||||
manifest.json # {"description": "..."} navigation text
|
||||
contract.json # mirror of the body's CONTRACT — TESTGEN.md
|
||||
skill.test.py # hermetic mechanics test — TESTGEN.md
|
||||
```
|
||||
|
||||
- The `<category>` directory name is the tree bucket; the `<leaf>` directory
|
||||
name is the leaf's `name`. The runner joins them as `category.name` only in
|
||||
run summaries.
|
||||
- Every leaf carries the four artifacts above. Their contents are governed by
|
||||
`CODEGEN.md` (body + `CONTRACT` + `INTEGRATION`) and `TESTGEN.md`
|
||||
(test + `contract.json` mirror); do not restate those rules here.
|
||||
- `manifest.json` holds a single non-empty `description` — the navigation text
|
||||
SemIf routes on. It is the seed analog of the registry entry a generated
|
||||
skill gets from the `llm` provider.
|
||||
- A root with no `pack.json` is treated as **one implicit, manifest-less pack**
|
||||
whose store is the root itself (a plain skills directory still works), but a
|
||||
committed pack should always have a manifest.
|
||||
|
||||
The committed `packs/` are the authoritative worked examples: `essentials` is
|
||||
the minimal bridge-less pack; `nextcloud`, `simplex`, `outlook`, and `browser`
|
||||
are bridge-backed packs.
|
||||
|
||||
## `pack.json` — the manifest
|
||||
|
||||
A single JSON object. Fields (from `packs._parse_pack`):
|
||||
|
||||
| Field | Type | Required | Meaning |
|
||||
| -------------------- | --------------- | -------- | ------- |
|
||||
| `name` | string | no\* | Pack id (snake_case). Defaults to the folder name. |
|
||||
| `title` | string | no | Human display name. Defaults to `name`. |
|
||||
| `concern` | string | **yes** | One- to two-sentence statement of the product/job the pack serves. (`description` is accepted as a legacy alias.) |
|
||||
| `categories` | object | **yes** | `{category_id: scope_description}` — the tree buckets this pack owns. |
|
||||
| `bridges` | array of string | no | Bridge names this pack talks to, referencing `bridges/registry.py` `CATALOG`. Empty for a bridge-less pack. |
|
||||
| `requires` | array of string | no | Optional heavy deps (e.g. `playwright`). Informational; the code's `check_requirements` is what enforces them. |
|
||||
| `enabled_by_default` | bool | no | Default `true`. Whether the pack loads when nothing else decides. |
|
||||
| `config` | object | no | The pack's config schema (see below). |
|
||||
|
||||
\* `name` is optional in the parser but should always be set explicitly.
|
||||
|
||||
Rules:
|
||||
|
||||
- **`concern` and `categories` are required** (`tests/test_packs.py` fails
|
||||
without them). A pack with no declared concern is not a pack.
|
||||
- **Category ids are the tree keys.** They are the same ids the leaf folders
|
||||
live under (`skills/<category>/<leaf>/`). When `categories` is omitted, the
|
||||
pack's categories are inferred from the `skills/` subdirectories — but declare
|
||||
them, because the description is load-bearing.
|
||||
- **A category description is navigation-critical.** It is what SemIf reads to
|
||||
route a request into the bucket; a bare id with no description routes badly.
|
||||
Write one to two sentences naming the concrete jobs the category does.
|
||||
- **`bridges[0]` owns the config block.** For a bridge pack, the pack's `config`
|
||||
schema maps onto `bridges.<bridges[0]>`; only the first bridge gets the block
|
||||
(a pack normally fronts one bridge).
|
||||
- Keep the manifest minimal: it binds metadata and settings, nothing executable.
|
||||
|
||||
## The config schema
|
||||
|
||||
`config` is a nested schema. A **leaf** is a dict with a `default`; a **block**
|
||||
is a dict without one (recurse). Each leaf spec:
|
||||
|
||||
| Key | Type | Meaning |
|
||||
| --------- | ------ | ------- |
|
||||
| `type` | string | `bool` \| `int` \| `float` \| `str` \| `list` \| `dict`. Checked by `check-config` (`int`/`float` are interchangeable). |
|
||||
| `default` | any | The value shipped in `config.example.json` and seeded by `config-init`. Required on a leaf. |
|
||||
| `doc` | string | One-line plain-language description. Shown in the generated example. |
|
||||
| `secret` | bool | `true` marks a credential: the example emits `"!secret <pack>_<key>"` instead of a value. |
|
||||
|
||||
Example (a bridge-backed pack, mirroring `packs/simplex/pack.json`):
|
||||
|
||||
```json
|
||||
"config": {
|
||||
"enabled": {"type": "bool", "default": true, "doc": "Load this pack's skills and run its bridge."},
|
||||
"host": {"type": "str", "default": "127.0.0.1", "doc": "Bridge bind address (localhost only)."},
|
||||
"port": {"type": "int", "default": 5227, "doc": "Bridge listen port."},
|
||||
"token": {"type": "str", "default": "", "secret": true, "doc": "Shared secret required in the X-Semif-Token header when set."},
|
||||
"reconnect": {
|
||||
"initial": {"type": "float", "default": 1.0, "doc": "Initial reconnect delay (seconds)."},
|
||||
"max": {"type": "float", "default": 60.0, "doc": "Maximum reconnect delay (seconds)."}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
How the schema maps onto the live config (`config.default_config` /
|
||||
`generate_example_config`):
|
||||
|
||||
- A **bridge pack** (`bridges` non-empty) maps its whole `config` schema onto
|
||||
the `bridges.<bridges[0]>` block.
|
||||
- A **bridge-less pack** maps only its `enabled` flag onto
|
||||
`packs.<name>.enabled`; its other keys are not carried (a bridge-less pack is
|
||||
configured through its skills' own config, not a pack block).
|
||||
- Secret leaves become `"!secret <pack>_<key>"` in `config.example.json`, where
|
||||
`<pack>_` is the pack name prefix (nested keys are prefixed with their path,
|
||||
e.g. `simplex_reconnect_initial`). Never write a real credential into the
|
||||
manifest.
|
||||
|
||||
Rules:
|
||||
|
||||
- Every leaf needs a `default` and a `doc`. A key with no `default` is treated
|
||||
as a nested block.
|
||||
- Put **operational** settings here (endpoints, accounts, defaults, timeouts) —
|
||||
the values a skill body reads from `ctx.config`. Per-request arguments never
|
||||
belong in config (see `CODEGEN.md`).
|
||||
- `config.json`, `config/`, and `secrets.json` are gitignored and per-machine.
|
||||
The manifest is committed structure only.
|
||||
|
||||
After changing a schema, regenerate and validate:
|
||||
|
||||
```sh
|
||||
python -m semif_agent.cli check-config --write-example config.example.json
|
||||
python -m semif_agent.cli check-config
|
||||
```
|
||||
|
||||
`config-init` seeds the split `config/` layout from the schemas (bootstrap runs
|
||||
it once, only when `config.json` is absent); afterwards only a human edits it.
|
||||
The generated example and the live config are never hand-edited.
|
||||
|
||||
## The enable switch
|
||||
|
||||
One switch governs the pack's skills **and** its bridge, so a host never offers
|
||||
a skill whose bridge is off. `packs.pack_enabled` resolves in order:
|
||||
|
||||
1. an explicit `packs.<name>.enabled` in config (wins);
|
||||
2. else, for a bridge pack, `bridges.<bridges[0]>.enabled` (the historical
|
||||
switch);
|
||||
3. else `enabled_by_default`.
|
||||
|
||||
Disabling a pack drops its categories entirely — including any generated body
|
||||
authored into one — and stops its bridge. The switch lives in the pack's
|
||||
`config/packs/<name>.json` fragment (written by `config-init`/`check-config`).
|
||||
|
||||
## Referencing a bridge by name
|
||||
|
||||
A pack whose skills talk to a third-party system lists that bridge in
|
||||
`bridges` and carries its config block in `config`. The bridge itself is a
|
||||
class in `semif_agent/bridges/` registered in `bridges/registry.py` `CATALOG`.
|
||||
Its `BridgeInfo` (name, service, description, URL config var, other config
|
||||
vars, auth header, endpoints) is injected into every codegen prompt via
|
||||
`describe_bridges()`, so bodies learn how to reach it without the pack
|
||||
restating protocol details.
|
||||
|
||||
Skill-facing config is **derived**, never duplicated: `derived_config_vars`
|
||||
turns the bridge's `bridges.<name>` block (`host`/`port`/`token`) into the
|
||||
`<url_config_var>` and `<auth_config_var>` values a body reads from
|
||||
`ctx.config` (e.g. `simplex_bridge_url`, `simplex_bridge_token`). Bodies call
|
||||
the bridge over the ordinary `http` transport; their hermetic tests are
|
||||
loopback HTTP like any other HTTP body.
|
||||
|
||||
Rules:
|
||||
|
||||
- The bridge block's `enabled`/`host`/`port`/`token` are the pack's config
|
||||
schema; the URL/token config vars are derived and must not be declared again.
|
||||
- A pack references a bridge; it never imports or edits bridge code. Gateway
|
||||
isolation is non-negotiable — read/send/address capability is a bridge, never
|
||||
a gateway feature.
|
||||
|
||||
## Authoring a new bridge
|
||||
|
||||
A **bridge** is a standalone process that stands up a simple, secure localhost
|
||||
HTTP layer in front of one third-party system, so a skill body never speaks
|
||||
that system's native protocol. Adding a service means:
|
||||
|
||||
1. **Write the bridge class** in `semif_agent/bridges/<name>.py`, subclassing
|
||||
`BridgeService` (`semif_agent/bridges/base.py`). Set the `name` attribute and
|
||||
a module-level/class `INFO = BridgeInfo(...)`:
|
||||
|
||||
```python
|
||||
from .base import BridgeInfo, BridgeService
|
||||
|
||||
class WidgetBridge(BridgeService):
|
||||
name = "widget"
|
||||
INFO = BridgeInfo(
|
||||
name="widget",
|
||||
service="widget_service",
|
||||
description="The user's Widget account: widgets and their status.",
|
||||
url_config_var="widget_bridge_url",
|
||||
endpoints=(
|
||||
"GET /widgets — list widgets",
|
||||
"POST /widgets — create a widget",
|
||||
),
|
||||
config_vars=("widget_bridge_token",),
|
||||
config_var_docs=(("widget_bridge_token", "Shared secret for the X-Semif-Token header."),),
|
||||
auth_header="X-Semif-Token",
|
||||
auth_config_var="widget_bridge_token",
|
||||
)
|
||||
|
||||
def check_requirements(self) -> tuple[bool, str | None]:
|
||||
return True, None # stdlib-only
|
||||
|
||||
def handle_get(self, path, query): ...
|
||||
def handle_post(self, path, payload): ...
|
||||
```
|
||||
|
||||
The base class owns the HTTP/JSON/auth plumbing (`start`/`stop`, the
|
||||
`X-Semif-Token` guard); a subclass implements only its routes. Override
|
||||
`start`/`stop`/`_stop_transport` only if the bridge owns a service daemon.
|
||||
|
||||
2. **Register it** in `bridges/registry.py` `CATALOG` (tuple order is
|
||||
presentation order). That is what makes it discoverable, describable to
|
||||
codegen, and runnable via `python -m semif_agent.cli bridge`.
|
||||
|
||||
3. **Give it a pack** whose `bridges` lists its name and whose `config` schema
|
||||
carries its block (`enabled`/`host`/`port`/`token` plus service vars). The
|
||||
schema is what gives the bridge its config defaults and validation.
|
||||
|
||||
4. **`build_bridge` special-casing** is needed only when the bridge requires the
|
||||
scheduler's configured `llm` client (pass `client=`) or top-level fallback
|
||||
values (like Nextcloud's `nextcloud_*`). A plain bridge needs no branch.
|
||||
|
||||
5. **Heavy/optional deps:** `check_requirements()` must **never raise** and must
|
||||
return `(ok, install_hint)`. `run_bridges` aborts **all** bridges when one
|
||||
reports `ok=False`, so a bridge with an optional dependency (like the
|
||||
browser) must return `(True, None)` and surface a per-call `503` with an
|
||||
install hint instead — a missing browser must not take down simplex/llm/
|
||||
nextcloud. Lazy-import every heavy dep so the core stays stdlib-only.
|
||||
|
||||
6. **Report real failures:** a transport/HTTP/bad-reply failure is a `502`, a
|
||||
missing connection/dep is a `503`, bad input a `400` — never a fabricated
|
||||
success. The bridge owns its own state (cursors, daemons); bodies keep none.
|
||||
|
||||
7. **Test it hermetically:** add `tests/test_<name>_bridge.py` driving the real
|
||||
routes over a loopback `http.server` (inject a fake driver where a real
|
||||
service would be needed). No external network, no real daemon. `describe_
|
||||
bridges()` picks the new bridge up automatically.
|
||||
|
||||
8. **Regenerate** `config.example.json` and run `check-config` (see above), and
|
||||
document the bridge in `AGENTS.md` (bridges section). Never widen the
|
||||
gateway for a new capability.
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
A new pack is accepted only if:
|
||||
|
||||
1. `packs/<name>/pack.json` parses as JSON, declares a non-empty `concern` and
|
||||
a non-empty `categories` map, and every `bridges` name exists in
|
||||
`bridges/registry.py` `CATALOG`.
|
||||
2. Every leaf under `skills/<category>/<leaf>/` is a complete skill folder
|
||||
(`skill.py`, `manifest.json`, `contract.json`, `skill.test.py`) satisfying
|
||||
`CODEGEN.md` and `TESTGEN.md`: a module-level `act`, a flat `CONTRACT` whose
|
||||
every key is read from `ctx.config`, an `INTEGRATION` consistent with the
|
||||
code, a non-empty `manifest.json` `description`, and `contract.json` an exact
|
||||
mirror of the body's `CONTRACT`.
|
||||
3. The leaf's hermetic mechanics test passes
|
||||
(`python skill.test.py` from the leaf folder). This proves mechanics only —
|
||||
only a real run against the user's service proves the integration.
|
||||
4. Each category id in the manifest is the id its leaves live under, and each
|
||||
category has a navigation-grade scope description.
|
||||
5. The `config` schema is well-formed (every leaf has `default` + `doc`; secrets
|
||||
marked `secret`), maps onto `bridges.<bridges[0]>` (or `packs.<name>` for a
|
||||
bridge-less pack), and the regenerated `config.example.json` + `check-config`
|
||||
are clean.
|
||||
6. Nothing deployment-specific (hostname, address, username, absolute home
|
||||
path, local model name) appears in any committed file —
|
||||
`tests/test_no_env_leaks.py` enforces this. Secrets live in `secrets.json`.
|
||||
|
||||
`tests/test_packs.py` enforces items 1–3 mechanically for every committed pack
|
||||
(valid manifest, concern + categories, integration matches code, contract flat
|
||||
and mirrored, manifest description, hermetic test passes) and keeps the shipped
|
||||
pack ids present.
|
||||
|
||||
## Worked example — a minimal bridge-less pack
|
||||
|
||||
The smallest complete pack needs a manifest and one leaf. `packs/essentials/`
|
||||
is the committed model; a hypothetical `packs/hello/`:
|
||||
|
||||
```
|
||||
packs/hello/
|
||||
pack.json
|
||||
skills/hello/greet/
|
||||
skill.py
|
||||
manifest.json
|
||||
contract.json
|
||||
skill.test.py
|
||||
```
|
||||
|
||||
`pack.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "hello",
|
||||
"title": "Hello",
|
||||
"concern": "A trivial local greeting utility that needs no account, network, or bridge.",
|
||||
"categories": {
|
||||
"hello": "Greetings: producing a friendly greeting for the user."
|
||||
},
|
||||
"bridges": [],
|
||||
"requires": [],
|
||||
"enabled_by_default": true,
|
||||
"config": {
|
||||
"enabled": {"type": "bool", "default": true, "doc": "Load the hello skills."}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`skills/hello/greet/manifest.json`:
|
||||
|
||||
```json
|
||||
{ "description": "Produce a friendly greeting." }
|
||||
```
|
||||
|
||||
The leaf's `skill.py` (a `compute`-transport body — no external action, empty
|
||||
`CONTRACT`) and `skill.test.py` follow `CODEGEN.md`/`TESTGEN.md`; the committed
|
||||
`packs/essentials/skills/time/now/` is the reference for exactly this shape.
|
||||
|
||||
For a bridge-backed pack, start from `packs/simplex/pack.json` (bridge
|
||||
reference + `host`/`port`/`token` + service vars) and
|
||||
`packs/simplex/skills/simplex/send_message/` (a body that calls the bridge over
|
||||
HTTP). For a bridge with top-level fallbacks, see `packs/nextcloud/`.
|
||||
|
||||
## Checklist
|
||||
|
||||
1. Decide the concern and the category ids; write navigation-grade category
|
||||
descriptions.
|
||||
2. If a third-party system is involved and no bridge fronts it, author the
|
||||
bridge first (see "Authoring a new bridge") and register it in `CATALOG`.
|
||||
3. Create `packs/<name>/pack.json` with `name`, `title`, `concern`,
|
||||
`categories`, `bridges` (if any), `requires`, `enabled_by_default`, and the
|
||||
`config` schema.
|
||||
4. Add each leaf under `skills/<category>/<leaf>/` following `CODEGEN.md` and
|
||||
`TESTGEN.md` (`skill.py`, `manifest.json`, `contract.json`, `skill.test.py`).
|
||||
5. Regenerate and validate config:
|
||||
`python -m semif_agent.cli check-config --write-example config.example.json`
|
||||
then `python -m semif_agent.cli check-config`.
|
||||
6. Run the tests:
|
||||
`python3 -m pytest tests/test_packs.py tests/test_codegen.py -q`
|
||||
(plus `tests/test_<name>_bridge.py` if you added a bridge).
|
||||
7. Cross-link the pack/bridge in `AGENTS.md` (and `README.md` for a shipped
|
||||
pack).
|
||||
8. Verify a real run against the user's actual service — the only proof the
|
||||
integration works.
|
||||
Reference in new issue
Block a user