rubik 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +2 -2
  3. data/CHANGELOG.md +100 -1
  4. data/bin/rubik +15 -10
  5. data/lib/rubik/cli/workspace_trust.rb +1 -0
  6. data/lib/rubik/concurrency/worker.rb +143 -0
  7. data/lib/rubik/concurrency.rb +1 -0
  8. data/lib/rubik/config/deep_ops.rb +17 -0
  9. data/lib/rubik/config/defaults.rb +0 -1
  10. data/lib/rubik/config/schema.rb +0 -1
  11. data/lib/rubik/config.rb +7 -3
  12. data/lib/rubik/context/repo_context_reader.rb +27 -8
  13. data/lib/rubik/events.rb +18 -0
  14. data/lib/rubik/kernel/unit.rb +4 -3
  15. data/lib/rubik/orchestrator/auto_apply.rb +2 -0
  16. data/lib/rubik/orchestrator/budget_manager.rb +18 -2
  17. data/lib/rubik/orchestrator/goal_ledger/deep_ops.rb +41 -0
  18. data/lib/rubik/orchestrator/goal_ledger/drift.rb +2 -1
  19. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +56 -50
  20. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +26 -0
  21. data/lib/rubik/orchestrator/goal_ledger/patch.rb +10 -17
  22. data/lib/rubik/orchestrator/goal_ledger/suspend_lifecycle.rb +9 -41
  23. data/lib/rubik/orchestrator/goal_ledger/validator.rb +6 -20
  24. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +3 -14
  25. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +21 -14
  26. data/lib/rubik/orchestrator/goal_state.rb +15 -15
  27. data/lib/rubik/orchestrator/loop.rb +62 -63
  28. data/lib/rubik/orchestrator/lsp_feedback.rb +7 -1
  29. data/lib/rubik/orchestrator/subagent/admission_policy.rb +161 -0
  30. data/lib/rubik/orchestrator/subagent/builtin_types.rb +3 -2
  31. data/lib/rubik/orchestrator/subagent/notices.rb +5 -4
  32. data/lib/rubik/orchestrator/subagent/profile_contract.rb +4 -10
  33. data/lib/rubik/orchestrator/subagent/run_cycle.rb +22 -1
  34. data/lib/rubik/orchestrator/subagent/scoped_registry.rb +34 -2
  35. data/lib/rubik/orchestrator/subagent/type_registry.rb +19 -1
  36. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +6 -1
  37. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +6 -1
  38. data/lib/rubik/orchestrator/turn_executor.rb +2 -1
  39. data/lib/rubik/platform/path.rb +0 -1
  40. data/lib/rubik/prompt/context_builder.rb +24 -18
  41. data/lib/rubik/prompt/context_compactor.rb +31 -8
  42. data/lib/rubik/prompt/conversation_rows.rb +33 -0
  43. data/lib/rubik/prompt/doctrine.rb +4 -2
  44. data/lib/rubik/prompt/emergency_compactor.rb +5 -4
  45. data/lib/rubik/repl/commands/agent_execution.rb +13 -2
  46. data/lib/rubik/repl/commands/buffer.rb +4 -0
  47. data/lib/rubik/repl/commands/mcp.rb +5 -0
  48. data/lib/rubik/repl/commands/subagents.rb +4 -4
  49. data/lib/rubik/repl/commands/workspace.rb +22 -2
  50. data/lib/rubik/repl/config_wiring.rb +24 -7
  51. data/lib/rubik/repl/input_execution.rb +1 -1
  52. data/lib/rubik/repl/lifecycle.rb +3 -2
  53. data/lib/rubik/repl/pruner/harness.rb +52 -67
  54. data/lib/rubik/repl/pruner/rules.rb +3 -1
  55. data/lib/rubik/repl/service_catalog.rb +1 -1
  56. data/lib/rubik/repl/synthesized_command_context.rb +3 -0
  57. data/lib/rubik/repl/turn_recorder.rb +5 -4
  58. data/lib/rubik/repl.rb +1 -0
  59. data/lib/rubik/runtime/context.rb +5 -0
  60. data/lib/rubik/runtime/discovery.rb +55 -0
  61. data/lib/rubik/runtime/overlay_storage.rb +54 -23
  62. data/lib/rubik/runtime/repair_probe.rb +20 -2
  63. data/lib/rubik/runtime/sealed_coeffects.rb +58 -0
  64. data/lib/rubik/runtime/synthesized_command_context.rb +80 -0
  65. data/lib/rubik/runtime/synthesized_tool_context.rb +123 -0
  66. data/lib/rubik/runtime.rb +2 -0
  67. data/lib/rubik/security/classifier/classifier.rb +16 -8
  68. data/lib/rubik/security/classifier/commands/biome.yaml +49 -0
  69. data/lib/rubik/security/classifier/commands/bun.yaml +91 -0
  70. data/lib/rubik/security/classifier/commands/bundle.yaml +107 -0
  71. data/lib/rubik/security/classifier/commands/clang-format.yaml +21 -0
  72. data/lib/rubik/security/classifier/commands/clang.yaml +37 -0
  73. data/lib/rubik/security/classifier/commands/cmake.yaml +37 -0
  74. data/lib/rubik/security/classifier/commands/ctest.yaml +30 -0
  75. data/lib/rubik/security/classifier/commands/gcc.yaml +36 -0
  76. data/lib/rubik/security/classifier/commands/gem.yaml +104 -0
  77. data/lib/rubik/security/classifier/commands/gofmt.yaml +15 -0
  78. data/lib/rubik/security/classifier/commands/pnpm.yaml +8 -8
  79. data/lib/rubik/security/classifier/commands/python.yaml +18 -1
  80. data/lib/rubik/security/classifier/commands/python3.yaml +18 -1
  81. data/lib/rubik/security/classifier/commands/rake.yaml +60 -0
  82. data/lib/rubik/security/classifier/commands/rspec.yaml +42 -0
  83. data/lib/rubik/security/classifier/commands/rubocop.yaml +38 -0
  84. data/lib/rubik/security/classifier/commands/ruby.yaml +25 -0
  85. data/lib/rubik/security/classifier/commands/rustc.yaml +34 -0
  86. data/lib/rubik/security/classifier/commands/zig.yaml +77 -0
  87. data/lib/rubik/security/classifier/database.rb +12 -1
  88. data/lib/rubik/security/classifier/matcher.rb +17 -2
  89. data/lib/rubik/security/classifier/models.rb +3 -2
  90. data/lib/rubik/security/classifier.rb +5 -0
  91. data/lib/rubik/security/guard.rb +133 -39
  92. data/lib/rubik/storage/vakanuvis/harness.rb +49 -89
  93. data/lib/rubik/tools/base.rb +24 -7
  94. data/lib/rubik/tools/builtins/files.rb +2 -0
  95. data/lib/rubik/tools/builtins/goal_ledger.rb +9 -9
  96. data/lib/rubik/tools/builtins/lifecycle.rb +1 -1
  97. data/lib/rubik/tools/builtins/synthesis.rb +121 -64
  98. data/lib/rubik/tools/builtins/unsynthesize.rb +38 -0
  99. data/lib/rubik/tools/catalog.rb +3 -0
  100. data/lib/rubik/tools/declaration.rb +6 -2
  101. data/lib/rubik/tools/preflight_runner.rb +107 -14
  102. data/lib/rubik/tools/registry.rb +2 -0
  103. data/lib/rubik/tools/uri_resolver.rb +3 -1
  104. data/lib/rubik/uris.rb +5 -1
  105. data/lib/rubik/version.rb +1 -1
  106. data/lib/rubik.rb +1 -0
  107. metadata +25 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4570babb9dc3d48184b0285c431ef1d7fd37e5f900d1b745033d2f2fd7d35c0
