workspaces 0.1.20 → 0.1.22

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.
@@ -1,9 +1,9 @@
1
- <main class="dashboard" id="workspace" data-id="<%= @id %>" data-status="<%= field('status') %>" data-started="<%= field('prepare_started_at') %>" data-completed="<%= field('completed_at') %>" data-busy="<%= @presentation['busy'] %>">
1
+ <main class="dashboard dashboard--narrow" id="workspace" data-id="<%= @id %>" data-status="<%= field('status') %>" data-started="<%= field('prepare_started_at') %>" data-completed="<%= field('completed_at') %>" data-busy="<%= @presentation['busy'] %>">
2
2
  <header class="dashboard__header">
3
3
  <div class="dashboard__title"><p class="eyebrow">Managed workspace</p><h1 class="mono-title"><%= @id %></h1></div>
4
4
  <div class="header-actions"><a class="button button--subtle" href="/workspaces/<%= @id %>">Back to workspace</a><a class="button button--subtle" href="/workspaces">All workspaces</a></div>
5
5
  </header>
6
- <section class="status-panel lead" id="lead" data-section="lead" aria-labelledby="status" <%= 'hidden' unless section?('lead') %>>
6
+ <section class="status-panel lead" id="lead" data-section="lead" data-state="<%= escape(@presentation['state']) %>" aria-labelledby="status" <%= 'hidden' unless section?('lead') %>>
7
7
  <div class="lead__text">
8
8
  <h2 id="status" role="status"><%= escape(@presentation['headline']) %></h2>
9
9
  <p id="summary" class="lead__summary"><%= escape(@presentation['summary']) %></p>
@@ -40,6 +40,12 @@ module Workspaces
40
40
 
41
41
  PREVIEW_STATUS_KEYS = %w(state busy ready headline summary progress lead_action current_preset sleep open).freeze
42
42
  PREVIEW_ERROR = 'Setup failed. Open settings for the full log.'.freeze
43
+ # The Arb palette (dashboard/arb-palette.css) defines its colours per theme, selected by data-theme on the root
44
+ # element. The theme follows the visitor's operating-system setting, with dark when there is none. A tiny script
45
+ # in the head sets it before anything paints, so a light-mode visitor never sees a dark flash; a page without
46
+ # script stays dark, which the palette and the stylesheet are both tested in.
47
+ THEME_SCRIPT = 'document.documentElement.dataset.theme=' \
48
+ "window.matchMedia&&window.matchMedia('(prefers-color-scheme: light)').matches?'light':'dark'".freeze
43
49
 
44
50
  # What the card polls for on a preview host, where the workspace's own pages share the origin.
45
51
  # Only what the card draws: no log, no steps, no environment and no error text.
@@ -58,18 +64,15 @@ module Workspaces
58
64
  new({}).render_checkout_authentication_error(path)
59
65
  end
60
66
 
67
+ # The Arb Previews icon from the Arb design system (arb-previews/favicon.svg, unchanged), tuned for 16 pixels.
68
+ # It carries no environment signal: the old icon was blue on localhost and gold when hosted, and the kit
69
+ # forbids recolouring or badging its marks, so the page itself (the address bar, the host in the URL) is how
70
+ # a localhost dashboard is told from a hosted one.
71
+ # The Arb Previews icon, from the design system (arb-previews/favicon.svg, copied unchanged). It is the 16 px
72
+ # tuned vector, so it is served as it is: the kit forbids recolouring or badging it, which means the tab no
73
+ # longer shows whether this dashboard is the local one or a hosted one.
61
74
  def self.favicon
62
- local = Config.public_uri.host.match?(/(?:\A|\.)localhost\z/i)
63
- color = local ? '#4f8dff' : '#f1bb53'
64
- <<~SVG
65
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Workspaces">
66
- <rect x="3" y="3" width="58" height="58" rx="13" fill="#0f1524"/>
67
- <rect x="11" y="13" width="42" height="38" rx="5" fill="none" stroke="#{color}" stroke-width="4"/>
68
- <path d="M11 25h42M32 25v26" fill="none" stroke="#{color}" stroke-width="4"/>
69
- <circle cx="18" cy="19" r="2" fill="#{color}"/>
70
- <circle cx="25" cy="19" r="2" fill="#{color}"/>
71
- </svg>
72
- SVG
75
+ File.read(File.join(__dir__, 'dashboard/arb-previews-favicon.svg'), encoding: 'UTF-8')
73
76
  end
