# Program model

Everything the Agent can use is a Program. A Program lists callable entries in `PROGRAM_ENTRIES` (optional `ENTRY_OPTIONS`); each becomes one tool in the shared registry. Unlisted plain functions are not recorded.

## Directory

| Under openprogram/programs/ | Holds |
| --- | --- |
| `tools/` | Shipped with the core; always given to the Agent |
| `applications/` | Enabled one by one; may carry a UI |
| `_authoring/` | What Program code imports: errors, JSON parsing, reports, tool helpers, provider registries, guides, prechecks, application client |
| `_host/runtime.py` | `@function` and the shared registry |
| `_host/context.py` | `ProgramContext` |
| `_host/services.py` | Named services shared between Programs and plugins |
| `_host/registry.py` | Loads every Program, keeps its context |
| `_host/packages.py` | Python packages: entry registration, the user's packages |
| `_host/sources.py` | Harness catalogue, owner-recorded program sources |
| `_host/check.py` | `openprogram programs check` |
| `_host/ui/` | Application loader, catalog, UI, operation runtime, host calls and recordings for packages |

## Tools and applications

|  | `tools/` | `applications/` |
| --- | --- | --- |
| Ships with | The core | The core, or installed |
| Given to the Agent | Always | Only while enabled |
| Disabled alone | No | Yes |
| UI | None | May carry one, in an App window |
| `setup(ctx)` | No | Optional |
| Entries | At least one. Resident, the cold ones deferred | May be none. Deferred, also while open in a window |
| Belongs here when | The Agent loses a basic ability without it and it needs no screen | Everything else: browser, user workflows, installed applications, external harnesses |

| Location | Built-in |
| --- | --- |
| `tools/interaction/ask_user.py` | `ask_user`: a running Program asks the user and waits |
| `tools/planning/goal/` | Chat Goals: `create_goal`, `get_goal`, `update_goal`, the `goal` Program |
| `applications/browser/` | No entries; service `browser` behind `computer_use` with `type="browser"`, for the built-in browser and the user's Chrome and Edge |
| `applications/workflow_recording/` | No entries; service `workflow_recording` |
| `applications/office_editor/` | No entries; service `office_editor` behind the document window's Office routes |
| `_authoring/` | `errors`, `json_parsing`, `reports`, imported as `openprogram.programs._authoring.<module>` |

Most tools register with `@function` on importing `openprogram.programs.tools`. A tool package with `PROGRAM_ENTRIES` is named in `registry.TOOL_PROGRAMS` (only `goal`) and registers through its own context.

**Application packages**

Either form, or both in one directory:

- Python: `__init__.py` with `PROGRAM_ENTRIES`, `ENTRY_OPTIONS`, optional `setup(ctx)`.
- Declarative: `application.json`, UI (`index.html` or `ui/`), optional `backend.py` whose operations the manifest declares.

`programs/_host/ui/loader.py` `mount(ctx, root)` mounts a declarative package, built in (manifest id is the directory name) or installed. It reads the manifest, digests the directory and registers:

- The UI, served from the application's own directory under that digest.
- Resource provider `application.<id>`.
- One entry per operation marked `agent: true`, named `<id>__<operation>`, other than `[A-Za-z0-9_-]` replaced by `_`. Takes `input` (and `project_id` if project-scoped), runs through the worker's application service, one process per operation with manifest capability checks, and returns the finished run. Deferred, needs approval, not in `DEFAULT_TOOLS`. `agent: false` gets no entry.

Installed: recorded in `~/.openprogram/programs/catalog.json` by source directory, nothing copied. Loads in the worker at start, after install, enable and disable, and when the watcher sees a change. Disabling disposes the context; a changed source is disposed and reloaded with a new digest.

Built in: a `builtin-application` row (`{"id": "browser", "enabled": false}`), read by `catalog.builtin_enabled`. `registry.sync_builtin_application` matches it on every load, so the setting holds in every process and across restarts; `PATCH /api/applications/<id>` applies it live. A re-enabled entry is the same tool with its toolsets and tier.

## ProgramContext

The single registration interface; one per application package and plugin. Every registration records its undo; `ctx.dispose()` runs them newest first and restores the host. It writes into the registries the host already has.

