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,82 @@
1
+ require "io/console"
2
+ require "json"
3
+ require "socket"
4
+ require_relative "native_runtime"
5
+
6
+ module Kward
7
+ module NativeShell
8
+ # A single-threaded interpreter process owns the terminal and mutable shell
9
+ # state. Requests/state use a private socket, never terminal input/output.
10
+ class Worker
11
+ def self.run(descriptor)
12
+ control = UNIXSocket.for_fd(descriptor)
13
+ control.close_on_exec = true
14
+ control.sync = true
15
+ $stdout.sync = true
16
+ $stderr.sync = true
17
+ runtime = nil
18
+ Signal.trap("INT") { raise Interrupt }
19
+ %w[TERM HUP USR2].each { |signal| Signal.trap(signal) { raise SystemExit } }
20
+ %w[TSTP TTIN TTOU].each { |signal| Signal.trap(signal, "IGNORE") }
21
+
22
+ line = control.gets
23
+ return unless line
24
+ request = JSON.parse(line)
25
+ runtime = Runtime.new(cwd: request.fetch("cwd"), env: ENV.to_h, aliases: request.fetch("aliases", {}), interactive: true, process_owned: true)
26
+ runtime.with_signals do
27
+ loop do
28
+ status = begin
29
+ $stdin.echo = true
30
+ if request["config"]
31
+ apply_configuration(runtime, request["config"])
32
+ elsif request["suppress_git_pager"]
33
+ runtime.with_pager_suppressed { runtime.run(request.fetch("command")) }
34
+ else
35
+ runtime.run(request.fetch("command"))
36
+ end
37
+ rescue Interrupt
38
+ 130
39
+ ensure
40
+ $stdin.echo = false
41
+ end
42
+ status = runtime.finish if runtime.exited
43
+ $stdout.write("#{request.fetch('marker')}:#{status}:\n")
44
+ control.puts(JSON.generate(cwd: runtime.cwd, env: runtime.environment, aliases: runtime.aliases, exited: runtime.exited))
45
+ break if runtime.exited
46
+ loop do
47
+ begin
48
+ runtime.poll_signals
49
+ break if runtime.exited || IO.select([control], nil, nil, 0.05)
50
+ rescue Interrupt
51
+ # Ctrl-C at an idle prompt cancels input, not the shell lifetime.
52
+ end
53
+ end
54
+ if runtime.exited
55
+ runtime.finish
56
+ break
57
+ end
58
+ line = control.gets
59
+ break unless line
60
+ request = JSON.parse(line)
61
+ end
62
+ end
63
+ rescue EOFError, Errno::EIO, Errno::EPIPE
64
+ nil
65
+ ensure
66
+ runtime&.close
67
+ end
68
+
69
+ def self.apply_configuration(runtime, config)
70
+ config.fetch("env", {}).each do |key, value|
71
+ status = runtime.run("export #{key}=#{Shellwords.escape(value)}")
72
+ return status unless status.zero?
73
+ end
74
+ runtime.aliases.merge!(config.fetch("aliases", {}))
75
+ runtime.run(":")
76
+ end
77
+ private_class_method :apply_configuration
78
+ end
79
+ end
80
+ end
81
+
82
+ Kward::NativeShell::Worker.run(Integer(ARGV.fetch(0))) if $PROGRAM_NAME == __FILE__
@@ -2,18 +2,21 @@ require "io/console"
2
2
  require "pty"
3
3
  require "securerandom"
4
4
  require "shellwords"
5
- require_relative "ansi"
5
+ require "json"
6
+ require "socket"
7
+ require "rbconfig"
8
+ require_relative "native_syntax"
9
+ require_relative "../terminal/ansi"
6
10
  require_relative "kwsh"
7
11
  require_relative "kwshrc"
8
- require_relative "local_pty_command_runner"
9
- require_relative "detached_run"
12
+ require_relative "../pty/local_pty_runner"
13
+ require_relative "../pty/detached_run"
10
14
 
