Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

Accepted is not published: keep an unknown state in a delivery receipt

A publishing task can have a remote ID and an uncertain final outcome at the same time. A client that stores only success or failure loses that distinction. Here is a small, executable JavaScript model for making the di

A publishing task can have a remote ID and an uncertain final outcome at the same time. A client that stores only success or failure loses that distinction.

Here is a small, executable JavaScript model for making the distinction explicit. It is a teaching example: it does not send requests, retry work or prove that a real page has been published.

Start with evidence, not a green checkmark

HTTP202 tells a client that a request was accepted for processing. It does not establish completion. RFC9110 section15.3.3 describes the same boundary at the protocol level.

A particular CMS may return a different status or use a synchronous contract. Follow that contract. The useful design question is what each observation actually establishes.

State Evidence available
pending A local operation key exists
accepted An acknowledgement supplied a remote ID
unknown The final outcome has not been established
published A matching readback supplied explicit verification and a URL

These are local model states, not new meanings for HTTP status codes. In particular, unknown is not an instruction to retry.

Keep the key and the remote ID

The operation key correlates observations. The remote ID identifies the record being reconciled. A timeout should preserve both pieces of information when they are available.

The example below accepts three event kinds: ack, timeout and readback. It returns a new frozen receipt instead of mutating earlier evidence.

Save this complete module as receipt.mjs:

// Teaching model. It performs no requests, retries, persistence or deduplication.
const STATES = new Set(['pending', 'accepted', 'unknown', 'published']);
const requiredText = value => typeof value === 'string' && value.trim().length > 0;

export function newReceipt(key) {
  if (!requiredText(key) || key.length > 128) throw new Error('A short stable key is required');
  return Object.freeze({key, state: 'pending', remoteId: null, url: null});
}

function publicUrl(value) {
  if (!requiredText(value)) throw new Error('Readback requires a URL');
  const url = new URL(value);
  if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) {
    throw new Error('Use an HTTP(S) URL without embedded credentials');
  }
  return url.href;
}

export function applyEvent(previous, event) {
  if (!previous || !STATES.has(previous.state)) throw new Error('Invalid receipt');
  if (!event || event.key !== previous.key) throw new Error('Event key does not match');
  if (!['ack', 'timeout', 'readback'].includes(event.kind)) throw new Error('Unknown event');
  // A late callback cannot turn established publication evidence into uncertainty.
  if (previous.state === 'published') return previous;

  if (event.kind === 'timeout') {
    return Object.freeze({...previous, state: 'unknown'});
  }
  if (!requiredText(event.remoteId)) throw new Error('Remote ID is required');
  if (previous.remoteId && event.remoteId !== previous.remoteId) {
    throw new Error('Remote ID changed; investigate instead of guessing');
  }
  if (event.kind === 'ack') {
    return Object.freeze({...previous, remoteId: event.remoteId,
      state: previous.state === 'unknown' ? 'unknown' : 'accepted'});
  }
  if (event.verified !== true) throw new Error('Readback needs explicit verification');
  return Object.freeze({...previous, state: 'published', remoteId: event.remoteId,
    url: publicUrl(event.url)});
}

The two checks before the state transition matter. An event must belong to the same operation, and a known remote ID must not silently change during reconciliation.

The verified flag is deliberately strict: the Boolean true is accepted, while a string containing "true" is not. This is a contract with the caller. It does not perform verification on the caller's behalf.

Exercise the uncertain path

Save the following alongside the module as example.mjs, then run it with Node.js:

import assert from 'node:assert/strict';
import {newReceipt, applyEvent} from './receipt.mjs';

const key = 'teaching-article-v1';
let receipt = newReceipt(key);
receipt = applyEvent(receipt, {
  key, kind: 'ack', remoteId: 'demo-17'
});
assert.equal(receipt.state, 'accepted');
assert.equal(receipt.url, null);

receipt = applyEvent(receipt, {key, kind: 'timeout'});
assert.equal(receipt.state, 'unknown');
assert.equal(receipt.remoteId, 'demo-17');

assert.throws(() => applyEvent(receipt, {
  key, kind: 'readback', remoteId: 'demo-18',
  verified: true,
  url: 'https://example.invalid/articles/different-demo'
}));

receipt = applyEvent(receipt, {
  key, kind: 'readback', remoteId: 'demo-17',
  verified: true,
  url: 'https://example.invalid/articles/teaching-demo'
});
assert.equal(receipt.state, 'published');
assert.equal(
  applyEvent(receipt, {key, kind: 'timeout'}),
  receipt
);
console.log(receipt);

The example.invalid URLs and demo IDs are fictional. Nothing is fetched when this program runs.

The important intermediate result is unknown with remoteId still set to demo-17. A later lookup can use that identity. A different record is rejected instead of being quietly treated as the result of the original operation.

Why a late acknowledgement stays uncertain

Suppose the timeout arrives first and the acknowledgement arrives later. The acknowledgement adds useful information: there is now a remote ID. It still does not tell the client whether the final action completed.

For that reason, the ack branch preserves unknown. A verified matching readback can advance the model to published. After that, a late timeout does not erase the established result.

This is one possible reconciliation policy. Event ordering, explicit server rejection and cancellation may require a richer model in a real application.

Check invariants beyond the happy path

Useful checks include a missing operation key, an event for another operation, a different remote ID, a non-Boolean verification flag, an executable URL and a late callback after publication.

The module was also exercised locally for eleven groups of assertions, including preservation of earlier immutable receipts. Those checks establish the behavior of this teaching model; they do not establish the behavior of a CMS or distributed queue.

Parsing a saved receipt as JSON is another separate boundary. JSON.parse parses syntax. A production reader still needs to validate its schema and application rules before treating it as evidence.

What a real integration must add

A local key does not prevent duplicates by itself. The receiving system needs a documented idempotency mechanism, or the integration needs a reconciliation strategy that handles duplicate attempts explicitly.

The real readback also needs authentication, record identity, intended visibility and content checks. An HTTP200 response could be a login screen or the wrong article. The verified flag should only be set after those checks have actually succeeded.

Durable storage and concurrent workers require their own consistency rules. This small reducer has no database transaction, lock, retry scheduler, cancellation state or durable event log. It deliberately leaves those decisions visible instead of hiding them behind a success Boolean.

The result is a modest starting point: a receipt says what evidence has been observed, and uncertainty remains available for the next decision.

AI disclosure: this article was drafted with AI assistance. The teaching code and example were executed locally; the examples are illustrative and contain no production credentials.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.