yatfa 1.0.96 → 1.0.98

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.
package/README.md CHANGED
@@ -79,9 +79,9 @@ Example `yatfa.env.rb`:
79
79
  | Variable | Required | Description |
80
80
  |----------|----------|-------------|
81
81
  | `AGENT_TYPE` | ✅ | `worker`, `planner`, `reviewer`, or `researcher` |
82
- | `YATFA_URL` | ✅ | YATFA platform URL (WebSocket and API URLs are derived from this) |
83
- | `YATFA_API_KEY` | | MCP API key (get from YATFA dashboard) |
84
- | `GH_TOKEN` | | GitHub token for git operations |
82
+ | `YATFA_API_KEY` | ✅ | MCP API key (get from YATFA dashboard) |
83
+ | `YATFA_URL` | | Yatfa platform URL (defaults to `https://yatfa.com`; `YATFA_API_URL` & `YATFA_WS_URL` are derived from this) |
84
+ | `GH_TOKEN` | | GitHub token for git operations (fallback; backend token auth is preferred) |
85
85
 
86
86
  ## Agent Types
87
87
 
@@ -199,7 +199,7 @@ YATFA_API_KEY=your_api_key_here
199
199
  AGENT_TYPE=worker
200
200
  ```
201
201
 
202
- Note: `YATFA_API_URL` and `YATFA_WS_URL` are derived from `YATFA_URL` by `setup-agent.rb`. Project is automatically determined by the API key authentication.
202
+ Note: `YATFA_URL` is optional and defaults to `https://yatfa.com` (the hosted deployment works with zero URL configuration; self-hosted installs set it to their server). `YATFA_API_URL` (`/api/v1`) and `YATFA_WS_URL` (`/cable`, scheme-adjusted) are always derived from it — you never need to set them. The git credential helper derives `YATFA_API_URL` itself, so even a bare `docker exec` shell with no other env can authenticate. Project is automatically determined by the API key.
203
203
 
204
204
  ### Agent Self-Update
205
205
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require_relative "../../setup/url_helper"
4
5
 
5
6
  module YatfaAgent
6
7
  # Docker command assembly.
@@ -68,10 +69,12 @@ module YatfaAgent
68
69
  docker_cmd += ["-e", "OPENCLAW_TELEGRAM_BOT_TOKEN=#{agent_config['telegram_bot_token']}"]
69
70
  end
70
71
 
71
- # Pass YATFA_URL if configured (optional for claw)
72
+ # Pass YATFA_URL if configured (optional for claw). The API URL is
73
+ # derived via the shared UrlHelper so an explicit override is honored
74
+ # and the /api/v1 suffix rule stays in one place (YATFA-3018).
72
75
  if config["yatfa_url"]
73
76
  docker_cmd += ["-e", "YATFA_URL=#{config['yatfa_url']}"]
74
- docker_cmd += ["-e", "YATFA_API_URL=#{config['yatfa_url']}/api/v1"]
77
+ docker_cmd += ["-e", "YATFA_API_URL=#{YatfaAgent::UrlHelper.api_url(config)}"]
75
78
  end
76
79
 
77
80
  # Pass MCP API key if configured (for future MCP integration)
@@ -80,14 +83,29 @@ module YatfaAgent
80
83
  end
81
84
 
82
85
  else
83
- # Standard agent setup (worker, planner, reviewer, researcher)
86
+ # Standard agent setup (worker, planner, reviewer, researcher).
87
+ # Inject all three URLs as container-wide -e env (matching the claw
88
+ # branch) so any shell in the container — including `docker exec` shells
89
+ # that are not children of the setup process — sees a consistent
90
+ # YATFA_API_URL. YATFA_URL defaults to https://yatfa.com; API/WS URLs are
91
+ # derived from it via the shared UrlHelper (YATFA-3018). Previously only
92
+ # YATFA_URL + YATFA_API_KEY were injected here, leaving YATFA_API_URL
93
+ # empty outside the setup process and breaking git auth.
94
+ #
95
+ # An operator may override any URL via the env block; those overrides
96
+ # are already injected above as -e flags, so skip them here to avoid
97
+ # clobbering (the last -e flag for a key wins in docker). Derivation
98
+ # uses the effective source so env-block YATFA_URL flows into API/WS.
99
+ url_source = config.merge(merged_env)
84
100
  docker_cmd += [
85
101
  "-e", "YATFA_VERSION=main",
86
102
  "-e", "SKILLS=#{agent_def[:skills]&.join(',')}",
87
103
  "-e", "AGENT_TYPE=#{agent_type}",
88
- "-e", "YATFA_URL=#{YatfaAgent::Config.yatfa_url(config)}",
89
104
  "-e", "YATFA_API_KEY=#{agent_config['mcp_api_key']}"
90
105
  ]
