@open-artifacts/bus 0.198.0 → 0.199.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/oa-bus.ts +1 -1
- package/assets/oa_bus/bus.py +168 -9
- package/assets/oa_bus/plugin.py +81 -6
- package/assets/oa_bus/plugin.yaml +2 -0
- package/assets/oa_bus/roots.py +26 -0
- package/assets/oa_bus/state.py +98 -0
- package/assets/openclaw/index.js +1 -1
- package/assets/openclaw/package.json +1 -1
- package/dist/index.js +7 -3
- package/package.json +2 -2
package/assets/oa-bus.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { StringDecoder } from "node:string_decoder";
|
|
|
11
11
|
type Json = Record<string, unknown>;
|
|
12
12
|
|
|
13
13
|
/** The IPC marker (CONTRACT.md): the package version, stamped by the CLI's bundler. */
|
|
14
|
-
const CLIENT_VERSION = "0.
|
|
14
|
+
const CLIENT_VERSION = "0.199.1";
|
|
15
15
|
|
|
16
16
|
type Frame = Json & { type: string; id: string };
|
|
17
17
|
type Ctx = {
|
package/assets/oa_bus/bus.py
CHANGED
|
@@ -9,6 +9,7 @@ import os
|
|
|
9
9
|
import re
|
|
10
10
|
import sys
|
|
11
11
|
import threading
|
|
12
|
+
import time
|
|
12
13
|
import unicodedata
|
|
13
14
|
|
|
14
15
|
from . import copy
|
|
@@ -21,6 +22,11 @@ from .wrap import wrap
|
|
|
21
22
|
SHOWN_MAX = 1000
|
|
22
23
|
# A delivery waits this long for the current bus name before using the welcome's.
|
|
23
24
|
SELF_TIMEOUT_S = 2.0
|
|
25
|
+
# A wake (a keyed inject_message that starts a turn) holds back further wakes for this root until
|
|
26
|
+
# its turn starts, or this long: mail that arrives meanwhile rides in on that turn's context.
|
|
27
|
+
WAKE_HOLD_S = 60.0
|
|
28
|
+
# After Hermes refuses a wake (no allow_gateway_injection, say) this root is not tried again for this long.
|
|
29
|
+
WAKE_BACKOFF_S = 600.0
|
|
24
30
|
|
|
25
31
|
|
|
26
32
|
def slug_of(text, kind="hermes"):
|
|
@@ -201,13 +207,30 @@ class _Root:
|
|
|
201
207
|
self.retry = None
|
|
202
208
|
self.stopped = False
|
|
203
209
|
self.shown = {}
|
|
210
|
+
# The gateway's routing key for this root's session, learned in a turn hook; None until then.
|
|
211
|
+
self.session_key = None
|
|
212
|
+
# "dm" (one person), "shared" (a dm-type chat with no single user), "group", "channel",
|
|
213
|
+
# "thread"; None when Hermes did not say. Anything but "dm" is not woken unless listed.
|
|
214
|
+
self.chat_type = None
|
|
215
|
+
# When a wake was last sent and its turn has not started yet.
|
|
216
|
+
self.woken_at = None
|
|
217
|
+
# No wake is tried for this root before this time (a refused one).
|
|
218
|
+
self.wake_off_until = None
|
|
219
|
+
# The newest session id seen for this root.
|
|
220
|
+
self.session_id = None
|
|
204
221
|
|
|
205
222
|
|
|
206
223
|
class Bus:
|
|
207
224
|
"""`inject` is ctx.inject_message; `connect` makes a Link (replaced in tests)."""
|
|
208
225
|
|
|
209
226
|
def __init__(self, roots, inject=None, connect=None, start=None, cwd=None, spawn=None,
|
|
210
|
-
current_session=None, codex_runtime=None, habits_marker=None, title_of=None
|
|
227
|
+
current_session=None, codex_runtime=None, habits_marker=None, title_of=None,
|
|
228
|
+
clock=None, store=None, session_exists=None, wake_allow=None):
|
|
229
|
+
self._clock = clock or time.monotonic
|
|
230
|
+
self._store = store
|
|
231
|
+
# The owner's list (plugins.entries.<id>.settings.wake): session keys and platform names.
|
|
232
|
+
self._wake_allow = wake_allow or (lambda: [])
|
|
233
|
+
self._session_exists = session_exists or (lambda _sid: True)
|
|
211
234
|
self.roots = roots
|
|
212
235
|
# The user-set session title of a root, or None.
|
|
213
236
|
self._title_of = title_of or hermes_session_title
|
|
@@ -443,17 +466,81 @@ class Bus:
|
|
|
443
466
|
|
|
444
467
|
def _try_inject(self, root, text):
|
|
445
468
|
"""Injects into the interactive CLI when it is on this root's session and idle.
|
|
446
|
-
inject_message addresses the one CLI of the process, whatever session it is on now.
|
|
469
|
+
inject_message addresses the one CLI of the process, whatever session it is on now.
|
|
470
|
+
Any other root is woken by its session key when Hermes gave us one (see _wake)."""
|
|
447
471
|
if self._inject is None:
|
|
448
472
|
return False
|
|
449
473
|
with root.lock:
|
|
450
474
|
idle = root.platform == "cli" and not root.busy
|
|
451
|
-
if
|
|
452
|
-
|
|
475
|
+
if idle and self._is_current_cli(root):
|
|
476
|
+
try:
|
|
477
|
+
return bool(self._inject(text))
|
|
478
|
+
except Exception:
|
|
479
|
+
return False
|
|
480
|
+
return self._wake(root, text)
|
|
481
|
+
|
|
482
|
+
def _may_wake(self, root):
|
|
483
|
+
"""A direct message with one person; else only what the owner listed. An exact key wakes any
|
|
484
|
+
chat; a platform name wakes known dm or thread chats, never a group or channel. An unknown
|
|
485
|
+
chat type is woken only by its exact key."""
|
|
486
|
+
if root.chat_type == "dm":
|
|
487
|
+
return True
|
|
453
488
|
try:
|
|
454
|
-
|
|
489
|
+
listed = {x for x in self._wake_allow() if isinstance(x, str)}
|
|
455
490
|
except Exception:
|
|
456
491
|
return False
|
|
492
|
+
if root.session_key in listed:
|
|
493
|
+
return True
|
|
494
|
+
return root.chat_type == "thread" and root.platform in listed
|
|
495
|
+
|
|
496
|
+
def _wake(self, root, text):
|
|
497
|
+
"""Starts a turn for a root that is not an idle current CLI: inject_message(session_key=...)
|
|
498
|
+
queues the text on that session (starting a turn when it is idle) in a gateway or TUI host.
|
|
499
|
+
Never for a CLI root: Hermes's classic CLI ignores the key and takes the text for its current
|
|
500
|
+
session. One wake per root at a time: while one is pending, the text stays queued and the
|
|
501
|
+
woken turn's context carries it. False when it is not accepted (then it stays queued)."""
|
|
502
|
+
with root.lock:
|
|
503
|
+
key, sid = root.session_key, root.session_id
|
|
504
|
+
if not key or root.platform in (None, "cli") or not self._may_wake(root):
|
|
505
|
+
return False
|
|
506
|
+
now = self._clock()
|
|
507
|
+
if root.wake_off_until is not None and now < root.wake_off_until:
|
|
508
|
+
return False
|
|
509
|
+
if sid is not None and not self._session_alive(sid):
|
|
510
|
+
self._forget(root)
|
|
511
|
+
return False
|
|
512
|
+
with root.lock:
|
|
513
|
+
now = self._clock()
|
|
514
|
+
if root.woken_at is not None and now - root.woken_at < WAKE_HOLD_S:
|
|
515
|
+
return False
|
|
516
|
+
root.woken_at = now
|
|
517
|
+
try:
|
|
518
|
+
ok = bool(self._inject(text, session_key=key))
|
|
519
|
+
except Exception:
|
|
520
|
+
ok = False
|
|
521
|
+
if ok:
|
|
522
|
+
with root.lock:
|
|
523
|
+
root.wake_off_until = None
|
|
524
|
+
else:
|
|
525
|
+
with root.lock:
|
|
526
|
+
root.woken_at = None
|
|
527
|
+
root.wake_off_until = self._clock() + WAKE_BACKOFF_S
|
|
528
|
+
return ok
|
|
529
|
+
|
|
530
|
+
def _session_alive(self, sid):
|
|
531
|
+
try:
|
|
532
|
+
return bool(self._session_exists(sid))
|
|
533
|
+
except Exception:
|
|
534
|
+
return True
|
|
535
|
+
|
|
536
|
+
def _forget(self, root):
|
|
537
|
+
"""The root's session ended (Hermes reset it without a finalize): no key, no stored entry, no link."""
|
|
538
|
+
with root.lock:
|
|
539
|
+
root.session_key = None
|
|
540
|
+
root.chat_type = None
|
|
541
|
+
if self._store is not None:
|
|
542
|
+
self._store.drop(root.ref)
|
|
543
|
+
self.close(root.ref)
|
|
457
544
|
|
|
458
545
|
def _hand_over(self, root, text):
|
|
459
546
|
"""A bus notice: injected when the CLI is idle on this root, else queued for the next turn."""
|
|
@@ -469,7 +556,8 @@ class Bus:
|
|
|
469
556
|
p = self._habits_marker
|
|
470
557
|
return os.path.isfile(p) and not os.path.islink(p)
|
|
471
558
|
|
|
472
|
-
def turn_start(self, session_id, platform, user_message=None, conversation_history=None
|
|
559
|
+
def turn_start(self, session_id, platform, user_message=None, conversation_history=None,
|
|
560
|
+
session_key=None, chat_type=None):
|
|
473
561
|
"""pre_llm_call: the queued messages as this turn's context, or None.
|
|
474
562
|
|
|
475
563
|
Hermes drops a hook's context when the turn's real content is not text, and never
|
|
@@ -480,9 +568,21 @@ class Bus:
|
|
|
480
568
|
except NotOnBus:
|
|
481
569
|
return None
|
|
482
570
|
root = self.state(ref)
|
|
571
|
+
if isinstance(session_key, str) and session_key and root.session_key != session_key:
|
|
572
|
+
# Before taking this root's lock: _claim_key takes the old root's, and no thread ever holds
|
|
573
|
+
# two root locks at once, so two roots claiming each other cannot deadlock.
|
|
574
|
+
self._claim_key(root, session_key)
|
|
483
575
|
with root.lock:
|
|
484
576
|
root.busy = True
|
|
485
577
|
root.platform = platform or root.platform
|
|
578
|
+
root.woken_at = None # the turn a wake asked for has started
|
|
579
|
+
root.session_id = session_id
|
|
580
|
+
if isinstance(session_key, str) and session_key:
|
|
581
|
+
root.session_key = session_key
|
|
582
|
+
if chat_type is not None:
|
|
583
|
+
root.chat_type = chat_type
|
|
584
|
+
remember = (self._store is not None and root.session_key and root.platform not in (None, "cli"))
|
|
585
|
+
key_now, platform_now, type_now = root.session_key, root.platform, root.chat_type
|
|
486
586
|
if platform == "cli":
|
|
487
587
|
self._cli_root = ref
|
|
488
588
|
out = []
|
|
@@ -496,6 +596,8 @@ class Bus:
|
|
|
496
596
|
# the background (touch), so a root's first turn goes without.
|
|
497
597
|
habits = (root.link is not None and not root.link.closed
|
|
498
598
|
and self._habits_on())
|
|
599
|
+
if remember:
|
|
600
|
+
self._store.upsert(ref, session_id, key_now, platform_now, type_now or "unknown")
|
|
499
601
|
self.touch(ref)
|
|
500
602
|
# #1822: a hub that was down when the link opened: offer the proof again (rate limited
|
|
501
603
|
# by the link), off this thread, since it waits for the daemon.
|
|
@@ -510,6 +612,58 @@ class Bus:
|
|
|
510
612
|
parts = [t for t, _ in out] + ([HABITS] if habits else [])
|
|
511
613
|
return {"context": "\n\n".join(parts)}
|
|
512
614
|
|
|
615
|
+
def session_open(self, session_id):
|
|
616
|
+
"""on_session_open / on_session_start: the link opens now, not at the session's first turn.
|
|
617
|
+
Nothing here waits on the daemon, and a session that cannot be placed (a subagent, an unknown
|
|
618
|
+
id) is left alone."""
|
|
619
|
+
try:
|
|
620
|
+
ref = self.roots.resolve(session_id)
|
|
621
|
+
except NotOnBus:
|
|
622
|
+
return
|
|
623
|
+
self.touch(ref)
|
|
624
|
+
|
|
625
|
+
def _claim_key(self, root, key):
|
|
626
|
+
"""A session key is one chat. Another root holding it is the old one: it forgets the key, is
|
|
627
|
+
dropped from the store and closed, and its mail is never injected into this session."""
|
|
628
|
+
with self._lock:
|
|
629
|
+
others = [r for r in self._by_root.values() if r is not root]
|
|
630
|
+
for old in others:
|
|
631
|
+
with old.lock:
|
|
632
|
+
held = old.session_key == key
|
|
633
|
+
if held:
|
|
634
|
+
old.session_key = None
|
|
635
|
+
old.chat_type = None
|
|
636
|
+
if held:
|
|
637
|
+
if self._store is not None:
|
|
638
|
+
self._store.drop(old.ref)
|
|
639
|
+
self._spawn(lambda ref=old.ref: self.close(ref))
|
|
640
|
+
return True
|
|
641
|
+
|
|
642
|
+
def restore(self):
|
|
643
|
+
"""At load in a gateway process: links again the roots remembered from before a restart
|
|
644
|
+
(active in the last 7 days, still in Hermes's history), each with its session key, so mail
|
|
645
|
+
wakes them without a first turn. Run in the background; never raises."""
|
|
646
|
+
if self._store is None:
|
|
647
|
+
return
|
|
648
|
+
try:
|
|
649
|
+
entries = self._store.recent()
|
|
650
|
+
except Exception:
|
|
651
|
+
return
|
|
652
|
+
for ref, e in entries:
|
|
653
|
+
try:
|
|
654
|
+
if not self._session_exists(e["session_id"]):
|
|
655
|
+
self._store.drop(ref)
|
|
656
|
+
continue
|
|
657
|
+
root = self.state(ref)
|
|
658
|
+
with root.lock:
|
|
659
|
+
root.session_key = root.session_key or e["key"]
|
|
660
|
+
root.session_id = root.session_id or e["session_id"]
|
|
661
|
+
root.platform = root.platform or e["platform"]
|
|
662
|
+
root.chat_type = root.chat_type or (None if e["chat_type"] == "unknown" else e["chat_type"])
|
|
663
|
+
self.touch(ref)
|
|
664
|
+
except Exception:
|
|
665
|
+
continue
|
|
666
|
+
|
|
513
667
|
def turn_end(self, session_id):
|
|
514
668
|
"""on_session_end: the end of one run_conversation call."""
|
|
515
669
|
try:
|
|
@@ -520,14 +674,19 @@ class Bus:
|
|
|
520
674
|
with root.lock:
|
|
521
675
|
root.busy = False
|
|
522
676
|
|
|
523
|
-
def session_finalize(self, session_id):
|
|
524
|
-
"""on_session_finalize: a session closed; None means the whole process is going.
|
|
677
|
+
def session_finalize(self, session_id, reason=None):
|
|
678
|
+
"""on_session_finalize: a session closed; None means the whole process is going. A gateway
|
|
679
|
+
shutdown keeps the remembered root (the chat's session survives the restart); any other end
|
|
680
|
+
forgets it."""
|
|
525
681
|
if session_id is None:
|
|
526
682
|
self.close_all()
|
|
527
683
|
return
|
|
528
684
|
if self.roots.is_subagent(session_id):
|
|
529
685
|
return
|
|
530
686
|
try:
|
|
531
|
-
self.
|
|
687
|
+
ref = self.roots.root_of(session_id)
|
|
532
688
|
except NotOnBus:
|
|
533
689
|
return
|
|
690
|
+
if self._store is not None and reason != "shutdown":
|
|
691
|
+
self._store.drop(ref)
|
|
692
|
+
self.close(ref)
|
package/assets/oa_bus/plugin.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"""Registers the bus tools and hooks with Hermes (a plugin's `register(ctx)`)."""
|
|
2
2
|
|
|
3
3
|
import atexit
|
|
4
|
+
import os
|
|
5
|
+
import time
|
|
4
6
|
|
|
5
7
|
from . import proof as _proof
|
|
6
8
|
|
|
7
9
|
from .bus import Bus
|
|
8
|
-
from .roots import Roots
|
|
10
|
+
from .roots import Roots, hermes_session_exists
|
|
11
|
+
from .state import RootStore
|
|
9
12
|
from .tools import TOOLS, Tools
|
|
10
13
|
|
|
11
14
|
|
|
@@ -13,11 +16,59 @@ def _servers():
|
|
|
13
16
|
return _proof.proof_servers()
|
|
14
17
|
|
|
15
18
|
|
|
16
|
-
def
|
|
19
|
+
def _bound_session_key():
|
|
20
|
+
"""The gateway's routing key for the session this hook runs for, or None (not a gateway process,
|
|
21
|
+
an older Hermes, or no session bound to this thread)."""
|
|
22
|
+
try:
|
|
23
|
+
from gateway.session_context import bound_session_key
|
|
24
|
+
|
|
25
|
+
return bound_session_key() or None
|
|
26
|
+
except Exception:
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _bound_chat_type():
|
|
31
|
+
""""dm" for a chat with one person, "shared" for a dm-type chat with no single user, else the
|
|
32
|
+
gateway's own chat type ("group", "channel", "thread"); None when it cannot be told."""
|
|
33
|
+
try:
|
|
34
|
+
from gateway.session_context import get_session_env
|
|
35
|
+
|
|
36
|
+
kind = get_session_env("HERMES_SESSION_CHAT_TYPE", "")
|
|
37
|
+
if not kind:
|
|
38
|
+
return None
|
|
39
|
+
if kind == "dm" and not get_session_env("HERMES_SESSION_USER_ID", ""):
|
|
40
|
+
return "shared"
|
|
41
|
+
return kind
|
|
42
|
+
except Exception:
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _restore_when_live(bus, ready, tries=120, pause=0.5, sleep=time.sleep):
|
|
47
|
+
for _ in range(tries):
|
|
48
|
+
try:
|
|
49
|
+
if ready():
|
|
50
|
+
bus.restore()
|
|
51
|
+
return
|
|
52
|
+
except Exception:
|
|
53
|
+
return
|
|
54
|
+
sleep(pause)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _wake_list(ctx):
|
|
58
|
+
"""plugins.entries.<id>.settings.wake: the owner's list of session keys and platform names."""
|
|
59
|
+
try:
|
|
60
|
+
v = ctx.get_config("wake", [])
|
|
61
|
+
except Exception:
|
|
62
|
+
return []
|
|
63
|
+
return [x for x in v if isinstance(x, str)] if isinstance(v, list) else []
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def register(ctx, roots=None, bus=None, proof_servers=None, install=None, gateway_ready=None):
|
|
17
67
|
"""`proof_servers` (this instance's raw MCP server names) and `install` (what puts the proof
|
|
18
68
|
argument on the wire, see proof.py) are replaced in tests."""
|
|
19
69
|
roots = roots or Roots()
|
|
20
|
-
bus = bus or Bus(roots, inject=ctx.inject_message)
|
|
70
|
+
bus = bus or Bus(roots, inject=ctx.inject_message, store=RootStore(), session_exists=hermes_session_exists,
|
|
71
|
+
wake_allow=lambda: _wake_list(ctx))
|
|
21
72
|
tools = Tools(bus)
|
|
22
73
|
mine = proof_servers if proof_servers is not None else _servers()
|
|
23
74
|
put_on_wire = install or _proof.install
|
|
@@ -42,21 +93,45 @@ def register(ctx, roots=None, bus=None, proof_servers=None, install=None):
|
|
|
42
93
|
put_on_wire(bus, mine)
|
|
43
94
|
except Exception:
|
|
44
95
|
pass
|
|
45
|
-
return bus.turn_start(session_id, platform, user_message, conversation_history
|
|
96
|
+
return bus.turn_start(session_id, platform, user_message, conversation_history,
|
|
97
|
+
session_key=_bound_session_key(), chat_type=_bound_chat_type())
|
|
98
|
+
|
|
99
|
+
def on_session_open(session_id=None, **_kw):
|
|
100
|
+
bus.session_open(session_id)
|
|
101
|
+
|
|
102
|
+
def on_session_start(session_id=None, **_kw):
|
|
103
|
+
bus.session_open(session_id)
|
|
46
104
|
|
|
47
105
|
def on_session_end(session_id=None, **_kw):
|
|
48
106
|
bus.turn_end(session_id)
|
|
49
107
|
|
|
50
|
-
def on_session_finalize(session_id=None, **_kw):
|
|
51
|
-
bus.session_finalize(session_id)
|
|
108
|
+
def on_session_finalize(session_id=None, reason=None, **_kw):
|
|
109
|
+
bus.session_finalize(session_id, reason)
|
|
52
110
|
|
|
53
111
|
def subagent_start(parent_session_id=None, child_session_id=None, **_kw):
|
|
54
112
|
roots.note_subagent(parent_session_id, child_session_id)
|
|
55
113
|
|
|
56
114
|
ctx.register_hook("pre_llm_call", pre_llm_call)
|
|
115
|
+
# Presence before the first turn: on_session_open (a local session created or resumed) and
|
|
116
|
+
# on_session_start (a messaging chat's first turn). An older Hermes without either keeps
|
|
117
|
+
# opening the link at the first pre_llm_call.
|
|
118
|
+
for name, cb in (("on_session_open", on_session_open), ("on_session_start", on_session_start)):
|
|
119
|
+
try:
|
|
120
|
+
ctx.register_hook(name, cb)
|
|
121
|
+
except Exception:
|
|
122
|
+
pass
|
|
57
123
|
ctx.register_hook("on_session_end", on_session_end)
|
|
58
124
|
ctx.register_hook("on_session_finalize", on_session_finalize)
|
|
59
125
|
ctx.register_hook("subagent_start", subagent_start)
|
|
126
|
+
# The CLI's current session, when Hermes already knows it at load.
|
|
127
|
+
current = os.environ.get("HERMES_SESSION_ID")
|
|
128
|
+
if current:
|
|
129
|
+
bus._spawn(lambda: bus.session_open(current))
|
|
130
|
+
# A gateway process: link the chats it served before a restart once the gateway is live (Hermes
|
|
131
|
+
# publishes its injector then). Nowhere else: a CLI process must not take a gateway's roots.
|
|
132
|
+
ready = gateway_ready or (lambda: bool(getattr(getattr(ctx, "_manager", None), "has_gateway_message_injector", False)))
|
|
133
|
+
if gateway_ready is not None or hasattr(ctx, "_manager"):
|
|
134
|
+
bus._spawn(lambda: _restore_when_live(bus, ready))
|
|
60
135
|
atexit.register(bus.close_all)
|
|
61
136
|
# A reload unloads the plugin without exiting: shut this Bus then, so the next one can take over
|
|
62
137
|
# the proof wiring. An older Hermes has no on_unload.
|
package/assets/oa_bus/roots.py
CHANGED
|
@@ -79,6 +79,32 @@ def hermes_session_title(root_ref, db=None):
|
|
|
79
79
|
return None
|
|
80
80
|
|
|
81
81
|
|
|
82
|
+
def hermes_session_exists(session_id):
|
|
83
|
+
"""Whether Hermes's history still has this session, and it has not ended (read-only SessionDB).
|
|
84
|
+
|
|
85
|
+
True when Hermes cannot be asked (no API here): the caller's own age limit then decides."""
|
|
86
|
+
try:
|
|
87
|
+
from hermes_constants import get_hermes_home
|
|
88
|
+
from hermes_state import SessionDB
|
|
89
|
+
except Exception:
|
|
90
|
+
return True
|
|
91
|
+
try:
|
|
92
|
+
path = get_hermes_home() / "state.db"
|
|
93
|
+
if not path.exists():
|
|
94
|
+
return False
|
|
95
|
+
db = _dbs.get(path)
|
|
96
|
+
if db is None:
|
|
97
|
+
db = _dbs[path] = SessionDB(path, read_only=True)
|
|
98
|
+
# A compression rotates the id: the newest session of the lineage is the live one.
|
|
99
|
+
lineage = db.get_compression_lineage(session_id)
|
|
100
|
+
tip = lineage[-1] if lineage else session_id
|
|
101
|
+
row = db.get_session(tip)
|
|
102
|
+
# An idle, daily or compression reset ends the row (ended_at) without on_session_finalize.
|
|
103
|
+
return row is not None and not row.get("ended_at")
|
|
104
|
+
except Exception:
|
|
105
|
+
return True
|
|
106
|
+
|
|
107
|
+
|
|
82
108
|
class Roots:
|
|
83
109
|
def __init__(self, lineage_root=hermes_lineage_root):
|
|
84
110
|
self._lineage_root = lineage_root
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""The gateway roots this plugin has linked, kept across a gateway restart.
|
|
2
|
+
|
|
3
|
+
A messaging chat's session survives a restart with the same session id and key, but its first
|
|
4
|
+
turn is the only thing that opens a link. So each root's (session id, key, platform, last turn)
|
|
5
|
+
is kept in a small owner-only file next to the plugin, and the roots active in the last 7 days
|
|
6
|
+
are linked again when the plugin loads in a gateway process.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import stat
|
|
12
|
+
import tempfile
|
|
13
|
+
import threading
|
|
14
|
+
import time
|
|
15
|
+
|
|
16
|
+
STORE_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "gateway_roots.json")
|
|
17
|
+
MAX_ROOTS = 20
|
|
18
|
+
MAX_AGE_S = 7 * 24 * 3600
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class RootStore:
|
|
22
|
+
def __init__(self, path=None, clock=time.time):
|
|
23
|
+
self.path = path or STORE_FILE
|
|
24
|
+
self._clock = clock
|
|
25
|
+
self._lock = threading.Lock()
|
|
26
|
+
|
|
27
|
+
def _read(self):
|
|
28
|
+
try:
|
|
29
|
+
# Only our own owner-only regular file: anything else is not ours to trust.
|
|
30
|
+
st = os.lstat(self.path)
|
|
31
|
+
if not stat.S_ISREG(st.st_mode) or st.st_mode & 0o077:
|
|
32
|
+
return {}
|
|
33
|
+
if hasattr(os, "getuid") and st.st_uid != os.getuid():
|
|
34
|
+
return {}
|
|
35
|
+
with open(self.path, encoding="utf-8") as f:
|
|
36
|
+
data = json.load(f)
|
|
37
|
+
except (OSError, ValueError):
|
|
38
|
+
return {}
|
|
39
|
+
if not isinstance(data, dict):
|
|
40
|
+
return {}
|
|
41
|
+
return {
|
|
42
|
+
k: v for k, v in data.items()
|
|
43
|
+
if isinstance(k, str) and isinstance(v, dict)
|
|
44
|
+
and all(isinstance(v.get(f), str) and v[f] for f in ("session_id", "key", "platform", "chat_type"))
|
|
45
|
+
and isinstance(v.get("last_turn"), (int, float))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
def _write(self, data):
|
|
49
|
+
if not hasattr(os, "fchmod"):
|
|
50
|
+
raise OSError("cannot make the file owner-only here")
|
|
51
|
+
d = os.path.dirname(self.path)
|
|
52
|
+
fd, tmp = tempfile.mkstemp(dir=d, prefix=".roots-")
|
|
53
|
+
try:
|
|
54
|
+
os.fchmod(fd, 0o600)
|
|
55
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
56
|
+
json.dump(data, f)
|
|
57
|
+
os.replace(tmp, self.path)
|
|
58
|
+
except BaseException:
|
|
59
|
+
try:
|
|
60
|
+
os.unlink(tmp)
|
|
61
|
+
except OSError:
|
|
62
|
+
pass
|
|
63
|
+
raise
|
|
64
|
+
|
|
65
|
+
def upsert(self, root, session_id, key, platform, chat_type):
|
|
66
|
+
"""Never raises: a store that cannot be written only costs the restart restore."""
|
|
67
|
+
try:
|
|
68
|
+
with self._lock:
|
|
69
|
+
data = self._read()
|
|
70
|
+
# One key is one chat: another root stored under it is the old one.
|
|
71
|
+
for other in [r for r, e in data.items() if e["key"] == key and r != root]:
|
|
72
|
+
del data[other]
|
|
73
|
+
data[root] = {"session_id": session_id, "key": key, "platform": platform,
|
|
74
|
+
"chat_type": chat_type, "last_turn": self._clock()}
|
|
75
|
+
if len(data) > MAX_ROOTS:
|
|
76
|
+
for old in sorted(data, key=lambda r: data[r]["last_turn"])[: len(data) - MAX_ROOTS]:
|
|
77
|
+
del data[old]
|
|
78
|
+
self._write(data)
|
|
79
|
+
except Exception:
|
|
80
|
+
pass
|
|
81
|
+
|
|
82
|
+
def drop(self, root):
|
|
83
|
+
try:
|
|
84
|
+
with self._lock:
|
|
85
|
+
data = self._read()
|
|
86
|
+
if data.pop(root, None) is not None:
|
|
87
|
+
self._write(data)
|
|
88
|
+
except Exception:
|
|
89
|
+
pass
|
|
90
|
+
|
|
91
|
+
def recent(self):
|
|
92
|
+
"""[(root, entry)] active in the last 7 days, newest first, at most MAX_ROOTS."""
|
|
93
|
+
with self._lock:
|
|
94
|
+
data = self._read()
|
|
95
|
+
now = self._clock()
|
|
96
|
+
live = [(r, e) for r, e in data.items() if 0 <= now - e["last_turn"] <= MAX_AGE_S]
|
|
97
|
+
live.sort(key=lambda p: -p[1]["last_turn"])
|
|
98
|
+
return live[:MAX_ROOTS]
|
package/assets/openclaw/index.js
CHANGED
|
@@ -27744,7 +27744,7 @@ var base2 = { v: external_exports.literal(V), id: external_exports.string().min(
|
|
|
27744
27744
|
var late = { late: external_exports.array(external_exports.string().min(1)).optional() };
|
|
27745
27745
|
var isWellFormed2 = (s) => s.isWellFormed();
|
|
27746
27746
|
var WELL_FORMED = { message: "text is not well-formed unicode" };
|
|
27747
|
-
var CLIENT_VERSION = true ? "0.
|
|
27747
|
+
var CLIENT_VERSION = true ? "0.199.1" : "0.0.0";
|
|
27748
27748
|
var clientVersion = {
|
|
27749
27749
|
client_version: external_exports.string().min(1).max(64).optional()
|
|
27750
27750
|
};
|
package/dist/index.js
CHANGED
|
@@ -32729,7 +32729,7 @@ var init_frames = __esm({
|
|
|
32729
32729
|
late = { late: external_exports.array(external_exports.string().min(1)).optional() };
|
|
32730
32730
|
isWellFormed2 = (s) => s.isWellFormed();
|
|
32731
32731
|
WELL_FORMED = { message: "text is not well-formed unicode" };
|
|
32732
|
-
CLIENT_VERSION = true ? "0.
|
|
32732
|
+
CLIENT_VERSION = true ? "0.199.1" : "0.0.0";
|
|
32733
32733
|
clientVersion = {
|
|
32734
32734
|
client_version: external_exports.string().min(1).max(64).optional()
|
|
32735
32735
|
};
|
|
@@ -57446,7 +57446,7 @@ var noService = {
|
|
|
57446
57446
|
remove: async () => ({}),
|
|
57447
57447
|
cli: () => ["oa-bus"]
|
|
57448
57448
|
};
|
|
57449
|
-
var VERSION = true ? "0.
|
|
57449
|
+
var VERSION = true ? "0.199.1" : JSON.parse(
|
|
57450
57450
|
readFileSync21(new URL("../package.json", import.meta.url), "utf8")
|
|
57451
57451
|
).version;
|
|
57452
57452
|
var which = (cmd) => {
|
|
@@ -59340,6 +59340,7 @@ var HERMES_PLUGIN_NAME = "oa-bus";
|
|
|
59340
59340
|
var HERMES_MARKER = ".oa-bus-managed";
|
|
59341
59341
|
var HERMES_NO_CLI = "no hermes binary found; its config may still list oa-bus";
|
|
59342
59342
|
var HERMES_HABITS_MARKER = "habits.on";
|
|
59343
|
+
var HERMES_ROOTS_FILE = "gateway_roots.json";
|
|
59343
59344
|
var HERMES_STAGING = ".oa-bus-staging";
|
|
59344
59345
|
var HABITS_MARKER_TEXT = "The Open Artifacts bus habits are on for this plugin. Remove: oa-bus install --no-habits\n";
|
|
59345
59346
|
var real2 = (p) => {
|
|
@@ -59498,7 +59499,7 @@ var kind = (path) => {
|
|
|
59498
59499
|
function files(dir, rel = "") {
|
|
59499
59500
|
const out = [];
|
|
59500
59501
|
for (const name of readdirSync8(join28(dir, rel)).sort()) {
|
|
59501
|
-
if (name === "__pycache__" || name.startsWith(".") || name.endsWith(".pyc") || rel === "" && name === HERMES_HABITS_MARKER)
|
|
59502
|
+
if (name === "__pycache__" || name.startsWith(".") || name.endsWith(".pyc") || rel === "" && (name === HERMES_HABITS_MARKER || name === HERMES_ROOTS_FILE))
|
|
59502
59503
|
continue;
|
|
59503
59504
|
const r = rel ? `${rel}/${name}` : name;
|
|
59504
59505
|
if (kind(join28(dir, r)) === "dir") out.push(...files(dir, r));
|
|
@@ -59561,6 +59562,7 @@ async function installHermes(deps2) {
|
|
|
59561
59562
|
);
|
|
59562
59563
|
if (state !== "current") {
|
|
59563
59564
|
const keepHabits = state === "stale" && kind(join28(dest, HERMES_HABITS_MARKER)) === "file";
|
|
59565
|
+
const keepRoots = state === "stale" && kind(join28(dest, HERMES_ROOTS_FILE)) === "file" ? readFileSync24(join28(dest, HERMES_ROOTS_FILE)) : void 0;
|
|
59564
59566
|
const plugins2 = join28(dest, "..");
|
|
59565
59567
|
mkdirSync12(plugins2, { recursive: true });
|
|
59566
59568
|
const staging = join28(plugins2, "..", HERMES_STAGING);
|
|
@@ -59586,6 +59588,8 @@ async function installHermes(deps2) {
|
|
|
59586
59588
|
);
|
|
59587
59589
|
if (keepHabits)
|
|
59588
59590
|
writeFileSync16(join28(tmp, HERMES_HABITS_MARKER), HABITS_MARKER_TEXT);
|
|
59591
|
+
if (keepRoots !== void 0)
|
|
59592
|
+
writeFileSync16(join28(tmp, HERMES_ROOTS_FILE), keepRoots, { mode: 384 });
|
|
59589
59593
|
if (state === "stale") rmSync13(dest, { recursive: true, force: true });
|
|
59590
59594
|
renameSync17(tmp, dest);
|
|
59591
59595
|
} catch (e) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifacts/bus",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.199.1",
|
|
5
5
|
"description": "The Open Artifacts bus: your AI agents reach each other across machines.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
"typecheck": "tsc --noEmit"
|
|
30
30
|
},
|
|
31
31
|
"oaBus": {
|
|
32
|
-
"commit": "
|
|
32
|
+
"commit": "038e40331a282ed541626b4e7a99d26329a02fca"
|
|
33
33
|
}
|
|
34
34
|
}
|