refactor: keep runtime context in memory

This commit is contained in:
2026-06-07 17:07:14 +08:00
parent 1ed7e56f35
commit 9d4e5486e9
11 changed files with 273 additions and 269 deletions
+6 -8
View File
@@ -4,13 +4,13 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { createSkillManagerTool } from "../../.opencode/tools/skill_manager.js";
import { SessionRuntimeContextStore } from "../../src/sessions/runtimeContextStore.js";
import { type RuntimeSessionContext } from "../../src/runtime/sessionContext.js";
import { SkillStore } from "../../src/skills/store.js";
describe("skill_manager tool", () => {
let tempDir: string;
let skillStore: SkillStore;
let contextStore: SessionRuntimeContextStore;
let context: RuntimeSessionContext;
const toolContext = {
abort: new AbortController().signal,
@@ -39,16 +39,14 @@ describe("skill_manager tool", () => {
join(tempDir, "skills"),
join(tempDir, "backup", "skills"),
);
contextStore = new SessionRuntimeContextStore(join(tempDir, "contexts"));
await contextStore.initialize();
await contextStore.write({
context = {
actorKey: "actor-1",
allowLearningWrite: true,
clientSessionId: "client-session-1",
projectKey: "project-1",
sessionId: "session-1",
traceId: "trace-1",
});
};
});
afterEach(async () => {
@@ -58,7 +56,7 @@ describe("skill_manager tool", () => {
it("dispatches skill-level write, overwrite, and remove actions", async () => {
const tool = createSkillManagerTool(
skillStore,
contextStore,
{ read: () => context },
Promise.resolve(),
);
@@ -111,7 +109,7 @@ describe("skill_manager tool", () => {
it("writes the root skills index through the reserved alias", async () => {
const tool = createSkillManagerTool(
skillStore,
contextStore,
{ read: () => context },
Promise.resolve(),
);
+32
View File
@@ -0,0 +1,32 @@
import { describe, expect, it } from "bun:test";
import {
getRuntimeSessionContext,
removeRuntimeSessionContext,
setRuntimeSessionContext,
} from "../../src/runtime/sessionContext.js";
describe("runtime session context", () => {
it("stores authentication context in process memory", () => {
setRuntimeSessionContext({
accessToken: "token-1",
actorKey: "actor-1",
allowLearningWrite: true,
clientSessionId: "chat-session-1",
learningMode: "interactive",
projectId: "project-id-1",
projectKey: "project-1",
sessionId: "runtime-session-1",
traceId: "trace-1",
});
const runtimeContext = getRuntimeSessionContext("runtime-session-1");
expect(runtimeContext?.accessToken).toBe("token-1");
expect(runtimeContext?.clientSessionId).toBe("chat-session-1");
expect(runtimeContext?.sessionId).toBe("runtime-session-1");
removeRuntimeSessionContext("runtime-session-1");
expect(getRuntimeSessionContext("runtime-session-1")).toBeNull();
});
});
@@ -1,41 +0,0 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { SessionRuntimeContextStore } from "../../src/sessions/runtimeContextStore.js";
describe("SessionRuntimeContextStore", () => {
let tempDir: string;
let store: SessionRuntimeContextStore;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "tjwater-tool-context-"));
store = new SessionRuntimeContextStore(tempDir);
await store.initialize();
});
afterEach(async () => {
await rm(tempDir, { force: true, recursive: true });
});
it("writes and reads runtime session context by opencode session id", async () => {
await store.write({
accessToken: "token-1",
actorKey: "actor-1",
allowLearningWrite: true,
clientSessionId: "chat-session-1",
learningMode: "interactive",
projectId: "project-id-1",
projectKey: "project-1",
sessionId: "runtime-session-1",
traceId: "trace-1",
});
const runtimeContext = await store.read("runtime-session-1");
expect(runtimeContext?.accessToken).toBe("token-1");
expect(runtimeContext?.clientSessionId).toBe("chat-session-1");
expect(runtimeContext?.sessionId).toBe("runtime-session-1");
});
});