106
+ docker_cmd += ["-e", "YATFA_URL=#{YatfaAgent::UrlHelper.base_url(url_source)}"] unless merged_env["YATFA_URL"]
107
+ docker_cmd += ["-e", "YATFA_API_URL=#{YatfaAgent::UrlHelper.api_url(url_source)}"] unless merged_env["YATFA_API_URL"]
108
+ docker_cmd += ["-e", "YATFA_WS_URL=#{YatfaAgent::UrlHelper.ws_url(url_source)}"] unless merged_env["YATFA_WS_URL"]
91
109
 
92
110
  add_github_auth!(docker_cmd, config)
93
111
  add_git_config!(docker_cmd, config)
@@ -3,6 +3,11 @@
3
3
  require "json"
4
4
  require "digest"
5
5
  require_relative "http"
6
+ # Single source of truth for URL→{api,ws} derivation + the hosted default.
7
+ # Loaded here (not duplicated) so the host-side launcher routes ALL URL
8
+ # derivation through YatfaAgent::UrlHelper — the same module the in-container
9
+ # setup scripts use. See YATFA-3018.
10
+ require_relative "../../setup/url_helper"
6
11
 
7
12
  module YatfaAgent
8
13
  module Config
@@ -88,27 +93,6 @@ module YatfaAgent
88
93
  JSON.parse(JSON.generate(config))
89
94
  end
90
95
 
91
- def self.yatfa_url(config)
92
- config["yatfa_url"] || config["rails_api_url"]
93
- end
94
-
95
- def self.api_url(config)
96
- # Derive API URL from yatfa_url
97
- yatfa_base = yatfa_url(config)
98
-
99
- # If yatfa_url already includes /api/v1, return as-is
100
- return yatfa_base if yatfa_base.end_with?("/api/v1") || yatfa_base.end_with?("/api/")
101
-
102
- # Otherwise, assume it's the base URL and append /api/v1
103
- "#{yatfa_base}/api/v1"
104
- end
105
-
106
- def self.ws_url(config)
107
- # Derive WebSocket URL from yatfa_url by replacing protocol and adding /cable endpoint
108
- api_base = yatfa_url(config)
109
- "#{api_base.sub(/^https:\/\//, "wss://").sub(/^http:\/\//, "ws://")}/cable"
110
- end
111
-
112
96
  # Extract the first available agent MCP API key from the config.
113
97
  # Returns nil when no key is configured.
114
98
  def self.agent_api_key(config)
@@ -123,8 +107,11 @@ module YatfaAgent
123
107
  end
124
108
 
125
109
  def self.fetch_repositories!(config)
126
- # Support both old and new config keys
127
- api_base = api_url(config)
110
+ # Derive the API URL through the shared single source of truth
111
+ # (YatfaAgent::UrlHelper) so this host-side path honors an explicit
112
+ # YATFA_API_URL override and the hosted default exactly like the
113
+ # launcher and in-container setup scripts. See YATFA-3018.
114
+ api_base = YatfaAgent::UrlHelper.api_url(config)
128
115
 
129
116
  return unless api_base
130
117
 
@@ -49,7 +49,12 @@ module YatfaAgent
49
49
  # be identified, the API is unreachable, or no such config is stored.
50
50
  # Never raises — a missing remote config is a normal "fall back to error" case.
51
51
  def self.fetch_remote_dockerfile(config)
