@theoremjs/agents 0.3.4-next.787.g385fa7e → 0.3.4-next.791.gd4aff2e

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,5 +1,10 @@
1
1
  import type { BoundSystem } from '../../system-parts.js';
2
2
  import type { Profile, ResolvedGeneration, TurnEvent, TurnModel, TurnRequest } from '../../types.js';
3
3
  import { type StepExecutionState } from './state.js';
4
+ /** A turn whose final output the egress check blocked (withheld or replaced by policy copy). */
5
+ export declare const EGRESS_FILTERED_STOP: {
6
+ readonly kind: "filtered";
7
+ readonly native: "egress";
8
+ };
4
9
  declare function runAttemptsWithValidation(safe: TurnRequest, profile: Profile, generation: ResolvedGeneration, system: BoundSystem, provider: TurnModel, state: StepExecutionState): AsyncGenerator<TurnEvent>;
5
10
  export { runAttemptsWithValidation };
@@ -21,7 +21,10 @@ import { appendUserInput, recordStepEvent, } from './state.js';
21
21
  import { executeAttempt } from './steps.js';
22
22
  import { isWithheldOnBlock, shownToHost } from './stream.js';
23
23
  /** A turn whose final output the egress check blocked (withheld or replaced by policy copy). */
24
- const EGRESS_FILTERED_STOP = { kind: 'filtered', native: 'egress' };
24
+ export const EGRESS_FILTERED_STOP = {
25
+ kind: 'filtered',
26
+ native: 'egress',
27
+ };
25
28
  /** Reply text only: thoughts are not guarded output (`isGuardedOutput`). */
