Personal Telegram developer console for GitHub
feat/studio-g0-foundation
audit/commit-drilldown
audit/finalize-workspace-menu-security
audit/ops-actionability
audit/pr-menu-consolidation
audit/status-menu-consolidation
audit/telegram-menu-e2e
audit/telegram-menu-e2e-wave2
chore/actions-node24-runtime
cleanup/remove-diag
design/cloudflare-ops-read
docs/control-plane-2-7-design
docs/guide-v2-1-sync
docs/menu-audit-final-sync
docs/phase2-acceptance-closeout
docs/phase2-multifile-design
feat/bootstrap-worker
feat/cloudflare-ops-read
feat/cp2-multifile-final
feat/cp2-multifile-workspace
feat/cp3-github-write-final
feat/cp4-cloudflare-ops-final
feat/cp5-cicd-control-plane
feat/cp6-incident-manager
feat/cp7-automation-engine
feat/deep-file-browser
feat/final-delivery-16-20
feat/github-advanced-read
feat/in-app-guide-v2
feat/navigation-recovery-ux-v2
feat/ops-console-v2-1
feat/persistent-menu-code-input
feat/phase2-multifile-workspace
feat/phase2-multifile-workspace-v2
feat/plugin-foundation-v1
feat/safe-github-ops
feat/self-register-webhook
feat/studio-g0-foundation
feat/telegram-main-inline-menu
feat/telegram-pr-merge
feat/ux-platform-8-12
feat/v3a-readonly-copilot
feat/workspace-file-upload
feature/mobile-repo-browser
feature/multi-file-change-set
fix/approval-expiry-feedback
fix/callback-resilience-and-docs
fix/ci-actions-pagination
fix/cloudflare-fetch-binding
fix/editor-request-body-cap
fix/editor-safe-file-options
fix/issues-permission-release-empty-ux
fix/main-menu-callback-fallback
fix/phase2-file-editor-selection
fix/phase2-final-ux
fix/phase2-workspace-ref-persistence
fix/production-auto-deploy
fix/provider-write-error-feedback
fix/repository-id-navigation-fallback
fix/repository-pr-navigation
fix/telegram-callback-updates
fix/telegram-runtime-diagnostics
fix/workspace-editor-flow-v2
fix/workspace-recovery-validation
hardening/ci-zero-test-telegram-token
hardening/pr1-destructive-totp
hardening/pr2-kill-switch
main
multifile-workspace
nexa/edit-BwJEIIY3PW
nexa/edit-IPHT8irO7b
nexa/edit-b4Od3BIMYd
p2-multifile
phase2-20261005
phase2-20261005-a
phase2-20261005-b
phase2-20261005-c
phase2-20261005-d
phase2-20261005-e
phase2-20261005-f
phase2-20261005-g
phase2-20261005-h
phase2-20261005-i
phase2-20261005-j
phase2-20261005-k
phase2-20261005-l
phase2-20261005-m
phase2-20261005-n
phase2-20261005-o
phase2-20261005-p
phase2-20261005-q
phase2-20261005-r
phase2-20261005-s
phase2-20261005-t
phase2-20261005-u
phase2-20261005-v
phase2-20261005-w
phase2-20261005-x
phase2-20261005-y
phase2-20261005-z
phase2-acceptance-closeout
phase2-add-file-hotfix
phase2-change-set
phase2-design-20261005
phase2-design-final
phase2-implementation
phase2-multifile-implementation
phase2-multifile-spec
phase2-spec-final
phase2-workspace
phase2/multifile-workspace
plan/nexa-studio-v2-reviewed
security/remove-telegram-setup
spec/repository-workspace-editor
v3a/multifile
work/multifile
| 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, |
| 10 | maxBytes: MAX_BYTES, |
| 11 | maxFiles: MAX_WORKSPACE_FILES, |
| 12 | maxAggregateBytes: MAX_WORKSPACE_BYTES |
| 13 | }); |
| 14 | |
| 15 | const encoder = new TextEncoder(); |
| 16 | |
| 17 | const sensitivePath = (value) => { |
| 18 | const path = String(value || '') |
| 19 | .replaceAll('\\', '/') |
| 20 | .toLowerCase(); |
| 21 | const name = path.split('/').pop() || ''; |
| 22 | if (name === '.env' || name.startsWith('.env.')) return true; |
| 23 | if (name === 'id_rsa' || name === 'id_ed25519') return true; |
| 24 | if (/\.(pem|key|p12|pfx)$/i.test(name)) return true; |
| 25 | if (name === 'credentials.json') return true; |
| 26 | if (/^service-account.*\.json$/i.test(name)) return true; |
| 27 | return false; |
| 28 | }; |
| 29 | |
| 30 | const codePointLength = (value) => Array.from(value).length; |
| 31 | |
| 32 | export function validateEditableFile({ path, text, byteLength } = {}) { |
| 33 | const source = String(text ?? ''); |
| 34 | const charLength = codePointLength(source); |
| 35 | const computedBytes = encoder.encode(source).byteLength; |
| 36 | const providerBytes = Number(byteLength); |
| 37 | const effectiveBytes = |
| 38 | Number.isFinite(providerBytes) && providerBytes >= 0 |
| 39 | ? Math.max(computedBytes, providerBytes) |
| 40 | : computedBytes; |
| 41 | |
| 42 | let reason = ''; |
| 43 | if (sensitivePath(path)) reason = 'Sensitive file path is not editable.'; |
| 44 | else if (source.includes('\0')) |
| 45 | reason = 'Binary-like content is not editable.'; |
| 46 | else if ( |
| 47 | /^version https:\/\/git-lfs\.github\.com\/spec\/v1(?:\r?\n|$)/.test(source) |
| 48 | ) |
| 49 | reason = 'Git LFS pointer files are not editable.'; |
| 50 | else if (charLength > MAX_CHARS) |
| 51 | reason = `Editable text is limited to ${MAX_CHARS.toLocaleString('en-US')} Unicode characters.`; |
| 52 | else if (effectiveBytes > MAX_BYTES) |
| 53 | reason = `Editable text is limited to ${MAX_BYTES.toLocaleString('en-US')} UTF-8 bytes.`; |
| 54 | |
| 55 | return { |
| 56 | editable: reason === '', |
| 57 | reason, |
| 58 | text: source, |
| 59 | charLength, |
| 60 | byteLength: effectiveBytes |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | export function normalizeCommitMessage(value) { |
| 65 | const normalized = String(value ?? '') |
| 66 | .trim() |
| 67 | .replace(/\s+/g, ' '); |
| 68 | if (!normalized) throw new Error('Commit message is required'); |
| 69 | if (codePointLength(normalized) > MAX_COMMIT_MESSAGE) { |
| 70 | throw new Error('Commit message must be 120 characters or fewer'); |
| 71 | } |
| 72 | return normalized; |
| 73 | } |
| 74 | |
| 75 | export function makeTargetBranch(workspaceId) { |
| 76 | const id = String(workspaceId ?? ''); |
| 77 | if (!SAFE_HANDLE.test(id)) |
| 78 | throw new Error('Workspace ID must be a 10-character URL-safe handle'); |
| 79 | return `nexa/edit-${id}`; |
| 80 | } |
| 81 | |
| 82 | export async function digestText(value) { |
| 83 | const digest = await crypto.subtle.digest( |
| 84 | 'SHA-256', |
| 85 | encoder.encode(String(value ?? '')) |
| 86 | ); |
| 87 | return Array.from(new Uint8Array(digest), (byte) => |
| 88 | byte.toString(16).padStart(2, '0') |
| 89 | ).join(''); |
| 90 | } |
| 91 | |
| 92 | function linesOf(value) { |
| 93 | return String(value ?? '').split('\n'); |
| 94 | } |
| 95 | |
| 96 | function smallDiffOperations(beforeLines, afterLines) { |
| 97 | const rows = beforeLines.length + 1; |
| 98 | const cols = afterLines.length + 1; |
| 99 | const table = new Uint16Array(rows * cols); |
| 100 | const at = (i, j) => i * cols + j; |
| 101 | |
| 102 | for (let i = beforeLines.length - 1; i >= 0; i -= 1) { |
| 103 | for (let j = afterLines.length - 1; j >= 0; j -= 1) { |
| 104 | table[at(i, j)] = |
| 105 | beforeLines[i] === afterLines[j] |
| 106 | ? table[at(i + 1, j + 1)] + 1 |
| 107 | : Math.max(table[at(i + 1, j)], table[at(i, j + 1)]); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | const operations = []; |
| 112 | let i = 0; |
| 113 | let j = 0; |
| 114 | while (i < beforeLines.length || j < afterLines.length) { |
| 115 | if ( |
| 116 | i < beforeLines.length && |
| 117 | j < afterLines.length && |
| 118 | beforeLines[i] === afterLines[j] |
| 119 | ) { |
| 120 | operations.push({ kind: ' ', line: beforeLines[i] }); |
| 121 | i += 1; |
| 122 | j += 1; |
| 123 | continue; |
| 124 | } |
| 125 | if ( |
| 126 | j < afterLines.length && |
| 127 | (i >= beforeLines.length || table[at(i, j + 1)] >= table[at(i + 1, j)]) |
| 128 | ) { |
| 129 | operations.push({ kind: '+', line: afterLines[j] }); |
| 130 | j += 1; |
| 131 | continue; |
| 132 | } |
| 133 | operations.push({ kind: '-', line: beforeLines[i] }); |
| 134 | i += 1; |
| 135 | } |
| 136 | return operations; |
| 137 | } |
| 138 | |
| 139 | function diffOperations(beforeLines, afterLines) { |
| 140 | // Ordinary edits retain the original insertion-first LCS reconstruction. |
| 141 | // Larger edits use a fixed-distance band: memory is linear in line count, |
| 142 | // with explicit storage/work limits rather than a quadratic allocation. |
| 143 | const maxTableCells = 65536; |
| 144 | const maxDistance = 64; |
| 145 | const maxBandCells = 1000000; |
| 146 | let workRemaining = 2000000; |
| 147 | const tooComplex = () => { |
| 148 | const error = new Error( |
| 149 | 'This edit is too complex to preview safely. Split it into smaller changes and try again.' |
| 150 | ); |
| 151 | error.code = 'DIFF_COMPLEXITY_LIMIT'; |
| 152 | return error; |
| 153 | }; |
| 154 | const spend = (work = 1) => { |
| 155 | workRemaining -= work; |
| 156 | if (workRemaining < 0) throw tooComplex(); |
| 157 | }; |
| 158 | if (beforeLines.length > MAX_CHARS + 1 || afterLines.length > MAX_CHARS + 1) { |
| 159 | throw tooComplex(); |
| 160 | } |
| 161 | if ((beforeLines.length + 1) * (afterLines.length + 1) <= maxTableCells) { |
| 162 | return smallDiffOperations(beforeLines, afterLines); |
| 163 | } |
| 164 | |
| 165 | const operations = []; |
| 166 | let prefix = 0; |
| 167 | while ( |
| 168 | prefix < beforeLines.length && |
| 169 | prefix < afterLines.length && |
| 170 | beforeLines[prefix] === afterLines[prefix] |
| 171 | ) { |
| 172 | spend(); |
| 173 | operations.push({ kind: ' ', line: beforeLines[prefix++] }); |
| 174 | } |
| 175 | const n = beforeLines.length - prefix; |
| 176 | const m = afterLines.length - prefix; |
| 177 | if (n === 0 || m === 0) { |
| 178 | for (let i = 0; i < n; i += 1) { |
| 179 | operations.push({ kind: '-', line: beforeLines[prefix + i] }); |
| 180 | } |
| 181 | for (let j = 0; j < m; j += 1) { |
| 182 | operations.push({ kind: '+', line: afterLines[prefix + j] }); |
| 183 | } |
| 184 | return operations; |
| 185 | } |
| 186 | |
| 187 | // Trim suffixes only while measuring distance. Reconstructing against the |
| 188 | // full suffix preserves legacy ties when the suffix contains repeated lines. |
| 189 | let suffix = 0; |
| 190 | while ( |
| 191 | suffix < n && |
| 192 | suffix < m && |
| 193 | beforeLines[beforeLines.length - suffix - 1] === |
| 194 | afterLines[afterLines.length - suffix - 1] |
| 195 | ) { |
| 196 | spend(); |
| 197 | suffix += 1; |
| 198 | } |
| 199 | const distanceN = n - suffix; |
| 200 | const distanceM = m - suffix; |
| 201 | if (Math.abs(distanceN - distanceM) > maxDistance) throw tooComplex(); |
| 202 | const offset = maxDistance + 1; |
| 203 | const frontier = new Int32Array(2 * maxDistance + 3); |
| 204 | frontier.fill(-1); |
| 205 | frontier[offset + 1] = 0; |
| 206 | let distance = -1; |
| 207 | search: for (let d = 0; d <= maxDistance; d += 1) { |
| 208 | for (let k = -d; k <= d; k += 2) { |
| 209 | spend(); |
| 210 | const slot = offset + k; |
| 211 | let x = |
| 212 | k === -d || (k !== d && frontier[slot - 1] < frontier[slot + 1]) |
| 213 | ? frontier[slot + 1] |
| 214 | : frontier[slot - 1] + 1; |
| 215 | let y = x - k; |
| 216 | while ( |
| 217 | x < distanceN && |
| 218 | y < distanceM && |
| 219 | beforeLines[prefix + x] === afterLines[prefix + y] |
| 220 | ) { |
| 221 | spend(); |
| 222 | x += 1; |
| 223 | y += 1; |
| 224 | } |
| 225 | frontier[slot] = x; |
| 226 | if (x >= distanceN && y >= distanceM) { |
| 227 | distance = d; |
| 228 | break search; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | if (distance < 0) throw tooComplex(); |
| 233 | |
| 234 | // Every optimal path of edit distance D stays inside |i - j| <= D. |
| 235 | // Capped suffix distances in this band reconstruct the exact legacy path, |
| 236 | // including its preference for insertion when both choices are optimal. |
| 237 | const width = 2 * distance + 1; |
| 238 | const cells = (n + 1) * width; |
| 239 | if (cells > maxBandCells) throw tooComplex(); |
| 240 | spend(cells); |
| 241 | const unreachable = maxDistance + 1; |
| 242 | const table = new Uint8Array(cells); |
| 243 | table.fill(unreachable); |
| 244 | const at = (i, j) => i * width + j - i + distance; |
| 245 | const score = (i, j) => |
| 246 | i > n || j > m || Math.abs(i - j) > distance |
| 247 | ? unreachable |
| 248 | : table[at(i, j)]; |
| 249 | for (let i = n; i >= 0; i -= 1) { |
| 250 | for ( |
| 251 | let j = Math.min(m, i + distance); |
| 252 | j >= Math.max(0, i - distance); |
| 253 | j -= 1 |
| 254 | ) { |
| 255 | spend(); |
| 256 | const value = |
| 257 | i === n |
| 258 | ? m - j |
| 259 | : j === m |
| 260 | ? n - i |
| 261 | : beforeLines[prefix + i] === afterLines[prefix + j] |
| 262 | ? score(i + 1, j + 1) |
| 263 | : Math.min(score(i + 1, j), score(i, j + 1)) + 1; |
| 264 | table[at(i, j)] = Math.min(unreachable, value); |
| 265 | } |
| 266 | } |
| 267 | if (score(0, 0) !== distance) throw tooComplex(); |
| 268 | let i = 0; |
| 269 | let j = 0; |
| 270 | while (i < n || j < m) { |
| 271 | spend(); |
| 272 | if (i < n && j < m && beforeLines[prefix + i] === afterLines[prefix + j]) { |
| 273 | operations.push({ kind: ' ', line: beforeLines[prefix + i++] }); |
| 274 | j += 1; |
| 275 | } else if (j < m && (i === n || score(i, j + 1) <= score(i + 1, j))) { |
| 276 | operations.push({ kind: '+', line: afterLines[prefix + j++] }); |
| 277 | } else { |
| 278 | operations.push({ kind: '-', line: beforeLines[prefix + i++] }); |
| 279 | } |
| 280 | } |
| 281 | return operations; |
| 282 | } |
| 283 | |
| 284 | export function buildUnifiedDiff({ |
| 285 | path, |
| 286 | before, |
| 287 | after, |
| 288 | maxChars = 2600 |
| 289 | } = {}) { |
| 290 | const safePath = String(path || 'file').replace(/[\r\n]/g, ''); |
| 291 | const operations = diffOperations(linesOf(before), linesOf(after)); |
| 292 | const additions = operations.reduce( |
| 293 | (count, op) => count + (op.kind === '+' ? 1 : 0), |
| 294 | 0 |
| 295 | ); |
| 296 | const deletions = operations.reduce( |
| 297 | (count, op) => count + (op.kind === '-' ? 1 : 0), |
| 298 | 0 |
| 299 | ); |
| 300 | const raw = [ |
| 301 | `--- a/${safePath}`, |
| 302 | `+++ b/${safePath}`, |
| 303 | '@@', |
| 304 | ...operations.map((op) => `${op.kind}${op.line}`) |
| 305 | ].join('\n'); |
| 306 | |
| 307 | const limit = Math.max(32, Math.min(10000, Number(maxChars) || 2600)); |
| 308 | if (raw.length <= limit) { |
| 309 | return { text: raw, additions, deletions, truncated: false }; |
| 310 | } |
| 311 | const marker = '\n…'; |
| 312 | return { |
| 313 | text: `${raw.slice(0, Math.max(0, limit - marker.length))}${marker}`, |
| 314 | additions, |
| 315 | deletions, |
| 316 | truncated: true |
| 317 | }; |
| 318 | } |
| 319 | |
| 320 | export function validateWorkspaceFiles(files = []) { |
| 321 | if (!Array.isArray(files) || files.length === 0) |
| 322 | throw new Error('Workspace files are required'); |
| 323 | if (files.length > MAX_WORKSPACE_FILES) |
| 324 | throw new Error(`Workspace is limited to ${MAX_WORKSPACE_FILES} files`); |
| 325 | const seen = new Set(); |
| 326 | let totalBytes = 0; |
| 327 | const normalized = files.map((source) => { |
| 328 | const path = String(source?.path || '').replaceAll('\\', '/'); |
| 329 | if (!path) throw new Error('Workspace file path is required'); |
| 330 | if (seen.has(path)) throw new Error(`Duplicate workspace path: ${path}`); |
| 331 | seen.add(path); |
| 332 | const original = validateEditableFile({ |
| 333 | path, |
| 334 | text: source?.originalText ?? '' |
| 335 | }); |
| 336 | if (!original.editable) throw new Error(original.reason); |
| 337 | const draft = validateEditableFile({ |
| 338 | path, |
| 339 | text: source?.draftText ?? source?.originalText ?? '' |
| 340 | }); |
| 341 | if (!draft.editable) throw new Error(draft.reason); |
| 342 | totalBytes += Math.max(original.byteLength, draft.byteLength); |
| 343 | return { |
| 344 | ...source, |
| 345 | path, |
| 346 | originalText: original.text, |
| 347 | draftText: draft.text |
| 348 | }; |
| 349 | }); |
| 350 | if (totalBytes > MAX_WORKSPACE_BYTES) { |
| 351 | throw new Error( |
| 352 | 'Workspace aggregate text is limited to 131,072 UTF-8 bytes' |
| 353 | ); |
| 354 | } |
| 355 | return { valid: true, files: normalized, totalBytes }; |
| 356 | } |
| 357 | |
| 358 | export function buildWorkspaceDiff({ files = [], maxChars = 3000 } = {}) { |
| 359 | const validated = validateWorkspaceFiles(files); |
| 360 | const changed = []; |
| 361 | let additions = 0; |
| 362 | let deletions = 0; |
| 363 | for (const file of validated.files) { |
| 364 | if (file.originalText === file.draftText) continue; |
| 365 | const diff = buildUnifiedDiff({ |
| 366 | path: file.path, |
| 367 | before: file.originalText, |
| 368 | after: file.draftText, |
| 369 | maxChars: 10000 |
| 370 | }); |
| 371 | additions += diff.additions; |
| 372 | deletions += diff.deletions; |
| 373 | changed.push(diff.text); |
| 374 | } |
| 375 | const raw = changed.join('\n\n'); |
| 376 | const limit = Math.max(32, Math.min(10000, Number(maxChars) || 3000)); |
| 377 | const marker = '\n…'; |
| 378 | const truncated = raw.length > limit; |
| 379 | const text = truncated |
| 380 | ? `${raw.slice(0, Math.max(0, limit - marker.length))}${marker}` |
| 381 | : raw; |
| 382 | return { |
| 383 | text, |
| 384 | additions, |
| 385 | deletions, |
| 386 | filesChanged: changed.length, |
| 387 | truncated |
| 388 | }; |
| 389 | } |
| 390 | |
| 391 | export async function digestWorkspaceFiles(files = []) { |
| 392 | const validated = validateWorkspaceFiles(files); |
| 393 | const canonical = validated.files |
| 394 | .map((file) => ({ |
| 395 | path: file.path, |
| 396 | blobSha: String(file.blobSha || ''), |
| 397 | originalDigest: String(file.originalDigest || ''), |
| 398 | draftText: file.draftText, |
| 399 | draftDigest: String(file.draftDigest || '') |
| 400 | })) |
| 401 | .sort((a, b) => a.path.localeCompare(b.path)); |
| 402 | return digestText(JSON.stringify(canonical)); |
| 403 | } |