CodeNomad/scripts/test-session-navigation-native.mjs
Pascal André f747d47f41
Some checks are pending
Download Caches / seed (windows-2025) (push) Waiting to run
Download Caches / seed (ubuntu-24.04) (push) Waiting to run
Download Caches / seed (macos-15-intel) (push) Waiting to run
Download Caches / seed (macos-26) (push) Waiting to run
Publish help / build (push) Waiting to run
Publish help / deploy (push) Blocked by required conditions
fix(opencode): prepare cold Location RPCs before one-shot dispatch (#1448)
## Problem

OpenCode 2.2.7 intentionally removes `Plugin.awaitActivation` from
`rpc.call`. On unchanged CodeNomad, native cold history/pruning returns
`rpc.unavailable`, `/goal` incorrectly reports unsupported, and a
headless integration host can fail its first advertisement. The same
native checks pass on 2.2.6.

## Adaptation

- Adopt the existing bounded, connection-owned pruning readiness
foundation and its eight fixed broker families. Preparation targets the
already-authorized exact native Location and requires a fresh
lease/registration-scope acknowledgement, rather than treating a
backend-wide startup marker as universal readiness.
- Preserve session/workspace/connection, deletion/ABA, cancellation and
extension-grant fences around waits and the single functional dispatch.
No pruning mutation is probed, replayed or retried; generic RPC proxy
access stays closed.
- Share the native catalogue activation wait with `/goal`. Only method
absence after activation proves unsupported; transient registration loss
remains unavailable.
- Preserve current Memory provisioning and ownership options while
adopting the foundation. Abort the startup marker observer on close so
cleanup does not wait on an unreferenced polling timer.
- Correct the reference host discovery client, vendored byte-for-byte
from [opencode-integrations
#17](https://github.com/NeuralNomadsAI/opencode-integrations/pull/17):
payload-free status preparation may repeat while registration is
unavailable; advertise/withdraw are sent once, with no directory
fallback after dispatch. The external PR publishes source only;
installed hosts need the corresponding host update.
- Keep minimum, recommendation and authoring dependency pins unchanged.
Add cold fixed-broker native coverage to minimum/latest and
Windows/Linux/macOS native lanes.

## Validation

- **199** affected broker/lifecycle/readiness/goal tests pass; the final
cancellation cleanup additionally passes **40** focused tests.
- **30** real-browser Images/panel-extension journeys pass.
- Official isolated **2.2.3, 2.2.6 and 2.2.7**, driven by pinned **Node
24.22.2**: full pruning/history/Images/replay fixture, production goal
route, cold fixed preview/prune/history/Images brokers and headless
integration discovery/advertise/attach/withdraw all pass. The cold
broker check asserts one prune dispatch and retained conclusion bytes,
plus a new backend lease.
- **2.2.7 split storage** passes full pruning, native proxy/UI
contracts, assets/gallery, mixed split+inline content after flag
disable/restart, receipts, active-claim refusal and retained model
payload.
- Server/UI typechecks, complete server/UI/native-plugin build, **27**
package/vendor parity tests, **31** help tests, native driver test, YAML
and whitespace checks pass.
- External Python reference tests **9/9** and kit test typechecking
pass; Blender's vendored copy equals the reference.

Only private synthetic daemons, local providers and owned fixture state
were used. No shared service restart, real
credentials/database/configuration change or stable publication. Earlier
fixture attempts exposed an import-ID conflict, Windows path
normalization in the fixture ownership stub, and teardown ordering;
those harness defects were corrected without relaxing assertions. One
final rerun was needed after a simultaneous build cleaned its dist
input; the post-build rerun passes.

The native tests reuse source from the independently prepared readiness
foundation and broker work, adapted onto current dev without taking the
unrelated multi-backend branch. Public labels, controls and documented
steps stay unchanged; the fix restores their documented behavior. Large
existing touched files: `packages/server/src/index.ts` (~758 lines),
`packages/server/src/server/http-server.ts` (~2574),
`packages/server/src/workspaces/manager.ts` (~1230), and
`packages/server/src/server/routes/pending-requests.test.ts` (~1025).
Integrated CI and platform builds remain the final gate.
2026-11-10 16:25:51 +01:00

66 lines
4.8 KiB
JavaScript

import assert from "node:assert/strict"
export async function testSessionNavigationNative({ client, location, locationOptions, template, ready }) {
await ready({ client, location, locationOptions })
const seed = await client.session.create({ title: "Isolated long navigation fixture", location }, locationOptions)
const exported = await client.session.export({ sessionID: seed.id })
await client.session.remove({ sessionID: seed.id })
const id = index => `msg_navigation_${String(index).padStart(5, "0")}`
const nativeTool = structuredClone(template.content.find(part => part.type === "tool"))
assert(nativeTool)
const messages = Array.from({ length: 1501 }, (_, index) => ({
...structuredClone(template), id: id(index), content: index === 6
? [{ ...nativeTool, id: "part_navigation_tool" }]
: [{ type: "text", text: `Navigation passage ${index}` }],
}))
const session = await client.session.import({ ...exported, messages, location }, locationOptions)
const rpc = async (method, input) => (await client.rpc.call({ rpcID: "codenomad.session-pruning", method,
input: { sessionID: session.id, ...input }, location }, locationOptions)).output
try {
assert.deepEqual(await rpc("window", { target: { kind: "around", messageID: "msg_removed" } }),
{ status: "blocked", reason: "anchor_missing" }, "missing restore anchor is distinct from ownership conflicts")
const around = await rpc("window", { target: { kind: "around", messageID: id(1200) } })
assert.equal(around.status, "window", JSON.stringify(around))
assert.equal(around.messages.length, 200)
assert.equal(around.messages[80].id, id(1200))
const native = await client.session.export({ sessionID: session.id })
assert.deepEqual(around.messages, native.messages.slice(1120, 1320), "SQL reconstruction matches native export exactly")
assert.deepEqual(around.messages[80], await client.session.message.get({ sessionID: session.id, messageID: id(1200) }))
assert.deepEqual(await rpc("window", { target: around.resume }), around)
const prior = await rpc("window", { target: around.older })
assert(prior.messages.some(message => message.id === around.messages[0].id), "prior window retains the viewport anchor")
const following = await rpc("window", { target: around.newer })
assert(following.messages.some(message => message.id === around.messages.at(-1).id), "next window retains the viewport anchor")
let cursor, count = 0, indexedTool, indexedText
const checkpoints = []
do {
const outline = await rpc("outline", cursor ? { cursor } : {})
assert.equal(outline.status, "outline")
assert.equal(outline.entries.length, Math.min(16384, native.messages.length - count), "structural index does not wait for excerpt pagination")
assert(outline.entries.every(entry => !("preview" in entry)))
indexedTool ??= outline.entries.find(entry => entry.id === id(6))
indexedText ??= outline.entries.find(entry => entry.id === id(7))
checkpoints.push(...outline.checkpoints.map(({ changed, ...checkpoint }) => checkpoint))
count += outline.entries.length
cursor = outline.cursor
} while (cursor)
assert.equal(count, native.messages.length)
assert.equal(indexedTool?.toolName, nativeTool.name, "outline reads the representative name from native tool storage")
assert.equal(indexedText?.type, "assistant", "the fixture's template is a native assistant step")
assert.equal(indexedTool?.text, 0, "a native tool-only step has no visible text, hence no assistant marker")
assert.equal(indexedText?.text, 1, "outline counts native visible text parts")
assert(indexedTool && !("layout" in indexedTool), "outline keeps the entry shape older backends validate strictly")
const layouts = await rpc("outlineLayout", {})
assert.equal(layouts.status, "outline", JSON.stringify(layouts))
assert.equal(layouts.entries.find(entry => entry.id === id(6))?.layout, "X", "the versioned outline orders native visible parts")
assert.equal(layouts.entries.find(entry => entry.id === id(7))?.layout, "T")
const verified = await rpc("outline", { known: checkpoints })
assert.equal(verified.status, "outline", JSON.stringify(verified))
assert.equal(verified.entries.length, native.messages.length % 512, "unchanged saved ranges do not resend their structure")
assert.equal(verified.checkpoints.filter(checkpoint => !checkpoint.changed).length, 2)
const previews = await rpc("outlinePreview", { messageIDs: [id(1200)] })
assert.equal(previews.status, "previews", JSON.stringify(previews))
assert.equal(previews.entries[0].text, "Navigation passage 1200")
console.log("PASS: native 1501-message outline with representative tool metadata, direct distant windows, overlap and exact native payload/restore parity")
} finally { await client.session.remove({ sessionID: session.id }) }
}