11
15
  # Namespace for the Kward CLI agent runtime.
12
16
  module Kward
13
- # Owns one interactive shell process and serializes user and agent commands.
14
- #
15
- # The process is kept alive between commands so shell variables, functions,
16
- # aliases, directory changes, and child-shell state belong to one session.
17
+ # Owns one native kwsh interpreter process and serializes user and agent
18
+ # commands. The PTY owns terminal input/output; a private socket carries
19
+ # interpreter requests and authoritative shell state.
17
20
  class PersistentShellSession
18
21
  Result = Kwsh::Result
19
22
  InteractiveResult = Struct.new(:exit_status, :input_forwarded, :tab_action, :background, keyword_init: true)
@@ -29,17 +32,40 @@ module Kward
29
32
 
30
33
  READ_SIZE = 4_096
31
34
  STARTUP_TIMEOUT_SECONDS = 5
32
- STTY_COMMAND = if File.executable?("/bin/stty")
33
- "/bin/stty"
34
- elsif File.executable?("/usr/bin/stty")
35
- "/usr/bin/stty"
36
- else
37
- "stty"
38
- end.freeze
39
- STATEFUL_COMMANDS = %w[alias cd export pwd unset unalias].freeze
35
+ STATEFUL_COMMANDS = %w[alias cd export pwd unset unalias .].freeze
40
36
 
41
37
  attr_reader :cwd, :last_command, :timeout_seconds
42
38
 
39
+ def exited?
40
+ @native_exited == true
41
+ end
42
+
43
+ def input_prompt_label
44
+ @pending_source ? "Shell ... >" : prompt_label
45
+ end
46
+
47
+ def compose_input(line)
48
+ return line if !@pending_source && line.start_with?("?")
49
+
50
+ source = @pending_source ? "#{@pending_source}\n#{line}" : line
51
+ if incomplete_input?(source)
52
+ @pending_source = source
53
+ nil
54
+ else
55
+ @pending_source = nil
56
+ source
57
+ end
58
+ end
59
+
60
+ def incomplete_input?(source)
61
+ NativeShell::Parser.parse(source.sub(/\A\s*(?:capture|pty)\s+/, ""), aliases: @aliases || {})
62
+ false
63
+ rescue NativeShell::IncompleteInput
64
+ true
65
+ rescue NativeShell::SyntaxError
66
+ false
67
+ end
68
+
43
69
  def initialize(cwd: Dir.pwd, env: ENV.to_h, shell: Kwsh::DEFAULT_SHELL, configured_env: {}, aliases: {}, timeout_seconds: Kwsh::DEFAULT_TIMEOUT_SECONDS, max_output_bytes: Kwsh::DEFAULT_MAX_OUTPUT_BYTES, window_size_provider: nil)
44
70
  @cwd = File.expand_path(cwd.to_s.empty? ? Dir.pwd : cwd.to_s)
45
71
  @base_env = env.to_h.transform_keys(&:to_s).transform_values(&:to_s)
@@ -83,29 +109,39 @@ module Kward
83
109
  return Result.new(output: "", exit_status: 0) if command.empty?
84
110
 
85
111
  expanded_command = expand_alias(command)
86
- words = shell_words(expanded_command)
87
- sync_runtime_state(words)
88
- result = if words.empty?
112
+ native_command = command
113
+ syntax_command = command
114
+ if expanded_command.match?(/\A\s*(?:capture|pty)(?:\s|\z)/)
115
+ syntax_command = expanded_command.sub(/\A\s*(?:capture|pty)\s+/, "")
116
+ end
117
+ tree = NativeShell::Parser.parse(syntax_command, aliases: @aliases)
118
+ words = routing_words(expanded_command, tree)
119
+ result = if tree.values.first.empty?
89
120
  Result.new(output: "", exit_status: 0)
90
121
  elsif words.first == "clear"
91
122
  Result.new(output: "", exit_status: 0, clear: true)
92
123
  elsif %w[exit logout].include?(words.first)
