Files
TheTree_mock/docs/plans/original-completion-2026-10-02/checkpoints/integer-window-codec-r290/p23-02-native-integer-window-codec-local-r292-r02.gd

226 lines
16 KiB
GDScript

extends "/Users/korizatou114/Projects/とりにく工房ゲーム制作部/artifacts/original-completion-2026-10-02/p23-02-build-preview-monitor-compressed-r06.gd"
const Budget := preload("res://scripts/core/performance_budget.gd")
class CustomBudget extends Budget:
func _percentile(_raw: Array, _percentile: int) -> int: return 17
func _aggregated_value(_aggregation: int, average: int, _p95: int, _maximum: int) -> int: return average + 13
var candidate := false
var core: RefCounted
var records: Array = []
var signals: Array = []
var reenter := false
var reenter_active := false
var record_calls := 0
var guard_calls := 0
var getter_calls := 0
var clear_calls := 0
func state(b: Variant) -> Array:
return [b._samples.duplicate(true), b._ordered_samples.duplicate(true), b._ordered_sample_bytes.duplicate(true), b._integer_window_bytes.duplicate(true), b._totals.duplicate(true), b._sample_counts.duplicate(true), b._budgets.duplicate(true), b.sample_capacity]
func capture(b: Variant, label: String, result: Variant) -> void:
records.append([label, result.duplicate(true) if result is Dictionary or result is Array else result, state(b), signals.duplicate(true)])
func record(b: Variant, metric: StringName, value: int, label: String, direct := false, expected_ok := true) -> void:
record_calls += 1
var before := state(b)
var had_raw: bool = b._samples.has(metric)
var raw: Array = b._samples.get(metric, [])
var ordered: Array = b._ordered_samples.get(metric, [])
var reuse: bool = b.get_script().resource_path == "res://scripts/core/performance_budget.gd" and b._ordered_samples.has(metric) and b._ordered_sample_bytes.get(metric, PackedByteArray()) == var_to_bytes(raw)
for v: Variant in raw: reuse = reuse and typeof(v) == TYPE_INT
var before_core: Variant = b._p23r257_native_core if candidate else null
var result: Dictionary
if direct:
var native: Dictionary
if candidate: native = core.call(&"performance_record_sample", b, metric, value)
else:
var original: Dictionary = b.record_sample(metric, value)
native = {&"supported": bool(original.get(&"ok", false)), &"report": original.get(&"report", {})}
check(bool(native.get(&"supported", false)), label + " direct builtin operation supported")
result = {&"ok": bool(native.get(&"supported", false)), &"report": native.get(&"report", {})}
else: result = b.record_sample(metric, value)
check(bool(result.get(&"ok", false)) == expected_ok, label + " result disposition")
check(not had_raw or is_same(raw, b._samples.get(metric)), label + " original raw alias preserved")
check(not reuse or is_same(ordered, b._ordered_samples.get(metric)), label + " existing valid ordered alias preserved")
var core_ok := true
if candidate:
var after_core: Variant = b._p23r257_native_core
core_ok = is_same(before_core, after_core) if before_core != null else (after_core == null if direct or not expected_ok or b.get_script().resource_path != "res://scripts/core/performance_budget.gd" else after_core != null)
check(core_ok, label + " native object ownership separately asserted")
check(expected_ok or var_to_bytes(before) == var_to_bytes(state(b)), label + " invalid input retains all diagnostic state")
capture(b, label, result)
func getter(b: Variant, metric: StringName, label: String, all_reports := false) -> void:
getter_calls += 1
var result: Variant = b.get_all_reports() if all_reports else b.get_report(metric)
check(result is Dictionary or result is Array, label + " public report shape")
capture(b, label, result)
func clear_capture(b: Variant, metric: StringName, label: String) -> void:
clear_calls += 1
var budgets_before: Dictionary = b._budgets.duplicate(true)
b.clear(metric)
var cleared := true
for m: Dictionary in [b._samples, b._ordered_samples, b._ordered_sample_bytes, b._integer_window_bytes, b._totals, b._sample_counts]:
cleared = cleared and (m.is_empty() if metric.is_empty() else not m.has(metric))
check(cleared and var_to_bytes(budgets_before) == var_to_bytes(b._budgets), label + " six measurement maps cleared and budget settings preserved")
capture(b, label, {})
func guard(b: Variant, metric: StringName, value: int, label: String) -> void:
guard_calls += 1
var before := state(b)
var maps: Array = [b._samples, b._ordered_samples, b._ordered_sample_bytes, b._integer_window_bytes, b._totals, b._sample_counts, b._budgets]
var refs: Array = [b._samples.get(metric), b._ordered_samples.get(metric), b._budgets.get(metric)]
var flags_before: Array = []
var nested_before: Array = []
for m: Dictionary in maps:
flags_before.append(m.is_read_only())
nested_before.append(is_same(refs[2], m))
for ref: Variant in refs: flags_before.append(ref.is_read_only() if ref is Array or ref is Dictionary else null)
var same_before: Array = []
for i: int in range(maps.size()):
for j: int in range(i + 1, maps.size()): same_before.append(is_same(maps[i], maps[j]))
var result: Dictionary = core.call(&"performance_record_sample", b, metric, value) if candidate else {&"supported": false}
check(result.size() == 1 and typeof(result.get(&"supported")) == TYPE_BOOL and not bool(result[&"supported"]), label + " unsupported before mutation")
var after_maps: Array = [b._samples, b._ordered_samples, b._ordered_sample_bytes, b._integer_window_bytes, b._totals, b._sample_counts, b._budgets]
var same_after: Array = []
for i: int in range(after_maps.size()):
for j: int in range(i + 1, after_maps.size()): same_after.append(is_same(after_maps[i], after_maps[j]))
var after_refs: Array = [b._samples.get(metric), b._ordered_samples.get(metric), b._budgets.get(metric)]
var flags_after: Array = []
var nested_after: Array = []
var identity_ok := true
for i: int in range(maps.size()):
identity_ok = identity_ok and is_same(maps[i], after_maps[i])
flags_after.append(after_maps[i].is_read_only())
nested_after.append(is_same(after_refs[2], after_maps[i]))
for i: int in range(refs.size()):
identity_ok = identity_ok and is_same(refs[i], after_refs[i])
flags_after.append(after_refs[i].is_read_only() if after_refs[i] is Array or after_refs[i] is Dictionary else null)
check(var_to_bytes(before) == var_to_bytes(state(b)) and same_before == same_after and nested_before == nested_after and flags_before == flags_after and identity_ok, label + " full values, map/nested/reference identity and readonly flags unchanged")
capture(b, label, {&"result": result, &"nested_before": nested_before, &"nested_after": nested_after, &"readonly_before": flags_before, &"readonly_after": flags_after, &"identity_preserved": identity_ok})
func on_budget(metric: StringName, report: Dictionary, b: Variant) -> void:
signals.append([metric, report.duplicate(true)])
report[&"p95_us"] = -9999
if reenter and not reenter_active:
reenter_active = true
var nested: Dictionary = b.record_sample(metric, 13)
signals.append(["nested_return", nested.duplicate(true)])
reenter_active = false
var codec_rows: Array = []
func codec_one(values: Array, label: String) -> void:
var before := var_to_bytes(values)
var before_typed := values.is_typed()
var before_builtin := values.get_typed_builtin()
var before_readonly := values.is_read_only()
var result: Dictionary = core.call(&"encode_integer_window", values)
check(result.size() == 2 and result.get(&"supported") == true and result.get(&"bytes") is PackedByteArray, label + " exact supported shape")
var encoded: PackedByteArray = result.get(&"bytes", PackedByteArray())
check(encoded == before, label + " actual engine codec exact bytes")
check(var_to_bytes(values) == before and values.is_typed() == before_typed and values.get_typed_builtin() == before_builtin and values.is_read_only() == before_readonly, label + " input value/type/readonly preserved")
var another: Dictionary = core.call(&"encode_integer_window", values)
check(another.get(&"bytes") == before and not is_same(result, another), label + " independent report owner")
if not encoded.is_empty(): encoded[0] = encoded[0] ^ 255
result[&"bytes"] = encoded
var after: Dictionary = core.call(&"encode_integer_window", values)
check(another.get(&"bytes") == before and after.get(&"bytes") == before and var_to_bytes(values) == before, label + " returned bytes mutation isolated from source and repeated result")
codec_rows.append([label, before_typed, before_builtin, before_readonly, before, another.get(&"bytes")])
func codec_unsupported(values: Array, label: String) -> void:
var before := var_to_bytes(values)
var flags := [values.is_typed(), values.get_typed_builtin(), values.is_read_only()]
var result: Dictionary = core.call(&"encode_integer_window", values)
check(result.size() == 1 and result.get(&"supported") == false, label + " unsupported shape")
check(var_to_bytes(values) == before and flags == [values.is_typed(), values.get_typed_builtin(), values.is_read_only()], label + " unsupported no input mutation")
var again: Dictionary = core.call(&"encode_integer_window", values)
check(again.size() == 1 and again.get(&"supported") == false and not is_same(result, again), label + " repeated unsupported independent owner")
codec_rows.append([label, flags, before, result])
func run_codec() -> Dictionary:
check(core.has_method(&"encode_integer_window"), "actual integer codec method registered")
var bounds: Array[int] = [(-9223372036854775807 - 1), -2147483649, -2147483648, -1, 0, 1, 2147483647, 2147483648, 9223372036854775807]
for count: int in [0, 1, 2, 9, 511, 512, 513]:
for is_typed: bool in [false, true]:
for readonly: bool in [false, true]:
var values: Array = []
if is_typed: values = Array([], TYPE_INT, &"", null)
for i: int in range(count): values.append(bounds[i % bounds.size()])
if count % 2 == 1: values.reverse()
if readonly: values.make_read_only()
codec_one(values, "codec/count%s/typed%s/readonly%s" % [count, is_typed, readonly])
for unsupported: Array in [[1, 2.0], ["1"], [null], [[1]]]: codec_unsupported(unsupported, "codec/unsupported/%s" % codec_rows.size())
var empty_floats: Array[float] = []
codec_unsupported(empty_floats, "codec/unsupported/typed-empty-float")
check(codec_rows.size() == 33, "exact 28 supported and five unsupported codec cases")
return typed("integer-codec-fixtures", codec_rows)
func run() -> void:
output = OS.get_environment("THE_TREE_RUNTIME_MONITOR_OUTPUT")
var mode := OS.get_environment("THE_TREE_BUDGET_MODE")
if not output.is_absolute_path() or mode not in ["r239", "r257-r04"]: quit(98); return
candidate = mode == "r257-r04"
core = ClassDB.instantiate(&"TheTreeNumericCore")
check(core != null and core.has_method(&"performance_record_sample") == candidate, "exact old/new API selected")
check(FileAccess.get_sha256("res://scripts/core/performance_budget.gd") == OS.get_environment("THE_TREE_EXPECTED_BUDGET_SHA"), "exact original/candidate budget loaded")
var metric := &"path_query"
var b = Budget.new(512)
for i: int in range(600): record(b, metric, (i * 37) % 10007, "rolling512/%03d" % i)
for capacity: int in [1, 3, 0, -1]:
for aggregation: int in [0, 1, 2, 9]:
b = Budget.new(3); b.sample_capacity = capacity
b._budgets[metric] = {&"aggregation": aggregation, &"limit_us": 2}
for v: int in [0, 1, 2, 100, 9223372036854775807, 3]: record(b, metric, v, "capacity%s/aggregation%s/value%s" % [capacity, aggregation, v])
for aggregation: int in range(3):
b = Budget.new(3); b.set_budget(metric, 2, aggregation)
for v: int in [1, 2, 3]: record(b, metric, v, "mutation%s/warm%s" % [aggregation, v])
b._samples[metric].reverse()
getter(b, metric, "mutation%s/read-after-in-place" % aggregation)
record(b, metric, 4, "mutation%s/in-place" % aggregation)
var ints: Array[int] = [1, 5, 9]; b._samples[metric] = ints
record(b, metric, 12, "mutation%s/typed-int" % aggregation)
ints.reverse(); record(b, metric, 13, "mutation%s/typed-int-alias-reverse" % aggregation)
b._samples[metric] = [1, 5.8, -9.2]; record(b, metric, 14, "mutation%s/mixed-float-fallback" % aggregation)
var floats: Array[float] = [1.0, 5.8, -9.2]; b._samples[metric] = floats
record(b, metric, 15, "mutation%s/typed-float-fallback" % aggregation)
b._samples[metric] = ["1", "5", "-9"]; record(b, metric, 16, "mutation%s/string-fallback" % aggregation)
b._samples[metric] = [1, 5, 9]; record(b, metric, 17, "mutation%s/back-to-ints" % aggregation)
clear_capture(b, metric, "mutation%s/clear-metric" % aggregation)
record(b, metric, 18, "mutation%s/after-clear" % aggregation)
clear_capture(b, &"", "mutation%s/clear-all" % aggregation)
for i: int in range(20): getter(b, StringName("unknown%s" % i), "mutation%s/unknown%s" % [aggregation, i])
getter(b, metric, "mutation%s/all-reports" % aggregation, true)
for aggregation: int in range(3):
for limits: Array in [[9223372036854775806, 2, 4], [(-9223372036854775807 - 1), 3, 1], [0, 9223372036854775807, 0]]:
b = Budget.new(3); b.set_budget(metric, 2, aggregation); b._totals[metric] = int(limits[0]); b._sample_counts[metric] = int(limits[1])
record(b, metric, int(limits[2]), "lifetime%s/%s" % [aggregation, limits])
b = Budget.new(3); record(b, &"", 1, "invalid/empty-id", false, false); record(b, metric, -1, "invalid/negative", false, false)
for field: StringName in [&"_totals", &"_sample_counts"]:
b = Budget.new(3); b.get(field)[&"limit_us"] = 100; b._budgets[&"limit_us"] = b.get(field)
guard(b, &"limit_us", 1, "nested-budget-alias/%s/guard" % field)
record(b, &"limit_us", 1, "nested-budget-alias/%s/public" % field)
b = Budget.new(3); b._integer_window_bytes = b._ordered_sample_bytes
guard(b, metric, 7, "owner-byte-map-alias/guard"); record(b, metric, 7, "owner-byte-map-alias/public")
b = Budget.new(3); b._sample_counts = b._totals
guard(b, metric, 7, "owner-total-count-alias/guard"); record(b, metric, 7, "owner-total-count-alias/public")
b = Budget.new(3); b._samples[metric] = [1, 2]; b._samples[metric].make_read_only()
guard(b, metric, 7, "raw-readonly/guard")
b = Budget.new(3); b._sample_counts.make_read_only(); guard(b, metric, 7, "dictionary-readonly/guard")
b = Budget.new(3); b._samples[metric] = [1, 2.0]; guard(b, metric, 7, "noninteger/guard")
b = Budget.new(3); b._samples[metric] = [1, 2]; var malformed: Array[int] = [1]; b._ordered_samples[metric] = malformed; b._ordered_sample_bytes[metric] = var_to_bytes(b._samples[metric])
guard(b, metric, 7, "ordered-size/guard")
for nested: bool in [false, true]:
b = Budget.new(3); b.set_budget(metric, 1, 1); signals.clear(); reenter = nested; reenter_active = false
b.budget_exceeded.connect(Callable(self, "on_budget").bind(b))
record(b, metric, 7, "signal/reentry%s" % nested)
check(int(b._sample_counts[metric]) == (2 if nested else 1) and int(records[-1][1][&"report"][&"p95_us"]) == 7, "signal copy isolated and nested lifetime count")
b.budget_exceeded.disconnect(Callable(self, "on_budget").bind(b)); signals.clear(); reenter = false
b = Budget.new(3)
if candidate: b._p23r257_native_core = RefCounted.new()
record(b, metric, 5, "generic-core-fallback")
b = CustomBudget.new(3)
for v: int in [5, 1, 8]: record(b, metric, v, "custom-script/%s" % v)
b = Budget.new(3)
for v: int in [0, 9223372036854775807, 1]: record(b, metric, v, "direct-supported/%s" % v, true)
check(record_calls == 753, "exact record operation count")
check(guard_calls == 8, "exact unsupported guard count")
check(getter_calls == 66, "exact public getter count")
check(clear_calls == 6, "exact clear count")
var fixtures := typed("budget-local-fixtures", records)
var codec_fixtures := run_codec()
var report := {"plan_id":"P23-02", "mode":mode, "pid":OS.get_process_id(), "checks":checks, "failures":failures, "passed":failures.is_empty(), "record_calls":record_calls, "guard_calls":guard_calls, "getter_calls":getter_calls, "clear_calls":clear_calls, "case_count":records.size(), "fixtures":fixtures, "codec_cases":codec_rows.size(), "codec_fixtures":codec_fixtures, "candidate_adopted":false, "unit_accepted":false, "performance_accepted":false, "limits":["Original239/240 accepted r260-r02 fixture reused versus fresh candidate290/library291. Exact original record fixture body preserved; codec fixtures are separate. Full raw/order/bytes/totals/counts/budgets/capacity/report/signal trace bytes paired; native core object excluded from serialization and separately asserted. Guard-only malformed/read-only states prove native unsupported/no mutation; no original error-path execution or arbitrary state acceptance.", "No whole647, final172, performance, realtime x3/OS/Windows or adoption acceptance."]}
var f := FileAccess.open(output.path_join("report.json"), FileAccess.WRITE)
if f == null: quit(98); return
f.store_string(JSON.stringify(report, "\t")); f.close()
print("Native Budget record local: %d checks, %d failures" % [checks, failures.size()]); quit(0 if failures.is_empty() else 1)