@indigoai-us/hq-cloud 5.1.0 → 5.1.9

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 (100) hide show
  1. package/dist/bin/sync-runner.d.ts +134 -0
  2. package/dist/bin/sync-runner.d.ts.map +1 -0
  3. package/dist/bin/sync-runner.js +360 -0
  4. package/dist/bin/sync-runner.js.map +1 -0
  5. package/dist/bin/sync-runner.test.d.ts +10 -0
  6. package/dist/bin/sync-runner.test.d.ts.map +1 -0
  7. package/dist/bin/sync-runner.test.js +648 -0
  8. package/dist/bin/sync-runner.test.js.map +1 -0
  9. package/dist/cli/index.d.ts +1 -1
  10. package/dist/cli/index.d.ts.map +1 -1
  11. package/dist/cli/share.js +2 -2
  12. package/dist/cli/share.js.map +1 -1
  13. package/dist/cli/share.test.js +9 -1
  14. package/dist/cli/share.test.js.map +1 -1
  15. package/dist/cli/sync.d.ts +28 -0
  16. package/dist/cli/sync.d.ts.map +1 -1
  17. package/dist/cli/sync.js +33 -10
  18. package/dist/cli/sync.js.map +1 -1
  19. package/dist/cli/sync.test.js +15 -4
  20. package/dist/cli/sync.test.js.map +1 -1
  21. package/dist/cognito-auth.d.ts.map +1 -1
  22. package/dist/cognito-auth.js +19 -1
  23. package/dist/cognito-auth.js.map +1 -1
  24. package/dist/cognito-auth.test.d.ts +9 -0
  25. package/dist/cognito-auth.test.d.ts.map +1 -0
  26. package/dist/cognito-auth.test.js +113 -0
  27. package/dist/cognito-auth.test.js.map +1 -0
  28. package/dist/context.d.ts.map +1 -1
  29. package/dist/context.js +1 -0
  30. package/dist/context.js.map +1 -1
  31. package/dist/daemon-worker.d.ts +6 -1
  32. package/dist/daemon-worker.d.ts.map +1 -1
  33. package/dist/daemon-worker.js +12 -16
  34. package/dist/daemon-worker.js.map +1 -1
  35. package/dist/daemon.d.ts +2 -0
  36. package/dist/daemon.d.ts.map +1 -1
  37. package/dist/daemon.js +2 -0
  38. package/dist/daemon.js.map +1 -1
  39. package/dist/ignore.d.ts +13 -2
  40. package/dist/ignore.d.ts.map +1 -1
  41. package/dist/ignore.js +69 -12
  42. package/dist/ignore.js.map +1 -1
  43. package/dist/index.d.ts +24 -28
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +19 -134
  46. package/dist/index.js.map +1 -1
  47. package/dist/journal.d.ts +20 -4
  48. package/dist/journal.d.ts.map +1 -1
  49. package/dist/journal.js +45 -8
  50. package/dist/journal.js.map +1 -1
  51. package/dist/journal.test.d.ts +9 -0
  52. package/dist/journal.test.d.ts.map +1 -0
  53. package/dist/journal.test.js +114 -0
  54. package/dist/journal.test.js.map +1 -0
  55. package/dist/s3.d.ts +18 -6
  56. package/dist/s3.d.ts.map +1 -1
  57. package/dist/s3.js +57 -56
  58. package/dist/s3.js.map +1 -1
  59. package/dist/types.d.ts +34 -0
  60. package/dist/types.d.ts.map +1 -1
  61. package/dist/vault-client.d.ts +59 -0
  62. package/dist/vault-client.d.ts.map +1 -1
  63. package/dist/vault-client.js +72 -0
  64. package/dist/vault-client.js.map +1 -1
  65. package/dist/vault-client.test.js +160 -0
  66. package/dist/vault-client.test.js.map +1 -1
  67. package/dist/watcher.d.ts +7 -1
  68. package/dist/watcher.d.ts.map +1 -1
  69. package/dist/watcher.js +11 -5
  70. package/dist/watcher.js.map +1 -1
  71. package/package.json +15 -3
  72. package/src/bin/sync-runner.test.ts +804 -0
  73. package/src/bin/sync-runner.ts +499 -0
  74. package/src/cli/accept.ts +97 -0
  75. package/src/cli/conflict.ts +119 -0
  76. package/src/cli/index.ts +25 -0
  77. package/src/cli/invite.test.ts +247 -0
  78. package/src/cli/invite.ts +180 -0
  79. package/src/cli/promote.ts +123 -0
  80. package/src/cli/share.test.ts +155 -0
  81. package/src/cli/share.ts +212 -0
  82. package/src/cli/sync.test.ts +225 -0
  83. package/src/cli/sync.ts +225 -0
  84. package/src/cognito-auth.test.ts +156 -0
  85. package/src/cognito-auth.ts +18 -1
  86. package/src/context.test.ts +202 -0
  87. package/src/context.ts +178 -0
  88. package/src/daemon-worker.ts +13 -19
  89. package/src/daemon.ts +2 -0
  90. package/src/ignore.ts +76 -12
  91. package/src/index.ts +94 -165
  92. package/src/journal.test.ts +146 -0
  93. package/src/journal.ts +53 -11
  94. package/src/s3.ts +76 -66
  95. package/src/types.ts +37 -0
  96. package/src/vault-client.test.ts +563 -0
  97. package/src/vault-client.ts +478 -0
  98. package/src/watcher.ts +12 -5
  99. package/test/invite-flow.integration.test.ts +244 -0
  100. package/test/share-sync.integration.test.ts +210 -0
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Conflict resolution for hq share/sync (VLT-5 US-002).
3
+ *
4
+ * Interactive prompts in terminal mode; deterministic resolution via
5
+ * --on-conflict flag for worker/skill callers.
6
+ */
7
+
8
+ import * as fs from "fs";
9
+ import * as readline from "readline";
10
+
11
+ export type ConflictStrategy = "overwrite" | "keep" | "abort";
12
+
13
+ export interface ConflictInfo {
14
+ path: string;
15
+ localHash?: string;
16
+ remoteHash?: string;
17
+ localModified?: Date;
18
+ remoteModified?: Date;
19
+ direction: "push" | "pull";
20
+ }
21
+
22
+ export type ConflictResolution = "overwrite" | "keep" | "skip" | "diff" | "abort";
23
+
24
+ /**
25
+ * Resolve a conflict interactively or via strategy flag.
26
+ *
27
+ * In non-interactive mode (strategy provided), returns deterministically:
28
+ * overwrite → "overwrite"
29
+ * keep → "keep"
30
+ * abort → "abort"
31
+ *
32
+ * In interactive mode (strategy undefined), prompts the user.
33
+ */
34
+ export async function resolveConflict(
35
+ conflict: ConflictInfo,
36
+ strategy?: ConflictStrategy,
37
+ ): Promise<ConflictResolution> {
38
+ if (strategy) {
39
+ return strategy === "abort" ? "abort" : strategy;
40
+ }
41
+
42
+ return promptConflict(conflict);
43
+ }
44
+
45
+ async function promptConflict(conflict: ConflictInfo): Promise<ConflictResolution> {
46
+ const rl = readline.createInterface({
47
+ input: process.stdin,
48
+ output: process.stderr,
49
+ });
50
+
51
+ const direction = conflict.direction === "push"
52
+ ? "Remote has a newer version"
53
+ : "Local file has uncommitted edits";
54
+
55
+ console.error(`\n Conflict: ${conflict.path}`);
56
+ console.error(` ${direction}`);
57
+ if (conflict.localModified) {
58
+ console.error(` Local modified: ${conflict.localModified.toISOString()}`);
59
+ }
60
+ if (conflict.remoteModified) {
61
+ console.error(` Remote modified: ${conflict.remoteModified.toISOString()}`);
62
+ }
63
+
64
+ const options = conflict.direction === "push"
65
+ ? "[o]verwrite remote / [k]eep remote / [d]iff / [a]bort"
66
+ : "[o]verwrite local / [k]eep local / [d]iff / [s]kip";
67
+
68
+ const answer = await new Promise<string>((resolve) => {
69
+ rl.question(` ${options}: `, (ans) => {
70
+ rl.close();
71
+ resolve(ans.trim().toLowerCase());
72
+ });
73
+ });
74
+
75
+ switch (answer) {
76
+ case "o":
77
+ case "overwrite":
78
+ return "overwrite";
79
+ case "k":
80
+ case "keep":
81
+ return "keep";
82
+ case "d":
83
+ case "diff":
84
+ return "diff";
85
+ case "s":
86
+ case "skip":
87
+ return "skip";
88
+ case "a":
89
+ case "abort":
90
+ return "abort";
91
+ default:
92
+ // Default to keep (safe option)
93
+ console.error(" Unrecognized choice, keeping current version.");
94
+ return "keep";
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Show a simple diff between local and remote content.
100
+ * Returns the content strings for display.
101
+ */
102
+ export function showDiff(
103
+ localPath: string,
104
+ remoteContent: Buffer,
105
+ ): void {
106
+ const localContent = fs.existsSync(localPath)
107
+ ? fs.readFileSync(localPath, "utf-8")
108
+ : "(file does not exist locally)";
109
+ const remoteStr = remoteContent.toString("utf-8");
110
+
111
+ console.error("\n--- LOCAL ---");
112
+ console.error(localContent.slice(0, 2000));
113
+ if (localContent.length > 2000) console.error("... (truncated)");
114
+
115
+ console.error("\n--- REMOTE ---");
116
+ console.error(remoteStr.slice(0, 2000));
117
+ if (remoteStr.length > 2000) console.error("... (truncated)");
118
+ console.error("");
119
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * hq-cloud CLI entry points.
3
+ *
4
+ * Registers `hq share`, `hq sync`, and membership commands.
5
+ * These are consumed by @indigoai-us/hq-cli or invoked directly.
6
+ */
7
+
8
+ export { share } from "./share.js";
9
+ export type { ShareOptions, ShareResult } from "./share.js";
10
+
11
+ export { sync } from "./sync.js";
12
+ export type { SyncOptions, SyncResult, SyncProgressEvent } from "./sync.js";
13
+
14
+ export { resolveConflict, showDiff } from "./conflict.js";
15
+ export type { ConflictStrategy, ConflictInfo, ConflictResolution } from "./conflict.js";
16
+
17
+ // Membership commands (VLT-7)
18
+ export { invite, listInvites, revokeInvite } from "./invite.js";
19
+ export type { InviteOptions, InviteResult, InviteListOptions, InviteRevokeOptions } from "./invite.js";
20
+
21
+ export { accept, parseToken } from "./accept.js";
22
+ export type { AcceptOptions, AcceptResult } from "./accept.js";
23
+
24
+ export { promote } from "./promote.js";
25
+ export type { PromoteOptions, PromoteResult } from "./promote.js";
@@ -0,0 +1,247 @@
1
+ /**
2
+ * invite CLI command tests (VLT-7 US-002).
3
+ */
4
+
5
+ import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from "vitest";
6
+ import { invite, listInvites, revokeInvite } from "./invite.js";
7
+ import type { VaultServiceConfig } from "../types.js";
8
+
9
+ // ---------------------------------------------------------------------------
10
+ // Helpers
11
+ // ---------------------------------------------------------------------------
12
+
13
+ function jsonResponse(status: number, body: unknown): Response {
14
+ return new Response(JSON.stringify(body), {
15
+ status,
16
+ headers: { "Content-Type": "application/json" },
17
+ });
18
+ }
19
+
20
+ const VAULT_CONFIG: VaultServiceConfig = {
21
+ apiUrl: "https://vault.test.example.com",
22
+ authToken: "test-token",
23
+ };
24
+
25
+ let fetchSpy: MockInstance<typeof fetch>;
26
+
27
+ beforeEach(() => {
28
+ fetchSpy = vi.spyOn(globalThis, "fetch");
29
+ });
30
+
31
+ afterEach(() => {
32
+ vi.restoreAllMocks();
33
+ });
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // invite()
37
+ // ---------------------------------------------------------------------------
38
+
39
+ describe("invite", () => {
40
+ it("creates invite for email target and returns magic link", async () => {
41
+ // First call: entity.findBySlug to resolve company
42
+ fetchSpy
43
+ .mockResolvedValueOnce(
44
+ jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
45
+ )
46
+ // Second call: createInvite
47
+ .mockResolvedValueOnce(
48
+ jsonResponse(200, {
49
+ membership: { membershipKey: "psn_1#cmp_abc", role: "member", status: "pending" },
50
+ inviteToken: "tok_secure123",
51
+ }),
52
+ );
53
+
54
+ const result = await invite({
55
+ target: "alice@example.com",
56
+ role: "member",
57
+ company: "acme",
58
+ vaultConfig: VAULT_CONFIG,
59
+ callerUid: "psn_admin",
60
+ });
61
+
62
+ expect(result.magicLink).toBe("hq://accept/tok_secure123");
63
+ expect(result.inviteToken).toBe("tok_secure123");
64
+ expect(result.membership.status).toBe("pending");
65
+ });
66
+
67
+ it("creates invite for person UID target", async () => {
68
+ // Company is already a UID — no entity lookup needed
69
+ fetchSpy.mockResolvedValueOnce(
70
+ jsonResponse(200, {
71
+ membership: { membershipKey: "psn_bob#cmp_abc", role: "admin", status: "pending" },
72
+ inviteToken: "tok_456",
73
+ }),
74
+ );
75
+
76
+ const result = await invite({
77
+ target: "psn_bob",
78
+ role: "admin",
79
+ company: "cmp_abc",
80
+ vaultConfig: VAULT_CONFIG,
81
+ callerUid: "psn_admin",
82
+ });
83
+
84
+ expect(result.magicLink).toBe("hq://accept/tok_456");
85
+
86
+ // Should have called createInvite with personUid, not inviteeEmail
87
+ const body = JSON.parse(fetchSpy.mock.calls[0][1]?.body as string);
88
+ expect(body.personUid).toBe("psn_bob");
89
+ expect(body.inviteeEmail).toBeUndefined();
90
+ });
91
+
92
+ it("rejects --paths without --role guest", async () => {
93
+ await expect(
94
+ invite({
95
+ target: "alice@example.com",
96
+ role: "member",
97
+ paths: "docs/",
98
+ company: "acme",
99
+ vaultConfig: VAULT_CONFIG,
100
+ callerUid: "psn_admin",
101
+ }),
102
+ ).rejects.toThrow("--paths is only valid with --role guest");
103
+ });
104
+
105
+ it("allows --paths with --role guest", async () => {
106
+ fetchSpy
107
+ .mockResolvedValueOnce(
108
+ jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
109
+ )
110
+ .mockResolvedValueOnce(
111
+ jsonResponse(200, {
112
+ membership: { membershipKey: "psn_1#cmp_abc", role: "guest", status: "pending", allowedPrefixes: ["docs/", "shared/"] },
113
+ inviteToken: "tok_guest",
114
+ }),
115
+ );
116
+
117
+ const result = await invite({
118
+ target: "alice@example.com",
119
+ role: "guest",
120
+ paths: "docs/, shared/",
121
+ company: "acme",
122
+ vaultConfig: VAULT_CONFIG,
123
+ callerUid: "psn_admin",
124
+ });
125
+
126
+ expect(result.membership.allowedPrefixes).toEqual(["docs/", "shared/"]);
127
+
128
+ // Verify allowedPrefixes sent to API
129
+ const body = JSON.parse(fetchSpy.mock.calls[1][1]?.body as string);
130
+ expect(body.allowedPrefixes).toEqual(["docs/", "shared/"]);
131
+ });
132
+
133
+ it("maps VaultPermissionDeniedError to human-readable message", async () => {
134
+ fetchSpy
135
+ .mockResolvedValueOnce(
136
+ jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
137
+ )
138
+ .mockResolvedValueOnce(
139
+ jsonResponse(403, { message: "Admin required" }),
140
+ );
141
+
142
+ await expect(
143
+ invite({
144
+ target: "alice@example.com",
145
+ company: "acme",
146
+ vaultConfig: VAULT_CONFIG,
147
+ callerUid: "psn_member",
148
+ }),
149
+ ).rejects.toThrow("Permission denied — only admins and owners can invite members");
150
+ });
151
+
152
+ it("throws when no company specified", async () => {
153
+ await expect(
154
+ invite({
155
+ target: "alice@example.com",
156
+ vaultConfig: VAULT_CONFIG,
157
+ callerUid: "psn_admin",
158
+ }),
159
+ ).rejects.toThrow("No company specified");
160
+ });
161
+
162
+ it("maps VaultConflictError to human-readable message", async () => {
163
+ fetchSpy
164
+ .mockResolvedValueOnce(
165
+ jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
166
+ )
167
+ .mockResolvedValueOnce(
168
+ jsonResponse(409, { message: "Already exists" }),
169
+ );
170
+
171
+ await expect(
172
+ invite({
173
+ target: "alice@example.com",
174
+ company: "acme",
175
+ vaultConfig: VAULT_CONFIG,
176
+ callerUid: "psn_admin",
177
+ }),
178
+ ).rejects.toThrow("already has a membership or pending invite");
179
+ });
180
+ });
181
+
182
+ // ---------------------------------------------------------------------------
183
+ // listInvites()
184
+ // ---------------------------------------------------------------------------
185
+
186
+ describe("listInvites", () => {
187
+ it("returns pending invites for a company", async () => {
188
+ fetchSpy
189
+ .mockResolvedValueOnce(
190
+ jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
191
+ )
192
+ .mockResolvedValueOnce(
193
+ jsonResponse(200, {
194
+ invites: [
195
+ { membershipKey: "psn_1#cmp_abc", status: "pending", role: "member" },
196
+ { membershipKey: "psn_2#cmp_abc", status: "pending", role: "guest" },
197
+ ],
198
+ }),
199
+ );
200
+
201
+ const invites = await listInvites({
202
+ company: "acme",
203
+ vaultConfig: VAULT_CONFIG,
204
+ callerUid: "psn_admin",
205
+ });
206
+
207
+ expect(invites).toHaveLength(2);
208
+ });
209
+ });
210
+
211
+ // ---------------------------------------------------------------------------
212
+ // revokeInvite()
213
+ // ---------------------------------------------------------------------------
214
+
215
+ describe("revokeInvite", () => {
216
+ it("revokes a pending invite", async () => {
217
+ fetchSpy
218
+ .mockResolvedValueOnce(
219
+ jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
220
+ )
221
+ .mockResolvedValueOnce(new Response(null, { status: 204 }));
222
+
223
+ await expect(
224
+ revokeInvite({
225
+ tokenOrKey: "psn_1#cmp_abc",
226
+ company: "acme",
227
+ vaultConfig: VAULT_CONFIG,
228
+ }),
229
+ ).resolves.toBeUndefined();
230
+ });
231
+
232
+ it("maps 404 to human-readable message", async () => {
233
+ fetchSpy
234
+ .mockResolvedValueOnce(
235
+ jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
236
+ )
237
+ .mockResolvedValueOnce(jsonResponse(404, { message: "Not found" }));
238
+
239
+ await expect(
240
+ revokeInvite({
241
+ tokenOrKey: "psn_1#cmp_abc",
242
+ company: "acme",
243
+ vaultConfig: VAULT_CONFIG,
244
+ }),
245
+ ).rejects.toThrow("Invite not found");
246
+ });
247
+ });
@@ -0,0 +1,180 @@
1
+ /**
2
+ * `hq invite` command — create pending membership + magic link (VLT-7 US-002).
3
+ *
4
+ * Thin UX layer over VaultClient.createInvite(). Handles arg parsing,
5
+ * validation (paths only with guest role), and formats the magic link output.
6
+ */
7
+
8
+ import type { VaultServiceConfig } from "../types.js";
9
+ import {
10
+ VaultClient,
11
+ VaultAuthError,
12
+ VaultPermissionDeniedError,
13
+ VaultNotFoundError,
14
+ VaultConflictError,
15
+ } from "../vault-client.js";
16
+ import type { MembershipRole, Membership } from "../vault-client.js";
17
+
18
+ export interface InviteOptions {
19
+ /** Target — email address or person slug/uid */
20
+ target: string;
21
+ /** Role for the invitee (default: member) */
22
+ role?: MembershipRole;
23
+ /** Comma-separated allowed prefixes (only valid with role=guest) */
24
+ paths?: string;
25
+ /** Company slug or UID (defaults to active company) */
26
+ company?: string;
27
+ /** Vault service config */
28
+ vaultConfig: VaultServiceConfig;
29
+ /** Caller's person UID */
30
+ callerUid: string;
31
+ }
32
+
33
+ export interface InviteResult {
34
+ inviteToken: string;
35
+ magicLink: string;
36
+ membership: Membership;
37
+ }
38
+
39
+ export interface InviteListOptions {
40
+ company?: string;
41
+ vaultConfig: VaultServiceConfig;
42
+ callerUid: string;
43
+ }
44
+
45
+ export interface InviteRevokeOptions {
46
+ tokenOrKey: string;
47
+ /** Company slug or UID — required so the server can authorize the caller */
48
+ company: string;
49
+ vaultConfig: VaultServiceConfig;
50
+ }
51
+
52
+ /**
53
+ * Create a pending membership invite and return a magic link.
54
+ */
55
+ export async function invite(options: InviteOptions): Promise<InviteResult> {
56
+ const { target, role = "member", paths, company, vaultConfig, callerUid } = options;
57
+
58
+ // Validate: --paths only with --role guest
59
+ if (paths && role !== "guest") {
60
+ throw new Error("--paths is only valid with --role guest (allowedPrefixes are only meaningful for the guest role)");
61
+ }
62
+
63
+ const client = new VaultClient(vaultConfig);
64
+
65
+ // Resolve company UID
66
+ const companyUid = await resolveCompanyUid(client, company);
67
+
68
+ // Parse paths
69
+ const allowedPrefixes = paths
70
+ ? paths.split(",").map((p) => p.trim()).filter(Boolean)
71
+ : undefined;
72
+
73
+ // Determine if target is email or person identifier
74
+ const isEmail = target.includes("@");
75
+
76
+ try {
77
+ const result = await client.createInvite({
78
+ ...(isEmail ? { inviteeEmail: target } : { personUid: target }),
79
+ companyUid,
80
+ role,
81
+ allowedPrefixes,
82
+ invitedBy: callerUid,
83
+ });
84
+
85
+ const magicLink = `hq://accept/${result.inviteToken}`;
86
+
87
+ return {
88
+ inviteToken: result.inviteToken,
89
+ magicLink,
90
+ membership: result.membership,
91
+ };
92
+ } catch (err) {
93
+ if (err instanceof VaultAuthError) {
94
+ throw new Error("Authentication failed — run `hq auth` to refresh your session");
95
+ }
96
+ if (err instanceof VaultPermissionDeniedError) {
97
+ throw new Error("Permission denied — only admins and owners can invite members");
98
+ }
99
+ if (err instanceof VaultConflictError) {
100
+ throw new Error("This person already has a membership or pending invite for this company");
101
+ }
102
+ throw err;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * List pending invites for a company.
108
+ */
109
+ export async function listInvites(options: InviteListOptions): Promise<Membership[]> {
110
+ const { company, vaultConfig } = options;
111
+ const client = new VaultClient(vaultConfig);
112
+ const companyUid = await resolveCompanyUid(client, company);
113
+
114
+ try {
115
+ return await client.listPendingInvites(companyUid);
116
+ } catch (err) {
117
+ if (err instanceof VaultAuthError) {
118
+ throw new Error("Authentication failed — run `hq auth` to refresh your session");
119
+ }
120
+ if (err instanceof VaultPermissionDeniedError) {
121
+ throw new Error("Permission denied — only admins and owners can list invites");
122
+ }
123
+ throw err;
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Revoke a pending invite.
129
+ */
130
+ export async function revokeInvite(options: InviteRevokeOptions): Promise<void> {
131
+ const { tokenOrKey, company, vaultConfig } = options;
132
+ const client = new VaultClient(vaultConfig);
133
+ const companyUid = await resolveCompanyUid(client, company);
134
+
135
+ try {
136
+ await client.revokeMembership(tokenOrKey, companyUid);
137
+ } catch (err) {
138
+ if (err instanceof VaultAuthError) {
139
+ throw new Error("Authentication failed — run `hq auth` to refresh your session");
140
+ }
141
+ if (err instanceof VaultPermissionDeniedError) {
142
+ throw new Error("Permission denied — only admins and owners can revoke invites");
143
+ }
144
+ if (err instanceof VaultNotFoundError) {
145
+ throw new Error("Invite not found — it may have already been accepted or revoked");
146
+ }
147
+ throw err;
148
+ }
149
+ }
150
+
151
+ // ---------------------------------------------------------------------------
152
+ // Helpers
153
+ // ---------------------------------------------------------------------------
154
+
155
+ async function resolveCompanyUid(
156
+ client: VaultClient,
157
+ companyRef?: string,
158
+ ): Promise<string> {
159
+ if (!companyRef) {
160
+ throw new Error(
161
+ "No company specified. Use --company <slug> or set up .hq/config.json",
162
+ );
163
+ }
164
+
165
+ // If already a UID, return it
166
+ if (companyRef.startsWith("cmp_")) {
167
+ return companyRef;
168
+ }
169
+
170
+ // Resolve slug → UID via entity registry
171
+ try {
172
+ const entity = await client.entity.findBySlug("company", companyRef);
173
+ return entity.uid;
174
+ } catch (err) {
175
+ if (err instanceof VaultNotFoundError) {
176
+ throw new Error(`Company "${companyRef}" not found in the vault registry`);
177
+ }
178
+ throw err;
179
+ }
180
+ }
@@ -0,0 +1,123 @@
1
+ /**
2
+ * `hq promote` command — change an existing member's role (VLT-7 US-003).
3
+ *
4
+ * Admin+ only. Surfaces last-owner demotion errors as human messages.
5
+ */
6
+
7
+ import type { VaultServiceConfig } from "../types.js";
8
+ import {
9
+ VaultClient,
10
+ VaultAuthError,
11
+ VaultPermissionDeniedError,
12
+ VaultNotFoundError,
13
+ VaultConflictError,
14
+ } from "../vault-client.js";
15
+ import type { MembershipRole, Membership } from "../vault-client.js";
16
+
17
+ export interface PromoteOptions {
18
+ /** Person slug or UID of the member to promote */
19
+ target: string;
20
+ /** New role to assign */
21
+ newRole: MembershipRole;
22
+ /** Allowed prefixes (only valid with guest role) */
23
+ paths?: string;
24
+ /** Company slug or UID */
25
+ company?: string;
26
+ /** Caller's person UID */
27
+ callerUid: string;
28
+ /** Vault service config */
29
+ vaultConfig: VaultServiceConfig;
30
+ }
31
+
32
+ export interface PromoteResult {
33
+ membership: Membership;
34
+ previousRole?: MembershipRole;
35
+ }
36
+
37
+ /**
38
+ * Change a member's role.
39
+ */
40
+ export async function promote(options: PromoteOptions): Promise<PromoteResult> {
41
+ const { target, newRole, paths, company, callerUid, vaultConfig } = options;
42
+
43
+ // Validate: --paths only with guest role
44
+ if (paths && newRole !== "guest") {
45
+ throw new Error("--paths is only valid with --role guest (allowedPrefixes are only meaningful for the guest role)");
46
+ }
47
+
48
+ const client = new VaultClient(vaultConfig);
49
+
50
+ // Resolve company UID
51
+ const companyUid = await resolveCompanyUid(client, company);
52
+
53
+ // Build membership key from target + company
54
+ const membershipKey = buildMembershipKey(target, companyUid);
55
+
56
+ const allowedPrefixes = paths
57
+ ? paths.split(",").map((p) => p.trim()).filter(Boolean)
58
+ : undefined;
59
+
60
+ try {
61
+ const membership = await client.updateRole({
62
+ membershipKey,
63
+ newRole,
64
+ allowedPrefixes,
65
+ updaterUid: callerUid,
66
+ companyUid,
67
+ });
68
+
69
+ return { membership };
70
+ } catch (err) {
71
+ if (err instanceof VaultAuthError) {
72
+ throw new Error("Authentication failed — run `hq auth` to refresh your session");
73
+ }
74
+ if (err instanceof VaultPermissionDeniedError) {
75
+ throw new Error("Permission denied — only admins and owners can change member roles");
76
+ }
77
+ if (err instanceof VaultNotFoundError) {
78
+ throw new Error(`Member "${target}" not found in this company`);
79
+ }
80
+ if (err instanceof VaultConflictError) {
81
+ throw new Error("Cannot leave company without an owner — promote another member to owner first");
82
+ }
83
+ throw err;
84
+ }
85
+ }
86
+
87
+ // ---------------------------------------------------------------------------
88
+ // Helpers
89
+ // ---------------------------------------------------------------------------
90
+
91
+ function buildMembershipKey(personRef: string, companyUid: string): string {
92
+ // If already a composite key, use as-is
93
+ if (personRef.includes("#")) {
94
+ return personRef;
95
+ }
96
+ // Build composite key: personUid#companyUid
97
+ return `${personRef}#${companyUid}`;
98
+ }
99
+
100
+ async function resolveCompanyUid(
101
+ client: VaultClient,
102
+ companyRef?: string,
103
+ ): Promise<string> {
104
+ if (!companyRef) {
105
+ throw new Error(
106
+ "No company specified. Use --company <slug> or set up .hq/config.json",
107
+ );
108
+ }
109
+
110
+ if (companyRef.startsWith("cmp_")) {
111
+ return companyRef;
112
+ }
113
+
114
+ try {
115
+ const entity = await client.entity.findBySlug("company", companyRef);
116
+ return entity.uid;
117
+ } catch (err) {
118
+ if (err instanceof VaultNotFoundError) {
119
+ throw new Error(`Company "${companyRef}" not found in the vault registry`);
120
+ }
121
+ throw err;
122
+ }
123
+ }