Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
directory: a lookup one router answers with nothing is put to the next
Antonio Ojea27013faa6365
diff --git a/internal/node/directory_client.go b/internal/node/directory_client.goindex 5d0e44b8..b4899a0e 100644--- a/internal/node/directory_client.go+++ b/internal/node/directory_client.go@@ -185,7 +185,10 @@ func (d *routerDirectory) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfreturn info, nil}-// request sends req to the attached routers in turn until one answers.+// request sends req to the attached routers in turn until one answers. A+// lookup one router answers with nothing is put to the next as well: a+// router cut off from the routers closest to the key answers from what it+// can reach, and the member holds another that may not be.func (d *routerDirectory) request(ctx context.Context, op string, req *api.DirectoryRequest) (*api.DirectoryResponse, error) {start := time.Now()defer func() { directoryRequestSeconds.WithLabelValues(op).Observe(time.Since(start).Seconds()) }()@@ -199,15 +202,17 @@ func (d *routerDirectory) request(ctx context.Context, op string, req *api.Direclogger.Warnf("[Directory] %s: %v", op, errNoDirectoryRouter)return nil, errNoDirectoryRouter}+ lookup := op == directoryOpFindProviders || op == directoryOpFindPeervar lastErr error+ var empty *api.DirectoryResponsefor _, router := range routers {resp, err := d.ask(ctx, router, data)if err == nil {- result := directoryResultOK- if op != directoryOpProvide && op != directoryOpWithdraw && len(resp.GetProviders()) == 0 && resp.GetPeer() == nil {- result = directoryResultEmpty+ if lookup && len(resp.GetProviders()) == 0 && resp.GetPeer() == nil {+ empty = resp+ continue}- directoryRequestsTotal.WithLabelValues(op, result).Inc()+ directoryRequestsTotal.WithLabelValues(op, directoryResultOK).Inc()return resp, nil}lastErr = err@@ -216,6 +221,10 @@ func (d *routerDirectory) request(ctx context.Context, op string, req *api.Direcbreak}}+ if empty != nil {+ directoryRequestsTotal.WithLabelValues(op, directoryResultEmpty).Inc()+ return empty, nil+ }directoryRequestsTotal.WithLabelValues(op, directoryResultFailed).Inc()return nil, fmt.Errorf("%s: no attached router answered: %w", op, lastErr)}diff --git a/internal/node/directory_client_test.go b/internal/node/directory_client_test.goindex 52e08f47..e175256d 100644--- a/internal/node/directory_client_test.go+++ b/internal/node/directory_client_test.go@@ -227,6 +227,18 @@ func TestRouterDirectoryAsksTheAttachedRouters(t *testing.T) {t.Fatalf("FindPeer of the member = %v, %v; want it from the router's own connection", info, err)}+ // The first router knows nothing of a key the second holds: a lookup+ // one router answers with nothing is put to the next.+ elsewhere, _ := api.ServiceKey(api.ServiceType_SERVICE_TYPE_MCP, "elsewhere")+ dir2.add(elsewhere, member.ID(), member.Addrs()...)+ before1, before2 := dir1.servedCount(), dir2.servedCount()+ if found, err := directory.FindProviders(ctx, elsewhere, 0); err != nil || len(found) != 1 {+ t.Fatalf("FindProviders of a key only the second router holds = %v, %v; want the second router's answer", found, err)+ }+ if dir1.servedCount() != before1+1 || dir2.servedCount() != before2+1 {+ t.Fatalf("routers served %d and %d requests, want %d and %d: the second is asked when the first has nothing", dir1.servedCount(), dir2.servedCount(), before1+1, before2+1)+ }+// The first router fails: the second answers, and the member is none the wiser.dir1.fail(true)dir2.add(key, member.ID(), member.Addrs()...)diff --git a/sdk/js/src/discovery.test.ts b/sdk/js/src/discovery.test.tsnew file mode 100644index 00000000..1f2d4826--- /dev/null+++ b/sdk/js/src/discovery.test.ts@@ -0,0 +1,101 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++import { create, fromBinary, toBinary } from "@bufbuild/protobuf";+import { yamux } from "@chainsafe/libp2p-yamux";+import type { Connection, Libp2p } from "@libp2p/interface";+import { tcp } from "@libp2p/tcp";+import { tls } from "@libp2p/tls";+import { lpStream } from "@libp2p/utils";+import { createLibp2p } from "libp2p";+import assert from "node:assert/strict";+import { after, before, test } from "node:test";+import { DIRECTORY_PROTOCOL, DirectoryError, request, serviceKey } from "./discovery.ts";+import { DirectoryRequestSchema, DirectoryResponseSchema, PeerRecordSchema, type DirectoryResponse } from "./gen/agentmesh_pb.ts";++const PROVIDER = "12D3KooWA4Xop1JaT3MHxwYMkCepYsv4iPVopMXwCz5iHYdBfeSB";++let member: Libp2p;+let routerA: Libp2p;+let routerB: Libp2p;+const asked: string[] = [];+// What each router answers a find with; an announcement is always taken.+const answers = new Map<string, () => DirectoryResponse>();++async function node(): Promise<Libp2p> {+ return createLibp2p({ addresses: { listen: ["/ip4/127.0.0.1/tcp/0"] }, transports: [tcp()], connectionEncrypters: [tls()], streamMuxers: [yamux()] });+}++function serve(router: Libp2p, name: string): Promise<void> {+ return router.handle(DIRECTORY_PROTOCOL, (stream) => {+ void (async () => {+ const lp = lpStream(stream, { maxDataLength: 64 * 1024 });+ const req = fromBinary(DirectoryRequestSchema, (await lp.read()).subarray());+ asked.push(name);+ const resp = req.request.case === "provide" ? create(DirectoryResponseSchema, {}) : (answers.get(name) ?? (() => create(DirectoryResponseSchema, {})))();+ await lp.write(toBinary(DirectoryResponseSchema, resp));+ await stream.close();+ })();+ });+}++before(async () => {+ [member, routerA, routerB] = await Promise.all([node(), node(), node()]);+ await Promise.all([serve(routerA, "a"), serve(routerB, "b")]);+ await member.dial(routerA.getMultiaddrs());+ await member.dial(routerB.getMultiaddrs());+});++after(async () => {+ await Promise.all([member.stop(), routerA.stop(), routerB.stop()]);+});++function held(router: Libp2p) {+ return { peerId: router.peerId.toString(), connection: (): Connection | undefined => member.getConnections(router.peerId)[0] };+}++const found = () => create(DirectoryResponseSchema, { providers: [create(PeerRecordSchema, { peerId: PROVIDER, addrs: [`/ip4/10.0.0.9/tcp/4001/p2p/${PROVIDER}`] })] });+const nothing = () => create(DirectoryResponseSchema, {});++test("a lookup one router answers with nothing is put to the next; an announcement is not", async () => {+ // A router cut off from the routers closest to the key answers from what+ // it can reach; the member holds another that may not be.+ const key = await serviceKey("mcp", "calculator");+ const find = create(DirectoryRequestSchema, { request: { case: "findProviders", value: { key, limit: 20 } } });++ answers.set("a", nothing);+ answers.set("b", found);+ asked.length = 0;+ let resp = await request([held(routerA), held(routerB)], find);+ assert.equal(resp.providers[0]?.peerId, PROVIDER);+ assert.deepEqual(asked, ["a", "b"]);++ answers.set("b", nothing);+ asked.length = 0;+ resp = await request([held(routerA), held(routerB)], find);+ assert.equal(resp.providers.length, 0, "nobody has a record: the answer is empty, not an error");+ assert.deepEqual(asked, ["a", "b"]);++ answers.set("a", found);+ asked.length = 0;+ resp = await request([held(routerA), held(routerB)], find);+ assert.equal(resp.providers[0]?.peerId, PROVIDER);+ assert.deepEqual(asked, ["a"], "the first router answered");++ asked.length = 0;+ await request([held(routerA), held(routerB)], create(DirectoryRequestSchema, { request: { case: "provide", value: { key, addrs: [] } } }));+ assert.deepEqual(asked, ["a"], "an announcement one router took is not repeated");++ await assert.rejects(request([{ peerId: "nobody", connection: () => undefined }], find), DirectoryError);+});diff --git a/sdk/js/src/discovery.ts b/sdk/js/src/discovery.tsindex 466afec3..909db12b 100644--- a/sdk/js/src/discovery.ts+++ b/sdk/js/src/discovery.ts@@ -104,11 +104,15 @@ async function ask(conn: Connection, req: DirectoryRequest, signal: AbortSignal)/*** Sends req to the held routers in turn until one answers. A router that- * is restarting costs the request a timeout, not an answer.+ * is restarting costs the request a timeout, not an answer. A lookup one+ * router answers with nothing is put to the next as well: a router cut+ * off from the routers closest to the key answers from what it can reach.*/export async function request(routers: DirectoryRouter[], req: DirectoryRequest, signal?: AbortSignal): Promise<DirectoryResponse> {+ const lookup = req.request.case === "findProviders" || req.request.case === "findPeer";let last: unknown;let asked = 0;+ let empty: DirectoryResponse | undefined;for (const router of routers) {const conn = router.connection();if (conn === undefined) {@@ -117,7 +121,12 @@ export async function request(routers: DirectoryRouter[], req: DirectoryRequest,asked++;const bounded = signal === undefined ? AbortSignal.timeout(REQUEST_TIMEOUT_MS) : AbortSignal.any([signal, AbortSignal.timeout(REQUEST_TIMEOUT_MS)]);try {- return await ask(conn, req, bounded);+ const resp = await ask(conn, req, bounded);+ if (lookup && resp.providers.length === 0 && resp.peer === undefined) {+ empty = resp;+ continue;+ }+ return resp;} catch (err) {last = err;if (signal?.aborted === true) {@@ -125,6 +134,9 @@ export async function request(routers: DirectoryRouter[], req: DirectoryRequest,}}}+ if (empty !== undefined) {+ return empty;+ }if (asked === 0) {throw new DirectoryError("no held router to ask");}diff --git a/sdk/python/src/agent_mesh/discovery.py b/sdk/python/src/agent_mesh/discovery.pyindex 06e9bb56..ce901bf2 100644--- a/sdk/python/src/agent_mesh/discovery.py+++ b/sdk/python/src/agent_mesh/discovery.py@@ -107,16 +107,27 @@ async def _ask(host: IHost, router: ID, req: pb.DirectoryRequest) -> pb.Directorasync def request(host: IHost, routers: Iterable[ID], req: pb.DirectoryRequest) -> pb.DirectoryResponse:"""Sends req to the held routers in turn until one answers. A router that- is restarting costs the request a timeout, not an answer."""+ is restarting costs the request a timeout, not an answer. A lookup one+ router answers with nothing is put to the next as well: a router cut off+ from the routers closest to the key answers from what it can reach."""+ lookup = req.WhichOneof("request") in ("find_providers", "find_peer")last: Exception | None = Noneasked = 0+ empty: pb.DirectoryResponse | None = Nonefor router in routers:asked += 1try:- return await _ask(host, router, req)+ resp = await _ask(host, router, req)except Exception as err: # noqa: BLE001 - the next router is askedlast = errlogger.warning("directory: request through router %s failed: %s", router, err)+ continue+ if lookup and not resp.providers and not resp.HasField("peer"):+ empty = resp+ continue+ return resp+ if empty is not None:+ return emptyif asked == 0:raise DirectoryError("no held router to ask")raise DirectoryError(f"no held router answered: {last}")diff --git a/sdk/python/tests/test_discovery.py b/sdk/python/tests/test_discovery.pyindex 9e9d5b71..65785203 100644--- a/sdk/python/tests/test_discovery.py+++ b/sdk/python/tests/test_discovery.py@@ -216,3 +216,31 @@ def test_read_bounded_varint_prefixed_bytes_rejects_oversized_prefix_before_readassert bomb.payload_reads == 0trio.run(main)+++def test_an_empty_answer_is_put_to_the_next_router():+ """A router cut off from the routers closest to the key answers with+ nothing; the next held router is asked before the lookup is empty. An+ announcement is not repeated: one router taking it is enough."""++ def knows_nothing(req):+ return pb.DirectoryResponse()++ def answers(req):+ return pb.DirectoryResponse(providers=[_record(PROVIDER, "/ip4/10.0.0.9/tcp/4001")])++ async def main():+ host = _Host({ROUTER: knows_nothing, ROUTER_B: answers})+ found = await find_providers(host, [ROUTER, ROUTER_B], service_key("mcp", "calculator"))+ assert [p.peer_id for p in found] == [str(PROVIDER)]+ assert host.asked == [ROUTER, ROUTER_B]++ host = _Host({ROUTER: knows_nothing, ROUTER_B: knows_nothing})+ assert await find_providers(host, [ROUTER, ROUTER_B], service_key("mcp", "calculator")) == []+ assert host.asked == [ROUTER, ROUTER_B]++ host = _Host({ROUTER: knows_nothing, ROUTER_B: answers})+ await provide(host, [ROUTER, ROUTER_B], service_key("mcp", "calculator"), [])+ assert host.asked == [ROUTER], "an announcement one router took is not repeated"++ trio.run(main)