52
- api_base = Config.api_url(config)
52
+ # Derive the API URL through the shared single source of truth
53
+ # (YatfaAgent::UrlHelper) rather than the legacy Config.api_url, which
54
+ # neither honored an explicit YATFA_API_URL override nor applied the
55
+ # hosted default and could raise NoMethodError on an unconfigured URL.
56
+ # YatfaAgent::UrlHelper is loaded transitively via config.rb. See YATFA-3018.
57
+ api_base = YatfaAgent::UrlHelper.api_url(config)
53
58
  project_id = config["project_id"]
54
59
  api_key = Config.agent_api_key(config)
55
60
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yatfa",
3
- "version": "1.0.96",
3
+ "version": "1.0.98",
4
4
  "description": "YATFA - Yet Another Tool For Agents",
5
5
  "bin": "./bin/run-yatfa-agent.rb",
6
6
  "files": [
@@ -16,4 +16,4 @@
16
16
  },
17
17
  "author": "",
18
18
  "license": "ISC"
19
- }
19
+ }
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # Handles:
7
7
  # - Architecture detection and multi-strategy bridge download
8
- # (local override → Civo S3 presigned URL GitHub fallback)
8
+ # (local override → mounted binary public bucket download)
9
9
  # - tmux binary download and INSIDE_TMUX=1 patching
10
10
  # - Generic file download utility (download_file)
11
11
  # - Helper script installation to /usr/local/bin
@@ -58,22 +58,12 @@ def download_agent_bridge!
58
58
  return download_agent_bridge_tmux!(target_dir)
59
59
  end
60
60
 
61
- # 3. Try Civo bucket download (production mode)
62
- if ENV["CIVO_ACCESS_KEY_ID"] && ENV["CIVO_SECRET_KEY"]
63
- puts "📥 Downloading agent-bridge from Civo bucket..."
64
- if download_from_civo!(arch, target_dir)
65
- return download_agent_bridge_tmux!(target_dir)
66
- else
67
- puts "⚠️ Civo download failed, falling back to GitHub..."
68
- end
69
- end
70
-
71
- # 4. Fallback to GitHub raw URLs (legacy)
61
+ # 3. Download from the public bucket (production mode)
72
62
  bridge_url = "#{YATFA_RAW_URL}/bin/agent-bridge-linux-#{arch}"
73
- puts "📥 Downloading agent-bridge for linux-#{arch} from GitHub..."
63
+ puts "📥 Downloading agent-bridge for linux-#{arch} from bucket..."
74
64
  system("sudo", "curl", "-fsSL", bridge_url, "-o", "#{target_dir}/agent-bridge")
75
65
  unless $?.success?
76
- puts "❌ Failed to download agent-bridge from GitHub"
66
+ puts "❌ Failed to download agent-bridge from #{bridge_url}"
77
67
  exit 1
78
68
  end
79
69
  system("sudo", "chmod", "+x", "#{target_dir}/agent-bridge")
@@ -82,63 +72,6 @@ def download_agent_bridge!
82
72
  download_agent_bridge_tmux!(target_dir)
83
73
  end
84
74
 
