@opengeni/worker-bundle 0.7.4 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-BJUW5LCM.js +325 -0
- package/dist/chunk-BJUW5LCM.js.map +1 -0
- package/dist/host-export.d.ts +70 -0
- package/dist/host-export.js +7 -0
- package/dist/host-export.js.map +1 -0
- package/dist/index.d.ts +98 -6
- package/dist/index.js +2853 -834
- package/dist/index.js.map +1 -1
- package/dist/workflow-bundle.js +12358 -0
- package/package.json +18 -12
- package/src/activities/agent-turn.ts +2052 -554
- package/src/activities/capabilities.ts +9 -2
- package/src/activities/codex-capacity.ts +6 -11
- package/src/activities/codex-rotation.ts +75 -86
- package/src/activities/context-compaction.ts +104 -24
- package/src/activities/environment.ts +367 -92
- package/src/activities/expiring-material-renewal.ts +143 -0
- package/src/activities/goals.ts +4 -0
- package/src/activities/mcp-credentials.ts +38 -0
- package/src/activities/packs.ts +115 -2
- package/src/activities/parent-wake.ts +48 -38
- package/src/activities/run-credential-renewal.ts +70 -0
- package/src/activities/run-credentials.ts +132 -0
- package/src/activities/run-input.ts +23 -0
- package/src/activities/sandbox-lease.ts +1 -1
- package/src/activities/scheduled-tasks.ts +21 -0
- package/src/activities/session-state.ts +83 -12
- package/src/activities/streaming.ts +1 -0
- package/src/activities/toolspace-token-renewal.ts +57 -0
- package/src/activities/types.ts +44 -6
- package/src/activities/workspace-capture.ts +113 -93
- package/src/activities.ts +10 -2
- package/src/host-export-pump.ts +416 -0
- package/src/host-export.ts +22 -0
- package/src/http.ts +77 -28
- package/src/index.ts +360 -156
- package/src/sandbox-resume.ts +47 -1
- package/src/sandbox-routing.ts +1 -1
- package/src/worker-service-lifecycle.ts +104 -0
- package/src/workflows/activities.ts +13 -7
- package/src/workflows/session.ts +219 -23
- package/src/workflows.ts +1 -0
|
@@ -5,6 +5,8 @@ import {
|
|
|
5
5
|
type Settings,
|
|
6
6
|
} from "@opengeni/config";
|
|
7
7
|
import {
|
|
8
|
+
gitCredentialBindingIdForRepository,
|
|
9
|
+
gitCredentialProviderForRepository,
|
|
8
10
|
signDelegatedAccessToken,
|
|
9
11
|
type ConnectionCredentialsPort,
|
|
10
12
|
type GitCredentialProvider,
|
|
@@ -12,6 +14,9 @@ import {
|
|
|
12
14
|
type GitCredentials,
|
|
13
15
|
type ResourceRef,
|
|
14
16
|
type SandboxSecrets,
|
|
17
|
+
type SessionTurn,
|
|
18
|
+
type TurnInitiator,
|
|
19
|
+
type TurnInitiatorContext,
|
|
15
20
|
} from "@opengeni/contracts";
|
|
16
21
|
import {
|
|
17
22
|
loadVariableSetForRun as loadWorkspaceEnvironmentForRunFromDb,
|
|
@@ -29,8 +34,8 @@ export {
|
|
|
29
34
|
type WorkspaceEnvironmentForRun,
|
|
30
35
|
};
|
|
31
36
|
|
|
32
|
-
// §7.6
|
|
33
|
-
// provider can be called with the run's tenant context AND so the
|
|
37
|
+
// §7.6 connection-credential provider — the run's workspace identity, threaded so the connection-credential
|
|
38
|
+
// provider can be called with the run's tenant context AND so the workspace-scope cross-check
|
|
34
39
|
// cross-check has the run's workspace to assert the provider's echo against.
|
|
35
40
|
export type ConnectionScope = {
|
|
36
41
|
accountId: string;
|
|
@@ -39,12 +44,91 @@ export type ConnectionScope = {
|
|
|
39
44
|
|
|
40
45
|
export type GitTokenSeeds = Partial<Record<GitCredentialProvider, string>>;
|
|
41
46
|
export type GitTokenExpiries = Partial<Record<GitCredentialProvider, string>>;
|
|
47
|
+
export type GitCredentialBindingSeed = {
|
|
48
|
+
credentialBindingId: string;
|
|
49
|
+
provider: GitCredentialProvider;
|
|
50
|
+
token: string;
|
|
51
|
+
expiresAt?: string;
|
|
52
|
+
providerBindingCount?: number;
|
|
53
|
+
};
|
|
42
54
|
export type MintedRunGitCredentials = {
|
|
55
|
+
bindings: GitCredentialBindingSeed[];
|
|
56
|
+
// Compatibility views exist only when a provider has exactly one binding.
|
|
57
|
+
// A multi-binding provider intentionally has no provider-level alias.
|
|
43
58
|
gitTokens: GitTokenSeeds;
|
|
44
59
|
expiresAt: GitTokenExpiries;
|
|
45
60
|
};
|
|
46
61
|
|
|
47
|
-
|
|
62
|
+
export type GitHubTokenMintAuthorization = (selection: {
|
|
63
|
+
installationId: number;
|
|
64
|
+
repositoryIds: number[];
|
|
65
|
+
}) => Promise<void>;
|
|
66
|
+
|
|
67
|
+
export const TOOLSPACE_TOKEN_TTL_SECONDS = 60 * 60;
|
|
68
|
+
|
|
69
|
+
export type MintedSandboxToolspaceToken = {
|
|
70
|
+
token: string;
|
|
71
|
+
expiresAt: Date;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export async function mintSandboxToolspaceToken(
|
|
75
|
+
settings: Settings,
|
|
76
|
+
scope: ConnectionScope,
|
|
77
|
+
sessionId: string,
|
|
78
|
+
runId: string,
|
|
79
|
+
nowMs = Date.now(),
|
|
80
|
+
): Promise<MintedSandboxToolspaceToken | undefined> {
|
|
81
|
+
if (!settings.toolspaceEnabled || !settings.delegationSecret) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const expiresAtSeconds = Math.floor(nowMs / 1000) + TOOLSPACE_TOKEN_TTL_SECONDS;
|
|
85
|
+
const token = await signDelegatedAccessToken(settings.delegationSecret, {
|
|
86
|
+
accountId: scope.accountId,
|
|
87
|
+
workspaceId: scope.workspaceId,
|
|
88
|
+
subjectId: `sandbox:${runId}`,
|
|
89
|
+
subjectLabel: "sandbox toolspace",
|
|
90
|
+
permissions: ["toolspace:call"],
|
|
91
|
+
sessionId,
|
|
92
|
+
exp: expiresAtSeconds,
|
|
93
|
+
});
|
|
94
|
+
return { token, expiresAt: new Date(expiresAtSeconds * 1000) };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type GitCredentialAuthority = {
|
|
98
|
+
sessionId: string;
|
|
99
|
+
rootSessionId: string;
|
|
100
|
+
turnId: string;
|
|
101
|
+
attemptId: string;
|
|
102
|
+
executionGeneration: number;
|
|
103
|
+
initiator: TurnInitiator;
|
|
104
|
+
initiatorContext: TurnInitiatorContext;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export function gitCredentialAuthorityForTurn(input: {
|
|
108
|
+
sessionId: string;
|
|
109
|
+
rootSessionId: string;
|
|
110
|
+
attemptId: string;
|
|
111
|
+
turn: Pick<SessionTurn, "id" | "executionGeneration" | "initiator" | "initiatorContext">;
|
|
112
|
+
}): GitCredentialAuthority {
|
|
113
|
+
return {
|
|
114
|
+
sessionId: input.sessionId,
|
|
115
|
+
rootSessionId: input.rootSessionId,
|
|
116
|
+
turnId: input.turn.id,
|
|
117
|
+
attemptId: input.attemptId,
|
|
118
|
+
executionGeneration: input.turn.executionGeneration,
|
|
119
|
+
initiator: input.turn.initiator,
|
|
120
|
+
initiatorContext: input.turn.initiatorContext,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
type RunGitCredentialOptions = {
|
|
125
|
+
scope?: ConnectionScope;
|
|
126
|
+
authority?: GitCredentialAuthority;
|
|
127
|
+
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
128
|
+
authorizeGitHubTokenMint?: GitHubTokenMintAuthorization;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// §7.6 connection-credential provider — load the run's workspace environment, delegating the DECRYPT to a
|
|
48
132
|
// host `sandboxSecrets` provider when one is bound (the host owns the secret
|
|
49
133
|
// vault + encryption key in embedded/separate topologies) and otherwise running
|
|
50
134
|
// today's local `environmentsEncryptionKeyBytes`-keyed decrypt byte-for-byte.
|
|
@@ -72,7 +156,7 @@ export async function loadWorkspaceEnvironmentForRunWithCredentials(
|
|
|
72
156
|
workspaceId: scope.workspaceId,
|
|
73
157
|
variableSetId: environmentId,
|
|
74
158
|
});
|
|
75
|
-
//
|
|
159
|
+
// workspace-scope cross-check: the provider must echo THIS run's workspace before we apply its
|
|
76
160
|
// decrypted values into the sandbox.
|
|
77
161
|
assertWorkspaceEcho("sandboxSecrets", scope, secrets.workspaceId);
|
|
78
162
|
return {
|
|
@@ -83,7 +167,7 @@ export async function loadWorkspaceEnvironmentForRunWithCredentials(
|
|
|
83
167
|
};
|
|
84
168
|
}
|
|
85
169
|
|
|
86
|
-
//
|
|
170
|
+
// Workspace-scope cross-check. A credential provider echoes the workspace it scoped
|
|
87
171
|
// the credential to; we ASSERT it equals the run's workspace BEFORE the caller
|
|
88
172
|
// injects the credential. A host mapping bug returning tenant B's creds for a
|
|
89
173
|
// tenant-A run hard-throws here instead of landing tenant B's token in tenant
|
|
@@ -104,17 +188,15 @@ export async function sandboxEnvironmentForRun(
|
|
|
104
188
|
settings: Settings,
|
|
105
189
|
resources: ResourceRef[],
|
|
106
190
|
workspaceEnvironment: Record<string, string> = {},
|
|
107
|
-
// §7.6
|
|
191
|
+
// §7.6 connection-credential provider - optional host git-credential provider + the run scope it needs
|
|
108
192
|
// (unset, the standalone default → self-mint from `settings` byte-for-byte).
|
|
109
193
|
// `skipGitHubToken` (Stage D): a connected-machine turn skips the inert platform
|
|
110
194
|
// token mint entirely and returns the stable base env unchanged. `deferGitHubToken`
|
|
111
195
|
// is the lazy CLOUD path: apply stable git-auth pointers now, mint only the token
|
|
112
196
|
// value later. `= {}` default so the non-optional reads below are safe.
|
|
113
|
-
options: {
|
|
197
|
+
options: RunGitCredentialOptions & {
|
|
114
198
|
skipGitHubToken?: boolean;
|
|
115
199
|
deferGitHubToken?: boolean;
|
|
116
|
-
scope?: ConnectionScope;
|
|
117
|
-
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
118
200
|
sessionId?: string;
|
|
119
201
|
runId?: string;
|
|
120
202
|
} = {},
|
|
@@ -123,7 +205,9 @@ export async function sandboxEnvironmentForRun(
|
|
|
123
205
|
gitToken?: string;
|
|
124
206
|
gitTokens?: GitTokenSeeds;
|
|
125
207
|
gitTokenExpiresAt?: GitTokenExpiries;
|
|
208
|
+
gitCredentialBindings?: GitCredentialBindingSeed[];
|
|
126
209
|
toolspaceToken?: string;
|
|
210
|
+
toolspaceTokenExpiresAt?: Date;
|
|
127
211
|
}> {
|
|
128
212
|
// Precedence: deployment allowlist < git identity < workspace environment
|
|
129
213
|
// < backend-aware HOME (the STABLE base, shared with the API-direct attach
|
|
@@ -152,26 +236,15 @@ export async function sandboxEnvironmentForRun(
|
|
|
152
236
|
// budgeted, approval-tools excluded). Delivery mirrors the docker path: the
|
|
153
237
|
// caller threads it OFF-MANIFEST as the seed the runtime writes to
|
|
154
238
|
// $OPENGENI_TOOLSPACE_TOKEN_FILE over the box's exec channel.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
options.runId
|
|
162
|
-
) {
|
|
163
|
-
toolspaceToken = await signDelegatedAccessToken(settings.delegationSecret, {
|
|
164
|
-
accountId: options.scope.accountId,
|
|
165
|
-
workspaceId: options.scope.workspaceId,
|
|
166
|
-
subjectId: `sandbox:${options.runId}`,
|
|
167
|
-
subjectLabel: "sandbox toolspace",
|
|
168
|
-
permissions: ["toolspace:call"],
|
|
169
|
-
sessionId: options.sessionId,
|
|
170
|
-
exp: Math.floor(Date.now() / 1000) + 60 * 60,
|
|
171
|
-
});
|
|
239
|
+
const toolspaceScope = options.scope;
|
|
240
|
+
const toolspaceToken =
|
|
241
|
+
toolspaceScope && options.sessionId && options.runId
|
|
242
|
+
? await mintSandboxToolspaceToken(settings, toolspaceScope, options.sessionId, options.runId)
|
|
243
|
+
: undefined;
|
|
244
|
+
if (toolspaceToken && toolspaceScope) {
|
|
172
245
|
environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(
|
|
173
246
|
settings,
|
|
174
|
-
|
|
247
|
+
toolspaceScope.workspaceId,
|
|
175
248
|
);
|
|
176
249
|
}
|
|
177
250
|
const selections = gitCredentialSelections(resources);
|
|
@@ -185,14 +258,30 @@ export async function sandboxEnvironmentForRun(
|
|
|
185
258
|
// (validateNoEnvironmentDelta). The API-direct viewer attach path already drops the
|
|
186
259
|
// token under this exact contract — proof a box runs fine without it.
|
|
187
260
|
if (selections.length === 0 || options.skipGitHubToken) {
|
|
188
|
-
return {
|
|
261
|
+
return {
|
|
262
|
+
environment,
|
|
263
|
+
...(toolspaceToken
|
|
264
|
+
? {
|
|
265
|
+
toolspaceToken: toolspaceToken.token,
|
|
266
|
+
toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
|
|
267
|
+
}
|
|
268
|
+
: {}),
|
|
269
|
+
};
|
|
189
270
|
}
|
|
190
271
|
if (options.deferGitHubToken) {
|
|
191
272
|
applyGitAuthPointerEnvironment(
|
|
192
273
|
environment,
|
|
193
274
|
await resolveRunGitIdentityWithSelections(settings, selections, options),
|
|
194
275
|
);
|
|
195
|
-
return {
|
|
276
|
+
return {
|
|
277
|
+
environment,
|
|
278
|
+
...(toolspaceToken
|
|
279
|
+
? {
|
|
280
|
+
toolspaceToken: toolspaceToken.token,
|
|
281
|
+
toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
|
|
282
|
+
}
|
|
283
|
+
: {}),
|
|
284
|
+
};
|
|
196
285
|
}
|
|
197
286
|
// Run-scoped sandbox preparation for repository resources. GitHub retains the
|
|
198
287
|
// legacy request shape and standalone self-mint path. Non-GitHub providers are
|
|
@@ -215,46 +304,69 @@ export async function sandboxEnvironmentForRun(
|
|
|
215
304
|
...(minted.gitTokens.github ? { gitToken: minted.gitTokens.github } : {}),
|
|
216
305
|
...(Object.keys(minted.gitTokens).length > 0 ? { gitTokens: minted.gitTokens } : {}),
|
|
217
306
|
...(Object.keys(minted.expiresAt).length > 0 ? { gitTokenExpiresAt: minted.expiresAt } : {}),
|
|
218
|
-
...(
|
|
307
|
+
...(minted.bindings.length > 0 ? { gitCredentialBindings: minted.bindings } : {}),
|
|
308
|
+
...(toolspaceToken
|
|
309
|
+
? {
|
|
310
|
+
toolspaceToken: toolspaceToken.token,
|
|
311
|
+
toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
|
|
312
|
+
}
|
|
313
|
+
: {}),
|
|
219
314
|
};
|
|
220
315
|
}
|
|
221
316
|
|
|
222
317
|
export async function mintRunGitCredentials(
|
|
223
318
|
settings: Settings,
|
|
224
319
|
resources: ResourceRef[],
|
|
225
|
-
options: {
|
|
226
|
-
scope?: ConnectionScope;
|
|
227
|
-
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
228
|
-
} = {},
|
|
320
|
+
options: RunGitCredentialOptions = {},
|
|
229
321
|
): Promise<MintedRunGitCredentials | undefined> {
|
|
230
322
|
const selections = gitCredentialSelections(resources);
|
|
231
323
|
if (selections.length === 0) {
|
|
232
324
|
return undefined;
|
|
233
325
|
}
|
|
234
326
|
const minted = await mintRunGitTokensWithIdentity(settings, selections, options);
|
|
235
|
-
return
|
|
236
|
-
? { gitTokens: minted.gitTokens, expiresAt: minted.expiresAt }
|
|
327
|
+
return minted.bindings.length > 0
|
|
328
|
+
? { bindings: minted.bindings, gitTokens: minted.gitTokens, expiresAt: minted.expiresAt }
|
|
237
329
|
: undefined;
|
|
238
330
|
}
|
|
239
331
|
|
|
332
|
+
export async function mintRunGitCredentialBinding(
|
|
333
|
+
settings: Settings,
|
|
334
|
+
resources: ResourceRef[],
|
|
335
|
+
provider: GitCredentialProvider,
|
|
336
|
+
credentialBindingId: string,
|
|
337
|
+
options: RunGitCredentialOptions = {},
|
|
338
|
+
): Promise<GitCredentialBindingSeed | undefined> {
|
|
339
|
+
const selections = gitCredentialSelections(resources);
|
|
340
|
+
const selection = selections.find(
|
|
341
|
+
(candidate) =>
|
|
342
|
+
candidate.provider === provider && candidate.credentialBindingId === credentialBindingId,
|
|
343
|
+
);
|
|
344
|
+
if (!selection) {
|
|
345
|
+
return undefined;
|
|
346
|
+
}
|
|
347
|
+
const minted = await mintRunGitTokensWithIdentity(settings, [selection], options);
|
|
348
|
+
const binding = minted.bindings[0];
|
|
349
|
+
if (binding) {
|
|
350
|
+
binding.providerBindingCount = selections.filter(
|
|
351
|
+
(candidate) => candidate.provider === selection.provider,
|
|
352
|
+
).length;
|
|
353
|
+
}
|
|
354
|
+
return binding;
|
|
355
|
+
}
|
|
356
|
+
|
|
240
357
|
export async function mintRunGitTokens(
|
|
241
358
|
settings: Settings,
|
|
242
359
|
resources: ResourceRef[],
|
|
243
|
-
options: {
|
|
244
|
-
scope?: ConnectionScope;
|
|
245
|
-
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
246
|
-
} = {},
|
|
360
|
+
options: RunGitCredentialOptions = {},
|
|
247
361
|
): Promise<GitTokenSeeds | undefined> {
|
|
248
|
-
|
|
362
|
+
const tokens = (await mintRunGitCredentials(settings, resources, options))?.gitTokens;
|
|
363
|
+
return tokens && Object.keys(tokens).length > 0 ? tokens : undefined;
|
|
249
364
|
}
|
|
250
365
|
|
|
251
366
|
export async function mintRunGitToken(
|
|
252
367
|
settings: Settings,
|
|
253
368
|
resources: ResourceRef[],
|
|
254
|
-
options: {
|
|
255
|
-
scope?: ConnectionScope;
|
|
256
|
-
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
257
|
-
} = {},
|
|
369
|
+
options: RunGitCredentialOptions = {},
|
|
258
370
|
): Promise<string | undefined> {
|
|
259
371
|
return (await mintRunGitTokens(settings, resources, options))?.github;
|
|
260
372
|
}
|
|
@@ -262,10 +374,7 @@ export async function mintRunGitToken(
|
|
|
262
374
|
export async function resolveRunGitIdentity(
|
|
263
375
|
settings: Settings,
|
|
264
376
|
resources: ResourceRef[],
|
|
265
|
-
options: {
|
|
266
|
-
scope?: ConnectionScope;
|
|
267
|
-
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
268
|
-
} = {},
|
|
377
|
+
options: RunGitCredentialOptions = {},
|
|
269
378
|
): Promise<{ name: string; email: string } | null> {
|
|
270
379
|
const selections = gitCredentialSelections(resources);
|
|
271
380
|
if (selections.length === 0) {
|
|
@@ -277,38 +386,54 @@ export async function resolveRunGitIdentity(
|
|
|
277
386
|
async function mintRunGitTokensWithIdentity(
|
|
278
387
|
settings: Settings,
|
|
279
388
|
selections: GitCredentialSelection[],
|
|
280
|
-
options:
|
|
281
|
-
scope?: ConnectionScope;
|
|
282
|
-
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
283
|
-
},
|
|
389
|
+
options: RunGitCredentialOptions,
|
|
284
390
|
): Promise<{
|
|
391
|
+
bindings: GitCredentialBindingSeed[];
|
|
285
392
|
gitTokens: GitTokenSeeds;
|
|
286
393
|
expiresAt: GitTokenExpiries;
|
|
287
394
|
identity: { name: string; email: string } | null;
|
|
288
395
|
}> {
|
|
289
|
-
const
|
|
290
|
-
const expiresAt: GitTokenExpiries = {};
|
|
396
|
+
const bindings: GitCredentialBindingSeed[] = [];
|
|
291
397
|
let identity: { name: string; email: string } | null = null;
|
|
292
398
|
for (const selection of selections) {
|
|
293
399
|
let token: string | null = null;
|
|
400
|
+
let tokenExpiresAt: string | undefined;
|
|
401
|
+
if (
|
|
402
|
+
selection.provider === "github" &&
|
|
403
|
+
selection.installationId > 0 &&
|
|
404
|
+
selection.repositoryIds.length > 0
|
|
405
|
+
) {
|
|
406
|
+
// This callback belongs immediately next to the side effect. Turn startup
|
|
407
|
+
// performs its own admission check, but lazy provisioning and proactive
|
|
408
|
+
// renewal can happen much later in an intentionally unbounded run. Recheck
|
|
409
|
+
// the current workspace binding before every host-brokered or built-in
|
|
410
|
+
// GitHub installation-token mint.
|
|
411
|
+
await options.authorizeGitHubTokenMint?.({
|
|
412
|
+
installationId: selection.installationId,
|
|
413
|
+
repositoryIds: selection.repositoryIds,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
294
416
|
if (options?.gitCredentials && options.scope) {
|
|
295
|
-
const request = gitCredentialsRequestForSelection(
|
|
417
|
+
const request = gitCredentialsRequestForSelection(
|
|
418
|
+
options.scope,
|
|
419
|
+
requireGitCredentialAuthority(options),
|
|
420
|
+
selection,
|
|
421
|
+
"token",
|
|
422
|
+
);
|
|
296
423
|
const minted: GitCredentials = await options.gitCredentials(request);
|
|
297
|
-
//
|
|
424
|
+
// workspace-scope cross-check: assert the provider scoped the token to THIS run's workspace
|
|
298
425
|
// before accepting the token for clone seeding.
|
|
299
426
|
assertWorkspaceEcho("gitCredentials", options.scope, minted.workspaceId);
|
|
427
|
+
assertGitCredentialBindingEcho(selection, request, minted);
|
|
300
428
|
if (!minted.token) {
|
|
301
429
|
throw new Error(
|
|
302
430
|
"connection-credential provider (gitCredentials) did not return a token for a token request",
|
|
303
431
|
);
|
|
304
432
|
}
|
|
305
433
|
token = minted.token;
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
minted.expiresAt,
|
|
310
|
-
);
|
|
311
|
-
}
|
|
434
|
+
tokenExpiresAt = minted.expiresAt
|
|
435
|
+
? validatedGitCredentialExpiry(selection.provider, minted.expiresAt)
|
|
436
|
+
: undefined;
|
|
312
437
|
if (minted.identity) {
|
|
313
438
|
identity = minted.identity;
|
|
314
439
|
} else if (selection.provider === "github") {
|
|
@@ -320,16 +445,33 @@ async function mintRunGitTokensWithIdentity(
|
|
|
320
445
|
repositoryIds: selection.repositoryIds,
|
|
321
446
|
});
|
|
322
447
|
token = minted.token;
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
448
|
+
tokenExpiresAt = minted.expiresAt
|
|
449
|
+
? validatedGitCredentialExpiry("github", minted.expiresAt)
|
|
450
|
+
: undefined;
|
|
326
451
|
identity = githubAppBotIdentity(settings);
|
|
327
452
|
}
|
|
328
453
|
if (token) {
|
|
329
|
-
|
|
454
|
+
bindings.push({
|
|
455
|
+
credentialBindingId: selection.credentialBindingId,
|
|
456
|
+
provider: selection.provider,
|
|
457
|
+
token,
|
|
458
|
+
...(tokenExpiresAt ? { expiresAt: tokenExpiresAt } : {}),
|
|
459
|
+
});
|
|
330
460
|
}
|
|
331
461
|
}
|
|
332
|
-
|
|
462
|
+
const gitTokens: GitTokenSeeds = {};
|
|
463
|
+
const expiresAt: GitTokenExpiries = {};
|
|
464
|
+
const counts = new Map<GitCredentialProvider, number>();
|
|
465
|
+
for (const binding of bindings) {
|
|
466
|
+
counts.set(binding.provider, (counts.get(binding.provider) ?? 0) + 1);
|
|
467
|
+
}
|
|
468
|
+
for (const binding of bindings) {
|
|
469
|
+
binding.providerBindingCount = counts.get(binding.provider) ?? 1;
|
|
470
|
+
if (counts.get(binding.provider) !== 1) continue;
|
|
471
|
+
gitTokens[binding.provider] = binding.token;
|
|
472
|
+
if (binding.expiresAt) expiresAt[binding.provider] = binding.expiresAt;
|
|
473
|
+
}
|
|
474
|
+
return { bindings, gitTokens, expiresAt, identity };
|
|
333
475
|
}
|
|
334
476
|
|
|
335
477
|
function validatedGitCredentialExpiry(provider: GitCredentialProvider, value: string): string {
|
|
@@ -343,18 +485,20 @@ function validatedGitCredentialExpiry(provider: GitCredentialProvider, value: st
|
|
|
343
485
|
async function resolveRunGitIdentityWithSelections(
|
|
344
486
|
settings: Settings,
|
|
345
487
|
selections: GitCredentialSelection[],
|
|
346
|
-
options:
|
|
347
|
-
scope?: ConnectionScope;
|
|
348
|
-
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
349
|
-
},
|
|
488
|
+
options: RunGitCredentialOptions,
|
|
350
489
|
): Promise<{ name: string; email: string } | null> {
|
|
351
490
|
let identity: { name: string; email: string } | null = null;
|
|
352
491
|
for (const selection of selections) {
|
|
353
492
|
if (options.gitCredentials && options.scope) {
|
|
354
|
-
const
|
|
355
|
-
|
|
493
|
+
const request = gitCredentialsRequestForSelection(
|
|
494
|
+
options.scope,
|
|
495
|
+
requireGitCredentialAuthority(options),
|
|
496
|
+
selection,
|
|
497
|
+
"identity",
|
|
356
498
|
);
|
|
499
|
+
const resolved: GitCredentials = await options.gitCredentials(request);
|
|
357
500
|
assertWorkspaceEcho("gitCredentials", options.scope, resolved.workspaceId);
|
|
501
|
+
assertGitCredentialBindingEcho(selection, request, resolved);
|
|
358
502
|
if (resolved.identity) {
|
|
359
503
|
identity = resolved.identity;
|
|
360
504
|
} else if (selection.provider === "github") {
|
|
@@ -369,6 +513,10 @@ async function resolveRunGitIdentityWithSelections(
|
|
|
369
513
|
|
|
370
514
|
type GitCredentialSelection = {
|
|
371
515
|
provider: GitCredentialProvider;
|
|
516
|
+
credentialBindingId: string;
|
|
517
|
+
explicitCredentialBinding: boolean;
|
|
518
|
+
requireBindingEcho: boolean;
|
|
519
|
+
providerHost?: string;
|
|
372
520
|
installationId: number;
|
|
373
521
|
repositoryIds: number[];
|
|
374
522
|
repositoryRefs: GitCredentialRepositoryRef[];
|
|
@@ -376,31 +524,106 @@ type GitCredentialSelection = {
|
|
|
376
524
|
|
|
377
525
|
function gitCredentialsRequestForSelection(
|
|
378
526
|
scope: ConnectionScope,
|
|
527
|
+
authority: GitCredentialAuthority,
|
|
379
528
|
selection: GitCredentialSelection,
|
|
380
529
|
purpose?: "token" | "identity",
|
|
381
530
|
): Parameters<NonNullable<ConnectionCredentialsPort["gitCredentials"]>>[0] {
|
|
382
531
|
const legacy = {
|
|
383
532
|
accountId: scope.accountId,
|
|
384
533
|
workspaceId: scope.workspaceId,
|
|
534
|
+
...authority,
|
|
385
535
|
...(purpose ? { purpose } : {}),
|
|
386
536
|
installationId: selection.installationId,
|
|
387
537
|
repositoryIds: selection.repositoryIds,
|
|
388
538
|
};
|
|
539
|
+
const bindingEcho = selection.requireBindingEcho
|
|
540
|
+
? {
|
|
541
|
+
credentialBindingId: selection.credentialBindingId,
|
|
542
|
+
provider: selection.provider,
|
|
543
|
+
...(selection.providerHost ? { providerHost: selection.providerHost } : {}),
|
|
544
|
+
}
|
|
545
|
+
: {};
|
|
389
546
|
if (selection.provider === "github") {
|
|
390
547
|
return {
|
|
391
548
|
...legacy,
|
|
549
|
+
...bindingEcho,
|
|
392
550
|
repositoryRefs: selection.repositoryRefs,
|
|
393
551
|
};
|
|
394
552
|
}
|
|
395
553
|
return {
|
|
396
554
|
...legacy,
|
|
555
|
+
...bindingEcho,
|
|
397
556
|
provider: selection.provider,
|
|
398
557
|
repositoryRefs: selection.repositoryRefs,
|
|
399
558
|
};
|
|
400
559
|
}
|
|
401
560
|
|
|
561
|
+
function requireGitCredentialAuthority(options: RunGitCredentialOptions): GitCredentialAuthority {
|
|
562
|
+
if (!options.authority) {
|
|
563
|
+
throw new Error("host git credential resolution requires immutable session turn authority");
|
|
564
|
+
}
|
|
565
|
+
return options.authority;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function assertGitCredentialBindingEcho(
|
|
569
|
+
selection: GitCredentialSelection,
|
|
570
|
+
request: Parameters<NonNullable<ConnectionCredentialsPort["gitCredentials"]>>[0],
|
|
571
|
+
minted: GitCredentials,
|
|
572
|
+
): void {
|
|
573
|
+
if (!selection.requireBindingEcho) return;
|
|
574
|
+
if (minted.credentialBindingId !== request.credentialBindingId) {
|
|
575
|
+
throw new Error(
|
|
576
|
+
`connection-credential provider (${selection.provider}) returned the wrong credential binding`,
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
if (minted.provider !== selection.provider) {
|
|
580
|
+
throw new Error(
|
|
581
|
+
`connection-credential provider (${selection.provider}) returned the wrong provider echo`,
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
if (request.providerHost && minted.providerHost !== request.providerHost) {
|
|
585
|
+
throw new Error(
|
|
586
|
+
`connection-credential provider (${selection.provider}) returned the wrong provider host echo`,
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* The GitHub App installation + repository ids a run's git-credential mint
|
|
593
|
+
* would use for these resources, or null when no GitHub token would be minted.
|
|
594
|
+
* Derived from gitCredentialSelections so workspace-authorization rechecks
|
|
595
|
+
* cover exactly the ids that reach createGitHubAppInstallationToken —
|
|
596
|
+
* including legacy string-typed installationId/repositoryId refs, which the
|
|
597
|
+
* mint path coerces via positiveInteger.
|
|
598
|
+
*/
|
|
599
|
+
export function gitHubTokenMintSelections(
|
|
600
|
+
resources: ResourceRef[],
|
|
601
|
+
): Array<{ installationId: number; repositoryIds: number[] }> {
|
|
602
|
+
return gitCredentialSelections(resources)
|
|
603
|
+
.filter(
|
|
604
|
+
(candidate) =>
|
|
605
|
+
candidate.provider === "github" &&
|
|
606
|
+
candidate.installationId > 0 &&
|
|
607
|
+
candidate.repositoryIds.length > 0,
|
|
608
|
+
)
|
|
609
|
+
.map(({ installationId, repositoryIds }) => ({ installationId, repositoryIds }));
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** @deprecated Use gitHubTokenMintSelections for multi-installation sessions. */
|
|
613
|
+
export function gitHubTokenMintSelection(
|
|
614
|
+
resources: ResourceRef[],
|
|
615
|
+
): { installationId: number; repositoryIds: number[] } | null {
|
|
616
|
+
const selections = gitHubTokenMintSelections(resources);
|
|
617
|
+
if (selections.length > 1) {
|
|
618
|
+
throw new Error("GitHub resources span multiple credential bindings");
|
|
619
|
+
}
|
|
620
|
+
return selections[0] ?? null;
|
|
621
|
+
}
|
|
622
|
+
|
|
402
623
|
function gitCredentialSelections(resources: ResourceRef[]): GitCredentialSelection[] {
|
|
403
|
-
const
|
|
624
|
+
const byBinding = new Map<string, GitCredentialSelection>();
|
|
625
|
+
const remoteBindings = new Map<string, string>();
|
|
626
|
+
const bindingProviders = new Map<string, GitCredentialProvider>();
|
|
404
627
|
for (const resource of resources) {
|
|
405
628
|
if (resource.kind !== "repository") {
|
|
406
629
|
continue;
|
|
@@ -409,45 +632,95 @@ function gitCredentialSelections(resources: ResourceRef[]): GitCredentialSelecti
|
|
|
409
632
|
if (!provider) {
|
|
410
633
|
continue;
|
|
411
634
|
}
|
|
412
|
-
const
|
|
635
|
+
const installationId =
|
|
636
|
+
provider === "github"
|
|
637
|
+
? (positiveInteger(resource.githubInstallationId ?? resource.installationId) ?? 0)
|
|
638
|
+
: 0;
|
|
639
|
+
const credentialBindingId = gitCredentialBindingIdForRepository(resource, provider)!;
|
|
640
|
+
const bindingKey = `${provider}\u0000${credentialBindingId}`;
|
|
641
|
+
const boundProvider = bindingProviders.get(credentialBindingId);
|
|
642
|
+
if (boundProvider && boundProvider !== provider) {
|
|
643
|
+
throw new Error(
|
|
644
|
+
`credential binding ${credentialBindingId} is assigned to multiple Git providers`,
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
bindingProviders.set(credentialBindingId, provider);
|
|
648
|
+
const normalizedRemote = normalizedGitRemote(resource.uri);
|
|
649
|
+
const claimedBinding = remoteBindings.get(normalizedRemote);
|
|
650
|
+
if (claimedBinding && claimedBinding !== bindingKey) {
|
|
651
|
+
throw new Error(
|
|
652
|
+
`repository remote ${resource.uri} is claimed by multiple credential bindings`,
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
remoteBindings.set(normalizedRemote, bindingKey);
|
|
656
|
+
const entry = byBinding.get(bindingKey) ?? {
|
|
413
657
|
provider,
|
|
658
|
+
credentialBindingId,
|
|
659
|
+
explicitCredentialBinding: Boolean(resource.credentialBindingId),
|
|
660
|
+
requireBindingEcho: false,
|
|
414
661
|
installationId: 0,
|
|
415
662
|
repositoryIds: [],
|
|
416
663
|
repositoryRefs: [],
|
|
417
664
|
};
|
|
665
|
+
entry.explicitCredentialBinding ||= Boolean(resource.credentialBindingId);
|
|
418
666
|
const ref = gitCredentialRepositoryRef(resource, provider);
|
|
419
667
|
entry.repositoryRefs.push(ref);
|
|
420
668
|
if (provider === "github") {
|
|
421
|
-
const installationId = positiveInteger(
|
|
422
|
-
resource.githubInstallationId ?? resource.installationId,
|
|
423
|
-
);
|
|
424
669
|
const repositoryId = positiveInteger(resource.githubRepositoryId ?? resource.repositoryId);
|
|
425
670
|
if (installationId && repositoryId) {
|
|
426
671
|
if (entry.installationId > 0 && entry.installationId !== installationId) {
|
|
427
|
-
throw new Error(
|
|
672
|
+
throw new Error(
|
|
673
|
+
`GitHub credential binding ${credentialBindingId} spans multiple installations`,
|
|
674
|
+
);
|
|
428
675
|
}
|
|
429
676
|
entry.installationId = installationId;
|
|
430
677
|
entry.repositoryIds.push(repositoryId);
|
|
431
678
|
}
|
|
432
679
|
}
|
|
433
|
-
|
|
680
|
+
byBinding.set(bindingKey, entry);
|
|
681
|
+
}
|
|
682
|
+
const selections = [...byBinding.values()];
|
|
683
|
+
const providerCounts = new Map<GitCredentialProvider, number>();
|
|
684
|
+
for (const selection of selections) {
|
|
685
|
+
providerCounts.set(selection.provider, (providerCounts.get(selection.provider) ?? 0) + 1);
|
|
686
|
+
const hosts = new Set(
|
|
687
|
+
selection.repositoryRefs.map((ref) => normalizedGitHost(ref.uri)).filter(Boolean),
|
|
688
|
+
);
|
|
689
|
+
const [providerHost] = hosts;
|
|
690
|
+
if (hosts.size === 1 && providerHost) selection.providerHost = providerHost;
|
|
691
|
+
}
|
|
692
|
+
for (const selection of selections) {
|
|
693
|
+
selection.requireBindingEcho =
|
|
694
|
+
selection.explicitCredentialBinding || (providerCounts.get(selection.provider) ?? 0) > 1;
|
|
695
|
+
}
|
|
696
|
+
return selections;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function normalizedGitHost(uri: string): string {
|
|
700
|
+
try {
|
|
701
|
+
return new URL(uri).host.toLowerCase();
|
|
702
|
+
} catch {
|
|
703
|
+
return "";
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
function normalizedGitRemote(uri: string): string {
|
|
708
|
+
try {
|
|
709
|
+
const url = new URL(uri);
|
|
710
|
+
return `${url.protocol.toLowerCase()}//${url.host.toLowerCase()}${url.pathname.replace(/\/+$/, "").replace(/\.git$/, "")}`;
|
|
711
|
+
} catch {
|
|
712
|
+
return uri
|
|
713
|
+
.trim()
|
|
714
|
+
.replace(/\/+$/, "")
|
|
715
|
+
.replace(/\.git$/, "")
|
|
716
|
+
.toLowerCase();
|
|
434
717
|
}
|
|
435
|
-
return [...byProvider.values()];
|
|
436
718
|
}
|
|
437
719
|
|
|
438
720
|
function repositoryCredentialProvider(
|
|
439
721
|
resource: Extract<ResourceRef, { kind: "repository" }>,
|
|
440
722
|
): GitCredentialProvider | null {
|
|
441
|
-
|
|
442
|
-
return resource.provider;
|
|
443
|
-
}
|
|
444
|
-
if (
|
|
445
|
-
positiveInteger(resource.githubInstallationId) &&
|
|
446
|
-
positiveInteger(resource.githubRepositoryId)
|
|
447
|
-
) {
|
|
448
|
-
return "github";
|
|
449
|
-
}
|
|
450
|
-
return null;
|
|
723
|
+
return gitCredentialProviderForRepository(resource);
|
|
451
724
|
}
|
|
452
725
|
|
|
453
726
|
function gitCredentialRepositoryRef(
|
|
@@ -456,6 +729,8 @@ function gitCredentialRepositoryRef(
|
|
|
456
729
|
): GitCredentialRepositoryRef {
|
|
457
730
|
return {
|
|
458
731
|
provider,
|
|
732
|
+
...(resource.credentialBindingId ? { credentialBindingId: resource.credentialBindingId } : {}),
|
|
733
|
+
...(resource.access ? { access: resource.access } : {}),
|
|
459
734
|
uri: resource.uri,
|
|
460
735
|
ref: resource.ref,
|
|
461
736
|
...(resource.repositoryId !== undefined
|