| Method | Registers |
| --- | --- |
| `ctx.tool(fn)`, `ctx.tools_from(module)` | `PROGRAM_ENTRIES` as tools, owned by `ctx.owner` |
| `ctx.on`, `ctx.gate`, `ctx.transform` | Event-bus subscriptions |
| `ctx.context(name, build, ...)` | Context section `<owner>:<name>`: system prompt (`L0`, `L1`) or one call's prompt (`L2`) |
| `ctx.provide`, `ctx.service` | Named service. The core uses `services.lookup(name)`, `None` without it |
| `ctx.resource(provider)` | Provider for the `resource` tool; `application.<id>` is reserved |
| `ctx.command`, `ctx.skills`, `ctx.mcp_server`, `ctx.provider_root`, `ctx.api_provider` | Slash commands, skill roots, MCP servers, provider directories, model wire implementations |
| `ctx.effect(dispose)` | Anything else, with the given undo |

```
def setup(ctx):
    @ctx.tool
    def word_count(text: str) -> int:
        '''Count the words in a text.'''
        return len(text.split())

    ctx.on("tool.after", lambda event: ctx.log.info(event.payload["tool"]))
```

**Owners and reload**

| Owner (`_program_owner`) | For |
| --- | --- |
| `tool:<name>` | Tool package in `TOOL_PROGRAMS` |
| `application:<name>` | Built-in application |
| Package name | User Program |
| `external:<package>` | External harness |
| `plugin:<name>` | Plugin |

Every owner other than `tool:` and `plugin:` is an application; entries registered through its context start deferred.

The registry keeps owner → context. Loading again reuses it and records nothing twice. Unloading or hot reload (`programs/_host/watcher.py`) calls `dispose_program(owner)`; a reload registers through a fresh context.

## Plugins

Only a declarative package in the Claude Code, Codex, OpenCode or Hermes format: skills, slash commands, agents, hooks, MCP servers and provider directories named by a manifest. `plugins/manifest.py` reads them and mounts through a context owned by `plugin:<name>`. Hooks are shell commands subscribed through `ctx.gate` and `ctx.on`. No plugin code runs in the worker.

A manifest naming code (`python`, `module`, `setup`, `main`, `tools`, `client`, `web`, `sidebar`, a `module:name` hook mapping, a Hermes `__init__.py`) is refused at install and load: package it as an application with `openprogram programs install <dir>` and `application.json`. [Plugin runtime](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/extensions/plugin-runtime.html)

## Exposure tiers

| Tier | The model sees | Assigned to |
| --- | --- | --- |
| resident | Schema in every tools array | Tools in `RESIDENT_TOOLS`: the default set, which holds the loader `tool_search`, minus cold large-schema members, plus `read_conversation` |
| deferred | Name only; `tool_search` loads the schema | Other tools, tools registered `defer=True`, and application entries (`ProgramContext._adopt_tools`): built-in, installed, external harnesses, user packages |
| hidden | Nothing; Python only | `expose=False` |

Kept as `_tier`, set with `set_tier` (`programs._host.runtime.Tier`). Approval (`requires_approval`, possibly a function of the arguments) is independent: a deferred entry can need approval, and a resident one can run without it. `_defer` mirrors the tier for readers that price and persist the tools array. `openprogram programs list` and `/api/plugins/inspect` show tiers.

**Open in a window**

The signal is the session's application bindings (`programs/_host/ui/bindings.py`, table `resource_bindings`). An `open` naming the session binds an instance: the App's window through `POST /api/applications/<id>/open` with `session_id`, or the Agent's call through `application.<id>`. `release` or `POST /api/applications/<id>/close` removes it.

