@nexum-hub/agent 0.0.3-next.11 → 0.0.3-next.12

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.
@@ -109,7 +109,6 @@ export class AgentRuntime {
109
109
  #slots = new TransferStreamSlots();
110
110
  #lifetime = new AbortController();
111
111
  #stopping = new AbortController();
112
- #leases = new Set();
113
112
  #shutdownJoins = new Set();
114
113
  #invocation = new AsyncLocalStorage();
115
114
  #runs = new Map();
@@ -123,7 +122,6 @@ export class AgentRuntime {
123
122
  #logErrorCode = null;
124
123
  #logWriterClosed = false;
125
124
  #catalog;
126
- #refreshTimer;
127
125
  #closing = false;
128
126
  #closed = false;
129
127
  #closeTask;
@@ -223,7 +221,6 @@ export class AgentRuntime {
223
221
  data: () => this.#data(),
224
222
  context: (caller, projectId) => this.#context(caller, projectId),
225
223
  scope: (caller, signal, runId) => this.#scope(caller, signal, runId),
226
- startLease: (caller, source, signal) => this.#startLease(caller, source, signal),
227
224
  onShutdown: (join) => {
228
225
  this.#shutdownJoins.add(join);
229
226
  return () => {
@@ -480,14 +477,6 @@ export class AgentRuntime {
480
477
  for (const name of this.#enablement.enabled())
481
478
  await this.#extensions.enable(name, this.#enablement.get(name)?.config ?? {});
482
479
  await this.#documents.refresh(this.#published().docs);
483
- this.#refreshTimer = setInterval(() => {
484
- for (const entry of this.#runs.values())
485
- if (!terminalStatuses.has(this.#store.getRun(entry.services.runId).status))
486
- void this.#refresh(entry).catch(() => { });
487
- for (const entry of this.#leases)
488
- void this.#refreshLease(entry).catch(() => { });
489
- }, 10000);
490
- this.#refreshTimer.unref();
491
480
  }
492
481
  #published() {
493
482
  const extra = this.#materials.published();
@@ -672,104 +661,6 @@ export class AgentRuntime {
672
661
  },
673
662
  };
674
663
  }
675
- #startLease(caller, source, signal, projectId = this.#projectFor(caller)) {
676
- validateContract("wire.input:authorization.refresh", source);
677
- const controller = new AbortController();
678
- const scope = this.#scope(caller, AbortSignal.any([signal, controller.signal]), source.runId, projectId);
679
- const entry = {
680
- caller: copy(caller),
681
- projectId,
682
- source: copy(source),
683
- signal: scope.signal,
684
- controller,
685
- active: true,
686
- close: () => {
687
- if (!entry.active)
688
- return;
689
- entry.active = false;
690
- clearTimeout(entry.timer);
691
- entry.signal.removeEventListener("abort", entry.close);
692
- this.#leases.delete(entry);
693
- },
694
- };
695
- entry.signal.addEventListener("abort", entry.close, { once: true });
696
- this.#leases.add(entry);
697
- if (entry.signal.aborted)
698
- entry.close();
699
- else
700
- this.#scheduleLease(entry);
701
- return {
702
- scope,
703
- refresh: () => this.#refreshLease(entry),
704
- close: entry.close,
705
- };
706
- }
707
- #scheduleLease(entry) {
708
- clearTimeout(entry.timer);
709
- if (!entry.active)
710
- return;
711
- try {
712
- const policy = this.#authority.current(entry.caller, entry.projectId);
713
- if (entry.source.runId)
714
- this.#authority.authorize(entry.caller, {
715
- type: "path",
716
- value: this.#store.getWork(this.#store.getRun(entry.source.runId).contextId).pwd,
717
- access: "read",
718
- }, entry.projectId);
719
- const remaining = Date.parse(policy.expiresAt) - Date.now();
720
- if (remaining <= 0)
721
- throw new AgentError("authorization_expired", "The current authorization lease has expired.");
722
- entry.timer = setTimeout(() => this.#scheduleLease(entry), Math.min(remaining + 1, 2_147_483_647));
723
- entry.timer.unref();
724
- }
725
- catch (cause) {
726
- entry.controller.abort(cause);
727
- }
728
- }
729
- #revokeLeases(caller) {
730
- const cause = new AgentError("authorization_revoked", "The authenticated Cloud revoked this caller's authorization.");
731
- for (const entry of this.#leases)
732
- if (callerKey(entry.caller) === callerKey(caller) &&
733
- entry.caller.grantId === caller.grantId)
734
- entry.controller.abort(cause);
735
- }
736
- async #refreshLease(entry) {
737
- if (!entry.active)
738
- return;
739
- if (entry.refreshing)
740
- return entry.refreshing;
741
- entry.refreshing = (async () => {
742
- const observed = this.#authority.capture(entry.caller, entry.projectId);
743
- if (Date.parse(observed.context.authorization.expiresAt) - Date.now() >
744
- 15000)
745
- return;
746
- const fresh = await this.#control().request("authorization.refresh", observed.context, entry.source, entry.signal, 10000);
747
- if (entry.active) {
748
- this.#authority.refresh(observed, fresh.authorization);
749
- this.#scheduleLease(entry);
750
- }
751
- })();
752
- try {
753
- await entry.refreshing;
754
- }
755
- catch (cause) {
756
- if (isAgentError(cause) &&
757
- [
758
- "forbidden",
759
- "unauthorized",
760
- "grant_revoked",
761
- "grant_expired",
762
- "authorization_denied",
763
- "authorization_revoked",
764
- "authorization_expired",
765
- ].includes(cause.code))
766
- entry.controller.abort(cause);
767
- throw cause;
768
- }
769
- finally {
770
- entry.refreshing = undefined;
771
- }
772
- }
773
664
  #authorize(caller, target) {
774
665
  if (target.run &&
775
666
  target.action === "get" &&
@@ -862,19 +753,7 @@ export class AgentRuntime {
862
753
  const context = copy(origin.context);
863
754
  requireCondition((context.authorization.projectId ?? null) ===
864
755
  this.#store.getRun(services.runId).projectId, "authorization_scope_mismatch", "The accepted run origin must retain its actual work Project scope.");
865
- const lease = this.#startLease(context.caller, { runId: services.runId, originInvocationId: origin.id }, services.signal, context.authorization.projectId ?? null);
866
- const stop = () => {
867
- if (!this.#closing && !services.signal.aborted) {
868
- const cause = lease.scope.signal.reason;
869
- this.#engine.stopAcceptedRun(services.runId, {
870
- code: isAgentError(cause) ? cause.code : "authorization_revoked",
871
- });
872
- }
873
- };
874
- if (lease.scope.signal.aborted)
875
- stop();
876
- else
877
- lease.scope.signal.addEventListener("abort", stop, { once: true });
756
+ const runScope = this.#scope(context.caller, services.signal, services.runId, context.authorization.projectId ?? null);
878
757
  const announce = this.#connection.send({
879
758
  type: "event",
880
759
  name: "agent.run.changed",
@@ -894,7 +773,7 @@ export class AgentRuntime {
894
773
  context,
895
774
  services,
896
775
  announced: announce,
897
- lease,
776
+ scope: runScope,
898
777
  notify: () => {
899
778
  if (this.#closed)
900
779
  return;
@@ -914,7 +793,6 @@ export class AgentRuntime {
914
793
  })
915
794
  .catch(() => { });
916
795
  if (terminalStatuses.has(row.status)) {
917
- lease.close();
918
796
  this.#engine.off(services.runId, entry.notify);
919
797
  this.#runs.delete(services.runId);
920
798
  }
@@ -924,9 +802,9 @@ export class AgentRuntime {
924
802
  this.#engine.on(services.runId, entry.notify);
925
803
  return entry;
926
804
  }
927
- async #refresh(entry) {
805
+ async #checkAccepted(entry) {
928
806
  await entry.announced;
929
- await entry.lease.refresh();
807
+ entry.scope.assertCurrent();
930
808
  }
931
809
  #track(task) {
932
810
  this.#streams.add(task);
@@ -972,15 +850,11 @@ export class AgentRuntime {
972
850
  sha256: createHash("sha256").update(bytes).digest("hex"),
973
851
  }, signal);
974
852
  prepared = { control, stream: output.stream };
975
- const lease = this.#startLease(context.caller, {
976
- streamId: output.stream.streamId,
977
- originInvocationId: originId ?? invoke.id,
978
- }, signal, context.authorization.projectId ?? null);
853
+ const streamScope = this.#scope(context.caller, signal, runId, context.authorization.projectId ?? null);
979
854
  const task = this.#data(control)
980
- .sendFixedBody(output.stream, bytes, lease.scope.signal)
855
+ .sendFixedBody(output.stream, bytes, streamScope.signal)
981
856
  .catch((cause) => this.#failedStream(control, context, output.stream, cause))
982
857
  .finally(() => {
983
- lease.close();
984
858
  permit.release();
985
859
  });
986
860
  this.#track(task);
@@ -1003,7 +877,7 @@ export class AgentRuntime {
1003
877
  const request = async (action, params = {}, options) => {
1004
878
  requireCondition(capabilities[action], "host_capability_unavailable", "The original Host session did not provide this capability.");
1005
879
  await accepted.announced;
1006
- await this.#refresh(accepted);
880
+ await this.#checkAccepted(accepted);
1007
881
  binding.assertCurrent();
1008
882
  const signal = options?.signal
1009
883
  ? AbortSignal.any([binding.signal, options.signal])
@@ -1052,7 +926,7 @@ export class AgentRuntime {
1052
926
  async #attachment(reference, services) {
1053
927
  const accepted = this.#accepted(services);
1054
928
  await accepted.announced;
1055
- await this.#refresh(accepted);
929
+ await this.#checkAccepted(accepted);
1056
930
  if (typeof reference.sourceId === "string") {
1057
931
  const control = this.#control();
1058
932
  const controllerCaller = this.#invocation.getStore()?.context.caller ?? accepted.context.caller;
@@ -1115,7 +989,7 @@ export class AgentRuntime {
1115
989
  const accepted = this.#runs.get(runId);
1116
990
  requireCondition(accepted, "run_origin_unavailable", "The transfer needs its accepted run origin.");
1117
991
  await accepted.announced;
1118
- await this.#refresh(accepted);
992
+ await this.#checkAccepted(accepted);
1119
993
  const context = this.#context(binding.caller, accepted.context.authorization.projectId ?? null);
1120
994
  this.#channels.set(binding.transferId, {
1121
995
  context,
@@ -1153,22 +1027,6 @@ export class AgentRuntime {
1153
1027
  validateConnectionV2(message.connection, this.#connection.binding);
1154
1028
  if (message.name === "agent.catalog.sync")
1155
1029
  await this.#catalog.sync(message.data, signal);
1156
- else if (message.name === "agent.authorization.changed") {
1157
- if (message.data.authorization === null) {
1158
- this.#authority.revoke(message.data.caller);
1159
- this.#revokeLeases(message.data.caller);
1160
- }
1161
- else {
1162
- this.#authority.observe({
1163
- caller: message.data.caller,
1164
- authorization: message.data.authorization,
1165
- connection: message.connection,
1166
- });
1167
- for (const lease of this.#leases)
1168
- if (callerKey(lease.caller) === callerKey(message.data.caller))
1169
- this.#scheduleLease(lease);
1170
- }
1171
- }
1172
1030
  else
1173
1031
  throw new AgentError("invalid_control_direction", "This event is not an Agent application entry.");
1174
1032
  }
@@ -1188,9 +1046,6 @@ export class AgentRuntime {
1188
1046
  "agent.process.stop",
1189
1047
  ].includes(message.operation), "invalid_control_direction", "An owner Console session has only its declared Project and managed-process operations.");
1190
1048
  this.#authority.observe(message.context);
1191
- for (const lease of this.#leases)
1192
- if (callerKey(lease.caller) === callerKey(message.context.caller))
1193
- this.#scheduleLease(lease);
1194
1049
  return this.#invocation.run(copy(message), async () => {
1195
1050
  if (message.context.caller.source !== "owner_session")
1196
1051
  await this.#restoreConfigurations(message.context.caller);
@@ -1542,15 +1397,15 @@ export class AgentRuntime {
1542
1397
  sha256: metadata.sha256,
1543
1398
  }, signal);
1544
1399
  prepared = { context, stream };
1545
- const lease = this.#startLease(caller, { streamId: stream.streamId, originInvocationId: invoke.id }, this.#lifetime.signal);
1400
+ const streamScope = this.#scope(caller, this.#lifetime.signal);
1546
1401
  const task = (async () => {
1547
1402
  let source;
1548
1403
  try {
1549
1404
  source = await this.#artifacts.open(input.uri, {
1550
1405
  ...scope,
1551
- signal: lease.scope.signal,
1406
+ signal: streamScope.signal,
1552
1407
  });
1553
- await this.#data(control).sendFixedBody(stream, source.stream, lease.scope.signal);
1408
+ await this.#data(control).sendFixedBody(stream, source.stream, streamScope.signal);
1554
1409
  await source.closed;
1555
1410
  }
1556
1411
  finally {
@@ -1560,7 +1415,6 @@ export class AgentRuntime {
1560
1415
  })()
1561
1416
  .catch((cause) => this.#failedStream(control, context, stream, cause))
1562
1417
  .finally(() => {
1563
- lease.close();
1564
1418
  permit.release();
1565
1419
  });
1566
1420
  this.#track(task);
@@ -1627,7 +1481,6 @@ export class AgentRuntime {
1627
1481
  };
1628
1482
  const controller = new AbortController();
1629
1483
  const signal = AbortSignal.any([this.#lifetime.signal, controller.signal]);
1630
- let lease;
1631
1484
  let transportClosed;
1632
1485
  let enteredCore = false;
1633
1486
  const knownClosure = (closed) => {
@@ -1642,12 +1495,9 @@ export class AgentRuntime {
1642
1495
  const control = this.#control(), data = this.#data(control);
1643
1496
  const source = this.#channels.get(input.transferId);
1644
1497
  requireCondition(source, "transfer_origin_unavailable", "The transfer has no original approved channel source.");
1645
- lease = this.#startLease(caller, {
1646
- transferId: input.transferId,
1647
- originInvocationId: source.originInvocationId,
1648
- }, signal, reservation.context.authorization.projectId ?? null);
1498
+ const transferScope = this.#scope(caller, signal, undefined, reservation.context.authorization.projectId ?? null);
1649
1499
  if (input.stream.direction === "agent-to-cloud") {
1650
- const stream = data.openWrite(input.stream, lease.scope.signal, claim);
1500
+ const stream = data.openWrite(input.stream, transferScope.signal, claim);
1651
1501
  enteredCore = true;
1652
1502
  return await this.#transfers.commitClaim(reservation.core, {
1653
1503
  download: stream.writable,
@@ -1656,7 +1506,7 @@ export class AgentRuntime {
1656
1506
  }
1657
1507
  // The existing reservation is consumed before waiting for response headers
1658
1508
  // or the Host's first upload byte. A native Readable supplies the deferred I/O.
1659
- const opening = data.openRead(input.stream, lease.scope.signal, claim);
1509
+ const opening = data.openRead(input.stream, transferScope.signal, claim);
1660
1510
  void opening.catch(() => { });
1661
1511
  let actual;
1662
1512
  const readable = Readable.from((async function* () {
@@ -1694,7 +1544,6 @@ export class AgentRuntime {
1694
1544
  throw cause;
1695
1545
  }
1696
1546
  finally {
1697
- lease?.close();
1698
1547
  this.#reservations.delete(input.reservationId);
1699
1548
  }
1700
1549
  }
@@ -1705,7 +1554,6 @@ export class AgentRuntime {
1705
1554
  this.#closing = true;
1706
1555
  this.#stopping.abort(new AgentError("runtime_closed", "The Agent process is stopping."));
1707
1556
  await Promise.all([...this.#shutdownJoins].map((join) => join()));
1708
- clearInterval(this.#refreshTimer);
1709
1557
  await this.#catalog.close();
1710
1558
  if (this.#engine)
1711
1559
  for (const entry of this.#runs.values())