Skip to content
Merged
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
8 changes: 4 additions & 4 deletions catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@
{
"id": "houdini",
"package": "@neuralnomads/opencode-houdini",
"range": "^0.2",
"range": "^0.3",
"displayName": "OpenCode for Houdini",
"family": "native",
"plugin": "neuralnomads.houdini",
"rpc": "neuralnomads.houdini",
"summary": {
"en": "Inspect and edit the graph open in Houdini through an authenticated local host and optional conversation panel.",
"fr": "Inspecter et modifier le graphe ouvert dans Houdini grâce à un hôte local authentifié et un panneau de conversation facultatif."
"en": "Read, change and check the open Houdini scene from any OpenCode conversation, with an optional panel inside Houdini.",
"fr": "Lire, modifier et vérifier la scène Houdini ouverte depuis toute conversation OpenCode, avec un panneau facultatif dans Houdini."
},
"app": { "name": "Houdini", "versions": ">=21.0" },
"host": { "protocol": 1, "range": "^0.2.0" },
"host": { "protocol": 1, "range": "^0.3.0" },
"docs": "https://github.com/NeuralNomadsAI/opencode-integrations/blob/main/integrations/houdini/plugin/README.md",
"status": "preview",
"sources": { "git": ["github:NeuralNomadsAI/opencode-integrations#main::path:integrations/houdini/plugin"] }
Expand Down
229 changes: 20 additions & 209 deletions integrations/blender/plugin/src/attachments.ts
Original file line number Diff line number Diff line change
@@ -1,218 +1,29 @@
// Attachments that survive an OpenCode restart. The offer registry lives in memory (HST-12): a restarted daemon
// forgets which conversation used which Blender, while the Blender keeps offering itself under the same offer key
// for its whole process life. Each Location's plugin keeps the attachments of its own conversations in its storage
// (`attachment/<sessionID>`, the offer key only as a SHA-256 digest, never a token, port or script handle) and, when
// the same Blender offers itself again, attaches the conversation back after the exact-session ownership check of
// RPC-12. A stored attachment lives while its Blender is offered and LEASE_MS after it stops; an explicit detach
// removes it. Restoring happens when the plugin loads and when offers change, never inside a read (RPC-13).
// Blender's attachments kept across an OpenCode restart: the kit's attachments (packages/integration-kit), under
// Blender's own keys.

import { createHash } from "node:crypto"
import {
type AttachmentStorage,
type Attachments,
createAttachments as createKitAttachments,
hostDigest as kitHostDigest,
trackAdvertise as kitTrackAdvertise,
type OfferRegistry,
resetAttachmentsForTests as resetKit,
} from "./kit.ts"

import type { BindingAuthority, OfferRegistry } from "./kit.ts"
export { ATTACHMENT_LEASE_MS as LEASE_MS } from "./kit.ts"
export type { AttachmentStorage, Attachments }

export const LEASE_MS = 120_000
const PERSIST_EVERY_MS = 30_000
const PREFIX = "attachment/"
const PAGES = 100
const SESSION_ID = /^[A-Za-z0-9_-]{1,128}$/
const HOST = /^[0-9a-f]{64}$/
const APP = "blender"

export type AttachmentStorage = {
get(key: string): Promise<unknown>
set(key: string, value: never): Promise<void>
remove(key: string): Promise<void>
scan(options: { prefix: string; after?: string; limit?: number }): Promise<{
entries: readonly { key: string; value: unknown }[]
next?: string
}>
}

type Stored = { session: string; host: string; label: string; since: number; seen: number }

// Per daemon process, per registry: the digest of each live offer's key, and the conversations attachments were
// recorded for (all Locations), so that a list can say which conversation holds which Blender.
type Shared = {
hosts: WeakMap<OfferRegistry, Map<string, string>>
sessions: Set<string>
advertising: Promise<unknown>
}
const SHARED = Symbol.for("neuralnomads.blender.attachments")
const store = globalThis as unknown as Record<symbol, Shared | undefined>
store[SHARED] ??= { hosts: new WeakMap(), sessions: new Set(), advertising: Promise.resolve() }
const shared = store[SHARED]

export function hostDigest(offerKey: string) {
return createHash("sha256").update(`neuralnomads.blender.offer\0${offerKey}`).digest("hex")
}

function digests(registry: OfferRegistry) {
let map = shared.hosts.get(registry)
if (!map) {
map = new Map()
shared.hosts.set(registry, map)
}
return map
}

function valid(value: unknown): value is Stored {
const entry = value as Stored
return (
typeof entry === "object" &&
entry !== null &&
typeof entry.session === "string" &&
SESSION_ID.test(entry.session) &&
typeof entry.host === "string" &&
HOST.test(entry.host) &&
typeof entry.label === "string" &&
entry.label.length <= 120 &&
Number.isFinite(entry.since)
)
}

// The advertise handler, serialized per daemon process: the offer an accepted announcement created is the one
// that appeared during the call, so its key's digest names it (the registry keeps keys private).
export function trackAdvertise(
registry: OfferRegistry,
advertise: (input: unknown) => Promise<unknown>,
): (input: unknown) => Promise<unknown> {
return (input) => {
const run = shared.advertising.then(async () => {
const before = new Set(registry.hosts().offers.map((offer) => offer.offerID))
const result = (await advertise(input)) as { outcome?: string }
const key = (input as { offer_key?: unknown } | null)?.offer_key
if (result?.outcome === "accepted" && typeof key === "string") {
const created = registry.hosts().offers.filter((offer) => !before.has(offer.offerID))
if (created.length === 1 && created[0]) digests(registry).set(hostDigest(key), created[0].offerID)
}
return result
})
shared.advertising = run.catch(() => {})
return run
}
}
export const hostDigest = (offerKey: string) => kitHostDigest(offerKey, APP)

export type Attachments = ReturnType<typeof createAttachments>
export const trackAdvertise = (registry: OfferRegistry, advertise: (input: unknown) => Promise<unknown>) =>
kitTrackAdvertise(registry, advertise, APP)

export function createAttachments(options: {
registry: OfferRegistry
storage?: AttachmentStorage
now?: () => number
// A fresh ownership snapshot of the session at this Location (null when it is not verifiably ours).
owns: (sessionID: string) => Promise<BindingAuthority | null>
}) {
const { registry, storage } = options
const now = options.now ?? Date.now
const records = new Map<string, Stored>()
const persisted = new Map<string, number>()

const live = () => new Map(registry.hosts().offers.map((offer) => [offer.offerID, offer]))
const offerOf = (host: string) => digests(registry).get(host)
const hostOf = (offerID: string) => [...digests(registry)].find(([, id]) => id === offerID)?.[0]

// Storage failures never fail a Blender call: the attachment then lasts until OpenCode stops.
const write = async (record: Stored) => {
persisted.set(record.session, now())
try {
await storage?.set(PREFIX + record.session, record as never)
} catch {}
}

const forget = async (sessionID: string) => {
if (!records.delete(sessionID)) return
persisted.delete(sessionID)
try {
await storage?.remove(PREFIX + sessionID)
} catch {}
}

return {
// The stored attachments, once; their lease starts again now.
async load() {
if (!storage) return
let after: string | undefined
for (let page = 0; page < PAGES; page += 1) {
let result: Awaited<ReturnType<AttachmentStorage["scan"]>> | undefined
try {
result = await storage.scan({ prefix: PREFIX, ...(after ? { after } : {}), limit: 100 })
} catch {}
const entries = Array.isArray(result?.entries) ? result.entries : []
for (const entry of entries) {
if (!valid(entry?.value) || entry.key !== PREFIX + entry.value.session) continue
records.set(entry.value.session, { ...entry.value, seen: now() })
shared.sessions.add(entry.value.session)
}
if (typeof result?.next !== "string" || entries.length === 0) break
after = result.next
}
},

// The conversation is attached now (tool, automatic or RPC attachment): keep it.
async remember(sessionID: string) {
const bound = registry.binding(sessionID)
const host = bound && hostOf(bound.offerID)
const offer = bound && live().get(bound.offerID)
if (!bound || !host || !offer) return
shared.sessions.add(sessionID)
const previous = records.get(sessionID)
const record = {
session: sessionID,
host,
label: offer.label,
since: previous?.host === host ? previous.since : now(),
seen: now(),
}
records.set(sessionID, record)
await write(record)
},

forget,

// Attaches back each stored conversation (or only `sessionID`) whose Blender is offered again.
async restore(sessionID?: string) {
const offers = live()
for (const record of [...records.values()]) {
if (sessionID !== undefined && record.session !== sessionID) continue
if (registry.binding(record.session)) continue
const offerID = offerOf(record.host)
if (!offerID || offers.get(offerID)?.protocol !== "ok") continue
const authority = await options.owns(record.session)
// Synchronous after the snapshot (RPC-12): the same registry state, still stored (a detach may have
// forgotten it meanwhile), still no binding, the offer still live.
if (!authority || !registry.current(authority) || records.get(record.session) !== record) continue
if (registry.binding(record.session)) continue
if (!live().has(offerID)) continue
if (registry.attach(offerID, record.session).outcome === "attached") record.seen = now()
}
},

// Renews the lease of the stored attachments whose Blender is offered; forgets those gone longer than it.
async sweep() {
const offers = live()
for (const record of [...records.values()]) {
const offerID = offerOf(record.host)
const offer = offerID ? offers.get(offerID) : undefined
if (offer) {
record.seen = now()
record.label = offer.label
if (now() - (persisted.get(record.session) ?? 0) > PERSIST_EVERY_MS) await write(record)
} else if (now() - record.seen > LEASE_MS) await forget(record.session)
}
},

// Conversations other than `except` attached to the offer (those this daemon recorded, all Locations).
heldBy(offerID: string, except?: string) {
return [...shared.sessions].some(
(session) => session !== except && registry.binding(session)?.offerID === offerID,
)
},

has(sessionID: string) {
return records.has(sessionID)
},
}
}
export const createAttachments = (options: Omit<Parameters<typeof createKitAttachments>[0], "app">) =>
createKitAttachments({ ...options, app: APP })

export function resetAttachmentsForTests() {
shared.sessions.clear()
shared.advertising = Promise.resolve()
resetKit(APP)
}
84 changes: 11 additions & 73 deletions integrations/blender/plugin/src/closed.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,19 @@
// What became of a Blender that stopped answering a conversation: crashed (Blender wrote its crash report; on
// Windows its process stays open behind Blender's crash window), closed (its process gone, no report: closed, or
// ended by another program), still running a long call, or no longer answering. The add-on's hello names its
// process, the folder Blender writes "<file name>.crash.txt" to, and its document. Every Blender of a user shares
// that folder: only the report named after this Blender's document counts.
// What became of a Blender that stopped answering a conversation (the kit's crashWatch): crashed, closed, or
// alive without renewing its offer. Blender writes "<file name>.crash.txt" to its temporary folder; every Blender
// of a user shares that folder: only the report named after this Blender's document counts.

import { readdirSync, statSync } from "node:fs"
import { join } from "node:path"
import { crashWatch, type HostProcess, readProcess } from "./kit.ts"

import { isAlive } from "./tasks.ts"

export type HostProcess = {
pid: number
tempDir: string
label?: string
since: number
offerID?: string
document?: string
}

export function readProcess(hello: unknown, label?: string, now = Date.now()): HostProcess | null {
const read = hello as { process?: { pid?: unknown; temp_dir?: unknown }; document?: unknown } | null
const found = read?.process
if (!found || !Number.isInteger(found.pid) || (found.pid as number) <= 0) return null
if (typeof found.temp_dir !== "string" || !found.temp_dir) return null
const document = typeof read?.document === "string" && read.document ? read.document : undefined
return { pid: found.pid as number, tempDir: found.temp_dir, label, since: now, document }
}
export { type HostProcess, readProcess }

// The name Blender gives its crash report: the file's name, or "blender" for a file never saved.
function reportName(host: HostProcess) {
return `${host.document ? host.document.replace(/\.blend$/i, "") : "blender"}.crash.txt`
}

// This Blender's crash report when written since `after` (by default since this conversation first reached it).
export function crashReport(host: HostProcess, after = host.since): string | null {
const wanted = reportName(host).toLowerCase()
let names: string[]
try {
names = readdirSync(host.tempDir)
} catch {
return null
}
for (const name of names) {
if (name.toLowerCase() !== wanted) continue
const path = join(host.tempDir, name)
try {
if (statSync(path).mtimeMs >= after - 1_000) return path
} catch {
// removed meanwhile
}
}
return null
}

const again = "Open Blender again and attach it (blender_hosts)."

// Crashed or closed; null while its process lives without a new crash report.
export function closedMessage(
host: HostProcess,
alive: (pid: number) => boolean = isAlive,
after = host.since,
): string | null {
const name = host.label ? ` (${host.label})` : ""
const report = crashReport(host, after)
const living = alive(host.pid)
if (report)
return `Blender crashed${name}: crash report ${report}. ${
living
? `Its process ${host.pid} is still open (Blender's crash window) and answers nothing.`
: `Its process ${host.pid} has ended.`
} Changes after its last save are lost. ${again}`
if (living) return null
return `Blender closed${name}: its process ${host.pid} has ended, with no crash report in ${host.tempDir} (closed, or ended by another program). ${again}`
}

// Its offer is gone while its process lives without a crash report: the add-on stopped renewing it.
export function silentMessage(host: HostProcess) {
const name = host.label ? ` (${host.label})` : ""
return `Blender${name} no longer offers itself: its process ${host.pid} still runs but stopped renewing its offer (frozen, or OpenCode Link was disabled). blender_hosts lists the Blenders offered.`
}
export const { crashReport, closedMessage, silentMessage } = crashWatch({
appName: "Blender",
app: "blender",
part: "OpenCode Link",
isReport: (name, host) => name.toLowerCase() === reportName(host).toLowerCase(),
})
Loading
Loading