26
29
  function collectAttemptText(events) {
27
30
  const parts = [];
@@ -32,6 +32,7 @@ import { cloneTurnToolSnapshot } from '../../tools/resolve.js';
32
32
  import { turnDoneOf } from '../../turn-events.js';
33
33
  import { prepareLiveContext, prepareLiveInboundText } from '../live-inbound.js';
34
34
  import { assertLiveIngress } from '../live-ingress.js';
35
+ import { EGRESS_FILTERED_STOP } from '../runner/gates.js';
35
36
  import { sessionAgentCaller } from '../runner/mod.js';
36
37
  import { startLiveTrace } from './session-trace.js';
37
38
  function pageAnswerForLiveRequest(page, execution) {
@@ -198,6 +199,19 @@ async function applyOutbound(gate, events, turnPhase, onWithhold) {
198
199
  function isTurnComplete(event) {
199
200
  return event.type === 'session' && event.session.kind === 'turn_complete';
200
201
  }
202
+ function isGenerationDone(event) {
203
+ return event.type === 'done' && event.stop.kind === 'generation_complete';
204
+ }
205
+ /** The close of a reply already reported interrupted: only its checkpoint goes on. */
206
+ function checkpointOfClose(events) {
207
+ return events.flatMap((event) => {
208
+ if (event.type !== 'done')
209
+ return [event];
210
+ return event.providerState
211
+ ? [{ type: 'provider_checkpoint', providerState: event.providerState }]
212
+ : [];
213
+ });
214
+ }
201
215
  /**
202
216
  * The output a settled Live call sends upstream, or `undefined` when nothing
203
217
  * is sent (a held gate, or no result). The model reads it as the `functionResponse`:
@@ -237,9 +251,11 @@ function* drainPendingHostEvents(pendingHostEvents) {
237
251
  yield pending;
238
252
  }
239
253
  }
240
- function boundaryDoneEvents(doneBatch, turnPhase, interrupted) {
254
+ function boundaryDoneEvents(doneBatch, turnPhase, interrupted, withheld) {
241
255
  if (doneBatch.length > 0)
242
256
  return doneBatch;
257
+ if (withheld)
258
+ return [{ type: 'done', stop: EGRESS_FILTERED_STOP }];
243
259
  if (turnPhase === 'abort' || interrupted) {
244
260
  return [{ type: 'done', stop: { kind: 'interrupted' }, interrupted: true }];
245
261
  }
@@ -335,6 +351,8 @@ function buildLiveSession(args) {
335
351
  },
336
352
  };
337
353
  let cycle = 'idle';
354
+ /** How the reply stands: an interrupt before `generation_complete` cuts it off. */
355
+ let reply = 'open';
338
356
  let cycleStep = 0;
339
357
  /** What this cycle's tool results read from outside the host; a cycle starts clean, as a turn does. */
340
358
  let cycleTaint;
@@ -859,29 +877,48 @@ function buildLiveSession(args) {
859
877
  // why: An abort only drops what the gate held; there is no check to time.
860
878
  if (item.turnPhase !== 'abort')
861
879
  trace.outboundTime(performance.now() - gateStart);
880
+ if (hostEvents.some(isGenerationDone))
881
+ reply = 'generated';
882
+ // why: A close after a cut-off reply is not a finish, and the next cycle may already be open.
883
+ const closesCutOff = item.turnPhase === 'complete' && reply === 'cut_off';
884
+ if (item.turnPhase === 'complete')
885
+ reply = 'open';
886
+ if (item.turnPhase === 'abort') {
887
+ const cut = reply !== 'generated' && hostEvents.some((ev) => ev.type === 'done' && ev.interrupted);
888
+ reply = cut ? 'cut_off' : 'open';
889
+ }
862
890
  for (const ev of gated) {
863
891
  if (ev.type === 'guardrail')
864
892
  trace.outbound(ev);
865
893
  }
866
894
  recordLiveHistory(gated, recordUserText, recordAssistantText);
867
- const sealed = await sealLiveCheckpoints(gated, history, calls, profile.providerContinuation?.history !== 'stateless');
895
+ const sealed = await sealLiveCheckpoints(closesCutOff ? checkpointOfClose(gated) : gated, history, calls, profile.providerContinuation?.history !== 'stateless');
868
896
  const doneBatch = yield* yieldLiveNonDoneEvents(sealed, includeMatch);
869
897
  const tokens = await trace.settle();
870
898
  if (tokens)
871
899
  yield tokens;
872
- const interrupted = doneBatch.some((ev) => ev.type === 'done' && ev.interrupted);
873
- const completeBoundary = item.turnPhase === 'complete' || item.turnPhase === 'abort' || doneBatch.length > 0;
900
+ // why: `generation_complete` is news, not the end: the verdict on the reply comes after it.
901
+ for (const ev of doneBatch.filter(isGenerationDone)) {
902
+ yield projectGuardrailTurnEvent(stampDone(ev), includeMatch);
903
+ }
904
+ const ending = doneBatch.filter((ev) => !isGenerationDone(ev));
905
+ const interrupted = ending.some((ev) => ev.type === 'done' && ev.interrupted);
906
+ const completeBoundary = !closesCutOff &&
907
+ (item.turnPhase === 'complete' ||
908
+ item.turnPhase === 'abort' ||
909
+ withholdClose ||
910
+ ending.length > 0);
874
911
  // why: Boundary batches (`interactionStatus: IDLE`, or bare `turnComplete` when the
875
912
  // provider sends no status) often have no folded `done` — still end the cycle.
876
913
  if (completeBoundary && cycle === 'open') {
877
- const boundaryDone = boundaryDoneEvents(doneBatch, item.turnPhase, interrupted);
914
+ const boundaryDone = boundaryDoneEvents(ending, item.turnPhase, interrupted, withholdClose);
878
915
  for await (const ev of endCycleAroundDone(boundaryDone)) {
879
916
  yield projectGuardrailTurnEvent(stampDone(ev), includeMatch);
880
917
  }
881
918
  yield* drainPendingHostEvents(pendingHostEvents);
882
919
  }
883
- else if (doneBatch.length > 0) {
884
- for (const ev of doneBatch) {
920
+ else {
921
+ for (const ev of ending) {
885
922
  yield projectGuardrailTurnEvent(stampDone(ev), includeMatch);
886
923
  }
887
924
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.4-next.787.g385fa7e",
3
+ "version": "0.3.4-next.791.gd4aff2e",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",