Compare commits

2 Commits

Author SHA1 Message Date
8ball a2afeff8e0 Merge branch 'native-fixes': Firefox/BiDi protocol fixes, shared UX, portability on top of v3.6.4 (17 fix signatures, conflict resolved keeping both sides) 2026-11-08 23:26:02 +03:30
DeepSeek Harness Agent 0ed41d1f2f native-fixes: rebase browser-engine + shared fixes onto v3.6.4
Re-applies the reviewed fixes on top of upstream v3.6.4 (db71dd9), keeping the
new v3.6.4 logic intact: transparent-proxy rejection (directEgressOf), Edge
startup-boost survivor-proof restartSession (with a Firefox safety guard so
proxy relaunch never taskkills every Firefox window), and deploy-script
hardening.

Contents (unchanged from the previously reviewed branch, only rebased):
- Firefox/WebDriver BiDi: per-code-point typing, named-key table + always keyUp
  (Enter/Delete/PageUp/Escape), newTab create+navigate, readyState polling in
  waitForLoad, explicit session.end on detach, protocol browser.close before
  restart/dispose (no orphan browsers / "Maximum number of active sessions"),
  proxy relaunch + -proxy-server host:port, engine-scoped relaunch (Firefox
  refuses), attach guard for a browser this process already drives.
- Shared: Space key, shadow-DOM click{text}/type{into}, aria-label-first names,
  click{index} re-resolve + scrollIntoView.
- Native portability carried in: asar-aware schemastery loader, home-relative
  probe path, close action no longer leaking its session before Browser.close.
