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/assistant-lifecycle-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sapiom/harness": patch
---

Add authenticated Assistant inspection, Resume and Continue action routes with public provenance.
7 changes: 7 additions & 0 deletions packages/harness/docs/assistant-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@ seed before permitting Attach. Retries consult the original allocation identity
and receipt before reading a newer source record. Completed retries only verify
saved preparation and preserve an already-running child's lease and execution.
End and authority changes fence each asynchronous step and final attachment.

Boot-authorized POST actions at `/api/sessions/:id/assistant/inspect`, `/resume`
and `/continue` accept exact lifecycle revisions. Resume and Continue also require
an operation UUID; Continue includes the displayed record revision. Responses
project public session/lifecycle/provenance fields only. Continue retry storage
uses an opaque server-projected scope stable across restarts and separated by
authorized account, workspace, project, harness and native binding.
46 changes: 46 additions & 0 deletions packages/harness/src/core/assistant-history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,52 @@ it("discovers distinct same-folder Assistant identities with no Terminal vendor
expect(await history.list(other)).toEqual([]);
});

it("provides stable opaque Continue retry scope only for an authorized project identity", async () => {
const session = sessions[0]!;
expect((await history.entry(session.id))?.continuationScope).toBeUndefined();
session.agentMapIdentity = {
sessionId: session.id,
projectId: "project-a",
userId: "user-a",
};
const before = (await history.entry(session.id))!.continuationScope!;
expect(before).toMatch(/^[a-f\d]{64}$/);
expect((await history.entry(session.id))?.continuationScope).toBe(before);
for (const update of [
() => Object.assign(session.agentMapIdentity!, { projectId: "project-b" }),
() => Object.assign(session.agentMapIdentity!, { userId: "user-b" }),
() => Object.assign(session, { harness: "codex" }),
() =>
Object.assign(bindings.get(session.id)!, { conversationId: "ses_new" }),
() =>
Object.assign(bindings.get(session.id)!, {
contextAuthorityScope: "9".repeat(64),
}),
]) {
const previous = (await history.entry(session.id))!.continuationScope;
update();
expect((await history.entry(session.id))!.continuationScope).not.toBe(
previous,
);
}
});

it("rejects a project rebind during retained history IO", async () => {
const session = sessions[0]!;
session.agentMapIdentity = {
sessionId: session.id,
projectId: "project-a",
userId: "user-a",
};
const read = records.read.bind(records);
vi.spyOn(records, "read").mockImplementationOnce(async (binding) => {
const value = await read(binding);
Object.assign(session.agentMapIdentity!, { projectId: "project-b" });
return value;
});
await expect(history.entry(session.id)).rejects.toThrow(OpenCodeAccessError);
});

it("distinguishes missing, partial and failed retained records", async () => {
const first = bindings.get("studio-a")!;
await records.write({
Expand Down
28 changes: 24 additions & 4 deletions packages/harness/src/core/assistant-history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { realpath } from "node:fs/promises";
import { createHash } from "node:crypto";
import { isAbsolute } from "node:path";
import type { AssistantHistoryEntry } from "../shared/assistant-history.js";
import type { HarnessSession } from "../shared/types.js";
Expand All @@ -25,14 +26,13 @@ export class AssistantHistory {
return (await this.read(id))?.entry ?? null;
}

private async read(
id: string,
): Promise<{
private async read(id: string): Promise<{
entry: AssistantHistoryEntry;
binding: AssistantAssociation;
} | null> {
const session = this.options.sessions.get(id);
if (!session) return null;
const project = JSON.stringify([session.harness, session.agentMapIdentity]);
const binding = await this.options.authorize(id);
if (!binding) return null;
const lifecycle = await this.options.lifecycle.describe(id);
Expand Down Expand Up @@ -65,7 +65,11 @@ export class AssistantHistory {
)
throw new OpenCodeAccessError("Assistant binding changed");
const current = this.options.sessions.get(id);
if (!current || current.cwd !== session.cwd)
if (
!current ||
current.cwd !== session.cwd ||
JSON.stringify([current.harness, current.agentMapIdentity]) !== project
)
throw new OpenCodeAccessError("Workspace changed");
return {
binding,
Expand All @@ -80,6 +84,22 @@ export class AssistantHistory {
history,
nativeResume: "unchecked",
recordRevision,
...(current.agentMapIdentity
? {
continuationScope: createHash("sha256")
.update(
JSON.stringify([
"studio-assistant-continue-retry/v1",
binding.contextAuthorityScope,
binding.conversationId,
current.agentMapIdentity.projectId,
current.agentMapIdentity.userId,
current.harness,
]),
)
.digest("hex"),
}
: {}),
},
};
}
Expand Down
Loading
Loading