kward 0.84.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 (245) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +164 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/Gemfile.lock +6 -2
  5. data/README.md +5 -5
  6. data/Rakefile +3 -0
  7. data/doc/agent-tools.md +24 -1
  8. data/doc/api.md +20 -2
  9. data/doc/composer.md +5 -4
  10. data/doc/configuration.md +86 -14
  11. data/doc/extensibility.md +4 -1
  12. data/doc/getting-started.md +3 -3
  13. data/doc/git.md +4 -2
  14. data/doc/memory.md +1 -1
  15. data/doc/pan.md +6 -0
  16. data/doc/permissions.md +4 -4
  17. data/doc/plugins.md +504 -20
  18. data/doc/providers.md +10 -4
  19. data/doc/rpc.md +279 -23
  20. data/doc/sandboxing.md +11 -5
  21. data/doc/security.md +7 -1
  22. data/doc/session-management.md +41 -5
  23. data/doc/shell.md +643 -27
  24. data/doc/tabs.md +8 -3
  25. data/doc/transports.md +15 -0
  26. data/doc/troubleshooting.md +1 -1
  27. data/doc/usage.md +25 -12
  28. data/doc/workspace-tools.md +9 -0
  29. data/examples/plugins/openrouter_proxy/README.md +103 -0
  30. data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
  31. data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
  32. data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
  33. data/examples/plugins/openrouter_proxy/server.rb +186 -0
  34. data/examples/plugins/space_invaders.rb +1 -2
  35. data/examples/plugins/stardate_footer.rb +2 -2
  36. data/examples/plugins/telegram/plugin.rb +1 -1
  37. data/exe/kward +2 -2
  38. data/exe/kwsh +7 -0
  39. data/kward.gemspec +3 -1
  40. data/lib/kward/agent.rb +66 -19
  41. data/lib/kward/cli/commands.rb +41 -11
  42. data/lib/kward/cli/compaction.rb +9 -3
  43. data/lib/kward/cli/interactive_turn.rb +10 -0
  44. data/lib/kward/cli/plugins.rb +85 -12
  45. data/lib/kward/cli/prompt_interface.rb +29 -6
  46. data/lib/kward/cli/rendering.rb +24 -28
  47. data/lib/kward/cli/runtime_helpers.rb +168 -56
  48. data/lib/kward/cli/sessions.rb +9 -5
  49. data/lib/kward/cli/settings/menus.rb +745 -0
  50. data/lib/kward/cli/settings/model.rb +316 -0
  51. data/lib/kward/cli/settings.rb +6 -1055
  52. data/lib/kward/cli/slash_commands.rb +45 -4
  53. data/lib/kward/cli/tabs.rb +257 -44
  54. data/lib/kward/cli/tool_summaries.rb +2 -0
  55. data/lib/kward/{cli_transcript_formatter.rb → cli/transcript_formatter.rb} +6 -13
  56. data/lib/kward/cli/worktrees.rb +66 -2
  57. data/lib/kward/cli.rb +82 -31
  58. data/lib/kward/compactor.rb +18 -7
  59. data/lib/kward/config/core.rb +400 -0
  60. data/lib/kward/config/extensions.rb +99 -0
  61. data/lib/kward/config/paths.rb +11 -0
  62. data/lib/kward/config/prompts.rb +313 -0
  63. data/lib/kward/config/settings.rb +257 -0
  64. data/lib/kward/config_files.rb +14 -1008
  65. data/lib/kward/conversation.rb +41 -2
  66. data/lib/kward/entrypoint.rb +14 -0
  67. data/lib/kward/events.rb +2 -0
  68. data/lib/kward/generated_images.rb +237 -0
  69. data/lib/kward/image_attachments.rb +1 -1
  70. data/lib/kward/model/catalog.rb +6 -3
  71. data/lib/kward/model/client.rb +107 -38
  72. data/lib/kward/model/codex_images.rb +83 -0
  73. data/lib/kward/model/copilot_models.rb +10 -6
  74. data/lib/kward/model/model_info.rb +42 -7
  75. data/lib/kward/{openrouter_model_cache.rb → model/openrouter_model_cache.rb} +7 -5
  76. data/lib/kward/model/payloads.rb +18 -8
  77. data/lib/kward/model/sources.rb +3 -1
  78. data/lib/kward/model/stream_parser.rb +4 -7
  79. data/lib/kward/model/typesafe_client.rb +78 -0
  80. data/lib/kward/pan/index.html.erb +8 -1
  81. data/lib/kward/pan/server.rb +23 -8
  82. data/lib/kward/permissions/policy.rb +6 -2
  83. data/lib/kward/plugin_registry.rb +2 -659
  84. data/lib/kward/plugins/actions.rb +453 -0
  85. data/lib/kward/plugins/chat_contract.rb +121 -0
  86. data/lib/kward/{plugin_chat_runtime.rb → plugins/chat_runtime.rb} +62 -18
  87. data/lib/kward/plugins/codex_responses.rb +73 -0
  88. data/lib/kward/plugins/host.rb +248 -0
  89. data/lib/kward/plugins/http_server.rb +83 -0
  90. data/lib/kward/plugins/registry.rb +1229 -0
  91. data/lib/kward/plugins/resources.rb +206 -0
  92. data/lib/kward/plugins/turn_request.rb +36 -0
  93. data/lib/kward/plugins/ui.rb +219 -0
  94. data/lib/kward/prompt_interface/composer_controller.rb +0 -1
  95. data/lib/kward/prompt_interface/composer_renderer.rb +160 -73
  96. data/lib/kward/prompt_interface/composer_state.rb +1 -1
  97. data/lib/kward/prompt_interface/editor/controller.rb +2 -2
  98. data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
  99. data/lib/kward/prompt_interface/editor/runner.rb +1 -1
  100. data/lib/kward/prompt_interface/editor/state.rb +1 -1
  101. data/lib/kward/prompt_interface/file_overlay.rb +3 -3
  102. data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
  103. data/lib/kward/prompt_interface/git_prompt.rb +18 -11
  104. data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
  105. data/lib/kward/prompt_interface/interactive/state.rb +18 -20
  106. data/lib/kward/prompt_interface/key_handler.rb +108 -25
  107. data/lib/kward/prompt_interface/layout.rb +11 -1
  108. data/lib/kward/prompt_interface/overlay_renderer.rb +26 -9
  109. data/lib/kward/prompt_interface/plugin_ui_requests.rb +82 -0
  110. data/lib/kward/prompt_interface/project_browser.rb +27 -54
  111. data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
  112. data/lib/kward/prompt_interface/question_prompt.rb +19 -28
  113. data/lib/kward/prompt_interface/runtime_state.rb +37 -10
  114. data/lib/kward/prompt_interface/screen.rb +30 -20
  115. data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
  116. data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
  117. data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
  118. data/lib/kward/prompt_interface.rb +149 -30
  119. data/lib/kward/prompts/commands.rb +2 -1
  120. data/lib/kward/prompts.rb +7 -1
  121. data/lib/kward/{adaptive_pty_output_sink.rb → pty/adaptive_output_sink.rb} +39 -5
  122. data/lib/kward/{interactive_pty_runner.rb → pty/interactive_runner.rb} +22 -30
  123. data/lib/kward/{local_command_runner.rb → pty/local_command_runner.rb} +1 -1
  124. data/lib/kward/{local_pty_command_runner.rb → pty/local_pty_runner.rb} +3 -9
  125. data/lib/kward/{pty_output_sink.rb → pty/output_sink.rb} +9 -4
  126. data/lib/kward/pty/transcript_normalizer.rb +137 -0
  127. data/lib/kward/rpc/config_manager.rb +3 -0
  128. data/lib/kward/rpc/plugin_chat_manager.rb +30 -10
  129. data/lib/kward/rpc/prompt_bridge.rb +25 -0
  130. data/lib/kward/rpc/server.rb +118 -17
  131. data/lib/kward/rpc/session_manager.rb +208 -60
  132. data/lib/kward/rpc/session_tree_rows.rb +2 -2
  133. data/lib/kward/rpc/tool_metadata.rb +1 -1
  134. data/lib/kward/rpc/transcript_normalizer.rb +11 -7
  135. data/lib/kward/sandbox/command_runner.rb +1 -1
  136. data/lib/kward/{session_catalog.rb → sessions/catalog.rb} +1 -1
  137. data/lib/kward/{session_store.rb → sessions/store.rb} +39 -12
  138. data/lib/kward/{session_tree_nodes.rb → sessions/tree_nodes.rb} +3 -3
  139. data/lib/kward/{session_tree_renderer.rb → sessions/tree_renderer.rb} +4 -4
  140. data/lib/kward/{session_tree_tool_display.rb → sessions/tree_tool_display.rb} +1 -1
  141. data/lib/kward/{kwsh.rb → shell/kwsh.rb} +30 -10
  142. data/lib/kward/{kwshrc.rb → shell/kwshrc.rb} +11 -2
  143. data/lib/kward/shell/native_arithmetic.rb +255 -0
  144. data/lib/kward/shell/native_arrays.rb +102 -0
  145. data/lib/kward/shell/native_braces.rb +96 -0
  146. data/lib/kward/shell/native_builtins.rb +476 -0
  147. data/lib/kward/shell/native_completion.rb +169 -0
  148. data/lib/kward/shell/native_condition.rb +75 -0
  149. data/lib/kward/shell/native_directory.rb +116 -0
  150. data/lib/kward/shell/native_exec_state.rb +73 -0
  151. data/lib/kward/shell/native_expansion.rb +252 -0
  152. data/lib/kward/shell/native_fields.rb +62 -0
  153. data/lib/kward/shell/native_glob.rb +96 -0
  154. data/lib/kward/shell/native_history.rb +242 -0
  155. data/lib/kward/shell/native_interactive.rb +164 -0
  156. data/lib/kward/shell/native_invocation.rb +42 -0
  157. data/lib/kward/shell/native_jobs.rb +250 -0
  158. data/lib/kward/shell/native_line_editor.rb +382 -0
  159. data/lib/kward/shell/native_line_terminal.rb +286 -0
  160. data/lib/kward/shell/native_locale.rb +109 -0
  161. data/lib/kward/shell/native_locals.rb +57 -0
  162. data/lib/kward/shell/native_pattern.rb +200 -0
  163. data/lib/kward/shell/native_process.rb +279 -0
  164. data/lib/kward/shell/native_program.rb +40 -0
  165. data/lib/kward/shell/native_programmable_completion.rb +115 -0
  166. data/lib/kward/shell/native_prompt_format.rb +89 -0
  167. data/lib/kward/shell/native_runtime.rb +964 -0
  168. data/lib/kward/shell/native_substitution.rb +110 -0
  169. data/lib/kward/shell/native_syntax.rb +946 -0
  170. data/lib/kward/shell/native_traps.rb +196 -0
  171. data/lib/kward/shell/native_vi.rb +407 -0
  172. data/lib/kward/shell/native_vi_terminal.rb +19 -0
  173. data/lib/kward/shell/native_worker.rb +82 -0
  174. data/lib/kward/{persistent_shell_session.rb → shell/persistent_session.rb} +187 -193
  175. data/lib/kward/shell/prompt.rb +66 -0
  176. data/lib/kward/shell/prompt_agent.rb +17 -0
  177. data/lib/kward/{shell_prompt_session.rb → shell/prompt_session.rb} +1 -1
  178. data/lib/kward/shell/script_runner.rb +244 -0
  179. data/lib/kward/shell/standalone_agent.rb +31 -0
  180. data/lib/kward/shell/standalone_link.rb +78 -0
  181. data/lib/kward/shell/standalone_terminal.rb +287 -0
  182. data/lib/kward/skills/trust_store.rb +1 -1
  183. data/lib/kward/tabs/driver.rb +194 -0
  184. data/lib/kward/{tab_store.rb → tabs/store.rb} +2 -2
  185. data/lib/kward/{clipboard.rb → terminal/clipboard.rb} +1 -1
  186. data/lib/kward/{terminal_image_support.rb → terminal/image_support.rb} +1 -1
  187. data/lib/kward/{terminal_keys.rb → terminal/keys.rb} +22 -1
  188. data/lib/kward/terminal/locale_text.rb +29 -0
  189. data/lib/kward/{terminal_sequences.rb → terminal/sequences.rb} +45 -4
  190. data/lib/kward/tools/base.rb +18 -0
  191. data/lib/kward/tools/context_for_task.rb +15 -6
  192. data/lib/kward/tools/datetime.rb +85 -0
  193. data/lib/kward/tools/edit_file.rb +9 -6
  194. data/lib/kward/tools/generate_image.rb +36 -0
  195. data/lib/kward/tools/git_commit.rb +13 -7
  196. data/lib/kward/tools/list_directory.rb +4 -4
  197. data/lib/kward/tools/plugin_tool.rb +41 -0
  198. data/lib/kward/tools/prepare_shell_command.rb +1 -1
  199. data/lib/kward/tools/read_file.rb +7 -6
  200. data/lib/kward/tools/registry.rb +130 -21
  201. data/lib/kward/tools/run_shell_command.rb +10 -8
  202. data/lib/kward/tools/search/code.rb +1 -1
  203. data/lib/kward/tools/summarize_file_structure.rb +5 -5
  204. data/lib/kward/tools/tool_call.rb +4 -1
  205. data/lib/kward/tools/typesafe_evaluate.rb +81 -0
  206. data/lib/kward/tools/workspace_targets.rb +58 -0
  207. data/lib/kward/tools/write_file.rb +9 -6
  208. data/lib/kward/{export_path.rb → transcripts/export_path.rb} +1 -1
  209. data/lib/kward/{markdown_transcript.rb → transcripts/markdown_transcript.rb} +10 -2
  210. data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
  211. data/lib/kward/transport/contracts.rb +200 -0
  212. data/lib/kward/transport/gateway.rb +81 -36
  213. data/lib/kward/transport/plugin_chat_gateway.rb +3 -2
  214. data/lib/kward/transport.rb +1 -200
  215. data/lib/kward/turn_summary.rb +68 -0
  216. data/lib/kward/version.rb +1 -1
  217. data/lib/kward/{workspace_factory.rb → workspace/factory.rb} +2 -2
  218. data/lib/kward/{git_worktree_manager.rb → workspace/git_worktree_manager.rb} +28 -0
  219. data/lib/kward/{workspace.rb → workspace/workspace.rb} +3 -3
  220. data/lib/main.rb +6 -3
  221. data/templates/default/fulldoc/html/css/kward.css +125 -0
  222. data/templates/default/fulldoc/html/images/kward_screen_1.png +0 -0
  223. data/templates/default/fulldoc/html/setup.rb +1 -1
  224. data/templates/default/layout/html/layout.erb +16 -4
  225. metadata +150 -48
  226. data/lib/kward/pty_transcript_normalizer.rb +0 -93
  227. data/lib/kward/shell_prompt.rb +0 -52
  228. data/lib/kward/tab_driver.rb +0 -90
  229. data/templates/default/fulldoc/html/images/kward_workflow.svg +0 -52
  230. /data/lib/kward/{editor_prompt.rb → cli/editor_prompt.rb} +0 -0
  231. /data/lib/kward/{editor_prompt_session.rb → cli/editor_prompt_session.rb} +0 -0
  232. /data/lib/kward/{diff_view_mode.rb → prompt_interface/editor/diff_view_mode.rb} +0 -0
  233. /data/lib/kward/{editor_mode.rb → prompt_interface/editor/editor_mode.rb} +0 -0
  234. /data/lib/kward/{markdown_code_block.rb → prompt_interface/editor/markdown_code_block.rb} +0 -0
  235. /data/lib/kward/{scratchpad_languages.rb → prompt_interface/editor/scratchpad_languages.rb} +0 -0
  236. /data/lib/kward/{scratchpad_runner.rb → prompt_interface/editor/scratchpad_runner.rb} +0 -0
  237. /data/lib/kward/{detached_run.rb → pty/detached_run.rb} +0 -0
  238. /data/lib/kward/{session_diff.rb → sessions/diff.rb} +0 -0
  239. /data/lib/kward/{session_naming.rb → sessions/naming.rb} +0 -0
  240. /data/lib/kward/{session_trash.rb → sessions/trash.rb} +0 -0
  241. /data/lib/kward/{ansi.rb → terminal/ansi.rb} +0 -0
  242. /data/lib/kward/{terminal_text.rb → terminal/text.rb} +0 -0
  243. /data/lib/kward/{transcript_export.rb → transcripts/transcript_export.rb} +0 -0
  244. /data/lib/kward/{project_files.rb → workspace/files.rb} +0 -0
  245. /data/lib/kward/{path_guard.rb → workspace/path_guard.rb} +0 -0
