nofax 0.2.48 → 0.2.51

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/src/mcp-tools.mjs CHANGED
@@ -1,6 +1,7 @@
1
- import { loadConfig } from './config.mjs';
1
+ import { loadConfig, validateTopic } from './config.mjs';
2
2
  import {
3
3
  createRemoteRequest,
4
+ normalizeOptions,
4
5
  pollRemoteResponse,
5
6
  sendNotification,
6
7
  sendResponseConfirmation
@@ -102,6 +103,8 @@ function safeSliceEnd(text, end) {
102
103
  function validateText(value, name, max = 2200) {
103
104
  if (typeof value !== 'string' || !value.trim()) throw new Error(`NOFAX_${name}_REQUIRED`);
104
105
  const text = value.trim();
106
+ // Replacing an ill-formed code unit could change the request the human sees.
107
+ if (!text.isWellFormed()) throw new Error(`NOFAX_${name}_INVALID_UTF8`);
105
108
  if (text.length <= max) return text;
106
109
  const end = safeSliceEnd(text, max - 14);
107
110
  return `${text.slice(0, end)}…[truncated]`;
@@ -115,32 +118,10 @@ function validateWaitSeconds(value) {
115
118
  return seconds;
116
119
  }
117
120
 
118
- function boundChoiceLabel(label) {
119
- const end = safeSliceEnd(label, Math.min(label.length, 32));
120
- return label.slice(0, end);
121
- }
122
-
123
121
  function validateChoiceOptions(options) {
124
122
  if (!Array.isArray(options) || options.length < 1 || options.length > 3) throw new Error('NOFAX_CHOICE_LIMIT');
125
- const normalized = options.map((option) => {
126
- if (typeof option === 'string') {
127
- const value = option.trim();
128
- if (!value || value.length > 80) throw new Error('NOFAX_CHOICE_INVALID');
129
- return { value, label: boundChoiceLabel(value) };
130
- }
131
- if (!option || typeof option.value !== 'string' || typeof option.label !== 'string') throw new Error('NOFAX_CHOICE_INVALID');
132
- const value = option.value.trim();
133
- const label = option.label.trim();
134
- if (!value || !label || value.length > 80) throw new Error('NOFAX_CHOICE_INVALID');
135
- return { value, label: boundChoiceLabel(label) };
136
- });
137
- if (new Set(normalized.map((option) => option.value)).size !== normalized.length) {
138
- throw new Error('NOFAX_CHOICE_DUPLICATE');
139
- }
140
- if (new Set(normalized.map((option) => option.label)).size !== normalized.length) {
141
- throw new Error('NOFAX_CHOICE_DUPLICATE');
142
- }
143
- return normalized;
123
+ // Prevalidate the same rendered labels that ntfy will show on the phone.
124
+ return normalizeOptions(options);
144
125
  }
145
126
 
146
127
  export function createMcpToolHandlers(overrides = {}) {
@@ -158,6 +139,9 @@ export function createMcpToolHandlers(overrides = {}) {
158
139
  resolveRequestWithClaimImpl: overrides.resolveRequestWithClaimImpl ?? resolveRequestWithClaim,
159
140
  listPendingRequestsImpl: overrides.listPendingRequestsImpl ?? listPendingRequests,
160
141
  nowImpl: overrides.nowImpl ?? Date.now,
142
+ // Wall-clock adjustments must not extend a human-approval polling window.
143
+ // Existing callers that inject nowImpl for tests keep their virtual clock.
144
+ elapsedNowImpl: overrides.elapsedNowImpl ?? (overrides.nowImpl ?? (() => performance.now())),
161
145
  sleepImpl: overrides.sleepImpl ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)))
162
146
  };
163
147
 
@@ -259,9 +243,12 @@ export function createMcpToolHandlers(overrides = {}) {
259
243
  const current = request.server && request.topic
260
244
  ? { server: request.server, topic: request.topic }
261
245
  : await config();
262
- const deadline = deps.nowImpl() + seconds * 1000;
246
+ // Historical records remain readable, but a weak saved topic must never
247
+ // become a live approval-response transport after topic rules tighten.
248
+ validateTopic(current.topic);
249
+ const deadline = deps.elapsedNowImpl() + seconds * 1000;
263
250
  while (true) {
264
- const remainingBeforePoll = deadline - deps.nowImpl();
251
+ const remainingBeforePoll = deadline - deps.elapsedNowImpl();
265
252
  if (remainingBeforePoll <= 0) break;
266
253
  const response = await deps.pollRemoteResponseImpl({
267
254
  config: current,
@@ -271,29 +258,43 @@ export function createMcpToolHandlers(overrides = {}) {
271
258
  kind: request.kind,
272
259
  timeoutMs: remainingBeforePoll
273
260
  });
274
- if (response !== null) {
261
+ // A poll transport can return after its deadline despite a timeout
262
+ // hint. Do not turn that late result into an accepted human decision.
263
+ if (response !== null && deps.elapsedNowImpl() < deadline) {
264
+ // A wall-clock correction after request creation must not make a
265
+ // genuine terminal decision invalid or erase the human's denial.
266
+ const createdAtMs = Date.parse(request.createdAt);
267
+ const nowMs = deps.nowImpl();
268
+ const resolvedAtMs = Number.isFinite(createdAtMs) ? Math.max(nowMs, createdAtMs) : nowMs;
275
269
  const resolution = await deps.resolveRequestWithClaimImpl({
276
270
  home: deps.home,
277
271
  env: deps.env,
278
272
  requestId: request.requestId,
279
273
  response,
280
- resolvedAt: new Date(deps.nowImpl()).toISOString()
274
+ resolvedAt: new Date(resolvedAtMs).toISOString()
281
275
  });
282
276
  request = resolution.request;
283
- const acceptedResponse = resolution.claimed
284
- && request.decision === response.decision
285
- && (request.text ?? undefined) === (response.text ?? undefined);
277
+ // A new terminal claim is authoritative even when durable storage
278
+ // trims or bounds the human's raw refinement text.
279
+ const acceptedResponse = resolution.claimed && request.decision === response.decision;
280
+ let confirmationStatus;
286
281
  if (acceptedResponse) {
287
- await deps.sendResponseConfirmationImpl({
288
- config: current,
289
- response,
290
- kind: request.kind,
291
- title: request.kind === 'approval' ? 'Approval' : request.kind === 'refinement' ? 'Refinement' : 'Choice'
292
- });
282
+ try {
283
+ const confirmationDelivered = await deps.sendResponseConfirmationImpl({
284
+ config: current,
285
+ response: { ...response, text: request.text },
286
+ kind: request.kind,
287
+ title: request.kind === 'approval' ? 'Approval' : request.kind === 'refinement' ? 'Refinement' : 'Choice'
288
+ });
289
+ if (confirmationDelivered === false) confirmationStatus = 'failed';
290
+ } catch {
291
+ confirmationStatus = 'failed';
292
+ }
293
293
  }
294
- return terminalResult(request);
294
+ const result = terminalResult(request);
295
+ return confirmationStatus ? { ...result, confirmationStatus } : result;
295
296
  }
296
- const remaining = deadline - deps.nowImpl();
297
+ const remaining = deadline - deps.elapsedNowImpl();
297
298
  if (remaining <= 0) break;
298
299
  await deps.sleepImpl(Math.min(1000, remaining));
299
300
  }