wherever-dev 0.6.2 → 0.7.1
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/dist/attach-file-tool.d.ts +20 -0
- package/dist/attach-file-tool.d.ts.map +1 -0
- package/dist/attach-file-tool.js +104 -0
- package/dist/attach-file-tool.js.map +1 -0
- package/dist/index.js +192 -15
- package/dist/index.js.map +1 -1
- package/dist/session-pool.d.ts +14 -0
- package/dist/session-pool.d.ts.map +1 -1
- package/dist/session-pool.js +8 -0
- package/dist/session-pool.js.map +1 -1
- package/package.json +4 -3
- package/public/_app/immutable/assets/0.BfBppMSw.css +1 -0
- package/public/_app/immutable/chunks/{DtEGJeyB.js → BeU21UaG.js} +2 -2
- package/public/_app/immutable/chunks/{DtEGJeyB.js.map → BeU21UaG.js.map} +1 -1
- package/public/_app/immutable/chunks/BoA88XJv.js +2 -0
- package/public/_app/immutable/chunks/{Dwvtln8S.js.map → BoA88XJv.js.map} +1 -1
- package/public/_app/immutable/chunks/{Bn3ighFw.js → ZdVCXQta.js} +2 -2
- package/public/_app/immutable/chunks/{Bn3ighFw.js.map → ZdVCXQta.js.map} +1 -1
- package/public/_app/immutable/entry/{app.5x6-QYdD.js → app.d0JJtJ_7.js} +3 -3
- package/public/_app/immutable/entry/{app.5x6-QYdD.js.map → app.d0JJtJ_7.js.map} +1 -1
- package/public/_app/immutable/entry/start.DPK88Y6d.js +2 -0
- package/public/_app/immutable/entry/start.DPK88Y6d.js.map +1 -0
- package/public/_app/immutable/nodes/{0.BaCrnkif.js → 0.DUyFn8Ez.js} +2 -2
- package/public/_app/immutable/nodes/{0.BaCrnkif.js.map → 0.DUyFn8Ez.js.map} +1 -1
- package/public/_app/immutable/nodes/{1.DvIJ6W8S.js → 1.BMhygv-V.js} +2 -2
- package/public/_app/immutable/nodes/{1.DvIJ6W8S.js.map → 1.BMhygv-V.js.map} +1 -1
- package/public/_app/immutable/nodes/2.C5REldbo.js +88 -0
- package/public/_app/immutable/nodes/2.C5REldbo.js.map +1 -0
- package/public/_app/version.json +1 -1
- package/public/index.html +11 -11
- package/public/service-worker.js +1 -1
- package/public/_app/immutable/assets/0.CbRAwd0M.css +0 -1
- package/public/_app/immutable/chunks/Dwvtln8S.js +0 -2
- package/public/_app/immutable/entry/start.CJPjsQpp.js +0 -2
- package/public/_app/immutable/entry/start.CJPjsQpp.js.map +0 -1
- package/public/_app/immutable/nodes/2.AnNiHhvN.js +0 -88
- package/public/_app/immutable/nodes/2.AnNiHhvN.js.map +0 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ToolDefinition } from '@earendil-works/pi-coding-agent';
|
|
2
|
+
/**
|
|
3
|
+
* Build the `attach_file` tool for a SERVER-SIDE agent session.
|
|
4
|
+
*
|
|
5
|
+
* This is the server-created-session counterpart of the identical tool the
|
|
6
|
+
* CLI-bridge extension registers. It is intentionally SELF-CONTAINED (like
|
|
7
|
+
* `read`): it only validates the requested path and returns a normal tool
|
|
8
|
+
* result carrying that path. It does NOT read the file bytes and emits no
|
|
9
|
+
* side-channel message.
|
|
10
|
+
*
|
|
11
|
+
* The download affordance is driven entirely by the tool CALL: every connected
|
|
12
|
+
* client already receives tool_start/tool_end for this session, and the web UI
|
|
13
|
+
* recognizes the `attach_file` tool name and renders a download button from its
|
|
14
|
+
* `path` argument (hitting GET /session/download). That is why the same tool
|
|
15
|
+
* works in a pure web-frontend session with no CLI bridge present.
|
|
16
|
+
*
|
|
17
|
+
* `cwd` is the session's working directory; relative paths resolve against it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createAttachFileTool(cwd: string): ToolDefinition;
|
|
20
|
+
//# sourceMappingURL=attach-file-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attach-file-tool.d.ts","sourceRoot":"","sources":["../src/attach-file-tool.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAEtE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAqFhE"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { Type } from 'typebox';
|
|
5
|
+
/**
|
|
6
|
+
* Build the `attach_file` tool for a SERVER-SIDE agent session.
|
|
7
|
+
*
|
|
8
|
+
* This is the server-created-session counterpart of the identical tool the
|
|
9
|
+
* CLI-bridge extension registers. It is intentionally SELF-CONTAINED (like
|
|
10
|
+
* `read`): it only validates the requested path and returns a normal tool
|
|
11
|
+
* result carrying that path. It does NOT read the file bytes and emits no
|
|
12
|
+
* side-channel message.
|
|
13
|
+
*
|
|
14
|
+
* The download affordance is driven entirely by the tool CALL: every connected
|
|
15
|
+
* client already receives tool_start/tool_end for this session, and the web UI
|
|
16
|
+
* recognizes the `attach_file` tool name and renders a download button from its
|
|
17
|
+
* `path` argument (hitting GET /session/download). That is why the same tool
|
|
18
|
+
* works in a pure web-frontend session with no CLI bridge present.
|
|
19
|
+
*
|
|
20
|
+
* `cwd` is the session's working directory; relative paths resolve against it.
|
|
21
|
+
*/
|
|
22
|
+
export function createAttachFileTool(cwd) {
|
|
23
|
+
return {
|
|
24
|
+
name: 'attach_file',
|
|
25
|
+
label: 'Attach File',
|
|
26
|
+
description: 'Attach a file to the conversation so the person on the other end (e.g. a ' +
|
|
27
|
+
'phone/browser) can download it with one tap. The user is remote and CANNOT ' +
|
|
28
|
+
'reach the local filesystem, so a file path in your reply is NOT enough: they ' +
|
|
29
|
+
'can only obtain a file if you attach it with this tool. Pass the path to a ' +
|
|
30
|
+
'real, existing file inside the working directory (or an absolute path the ' +
|
|
31
|
+
'server is allowed to serve). Use this both right after generating a ' +
|
|
32
|
+
'deliverable (a PDF, an export, a report) AND whenever the user asks for a ' +
|
|
33
|
+
"file by name or type, including one created earlier in the conversation, " +
|
|
34
|
+
"e.g. 'give me the gpx', 'send me the pdf', 'download the report', 'can I " +
|
|
35
|
+
"have that file'. Resolve which file they mean from the conversation and " +
|
|
36
|
+
'attach it; if truly ambiguous, ask which file, otherwise attach the most ' +
|
|
37
|
+
'likely match rather than only printing its path.',
|
|
38
|
+
promptSnippet: 'Attach a file to the conversation so the remote user can download it',
|
|
39
|
+
promptGuidelines: [
|
|
40
|
+
"Use attach_file whenever the user asks you to give/send/download/share a file, or asks for a file by name or type (e.g. 'give me the gpx', 'send the pdf'), including files created earlier in the conversation: the user is remote and can only download a file you attach, so never answer such a request with just a file path.",
|
|
41
|
+
'Use attach_file right after generating a downloadable deliverable (PDF, export, archive) so the remote user can retrieve it without asking.',
|
|
42
|
+
],
|
|
43
|
+
parameters: Type.Object({
|
|
44
|
+
path: Type.String({
|
|
45
|
+
description: 'Path to the file to attach. Relative paths resolve against the working directory.',
|
|
46
|
+
}),
|
|
47
|
+
}),
|
|
48
|
+
async execute(_toolCallId, params) {
|
|
49
|
+
const rawPath = String(params.path || '').trim();
|
|
50
|
+
if (!rawPath) {
|
|
51
|
+
return {
|
|
52
|
+
content: [{ type: 'text', text: 'attach_file: no path provided.' }],
|
|
53
|
+
details: undefined,
|
|
54
|
+
isError: true,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
let resolved = rawPath;
|
|
58
|
+
if (resolved.startsWith('~')) {
|
|
59
|
+
resolved = path.join(os.homedir(), resolved.slice(1));
|
|
60
|
+
}
|
|
61
|
+
else if (!path.isAbsolute(resolved)) {
|
|
62
|
+
resolved = path.resolve(cwd, resolved);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
resolved = path.resolve(resolved);
|
|
66
|
+
}
|
|
67
|
+
let size = null;
|
|
68
|
+
try {
|
|
69
|
+
const st = fs.statSync(resolved);
|
|
70
|
+
if (!st.isFile()) {
|
|
71
|
+
return {
|
|
72
|
+
content: [
|
|
73
|
+
{ type: 'text', text: `attach_file: not a regular file: ${resolved}` },
|
|
74
|
+
],
|
|
75
|
+
details: undefined,
|
|
76
|
+
isError: true,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
size = st.size;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return {
|
|
83
|
+
content: [
|
|
84
|
+
{ type: 'text', text: `attach_file: file not found: ${resolved}` },
|
|
85
|
+
],
|
|
86
|
+
details: undefined,
|
|
87
|
+
isError: true,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const filename = path.basename(resolved);
|
|
91
|
+
return {
|
|
92
|
+
content: [
|
|
93
|
+
{
|
|
94
|
+
type: 'text',
|
|
95
|
+
text: `Attached "${filename}" (${size} bytes) to the conversation. ` +
|
|
96
|
+
`A download button is now shown to the user for: ${resolved}`,
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
details: { path: resolved, filename, size },
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=attach-file-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attach-file-tool.js","sourceRoot":"","sources":["../src/attach-file-tool.ts"],"names":[],"mappings":"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"}
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,107 @@ const __dirname = path.dirname(__filename);
|
|
|
15
15
|
const devStaticPath = path.resolve(__dirname, '../../web/build');
|
|
16
16
|
const prodStaticPath = path.resolve(__dirname, '../public');
|
|
17
17
|
const staticDir = fs.existsSync(devStaticPath) ? devStaticPath : prodStaticPath;
|
|
18
|
+
const MIME_TYPES = {
|
|
19
|
+
'.html': 'text/html',
|
|
20
|
+
'.js': 'text/javascript',
|
|
21
|
+
'.css': 'text/css',
|
|
22
|
+
'.json': 'application/json',
|
|
23
|
+
'.png': 'image/png',
|
|
24
|
+
'.jpg': 'image/jpeg',
|
|
25
|
+
'.jpeg': 'image/jpeg',
|
|
26
|
+
'.gif': 'image/gif',
|
|
27
|
+
'.svg': 'image/svg+xml',
|
|
28
|
+
'.ico': 'image/x-icon',
|
|
29
|
+
'.wasm': 'application/wasm',
|
|
30
|
+
'.txt': 'text/plain',
|
|
31
|
+
'.md': 'text/markdown',
|
|
32
|
+
'.pdf': 'application/pdf',
|
|
33
|
+
'.csv': 'text/csv',
|
|
34
|
+
'.zip': 'application/zip',
|
|
35
|
+
'.webp': 'image/webp',
|
|
36
|
+
'.webmanifest': 'application/manifest+json'
|
|
37
|
+
};
|
|
38
|
+
function mimeTypeFor(filePath) {
|
|
39
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
40
|
+
return MIME_TYPES[ext] || 'application/octet-stream';
|
|
41
|
+
}
|
|
42
|
+
function expandTilde(p) {
|
|
43
|
+
if (p.startsWith('~'))
|
|
44
|
+
return path.join(os.homedir(), p.slice(1));
|
|
45
|
+
return p;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resolve the allowed download roots for a session. Deny-by-default: only files
|
|
49
|
+
* whose REAL path lives under one of these roots are served. Always includes the
|
|
50
|
+
* session cwd and the resolved upload dir; config.downloads.roots adds more.
|
|
51
|
+
* Roots are themselves realpath-resolved so a symlinked root still matches its
|
|
52
|
+
* realpath-resolved targets.
|
|
53
|
+
*/
|
|
54
|
+
function resolveDownloadRoots(config, cwd) {
|
|
55
|
+
const roots = new Set();
|
|
56
|
+
const add = (p) => {
|
|
57
|
+
if (!p)
|
|
58
|
+
return;
|
|
59
|
+
try {
|
|
60
|
+
roots.add(fs.realpathSync(path.resolve(expandTilde(p))));
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Non-existent root: keep the lexical resolution so a not-yet-created path
|
|
64
|
+
// under it can still be validated lexically as a fallback.
|
|
65
|
+
roots.add(path.resolve(expandTilde(p)));
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
if (cwd)
|
|
69
|
+
add(cwd);
|
|
70
|
+
add(resolveUploadDir(config, cwd));
|
|
71
|
+
for (const r of config.downloads?.roots || [])
|
|
72
|
+
add(r);
|
|
73
|
+
return Array.from(roots);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Validate a requested download path against the allowed roots and return the
|
|
77
|
+
* safe absolute path to stream, or null if it escapes / does not exist / is not
|
|
78
|
+
* a regular file. This is the security-critical guard: it realpath-resolves the
|
|
79
|
+
* target BEFORE the containment check so neither `..` traversal nor an in-tree
|
|
80
|
+
* symlink can point outside an allowed root.
|
|
81
|
+
*/
|
|
82
|
+
function resolveSafeDownloadPath(requested, cwd, roots) {
|
|
83
|
+
if (!requested)
|
|
84
|
+
return null;
|
|
85
|
+
let candidate = expandTilde(requested);
|
|
86
|
+
if (!path.isAbsolute(candidate)) {
|
|
87
|
+
if (!cwd)
|
|
88
|
+
return null;
|
|
89
|
+
candidate = path.resolve(cwd, candidate);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
candidate = path.resolve(candidate);
|
|
93
|
+
}
|
|
94
|
+
let real;
|
|
95
|
+
try {
|
|
96
|
+
real = fs.realpathSync(candidate);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return null; // does not exist / broken symlink
|
|
100
|
+
}
|
|
101
|
+
let stat;
|
|
102
|
+
try {
|
|
103
|
+
stat = fs.statSync(real);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
if (!stat.isFile())
|
|
109
|
+
return null; // never serve directories/devices
|
|
110
|
+
const withinRoot = roots.some((root) => {
|
|
111
|
+
if (real === root)
|
|
112
|
+
return true;
|
|
113
|
+
return real.startsWith(root.endsWith(path.sep) ? root : root + path.sep);
|
|
114
|
+
});
|
|
115
|
+
if (!withinRoot)
|
|
116
|
+
return null;
|
|
117
|
+
return real;
|
|
118
|
+
}
|
|
18
119
|
function serveStaticFile(reqPath, res) {
|
|
19
120
|
let filePath = path.join(staticDir, reqPath === '/' ? 'index.html' : reqPath);
|
|
20
121
|
if (!filePath.startsWith(staticDir)) {
|
|
@@ -29,21 +130,7 @@ function serveStaticFile(reqPath, res) {
|
|
|
29
130
|
filePath = path.join(staticDir, 'index.html');
|
|
30
131
|
}
|
|
31
132
|
const ext = path.extname(filePath).toLowerCase();
|
|
32
|
-
const
|
|
33
|
-
'.html': 'text/html',
|
|
34
|
-
'.js': 'text/javascript',
|
|
35
|
-
'.css': 'text/css',
|
|
36
|
-
'.json': 'application/json',
|
|
37
|
-
'.png': 'image/png',
|
|
38
|
-
'.jpg': 'image/jpeg',
|
|
39
|
-
'.gif': 'image/gif',
|
|
40
|
-
'.svg': 'image/svg+xml',
|
|
41
|
-
'.ico': 'image/x-icon',
|
|
42
|
-
'.wasm': 'application/wasm',
|
|
43
|
-
'.txt': 'text/plain',
|
|
44
|
-
'.webmanifest': 'application/manifest+json'
|
|
45
|
-
};
|
|
46
|
-
const contentType = mimeTypes[ext] || 'application/octet-stream';
|
|
133
|
+
const contentType = mimeTypeFor(filePath);
|
|
47
134
|
// Cache-Control: content-hashed build assets under /_app/immutable/ never
|
|
48
135
|
// change for a given URL, so they can be cached aggressively. Everything
|
|
49
136
|
// else (HTML app shell, the manifest, top-level icons) must stay revalidated
|
|
@@ -473,6 +560,7 @@ async function main() {
|
|
|
473
560
|
sendJSON(res, 200, {
|
|
474
561
|
gitInitDefault: !!config.gitInitDefault,
|
|
475
562
|
uploadMethod: config.uploads?.method || 'websocket',
|
|
563
|
+
downloadsEnabled: config.downloads?.enabled !== false,
|
|
476
564
|
searchFolder: searchFolder || null,
|
|
477
565
|
searchCreateRemote: !!config.searchCreateRemote,
|
|
478
566
|
searchDefaultModel
|
|
@@ -837,6 +925,64 @@ async function main() {
|
|
|
837
925
|
}
|
|
838
926
|
return;
|
|
839
927
|
}
|
|
928
|
+
if (pathname === '/session/download' && req.method === 'GET') {
|
|
929
|
+
const config = getWhereverConfig();
|
|
930
|
+
if (config.downloads?.enabled === false) {
|
|
931
|
+
sendJSON(res, 403, { error: 'Downloads are disabled on this server' });
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
const qSessionId = url.searchParams.get('sessionId') || '';
|
|
935
|
+
const qPath = url.searchParams.get('path') || '';
|
|
936
|
+
if (!qSessionId || !qPath) {
|
|
937
|
+
sendJSON(res, 400, { error: 'Missing sessionId or path' });
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
const tracked = sessionPool.getSession(qSessionId);
|
|
941
|
+
const cwd = tracked?.cwd;
|
|
942
|
+
const roots = resolveDownloadRoots(config, cwd);
|
|
943
|
+
const safePath = resolveSafeDownloadPath(qPath, cwd, roots);
|
|
944
|
+
if (!safePath) {
|
|
945
|
+
// 404 (not 403) so we do not leak whether a path outside the allowed
|
|
946
|
+
// roots exists: escape and not-found are indistinguishable to the client.
|
|
947
|
+
sendJSON(res, 404, { error: 'File not found or not allowed' });
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
let stat;
|
|
951
|
+
try {
|
|
952
|
+
stat = fs.statSync(safePath);
|
|
953
|
+
}
|
|
954
|
+
catch {
|
|
955
|
+
sendJSON(res, 404, { error: 'File not found' });
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
const maxBytes = config.downloads?.maxBytes ?? 100 * 1024 * 1024;
|
|
959
|
+
if (stat.size > maxBytes) {
|
|
960
|
+
sendJSON(res, 413, { error: `File too large (${stat.size} > ${maxBytes} bytes)` });
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
const filename = path.basename(safePath);
|
|
964
|
+
// RFC 5987 encoded filename for non-ASCII names, plus a plain ASCII
|
|
965
|
+
// fallback with quotes escaped.
|
|
966
|
+
const asciiName = filename.replace(/["\\]/g, '_').replace(/[^\x20-\x7e]/g, '_');
|
|
967
|
+
const encodedName = encodeURIComponent(filename);
|
|
968
|
+
res.writeHead(200, {
|
|
969
|
+
'Content-Type': mimeTypeFor(safePath),
|
|
970
|
+
'Content-Length': stat.size,
|
|
971
|
+
'Content-Disposition': `attachment; filename="${asciiName}"; filename*=UTF-8''${encodedName}`,
|
|
972
|
+
'Cache-Control': 'no-store',
|
|
973
|
+
});
|
|
974
|
+
const stream = fs.createReadStream(safePath);
|
|
975
|
+
stream.on('error', (err) => {
|
|
976
|
+
if (!res.headersSent) {
|
|
977
|
+
sendJSON(res, 500, { error: `Read error: ${err.message}` });
|
|
978
|
+
}
|
|
979
|
+
else {
|
|
980
|
+
res.destroy();
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
stream.pipe(res);
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
840
986
|
if (pathname === '/session/transcribe' && req.method === 'POST') {
|
|
841
987
|
const config = getWhereverConfig();
|
|
842
988
|
const apiKey = config.speech?.apiKey || process.env.SPEECH_API_KEY;
|
|
@@ -1152,6 +1298,11 @@ async function handleWSMessage(msg, client, pool, clients, broadcast, onSessions
|
|
|
1152
1298
|
cwd: msg.cwd,
|
|
1153
1299
|
model: msg.model || '',
|
|
1154
1300
|
isStreaming: pool.isStreaming(msg.sessionFile),
|
|
1301
|
+
// Include any context-usage the CLI bridge already reported (cached
|
|
1302
|
+
// from a prior turn) so a web viewer joining an idle CLI session gets
|
|
1303
|
+
// the "11.3% / 1.0M" indicator in the initial payload instead of a
|
|
1304
|
+
// blank until the next turn. undefined -> omitted -> client keeps null.
|
|
1305
|
+
contextUsage: pool.getContextUsage(msg.sessionFile) ?? null,
|
|
1155
1306
|
};
|
|
1156
1307
|
for (const c of clients.values()) {
|
|
1157
1308
|
if (c.sessionId === msg.sessionFile && !c.isCliBridge) {
|
|
@@ -1459,6 +1610,25 @@ async function handleWSMessage(msg, client, pool, clients, broadcast, onSessions
|
|
|
1459
1610
|
return;
|
|
1460
1611
|
if (client.readOnly)
|
|
1461
1612
|
return;
|
|
1613
|
+
// The client stamps every send with the sessionId of the session it is
|
|
1614
|
+
// actually viewing. Treat that as AUTHORITATIVE: if it does not resolve to
|
|
1615
|
+
// the same tracked session this connection is attached to, REFUSE rather
|
|
1616
|
+
// than delivering to the wrong agent. This closes a switch/reconnect/resync
|
|
1617
|
+
// race where client.sessionId (per-connection, set only when a load
|
|
1618
|
+
// attaches) is stale relative to the session the client painted and
|
|
1619
|
+
// targeted, which silently misrouted a message into another session's
|
|
1620
|
+
// agent. The client surfaces session_error as a recoverable, retryable
|
|
1621
|
+
// failure (delivery watchdog + Retry), so no text is lost.
|
|
1622
|
+
const attached = pool.getSession(client.sessionId);
|
|
1623
|
+
const target = msg.sessionId ? pool.getSession(msg.sessionId) : attached;
|
|
1624
|
+
if (!attached || !target || target !== attached) {
|
|
1625
|
+
sendWS(client.ws, {
|
|
1626
|
+
type: 'session_error',
|
|
1627
|
+
sessionId: msg.sessionId,
|
|
1628
|
+
error: 'This message was not delivered because the session changed. Re-open the session and resend.',
|
|
1629
|
+
});
|
|
1630
|
+
break;
|
|
1631
|
+
}
|
|
1462
1632
|
const streaming = pool.isStreaming(client.sessionId);
|
|
1463
1633
|
await pool.sendUserMessage(client.sessionId, msg.message, streaming ? 'steer' : undefined);
|
|
1464
1634
|
break;
|
|
@@ -1466,6 +1636,13 @@ async function handleWSMessage(msg, client, pool, clients, broadcast, onSessions
|
|
|
1466
1636
|
case 'abort': {
|
|
1467
1637
|
if (!client.sessionId)
|
|
1468
1638
|
return;
|
|
1639
|
+
// Same authority guard as 'message': never abort a session other than the
|
|
1640
|
+
// one the client is actually looking at. A stale client.sessionId must not
|
|
1641
|
+
// silently interrupt a different session's running turn.
|
|
1642
|
+
const attached = pool.getSession(client.sessionId);
|
|
1643
|
+
const target = msg.sessionId ? pool.getSession(msg.sessionId) : attached;
|
|
1644
|
+
if (!attached || !target || target !== attached)
|
|
1645
|
+
break;
|
|
1469
1646
|
await pool.abortSession(client.sessionId);
|
|
1470
1647
|
break;
|
|
1471
1648
|
}
|