Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ v1 receipt 严格包含:

- durable binding 与调用 identity 完全相同;
- repository record、epoch artifact、baseline ref、head ref、commit/tree 与 pinned Git capability 一致;
- reopening 已存在 binding 时重新观测 source repository identity、HEAD commit 与 tree;任一项相对已接受边界发生漂移都 fail closed,不得静默复用旧 baseline;
- worktree registration 仍被 Maka ownership lock 锁定;
- HEAD/tree 等于 baseline,worktree clean;
- policy version/hash 等于实现实际执行的 canonical M0 policy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ function fakeManagedOwner(input: {
async openManagedWorkspaceBaseline() {
throw new Error('not used');
},
async openManagedWorkspaceBaselineFromExecutionStores() {
throw new Error('not used');
},
async withManagedWorkspaceExecution(handle, operation) {
assert.equal(handle, input.handle);
input.calls.push('managed:admit');
Expand Down
27 changes: 27 additions & 0 deletions packages/storage/src/__tests__/artifact-writer-lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ test('public Store mutation waits for a child-held writer lock and preserves met
});
});

test('an aborted artifact writer admission stops waiting for a child-held lock', {
timeout: TEST_TIMEOUT_MS,
}, async () => {
await withTemporaryDirectory(async (root) => {
const stateRoot = join(root, 'state');
const holder = await spawnLockHolder(stateRoot);
const abort = new AbortController();
let entered = false;
try {
const admission = withArtifactWriterLock(
stateRoot,
async () => {
entered = true;
},
abort.signal,
);
await assertPending(admission, 'abortable artifact writer admission');
abort.abort(new DOMException('Baseline admission cancelled', 'AbortError'));

await assert.rejects(admission, { name: 'AbortError' });
assert.equal(entered, false);
} finally {
await stopHolder(holder);
}
});
});

test('public Store mutations in separate processes reload and publish under one OS lock', {
timeout: TEST_TIMEOUT_MS,
}, async () => {
Expand Down
11 changes: 5 additions & 6 deletions packages/storage/src/__tests__/git-workspace-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Git workspace service', () => {
assert.equal((await restarted.inspectManagedWorkspace(adopted)).state, 'ready');
});

