revund-ruby-worker 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe8acdfb8380e8eccf8ae7aaefec9511a8093c1784cd466d634b639b4e2ae451
4
+ data.tar.gz: 190b547da8748b8365f2783948a638291f0dbfc0482f2fa22d77cc80fcac20f3
5
+ SHA512:
6
+ metadata.gz: 37deb1f281054a0bd187801fef50f7d75664b5043ce8f185dbd1355198d470267cc971fac442cc6387a9f7bebbb87aa1a507734e194378804b10e966a1b5da3e
7
+ data.tar.gz: cc5307d080f97ddb361b02bcce78a24545e23c75f101992f7d7e831eb6f12dbc67f61babb5a58126a6df5e198d4811bf013935d477bcb3f59de3003b49510a05
data/LICENSE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in one of the
111
+ following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Support. While redistributing the Work or
166
+ Derivative Works thereof, You may choose to offer, and charge a
167
+ fee for, acceptance of support, warranty, indemnity, or other
168
+ liability obligations and/or rights consistent with this License.
169
+ However, in accepting such obligations, You may act only on Your
170
+ own behalf and on Your sole responsibility, not on behalf of any
171
+ other Contributor, and only if You agree to indemnify, defend,
172
+ and hold each Contributor harmless for any liability incurred by,
173
+ or claims asserted against, such Contributor by reason of your
174
+ accepting any such warranty or support.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Revund
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # revund-ruby-worker
2
+
3
+ The Ruby AST sidecar for [Revund](https://revund.dev). A gRPC server that
4
+ implements `revund.worker.v1.Worker` — the universal contract every
5
+ Revund worker speaks. Built on
6
+ [whitequark/parser](https://github.com/whitequark/parser), the same library
7
+ RuboCop, Sorbet, and Brakeman use.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ gem install revund-ruby-worker
13
+ ```
14
+
15
+ You also need the [`revund`](https://revund.dev/install) CLI itself. The CLI
16
+ discovers `revund-ruby-worker` on `PATH` and spawns it on demand.
17
+
18
+ ## Usage
19
+
20
+ The worker is normally launched by the Revund CLI. To run it standalone:
21
+
22
+ ```bash
23
+ revund-ruby-worker # binds 0.0.0.0:50053
24
+ REVUND_RUBY_WORKER_PORT=0 revund-ruby-worker # OS-assigned port; prints "ready: 0.0.0.0:<port>" on stdout
25
+ ```
26
+
27
+ Point the CLI at it via the `REVUND_WORKERS` env var (plain
28
+ `host:port` — the CLI calls `Describe` to learn the worker's
29
+ languages and capabilities):
30
+
31
+ ```bash
32
+ REVUND_WORKERS=localhost:50053 revund review
33
+ ```
34
+
35
+ ## What it does
36
+
37
+ The worker advertises one capability via the `Describe` RPC:
38
+
39
+ | Capability | RPC | Purpose |
40
+ |---|---|---|
41
+ | `parse` | `Parse` | Returns the universal `ParsedFile` shape — requires, top-level decls (`class` / `module` / `def` / `defs` / `casgn`), method bodies (with cyclomatic complexity, hash, canonical hash, blocks), and concern evidence (Presentation/State/Network/IO/Config/Business/DataAccess/Transport). |
42
+
43
+ `ResolveSymbols` and `RunDiagnostics` are not implemented yet — the Revund CLI checks the capability list from `Describe` and skips RPCs the worker hasn't advertised.
44
+
45
+ ## Environment
46
+
47
+ | Variable | Default | Purpose |
48
+ |---|---|---|
49
+ | `REVUND_RUBY_WORKER_PORT` | `50053` | Bind port. Use `0` for OS-assigned (recommended when the CLI spawns the worker). |
50
+
51
+ ## Contract
52
+
53
+ The wire contract is defined in
54
+ [`proto/worker/v1/worker.proto`](./proto/worker/v1/worker.proto), vendored
55
+ inside the gem. The canonical source lives at
56
+ [`revund-dev/revund-workers/proto/worker/v1/worker.proto`](https://github.com/revund-dev/revund-workers/blob/main/proto/worker/v1/worker.proto).
57
+
58
+ ## License
59
+
60
+ Apache-2.0
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # revund-ruby-worker — entrypoint installed by RubyGems.
5
+ #
6
+ # Reads PORT from REVUND_RUBY_WORKER_PORT (default 50053).
7
+ #
8
+ # Prints "ready: 0.0.0.0:<port>" on stdout once the gRPC
9
+ # server has bound — the Revund CLI greps for that line
10
+ # to know when to start sending RPCs.
11
+
12
+ $LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
13
+
14
+ require 'ruby_worker/server'
15
+
16
+ port = (ENV['REVUND_RUBY_WORKER_PORT'] || '50053').to_i
17
+ RubyWorker::Server.new(port).run
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+ require 'fileutils'
5
+ require 'open3'
6
+
7
+ module RubyWorker
8
+ # Fetcher clones the repo into a local cache directory when
9
+ # the worker is dispatched in self-fetch mode (RepoSource on
10
+ # the request). Returns the absolute path to the cached
11
+ # checkout so the rest of the worker (Parser) can operate
12
+ # against it exactly as if the bot had cloned it.
13
+ #
14
+ # # Cache layout
15
+ #
16
+ # $REVUND_WORKER_CACHE_DIR/<sha256(url@ref)>/
17
+ #
18
+ # Default cache dir is /var/cache/revund-worker. The hash key
19
+ # includes both URL and ref so two reviews targeting different
20
+ # commits of the same repo share nothing — keeps tenant
21
+ # blast-radius to one cache entry.
22
+ #
23
+ # # Token hygiene (security)
24
+ #
25
+ # The token is used at clone time only:
26
+ #
27
+ # 1. Compose the authenticated URL via x-access-token convention.
28
+ # 2. Run `git clone --filter=blob:none --no-checkout <auth-url>`.
29
+ # 3. Immediately rewrite the remote URL to the un-authenticated
30
+ # form via `git remote set-url`. After this step the on-disk
31
+ # .git/config carries no token.
32
+ # 4. Fetch the requested ref and check it out.
33
+ #
34
+ # Errors and log messages NEVER include the URL with the embedded
35
+ # token; the sanitizer strips it before raising.
36
+ module Fetcher
37
+ DEFAULT_CACHE_DIR = '/var/cache/revund-worker'
38
+ DEFAULT_IDLE_TTL_SEC = 10 * 60 # 10 min
39
+
40
+ @last_touched = {}
41
+
42
+ # Resolve the local checkout for `src` (a Hash with :url,
43
+ # :ref, :auth_token, :auth_user). Clones if cold, returns
44
+ # the cached path if warm. Idempotent within the process
45
+ # lifetime.
46
+ def self.fetch_or_cache(src)
47
+ url = src[:url].to_s
48
+ ref = src[:ref].to_s
49
+ raise 'fetcher: repo_source.url is required' if url.empty?
50
+ raise 'fetcher: repo_source.ref is required' if ref.empty?
51
+
52
+ cache_dir = ENV['REVUND_WORKER_CACHE_DIR'] || DEFAULT_CACHE_DIR
53
+ FileUtils.mkdir_p(cache_dir)
54
+
55
+ key = cache_key(url, ref)
56
+ repo_dir = File.join(cache_dir, key)
57
+
58
+ if File.directory?(File.join(repo_dir, '.git'))
59
+ touch(repo_dir)
60
+ return repo_dir
61
+ end
62
+
63
+ clean_url = url
64
+ auth_url = inject_token(clean_url, src[:auth_token].to_s, src[:auth_user].to_s)
65
+ FileUtils.mkdir_p(repo_dir)
66
+
67
+ run('git', 'clone', '--filter=blob:none', '--no-checkout', auth_url, repo_dir)
68
+ # Strip the token BEFORE doing anything else.
69
+ run('git', '-C', repo_dir, 'remote', 'set-url', 'origin', clean_url)
70
+
71
+ run('git', '-C', repo_dir, 'fetch', 'origin', ref)
72
+ run('git', '-C', repo_dir, 'checkout', ref)
73
+
74
+ touch(repo_dir)
75
+ Thread.new { evict_idle(cache_dir) }
76
+
77
+ repo_dir
78
+ end
79
+
80
+ class << self
81
+ private
82
+
83
+ def cache_key(url, ref)
84
+ Digest::SHA256.hexdigest("#{url}@#{ref}")[0, 32]
85
+ end
86
+
87
+ def touch(dir)
88
+ @last_touched[dir] = (Time.now.to_f * 1000).to_i
89
+ end
90
+
91
+ def evict_idle(cache_dir)
92
+ ttl_sec = ENV['REVUND_WORKER_CACHE_TTL_SEC'].to_i
93
+ ttl_ms = ttl_sec.positive? ? ttl_sec * 1000 : DEFAULT_IDLE_TTL_SEC * 1000
94
+ cutoff = (Time.now.to_f * 1000).to_i - ttl_ms
95
+
96
+ Dir.children(cache_dir).each do |e|
97
+ full = File.join(cache_dir, e)
98
+ t = @last_touched[full] || begin
99
+ File.mtime(full).to_f * 1000
100
+ rescue StandardError
101
+ (Time.now.to_f * 1000).to_i
102
+ end
103
+ next if t > cutoff
104
+
105
+ begin
106
+ FileUtils.rm_rf(full)
107
+ @last_touched.delete(full)
108
+ rescue StandardError
109
+ # Best-effort; stale entries get retried next sweep.
110
+ end
111
+ end
112
+ end
113
+
114
+ # Compose the authenticated clone URL by inserting a
115
+ # basic-auth pair into the https URL. Username comes from
116
+ # RepoSource.auth_user; when empty defaults to
117
+ # "x-access-token" (GitHub). GitLab → "oauth2",
118
+ # Bitbucket → "x-token-auth".
119
+ def inject_token(clone_url, token, user = '')
120
+ return clone_url if token.empty?
121
+ return clone_url unless clone_url.start_with?('https://')
122
+
123
+ u = user.empty? ? 'x-access-token' : user
124
+ "https://#{u}:#{token}@" + clone_url.sub(%r{\Ahttps://}, '')
125
+ end
126
+
127
+ # Run a command, capture stderr, raise sanitized error on
128
+ # non-zero exit. The raised message NEVER contains an
129
+ # authenticated URL.
130
+ def run(*cmd)
131
+ _stdout, stderr, status = Open3.capture3(*cmd)
132
+ return if status.success?
133
+
134
+ sanitized = cmd.map { |a| looks_authenticated?(a) ? redact(a) : a }
135
+ raise "fetcher: #{cmd.first} #{sanitized.drop(1).join(' ')} exited #{status.exitstatus}: #{redact(stderr)}"
136
+ end
137
+
138
+ def looks_authenticated?(arg)
139
+ arg.is_a?(String) && arg =~ %r{\Ahttps?://[^/@]+:[^/@]+@}
140
+ end
141
+
142
+ def redact(str)
143
+ str.gsub(%r{(https?://)[^/@\s]+:[^/@\s]+@}, '\1[redacted]@')
144
+ end
145
+ end
146
+ end
147
+ end