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
js 233 lines 7.23 KB
Raw
1 const INTENT_ID_PATTERN = /^[A-Za-z0-9_-]{8}$/;
2 const FULL_SHA_PATTERN = /^[a-f0-9]{40}$/i;
3 function assertOwner(v) {
4 const x = String(v ?? '');
5 if (!x) throw new Error('Workspace owner is required');
6 return x;
7 }
8 function assertRepoId(v) {
9 const x = Number(v);
10 if (!Number.isSafeInteger(x) || x < 1)
11 throw new Error('Workspace repository ID is invalid');
12 return x;
13 }
14 function assertIntentId(v) {
15 const x = String(v || '');
16 if (!INTENT_ID_PATTERN.test(x))
17 throw new Error('Workspace intent ID is invalid');
18 return x;
19 }
20 function assertSha(v, label) {
21 const x = String(v || '');
22 if (!FULL_SHA_PATTERN.test(x)) throw new Error(`${label} SHA is invalid`);
23 return x;
24 }
25 function committedResult(intent, idempotent = true) {
26 return {
27 commitSha: assertSha(intent?.commitSha, 'Committed'),
28 branch: String(intent?.branch || intent?.targetBranch || ''),
29 idempotent
30 };
31 }
32 function materialMismatch(intent, ws, repoId, intentId) {
33 if (
34 Number(intent?.repoId) !== repoId ||
35 Number(ws?.repoId) !== repoId ||
36 String(ws?.id || '') !== String(intent?.workspaceId || '') ||
37 String(ws?.currentIntentId || '') !== intentId ||
38 String(ws?.baseBranch || '') !== String(intent?.baseBranch || '') ||
39 String(ws?.baseHeadSha || '') !== String(intent?.baseHeadSha || '') ||
40 String(ws?.baseTreeSha || '') !== String(intent?.baseTreeSha || '') ||
41 String(ws?.commitMessageDigest || '') !==
42 String(intent?.commitMessageDigest || '')
43 )
44 return true;
45 if (Array.isArray(intent?.files)) {
46 if (!Array.isArray(ws?.files) || intent.files.length !== ws.files.length)
47 return true;
48 return intent.files.some(
49 (f, i) =>
50 String(f.path) !== String(ws.files[i]?.path) ||
51 String(f.sourceBlobSha) !== String(ws.files[i]?.blobSha) ||
52 String(f.contentDigest) !== String(ws.files[i]?.draftDigest)
53 );
54 }
55 return (
56 String(ws?.path || '') !== String(intent?.path || '') ||
57 String(ws?.blobSha || '') !== String(intent?.sourceBlobSha || '') ||
58 String(ws?.draftDigest || '') !== String(intent?.contentDigest || '')
59 );
60 }
61 async function markStaleThenThrow(workspaces, ownerId, intentId, reason) {
62 try {
63 await workspaces.markStale({ ownerId, intentId, reason });
64 } catch {}
65 throw new Error(reason);
66 }
67 async function reconcileCandidate({
68 github,
69 workspaces,
70 ownerId,
71 repoId,
72 intent,
73 intentId
74 }) {
75 const candidate = assertSha(intent.candidateCommitSha, 'Candidate commit'),
76 branch = String(intent.targetBranch || ''),
77 existing = await github.getGitRef(repoId, branch, { allowMissing: true });
78 if (existing?.sha === candidate) {
79 await workspaces.completeCommit({
80 ownerId,
81 intentId,
82 commitSha: candidate,
83 branch
84 });
85 return { commitSha: candidate, branch, idempotent: true };
86 }
87 if (existing) throw new Error('Target branch exists at an unexpected commit');
88 const snapshot = await github.getBranchSnapshot(repoId, intent.baseBranch);
89 if (snapshot.headSha !== intent.baseHeadSha)
90 return markStaleThenThrow(
91 workspaces,
92 ownerId,
93 intentId,
94 'Base branch changed; commit intent is stale'
95 );
96 try {
97 await github.createGitBranchRef(repoId, { branch, sha: candidate });
98 } catch (error) {
99 const observed = await github.getGitRef(repoId, branch, {
100 allowMissing: true
101 });
102 if (observed?.sha !== candidate)
103 throw new Error(
104 `Unable to reconcile candidate branch after provider error: ${error?.message || 'unknown error'}`
105 );
106 }
107 await workspaces.completeCommit({
108 ownerId,
109 intentId,
110 commitSha: candidate,
111 branch
112 });
113 return { commitSha: candidate, branch, idempotent: true };
114 }
115 export async function commitWorkspaceIntent({
116 github,
117 workspaces,
118 ownerId,
119 repoId,
120 intentId
121 } = {}) {
122 if (!github) throw new Error('Workspace commit requires GitHub service');
123 if (!workspaces)
124 throw new Error('Workspace commit requires durable workspace store');
125 const owner = assertOwner(ownerId),
126 repositoryId = assertRepoId(repoId),
127 id = assertIntentId(intentId);
128 let intent = await workspaces.getIntent({ ownerId: owner, intentId: id });
129 if (!intent) throw new Error('Commit intent not found or expired');
130 if (Number(intent.repoId) !== repositoryId)
131 throw new Error('Commit intent repository mismatch');
132 if (intent.state === 'COMMITTED') return committedResult(intent, true);
133 const workspace = await workspaces.getWorkspace({
134 ownerId: owner,
135 workspaceId: intent.workspaceId
136 });
137 if (!workspace || materialMismatch(intent, workspace, repositoryId, id))
138 return markStaleThenThrow(
139 workspaces,
140 owner,
141 id,
142 'Workspace material changed; commit intent is stale'
143 );
144 const begun = await workspaces.beginCommit({ ownerId: owner, intentId: id });
145 intent = begun?.intent;
146 if (!intent) throw new Error('Commit intent could not begin');
147 if (begun.disposition === 'committed' || intent.state === 'COMMITTED')
148 return committedResult(intent, true);
149 if (intent.candidateCommitSha)
150 return reconcileCandidate({
151 github,
152 workspaces,
153 ownerId: owner,
154 repoId: repositoryId,
155 intent,
156 intentId: id
157 });
158 const snapshot = await github.getBranchSnapshot(
159 repositoryId,
160 intent.baseBranch
161 );
162 if (snapshot.headSha !== intent.baseHeadSha)
163 return markStaleThenThrow(
164 workspaces,
165 owner,
166 id,
167 'Base branch changed; commit intent is stale'
168 );
169 const existing = await github.getGitRef(repositoryId, intent.targetBranch, {
170 allowMissing: true
171 });
172 if (existing) throw new Error('Target branch already exists unexpectedly');
173 const files = Array.isArray(intent.files)
174 ? intent.files.map((f, i) => ({ intent: f, workspace: workspace.files[i] }))
175 : [
176 {
177 intent: { path: intent.path },
178 workspace: { draftText: workspace.draftText }
179 }
180 ];
181 const entries = [];
182 for (const item of files) {
183 const blob = await github.createGitBlob(
184 repositoryId,
185 item.workspace.draftText
186 );
187 entries.push({
188 path: item.intent.path,
189 mode: '100644',
190 type: 'blob',
191 sha: blob.sha
192 });
193 }
194 const tree = await github.createGitTree(repositoryId, {
195 baseTreeSha: intent.baseTreeSha,
196 entries
197 });
198 const commit = await github.createGitCommit(repositoryId, {
199 message: workspace.commitMessage,
200 treeSha: tree.sha,
201 parentSha: intent.baseHeadSha,
202 timestamp: intent.commitTimestamp
203 });
204 const commitSha = assertSha(commit.sha, 'Candidate commit');
205 await workspaces.setCandidateCommit({
206 ownerId: owner,
207 intentId: id,
208 commitSha
209 });
210 let idempotent = false;
211 try {
212 await github.createGitBranchRef(repositoryId, {
213 branch: intent.targetBranch,
214 sha: commitSha
215 });
216 } catch (error) {
217 const observed = await github.getGitRef(repositoryId, intent.targetBranch, {
218 allowMissing: true
219 });
220 if (observed?.sha !== commitSha)
221 throw new Error(
222 `Unable to reconcile target branch after provider error: ${error?.message || 'unknown error'}`
223 );
224 idempotent = true;
225 }
226 await workspaces.completeCommit({
227 ownerId: owner,
228 intentId: id,
229 commitSha,
230 branch: intent.targetBranch
231 });
232 return { commitSha, branch: intent.targetBranch, idempotent };
233 }