Each turn `TurnBindings` reads `open_application_ids(session_id)` and installs it (`_runtime.install_open_applications`). Opening loads nothing: the catalog line of an open application says so and ends with `activation_note`, and the model loads its tools by name ([Deferred tools](https://fzkuji.github.io/OpenProgram/docs/reference/design/function/deferred-tools.html)).

`split_tools_for_dispatch` and `tool_search` read `resident_now`: a resident entry or a deferred entry loaded with `tool_search` enters the tools array. The registered tier never changes; the copy for one request is `as_resident`.

## User directory

One per profile, resolved by `openprogram/state/program_directory.py` without importing the registry. Named profiles use `~/.openprogram-<profile>/programs/`.

| Under ~/.openprogram/programs/ | Holds |
| --- | --- |
| `<name>/` | Program source: Python package, application directory, or both. Names starting with `.` or `_`, symlinks and files are not scanned |
| `catalog.json` | Applications by source directory, with settings. Sources elsewhere (such as `examples/applications/`) keep `enabled`, `hidden`, `display_title` |
| `.runtime/` | `data/`, `runs/`, `environments/`, `staging/`, `state.sqlite`, `versions/` (read only when an application's source is gone) |
| `.removed/` | Set aside by `openprogram programs uninstall` |

Files under `.runtime/` and `.removed/` are not source: role calls from them carry no call site.

**How each kind loads**

- **Package**: `_host/packages.py`, under its directory name (a Python identifier shadowing no module). Its own Git repository; only a clean, committed checkout loads, so a passing `openprogram programs check` makes a change callable. Uncommitted edits keep serving the last loaded version.
- **Application**: `loader.mount` in the worker. A directory without a catalog row is recorded on the next `loader.sync` (`catalog.adopt_user_applications`), unless it needs trust (a Python backend or `files.project.write`): that waits for `openprogram programs install <dir> --trust`. An uninstalled row is never re-recorded.
- **Harness clone** (a repository with `<package>/agentics/__init__.py`) installed from git: recorded in `program-sources.json`, owned by `external:<package>`.

One scan (`program_directory.program_dirs`) serves both loaders. The watcher fingerprints the top level, the files and Git state of every package and the catalog with each enabled application's source; a change re-runs discovery.

**Migration from the older layout**

`program_directory.migrate_user_directory` runs at worker start, before anything loads, for that profile:

| From | To |
| --- | --- |
| `workflows/<entry>` | `programs/<entry>` |
| `applications/catalog.json` and lock | `programs/` |
| Rest of `applications/` | `programs/.runtime/` |
| Clones under `openprogram/programs/packages/` or `applications/` (running package, or checkout recorded as `catalog_root`) | `programs/<name>`, rewriting its `program-sources.json` and `catalog.json` rows |

Renamed, never copied. An item whose destination exists stays and is logged. Emptied directories are removed; catalog rows pointing into them are rewritten. Each move logs one line; a second start does nothing. No settings key names either directory.

## CLI

| openprogram programs | Does |
| --- | --- |
| `list [--json]` | Every Program: kind, owner, load state, directory, enabled, entries and tiers |
| `run NAME [--arg k=v]` | Calls an entry in this process |
| `run APP OPERATION [--input JSON] [--project ID] [--request-key KEY]` | Starts an Agent-visible operation in the worker; the key makes a retried start safe |
| `status RUN_ID`, `cancel RUN_ID` | Inspect or cancel an operation run |
| `check DIRECTORY [-m MSG] [--json]` | Package: validates and tests the working copy in the OS sandbox, then commits; its checked version is not imported during the check, so a version that no longer loads can be repaired. Application: validates |
| `install SOURCE [--upgrade] [--replace] [--trust]` | Records a local application directory; clones a local package repository, git URL or `owner/repo` into `~/.openprogram/programs/<name>` |
| `uninstall NAME` | Unregisters an application (data kept), or moves a directory into `.removed/` |
| `enable ID`, `disable ID` | Loads or removes an application's UI, resource, services and entries; a tool is refused |

State-changing application verbs go through the owner API (`/api/applications/*`); `list` and `check` need no worker. The REPL's `/programs` lists the same rows. Code: `openprogram/interfaces/cli/commands/capabilities/programs.py`.

## Implementation status

Everything above is implemented. The browser core reaches `browser` only through the service and runs without it ([browser application](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/computer-and-browser-use.html#the-browser-application)). Applications gained capability `files.project.write`. Tool names, toolsets and options are unchanged, and the Programs page keeps `workflow/<name>` explorer paths, used by the Web app with the owner API's `/api/applications/*` routes. User packages import reports from `openprogram.programs._authoring.reports`.

Removed: `programs/workflow/` and its import path; `agent_browser`, `browser_agent`, `playwright_browser`, CLI `/browser`; `openprogram workflows`, `openprogram apps`, `programs apps`, `programs available`, `gui` and `all` install selectors; pip as a plugin source; plugin Web modules, pages and sidebar rows.

[Function calling](https://fzkuji.github.io/OpenProgram/docs/reference/design/function/calling-unification.html)