4
- data.tar.gz: 198350a1de0ef4233150c52445becc263b1456f013190d3400264ffde790ecfe
3
+ metadata.gz: dad89865cb319b5a3cbf571625db00622882f6a19a830f3aa8002f8aef6b082f
4
+ data.tar.gz: 36e44aa9a31892e84c3bd6b6ae0623d0f2cf90c28c0302e9eee57ff0f23947e5
5
5
  SHA512:
6
- metadata.gz: 1c4761f4faf92b3d0c6fc8997196c92c975ec973a84ace43739a7cc433349ae8b266894cf024f4c66023d14592fd2e1aad416d53f41c5fe85d5d5adb7b8ff2af
7
- data.tar.gz: 48c5177c5a1fe0e3209df5defd222845f9194d7f86cfd281cc9190cce3e332e9c7ad062a88243cd403a5ca94ef744bb5bf8f48f87afd85064ef913031e5f8eb2
6
+ metadata.gz: 62e943b2e50537c0d18e1c0ecabbecae1b5323a6e15a6dedd2e0975d20fd95e2491df2b7479ab9a596db639c86e7f814909c5d21b1c0b1150aef82e8bd9267d5
7
+ data.tar.gz: 4bddb1287a07acb0d34310e2a6dc4926b1d60787c3a3da60eba5e51bb7f832d2b41136be9ea3ab245daefde011d5081c23c50b7cafebccdeb9eada8daa8f49f5
data/ATTRIBUTION.md CHANGED
@@ -13,7 +13,7 @@ Rubik is built on the shoulders of giants. This document acknowledges and credit
13
13
  - **License:** MIT License
14
14
  - **Rubik Integration:**
15
15
  - Ported to pure Ruby under [`lib/rubik/security/classifier/`](lib/rubik/security/classifier/).
16
- - Includes the comprehensive command database (175 popular CLI tool definition YAML files under [`lib/rubik/security/classifier/commands/`](lib/rubik/security/classifier/commands/)).
16
+ - Includes the comprehensive command database (191 popular CLI tool definition YAML files under [`lib/rubik/security/classifier/commands/`](lib/rubik/security/classifier/commands/)).
17
17
  - Adapted to use `tree-sitter` for AST parsing, and extended with first-class **PowerShell** (`pwsh`) cmdlet risk classification and execution policy checking.
18
18
 
