@ziggs-ai/agent-sdk 0.48.21 → 0.48.22

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.48.22
6
+
7
+ - An agent's own `wake_at` that is refused because the work's audience or authority changed since it was set now runs once under a freshly prepared execution context. Before, every retry reused the ended context and was refused again.
8
+
5
9
  ## 0.48.21
6
10
 
7
11
  ## 0.48.20
package/dist/Agent.d.ts CHANGED
@@ -273,6 +273,8 @@ export declare class Agent {
273
273
  private wakeLedger;
274
274
  /** Retries so far for an own wake whose turn failed, by wake id. */
275
275
  private readonly wakeRetries;
276
+ /** Replaced in tests. */
277
+ private prepareExecution;
276
278
  /** The per-agent inbox long-poll loop — this agent's sole wake source. */
277
279
  private inboxLoop;
278
280
  private inboxLoopEnabled;
@@ -302,6 +304,21 @@ export declare class Agent {
302
304
  constructor(options?: AgentOptions);
303
305
  private _validate;
304
306
  private _setup;
307
+ /**
308
+ * Run one timer event on its lane. An agent's own wake that is refused
309
+ * because the work's audience or authority changed since it was set is run
310
+ * once more under a context prepared now: the backend checks the agent's
311
+ * authority again against the live audience, the way it does for a
312
+ * redelivered inbox item. Retrying under the saved context can only be
313
+ * refused again.
314
+ */
315
+ private runTimerEvent;
316
+ /**
317
+ * The lane's action, prepared again, when `err` says its saved context was
318
+ * ended by a change of audience or authority. Undefined for any other
319
+ * failure, for a lane with no exact action, and when the backend refuses.
320
+ */
321
+ private freshContextAfterChange;
305
322
  private executionControlOn;
306
323
  /**
307
324
  * The door every work tool passes: the agent names a task it holds, the
package/dist/Agent.js CHANGED
@@ -1,4 +1,4 @@
1
- import { runWorkExecution } from '@ziggs-ai/api-client';
1
+ import { prepareWorkExecution, runWorkExecution } from '@ziggs-ai/api-client';
2
2
  import { BucketsClient, MessageRecordsClient } from '@ziggs-ai/api-client/http';
3
3
  import { responseCommand } from './runtime/messageCommand.js';
4
4
  import { validateWorkExecution, withWorkExecution, currentWorkExecution } from '@ziggs-ai/api-client';
@@ -231,6 +231,8 @@ export class Agent {
231
231
  wakeLedger;
232
232
  /** Retries so far for an own wake whose turn failed, by wake id. */
233
233
  wakeRetries = new Map();
234
+ /** Replaced in tests. */
235
+ prepareExecution = prepareWorkExecution;
234
236
  /** The per-agent inbox long-poll loop — this agent's sole wake source. */
235
237
  inboxLoop = null;
236
238
  inboxLoopEnabled;
@@ -464,19 +466,67 @@ export class Agent {
464
466
  // exact wake; a crash/failure leaves it durable, and an older completion
465
467
  // cannot consume a timer armed while that work was running.
466
468
  this.stateTimers = new StateTimers((ev, laneKey) => {
467
- const work = this.eventQueue.enqueue(ev, laneKey).then(() => {
468
- if (ev.type === 'wake-at') {
469
- this.wakeRetries.delete(ev.wakeId);
470
- this.wakeLedger.done(laneKey, ev.wakeId);
471
- }
472
- }).catch((err) => {
473
- runtimeLog.warn('Agent', `state-timer frame failed laneKey=${laneKey}: ${err?.message || err}`);
474
- if (ev.type === 'wake-at')
475
- this.retryOwnWake(ev, laneKey);
476
- });
477
- this.wakeLedger.track(work);
469
+ this.wakeLedger.track(this.runTimerEvent(ev, laneKey));
478
470
  });
479
471
  }
472
+ /**
473
+ * Run one timer event on its lane. An agent's own wake that is refused
474
+ * because the work's audience or authority changed since it was set is run
475
+ * once more under a context prepared now: the backend checks the agent's
476
+ * authority again against the live audience, the way it does for a
477
+ * redelivered inbox item. Retrying under the saved context can only be
478
+ * refused again.
479
+ */
480
+ async runTimerEvent(ev, laneKey) {
481
+ const settle = () => {
482
+ if (ev.type !== 'wake-at')
483
+ return;
484
+ this.wakeRetries.delete(ev.wakeId);
485
+ this.wakeLedger.done(laneKey, ev.wakeId);
486
+ };
487
+ try {
488
+ await this.eventQueue.enqueue(ev, laneKey);
489
+ settle();
490
+ return;
491
+ }
492
+ catch (err) {
493
+ runtimeLog.warn('Agent', `state-timer frame failed laneKey=${laneKey}: ${err?.message || err}`);
494
+ if (ev.type !== 'wake-at')
495
+ return;
496
+ const fresh = await this.freshContextAfterChange(err, laneKey);
497
+ if (fresh) {
498
+ try {
499
+ await this.eventQueue.enqueue(ev, laneKey, fresh);
500
+ runtimeLog.info('Agent', `wake-at laneKey=${laneKey} ran under a fresh execution context`);
501
+ settle();
502
+ return;
503
+ }
504
+ catch (again) {
505
+ runtimeLog.warn('Agent', `wake-at laneKey=${laneKey} failed under a fresh execution context: ${again?.message || again}`);
506
+ }
507
+ }
508
+ this.retryOwnWake(ev, laneKey);
509
+ }
510
+ }
511
+ /**
512
+ * The lane's action, prepared again, when `err` says its saved context was
513
+ * ended by a change of audience or authority. Undefined for any other
514
+ * failure, for a lane with no exact action, and when the backend refuses.
515
+ */
516
+ async freshContextAfterChange(err, laneKey) {
517
+ if (err?.code !== 'EXECUTION_CONTEXT_CHANGED')
518
+ return undefined;
519
+ const action = this.sessions.get(laneKey)?.executionContext?.action;
520
+ if (!action || !this.options.operatorKey || !this.options.agentId)
521
+ return undefined;
522
+ try {
523
+ return await this.prepareExecution(action, this.options.operatorKey, this.options.agentId, resolveBackendUrl());
524
+ }
525
+ catch (refused) {
526
+ runtimeLog.warn('Agent', `wake-at laneKey=${laneKey} could not prepare a fresh execution context: ${refused?.message || refused}`);
527
+ return undefined;
528
+ }
529
+ }
480
530
  executionControlOn() {
481
531
  return this.options.executionControl ?? isClaudeCognition(this.options.cognition);
482
532
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ziggs-ai/agent-sdk",
3
- "version": "0.48.21",
3
+ "version": "0.48.22",
4
4
  "description": "Agent framework SDK for building autonomous agents on the Ziggs platform",
5
5
  "homepage": "https://ziggsai.com/docs",
6
6
  "bugs": {