pinesandbox 0.3.20 → 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 +92 -30
- data/lib/pinesandbox/adapters/connect_aware_net_http.rb +32 -0
- data/lib/pinesandbox/adapters/coordinator_adapter.rb +127 -60
- data/lib/pinesandbox/agent_mode.rb +32 -18
- data/lib/pinesandbox/agent_turn.rb +1 -1
- data/lib/pinesandbox/artifacts.rb +18 -9
- data/lib/pinesandbox/attach_credentials_source.rb +55 -12
- data/lib/pinesandbox/author_terminal_translator.rb +12 -11
- data/lib/pinesandbox/client.rb +68 -30
- data/lib/pinesandbox/computer.rb +139 -69
- 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 +4 -0
- data/lib/pinesandbox/errors.rb +141 -12
- data/lib/pinesandbox/response_handler.rb +4 -1
- data/lib/pinesandbox/runtime_admission.rb +112 -0
- data/lib/pinesandbox/sandbox_handle.rb +71 -40
- data/lib/pinesandbox/session.rb +4 -0
- data/lib/pinesandbox/terminal_reason.rb +31 -0
- data/lib/pinesandbox/timeouts.rb +22 -0
- data/lib/pinesandbox/version.rb +1 -1
- data/lib/pinesandbox/zone.rb +3 -0
- data/lib/pinesandbox.rb +2 -0
- metadata +5 -1
|
@@ -45,22 +45,25 @@ module PineSandbox
|
|
|
45
45
|
# it saved to get the same turn back. When no response arrives within the
|
|
46
46
|
# resend budget, #run raises the last failure — a ConnectionError,
|
|
47
47
|
# TimeoutError or ApiError — extended with RunUnconfirmedError, with
|
|
48
|
-
# #idempotency_key naming the key to send it again with.
|
|
48
|
+
# #idempotency_key naming the key to send it again with. After a lost
|
|
49
|
+
# response, any error a later send raises is extended the same way, a
|
|
50
|
+
# SandboxGoneError included, so rescue RunUnconfirmedError first.
|
|
49
51
|
#
|
|
50
|
-
# secrets: optional name→value secrets
|
|
51
|
-
#
|
|
52
|
-
#
|
|
53
|
-
# See the spec's SessionSecrets schema for the naming rules and lifecycle
|
|
54
|
-
# (ephemeral; re-send after re-attach).
|
|
52
|
+
# secrets: optional name→value secrets. The agent can discover the supplied names.
|
|
53
|
+
# See the spec's SessionSecrets schema for limits, lifetime and privacy
|
|
54
|
+
# boundaries; later tool output can still expose values to the model.
|
|
55
55
|
def run(goal, context: nil, skills: nil, constraints: nil, secrets: nil, idempotency_key: nil)
|
|
56
56
|
idempotency_key ||= SecureRandom.uuid_v7
|
|
57
|
-
|
|
57
|
+
sends = 0
|
|
58
|
+
send_turn do |timeout|
|
|
58
59
|
coordinator.agent_run(token: ct, name: name, goal: goal,
|
|
59
60
|
context: context, skills: skills, constraints: constraints,
|
|
60
|
-
secrets: secrets, idempotency_key: idempotency_key
|
|
61
|
+
secrets: secrets, idempotency_key: idempotency_key, timeout: timeout,
|
|
62
|
+
reached: -> { sends += 1 })
|
|
61
63
|
end
|
|
62
|
-
rescue
|
|
63
|
-
|
|
64
|
+
rescue Error => e
|
|
65
|
+
# A later send's answer can't show what an earlier one whose response was lost did.
|
|
66
|
+
if lost_response?(e) || sends > 1
|
|
64
67
|
e.idempotency_key = idempotency_key
|
|
65
68
|
e.extend(RunUnconfirmedError)
|
|
66
69
|
end
|
|
@@ -74,13 +77,17 @@ module PineSandbox
|
|
|
74
77
|
# "running" or "canceling"; timeout 0 reads it once. Waiting never cancels
|
|
75
78
|
# the turn or follows its next turn. A read whose response was lost (a
|
|
76
79
|
# network fault, a 408, or a gateway 502, 503 or 504), or whose problem is
|
|
77
|
-
# marked retryable, is read again until the timeout;
|
|
78
|
-
#
|
|
79
|
-
#
|
|
80
|
-
#
|
|
80
|
+
# marked retryable, is read again until the timeout; then wait returns the
|
|
81
|
+
# turn as it last read it, or raises the last error when no read came
|
|
82
|
+
# back. Such an error means this process lost the Computer, not that the
|
|
83
|
+
# turn failed: the turn keeps running, so wait again rather than stopping
|
|
84
|
+
# the Computer. Any other error raises, including a missing or expired
|
|
85
|
+
# turn. A read that comes back sooner than it asked the Computer to hold
|
|
86
|
+
# is followed by a pause of up to a second, so waiting never spins.
|
|
81
87
|
def wait(turn_id:, timeout:)
|
|
82
88
|
deadline = monotonic + timeout
|
|
83
89
|
failures = 0
|
|
90
|
+
last = nil
|
|
84
91
|
loop do
|
|
85
92
|
hold = (deadline - monotonic).clamp(0, WAIT_HOLD_SECONDS).floor
|
|
86
93
|
started = monotonic
|
|
@@ -88,13 +95,16 @@ module PineSandbox
|
|
|
88
95
|
turn = coordinator.agent_turn(token: read_token, name: name, turn_id: turn_id, wait_seconds: hold)
|
|
89
96
|
rescue TimeoutError, ConnectionError, ApiError => e
|
|
90
97
|
retryable = lost_response?(e) || (e.is_a?(ApiError) && e.retryable? == true)
|
|
91
|
-
raise unless retryable
|
|
98
|
+
raise unless retryable
|
|
99
|
+
return AgentTurn.wrap(last) if last && monotonic >= deadline
|
|
100
|
+
raise unless monotonic < deadline
|
|
92
101
|
|
|
93
102
|
failures += 1
|
|
94
103
|
sleep([retry_delay(failures, e), deadline - monotonic].min)
|
|
95
104
|
next
|
|
96
105
|
end
|
|
97
106
|
failures = 0
|
|
107
|
+
last = turn
|
|
98
108
|
return AgentTurn.wrap(turn) unless WAITING_STATES.include?(turn["state"]) && monotonic < deadline
|
|
99
109
|
|
|
100
110
|
# The Computer holds whole seconds, and may answer before the hold
|
|
@@ -113,7 +123,9 @@ module PineSandbox
|
|
|
113
123
|
# leaves the question open), so the answer can reference a password or
|
|
114
124
|
# code as $NAME.
|
|
115
125
|
def answer(turn_id:, answer:, secrets: nil)
|
|
116
|
-
send_turn
|
|
126
|
+
send_turn do |timeout|
|
|
127
|
+
coordinator.agent_answer(token: ct, name: name, turn_id: turn_id, answer: answer, secrets: secrets, timeout: timeout)
|
|
128
|
+
end
|
|
117
129
|
end
|
|
118
130
|
|
|
119
131
|
# steer(turn_id:, text:) — add guidance to turn_id while it runs. Returns
|
|
@@ -132,7 +144,7 @@ module PineSandbox
|
|
|
132
144
|
# answered turn raises a 409 (/errors/stale-turn): cancel the turn its
|
|
133
145
|
# answer opened instead.
|
|
134
146
|
def cancel(turn_id:)
|
|
135
|
-
send_turn { coordinator.agent_cancel(token: ct, name: name, turn_id: turn_id) }
|
|
147
|
+
send_turn { |timeout| coordinator.agent_cancel(token: ct, name: name, turn_id: turn_id, timeout: timeout) }
|
|
136
148
|
end
|
|
137
149
|
|
|
138
150
|
# reset — forget the agent's thread so the next run starts on a fresh memory
|
|
@@ -185,11 +197,13 @@ module PineSandbox
|
|
|
185
197
|
|
|
186
198
|
# Send a mutation whose repeat names the same turn, resending it after a
|
|
187
199
|
# lost response, and wrap the turn it returns.
|
|
200
|
+
# Yields the timeout for this send: what is left of the budget, at most the
|
|
201
|
+
# client's request timeout, so no send outlasts the budget.
|
|
188
202
|
def send_turn
|
|
189
203
|
attempt = 0
|
|
190
204
|
started = monotonic
|
|
191
205
|
begin
|
|
192
|
-
AgentTurn.wrap(yield)
|
|
206
|
+
AgentTurn.wrap(yield([RESEND_BUDGET_SECONDS - (monotonic - started), coordinator.timeouts.request].min))
|
|
193
207
|
rescue TimeoutError, ConnectionError, ApiError => e
|
|
194
208
|
raise unless lost_response?(e)
|
|
195
209
|
|
|
@@ -57,7 +57,7 @@ module PineSandbox
|
|
|
57
57
|
# turn did not end with one.
|
|
58
58
|
def question
|
|
59
59
|
ended = result
|
|
60
|
-
ended.is_a?(Hash) && ended["terminal_reason"] ==
|
|
60
|
+
ended.is_a?(Hash) && ended["terminal_reason"] == TerminalReason::NEEDS_INPUT ? ended["summary"] : nil
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
def started_at = parse_time(self["started_at"])
|
|
@@ -12,6 +12,8 @@ require_relative "response_handler"
|
|
|
12
12
|
require_relative "streamed_read"
|
|
13
13
|
require_relative "version"
|
|
14
14
|
require_relative "models/artifact_file"
|
|
15
|
+
require_relative "timeouts"
|
|
16
|
+
require_relative "adapters/connect_aware_net_http"
|
|
15
17
|
|
|
16
18
|
module PineSandbox
|
|
17
19
|
# @api private — what a path in a Computer's artifacts may be, checked the
|
|
@@ -193,10 +195,11 @@ module PineSandbox
|
|
|
193
195
|
|
|
194
196
|
# @api private — Client#artifacts builds the handle. `token_source` is
|
|
195
197
|
# the project-key Portal client (#artifact_token).
|
|
196
|
-
def initialize(computer_id:, token_source:, clock: PineSandbox::SYSTEM_CLOCK)
|
|
198
|
+
def initialize(computer_id:, token_source:, clock: PineSandbox::SYSTEM_CLOCK, timeouts: Timeouts::DEFAULT)
|
|
197
199
|
@computer_id = computer_id
|
|
198
200
|
@token_source = token_source
|
|
199
201
|
@clock = clock
|
|
202
|
+
@timeouts = timeouts
|
|
200
203
|
@tokens = {}
|
|
201
204
|
@connections = {}
|
|
202
205
|
@mutex = Mutex.new
|
|
@@ -420,9 +423,9 @@ module PineSandbox
|
|
|
420
423
|
@mutex.synchronize do
|
|
421
424
|
@connections[origin] ||= Faraday.new(url: origin) do |conn|
|
|
422
425
|
conn.headers["User-Agent"] = PineSandbox::USER_AGENT
|
|
423
|
-
conn.options.open_timeout =
|
|
424
|
-
conn.options.timeout =
|
|
425
|
-
conn.adapter
|
|
426
|
+
conn.options.open_timeout = @timeouts.connect
|
|
427
|
+
conn.options.timeout = @timeouts.request
|
|
428
|
+
conn.adapter Adapters::ConnectAwareNetHttp
|
|
426
429
|
end
|
|
427
430
|
end
|
|
428
431
|
end
|
|
@@ -447,15 +450,19 @@ module PineSandbox
|
|
|
447
450
|
# Runs the block, a request safe to send again, once more after each
|
|
448
451
|
# transport fault or answer the storage retry rule allows, with
|
|
449
452
|
# RetryPolicy's transient backoff, up to RetryPolicy::TRANSIENT_ATTEMPTS
|
|
450
|
-
# sends, and returns the last answer. Yields whether
|
|
451
|
-
#
|
|
453
|
+
# sends, and returns the last answer. Yields whether an earlier send may
|
|
454
|
+
# have reached storage, so this one may find its work already done; one
|
|
455
|
+
# whose connection never opened did not.
|
|
452
456
|
def resending
|
|
453
457
|
attempt = 0
|
|
458
|
+
reached = false
|
|
454
459
|
loop do
|
|
455
460
|
attempt += 1
|
|
456
461
|
begin
|
|
457
|
-
response = yield
|
|
458
|
-
|
|
462
|
+
response = yield reached
|
|
463
|
+
reached = true
|
|
464
|
+
rescue PineSandbox::ConnectionError, PineSandbox::TimeoutError => e
|
|
465
|
+
reached ||= !(e.is_a?(PineSandbox::ConnectionError) && e.not_sent?)
|
|
459
466
|
raise if attempt >= RetryPolicy::TRANSIENT_ATTEMPTS
|
|
460
467
|
else
|
|
461
468
|
return response unless retry_status?(response.status) && attempt < RetryPolicy::TRANSIENT_ATTEMPTS
|
|
@@ -709,7 +716,9 @@ module PineSandbox
|
|
|
709
716
|
rescue Faraday::TimeoutError => e
|
|
710
717
|
raise PineSandbox::TimeoutError, PineSandbox.with_context("storage request timed out: #{e.message}", op: op)
|
|
711
718
|
rescue Faraday::ConnectionFailed, Faraday::SSLError => e
|
|
712
|
-
|
|
719
|
+
error = PineSandbox::ConnectionError.new(PineSandbox.with_context("storage connection failed: #{e.message}", op: op))
|
|
720
|
+
error.not_sent = Adapters::ConnectAwareNetHttp.not_sent?(e)
|
|
721
|
+
raise error
|
|
713
722
|
end
|
|
714
723
|
|
|
715
724
|
# Raises the error for a storage answer that isn't a success, of the
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
require "faraday"
|
|
2
2
|
require "json"
|
|
3
3
|
require "time"
|
|
4
|
+
require_relative "clock"
|
|
4
5
|
require_relative "errors"
|
|
6
|
+
require_relative "timeouts"
|
|
7
|
+
require_relative "adapters/connect_aware_net_http"
|
|
5
8
|
|
|
6
9
|
module PineSandbox
|
|
7
10
|
# The pk_-backed attach-credential provider — the DEFAULT attach path
|
|
@@ -32,7 +35,8 @@ module PineSandbox
|
|
|
32
35
|
LOCATIONS_PATH = "/v1/locations"
|
|
33
36
|
|
|
34
37
|
def initialize(api_key:, host:, protocol: "https",
|
|
35
|
-
open_timeout:
|
|
38
|
+
open_timeout: Timeouts::DEFAULT_CONNECT, request_timeout: Timeouts::DEFAULT_REQUEST, connection: nil, gateway_url: nil,
|
|
39
|
+
clock: PineSandbox::SYSTEM_CLOCK)
|
|
36
40
|
raise ArgumentError, "api_key required (a pk_ client key)" if api_key.to_s.empty?
|
|
37
41
|
|
|
38
42
|
@api_key = api_key.to_s
|
|
@@ -42,6 +46,7 @@ module PineSandbox
|
|
|
42
46
|
@open_timeout = open_timeout
|
|
43
47
|
@request_timeout = request_timeout
|
|
44
48
|
@connection = connection
|
|
49
|
+
@clock = clock
|
|
45
50
|
end
|
|
46
51
|
|
|
47
52
|
# Register `computer_id` into the portal ownership registry (idempotent —
|
|
@@ -125,8 +130,9 @@ module PineSandbox
|
|
|
125
130
|
end
|
|
126
131
|
|
|
127
132
|
# One authorization-registry read before Start; never a Computer status poll.
|
|
128
|
-
|
|
129
|
-
|
|
133
|
+
# request_timeout (seconds) bounds it, retries included.
|
|
134
|
+
def current_binding(computer_id, request_timeout: nil)
|
|
135
|
+
resp = get("#{REGISTER_PATH}/#{computer_id}", op: "computer binding lookup", request_timeout: request_timeout)
|
|
130
136
|
if resp.status == 404
|
|
131
137
|
raise UnknownComputerError.new("Computer not found", **error_attributes(resp))
|
|
132
138
|
end
|
|
@@ -353,8 +359,8 @@ module PineSandbox
|
|
|
353
359
|
end
|
|
354
360
|
end
|
|
355
361
|
|
|
356
|
-
def get(path, op:)
|
|
357
|
-
request(:get, path, op: op) do |req|
|
|
362
|
+
def get(path, op:, request_timeout: nil)
|
|
363
|
+
request(:get, path, op: op, request_timeout: request_timeout) do |req|
|
|
358
364
|
req.headers["Authorization"] = "Bearer #{@api_key}"
|
|
359
365
|
req.headers["Accept"] = "application/json"
|
|
360
366
|
end
|
|
@@ -367,8 +373,32 @@ module PineSandbox
|
|
|
367
373
|
end
|
|
368
374
|
end
|
|
369
375
|
|
|
370
|
-
|
|
371
|
-
|
|
376
|
+
# A request whose connection could not be opened was not sent, so it is
|
|
377
|
+
# tried again on the transient backoff; any other fault raises.
|
|
378
|
+
def request(method, path, op:, request_timeout: nil, &block)
|
|
379
|
+
attempt = 0
|
|
380
|
+
deadline = @clock.monotonic + request_timeout if request_timeout
|
|
381
|
+
begin
|
|
382
|
+
attempt += 1
|
|
383
|
+
send_once(method, path, op: op, deadline: deadline, &block)
|
|
384
|
+
rescue PineSandbox::ConnectionError => e
|
|
385
|
+
raise unless e.not_sent? && attempt < RetryPolicy::TRANSIENT_ATTEMPTS
|
|
386
|
+
|
|
387
|
+
delay = RetryPolicy.transient_backoff(attempt, @clock)
|
|
388
|
+
@clock.sleep(deadline ? [delay, time_left(deadline, method, path, op)].min : delay)
|
|
389
|
+
retry
|
|
390
|
+
end
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
def send_once(method, path, op:, deadline: nil)
|
|
394
|
+
left = deadline && time_left(deadline, method, path, op)
|
|
395
|
+
connection.public_send(method, path) do |req|
|
|
396
|
+
if left
|
|
397
|
+
req.options.timeout = left
|
|
398
|
+
req.options.open_timeout = [@open_timeout, left].min
|
|
399
|
+
end
|
|
400
|
+
yield req
|
|
401
|
+
end
|
|
372
402
|
rescue Faraday::TimeoutError => e
|
|
373
403
|
raise PineSandbox::TimeoutError,
|
|
374
404
|
PineSandbox.with_context(
|
|
@@ -376,11 +406,23 @@ module PineSandbox
|
|
|
376
406
|
op: "#{method.to_s.upcase} #{path.split('?').first}"
|
|
377
407
|
)
|
|
378
408
|
rescue Faraday::ConnectionFailed => e
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
409
|
+
error = PineSandbox::ConnectionError.new(
|
|
410
|
+
PineSandbox.with_context(
|
|
411
|
+
"portal #{op} connection failed: #{e.message}",
|
|
412
|
+
op: "#{method.to_s.upcase} #{path.split('?').first}"
|
|
413
|
+
)
|
|
414
|
+
)
|
|
415
|
+
error.not_sent = Adapters::ConnectAwareNetHttp.not_sent?(e)
|
|
416
|
+
raise error
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def time_left(deadline, method, path, op)
|
|
420
|
+
left = deadline - @clock.monotonic
|
|
421
|
+
return left if left.positive?
|
|
422
|
+
|
|
423
|
+
raise PineSandbox::TimeoutError,
|
|
424
|
+
PineSandbox.with_context("portal #{op} call timed out: deadline exceeded",
|
|
425
|
+
op: "#{method.to_s.upcase} #{path.split('?').first}")
|
|
384
426
|
end
|
|
385
427
|
|
|
386
428
|
def request_id(resp)
|
|
@@ -426,6 +468,7 @@ module PineSandbox
|
|
|
426
468
|
|
|
427
469
|
def connection
|
|
428
470
|
@connection ||= Zone.faraday(scheme: @protocol, host: @host, gateway_url: @gateway_url) do |f|
|
|
471
|
+
f.headers["User-Agent"] = PineSandbox::USER_AGENT
|
|
429
472
|
f.options.open_timeout = @open_timeout
|
|
430
473
|
f.options.timeout = @request_timeout
|
|
431
474
|
end
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require_relative "errors"
|
|
2
|
+
require_relative "terminal_reason"
|
|
2
3
|
|
|
3
4
|
module PineSandbox
|
|
4
5
|
# Maps a streaming authoring run's terminal Result (the last event of
|
|
@@ -9,16 +10,16 @@ module PineSandbox
|
|
|
9
10
|
# values below; the optional `author` object carries the typed
|
|
10
11
|
# authoring outcome.
|
|
11
12
|
module AuthorTerminalTranslator
|
|
12
|
-
AUTHOR_TERMINAL_DRAFT_REGISTERED =
|
|
13
|
-
AUTHOR_TERMINAL_CLARIFICATION_REQUEST =
|
|
14
|
-
AUTHOR_TERMINAL_NOT_REUSABLE =
|
|
15
|
-
AUTHOR_TERMINAL_REGISTRATION_FAILED =
|
|
16
|
-
AUTHOR_TERMINAL_CANCELED =
|
|
17
|
-
AUTHOR_TERMINAL_FAILED =
|
|
13
|
+
AUTHOR_TERMINAL_DRAFT_REGISTERED = TerminalReason::DRAFT_REGISTERED
|
|
14
|
+
AUTHOR_TERMINAL_CLARIFICATION_REQUEST = TerminalReason::CLARIFICATION_REQUESTED
|
|
15
|
+
AUTHOR_TERMINAL_NOT_REUSABLE = TerminalReason::NOT_REUSABLE
|
|
16
|
+
AUTHOR_TERMINAL_REGISTRATION_FAILED = TerminalReason::REGISTRATION_FAILED
|
|
17
|
+
AUTHOR_TERMINAL_CANCELED = TerminalReason::CANCELED
|
|
18
|
+
AUTHOR_TERMINAL_FAILED = TerminalReason::FAILED
|
|
18
19
|
# Terminal reason when the Computer's services restarted while the run
|
|
19
20
|
# was in flight. Surfaced to the caller as an Error so they can retry —
|
|
20
21
|
# the authoring itself didn't fail; the run got interrupted.
|
|
21
|
-
AUTHOR_TERMINAL_COORDINATOR_RESTARTED =
|
|
22
|
+
AUTHOR_TERMINAL_COORDINATOR_RESTARTED = TerminalReason::COORDINATOR_RESTARTED
|
|
22
23
|
|
|
23
24
|
module_function
|
|
24
25
|
|
|
@@ -43,9 +44,9 @@ module PineSandbox
|
|
|
43
44
|
{ "learned" => false, "reason" => "the run was too ambiguous to author a reliable skill — teach it instead" }
|
|
44
45
|
when AUTHOR_TERMINAL_NOT_REUSABLE
|
|
45
46
|
{ "learned" => false, "reason" => summary || "the navigation is not a reusable skill" }
|
|
46
|
-
when
|
|
47
|
+
when *TerminalReason::AUTHOR_FAILED
|
|
47
48
|
raise PineSandbox::Error, "author run #{reason}: #{summary}"
|
|
48
|
-
when
|
|
49
|
+
when *TerminalReason::AUTHOR_INTERRUPTED
|
|
49
50
|
raise PineSandbox::Error,
|
|
50
51
|
"author run interrupted because the Computer's services restarted — retry the learn call"
|
|
51
52
|
else
|
|
@@ -82,9 +83,9 @@ module PineSandbox
|
|
|
82
83
|
}
|
|
83
84
|
when AUTHOR_TERMINAL_NOT_REUSABLE
|
|
84
85
|
{ "taught" => false, "reason" => summary || "the demonstration is not a reusable skill" }
|
|
85
|
-
when
|
|
86
|
+
when *TerminalReason::AUTHOR_FAILED
|
|
86
87
|
raise PineSandbox::Error, "author run #{reason}: #{summary}"
|
|
87
|
-
when
|
|
88
|
+
when *TerminalReason::AUTHOR_INTERRUPTED
|
|
88
89
|
raise PineSandbox::Error,
|
|
89
90
|
"author run interrupted because the Computer's services restarted — retry the teach call"
|
|
90
91
|
else
|
data/lib/pinesandbox/client.rb
CHANGED
|
@@ -91,14 +91,24 @@ module PineSandbox
|
|
|
91
91
|
alias_method :to_s, :inspect
|
|
92
92
|
end
|
|
93
93
|
|
|
94
|
+
# `connect_timeout:` bounds opening one connection (the TCP connect, then
|
|
95
|
+
# the TLS handshake), default 10 seconds; a connection that could not be
|
|
96
|
+
# opened sent nothing, so the request is tried again. `request_timeout:`
|
|
97
|
+
# bounds one request that has no budget of its own, default 30 seconds; a
|
|
98
|
+
# turn wait adds the time the Computer holds it. Raise them for a slow or
|
|
99
|
+
# distant network.
|
|
100
|
+
#
|
|
94
101
|
# `clock:` (@api private) replaces the SDK's view of time — every wait,
|
|
95
102
|
# deadline and jitter — for tests that run retries on a virtual clock.
|
|
96
103
|
def initialize(endpoint: nil, api_key: nil,
|
|
97
104
|
control_host: nil, spec_version: DelegatedConnection::SPEC_VERSION,
|
|
98
105
|
control_token_provider: nil, attach_credentials_provider: nil,
|
|
99
|
-
gateway_url: nil,
|
|
106
|
+
gateway_url: nil, connect_timeout: Timeouts::DEFAULT_CONNECT,
|
|
107
|
+
request_timeout: Timeouts::DEFAULT_REQUEST, clock: PineSandbox::SYSTEM_CLOCK)
|
|
100
108
|
raise ArgumentError, "endpoint required (e.g. \"https://pinesandbox.io\")" if endpoint.nil?
|
|
101
109
|
|
|
110
|
+
@timeouts = Timeouts.new(connect: connect_timeout, request: request_timeout)
|
|
111
|
+
|
|
102
112
|
@zone =
|
|
103
113
|
if endpoint.is_a?(PineSandbox::Zone)
|
|
104
114
|
raise ArgumentError, "pass gateway_url to the Zone, not with one" unless gateway_url.nil?
|
|
@@ -109,7 +119,7 @@ module PineSandbox
|
|
|
109
119
|
end
|
|
110
120
|
@spec_version = spec_version
|
|
111
121
|
@clock = clock
|
|
112
|
-
@diagnostics = Diagnostics.new(api_key: api_key, zone: @zone) unless api_key.to_s.empty?
|
|
122
|
+
@diagnostics = Diagnostics.new(api_key: api_key, zone: @zone, timeouts: @timeouts) unless api_key.to_s.empty?
|
|
113
123
|
|
|
114
124
|
token_source =
|
|
115
125
|
if control_token_provider
|
|
@@ -118,7 +128,8 @@ module PineSandbox
|
|
|
118
128
|
raise ArgumentError, "api_key required (a pk_ client key) unless control_token_provider is given" if api_key.to_s.empty?
|
|
119
129
|
|
|
120
130
|
ControlTokenSource.new(api_key: api_key, host: @zone.control_host, protocol: @zone.http_scheme,
|
|
121
|
-
gateway_url: @zone.gateway_url,
|
|
131
|
+
gateway_url: @zone.gateway_url, open_timeout: @timeouts.connect,
|
|
132
|
+
request_timeout: @timeouts.request, clock: -> { clock.now },
|
|
122
133
|
sleeper: ->(seconds) { clock.sleep(seconds) }, random: -> { clock.rand })
|
|
123
134
|
end
|
|
124
135
|
|
|
@@ -133,16 +144,18 @@ module PineSandbox
|
|
|
133
144
|
end
|
|
134
145
|
elsif !api_key.to_s.empty?
|
|
135
146
|
AttachCredentialsSource.new(api_key: api_key, host: @zone.control_host, protocol: @zone.http_scheme,
|
|
136
|
-
gateway_url: @zone.gateway_url
|
|
147
|
+
gateway_url: @zone.gateway_url, open_timeout: @timeouts.connect,
|
|
148
|
+
request_timeout: @timeouts.request, clock: clock)
|
|
137
149
|
else
|
|
138
150
|
raise ArgumentError, "attach credentials unavailable: pass api_key (a pk_ client key) or attach_credentials_provider:"
|
|
139
151
|
end
|
|
140
152
|
|
|
141
153
|
@control_plane = ControlPlane.new(control_host: @zone.control_host, token_source: token_source,
|
|
142
154
|
protocol: @zone.http_scheme, spec_version: @spec_version,
|
|
143
|
-
gateway_url: @zone.gateway_url,
|
|
155
|
+
gateway_url: @zone.gateway_url, open_timeout: @timeouts.connect,
|
|
156
|
+
request_timeout: @timeouts.request, clock: clock)
|
|
144
157
|
@connection = Connection.new(attach_provider: attach_provider, zone: @zone, control_plane: @control_plane,
|
|
145
|
-
clock: clock)
|
|
158
|
+
clock: clock, timeouts: @timeouts)
|
|
146
159
|
end
|
|
147
160
|
|
|
148
161
|
# @api private — retained for compatibility; applications use Computer operations.
|
|
@@ -196,7 +209,7 @@ module PineSandbox
|
|
|
196
209
|
provider = @connection.attach_provider
|
|
197
210
|
raise ArgumentError, "artifacts require a project API key provider" unless provider.respond_to?(:artifact_token)
|
|
198
211
|
|
|
199
|
-
Artifacts.new(computer_id: computer_id, token_source: provider, clock: @clock)
|
|
212
|
+
Artifacts.new(computer_id: computer_id, token_source: provider, clock: @clock, timeouts: @timeouts)
|
|
200
213
|
end
|
|
201
214
|
|
|
202
215
|
# PERMANENTLY delete a Computer: its identity and saved state end (stop
|
|
@@ -282,9 +295,14 @@ module PineSandbox
|
|
|
282
295
|
start_existing_computer(*args, **options, recover: false)
|
|
283
296
|
end
|
|
284
297
|
|
|
285
|
-
#
|
|
286
|
-
#
|
|
287
|
-
#
|
|
298
|
+
# Stops the runtime that the Computer's binding points to when it's called,
|
|
299
|
+
# which may be a runtime another worker has just started. Use it only to
|
|
300
|
+
# recover after losing the Computer handle, and run the recoveries of one
|
|
301
|
+
# Computer one at a time. Returns true when that runtime is no longer
|
|
302
|
+
# running, or when there was nothing to stop. true does not confirm that the
|
|
303
|
+
# final save succeeded. Returns false when the runtime did not stop within
|
|
304
|
+
# wait_timeout (seconds, default 390; 0 skips only the wait). When
|
|
305
|
+
# Computer#stop returns false, call stop on that handle again instead.
|
|
288
306
|
def stop_computer(computer_id, **wait_options)
|
|
289
307
|
raise ArgumentError, "id must be a lowercase UUIDv7" unless computer_id.to_s.match?(Computer::UUID_V7)
|
|
290
308
|
provider = @connection.attach_provider
|
|
@@ -311,49 +329,69 @@ module PineSandbox
|
|
|
311
329
|
start_existing_computer(*args, **options, recover: true)
|
|
312
330
|
end
|
|
313
331
|
|
|
332
|
+
# ready_timeout covers the whole Start: the release wait, the create and the start.
|
|
314
333
|
def start_existing_computer(positional_id = nil, id: nil, key:, capture_keypair: nil, prior_capture_keypairs: [],
|
|
315
|
-
binding_revision: 0, prior_keys: {}, ephemeral: false, location: nil, recover: false,
|
|
334
|
+
binding_revision: 0, prior_keys: {}, ephemeral: false, location: nil, recover: false,
|
|
335
|
+
ready_timeout: Computer::DEFAULT_READY_TIMEOUT, **attach_opts)
|
|
316
336
|
if positional_id && id && positional_id != id
|
|
317
337
|
raise ArgumentError, "attach_computer got two different ids (positional and id:)"
|
|
318
338
|
end
|
|
319
339
|
id ||= positional_id
|
|
320
340
|
raise ArgumentError, "attach_computer requires the Computer id" if id.nil?
|
|
321
341
|
|
|
342
|
+
started = @clock.monotonic
|
|
343
|
+
Computer.check_ready_timeout!(ready_timeout)
|
|
322
344
|
provider = @connection.attach_provider
|
|
323
345
|
if recover && !provider.respond_to?(:current_binding)
|
|
324
346
|
raise ArgumentError, "saved-state recovery requires a provider with current_binding"
|
|
325
347
|
end
|
|
326
348
|
if provider.respond_to?(:current_binding)
|
|
327
|
-
reference =
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
begin
|
|
331
|
-
sandbox = @connection.control_plane.get(sandbox_id)
|
|
332
|
-
unless sandbox.status == :terminated && sandbox.reason == "COMPUTE_RELEASED"
|
|
333
|
-
unless recover || sandbox.status == :stopping
|
|
334
|
-
raise SandboxRetainedError.new(sandbox_id: sandbox_id, state: sandbox.status)
|
|
335
|
-
end
|
|
336
|
-
handle = SandboxHandle.new(control_plane: @connection.control_plane, sandbox_id: sandbox_id)
|
|
337
|
-
unless handle.release_failed_sandbox(sandbox)
|
|
338
|
-
raise TimeoutError, "The failed Computer's shutdown is unconfirmed; nothing was started"
|
|
339
|
-
end
|
|
340
|
-
end
|
|
341
|
-
rescue NotFoundError
|
|
342
|
-
# Authoritative project-scoped absence allows ordinary Start.
|
|
343
|
-
end
|
|
349
|
+
reference = within_ready_budget(started, ready_timeout, nil) do |left|
|
|
350
|
+
# A provider of the caller's own reads the binding on its own terms.
|
|
351
|
+
provider.is_a?(AttachCredentialsSource) ? provider.current_binding(id, request_timeout: left) : provider.current_binding(id)
|
|
344
352
|
end
|
|
345
353
|
end
|
|
354
|
+
binding_revision = reference.fetch("binding_revision") if reference
|
|
346
355
|
|
|
356
|
+
# Invalid caller material and options fail here, before Recover can release anything.
|
|
347
357
|
computer = Computer.new(id, key: key, binding_revision: binding_revision,
|
|
348
358
|
**(ephemeral ? { ephemeral: true } : {}))
|
|
349
359
|
configure_capture_keypairs(computer, capture_keypair, prior_capture_keypairs)
|
|
350
360
|
prior_keys.each { |version, bytes| computer.add_prior_key(version, bytes) }
|
|
351
361
|
attach_opts = attach_opts.merge(location: location) if location
|
|
352
|
-
|
|
362
|
+
computer.check_attach_options!(@connection, ready_timeout: ready_timeout, **attach_opts)
|
|
363
|
+
|
|
364
|
+
if reference && (sandbox_id = reference["sandbox_id"])
|
|
365
|
+
sandbox = begin
|
|
366
|
+
within_ready_budget(started, ready_timeout, sandbox_id) do |left|
|
|
367
|
+
@connection.control_plane.get(sandbox_id, request_timeout: left)
|
|
368
|
+
end
|
|
369
|
+
rescue NotFoundError
|
|
370
|
+
nil # Authoritative project-scoped absence.
|
|
371
|
+
end
|
|
372
|
+
SandboxHandle.new(control_plane: @connection.control_plane, sandbox_id: sandbox_id)
|
|
373
|
+
.admit(recover ? :recover : :start, sandbox, started: started, deadline: started + ready_timeout)
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
attach_with_recovery_context(computer, initial_revision: binding_revision, ready_timeout: ready_timeout,
|
|
377
|
+
budget_start: started, **attach_opts)
|
|
353
378
|
end
|
|
354
379
|
|
|
355
380
|
private :start_existing_computer
|
|
356
381
|
|
|
382
|
+
# Yields what is left of a Start's ready_timeout to a read of the Computer's binding or
|
|
383
|
+
# current runtime; the budget running out there is the Start's ReadyTimeoutError.
|
|
384
|
+
def within_ready_budget(started, ready_timeout, sandbox_id)
|
|
385
|
+
yield started + ready_timeout - @clock.monotonic
|
|
386
|
+
rescue PineSandbox::TimeoutError
|
|
387
|
+
raise if @clock.monotonic < started + ready_timeout
|
|
388
|
+
|
|
389
|
+
waited = @clock.monotonic - started
|
|
390
|
+
raise ReadyTimeoutError.new(PineSandbox.runtime_message(:current_read_timed_out, sandbox_id: sandbox_id, waited: waited),
|
|
391
|
+
sandbox_id: sandbox_id, waited: waited)
|
|
392
|
+
end
|
|
393
|
+
private :within_ready_budget
|
|
394
|
+
|
|
357
395
|
# Compatibility name for saved-state Start; never a memory Resume.
|
|
358
396
|
alias_method :attach_computer, :start_computer
|
|
359
397
|
|
|
@@ -436,7 +474,7 @@ module PineSandbox
|
|
|
436
474
|
end
|
|
437
475
|
|
|
438
476
|
def attach_with_recovery_context(computer, initial_revision:, recovery_credentials: nil, **attach_opts)
|
|
439
|
-
computer.
|
|
477
|
+
computer.attach_runtime(@connection, **attach_opts)
|
|
440
478
|
rescue StandardError => e
|
|
441
479
|
revision = computer.binding_revision
|
|
442
480
|
sandbox_id = computer.last_authorized_sandbox_id
|