@antasphere/cli-core 0.5.0 → 0.7.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.
Files changed (48) hide show
  1. package/README.md +324 -85
  2. package/dist/auth.d.ts +1 -1
  3. package/dist/auth.d.ts.map +1 -1
  4. package/dist/config.d.ts +120 -62
  5. package/dist/config.d.ts.map +1 -1
  6. package/dist/config.js +207 -91
  7. package/dist/config.js.map +1 -1
  8. package/dist/connect-on-demand.d.ts +196 -99
  9. package/dist/connect-on-demand.d.ts.map +1 -1
  10. package/dist/connect-on-demand.js +565 -117
  11. package/dist/connect-on-demand.js.map +1 -1
  12. package/dist/connect.d.ts +8 -8
  13. package/dist/connect.d.ts.map +1 -1
  14. package/dist/index.d.ts +25 -17
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +20 -15
  17. package/dist/index.js.map +1 -1
  18. package/dist/kept-keys.d.ts +208 -0
  19. package/dist/kept-keys.d.ts.map +1 -0
  20. package/dist/kept-keys.js +484 -0
  21. package/dist/kept-keys.js.map +1 -0
  22. package/dist/lock.d.ts +65 -0
  23. package/dist/lock.d.ts.map +1 -0
  24. package/dist/lock.js +408 -0
  25. package/dist/lock.js.map +1 -0
  26. package/dist/migrate.d.ts +9 -3
  27. package/dist/migrate.d.ts.map +1 -1
  28. package/dist/migrate.js +32 -11
  29. package/dist/migrate.js.map +1 -1
  30. package/dist/resolve.d.ts +12 -4
  31. package/dist/resolve.d.ts.map +1 -1
  32. package/dist/resolve.js +12 -8
  33. package/dist/resolve.js.map +1 -1
  34. package/dist/sign-in.d.ts +266 -0
  35. package/dist/sign-in.d.ts.map +1 -0
  36. package/dist/sign-in.js +398 -0
  37. package/dist/sign-in.js.map +1 -0
  38. package/package.json +1 -1
  39. package/src/auth.ts +1 -1
  40. package/src/config.ts +303 -136
  41. package/src/connect-on-demand.ts +801 -223
  42. package/src/connect.ts +8 -8
  43. package/src/index.ts +42 -26
  44. package/src/kept-keys.ts +561 -0
  45. package/src/lock.ts +479 -0
  46. package/src/migrate.ts +33 -11
  47. package/src/resolve.ts +20 -15
  48. package/src/sign-in.ts +601 -0
package/README.md CHANGED
@@ -9,10 +9,11 @@ stranded-key recovery, and the `CliIo` prompt seam. Tool commands, wire types,
9
9
  and API surfaces live in the tools — this package carries only config/login
10
10
  plumbing, never secrets.
11
11
 
12
- The model: one identity at the hub (`antasphere login`, a user-scoped `ant_`
13
- key); per-tool credentials minted from it by the exchange
14
- (`POST {hub}/api/v1/sso/tool-token`, then `POST {tool}/api/v1/sso/cli-connect`
15
- — the exchange names no organization, hub ADR 014); the organization a
12
+ The model: one login per Antasphere hub (`antasphere login`, a user-scoped
13
+ `ant_` key); per-tool credentials minted by the exchange from the login at the
14
+ hub each site trusts (`POST {hub}/api/v1/sso/tool-token`, then
15
+ `POST {tool}/api/v1/sso/cli-connect` — the exchange names no organization,
16
+ hub ADR 014); the organization a
16
17
  per-request parameter (the hub's server-side default organization, or
17
18
  `--org <hub org id or name>` on one command, which the tool maps to its own
18
19
  workspace through `central_account_id`). Every tool CLI ships its cloud URL and
@@ -46,17 +47,26 @@ Each file holds one tool's profiles. A tool file, e.g. `tools/slideless.json`:
46
47
  "activeProfile": "cloud",
