@omercnet/paseo-omp 0.4.0-next.120.1 → 0.4.0-next.123.1

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.
@@ -29,6 +29,8 @@ import {
29
29
  } from "./config-normalization";
30
30
  import { OmpHostToolsBridge, type OmpMcpConnector, validateOmpHostToolConfig } from "./host-tools";
31
31
  import { isOmpImageMimeType, OmpImageMaterializer } from "./image";
32
+ import type { OmpRuntime, OmpRuntimeSession } from "./omp-rpc";
33
+ import { buildOmpSpawnRequest, type OmpStartOptions } from "./omp-rpc-environment";
32
34
  import type {
33
35
  OmpAvailableCommand,
34
36
  OmpBranchResult,
@@ -37,15 +39,12 @@ import type {
37
39
  OmpMessage,
38
40
  OmpModel,
39
41
  OmpRpcEvent,
40
- OmpRuntime,
41
- OmpRuntimeSession,
42
42
  OmpSessionState,
43
43
  OmpSessionStats,
44
44
  OmpToolApprovalCancel,
45
45
  OmpToolApprovalRequest,
46
- } from "./omp-rpc";
47
- import { OmpRpcRequestRejectedError } from "./omp-rpc";
48
- import { buildOmpSpawnRequest, type OmpStartOptions } from "./omp-rpc-environment";
46
+ } from "./omp-rpc-protocol";
47
+ import { OmpRpcRequestRejectedError } from "./omp-rpc-transport";
49
48
  import {
50
49
  inlinePromptFrameBytes,
51
50
  isSafeCommandName,
@@ -67,6 +66,19 @@ import {
67
66
  } from "./security";
68
67
  import type { OmpSessionDescriptor } from "./session-descriptors";
69
68
  import { validateNativeSessionId } from "./session-descriptors";
69
+ import { isNativeTurnActivity, isPassiveUiMethod, isRuntimeConfigEvent } from "./session-events";
70
+ import {
71
+ type ActiveTurn,
72
+ assistantTerminalOutcome,
73
+ classifyTerminalCandidate,
74
+ createActiveTurn,
75
+ historyTerminalOutcome,
76
+ nativeEntryId,
77
+ type PendingUser,
78
+ type TerminalCandidate,
79
+ terminalOutcome,
80
+ unknownTerminalOutcomeError,
81
+ } from "./session-terminal";
70
82
  import { OmpSubsessionProjector } from "./subsessions";
71
83
  import {
72
84
  defaultOmpTimelineScheduler,
@@ -236,86 +248,6 @@ async function waitForReplay<T>(operation: Promise<T>, signal: AbortSignal): Pro
236
248
  }
237
249
  }
238
250
 
