Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(browser-extension): 共享 native host manifest 保留其他运行中 Cindy 的扩展 id

Dasheac504c6cb21
diff --git a/apps/desktop/src/main/browser-extension/__tests__/native-binding.test.ts b/apps/desktop/src/main/browser-extension/__tests__/native-binding.test.ts
index 015af5a88d..36d209169d 100644
--- a/apps/desktop/src/main/browser-extension/__tests__/native-binding.test.ts
+++ b/apps/desktop/src/main/browser-extension/__tests__/native-binding.test.ts
@@ -25,7 +25,11 @@ const EXT = 'a'.repeat(32);
const dirs: string[] = [];
const servers: net.Server[] = [];
afterEach(async () => {
- await Promise.all(servers.splice(0).map((server) => new Promise<void>((resolve) => server.close(() => resolve()))));
+ await Promise.all(
+ servers
+ .splice(0)
+ .map((server) => new Promise<void>((resolve) => server.close(() => resolve()))),
+ );
await Promise.all(dirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
});
@@ -70,7 +74,9 @@ describe('native-binding', () => {
expect(JSON.stringify(manifest)).not.toContain(n.token);
// Windows reports its own permission bits (0o666); owner-only modes are a POSIX property.
if (process.platform !== 'win32') {
- expect((await fs.stat(path.join(root, 'connections', '18819.json'))).mode & 0o777).toBe(0o600);
+ expect((await fs.stat(path.join(root, 'connections', '18819.json'))).mode & 0o777).toBe(
+ 0o600,
+ );
expect((await fs.stat(root)).mode & 0o777).toBe(0o700);
}
expect(await fs.readFile(path.join(root, 'cindy-browser-native'), 'utf8')).toBe(
@@ -107,6 +113,41 @@ describe('native-binding', () => {
await b.close();
});
+ // Review 2026-10-30 (Codex P2): the shared manifest keeps the sources of every running Cindy.
+ it('keeps the extension ids of other running Cindys in the shared manifest', async () => {
+ const { binaryDir, profile, root } = await fixture();
+ const opts = {
+ hostName: 'com.example.browser',
+ binaryDir,
+ root,
+ profiles: [profile],
+ platform: 'darwin' as const,
+ arch: 'arm64',
+ };
+ const listen = async () => {
+ const server = net.createServer();
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
+ servers.push(server);
+ return (server.address() as net.AddressInfo).port;
+ };
+ const other = 'b'.repeat(32);
+ const first = createNativeBinding(opts);
+ const second = createNativeBinding(opts);
+ await first.install(await listen(), [EXT]);
+ await second.install(await listen(), [other]);
+ const manifest = JSON.parse(
+ await fs.readFile(
+ path.join(profile, 'NativeMessagingHosts', 'com.example.browser.json'),
+ 'utf8',
+ ),
+ );
+ expect([...manifest.allowed_origins].sort()).toEqual(
+ ['chrome-extension://' + EXT + '/', 'chrome-extension://' + other + '/'].sort(),
+ );
+ await first.close();
+ await second.close();
+ });
+
// Review 2026-10-29: registrations were never withdrawn. Every Cindy on the account shares one,
// so it may only go once no running Cindy still serves a connection.
it('withdraws the browser registration only after the last running Cindy closes', async () => {
@@ -160,7 +201,10 @@ describe('native-binding', () => {
const deadPort = port === 65535 ? port - 1 : port + 1;
const live = createNativeBinding(opts);
await live.install(port, [EXT]);
- await fs.writeFile(path.join(root, 'connections', `${deadPort}.json`), JSON.stringify({ token: 'x'.repeat(64) }));
+ await fs.writeFile(
+ path.join(root, 'connections', `${deadPort}.json`),
+ JSON.stringify({ token: 'x'.repeat(64) }),
+ );
await live.close();
await expect(fs.access(registration)).rejects.toThrow();
await expect(fs.access(path.join(root, 'connections', `${deadPort}.json`))).rejects.toThrow();
diff --git a/apps/desktop/src/main/browser-extension/native-binding.ts b/apps/desktop/src/main/browser-extension/native-binding.ts
index bc07dc774e..591e2664cb 100644
--- a/apps/desktop/src/main/browser-extension/native-binding.ts
+++ b/apps/desktop/src/main/browser-extension/native-binding.ts
@@ -155,7 +155,11 @@ export function createNativeBinding(options: NativeBindingOptions): NativeBindin
for (const vendor of ['Google\\Chrome', 'Microsoft\\Edge', 'BraveSoftware\\Brave-Browser']) {
await run!(
reg,
- ['DELETE', 'HKCU\\Software\\' + vendor + '\\NativeMessagingHosts\\' + options.hostName, '/f'],
+ [
+ 'DELETE',
+ 'HKCU\\Software\\' + vendor + '\\NativeMessagingHosts\\' + options.hostName,
+ '/f',
+ ],
{ windowsHide: true, timeout: 10000 },
).catch(() => undefined);
}
@@ -176,6 +180,33 @@ export function createNativeBinding(options: NativeBindingOptions): NativeBindin
}
}
+ /**
+ * Extension ids of every running Cindy that shares this registration (release and dev builds, other
+ * plugin versions) plus our own: the shared manifest must keep all of their sources, or the last
+ * installer would cut the others' extensions off from the native host.
+ */
+ async function liveExtensionIds(ownPort: number, own: readonly string[]): Promise<string[]> {
+ const ids = new Set(own);
+ const dir = path.join(root, 'connections');
+ for (const name of (await fs.readdir(dir).catch(() => [])) as string[]) {
+ if (!name.endsWith('.json')) continue;
+ const port = Number(name.slice(0, -'.json'.length));
+ if (!Number.isInteger(port) || port <= 0 || port === ownPort || !(await portListening(port)))
+ continue;
+ try {
+ const saved = JSON.parse(await fs.readFile(path.join(dir, name), 'utf8')) as {
+ extensionIds?: unknown;
+ };
+ if (Array.isArray(saved.extensionIds))
+ for (const id of saved.extensionIds)
+ if (typeof id === 'string' && /^[a-p]{32}$/.test(id)) ids.add(id);
+ } catch {
+ // An unreadable file contributes nothing.
+ }
+ }
+ return [...ids];
+ }
+
async function install(port: number, extensionIds: readonly string[]) {
try {
await privateDirectory(root);
@@ -183,7 +214,7 @@ export function createNativeBinding(options: NativeBindingOptions): NativeBindin
// Claim the connection before touching the shared registration, so a binding closing at the
// same time (plugin update, re-approval, account switch) still sees this one in use.
config = path.join(root, 'connections', port + '.json');
- await atomic(config, JSON.stringify({ token }));
+ await atomic(config, JSON.stringify({ token, extensionIds: [...extensionIds] }));
const binaryName = 'cindy-browser-native' + (platform === 'win32' ? '.exe' : '');
const source = path.join(options.binaryDir, platform + '-' + arch, binaryName);
const binary = path.join(root, binaryName);
@@ -196,7 +227,9 @@ export function createNativeBinding(options: NativeBindingOptions): NativeBindin
description: 'Cindy browser automatic key binding',
path: binary,
type: 'stdio',
- allowed_origins: extensionIds.map((id) => 'chrome-extension://' + id + '/'),
+ allowed_origins: (await liveExtensionIds(port, extensionIds)).map(
+ (id) => 'chrome-extension://' + id + '/',
+ ),
};
if (platform === 'win32') {
const file = path.join(root, options.hostName + '.json');
@@ -248,7 +281,13 @@ export function createNativeBinding(options: NativeBindingOptions): NativeBindin
if (!config) return;
const data = await fs.readFile(config, 'utf8').catch(() => '');
// 只删自己写的授权;同端口被另一实例接管时保留它的令牌。
- if (data === JSON.stringify({ token })) await fs.rm(config, { force: true });
+ let saved: unknown;
+ try {
+ saved = (JSON.parse(data) as { token?: unknown }).token;
+ } catch {
+ saved = undefined;
+ }
+ if (saved === token) await fs.rm(config, { force: true });
config = null;
await unregisterIfUnused().catch(() => undefined);
}