|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#38982] `get_metadata_item` without `state` answers the `version` a |
| 5 | + * following `save_metadata_draft` takes as `ifMatch`. The protocol's |
| 6 | + * `previewDrafts` read, which serves a pending draft in place of the published |
| 7 | + * item, answers no `version` (no single lifecycle's token describes it), so an |
| 8 | + * agent that read an item that way and then saved it guessed `ifMatch` and was |
| 9 | + * refused. The door now answers the token of the lifecycle it served: the |
| 10 | + * draft's when the answer says `draft: true`, the published item's otherwise. |
| 11 | + * |
| 12 | + * Every case runs the real metadata protocol over a SQLite file: each token is |
| 13 | + * one a read answered, and each save is the protocol's own judgement of it. |
| 14 | + * No token is built by hand. |
| 15 | + */ |
| 16 | + |
| 17 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 18 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 19 | +import { tmpdir } from 'node:os'; |
| 20 | +import { join } from 'node:path'; |
| 21 | +import { ObjectQL } from '@objectstack/objectql'; |
| 22 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 23 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 24 | +import { |
| 25 | + SysMetadataObject, |
| 26 | + SysMetadataHistoryObject, |
| 27 | + SysMetadataAuditObject, |
| 28 | + SysMetadataCommitObject, |
| 29 | +} from '@objectstack/metadata-core'; |
| 30 | +import { |
| 31 | + createOpenAuthoringRegistry, |
| 32 | + type OpenAuthoringProtocol, |
| 33 | + type OpenAuthoringReadGateSources, |
| 34 | + type OpenAuthoringWriter, |
| 35 | +} from './mcp-authoring.js'; |
| 36 | + |
| 37 | +const CONTEXT = { door: 'mcp' as const, principal: { userId: 'u1' }, environmentId: 'env_1' }; |
| 38 | +const PKG = 'com.example.crm_studio'; |
| 39 | +const FLOW = 'crm_opportunity_followup'; |
| 40 | + |
| 41 | +const AUTHOR_READ_GATE: OpenAuthoringReadGateSources = (protocol) => ({ |
| 42 | + resolveCaller: async () => ({ userId: 'u1', systemPermissions: ['manage_metadata'], mayWriteItem: true }), |
| 43 | + resolveSecurityService: async () => undefined, |
| 44 | + listMetaItems: (type) => protocol.getMetaItems?.({ type }), |
| 45 | + serviceProbe: () => null, |
| 46 | + navPruneLogged: new Set<string>(), |
| 47 | +}); |
| 48 | + |
| 49 | +const WRITER: OpenAuthoringWriter = { |
| 50 | + userTextLocales: ['en-US'], |
| 51 | + writeVerdict: async () => ({ allowed: true }), |
| 52 | + itemSaveRefusal: async () => undefined, |
| 53 | +}; |
| 54 | + |
| 55 | +/** An autolaunched flow, labelled so each save differs. */ |
| 56 | +const flow = (label: string) => ({ |
| 57 | + name: FLOW, |
| 58 | + label, |
| 59 | + type: 'autolaunched', |
| 60 | + nodes: [ |
| 61 | + { id: 'start', type: 'start', label: 'Start' }, |
| 62 | + { id: 'end', type: 'end', label: 'End' }, |
| 63 | + ], |
| 64 | + edges: [{ id: 'e1', source: 'start', target: 'end' }], |
| 65 | +}); |
| 66 | + |
| 67 | +let cleanup: Array<() => void> = []; |
| 68 | +afterEach(() => { |
| 69 | + for (const c of cleanup.reverse()) c(); |
| 70 | + cleanup = []; |
| 71 | +}); |
| 72 | + |
| 73 | +async function realProtocol(): Promise<OpenAuthoringProtocol> { |
| 74 | + const dir = mkdtempSync(join(tmpdir(), 'os-38982-')); |
| 75 | + cleanup.push(() => rmSync(dir, { recursive: true, force: true })); |
| 76 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: join(dir, 'data.sqlite') }, useNullAsDefault: true }); |
| 77 | + const platform = [SysMetadataObject, SysMetadataHistoryObject, SysMetadataAuditObject, SysMetadataCommitObject] as any[]; |
| 78 | + await driver.initObjects(platform); |
| 79 | + const engine = new ObjectQL(); |
| 80 | + engine.registerDriver(driver as any, true); |
| 81 | + await engine.init(); |
| 82 | + for (const o of platform) engine.registry.registerObject(o, '@objectstack/platform-objects'); |
| 83 | + engine.registry.registerObject({ |
| 84 | + name: 'crm_opportunity', |
| 85 | + label: 'Opportunity', |
| 86 | + sharingModel: 'private', |
| 87 | + fields: { name: { type: 'text', label: 'Name' } }, |
| 88 | + } as any, 'com.example.crm'); |
| 89 | + engine.registry.installPackage({ id: 'com.example.crm', name: 'CRM', version: '1.0.0', type: 'app', scope: 'project' } as any, 'code' as never); |
| 90 | + engine.registry.installPackage({ id: PKG, name: 'CRM Studio', version: '1.0.0', type: 'app' } as any, 'authored'); |
| 91 | + cleanup.push(() => { void engine.destroy(); }); |
| 92 | + return new ObjectStackProtocolImplementation(engine as any) as unknown as OpenAuthoringProtocol; |
| 93 | +} |
| 94 | + |
| 95 | +function door(protocol: OpenAuthoringProtocol) { |
| 96 | + const registry = createOpenAuthoringRegistry({ |
| 97 | + admitted: true, |
| 98 | + userId: 'u1', |
| 99 | + getProtocol: async () => protocol, |
| 100 | + readGate: AUTHOR_READ_GATE, |
| 101 | + writer: WRITER, |
| 102 | + logger: { warn: () => {} }, |
| 103 | + }); |
| 104 | + return async (toolName: string, input: Record<string, unknown>) => { |
| 105 | + const result = await registry.execute({ toolCallId: `t-${toolName}`, toolName, input }, CONTEXT); |
| 106 | + return { isError: result.isError === true, body: JSON.parse(result.output.value) }; |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +/** The item published at `PKG`, with no pending draft. */ |
| 111 | +async function published(protocol: OpenAuthoringProtocol, label = 'Published') { |
| 112 | + await protocol.saveMetaItem!({ type: 'flow', name: FLOW, item: flow(label), packageId: PKG, mode: 'publish', actor: 'u1' } as any); |
| 113 | +} |
| 114 | + |
| 115 | +const save = (call: ReturnType<typeof door>, label: string, ifMatch: unknown) => |
| 116 | + call('save_metadata_draft', { type: 'flow', name: FLOW, item: flow(label), package: PKG, ifMatch }); |
| 117 | + |
| 118 | +describe('[#38982] get_metadata_item without state answers the version a following save takes', () => { |
| 119 | + it('no pending draft: it answers the published item\'s version, the one a state: active read answers, and a save pinned to it is written', async () => { |
| 120 | + const protocol = await realProtocol(); |
| 121 | + await published(protocol); |
| 122 | + const call = door(protocol); |
| 123 | + |
| 124 | + const read = await call('get_metadata_item', { type: 'flow', name: FLOW }); |
| 125 | + expect(read.isError, JSON.stringify(read.body)).toBe(false); |
| 126 | + expect(read.body.draft).toBe(false); |
| 127 | + expect(typeof read.body.version).toBe('string'); |
| 128 | + const active = await call('get_metadata_item', { type: 'flow', name: FLOW, state: 'active' }); |
| 129 | + expect(read.body.version).toBe(active.body.version); |
| 130 | + // The answer's key order is unchanged: the version rides ahead of the item. |
| 131 | + expect(Object.keys(read.body)).toEqual(['type', 'name', 'draft', 'version', 'item']); |
| 132 | + |
| 133 | + const saved = await save(call, 'First edit', read.body.version); |
| 134 | + expect(saved.isError, JSON.stringify(saved.body)).toBe(false); |
| 135 | + expect(saved.body.saved).toMatchObject({ state: 'draft', version: expect.any(String) }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('a pending draft: it answers the draft\'s version, the one a state: draft read answers, and a save pinned to it is written', async () => { |
| 139 | + const protocol = await realProtocol(); |
| 140 | + await published(protocol); |
| 141 | + const call = door(protocol); |
| 142 | + const activeVersion = (await call('get_metadata_item', { type: 'flow', name: FLOW })).body.version; |
| 143 | + expect((await save(call, 'First edit', activeVersion)).isError).toBe(false); |
| 144 | + |
| 145 | + const read = await call('get_metadata_item', { type: 'flow', name: FLOW }); |
| 146 | + expect(read.isError, JSON.stringify(read.body)).toBe(false); |
| 147 | + expect(read.body.draft).toBe(true); |
| 148 | + expect(read.body.item.label).toBe('First edit'); |
| 149 | + const pending = await call('get_metadata_item', { type: 'flow', name: FLOW, state: 'draft' }); |
| 150 | + expect(read.body.version).toBe(pending.body.version); |
| 151 | + expect(read.body.version).not.toBe(activeVersion); |
| 152 | + |
| 153 | + // The control: the published item's version is not the token this save |
| 154 | + // takes once a draft is pending (the card's 409). |
| 155 | + const stale = await save(call, 'Second edit', activeVersion); |
| 156 | + expect(stale.isError).toBe(true); |
| 157 | + expect(stale.body.error).toMatchObject({ code: 'METADATA_CONFLICT', status: 409 }); |
| 158 | + |
| 159 | + const saved = await save(call, 'Second edit', read.body.version); |
| 160 | + expect(saved.isError, JSON.stringify(saved.body)).toBe(false); |
| 161 | + expect((await call('get_metadata_item', { type: 'flow', name: FLOW, state: 'draft' })).body.item.label).toBe('Second edit'); |
| 162 | + }); |
| 163 | + |
| 164 | + it('an item that was never published: the first draft\'s version, and a save pinned to it is written', async () => { |
| 165 | + const protocol = await realProtocol(); |
| 166 | + const call = door(protocol); |
| 167 | + expect((await call('save_metadata_draft', { type: 'flow', name: FLOW, item: flow('Draft only'), package: PKG })).isError).toBe(false); |
| 168 | + |
| 169 | + const read = await call('get_metadata_item', { type: 'flow', name: FLOW }); |
| 170 | + expect(read.body.draft).toBe(true); |
| 171 | + expect(typeof read.body.version).toBe('string'); |
| 172 | + const saved = await save(call, 'Draft edited', read.body.version); |
| 173 | + expect(saved.isError, JSON.stringify(saved.body)).toBe(false); |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +describe('[#38982] a change while the item is read never answers a token newer than what was served', () => { |
| 178 | + /** |
| 179 | + * The real protocol, with `change` run once on the served read |
| 180 | + * (`previewDrafts`), before it reads: a writer that lands between the |
| 181 | + * door's token reads and the served read. |
| 182 | + */ |
| 183 | + function racing(protocol: OpenAuthoringProtocol, change: () => Promise<unknown>): OpenAuthoringProtocol { |
| 184 | + let fired = false; |
| 185 | + return new Proxy(protocol, { |
| 186 | + get(target, key, receiver) { |
| 187 | + const value = Reflect.get(target, key, receiver); |
| 188 | + if (key !== 'getMetaItem' || typeof value !== 'function') return typeof value === 'function' ? value.bind(target) : value; |
| 189 | + return async (request: { previewDrafts?: boolean }) => { |
| 190 | + if (request.previewDrafts && !fired) { |
| 191 | + fired = true; |
| 192 | + await change(); |
| 193 | + } |
| 194 | + return value.call(target, request); |
| 195 | + }; |
| 196 | + }, |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | + it('the draft saved again in between: the token answered is the older draft\'s, and a save pinned to it is refused, never written over the newer draft', async () => { |
| 201 | + const protocol = await realProtocol(); |
| 202 | + await published(protocol); |
| 203 | + const direct = door(protocol); |
| 204 | + const activeVersion = (await direct('get_metadata_item', { type: 'flow', name: FLOW })).body.version; |
| 205 | + expect((await save(direct, 'First edit', activeVersion)).isError).toBe(false); |
| 206 | + const firstDraftVersion = (await direct('get_metadata_item', { type: 'flow', name: FLOW, state: 'draft' })).body.version; |
| 207 | + |
| 208 | + const call = door(racing(protocol, async () => { |
| 209 | + expect((await save(direct, 'Someone else', firstDraftVersion)).isError).toBe(false); |
| 210 | + })); |
| 211 | + const read = await call('get_metadata_item', { type: 'flow', name: FLOW }); |
| 212 | + expect(read.body.draft).toBe(true); |
| 213 | + expect(read.body.item.label).toBe('Someone else'); |
| 214 | + expect(read.body.version).toBe(firstDraftVersion); |
| 215 | + |
| 216 | + const refused = await save(direct, 'Mine', read.body.version); |
| 217 | + expect(refused.body.error).toMatchObject({ code: 'METADATA_CONFLICT', status: 409 }); |
| 218 | + expect((await direct('get_metadata_item', { type: 'flow', name: FLOW, state: 'draft' })).body.item.label).toBe('Someone else'); |
| 219 | + }); |
| 220 | + |
| 221 | + it('the draft discarded in between: the published item is served with no version, since the token read was the draft\'s', async () => { |
| 222 | + const protocol = await realProtocol(); |
| 223 | + await published(protocol); |
| 224 | + const direct = door(protocol); |
| 225 | + const activeVersion = (await direct('get_metadata_item', { type: 'flow', name: FLOW })).body.version; |
| 226 | + expect((await save(direct, 'First edit', activeVersion)).isError).toBe(false); |
| 227 | + |
| 228 | + const call = door(racing(protocol, async () => { |
| 229 | + expect((await direct('delete_metadata_draft', { type: 'flow', name: FLOW, package: PKG })).isError).toBe(false); |
| 230 | + })); |
| 231 | + const read = await call('get_metadata_item', { type: 'flow', name: FLOW }); |
| 232 | + expect(read.isError, JSON.stringify(read.body)).toBe(false); |
| 233 | + expect(read.body.draft).toBe(false); |
| 234 | + expect('version' in read.body).toBe(false); |
| 235 | + }); |
| 236 | + |
| 237 | + it('a first draft saved in between: the draft is served with no version, since the token read was the published item\'s', async () => { |
| 238 | + const protocol = await realProtocol(); |
| 239 | + await published(protocol); |
| 240 | + const direct = door(protocol); |
| 241 | + const activeVersion = (await direct('get_metadata_item', { type: 'flow', name: FLOW, state: 'active' })).body.version; |
| 242 | + |
| 243 | + const call = door(racing(protocol, async () => { |
| 244 | + expect((await save(direct, 'Someone else', activeVersion)).isError).toBe(false); |
| 245 | + })); |
| 246 | + const read = await call('get_metadata_item', { type: 'flow', name: FLOW }); |
| 247 | + expect(read.isError, JSON.stringify(read.body)).toBe(false); |
| 248 | + expect(read.body.draft).toBe(true); |
| 249 | + expect('version' in read.body).toBe(false); |
| 250 | + }); |
| 251 | +}); |
0 commit comments