test('reopens an existing managed epoch after the source checkout advances', async () => {
test('rejects reopening an existing managed epoch after the source checkout advances', async () => {
const root = await temporaryRoot();
const sourceRoot = await createEligibleSource(join(root, 'source'));
const storageRoot = join(root, 'storage');
Expand All @@ -199,11 +199,10 @@ describe('Git workspace service', () => {
);

const restarted = await serviceAt(storageRoot);
const reopened = await restarted.openManagedWorkspaceFromBinding(created);

assert.deepEqual(reopened, created);
assert.equal((await restarted.inspectManagedWorkspace(reopened)).state, 'ready');
assert.equal(await readFile(join(reopened.worktreePath, 'tracked.txt'), 'utf8'), 'tracked\n');
await assert.rejects(
restarted.openManagedWorkspaceFromBinding(created),
isWorkspaceError('managed_workspace_source_drifted'),
);
});

test('creates a second epoch with a different baseline in the same managed repository', async () => {
Expand Down
153 changes: 153 additions & 0 deletions packages/storage/src/__tests__/managed-dependency-environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,37 @@ test('rejects a second authority for the same storage root in one process', asyn
await first.close();
});

test('rejects a pre-aborted acquisition before invoking the producer', async (t) => {
const storageRoot = await mkdtemp(join(tmpdir(), 'maka-dependency-pre-abort-'));
t.after(() => rm(storageRoot, { recursive: true, force: true }));
let provisionCalls = 0;
const authority = await createManagedDependencyEnvironmentAuthority({
storageRoot,
producer: {
capability: FIXTURE_PRODUCER_CAPABILITY,
packageManagerName: 'npm',
packageManagerVersion: '11.12.1',
nodeRuntime: fixtureNodeRuntime(),
async provision() {
provisionCalls += 1;
},
},
});
const source = dependencySourceForName('pre-abort');
const abort = new AbortController();
abort.abort(new DOMException('Cancelled before acquisition', 'AbortError'));

await assert.rejects(
authority.acquire(computeManagedDependencyEnvironmentIdentity(source), {
...source,
abortSignal: abort.signal,
}),
{ name: 'AbortError' },
);
assert.equal(provisionCalls, 0);
await authority.close();
});

test('rejects a published environment whose dependency content was modified', async (t) => {
const storageRoot = await mkdtemp(join(tmpdir(), 'maka-dependency-tamper-'));
t.after(() => rm(storageRoot, { recursive: true, force: true }));
Expand Down Expand Up @@ -595,6 +626,128 @@ test('publishes one Maka-owned artifact for concurrent equivalent acquisitions',
await authority.close();
});

test('does not cancel shared provisioning while another acquisition still needs it', async (t) => {
const storageRoot = await mkdtemp(join(tmpdir(), 'maka-dependency-shared-abort-'));
t.after(() => rm(storageRoot, { recursive: true, force: true }));
let acknowledgeProvision!: () => void;
const provisionStarted = new Promise<void>((resolve) => {
acknowledgeProvision = resolve;
});
let finishProvision!: () => void;
const provisionAllowed = new Promise<void>((resolve) => {
finishProvision = resolve;
});
let producerSignal: AbortSignal | undefined;
const authority = await createManagedDependencyEnvironmentAuthority({
storageRoot,
producer: {
capability: FIXTURE_PRODUCER_CAPABILITY,
packageManagerName: 'npm',
packageManagerVersion: '11.12.1',
nodeRuntime: fixtureNodeRuntime(),
async provision(input) {
producerSignal = input.abortSignal;
acknowledgeProvision();
await Promise.race([
provisionAllowed,
new Promise<never>((_resolve, reject) => {
const abort = () =>
reject(
input.abortSignal?.reason ??
new DOMException('Shared provision cancelled', 'AbortError'),
);
if (input.abortSignal?.aborted) abort();
else input.abortSignal?.addEventListener('abort', abort, { once: true });
}),
]);
input.abortSignal?.throwIfAborted();
await writeFile(join(input.outputRoot, 'payload'), 'shared\n', 'utf8');
},
},
});
const source = dependencySourceForName('shared-abort');
const identity = computeManagedDependencyEnvironmentIdentity(source);
const firstAbort = new AbortController();
const first = authority.acquire(identity, { ...source, abortSignal: firstAbort.signal });
await provisionStarted;
const second = authority.acquire(identity, source);

firstAbort.abort(new DOMException('First waiter cancelled', 'AbortError'));

await assert.rejects(first, { name: 'AbortError' });
assert.equal(producerSignal?.aborted, false);
finishProvision();
const lease = await second;
assert.equal(await readFile(join(lease.dependencyRoot, 'payload'), 'utf8'), 'shared\n');
await lease.release();
await authority.close();
});

test('starts a fresh publication when a new caller arrives during aborted publication cleanup', async (t) => {
const storageRoot = await mkdtemp(join(tmpdir(), 'maka-dependency-abort-handoff-'));
t.after(() => rm(storageRoot, { recursive: true, force: true }));
let provisionCalls = 0;
let acknowledgeFirstStart!: () => void;
const firstStarted = new Promise<void>((resolve) => {
acknowledgeFirstStart = resolve;
});
let acknowledgeFirstAbort!: () => void;
const firstAborted = new Promise<void>((resolve) => {
acknowledgeFirstAbort = resolve;
});
let finishFirstCleanup!: () => void;
const firstCleanupAllowed = new Promise<void>((resolve) => {
finishFirstCleanup = resolve;
});
const authority = await createManagedDependencyEnvironmentAuthority({
storageRoot,
producer: {
capability: FIXTURE_PRODUCER_CAPABILITY,
packageManagerName: 'npm',
packageManagerVersion: '11.12.1',
nodeRuntime: fixtureNodeRuntime(),
async provision(input) {
provisionCalls += 1;
if (provisionCalls === 1) {
acknowledgeFirstStart();
await new Promise<void>((resolve) => {
const onAbort = () => {
acknowledgeFirstAbort();
resolve();
};
if (input.abortSignal?.aborted) onAbort();
else input.abortSignal?.addEventListener('abort', onAbort, { once: true });
});
await firstCleanupAllowed;
input.abortSignal?.throwIfAborted();
}
await writeFile(join(input.outputRoot, 'payload'), 'fresh\n', 'utf8');
},
},
});
t.after(() => authority.close().catch(() => undefined));
const source = dependencySourceForName('abort-handoff');
const identity = computeManagedDependencyEnvironmentIdentity(source);
const firstAbort = new AbortController();
const first = authority.acquire(identity, { ...source, abortSignal: firstAbort.signal });
await firstStarted;

firstAbort.abort(new DOMException('First caller cancelled', 'AbortError'));
await assert.rejects(first, { name: 'AbortError' });
await firstAborted;

const second = authority.acquire(identity, source);
await Promise.resolve();
assert.equal(provisionCalls, 1, 'fresh publication started before aborted cleanup settled');
finishFirstCleanup();

const lease = await second;
assert.equal(provisionCalls, 2);
assert.equal(await readFile(join(lease.dependencyRoot, 'payload'), 'utf8'), 'fresh\n');
await lease.release();
await authority.close();
});

test('close drains an acquisition through lease installation before deciding its outcome', async (t) => {
const storageRoot = await mkdtemp(join(tmpdir(), 'maka-dependency-close-drain-'));
t.after(() => rm(storageRoot, { recursive: true, force: true }));
Expand Down
44 changes: 44 additions & 0 deletions packages/storage/src/__tests__/managed-workspace-baseline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,50 @@ test('reissues execution authority after a real process crash during admission v
}
});

test('rejects reopening an accepted baseline after the source HEAD advances', async () => {
const root = await temporaryRoot();
const storageRoot = join(root, 'storage');
const sourceRoot = await createEligibleSource(join(root, 'source'));
const capability = await resolveStorageRoot({ path: storageRoot, kind: 'interactive' });
const rootOwner = await tryAcquireInteractiveRootOwner(capability);
assert.ok(rootOwner);
const runtimeStore = createSqliteRuntimeStore(join(storageRoot, 'runtime.sqlite'));
try {
const owner = await openManagedWorkspaceOwner({
rootOwner,
gitRuntime: {
executablePath: gitExecutablePath,
expectedSha256: gitExecutableSha256,
},
});
const request = openRequest(sourceRoot);
await owner.openManagedWorkspaceBaseline(runtimeStore, request);

await writeFile(join(sourceRoot, 'tracked.txt'), 'advanced\n', 'utf8');
await git(sourceRoot, 'add', 'tracked.txt');
await git(
sourceRoot,
'-c',
'user.name=Maka Test',
'-c',
'user.email=test@maka.invalid',
'commit',
'--quiet',
'-m',
'advance source',
);

await assert.rejects(
owner.openManagedWorkspaceBaseline(runtimeStore, request),
/source no longer matches its accepted Git boundary/u,
);
await owner.close();
} finally {
runtimeStore.close();
await rootOwner.close();
}
});

test('rejects a source tree containing a non-UTF-8 Git path', {
skip: process.platform === 'win32',
}, async () => {
Expand Down
Loading