kward 0.85.0 → 0.86.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +130 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/Gemfile.lock +6 -2
  5. data/README.md +3 -3
  6. data/Rakefile +3 -0
  7. data/doc/agent-tools.md +11 -0
  8. data/doc/api.md +4 -1
  9. data/doc/composer.md +4 -3
  10. data/doc/configuration.md +60 -11
  11. data/doc/extensibility.md +2 -0
  12. data/doc/getting-started.md +3 -3
  13. data/doc/git.md +1 -1
  14. data/doc/memory.md +1 -1
  15. data/doc/plugins.md +40 -5
  16. data/doc/providers.md +10 -4
  17. data/doc/rpc.md +125 -7
  18. data/doc/session-management.md +36 -1
  19. data/doc/shell.md +643 -27
  20. data/doc/tabs.md +2 -1
  21. data/doc/troubleshooting.md +1 -1
  22. data/doc/usage.md +21 -11
  23. data/examples/plugins/openrouter_proxy/README.md +103 -0
  24. data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
  25. data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
  26. data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
  27. data/examples/plugins/openrouter_proxy/server.rb +186 -0
  28. data/examples/plugins/space_invaders.rb +0 -1
  29. data/exe/kward +2 -2
  30. data/exe/kwsh +7 -0
  31. data/kward.gemspec +3 -1
  32. data/lib/kward/agent.rb +43 -9
  33. data/lib/kward/cli/commands.rb +41 -11
  34. data/lib/kward/cli/interactive_turn.rb +10 -0
  35. data/lib/kward/cli/plugins.rb +5 -1
  36. data/lib/kward/cli/prompt_interface.rb +4 -1
  37. data/lib/kward/cli/rendering.rb +21 -28
  38. data/lib/kward/cli/runtime_helpers.rb +52 -34
  39. data/lib/kward/cli/settings/model.rb +1 -12
  40. data/lib/kward/cli/tabs.rb +91 -18
  41. data/lib/kward/cli/tool_summaries.rb +2 -0
  42. data/lib/kward/cli/transcript_formatter.rb +3 -10
  43. data/lib/kward/cli/worktrees.rb +1 -0
  44. data/lib/kward/cli.rb +56 -7
  45. data/lib/kward/config/core.rb +16 -5
  46. data/lib/kward/config/extensions.rb +3 -0
  47. data/lib/kward/config/paths.rb +11 -0
  48. data/lib/kward/config/settings.rb +7 -0
  49. data/lib/kward/conversation.rb +11 -1
  50. data/lib/kward/entrypoint.rb +14 -0
  51. data/lib/kward/events.rb +2 -0
  52. data/lib/kward/generated_images.rb +237 -0
  53. data/lib/kward/model/catalog.rb +6 -3
  54. data/lib/kward/model/client.rb +105 -36
  55. data/lib/kward/model/codex_images.rb +83 -0
  56. data/lib/kward/model/copilot_models.rb +8 -4
  57. data/lib/kward/model/model_info.rb +27 -9
  58. data/lib/kward/model/openrouter_model_cache.rb +4 -2
  59. data/lib/kward/model/payloads.rb +6 -5
  60. data/lib/kward/model/sources.rb +3 -1
  61. data/lib/kward/model/stream_parser.rb +4 -7
  62. data/lib/kward/pan/index.html.erb +8 -1
  63. data/lib/kward/pan/server.rb +13 -1
  64. data/lib/kward/plugins/codex_responses.rb +73 -0
  65. data/lib/kward/plugins/host.rb +16 -0
  66. data/lib/kward/plugins/http_server.rb +83 -0
  67. data/lib/kward/plugins/registry.rb +39 -0
  68. data/lib/kward/prompt_interface/composer_controller.rb +0 -1
  69. data/lib/kward/prompt_interface/composer_renderer.rb +159 -72
  70. data/lib/kward/prompt_interface/editor/controller.rb +1 -1
  71. data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
  72. data/lib/kward/prompt_interface/file_overlay.rb +3 -3
  73. data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
  74. data/lib/kward/prompt_interface/git_prompt.rb +18 -11
  75. data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
  76. data/lib/kward/prompt_interface/interactive/state.rb +18 -20
  77. data/lib/kward/prompt_interface/key_handler.rb +108 -25
  78. data/lib/kward/prompt_interface/layout.rb +11 -1
  79. data/lib/kward/prompt_interface/overlay_renderer.rb +25 -8
  80. data/lib/kward/prompt_interface/project_browser.rb +27 -54
  81. data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
  82. data/lib/kward/prompt_interface/question_prompt.rb +19 -28
  83. data/lib/kward/prompt_interface/runtime_state.rb +31 -9
  84. data/lib/kward/prompt_interface/screen.rb +24 -21
  85. data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
  86. data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
  87. data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
  88. data/lib/kward/prompt_interface.rb +109 -39
  89. data/lib/kward/prompts.rb +7 -1
  90. data/lib/kward/pty/adaptive_output_sink.rb +38 -4
  91. data/lib/kward/pty/interactive_runner.rb +20 -28
  92. data/lib/kward/pty/transcript_normalizer.rb +65 -21
  93. data/lib/kward/rpc/config_manager.rb +3 -0
  94. data/lib/kward/rpc/server.rb +27 -5
  95. data/lib/kward/rpc/session_manager.rb +62 -17
  96. data/lib/kward/rpc/transcript_normalizer.rb +11 -7
  97. data/lib/kward/sessions/store.rb +30 -3
  98. data/lib/kward/shell/kwsh.rb +27 -7
  99. data/lib/kward/shell/kwshrc.rb +11 -2
  100. data/lib/kward/shell/native_arithmetic.rb +255 -0
  101. data/lib/kward/shell/native_arrays.rb +102 -0
  102. data/lib/kward/shell/native_braces.rb +96 -0
  103. data/lib/kward/shell/native_builtins.rb +476 -0
  104. data/lib/kward/shell/native_completion.rb +169 -0
  105. data/lib/kward/shell/native_condition.rb +75 -0
  106. data/lib/kward/shell/native_directory.rb +116 -0
  107. data/lib/kward/shell/native_exec_state.rb +73 -0
  108. data/lib/kward/shell/native_expansion.rb +252 -0
  109. data/lib/kward/shell/native_fields.rb +62 -0
  110. data/lib/kward/shell/native_glob.rb +96 -0
  111. data/lib/kward/shell/native_history.rb +242 -0
  112. data/lib/kward/shell/native_interactive.rb +164 -0
  113. data/lib/kward/shell/native_invocation.rb +42 -0
  114. data/lib/kward/shell/native_jobs.rb +250 -0
  115. data/lib/kward/shell/native_line_editor.rb +382 -0
  116. data/lib/kward/shell/native_line_terminal.rb +286 -0
  117. data/lib/kward/shell/native_locale.rb +109 -0
  118. data/lib/kward/shell/native_locals.rb +57 -0
  119. data/lib/kward/shell/native_pattern.rb +200 -0
  120. data/lib/kward/shell/native_process.rb +279 -0
  121. data/lib/kward/shell/native_program.rb +40 -0
  122. data/lib/kward/shell/native_programmable_completion.rb +115 -0
  123. data/lib/kward/shell/native_prompt_format.rb +89 -0
  124. data/lib/kward/shell/native_runtime.rb +964 -0
  125. data/lib/kward/shell/native_substitution.rb +110 -0
  126. data/lib/kward/shell/native_syntax.rb +946 -0
  127. data/lib/kward/shell/native_traps.rb +196 -0
  128. data/lib/kward/shell/native_vi.rb +407 -0
  129. data/lib/kward/shell/native_vi_terminal.rb +19 -0
  130. data/lib/kward/shell/native_worker.rb +82 -0
  131. data/lib/kward/shell/persistent_session.rb +184 -190
  132. data/lib/kward/shell/prompt.rb +14 -0
  133. data/lib/kward/shell/prompt_agent.rb +17 -0
  134. data/lib/kward/shell/script_runner.rb +244 -0
  135. data/lib/kward/shell/standalone_agent.rb +31 -0
  136. data/lib/kward/shell/standalone_link.rb +78 -0
  137. data/lib/kward/shell/standalone_terminal.rb +287 -0
  138. data/lib/kward/terminal/keys.rb +22 -1
  139. data/lib/kward/terminal/locale_text.rb +29 -0
  140. data/lib/kward/terminal/sequences.rb +45 -4
  141. data/lib/kward/tools/datetime.rb +85 -0
  142. data/lib/kward/tools/generate_image.rb +36 -0
  143. data/lib/kward/tools/registry.rb +35 -9
  144. data/lib/kward/tools/tool_call.rb +3 -1
  145. data/lib/kward/transcripts/markdown_transcript.rb +8 -0
  146. data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
  147. data/lib/kward/transport/gateway.rb +2 -1
  148. data/lib/kward/turn_summary.rb +68 -0
  149. data/lib/kward/version.rb +1 -1
  150. data/lib/main.rb +6 -3
  151. metadata +84 -1
