@moxt-ai/mobius 0.0.52 → 0.0.54

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.
@@ -0,0 +1,27 @@
1
+ import type { DaemonTimer } from "./timer.ts";
2
+ /**
3
+ * Coalesces streaming assistant text into a bounded, ordered event stream.
4
+ *
5
+ * Token-level Agents (Claude Code enables `includePartialMessages`) emit one
6
+ * update per streamed token, so a single final answer can arrive as hundreds or
7
+ * thousands of chunks. Forwarding each chunk as its own durable Session event
8
+ * floods the owning Attempt's replay window and stalls the turn's text behind
9
+ * the relay round trip. Buffering the text and flushing on a short timer bounds
10
+ * the event rate without splitting or reordering the answer.
11
+ *
12
+ * Chunks are buffered in a single slot rather than keyed by message id: the
13
+ * deltas of one turn are sequential, and a per-id map would reorder text that
14
+ * interleaves across ids when the slots flush.
15
+ */
16
+ export declare class CoalescingTextPublisher {
17
+ #private;
18
+ constructor(timer: DaemonTimer, publish: (messageId: string, text: string) => void, coalescingMilliseconds: number, maximumTextLength: number);
19
+ /** Emit any buffered text now, cancelling the pending flush. */
20
+ readonly flush: () => void;
21
+ /**
22
+ * Buffer a chunk, emitting the buffered text when the message changes or the
23
+ * buffer would exceed the protocol's per-event text limit.
24
+ */
25
+ readonly record: (messageId: string, text: string) => void;
26
+ }
27
+ //# sourceMappingURL=coalescing-text-publisher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coalescing-text-publisher.d.ts","sourceRoot":"","sources":["../../src/relay-connection/coalescing-text-publisher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAuB,WAAW,EAAE,MAAM,YAAY,CAAC;AAEnE;;;;;;;;;;;;;GAaG;AACH,qBAAa,uBAAuB;;IAShC,YACI,KAAK,EAAE,WAAW,EAClB,OAAO,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,EAClD,sBAAsB,EAAE,MAAM,EAC9B,iBAAiB,EAAE,MAAM,EAM5B;IAED,gEAAgE;IAChE,QAAQ,CAAC,KAAK,QAAO,IAAI,CAUvB;IAEF;;;OAGG;IACH,QAAQ,CAAC,MAAM,cAAe,MAAM,QAAQ,MAAM,KAAG,IAAI,CAavD;CACL"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Coalesces streaming assistant text into a bounded, ordered event stream.
3
+ *
4
+ * Token-level Agents (Claude Code enables `includePartialMessages`) emit one
5
+ * update per streamed token, so a single final answer can arrive as hundreds or
6
+ * thousands of chunks. Forwarding each chunk as its own durable Session event
7
+ * floods the owning Attempt's replay window and stalls the turn's text behind
8
+ * the relay round trip. Buffering the text and flushing on a short timer bounds
9
+ * the event rate without splitting or reordering the answer.
10
+ *
11
+ * Chunks are buffered in a single slot rather than keyed by message id: the
12
+ * deltas of one turn are sequential, and a per-id map would reorder text that
13
+ * interleaves across ids when the slots flush.
14
+ */
15
+ export class CoalescingTextPublisher {
16
+ #coalescingMilliseconds;
17
+ #maximumTextLength;
18
+ #publish;
19
+ #timer;
20
+ #messageId = "";
21
+ #scheduled = null;
22
+ #text = "";
23
+ constructor(timer, publish, coalescingMilliseconds, maximumTextLength) {
24
+ this.#publish = publish;
25
+ this.#timer = timer;
26
+ this.#coalescingMilliseconds = coalescingMilliseconds;
27
+ this.#maximumTextLength = maximumTextLength;
28
+ }
29
+ /** Emit any buffered text now, cancelling the pending flush. */
30
+ flush = () => {
31
+ this.#scheduled?.cancel();
32
+ this.#scheduled = null;
33
+ const messageId = this.#messageId;
34
+ const text = this.#text;
35
+ this.#messageId = "";
36
+ this.#text = "";
37
+ if (text.length > 0) {
38
+ this.#publish(messageId, text);
39
+ }
40
+ };
41
+ /**
42
+ * Buffer a chunk, emitting the buffered text when the message changes or the
43
+ * buffer would exceed the protocol's per-event text limit.
44
+ */
45
+ record = (messageId, text) => {
46
+ if (text.length === 0) {
47
+ return;
48
+ }
49
+ if (this.#text.length > 0 &&
50
+ (this.#messageId !== messageId || this.#text.length + text.length > this.#maximumTextLength)) {
51
+ this.flush();
52
+ }
53
+ this.#messageId = messageId;
54
+ this.#text = `${this.#text}${text}`;
55
+ this.#scheduled ??= this.#timer.schedule(this.flush, this.#coalescingMilliseconds);
56
+ };
57
+ }
58
+ //# sourceMappingURL=coalescing-text-publisher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coalescing-text-publisher.js","sourceRoot":"","sources":["../../src/relay-connection/coalescing-text-publisher.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,uBAAuB;IACvB,uBAAuB,CAAS;IAChC,kBAAkB,CAAS;IAC3B,QAAQ,CAA4C;IACpD,MAAM,CAAc;IAC7B,UAAU,GAAG,EAAE,CAAC;IAChB,UAAU,GAA+B,IAAI,CAAC;IAC9C,KAAK,GAAG,EAAE,CAAC;IAEX,YACI,KAAkB,EAClB,OAAkD,EAClD,sBAA8B,EAC9B,iBAAyB;QAEzB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,uBAAuB,GAAG,sBAAsB,CAAC;QACtD,IAAI,CAAC,kBAAkB,GAAG,iBAAiB,CAAC;IAChD,CAAC;IAED,gEAAgE;IACvD,KAAK,GAAG,GAAS,EAAE;QACxB,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;QAC1B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC;QAClC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC;QACxB,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAClB,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QACnC,CAAC;IACL,CAAC,CAAC;IAEF;;;OAGG;IACM,MAAM,GAAG,CAAC,SAAiB,EAAE,IAAY,EAAQ,EAAE;QACxD,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACpB,OAAO;QACX,CAAC;QACD,IACI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;YACrB,CAAC,IAAI,CAAC,UAAU,KAAK,SAAS,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,kBAAkB,CAAC,EAC9F,CAAC;YACC,IAAI,CAAC,KAAK,EAAE,CAAC;QACjB,CAAC;QACD,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,GAAG,IAAI,CAAC,KAAK,GAAG,IAAI,EAAE,CAAC;QACpC,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,uBAAuB,CAAC,CAAC;IACvF,CAAC,CAAC;CACL"}
@@ -1 +1 @@
1
- {"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/relay-connection/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAwBnC,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAsF9E"}
1
+ {"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/relay-connection/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAyBnC,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CA0F9E"}
@@ -1,6 +1,7 @@
1
1
  import { WebSocket } from "undici";
2
2
  import { environmentProxyDispatcher } from "../network/environment-proxy.js";
3
3
  import { runtimeAuthorization } from "./runtime-authentication.js";
4
+ import { isRuntimeRelayRevocation, RuntimeRelayRevokedError } from "./runtime-revocation.js";
4
5
  const CONNECTION_TIMEOUT_MILLISECONDS = 15_000;
5
6
  class RelayConnectionError extends Error {
6
7
  code;
@@ -46,6 +47,10 @@ export function connectRelay(url, signal) {
46
47
  }, { cause: signal.reason }));
