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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ jobs:
- run: npm run check:markers

gaffer:
name: Gaffer host and panel consent (1.7.6.0, private profile)
name: Gaffer host, programs and panel consent (1.7.6.0, private profile)
runs-on: windows-latest
timeout-minutes: 15
timeout-minutes: 25
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand All @@ -57,11 +57,11 @@ jobs:
Invoke-WebRequest 'https://github.com/GafferHQ/gaffer/releases/download/1.7.6.0/gaffer-1.7.6.0-windows.zip' -OutFile $archive
if ((Get-FileHash $archive -Algorithm SHA256).Hash -ne '1B6C03F768E6FDFDE686A2AF256E59A313E1BFC64643371FA587DAC24AC69FC0') { throw 'Gaffer checksum mismatch' }
Expand-Archive $archive -DestinationPath $env:RUNNER_TEMP
- name: Exercise the real graph, authenticated listener and offscreen panel
- name: Exercise OpenCode Link, the fixed programs and the offscreen panel in the real Gaffer
shell: pwsh
run: |
$env:GAFFER_TEST_BIN = Join-Path $env:RUNNER_TEMP 'gaffer-1.7.6.0-windows/bin/gaffer.cmd'
node --test integrations/gaffer/test/real-host.test.ts
node --test --test-concurrency=1 integrations/gaffer/test/real-host.test.ts integrations/gaffer/test/gaffer.test.ts

blender-native:
name: Blender binding contract (OpenCode 2.2.5, private service)
Expand Down
8 changes: 4 additions & 4 deletions catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@
{
"id": "gaffer",
"package": "@neuralnomads/opencode-gaffer",
"range": "^0.2",
"range": "^0.3",
"displayName": "OpenCode for Gaffer",
"family": "native",
"plugin": "neuralnomads.gaffer",
"rpc": "neuralnomads.gaffer",
"summary": {
"en": "Inspect and edit the open Gaffer graph through an authenticated host, from any OpenCode client or the in-app panel.",
"fr": "Inspecter et modifier le graphe Gaffer ouvert via un hôte authentifié, depuis tout client OpenCode ou le panneau intégré."
"en": "Read, change and check the scripts open in Gaffer from any OpenCode conversation, with an optional panel inside Gaffer.",
"fr": "Lire, modifier et vérifier les scripts ouverts dans Gaffer depuis toute conversation OpenCode, avec un panneau facultatif dans Gaffer."
},
"app": { "name": "Gaffer", "versions": ">=1.6" },
"host": { "protocol": 1, "range": "^0.2.0" },
"host": { "protocol": 1, "range": "^0.3.0" },
"docs": "https://github.com/NeuralNomadsAI/opencode-integrations/blob/main/integrations/gaffer/plugin/README.md",
"status": "preview",
"sources": { "git": ["github:NeuralNomadsAI/opencode-integrations#main::path:integrations/gaffer/plugin"] }
Expand Down
2 changes: 1 addition & 1 deletion integrations/gaffer/host/manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "name": "OpenCode Link for Gaffer", "version": "0.2.0", "protocol": 1, "license": "MIT" }
{ "name": "OpenCode Link for Gaffer", "version": "0.3.0", "protocol": 1, "license": "MIT" }
191 changes: 191 additions & 0 deletions integrations/gaffer/host/python/GafferOpenCode/Advertiser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
"""Offers this Gaffer's open scripts to the discovered OpenCode services (standard library only).

Runs on its own thread: discovery and RPC calls never block Gaffer's main thread. One offer per open script
(labelled with its file name), renewed every 5 seconds straight at the services that took it; a script closed is
withdrawn, and an offer whose renewals stop expires within 15 seconds anyway. Results carry service file names,
labels and outcomes only, never the daemon password nor the host token.
"""

import os
import re
import secrets
import threading
import time

RPC_ID = "neuralnomads.gaffer"
PROTOCOL = 1
RENEW_SECONDS = 5.0
# One renewal may wait this long for a busy service: an offer lasts 15 s, renewed every 5 s.
RENEW_TIMEOUT = 8.0
# Renewals go straight to the services that took the offers; every this many renewals (or after a failure) the
# services are discovered again, which finds a service started since.
DISCOVER_EVERY = 12


def _launchKey() :

"""A Gaffer started by OpenCode offers its first script under the key OpenCode chose."""

key = os.environ.get( "OPENCODE_GAFFER_OFFER_KEY", "" )
return key if re.fullmatch( r"gaffer-launch-[0-9a-f]{32}", key ) else None


