The escape record: skippable in a frame, refused as a key (L2/L3 codec)

len == 0 cannot spell a segment record — path syntax has no empty segment, so /a//b is invalid rather than a spelling of /a/b. That makes zero free to reserve, and RFC-0018 §5.4 spends it on the escape record: 00 <u8 kind> <u8 len> <len bytes>.

Its whole value is an asymmetry (CONTEXT.md §Segment / view). In a frame path an escape is admissible: a forwarder that does not implement kind steps over it by the record’s declared length rather than dropping a frame it is only relaying. In canonical / key context it is rejected: the vertex-map key must stay pure-string, so that a byte prefix still implies an ancestor.

What to notice

  • The skip needs no knowledge of the kind. The example escapes with kind 0x7F, which this build assigns no meaning to at all, and packed_record_span still steps over it and lands exactly on the next literal record. The node least likely to implement a kind is precisely the node that must step over it, which is why the record is self-delimiting.

  • The two contexts are two functions, not a bool argument. packed_record_span (frame path) admits the escape; packed_path_valid_key (canonical) refuses it, and wire::path_key answers nullopt for the decoded PATH. No call site has to decide the question with a flag it might pass the wrong way round.

  • The frame is still well-formed. The escape-bearing PATH decodes cleanly — it is a valid frame that is not a key. Those are different verdicts and the example asserts both.

  • kPackedEscapeKindPair (0x16) is the kind of RFC-0029’s PAIR element. RFC-0029 spells a vertex’s (index, generation) there; packed_path.hpp stays kind-agnostic by design, so in this example the escape is simply bytes that arrived.

  • Nothing here is conditional — the target builds and runs under every CI leg.

Source

 1/*
 2 * SPDX-License-Identifier: Apache-2.0
 3 * SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
 4 */
 5
 6/**
 7 * @file
 8 * @brief ONE CONCEPT — the escape record: admissible in a frame path, rejected as a key.
 9 *
10 * `len == 0` cannot spell a segment record (path syntax has no empty segment), so RFC-0018
11 * §5.4 reserves it for the **escape record** `00 <u8 kind> <u8 len> <len bytes>`. Its whole
12 * point is asymmetry (`CONTEXT.md` §Segment / view): a forwarder steps over a `kind` it does
13 * not implement **by the record's declared length** rather than dropping a frame it is only
14 * relaying, while canonical / key context REFUSES it — an element is not canonical bytes, and
15 * the vertex-map key must stay pure-string.
16 *
17 * Both halves are checked here, including the skip over a kind this build has never heard
18 * of. `kPackedEscapeKindPair` (`0x16`) is the kind RFC-0029's PAIR element uses.
19 * `packed_path.hpp` is kind-agnostic: here the escape is simply bytes that arrived.
20 *
21 * Runs under ctest as `example_wire_path_escape`; returns non-zero on any failed check.
22 */
23
24#include <cstddef>
25#include <cstdint>
26#include <cstdio>
27#include <optional>
28#include <vector>
29
30#include "libtracer/packed_path.hpp"
31#include "libtracer/tlv_emit.hpp"
32#include "libtracer/tracer.hpp"
33
34namespace {
35
36using tr::wire::opt_t;
37using tr::wire::type_t;
38
39/** @brief Report expectation @p what and record a failure on @p ok. */
40void check(bool& ok, bool cond, const char* what) {
41    std::printf("  [%s] %s\n", cond ? "ok" : "FAIL", what);
42    ok = ok && cond;
43}
44
45/** @brief A `kind` byte this build assigns no meaning to — the non-implementing hop's case. */
46constexpr std::uint8_t kUnknownKind = 0x7F;
47
48}  // namespace
49
50int main() {
51    bool ok = true;
52    const std::vector<std::byte> payload(4, std::byte{0xEE});
53
54    // "sensor" then an escape of an UNKNOWN kind then "temp".
55    std::vector<std::byte> body;
56    check(ok, tr::wire::emit_path_segment(body, "sensor"), "a literal record leads");
57    const std::size_t at = body.size();
58    check(ok, tr::wire::emit_path_escape(body, kUnknownKind, payload), "the escape record appends");
59    check(ok, tr::wire::emit_path_segment(body, "temp"), "and a literal record follows it");
60
61    const std::span<const std::byte> view(body);
62    check(ok, tr::wire::packed_record_is_escape(view, at),
63          "the record at that offset is an escape");
64    check(ok, tr::wire::packed_escape_kind(view, at) == kUnknownKind, "its kind reads back");
65    const auto esc = tr::wire::packed_escape_payload(view, at);
66    check(ok, esc && esc->size() == payload.size(), "and its declared payload is delimited");
67
68    // The forwarding property: step over it knowing nothing but its length.
69    const std::size_t span = tr::wire::packed_record_span(view, at);
70    std::printf("stepped over kind 0x%02X in %zu bytes without implementing it\n", kUnknownKind,
71                span);
72    check(ok, span == tr::wire::kPackedEscapeOverhead + payload.size(),
73          "packed_record_span steps over an unknown kind by its declared length");
74    check(ok, tr::wire::packed_record_is_escape(view, at + span) == false,
75          "landing exactly on the next literal record");
76
77    // The other half of the asymmetry: this body is not a key and never becomes one.
78    check(ok, !tr::wire::packed_path_valid_key(view), "an escape-bearing body is not a valid key");
79    std::vector<std::byte> frame;
80    tr::wire::emit_tlv(frame, type_t::PATH, opt_t{}, body);
81    const auto decoded = tr::wire::tlv_node_t::over(frame);
82    check(ok, decoded.has_value(), "the frame itself is well-formed and decodes");
83    check(ok, decoded && tr::wire::path_key(*decoded) == std::nullopt,
84          "yet path_key refuses it — canonical context rejects the escape");
85    return ok ? 0 : 1;
86}

See also: path module · frame codec · addressing reference.