thekevinscott commited on
Commit
fb612f0
·
verified ·
1 Parent(s): a1d45ab

Upload experiments v1, v2-models-effort, v2-tests-configuration as on disk 2026-11-11 (part 38)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. v2-models-effort/runs/20261017T153358Z_417c8e22/run.json +10 -0
  2. v2-models-effort/runs/20261017T153358Z_417c8e22/stderr.txt +0 -0
  3. v2-models-effort/runs/20261017T153358Z_417c8e22/transcript/.claude/projects/-target/c36440b8-a824-4dd1-8283-8e3a5c6cb1b9.jsonl +0 -0
  4. v2-models-effort/runs/20261017T153618Z_9dd9c336/manifest.json +99 -0
  5. v2-models-effort/runs/20261017T153618Z_9dd9c336/proxy.log +35 -0
  6. v2-models-effort/runs/20261017T153618Z_9dd9c336/result.json +1 -0
  7. v2-models-effort/runs/20261017T153618Z_9dd9c336/run.json +10 -0
  8. v2-models-effort/runs/20261017T153618Z_9dd9c336/stderr.txt +0 -0
  9. v2-models-effort/runs/20261017T153618Z_9dd9c336/transcript/.claude/projects/-target/426654a1-ed55-46ac-aa9c-1446ce834784.jsonl +0 -0
  10. v2-models-effort/runs/20261017T153819Z_dc2cffdb/manifest.json +105 -0
  11. v2-models-effort/runs/20261017T153819Z_dc2cffdb/proxy.log +35 -0
  12. v2-models-effort/runs/20261017T153819Z_dc2cffdb/result.json +1 -0
  13. v2-models-effort/runs/20261017T153819Z_dc2cffdb/run.json +10 -0
  14. v2-models-effort/runs/20261017T153819Z_dc2cffdb/stderr.txt +0 -0
  15. v2-models-effort/runs/20261017T153819Z_dc2cffdb/transcript/.claude/projects/-target/2bbd7a97-c800-4430-8342-666e10a142f5.jsonl +0 -0
  16. v2-models-effort/runs/20261017T174414Z_0973797c/manifest.json +99 -0
  17. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/.gitignore +3 -0
  18. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/README.md +69 -0
  19. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/GBNF.test.ts +55 -0
  20. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/GBNF.ts +29 -0
  21. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/colorize.test.ts +16 -0
  22. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/colorize.ts +14 -0
  23. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-input-as-code-points.test.ts +20 -0
  24. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-input-as-code-points.ts +30 -0
  25. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-parent-stack-id.test.ts +56 -0
  26. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-parent-stack-id.ts +14 -0
  27. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-serialized-rule-key.test.ts +35 -0
  28. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-serialized-rule-key.ts +29 -0
  29. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/grammar-graph-types.test.ts +28 -0
  30. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/grammar-graph-types.ts +94 -0
  31. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-node.test.ts +51 -0
  32. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-node.ts +35 -0
  33. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-pointer.test.ts +84 -0
  34. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-pointer.ts +91 -0
  35. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph.test.ts +84 -0
  36. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph.ts +226 -0
  37. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/parse-state.test.ts +68 -0
  38. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/parse-state.ts +45 -0
  39. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/pointers.ts +23 -0
  40. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/print.test.ts +83 -0
  41. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/print.ts +64 -0
  42. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/rule-ref.test.ts +22 -0
  43. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/rule-ref.ts +33 -0
  44. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/type-guards.test.ts +129 -0
  45. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/type-guards.ts +60 -0
  46. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-parser/build-rule-stack.test.ts +338 -0
  47. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-parser/build-rule-stack.ts +96 -0
  48. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/index.ts +11 -0
  49. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/rules-builder/index.ts +2 -0
  50. v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/rules-builder/is-word-char.test.ts +27 -0
