Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/paused-continuation-admission.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sapiom/harness": patch
---

Reserve exclusive paused Assistant continuation preparation and reject retries that lose committed accepted material.
8 changes: 8 additions & 0 deletions packages/harness/docs/assistant-continuation.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ separate revision lock. A private child lookup is durable before allocation;
ordinary attachment is blocked until the linked receipt is verified prepared.
Missing or corrupt linked receipts fail closed. An existing receipt is read
before newer source history, so retries use the same frozen record and child.

Trusted continuation preparation shares the lifecycle admission slot with
inspection, Attach and Resume. It requires an open matching revision, retains
its exact host only after successful checks, and grants no runtime lease.
End preempts preparation; ordinary attachment follows verified preparation.
If a previous acceptance already committed, a retry validates its original
manifest and material before any writes. Pending receipt input cannot repair
lost committed material, including after a lost acceptance acknowledgement.
71 changes: 71 additions & 0 deletions packages/harness/src/core/assistant-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,74 @@ it("pending Continue children cannot attach and End fences a late admission resu
expect(ensure).not.toHaveBeenCalled();
expect(associate).not.toHaveBeenCalled();
});

it("Continue exclusively prepares a gated child and keeps its exact runtime without a lease", async () => {
canAttach.mockResolvedValue(false);
const held = gate(), prepare = vi.fn(async () => { await held.promise; return "seeded"; });
const preparing = coordinator.prepareContinuation("studio-a", 0, prepare);
await vi.waitFor(() => expect(prepare).toHaveBeenCalledOnce());
const provisional = current;
await expect(coordinator.inspect("studio-a", 0, vi.fn())).rejects.toThrow();
await expect(coordinator.attach("studio-a", 0)).rejects.toThrow();
await expect(coordinator.prepareContinuation("studio-a", 0, vi.fn())).rejects.toThrow();
held.release();
expect(await preparing).toBe("seeded");
expect(canAttach).not.toHaveBeenCalled();
expect(current).toBe(provisional);
expect(await store.lifecycle("studio-a")).toBeNull();
expect(associate).not.toHaveBeenCalled();
expect(observe).not.toHaveBeenCalled();
expect(retire).not.toHaveBeenCalled();
await expect(coordinator.use("studio-a", "not-a-lease")).rejects.toThrow();
canAttach.mockResolvedValue(true);
const attached = await coordinator.attach("studio-a", 0);
expect(current).toBe(provisional);
expect(attached.lifecycle).toMatchObject({ revision: 1, execution: "paused" });
expect(observe).toHaveBeenCalledOnce();
});

it("failed Continue preparation retires only its provisional host and releases the slot", async () => {
await expect(coordinator.prepareContinuation("studio-a", 0, async () => {
throw new Error("seed unavailable");
})).rejects.toThrow("seed unavailable");
expect(current).toBeNull();
expect(retire).toHaveBeenCalledOnce();
expect(await coordinator.prepareContinuation("studio-a", 0, async () => "retried")).toBe("retried");
expect(current).not.toBeNull();
expect(await store.lifecycle("studio-a")).toBeNull();
});

it("Continue requires an open exact revision and discards a late result after durable change", async () => {
await store.transition("studio-a", 0, { lifecycle: "ended", execution: "paused" });
await expect(coordinator.prepareContinuation("studio-a", 1, vi.fn())).rejects.toThrow();
await expect(coordinator.prepareContinuation("studio-a", 0, vi.fn())).rejects.toThrow();
expect(ensure).not.toHaveBeenCalled();
await store.transition("studio-a", 1, { lifecycle: "open", execution: "paused" });
await expect(coordinator.prepareContinuation("studio-a", 2, async () => {
await store.transition("studio-a", 2, { lifecycle: "ending", execution: "paused" });
return "late";
})).rejects.toMatchObject({ failure: { code: "lifecycle_changed" } });
expect(current).toBeNull();
expect(observe).not.toHaveBeenCalled();
});