# Stable for the life of the Gaffer process; each script's key adds its number (the first script: none).
OFFER_KEY = _launchKey() or "gaffer-{}-{}".format( os.getpid(), secrets.token_hex( 8 ) )


def offerKey( number ) :

return OFFER_KEY if number == 0 else "{}.{}".format( OFFER_KEY, number )


class Advertiser :

"""`snapshot()` returns ( port, token, [ ( script_handle, label, offer_key ), ... ] ), or a None token while not
ready."""

def __init__( self, snapshot, env = None, interval = RENEW_SECONDS ) :

self.__snapshot = snapshot
self.__env = env
self.__interval = interval
self.__stop = threading.Event()
self.__lock = threading.Lock()
self.__results = []
self.__count = 0
self.__targets = {}
self.__offered = {} # offer_key: token, of the offers made
self.__thread = threading.Thread( target = self.__loop, name = "GafferOpenCodeAdvertiser", daemon = True )

def start( self ) :

self.__thread.start()
return self

@property
def results( self ) :

with self.__lock :
return list( self.__results )

def advertiseOnce( self ) :

port, token, scripts = self.__snapshot()
if token is None :
return None
offers = [
{
"host" : "127.0.0.1", "port" : port, "token" : token, "script_handle" : handle, "label" : label,
"protocol" : PROTOCOL, "offer_key" : key,
}
for handle, label, key in scripts
]
# A script closed since: its offer is withdrawn rather than left to expire.
keys = { offer["offer_key"] for offer in offers }
for key in [ key for key in self.__offered if key not in keys ] :
self.__withdraw( key, self.__offered.pop( key ) )
results = self.__renew( offers ) if offers else []
for offer in offers :
self.__offered[offer["offer_key"]] = token
with self.__lock :
self.__results = results
return results

def __renew( self, offers ) :

"""Renews at the services that took the offers (one request per offer); discovers them again on a failure and
every DISCOVER_EVERY renewals (a discovery reads each service's info and the plugin's status first)."""

from . import opencode_service # the kit's client, vendored into the payload

self.__count += 1
targets = self.__targets
if targets and self.__count % DISCOVER_EVERY :
results, failed = [], False
for name, ( service, directory ) in targets.items() :
for offer in offers :
try :
status, body = opencode_service.call( service, RPC_ID, "advertise", offer, directory, RENEW_TIMEOUT )
outcome = opencode_service._outcome( "advertise", status, body )
except ( OSError, ValueError ) :
outcome = "error"
results.append( { "service" : name, "label" : offer["label"], "outcome" : outcome } )
failed = failed or outcome not in ( "accepted", "renewed" )
if not failed :
return results
found = {}
results = []
try :
services = opencode_service.discover( env = self.__env, timeout = RENEW_TIMEOUT )
except ( OSError, ValueError ) :
services = []
for service in services :
for directory in opencode_service.location_candidates( None, self.__env ) :
outcomes = []
try :
deadline = time.monotonic() + RENEW_TIMEOUT
status, body = opencode_service._ready( service, RPC_ID, directory, deadline )
if opencode_service._unavailable( status, body ) :
continue
if status != 200 :
outcomes = [ ( offer, "error" ) for offer in offers ]
else :
for offer in offers :
try :
status, body = opencode_service.call(
service, RPC_ID, "advertise", offer, directory, max( 0.5, deadline - time.monotonic() )
)
outcomes.append( ( offer, opencode_service._outcome( "advertise", status, body ) ) )
except ( OSError, ValueError ) :
outcomes.append( ( offer, "error" ) )
except ( OSError, ValueError ) :
outcomes = [ ( offer, "error" ) for offer in offers ]
for offer, outcome in outcomes :
results.append( { "service" : service.file, "label" : offer["label"], "outcome" : outcome } )
if any( outcome in ( "accepted", "renewed" ) for _, outcome in outcomes ) :
found[service.file] = ( service, directory )
break
else :
results.append( { "service" : service.file, "outcome" : "unavailable" } )
self.__targets = found
return results

def __withdraw( self, key, token ) :

from . import opencode_service

try :
opencode_service.withdraw_all( RPC_ID, key, token, env = self.__env )
except ( OSError, ValueError ) :
pass

def __loop( self ) :

while not self.__stop.is_set() :
try :
results = self.advertiseOnce() or []
except Exception : # the next renewal tries again
results = []
taken = all( result.get( "outcome" ) in ( "accepted", "renewed" ) for result in results ) and results
# A renewal that failed is tried again soon: an offer lasts 15 s from the last one taken.
self.__stop.wait( self.__interval if taken else min( self.__interval, 1.0 ) )