v2-models-effort/runs/20261017T153358Z_417c8e22/run.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "params": {
3
+ "source-language": "python",
4
+ "model": "claude-fable-5-1",
5
+ "effort": "low"
6
+ },
7
+ "created": "2026-10-17T15:33:58Z",
8
+ "ended": "2026-10-17T15:40:08Z",
9
+ "exit_code": 1
10
+ }
v2-models-effort/runs/20261017T153358Z_417c8e22/stderr.txt ADDED
File without changes
v2-models-effort/runs/20261017T153358Z_417c8e22/transcript/.claude/projects/-target/c36440b8-a824-4dd1-8283-8e3a5c6cb1b9.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
v2-models-effort/runs/20261017T153618Z_9dd9c336/manifest.json ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "timestamp": "2026-10-17T15:36:18Z",
3
+ "completed_at": "2026-10-17T15:40:44Z",
4
+ "exit_code": 1,
5
+ "study": "v2-model-effort",
6
+ "condition": {
7
+ "name": "source-python_unit-tests_source-integration-tests_effort-high_model-claude-haiku-5-5",
8
+ "source_language": "python",
9
+ "target_language": "javascript",
10
+ "include_unit_tests": true,
11
+ "include_source_integration_tests": true,
12
+ "include_target_integration_tests": false,
13
+ "effort": "high",
14
+ "model": "claude-haiku-5-5"
15
+ },
16
+ "derivation": {
17
+ "gbnf_commit": "13f1aca495d11e160fffd68c4ba299a2415909d8"
18
+ },
19
+ "reference_implementation": {
20
+ "included": [
21
+ "python/.gitignore",
22
+ "python/MANIFEST.in",
23
+ "python/Makefile",
24
+ "python/README.md",
25
+ "python/gbnf/GBNF.py",
26
+ "python/gbnf/__init__.py",
27
+ "python/gbnf/grammar_graph/__init__.py",
28
+ "python/gbnf/grammar_graph/colorize.py",
29
+ "python/gbnf/grammar_graph/colorize_test.py",
30
+ "python/gbnf/grammar_graph/get_input_as_code_points.py",
31
+ "python/gbnf/grammar_graph/get_input_as_code_points_test.py",
32
+ "python/gbnf/grammar_graph/get_parent_stack_id.py",
33
+ "python/gbnf/grammar_graph/get_parent_stack_id_test.py",
34
+ "python/gbnf/grammar_graph/get_serialized_rule_key.py",
35
+ "python/gbnf/grammar_graph/get_serialized_rule_key_test.py",
36
+ "python/gbnf/grammar_graph/grammar_graph_types.py",
37
+ "python/gbnf/grammar_graph/graph.py",
38
+ "python/gbnf/grammar_graph/graph_node.py",
39
+ "python/gbnf/grammar_graph/graph_node_test.py",
40
+ "python/gbnf/grammar_graph/graph_pointer.py",
41
+ "python/gbnf/grammar_graph/graph_pointer_test.py",
42
+ "python/gbnf/grammar_graph/graph_test.py",
43
+ "python/gbnf/grammar_graph/parse_state.py",
44
+ "python/gbnf/grammar_graph/parse_state_test.py",
45
+ "python/gbnf/grammar_graph/pointers.py",
46
+ "python/gbnf/grammar_graph/print.py",
47
+ "python/gbnf/grammar_graph/print_test.py",
48
+ "python/gbnf/grammar_graph/rule_ref.py",
49
+ "python/gbnf/grammar_graph/rule_ref_test.py",
50
+ "python/gbnf/grammar_graph/type_guards.py",
51
+ "python/gbnf/grammar_graph/type_guards_test.py",
52
+ "python/gbnf/grammar_parser/__init__.py",
53
+ "python/gbnf/grammar_parser/build_rule_stack.py",
54
+ "python/gbnf/grammar_parser/build_rule_stack_test.py",
55
+ "python/gbnf/rules_builder/__init__.py",
56
+ "python/gbnf/rules_builder/is_word_char.py",
57
+ "python/gbnf/rules_builder/is_word_char_test.py",
58
+ "python/gbnf/rules_builder/parse_char.py",
59
+ "python/gbnf/rules_builder/parse_char_test.py",
60
+ "python/gbnf/rules_builder/parse_name.py",
61
+ "python/gbnf/rules_builder/parse_name_test.py",
62
+ "python/gbnf/rules_builder/parse_space.py",
63
+ "python/gbnf/rules_builder/parse_space_test.py",
64
+ "python/gbnf/rules_builder/rules_builder.py",
65
+ "python/gbnf/rules_builder/rules_builder_test.py",
66
+ "python/gbnf/rules_builder/rules_builder_types.py",
67
+ "python/gbnf/rules_builder/symbol_ids.py",
68
+ "python/gbnf/utils/__init__.py",
69
+ "python/gbnf/utils/errors/__init__.py",
70
+ "python/gbnf/utils/errors/build_error_position.py",
71
+ "python/gbnf/utils/errors/build_error_position_test.py",
72
+ "python/gbnf/utils/errors/errors_types.py",
73
+ "python/gbnf/utils/errors/get_input_as_string.py",
74
+ "python/gbnf/utils/errors/get_input_as_string_test.py",
75
+ "python/gbnf/utils/errors/grammar_parse_error.py",
76
+ "python/gbnf/utils/errors/grammar_parse_error_test.py",
77
+ "python/gbnf/utils/errors/input_parse_error.py",
78
+ "python/gbnf/utils/errors/input_parse_error_test.py",
79
+ "python/gbnf/utils/is_point_in_range.py",
80
+ "python/gbnf/utils/is_point_in_range_test.py",
81
+ "python/gbnf/utils/validate_non_empty.py",
82
+ "python/pyproject.toml",
83
+ "python/requirements.txt",
84
+ "python/tests/generated/iteration/iteration_test.py",
85
+ "python/tests/generated/iteration/iteration_with_additional_strings_test.py",
86
+ "python/tests/generated/iteration/iteration_with_an_initial_string_test.py",
87
+ "python/tests/generated/validation/validate_grammar_test.py",
88
+ "python/tests/generated/validation/validate_input_test.py",
89
+ "python/uv.lock"
90
+ ]
91
+ },
92
+ "sandbox": {
93
+ "image_id": "sha256:91efe595dc5032aa5583be9919f97a98422e87291992b5ab66c8a8b851a029b2"
94
+ },
95
+ "harness": {
96
+ "commit": "9e24e21259430f5e40bca73c392757016eda7cda",
97
+ "dirty": true
98
+ }
99
+ }
v2-models-effort/runs/20261017T153618Z_9dd9c336/proxy.log ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ NOTICE Oct 17 15:36:30.698 [1]: Initializing tinyproxy ...
2
+ NOTICE Oct 17 15:36:30.699 [1]: Reloading config file (/etc/tinyproxy/tinyproxy.conf)
3
+ INFO Oct 17 15:36:30.700 [1]: Added address [0.0.0.0] to listen addresses.
4
+ INFO Oct 17 15:36:30.702 [1]: Adding Port [443] to the list allowed by CONNECT
5
+ NOTICE Oct 17 15:36:30.703 [1]: Reloading config file finished
6
+ INFO Oct 17 15:36:30.705 [1]: listen_sock called with addr = '0.0.0.0'
7
+ INFO Oct 17 15:36:30.706 [1]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
8
+ INFO Oct 17 15:36:30.707 [1]: listening on fd [3]
9
+ INFO Oct 17 15:36:30.708 [1]: Not running as root, so not changing UID/GID.
10
+ INFO Oct 17 15:36:30.710 [1]: Setting the various signals.
11
+ INFO Oct 17 15:36:30.711 [1]: Starting main loop. Accepting connections.
12
+ CONNECT Oct 17 15:36:31.035 [1]: Connect (file descriptor 5): 172.31.0.3
13
+ CONNECT Oct 17 15:36:31.036 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1
14
+ INFO Oct 17 15:36:31.039 [1]: No upstream proxy for api.anthropic.com
15
+ INFO Oct 17 15:36:31.041 [1]: opensock: opening connection to api.anthropic.com:443
16
+ INFO Oct 17 15:36:31.043 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
17
+ CONNECT Oct 17 15:36:31.093 [1]: Established connection to host "api.anthropic.com" using file descriptor 6.
18
+ INFO Oct 17 15:36:31.094 [1]: Not sending client headers to remote machine
19
+ CONNECT Oct 17 15:36:31.166 [1]: Connect (file descriptor 7): 172.31.0.3
20
+ CONNECT Oct 17 15:36:31.169 [1]: Request (file descriptor 7): CONNECT api.anthropic.com:443 HTTP/1.1
21
+ INFO Oct 17 15:36:31.170 [1]: No upstream proxy for api.anthropic.com
22
+ INFO Oct 17 15:36:31.171 [1]: opensock: opening connection to api.anthropic.com:443
23
+ INFO Oct 17 15:36:31.174 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
24
+ CONNECT Oct 17 15:36:31.181 [1]: Established connection to host "api.anthropic.com" using file descriptor 8.
25
+ INFO Oct 17 15:36:31.182 [1]: Not sending client headers to remote machine
26
+ CONNECT Oct 17 15:36:31.205 [1]: Connect (file descriptor 9): 172.31.0.3
27
+ CONNECT Oct 17 15:36:31.207 [1]: Request (file descriptor 9): CONNECT api.anthropic.com:443 HTTP/1.1
28
+ INFO Oct 17 15:36:31.208 [1]: No upstream proxy for api.anthropic.com
29
+ INFO Oct 17 15:36:31.210 [1]: opensock: opening connection to api.anthropic.com:443
30
+ INFO Oct 17 15:36:31.211 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
31
+ CONNECT Oct 17 15:36:31.218 [1]: Established connection to host "api.anthropic.com" using file descriptor 10.
32
+ INFO Oct 17 15:36:31.219 [1]: Not sending client headers to remote machine
33
+ INFO Oct 17 15:36:31.373 [1]: Closed connection between local client (fd:5) and remote client (fd:6)
34
+ INFO Oct 17 15:36:31.468 [1]: Closed connection between local client (fd:7) and remote client (fd:8)
35
+ INFO Oct 17 15:40:43.341 [1]: Closed connection between local client (fd:9) and remote client (fd:10)
v2-models-effort/runs/20261017T153618Z_9dd9c336/result.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"duration_api_ms":251447,"stop_reason":"stop_sequence","session_id":"426654a1-ed55-46ac-aa9c-1446ce834784","total_cost_usd":0.040655510000000006,"usage":{"input_tokens":16,"cache_creation_input_tokens":40846,"cache_read_input_tokens":261021,"output_tokens":59749,"output_tokens_details":{"thinking_tokens":57030},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":40846,"ephemeral_5m_input_tokens":0},"inference_geo":"global","iterations":[],"speed":"standard","fallback_credit":null},"modelUsage":{"claude-haiku-5-5":{"inputTokens":16,"outputTokens":59749,"cacheReadInputTokens":261021,"cacheCreationInputTokens":40846,"webSearchRequests":0,"costUSD":0.040655510000000006,"contextWindow":1000000,"maxOutputTokens":128000,"thinkingTokens":57030,"canonicalModel":"claude-haiku-5-5","provider":"firstParty","costBasis":"list"}},"permission_denials":[],"terminal_reason":"api_error","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","subagent_stats":{"spawned":0,"requested":{"background":0,"foreground":0,"unset":0},"started_in_background":0,"max_depth":0,"spawned_by_subagents":0,"completed":0,"failed":0,"killed":{"parent":0,"user":0,"system":0},"refused":{"depth_limit":0,"concurrency_limit":0,"budget":0},"by_type":{}},"safety_stops":0,"is_error":true,"num_turns":10,"subtype":"success","api_error_status":429,"api_error":"usage_limit_reached","result":"You've hit your monthly spend limit · raise it at https://claude.ai/settings/usage?from=cc_cli_limit_message · your session limit resets 5:40pm (UTC)","type":"result","duration_ms":252168,"uuid":"3ffbb395-de14-4a8a-94d3-c85f9e3a6825","queued_turn_count":0,"result_index":0}
v2-models-effort/runs/20261017T153618Z_9dd9c336/run.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "params": {
3
+ "source-language": "python",
4
+ "model": "claude-haiku-5-5",
5
+ "effort": "high"
6
+ },
7
+ "created": "2026-10-17T15:36:18Z",
8
+ "ended": "2026-10-17T15:40:44Z",
9
+ "exit_code": 1
10
+ }
v2-models-effort/runs/20261017T153618Z_9dd9c336/stderr.txt ADDED
File without changes
v2-models-effort/runs/20261017T153618Z_9dd9c336/transcript/.claude/projects/-target/426654a1-ed55-46ac-aa9c-1446ce834784.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
v2-models-effort/runs/20261017T153819Z_dc2cffdb/manifest.json ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "timestamp": "2026-10-17T15:38:19Z",
3
+ "completed_at": "2026-10-17T15:40:18Z",
4
+ "exit_code": 1,
5
+ "study": "v2-model-effort",
6
+ "condition": {
7
+ "name": "source-javascript_unit-tests_source-integration-tests_effort-low_model-claude-fable-5-1",
8
+ "source_language": "javascript",
9
+ "target_language": "python",
10
+ "include_unit_tests": true,
11
+ "include_source_integration_tests": true,
12
+ "include_target_integration_tests": false,
13
+ "effort": "low",
14
+ "model": "claude-fable-5-1"
15
+ },
16
+ "derivation": {
17
+ "gbnf_commit": "13f1aca495d11e160fffd68c4ba299a2415909d8"
18
+ },
19
+ "reference_implementation": {
20
+ "included": [
21
+ "javascript/.eslintrc.cjs",
22
+ "javascript/.gitignore",
23
+ "javascript/.npmignore",
24
+ "javascript/README.md",
25
+ "javascript/integration-tests/.gitignore",
26
+ "javascript/integration-tests/generated/iteration/grammars.test.ts",
27
+ "javascript/integration-tests/generated/iteration/iteration-with-additional-strings.test.ts",
28
+ "javascript/integration-tests/generated/iteration/iteration-with-an-initial-string.test.ts",
29
+ "javascript/integration-tests/generated/iteration/iteration.test.ts",
30
+ "javascript/integration-tests/generated/validation/validate-grammar.test.ts",
31
+ "javascript/integration-tests/generated/validation/validate-input.test.ts",
32
+ "javascript/package.json",
33
+ "javascript/src/gbnf.test.ts",
34
+ "javascript/src/gbnf.ts",
35
+ "javascript/src/grammar-graph/colorize.test.ts",
36
+ "javascript/src/grammar-graph/colorize.ts",
37
+ "javascript/src/grammar-graph/generic-set.test.ts",
38
+ "javascript/src/grammar-graph/generic-set.ts",
39
+ "javascript/src/grammar-graph/get-input-as-code-points.test.ts",
40
+ "javascript/src/grammar-graph/get-input-as-code-points.ts",
41
+ "javascript/src/grammar-graph/get-parent-stack-id.test.ts",
42
+ "javascript/src/grammar-graph/get-parent-stack-id.ts",
43
+ "javascript/src/grammar-graph/get-serialized-rule-key.test.ts",
44
+ "javascript/src/grammar-graph/get-serialized-rule-key.ts",
45
+ "javascript/src/grammar-graph/graph-node.test.ts",
46
+ "javascript/src/grammar-graph/graph-node.ts",
47
+ "javascript/src/grammar-graph/graph-pointer.test.ts",
48
+ "javascript/src/grammar-graph/graph-pointer.ts",
49
+ "javascript/src/grammar-graph/graph.test.ts",
50
+ "javascript/src/grammar-graph/graph.ts",
51
+ "javascript/src/grammar-graph/index.ts",
52
+ "javascript/src/grammar-graph/parse-state.test.ts",
53
+ "javascript/src/grammar-graph/parse-state.ts",
54
+ "javascript/src/grammar-graph/print.test.ts",
55
+ "javascript/src/grammar-graph/print.ts",
56
+ "javascript/src/grammar-graph/rule-ref.test.ts",
57
+ "javascript/src/grammar-graph/rule-ref.ts",
58
+ "javascript/src/grammar-graph/type-guards.test.ts",
59
+ "javascript/src/grammar-graph/type-guards.ts",
60
+ "javascript/src/grammar-graph/types.ts",
61
+ "javascript/src/grammar-parser/build-rule-stack.test.ts",
62
+ "javascript/src/grammar-parser/build-rule-stack.ts",
63
+ "javascript/src/index.ts",
64
+ "javascript/src/rules-builder/index.ts",
65
+ "javascript/src/rules-builder/is-word-char.test.ts",
66
+ "javascript/src/rules-builder/is-word-char.ts",
67
+ "javascript/src/rules-builder/parse-char.test.ts",
68
+ "javascript/src/rules-builder/parse-char.ts",
69
+ "javascript/src/rules-builder/parse-name.test.ts",
70
+ "javascript/src/rules-builder/parse-name.ts",
71
+ "javascript/src/rules-builder/parse-space.test.ts",
72
+ "javascript/src/rules-builder/parse-space.ts",
73
+ "javascript/src/rules-builder/rules-builder.test.ts",
74
+ "javascript/src/rules-builder/rules-builder.ts",
75
+ "javascript/src/rules-builder/symbol-ids.ts",
76
+ "javascript/src/rules-builder/type-guards.ts",
77
+ "javascript/src/rules-builder/types.ts",
78
+ "javascript/src/umd.ts",
79
+ "javascript/src/utils/errors/build-error-position.test.ts",
80
+ "javascript/src/utils/errors/build-error-position.ts",
81
+ "javascript/src/utils/errors/get-input-as-string.test.ts",
82
+ "javascript/src/utils/errors/get-input-as-string.ts",
83
+ "javascript/src/utils/errors/grammar-parse-error.test.ts",
84
+ "javascript/src/utils/errors/grammar-parse-error.ts",
85
+ "javascript/src/utils/errors/input-parse-error.test.ts",
86
+ "javascript/src/utils/errors/input-parse-error.ts",
87
+ "javascript/src/utils/is-point-in-range.test.ts",
88
+ "javascript/src/utils/is-point-in-range.ts",
89
+ "javascript/tsconfig.json",
90
+ "javascript/tsconfig.test.json",
91
+ "javascript/vite.config.esm.ts",
92
+ "javascript/vite.config.ts",
93
+ "javascript/vite.config.umd.ts",
94
+ "javascript/vitest.config.integration.ts",
95
+ "javascript/vitest.config.unit.ts"
96
+ ]
97
+ },
98
+ "sandbox": {
99
+ "image_id": "sha256:91efe595dc5032aa5583be9919f97a98422e87291992b5ab66c8a8b851a029b2"
100
+ },
101
+ "harness": {
102
+ "commit": "9e24e21259430f5e40bca73c392757016eda7cda",
103
+ "dirty": true
104
+ }
105
+ }
v2-models-effort/runs/20261017T153819Z_dc2cffdb/proxy.log ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ NOTICE Oct 17 15:38:31.000 [1]: Initializing tinyproxy ...
2
+ NOTICE Oct 17 15:38:31.002 [1]: Reloading config file (/etc/tinyproxy/tinyproxy.conf)
3
+ INFO Oct 17 15:38:31.003 [1]: Added address [0.0.0.0] to listen addresses.
4
+ INFO Oct 17 15:38:31.004 [1]: Adding Port [443] to the list allowed by CONNECT
5
+ NOTICE Oct 17 15:38:31.006 [1]: Reloading config file finished
6
+ INFO Oct 17 15:38:31.007 [1]: listen_sock called with addr = '0.0.0.0'
7
+ INFO Oct 17 15:38:31.008 [1]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
8
+ INFO Oct 17 15:38:31.010 [1]: listening on fd [3]
9
+ INFO Oct 17 15:38:31.011 [1]: Not running as root, so not changing UID/GID.
10
+ INFO Oct 17 15:38:31.012 [1]: Setting the various signals.
11
+ INFO Oct 17 15:38:31.013 [1]: Starting main loop. Accepting connections.
12
+ CONNECT Oct 17 15:38:31.279 [1]: Connect (file descriptor 5): 192.168.32.3
13
+ CONNECT Oct 17 15:38:31.280 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1
14
+ INFO Oct 17 15:38:31.282 [1]: No upstream proxy for api.anthropic.com
15
+ INFO Oct 17 15:38:31.285 [1]: opensock: opening connection to api.anthropic.com:443
16
+ INFO Oct 17 15:38:31.290 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
17
+ CONNECT Oct 17 15:38:31.397 [1]: Connect (file descriptor 7): 192.168.32.3
18
+ CONNECT Oct 17 15:38:31.398 [1]: Request (file descriptor 7): CONNECT api.anthropic.com:443 HTTP/1.1
19
+ INFO Oct 17 15:38:31.399 [1]: No upstream proxy for api.anthropic.com
20
+ INFO Oct 17 15:38:31.401 [1]: opensock: opening connection to api.anthropic.com:443
21
+ CONNECT Oct 17 15:38:31.402 [1]: Established connection to host "api.anthropic.com" using file descriptor 6.
22
+ INFO Oct 17 15:38:31.404 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
23
+ INFO Oct 17 15:38:31.404 [1]: Not sending client headers to remote machine
24
+ CONNECT Oct 17 15:38:31.411 [1]: Established connection to host "api.anthropic.com" using file descriptor 8.
25
+ INFO Oct 17 15:38:31.412 [1]: Not sending client headers to remote machine
26
+ CONNECT Oct 17 15:38:31.436 [1]: Connect (file descriptor 9): 192.168.32.3
27
+ CONNECT Oct 17 15:38:31.438 [1]: Request (file descriptor 9): CONNECT api.anthropic.com:443 HTTP/1.1
28
+ INFO Oct 17 15:38:31.439 [1]: No upstream proxy for api.anthropic.com
29
+ INFO Oct 17 15:38:31.441 [1]: opensock: opening connection to api.anthropic.com:443
30
+ INFO Oct 17 15:38:31.443 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
31
+ CONNECT Oct 17 15:38:31.449 [1]: Established connection to host "api.anthropic.com" using file descriptor 10.
32
+ INFO Oct 17 15:38:31.451 [1]: Not sending client headers to remote machine
33
+ INFO Oct 17 15:38:31.620 [1]: Closed connection between local client (fd:5) and remote client (fd:6)
34
+ INFO Oct 17 15:38:31.632 [1]: Closed connection between local client (fd:7) and remote client (fd:8)
35
+ INFO Oct 17 15:40:17.965 [1]: Closed connection between local client (fd:9) and remote client (fd:10)
v2-models-effort/runs/20261017T153819Z_dc2cffdb/result.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"duration_api_ms":105762,"stop_reason":"stop_sequence","session_id":"2bbd7a97-c800-4430-8342-666e10a142f5","total_cost_usd":1.8303509999999998,"usage":{"input_tokens":418,"cache_creation_input_tokens":64296,"cache_read_input_tokens":590404,"output_tokens":7853,"output_tokens_details":{"thinking_tokens":4724},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":64296,"ephemeral_5m_input_tokens":0},"inference_geo":"global","iterations":[],"speed":"standard","fallback_credit":null},"modelUsage":{"claude-fable-5-1":{"inputTokens":418,"outputTokens":7853,"cacheReadInputTokens":590404,"cacheCreationInputTokens":64296,"webSearchRequests":0,"costUSD":1.8303509999999998,"contextWindow":1000000,"maxOutputTokens":64000,"thinkingTokens":4724,"canonicalModel":"claude-fable-5-1","provider":"firstParty","costBasis":"list"}},"permission_denials":[],"terminal_reason":"api_error","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","subagent_stats":{"spawned":0,"requested":{"background":0,"foreground":0,"unset":0},"started_in_background":0,"max_depth":0,"spawned_by_subagents":0,"completed":0,"failed":0,"killed":{"parent":0,"user":0,"system":0},"refused":{"depth_limit":0,"concurrency_limit":0,"budget":0},"by_type":{}},"safety_stops":0,"is_error":true,"num_turns":15,"subtype":"success","api_error_status":429,"api_error":"usage_limit_reached","result":"You've hit your monthly spend limit · raise it at https://claude.ai/settings/usage?from=cc_cli_limit_message · your session limit resets 5:40pm (UTC)","type":"result","duration_ms":106562,"uuid":"ba67bddd-75bf-4f67-991c-47f6acbbdde2","queued_turn_count":0,"result_index":0}
v2-models-effort/runs/20261017T153819Z_dc2cffdb/run.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "params": {
3
+ "source-language": "javascript",
4
+ "model": "claude-fable-5-1",
5
+ "effort": "low"
6
+ },
7
+ "created": "2026-10-17T15:38:19Z",
8
+ "ended": "2026-10-17T15:40:18Z",
9
+ "exit_code": 1
10
+ }
v2-models-effort/runs/20261017T153819Z_dc2cffdb/stderr.txt ADDED
File without changes
v2-models-effort/runs/20261017T153819Z_dc2cffdb/transcript/.claude/projects/-target/2bbd7a97-c800-4430-8342-666e10a142f5.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
v2-models-effort/runs/20261017T174414Z_0973797c/manifest.json ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "timestamp": "2026-10-17T17:44:14Z",
3
+ "completed_at": "2026-10-17T18:13:23Z",
4
+ "exit_code": 0,
5
+ "study": "v2-model-effort",
6
+ "condition": {
7
+ "name": "source-python_unit-tests_source-integration-tests_effort-xhigh_model-claude-haiku-5-5",
8
+ "source_language": "python",
9
+ "target_language": "javascript",
10
+ "include_unit_tests": true,
11
+ "include_source_integration_tests": true,
12
+ "include_target_integration_tests": false,
13
+ "effort": "xhigh",
14
+ "model": "claude-haiku-5-5"
15
+ },
16
+ "derivation": {
17
+ "gbnf_commit": "13f1aca495d11e160fffd68c4ba299a2415909d8"
18
+ },
19
+ "reference_implementation": {
20
+ "included": [
21
+ "python/.gitignore",
22
+ "python/MANIFEST.in",
23
+ "python/Makefile",
24
+ "python/README.md",
25
+ "python/gbnf/GBNF.py",
26
+ "python/gbnf/__init__.py",
27
+ "python/gbnf/grammar_graph/__init__.py",
28
+ "python/gbnf/grammar_graph/colorize.py",
29
+ "python/gbnf/grammar_graph/colorize_test.py",
30
+ "python/gbnf/grammar_graph/get_input_as_code_points.py",
31
+ "python/gbnf/grammar_graph/get_input_as_code_points_test.py",
32
+ "python/gbnf/grammar_graph/get_parent_stack_id.py",
33
+ "python/gbnf/grammar_graph/get_parent_stack_id_test.py",
34
+ "python/gbnf/grammar_graph/get_serialized_rule_key.py",
35
+ "python/gbnf/grammar_graph/get_serialized_rule_key_test.py",
36
+ "python/gbnf/grammar_graph/grammar_graph_types.py",
37
+ "python/gbnf/grammar_graph/graph.py",
38
+ "python/gbnf/grammar_graph/graph_node.py",
39
+ "python/gbnf/grammar_graph/graph_node_test.py",
40
+ "python/gbnf/grammar_graph/graph_pointer.py",
41
+ "python/gbnf/grammar_graph/graph_pointer_test.py",
42
+ "python/gbnf/grammar_graph/graph_test.py",
43
+ "python/gbnf/grammar_graph/parse_state.py",
44
+ "python/gbnf/grammar_graph/parse_state_test.py",
45
+ "python/gbnf/grammar_graph/pointers.py",
46
+ "python/gbnf/grammar_graph/print.py",
47
+ "python/gbnf/grammar_graph/print_test.py",
48
+ "python/gbnf/grammar_graph/rule_ref.py",
49
+ "python/gbnf/grammar_graph/rule_ref_test.py",
50
+ "python/gbnf/grammar_graph/type_guards.py",
51
+ "python/gbnf/grammar_graph/type_guards_test.py",
52
+ "python/gbnf/grammar_parser/__init__.py",
53
+ "python/gbnf/grammar_parser/build_rule_stack.py",
54
+ "python/gbnf/grammar_parser/build_rule_stack_test.py",
55
+ "python/gbnf/rules_builder/__init__.py",
56
+ "python/gbnf/rules_builder/is_word_char.py",
57
+ "python/gbnf/rules_builder/is_word_char_test.py",
58
+ "python/gbnf/rules_builder/parse_char.py",
59
+ "python/gbnf/rules_builder/parse_char_test.py",
60
+ "python/gbnf/rules_builder/parse_name.py",
61
+ "python/gbnf/rules_builder/parse_name_test.py",
62
+ "python/gbnf/rules_builder/parse_space.py",
63
+ "python/gbnf/rules_builder/parse_space_test.py",
64
+ "python/gbnf/rules_builder/rules_builder.py",
65
+ "python/gbnf/rules_builder/rules_builder_test.py",
66
+ "python/gbnf/rules_builder/rules_builder_types.py",
67
+ "python/gbnf/rules_builder/symbol_ids.py",
68
+ "python/gbnf/utils/__init__.py",
69
+ "python/gbnf/utils/errors/__init__.py",
70
+ "python/gbnf/utils/errors/build_error_position.py",
71
+ "python/gbnf/utils/errors/build_error_position_test.py",
72
+ "python/gbnf/utils/errors/errors_types.py",
73
+ "python/gbnf/utils/errors/get_input_as_string.py",
74
+ "python/gbnf/utils/errors/get_input_as_string_test.py",
75
+ "python/gbnf/utils/errors/grammar_parse_error.py",
76
+ "python/gbnf/utils/errors/grammar_parse_error_test.py",
77
+ "python/gbnf/utils/errors/input_parse_error.py",
78
+ "python/gbnf/utils/errors/input_parse_error_test.py",
79
+ "python/gbnf/utils/is_point_in_range.py",
80
+ "python/gbnf/utils/is_point_in_range_test.py",
81
+ "python/gbnf/utils/validate_non_empty.py",
82
+ "python/pyproject.toml",
83
+ "python/requirements.txt",
84
+ "python/tests/generated/iteration/iteration_test.py",
85
+ "python/tests/generated/iteration/iteration_with_additional_strings_test.py",
86
+ "python/tests/generated/iteration/iteration_with_an_initial_string_test.py",
87
+ "python/tests/generated/validation/validate_grammar_test.py",
88
+ "python/tests/generated/validation/validate_input_test.py",
89
+ "python/uv.lock"
90
+ ]
91
+ },
92
+ "sandbox": {
93
+ "image_id": "sha256:b99c05eb5dfe24a04ba60f290cfad23a2f16684e8ca5273abce1291a4b7ef897"
94
+ },
95
+ "harness": {
96
+ "commit": "9e24e21259430f5e40bca73c392757016eda7cda",
97
+ "dirty": true
98
+ }
99
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ node_modules
2
+ dist
3
+ coverage
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/README.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GBNF (TypeScript)
2
+
3
+ A TypeScript port of the Python `gbnf` library (`python/` in the reference tree): a parser for GBNF grammars that lets you feed input one piece at a time and check which continuations the grammar still allows.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { GBNF, GrammarParseError, InputParseError } from "gbnf";
9
+
10
+ const state = GBNF('root ::= "foo" | "bar"'); // throws GrammarParseError for an invalid grammar
11
+ const next = state.add("f"); // throws InputParseError if the input is not allowed
12
+
13
+ for (const rule of next) { // rules the grammar allows next
14
+ console.log(rule.type, rule.value); // RuleChar / RuleCharExclude / RuleEnd
15
+ }
16
+ ```
17
+
18
+ | Python | TypeScript |
19
+ | --------------------------- | ------------------------------------------- |
20
+ | `GBNF(grammar, initial)` | `GBNF(grammar, initial?)` |
21
+ | `state + text`, `state(text)` | `state.add(text)` |
22
+ | `[*state]` | `[...state]` |
23
+ | `state.size`, `state.grammar` | `state.size`, `state.grammar` (getters) |
24
+ | `err.error_for_most_recent_input` | `err.errorForMostRecentInput` |
25
+ | `rule == other` | `rule.equals(other)` |
26
+ | `err == other` | compare `err.message` (errors compare by message) |
27
+
28
+ ## Layout
29
+
30
+ ```
31
+ gbnf/
32
+ GBNF.ts entry point
33
+ index.ts public exports
34
+ grammar-graph/ graph, pointers, parse state, printing
35
+ grammar-parser/ builds rule stacks from linear rule definitions
36
+ rules-builder/ tokenizes and parses GBNF text
37
+ utils/ error classes, range helpers, Python-compatible helpers
38
+ tests/generated/ integration tests (validation, iteration)
39
+ ```
40
+
41
+ Unit tests are colocated with the modules as `*.test.ts`, mirroring the Python `*_test.py` files.
42
+
43
+ ## Running the tests
44
+
45
+ ```sh
46
+ vitest run # or: pnpm test
47
+ ```
48
+
49
+ The tests use vitest 2.1.3 and have no package dependencies, so they run with the global install. `vitest.config.ts` maps the `gbnf` import specifier to `gbnf/index.ts`.
50
+
51
+ `pnpm build` runs `tsc -p tsconfig.build.json`, which needs a local `typescript` install.
52
+
53
+ ## Differences from the Python version
54
+
55
+ Behavior matches the Python port, with these deliberate exceptions:
56
+
57
+ - **Code points.** Grammars and inputs are handled as sequences of code points, as in Python, so astral characters (emoji) count as one character in literals and in error positions.
58
+ - **Missing `root`.** A grammar with no `root` rule raises `GrammarParseError` ("Grammar does not contain a 'root' symbol"). Python raised a `KeyError`.
59
+ - **Truncated input.** Unterminated literals, brackets, and a trailing `\r` raise `GrammarParseError` (or parse normally). Python raised an `IndexError`.
60
+ - **Error formatting.** `buildErrorPosition` no longer crashes on some multi-line error positions where Python raised `IndexError`. The caret placement is unchanged, so single-line and existing multi-line output is identical.
61
+ - **Printing.** `Graph.print` works for graphs that contain `RuleEnd` nodes. Python raised an `AttributeError` there.
62
+ - **Rule order.** Python iterated a `set` of referenced nodes, so the order of rules from alternatives that reference a multi-branch rule was arbitrary. The port uses insertion order, which is deterministic. The set of rules is the same.
63
+ - **Exceptions.** Python `ValueError`s are plain `Error`s here, with the same messages.
64
+
65
+ ## Verification
66
+
67
+ - The Python unit and integration suites were ported and pass (`vitest run`: 522 tests).
68
+ - Mutation checks on scratch copies (range matching, comment skipping, caret line count, rule de-duplication, the `+` operator) each made the suite fail.
69
+ - Outputs were compared against the Python reference on 14,000 generated grammars: 8,000 mostly invalid ones and 6,000 valid-by-construction ones with sampled inputs. Rule contents matched in every case. Error messages and caret positions matched in every case except the deliberate differences above. Left-recursive and nullable-repetition grammars overflow the stack in both implementations.
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/GBNF.test.ts ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { GBNF, GrammarParseError, InputParseError, RuleChar, RuleEnd } from "./index.js";
3
+
4
+ function errorFrom(fn: () => unknown): unknown {
5
+ try {
6
+ fn();
7
+ } catch (error) {
8
+ return error;
9
+ }
10
+ throw new Error("expected the function to throw");
11
+ }
12
+
13
+ describe("GBNF", () => {
14
+ it("rejects a non-string grammar", () => {
15
+ expect(() => GBNF(1 as unknown as string)).toThrow("grammar must be a string");
16
+ });
17
+
18
+ it("rejects a non-string initial string", () => {
19
+ expect(() => GBNF('root ::= "a"', 1 as unknown as string)).toThrow("input must be a string");
20
+ });
21
+
22
+ it("reports a grammar without a root symbol as a grammar error", () => {
23
+ const error = errorFrom(() => GBNF('foo ::= "a"'));
24
+ expect(error).toBeInstanceOf(GrammarParseError);
25
+ expect((error as GrammarParseError).reason).toBe("Grammar does not contain a 'root' symbol");
26
+ });
27
+
28
+ it("reports an unterminated string as a grammar error", () => {
29
+ const error = errorFrom(() => GBNF('root ::= "abc'));
30
+ expect(error).toBeInstanceOf(GrammarParseError);
31
+ expect((error as GrammarParseError).reason).toBe(
32
+ "Unexpected end of grammar input, failed to complete parse",
33
+ );
34
+ });
35
+
36
+ it("accepts a grammar that ends with a carriage return", () => {
37
+ expect([...GBNF('root ::= "a"\r')]).toEqual([new RuleChar([97])]);
38
+ });
39
+
40
+ it("reports positions in code points, so astral characters count as one position", () => {
41
+ const error = errorFrom(() => GBNF('root ::= "\u{1F600}a"').add("\u{1F600}b"));
42
+ expect(error).toBeInstanceOf(InputParseError);
43
+ expect((error as InputParseError).message).toBe(
44
+ ["Failed to parse input string:", "", "\u{1F600}b", " ^"].join("\n"),
45
+ );
46
+ });
47
+
48
+ it("parses an astral character in a grammar literal as a single code point", () => {
49
+ expect([...GBNF('root ::= "\u{1F600}" "a"')]).toEqual([new RuleChar([0x1f600])]);
50
+ });
51
+
52
+ it("iterates over a rule that ends the grammar", () => {
53
+ expect([...GBNF('root ::= "a"?')]).toEqual([new RuleChar([97]), new RuleEnd()]);
54
+ });
55
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/GBNF.ts ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Graph } from "./grammar-graph/graph.js";
2
+ import { ParseState } from "./grammar-graph/parse-state.js";
3
+ import { buildRuleStack } from "./grammar-parser/build-rule-stack.js";
4
+ import { RulesBuilder } from "./rules-builder/rules-builder.js";
5
+ import { GrammarParseError } from "./utils/errors/grammar-parse-error.js";
6
+
7
+ export function GBNF(grammar: string, initialString = ""): ParseState {
8
+ if (typeof grammar !== "string") {
9
+ throw new Error("grammar must be a string");
10
+ }
11
+
12
+ if (typeof initialString !== "string") {
13
+ throw new Error("input must be a string");
14
+ }
15
+
16
+ const rulesBuilder = new RulesBuilder(grammar);
17
+ const { rules, symbolIds } = rulesBuilder;
18
+ if (rules.length === 0) {
19
+ throw new GrammarParseError(grammar, 0, "No rules were found");
20
+ }
21
+ const rootId = symbolIds.get("root");
22
+ if (rootId === undefined) {
23
+ throw new GrammarParseError(grammar, 0, "Grammar does not contain a 'root' symbol");
24
+ }
25
+
26
+ const stackedRules = rules.map((rule) => buildRuleStack(rule));
27
+ const graph = new Graph(grammar, stackedRules, rootId);
28
+ return new ParseState(graph, graph.add(initialString));
29
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/colorize.test.ts ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { Color, colorize } from "./colorize.js";
3
+
4
+ describe("colorize", () => {
5
+ it("colorizes strings", () => {
6
+ expect(colorize("hello", Color.BLUE)).toBe("\x1b[34mhello");
7
+ expect(colorize("test", Color.CYAN)).toBe("\x1b[36mtest");
8
+ expect(colorize("example", Color.GREEN)).toBe("\x1b[32mexample");
9
+ });
10
+
11
+ it("colorizes numbers", () => {
12
+ expect(colorize(123, Color.RED)).toBe("\x1b[31m123");
13
+ expect(colorize(456, Color.GRAY)).toBe("\x1b[90m456");
14
+ expect(colorize(789, Color.YELLOW)).toBe("\x1b[33m789");
15
+ });
16
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/colorize.ts ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const Color = {
2
+ BLUE: "\x1b[34m",
3
+ CYAN: "\x1b[36m",
4
+ GREEN: "\x1b[32m",
5
+ RED: "\x1b[31m",
6
+ GRAY: "\x1b[90m",
7
+ YELLOW: "\x1b[33m",
8
+ } as const;
9
+
10
+ export type Colorize = (text: string | number, color: string) => string;
11
+
12
+ export function colorize(text: string | number, color: string): string {
13
+ return `${color}${text}`;
14
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-input-as-code-points.test.ts ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { getInputAsCodePoints } from "./get-input-as-code-points.js";
3
+
4
+ describe("get input as code points", () => {
5
+ it("returns code points for a string", () => {
6
+ expect(getInputAsCodePoints("abc")).toEqual([97, 98, 99]);
7
+ });
8
+
9
+ it("returns code points for a string containing astral characters", () => {
10
+ expect(getInputAsCodePoints("a\u{1F600}")).toEqual([97, 0x1f600]);
11
+ });
12
+
13
+ it("returns code points for a number", () => {
14
+ expect(getInputAsCodePoints(99)).toEqual([99]);
15
+ });
16
+
17
+ it("returns code points for an array of numbers", () => {
18
+ expect(getInputAsCodePoints([99, 100, 101])).toEqual([99, 100, 101]);
19
+ });
20
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-input-as-code-points.ts ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { ValidInput } from "./grammar-graph-types.js";
2
+
3
+ export function getCodePoint(char: string): number {
4
+ const codePoint = char.codePointAt(0);
5
+ if (codePoint === undefined) {
6
+ throw new Error(`Could not get code point for character: ${char}`);
7
+ }
8
+ return codePoint;
9
+ }
10
+
11
+ export function getInputAsCodePoints(src: ValidInput): number[] {
12
+ if (typeof src === "number") {
13
+ return [src];
14
+ }
15
+
16
+ if (Array.isArray(src)) {
17
+ for (const codePoint of src) {
18
+ if (!Number.isInteger(codePoint)) {
19
+ throw new Error(`code_point must be an integer for ${codePoint} if src is a list`);
20
+ }
21
+ }
22
+ return src;
23
+ }
24
+
25
+ if (typeof src === "string") {
26
+ return Array.from(src, (char) => getCodePoint(char));
27
+ }
28
+
29
+ throw new Error(`Invalid input type: ${typeof src}`);
30
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-parent-stack-id.test.ts ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { Color, type Colorize } from "./colorize.js";
3
+ import { getParentStackId } from "./get-parent-stack-id.js";
4
+ import { RuleEnd } from "./grammar-graph-types.js";
5
+ import { GraphNode } from "./graph-node.js";
6
+ import { GraphPointer } from "./graph-pointer.js";
7
+
8
+ function s(value: string): string {
9
+ return JSON.stringify(value);
10
+ }
11
+
12
+ const mockColorize: Colorize = (text, color) => `[${s(color)}]:${text}`;
13
+
14
+ const red = s(Color.RED);
15
+ const gray = s(Color.GRAY);
16
+
17
+ function createMockPointer(
18
+ stackId: number,
19
+ pathId: number,
20
+ stepId: number,
21
+ parent?: GraphPointer,
22
+ ): GraphPointer {
23
+ return new GraphPointer(new GraphNode(new RuleEnd(), { stackId, pathId, stepId }), parent);
24
+ }
25
+
26
+ describe("get parent stack id", () => {
27
+ it("returns an empty string if there are no parents", () => {
28
+ const pointer = createMockPointer(1, 1, 1);
29
+ expect(getParentStackId(pointer, mockColorize)).toBe("");
30
+ });
31
+
32
+ it("returns a single parent id colored correctly", () => {
33
+ const parentPointer = createMockPointer(1, 1, 1);
34
+ const pointer = createMockPointer(2, 2, 2, parentPointer);
35
+ expect(getParentStackId(pointer, mockColorize)).toBe(`[${red}]:1,1,1`);
36
+ });
37
+
38
+ it("returns multiple parent ids separated by colored arrows", () => {
39
+ const grandparentPointer = createMockPointer(0, 0, 0);
40
+ const parentPointer = createMockPointer(1, 1, 1, grandparentPointer);
41
+ const pointer = createMockPointer(2, 2, 2, parentPointer);
42
+ expect(getParentStackId(pointer, mockColorize)).toBe(
43
+ `[${red}]:1,1,1[${gray}]:<-[${red}]:0,0,0`,
44
+ );
45
+ });
46
+
47
+ it("handles deep nesting of pointers", () => {
48
+ const greatGrandparentPointer = createMockPointer(0, 0, 0);
49
+ const grandparentPointer = createMockPointer(1, 1, 1, greatGrandparentPointer);
50
+ const parentPointer = createMockPointer(2, 2, 2, grandparentPointer);
51
+ const pointer = createMockPointer(3, 3, 3, parentPointer);
52
+ expect(getParentStackId(pointer, mockColorize)).toBe(
53
+ `[${red}]:2,2,2[${gray}]:<-[${red}]:1,1,1[${gray}]:<-[${red}]:0,0,0`,
54
+ );
55
+ });
56
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-parent-stack-id.ts ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Color, type Colorize } from "./colorize.js";
2
+ import type { GraphPointer } from "./graph-pointer.js";
3
+
4
+ export function getParentStackId(pointer: GraphPointer, col: Colorize): string {
5
+ const stackIds: string[] = [];
6
+ let parent: GraphPointer | undefined = pointer.parent;
7
+ while (parent) {
8
+ const { stackId, pathId, stepId } = parent.node.meta;
9
+ stackIds.push(`${stackId},${pathId},${stepId}`);
10
+ parent = parent.parent;
11
+ }
12
+ const arrow = col("<-", Color.GRAY);
13
+ return stackIds.map((stackId) => col(stackId, Color.RED)).join(arrow);
14
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-serialized-rule-key.test.ts ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { KEY_TRANSLATION, getSerializedRuleKey } from "./get-serialized-rule-key.js";
3
+ import { RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from "./grammar-graph-types.js";
4
+ import { RuleRef } from "./rule-ref.js";
5
+
6
+ describe("get serialized rule key", () => {
7
+ it("returns the type for end rules", () => {
8
+ expect(getSerializedRuleKey(new RuleEnd())).toBe(`${KEY_TRANSLATION.RuleEnd}`);
9
+ });
10
+
11
+ it("returns the type and value for character rules", () => {
12
+ expect(getSerializedRuleKey(new RuleChar([97]))).toBe(`${KEY_TRANSLATION.RuleChar}-[97]`);
13
+ });
14
+
15
+ it("returns the type and value for character exclude rules", () => {
16
+ expect(getSerializedRuleKey(new RuleCharExclude([97]))).toBe(
17
+ `${KEY_TRANSLATION.RuleCharExclude}-[97]`,
18
+ );
19
+ });
20
+
21
+ it("serializes ranges the way Python's json.dumps does", () => {
22
+ expect(getSerializedRuleKey(new RuleChar([97, [98, 99]]))).toBe("1-[97, [98, 99]]");
23
+ });
24
+
25
+ it("returns the ref type with the value for reference rules", () => {
26
+ expect(getSerializedRuleKey(new RuleRef(99))).toBe("3-99");
27
+ });
28
+
29
+ it("throws for unknown rule types", () => {
30
+ const rule = { type: "UNKNOWN", value: "something" } as unknown as UnresolvedRule;
31
+ expect(() => getSerializedRuleKey(rule)).toThrow(
32
+ 'Unknown rule type: {"type": "UNKNOWN", "value": "something"}',
33
+ );
34
+ });
35
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/get-serialized-rule-key.ts ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { pythonJsonDumps } from "../utils/py-compat.js";
2
+ import type { UnresolvedRule } from "./grammar-graph-types.js";
3
+ import { isRuleChar, isRuleCharExclude, isRuleEnd, isRuleRef } from "./type-guards.js";
4
+
5
+ export const KEY_TRANSLATION = {
6
+ RuleEnd: 0,
7
+ RuleChar: 1,
8
+ RuleCharExclude: 2,
9
+ } as const;
10
+
11
+ export function getSerializedRuleKey(rule: UnresolvedRule): string {
12
+ if (isRuleEnd(rule)) {
13
+ return `${KEY_TRANSLATION.RuleEnd}`;
14
+ }
15
+
16
+ if (isRuleChar(rule)) {
17
+ return `${KEY_TRANSLATION.RuleChar}-${pythonJsonDumps(rule.value)}`;
18
+ }
19
+
20
+ if (isRuleCharExclude(rule)) {
21
+ return `${KEY_TRANSLATION.RuleCharExclude}-${pythonJsonDumps(rule.value)}`;
22
+ }
23
+
24
+ if (isRuleRef(rule)) {
25
+ return `3-${rule.value}`;
26
+ }
27
+
28
+ throw new Error(`Unknown rule type: ${pythonJsonDumps(rule)}`);
29
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/grammar-graph-types.test.ts ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { RuleChar, RuleCharExclude, RuleEnd } from "./grammar-graph-types.js";
3
+ import { RuleRef } from "./rule-ref.js";
4
+
5
+ describe("rule equality", () => {
6
+ it("compares char rules by value", () => {
7
+ expect(new RuleChar([97]).equals(new RuleChar([97]))).toBe(true);
8
+ expect(new RuleChar([97]).equals(new RuleChar([98]))).toBe(false);
9
+ expect(new RuleChar([97, [98, 99]]).equals(new RuleChar([97, [98, 99]]))).toBe(true);
10
+ expect(new RuleChar([97, [98, 99]]).equals(new RuleChar([97, [98, 100]]))).toBe(false);
11
+ expect(new RuleChar([97]).equals(new RuleChar([97, 98]))).toBe(false);
12
+ });
13
+
14
+ it("does not treat different rule types as equal", () => {
15
+ expect(new RuleChar([97]).equals(new RuleCharExclude([97]))).toBe(false);
16
+ expect(new RuleChar([97]).equals(new RuleEnd())).toBe(false);
17
+ });
18
+
19
+ it("treats all end rules as equal", () => {
20
+ expect(new RuleEnd().equals(new RuleEnd())).toBe(true);
21
+ });
22
+
23
+ it("compares rule refs by value", () => {
24
+ expect(new RuleRef(3).equals(new RuleRef(3))).toBe(true);
25
+ expect(new RuleRef(3).equals(new RuleRef(4))).toBe(false);
26
+ expect(new RuleRef(3).equals(new RuleEnd())).toBe(false);
27
+ });
28
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/grammar-graph-types.ts ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { pythonJsonDumps } from "../utils/py-compat.js";
2
+ import type { Colorize } from "./colorize.js";
3
+ import type { GraphPointer } from "./graph-pointer.js";
4
+ import type { RuleRef } from "./rule-ref.js";
5
+
6
+ export type { ValidInput } from "../utils/errors/errors-types.js";
7
+
8
+ export interface PrintOpts {
9
+ pointers: Iterable<GraphPointer>;
10
+ colorize: Colorize;
11
+ showPosition: boolean;
12
+ }
13
+
14
+ export type Range = [number, number];
15
+
16
+ export type CharValue = number | Range;
17
+
18
+ export abstract class Rule {
19
+ abstract get type(): string;
20
+
21
+ /** Value equality, the TypeScript counterpart of Python's `==` for rules. */
22
+ equals(other: unknown): boolean {
23
+ return other instanceof Rule && other.type === this.type;
24
+ }
25
+
26
+ toJSON(): { type: string; value?: unknown } {
27
+ return { type: this.type };
28
+ }
29
+ }
30
+
31
+ function charValuesEqual(a: CharValue[], b: CharValue[]): boolean {
32
+ return (
33
+ a.length === b.length &&
34
+ a.every((item, idx) => {
35
+ const other = b[idx];
36
+ if (Array.isArray(item) && Array.isArray(other)) {
37
+ return item[0] === other[0] && item[1] === other[1];
38
+ }
39
+ return item === other;
40
+ })
41
+ );
42
+ }
43
+
44
+ export abstract class RuleWithValue extends Rule {
45
+ readonly value: CharValue[];
46
+
47
+ constructor(value: CharValue[]) {
48
+ super();
49
+ this.value = [...value];
50
+ }
51
+
52
+ equals(other: unknown): boolean {
53
+ return (
54
+ super.equals(other) &&
55
+ other instanceof RuleWithValue &&
56
+ charValuesEqual(this.value, other.value)
57
+ );
58
+ }
59
+
60
+ toJSON(): { type: string; value: CharValue[] } {
61
+ return { type: this.type, value: this.value };
62
+ }
63
+
64
+ toString(): string {
65
+ return `${this.type}(value=${pythonJsonDumps(this.value)})`;
66
+ }
67
+ }
68
+
69
+ export class RuleChar extends RuleWithValue {
70
+ get type(): "RuleChar" {
71
+ return "RuleChar";
72
+ }
73
+ }
74
+
75
+ export class RuleCharExclude extends RuleWithValue {
76
+ get type(): "RuleCharExclude" {
77
+ return "RuleCharExclude";
78
+ }
79
+ }
80
+
81
+ export class RuleEnd extends Rule {
82
+ get type(): "RuleEnd" {
83
+ return "RuleEnd";
84
+ }
85
+
86
+ toString(): string {
87
+ return "RuleEnd()";
88
+ }
89
+ }
90
+
91
+ export type UnresolvedRule = RuleChar | RuleCharExclude | RuleRef | RuleEnd;
92
+
93
+ // RuleRefs should never be exposed to the end user.
94
+ export type ResolvedRule = RuleCharExclude | RuleChar | RuleEnd;
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-node.test.ts ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import type { PrintOpts } from "./grammar-graph-types.js";
3
+ import { GraphNode, type GraphNodeMeta } from "./graph-node.js";
4
+ import { printGraphNode } from "./print.js";
5
+ import { RuleRef } from "./rule-ref.js";
6
+
7
+ vi.mock("./print.js", () => ({
8
+ printGraphNode: vi.fn(),
9
+ }));
10
+
11
+ const meta: GraphNodeMeta = { stackId: 1, pathId: 2, stepId: 3 };
12
+ const rule = new RuleRef(42);
13
+
14
+ describe("graph node", () => {
15
+ beforeEach(() => {
16
+ vi.mocked(printGraphNode).mockReset();
17
+ vi.mocked(printGraphNode).mockReturnValue(() => "mocked_response");
18
+ });
19
+
20
+ it("constructs with a rule and meta", () => {
21
+ const node = new GraphNode(rule, meta);
22
+ expect(node.rule).toBe(rule);
23
+ expect(node.meta).toEqual(meta);
24
+ });
25
+
26
+ it("throws if meta is undefined", () => {
27
+ expect(() => new GraphNode(rule, undefined as unknown as GraphNodeMeta)).toThrow(
28
+ "Meta is undefined",
29
+ );
30
+ });
31
+
32
+ it("correctly calculates and caches its id", () => {
33
+ const node = new GraphNode(rule, meta);
34
+ expect(node.id).toBe("1,2,3");
35
+ node.meta = { stackId: 4, pathId: 5, stepId: 6 };
36
+ expect(node.id).toBe("1,2,3");
37
+ });
38
+
39
+ it("delegates print to the printGraphNode function", () => {
40
+ const node = new GraphNode(rule, meta);
41
+ const opts: PrintOpts = { colorize: (x) => String(x), pointers: [], showPosition: false };
42
+ expect(node.print(opts)).toBe("mocked_response");
43
+ expect(printGraphNode).toHaveBeenCalledWith(node);
44
+ });
45
+
46
+ it("handles next node linkage", () => {
47
+ const nextNode = new GraphNode(new RuleRef(43), meta);
48
+ const node = new GraphNode(rule, meta, nextNode);
49
+ expect(node.next).toBe(nextNode);
50
+ });
51
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-node.ts ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { PrintOpts, UnresolvedRule } from "./grammar-graph-types.js";
2
+ import { printGraphNode } from "./print.js";
3
+
4
+ export interface GraphNodeMeta {
5
+ stackId: number;
6
+ pathId: number;
7
+ stepId: number;
8
+ }
9
+
10
+ export class GraphNode<T extends UnresolvedRule = UnresolvedRule> {
11
+ rule: T;
12
+ next: GraphNode | undefined;
13
+ meta: GraphNodeMeta;
14
+ #id: string | undefined;
15
+
16
+ constructor(rule: T, meta: GraphNodeMeta, nextNode?: GraphNode) {
17
+ this.rule = rule;
18
+ if (meta === undefined || meta === null) {
19
+ throw new Error("Meta is undefined");
20
+ }
21
+ this.meta = meta;
22
+ this.next = nextNode;
23
+ }
24
+
25
+ get id(): string {
26
+ if (this.#id === undefined) {
27
+ this.#id = `${this.meta.stackId},${this.meta.pathId},${this.meta.stepId}`;
28
+ }
29
+ return this.#id;
30
+ }
31
+
32
+ print(opts: PrintOpts): string {
33
+ return printGraphNode(this)(opts);
34
+ }
35
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-pointer.test.ts ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from "./grammar-graph-types.js";
3
+ import { GraphNode } from "./graph-node.js";
4
+ import { GraphPointer } from "./graph-pointer.js";
5
+
6
+ const meta = (stackId: number, pathId: number, stepId: number) => ({ stackId, pathId, stepId });
7
+
8
+ describe("graph pointer", () => {
9
+ it("constructs with a node", () => {
10
+ const node = new GraphNode(new RuleEnd(), meta(1, 2, 3));
11
+ const pointer = new GraphPointer(node);
12
+ expect(pointer.node).toBe(node);
13
+ expect(pointer.id).toBe("1,2,3");
14
+ expect(pointer.parent).toBeUndefined();
15
+ });
16
+
17
+ it("throws if the node is undefined", () => {
18
+ expect(() => new GraphPointer(undefined as unknown as GraphNode)).toThrow("Node is undefined");
19
+ });
20
+
21
+ it("initializes correctly with a node and a parent", () => {
22
+ const parentNode = new GraphNode(new RuleChar([97]), meta(1, 2, 3));
23
+ const childNode = new GraphNode(new RuleChar([98]), meta(4, 5, 6));
24
+ const parentPointer = new GraphPointer(parentNode);
25
+ const childPointer = new GraphPointer(childNode, parentPointer);
26
+ expect(childPointer.parent).toBe(parentPointer);
27
+ expect(childPointer.id).toBe("1,2,3-4,5,6");
28
+ });
29
+
30
+ describe("resolve", () => {
31
+ it("throws on unknown rule types", () => {
32
+ const node = new GraphNode(
33
+ { type: "UNKNOWN_RULE_TYPE" } as unknown as UnresolvedRule,
34
+ meta(1, 2, 3),
35
+ );
36
+ const pointer = new GraphPointer(node);
37
+ expect(() => [...pointer.resolve()]).toThrow("Unknown rule");
38
+ });
39
+
40
+ it("yields a char rule", () => {
41
+ const node = new GraphNode(new RuleChar([97]), meta(1, 2, 3));
42
+ const pointer = new GraphPointer(node);
43
+ expect([...pointer.resolve()]).toEqual([pointer]);
44
+ });
45
+
46
+ it("yields a char excluded rule", () => {
47
+ const node = new GraphNode(new RuleCharExclude([97]), meta(1, 2, 3));
48
+ const pointer = new GraphPointer(node);
49
+ expect([...pointer.resolve()]).toEqual([pointer]);
50
+ });
51
+
52
+ it("yields an end rule without a parent", () => {
53
+ const node = new GraphNode(new RuleEnd(), meta(1, 2, 3));
54
+ const pointer = new GraphPointer(node);
55
+ expect([...pointer.resolve()]).toEqual([pointer]);
56
+ });
57
+
58
+ it("yields the parent of an end rule with a parent", () => {
59
+ const rule = new RuleEnd();
60
+ const parentPointer = new GraphPointer(new GraphNode(rule, meta(2, 1, 1)));
61
+ const childPointer = new GraphPointer(new GraphNode(rule, meta(1, 1, 1)), parentPointer);
62
+ expect([...childPointer.resolve()]).toEqual([parentPointer]);
63
+ });
64
+
65
+ it("yields the grandparent of an end rule with a parent that is also an end rule", () => {
66
+ const rule = new RuleEnd();
67
+ const grandparentPointer = new GraphPointer(new GraphNode(rule, meta(2, 1, 1)));
68
+ const parentPointer = new GraphPointer(new GraphNode(rule, meta(2, 1, 1)), grandparentPointer);
69
+ const childPointer = new GraphPointer(new GraphNode(rule, meta(1, 1, 1)), parentPointer);
70
+ expect([...childPointer.resolve()]).toEqual([grandparentPointer]);
71
+ });
72
+
73
+ it("yields the parent of an end rule when that parent is a char rule with a grandparent", () => {
74
+ const rule = new RuleEnd();
75
+ const grandparentPointer = new GraphPointer(new GraphNode(rule, meta(2, 1, 1)));
76
+ const parentPointer = new GraphPointer(
77
+ new GraphNode(new RuleChar([97]), meta(2, 1, 1)),
78
+ grandparentPointer,
79
+ );
80
+ const childPointer = new GraphPointer(new GraphNode(rule, meta(1, 1, 1)), parentPointer);
81
+ expect([...childPointer.resolve()]).toEqual([parentPointer]);
82
+ });
83
+ });
84
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph-pointer.ts ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { PrintOpts, UnresolvedRule } from "./grammar-graph-types.js";
2
+ import type { GraphNode } from "./graph-node.js";
3
+ import { printGraphPointer } from "./print.js";
4
+ import { isRuleChar, isRuleCharExclude, isRuleEnd, isRuleRef } from "./type-guards.js";
5
+
6
+ export class GraphPointer<T extends UnresolvedRule = UnresolvedRule> {
7
+ node: GraphNode<T>;
8
+ parent: GraphPointer | undefined;
9
+ id: string;
10
+ #valid: boolean | undefined = undefined;
11
+
12
+ constructor(node: GraphNode<T>, parent?: GraphPointer) {
13
+ if (node === undefined || node === null) {
14
+ throw new Error("Node is undefined");
15
+ }
16
+ this.node = node;
17
+ this.parent = parent;
18
+ this.id = parent ? `${parent.id}-${node.id}` : node.id;
19
+ }
20
+
21
+ get rule(): T {
22
+ return this.node.rule;
23
+ }
24
+
25
+ get valid(): boolean | undefined {
26
+ return this.#valid;
27
+ }
28
+
29
+ set valid(valid: boolean | undefined) {
30
+ this.#valid = valid;
31
+ }
32
+
33
+ print(opts: PrintOpts): string {
34
+ return printGraphPointer(this)(opts);
35
+ }
36
+
37
+ /**
38
+ * Resolve the graph pointer down to char, char-exclude, or top-level end rules.
39
+ *
40
+ * @param resolved - Whether the pointer has already been resolved. Defaults to false.
41
+ */
42
+ *resolve(resolved = false): Generator<GraphPointer> {
43
+ const rule = this.node.rule;
44
+ if (isRuleRef(rule)) {
45
+ if (resolved) {
46
+ if (!this.node.next) {
47
+ throw new Error(`No next node: ${this.node.id}`);
48
+ }
49
+ yield* new GraphPointer(this.node.next, this.parent).resolve();
50
+ } else {
51
+ for (const node of rule.nodes) {
52
+ yield* new GraphPointer(node, this).resolve();
53
+ }
54
+ }
55
+ } else if (isRuleEnd(rule)) {
56
+ if (!this.parent) {
57
+ yield this;
58
+ } else {
59
+ yield* this.parent.resolve(true);
60
+ }
61
+ } else if (isRuleChar(rule) || isRuleCharExclude(rule)) {
62
+ yield this;
63
+ } else {
64
+ throw new Error(`Unknown rule: ${JSON.stringify(rule)}`);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Fetch the next resolved graph pointer.
70
+ */
71
+ *fetchNext(): Generator<GraphPointer> {
72
+ // If this pointer is invalid, then we don't return any new pointers
73
+ if (!this.valid) {
74
+ return;
75
+ }
76
+
77
+ // If this pointer is an end node, we return the parent's next node.
78
+ // If no parent exists, we return nothing, since it's the end of the line.
79
+ if (isRuleEnd(this.node.rule)) {
80
+ if (this.parent) {
81
+ yield* this.parent.fetchNext();
82
+ }
83
+ } else {
84
+ if (!this.node.next) {
85
+ throw new Error(`No next node: ${this.node.id}`);
86
+ }
87
+ const pointer = new GraphPointer(this.node.next, this.parent);
88
+ yield* pointer.resolve();
89
+ }
90
+ }
91
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph.test.ts ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { RuleChar, RuleCharExclude, RuleEnd, type UnresolvedRule } from "./grammar-graph-types.js";
3
+ import { Graph } from "./graph.js";
4
+ import { GraphNode } from "./graph-node.js";
5
+ import { GraphPointer } from "./graph-pointer.js";
6
+ import { Pointers } from "./pointers.js";
7
+ import { RuleRef } from "./rule-ref.js";
8
+
9
+ const grammar = "example-grammar";
10
+ const stackedRules: UnresolvedRule[][][] = [
11
+ [[new RuleChar([65, 66, 67])], [new RuleChar([68, 69, 70])]],
12
+ [[new RuleChar([71, 72, 73])], [new RuleChar([74, 75, 76])]],
13
+ ];
14
+ const rootId = 0;
15
+
16
+ describe("graph", () => {
17
+ it("creates a graph instance", () => {
18
+ const graph = new Graph(grammar, stackedRules, rootId);
19
+ expect(graph).toBeInstanceOf(Graph);
20
+ expect(graph.grammar).toBe(grammar);
21
+ });
22
+
23
+ it("gets the root node", () => {
24
+ const graph = new Graph(grammar, stackedRules, rootId);
25
+ const rootNode = graph.getRootNode(rootId);
26
+ expect(rootNode).toBeInstanceOf(Map);
27
+ expect(rootNode.size).toBe(2);
28
+ });
29
+
30
+ it("gets the initial pointers", () => {
31
+ const graph = new Graph(grammar, stackedRules, rootId);
32
+ expect(graph.getInitialPointers().size).toBe(2);
33
+ });
34
+
35
+ it("prints the graph", () => {
36
+ const graph = new Graph(grammar, stackedRules, rootId);
37
+ const printedGraph = graph.print(new Pointers(), true);
38
+ expect(typeof printedGraph).toBe("string");
39
+ expect(printedGraph.length).toBeGreaterThan(0);
40
+ });
41
+
42
+ it("prints graphs that contain end rules", () => {
43
+ const graph = new Graph(grammar, [[[new RuleChar([97]), new RuleEnd()]]], 0);
44
+ expect(graph.print(new Pointers(), false)).toBe("{0,0,0}[a]-> {0,0,1}RuleEnd");
45
+ });
46
+
47
+ it("iterates over pointers", () => {
48
+ const graph = new Graph(grammar, stackedRules, rootId);
49
+ const rules: UnresolvedRule[] = [
50
+ new RuleChar([65, 66, 67]),
51
+ new RuleChar([68, 69, 70]),
52
+ new RuleChar([71, 72, 73]),
53
+ new RuleChar([74, 75, 76]),
54
+ new RuleCharExclude([1]),
55
+ new RuleEnd(),
56
+ ];
57
+ const mockPointers = new Pointers(
58
+ ...rules.map(
59
+ (rule, stepId) => new GraphPointer(new GraphNode(rule, { stackId: 0, pathId: 0, stepId })),
60
+ ),
61
+ );
62
+
63
+ const result = [...graph.iterateOverPointers(mockPointers)];
64
+ expect(result).toHaveLength(6);
65
+ for (const [rule, pointers] of result) {
66
+ expect(rules).toContain(rule);
67
+ expect(pointers.length).toBeGreaterThanOrEqual(1);
68
+ for (const pointer of pointers) {
69
+ expect([...mockPointers]).toContain(pointer);
70
+ }
71
+ }
72
+ });
73
+
74
+ it("raises an error on a reference rule", () => {
75
+ const graph = new Graph(grammar, stackedRules, rootId);
76
+ const mockPointers = [
77
+ new GraphPointer(new GraphNode(new RuleRef(0), { stackId: 0, pathId: 0, stepId: 0 })),
78
+ ];
79
+
80
+ expect(() => [...graph.iterateOverPointers(mockPointers)]).toThrow(
81
+ "Encountered a reference rule in the graph",
82
+ );
83
+ });
84
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/graph.ts ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { InputParseError } from "../utils/errors/input-parse-error.js";
2
+ import { isPointInRange } from "../utils/is-point-in-range.js";
3
+ import { colorize } from "./colorize.js";
4
+ import { getInputAsCodePoints } from "./get-input-as-code-points.js";
5
+ import { getSerializedRuleKey } from "./get-serialized-rule-key.js";
6
+ import type { PrintOpts, UnresolvedRule, ValidInput } from "./grammar-graph-types.js";
7
+ import { GraphNode } from "./graph-node.js";
8
+ import { GraphPointer } from "./graph-pointer.js";
9
+ import { Pointers } from "./pointers.js";
10
+ import { RuleRef } from "./rule-ref.js";
11
+ import { isRange, isRuleChar, isRuleCharExclude, isRuleEnd, isRuleRef } from "./type-guards.js";
12
+
13
+ export type RootNode = Map<number, GraphNode>;
14
+
15
+ export class Graph {
16
+ readonly grammar: string;
17
+ previousCodePoints: number[] = [];
18
+ readonly #roots = new Map<number, RootNode>();
19
+ readonly #rootNode: RootNode;
20
+
21
+ constructor(grammar: string, stackedRules: UnresolvedRule[][][], rootId: number) {
22
+ this.grammar = grammar;
23
+ const ruleRefs: RuleRef[] = [];
24
+
25
+ stackedRules.forEach((stack, stackId) => {
26
+ const nodes: RootNode = new Map();
27
+ stack.forEach((path, pathId) => {
28
+ let node: GraphNode | undefined = undefined;
29
+ for (let stepId = path.length - 1; stepId >= 0; stepId--) {
30
+ const nextNode: GraphNode | undefined = node;
31
+ const rule = stack[pathId][stepId];
32
+ // Building the key also rejects unknown rule types.
33
+ getSerializedRuleKey(rule);
34
+ if (isRuleRef(rule)) {
35
+ ruleRefs.push(rule);
36
+ }
37
+ node = new GraphNode(rule, { stackId, pathId, stepId }, nextNode);
38
+ }
39
+
40
+ if (node === undefined) {
41
+ throw new Error("Could not get node");
42
+ }
43
+ nodes.set(pathId, node);
44
+ });
45
+ this.#roots.set(stackId, nodes);
46
+ });
47
+
48
+ const rootNode = this.#roots.get(rootId);
49
+ if (rootNode === undefined) {
50
+ throw new Error(`Root node not found for value: ${rootId}`);
51
+ }
52
+ this.#rootNode = rootNode;
53
+
54
+ for (const ruleRef of ruleRefs) {
55
+ ruleRef.nodes = new Set(this.getRootNode(ruleRef.value).values());
56
+ }
57
+ }
58
+
59
+ getRootNode(value: number): RootNode {
60
+ const rootNode = this.#roots.get(value);
61
+ if (rootNode === undefined) {
62
+ throw new Error(`Root node not found for value: ${value}`);
63
+ }
64
+ return rootNode;
65
+ }
66
+
67
+ getInitialPointers(): Pointers {
68
+ const pointers = new Pointers();
69
+
70
+ for (const [node, parent] of this.fetchNodesForRootNode(this.#rootNode)) {
71
+ const pointer = new GraphPointer(node, parent);
72
+ for (const resolvedPointer of this.resolvePointer(pointer)) {
73
+ pointers.add(resolvedPointer);
74
+ }
75
+ }
76
+ return pointers;
77
+ }
78
+
79
+ setValid(pointers: Iterable<GraphPointer>, valid: boolean): void {
80
+ for (const pointer of pointers) {
81
+ pointer.valid = valid;
82
+ }
83
+ }
84
+
85
+ parse(currentPointers: Pointers, codePoint: number): Pointers {
86
+ for (const [rule, graphPointers] of this.iterateOverPointers(currentPointers)) {
87
+ if (isRuleChar(rule)) {
88
+ let valid = false;
89
+ for (const possibleCodePoint of rule.value) {
90
+ if (valid) {
91
+ break;
92
+ } else if (isRange(possibleCodePoint)) {
93
+ if (isPointInRange(codePoint, possibleCodePoint)) {
94
+ valid = true;
95
+ }
96
+ } else if (codePoint === possibleCodePoint) {
97
+ valid = true;
98
+ }
99
+ }
100
+ this.setValid(graphPointers, valid);
101
+ } else if (isRuleCharExclude(rule)) {
102
+ let valid = true;
103
+ for (const possibleCodePoint of rule.value) {
104
+ if (!valid) {
105
+ break;
106
+ } else if (isRange(possibleCodePoint)) {
107
+ if (isPointInRange(codePoint, possibleCodePoint)) {
108
+ valid = false;
109
+ }
110
+ } else if (codePoint === possibleCodePoint) {
111
+ valid = false;
112
+ }
113
+ }
114
+ this.setValid(graphPointers, valid);
115
+ } else if (!isRuleEnd(rule)) {
116
+ throw new Error(`Unsupported rule: ${String(rule)}`);
117
+ }
118
+ }
119
+
120
+ // a pointer's id is the sum of its node's id and its parent's id chain.
121
+ // if two pointers share the same id, it means they point to the same node and have identical parent chains.
122
+ // for the purposes of walking the graph, we only need to keep one of them.
123
+ const nextPointers = new Pointers();
124
+ for (const currentPointer of currentPointers) {
125
+ for (const unresolvedNextPointer of currentPointer.fetchNext()) {
126
+ for (const resolvedNextPointer of this.resolvePointer(unresolvedNextPointer)) {
127
+ nextPointers.add(resolvedNextPointer);
128
+ }
129
+ }
130
+ }
131
+ return nextPointers;
132
+ }
133
+
134
+ *resolvePointer(unresolvedPointer: GraphPointer): Generator<GraphPointer> {
135
+ for (const resolvedPointer of unresolvedPointer.resolve()) {
136
+ if (isRuleRef(resolvedPointer.node.rule)) {
137
+ throw new Error("Encountered a reference rule when building pointers to the graph");
138
+ }
139
+ if (isRuleEnd(resolvedPointer.node.rule) && resolvedPointer.parent !== undefined) {
140
+ throw new Error(
141
+ "Encountered an ending rule with a parent when building pointers to the graph",
142
+ );
143
+ }
144
+ yield resolvedPointer;
145
+ }
146
+ }
147
+
148
+ add(src: ValidInput, pointers?: Pointers): Pointers {
149
+ if (typeof src !== "string") {
150
+ throw new Error("src must be a string in graph.add");
151
+ }
152
+ let currentPointers = pointers && pointers.size > 0 ? pointers : this.getInitialPointers();
153
+
154
+ const codePoints = getInputAsCodePoints(src);
155
+ for (const codePoint of codePoints) {
156
+ if (!Number.isInteger(codePoint)) {
157
+ throw new Error("code_point must be an integer!");
158
+ }
159
+ }
160
+
161
+ for (let codePointPos = 0; codePointPos < codePoints.length; codePointPos++) {
162
+ currentPointers = this.parse(currentPointers, codePoints[codePointPos]);
163
+ if (currentPointers.size === 0) {
164
+ throw new InputParseError(codePoints, codePointPos, this.previousCodePoints);
165
+ }
166
+ }
167
+ for (const codePoint of codePoints) {
168
+ this.previousCodePoints.push(codePoint);
169
+ }
170
+ return currentPointers;
171
+ }
172
+
173
+ // generator that yields either the node, or if a reference rule, the referenced node
174
+ // we need this function, as distinct from leveraging the logic in GraphPointer,
175
+ // because that needs a rule ref with already defined nodes; this function is used to _set_ those nodes
176
+ *fetchNodesForRootNode(
177
+ rootNodes: RootNode,
178
+ parent?: GraphPointer,
179
+ ): Generator<[GraphNode, GraphPointer | undefined]> {
180
+ for (const node of rootNodes.values()) {
181
+ if (isRuleRef(node.rule)) {
182
+ yield* this.fetchNodesForRootNode(
183
+ this.getRootNode(node.rule.value),
184
+ new GraphPointer(node, parent),
185
+ );
186
+ } else {
187
+ yield [node, parent];
188
+ }
189
+ }
190
+ }
191
+
192
+ print(pointers: Pointers = new Pointers(), colors = false): string {
193
+ const graphView: string[] = [];
194
+ for (const rootNode of this.#roots.values()) {
195
+ for (const node of rootNode.values()) {
196
+ const opts: PrintOpts = {
197
+ pointers,
198
+ showPosition: true,
199
+ colorize: colors ? colorize : (text) => String(text),
200
+ };
201
+ graphView.push(node.print(opts));
202
+ }
203
+ }
204
+ return graphView.join("\n");
205
+ }
206
+
207
+ *iterateOverPointers(
208
+ pointers: Iterable<GraphPointer>,
209
+ ): Generator<[UnresolvedRule, GraphPointer[]]> {
210
+ const seenRules = new Map<UnresolvedRule, GraphPointer[]>();
211
+ for (const pointer of pointers) {
212
+ const rule = pointer.rule;
213
+ if (isRuleRef(rule)) {
214
+ throw new Error("Encountered a reference rule in the graph");
215
+ }
216
+
217
+ let seenRule = seenRules.get(rule);
218
+ if (seenRule === undefined) {
219
+ seenRule = [];
220
+ seenRules.set(rule, seenRule);
221
+ }
222
+ seenRule.push(pointer);
223
+ }
224
+ yield* seenRules.entries();
225
+ }
226
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/parse-state.test.ts ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { RuleChar } from "./grammar-graph-types.js";
3
+ import type { Graph } from "./graph.js";
4
+ import { GraphNode } from "./graph-node.js";
5
+ import { GraphPointer } from "./graph-pointer.js";
6
+ import { ParseState } from "./parse-state.js";
7
+ import { Pointers } from "./pointers.js";
8
+
9
+ function makeMockGraph() {
10
+ return {
11
+ add: vi.fn((_input: string, pointers: Pointers) => pointers),
12
+ grammar: "sample-grammar",
13
+ };
14
+ }
15
+
16
+ function makeMockPointers(): Pointers {
17
+ return new Pointers(
18
+ new GraphPointer(new GraphNode(new RuleChar([65]), { stackId: 1, pathId: 1, stepId: 1 })),
19
+ );
20
+ }
21
+
22
+ describe("parse state", () => {
23
+ it("constructs with the given graph and pointers", () => {
24
+ const parseState = new ParseState(makeMockGraph() as unknown as Graph, makeMockPointers());
25
+ expect(parseState).toBeInstanceOf(ParseState);
26
+ });
27
+
28
+ it("returns unique rules from pointers", () => {
29
+ const parseState = new ParseState(makeMockGraph() as unknown as Graph, makeMockPointers());
30
+ const rules = [...parseState.rules()];
31
+ expect(rules).toHaveLength(1);
32
+ expect(rules[0]).toBeInstanceOf(RuleChar);
33
+ expect(rules[0].value).toEqual([65]);
34
+ });
35
+
36
+ it("iterates over unique rules using the iterator protocol", () => {
37
+ const parseState = new ParseState(makeMockGraph() as unknown as Graph, makeMockPointers());
38
+ const rules = [...parseState];
39
+ expect(rules).toHaveLength(1);
40
+ expect(rules[0]).toBeInstanceOf(RuleChar);
41
+ });
42
+
43
+ it("adds new input and returns a new parse state with updated pointers", () => {
44
+ const mockGraph = makeMockGraph();
45
+ const pointers = makeMockPointers();
46
+ const parseState = new ParseState(mockGraph as unknown as Graph, pointers);
47
+ const newState = parseState.add("B");
48
+ expect(newState).toBeInstanceOf(ParseState);
49
+ expect(mockGraph.add).toHaveBeenCalledWith("B", pointers);
50
+ });
51
+
52
+ it("rejects non-string input", () => {
53
+ const parseState = new ParseState(makeMockGraph() as unknown as Graph, makeMockPointers());
54
+ expect(() => parseState.add(1 as unknown as string)).toThrow(
55
+ "input text must be of type string",
56
+ );
57
+ });
58
+
59
+ it("calculates the size of unique rules correctly", () => {
60
+ const parseState = new ParseState(makeMockGraph() as unknown as Graph, makeMockPointers());
61
+ expect(parseState.size).toBe(1);
62
+ });
63
+
64
+ it("provides access to the underlying graph grammar", () => {
65
+ const parseState = new ParseState(makeMockGraph() as unknown as Graph, makeMockPointers());
66
+ expect(parseState.grammar).toBe("sample-grammar");
67
+ });
68
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/parse-state.ts ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { ResolvedRule } from "./grammar-graph-types.js";
2
+ import type { Graph } from "./graph.js";
3
+ import { Pointers } from "./pointers.js";
4
+
5
+ export class ParseState implements Iterable<ResolvedRule> {
6
+ readonly #graph: Graph;
7
+ readonly #pointers: Pointers;
8
+
9
+ constructor(graph: Graph, pointers: Pointers) {
10
+ this.#graph = graph;
11
+ this.#pointers = pointers;
12
+ }
13
+
14
+ [Symbol.iterator](): Generator<ResolvedRule> {
15
+ return this.rules();
16
+ }
17
+
18
+ *rules(): Generator<ResolvedRule> {
19
+ const seen = new Set<string>();
20
+ for (const pointer of this.#pointers) {
21
+ const rule = pointer.rule as ResolvedRule;
22
+ const key = JSON.stringify(rule);
23
+ if (!seen.has(key)) {
24
+ seen.add(key);
25
+ yield rule;
26
+ }
27
+ }
28
+ }
29
+
30
+ add(text: string): ParseState {
31
+ if (typeof text !== "string") {
32
+ throw new Error("input text must be of type string");
33
+ }
34
+ const pointers = this.#graph.add(text, this.#pointers);
35
+ return new ParseState(this.#graph, pointers);
36
+ }
37
+
38
+ get size(): number {
39
+ return [...this.rules()].length;
40
+ }
41
+
42
+ get grammar(): string {
43
+ return this.#graph.grammar;
44
+ }
45
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/pointers.ts ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { GraphPointer } from "./graph-pointer.js";
2
+
3
+ export class Pointers implements Iterable<GraphPointer> {
4
+ readonly #pointers = new Map<string, GraphPointer>();
5
+
6
+ constructor(...pointers: GraphPointer[]) {
7
+ for (const pointer of pointers) {
8
+ this.add(pointer);
9
+ }
10
+ }
11
+
12
+ add(pointer: GraphPointer): void {
13
+ this.#pointers.set(pointer.id, pointer);
14
+ }
15
+
16
+ [Symbol.iterator](): Iterator<GraphPointer> {
17
+ return this.#pointers.values();
18
+ }
19
+
20
+ get size(): number {
21
+ return this.#pointers.size;
22
+ }
23
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/print.test.ts ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import type { Colorize } from "./colorize.js";
3
+ import { RuleChar } from "./grammar-graph-types.js";
4
+ import type { GraphNode } from "./graph-node.js";
5
+ import type { GraphPointer } from "./graph-pointer.js";
6
+ import { getParentStackId } from "./get-parent-stack-id.js";
7
+ import { printGraphNode, printGraphPointer } from "./print.js";
8
+ import { RuleRef } from "./rule-ref.js";
9
+
10
+ vi.mock("./get-parent-stack-id.js", () => ({
11
+ getParentStackId: vi.fn(),
12
+ }));
13
+
14
+ const COLORS: Record<string, string> = {
15
+ "\x1b[34m": "BLUE",
16
+ "\x1b[36m": "CYAN",
17
+ "\x1b[32m": "GREEN",
18
+ "\x1b[31m": "RED",
19
+ "\x1b[90m": "GRAY",
20
+ "\x1b[33m": "YELLOW",
21
+ };
22
+
23
+ const mockColorize: Colorize = (text, color) => {
24
+ if (!(color in COLORS)) {
25
+ throw new Error(`Invalid color: ${color}`);
26
+ }
27
+ return `[${COLORS[color]}]:${text}`;
28
+ };
29
+
30
+ function createMockNode(id: string, rule: unknown, next?: unknown) {
31
+ return {
32
+ id,
33
+ rule,
34
+ next,
35
+ print: () => `Node(${id})`,
36
+ } as unknown as GraphNode;
37
+ }
38
+
39
+ describe("print", () => {
40
+ beforeEach(() => {
41
+ vi.mocked(getParentStackId).mockReset();
42
+ });
43
+
44
+ describe("printGraphPointer", () => {
45
+ it("prints graph pointer details correctly", () => {
46
+ const mockNode = createMockNode("1", new RuleRef(100));
47
+ const mockPointer = {
48
+ node: mockNode,
49
+ parent: undefined,
50
+ print: () => `Pointer to ${mockNode.id}`,
51
+ } as unknown as GraphPointer;
52
+ vi.mocked(getParentStackId).mockReturnValue("foo");
53
+ const result = printGraphPointer(mockPointer)({
54
+ colorize: mockColorize,
55
+ pointers: [],
56
+ showPosition: false,
57
+ });
58
+ expect(result).toBe("[RED]:*foo");
59
+ });
60
+ });
61
+
62
+ describe("printGraphNode", () => {
63
+ it("prints a graph node with a character rule", () => {
64
+ const mockNode = createMockNode("1", new RuleChar([65]));
65
+ const result = printGraphNode(mockNode)({
66
+ colorize: mockColorize,
67
+ showPosition: false,
68
+ pointers: [],
69
+ });
70
+ expect(result).toBe("[GRAY]:[[YELLOW]:A[GRAY]:]");
71
+ });
72
+
73
+ it("prints a graph node with a rule reference", () => {
74
+ const mockNode = createMockNode("1", new RuleRef(200));
75
+ const result = printGraphNode(mockNode)({
76
+ colorize: mockColorize,
77
+ showPosition: true,
78
+ pointers: [],
79
+ });
80
+ expect(result).toBe("[BLUE]:{[GRAY]:1[BLUE]:}[GRAY]:Ref([GREEN]:200[GRAY]:)");
81
+ });
82
+ });
83
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/print.ts ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Color, type Colorize } from "./colorize.js";
2
+ import type { PrintOpts } from "./grammar-graph-types.js";
3
+ import type { GraphNode } from "./graph-node.js";
4
+ import type { GraphPointer } from "./graph-pointer.js";
5
+ import { getParentStackId } from "./get-parent-stack-id.js";
6
+ import { isRange, isRuleChar, isRuleRef } from "./type-guards.js";
7
+
8
+ export function printGraphPointer(pointer: GraphPointer): (opts: PrintOpts) => string {
9
+ return (opts) => {
10
+ const col: Colorize = opts.colorize;
11
+ return col(`*${getParentStackId(pointer, col)}`, Color.RED);
12
+ };
13
+ }
14
+
15
+ export function printGraphNode(node: GraphNode): (opts: PrintOpts) => string {
16
+ return (opts) => {
17
+ const pointers = opts.pointers;
18
+ const col: Colorize = opts.colorize;
19
+ const rule = node.rule;
20
+ const parts: string[] = [];
21
+ if (opts.showPosition) {
22
+ parts.push(col("{", Color.BLUE), col(node.id, Color.GRAY), col("}", Color.BLUE));
23
+ }
24
+
25
+ if (isRuleChar(rule)) {
26
+ const values = rule.value.map((value) =>
27
+ isRange(value)
28
+ ? value.map((codePoint) => col(getChar(codePoint), Color.YELLOW)).join("")
29
+ : getChar(value),
30
+ );
31
+ parts.push(col("[", Color.GRAY), col(values.join(""), Color.YELLOW), col("]", Color.GRAY));
32
+ } else if (isRuleRef(rule)) {
33
+ parts.push(col("Ref(", Color.GRAY), col(rule.value, Color.GREEN), col(")", Color.GRAY));
34
+ } else {
35
+ parts.push(col(rule.type, Color.YELLOW));
36
+ }
37
+
38
+ for (const pointer of pointers) {
39
+ const pointerParts: string[] = [];
40
+ if (pointer.node === node) {
41
+ pointerParts.push(pointer.print(opts));
42
+ }
43
+
44
+ if (pointerParts.length > 0) {
45
+ parts.push(col("[", Color.GRAY), col(pointerParts.join(""), Color.YELLOW), col("]", Color.GRAY));
46
+ }
47
+ }
48
+
49
+ const partsToReturn = [parts.join("")];
50
+ if (node.next) {
51
+ partsToReturn.push(node.next.print(opts));
52
+ }
53
+
54
+ return partsToReturn.join(col("-> ", Color.GRAY));
55
+ };
56
+ }
57
+
58
+ export function getChar(charCode: number): string {
59
+ const char = String.fromCodePoint(charCode);
60
+ if (char === "\n") {
61
+ return "\\n";
62
+ }
63
+ return char;
64
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/rule-ref.test.ts ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import type { GraphNode } from "./graph-node.js";
3
+ import { RuleRef } from "./rule-ref.js";
4
+
5
+ describe("rule ref", () => {
6
+ it("initializes with a given value", () => {
7
+ const ruleRef = new RuleRef(123);
8
+ expect(ruleRef.value).toBe(123);
9
+ });
10
+
11
+ it("allows setting and getting nodes", () => {
12
+ const mockNodes = new Set<GraphNode>([{} as GraphNode]);
13
+ const ruleRef = new RuleRef(123);
14
+ ruleRef.nodes = mockNodes;
15
+ expect(ruleRef.nodes).toBe(mockNodes);
16
+ });
17
+
18
+ it("throws an error if trying to get nodes before setting", () => {
19
+ const ruleRef = new RuleRef(123);
20
+ expect(() => ruleRef.nodes).toThrow("Nodes are not set");
21
+ });
22
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/rule-ref.ts ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { GraphNode } from "./graph-node.js";
2
+
3
+ export class RuleRef {
4
+ readonly value: number;
5
+ #nodes: Set<GraphNode> | undefined;
6
+
7
+ constructor(value: number) {
8
+ this.value = value;
9
+ }
10
+
11
+ get type(): "RuleRef" {
12
+ return "RuleRef";
13
+ }
14
+
15
+ get nodes(): Set<GraphNode> {
16
+ if (this.#nodes === undefined) {
17
+ throw new Error("Nodes are not set");
18
+ }
19
+ return this.#nodes;
20
+ }
21
+
22
+ set nodes(nodes: Set<GraphNode>) {
23
+ this.#nodes = nodes;
24
+ }
25
+
26
+ equals(other: unknown): boolean {
27
+ return other instanceof RuleRef && other.value === this.value;
28
+ }
29
+
30
+ toString(): string {
31
+ return `RuleRef(value=${this.value})`;
32
+ }
33
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/type-guards.test.ts ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { RuleChar, RuleCharExclude, RuleEnd } from "./grammar-graph-types.js";
3
+ import { GraphNode } from "./graph-node.js";
4
+ import { GraphPointer } from "./graph-pointer.js";
5
+ import { RuleRef } from "./rule-ref.js";
6
+ import {
7
+ isGraphPointerRuleCharExclude,
8
+ isGraphPointerRuleChar,
9
+ isGraphPointerRuleEnd,
10
+ isGraphPointerRuleRef,
11
+ isRange,
12
+ isRule,
13
+ isRuleChar,
14
+ isRuleCharExclude,
15
+ isRuleEnd,
16
+ isRuleRef,
17
+ } from "./type-guards.js";
18
+
19
+ const meta = { stackId: 1, pathId: 2, stepId: 3 };
20
+
21
+ function pointerTo(rule: GraphNode["rule"]): GraphPointer {
22
+ return new GraphPointer(new GraphNode(rule, meta));
23
+ }
24
+
25
+ describe("rule type guards", () => {
26
+ describe("isGraphPointerRuleRef", () => {
27
+ it("returns true", () => {
28
+ expect(isGraphPointerRuleRef(pointerTo(new RuleRef(1)))).toBe(true);
29
+ });
30
+
31
+ it("returns false", () => {
32
+ expect(isGraphPointerRuleRef(pointerTo(new RuleEnd()))).toBe(false);
33
+ });
34
+ });
35
+
36
+ describe("isGraphPointerRuleEnd", () => {
37
+ it("returns false", () => {
38
+ expect(isGraphPointerRuleEnd(pointerTo(new RuleRef(1)))).toBe(false);
39
+ });
40
+
41
+ it("returns true", () => {
42
+ expect(isGraphPointerRuleEnd(pointerTo(new RuleEnd()))).toBe(true);
43
+ });
44
+ });
45
+
46
+ describe("isGraphPointerRuleChar", () => {
47
+ it("returns true", () => {
48
+ expect(isGraphPointerRuleChar(pointerTo(new RuleChar([97])))).toBe(true);
49
+ });
50
+
51
+ it("returns false", () => {
52
+ expect(isGraphPointerRuleChar(pointerTo(new RuleEnd()))).toBe(false);
53
+ });
54
+ });
55
+
56
+ describe("isGraphPointerRuleCharExclude", () => {
57
+ it("returns true", () => {
58
+ expect(isGraphPointerRuleCharExclude(pointerTo(new RuleCharExclude([97])))).toBe(true);
59
+ });
60
+
61
+ it("returns false", () => {
62
+ expect(isGraphPointerRuleCharExclude(pointerTo(new RuleChar([97])))).toBe(false);
63
+ });
64
+ });
65
+
66
+ describe("isRule", () => {
67
+ it("returns false for null", () => {
68
+ expect(isRule(null)).toBe(false);
69
+ });
70
+
71
+ it("returns false for a non-rule", () => {
72
+ expect(isRule({ type: "invalid", value: [] })).toBe(false);
73
+ });
74
+
75
+ it("returns true for valid rule objects", () => {
76
+ expect(isRule(new RuleChar([65, [66, 67]]))).toBe(true);
77
+ });
78
+ });
79
+
80
+ describe("isRuleRef", () => {
81
+ it("returns true for valid rule refs", () => {
82
+ expect(isRuleRef(new RuleRef(1))).toBe(true);
83
+ });
84
+
85
+ it("returns false for invalid rule refs", () => {
86
+ expect(isRuleRef(new RuleChar([65]))).toBe(false);
87
+ });
88
+ });
89
+
90
+ describe("isRuleEnd", () => {
91
+ it("returns true for valid rule ends", () => {
92
+ expect(isRuleEnd(new RuleEnd())).toBe(true);
93
+ });
94
+
95
+ it("returns false for invalid rule ends", () => {
96
+ expect(isRuleEnd(new RuleChar([65]))).toBe(false);
97
+ });
98
+ });
99
+
100
+ describe("isRuleChar", () => {
101
+ it("returns true for valid rule chars", () => {
102
+ expect(isRuleChar(new RuleChar([65]))).toBe(true);
103
+ });
104
+
105
+ it("returns false for invalid rule chars", () => {
106
+ expect(isRuleChar(new RuleCharExclude([65]))).toBe(false);
107
+ });
108
+ });
109
+
110
+ describe("isRuleCharExclude", () => {
111
+ it("returns true for valid rule chars", () => {
112
+ expect(isRuleCharExclude(new RuleCharExclude([65]))).toBe(true);
113
+ });
114
+
115
+ it("returns false for invalid rule chars", () => {
116
+ expect(isRuleCharExclude(new RuleChar([65]))).toBe(false);
117
+ });
118
+ });
119
+
120
+ describe("isRange", () => {
121
+ it("returns true for valid ranges", () => {
122
+ expect(isRange([1, 10])).toBe(true);
123
+ });
124
+
125
+ it.each([[[1, "10"]], [[1]], [5]])("returns false for invalid range %j", (value) => {
126
+ expect(isRange(value)).toBe(false);
127
+ });
128
+ });
129
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-graph/type-guards.ts ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ type Range,
3
+ RuleChar,
4
+ RuleCharExclude,
5
+ RuleEnd,
6
+ type UnresolvedRule,
7
+ } from "./grammar-graph-types.js";
8
+ import type { GraphPointer } from "./graph-pointer.js";
9
+ import { RuleRef } from "./rule-ref.js";
10
+
11
+ export function isRule(rule: unknown): rule is UnresolvedRule {
12
+ return (
13
+ rule !== null &&
14
+ rule !== undefined &&
15
+ (rule instanceof RuleChar ||
16
+ rule instanceof RuleCharExclude ||
17
+ rule instanceof RuleEnd ||
18
+ rule instanceof RuleRef)
19
+ );
20
+ }
21
+
22
+ export function isRuleRef(rule: unknown): rule is RuleRef {
23
+ return rule instanceof RuleRef;
24
+ }
25
+
26
+ export function isRuleEnd(rule: unknown): rule is RuleEnd {
27
+ return rule instanceof RuleEnd;
28
+ }
29
+
30
+ export function isRuleChar(rule: unknown): rule is RuleChar {
31
+ return rule instanceof RuleChar;
32
+ }
33
+
34
+ export function isRuleCharExclude(rule: unknown): rule is RuleCharExclude {
35
+ return rule instanceof RuleCharExclude;
36
+ }
37
+
38
+ export function isRange(input: unknown): input is Range {
39
+ return (
40
+ Array.isArray(input) &&
41
+ input.length === 2 &&
42
+ input.every((n) => Number.isInteger(n))
43
+ );
44
+ }
45
+
46
+ export function isGraphPointerRuleRef(pointer: GraphPointer): boolean {
47
+ return isRuleRef(pointer.rule);
48
+ }
49
+
50
+ export function isGraphPointerRuleEnd(pointer: GraphPointer): boolean {
51
+ return isRuleEnd(pointer.rule);
52
+ }
53
+
54
+ export function isGraphPointerRuleChar(pointer: GraphPointer): boolean {
55
+ return isRuleChar(pointer.rule);
56
+ }
57
+
58
+ export function isGraphPointerRuleCharExclude(pointer: GraphPointer): boolean {
59
+ return isRuleCharExclude(pointer.rule);
60
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-parser/build-rule-stack.test.ts ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { type Range, RuleChar, RuleCharExclude, RuleEnd } from "../grammar-graph/grammar-graph-types.js";
3
+ import { RuleRef } from "../grammar-graph/rule-ref.js";
4
+ import {
5
+ type InternalRuleDef,
6
+ InternalRuleDefAlt,
7
+ InternalRuleDefChar,
8
+ InternalRuleDefCharAlt,
9
+ InternalRuleDefCharNot,
10
+ InternalRuleDefCharRngUpper,
11
+ InternalRuleDefEnd,
12
+ InternalRuleDefReference,
13
+ } from "../rules-builder/rules-builder-types.js";
14
+ import { buildRuleStack } from "./build-rule-stack.js";
15
+
16
+ function ichar(value: number[]): InternalRuleDefChar {
17
+ return new InternalRuleDefChar(value);
18
+ }
19
+
20
+ function icharAlt(value: number): InternalRuleDefCharAlt {
21
+ return new InternalRuleDefCharAlt(value);
22
+ }
23
+
24
+ function icharRngUpper(value: number): InternalRuleDefCharRngUpper {
25
+ return new InternalRuleDefCharRngUpper(value);
26
+ }
27
+
28
+ function icharNot(value: number[]): InternalRuleDefCharNot {
29
+ return new InternalRuleDefCharNot(value);
30
+ }
31
+
32
+ function ialt(): InternalRuleDefAlt {
33
+ return new InternalRuleDefAlt();
34
+ }
35
+
36
+ function iend(): InternalRuleDefEnd {
37
+ return new InternalRuleDefEnd();
38
+ }
39
+
40
+ function iref(value: number): InternalRuleDefReference {
41
+ return new InternalRuleDefReference(value);
42
+ }
43
+
44
+ function makeRange(lower: number | string, upper: number | string): Range {
45
+ return [
46
+ typeof lower === "number" ? lower : (lower.codePointAt(0) as number),
47
+ typeof upper === "number" ? upper : (upper.codePointAt(0) as number),
48
+ ];
49
+ }
50
+
51
+ describe("build rule stack", () => {
52
+ it("it builds rule stack for a single path", () => {
53
+ expect(buildRuleStack([ichar([120])])).toStrictEqual([[new RuleChar([120]), new RuleEnd()]]);
54
+ });
55
+
56
+ it("it builds rule stack for two alternate paths", () => {
57
+ const result = buildRuleStack([ichar([120]), ialt(), ichar([121])]);
58
+ expect(result).toStrictEqual([[new RuleChar([120]), new RuleEnd()], [new RuleChar([121]), new RuleEnd()]]);
59
+ });
60
+
61
+ it("it builds rule stack for three alternate paths", () => {
62
+ expect(buildRuleStack([ichar([120]), ialt(), ichar([121]), ialt(), ichar([122])])).toStrictEqual([
63
+ [new RuleChar([120]), new RuleEnd()],
64
+ [new RuleChar([121]), new RuleEnd()],
65
+ [new RuleChar([122]), new RuleEnd()],
66
+ ]);
67
+ });
68
+
69
+ it("it builds rule stack for char not", () => {
70
+ expect(buildRuleStack([icharNot([120]), ialt(), icharNot([121]), ialt(), icharNot([122])])).toStrictEqual([
71
+ [new RuleCharExclude([120]), new RuleEnd()],
72
+ [new RuleCharExclude([121]), new RuleEnd()],
73
+ [new RuleCharExclude([122]), new RuleEnd()],
74
+ ]);
75
+ });
76
+
77
+ it("it builds rule stack for mixed char and char not", () => {
78
+ expect(buildRuleStack([icharNot([120]), ialt(), ichar([121]), ialt(), icharNot([122])])).toStrictEqual([
79
+ [new RuleCharExclude([120]), new RuleEnd()],
80
+ [new RuleChar([121]), new RuleEnd()],
81
+ [new RuleCharExclude([122]), new RuleEnd()],
82
+ ]);
83
+ });
84
+
85
+ it("it builds rule stack for char not with two characters and a range", () => {
86
+ const result = buildRuleStack([icharNot([120]), icharAlt(121), icharAlt(122), icharRngUpper(130)]);
87
+ expect(result).toStrictEqual([[new RuleCharExclude([120, 121, makeRange(122, 130)]), new RuleEnd()]]);
88
+ });
89
+
90
+ describe("ranges", () => {
91
+ it.each([
92
+ [
93
+ "[a-z]",
94
+ [ichar([97]), icharRngUpper(122), iend()],
95
+ [[new RuleChar([makeRange("a", "z")]), new RuleEnd()]],
96
+ ],
97
+ [
98
+ "[a-zA-Z]",
99
+ [ichar([97]), icharRngUpper(122), icharAlt(65), icharRngUpper(90), iend()],
100
+ [[new RuleChar([makeRange("a", "z"), makeRange("A", "Z")]), new RuleEnd()]],
101
+ ],
102
+ [
103
+ "[a-zA-Z0-9]",
104
+ [
105
+ ichar([97]),
106
+ icharRngUpper(122),
107
+ icharAlt(65),
108
+ icharRngUpper(90),
109
+ icharAlt(48),
110
+ icharRngUpper(57),
111
+ iend(),
112
+ ],
113
+ [
114
+ [
115
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z"), makeRange("0", "9")]),
116
+ new RuleEnd(),
117
+ ],
118
+ ],
119
+ ],
120
+ ])("it builds rule stack for a char with no modifiers", (grammar, input, expected) => {
121
+ const result = buildRuleStack(input);
122
+ expect(result).toStrictEqual(expected);
123
+ });
124
+ });
125
+ it.each([
126
+ [
127
+ "[a-z]?",
128
+ [ichar([97]), icharRngUpper(122), ialt(), iend()],
129
+ [[new RuleChar([makeRange("a", "z")]), new RuleEnd()], [new RuleEnd()]],
130
+ ],
131
+ [
132
+ "[a-zA-Z]?",
133
+ [ichar([97]), icharRngUpper(122), icharAlt(65), icharRngUpper(90), ialt(), iend()],
134
+ [
135
+ [new RuleChar([makeRange("a", "z"), makeRange("A", "Z")]), new RuleEnd()],
136
+ [new RuleEnd()],
137
+ ],
138
+ ],
139
+ [
140
+ "[a-zA-Z0-9]?",
141
+ [
142
+ ichar([97]),
143
+ icharRngUpper(122),
144
+ icharAlt(65),
145
+ icharRngUpper(90),
146
+ icharAlt(48),
147
+ icharRngUpper(57),
148
+ ialt(),
149
+ iend(),
150
+ ],
151
+ [
152
+ [
153
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z"), makeRange("0", "9")]),
154
+ new RuleEnd(),
155
+ ],
156
+ [new RuleEnd()],
157
+ ],
158
+ ],
159
+ ])("it builds rule stack for a char with question mark modifier", (grammar, input, expected) => {
160
+ expect(buildRuleStack(input)).toStrictEqual(expected);
161
+ });
162
+
163
+ it.each([
164
+ [
165
+ "[a-z]+",
166
+ [
167
+ ichar([97]),
168
+ icharRngUpper(122),
169
+ iref(1),
170
+ ialt(),
171
+ ichar([97]),
172
+ icharRngUpper(122),
173
+ iend(),
174
+ ],
175
+ [
176
+ [new RuleChar([makeRange("a", "z")]), new RuleRef(1), new RuleEnd()],
177
+ [new RuleChar([makeRange("a", "z")]), new RuleEnd()],
178
+ ],
179
+ ],
180
+ [
181
+ "[a-zA-Z]+",
182
+ [
183
+ ichar([97]),
184
+ icharRngUpper(122),
185
+ icharAlt(65),
186
+ icharRngUpper(90),
187
+ iref(1),
188
+ ialt(),
189
+ ichar([97]),
190
+ icharRngUpper(122),
191
+ icharAlt(65),
192
+ icharRngUpper(90),
193
+ iend(),
194
+ ],
195
+ [
196
+ [
197
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z")]),
198
+ new RuleRef(1),
199
+ new RuleEnd(),
200
+ ],
201
+ [new RuleChar([makeRange("a", "z"), makeRange("A", "Z")]), new RuleEnd()],
202
+ ],
203
+ ],
204
+ [
205
+ "[a-zA-Z0-9]+",
206
+ [
207
+ ichar([97]),
208
+ icharRngUpper(122),
209
+ icharAlt(65),
210
+ icharRngUpper(90),
211
+ icharAlt(48),
212
+ icharRngUpper(57),
213
+ iref(1),
214
+ ialt(),
215
+ ichar([97]),
216
+ icharRngUpper(122),
217
+ icharAlt(65),
218
+ icharRngUpper(90),
219
+ icharAlt(48),
220
+ icharRngUpper(57),
221
+ iend(),
222
+ ],
223
+ [
224
+ [
225
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z"), makeRange("0", "9")]),
226
+ new RuleRef(1),
227
+ new RuleEnd(),
228
+ ],
229
+ [
230
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z"), makeRange("0", "9")]),
231
+ new RuleEnd(),
232
+ ],
233
+ ],
234
+ ],
235
+ ])("it builds rule stack for a char with plus modifier", (grammar, input, expected) => {
236
+ const result = buildRuleStack(input);
237
+ expect(result).toStrictEqual(expected);
238
+ });
239
+
240
+ it.each([
241
+ [
242
+ "[a-z]*",
243
+ [ichar([97]), icharRngUpper(122), iref(1), ialt(), iend()],
244
+ [
245
+ [new RuleChar([makeRange("a", "z")]), new RuleRef(1), new RuleEnd()],
246
+ [new RuleEnd()],
247
+ ],
248
+ ],
249
+ [
250
+ "[a-zA-Z]*",
251
+ [
252
+ ichar([97]),
253
+ icharRngUpper(122),
254
+ icharAlt(65),
255
+ icharRngUpper(90),
256
+ iref(1),
257
+ ialt(),
258
+ iend(),
259
+ ],
260
+ [
261
+ [
262
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z")]),
263
+ new RuleRef(1),
264
+ new RuleEnd(),
265
+ ],
266
+ [new RuleEnd()],
267
+ ],
268
+ ],
269
+ [
270
+ "[a-zA-Z0-9]*",
271
+ [
272
+ ichar([97]),
273
+ icharRngUpper(122),
274
+ icharAlt(65),
275
+ icharRngUpper(90),
276
+ icharAlt(48),
277
+ icharRngUpper(57),
278
+ iref(1),
279
+ ialt(),
280
+ iend(),
281
+ ],
282
+ [
283
+ [
284
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z"), makeRange("0", "9")]),
285
+ new RuleRef(1),
286
+ new RuleEnd(),
287
+ ],
288
+ [new RuleEnd()],
289
+ ],
290
+ ],
291
+ ])("it builds rule stack for a char with asterisk modifier", (grammar, input, expected) => {
292
+ const result = buildRuleStack(input);
293
+ expect(result).toStrictEqual(expected);
294
+ });
295
+
296
+ it("char with range", () => {
297
+ const input: InternalRuleDef[] = [
298
+ ichar([97]),
299
+ icharRngUpper(122),
300
+ icharAlt(65),
301
+ icharRngUpper(90),
302
+ icharAlt(95),
303
+ iend(),
304
+ ];
305
+ expect(buildRuleStack(input)).toStrictEqual([[new RuleChar([makeRange("a", "z"), makeRange("A", "Z"), 95]), new RuleEnd()]]);
306
+ });
307
+
308
+ it("it builds rule stack for situation root ws plus newline ws star", () => {
309
+ const input: InternalRuleDef[] = [ichar([32]), icharAlt(92), icharAlt(110), iref(4), ialt(), iend()];
310
+ const expected = [[new RuleChar([32, 92, 110]), new RuleRef(4), new RuleEnd()], [new RuleEnd()]];
311
+ expect(buildRuleStack(input)).toStrictEqual(expected);
312
+ });
313
+
314
+ it("it builds rule stack for situation root char plus char range", () => {
315
+ const input: InternalRuleDef[] = [
316
+ ichar([97]),
317
+ icharRngUpper(122),
318
+ icharAlt(65),
319
+ icharRngUpper(90),
320
+ iref(1),
321
+ ialt(),
322
+ ichar([97]),
323
+ icharRngUpper(122),
324
+ icharAlt(65),
325
+ icharRngUpper(90),
326
+ iend(),
327
+ ];
328
+ const expected = [
329
+ [
330
+ new RuleChar([makeRange("a", "z"), makeRange("A", "Z")]),
331
+ new RuleRef(1),
332
+ new RuleEnd(),
333
+ ],
334
+ [new RuleChar([makeRange("a", "z"), makeRange("A", "Z")]), new RuleEnd()],
335
+ ];
336
+ expect(buildRuleStack(input)).toStrictEqual(expected);
337
+ });
338
+ });
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/grammar-parser/build-rule-stack.ts ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ type CharValue,
3
+ type Range,
4
+ RuleChar,
5
+ RuleCharExclude,
6
+ RuleEnd,
7
+ type UnresolvedRule,
8
+ } from "../grammar-graph/grammar-graph-types.js";
9
+ import { RuleRef } from "../grammar-graph/rule-ref.js";
10
+ import { isRange } from "../grammar-graph/type-guards.js";
11
+ import {
12
+ type InternalRuleDef,
13
+ InternalRuleDefAlt,
14
+ InternalRuleDefChar,
15
+ InternalRuleDefCharAlt,
16
+ InternalRuleDefCharNot,
17
+ InternalRuleDefCharRngUpper,
18
+ InternalRuleDefEnd,
19
+ InternalRuleDefReference,
20
+ } from "../rules-builder/rules-builder-types.js";
21
+
22
+ function makeCharRule(
23
+ ruleDef: InternalRuleDefChar | InternalRuleDefCharNot,
24
+ ): RuleChar | RuleCharExclude {
25
+ if (ruleDef instanceof InternalRuleDefCharNot) {
26
+ return new RuleCharExclude(ruleDef.value);
27
+ }
28
+ return new RuleChar(ruleDef.value);
29
+ }
30
+
31
+ export function buildRuleStack(linearRules: InternalRuleDef[]): UnresolvedRule[][] {
32
+ let paths: UnresolvedRule[] = [];
33
+ const stack: UnresolvedRule[][] = [];
34
+ let idx = 0;
35
+
36
+ while (idx < linearRules.length) {
37
+ const ruleDef = linearRules[idx];
38
+ if (ruleDef instanceof InternalRuleDefChar || ruleDef instanceof InternalRuleDefCharNot) {
39
+ // this could be a single char, or a range, or a sequence of alts; we don't know until we step through it.
40
+ const charRule = makeCharRule(ruleDef);
41
+ idx += 1;
42
+ let rule: InternalRuleDef | undefined = linearRules[idx];
43
+ while (
44
+ idx < linearRules.length &&
45
+ (rule instanceof InternalRuleDefCharRngUpper || rule instanceof InternalRuleDefCharAlt)
46
+ ) {
47
+ if (rule instanceof InternalRuleDefCharRngUpper) {
48
+ // previous rule value should be a number
49
+ const prevValue: CharValue | undefined = charRule.value.pop();
50
+ if (isRange(prevValue)) {
51
+ throw new Error(
52
+ `Unexpected range, expected a number but got an array: ${JSON.stringify(prevValue)}`,
53
+ );
54
+ }
55
+ if (prevValue === undefined) {
56
+ throw new Error("Unexpected undefined value");
57
+ }
58
+
59
+ const range: Range = [prevValue, rule.value];
60
+ charRule.value.push(range);
61
+ }
62
+ if (rule instanceof InternalRuleDefCharAlt) {
63
+ charRule.value.push(rule.value);
64
+ }
65
+ idx += 1;
66
+ rule = linearRules[idx];
67
+ }
68
+ paths.push(charRule);
69
+ } else if (ruleDef instanceof InternalRuleDefAlt) {
70
+ if (paths.length === 0) {
71
+ throw new Error("Encountered alt without anything before it");
72
+ }
73
+ paths.push(new RuleEnd());
74
+ stack.push(paths);
75
+ paths = [];
76
+ idx += 1;
77
+ } else if (ruleDef instanceof InternalRuleDefEnd) {
78
+ paths.push(new RuleEnd());
79
+ idx += 1;
80
+ } else if (ruleDef instanceof InternalRuleDefReference) {
81
+ paths.push(new RuleRef(ruleDef.value));
82
+ idx += 1;
83
+ } else if (ruleDef instanceof InternalRuleDefCharAlt) {
84
+ throw new Error(`Encountered char alt, should be handled by above block: ${ruleDef.value}`);
85
+ } else {
86
+ throw new Error(`Unsupported rule type: ${ruleDef.kind}`);
87
+ }
88
+ }
89
+
90
+ if (!(paths[paths.length - 1] instanceof RuleEnd)) {
91
+ paths.push(new RuleEnd());
92
+ }
93
+
94
+ stack.push(paths);
95
+ return stack;
96
+ }
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/index.ts ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export { GBNF } from "./GBNF.js";
2
+ export { ParseState } from "./grammar-graph/parse-state.js";
3
+ export {
4
+ RuleChar,
5
+ RuleCharExclude,
6
+ RuleEnd,
7
+ type ResolvedRule,
8
+ type UnresolvedRule,
9
+ } from "./grammar-graph/grammar-graph-types.js";
10
+ export { GrammarParseError, InputParseError } from "./utils/errors/index.js";
11
+ export type { ValidInput } from "./utils/errors/index.js";
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/rules-builder/index.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ export { GrammarParseError, InputParseError } from "../utils/errors/index.js";
2
+ export { RulesBuilder } from "./rules-builder.js";
v2-models-effort/runs/20261017T174414Z_0973797c/ported_implementation/gbnf/rules-builder/is-word-char.test.ts ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { isWordChar } from "./is-word-char.js";
3
+
4
+ describe("is word char", () => {
5
+ it("returns true for lowercase letters", () => {
6
+ expect(isWordChar("a")).toBe(true);
7
+ expect(isWordChar("z")).toBe(true);
8
+ });
9
+
10
+ it("returns true for uppercase letters", () => {
11
+ expect(isWordChar("A")).toBe(true);
12
+ expect(isWordChar("Z")).toBe(true);
13
+ });
14
+
15
+ it("returns false for digits", () => {
16
+ expect(isWordChar("0")).toBe(false);
17
+ expect(isWordChar("9")).toBe(false);
18
+ });
19
+
20
+ it("returns false for non-word characters", () => {
21
+ expect(isWordChar("-")).toBe(false);
22
+ expect(isWordChar("@")).toBe(false);
23
+ expect(isWordChar("_")).toBe(false);
24
+ expect(isWordChar("?")).toBe(false);
25
+ expect(isWordChar(" ")).toBe(false);
26
+ });
27
+ });