74
77
 
75
78
  def initialize(snapshot, preview: false)
@@ -110,11 +113,14 @@ module Workspaces
110
113
  end
111
114
 
112
115
  def document(title, body)
113
- css = File.read(File.join(__dir__, 'dashboard/styles.css'), encoding: 'UTF-8')
116
+ palette = File.read(File.join(__dir__, 'dashboard/arb-palette.css'), encoding: 'UTF-8')
117
+ css = palette + File.read(File.join(__dir__, 'dashboard/styles.css'), encoding: 'UTF-8')
114
118
  script = File.read(File.join(__dir__, 'dashboard/app.js'), encoding: 'UTF-8') +
115
119
  File.read(File.join(__dir__, 'dashboard/environment.js'), encoding: 'UTF-8')
116
- '<!doctype html><html lang="en"><head><meta charset="utf-8">' \
120
+ '<!doctype html><html lang="en" data-theme="dark"><head><meta charset="utf-8">' \
117
121
  '<meta name="viewport" content="width=device-width, initial-scale=1">' \
122
+ '<meta name="color-scheme" content="dark light">' \
123
+ "<script>#{THEME_SCRIPT}</script>" \
118
124
  '<link rel="icon" type="image/svg+xml" href="/favicon.svg">' \
119
125
  "<title>#{escape(title)}</title><style>#{css}</style></head>" \
120
126
  "<body>#{body}<script>#{script}</script></body></html>"
@@ -319,6 +325,11 @@ module Workspaces
319
325
  @preview ? Config.settings_url(@id) : "/workspaces/#{@id}/settings"
320
326
  end
321
327
 
328
+ # Back to the list of every workspace, from the card.
329
+ def index_href
330
+ @preview ? Config.index_url : '/workspaces'
331
+ end
332
+
322
333
  # Where the card's start form posts. A shared card with presets saves the chosen one through the
323
334
  # environment form; a preview card starts through its own origin.
324
335
  def start_form_action
@@ -165,7 +165,7 @@ module Workspaces
165
165
  else
166
166
  @coordinator.update_environment(id, **parse_environment_changes(params))
167
167
  end
168
- return response(409, 'Workspace is busy preparing, restarting, or running a command') unless updated
168
+ return response(409, WorkspaceLock.busy_message(id)) unless updated
169
169
 
170
170
  redirect(Config.dashboard_url(id))
171
171
  rescue EnvironmentOverrides::Invalid => e
@@ -326,13 +326,23 @@ module Workspaces
326
326
  end
327
327
 
328
328
  snapshot = @coordinator.snapshot(id, include_log: false)
329
- return unavailable(id, request) unless snapshot['status'] == 'ready' && !snapshot['active']
329
+ return unavailable(id, request) unless servable?(snapshot)
330
330
 
331
331
  request.env[WORKSPACE_ID_ENV] = id
332
332
  request.env[WORKSPACE_ENV] = snapshot
333
333
  @forwarder.call(request.env)
334
334
  end
335
335
 
336
+ # A ready workspace is served unless something is changing it. A command run with `exec` leaves the
337
+ # server alone, so a preview keeps working while one runs, and a command that hangs cannot take the
338
+ # preview down with it. Anything else holding the lock (a restart, a rebuild, a PR update, removal) may
339
+ # be replacing the server or its files, and so does an unknown holder: those are still refused.
340
+ def servable?(snapshot)
341
+ return false unless snapshot['status'] == 'ready'
342
+
343
+ !snapshot['active'] || snapshot['lock_operation'] == 'exec'
344
+ end
345
+
336
346
  # A lease keeps the workspace awake for as long as its request is being served. A response body
337
347
  # that is still being produced (anything but a buffered Array) keeps it until the server closes it.
338
348
  def hold_lease(result, lease)
@@ -1,4 +1,5 @@
1
1
  require_relative 'worktree'
2
+ require_relative 'workspace_lock'
2
3
  require_relative 'state_store'
3
4
  require_relative 'backend'
4
5
  require_relative 'recipe'
@@ -28,23 +29,24 @@ module Workspaces
28
29
  workspace
29
30
  end
30
31
 
