@kodelyth/lobster 2026.5.42 → 2026.6.2

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.
@@ -1,279 +0,0 @@
1
- import type { KlawPluginApi } from "../runtime-api.js";
2
- import type { LobsterEnvelope, LobsterRunner, LobsterRunnerParams } from "./lobster-runner.js";
3
-
4
- type JsonLike =
5
- | null
6
- | boolean
7
- | number
8
- | string
9
- | JsonLike[]
10
- | {
11
- [key: string]: JsonLike;
12
- };
13
-
14
- type BoundTaskFlow = ReturnType<
15
- NonNullable<KlawPluginApi["runtime"]>["tasks"]["managedFlows"]["bindSession"]
16
- >;
17
-
18
- type FlowRecord = ReturnType<BoundTaskFlow["createManaged"]>;
19
- type MutationResult = ReturnType<BoundTaskFlow["setWaiting"]>;
20
-
21
- type LobsterApprovalWaitState = {
22
- kind: "lobster_approval";
23
- prompt: string;
24
- items: JsonLike[];
25
- resumeToken?: string;
26
- approvalId?: string;
27
- };
28
-
29
- type RunManagedLobsterFlowParams = {
30
- taskFlow: BoundTaskFlow;
31
- runner: LobsterRunner;
32
- runnerParams: LobsterRunnerParams;
33
- controllerId: string;
34
- goal: string;
35
- stateJson?: JsonLike;
36
- currentStep?: string;
37
- waitingStep?: string;
38
- };
39
-
40
- type ResumeManagedLobsterFlowParams = {
41
- taskFlow: BoundTaskFlow;
42
- runner: LobsterRunner;
43
- runnerParams: LobsterRunnerParams & {
44
- action: "resume";
45
- approve: boolean;
46
- } & ({ token: string } | { approvalId: string });
47
- flowId: string;
48
- expectedRevision: number;
49
- currentStep?: string;
50
- waitingStep?: string;
51
- };
52
-
53
- export type ManagedLobsterFlowResult =
54
- | {
55
- ok: true;
56
- envelope: LobsterEnvelope;
57
- flow: FlowRecord;
58
- mutation: MutationResult;
59
- }
60
- | {
61
- ok: false;
62
- flow?: FlowRecord;
63
- mutation?: MutationResult;
64
- error: Error;
65
- };
66
-
67
- function toJsonLike(value: unknown, seen = new WeakSet<object>()): JsonLike {
68
- if (value === null) {
69
- return null;
70
- }
71
- switch (typeof value) {
72
- case "boolean":
73
- case "string":
74
- return value;
75
- case "number":
76
- return Number.isFinite(value) ? value : String(value);
77
- case "bigint":
78
- return value.toString();
79
- case "undefined":
80
- case "function":
81
- case "symbol":
82
- return null;
83
- case "object": {
84
- if (value instanceof Date) {
85
- return value.toISOString();
86
- }
87
- if (Array.isArray(value)) {
88
- return value.map((item) => toJsonLike(item, seen));
89
- }
90
- if (seen.has(value)) {
91
- return "[Circular]";
92
- }
93
- seen.add(value);
94
- const jsonObject: Record<string, JsonLike> = {};
95
- for (const [key, entry] of Object.entries(value)) {
96
- if (entry === undefined || typeof entry === "function" || typeof entry === "symbol") {
97
- continue;
98
- }
99
- jsonObject[key] = toJsonLike(entry, seen);
100
- }
101
- seen.delete(value);
102
- return jsonObject;
103
- }
104
- }
105
- return null;
106
- }
107
-
108
- function buildApprovalWaitState(envelope: Extract<LobsterEnvelope, { ok: true }>): JsonLike {
109
- if (!envelope.requiresApproval) {
110
- return {
111
- kind: "lobster_approval",
112
- prompt: "",
113
- items: [],
114
- } satisfies LobsterApprovalWaitState;
115
- }
116
- return {
117
- kind: "lobster_approval",
118
- prompt: envelope.requiresApproval.prompt,
119
- items: envelope.requiresApproval.items.map((item) => toJsonLike(item)),
120
- ...(envelope.requiresApproval.resumeToken
121
- ? { resumeToken: envelope.requiresApproval.resumeToken }
122
- : {}),
123
- ...(envelope.requiresApproval.approvalId
124
- ? { approvalId: envelope.requiresApproval.approvalId }
125
- : {}),
126
- } satisfies LobsterApprovalWaitState;
127
- }
128
-
129
- function applyEnvelopeToFlow(params: {
130
- taskFlow: BoundTaskFlow;
131
- flow: FlowRecord;
132
- envelope: LobsterEnvelope;
133
- waitingStep: string;
134
- }): MutationResult {
135
- const { taskFlow, flow, envelope, waitingStep } = params;
136
-
137
- if (!envelope.ok) {
138
- return taskFlow.fail({
139
- flowId: flow.flowId,
140
- expectedRevision: flow.revision,
141
- });
142
- }
143
-
144
- if (envelope.status === "needs_approval") {
145
- return taskFlow.setWaiting({
146
- flowId: flow.flowId,
147
- expectedRevision: flow.revision,
148
- currentStep: waitingStep,
149
- waitJson: buildApprovalWaitState(envelope),
150
- });
151
- }
152
-
153
- return taskFlow.finish({
154
- flowId: flow.flowId,
155
- expectedRevision: flow.revision,
156
- });
157
- }
158
-
159
- function buildEnvelopeError(envelope: Extract<LobsterEnvelope, { ok: false }>) {
160
- return new Error(envelope.error.message);
161
- }
162
-
163
- export async function runManagedLobsterFlow(
164
- params: RunManagedLobsterFlowParams,
165
- ): Promise<ManagedLobsterFlowResult> {
166
- const flow = params.taskFlow.createManaged({
167
- controllerId: params.controllerId,
168
- goal: params.goal,
169
- currentStep: params.currentStep ?? "run_lobster",
170
- ...(params.stateJson !== undefined ? { stateJson: params.stateJson } : {}),
171
- });
172
-
173
- try {
174
- const envelope = await params.runner.run(params.runnerParams);
175
- const mutation = applyEnvelopeToFlow({
176
- taskFlow: params.taskFlow,
177
- flow,
178
- envelope,
179
- waitingStep: params.waitingStep ?? "await_lobster_approval",
180
- });
181
- if (!envelope.ok) {
182
- return {
183
- ok: false,
184
- flow,
185
- mutation,
186
- error: buildEnvelopeError(envelope),
187
- };
188
- }
189
- return {
190
- ok: true,
191
- envelope,
192
- flow,
193
- mutation,
194
- };
195
- } catch (error) {
196
- const err = error instanceof Error ? error : new Error(String(error));
197
- try {
198
- const mutation = params.taskFlow.fail({
199
- flowId: flow.flowId,
200
- expectedRevision: flow.revision,
201
- });
202
- return {
203
- ok: false,
204
- flow,
205
- mutation,
206
- error: err,
207
- };
208
- } catch {
209
- return {
210
- ok: false,
211
- flow,
212
- error: err,
213
- };
214
- }
215
- }
216
- }
217
-
218
- export async function resumeManagedLobsterFlow(
219
- params: ResumeManagedLobsterFlowParams,
220
- ): Promise<ManagedLobsterFlowResult> {
221
- const resumed = params.taskFlow.resume({
222
- flowId: params.flowId,
223
- expectedRevision: params.expectedRevision,
224
- status: "running",
225
- currentStep: params.currentStep ?? "resume_lobster",
226
- });
227
-
228
- if (!resumed.applied) {
229
- return {
230
- ok: false,
231
- mutation: resumed,
232
- error: new Error(`TaskFlow resume failed: ${resumed.code}`),
233
- };
234
- }
235
-
236
- try {
237
- const envelope = await params.runner.run(params.runnerParams);
238
- const mutation = applyEnvelopeToFlow({
239
- taskFlow: params.taskFlow,
240
- flow: resumed.flow,
241
- envelope,
242
- waitingStep: params.waitingStep ?? "await_lobster_approval",
243
- });
244
- if (!envelope.ok) {
245
- return {
246
- ok: false,
247
- flow: resumed.flow,
248
- mutation,
249
- error: buildEnvelopeError(envelope),
250
- };
251
- }
252
- return {
253
- ok: true,
254
- envelope,
255
- flow: resumed.flow,
256
- mutation,
257
- };
258
- } catch (error) {
259
- const err = error instanceof Error ? error : new Error(String(error));
260
- try {
261
- const mutation = params.taskFlow.fail({
262
- flowId: params.flowId,
263
- expectedRevision: resumed.flow.revision,
264
- });
265
- return {
266
- ok: false,
267
- flow: resumed.flow,
268
- mutation,
269
- error: err,
270
- };
271
- } catch {
272
- return {
273
- ok: false,
274
- flow: resumed.flow,
275
- error: err,
276
- };
277
- }
278
- }
279
- }
@@ -1,352 +0,0 @@
1
- import { createTestPluginApi } from "klaw/plugin-sdk/plugin-test-api";
2
- import { describe, expect, it, vi } from "vitest";
3
- import type { KlawPluginApi, KlawPluginToolContext } from "../runtime-api.js";
4
- import { createLobsterTool } from "./lobster-tool.js";
5
- import { createFakeTaskFlow } from "./taskflow-test-helpers.js";
6
-
7
- function fakeApi(overrides: Partial<KlawPluginApi> = {}): KlawPluginApi {
8
- return createTestPluginApi({
9
- id: "lobster",
10
- name: "lobster",
11
- source: "test",
12
- runtime: { version: "test" } as any,
13
- resolvePath: (p) => p,
14
- ...overrides,
15
- });
16
- }
17
-
18
- function fakeCtx(overrides: Partial<KlawPluginToolContext> = {}): KlawPluginToolContext {
19
- return {
20
- config: {},
21
- workspaceDir: "/tmp",
22
- agentDir: "/tmp",
23
- agentId: "main",
24
- sessionKey: "main",
25
- messageChannel: undefined,
26
- agentAccountId: undefined,
27
- sandboxed: false,
28
- ...overrides,
29
- };
30
- }
31
-
32
- function requireRecord(value: unknown, label: string): Record<string, unknown> {
33
- if (value === null || typeof value !== "object" || Array.isArray(value)) {
34
- throw new Error(`expected ${label} to be a record`);
35
- }
36
- return value as Record<string, unknown>;
37
- }
38
-
39
- describe("lobster plugin tool", () => {
40
- it("returns the Lobster envelope in details", async () => {
41
- const runner = {
42
- run: vi.fn().mockResolvedValue({
43
- ok: true,
44
- status: "ok",
45
- output: [{ hello: "world" }],
46
- requiresApproval: null,
47
- }),
48
- };
49
-
50
- const tool = createLobsterTool(fakeApi(), { runner });
51
- const res = await tool.execute("call1", {
52
- action: "run",
53
- pipeline: "noop",
54
- timeoutMs: 1000,
55
- });
56
-
57
- expect(runner.run).toHaveBeenCalledWith({
58
- action: "run",
59
- pipeline: "noop",
60
- cwd: process.cwd(),
61
- timeoutMs: 1000,
62
- maxStdoutBytes: 512_000,
63
- });
64
- const details = requireRecord(res.details, "lobster tool details");
65
- expect(details.ok).toBe(true);
66
- expect(details.status).toBe("ok");
67
- expect(details.output).toEqual([{ hello: "world" }]);
68
- expect(details.requiresApproval).toBeNull();
69
- });
70
-
71
- it("supports approval envelopes without changing the tool contract", async () => {
72
- const runner = {
73
- run: vi.fn().mockResolvedValue({
74
- ok: true,
75
- status: "needs_approval",
76
- output: [],
77
- requiresApproval: {
78
- type: "approval_request",
79
- prompt: "Send these alerts?",
80
- items: [{ id: "alert-1" }],
81
- resumeToken: "resume-token-1",
82
- },
83
- }),
84
- };
85
-
86
- const tool = createLobsterTool(fakeApi(), { runner });
87
- const res = await tool.execute("call-injected-runner", {
88
- action: "run",
89
- pipeline: "noop",
90
- argsJson: '{"since_hours":1}',
91
- timeoutMs: 1500,
92
- maxStdoutBytes: 4096,
93
- });
94
-
95
- expect(runner.run).toHaveBeenCalledWith({
96
- action: "run",
97
- pipeline: "noop",
98
- argsJson: '{"since_hours":1}',
99
- cwd: process.cwd(),
100
- timeoutMs: 1500,
101
- maxStdoutBytes: 4096,
102
- });
103
- const details = requireRecord(res.details, "approval lobster tool details");
104
- expect(details.ok).toBe(true);
105
- expect(details.status).toBe("needs_approval");
106
- const approval = requireRecord(details.requiresApproval, "approval request");
107
- expect(approval.type).toBe("approval_request");
108
- expect(approval.prompt).toBe("Send these alerts?");
109
- expect(approval.resumeToken).toBe("resume-token-1");
110
- });
111
-
112
- it("throws when the runner returns an error envelope", async () => {
113
- const tool = createLobsterTool(fakeApi(), {
114
- runner: {
115
- run: vi.fn().mockResolvedValue({
116
- ok: false,
117
- error: {
118
- type: "runtime_error",
119
- message: "boom",
120
- },
121
- }),
122
- },
123
- });
124
-
125
- await expect(
126
- tool.execute("call-runner-error", {
127
- action: "run",
128
- pipeline: "noop",
129
- }),
130
- ).rejects.toThrow("boom");
131
- });
132
-
133
- it("can run through managed TaskFlow mode", async () => {
134
- const runner = {
135
- run: vi.fn().mockResolvedValue({
136
- ok: true,
137
- status: "needs_approval",
138
- output: [],
139
- requiresApproval: {
140
- type: "approval_request",
141
- prompt: "Approve this?",
142
- items: [{ id: "item-1" }],
143
- resumeToken: "resume-1",
144
- approvalId: "approval-1",
145
- },
146
- }),
147
- };
148
- const taskFlow = createFakeTaskFlow();
149
-
150
- const tool = createLobsterTool(fakeApi(), { runner, taskFlow });
151
- const res = await tool.execute("call-managed-run", {
152
- action: "run",
153
- pipeline: "noop",
154
- flowControllerId: "tests/lobster",
155
- flowGoal: "Run Lobster workflow",
156
- flowStateJson: '{"lane":"email"}',
157
- flowCurrentStep: "run_lobster",
158
- flowWaitingStep: "await_review",
159
- });
160
-
161
- expect(taskFlow.createManaged).toHaveBeenCalledWith({
162
- controllerId: "tests/lobster",
163
- goal: "Run Lobster workflow",
164
- currentStep: "run_lobster",
165
- stateJson: { lane: "email" },
166
- });
167
- expect(taskFlow.setWaiting).toHaveBeenCalledWith({
168
- flowId: "flow-1",
169
- expectedRevision: 1,
170
- currentStep: "await_review",
171
- waitJson: {
172
- kind: "lobster_approval",
173
- prompt: "Approve this?",
174
- items: [{ id: "item-1" }],
175
- resumeToken: "resume-1",
176
- approvalId: "approval-1",
177
- },
178
- });
179
- const details = requireRecord(res.details, "managed run lobster tool details");
180
- expect(details.ok).toBe(true);
181
- expect(details.status).toBe("needs_approval");
182
- const flow = requireRecord(details.flow, "managed run flow details");
183
- expect(flow.flowId).toBe("flow-1");
184
- const mutation = requireRecord(details.mutation, "managed run mutation details");
185
- expect(mutation.applied).toBe(true);
186
- });
187
-
188
- it("rejects managed TaskFlow params when no bound taskFlow runtime is available", async () => {
189
- const tool = createLobsterTool(fakeApi(), {
190
- runner: { run: vi.fn() },
191
- });
192
-
193
- await expect(
194
- tool.execute("call-missing-taskflow", {
195
- action: "run",
196
- pipeline: "noop",
197
- flowControllerId: "tests/lobster",
198
- flowGoal: "Run Lobster workflow",
199
- }),
200
- ).rejects.toThrow(/Managed TaskFlow run mode requires a bound taskFlow runtime/);
201
- });
202
-
203
- it("rejects invalid flowStateJson in managed TaskFlow mode", async () => {
204
- const tool = createLobsterTool(fakeApi(), {
205
- runner: { run: vi.fn() },
206
- taskFlow: createFakeTaskFlow(),
207
- });
208
-
209
- await expect(
210
- tool.execute("call-invalid-flow-json", {
211
- action: "run",
212
- pipeline: "noop",
213
- flowControllerId: "tests/lobster",
214
- flowGoal: "Run Lobster workflow",
215
- flowStateJson: "{bad",
216
- }),
217
- ).rejects.toThrow(/flowStateJson must be valid JSON/);
218
- });
219
-
220
- it("can resume managed TaskFlow mode with only approvalId", async () => {
221
- const runner = {
222
- run: vi.fn().mockResolvedValue({
223
- ok: true,
224
- status: "ok",
225
- output: [],
226
- requiresApproval: null,
227
- }),
228
- };
229
- const taskFlow = createFakeTaskFlow();
230
- const tool = createLobsterTool(fakeApi(), { runner, taskFlow });
231
-
232
- const res = await tool.execute("call-managed-resume-approval-id", {
233
- action: "resume",
234
- approvalId: "approval-1",
235
- approve: true,
236
- flowId: "flow-1",
237
- flowExpectedRevision: 1,
238
- flowCurrentStep: "resume_lobster",
239
- });
240
-
241
- expect(taskFlow.resume).toHaveBeenCalledWith({
242
- flowId: "flow-1",
243
- expectedRevision: 1,
244
- status: "running",
245
- currentStep: "resume_lobster",
246
- });
247
- expect(runner.run).toHaveBeenCalledWith({
248
- action: "resume",
249
- approvalId: "approval-1",
250
- approve: true,
251
- cwd: process.cwd(),
252
- timeoutMs: 20_000,
253
- maxStdoutBytes: 512_000,
254
- });
255
- const details = requireRecord(res.details, "managed resume lobster tool details");
256
- expect(details.ok).toBe(true);
257
- expect(details.status).toBe("ok");
258
- const mutation = requireRecord(details.mutation, "managed resume mutation details");
259
- expect(mutation.applied).toBe(true);
260
- });
261
-
262
- it("rejects managed TaskFlow resume mode without a token or approvalId", async () => {
263
- const tool = createLobsterTool(fakeApi(), {
264
- runner: { run: vi.fn() },
265
- taskFlow: createFakeTaskFlow(),
266
- });
267
-
268
- await expect(
269
- tool.execute("call-missing-resume-token", {
270
- action: "resume",
271
- flowId: "flow-1",
272
- flowExpectedRevision: 1,
273
- approve: true,
274
- }),
275
- ).rejects.toThrow(/token or approvalId required when using managed TaskFlow resume mode/);
276
- });
277
-
278
- it("rejects managed TaskFlow resume mode without approve", async () => {
279
- const tool = createLobsterTool(fakeApi(), {
280
- runner: { run: vi.fn() },
281
- taskFlow: createFakeTaskFlow(),
282
- });
283
-
284
- await expect(
285
- tool.execute("call-missing-resume-approve", {
286
- action: "resume",
287
- token: "resume-token",
288
- flowId: "flow-1",
289
- flowExpectedRevision: 1,
290
- }),
291
- ).rejects.toThrow(/approve required when using managed TaskFlow resume mode/);
292
- });
293
-
294
- it("requires action", async () => {
295
- const tool = createLobsterTool(fakeApi(), {
296
- runner: { run: vi.fn() },
297
- });
298
- await expect(tool.execute("call-action-missing", {})).rejects.toThrow(/action required/);
299
- });
300
-
301
- it("rejects unknown action", async () => {
302
- const tool = createLobsterTool(fakeApi(), {
303
- runner: { run: vi.fn() },
304
- });
305
- await expect(
306
- tool.execute("call-action-unknown", {
307
- action: "explode",
308
- }),
309
- ).rejects.toThrow(/Unknown action/);
310
- });
311
-
312
- it("rejects absolute cwd", async () => {
313
- const tool = createLobsterTool(fakeApi(), {
314
- runner: { run: vi.fn() },
315
- });
316
- await expect(
317
- tool.execute("call-absolute-cwd", {
318
- action: "run",
319
- pipeline: "noop",
320
- cwd: "/tmp",
321
- }),
322
- ).rejects.toThrow(/cwd must be a relative path/);
323
- });
324
-
325
- it("rejects cwd that escapes the gateway working directory", async () => {
326
- const tool = createLobsterTool(fakeApi(), {
327
- runner: { run: vi.fn() },
328
- });
329
- await expect(
330
- tool.execute("call-escape-cwd", {
331
- action: "run",
332
- pipeline: "noop",
333
- cwd: "../../etc",
334
- }),
335
- ).rejects.toThrow(/must stay within/);
336
- });
337
-
338
- it("can be gated off in sandboxed contexts", () => {
339
- const api = fakeApi();
340
- const factoryTool = (ctx: KlawPluginToolContext) => {
341
- if (ctx.sandboxed) {
342
- return null;
343
- }
344
- return createLobsterTool(api, {
345
- runner: { run: vi.fn() },
346
- });
347
- };
348
-
349
- expect(factoryTool(fakeCtx({ sandboxed: true }))).toBeNull();
350
- expect(factoryTool(fakeCtx({ sandboxed: false }))?.name).toBe("lobster");
351
- });
352
- });