@cotal-ai/manager 0.166.2 → 0.167.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.
@@ -0,0 +1,221 @@
1
+ import { type NatsConnection } from "@nats-io/transport-node";
2
+ import { type CleanupFailure, type DeprovisionResourceAccounting, type Identity, type RuntimeReference, type SecretStore, type SpaceAuth, type dialerFor } from "@cotal-ai/core";
3
+ import type { ManagerOptions } from "./manager.js";
4
+ import type { AgentHandle } from "./runtime/index.js";
5
+ /** What the teardown chain (trackDeprovision → deprovision → driveDeprovision →
6
+ * driveStaticRetirement) needs to tear one incarnation down. Each hop passes the value on as a
7
+ * variable, which TypeScript does not excess-check, so the hops share this one declaration. */
8
+ export interface TeardownTarget {
9
+ id: string;
10
+ name: string;
11
+ lifecycleUid: string;
12
+ userOwner?: string;
13
+ delegated?: true;
14
+ secretPaths?: {
15
+ creds?: string;
16
+ actorToken?: string;
17
+ sentinelCreds?: string;
18
+ health?: string;
19
+ };
20
+ runtime?: RuntimeReference;
21
+ delegatedHandle?: AgentHandle;
22
+ launch?: {
23
+ allowSubscribe: readonly string[];
24
+ };
25
+ /** The departed agent's admitted renewal, which the static terminal drains before it begins. */
26
+ staticCredentialRenewal?: Promise<void>;
27
+ /** Set when this lifecycle's inline snapshot is settled: the file is gone, or the lifecycle never
28
+ * had one. A live slot's free and a manager shutdown set it once they removed the seat's own, and
29
+ * a spawn's rollback once it removed the one it wrote or its write was refused, since a file that
30
+ * write refused was never this lifecycle's. Absent after a removal that failed, and for a
31
+ * lifecycle known only by its slot row; the static terminal then removes the uid's path. */
32
+ snapshotSettled?: true;
33
+ }
34
+ /** A name held pending its incarnation's retirement. It keeps the teardown target whole, so a
35
+ * same-name spawn or a resume re-drives the same teardown the first drive ran. */
36
+ export interface RetirementHold {
37
+ opId: string;
38
+ owner: string;
39
+ actor: string;
40
+ target: TeardownTarget;
41
+ startedAt: number;
42
+ lastError?: string;
43
+ standingAuthorityLive?: boolean;
44
+ lastResources?: DeprovisionResourceAccounting;
45
+ }
46
+ /** The retirement coordinator reads current authority at each call and delegates process and
47
+ * maintenance ownership to its manager. */
48
+ export interface RetirementDeps {
49
+ auth(): SpaceAuth | undefined;
50
+ remoteAuthority(): Pick<NonNullable<ManagerOptions["remoteAuthority"]>, "owner" | "actors" | "prepareAgentRetirement" | "mintRetirementRequester"> | undefined;
51
+ userMode(): boolean;
52
+ workspaceRoot(): string;
53
+ space(): string;
54
+ servers(): string | undefined;
55
+ secrets(): SecretStore;
56
+ serveIdentity(): Identity | undefined;
57
+ lifecycleUid(): string;
58
+ instanceId(): string;
59
+ serveEpoch(): number | undefined;
60
+ dial(opts: Parameters<ReturnType<typeof dialerFor>>[0]): Promise<NatsConnection>;
61
+ withRetirementSlot<T>(run: () => Promise<T>): Promise<T>;
62
+ driveStaticRetirement(a: TeardownTarget): Promise<void>;
63
+ awaitHandleExit(handle: AgentHandle): Promise<void>;
64
+ shredUserSecrets(failures: CleanupFailure[], family: {
65
+ actorToken?: string;
66
+ sentinelCreds?: string;
67
+ health?: string;
68
+ }): Promise<void>;
69
+ dropSeatRecord(a: {
70
+ name: string;
71
+ lifecycleUid: string;
72
+ }): void;
73
+ fenceRetiringAcl(a: {
74
+ id: string;
75
+ lifecycleUid: string;
76
+ }): Promise<string | undefined>;
77
+ lifecycleMembershipInventory(a: {
78
+ id: string;
79
+ lifecycleUid: string;
80
+ }): Promise<{
81
+ complete: boolean;
82
+ channels: string[];
83
+ reason?: string;
84
+ }>;
85
+ brokerTeardown<T>(task: Promise<T>, message: string): Promise<T>;
86
+ trackLifecycle(): () => void;
87
+ }
88
+ /** Retirement holds, exact-lifecycle teardown flights and confirmed predecessor coordinates. */
89
+ export declare class Retirement {
90
+ private readonly deps;
91
+ /** Names RESERVED PENDING RETIREMENT: a despawned agent's name stays held until
92
+ * the auth plane confirms its lifecycle's retirement TERMINAL over the auth-admin rail - the
93
+ * alias-reuse gate that closes the same-name despawn→respawn race at its root. An UNCERTAIN
94
+ * outcome (rail down, timeout) keeps the hold with the last attempt's copy; a same-name spawn
95
+ * refuses legibly AND re-fires the request. In-memory: across a manager restart the durable
96
+ * truth is the auth-side lifecycle head itself (an unretired head refuses issuance - the
97
+ * named residual this belt narrows, not replaces). */
98
+ private retiring;
99
+ /** SINGLE-FLIGHT guard for {@link requestRetirement} (audit #1): one in-flight rail round-trip per
100
+ * (name, lifecycleUid). The detached `deprovision` call and every same-name-spawn nudge for THAT
101
+ * lifecycle JOIN the same promise instead of stacking independent requests that dual-enter the
102
+ * barrier; a fresh trigger after it settles re-drives. Keyed by (name, uid) - NOT name alone - so a
103
+ * same-name SUCCESSOR (which can spawn after the hold clears but before this flight's `nc.close`
104
+ * yield settles) never joins the predecessor's rail request and skips its own retirement. */
105
+ private retiringFlight;
106
+ /** Wire principals of RETIRED static incarnations: populated at every completed
107
+ * static terminal and from the boot sweep's retired slot rows, so a copied credential of a
108
+ * retired incarnation is refused at the control surface even across a manager restart. The
109
+ * durable truth is the slot row + principal-keyed head; this set is the in-memory index of it
110
+ * (one string per retired incarnation - bounded by lifecycle count, never pruned in-process). */
111
+ private readonly retiredPrincipals;
112
+ /** Exact predecessor incarnations whose full hosted retirement reached a terminal answer. Presence
113
+ * is advisory and can retain that old lifecycle briefly after its process exits. Resume may ignore
114
+ * only this exact (alias, principal, lifecycleUid) row when adopting a different lifecycle; every
115
+ * unknown, current, or differently identified live row still refuses. A later retirement of the
116
+ * same alias replaces its prior predecessor coordinate. */
117
+ private readonly confirmedRetiredPredecessors;
118
+ /** SINGLE-FLIGHT guard for {@link deprovision} (INT-2/C): one in-flight teardown per
119
+ * (name, lifecycleUid). The detached freeSlot teardown and every same-name-spawn nudge that
120
+ * re-drives it JOIN one promise instead of launching a SECOND, concurrent teardown. Without it,
121
+ * two teardowns race the NAME-KEYED ledger revoke: once the first frees the alias and a successor
122
+ * mints its own row, the second's delayed revoke (which carries no lifecycle coordinate) would
123
+ * delete the SUCCESSOR's standing authority. Keyed by (name, uid) so a later same-name lifecycle
124
+ * gets its own flight; a fresh trigger after settle re-drives only if the hold still stands. */
125
+ private deprovisioningFlight;
126
+ constructor(deps: RetirementDeps);
127
+ private get auth();
128
+ private get remoteAuthority();
129
+ private get userMode();
130
+ private get workspaceRoot();
131
+ private get space();
132
+ private get servers();
133
+ private get secrets();
134
+ private get managerServeIdentity();
135
+ private get managerLifecycleUid();
136
+ private get managerInstanceId();
137
+ private dial;
138
+ private withRetirementSlot;
139
+ private driveStaticRetirement;
140
+ private awaitHandleExit;
141
+ private shredUserSecrets;
142
+ private dropSeatRecord;
143
+ private fenceRetiringAcl;
144
+ private lifecycleMembershipInventory;
145
+ hold(name: string, hold: RetirementHold): void;
146
+ held(name: string): RetirementHold | undefined;
147
+ hasHold(name: string): boolean;
148
+ predecessor(name: string): {
149
+ principal: string;
150
+ lifecycleUid: string;
151
+ } | undefined;
152
+ get pendingCount(): number;
153
+ get inFlightCount(): number;
154
+ rememberRetired(principal: string): void;
155
+ recordStaticRetirement(a: {
156
+ id: string;
157
+ name: string;
158
+ lifecycleUid: string;
159
+ }): void;
160
+ membershipRefusal(caller: string): string | undefined;
161
+ /** Tear down a departed agent's minted footprint (#159 B2, auth mode): its local-principal durables
162
+ * (`dm_local-<id>`, `dlv_local-<id>`), its read-ACL row, and its creds file - everything the spawn's
163
+ * `provisionAgent` + creds-write left behind. Mints an EPHEMERAL, TARGET-PINNED `deprovisioner` cred
164
+ * (mirrors the ephemeral `provisioner`/`purger`): it can delete only THIS agent's local-principal footprint,
165
+ * never a peer's and never the role-shared `svc_<role>` (which its siblings still bind). Open mesh →
166
+ * no-op (nothing was minted). Idempotent at the broker (missing consumer / ACL row = no-op) and on
167
+ * disk (`force` tolerates an absent creds file, e.g. a ledgered deploy that wrote none).
168
+ *
169
+ * Removing the creds file is footprint REDUCTION, not revocation: a JWT copied off disk before exit
170
+ * keeps its inline publish/live-sub/control grants until key rotation or JWT expiry - cred revocation
171
+ * is the separate per-user-auth work, not this. Tearing down the durables + ACL row still shrinks the
172
+ * delivery surface a stale copy could use. */
173
+ deprovision(a: TeardownTarget): Promise<void>;
174
+ /** The actual footprint teardown (wrapped by {@link deprovision}'s single-flight). */
175
+ driveDeprovision(a: TeardownTarget): Promise<void>;
176
+ /** Request the auth-side retirement of a departed agent's lifecycle: an ephemeral
177
+ * `retirement-requester` credential (request + reply only), the generic `retireLifecycle` op,
178
+ * a STABLE opId (derived from the lifecycleUid, so every retry re-drives the SAME operation),
179
+ * and the four-outcome handling in operator vocabulary. */
180
+ requestRetirement(a: {
181
+ id: string;
182
+ name: string;
183
+ lifecycleUid: string;
184
+ }): Promise<void>;
185
+ /** The rail round-trip for one retirement (wrapped by {@link requestRetirement}'s single-flight). */
186
+ driveRetirement(a: {
187
+ id: string;
188
+ name: string;
189
+ lifecycleUid: string;
190
+ }): Promise<void>;
191
+ /** Apply one terminal retirement answer. The predecessor coordinate is recorded only when the
192
+ * answer clears this exact lifecycle's hold, never for an ABA-late answer or an incomplete hosted
193
+ * teardown whose standing authority still lives. */
194
+ confirmRetirement(a: {
195
+ id: string;
196
+ name: string;
197
+ lifecycleUid: string;
198
+ }): void;
199
+ /** The teardown's ASYNC BROKER PHASE: mint the ephemeral target-pinned deprovisioner cred and
200
+ * delete the agent's broker footprint (dm_/dlv_ durables + read-ACL row). Split from
201
+ * {@link deprovision} because it runs LAST in the ordered teardown chain - after the creds/secret
202
+ * shred and the awaited ledger revoke, which precede it in that same single-flighted chain (the
203
+ * revoke is awaited and can be deliberately slow, so it is not merely a synchronous prefix). The name
204
+ * is NOT freed while any teardown phase is still in flight - the hold clears only after the
205
+ * standing-authority revoke AND the lifecycle retirement both confirm (see {@link driveRetirement}) -
206
+ * but the deletes here are still lifecycle-uid-pinned so even a replayed/stale teardown can never
207
+ * reach a same-name successor's footprint (its names embed a different uid). Each call reports its
208
+ * own accounting: the return value, or the {@link DeprovisionError} it throws once the deletes ran. */
209
+ deprovisionBroker(a: {
210
+ id: string;
211
+ name: string;
212
+ lifecycleUid: string;
213
+ launch?: {
214
+ allowSubscribe: readonly string[];
215
+ };
216
+ }): Promise<DeprovisionResourceAccounting>;
217
+ /** A cleanup spawned by accepted active-mode work is part of that work for maintenance draining,
218
+ * even where the ordinary control reply remains fire-and-forget. */
219
+ trackDeprovision(a: TeardownTarget, context?: string): void;
220
+ }
221
+ //# sourceMappingURL=retirement.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retirement.d.ts","sourceRoot":"","sources":["../src/retirement.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAClF,OAAO,EAKL,KAAK,cAAc,EAAE,KAAK,6BAA6B,EAAE,KAAK,QAAQ,EAAE,KAAK,gBAAgB,EAC7F,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,KAAK,SAAS,EACjD,MAAM,gBAAgB,CAAC;AAGxB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD;;gGAEgG;AAChG,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,IAAI,CAAC;IACjB,WAAW,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC/F,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,eAAe,CAAC,EAAE,WAAW,CAAC;IAC9B,MAAM,CAAC,EAAE;QAAE,cAAc,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,CAAC;IAC/C,gGAAgG;IAChG,uBAAuB,C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