2026-11-08 21:54:19 +03:30
+200 -39
View File
@@ -1,6 +1,8 @@
/** /**
* Edge Kit Browser v3 — browsing the way a human does it. * Edge Kit Browser v3 — browsing the way a human does it.
* *
* Local branch overlay: EDGE_KIT_NATIVE_FIXES (pending review in 'native-fixes').
*
* Design: agents perceive with `see` (visible text + numbered actionable * Design: agents perceive with `see` (visible text + numbered actionable
* elements, across shadow DOM and dialogs), act like a person (`click` a * elements, across shadow DOM and dialogs), act like a person (`click` a
* numbered element or the text they can read, `type` into a field by its * numbered element or the text they can read, `type` into a field by its
@@ -21,9 +23,38 @@ import { promises as fs } from 'node:fs';
// resolve it by bare name, so import it from the running installation (the // resolve it by bare name, so import it from the running installation (the
// very file the Web search plugin loads). If that ever fails to resolve, the // very file the Web search plugin loads). If that ever fails to resolve, the
// kit still works — only the settings page stays hidden. // kit still works — only the settings page stays hidden.
/* EDGE_KIT_NATIVE_PORTABILITY: resolve schemastery from the running DeepSeek Harness
* installation instead of a hardcoded per-user path. Candidate roots cover Electron's
* resources directory (with app.asar), a resources folder beside the executable, and the
* per-user install location; the CommonJS build is tried before the ES module build. If
* none resolves the kit still runs and only the settings page stays hidden. */
let z = null; let z = null;
try { try {
({ default: z } = await import('file:///C:/Users/Administrator/AppData/Local/Programs/DeepSeek%20Harness/resources/app.asar/dsh/node_modules/@deepseek-ai/schemastery/lib/index.mjs')); await (async () => {
const [{ createRequire }, { pathToFileURL }, pathMod] = await Promise.all([
import('node:module'),
import('node:url'),
import('node:path'),
]);
const requireFromHere = createRequire(import.meta.url);
const roots = [];
try { if (process.resourcesPath) roots.push(process.resourcesPath); } catch { /* not Electron */ }
try { roots.push(pathMod.join(pathMod.dirname(process.execPath), 'resources')); } catch { /* no execPath */ }
if (process.env.LOCALAPPDATA) roots.push(pathMod.join(process.env.LOCALAPPDATA, 'Programs', 'DeepSeek Harness', 'resources'));
for (const root of roots) {
const base = pathMod.join(root, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai', 'schemastery');
try {
const cjs = requireFromHere(pathMod.join(base, 'lib', 'index.cjs'));
z = (cjs && (cjs.default || cjs)) || null;
} catch { /* try the ES module build next */ }
if (z) break;
try {
const esm = await import(pathToFileURL(pathMod.join(base, 'lib', 'index.mjs')).href);
z = (esm && (esm.default || esm)) || null;
} catch { /* try the next candidate root */ }
if (z) break;
}
})();
} catch { z = null; } } catch { z = null; }
export const inject = ['tools', 'web']; export const inject = ['tools', 'web'];
@@ -266,8 +297,18 @@ function bidiCall(m, p) {
}); });
} }
const KEYBI = { Enter: '\uE006', Tab: '\uE004', Space: ' ', Backspace: '\uE003', Escape: '\uE01B', ArrowUp: '\uE013', ArrowDown: '\uE015', ArrowLeft: '\uE012', ArrowRight: '\uE014', Home: '\uE010', End: '\uE011', Shift: '\uE008', Control: '\uE009', Alt: '\uE00A' }; const KEYBI = { Enter: '\uE006', Tab: '\uE004', Space: ' ', Backspace: '\uE003', Escape: '\uE00C', Delete: '\uE017', Insert: '\uE016', PageUp: '\uE00E', PageDown: '\uE00F', ArrowUp: '\uE013', ArrowDown: '\uE015', ArrowLeft: '\uE012', ArrowRight: '\uE014', Home: '\uE010', End: '\uE011', Shift: '\uE008', Control: '\uE009', Alt: '\uE00A' };
const keyVal = (params) => (typeof params.text === 'string' && params.text.length === 1 ? params.text : KEYBI[params.code || params.key] || (typeof params.key === 'string' ? params.key : ' ')); // Named keys (Enter/Delete/…) must win over their one-character text form: a
// "keyDown" value of '\r' is not the Enter key in WebDriver BiDi, and named keys
// like Delete have no text at all. Character keys fall back to the typed text.
const keyVal = (params) => {
const named = KEYBI[params.code || params.key];
if (named) return named;
if (typeof params.text === 'string' && params.text.length === 1 && params.text !== '\r') return params.text;
if (typeof params.code === 'string' && /^Key[A-Z]$/.test(params.code)) return params.code.slice(3).toLowerCase();
if (typeof params.code === 'string' && /^Digit[0-9]$/.test(params.code)) return params.code.slice(5);
return typeof params.key === 'string' ? params.key : ' ';
};
async function bidiSend(method, params, sid) { async function bidiSend(method, params, sid) {
const ctx = sid ?? session.context; const ctx = sid ?? session.context;
@@ -292,7 +333,16 @@ async function bidiSend(method, params, sid) {
return { targetInfos: (r.contexts || []).map((c) => ({ targetId: c.context, url: c.url || '', title: '', type: 'page' })) }; return { targetInfos: (r.contexts || []).map((c) => ({ targetId: c.context, url: c.url || '', title: '', type: 'page' })) };
} }
case 'Target.attachToTarget': session.context = params.targetId; return { sessionId: params.targetId }; case 'Target.attachToTarget': session.context = params.targetId; return { sessionId: params.targetId };
case 'Target.createTarget': { const r = await bidiCall('browsingContext.create', { url: params.url || 'about:blank', type: 'tab' }); return { targetId: r.context }; } case 'Target.createTarget': {
// Firefox's browsingContext.create ignores an inline URL: create the tab
// first and navigate it explicitly, then wait for the load to settle.
const wanted = typeof params.url === 'string' && params.url ? params.url : 'about:blank';
const r = await bidiCall('browsingContext.create', { type: 'tab' });
if (wanted !== 'about:blank') {
try { await bidiCall('browsingContext.navigate', { url: wanted, context: r.context, wait: 'complete' }); } catch { /* about:blank tab is usable */ }
}
return { targetId: r.context };
}
case 'Target.closeTarget': await bidiCall('browsingContext.close', { context: params.targetId }); return {}; case 'Target.closeTarget': await bidiCall('browsingContext.close', { context: params.targetId }); return {};
case 'Runtime.evaluate': { case 'Runtime.evaluate': {
// Firefox's remote agent reads the draft-style `expression` param; the // Firefox's remote agent reads the draft-style `expression` param; the
@@ -306,7 +356,18 @@ async function bidiSend(method, params, sid) {
if (r.exceptionDetails) return { exceptionDetails: { text: r.exceptionDetails.text || JSON.stringify(r.exceptionDetails.stacktrace || {}) } }; if (r.exceptionDetails) return { exceptionDetails: { text: r.exceptionDetails.text || JSON.stringify(r.exceptionDetails.stacktrace || {}) } };
return { result: { value: decr(r.result) } }; return { result: { value: decr(r.result) } };
} }
case 'Input.insertText': await pointer('key', [{ type: 'text', text: params.text }]); return {}; // Firefox rejects the draft `{type:'text'}` key action; type by dispatching
// one keyDown/keyUp pair per code point (real input events, keeps focus and
// selection behaviour), which is what BiDi actually supports.
case 'Input.insertText': {
const acts = [];
for (const ch of [...String(params.text)]) {
const value = ch === '\n' ? '\r' : ch;
acts.push({ type: 'keyDown', value }, { type: 'keyUp', value });
}
if (acts.length) await pointer('key', acts);
return {};
}
case 'Input.dispatchKeyEvent': { case 'Input.dispatchKeyEvent': {
const v = keyVal(params); const v = keyVal(params);
const acts = params.type === 'keyUp' ? [{ type: 'keyUp', value: v }] : params.type === 'keyDown' ? [{ type: 'keyDown', value: v }, { type: 'keyUp', value: v }] : [{ type: 'keyDown', value: v }]; const acts = params.type === 'keyUp' ? [{ type: 'keyUp', value: v }] : params.type === 'keyDown' ? [{ type: 'keyDown', value: v }, { type: 'keyUp', value: v }] : [{ type: 'keyDown', value: v }];
@@ -319,6 +380,7 @@ async function bidiSend(method, params, sid) {
if (params.type === 'mouseReleased') acts.push({ type: 'pointerUp', x: params.x, y: params.y, button: 0 }); if (params.type === 'mouseReleased') acts.push({ type: 'pointerUp', x: params.x, y: params.y, button: 0 });
await pointer('pointer', acts); return {}; await pointer('pointer', acts); return {};
} }
case 'session.end': try { await bidiCall('session.end', {}); } catch { /* no session */ } return {};
case 'Browser.close': try { await bidiCall('browser.close', {}); } catch { /* browser gone */ } return {}; case 'Browser.close': try { await bidiCall('browser.close', {}); } catch { /* browser gone */ } return {};
default: return {}; default: return {};
} }
@@ -355,14 +417,28 @@ async function evaluate(expression) {
async function waitForLoad(timeoutMs = 15000) { async function waitForLoad(timeoutMs = 15000) {
const loaded = new Promise((resolve) => { const loaded = new Promise((resolve) => {
let done = false, poll = null;
const finish = (how) => {
if (done) return;
done = true;
clearTimeout(timer);
clearInterval(poll);
eventHooks.delete(hook);
resolve(how);
};
const hook = (msg) => { const hook = (msg) => {
if (msg.method === 'Page.loadEventFired' && msg.sessionId === session?.sessionId) { if (msg.method === 'Page.loadEventFired' && msg.sessionId === session?.sessionId) finish('load');
eventHooks.delete(hook);
resolve('load');
}
}; };
eventHooks.add(hook); eventHooks.add(hook);
setTimeout(() => { eventHooks.delete(hook); resolve('timeout'); }, timeoutMs); const timer = setTimeout(() => finish('timeout'), timeoutMs);
// BiDi (Firefox) can fire the load event before a freshly created tab is
// attached/subscribed, so an event-only wait misses it and burns the whole
// timeout (newTab took ~15 s before this). Poll the document state as a
// fast second signal.
poll = setInterval(async () => {
if (session?.engine !== 'bidi' || done) return;
try { if ((await evaluate('document.readyState')) === 'complete') finish('readyState'); } catch { /* navigating */ }
}, 250);
}); });
const how = await loaded; const how = await loaded;
await new Promise((r) => setTimeout(r, 400)); // settle for hydration await new Promise((r) => setTimeout(r, 400)); // settle for hydration
@@ -393,7 +469,17 @@ async function bidiHandshake() {
// session.status answers, but commands only work after session.new // session.status answers, but commands only work after session.new
// with capabilities — empirically probed against Firefox 158. // with capabilities — empirically probed against Firefox 158.
await bidiCall('session.status', {}); await bidiCall('session.status', {});
const r = await bidiCall('session.new', { capabilities: { alwaysMatch: {}, firstMatch: [{}] } }); let r;
try {
r = await bidiCall('session.new', { capabilities: { alwaysMatch: {}, firstMatch: [{}] } });
} catch (e) {
// Firefox allows one BiDi session; a previous detach or another client can
// hold it, and a bare WebSocket close does not release it.
if (/Maximum number of active sessions/i.test(String((e && e.message) || ''))) {
throw new Error('Firefox already has an active WebDriver BiDi session (a previous detach or another client left it behind). Restart that Firefox instance, then retry.');
}
throw e;
}
session.bidiSessionId = r && r.sessionId; session.bidiSessionId = r && r.sessionId;
session.bidiReady = true; session.bidiReady = true;
} }
@@ -401,7 +487,15 @@ async function bidiHandshake() {
async function attachOnly(port) { async function attachOnly(port) {
const { ws, info } = await connect(port); const { ws, info } = await connect(port);
session = { ws, port, child: null, headless: null, profileDir: null, attached: true, kind: 'unknown', engine: info.bidi ? 'bidi' : 'cdp' }; session = { ws, port, child: null, headless: null, profileDir: null, attached: true, kind: 'unknown', engine: info.bidi ? 'bidi' : 'cdp' };
await attachToPage(); try {
await attachToPage();
} catch (error) {
// Never leave a half-attached session behind: status would report
// running:true with no active tab for every later call.
try { ws.close(); } catch { /* already gone */ }
session = null;
throw error;
}
} }
async function doLaunch(args) { async function doLaunch(args) {
@@ -437,7 +531,9 @@ async function doLaunch(args) {
'-profile', profileDir, '-profile', profileDir,
'-no-remote', '-no-remote',
...(headless ? ['-headless'] : []), ...(headless ? ['-headless'] : []),
...(proxyFlag ? ['-proxy-server', proxyFlag] : ['-preferences', '{"network.proxy.type":0}']), // Firefox's -proxy-server takes host:port, not an http:// URL; a
// proxy-less launch states direct traffic explicitly (network.proxy.type=0).
...(proxyFlag ? ['-proxy-server', proxyFlag.replace(/^https?:\/\//, '')] : ['-preferences', '{"network.proxy.type":0}']),
'about:blank', 'about:blank',
] : [ ] : [
`--remote-debugging-port=${port}`, `--remote-debugging-port=${port}`,
@@ -678,7 +774,13 @@ async function egressVia(hops) {
async function restartSession() { async function restartSession() {
const la = session && session.launchArgs ? session.launchArgs : null; const la = session && session.launchArgs ? session.launchArgs : null;
if (!session || session.attached || !la) return false; if (!session || session.attached || !la) return false;
const { child, ws, kind } = session; const { child, ws, engine, kind } = session;
// Close the browser itself before dropping the socket: for Firefox the spawn
// handle is only the Windows launcher, so killing it can orphan the browser
// and leave Firefox's single BiDi session registered (the next session.new
// then fails with "Maximum number of active sessions").
try { ws.send(JSON.stringify({ id: nextId++, method: engine === 'bidi' ? 'browser.close' : 'Browser.close', params: {} })); } catch { /* gone */ }
await new Promise((r) => setTimeout(r, 300));
session = null; session = null;
try { ws.close(); } catch { /* gone */ } try { ws.close(); } catch { /* gone */ }
if (child && child.exitCode === null) child.kill(); if (child && child.exitCode === null) child.kill();
@@ -691,9 +793,11 @@ async function restartSession() {
if (!(await waitFree(6000))) { if (!(await waitFree(6000))) {
// A survivor browser (Edge startup-boost, profile handoff, leftover window) // A survivor browser (Edge startup-boost, profile handoff, leftover window)
// retook the port without our new flags — a fresh spawn would merge into it // retook the port without our new flags — a fresh spawn would merge into it
// and silently lose proxy routing. Kill instances of this browser kind // and silently lose proxy routing. For Chromium, kill instances of this
// (same semantics as relaunch:true; logins survive, windows reopen). // browser kind (same semantics as relaunch:true; logins survive, windows
const exe = kind === 'firefox' ? 'firefox' : kind === 'chrome' ? 'chrome' : 'msedge'; // reopen). Firefox refuses: taskkill would close every Firefox window.
if (kind === 'firefox') return false;
const exe = kind === 'chrome' ? 'chrome' : 'msedge';
const killCmd = process.platform === 'win32' ? `taskkill /IM ${exe}.exe /F` : `pkill -9 -x ${exe}`; const killCmd = process.platform === 'win32' ? `taskkill /IM ${exe}.exe /F` : `pkill -9 -x ${exe}`;
await new Promise((r) => exec(killCmd, () => r())); await new Promise((r) => exec(killCmd, () => r()));
if (!(await waitFree(8000))) return false; if (!(await waitFree(8000))) return false;
@@ -728,13 +832,20 @@ for (let c = 0; c < 26; c++) KEYCODES['Key' + String.fromCharCode(65 + c)] = [65
for (let d = 0; d < 10; d++) KEYCODES['Digit' + d] = [48 + d, 48 + d]; for (let d = 0; d < 10; d++) KEYCODES['Digit' + d] = [48 + d, 48 + d];
async function pressKey(key, times = 1) { async function pressKey(key, times = 1) {
const vk = KEYCODES[key] || [key.length === 1 ? key.toUpperCase().charCodeAt(0) : 0, 0]; // Normalize the requested key: named keys are case-insensitive ("Space" and
const code = KEYCODES[key] ? key : (key.length === 1 ? `Key${key.toUpperCase()}` : ''); // "space" are the same key), and the character-carrying keys (Space, Enter)
const text = key === 'Space' ? ' ' : key === 'Enter' ? '\r' : (key.length === 1 ? key : undefined); // must carry their real character in `key`/`text` — CDP otherwise reports an
const base = { key, code, windowsVirtualKeyCode: vk[0], nativeVirtualKeyCode: vk[1] }; // empty event.key for the named form and games/forms never see the key.
const requested = String(key);
const named = Object.keys(KEYCODES).find((candidate) => candidate.toLowerCase() === requested.toLowerCase());
const keyName = named || requested;
const vk = KEYCODES[keyName] || [requested.length === 1 ? requested.toUpperCase().charCodeAt(0) : 0, 0];
const code = KEYCODES[keyName] ? keyName : (requested.length === 1 ? `Key${requested.toUpperCase()}` : '');
const char = keyName === 'Space' ? ' ' : keyName === 'Enter' ? '\r' : (requested.length === 1 ? requested : undefined);
const base = { key: keyName === 'Space' ? ' ' : keyName, code, windowsVirtualKeyCode: vk[0], nativeVirtualKeyCode: vk[1] };
for (let i = 0; i < times; i++) { for (let i = 0; i < times; i++) {
await send('Input.dispatchKeyEvent', { type: text !== undefined ? 'keyDown' : 'rawKeyDown', ...base, ...(text !== undefined ? { text } : {}) }, session.sessionId); await send('Input.dispatchKeyEvent', { type: char !== undefined ? 'keyDown' : 'rawKeyDown', ...base, ...(char !== undefined ? { text: char } : {}) }, session.sessionId);
await send('Input.dispatchKeyEvent', { type: text !== undefined ? 'keyUp' : 'rawKeyDown', ...base }, session.sessionId).catch(async () => { await send('Input.dispatchKeyEvent', { type: 'keyUp', ...base }, session.sessionId).catch(async () => {
await send('Input.dispatchKeyEvent', { type: 'keyUp', ...base }, session.sessionId); await send('Input.dispatchKeyEvent', { type: 'keyUp', ...base }, session.sessionId);
}); });
if (times > 1) await new Promise((r) => setTimeout(r, 90)); if (times > 1) await new Promise((r) => setTimeout(r, 90));
@@ -764,8 +875,8 @@ const WALKER = `
try { const cs = getComputedStyle(el); if (cs.visibility === 'hidden' || cs.display === 'none' || cs.opacity === '0') return null; } catch { } try { const cs = getComputedStyle(el); if (cs.visibility === 'hidden' || cs.display === 'none' || cs.opacity === '0') return null; } catch { }
return r; }; return r; };
const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim(); const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim();
const nameOf = (el) => clean(el.innerText).slice(0, 70) const nameOf = (el) => clean(el.getAttribute('aria-label')).slice(0, 70)
|| clean(el.getAttribute('aria-label')) || clean(el.getAttribute('placeholder')) || clean(el.innerText).slice(0, 70) || clean(el.getAttribute('placeholder'))
|| clean(el.getAttribute('title')) || clean(el.getAttribute('alt')) || clean(el.value) || ''; || clean(el.getAttribute('title')) || clean(el.getAttribute('alt')) || clean(el.value) || '';
const isInter = (el) => { const isInter = (el) => {
const tag = el.tagName.toLowerCase(); const role = (el.getAttribute('role') || '').toLowerCase(); const tag = el.tagName.toLowerCase(); const role = (el.getAttribute('role') || '').toLowerCase();
@@ -791,7 +902,7 @@ async function seeOnce(opts = {}) {
const key = tag + '|' + (el.getAttribute('role') || '') + '|' + name; const key = tag + '|' + (el.getAttribute('role') || '') + '|' + name;
if (seen.has(key)) continue; seen.add(key); if (seen.has(key)) continue; seen.add(key);
els.push({ role: (el.getAttribute('role') || (tag === 'a' ? 'link' : tag)), name, els.push({ role: (el.getAttribute('role') || (tag === 'a' ? 'link' : tag)), name,
x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2) }); x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2), el });
if (els.length >= 60) return; if (els.length >= 60) return;
} }
} }
@@ -808,9 +919,13 @@ async function seeOnce(opts = {}) {
} }
} }
dwalk(document); dwalk(document);
// Keep the live element references from this see so click{index} can
// re-resolve its row after scrolling or DOM movement instead of firing at a
// stale, cached coordinate (which silently no-ops on a moved page).
try { window.__edgeKitElements = els.map((entry) => entry.el); } catch { }
return { url: location.href, title: document.title, return { url: location.href, title: document.title,
text: text.slice(0, ${Math.min(opts.maxText ?? cfgVal('seeTextCap', 2200), 6000)}), text: text.slice(0, ${Math.min(opts.maxText ?? cfgVal('seeTextCap', 2200), 6000)}),
elements: els, dialogs: [...new Set(dialogs)].slice(0, 4) }; elements: els.map(({ role, name, x, y }) => ({ role, name, x, y })), dialogs: [...new Set(dialogs)].slice(0, 4) };
})()`; })()`;
return evaluate(js); return evaluate(js);
} }
@@ -821,8 +936,9 @@ async function findByName(needle) {
const want = ${JSON.stringify(String(needle).toLowerCase())}; const want = ${JSON.stringify(String(needle).toLowerCase())};
let best = null; let best = null;
function walk(root) { function walk(root) {
for (const el of root.querySelectorAll('a,button,input,textarea,select,summary,[role],[jsaction],[contenteditable]')) { for (const el of root.querySelectorAll('*')) {
if (el.shadowRoot) walk(el.shadowRoot); if (el.shadowRoot) walk(el.shadowRoot);
if (!isInter(el) && !el.matches('a,button,input,textarea,select,summary,[role],[jsaction],[contenteditable]')) continue;
const label = nameOf(el).toLowerCase(); const label = nameOf(el).toLowerCase();
if (!label.includes(want)) continue; if (!label.includes(want)) continue;
const r = vis(el); if (!r) continue; const r = vis(el); if (!r) continue;
@@ -848,8 +964,9 @@ async function focusField(by) {
}; };
let best = null; let best = null;
function walk(root) { function walk(root) {
for (const el of root.querySelectorAll('input,textarea,select,[contenteditable="true"]')) { for (const el of root.querySelectorAll('*')) {
if (el.shadowRoot) walk(el.shadowRoot); if (el.shadowRoot) walk(el.shadowRoot);
if (!el.matches('input,textarea,select,[contenteditable="true"]')) continue;
if (el.type === 'hidden' || el.disabled) continue; if (el.type === 'hidden' || el.disabled) continue;
const hay = labelOf(el); const hay = labelOf(el);
if (!hay.includes(want)) continue; if (!hay.includes(want)) continue;
@@ -876,18 +993,30 @@ async function run(args) {
switch (action) { switch (action) {
case 'launch': { case 'launch': {
if (args.relaunch) { if (args.relaunch) {
// Take over a profile-owning Edge we do not control (or do): close all // Take over a profile-owning browser we do not control (or do): close
// msedge instances, then spawn fresh debuggable. Session restore brings // its instances, then spawn a fresh debuggable one. Session restore
// the user's tabs back on the reopened browser. // brings the user's tabs back on the reopened browser. Only the engine
// being relaunched is touched, and Firefox refuses outright because
// taskkill would close every Firefox window the user has open.
const relKind = args.executablePath ? kindOf(args.executablePath) : String(args.browser || cfgVal('browser', 'edge')).toLowerCase();
if (relKind === 'firefox') throw new Error('relaunch is not supported for Firefox: it would kill every Firefox window. Use an isolated profileDir, or close that Firefox yourself.');
try { session?.ws?.close(); } catch { /* gone */ } try { session?.ws?.close(); } catch { /* gone */ }
session = null; session = null;
await new Promise((res) => exec('taskkill /IM msedge.exe /F', () => res())); await new Promise((res) => exec(`taskkill /IM ${relKind === 'chrome' ? 'chrome.exe' : 'msedge.exe'} /F`, () => res()));
await new Promise((r) => setTimeout(r, 1800)); await new Promise((r) => setTimeout(r, 1800));
} }
return doLaunch(args); return doLaunch(args);
} }
case 'attach': { case 'attach': {
const port = effectivePort(args); const port = effectivePort(args);
// Firefox allows exactly one WebDriver BiDi session. Re-attaching to a
// browser this process already drives would open a second socket against
// that single session, fail the handshake, and strand the browser with no
// owner (neither relaunch nor close could reach it). Report the existing
// connection instead.
if (session && session.port === port && session.ws.readyState === 1) {
return { ok: true, attached: true, port, liveMode: !!session.attached, alreadyConnected: true, note: 'already connected to this browser in this process' };
}
await attachOnly(port); await attachOnly(port);
return { ok: true, attached: true, port, liveMode: true }; return { ok: true, attached: true, port, liveMode: true };
} }
@@ -960,7 +1089,18 @@ async function run(args) {
if (Number.isInteger(args.index)) { if (Number.isInteger(args.index)) {
const e = lastElements[args.index]; const e = lastElements[args.index];
if (!e) return { ok: false, error: `no element #${args.index} in the last see; call see first` }; if (!e) return { ok: false, error: `no element #${args.index} in the last see; call see first` };
point = { x: e.x, y: e.y, name: e.name }; // Re-resolve the element captured by the most recent see: scroll it into
// view and measure it again, so a scrolled or re-laid-out page cannot
// turn the click into a silent miss at a stale coordinate.
point = await evaluate(`(() => {
const list = window.__edgeKitElements || [];
const el = list[${Number(args.index)}];
if (!el || !el.isConnected) return null;
el.scrollIntoView({ block: 'center' });
const r = el.getBoundingClientRect();
return { x: r.x + r.width / 2, y: r.y + r.height / 2, name: (el.innerText || el.getAttribute('aria-label') || '').trim().slice(0, 60) };
})()`);
if (!point) { lastElements = []; return { ok: false, error: `element #${args.index} from the last see is gone; call see again` }; }
} else if (typeof args.text === 'string' && args.text) { } else if (typeof args.text === 'string' && args.text) {
point = await findByName(args.text); point = await findByName(args.text);
} else if (typeof args.selector === 'string' && args.selector) { } else if (typeof args.selector === 'string' && args.selector) {
@@ -1152,15 +1292,22 @@ async function run(args) {
case 'close': { case 'close': {
if (!session) return { ok: true, note: 'no active session' }; if (!session) return { ok: true, note: 'no active session' };
const { child, attached, ws } = session; const { child, attached, ws } = session;
session = null;
if (attached) { if (attached) {
// Firefox keeps its single BiDi session registered after a bare
// WebSocket close, so detach by ending that session first (the browser
// itself is borrowed and stays up).
try { if (session.engine === 'bidi' && session.bidiReady) await send('session.end', {}, null); } catch { /* best effort */ }
session = null;
try { ws.close(); } catch { /* already gone */ } try { ws.close(); } catch { /* already gone */ }
return { ok: true, detached: true, note: 'left the borrowed Edge running (live mode)' }; return { ok: true, detached: true, note: 'left the borrowed browser running (live mode)' };
} }
await Promise.race([ await Promise.race([
send('Browser.close', {}, null).catch(() => {}), send('Browser.close', {}, null).catch(() => {}),
new Promise((r) => setTimeout(r, 1500)), new Promise((r) => setTimeout(r, 1500)),
]); ]);
/* EDGE_KIT_NATIVE_CLOSE_FIX: clear the session only after Browser.close was sent; the
* upstream order cleared it first, so send() threw and close leaked the browser. */
session = null;
if (child && child.exitCode === null) child.kill(); if (child && child.exitCode === null) child.kill();
try { ws.close(); } catch { /* already gone */ } try { ws.close(); } catch { /* already gone */ }
return { ok: true, closed: true }; return { ok: true, closed: true };
@@ -1209,7 +1356,9 @@ async function withPage(navigateTo, extract, signal) {
} }
} }
let initLock = null; let initLock = null;
const PROBE_FILE = 'C:\\Users\\Administrator\\Documents\\deepseek-harness\\default-workspace\\edge-kit-seam-probe.json'; /* EDGE_KIT_NATIVE_PORTABILITY: keep the activation probe under the current user's
* home directory instead of a hardcoded developer path. */
const PROBE_FILE = path.join(os.homedir(), 'Documents', 'deepseek-harness', 'default-workspace', 'edge-kit-seam-probe.json');
let probed = false; let probed = false;
const FETCH_EXTRACT = `(async () => { const FETCH_EXTRACT = `(async () => {
@@ -1374,8 +1523,20 @@ export function apply(ctx, config) {
execute: async (args) => run(args), execute: async (args) => run(args),
}); });
return () => { return () => {
try { session?.child?.kill(); } catch { /* already gone */ } // Release the browser this plugin owns — or only the BiDi session when the
try { session?.ws?.close(); } catch { /* already gone */ } // browser is borrowed. Killing the spawn handle alone is not enough for
// Firefox (it is only the Windows launcher), so the browser could outlive
// the host with its single BiDi session still registered.
const ending = session;
try {
if (ending && ending.attached && ending.engine === 'bidi' && ending.bidiReady) {
ending.ws.send(JSON.stringify({ id: nextId++, method: 'session.end', params: {} }));
} else if (ending && !ending.attached && ending.ws.readyState === 1) {
ending.ws.send(JSON.stringify({ id: nextId++, method: ending.engine === 'bidi' ? 'browser.close' : 'Browser.close', params: {} }));
}
} catch { /* already gone */ }
try { ending?.child?.kill(); } catch { /* already gone */ }
try { ending?.ws?.close(); } catch { /* already gone */ }
session = null; session = null;
lastElements = []; lastElements = [];
cfg = null; cfg = null;