module scheme // Only the initial inherited raw buffer can precede an authoritative getter. // Readiness may decode it without running Scheme. Its points share this one // lazily owned, single-assignment origin so escaped snapshots resolve together. @[heap] struct CustomRawAnchor { mut: base Value known bool symbolic bool } fn (mut anchor CustomRawAnchor) resolve(base Value) ! { if anchor.symbolic { return failure('symbolic raw origin has no absolute position') } check_custom_binary_position(base)! if anchor.known { if !numeric_equal(anchor.base, base) { return failure('initial raw origin is already resolved') } return } anchor.base = base anchor.known = true } // The offset belongs to one bounded raw publication, never the port lifetime. // Old CR/carry/probe endpoints keep their own exact anchor across arbitrarily // long ignored input or noncontiguous continuation replay. No per-point box. struct CustomRawPoint { valid bool origin Value initial &CustomRawAnchor = unsafe { nil } offset int } fn custom_raw_point(origin Value, offset int) !CustomRawPoint { check_custom_binary_position(origin)! if offset < 0 { return failure('negative raw boundary offset') } return CustomRawPoint{ valid: true, origin: origin, offset: offset } } fn custom_initial_raw_point(anchor &CustomRawAnchor, offset int) !CustomRawPoint { if isnil(anchor) || anchor.symbolic || offset < 0 { return failure('invalid initial raw boundary') } return CustomRawPoint{ valid: true, initial: anchor, offset: offset } } fn (point CustomRawPoint) absolute() !Value { if !point.valid || point.offset < 0 { return failure('invalid raw boundary') } mut base := point.origin if !isnil(point.initial) { if point.initial.symbolic { return failure('symbolic raw origin has no absolute position') } if !point.initial.known { return failure('initial raw origin is unknown') } base = point.initial.base } check_custom_binary_position(base)! if point.offset == 0 { return base } return numeric_add(base, integer(point.offset)) } // Compare anchor identity without big-integer arithmetic on the scalar path. // Equal numeric values with distinct boxes conservatively start a new span. @[inline] fn (point CustomRawPoint) same_origin(other CustomRawPoint) bool { if !point.valid || !other.valid { return false } if !isnil(point.initial) || !isnil(other.initial) { if voidptr(point.initial) != voidptr(other.initial) { return false } if point.initial.symbolic { return point.origin.tag == .integer && other.origin.tag == .integer && point.origin.whole() == other.origin.whole() } return true } if point.origin.tag != other.origin.tag { return false } return if point.origin.tag == .integer { point.origin.whole() == other.origin.whole() } else { point.origin.tag == .big_integer && point.origin.pointer() == other.origin.pointer() } } @[inline] fn (point CustomRawPoint) same(other CustomRawPoint) bool { return point.offset == other.offset && point.same_origin(other) } @[inline] fn (point CustomRawPoint) delta(other CustomRawPoint) i64 { if !point.same_origin(other) { return -1 } return i64(point.offset) - other.offset } fn (point CustomRawPoint) retreat(distance i64) !CustomRawPoint { if !point.valid || distance < 0 || distance > point.offset { return failure('raw boundary is outside its publication') } return CustomRawPoint{ ...point, offset: point.offset - int(distance) } } // Missing absolute callbacks do not imply position zero. These nonmaterializable // coordinates only prove identity/relative offsets within one acknowledged raw // publication. The otherwise-unused origin Value carries an inline cohort ID; // the symbolic anchor prevents it from ever becoming an absolute coordinate. fn custom_symbolic_raw_point(anchor &CustomRawAnchor, cohort u64, offset int) !CustomRawPoint { if isnil(anchor) || !anchor.symbolic || cohort > u64(max_i64) || offset < 0 { return failure('invalid symbolic raw boundary') } return CustomRawPoint{ valid: true, initial: anchor, origin: integer(i64(cohort)), offset: offset } } struct CustomRawCohorts { mut: anchor &CustomRawAnchor = unsafe { nil } serial u64 } // One cold anchor serves all ordinary publications. Rotating it on counter // exhaustion avoids aliasing even if an old continuation still holds ID 1. fn (mut cohorts CustomRawCohorts) next() CustomRawPoint { if isnil(cohorts.anchor) || cohorts.serial == u64(max_i64) { cohorts.anchor = &CustomRawAnchor{ symbolic: true } cohorts.serial = 0 } cohorts.serial++ return CustomRawPoint{ valid: true, initial: cohorts.anchor, origin: integer(i64(cohorts.serial)) } }