31
- def with_lock(id)
32
+ # Runs the block holding the workspace's lock, or raises ArgumentError saying who holds it. The block
33
+ # gets the workspace, and the lock when it wants to add to the holder record.
34
+ def with_lock(id, operation: 'busy', detail: nil)
32
35
  workspace = find(id)
33
- Config.locks_dir.mkpath
34
- File.open(Config.prepare_lock_path(id), File::RDWR | File::CREAT, 0o600) do |lock|
35
- unless lock.flock(File::LOCK_EX | File::LOCK_NB)
36
- raise ArgumentError,
37
- 'Workspace is busy preparing, restarting, or running a command'
38
- end
39
-
40
- yield workspace
36
+ lock = WorkspaceLock.acquire(id, operation: operation, detail: detail)
37
+ raise ArgumentError, WorkspaceLock.busy_message(id) unless lock
38
+
39
+ begin
40
+ yield workspace, lock
41
+ ensure
42
+ lock.close
41
43
  end
42
44
  end
43
45
 
44
46
  # Removes the workspace and its saved settings and secrets. Returns the secrets that could not be
45
47
  # removed (empty when all went), so callers can say so rather than report a clean removal.
46
48
  def remove(id, force: false)
47
- with_lock(id) do |workspace|
49
+ with_lock(id, operation: 'remove') do |workspace|
48
50
  raise ArgumentError, 'Workspace has uncommitted/untracked work' if !force && workspace.dirty?
49
51
 
50
52
  Backend.new(id).stop!
@@ -78,7 +80,7 @@ module Workspaces
78
80
  end
79
81
 
80
82
  def update_pr(id)
81
- with_lock(id) do |workspace|
83
+ with_lock(id, operation: 'update-pr') do |workspace|
82
84
  result = workspace.update_from_pr!
83
85
  message = result['updated'] ? 'PR checkout updated. Rebuild & restart to run the new code.' : 'PR checkout is already current.'