239
- type PendingUser = {
240
- clientMessageId: string;
241
- text: string;
242
- accepted: boolean;
243
- fallbackOnFinish: boolean;
244
- bufferedEchoes: OmpMessage[];
245
- };
246
- type TerminalCorrelationEvidence = "fresh-native-user" | "current-assistant";
247
-
248
- type TerminalCorrelation = {
249
- policy: "initial-turn" | "ordered-legacy" | "native-command";
250
- evidence: Map<TerminalCorrelationEvidence, number>;
251
- };
252
-
253
- type TerminalCandidate = {
254
- event: Extract<OmpRpcEvent, { type: "agent_end" }>;
255
- arrivalSequence: number;
256
- confidence:
257
- | "keyed"
258
- | "initial-turn"
259
- | "ordered-legacy"
260
- | "native-command"
261
- | "interrupted"
262
- | "ambiguous";
263
- };
264
-
265
- type ActiveTurn = {
266
- turnId: string;
267
- clientMessageId: string;
268
- generation: number;
269
- promptResultEmitted: boolean;
270
- started: boolean;
271
- terminal: boolean;
272
- interrupted: boolean;
273
- starting: boolean;
274
- nativeActivity: boolean;
275
- userEchoObserved: boolean;
276
- localOnlyDisabled: boolean;
277
- localOnlyEligible: boolean;
278
- awaitingPermissionEvidence: boolean;
279
- activitySequence: number;
280
- acknowledged: boolean;
281
- terminalCorrelation: TerminalCorrelation;
282
- replayingBufferedEvents: boolean;
283
- agentInvoked?: boolean;
284
- nativeRequestId?: string;
285
- promptAcceptedEventIndex?: number;
286
- localOnlyTimer?: unknown;
287
- usagePollTimer?: unknown;
288
- usagePoll?: Promise<void>;
289
- usageSampleFloor: number;
290
- manualCompaction: boolean;
291
- manualCompactionPending: boolean;
292
- manualCompactionDeadlineTimer?: unknown;
293
- agentEndPending: boolean;
294
- agentEndRetryTimer?: unknown;
295
- agentEndDeadlineTimer?: unknown;
296
- agentEndCheck?: Promise<void>;
297
- ambiguousTerminalTimer?: unknown;
298
- terminalizing: boolean;
299
- terminalization?: Promise<void>;
300
- terminalOutcome?: TurnOutcome;
301
- operationalTerminalStage?: "failed" | "unresolved";
302
- terminalWake?: VoidDeferred;
303
- steerReady: VoidDeferred;
304
- steersInFlight: number;
305
- deferredAgentEnd?: TerminalCandidate;
306
- activeToolCallIds: Set<string>;
307
- bufferedEvents: OmpRpcEvent[];
308
- pendingUsers: PendingUser[];
309
- userEchoes: OmpMessage[];
310
- userCorrelationActive: boolean;
311
- userLookups: Set<Promise<void>>;
312
- completedMessageCount: number;
313
- streamedMessageEntryIds: string[];
314
- streamedMessageIdentityComplete: boolean;
315
- lastCompletedAssistantOutcome?: AgentEndOutcome;
316
- lastCompletedAssistantEntryId?: string;
317
- };
318
-
319
251
  type PendingAbort = {
320
252
  turn: ActiveTurn;
321
253
  generation: number;
@@ -363,18 +295,6 @@ function permissionFingerprint(request: OmpQuestionRequest): string {
363
295
  return createHash("sha256").update(JSON.stringify(request)).digest("base64url");
364
296
  }
365
297
 
366
- type VoidDeferred = {
367
- promise: Promise<void>;
368
- resolve(value?: void | PromiseLike<void>): void;
369
- reject(reason?: unknown): void;
370
- };
371
-
372
- type TurnOutcome = {
373
- state: "completed" | "failed" | "canceled";
374
- error?: ProviderError;
375
- usageSampled: boolean;
376
- };
377
-
378
298
  type ActiveCompaction = {
379
299
  id: string;
380
300
  trigger: "auto" | "manual";
@@ -409,258 +329,6 @@ async function settleSessionCleanup(promises: readonly Promise<void>[]): Promise
409
329
  throw new AggregateError(failures, "OMP session initialization cleanup failed");
410
330
  }
411
331
  }
