@workbasehq/core 0.2.7 → 0.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workbasehq/core",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
package/src/app.ts CHANGED
@@ -75,7 +75,7 @@ function buildApp(input: InstanceInput, env: RuntimeEnv, options: RuntimeOptions
75
75
  bindings: [ulid(), error.code, status, c.get('correlationId'), new Date().toISOString()], expectRows: 1,
76
76
  }])
77
77
  } else if (status === 403 || status === 409) {
78
- if (c.req.path.startsWith('/auth/')) await identityWrite(env, config, 'security_denied', [{
78
+ if (c.req.path.startsWith('/auth/') && c.req.path !== '/auth/register') await identityWrite(env, config, 'security_denied', [{
79
79
  sql: 'INSERT INTO security_log (id,operation,status,correlation_id,occurred_at) VALUES (?,?,?,?,?)',
80
80
  bindings: [ulid(), `${c.req.method} ${routePath(c, -1)}`, status, c.get('correlationId'), new Date().toISOString()], expectRows: 1,
81
81
  }])
package/src/config.ts CHANGED
@@ -18,7 +18,7 @@ export { Q, Cond }
18
18
  /** SQLite date formatting with column operands; values use the library wire encoding. */
19
19
  export const Fn = { ...LibraryFn, strftime: (format: string, date: string) => Q.expr(`FN(${JSON.stringify(['STRFTIME', Q.value(format).serialize(), Q.expr(date).serialize()])})`) }
20
20
 
21
- export const CORE_VERSION = '0.2.7'
21
+ export const CORE_VERSION = '0.2.9'
22
22
  const navigationIconSchema = z.enum(['LayoutDashboard', 'Users', 'MapPin', 'Coins', 'Wallet', 'ArrowLeftRight', 'Settings', 'Building2', 'Activity', 'Workflow', 'Database', 'Folder'])
23
23
  const identifier = z.string().regex(/^[a-z][a-z0-9_]*$/)
24
24
  export const serializedExpressionSchema = z.union([z.string().min(1).max(32000), z.record(z.string(), z.unknown()).transform(value => JSON.stringify(value))])
@@ -137,18 +137,27 @@ export function defineInstance(input: Omit<ShorthandInput, 'roles' | 'entities'
137
137
  for (const plugin of resolved.plugins) {
138
138
  if (plugin.configure) contribution = { ...contribution, ...plugin.configure(plugin.configSchema?.parse(pluginConfig[plugin.name])) }
139
139
  }
140
- const config = schema.parse(expand({ ...model, ...input, entities: input.entities ?? model.entities ?? {}, permissions: input.permissions ?? model.permissions ?? {}, ...contribution, roles: contribution.roles ?? input.roles ?? [],
140
+ const expanded = expand({ ...model, ...input, entities: input.entities ?? model.entities ?? {}, permissions: input.permissions ?? model.permissions ?? {}, ...contribution, roles: contribution.roles ?? input.roles ?? [],
141
141
  organizationPermissions: { ...model.organizationPermissions, ...contribution.organizationPermissions, ...input.organizationPermissions },
142
142
  ...(input.pluginConfig || Object.keys(pluginConfig).length ? { pluginConfig } : {}),
143
- }))
143
+ })
144
+ // Append built-ins after shorthand expansion: wildcards/default tables keep their
145
+ // existing entries byte-for-byte. Newly materialized roles receive no grants.
146
+ const authoredRoles = expanded.roles
147
+ const roles = [...authoredRoles]
148
+ for (const role of [expanded.organizationOwnerRole ?? 'owner', 'member']) if (!roles.includes(role)) roles.push(role)
149
+ const config = schema.parse({ ...expanded, roles })
144
150
  for (const name of Object.keys(config.pluginConfig ?? {})) {
145
151
  if (!config.plugins?.some(plugin => plugin.name === name)) throw new Error(`Unknown plugin config: ${name}`)
146
152
  }
147
153
  // Custom roles have no default grants. Preserve sparse explicit/model tables;
148
154
  // implicit tables retain their existing empty entries for custom roles.
149
155
  const defaultRoles = input.organizationPermissions === undefined && model.organizationPermissions === undefined
150
- ? config.roles : config.roles.filter(role => Object.hasOwn(defaultOrganizationPermissions, role))
151
- config.organizationPermissions = { ...Object.fromEntries(defaultRoles.map(role => [role, [...(defaultOrganizationPermissions[role] ?? [])]])), ...config.organizationPermissions }
156
+ ? authoredRoles.filter(role => role !== 'member') : authoredRoles.filter(role => Object.hasOwn(defaultOrganizationPermissions, role))
157
+ // Only absent entries receive legacy defaults; a persisted [] stays empty on reload.
158
+ config.organizationPermissions = { ...Object.fromEntries(defaultRoles.filter(role => !Object.hasOwn(config.organizationPermissions, role))
159
+ .map(role => [role, [...(defaultOrganizationPermissions[role] ?? [])]])), ...config.organizationPermissions }
160
+ if (!authoredRoles.includes(config.organizationOwnerRole) && !Object.hasOwn(config.organizationPermissions, config.organizationOwnerRole)) config.organizationPermissions[config.organizationOwnerRole] = []
152
161
  if (config.organizationOwnerRole !== 'owner' && !config.roles.includes(config.organizationOwnerRole)) throw new Error('Unknown organization owner role')
153
162
  for (const [name, entity] of Object.entries(config.entities)) {
154
163
  if ((reserved.has(name) && !(name === 'users' && config.account?.entity === name)) || name === 'atomic_assert' || name.startsWith('sqlite_') || name.startsWith('d1_')) throw new Error(`Reserved entity: ${name}`)
@@ -3,7 +3,7 @@ import type { InstanceConfig } from '../config'
3
3
  import type { Scope } from '../identity'
4
4
  import type { AtomicOperation } from './types'
5
5
 
6
- export { isOrganizationAdministrator, organizationRoleRank, organizationPermission } from '../organization-permissions'
6
+ export { isOrganizationAdministrator, canGrantRole, organizationPermission } from '../organization-permissions'
7
7
  export function atomicPermission(config: InstanceConfig, scope: Scope, operation: AtomicOperation, input?: unknown) {
8
8
  kernelFor(config).atomicPermission(scope, operation, input)
9
9
  }
@@ -144,9 +144,17 @@ loader relative to the installed package. There is no instance registry or confi
144
144
  discovery. Worker consumers can omit the optional `tsx` peer.
145
145
 
146
146
  Omit `--config` for a plain SQL database. With a config, migration verifies the SQL
147
- ledger and instance revision/core version, applies plugin migrations, and verifies
148
- their ledger and schema hooks. SQL must already contain the matching
149
- `instance_meta` revision and core version.
147
+ ledger and instance revision, applies plugin migrations, and verifies their ledger
148
+ and schema hooks. SQL must already contain the matching `instance_meta` revision.
149
+ An existing SDK stamp must be a numeric release in the running SDK's major and
150
+ no newer than that SDK. Unsupported stamps fail with
151
+ `migration_instance_revision_mismatch` before any SQL or plugin migrations or
152
+ ledger writes; the version is checked again after SQL and before plugin execution.
153
+ After verification, a stale core version is refreshed on that revision's row,
154
+ including releases with no pending SQL or plugin migrations. Older revision rows
155
+ and migration receipts are preserved; rerunning with the current version writes
156
+ no marker. A failure before the refresh can leave the old stamp; retry the deploy
157
+ to resume verification and refresh it before Worker upload.
150
158
 
151
159
  Files must be contiguous `0001_name.sql`, `0002_name.sql`, etc. Each SQL file and
152
160
  its receipt execute in one D1 HTTP transaction. Changed or missing historical SQL,
@@ -1,7 +1,7 @@
1
1
  import { MigrationDiagnostic } from './diagnostics'
2
2
  import { CORE_VERSION } from '../config'
3
3
  import { hashMigrationRows } from '../schema-compatibility'
4
- import { literal } from '../migrations/schema'
4
+ import { coreVersionSQL, literal } from '../migrations/schema'
5
5
  import { pluginMigrationStatus } from '../kernel/migrations'
6
6
  import type { KernelRegistry } from '../kernel/registry'
7
7
  import { migrateD1, type D1Query, type Migration } from './d1-migrations'
@@ -39,16 +39,38 @@ export function queryDatabase(query: D1Query): D1Database {
39
39
  } } as unknown as D1Database
40
40
  }
41
41
 
42
+ // Match MCP's supported baseline: numeric release versions in the same major,
43
+ // no newer than this SDK. Compare components numerically (0.2.10 > 0.2.8).
44
+ function supportedCoreVersion(value: unknown) {
45
+ const version = typeof value === 'string' && value.match(/^(\d+)\.(\d+)\.(\d+)$/)?.slice(1).map(Number)
46
+ const current = CORE_VERSION.split('.').map(Number)
47
+ return version && version.every(Number.isSafeInteger) && version.join('.') === value && version[0] === current[0] &&
48
+ (version[1] < current[1] || (version[1] === current[1] && version[2] <= current[2]))
49
+ }
50
+
42
51
  export async function migrateInstance(query: D1Query, files: readonly Migration[], kernel: KernelRegistry) {
43
- const legacy = await migrateD1(query, files)
44
52
  const db = queryDatabase(query)
53
+ const metaSQL = 'SELECT revision,core_version FROM instance_meta ORDER BY revision DESC LIMIT 1'
54
+ // Check an existing database before even creating a ledger or applying SQL:
55
+ // a pending SQL file could otherwise overwrite an unsupported SDK stamp.
56
+ const existing = await db.prepare("SELECT name FROM sqlite_master WHERE name='instance_meta'").first()
57
+ if (existing) {
58
+ const stored = await db.prepare(metaSQL).first<{ core_version: string }>()
59
+ if (!supportedCoreVersion(stored?.core_version)) throw new MigrationDiagnostic({ code: 'migration_instance_revision_mismatch' })
60
+ }
61
+ const legacy = await migrateD1(query, files)
45
62
  const rows = await db.prepare('SELECT filename,checksum FROM _migrations ORDER BY filename').all<{ filename: string; checksum: string }>()
46
63
  const expectedRows = [...files].sort((a, b) => a.filename < b.filename ? -1 : a.filename > b.filename ? 1 : 0)
47
64
  if (await hashMigrationRows(rows.results) !== await hashMigrationRows(expectedRows)) throw new MigrationDiagnostic({ code: 'migration_instance_verification_failed' })
48
- const meta = await db.prepare('SELECT revision,core_version FROM instance_meta ORDER BY revision DESC LIMIT 1').first<{ revision: number; core_version: string }>()
49
- if (meta?.revision !== kernel.config.revision || meta.core_version !== CORE_VERSION) throw new MigrationDiagnostic({ code: 'migration_instance_revision_mismatch' })
65
+ const meta = await db.prepare(metaSQL).first<{ revision: number; core_version: string }>()
66
+ if (meta?.revision !== kernel.config.revision || !supportedCoreVersion(meta.core_version)) throw new MigrationDiagnostic({ code: 'migration_instance_revision_mismatch' })
50
67
  await kernel.applyMigrations(db)
51
68
  const status = await pluginMigrationStatus(db, kernel.plugins, kernel.config)
52
69
  if (status.expected !== status.applied) throw new MigrationDiagnostic({ code: 'migration_plugin_verification_failed' })
70
+ // A release can change CORE_VERSION without adding SQL or a config revision.
71
+ // Stamp it only after both ledgers and plugin schema checks have succeeded.
72
+ if (meta.core_version !== CORE_VERSION) await db.prepare(coreVersionSQL(kernel.config.revision)).run()
73
+ const applied = await db.prepare(metaSQL).first<{ revision: number; core_version: string }>()
74
+ if (applied?.revision !== kernel.config.revision || applied.core_version !== CORE_VERSION) throw new MigrationDiagnostic({ code: 'migration_instance_revision_mismatch' })
53
75
  return { ...legacy, pluginMigrations: status.applied }
54
76
  }
@@ -1,7 +1,8 @@
1
1
  import { defineInstance, inspectInstance, type InstanceConfig } from '../config'
2
2
  import { column, createEntity, initialSchema, literal, quote, revisionSQL } from './schema'
3
+ import { rebuildWithNullableRefs, type SupplementalIndex } from './rebuild'
3
4
  export interface MigrationPlan { sql: string; destructive: string[]; blocked: string[]; warnings: string[] }
4
- export function plan(fromConfig: InstanceConfig | null, toConfig: InstanceConfig, options: { allowDestructive?: boolean } = {}): MigrationPlan {
5
+ export function plan(fromConfig: InstanceConfig | null, toConfig: InstanceConfig, options: { allowDestructive?: boolean; indexes?: SupplementalIndex[] } = {}): MigrationPlan {
5
6
  const to = defineInstance(toConfig)
6
7
  if (!fromConfig) return { sql: initialSchema(to), destructive: [], blocked: [], warnings: [] }
7
8
  // An older schema may violate a new presentation invariant; the result must
@@ -10,6 +11,8 @@ export function plan(fromConfig: InstanceConfig | null, toConfig: InstanceConfig
10
11
  if (from.id !== to.id) throw new Error('Cannot migrate between different instance IDs')
11
12
  if (to.revision <= from.revision) throw new Error('Target revision must increase')
12
13
  const sql: string[] = []; const destructive: string[] = []; const blocked: string[] = []; const warnings: string[] = []
14
+ const rebuilds = new Map<string, string[]>()
15
+ const referenceBackfills: string[] = []
13
16
  // Visit removed parents first, then reverse: every child is dropped before its refs.
14
17
  const removed = new Set(Object.keys(from.entities).filter(name => !to.entities[name]))
15
18
  const visited = new Set<string>(); const visiting = new Set<string>(); const parentsFirst: string[] = []
@@ -55,7 +58,19 @@ export function plan(fromConfig: InstanceConfig | null, toConfig: InstanceConfig
55
58
  blocked.push(`Changing an existing column's type/default/constraints requires a manually reviewed migration: ${name}.${key}`)
56
59
  }
57
60
  } else {
58
- if (field.type === 'ref') { blocked.push(`Adding a composite reference requires a manually reviewed migration: ${name}.${key}`); continue }
61
+ if (field.type === 'ref') {
62
+ if (field.required) blocked.push(`Required added reference needs a manually reviewed backfill migration: ${name}.${key}`)
63
+ else if (name === 'users' && (from.account?.entity === name || to.account?.entity === name)) blocked.push(`Account entity users requires a manually reviewed credential-schema migration before adding reference: ${name}.${key}`)
64
+ else {
65
+ const added = rebuilds.get(name) ?? []; added.push(key); rebuilds.set(name, added)
66
+ if (field.backfillFrom) {
67
+ const ref = next.fields[field.backfillFrom.refField]
68
+ // A replay must also preserve links deliberately cleared after the upgrade.
69
+ referenceBackfills.push(`UPDATE ${quote(name)} SET ${quote(key)} = (SELECT source.${quote(field.backfillFrom.valueField)} FROM ${quote(ref.entity!)} source WHERE source.organization_id = ${quote(name)}.organization_id AND source.id = ${quote(name)}.${quote(field.backfillFrom.refField)}) WHERE NOT EXISTS (SELECT 1 FROM instance_meta WHERE revision = ${to.revision});`)
70
+ }
71
+ }
72
+ continue
73
+ }
59
74
  if (field.required && (field.default === undefined || field.default === null)) { blocked.push(`Required added field needs a non-null backfill default: ${name}.${key}`); continue }
60
75
  sql.push(`ALTER TABLE ${quote(name)} ADD COLUMN ${column(key, field)};`)
61
76
  if (field.backfillFrom) {
@@ -79,6 +94,19 @@ export function plan(fromConfig: InstanceConfig | null, toConfig: InstanceConfig
79
94
  if (destructive.length && !options.allowDestructive) blocked.push('Destructive operations require --allow-destructive')
80
95
  if (destructive.length) warnings.push('Review destructive operations and take a backup before applying; this planner never applies SQL.')
81
96
  if (blocked.length) return { sql: `-- BLOCKED: ${blocked.join('\n-- ')}\n`, destructive, blocked, warnings }
82
- sql.push(revisionSQL(to.revision))
97
+ const rebuildSQL = [...rebuilds].map(([name, added]) => rebuildWithNullableRefs(name, from, to, added, options.indexes ?? []))
98
+ // Validate the previous physical schema before any proposed ALTER statements.
99
+ if (rebuildSQL.length) {
100
+ sql.unshift('PRAGMA defer_foreign_keys = ON;', ...rebuildSQL.map(rebuild => rebuild.guard))
101
+ for (const [i, rebuild] of rebuildSQL.entries()) {
102
+ sql.push(rebuild.sql)
103
+ // Keep the entire graph deferred until every table has its final shape.
104
+ if (i === rebuildSQL.length - 1) sql.push(rebuild.check)
105
+ sql.push(rebuild.cleanup)
106
+ }
107
+ sql.push('PRAGMA defer_foreign_keys = OFF;')
108
+ }
109
+ sql.push(...referenceBackfills)
110
+ sql.push(rebuilds.size ? revisionSQL(to.revision).replace('INSERT INTO', 'INSERT OR IGNORE INTO') : revisionSQL(to.revision))
83
111
  return { sql: `-- ${from.id}: revision ${from.revision} -> ${to.revision}\n${sql.join('\n')}\n`, destructive, blocked, warnings }
84
112
  }
@@ -0,0 +1,158 @@
1
+ import type { InstanceConfig } from '../config'
2
+ import { createEntity, literal, quote } from './schema'
3
+
4
+ /** Non-unique, organization-scoped indexes owned by the caller (e.g. configuration MCP). */
5
+ export interface SupplementalIndex { entity: string; name: string; fields: string[] }
6
+
7
+ const differences = ['rowid', 'columns', 'indexes', 'foreign_keys', 'inline_constraints', 'triggers', 'incoming_actions'] as const
8
+
9
+ /** Canonical multiset of top-level definitions. Token boundaries and string
10
+ * contents survive normalization. Quoted identifiers (i), bare words (w),
11
+ * numbers (n), strings (s) and punctuation (p) remain distinct. Only whitespace,
12
+ * identifier quote style/case and definition order are ignored. Numeric states
13
+ * consume decimal/hex digits and signed exponents without swallowing operators.
14
+ * Comments, unknown characters, incomplete tokens and table suffixes fail closed.
15
+ */
16
+ function definitions(table: string) {
17
+ const ch = 'substr(body, pos, 1)', next = 'substr(body, pos + 1, 1)'
18
+ const word = `${ch} GLOB '[a-zA-Z0-9_$]'`
19
+ const digit = `${ch} GLOB '[0-9]'`
20
+ const number = `mode IN ('integer', 'fraction', 'exponent', 'sign', 'digits', 'hex_start', 'hex')`
21
+ const numberEnd = `mode IN ('integer', 'fraction', 'digits', 'hex')`
22
+ const numberMore = `(${digit} AND mode IN ('integer', 'fraction', 'exponent', 'sign', 'digits'))
23
+ OR (${ch} GLOB '[0-9a-fA-F]' AND mode IN ('hex_start', 'hex'))
24
+ OR (${ch} = '.' AND mode = 'integer')
25
+ OR (${ch} IN ('e', 'E') AND mode IN ('integer', 'fraction'))
26
+ OR (${ch} IN ('+', '-') AND mode = 'exponent')
27
+ OR (${ch} IN ('x', 'X') AND mode = 'integer' AND token = '0')`
28
+ const flush = `(mode = 'word' AND NOT (${word})) OR
29
+ (${numberEnd} AND NOT (${numberMore}) AND NOT (${word}) AND ${ch} <> '.')`
30
+ const quoted = `mode IN (${["'", '"', '`', ']'].map(literal).join(', ')})`
31
+ const closing = `${quoted} AND ${ch} = mode`
32
+ const escaped = `${closing} AND ${next} = mode`
33
+ const boundary = `mode = '' AND depth = 0 AND ${ch} IN (',', ')')`
34
+ const space = `${ch} IN (' ', char(9), char(10), char(12), char(13))`
35
+ const opening = `${ch} IN (${["'", '"', '`', '['].map(literal).join(', ')})`
36
+ const punctuation = `${ch} IN (${[...'(),.+-*/%<>=~&|!'].map(literal).join(', ')})`
37
+ return `(WITH RECURSIVE source(body) AS (
38
+ SELECT substr(sql, instr(sql, '(') + 1) FROM sqlite_schema WHERE type = 'table' AND lower(name) = lower(${table})
39
+ ), scan(pos, depth, mode, token, definition, parts) AS (
40
+ SELECT 1, 0, '', '', '', json_array()
41
+ UNION ALL
42
+ SELECT pos + CASE WHEN ${flush} THEN 0 WHEN ${escaped} THEN 2 ELSE 1 END,
43
+ depth + CASE WHEN mode = '' AND ${ch} = '(' THEN 1 WHEN mode = '' AND ${ch} = ')' AND depth > 0 THEN -1 ELSE 0 END,
44
+ CASE WHEN ${flush} THEN ''
45
+ WHEN ${number} THEN CASE
46
+ WHEN NOT (${numberMore}) THEN 'invalid'
47
+ WHEN mode IN ('hex_start', 'hex') THEN 'hex'
48
+ WHEN ${ch} IN ('x', 'X') THEN 'hex_start'
49
+ WHEN ${ch} IN ('e', 'E') THEN 'exponent'
50
+ WHEN ${ch} IN ('+', '-') THEN 'sign'
51
+ WHEN mode IN ('exponent', 'sign', 'digits') THEN 'digits'
52
+ WHEN ${ch} = '.' THEN 'fraction' ELSE mode END
53
+ WHEN ${quoted} THEN CASE WHEN ${closing} AND NOT (${escaped}) THEN '' ELSE mode END
54
+ WHEN mode = 'word' THEN 'word'
55
+ WHEN ${boundary} AND ${ch} = ')' THEN 'done'
56
+ WHEN ${opening} THEN CASE WHEN ${ch} = '[' THEN ']' ELSE ${ch} END
57
+ WHEN ${digit} THEN 'integer'
58
+ WHEN ${ch} = '.' AND ${next} GLOB '[0-9]' THEN 'fraction'
59
+ WHEN ${word} THEN 'word'
60
+ WHEN (${ch} = '-' AND ${next} = '-') OR (${ch} = '/' AND ${next} = '*') THEN 'invalid'
61
+ WHEN (${punctuation}) OR (${space}) THEN '' ELSE 'invalid' END,
62
+ CASE WHEN ${flush} THEN ''
63
+ WHEN ${number} THEN token || ${ch}
64
+ WHEN ${quoted} THEN CASE WHEN ${closing} AND NOT (${escaped}) THEN '' ELSE token || ${ch} END
65
+ WHEN (${word}) OR (${ch} = '.' AND ${next} GLOB '[0-9]') THEN token || ${ch} ELSE '' END,
66
+ CASE WHEN ${flush} THEN definition || json_quote(CASE WHEN mode = 'word' THEN 'w:' || lower(token) ELSE 'n:' || token END) || ','
67
+ WHEN ${number} THEN definition
68
+ WHEN ${quoted} THEN CASE WHEN ${closing} AND NOT (${escaped})
69
+ THEN definition || json_quote(CASE WHEN mode = ${literal("'")} THEN 's:' || token ELSE 'i:' || lower(token) END) || ',' ELSE definition END
70
+ WHEN ${boundary} THEN ''
71
+ WHEN ${opening} OR (${word}) OR (${space}) OR (${ch} = '.' AND ${next} GLOB '[0-9]') THEN definition
72
+ ELSE definition || json_quote('p:' || ${ch}) || ',' END,
73
+ CASE WHEN ${boundary} THEN json_insert(parts, '$[#]', definition) ELSE parts END
74
+ FROM scan, source WHERE mode NOT IN ('done', 'invalid') AND pos <= length(body)
75
+ ) SELECT json_group_array(value) FROM (
76
+ SELECT value FROM scan, source, json_each(parts)
77
+ WHERE mode = 'done' AND trim(substr(body, pos), ' ' || char(9) || char(10) || char(12) || char(13)) = '' ORDER BY value
78
+ ))`
79
+ }
80
+
81
+ /** Use SQLite itself to describe both the physical table and a generator-built
82
+ * witness. Keep automatic indexes and auxiliary index columns in the fingerprint.
83
+ * Compare ASCII-case-folded identifiers rather than history-dependent column/FK
84
+ * IDs. Preserve default expressions and string/numeric literal contents exactly.
85
+ * Body tokens cover CHECK, COLLATE, conflict clauses and FK deferrability, which
86
+ * PRAGMAs do not expose, so unfamiliar DDL still fails closed.
87
+ */
88
+ function fingerprint(name: string, indexNames: [string, string][] = []) {
89
+ const table = literal(name)
90
+ const indexName = indexNames.length ? `CASE lower(name) ${indexNames.map(([physical, canonical]) => `WHEN lower(${literal(physical)}) THEN lower(${literal(canonical)})`).join(' ')} ELSE lower(name) END` : 'lower(name)'
91
+ return `json_object(
92
+ 'rowid', (SELECT wr FROM pragma_table_list WHERE schema = 'main' AND lower(name) = lower(${table})),
93
+ 'columns', (SELECT json_group_array(json_array(lower(name), lower(type), "notnull", dflt_value, pk, hidden)) FROM (SELECT * FROM pragma_table_xinfo(${table}) ORDER BY lower(name))),
94
+ 'indexes', (SELECT json_group_array(json_array(index_name, origin, "unique", partial, json(columns))) FROM (
95
+ SELECT CASE WHEN origin = 'c' THEN ${indexName} ELSE origin END AS index_name, origin, "unique", partial,
96
+ (SELECT json_group_array(json_array(seqno, CASE WHEN cid < 0 THEN cid END, lower(name), "desc", lower(coll), "key")) FROM (SELECT * FROM pragma_index_xinfo(i.name) ORDER BY seqno)) AS columns
97
+ FROM pragma_index_list(${table}) i ORDER BY index_name, columns)),
98
+ 'foreign_keys', (SELECT json_group_array(json_array("table", on_update, on_delete, match, json(columns))) FROM (
99
+ SELECT lower("table") AS "table", on_update, on_delete, match,
100
+ (SELECT json_group_array(json_array(seq, lower("from"), lower("to"))) FROM (SELECT * FROM pragma_foreign_key_list(${table}) WHERE id = fk.id ORDER BY seq)) AS columns
101
+ FROM pragma_foreign_key_list(${table}) fk WHERE seq = 0 ORDER BY "table", on_update, on_delete, match, columns)),
102
+ 'inline_constraints', ${definitions(table)},
103
+ 'triggers', (SELECT count(*) FROM sqlite_schema WHERE type = 'trigger' AND lower(tbl_name) = lower(${table})),
104
+ 'incoming_actions', NOT EXISTS (SELECT 1 FROM sqlite_schema s, pragma_foreign_key_list(s.name) fk WHERE s.type = 'table' AND s.name NOT GLOB '_cf_*' AND s.name NOT GLOB 'sqlite_*' AND lower(fk."table") = lower(${table}) AND (fk.on_delete <> 'NO ACTION' OR fk.on_update <> 'NO ACTION'))
105
+ )`
106
+ }
107
+
108
+ function witness(name: string, expected: string, entity: InstanceConfig['entities'][string], indexes: SupplementalIndex[]) {
109
+ const suffixes = ['org_idx', ...Object.entries(entity.fields).filter(([, f]) => f.type === 'ref' && !f.computed).map(([key]) => `${key}_idx`)]
110
+ const names: [string, string][] = [...suffixes.map(suffix => [`${expected}_${suffix}`, `${name}_${suffix}`] as [string, string]), ...indexes.map((index, i) => [`${expected}_supplemental_${i}`, index.name] as [string, string])]
111
+ return { shape: fingerprint(expected, names), sql: createEntity(expected, entity)
112
+ + '\n' + indexes.map((index, i) => `CREATE INDEX ${quote(`${expected}_supplemental_${i}`)} ON ${quote(expected)} (${['organization_id', ...index.fields].map(quote).join(', ')});`).join('\n')
113
+ }
114
+ }
115
+
116
+ /** The runner must submit the whole migration in one D1 transaction/batch.
117
+ * D1 cannot disable foreign_keys inside that transaction. Defer NO ACTION checks,
118
+ * recreate the original name, validate the final graph, then restore enforcement.
119
+ */
120
+ export function rebuildWithNullableRefs(name: string, previous: InstanceConfig, config: InstanceConfig, added: string[], indexes: SupplementalIndex[]) {
121
+ const entity = config.entities[name]
122
+ let prefix = `workbase_rebuild_${name}_${config.revision}`
123
+ while (['rows', 'guard', 'previous', 'next'].some(suffix => config.entities[`${prefix}_${suffix}`] || previous.entities[`${prefix}_${suffix}`])) prefix += '_'
124
+ const rows = quote(`${prefix}_rows`), guard = quote(`${prefix}_guard`), table = quote(name)
125
+ const fields = Object.entries(entity.fields).filter(([, field]) => !field.computed)
126
+ const columns = ['id', 'organization_id', 'created_at', 'updated_at', 'version', ...fields.map(([key]) => key)]
127
+ const supplemental = indexes.filter(index => index.entity === name)
128
+ const before = `${prefix}_previous`, after = `${prefix}_next`
129
+ const previousIndexes = supplemental.map(index => ({ ...index, fields: index.fields.map(key => previous.entities[name].fields[key] ? key : entity.fields[key]?.renameFrom ?? key) }))
130
+ const previousWitness = witness(name, before, previous.entities[name], previousIndexes)
131
+ const nextWitness = witness(name, after, entity, supplemental)
132
+ const guardSQL = `-- Verify ${name} against fresh generator shapes before any schema changes.
133
+ ${previousWitness.sql}
134
+ ${nextWitness.sql}
135
+ CREATE TABLE ${guard} (
136
+ reason TEXT DEFAULT '' ${differences.map(part => `CONSTRAINT ${quote(`rebuild_requires_review_${name}_${part}`)} CHECK (reason <> ${literal(part)})`).join(' ')},
137
+ ok INTEGER CONSTRAINT rebuild_requires_review CHECK (ok = 1)
138
+ );
139
+ WITH actual(shape) AS (SELECT ${fingerprint(name)}),
140
+ expected(shape) AS (SELECT CASE WHEN EXISTS (SELECT 1 FROM instance_meta WHERE revision = ${config.revision})
141
+ THEN ${nextWitness.shape} ELSE ${previousWitness.shape} END)
142
+ INSERT INTO ${guard} (reason, ok) SELECT
143
+ CASE ${differences.map(part => `WHEN json_extract(actual.shape, '$.${part}') IS NOT json_extract(expected.shape, '$.${part}') THEN ${literal(part)}`).join('\n ')} ELSE '' END,
144
+ actual.shape = expected.shape FROM actual, expected;
145
+ DROP TABLE ${quote(before)};
146
+ DROP TABLE ${quote(after)};`
147
+ // SELECT * comes first deliberately: SQLite keeps its existing column when a
148
+ // fallback alias duplicates it. First apply supplies NULL/default; a replay
149
+ // copies the already-added reference, including links written since apply.
150
+ const fallbacks = added.map(key => `${literal(entity.fields[key].default ?? null)} AS ${quote(key)}`).join(', ')
151
+ return { guard: guardSQL, sql: `-- Rebuild ${name} atomically; physical schema differences require a manually reviewed migration.
152
+ CREATE TABLE ${rows} AS SELECT ${columns.map(quote).join(', ')} FROM (SELECT *, ${fallbacks} FROM ${table});
153
+ DROP TABLE ${table};
154
+ ${createEntity(name, entity)}
155
+ ${supplemental.map(index => `CREATE INDEX ${quote(index.name)} ON ${table} (${['organization_id', ...index.fields].map(quote).join(', ')});`).join('\n')}
156
+ INSERT INTO ${table} (${columns.map(quote).join(', ')}) SELECT ${columns.map(quote).join(', ')} FROM ${rows};
157
+ DROP TABLE ${rows};`, check: `INSERT INTO ${guard} (ok) SELECT NOT EXISTS (SELECT 1 FROM pragma_foreign_key_check);`, cleanup: `DROP TABLE ${guard};` }
158
+ }
@@ -68,12 +68,15 @@ CREATE INDEX memberships_org_role_idx ON memberships(organization_id,role);
68
68
  CREATE TABLE instance_meta (revision INTEGER PRIMARY KEY, core_version TEXT NOT NULL, applied_at TEXT NOT NULL);
69
69
  `
70
70
  export const revisionSQL = (revision: number) => `INSERT INTO instance_meta VALUES (${revision}, '${CORE_VERSION}', strftime('%Y-%m-%dT%H:%M:%fZ','now'));`
71
+ // Revisions are the primary key: preserve older rows and refresh only the current
72
+ // revision's SDK stamp after deploy-time migration verification succeeds.
73
+ export const coreVersionSQL = (revision: number) => `UPDATE instance_meta SET core_version = ${literal(CORE_VERSION)}, applied_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE revision = ${literal(revision)} AND revision = (SELECT MAX(revision) FROM instance_meta) AND core_version <> ${literal(CORE_VERSION)};`
71
74
  export const initialSchema = (config: InstanceConfig) => configuredSystemSchema(config) + opsSchema + (config.account?.entity === 'users' ? betterAuthSchema.replace('user_id TEXT NOT NULL REFERENCES users(id)', 'user_id TEXT NOT NULL REFERENCES "user"(id)') : betterAuthSchema) + emailDeliverySchema + workflowSchema + Object.entries(config.entities).filter(([n]) => !(n === 'users' && config.account?.entity === n)).map(([n, e]) => createEntity(n, e)).join('\n') + '\n' + revisionSQL(config.revision) + '\n'
72
75
 
73
76
  /** Fresh schemas support configured application roles and the explicit users/account extension.
74
77
  * Existing installations keep their append-only migration chain. */
75
78
  function configuredSystemSchema(config: InstanceConfig) {
76
- let sql = systemSchema.replaceAll("'owner','dispatcher','viewer'", [...new Set(['owner', 'dispatcher', 'viewer', ...config.roles])].map(literal).join(','))
79
+ let sql = systemSchema.replace(/ CHECK ?\(role IN \('owner','dispatcher','viewer'\)\)/g, '')
77
80
  if (config.account?.entity === 'users') {
78
81
  const domain = createEntity('users', config.entities.users)
79
82
  .replace('id TEXT PRIMARY KEY,', "id TEXT PRIMARY KEY, email TEXT UNIQUE, password_hash TEXT NOT NULL DEFAULT '', salt TEXT NOT NULL DEFAULT '',")
@@ -3,11 +3,36 @@ import type { Scope } from './types'
3
3
  import { ApiError } from './errors'
4
4
 
5
5
  export const isOrganizationAdministrator = (config: InstanceConfig, role: string) => role === 'owner' || role === config.organizationOwnerRole
6
- /** Preserve the existing role ranks, with the configured owner above ordinary roles. */
7
- export const organizationRoleRank = (config: InstanceConfig, role: string) => isOrganizationAdministrator(config, role) ? 2 : role === 'dispatcher' ? 1 : 0
6
+ /** Shared by resource authorization, live token projection and role granting. */
7
+ export function roleNamePrivileges(role: string): ReadonlySet<string> {
8
+ return new Set(role === 'owner' ? ['audit.read', 'workflows.resume', 'workflows.cancel'] : [])
9
+ }
10
+ /** Role-level authority; row policies, field masks and credential restrictions
11
+ * are enforced separately. Prefixes keep entity and system grants disjoint.
12
+ */
13
+ export function effectiveRolePrivileges(config: InstanceConfig, role: string): ReadonlySet<string> {
14
+ if (!config.roles.includes(role)) return new Set()
15
+ const privileges = new Set([
16
+ ...roleNamePrivileges(role), ...(config.organizationPermissions[role] ?? []),
17
+ // These resource operations are available to every configured membership role.
18
+ 'members.read', 'invitations.read', 'organizations.create', 'members.leave', 'invitations.accept', 'workflows.read',
19
+ ].map(action => `system:${action}`))
20
+ for (const [entity, operations] of Object.entries(config.permissions[role] ?? {})) {
21
+ for (const operation of operations) privileges.add(`entity:${entity}:${operation}`)
22
+ }
23
+ return privileges
24
+ }
25
+ /** A grant may contain only effective privileges held by the issuer.
26
+ * Policies and field masks remain independently enforced at execution time. Unknown
27
+ * roles never authorize a grant, including an otherwise empty target role.
28
+ */
29
+ export function canGrantRole(config: InstanceConfig, actorRole: string, targetRole: string): boolean {
30
+ if (!config.roles.includes(actorRole) || !config.roles.includes(targetRole)) return false
31
+ const issuer = effectiveRolePrivileges(config, actorRole)
32
+ return [...effectiveRolePrivileges(config, targetRole)].every(privilege => issuer.has(privilege))
33
+ }
8
34
  export function organizationPermission(config: InstanceConfig, scope: Scope, action: OrganizationAction) {
9
35
  if (scope.actor.type !== 'user') throw new ApiError(403, 'user_required')
10
36
  if ((action === 'members.read' || action === 'invitations.read') && config.roles.includes(scope.role)) return
11
37
  if (!Object.hasOwn(config.organizationPermissions, scope.role) || !config.organizationPermissions[scope.role].includes(action)) throw new ApiError(403, 'permission_denied')
12
- if (action !== 'members.read' && !action.startsWith('invitations.') && !isOrganizationAdministrator(config, scope.role)) throw new ApiError(403, 'owner_required')
13
38
  }
@@ -1,12 +1,11 @@
1
1
  import type { InstanceConfig, OrganizationAction } from './config'
2
- import { isOrganizationAdministrator, organizationRoleRank } from './execution/permissions'
2
+ import { canGrantRole } from './execution/permissions'
3
3
  import type { Scope } from './identity'
4
4
  import type { AtomicRule, SqlStatement } from './execution/types'
5
5
 
6
- export function issuerGuard(config: InstanceConfig, scope: Scope, action: OrganizationAction, grantRole?: string) {
7
- const roles = Object.entries(config.organizationPermissions).filter(([, grants]) => grants.includes(action)).map(([role]) => role)
8
- .filter(role => action.startsWith('invitations.') || isOrganizationAdministrator(config, role))
9
- .filter(role => grantRole === undefined || organizationRoleRank(config, role) >= organizationRoleRank(config, grantRole))
6
+ export function issuerGuard(config: InstanceConfig, scope: Scope, action: OrganizationAction | undefined, grantRole?: string) {
7
+ const roles = config.roles.filter(role => action === undefined || config.organizationPermissions[role]?.includes(action))
8
+ .filter(role => grantRole === undefined || canGrantRole(config, role, grantRole))
10
9
  return { sql: `EXISTS (SELECT 1 FROM memberships issuer WHERE issuer.organization_id=? AND issuer.user_id=? AND issuer.role IN (${roles.map(() => '?').join(',') || 'NULL'}))`, values: [scope.organizationId, scope.actor.id, ...roles] }
11
10
  }
12
11
  /** Each gate raises an SQL assertion before any later membership, email or audit write. */
@@ -14,7 +13,7 @@ export function organizationRule(name: string, ...statements: SqlStatement[]): A
14
13
  return { name, statements: statements.map(statement => ({ ...statement, expectRows: 1 })) }
15
14
  }
16
15
  export function memberMutationRule(config: InstanceConfig, scope: Scope, userId: string, role: string | null, remove: boolean, leave: boolean): AtomicRule {
17
- const guard = leave ? { sql: '1', values: [] } : issuerGuard(config, scope, remove ? 'members.remove' : 'members.update')
16
+ const guard = leave ? { sql: '1', values: [] } : issuerGuard(config, scope, remove ? 'members.remove' : 'members.update', remove ? undefined : role!)
18
17
  const predicate = `organization_id=? AND user_id=? AND (role<>? OR ?=? OR (SELECT count(*) FROM memberships owners WHERE owners.organization_id=? AND owners.role=?)>1) AND ${guard.sql}`
19
18
  const values = [scope.organizationId, userId, config.organizationOwnerRole, role, config.organizationOwnerRole, scope.organizationId, config.organizationOwnerRole, ...guard.values]
20
19
  return { name: 'last_owner', statements: [{
@@ -1,5 +1,5 @@
1
1
  import { authorizeOperation } from './authorization'
2
- import { isOrganizationAdministrator } from './execution/permissions'
2
+ import { organizationPermission } from './execution/permissions'
3
3
  import { ApiError } from './errors'
4
4
  import { z } from 'zod'
5
5
  import { coreExecutionContext } from './execution/context'
@@ -33,7 +33,8 @@ export function membershipExecutor(config: InstanceConfig, env: RuntimeEnv) {
33
33
  const context = coreExecutionContext(config, scope, new D1AtomicStore(env.DB, { env, config }), new D1AtomicReader(env.DB))
34
34
  const operation = defineAtomicOperation({ name, permission: { system }, input: z.strictObject({}), plan: () => plan })
35
35
  if (['members.update', 'organizations.create', 'invitations.accept'].includes(system)) {
36
- if (context.scope.actor.type !== 'user' || (system === 'members.update' && !isOrganizationAdministrator(config, context.scope.role))) throw new ApiError(403, 'field_forbidden')
36
+ if (context.scope.actor.type !== 'user') throw new ApiError(403, 'field_forbidden')
37
+ if (system === 'members.update') organizationPermission(config, context.scope, system)
37
38
  const capability = Object.freeze({ operation, mode: system === 'members.update' ? 'update' as const : 'create' as const })
38
39
  memberCapabilities.add(capability)
39
40
  memberSlots.set(context, capability)
package/src/policies.ts CHANGED
@@ -5,6 +5,7 @@ import { fieldsFor, fieldType, filterSchema, type Filter } from './query/schema'
5
5
  import type { Predicate, PredicateOperators, TableFilter } from './kernel/plugin'
6
6
  import type { Scope } from './types'
7
7
  import { ApiError } from './errors'
8
+ import { roleNamePrivileges } from './organization-permissions'
8
9
  import { quote } from './migrations/schema'
9
10
  import { kernelFor } from './kernel/registry'
10
11
 
@@ -149,7 +150,7 @@ export function rbacRoles(scope: Scope): string[] {
149
150
  }
150
151
  /** System audit access requires an owner credential and a live owner issuer. */
151
152
  export function hasOwnerRole(scope: Scope): boolean {
152
- return (scope.actor.type !== 'agent' || projections.has(scope)) && rbacRoles(scope).every(role => role === 'owner')
153
+ return (scope.actor.type !== 'agent' || projections.has(scope)) && rbacRoles(scope).every(role => roleNamePrivileges(role).has('audit.read'))
153
154
  }
154
155
  export function policyFilter(config: InstanceConfig, entity: string, scope: Scope): TableFilter {
155
156
  const filters = rbacRoles(scope).map(role => rolePolicyFilter(config, entity, scope, role))