@cotal-ai/cli 0.191.0 → 0.193.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/dist/command.d.ts.map +1 -1
- package/dist/command.js +51 -8
- package/dist/command.js.map +1 -1
- package/dist/commands/agents.d.ts.map +1 -1
- package/dist/commands/agents.js +38 -23
- package/dist/commands/agents.js.map +1 -1
- package/dist/commands/app-identity.d.ts +6 -1
- package/dist/commands/app-identity.d.ts.map +1 -1
- package/dist/commands/app-identity.js +30 -9
- package/dist/commands/app-identity.js.map +1 -1
- package/dist/commands/channel-members.js +1 -1
- package/dist/commands/channel-members.js.map +1 -1
- package/dist/commands/channels.d.ts.map +1 -1
- package/dist/commands/channels.js +7 -2
- package/dist/commands/channels.js.map +1 -1
- package/dist/commands/chat.d.ts.map +1 -1
- package/dist/commands/chat.js +20 -6
- package/dist/commands/chat.js.map +1 -1
- package/dist/commands/clean.d.ts +2 -2
- package/dist/commands/clean.d.ts.map +1 -1
- package/dist/commands/clean.js +3 -2
- package/dist/commands/clean.js.map +1 -1
- package/dist/commands/completion.d.ts.map +1 -1
- package/dist/commands/completion.js +11 -17
- package/dist/commands/completion.js.map +1 -1
- package/dist/commands/console.js +2 -2
- package/dist/commands/console.js.map +1 -1
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +17 -8
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/down-manifest.js +1 -1
- package/dist/commands/down-manifest.js.map +1 -1
- package/dist/commands/down-preserve.js +4 -4
- package/dist/commands/down-preserve.js.map +1 -1
- package/dist/commands/down.d.ts.map +1 -1
- package/dist/commands/down.js +26 -13
- package/dist/commands/down.js.map +1 -1
- package/dist/commands/endpoint.d.ts.map +1 -1
- package/dist/commands/endpoint.js +3 -2
- package/dist/commands/endpoint.js.map +1 -1
- package/dist/commands/ext.d.ts +2 -0
- package/dist/commands/ext.d.ts.map +1 -1
- package/dist/commands/ext.js +211 -41
- package/dist/commands/ext.js.map +1 -1
- package/dist/commands/help.js +12 -11
- package/dist/commands/help.js.map +1 -1
- package/dist/commands/history-clear.d.ts.map +1 -1
- package/dist/commands/history-clear.js +6 -2
- package/dist/commands/history-clear.js.map +1 -1
- package/dist/commands/history.d.ts.map +1 -1
- package/dist/commands/history.js +17 -10
- package/dist/commands/history.js.map +1 -1
- package/dist/commands/join.d.ts.map +1 -1
- package/dist/commands/join.js +3 -8
- package/dist/commands/join.js.map +1 -1
- package/dist/commands/meshes.d.ts.map +1 -1
- package/dist/commands/meshes.js +2 -1
- package/dist/commands/meshes.js.map +1 -1
- package/dist/commands/send.d.ts.map +1 -1
- package/dist/commands/send.js +68 -38
- package/dist/commands/send.js.map +1 -1
- package/dist/commands/service-actions.d.ts.map +1 -1
- package/dist/commands/service-actions.js +14 -4
- package/dist/commands/service-actions.js.map +1 -1
- package/dist/commands/service-registry.d.ts.map +1 -1
- package/dist/commands/service-registry.js +6 -8
- package/dist/commands/service-registry.js.map +1 -1
- package/dist/commands/service-unit-entry.d.ts.map +1 -1
- package/dist/commands/service-unit-entry.js +8 -6
- package/dist/commands/service-unit-entry.js.map +1 -1
- package/dist/commands/setup.js +1 -1
- package/dist/commands/setup.js.map +1 -1
- package/dist/commands/space-registration.d.ts +2 -0
- package/dist/commands/space-registration.d.ts.map +1 -1
- package/dist/commands/space-registration.js +5 -0
- package/dist/commands/space-registration.js.map +1 -1
- package/dist/commands/space.d.ts.map +1 -1
- package/dist/commands/space.js +3 -3
- package/dist/commands/space.js.map +1 -1
- package/dist/commands/spawn-flags.d.ts +5 -1
- package/dist/commands/spawn-flags.d.ts.map +1 -1
- package/dist/commands/spawn-flags.js +4 -0
- package/dist/commands/spawn-flags.js.map +1 -1
- package/dist/commands/spawn-foreground.d.ts +1 -0
- package/dist/commands/spawn-foreground.d.ts.map +1 -1
- package/dist/commands/spawn-foreground.js +1 -1
- package/dist/commands/spawn-foreground.js.map +1 -1
- package/dist/commands/spawn-preview.d.ts.map +1 -1
- package/dist/commands/spawn-preview.js +16 -10
- package/dist/commands/spawn-preview.js.map +1 -1
- package/dist/commands/spawn.d.ts.map +1 -1
- package/dist/commands/spawn.js +10 -8
- package/dist/commands/spawn.js.map +1 -1
- package/dist/commands/stack-down.d.ts.map +1 -1
- package/dist/commands/stack-down.js +3 -1
- package/dist/commands/stack-down.js.map +1 -1
- package/dist/commands/stack-up.d.ts +1 -0
- package/dist/commands/stack-up.d.ts.map +1 -1
- package/dist/commands/stack-up.js +8 -4
- package/dist/commands/stack-up.js.map +1 -1
- package/dist/commands/stack-user-auth.d.ts.map +1 -1
- package/dist/commands/stack-user-auth.js +2 -1
- package/dist/commands/stack-user-auth.js.map +1 -1
- package/dist/commands/status-delivery.d.ts +6 -1
- package/dist/commands/status-delivery.d.ts.map +1 -1
- package/dist/commands/status-delivery.js +3 -1
- package/dist/commands/status-delivery.js.map +1 -1
- package/dist/commands/status-health.d.ts +7 -2
- package/dist/commands/status-health.d.ts.map +1 -1
- package/dist/commands/status-health.js +29 -19
- package/dist/commands/status-health.js.map +1 -1
- package/dist/commands/status-legacy.d.ts +7 -0
- package/dist/commands/status-legacy.d.ts.map +1 -1
- package/dist/commands/status-legacy.js +9 -3
- package/dist/commands/status-legacy.js.map +1 -1
- package/dist/commands/status-meshes.d.ts.map +1 -1
- package/dist/commands/status-meshes.js +19 -13
- package/dist/commands/status-meshes.js.map +1 -1
- package/dist/commands/status-render.d.ts.map +1 -1
- package/dist/commands/status-render.js +31 -11
- package/dist/commands/status-render.js.map +1 -1
- package/dist/commands/status.d.ts.map +1 -1
- package/dist/commands/status.js +51 -31
- package/dist/commands/status.js.map +1 -1
- package/dist/commands/up-args.d.ts +1 -0
- package/dist/commands/up-args.d.ts.map +1 -1
- package/dist/commands/up-args.js +28 -3
- package/dist/commands/up-args.js.map +1 -1
- package/dist/commands/up-detach.d.ts +3 -0
- package/dist/commands/up-detach.d.ts.map +1 -1
- package/dist/commands/up-detach.js +2 -0
- package/dist/commands/up-detach.js.map +1 -1
- package/dist/commands/up-foreground-listener.d.ts +2 -0
- package/dist/commands/up-foreground-listener.d.ts.map +1 -1
- package/dist/commands/up-foreground-listener.js +7 -2
- package/dist/commands/up-foreground-listener.js.map +1 -1
- package/dist/commands/up.d.ts.map +1 -1
- package/dist/commands/up.js +15 -15
- package/dist/commands/up.js.map +1 -1
- package/dist/commands/update-offline.d.ts +2 -0
- package/dist/commands/update-offline.d.ts.map +1 -1
- package/dist/commands/update-offline.js +119 -32
- package/dist/commands/update-offline.js.map +1 -1
- package/dist/commands/update-release.d.ts +3 -1
- package/dist/commands/update-release.d.ts.map +1 -1
- package/dist/commands/update-release.js +63 -23
- package/dist/commands/update-release.js.map +1 -1
- package/dist/commands/update.d.ts +1 -0
- package/dist/commands/update.d.ts.map +1 -1
- package/dist/commands/update.js +22 -17
- package/dist/commands/update.js.map +1 -1
- package/dist/commands/watch.d.ts.map +1 -1
- package/dist/commands/watch.js +11 -4
- package/dist/commands/watch.js.map +1 -1
- package/dist/commands/who.d.ts.map +1 -1
- package/dist/commands/who.js +2 -0
- package/dist/commands/who.js.map +1 -1
- package/dist/commands/whoami.d.ts.map +1 -1
- package/dist/commands/whoami.js +5 -1
- package/dist/commands/whoami.js.map +1 -1
- package/dist/console/root.d.ts +4 -2
- package/dist/console/root.d.ts.map +1 -1
- package/dist/console/root.js +6 -5
- package/dist/console/root.js.map +1 -1
- package/dist/console/tool-calls.d.ts +7 -1
- package/dist/console/tool-calls.d.ts.map +1 -1
- package/dist/console/tool-calls.js +29 -34
- package/dist/console/tool-calls.js.map +1 -1
- package/dist/console/ui/SpacePicker.d.ts +3 -1
- package/dist/console/ui/SpacePicker.d.ts.map +1 -1
- package/dist/console/ui/SpacePicker.js +4 -4
- package/dist/console/ui/SpacePicker.js.map +1 -1
- package/dist/ext-loader.d.ts.map +1 -1
- package/dist/ext-loader.js +3 -1
- package/dist/ext-loader.js.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/app-user-identity.d.ts +8 -2
- package/dist/lib/app-user-identity.d.ts.map +1 -1
- package/dist/lib/app-user-identity.js +359 -111
- package/dist/lib/app-user-identity.js.map +1 -1
- package/dist/lib/broker-service.d.ts.map +1 -1
- package/dist/lib/broker-service.js +3 -0
- package/dist/lib/broker-service.js.map +1 -1
- package/dist/lib/broker-store-health.d.ts +1 -1
- package/dist/lib/broker-store-health.d.ts.map +1 -1
- package/dist/lib/broker-store-health.js +12 -6
- package/dist/lib/broker-store-health.js.map +1 -1
- package/dist/lib/choose-target.d.ts.map +1 -1
- package/dist/lib/choose-target.js +8 -4
- package/dist/lib/choose-target.js.map +1 -1
- package/dist/lib/completion-scripts.d.ts.map +1 -1
- package/dist/lib/completion-scripts.js +1 -1
- package/dist/lib/completion-scripts.js.map +1 -1
- package/dist/lib/control.d.ts +1 -3
- package/dist/lib/control.d.ts.map +1 -1
- package/dist/lib/control.js +6 -7
- package/dist/lib/control.js.map +1 -1
- package/dist/lib/controller-preserve.js +2 -2
- package/dist/lib/controller-preserve.js.map +1 -1
- package/dist/lib/controller-restore.d.ts.map +1 -1
- package/dist/lib/controller-restore.js +17 -7
- package/dist/lib/controller-restore.js.map +1 -1
- package/dist/lib/conversation-identity.d.ts +3 -3
- package/dist/lib/conversation-identity.d.ts.map +1 -1
- package/dist/lib/conversation-identity.js +8 -11
- package/dist/lib/conversation-identity.js.map +1 -1
- package/dist/lib/delivery-proc.d.ts +1 -0
- package/dist/lib/delivery-proc.d.ts.map +1 -1
- package/dist/lib/endpoint-inventory.d.ts.map +1 -1
- package/dist/lib/endpoint-inventory.js +11 -15
- package/dist/lib/endpoint-inventory.js.map +1 -1
- package/dist/lib/extension-remove-cache.d.ts +5 -0
- package/dist/lib/extension-remove-cache.d.ts.map +1 -0
- package/dist/lib/extension-remove-cache.js +90 -0
- package/dist/lib/extension-remove-cache.js.map +1 -0
- package/dist/lib/join-message.d.ts +4 -0
- package/dist/lib/join-message.d.ts.map +1 -0
- package/dist/lib/join-message.js +13 -0
- package/dist/lib/join-message.js.map +1 -0
- package/dist/lib/launch-modes.d.ts.map +1 -1
- package/dist/lib/launch-modes.js +8 -1
- package/dist/lib/launch-modes.js.map +1 -1
- package/dist/lib/live-names.d.ts +2 -1
- package/dist/lib/live-names.d.ts.map +1 -1
- package/dist/lib/live-names.js +3 -2
- package/dist/lib/live-names.js.map +1 -1
- package/dist/lib/manager-proc.d.ts +1 -0
- package/dist/lib/manager-proc.d.ts.map +1 -1
- package/dist/lib/manager-proc.js +1 -0
- package/dist/lib/manager-proc.js.map +1 -1
- package/dist/lib/persona-files.d.ts +1 -1
- package/dist/lib/persona-files.d.ts.map +1 -1
- package/dist/lib/persona-files.js +5 -2
- package/dist/lib/persona-files.js.map +1 -1
- package/dist/lib/release-archive.d.ts +13 -0
- package/dist/lib/release-archive.d.ts.map +1 -0
- package/dist/lib/release-archive.js +39 -0
- package/dist/lib/release-archive.js.map +1 -0
- package/dist/lib/seat-restore.d.ts.map +1 -1
- package/dist/lib/seat-restore.js +15 -19
- package/dist/lib/seat-restore.js.map +1 -1
- package/dist/lib/seat-resume.d.ts.map +1 -1
- package/dist/lib/seat-resume.js +9 -2
- package/dist/lib/seat-resume.js.map +1 -1
- package/dist/lib/self-exec.d.ts +4 -0
- package/dist/lib/self-exec.d.ts.map +1 -1
- package/dist/lib/self-exec.js +5 -0
- package/dist/lib/self-exec.js.map +1 -1
- package/dist/lib/service-census.d.ts +2 -2
- package/dist/lib/service-definitions.d.ts.map +1 -1
- package/dist/lib/service-definitions.js +3 -1
- package/dist/lib/service-definitions.js.map +1 -1
- package/dist/lib/service-private-seed.d.ts.map +1 -1
- package/dist/lib/service-private-seed.js +11 -3
- package/dist/lib/service-private-seed.js.map +1 -1
- package/dist/lib/service-process.d.ts +2 -0
- package/dist/lib/service-process.d.ts.map +1 -1
- package/dist/lib/service-process.js +5 -5
- package/dist/lib/service-process.js.map +1 -1
- package/dist/lib/service-systemd-control.d.ts +1 -1
- package/dist/lib/service-systemd-control.d.ts.map +1 -1
- package/dist/lib/service-systemd-control.js +92 -16
- package/dist/lib/service-systemd-control.js.map +1 -1
- package/dist/lib/service-systemd-reseed.d.ts.map +1 -1
- package/dist/lib/service-systemd-reseed.js +4 -3
- package/dist/lib/service-systemd-reseed.js.map +1 -1
- package/dist/lib/service-systemd-uninstall-plan.d.ts +23 -0
- package/dist/lib/service-systemd-uninstall-plan.d.ts.map +1 -1
- package/dist/lib/service-systemd-uninstall-plan.js +7 -5
- package/dist/lib/service-systemd-uninstall-plan.js.map +1 -1
- package/dist/lib/service-systemd-uninstall.d.ts.map +1 -1
- package/dist/lib/service-systemd-uninstall.js +12 -4
- package/dist/lib/service-systemd-uninstall.js.map +1 -1
- package/dist/lib/service-systemd-unit.d.ts.map +1 -1
- package/dist/lib/service-systemd-unit.js +18 -8
- package/dist/lib/service-systemd-unit.js.map +1 -1
- package/dist/lib/service-systemd.d.ts +17 -3
- package/dist/lib/service-systemd.d.ts.map +1 -1
- package/dist/lib/service-systemd.js +39 -6
- package/dist/lib/service-systemd.js.map +1 -1
- package/dist/lib/systemd-supervision.d.ts +12 -5
- package/dist/lib/systemd-supervision.d.ts.map +1 -1
- package/dist/lib/systemd-supervision.js +9 -6
- package/dist/lib/systemd-supervision.js.map +1 -1
- package/dist/lib/team-agent-removal.d.ts +1 -1
- package/dist/lib/terminal-text.d.ts +3 -0
- package/dist/lib/terminal-text.d.ts.map +1 -0
- package/dist/lib/terminal-text.js +6 -0
- package/dist/lib/terminal-text.js.map +1 -0
- package/dist/lib/update-managers.d.ts +1 -1
- package/dist/lib/update-managers.d.ts.map +1 -1
- package/dist/lib/update-managers.js +4 -2
- package/dist/lib/update-managers.js.map +1 -1
- package/dist/lib/update-pending.d.ts.map +1 -1
- package/dist/lib/update-pending.js +3 -0
- package/dist/lib/update-pending.js.map +1 -1
- package/dist/lib/update-recovery-state.d.ts +1 -1
- package/dist/lib/update-recovery-state.d.ts.map +1 -1
- package/dist/lib/update-recovery-state.js +8 -2
- package/dist/lib/update-recovery-state.js.map +1 -1
- package/dist/lib/version.d.ts +6 -0
- package/dist/lib/version.d.ts.map +1 -1
- package/dist/lib/version.js +17 -8
- package/dist/lib/version.js.map +1 -1
- package/dist/seed/manager-guard.d.ts +2 -0
- package/dist/seed/manager-guard.d.ts.map +1 -1
- package/dist/seed/manager-guard.js +6 -2
- package/dist/seed/manager-guard.js.map +1 -1
- package/dist/seed/paths.d.ts +2 -1
- package/dist/seed/paths.d.ts.map +1 -1
- package/dist/seed/paths.js +28 -4
- package/dist/seed/paths.js.map +1 -1
- package/dist/seed/reconcile.d.ts +4 -1
- package/dist/seed/reconcile.d.ts.map +1 -1
- package/dist/seed/reconcile.js +176 -32
- package/dist/seed/reconcile.js.map +1 -1
- package/dist/seed/store.d.ts +1 -0
- package/dist/seed/store.d.ts.map +1 -1
- package/dist/seed/store.js +1 -1
- package/dist/seed/store.js.map +1 -1
- package/dist/seed/verify.d.ts +5 -1
- package/dist/seed/verify.d.ts.map +1 -1
- package/dist/seed/verify.js +25 -5
- package/dist/seed/verify.js.map +1 -1
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +14 -4
- package/dist/telemetry.js.map +1 -1
- package/package.json +7 -5
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { createHash, randomBytes } from "node:crypto";
|
|
2
2
|
import { closeSync, existsSync, fsyncSync, lstatSync, openSync, readFileSync, readdirSync, renameSync, rmdirSync, unlinkSync } from "node:fs";
|
|
3
3
|
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { setTimeout as wait } from "node:timers/promises";
|
|
5
|
+
import { acquireLock, assertLifecycleToken, canonicalJson, checkConversationApprovalDecision, checkConversationApprovalPending, checkConversationPendingAnswer, checkConversationSelection, conversationApprovalRefusal, conversationGrant, deprovisionAgent, isReachable, mintCreds, mkSecretDir, newIdentity, principalKey, provisionAgentDurables, processStartToken, registry, resolveAuthProvider, writeSecretFileAtomic, } from "@cotal-ai/core";
|
|
6
|
+
import { checkDiscoveryDocument, checkIdpUrl, codeSpan, discoveryDocumentUrl, pinnedFetch, getSpaceAuth, homeCotalDir, loginRecoveryCommand, oneLine, resolveTargetOrThrow, userAuthStateDir, windowsShellWord, workspaceSecretStore } from "@cotal-ai/workspace";
|
|
6
7
|
import { CONVERSATION_RESUME_PREFIX } from "@cotal-ai/workspace/conversation-resume";
|
|
7
8
|
import { agentCapRefusal, checkConversationMaterial, conversationRetirementPhase, discoverConversationAuthority, legacyAgentCapRefusal, legacyNameTakenRefusal, nameTakenRefusal, selectionRefusal, signedInSpace, } from "./conversation-identity.js";
|
|
8
9
|
import { withProvisioner } from "./provisioner.js";
|
|
10
|
+
import { shellWord } from "./endpoint-errors.js";
|
|
9
11
|
import { confirmConversationResume, ConversationRequestError, createResumeProof, matchesResumeBinding, resumeToken } from "./conversation-resume.js";
|
|
10
12
|
const RECORD = "identity.json";
|
|
11
13
|
const hash = (text) => createHash("sha256").update(text).digest("hex");
|
|
@@ -42,7 +44,9 @@ function read(dir) {
|
|
|
42
44
|
const record = JSON.parse(readFileSync(file, "utf8"));
|
|
43
45
|
if (record.v !== 1 || typeof record.space !== "string" || typeof record.root !== "string" ||
|
|
44
46
|
typeof record.session !== "string" || !/^[A-Za-z0-9_-]{22,64}$/.test(record.requestId) ||
|
|
45
|
-
|
|
47
|
+
(record.replaces !== undefined && (typeof record.replaces !== "string" || record.replaces === record.requestId || !/^[A-Za-z0-9_-]{22,64}$/.test(record.replaces))) ||
|
|
48
|
+
(record.tokenHashOnly !== undefined && (record.tokenHashOnly !== true || record.proof || !record.remote?.material)) ||
|
|
49
|
+
!(record.proof || record.tokenHashOnly ? /^[a-f0-9]{64}$/ : /^[A-Za-z0-9_-]{43}$/).test(record.token) || !["creating", "ready", "retiring"].includes(record.state) ||
|
|
46
50
|
!record.consumer || !Number.isInteger(record.consumer.pid) || record.consumer.pid <= 0 ||
|
|
47
51
|
Boolean(record.remote) === Boolean(record.local))
|
|
48
52
|
throw new Error("conversation identity recovery record is invalid; it was retained");
|
|
@@ -73,7 +77,7 @@ function consumerGone(consumer) {
|
|
|
73
77
|
}
|
|
74
78
|
/** The creation body. A replay sends the same one, so its digest names the same request. */
|
|
75
79
|
function creationBody(record) {
|
|
76
|
-
return { v: 1, requestId: record.requestId, tokenHash: record.proof ? record.token : hash(record.token), label: record.label,
|
|
80
|
+
return { v: 1, requestId: record.requestId, tokenHash: record.proof || record.tokenHashOnly ? record.token : hash(record.token), label: record.label,
|
|
77
81
|
...(record.selection ? { selection: record.selection } : {}) };
|
|
78
82
|
}
|
|
79
83
|
/** An issued answer must carry the person's decision on the approval the request saw. Only the exact
|
|
@@ -103,15 +107,39 @@ export class ConversationRefusal extends Error {
|
|
|
103
107
|
}
|
|
104
108
|
}
|
|
105
109
|
export { ConversationRequestError } from "./conversation-resume.js";
|
|
110
|
+
const HELD_APPROVAL_NAME = "The name is in use, and the earlier conversation still holds it.";
|
|
111
|
+
const HELD_PROOFLESS_NAME = "This conversation's earlier approval still holds the name and has no resume proof. Connect with another name to request new approval. To end the earlier grant, run `cotal logout` or ask the space operator to revoke the identity.";
|
|
112
|
+
function accountRefusalExplanation(value) {
|
|
113
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
114
|
+
return undefined;
|
|
115
|
+
const { reason, code, error } = value;
|
|
116
|
+
switch (reason ?? code ?? error) {
|
|
117
|
+
case "not signed in": return "This computer is not signed in.";
|
|
118
|
+
case "machine-forgotten": return "This computer was forgotten by the account service.";
|
|
119
|
+
case "computer-removed": return "This computer was removed from your account.";
|
|
120
|
+
default: return undefined;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Use the saved provider pin, not any value from the refusal. */
|
|
124
|
+
function accountSignInHint(idpUrl) {
|
|
125
|
+
const checked = checkIdpUrl(idpUrl);
|
|
126
|
+
if (!checked.ok)
|
|
127
|
+
return " Sign in to the space's provider on this computer again.";
|
|
128
|
+
const url = checked.url.href;
|
|
129
|
+
const word = process.platform === "win32" ? windowsShellWord(url) : shellWord(url);
|
|
130
|
+
if (!word)
|
|
131
|
+
return ` Sign in on this computer again. Pass the space's provider address ${codeSpan(url)} to \`cotal login --idp\`.`;
|
|
132
|
+
return ` Sign in on this computer again with ${codeSpan(loginRecoveryCommand(url) === "cotal login" ? "cotal login" : `cotal login --idp ${word}`)}.`;
|
|
133
|
+
}
|
|
106
134
|
/** What one creation answer proves. A selection refusal, a recorded denial, expiry or cap refusal,
|
|
107
135
|
* and a pending approval each prove that nothing was issued. Any other answer than an exact issued identity
|
|
108
136
|
* leaves the outcome unknown, so it throws and the request stays saved. */
|
|
109
|
-
function creationOutcome(record, authority, response, what) {
|
|
137
|
+
function creationOutcome(record, authority, response, what, heldName = HELD_APPROVAL_NAME) {
|
|
110
138
|
const remote = record.remote;
|
|
111
139
|
if (!remote.material) {
|
|
112
140
|
const nameTaken = nameTakenRefusal(response.status, response.body, record.requestId);
|
|
113
141
|
if (nameTaken)
|
|
114
|
-
return { refused:
|
|
142
|
+
return { refused: heldName, decision: "refused" };
|
|
115
143
|
const cap = agentCapRefusal(response.status, response.body, authority, record.requestId);
|
|
116
144
|
if (cap)
|
|
117
145
|
return { refused: cap, decision: "refused" };
|
|
@@ -130,9 +158,14 @@ function creationOutcome(record, authority, response, what) {
|
|
|
130
158
|
return { pending };
|
|
131
159
|
}
|
|
132
160
|
}
|
|
133
|
-
if (response.status !== 200 && response.status !== 201)
|
|
134
|
-
|
|
135
|
-
|
|
161
|
+
if (response.status !== 200 && response.status !== 201) {
|
|
162
|
+
if (legacyNameTakenRefusal(response.status, response.body))
|
|
163
|
+
throw new ConversationRequestError(heldName, false);
|
|
164
|
+
const explanation = response.status === 401 ? accountRefusalExplanation(response.body) : undefined;
|
|
165
|
+
const hint = explanation ? `. ${explanation}${accountSignInHint(remote.idpUrl)}` : "";
|
|
166
|
+
throw new ConversationRequestError(legacyAgentCapRefusal(response.status, response.body) ??
|
|
167
|
+
`conversation identity ${what} answered HTTP ${response.status}; its original request was retained${hint}`, response.status >= 500 || [408, 425, 429].includes(response.status));
|
|
168
|
+
}
|
|
136
169
|
return { material: checkConversationMaterial(response.body, authority, record.requestId, record.selection, approvalExpectation(remote)) };
|
|
137
170
|
}
|
|
138
171
|
function request(provider) {
|
|
@@ -163,7 +196,7 @@ async function noNativeSession(target) {
|
|
|
163
196
|
/** The session key is supplied by the native host, not guessed from a parent pid or an open inbox.
|
|
164
197
|
* A new request waits for a person's approval: until it is decided this returns `pending`, and
|
|
165
198
|
* connecting again replays the same saved request and secret. */
|
|
166
|
-
export async function provisionUserApp(target, name, session, events, selection) {
|
|
199
|
+
export async function provisionUserApp(target, name, session, events, selection, watch = false) {
|
|
167
200
|
if (!session || !/^[A-Za-z0-9_-]{1,128}$/.test(session))
|
|
168
201
|
throw new Error(await noNativeSession(target));
|
|
169
202
|
const root = identityRoot();
|
|
@@ -173,40 +206,44 @@ export async function provisionUserApp(target, name, session, events, selection)
|
|
|
173
206
|
// hosts cannot open different identities for the same native conversation at the same time.
|
|
174
207
|
const sessionLock = acquireLock(join(root, `session-${hash(session)}.lock`), { waitMs: 0 });
|
|
175
208
|
try {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (!isUserAppDir(dir) || !existsSync(join(dir, RECORD)))
|
|
179
|
-
continue;
|
|
180
|
-
const previous = read(dir);
|
|
181
|
-
if (previous.session !== session || previous.space === target.space)
|
|
182
|
-
continue;
|
|
183
|
-
if (previous.proof && !matchesResumeBinding(previous, previous.proof))
|
|
184
|
-
continue;
|
|
185
|
-
if (previous.consumer.pid !== process.ppid && !consumerGone(previous.consumer))
|
|
186
|
-
throw new Error("this native conversation still has a live consumer on another space; disconnect it before choosing another");
|
|
187
|
-
// A pending request or a dead consumer must finish its exact retirement before a new target
|
|
188
|
-
// receives a request. Failure keeps the old record and sends nothing to the new authority.
|
|
189
|
-
await retireUserApp(dir);
|
|
190
|
-
}
|
|
191
|
-
return await provisionOnSpace(target, name, session, events, selection);
|
|
209
|
+
await prepareOtherSpaces(session, target.space);
|
|
210
|
+
return await provisionOnSpace(target, name, session, events, selection, watch);
|
|
192
211
|
}
|
|
193
212
|
finally {
|
|
194
213
|
sessionLock.release();
|
|
195
214
|
}
|
|
196
215
|
}
|
|
197
|
-
|
|
216
|
+
/** A native session has one live consumer. Ready approvals on other spaces stay intact. */
|
|
217
|
+
async function prepareOtherSpaces(session, space) {
|
|
218
|
+
const records = [...savedSessionRecords(session)];
|
|
219
|
+
const retire = [];
|
|
220
|
+
for (const { dir, record } of records) {
|
|
221
|
+
if (record.state !== "retiring" && record.consumer.pid !== process.ppid && !consumerGone(record.consumer))
|
|
222
|
+
throw new Error(record.space === space ? "this native conversation still has a live inbox consumer; no identity was reissued"
|
|
223
|
+
: "this native conversation still has a live consumer on another space; disconnect it before choosing another");
|
|
224
|
+
if (record.space === space)
|
|
225
|
+
continue;
|
|
226
|
+
if (!hasIssuedApproval(record))
|
|
227
|
+
retire.push(dir);
|
|
228
|
+
}
|
|
229
|
+
for (const dir of retire)
|
|
230
|
+
await retireUserApp(dir);
|
|
231
|
+
}
|
|
232
|
+
async function provisionOnSpace(target, name, session, events, selection, watch = false) {
|
|
198
233
|
const store = workspaceSecretStore(target.root);
|
|
199
234
|
const infra = target.userAuth?.remote === true ? undefined : await getSpaceAuth(store, target.space);
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (saved.state
|
|
235
|
+
const selected = selectSavedApproval(target.space, session, name, selection);
|
|
236
|
+
let { dir } = selected;
|
|
237
|
+
const saved = selected.record;
|
|
238
|
+
if (saved?.remote && saved.session === session) {
|
|
239
|
+
if ((saved.accountKey && saved.state !== "retiring") || (saved.proof && matchesResumeBinding(saved, saved.proof)))
|
|
240
|
+
requireResumeLogin(saved);
|
|
241
|
+
if (hasIssuedApproval(saved) && saved.trust !== trust(target))
|
|
205
242
|
throw new Error(RESUME_UNAVAILABLE);
|
|
206
243
|
}
|
|
207
244
|
const authority = infra ? undefined : await discoverConversationAuthority(target).catch((error) => {
|
|
208
245
|
if (saved?.proof && saved.state === "ready")
|
|
209
|
-
throw new Error(`Conversation identity discovery did not confirm the saved authority. The saved identity was retained. ${oneLine(error.message)}`, { cause: error });
|
|
246
|
+
throw new Error(`Conversation identity discovery did not confirm the saved authority. The saved identity was retained. ${oneLine(error.message)}${FRESH_CONVERSATION}`, { cause: error });
|
|
210
247
|
throw error;
|
|
211
248
|
});
|
|
212
249
|
const policy = authority?.bundle.policy ?? target.policy;
|
|
@@ -220,9 +257,31 @@ async function provisionOnSpace(target, name, session, events, selection) {
|
|
|
220
257
|
const root = identityRoot();
|
|
221
258
|
mkSecretDir(root);
|
|
222
259
|
privateDir(root);
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
260
|
+
const expected = { name, session, trust: trust(target), selection };
|
|
261
|
+
const prepared = await prepareSavedIdentity(dir, expected, authority);
|
|
262
|
+
let { reason, resumed } = prepared;
|
|
263
|
+
let replaces;
|
|
264
|
+
let heldName = HELD_APPROVAL_NAME;
|
|
265
|
+
const visited = new Set();
|
|
266
|
+
for (let prior = prepared.replacement ? saved : undefined; prior;) {
|
|
267
|
+
if (prior.label === name && !prior.proof && hasIssuedApproval(prior))
|
|
268
|
+
heldName = HELD_PROOFLESS_NAME;
|
|
269
|
+
replaces = prior.requestId;
|
|
270
|
+
if (visited.has(replaces))
|
|
271
|
+
throw new Error("saved conversation approval history is invalid; no new approval was requested");
|
|
272
|
+
visited.add(replaces);
|
|
273
|
+
dir = join(root, replacementKey(prior, replaces));
|
|
274
|
+
const stagedRecord = existsSync(join(dir, RECORD)) ? read(dir) : undefined;
|
|
275
|
+
if (stagedRecord && (stagedRecord.replaces !== replaces || stagedRecord.session !== session || stagedRecord.trust !== expected.trust))
|
|
276
|
+
throw new Error("the saved replacement does not match this conversation; it was retained");
|
|
277
|
+
if (stagedRecord?.accountKey)
|
|
278
|
+
requireResumeLogin(stagedRecord);
|
|
279
|
+
const staged = await prepareSavedIdentity(dir, expected, authority);
|
|
280
|
+
resumed = staged.resumed;
|
|
281
|
+
reason = staged.reason ?? reason;
|
|
282
|
+
prior = staged.replacement ? stagedRecord : undefined;
|
|
283
|
+
}
|
|
284
|
+
// A reconnect first finishes the previous explicit departure.
|
|
226
285
|
if (existsSync(join(dir, RECORD)) && read(dir).state === "retiring") {
|
|
227
286
|
try {
|
|
228
287
|
await retireUserApp(dir);
|
|
@@ -239,7 +298,7 @@ async function provisionOnSpace(target, name, session, events, selection) {
|
|
|
239
298
|
record = read(dir);
|
|
240
299
|
if (record.session !== session || record.trust !== trust(target) || record.label !== name)
|
|
241
300
|
throw new Error("this conversation has a saved identity under different mesh pins or a different name; disconnect it before connecting again");
|
|
242
|
-
if (canonicalJson(record.selection ?? null) !== canonicalJson(selection ?? null))
|
|
301
|
+
if (resumed !== record.requestId && canonicalJson(record.selection ?? null) !== canonicalJson(selection ?? null))
|
|
243
302
|
throw new Error("this conversation has a saved identity with a different selection; connect with that selection to resume it, then disconnect before choosing another");
|
|
244
303
|
if (record.state === "retiring")
|
|
245
304
|
throw new Error("this conversation's prior identity is still retiring; retry disconnect before connecting again");
|
|
@@ -263,7 +322,7 @@ async function provisionOnSpace(target, name, session, events, selection) {
|
|
|
263
322
|
record = {
|
|
264
323
|
v: 1, space: target.space, root: target.root, trust: trust(target), retirementTarget: { space: target.space, root: target.root, server: target.server,
|
|
265
324
|
tlsRequired: target.tlsRequired, userAuth: target.userAuth }, session,
|
|
266
|
-
requestId: randomBytes(24).toString("base64url"), label: name, token, state: "creating",
|
|
325
|
+
requestId: randomBytes(24).toString("base64url"), ...(replaces ? { replaces } : {}), label: name, token, state: "creating",
|
|
267
326
|
consumer: { pid: process.ppid, start: processStartToken(process.ppid) }, ...(selection ? { selection } : {}),
|
|
268
327
|
remote: { url: authority.url, idpUrl: target.userAuth.idp.url, approval: {} },
|
|
269
328
|
};
|
|
@@ -282,7 +341,7 @@ async function provisionOnSpace(target, name, session, events, selection) {
|
|
|
282
341
|
throw new Error("the selected mesh does not advertise connection approval v1; the saved request was retained and not sent");
|
|
283
342
|
markUnderApproval(dir, record);
|
|
284
343
|
const response = await requestForRecord(provider, record)({ url: authority.url, idpUrl: record.remote.idpUrl, method: "POST", body: creationBody(record) });
|
|
285
|
-
const outcome = creationOutcome(record, authority, response, "creation");
|
|
344
|
+
const outcome = creationOutcome(record, authority, response, "creation", heldName);
|
|
286
345
|
if ("refused" in outcome) {
|
|
287
346
|
discard(dir);
|
|
288
347
|
throw new ConversationRefusal(outcome.decision, outcome.refused);
|
|
@@ -305,6 +364,10 @@ async function provisionOnSpace(target, name, session, events, selection) {
|
|
|
305
364
|
const grant = conversationGrant(local, record.selection);
|
|
306
365
|
await withProvisioner({ space: target.space, servers: target.server, auth: infra }, "app-provisioner", (provisioner) => provisionAgentDurables(provisioner, local, { subscribe: [], allowSubscribe: grant.allowSubscribe, ...(grant.role ? { role: grant.role } : {}) }));
|
|
307
366
|
}
|
|
367
|
+
if (watch && record.remote) {
|
|
368
|
+
save(dir, record);
|
|
369
|
+
return { env: {}, retire: { dir }, grant: record.remote.material.grant };
|
|
370
|
+
}
|
|
308
371
|
record.state = "ready";
|
|
309
372
|
save(dir, record);
|
|
310
373
|
const material = record.remote?.material ?? record.local;
|
|
@@ -321,7 +384,9 @@ async function provisionOnSpace(target, name, session, events, selection) {
|
|
|
321
384
|
COTAL_SUBSCRIBE: "", COTAL_ALLOW_SUBSCRIBE: grant.allowSubscribe.join(","), COTAL_ALLOW_PUBLISH: grant.allowPublish.join(","),
|
|
322
385
|
...(grant.role ? { COTAL_ROLE: grant.role } : {}), ...(grant.scope.length ? { COTAL_CAPABILITIES: grant.scope.join(",") } : {}),
|
|
323
386
|
COTAL_WORKSPACE_ROOT: homeCotalDir(), ...(events ? { COTAL_EVENTS: "1" } : {}),
|
|
324
|
-
...(record.
|
|
387
|
+
...(record.remote ? { COTAL_RETIRE_APPROVAL_SESSION: "1" } : {}),
|
|
388
|
+
...(record.remote && !record.proof ? { COTAL_RELEASE_PROOFLESS_APPROVAL: "1" } : {}),
|
|
389
|
+
...(hasIssuedApproval(record) ? { COTAL_CONVERSATION_APPROVAL: resumed === record.requestId ? "resumed" : "resumable" } : {}),
|
|
325
390
|
...(policy?.events === "required" ? { COTAL_EVENTS_REQUIRED: "1" } : {}),
|
|
326
391
|
}, retire: { dir }, grant };
|
|
327
392
|
}
|
|
@@ -330,8 +395,6 @@ async function provisionOnSpace(target, name, session, events, selection) {
|
|
|
330
395
|
}
|
|
331
396
|
}
|
|
332
397
|
function attachResumeProof(record, authority, provider) {
|
|
333
|
-
if (!authority.resume)
|
|
334
|
-
return;
|
|
335
398
|
const binding = account(provider);
|
|
336
399
|
let accountKey;
|
|
337
400
|
try {
|
|
@@ -342,71 +405,45 @@ function attachResumeProof(record, authority, provider) {
|
|
|
342
405
|
} // A legacy login without a subject still uses ordinary approval.
|
|
343
406
|
if (!accountKey)
|
|
344
407
|
return;
|
|
408
|
+
record.accountKey = accountKey;
|
|
409
|
+
if (!authority.resume)
|
|
410
|
+
return;
|
|
345
411
|
const resume = createResumeProof(record);
|
|
346
412
|
if (resume)
|
|
347
|
-
Object.assign(record, {
|
|
413
|
+
Object.assign(record, { proof: resume.proof, token: resume.tokenHash });
|
|
348
414
|
}
|
|
349
415
|
async function prepareSavedIdentity(dir, expected, authority) {
|
|
350
416
|
if (!existsSync(join(dir, RECORD)))
|
|
351
417
|
return {};
|
|
352
418
|
const previous = read(dir);
|
|
353
|
-
if (previous.state === "retiring")
|
|
419
|
+
if (previous.state === "retiring") {
|
|
354
420
|
return {};
|
|
355
|
-
|
|
421
|
+
}
|
|
422
|
+
const selectionChanged = canonicalJson(previous.selection ?? null) !== canonicalJson(expected.selection ?? null);
|
|
423
|
+
const changed = previous.label !== expected.name || selectionChanged;
|
|
356
424
|
if (previous.proof) {
|
|
357
425
|
if (matchesResumeBinding(previous, previous.proof) && previous.trust !== expected.trust)
|
|
358
426
|
throw new Error(RESUME_UNAVAILABLE);
|
|
359
427
|
if (previous.consumer.pid !== process.ppid && !consumerGone(previous.consumer))
|
|
360
428
|
throw new Error("this native conversation still has a live inbox consumer; no identity was reissued");
|
|
361
|
-
if (previous.session !== expected.session
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
setAside(dir, current, `-${randomBytes(8).toString("hex")}`);
|
|
368
|
-
}
|
|
369
|
-
finally {
|
|
370
|
-
lock.release();
|
|
371
|
-
}
|
|
372
|
-
return { reason: "The saved approval could not be used on this computer." };
|
|
373
|
-
}
|
|
374
|
-
if (!previous.accountKey) {
|
|
375
|
-
await retireUserApp(dir);
|
|
376
|
-
return { reason: "The saved approval does not identify its approving account. Fresh approval is required." };
|
|
429
|
+
if (previous.session !== expected.session)
|
|
430
|
+
throw new Error("This native session does not match the saved approval. Nothing was connected.");
|
|
431
|
+
if (!matchesResumeBinding(previous, previous.proof) && hasIssuedApproval(previous)) {
|
|
432
|
+
if (previous.trust === expected.trust && authority?.approval && matchesSavedAuthority(previous, authority))
|
|
433
|
+
return replacementApproval("The saved approval could not be used on this computer.");
|
|
434
|
+
throw new Error(`The saved approval could not be used on this computer. The saved identity was retained.${FRESH_CONVERSATION}`);
|
|
377
435
|
}
|
|
378
436
|
if (previous.state === "ready")
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
return { reason: "The saved approval could not be used on this computer." };
|
|
388
|
-
}
|
|
389
|
-
if (previous.state === "ready") {
|
|
390
|
-
if (changed) {
|
|
391
|
-
await retireUserApp(dir);
|
|
392
|
-
return { reason: "The saved approval does not cover this name or selection." };
|
|
393
|
-
}
|
|
394
|
-
const lock = acquireLock(`${dir}.lock`, { waitMs: 0 });
|
|
395
|
-
try {
|
|
396
|
-
const current = read(dir);
|
|
397
|
-
if (current.state !== "ready" || current.requestId !== previous.requestId)
|
|
398
|
-
return {};
|
|
399
|
-
if (await resumeSavedIdentity(current, authority))
|
|
400
|
-
return { resumed: current.requestId };
|
|
401
|
-
current.state = "retiring";
|
|
402
|
-
save(dir, current);
|
|
403
|
-
}
|
|
404
|
-
finally {
|
|
405
|
-
lock.release();
|
|
406
|
-
}
|
|
407
|
-
await retireUserApp(dir);
|
|
408
|
-
return { reason: "The authority did not accept the saved approval." };
|
|
437
|
+
return prepareReadyApproval(dir, previous, authority, !matchesApprovedRequest(previous, expected.name, expected.selection));
|
|
438
|
+
}
|
|
439
|
+
if (!previous.proof && previous.remote?.material && previous.state === "ready" &&
|
|
440
|
+
(changed || previous.tokenHashOnly || previous.consumer.pid !== process.ppid)) {
|
|
441
|
+
if (previous.trust === expected.trust && authority?.approval && matchesSavedAuthority(previous, authority)) {
|
|
442
|
+
if (consumerGone(previous.consumer))
|
|
443
|
+
releaseUserApp(dir, false);
|
|
444
|
+
return replacementApproval(changed ? "This request differs from the saved approval." : "The saved approval has no resume proof.");
|
|
409
445
|
}
|
|
446
|
+
throw new Error(RESUME_UNAVAILABLE);
|
|
410
447
|
}
|
|
411
448
|
if (previous.session !== expected.session || previous.trust !== expected.trust)
|
|
412
449
|
return {};
|
|
@@ -414,11 +451,123 @@ async function prepareSavedIdentity(dir, expected, authority) {
|
|
|
414
451
|
if (changed && previous.state === "creating" && previous.remote && !previous.remote.material) {
|
|
415
452
|
if (previous.consumer.pid !== process.ppid && !consumerGone(previous.consumer))
|
|
416
453
|
throw new Error("this native conversation still has a live inbox consumer; its pending request was not replaced");
|
|
417
|
-
await
|
|
454
|
+
await retire(dir, (record) => record.state === "creating" && !record.remote?.material);
|
|
418
455
|
}
|
|
419
456
|
return {};
|
|
420
457
|
}
|
|
421
|
-
|
|
458
|
+
function replacementApproval(reason) {
|
|
459
|
+
return { replacement: true, reason: `${reason} The saved approval and its grant were retained. This request needs its own approval.` };
|
|
460
|
+
}
|
|
461
|
+
const primaryKey = (space, session) => hash(`${space}\0${session}`);
|
|
462
|
+
const replacementKey = (r, prior) => hash(`${r.space}\0${r.session}\0replacement\0${prior}`);
|
|
463
|
+
function matchesApprovedRequest(record, name, selection) {
|
|
464
|
+
if (record.label !== name)
|
|
465
|
+
return false;
|
|
466
|
+
if (!selection && record.proof)
|
|
467
|
+
return true;
|
|
468
|
+
const wanted = canonicalJson(selection ?? null);
|
|
469
|
+
return wanted === canonicalJson(record.selection ?? null) || wanted === canonicalJson(record.remote?.material?.approval?.approved ?? null);
|
|
470
|
+
}
|
|
471
|
+
/** Prefer a matching approved selection, never an identity from another space. */
|
|
472
|
+
function selectSavedApproval(space, session, name, selection) {
|
|
473
|
+
const dir = join(identityRoot(), primaryKey(space, session));
|
|
474
|
+
const original = existsSync(join(dir, RECORD)) ? read(dir) : undefined;
|
|
475
|
+
const records = [...savedSessionRecords(session)].filter((entry) => entry.record.space === space);
|
|
476
|
+
const matching = records.filter(({ record }) => hasIssuedApproval(record) && record.label === name &&
|
|
477
|
+
canonicalJson(record.selection ?? null) === canonicalJson(selection ?? null));
|
|
478
|
+
const exact = matching.find(({ record }) => !matching.some(({ record: other }) => other.replaces === record.requestId));
|
|
479
|
+
return exact ?? (original ? { dir, record: original } : records.find(({ record }) => matchesApprovedRequest(record, name, selection)) ?? records[0] ?? { dir });
|
|
480
|
+
}
|
|
481
|
+
async function prepareReadyApproval(dir, previous, authority, mismatch) {
|
|
482
|
+
const lock = acquireLock(`${dir}.lock`, { waitMs: 0 });
|
|
483
|
+
try {
|
|
484
|
+
const current = read(dir);
|
|
485
|
+
if (current.state !== "ready" || current.requestId !== previous.requestId)
|
|
486
|
+
return {};
|
|
487
|
+
if (authority?.approval && matchesSavedAuthority(current, authority)) {
|
|
488
|
+
checkedSavedMaterial(current, authority, "retire");
|
|
489
|
+
if (mismatch)
|
|
490
|
+
return replacementApproval("This request differs from the saved approval.");
|
|
491
|
+
if (!current.accountKey || !current.proof || !resumeToken({ ...current, proof: current.proof }))
|
|
492
|
+
return replacementApproval("The saved approval could not be used on this computer.");
|
|
493
|
+
if (!authority.resume)
|
|
494
|
+
return replacementApproval("The space does not currently offer approval resume.");
|
|
495
|
+
if (canonicalJson(current.remote?.material?.policy ?? null) !== canonicalJson(authority.bundle.policy ?? null))
|
|
496
|
+
return replacementApproval("The space changed its event policy.");
|
|
497
|
+
}
|
|
498
|
+
if (await resumeSavedIdentity(current, authority))
|
|
499
|
+
return { resumed: current.requestId };
|
|
500
|
+
if (authority?.approval)
|
|
501
|
+
return replacementApproval("The authority did not accept the saved approval for reconnecting.");
|
|
502
|
+
}
|
|
503
|
+
finally {
|
|
504
|
+
lock.release();
|
|
505
|
+
}
|
|
506
|
+
throw new Error(`The authority did not accept the saved approval. The saved identity was retained.${FRESH_CONVERSATION}`);
|
|
507
|
+
}
|
|
508
|
+
const FRESH_CONVERSATION = " If the problem persists, use a new conversation to request fresh approval without replacing this saved approval.";
|
|
509
|
+
const RESUME_RETRY_MS = 10_000;
|
|
510
|
+
const RESUME_UNAVAILABLE = `The space does not currently offer resume at the saved authority and trust pins. The saved identity was retained.${FRESH_CONVERSATION}`;
|
|
511
|
+
function savedApprovalDescription(record) {
|
|
512
|
+
const decision = record.remote?.material?.approval;
|
|
513
|
+
if (!decision)
|
|
514
|
+
return undefined;
|
|
515
|
+
const { approved } = checkConversationApprovalDecision(decision);
|
|
516
|
+
const channels = approved.channels.map(({ channel, access }) => `#${channel} (${access})`).join(", ") || "none";
|
|
517
|
+
const capabilities = approved.capabilities.filter((c) => !c.startsWith("role:")).join(", ") || "none";
|
|
518
|
+
return `Saved approval: name ${JSON.stringify(record.label)}, space ${JSON.stringify(record.space)}. ` +
|
|
519
|
+
`Channels: ${channels}. Role: ${approved.role ?? "none"}. Capabilities: ${capabilities}. ` +
|
|
520
|
+
`To reconnect, call \`cotal_connect\` with name ${JSON.stringify(record.label)} and space ${JSON.stringify(record.space)}, omitting channels, role and capabilities.`;
|
|
521
|
+
}
|
|
522
|
+
async function savedSpaceCanResume(record) {
|
|
523
|
+
if (!record.accountKey || !record.proof || !record.remote?.material || !resumeToken({ ...record, proof: record.proof }))
|
|
524
|
+
return false;
|
|
525
|
+
try {
|
|
526
|
+
const target = await resolveTargetOrThrow({ space: record.space, readOnly: true });
|
|
527
|
+
if (trust(target) !== record.trust || !await isReachable(target.server))
|
|
528
|
+
return false;
|
|
529
|
+
const authority = requireResumeAuthority(record, await discoverConversationAuthority(target));
|
|
530
|
+
checkConversationMaterial(record.remote.material, authority, record.requestId, record.selection, approvalExpectation(record.remote));
|
|
531
|
+
return true;
|
|
532
|
+
}
|
|
533
|
+
catch {
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function* savedSessionRecords(session, retiring = false) {
|
|
538
|
+
if (!session || !/^[A-Za-z0-9_-]{1,128}$/.test(session))
|
|
539
|
+
return;
|
|
540
|
+
const root = identityRoot();
|
|
541
|
+
if (!existsSync(root))
|
|
542
|
+
return;
|
|
543
|
+
privateDir(root);
|
|
544
|
+
for (const entry of readdirSync(root)) {
|
|
545
|
+
const dir = join(root, entry);
|
|
546
|
+
const setAside = retiring && isUnconfirmedDirectory(entry);
|
|
547
|
+
if ((!isUserAppDir(dir) && !setAside) || !existsSync(join(dir, RECORD)))
|
|
548
|
+
continue;
|
|
549
|
+
let record;
|
|
550
|
+
try {
|
|
551
|
+
record = read(dir);
|
|
552
|
+
}
|
|
553
|
+
catch {
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (record.session === session && (setAside ? matchesUnconfirmed(entry, record) :
|
|
557
|
+
entry === (record.replaces ? replacementKey(record, record.replaces) : primaryKey(record.space, session))) &&
|
|
558
|
+
(retiring || !record.proof || matchesResumeBinding(record, record.proof)))
|
|
559
|
+
yield { dir, record };
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
/** Return guidance only for the bound native session on the approving account and device. */
|
|
563
|
+
export async function savedUserAppApproval(session) {
|
|
564
|
+
const records = [...savedSessionRecords(session)].filter(({ record }) => hasIssuedApproval(record) && record.proof);
|
|
565
|
+
const [record, other] = records.map((entry) => entry.record);
|
|
566
|
+
if (!record?.proof || !record.accountKey || other)
|
|
567
|
+
return undefined;
|
|
568
|
+
requireResumeLogin(record);
|
|
569
|
+
return await savedSpaceCanResume(record) ? savedApprovalDescription(record) : undefined;
|
|
570
|
+
}
|
|
422
571
|
function requireResumeLogin(record) {
|
|
423
572
|
let key;
|
|
424
573
|
try {
|
|
@@ -433,22 +582,48 @@ function requireResumeLogin(record) {
|
|
|
433
582
|
if (record.accountKey && record.accountKey !== key)
|
|
434
583
|
throw new Error("This conversation was approved by another account. Sign in with the original account or connect a different conversation for fresh approval. The saved identity was retained.");
|
|
435
584
|
}
|
|
585
|
+
function matchesSavedAuthority(record, authority) {
|
|
586
|
+
return record.remote?.url === authority.url && record.remote.idpUrl === authority.bundle.userAuth.idp.url;
|
|
587
|
+
}
|
|
436
588
|
function requireResumeAuthority(record, authority) {
|
|
437
|
-
if (!authority?.resume || record
|
|
589
|
+
if (!authority?.resume || !matchesSavedAuthority(record, authority))
|
|
438
590
|
throw new Error(RESUME_UNAVAILABLE);
|
|
439
591
|
return authority;
|
|
440
592
|
}
|
|
593
|
+
function checkedSavedMaterial(record, authority, use) {
|
|
594
|
+
const remote = record.remote;
|
|
595
|
+
if (!remote.material)
|
|
596
|
+
throw new Error(`The saved approval has no issued identity. The saved record was retained.${FRESH_CONVERSATION}`);
|
|
597
|
+
try {
|
|
598
|
+
return checkConversationMaterial(remote.material, authority, record.requestId, record.selection, approvalExpectation(remote), use);
|
|
599
|
+
}
|
|
600
|
+
catch (error) {
|
|
601
|
+
throw new Error(`${oneLine(error.message)}. The saved identity was retained.${FRESH_CONVERSATION}`, { cause: error });
|
|
602
|
+
}
|
|
603
|
+
}
|
|
441
604
|
async function resumeSavedIdentity(record, discovered) {
|
|
442
605
|
if (!record.accountKey)
|
|
443
606
|
throw new Error("The saved approval does not identify its approving account. Fresh approval is required.");
|
|
444
607
|
requireResumeLogin(record);
|
|
445
|
-
const
|
|
446
|
-
const remote = record.remote;
|
|
447
|
-
if (!remote.material)
|
|
448
|
-
throw new Error("The saved approval has no issued identity. The saved record was retained.");
|
|
449
|
-
const material = checkConversationMaterial(remote.material, authority, record.requestId, record.selection, approvalExpectation(remote));
|
|
608
|
+
const material = checkedSavedMaterial(record, requireResumeAuthority(record, discovered), "connect");
|
|
450
609
|
const token = record.proof && resumeToken({ ...record, proof: record.proof });
|
|
451
|
-
|
|
610
|
+
if (!token || !material.approval)
|
|
611
|
+
return false;
|
|
612
|
+
const probe = async () => {
|
|
613
|
+
try {
|
|
614
|
+
return await confirmConversationResume(material, token);
|
|
615
|
+
}
|
|
616
|
+
catch (error) {
|
|
617
|
+
if (error instanceof ConversationRequestError)
|
|
618
|
+
throw new ConversationRequestError(`${error.message}${FRESH_CONVERSATION}`, error.retryable);
|
|
619
|
+
throw new ConversationRequestError(`The conversation resume exchange could not be reached. The saved identity was retained. Wait before trying again.${FRESH_CONVERSATION}`, true);
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
if (await probe())
|
|
623
|
+
return true;
|
|
624
|
+
await wait(RESUME_RETRY_MS);
|
|
625
|
+
requireResumeLogin(record);
|
|
626
|
+
return probe();
|
|
452
627
|
}
|
|
453
628
|
/** Remove a creation record the authority refused before writing anything. */
|
|
454
629
|
function discard(dir) {
|
|
@@ -467,10 +642,78 @@ export function isUserAppDir(dir) {
|
|
|
467
642
|
export function retireUserApp(dir) {
|
|
468
643
|
return retire(dir);
|
|
469
644
|
}
|
|
645
|
+
/** Release a stopped, claimed proofless approval's secret. Pending retirement keeps its retry material. */
|
|
646
|
+
export function releaseUserApp(dir, ownsConsumer = true) {
|
|
647
|
+
if (!isUserAppDir(dir) || !existsSync(join(dir, RECORD)))
|
|
648
|
+
return;
|
|
649
|
+
const lock = acquireLock(`${dir}.lock`, { waitMs: 0 });
|
|
650
|
+
try {
|
|
651
|
+
const record = read(dir);
|
|
652
|
+
if (record.state !== "ready" || record.proof || !record.remote?.material ||
|
|
653
|
+
!(ownsConsumer && record.consumer.pid === process.ppid) && !consumerGone(record.consumer))
|
|
654
|
+
return;
|
|
655
|
+
discardProoflessSecret(dir, record);
|
|
656
|
+
}
|
|
657
|
+
finally {
|
|
658
|
+
lock.release();
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
function discardProoflessSecret(dir, record) {
|
|
662
|
+
if (record.proof || !record.remote?.material)
|
|
663
|
+
return;
|
|
664
|
+
if (!record.tokenHashOnly) {
|
|
665
|
+
record.token = hash(record.token);
|
|
666
|
+
record.tokenHashOnly = true;
|
|
667
|
+
save(dir, record);
|
|
668
|
+
}
|
|
669
|
+
const token = join(dir, "actor.token");
|
|
670
|
+
if (existsSync(token))
|
|
671
|
+
unlinkSync(token);
|
|
672
|
+
if (process.platform !== "win32")
|
|
673
|
+
syncFile(dir);
|
|
674
|
+
}
|
|
675
|
+
/** Retire a native session's approvals without touching another process's live consumer. */
|
|
676
|
+
export async function retireUserAppSession(session) {
|
|
677
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(session))
|
|
678
|
+
throw new Error("retire needs a valid native session");
|
|
679
|
+
const root = identityRoot();
|
|
680
|
+
if (!existsSync(root))
|
|
681
|
+
return;
|
|
682
|
+
privateDir(root);
|
|
683
|
+
const lock = acquireLock(join(root, `session-${hash(session)}.lock`), { waitMs: 0 });
|
|
684
|
+
try {
|
|
685
|
+
const remaining = [];
|
|
686
|
+
for (const { dir, record } of [...savedSessionRecords(session, true)]) {
|
|
687
|
+
try {
|
|
688
|
+
await retire(dir, (current) => {
|
|
689
|
+
if (current.session !== session || current.requestId !== record.requestId || current.proof && !matchesResumeBinding(current, current.proof) ||
|
|
690
|
+
(current.consumer.pid !== process.ppid && !consumerGone(current.consumer)))
|
|
691
|
+
throw new Error("the saved record changed or is still held by another process");
|
|
692
|
+
return true;
|
|
693
|
+
}, homeCotalDir(), true);
|
|
694
|
+
}
|
|
695
|
+
catch (error) {
|
|
696
|
+
const detail = error instanceof Error
|
|
697
|
+
? /^conversation identity retirement is pending at phase "(?:retired|draining)"/.exec(error.message)?.[0] : undefined;
|
|
698
|
+
remaining.push(`${JSON.stringify(oneLine(record.label))} in ${JSON.stringify(oneLine(record.space))}${detail ? `: ${detail}` : ""}`);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
if (remaining.length)
|
|
702
|
+
throw new Error(`retirement was not confirmed for ${remaining.join(", ")}; retry disconnect or revoke the listed approvals at their space authorities`);
|
|
703
|
+
}
|
|
704
|
+
finally {
|
|
705
|
+
lock.release();
|
|
706
|
+
}
|
|
707
|
+
}
|
|
470
708
|
function idpKey(url) {
|
|
471
709
|
const checked = checkIdpUrl(url);
|
|
472
710
|
return checked.ok ? checked.url.origin + checked.url.pathname.replace(/\/+$/, "") : undefined;
|
|
473
711
|
}
|
|
712
|
+
function matchesSignOut(entry, record, idp, accountKey) {
|
|
713
|
+
return !!record.remote && idpKey(record.remote.idpUrl) === idp &&
|
|
714
|
+
(!record.accountKey || !accountKey || record.accountKey === accountKey) &&
|
|
715
|
+
(!isUnconfirmedDirectory(entry) || matchesUnconfirmed(entry, record));
|
|
716
|
+
}
|
|
474
717
|
export const conversationSignOut = {
|
|
475
718
|
kind: "conversation-sign-out", name: "native",
|
|
476
719
|
async retire({ dir: home, idp, accountKey }) {
|
|
@@ -486,14 +729,14 @@ export const conversationSignOut = {
|
|
|
486
729
|
continue;
|
|
487
730
|
let failure = {};
|
|
488
731
|
try {
|
|
489
|
-
const matches = (r) =>
|
|
490
|
-
(!r.accountKey || !accountKey || r.accountKey === accountKey) && (setAside ? matchesUnconfirmed(entry, r) : !!r.proof) &&
|
|
491
|
-
(!r.proof || matchesResumeBinding(r, r.proof));
|
|
732
|
+
const matches = (r) => matchesSignOut(entry, r, idp, accountKey);
|
|
492
733
|
const record = read(dir);
|
|
493
734
|
if (!matches(record))
|
|
494
735
|
continue;
|
|
495
736
|
failure = { label: oneLine(record.label), space: oneLine(record.space) };
|
|
496
|
-
|
|
737
|
+
if (record.proof && !matchesResumeBinding(record, record.proof))
|
|
738
|
+
throw new Error("the saved proof does not match this record");
|
|
739
|
+
await retire(dir, (current) => matches(current) && (!current.proof || matchesResumeBinding(current, current.proof)), home, true);
|
|
497
740
|
}
|
|
498
741
|
catch {
|
|
499
742
|
failures.push(failure);
|
|
@@ -521,8 +764,7 @@ export const conversationSignOut = {
|
|
|
521
764
|
continue;
|
|
522
765
|
try {
|
|
523
766
|
const r = read(dir);
|
|
524
|
-
if (r.remote?.material &&
|
|
525
|
-
matchesResumeBinding(r, r.proof) && (!setAside || matchesUnconfirmed(entry, r)))
|
|
767
|
+
if (r.remote?.material && matchesSignOut(entry, r, idp, accountKey))
|
|
526
768
|
total++;
|
|
527
769
|
}
|
|
528
770
|
catch { /* unreadable entry does not block sign-in */ }
|
|
@@ -537,19 +779,25 @@ function matchesUnconfirmed(entry, record) {
|
|
|
537
779
|
const prefix = `unconfirmed-${record.requestId}`;
|
|
538
780
|
return record.state === "retiring" && (entry === prefix || (entry.startsWith(prefix) && /^-[a-f0-9]{16}$/.test(entry.slice(prefix.length))));
|
|
539
781
|
}
|
|
540
|
-
|
|
782
|
+
const hasIssuedApproval = (r) => r.state === "ready" && !!r.remote?.material;
|
|
783
|
+
/** Clean up dead consumers in this mesh. Issued approvals stay saved unless retirement is
|
|
541
784
|
* underway. A live or unknown consumer keeps its identity and is never locked. */
|
|
542
785
|
export async function retireDeadUserApps(target) {
|
|
543
786
|
const root = identityRoot();
|
|
544
787
|
if (!existsSync(root))
|
|
545
788
|
return;
|
|
546
789
|
const dead = (record) => record.trust === trust(target) && consumerGone(record.consumer) &&
|
|
547
|
-
|
|
790
|
+
(record.state !== "ready" || !record.remote?.material && !record.local?.sentinelCreds);
|
|
548
791
|
for (const name of readdirSync(root)) {
|
|
549
792
|
const dir = join(root, name);
|
|
550
793
|
try {
|
|
551
|
-
if (isUserAppDir(dir)
|
|
794
|
+
if (!isUserAppDir(dir) || !existsSync(join(dir, RECORD)))
|
|
795
|
+
continue;
|
|
796
|
+
const record = read(dir);
|
|
797
|
+
if (dead(record))
|
|
552
798
|
await retire(dir, dead);
|
|
799
|
+
else if (record.trust === trust(target) && consumerGone(record.consumer))
|
|
800
|
+
releaseUserApp(dir, false);
|
|
553
801
|
}
|
|
554
802
|
catch { /* retained for a later run */ }
|
|
555
803
|
}
|
|
@@ -646,7 +894,7 @@ async function retire(dir, when, home = homeCotalDir(), signOut = false) {
|
|
|
646
894
|
record.remote.material = material;
|
|
647
895
|
save(dir, record);
|
|
648
896
|
}
|
|
649
|
-
material = checkConversationMaterial(material, authority, record.requestId, record.selection, approvalExpectation(record.remote));
|
|
897
|
+
material = checkConversationMaterial(material, authority, record.requestId, record.selection, approvalExpectation(record.remote), "retire");
|
|
650
898
|
const expected = new URL(`${authority.url}/${material.actor}`);
|
|
651
899
|
expected.searchParams.set("lifecycleUid", assertLifecycleToken(material.lifecycleUid));
|
|
652
900
|
if (material.retire.url !== expected.href)
|
|
@@ -678,7 +926,7 @@ async function retire(dir, when, home = homeCotalDir(), signOut = false) {
|
|
|
678
926
|
const creds = await mintCreds(infra, newIdentity(), "deprovisioner", { deprovisionTarget: {
|
|
679
927
|
principal: principalKey(local.owner, local.actor).key, lifecycleUid: local.lifecycleUid
|
|
680
928
|
} });
|
|
681
|
-
await deprovisionAgent({ servers: target.server, space: target.space, targetId: principalKey(local.owner, local.actor).key,
|
|
929
|
+
await deprovisionAgent({ servers: target.server, space: target.space, tls: target.tlsRequired, targetId: principalKey(local.owner, local.actor).key,
|
|
682
930
|
lifecycleUid: local.lifecycleUid, creds });
|
|
683
931
|
}
|
|
684
932
|
removeSavedIdentity(dir);
|