def stop( self, token, withdraw = True ) :

"""Stops renewing; withdraws every offer from every service with the token it was announced with."""

from . import opencode_service

self.__stop.set()
if self.__thread.is_alive() and self.__thread is not threading.current_thread() :
self.__thread.join( timeout = opencode_service.TIMEOUT * 2 )
if not withdraw or token is None :
return []
results = []
for key in list( self.__offered ) :
try :
results += opencode_service.withdraw_all( RPC_ID, key, token, env = self.__env )
except ( OSError, ValueError ) :
pass
self.__offered = {}
return results
129 changes: 129 additions & 0 deletions integrations/gaffer/host/python/GafferOpenCode/Gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""Runs requests on Gaffer's main thread, and says which runs.

In the interface, through GafferUI.EventLoop.executeOnUIThread (callable from any thread: it queues the call and
schedules it with a queued Qt invocation, without waiting for the main thread); in `gaffer python`, through pump(),
which the main thread calls. A request the main thread has not taken within QUEUE_WAIT_S is answered busy, naming
what runs there and since when. `running()` answers from any thread. Never one request inside another: a request
reached from a nested event loop (a dialog, processEvents) while another runs is put back and retried.
"""

import queue
import threading
import time

QUEUE_WAIT_S = 10.0
RUN_LIMIT_S = 3600.0
RETRY_S = 0.05


class GateError( RuntimeError ) :

def __init__( self, code, message, data = None ) :

RuntimeError.__init__( self, message )
self.code = code
self.data = data or {}


class UIThreadGate :

def __init__( self, post = None ) :

# post( callable ) schedules a call on the main thread (the interface's event loop); None: pump() serves.
self.__post = post
self.__mainThread = threading.get_ident()
self.__running = None
self.__queued = 0
self.__count = threading.Lock()
self.__jobs = queue.Queue()

def running( self ) :

current = self.__running
if current is None :
return { "busy" : False, "queued" : self.__queued }
label, since = current
return { "busy" : True, "running" : label, "seconds" : round( time.monotonic() - since, 1 ), "queued" : self.__queued }

def __invoke( self, label, callback ) :

self.__running = ( label, time.monotonic() )
try :
return callback()
finally :
self.__running = None

def pump( self, seconds = 0.05, stop = None ) :

"""`gaffer python`: runs the queued requests on this (the main) thread for `seconds`, or until `stop()`."""

deadline = time.monotonic() + seconds
while time.monotonic() < deadline and not ( stop is not None and stop() ) :
try :
job = self.__jobs.get( timeout = min( 0.05, max( 0.0, deadline - time.monotonic() ) ) )
except queue.Empty :
continue
job()

def __schedule( self, job ) :

if self.__post is None :
self.__jobs.put( job )
else :
self.__post( job )

def run( self, callback, label = "a request" ) :

if threading.get_ident() == self.__mainThread :
return self.__invoke( label, callback )
started = threading.Event()
complete = threading.Event()
expired = threading.Event()
lock = threading.Lock()
result = []

def invoke() :
if self.__running is not None :
# Reached from a nested event loop while a request runs: later, never inside it.
threading.Timer( RETRY_S, lambda : self.__schedule( invoke ) ).start()
return
with lock :
if expired.is_set() :
return
started.set()
try :
result.append( ( True, self.__invoke( label, callback ) ) )
except Exception as error :
result.append( ( False, error ) )
finally :
complete.set()

with self.__count :
self.__queued += 1
try :
self.__schedule( invoke )
if not started.wait( QUEUE_WAIT_S ) :
with lock :
if not started.is_set() :
expired.set()
now = self.running()
doing = (
"it still runs {} ({} s so far): nothing was done for this request; ask again once it has "
"finished".format( now["running"], now["seconds"] )
if now.get( "busy" )
else "a modal dialog, a long computation or a script outside OpenCode holds it"
)
raise GateError(
"ui_timeout",
"Gaffer's main thread did not take the request within {:.0f} s: {}".format( QUEUE_WAIT_S, doing ),
{ "running" : now.get( "running" ), "seconds" : now.get( "seconds" ) },
)
finally :
with self.__count :
self.__queued -= 1
if not complete.wait( RUN_LIMIT_S ) :
raise GateError( "ui_timeout", "Gaffer did not finish the request within {:.0f} s".format( RUN_LIMIT_S ) )
succeeded, value = result[0]
if not succeeded :
raise value
return value
Loading
Loading