gestalt-mobile 0.61.1 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -5
- package/dist/client/assets/{index-Cp-JfcNd.css → index-CRnoGk06.css} +1 -1
- package/dist/client/assets/{index-DeNYHfGR.js → index-DehRk2EQ.js} +7 -3
- package/dist/client/index.html +2 -2
- package/dist/server/server/composition.js +47 -2
- package/dist/server/server/features/plans/application/parse-supervised-plan.js +5 -0
- package/dist/server/server/features/plans/application/supervised-plan-registry.js +11 -2
- package/dist/server/server/features/sessions/model/relay-session.js +17 -2
- package/dist/server/server/features/sessions/start-session/use-case.js +1 -0
- package/dist/server/server/features/skills/list-available/endpoint.js +1 -0
- package/dist/server/server/features/skills/list-profiles/endpoint.js +1 -0
- package/dist/server/server/features/skills/model/skill-profile.js +20 -7
- package/dist/server/server/features/skills/replace-profile/endpoint.js +6 -1
- package/dist/server/server/platform/codex/codex-playwright.js +399 -0
- package/dist/server/server/platform/codex/managed-playwright.js +150 -0
- package/dist/server/server/platform/codex/session-runtime.js +34 -1
- package/dist/server/server/platform/skills/codex-skill-catalog.js +1 -1
- package/dist/server/shared/contracts/managed-playwright.js +34 -0
- package/dist/server/shared/contracts/profile-capabilities.js +13 -0
- package/package.json +1 -1
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2026 Dyne.org foundation
|
|
3
|
+
* Designed by Denis Roio <jaromil@dyne.org>
|
|
4
|
+
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
|
+
*/
|
|
6
|
+
import { readFile, realpath } from 'node:fs/promises';
|
|
7
|
+
import { isAbsolute } from 'node:path';
|
|
8
|
+
import { managedPlaywrightRequestSchema } from '../../../shared/contracts/managed-playwright.js';
|
|
9
|
+
import { profileCapabilitiesSchema } from '../../../shared/contracts/profile-capabilities.js';
|
|
10
|
+
import { beforeDeadline } from '../retrieval/codex-xerj.js';
|
|
11
|
+
import { ManagedPlaywright, playwrightServerName, playwrightTools, } from './managed-playwright.js';
|
|
12
|
+
/** One browser owner per relay root. This is a capability adapter, not a global MCP registry. */
|
|
13
|
+
export class CodexPlaywright {
|
|
14
|
+
installation;
|
|
15
|
+
bindings = new Map();
|
|
16
|
+
prepared = new WeakMap();
|
|
17
|
+
constructor(installation) {
|
|
18
|
+
this.installation = installation;
|
|
19
|
+
}
|
|
20
|
+
async binding(authority) {
|
|
21
|
+
const workspace = await realpath(authority.session.workspacePath);
|
|
22
|
+
const old = this.bindings.get(authority.session.id);
|
|
23
|
+
if (old &&
|
|
24
|
+
old.workspace === workspace &&
|
|
25
|
+
old.root === (authority.session.threadId ?? null) &&
|
|
26
|
+
old.plan === authority.planIdentity &&
|
|
27
|
+
old.milestone === authority.activePlanId)
|
|
28
|
+
return old;
|
|
29
|
+
const binding = {
|
|
30
|
+
workspace,
|
|
31
|
+
root: authority.session.threadId ?? null,
|
|
32
|
+
plan: authority.planIdentity,
|
|
33
|
+
milestone: authority.activePlanId,
|
|
34
|
+
rebind: old?.applied === true,
|
|
35
|
+
requested: false,
|
|
36
|
+
released: false,
|
|
37
|
+
applied: old?.applied ?? false,
|
|
38
|
+
connected: false,
|
|
39
|
+
packageStatus: 'unknown',
|
|
40
|
+
chromeStatus: 'unknown',
|
|
41
|
+
nodeStatus: 'unknown',
|
|
42
|
+
chromeVersion: null,
|
|
43
|
+
readinessObservedAt: null,
|
|
44
|
+
};
|
|
45
|
+
this.bindings.set(authority.session.id, binding);
|
|
46
|
+
return binding;
|
|
47
|
+
}
|
|
48
|
+
consent(authority) {
|
|
49
|
+
const snapshot = profileCapabilitiesSchema.safeParse(authority.session.effectiveSkillSelection?.capabilities);
|
|
50
|
+
return (authority.session.provider === 'codex' &&
|
|
51
|
+
snapshot.success &&
|
|
52
|
+
snapshot.data.playwright.enabled === true);
|
|
53
|
+
}
|
|
54
|
+
async control(authority, arguments_, rootOwned) {
|
|
55
|
+
const parsed = managedPlaywrightRequestSchema.safeParse(arguments_);
|
|
56
|
+
if (!rootOwned || !parsed.success)
|
|
57
|
+
return {
|
|
58
|
+
capability: 'playwright',
|
|
59
|
+
status: 'unavailable',
|
|
60
|
+
reason: 'root-supervisor-required-or-invalid-request',
|
|
61
|
+
};
|
|
62
|
+
if (parsed.data.planId !== undefined && parsed.data.planId !== authority.activePlanId)
|
|
63
|
+
return { capability: 'playwright', status: 'unavailable', reason: 'unknown-plan-id' };
|
|
64
|
+
const binding = await this.binding(authority);
|
|
65
|
+
if (parsed.data.action === 'stop') {
|
|
66
|
+
binding.requested = false;
|
|
67
|
+
binding.released = true;
|
|
68
|
+
binding.reason = undefined;
|
|
69
|
+
}
|
|
70
|
+
if (!this.consent(authority)) {
|
|
71
|
+
binding.requested = false;
|
|
72
|
+
if (binding.applied)
|
|
73
|
+
return {
|
|
74
|
+
capability: 'playwright',
|
|
75
|
+
status: 'pending',
|
|
76
|
+
boundary: 'owned-runtime-resume',
|
|
77
|
+
reason: 'profile-disabled',
|
|
78
|
+
};
|
|
79
|
+
return { capability: 'playwright', status: 'off' };
|
|
80
|
+
}
|
|
81
|
+
if (parsed.data.action === 'start') {
|
|
82
|
+
if (authority.fenced)
|
|
83
|
+
return {
|
|
84
|
+
capability: 'playwright',
|
|
85
|
+
status: 'unavailable',
|
|
86
|
+
reason: 'manual-off-or-attention',
|
|
87
|
+
};
|
|
88
|
+
if (binding.reason)
|
|
89
|
+
binding.applied = false;
|
|
90
|
+
binding.requested = true;
|
|
91
|
+
binding.released = false;
|
|
92
|
+
binding.reason = undefined;
|
|
93
|
+
}
|
|
94
|
+
return this.status(binding, this.desired(authority, binding));
|
|
95
|
+
}
|
|
96
|
+
desired(authority, binding) {
|
|
97
|
+
return (this.consent(authority) &&
|
|
98
|
+
!authority.fenced &&
|
|
99
|
+
!binding.released &&
|
|
100
|
+
(binding.requested || authority.declared));
|
|
101
|
+
}
|
|
102
|
+
status(binding, desired) {
|
|
103
|
+
const pending = binding.rebind || desired !== binding.applied;
|
|
104
|
+
return {
|
|
105
|
+
capability: 'playwright',
|
|
106
|
+
status: pending
|
|
107
|
+
? 'pending'
|
|
108
|
+
: binding.connected
|
|
109
|
+
? 'connected'
|
|
110
|
+
: binding.reason
|
|
111
|
+
? 'unavailable'
|
|
112
|
+
: 'idle',
|
|
113
|
+
...(pending ? { boundary: 'owned-runtime-resume' } : {}),
|
|
114
|
+
...(binding.reason ? { reason: binding.reason } : {}),
|
|
115
|
+
...(binding.version ? { packageVersion: binding.version } : {}),
|
|
116
|
+
packageStatus: binding.packageStatus,
|
|
117
|
+
chrome: { status: binding.chromeStatus, version: binding.chromeVersion },
|
|
118
|
+
nodeStatus: binding.nodeStatus,
|
|
119
|
+
readinessObservedAt: binding.readinessObservedAt,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
needsResume(authority) {
|
|
123
|
+
const existing = this.bindings.get(authority.session.id);
|
|
124
|
+
const desired = this.consent(authority) &&
|
|
125
|
+
!authority.fenced &&
|
|
126
|
+
!existing?.released &&
|
|
127
|
+
(existing?.requested || authority.declared);
|
|
128
|
+
// Browser-off chat must not acquire a filesystem/readiness dependency or
|
|
129
|
+
// an extra asynchronous boundary on every Autopilot root continuation.
|
|
130
|
+
if (!desired && !existing?.applied && !existing?.rebind)
|
|
131
|
+
return false;
|
|
132
|
+
if (existing &&
|
|
133
|
+
!existing.rebind &&
|
|
134
|
+
existing.workspace === authority.session.workspacePath &&
|
|
135
|
+
existing.root === authority.session.threadId &&
|
|
136
|
+
existing.plan === authority.planIdentity &&
|
|
137
|
+
existing.milestone === authority.activePlanId &&
|
|
138
|
+
desired === existing.applied)
|
|
139
|
+
return false;
|
|
140
|
+
return this.binding(authority).then((binding) => binding.rebind || this.desired(authority, binding) !== binding.applied);
|
|
141
|
+
}
|
|
142
|
+
/** Called only at thread start/resume. Native app-server owns the managed stdio child. */
|
|
143
|
+
async prepare(base, authority, input) {
|
|
144
|
+
const binding = await this.binding(authority);
|
|
145
|
+
const current = () => {
|
|
146
|
+
const now = input.current?.() ?? authority;
|
|
147
|
+
return (this.bindings.get(authority.session.id) === binding &&
|
|
148
|
+
now.session.id === authority.session.id &&
|
|
149
|
+
now.session.workspacePath === authority.session.workspacePath &&
|
|
150
|
+
((now.session.threadId ?? null) === (authority.session.threadId ?? null) ||
|
|
151
|
+
now.session.threadId === binding.root) &&
|
|
152
|
+
now.planIdentity === authority.planIdentity &&
|
|
153
|
+
now.activePlanId === authority.activePlanId &&
|
|
154
|
+
this.consent(now) === this.consent(authority) &&
|
|
155
|
+
now.fenced === authority.fenced);
|
|
156
|
+
};
|
|
157
|
+
const skill = input.skills.find((entry) => entry.name === 'gestalt:playwright');
|
|
158
|
+
let native = {};
|
|
159
|
+
let compatible = false;
|
|
160
|
+
try {
|
|
161
|
+
const response = (await beforeDeadline(input.rpc.request('config/read', { includeLayers: false }), input.deadline));
|
|
162
|
+
native = response.config ?? {};
|
|
163
|
+
compatible = !!response.config;
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
/* Optional capability stays unavailable. */
|
|
167
|
+
}
|
|
168
|
+
const servers = omitUnset(native.mcp_servers ?? {});
|
|
169
|
+
const reserved = servers[playwrightServerName] ?? {};
|
|
170
|
+
const mcp = {};
|
|
171
|
+
for (const [name, server] of Object.entries(servers)) {
|
|
172
|
+
if (Array.isArray(server?.args) &&
|
|
173
|
+
server.args[0] === 'playwright' &&
|
|
174
|
+
server.args[1] === 'mcp')
|
|
175
|
+
mcp[name] = { ...server, enabled: false, required: false };
|
|
176
|
+
}
|
|
177
|
+
const rules = native.skills;
|
|
178
|
+
let enabled = false, guidance = '', manager = 'gestalt';
|
|
179
|
+
const options = [];
|
|
180
|
+
binding.connected = false;
|
|
181
|
+
binding.reason = undefined;
|
|
182
|
+
if (this.desired(authority, binding)) {
|
|
183
|
+
binding.reason = 'installation-or-runtime-unavailable';
|
|
184
|
+
binding.packageStatus = 'unknown';
|
|
185
|
+
binding.chromeStatus = 'unknown';
|
|
186
|
+
binding.nodeStatus = 'unknown';
|
|
187
|
+
binding.chromeVersion = null;
|
|
188
|
+
binding.readinessObservedAt = null;
|
|
189
|
+
try {
|
|
190
|
+
const install = await beforeDeadline(this.installation.check(), input.deadline);
|
|
191
|
+
binding.packageStatus = install.status;
|
|
192
|
+
binding.readinessObservedAt = new Date().toISOString();
|
|
193
|
+
if (install.status === 'installed') {
|
|
194
|
+
manager = install.manager;
|
|
195
|
+
binding.version = install.version;
|
|
196
|
+
let validArgs = true;
|
|
197
|
+
if (reserved.args !== undefined) {
|
|
198
|
+
const args = reserved.args;
|
|
199
|
+
validArgs = Array.isArray(args) && args[0] === 'playwright' && args[1] === 'mcp';
|
|
200
|
+
if (validArgs)
|
|
201
|
+
for (let i = 2; i < args.length; i += 2) {
|
|
202
|
+
const option = args[i], value = args[i + 1];
|
|
203
|
+
if (typeof value !== 'string' || /[\x00-\x1f\x7f]/.test(value)) {
|
|
204
|
+
validArgs = false;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
if (option === '--cwd' && (await realpath(value)) === binding.workspace)
|
|
208
|
+
continue;
|
|
209
|
+
if (option !== '--executable-path' || !isAbsolute(value) || options.length) {
|
|
210
|
+
validArgs = false;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
options.push(option, value);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const conflict = reserved.enabled === false ||
|
|
217
|
+
reserved.url !== undefined ||
|
|
218
|
+
(reserved.command !== undefined && reserved.command !== manager) ||
|
|
219
|
+
reserved.env !== undefined ||
|
|
220
|
+
(Array.isArray(reserved.env_vars) && reserved.env_vars.length > 0);
|
|
221
|
+
if (compatible &&
|
|
222
|
+
skill &&
|
|
223
|
+
rules?.include_instructions !== false &&
|
|
224
|
+
rules?.config
|
|
225
|
+
?.filter((rule) => rule.name === skill.name || rule.path === skill.path)
|
|
226
|
+
.at(-1)?.enabled !== false &&
|
|
227
|
+
!conflict &&
|
|
228
|
+
validArgs) {
|
|
229
|
+
guidance = await beforeDeadline(readFile(skill.path, 'utf8'), input.deadline);
|
|
230
|
+
if (guidance.length <= 16_384) {
|
|
231
|
+
const readiness = await beforeDeadline(this.installation.ready(manager, binding.workspace, input.deadline, options), input.deadline);
|
|
232
|
+
enabled = readiness.ready;
|
|
233
|
+
binding.packageStatus =
|
|
234
|
+
readiness.package === 'unknown' ? 'installed' : readiness.package;
|
|
235
|
+
binding.chromeStatus = readiness.chrome;
|
|
236
|
+
binding.chromeVersion = readiness.chromeVersion;
|
|
237
|
+
binding.nodeStatus = readiness.node;
|
|
238
|
+
binding.readinessObservedAt = new Date().toISOString();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
/* No installer, config repair or shell fallback. */
|
|
245
|
+
}
|
|
246
|
+
if (enabled)
|
|
247
|
+
binding.reason = undefined;
|
|
248
|
+
}
|
|
249
|
+
if (!current()) {
|
|
250
|
+
enabled = false;
|
|
251
|
+
binding.reason = 'authority-changed';
|
|
252
|
+
}
|
|
253
|
+
binding.applied = this.desired(authority, binding);
|
|
254
|
+
binding.rebind = false;
|
|
255
|
+
const disabled = {
|
|
256
|
+
command: manager,
|
|
257
|
+
args: ['playwright', 'mcp', '--cwd', binding.workspace, ...options],
|
|
258
|
+
cwd: binding.workspace,
|
|
259
|
+
enabled: false,
|
|
260
|
+
required: false,
|
|
261
|
+
};
|
|
262
|
+
let available = enabled;
|
|
263
|
+
const state = {
|
|
264
|
+
get ready() {
|
|
265
|
+
return base.ready || available;
|
|
266
|
+
},
|
|
267
|
+
deadline: Math.max(base.deadline, input.deadline),
|
|
268
|
+
repositories: base.repositories,
|
|
269
|
+
get capabilities() {
|
|
270
|
+
return [
|
|
271
|
+
...(base.capabilities ?? []),
|
|
272
|
+
{
|
|
273
|
+
skillName: 'gestalt:playwright',
|
|
274
|
+
serverName: playwrightServerName,
|
|
275
|
+
ready: binding.connected && current(),
|
|
276
|
+
diagnostic: binding.reason,
|
|
277
|
+
},
|
|
278
|
+
];
|
|
279
|
+
},
|
|
280
|
+
get skillsConfig() {
|
|
281
|
+
return base.skillsConfig.map((entry) => entry.path === skill?.path ? { ...entry, enabled: false } : entry);
|
|
282
|
+
},
|
|
283
|
+
get config() {
|
|
284
|
+
return {
|
|
285
|
+
...base.config,
|
|
286
|
+
mcp_servers: {
|
|
287
|
+
...base.config.mcp_servers,
|
|
288
|
+
...mcp,
|
|
289
|
+
[playwrightServerName]: available
|
|
290
|
+
? {
|
|
291
|
+
...reserved,
|
|
292
|
+
...disabled,
|
|
293
|
+
enabled: true,
|
|
294
|
+
env_vars: ['GESTALT_HOME'],
|
|
295
|
+
startup_readiness: 'connection',
|
|
296
|
+
startup_timeout_ms: Math.max(1, input.deadline - Date.now()),
|
|
297
|
+
}
|
|
298
|
+
: disabled,
|
|
299
|
+
},
|
|
300
|
+
skills: {
|
|
301
|
+
config: [
|
|
302
|
+
...(base.config.skills?.config ?? []),
|
|
303
|
+
{ name: 'gestalt:playwright', enabled: false },
|
|
304
|
+
],
|
|
305
|
+
},
|
|
306
|
+
developer_instructions: `${base.config.developer_instructions ?? ''}\n<gestalt_playwright_unavailable>Browser guidance is inactive until this host verifies connected standard tools. Use gestalt_managed_playwright for session-scoped requests. Installation or a plan declaration never grants consent. Pending activation requires a safe owned-runtime resume.</gestalt_playwright_unavailable>`,
|
|
307
|
+
};
|
|
308
|
+
},
|
|
309
|
+
// Each verifier already disables only its failed connection. A shared
|
|
310
|
+
// fallback must not disable a healthy browser after an XERJ/Serena failure.
|
|
311
|
+
fallback() { },
|
|
312
|
+
get turnInstructions() {
|
|
313
|
+
return binding.connected && current()
|
|
314
|
+
? `<gestalt_playwright_capability>Verified managed browser tools are connected. Host MCP approvals apply; shell sandbox roots do not confine this browser.\n${guidance}\n</gestalt_playwright_capability>`
|
|
315
|
+
: '';
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
this.prepared.set(state, {
|
|
319
|
+
binding,
|
|
320
|
+
base,
|
|
321
|
+
enabled,
|
|
322
|
+
deadline: input.deadline,
|
|
323
|
+
current,
|
|
324
|
+
fail() {
|
|
325
|
+
available = false;
|
|
326
|
+
binding.connected = false;
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
return state;
|
|
330
|
+
}
|
|
331
|
+
async verify(rpc, threadId, state) {
|
|
332
|
+
const prepared = this.prepared.get(state);
|
|
333
|
+
if (!prepared?.enabled)
|
|
334
|
+
return true;
|
|
335
|
+
try {
|
|
336
|
+
while (Date.now() < prepared.deadline) {
|
|
337
|
+
const result = (await beforeDeadline(rpc.request('mcpServerStatus/list', {
|
|
338
|
+
threadId,
|
|
339
|
+
serverName: playwrightServerName,
|
|
340
|
+
detail: 'full',
|
|
341
|
+
}), prepared.deadline));
|
|
342
|
+
const server = result.data?.find((entry) => entry.name === playwrightServerName);
|
|
343
|
+
if (prepared.current() &&
|
|
344
|
+
server?.runtimeStatus === 'connected' &&
|
|
345
|
+
!server.toolsError &&
|
|
346
|
+
playwrightTools.every((tool) => Object.hasOwn(server.tools ?? {}, tool))) {
|
|
347
|
+
prepared.binding.connected = true;
|
|
348
|
+
prepared.binding.root = threadId;
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
if (server?.runtimeStatus !== 'starting')
|
|
352
|
+
break;
|
|
353
|
+
await beforeDeadline(new Promise((resolve) => setTimeout(resolve, 25)), prepared.deadline);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
/* Keep chat, XERJ and Serena usable. */
|
|
358
|
+
}
|
|
359
|
+
prepared.binding.reason = 'connection-or-tools-unavailable';
|
|
360
|
+
prepared.fail();
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
release(sessionId) {
|
|
364
|
+
this.bindings.delete(sessionId);
|
|
365
|
+
}
|
|
366
|
+
/** The runtime binds even an unavailable fresh capability to its actual root.
|
|
367
|
+
* Otherwise a failed fresh declaration looks like a fork at the first turn
|
|
368
|
+
* and requests a resume before Codex has created a durable rollout. */
|
|
369
|
+
bound(state, threadId) {
|
|
370
|
+
const prepared = this.prepared.get(state);
|
|
371
|
+
if (prepared)
|
|
372
|
+
prepared.binding.root = threadId;
|
|
373
|
+
}
|
|
374
|
+
observe(sessionId, notification) {
|
|
375
|
+
if (!['mcpServer/statusUpdated', 'mcpServer/startupStatus/updated'].includes(notification.method))
|
|
376
|
+
return;
|
|
377
|
+
const value = notification.params;
|
|
378
|
+
const binding = this.bindings.get(sessionId);
|
|
379
|
+
if (!binding ||
|
|
380
|
+
value?.name !== playwrightServerName ||
|
|
381
|
+
(value.threadId && value.threadId !== binding.root) ||
|
|
382
|
+
!['failed', 'cancelled', 'disconnected'].includes(String(value.status)))
|
|
383
|
+
return;
|
|
384
|
+
binding.connected = false;
|
|
385
|
+
binding.reason = 'connection-or-tools-unavailable';
|
|
386
|
+
}
|
|
387
|
+
base(state) {
|
|
388
|
+
return this.prepared.get(state)?.base ?? state;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function omitUnset(value) {
|
|
392
|
+
if (Array.isArray(value))
|
|
393
|
+
return value.map(omitUnset);
|
|
394
|
+
if (value !== null && typeof value === 'object')
|
|
395
|
+
return Object.fromEntries(Object.entries(value)
|
|
396
|
+
.filter(([, entry]) => entry !== null && entry !== undefined)
|
|
397
|
+
.map(([key, entry]) => [key, omitUnset(entry)]));
|
|
398
|
+
return value;
|
|
399
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2026 Dyne.org foundation
|
|
3
|
+
* Designed by Denis Roio <jaromil@dyne.org>
|
|
4
|
+
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
|
+
*/
|
|
6
|
+
import { constants } from 'node:fs';
|
|
7
|
+
import { access, readFile, realpath, stat } from 'node:fs/promises';
|
|
8
|
+
import { homedir } from 'node:os';
|
|
9
|
+
import { basename, dirname, isAbsolute, join } from 'node:path';
|
|
10
|
+
import { spawn } from 'node:child_process';
|
|
11
|
+
export const playwrightServerName = 'gestalt-playwright';
|
|
12
|
+
export const playwrightTools = [
|
|
13
|
+
'browser_navigate',
|
|
14
|
+
'browser_snapshot',
|
|
15
|
+
'browser_fill_form',
|
|
16
|
+
'browser_take_screenshot',
|
|
17
|
+
'browser_close',
|
|
18
|
+
];
|
|
19
|
+
const unknownReadiness = () => ({
|
|
20
|
+
ready: false,
|
|
21
|
+
package: 'unknown',
|
|
22
|
+
chrome: 'unknown',
|
|
23
|
+
node: 'unknown',
|
|
24
|
+
chromeVersion: null,
|
|
25
|
+
});
|
|
26
|
+
/** Bounded reads only. Installation and Chrome ownership remain with the operator. */
|
|
27
|
+
export class ManagedPlaywright {
|
|
28
|
+
environment;
|
|
29
|
+
constructor(environment = process.env) {
|
|
30
|
+
this.environment = environment;
|
|
31
|
+
}
|
|
32
|
+
async check() {
|
|
33
|
+
const home = this.environment.GESTALT_HOME ?? join(this.environment.HOME ?? homedir(), '.gestalt');
|
|
34
|
+
try {
|
|
35
|
+
const root = await realpath(join(home, 'playwright'));
|
|
36
|
+
const path = join(root, 'active.json');
|
|
37
|
+
if ((await stat(path)).size > 262_144)
|
|
38
|
+
return { status: 'unavailable' };
|
|
39
|
+
const install = JSON.parse(await readFile(path, 'utf8'));
|
|
40
|
+
if (install.schemaVersion !== 1 ||
|
|
41
|
+
install.contractVersion !== 1 ||
|
|
42
|
+
typeof install.version !== 'string' ||
|
|
43
|
+
install.version.length > 32 ||
|
|
44
|
+
!/^\d+\.\d+\.\d+$/.test(install.version) ||
|
|
45
|
+
typeof install.release !== 'string' ||
|
|
46
|
+
!isAbsolute(install.release))
|
|
47
|
+
return { status: 'unavailable' };
|
|
48
|
+
const release = await realpath(install.release);
|
|
49
|
+
if (dirname(release) !== root || !basename(release).startsWith(`release-${install.version}-`))
|
|
50
|
+
return { status: 'unavailable' };
|
|
51
|
+
const candidates = this.environment.GESTALT_MANAGER_BIN
|
|
52
|
+
? [this.environment.GESTALT_MANAGER_BIN]
|
|
53
|
+
: (this.environment.PATH ?? '')
|
|
54
|
+
.split(':')
|
|
55
|
+
.filter(Boolean)
|
|
56
|
+
.map((path) => join(path, 'gestalt'));
|
|
57
|
+
for (const manager of candidates) {
|
|
58
|
+
if (!isAbsolute(manager))
|
|
59
|
+
continue;
|
|
60
|
+
try {
|
|
61
|
+
await access(manager, constants.X_OK);
|
|
62
|
+
return { status: 'installed', manager, version: install.version };
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
/* Try the next operator-owned executable. */
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { status: 'unavailable' };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
return {
|
|
72
|
+
status: error.code === 'ENOENT' ? 'absent' : 'unavailable',
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Doctor performs no repair, installation or browser launch. Output never escapes this adapter. */
|
|
77
|
+
async ready(manager, cwd, deadline, options = []) {
|
|
78
|
+
if (Date.now() >= deadline)
|
|
79
|
+
return unknownReadiness();
|
|
80
|
+
const env = Object.fromEntries(['PATH', 'HOME', 'TMPDIR', 'GESTALT_HOME']
|
|
81
|
+
.filter((key) => this.environment[key] !== undefined)
|
|
82
|
+
.map((key) => [key, this.environment[key]]));
|
|
83
|
+
return new Promise((resolve) => {
|
|
84
|
+
const child = spawn(manager, ['playwright', 'doctor', '--json', '--cwd', cwd, ...options], {
|
|
85
|
+
cwd,
|
|
86
|
+
env,
|
|
87
|
+
shell: false,
|
|
88
|
+
detached: true,
|
|
89
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
90
|
+
});
|
|
91
|
+
let output = '', overflow = false;
|
|
92
|
+
const kill = () => {
|
|
93
|
+
if (child.pid)
|
|
94
|
+
try {
|
|
95
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
/* Only this owned group. */
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const timer = setTimeout(kill, Math.max(1, deadline - Date.now()));
|
|
102
|
+
child.stdout.on('data', (data) => {
|
|
103
|
+
if (output.length + data.length > 32768) {
|
|
104
|
+
overflow = true;
|
|
105
|
+
kill();
|
|
106
|
+
}
|
|
107
|
+
else
|
|
108
|
+
output += data;
|
|
109
|
+
});
|
|
110
|
+
child.once('error', () => {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
resolve(unknownReadiness());
|
|
113
|
+
});
|
|
114
|
+
child.once('close', (code) => {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
kill();
|
|
117
|
+
try {
|
|
118
|
+
const value = JSON.parse(output);
|
|
119
|
+
if (overflow || value.schemaVersion !== 1 || value.contractVersion !== 1) {
|
|
120
|
+
resolve(unknownReadiness());
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const status = (ready) => ready === true
|
|
124
|
+
? 'ready'
|
|
125
|
+
: ready === false
|
|
126
|
+
? 'unavailable'
|
|
127
|
+
: 'unknown';
|
|
128
|
+
resolve({
|
|
129
|
+
ready: code === 0 &&
|
|
130
|
+
value.ready === true &&
|
|
131
|
+
value.package?.ready === true &&
|
|
132
|
+
value.chrome?.ready === true &&
|
|
133
|
+
value.node?.ready === true,
|
|
134
|
+
package: status(value.package?.ready),
|
|
135
|
+
chrome: status(value.chrome?.ready),
|
|
136
|
+
node: status(value.node?.ready),
|
|
137
|
+
chromeVersion: typeof value.chrome?.version === 'string' &&
|
|
138
|
+
value.chrome.version.length <= 64 &&
|
|
139
|
+
/^\d+(?:\.\d+){1,3}$/.test(value.chrome.version)
|
|
140
|
+
? value.chrome.version
|
|
141
|
+
: null,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
resolve(unknownReadiness());
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -17,6 +17,7 @@ import { gestaltOrgPlanHealthDynamicTool } from '../../../shared/contracts/org-p
|
|
|
17
17
|
import { gestaltTestRunnerDynamicTool } from '../../../shared/contracts/host-tests.js';
|
|
18
18
|
import { gestaltAutopilotWaitLeaseDynamicTool } from '../../../shared/contracts/autopilot-wait-lease.js';
|
|
19
19
|
import { gestaltAgentCapacityRecoveryDynamicTool } from '../../../shared/contracts/agent-capacity-recovery.js';
|
|
20
|
+
import { gestaltManagedPlaywrightDynamicTool } from '../../../shared/contracts/managed-playwright.js';
|
|
20
21
|
import { countDiffLines } from '../../../shared/contracts/file-change.js';
|
|
21
22
|
import { threadPlanName } from './thread-plan-name.js';
|
|
22
23
|
import { WriterAcquisitionError, } from '../../features/sessions/application/writer-acquisition.js';
|
|
@@ -193,6 +194,8 @@ export class CodexSessionRuntime {
|
|
|
193
194
|
await this.prepareRetrieval(resource, session);
|
|
194
195
|
const startedThreadId = await this.startThread(resource.process, session, resource.skillsConfig, resource.modelConfig);
|
|
195
196
|
resource.threadId = startedThreadId;
|
|
197
|
+
if (resource.retrieval)
|
|
198
|
+
this.retrieval?.bound?.(resource.retrieval, startedThreadId);
|
|
196
199
|
await this.verifyRetrieval(resource, session, true);
|
|
197
200
|
this.sessions.set(session.id, resource);
|
|
198
201
|
await this.writePendingThreadName(session.id);
|
|
@@ -224,6 +227,7 @@ export class CodexSessionRuntime {
|
|
|
224
227
|
}
|
|
225
228
|
}
|
|
226
229
|
this.stop(sessionId);
|
|
230
|
+
this.retrieval?.release?.(sessionId);
|
|
227
231
|
}
|
|
228
232
|
/** Replaces only this session's app-server while retaining its durable root thread. */
|
|
229
233
|
async recycle(session, now) {
|
|
@@ -239,6 +243,8 @@ export class CodexSessionRuntime {
|
|
|
239
243
|
resource.beginExplicitShutdown();
|
|
240
244
|
for (const resource of resources)
|
|
241
245
|
resource.dispose();
|
|
246
|
+
for (const resource of resources)
|
|
247
|
+
this.retrieval?.release?.(resource.sessionId);
|
|
242
248
|
}
|
|
243
249
|
/** Keeps a detached session's supervised-plan status observable without launching Codex. */
|
|
244
250
|
async watchPlanStatus(session) {
|
|
@@ -285,12 +291,33 @@ export class CodexSessionRuntime {
|
|
|
285
291
|
return pending && !pending.settling ? 'available' : 'cleared';
|
|
286
292
|
}
|
|
287
293
|
async startTurn(session, text, clientUserMessageId, now) {
|
|
294
|
+
const resumeRequested = this.retrieval?.needsResume?.(session);
|
|
295
|
+
if (resumeRequested === true || (resumeRequested && (await resumeRequested))) {
|
|
296
|
+
// The same root resumes only at an idle boundary. Never recycle a writer
|
|
297
|
+
// with outstanding child work or held host requests to attach a browser.
|
|
298
|
+
const current = this.sessions.get(session.id);
|
|
299
|
+
const topology = await this.readExecutorTopology(session);
|
|
300
|
+
if (current &&
|
|
301
|
+
!session.activeTurnId &&
|
|
302
|
+
current.pendingRequests.size === 0 &&
|
|
303
|
+
topology.kind === 'observed' &&
|
|
304
|
+
!topology.rootActive &&
|
|
305
|
+
topology.children.every((child) => child.qualified !== false &&
|
|
306
|
+
['idle', 'notLoaded', 'systemError'].includes(child.status ?? ''))) {
|
|
307
|
+
session = await this.recycle(session, now);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
288
310
|
const resource = this.sessions.get(session.id);
|
|
289
311
|
if (!resource || !session.threadId)
|
|
290
312
|
throw new Error('CODEX_SESSION_NOT_RUNNING');
|
|
291
313
|
const result = decodeTurnStart(await resource.process.rpc.request('turn/start', {
|
|
292
314
|
threadId: session.threadId,
|
|
293
|
-
input: [
|
|
315
|
+
input: [
|
|
316
|
+
...(resource.retrieval?.turnInstructions
|
|
317
|
+
? [{ type: 'text', text: resource.retrieval.turnInstructions, text_elements: [] }]
|
|
318
|
+
: []),
|
|
319
|
+
{ type: 'text', text, text_elements: [] },
|
|
320
|
+
],
|
|
294
321
|
...(clientUserMessageId ? { clientUserMessageId } : {}),
|
|
295
322
|
...(session.model ? { model: session.model } : {}),
|
|
296
323
|
...(session.modelSettings?.reasoningEffort
|
|
@@ -986,6 +1013,7 @@ export class CodexSessionRuntime {
|
|
|
986
1013
|
gestaltTestRunnerDynamicTool,
|
|
987
1014
|
gestaltAutopilotWaitLeaseDynamicTool,
|
|
988
1015
|
gestaltAgentCapacityRecoveryDynamicTool,
|
|
1016
|
+
gestaltManagedPlaywrightDynamicTool,
|
|
989
1017
|
],
|
|
990
1018
|
});
|
|
991
1019
|
result = {
|
|
@@ -1002,6 +1030,8 @@ export class CodexSessionRuntime {
|
|
|
1002
1030
|
result = rebindMissingRollout(session, error, replacementThreadId, now);
|
|
1003
1031
|
}
|
|
1004
1032
|
resource.threadId = result.session.threadId;
|
|
1033
|
+
if (resource.retrieval)
|
|
1034
|
+
this.retrieval?.bound?.(resource.retrieval, resource.threadId);
|
|
1005
1035
|
await this.verifyRetrieval(resource, session, result.replacementCreated);
|
|
1006
1036
|
if (result.replacementCreated && resource.threadId !== result.session.threadId)
|
|
1007
1037
|
result = rebindMissingRollout(session, missingRollout, resource.threadId, now);
|
|
@@ -1090,6 +1120,7 @@ export class CodexSessionRuntime {
|
|
|
1090
1120
|
gestaltTestRunnerDynamicTool,
|
|
1091
1121
|
gestaltAutopilotWaitLeaseDynamicTool,
|
|
1092
1122
|
gestaltAgentCapacityRecoveryDynamicTool,
|
|
1123
|
+
gestaltManagedPlaywrightDynamicTool,
|
|
1093
1124
|
],
|
|
1094
1125
|
...(session.model ? { model: session.model } : {}),
|
|
1095
1126
|
...this.permissionParams(session),
|
|
@@ -1153,6 +1184,7 @@ export class CodexSessionRuntime {
|
|
|
1153
1184
|
const notificationUnsubscribe = process.rpc.onNotification((notification) => {
|
|
1154
1185
|
if (!resource.active)
|
|
1155
1186
|
return;
|
|
1187
|
+
this.retrieval?.observe?.(session.id, notification);
|
|
1156
1188
|
// Internal evidence may only be constructed by this resource's tracker.
|
|
1157
1189
|
if (notification.method === 'gestalt/mcpStartup')
|
|
1158
1190
|
return;
|
|
@@ -1367,6 +1399,7 @@ export class CodexSessionRuntime {
|
|
|
1367
1399
|
if (fresh) {
|
|
1368
1400
|
await resource.process.rpc.request('thread/unsubscribe', { threadId: resource.threadId });
|
|
1369
1401
|
resource.threadId = await this.startThread(resource.process, session, resource.skillsConfig, resource.modelConfig);
|
|
1402
|
+
this.retrieval.bound?.(state, resource.threadId);
|
|
1370
1403
|
return;
|
|
1371
1404
|
}
|
|
1372
1405
|
await resource.process.rpc.request('thread/resume', {
|
|
@@ -72,7 +72,7 @@ export class CodexSkillCatalog {
|
|
|
72
72
|
const stable = availableSkillSchema.safeParse(skill);
|
|
73
73
|
if (!stable.success)
|
|
74
74
|
throw new SkillProfileError('INVALID_SKILL_DISCOVERY', 'Invalid Codex skill metadata.');
|
|
75
|
-
return stable.data.name
|
|
75
|
+
return ['gestalt:xerj', 'gestalt:playwright'].includes(stable.data.name)
|
|
76
76
|
? { ...stable.data, enabled: false }
|
|
77
77
|
: stable.data;
|
|
78
78
|
});
|