@@ -0,0 +1,109 @@
1
+ require "fiddle"
2
+
3
+ module Kward
4
+ module NativeShell
5
+ # POSIX locale objects are thread-local, never process-global setlocale.
6
+ # Wildcard iteration stays in Pattern; libc only evaluates one bracket or
7
+ # compares two strings, avoiding recursive libc glob backtracking.
8
+ class Locale
9
+ DARWIN = RUBY_PLATFORM.include?("darwin")
10
+ CTYPE_MASK = DARWIN ? 2 : 1
11
+ COLLATE_MASK = DARWIN ? 1 : 8
12
+ CODESET = DARWIN ? 0 : 14
13
+ POINTER = Fiddle::TYPE_VOIDP
14
+ NEW = Fiddle::Function.new(Fiddle::Handle::DEFAULT["newlocale"], [Fiddle::TYPE_INT, POINTER, POINTER], POINTER)
15
+ FREE = Fiddle::Function.new(Fiddle::Handle::DEFAULT["freelocale"], [POINTER], Fiddle::TYPE_VOID)
16
+ USE = Fiddle::Function.new(Fiddle::Handle::DEFAULT["uselocale"], [POINTER], POINTER)
17
+ INFO = Fiddle::Function.new(Fiddle::Handle::DEFAULT["nl_langinfo_l"], [Fiddle::TYPE_INT, POINTER], POINTER)
18
+ MATCH = Fiddle::Function.new(Fiddle::Handle::DEFAULT["fnmatch"], [POINTER, POINTER, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
19
+ COMPARE = Fiddle::Function.new(Fiddle::Handle::DEFAULT["strcoll"], [POINTER, POINTER], Fiddle::TYPE_INT)
20
+
21
+ UPPER = Fiddle::Function.new(Fiddle::Handle::DEFAULT["towupper"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
22
+ LOWER = Fiddle::Function.new(Fiddle::Handle::DEFAULT["towlower"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
23
+
24
+ attr_reader :encoding, :multichar
25
+
26
+ def self.with(variables)
27
+ raise ExecutionError, "native locale support requires macOS or Linux" unless DARWIN || RUBY_PLATFORM.include?("linux")
28
+ handle = nil
29
+ collation = "C"
30
+ { "LC_CTYPE" => CTYPE_MASK, "LC_COLLATE" => COLLATE_MASK }.each do |category, mask|
31
+ name = [variables["LC_ALL"], variables[category], variables["LANG"], "C"].find { |value| value && !value.empty? }
32
+ raise ExecutionError, "NUL bytes are not allowed in locale names" if name.include?("\0")
33
+ collation = name if category == "LC_COLLATE"
34
+ updated = NEW.call(mask, name, handle || 0)
35
+ raise ExecutionError, "unavailable locale: #{name}" if updated.null?
36
+ # Darwin clones base; Linux newlocale consumes it on success.
37
+ # https://github.com/apple-oss-distributions/Libc/blob/main/locale/xlocale.c
38
+ FREE.call(handle) if DARWIN && handle
39
+ handle = updated
40
+ end
41
+ previous = USE.call(handle)
42
+ yield new(INFO.call(CODESET, handle).to_s, multichar: !collation.match?(/\A(?:C|POSIX)(?:[.@].*)?\z/))
43
+ ensure
44
+ USE.call(previous) if previous
45
+ FREE.call(handle) if handle
46
+ end
47
+
48
+ def initialize(codeset, multichar: true)
49
+ @multichar = multichar
50
+ @encoding = Encoding.find(codeset)
51
+ @encoding = Encoding::BINARY if @encoding == Encoding::US_ASCII
52
+ rescue ArgumentError
53
+ raise ExecutionError, "unsupported locale encoding: #{codeset}"
54
+ end
55
+
56
+ def text(value)
57
+ value.dup.force_encoding(@encoding)
58
+ end
59
+
60
+ def swapcase(value)
61
+ text(value).each_char.map do |character|
62
+ codepoint = character.encode(Encoding::UTF_8).ord
63
+ upper = UPPER.call(codepoint)
64
+ (upper == codepoint ? LOWER.call(codepoint) : upper).chr(Encoding::UTF_8).encode(@encoding)
65
+ rescue EncodingError, RangeError
66
+ character
67
+ end.join
68
+ end
69
+
70
+ def bracket?(source, value)
71
+ validate(source, value)
72
+ MATCH.call(source, value, 0).zero?
73
+ end
74
+
75
+ def collating_element?(value)
76
+ value.length == 1 || multichar && symbol?(value, value)
77
+ end
78
+
79
+ def symbol?(name, value)
80
+ return name == value if name.length == 1
81
+ # Never let candidate input close the bracket and introduce libc glob
82
+ # operators. Whole-pattern wildcard iteration belongs to Pattern.
83
+ return false if name.empty? || unsafe_symbol?(name)
84
+ bracket?("[[.#{name}.]]", value)
85
+ end
86
+
87
+ def equivalent?(name, value)
88
+ return false if name.length > 1 && unsafe_symbol?(name)
89
+ bracket?("[[=#{name}=]]", value) ||
90
+ symbol?(name, name) && collating_element?(value) && compare(name, value).zero?
91
+ end
92
+
93
+ def compare(left, right)
94
+ validate(left, right)
95
+ COMPARE.call(left, right)
96
+ end
97
+
98
+ private
99
+
100
+ def unsafe_symbol?(name)
101
+ ["[", "]", "\\", "*", "?"].any? { |character| name.include?(character) }
102
+ end
103
+
104
+ def validate(*values)
105
+ raise ExecutionError, "NUL bytes are not supported in locale comparisons" if values.any? { |value| value.include?("\0") }
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,57 @@
1
+ module Kward
2
+ module NativeShell
3
+ # A function sees its caller's active bindings. Save only the names it
4
+ # declares local so other assignments still affect the enclosing shell.
5
+ module Locals
6
+ private
7
+
8
+ def local_builtin(args)
9
+ args.shift if args.first == "--"
10
+ raise ExecutionError, "local: expected a name or assignment" if args.empty?
11
+
12
+ args.each do |argument|
13
+ name, value = argument.split("=", 2)
14
+ declare_local(name.to_s)
15
+ assign(name, value) if value
16
+ end
17
+ 0
18
+ end
19
+
20
+ def local_array(name, words)
21
+ array = Arrays::ArrayValue.new(associative: false, values: {})
22
+ populate_array(array, name, words)
23
+ declare_local(name)
24
+ @variables.delete(name)
25
+ @arrays[name] = array
26
+ 0
27
+ rescue ExpansionError, ExecutionError => e
28
+ command_error(e, @streams)
29
+ end
30
+
31
+ def declare_local(name)
32
+ scope = @local_scopes.last
33
+ raise ExecutionError, "local: only valid in a function" unless scope
34
+ validate_name(name)
35
+ raise ExecutionError, "#{name}: readonly variable" if @readonly.include?(name)
36
+ return if scope.key?(name)
37
+
38
+ scope[name] = [@variables.delete(name), @arrays.delete(name), @exported.include?(name),
39
+ (@line_tracking if name == "LINENO"), (@getopts_index if name == "OPTIND")]
40
+ @command_hash.clear if name == "PATH"
41
+ @getopts_index = nil if name == "OPTIND"
42
+ end
43
+
44
+ def restore_locals(scope)
45
+ scope.each do |name, (value, array, exported, line_tracking, getopts_index)|
46
+ value ? @variables[name] = value : @variables.delete(name)
47
+ array ? @arrays[name] = array : @arrays.delete(name)
48
+ exported ? @exported.add(name) : @exported.delete(name)
49
+ @readonly.delete(name)
50
+ @command_hash.clear if name == "PATH"
51
+ @line_tracking = line_tracking if name == "LINENO"
52
+ @getopts_index = getopts_index if name == "OPTIND"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,200 @@
1
+ module Kward
2
+ module NativeShell
3
+ # Shell patterns for case and parameter trimming, not filesystem globbing.
4
+ # Wildcards match slashes and leading dots here. Only a single-character
5
+ # bracket expression uses Regexp; arbitrary shell text is always escaped.
6
+ class Pattern
7
+ CLASSES = %w[alnum alpha blank cntrl digit graph lower print punct space upper xdigit].freeze
8
+
9
+ class LimitError < ArgumentError; end
10
+ MAX_MATCH_STEPS = 1_000_000
11
+ MAX_MATCH_BYTES = 16 * 1024 * 1024
12
+
13
+ # Test alternatives separately: libc fnmatch can commit to a shorter
14
+ # element in a mixed bracket even when a longer element would match.
15
+ class Bracket
16
+ def initialize(locale, elements, negated)
17
+ @locale, @negated = locale, negated
18
+ @elements = []
19
+ until elements.empty?
20
+ first = elements.shift
21
+ if elements.length >= 2 && elements.first.first == :dash
22
+ elements.shift
23
+ @elements << [:range, first, elements.shift]
24
+ else
25
+ @elements << first
26
+ end
27
+ end
28
+ end
29
+
30
+ def match?(value)
31
+ return false unless @locale.collating_element?(value)
32
+ matched = @elements.any? do |kind, text, last|
33
+ case kind
34
+ when :range
35
+ lower, upper = endpoint(text), endpoint(last)
36
+ lower && upper && @locale.compare(lower, value) <= 0 && @locale.compare(value, upper) <= 0
37
+ when :symbol then @locale.symbol?(text, value)
38
+ when :equivalent then @locale.equivalent?(text, value)
39
+ when :class then value.length == 1 && @locale.bracket?("[[:#{text}:]]", value)
40
+ else value == text
41
+ end
42
+ end
43
+ @negated ? !matched : matched
44
+ end
45
+
46
+ private
47
+
48
+ def endpoint(element)
49
+ kind, text = element
50
+ text if %i[literal dash].include?(kind) || kind == :symbol && @locale.symbol?(text, text)
51
+ end
52
+ end
53
+
54
+ def initialize(source, locale: nil)
55
+ @locale = locale
56
+ @source = locale ? locale.text(source) : source
57
+ @index = 0
58
+ @tokens = []
59
+ while (character = @source[@index])
60
+ @index += 1
61
+ @tokens << case character
62
+ when "*" then :star
63
+ when "?" then :any
64
+ when "\\"
65
+ escaped = @source[@index]
66
+ @index += 1 if escaped
67
+ escaped || "\\"
68
+ when "[" then bracket || "["
69
+ else character
70
+ end
71
+ end
72
+ end
73
+
74
+ def wildcards?
75
+ @tokens.any? { |token| token == :star || token == :any || token.is_a?(Regexp) || token.is_a?(Bracket) }
76
+ end
77
+
78
+ def match?(value)
79
+ characters = (@locale ? @locale.text(value) : value).chars
80
+ return match_collating(characters) if @locale&.multichar && @tokens.any? { |token| token.is_a?(Bracket) }
81
+ token_index = 0
82
+ value_index = 0
83
+ star_index = nil
84
+ retry_index = 0
85
+ while value_index < characters.length
86
+ token = @tokens[token_index]
87
+ if token == :star
88
+ star_index = token_index
89
+ token_index += 1
90
+ retry_index = value_index
91
+ elsif token == :any || (token.is_a?(Regexp) || token.is_a?(Bracket)) && token.match?(characters[value_index]) || token == characters[value_index]
92
+ token_index += 1
93
+ value_index += 1
94
+ elsif star_index
95
+ token_index = star_index + 1
96
+ retry_index += 1
97
+ value_index = retry_index
98
+ else
99
+ return false
100
+ end
101
+ end
102
+ token_index += 1 while @tokens[token_index] == :star
103
+ token_index == @tokens.length
104
+ end
105
+
106
+ private
107
+
108
+ # Iterative frontier matching keeps both short and long collating-element
109
+ # paths alive without recursive backtracking. Work and copied bytes are
110
+ # bounded explicitly, including unsuccessful prefix probes.
111
+ def match_collating(characters)
112
+ positions = [0]
113
+ steps = bytes = 0
114
+ @tokens.each_with_index do |token, index|
115
+ if token == :star
116
+ return true if index == @tokens.length - 1
117
+ first = positions.min
118
+ steps += characters.length - first + 1
119
+ raise LimitError, "collating pattern match exceeds work limit" if steps > MAX_MATCH_STEPS
120
+ positions = (first..characters.length).to_a
121
+ else
122
+ following = {}
123
+ positions.each do |start|
124
+ next if start == characters.length
125
+ if token.is_a?(Bracket)
126
+ candidate = +""
127
+ start.upto(characters.length - 1) do |finish|
128
+ candidate << characters[finish]
129
+ steps += 1
130
+ bytes += candidate.bytesize
131
+ raise LimitError, "collating pattern match exceeds work limit" if steps > MAX_MATCH_STEPS || bytes > MAX_MATCH_BYTES
132
+ following[finish + 1] = true if token.match?(candidate)
133
+ end
134
+ else
135
+ steps += 1
136
+ following[start + 1] = true if token == :any || token == characters[start]
137
+ end
138
+ end
139
+ positions = following.keys
140
+ end
141
+ raise LimitError, "collating pattern match exceeds work limit" if steps > MAX_MATCH_STEPS
142
+ return false if positions.empty?
143
+ end
144
+ positions.include?(characters.length)
145
+ end
146
+
147
+ def bracket_literal(character)
148
+ character == "&" ? "\\&" : Regexp.escape(character)
149
+ end
150
+
151
+ def bracket
152
+ start = @index
153
+ content = +""
154
+ elements = []
155
+ negated = ["!", "^"].include?(@source[@index])
156
+ if negated
157
+ content << "^"
158
+ @index += 1
159
+ end
160
+ if @source[@index] == "]"
161
+ content << "\\]"
162
+ elements << [:literal, "]"]
163
+ @index += 1
164
+ end
165
+ entries = 0
166
+ while (character = @source[@index])
167
+ if character == "]" && (entries.positive? || content.end_with?("\\]"))
168
+ @index += 1
169
+ return Bracket.new(@locale, elements, negated) if @locale
170
+ return Regexp.new("\\A[#{content}]\\z")
171
+ elsif character == "\\" && @source[@index + 1]
172
+ content << bracket_literal(@source[@index + 1])
173
+ elements << [:literal, @source[@index + 1]]
174
+ @index += 2
175
+ elsif (match = @source[@index..].match(/\A\[([.=])(.*?)\1\]/m))
176
+ # Collating symbols and equivalence classes are interpreted by libc.
177
+ return /\A\b\B\z/ if !@locale && match[2].length != 1
178
+ content << bracket_literal(match[2])
179
+ elements << [match[1] == "." ? :symbol : :equivalent, match[2]]
180
+ @index += match[0].length
181
+ elsif (match = @source[@index..].match(/\A\[:([a-z]+):\]/)) && CLASSES.include?(match[1])
182
+ content << match[0]
183
+ elements << [:class, match[1]]
184
+ @index += match[0].length
185
+ else
186
+ content << (character == "-" ? "-" : bracket_literal(character))
187
+ elements << [character == "-" ? :dash : :literal, character]
188
+ @index += 1
189
+ end
190
+ entries += 1
191
+ end
192
+ @index = start
193
+ nil
194
+ rescue RegexpError
195
+ # An invalid range cannot match a character.
196
+ /\A\b\B\z/
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,279 @@
1
+ require "fiddle"
2
+ require "fcntl"
3
+ require "io/nonblock"
4
+ require "rbconfig"
5
+ require_relative "native_exec_state"
6
+
7
+ module Kward
8
+ module NativeShell
9
+ # Ruby's Process.spawn/exec retry ENOEXEC through /bin/sh, even with argv.
10
+ # Call execve directly, interpreting executable text only with native kwsh.
11
+ module NativeProcess
12
+ EXECVE = Fiddle::Function.new(
13
+ Fiddle::Handle::DEFAULT["execve"],
14
+ [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP],
15
+ Fiddle::TYPE_INT
16
+ )
17
+
18
+ DUP2 = Fiddle::Function.new(Fiddle::Handle::DEFAULT["dup2"], [Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
19
+ FCNTL = Fiddle::Function.new(Fiddle::Handle::DEFAULT["fcntl"], [Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
20
+ CLOSE = Fiddle::Function.new(Fiddle::Handle::DEFAULT["close"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
21
+
22
+ SIGACTION = Fiddle::Function.new(Fiddle::Handle::DEFAULT["sigaction"],
23
+ [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
24
+
25
+ # Ruby does not expose WCONTINUED on every supported platform.
26
+ # sys/wait.h on Darwin; Linux bits/waitflags.h uses 0x08.
27
+ WAIT_CONTINUED = if Process.const_defined?(:WCONTINUED)
28
+ Process.const_get(:WCONTINUED)
29
+ elsif RUBY_PLATFORM.include?("darwin")
30
+ 0x10
31
+ elsif RUBY_PLATFORM.include?("linux")
32
+ 0x08
33
+ else
34
+ 0
35
+ end
36
+
37
+ # Only the standalone executable calls this discovery boundary. Hosted
38
+ # runtimes receive an explicit descriptor map, never the agent's open IOs.
39
+ def self.inherited_descriptors
40
+ (3..9).each_with_object({}) do |descriptor, streams|
41
+ flags = FCNTL.call(descriptor, Fcntl::F_GETFD, 0)
42
+ next if flags < 0 || flags & Fcntl::FD_CLOEXEC != 0
43
+ io = IO.for_fd(descriptor)
44
+ io.close_on_exec = true
45
+ io.sync = true
46
+ streams[descriptor] = io
47
+ end
48
+ end
49
+
50
+ def self.standalone(arguments, name:, program: "kwsh", edit_lines: false, load_rc: false, shell_agent: false)
51
+ inherited = inherited_descriptors
52
+ relay = shell_agent && standalone_terminal?(arguments)
53
+ # Ruby owns private wake-up descriptors before this script starts.
54
+ # Re-exec the trusted VM with 3–9 occupied so its new private handles
55
+ # cannot collide with shell redirections. PID and user IOs are retained.
56
+ bootstrap = <<~RUBY
57
+ $LOAD_PATH.replace(#{$LOAD_PATH.map { |path| File.expand_path(path) }.inspect})
58
+ require #{File.expand_path("script_runner", __dir__).inspect}
59
+ descriptors = ARGV.shift.split(",").to_h do |value|
60
+ fd = Integer(value)
61
+ source = IO.for_fd(fd, autoclose: false)
62
+ io = IO.for_fd(source.fcntl(Fcntl::F_DUPFD, 32))
63
+ io.close_on_exec = true
64
+ io.sync = true
65
+ [fd, io]
66
+ end
67
+ File.open(File::NULL, "r+") do |guard|
68
+ (3..9).each do |fd|
69
+ result = Kward::NativeShell::NativeProcess::DUP2.call(guard.fileno, fd)
70
+ raise SystemCallError.new("dup2", Fiddle.last_error) if result < 0
71
+ IO.for_fd(fd, autoclose: false).close_on_exec = true
72
+ end
73
+ end
74
+ $0 = ARGV.shift
75
+ link = if #{relay.inspect}
76
+ require #{File.expand_path("standalone_link", __dir__).inspect}
77
+ Kward::NativeShell::StandaloneLink.new(30)
78
+ end
79
+ exit Kward::NativeShell::ScriptRunner.run(ARGV, descriptors: descriptors, own_descriptors: true, process_owned: true,
80
+ program: #{program.inspect}, edit_lines: #{edit_lines.inspect}, load_rc: #{load_rc.inspect}, shell_link: link)
81
+ RUBY
82
+ mappings = (3..9).to_h { |fd| [fd, inherited.fetch(fd) { [File::NULL, File::RDWR] }] }
83
+ # Only the trusted Ruby executable uses Ruby's exec API; user programs
84
+ # still go through execve and the native-only ENOEXEC path below.
85
+ ruby_arguments = [RbConfig.ruby, "--disable=gems,rubyopt", "-e", bootstrap, "--", inherited.keys.join(","), name, *arguments]
86
+ if relay
87
+ require_relative "standalone_terminal"
88
+ exit StandaloneTerminal.new.run(ruby_arguments, mappings)
89
+ end
90
+ Kernel.exec(*ruby_arguments, **mappings)
91
+ end
92
+
93
+ def self.standalone_terminal?(arguments)
94
+ return false unless $stdin.tty? && $stdout.tty? && $stderr.tty?
95
+ return false unless Process.uid == Process.euid && Process.gid == Process.egid
96
+
97
+ require_relative "native_invocation"
98
+ Invocation.parse(arguments).prompt?($stdin, $stderr)
99
+ rescue ArgumentError
100
+ false # Let ScriptRunner report invocation errors as usual.
101
+ end
102
+
103
+ def self.ignored_signals
104
+ # Darwin and Linux place the handler pointer first. This buffer exceeds
105
+ # both supported sigaction layouts; querying never changes a disposition.
106
+ action = Fiddle::Pointer.malloc(256, Fiddle::RUBY_FREE)
107
+ Signal.list.filter_map do |name, number|
108
+ next if number.zero? || %w[KILL STOP].include?(name)
109
+ raise SystemCallError.new("sigaction", Fiddle.last_error) if SIGACTION.call(number, nil, action) < 0
110
+ name if action[0, Fiddle::SIZEOF_VOIDP].unpack1("J") == 1 # SIG_IGN
111
+ end
112
+ end
113
+
114
+ def self.wait(pid)
115
+ flags = Process::WUNTRACED | WAIT_CONTINUED
116
+ _, status = Process.wait2(pid, flags)
117
+ # A group can resume while its supervisor still holds a stop report.
118
+ # Prefer newer kernel state before propagating that stop back to the group.
119
+ if status.stopped?
120
+ while (update = Process.waitpid2(pid, flags | Process::WNOHANG))
121
+ status = update.last
122
+ break if status.exited? || status.signaled?
123
+ end
124
+ end
125
+ status
126
+ end
127
+
128
+ def self.spawn(executable, arguments, environment, cwd:, streams:, mask: nil, hosted: false, signals: {})
129
+ validate(arguments, environment)
130
+ streams.values.compact.uniq.each(&:flush)
131
+ Process.fork { exit!(replace(executable, arguments, environment, cwd: cwd, streams: streams, mask: mask, hosted: hosted, signals: signals)) }
132
+ end
133
+
134
+ def self.validate(arguments, environment)
135
+ if arguments.any? { |argument| argument.include?("\0") } || environment.any? { |key, value| key.include?("\0") || value.include?("\0") }
136
+ raise ArgumentError, "NUL bytes are not allowed in process arguments or environment"
137
+ end
138
+ end
139
+
140
+ def self.replace(executable, arguments, environment, cwd:, streams:, mask: nil, hosted: false, signals: {}, recover: false)
141
+ validate(arguments, environment)
142
+ load_path = $LOAD_PATH.map { |path| File.expand_path(path) }
143
+ streams.values.compact.uniq.each(&:flush)
144
+ # Keep directory ownership intact even if exec fails after remapping.
145
+ # This runs only in an exec-bound process, never in the agent host.
146
+ if cwd.respond_to?(:enter)
147
+ cwd.protect_descriptors(streams.keys)
148
+ end
149
+ if recover
150
+ streams.each_key do |fd|
151
+ next if fd < 3 || FCNTL.call(fd, Fcntl::F_GETFD, 0) < 0
152
+ # Embedded callers must not overwrite Ruby's live internal handles.
153
+ IO.for_fd(fd, autoclose: false)
154
+ end
155
+ state = ExecState.new(([0, 1, 2] + streams.keys + streams.values.compact.map(&:fileno)).uniq)
156
+ end
157
+ if cwd.respond_to?(:enter)
158
+ cwd.enter
159
+ else
160
+ Dir.chdir(cwd)
161
+ end
162
+ # Snapshot all sources before replacing any standard descriptor:
163
+ # `2>&1 >file` must retain the original stdout for stderr.
164
+ copies = {}
165
+ streams.each do |descriptor, io|
166
+ unless io
167
+ copies[descriptor] = nil
168
+ next
169
+ end
170
+ copy = IO.for_fd(io.fcntl(Fcntl::F_DUPFD, 32))
171
+ copy.close_on_exec = true
172
+ copies[descriptor] = copy
173
+ end
174
+ [STDIN, STDOUT, STDERR].each_with_index do |standard, descriptor|
175
+ source = copies[descriptor]
176
+ if source
177
+ standard.reopen(source)
178
+ standard.close_on_exec = false
179
+ standard.nonblock = false
180
+ standard.sync = true
181
+ elsif recover
182
+ standard.reopen(File::NULL, descriptor.zero? ? "r" : "w")
183
+ else
184
+ standard.close
185
+ end
186
+ end
187
+ copies.each do |descriptor, source|
188
+ next if descriptor < 3
189
+ if source
190
+ raise SystemCallError.new("dup2", Fiddle.last_error) if DUP2.call(source.fileno, descriptor) < 0
191
+ FCNTL.call(descriptor, Fcntl::F_SETFD, 0)
192
+ flags = FCNTL.call(descriptor, Fcntl::F_GETFL, 0)
193
+ FCNTL.call(descriptor, Fcntl::F_SETFL, flags & ~Fcntl::O_NONBLOCK)
194
+ else
195
+ CLOSE.call(descriptor)
196
+ end
197
+ end
198
+ copies.values.compact.each(&:close)
199
+ File.umask(mask) if mask
200
+ signals.each do |name, handler|
201
+ previous = Signal.trap(name, handler)
202
+ state.signals[name] = previous if state
203
+ end
204
+ begin
205
+ execve(executable, arguments, environment)
206
+ rescue Errno::ENOEXEC
207
+ prefix = File.binread(executable, 4096) || "".b
208
+ raise if prefix.include?("\0") || prefix.start_with?("\x7fELF".b)
209
+
210
+ # A fresh VM resets caught signals, closes private CLOEXEC descriptors,
211
+ # and preserves the PID for exec. Only trusted bootstrap code is Ruby;
212
+ # the script and its arguments remain data for the native interpreter.
213
+ bootstrap = <<~RUBY
214
+ $LOAD_PATH.replace(#{load_path.inspect})
215
+ require #{File.expand_path("script_runner", __dir__).inspect}
216
+ hosted = ARGV.shift == "1"
217
+ descriptors = ARGV.shift.split(",").to_h do |value|
218
+ fd = Integer(value)
219
+ io = IO.for_fd(fd)
220
+ io.close_on_exec = true
221
+ io.sync = true
222
+ [fd, io]
223
+ end
224
+ name = ARGV.shift
225
+ exit Kward::NativeShell::ScriptRunner.run(["--", *ARGV], name: name, descriptors: descriptors, own_descriptors: true, hosted: hosted, process_owned: true)
226
+ RUBY
227
+ descriptors = streams.filter_map { |fd, io| fd if fd > 2 && io }.join(",")
228
+ execve(RbConfig.ruby, [RbConfig.ruby, "--disable=gems,rubyopt", "-e", bootstrap, "--",
229
+ hosted ? "1" : "0", descriptors, arguments.first, executable, *arguments.drop(1)], environment)
230
+ end
231
+ rescue StandardError => e
232
+ unless STDERR.closed?
233
+ STDERR.puts("kwsh: #{e.message}")
234
+ STDERR.flush
235
+ end
236
+ e.is_a?(Errno::ENOENT) ? 127 : 126
237
+ ensure
238
+ begin
239
+ state&.restore
240
+ ensure
241
+ state&.close
242
+ copies&.each_value { |io| io.close if io && !io.closed? }
243
+ end
244
+ end
245
+
246
+ def self.redirect_standard_streams(streams)
247
+ copies = {}
248
+ streams.each do |fd, io|
249
+ next unless fd < 3
250
+ copies[fd] = io && IO.for_fd(io.fcntl(Fcntl::F_DUPFD, 32))
251
+ copies[fd].close_on_exec = true if copies[fd]
252
+ end
253
+ copies.each do |fd, source|
254
+ standard = [STDIN, STDOUT, STDERR][fd]
255
+ if source
256
+ standard.reopen(source)
257
+ standard.sync = true
258
+ else
259
+ # POSIX permits closed standard descriptors to be reopened on an
260
+ # unspecified file. Keep the standalone prompt's Ruby IOs usable.
261
+ standard.reopen(File::NULL, fd.zero? ? "r" : "w")
262
+ end
263
+ end
264
+ ensure
265
+ copies&.each_value { |io| io.close if io && !io.closed? }
266
+ end
267
+
268
+ def self.execve(executable, arguments, environment)
269
+ argv = arguments.map { |argument| Fiddle::Pointer[argument + "\0"] }
270
+ envp = environment.map { |key, value| Fiddle::Pointer["#{key}=#{value}\0"] }
271
+ argv_pointer = (argv.map(&:to_i) + [0]).pack("J*")
272
+ envp_pointer = (envp.map(&:to_i) + [0]).pack("J*")
273
+ EXECVE.call(executable, argv_pointer, envp_pointer)
274
+ raise SystemCallError.new(executable, Fiddle.last_error)
275
+ end
276
+ private_class_method :execve
277
+ end
278
+ end
279
+ end
@@ -0,0 +1,40 @@
1
+ require "stringio"
2
+
3
+ module Kward
4
+ module NativeShell
5
+ # Read one complete command at a time. In stdin mode the next byte may belong
6
+ # to read or an external command, so never buffer past the logical boundary.
7
+ module Program
8
+ def self.source_encoding(source)
9
+ source = source.dup.force_encoding(Encoding::UTF_8)
10
+ source.force_encoding(Encoding::BINARY) unless source.valid_encoding?
11
+ source
12
+ end
13
+ private_class_method :source_encoding
14
+
15
+ def self.each(input, aliases: {}, on_read: nil)
16
+ source = +"".b
17
+ line = 1
18
+ loop do
19
+ begin
20
+ byte = input.readpartial(1)
21
+ rescue EOFError
22
+ yield Parser.parse(source_encoding(source), aliases: aliases, line: line) unless source.empty?
23
+ break
24
+ end
25
+ on_read&.call(byte)
26
+ source << byte
27
+ next unless byte == "\n"
28
+ begin
29
+ tree = Parser.parse(source_encoding(source), aliases: aliases, line: line, eof: false)
30
+ rescue IncompleteInput
31
+ next
32
+ end
33
+ yield tree
34
+ line += source.count("\n")
35
+ source = +"".b
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end