85
- def download_from_civo!(arch, target_dir)
86
- require "aws-sdk-s3"
87
-
88
- region = ENV["CIVO_REGION"] || "LON1"
89
- endpoint = ENV["CIVO_ENDPOINT"] || "https://objectstore.fra1.civo.com"
90
- bucket = ENV["YATFA_BINARIES_BUCKET"] || "yatfa"
91
- version = ENV["AGENT_BRIDGE_VERSION"] || "latest"
92
-
93
- binary_name = "agent-bridge-linux-#{arch}"
94
- key = "agent-bridge/#{version}/#{binary_name}"
95
-
96
- puts " Region: #{region}"
97
- puts " Bucket: #{bucket}"
98
- puts " Version: #{version}"
99
- puts " Binary: #{binary_name}"
100
-
101
- begin
102
- # Configure Civo S3-compatible client
103
- Aws.config.update({
104
- region: region,
105
- endpoint: endpoint,
106
- access_key_id: ENV["CIVO_ACCESS_KEY_ID"],
107
- secret_access_key: ENV["CIVO_SECRET_KEY"]
108
- })
109
-
110
- s3 = Aws::S3::Resource.new
111
- obj = s3.bucket(bucket).object(key)
112
-
113
- # Generate presigned URL (valid for 1 hour)
114
- url = obj.presigned_url(:get, expires_in: 3600)
115
-
116
- puts "✅ Generated presigned URL"
117
-
118
- # Download via curl (array form avoids shell interpolation of URL)
119
- system("sudo", "curl", "-fsSL", url, "-o", "#{target_dir}/agent-bridge")
120
-
121
- unless $?.success?
122
- puts "❌ Failed to download agent-bridge from Civo"
123
- return false
124
- end
125
-
126
- system("sudo", "chmod", "+x", "#{target_dir}/agent-bridge")
127
- puts "✅ Downloaded agent-bridge from Civo"
128
- return true
129
-
130
- rescue LoadError
131
- puts "⚠️ aws-sdk-s3 gem not installed"
132
- return false
133
- rescue Aws::S3::Errors::NoSuchKey
134
- puts "❌ Version #{version} not found in bucket"
135
- return false
136
- rescue => e
137
- puts "❌ Civo download failed: #{e.message}"
138
- return false
139
- end
140
- end
141
-
142
75
  def download_agent_bridge_tmux!(target_dir)
143
76
  bridge_tmux_url = "#{YATFA_RAW_URL}/bin/agent-bridge-tmux"
144
77
 
@@ -36,6 +36,41 @@ require "uri"
36
36
  require "time"
37
37
  require "fileutils"
38
38
 
39
+ # Self-healing URL derivation (YATFA-3018). This helper runs in isolation as
40
+ # /usr/local/bin/git-auth-helper (copied there by setup-github.rb alongside
41
+ # url_helper.rb), so it derives YATFA_API_URL from YATFA_URL itself — defaulting
42
+ # to https://yatfa.com — rather than bailing when YATFA_API_URL is empty (the
43
+ # exact failure operators hit in `docker exec` shells outside the setup
44
+ # process). Load the shared module: in-tree via require_relative (tests / setup/),
45
+ # or from the absolute install path when running as the copied binary. A
46
+ # last-resort inline mirror keeps auth working even if the sibling file is
47
+ # missing (version skew / half install) — a credential helper must never break
48
+ # on a missing file. Keep the mirror in sync with setup/url_helper.rb.
49
+ begin
50
+ require_relative "url_helper"
51
+ rescue LoadError
52
+ begin
53
+ require "/usr/local/bin/url_helper"
54
+ rescue LoadError
55
+ module YatfaAgent
56
+ module UrlHelper
57
+ DEFAULT_YATFA_URL = "https://yatfa.com"
58
+
59
+ module_function
60
+
61
+ def api_url(source = ENV)
62
+ explicit = source["YATFA_API_URL"].to_s.strip
63
+ return explicit unless explicit.empty?
64
+
65
+ base = source["YATFA_URL"].to_s.strip
66
+ base = DEFAULT_YATFA_URL if base.empty?
67
+ base.end_with?("/api/v1") || base.end_with?("/api/") ? base : "#{base}/api/v1"
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
39
74
  module YatfaAgent
40
75
  module GitCredentialHelper
41
76
  DEFAULT_CACHE_DIR = "/tmp/github-token-cache"
@@ -116,9 +151,11 @@ module YatfaAgent
116
151
  # using the per-installation cache and falling back to the default token
117
152
  # when a repo-scoped fetch fails (older backend, unregistered repo, etc.).
118
153
  def resolve_token(repo, cache_dir:, env:, http_fetch:)
119
- api_url = env["YATFA_API_URL"]
154
+ # Self-heal: derive YATFA_API_URL from YATFA_URL (default https://yatfa.com)
155
+ # when not explicitly set, so a bare `docker exec` shell with no manual
156
+ # export can still auth. An explicit YATFA_API_URL override is honored.
157
+ api_url = YatfaAgent::UrlHelper.api_url(env)
120
158
  api_key = env["YATFA_API_KEY"]
