wherever-dev 0.21.0 → 0.21.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/CHANGELOG.md +6 -0
- package/dist/attach-file-tool.d.ts +20 -1
- package/dist/attach-file-tool.d.ts.map +1 -1
- package/dist/attach-file-tool.js +45 -3
- package/dist/attach-file-tool.js.map +1 -1
- package/dist/download-grants.d.ts +12 -0
- package/dist/download-grants.d.ts.map +1 -0
- package/dist/download-grants.js +76 -0
- package/dist/download-grants.js.map +1 -0
- package/dist/index.js +35 -13
- package/dist/index.js.map +1 -1
- package/dist/session-pool.d.ts +11 -0
- package/dist/session-pool.d.ts.map +1 -1
- package/dist/session-pool.js +19 -1
- package/dist/session-pool.js.map +1 -1
- package/package.json +1 -1
- package/public/_app/immutable/chunks/{idnZrCTB.js → BQ9B_G4h.js} +2 -2
- package/public/_app/immutable/chunks/{idnZrCTB.js.map → BQ9B_G4h.js.map} +1 -1
- package/public/_app/immutable/chunks/{DJMI34rR.js → BUONgvl4.js} +2 -2
- package/public/_app/immutable/chunks/{DJMI34rR.js.map → BUONgvl4.js.map} +1 -1
- package/public/_app/immutable/chunks/{CkzyFwPn.js → DauELski.js} +2 -2
- package/public/_app/immutable/chunks/{CkzyFwPn.js.map → DauELski.js.map} +1 -1
- package/public/_app/immutable/entry/{app.CF_gzWKW.js → app.BZO7Xckk.js} +3 -3
- package/public/_app/immutable/entry/{app.CF_gzWKW.js.map → app.BZO7Xckk.js.map} +1 -1
- package/public/_app/immutable/entry/start.DWD60uNu.js +2 -0
- package/public/_app/immutable/entry/start.DWD60uNu.js.map +1 -0
- package/public/_app/immutable/nodes/{0.CIIhZFWl.js → 0.Czz2XmZU.js} +2 -2
- package/public/_app/immutable/nodes/{0.CIIhZFWl.js.map → 0.Czz2XmZU.js.map} +1 -1
- package/public/_app/immutable/nodes/{1.D4WVeMej.js → 1.DDvrriUW.js} +2 -2
- package/public/_app/immutable/nodes/{1.D4WVeMej.js.map → 1.DDvrriUW.js.map} +1 -1
- package/public/_app/immutable/nodes/{2._KcrsFWH.js → 2.C7E8EMKX.js} +2 -2
- package/public/_app/immutable/nodes/{2._KcrsFWH.js.map → 2.C7E8EMKX.js.map} +1 -1
- package/public/_app/version.json +1 -1
- package/public/index.html +10 -10
- package/public/service-worker.js +1 -1
- package/public/_app/immutable/entry/start.GpPJ13Gr.js +0 -2
- package/public/_app/immutable/entry/start.GpPJ13Gr.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# wherever-dev
|
|
2
2
|
|
|
3
|
+
## 0.21.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- b2e8019: Fix `attach_file` downloads that 404ed when the file lived outside the session cwd (e.g. a PDF built in a sibling project). A successful `attach_file` call is now a download grant for exactly that file, read from the session transcript so it survives restarts and covers CLI-bridge sessions. `GET /session/download` also resolves sessions the server has not loaded (cwd + grants from disk). As a fallback, `attach_file` now errors with an actionable message when the server would refuse the download anyway (downloads disabled, or the file over `downloads.maxBytes`).
|
|
8
|
+
|
|
3
9
|
## 0.21.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
|
@@ -15,6 +15,25 @@ import type { ToolDefinition } from '@earendil-works/pi-coding-agent';
|
|
|
15
15
|
* works in a pure web-frontend session with no CLI bridge present.
|
|
16
16
|
*
|
|
17
17
|
* `cwd` is the session's working directory; relative paths resolve against it.
|
|
18
|
+
*
|
|
19
|
+
* Serving: a successful call is itself the download grant (download-grants.ts),
|
|
20
|
+
* so any existing file the agent attaches is downloadable, inside the cwd or not.
|
|
21
|
+
* The remaining refusals are server-wide policy, which `downloadsConfig` exposes
|
|
22
|
+
* so the tool can FAIL LOUDLY instead of showing a button that can only error:
|
|
23
|
+
* downloads disabled, or the file over `downloads.maxBytes`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createAttachFileTool(cwd: string, downloadsConfig?: () => DownloadsPolicy | undefined): ToolDefinition;
|
|
26
|
+
/** The slice of `WhereverConfig.downloads` the tool needs to predict a refusal. */
|
|
27
|
+
export interface DownloadsPolicy {
|
|
28
|
+
enabled?: boolean;
|
|
29
|
+
maxBytes?: number;
|
|
30
|
+
}
|
|
31
|
+
/** Default `downloads.maxBytes`, kept in step with GET /session/download. */
|
|
32
|
+
export declare const DEFAULT_DOWNLOAD_MAX_BYTES: number;
|
|
33
|
+
/**
|
|
34
|
+
* Why GET /session/download would refuse this file, as an actionable tool error,
|
|
35
|
+
* or null when it will be served. Mirrors the endpoint's policy checks so the
|
|
36
|
+
* agent learns at attach time rather than the user at tap time.
|
|
18
37
|
*/
|
|
19
|
-
export declare function
|
|
38
|
+
export declare function downloadRefusal(policy: DownloadsPolicy | undefined, size: number, resolved: string): string | null;
|
|
20
39
|
//# sourceMappingURL=attach-file-tool.d.ts.map
|
|
@@ -1 +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
|
|
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;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,MAAM,EACX,eAAe,GAAE,MAAM,eAAe,GAAG,SAA2B,GACnE,cAAc,CAqGhB;AAED,mFAAmF;AACnF,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,6EAA6E;AAC7E,eAAO,MAAM,0BAA0B,QAAoB,CAAC;AAE5D;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,eAAe,GAAG,SAAS,EACnC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,GACf,MAAM,GAAG,IAAI,CAgBf"}
|
package/dist/attach-file-tool.js
CHANGED
|
@@ -18,8 +18,14 @@ import { Type } from 'typebox';
|
|
|
18
18
|
* works in a pure web-frontend session with no CLI bridge present.
|
|
19
19
|
*
|
|
20
20
|
* `cwd` is the session's working directory; relative paths resolve against it.
|
|
21
|
+
*
|
|
22
|
+
* Serving: a successful call is itself the download grant (download-grants.ts),
|
|
23
|
+
* so any existing file the agent attaches is downloadable, inside the cwd or not.
|
|
24
|
+
* The remaining refusals are server-wide policy, which `downloadsConfig` exposes
|
|
25
|
+
* so the tool can FAIL LOUDLY instead of showing a button that can only error:
|
|
26
|
+
* downloads disabled, or the file over `downloads.maxBytes`.
|
|
21
27
|
*/
|
|
22
|
-
export function createAttachFileTool(cwd) {
|
|
28
|
+
export function createAttachFileTool(cwd, downloadsConfig = () => undefined) {
|
|
23
29
|
return {
|
|
24
30
|
name: 'attach_file',
|
|
25
31
|
label: 'Attach File',
|
|
@@ -27,8 +33,8 @@ export function createAttachFileTool(cwd) {
|
|
|
27
33
|
'phone/browser) can download it with one tap. The user is remote and CANNOT ' +
|
|
28
34
|
'reach the local filesystem, so a file path in your reply is NOT enough: they ' +
|
|
29
35
|
'can only obtain a file if you attach it with this tool. Pass the path to a ' +
|
|
30
|
-
'real, existing file
|
|
31
|
-
'
|
|
36
|
+
'real, existing file (relative to the working directory, or absolute; it does ' +
|
|
37
|
+
'not need to be inside the working directory). Use this both right after generating a ' +
|
|
32
38
|
'deliverable (a PDF, an export, a report) AND whenever the user asks for a ' +
|
|
33
39
|
"file by name or type, including one created earlier in the conversation, " +
|
|
34
40
|
"e.g. 'give me the gpx', 'send me the pdf', 'download the report', 'can I " +
|
|
@@ -87,6 +93,22 @@ export function createAttachFileTool(cwd) {
|
|
|
87
93
|
isError: true,
|
|
88
94
|
};
|
|
89
95
|
}
|
|
96
|
+
const refusal = downloadRefusal(downloadsConfig(), size ?? 0, resolved);
|
|
97
|
+
if (refusal) {
|
|
98
|
+
return {
|
|
99
|
+
content: [{ type: 'text', text: refusal }],
|
|
100
|
+
details: undefined,
|
|
101
|
+
isError: true,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// Report the REAL path: it is what the download grant names, so the grant
|
|
105
|
+
// pins the file attached now, not whatever a symlink points at later.
|
|
106
|
+
try {
|
|
107
|
+
resolved = fs.realpathSync(resolved);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// Raced away after the stat: keep the lexical path; the download will 404.
|
|
111
|
+
}
|
|
90
112
|
const filename = path.basename(resolved);
|
|
91
113
|
return {
|
|
92
114
|
content: [
|
|
@@ -101,4 +123,24 @@ export function createAttachFileTool(cwd) {
|
|
|
101
123
|
},
|
|
102
124
|
};
|
|
103
125
|
}
|
|
126
|
+
/** Default `downloads.maxBytes`, kept in step with GET /session/download. */
|
|
127
|
+
export const DEFAULT_DOWNLOAD_MAX_BYTES = 100 * 1024 * 1024;
|
|
128
|
+
/**
|
|
129
|
+
* Why GET /session/download would refuse this file, as an actionable tool error,
|
|
130
|
+
* or null when it will be served. Mirrors the endpoint's policy checks so the
|
|
131
|
+
* agent learns at attach time rather than the user at tap time.
|
|
132
|
+
*/
|
|
133
|
+
export function downloadRefusal(policy, size, resolved) {
|
|
134
|
+
if (policy?.enabled === false) {
|
|
135
|
+
return (`attach_file: downloads are disabled on this server (downloads.enabled is false), ` +
|
|
136
|
+
`so the user cannot download ${resolved}. Tell the user the file path instead.`);
|
|
137
|
+
}
|
|
138
|
+
const maxBytes = policy?.maxBytes ?? DEFAULT_DOWNLOAD_MAX_BYTES;
|
|
139
|
+
if (size > maxBytes) {
|
|
140
|
+
return (`attach_file: ${resolved} is ${size} bytes, over the server's download limit of ` +
|
|
141
|
+
`${maxBytes} bytes (downloads.maxBytes). Produce a smaller file (compress, split ` +
|
|
142
|
+
`or downscale it) and attach that instead.`);
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
104
146
|
//# sourceMappingURL=attach-file-tool.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attach-file-tool.js","sourceRoot":"","sources":["../src/attach-file-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAG/B
|
|
1
|
+
{"version":3,"file":"attach-file-tool.js","sourceRoot":"","sources":["../src/attach-file-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAG/B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,oBAAoB,CAClC,GAAW,EACX,kBAAqD,GAAG,EAAE,CAAC,SAAS;IAEpE,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,KAAK,EAAE,aAAa;QACpB,WAAW,EACT,2EAA2E;YAC3E,6EAA6E;YAC7E,+EAA+E;YAC/E,6EAA6E;YAC7E,+EAA+E;YAC/E,uFAAuF;YACvF,4EAA4E;YAC5E,2EAA2E;YAC3E,2EAA2E;YAC3E,0EAA0E;YAC1E,2EAA2E;YAC3E,kDAAkD;QACpD,aAAa,EACX,sEAAsE;QACxE,gBAAgB,EAAE;YAChB,oUAAoU;YACpU,6IAA6I;SAC9I;QACD,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;YACtB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC;gBAChB,WAAW,EACT,mFAAmF;aACtF,CAAC;SACH,CAAC;QACF,KAAK,CAAC,OAAO,CAAC,WAAmB,EAAE,MAAe;YAChD,MAAM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AAmF;YACnF,+BAA+B,QAAQ,wCAAwC,CAChF,CAAC;IACJ,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,EAAE,QAAQ,IAAI,0BAA0B,CAAC;IAChE,IAAI,IAAI,GAAG,QAAQ,EAAE,CAAC;QACpB,OAAO,CACL,gBAAgB,QAAQ,OAAO,IAAI,8CAA8C;YACjF,GAAG,QAAQ,uEAAuE;YAClF,2CAA2C,CAC5C,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The absolute paths granted by successful `attach_file` tool results in the
|
|
3
|
+
* transcript at `sessionFile`. Returns `[]` when the file is missing/unreadable.
|
|
4
|
+
* Cached against the file's mtime + size, so a long transcript is re-scanned
|
|
5
|
+
* only after it changes.
|
|
6
|
+
*/
|
|
7
|
+
export declare function readAttachGrants(sessionFile: string): Promise<string[]>;
|
|
8
|
+
/** Extract the granted path from one transcript line, or null if it is not a successful attach_file result. */
|
|
9
|
+
export declare function attachGrantFromLine(line: string): string | null;
|
|
10
|
+
/** Test hook: forget every cached scan. */
|
|
11
|
+
export declare function clearAttachGrantCache(): void;
|
|
12
|
+
//# sourceMappingURL=download-grants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"download-grants.d.ts","sourceRoot":"","sources":["../src/download-grants.ts"],"names":[],"mappings":"AAuCA;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAoC7E;AAED,+GAA+G;AAC/G,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAe/D;AAED,2CAA2C;AAC3C,wBAAgB,qBAAqB,IAAI,IAAI,CAE5C"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import readline from 'node:readline';
|
|
3
|
+
const cache = new Map();
|
|
4
|
+
const MAX_CACHE_ENTRIES = 64;
|
|
5
|
+
/** Cheap pre-filter so only lines that can possibly be an attach result are JSON-parsed. */
|
|
6
|
+
const NEEDLE = '"toolName":"attach_file"';
|
|
7
|
+
/**
|
|
8
|
+
* The absolute paths granted by successful `attach_file` tool results in the
|
|
9
|
+
* transcript at `sessionFile`. Returns `[]` when the file is missing/unreadable.
|
|
10
|
+
* Cached against the file's mtime + size, so a long transcript is re-scanned
|
|
11
|
+
* only after it changes.
|
|
12
|
+
*/
|
|
13
|
+
export async function readAttachGrants(sessionFile) {
|
|
14
|
+
let stat;
|
|
15
|
+
try {
|
|
16
|
+
stat = await fs.promises.stat(sessionFile);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
if (!stat.isFile())
|
|
22
|
+
return [];
|
|
23
|
+
const cached = cache.get(sessionFile);
|
|
24
|
+
if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
|
|
25
|
+
return cached.paths;
|
|
26
|
+
}
|
|
27
|
+
const paths = new Set();
|
|
28
|
+
try {
|
|
29
|
+
const rl = readline.createInterface({
|
|
30
|
+
input: fs.createReadStream(sessionFile, { encoding: 'utf8' }),
|
|
31
|
+
crlfDelay: Infinity,
|
|
32
|
+
});
|
|
33
|
+
for await (const line of rl) {
|
|
34
|
+
if (!line.includes(NEEDLE))
|
|
35
|
+
continue;
|
|
36
|
+
const granted = attachGrantFromLine(line);
|
|
37
|
+
if (granted)
|
|
38
|
+
paths.add(granted);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
const result = Array.from(paths);
|
|
45
|
+
if (cache.size >= MAX_CACHE_ENTRIES && !cache.has(sessionFile)) {
|
|
46
|
+
const oldest = cache.keys().next().value;
|
|
47
|
+
if (oldest !== undefined)
|
|
48
|
+
cache.delete(oldest);
|
|
49
|
+
}
|
|
50
|
+
cache.set(sessionFile, { mtimeMs: stat.mtimeMs, size: stat.size, paths: result });
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
/** Extract the granted path from one transcript line, or null if it is not a successful attach_file result. */
|
|
54
|
+
export function attachGrantFromLine(line) {
|
|
55
|
+
let entry;
|
|
56
|
+
try {
|
|
57
|
+
entry = JSON.parse(line);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
if (entry?.type !== 'message')
|
|
63
|
+
return null;
|
|
64
|
+
const message = entry.message;
|
|
65
|
+
if (!message || message.role !== 'toolResult' || message.toolName !== 'attach_file')
|
|
66
|
+
return null;
|
|
67
|
+
if (message.isError === true)
|
|
68
|
+
return null;
|
|
69
|
+
const p = message.details?.path;
|
|
70
|
+
return typeof p === 'string' && p.length > 0 ? p : null;
|
|
71
|
+
}
|
|
72
|
+
/** Test hook: forget every cached scan. */
|
|
73
|
+
export function clearAttachGrantCache() {
|
|
74
|
+
cache.clear();
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=download-grants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"download-grants.js","sourceRoot":"","sources":["../src/download-grants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,QAAQ,MAAM,eAAe,CAAC;AAgCrC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAsB,CAAC;AAC5C,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,4FAA4F;AAC5F,MAAM,MAAM,GAAG,0BAA0B,CAAC;AAE1C;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,WAAmB;IACxD,IAAI,IAAc,CAAC;IACnB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAC7C,CAAC;IAAC,M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CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACjD,CAAC;IACD,KAAK,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;IAClF,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,+GAA+G;AAC/G,MAAM,UAAU,mBAAmB,CAAC,IAAY;IAC9C,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QACH,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAK,KAAmC,EAAE,IAAI,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IAC1E,MAAM,OAAO,GAAI,KAA+B,CAAC,OAEpC,CAAC;IACd,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,YAAY,IAAI,OAAO,CAAC,QAAQ,KAAK,aAAa;QAAE,OAAO,IAAI,CAAC;IACjG,IAAI,OAAO,CAAC,OAAO,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAC1C,MAAM,CAAC,GAAG,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC;IAChC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC1D,CAAC;AAED,2CAA2C;AAC3C,MAAM,UAAU,qBAAqB;IACnC,KAAK,CAAC,KAAK,EAAE,CAAC;AAChB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,8 @@ import { matchRemoteRepoRule, resolveRemoteCandidates } from './remote-candidate
|
|
|
12
12
|
import { RestoreJobRegistry } from './restore-jobs.js';
|
|
13
13
|
import { INITIAL_HISTORY_LIMIT, HISTORY_PAGE_SIZE } from './protocol.js';
|
|
14
14
|
import { gateUploadFolder, gateUploadWrite, uploadMaxBytes, uploadTooLarge } from './upload-gate.js';
|
|
15
|
+
import { readAttachGrants } from './download-grants.js';
|
|
16
|
+
import { DEFAULT_DOWNLOAD_MAX_BYTES } from './attach-file-tool.js';
|
|
15
17
|
import { UploadStaging, UPLOAD_CAPABILITY, isValidUploadId, parseChunkFrame, CHUNK_FRAME_KIND } from './upload-staging.js';
|
|
16
18
|
import fs from 'node:fs';
|
|
17
19
|
import path from 'node:path';
|
|
@@ -193,13 +195,18 @@ function getUploadStaging() {
|
|
|
193
195
|
return uploadStaging;
|
|
194
196
|
}
|
|
195
197
|
/**
|
|
196
|
-
* Validate a requested download path against the allowed roots and
|
|
197
|
-
* safe absolute path to stream, or null if it
|
|
198
|
-
* a regular file. This is the security-critical
|
|
199
|
-
* target BEFORE the containment check so neither
|
|
200
|
-
* symlink can point outside an allowed root.
|
|
198
|
+
* Validate a requested download path against the allowed roots and the session's
|
|
199
|
+
* attach grants, and return the safe absolute path to stream, or null if it
|
|
200
|
+
* escapes / does not exist / is not a regular file. This is the security-critical
|
|
201
|
+
* guard: it realpath-resolves the target BEFORE the containment check so neither
|
|
202
|
+
* `..` traversal nor an in-tree symlink can point outside an allowed root.
|
|
203
|
+
*
|
|
204
|
+
* `grants` are the files the agent explicitly offered with `attach_file` (see
|
|
205
|
+
* download-grants.ts). They are matched EXACTLY on real path, never as roots, so
|
|
206
|
+
* attaching one file outside the roots does not open its directory. Loaded lazily:
|
|
207
|
+
* the transcript is only scanned when the root check has already failed.
|
|
201
208
|
*/
|
|
202
|
-
function resolveSafeDownloadPath(requested, cwd, roots) {
|
|
209
|
+
async function resolveSafeDownloadPath(requested, cwd, roots, grants) {
|
|
203
210
|
if (!requested)
|
|
204
211
|
return null;
|
|
205
212
|
let candidate = expandTilde(requested);
|
|
@@ -236,9 +243,22 @@ function resolveSafeDownloadPath(requested, cwd, roots) {
|
|
|
236
243
|
return true;
|
|
237
244
|
return real.startsWith(root.endsWith(path.sep) ? root : root + path.sep);
|
|
238
245
|
});
|
|
239
|
-
if (
|
|
240
|
-
return
|
|
241
|
-
|
|
246
|
+
if (withinRoot)
|
|
247
|
+
return real;
|
|
248
|
+
if (grants) {
|
|
249
|
+
for (const granted of await grants()) {
|
|
250
|
+
let grantedReal;
|
|
251
|
+
try {
|
|
252
|
+
grantedReal = fs.realpathSync(path.resolve(granted));
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (grantedReal === real)
|
|
258
|
+
return real;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
242
262
|
}
|
|
243
263
|
function serveStaticFile(reqPath, res) {
|
|
244
264
|
let filePath = path.join(staticDir, reqPath === '/' ? 'index.html' : reqPath);
|
|
@@ -1978,10 +1998,12 @@ async function main() {
|
|
|
1978
1998
|
sendJSON(res, 400, { error: 'Missing sessionId or path' });
|
|
1979
1999
|
return;
|
|
1980
2000
|
}
|
|
1981
|
-
|
|
1982
|
-
|
|
2001
|
+
// Live OR on-disk: a session the server has not loaded still has a cwd
|
|
2002
|
+
// (its roots) and a transcript (its attach grants).
|
|
2003
|
+
const located = await sessionPool.locateSession(qSessionId);
|
|
2004
|
+
const cwd = located?.cwd;
|
|
1983
2005
|
const roots = resolveDownloadRoots(config, cwd);
|
|
1984
|
-
const safePath = resolveSafeDownloadPath(qPath, cwd, roots);
|
|
2006
|
+
const safePath = await resolveSafeDownloadPath(qPath, cwd, roots, located ? () => readAttachGrants(located.sessionFile) : undefined);
|
|
1985
2007
|
if (!safePath) {
|
|
1986
2008
|
// 404 (not 403) so we do not leak whether a path outside the allowed
|
|
1987
2009
|
// roots exists: escape and not-found are indistinguishable to the client.
|
|
@@ -1996,7 +2018,7 @@ async function main() {
|
|
|
1996
2018
|
sendJSON(res, 404, { error: 'File not found' });
|
|
1997
2019
|
return;
|
|
1998
2020
|
}
|
|
1999
|
-
const maxBytes = config.downloads?.maxBytes ??
|
|
2021
|
+
const maxBytes = config.downloads?.maxBytes ?? DEFAULT_DOWNLOAD_MAX_BYTES;
|
|
2000
2022
|
if (stat.size > maxBytes) {
|
|
2001
2023
|
sendJSON(res, 413, { error: `File too large (${stat.size} > ${maxBytes} bytes)` });
|
|
2002
2024
|
return;
|