@@ -0,0 +1,169 @@
1
+ require_relative "native_syntax"
2
+
3
+ module Kward
4
+ module NativeShell
5
+ # Complete literal words without expanding the draft or executing shell code.
6
+ class Completion
7
+ Result = Struct.new(:range, :replacement, :candidates, :choices, :choice_range, keyword_init: true)
8
+ COMMAND_BOUNDARIES = %w[; && || | & ( ) { } ;; ;&].freeze
9
+ COMMAND_PREFIXES = %w[! if then elif else while until do].freeze
10
+ REDIRECTIONS = %w[< > << <<- >> <> >| <& >&].freeze
11
+
12
+ def initialize(directory:, variables:, commands:)
13
+ @directory, @variables, @commands = directory, variables, commands
14
+ end
15
+
16
+ def complete(input, cursor, &programmed)
17
+ source = input[0...cursor]
18
+ tokens = lex(source)
19
+ current = tokens.pop if tokens.last&.value.is_a?(Word) && tokens.last.finish >= cursor
20
+ start = current ? current.start : cursor
21
+ raw = source[start..]
22
+ prefix = literal_text(current) if current
23
+ return if current && !prefix
24
+ return unless current || source[tokens.last&.finish || 0..].match?(/\A[ \t\r]*\z/)
25
+
26
+ prefix ||= ""
27
+ words, redirect = command_context(tokens)
28
+ command = words.first || ("" unless words.empty?)
29
+ command_position = command.nil? && !redirect
30
+ tilde = raw.start_with?("~/")
31
+ names = programmed.call(words + [prefix]) if programmed && words.first && !redirect && words.none?(&:nil?)
32
+ if names
33
+ names = names.select { |name| candidate?(name, prefix) }.uniq.sort
34
+ tilde = false
35
+ elsif command_position && !prefix.include?("/")
36
+ names = command_candidates(prefix)
37
+ else
38
+ names = path_candidates(prefix, tilde: tilde, directories_only: command == "cd" && !redirect, executable: command_position)
39
+ end
40
+ return if names.empty?
41
+
42
+ match = names.one? ? names.first : common_prefix(names)
43
+ finish = word_end(input, current, cursor)
44
+ format = ->(name) { candidate_text(name, tilde: tilde, command: command_position && !prefix.include?("/")) }
45
+ choices = names.map do |name|
46
+ text = format.call(name)
47
+ text += " " if finish == input.length && !name.end_with?("/")
48
+ text
49
+ end
50
+ extend_word = names.one? || match.length > prefix.length
51
+ Result.new(range: start...(extend_word ? finish : cursor),
52
+ replacement: names.one? ? choices.first : (extend_word ? format.call(match) : raw),
53
+ candidates: names, choices: choices, choice_range: start...finish)
54
+ rescue SyntaxError, ArgumentError, EncodingError, SystemCallError
55
+ nil
56
+ end
57
+
58
+ private
59
+
60
+ def lex(source)
61
+ Lexer.new(source, eof: false).tokens
62
+ rescue IncompleteInput => e
63
+ # The cursor is often inside a quoted filename. Close the quote for
64
+ # tokenization only; other incomplete syntax is not evaluated or repaired.
65
+ quote = if e.message.end_with?("unclosed single quote")
66
+ "'"
67
+ elsif e.message.end_with?("unclosed double quote")
68
+ '"'
69
+ else
70
+ raise
71
+ end
72
+ Lexer.new(source + quote, eof: false).tokens
73
+ end
74
+
75
+ def word_end(input, current, cursor)
76
+ return cursor unless current && cursor < input.length
77
+
78
+ word = Lexer.new(input[current.start..], eof: false).next_token
79
+ current.start + word.finish
80
+ end
81
+
82
+ def literal_text(token)
83
+ return unless token.value.parts.all? { |part| part.kind == :literal }
84
+
85
+ token.value.parts.map(&:text).join
86
+ end
87
+
88
+ def command_context(tokens)
89
+ words = []
90
+ redirect = false
91
+ tokens.each do |token|
92
+ value = token.value
93
+ if COMMAND_BOUNDARIES.include?(value)
94
+ words.clear
95
+ redirect = false
96
+ elsif REDIRECTIONS.include?(value) || (value.is_a?(Array) && value.first == :redirect)
97
+ redirect = true
98
+ elsif value.is_a?(Word)
99
+ if redirect
100
+ redirect = false
101
+ elsif !words.empty? || !value.assignment && !COMMAND_PREFIXES.include?(value.literal)
102
+ words << literal_text(token)
103
+ end
104
+ end
105
+ end
106
+ [words, redirect]
107
+ end
108
+
109
+ def command_candidates(prefix)
110
+ return [] if prefix.empty?
111
+
112
+ names = @commands.select { |name| candidate?(name, prefix) }
113
+ @variables.fetch("PATH", "/usr/bin:/bin").split(File::PATH_SEPARATOR, -1).each do |entry|
114
+ directory = @directory.resolve(entry.empty? ? "." : entry)
115
+ Dir.each_child(directory, encoding: prefix.encoding) do |name|
116
+ next unless candidate?(name, prefix)
117
+
118
+ file = File.join(directory, name)
119
+ names << name if File.file?(file) && File.executable?(file)
120
+ end
121
+ rescue SystemCallError
122
+ next
123
+ end
124
+ names.uniq.sort
125
+ end
126
+
127
+ def path_candidates(prefix, tilde:, directories_only:, executable:)
128
+ dir, slash, basename = prefix.rpartition("/")
129
+ spelling = dir + slash
130
+ path = if tilde
131
+ return [] if @variables["HOME"].to_s.empty?
132
+
133
+ File.join(@variables["HOME"], spelling.delete_prefix("~/"))
134
+ else
135
+ spelling.empty? ? "." : spelling
136
+ end
137
+ directory = @directory.resolve(path)
138
+ Dir.each_child(directory, encoding: prefix.encoding).filter_map do |name|
139
+ next unless candidate?(name, basename)
140
+ next if name.start_with?(".") && !basename.start_with?(".")
141
+
142
+ file = File.join(directory, name)
143
+ if File.directory?(file)
144
+ "#{spelling}#{name}/"
145
+ elsif !directories_only && (!executable || (File.file?(file) && File.executable?(file)))
146
+ "#{spelling}#{name}"
147
+ end
148
+ end.sort
149
+ end
150
+
151
+ def candidate?(name, prefix)
152
+ name.valid_encoding? && name.start_with?(prefix) && !name.match?(/[[:cntrl:]]/)
153
+ end
154
+
155
+ def candidate_text(match, tilde:, command:)
156
+ return "~/#{Shellwords.escape(match.delete_prefix('~/'))}" if tilde && match != "~/"
157
+ return match if tilde || (command && match.match?(/\A[A-Za-z0-9_!%,@-]+\z/))
158
+
159
+ Shellwords.escape(match)
160
+ end
161
+
162
+ def common_prefix(names)
163
+ prefix = names.first.dup
164
+ names.drop(1).each { |name| prefix.chop! until name.start_with?(prefix) }
165
+ prefix
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,75 @@
1
+ module Kward
2
+ module NativeShell
3
+ module Condition
4
+ UNARY = %w[-n -z -e -a -f -d -s -r -w -x -L -h -b -c -p -S -t -v].freeze
5
+ BINARY = %w[= == != =~ < > -eq -ne -lt -le -gt -ge -nt -ot -ef].freeze
6
+
7
+ private
8
+
9
+ def condition_value(tree)
10
+ kind, *values = tree
11
+ case kind
12
+ when :and then condition_value(values[0]) && condition_value(values[1])
13
+ when :or then condition_value(values[0]) || condition_value(values[1])
14
+ when :not then !condition_value(values.first)
15
+ when :string then !scalar(values.first).empty?
16
+ when :unary
17
+ operator, word = values
18
+ value = scalar(word)
19
+ path = @directory.resolve(value) unless %w[-n -z -v -t].include?(operator)
20
+ case operator
21
+ when "-n" then !value.empty?
22
+ when "-z" then value.empty?
23
+ when "-v" then parameter_set?(value)
24
+ when "-t" then @streams[value.to_i]&.tty? || false
25
+ when "-e", "-a" then File.exist?(path)
26
+ when "-f" then File.file?(path)
27
+ when "-d" then File.directory?(path)
28
+ when "-s" then File.size?(path).to_i.positive?
29
+ when "-r" then File.readable?(path)
30
+ when "-w" then File.writable?(path)
31
+ when "-x" then File.executable?(path)
32
+ when "-L", "-h" then File.symlink?(path)
33
+ when "-b" then File.blockdev?(path)
34
+ when "-c" then File.chardev?(path)
35
+ when "-p" then File.pipe?(path)
36
+ when "-S" then File.socket?(path)
37
+ end
38
+ when :binary
39
+ operator, left, right = values
40
+ value = scalar(left)
41
+ if %w[= == !=].include?(operator)
42
+ match = matches_pattern?(pattern(right), value)
43
+ return operator == "!=" ? !match : match
44
+ end
45
+ if operator == "=~"
46
+ expression = word_fields(right).flatten.map { |part| part.quoted ? Regexp.escape(part.text) : part.text }.join
47
+ match = Regexp.new(expression, timeout: 0.1).match(value)
48
+ captures = match ? match.to_a.each_with_index.to_h { |capture, index| [index, capture.to_s] } : {}
49
+ @arrays["BASH_REMATCH"] = Arrays::ArrayValue.new(associative: false, values: captures)
50
+ return !!match
51
+ end
52
+ other = scalar(right)
53
+ case operator
54
+ when "<" then Locale.with(@variables) { |locale| locale.compare(value, other).negative? }
55
+ when ">" then Locale.with(@variables) { |locale| locale.compare(value, other).positive? }
56
+ when "-eq", "-ne", "-lt", "-le", "-gt", "-ge"
57
+ operators = { "-eq" => "==", "-ne" => "!=", "-lt" => "<", "-le" => "<=", "-gt" => ">", "-ge" => ">=" }
58
+ Arithmetic.binary(operators.fetch(operator), arithmetic_value(value).to_i, arithmetic_value(other).to_i) == 1
59
+ when "-nt", "-ot", "-ef"
60
+ first, second = [value, other].map { |name| @directory.resolve(name) }
61
+ if operator == "-ef"
62
+ File.exist?(first) && File.exist?(second) && File.identical?(first, second)
63
+ elsif operator == "-nt"
64
+ File.exist?(first) && (!File.exist?(second) || File.mtime(first) > File.mtime(second))
65
+ else
66
+ File.exist?(second) && (!File.exist?(first) || File.mtime(first) < File.mtime(second))
67
+ end
68
+ end
69
+ end
70
+ rescue RegexpError, Regexp::TimeoutError => e
71
+ raise ExecutionError, "conditional regular expression: #{e.message}"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,116 @@
1
+ require "fiddle"
2
+ require "fcntl"
3
+
4
+ module Kward
5
+ module NativeShell
6
+ # Owns directory identity independently of the shell's logical PWD. Never
7
+ # changes the host cwd; only an exec-bound child may enter this descriptor.
8
+ class Directory
9
+ OPENAT = Fiddle::Function.new(Fiddle::Handle::DEFAULT["openat"], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_VARIADIC], Fiddle::TYPE_INT)
10
+ FACCESSAT = Fiddle::Function.new(Fiddle::Handle::DEFAULT["faccessat"], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
11
+ FCHDIR = Fiddle::Function.new(Fiddle::Handle::DEFAULT["fchdir"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
12
+
13
+ # Darwin sys/fcntl.h: O_SEARCH | O_CLOEXEC, AT_FDCWD, AT_EACCESS.
14
+ # Linux asm-generic/fcntl.h: O_PATH | O_DIRECTORY | O_CLOEXEC. O_PATH
15
+ # needs a separate effective-ID search-permission check on the directory.
16
+ FLAGS, CLOEXEC, AT_FDCWD, AT_EACCESS = if RUBY_PLATFORM.include?("darwin")
17
+ [0x41100000, 0x01000000, -2, 0x10]
18
+ elsif RUBY_PLATFORM.include?("linux")
19
+ [0x290000, 0x80000, -100, 0x200]
20
+ else
21
+ raise LoadError, "native directory handles require macOS or Linux"
22
+ end
23
+
24
+ def initialize(path, base: nil)
25
+ raise ArgumentError, "NUL bytes are not allowed in directory paths" if path.include?("\0")
26
+ fd = OPENAT.call(base ? base.fileno : AT_FDCWD, path, FLAGS, Fiddle::TYPE_INT, 0)
27
+ raise SystemCallError.new(path, Fiddle.last_error) if fd < 0
28
+ @handle = IO.for_fd(fd)
29
+ if FACCESSAT.call(fd, ".", 1, AT_EACCESS) < 0
30
+ error = Fiddle.last_error
31
+ close
32
+ raise SystemCallError.new(path, error)
33
+ end
34
+ end
35
+
36
+ def initialize_copy(other)
37
+ super
38
+ @handle = other.handle.dup
39
+ @handle.close_on_exec = true
40
+ end
41
+
42
+ def fileno
43
+ @handle.fileno
44
+ end
45
+
46
+ def close
47
+ @handle.close unless @handle.closed?
48
+ end
49
+
50
+ def closed?
51
+ @handle.closed?
52
+ end
53
+
54
+ def same?(path)
55
+ stat = File.stat(path)
56
+ current = @handle.stat
57
+ stat.dev == current.dev && stat.ino == current.ino
58
+ rescue SystemCallError
59
+ false
60
+ end
61
+
62
+ def physical_path
63
+ File.realpath(access_path)
64
+ end
65
+
66
+ def access_path
67
+ path = if RUBY_PLATFORM.include?("darwin")
68
+ # F_GETPATH uses the kernel's MAXPATHLEN-sized output buffer.
69
+ buffer = "\0" * 1024
70
+ @handle.fcntl(50, buffer)
71
+ buffer.split("\0", 2).first
72
+ else
73
+ File.readlink("/proc/self/fd/#{fileno}")
74
+ end
75
+ raise Errno::ENOENT, "current directory has no pathname" unless same?(path)
76
+ path
77
+ end
78
+
79
+ def resolve(path)
80
+ return path if path.start_with?("/")
81
+ # Do not expand_path here: symlink/.. must be resolved by the kernel.
82
+ File.join(access_path, path.delete_prefix("./"))
83
+ end
84
+
85
+ def open_file(path, mode = "r")
86
+ raise ArgumentError, "NUL bytes are not allowed in file paths" if path.include?("\0")
87
+ flags = mode.is_a?(Integer) ? mode : { "r" => File::RDONLY, "w" => File::WRONLY | File::CREAT | File::TRUNC, "a" => File::WRONLY | File::CREAT | File::APPEND }.fetch(mode)
88
+ fd = OPENAT.call(fileno, path, flags | CLOEXEC, Fiddle::TYPE_INT, 0o666)
89
+ raise SystemCallError.new(path, Fiddle.last_error) if fd < 0
90
+ file = IO.for_fd(fd)
91
+ return file unless block_given?
92
+ begin
93
+ yield file
94
+ ensure
95
+ file.close
96
+ end
97
+ end
98
+
99
+ def protect_descriptors(descriptors)
100
+ return unless descriptors.include?(fileno)
101
+ replacement = IO.for_fd(@handle.fcntl(Fcntl::F_DUPFD, [32, descriptors.max + 1].max))
102
+ replacement.close_on_exec = true
103
+ @handle.close
104
+ @handle = replacement
105
+ end
106
+
107
+ def enter
108
+ raise SystemCallError.new("fchdir", Fiddle.last_error) if FCHDIR.call(fileno) < 0
109
+ end
110
+
111
+ protected
112
+
113
+ attr_reader :handle
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,73 @@
1
+ require_relative "native_directory"
2
+
3
+ module Kward
4
+ module NativeShell
5
+ module NativeProcess
6
+ # A failed standalone interactive exec must return to a live Ruby process.
7
+ # Shell redirections are persisted separately; these backups protect the
8
+ # process's original IOs (including private descriptors), cwd and signals.
9
+ class ExecState
10
+ attr_reader :signals
11
+
12
+ def initialize(descriptors)
13
+ @streams = {}
14
+ @flags = {}
15
+ @signals = {}
16
+ @sync = [STDIN, STDOUT, STDERR].map { |io| io.sync unless io.closed? }
17
+ @mask = File.umask
18
+ @directory = Directory.new(".")
19
+ @directory.protect_descriptors(descriptors)
20
+ descriptors.each do |fd|
21
+ flags = FCNTL.call(fd, Fcntl::F_GETFD, 0)
22
+ if flags < 0
23
+ raise SystemCallError.new("fcntl", Fiddle.last_error) unless Fiddle.last_error == Errno::EBADF::Errno
24
+ @streams[fd] = nil
25
+ next
26
+ end
27
+ copy = FCNTL.call(fd, Fcntl::F_DUPFD, [32, descriptors.max + 1].max)
28
+ raise SystemCallError.new("dup", Fiddle.last_error) if copy < 0
29
+ @streams[fd] = IO.for_fd(copy)
30
+ @streams[fd].close_on_exec = true
31
+ @flags[fd] = [flags, FCNTL.call(fd, Fcntl::F_GETFL, 0)]
32
+ end
33
+ complete = true
34
+ ensure
35
+ close unless complete
36
+ end
37
+
38
+ def restore
39
+ @streams.each do |fd, source|
40
+ if fd < 3
41
+ standard = [STDIN, STDOUT, STDERR][fd]
42
+ if source
43
+ standard.reopen(source)
44
+ standard.sync = @sync[fd]
45
+ else
46
+ standard.close unless standard.closed?
47
+ end
48
+ elsif source
49
+ raise SystemCallError.new("dup2", Fiddle.last_error) if DUP2.call(source.fileno, fd) < 0
50
+ else
51
+ CLOSE.call(fd)
52
+ end
53
+ next unless source
54
+ descriptor_flags, file_flags = @flags.fetch(fd)
55
+ raise SystemCallError.new("fcntl", Fiddle.last_error) if FCNTL.call(fd, Fcntl::F_SETFD, descriptor_flags) < 0
56
+ raise SystemCallError.new("fcntl", Fiddle.last_error) if FCNTL.call(fd, Fcntl::F_SETFL, file_flags) < 0
57
+ end
58
+ @directory.enter
59
+ File.umask(@mask)
60
+ @signals.each { |name, handler| Signal.trap(name, handler || "DEFAULT") }
61
+ rescue StandardError => e
62
+ # Continuing with corrupt descriptors could expose private resources.
63
+ raise SystemExit.new(126, "exec recovery failed: #{e.message}")
64
+ end
65
+
66
+ def close
67
+ @streams.each_value { |io| io.close if io && !io.closed? }
68
+ @directory&.close
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,252 @@
1
+ require "etc"
2
+ require_relative "native_fields"
3
+ require_relative "native_pattern"
4
+ require_relative "native_locale"
5
+ require_relative "native_glob"
6
+ require_relative "native_braces"
7
+
8
+ module Kward
9
+ module NativeShell
10
+ class ExpansionError < StandardError; end
11
+
12
+ # Expansions preserve quote provenance. Substitutions execute parsed native
13
+ # trees; expanded values are never reparsed as shell source.
14
+ module Expansion
15
+ private
16
+
17
+ def ifs
18
+ @variables.fetch("IFS", Fields::DEFAULT_IFS)
19
+ end
20
+
21
+ def parameter(name)
22
+ return array_read(name).to_s if name.is_a?(Reference)
23
+ if @arrays.key?(name)
24
+ array = @arrays[name]
25
+ return array.values[array.associative ? "0" : 0].to_s
26
+ end
27
+ case name
28
+ when "?" then @status.to_s
29
+ when "!" then @last_background_pid.to_s
30
+ when "$" then @shell_pid.to_s
31
+ when "-" then Builtins::SHELL_OPTIONS.select { |_, name| @options.include?(name) }.keys.join + (@interactive ? "i" : "")
32
+ when "#" then @arguments.length.to_s
33
+ when "@" then @arguments.join(" ")
34
+ when "*" then @arguments.join(ifs[0].to_s)
35
+ when "0" then @name
36
+ when /\A\d+\z/ then @arguments[name.to_i - 1].to_s
37
+ else @variables.fetch(name, "")
38
+ end
39
+ end
40
+
41
+ def parameter_set?(name)
42
+ if name.is_a?(Reference)
43
+ return !array_values(name).empty? if %w[@ *].include?(name.subscript.literal)
44
+ return !array_read(name).nil?
45
+ end
46
+ if @arrays.key?(name)
47
+ array = @arrays[name]
48
+ return array.values.key?(array.associative ? "0" : 0)
49
+ end
50
+ case name
51
+ when "@", "*" then !@arguments.empty?
52
+ when "!" then !@last_background_pid.nil?
53
+ when "?", "$", "#", "0", "-" then true
54
+ when /\A\d+\z/ then name.to_i <= @arguments.length
55
+ else @variables.key?(name)
56
+ end
57
+ end
58
+
59
+ def arithmetic_value(expression)
60
+ if expression.is_a?(Word)
61
+ expression = expression.parts.map do |part|
62
+ part.kind == :literal ? part.text : scalar(Word.new(parts: [part]))
63
+ end.join
64
+ end
65
+ Arithmetic.evaluate(expression, assign: method(:assign)) do |name|
66
+ raise ExpansionError, "#{name}: unbound variable" if @options.include?("nounset") && !parameter_set?(name)
67
+ parameter(name)
68
+ end.to_s
69
+ rescue Arithmetic::Error => e
70
+ raise ExecutionError, e.message
71
+ end
72
+
73
+ def expand(word, assignment: false)
74
+ words = assignment ? [word] : Braces.expand(word)
75
+ words.flat_map { |item| expand_word(item, assignment: assignment) }
76
+ rescue Pattern::LimitError => e
77
+ raise ExpansionError, e.message
78
+ end
79
+
80
+ def expand_word(word, assignment: false)
81
+ groups = word_fields(word, assignment: assignment)
82
+ return [groups.map { |group| Fields.value(group) }.join(" ")] if assignment
83
+
84
+ groups.flat_map do |group|
85
+ preserve_empty = group.any? { |character| character.quoted && character.text.empty? }
86
+ group = group.reject { |character| character.text.empty? }
87
+ ranges = Fields.ranges(group, ifs)
88
+ next [""] if ranges.empty? && preserve_empty
89
+ ranges.flat_map do |range|
90
+ characters = group[range]
91
+ value = Fields.value(characters)
92
+ next [value] if @options.include?("noglob")
93
+ pattern = Fields.pattern(characters, glob: true)
94
+ matches = if pattern.match?(/[*?\[]/)
95
+ Locale.with(@variables) { |locale| Glob.new(pattern, cwd: @directory, locale: locale, globstar: @globstar).matches }
96
+ else
97
+ []
98
+ end
99
+ matches.empty? ? [value] : matches
100
+ end
101
+ end
102
+ end
103
+
104
+ def scalar(word)
105
+ word_fields(word).map { |group| Fields.value(group) }.join(" ")
106
+ end
107
+
108
+ def pattern(word)
109
+ word_fields(word).map { |group| Fields.pattern(group) }.join(" ")
110
+ end
111
+
112
+ def matches_pattern?(pattern, value)
113
+ Locale.with(@variables) { |locale| Pattern.new(pattern, locale: locale).match?(value) }
114
+ rescue Pattern::LimitError => e
115
+ raise ExpansionError, e.message
116
+ end
117
+
118
+ def word_fields(word, expanded: false, assignment: false)
119
+ groups = [[]]
120
+ word.parts.each_with_index do |part, index|
121
+ additions = case part.kind
122
+ when :parameter
123
+ parameter_fields(part.text, part.quoted)
124
+ when :command
125
+ [Fields.characters(capture_substitution(part.text), quoted: part.quoted, split: !part.quoted)]
126
+ when :process
127
+ [Fields.characters(process_substitution(*part.text), quoted: true)]
128
+ when :arithmetic
129
+ [Fields.characters(arithmetic_value(part.text), quoted: part.quoted, split: !part.quoted)]
130
+ else
131
+ [literal_fields(part, first: index.zero?, following: word.parts[index + 1], assignment: assignment, expanded: expanded)]
132
+ end
133
+ next if additions.empty?
134
+ groups.last.concat(additions.first)
135
+ groups.concat(additions.drop(1))
136
+ end
137
+ groups
138
+ rescue ExecutionError, Pattern::LimitError => e
139
+ raise ExpansionError, e.message
140
+ end
141
+
142
+ def literal_fields(part, first:, following:, assignment:, expanded:)
143
+ return Fields.characters(part.text, quoted: true) if part.quoted
144
+ pieces = assignment ? part.text.split(":", -1) : [part.text]
145
+ pieces.each_with_index.flat_map do |piece, index|
146
+ prefix = assignment && first && index.zero? ? piece[/\A[A-Za-z_][A-Za-z0-9_]*=/].to_s : ""
147
+ value = piece.delete_prefix(prefix)
148
+ match = value.match(/\A~([^\/]*)(\/.*)?\z/m) if first || index.positive?
149
+ continued = index == pieces.length - 1 && following && match && !match[2]
150
+ home = tilde_home(match[1]) if match && !continued
151
+ fields = if home
152
+ Fields.characters(prefix, split: expanded) + Fields.characters(home, quoted: true) + Fields.characters(match[2].to_s, split: expanded)
153
+ else
154
+ Fields.characters(piece, split: expanded)
155
+ end
156
+ index.zero? ? fields : Fields.characters(":", split: expanded) + fields
157
+ end
158
+ end
159
+
160
+ def tilde_home(name)
161
+ case name
162
+ when "" then @variables.fetch("HOME") { Etc.getpwuid.dir }
163
+ when "+" then @variables["PWD"]
164
+ when "-" then @variables["OLDPWD"]
165
+ else Etc.getpwnam(name).dir
166
+ end
167
+ rescue ArgumentError
168
+ nil
169
+ end
170
+
171
+ def parameter_fields(expression, quoted)
172
+ return parameter_value_fields(expression, quoted) unless expression.is_a?(Parameter)
173
+
174
+ name, operator, operand = expression.name, expression.operator, expression.word
175
+ value = parameter(name)
176
+ if operator == :keys
177
+ return array_fields(name, quoted, keys: true)
178
+ end
179
+ if operator == :length
180
+ all = name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
181
+ raise ExpansionError, "#{name}: unbound variable" if @options.include?("nounset") && !all && !%w[@ *].include?(name) && !parameter_set?(name)
182
+ length = if name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
183
+ array_values(name).length
184
+ else
185
+ %w[@ *].include?(name) ? @arguments.length : Locale.with(@variables) { |locale| locale.text(value).length }
186
+ end
187
+ return [Fields.characters(length.to_s, quoted: quoted, split: !quoted)]
188
+ end
189
+ if operator.start_with?("#", "%")
190
+ source = pattern(operand)
191
+ values = if name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
192
+ array_values(name)
193
+ else
194
+ %w[@ *].include?(name) ? @arguments : [value]
195
+ end
196
+ values = Locale.with(@variables) do |locale|
197
+ match = Pattern.new(source, locale: locale)
198
+ values.map { |item| remove_pattern(locale.text(item), match, operator) }
199
+ end
200
+ return parameter_value_fields(name, quoted, values: values)
201
+ end
202
+
203
+ empty = value.empty? && !(quoted && %w[@ *].include?(name))
204
+ missing = !parameter_set?(name) || operator.start_with?(":") && empty
205
+ action = operator.delete_prefix(":")
206
+ use_operand = action == "+" ? !missing : missing
207
+ return action == "+" ? [Fields.characters("", quoted: quoted)] : parameter_value_fields(name, quoted) unless use_operand
208
+
209
+ groups = word_fields(operand, expanded: true)
210
+ groups = [groups.flatten] if ifs.empty?
211
+ result = groups.map { |group| Fields.value(group) }.join(ifs[0].to_s)
212
+ case action
213
+ when "="
214
+ raise ExpansionError, "#{name}: cannot assign a special or positional parameter" unless name.is_a?(Reference) || name.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/)
215
+ assign(name, result)
216
+ when "?"
217
+ message = operand.parts.empty? ? "parameter is unset#{operator.start_with?(":") ? ' or null' : ''}" : result
218
+ raise ExpansionError, "#{name}: #{message}"
219
+ end
220
+ groups.first.concat(Fields.characters("", quoted: true)) if quoted && groups.all?(&:empty?)
221
+ groups
222
+ end
223
+
224
+ def parameter_value_fields(name, quoted, values: nil)
225
+ all = name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
226
+ if @options.include?("nounset") && !all && !%w[@ *].include?(name) && !parameter_set?(name)
227
+ raise ExpansionError, "#{name}: unbound variable"
228
+ end
229
+ if name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
230
+ return array_fields(name, quoted, values: values)
231
+ end
232
+ if name == "@" || name == "*" && !quoted
233
+ (values || @arguments).map { |value| Fields.characters(value, quoted: quoted, split: !quoted) }
234
+ else
235
+ value = values ? values.join(name == "*" ? ifs[0].to_s : " ") : parameter(name)
236
+ [Fields.characters(value, quoted: quoted, split: !quoted)]
237
+ end
238
+ end
239
+
240
+ def remove_pattern(value, pattern, operator)
241
+ sizes = operator.length == 2 ? value.length.downto(0) : 0.upto(value.length)
242
+ prefix = operator.start_with?("#")
243
+ sizes.each do |size|
244
+ candidate = prefix ? value[0, size] : value[value.length - size, size]
245
+ next unless pattern.match?(candidate)
246
+ return prefix ? value[size..] : value[0, value.length - size]
247
+ end
248
+ value
249
+ end
250
+ end
251
+ end
252
+ end