121
- return nil if api_url.nil? || api_url.to_s.empty?
122
159
  return nil if api_key.nil? || api_key.to_s.empty?
123
160
 
124
161
  cached = read_cached_token(repo, cache_dir)
@@ -134,6 +171,19 @@ module YatfaAgent
134
171
  data["token"]
135
172
  end
136
173
 
174
+ # When required config is missing, return a specific diagnostic naming the
175
+ # offending variable; otherwise nil. The API URL is self-healed (derived
176
+ # from YATFA_URL, default https://yatfa.com), so the only hard requirement
177
+ # is the YATFA_API_KEY secret — previously this surfaced as the opaque
178
+ # "failed to obtain GitHub token" (YATFA-3018).
179
+ def missing_config_message(env_hash, repo: nil)
180
+ api_key = env_hash["YATFA_API_KEY"]
181
+ return nil unless api_key.nil? || api_key.to_s.empty?
182
+
183
+ repo_suffix = repo ? " for #{repo}" : ""
184
+ "git-auth-helper: YATFA_API_KEY is not set — cannot mint a GitHub token#{repo_suffix}"
185
+ end
186
+
137
187
  # Dispatch entry point. Returns a process exit code (0 success, 1 failure).
138
188
  def run(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr,
139
189
  cache_dir: DEFAULT_CACHE_DIR, env: nil, http_fetch: nil)
@@ -145,6 +195,10 @@ module YatfaAgent
145
195
  if action == "get"
146
196
  attrs = parse_input(read_stdin(stdin))
147
197
  repo = normalize_path(attrs["path"])
198
+ if (msg = missing_config_message(env_hash, repo: repo))
199
+ stderr.puts(msg)
200
+ return 1
201
+ end
148
202
  token = resolve_token(repo, cache_dir: cache_dir, env: env_hash, http_fetch: fetch)
149
203
  if token.nil?
150
204
  stderr.puts("git-auth-helper: failed to obtain GitHub token" + (repo ? " for #{repo}" : ""))
@@ -160,6 +214,10 @@ module YatfaAgent
160
214
  end
161
215
 
162
216
  # Token-only mode (e.g. the `gh` wrapper): print just the default token.
217
+ if (msg = missing_config_message(env_hash))
218
+ stderr.puts(msg)
219
+ return 1
220
+ end
163
221
  token = resolve_token(nil, cache_dir: cache_dir, env: env_hash, http_fetch: fetch)
164
222
  if token.nil?
165
223
  stderr.puts("git-auth-helper: failed to obtain GitHub token")
@@ -36,6 +36,7 @@ require "time"
36
36
  require "socket"
37
37
  require "timeout"
38
38
  require_relative "http_client"
39
+ require_relative "url_helper"
39
40
  require_relative "setup-github"
40
41
  require_relative "repositories"
41
42
  require_relative "agent_bridge"
@@ -52,47 +53,32 @@ YATFA_RAW_URL = "#{YATFA_SCRIPTS_BASE_URL}/#{YATFA_VERSION}"
52
53
  VALID_AGENT_TYPES = %w[planner worker reviewer researcher claw]
53
54
 
54
55
  def check_env!
55
- agent_type = ENV["AGENT_TYPE"]
56
-
57
56
  # Note: claw agents have a separate entry point in setup-claw-agent.rb
58
57
  # which calls check_env_claw! directly. This method only handles standard agent types.
59
58
 
60
- required = %w[YATFA_URL]
61
- missing = required.select { |var| ENV[var].to_s.empty? }
62
- unless missing.empty?
63
- puts "❌ Missing environment variables: #{missing.join(', ')}"
64
- puts ""
65
- puts "Required:"
66
- puts " YATFA_URL - Yatfa platform URL (https://yatfa.example.com)"
59
+ # YATFA_API_KEY is the single required secret — the agent cannot authenticate
60
+ # to the backend or mint GitHub tokens without it. Fail fast with a specific
61
+ # message rather than starting a half-working agent (YATFA-3018).
62
+ if ENV["YATFA_API_KEY"].to_s.empty?
63
+ puts "❌ Missing required environment variable: YATFA_API_KEY"
64
+ puts " YATFA_API_KEY is the MCP API key for this agent (from the Yatfa dashboard)."
65
+ puts " The agent needs it to authenticate to the backend and to mint GitHub tokens."
67
66
  puts ""