19
19
  ```
@@ -121,7 +121,7 @@ SOFTWARE.
121
121
  | **`strings-ansi`** | Piotr Murach | MIT | ANSI-aware string truncation and alignment for REPL formatters. |
122
122
  | **`reline`** | Hiroshi SHIBATA & Ruby Team | Ruby License / BSD-2-Clause | Line-editing, history navigation, autocompletion hooks, and headless terminal adaptation. |
123
123
  | **`prism`** | Kevin Newton & Ruby Core Team | MIT | High-performance Ruby parsing and AST extraction ([`Rubik::AST::RubyExtractor`](lib/rubik/ast/ruby_extractor.rb)). |
124
- | **`minitest`** | Ryan Davis & seattlerb | MIT | Fast, modular test suite powering Rubik's 4,200+ unit, integration, and contract tests. |
124
+ | **`minitest`** | Ryan Davis & seattlerb | MIT | Fast, modular test suite powering Rubik's 4,300+ unit, integration, and contract tests. |
125
125
  | **`rubocop`** | Bozhidar Batsov & contributors | MIT | Static code analysis, style enforcement, and architectural hygiene gates. |
126
126
  | **`benchmark`, `logger`, `pstore`, `reline`, `fiddle`** | Ruby core / rubygems teams | Ruby License / BSD-2-Clause | Ruby stdlib gems pinned explicitly in `rubik.gemspec` as they are unbundled from the default set in Ruby 3.4/3.5. |
127
127
 
data/CHANGELOG.md CHANGED
@@ -8,6 +8,104 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ## [0.9.0] - 2026-11-09
12
+
13
+ The permission-architecture minor: subagent command permissions, the
14
+ synthesis security gate, and tool visibility are overhauled end to end
15
+ (F1-F8 command classifier, trust discovery, sealed contexts, admission
16
+ policy), the goal-ledger aligns SKILL.state with arXiv:2608.26263, context
17
+ assembly hardens its budget boundaries and injection defense — and the
18
+ single-homing wave continues (bounded-queue worker, event-name vocabulary,
19
+ sigma-row grammar, sealed-context deny-list each collapse to one owner).
20
+
21
+ ### Added
22
+
23
+ - **Subagent tool admission single-homed behind one policy (#393):** the
24
+ dynamic-origin vocabulary, risk ladder, per-plane rules, and capability
25
+ labels live in one `AdmissionPolicy` while the executor stays
26
+ `ScopedRegistry`; unknown planes raise at the boundary, every non-builtin
27
+ origin is ladder-gated on research/coder, a caller-provided `tool_filter`
28
+ is rejected up front (the installed-filter marker keeps re-registration
29
+ legal), and filter-less type_defs fail the label simulation loudly.
30
+ - **Subagent command permissions, synthesis security gate, and tool
31
+ visibility overhauled (#392):** the classifier command DB expands with 19
32
+ command families, workspace trust discovery and interactive approval land,
33
+ Ruby synthesis decouples from the shell classifier with
34
+ `SynthesizedCommandContext` sealed, preflight goes conditional, and the
35
+ 2-arity sealed tool context arrives; the review round closed the sealed
36
+ `tool_registry`/`tools` escape, preflight verdict spoofing
37
+ (`exit!`/`SystemExit`/strict-boolean verdict), the nil-type_def silent
38
+ grant, and several silent downgrades that now warn.
39
+ - **Permission & authorization architecture hardening (F1-F8) (#391):**
40
+ synthesis forbidden capabilities block constant-aliasing escapes
41
+ (Kernel, File, ENV, ObjectSpace, Rubik, Fiber); `.rubik/**` joins
42
+ `Guard::DEFAULT_CRITICAL_PATTERNS` and untrusted project configs get
43
+ sensitive keys stripped; `RepairProbe` is confined to a diagnostic
44
+ whitelist behind `Guard#verify_command_safety!`; `rubik --trust` CLI flag
45
+ for trusted workspaces; `!mcp` mutating commands restricted for agent
46
+ callers; classifier Windows dangerous roots and HOME prefix detection
47
+ fixed with `critical_path?` verification on shell write paths; relative
48
+ traversal blocked in `BaseTool.normalize_path_filter`;
49
+ `buffer.model_can_apply` enforced with `!autoapply`/`!format` agent-caller
50
+ guards.
51
+ - **GoalLedger aligned with arXiv:2608.26263 (#389):** continuous
52
+ observation projection hooks `BudgetManager#evaluate_context_compaction!`
53
+ so banked tool observations are projected without waiting for ceiling
54
+ exhaustion, verbose assistant reasoning is pruned on banked calls,
55
+ deterministic RFC 6902 index attributes land on ledger list items,
56
+ `rubik://goal/ledger.json` exposes machine-readable state, `DeepOps#
57
+ deep_freeze` freezes in-place, and the declarative verification specs are
58
+ documented in system prompts.
59
+
60
+ ### Changed
61
+
62
+ - **`network.allow` alias retired; buffer-sync writers derived from
63
+ Declarations (#397):** the deprecation listener names the
64
+ `sandbox.allow_network` replacement and SECURITY.md states honestly that
65
+ config-file entries are silently ignored; the LSP buffer-sync writer set
66
+ is derived from tool Declarations instead of a hand-maintained list.
67
+ - **Event-name vocabulary single-homed in a zero-require leaf (#396):**
68
+ `lib/rubik/events.rb` owns the bus event names; every emit/subscribe site
69
+ uses the shared symbols so names cannot drift from their single home.
70
+ - **Bounded-queue worker skeleton single-homed (#395):** atomic admission,
71
+ idempotent shutdown, busy accounting, and the guarded dispatch-failure net
72
+ live in `Rubik::Concurrency::Worker`; pruner and vakanuvis harnesses
73
+ migrate onto it.
74
+ - **Sigma-row grammar single home; verifier seam narrowed (#388):**
75
+ `GOAL_LEDGER_TAG`/`LEDGER_DRIFT_TAG` live in the prompt layer (consumed by
76
+ XmlRender, DriftNote, the injection-escape site, and the new
77
+ `drift_message?` predicate), the ledger's byte-for-byte reimplementations
78
+ are deleted, and `Verifier.at_step_boundary!` speaks values instead of
79
+ reaching through the loop object.
80
+ - **Max-steps terminal carries a structured reason (#387):** `reason:
81
+ :budget_exhausted` on the terminal hash; both consumers (goal
82
+ auto-continue, ask-continue) branch on the symbol instead of
83
+ substring-matching the English prose, so rewording can no longer dead the
84
+ `agent.goal_auto_continue_limit` knob.
85
+ - **Goal-ledger copy families consolidated (#385):** `stamp_row!`, a shared
86
+ `DeepOps` page, schema-derived seed/snapshot/load-check with a tripwire
87
+ test, and `Ledger.load` give the stamp, deep-copy discipline, and sigma
88
+ vocabulary single owners; dead `Verifier.stale?` and `Patch#raw_bytesize`
89
+ deleted; five degradation pins kept green through the refactor.
90
+
91
+ ### Fixed
92
+
93
+ - **Context assembly, budget boundaries, and injection defense hardened
94
+ (F1-F9) (#390):** tool schema tokens reserved in compaction with unified
95
+ max_output_tokens resolution; dynamic system prompt fingerprinting with
96
+ in-place refresh on tool/skill/subagent changes; protocol files capped
97
+ (highest priority only, 8k tokens) with closing-tag sanitization; XML
98
+ escaping in custom command rendering; LSP diagnostics capped at 50 with
99
+ error-first priority; 'self' subagent inherits protocol XML and the
100
+ environment line; `sanitize_dialogue_turn` validates roles and neutralizes
101
+ unescaped pin tags from prior turns; `SystemCallError`/`EncodingError`
102
+ handled in git branch detection.
103
+ - **Suspend tokens assigned absolutely and reported honestly (#386):** the
104
+ transient-ClientError rescue passes real `orchestrator.total_tokens` into
105
+ the suspended terminal (was zero), `GoalState#suspend!` assigns absolutely
106
+ instead of additive `+=` so suspend→resume→suspend no longer
107
+ double-counts, and dead `GoalState#record_tokens!` is deleted.
108
+
11
109
  ## [0.8.1] - 2026-11-06
12
110
 
13
111
  A reliability-and-single-homing patch: the kernel hardens the spatiotemporal
@@ -1367,7 +1465,8 @@ installation section.
1367
1465
  - Context: serialized index mutations, snapshot-outside-lock reconcile,
1368
1466
  streaming-first truncation.
1369
1467
 
1370
- [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.8.1...HEAD
1468
+ [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.9.0...HEAD
1469
+ [0.9.0]: https://github.com/ozgurulukir/rubik/compare/v0.8.1...v0.9.0
1371
1470
  [0.8.1]: https://github.com/ozgurulukir/rubik/compare/v0.8.0...v0.8.1
1372
1471
  [0.8.0]: https://github.com/ozgurulukir/rubik/compare/v0.7.1...v0.8.0
1373
1472
  [0.7.1]: https://github.com/ozgurulukir/rubik/compare/v0.7.0...v0.7.1
data/bin/rubik CHANGED
@@ -13,7 +13,7 @@ require "optparse"
13
13
  # inner handler has been left uninstalled -- always terminates the process, so
14
14
  # the user is never stuck unable to exit.
15
15
  trap("INT") do
16
- $stderr.puts "\n[rubik] Interrupted. Exiting."
16
+ warn "\n[rubik] Interrupted. Exiting."
17
17
  exit!(130)
18
18
  end
19
19
 
@@ -52,6 +52,10 @@ OptionParser.new do |opts|
52
52
  options[:script] = file
53
53
  end
54
54
 
55
+ opts.on("--trust", "Explicitly trust the current workspace") do
56
+ options[:trust] = true
57
+ end
58
+
55
59
  opts.on("--no-color", "Disable ANSI colors, banner and spinner") do
56
60
  ENV["NO_COLOR"] = "1"
57
61
  end
@@ -61,21 +65,22 @@ OptionParser.new do |opts|
61
65
  end
62
66
  end.parse!
63
67
 
64
- workspace_trusted = options[:script] ||
65
- Rubik::CLI::WorkspaceTrust.authorize!(
66
- workspace: Dir.pwd,
67
- input: $stdin,
68
- output: $stdout
69
- )
68
+ Rubik::CLI::WorkspaceTrust.trust!(Dir.pwd) if options[:trust]
69
+
70
+ workspace_trusted = options[:trust] ||
71
+ options[:script] ||
72
+ Rubik::CLI::WorkspaceTrust.authorize!(
73
+ workspace: Dir.pwd,
74
+ input: $stdin,
75
+ output: $stdout
76
+ )
70
77
  Kernel.exit(0) unless workspace_trusted
71
78
 
72
79
  shell = Rubik::REPL::Shell.new
73
80
 
74
81
  if options[:continue]
75
82
  res = shell.resume_session(options[:continue])
76
- unless res[:ok]
77
- warn "[Rubik] Warning: #{res[:error]}"
78
- end
83
+ warn "[Rubik] Warning: #{res[:error]}" unless res[:ok]
79
84
  end
80
85
 
81
86
  if options[:script]
@@ -46,6 +46,7 @@ module Rubik
46
46
  end
47
47
 
48
48
  def trusted?(workspace, store_path: default_store_path)
49
+ return true if ENV["RUBIK_TRUST_WORKSPACE"] == "1" && store_path == default_store_path
49
50
  return false unless File.file?(store_path)
50
51
 
51
52
  store = YAML::Store.new(store_path, true)
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "join"
4
+
5
+ module Rubik
6
+ module Concurrency
7
+ # Single home of the bounded-queue worker skeleton shared by the pruner
8
+ # and vakanuvis harnesses: one worker thread drains a bounded FIFO until
9
+ # a STOP marker, with atomic admission (stopped-check + push share one
10
+ # monitor critical section), idempotent shutdown, busy accounting and a
11
+ # last-ditch dispatch net. Mechanics only, per the join.rb contract: the
12
+ # host keeps its own monitor and log channel — the skeleton never
13
+ # allocates a lock and never logs directly.
14
+ #
15
+ # Host contract — state (host-initialized, skeleton-operated):
16
+ # @monitor / @queue / @worker — host-owned; domain state shares the monitor
17
+ # @stopped / @busy — skeleton-owned flags under @monitor
18
+ # plus private hooks: worker_max_queue (FIFO bound), worker_warn (log
19
+ # sink), worker_drop_accounted (overflow reconciliation),
20
+ # worker_dispatch_failed (net: receives kind, payload, error so the
21
+ # host reconciles its accounting), worker_before_join
22
+ # (pre-join teardown, default none) and handle_event (dispatch hook).
23
+ module Worker
24
+ # Queue terminator; drop-oldest must never swallow it (the worker would drain forever).
25
+ STOP = :__worker_stop__
26
+
27
+ # Shutdown grants the worker JOIN_TIMEOUT seconds to drain; the idle
28
+ # probe polls at a cadence that keeps teardown feeling immediate.
29
+ JOIN_TIMEOUT = 2
30
+ IDLE_POLL = 0.01
31
+
32
+ def stopped? = @monitor.synchronize { @stopped }
33
+
34
+ # Queue + in-flight count: incremented at admission, decremented when
35
+ # consumed or dropped — closes the accounting window pop would open.
36
+ def busy? = @monitor.synchronize { @busy.positive? }
37
+
38
+ def queue_size = @queue.size
39
+
40
+ # Idle probe: Join's mechanics are handle-shaped (join a thread), this
41
+ # is predicate-shaped (poll busy?) — same deadline discipline, own loop.
42
+ def wait_idle(timeout: 2.0)
43
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
44
+ sleep IDLE_POLL while busy? && Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
45
+ !busy?
46
+ end
47
+
48
+ def shutdown
49
+ @monitor.synchronize do
50
+ return if @stopped
51
+
52
+ @stopped = true
53
+ @busy += 1 # the STOP marker counts as consumed work
54
+ @queue.push(STOP)
55
+ end
56
+ worker_before_join
57
+ outcome = Join.thread(@worker, timeout: JOIN_TIMEOUT)
58
+ if outcome == :timeout
59
+ # Thread#kill waits out the dying thread's unwind, so the in-flight
60
+ # ensure decrement runs before we return; busy then equals the
61
+ # unconsumed queue — lost work stays honestly counted (-1: the
62
+ # STOP marker itself is still queued).
63
+ worker_warn("worker did not stop in #{JOIN_TIMEOUT}s — killing with #{queue_size - 1} event(s) unprocessed")
64
+ @worker.kill
65
+ elsif outcome.is_a?(StandardError)
66
+ # Error-as-data: Join returns a dead-with-exception thread's error
67
+ # — teardown completes, the death is reported, never swallowed.
68
+ worker_warn("worker thread died: #{outcome.class}: #{outcome.message}")
69
+ end
70
+ @worker = nil
71
+ end
72
+
73
+ # Atomic admission: the stopped-check and the push share one critical
74
+ # section — an event racing shutdown either lands before STOP (and is
75
+ # consumed) or is refused; it can never straddle the marker and leak a
76
+ # busy increment behind an unconsumable item. The overflow hook fires
77
+ # OUTSIDE the monitor: a slow host reconcile (gate locks, log I/O) must
78
+ # never stall admission, shutdown or wait_idle.
79
+ def enqueue_event(kind, payload)
80
+ dropped = nil
81
+ @monitor.synchronize do
82
+ return false if @stopped
83
+
84
+ if @queue.size >= worker_max_queue
85
+ dropped = drop_oldest!
86
+ return false if dropped == STOP
87
+ end
88
+ @busy += 1
89
+ @queue.push([kind, payload])
90
+ end
91
+ worker_drop_accounted(dropped) if dropped
92
+ true
93
+ end
94
+
95
+ private
96
+
97
+ def worker_before_join; end
98
+
99
+ def run_worker
100
+ loop do
101
+ kind, payload = @queue.pop
102
+ break if kind == STOP
103
+
104
+ handle_event(kind, payload)
105
+ rescue StandardError => e
106
+ # A raise escaping handle_event's own rescue would kill the worker
107
+ # with queued events unconsumed; hand the item to the hook (the
108
+ # host reconciles its accounting) and keep draining. The net is
109
+ # itself guarded: when it runs, log/reconcile infrastructure is
110
+ # already failing and a second raise must not kill the worker.
111
+ begin
112
+ worker_dispatch_failed(kind, payload, e)
113
+ rescue StandardError
114
+ nil
115
+ end
116
+ ensure
117
+ @monitor.synchronize { @busy -= 1 }
118
+ end
119
+ end
120
+
121
+ # Make room for one admission (assumes @monitor held): pops the oldest
122
+ # item, decrements its busy increment, and returns it for the caller to
123
+ # hand to worker_drop_accounted outside the monitor. Returns STOP
124
+ # (marker re-pushed; the caller refuses rather than strand an event
125
+ # behind the terminator) or nil when the worker drained the queue
126
+ # between the size check and this pop (nothing dropped, unaccounted).
127
+ def drop_oldest!
128
+ item = @queue.pop(true)
129
+ if item == STOP
130
+ @queue.push(STOP)
131
+ return STOP
132
+ end
133
+
134
+ @busy -= 1
135
+ item
136
+ rescue ThreadError
137
+ # Worker drained between the size check and pop(true): no-op race
138
+ # loss — nothing was dropped, nothing to account.
139
+ nil
140
+ end
141
+ end
142
+ end
143
+ end
@@ -6,6 +6,7 @@ require_relative "concurrency/event_loop"
6
6
  require_relative "concurrency/scheduler"
7
7
  require_relative "concurrency/boundary"
8
8
  require_relative "concurrency/join"
9
+ require_relative "concurrency/worker"
9
10
  require_relative "concurrency/fiber_timeout"
10
11
 
11
12
  module Rubik
@@ -3,6 +3,10 @@
3
3
  module Rubik
4
4
  class Config
5
5
  module DeepOps
6
+ SENSITIVE_PROJECT_CONFIG_KEYS = [
7
+ "security", "sandbox", "plugins", "mcp", "lsp", "project", "test", "linter", "adapter"
8
+ ].freeze
9
+
6
10
  private
7
11
 
8
12
  def deep_dup(obj)
@@ -89,6 +93,19 @@ module Rubik
89
93
  end
90
94
  target[last_key] = value
91
95
  end
96
+
97
+ def filter_untrusted_project_config(hash)
98
+ return hash unless hash.is_a?(Hash)
99
+
100
+ filtered = deep_dup(hash)
101
+ SENSITIVE_PROJECT_CONFIG_KEYS.each { |key| filtered.delete(key) }
102
+ if filtered["workspace"].is_a?(Hash)
103
+ ["allow_external_writes", "allowed_external_dirs", "additional_roots"].each do |k|
104
+ filtered["workspace"].delete(k)
105
+ end
106
+ end
107
+ filtered
108
+ end
92
109
  end
93
110
  end
94
111
  end
@@ -72,7 +72,6 @@ module Rubik
72
72
  "command_allowlist" => [], # operator-managed token-prefix entries (e.g. "node .gitnexus/run.cjs")
73
73
  "allow_safe_probes" => true # `bash --version`-style version/help probes bypass the blocklist
74
74
  },
75
- "network" => { "allow" => false },
76
75
  "buffer" => {
77
76
  "auto_apply" => "manual",
78
77
  "model_can_apply" => true,
@@ -41,7 +41,6 @@ module Rubik
41
41
  "sandbox.shell" => { type: :enum, allowed: ["auto", "bash", "powershell"] },
42
42
  "security.allow_safe_probes" => { type: :boolean },
43
43
  "security.command_allowlist" => { type: :array },
44
- "network.allow" => { type: :boolean },
45
44
  "buffer.auto_apply" => { type: :enum, allowed: ["manual", "on_test", "on_finish", "always"] },
46
45
  "buffer.model_can_apply" => { type: :boolean },
47
46
  "buffer.prompt_indicator" => { type: :boolean },
data/lib/rubik/config.rb CHANGED
@@ -18,6 +18,7 @@ module Rubik
18
18
  include ModelCatalog
19
19
 
20
20
  DEFAULT_CONFIG = Defaults::DEFAULT_CONFIG
21
+ SENSITIVE_PROJECT_CONFIG_KEYS = DeepOps::SENSITIVE_PROJECT_CONFIG_KEYS
21
22
 
22
23
  attr_reader :data, :warnings, :root_dir, :home_dir,
23
24
  :defaults_layer, :global_layer, :project_layer, :env_layer, :session_layer
@@ -34,9 +35,7 @@ module Rubik
34
35
  @event_bus = nil
35
36
  @session_layer = {}
36
37
  @defaults_layer = deep_dup(DEFAULT_CONFIG)
37
- @global_layer = {}
38
- @project_layer = {}
39
- @env_layer = {}
38
+ @global_layer = @project_layer = @env_layer = {}
40
39
  ensure_rubik_directories
41
40
  @data = load_merged_config
42
41
  end
@@ -283,6 +282,11 @@ module Rubik
283
282
  @global_layer = {} unless @global_layer.is_a?(Hash)
284
283
  @project_layer = load_yaml(project_config_path)
285
284
  @project_layer = {} unless @project_layer.is_a?(Hash)
285
+
286
+ if @project_layer.any? && defined?(Rubik::CLI::WorkspaceTrust) && !Rubik::CLI::WorkspaceTrust.trusted?(@root_dir)
287
+ @project_layer = filter_untrusted_project_config(@project_layer)
288
+ end
289
+
286
290
  @env_layer = EnvLoader.load_env(ENV, DEFAULT_CONFIG, @warnings)
287
291
  @session_layer = {}
288
292
 
@@ -29,9 +29,10 @@ module Rubik
29
29
  "Gemfile", "package.json", "Cargo.toml", "go.mod", "Makefile", "pyproject.toml", "Rakefile", "index.html", "README.md"
30
30
  ].freeze
31
31
 
32
- def initialize(root_dir: Dir.pwd, protocol_candidates: DEFAULT_PROTOCOL_CANDIDATES)
32
+ def initialize(root_dir: Dir.pwd, protocol_candidates: DEFAULT_PROTOCOL_CANDIDATES, config: nil)
33
33
  @root_dir = root_dir
34
34
  @protocol_candidates = protocol_candidates
35
+ @config = config
35
36
  end
36
37
 
37
38
  # Returns a Hash with three keys, each pre-extracted and pre-rendered
@@ -53,17 +54,35 @@ module Rubik
53
54
  end
54
55
 
55
56
  # Render the matched protocol files into an XML block.
57
+ # Caps content to avoid blowing out the context window (F3) and neutralizes
58
+ # closing tags to prevent prompt injection.
56
59
  def read_protocol_xml
57
60
  files = find_protocol_files
58
61
  return "" if files.empty?
59
62
 
60
- files.map do |f|
61
- content = Rubik::Platform.read_text(f[:full_path]).strip
62
- <<~XML
63
- <protocol source="#{f[:path]}">
64
- #{content}
65
- </protocol>
66
- XML
63
+ selection = (@config&.fetch("context.protocol_selection", "first") || "first").to_s
64
+ effective_files = selection == "all" ? files : files.first(1)
65
+ max_tokens = (@config&.fetch("context.protocol_max_tokens", 8192) || 8192).to_i
66
+ max_chars = [max_tokens * 4, 4096].max
67
+
68
+ effective_files.map do |f|
69
+ raw_content = Rubik::Platform.read_text(f[:full_path]).strip
70
+ # Neutralize closing tags to prevent section breakout
71
+ content = raw_content.gsub("</protocol>", "&lt;/protocol&gt;")
72
+ if content.length > max_chars
73
+ trunc = Rubik::Tools::Truncator.truncate(content, max_chars: max_chars, strategy: :head_tail)
74
+ <<~XML
75
+ <protocol source="#{f[:path]}" truncated="true">
76
+ #{trunc[:text]}
77
+ </protocol>
78
+ XML
79
+ else
80
+ <<~XML
81
+ <protocol source="#{f[:path]}">
82
+ #{content}
83
+ </protocol>
84
+ XML
85
+ end
67
86
  end.join("\n").strip
68
87
  end
69
88
 
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ # THE single home of the event bus's name vocabulary. Publishers and
5
+ # subscribers across layers (orchestrator, repl, storage, kernel) read
6
+ # these constants instead of redeclaring the names — a redeclaration is
7
+ # one rename away from a silently dead subscription. Zero requires, a
8
+ # dependency-free leaf like Coeffects/Uris: any layer can bind to the
9
+ # shared names without dragging another layer's logic in.
10
+ module Events
11
+ SUBAGENT_COMPLETED = :subagent_completed
12
+ SUBAGENT_MESSAGE_TO_PARENT = :subagent_message_to_parent
13
+ TURN_FINISHED = :turn_finished
14
+ GOAL_FINISHED = :goal_finished
15
+ GOAL_SUSPENDED = :goal_suspended
16
+ UNIT_STATE_CHANGED = :unit_state_changed
17
+ end
18
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "monitor"
4
+ require_relative "../events"
4
5
 
5
6
  module Rubik
6
7
  module Kernel
@@ -98,9 +99,9 @@ module Rubik
98
99
  level = TRANSITION_SEVERITIES.fetch(to_state, :info)
99
100
  subsystem = (@type == :subagent ? :subagent : :kernel)
100
101
  @dmesg&.log(subsystem, level, msg, facility_id: @facility_id, unit_id: @id, from_state: from_state, to_state: to_state)
101
- @dispatcher&.post(:unit_state_changed, unit_id: @id, type: @type, from_state: from_state,
102
- to_state: to_state, reason: reason, facility_id: @facility_id,
103
- subsystem: subsystem, level: level)
102
+ @dispatcher&.post(Rubik::Events::UNIT_STATE_CHANGED, unit_id: @id, type: @type, from_state: from_state,
103
+ to_state: to_state, reason: reason, facility_id: @facility_id,
104
+ subsystem: subsystem, level: level)
104
105
  end
105
106
  end
106
107
 
@@ -58,6 +58,8 @@ module Rubik
58
58
  # overrides config resolution entirely — the StepExecutor injection
59
59
  # seam (its tests pass explicit modes positionally).
60
60
  def apply?(config, trigger:, subagent: false, tool: nil, mode: nil)
61
+ return false if config.respond_to?(:fetch) && config.fetch("buffer.model_can_apply", true) == false
62
+
61
63
  return false if trigger == :pre_execution && !tool.nil? && !test_class_tool?(tool)
62
64
 
63
65
  resolved = mode || (subagent && subagent_flag_on?(config) ? "always" : self.mode(config))
@@ -39,12 +39,20 @@ module Rubik
39
39
  end
40
40
 
41
41
  def evaluate_context_compaction!
42
+ project_goal_ledger_observations! if @deps.respond_to?(:goal_mode?) && @deps.goal_mode?
43
+
42
44
  return unless @deps.context_limit
43
45
 
44
46
  context_cfg = ProviderRecovery.compaction_config(@deps.config)
47
+ reserved = if @deps.respond_to?(:tool_registry) && @deps.tool_registry&.schemas
48
+ Rubik::Prompt::TokenEstimator.estimate_tool_schemas(@deps.tool_registry.schemas)
49
+ else
50
+ 0
51
+ end
45
52
  needs = Rubik::Prompt::ContextCompactor.needs_compaction?(
46
53
  @deps.history, context_limit: @deps.context_limit,
47
- max_output_tokens: @deps.max_output_tokens, config: context_cfg
54
+ max_output_tokens: @deps.max_output_tokens, config: context_cfg,
55
+ reserved_tokens: reserved
48
56
  )
49
57
  return unless needs
50
58
 
@@ -55,12 +63,20 @@ module Rubik
55
63
  @deps.replace_history!(Rubik::Prompt::ContextCompactor.compact_history(
56
64
  @deps.history, context_limit: @deps.context_limit,
57
65
  max_output_tokens: @deps.max_output_tokens, config: context_cfg,
58
- absorb: GoalLedger::ObservationProjection.directive_for(deps: @deps)
66
+ absorb: GoalLedger::ObservationProjection.directive_for(deps: @deps),
67
+ reserved_tokens: reserved
59
68
  ))
60
69
  manager = @deps.runtime_manager if @deps.respond_to?(:runtime_manager)
61
70
  Rubik::Storage::Vakanuvis::Orientation.inject_after_compaction!(@deps.history, manager, config: @deps.config)
62
71
  end
63
72
 
73
+ def project_goal_ledger_observations!
74
+ directive = GoalLedger::ObservationProjection.directive_for(deps: @deps)
75
+ directive&.apply!(@deps.history)
76
+ rescue StandardError => e
77
+ Rubik::Log.warn(:orchestrator, "goal ledger observation projection failed (#{e.class}: #{e.message}); step continues")
78
+ end
79
+
64
80
  def track_token_usage!(res)
65
81
  return unless res.respond_to?(:usage) && res.usage
66
82
 
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module Orchestrator
5
+ module GoalLedger
6
+ # Σ ailesinin derin kopya/donma disiplininin TEK evi (aynı iki yordamın
7
+ # ledger/validator/patch'teki beş el kopyası yerine bir sayfa): Hash
8
+ # anahtarları da kopyalanır/donar; yaprak skalarlar paylaşılır (zaten
9
+ # immutable). Ledger include eder, Validator ve Patch extend eder —
10
+ # yöntemler private kalır, yalnız implicit self ile erişilir.
11
+ #
12
+ # Rubik::Config::DeepOps ile aynı adı taşır ama semantiği bilinçli
13
+ # olarak farklıdır: burada yaprak String DUP EDİLMEZ (config kopyası
14
+ # savunmacıdır; Σ dünyasında String'ler zaten JSON round-trip'i ile
15
+ # taze akar — paylaşım güvenlidir ve donma sözleşmesini bozmaz).
16
+ module DeepOps
17
+ private
18
+
19
+ def deep_dup(value)
20
+ case value
21
+ when Hash then value.to_a.to_h { |key, val| [key.dup, deep_dup(val)] }
22
+ when Array then value.map { |val| deep_dup(val) }
23
+ else value
24
+ end
25
+ end
26
+
27
+ def deep_freeze(value)
28
+ case value
29
+ when Hash then value.each do |key, val|
30
+ deep_freeze(key)
31
+ deep_freeze(val)
32
+ end.freeze
33
+ when Array then value.each { |val| deep_freeze(val) }.freeze
34
+ when String then value.freeze
35
+ else value
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -63,7 +63,8 @@ module Rubik
63
63
 
64
64
  esc = Rubik::Platform.method(:xml_escape)
65
65
  body = entries.last(Drift::MAX_NOTE_ENTRIES).map { |line| esc.call(line.to_s.slice(0, Drift::MAX_NOTE_CHARS)) }
66
- %(<ledger_drift rev="#{snap['rev']}" entries="#{body.size}">\n #{body.join("\n ")}\n</ledger_drift>)
66
+ tag = Prompt::ContextCompactor::LEDGER_DRIFT_TAG
67
+ %(<#{tag} rev="#{snap['rev']}" entries="#{body.size}">\n #{body.join("\n ")}\n</#{tag}>)
67
68
  end
68
69
  end
69
70
  end