84
86
  StateStore.new(Config.state_file).set(id, 'source_update_message' => message,
@@ -1,3 +1,3 @@
1
1
  module Workspaces
2
- VERSION = '0.1.20'
2
+ VERSION = '0.1.22'
3
3
  end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'time'
5
+ require_relative 'config'
6
+ require_relative 'duration_format'
7
+
8
+ module Workspaces
9
+ # The per-workspace lock that serializes preparing, restarting, stopping, removing, updating and
10
+ # running commands, plus a record of who holds it.
11
+ #
12
+ # The lock is a non-blocking exclusive flock, so the kernel releases it when the holder exits for
13
+ # any reason, including SIGKILL. A holder that died therefore never leaves the lock stuck; only a
14
+ # holder that is alive and not finishing does. The record in the file is written by the holder and
15
+ # emptied when it lets go. It is only meaningful while the lock is held, so a record left behind by
16
+ # a holder that died is ignored.
17
+ class WorkspaceLock
18
+ GENERIC_BUSY = 'Workspace is busy preparing, restarting, or running a command'
19
+
20
+ OPERATIONS = {
21
+ 'prepare' => 'preparing', 'restart' => 'restarting', 'resume' => 're-running setup steps',
22
+ 'sleep' => 'putting the workspace to sleep', 'wake' => 'waking the workspace',
23
+ 'stop' => 'stopping the server', 'remove' => 'removing the workspace',
24
+ 'update-pr' => 'updating the PR checkout'
25
+ }.freeze
26
+
27
+ # Takes the lock, or returns nil when someone else holds it.
28
+ def self.acquire(id, operation:, detail: nil)
29
+ Config.locks_dir.mkpath
30
+ file = File.open(Config.prepare_lock_path(id), File::RDWR | File::CREAT, 0o600)
31
+ unless file.flock(File::LOCK_EX | File::LOCK_NB)
32
+ file.close
33
+ return nil
34
+ end
35
+
36
+ lock = new(file)
37
+ lock.update('operation' => operation, 'pid' => Process.pid, 'started_at' => Time.now.utc.iso8601,
38
+ 'detail' => detail)
39
+ lock
40
+ rescue StandardError
41
+ file&.close unless file&.closed?
42
+ raise
43
+ end
44
+
45
+ # True while some process holds the lock.
46
+ def self.held?(id)
47
+ path = Config.prepare_lock_path(id)
48
+ return false unless path.file?
49
+
50
+ File.open(path, File::RDWR) { |file| !file.flock(File::LOCK_EX | File::LOCK_NB) }
51
+ end
52
+
53
+ # What the current holder wrote, or nil when nothing holds the lock or the record cannot be read.
54
+ def self.holder(id)
55
+ return nil unless held?(id)
56
+
57
+ record = JSON.parse(Config.prepare_lock_path(id).read)
58
+ record.is_a?(Hash) ? record : nil
59
+ rescue JSON::ParserError, SystemCallError
60
+ nil
61
+ end
62
+
63
+ def self.busy_message(id)
64
+ record = holder(id)
65
+ return GENERIC_BUSY unless record
66
+
67
+ "Workspace is busy: #{describe(record)}"
68
+ end
69
+
70
+ def self.describe(record)
71
+ operation = record['operation'].to_s
72
+ return describe_command(record) if operation == 'exec'
73
+
74
+ OPERATIONS.fetch(operation, operation)
75
+ end
76
+
77
+ def self.describe_command(record)
78
+ text = +'running a command'
79
+ text << " (#{record['detail']})" if record['detail']
80
+ pids = [record['child_pid'] && "pid #{record['child_pid']}"].compact
81
+ text << ", #{pids.join(', ')}" unless pids.empty?
82
+ elapsed = elapsed_text(record['started_at'])
83
+ text << ", for #{elapsed}" if elapsed
84
+ limit = DurationFormat.call(record['timeout_seconds'])
85
+ text << ". It will be stopped automatically after #{limit}" if limit
86
+ text
87
+ end
88
+ private_class_method :describe_command
89
+
90
+ def self.elapsed_text(started_at)
91
+ DurationFormat.call(Time.now - Time.iso8601(started_at.to_s))
92
+ rescue ArgumentError
93
+ nil
94
+ end
95
+ private_class_method :elapsed_text
96
+
97
+ def initialize(file)
98
+ @file = file
99
+ @record = {}
100
+ end
101
+
102
+ # Adds to what the holder has told the world, keeping what it already said.
103
+ def update(fields)
104
+ @record = @record.merge(fields.compact)
105
+ @file.rewind
106
+ @file.truncate(0)
107
+ @file.write(JSON.generate(@record))
108
+ @file.flush
109
+ end
110
+
111
+ # A descriptor the background worker owns, so the request that took the lock can close its own
112
+ # without releasing it.
113
+ def dup
114
+ self.class.allocate.tap { |copy| copy.send(:adopt, @file.dup, @record) }
115
+ end
116
+
117
+ def close
118
+ return if @file.closed?
119
+
120
+ @file.truncate(0)
121
+ @file.close
122
+ rescue IOError, SystemCallError
123
+ @file.close unless @file.closed?
124
+ end
125
+
126
+ def closed?
127
+ @file.closed?
128
+ end
129
+
130
+ private
131
+
132
+ def adopt(file, record)
133
+ @file = file
134
+ @record = record
135
+ end
136
+ end
137
+ end
data/lib/workspaces.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative 'workspaces/config'
2
+ require_relative 'workspaces/workspace_lock'
2
3
  require_relative 'workspaces/state_store'
3
4
  require_relative 'workspaces/port_allocator'
4
5
  require_relative 'workspaces/child_env'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: workspaces
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.20
4
+ version: 0.1.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Workspaces contributors
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-30 00:00:00.000000000 Z
11
+ date: 2026-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -79,6 +79,8 @@ files:
79
79
  - lib/workspaces/config.rb
80
80
  - lib/workspaces/coordinator.rb
81
81
  - lib/workspaces/dashboard/app.js
82
+ - lib/workspaces/dashboard/arb-palette.css
83
+ - lib/workspaces/dashboard/arb-previews-favicon.svg
82
84
  - lib/workspaces/dashboard/card.html.erb
83
85
  - lib/workspaces/dashboard/checkout_authentication_error.html.erb
84
86
  - lib/workspaces/dashboard/environment.js
@@ -107,6 +109,7 @@ files:
107
109
  - lib/workspaces/vault/file_vault.rb
108
110
  - lib/workspaces/version.rb
109
111
  - lib/workspaces/workflow.rb
112
+ - lib/workspaces/workspace_lock.rb
110
113
  - lib/workspaces/worktree.rb
111
114
  homepage:
112
115
  licenses: