Skip to content
Merged
21 changes: 21 additions & 0 deletions .changeset/compose-stacks-job-name-across-packages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/spec': minor
---

fix(spec)!: composition refuses one job name enabled in two packages of the composed artifact, where the boot would schedule none of them

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — no skill teaches declaring one job name in two packages of a composition; the refusal names every enabled job of the name and prescribes the fix

<!-- adr-0087: registered compose-stacks-job-name-across-packages-refused -->

**BREAKING** accept-set narrowing, shipped as `minor`.

- **Refused now, at composition (`STACK_CROSS_REFERENCE_INVALID`, 422), once the result carries `packages[]`.** One job name enabled in two package bodies, or in a package body and the residual (a top-level job no body declares: under `composeStacks(stacks, { manifest: 'preserve' })` an input with no `manifest`; under `'first'`, `'last'` or an index any top-level job beside an input that is itself a multi-package artifact). The boot's job binder schedules every job of an artifact by its bare name, whichever package declares it, and refuses a name two enabled jobs share, so none of them was scheduled (`NOT SCHEDULED` at `error`); the install-local door already refused such a package. The finding names every enabled job of the name: `Job 'crm_nightly' is declared enabled twice across the packages of one composed artifact: 'com.example.sales' (stack #0) jobs[0] and 'com.example.billing' (stack #1) jobs[0]. …`, a residual job marked `(the top level, outside every package body)`.
- **Read as the binder reads it.** Enabled jobs only, by name; each job counted once per place it is reached, so one job object that two inputs carry (a built stack shared by two compositions, or passed twice) is two jobs, as the binder binds both; the residual is counted beyond the package bodies' flattened copies, never by content.
- **Also refused now: the residual's own pair beside a body job of that name.** Two residual jobs of one name are the one-package finding (`Job 'NAME' is declared twice in \`jobs\``), as before when no package body carries the name. When a body does carry it, the pair used to be read as that body's copies and composed; it is refused now, with the one-package wording, which counts a disabled job too. So is one job object reached twice in one holder.
- **Unchanged.** A disabled twin composes (a disabled job is scheduled by nothing). A body job's own flattened copy is not a second job. Distinct job names compose. A flat composition (no `packages[]`) is one package and keeps its own finding.
- **Installed artifacts.** An artifact built before this keeps loading; the binder still refuses the name at boot, naming every holder.

FROM → TO: two packages composed into one artifact each declaring an enabled job `crm_nightly` → rename one of the two jobs, or disable one (`enabled: false`).
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,14 @@ describe('[#32572] install-local refuses a package with two enabled jobs of one
jobs: [TWIN],
} as never);
// No `manifest`: its jobs reach the artifact's top level and no package body — the residual.
const host = defineStack({ objects: [{ ...SYNCED, name: 'jobs_app_host_rec', label: 'Host record' }], jobs: [TWIN_SLOW] } as never);
// Composition now refuses a job name enabled in a body and the residual, so this is an artifact it did
// not build (the door's backstop case): the residual's job is composed under a stand-in name, and its
// own name restored in the JSON the door reads.
const STAND_IN = `${TWIN.name}_stand_in`;
const host = defineStack({ objects: [{ ...SYNCED, name: 'jobs_app_host_rec', label: 'Host record' }], jobs: [{ ...TWIN_SLOW, name: STAND_IN }] } as never);
const bundle = JSON.parse(JSON.stringify(composeStacks([alpha, host, defineStack(artifact(undefined) as never)] as never[], { manifest: 'preserve' })));
for (const job of bundle.jobs as Array<{ name: string }>) if (job.name === STAND_IN) job.name = TWIN.name;
expect(bundle.jobs.map((job: { name: string }) => job.name), 'the body job\'s top-level copy and the residual\'s own job').toEqual([TWIN.name, TWIN.name]);

const res = await install(bundle);