47
48
  "profiles": {
48
49
  "cloud": {
49
- "baseUrl": "https://slideless.antasphere.com",
50
- "connectKeys": {
51
- "default": {
52
- "apiKey": "slk_…",
50
+ "baseUrl": "https://slideless.example.com",
51
+ "site": {
52
+ "hub": "https://account.example.com",
53
+ "federated": true,
54
+ "name": "Slideless",
55
+ "cliVersion": "0.15.0",
56
+ "seenAt": "2026-11-25T09:12:44.000Z"
57
+ },
58
+ "hubKeys": [
59
+ {
60
+ "hub": "https://account.example.com",
53
61
  "email": "you@example.com",
54
- "mintedAt": "2026-09-30T09:12:44.000Z"
62
+ "apiKey": "slk_…",
63
+ "mintedAt": "2026-11-25T09:12:44.000Z"
55
64
  }
56
- }
65
+ ],
66
+ "lastHub": "https://account.example.com"
57
67
  },
58
- "share.antasphere.com": {
59
- "baseUrl": "https://share.antasphere.com",
68
+ "share.example.com": {
69
+ "baseUrl": "https://share.example.com",
60
70
  "apiKey": "slk_…"
61
71
  },
62
72
  "localhost:3400": {
@@ -67,10 +77,19 @@ Each file holds one tool's profiles. A tool file, e.g. `tools/slideless.json`:
67
77
  }
68
78
  ```
69
79
 
70
- `baseUrl` is the instance the profile is; a profile with no `baseUrl` is at the
71
- tool's cloud URL. `apiKey` is the tool's own login; `connectKeys` holds the keys
72
- the hub exchange minted, one per hub profile (the slot name, here `default`, is
73
- the hub profile's name).
80
+ `baseUrl` is the site the profile is; a profile with no `baseUrl` is at the
81
+ tool's cloud URL. `site` is what that site last said of itself in its
82
+ discovery (`GET /api/v1/instance`): the hub it names (`null` for a site from
83
+ before sites named theirs), whether it signs in through a hub, its name, the
84
+ CLI version it announces, and when. `apiKey` is the tool's own login;
85
+ `hubKeys` are its KEPT KEYS, the keys the hub exchange minted for this site,
86
+ one per hub and account — never filed under a login's name, so renaming a
87
+ login or signing out and in again strands nothing. `connectKeys`, a map keyed
88
+ by a login's name, is their old shape: the first command that resolves a
89
+ credential on the profile moves each entry once into `hubKeys` (its login
90
+ still in place with that address), or revokes it on the site and forgets it.
91
+ `lastHub` is the address of the hub the profile's last exchange went through,
92
+ in its normal form (`normalizeUrl`, its path kept; no key, no address).
74
93
 
75
94
  The hub's `config.json`:
76
95
 
@@ -80,7 +99,7 @@ The hub's `config.json`:
80
99
  "profiles": {
81
100
  "default": {
82
101
  "apiKey": "ant_…",
83
- "baseUrl": "https://account.antasphere.com",
102
+ "baseUrl": "https://account.example.com",
84
103
  "email": "you@example.com",
85
104
  "workspaceId": "…"
86
105
  }
@@ -88,15 +107,28 @@ The hub's `config.json`:
88
107
  }
89
108
  ```
90
109
 
110
+ Each entry that holds a key is a LOGIN: one account signed in at one hub (its
111
+ `baseUrl`; one with none is at the hub's cloud URL). Several may share a hub.
91
112
  `workspaceId` is the default organization at login — informational; the hub's
92
- server-side default is what applies. All config I/O is env-injected (no
113
+ server-side default is what applies. Its `activeProfile` is the account CLI's
114
+ own default: no tool command reads it. All config I/O is env-injected (no
93
115
  `process.env` reads) — tests point the whole config world at a temp dir via
94
116
  `XDG_CONFIG_HOME`.
95
117
 
118
+ Many commands share one file (agent sessions side by side on one machine), so
119
+ a write goes to a temporary file in the same directory, renamed over the real
120
+ one: a reader sees the old file or the new one, never half of one. Every
121
+ read-modify-write holds `<file>.lock` for its whole span; a dead holder's lock
122
+ is taken over (its pid gone on this host, older than 30 s, or empty and older
123
+ than 2 s). What a take-over leaves beside the lock is removed by a later
124
+ holder once it is older than the lock's age limit, never sooner. A file that
125
+ exists and does not parse is an error naming it, never an empty config, and
126
+ nothing is written when nothing changed.
127
+
96
128
  ## Profiles
97
129
 
98
- A profile is an instance, named for its host by default (`cloud` for the tool's
99
- cloud URL, `share.antasphere.com`, `localhost:3400`). `selectProfile` applies
130
+ A profile is a site, named for its host by default (`cloud` for the tool's
131
+ cloud URL, `share.example.com`, `localhost:3400`). `selectProfile` applies
100
132
  one rule, in this order:
101
133
 
102
134
  1. `--profile <name>` wins, and the profile must exist.
@@ -114,46 +146,191 @@ one rule, in this order:
114
146
  `cloud`, or the host (then `<host>-2`, …) when `cloud` is taken elsewhere.
115
147
 
116
148
  `created: true` means the selected profile is not on disk yet (made in memory
117
- for an instance URL, or the implicit `cloud` profile) and its name is free. A
118
- profile with no `baseUrl` is at the cloud URL, for the selection and for the
119
- connect seam alike (the chassis hands `connectOnDemand` the same `cloudUrl`).
120
- Selection never writes;
121
- `storeConnectKey` is the only thing that writes such a profile, on first
122
- contact, and makes it active only when the tool had no profile at all. It never
123
- renames or repoints an existing profile, and never changes which profile is
124
- active once the tool has one.
149
+ for an instance URL, or the implicit `cloud` profile) and its name is free
150
+ (`freshProfileName`). A profile with no `baseUrl` is at the cloud URL, for the
151
+ selection and for the connect seam alike (the chassis hands `connectOnDemand`
152
+ the same `cloudUrl`). Selection never writes; the first key `connectOnDemand`
153
+ keeps writes such a profile, with its site, and makes it the default only when
154
+ the tool had no profile at all. Nothing renames or repoints an existing
155
+ profile, and nothing changes which profile is the default once the tool has
156
+ one.
125
157
 
126
158
  An active profile pointing at an unreachable instance is still the selection —
127
159
  there is no silent fallback to `cloud`. The tool's error names the profile and
128
160
  says the hub login exists; that wording is the tool's. `default` is an ordinary
129
161
  profile name now, nothing pins it.
130
162
 
163
+ ### The sign-in follows the site
164
+
165
+ A command with no key of its own signs in with the Antasphere login at the hub
166
+ its SITE trusts, never with the machine's active hub login. `planSignIn` is
167
+ that one decision, shared by the command (`connectOnDemand`) and a listing:
168
+
169
+ 1. **A site that names its hub** (discovery `auth.hub`): that hub, and nothing
170
+ overrides it. `ANTASPHERE_URL`, or `--hub` (the `hubUrl` option), naming
171
+ another hub is refused before any exchange, both named.
172
+ 2. **A site that names none** (a server from before), or one never reached:
173
+ `--hub`, else `ANTASPHERE_URL`, else the hub of the profile's newest kept
174
+ key whose login is still in place, else the hub of its last sign-in
175
+ (`lastHub`) while a login is in place there, else an error naming the
176
+ machine's logins and the one command that picks one. Never a guess, even
177
+ with one login.
178
+ 3. **The login at that hub**: `--email` (the `email` option) when given, else
179
+ the only one, else the one whose address the profile's newest kept key there
180
+ carries, else an error naming them; none there, an error naming the hub.
181
+
182
+ A profile remembers the hub of its last sign-in: every exchange writes the
183
+ address of the hub it went through, in its normal form, on the profile
184
+ (`lastHub`, with the key it minted, whatever chose that hub), so a site that
185
+ names no hub finds its hub again after its kept key is gone (an account that
186
+ signed out, then in again).
187
+ For such a site the order is `--hub`, `ANTASPHERE_URL`, the newest kept key
188
+ whose login is in place, then `lastHub` when a login is in place at that hub
189
+ (the login there by rule 3), then the pick-a-hub sentence, which then reads
190
+ `… and profile "<p>" last signed in through <lastHub>, where this machine holds
191
+ no login now. …`. A site that names its hub ignores the field. An exchange that
192
+ the remembered hub refuses because it does not know the site ends on the hub's
193
+ error and `Profile "<p>" last signed in through <hub>; if <site> trusts another
194
+ hub now, run <bin> <profile> login --hub <hub>.` (`<profile>` as in the
195
+ sentences below).
196
+
197
+ A kept key is served when the profile is at the command's site, its hub is the
198
+ one rule 2 picks, and the login rule 3 picks carries its address (trimmed,
199
+ case-insensitive, both present). A kept key whose login is gone (no login at its
200
+ hub carrying its address, nor one carrying none: nobody signed in there, or only
201
+ other accounts) is stale, and so is one whose hub is not the one the site
202
+ names; an account that signs in again mints a new key. Stale keys are revoked
203
+ on the profile's own site
204
+ by the next command that resolves a credential on it, whatever that command
205
+ ends with (a key served, an error, an exchange), then forgotten; the file's lock
206
+ is never held across the network (decide, release, revoke, then forget what is
207
+ still there), and a site that does not answer keeps its key for the next
208
+ command: every revoke on a command's way is bounded (`requestTimeoutMs`, 10 s
209
+ when the tool gives none), and one the limit aborts is no answer, so a site
210
+ that holds the connection open never stops the command. A 5xx is no answer
211
+ either. A site that answers the key already unusable (a 401) or refuses to
212
+ revoke it (a 403) has it forgotten all the same, and `notify` says so:
213
+ `A stale key of profile "<p>" was already unusable on <site>; forgetting it.`,
214
+ or `<site> refused to revoke a stale key of profile "<p>" (<code>): it stays valid there; revoke it from that site. Forgetting it here.`
215
+ A stale key another
216
+ profile at that same site still holds is never
217
+ revoked there: it is forgotten on this profile only, and `notify` gets
218
+ `Kept on <site>: profile "<other>" still holds that key.`, once per profile
219
+ that holds one. A profile at another site never counts: it can never use the
220
+ key. A hub file that does not parse is an error naming it, never « no
221
+ login »: nothing is revoked then.
222
+
223
+ Unknown is not gone. A login is an entry of the hub file that holds a key and
224
+ whose hub can be told: its `baseUrl`, an `http:` or `https:` URL, else (no
225
+ `baseUrl`, or a blank one) the hub's cloud URL, `hubCloudUrl`, which
226
+ `connectOnDemand`, `refreshConnectKey` and `planSignIn` require (a `TypeError`
227
+ naming it when it is missing, blank or not such a URL). An entry that holds a
228
+ key and whose `baseUrl` does not parse as one is no login, and while one is
229
+ there no kept key is judged gone for want of a login: nothing is revoked on
230
+ that ground (a key of another hub than the one the site names, and a duplicate,
231
+ are still stale). `undecidedLogins` names those entries, for a listing to say
232
+ so. A login that carries no address is « cannot tell » too: it may be the
233
+ account of any key at its hub, so while one is there no key at its hub is
234
+ judged gone (a login there with another address, or no login there, still
235
+ makes a key gone). Such a login is never served a kept key: its exchange runs
236
+ and tells its address (the login takes it, `healed`), and from then on the
237
+ rules above settle the old key: the same account, the older of its two keys is
238
+ a stale duplicate; another account, the old key is gone. An old slot whose
239
+ login of that name carries no address stays where it is, neither moved nor
240
+ stale, until the address is known. A key that carries no address is matched by
241
+ no login.
242
+
243
+ What a profile remembers of its site (`rememberSite`) is read again on every
244
+ exchange, on `login` and on `profiles --check` (the chassis's calls), and
245
+ before a kept key is served only when that memory is missing or older than a
246
+ day: one GET, then the plan again; offline, the kept key serves. Otherwise a
247
+ kept key costs no request.
248
+
249
+ The mint is single-flight: commands that meet a profile with no kept key at
250
+ once take one lock per (profile, hub, address), beside the tool's file
251
+ (`<file>.mint-<id>.lock`, 60 s, a holder older than 120 s taken over, a waiter
252
+ past the limit proceeds alone), and the first to hold it mints while the
253
+ others find its key, a login with no address included: the first writes the
254
+ address its exchange told on that login before it lets the lock go, and each
255
+ other one reads the login again under the lock. At the store the first key
256
+ stored wins: a key another
257
+ command stored in the slot after this command read the file under the mint
258
+ lock is kept, and this command revokes its own key on the site and uses the
259
+ stored one; when the site does not answer that revoke, the key is filed
260
+ undated in its slot, a stale duplicate the next command revokes, and when it
261
+ refuses it (a 403), nothing is filed and `notify` gets the line below. A key the
262
+ profile already held at that read is not first stored (a login with no address
263
+ mints even where its account has a key): the new key is stored beside it, and
264
+ the older one is a stale duplicate the next command revokes on the site. A key
265
+ minted for a profile that cannot keep it is revoked
266
+ on its site, and when that revoke is not answered or refused, `notify` gets one line:
267
+ `A key minted on <site> could not be revoked there; revoke it from that site.`
268
+ After a 401, `refreshConnectKey` evicts the key only while it is still the one
269
+ refused, and signs in again for the refused key's account: the recovery
270
+ continues the command whose key was refused. The refused key's hub is never
271
+ taken as `--hub`. For a site that names no hub it is the recovery's first hint:
272
+ `--hub`, `ANTASPHERE_URL`, the refused key's hub while a login is in place
273
+ there, then the newest kept key in place, then `lastHub`, else the pick-a-hub
274
+ sentence. A site that names a hub decides it. The eviction also keeps that hub
275
+ as the profile's `lastHub` when the profile holds none, for the commands after
276
+ it, and an exchange that hub refuses because it does not know the site ends on
277
+ the way out, as at the remembered hub.
278
+
279
+ The sentences, each with the tool's binary (`bin`). Every command they name
280
+ selects its profile one way: `<profile>` is `--profile <p>` when that profile is
281
+ on disk at the site, else `--api-url <site>` (`--profile` names only a profile
282
+ on disk; `--api-url` selects the site's profile, or makes it):
283
+
284
+ - `<site> signs in through the Antasphere hub <H>, and ANTASPHERE_URL names <U>. Unset ANTASPHERE_URL: a site that names its hub decides it.`
285
+ - `<site> signs in through the Antasphere hub <H>, and --hub names <U>. Drop --hub: a site that names its hub decides it.`
286
+ - `<site> names no Antasphere hub (it runs a version from before sites named theirs), and profile "<p>" has never signed in there. This machine holds <n> Antasphere login(s): <email> at <hub>, …. Pick the hub this site trusts: <bin> <profile> login --hub <hub>.`
287
+ - `<site> signs in through the Antasphere hub <H>, and this machine holds no Antasphere login there. Run <bin> <profile> login to sign in.`
288
+ - `This machine holds <n> Antasphere logins at <H> (<e1>, <e2>, …), and profile "<p>" has used none of them. Pick one: <bin> <profile> login --email <address>.`
289
+ - `Profile "<p>" is the site <A>, and this command targets <B>.`
290
+ - `Profile "<p>" has no site of its own and holds keys of no known site, so this command cannot keep a key on it. Sign in on a profile of <site> instead (<bin> <profile> login).` (a profile with no `baseUrl` and no cloud URL to be at never takes a site while it holds a key: it is refused before any request, and a key minted while another writer gave it one is revoked on the site; `<profile>` names the profile of that site on disk, or the site when none is)
291
+
131
292
  An illustration of what a tool can print for the three profiles above (a
132
- `slideless profiles`-style listing, not a command this package provides):
293
+ `profiles`-style listing, not a command this package provides):
133
294
 
134
295
  ```
135
- PROFILE INSTANCE SIGNS IN WITH
136
- * cloud https://slideless.antasphere.com hub
137
- share.antasphere.com https://share.antasphere.com key
138
- localhost:3400 http://localhost:3400 key
296
+ PROFILE SITE SIGN-IN
297
+ * cloud https://slideless.example.com signed in: you@example.com at https://account.example.com
298
+ share.example.com https://share.example.com signed in with its own key
299
+ localhost:3400 http://localhost:3400 signed in with its own key
139
300
  ```
140
301
 
141
- The last column is `profileCredential`: `key` (the profile's own `apiKey`, the
142
- tool's login), else `hub` (a key the exchange minted), else `none`.
302
+ The last column is the plan's `state` (`signed_in`, `will_sign_in`,
303
+ `needs_login`, `unknown`) with the login it names. No listing prints any part
304
+ of a key.
143
305
 
144
306
  ## Surface
145
307
 
146
- - `configPath / loadConfig / saveConfig / clearConfig (env, tool)` — the
147
- tool-namespaced store. `HUB_TOOL` names the hub namespace. `redactKey`
148
- renders keys safely for terminals. `parseConfig(raw)` is the tolerant parse
149
- (corrupt or foreign shapes → empty config); `assertToolNamespace(tool)`
150
- refuses anything but a lowercase slug.
308
+ - `configPath / loadConfig / clearConfig (env, tool)` and
309
+ `updateConfig(env, tool, mutate)` — the tool-namespaced store. `loadConfig`
310
+ is strict: a file that does not parse throws `CliConfigError` (a
311
+ `CliUsageError`, exit code 1) naming it. `updateConfig` is the one locked
312
+ read-modify-write, and every write goes through it: `mutate` changes the
313
+ draft it is given or returns the config to write, and the answer is
314
+ `{ path, changed, config }`; a config the strict loader would refuse is
315
+ never written (`CliConfigError`, nothing written). `HUB_TOOL` names the hub
316
+ namespace.
317
+ `parseConfig(raw)` is the tolerant parse (corrupt or foreign shapes → empty
318
+ config), for a foreign file such as a legacy one; `assertToolNamespace(tool)`
319
+ refuses anything but a lowercase slug. `redactKey` is kept, deprecated: no
320
+ listing prints any part of a key.
321
+ - `withFileLockAsync(lockPath, fn, { timeoutMs?, staleMs?, emptyStaleMs? })` —
322
+ the config files' lock protocol for work that spans the network (the
323
+ single-flight mint): the waits are awaited, the lock is released by token
324
+ when `fn` settles, and it is not re-entrant.
151
325
  - `resolveProfile(config, requested, { unknownHint })` — the named (or active)
152
326
  profile; an explicitly named missing profile throws `CliUsageError`.
153
327
  - `selectProfile({ config, requested, apiUrl, cloudUrl, unknownHint })` — the
154
328
  selection rule of [Profiles](#profiles); returns
155
329
  `{ name, profile, source, created }`, `source` being `flag`, `url`, `active`
156
330
  or `cloud`. Nothing is written, no profile is repointed.
331
+ `freshProfileName(config, url, cloudUrl?)` is the free name it gives a new
332
+ profile (`cloud`, else the host, then `<host>-2`, …), and the name a new
333
+ login of the hub file takes.
157
334
  - `CLOUD_PROFILE` — `'cloud'`, the implicit profile at the tool's cloud URL.
158
335
  - `normalizeUrl(url) / sameInstance(a, b) / profileNameFor(url, cloudUrl)` —
159
336
  the comparable form of an instance URL (scheme, host with port, path; no
@@ -162,8 +339,8 @@ tool's login), else `hub` (a key the exchange minted), else `none`.
162
339
  `profileUrl(profile, cloudUrl)` is the instance a profile is at (its
163
340
  `baseUrl`, a blank one counting as none, else the cloud URL);
164
341
  `firstSet(...values)` the first non-blank value, trimmed — the one rule
165
- every resolver applies to flags, env vars and profile fields, the hub
166
- profile's `apiKey` and `baseUrl` included.
342
+ every resolver applies to flags, env vars and profile fields, a login's
343
+ `apiKey` and `baseUrl` included.
167
344
  - `resolveBaseUrl({ flag, env, envVar, profile, cloudUrl, missingMessage })` —
168
345
  flag → tool env var → profile → cloud URL, trailing slashes stripped; a
169
346
  blank value counts as unset (`SLIDELESS_URL=""` never hides the cloud URL);
@@ -200,50 +377,110 @@ expiresInDays?, workspaceId? })` (the full key appears exactly once),
200
377
  pre-`sso:exchange` key), `needsReExchange` (tool 401 on an expired/replayed
201
378
  JWT), `refusal` (a 403 that is not the scope case, from either side — the
202
379
  account was refused, not the key) and `exitCode` (3 for a refusal, else 1).
203
- - `lookupConnectKey / storeConnectKey / removeConnectKey / profileCredential` —
204
- the cache of exchange-minted user-scoped keys, held on the profile of the
205
- instance they were minted for, in its `connectKeys` map (slot = the hub
206
- profile name; `activeHubProfile(env)` resolves it). ONE key per hub account
207
- serves every org — the org is never part of the credential or the slot.
208
- `storeConnectKey(env, tool, profileName, hubProfileName, entry, { baseUrl })`
209
- writes `baseUrl` only on a profile that has none, a blank one counting as
210
- none (a profile is never repointed), creates a profile not on disk yet, and makes it active only when
211
- the tool had no profile at all. `removeConnectKey` never touches the tool's
212
- own `apiKey`. `profileCredential(profile, hubProfileName?)` answers `key` /
213
- `hub` / `none`.
214
- - `connectOnDemand({ tool, toolBaseUrl, cloudUrl?, profileName, env, resource?,
215
- fetch?, notify?, messages?, cached? })` — the seam a tool CLI runs when NO
216
- direct credential resolved. `profileName` is REQUIRED: the profile
217
- `selectProfile` chose; `cloudUrl` is what a profile with no `baseUrl` is at,
218
- the same value the selection got. The cache is read first, on that profile
219
- only when it is at this instance (a hit costs zero network); `cached: false`
220
- skips it. On a miss the
221
- anonymous `GET /api/v1/instance` probe gates the flow (a self-hosted or
222
- unreachable instance answers `{ outcome: 'not_cloud' }` and the tool raises
223
- its classic no-key error), the hub credential is read from the `HUB_TOOL`
224
- profile, `exchange` runs with one re-exchange retry on a dead JWT (a missing
225
- or rejected hub login → a guided `antasphere login` `CliUsageError`, on
226
- either attempt; a refusal propagates as its `CliConnectError`), and the key
227
- is stored on the selected profile — never on one that is at another
228
- instance. Success:
229
- `{ outcome: 'connected', key, cached, result?, stored? }` — `cached: true` =
230
- served from the cache. `notify` receives the "Connected to …" notice on fresh
231
- exchanges (cache hits are silent); `messages` overrides the copy.
232
- `probeToolInstance(toolBaseUrl, fetch?)` is the discovery probe on its own;
233
- `activeHubProfile(env)` is the active hub profile, whose name is the cache
234
- slot.
235
- - `refreshConnectKey(opts)` — the stranded-key recovery, same options as
236
- `connectOnDemand`. When a request answers 401 with a key served
237
- `cached: true`, the chassis calls it once: it evicts that key (only from a
238
- profile at this instance; a key of another instance is never touched) and
239
- exchanges again with the cache skipped. The chassis retries its request once
240
- with the new key, and a second 401 fails; `not_cloud` means nothing to
241
- recover with.
380
+ - `listLogins(env, { hubCloudUrl? })` — the machine's logins,
381
+ `CliLogin { name, hub, email?, profile }`: every entry of the hub file that
382
+ holds a key and whose hub can be told, its hub its `baseUrl` (an `http:` or
383
+ `https:` URL; none, or a blank one: `hubCloudUrl`) normalized, in the file's
384
+ order. `undecidedLogins(env, { hubCloudUrl })` names the other entries that
385
+ hold a key (a `baseUrl` that does not parse as such a URL), in the file's
386
+ order: while there is one, no kept key is judged gone for want of a login.
387
+ Its `hubCloudUrl` is required, as `planSignIn`'s.
388
+ - `chooseHub({ siteUrl, siteHub, profile?, logins, hubUrl?, envHub?, profileName, bin })`
389
+ — rule R2 of [The sign-in follows the site](#the-sign-in-follows-the-site):
390
+ `{ kind: 'hub', hub, source }` (`site`, `option`, `env`, `kept_key`, `remembered`),
391
+ `{ kind: 'refused', sentence }`, or `{ kind: 'pick_hub', sentence, candidates }`.
392
+ `chooseLogin({ hub, siteUrl, profile?, logins, email?, profileName, bin })` —
393
+ rule 3: `{ kind: 'login', login }`, `{ kind: 'no_login_at_hub', sentence }`
394
+ or `{ kind: 'pick_login', sentence, candidates }`. For both, `profile` is the
395
+ profile on disk at the site: given, the sentences name it by `--profile`;
396
+ omitted (not on disk, or at another site), they name `--api-url <site>`.
397
+ - `planSignIn({ env, tool, profileName, profile, siteUrl, cloudUrl?, hubCloudUrl, envHub?, hubUrl?, email? })`
398
+ — the plan a command and a listing share, from the two files alone (it
399
+ writes nothing and asks nothing of the network; `envHub` is the only door
400
+ for `ANTASPHERE_URL`, which a listing leaves out). `hubCloudUrl` is
401
+ required: a `TypeError` naming it when it is missing, blank or not an
402
+ `http:` or `https:` URL. `SignInPlan`:
403
+ `state` (`signed_in`, `will_sign_in`, `needs_login`, `unknown`), `via`
404
+ (`key`, `login` or null), `hub`, `login`, `keptKey` (the servable one),
405
+ `reason` (`own_key`, `kept_key`, `login_ready`, `no_login_at_hub`,
406
+ `pick_hub`, `pick_login`, `own_sign_in`, `never_reached`, `refused`),
407
+ `refusal` (its sentence), `candidates`, and `stale` (what the next command
408
+ on the profile revokes on its site, old slots included with hub `''`).
409
+ - `probeToolInstance(toolBaseUrl, fetch?)` — the anonymous discovery probe:
410
+ `{ url, cloud, edition, authMethods, hub, name, cliVersion }` (`hub` is
411
+ `auth.hub` normalized, or null), or null when the site is unreachable or
412
+ foreign. `rememberSite(env, tool, profileName, probe, { force?, cloudUrl? })`
413
+ writes those facts on the profile `site`, only for a profile on disk at that
414
+ site, and only when they changed, when what it holds is more than a day old,
415
+ or with `force` (`profiles --check`); it answers whether the file changed.
416
+ - `keysOfProfile(profile)` — every key a profile holds, each once:
417
+ `{ apiKey, kind: 'own' | 'kept' | 'old', hub?, email? }`, the list a logout
418
+ or a removal revokes on the profile's own site (never printed).
419
+ `forgetKeys(env, tool, profileName, apiKeys)` removes those keys,
420
+ compare-and-remove under the lock (the own key too when listed), and
421
+ answers how many entries went.
422
+ `keysHeldElsewhere(config, profileName, site, cloudUrl?)` maps each key
423
+ another profile at the same site holds (trimmed) to the first such profile's
424
+ name in the file's order: the keys a revoke on that site leaves alone,
425
+ forgotten on this profile only. A profile at another site, or with no site,
426
+ never counts.
427
+ - `connectOnDemand({ tool, toolBaseUrl, cloudUrl?, hubCloudUrl, hubUrl?, email?, bin?,
428
+ profileName, env, resource?, fetch?, notify?, messages?, cached?, requestTimeoutMs? })` — the seam a
429
+ tool CLI runs when NO direct credential resolved, the sign-in of
430
+ [The sign-in follows the site](#the-sign-in-follows-the-site). `profileName`
431
+ is REQUIRED: the profile `selectProfile` chose; one at another site is
432
+ refused before any request. `hubCloudUrl` is REQUIRED too, here and in
433
+ `refreshConnectKey`: a `TypeError` naming it, before any read, request or
434
+ write, when it is missing, blank or not an `http:` or `https:` URL. The old
435
+ slots are moved once; a servable kept
436
+ key is served (`cached: true`, zero network but the daily discovery read and
437
+ the revoke of the profile's stale keys);
438
+ otherwise the anonymous probe gates the flow (a self-hosted or unreachable
439
+ site answers `{ outcome: 'not_cloud' }` and the tool raises its classic
440
+ no-key error), the plan is made again on what the site said, the stale keys
441
+ are revoked on the site, and the single-flight exchange runs at the chosen
442
+ login's own hub with one re-exchange retry on a dead JWT (a rejected hub key
443
+ → `messages.hubLoginRejected`, on either attempt; a refusal propagates as
444
+ its `CliConnectError`). Success:
445
+ `{ outcome: 'connected', key, cached, login: { name, hub, email }, result?, stored?, healed? }`;
446
+ `healed: { from, to }` says the login's address was replaced by the hub's
447
+ answer. `notify` receives the "Connected to …" notice on fresh exchanges
448
+ (kept keys are silent), the line that says a key minted on the site
449
+ could not be revoked there, the line that says a stale key was left on
450
+ the site for another profile that holds it, and the lines that say a stale
451
+ key was already unusable on its site or that its site refused to revoke it
452
+ (both forgotten); `messages` overrides
453
+ `hubLoginRejected` and `connected`. `cached: false` skips serving before discovery; a key the file
454
+ keeps for the slot is still used (`forgetKeys` it first to mint).
455
+ `requestTimeoutMs` is the limit of each revoke it sends on the command's way
456
+ (10 s when absent; a value that is not an integer from 1 to 2 147 483 647,
457
+ 2^31 − 1, reads as absent: a Node timer refuses a fraction and cuts a longer
458
+ delay to 1 ms), the same in `refreshConnectKey`.
459
+ `HUB_URL_ENV` is `ANTASPHERE_URL`.
460
+ - `refreshConnectKey({ ...opts, refusedKey })` — the stranded-key recovery.
461
+ When a request answers 401 with a key served `cached: true`, the chassis
462
+ calls it once with that key: compare-and-evict (only on a profile at this
463
+ site), then the kept key the plan serves when another command already
464
+ replaced the refused one (no exchange), else the exchange again. The
465
+ refused key's address, read before the eviction, acts as `email` when the
466
+ caller passes none. Its hub is a hint, never `hubUrl`: for a site that names
467
+ no hub, it comes right after `ANTASPHERE_URL` while a login is in place
468
+ there, before the newest kept key in place and `lastHub`; a site that names
469
+ a hub decides it. The eviction's own write also gives it to the profile as
470
+ its `lastHub` when the profile holds none (a profile from before the field).
471
+ The chassis retries its request once with the new key, and a second 401
472
+ fails; `not_cloud` means nothing to recover with.
242
473
  - `migrateLegacyConfig(env, { tool, legacyDir })` — one-shot, non-destructive
243
474
  import of a tool's pre-cli-core `$XDG_CONFIG_HOME/<legacyDir>/config.json`
244
475
  into the shared home. Imports only when the tool has no profiles in the
245
- shared home yet and the legacy file holds at least one; the legacy file is
246
- left in place.
476
+ shared home yet and the legacy file holds at least one that is an object
477
+ (only those come across); the legacy file is left in place.
478
+
479
+ Gone since 0.6 (the consumers roll together): `saveConfig` (every write is
480
+ `updateConfig`), `selectHubProfile` and `activeHubProfile` (the machine's
481
+ active login plays no part), and the name-slotted cache `lookupConnectKey`,
482
+ `cachedConnectKey`, `storeConnectKey`, `removeConnectKey`, `profileCredential`
483
+ (the kept keys and `planSignIn`).
247
484
 
248
485
  ## Exit codes
249
486
 
@@ -256,7 +493,7 @@ Runners read `exitCode` off `CliUsageError` and `CliConnectError`.
256
493
  ## Consuming it
257
494
 
258
495
  ```json
259
- "dependencies": { "@antasphere/cli-core": "^0.5.0" }
496
+ "dependencies": { "@antasphere/cli-core": "^0.7.0" }
260
497
  ```
261
498
 
262
499
  Registry tarballs ship a prebuilt `dist/`, no build script runs on install.
@@ -270,7 +507,7 @@ with no npm token — npm authenticates the workflow's OIDC identity directly.
270
507
 
271
508
  ```bash
272
509
  # bump package.json#version, commit, then:
273
- git tag v0.5.0 && git push origin v0.5.0
510
+ git tag v0.7.0 && git push origin v0.7.0
274
511
  ```
275
512
 
276
513
  The first publish was done by hand, and the trusted publisher is configured on
@@ -290,6 +527,8 @@ pnpm install
290
527
  pnpm lint && pnpm typecheck && pnpm test && pnpm build
291
528
  ```
292
529
 
530
+ `pnpm test` takes several minutes: the two fifty-round tests of `test/concurrency.test.ts` (twenty writer processes on one profiles file, each round) run for minutes each, with a 900 s limit apiece.
531
+
293
532
  Branches per the workspace rule: `prod` (default, releases/tags) + `dev`
294
533
  (day-to-day work). License: MIT (permissive on purpose — this is plumbing
295
534
  consumed by public CLIs).
package/dist/auth.d.ts CHANGED
@@ -61,7 +61,7 @@ export interface CliAuthRevoked {
61
61
  id: string;
62
62
  }
63
63
  export interface CliAuthClientOptions {
64
- /** Instance origin, e.g. https://account.antasphere.com */
64
+ /** Instance origin, e.g. https://account.example.com */
65
65
  baseUrl: string;
66
66
  fet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