@opengeni/worker-bundle 0.2.0

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.
@@ -0,0 +1,129 @@
1
+ import type { Settings } from "@opengeni/config";
2
+ import type { Session, SessionGoal } from "@opengeni/contracts";
3
+ import { getSession, getSessionGoal, wakeParentSessionForChildCompletion, type Database } from "@opengeni/db";
4
+ import type { EventBus } from "@opengeni/events";
5
+ import type { ActivityServices, WakeSessionWorkflowSignal } from "./types";
6
+
7
+ export type NotifyServices = {
8
+ db: Database;
9
+ bus: EventBus;
10
+ settings: Settings;
11
+ observability: ActivityServices["observability"];
12
+ wakeSessionWorkflow: WakeSessionWorkflowSignal | null;
13
+ };
14
+
15
+ /**
16
+ * Deliver exactly one completion wake to a spawned worker's parent (manager)
17
+ * session when the worker reaches a terminal-for-now state. No-op for a
18
+ * parentless session (direct API create / scheduled run). Idempotent per
19
+ * terminal episode: the idempotency key is the child's current lastSequence,
20
+ * which advances every time the child does work, so a retry of the same
21
+ * terminal transition (activity retry, the workflow's idle re-check, the
22
+ * runAgentTurn-failed path overlapping a workflow-level wake) is deduped while
23
+ * a genuinely new idle-after-work episode notifies again. The parent's queued
24
+ * turn is delivered by the DB wake; signalling the parent's workflow
25
+ * (signalWithStart) ensures a parent whose workflow already completed gets a
26
+ * fresh run to claim it. Failures here never fail the child: the wake is a
27
+ * best-effort nudge layered on durable DB state.
28
+ *
29
+ * Lives in its own module so both the session-state terminal activities
30
+ * (markSessionIdle / failSession) and runAgentTurn's in-turn failure path can
31
+ * call it without a circular import between those two activity modules.
32
+ */
33
+ export async function notifyParentOfChildTerminal(
34
+ svc: NotifyServices,
35
+ workspaceId: string,
36
+ childSessionId: string,
37
+ terminalStatus: "idle" | "failed",
38
+ // Stable identifier for the terminal episode, used as the idempotency key so
39
+ // the same completion never wakes the parent twice. A FAILURE passes the
40
+ // failed turn's id: both the in-turn wake (runAgentTurn) and the
41
+ // workflow-level wake (failSession, after it appends more events that would
42
+ // shift lastSequence) key on the same turn, so a finally-throw that turns one
43
+ // failure into both paths still dedupes. An idle episode has no single
44
+ // owning turn, so it falls back to the child's lastSequence — which advances
45
+ // per work batch and is stable across retries of that same idle transition.
46
+ episodeKey?: string | null,
47
+ ): Promise<void> {
48
+ try {
49
+ const child = await getSession(svc.db, workspaceId, childSessionId);
50
+ if (!child || !child.parentSessionId) {
51
+ return;
52
+ }
53
+ const goal = await getSessionGoal(svc.db, workspaceId, childSessionId);
54
+ const clientEventId = `child-completion:${childSessionId}:${episodeKey ?? child.lastSequence}`;
55
+ const result = await wakeParentSessionForChildCompletion(svc.db, {
56
+ workspaceId,
57
+ parentSessionId: child.parentSessionId,
58
+ clientEventId,
59
+ text: childCompletionWakeText(child, goal, terminalStatus),
60
+ childCompletion: childCompletionPayload(child, goal, terminalStatus),
61
+ reasoningEffortFallback: svc.settings.openaiReasoningEffort,
62
+ });
63
+ if (!result.delivered) {
64
+ return;
65
+ }
66
+ await svc.bus.publish(workspaceId, child.parentSessionId, result.events);
67
+ if (svc.wakeSessionWorkflow) {
68
+ await svc.wakeSessionWorkflow({
69
+ accountId: child.accountId,
70
+ workspaceId,
71
+ sessionId: child.parentSessionId,
72
+ workflowId: result.temporalWorkflowId,
73
+ });
74
+ }
75
+ svc.observability.info("Woke parent session on worker completion", {
76
+ childSessionId,
77
+ parentSessionId: child.parentSessionId,
78
+ terminalStatus,
79
+ });
80
+ } catch (error) {
81
+ // A parent-wake failure must never fail the child's terminal activity.
82
+ svc.observability.error("Failed to wake parent session on worker completion", {
83
+ childSessionId,
84
+ error: error instanceof Error ? error.message : String(error),
85
+ });
86
+ }
87
+ }
88
+
89
+ function childCompletionPayload(child: Session, goal: SessionGoal | null, terminalStatus: "idle" | "failed"): Record<string, unknown> {
90
+ return {
91
+ childSessionId: child.id,
92
+ status: terminalStatus,
93
+ ...(goal
94
+ ? {
95
+ goal: {
96
+ status: goal.status,
97
+ text: goal.text,
98
+ ...(goal.evidence ? { evidence: goal.evidence } : {}),
99
+ ...(goal.rationale ? { rationale: goal.rationale } : {}),
100
+ ...(goal.pausedReason ? { pausedReason: goal.pausedReason } : {}),
101
+ },
102
+ }
103
+ : {}),
104
+ };
105
+ }
106
+
107
+ function childCompletionWakeText(child: Session, goal: SessionGoal | null, terminalStatus: "idle" | "failed"): string {
108
+ const lines: string[] = [];
109
+ if (terminalStatus === "failed") {
110
+ lines.push(`A worker session you spawned has FAILED. Worker session id: ${child.id}.`);
111
+ } else if (goal?.status === "completed") {
112
+ lines.push(`A worker session you spawned has COMPLETED its goal. Worker session id: ${child.id}.`);
113
+ } else if (goal?.status === "paused") {
114
+ lines.push(`A worker session you spawned has PAUSED its goal and gone idle. Worker session id: ${child.id}.`);
115
+ } else {
116
+ lines.push(`A worker session you spawned has finished its work and gone idle. Worker session id: ${child.id}.`);
117
+ }
118
+ if (goal) {
119
+ lines.push(`Worker goal: ${goal.text}`);
120
+ if (goal.status === "completed" && goal.evidence) {
121
+ lines.push(`Completion evidence: ${goal.evidence}`);
122
+ }
123
+ if (goal.status === "paused" && goal.rationale) {
124
+ lines.push(`Pause rationale: ${goal.rationale}`);
125
+ }
126
+ }
127
+ lines.push("Read the worker's session events/notebook output for its result, then continue. If your own goal was paused awaiting this worker, resume it now.");
128
+ return lines.join("\n");
129
+ }
@@ -0,0 +1,248 @@
1
+ // apps/worker/src/activities/recording.ts — the recording finalize helper (P4.3).
2
+ //
3
+ // The "agent films itself proving the fix" finalize loop, run IN the process that
4
+ // already holds the resumed-by-id box (the agent turn's own activity). The bytes
5
+ // go box → process memory → object-storage PUT and are NEVER serialized as a
6
+ // Temporal activity result (F10): read + PUT happen in ONE invocation here.
7
+ //
8
+ // Ordering invariant (F9): the box file is deleted ONLY after the storage PUT
9
+ // confirms and the `available` row commits — so a failed upload leaves the bytes
10
+ // recoverable on the box.
11
+
12
+ import type { Settings } from "@opengeni/config";
13
+ import type { Database } from "@opengeni/db";
14
+ import { deleteRecording, updateRecording } from "@opengeni/db";
15
+ import type {
16
+ RecordingAvailablePayload,
17
+ RecordingCodec,
18
+ RecordingFailedReason,
19
+ RecordingStartedPayload,
20
+ } from "@opengeni/contracts";
21
+ import {
22
+ contentTypeForCodec,
23
+ deleteRecordingArtifacts,
24
+ readRecordingBytes,
25
+ RecordingError,
26
+ recordingStorageKey,
27
+ startRecording as startRecordingOnBox,
28
+ stopRecording as stopRecordingOnBox,
29
+ type RecordingProcess,
30
+ } from "@opengeni/runtime";
31
+ import type { ObjectStorage } from "@opengeni/storage";
32
+ import { DOWNLOAD_URL_TTL_SECONDS } from "@opengeni/storage";
33
+
34
+ export type RecordingMode = "manual" | "on-turn" | "on-verify";
35
+
36
+ export type ActiveRecording = {
37
+ recordingId: string;
38
+ turnId: string | null;
39
+ mode: RecordingMode;
40
+ proc: RecordingProcess;
41
+ dimensions: [number, number];
42
+ framerate: number;
43
+ };
44
+
45
+ /**
46
+ * Insert the recording row, launch ffmpeg on the box, and return the live handle.
47
+ * Emits the `recording.started` payload (the caller publishes it on the spine).
48
+ */
49
+ export async function beginRecording(args: {
50
+ settings: Settings;
51
+ db: Database;
52
+ accountId: string;
53
+ workspaceId: string;
54
+ sessionId: string;
55
+ turnId: string | null;
56
+ recordingId: string;
57
+ mode: RecordingMode;
58
+ session: unknown;
59
+ runAs?: string | undefined;
60
+ reason?: string | null;
61
+ }): Promise<{ active: ActiveRecording; started: RecordingStartedPayload }> {
62
+ const { settings, db } = args;
63
+ const codec = settings.recordingDefaultCodec as RecordingCodec;
64
+ const dimensions: [number, number] = [settings.streamResolutionWidth, settings.streamResolutionHeight];
65
+ const framerate = settings.recordingFramerate;
66
+ await import("@opengeni/db").then(({ insertRecording }) => insertRecording(db, {
67
+ id: args.recordingId,
68
+ accountId: args.accountId,
69
+ workspaceId: args.workspaceId,
70
+ sessionId: args.sessionId,
71
+ turnId: args.turnId,
72
+ mode: args.mode,
73
+ codec,
74
+ width: dimensions[0],
75
+ height: dimensions[1],
76
+ reason: scrubFreeText(args.reason),
77
+ }));
78
+ const proc = await startRecordingOnBox(args.session, {
79
+ recordingId: args.recordingId,
80
+ codec,
81
+ framerate,
82
+ maxSeconds: settings.recordingMaxSeconds,
83
+ dimensions,
84
+ ...(args.runAs ? { runAs: args.runAs } : {}),
85
+ });
86
+ const active: ActiveRecording = {
87
+ recordingId: args.recordingId,
88
+ turnId: args.turnId,
89
+ mode: args.mode,
90
+ proc,
91
+ dimensions,
92
+ framerate,
93
+ };
94
+ const started: RecordingStartedPayload = {
95
+ recordingId: args.recordingId,
96
+ turnId: args.turnId,
97
+ mode: args.mode,
98
+ codec,
99
+ dimensions,
100
+ framerate,
101
+ startedAt: new Date().toISOString(),
102
+ reason: scrubFreeText(args.reason),
103
+ };
104
+ return { active, started };
105
+ }
106
+
107
+ export type FinalizeOutcome =
108
+ | { ok: true; available: RecordingAvailablePayload }
109
+ | { ok: false; reason: RecordingFailedReason; detail: string | null };
110
+
111
+ /**
112
+ * Stop ffmpeg, read the bytes off the box, PUT them to storage, commit `available`,
113
+ * and (only then) delete the box file (F9). Returns the available payload, or a
114
+ * failure reason. NEVER throws — finalize runs in a turn `finally` and must not
115
+ * mask the turn outcome.
116
+ */
117
+ export async function finalizeRecording(args: {
118
+ settings: Settings;
119
+ db: Database;
120
+ objectStorage: ObjectStorage | null;
121
+ accountId: string;
122
+ workspaceId: string;
123
+ sessionId: string;
124
+ active: ActiveRecording;
125
+ session: unknown;
126
+ runAs?: string | undefined;
127
+ }): Promise<FinalizeOutcome> {
128
+ const { settings, db, objectStorage, active } = args;
129
+ const codec = settings.recordingDefaultCodec as RecordingCodec;
130
+ const fail = async (reason: RecordingFailedReason, detail: string | null): Promise<FinalizeOutcome> => {
131
+ await updateRecording(db, {
132
+ accountId: args.accountId,
133
+ workspaceId: args.workspaceId,
134
+ recordingId: active.recordingId,
135
+ state: "failed",
136
+ reason: scrubFreeText(detail),
137
+ }).catch(() => undefined);
138
+ return { ok: false, reason, detail: scrubFreeText(detail) };
139
+ };
140
+
141
+ try {
142
+ await updateRecording(db, {
143
+ accountId: args.accountId,
144
+ workspaceId: args.workspaceId,
145
+ recordingId: active.recordingId,
146
+ state: "finalizing",
147
+ }).catch(() => undefined);
148
+
149
+ // 1. SIGINT ffmpeg and wait for the clean trailer.
150
+ await stopRecordingOnBox(args.session, active.proc);
151
+
152
+ // 2. Read bytes off the box (size-gated; no eager delete — F8/F9).
153
+ const finalized = await readRecordingBytes(args.session, active.proc, settings.recordingMaxBytes);
154
+
155
+ if (!objectStorage) {
156
+ return await fail("upload-failed", "object storage is not configured");
157
+ }
158
+
159
+ // 3. PUT to storage (the byte transfer stays IN this process — F10).
160
+ //
161
+ // SERVER-SIDE authenticated direct PUT, NOT presign+fetch. The worker holds the
162
+ // storage creds, so a presigned URL buys nothing — and on a split public/
163
+ // internal endpoint topology (a PUBLIC `objectStorageEndpoint` with no in-cluster
164
+ // route, the preview case) the presigned URL points at the public host and the
165
+ // fetch PUT 401s. `putObject` sends the bytes straight to the backend over the
166
+ // configured (in-cluster) endpoint with the in-process SDK client. Browser
167
+ // uploads keep using `createPutUrl`; this is the trusted-server twin.
168
+ const key = recordingStorageKey(args.workspaceId, args.sessionId, active.recordingId, codec);
169
+ try {
170
+ // `.slice()` detaches a fresh ArrayBuffer from the box read.
171
+ await objectStorage.putObject({ key, contentType: finalized.contentType, body: finalized.bytes.slice() });
172
+ } catch (uploadError) {
173
+ return await fail("upload-failed", uploadError instanceof Error ? uploadError.message : String(uploadError));
174
+ }
175
+
176
+ // 4. Commit `available` with the artifact ref.
177
+ await updateRecording(db, {
178
+ accountId: args.accountId,
179
+ workspaceId: args.workspaceId,
180
+ recordingId: active.recordingId,
181
+ state: "available",
182
+ storageKey: key,
183
+ sizeBytes: finalized.sizeBytes,
184
+ durationSeconds: finalized.durationSeconds,
185
+ });
186
+
187
+ // 5. ONLY NOW delete the box artifacts (F9 — never before a confirmed PUT).
188
+ await deleteRecordingArtifacts(args.session, active.proc);
189
+
190
+ const available: RecordingAvailablePayload = {
191
+ recordingId: active.recordingId,
192
+ turnId: active.turnId,
193
+ codec,
194
+ contentType: contentTypeForCodec(codec),
195
+ storageKey: key,
196
+ durationSeconds: finalized.durationSeconds,
197
+ sizeBytes: finalized.sizeBytes,
198
+ dimensions: active.dimensions,
199
+ };
200
+ return { ok: true, available };
201
+ } catch (error) {
202
+ const reason: RecordingFailedReason = error instanceof RecordingError ? error.reason : "ffmpeg-error";
203
+ return await fail(reason, error instanceof Error ? error.message : String(error));
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Discard an on-turn recording that captured NO computer-use activity (a plain
209
+ * text turn): stop ffmpeg on the box, delete the box artifacts, and remove the
210
+ * recording row entirely. NO storage PUT and NO `recording.failed` — a clean
211
+ * no-op. NEVER throws (it runs in the turn `finally` and must not mask the turn
212
+ * outcome). Returns nothing; the caller emits no recording event for a discard.
213
+ */
214
+ export async function discardRecording(args: {
215
+ db: Database;
216
+ accountId: string;
217
+ workspaceId: string;
218
+ active: ActiveRecording;
219
+ session: unknown;
220
+ }): Promise<void> {
221
+ const { db, active } = args;
222
+ try {
223
+ // Stop ffmpeg cleanly, then delete the partial artifact off the box. Both are
224
+ // best-effort: a stuck box must not strand the discard (the box rides the
225
+ // provider idle-timeout regardless).
226
+ await stopRecordingOnBox(args.session, active.proc).catch(() => undefined);
227
+ await deleteRecordingArtifacts(args.session, active.proc).catch(() => undefined);
228
+ } finally {
229
+ // Remove the phantom row inserted at beginRecording so a no-activity turn
230
+ // leaves no recording trace at all.
231
+ await deleteRecording(db, {
232
+ accountId: args.accountId,
233
+ workspaceId: args.workspaceId,
234
+ recordingId: active.recordingId,
235
+ }).catch(() => undefined);
236
+ }
237
+ }
238
+
239
+ export { DOWNLOAD_URL_TTL_SECONDS };
240
+
241
+ // Agent/ffmpeg-controlled free text rides redact() like every payload, but we
242
+ // also cap it here (defense in depth — a path/URL with creds shouldn't ride a
243
+ // reason/detail field unbounded; the redactor scrubs known secret shapes).
244
+ function scrubFreeText(value: string | null | undefined): string | null {
245
+ if (value === null || value === undefined) return null;
246
+ const trimmed = String(value).slice(0, 2_000);
247
+ return trimmed.length === 0 ? null : trimmed;
248
+ }
@@ -0,0 +1,54 @@
1
+ const MIN_REDACTABLE_VALUE_LENGTH = 6;
2
+
3
+ export type SecretForRedaction = {
4
+ name: string;
5
+ value: string;
6
+ };
7
+
8
+ /**
9
+ * Defense-in-depth against an agent echoing injected workspace environment
10
+ * values (for example via `env`) into session events. Deep-walks payloads and
11
+ * replaces every exact occurrence of each secret value with
12
+ * `[redacted:<NAME>]`. Values shorter than six characters are skipped to avoid
13
+ * a false-positive flood; secrets are applied longest-value-first so
14
+ * overlapping values redact deterministically.
15
+ */
16
+ export function createSecretRedactor(secrets: SecretForRedaction[]): (payload: unknown) => unknown {
17
+ const usable = secrets
18
+ .filter((secret) => secret.value.length >= MIN_REDACTABLE_VALUE_LENGTH)
19
+ .sort((a, b) => b.value.length - a.value.length);
20
+ if (usable.length === 0) {
21
+ return identityRedactor;
22
+ }
23
+ const redact = (payload: unknown): unknown => {
24
+ if (typeof payload === "string") {
25
+ let out = payload;
26
+ for (const secret of usable) {
27
+ if (out.includes(secret.value)) {
28
+ out = out.split(secret.value).join(`[redacted:${secret.name}]`);
29
+ }
30
+ }
31
+ return out;
32
+ }
33
+ if (Array.isArray(payload)) {
34
+ return payload.map(redact);
35
+ }
36
+ if (isPlainObject(payload)) {
37
+ return Object.fromEntries(Object.entries(payload).map(([key, value]) => [key, redact(value)]));
38
+ }
39
+ return payload;
40
+ };
41
+ return redact;
42
+ }
43
+
44
+ export function identityRedactor(payload: unknown): unknown {
45
+ return payload;
46
+ }
47
+
48
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
49
+ if (!value || typeof value !== "object") {
50
+ return false;
51
+ }
52
+ const proto = Object.getPrototypeOf(value);
53
+ return proto === Object.prototype || proto === null;
54
+ }