@ziggs-ai/agent-sdk 0.45.1 → 0.46.0
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 +14 -0
- package/dist/Agent.d.ts +19 -6
- package/dist/Agent.js +53 -29
- package/dist/brain/ClaudeBrain.d.ts +0 -7
- package/dist/brain/ClaudeBrain.js +0 -19
- package/dist/platform/inboxLoop.d.ts +20 -6
- package/dist/platform/inboxLoop.js +39 -16
- package/dist/skills/createSkillAgent.js +0 -1
- package/package.json +2 -2
- package/dist/brain/hireActivationNotice.d.ts +0 -19
- package/dist/brain/hireActivationNotice.js +0 -80
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.46.0
|
|
6
|
+
|
|
7
|
+
- An agent wakes once when its own proposal is approved or declined, and carries on under that agreement. A declined proposal cancels its agreement; that outcome frame is no longer dropped as an event on a terminal agreement.
|
|
8
|
+
- Removed the hire-activation skip. `actsOnHireActivation` (Agent and ClaudeBrain option) is gone, and so are `isHireActivationNotice`, `shouldSkipAgentFormedHireActivation` and the `agent_formed_hire_activation` / `task_only_activation` no-op reasons. A hired provider no longer gets an activation wake from the server, so there is nothing to skip; it waits for assigned work.
|
|
9
|
+
- While wakes run, the inbox loop passes the last envelope's `bellAt` back as `seen`, so the server holds the read until new mail arrives instead of the loop re-reading every few seconds. A message from someone else starts at once, and a busy agent no longer runs into the inbox polling limit.
|
|
10
|
+
|
|
11
|
+
## 0.45.3
|
|
12
|
+
|
|
13
|
+
- `sharedInbox` (or `ZIGGS_AGENT_SHARED_INBOX=1`) runs the agent as one of several host processes. Each read tells the server how many more threads this process can start, so work spreads across processes and one thread stays on one process at a time. Only the process holding the inbox lease runs the agent's declared polls and restores its saved wake-ups, and the polls move when leadership does. Every wake reloads the thread's session from `sessionStore`, since the thread may have last run elsewhere. Requires api-client 0.40.3.
|
|
14
|
+
|
|
15
|
+
## 0.45.2
|
|
16
|
+
|
|
17
|
+
- A message record without `rootMessageId` is refused: the wake fails and the message stays pending, instead of running on a lane of its own. Every message the backend writes names its thread.
|
|
18
|
+
|
|
5
19
|
## 0.45.1
|
|
6
20
|
|
|
7
21
|
- `task_set_result` says a completion with no stated charge is free on an agreement priced at completion. A charge written as text ("300", "300 points") is read as a number; a negative one is dropped. Requires api-client 0.40.2.
|
package/dist/Agent.d.ts
CHANGED
|
@@ -111,12 +111,6 @@ export interface AgentOptions {
|
|
|
111
111
|
cognition?: CognitionKind;
|
|
112
112
|
/** Code-enforced work agreement/task binding. Defaults on for the Claude brain; other brains can opt in. */
|
|
113
113
|
executionControl?: boolean;
|
|
114
|
-
/**
|
|
115
|
-
* Spend a model turn on an activation notice for a hire an agent formed.
|
|
116
|
-
* Default off — the host records a no-op and waits for the assigned task.
|
|
117
|
-
* A hire a person formed (store claim) still wakes regardless of this flag.
|
|
118
|
-
*/
|
|
119
|
-
actsOnHireActivation?: boolean;
|
|
120
114
|
/**
|
|
121
115
|
* Before a Claude turn on chat messages, ask the backend whether
|
|
122
116
|
* they ask this agent for anything; a confident "no" skips the turn. On by
|
|
@@ -202,6 +196,15 @@ export interface AgentOptions {
|
|
|
202
196
|
* to `ZIGGS_AGENT_INBOX_CONCURRENCY`, else 4.
|
|
203
197
|
*/
|
|
204
198
|
inboxConcurrency?: number;
|
|
199
|
+
/**
|
|
200
|
+
* Run as one of several processes hosting this agent at once. Each process
|
|
201
|
+
* takes its own threads (one thread stays on one process at a time); the
|
|
202
|
+
* one holding the inbox lease leads and alone runs the agent's declared
|
|
203
|
+
* polls and restores its saved wake-ups. Every wake reloads the thread's
|
|
204
|
+
* session from `sessionStore`, since the thread may have last run on
|
|
205
|
+
* another process. Defaults to `ZIGGS_AGENT_SHARED_INBOX=1`.
|
|
206
|
+
*/
|
|
207
|
+
sharedInbox?: boolean;
|
|
205
208
|
}
|
|
206
209
|
/**
|
|
207
210
|
* One wake, as the brain received it.
|
|
@@ -467,6 +470,16 @@ export declare class Agent {
|
|
|
467
470
|
private _armIfTimedParked;
|
|
468
471
|
private _resolveOwnAgentId;
|
|
469
472
|
connectAsync(_timeout?: number): Promise<this>;
|
|
473
|
+
/** Whether this process runs as one of several hosts of the agent. */
|
|
474
|
+
private get sharedInbox();
|
|
475
|
+
private leading;
|
|
476
|
+
private wakesRestored;
|
|
477
|
+
/**
|
|
478
|
+
* Shared hosts: start the agent's polls (and restore its saved wake-ups,
|
|
479
|
+
* once) when this process becomes the leader, and stop the polls when it
|
|
480
|
+
* stops leading, so exactly one process runs them.
|
|
481
|
+
*/
|
|
482
|
+
private setLeadership;
|
|
470
483
|
/**
|
|
471
484
|
* Arm the lane's timer for the agent's own `wake_at`, and save it so a
|
|
472
485
|
* restart arms it again.
|
package/dist/Agent.js
CHANGED
|
@@ -43,13 +43,12 @@ import { resolveBackendUrl } from './shared/backendUrl.js';
|
|
|
43
43
|
import { laneUnderAgreement } from '@ziggs-ai/api-client';
|
|
44
44
|
import { TaskService } from './platform/tasks/TaskService.js';
|
|
45
45
|
import { AgreementService } from './platform/agreements/AgreementService.js';
|
|
46
|
-
import { INBOX_STOP_TIMEOUT_MS, runInboxLoop } from './platform/inboxLoop.js';
|
|
46
|
+
import { INBOX_STOP_TIMEOUT_MS, runInboxLoop, sharedInboxHost } from './platform/inboxLoop.js';
|
|
47
47
|
import { requestExactMatch } from './platform/requestMatch.js';
|
|
48
48
|
import { SeenMessages } from './platform/SeenMessages.js';
|
|
49
49
|
import { runtimeLog } from './shared/runtimeLog.js';
|
|
50
50
|
import { isTelemetryIngestEnabled, sendTelemetry, } from './platform/telemetryIngest.js';
|
|
51
51
|
import { buildWakeOutcomePayload, classifyWakeOutcome, wakeWasAsked, } from './runtime/wakeOutcome.js';
|
|
52
|
-
import { agreementIdFromWake, formedByFromWake, isHireActivationNotice, shouldSkipAgentFormedHireActivation, } from './brain/hireActivationNotice.js';
|
|
53
52
|
// ConnectionPool lives in a separate module; resolved via a computed-path
|
|
54
53
|
// dynamic import to avoid a circular import with Agent.
|
|
55
54
|
async function loadConnectionPool() {
|
|
@@ -284,13 +283,13 @@ export class Agent {
|
|
|
284
283
|
wakeTap: resolved.wakeTap,
|
|
285
284
|
inboxCatchUp: resolved.inboxCatchUp,
|
|
286
285
|
inboxConcurrency: resolved.inboxConcurrency,
|
|
286
|
+
sharedInbox: resolved.sharedInbox,
|
|
287
287
|
telemetryIngest: resolved.telemetryIngest,
|
|
288
288
|
brain: resolved.brain,
|
|
289
289
|
polls: resolved.polls,
|
|
290
290
|
pollKinds: resolved.pollKinds,
|
|
291
291
|
cognition: resolved.cognition ?? 'fsm',
|
|
292
292
|
executionControl: resolved.executionControl,
|
|
293
|
-
actsOnHireActivation: resolved.actsOnHireActivation === true,
|
|
294
293
|
wakeGate: resolved.wakeGate,
|
|
295
294
|
claudeMaxTurns: resolved.claudeMaxTurns,
|
|
296
295
|
claudeStatelessPerWake: resolved.claudeStatelessPerWake,
|
|
@@ -433,7 +432,6 @@ export class Agent {
|
|
|
433
432
|
// services from the effect handler's deps like the FSM path.
|
|
434
433
|
maxTurns: this.options.claudeMaxTurns,
|
|
435
434
|
statelessPerWake: this.options.claudeStatelessPerWake,
|
|
436
|
-
actsOnHireActivation: this.options.actsOnHireActivation === true,
|
|
437
435
|
...(this.options.wakeGate === false || !this.options.operatorKey
|
|
438
436
|
? {}
|
|
439
437
|
: {
|
|
@@ -845,7 +843,9 @@ export class Agent {
|
|
|
845
843
|
async wakeSuppression(event, _sessionId) {
|
|
846
844
|
const execution = currentWorkExecution();
|
|
847
845
|
const agreementId = execution ? execution.agreementId : workScopeFromWake(event).agreementId;
|
|
848
|
-
|
|
846
|
+
// A declined proposal cancels its agreement, and its proposer still wakes
|
|
847
|
+
// once to carry on: that outcome is the one terminal frame with work in it.
|
|
848
|
+
if (!isProposalOutcome(event) && await this.agreementIsTerminal(agreementId)) {
|
|
849
849
|
return { reason: 'agreement_terminal', because: 'its agreement is terminal' };
|
|
850
850
|
}
|
|
851
851
|
if (this.isSettledTaskEcho(event)) {
|
|
@@ -854,25 +854,6 @@ export class Agent {
|
|
|
854
854
|
because: 'its task already reported a result',
|
|
855
855
|
};
|
|
856
856
|
}
|
|
857
|
-
if (isHireActivationNotice(event)) {
|
|
858
|
-
const id = agreementIdFromWake(event);
|
|
859
|
-
const agreement = id && this.options.actsOnHireActivation !== true
|
|
860
|
-
? await this.agreementService.get(id).catch(() => null)
|
|
861
|
-
: null;
|
|
862
|
-
if (agreement?.terms?.interactionMode === 'tasks')
|
|
863
|
-
return { reason: 'task_only_activation', because: 'task-only service waits for assigned work' };
|
|
864
|
-
const formedBy = formedByFromWake(event) ?? agreement?.formedBy;
|
|
865
|
-
if (shouldSkipAgentFormedHireActivation({
|
|
866
|
-
event,
|
|
867
|
-
formedBy,
|
|
868
|
-
actsOnHireActivation: this.options.actsOnHireActivation === true,
|
|
869
|
-
})) {
|
|
870
|
-
return {
|
|
871
|
-
reason: 'agent_formed_hire_activation',
|
|
872
|
-
because: 'agent-formed hire activation',
|
|
873
|
-
};
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
857
|
return null;
|
|
877
858
|
}
|
|
878
859
|
/**
|
|
@@ -1170,7 +1151,9 @@ export class Agent {
|
|
|
1170
1151
|
this.stateTimers.consumeForEvent(frame, laneKey);
|
|
1171
1152
|
// Cold-start recovery: restore FSM state from sessionStore when the
|
|
1172
1153
|
// in-memory lane has no prior context (e.g. after a container restart).
|
|
1173
|
-
|
|
1154
|
+
// A shared host reloads on every wake: the thread may have run on another
|
|
1155
|
+
// process since this one last held it, and its copy would be stale.
|
|
1156
|
+
if (this.sessionStore && (this.sharedInbox || (session.ctx === undefined && session.state === undefined))) {
|
|
1174
1157
|
try {
|
|
1175
1158
|
const persisted = await this.sessionStore.load(laneKey);
|
|
1176
1159
|
if (persisted) {
|
|
@@ -1707,6 +1690,8 @@ export class Agent {
|
|
|
1707
1690
|
seen: this.seenMessages,
|
|
1708
1691
|
label,
|
|
1709
1692
|
concurrency: this.options.inboxConcurrency,
|
|
1693
|
+
sharedHost: this.sharedInbox,
|
|
1694
|
+
onLeadership: (leader) => this.setLeadership(leader),
|
|
1710
1695
|
onMessage: (text, metadata) => {
|
|
1711
1696
|
return this.handleMessage(text, metadata);
|
|
1712
1697
|
},
|
|
@@ -1732,13 +1717,46 @@ export class Agent {
|
|
|
1732
1717
|
},
|
|
1733
1718
|
});
|
|
1734
1719
|
}
|
|
1735
|
-
this.pollRunner?.start();
|
|
1736
1720
|
this.started = true;
|
|
1737
|
-
//
|
|
1738
|
-
//
|
|
1739
|
-
|
|
1721
|
+
// Shared hosts: only the leader runs the agent's polls and restores its
|
|
1722
|
+
// saved wake-ups, or every process would run them. The inbox tells this
|
|
1723
|
+
// process whether it leads after each read (setLeadership).
|
|
1724
|
+
if (!this.sharedInbox || !this.inboxLoopEnabled) {
|
|
1725
|
+
this.pollRunner?.start();
|
|
1726
|
+
// Not awaited: starting stays synchronous, so a second connect in the
|
|
1727
|
+
// same turn still sees `started`. A wake re-armed a moment later is fine.
|
|
1728
|
+
void this.restoreWakes();
|
|
1729
|
+
}
|
|
1740
1730
|
return this;
|
|
1741
1731
|
}
|
|
1732
|
+
/** Whether this process runs as one of several hosts of the agent. */
|
|
1733
|
+
get sharedInbox() {
|
|
1734
|
+
return sharedInboxHost(this.options.sharedInbox);
|
|
1735
|
+
}
|
|
1736
|
+
leading = false;
|
|
1737
|
+
wakesRestored = false;
|
|
1738
|
+
/**
|
|
1739
|
+
* Shared hosts: start the agent's polls (and restore its saved wake-ups,
|
|
1740
|
+
* once) when this process becomes the leader, and stop the polls when it
|
|
1741
|
+
* stops leading, so exactly one process runs them.
|
|
1742
|
+
*/
|
|
1743
|
+
setLeadership(leader) {
|
|
1744
|
+
if (!this.sharedInbox || leader === this.leading)
|
|
1745
|
+
return;
|
|
1746
|
+
this.leading = leader;
|
|
1747
|
+
if (leader) {
|
|
1748
|
+
this.pollRunner?.start();
|
|
1749
|
+
if (!this.wakesRestored) {
|
|
1750
|
+
this.wakesRestored = true;
|
|
1751
|
+
void this.restoreWakes();
|
|
1752
|
+
}
|
|
1753
|
+
runtimeLog.info(this.options.name || 'agent', 'leads its shared inbox: running polls');
|
|
1754
|
+
}
|
|
1755
|
+
else {
|
|
1756
|
+
this.pollRunner?.stop();
|
|
1757
|
+
runtimeLog.info(this.options.name || 'agent', 'no longer leads its shared inbox: polls stopped');
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1742
1760
|
/**
|
|
1743
1761
|
* Arm the lane's timer for the agent's own `wake_at`, and save it so a
|
|
1744
1762
|
* restart arms it again.
|
|
@@ -1942,3 +1960,9 @@ export function readScopesForWake(asked, wake) {
|
|
|
1942
1960
|
});
|
|
1943
1961
|
return narrowed.every(Boolean) ? narrowed : wake;
|
|
1944
1962
|
}
|
|
1963
|
+
/** The outcome of this agent's own proposal (the server hands it only to the proposer). */
|
|
1964
|
+
function isProposalOutcome(event) {
|
|
1965
|
+
const frame = (event ?? {});
|
|
1966
|
+
const reason = frame.reason ?? frame.resource?.reason;
|
|
1967
|
+
return reason === 'proposal_approved' || reason === 'proposal_approved_execute' || reason === 'proposal_rejected';
|
|
1968
|
+
}
|
|
@@ -47,12 +47,6 @@ export interface ClaudeBrainOptions {
|
|
|
47
47
|
queryFn?: typeof query;
|
|
48
48
|
/** Full authoritative agreement read used to recognize listing bookkeeping. */
|
|
49
49
|
readAgreement?: (agreementId: string) => Promise<unknown>;
|
|
50
|
-
/**
|
|
51
|
-
* Opt in to spending a model turn on an activation notice for a hire
|
|
52
|
-
* this agent's counterparty formed. Default is off — the host records a
|
|
53
|
-
* no-op and waits for the task.
|
|
54
|
-
*/
|
|
55
|
-
actsOnHireActivation?: boolean;
|
|
56
50
|
/**
|
|
57
51
|
* Asked before a turn is spent on chat messages; a confident
|
|
58
52
|
* "context" skips the turn. Absent means no gate (the default here; the
|
|
@@ -106,7 +100,6 @@ export declare class ClaudeBrain implements Brain {
|
|
|
106
100
|
private statelessPerWake;
|
|
107
101
|
private queryFn;
|
|
108
102
|
private readAgreement;
|
|
109
|
-
private actsOnHireActivation;
|
|
110
103
|
private askWakeGate;
|
|
111
104
|
/**
|
|
112
105
|
* Per-lane tool context. One agent object serves every room, and a
|
|
@@ -15,7 +15,6 @@ import { assertHostedAgentZiggsTools } from './hostedAgentConnection.js';
|
|
|
15
15
|
import { runtimeLog } from '../shared/runtimeLog.js';
|
|
16
16
|
import { resolveOnBehalfAgreementId, } from './onBehalfAgreement.js';
|
|
17
17
|
import { filterStandingOfferNotices } from './standingOfferNotice.js';
|
|
18
|
-
import { formedByFromWake, shouldSkipAgentFormedHireActivation, } from './hireActivationNotice.js';
|
|
19
18
|
import { gateableMessages, wakeAddressId, wakeAgreementId } from './wakeGate.js';
|
|
20
19
|
/**
|
|
21
20
|
* status notices must not spend a Claude turn answering
|
|
@@ -208,7 +207,6 @@ export class ClaudeBrain {
|
|
|
208
207
|
statelessPerWake;
|
|
209
208
|
queryFn;
|
|
210
209
|
readAgreement;
|
|
211
|
-
actsOnHireActivation;
|
|
212
210
|
askWakeGate;
|
|
213
211
|
/**
|
|
214
212
|
* Per-lane tool context. One agent object serves every room, and a
|
|
@@ -245,7 +243,6 @@ export class ClaudeBrain {
|
|
|
245
243
|
operatorKey: this.operatorKey,
|
|
246
244
|
agentId: this.agentId,
|
|
247
245
|
}));
|
|
248
|
-
this.actsOnHireActivation = opts.actsOnHireActivation === true;
|
|
249
246
|
this.askWakeGate = opts.askWakeGate ?? null;
|
|
250
247
|
}
|
|
251
248
|
async tick({ event, ctx, state, identity, eff, memory, signal, checkout }) {
|
|
@@ -265,22 +262,6 @@ export class ClaudeBrain {
|
|
|
265
262
|
};
|
|
266
263
|
const nextCtx = bootstrapCtx(identity, ctx);
|
|
267
264
|
nextCtx.lastOutcome = outcomeFromEvent(rawEvent ?? null, identity.agentId);
|
|
268
|
-
if (shouldSkipAgentFormedHireActivation({
|
|
269
|
-
event: rawEvent,
|
|
270
|
-
formedBy: formedByFromWake(rawEvent),
|
|
271
|
-
actsOnHireActivation: this.actsOnHireActivation,
|
|
272
|
-
})) {
|
|
273
|
-
const sessionId = identity.sessionId;
|
|
274
|
-
runtimeLog.info('ClaudeBrain', `agent-formed hire activation — skipping LLM session=${sessionId} agent=${this.agentId}`);
|
|
275
|
-
return {
|
|
276
|
-
ctx: nextCtx,
|
|
277
|
-
state: state ?? DEFAULT_STATE,
|
|
278
|
-
parked: true,
|
|
279
|
-
stateTimeoutUnhandled: false,
|
|
280
|
-
metrics: { llmCalls: 0, tokens: { total: 0, input: 0, output: 0 } },
|
|
281
|
-
skippedReason: 'agent_formed_hire_activation',
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
265
|
if (isNoticeOnlyWake(rawEvent)) {
|
|
285
266
|
runtimeLog.info('ClaudeBrain', `notice-only wake — skipping LLM session=${identity.sessionId} agent=${this.agentId} ` +
|
|
286
267
|
`type=${rawEvent?.type ?? 'unknown'}`);
|
|
@@ -41,7 +41,20 @@ export interface InboxLoopDeps {
|
|
|
41
41
|
* `ZIGGS_AGENT_INBOX_CONCURRENCY`, else {@link DEFAULT_INBOX_CONCURRENCY}.
|
|
42
42
|
*/
|
|
43
43
|
concurrency?: number;
|
|
44
|
+
/**
|
|
45
|
+
* This process is one of several hosting the agent. Each read says how many
|
|
46
|
+
* more threads this host can start, so the work spreads across hosts.
|
|
47
|
+
* Defaults to `ZIGGS_AGENT_SHARED_INBOX=1`.
|
|
48
|
+
*/
|
|
49
|
+
sharedHost?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Shared hosts: told after every read whether this host leads (holds the
|
|
52
|
+
* inbox lease). The leader runs the agent's polls and timers.
|
|
53
|
+
*/
|
|
54
|
+
onLeadership?: (leader: boolean) => void;
|
|
44
55
|
}
|
|
56
|
+
/** Whether this process runs as one of several hosts of its agents. */
|
|
57
|
+
export declare function sharedInboxHost(explicit?: boolean): boolean;
|
|
45
58
|
export interface InboxPassResult {
|
|
46
59
|
/** Delivery references this envelope carried. */
|
|
47
60
|
deliveries: number;
|
|
@@ -55,6 +68,8 @@ export interface InboxPassResult {
|
|
|
55
68
|
acked: boolean;
|
|
56
69
|
/** True when there was more mail than one envelope carries. */
|
|
57
70
|
truncated: boolean;
|
|
71
|
+
/** The latest delivery this envelope answered, to pass back as `seen`. */
|
|
72
|
+
bellAt?: string;
|
|
58
73
|
/**
|
|
59
74
|
* Handovers into the host that failed this pass. Non-zero means the
|
|
60
75
|
* watermark was deliberately held, so the same window arrives again.
|
|
@@ -143,6 +158,7 @@ export declare function pollInboxOnce(deps: InboxLoopDeps, waitSeconds?: number,
|
|
|
143
158
|
forceAck?: boolean;
|
|
144
159
|
signal?: AbortSignal;
|
|
145
160
|
dispatch?: InboxDispatch;
|
|
161
|
+
seen?: string;
|
|
146
162
|
}): Promise<InboxPassResult>;
|
|
147
163
|
export interface InboxLoopHandle {
|
|
148
164
|
/**
|
|
@@ -190,11 +206,10 @@ export declare const INBOX_STOP_TIMEOUT_MS = 30000;
|
|
|
190
206
|
*
|
|
191
207
|
* Wakes do not hold the loop: a pass starts its wakes (up to the agent's
|
|
192
208
|
* bound) and the loop reads again, so a new thread is taken up while another
|
|
193
|
-
* is still being answered. The server
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
* after the running wake.
|
|
209
|
+
* is still being answered. The server lists a row until it is handled, so
|
|
210
|
+
* while wakes run the loop passes back the envelope's `bellAt` as `seen`: the
|
|
211
|
+
* held read then answers only mail delivered after it, and a message from
|
|
212
|
+
* someone else is taken up as soon as it arrives.
|
|
198
213
|
*
|
|
199
214
|
* The FIRST pass is an immediate read. A held poll only answers to new mail, so
|
|
200
215
|
* a host that just started (or restarted after a crash) would otherwise sit
|
|
@@ -203,5 +218,4 @@ export declare const INBOX_STOP_TIMEOUT_MS = 30000;
|
|
|
203
218
|
*/
|
|
204
219
|
export declare function runInboxLoop(deps: InboxLoopDeps, opts?: {
|
|
205
220
|
waitSeconds?: number;
|
|
206
|
-
busyRecheckMs?: number;
|
|
207
221
|
}): InboxLoopHandle;
|
|
@@ -3,6 +3,12 @@ import { deliveryHandlingId, withWorkExecution } from '@ziggs-ai/api-client';
|
|
|
3
3
|
import { MessageRecordsClient, InboxClient, planInboxAck, planPartialInboxAck, } from '@ziggs-ai/api-client';
|
|
4
4
|
import { runtimeLog } from '../shared/runtimeLog.js';
|
|
5
5
|
import { isRateLimited } from '@ziggs-ai/api-client';
|
|
6
|
+
/** Whether this process runs as one of several hosts of its agents. */
|
|
7
|
+
export function sharedInboxHost(explicit) {
|
|
8
|
+
if (explicit !== undefined)
|
|
9
|
+
return explicit;
|
|
10
|
+
return /^(1|true|yes|shared)$/i.test(process.env.ZIGGS_AGENT_SHARED_INBOX ?? '');
|
|
11
|
+
}
|
|
6
12
|
/** How many assigned wakes one agent runs at once unless told otherwise. */
|
|
7
13
|
export const DEFAULT_INBOX_CONCURRENCY = 4;
|
|
8
14
|
/**
|
|
@@ -111,6 +117,7 @@ function inboxClient(deps) {
|
|
|
111
117
|
workspaceIds: deps.creds.workspaceIds,
|
|
112
118
|
app: deps.creds.app,
|
|
113
119
|
sessionName: deps.creds.sessionName,
|
|
120
|
+
...(sharedInboxHost(deps.sharedHost) ? { sharedHost: true } : {}),
|
|
114
121
|
});
|
|
115
122
|
}
|
|
116
123
|
async function releaseInboxHostBestEffort(deps) {
|
|
@@ -150,13 +157,13 @@ async function deliverAssignedMessage(deps, ref, reader, attempt, handledIds, tu
|
|
|
150
157
|
const execution = await prepareWorkExecution(ref.action, deps.creds.operatorKey, deps.ownAgentId, deps.baseUrl);
|
|
151
158
|
return withWorkExecution(execution, async () => {
|
|
152
159
|
const row = await reader.get(ref.resourceId);
|
|
153
|
-
//
|
|
154
|
-
const
|
|
155
|
-
|
|
160
|
+
// Every message names its thread; one that does not is refused, not guessed.
|
|
161
|
+
const threadRootId = row.rootMessageId;
|
|
162
|
+
if (typeof threadRootId !== 'string' || !threadRootId)
|
|
163
|
+
throw new Error(`Message ${row.messageId} names no thread root`);
|
|
156
164
|
const metadata = { messageId: row.messageId, ...(row.mandate ?? {}),
|
|
157
165
|
sender: { id: row.sender.ref, type: row.sender.kind }, receiver: { id: deps.ownAgentId, type: 'agent' },
|
|
158
|
-
entryType: 'message', intent: row.intent, timestamp: row.timestamp, roomless: true,
|
|
159
|
-
...(threadRootId ? { threadRootId } : {}) };
|
|
166
|
+
entryType: 'message', intent: row.intent, timestamp: row.timestamp, roomless: true, threadRootId };
|
|
160
167
|
// Hand over in dispatch order; only the read above runs ahead.
|
|
161
168
|
await turn.ready;
|
|
162
169
|
const handover = deps.onMessage(row.content.text, {
|
|
@@ -190,13 +197,21 @@ async function deliverAssignedMessage(deps, ref, reader, attempt, handledIds, tu
|
|
|
190
197
|
export async function pollInboxOnce(deps, waitSeconds = 0, opts = {}) {
|
|
191
198
|
const label = deps.label ?? 'inboxLoop';
|
|
192
199
|
const inbox = inboxClient(deps);
|
|
200
|
+
const shared = sharedInboxHost(deps.sharedHost);
|
|
201
|
+
const free = opts.dispatch
|
|
202
|
+
? Math.max(0, opts.dispatch.limit - opts.dispatch.size)
|
|
203
|
+
: inboxConcurrency(deps.concurrency);
|
|
193
204
|
const envelope = await inbox.getInbox({
|
|
194
205
|
...(waitSeconds > 0 ? { waitSeconds } : {}),
|
|
206
|
+
...(opts.seen ? { seen: opts.seen } : {}),
|
|
195
207
|
...(opts.signal ? { signal: opts.signal } : {}),
|
|
208
|
+
...(shared ? { capacity: free } : {}),
|
|
196
209
|
});
|
|
197
210
|
// A response may race shutdown. Do not enqueue another wake after stopping;
|
|
198
211
|
// an already-running pass keeps its lease through its final acknowledgement.
|
|
199
212
|
opts.signal?.throwIfAborted();
|
|
213
|
+
if (envelope.host)
|
|
214
|
+
deps.onLeadership?.(envelope.host.leader);
|
|
200
215
|
let messagesDelivered = 0;
|
|
201
216
|
let resourceEvents = 0;
|
|
202
217
|
let tasksDelivered = 0;
|
|
@@ -585,6 +600,7 @@ export async function pollInboxOnce(deps, waitSeconds = 0, opts = {}) {
|
|
|
585
600
|
'window will be redelivered');
|
|
586
601
|
}
|
|
587
602
|
return {
|
|
603
|
+
...(envelope.bellAt ? { bellAt: envelope.bellAt } : {}),
|
|
588
604
|
deliveries: envelope.deliveries.length,
|
|
589
605
|
messagesDelivered,
|
|
590
606
|
resourceEvents,
|
|
@@ -605,9 +621,8 @@ export async function pollInboxOnce(deps, waitSeconds = 0, opts = {}) {
|
|
|
605
621
|
* some defaults ship with. Lower it if your deployment sits behind a shorter one.
|
|
606
622
|
*/
|
|
607
623
|
const DEFAULT_WAIT_SECONDS = 110;
|
|
608
|
-
/**
|
|
609
|
-
|
|
610
|
-
const BUSY_RECHECK_MS = 3_000;
|
|
624
|
+
/** The shortest gap between two reads that found nothing new. */
|
|
625
|
+
const IDLE_REREAD_MS = 1_000;
|
|
611
626
|
const ERROR_BACKOFF_BASE_MS = 1_000;
|
|
612
627
|
const ERROR_BACKOFF_MAX_MS = 30_000;
|
|
613
628
|
/**
|
|
@@ -657,11 +672,10 @@ function withinMs(promise, ms) {
|
|
|
657
672
|
*
|
|
658
673
|
* Wakes do not hold the loop: a pass starts its wakes (up to the agent's
|
|
659
674
|
* bound) and the loop reads again, so a new thread is taken up while another
|
|
660
|
-
* is still being answered. The server
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
* after the running wake.
|
|
675
|
+
* is still being answered. The server lists a row until it is handled, so
|
|
676
|
+
* while wakes run the loop passes back the envelope's `bellAt` as `seen`: the
|
|
677
|
+
* held read then answers only mail delivered after it, and a message from
|
|
678
|
+
* someone else is taken up as soon as it arrives.
|
|
665
679
|
*
|
|
666
680
|
* The FIRST pass is an immediate read. A held poll only answers to new mail, so
|
|
667
681
|
* a host that just started (or restarted after a crash) would otherwise sit
|
|
@@ -671,8 +685,11 @@ function withinMs(promise, ms) {
|
|
|
671
685
|
export function runInboxLoop(deps, opts = {}) {
|
|
672
686
|
const label = deps.label ?? 'inboxLoop';
|
|
673
687
|
const waitSeconds = opts.waitSeconds ?? DEFAULT_WAIT_SECONDS;
|
|
674
|
-
const busyRecheckMs = Math.min(opts.busyRecheckMs ?? BUSY_RECHECK_MS, waitSeconds * 1000);
|
|
675
688
|
const dispatch = new InboxDispatch(deps.concurrency);
|
|
689
|
+
// The delivery the last read answered. Passed back while wakes run, so the
|
|
690
|
+
// next held read waits for new mail; never after a failed pass, whose rows
|
|
691
|
+
// must come back on the retry.
|
|
692
|
+
let seen;
|
|
676
693
|
let running = true;
|
|
677
694
|
let consecutiveErrors = 0;
|
|
678
695
|
let heldPasses = 0;
|
|
@@ -688,7 +705,13 @@ export function runInboxLoop(deps, opts = {}) {
|
|
|
688
705
|
while (running) {
|
|
689
706
|
try {
|
|
690
707
|
lastAttemptAt = Date.now();
|
|
691
|
-
const pass = await pollInboxOnce(deps, first ? 0 : waitSeconds, {
|
|
708
|
+
const pass = await pollInboxOnce(deps, first ? 0 : waitSeconds, {
|
|
709
|
+
signal: shutdown.signal,
|
|
710
|
+
dispatch,
|
|
711
|
+
...(seen && dispatch.size > 0 && !heldPasses ? { seen } : {}),
|
|
712
|
+
});
|
|
713
|
+
if (pass.bellAt)
|
|
714
|
+
seen = pass.bellAt;
|
|
692
715
|
lastPassAt = Date.now();
|
|
693
716
|
first = false;
|
|
694
717
|
consecutiveErrors = 0;
|
|
@@ -697,7 +720,7 @@ export function runInboxLoop(deps, opts = {}) {
|
|
|
697
720
|
if (heldPasses)
|
|
698
721
|
await sleep(Math.min(ERROR_BACKOFF_MAX_MS, ERROR_BACKOFF_BASE_MS * 2 ** Math.min(heldPasses - 1, 5)), shutdown.signal);
|
|
699
722
|
else if (dispatch.size > 0 && !passMadeProgress(pass))
|
|
700
|
-
await dispatch.settledOr(
|
|
723
|
+
await dispatch.settledOr(IDLE_REREAD_MS, shutdown.signal);
|
|
701
724
|
}
|
|
702
725
|
catch (err) {
|
|
703
726
|
if (!running)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ziggs-ai/agent-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0",
|
|
4
4
|
"description": "Agent framework SDK for building autonomous agents on the Ziggs platform",
|
|
5
5
|
"homepage": "https://ziggsai.com/docs",
|
|
6
6
|
"bugs": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@anthropic-ai/claude-agent-sdk": "^0.3.284",
|
|
31
31
|
"@anthropic-ai/sdk": "^0.110.0",
|
|
32
32
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
33
|
-
"@ziggs-ai/api-client": "0.40.
|
|
33
|
+
"@ziggs-ai/api-client": "0.40.3",
|
|
34
34
|
"@ziggs-ai/contracts": "0.46.0",
|
|
35
35
|
"ajv": "^8.17.1",
|
|
36
36
|
"dotenv": "^17.2.3",
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Whether a hire-activation notice earns a model call is the host's
|
|
3
|
-
* decision. The rule reads the agreement row (`formedBy`), never an
|
|
4
|
-
* agent kind, name, or id shape.
|
|
5
|
-
*
|
|
6
|
-
* A hire an agent formed is a recorded no-op unless the hired agent's
|
|
7
|
-
* manifest opts in. A hire a person formed (store claim) still wakes
|
|
8
|
-
* so the provider can open the room and greet.
|
|
9
|
-
*/
|
|
10
|
-
export type AgreementFormedBy = 'agent' | 'principal';
|
|
11
|
-
export declare function formedByFromWake(event: unknown): AgreementFormedBy | undefined;
|
|
12
|
-
export declare function agreementIdFromWake(event: unknown): string | undefined;
|
|
13
|
-
/** Chat activation ping, store-claim doorbell, or approval-echo fan-out. */
|
|
14
|
-
export declare function isHireActivationNotice(event: unknown): boolean;
|
|
15
|
-
export declare function shouldSkipAgentFormedHireActivation(input: {
|
|
16
|
-
event: unknown;
|
|
17
|
-
formedBy?: AgreementFormedBy;
|
|
18
|
-
actsOnHireActivation?: boolean;
|
|
19
|
-
}): boolean;
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Whether a hire-activation notice earns a model call is the host's
|
|
3
|
-
* decision. The rule reads the agreement row (`formedBy`), never an
|
|
4
|
-
* agent kind, name, or id shape.
|
|
5
|
-
*
|
|
6
|
-
* A hire an agent formed is a recorded no-op unless the hired agent's
|
|
7
|
-
* manifest opts in. A hire a person formed (store claim) still wakes
|
|
8
|
-
* so the provider can open the room and greet.
|
|
9
|
-
*/
|
|
10
|
-
import { SERVER_EVENTS } from '@ziggs-ai/contracts';
|
|
11
|
-
const ACTIVATION_LIFECYCLE_OPS = new Set([
|
|
12
|
-
'proposal_approved',
|
|
13
|
-
'proposal_approved_execute',
|
|
14
|
-
]);
|
|
15
|
-
function asRecord(value) {
|
|
16
|
-
return value && typeof value === 'object' ? value : null;
|
|
17
|
-
}
|
|
18
|
-
export function formedByFromWake(event) {
|
|
19
|
-
const ev = asRecord(event);
|
|
20
|
-
if (!ev)
|
|
21
|
-
return undefined;
|
|
22
|
-
const direct = ev.formedBy;
|
|
23
|
-
if (direct === 'agent' || direct === 'principal')
|
|
24
|
-
return direct;
|
|
25
|
-
const agreement = asRecord(ev.agreement);
|
|
26
|
-
const fromAgreement = agreement?.formedBy;
|
|
27
|
-
if (fromAgreement === 'agent' || fromAgreement === 'principal')
|
|
28
|
-
return fromAgreement;
|
|
29
|
-
const resource = asRecord(ev.resource);
|
|
30
|
-
const fromResource = resource?.formedBy;
|
|
31
|
-
if (fromResource === 'agent' || fromResource === 'principal')
|
|
32
|
-
return fromResource;
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
export function agreementIdFromWake(event) {
|
|
36
|
-
const ev = asRecord(event);
|
|
37
|
-
if (!ev)
|
|
38
|
-
return undefined;
|
|
39
|
-
if (typeof ev.agreementId === 'string' && ev.agreementId)
|
|
40
|
-
return ev.agreementId;
|
|
41
|
-
const agreement = asRecord(ev.agreement);
|
|
42
|
-
if (typeof agreement?.agreementId === 'string' && agreement.agreementId) {
|
|
43
|
-
return agreement.agreementId;
|
|
44
|
-
}
|
|
45
|
-
const resource = asRecord(ev.resource);
|
|
46
|
-
if (typeof resource?.agreementId === 'string' && resource.agreementId) {
|
|
47
|
-
return resource.agreementId;
|
|
48
|
-
}
|
|
49
|
-
if (resource?.kind === 'agreement' &&
|
|
50
|
-
typeof resource.resourceId === 'string' &&
|
|
51
|
-
resource.resourceId) {
|
|
52
|
-
return resource.resourceId;
|
|
53
|
-
}
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
/** Chat activation ping, store-claim doorbell, or approval-echo fan-out. */
|
|
57
|
-
export function isHireActivationNotice(event) {
|
|
58
|
-
const ev = asRecord(event);
|
|
59
|
-
if (!ev)
|
|
60
|
-
return false;
|
|
61
|
-
const type = ev.type;
|
|
62
|
-
const operation = typeof ev.operation === 'string' ? ev.operation : undefined;
|
|
63
|
-
if (type === 'agreement_lifecycle' && operation && ACTIVATION_LIFECYCLE_OPS.has(operation)) {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
if (type !== SERVER_EVENTS.resourceChanged)
|
|
67
|
-
return false;
|
|
68
|
-
const resource = asRecord(ev.resource) ?? ev;
|
|
69
|
-
const reason = typeof resource.reason === 'string' ? resource.reason : undefined;
|
|
70
|
-
if (reason === 'proposal_approved')
|
|
71
|
-
return true;
|
|
72
|
-
return typeof reason === 'string' && reason.startsWith('approval:');
|
|
73
|
-
}
|
|
74
|
-
export function shouldSkipAgentFormedHireActivation(input) {
|
|
75
|
-
if (input.actsOnHireActivation)
|
|
76
|
-
return false;
|
|
77
|
-
if (input.formedBy !== 'agent')
|
|
78
|
-
return false;
|
|
79
|
-
return isHireActivationNotice(input.event);
|
|
80
|
-
}
|