nodus-router 0.1.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Shawn Knight
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,186 @@
1
+ Metadata-Version: 2.4
2
+ Name: nodus-router
3
+ Version: 0.1.0
4
+ Summary: Route inbound context to agent + session scope via fnmatch binding rules
5
+ Author: Shawn Knight
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Masterplanner25/nodus-router
8
+ Project-URL: Repository, https://github.com/Masterplanner25/nodus-router
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Provides-Extra: session
13
+ Requires-Dist: nodus-session>=0.1.0; extra == "session"
14
+ Provides-Extra: dev
15
+ Requires-Dist: pytest>=8.0; extra == "dev"
16
+ Requires-Dist: nodus-session>=0.1.0; extra == "dev"
17
+ Dynamic: license-file
18
+
19
+ # nodus-router
20
+
21
+ **Route inbound context to agent + session scope via fnmatch binding rules.**
22
+
23
+ Maps channel/peer/thread patterns to agent IDs with priority-ordered binding
24
+ rules. Resolves inbound messages to a `RouteMatch` containing the agent ID,
25
+ a `SessionKey`, and the matched binding. No required external dependencies
26
+ — pure stdlib with optional `nodus-session` integration.
27
+
28
+ > **Status:** v0.1.0 — prepared, not yet published.
29
+
30
+ ---
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ pip install nodus-router
36
+
37
+ # With nodus-session SessionKey integration:
38
+ pip install "nodus-router[session]"
39
+ ```
40
+
41
+ ---
42
+
43
+ ## What it provides
44
+
45
+ | Component | Purpose |
46
+ |---|---|
47
+ | `InboundContext` | Inbound message context: channel, peer, thread, roles, metadata |
48
+ | `RouteBinding` | fnmatch pattern → agent_id mapping with priority |
49
+ | `RoutingTable` | Thread-safe ordered list of `RouteBinding` objects |
50
+ | `RouteResolver` | Evaluates table, returns `RouteMatch` (with default fallback) |
51
+ | `RouteMatch` | Resolved agent_id, `SessionKey`, and matched binding |
52
+
53
+ ---
54
+
55
+ ## Quick start
56
+
57
+ ```python
58
+ from nodus_router import InboundContext, RouteBinding, RoutingTable, RouteResolver
59
+
60
+ table = RoutingTable()
61
+ table.add(RouteBinding(
62
+ channel_pattern="slack",
63
+ peer_pattern="*",
64
+ agent_id="general-agent",
65
+ priority=10,
66
+ ))
67
+ table.add(RouteBinding(
68
+ channel_pattern="slack",
69
+ peer_pattern="U123*",
70
+ agent_id="vip-agent",
71
+ priority=20, # higher priority wins
72
+ ))
73
+
74
+ resolver = RouteResolver(table, default_agent_id="fallback-agent")
75
+
76
+ ctx = InboundContext(channel_id="slack", peer_id="U123456", thread_id=None)
77
+ match = resolver.resolve(ctx)
78
+
79
+ print(match.agent_id) # "vip-agent"
80
+ print(match.session_key) # SessionKey(agent_id="vip-agent", channel="slack", ...)
81
+ print(match.binding) # the matched RouteBinding
82
+ ```
83
+
84
+ ---
85
+
86
+ ## InboundContext
87
+
88
+ ```python
89
+ from nodus_router import InboundContext
90
+
91
+ ctx = InboundContext(
92
+ channel_id="slack",
93
+ peer_id="U123456",
94
+ thread_id="T789", # optional
95
+ roles=["user"], # optional list of role strings
96
+ metadata={}, # optional extra data
97
+ )
98
+ ```
99
+
100
+ ---
101
+
102
+ ## RouteBinding
103
+
104
+ ```python
105
+ from nodus_router import RouteBinding
106
+
107
+ binding = RouteBinding(
108
+ channel_pattern="slack", # fnmatch — "*" matches any channel
109
+ peer_pattern="U123*", # fnmatch — matches any peer starting with U123
110
+ agent_id="vip-agent",
111
+ priority=20, # higher priority evaluated first
112
+ )
113
+ ```
114
+
115
+ Patterns use `fnmatch` — `*` matches any sequence, `?` matches one character.
116
+
117
+ ---
118
+
119
+ ## RoutingTable
120
+
121
+ ```python
122
+ from nodus_router import RoutingTable
123
+
124
+ table = RoutingTable()
125
+ table.add(binding)
126
+ table.remove(agent_id="vip-agent")
127
+ table.list_bindings() # list sorted by priority descending
128
+ len(table)
129
+ ```
130
+
131
+ Thread-safe — safe to add/remove from multiple threads.
132
+
133
+ ---
134
+
135
+ ## RouteResolver
136
+
137
+ ```python
138
+ from nodus_router import RouteResolver
139
+
140
+ resolver = RouteResolver(
141
+ table,
142
+ default_agent_id="fallback-agent", # used when no binding matches
143
+ )
144
+
145
+ match = resolver.resolve(ctx)
146
+ # match.agent_id — resolved agent
147
+ # match.session_key — SessionKey (from nodus-session if installed, else fallback)
148
+ # match.binding — matched RouteBinding | None (None = default fallback)
149
+ ```
150
+
151
+ ---
152
+
153
+ ## SessionKey integration
154
+
155
+ When `nodus-session` is installed, `RouteMatch.session_key` is a real
156
+ `SessionKey` from that package. Without it, a lightweight fallback dataclass
157
+ with the same fields is used — no functionality is lost.
158
+
159
+ ```bash
160
+ pip install "nodus-router[session]"
161
+ ```
162
+
163
+ ---
164
+
165
+ ## Design
166
+
167
+ - **No required dependencies.** `fnmatch` and `threading` are stdlib.
168
+ `nodus-session` is optional — a fallback `SessionKey` is used when absent.
169
+ - **Priority ordering.** Bindings are sorted by `priority` descending;
170
+ first match wins.
171
+ - **Thread-safe.** `RoutingTable` uses `threading.Lock`.
172
+
173
+ ---
174
+
175
+ ## Development
176
+
177
+ ```bash
178
+ pip install -e ".[dev]"
179
+ pytest tests/ -q
180
+ ```
181
+
182
+ ---
183
+
184
+ ## License
185
+
186
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,168 @@
1
+ # nodus-router
2
+
3
+ **Route inbound context to agent + session scope via fnmatch binding rules.**
4
+
5
+ Maps channel/peer/thread patterns to agent IDs with priority-ordered binding
6
+ rules. Resolves inbound messages to a `RouteMatch` containing the agent ID,
7
+ a `SessionKey`, and the matched binding. No required external dependencies
8
+ — pure stdlib with optional `nodus-session` integration.
9
+
10
+ > **Status:** v0.1.0 — prepared, not yet published.
11
+
12
+ ---
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ pip install nodus-router
18
+
19
+ # With nodus-session SessionKey integration:
20
+ pip install "nodus-router[session]"
21
+ ```
22
+
23
+ ---
24
+
25
+ ## What it provides
26
+
27
+ | Component | Purpose |
28
+ |---|---|
29
+ | `InboundContext` | Inbound message context: channel, peer, thread, roles, metadata |
30
+ | `RouteBinding` | fnmatch pattern → agent_id mapping with priority |
31
+ | `RoutingTable` | Thread-safe ordered list of `RouteBinding` objects |
32
+ | `RouteResolver` | Evaluates table, returns `RouteMatch` (with default fallback) |
33
+ | `RouteMatch` | Resolved agent_id, `SessionKey`, and matched binding |
34
+
35
+ ---
36
+
37
+ ## Quick start
38
+
39
+ ```python
40
+ from nodus_router import InboundContext, RouteBinding, RoutingTable, RouteResolver
41
+
42
+ table = RoutingTable()
43
+ table.add(RouteBinding(
44
+ channel_pattern="slack",
45
+ peer_pattern="*",
46
+ agent_id="general-agent",
47
+ priority=10,
48
+ ))
49
+ table.add(RouteBinding(
50
+ channel_pattern="slack",
51
+ peer_pattern="U123*",
52
+ agent_id="vip-agent",
53
+ priority=20, # higher priority wins
54
+ ))
55
+
56
+ resolver = RouteResolver(table, default_agent_id="fallback-agent")
57
+
58
+ ctx = InboundContext(channel_id="slack", peer_id="U123456", thread_id=None)
59
+ match = resolver.resolve(ctx)
60
+
61
+ print(match.agent_id) # "vip-agent"
62
+ print(match.session_key) # SessionKey(agent_id="vip-agent", channel="slack", ...)
63
+ print(match.binding) # the matched RouteBinding
64
+ ```
65
+
66
+ ---
67
+
68
+ ## InboundContext
69
+
70
+ ```python
71
+ from nodus_router import InboundContext
72
+
73
+ ctx = InboundContext(
74
+ channel_id="slack",
75
+ peer_id="U123456",
76
+ thread_id="T789", # optional
77
+ roles=["user"], # optional list of role strings
78
+ metadata={}, # optional extra data
79
+ )
80
+ ```
81
+
82
+ ---
83
+
84
+ ## RouteBinding
85
+
86
+ ```python
87
+ from nodus_router import RouteBinding
88
+
89
+ binding = RouteBinding(
90
+ channel_pattern="slack", # fnmatch — "*" matches any channel
91
+ peer_pattern="U123*", # fnmatch — matches any peer starting with U123
92
+ agent_id="vip-agent",
93
+ priority=20, # higher priority evaluated first
94
+ )
95
+ ```
96
+
97
+ Patterns use `fnmatch` — `*` matches any sequence, `?` matches one character.
98
+
99
+ ---
100
+
101
+ ## RoutingTable
102
+
103
+ ```python
104
+ from nodus_router import RoutingTable
105
+
106
+ table = RoutingTable()
107
+ table.add(binding)
108
+ table.remove(agent_id="vip-agent")
109
+ table.list_bindings() # list sorted by priority descending
110
+ len(table)
111
+ ```
112
+
113
+ Thread-safe — safe to add/remove from multiple threads.
114
+
115
+ ---
116
+
117
+ ## RouteResolver
118
+
119
+ ```python
120
+ from nodus_router import RouteResolver
121
+
122
+ resolver = RouteResolver(
123
+ table,
124
+ default_agent_id="fallback-agent", # used when no binding matches
125
+ )
126
+
127
+ match = resolver.resolve(ctx)
128
+ # match.agent_id — resolved agent
129
+ # match.session_key — SessionKey (from nodus-session if installed, else fallback)
130
+ # match.binding — matched RouteBinding | None (None = default fallback)
131
+ ```
132
+
133
+ ---
134
+
135
+ ## SessionKey integration
136
+
137
+ When `nodus-session` is installed, `RouteMatch.session_key` is a real
138
+ `SessionKey` from that package. Without it, a lightweight fallback dataclass
139
+ with the same fields is used — no functionality is lost.
140
+
141
+ ```bash
142
+ pip install "nodus-router[session]"
143
+ ```
144
+
145
+ ---
146
+
147
+ ## Design
148
+
149
+ - **No required dependencies.** `fnmatch` and `threading` are stdlib.
150
+ `nodus-session` is optional — a fallback `SessionKey` is used when absent.
151
+ - **Priority ordering.** Bindings are sorted by `priority` descending;
152
+ first match wins.
153
+ - **Thread-safe.** `RoutingTable` uses `threading.Lock`.
154
+
155
+ ---
156
+
157
+ ## Development
158
+
159
+ ```bash
160
+ pip install -e ".[dev]"
161
+ pytest tests/ -q
162
+ ```
163
+
164
+ ---
165
+
166
+ ## License
167
+
168
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,27 @@
1
+ """nodus-router — route inbound context to agent + session scope.
2
+
3
+ Context:
4
+ InboundContext — channel_id, peer_id, thread_id, roles, metadata
5
+
6
+ Binding:
7
+ RouteBinding — fnmatch patterns mapped to an agent_id + priority
8
+
9
+ Table:
10
+ RoutingTable — thread-safe ordered list of RouteBinding objects
11
+
12
+ Resolution:
13
+ RouteMatch — resolved agent_id + SessionKey + matching binding
14
+ RouteResolver — evaluate table, return RouteMatch (with default fallback)
15
+ """
16
+ from .binding import RouteBinding
17
+ from .context import InboundContext
18
+ from .resolver import RouteMatch, RouteResolver
19
+ from .table import RoutingTable
20
+
21
+ __all__ = [
22
+ "InboundContext",
23
+ "RouteBinding",
24
+ "RoutingTable",
25
+ "RouteMatch",
26
+ "RouteResolver",
27
+ ]
@@ -0,0 +1,52 @@
1
+ """RouteBinding — fnmatch pattern matching on inbound context fields."""
2
+ from __future__ import annotations
3
+
4
+ import fnmatch
5
+ from dataclasses import dataclass
6
+
7
+ from .context import InboundContext
8
+
9
+
10
+ @dataclass
11
+ class RouteBinding:
12
+ """One routing rule that maps a context pattern to an agent.
13
+
14
+ All patterns use :func:`fnmatch.fnmatch` semantics:
15
+ ``"*"`` matches anything, ``"slack"`` matches exactly, ``"sl*"`` matches
16
+ any string starting with ``"sl"``.
17
+
18
+ Attributes
19
+ ----------
20
+ agent_id: Target agent when this binding matches.
21
+ channel_pattern: Pattern matched against ``InboundContext.channel_id``.
22
+ peer_pattern: Pattern matched against ``InboundContext.peer_id``.
23
+ role_pattern: Pattern matched against any item in ``InboundContext.roles``.
24
+ thread_pattern: Pattern matched against ``InboundContext.thread_id``.
25
+ priority: Lower values are evaluated first in the routing table.
26
+ """
27
+
28
+ agent_id: str
29
+ channel_pattern: str = "*"
30
+ peer_pattern: str = "*"
31
+ role_pattern: str = "*"
32
+ thread_pattern: str = "*"
33
+ priority: int = 0
34
+
35
+ def matches(self, ctx: InboundContext) -> bool:
36
+ """Return True if *ctx* satisfies all pattern constraints."""
37
+ if not fnmatch.fnmatch(ctx.channel_id, self.channel_pattern):
38
+ return False
39
+
40
+ if self.peer_pattern != "*":
41
+ if not ctx.peer_id or not fnmatch.fnmatch(ctx.peer_id, self.peer_pattern):
42
+ return False
43
+
44
+ if self.role_pattern != "*":
45
+ if not any(fnmatch.fnmatch(role, self.role_pattern) for role in ctx.roles):
46
+ return False
47
+
48
+ if self.thread_pattern != "*":
49
+ if not ctx.thread_id or not fnmatch.fnmatch(ctx.thread_id, self.thread_pattern):
50
+ return False
51
+
52
+ return True
@@ -0,0 +1,25 @@
1
+ """InboundContext — what routing evaluates to find the right agent+session."""
2
+ from __future__ import annotations
3
+
4
+ from dataclasses import dataclass, field
5
+ from typing import Any
6
+
7
+
8
+ @dataclass
9
+ class InboundContext:
10
+ """Describes an inbound message or event that needs to be routed.
11
+
12
+ Attributes
13
+ ----------
14
+ channel_id: The channel the message arrived on (e.g. ``"slack"``).
15
+ peer_id: Platform-native sender ID (e.g. ``"U0123456"``).
16
+ thread_id: Thread or topic ID (for threaded channels).
17
+ roles: Sender's roles or groups (e.g. Discord guild roles).
18
+ metadata: Additional routing hints (account ID, guild ID, etc.).
19
+ """
20
+
21
+ channel_id: str
22
+ peer_id: str | None = None
23
+ thread_id: str | None = None
24
+ roles: list[str] = field(default_factory=list)
25
+ metadata: dict[str, Any] = field(default_factory=dict)
@@ -0,0 +1,98 @@
1
+ """RouteResolver — evaluate a RoutingTable and return a RouteMatch."""
2
+ from __future__ import annotations
3
+
4
+ from dataclasses import dataclass
5
+
6
+ from .binding import RouteBinding
7
+ from .context import InboundContext
8
+ from .table import RoutingTable
9
+
10
+ try:
11
+ from nodus_session import SessionKey as _SessionKey
12
+ except ImportError:
13
+ from dataclasses import dataclass as _dc # type: ignore[assignment]
14
+
15
+ @_dc
16
+ class _SessionKey: # type: ignore[no-redef]
17
+ agent_id: str
18
+ channel: str | None = None
19
+ peer: str | None = None
20
+ thread: str | None = None
21
+
22
+ def to_string(self):
23
+ parts = [self.agent_id]
24
+ if self.channel and self.peer:
25
+ parts.append(f"channel:{self.channel}")
26
+ parts.append(f"peer:{self.peer}")
27
+ return "!".join(parts)
28
+
29
+ @classmethod
30
+ def from_string(cls, raw):
31
+ return cls(agent_id=raw)
32
+
33
+
34
+ @dataclass
35
+ class RouteMatch:
36
+ """The result of route resolution.
37
+
38
+ Attributes
39
+ ----------
40
+ agent_id: The agent that should handle this context.
41
+ session_key: The session key for this agent + context combination.
42
+ binding: The ``RouteBinding`` that produced this match.
43
+ """
44
+
45
+ agent_id: str
46
+ session_key: "_SessionKey"
47
+ binding: RouteBinding
48
+
49
+
50
+ def _build_session_key(agent_id: str, ctx: InboundContext) -> "_SessionKey":
51
+ """Build a SessionKey from a route match and its context."""
52
+ return _SessionKey(
53
+ agent_id=agent_id,
54
+ channel=ctx.channel_id,
55
+ peer=ctx.peer_id,
56
+ thread=ctx.thread_id,
57
+ )
58
+
59
+
60
+ class RouteResolver:
61
+ """Evaluate the routing table and return the first match.
62
+
63
+ Args:
64
+ table: The ordered routing table.
65
+ default_agent_id: Agent to use when no binding matches (default: ``"main"``).
66
+ """
67
+
68
+ def __init__(
69
+ self,
70
+ table: RoutingTable,
71
+ default_agent_id: str = "main",
72
+ ) -> None:
73
+ self._table = table
74
+ self._default_agent_id = default_agent_id
75
+
76
+ def resolve(self, ctx: InboundContext) -> RouteMatch:
77
+ """Return the first matching ``RouteMatch`` for *ctx*.
78
+
79
+ Falls back to *default_agent_id* when no binding matches.
80
+ """
81
+ for binding in self._table.bindings():
82
+ if binding.matches(ctx):
83
+ return RouteMatch(
84
+ agent_id=binding.agent_id,
85
+ session_key=_build_session_key(binding.agent_id, ctx),
86
+ binding=binding,
87
+ )
88
+
89
+ # Default fallback
90
+ default_binding = RouteBinding(
91
+ agent_id=self._default_agent_id,
92
+ priority=999,
93
+ )
94
+ return RouteMatch(
95
+ agent_id=self._default_agent_id,
96
+ session_key=_build_session_key(self._default_agent_id, ctx),
97
+ binding=default_binding,
98
+ )
@@ -0,0 +1,39 @@
1
+ """RoutingTable — ordered list of RouteBinding objects."""
2
+ from __future__ import annotations
3
+
4
+ import threading
5
+
6
+ from .binding import RouteBinding
7
+
8
+
9
+ class RoutingTable:
10
+ """Thread-safe ordered list of ``RouteBinding`` objects.
11
+
12
+ Bindings are sorted by ``priority`` (ascending) and evaluated in order.
13
+ """
14
+
15
+ def __init__(self) -> None:
16
+ self._bindings: list[RouteBinding] = []
17
+ self._lock = threading.Lock()
18
+
19
+ def add(self, binding: RouteBinding) -> None:
20
+ """Add *binding*, maintaining sort by priority."""
21
+ with self._lock:
22
+ self._bindings.append(binding)
23
+ self._bindings.sort(key=lambda b: b.priority)
24
+
25
+ def remove(self, agent_id: str) -> int:
26
+ """Remove all bindings for *agent_id*. Returns count removed."""
27
+ with self._lock:
28
+ before = len(self._bindings)
29
+ self._bindings = [b for b in self._bindings if b.agent_id != agent_id]
30
+ return before - len(self._bindings)
31
+
32
+ def bindings(self) -> list[RouteBinding]:
33
+ """Return a copy of the ordered binding list."""
34
+ with self._lock:
35
+ return list(self._bindings)
36
+
37
+ def __len__(self) -> int:
38
+ with self._lock:
39
+ return len(self._bindings)
@@ -0,0 +1,186 @@
1
+ Metadata-Version: 2.4
2
+ Name: nodus-router
3
+ Version: 0.1.0
4
+ Summary: Route inbound context to agent + session scope via fnmatch binding rules
5
+ Author: Shawn Knight
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Masterplanner25/nodus-router
8
+ Project-URL: Repository, https://github.com/Masterplanner25/nodus-router
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Provides-Extra: session
13
+ Requires-Dist: nodus-session>=0.1.0; extra == "session"
14
+ Provides-Extra: dev
15
+ Requires-Dist: pytest>=8.0; extra == "dev"
16
+ Requires-Dist: nodus-session>=0.1.0; extra == "dev"
17
+ Dynamic: license-file
18
+
19
+ # nodus-router
20
+
21
+ **Route inbound context to agent + session scope via fnmatch binding rules.**
22
+
23
+ Maps channel/peer/thread patterns to agent IDs with priority-ordered binding
24
+ rules. Resolves inbound messages to a `RouteMatch` containing the agent ID,
25
+ a `SessionKey`, and the matched binding. No required external dependencies
26
+ — pure stdlib with optional `nodus-session` integration.
27
+
28
+ > **Status:** v0.1.0 — prepared, not yet published.
29
+
30
+ ---
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ pip install nodus-router
36
+
37
+ # With nodus-session SessionKey integration:
38
+ pip install "nodus-router[session]"
39
+ ```
40
+
41
+ ---
42
+
43
+ ## What it provides
44
+
45
+ | Component | Purpose |
46
+ |---|---|
47
+ | `InboundContext` | Inbound message context: channel, peer, thread, roles, metadata |
48
+ | `RouteBinding` | fnmatch pattern → agent_id mapping with priority |
49
+ | `RoutingTable` | Thread-safe ordered list of `RouteBinding` objects |
50
+ | `RouteResolver` | Evaluates table, returns `RouteMatch` (with default fallback) |
51
+ | `RouteMatch` | Resolved agent_id, `SessionKey`, and matched binding |
52
+
53
+ ---
54
+
55
+ ## Quick start
56
+
57
+ ```python
58
+ from nodus_router import InboundContext, RouteBinding, RoutingTable, RouteResolver
59
+
60
+ table = RoutingTable()
61
+ table.add(RouteBinding(
62
+ channel_pattern="slack",
63
+ peer_pattern="*",
64
+ agent_id="general-agent",
65
+ priority=10,
66
+ ))
67
+ table.add(RouteBinding(
68
+ channel_pattern="slack",
69
+ peer_pattern="U123*",
70
+ agent_id="vip-agent",
71
+ priority=20, # higher priority wins
72
+ ))
73
+
74
+ resolver = RouteResolver(table, default_agent_id="fallback-agent")
75
+
76
+ ctx = InboundContext(channel_id="slack", peer_id="U123456", thread_id=None)
77
+ match = resolver.resolve(ctx)
78
+
79
+ print(match.agent_id) # "vip-agent"
80
+ print(match.session_key) # SessionKey(agent_id="vip-agent", channel="slack", ...)
81
+ print(match.binding) # the matched RouteBinding
82
+ ```
83
+
84
+ ---
85
+
86
+ ## InboundContext
87
+
88
+ ```python
89
+ from nodus_router import InboundContext
90
+
91
+ ctx = InboundContext(
92
+ channel_id="slack",
93
+ peer_id="U123456",
94
+ thread_id="T789", # optional
95
+ roles=["user"], # optional list of role strings
96
+ metadata={}, # optional extra data
97
+ )
98
+ ```
99
+
100
+ ---
101
+
102
+ ## RouteBinding
103
+
104
+ ```python
105
+ from nodus_router import RouteBinding
106
+
107
+ binding = RouteBinding(
108
+ channel_pattern="slack", # fnmatch — "*" matches any channel
109
+ peer_pattern="U123*", # fnmatch — matches any peer starting with U123
110
+ agent_id="vip-agent",
111
+ priority=20, # higher priority evaluated first
112
+ )
113
+ ```
114
+
115
+ Patterns use `fnmatch` — `*` matches any sequence, `?` matches one character.
116
+
117
+ ---
118
+
119
+ ## RoutingTable
120
+
121
+ ```python
122
+ from nodus_router import RoutingTable
123
+
124
+ table = RoutingTable()
125
+ table.add(binding)
126
+ table.remove(agent_id="vip-agent")
127
+ table.list_bindings() # list sorted by priority descending
128
+ len(table)
129
+ ```
130
+
131
+ Thread-safe — safe to add/remove from multiple threads.
132
+
133
+ ---
134
+
135
+ ## RouteResolver
136
+
137
+ ```python
138
+ from nodus_router import RouteResolver
139
+
140
+ resolver = RouteResolver(
141
+ table,
142
+ default_agent_id="fallback-agent", # used when no binding matches
143
+ )
144
+
145
+ match = resolver.resolve(ctx)
146
+ # match.agent_id — resolved agent
147
+ # match.session_key — SessionKey (from nodus-session if installed, else fallback)
148
+ # match.binding — matched RouteBinding | None (None = default fallback)
149
+ ```
150
+
151
+ ---
152
+
153
+ ## SessionKey integration
154
+
155
+ When `nodus-session` is installed, `RouteMatch.session_key` is a real
156
+ `SessionKey` from that package. Without it, a lightweight fallback dataclass
157
+ with the same fields is used — no functionality is lost.
158
+
159
+ ```bash
160
+ pip install "nodus-router[session]"
161
+ ```
162
+
163
+ ---
164
+
165
+ ## Design
166
+
167
+ - **No required dependencies.** `fnmatch` and `threading` are stdlib.
168
+ `nodus-session` is optional — a fallback `SessionKey` is used when absent.
169
+ - **Priority ordering.** Bindings are sorted by `priority` descending;
170
+ first match wins.
171
+ - **Thread-safe.** `RoutingTable` uses `threading.Lock`.
172
+
173
+ ---
174
+
175
+ ## Development
176
+
177
+ ```bash
178
+ pip install -e ".[dev]"
179
+ pytest tests/ -q
180
+ ```
181
+
182
+ ---
183
+
184
+ ## License
185
+
186
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,14 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ nodus_router/__init__.py
5
+ nodus_router/binding.py
6
+ nodus_router/context.py
7
+ nodus_router/resolver.py
8
+ nodus_router/table.py
9
+ nodus_router.egg-info/PKG-INFO
10
+ nodus_router.egg-info/SOURCES.txt
11
+ nodus_router.egg-info/dependency_links.txt
12
+ nodus_router.egg-info/requires.txt
13
+ nodus_router.egg-info/top_level.txt
14
+ tests/test_router.py
@@ -0,0 +1,7 @@
1
+
2
+ [dev]
3
+ pytest>=8.0
4
+ nodus-session>=0.1.0
5
+
6
+ [session]
7
+ nodus-session>=0.1.0
@@ -0,0 +1 @@
1
+ nodus_router
@@ -0,0 +1,28 @@
1
+ [build-system]
2
+ requires = ["setuptools>=80", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "nodus-router"
7
+ version = "0.1.0"
8
+ description = "Route inbound context to agent + session scope via fnmatch binding rules"
9
+ authors = [{ name = "Shawn Knight" }]
10
+ license = { text = "MIT" }
11
+ readme = "README.md"
12
+ requires-python = ">=3.11"
13
+ dependencies = []
14
+
15
+ [project.optional-dependencies]
16
+ session = ["nodus-session>=0.1.0"]
17
+ dev = ["pytest>=8.0", "nodus-session>=0.1.0"]
18
+
19
+ [project.urls]
20
+ Homepage = "https://github.com/Masterplanner25/nodus-router"
21
+ Repository = "https://github.com/Masterplanner25/nodus-router"
22
+
23
+ [tool.setuptools.packages.find]
24
+ where = ["."]
25
+ include = ["nodus_router*"]
26
+
27
+ [tool.pytest.ini_options]
28
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,148 @@
1
+ import pytest
2
+ from nodus_router import (
3
+ InboundContext, RouteBinding, RouteMatch, RouteResolver, RoutingTable,
4
+ )
5
+
6
+
7
+ def _ctx(channel="slack", peer="U1", thread=None, roles=None):
8
+ return InboundContext(
9
+ channel_id=channel, peer_id=peer,
10
+ thread_id=thread, roles=roles or [],
11
+ )
12
+
13
+
14
+ def _table(*bindings):
15
+ t = RoutingTable()
16
+ for b in bindings:
17
+ t.add(b)
18
+ return t
19
+
20
+
21
+ # ── RouteBinding.matches ──────────────────────────────────────────────────────
22
+
23
+ def test_wildcard_matches_all():
24
+ b = RouteBinding("main") # all defaults = "*"
25
+ assert b.matches(_ctx("slack", "U1")) is True
26
+ assert b.matches(_ctx("discord", "D2")) is True
27
+
28
+
29
+ def test_channel_pattern_match():
30
+ b = RouteBinding("agent", channel_pattern="slack")
31
+ assert b.matches(_ctx("slack")) is True
32
+ assert b.matches(_ctx("discord")) is False
33
+
34
+
35
+ def test_channel_pattern_wildcard():
36
+ b = RouteBinding("agent", channel_pattern="sl*")
37
+ assert b.matches(_ctx("slack")) is True
38
+ assert b.matches(_ctx("slackcorp")) is True
39
+ assert b.matches(_ctx("discord")) is False
40
+
41
+
42
+ def test_peer_pattern():
43
+ b = RouteBinding("agent", peer_pattern="U123*")
44
+ assert b.matches(_ctx(peer="U12345")) is True
45
+ assert b.matches(_ctx(peer="D999")) is False
46
+
47
+
48
+ def test_peer_pattern_no_peer_fails():
49
+ b = RouteBinding("agent", peer_pattern="U*")
50
+ assert b.matches(InboundContext(channel_id="slack")) is False
51
+
52
+
53
+ def test_role_pattern():
54
+ b = RouteBinding("agent", role_pattern="admin*")
55
+ assert b.matches(_ctx(roles=["admin", "user"])) is True
56
+ assert b.matches(_ctx(roles=["user"])) is False
57
+
58
+
59
+ def test_thread_pattern():
60
+ b = RouteBinding("agent", thread_pattern="thread-*")
61
+ assert b.matches(_ctx(thread="thread-123")) is True
62
+ assert b.matches(_ctx(thread="dm-456")) is False
63
+
64
+
65
+ def test_thread_pattern_no_thread_fails():
66
+ b = RouteBinding("agent", thread_pattern="t*")
67
+ assert b.matches(_ctx()) is False
68
+
69
+
70
+ # ── RoutingTable ──────────────────────────────────────────────────────────────
71
+
72
+ def test_table_add_and_length():
73
+ t = RoutingTable()
74
+ assert len(t) == 0
75
+ t.add(RouteBinding("a"))
76
+ assert len(t) == 1
77
+
78
+
79
+ def test_table_sorted_by_priority():
80
+ t = RoutingTable()
81
+ t.add(RouteBinding("low", priority=10))
82
+ t.add(RouteBinding("high", priority=1))
83
+ bindings = t.bindings()
84
+ assert bindings[0].agent_id == "high"
85
+
86
+
87
+ def test_table_remove():
88
+ t = RoutingTable()
89
+ t.add(RouteBinding("a", channel_pattern="slack"))
90
+ t.add(RouteBinding("a", channel_pattern="discord"))
91
+ assert t.remove("a") == 2
92
+ assert len(t) == 0
93
+
94
+
95
+ def test_table_remove_unknown():
96
+ t = RoutingTable()
97
+ assert t.remove("nonexistent") == 0
98
+
99
+
100
+ # ── RouteResolver ─────────────────────────────────────────────────────────────
101
+
102
+ def test_resolve_first_match():
103
+ t = _table(
104
+ RouteBinding("slack-agent", channel_pattern="slack", priority=0),
105
+ RouteBinding("default-agent", priority=1),
106
+ )
107
+ r = RouteResolver(t)
108
+ match = r.resolve(_ctx("slack"))
109
+ assert match.agent_id == "slack-agent"
110
+
111
+
112
+ def test_resolve_fallback_to_default():
113
+ t = _table(RouteBinding("special", channel_pattern="discord"))
114
+ r = RouteResolver(t, default_agent_id="fallback")
115
+ match = r.resolve(_ctx("telegram"))
116
+ assert match.agent_id == "fallback"
117
+
118
+
119
+ def test_resolve_empty_table_uses_default():
120
+ r = RouteResolver(RoutingTable(), default_agent_id="main")
121
+ match = r.resolve(_ctx("slack"))
122
+ assert match.agent_id == "main"
123
+
124
+
125
+ def test_resolve_returns_session_key_with_channel():
126
+ t = _table(RouteBinding("main"))
127
+ r = RouteResolver(t)
128
+ match = r.resolve(_ctx("discord", "D123"))
129
+ assert match.session_key.channel == "discord"
130
+ assert match.session_key.peer == "D123"
131
+ assert match.session_key.agent_id == "main"
132
+
133
+
134
+ def test_resolve_returns_binding():
135
+ binding = RouteBinding("agent", channel_pattern="slack")
136
+ t = _table(binding)
137
+ r = RouteResolver(t)
138
+ match = r.resolve(_ctx("slack"))
139
+ assert match.binding is binding
140
+
141
+
142
+ def test_priority_order_respected():
143
+ t = RoutingTable()
144
+ t.add(RouteBinding("first", channel_pattern="slack", priority=0))
145
+ t.add(RouteBinding("second", channel_pattern="slack", priority=1))
146
+ r = RouteResolver(t)
147
+ match = r.resolve(_ctx("slack"))
148
+ assert match.agent_id == "first"