68
- puts "Optional:"
69
- puts " YATFA_API_URL - API URL for MCP tools (derived from YATFA_URL)"
70
- puts " YATFA_API_KEY - MCP API key"
71
- puts " GH_TOKEN - GitHub token"
67
+ puts "YATFA_URL is optional (defaults to https://yatfa.com);"
68
+ puts " YATFA_API_URL / YATFA_WS_URL are derived from it."
72
69
  exit 1
73
70
  end
74
71
 
75
- # Derive YATFA_API_URL and YATFA_WS_URL from YATFA_URL if not set
76
- # IMPORTANT: Always use original YATFA_URL for derivations, not modified values
77
- yatfa_url = ENV["YATFA_URL"]
78
-
79
- # Derive YATFA_API_URL: append /api/v1 if not already present
80
- unless ENV["YATFA_API_URL"]
81
- if yatfa_url.end_with?("/api/v1") || yatfa_url.end_with?("/api/")
82
- ENV["YATFA_API_URL"] = yatfa_url
83
- else
84
- ENV["YATFA_API_URL"] = "#{yatfa_url}/api/v1"
85
- end
86
- puts "📡 Derived YATFA_API_URL: #{ENV['YATFA_API_URL']}"
87
- end
88
-
89
- # Derive YATFA_WS_URL: replace protocol with ws/wss and add /cable endpoint
90
- # IMPORTANT: Use original yatfa_url before any modifications
91
- original_yatfa_url = ENV["YATFA_URL"]
92
- unless ENV["YATFA_WS_URL"]
93
- ENV["YATFA_WS_URL"] = "#{original_yatfa_url.sub(/^https:\/\//, 'wss://').sub(/^http:\/\//, 'ws://')}/cable"
94
- puts "📡 Derived YATFA_WS_URL: #{ENV['YATFA_WS_URL']}"
95
- end
72
+ # Derive the three URLs via the shared UrlHelper (YATFA-3018). YATFA_URL
73
+ # defaults to https://yatfa.com when unset; YATFA_API_URL (/api/v1) and
74
+ # YATFA_WS_URL (scheme-swapped /cable) are derived from it. Explicit
75
+ # overrides for any of the three are honored (backwards compatible).
76
+ ENV["YATFA_URL"] = YatfaAgent::UrlHelper.base_url
77
+ ENV["YATFA_API_URL"] = YatfaAgent::UrlHelper.api_url
78
+ ENV["YATFA_WS_URL"] = YatfaAgent::UrlHelper.ws_url
79
+ puts "📡 YATFA_URL: #{ENV['YATFA_URL']}"
80
+ puts "📡 YATFA_API_URL: #{ENV['YATFA_API_URL']}"
81
+ puts "📡 YATFA_WS_URL: #{ENV['YATFA_WS_URL']}"
96
82
 
97
83
  agent_type = ENV["AGENT_TYPE"]
98
84
  if agent_type && !VALID_AGENT_TYPES.include?(agent_type)
@@ -14,6 +14,9 @@
14
14
  # - fetch_llm_credentials! is defined by the parent setup-agent.rb
15
15
  # - Standard library requires (json, fileutils, pathname, etc.) are loaded
16
16
  # by setup-agent.rb before this file is required
17
+ # - YatfaAgent::UrlHelper is provided by setup/url_helper.rb
18
+
19
+ require_relative "url_helper" unless defined?(YatfaAgent::UrlHelper)
17
20
 
18
21
  def check_env_claw!
19
22
  required = %w[OPENCLAW_TELEGRAM_BOT_TOKEN]
@@ -37,9 +40,11 @@ def check_env_claw!
37
40
  exit 1
38
41
  end
39
42
 