93
- exit_result(command, words)
124
+ execute_command(expanded_command.sub(/\Alogout\b/, "exit"), display_command: command, cancellation: cancellation, &block)
94
125
  elsif words.first == "capture"
95
126
  capture_result(command, expanded_command, cancellation: cancellation, &block)
96
127
  elsif words.first == "pty"
97
128
  interactive_result(command, expanded_command)
98
- elsif %w[source .].include?(words.first)
129
+ elsif words.first == "source"
99
130
  source_result(command, words)
100
- elsif (editor_result = editor_command_result(command))
131
+ elsif words.any? && (editor_result = editor_command_result(command))
101
132
  editor_result
102
133
  elsif stateful_command?(words)
103
- execute_command(expanded_command, display_command: command, cancellation: cancellation, &block)
134
+ execute_command(native_command, display_command: command, cancellation: cancellation, &block)
104
135
  else
105
- Result.new(output: command_echo(command), exit_status: 0, interactive_command: expanded_command)
136
+ Result.new(output: command_echo(command), exit_status: 0, interactive_command: native_command)
106
137
  end
107
138
  remember_result(command, result)
108
139
  result
140
+ rescue NativeShell::SyntaxError
141
+ # Let the interpreter record the failure too, so the next $? is correct.
142
+ result = execute_command(syntax_command, display_command: command, cancellation: cancellation, &block)
143
+ remember_result(command, result)
144
+ result
109
145
  rescue ArgumentError => e
110
146
  result = Result.new(output: "#{command_echo(command)}kwsh: #{e.message}\n", exit_status: 2)
111
147
  remember_result(command, result)
@@ -170,10 +206,16 @@ module Kward
170
206
  end
171
207
 
172
208
  def expand_alias(command, interactive: false)
209
+ token = NativeShell::Lexer.new(command).next_token&.value
210
+ return command unless token.is_a?(NativeShell::Word) && token.literal
173
211
  @routing_shell.expand_alias(command, interactive: interactive)
212
+ rescue NativeShell::SyntaxError
213
+ command
174
214
  end
175
215
 
176
216
  def editor_command_result(command, display_command: command)
217
+ token = NativeShell::Lexer.new(command).next_token&.value
218
+ return unless token.is_a?(NativeShell::Word) && token.literal
177
219
  @routing_shell.editor_command_result(command, display_command: display_command)
178
220
  end
179
221
 
@@ -187,6 +229,7 @@ module Kward
187
229
  @write_mutex.synchronize do
188
230
  @reader&.close unless @reader&.closed?
189
231
  @writer&.close unless @writer&.closed?
232
+ @control&.close unless @control&.closed?
190
233
  end
191
234
  @process_thread&.join(0.5)
192
235
  @process_thread&.kill if @process_thread&.alive?
@@ -229,19 +272,27 @@ module Kward
229
272
  end
230
273
 
231
274
  def start_process
275
+ @protocol_pending = false
232
276
  ready = Queue.new
233
- process_env = @env.merge("PS1" => "", "PS2" => "")
277
+ @control, child_control = UNIXSocket.pair
278
+ @control.sync = true
279
+ worker = File.expand_path("native_worker.rb", __dir__)
234
280
  @process_thread = Thread.new do
235
281
  begin
236
- PTY.spawn(process_env, @shell, "-i", chdir: @cwd) do |reader, writer, pid|
282
+ PTY.spawn(@env, RbConfig.ruby, worker, "3", 3 => child_control, chdir: @cwd, unsetenv_others: true) do |reader, writer, pid|
283
+ child_control.close
237
284
  @reader = reader
238
285
  @writer = writer
239
286
  @pid = pid
240
287
  ready << true
241
- sleep 0.05 until @closed
288
+ until @closed
289
+ sleep 0.01 unless drain_idle_output
290
+ end
242
291
  end
243
292
  rescue StandardError => e
244
293
  ready << e
294
+ ensure
295
+ child_control.close unless child_control.closed?
245
296
  end
246
297
  end
247
298
  started = ready.pop
