@zieglar/pi-chat-exchange 0.3.3 → 0.3.4

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.
@@ -21,7 +21,7 @@ export function createTaskOwnerComposition(configPath, config, live, host, bound
21
21
  if (initial.lease !== "ACTIVE" || initial.exchangeId === undefined)
22
22
  throw new Error(INVALID);
23
23
  const scope = Object.freeze({ exchangeId: initial.exchangeId, from: selected.endpointId, to: config.endpoint.endpointId });
24
- const taskOwner = createTaskOwnerRegistryPort(sidecar.registryDirectory, scope);
24
+ const taskOwner = createTaskOwnerRegistryPort(sidecar.registryDirectory, scope, sidecar.ownerProof);
25
25
  const taskOwnerAdmission = () => {
26
26
  const operational = bound.operationalStatus();
27
27
  const status = host.status(config.endpoint.endpointId);
@@ -25,7 +25,7 @@ import { callMessagingTool, CE_ACK_TOOL, isAckToolName, isMessagingToolName, MES
25
25
  import { callEndpointDiscovery, CE_ENDPOINTS_TOOL, CE_ENDPOINTS_TOOL_NAME } from "./endpoint-discovery.js";
26
26
  import { callTaskOwner, CE_TASK_OWNER_TOOL, CE_TASK_OWNER_TOOL_NAME } from "./task-owner.js";
27
27
  export const SERVER_NAME = "pi-chat-exchange";
28
- export const SERVER_VERSION = "0.3.3";
28
+ export const SERVER_VERSION = "0.3.4";
29
29
  const PING_TOOL = {
30
30
  name: PING_TOOL_NAME,
31
31
  description: "Read-only liveness probe for the pi-chat-exchange service. " +
@@ -1,5 +1,5 @@
1
1
  import fs from "node:fs";
2
- import { isAbsolute, join, parse, resolve, sep } from "node:path";
2
+ import { basename, dirname, isAbsolute, join, parse, resolve, sep } from "node:path";
3
3
  import { validMessageId, validRegisteredTaskOwner } from "./task-owner.js";
4
4
  const MAX_BYTES = 16_384;
5
5
  const INVALID = "CE_MCP_TASK_OWNERSHIP_INVALID";
@@ -78,30 +78,58 @@ export function loadTaskOwnershipSidecar(configPath) {
78
78
  }
79
79
  try {
80
80
  const value = parseFlatUniqueJson(readPrivateBounded(path));
81
- const expectedKeys = "piEndpoint" in value ? 3 : 2;
82
- if (Object.keys(value).length !== expectedKeys || value.schemaVersion !== 1 || typeof value.registryDirectory !== "string" ||
81
+ if (Object.keys(value).some(key => !["schemaVersion", "registryDirectory", "piEndpoint", "ownerProof"].includes(key)) ||
82
+ value.schemaVersion !== 1 || typeof value.registryDirectory !== "string" ||
83
83
  value.registryDirectory.includes("\0") || !isAbsolute(value.registryDirectory) || resolve(value.registryDirectory) !== value.registryDirectory)
84
84
  return invalid();
85
85
  const piEndpoint = value.piEndpoint;
86
86
  if ("piEndpoint" in value && (typeof piEndpoint !== "string" || !/^pi\/[A-Za-z0-9-]{1,64}$/.test(piEndpoint)))
87
87
  return invalid();
88
+ if ("ownerProof" in value && (value.ownerProof !== "ce-store-v1" || typeof piEndpoint !== "string"))
89
+ return invalid();
88
90
  return { schemaVersion: 1, registryDirectory: value.registryDirectory,
89
- ...(typeof piEndpoint === "string" ? { piEndpoint } : {}) };
91
+ ...(typeof piEndpoint === "string" ? { piEndpoint } : {}),
92
+ ...(value.ownerProof === "ce-store-v1" ? { ownerProof: "ce-store-v1" } : {}) };
90
93
  }
91
94
  catch {
92
95
  return invalid();
93
96
  }
94
97
  }
95
- export function createTaskOwnerRegistryPort(directory, scope) {
98
+ export function createTaskOwnerRegistryPort(directory, scope, ownerProof) {
96
99
  return Object.freeze({ lookup(messageId) {
97
100
  const unverified = { messageId, ownerStatus: "UNVERIFIED" };
98
101
  if (!validMessageId(messageId))
99
102
  return unverified;
100
103
  try {
104
+ if (ownerProof === undefined) {
105
+ assertPrivateDirectory(directory);
106
+ const candidate = parseFlatUniqueJson(readPrivateBounded(join(directory, `${messageId}.json`)));
107
+ if (!validRegisteredTaskOwner(candidate) || candidate.messageId !== messageId || candidate.exchangeId !== scope.exchangeId ||
108
+ candidate.from !== scope.from || candidate.to !== scope.to || candidate.taskOwner !== scope.from)
109
+ return unverified;
110
+ return candidate;
111
+ }
112
+ if (basename(directory) !== "metadata")
113
+ return unverified;
114
+ const registryRoot = dirname(directory);
115
+ const owners = join(registryRoot, "owners");
116
+ assertPrivateDirectory(registryRoot);
101
117
  assertPrivateDirectory(directory);
118
+ assertPrivateDirectory(owners);
119
+ const registryScope = parseFlatUniqueJson(readPrivateBounded(join(registryRoot, "scope.json")));
120
+ if (Object.keys(registryScope).length !== 3 || registryScope.exchangeId !== scope.exchangeId ||
121
+ registryScope.from !== scope.from || registryScope.to !== scope.to)
122
+ return unverified;
102
123
  const candidate = parseFlatUniqueJson(readPrivateBounded(join(directory, `${messageId}.json`)));
103
124
  if (!validRegisteredTaskOwner(candidate) || candidate.messageId !== messageId || candidate.exchangeId !== scope.exchangeId ||
104
- candidate.from !== scope.from || candidate.to !== scope.to || candidate.taskOwner !== scope.from)
125
+ candidate.from !== scope.from || candidate.to !== scope.to)
126
+ return unverified;
127
+ // The Store writes CONFLICT to the owner before updating any projections.
128
+ // Opening the owner last makes a successful lookup valid at that instant,
129
+ // assuming supported Store writes and no rollback of its private directory.
130
+ const owner = parseFlatUniqueJson(readPrivateBounded(join(owners, `${candidate.requestMessageId}.json`)));
131
+ if (Object.keys(owner).length !== 3 || owner.ownerStatus !== "REGISTERED" ||
132
+ owner.projectLabel !== candidate.projectLabel || owner.taskOwner !== candidate.taskOwner)
105
133
  return unverified;
106
134
  return candidate;
107
135
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zieglar/pi-chat-exchange",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public",