40
- # Derive YATFA_API_URL from YATFA_URL if set (optional for claw)
43
+ # Derive YATFA_API_URL from YATFA_URL via the shared helper (optional for
44
+ # claw — yatfa integration is opt-in, so only when YATFA_URL is set). The
45
+ # /api/v1 suffix rule lives in one place (YATFA-3018).
41
46
  if ENV["YATFA_URL"] && !ENV["YATFA_API_URL"]
42
- ENV["YATFA_API_URL"] = "#{ENV['YATFA_URL']}/api/v1"
47
+ ENV["YATFA_API_URL"] = YatfaAgent::UrlHelper.with_api_suffix(ENV["YATFA_URL"])
43
48
  end
44
49
 
45
50
  puts "✅ Claw environment validated"
@@ -4,26 +4,14 @@
4
4
  # Extracted from setup-agent.rb for modularity
5
5
 
6
6
  def setup_github_auth!
7
- rails_api_url = ENV["YATFA_API_URL"]
8
7
  rails_api_key = ENV["YATFA_API_KEY"]
9
8
 
10
- # Derive YATFA_API_URL from YATFA_URL if not set
11
- unless rails_api_url && !rails_api_url.empty?
12
- yatfa_url = ENV["YATFA_URL"]
13
- if yatfa_url && !yatfa_url.empty?
14
- rails_api_url = "#{yatfa_url}/api/v1"
15
- ENV["YATFA_API_URL"] = rails_api_url
16
- end
17
- end
18
-
19
- # Validate YATFA_API_URL format before proceeding
20
- if rails_api_url && !rails_api_url.empty? && !rails_api_url.include?('/api/v1')
21
- puts "⚠️ Warning: YATFA_API_URL must include /api/v1 path"
22
- puts " Current: #{rails_api_url}"
23
- puts " Expected: https://example.com/api/v1"
24
- puts " Falling back to alternative authentication..."
25
- rails_api_url = nil # Clear it to fall through to other methods
26
- end
9
+ # Derive YATFA_API_URL via the shared UrlHelper (YATFA-3018): honors an
10
+ # explicit override, else derives /api/v1 from YATFA_URL (default
11
+ # https://yatfa.com). check_env! already set this for standard agents, but
12
+ # re-derive defensively so a re-run / partial flow is consistent.
13
+ ENV["YATFA_API_URL"] = YatfaAgent::UrlHelper.api_url
14
+ rails_api_url = ENV["YATFA_API_URL"]
27
15
 
28
16
  # New method: Get token from Rails backend (preferred)
29
17
  if rails_api_url && !rails_api_url.empty? && rails_api_key && !rails_api_key.empty?
@@ -38,6 +26,16 @@ def setup_github_auth!
38
26
  system("sudo", "mv", "/tmp/git-auth-helper", helper_path)
39
27
  system("sudo", "chmod", "+x", helper_path)
40
28
 
29
+ # Install the shared URL helper alongside the credential helper. The helper
30
+ # runs in isolation as /usr/local/bin/git-auth-helper and loads it via
31
+ # `require_relative "url_helper"` to self-heal YATFA_API_URL from YATFA_URL
32
+ # (default https://yatfa.com) in bare `docker exec` shells (YATFA-3018).
33
+ url_helper_src = File.join(__dir__, "url_helper.rb")
34
+ if File.exist?(url_helper_src)
35
+ File.write("/tmp/url_helper", File.read(url_helper_src))
36
+ system("sudo", "mv", "/tmp/url_helper", "/usr/local/bin/url_helper.rb")
37
+ end
38
+
41
39
  # Wire the helper into git. useHttpPath=true makes git send the repo path on
42
40
  # stdin so the helper can request a repo-scoped token (?repo=owner/repo).
43
41
  # Array form avoids shell interpolation of the helper path.
package/setup/skills.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # Extracted from setup-agent.rb for modularity (Round 2)
5
5
  #
6
6
  # Handles:
7
- # - Installing built-in skills from the SKILLS env var (Civo-hosted SKILL.md files)
7
+ # - Installing built-in skills from the SKILLS env var (R2-hosted SKILL.md files)
8
8
  # - Fetching community skills list from the backend whoami API
9
9
  # - Installing community skills from external GitHub repos