47
48
  };
48
49
  const onClose = (event) => {
50
+ if (isRuntimeRelayRevocation(event.code, event.reason)) {
51
+ fail(new RuntimeRelayRevokedError(event.code, event.reason));
52
+ return;
53
+ }
49
54
  fail(new RelayConnectionError("relay_connection_closed", "Relay connection closed before it was ready", {
50
55
  closeCode: event.code.toString(),
51
56
  closeReason: event.reason,
@@ -1 +1 @@
1
- {"version":3,"file":"connect.js","sourceRoot":"","sources":["../../src/relay-connection/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEnC,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAEnE,MAAM,+BAA+B,GAAG,MAAM,CAAC;AAE/C,MAAM,oBAAqB,SAAQ,KAAK;IAC3B,IAAI,CAAS;IACb,iBAAiB,CAAmC;IAE7D,YACI,IAAY,EACZ,OAAe,EACf,iBAAiB,GAAqC,EAAE,EACxD,OAAO,GAAiB,EAAE;QAE1B,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACvC,CAAC;CACJ;AAED,MAAM,UAAU,YAAY,CAAC,GAAQ,EAAE,MAAmB;IACtD,MAAM,CAAC,cAAc,EAAE,CAAC;IAExB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;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AAC,CAAC;QAExD,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO,EAAE,CAAC;QACd,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * A Runtime relay revocation is terminal for this local Runtime. It is normalized from the
3
+ * deployed Relay close record so callers never need to branch on transport text.
4
+ */
5
+ export declare class RuntimeRelayRevokedError extends Error {
6
+ readonly code = "runtime_relay_revoked";
7
+ readonly closeCode: number;
8
+ readonly closeReason: string;
9
+ constructor(closeCode: number, closeReason: string, options?: ErrorOptions);
10
+ }
11
+ export declare function isRuntimeRelayRevocation(closeCode: number, closeReason: string): boolean;
12
+ //# sourceMappingURL=runtime-revocation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime-revocation.d.ts","sourceRoot":"","sources":["../../src/relay-connection/runtime-revocation.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,IAAI,2BAA2B;IACxC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAE7B,YAAY,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB,EAK7E;CACJ;AAED,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAExF"}
@@ -0,0 +1,21 @@
1
+ const runtimeRevocationCloseCode = 1008;
2
+ const runtimeRevocationCloseReason = "Runtime revoked";
3
+ /**
4
+ * A Runtime relay revocation is terminal for this local Runtime. It is normalized from the
5
+ * deployed Relay close record so callers never need to branch on transport text.
6
+ */
7
+ export class RuntimeRelayRevokedError extends Error {
8
+ code = "runtime_relay_revoked";
9
+ closeCode;
10
+ closeReason;
11
+ constructor(closeCode, closeReason, options = {}) {
12
+ super("The Runtime was revoked by the Relay", options);
13
+ this.closeCode = closeCode;
14
+ this.closeReason = closeReason;
15
+ this.name = "RuntimeRelayRevokedError";
16
+ }
17
+ }
18
+ export function isRuntimeRelayRevocation(closeCode, closeReason) {
19
+ return closeCode === runtimeRevocationCloseCode && closeReason === runtimeRevocationCloseReason;
20
+ }
21
+ //# sourceMappingURL=runtime-revocation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime-revocation.js","sourceRoot":"","sources":["../../src/relay-connection/runtime-revocation.ts"],"names":[],"mappings":"AAAA,MAAM,0BAA0B,GAAG,IAAI,CAAC;AACxC,MAAM,4BAA4B,GAAG,iBAAiB,CAAC;AAEvD;;;GAGG;AACH,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACtC,IAAI,GAAG,uBAAuB,CAAC;IAC/B,SAAS,CAAS;IAClB,WAAW,CAAS;IAE7B,YAAY,SAAiB,EAAE,WAAmB,EAAE,OAAO,GAAiB,EAAE;QAC1E,KAAK,CAAC,sCAAsC,EAAE,OAAO,CAAC,CAAC;QACvD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IAC3C,CAAC;CACJ;AAED,MAAM,UAAU,wBAAwB,CAAC,SAAiB,EAAE,WAAmB;IAC3E,OAAO,SAAS,KAAK,0BAA0B,IAAI,WAAW,KAAK,4BAA4B,CAAC;AACpG,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"run-daemon.d.ts","sourceRoot":"","sources":["../src/run-daemon.ts"],"names":[],"mappings":"AAgJA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAIzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAUhD,OAAO,EAA4B,KAAK,WAAW,EAAqB,MAAM,6BAA6B,CAAC;AA6/E5G,wBAAsB,SAAS,CAC3B,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,WAAW,EACnB,KAAK,GAAE,WAA+B,GACvC,OAAO,CAAC,IAAI,CAAC,CAoJf"}
1
+ {"version":3,"file":"run-daemon.d.ts","sourceRoot":"","sources":["../src/run-daemon.ts"],"names":[],"mappings":"AAgJA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAIzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAYhD,OAAO,EAA4B,KAAK,WAAW,EAAqB,MAAM,6BAA6B,CAAC;AA8gF5G,wBAAsB,SAAS,CAC3B,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,WAAW,EACnB,KAAK,GAAE,WAA+B,GACvC,OAAO,CAAC,IAAI,CAAC,CAuJf"}
@@ -19,14 +19,20 @@ import { AccumulatingOperationMetrics } from "./infra/operation-metrics.js";
19
19
  import { NativeSessionService } from "./native-sessions/service.js";
20
20
  import { environmentProxyDispatcher } from "./network/environment-proxy.js";
21
21
  import { PromptArtifactError, resolvePromptArtifacts } from "./prompt-artifact-resolver.js";
22
+ import { CoalescingTextPublisher } from "./relay-connection/coalescing-text-publisher.js";
22
23
  import { connectRelay } from "./relay-connection/connect.js";
23
24
  import { RelayConnectionDiagnostics } from "./relay-connection/diagnostics.js";
24
25
  import { RelayConnectionLiveness } from "./relay-connection/liveness.js";
25
26
  import { relayHttpEndpoint, runtimeAuthorization } from "./relay-connection/runtime-authentication.js";
27
+ import { isRuntimeRelayRevocation, RuntimeRelayRevokedError } from "./relay-connection/runtime-revocation.js";
26
28
  import { RuntimeDeliveryInboxCapacityError, SessionEventOutbox } from "./relay-connection/session-event-outbox.js";
27
29
  import { systemDaemonTimer } from "./relay-connection/timer.js";
28
30
  const RELAY_RECONNECT_DELAY_MILLISECONDS = 500;
29
31
  const AGENT_ACTIVITY_COALESCING_MILLISECONDS = 100;
32
+ const AGENT_TEXT_COALESCING_MILLISECONDS = 100;
33
+ // Mirrors the protocol's per-event text limit; a delta larger than this is
34
+ // rejected by the Session event codec rather than truncated.
35
+ const AGENT_TEXT_MAXIMUM_LENGTH = 100_000;
30
36
  const ACTIVE_TURN_RECONCILIATION_TIMEOUT_MILLISECONDS = 20_000;
31
37
  const ACTIVE_TURN_RECONCILIATION_CLOSE_CODE = 4009;
32
38
  class DaemonAcpObserver {
@@ -587,24 +593,34 @@ async function resolveSessionDirectories(directories, bindingId, directoryId, ad
587
593
  async function discoverAgentSettings(command, config, directories, sessions, signal) {
588
594
  const agent = config.catalog.find(command.agentId);
589
595
  const cwd = await directories.resolve(config.bindingId, "default", signal);
590
- const snapshot = await sessions.inspectAgentSettings({
596
+ const baseSnapshot = await sessions.inspectAgentSettings({
591
597
  additionalDirectories: [],
592
598
  agent,
593
599
  cwd,
594
600
  signal,
595
601
  });
602
+ const selectedSnapshot = command.model === undefined
603
+ ? baseSnapshot
604
+ : await sessions.inspectAgentSettings({
605
+ additionalDirectories: [],
606
+ agent,
607
+ cwd,
608
+ model: command.model,
609
+ signal,
610
+ });
596
611
  const adapter = agentSettingsAdapterForAgent(agent.id);
597
- const settings = settingsFromSnapshot(agent, snapshot.configOptions);
598
- const modelEffort = await discoverModelEffort(command, agent, adapter.modelEffortResolution, snapshot.configOptions, cwd, sessions, signal);
612
+ const baseSettings = settingsFromSnapshot(agent, baseSnapshot.configOptions);
613
+ const settings = settingsFromSnapshot(agent, selectedSnapshot.configOptions);
614
+ const modelEffort = await discoverModelEffort(command, agent, adapter.modelEffortResolution, baseSnapshot.configOptions, selectedSnapshot.configOptions, cwd, sessions, signal);
599
615
  return {
600
616
  agentId: command.agentId,
601
- agentName: snapshot.agentName,
602
- agentVersion: snapshot.agentVersion,
617
+ agentName: selectedSnapshot.agentName,
618
+ agentVersion: selectedSnapshot.agentVersion,
603
619
  kind: AGENT_SETTINGS_DISCOVERED_KIND,
604
620
  modelEffort,
605
621
  requestId: command.requestId,
606
622
  settings,
607
- settingsRevision: settingsRevision(agent, snapshot.agentVersion, settings, modelEffort),
623
+ settingsRevision: settingsRevision(agent, baseSnapshot.agentVersion, baseSettings, modelEffort),
608
624
  };
609
625
  }
610
626
  function settingFromOption(option) {
@@ -650,19 +666,13 @@ function unavailableModelEffort() {
650
666
  valuesByModel: {},
651
667
  };
652
668
  }
653
- async function discoverModelEffort(command, agent, resolution, initialOptions, cwd, sessions, signal) {
669
+ async function discoverModelEffort(command, agent, resolution, initialOptions, selectedOptions, cwd, sessions, signal) {
654
670
  const model = modelOption(initialOptions);
655
- const effort = effortOption(initialOptions);
656
671
  if (resolution !== AGENT_MODEL_EFFORT_RESOLUTION_COMPLETE &&
657
672
  resolution !== AGENT_MODEL_EFFORT_RESOLUTION_ON_MODEL_SELECTION) {
658
673
  return unavailableModelEffort();
659
674
  }
660
- if (model === undefined ||
661
- model.type !== "select" ||
662
- model.values.length === 0 ||
663
- effort === undefined ||
664
- effort.type !== "select" ||
665
- effort.values.length < 2) {
675
+ if (model === undefined || model.type !== "select" || model.values.length === 0) {
666
676
  return unavailableModelEffort();
667
677
  }
668
678
  const inspectModel = async (modelValue) => (await sessions.inspectAgentSettings({
@@ -674,6 +684,10 @@ async function discoverModelEffort(command, agent, resolution, initialOptions, c
674
684
  })).configOptions;
675
685
  const valuesByModel = {};
676
686
  if (resolution === "complete") {
687
+ const effort = effortOption(initialOptions);
688
+ if (effort === undefined || effort.type !== "select" || effort.values.length < 2) {
689
+ return unavailableModelEffort();
690
+ }
677
691
  for (const value of model.values) {
678
692
  const selectedEffort = effortOption(await inspectModel(value.value));
679
693
  valuesByModel[value.value] = selectedEffort?.type === "select" ? selectedEffort.values : [];
@@ -689,23 +703,29 @@ async function discoverModelEffort(command, agent, resolution, initialOptions, c
689
703
  if (!model.values.some((value) => value.value === command.model)) {
690
704
  throw new AcpExecutionError("The selected Agent model is unavailable", {}, "invalid_session_configuration");
691
705
  }
692
- const selectedEffort = effortOption(await inspectModel(command.model));
706
+ const selectedEffort = effortOption(selectedOptions);
693
707
  valuesByModel[command.model] = selectedEffort?.type === "select" ? selectedEffort.values : [];
694
708
  }
709
+ const selectedEffort = effortOption(selectedOptions);
695
710
  return {
696
- effortSettingId: effort.id,
711
+ effortSettingId: selectedEffort?.id ?? "",
697
712
  modelSettingId: model.id,
698
713
  resolution: "on_model_selection",
699
714
  valuesByModel,
700
715
  };
701
716
  }
702
717
  function settingsRevision(agent, reportedAgentVersion, settings, modelEffort) {
703
- // A dynamic effort lookup adds one model's values to an otherwise stable
704
- // Agent capability document. It must not look like the Agent's settings
705
- // changed each time a caller switches models.
718
+ // A dynamic effort lookup adds one model's settings and values to an
719
+ // otherwise stable Agent capability document. It must not look like the
720
+ // Agent's settings changed each time a caller switches models.
706
721
  const revisionModelEffort = modelEffort.resolution === AGENT_MODEL_EFFORT_RESOLUTION_COMPLETE
707
722
  ? modelEffort
708
- : { ...modelEffort, valuesByModel: {} };
723
+ : {
724
+ effortSettingId: "",
725
+ modelSettingId: modelEffort.modelSettingId,
726
+ resolution: modelEffort.resolution,
727
+ valuesByModel: {},
728
+ };
709
729
  return createHash("sha256")
710
730
  .update(JSON.stringify({
711
731
  adapterVersion: agent.adapterVersion,
@@ -862,6 +882,15 @@ async function executePrompt(outbox, publisher, command, config, directories, au
862
882
  const activities = new CoalescingDaemonActivityPublisher(timer, (activity) => {
863
883
  publisher.recordAttempt(command.sessionId, encodeDaemonActivity(command, activity), attemptId);
864
884
  });
885
+ const text = new CoalescingTextPublisher(timer, (messageId, delta) => {
886
+ publisher.recordAttempt(command.sessionId, encodeAgentMessageDeltaEvent({
887
+ commandId: command.commandId,
888
+ kind: AGENT_MESSAGE_DELTA_KIND,
889
+ messageId,
890
+ sessionId: command.sessionId,
891
+ text: delta,
892
+ }), attemptId);
893
+ }, AGENT_TEXT_COALESCING_MILLISECONDS, AGENT_TEXT_MAXIMUM_LENGTH);
865
894
  try {
866
895
  const resolved = await resolveSessionDirectories(directories, config.bindingId, command.directoryId, command.additionalDirectoryIds, signal);
867
896
  const agent = config.catalog.find(command.agentId);
@@ -928,14 +957,8 @@ async function executePrompt(outbox, publisher, command, config, directories, au
928
957
  disposition: command.disposition,
929
958
  images: attachments.images,
930
959
  onActivity: activities.record,
931
- onText: (text, messageId) => {
932
- publisher.recordAttempt(command.sessionId, encodeAgentMessageDeltaEvent({
933
- commandId: command.commandId,
934
- kind: AGENT_MESSAGE_DELTA_KIND,
935
- messageId,
936
- sessionId: command.sessionId,
937
- text,
938
- }), attemptId);
960
+ onText: (delta, messageId) => {
961
+ text.record(messageId, delta);
939
962
  return Promise.resolve();
940
963
  },
941
964
  prompt: command.prompt,
@@ -946,6 +969,7 @@ async function executePrompt(outbox, publisher, command, config, directories, au
946
969
  });
947
970
  stopReason = result.stopReason;
948
971
  }
972
+ text.flush();
949
973
  activities.flush();
950
974
  publisher.recordAttempt(command.sessionId, encodeSessionCompletedEvent({
951
975
  commandId: command.commandId,
@@ -961,6 +985,7 @@ async function executePrompt(outbox, publisher, command, config, directories, au
961
985
  });
962
986
  }
963
987
  catch (error) {
988
+ text.flush();
964
989
  activities.flush();
965
990
  audit.record({
966
991
  commandId: command.commandId,
@@ -1024,17 +1049,6 @@ function assertSessionConfigurationSchema(options, selections) {
1024
1049
  }
1025
1050
  }
1026
1051
  }
1027
- function assertSessionConfigurationCoverage(options, selections) {
1028
- const selectionsById = new Map(selections.map((selection) => [selection.configId, selection]));
1029
- if (selectionsById.size !== selections.length) {
1030
- throw invalidSessionConfiguration("The Turn configuration contains duplicate Agent settings");
1031
- }
1032
- for (const option of options) {
1033
- if (!selectionsById.has(option.id)) {
1034
- throw invalidSessionConfiguration(`The Turn configuration does not match Agent setting ${option.id}`);
1035
- }
1036
- }
1037
- }
1038
1052
  function sessionConfigurationSelectionIsAvailable(option, selection) {
1039
1053
  if (option.type !== selection.valueType) {
1040
1054
  return false;
@@ -1130,7 +1144,6 @@ async function applySessionConfigurationSelection(sessions, audit, options, sele
1130
1144
  });
1131
1145
  }
1132
1146
  async function applyCompleteSessionConfiguration(sessions, audit, initialOptions, selections, agent, cwd, additionalDirectories, commandId, sessionId, signal) {
1133
- assertSessionConfigurationCoverage(initialOptions, selections);
1134
1147
  let options = initialOptions;
1135
1148
  const orderedSelections = selections.toSorted((left, right) => {
1136
1149
  const priority = sessionConfigurationPriority(left, initialOptions) - sessionConfigurationPriority(right, initialOptions);
@@ -1444,6 +1457,10 @@ async function runRelayConnection(config, directories, audit, sessions, installa
1444
1457
  signal.addEventListener("abort", stop, { once: true });
1445
1458
  const handleClose = (event) => {
1446
1459
  diagnostics.connectionClosed(connectionEpoch, event.code, event.reason, "relay");
1460
+ if (isRuntimeRelayRevocation(event.code, event.reason)) {
1461
+ rejectConnection(new RuntimeRelayRevokedError(event.code, event.reason));
1462
+ return;
1463
+ }
1447
1464
  resolveConnection();
1448
1465
  };
1449
1466
  const handleError = (event) => {
@@ -1960,6 +1977,9 @@ export async function runDaemon(config, audit, signal, timer = systemDaemonTimer
1960
1977
  if (signal.aborted) {
1961
1978
  return;
1962
1979
  }
1980
+ if (error instanceof RuntimeRelayRevokedError) {
1981
+ throw error;
1982
+ }
1963
1983
  diagnostics.connectionFailed(connectionEpoch, error, "connection");
1964
1984
  }
1965
1985
  await waitForRelayRetry(signal, timer);