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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +164 -0
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +6 -2
- data/README.md +5 -5
- data/Rakefile +3 -0
- data/doc/agent-tools.md +24 -1
- data/doc/api.md +20 -2
- data/doc/composer.md +5 -4
- data/doc/configuration.md +86 -14
- data/doc/extensibility.md +4 -1
- data/doc/getting-started.md +3 -3
- data/doc/git.md +4 -2
- data/doc/memory.md +1 -1
- data/doc/pan.md +6 -0
- data/doc/permissions.md +4 -4
- data/doc/plugins.md +504 -20
- data/doc/providers.md +10 -4
- data/doc/rpc.md +279 -23
- data/doc/sandboxing.md +11 -5
- data/doc/security.md +7 -1
- data/doc/session-management.md +41 -5
- data/doc/shell.md +643 -27
- data/doc/tabs.md +8 -3
- data/doc/transports.md +15 -0
- data/doc/troubleshooting.md +1 -1
- data/doc/usage.md +25 -12
- data/doc/workspace-tools.md +9 -0
- data/examples/plugins/openrouter_proxy/README.md +103 -0
- data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
- data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
- data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
- data/examples/plugins/openrouter_proxy/server.rb +186 -0
- data/examples/plugins/space_invaders.rb +1 -2
- data/examples/plugins/stardate_footer.rb +2 -2
- data/examples/plugins/telegram/plugin.rb +1 -1
- data/exe/kward +2 -2
- data/exe/kwsh +7 -0
- data/kward.gemspec +3 -1
- data/lib/kward/agent.rb +66 -19
- data/lib/kward/cli/commands.rb +41 -11
- data/lib/kward/cli/compaction.rb +9 -3
- data/lib/kward/cli/interactive_turn.rb +10 -0
- data/lib/kward/cli/plugins.rb +85 -12
- data/lib/kward/cli/prompt_interface.rb +29 -6
- data/lib/kward/cli/rendering.rb +24 -28
- data/lib/kward/cli/runtime_helpers.rb +168 -56
- data/lib/kward/cli/sessions.rb +9 -5
- data/lib/kward/cli/settings/menus.rb +745 -0
- data/lib/kward/cli/settings/model.rb +316 -0
- data/lib/kward/cli/settings.rb +6 -1055
- data/lib/kward/cli/slash_commands.rb +45 -4
- data/lib/kward/cli/tabs.rb +257 -44
- data/lib/kward/cli/tool_summaries.rb +2 -0
- data/lib/kward/{cli_transcript_formatter.rb → cli/transcript_formatter.rb} +6 -13
- data/lib/kward/cli/worktrees.rb +66 -2
- data/lib/kward/cli.rb +82 -31
- data/lib/kward/compactor.rb +18 -7
- data/lib/kward/config/core.rb +400 -0
- data/lib/kward/config/extensions.rb +99 -0
- data/lib/kward/config/paths.rb +11 -0
- data/lib/kward/config/prompts.rb +313 -0
- data/lib/kward/config/settings.rb +257 -0
- data/lib/kward/config_files.rb +14 -1008
- data/lib/kward/conversation.rb +41 -2
- data/lib/kward/entrypoint.rb +14 -0
- data/lib/kward/events.rb +2 -0
- data/lib/kward/generated_images.rb +237 -0
- data/lib/kward/image_attachments.rb +1 -1
- data/lib/kward/model/catalog.rb +6 -3
- data/lib/kward/model/client.rb +107 -38
- data/lib/kward/model/codex_images.rb +83 -0
- data/lib/kward/model/copilot_models.rb +10 -6
- data/lib/kward/model/model_info.rb +42 -7
- data/lib/kward/{openrouter_model_cache.rb → model/openrouter_model_cache.rb} +7 -5
- data/lib/kward/model/payloads.rb +18 -8
- data/lib/kward/model/sources.rb +3 -1
- data/lib/kward/model/stream_parser.rb +4 -7
- data/lib/kward/model/typesafe_client.rb +78 -0
- data/lib/kward/pan/index.html.erb +8 -1
- data/lib/kward/pan/server.rb +23 -8
- data/lib/kward/permissions/policy.rb +6 -2
- data/lib/kward/plugin_registry.rb +2 -659
- data/lib/kward/plugins/actions.rb +453 -0
- data/lib/kward/plugins/chat_contract.rb +121 -0
- data/lib/kward/{plugin_chat_runtime.rb → plugins/chat_runtime.rb} +62 -18
- data/lib/kward/plugins/codex_responses.rb +73 -0
- data/lib/kward/plugins/host.rb +248 -0
- data/lib/kward/plugins/http_server.rb +83 -0
- data/lib/kward/plugins/registry.rb +1229 -0
- data/lib/kward/plugins/resources.rb +206 -0
- data/lib/kward/plugins/turn_request.rb +36 -0
- data/lib/kward/plugins/ui.rb +219 -0
- data/lib/kward/prompt_interface/composer_controller.rb +0 -1
- data/lib/kward/prompt_interface/composer_renderer.rb +160 -73
- data/lib/kward/prompt_interface/composer_state.rb +1 -1
- data/lib/kward/prompt_interface/editor/controller.rb +2 -2
- data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
- data/lib/kward/prompt_interface/editor/runner.rb +1 -1
- data/lib/kward/prompt_interface/editor/state.rb +1 -1
- data/lib/kward/prompt_interface/file_overlay.rb +3 -3
- data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
- data/lib/kward/prompt_interface/git_prompt.rb +18 -11
- data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
- data/lib/kward/prompt_interface/interactive/state.rb +18 -20
- data/lib/kward/prompt_interface/key_handler.rb +108 -25
- data/lib/kward/prompt_interface/layout.rb +11 -1
- data/lib/kward/prompt_interface/overlay_renderer.rb +26 -9
- data/lib/kward/prompt_interface/plugin_ui_requests.rb +82 -0
- data/lib/kward/prompt_interface/project_browser.rb +27 -54
- data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
- data/lib/kward/prompt_interface/question_prompt.rb +19 -28
- data/lib/kward/prompt_interface/runtime_state.rb +37 -10
- data/lib/kward/prompt_interface/screen.rb +30 -20
- data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
- data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
- data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
- data/lib/kward/prompt_interface.rb +149 -30
- data/lib/kward/prompts/commands.rb +2 -1
- data/lib/kward/prompts.rb +7 -1
- data/lib/kward/{adaptive_pty_output_sink.rb → pty/adaptive_output_sink.rb} +39 -5
- data/lib/kward/{interactive_pty_runner.rb → pty/interactive_runner.rb} +22 -30
- data/lib/kward/{local_command_runner.rb → pty/local_command_runner.rb} +1 -1
- data/lib/kward/{local_pty_command_runner.rb → pty/local_pty_runner.rb} +3 -9
- data/lib/kward/{pty_output_sink.rb → pty/output_sink.rb} +9 -4
- data/lib/kward/pty/transcript_normalizer.rb +137 -0
- data/lib/kward/rpc/config_manager.rb +3 -0
- data/lib/kward/rpc/plugin_chat_manager.rb +30 -10
- data/lib/kward/rpc/prompt_bridge.rb +25 -0
- data/lib/kward/rpc/server.rb +118 -17
- data/lib/kward/rpc/session_manager.rb +208 -60
- data/lib/kward/rpc/session_tree_rows.rb +2 -2
- data/lib/kward/rpc/tool_metadata.rb +1 -1
- data/lib/kward/rpc/transcript_normalizer.rb +11 -7
- data/lib/kward/sandbox/command_runner.rb +1 -1
- data/lib/kward/{session_catalog.rb → sessions/catalog.rb} +1 -1
- data/lib/kward/{session_store.rb → sessions/store.rb} +39 -12
- data/lib/kward/{session_tree_nodes.rb → sessions/tree_nodes.rb} +3 -3
- data/lib/kward/{session_tree_renderer.rb → sessions/tree_renderer.rb} +4 -4
- data/lib/kward/{session_tree_tool_display.rb → sessions/tree_tool_display.rb} +1 -1
- data/lib/kward/{kwsh.rb → shell/kwsh.rb} +30 -10
- data/lib/kward/{kwshrc.rb → shell/kwshrc.rb} +11 -2
- data/lib/kward/shell/native_arithmetic.rb +255 -0
- data/lib/kward/shell/native_arrays.rb +102 -0
- data/lib/kward/shell/native_braces.rb +96 -0
- data/lib/kward/shell/native_builtins.rb +476 -0
- data/lib/kward/shell/native_completion.rb +169 -0
- data/lib/kward/shell/native_condition.rb +75 -0
- data/lib/kward/shell/native_directory.rb +116 -0
- data/lib/kward/shell/native_exec_state.rb +73 -0
- data/lib/kward/shell/native_expansion.rb +252 -0
- data/lib/kward/shell/native_fields.rb +62 -0
- data/lib/kward/shell/native_glob.rb +96 -0
- data/lib/kward/shell/native_history.rb +242 -0
- data/lib/kward/shell/native_interactive.rb +164 -0
- data/lib/kward/shell/native_invocation.rb +42 -0
- data/lib/kward/shell/native_jobs.rb +250 -0
- data/lib/kward/shell/native_line_editor.rb +382 -0
- data/lib/kward/shell/native_line_terminal.rb +286 -0
- data/lib/kward/shell/native_locale.rb +109 -0
- data/lib/kward/shell/native_locals.rb +57 -0
- data/lib/kward/shell/native_pattern.rb +200 -0
- data/lib/kward/shell/native_process.rb +279 -0
- data/lib/kward/shell/native_program.rb +40 -0
- data/lib/kward/shell/native_programmable_completion.rb +115 -0
- data/lib/kward/shell/native_prompt_format.rb +89 -0
- data/lib/kward/shell/native_runtime.rb +964 -0
- data/lib/kward/shell/native_substitution.rb +110 -0
- data/lib/kward/shell/native_syntax.rb +946 -0
- data/lib/kward/shell/native_traps.rb +196 -0
- data/lib/kward/shell/native_vi.rb +407 -0
- data/lib/kward/shell/native_vi_terminal.rb +19 -0
- data/lib/kward/shell/native_worker.rb +82 -0
- data/lib/kward/{persistent_shell_session.rb → shell/persistent_session.rb} +187 -193
- data/lib/kward/shell/prompt.rb +66 -0
- data/lib/kward/shell/prompt_agent.rb +17 -0
- data/lib/kward/{shell_prompt_session.rb → shell/prompt_session.rb} +1 -1
- data/lib/kward/shell/script_runner.rb +244 -0
- data/lib/kward/shell/standalone_agent.rb +31 -0
- data/lib/kward/shell/standalone_link.rb +78 -0
- data/lib/kward/shell/standalone_terminal.rb +287 -0
- data/lib/kward/skills/trust_store.rb +1 -1
- data/lib/kward/tabs/driver.rb +194 -0
- data/lib/kward/{tab_store.rb → tabs/store.rb} +2 -2
- data/lib/kward/{clipboard.rb → terminal/clipboard.rb} +1 -1
- data/lib/kward/{terminal_image_support.rb → terminal/image_support.rb} +1 -1
- data/lib/kward/{terminal_keys.rb → terminal/keys.rb} +22 -1
- data/lib/kward/terminal/locale_text.rb +29 -0
- data/lib/kward/{terminal_sequences.rb → terminal/sequences.rb} +45 -4
- data/lib/kward/tools/base.rb +18 -0
- data/lib/kward/tools/context_for_task.rb +15 -6
- data/lib/kward/tools/datetime.rb +85 -0
- data/lib/kward/tools/edit_file.rb +9 -6
- data/lib/kward/tools/generate_image.rb +36 -0
- data/lib/kward/tools/git_commit.rb +13 -7
- data/lib/kward/tools/list_directory.rb +4 -4
- data/lib/kward/tools/plugin_tool.rb +41 -0
- data/lib/kward/tools/prepare_shell_command.rb +1 -1
- data/lib/kward/tools/read_file.rb +7 -6
- data/lib/kward/tools/registry.rb +130 -21
- data/lib/kward/tools/run_shell_command.rb +10 -8
- data/lib/kward/tools/search/code.rb +1 -1
- data/lib/kward/tools/summarize_file_structure.rb +5 -5
- data/lib/kward/tools/tool_call.rb +4 -1
- data/lib/kward/tools/typesafe_evaluate.rb +81 -0
- data/lib/kward/tools/workspace_targets.rb +58 -0
- data/lib/kward/tools/write_file.rb +9 -6
- data/lib/kward/{export_path.rb → transcripts/export_path.rb} +1 -1
- data/lib/kward/{markdown_transcript.rb → transcripts/markdown_transcript.rb} +10 -2
- data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
- data/lib/kward/transport/contracts.rb +200 -0
- data/lib/kward/transport/gateway.rb +81 -36
- data/lib/kward/transport/plugin_chat_gateway.rb +3 -2
- data/lib/kward/transport.rb +1 -200
- data/lib/kward/turn_summary.rb +68 -0
- data/lib/kward/version.rb +1 -1
- data/lib/kward/{workspace_factory.rb → workspace/factory.rb} +2 -2
- data/lib/kward/{git_worktree_manager.rb → workspace/git_worktree_manager.rb} +28 -0
- data/lib/kward/{workspace.rb → workspace/workspace.rb} +3 -3
- data/lib/main.rb +6 -3
- data/templates/default/fulldoc/html/css/kward.css +125 -0
- data/templates/default/fulldoc/html/images/kward_screen_1.png +0 -0
- data/templates/default/fulldoc/html/setup.rb +1 -1
- data/templates/default/layout/html/layout.erb +16 -4
- metadata +150 -48
- data/lib/kward/pty_transcript_normalizer.rb +0 -93
- data/lib/kward/shell_prompt.rb +0 -52
- data/lib/kward/tab_driver.rb +0 -90
- data/templates/default/fulldoc/html/images/kward_workflow.svg +0 -52
- /data/lib/kward/{editor_prompt.rb → cli/editor_prompt.rb} +0 -0
- /data/lib/kward/{editor_prompt_session.rb → cli/editor_prompt_session.rb} +0 -0
- /data/lib/kward/{diff_view_mode.rb → prompt_interface/editor/diff_view_mode.rb} +0 -0
- /data/lib/kward/{editor_mode.rb → prompt_interface/editor/editor_mode.rb} +0 -0
- /data/lib/kward/{markdown_code_block.rb → prompt_interface/editor/markdown_code_block.rb} +0 -0
- /data/lib/kward/{scratchpad_languages.rb → prompt_interface/editor/scratchpad_languages.rb} +0 -0
- /data/lib/kward/{scratchpad_runner.rb → prompt_interface/editor/scratchpad_runner.rb} +0 -0
- /data/lib/kward/{detached_run.rb → pty/detached_run.rb} +0 -0
- /data/lib/kward/{session_diff.rb → sessions/diff.rb} +0 -0
- /data/lib/kward/{session_naming.rb → sessions/naming.rb} +0 -0
- /data/lib/kward/{session_trash.rb → sessions/trash.rb} +0 -0
- /data/lib/kward/{ansi.rb → terminal/ansi.rb} +0 -0
- /data/lib/kward/{terminal_text.rb → terminal/text.rb} +0 -0
- /data/lib/kward/{transcript_export.rb → transcripts/transcript_export.rb} +0 -0
- /data/lib/kward/{project_files.rb → workspace/files.rb} +0 -0
- /data/lib/kward/{path_guard.rb → workspace/path_guard.rb} +0 -0
|
@@ -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
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
module Kward
|
|
2
|
+
module NativeShell
|
|
3
|
+
# Bash's small programmable-completion subset used by rbenv. The callback
|
|
4
|
+
# is a trusted, explicitly registered function; the draft is data only.
|
|
5
|
+
module ProgrammableCompletion
|
|
6
|
+
COMPLETION_TIMEOUT = 2.0
|
|
7
|
+
COMPLETION_BYTES = 64 * 1024
|
|
8
|
+
COMPLETION_COUNT = 256
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def complete_builtin(args)
|
|
13
|
+
flag, function, *commands = args
|
|
14
|
+
commands.shift if commands.first == "--"
|
|
15
|
+
unless flag == "-F" && function&.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/) && !commands.empty? && commands.none?(&:empty?)
|
|
16
|
+
raise ExecutionError, "complete: expected -F function command..."
|
|
17
|
+
end
|
|
18
|
+
commands.each { |command| @completion_functions[command] = function }
|
|
19
|
+
0
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def compgen_builtin(args, streams)
|
|
23
|
+
flag, list, *rest = args
|
|
24
|
+
rest.shift if rest.first == "--"
|
|
25
|
+
raise ExecutionError, "compgen: expected -W wordlist [--] [prefix]" unless flag == "-W" && list && rest.size <= 1
|
|
26
|
+
|
|
27
|
+
prefix = rest.first.to_s
|
|
28
|
+
matches = Shellwords.split(list).select { |word| word.start_with?(prefix) }.uniq
|
|
29
|
+
matches.each { |word| write_line(streams, word) }
|
|
30
|
+
matches.empty? ? 1 : 0
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def programmed_candidates(words, cancel:)
|
|
34
|
+
function = @completion_functions[words.first]
|
|
35
|
+
return unless @functions.key?(function)
|
|
36
|
+
|
|
37
|
+
reader, writer = IO.pipe
|
|
38
|
+
pid = Process.fork do
|
|
39
|
+
Process.setsid
|
|
40
|
+
reader.close
|
|
41
|
+
File.open(File::NULL, "r+") do |null|
|
|
42
|
+
[STDIN, STDOUT, STDERR].each { |io| io.reopen(null) }
|
|
43
|
+
writer.write(completion_reply(function, words, null))
|
|
44
|
+
end
|
|
45
|
+
exit!(0)
|
|
46
|
+
rescue StandardError, Interrupt
|
|
47
|
+
exit!(1)
|
|
48
|
+
end
|
|
49
|
+
writer.close
|
|
50
|
+
read_completion(reader, cancel).split("\0").map do |word|
|
|
51
|
+
word.force_encoding(words.last.encoding)
|
|
52
|
+
end
|
|
53
|
+
rescue IOError, SystemCallError
|
|
54
|
+
[]
|
|
55
|
+
ensure
|
|
56
|
+
reader&.close unless reader&.closed?
|
|
57
|
+
writer&.close unless writer&.closed?
|
|
58
|
+
if pid
|
|
59
|
+
# Keep the pid reserved until both the worker and its ordinary child
|
|
60
|
+
# processes have been signaled, including when cancellation races setsid.
|
|
61
|
+
Process.kill("KILL", pid) rescue nil
|
|
62
|
+
Process.kill("KILL", -pid) rescue nil
|
|
63
|
+
Process.waitpid(pid) rescue nil
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def completion_reply(function, words, null)
|
|
68
|
+
reset_child_state(job_group: Process.getpgrp)
|
|
69
|
+
@process_owned = true
|
|
70
|
+
@replace_process = false
|
|
71
|
+
@base_streams = { 0 => null, 1 => null, 2 => null }
|
|
72
|
+
@persistent_descriptors = {}
|
|
73
|
+
%w[COMP_WORDS COMP_CWORD COMPREPLY].each do |name|
|
|
74
|
+
@variables.delete(name)
|
|
75
|
+
@arrays.delete(name)
|
|
76
|
+
@readonly.delete(name)
|
|
77
|
+
@exported.delete(name)
|
|
78
|
+
end
|
|
79
|
+
@variables["COMP_CWORD"] = (words.length - 1).to_s
|
|
80
|
+
@arrays["COMP_WORDS"] = Arrays::ArrayValue.new(associative: false, values: words.each_with_index.to_h { |word, index| [index, word] })
|
|
81
|
+
@arrays["COMPREPLY"] = Arrays::ArrayValue.new(associative: false, values: {})
|
|
82
|
+
call_function(function, [words.first, words.last, words[-2].to_s], @base_streams)
|
|
83
|
+
reply = @arrays["COMPREPLY"]
|
|
84
|
+
buffer = +"".b
|
|
85
|
+
count = 0
|
|
86
|
+
reply&.values&.each_value do |value|
|
|
87
|
+
next if value.empty? || value.include?("\0")
|
|
88
|
+
break if count >= COMPLETION_COUNT || buffer.bytesize + value.bytesize + 1 > COMPLETION_BYTES
|
|
89
|
+
|
|
90
|
+
buffer << value.b << "\0"
|
|
91
|
+
count += 1
|
|
92
|
+
end
|
|
93
|
+
buffer
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def read_completion(reader, cancel)
|
|
97
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + COMPLETION_TIMEOUT
|
|
98
|
+
buffer = +"".b
|
|
99
|
+
loop do
|
|
100
|
+
return "" if cancel&.call
|
|
101
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
102
|
+
return "" unless remaining.positive?
|
|
103
|
+
next unless IO.select([reader], nil, nil, [remaining, 0.025].min)
|
|
104
|
+
|
|
105
|
+
chunk = reader.read_nonblock(8192, exception: false)
|
|
106
|
+
return buffer if chunk.nil?
|
|
107
|
+
next if chunk == :wait_readable
|
|
108
|
+
|
|
109
|
+
buffer << chunk
|
|
110
|
+
return "" if buffer.bytesize > COMPLETION_BYTES
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
require "etc"
|
|
2
|
+
require_relative "../terminal/locale_text"
|
|
3
|
+
require_relative "../terminal/sequences"
|
|
4
|
+
|
|
5
|
+
module Kward
|
|
6
|
+
module NativeShell
|
|
7
|
+
# Expand only known prompt escapes. Metadata is text, never format source
|
|
8
|
+
# or terminal control data. Only styles generated here occupy zero cells.
|
|
9
|
+
class PromptFormat
|
|
10
|
+
ESCAPES = /(%(?:[FK]\{[^{}%]*\}|1~|[~d\/nm?#%fkbB]))/n.freeze
|
|
11
|
+
COLORS = %w[black red green yellow blue magenta cyan white].freeze
|
|
12
|
+
STYLES = { "%f" => [39], "%k" => [49], "%B" => [1], "%b" => [22] }.freeze
|
|
13
|
+
|
|
14
|
+
def initialize(template, directory:, home:, status:, color:, prefix: "")
|
|
15
|
+
@directory = directory.b
|
|
16
|
+
@home = home.to_s.b
|
|
17
|
+
@status = status
|
|
18
|
+
@parts = template.b.split(ESCAPES).filter_map do |part|
|
|
19
|
+
style = STYLES[part] || color_style(part)
|
|
20
|
+
style ? (style if color) : expand(part)
|
|
21
|
+
end
|
|
22
|
+
@parts.unshift(prefix.b) unless prefix.empty?
|
|
23
|
+
@styled = @parts.any? { |part| part.is_a?(Array) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def cells(encoding)
|
|
27
|
+
@parts.flat_map do |part|
|
|
28
|
+
if part.is_a?(Array)
|
|
29
|
+
[[TerminalSequences.sgr(*part), 0]]
|
|
30
|
+
else
|
|
31
|
+
TerminalText.locale_cells(part.b.force_encoding(encoding).scrub)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def render(cells)
|
|
37
|
+
text = cells.map(&:first).join
|
|
38
|
+
@styled ? text + TerminalSequences::SGR_RESET : text
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def expand(part)
|
|
44
|
+
case part
|
|
45
|
+
when "%%" then "%"
|
|
46
|
+
when "%d", "%/" then @directory
|
|
47
|
+
when "%~" then abbreviated_directory
|
|
48
|
+
when "%1~" then File.basename(abbreviated_directory)
|
|
49
|
+
when "%n" then username
|
|
50
|
+
when "%m" then Etc.uname.fetch(:nodename).split(".", 2).first.to_s
|
|
51
|
+
when "%?" then @status.to_s
|
|
52
|
+
when "%#" then Process.euid.zero? ? "#" : "%"
|
|
53
|
+
else part
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def abbreviated_directory
|
|
58
|
+
return @directory unless @home.start_with?("/")
|
|
59
|
+
|
|
60
|
+
home = @home.sub(%r{/+\z}, "")
|
|
61
|
+
return "~" if @directory == (home.empty? ? "/" : home)
|
|
62
|
+
return @directory unless @directory.start_with?("#{home}/")
|
|
63
|
+
|
|
64
|
+
"~#{@directory.delete_prefix(home)}"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def username
|
|
68
|
+
Etc.getpwuid(Process.euid).name
|
|
69
|
+
rescue ArgumentError
|
|
70
|
+
Process.euid.to_s
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def color_style(part)
|
|
74
|
+
match = /\A%([FK])\{([^{}]*)\}\z/n.match(part)
|
|
75
|
+
return unless match
|
|
76
|
+
|
|
77
|
+
foreground = match[1] == "F"
|
|
78
|
+
specification = match[2]
|
|
79
|
+
if (index = COLORS.index(specification))
|
|
80
|
+
[(foreground ? 30 : 40) + index]
|
|
81
|
+
elsif specification.match?(/\A\d{1,3}\z/) && (index = specification.to_i) <= 255
|
|
82
|
+
[foreground ? 38 : 48, 5, index]
|
|
83
|
+
elsif specification.match?(/\A#[0-9a-fA-F]{6}\z/)
|
|
84
|
+
[foreground ? 38 : 48, 2, *specification[1..].scan(/../).map(&:hex)]
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|