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 154 lines 4.48 KB
Raw
1 const AUDIT_METADATA_KEYS = Object.freeze(
2 new Set([
3 'repoId',
4 'workspaceId',
5 'intentId',
6 'path',
7 'contentDigest',
8 'commitMessageDigest',
9 'baseHeadSha',
10 'targetBranch',
11 'commitSha',
12 'branch',
13 'idempotent'
14 ])
15 );
16 function sanitizeAuditMetadata(value) {
17 if (!value || typeof value !== 'object' || Array.isArray(value)) return {};
18 const safe = {};
19 for (const [key, raw] of Object.entries(value)) {
20 if (!AUDIT_METADATA_KEYS.has(key)) continue;
21 if (typeof raw === 'number' && Number.isSafeInteger(raw)) {
22 safe[key] = raw;
23 continue;
24 }
25 if (typeof raw === 'boolean') {
26 safe[key] = raw;
27 continue;
28 }
29 if (typeof raw === 'string') safe[key] = raw.slice(0, 240);
30 }
31 return safe;
32 }
33 function withoutAuditMetadata(result) {
34 if (
35 !result ||
36 typeof result !== 'object' ||
37 Array.isArray(result) ||
38 !Object.hasOwn(result, 'audit')
39 )
40 return result;
41 const { audit, ...publicResult } = result;
42 return publicResult;
43 }
44 export class PluginRouter {
45 constructor({ registry, policy, audit, killSwitch = null } = {}) {
46 if (!registry) throw new Error('PluginRouter registry is required');
47 if (!policy) throw new Error('PluginRouter policy is required');
48 if (!audit) throw new Error('PluginRouter audit logger is required');
49 this.registry = registry;
50 this.policy = policy;
51 this.audit = audit;
52 this.killSwitch = killSwitch;
53 }
54 async dispatchCommand(command, context = {}) {
55 return this.#dispatch('resolveCommand', command, context);
56 }
57 async dispatchCallback(data, context = {}) {
58 return this.#dispatch('resolveCallback', data, context);
59 }
60 async dispatchMessage(payload, context = {}) {
61 return this.#dispatch('resolveMessage', payload, context);
62 }
63 async #dispatch(resolverName, input, context) {
64 for (const plugin of this.registry.getPlugins()) {
65 const resolver = plugin?.[resolverName];
66 if (typeof resolver !== 'function') continue;
67 const route = await resolver.call(plugin, input, context);
68 if (!route) continue;
69 const pluginId = plugin.manifest.id;
70 const actionId = route.actionId;
71 const registeredAction = this.registry.getAction(actionId);
72 const action =
73 registeredAction?.pluginId === pluginId ? registeredAction : null;
74 const decision = this.policy.evaluate({
75 action,
76 isOwner: context.isOwner === true,
77 approved: context.approved === true
78 });
79 const routeAudit = sanitizeAuditMetadata(route.audit);
80 this.audit.log({
81 event: 'action_attempt',
82 pluginId,
83 actionId,
84 risk: decision.risk,
85 outcome: decision.allowed
86 ? 'allowed'
87 : decision.requiresApproval
88 ? 'approval_required'
89 : 'denied',
90 reason: decision.reason,
91 ...routeAudit
92 });
93 if (!decision.allowed)
94 return {
95 ok: false,
96 pluginId,
97 actionId,
98 risk: decision.risk,
99 requiresApproval: decision.requiresApproval,
100 denied: !decision.requiresApproval,
101 reason: decision.reason,
102 approval: route.approval || null
103 };
104 if (this.killSwitch && (await this.killSwitch.blocks(decision.risk))) {
105 this.audit.log({
106 event: 'kill_switch_block',
107 pluginId,
108 actionId,
109 risk: decision.risk,
110 outcome: 'denied'
111 });
112 return {
113 ok: false,
114 pluginId,
115 actionId,
116 risk: decision.risk,
117 requiresApproval: false,
118 denied: true,
119 killSwitchLocked: true,
120 reason: 'Emergency kill switch is locked'
121 };
122 }
123 if (typeof route.execute !== 'function')
124 throw new Error(
125 `Plugin route execute function is required for ${actionId}`
126 );
127 try {
128 const result = await route.execute();
129 this.audit.log({
130 event: 'action_result',
131 pluginId,
132 actionId,
133 risk: decision.risk,
134 outcome: 'success',
135 ...routeAudit,
136 ...sanitizeAuditMetadata(result?.audit)
137 });
138 return withoutAuditMetadata(result);
139 } catch (error) {
140 this.audit.log({
141 event: 'action_result',
142 pluginId,
143 actionId,
144 risk: decision.risk,
145 outcome: 'error',
146 error: error?.message || 'Unknown plugin error',
147 ...routeAudit
148 });
149 throw error;
150 }
151 }
152 return null;
153 }
154 }