Repository navigation
fix: trace and dispose continuity context hook #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { mkdtemp, writeFile, chmod, rm } from "node:fs/promises" | ||
| import { tmpdir } from "node:os" | ||
| import { join } from "node:path" | ||
| import plugin from "../plugin/index.js" | ||
|
|
||
| test("fetches continuity once, applies it to every model request, and disposes the hook", async () => { | ||
| const directory = await mkdtemp(join(tmpdir(), "musubi-opencode-plugin-")) | ||
| const bridge = join(directory, "bridge") | ||
| const calls = join(directory, "calls") | ||
| await writeFile(bridge, '#!/usr/bin/env node\nconst fs = require("node:fs"); process.stdin.resume(); process.stdin.on("end", () => { fs.appendFileSync(process.env.MUSUBI_TEST_BRIDGE_CALLS, "x"); process.stdout.write(JSON.stringify({ok:true,text:process.env.MUSUBI_TEST_BRIDGE_TEXT ?? "## Musubi continuity\\nRecent memory available."})) })\n') | ||
| await chmod(bridge, 0o755) | ||
| const old = Object.fromEntries(["MUSUBI_ACTOR", "MUSUBI_PRESENCE", "MUSUBI_ZONE", "MUSUBI_OPENCODE_BRIDGE_BIN", "MUSUBI_TEST_BRIDGE_CALLS", "MUSUBI_TEST_BRIDGE_TEXT"].map((key) => [key, process.env[key]])) | ||
| Object.assign(process.env, { | ||
| MUSUBI_ACTOR: "iris", | ||
| MUSUBI_PRESENCE: "iris/agent", | ||
| MUSUBI_ZONE: "home", | ||
| MUSUBI_OPENCODE_BRIDGE_BIN: bridge, | ||
| MUSUBI_TEST_BRIDGE_CALLS: calls, | ||
| }) | ||
| let contextHook | ||
| let disposed = false | ||
| const context = { | ||
| mcp: { transform: async () => {} }, | ||
| session: { | ||
| hook: async (name, callback) => { | ||
| expect(name).toBe("context") | ||
| contextHook = callback | ||
| return { dispose: async () => { disposed = true } } | ||
| }, | ||
| get: async () => ({ id: "ses_primary" }), | ||
| }, | ||
| event: { | ||
| async *subscribe({ signal }) { | ||
| await new Promise((resolve) => signal.addEventListener("abort", resolve, { once: true })) | ||
| }, | ||
| }, | ||
| } | ||
| try { | ||
| const cleanup = await plugin.setup(context) | ||
| const request = { sessionID: "ses_primary", system: [] } | ||
| await contextHook(request) | ||
| expect(request.system).toEqual([{ type: "text", text: "## Musubi continuity\nRecent memory available." }]) | ||
| await contextHook(request) | ||
| expect(request.system).toHaveLength(1) | ||
| const laterRequest = { sessionID: "ses_primary", system: [] } | ||
| await contextHook(laterRequest) | ||
| expect(laterRequest.system).toEqual([{ type: "text", text: "## Musubi continuity\nRecent memory available." }]) | ||
| expect(await Bun.file(calls).text()).toBe("x") | ||
| process.env.MUSUBI_TEST_BRIDGE_TEXT = " \n " | ||
| const emptyRequest = { sessionID: "ses_empty", system: [] } | ||
| await contextHook(emptyRequest) | ||
| expect(emptyRequest.system).toHaveLength(0) | ||
| process.env.MUSUBI_TEST_BRIDGE_TEXT = "## Musubi continuity\nRecovered memory." | ||
| await contextHook(emptyRequest) | ||
| expect(emptyRequest.system).toEqual([{ type: "text", text: "## Musubi continuity\nRecovered memory." }]) | ||
| expect(await Bun.file(calls).text()).toBe("xxx") | ||
| await cleanup() | ||
| expect(disposed).toBe(true) | ||
| } finally { | ||
| for (const [key, value] of Object.entries(old)) { | ||
| if (value === undefined) delete process.env[key] | ||
| else process.env[key] = value | ||
| } | ||
| await rm(directory, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.