10
10
  #
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ # YATFA URL derivation — the single source of truth.
4
+ #
5
+ # `YATFA_URL` is the only URL variable an operator ever configures. The API URL
6
+ # (`<base>/api/v1`) and the WebSocket URL (`<scheme-adjusted base>/cable`) are
7
+ # ALWAYS derived from it. Both the host-side launcher (lib/yatfa_agent/) and the
8
+ # in-container setup scripts (setup/*) load this module so the derivation lives
9
+ # in exactly one place.
10
+ #
11
+ # Rules (see YATFA-3018):
12
+ # 1. `YATFA_URL` defaults to `https://yatfa.com` when unset/blank, so the
13
+ # hosted deployment works with zero URL configuration.
14
+ # 2. `YATFA_API_URL` / `YATFA_WS_URL`, when explicitly set, are honored as
15
+ # overrides (backwards compatible) — never re-derived.
16
+ # 3. The API URL appends `/api/v1` unless the base already carries an `/api`
17
+ # path; the WS URL swaps the scheme (http→ws, https→wss) and appends
18
+ # `/cable`.
19
+ #
20
+ # Every method takes an optional +source+ — any hash-like object (ENV, or a
21
+ # string-keyed config hash) — so the same logic serves the launcher (config
22
+ # hash) and the container scripts (ENV).
23
+
24
+ module YatfaAgent
25
+ module UrlHelper
26
+ DEFAULT_YATFA_URL = "https://yatfa.com"
27
+ API_SUFFIX = "/api/v1"
28
+ WS_SUFFIX = "/cable"
29
+
30
+ module_function
31
+
32
+ # Resolve the base platform URL, applying the hosted default when unset.
33
+ # Reads +YATFA_URL+ (ENV convention) then +yatfa_url+ / +rails_api_url+
34
+ # (config-hash convention).
35
+ def base_url(source = ENV)
36
+ value = source_value(source, "YATFA_URL") ||
37
+ source_value(source, "yatfa_url") ||
38
+ source_value(source, "rails_api_url")
39
+ value = value.to_s.strip
40
+ value.empty? ? DEFAULT_YATFA_URL : value
41
+ end
42
+
43
+ # Derive the API URL. An explicit +YATFA_API_URL+ override wins; otherwise
44
+ # the suffix is appended to the resolved base URL.
45
+ def api_url(source = ENV)
46
+ explicit = source_value(source, "YATFA_API_URL").to_s.strip
47
+ return explicit unless explicit.empty?
48
+
49
+ with_api_suffix(base_url(source))
50
+ end
51
+
52
+ # Derive the WebSocket URL. An explicit +YATFA_WS_URL+ override wins;
53
+ # otherwise the base scheme is swapped (http→ws, https→wss) and +/cable+
54
+ # is appended.
55
+ def ws_url(source = ENV)
56
+ explicit = source_value(source, "YATFA_WS_URL").to_s.strip
57
+ return explicit unless explicit.empty?
58
+
59
+ "#{to_ws_scheme(base_url(source))}#{WS_SUFFIX}"
60
+ end
61
+
62
+ # Append +/api/v1+ to +base+ unless it already ends with +/api/v1+ or
63
+ # +/api/+. Shared with YatfaAgent::Config so the suffix rule is defined once.
64
+ def with_api_suffix(base)
65
+ base = base.to_s
66
+ return base if base.end_with?("/api/v1") || base.end_with?("/api/")
67
+
68
+ "#{base}#{API_SUFFIX}"
69
+ end
70
+
71
+ # Swap the scheme of +base+ to its WebSocket equivalent. Already-ws schemes
72
+ # are left untouched.
73
+ def to_ws_scheme(base)
74
+ base.to_s.sub(/\Ahttps:\/\//, "wss://").sub(/\Ahttp:\/\//, "ws://")
75
+ end
76
+
77
+ # Safely read +key+ from a hash-like +source+ (ENV or a config Hash).
78
+ def source_value(source, key)
79
+ return nil unless source.respond_to?(:[])
80
+
81
+ source[key]
82
+ rescue StandardError
83
+ nil
84
+ end
85
+ end
86
+ end