@modelprofile.com/flexharness-agent 16.3.0 → 16.4.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.
@@ -149,17 +149,23 @@ const isModelOutputEvent = (event: TAgentEvent): boolean =>
149
149
  || event.type === 'tool-result'
150
150
  || event.type === 'model-message';
151
151
 
152
- /** Accepted and active generations, and terminal generations with actual model/tool output. */
152
+ /**
153
+ * Accepted and active generations, and terminal generations with actual model/tool output. An
154
+ * ephemeral generation is visible only until its outcome.
155
+ */
153
156
  const isModelVisibleTransaction = (
154
157
  transaction: IAgentGenerationTransaction,
155
158
  producedOutput: ReadonlySet<string>,
156
159
  ): boolean =>
157
- transaction.state === 'accepted'
158
- || transaction.state === 'execution-started'
159
- || (
160
- (transaction.state === 'interrupted' || transaction.state === 'rejected')
161
- && transaction.executionStarted !== undefined
162
- && producedOutput.has(transaction.generationId)
160
+ (transaction.begun.ephemeral !== true || transaction.outcome === undefined)
161
+ && (
162
+ transaction.state === 'accepted'
163
+ || transaction.state === 'execution-started'
164
+ || (
165
+ (transaction.state === 'interrupted' || transaction.state === 'rejected')
166
+ && transaction.executionStarted !== undefined
167
+ && producedOutput.has(transaction.generationId)
168
+ )
163
169
  );
164
170
 
165
171
  /** The generations whose model produced output in `events`. */
@@ -199,12 +205,47 @@ const interruptedToolResult = (
199
205
  parentIds: [source.id],
200
206
  });
201
207
 
208
+ /**
209
+ * The events a model sees, in the order it sees them. A generation's pinned context message is
210
+ * committed once its lease is prepared, after its prompt; the model sees it right before the
211
+ * prompt. Nothing committed after the generation's first model call moves.
212
+ */
202
213
  export const filterModelVisibleAgentEvents = (
203
214
  events: readonly TAgentEvent[],
204
215
  ): TAgentEvent[] => {
205
216
  const transactions = getAgentGenerationTransactions(events);
206
217
  const producedOutput = generationsWithModelOutput(events);
207
218
  const resultedToolCalls = new Set<string>();
219
+ /** The pinned context messages of each generation whose prompt precedes them, by its prompt's id. */
220
+ const pinnedBeforePrompt = new Map<string, TAgentEvent[]>();
221
+ const promptIds = new Map<string, string>();
222
+ /**
223
+ * Generations that have started a model call. Only a pinned context message committed before its
224
+ * generation's first model call moves: once a call has been sent, every later call of the
225
+ * generation must extend that request byte for byte, so anything committed after it stays where
226
+ * it was appended.
227
+ */
228
+ const inferringGenerations = new Set<string>();
229
+ for (const event of events) {
230
+ if (!event.generationId) continue;
231
+ if (event.type === 'runtime-event' && event.event.type === 'inference-started') {
232
+ inferringGenerations.add(event.generationId);
233
+ continue;
234
+ }
235
+ if (event.type !== 'user-message') continue;
236
+ const transaction = transactions.get(event.generationId);
237
+ if (!event.pinnedContext && transaction && event.causationId === transaction.begun.id) {
238
+ promptIds.set(event.generationId, event.id);
239
+ } else if (
240
+ event.pinnedContext
241
+ && promptIds.has(event.generationId)
242
+ && !inferringGenerations.has(event.generationId)
243
+ ) {
244
+ const promptId = promptIds.get(event.generationId)!;
245
+ pinnedBeforePrompt.set(promptId, [...pinnedBeforePrompt.get(promptId) ?? [], event]);
246
+ }
247
+ }
248
+ const movedPinnedIds = new Set([...pinnedBeforePrompt.values()].flat().map((event) => event.id));
208
249
  for (const event of events) {
209
250
  if (!event.generationId) continue;
210
251
  if (event.type === 'tool-result') resultedToolCalls.add(`${event.generationId}\0${event.toolCallId}`);
@@ -254,7 +295,8 @@ export const filterModelVisibleAgentEvents = (
254
295
  }
255
296
  const transaction = transactions.get(event.generationId);
256
297
  if (transaction && !isModelVisibleTransaction(transaction, producedOutput)) continue;
257
- visibleEvents.push(event);
298
+ if (movedPinnedIds.has(event.id)) continue;
299
+ visibleEvents.push(...pinnedBeforePrompt.get(event.id) ?? [], event);
258
300
  if (transaction?.state !== 'interrupted' && transaction?.state !== 'rejected') continue;
259
301
  if (event.type === 'tool-call' && event.providerExecuted !== true) {
260
302
  closeOpenToolCall(event.toolCallId, event.toolName, event);