it("End preempts hung Continue preparation and its late result cannot retire a replacement", async () => {
const held = gate(), prepare = vi.fn(async () => { await held.promise; return "old"; });
const preparing = coordinator.prepareContinuation("studio-a", 0, prepare);
const rejected = expect(preparing).rejects.toMatchObject({ failure: { code: "lifecycle_changed" } });
await vi.waitFor(() => expect(prepare).toHaveBeenCalledOnce());
const old = current!;
const ending = coordinator.beginEnd("studio-a");
expect(old.signal.aborted).toBe(true);
await rejected;
await ending.persistence;
const ended = await coordinator.finishEnd(ending.fence);
const opened = await store.transition("studio-a", ended.revision, { lifecycle: "open", execution: "paused" });
await coordinator.prepareContinuation("studio-a", opened.revision, async () => "new");
const replacement = current!;
held.release();
await Promise.resolve();
expect(current).toBe(replacement);
expect(replacement.signal.aborted).toBe(false);
expect(observe).not.toHaveBeenCalled();
});
35 changes: 30 additions & 5 deletions packages/harness/src/core/assistant-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,35 @@ export class AssistantLifecycleCoordinator {
return { conversationId, lease, lifecycle: { ...lifecycle } };
}

/** Exclusive temporary ownership, shared with Attach and future Resume. Never
/** Exclusive temporary ownership, shared with Attach, Continue and Resume. Never
* creates an association or grants execution. Callback results stay private;
* the caller revalidates its saved binding after provisional cleanup. */
async inspect<T>(
inspect<T>(
id: string,
expectedRevision: number,
read: (hosted: HostedOpenCode, signal: AbortSignal) => Promise<T>,
callerSignal?: AbortSignal,
): Promise<T> {
return this.prepareRuntime(id, expectedRevision, read, false, callerSignal);
}

/** Trusted Continue preparation owns an open child before its receipt permits
* Attach. Keep the exact runtime on success, but grant no lease or execution. */
prepareContinuation<T>(
id: string,
expectedRevision: number,
prepare: (hosted: HostedOpenCode, signal: AbortSignal) => Promise<T>,
callerSignal?: AbortSignal,
): Promise<T> {
return this.prepareRuntime(id, expectedRevision, prepare, true, callerSignal);
Comment on lines +257 to +263

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Continuation fencing remains unwired

Production constructs the coordinator without canAttach, and only tests call prepareContinuation. Continue operations remain unchanged until the planned consumer wiring lands.

Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

}

private async prepareRuntime<T>(
id: string,
expectedRevision: number,
read: (hosted: HostedOpenCode, signal: AbortSignal) => Promise<T>,
retain: boolean,
callerSignal?: AbortSignal,
): Promise<T> {
if (
this.closed ||
Expand Down Expand Up @@ -276,7 +297,11 @@ export class AssistantLifecycleCoordinator {
check();
const state = await awaitAssistantInspection(this.describe(id), signal);
check();
if (state.revision !== expectedRevision || state.lifecycle === "ending")
if (
state.revision !== expectedRevision ||
state.lifecycle === "ending" ||
(retain && state.lifecycle !== "open")
)
Comment on lines +300 to +304

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Active leases survive continuation preparation

prepareContinuation accepts a session with an existing lease, so preparation can overlap live use of the same runtime. inspections blocks new attachment but neither rejects nor revokes the lease already stored in leases. Continuation seeding can race user dispatch and alter an active conversation.

Learn more

Continuation preparation uses the same admission slot as attachment and inspection, but the slot only prevents new operations from starting. An attachment completed before preparation leaves a lease in leases. Requests using that lease continue through use, which does not consult inspections. The callback can therefore mutate the native conversation while an already-admitted request uses it.

Example: A child is attached and receives a paused lease. Before or during enablement, a continuation retry calls prepareContinuation with the current revision. Preparation is admitted because the lifecycle is open, while the existing lease remains usable against the same host.

Recommended fix: Reject retained preparation when the session has a current lease. Permit an obsolete lease whose host is no longer current, since the retained path can safely allocate a replacement.

Suggested change
if (
state.revision !== expectedRevision ||
state.lifecycle === "ending" ||
(retain && state.lifecycle !== "open")
)
if (
state.revision !== expectedRevision ||
state.lifecycle === "ending" ||
(retain &&
(state.lifecycle !== "open" ||
this.leases.get(id)?.hosted.isCurrent() === true))
)
Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

throw failure("lifecycle_changed");
};
let hosted: HostedOpenCode | undefined;
Expand Down Expand Up @@ -322,7 +347,7 @@ export class AssistantLifecycleCoordinator {
return result;
} finally {
try {
if (provisional && started) {
if (provisional && started && !(complete && retain)) {
if (
this.inspections.get(id) === controller &&
this.generation(id) === fence.generation
Expand All @@ -334,7 +359,7 @@ export class AssistantLifecycleCoordinator {
void this.options.host.retireExact(hosted).catch(() => {});
}
}
if (complete) await revision();
if (complete && !retain) await revision();
} finally {
clearTimeout(timer);
if (this.inspections.get(id) === controller)
Expand Down
18 changes: 18 additions & 0 deletions packages/harness/src/core/assistant-source-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ afterEach(async () => {
await fs.rm(directory, { recursive: true, force: true });
});

it("does not repair a committed acceptance's missing material from retried pending input", async () => {
const fixture = await retain();
const source = fixture.accepted.instructionSet.sources.find(
(item) => item.status === "available",
)!;
if (source.status !== "available")
throw new Error("fixture source unavailable");
const objectPath = join(root(), "objects", source.contentHash);
await fs.rm(objectPath);
await expect(
retain(new FileAssistantSourceStore(directory, sourceScope), fixture),
).rejects.toBeInstanceOf(AssistantContextError);
await expect(fs.stat(objectPath)).rejects.toMatchObject({ code: "ENOENT" });
expect(JSON.parse(await fs.readFile(manifest(), "utf8"))).toEqual(
fixture.accepted,
);
});

describe("durable accepted Assistant sources", () => {
it("reads exact retained text after restart and detaches returned data", async () => {
const fixture = await retain();
Expand Down
17 changes: 17 additions & 0 deletions packages/harness/src/core/assistant-source-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@
);
signal.throwIfAborted();
const root = await this.prepare(true, signal);
const committed = await this.read(
root,
"accepted",
`${ref.acceptanceId}.json`,
signal,
).catch((error: unknown) => {
if (isCode(error, "ENOENT")) return null;
throw error;
});
if (committed !== null) {
// A lost acknowledgement may retry this ID. An existing acceptance
// remains authoritative: validate its original material before any
// writes, rather than repairing source loss from pending input.
check(committed.equals(Buffer.from(encodeAssistantContext(record))));
await this.readAccepted(ref, authority, signal);
return;
}
for (const source of record.instructionSet.sources) {
if (source.status !== "available") continue;
const material = copies.find((item) => item.sourceId === source.id)!;
Expand Down Expand Up @@ -184,7 +201,7 @@
return result;
} catch {
signal.throwIfAborted();
throw new AssistantContextError();

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects manifest tampering and directory substitutions

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:280:21

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects a symlink object on read and idempotent publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:263:23

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects a corrupt object on read and idempotent publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:263:23

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects a missing object on read and idempotent publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:263:23

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects foreign scopes, conversations and malformed file keys before IO

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:239:21

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > snapshots inputs before asynchronous publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:227:5

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > supports concurrent idempotent writers without replacing immutable targets

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:191:5

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > preserves complete package resources and executable facts across restart

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ src/core/assistant-source-store.test.ts:166:5

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > reads exact retained text after restart and detaches returned data

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:86:21

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-11-arm)

src/core/assistant-source-store.test.ts > does not repair a committed acceptance's missing material from retried pending input

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:67:19

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects manifest tampering and directory substitutions

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:280:21

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects a symlink object on read and idempotent publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:263:23

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects a corrupt object on read and idempotent publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:263:23

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects a missing object on read and idempotent publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:263:23

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > rejects foreign scopes, conversations and malformed file keys before IO

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:239:21

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > snapshots inputs before asynchronous publication

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:227:5

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > supports concurrent idempotent writers without replacing immutable targets

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:191:5

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > preserves complete package resources and executable facts across restart

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ src/core/assistant-source-store.test.ts:166:5

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > durable accepted Assistant sources > reads exact retained text after restart and detaches returned data

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:86:21

Check failure on line 204 in packages/harness/src/core/assistant-source-store.ts

View workflow job for this annotation

GitHub Actions / platform (windows-latest)

src/core/assistant-source-store.test.ts > does not repair a committed acceptance's missing material from retried pending input

StudioAssistantContextError: Studio assistant context could not be verified ❯ FileAssistantSourceStore.safe src/core/assistant-source-store.ts:204:13 ❯ retain src/core/assistant-source-store.test.ts:45:3 ❯ src/core/assistant-source-store.test.ts:67:19
}
}

Expand Down
Loading