@swirls/looms 0.11.1 → 0.11.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.
@@ -23,6 +23,10 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
23
23
  }>;
24
24
  'turn.started': import("effect/Schema").Struct<{
25
25
  readonly turn: import("effect/Schema").Finite;
26
+ readonly messageCount: import("effect/Schema").optional<import("effect/Schema").Finite>;
27
+ }>;
28
+ 'turn.settled': import("effect/Schema").Struct<{
29
+ readonly turn: import("effect/Schema").Finite;
26
30
  }>;
27
31
  'turn.text_delta': import("effect/Schema").Struct<{
28
32
  readonly turn: import("effect/Schema").Finite;
@@ -455,10 +459,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
455
459
  readonly arguments: import("effect/Schema").Json;
456
460
  };
457
461
  };
462
+ }) | (Omit<EventInput, "payload" | "type"> & {
463
+ readonly type: "agent.turn.settled";
464
+ readonly payload: {
465
+ readonly turn: number;
466
+ };
458
467
  }) | (Omit<EventInput, "payload" | "type"> & {
459
468
  readonly type: "agent.turn.started";
460
469
  readonly payload: {
461
470
  readonly turn: number;
471
+ readonly messageCount?: number | undefined;
462
472
  };
463
473
  }) | (Omit<EventInput, "payload" | "type"> & {
464
474
  readonly type: "agent.turn.stopped";
@@ -955,10 +965,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
955
965
  readonly arguments: import("effect/Schema").Json;
956
966
  };
957
967
  };
968
+ }) | (Omit<EventInput, "payload" | "type"> & {
969
+ readonly type: "agent.turn.settled";
970
+ readonly payload: {
971
+ readonly turn: number;
972
+ };
958
973
  }) | (Omit<EventInput, "payload" | "type"> & {
959
974
  readonly type: "agent.turn.started";
960
975
  readonly payload: {
961
976
  readonly turn: number;
977
+ readonly messageCount?: number | undefined;
962
978
  };
963
979
  }) | (Omit<EventInput, "payload" | "type"> & {
964
980
  readonly type: "agent.turn.stopped";
@@ -1592,10 +1608,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
1592
1608
  readonly arguments: import("effect/Schema").Json;
1593
1609
  };
1594
1610
  };
1611
+ }) | (Omit<EventInput, "payload" | "type"> & {
1612
+ readonly type: "agent.turn.settled";
1613
+ readonly payload: {
1614
+ readonly turn: number;
1615
+ };
1595
1616
  }) | (Omit<EventInput, "payload" | "type"> & {
1596
1617
  readonly type: "agent.turn.started";
1597
1618
  readonly payload: {
1598
1619
  readonly turn: number;
1620
+ readonly messageCount?: number | undefined;
1599
1621
  };
1600
1622
  }) | (Omit<EventInput, "payload" | "type"> & {
1601
1623
  readonly type: "agent.turn.stopped";
@@ -2092,10 +2114,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
2092
2114
  readonly arguments: import("effect/Schema").Json;
2093
2115
  };
2094
2116
  };
