Agent mode: an AI agent plays in the player's own tab, with the agent's clock or a running one

The MCP server gains a second session, --browser: the game in the player's tab, reached through the
agent relay npm run dev hosts at /agent (loopback unless --host; Host and Origin checked, no token).
The player turns it on in Settings → Agent API, a section only the development page has, and picks the
clock: a pace of 30 s to 5 min a day that nobody can pause, or the agent's own. Headless gets the
running clock too (--clock realtime --pace MS) and both get a wait tool.

All of it lives in src/agent/ (development page only). The game offers HOOKS, UI.clockLocked,
setSpeed() and runClock(dt), the frame's day block moved out unchanged, and nothing more: the page
players get grows by under 1 KB gzipped and runs exactly as before.
This commit is contained in:
Wolfgang Nagele 2026-11-03 15:30:04 -05:00
parent 2526dd458d
commit 7438fd1065
53 changed files with 1827 additions and 170 deletions

View file

@ -9,15 +9,15 @@ const { load } = require('./harness');
const page = require('../scripts/lib/page');
// Names the agent files define at the top level: none may appear in the page players get.
const AGENT_NAMES = ['TUBES', 'API_CMD', 'API_DOMAINS', 'apiCall', 'apiDispatch', 'apiSchema'];
const AGENT_NAMES = ['TUBES', 'API_CMD', 'API_DOMAINS', 'API_RELAY', 'apiCall', 'apiDispatch', 'apiSchema', 'apiAgentStart'];
test('the facade is inert until called, and listens to the game only during a call', () => {
const a = load({ agent: true });
assert.equal(a.ev('toast.name'), 'toast', 'loading the page leaves toast() alone');
assert.equal(a.ev('spend.name'), 'spend', 'and spend()');
assert.equal(a.ev('API_TOASTS.depth'), 0);
assert.deepEqual(JSON.parse(a.ev('JSON.stringify(TUBES.tools)')).sort(), ['act', 'actions', 'advance', 'catalog', 'city', 'classes', 'click', 'find',
'hot_spots', 'network', 'open_link', 'panel', 'peering_check', 'results', 'schema', 'screen', 'state', 'status', 'traceroute', 'ui'].sort());
assert.deepEqual(JSON.parse(a.ev('JSON.stringify(TUBES.tools)')).sort(), ['act', 'actions', 'advance', 'catalog', 'city', 'classes', 'click', 'clock', 'find',
'hot_spots', 'network', 'open_link', 'panel', 'peering_check', 'results', 'schema', 'screen', 'since', 'state', 'status', 'traceroute', 'ui'].sort());
// Before a game there is a contract to read, and nothing else.
assert.ok(a.ev("TUBES.call('schema',{section:'commands'}).buildPop"));
assert.throws(() => a.ev("TUBES.call('status',{})"), /No game is running/);
@ -36,6 +36,21 @@ test('the facade is inert until called, and listens to the game only during a ca
assert.deepEqual(a.errors, []);
});
test('headless and the player\'s tab share one contract; only the tab holds the dice back and names its owner', () => {
const a = load({ agent: true });
a.ev('ACT.start({});S.nextFail=S.day+30;compute()');
const here = a.ev("TUBES.call('schema',{})"), there = a.ev("TUBES.call('schema',{},{relay:true})");
for (const k of ['commands', 'view', 'drafts', 'denied', 'domains', 'results']) assert.deepEqual(here[k], there[k], k);
assert.equal(here.mode.backend, 'headless');
assert.equal(there.mode.backend, 'browser');
assert.ok(there.session.startScn.refused && !here.session.startScn.refused, 'starting a scenario is the player\'s in their tab');
assert.equal(a.ev("TUBES.call('state',{path:'nextFail'})"), a.ev('S.nextFail'), 'headless reads everything');
assert.equal(a.ev("TUBES.call('state',{path:'nextFail'},{relay:true})"), null, 'the tab holds it back');
assert.equal(a.ev("'rng' in TUBES.call('state',{},{relay:true})"), false);
assert.equal(a.ev("TUBES.call('state',{path:'cash'},{relay:true})"), a.ev('S.cash'));
assert.deepEqual(a.errors, []);
});
test('the page players get has no agent code; the development page has it', () => {
const ship = page.assemble().html, dev = page.assemble({ dev: true }).html;
assert.ok(!ship.includes('@include-dev'), 'the marker line is dropped whole');
@ -51,6 +66,7 @@ test('the page players get has no agent code; the development page has it', () =
// The players' page is the game alone: no facade, and it boots and plays.
const p = load();
assert.equal(p.ev('typeof TUBES+typeof apiCall+typeof API_CMD'), 'undefinedundefinedundefined');
assert.equal(p.ev('JSON.stringify(HOOKS)+UI.clockLocked'), '{}false', 'no hooks, and the clock is the player\'s');
p.ev("ACT.start({});ACT.launchBB({city:'fra'});ACT.buyTransit({city:'fra',net:'cog',tier:1});for(let i=0;i<5;i++)dayTick()");
assert.ok(p.ev('R.served') > 0);
assert.deepEqual(p.errors, []);