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 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { DurableWorkspaceStoreClient } from '../src/core/workspace-store.js'; |
| 4 | import { WorkspaceStore } from '../src/durable/workspace-store.js'; |
| 5 | |
| 6 | function makeState() { |
| 7 | const data = new Map(); |
| 8 | let transactionTail = Promise.resolve(); |
| 9 | const api = { |
| 10 | async get(key) { |
| 11 | return structuredClone(data.get(key)); |
| 12 | }, |
| 13 | async put(key, value) { |
| 14 | data.set(key, structuredClone(value)); |
| 15 | }, |
| 16 | async delete(key) { |
| 17 | return data.delete(key); |
| 18 | } |
| 19 | }; |
| 20 | return { |
| 21 | data, |
| 22 | storage: { |
| 23 | ...api, |
| 24 | transaction(fn) { |
| 25 | const result = transactionTail.then(() => fn(api)); |
| 26 | transactionTail = result.catch(() => {}); |
| 27 | return result; |
| 28 | }, |
| 29 | async setAlarm() {} |
| 30 | } |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | function fixture() { |
| 35 | const states = new Map(); |
| 36 | const namespace = { |
| 37 | idFromName: String, |
| 38 | get(id) { |
| 39 | if (!states.has(id)) states.set(id, makeState()); |
| 40 | const store = new WorkspaceStore(states.get(id)); |
| 41 | return { fetch: (request) => store.fetch(request) }; |
| 42 | } |
| 43 | }; |
| 44 | const client = new DurableWorkspaceStoreClient({ |
| 45 | namespace, |
| 46 | now: () => 1000 |
| 47 | }); |
| 48 | return { client, states }; |
| 49 | } |
| 50 | |
| 51 | const initial = { |
| 52 | ownerId: 'A', |
| 53 | repoId: 123, |
| 54 | repoFullName: 'owner/repo', |
| 55 | baseBranch: 'main', |
| 56 | baseHeadSha: 'a'.repeat(40), |
| 57 | baseTreeSha: 'b'.repeat(40), |
| 58 | path: 'a.txt', |
| 59 | blobSha: 'c'.repeat(40), |
| 60 | originalText: 'before\n', |
| 61 | originalDigest: 'd'.repeat(64) |
| 62 | }; |
| 63 | |
| 64 | const edit = (workspaceId, overrides = {}) => ({ |
| 65 | ownerId: 'A', |
| 66 | workspaceId, |
| 67 | path: 'a.txt', |
| 68 | text: 'after\n', |
| 69 | digest: 'e'.repeat(64), |
| 70 | expectedRevision: 0, |
| 71 | ...overrides |
| 72 | }); |
| 73 | |
| 74 | const isConflict = (error) => { |
| 75 | assert.equal(error.status, 409); |
| 76 | assert.equal(error.code, 'REVISION_CONFLICT'); |
| 77 | assert.equal(error.message.includes('PRIVATE_DRAFT_SENTINEL'), false); |
| 78 | return true; |
| 79 | }; |
| 80 | |
| 81 | test('per-file revisions start at zero and successful conditional edits advance them', async () => { |
| 82 | const { client } = fixture(); |
| 83 | let workspace = await client.createWorkspace(initial); |
| 84 | assert.equal(workspace.files[0].revision, 0); |
| 85 | workspace = await client.updateWorkspaceFileDraft(edit(workspace.id)); |
| 86 | assert.equal(workspace.files[0].revision, 1); |
| 87 | assert.equal(workspace.files[0].draftText, 'after\n'); |
| 88 | workspace = await client.updateWorkspaceFileDraft( |
| 89 | edit(workspace.id, { |
| 90 | expectedRevision: 1, |
| 91 | text: 'next\n', |
| 92 | digest: 'f'.repeat(64) |
| 93 | }) |
| 94 | ); |
| 95 | assert.equal(workspace.files[0].revision, 2); |
| 96 | }); |
| 97 | |
| 98 | test('stale revisions return a typed conflict without changing workspace or READY intent', async () => { |
| 99 | const { client, states } = fixture(); |
| 100 | const workspace = await client.createWorkspace(initial); |
| 101 | await client.updateWorkspaceFileDraft(edit(workspace.id)); |
| 102 | await client.sealIntent({ |
| 103 | ownerId: 'A', |
| 104 | workspaceId: workspace.id, |
| 105 | intent: { intentDigest: '1'.repeat(64) } |
| 106 | }); |
| 107 | await client.setPendingInput({ |
| 108 | ownerId: 'A', |
| 109 | workspaceId: workspace.id, |
| 110 | mode: 'draft_file' |
| 111 | }); |
| 112 | const before = structuredClone(states.get('A').data); |
| 113 | await assert.rejects( |
| 114 | client.updateWorkspaceFileDraft( |
| 115 | edit(workspace.id, { |
| 116 | text: 'PRIVATE_DRAFT_SENTINEL', |
| 117 | digest: 'f'.repeat(64) |
| 118 | }) |
| 119 | ), |
| 120 | isConflict |
| 121 | ); |
| 122 | assert.deepEqual(states.get('A').data, before); |
| 123 | }); |
| 124 | |
| 125 | test('a same-file concurrent If-Match update has exactly one winner', async () => { |
| 126 | const { client } = fixture(); |
| 127 | const workspace = await client.createWorkspace(initial); |
| 128 | const outcomes = await Promise.allSettled([ |
| 129 | client.updateWorkspaceFileDraft(edit(workspace.id)), |
| 130 | client.updateWorkspaceFileDraft( |
| 131 | edit(workspace.id, { text: 'second\n', digest: 'f'.repeat(64) }) |
| 132 | ) |
| 133 | ]); |
| 134 | assert.equal(outcomes.filter((r) => r.status === 'fulfilled').length, 1); |
| 135 | const rejected = outcomes.find((r) => r.status === 'rejected'); |
| 136 | isConflict(rejected.reason); |
| 137 | const stored = await client.getWorkspace({ |
| 138 | ownerId: 'A', |
| 139 | workspaceId: workspace.id |
| 140 | }); |
| 141 | assert.equal(stored.files[0].revision, 1); |
| 142 | assert.equal( |
| 143 | stored.files[0].draftText, |
| 144 | outcomes.find((r) => r.status === 'fulfilled').value.files[0].draftText |
| 145 | ); |
| 146 | }); |
| 147 | |
| 148 | test('concurrent edits of different files preserve both edits and independent revisions', async () => { |
| 149 | const { client } = fixture(); |
| 150 | let workspace = await client.createWorkspace(initial); |
| 151 | workspace = await client.upsertWorkspaceFile({ |
| 152 | ownerId: 'A', |
| 153 | workspaceId: workspace.id, |
| 154 | file: { ...initial, path: 'b.txt' } |
| 155 | }); |
| 156 | assert.equal(workspace.files[1].revision, 0); |
| 157 | await Promise.all([ |
| 158 | client.updateWorkspaceFileDraft(edit(workspace.id)), |
| 159 | client.updateWorkspaceFileDraft( |
| 160 | edit(workspace.id, { |
| 161 | path: 'b.txt', |
| 162 | text: 'second\n', |
| 163 | digest: 'f'.repeat(64) |
| 164 | }) |
| 165 | ) |
| 166 | ]); |
| 167 | const stored = await client.getWorkspace({ |
| 168 | ownerId: 'A', |
| 169 | workspaceId: workspace.id |
| 170 | }); |
| 171 | assert.deepEqual( |
| 172 | stored.files.map(({ path, draftText, revision }) => ({ |
| 173 | path, |
| 174 | draftText, |
| 175 | revision |
| 176 | })), |
| 177 | [ |
| 178 | { path: 'a.txt', draftText: 'after\n', revision: 1 }, |
| 179 | { path: 'b.txt', draftText: 'second\n', revision: 1 } |
| 180 | ] |
| 181 | ); |
| 182 | }); |
| 183 | |
| 184 | test('a concurrent commit-message write cannot restore an earlier file draft', async () => { |
| 185 | const { client } = fixture(); |
| 186 | const workspace = await client.createWorkspace(initial); |
| 187 | await Promise.all([ |
| 188 | client.updateWorkspaceFileDraft(edit(workspace.id)), |
| 189 | client.setCommitMessage({ |
| 190 | ownerId: 'A', |
| 191 | workspaceId: workspace.id, |
| 192 | message: 'Update file', |
| 193 | digest: '1'.repeat(64) |
| 194 | }) |
| 195 | ]); |
| 196 | const stored = await client.getWorkspace({ |
| 197 | ownerId: 'A', |
| 198 | workspaceId: workspace.id |
| 199 | }); |
| 200 | assert.equal(stored.files[0].draftText, 'after\n'); |
| 201 | assert.equal(stored.files[0].revision, 1); |
| 202 | assert.equal(stored.commitMessage, 'Update file'); |
| 203 | }); |
| 204 | |
| 205 | test('concurrent intent sealing cannot restore an earlier file draft', async () => { |
| 206 | const { client } = fixture(); |
| 207 | const workspace = await client.createWorkspace(initial); |
| 208 | await Promise.all([ |
| 209 | client.updateWorkspaceFileDraft(edit(workspace.id)), |
| 210 | client.sealIntent({ |
| 211 | ownerId: 'A', |
| 212 | workspaceId: workspace.id, |
| 213 | intent: { intentDigest: '1'.repeat(64) } |
| 214 | }) |
| 215 | ]); |
| 216 | const stored = await client.getWorkspace({ |
| 217 | ownerId: 'A', |
| 218 | workspaceId: workspace.id |
| 219 | }); |
| 220 | assert.equal(stored.files[0].draftText, 'after\n'); |
| 221 | assert.equal(stored.files[0].revision, 1); |
| 222 | }); |
| 223 | |
| 224 | test('legacy edits without expectedRevision still invalidate stale conditional edits', async () => { |
| 225 | const { client } = fixture(); |
| 226 | let workspace = await client.createWorkspace(initial); |
| 227 | workspace = await client.updateWorkspaceFileDraft( |
| 228 | edit(workspace.id, { expectedRevision: undefined }) |
| 229 | ); |
| 230 | assert.equal(workspace.files[0].revision, 1); |
| 231 | await assert.rejects( |
| 232 | client.updateWorkspaceFileDraft(edit(workspace.id)), |
| 233 | isConflict |
| 234 | ); |
| 235 | workspace = await client.updateDraft({ |
| 236 | ownerId: 'A', |
| 237 | workspaceId: workspace.id, |
| 238 | text: 'legacy\n', |
| 239 | digest: 'f'.repeat(64) |
| 240 | }); |
| 241 | assert.equal(workspace.files[0].revision, 2); |
| 242 | await assert.rejects( |
| 243 | client.updateWorkspaceFileDraft( |
| 244 | edit(workspace.id, { expectedRevision: 1 }) |
| 245 | ), |
| 246 | isConflict |
| 247 | ); |
| 248 | }); |
| 249 | |
| 250 | test('identical draft calls advance revisions while preserving an identical READY intent', async () => { |
| 251 | const { client } = fixture(); |
| 252 | const workspace = await client.createWorkspace(initial); |
| 253 | const intent = await client.sealIntent({ |
| 254 | ownerId: 'A', |
| 255 | workspaceId: workspace.id, |
| 256 | intent: { intentDigest: '1'.repeat(64) } |
| 257 | }); |
| 258 | let updated = await client.updateWorkspaceFileDraft( |
| 259 | edit(workspace.id, { |
| 260 | text: initial.originalText, |
| 261 | digest: initial.originalDigest |
| 262 | }) |
| 263 | ); |
| 264 | assert.equal(updated.files[0].revision, 1); |
| 265 | assert.equal(updated.currentIntentId, intent.id); |
| 266 | assert.equal(updated.state, 'READY'); |
| 267 | updated = await client.updateDraft({ |
| 268 | ownerId: 'A', |
| 269 | workspaceId: workspace.id, |
| 270 | text: initial.originalText, |
| 271 | digest: initial.originalDigest |
| 272 | }); |
| 273 | assert.equal(updated.files[0].revision, 2); |
| 274 | assert.equal(updated.currentIntentId, intent.id); |
| 275 | updated = await client.updateWorkspaceFileDraft( |
| 276 | edit(workspace.id, { expectedRevision: 2 }) |
| 277 | ); |
| 278 | assert.equal(updated.currentIntentId, null); |
| 279 | assert.equal(updated.state, 'DRAFT'); |
| 280 | }); |
| 281 | |
| 282 | test('pre-revision multi-file and legacy records read as zero and accept the first conditional write', async () => { |
| 283 | for (const legacy of [false, true]) { |
| 284 | const { client, states } = fixture(); |
| 285 | const workspace = await client.createWorkspace(initial); |
| 286 | const stored = structuredClone( |
| 287 | states.get('A').data.get(`ws:${workspace.id}`) |
| 288 | ); |
| 289 | delete stored.files[0].revision; |
| 290 | if (legacy) delete stored.files; |
| 291 | states.get('A').data.set(`ws:${workspace.id}`, stored); |
| 292 | const loaded = await client.getWorkspace({ |
| 293 | ownerId: 'A', |
| 294 | workspaceId: workspace.id |
| 295 | }); |
| 296 | assert.equal(loaded.files[0].revision, 0); |
| 297 | const updated = await client.updateWorkspaceFileDraft(edit(workspace.id)); |
| 298 | assert.equal(updated.files[0].revision, 1); |
| 299 | } |
| 300 | }); |
| 301 | |
| 302 | test('removing and readding a path cannot revive an old revision', async () => { |
| 303 | const { client } = fixture(); |
| 304 | let workspace = await client.createWorkspace(initial); |
| 305 | workspace = await client.upsertWorkspaceFile({ |
| 306 | ownerId: 'A', |
| 307 | workspaceId: workspace.id, |
| 308 | file: { ...initial, path: 'b.txt' } |
| 309 | }); |
| 310 | await client.updateWorkspaceFileDraft(edit(workspace.id)); |
| 311 | await client.removeWorkspaceFile({ |
| 312 | ownerId: 'A', |
| 313 | workspaceId: workspace.id, |
| 314 | path: 'a.txt' |
| 315 | }); |
| 316 | workspace = await client.upsertWorkspaceFile({ |
| 317 | ownerId: 'A', |
| 318 | workspaceId: workspace.id, |
| 319 | file: initial |
| 320 | }); |
| 321 | assert.equal(workspace.files[0].revision, 0); |
| 322 | assert.equal(workspace.files[1].revision, 2); |
| 323 | for (const expectedRevision of [0, 1]) { |
| 324 | await assert.rejects( |
| 325 | client.updateWorkspaceFileDraft(edit(workspace.id, { expectedRevision })), |
| 326 | isConflict |
| 327 | ); |
| 328 | } |
| 329 | workspace = await client.updateWorkspaceFileDraft( |
| 330 | edit(workspace.id, { expectedRevision: 2 }) |
| 331 | ); |
| 332 | assert.equal(workspace.files[1].revision, 3); |
| 333 | }); |
| 334 | |
| 335 | test('foreign-owner conditional updates have no effect on the owner workspace', async () => { |
| 336 | const { client, states } = fixture(); |
| 337 | const workspace = await client.createWorkspace(initial); |
| 338 | const before = structuredClone(states.get('A').data); |
| 339 | await assert.rejects( |
| 340 | client.updateWorkspaceFileDraft(edit(workspace.id, { ownerId: 'B' })), |
| 341 | /not found or expired/i |
| 342 | ); |
| 343 | assert.deepEqual(states.get('A').data, before); |
| 344 | assert.equal( |
| 345 | await client.getWorkspace({ ownerId: 'B', workspaceId: workspace.id }), |
| 346 | null |
| 347 | ); |
| 348 | }); |
| 349 | |
| 350 | test('invalid expected revisions cannot disable optimistic concurrency', async () => { |
| 351 | const { client, states } = fixture(); |
| 352 | const workspace = await client.createWorkspace(initial); |
| 353 | const before = structuredClone(states.get('A').data); |
| 354 | for (const expectedRevision of [ |
| 355 | null, |
| 356 | '', |
| 357 | '0', |
| 358 | -1, |
| 359 | 0.5, |
| 360 | Number.MAX_SAFE_INTEGER + 1 |
| 361 | ]) { |
| 362 | await assert.rejects( |
| 363 | client.updateWorkspaceFileDraft(edit(workspace.id, { expectedRevision })), |
| 364 | /revision.*invalid/i |
| 365 | ); |
| 366 | } |
| 367 | assert.deepEqual(states.get('A').data, before); |
| 368 | }); |
| 369 | |
| 370 | const materialMutations = (client, workspaceId) => [ |
| 371 | () => client.updateWorkspaceFileDraft(edit(workspaceId)), |
| 372 | () => |
| 373 | client.updateDraft({ |
| 374 | ownerId: 'A', |
| 375 | workspaceId, |
| 376 | text: 'legacy\n', |
| 377 | digest: 'f'.repeat(64) |
| 378 | }), |
| 379 | () => |
| 380 | client.upsertWorkspaceFile({ |
| 381 | ownerId: 'A', |
| 382 | workspaceId, |
| 383 | file: { ...initial, path: 'c.txt' } |
| 384 | }), |
| 385 | () => |
| 386 | client.removeWorkspaceFile({ ownerId: 'A', workspaceId, path: 'b.txt' }), |
| 387 | () => |
| 388 | client.setCommitMessage({ |
| 389 | ownerId: 'A', |
| 390 | workspaceId, |
| 391 | message: 'Late message', |
| 392 | digest: 'f'.repeat(64) |
| 393 | }), |
| 394 | () => |
| 395 | client.sealIntent({ |
| 396 | ownerId: 'A', |
| 397 | workspaceId, |
| 398 | intent: { intentDigest: 'f'.repeat(64) } |
| 399 | }) |
| 400 | ]; |
| 401 | |
| 402 | test('writes queued behind beginCommit cannot reset an in-flight workspace', async () => { |
| 403 | const { client, states } = fixture(); |
| 404 | let workspace = await client.createWorkspace(initial); |
| 405 | workspace = await client.upsertWorkspaceFile({ |
| 406 | ownerId: 'A', |
| 407 | workspaceId: workspace.id, |
| 408 | file: { ...initial, path: 'b.txt' } |
| 409 | }); |
| 410 | const intent = await client.sealIntent({ |
| 411 | ownerId: 'A', |
| 412 | workspaceId: workspace.id, |
| 413 | intent: { intentDigest: '1'.repeat(64) } |
| 414 | }); |
| 415 | const operations = materialMutations(client, workspace.id); |
| 416 | const [started, ...outcomes] = await Promise.allSettled([ |
| 417 | client.beginCommit({ ownerId: 'A', intentId: intent.id }), |
| 418 | ...operations.map((operation) => operation()) |
| 419 | ]); |
| 420 | assert.equal(started.status, 'fulfilled'); |
| 421 | assert.equal(started.value.disposition, 'start'); |
| 422 | for (const outcome of outcomes) { |
| 423 | assert.equal(outcome.status, 'rejected'); |
| 424 | assert.equal(outcome.reason.status, 409); |
| 425 | assert.equal(outcome.reason.code, 'WORKSPACE_NOT_EDITABLE'); |
| 426 | } |
| 427 | const stored = await client.getWorkspace({ |
| 428 | ownerId: 'A', |
| 429 | workspaceId: workspace.id |
| 430 | }); |
| 431 | assert.equal(stored.files[0].draftText, 'before\n'); |
| 432 | assert.equal(stored.files[0].revision, 0); |
| 433 | assert.equal(stored.files.length, 2); |
| 434 | assert.equal(stored.commitMessage, ''); |
| 435 | assert.equal(stored.currentIntentId, intent.id); |
| 436 | assert.equal( |
| 437 | states.get('A').data.get(`intent:${intent.id}`).state, |
| 438 | 'COMMITTING' |
| 439 | ); |
| 440 | }); |
| 441 | |
| 442 | test('committed workspace material and message cannot be changed by a delayed write', async () => { |
| 443 | const { client, states } = fixture(); |
| 444 | let workspace = await client.createWorkspace(initial); |
| 445 | workspace = await client.upsertWorkspaceFile({ |
| 446 | ownerId: 'A', |
| 447 | workspaceId: workspace.id, |
| 448 | file: { ...initial, path: 'b.txt' } |
| 449 | }); |
| 450 | const intent = await client.sealIntent({ |
| 451 | ownerId: 'A', |
| 452 | workspaceId: workspace.id, |
| 453 | intent: { intentDigest: '1'.repeat(64) } |
| 454 | }); |
| 455 | await client.beginCommit({ ownerId: 'A', intentId: intent.id }); |
| 456 | await client.completeCommit({ |
| 457 | ownerId: 'A', |
| 458 | intentId: intent.id, |
| 459 | commitSha: 'e'.repeat(40), |
| 460 | branch: `nexa/edit-${workspace.id}` |
| 461 | }); |
| 462 | const before = structuredClone(states.get('A').data); |
| 463 | for (const operation of materialMutations(client, workspace.id)) { |
| 464 | await assert.rejects(operation(), (error) => { |
| 465 | assert.equal(error.status, 409); |
| 466 | assert.equal(error.code, 'WORKSPACE_NOT_EDITABLE'); |
| 467 | return true; |
| 468 | }); |
| 469 | } |
| 470 | assert.deepEqual(states.get('A').data, before); |
| 471 | }); |
| 472 | |
| 473 | test('concurrent file additions cannot exceed the eight-file workspace limit', async () => { |
| 474 | const { client } = fixture(); |
| 475 | let workspace = await client.createWorkspace(initial); |
| 476 | for (let index = 1; index < 7; index++) { |
| 477 | workspace = await client.upsertWorkspaceFile({ |
| 478 | ownerId: 'A', |
| 479 | workspaceId: workspace.id, |
| 480 | file: { ...initial, path: `${index}.txt` } |
| 481 | }); |
| 482 | } |
| 483 | const outcomes = await Promise.allSettled( |
| 484 | ['last.txt', 'excess.txt'].map((path) => |
| 485 | client.upsertWorkspaceFile({ |
| 486 | ownerId: 'A', |
| 487 | workspaceId: workspace.id, |
| 488 | file: { ...initial, path } |
| 489 | }) |
| 490 | ) |
| 491 | ); |
| 492 | assert.equal( |
| 493 | outcomes.filter((outcome) => outcome.status === 'fulfilled').length, |
| 494 | 1 |
| 495 | ); |
| 496 | const failure = outcomes.find((outcome) => outcome.status === 'rejected'); |
| 497 | assert.equal(failure.reason.status, 422); |
| 498 | assert.equal(failure.reason.code, 'WORKSPACE_LIMIT_EXCEEDED'); |
| 499 | const stored = await client.getWorkspace({ |
| 500 | ownerId: 'A', |
| 501 | workspaceId: workspace.id |
| 502 | }); |
| 503 | assert.equal(stored.files.length, 8); |
| 504 | }); |
| 505 | |
| 506 | test('concurrent different-file edits enforce the UTF-8 aggregate boundary atomically', async () => { |
| 507 | const { client } = fixture(); |
| 508 | const large = '😀'.repeat(10000); |
| 509 | let workspace = await client.createWorkspace({ |
| 510 | ...initial, |
| 511 | originalText: large |
| 512 | }); |
| 513 | for (const path of ['b.txt', 'c.txt']) { |
| 514 | workspace = await client.upsertWorkspaceFile({ |
| 515 | ownerId: 'A', |
| 516 | workspaceId: workspace.id, |
| 517 | file: { ...initial, path, originalText: large } |
| 518 | }); |
| 519 | } |
| 520 | const outcomes = await Promise.allSettled( |
| 521 | ['a.txt', 'b.txt'].map((path) => |
| 522 | client.updateWorkspaceFileDraft( |
| 523 | edit(workspace.id, { path, text: large + 'x'.repeat(6000) }) |
| 524 | ) |
| 525 | ) |
| 526 | ); |
| 527 | assert.equal( |
| 528 | outcomes.filter((outcome) => outcome.status === 'fulfilled').length, |
| 529 | 1 |
| 530 | ); |
| 531 | const failure = outcomes.find((outcome) => outcome.status === 'rejected'); |
| 532 | assert.equal(failure.reason.status, 422); |
| 533 | assert.equal(failure.reason.code, 'WORKSPACE_LIMIT_EXCEEDED'); |
| 534 | const stored = await client.getWorkspace({ |
| 535 | ownerId: 'A', |
| 536 | workspaceId: workspace.id |
| 537 | }); |
| 538 | assert.equal( |
| 539 | stored.files.reduce( |
| 540 | (total, file) => |
| 541 | total + new TextEncoder().encode(file.draftText).byteLength, |
| 542 | 0 |
| 543 | ), |
| 544 | 126000 |
| 545 | ); |
| 546 | assert.deepEqual(stored.files.map((file) => file.revision).sort(), [0, 0, 1]); |
| 547 | }); |
| 548 | |
| 549 | test('workspace aggregate policy retains original text when an edited draft shrinks', async () => { |
| 550 | const { client, states } = fixture(); |
| 551 | const large = '😀'.repeat(15000); |
| 552 | let workspace = await client.createWorkspace({ |
| 553 | ...initial, |
| 554 | originalText: large |
| 555 | }); |
| 556 | workspace = await client.upsertWorkspaceFile({ |
| 557 | ownerId: 'A', |
| 558 | workspaceId: workspace.id, |
| 559 | file: { ...initial, path: 'b.txt', originalText: large } |
| 560 | }); |
| 561 | await client.updateWorkspaceFileDraft(edit(workspace.id, { text: '' })); |
| 562 | await client.updateWorkspaceFileDraft( |
| 563 | edit(workspace.id, { path: 'b.txt', text: '' }) |
| 564 | ); |
| 565 | const before = structuredClone(states.get('A').data); |
| 566 | await assert.rejects( |
| 567 | client.upsertWorkspaceFile({ |
| 568 | ownerId: 'A', |
| 569 | workspaceId: workspace.id, |
| 570 | file: { ...initial, path: 'c.txt', originalText: 'x'.repeat(11073) } |
| 571 | }), |
| 572 | (error) => { |
| 573 | assert.equal(error.status, 422); |
| 574 | assert.equal(error.code, 'WORKSPACE_LIMIT_EXCEEDED'); |
| 575 | return true; |
| 576 | } |
| 577 | ); |
| 578 | assert.deepEqual(states.get('A').data, before); |
| 579 | }); |
| 580 | |
| 581 | const reviewOf = (workspace) => ({ |
| 582 | baseHeadSha: workspace.baseHeadSha, |
| 583 | files: workspace.files.map(({ path, revision }) => ({ path, revision })) |
| 584 | }); |
| 585 | |
| 586 | const reviewedIntent = (workspaceId, overrides = {}) => ({ |
| 587 | workspaceId, |
| 588 | repoId: 123, |
| 589 | baseBranch: 'main', |
| 590 | baseHeadSha: 'a'.repeat(40), |
| 591 | baseTreeSha: 'b'.repeat(40), |
| 592 | files: [ |
| 593 | { |
| 594 | path: 'a.txt', |
| 595 | sourceBlobSha: 'c'.repeat(40), |
| 596 | contentDigest: 'd'.repeat(64) |
| 597 | } |
| 598 | ], |
| 599 | commitMessageDigest: '1'.repeat(64), |
| 600 | intentDigest: '2'.repeat(64), |
| 601 | ...overrides |
| 602 | }); |
| 603 | |
| 604 | test('a message change queued before sealing rejects its displayed digest atomically', async () => { |
| 605 | const { client, states } = fixture(); |
| 606 | const workspace = await client.createWorkspace(initial); |
| 607 | await client.setCommitMessage({ |
| 608 | ownerId: 'A', |
| 609 | workspaceId: workspace.id, |
| 610 | message: 'Reviewed message', |
| 611 | digest: '1'.repeat(64) |
| 612 | }); |
| 613 | const [saved, sealed] = await Promise.allSettled([ |
| 614 | client.setCommitMessage({ |
| 615 | ownerId: 'A', |
| 616 | workspaceId: workspace.id, |
| 617 | message: 'Unreviewed message', |
| 618 | digest: '2'.repeat(64) |
| 619 | }), |
| 620 | client.sealIntent({ |
| 621 | ownerId: 'A', |
| 622 | workspaceId: workspace.id, |
| 623 | intent: reviewedIntent(workspace.id), |
| 624 | expectedReview: { |
| 625 | ...reviewOf(workspace), |
| 626 | commitMessageDigest: '1'.repeat(64) |
| 627 | } |
| 628 | }) |
| 629 | ]); |
| 630 | assert.equal(saved.status, 'fulfilled'); |
| 631 | assert.equal(sealed.status, 'rejected'); |
| 632 | isConflict(sealed.reason); |
| 633 | const stored = await client.getWorkspace({ |
| 634 | ownerId: 'A', |
| 635 | workspaceId: workspace.id |
| 636 | }); |
| 637 | assert.equal(stored.commitMessage, 'Unreviewed message'); |
| 638 | assert.equal(stored.currentIntentId, null); |
| 639 | assert.equal(stored.state, 'DRAFT'); |
| 640 | assert.equal( |
| 641 | [...states.get('A').data.keys()].some((key) => key.startsWith('intent:')), |
| 642 | false |
| 643 | ); |
| 644 | }); |
| 645 | |
| 646 | test('sealing accepts a matching displayed message digest and preserves legacy review callers', async () => { |
| 647 | const { client } = fixture(); |
| 648 | const workspace = await client.createWorkspace(initial); |
| 649 | await client.setCommitMessage({ |
| 650 | ownerId: 'A', |
| 651 | workspaceId: workspace.id, |
| 652 | message: 'Reviewed message', |
| 653 | digest: '1'.repeat(64) |
| 654 | }); |
| 655 | const sealed = await client.sealIntent({ |
| 656 | ownerId: 'A', |
| 657 | workspaceId: workspace.id, |
| 658 | intent: reviewedIntent(workspace.id), |
| 659 | expectedReview: { |
| 660 | ...reviewOf(workspace), |
| 661 | commitMessageDigest: '1'.repeat(64) |
| 662 | } |
| 663 | }); |
| 664 | const repeated = await client.sealIntent({ |
| 665 | ownerId: 'A', |
| 666 | workspaceId: workspace.id, |
| 667 | intent: { intentDigest: '2'.repeat(64) }, |
| 668 | expectedReview: reviewOf(workspace) |
| 669 | }); |
| 670 | assert.equal(sealed.state, 'READY'); |
| 671 | assert.equal(repeated.id, sealed.id); |
| 672 | }); |
| 673 | |
| 674 | test('a message-bound review seals only proposed material matching the current workspace', async () => { |
| 675 | const { client, states } = fixture(); |
| 676 | const workspace = await client.createWorkspace(initial); |
| 677 | await client.setCommitMessage({ |
| 678 | ownerId: 'A', |
| 679 | workspaceId: workspace.id, |
| 680 | message: 'Reviewed message', |
| 681 | digest: '1'.repeat(64) |
| 682 | }); |
| 683 | const before = structuredClone(states.get('A').data); |
| 684 | for (const overrides of [ |
| 685 | { workspaceId: 'OTHER12345' }, |
| 686 | { repoId: 999 }, |
| 687 | { baseBranch: 'other' }, |
| 688 | { baseHeadSha: 'f'.repeat(40) }, |
| 689 | { baseTreeSha: 'f'.repeat(40) }, |
| 690 | { commitMessageDigest: 'f'.repeat(64) }, |
| 691 | { files: [] }, |
| 692 | { |
| 693 | files: [ |
| 694 | { |
| 695 | path: 'b.txt', |
| 696 | sourceBlobSha: 'c'.repeat(40), |
| 697 | contentDigest: 'd'.repeat(64) |
| 698 | } |
| 699 | ] |
| 700 | }, |
| 701 | { |
| 702 | files: [ |
| 703 | { |
| 704 | path: 'a.txt', |
| 705 | sourceBlobSha: 'f'.repeat(40), |
| 706 | contentDigest: 'd'.repeat(64) |
| 707 | } |
| 708 | ] |
| 709 | }, |
| 710 | { |
| 711 | files: [ |
| 712 | { |
| 713 | path: 'a.txt', |
| 714 | sourceBlobSha: 'c'.repeat(40), |
| 715 | contentDigest: 'f'.repeat(64) |
| 716 | } |
| 717 | ] |
| 718 | } |
| 719 | ]) { |
| 720 | await assert.rejects( |
| 721 | client.sealIntent({ |
| 722 | ownerId: 'A', |
| 723 | workspaceId: workspace.id, |
| 724 | intent: reviewedIntent(workspace.id, overrides), |
| 725 | expectedReview: { |
| 726 | ...reviewOf(workspace), |
| 727 | commitMessageDigest: '1'.repeat(64) |
| 728 | } |
| 729 | }), |
| 730 | isConflict |
| 731 | ); |
| 732 | assert.deepEqual(states.get('A').data, before); |
| 733 | } |
| 734 | }); |
| 735 | |
| 736 | test('sealing a stale review cannot replace the currently edited workspace intent', async () => { |
| 737 | const { client, states } = fixture(); |
| 738 | const workspace = await client.createWorkspace(initial); |
| 739 | const expectedReview = reviewOf(workspace); |
| 740 | await client.updateWorkspaceFileDraft(edit(workspace.id)); |
| 741 | const before = structuredClone(states.get('A').data); |
| 742 | await assert.rejects( |
| 743 | client.sealIntent({ |
| 744 | ownerId: 'A', |
| 745 | workspaceId: workspace.id, |
| 746 | intent: { intentDigest: '1'.repeat(64) }, |
| 747 | expectedReview |
| 748 | }), |
| 749 | isConflict |
| 750 | ); |
| 751 | assert.deepEqual(states.get('A').data, before); |
| 752 | }); |
| 753 | |
| 754 | test('review sealing compares the exact file set and base head rather than only matching revisions', async () => { |
| 755 | const { client, states } = fixture(); |
| 756 | let workspace = await client.createWorkspace(initial); |
| 757 | workspace = await client.upsertWorkspaceFile({ |
| 758 | ownerId: 'A', |
| 759 | workspaceId: workspace.id, |
| 760 | file: { ...initial, path: 'b.txt' } |
| 761 | }); |
| 762 | const before = structuredClone(states.get('A').data); |
| 763 | for (const expectedReview of [ |
| 764 | { ...reviewOf(workspace), baseHeadSha: 'f'.repeat(40) }, |
| 765 | { ...reviewOf(workspace), files: [{ path: 'a.txt', revision: 0 }] }, |
| 766 | { |
| 767 | ...reviewOf(workspace), |
| 768 | files: [ |
| 769 | { path: 'a.txt', revision: 0 }, |
| 770 | { path: 'c.txt', revision: 0 } |
| 771 | ] |
| 772 | } |
| 773 | ]) { |
| 774 | await assert.rejects( |
| 775 | client.sealIntent({ |
| 776 | ownerId: 'A', |
| 777 | workspaceId: workspace.id, |
| 778 | intent: { intentDigest: '1'.repeat(64) }, |
| 779 | expectedReview |
| 780 | }), |
| 781 | isConflict |
| 782 | ); |
| 783 | assert.deepEqual(states.get('A').data, before); |
| 784 | } |
| 785 | }); |
| 786 | |
| 787 | test('an edit queued before review sealing wins and rejects the stale review atomically', async () => { |
| 788 | const { client } = fixture(); |
| 789 | const workspace = await client.createWorkspace(initial); |
| 790 | const [saved, sealed] = await Promise.allSettled([ |
| 791 | client.updateWorkspaceFileDraft(edit(workspace.id)), |
| 792 | client.sealIntent({ |
| 793 | ownerId: 'A', |
| 794 | workspaceId: workspace.id, |
| 795 | intent: { intentDigest: '1'.repeat(64) }, |
| 796 | expectedReview: reviewOf(workspace) |
| 797 | }) |
| 798 | ]); |
| 799 | assert.equal(saved.status, 'fulfilled'); |
| 800 | assert.equal(sealed.status, 'rejected'); |
| 801 | isConflict(sealed.reason); |
| 802 | const stored = await client.getWorkspace({ |
| 803 | ownerId: 'A', |
| 804 | workspaceId: workspace.id |
| 805 | }); |
| 806 | assert.equal(stored.files[0].revision, 1); |
| 807 | assert.equal(stored.currentIntentId, null); |
| 808 | assert.equal(stored.state, 'DRAFT'); |
| 809 | }); |
| 810 | |
| 811 | test('an edit queued after a matching review seal invalidates its READY intent', async () => { |
| 812 | const { client } = fixture(); |
| 813 | const workspace = await client.createWorkspace(initial); |
| 814 | const [sealed] = await Promise.all([ |
| 815 | client.sealIntent({ |
| 816 | ownerId: 'A', |
| 817 | workspaceId: workspace.id, |
| 818 | intent: { intentDigest: '1'.repeat(64) }, |
| 819 | expectedReview: reviewOf(workspace) |
| 820 | }), |
| 821 | client.updateWorkspaceFileDraft(edit(workspace.id)) |
| 822 | ]); |
| 823 | assert.equal(sealed.state, 'READY'); |
| 824 | const stored = await client.getWorkspace({ |
| 825 | ownerId: 'A', |
| 826 | workspaceId: workspace.id |
| 827 | }); |
| 828 | assert.equal(stored.files[0].revision, 1); |
| 829 | assert.equal(stored.currentIntentId, null); |
| 830 | assert.equal(stored.state, 'DRAFT'); |
| 831 | }); |