@oxygen-agent/cli 1.632.3 → 1.662.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,228 @@
1
+ import { type TagKind } from "./tags.js";
2
+ /**
3
+ * Workspace objects that can carry a comment thread or an approval request.
4
+ *
5
+ * Seeded from {@link TAG_KINDS}: anything worth labelling for a campaign is
6
+ * worth discussing with the client who signs that campaign off. The list is
7
+ * RESTATED rather than aliased because it is also a stored value — the
8
+ * `ox_collab` CHECK constraint and existing rows pin it — so widening the tag
9
+ * vocabulary must be a deliberate decision here, not a silent side effect that
10
+ * lets the API accept a kind the database rejects. The alignment is enforced
11
+ * both ways: {@link EveryTagKindIsCommentable} fails the build if a taggable
12
+ * kind is missing, and the paired test pins today's list exactly.
13
+ */
14
+ export declare const COLLAB_SUBJECT_KINDS: readonly ["knowledge_page", "publishing_post", "sequence", "table", "workflow", "recipe", "conversation", "mailbox", "sender", "record", "domain", "project"];
15
+ export type CollabSubjectKind = (typeof COLLAB_SUBJECT_KINDS)[number];
16
+ /** Compile-time assertion helper: instantiating with `false` is a type error. */
17
+ type AssertTrue<T extends true> = T;
18
+ /**
19
+ * Build-time proof that every taggable kind is also commentable. Adding a kind
20
+ * to {@link TAG_KINDS} without adding it here is a compile error, which is the
21
+ * point: the alternative is an API that accepts `--on <newkind>:<id>` and a
22
+ * database that rejects the insert.
23
+ *
24
+ * Divergence is legal in the other direction only — Collaboration may grow a
25
+ * subject kind that carries no tags.
26
+ */
27
+ export type EveryTagKindIsCommentable = AssertTrue<TagKind extends CollabSubjectKind ? true : false>;
28
+ export declare function isCollabSubjectKind(value: unknown): value is CollabSubjectKind;
29
+ /**
30
+ * How each subject kind is named in output. Total over CollabSubjectKind, so
31
+ * adding a kind is a compile error until it is labelled — the guarantee
32
+ * `TAG_KIND_LABELS` had to learn the hard way when three hand-maintained copies
33
+ * of the same map drifted apart.
34
+ *
35
+ * `many` keeps proper nouns intact ("CRM records") so an inline count reads
36
+ * correctly; headings run it through {@link collabSubjectHeading}, which only
37
+ * uppercases a leading lowercase letter.
38
+ */
39
+ export declare const COLLAB_SUBJECT_LABELS: Record<CollabSubjectKind, {
40
+ one: string;
41
+ many: string;
42
+ }>;
43
+ /**
44
+ * What a thread on each kind is usually about, for `--help` and MCP tool
45
+ * descriptions. Total over CollabSubjectKind for the same reason the labels
46
+ * are: a blind-user eval (2026-07-21) found `tags --help` still advertising 7
47
+ * of 13 kinds because the prose was hand-written beside a hand-maintained
48
+ * marker map, so six taggable kinds were invisible from the one place a user
49
+ * looks. Here the help text cannot omit a kind — the compiler rejects the map
50
+ * until the kind is glossed, and {@link COLLAB_SUBJECT_KINDS_PROSE} is DERIVED
51
+ * from the labels rather than typed out a second time.
52
+ */
53
+ export declare const COLLAB_SUBJECT_PROSE: Record<CollabSubjectKind, string>;
54
+ /**
55
+ * Every commentable kind in prose, for CLI `--help` and MCP tool descriptions.
56
+ * Derived from {@link COLLAB_SUBJECT_LABELS} on purpose: a hand-written copy is
57
+ * exactly what drifted in the tags help.
58
+ */
59
+ export declare const COLLAB_SUBJECT_KINDS_PROSE: string;
60
+ /** Inline label for a count: `collabSubjectLabel("sequence", 1)` -> "sequence". */
61
+ export declare function collabSubjectLabel(kind: string, count: number): string;
62
+ /** Section heading for a kind: "Sequences", "CRM records". */
63
+ export declare function collabSubjectHeading(kind: string): string;
64
+ /** A thread's or request's subject: which kind of object, and which one. */
65
+ export type CollabSubjectRef = {
66
+ kind: CollabSubjectKind;
67
+ id: string;
68
+ };
69
+ /**
70
+ * Render a subject as the `<kind>:<id>` string every surface accepts
71
+ * (`--on sequence:9f1c...`). Throws rather than emitting an unparseable ref,
72
+ * because a malformed ref written into a stored row is drift nobody notices
73
+ * until a read fails.
74
+ */
75
+ export declare function formatSubjectRef(ref: CollabSubjectRef): string;
76
+ /**
77
+ * Parse `<kind>:<id>`. Splits on the FIRST colon only, so an id that itself
78
+ * contains colons survives the round trip; an input with no colon at all is
79
+ * rejected rather than being guessed at (there is no default kind — a bare id
80
+ * would silently address the wrong store).
81
+ */
82
+ export declare function tryParseSubjectRef(value: unknown): CollabSubjectRef | null;
83
+ /** Throwing variant of {@link tryParseSubjectRef}, for surfaces that want the typed error. */
84
+ export declare function parseSubjectRef(value: unknown): CollabSubjectRef;
85
+ /**
86
+ * Sub-object addressing inside one subject, so commenting on a single cell does
87
+ * not need a thread table per kind: `row:<rowId>` for a whole row,
88
+ * `row:<rowId>#<columnKey>` for one cell, `column:<columnKey>` for a whole
89
+ * column. Unknown prefixes are still rejected rather than stored — an address no
90
+ * reader understands is worse than a refusal.
91
+ *
92
+ * `column:` was added in the second Collaboration pass because a column is what
93
+ * an agency and its client actually argue about ("why is Stage set this way?"),
94
+ * and it had no address at all: every path had to name a row first. It is a
95
+ * SIBLING of `row:`, not a variant of it, which is why the type below is a
96
+ * discriminated union — a column path has no row, and an optional `rowId` would
97
+ * let `{}` typecheck as a valid path.
98
+ */
99
+ export declare const SUBJECT_PATH_ROW_PREFIX = "row:";
100
+ export declare const SUBJECT_PATH_COLUMN_PREFIX = "column:";
101
+ export type CollabSubjectPath = {
102
+ kind: "row";
103
+ rowId: string;
104
+ columnKey?: string;
105
+ } | {
106
+ kind: "column";
107
+ columnKey: string;
108
+ };
109
+ /** Every path form in prose, for `--help`, MCP descriptors, and error messages. */
110
+ export declare const SUBJECT_PATH_FORMS_PROSE = "\"row:<rowId>\" for one row, \"row:<rowId>#<columnKey>\" for one cell, or \"column:<columnKey>\" for one column";
111
+ /**
112
+ * Render a sub-object path: `{ kind: "row", rowId: "abc" }` -> "row:abc"; with a
113
+ * column -> "row:abc#email"; `{ kind: "column", columnKey: "email" }` ->
114
+ * "column:email".
115
+ */
116
+ export declare function formatSubjectPath(path: CollabSubjectPath): string;
117
+ export declare function tryParseSubjectPath(value: unknown): CollabSubjectPath | null;
118
+ /** Throwing variant of {@link tryParseSubjectPath}. */
119
+ export declare function parseSubjectPath(value: unknown): CollabSubjectPath;
120
+ /**
121
+ * The gates a workspace can require. `launch` is the client's go/no-go before a
122
+ * campaign sends; `signoff` is their sign-off on the list that campaign sends
123
+ * to. Both are OPT-IN per subject (or per subject kind) — an unset gate leaves
124
+ * today's behavior untouched.
125
+ */
126
+ export declare const COLLAB_GATE_KINDS: readonly ["launch", "signoff"];
127
+ export type CollabGateKind = (typeof COLLAB_GATE_KINDS)[number];
128
+ export declare function isCollabGateKind(value: unknown): value is CollabGateKind;
129
+ /**
130
+ * Which subject kinds each gate can be required on. Total over CollabGateKind,
131
+ * so a new gate cannot ship without naming the primitives that must honor it —
132
+ * a gate nobody enforces is worse than no gate, because the workspace believes
133
+ * it is protected.
134
+ */
135
+ export declare const GATE_KIND_SUBJECTS: Record<CollabGateKind, readonly CollabSubjectKind[]>;
136
+ /** What each gate means, for `--help` and MCP descriptors. Total over CollabGateKind. */
137
+ export declare const COLLAB_GATE_PROSE: Record<CollabGateKind, string>;
138
+ export declare function isGateKindValidForSubject(gateKind: unknown, subjectKind: unknown): gateKind is CollabGateKind;
139
+ /** Every gate that can be required on a subject kind — drives `approvals gate list`. */
140
+ export declare function gateKindsForSubject(subjectKind: unknown): CollabGateKind[];
141
+ /** A thread is open until somebody resolves it; reopening flips it back. */
142
+ export declare const COLLAB_THREAD_STATUSES: readonly ["open", "resolved"];
143
+ export type CollabThreadStatus = (typeof COLLAB_THREAD_STATUSES)[number];
144
+ export declare function isCollabThreadStatus(value: unknown): value is CollabThreadStatus;
145
+ export declare const COLLAB_REQUEST_STATUSES: readonly ["pending", "approved", "changes_requested", "rejected", "cancelled", "expired"];
146
+ export type CollabRequestStatus = (typeof COLLAB_REQUEST_STATUSES)[number];
147
+ export declare function isCollabRequestStatus(value: unknown): value is CollabRequestStatus;
148
+ /**
149
+ * The whole feature hinges on this one line: ONLY `approved` opens a gate.
150
+ *
151
+ * `changes_requested` is a decision, and it ends the requester's wait, but it is
152
+ * not consent — the client asked for edits. Treating it as terminal-and-done is
153
+ * the obvious bug (a request that is no longer pending looks "handled"), so the
154
+ * gate check asks this function rather than testing `status !== "pending"`.
155
+ */
156
+ export declare function requestStatusOpensGate(status: unknown): boolean;
157
+ /** Statuses that no longer await a decision. `pending` is the only live state. */
158
+ export declare const COLLAB_REQUEST_TERMINAL_STATUSES: readonly CollabRequestStatus[];
159
+ export declare function isTerminalRequestStatus(status: unknown): boolean;
160
+ /**
161
+ * What an approver may do, as the CLI/MCP spells it — and the status each
162
+ * decision writes. Total over CollabDecision so the API, the CLI flags, and the
163
+ * MCP tool cannot each invent their own mapping (the failure mode being a
164
+ * `changes_requested` decision stored as `rejected`, which reads to the agency
165
+ * as "the client said no" rather than "the client wants edits").
166
+ */
167
+ export declare const COLLAB_DECISIONS: readonly ["approve", "reject", "changes_requested"];
168
+ export type CollabDecision = (typeof COLLAB_DECISIONS)[number];
169
+ export declare function isCollabDecision(value: unknown): value is CollabDecision;
170
+ export declare const COLLAB_DECISION_STATUSES: Record<CollabDecision, CollabRequestStatus>;
171
+ /** Why a notification exists. Delivery itself is the notifier's problem. */
172
+ export declare const COLLAB_NOTIFICATION_KINDS: readonly ["assigned", "mentioned", "decided", "replied"];
173
+ export type CollabNotificationKind = (typeof COLLAB_NOTIFICATION_KINDS)[number];
174
+ export declare function isCollabNotificationKind(value: unknown): value is CollabNotificationKind;
175
+ export declare const COLLAB_NOTIFICATION_STATUSES: readonly ["pending", "sent", "failed", "skipped"];
176
+ export type CollabNotificationStatus = (typeof COLLAB_NOTIFICATION_STATUSES)[number];
177
+ export declare function isCollabNotificationStatus(value: unknown): value is CollabNotificationStatus;
178
+ /**
179
+ * Body/field caps. Shared so the API, the CLI, and the tenant column widths
180
+ * agree — three independently chosen limits means the CLI accepts what the API
181
+ * truncates.
182
+ */
183
+ export declare const MAX_COMMENT_BODY_LENGTH = 10000;
184
+ export declare const MAX_THREAD_TITLE_LENGTH = 200;
185
+ export declare const MAX_DECISION_NOTE_LENGTH = 2000;
186
+ /** Bounds the notification fan-out one comment or request can trigger. */
187
+ export declare const MAX_MENTIONS_PER_COMMENT = 25;
188
+ export declare const MAX_ASSIGNEES_PER_REQUEST = 10;
189
+ /**
190
+ * Normalize one comment body: trim, reject blanks, non-strings, and anything
191
+ * over {@link MAX_COMMENT_BODY_LENGTH}. Returns `null` so each surface can raise
192
+ * its own typed error (the CLI wants an exit code, the API wants a status).
193
+ *
194
+ * Deliberately does NOT strip or rewrite the body beyond trimming — a comment is
195
+ * the client's words, and Markdown, quoted text, and whitespace layout are part
196
+ * of what they wrote.
197
+ */
198
+ export declare function normalizeCommentBody(value: unknown): string | null;
199
+ /**
200
+ * The Oxygen-side membership role overlay (control DB
201
+ * `organization_memberships.oxygen_role`). NULL means no override — today's
202
+ * behavior for every existing member — and `client` is the restricted role an
203
+ * agency hands its client: read the work, comment on it, decide the approvals
204
+ * assigned to them, and nothing else.
205
+ *
206
+ * Deliberately not a Clerk custom role: those need dashboard configuration, and
207
+ * repo doctrine requires asking a human before touching Clerk. Keep this list in
208
+ * step with the `organization_memberships_oxygen_role_check` constraint in
209
+ * control migration 0095 — the CHECK is what stops a typo from creating a role
210
+ * nothing enforces.
211
+ */
212
+ export declare const COLLAB_MEMBER_ROLES: readonly ["client"];
213
+ export type CollabMemberRole = (typeof COLLAB_MEMBER_ROLES)[number];
214
+ export declare function isCollabMemberRole(value: unknown): value is CollabMemberRole;
215
+ /**
216
+ * The CLERK workspace roles that carry admin authority (control DB
217
+ * `organization_memberships.role`). Both spellings of each: Clerk writes the
218
+ * `org:`-prefixed form, older rows carry the bare one.
219
+ *
220
+ * The overlay above and this list are the two halves of one question — "may this
221
+ * member do X" — and a Clerk admin's role OVERRIDES the overlay, which is why
222
+ * granting `client` to one is refused. Keeping the vocabulary here means the
223
+ * collaboration lib can ask without importing the billing graph, and there is
224
+ * one list to change if Clerk ever gains another admin-ish role.
225
+ */
226
+ export declare const WORKSPACE_ADMIN_ROLES: readonly ["admin", "owner", "org:admin", "org:owner"];
227
+ export declare function isWorkspaceAdminRole(role: string): boolean;
228
+ export {};
@@ -0,0 +1,410 @@
1
+ // Collaboration vocabulary (Control-layer interaction state, NOT a sixteenth
2
+ // primitive). A comment thread or an approval request is state ABOUT a GTM
3
+ // artifact — the artifact itself still belongs to its owning primitive, and the
4
+ // approval gate is honored by that primitive's own route (Sequences still owns
5
+ // whether a sequence launches). Same precedent ADR 0013 set for the Workspace
6
+ // Copilot: durable interaction state may own a tenant store, GTM primitive
7
+ // state may not.
8
+ //
9
+ // This module is the ONE vocabulary the tenant schema (`ox_collab`), the
10
+ // `/api/cli/collab/*` routes, the CLI groups, and the MCP tools all read from:
11
+ // which kinds can carry a thread, how a subject is addressed as a string, which
12
+ // gates apply to which kinds, and what the statuses are. Nothing here talks to a
13
+ // database or a transport — it is deliberately importable from every surface.
14
+ //
15
+ // Unlike Tags (a label that fits in a `text[]` column on the row it describes),
16
+ // a thread is an independent entity with its own author, body, replies, and
17
+ // lifecycle, so it is stored separately and addressed by an explicit subject
18
+ // reference — `<kind>:<id>` — rather than by a column on the subject.
19
+ import { OxygenError } from "./cli-result.js";
20
+ /**
21
+ * Workspace objects that can carry a comment thread or an approval request.
22
+ *
23
+ * Seeded from {@link TAG_KINDS}: anything worth labelling for a campaign is
24
+ * worth discussing with the client who signs that campaign off. The list is
25
+ * RESTATED rather than aliased because it is also a stored value — the
26
+ * `ox_collab` CHECK constraint and existing rows pin it — so widening the tag
27
+ * vocabulary must be a deliberate decision here, not a silent side effect that
28
+ * lets the API accept a kind the database rejects. The alignment is enforced
29
+ * both ways: {@link EveryTagKindIsCommentable} fails the build if a taggable
30
+ * kind is missing, and the paired test pins today's list exactly.
31
+ */
32
+ export const COLLAB_SUBJECT_KINDS = [
33
+ "knowledge_page",
34
+ "publishing_post",
35
+ "sequence",
36
+ "table",
37
+ "workflow",
38
+ "recipe",
39
+ "conversation",
40
+ "mailbox",
41
+ "sender",
42
+ "record",
43
+ "domain",
44
+ "project",
45
+ ];
46
+ export function isCollabSubjectKind(value) {
47
+ return typeof value === "string" && COLLAB_SUBJECT_KINDS.includes(value);
48
+ }
49
+ /**
50
+ * How each subject kind is named in output. Total over CollabSubjectKind, so
51
+ * adding a kind is a compile error until it is labelled — the guarantee
52
+ * `TAG_KIND_LABELS` had to learn the hard way when three hand-maintained copies
53
+ * of the same map drifted apart.
54
+ *
55
+ * `many` keeps proper nouns intact ("CRM records") so an inline count reads
56
+ * correctly; headings run it through {@link collabSubjectHeading}, which only
57
+ * uppercases a leading lowercase letter.
58
+ */
59
+ export const COLLAB_SUBJECT_LABELS = {
60
+ knowledge_page: { one: "knowledge page", many: "knowledge pages" },
61
+ publishing_post: { one: "publishing post", many: "publishing posts" },
62
+ sequence: { one: "sequence", many: "sequences" },
63
+ table: { one: "table", many: "tables" },
64
+ workflow: { one: "workflow", many: "workflows" },
65
+ recipe: { one: "recipe", many: "recipes" },
66
+ conversation: { one: "conversation", many: "conversations" },
67
+ mailbox: { one: "mailbox", many: "mailboxes" },
68
+ sender: { one: "sender account", many: "sender accounts" },
69
+ record: { one: "CRM record", many: "CRM records" },
70
+ domain: { one: "sending domain", many: "sending domains" },
71
+ project: { one: "project", many: "projects" },
72
+ };
73
+ /**
74
+ * What a thread on each kind is usually about, for `--help` and MCP tool
75
+ * descriptions. Total over CollabSubjectKind for the same reason the labels
76
+ * are: a blind-user eval (2026-07-21) found `tags --help` still advertising 7
77
+ * of 13 kinds because the prose was hand-written beside a hand-maintained
78
+ * marker map, so six taggable kinds were invisible from the one place a user
79
+ * looks. Here the help text cannot omit a kind — the compiler rejects the map
80
+ * until the kind is glossed, and {@link COLLAB_SUBJECT_KINDS_PROSE} is DERIVED
81
+ * from the labels rather than typed out a second time.
82
+ */
83
+ export const COLLAB_SUBJECT_PROSE = {
84
+ knowledge_page: "a wiki page's copy before it becomes canonical",
85
+ publishing_post: "a post's copy before it is published",
86
+ sequence: "a campaign's messaging before it launches",
87
+ table: "a list's rows and columns, or one cell's value",
88
+ workflow: "an automation's graph before it runs live",
89
+ recipe: "an installable motion before it is installed",
90
+ conversation: "a reply in an inbox thread before it is sent",
91
+ mailbox: "a mailbox's configuration or health",
92
+ sender: "a sender account's caps and warmup",
93
+ record: "a CRM record's truth",
94
+ domain: "a sending domain's DNS and warmup",
95
+ project: "a project's scope",
96
+ };
97
+ /** Oxford-comma list: ["a"] -> "a"; ["a","b"] -> "a and b"; ["a","b","c"] -> "a, b, and c". */
98
+ function formatProseList(items) {
99
+ if (items.length <= 1)
100
+ return items[0] ?? "";
101
+ if (items.length === 2)
102
+ return `${items[0]} and ${items[1]}`;
103
+ return `${items.slice(0, -1).join(", ")}, and ${items[items.length - 1]}`;
104
+ }
105
+ /**
106
+ * Every commentable kind in prose, for CLI `--help` and MCP tool descriptions.
107
+ * Derived from {@link COLLAB_SUBJECT_LABELS} on purpose: a hand-written copy is
108
+ * exactly what drifted in the tags help.
109
+ */
110
+ export const COLLAB_SUBJECT_KINDS_PROSE = formatProseList(COLLAB_SUBJECT_KINDS.map((kind) => COLLAB_SUBJECT_LABELS[kind].many));
111
+ /** Inline label for a count: `collabSubjectLabel("sequence", 1)` -> "sequence". */
112
+ export function collabSubjectLabel(kind, count) {
113
+ const entry = isCollabSubjectKind(kind) ? COLLAB_SUBJECT_LABELS[kind] : null;
114
+ if (!entry)
115
+ return kind;
116
+ return count === 1 ? entry.one : entry.many;
117
+ }
118
+ /** Section heading for a kind: "Sequences", "CRM records". */
119
+ export function collabSubjectHeading(kind) {
120
+ const entry = isCollabSubjectKind(kind) ? COLLAB_SUBJECT_LABELS[kind] : null;
121
+ const label = entry ? entry.many : kind;
122
+ return label.charAt(0).toUpperCase() + label.slice(1);
123
+ }
124
+ /**
125
+ * Render a subject as the `<kind>:<id>` string every surface accepts
126
+ * (`--on sequence:9f1c...`). Throws rather than emitting an unparseable ref,
127
+ * because a malformed ref written into a stored row is drift nobody notices
128
+ * until a read fails.
129
+ */
130
+ export function formatSubjectRef(ref) {
131
+ if (!isCollabSubjectKind(ref.kind)) {
132
+ throw invalidSubjectRef(`Unknown subject kind "${ref.kind}".`, { kind: ref.kind });
133
+ }
134
+ const id = ref.id.trim();
135
+ if (!id) {
136
+ throw invalidSubjectRef("Subject id is required.", { kind: ref.kind });
137
+ }
138
+ return `${ref.kind}:${id}`;
139
+ }
140
+ /**
141
+ * Parse `<kind>:<id>`. Splits on the FIRST colon only, so an id that itself
142
+ * contains colons survives the round trip; an input with no colon at all is
143
+ * rejected rather than being guessed at (there is no default kind — a bare id
144
+ * would silently address the wrong store).
145
+ */
146
+ export function tryParseSubjectRef(value) {
147
+ if (typeof value !== "string")
148
+ return null;
149
+ const raw = value.trim();
150
+ const separator = raw.indexOf(":");
151
+ if (separator <= 0)
152
+ return null;
153
+ const kind = raw.slice(0, separator);
154
+ const id = raw.slice(separator + 1).trim();
155
+ if (!isCollabSubjectKind(kind) || !id)
156
+ return null;
157
+ return { kind, id };
158
+ }
159
+ /** Throwing variant of {@link tryParseSubjectRef}, for surfaces that want the typed error. */
160
+ export function parseSubjectRef(value) {
161
+ const parsed = tryParseSubjectRef(value);
162
+ if (parsed)
163
+ return parsed;
164
+ throw invalidSubjectRef(`Subject must be "<kind>:<id>" — for example "sequence:9f1c2b7e". Valid kinds: ${COLLAB_SUBJECT_KINDS.join(", ")}.`, { subject: typeof value === "string" ? value : null });
165
+ }
166
+ function invalidSubjectRef(message, details) {
167
+ return new OxygenError("invalid_subject_ref", message, {
168
+ details: { ...details, valid_kinds: [...COLLAB_SUBJECT_KINDS] },
169
+ exitCode: 2,
170
+ });
171
+ }
172
+ /**
173
+ * Sub-object addressing inside one subject, so commenting on a single cell does
174
+ * not need a thread table per kind: `row:<rowId>` for a whole row,
175
+ * `row:<rowId>#<columnKey>` for one cell, `column:<columnKey>` for a whole
176
+ * column. Unknown prefixes are still rejected rather than stored — an address no
177
+ * reader understands is worse than a refusal.
178
+ *
179
+ * `column:` was added in the second Collaboration pass because a column is what
180
+ * an agency and its client actually argue about ("why is Stage set this way?"),
181
+ * and it had no address at all: every path had to name a row first. It is a
182
+ * SIBLING of `row:`, not a variant of it, which is why the type below is a
183
+ * discriminated union — a column path has no row, and an optional `rowId` would
184
+ * let `{}` typecheck as a valid path.
185
+ */
186
+ export const SUBJECT_PATH_ROW_PREFIX = "row:";
187
+ export const SUBJECT_PATH_COLUMN_PREFIX = "column:";
188
+ /** Every path form in prose, for `--help`, MCP descriptors, and error messages. */
189
+ export const SUBJECT_PATH_FORMS_PROSE = '"row:<rowId>" for one row, "row:<rowId>#<columnKey>" for one cell, or "column:<columnKey>" for one column';
190
+ /**
191
+ * Render a sub-object path: `{ kind: "row", rowId: "abc" }` -> "row:abc"; with a
192
+ * column -> "row:abc#email"; `{ kind: "column", columnKey: "email" }` ->
193
+ * "column:email".
194
+ */
195
+ export function formatSubjectPath(path) {
196
+ if (path.kind === "column") {
197
+ const columnKey = path.columnKey.trim();
198
+ if (!columnKey || columnKey.includes("#")) {
199
+ throw invalidSubjectPath('Column key is required and cannot contain "#".', {
200
+ column_key: path.columnKey,
201
+ });
202
+ }
203
+ return `${SUBJECT_PATH_COLUMN_PREFIX}${columnKey}`;
204
+ }
205
+ const rowId = path.rowId.trim();
206
+ if (!rowId || rowId.includes("#")) {
207
+ throw invalidSubjectPath('Row id is required and cannot contain "#".', { row_id: path.rowId });
208
+ }
209
+ const columnKey = path.columnKey?.trim() ?? "";
210
+ if (path.columnKey !== undefined && !columnKey) {
211
+ throw invalidSubjectPath("Column key cannot be blank.", { row_id: rowId });
212
+ }
213
+ const base = `${SUBJECT_PATH_ROW_PREFIX}${rowId}`;
214
+ return columnKey ? `${base}#${columnKey}` : base;
215
+ }
216
+ export function tryParseSubjectPath(value) {
217
+ if (typeof value !== "string")
218
+ return null;
219
+ const raw = value.trim();
220
+ if (raw.startsWith(SUBJECT_PATH_COLUMN_PREFIX)) {
221
+ const columnKey = raw.slice(SUBJECT_PATH_COLUMN_PREFIX.length).trim();
222
+ // No `#` inside a column path: a column addresses no row, so a hash here
223
+ // means the caller meant a cell and got the prefix wrong.
224
+ if (!columnKey || columnKey.includes("#"))
225
+ return null;
226
+ return { kind: "column", columnKey };
227
+ }
228
+ if (!raw.startsWith(SUBJECT_PATH_ROW_PREFIX))
229
+ return null;
230
+ const remainder = raw.slice(SUBJECT_PATH_ROW_PREFIX.length);
231
+ const hash = remainder.indexOf("#");
232
+ if (hash < 0) {
233
+ const rowId = remainder.trim();
234
+ return rowId ? { kind: "row", rowId } : null;
235
+ }
236
+ const rowId = remainder.slice(0, hash).trim();
237
+ const columnKey = remainder.slice(hash + 1).trim();
238
+ if (!rowId || !columnKey || columnKey.includes("#"))
239
+ return null;
240
+ return { kind: "row", rowId, columnKey };
241
+ }
242
+ /** Throwing variant of {@link tryParseSubjectPath}. */
243
+ export function parseSubjectPath(value) {
244
+ const parsed = tryParseSubjectPath(value);
245
+ if (parsed)
246
+ return parsed;
247
+ throw invalidSubjectPath(`Path must be ${SUBJECT_PATH_FORMS_PROSE} — for example "row:8f2c1a4d#email".`, { path: typeof value === "string" ? value : null });
248
+ }
249
+ function invalidSubjectPath(message, details) {
250
+ return new OxygenError("invalid_subject_path", message, { details, exitCode: 2 });
251
+ }
252
+ /**
253
+ * The gates a workspace can require. `launch` is the client's go/no-go before a
254
+ * campaign sends; `signoff` is their sign-off on the list that campaign sends
255
+ * to. Both are OPT-IN per subject (or per subject kind) — an unset gate leaves
256
+ * today's behavior untouched.
257
+ */
258
+ export const COLLAB_GATE_KINDS = ["launch", "signoff"];
259
+ export function isCollabGateKind(value) {
260
+ return typeof value === "string" && COLLAB_GATE_KINDS.includes(value);
261
+ }
262
+ /**
263
+ * Which subject kinds each gate can be required on. Total over CollabGateKind,
264
+ * so a new gate cannot ship without naming the primitives that must honor it —
265
+ * a gate nobody enforces is worse than no gate, because the workspace believes
266
+ * it is protected.
267
+ */
268
+ export const GATE_KIND_SUBJECTS = {
269
+ launch: ["sequence"],
270
+ signoff: ["table"],
271
+ };
272
+ /** What each gate means, for `--help` and MCP descriptors. Total over CollabGateKind. */
273
+ export const COLLAB_GATE_PROSE = {
274
+ launch: "approve before a sequence starts sending live",
275
+ signoff: "approve the table's rows before they are worked",
276
+ };
277
+ export function isGateKindValidForSubject(gateKind, subjectKind) {
278
+ if (!isCollabGateKind(gateKind) || !isCollabSubjectKind(subjectKind))
279
+ return false;
280
+ return GATE_KIND_SUBJECTS[gateKind].includes(subjectKind);
281
+ }
282
+ /** Every gate that can be required on a subject kind — drives `approvals gate list`. */
283
+ export function gateKindsForSubject(subjectKind) {
284
+ if (!isCollabSubjectKind(subjectKind))
285
+ return [];
286
+ return COLLAB_GATE_KINDS.filter((gate) => GATE_KIND_SUBJECTS[gate].includes(subjectKind));
287
+ }
288
+ /** A thread is open until somebody resolves it; reopening flips it back. */
289
+ export const COLLAB_THREAD_STATUSES = ["open", "resolved"];
290
+ export function isCollabThreadStatus(value) {
291
+ return typeof value === "string" && COLLAB_THREAD_STATUSES.includes(value);
292
+ }
293
+ export const COLLAB_REQUEST_STATUSES = [
294
+ "pending",
295
+ "approved",
296
+ "changes_requested",
297
+ "rejected",
298
+ "cancelled",
299
+ "expired",
300
+ ];
301
+ export function isCollabRequestStatus(value) {
302
+ return typeof value === "string" && COLLAB_REQUEST_STATUSES.includes(value);
303
+ }
304
+ /**
305
+ * The whole feature hinges on this one line: ONLY `approved` opens a gate.
306
+ *
307
+ * `changes_requested` is a decision, and it ends the requester's wait, but it is
308
+ * not consent — the client asked for edits. Treating it as terminal-and-done is
309
+ * the obvious bug (a request that is no longer pending looks "handled"), so the
310
+ * gate check asks this function rather than testing `status !== "pending"`.
311
+ */
312
+ export function requestStatusOpensGate(status) {
313
+ return status === "approved";
314
+ }
315
+ /** Statuses that no longer await a decision. `pending` is the only live state. */
316
+ export const COLLAB_REQUEST_TERMINAL_STATUSES = [
317
+ "approved",
318
+ "changes_requested",
319
+ "rejected",
320
+ "cancelled",
321
+ "expired",
322
+ ];
323
+ export function isTerminalRequestStatus(status) {
324
+ return isCollabRequestStatus(status) && status !== "pending";
325
+ }
326
+ /**
327
+ * What an approver may do, as the CLI/MCP spells it — and the status each
328
+ * decision writes. Total over CollabDecision so the API, the CLI flags, and the
329
+ * MCP tool cannot each invent their own mapping (the failure mode being a
330
+ * `changes_requested` decision stored as `rejected`, which reads to the agency
331
+ * as "the client said no" rather than "the client wants edits").
332
+ */
333
+ export const COLLAB_DECISIONS = ["approve", "reject", "changes_requested"];
334
+ export function isCollabDecision(value) {
335
+ return typeof value === "string" && COLLAB_DECISIONS.includes(value);
336
+ }
337
+ export const COLLAB_DECISION_STATUSES = {
338
+ approve: "approved",
339
+ reject: "rejected",
340
+ changes_requested: "changes_requested",
341
+ };
342
+ /** Why a notification exists. Delivery itself is the notifier's problem. */
343
+ export const COLLAB_NOTIFICATION_KINDS = ["assigned", "mentioned", "decided", "replied"];
344
+ export function isCollabNotificationKind(value) {
345
+ return (typeof value === "string" && COLLAB_NOTIFICATION_KINDS.includes(value));
346
+ }
347
+ export const COLLAB_NOTIFICATION_STATUSES = ["pending", "sent", "failed", "skipped"];
348
+ export function isCollabNotificationStatus(value) {
349
+ return (typeof value === "string" && COLLAB_NOTIFICATION_STATUSES.includes(value));
350
+ }
351
+ /**
352
+ * Body/field caps. Shared so the API, the CLI, and the tenant column widths
353
+ * agree — three independently chosen limits means the CLI accepts what the API
354
+ * truncates.
355
+ */
356
+ export const MAX_COMMENT_BODY_LENGTH = 10_000;
357
+ export const MAX_THREAD_TITLE_LENGTH = 200;
358
+ export const MAX_DECISION_NOTE_LENGTH = 2_000;
359
+ /** Bounds the notification fan-out one comment or request can trigger. */
360
+ export const MAX_MENTIONS_PER_COMMENT = 25;
361
+ export const MAX_ASSIGNEES_PER_REQUEST = 10;
362
+ /**
363
+ * Normalize one comment body: trim, reject blanks, non-strings, and anything
364
+ * over {@link MAX_COMMENT_BODY_LENGTH}. Returns `null` so each surface can raise
365
+ * its own typed error (the CLI wants an exit code, the API wants a status).
366
+ *
367
+ * Deliberately does NOT strip or rewrite the body beyond trimming — a comment is
368
+ * the client's words, and Markdown, quoted text, and whitespace layout are part
369
+ * of what they wrote.
370
+ */
371
+ export function normalizeCommentBody(value) {
372
+ if (typeof value !== "string")
373
+ return null;
374
+ const normalized = value.trim();
375
+ if (!normalized || normalized.length > MAX_COMMENT_BODY_LENGTH)
376
+ return null;
377
+ return normalized;
378
+ }
379
+ /**
380
+ * The Oxygen-side membership role overlay (control DB
381
+ * `organization_memberships.oxygen_role`). NULL means no override — today's
382
+ * behavior for every existing member — and `client` is the restricted role an
383
+ * agency hands its client: read the work, comment on it, decide the approvals
384
+ * assigned to them, and nothing else.
385
+ *
386
+ * Deliberately not a Clerk custom role: those need dashboard configuration, and
387
+ * repo doctrine requires asking a human before touching Clerk. Keep this list in
388
+ * step with the `organization_memberships_oxygen_role_check` constraint in
389
+ * control migration 0095 — the CHECK is what stops a typo from creating a role
390
+ * nothing enforces.
391
+ */
392
+ export const COLLAB_MEMBER_ROLES = ["client"];
393
+ export function isCollabMemberRole(value) {
394
+ return typeof value === "string" && COLLAB_MEMBER_ROLES.includes(value);
395
+ }
396
+ /**
397
+ * The CLERK workspace roles that carry admin authority (control DB
398
+ * `organization_memberships.role`). Both spellings of each: Clerk writes the
399
+ * `org:`-prefixed form, older rows carry the bare one.
400
+ *
401
+ * The overlay above and this list are the two halves of one question — "may this
402
+ * member do X" — and a Clerk admin's role OVERRIDES the overlay, which is why
403
+ * granting `client` to one is refused. Keeping the vocabulary here means the
404
+ * collaboration lib can ask without importing the billing graph, and there is
405
+ * one list to change if Clerk ever gains another admin-ish role.
406
+ */
407
+ export const WORKSPACE_ADMIN_ROLES = ["admin", "owner", "org:admin", "org:owner"];
408
+ export function isWorkspaceAdminRole(role) {
409
+ return WORKSPACE_ADMIN_ROLES.includes(role.toLowerCase());
410
+ }
@@ -12,8 +12,8 @@
12
12
  */
13
13
  /** Data types an import can infer for a brand-new column. Dates-only by design. */
14
14
  export type ImportColumnDataType = "text" | "timestamptz";
15
- /** Data types a `text` column can be explicitly retyped into. */
16
- export type RetypeableDataType = "text" | "numeric" | "boolean" | "timestamptz";
15
+ /** Data types a plain data column can be explicitly retyped into. */
16
+ export type RetypeableDataType = "text" | "numeric" | "boolean" | "jsonb" | "timestamptz";
17
17
  export declare const RETYPEABLE_DATA_TYPES: readonly RetypeableDataType[];
18
18
  export declare const EXCEL_SERIAL_MIN = 10000;
19
19
  export declare const EXCEL_SERIAL_MAX = 80000;
@@ -41,7 +41,7 @@ export declare function parseDateValueToIso(value: unknown, options?: {
41
41
  export declare function inferImportColumnDataType(label: string, values: Iterable<unknown>): ImportColumnDataType;
42
42
  export type CoercionResult = {
43
43
  ok: true;
44
- value: string | number | boolean | null;
44
+ value: unknown | null;
45
45
  } | {
46
46
  ok: false;
47
47
  reason: string;