Personal Telegram developer console for GitHub
feat(workspace): add bounded multi-file policy and aggregate diff
davoodmehraban89 committed
Oct 5, 2026 at 05:36 UTC
cbcadb9c0d70b9901557d16b2b78b2821bf99dc8
1 file changed
+63
-1
src/core/workspace-policy.js
+63
-1
@@ -1,11 +1,15 @@
1
const MAX_CHARS = 20000;
2
const MAX_BYTES = 65536;
3
const MAX_COMMIT_MESSAGE = 120;
4
+const MAX_WORKSPACE_FILES = 8;
5
+const MAX_WORKSPACE_BYTES = 131072;
6
const SAFE_HANDLE = /^[A-Za-z0-9_-]{10}$/;
7
8
export const WORKSPACE_EDITOR_LIMITS = Object.freeze({
9
maxChars: MAX_CHARS,
8
- maxBytes: MAX_BYTES
10
+ maxBytes: MAX_BYTES,
11
+ maxFiles: MAX_WORKSPACE_FILES,
12
+ maxAggregateBytes: MAX_WORKSPACE_BYTES
13
});
14
15
const encoder = new TextEncoder();
@@ -131,3 +135,61 @@ export function buildUnifiedDiff({ path, before, after, maxChars = 2600 } = {})
135
truncated: true
136
};
137
}
138
+
139
+
140
+export function validateWorkspaceFiles(files = []) {
141
+ if (!Array.isArray(files) || files.length === 0) throw new Error('Workspace files are required');
142
+ if (files.length > MAX_WORKSPACE_FILES) throw new Error(`Workspace is limited to ${MAX_WORKSPACE_FILES} files`);
143
+ const seen = new Set();
144
+ let totalBytes = 0;
145
+ const normalized = files.map(source => {
146
+ const path = String(source?.path || '').replaceAll('\\', '/');
147
+ if (!path) throw new Error('Workspace file path is required');
148
+ if (seen.has(path)) throw new Error(`Duplicate workspace path: ${path}`);
149
+ seen.add(path);
150
+ const original = validateEditableFile({ path, text: source?.originalText ?? '' });
151
+ if (!original.editable) throw new Error(original.reason);
152
+ const draft = validateEditableFile({ path, text: source?.draftText ?? source?.originalText ?? '' });
153
+ if (!draft.editable) throw new Error(draft.reason);
154
+ totalBytes += Math.max(original.byteLength, draft.byteLength);
155
+ return { ...source, path, originalText: original.text, draftText: draft.text };
156
+ });
157
+ if (totalBytes > MAX_WORKSPACE_BYTES) {
158
+ throw new Error('Workspace aggregate text is limited to 131,072 UTF-8 bytes');
159
+ }
160
+ return { valid: true, files: normalized, totalBytes };
161
+}
162
+
163
+export function buildWorkspaceDiff({ files = [], maxChars = 3000 } = {}) {
164
+ const validated = validateWorkspaceFiles(files);
165
+ const changed = [];
166
+ let additions = 0;
167
+ let deletions = 0;
168
+ for (const file of validated.files) {
169
+ if (file.originalText === file.draftText) continue;
170
+ const diff = buildUnifiedDiff({ path: file.path, before: file.originalText, after: file.draftText, maxChars: 10000 });
171
+ additions += diff.additions;
172
+ deletions += diff.deletions;
173
+ changed.push(diff.text);
174
+ }
175
+ const raw = changed.join('\n\n');
176
+ const limit = Math.max(32, Math.min(10000, Number(maxChars) || 3000));
177
+ const marker = '\n…';
178
+ const truncated = raw.length > limit;
179
+ const text = truncated ? `${raw.slice(0, Math.max(0, limit - marker.length))}${marker}` : raw;
180
+ return { text, additions, deletions, filesChanged: changed.length, truncated };
181
+}
182
+
183
+export async function digestWorkspaceFiles(files = []) {
184
+ const validated = validateWorkspaceFiles(files);
185
+ const canonical = validated.files
186
+ .map(file => ({
187
+ path: file.path,
188
+ blobSha: String(file.blobSha || ''),
189
+ originalDigest: String(file.originalDigest || ''),
190
+ draftText: file.draftText,
191
+ draftDigest: String(file.draftDigest || '')
192
+ }))
193
+ .sort((a, b) => a.path.localeCompare(b.path));
194
+ return digestText(JSON.stringify(canonical));
195
+}