@opengeni/worker-bundle 0.8.3 → 0.11.1
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/index.js +2735 -905
- package/dist/index.js.map +1 -1
- package/dist/workflow-bundle.js +7 -4
- package/package.json +11 -11
- package/src/activities/agent-turn.ts +1123 -462
- package/src/activities/capabilities.ts +3 -1
- package/src/activities/codex-fleet-shadow.ts +296 -0
- package/src/activities/environment.ts +142 -11
- package/src/activities/goals.ts +13 -79
- package/src/activities/rig-verification.ts +897 -244
- package/src/activities/run-input.ts +186 -13
- package/src/activities/sandbox-lease.ts +162 -84
- package/src/activities/scheduled-tasks.ts +42 -27
- package/src/observability-metrics.ts +83 -15
- package/src/sandbox-resume.ts +249 -94
- package/src/sandbox-routing.ts +597 -2
- package/src/workflows/activities.ts +4 -3
- package/src/workflows/rig-verification.ts +3 -1
|
@@ -41,6 +41,7 @@ export async function settingsWithSessionMcpServersForRun(
|
|
|
41
41
|
db: Database,
|
|
42
42
|
workspaceId: string,
|
|
43
43
|
sessionId: string,
|
|
44
|
+
attemptId: string,
|
|
44
45
|
settings: Settings,
|
|
45
46
|
): Promise<Settings> {
|
|
46
47
|
const encryptionKey = environmentsEncryptionKeyBytes(settings);
|
|
@@ -57,7 +58,7 @@ export async function settingsWithSessionMcpServersForRun(
|
|
|
57
58
|
}
|
|
58
59
|
return settingsWithSessionMcpServers(
|
|
59
60
|
settings,
|
|
60
|
-
await listSessionMcpServersForRun(db, workspaceId, sessionId, encryptionKey ?? null),
|
|
61
|
+
await listSessionMcpServersForRun(db, workspaceId, sessionId, attemptId, encryptionKey ?? null),
|
|
61
62
|
);
|
|
62
63
|
}
|
|
63
64
|
|
|
@@ -173,6 +174,7 @@ export function withCodexProvider(settings: Settings): Settings {
|
|
|
173
174
|
baseUrl: CODEX_PROVIDER_BASE_URL,
|
|
174
175
|
models: CODEX_FALLBACK_MODEL_SLUGS.map((slug) => ({
|
|
175
176
|
id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
|
|
177
|
+
upstreamModelId: slug,
|
|
176
178
|
label: slug,
|
|
177
179
|
reasoningEffort: true,
|
|
178
180
|
// These three values are the live Codex CLI catalog policy, kept distinct
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_CODEX_FLEET_POLICY_V1,
|
|
3
|
+
compareCodexFleetCanonicalStringsV1,
|
|
4
|
+
createCodexFleetReplayRecordV1,
|
|
5
|
+
type CodexFleetCandidateStatus,
|
|
6
|
+
type CodexFleetDecisionInputV1,
|
|
7
|
+
type CodexFleetReplayRecordV1,
|
|
8
|
+
} from "@opengeni/contracts";
|
|
9
|
+
import type { CodexLeaseAccountStatus } from "@opengeni/db";
|
|
10
|
+
import { CancelledFailure } from "@temporalio/activity";
|
|
11
|
+
import { createHmac } from "node:crypto";
|
|
12
|
+
import { TurnAttemptFencedError } from "./turn-attempt-fenced";
|
|
13
|
+
|
|
14
|
+
export const CODEX_FLEET_SHADOW_MAX_PAYLOAD_BYTES = 34 * 1_024;
|
|
15
|
+
|
|
16
|
+
export type CodexFleetActualDecisionV1 = {
|
|
17
|
+
outcome: "selected" | "waiting" | "none";
|
|
18
|
+
candidateKey: string | null;
|
|
19
|
+
reason: "lease_reused" | "pin" | "rotation" | "active" | "all_capped" | "none";
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type CodexFleetShadowComparisonV1 =
|
|
23
|
+
| "match"
|
|
24
|
+
| "different_candidate"
|
|
25
|
+
| "different_outcome"
|
|
26
|
+
| "not_comparable_truncated";
|
|
27
|
+
|
|
28
|
+
export type CodexFleetShadowPayloadV1 = {
|
|
29
|
+
schemaVersion: 1;
|
|
30
|
+
mode: "shadow";
|
|
31
|
+
actual: CodexFleetActualDecisionV1;
|
|
32
|
+
comparison: CodexFleetShadowComparisonV1;
|
|
33
|
+
replay: CodexFleetReplayRecordV1;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type CodexFleetShadowBuildInputV1 = {
|
|
37
|
+
accounts: readonly CodexLeaseAccountStatus[];
|
|
38
|
+
actualCredentialId: string | null;
|
|
39
|
+
actualOutcome: CodexFleetActualDecisionV1["outcome"];
|
|
40
|
+
actualReason: CodexFleetActualDecisionV1["reason"];
|
|
41
|
+
affinityCredentialId: string | null;
|
|
42
|
+
fencedInFlight: boolean;
|
|
43
|
+
nearExhaustionPct: number;
|
|
44
|
+
now: Date;
|
|
45
|
+
/** Per-event entropy used only to unlink account aliases; never persisted. */
|
|
46
|
+
aliasSeed: string;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type CodexFleetShadowPublicationResultV1 =
|
|
50
|
+
| { outcome: "disabled" }
|
|
51
|
+
| {
|
|
52
|
+
outcome: "published";
|
|
53
|
+
payload: CodexFleetShadowPayloadV1;
|
|
54
|
+
payloadBytes: number;
|
|
55
|
+
}
|
|
56
|
+
| {
|
|
57
|
+
outcome: "failed";
|
|
58
|
+
stage: "build" | "serialize" | "publish";
|
|
59
|
+
reason: "payload_too_large" | "unexpected";
|
|
60
|
+
errorName: string;
|
|
61
|
+
payloadBytes: number | null;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const CODEX_FLEET_SHADOW_DECISION_MAX_METRIC_SERIES = 3 * 3 * 4 * 4 * 2;
|
|
65
|
+
export const CODEX_FLEET_SHADOW_ERROR_MAX_METRIC_SERIES = 3 * 2;
|
|
66
|
+
|
|
67
|
+
/** Fixed low-cardinality labels; workspace/account/tenant identity is structurally absent. */
|
|
68
|
+
export function codexFleetShadowDecisionMetricLabelsV1(payload: CodexFleetShadowPayloadV1) {
|
|
69
|
+
return {
|
|
70
|
+
actual_outcome: payload.actual.outcome,
|
|
71
|
+
shadow_outcome: payload.replay.decision.outcome,
|
|
72
|
+
comparison: payload.comparison,
|
|
73
|
+
confidence: payload.replay.decision.confidence,
|
|
74
|
+
truncated: payload.replay.truncatedCandidateCount > 0 ? "true" : "false",
|
|
75
|
+
} as const;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function codexFleetShadowErrorMetricLabelsV1(
|
|
79
|
+
result: Extract<CodexFleetShadowPublicationResultV1, { outcome: "failed" }>,
|
|
80
|
+
) {
|
|
81
|
+
return { stage: result.stage, reason: result.reason } as const;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Default-off, bounded, fail-open runtime seam for the shadow record.
|
|
86
|
+
*
|
|
87
|
+
* The typed result intentionally carries no error message: provider, database,
|
|
88
|
+
* or transport errors can include private metadata. Callers may count/log only
|
|
89
|
+
* the bounded stage, reason, name, and size fields returned here. A failure can
|
|
90
|
+
* never replace or clear the credential already selected by the allocator.
|
|
91
|
+
*/
|
|
92
|
+
export async function publishCodexFleetShadowDecisionV1(input: {
|
|
93
|
+
enabled: boolean;
|
|
94
|
+
decision: CodexFleetShadowBuildInputV1;
|
|
95
|
+
publish: (
|
|
96
|
+
events: Array<{
|
|
97
|
+
type: "codex.fleet.decision";
|
|
98
|
+
payload: CodexFleetShadowPayloadV1;
|
|
99
|
+
}>,
|
|
100
|
+
) => Promise<unknown>;
|
|
101
|
+
}): Promise<CodexFleetShadowPublicationResultV1> {
|
|
102
|
+
if (!input.enabled) return { outcome: "disabled" };
|
|
103
|
+
|
|
104
|
+
let stage: "build" | "serialize" | "publish" = "build";
|
|
105
|
+
let payloadBytes: number | null = null;
|
|
106
|
+
try {
|
|
107
|
+
const payload = buildCodexFleetShadowPayloadV1(input.decision);
|
|
108
|
+
stage = "serialize";
|
|
109
|
+
payloadBytes = new TextEncoder().encode(JSON.stringify(payload)).byteLength;
|
|
110
|
+
if (payloadBytes > CODEX_FLEET_SHADOW_MAX_PAYLOAD_BYTES) {
|
|
111
|
+
return {
|
|
112
|
+
outcome: "failed",
|
|
113
|
+
stage,
|
|
114
|
+
reason: "payload_too_large",
|
|
115
|
+
errorName: "CodexFleetShadowPayloadTooLargeError",
|
|
116
|
+
payloadBytes,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
stage = "publish";
|
|
120
|
+
await input.publish([{ type: "codex.fleet.decision", payload }]);
|
|
121
|
+
return { outcome: "published", payload, payloadBytes };
|
|
122
|
+
} catch (error) {
|
|
123
|
+
// These are authoritative activity lifecycle signals, not shadow failures.
|
|
124
|
+
// Swallowing either would allow a superseded attempt to continue mutating.
|
|
125
|
+
if (error instanceof TurnAttemptFencedError || error instanceof CancelledFailure) throw error;
|
|
126
|
+
return {
|
|
127
|
+
outcome: "failed",
|
|
128
|
+
stage,
|
|
129
|
+
reason: "unexpected",
|
|
130
|
+
errorName: safeErrorName(error),
|
|
131
|
+
payloadBytes,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function buildCodexFleetShadowPayloadV1(
|
|
137
|
+
input: CodexFleetShadowBuildInputV1,
|
|
138
|
+
): CodexFleetShadowPayloadV1 {
|
|
139
|
+
// Randomized keyed ordering makes c00/c01 unlinkable across events while the
|
|
140
|
+
// supplied event seed keeps this one replay/payload build deterministic.
|
|
141
|
+
const eventAccounts = [...input.accounts].sort(
|
|
142
|
+
(left, right) =>
|
|
143
|
+
compareCodexFleetCanonicalStringsV1(
|
|
144
|
+
aliasSortKey(input.aliasSeed, left.id),
|
|
145
|
+
aliasSortKey(input.aliasSeed, right.id),
|
|
146
|
+
) || compareCodexFleetCanonicalStringsV1(left.id, right.id),
|
|
147
|
+
);
|
|
148
|
+
const aliases = new Map(
|
|
149
|
+
eventAccounts.map((account, index) => [account.id, candidateAlias(index)] as const),
|
|
150
|
+
);
|
|
151
|
+
const actualCandidateKey = input.actualCredentialId
|
|
152
|
+
? (aliases.get(input.actualCredentialId) ?? null)
|
|
153
|
+
: null;
|
|
154
|
+
const currentCandidateKey = input.affinityCredentialId
|
|
155
|
+
? (aliases.get(input.affinityCredentialId) ?? null)
|
|
156
|
+
: null;
|
|
157
|
+
const decisionInput: CodexFleetDecisionInputV1 = {
|
|
158
|
+
observedAtMs: input.now.getTime(),
|
|
159
|
+
request: {
|
|
160
|
+
placement: input.fencedInFlight ? "fenced_in_flight" : "new",
|
|
161
|
+
priority: "standard",
|
|
162
|
+
currentCandidateKey: input.fencedInFlight ? actualCandidateKey : currentCandidateKey,
|
|
163
|
+
waitAgeMs: 0,
|
|
164
|
+
overlayKey: null,
|
|
165
|
+
overlayMode: "none",
|
|
166
|
+
},
|
|
167
|
+
admission: {
|
|
168
|
+
// The shadow policy does not invent hard capacity from account count. A later typed
|
|
169
|
+
// admission snapshot supplies dynamic capacity; shadow v1 records unknown.
|
|
170
|
+
dynamicCapacityUnits: null,
|
|
171
|
+
inUseUnits: input.accounts.reduce((sum, account) => sum + account.activeLeaseCount, 0),
|
|
172
|
+
queuedManagerCount: 0,
|
|
173
|
+
emergencyFuseActive: false,
|
|
174
|
+
},
|
|
175
|
+
candidates: eventAccounts.map((account, index) => ({
|
|
176
|
+
key: candidateAlias(index),
|
|
177
|
+
status: normalizeStatus(account.status),
|
|
178
|
+
allocatorEnabled: account.allocatorEnabled,
|
|
179
|
+
cooldownRemainingMs: remainingMs(account.exhaustedUntil, input.now),
|
|
180
|
+
activeLeaseCount: account.activeLeaseCount,
|
|
181
|
+
quota: {
|
|
182
|
+
primary: {
|
|
183
|
+
usedPercent: account.primaryUsedPercent,
|
|
184
|
+
resetRemainingMs: remainingMs(account.primaryResetAt, input.now),
|
|
185
|
+
},
|
|
186
|
+
secondary: {
|
|
187
|
+
usedPercent: account.secondaryUsedPercent,
|
|
188
|
+
resetRemainingMs: remainingMs(account.secondaryResetAt, input.now),
|
|
189
|
+
},
|
|
190
|
+
checkedAgeMs: ageMs(account.usageCheckedAt, input.now),
|
|
191
|
+
confidence: quotaConfidence(account),
|
|
192
|
+
},
|
|
193
|
+
// Current production cache evidence is aggregate metric + opaque log data,
|
|
194
|
+
// not allocator state. Preserve that gap as unknown instead of fabricating 0.
|
|
195
|
+
cache: {
|
|
196
|
+
hitRatio: null,
|
|
197
|
+
sampledTokens: null,
|
|
198
|
+
checkedAgeMs: null,
|
|
199
|
+
confidence: "unknown",
|
|
200
|
+
state: "unknown",
|
|
201
|
+
thresholdObservedForMs: null,
|
|
202
|
+
},
|
|
203
|
+
// The shadow policy has no typed workspace-local burn feed at this boundary yet.
|
|
204
|
+
observedBurn: {
|
|
205
|
+
primaryPercentPerHour: null,
|
|
206
|
+
secondaryPercentPerHour: null,
|
|
207
|
+
confidence: "unknown",
|
|
208
|
+
},
|
|
209
|
+
// Provider accounting has not supplied a typed burn observation yet.
|
|
210
|
+
// Unknown inference is explicit and contributes no fabricated tenant truth.
|
|
211
|
+
inferredUnexplainedBurn: {
|
|
212
|
+
primaryPercentPerHour: null,
|
|
213
|
+
secondaryPercentPerHour: null,
|
|
214
|
+
confidence: "unknown",
|
|
215
|
+
},
|
|
216
|
+
overlayKeys: [],
|
|
217
|
+
})),
|
|
218
|
+
};
|
|
219
|
+
const replay = createCodexFleetReplayRecordV1(decisionInput, {
|
|
220
|
+
...DEFAULT_CODEX_FLEET_POLICY_V1,
|
|
221
|
+
placementUsageCeilingPercent: input.nearExhaustionPct,
|
|
222
|
+
});
|
|
223
|
+
const actual: CodexFleetActualDecisionV1 = {
|
|
224
|
+
outcome: input.actualOutcome,
|
|
225
|
+
candidateKey: actualCandidateKey,
|
|
226
|
+
reason: input.actualReason,
|
|
227
|
+
};
|
|
228
|
+
return {
|
|
229
|
+
schemaVersion: 1,
|
|
230
|
+
mode: "shadow",
|
|
231
|
+
actual,
|
|
232
|
+
comparison: compareDecision(actual, replay),
|
|
233
|
+
replay,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function compareDecision(
|
|
238
|
+
actual: CodexFleetActualDecisionV1,
|
|
239
|
+
replay: CodexFleetReplayRecordV1,
|
|
240
|
+
): CodexFleetShadowComparisonV1 {
|
|
241
|
+
if (
|
|
242
|
+
replay.truncatedCandidateCount > 0 &&
|
|
243
|
+
actual.candidateKey !== null &&
|
|
244
|
+
!replay.input.candidates.some((candidate) => candidate.key === actual.candidateKey)
|
|
245
|
+
) {
|
|
246
|
+
return "not_comparable_truncated";
|
|
247
|
+
}
|
|
248
|
+
const shadowOutcome = replay.decision.outcome === "selected" ? "selected" : "none";
|
|
249
|
+
const actualComparableOutcome = actual.outcome === "selected" ? "selected" : "none";
|
|
250
|
+
if (shadowOutcome !== actualComparableOutcome) return "different_outcome";
|
|
251
|
+
if (shadowOutcome === "selected") {
|
|
252
|
+
return replay.decision.selectedCandidateKey === actual.candidateKey
|
|
253
|
+
? "match"
|
|
254
|
+
: "different_candidate";
|
|
255
|
+
}
|
|
256
|
+
return "match";
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function normalizeStatus(status: string): CodexFleetCandidateStatus {
|
|
260
|
+
if (status === "active" || status === "needs_relogin" || status === "error") return status;
|
|
261
|
+
return "unknown";
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function candidateAlias(index: number): string {
|
|
265
|
+
return `c${index.toString(36).padStart(2, "0")}`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function aliasSortKey(seed: string, credentialId: string): string {
|
|
269
|
+
return createHmac("sha256", seed).update(credentialId).digest("hex");
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function quotaConfidence(account: CodexLeaseAccountStatus): "unknown" | "low" | "medium" | "high" {
|
|
273
|
+
if (!account.usageCheckedAt) return "unknown";
|
|
274
|
+
const windows = [
|
|
275
|
+
[account.primaryUsedPercent, account.primaryResetAt],
|
|
276
|
+
[account.secondaryUsedPercent, account.secondaryResetAt],
|
|
277
|
+
] as const;
|
|
278
|
+
const complete = windows.filter(([used, reset]) => used !== null && reset !== null).length;
|
|
279
|
+
const partial = windows.some(([used, reset]) => (used === null) !== (reset === null));
|
|
280
|
+
if (complete === 0) return "unknown";
|
|
281
|
+
if (partial) return "low";
|
|
282
|
+
return complete === windows.length ? "high" : "medium";
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function remainingMs(value: Date | null, now: Date): number | null {
|
|
286
|
+
return value ? Math.max(0, value.getTime() - now.getTime()) : null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function ageMs(value: Date | null, now: Date): number | null {
|
|
290
|
+
return value ? Math.max(0, now.getTime() - value.getTime()) : null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function safeErrorName(error: unknown): string {
|
|
294
|
+
if (!(error instanceof Error)) return "UnknownError";
|
|
295
|
+
return /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(error.name) ? error.name : "Error";
|
|
296
|
+
}
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
type ConnectionCredentialsPort,
|
|
12
12
|
type GitCredentialProvider,
|
|
13
13
|
type GitCredentialRepositoryRef,
|
|
14
|
+
type GitCredentialTransport,
|
|
14
15
|
type GitCredentials,
|
|
15
16
|
type ResourceRef,
|
|
16
17
|
type SandboxSecrets,
|
|
@@ -48,6 +49,7 @@ export type GitCredentialBindingSeed = {
|
|
|
48
49
|
credentialBindingId: string;
|
|
49
50
|
provider: GitCredentialProvider;
|
|
50
51
|
token: string;
|
|
52
|
+
transport?: GitCredentialTransport;
|
|
51
53
|
expiresAt?: string;
|
|
52
54
|
providerBindingCount?: number;
|
|
53
55
|
};
|
|
@@ -71,11 +73,14 @@ export type MintedSandboxToolspaceToken = {
|
|
|
71
73
|
expiresAt: Date;
|
|
72
74
|
};
|
|
73
75
|
|
|
76
|
+
export type SandboxToolspaceAuthority = {
|
|
77
|
+
sessionId: string;
|
|
78
|
+
};
|
|
79
|
+
|
|
74
80
|
export async function mintSandboxToolspaceToken(
|
|
75
81
|
settings: Settings,
|
|
76
82
|
scope: ConnectionScope,
|
|
77
|
-
|
|
78
|
-
runId: string,
|
|
83
|
+
authority: SandboxToolspaceAuthority,
|
|
79
84
|
nowMs = Date.now(),
|
|
80
85
|
): Promise<MintedSandboxToolspaceToken | undefined> {
|
|
81
86
|
if (!settings.toolspaceEnabled || !settings.delegationSecret) {
|
|
@@ -85,10 +90,10 @@ export async function mintSandboxToolspaceToken(
|
|
|
85
90
|
const token = await signDelegatedAccessToken(settings.delegationSecret, {
|
|
86
91
|
accountId: scope.accountId,
|
|
87
92
|
workspaceId: scope.workspaceId,
|
|
88
|
-
subjectId: `sandbox:${
|
|
93
|
+
subjectId: `sandbox:${authority.sessionId}`,
|
|
89
94
|
subjectLabel: "sandbox toolspace",
|
|
90
95
|
permissions: ["toolspace:call"],
|
|
91
|
-
sessionId,
|
|
96
|
+
sessionId: authority.sessionId,
|
|
92
97
|
exp: expiresAtSeconds,
|
|
93
98
|
});
|
|
94
99
|
return { token, expiresAt: new Date(expiresAtSeconds * 1000) };
|
|
@@ -197,8 +202,7 @@ export async function sandboxEnvironmentForRun(
|
|
|
197
202
|
options: RunGitCredentialOptions & {
|
|
198
203
|
skipGitHubToken?: boolean;
|
|
199
204
|
deferGitHubToken?: boolean;
|
|
200
|
-
|
|
201
|
-
runId?: string;
|
|
205
|
+
toolspaceAuthority?: SandboxToolspaceAuthority;
|
|
202
206
|
} = {},
|
|
203
207
|
): Promise<{
|
|
204
208
|
environment: Record<string, string>;
|
|
@@ -232,14 +236,15 @@ export async function sandboxEnvironmentForRun(
|
|
|
232
236
|
// backend, including a connected machine. Unlike platform git provider tokens
|
|
233
237
|
// (inert on selfhosted → skipped above), the toolspace token is the machine's
|
|
234
238
|
// only path to programmatic tool calling, and it grants no more than the
|
|
235
|
-
// machine owner's own authority (toolspace:call, own-session-bound,
|
|
236
|
-
// budgeted, approval-tools
|
|
237
|
-
//
|
|
239
|
+
// machine owner's own authority (toolspace:call, own-session-bound,
|
|
240
|
+
// short-lived and renewable, per-active-turn budgeted, approval-tools
|
|
241
|
+
// excluded). Delivery mirrors the docker path: the caller threads it
|
|
242
|
+
// OFF-MANIFEST as the seed the runtime writes to
|
|
238
243
|
// $OPENGENI_TOOLSPACE_TOKEN_FILE over the box's exec channel.
|
|
239
244
|
const toolspaceScope = options.scope;
|
|
240
245
|
const toolspaceToken =
|
|
241
|
-
toolspaceScope && options.
|
|
242
|
-
? await mintSandboxToolspaceToken(settings, toolspaceScope, options.
|
|
246
|
+
toolspaceScope && options.toolspaceAuthority
|
|
247
|
+
? await mintSandboxToolspaceToken(settings, toolspaceScope, options.toolspaceAuthority)
|
|
243
248
|
: undefined;
|
|
244
249
|
if (toolspaceToken && toolspaceScope) {
|
|
245
250
|
environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(
|
|
@@ -397,6 +402,7 @@ async function mintRunGitTokensWithIdentity(
|
|
|
397
402
|
let identity: { name: string; email: string } | null = null;
|
|
398
403
|
for (const selection of selections) {
|
|
399
404
|
let token: string | null = null;
|
|
405
|
+
let transport: GitCredentialTransport | undefined;
|
|
400
406
|
let tokenExpiresAt: string | undefined;
|
|
401
407
|
if (
|
|
402
408
|
selection.provider === "github" &&
|
|
@@ -431,6 +437,7 @@ async function mintRunGitTokensWithIdentity(
|
|
|
431
437
|
);
|
|
432
438
|
}
|
|
433
439
|
token = minted.token;
|
|
440
|
+
transport = validatedGitCredentialTransport(selection, minted);
|
|
434
441
|
tokenExpiresAt = minted.expiresAt
|
|
435
442
|
? validatedGitCredentialExpiry(selection.provider, minted.expiresAt)
|
|
436
443
|
: undefined;
|
|
@@ -455,6 +462,7 @@ async function mintRunGitTokensWithIdentity(
|
|
|
455
462
|
credentialBindingId: selection.credentialBindingId,
|
|
456
463
|
provider: selection.provider,
|
|
457
464
|
token,
|
|
465
|
+
...(transport ? { transport } : {}),
|
|
458
466
|
...(tokenExpiresAt ? { expiresAt: tokenExpiresAt } : {}),
|
|
459
467
|
});
|
|
460
468
|
}
|
|
@@ -467,6 +475,7 @@ async function mintRunGitTokensWithIdentity(
|
|
|
467
475
|
}
|
|
468
476
|
for (const binding of bindings) {
|
|
469
477
|
binding.providerBindingCount = counts.get(binding.provider) ?? 1;
|
|
478
|
+
if (binding.transport?.kind === "http_broker") continue;
|
|
470
479
|
if (counts.get(binding.provider) !== 1) continue;
|
|
471
480
|
gitTokens[binding.provider] = binding.token;
|
|
472
481
|
if (binding.expiresAt) expiresAt[binding.provider] = binding.expiresAt;
|
|
@@ -482,6 +491,128 @@ function validatedGitCredentialExpiry(provider: GitCredentialProvider, value: st
|
|
|
482
491
|
return new Date(timestamp).toISOString();
|
|
483
492
|
}
|
|
484
493
|
|
|
494
|
+
const MAX_GIT_HTTP_BROKER_ROUTES = 256;
|
|
495
|
+
const MAX_GIT_HTTP_BROKER_URI_LENGTH = 4_096;
|
|
496
|
+
|
|
497
|
+
function validatedGitCredentialTransport(
|
|
498
|
+
selection: GitCredentialSelection,
|
|
499
|
+
minted: GitCredentials,
|
|
500
|
+
): GitCredentialTransport | undefined {
|
|
501
|
+
const transport = minted.transport;
|
|
502
|
+
if (!transport) return undefined;
|
|
503
|
+
|
|
504
|
+
if (
|
|
505
|
+
typeof transport !== "object" ||
|
|
506
|
+
transport.kind !== "http_broker" ||
|
|
507
|
+
!Array.isArray(transport.repositories)
|
|
508
|
+
) {
|
|
509
|
+
throw new Error(
|
|
510
|
+
`connection-credential provider (${selection.provider}) returned an invalid Git credential transport`,
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
const requestedUris = new Set(selection.repositoryRefs.map((repository) => repository.uri));
|
|
514
|
+
if (
|
|
515
|
+
requestedUris.size === 0 ||
|
|
516
|
+
requestedUris.size > MAX_GIT_HTTP_BROKER_ROUTES ||
|
|
517
|
+
transport.repositories.length !== requestedUris.size
|
|
518
|
+
) {
|
|
519
|
+
throw new Error(
|
|
520
|
+
`connection-credential provider (${selection.provider}) returned an incomplete Git HTTP broker route set`,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const echoedUris = new Set<string>();
|
|
525
|
+
const brokerUris = new Set<string>();
|
|
526
|
+
const repositories = transport.repositories.map((route) => {
|
|
527
|
+
if (
|
|
528
|
+
!route ||
|
|
529
|
+
typeof route !== "object" ||
|
|
530
|
+
typeof route.repositoryUri !== "string" ||
|
|
531
|
+
typeof route.brokerUri !== "string"
|
|
532
|
+
) {
|
|
533
|
+
throw new Error(
|
|
534
|
+
`connection-credential provider (${selection.provider}) returned an invalid Git HTTP broker route`,
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
if (!requestedUris.has(route.repositoryUri) || echoedUris.has(route.repositoryUri)) {
|
|
538
|
+
throw new Error(
|
|
539
|
+
`connection-credential provider (${selection.provider}) returned an unexpected Git HTTP broker repository`,
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
echoedUris.add(route.repositoryUri);
|
|
543
|
+
if (
|
|
544
|
+
route.brokerUri.length === 0 ||
|
|
545
|
+
route.brokerUri.length > MAX_GIT_HTTP_BROKER_URI_LENGTH ||
|
|
546
|
+
route.brokerUri.trim() !== route.brokerUri
|
|
547
|
+
) {
|
|
548
|
+
throw new Error(
|
|
549
|
+
`connection-credential provider (${selection.provider}) returned an invalid Git HTTP broker URI`,
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
let brokerUrl: URL;
|
|
553
|
+
try {
|
|
554
|
+
brokerUrl = new URL(route.brokerUri);
|
|
555
|
+
} catch {
|
|
556
|
+
throw new Error(
|
|
557
|
+
`connection-credential provider (${selection.provider}) returned an invalid Git HTTP broker URI`,
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
if (
|
|
561
|
+
brokerUrl.protocol !== "https:" ||
|
|
562
|
+
brokerUrl.username ||
|
|
563
|
+
brokerUrl.password ||
|
|
564
|
+
brokerUrl.search ||
|
|
565
|
+
brokerUrl.hash ||
|
|
566
|
+
brokerUris.has(brokerUrl.href) ||
|
|
567
|
+
brokerUrl.href === route.repositoryUri
|
|
568
|
+
) {
|
|
569
|
+
throw new Error(
|
|
570
|
+
`connection-credential provider (${selection.provider}) returned an unsafe Git HTTP broker URI`,
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
brokerUris.add(brokerUrl.href);
|
|
574
|
+
return {
|
|
575
|
+
repositoryUri: route.repositoryUri,
|
|
576
|
+
brokerUri: brokerUrl.href,
|
|
577
|
+
};
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
if ([...requestedUris].some((uri) => !echoedUris.has(uri))) {
|
|
581
|
+
throw new Error(
|
|
582
|
+
`connection-credential provider (${selection.provider}) returned an incomplete Git HTTP broker route set`,
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
return { kind: "http_broker", repositories };
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export function assertGitCredentialRenewalTransportUnchanged(
|
|
589
|
+
initial: GitCredentialBindingSeed,
|
|
590
|
+
renewed: GitCredentialBindingSeed,
|
|
591
|
+
): void {
|
|
592
|
+
if (!initial.transport && !renewed.transport) return;
|
|
593
|
+
if (
|
|
594
|
+
!initial.transport ||
|
|
595
|
+
!renewed.transport ||
|
|
596
|
+
initial.transport.kind !== renewed.transport.kind
|
|
597
|
+
) {
|
|
598
|
+
throw new Error(
|
|
599
|
+
`renewed ${initial.provider} credential changed transport for binding ${initial.credentialBindingId}`,
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
const routeKey = (route: { repositoryUri: string; brokerUri: string }) =>
|
|
603
|
+
`${route.repositoryUri}\u0000${route.brokerUri}`;
|
|
604
|
+
const initialRoutes = initial.transport.repositories.map(routeKey).sort();
|
|
605
|
+
const renewedRoutes = renewed.transport.repositories.map(routeKey).sort();
|
|
606
|
+
if (
|
|
607
|
+
initialRoutes.length !== renewedRoutes.length ||
|
|
608
|
+
initialRoutes.some((route, index) => route !== renewedRoutes[index])
|
|
609
|
+
) {
|
|
610
|
+
throw new Error(
|
|
611
|
+
`renewed ${initial.provider} credential changed Git HTTP broker routes for binding ${initial.credentialBindingId}`,
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
485
616
|
async function resolveRunGitIdentityWithSelections(
|
|
486
617
|
settings: Settings,
|
|
487
618
|
selections: GitCredentialSelection[],
|
package/src/activities/goals.ts
CHANGED
|
@@ -11,20 +11,17 @@ import {
|
|
|
11
11
|
type ToolRef,
|
|
12
12
|
} from "@opengeni/contracts";
|
|
13
13
|
import {
|
|
14
|
-
addSessionSystemUpdate,
|
|
15
14
|
enqueueSessionWorkflowWakeIfRunnable,
|
|
16
|
-
evaluateGoalContinuation,
|
|
17
15
|
getBillingBalance,
|
|
18
16
|
getLatestStartedSessionTurn,
|
|
19
17
|
getWorkspaceModelPolicy,
|
|
20
18
|
getSessionGoal,
|
|
21
19
|
isCodexBilledTurn,
|
|
22
|
-
|
|
20
|
+
materializeGoalContinuation,
|
|
23
21
|
requireSession,
|
|
24
22
|
sumUsageQuantity,
|
|
25
23
|
type Database,
|
|
26
24
|
} from "@opengeni/db";
|
|
27
|
-
import { appendAndPublishEvents } from "@opengeni/events";
|
|
28
25
|
import type { ActivityServices, MaybeContinueGoalInput, MaybeContinueGoalResult } from "./types";
|
|
29
26
|
|
|
30
27
|
export function createGoalActivities(services: () => Promise<ActivityServices>) {
|
|
@@ -111,92 +108,29 @@ export function createGoalActivities(services: () => Promise<ActivityServices>)
|
|
|
111
108
|
input.workspaceId,
|
|
112
109
|
isCodexRun,
|
|
113
110
|
);
|
|
114
|
-
const decision = await
|
|
111
|
+
const decision = await materializeGoalContinuation(db, {
|
|
112
|
+
accountId: input.accountId,
|
|
115
113
|
workspaceId: input.workspaceId,
|
|
116
114
|
sessionId: input.sessionId,
|
|
115
|
+
workflowId: input.workflowId,
|
|
117
116
|
defaultMaxAutoContinuations: settings.goalMaxAutoContinuations ?? null,
|
|
118
117
|
noProgressLimit: settings.goalNoProgressLimit,
|
|
119
118
|
// A model-policy block takes precedence: it is deterministic (a budget
|
|
120
119
|
// pause can clear on its own; a policy pause needs a model/policy change)
|
|
121
120
|
// and rides the same visible-pause channel.
|
|
122
121
|
budgetBlocked: modelPolicyBlocked ?? budgetBlocked,
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
await appendAndPublishEvents(db, bus, input.workspaceId, input.sessionId, [
|
|
129
|
-
{
|
|
130
|
-
type: "goal.paused",
|
|
131
|
-
payload: {
|
|
132
|
-
goalId: decision.goal.id,
|
|
133
|
-
actor: "system",
|
|
134
|
-
reason: decision.reason,
|
|
135
|
-
...(decision.goal.rationale ? { rationale: decision.goal.rationale } : {}),
|
|
136
|
-
autoContinuations: decision.goal.autoContinuations,
|
|
137
|
-
noProgressStreak: decision.goal.noProgressStreak,
|
|
138
|
-
},
|
|
139
|
-
},
|
|
140
|
-
]);
|
|
141
|
-
return { action: "paused" };
|
|
142
|
-
}
|
|
143
|
-
// Stop/continue race guard: a concurrent goal_complete/goal_pause/operator
|
|
144
|
-
// PATCH between the locked decision and this synthesis must win. The
|
|
145
|
-
// version check also catches a replace. A pause landing after this point
|
|
146
|
-
// results in at most one already-admitted continuation turn; the next
|
|
147
|
-
// pass sees the non-active goal and stops, and interrupt-driven pauses
|
|
148
|
-
// additionally cancel the claimed turn via the workflow interrupt path.
|
|
149
|
-
const recheck = await getSessionGoal(db, input.workspaceId, input.sessionId);
|
|
150
|
-
if (!recheck || recheck.status !== "active" || recheck.version !== decision.goal.version) {
|
|
151
|
-
return { action: "none" };
|
|
152
|
-
}
|
|
153
|
-
const prompt = goalContinuationPrompt(decision.goal, decision.autoContinuation, decision.cap);
|
|
154
|
-
const update = await addSessionSystemUpdate(db, {
|
|
155
|
-
accountId: input.accountId,
|
|
156
|
-
workspaceId: input.workspaceId,
|
|
157
|
-
sessionId: input.sessionId,
|
|
158
|
-
kind: "goal_continuation",
|
|
159
|
-
classification: "info",
|
|
160
|
-
sourceId: decision.goal.id,
|
|
161
|
-
dedupeKey: `goal-continuation:${decision.goal.id}:${decision.goal.version}:${decision.autoContinuation}`,
|
|
162
|
-
summary: prompt,
|
|
163
|
-
payload: {
|
|
164
|
-
type: "goal_continuation",
|
|
165
|
-
goalId: decision.goal.id,
|
|
166
|
-
goalVersion: decision.goal.version,
|
|
167
|
-
autoContinuation: decision.autoContinuation,
|
|
168
|
-
maxAutoContinuations: decision.cap,
|
|
169
|
-
prompt,
|
|
170
|
-
policy: {
|
|
171
|
-
model: continuationModel,
|
|
172
|
-
reasoningEffort: continuationReasoningEffort,
|
|
173
|
-
tools: withFirstPartyTools(settings, session.tools),
|
|
174
|
-
sandboxBackend: session.sandboxBackend,
|
|
175
|
-
},
|
|
122
|
+
policy: {
|
|
123
|
+
model: continuationModel,
|
|
124
|
+
reasoningEffort: continuationReasoningEffort,
|
|
125
|
+
tools: withFirstPartyTools(settings, session.tools),
|
|
126
|
+
sandboxBackend: session.sandboxBackend,
|
|
176
127
|
},
|
|
177
|
-
|
|
128
|
+
prompt: goalContinuationPrompt,
|
|
178
129
|
});
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
await bus.publish(input.workspaceId, input.sessionId, update.events);
|
|
130
|
+
if (decision.events.length > 0) {
|
|
131
|
+
await bus.publish(input.workspaceId, input.sessionId, decision.events);
|
|
182
132
|
}
|
|
183
|
-
|
|
184
|
-
// user-initiated and scheduled turns.
|
|
185
|
-
await recordUsageEvent(db, {
|
|
186
|
-
accountId: input.accountId,
|
|
187
|
-
workspaceId: input.workspaceId,
|
|
188
|
-
eventType: "agent_run.created",
|
|
189
|
-
quantity: 1,
|
|
190
|
-
unit: "run",
|
|
191
|
-
sourceResourceType: "session_system_update",
|
|
192
|
-
sourceResourceId: update.update.id,
|
|
193
|
-
sessionId: input.sessionId,
|
|
194
|
-
initiator: { kind: "service", subjectId: "goal-continuation" },
|
|
195
|
-
initiatorContext: { goalId: decision.goal.id },
|
|
196
|
-
origin: "goal",
|
|
197
|
-
idempotencyKey: `agent_run.created:goal:${input.workspaceId}:${update.update.id}`,
|
|
198
|
-
});
|
|
199
|
-
return { action: "continue" };
|
|
133
|
+
return { action: decision.action };
|
|
200
134
|
}
|
|
201
135
|
|
|
202
136
|
return {
|