pinesandbox 0.3.19 → 0.3.21
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/README.md +154 -33
- data/lib/pinesandbox/adapters/connect_aware_net_http.rb +32 -0
- data/lib/pinesandbox/adapters/coordinator_adapter.rb +153 -135
- data/lib/pinesandbox/agent_mode.rb +32 -18
- data/lib/pinesandbox/agent_turn.rb +2 -1
- data/lib/pinesandbox/artifacts.rb +785 -0
- data/lib/pinesandbox/attach_credentials_source.rb +100 -15
- data/lib/pinesandbox/author_terminal_translator.rb +12 -11
- data/lib/pinesandbox/client.rb +83 -30
- data/lib/pinesandbox/computer.rb +181 -81
- data/lib/pinesandbox/connection.rb +7 -3
- data/lib/pinesandbox/control_plane.rb +29 -15
- data/lib/pinesandbox/control_token_source.rb +3 -1
- data/lib/pinesandbox/diagnostics.rb +4 -3
- data/lib/pinesandbox/error_taxonomy.rb +6 -0
- data/lib/pinesandbox/errors.rb +168 -12
- data/lib/pinesandbox/models/artifact_file.rb +40 -0
- data/lib/pinesandbox/models/computer_session.rb +7 -3
- data/lib/pinesandbox/operations.rb +2 -0
- data/lib/pinesandbox/response_handler.rb +4 -1
- data/lib/pinesandbox/runtime_admission.rb +112 -0
- data/lib/pinesandbox/sandbox_handle.rb +71 -39
- data/lib/pinesandbox/session.rb +22 -3
- data/lib/pinesandbox/streamed_read.rb +80 -0
- data/lib/pinesandbox/terminal_reason.rb +31 -0
- data/lib/pinesandbox/timeouts.rb +22 -0
- data/lib/pinesandbox/version.rb +3 -1
- data/lib/pinesandbox/zone.rb +3 -0
- data/lib/pinesandbox.rb +4 -0
- metadata +8 -1
data/lib/pinesandbox/computer.rb
CHANGED
|
@@ -8,20 +8,21 @@ require_relative "bind_hpke"
|
|
|
8
8
|
require_relative "capture_keypair"
|
|
9
9
|
require_relative "adapters/coordinator_adapter"
|
|
10
10
|
require_relative "adapters/snapshot_adapter"
|
|
11
|
+
require_relative "artifacts"
|
|
11
12
|
require_relative "session"
|
|
12
13
|
|
|
13
14
|
module PineSandbox
|
|
14
15
|
# A persistent Computer — the one object in this SDK. It is either
|
|
15
16
|
# powered-off (just an `id` + the per-Computer `key` you hold in your DB)
|
|
16
|
-
# or powered-on (started and attached).
|
|
17
|
-
# bind/restore handshake so the Computer resumes its prior state,
|
|
18
|
-
# returns
|
|
19
|
-
#
|
|
20
|
-
# later
|
|
17
|
+
# or powered-on (started and attached). Client#start_computer starts it,
|
|
18
|
+
# runs the bind/restore handshake so the Computer resumes its prior state,
|
|
19
|
+
# and returns it; you then drive it through `create_session`. `stop` shuts
|
|
20
|
+
# the Computer down after Pine tries a final save, and Client#start_computer
|
|
21
|
+
# starts it again later from its saved state (Client#recover_computer after
|
|
22
|
+
# a failure).
|
|
21
23
|
#
|
|
22
|
-
# computer =
|
|
23
|
-
# computer.
|
|
24
|
-
# session = computer.create_session(browser: true)
|
|
24
|
+
# computer = client.start_computer(credentials)
|
|
25
|
+
# session = computer.create_session
|
|
25
26
|
# session.create_tab(url: "https://example.com")
|
|
26
27
|
# computer.stop
|
|
27
28
|
#
|
|
@@ -35,6 +36,9 @@ module PineSandbox
|
|
|
35
36
|
class Computer
|
|
36
37
|
CURRENT_KEY_VERSION = 1
|
|
37
38
|
DEFAULT_MAX_BIND_ATTEMPTS = 3
|
|
39
|
+
# How long, in seconds, a Computer keeps running without use when a start
|
|
40
|
+
# names no timeout:.
|
|
41
|
+
DEFAULT_IDLE_TIMEOUT = 1800
|
|
38
42
|
# How long to wait for the Computer to start; independent of its idle timeout.
|
|
39
43
|
DEFAULT_READY_TIMEOUT = 300
|
|
40
44
|
# Wall-clock budget for a just-started Computer to become bindable. Spans
|
|
@@ -72,7 +76,7 @@ module PineSandbox
|
|
|
72
76
|
# route by token class (`/exec` is ps_-only; ps_ is bound to its own
|
|
73
77
|
# session), so a raw call here can only do what the caller's own token
|
|
74
78
|
# already permits.
|
|
75
|
-
|
|
79
|
+
def coordinator = coordinator!
|
|
76
80
|
|
|
77
81
|
# A fresh Computer identity: a UUIDv7 `id` + a 32-byte state `key` — the SAME
|
|
78
82
|
# pair create_computer generates internally, exposed as the SDK's built-in
|
|
@@ -192,65 +196,81 @@ module PineSandbox
|
|
|
192
196
|
# just-started Computer down and leaves it powered-off before a typed error.
|
|
193
197
|
#
|
|
194
198
|
# @param config [PineSandbox::Connection]
|
|
195
|
-
# timeout is how long the Computer keeps
|
|
196
|
-
#
|
|
197
|
-
#
|
|
199
|
+
# timeout is the idle timeout: how long, in seconds, the Computer keeps
|
|
200
|
+
# running after its last activity (default 1800, at least 60, at most the
|
|
201
|
+
# project's allowance). Activity is anything done on the Computer: a command,
|
|
202
|
+
# a browser action, a file write, a message to the agent, an agent turn while
|
|
203
|
+
# it runs, a person in control. Each one restarts the countdown. Only looking
|
|
204
|
+
# doesn't: reading status or results, and listening to events. After a whole
|
|
205
|
+
# timeout with no activity the Computer stops; call #stop yourself to wait
|
|
206
|
+
# for its save. #set_idle_timeout changes the timeout while the Computer runs.
|
|
207
|
+
# ready_timeout bounds how long it takes to start; bind_ready_timeout
|
|
208
|
+
# separately bounds the bind/restore handshake.
|
|
198
209
|
# @param pod_env [nil, Hash] Deprecated; only nil or an empty hash is accepted.
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
raise ArgumentError, "ready_timeout must be a positive finite number of seconds"
|
|
214
|
-
end
|
|
210
|
+
# @deprecated Use Client#start_computer or Client#recover_computer, which check the current runtime first.
|
|
211
|
+
def attach(config, **options)
|
|
212
|
+
PineSandbox.deprecated_name("Computer#attach", "Client#start_computer")
|
|
213
|
+
attach_runtime(config, **options)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# @api private — #attach without its deprecation warning, for Client.
|
|
217
|
+
# budget_start: when ready_timeout began (nil: now).
|
|
218
|
+
def attach_runtime(config, timeout: DEFAULT_IDLE_TIMEOUT, ready_timeout: DEFAULT_READY_TIMEOUT, pod_env: nil, metadata: nil,
|
|
219
|
+
location: nil,
|
|
220
|
+
max_bind_attempts: DEFAULT_MAX_BIND_ATTEMPTS,
|
|
221
|
+
bind_ready_timeout: DEFAULT_BIND_READY_TIMEOUT, budget_start: nil)
|
|
222
|
+
requested_location = check_attach_options!(config, ready_timeout: ready_timeout, pod_env: pod_env,
|
|
223
|
+
location: location, max_bind_attempts: max_bind_attempts)
|
|
215
224
|
|
|
216
225
|
# Refuse to re-attach a live Computer: silently tearing down the prior pod
|
|
217
226
|
# here would force-kill it (ungraceful) and lose state since the last
|
|
218
227
|
# checkpoint. Make the teardown an explicit caller choice — #stop (graceful,
|
|
219
228
|
# persist) or #kill (discard) — then attach again.
|
|
220
229
|
raise BindError.new("Computer already attached — call #stop (graceful) or #kill first") if @sandbox
|
|
221
|
-
# A persistent attach requires the capture keypair (it seals every capture
|
|
222
|
-
# to it); an ephemeral Computer persists nothing, so it binds without one.
|
|
223
|
-
if @capture_generation.zero? && !@ephemeral
|
|
224
|
-
raise ArgumentError, "v3 attach requires a capture_keypair (persist both halves)"
|
|
225
|
-
end
|
|
226
|
-
# Preserve omission as omission. @location is only the last Portal
|
|
227
|
-
# receipt, not authority to overwrite a newer country another client may
|
|
228
|
-
# have committed after this instance stopped.
|
|
229
|
-
requested_location = normalize_location(location) if location
|
|
230
230
|
@last_authorized_sandbox_id = nil
|
|
231
231
|
@connection = config
|
|
232
232
|
@clock = config.respond_to?(:clock) ? config.clock : PineSandbox::SYSTEM_CLOCK
|
|
233
233
|
|
|
234
234
|
# Provision via the project-scoped control plane (api.<zone>, project JWS).
|
|
235
235
|
create_id = SecureRandom.uuid
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
236
|
+
budget_start ||= monotonic_now
|
|
237
|
+
ready_deadline = budget_start + ready_timeout
|
|
238
|
+
body = build_computer_create_body(timeout: timeout, metadata: metadata).merge(create_id: create_id)
|
|
239
|
+
sent = false
|
|
240
|
+
info = begin
|
|
241
|
+
config.control_plane.create_computer(body: body, request_timeout: ready_deadline - monotonic_now,
|
|
242
|
+
reached: -> { sent = true })
|
|
243
|
+
rescue PineSandbox::TimeoutError => e
|
|
244
|
+
raise unless sent && monotonic_now >= ready_deadline
|
|
245
|
+
|
|
246
|
+
# The budget cut the create off, so a runtime may exist that nothing would delete. Sent
|
|
247
|
+
# again with its create_id, the create names it; the spent budget below then deletes it.
|
|
248
|
+
begin
|
|
249
|
+
config.control_plane.create_computer(body: body, request_timeout: connection_timeouts(config).request)
|
|
250
|
+
rescue StandardError
|
|
251
|
+
raise e
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
@sandbox = SandboxHandle.new(control_plane: config.control_plane, sandbox_id: info.id,
|
|
255
|
+
status: info.status, reason: info.reason)
|
|
242
256
|
# Wait for the pod to report Running (a CUA pod's readiness; the bind handshake
|
|
243
257
|
# below is the functional coord-readiness check). A pod that fails or never
|
|
244
258
|
# becomes Ready is destroyed before the typed error leaves #attach.
|
|
245
259
|
begin
|
|
246
|
-
|
|
260
|
+
# The create took the budget: a runtime that already runs isn't bound either.
|
|
261
|
+
if monotonic_now >= ready_deadline
|
|
262
|
+
raise ReadyTimeoutError.new(sandbox_id: info.id, last_state: info.status, last_reason: info.reason,
|
|
263
|
+
waited: monotonic_now - budget_start, target: :running, new_runtime: true)
|
|
264
|
+
end
|
|
265
|
+
@sandbox.wait_until_running(timeout: [ready_deadline - monotonic_now, 0].max, started: budget_start)
|
|
247
266
|
rescue StandardError
|
|
248
267
|
reset_bound_state
|
|
249
268
|
raise
|
|
250
269
|
end
|
|
251
270
|
# Drive the Computer at its gateway host `https://<sandbox_id>.computer.<zone>`
|
|
252
271
|
# — port-less, header-less, role-by-path; coord gates every route on the bearer.
|
|
253
|
-
@coordinator = Adapters::CoordinatorAdapter.new(sandbox_id: info.id, zone: config.zone, clock: @clock
|
|
272
|
+
@coordinator = Adapters::CoordinatorAdapter.new(sandbox_id: info.id, zone: config.zone, clock: @clock,
|
|
273
|
+
timeouts: connection_timeouts(config))
|
|
254
274
|
|
|
255
275
|
# Two retry budgets converge on the bind handshake:
|
|
256
276
|
# - readiness (DEADLINE-bounded): the Computer sandbox was just started, so the
|
|
@@ -309,11 +329,44 @@ module PineSandbox
|
|
|
309
329
|
self
|
|
310
330
|
end
|
|
311
331
|
|
|
312
|
-
#
|
|
313
|
-
|
|
332
|
+
# @api private — Start checks it before its first read, which the budget bounds.
|
|
333
|
+
def self.check_ready_timeout!(ready_timeout)
|
|
334
|
+
return if (ready_timeout.is_a?(Integer) || ready_timeout.is_a?(Float)) && ready_timeout.finite? && ready_timeout.positive?
|
|
335
|
+
|
|
336
|
+
raise ArgumentError, "ready_timeout must be a positive finite number of seconds"
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# @api private — #attach_runtime's checks, which Start runs before its gate. Takes
|
|
340
|
+
# every #attach_runtime keyword, so an unknown one fails here. Returns the normalized location.
|
|
341
|
+
def check_attach_options!(config, ready_timeout: DEFAULT_READY_TIMEOUT, pod_env: nil, location: nil,
|
|
342
|
+
max_bind_attempts: DEFAULT_MAX_BIND_ATTEMPTS, timeout: nil, metadata: nil,
|
|
343
|
+
bind_ready_timeout: nil)
|
|
344
|
+
unless config.respond_to?(:attach_provider) && config.respond_to?(:control_plane) && config.respond_to?(:zone)
|
|
345
|
+
raise ArgumentError, "config must be a PineSandbox::Connection (got #{config.class})"
|
|
346
|
+
end
|
|
347
|
+
unless max_bind_attempts.is_a?(Integer) && max_bind_attempts.positive?
|
|
348
|
+
raise ArgumentError, "max_bind_attempts must be a positive integer"
|
|
349
|
+
end
|
|
350
|
+
unless pod_env.nil? || pod_env == {}
|
|
351
|
+
raise ArgumentError, "pod_env is unsupported for Computer attach; omit it or pass an empty hash"
|
|
352
|
+
end
|
|
353
|
+
Computer.check_ready_timeout!(ready_timeout)
|
|
354
|
+
# A persistent attach requires the capture keypair (it seals every capture
|
|
355
|
+
# to it); an ephemeral Computer persists nothing, so it binds without one.
|
|
356
|
+
if @capture_generation.zero? && !@ephemeral
|
|
357
|
+
raise ArgumentError, "v3 attach requires a capture_keypair (persist both halves)"
|
|
358
|
+
end
|
|
359
|
+
# Preserve omission as omission. @location is only the last Portal
|
|
360
|
+
# receipt, not authority to overwrite a newer country another client may
|
|
361
|
+
# have committed after this instance stopped.
|
|
362
|
+
normalize_location(location) if location
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
# attach + auto-stop. Yields self; stops the Computer (Pine tries a final
|
|
366
|
+
# save first) when the block returns or raises.
|
|
314
367
|
def with_attached(config, **opts)
|
|
315
368
|
PineSandbox.deprecated_name("Computer#with_attached", "Client#with_computer")
|
|
316
|
-
|
|
369
|
+
attach_runtime(config, **opts)
|
|
317
370
|
yield self
|
|
318
371
|
ensure
|
|
319
372
|
# Graceful by default: persist state on the way out (no-op if attach
|
|
@@ -342,7 +395,8 @@ module PineSandbox
|
|
|
342
395
|
@connection = config
|
|
343
396
|
@clock = config.respond_to?(:clock) ? config.clock : PineSandbox::SYSTEM_CLOCK
|
|
344
397
|
@coordinator = Adapters::CoordinatorAdapter.new(sandbox_id: sandbox_id, zone: config.zone,
|
|
345
|
-
discovery_token: computer_token, clock: @clock
|
|
398
|
+
discovery_token: computer_token, clock: @clock,
|
|
399
|
+
timeouts: connection_timeouts(config))
|
|
346
400
|
@sandbox = SandboxHandle.new(control_plane: config.control_plane, sandbox_id: sandbox_id, status: status)
|
|
347
401
|
@computer_token = computer_token
|
|
348
402
|
@binding_revision = binding_revision
|
|
@@ -352,7 +406,8 @@ module PineSandbox
|
|
|
352
406
|
# The Computer-tier token (`ct_*`) delivered in the bind response. Raises
|
|
353
407
|
# if called before #attach (or after #stop).
|
|
354
408
|
def computer_token
|
|
355
|
-
|
|
409
|
+
require_binding!
|
|
410
|
+
@computer_token
|
|
356
411
|
end
|
|
357
412
|
|
|
358
413
|
# INFORMATIONAL ONLY: the `expires_at` returned with the ct_ in the bind
|
|
@@ -386,25 +441,36 @@ module PineSandbox
|
|
|
386
441
|
# friendly adjective-noun-hex name (e.g. "brave-otter-9f3a1c") — read it back
|
|
387
442
|
# off the returned {Session}. `label` is an optional human-meaningful tag,
|
|
388
443
|
# distinct from the addressing name. A duplicate name raises
|
|
389
|
-
# PineSandbox::ConflictError.
|
|
390
|
-
#
|
|
391
|
-
|
|
444
|
+
# PineSandbox::ConflictError. Every session has its own browser window.
|
|
445
|
+
#
|
|
446
|
+
# `artifact_access` is what the session's agent may do with the Computer's
|
|
447
|
+
# artifacts (see Client#artifacts): "none" (the default: nothing, and the
|
|
448
|
+
# agent isn't told about them), "read_only" (list and read) or
|
|
449
|
+
# "read_write" (also save and delete files and make public links). A
|
|
450
|
+
# Symbol works too. Your own access with the project key is the same
|
|
451
|
+
# either way.
|
|
452
|
+
def create_session(name: nil, label: nil, artifact_access: nil)
|
|
453
|
+
unless artifact_access.nil? || ((artifact_access.is_a?(String) || artifact_access.is_a?(Symbol)) &&
|
|
454
|
+
Session::ARTIFACT_ACCESS.include?(artifact_access.to_s))
|
|
455
|
+
raise ArgumentError, "artifact_access must be one of #{Session::ARTIFACT_ACCESS.join(', ')}"
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
options = { name: name, label: label }
|
|
459
|
+
options[:artifact_access] = artifact_access.to_s unless artifact_access.nil?
|
|
392
460
|
rebind_on_unauthorized do
|
|
393
|
-
payload = coordinator!.create_session(
|
|
394
|
-
token: computer_token, name: name, label: label, browser: browser, blind: blind
|
|
395
|
-
)
|
|
461
|
+
payload = coordinator!.create_session(token: computer_token, **options)
|
|
396
462
|
Session.new(parent: self, payload: payload)
|
|
397
463
|
end
|
|
398
464
|
end
|
|
399
465
|
|
|
400
466
|
# Desktop delegation in one call:
|
|
401
467
|
# computer.delegate_desktop(name: session.name)
|
|
402
|
-
# Ensures the named
|
|
468
|
+
# Ensures the named desktop session exists, then returns its
|
|
403
469
|
# browser-safe {DelegatedConnection} (host + session name + spec major +
|
|
404
470
|
# a fresh short-TTL `dt_`; no `ct_`/`ps_`/JWS). `create: true` (default) creates
|
|
405
471
|
# the session if absent; `create: false` requires it. To refresh the `dt_`
|
|
406
472
|
# before a reconnect, call `session(name).delegate` again (server-side).
|
|
407
|
-
def delegate_desktop(name:, create: true
|
|
473
|
+
def delegate_desktop(name:, create: true)
|
|
408
474
|
require_session_name!(name)
|
|
409
475
|
s =
|
|
410
476
|
begin
|
|
@@ -412,7 +478,7 @@ module PineSandbox
|
|
|
412
478
|
rescue PineSandbox::NotFoundError
|
|
413
479
|
raise unless create
|
|
414
480
|
|
|
415
|
-
create_session(name: name
|
|
481
|
+
create_session(name: name)
|
|
416
482
|
end
|
|
417
483
|
s.delegate
|
|
418
484
|
end
|
|
@@ -426,14 +492,15 @@ module PineSandbox
|
|
|
426
492
|
# Rebuilds a session from the name and token saved when it was created,
|
|
427
493
|
# under this Computer's current binding.
|
|
428
494
|
# No request, session creation or token refresh; authorization is checked
|
|
429
|
-
# on first use. Metadata remains available through #session(name)
|
|
495
|
+
# on first use. Metadata remains available through #session(name); the
|
|
496
|
+
# rebuilt session's #artifact_access is nil, since only the Computer knows it.
|
|
430
497
|
def adopt_session(name, session_token)
|
|
431
498
|
coordinator!
|
|
432
499
|
unless name.is_a?(String) && !name.empty? && session_token.is_a?(String) && !session_token.empty?
|
|
433
500
|
raise ArgumentError, "session name and token are required"
|
|
434
501
|
end
|
|
435
502
|
|
|
436
|
-
Session.new(parent: self, payload: { "name" => name, "token" => session_token })
|
|
503
|
+
Session.new(parent: self, payload: { "name" => name, "token" => session_token }, artifact_access_known: false)
|
|
437
504
|
end
|
|
438
505
|
|
|
439
506
|
# computer.skills.* — the Computer's learned-skill lifecycle (Computer Skills): list/get the
|
|
@@ -443,6 +510,23 @@ module PineSandbox
|
|
|
443
510
|
@skills ||= Skills.new(self)
|
|
444
511
|
end
|
|
445
512
|
|
|
513
|
+
# A public link to one of the Computer's artifacts (see Client#artifacts),
|
|
514
|
+
# served by this running Computer: anyone with the URL can download the
|
|
515
|
+
# file as it is when they open it. The link stops working after
|
|
516
|
+
# `expires_in_seconds` (60 to 2,592,000) if given, when the file is
|
|
517
|
+
# deleted, and always when the Computer stops. Returns the response Hash,
|
|
518
|
+
# { "url" => …, "expires_at" => … }; "expires_at" only with an expiry.
|
|
519
|
+
def artifact_link(path, expires_in_seconds: nil)
|
|
520
|
+
path = ArtifactPath.check!(path)
|
|
521
|
+
unless expires_in_seconds.nil? || (expires_in_seconds.is_a?(Integer) && expires_in_seconds.between?(60, 2_592_000))
|
|
522
|
+
raise ArgumentError, "expires_in_seconds must be an integer from 60 to 2592000"
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
rebind_on_unauthorized do
|
|
526
|
+
coordinator!.create_artifact_link(token: computer_token, path: path, expires_in_seconds: expires_in_seconds)
|
|
527
|
+
end
|
|
528
|
+
end
|
|
529
|
+
|
|
446
530
|
# capabilities — the product features this Computer can use for its binding, and the limits
|
|
447
531
|
# it enforces on your calls.
|
|
448
532
|
def capabilities
|
|
@@ -554,28 +638,33 @@ module PineSandbox
|
|
|
554
638
|
# Preserve the current sessions, capabilities and live process memory.
|
|
555
639
|
# These bounded calls wait for observed completion; neither rebinds.
|
|
556
640
|
def pause(timeout: 360)
|
|
557
|
-
|
|
558
|
-
|
|
641
|
+
require_binding!
|
|
559
642
|
@sandbox.pause(timeout: timeout)
|
|
560
643
|
self
|
|
561
644
|
end
|
|
562
645
|
|
|
563
646
|
def resume(timeout: 360)
|
|
564
|
-
|
|
565
|
-
|
|
647
|
+
require_binding!
|
|
566
648
|
@sandbox.resume(timeout: timeout)
|
|
567
649
|
self
|
|
568
650
|
end
|
|
569
651
|
|
|
570
|
-
#
|
|
571
|
-
#
|
|
572
|
-
#
|
|
573
|
-
#
|
|
652
|
+
# Stops the runtime this handle is bound to (its sandbox_id); Pine tries a
|
|
653
|
+
# final save first. Returns true when that runtime is no longer running, or
|
|
654
|
+
# when the handle has no binding. true does not confirm that the final save
|
|
655
|
+
# succeeded: the save result cannot be read. After true the handle has no
|
|
656
|
+
# binding, and calls that need one raise BindError.
|
|
657
|
+
#
|
|
658
|
+
# Returns false when the runtime did not stop within wait_timeout (seconds,
|
|
659
|
+
# default 390); by then the Stop was submitted or the runtime was already
|
|
660
|
+
# shutting down. A wait_timeout of 0 skips only the wait; the Stop is still
|
|
661
|
+
# submitted. After false, or an error (the Stop may or may not have been
|
|
662
|
+
# submitted), call stop again on this handle.
|
|
574
663
|
def stop(wait_timeout: 390)
|
|
575
664
|
return true unless @sandbox
|
|
576
665
|
|
|
577
666
|
gone = @sandbox.stop(wait_timeout: wait_timeout)
|
|
578
|
-
clear_bound_state if gone
|
|
667
|
+
clear_bound_state("Stop") if gone
|
|
579
668
|
gone
|
|
580
669
|
end
|
|
581
670
|
|
|
@@ -585,7 +674,7 @@ module PineSandbox
|
|
|
585
674
|
# for #kill only to recover.)
|
|
586
675
|
#
|
|
587
676
|
# NOTE: there is intentionally no #destroy alias. The lifecycle verbs are
|
|
588
|
-
# distinct by intent: #stop
|
|
677
|
+
# distinct by intent: #stop tries a final save before shutdown;
|
|
589
678
|
# #kill is ungraceful force-recovery. The "done forever" verb —
|
|
590
679
|
# permanent delete, which ends the identity and purges saved state — is
|
|
591
680
|
# Client#delete_computer (it kills a still-attached Computer first).
|
|
@@ -593,7 +682,7 @@ module PineSandbox
|
|
|
593
682
|
return true unless @sandbox
|
|
594
683
|
|
|
595
684
|
deleted = @sandbox.kill
|
|
596
|
-
clear_bound_state if deleted
|
|
685
|
+
clear_bound_state("Kill") if deleted
|
|
597
686
|
deleted
|
|
598
687
|
end
|
|
599
688
|
|
|
@@ -611,6 +700,7 @@ module PineSandbox
|
|
|
611
700
|
|
|
612
701
|
kill
|
|
613
702
|
provider.delete_computer(id)
|
|
703
|
+
clear_bound_state("Delete")
|
|
614
704
|
true
|
|
615
705
|
end
|
|
616
706
|
|
|
@@ -622,8 +712,7 @@ module PineSandbox
|
|
|
622
712
|
# absence and transport/auth failures remain distinct errors. This never
|
|
623
713
|
# clears a binding, resumes memory or starts the Computer again.
|
|
624
714
|
def status
|
|
625
|
-
|
|
626
|
-
|
|
715
|
+
require_binding!
|
|
627
716
|
@sandbox.status
|
|
628
717
|
end
|
|
629
718
|
|
|
@@ -631,8 +720,7 @@ module PineSandbox
|
|
|
631
720
|
# including while paused. If it has stopped, no Portal lookup, rebind or
|
|
632
721
|
# restart follows.
|
|
633
722
|
def sandbox_details
|
|
634
|
-
|
|
635
|
-
|
|
723
|
+
require_binding!
|
|
636
724
|
info = @sandbox.sandbox_details
|
|
637
725
|
SandboxDetails.from_info(info)
|
|
638
726
|
end
|
|
@@ -640,7 +728,7 @@ module PineSandbox
|
|
|
640
728
|
# An unchanged value is not a heartbeat. On conflict or a lost reply,
|
|
641
729
|
# observe sandbox_details before deciding whether another edit is needed.
|
|
642
730
|
def set_idle_timeout(seconds:)
|
|
643
|
-
|
|
731
|
+
require_binding!
|
|
644
732
|
raise ArgumentError, "idle timeout must be an integer of at least 60 seconds" unless seconds.is_a?(Integer) && seconds >= 60
|
|
645
733
|
|
|
646
734
|
SandboxDetails.from_info(@sandbox.set_idle_timeout(seconds: seconds))
|
|
@@ -649,8 +737,7 @@ module PineSandbox
|
|
|
649
737
|
# Save after Create/Start, alongside the durable credentials. Never log JSON:
|
|
650
738
|
# it contains the live Computer token. Stop clears this object's binding.
|
|
651
739
|
def binding
|
|
652
|
-
|
|
653
|
-
|
|
740
|
+
require_binding!
|
|
654
741
|
Binding.new(computer_id: id.dup, sandbox_id: @sandbox.id.dup,
|
|
655
742
|
computer_token: @computer_token.dup, binding_revision: @binding_revision).validate!
|
|
656
743
|
end
|
|
@@ -680,6 +767,10 @@ module PineSandbox
|
|
|
680
767
|
|
|
681
768
|
private
|
|
682
769
|
|
|
770
|
+
def connection_timeouts(config)
|
|
771
|
+
config.respond_to?(:timeouts) ? config.timeouts : Timeouts::DEFAULT
|
|
772
|
+
end
|
|
773
|
+
|
|
683
774
|
# A Computer has no default session: its session helpers need the name.
|
|
684
775
|
def require_session_name!(name)
|
|
685
776
|
raise ArgumentError, "session name is required" if name.to_s.empty?
|
|
@@ -698,7 +789,15 @@ module PineSandbox
|
|
|
698
789
|
# The bound coordinator, or a clear error if powered-off. Keeps every
|
|
699
790
|
# drive method from NoMethodError-ing on a nil coordinator.
|
|
700
791
|
def coordinator!
|
|
701
|
-
|
|
792
|
+
require_binding!
|
|
793
|
+
@coordinator
|
|
794
|
+
end
|
|
795
|
+
|
|
796
|
+
def require_binding!
|
|
797
|
+
return if @sandbox && @computer_token
|
|
798
|
+
|
|
799
|
+
reason = @unbound_by ? "#{@unbound_by} was called on this handle" : "it was never attached"
|
|
800
|
+
raise BindError, "Computer #{id}: this handle has no binding (#{reason})"
|
|
702
801
|
end
|
|
703
802
|
|
|
704
803
|
# Ungraceful tear-down: best-effort kill (no wait) + drop the local binding.
|
|
@@ -716,7 +815,8 @@ module PineSandbox
|
|
|
716
815
|
|
|
717
816
|
# Drop the local binding WITHOUT touching the pod — used after a graceful
|
|
718
817
|
# #stop has already terminated it (so we don't double-DELETE).
|
|
719
|
-
def clear_bound_state
|
|
818
|
+
def clear_bound_state(by = nil)
|
|
819
|
+
@unbound_by = by if by
|
|
720
820
|
@sandbox = nil
|
|
721
821
|
@coordinator = nil
|
|
722
822
|
@computer_token = nil
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require_relative "zone"
|
|
2
2
|
require_relative "clock"
|
|
3
|
+
require_relative "timeouts"
|
|
3
4
|
|
|
4
5
|
module PineSandbox
|
|
5
6
|
# Internal value object bundling everything Computer#attach needs against the
|
|
@@ -9,10 +10,12 @@ module PineSandbox
|
|
|
9
10
|
# (project-JWS create/inspect/destroy at api.<zone>). Built by
|
|
10
11
|
# PineSandbox::Client — not a public constructor.
|
|
11
12
|
class Connection
|
|
12
|
-
attr_reader :zone, :control_plane, :attach_provider, :clock
|
|
13
|
+
attr_reader :zone, :control_plane, :attach_provider, :clock, :timeouts
|
|
13
14
|
|
|
14
|
-
# `clock` paces every wait the Computer and its data-plane client make
|
|
15
|
-
|
|
15
|
+
# `clock` paces every wait the Computer and its data-plane client make;
|
|
16
|
+
# `timeouts` bounds their connections and requests.
|
|
17
|
+
def initialize(zone:, control_plane:, attach_provider:, clock: PineSandbox::SYSTEM_CLOCK,
|
|
18
|
+
timeouts: Timeouts::DEFAULT)
|
|
16
19
|
raise ArgumentError, "attach_provider required (mints the per-attach bind_token + broker_grant)" unless attach_provider
|
|
17
20
|
raise ArgumentError, "zone required (a PineSandbox::Zone)" unless zone.is_a?(PineSandbox::Zone)
|
|
18
21
|
raise ArgumentError, "control_plane required" unless control_plane
|
|
@@ -21,6 +24,7 @@ module PineSandbox
|
|
|
21
24
|
@zone = zone
|
|
22
25
|
@control_plane = control_plane
|
|
23
26
|
@clock = clock
|
|
27
|
+
@timeouts = timeouts
|
|
24
28
|
end
|
|
25
29
|
end
|
|
26
30
|
end
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
require "faraday"
|
|
2
|
+
require_relative "timeouts"
|
|
3
|
+
require_relative "adapters/connect_aware_net_http"
|
|
2
4
|
require "json"
|
|
3
5
|
require "time"
|
|
4
6
|
require_relative "errors"
|
|
@@ -27,10 +29,10 @@ module PineSandbox
|
|
|
27
29
|
# public operation table) is sent on a transient transport failure
|
|
28
30
|
# (connection reset / timeout). clock drives deadlines, backoff and jitter;
|
|
29
31
|
# sleeper, when given, replaces only its waits (tests).
|
|
30
|
-
attr_reader :clock
|
|
32
|
+
attr_reader :clock, :request_timeout
|
|
31
33
|
|
|
32
34
|
def initialize(control_host:, token_source:, protocol: "https", spec_version: 1,
|
|
33
|
-
open_timeout:
|
|
35
|
+
open_timeout: Timeouts::DEFAULT_CONNECT, request_timeout: Timeouts::DEFAULT_REQUEST, connection: nil,
|
|
34
36
|
max_transient_attempts: RetryPolicy::TRANSIENT_ATTEMPTS, sleeper: nil, gateway_url: nil,
|
|
35
37
|
clock: PineSandbox::SYSTEM_CLOCK)
|
|
36
38
|
@control_host = control_host.to_s
|
|
@@ -48,14 +50,16 @@ module PineSandbox
|
|
|
48
50
|
|
|
49
51
|
# Internal adapter for the public Computer contract. No generic sandbox
|
|
50
52
|
# create, image selection or task dispatch is available through this client.
|
|
51
|
-
|
|
53
|
+
# reached, when given, is called for each send that may have reached the server.
|
|
54
|
+
def create_computer(body:, request_timeout: nil, reached: nil)
|
|
52
55
|
# A create ID permits identical retries without extending the Computer's expiry.
|
|
53
56
|
keyed = !!body[:create_id]
|
|
54
57
|
deadline = monotonic_now + request_timeout if request_timeout
|
|
55
58
|
busy_repeats = 0
|
|
56
59
|
loop do
|
|
57
60
|
resp = authed(:post, "computer-sandboxes", retry_transient: keyed && replay_safe?("createComputer"),
|
|
58
|
-
request_timeout: deadline && remaining_timeout(deadline)
|
|
61
|
+
request_timeout: deadline && remaining_timeout(deadline),
|
|
62
|
+
reached: reached) do |req|
|
|
59
63
|
req.headers["Content-Type"] = "application/json"
|
|
60
64
|
req.body = JSON.generate(body)
|
|
61
65
|
end
|
|
@@ -161,20 +165,21 @@ module PineSandbox
|
|
|
161
165
|
# is safe even for create. When retry_transient is set (idempotent reads + keyed
|
|
162
166
|
# creates — see the call sites), a transient transport failure (connection reset /
|
|
163
167
|
# timeout) is also re-sent with bounded backoff. Legacy unkeyed creates
|
|
164
|
-
# remain single-shot on transport failure
|
|
165
|
-
|
|
168
|
+
# remain single-shot on transport failure, unless the connection could not
|
|
169
|
+
# be opened: then nothing was sent and any request is tried again.
|
|
170
|
+
def authed(method, path, retry_transient: false, request_timeout: nil, reached: nil, &block)
|
|
166
171
|
attempt = 0
|
|
167
172
|
deadline = monotonic_now + request_timeout if request_timeout
|
|
168
173
|
begin
|
|
169
174
|
remaining_timeout(deadline)
|
|
170
|
-
resp = send_request(method, path, @token_source.token, deadline: deadline, &block)
|
|
175
|
+
resp = send_request(method, path, @token_source.token, deadline: deadline, reached: reached, &block)
|
|
171
176
|
return resp unless resp.status == 401
|
|
172
177
|
|
|
173
178
|
remaining_timeout(deadline)
|
|
174
|
-
send_request(method, path, @token_source.token(force_refresh: true), deadline: deadline, &block)
|
|
175
|
-
rescue PineSandbox::ConnectionError, PineSandbox::TimeoutError
|
|
179
|
+
send_request(method, path, @token_source.token(force_refresh: true), deadline: deadline, reached: reached, &block)
|
|
180
|
+
rescue PineSandbox::ConnectionError, PineSandbox::TimeoutError => e
|
|
176
181
|
attempt += 1
|
|
177
|
-
raise unless retry_transient && attempt < @max_transient_attempts
|
|
182
|
+
raise unless (retry_transient || not_sent?(e)) && attempt < @max_transient_attempts
|
|
178
183
|
delay = transient_backoff(attempt)
|
|
179
184
|
delay = [delay, remaining_timeout(deadline)].min if deadline
|
|
180
185
|
@sleeper.call(delay)
|
|
@@ -187,9 +192,9 @@ module PineSandbox
|
|
|
187
192
|
RetryPolicy.transient_backoff(attempt, @clock)
|
|
188
193
|
end
|
|
189
194
|
|
|
190
|
-
def send_request(method, path, token, deadline: nil, &block)
|
|
195
|
+
def send_request(method, path, token, deadline: nil, reached: nil, &block)
|
|
191
196
|
remaining = remaining_timeout(deadline)
|
|
192
|
-
connection.public_send(method, path) do |req|
|
|
197
|
+
response = connection.public_send(method, path) do |req|
|
|
193
198
|
if remaining
|
|
194
199
|
req.options.timeout = remaining
|
|
195
200
|
req.options.open_timeout = [@open_timeout, remaining].min
|
|
@@ -198,16 +203,25 @@ module PineSandbox
|
|
|
198
203
|
req.headers["Accept"] = "application/json"
|
|
199
204
|
block&.call(req)
|
|
200
205
|
end
|
|
206
|
+
reached&.call
|
|
207
|
+
response
|
|
201
208
|
rescue Faraday::TimeoutError => e
|
|
209
|
+
reached&.call
|
|
202
210
|
raise PineSandbox::TimeoutError,
|
|
203
211
|
PineSandbox.with_context("control plane request timed out: #{e.message}",
|
|
204
212
|
op: "#{method.upcase} /#{path.to_s.split("?").first}")
|
|
205
213
|
rescue Faraday::ConnectionFailed => e
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
214
|
+
error = PineSandbox::ConnectionError.new(
|
|
215
|
+
PineSandbox.with_context("control plane request failed: #{e.message}",
|
|
216
|
+
op: "#{method.upcase} /#{path.to_s.split("?").first}")
|
|
217
|
+
)
|
|
218
|
+
error.not_sent = Adapters::ConnectAwareNetHttp.not_sent?(e)
|
|
219
|
+
reached&.call unless error.not_sent?
|
|
220
|
+
raise error
|
|
209
221
|
end
|
|
210
222
|
|
|
223
|
+
def not_sent?(error) = error.is_a?(PineSandbox::ConnectionError) && error.not_sent?
|
|
224
|
+
|
|
211
225
|
def remaining_timeout(deadline)
|
|
212
226
|
return unless deadline
|
|
213
227
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
require "faraday"
|
|
2
|
+
require_relative "timeouts"
|
|
3
|
+
require_relative "adapters/connect_aware_net_http"
|
|
2
4
|
require "base64"
|
|
3
5
|
require "json"
|
|
4
6
|
require "time"
|
|
@@ -26,7 +28,7 @@ module PineSandbox
|
|
|
26
28
|
def initialize(api_key:, host:, protocol: "https",
|
|
27
29
|
clock: -> { Time.now }, sleeper: ->(s) { Kernel.sleep(s) }, random: -> { Kernel.rand },
|
|
28
30
|
skew: DEFAULT_SKEW_SECONDS, max_attempts: DEFAULT_MAX_ATTEMPTS,
|
|
29
|
-
open_timeout:
|
|
31
|
+
open_timeout: Timeouts::DEFAULT_CONNECT, request_timeout: Timeouts::DEFAULT_REQUEST, connection: nil, gateway_url: nil)
|
|
30
32
|
raise ArgumentError, "api_key required (a pk_ client key)" if api_key.to_s.empty?
|
|
31
33
|
|
|
32
34
|
@api_key = api_key.to_s
|
|
@@ -2,6 +2,7 @@ require "faraday"
|
|
|
2
2
|
require "json"
|
|
3
3
|
require "time"
|
|
4
4
|
require_relative "errors"
|
|
5
|
+
require_relative "timeouts"
|
|
5
6
|
|
|
6
7
|
module PineSandbox
|
|
7
8
|
# Hash-compatible public payload with per-request scheduling metadata.
|
|
@@ -17,11 +18,11 @@ module PineSandbox
|
|
|
17
18
|
|
|
18
19
|
# @api private — the Client exposes the project-scoped diagnostics operations.
|
|
19
20
|
class Diagnostics
|
|
20
|
-
def initialize(api_key:, zone:)
|
|
21
|
+
def initialize(api_key:, zone:, timeouts: Timeouts::DEFAULT)
|
|
21
22
|
@api_key = api_key
|
|
22
23
|
@connection = zone.faraday(zone.control_host) do |f|
|
|
23
|
-
f.options.open_timeout =
|
|
24
|
-
f.options.timeout =
|
|
24
|
+
f.options.open_timeout = timeouts.connect
|
|
25
|
+
f.options.timeout = timeouts.request
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
|