-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Hermetic Liquid Template Recording and Replay System #1975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tobi
wants to merge
14
commits into
main
Choose a base branch
from
liquid-hermetic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1b1a40c
make theme_runner actually useful outside of the performance benchmarks
tobi 41d0d6d
the performance runner wasn't actually working before
tobi baee70a
also fix profiler
tobi 2420853
Apply suggestions from code review
tobi 14e70cf
memory-profiler update
tobi 544b512
more profile cleanups
tobi 211d11c
Implement hermetic template recording and replay system
tobi b939f24
Add ProductDrop class for proper Liquid Drop object handling
tobi 0276b2d
Reorder JSON structure and add CLI tools for better usability
tobi 5bd6ea7
Implement TrackableHash/Array wrappers for hermetic variable recording
tobi e624e56
Fix loop event processing to prevent Hash-to-Array conversion
tobi 548e949
Add comprehensive CLI roundtrip unit test
tobi 7956439
Update replayer and file system components for hermetic recording
tobi f8c4ea5
Update integration tests and test helper for hermetic recording
tobi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require_relative '../lib/liquid' | ||
| require_relative '../performance/theme_runner' | ||
|
|
||
| def usage | ||
| puts <<~USAGE | ||
| Usage: liquid-record [options] <output_file> <theme_name> [template_name] | ||
|
|
||
| Records liquid template execution to a JSON file for hermetic replay. | ||
|
|
||
| Arguments: | ||
| output_file - Path to save the recording JSON file | ||
| theme_name - Name of theme in performance/tests/ (e.g., vogue, tribble) | ||
| template_name - Specific template to record (optional, defaults to 'index') | ||
|
|
||
| Options: | ||
| --verify - After recording, replay and verify output matches (shows diff if different) | ||
|
|
||
| Examples: | ||
| liquid-record recording.json vogue | ||
| liquid-record product.json vogue product | ||
| liquid-record --verify product.json vogue product | ||
| USAGE | ||
| end | ||
|
|
||
| def main | ||
| # Parse options | ||
| verify_mode = false | ||
| args = ARGV.dup | ||
|
|
||
| if args.include?('--verify') | ||
| verify_mode = true | ||
| args.delete('--verify') | ||
| end | ||
|
|
||
| if args.length < 2 || args.length > 3 | ||
| usage | ||
| exit 1 | ||
| end | ||
|
|
||
| output_file = args[0] | ||
| theme_name = args[1] | ||
| template_name = args[2] || 'index' | ||
|
|
||
| test_name = "#{theme_name}/#{template_name}.liquid" | ||
|
|
||
| puts "Recording template execution: #{test_name}" | ||
| puts "Output file: #{output_file}" | ||
| STDOUT.flush | ||
|
|
||
| begin | ||
| original_output = nil | ||
| Liquid::TemplateRecorder.record(output_file) do | ||
| # Use ThemeRunner to get realistic data, but parse template during recording | ||
| theme_runner = ThemeRunner.new | ||
| test = theme_runner.find_test(test_name) | ||
| compiled = theme_runner.send(:compile_test, test) | ||
|
|
||
| # Parse the template within the recording context so our wrapper can capture it | ||
| template = Liquid::Template.parse(test[:liquid]) | ||
| original_output = template.render(compiled[:assigns]) | ||
| original_output | ||
| end | ||
|
|
||
| puts "Recording completed successfully!" | ||
| puts "File size: #{File.size(output_file)} bytes" | ||
| STDOUT.flush | ||
|
|
||
| # Run verification if requested | ||
| if verify_mode | ||
| puts | ||
| puts "Running verification..." | ||
| STDOUT.flush | ||
| verify_recording(output_file, original_output) | ||
| end | ||
| rescue => e | ||
| puts "Error during recording: #{e.message}" | ||
| puts e.backtrace.first(5) | ||
| exit 1 | ||
| end | ||
| end | ||
|
|
||
| def verify_recording(recording_file, expected_output) | ||
| begin | ||
| # Load and create replayer | ||
| require_relative '../lib/liquid/template_recorder/replayer' | ||
| replayer = Liquid::TemplateRecorder.replay_from(recording_file, mode: :compute) | ||
|
|
||
| # Get replayed output | ||
| replayed_output = replayer.render | ||
|
|
||
| if expected_output == replayed_output | ||
| puts "✅ Verification PASSED - outputs match perfectly!" | ||
| puts "Output length: #{expected_output.length} characters" | ||
| else | ||
| puts "❌ Verification FAILED - outputs differ" | ||
| puts "Expected length: #{expected_output.length}" | ||
| puts "Actual length: #{replayed_output.length}" | ||
|
|
||
| # Write outputs to temp files for diff | ||
| require 'tempfile' | ||
| expected_file = Tempfile.new(['expected', '.txt']) | ||
| actual_file = Tempfile.new(['actual', '.txt']) | ||
|
|
||
| expected_file.write(expected_output) | ||
| expected_file.flush | ||
|
|
||
| actual_file.write(replayed_output) | ||
| actual_file.flush | ||
|
|
||
| puts | ||
| puts "Showing diff (expected vs actual):" | ||
| puts "=" * 50 | ||
| system("diff -u #{expected_file.path} #{actual_file.path}") | ||
|
|
||
| expected_file.close | ||
| actual_file.close | ||
|
|
||
| exit 1 | ||
| end | ||
| rescue => e | ||
| puts "❌ Verification ERROR: #{e.message}" | ||
| puts e.backtrace.first(3) | ||
| exit 1 | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require_relative '../lib/liquid' | ||
|
|
||
| def usage | ||
| puts <<~USAGE | ||
| Usage: liquid-replay <recording_file> [mode] | ||
|
|
||
| Replays a liquid template recording in hermetic mode. | ||
|
|
||
| Arguments: | ||
| recording_file - Path to the JSON recording file | ||
| mode - Replay mode: compute (default), strict, or verify | ||
|
|
||
| Modes: | ||
| compute - Normal replay with computed results | ||
| strict - Strict replay, errors on unexpected calls | ||
| verify - Verification mode, compares against recorded results | ||
|
|
||
| Examples: | ||
| liquid-replay recording.json | ||
| liquid-replay recording.json strict | ||
| liquid-replay recording.json verify | ||
| USAGE | ||
| end | ||
|
|
||
| def main | ||
| if ARGV.length < 1 || ARGV.length > 2 | ||
| usage | ||
| exit 1 | ||
| end | ||
|
|
||
| recording_file = ARGV[0] | ||
| mode = (ARGV[1] || 'compute').to_sym | ||
|
|
||
| unless File.exist?(recording_file) | ||
| puts "Error: Recording file '#{recording_file}' not found" | ||
| exit 1 | ||
| end | ||
|
|
||
| unless [:compute, :strict, :verify].include?(mode) | ||
| puts "Error: Invalid mode '#{mode}'. Must be compute, strict, or verify" | ||
| exit 1 | ||
| end | ||
|
|
||
| puts "Replaying recording: #{recording_file}" | ||
| puts "Mode: #{mode}" | ||
|
|
||
| begin | ||
| replayer = Liquid::TemplateRecorder.replay_from(recording_file, mode: mode) | ||
| result = replayer.render | ||
|
|
||
| puts "\nReplay completed successfully!" | ||
| puts "Output length: #{result.length} characters" | ||
| puts "\nRendered output:" | ||
| puts "-" * 40 | ||
| puts result | ||
| puts "-" * 40 | ||
| STDOUT.flush | ||
| rescue => e | ||
| puts "Error during replay: #{e.message}" | ||
| puts e.backtrace.first(5) | ||
| exit 1 | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -144,6 +144,11 @@ def collection_segment(context) | |||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def render_segment(context, output, segment) | ||||||||||||||||||
| # Recording hook - loop enter | ||||||||||||||||||
| if (recorder = context.registers[:recorder]) | ||||||||||||||||||
| recorder.for_enter(@collection_name.name, @variable_name) | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| for_stack = context.registers[:for_stack] ||= [] | ||||||||||||||||||
| length = segment.length | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -155,7 +160,12 @@ def render_segment(context, output, segment) | |||||||||||||||||
| begin | ||||||||||||||||||
| context['forloop'] = loop_vars | ||||||||||||||||||
|
|
||||||||||||||||||
| segment.each do |item| | ||||||||||||||||||
| segment.each_with_index do |item, index| | ||||||||||||||||||
| # Recording hook - item binding | ||||||||||||||||||
| if (recorder = context.registers[:recorder]) | ||||||||||||||||||
| recorder.for_item(index, item) | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| context[@variable_name] = item | ||||||||||||||||||
| @for_block.render_to_output_buffer(context, output) | ||||||||||||||||||
| loop_vars.send(:increment!) | ||||||||||||||||||
|
|
@@ -168,6 +178,11 @@ def render_segment(context, output, segment) | |||||||||||||||||
| end | ||||||||||||||||||
| ensure | ||||||||||||||||||
| for_stack.pop | ||||||||||||||||||
|
|
||||||||||||||||||
| # Recording hook - loop exit | ||||||||||||||||||
| if (recorder = context.registers[:recorder]) | ||||||||||||||||||
| recorder.for_exit | ||||||||||||||||||
| end | ||||||||||||||||||
|
Comment on lines
+181
to
+185
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| end | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.