Skip to content

Commit 80bfe64

Browse files
committed
feat(runtime)!: clone answers its package; SDK method, ledger row, Setup link, docs, changeset
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5pRVPsxnFxPBAQLUMh
1 parent 97489b2 commit 80bfe64

12 files changed

Lines changed: 219 additions & 49 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'@objectstack/spec': patch
3+
'@objectstack/runtime': patch
4+
'@objectstack/client': patch
5+
'@object-ui/app-shell': patch
6+
---
7+
8+
feat(runtime)!: the flow clone door is "new similar" — the copy lands switched off in a writable package, by default the source's paired `<base>.custom`, never package-less (#28149)
9+
10+
Clause-②: yes (narrowing) — `POST /api/v1/automation/:name/clone` accepts a new optional `package` member and answers two new members (`packageId`, the copy's package, beside `flow` and `notice`), and a request it accepted before is now refused: a clone of a flow no read-only package ships, sent with no `package`, is refused `400` where it was written package-less.
11+
Metadata-protocol: none
12+
13+
<!-- adr-0087: not-required (no-migration-prescription) No key of any metadata schema is added, removed, renamed or re-shaped and no stored metadata changes: the door's request body gains one optional member and its answer two, and a copy written before this change stays where it is. The remedy for the newly refused request is the request's own (name the writable package with `package`), so there is nothing for a migration to rewrite and no tombstone. -->
14+
15+
**BREAKING** request narrowing at the flow clone door (`@objectstack/runtime`'s `/automation` domain), per ADR-0146 D3/D4.
16+
17+
- **Before.** The clone door wrote the copy as a package-less environment row with `status: 'draft'`, which the engine arms like `active`, so a copy of a record-triggered flow ran beside its source as soon as it was saved.
18+
- **Now.** The copy lands in a writable package and is written switched off (`status: 'obsolete'`, the value Studio's Enabled switch writes for Off). With no `package`, the target is the source package's paired customization package `<base>.custom`, ensured through the protocol's ensure door (the one creation path of the pair). With `package`, the target must be installed and writable (`422 WRITABLE_PACKAGE_REQUIRED` otherwise) and depend on the source's package (`409 RESOURCE_CONFLICT` otherwise). Every refusal is answered before anything is written. The save is a first write, so a name the target package already holds is refused `409 RESOURCE_CONFLICT` instead of overwriting it. The answer is `{ flow, packageId, notice }`, and the notice says the copy is switched off.
19+
- **Unchanged.** The whole-definition copy, the mandatory new name and label, the credential refusal, the walled-environment refusal and the absence of clone lineage.
20+
- **New surface.** `CloneFlowRequestSchema` and `CloneFlowResponseSchema` (`@objectstack/spec/api`), the SDK's `client.automation.clone(name, { name, label, package? })`, and the route ledger row now `sdk` with both schemas named. Setup's "Open the clone in Studio" link opens the copy in the package the answer names.
21+
- **Who sees it.** A client that clones a flow authored in the deployment (or in a writable package) without naming a package; and anything that read the copy's `status` as `draft`.
22+
23+
**The remedy.** Name the writable package the copy lands in with `package`. To have the copy run, switch it on (`POST /api/v1/automation/:name/toggle`, body `{enabled: true}`).

‎content/docs/concepts/metadata-lifecycle.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ See [ADR-0005](https://github.com/objectstack-ai/objectstack/blob/main/docs/adr/
146146

147147
| Type | Sanctioned path the refusal names |
148148
| :--- | :--- |
149-
| `flow` | Clone it under a new name (`POST /api/v1/automation/:name/clone`, body `{name, label}`), or switch it off (`POST /api/v1/automation/:name/toggle`, body `{enabled: false}`). A flow that holds a credential (an inbound hook's `secret`, an `http` node's `signingSecret`) is not cloned in one step: the clone door refuses it with `409`, because a copy would share the secret, and the copy is authored as a new flow with its own. |
149+
| `flow` | Clone it under a new name (`POST /api/v1/automation/:name/clone`, body `{name, label}`), or switch it off (`POST /api/v1/automation/:name/toggle`, body `{enabled: false}`). A flow that holds a credential (an inbound hook's `secret`, an `http` node's `signingSecret`) is not cloned in one step: the clone door refuses it with `409`, because a copy would share the secret, and the copy is authored as a new flow with its own. The copy is written switched off, into the source package's paired customization package `<base>.custom` (created on first use), or into the writable package the body's optional `package` names; a flow no read-only package ships has no pair, so its clone names `package`. The answer's `packageId` says where the copy landed ([ADR-0146](https://github.com/objectstack-ai/objectstack/blob/main/docs/adr/0146-studio-designs-setup-administers-one-editor-per-item-paired-customize-package.md) D3, D4). |
150150
| `action` | Switch it off (`POST /api/v1/actions/_activation/:object/:action`, body `{enabled: false}`; `:object` is `global` for an object-less action). No clone is offered. |
151151
| `permission` | Clone it under a new name: the "Clone" action on the permission set, or the clone door `POST /api/v1/security/permission-sets/:name/clone` (body `{name, label}`) with a new name. The clone door copies the whole definition from the security catalog. |
152152

‎packages/client/src/index.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ import {
129129
ReassignOrphanedMetadataResponse,
130130
DuplicatePackageResponse,
131131
EnsureCustomizationPackageResponse,
132+
CloneFlowRequest,
133+
CloneFlowResponse,
132134
ValidateDataIssue,
133135
} from '@objectstack/spec/api';
134136
// [#17536] The element the two `/packages` READ doors are declared to serve.
@@ -5875,6 +5877,40 @@ export class ObjectStackClient {
58755877
return this.unwrapResponse(res);
58765878
},
58775879

5880+
/**
5881+
* [#28149, ADR-0146 D4] "New similar": copy the flow `name` under a NEW
5882+
* machine name, into a package, switched off.
5883+
*
5884+
* `POST /automation/:name/clone` copies the whole definition, renames
5885+
* and relabels it, and writes it with `status: 'obsolete'`, so the copy
5886+
* never runs beside its original; switch it on by saving its
5887+
* definition with `status: 'active'` into its package and publishing.
5888+
* Its references are not re-pointed, and nothing records what it was
5889+
* copied from (ADR-0126 §9).
5890+
*
5891+
* `body.package` names the package the copy lands in: an installed,
5892+
* writable package whose dependency closure holds the source's
5893+
* package. Omitted, the copy lands in the source's paired
5894+
* customization package `<base>.custom`, created on first use through
5895+
* the ensure door (`packages.ensureCustomization`). The answer names the
5896+
* package in `packageId`.
5897+
*
5898+
* Refused, with nothing written: an unknown source (404); a taken name
5899+
* (409 `RESOURCE_CONFLICT`); a source holding a credential (409); an
5900+
* unknown or read-only `package` (422 `WRITABLE_PACKAGE_REQUIRED`), or
5901+
* one whose closure lacks the source's package (409
5902+
* `RESOURCE_CONFLICT`); `package` omitted for a source no package ships,
5903+
* which has no pair (400). Requires `manage_metadata`.
5904+
*/
5905+
clone: async (name: string, body: CloneFlowRequest): Promise<CloneFlowResponse['data']> => {
5906+
const route = this.getRoute('automation');
5907+
const res = await this.fetch(`${this.baseUrl}${route}/${encodeURIComponent(name)}/clone`, {
5908+
method: 'POST',
5909+
body: JSON.stringify(body),
5910+
});
5911+
return this.unwrapResponse<CloneFlowResponse['data']>(res);
5912+
},
5913+
58785914
/**
58795915
* Execution run history
58805916
*/

‎packages/qa/dogfood/test/automation-flow-clone-door.dogfood.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const SOURCE = 'crm_convert_lead_wizard';
4545
/** A customer-owned machine name no shipped flow uses. */
4646
const CLONE = 'crm_convert_lead_wizard_clone_20676';
4747
const CLONE_LABEL = 'Convert Lead (clone)';
48+
/** [#28149] The CRM app's paired customization package (`<base>.custom`, ADR-0146 D3). */
49+
const CRM_PAIR = 'com.example.crm.custom';
4850

4951
interface ErrorBody { success?: boolean; error?: { code?: string; httpStatus?: number } }
5052

@@ -76,7 +78,7 @@ describe('#20676 — POST /automation/:name/clone is mounted on the dispatcher b
7678
expect(readBack.status).toBe(404);
7779
});
7880

79-
it('a legal clone answers 200 with the flow and FLOW_CLONE_NOTICE, and the clone reads back', async () => {
81+
it('a legal clone answers 200 with the flow, its package and FLOW_CLONE_NOTICE, and the clone reads back', async () => {
8082
// Control: the target does not exist before the clone, so the read-back
8183
// below is evidence of THIS request rather than of the fixture.
8284
const before = await stack.apiAs(adminToken, 'GET', `/automation/${CLONE}`);
@@ -90,11 +92,14 @@ describe('#20676 — POST /automation/:name/clone is mounted on the dispatcher b
9092
expect(res.status, `legal clone: ${text}`).toBe(200);
9193
const body = (await res.json()) as {
9294
success?: boolean;
93-
data?: { flow?: { name?: string; label?: string; status?: string }; notice?: string };
95+
data?: { flow?: { name?: string; label?: string; status?: string }; packageId?: string; notice?: string };
9496
};
9597
expect(body.success).toBe(true);
9698
expect(body.data?.notice).toBe(FLOW_CLONE_NOTICE);
97-
expect(body.data?.flow).toMatchObject({ name: CLONE, label: CLONE_LABEL, status: 'draft' });
99+
// [#28149, ADR-0146 D4] "New similar": switched off, in the CRM's paired
100+
// customization package, which the clone ensured on first use.
101+
expect(body.data?.flow).toMatchObject({ name: CLONE, label: CLONE_LABEL, status: 'obsolete' });
102+
expect(body.data?.packageId).toBe(CRM_PAIR);
98103

99104
const readBack = await stack.apiAs(adminToken, 'GET', `/automation/${CLONE}`);
100105
const readText = await readBack.clone().text();

‎packages/runtime/src/domains/automation-flow-clone-custom-package.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import { ObjectQL } from '@objectstack/objectql';
4141
import { SqlDriver } from '@objectstack/driver-sql';
4242
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
4343
import { SysMetadata, SysMetadataHistoryObject, SysMetadataAuditObject } from '@objectstack/platform-objects/metadata';
44-
import { CloneFlowResponseSchema } from '@objectstack/spec/api';
44+
import { CloneFlowRequestSchema, CloneFlowResponseSchema } from '@objectstack/spec/api';
45+
import { ROUTE_LEDGER } from '../route-ledger.js';
4546
import { HttpDispatcher } from '../http-dispatcher.js';
4647
import { validationFailureDetails, VALIDATION_FAILED_STATUS } from '../validation-failure.js';
4748
import { FLOW_CLONE_NOTICE, FLOW_CLONE_STATUS } from '../flow-clone.js';
@@ -308,3 +309,33 @@ describe('[#28149] a source no package ships has no pair', () => {
308309
expect(answer.data.flow.status).toBe('obsolete');
309310
}, 60_000);
310311
});
312+
313+
describe('[#28149] the contract: the ledger row names the schemas this suite parses with', () => {
314+
it('the clone row is SDK surface and names CloneFlowRequestSchema / CloneFlowResponseSchema', () => {
315+
const row = ROUTE_LEDGER.find((e) => e.route === 'POST /automation/:name/clone');
316+
expect(row, 'the clone row must exist in ROUTE_LEDGER').toBeDefined();
317+
expect(row!.disposition).toBe('sdk');
318+
expect(row!.client).toBe('automation.clone');
319+
expect(row!.requestSchema).toBe('CloneFlowRequestSchema');
320+
expect(row!.responseSchema).toBe('CloneFlowResponseSchema');
321+
});
322+
323+
it('each declared member reaches the door as sent, and an undeclared one is refused on its key', async () => {
324+
expect(Object.keys(CloneFlowRequestSchema.shape).sort()).toEqual(['label', 'name', 'package']);
325+
const rig = await boot();
326+
327+
const sent = { name: 'ext_sent', label: 'Sent as is', package: EXT.id };
328+
expect(CloneFlowRequestSchema.safeParse(sent).success).toBe(true);
329+
const landed = await rig.clone(SOURCE, sent);
330+
expect(landed.status, JSON.stringify(landed.body)).toBe(200);
331+
expect(landed.data.flow).toMatchObject({ name: sent.name, label: sent.label });
332+
expect(landed.data.packageId).toBe(sent.package);
333+
334+
const undeclared = { name: 'ext_status', label: 'Chooses its status', status: 'active' };
335+
expect(CloneFlowRequestSchema.safeParse(undeclared).success).toBe(false);
336+
const refused = await rig.clone(SOURCE, undeclared);
337+
expect(refused.status).toBe(400);
338+
expect(refused.body.error.fields).toMatchObject([{ field: 'status', code: 'unknown_field' }]);
339+
expect(await rig.rows('ext_status')).toEqual([]);
340+
}, 60_000);
341+
});

‎packages/runtime/src/domains/automation-flow-save-actor.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ import type { HttpProtocolContext } from '../http-dispatcher.js';
3636
/** `registry.registerObject` requires a package id (see the sibling real-stack tests). */
3737
const TEST_PACKAGE_ID = 'objectstack-test';
3838
const SOURCE = 'customer_source_flow';
39+
/**
40+
* [#28149] A clone lands in a writable package, never package-less. SOURCE is
41+
* authored in the deployment, so no package ships it and it has no paired
42+
* `<base>.custom`: its copy names this installed, writable package.
43+
*/
44+
const TARGET = { id: 'com.acme.workspace', name: 'Workspace', version: '1.0.0', type: 'app', namespace: 'ws' };
3945

4046
const definitionOf = (name: string, label: string) => ({
4147
name,
@@ -78,6 +84,7 @@ async function boot() {
7884
engine.registry.registerObject(SysMetadataHistoryObject as any, TEST_PACKAGE_ID);
7985
engine.registry.registerObject(SysMetadataAuditObject as any, TEST_PACKAGE_ID);
8086
await engine.syncSchemas();
87+
engine.registry.installPackage(TARGET as any, 'code');
8188
const protocol = new ObjectStackProtocolImplementation(engine as any);
8289

8390
const flows = new Map<string, unknown>([[SOURCE, definitionOf(SOURCE, 'Source')]]);
@@ -88,7 +95,7 @@ async function boot() {
8895
getFlow: async (name: string) => flows.get(name) ?? null,
8996
listFlows: async () => [...flows.keys()],
9097
};
91-
const services: Record<string, unknown> = { protocol, automation };
98+
const services: Record<string, unknown> = { protocol, automation, objectql: engine };
9299
const resolve = (name: string): unknown => services[name];
93100
const kernel: any = { getService: resolve, getServiceAsync: async (name: string) => resolve(name), context: { getService: resolve } };
94101

@@ -123,7 +130,7 @@ describe('[#27414] the /automation definition doors record the user who saved',
123130
it('POST /:name/clone records the admin who cloned on the copy', async () => {
124131
const h = await boot();
125132
const result = await h.dispatcher.handleAutomation(
126-
`/${SOURCE}/clone`, 'POST', { name: 'my_copy', label: 'My Copy' }, author('u_admin'), undefined,
133+
`/${SOURCE}/clone`, 'POST', { name: 'my_copy', label: 'My Copy', package: TARGET.id }, author('u_admin'), undefined,
127134
);
128135
expect(statusOf(result)).toBe(200);
129136
expect(await h.recorded('my_copy')).toEqual({ created_by: 'u_admin', updated_by: 'u_admin', recorded_by: ['u_admin'] });
@@ -135,7 +142,7 @@ describe('[#27414] the /automation definition doors record the user who saved',
135142
expect(statusOf(await h.dispatcher.handleAutomation('/sys_flow', 'PUT', { definition: definitionOf('sys_flow', 'v2') }, SYSTEM(), undefined))).toBe(200);
136143
expect(await h.recorded('sys_flow')).toEqual({ created_by: null, updated_by: null, recorded_by: [null, null] });
137144

138-
expect(statusOf(await h.dispatcher.handleAutomation(`/${SOURCE}/clone`, 'POST', { name: 'sys_copy', label: 'System Copy' }, SYSTEM(), undefined))).toBe(200);
145+
expect(statusOf(await h.dispatcher.handleAutomation(`/${SOURCE}/clone`, 'POST', { name: 'sys_copy', label: 'System Copy', package: TARGET.id }, SYSTEM(), undefined))).toBe(200);
139146
expect(await h.recorded('sys_copy')).toEqual({ created_by: null, updated_by: null, recorded_by: [null] });
140147
}, 60_000);
141148
});

‎packages/runtime/src/domains/automation-packaged-base-lock.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import type { HttpProtocolContext } from '../http-dispatcher.js';
5151
/** A flow a code package ships — registered with a package id, as an artifact loader does. */
5252
const PACKAGED = 'pkg_alert_flow';
5353
const PACKAGE_ID = 'com.example.pkg';
54+
/** [#28149] The base's paired customization package — where a clone lands. */
55+
const PAIR_PACKAGE_ID = 'com.example.pkg.custom';
5456
/** The customer's own flow — lives in the engine only (authored through `POST /automation`). */
5557
const CUSTOMER = 'customer_flow';
5658
/** A runtime-authored registry item with no package provenance. */
@@ -127,8 +129,17 @@ function boot(
127129
environmentId,
128130
);
129131

132+
// [#28149] The installed packages the clone door's target rule reads: the
133+
// code-shipped base (read-only) and its writable pair, which depends on it.
134+
const manifests: Record<string, { id: string; scope?: string; dependencies?: Record<string, string> }> = {
135+
[PACKAGE_ID]: { id: PACKAGE_ID, scope: 'system' },
136+
[PAIR_PACKAGE_ID]: { id: PAIR_PACKAGE_ID, dependencies: { [PACKAGE_ID]: '*' } },
137+
};
138+
const objectql = { registry: { getPackage: (id: string) => (manifests[id] ? { manifest: manifests[id] } : undefined) } };
139+
130140
const services: Record<string, unknown> = {
131141
automation: { handlerReady: true, registerFlow, unregisterFlow, toggleFlow, getFlow },
142+
objectql,
132143
};
133144
if (mode === 'real') services.protocol = protocol;
134145
const resolve = (name: string): unknown => services[name];
@@ -350,11 +361,20 @@ describe('what the lock leaves open', () => {
350361
// protocol's own save — pinned in
351362
// `automation-tenant-authored-write.test.ts`; stood in for here, where
352363
// the store is not this file's subject.
353-
vi.spyOn(h.protocol, 'saveMetaItem').mockResolvedValue({ success: true } as never);
364+
const save = vi.spyOn(h.protocol, 'saveMetaItem').mockResolvedValue({ success: true } as never);
365+
// [#28149] The copy lands in the source's paired `<base>.custom`,
366+
// ensured through N-13's door — stood in for here (its own suite pins
367+
// it); the pair's manifest depends on the base, as the ensure door
368+
// writes it.
369+
vi.spyOn(h.protocol, 'ensureCustomizationPackage').mockResolvedValue(
370+
{ basePackageId: PACKAGE_ID, packageId: PAIR_PACKAGE_ID } as never,
371+
);
354372
const { response } = await h.dispatcher.handleAutomation(
355373
`/${PACKAGED}/clone`, 'POST', { name: 'my_alert_copy', label: 'My Alert' }, AUTHOR(), undefined,
356374
);
357375
expect(statusOf(response)).toBe(200);
376+
expect((response as any)?.body?.data?.packageId).toBe(PAIR_PACKAGE_ID);
377+
expect(save).toHaveBeenCalledWith(expect.objectContaining({ packageId: PAIR_PACKAGE_ID }));
358378
expect(h.registerFlow).toHaveBeenCalledWith('my_alert_copy', expect.objectContaining({ name: 'my_alert_copy' }));
359379
expect(h.held(PACKAGED)).toEqual(definitionOf(PACKAGED));
360380
});

0 commit comments

Comments
 (0)