2117
+ }) | (Omit<EventInput, "payload" | "type"> & {
2118
+ readonly type: "agent.turn.settled";
2119
+ readonly payload: {
2120
+ readonly turn: number;
2121
+ };
2095
2122
  }) | (Omit<EventInput, "payload" | "type"> & {
2096
2123
  readonly type: "agent.turn.started";
2097
2124
  readonly payload: {
2098
2125
  readonly turn: number;
2126
+ readonly messageCount?: number | undefined;
2099
2127
  };
2100
2128
  }) | (Omit<EventInput, "payload" | "type"> & {
2101
2129
  readonly type: "agent.turn.stopped";
@@ -15,6 +15,10 @@ export declare const agentModule: import("@swirls/looms/core").ModuleScope<"agen
15
15
  }>;
16
16
  'turn.started': import("effect/Schema").Struct<{
17
17
  readonly turn: import("effect/Schema").Finite;
18
+ readonly messageCount: import("effect/Schema").optional<import("effect/Schema").Finite>;
19
+ }>;
20
+ 'turn.settled': import("effect/Schema").Struct<{
21
+ readonly turn: import("effect/Schema").Finite;
18
22
  }>;
19
23
  'turn.text_delta': import("effect/Schema").Struct<{
20
24
  readonly turn: import("effect/Schema").Finite;
@@ -222,6 +226,10 @@ export declare const agentModule: import("@swirls/looms/core").ModuleScope<"agen
222
226
  }>;
223
227
  'turn.started': import("effect/Schema").Struct<{
224
228
  readonly turn: import("effect/Schema").Finite;
229
+ readonly messageCount: import("effect/Schema").optional<import("effect/Schema").Finite>;
230
+ }>;
231
+ 'turn.settled': import("effect/Schema").Struct<{
232
+ readonly turn: import("effect/Schema").Finite;
225
233
  }>;
226
234
  'turn.text_delta': import("effect/Schema").Struct<{
227
235
  readonly turn: import("effect/Schema").Finite;
@@ -35,6 +35,10 @@ function attachToolCall(lines, toolCall) {
35
35
  }
36
36
  return lines.with(index, { ...line, toolCalls: [...(line.toolCalls ?? []), toolCall] });
37
37
  }
38
+ /** Schedule another LLM turn only when no tool calls are still open. */
39
+ function wakeIfIdle(state) {
40
+ return state.pendingToolCalls.length === 0;
41
+ }
38
42
  /** Drop the turn in progress: answer its open tool calls and cancel the children they started. */
39
43
  function abandonTurn(state) {
40
44
  return {
@@ -48,6 +52,8 @@ function abandonTurn(state) {
48
52
  pendingEmits: [],
49
53
  pendingEffectTools: {},
50
54
  needsLlmCall: false,
55
+ deferWake: false,
56
+ midCallMessages: [],
51
57
  abandonedChildThreadIds: [
52
58
  ...(state.abandonedChildThreadIds ?? []),
53
59
  ...state.pendingSpawns.map((item) => item.childThreadId),
@@ -76,6 +82,8 @@ export const agentThread = agentModule.thread({
76
82
  output: null,
77
83
  pendingEffectTools: {},
78
84
  consumedSteerMessageIds: [],
85
+ deferWake: false,
86
+ midCallMessages: [],
79
87
  }),
80
88
  step(state, event, ctx) {
81
89
  switch (event.type) {
@@ -99,22 +107,38 @@ export const agentThread = agentModule.thread({
99
107
  case 'agent.lineage':
100
108
  return { ...state, depth: event.payload.depth };
101
109
  case 'agent.turn.started': {
110
+ // Mid-call steers are already in `lines` when this fires (first event of the batch).
111
+ // Defer the wake until `agent.turn.settled` so we do not schedule the next LLM before
112
+ // `agent.message` / tool-call events from this same turn are applied. Snapshot the
113
+ // extras so compaction can re-attach them instead of losing the steer.
114
+ const messageCount = event.payload.messageCount ?? state.lines.length;
115
+ const midCallMessages = state.lines.slice(messageCount);
116
+ const arrivedMidCall = midCallMessages.length > 0;
102
117
  return {
103
118
  ...state,
104
119
  turn: event.payload.turn,
105
120
  needsLlmCall: false,
121
+ deferWake: arrivedMidCall,
122
+ midCallMessages: arrivedMidCall ? midCallMessages : [],
106
123
  pendingSteer: null,
107
124
  abandonedChildThreadIds: [],
108
125
  };
109
126
  }
127
+ case 'agent.turn.settled': {
128
+ const deferWake = state.deferWake === true;
129
+ return {
130
+ ...state,
131
+ deferWake: false,
132
+ midCallMessages: [],
133
+ needsLlmCall: deferWake && wakeIfIdle(state),
134
+ };
135
+ }
110
136
  case 'agent.message.received': {
111
137
  const message = event.payload.message;
112
- const lines = [...state.lines, message];
113
- const needsLlmCall = state.pendingToolCalls.length === 0;
114
138
  return {
115
139
  ...state,
116
- lines,
117
- needsLlmCall,
140
+ lines: [...state.lines, message],
141
+ needsLlmCall: wakeIfIdle(state),
118
142
  };
119
143
  }
120
144
  case 'agent.message': {
@@ -152,7 +176,6 @@ export const agentThread = agentModule.thread({
152
176
  name: name ?? undefined,
153
177
  },
154
178
  ];
155
- const needsLlmCall = pendingToolCalls.length === 0;
156
179
  return {
157
180
  ...state,
158
181
  lines,
@@ -162,7 +185,7 @@ export const agentThread = agentModule.thread({
162
185
  pendingSpawns,
163
186
  pendingEffects,
164
187
  pendingEmits,
165
- needsLlmCall,
188
+ needsLlmCall: wakeIfIdle({ pendingToolCalls }),
166
189
  };
167
190
  }
168
191
  case 'agent.steered': {
@@ -188,18 +211,22 @@ export const agentThread = agentModule.thread({
188
211
  ...state,
189
212
  lines: [...state.lines, message],
190
213
  pendingSteer: message,
191
- needsLlmCall: state.pendingToolCalls.length === 0,
214
+ needsLlmCall: wakeIfIdle(state),
192
215
  consumedSteerMessageIds,
193
216
  };
194
217
  }
195
218
  case 'agent.context.compacted': {
196
219
  const compactedMessages = event.payload.messages;
220
+ const midCallMessages = state.midCallMessages ?? [];
197
221
  const alreadyCompactedTurns = state.alreadyCompactedTurns
198
222
  ? [...state.alreadyCompactedTurns, event.payload.turn]
199
223
  : [event.payload.turn];
224
+ const baseLines = compactedMessages ? [...compactedMessages] : state.lines;
200
225
  return {
201
226
  ...state,
202
- lines: compactedMessages ? [...compactedMessages] : state.lines,
227
+ // Compaction replaces `lines`; put mid-call steers back so the deferred wake still
228
+ // has something new to answer.
229
+ lines: midCallMessages.length > 0 ? [...baseLines, ...midCallMessages] : baseLines,
203
230
  alreadyCompactedTurns,
204
231
  };
205
232
  }
@@ -101,6 +101,16 @@ export interface AgentState {
101
101
  * Optional so snapshots taken before turns could be abandoned still fold.
102
102
  */
103
103
  abandonedChildThreadIds?: string[];
104
+ /**
105
+ * Set when a steer arrived during the LLM call. Honored by `agent.turn.settled` after the
106
+ * rest of the turn's events so we do not schedule the next call before `agent.message`.
107
+ */
108
+ deferWake?: boolean;
109
+ /**
110
+ * User/system lines that arrived after the LLM call began. Re-attached after compaction so a
111
+ * mid-call steer is not lost when `lines` are replaced.
112
+ */
113
+ midCallMessages?: Message[];
104
114
  }
105
115
  export type AgentMessageDelivery = 'steer' | 'followUp' | 'nextTurn';
106
116
  export type AgentSessionTurnStatus = 'completed' | 'failed' | 'cancelled';
@@ -61,10 +61,14 @@ export class LoomsDurableObject extends DurableObject {
61
61
  worker: config.worker,
62
62
  });
63
63
  const delivery = store.projectorDelivery;
64
- const recoverProjectors = delivery
65
- ? Effect.runPromise(delivery.recover(runId).pipe(Effect.andThen(delivery.deliver(runId))))
66
- : Promise.resolve();
67
- return recoverProjectors.then(() => cell.recoverWake()).then(() => cell);
64
+ // Projector recover/deliver opens Hyperdrive/Postgres and runs projector.init.
65
+ // That must not block the first request on a cold (or empty) cell. Schedule it
66
+ // the same way post-append delivery uses waitUntil; deliverExclusive still
67
+ // serializes per run and projector, and alarms retry from durable cursors.
68
+ if (delivery) {
69
+ this.ctx.waitUntil(Effect.runPromise(delivery.recover(runId).pipe(Effect.andThen(delivery.deliver(runId)))));
70
+ }
71
+ return cell.recoverWake().then(() => cell);
68
72
  });
69
73
  }
70
74
  fetch(req) {
@@ -146,10 +150,14 @@ export class LoomsDurableObject extends DurableObject {
146
150
  return this.getCell()
147
151
  .then((cell) => {
148
152
  const delivery = this.store?.projectorDelivery;
149
- const delivered = delivery
150
- ? Effect.runPromise(delivery.deliver(this.ctx.id.name ?? ''))
151
- : Promise.resolve();
152
- return delivered.then(() => cell.wake());
153
+ const runId = this.ctx.id.name ?? '';
154
+ // Projector delivery must not delay timer wakes; waitUntil keeps the
155
+ // isolate alive until delivery finishes, and failed work is retried from
156
+ // durable cursors on the next alarm or request.
157
+ if (delivery) {
158
+ this.ctx.waitUntil(Effect.runPromise(delivery.deliver(runId)));
159
+ }
160
+ return cell.wake();
153
161
  })
154
162
  .then(() => undefined);
155
163
  }