pi-mono/apps/p2/docs/agent-plugins.md
Mario Zechner b9c590049e feat(p2): deliver the web client through Chord's web surface
Pages load Chord's base bundle (src/hosts/web/entry.ts, startPage) and the
plugins' web facets as Chord plugin scripts (the set as one combined file),
served by p2's own HTTP stack (webSurface routes: false) under /_chord/web/.
A plugin reload applies a plugin's web change in open pages in place
(scroll and other panes kept); only base changes reload a page. The stale
page check compares the base hash; the 5 s version poll, /__p2_web and the
8 MiB web bundle cap are gone.

- views owns cross-plugin page state through the ViewsPages registry;
  sharedContext shares React contexts across plugin scripts
- the server serves Chord's locale choice the page's language follows
- the service worker keeps /_chord/web/ files (not /_chord/web/base)
- the Cloud Brain bundle is built with buildBase and pageBootstrap and
  starts static (follow: false); ui-cloud-bundle tests it in Chromium
- ui-stale-page becomes ui-web-surface: in-place plugin change, base
  change reload, old page refused, offline start then in-place change
2026-11-12 13:55:20 +00:00

6.4 KiB

Agent-written plugins

The agent writes real p2 plugins (multiplayer or persistent views, server objects, tools) and applies them itself. Html artifacts (docs/artifacts.md) stay for single-player pages.

Problem

An html artifact is one page per viewer. A shared counter, a game with rules or a board several people edit needs state on the server, rules applied there and events between viewers: a plugin. Before this, only a p2 checkout's built-in plugins could be changed, only by owner-started turns, and only in dev mode.

How it works

  1. The user plugin root, <data>/plugins/ (userPluginsDirectory, src/runtime/host-dirs.ts), is a second Chord plugins root after apps/p2/plugins/ (createP2Plugins, src/hosts/plugins.ts). It is loaded and watched like the built-in root on the server, every worker, the web and the TUI bundles. A built-in wins a name clash (listRoots: the first root wins; discovery reports the clash). The server creates the directory at start (mode 0700). User plugins import p2 only through @p2/app/v1 and its per-kind modules; the bundles resolve react and the other host packages from the app's and the repository's node_modules (common.nodePaths, src/hosts/bundles.ts). pluginFile(path) from @earendil-works/chord/plugins resolves against the plugin's own directory in whichever root it lives (the facet's plugin.dir).
  2. Writing uses the ordinary coding tools, no plugin file tools: the agent works in the server's environment. A session working on this machine writes there directly; any other session spawns a subagent with env: null and cwd: <data>/plugins (location local:<data>/plugins), whose tools then act on the server's files.
  3. Applying is reload_plugins (plugins/dev-tools): the worker calls the server object PluginReload (worker principals only), which calls ServerParts.plugins.reload(requested?) (applyPlugins in src/hosts/server.ts):
    • requested, else every dirty plugin plus every plugin of the user root (an unchanged one keeps its digest, so its hosts keep it as it is: an apply never depends on the watcher having seen the last write; a directory not discovered yet is waited for once);
    • one rebuild: Chord's build (web facets included), then the TUI bundle. Any failure stops before the rollout, so the old code stays; the rollout ends with the web surface's step, which pages apply in place;
    • per plugin: applied, removed, failed (the build's esbuild errors as <kind>: file:line:column: message, or a host's load error), refused (unknown, or a discovery problem: an invalid package.json, a built-in's name), plus the server's and workers' errors logged since the apply began (processLogs);
    • in dev mode the dev status is notified like the Reload button.
  4. Showing: view_open (code mode) opens any kind a web facet registered in ViewKinds ({ <kind>: { Pane } }, docs/views.md), with item naming what it shows. A plugin keeping several items per session also contributes an artifact kind (ArtifactKinds), so the artifacts side panel lists them and opens them as views.
  5. Guidance is a skill, plugins/agent-plugins/skills/writing-p2-plugins/SKILL.md, with a complete, tested example next to it (example/counter/: a shared counter with presence, a wave event, a pane and a code-mode tool). Plugins' skills come from the worker registry PluginSkills (src/runtime/plugin-skills.ts, exported by @p2/app/v1/worker), keyed by plugin, { dir } (a facet passes pluginFile("skills")). The prompt plugin lists them in <available_skills> after the environment's and the project's skills (the first skill of a name wins), counted as global skills (a locked session gets them only with its global instructions), and only for conversations working on the server's machine: the listing points at the plugin's files, never copied to another environment. Its local watches rewatch when the registry's directories change. A conversation on another environment gets instead the agent-plugins plugin's p2-plugins prompt section: multiplayer or persistent views are plugins; start a subagent at local:<data>/plugins, whose skills include the guide.
  6. Plugin tools are code-mode only by default (callers: ["tools"]), grouped by the code-mode namespace their extension declares (namespace: { name, description, instructions? }, Durable's ToolNamespace): the prompt lists the namespace and its description from the extensions present, so it stays stable while tools come and go, and describeNamespace(name) gives the instructions and tools. The skill and the example say so; leaving callers out offers a tool in the model's main list.

Decisions

  • No per-turn authority. origin is gone from p2 (tool requirements, originOf, answerOrigin, the origin fields of agent-written entries and of Conversations, PeerConversation, ConversationBroker and AgentLocations, each bumped a version). Whoever may send input in a session may have its agent use every tool it has. What a session's agents reach is decided by its tool selection, its location (a sandbox) and its lock. reload_plugins has no requirements, like bash: subagents can be given it.
  • A failed build applies nothing, bundles included.
  • Views evolves by versions (evolve with accepts) instead of changing in place: version 2 added plugin kinds, version 3 item and dropped view state and the library.
  • Kind names are not checked against what pages register: the pane shows "Unknown view kind".

Tests

test/p2/agent-plugins.test.ts: a subagent at local:<root> copies the example with bash and applies it with reload_plugins (server object, event, presence, the code-mode tool and its namespace in the prompt, the view kind, the web script in the pages' combined file); an edit reloads in place; build errors with file and line, refusals (a built-in's name, an unknown plugin); pluginFile in the user root and its skills listed after the project's; a subagent given other tools cannot reload; a guest's turn uses reload_plugins and the view tools; in Chromium, an unknown kind's pane becomes the plugin's after the reload. envs.test.ts: a remote conversation lists no plugin skill and gets the hint. dev-tools.test.ts: the tool's results. tool-access.test.ts: scope and locked without origin.