@opengeni/worker-bundle 0.7.5 → 0.10.8
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 +4097 -1271
- package/dist/index.js.map +1 -1
- package/dist/workflow-bundle.js +12360 -0
- package/package.json +18 -12
- package/src/activities/agent-turn.ts +2674 -828
- package/src/activities/capabilities.ts +11 -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 +503 -97
- 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/rig-verification.ts +897 -244
- 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 +33 -2
- package/src/activities/scheduled-tasks.ts +21 -0
- package/src/activities/session-state.ts +72 -0
- 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/observability-metrics.ts +83 -15
- 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/rig-verification.ts +3 -1
- package/src/workflows/session.ts +219 -23
- package/src/workflows.ts +1 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
export type ExpiringMaterialRenewalPolicy = {
|
|
2
|
+
defaultRefreshMs: number;
|
|
3
|
+
expiryLeadMs: number;
|
|
4
|
+
minRefreshMs: number;
|
|
5
|
+
maxRetryMs: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type RenewalTimer = unknown;
|
|
9
|
+
|
|
10
|
+
export type ExpiringMaterialRenewalController = {
|
|
11
|
+
refreshNow(): Promise<void>;
|
|
12
|
+
stop(): Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ExpiringMaterialRenewalOptions<T> = {
|
|
16
|
+
initialExpiresAt: Date | null;
|
|
17
|
+
resolve: () => Promise<T>;
|
|
18
|
+
write: (material: T) => Promise<void>;
|
|
19
|
+
expiresAt: (material: T) => Date | null;
|
|
20
|
+
policy: ExpiringMaterialRenewalPolicy;
|
|
21
|
+
now?: () => number;
|
|
22
|
+
schedule?: (callback: () => void, delayMs: number) => RenewalTimer;
|
|
23
|
+
clearSchedule?: (timer: RenewalTimer) => void;
|
|
24
|
+
onSuccess?: (result: { material: T; nextDelayMs: number }) => void;
|
|
25
|
+
onFailure?: (failure: { retryDelayMs: number; errorClass: string }) => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function nextExpiringMaterialRenewalDelay(
|
|
29
|
+
expiresAt: Date | null,
|
|
30
|
+
policy: ExpiringMaterialRenewalPolicy,
|
|
31
|
+
nowMs = Date.now(),
|
|
32
|
+
): number {
|
|
33
|
+
if (!expiresAt || !Number.isFinite(expiresAt.getTime())) {
|
|
34
|
+
return policy.defaultRefreshMs;
|
|
35
|
+
}
|
|
36
|
+
return Math.min(
|
|
37
|
+
policy.defaultRefreshMs,
|
|
38
|
+
Math.max(policy.minRefreshMs, expiresAt.getTime() - nowMs - policy.expiryLeadMs),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Attempt-owned single-flight renewal for expiring sandbox material.
|
|
44
|
+
*
|
|
45
|
+
* A late resolver cannot write after stop. A physical sandbox write that already
|
|
46
|
+
* started is drained so turn settlement cannot race a stale mutation.
|
|
47
|
+
*/
|
|
48
|
+
export function startExpiringMaterialRenewalLoop<T>(
|
|
49
|
+
options: ExpiringMaterialRenewalOptions<T>,
|
|
50
|
+
): ExpiringMaterialRenewalController {
|
|
51
|
+
const now = options.now ?? Date.now;
|
|
52
|
+
const schedule =
|
|
53
|
+
options.schedule ??
|
|
54
|
+
((callback: () => void, delayMs: number): ReturnType<typeof setTimeout> => {
|
|
55
|
+
const timer = setTimeout(callback, delayMs);
|
|
56
|
+
timer.unref?.();
|
|
57
|
+
return timer;
|
|
58
|
+
});
|
|
59
|
+
const clearSchedule =
|
|
60
|
+
options.clearSchedule ??
|
|
61
|
+
((timer: RenewalTimer): void => clearTimeout(timer as ReturnType<typeof setTimeout>));
|
|
62
|
+
let stopped = false;
|
|
63
|
+
let timer: RenewalTimer | null = null;
|
|
64
|
+
let inFlight: Promise<void> | null = null;
|
|
65
|
+
let writeInFlight: Promise<void> | null = null;
|
|
66
|
+
let retryDelayMs = options.policy.minRefreshMs;
|
|
67
|
+
|
|
68
|
+
const clearTimer = (): void => {
|
|
69
|
+
if (timer === null) return;
|
|
70
|
+
clearSchedule(timer);
|
|
71
|
+
timer = null;
|
|
72
|
+
};
|
|
73
|
+
const scheduleRefresh = (delayMs: number): void => {
|
|
74
|
+
if (stopped) return;
|
|
75
|
+
clearTimer();
|
|
76
|
+
timer = schedule(() => {
|
|
77
|
+
timer = null;
|
|
78
|
+
void refreshNow();
|
|
79
|
+
}, delayMs);
|
|
80
|
+
};
|
|
81
|
+
const performRefresh = async (): Promise<void> => {
|
|
82
|
+
try {
|
|
83
|
+
const material = await options.resolve();
|
|
84
|
+
if (stopped) return;
|
|
85
|
+
const write = options.write(material);
|
|
86
|
+
writeInFlight = write;
|
|
87
|
+
try {
|
|
88
|
+
await write;
|
|
89
|
+
} finally {
|
|
90
|
+
if (writeInFlight === write) writeInFlight = null;
|
|
91
|
+
}
|
|
92
|
+
if (stopped) return;
|
|
93
|
+
retryDelayMs = options.policy.minRefreshMs;
|
|
94
|
+
const nextDelayMs = nextExpiringMaterialRenewalDelay(
|
|
95
|
+
options.expiresAt(material),
|
|
96
|
+
options.policy,
|
|
97
|
+
now(),
|
|
98
|
+
);
|
|
99
|
+
try {
|
|
100
|
+
options.onSuccess?.({ material, nextDelayMs });
|
|
101
|
+
} catch {
|
|
102
|
+
// Observability never owns credential liveness.
|
|
103
|
+
}
|
|
104
|
+
scheduleRefresh(nextDelayMs);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (stopped) return;
|
|
107
|
+
const scheduledRetryMs = retryDelayMs;
|
|
108
|
+
retryDelayMs = Math.min(retryDelayMs * 2, options.policy.maxRetryMs);
|
|
109
|
+
try {
|
|
110
|
+
options.onFailure?.({
|
|
111
|
+
retryDelayMs: scheduledRetryMs,
|
|
112
|
+
errorClass: error instanceof Error ? error.name : "UnknownError",
|
|
113
|
+
});
|
|
114
|
+
} catch {
|
|
115
|
+
// Observability never owns credential liveness.
|
|
116
|
+
}
|
|
117
|
+
scheduleRefresh(scheduledRetryMs);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const refreshNow = (): Promise<void> => {
|
|
121
|
+
if (stopped) return Promise.resolve();
|
|
122
|
+
if (inFlight) return inFlight;
|
|
123
|
+
clearTimer();
|
|
124
|
+
const operation = performRefresh();
|
|
125
|
+
inFlight = operation;
|
|
126
|
+
void operation.finally(() => {
|
|
127
|
+
if (inFlight === operation) inFlight = null;
|
|
128
|
+
});
|
|
129
|
+
return operation;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
scheduleRefresh(
|
|
133
|
+
nextExpiringMaterialRenewalDelay(options.initialExpiresAt, options.policy, now()),
|
|
134
|
+
);
|
|
135
|
+
return {
|
|
136
|
+
refreshNow,
|
|
137
|
+
stop: async () => {
|
|
138
|
+
stopped = true;
|
|
139
|
+
clearTimer();
|
|
140
|
+
await writeInFlight?.catch(() => undefined);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
package/src/activities/goals.ts
CHANGED
|
@@ -190,6 +190,10 @@ export function createGoalActivities(services: () => Promise<ActivityServices>)
|
|
|
190
190
|
unit: "run",
|
|
191
191
|
sourceResourceType: "session_system_update",
|
|
192
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",
|
|
193
197
|
idempotencyKey: `agent_run.created:goal:${input.workspaceId}:${update.update.id}`,
|
|
194
198
|
});
|
|
195
199
|
return { action: "continue" };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Settings } from "@opengeni/config";
|
|
2
|
+
import type { ConnectionCredentialsPort, SessionTurn } from "@opengeni/contracts";
|
|
3
|
+
import {
|
|
4
|
+
buildConnectionTokenResolver,
|
|
5
|
+
buildHostConnectionTokenResolver,
|
|
6
|
+
type Database,
|
|
7
|
+
type ResolveConnectionCredentialInput,
|
|
8
|
+
type ResolveConnectionCredentialResult,
|
|
9
|
+
} from "@opengeni/db";
|
|
10
|
+
|
|
11
|
+
export function connectionTokenResolverForTurn(input: {
|
|
12
|
+
db: Database;
|
|
13
|
+
settings: Settings;
|
|
14
|
+
connectionCredentials?: ConnectionCredentialsPort | null;
|
|
15
|
+
accountId: string;
|
|
16
|
+
workspaceId: string;
|
|
17
|
+
sessionId: string;
|
|
18
|
+
rootSessionId: string;
|
|
19
|
+
attemptId: string;
|
|
20
|
+
turn: SessionTurn;
|
|
21
|
+
}): (request: ResolveConnectionCredentialInput) => Promise<ResolveConnectionCredentialResult> {
|
|
22
|
+
const hostResolver = input.connectionCredentials?.mcpCredentials;
|
|
23
|
+
if (!hostResolver) {
|
|
24
|
+
return buildConnectionTokenResolver(input.db, input.settings);
|
|
25
|
+
}
|
|
26
|
+
return buildHostConnectionTokenResolver(hostResolver, {
|
|
27
|
+
accountId: input.accountId,
|
|
28
|
+
workspaceId: input.workspaceId,
|
|
29
|
+
sessionId: input.sessionId,
|
|
30
|
+
rootSessionId: input.rootSessionId,
|
|
31
|
+
turnId: input.turn.id,
|
|
32
|
+
attemptId: input.attemptId,
|
|
33
|
+
executionGeneration: input.turn.executionGeneration,
|
|
34
|
+
initiator: input.turn.initiator,
|
|
35
|
+
initiatorContext: input.turn.initiatorContext,
|
|
36
|
+
surface: "model",
|
|
37
|
+
});
|
|
38
|
+
}
|
package/src/activities/packs.ts
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import type { Settings } from "@opengeni/config";
|
|
2
2
|
import { CapabilityPack } from "@opengeni/contracts";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
getWorkspace,
|
|
5
|
+
getWorkspacePack,
|
|
6
|
+
listCapabilityInstallations,
|
|
7
|
+
listPackInstallations,
|
|
8
|
+
type Database,
|
|
9
|
+
} from "@opengeni/db";
|
|
10
|
+
import {
|
|
11
|
+
isSkillLibraryEntryId,
|
|
12
|
+
loadSkillLibrarySkill,
|
|
13
|
+
type EffectiveSkillSelection,
|
|
14
|
+
type PackSkill,
|
|
15
|
+
} from "@opengeni/runtime";
|
|
5
16
|
|
|
6
17
|
/**
|
|
7
18
|
* The pack-scoped runtime for a workspace: the sandbox image its sessions run
|
|
@@ -15,6 +26,19 @@ export type WorkspacePackRuntime = {
|
|
|
15
26
|
|
|
16
27
|
const emptyPackRuntime: WorkspacePackRuntime = { sandboxImage: null, skills: [] };
|
|
17
28
|
|
|
29
|
+
export type WorkspaceSkillLibraryRuntime = {
|
|
30
|
+
skillLibrarySkills: PackSkill[];
|
|
31
|
+
skillLibrarySelections: EffectiveSkillSelection[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type SkillLibraryInstallationMetadata = {
|
|
35
|
+
libraryId: string;
|
|
36
|
+
libraryVersion: string;
|
|
37
|
+
contentSha256: string;
|
|
38
|
+
sourceCommit: string;
|
|
39
|
+
provenance: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
18
42
|
/**
|
|
19
43
|
* Resolves the pack-scoped runtime from the workspace's active pack
|
|
20
44
|
* installations. Only registered (manifest-backed) packs can contribute a
|
|
@@ -45,6 +69,95 @@ export async function resolveWorkspacePackRuntime(
|
|
|
45
69
|
return workspacePackRuntimeFromPacks(packs);
|
|
46
70
|
}
|
|
47
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Resolves active immutable curated skills separately from pack skills. The
|
|
74
|
+
* capability installation is the workspace selection boundary; it carries
|
|
75
|
+
* only secret-free exact identity metadata, and the runtime loader verifies
|
|
76
|
+
* the pinned artifact hash before any content is materialized.
|
|
77
|
+
*/
|
|
78
|
+
export async function resolveWorkspaceSkillLibraryRuntime(
|
|
79
|
+
db: Database,
|
|
80
|
+
workspaceId: string,
|
|
81
|
+
): Promise<WorkspaceSkillLibraryRuntime> {
|
|
82
|
+
const installations = await listCapabilityInstallations(db, workspaceId);
|
|
83
|
+
const activeSkills = installations
|
|
84
|
+
.filter(
|
|
85
|
+
(installation) =>
|
|
86
|
+
installation.status === "active" &&
|
|
87
|
+
installation.kind === "skill" &&
|
|
88
|
+
installation.capabilityId.startsWith("skill:") &&
|
|
89
|
+
isSkillLibraryEntryId(installation.capabilityId.slice("skill:".length)),
|
|
90
|
+
)
|
|
91
|
+
.sort((a, b) => a.capabilityId.localeCompare(b.capabilityId));
|
|
92
|
+
const skillLibrarySkills: PackSkill[] = [];
|
|
93
|
+
const skillLibrarySelections: EffectiveSkillSelection[] = [];
|
|
94
|
+
for (const installation of activeSkills) {
|
|
95
|
+
const metadata = parseSkillLibraryInstallationMetadata(
|
|
96
|
+
installation.metadata,
|
|
97
|
+
installation.capabilityId,
|
|
98
|
+
);
|
|
99
|
+
if (installation.capabilityId !== `skill:${metadata.libraryId}`) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
`Skill installation id does not match its library entry: ${installation.capabilityId} -> ${metadata.libraryId}`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
const loaded = loadSkillLibrarySkill(metadata.libraryId, metadata.libraryVersion);
|
|
105
|
+
if (installation.config.version !== metadata.libraryVersion) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Skill installation version mismatch for ${metadata.libraryId}@${metadata.libraryVersion}: config has ${String(installation.config.version)}`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (loaded.entry.contentSha256 !== metadata.contentSha256) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
`Skill installation hash mismatch for ${metadata.libraryId}@${metadata.libraryVersion}: expected ${metadata.contentSha256}, catalog has ${loaded.entry.contentSha256}`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (
|
|
116
|
+
loaded.entry.sourceCommit !== metadata.sourceCommit ||
|
|
117
|
+
loaded.entry.provenance !== metadata.provenance
|
|
118
|
+
) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`Skill installation provenance mismatch for ${metadata.libraryId}@${metadata.libraryVersion}; re-enable the skill from the current catalog`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
skillLibrarySkills.push({
|
|
124
|
+
name: loaded.skill.name,
|
|
125
|
+
description: loaded.skill.description,
|
|
126
|
+
files: loaded.skill.files.map((file) => ({ path: file.path, content: file.content })),
|
|
127
|
+
});
|
|
128
|
+
skillLibrarySelections.push({
|
|
129
|
+
id: loaded.entry.id,
|
|
130
|
+
name: loaded.entry.name,
|
|
131
|
+
source: "library",
|
|
132
|
+
version: loaded.entry.version,
|
|
133
|
+
contentSha256: loaded.entry.contentSha256,
|
|
134
|
+
reason: "enabled workspace capability installation",
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return { skillLibrarySkills, skillLibrarySelections };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function parseSkillLibraryInstallationMetadata(
|
|
141
|
+
metadata: Record<string, unknown>,
|
|
142
|
+
capabilityId: string,
|
|
143
|
+
): SkillLibraryInstallationMetadata {
|
|
144
|
+
const libraryId = stringMetadata(metadata.libraryId);
|
|
145
|
+
const libraryVersion = stringMetadata(metadata.libraryVersion);
|
|
146
|
+
const contentSha256 = stringMetadata(metadata.contentSha256);
|
|
147
|
+
const sourceCommit = stringMetadata(metadata.sourceCommit);
|
|
148
|
+
const provenance = stringMetadata(metadata.provenance);
|
|
149
|
+
if (!libraryId || !libraryVersion || !contentSha256 || !sourceCommit || !provenance) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`Skill installation ${capabilityId} is missing immutable library metadata; re-enable the skill from the current catalog`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return { libraryId, libraryVersion, contentSha256, sourceCommit, provenance };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function stringMetadata(value: unknown): string | null {
|
|
158
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
159
|
+
}
|
|
160
|
+
|
|
48
161
|
/**
|
|
49
162
|
* Pure composition rule for enabled pack manifests. v1 keeps this small by
|
|
50
163
|
* design: at most one enabled pack may declare a sandbox image (no image
|