Skip to content
Open
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
2 changes: 1 addition & 1 deletion atomic-integration.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
schema = 1
agent = "opencode"
version = "1.1.1"
version = "1.2.0"

[requires]
atomic = ">=0.11.0"
Expand Down
30 changes: 28 additions & 2 deletions plugins/atomic-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ const createTracker = (directory, invoke) => {
reset(state);
sessions.delete(sid);
}
// Read-only lookup: never materialize state just to ask. A session the
// plugin has started has a session ledger on disk, which is the CLI's
// precondition for accepting a turn-bound work-start declaration.
function isStarted(sid) {
const state = sessions.get(sid);
return !!(state && state.started);
}
return {
stateFor,
hook,
Expand All @@ -193,6 +200,7 @@ const createTracker = (directory, invoke) => {
beginTurn,
stopTurn,
endSession,
isStarted,
};
};

Expand Down Expand Up @@ -325,9 +333,20 @@ export const AtomicHooksPlugin = async ({ directory, $ }) => {
state.tools.delete(input.callID);
});
},
"shell.env": async (_input, output) => {
"shell.env": async (input, output) => {
output.env.ATOMIC_AGENT = "opencode";
output.env.ATOMIC_AGENT_VERSION = ATOMIC_AGENT_VERSION;
// Supply session context so a work-start declaration
// (`atomic intent update <id> --status in_progress`) binds the intent
// to this turn: the turn's recording then carries the intent UID in
// plan_id, which is how the intent graph attributes the turn's changes.
// Without the variable the CLI stays a status-only update. Only
// sessions this plugin started get it — the CLI rejects a declaration
// for an unknown session, and a failed binding fails the whole update.
// Terminal shells (no sessionID in the event) keep the human path.
const sid = input?.sessionID;
if (typeof sid === "string" && sid && t.isStarted(sid))
output.env.ATOMIC_SESSION_ID = sid;
},
};
};
Expand Down Expand Up @@ -365,7 +384,7 @@ const setupV2 = async (ctx, invoke) => {
directory,
invoke ?? ((json, verb) => spawnHook(directory, json, verb)),
);
const { enqueue, stopTurn } = t;
const { enqueue, stopTurn, isStarted } = t;
// The public event stream may not be scoped to this plugin instance's
// location, so only track sessions that are known to belong to it.
const known = new Set();
Expand Down Expand Up @@ -458,6 +477,13 @@ const setupV2 = async (ctx, invoke) => {
await ctx.shell.hook("create.before", (event) => {
event.env.ATOMIC_AGENT = "opencode";
event.env.ATOMIC_AGENT_VERSION = ATOMIC_AGENT_VERSION;
// Same session-context contract as the v1 `shell.env` hook: a started
// session's shells carry ATOMIC_SESSION_ID so `atomic intent update
// --status in_progress` binds the intent to the active turn instead of
// degrading to a status-only update.
const sid = event?.sessionID;
if (typeof sid === "string" && sid && isStarted(sid))
event.env.ATOMIC_SESSION_ID = sid;
});
} catch (error) {
try {
Expand Down
56 changes: 56 additions & 0 deletions tests/session-isolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,60 @@ describe("OpenCode v2 plugin", () => {
await until(() => f.calls.some((c) => c.verb === "session-end"));
await f.cleanup?.();
});

test("started sessions expose ATOMIC_SESSION_ID to shells", async () => {
const f = await fixtureV2();
await f.prompt("bind", "ses_bind");
const env: any = {};
f.hooks["shell.create.before"]({
command: "atomic intent update DEMO::you::1 --status in_progress",
cwd: f.root,
sessionID: "ses_bind",
env,
});
expect(env.ATOMIC_SESSION_ID).toBe("ses_bind");
const foreign: any = {};
f.hooks["shell.create.before"]({
command: "ls",
cwd: f.root,
sessionID: "ses_elsewhere",
env: foreign,
});
expect(foreign.ATOMIC_SESSION_ID).toBeUndefined();
expect(foreign.ATOMIC_AGENT).toBe("opencode");
await f.cleanup?.();
});
});

describe("work-start binding context", () => {
test("v1 started sessions expose ATOMIC_SESSION_ID to shells", async () => {
const f = await fixture();
await f.prompt("bind");
const env: any = {};
await f.plugin["shell.env"](
{ cwd: f.root, sessionID: "bind" },
{ env },
);
expect(env).toMatchObject({
ATOMIC_AGENT: "opencode",
ATOMIC_SESSION_ID: "bind",
});
});

test("v1 unstarted sessions and terminal shells get no session id", async () => {
const f = await fixture();
const unknown: any = {};
await f.plugin["shell.env"](
{ cwd: f.root, sessionID: "never-started" },
{ env: unknown },
);
expect(unknown.ATOMIC_AGENT).toBe("opencode");
expect(unknown.ATOMIC_SESSION_ID).toBeUndefined();
const terminal: any = {};
await f.plugin["shell.env"]({ cwd: f.root }, { env: terminal });
expect(terminal.ATOMIC_AGENT).toBe("opencode");
expect(terminal.ATOMIC_SESSION_ID).toBeUndefined();
// The read-only lookup must not materialize state for an unknown session.
expect(f.calls.filter((c) => c.payload.session_id === "never-started")).toHaveLength(0);
});
});