@@ -249,6 +300,7 @@ module Kward
249
300
 
250
301
  @reader.sync = true
251
302
  @writer.sync = true
303
+ @window_size = nil
252
304
  update_window_size
253
305
  end
254
306
 
@@ -259,12 +311,14 @@ module Kward
259
311
  update_cwd(protocol[:cwd])
260
312
  end
261
313
 
262
- def execute_protocol(command, input: nil, sink: nil, timeout_seconds: nil, max_output_bytes: @max_output_bytes, cancellation: nil, suppress_git_pager: false, tab_action_handler: nil, on_detach: nil)
314
+ def execute_protocol(command, input: nil, sink: nil, timeout_seconds: nil, max_output_bytes: @max_output_bytes, cancellation: nil, suppress_git_pager: false, tab_action_handler: nil, on_detach: nil, config: nil)
263
315
  @command_mutex.synchronize do
264
316
  ensure_open
265
317
  cancellation&.raise_if_cancelled!
266
318
  marker = next_marker
267
- write_protocol_command(command, marker, suppress_git_pager: suppress_git_pager)
319
+ @protocol_pending = true
320
+ update_window_size
321
+ write_protocol_command(command, marker, suppress_git_pager: suppress_git_pager, config: config)
268
322
  read_until_marker(
269
323
  marker,
270
324
  input: input,
@@ -284,48 +338,19 @@ module Kward
284
338
  raise
285
339
  end
286
340
 
287
- def write_protocol_command(command, marker, suppress_git_pager: false)
288
- escaped_command = Shellwords.escape(command.to_s)
289
- pager_setup, pager_restore = git_pager_protocol(marker, suppress: suppress_git_pager)
290
- protocol = <<~SHELL
291
- if true; then
292
- if [ -n "${ZSH_VERSION-}" ]; then unsetopt zle 2>/dev/null; fi
293
- PS1=''
294
- PS2=''
295
- export PS1 PS2
296
- #{STTY_COMMAND} echo
297
- #{pager_setup}
298
- if eval #{escaped_command}; then __kward_status=$?; else __kward_status=$?; fi
299
- #{pager_restore}
300
- __kward_cwd=$(pwd 2>/dev/null)
301
- #{STTY_COMMAND} -echo
302
- printf '#{marker}:%s:%s\\n' "$__kward_status" "$__kward_cwd"
303
- fi
304
- SHELL
305
- write_raw(protocol)
306
- end
307
-
308
- def git_pager_protocol(marker, suppress:)
309
- return ["", ""] unless suppress
310
-
311
- was_set = "#{marker}_GIT_PAGER_WAS_SET"
312
- saved_value = "#{marker}_GIT_PAGER_VALUE"
313
- setup = <<~SHELL
314
- #{was_set}=${GIT_PAGER+x}
315
- #{saved_value}=${GIT_PAGER-}
316
- GIT_PAGER=cat
317
- export GIT_PAGER
318
- SHELL
319
- restore = <<~SHELL
320
- if [ "$#{was_set}" = x ]; then
321
- GIT_PAGER="$#{saved_value}"
322
- export GIT_PAGER
323
- else
324
- unset GIT_PAGER
325
- fi
326
- unset #{was_set} #{saved_value}
327
- SHELL
328
- [setup, restore]
341
+ def write_protocol_command(command, marker, suppress_git_pager: false, config: nil)
342
+ @control.puts(JSON.generate(command: command, marker: marker, cwd: @cwd, aliases: @aliases, suppress_git_pager: suppress_git_pager, config: config))
343
+ end
344
+
345
+ def receive_runtime_state
346
+ state = JSON.parse(@control.gets || raise(IOError, "The native shell control channel closed."))
347
+ @env = state.fetch("env")
348
+ @aliases = state.fetch("aliases")
349
+ @native_exited = state.fetch("exited")
350
+ update_cwd(state.fetch("cwd"))
351
+ @routing_shell = build_routing_shell
352
+ @completion_shell = nil
353
+ @cwd
329
354
  end
330
355
 
331
356
  def read_until_marker(marker, input:, sink:, timeout_seconds:, max_output_bytes:, cancellation:, tab_action_handler: nil, on_detach: nil, initial_buffer: nil)
@@ -379,7 +404,8 @@ module Kward
379
404
  end
380
405
  parsed = extract_marker(buffer, marker)
381
406
  if parsed
382
- before, status, cwd = parsed
407
+ before, status, = parsed
408
+ cwd = receive_runtime_state
383
409
  captured_bytes, truncated = emit_output(
384
410
  before,
385
411
  sink,
@@ -389,6 +415,7 @@ module Kward
389
415
  max_output_bytes
390
416
  )
391
417
  drain_prompt_output
418
+ @protocol_pending = false
392
419
  return {
393
420
  output: captured,
394
421
  status: status,
@@ -414,51 +441,54 @@ module Kward
414
441
  end
415
442
  end
416
443
 
444
+ input_chunk = :wait_readable
417
445
  if input && readable.include?(input)
418
446
  input_chunk = input.read_nonblock(READ_SIZE, exception: false)
419
- if input_chunk.nil?
420
- input = nil
421
- elsif input_chunk != :wait_readable
422
- filtered = tab_action_handler&.call(input_chunk)
423
- input_chunk = filtered[:input].to_s if filtered
424
- unless input_chunk.empty?
425
- notify_input_forwarded(sink)
426
- write_raw(input_chunk)
427
- input_forwarded = true
428
- end
429
- if filtered && filtered[:tab_action]
430
- if on_detach
431
- detached_sink = on_detach.call
432
- detached = detach_protocol(
433
- marker,
434
- sink: detached_sink,
435
- timeout_seconds: timeout_seconds,
436
- max_output_bytes: max_output_bytes,
437
- initial_buffer: buffer,
438
- input_forwarded: input_forwarded
439
- )
440
- raise CommandInterrupted.new(
441
- output: captured,
442
- status: 0,
443
- cwd: @cwd,
444
- input_forwarded: input_forwarded,
445
- tab_action: filtered[:tab_action],
446
- truncated: truncated,
447
- background: detached
448
- )
449
- end
450
-
451
- interrupt_process
452
- raise CommandInterrupted.new(
453
- output: captured,
454
- status: 130,
455
- cwd: @cwd,
456
- input_forwarded: input_forwarded,
457
- tab_action: filtered[:tab_action],
458
- truncated: truncated
459
- )
460
- end
447
+ input = nil if input_chunk.nil?
448
+ end
449
+ filtered = nil
450
+ if input_chunk.nil? || input_chunk == :wait_readable
451
+ input_chunk = tab_action_handler.flush_pending_escape if tab_action_handler.respond_to?(:flush_pending_escape)
452
+ else
453
+ filtered = tab_action_handler&.call(input_chunk)
454
+ input_chunk = filtered[:input].to_s if filtered
455
+ end
456
+ if input_chunk.is_a?(String) && !input_chunk.empty?
457
+ notify_input_forwarded(sink)
458
+ write_raw(input_chunk)
459
+ input_forwarded = true
460
+ end
461
+ if filtered && filtered[:tab_action]
462
+ if on_detach
463
+ detached_sink = on_detach.call
464
+ detached = detach_protocol(
465
+ marker,
466
+ sink: detached_sink,
467
+ timeout_seconds: timeout_seconds,
468
+ max_output_bytes: max_output_bytes,
469
+ initial_buffer: buffer,
470
+ input_forwarded: input_forwarded
471
+ )
472
+ raise CommandInterrupted.new(
473
+ output: captured,
474
+ status: 0,
475
+ cwd: @cwd,
476
+ input_forwarded: input_forwarded,
477
+ tab_action: filtered[:tab_action],
478
+ truncated: truncated,
479
+ background: detached
480
+ )
461
481
  end
482
+
483
+ interrupt_process
484
+ raise CommandInterrupted.new(
485
+ output: captured,
486
+ status: 130,
487
+ cwd: @cwd,
488
+ input_forwarded: input_forwarded,
489
+ tab_action: filtered[:tab_action],
490
+ truncated: truncated
491
+ )
462
492
  end
463
493
  update_window_size
464
494
  end
@@ -512,8 +542,27 @@ module Kward
512
542
  buffer.bytesize - suffix_length
513
543
  end
514
544
 
545
+ # Idle job output must not fill the PTY and block background processes.
546
+ # Never compete with an active reader, including a tab-detached command
547
+ # whose protocol outlives execute_protocol's command mutex ownership.
548
+ def drain_idle_output
549
+ return false unless @command_mutex.try_lock
550
+
551
+ begin
552
+ return false if @protocol_pending || @closed
553
+
554
+ chunk = read_chunk
555
+ !chunk.nil? && chunk != :wait_readable
556
+ ensure
557
+ @command_mutex.unlock
558
+ end
559
+ rescue IOError, Errno::EIO
560
+ false
561
+ end
562
+
515
563
  def drain_prompt_output
516
- loop do
564
+ # A continuously writing job must not prevent returning to the prompt.
565
+ 16.times do
517
566
  readable = IO.select([@reader], nil, nil, 0.01)&.first || []
518
567
  break unless readable.include?(@reader)
519
568
 
@@ -574,7 +623,7 @@ module Kward
574
623
  output << "Exit status: #{status}\n" unless status.zero?
575
624
  update_cwd(protocol[:cwd])
576
625
  block&.call(output)
577
- Result.new(output: output, exit_status: status, streamed: block_given?)
626
+ Result.new(output: output, exit_status: status, streamed: block_given?, exit_shell: exited?)
578
627
  end
579
628
 
580
629
  def source_result(command, words)
@@ -584,19 +633,10 @@ module Kward
584
633
 
585
634
  path = Kwshrc.resolve_path(words[1], cwd: @cwd, env: @env)
586
635
  config = Kwshrc.read_file(path, env: @env)
587
- exports = config[:env]
588
- if exports.any?
589
- assignments = exports.map { |key, value| "#{key}=#{Shellwords.escape(value)}" }
590
- protocol = execute_protocol("export #{assignments.join(" ")}", timeout_seconds: STARTUP_TIMEOUT_SECONDS, max_output_bytes: nil)
591
- unless protocol[:status].to_i.zero?
592
- return Result.new(output: "#{command_echo(command)}kwsh: source: could not apply environment\n", exit_status: 1)
593
- end
636
+ protocol = execute_protocol(":", config: config, timeout_seconds: STARTUP_TIMEOUT_SECONDS, max_output_bytes: nil)
637
+ unless protocol[:status].to_i.zero?
638
+ return Result.new(output: "#{command_echo(command)}kwsh: source: could not apply configuration\n", exit_status: 1)
594
639
  end
595
-
596
- @env.merge!(exports)
597
- @aliases.merge!(config[:aliases])
598
- @routing_shell = build_routing_shell
599
- @completion_shell = nil
600
640
  Result.new(output: command_echo(command), exit_status: 0)
601
641
  rescue Kwshrc::ParseError => e
602
642
  Result.new(output: "#{command_echo(command)}kwsh: source: #{e.message}\n", exit_status: 1)
@@ -626,73 +666,25 @@ module Kward
626
666
  Result.new(output: command_echo(command), exit_status: 0, interactive_command: interactive_command)
627
667
  end
628
668
 
629
- def exit_result(command, words)
630
- if words.length > 2 || (words[1] && !words[1].match?(/\A\d+\z/))
631
- return Result.new(output: "#{command_echo(command)}kwsh: #{words.first}: numeric status expected\n", exit_status: 2)
632
- end
633
-
634
- Result.new(output: command_echo(command), exit_status: words[1].to_i, exit_shell: true)
635
- end
636
-
637
- def sync_runtime_state(words)
638
- changed = sync_runtime_aliases(words) || sync_runtime_environment(words)
639
- return unless changed
640
-
641
- @routing_shell = build_routing_shell
642
- @completion_shell = nil
643
- end
644
-
645
- def sync_runtime_aliases(words)
646
- case words.first
647
- when "alias"
648
- before = @aliases.dup
649
- words.drop(1).each do |assignment|
650
- name, value = assignment.split("=", 2)
651
- @aliases[name] = value.to_s if value && Kwsh.valid_alias_name?(name)
652
- end
653
- before != @aliases
654
- when "unalias"
655
- before = @aliases.dup
656
- if words == ["unalias", "-a"]
657
- @aliases.clear
658
- else
659
- words.drop(1).reject { |name| name.start_with?("-") }.each { |name| @aliases.delete(name) }
660
- end
661
- before != @aliases
662
- else
663
- false
664
- end
665
- end
666
-
667
- def sync_runtime_environment(words)
668
- before = @env.dup
669
- case words.first
670
- when "export"
671
- words.drop(1).reject { |assignment| assignment.start_with?("-") }.each do |assignment|
672
- key, value = assignment.split("=", 2)
673
- @env[key] = value.to_s if value && valid_environment_key?(key)
674
- end
675
- when "unset"
676
- words.drop(1).reject { |key| key == "--" || key.start_with?("-") }.each do |key|
677
- @env.delete(key) if valid_environment_key?(key)
678
- end
679
- else
680
- if words.all? { |word| word.match?(/\A[A-Za-z_][A-Za-z0-9_]*=.*/) }
681
- words.each do |assignment|
682
- key, value = assignment.split("=", 2)
683
- @env[key] = value.to_s if valid_environment_key?(key)
684
- end
685
- end
686
- end
687
- before != @env
688
- end
669
+ # Host-only conveniences apply only to a single simple command. Compound
670
+ # programs always belong to the interpreter, even when they start with cd.
671
+ def routing_words(command, tree)
672
+ return ["capture"] if command.match?(/\A\s*capture(?:\s|\z)/)
673
+ return ["pty"] if command.match?(/\A\s*pty(?:\s|\z)/)
674
+ commands = tree.values.first
675
+ return [] unless commands.length == 1 && commands.first.kind == :pipeline
676
+ stages, negate = commands.first.values
677
+ return [] if negate || stages.length != 1 || stages.first.kind != :simple
678
+ _, redirects = stages.first.values
679
+ return [] unless redirects.empty?
689
680
 
690
- def valid_environment_key?(key)
691
- key.to_s.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/)
681
+ shell_words(command)
682
+ rescue ArgumentError
683
+ []
692
684
  end
693
685
 
694
686
  def stateful_command?(words)
695
- STATEFUL_COMMANDS.include?(words.first) || words.all? { |word| word.match?(/\A[A-Za-z_][A-Za-z0-9_]*=.*/) }
687
+ STATEFUL_COMMANDS.include?(words.first) || words.any? && words.all? { |word| word.match?(/\A[A-Za-z_][A-Za-z0-9_]*=.*/) }
696
688
  end
697
689
 
698
690
  def shell_words(command)
@@ -803,8 +795,8 @@ module Kward
803
795
  size = [rows, columns]
804
796
  return if size == @window_size
805
797
 
806
- @window_size = size
807
798
  @reader.winsize = size
799
+ @window_size = size
808
800
  rescue StandardError
809
801
  nil
810
802
  end
@@ -819,10 +811,12 @@ module Kward
819
811
  def terminate_process_group(pid)
820
812
  return unless pid
821
813
 
822
- signal_process("TERM", -pid) || signal_process("TERM", pid)
814
+ # The private worker's shutdown signal cannot be overridden by script traps.
815
+ # It closes job groups before the bounded hard-kill fallback below.
816
+ signal_process("USR2", pid)
823
817
  deadline = Time.now + 0.2
824
818
  while Time.now < deadline
825
- return unless process_running?(pid)
819
+ break unless process_running?(pid)
826
820
 
827
821
  sleep 0.02
828
822
  end