@neuralnomads/codenomad 0.18.0 → 0.19.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 +12 -0
- package/THIRD_PARTY_NOTICES.md +26 -0
- package/dist/auth/manager.js +1 -1
- package/dist/background-processes/manager.js +15 -30
- package/dist/background-processes/manager.test.js +123 -0
- package/dist/config/schema.js +2 -1
- package/dist/config/schema.test.js +13 -0
- package/dist/events/bus.js +17 -0
- package/dist/events/bus.test.js +37 -0
- package/dist/filesystem/__tests__/search-cache.test.js +20 -8
- package/dist/filesystem/__tests__/search.test.js +53 -0
- package/dist/filesystem/search-cache.js +6 -10
- package/dist/filesystem/search.js +22 -9
- package/dist/index.js +77 -58
- package/dist/index.test.js +62 -0
- package/dist/opencode-plugin/codenomad-opencode-plugin.tgz +0 -0
- package/dist/opencode-update/service.js +150 -0
- package/dist/opencode-update/service.test.js +107 -0
- package/dist/permissions/auto-accept-manager.js +380 -0
- package/dist/permissions/auto-accept-manager.test.js +736 -0
- package/dist/permissions/auto-accept-store.js +118 -0
- package/dist/permissions/auto-accept-store.test.js +157 -0
- package/dist/permissions/opencode-replier.js +31 -0
- package/dist/permissions/opencode-yolo-metadata.js +81 -0
- package/dist/permissions/opencode-yolo-metadata.test.js +54 -0
- package/dist/server/__tests__/listener-retry.test.js +15 -0
- package/dist/server/__tests__/remote-proxy.test.js +151 -47
- package/dist/server/http-server.js +22 -9
- package/dist/server/remote-proxy.js +91 -34
- package/dist/server/routes/auth-pages/login.html +3 -3
- package/dist/server/routes/auth.js +1 -1
- package/dist/server/routes/auth.test.js +21 -0
- package/dist/server/routes/opencode-update.js +37 -0
- package/dist/server/routes/opencode-update.test.js +34 -0
- package/dist/server/routes/remote-servers.js +1 -1
- package/dist/server/routes/settings.js +48 -2
- package/dist/server/routes/settings.test.js +58 -0
- package/dist/server/routes/usage.js +18 -0
- package/dist/server/routes/usage.test.js +26 -0
- package/dist/server/routes/workspaces.js +34 -2
- package/dist/server/routes/workspaces.test.js +113 -0
- package/dist/server/routes/worktrees.js +19 -0
- package/dist/server/routes/yolo.js +13 -0
- package/dist/server/tls.js +2 -2
- package/dist/settings/binaries.js +4 -1
- package/dist/settings/binaries.test.js +19 -0
- package/dist/settings/public-config.js +14 -0
- package/dist/settings/public-config.test.js +56 -0
- package/dist/settings/service.js +17 -5
- package/dist/settings/service.test.js +51 -0
- package/dist/settings/yaml-doc-store.js +55 -4
- package/dist/settings/yaml-doc-store.test.js +78 -0
- package/dist/shutdown.js +83 -0
- package/dist/shutdown.test.js +148 -0
- package/dist/sidecars/manager.js +26 -3
- package/dist/sidecars/manager.test.js +42 -0
- package/dist/speech/service.js +121 -10
- package/dist/speech/service.test.js +247 -0
- package/dist/ui/remote-ui.js +2 -2
- package/dist/usage/providers/api-key.js +171 -0
- package/dist/usage/providers/google.js +171 -0
- package/dist/usage/providers/minimax.js +109 -0
- package/dist/usage/providers/oauth.js +76 -0
- package/dist/usage/providers/special.js +148 -0
- package/dist/usage/service.js +83 -0
- package/dist/usage/service.test.js +61 -0
- package/dist/usage/shared.js +130 -0
- package/dist/usage/shared.test.js +33 -0
- package/dist/usage/types.js +1 -0
- package/dist/workspaces/__tests__/spawn.test.js +64 -7
- package/dist/workspaces/__tests__/workspace-identity.test.js +163 -0
- package/dist/workspaces/binary-path.test.js +10 -0
- package/dist/workspaces/instance-client.js +42 -0
- package/dist/workspaces/instance-client.test.js +149 -0
- package/dist/workspaces/instance-events.js +2 -2
- package/dist/workspaces/launch-cleanup.test.js +29 -0
- package/dist/workspaces/loopback.js +8 -0
- package/dist/workspaces/manager.js +473 -128
- package/dist/workspaces/manager.test.js +335 -0
- package/dist/workspaces/process-identity.darwin.test.js +88 -0
- package/dist/workspaces/process-identity.js +449 -0
- package/dist/workspaces/process-identity.test.js +140 -0
- package/dist/workspaces/runtime.js +471 -138
- package/dist/workspaces/runtime.test.js +286 -0
- package/dist/workspaces/spawn.js +86 -15
- package/dist/workspaces/workspace-identity.js +27 -0
- package/package.json +2 -1
- package/public/assets/FilesTab-Cbs7b7y9.js +2 -0
- package/public/assets/{GitChangesTab-DyRStL3G.js → GitChangesTab-D3H4cqJJ.js} +2 -2
- package/public/assets/{SplitFilePanel-C4FztlTR.js → SplitFilePanel-C_Bcle0r.js} +1 -1
- package/public/assets/StatusTab-D1-tfkgN.js +1 -0
- package/public/assets/{align-justify-GNs_d9nT.js → align-justify-B5HUL_LR.js} +1 -1
- package/public/assets/{bundle-full-B0vc8NGv.js → bundle-full-CEFzES5w.js} +1 -1
- package/public/assets/{diff-viewer-BmSzmlPC.js → diff-viewer-BBC2_yI1.js} +1 -1
- package/public/assets/{git-diff-vendor-CSgooKT_.js → git-diff-vendor-CYS3ApzA.js} +1 -1
- package/public/assets/{index-ChDVvMF4.js → index-B81f0aWL.js} +1 -1
- package/public/assets/index-BHlYVddm.js +1 -0
- package/public/assets/index-Best16ZB.js +2 -0
- package/public/assets/index-Boq37ErX.js +1 -0
- package/public/assets/index-By9pgzQk.css +1 -0
- package/public/assets/index-C0caoeJg.js +1 -0
- package/public/assets/index-Dqg2UVvS.js +1 -0
- package/public/assets/index-it6EGJV6.js +1 -0
- package/public/assets/{index-Bv867yIC.js → index-lORR8OnW.js} +1 -1
- package/public/assets/index-ppe6yWaU.js +1 -0
- package/public/assets/index-wEqeBwqT.js +1 -0
- package/public/assets/index-yEcajWX_.js +1 -0
- package/public/assets/{loading-DRzViX8M.js → loading-CbUWFqQ_.js} +1 -1
- package/public/assets/main-DfSREDgt.js +69 -0
- package/public/assets/{markdown-BlxzERp2.js → markdown-CEtfpKK-.js} +60 -47
- package/public/assets/monaco-viewer-NKl8kwH2.js +26 -0
- package/public/assets/terminal-square-q-by1uT7.js +1 -0
- package/public/assets/tool-call-CZL2uqIM.js +59 -0
- package/public/assets/unified-picker-CvJXD-j8.js +1 -0
- package/public/assets/{wrap-text-CMFBVbBi.js → wrap-text-Bo7xr74P.js} +1 -1
- package/public/index.html +5 -5
- package/public/loading.html +5 -5
- package/public/sw.js +1 -1
- package/public/ui-version.json +1 -1
- package/public/assets/FilesTab-CFF9uJuc.js +0 -2
- package/public/assets/StatusTab-CcoPFya8.js +0 -1
- package/public/assets/index--bYE3fA1.js +0 -1
- package/public/assets/index-B-9c-wPM.css +0 -1
- package/public/assets/index-BDJqHo1E.js +0 -1
- package/public/assets/index-BEXDNmDA.js +0 -1
- package/public/assets/index-BWX6Cyx3.js +0 -2
- package/public/assets/index-CUB1cngT.js +0 -1
- package/public/assets/index-CyGPk9Qx.js +0 -1
- package/public/assets/index-D9T8sYiH.js +0 -1
- package/public/assets/index-DQ8RfkwK.js +0 -1
- package/public/assets/index-udaOwtFG.js +0 -1
- package/public/assets/main-DUP17Bqi.js +0 -68
- package/public/assets/monaco-viewer-BbohfXZk.js +0 -26
- package/public/assets/tool-call-B0Ri40qI.js +0 -59
- package/public/assets/unified-picker-CGg8mlSo.js +0 -1
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { AutoAcceptStore } from "./auto-accept-store";
|
|
2
|
+
const PERMISSION_ASK_TYPES = new Set(["permission.v2.asked", "permission.asked", "permission.updated"]);
|
|
3
|
+
const PERMISSION_REPLIED_TYPES = new Set(["permission.v2.replied", "permission.replied"]);
|
|
4
|
+
const SESSION_UPSERT_TYPES = new Set(["session.updated", "session.created"]);
|
|
5
|
+
const SESSION_REMOVE_TYPES = new Set(["session.deleted"]);
|
|
6
|
+
export class AutoAcceptManager {
|
|
7
|
+
constructor(deps) {
|
|
8
|
+
this.deps = deps;
|
|
9
|
+
this.store = new AutoAcceptStore();
|
|
10
|
+
/** instanceId:permissionId entries currently being replied, to dedupe re-emissions */
|
|
11
|
+
this.inFlight = new Set();
|
|
12
|
+
/** instanceId -> (permissionId -> pending permission) awaiting a reply */
|
|
13
|
+
this.pending = new Map();
|
|
14
|
+
/** instanceId:permissionId -> failure count, to stop retrying stuck permissions */
|
|
15
|
+
this.replyAttempts = new Map();
|
|
16
|
+
this.hydratedInstances = new Set();
|
|
17
|
+
this.hydration = new Map();
|
|
18
|
+
this.queuedEvents = new Map();
|
|
19
|
+
this.instanceGeneration = new Map();
|
|
20
|
+
this.sessionWorkspaces = new Map();
|
|
21
|
+
this.mutations = new Map();
|
|
22
|
+
}
|
|
23
|
+
start() {
|
|
24
|
+
if (this.unsubscribe)
|
|
25
|
+
return;
|
|
26
|
+
const handler = (payload) => {
|
|
27
|
+
if (!payload || !payload.instanceId || !payload.event)
|
|
28
|
+
return;
|
|
29
|
+
if (this.deps.persistence && !this.hydratedInstances.has(payload.instanceId)) {
|
|
30
|
+
const queued = this.queuedEvents.get(payload.instanceId) ?? [];
|
|
31
|
+
queued.push(payload.event);
|
|
32
|
+
this.queuedEvents.set(payload.instanceId, queued);
|
|
33
|
+
void this.hydrateInstance(payload.instanceId).catch((error) => {
|
|
34
|
+
this.deps.logger.warn({ instanceId: payload.instanceId, err: error }, "Failed to hydrate persisted Yolo state");
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
this.handleInstanceEvent(payload.instanceId, payload.event);
|
|
39
|
+
};
|
|
40
|
+
const onStarted = (event) => {
|
|
41
|
+
const instanceId = event.workspace?.id;
|
|
42
|
+
if (!instanceId)
|
|
43
|
+
return;
|
|
44
|
+
void this.hydrateInstance(instanceId).catch((error) => {
|
|
45
|
+
this.deps.logger.warn({ instanceId, err: error }, "Failed to hydrate persisted Yolo state");
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
const onStopped = (event) => {
|
|
49
|
+
if (event?.workspaceId)
|
|
50
|
+
this.clearInstance(event.workspaceId);
|
|
51
|
+
};
|
|
52
|
+
const onError = (event) => {
|
|
53
|
+
if (event?.workspace?.id)
|
|
54
|
+
this.clearInstance(event.workspace.id);
|
|
55
|
+
};
|
|
56
|
+
this.deps.eventBus.on("instance.event", handler);
|
|
57
|
+
this.deps.eventBus.on("workspace.started", onStarted);
|
|
58
|
+
this.deps.eventBus.on("workspace.stopped", onStopped);
|
|
59
|
+
this.deps.eventBus.on("workspace.error", onError);
|
|
60
|
+
this.unsubscribe = () => {
|
|
61
|
+
this.deps.eventBus.off("instance.event", handler);
|
|
62
|
+
this.deps.eventBus.off("workspace.started", onStarted);
|
|
63
|
+
this.deps.eventBus.off("workspace.stopped", onStopped);
|
|
64
|
+
this.deps.eventBus.off("workspace.error", onError);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
stop() {
|
|
68
|
+
this.unsubscribe?.();
|
|
69
|
+
this.unsubscribe = undefined;
|
|
70
|
+
}
|
|
71
|
+
isEnabled(instanceId, sessionId) {
|
|
72
|
+
return this.store.isEnabled(instanceId, sessionId);
|
|
73
|
+
}
|
|
74
|
+
hydrateInstance(instanceId) {
|
|
75
|
+
if (!this.deps.persistence || this.hydratedInstances.has(instanceId))
|
|
76
|
+
return Promise.resolve();
|
|
77
|
+
const existing = this.hydration.get(instanceId);
|
|
78
|
+
if (existing)
|
|
79
|
+
return existing;
|
|
80
|
+
const generation = this.instanceGeneration.get(instanceId) ?? 0;
|
|
81
|
+
let hydrated = false;
|
|
82
|
+
const pending = this.deps.persistence.loadSessions(instanceId).then((sessions) => {
|
|
83
|
+
if ((this.instanceGeneration.get(instanceId) ?? 0) !== generation)
|
|
84
|
+
return;
|
|
85
|
+
this.store.clearInstance(instanceId);
|
|
86
|
+
const workspaces = new Map();
|
|
87
|
+
for (const session of sessions) {
|
|
88
|
+
this.store.upsertSession(instanceId, session);
|
|
89
|
+
if (session.workspaceId)
|
|
90
|
+
workspaces.set(session.id, session.workspaceId);
|
|
91
|
+
}
|
|
92
|
+
this.sessionWorkspaces.set(instanceId, workspaces);
|
|
93
|
+
for (const session of sessions) {
|
|
94
|
+
if (!session.yoloEnabled || this.store.familyRoot(instanceId, session.id) !== session.id)
|
|
95
|
+
continue;
|
|
96
|
+
this.store.setEnabled(instanceId, session.id, true);
|
|
97
|
+
this.deps.eventBus.publish({ type: "yolo.stateChanged", instanceId, sessionId: session.id, enabled: true });
|
|
98
|
+
this.drainPending(instanceId, session.id);
|
|
99
|
+
}
|
|
100
|
+
this.hydratedInstances.add(instanceId);
|
|
101
|
+
hydrated = true;
|
|
102
|
+
}).finally(() => {
|
|
103
|
+
if ((this.instanceGeneration.get(instanceId) ?? 0) !== generation)
|
|
104
|
+
return;
|
|
105
|
+
this.hydration.delete(instanceId);
|
|
106
|
+
if (!hydrated)
|
|
107
|
+
return;
|
|
108
|
+
const queued = this.queuedEvents.get(instanceId) ?? [];
|
|
109
|
+
this.queuedEvents.delete(instanceId);
|
|
110
|
+
for (const event of queued)
|
|
111
|
+
this.handleInstanceEvent(instanceId, event);
|
|
112
|
+
});
|
|
113
|
+
this.hydration.set(instanceId, pending);
|
|
114
|
+
return pending;
|
|
115
|
+
}
|
|
116
|
+
toggle(instanceId, sessionId) {
|
|
117
|
+
if (this.deps.persistence)
|
|
118
|
+
return this.togglePersisted(instanceId, sessionId);
|
|
119
|
+
const enabled = this.store.toggle(instanceId, sessionId);
|
|
120
|
+
this.deps.eventBus.publish({ type: "yolo.stateChanged", instanceId, sessionId, enabled });
|
|
121
|
+
if (enabled) {
|
|
122
|
+
this.drainPending(instanceId, sessionId);
|
|
123
|
+
}
|
|
124
|
+
return enabled;
|
|
125
|
+
}
|
|
126
|
+
async togglePersisted(instanceId, sessionId) {
|
|
127
|
+
await this.hydrateInstance(instanceId);
|
|
128
|
+
const generation = this.instanceGeneration.get(instanceId) ?? 0;
|
|
129
|
+
const mutation = (this.mutations.get(instanceId) ?? Promise.resolve(false)).catch(() => false).then(async () => {
|
|
130
|
+
if ((this.instanceGeneration.get(instanceId) ?? 0) !== generation) {
|
|
131
|
+
return this.store.isEnabled(instanceId, sessionId);
|
|
132
|
+
}
|
|
133
|
+
const rootSessionId = this.store.familyRoot(instanceId, sessionId);
|
|
134
|
+
const traversedRootSessionIds = new Set([rootSessionId]);
|
|
135
|
+
const enabled = !this.store.isEnabled(instanceId, rootSessionId);
|
|
136
|
+
await this.deps.persistence.persist(instanceId, rootSessionId, enabled, this.sessionWorkspaces.get(instanceId)?.get(rootSessionId));
|
|
137
|
+
if ((this.instanceGeneration.get(instanceId) ?? 0) !== generation) {
|
|
138
|
+
return this.store.isEnabled(instanceId, rootSessionId);
|
|
139
|
+
}
|
|
140
|
+
let persistedRootSessionId = rootSessionId;
|
|
141
|
+
let currentRootSessionId = this.store.familyRoot(instanceId, sessionId);
|
|
142
|
+
while (currentRootSessionId !== persistedRootSessionId) {
|
|
143
|
+
traversedRootSessionIds.add(currentRootSessionId);
|
|
144
|
+
await this.deps.persistence.persist(instanceId, currentRootSessionId, enabled, this.sessionWorkspaces.get(instanceId)?.get(currentRootSessionId));
|
|
145
|
+
if (enabled) {
|
|
146
|
+
await this.deps.persistence.persist(instanceId, persistedRootSessionId, false, this.sessionWorkspaces.get(instanceId)?.get(persistedRootSessionId));
|
|
147
|
+
}
|
|
148
|
+
persistedRootSessionId = currentRootSessionId;
|
|
149
|
+
currentRootSessionId = this.store.familyRoot(instanceId, sessionId);
|
|
150
|
+
}
|
|
151
|
+
if ((this.instanceGeneration.get(instanceId) ?? 0) !== generation) {
|
|
152
|
+
return this.store.isEnabled(instanceId, currentRootSessionId);
|
|
153
|
+
}
|
|
154
|
+
if (enabled) {
|
|
155
|
+
this.store.setEnabled(instanceId, currentRootSessionId, true);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
for (const traversedRootSessionId of traversedRootSessionIds) {
|
|
159
|
+
this.store.setEnabled(instanceId, traversedRootSessionId, false);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
this.deps.eventBus.publish({ type: "yolo.stateChanged", instanceId, sessionId: currentRootSessionId, enabled });
|
|
163
|
+
if (enabled)
|
|
164
|
+
this.drainPending(instanceId, currentRootSessionId);
|
|
165
|
+
return enabled;
|
|
166
|
+
});
|
|
167
|
+
const settled = mutation.finally(() => {
|
|
168
|
+
if (this.mutations.get(instanceId) === settled)
|
|
169
|
+
this.mutations.delete(instanceId);
|
|
170
|
+
});
|
|
171
|
+
this.mutations.set(instanceId, settled);
|
|
172
|
+
return settled;
|
|
173
|
+
}
|
|
174
|
+
clearInstance(instanceId) {
|
|
175
|
+
this.instanceGeneration.set(instanceId, (this.instanceGeneration.get(instanceId) ?? 0) + 1);
|
|
176
|
+
this.hydratedInstances.delete(instanceId);
|
|
177
|
+
this.hydration.delete(instanceId);
|
|
178
|
+
this.queuedEvents.delete(instanceId);
|
|
179
|
+
this.sessionWorkspaces.delete(instanceId);
|
|
180
|
+
this.mutations.delete(instanceId);
|
|
181
|
+
this.store.clearInstance(instanceId);
|
|
182
|
+
this.pending.delete(instanceId);
|
|
183
|
+
const prefix = `${instanceId}:`;
|
|
184
|
+
for (const key of Array.from(this.inFlight.keys())) {
|
|
185
|
+
if (key.startsWith(prefix))
|
|
186
|
+
this.inFlight.delete(key);
|
|
187
|
+
}
|
|
188
|
+
for (const key of Array.from(this.replyAttempts.keys())) {
|
|
189
|
+
if (key.startsWith(prefix))
|
|
190
|
+
this.replyAttempts.delete(key);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
handleInstanceEvent(instanceId, event) {
|
|
194
|
+
if (!event || typeof event.type !== "string")
|
|
195
|
+
return;
|
|
196
|
+
if (SESSION_UPSERT_TYPES.has(event.type)) {
|
|
197
|
+
this.ingestSession(instanceId, event.properties);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (SESSION_REMOVE_TYPES.has(event.type)) {
|
|
201
|
+
const info = event.properties?.info;
|
|
202
|
+
const id = readString(info?.id) ?? readString(event.properties?.id);
|
|
203
|
+
if (id) {
|
|
204
|
+
this.store.removeSession(instanceId, id);
|
|
205
|
+
this.removePendingForSession(instanceId, id);
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (PERMISSION_REPLIED_TYPES.has(event.type)) {
|
|
210
|
+
this.handlePermissionReplied(instanceId, event.properties);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (PERMISSION_ASK_TYPES.has(event.type)) {
|
|
214
|
+
this.handlePermissionRequest(instanceId, event.type, event.properties);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
ingestSession(instanceId, properties) {
|
|
218
|
+
// OpenCode wraps session records under `properties.info` for
|
|
219
|
+
// session.created/updated/deleted (see SDK EventSessionUpdated). Accept a
|
|
220
|
+
// flat fallback only for defensive compatibility.
|
|
221
|
+
const info = properties;
|
|
222
|
+
const session = (info && typeof info === "object" && "info" in info ? info.info : info);
|
|
223
|
+
if (!session || typeof session.id !== "string")
|
|
224
|
+
return;
|
|
225
|
+
const parentId = session.parentID ?? session.parentId ?? null;
|
|
226
|
+
const revert = session.revert ?? undefined;
|
|
227
|
+
const enabledBefore = this.store.enabledRoots(instanceId);
|
|
228
|
+
this.store.upsertSession(instanceId, { id: session.id, parentId, revert });
|
|
229
|
+
if (typeof session.workspaceID === "string" && session.workspaceID) {
|
|
230
|
+
const workspaces = this.sessionWorkspaces.get(instanceId) ?? new Map();
|
|
231
|
+
workspaces.set(session.id, session.workspaceID);
|
|
232
|
+
this.sessionWorkspaces.set(instanceId, workspaces);
|
|
233
|
+
}
|
|
234
|
+
this.persistRootMigration(instanceId, enabledBefore, this.store.enabledRoots(instanceId));
|
|
235
|
+
// Session ancestry may have changed (parent discovered, revert toggled).
|
|
236
|
+
// Re-drain pending permissions whose family root may have migrated into
|
|
237
|
+
// an enabled family — mirrors the old UI's drainAutoAcceptPermissions-
|
|
238
|
+
// ForInstance trigger on session.updated (#497).
|
|
239
|
+
this.drainPending(instanceId, session.id);
|
|
240
|
+
}
|
|
241
|
+
persistRootMigration(instanceId, before, after) {
|
|
242
|
+
if (!this.deps.persistence)
|
|
243
|
+
return;
|
|
244
|
+
const removed = before.filter((id) => !after.includes(id));
|
|
245
|
+
const added = after.filter((id) => !before.includes(id));
|
|
246
|
+
if (removed.length === 0 && added.length === 0)
|
|
247
|
+
return;
|
|
248
|
+
const generation = this.instanceGeneration.get(instanceId) ?? 0;
|
|
249
|
+
const mutation = (this.mutations.get(instanceId) ?? Promise.resolve(false)).catch(() => false).then(async () => {
|
|
250
|
+
if ((this.instanceGeneration.get(instanceId) ?? 0) !== generation)
|
|
251
|
+
return false;
|
|
252
|
+
const enabledRoots = new Set(this.store.enabledRoots(instanceId));
|
|
253
|
+
for (const rootSessionId of added) {
|
|
254
|
+
if (!enabledRoots.has(rootSessionId))
|
|
255
|
+
continue;
|
|
256
|
+
await this.deps.persistence.persist(instanceId, rootSessionId, true, this.sessionWorkspaces.get(instanceId)?.get(rootSessionId));
|
|
257
|
+
}
|
|
258
|
+
for (const rootSessionId of removed) {
|
|
259
|
+
if (enabledRoots.has(rootSessionId))
|
|
260
|
+
continue;
|
|
261
|
+
if ((this.instanceGeneration.get(instanceId) ?? 0) !== generation)
|
|
262
|
+
return false;
|
|
263
|
+
await this.deps.persistence.persist(instanceId, rootSessionId, false, this.sessionWorkspaces.get(instanceId)?.get(rootSessionId));
|
|
264
|
+
}
|
|
265
|
+
return false;
|
|
266
|
+
});
|
|
267
|
+
const settled = mutation.finally(() => {
|
|
268
|
+
if (this.mutations.get(instanceId) === settled)
|
|
269
|
+
this.mutations.delete(instanceId);
|
|
270
|
+
});
|
|
271
|
+
this.mutations.set(instanceId, settled);
|
|
272
|
+
void settled.catch((error) => {
|
|
273
|
+
this.deps.logger.warn({ instanceId, err: error }, "Failed to migrate persisted Yolo family root");
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
handlePermissionRequest(instanceId, eventType, permission) {
|
|
277
|
+
const request = permission;
|
|
278
|
+
if (!request)
|
|
279
|
+
return;
|
|
280
|
+
const permissionId = readString(request.id);
|
|
281
|
+
const sessionId = readString(request.sessionID) ?? readString(request.sessionId);
|
|
282
|
+
if (!permissionId || !sessionId)
|
|
283
|
+
return;
|
|
284
|
+
// Infer source from the event type, but prefer the already-tracked source
|
|
285
|
+
// for permission.updated (which may belong to a v2 permission).
|
|
286
|
+
const existing = this.pending.get(instanceId)?.get(permissionId);
|
|
287
|
+
const source = eventType === "permission.v2.asked" ? "v2" : (existing?.source ?? "legacy");
|
|
288
|
+
// `permission.updated` represents a detail change for a permission that
|
|
289
|
+
// is *already* pending. If it is no longer in our pending set it was
|
|
290
|
+
// already replied to (by us or the user) — skip to avoid a duplicate reply.
|
|
291
|
+
if (eventType === "permission.updated" && !this.pending.get(instanceId)?.has(permissionId)) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
this.addPending(instanceId, { permissionId, sessionId, source });
|
|
295
|
+
if (!this.store.isEnabled(instanceId, sessionId))
|
|
296
|
+
return;
|
|
297
|
+
this.tryAutoAccept(instanceId, permissionId, sessionId, source);
|
|
298
|
+
}
|
|
299
|
+
handlePermissionReplied(instanceId, properties) {
|
|
300
|
+
const request = properties ?? {};
|
|
301
|
+
const permissionId = readString(request.id) ??
|
|
302
|
+
readString(request.requestID) ??
|
|
303
|
+
readString(request.permissionID) ??
|
|
304
|
+
readString(request.requestId) ??
|
|
305
|
+
readString(request.permissionId);
|
|
306
|
+
if (permissionId)
|
|
307
|
+
this.removePending(instanceId, permissionId);
|
|
308
|
+
}
|
|
309
|
+
tryAutoAccept(instanceId, permissionId, sessionId, source) {
|
|
310
|
+
const key = `${instanceId}:${permissionId}`;
|
|
311
|
+
if (this.inFlight.has(key))
|
|
312
|
+
return;
|
|
313
|
+
const attempts = this.replyAttempts.get(key) ?? 0;
|
|
314
|
+
if (attempts >= AutoAcceptManager.MAX_REPLY_ATTEMPTS)
|
|
315
|
+
return;
|
|
316
|
+
this.inFlight.add(key);
|
|
317
|
+
this.replyAttempts.set(key, attempts + 1);
|
|
318
|
+
const reply = { instanceId, permissionId, sessionId, source, reply: "once" };
|
|
319
|
+
void this.deps.replier(reply)
|
|
320
|
+
.then(() => {
|
|
321
|
+
this.replyAttempts.delete(key);
|
|
322
|
+
this.removePending(instanceId, permissionId);
|
|
323
|
+
this.deps.eventBus.publish({ type: "yolo.autoAccepted", instanceId, sessionId, permissionId });
|
|
324
|
+
})
|
|
325
|
+
.catch((error) => {
|
|
326
|
+
this.deps.logger.error({ instanceId, permissionId, err: error, attempt: attempts + 1 }, "Yolo auto-accept reply failed");
|
|
327
|
+
if (attempts + 1 >= AutoAcceptManager.MAX_REPLY_ATTEMPTS) {
|
|
328
|
+
this.removePending(instanceId, permissionId);
|
|
329
|
+
}
|
|
330
|
+
})
|
|
331
|
+
.finally(() => {
|
|
332
|
+
this.inFlight.delete(key);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
/** Auto-accept all pending permissions belonging to the same family root. */
|
|
336
|
+
drainPending(instanceId, sessionId) {
|
|
337
|
+
const instancePending = this.pending.get(instanceId);
|
|
338
|
+
if (!instancePending || instancePending.size === 0)
|
|
339
|
+
return;
|
|
340
|
+
const root = this.store.familyRoot(instanceId, sessionId);
|
|
341
|
+
for (const entry of Array.from(instancePending.values())) {
|
|
342
|
+
if (this.store.familyRoot(instanceId, entry.sessionId) === root) {
|
|
343
|
+
this.tryAutoAccept(instanceId, entry.permissionId, entry.sessionId, entry.source);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
addPending(instanceId, entry) {
|
|
348
|
+
let instancePending = this.pending.get(instanceId);
|
|
349
|
+
if (!instancePending) {
|
|
350
|
+
instancePending = new Map();
|
|
351
|
+
this.pending.set(instanceId, instancePending);
|
|
352
|
+
}
|
|
353
|
+
instancePending.set(entry.permissionId, entry);
|
|
354
|
+
}
|
|
355
|
+
removePending(instanceId, permissionId) {
|
|
356
|
+
const instancePending = this.pending.get(instanceId);
|
|
357
|
+
if (instancePending?.delete(permissionId)) {
|
|
358
|
+
this.replyAttempts.delete(`${instanceId}:${permissionId}`);
|
|
359
|
+
if (instancePending.size === 0)
|
|
360
|
+
this.pending.delete(instanceId);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
removePendingForSession(instanceId, sessionId) {
|
|
364
|
+
const instancePending = this.pending.get(instanceId);
|
|
365
|
+
if (!instancePending)
|
|
366
|
+
return;
|
|
367
|
+
for (const [permId, entry] of Array.from(instancePending)) {
|
|
368
|
+
if (entry.sessionId === sessionId) {
|
|
369
|
+
instancePending.delete(permId);
|
|
370
|
+
this.replyAttempts.delete(`${instanceId}:${permId}`);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (instancePending.size === 0)
|
|
374
|
+
this.pending.delete(instanceId);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
AutoAcceptManager.MAX_REPLY_ATTEMPTS = 3;
|
|
378
|
+
function readString(value) {
|
|
379
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
380
|
+
}
|