412
- function nativeEntryId(message: OmpMessage): string | undefined {
413
- return message.entryId;
414
- }
415
-
416
- type AgentEndOutcome = "completed" | "failed" | "canceled";
417
- type AssistantTerminalStatus = AgentEndOutcome | "unavailable";
418
-
419
- function assistantTerminalOutcome(
420
- message: Extract<OmpMessage, { role: "assistant" }>,
421
- ): AgentEndOutcome {
422
- const stopReason = message.stopReason?.toLowerCase();
423
- if (stopReason === "aborted" || stopReason === "canceled" || stopReason === "cancelled") {
424
- return "canceled";
425
- }
426
- return stopReason === "error" || message.errorMessage ? "failed" : "completed";
427
- }
428
-
429
- function lastAssistantStatus(
430
- messages: readonly OmpMessage[],
431
- startIndex = 0,
432
- ): AssistantTerminalStatus {
433
- for (let index = messages.length - 1; index >= startIndex; index -= 1) {
434
- const message = messages[index];
435
- if (message?.role !== "assistant") continue;
436
- return assistantTerminalOutcome(message);
437
- }
438
- return "unavailable";
439
- }
440
-
441
- function terminalOutcome(
442
- event: Extract<OmpRpcEvent, { type: "agent_end" }>,
443
- turn: Pick<ActiveTurn, "completedMessageCount" | "lastCompletedAssistantOutcome">,
444
- ): AgentEndOutcome | undefined {
445
- const messages = event.messages;
446
- if (
447
- messages !== undefined &&
448
- (event.messageCount === undefined || messages.length >= event.messageCount)
449
- ) {
450
- const status = lastAssistantStatus(messages);
451
- return status === "unavailable" ? "completed" : status;
452
- }
453
- if (event.messageCount === 0) return "completed";
454
- if (
455
- turn.lastCompletedAssistantOutcome !== undefined &&
456
- (event.messageCount === undefined || turn.completedMessageCount >= event.messageCount)
457
- ) {
458
- return turn.lastCompletedAssistantOutcome;
459
- }
460
- return undefined;
461
- }
462
-
463
- function historyTerminalOutcome(
464
- messages: readonly OmpMessage[],
465
- declaredCount: number,
466
- turn: Pick<
467
- ActiveTurn,
468
- | "streamedMessageEntryIds"
469
- | "streamedMessageIdentityComplete"
470
- | "lastCompletedAssistantOutcome"
471
- | "lastCompletedAssistantEntryId"
472
- >,
473
- retainedMessages: readonly OmpMessage[],
474
- ): AgentEndOutcome | undefined {
475
- if (
476
- messages.length < declaredCount ||
477
- !turn.streamedMessageIdentityComplete ||
478
- turn.streamedMessageEntryIds.length === 0
479
- ) {
480
- return undefined;
481
- }
482
- const startIndex = messages.length - declaredCount;
483
- let historyIndex = startIndex;
484
- for (const entryId of turn.streamedMessageEntryIds) {
485
- while (historyIndex < messages.length) {
486
- const historyMessage = messages[historyIndex];
487
- if (historyMessage && nativeEntryId(historyMessage) === entryId) break;
488
- historyIndex += 1;
489
- }
490
- if (historyIndex >= messages.length) return undefined;
491
- const correlated = messages[historyIndex];
492
- if (
493
- entryId === turn.lastCompletedAssistantEntryId &&
494
- (correlated?.role !== "assistant" ||
495
- assistantTerminalOutcome(correlated) !== turn.lastCompletedAssistantOutcome)
496
- )
497
- return undefined;
498
- historyIndex += 1;
499
- }
500
- historyIndex = startIndex;
501
- for (const retained of retainedMessages) {
502
- const entryId = nativeEntryId(retained);
503
- if (!entryId) return undefined;
504
- while (historyIndex < messages.length) {
505
- const historyMessage = messages[historyIndex];
506
- if (historyMessage && nativeEntryId(historyMessage) === entryId) break;
507
- historyIndex += 1;
508
- }
509
- if (historyIndex >= messages.length) return undefined;
510
- const correlated = messages[historyIndex];
511
- if (!correlated || correlated.role !== retained.role) return undefined;
512
- if (
513
- retained.role === "assistant" &&
514
- correlated.role === "assistant" &&
515
- assistantTerminalOutcome(retained) !== assistantTerminalOutcome(correlated)
516
- )
517
- return undefined;
518
- historyIndex += 1;
519
- }
520
- const status = lastAssistantStatus(messages, startIndex);
521
- return status === "unavailable" ? undefined : status;
522
- }
523
-
524
- function unknownTerminalOutcomeError(
525
- event: Extract<OmpRpcEvent, { type: "agent_end" }>,
526
- turn: Pick<ActiveTurn, "completedMessageCount" | "lastCompletedAssistantOutcome">,
527
- ): string {
528
- const retainedMessages = event.messages?.length ?? 0;
529
- const retainedStatus = event.messages ? lastAssistantStatus(event.messages) : "unavailable";
530
- const lastStatus =
531
- retainedStatus === "unavailable"
532
- ? (turn.lastCompletedAssistantOutcome ?? "unavailable")
533
- : retainedStatus;
534
- return (
535
- "OMP agent_end omitted terminal messages; outcome is unknown " +
536
- `(declaredCount=${event.messageCount ?? "unavailable"}, ` +
537
- `observedCount=${turn.completedMessageCount}, ` +
538
- `retainedTerminalMessages=${retainedMessages}, lastAssistantStatus=${lastStatus})`
539
- );
540
- }
541
-
542
- function isNativeTurnActivity(event: OmpRpcEvent): boolean {
543
- if (
544
- event.type === "agent_start" ||
545
- event.type === "turn_start" ||
546
- event.type === "turn_end" ||
547
- event.type === "agent_end" ||
548
- event.type === "auto_compaction_start" ||
549
- event.type === "auto_compaction_end"
550
- ) {
551
- return true;
552
- }
553
- if (
554
- event.type === "message_start" ||
555
- event.type === "message_update" ||
556
- event.type === "message_end"
557
- ) {
558
- return event.message.role === "assistant";
559
- }
560
- return event.type.startsWith("tool_execution_");
561
- }
562
- function isRuntimeConfigEvent(event: OmpRpcEvent): boolean {
563
- return (
564
- event.type === "model_changed" ||
565
- event.type === "thinking_level_changed" ||
566
- event.type === "retry_fallback_applied" ||
567
- event.type === "retry_fallback_succeeded"
568
- );
569
- }
570
-
571
- function isPassiveUiMethod(method: string): boolean {
572
- return (
573
- method === "cancel" ||
574
- method === "notify" ||
575
- method === "open_url" ||
576
- method === "setStatus" ||
577
- method === "setWidget" ||
578
- method === "setTitle" ||
579
- method === "set_editor_text"
580
- );
581
- }
582
- function createActiveTurn(
583
- clientMessageId: string,
584
- text: string,
585
- generation: number,
586
- terminalPolicy: TerminalCorrelation["policy"],
587
- manualCompaction = false,
588
- ): ActiveTurn {
589
- return {
590
- turnId: randomUUID(),
591
- clientMessageId,
592
- agentInvoked: undefined,
593
- generation,
594
- promptResultEmitted: false,
595
- started: false,
596
- terminal: false,
597
- awaitingPermissionEvidence: false,
598
- interrupted: false,
599
- starting: true,
600
- nativeActivity: false,
601
- userEchoObserved: false,
602
- localOnlyDisabled: false,
603
- localOnlyEligible: false,
604
- usageSampleFloor: 0,
605
- agentEndPending: false,
606
- terminalizing: false,
607
- manualCompactionPending: manualCompaction,
608
- manualCompaction,
609
- activitySequence: 0,
610
- acknowledged: false,
611
- terminalCorrelation: { policy: terminalPolicy, evidence: new Map() },
612
- replayingBufferedEvents: false,
613
- steersInFlight: 0,
614
- activeToolCallIds: new Set(),
615
- steerReady: Promise.withResolvers<void>(),
616
- userCorrelationActive: false,
617
- userLookups: new Set(),
618
- userEchoes: [],
619
- completedMessageCount: 0,
620
- bufferedEvents: [],
621
- streamedMessageEntryIds: [],
622
- streamedMessageIdentityComplete: true,
623
- pendingUsers: [
624
- {
625
- clientMessageId,
626
- text,
627
- accepted: true,
628
- fallbackOnFinish: true,
629
- bufferedEchoes: [],
630
- },
631
- ],
632
- };
633
- }
634
- function classifyTerminalCandidate(
635
- turn: ActiveTurn,
636
- event: Extract<OmpRpcEvent, { type: "agent_end" }>,
637
- arrivalSequence = turn.activitySequence,
638
- ): TerminalCandidate {
639
- if (event.requestId !== undefined) return { event, confidence: "keyed", arrivalSequence };
640
- if (turn.agentInvoked === false) return { event, confidence: "ambiguous", arrivalSequence };
641
- if (turn.terminalCorrelation.policy === "initial-turn") {
642
- return { event, confidence: "initial-turn", arrivalSequence };
643
- }
644
- const assistantSequence = turn.terminalCorrelation.evidence.get("current-assistant");
645
- if (
646
- turn.terminalCorrelation.policy === "native-command" &&
647
- assistantSequence !== undefined &&
648
- assistantSequence <= arrivalSequence
649
- ) {
650
- return { event, confidence: "native-command", arrivalSequence };
651
- }
652
- const userSequence = turn.terminalCorrelation.evidence.get("fresh-native-user");
653
- if (
654
- userSequence !== undefined &&
655
- assistantSequence !== undefined &&
656
- userSequence < assistantSequence &&
657
- assistantSequence <= arrivalSequence
658
- ) {
659
- return { event, confidence: "ordered-legacy", arrivalSequence };
660
- }
661
- return { event, confidence: "ambiguous", arrivalSequence };
662
- }
663
-
664
332
  export class OmpProviderSession {
665
333
  readonly id: string;
666
334
  readonly cwd: string;
@@ -2,14 +2,13 @@ import { createHash } from "node:crypto";
2
2
  import { basename, extname, isAbsolute } from "node:path";
3
3
  import type { ProviderEvent } from "@getpaseo/plugin/server/provider";
4
4
  import { z } from "zod";
5
+ import type { OmpRuntime, OmpRuntimeSession } from "./omp-rpc";
5
6
  import type {
6
7
  OmpAgentSessionEvent,
7
8
  OmpMessage,
8
- OmpRuntime,
9
- OmpRuntimeSession,
10
9
  OmpSubagentEvent,
11
10
  OmpSubagentSnapshot,
12
- } from "./omp-rpc";
11
+ } from "./omp-rpc-protocol";
13
12
  import {
14
13
  BoundedStringSet,
15
14
  boundedJsonBytes,
@@ -11,7 +11,7 @@ import {
11
11
  OMP_MESSAGE_REPLAY_POLICIES,
12
12
  type OmpMessage,
13
13
  type OmpRpcEvent,
14
- } from "./omp-rpc";
14
+ } from "./omp-rpc-protocol";
15
15
  import {
16
16
  boundedJsonBytes,
17
17
  type JsonValue,