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
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
- The user plugin root,
<data>/plugins/(userPluginsDirectory,src/runtime/host-dirs.ts), is a second Chord plugins root afterapps/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/v1and its per-kind modules; the bundles resolvereactand the other host packages from the app's and the repository'snode_modules(common.nodePaths,src/hosts/bundles.ts).pluginFile(path)from@earendil-works/chord/pluginsresolves against the plugin's own directory in whichever root it lives (the facet'splugin.dir). - 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: nullandcwd: <data>/plugins(locationlocal:<data>/plugins), whose tools then act on the server's files. - Applying is
reload_plugins(plugins/dev-tools): the worker calls the server objectPluginReload(worker principals only), which callsServerParts.plugins.reload(requested?)(applyPluginsinsrc/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 invalidpackage.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.
- Showing:
view_open(code mode) opens any kind a web facet registered inViewKinds({ <kind>: { Pane } }, docs/views.md), withitemnaming 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. - 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 registryPluginSkills(src/runtime/plugin-skills.ts, exported by@p2/app/v1/worker), keyed by plugin,{ dir }(a facet passespluginFile("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'sp2-pluginsprompt section: multiplayer or persistent views are plugins; start a subagent atlocal:<data>/plugins, whose skills include the guide. - Plugin tools are code-mode only by default (
callers: ["tools"]), grouped by the code-mode namespace their extension declares (namespace: { name, description, instructions? }, Durable'sToolNamespace): the prompt lists the namespace and its description from the extensions present, so it stays stable while tools come and go, anddescribeNamespace(name)gives the instructions and tools. The skill and the example say so; leavingcallersout offers a tool in the model's main list.
Decisions
- No per-turn authority.
originis gone from p2 (tool requirements,originOf,answerOrigin, theoriginfields of agent-written entries and ofConversations,PeerConversation,ConversationBrokerandAgentLocations, 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_pluginshas no requirements, like bash: subagents can be given it. - A failed build applies nothing, bundles included.
Viewsevolves by versions (evolvewithaccepts) instead of changing in place: version 2 added plugin kinds, version 3itemand 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.