Expand Down
59 changes: 46 additions & 13 deletions packages/runtime/src/app-artifact-handlers.job-pull.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,30 @@ const beta = defineStack({
/** The artifact as `os build` writes it and a boot reads it back: composed, then through JSON. */
const jsonLoaded = (stacks: unknown[]) =>
JSON.parse(JSON.stringify(composeStacks(stacks as never[], { manifest: 'preserve' }))) as Record<string, any>;
/** The stand-in suffix {@link handBuilt} gives each input's job names, and takes back off. */
const STAND_IN = '__hand_built_';
/**
* The artifact a build emitted before composition judged a job name enabled in two holders — the binder's
* backstop case, an artifact `composeStacks` did not build. `composeStacks` now refuses that pair, so it is
* composed with each input's job names made unique and the names restored in the JSON a boot reads: the
* layout {@link jsonLoaded} emits (the body copies at the top level, the residual's own jobs beside them),
* name for name. Pinned equal to {@link jsonLoaded} on a composition that still composes, below.
*/
const handBuilt = (stacks: unknown[]) => {
const tagged = stacks.map((stack, i) => {
const plain = JSON.parse(JSON.stringify(stack)) as { jobs?: Array<{ name: string }> };
if (!Array.isArray(plain.jobs)) return stack;
return defineStack({ ...plain, jobs: plain.jobs.map((job) => ({ ...job, name: `${job.name}${STAND_IN}${i}` })) } as never);
});
const bundle = jsonLoaded(tagged);
const restore = (jobs: unknown) => {
if (!Array.isArray(jobs)) return;
for (const job of jobs as Array<{ name: string }>) job.name = job.name.replace(new RegExp(`${STAND_IN}\\d+$`), '');
};
restore(bundle.jobs);
for (const entry of (bundle.packages ?? []) as Array<{ manifest?: { jobs?: unknown } }>) restore(entry.manifest?.jobs);
return bundle;
};

/**
* Schedule `bundle` as `appId` on a real job service (`CronJobAdapter` unless `makeAdapter` says otherwise), then fire every job it holds:
Expand Down Expand Up @@ -845,20 +869,29 @@ async function scheduleAndRun(bundle: unknown, appId: string, makeAdapter: () =>
* was pulled as the body before. [#32411] With both jobs enabled the binder now
* refuses the name at bind and schedules neither; a run meets the attribution
* refusal when the name's twin is disabled (the fail-closed pin below).
* Composition now refuses an ENABLED pair, so an enabled pair here is an artifact it did not build
* ({@link handBuilt}): the binder is the backstop for it.
*/
describe('[#32355] a job name a package body declares and the residual carries is attributed to neither', () => {
withPosture(undefined);

it('`handBuilt` is the layout a build emits: on compositions that still compose, it equals `jsonLoaded`', () => {
const withResidual = [alpha([pullJob('sync', 'alpha_map'), pullJob('alpha_only', 'alpha_map')]), host([pullJob('host_sync', 'host_map')]), beta];
expect(handBuilt(withResidual)).toEqual(jsonLoaded(withResidual));
const bodiesOnly = [alpha([pullJob('sync', 'alpha_map')]), beta];
expect(handBuilt(bodiesOnly)).toEqual(jsonLoaded(bodiesOnly));
});

it('the artifact a build emits carries the pair: the body job copied to the top level, and the residual\'s own job of the same name', () => {
const bundle = jsonLoaded([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]);
const bundle = handBuilt([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]);
expect(bundle.manifest.id).toBe('com.example.beta');
expect(bundle.jobs.map((j: any) => [j.name, j.pull.mapping])).toEqual([['sync', 'alpha_map'], ['sync', 'host_map']]);
expect(bundle.packages.map((p: any) => [p.manifest.id, (p.manifest.jobs ?? []).map((j: any) => j.name)]))
.toEqual([['com.example.alpha', ['sync']], ['com.example.beta', []]]);
});

it('JSON-loaded, on a real job service: the name is attributed to neither, and both enabled jobs are refused at bind — never pulled as the body', async () => {
const bundle = jsonLoaded([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]);
const bundle = handBuilt([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]);
const { out, answers, pulls } = await scheduleAndRun(bundle, 'com.example.beta');
// [#32411] Neither is scheduled: one key would have held only the later (the residual's, `host_map`).
expect(out.pulls).toEqual([]);
Expand Down Expand Up @@ -889,7 +922,7 @@ describe('[#32355] a job name a package body declares and the residual carries i

it('control: the residual\'s owner IS the one declaring body — one holder, that body; both enabled, refused at bind', async () => {
// `alpha` last: the artifact's id is alpha's, so the residual runs as alpha, the body that declares `sync`.
const bundle = jsonLoaded([host([pullJob('sync', 'host_map')]), alpha([pullJob('sync', 'alpha_map')])]);
const bundle = handBuilt([host([pullJob('sync', 'host_map')]), alpha([pullJob('sync', 'alpha_map')])]);
expect(bundle.manifest.id).toBe('com.example.alpha');
// Attribution: one holder, alpha, for every job of the name — as before.
const of = jobDeclaringPackages(bundle, 'com.example.alpha');
Expand All @@ -902,7 +935,7 @@ describe('[#32355] a job name a package body declares and the residual carries i
});

it('controls: a residual job with a name of its own binds as today, and a body job with no residual twin binds to its body', async () => {
const bundle = jsonLoaded([
const bundle = handBuilt([
alpha([pullJob('sync', 'alpha_map'), pullJob('alpha_only', 'alpha_map')]),
host([pullJob('host_sync', 'host_map'), pullJob('sync', 'host_map')]),
beta,
Expand Down Expand Up @@ -974,9 +1007,9 @@ describe('[#32411] two enabled jobs of one name in one artifact are refused at b
// or alpha's (the residual runs as alpha, the one body declaring `sync`).
const ARRANGEMENTS = [
['the residual runs as a package that does not declare the name', (jobs: { body: unknown[]; residual: unknown[] }) =>
({ bundle: jsonLoaded([alpha(jobs.body), host(jobs.residual), beta]), appId: 'com.example.beta' })],
({ bundle: handBuilt([alpha(jobs.body), host(jobs.residual), beta]), appId: 'com.example.beta' })],
['the residual runs as the one body that declares the name', (jobs: { body: unknown[]; residual: unknown[] }) =>
({ bundle: jsonLoaded([host(jobs.residual), alpha(jobs.body)]), appId: 'com.example.alpha' })],
({ bundle: handBuilt([host(jobs.residual), alpha(jobs.body)]), appId: 'com.example.alpha' })],
] as const;

/** The `error` calls that carry the coded refusal for `name`: the message, and the error's code and status. */
Expand Down Expand Up @@ -1024,7 +1057,7 @@ describe('[#32411] two enabled jobs of one name in one artifact are refused at b
const tick = (note: string) => ({ name: 'tick', schedule: INTERVAL, body: { language: 'js', capabilities: [], source: `return; // ${note}` } });
const gamma = defineStack({ manifest: { id: 'com.example.gamma', name: 'Gamma', version: '0.1.0', type: 'module' }, jobs: [tick('gamma')] } as never);
const delta = defineStack({ manifest: { id: 'com.example.delta', name: 'Delta', version: '0.1.0', type: 'app' }, jobs: [tick('delta')] } as never);
const bundle = jsonLoaded([gamma, delta]);
const bundle = handBuilt([gamma, delta]);
const { out, answers, logger } = await scheduleAndRun(bundle, 'com.example.delta', () => new IntervalJobAdapter());
expect(out.bodies).toEqual([]);
expect(out.refused).toEqual(['tick', 'tick']);
Expand Down Expand Up @@ -1108,7 +1141,7 @@ describe('[#32412] the residual-twin job refusal names the residual as a holder
mappings: [{ ...mappingOf('beta_map', 'beta_api'), targetObject: 'beta_record' }],
jobs,
} as never);
const twin = () => jsonLoaded([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]);
const twin = () => handBuilt([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]);

it('no body carries the artifact\'s id: two candidates — the declaring body and the residual — never one', () => {
const bundle = twin();
Expand All @@ -1133,7 +1166,7 @@ describe('[#32412] the residual-twin job refusal names the residual as a holder
});

it('the residual\'s owner is one of several declaring bodies: every body, then the residual', () => {
const bundle = jsonLoaded([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), betaWith([pullJob('sync', 'beta_map')])]);
const bundle = handBuilt([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), betaWith([pullJob('sync', 'beta_map')])]);
expect(bundle.manifest.id).toBe('com.example.beta');
const of = jobDeclaringPackages(bundle, 'com.example.beta');
for (const job of bundle.jobs) {
Expand Down Expand Up @@ -1175,9 +1208,9 @@ describe('[#32412] the residual-twin job refusal names the residual as a holder
});

it('controls: the one holder (the residual\'s owner IS the one declaring body) and two bodies with no residual twin keep their answers', () => {
const owned = jsonLoaded([host([pullJob('sync', 'host_map')]), alpha([pullJob('sync', 'alpha_map')])]);
const owned = handBuilt([host([pullJob('sync', 'host_map')]), alpha([pullJob('sync', 'alpha_map')])]);
for (const job of owned.jobs) expect(jobDeclaringPackages(owned, 'com.example.alpha')(job)).toBe('com.example.alpha');
const twoBodies = jsonLoaded([alpha([pullJob('sync', 'alpha_map')]), betaWith([pullJob('sync', 'beta_map')])]);
const twoBodies = handBuilt([alpha([pullJob('sync', 'alpha_map')]), betaWith([pullJob('sync', 'beta_map')])]);
// No residual job: met by name, the #32071 refusal names the two bodies and nothing else.
expect((jobDeclaringPackages(twoBodies, 'com.example.beta')(pullJob('sync', 'alpha_map')) as { candidates?: unknown }).candidates)
.toEqual([ALPHA, { kind: 'package', packageId: 'com.example.beta' }]);
Expand All @@ -1200,9 +1233,9 @@ describe('[#32572] collectJobNameCollisions answers the refusals the binder refu

const ARRANGEMENTS = [
['a package body and the residual (the residual runs as no declaring body)',
() => ({ bundle: jsonLoaded([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]), appId: 'com.example.beta' })],
() => ({ bundle: handBuilt([alpha([pullJob('sync', 'alpha_map')]), host([pullJob('sync', 'host_map')]), beta]), appId: 'com.example.beta' })],
['a package body and the residual (the residual runs as that body)',
() => ({ bundle: jsonLoaded([host([pullJob('sync', 'host_map')]), alpha([pullJob('sync', 'alpha_map')])]), appId: 'com.example.alpha' })],
() => ({ bundle: handBuilt([host([pullJob('sync', 'host_map')]), alpha([pullJob('sync', 'alpha_map')])]), appId: 'com.example.alpha' })],
['a single-package artifact listing two names twice',
() => ({ bundle: { id: APP_ID, version: '0.1.0', type: 'app', mappings: [MAPPING], jobs: [
{ ...PULL_JOB, name: 'twice' }, { ...PULL_JOB, name: 'again' }, { ...PULL_JOB, name: 'twice' }, { ...PULL_JOB, name: 'again' },
Expand Down
21 changes: 14 additions & 7 deletions packages/runtime/src/app-artifact-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,8 @@ export function judgeJobPull(
* that name than its bodies declare — the flattened top level copies each
* body job once, so every job of the name beyond those copies is a top-level
* job of its own (`composeStacks(…, { manifest: 'preserve' })` with an input
* that has no `manifest`). By count, never by comparing a copy's content with
* that has no `manifest`, which composes such a pair only when one of the two
* is disabled; an enabled pair it refuses). By count, never by comparing a copy's content with
* its body's: a conversion replays a body under its own version window, so a
* genuine copy need not stay equal to the body it was copied from. Such a name
* answers the one holder when the body and the residual's owner are one
Expand Down Expand Up @@ -1390,12 +1391,18 @@ function describeJobRunForm(job: unknown): string {
* with nothing said. Nothing picks one of them, so the name is refused: none of its jobs is scheduled.
*
* The holders the refusal names are every body declaring the name, and — when the artifact holds more jobs
* of the name than its bodies declare (the residual's own, `composeStacks(…, { manifest: 'preserve' })` with
* an input that has no `manifest`) — the artifact's top level outside every package body, as
* {@link jobNameRefusal} names it; an artifact without `packages[]` is its app's alone. The refusal's
* `candidates` are every declaring body as a `package`, then the residual as `{ kind: 'residual', ownerId }`
* (the artifact's id), or the app itself for an artifact without `packages[]`. Each job is named by
* its run form. Counted by name, never by content: a conversion replays a body under its own version window.
* of the name than its bodies declare (the residual's own: a top-level job of an input with no `manifest`) —
* the artifact's top level outside every package body, as {@link jobNameRefusal} names it; an artifact
* without `packages[]` is its app's alone. The refusal's `candidates` are every declaring body as a
* `package`, then the residual as `{ kind: 'residual', ownerId }` (the artifact's id), or the app itself for
* an artifact without `packages[]`. Each job is named by its run form. Counted by name, never by content: a
* conversion replays a body under its own version window.
*
* `composeStacks` refuses the same names by the same reading (enabled jobs only, by name, each job counted
* per place it is reached — one job object two bodies carry is two jobs here — and the residual by count
* beyond the bodies' copies), so a composition does not build an artifact this refuses. This stays the
* backstop for an artifact no composition built: hand-written, or built before composition judged a shared
* job name.
*
* A disabled job is scheduled by nothing, so it collides with nothing and is not counted: its enabled twin
* keeps the run-time attribution refusal {@link jobDeclaringPackages} gives it.
Expand Down
Loading
Loading