textus 0.29.0 → 0.35.1

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 (99) hide show
  1. checksums.yaml +4 -4
  2. data/ARCHITECTURE.md +2 -235
  3. data/CHANGELOG.md +169 -0
  4. data/README.md +85 -64
  5. data/SPEC.md +366 -201
  6. data/docs/conventions.md +42 -37
  7. data/lib/textus/boot.rb +93 -76
  8. data/lib/textus/cli/group/{refresh.rb → fetch.rb} +4 -4
  9. data/lib/textus/cli/verb/build.rb +1 -1
  10. data/lib/textus/cli/verb/fetch.rb +14 -0
  11. data/lib/textus/cli/verb/{refresh_stale.rb → fetch_stale.rb} +3 -3
  12. data/lib/textus/cli/verb/get.rb +1 -1
  13. data/lib/textus/cli/verb/hook_run.rb +2 -6
  14. data/lib/textus/cli/verb/hooks.rb +1 -1
  15. data/lib/textus/cli/verb/put.rb +5 -14
  16. data/lib/textus/cli/verb/retain.rb +19 -0
  17. data/lib/textus/cli/verb/rule_list.rb +8 -8
  18. data/lib/textus/cli.rb +21 -18
  19. data/lib/textus/container.rb +1 -2
  20. data/lib/textus/dispatcher.rb +11 -3
  21. data/lib/textus/doctor/check/{refresh_locks.rb → fetch_locks.rb} +7 -7
  22. data/lib/textus/doctor/check/proposal_targets.rb +45 -0
  23. data/lib/textus/doctor/check/rule_ambiguity.rb +3 -3
  24. data/lib/textus/doctor/check.rb +8 -5
  25. data/lib/textus/doctor.rb +2 -1
  26. data/lib/textus/domain/action.rb +3 -3
  27. data/lib/textus/domain/duration.rb +22 -0
  28. data/lib/textus/domain/freshness/evaluator.rb +3 -3
  29. data/lib/textus/domain/freshness/policy.rb +2 -2
  30. data/lib/textus/domain/freshness.rb +7 -7
  31. data/lib/textus/domain/outcome.rb +2 -2
  32. data/lib/textus/domain/permission.rb +2 -10
  33. data/lib/textus/domain/policy/base_guards.rb +25 -0
  34. data/lib/textus/domain/policy/evaluation.rb +18 -0
  35. data/lib/textus/domain/policy/{refresh.rb → fetch.rb} +2 -16
  36. data/lib/textus/domain/policy/guard.rb +35 -0
  37. data/lib/textus/domain/policy/guard_factory.rb +40 -0
  38. data/lib/textus/domain/policy/predicates/author_held.rb +33 -0
  39. data/lib/textus/domain/policy/predicates/etag_match.rb +32 -0
  40. data/lib/textus/domain/policy/predicates/fresh_within.rb +58 -0
  41. data/lib/textus/domain/policy/predicates/registry.rb +39 -0
  42. data/lib/textus/domain/policy/predicates/schema_valid.rb +30 -19
  43. data/lib/textus/domain/policy/predicates/target_is_canon.rb +33 -0
  44. data/lib/textus/domain/policy/predicates/zone_writable_by.rb +39 -0
  45. data/lib/textus/domain/policy/retention.rb +26 -0
  46. data/lib/textus/domain/retention.rb +44 -0
  47. data/lib/textus/domain/staleness/intake_check.rb +6 -6
  48. data/lib/textus/envelope/io/reader.rb +4 -0
  49. data/lib/textus/envelope/io/writer.rb +8 -0
  50. data/lib/textus/envelope.rb +2 -2
  51. data/lib/textus/errors.rb +25 -28
  52. data/lib/textus/hooks/event_bus.rb +12 -24
  53. data/lib/textus/hooks/rpc_registry.rb +9 -35
  54. data/lib/textus/hooks/signature.rb +31 -0
  55. data/lib/textus/init.rb +24 -18
  56. data/lib/textus/maintenance/zone_mv.rb +1 -1
  57. data/lib/textus/manifest/capabilities.rb +29 -0
  58. data/lib/textus/manifest/data.rb +16 -8
  59. data/lib/textus/manifest/entry/base.rb +2 -2
  60. data/lib/textus/manifest/policy.rb +62 -19
  61. data/lib/textus/manifest/rules.rb +25 -14
  62. data/lib/textus/manifest/schema.rb +78 -38
  63. data/lib/textus/manifest.rb +6 -5
  64. data/lib/textus/mcp/server.rb +2 -10
  65. data/lib/textus/mcp/session.rb +7 -23
  66. data/lib/textus/mcp/tool_schemas.rb +3 -3
  67. data/lib/textus/mcp/tools.rb +7 -7
  68. data/lib/textus/ports/audit_subscriber.rb +1 -1
  69. data/lib/textus/ports/{refresh → fetch}/detached.rb +4 -4
  70. data/lib/textus/ports/{refresh → fetch}/lock.rb +1 -1
  71. data/lib/textus/projection.rb +1 -1
  72. data/lib/textus/read/freshness.rb +9 -9
  73. data/lib/textus/read/get.rb +8 -8
  74. data/lib/textus/read/{get_or_refresh.rb → get_or_fetch.rb} +11 -11
  75. data/lib/textus/read/policy_explain.rb +19 -10
  76. data/lib/textus/read/pulse.rb +5 -4
  77. data/lib/textus/read/retainable.rb +17 -0
  78. data/lib/textus/read/validator.rb +1 -1
  79. data/lib/textus/role_scope.rb +3 -2
  80. data/lib/textus/schema/tools.rb +5 -5
  81. data/lib/textus/version.rb +1 -1
  82. data/lib/textus/write/accept.rb +19 -55
  83. data/lib/textus/write/delete.rb +15 -17
  84. data/lib/textus/write/{refresh_all.rb → fetch_all.rb} +6 -6
  85. data/lib/textus/write/{refresh_orchestrator.rb → fetch_orchestrator.rb} +14 -14
  86. data/lib/textus/write/{refresh_worker.rb → fetch_worker.rb} +23 -30
  87. data/lib/textus/write/intake_fetch.rb +23 -0
  88. data/lib/textus/write/mv.rb +17 -15
  89. data/lib/textus/write/put.rb +15 -17
  90. data/lib/textus/write/reject.rb +11 -5
  91. data/lib/textus/write/retention_sweep.rb +55 -0
  92. metadata +32 -18
  93. data/lib/textus/cli/verb/refresh.rb +0 -14
  94. data/lib/textus/domain/authorizer.rb +0 -37
  95. data/lib/textus/domain/policy/predicates/accept_authority_signed.rb +0 -33
  96. data/lib/textus/domain/policy/promote.rb +0 -26
  97. data/lib/textus/domain/policy/promotion.rb +0 -57
  98. data/lib/textus/manifest/role_kinds.rb +0 -21
  99. data/lib/textus/write/authority_gate.rb +0 -24
@@ -11,12 +11,13 @@ module Textus
11
11
  accept: Textus::Write::Accept,
12
12
  reject: Textus::Write::Reject,
13
13
  publish: Textus::Write::Publish,
14
- refresh: Textus::Write::RefreshWorker,
15
- refresh_all: Textus::Write::RefreshAll,
14
+ fetch: Textus::Write::FetchWorker,
15
+ fetch_all: Textus::Write::FetchAll,
16
+ retention_sweep: Textus::Write::RetentionSweep,
16
17
 
17
18
  # Read
18
19
  get: Textus::Read::Get,
19
- get_or_refresh: Textus::Read::GetOrRefresh,
20
+ get_or_fetch: Textus::Read::GetOrFetch,
20
21
  list: Textus::Read::List,
21
22
  where: Textus::Read::Where,
22
23
  uid: Textus::Read::Uid,
@@ -33,6 +34,7 @@ module Textus
33
34
  validate_all: Textus::Read::ValidateAll,
34
35
  doctor: Textus::Read::Doctor,
35
36
  boot: Textus::Read::Boot,
37
+ retainable: Textus::Read::Retainable,
36
38
 
37
39
  # Maintenance
38
40
  migrate: Textus::Maintenance::Migrate,
@@ -45,5 +47,11 @@ module Textus
45
47
  def self.fetch(verb)
46
48
  VERBS.fetch(verb.to_sym) { raise UsageError.new("unknown verb: #{verb.inspect}") }
47
49
  end
50
+
51
+ # Single home for the uniform use-case invocation protocol (ADR 0023):
52
+ # look up the verb, construct on (container:, call:), and invoke #call.
53
+ def self.invoke(verb, container:, call:, args: [], kwargs: {})
54
+ fetch(verb).new(container: container, call: call).call(*args, **kwargs)
55
+ end
48
56
  end
49
57
  end
@@ -1,13 +1,13 @@
1
1
  module Textus
2
2
  module Doctor
3
3
  class Check
4
- # Lists per-key refresh lock files under <root>/.locks/ whose
4
+ # Lists per-key fetch lock files under <root>/.locks/ whose
5
5
  # recorded PID is no longer running. These are forensic artifacts only:
6
- # Refresh::Lock uses flock(2), which the kernel releases on process
6
+ # Fetch::Lock uses flock(2), which the kernel releases on process
7
7
  # death, so stale files do not block subsequent acquires. The check
8
8
  # exists to let users clean up clutter and notice unexpected accumulation
9
- # (e.g. a refresh path that crashes repeatedly).
10
- class RefreshLocks < Check
9
+ # (e.g. a fetch path that crashes repeatedly).
10
+ class FetchLocks < Check
11
11
  def call
12
12
  dir = File.join(root, ".locks")
13
13
  return [] unless File.directory?(dir)
@@ -23,11 +23,11 @@ module Textus
23
23
  return nil if pid_alive?(pid)
24
24
 
25
25
  {
26
- "code" => "refresh_lock.stale",
26
+ "code" => "fetch_lock.stale",
27
27
  "level" => "info",
28
28
  "subject" => path,
29
- "message" => "refresh lock file at #{path} records dead PID #{pid} " \
30
- "(does not block refresh; flock is kernel-released on exit)",
29
+ "message" => "fetch lock file at #{path} records dead PID #{pid} " \
30
+ "(does not block fetch; flock is kernel-released on exit)",
31
31
  "fix" => "safe to delete: rm #{path}",
32
32
  }
33
33
  rescue Errno::ENOENT
@@ -0,0 +1,45 @@
1
+ module Textus
2
+ module Doctor
3
+ class Check
4
+ # Flags pending proposals whose `proposal.target_key` cannot ever be
5
+ # accepted: it points at a non-canon zone or resolves to no declared
6
+ # entry (ADR 0035). Reads the live queue zone; silent when there is no
7
+ # queue zone. Warnings, not errors — they are stale junk, not store
8
+ # corruption (the accept gate already refuses them).
9
+ class ProposalTargets < Check
10
+ def call
11
+ queue = manifest.policy.queue_zone
12
+ return [] unless queue
13
+
14
+ dispatch(:list, zone: queue).filter_map { |row| issue_for(row["key"]) }
15
+ end
16
+
17
+ private
18
+
19
+ def issue_for(key)
20
+ target = dispatch(:get, key).meta&.dig("proposal", "target_key")
21
+ return nil if target.nil? # not a proposal entry — skip
22
+
23
+ zone = manifest.resolver.resolve(target).entry.zone
24
+ return nil if manifest.policy.declared_kind(zone.to_s) == :canon
25
+
26
+ {
27
+ "code" => "proposal.target_not_canon",
28
+ "level" => "warning",
29
+ "subject" => key,
30
+ "message" => "proposal '#{key}' targets '#{target}' in zone '#{zone}' (not canon); it can never be accepted",
31
+ "fix" => "delete the proposal, or repoint target_key at a canon zone",
32
+ }
33
+ rescue Textus::UnknownKey
34
+ {
35
+ "code" => "proposal.target_unresolved",
36
+ "level" => "warning",
37
+ "subject" => key,
38
+ "message" => "proposal '#{key}' targets '#{target}', which resolves to no declared entry",
39
+ "fix" => "delete the proposal, or fix target_key",
40
+ }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -2,11 +2,11 @@ module Textus
2
2
  module Doctor
3
3
  class Check
4
4
  # Flags entries whose key is matched by two or more rule blocks of the
5
- # SAME specificity in the same slot (refresh / handler_allowlist /
6
- # promote). Ties are non-deterministic in the parser's pick step, so
5
+ # SAME specificity in the same slot (fetch / handler_allowlist /
6
+ # guard). Ties are non-deterministic in the parser's pick step, so
7
7
  # they're a configuration smell — surface them.
8
8
  class RuleAmbiguity < Check
9
- SLOTS = %i[refresh handler_allowlist promote].freeze
9
+ SLOTS = %i[fetch handler_allowlist guard].freeze
10
10
 
11
11
  def call
12
12
  out = []
@@ -28,11 +28,14 @@ module Textus
28
28
  def manifest = @container.manifest
29
29
  def rpc = @container.rpc
30
30
 
31
- # Dispatch a verb through the static Dispatcher table.
32
- def dispatch(verb, *, **)
33
- klass = Textus::Dispatcher.fetch(verb)
34
- call_value = Textus::Call.build(role: Textus::Role::DEFAULT)
35
- klass.new(container: @container, call: call_value).call(*, **)
31
+ # Dispatch a verb through the single use-case invocation seam (ADR 0026).
32
+ def dispatch(verb, *args, **kwargs)
33
+ Textus::Dispatcher.invoke(
34
+ verb,
35
+ container: @container,
36
+ call: Textus::Call.build(role: Textus::Role::DEFAULT),
37
+ args: args, kwargs: kwargs
38
+ )
36
39
  end
37
40
  end
38
41
  end
data/lib/textus/doctor.rb CHANGED
@@ -23,7 +23,8 @@ module Textus
23
23
  Check::SchemaViolations,
24
24
  Check::RuleAmbiguity,
25
25
  Check::HandlerAllowlist,
26
- Check::RefreshLocks,
26
+ Check::FetchLocks,
27
+ Check::ProposalTargets,
27
28
  ].freeze
28
29
 
29
30
  ALL_CHECKS = CHECKS.map(&:name_key).freeze
@@ -1,9 +1,9 @@
1
1
  module Textus
2
2
  module Domain
3
3
  module Action
4
- Return = Data.define
5
- RefreshSync = Data.define
6
- RefreshTimed = Data.define(:budget_ms)
4
+ Return = Data.define
5
+ FetchSync = Data.define
6
+ FetchTimed = Data.define(:budget_ms)
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,22 @@
1
+ module Textus
2
+ module Domain
3
+ # Parses a duration value into whole seconds. Accepts a bare integer (or
4
+ # integer-string) of seconds, or `<n><unit>` with unit s/m/h/d. Returns
5
+ # nil for nil or any unparseable value.
6
+ module Duration
7
+ UNIT_SECONDS = { "s" => 1, "m" => 60, "h" => 3600, "d" => 86_400 }.freeze
8
+
9
+ def self.seconds(value)
10
+ return nil if value.nil?
11
+
12
+ str = value.to_s.strip
13
+ return str.to_i if str.match?(/\A\d+\z/)
14
+
15
+ m = str.match(/\A(\d+)\s*([smhd])\z/)
16
+ return nil unless m
17
+
18
+ m[1].to_i * UNIT_SECONDS.fetch(m[2])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -9,15 +9,15 @@ module Textus
9
9
  def call(policy, envelope, now:)
10
10
  return Verdict.fresh if policy.ttl_seconds.nil?
11
11
 
12
- last_str = envelope&.meta&.dig("last_refreshed_at")
13
- return Verdict.stale("never refreshed") if last_str.nil?
12
+ last_str = envelope&.meta&.dig("last_fetched_at")
13
+ return Verdict.stale("never fetched") if last_str.nil?
14
14
 
15
15
  last = begin
16
16
  Time.parse(last_str.to_s)
17
17
  rescue ArgumentError, TypeError
18
18
  nil
19
19
  end
20
- return Verdict.stale("unparseable last_refreshed_at: #{last_str.inspect}") if last.nil?
20
+ return Verdict.stale("unparseable last_fetched_at: #{last_str.inspect}") if last.nil?
21
21
 
22
22
  age = now - last
23
23
  return Verdict.fresh if age <= policy.ttl_seconds
@@ -7,8 +7,8 @@ module Textus
7
7
 
8
8
  case on_stale
9
9
  when :warn then Action::Return.new
10
- when :sync then Action::RefreshSync.new
11
- when :timed_sync then Action::RefreshTimed.new(budget_ms: sync_budget_ms)
10
+ when :sync then Action::FetchSync.new
11
+ when :timed_sync then Action::FetchTimed.new(budget_ms: sync_budget_ms)
12
12
  else Action::Return.new
13
13
  end
14
14
  end
@@ -8,19 +8,19 @@ module Textus
8
8
  #
9
9
  # Note on wire format: `#to_h_for_wire` is intentionally narrower than the
10
10
  # full field set. It emits the legacy keys ("stale", "stale_reason",
11
- # "refreshing", and "refresh_error" when present) so the CLI JSON wire
11
+ # "fetching", and "fetch_error" when present) so the CLI JSON wire
12
12
  # stays byte-identical with textus/3. The gem-side fields `checked_at`
13
13
  # and `ttl_remaining_ms` are NOT emitted on the wire in this phase.
14
14
  Freshness = Data.define(
15
- :stale, :refreshing, :reason, :refresh_error, :checked_at, :ttl_remaining_ms
15
+ :stale, :fetching, :reason, :fetch_error, :checked_at, :ttl_remaining_ms
16
16
  ) do
17
- def self.build(stale:, refreshing: false, reason: nil, refresh_error: nil,
17
+ def self.build(stale:, fetching: false, reason: nil, fetch_error: nil,
18
18
  checked_at: nil, ttl_remaining_ms: nil)
19
19
  new(
20
20
  stale: stale,
21
- refreshing: refreshing,
21
+ fetching: fetching,
22
22
  reason: reason,
23
- refresh_error: refresh_error,
23
+ fetch_error: fetch_error,
24
24
  checked_at: checked_at,
25
25
  ttl_remaining_ms: ttl_remaining_ms,
26
26
  )
@@ -30,9 +30,9 @@ module Textus
30
30
  h = {
31
31
  "stale" => stale,
32
32
  "stale_reason" => reason,
33
- "refreshing" => refreshing,
33
+ "fetching" => fetching,
34
34
  }
35
- h["refresh_error"] = refresh_error unless refresh_error.nil?
35
+ h["fetch_error"] = fetch_error unless fetch_error.nil?
36
36
  h
37
37
  end
38
38
  end
@@ -1,8 +1,8 @@
1
1
  module Textus
2
2
  module Domain
3
3
  module Outcome
4
- Skipped = Data.define
5
- Refreshed = Data.define(:envelope)
4
+ Skipped = Data.define
5
+ Fetched = Data.define(:envelope)
6
6
  Detached = Data.define
7
7
  Failed = Data.define(:error)
8
8
  end
@@ -1,15 +1,7 @@
1
1
  module Textus
2
2
  module Domain
3
- Permission = Data.define(:zone, :write_policy, :read_policy) do
4
- def allows_write?(role)
5
- write_policy.include?(role.to_s)
6
- end
7
-
8
- def allows_read?(role)
9
- return true if [:all, ["all"]].include?(read_policy)
10
-
11
- read_policy.include?(role.to_s)
12
- end
3
+ Permission = Data.define(:zone, :writers) do
4
+ def allows_write?(role) = writers.include?(role.to_s)
13
5
  end
14
6
  end
15
7
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Domain
5
+ module Policy
6
+ # The CLOSED floor (ADR 0031 §4): predicate names every transition
7
+ # evaluates regardless of rules:. rules[].guard only ADDS to these.
8
+ module BaseGuards
9
+ # The minimal floor — only what the verb is meaningless without.
10
+ # schema_valid / etag_match / fresh_within are NOT here: they are
11
+ # composable-only, added per-key via rules[].guard (ADR 0031).
12
+ BASE = {
13
+ put: %w[zone_writable_by],
14
+ delete: %w[zone_writable_by],
15
+ mv: %w[zone_writable_by],
16
+ accept: %w[author_held target_is_canon],
17
+ reject: %w[author_held],
18
+ fetch: %w[zone_writable_by],
19
+ }.freeze
20
+
21
+ def self.for(transition) = BASE.fetch(transition, [])
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Domain
5
+ module Policy
6
+ # Immutable context handed to every predicate. `snapshot` is the
7
+ # manifest (pure, no I/O); `envelope` is the entry under evaluation
8
+ # (nil when no bytes exist yet, e.g. a fresh put). `origin`/`target`
9
+ # are dotted keys; `transition` is the verb symbol.
10
+ Evaluation = Data.define(
11
+ :actor, :transition, :origin, :target, :envelope, :snapshot
12
+ ) do
13
+ def manifest = snapshot
14
+ def role = actor
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,7 +1,7 @@
1
1
  module Textus
2
2
  module Domain
3
3
  module Policy
4
- class Refresh
4
+ class Fetch
5
5
  ALLOWED_ON_STALE = %i[warn sync timed_sync].freeze
6
6
 
7
7
  attr_reader :ttl, :on_stale, :sync_budget_ms, :fetch_timeout_seconds
@@ -21,21 +21,7 @@ module Textus
21
21
  end
22
22
 
23
23
  def ttl_seconds
24
- return nil if @ttl.nil?
25
-
26
- str = @ttl.to_s.strip
27
- return str.to_i if str.match?(/\A\d+\z/)
28
-
29
- m = str.match(/\A(\d+)\s*([smhd])\z/)
30
- return nil unless m
31
-
32
- n = m[1].to_i
33
- case m[2]
34
- when "s" then n
35
- when "m" then n * 60
36
- when "h" then n * 3600
37
- when "d" then n * 86_400
38
- end
24
+ Textus::Domain::Duration.seconds(@ttl)
39
25
  end
40
26
 
41
27
  def to_freshness_policy
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Domain
5
+ module Policy
6
+ # An ordered list of pure predicates over one Evaluation (ADR 0031).
7
+ # check! short-circuits on the first failing predicate that defines a
8
+ # bespoke #error (only zone_writable_by → WriteForbidden, the product's
9
+ # legible topology refusal); every other failure accumulates into
10
+ # GuardFailed naming the unmet predicate(s).
11
+ class Guard
12
+ attr_reader :predicates
13
+
14
+ def initialize(predicates)
15
+ @predicates = predicates
16
+ end
17
+
18
+ def check!(eval)
19
+ accumulated = []
20
+ @predicates.each do |pred|
21
+ next if pred.call(eval)
22
+ raise pred.error(eval) if pred.respond_to?(:error)
23
+
24
+ accumulated << [pred.name, pred.reason]
25
+ end
26
+ raise Textus::GuardFailed.new(accumulated) unless accumulated.empty?
27
+ end
28
+
29
+ def explain(eval)
30
+ @predicates.map { |p| [p.name, p.call(eval), p.reason] }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Domain
5
+ module Policy
6
+ # Builds the effective Guard for (transition, key): base floor ++
7
+ # the predicates declared under rules[].guard[transition]. The single
8
+ # place the closed floor and the open ceiling are composed.
9
+ class GuardFactory
10
+ def initialize(manifest:, schemas:, extra: {})
11
+ @manifest = manifest
12
+ @schemas = schemas
13
+ @extra = extra # transient per-call params, e.g. { if_etag: "..." }
14
+ end
15
+
16
+ def for(transition, key)
17
+ specs = BaseGuards.for(transition) + composed(transition, key)
18
+ predicates = specs.map { |spec| build(spec) }.uniq(&:name)
19
+ Guard.new(predicates)
20
+ end
21
+
22
+ private
23
+
24
+ def composed(transition, key)
25
+ guard_map = @manifest.rules.for(key).guard
26
+ return [] if guard_map.nil?
27
+
28
+ Array(guard_map[transition.to_s])
29
+ end
30
+
31
+ def build(spec)
32
+ # etag_match takes a per-call param rather than a manifest one.
33
+ return Predicates::EtagMatch.new(if_etag: @extra[:if_etag]) if spec == "etag_match"
34
+
35
+ Predicates::Registry.build(spec, schemas: @schemas)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Domain
5
+ module Policy
6
+ module Predicates
7
+ # Predicate: the acting role must hold the 'author' capability in the
8
+ # active manifest (ADR 0030 capability roles). Folds in the old
9
+ # Write::AuthorityGate so accept/reject and rules[].guard share one
10
+ # implementation. No bespoke #error — failures accumulate into
11
+ # GuardFailed (ADR 0031).
12
+ class AuthorHeld
13
+ attr_reader :reason
14
+
15
+ def name = "author_held"
16
+
17
+ def call(eval)
18
+ holders = eval.manifest.policy.roles_with_capability("author")
19
+ return true if holders.include?(eval.actor.to_s)
20
+
21
+ @reason =
22
+ if holders.empty?
23
+ "no role holds the 'author' capability; #{eval.transition} is disabled"
24
+ else
25
+ "role '#{eval.actor}' lacks the 'author' capability (held by: #{holders.join(", ")})"
26
+ end
27
+ false
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Domain
5
+ module Policy
6
+ module Predicates
7
+ # Advisory pre-flight etag check for policy explain. The
8
+ # authoritative compare-and-write stays in Envelope::IO::Writer
9
+ # (atomic write-then-audit, ADR 0017). Passes when no if_etag is
10
+ # supplied (params[:if_etag] nil) — guard does not require it.
11
+ class EtagMatch
12
+ attr_reader :reason
13
+
14
+ def initialize(if_etag: nil)
15
+ @if_etag = if_etag
16
+ end
17
+
18
+ def name = "etag_match"
19
+
20
+ def call(eval)
21
+ return true if @if_etag.nil?
22
+ return true if eval.envelope.nil? # creating; Writer handles race
23
+ return true if eval.envelope.etag == @if_etag
24
+
25
+ @reason = "etag mismatch: wanted #{@if_etag}, have #{eval.envelope.etag}"
26
+ false
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+
5
+ module Textus
6
+ module Domain
7
+ module Policy
8
+ module Predicates
9
+ # Parameterized predicate: the entry must have been written within
10
+ # `duration` of now. Duration strings ("1h", "30m", "7d") parse via
11
+ # Domain::Duration.seconds. Passes when no envelope exists yet.
12
+ class FreshWithin
13
+ attr_reader :reason
14
+
15
+ def initialize(duration:, now: nil)
16
+ @seconds = Textus::Domain::Duration.seconds(duration)
17
+ @now = now
18
+ end
19
+
20
+ def name = "fresh_within"
21
+
22
+ def call(eval)
23
+ return true if eval.envelope.nil? || @seconds.nil?
24
+
25
+ written = written_at(eval.envelope)
26
+ return true if written.nil?
27
+
28
+ now = @now || Textus::Ports::Clock.now
29
+ return true if now - written <= @seconds
30
+
31
+ @reason = "entry older than #{@seconds}s (written #{written.iso8601})"
32
+ false
33
+ end
34
+
35
+ private
36
+
37
+ # Domain-pure: reads the stored write timestamp from the envelope's
38
+ # freshness (checked_at) or meta (last_fetched_at/generated_at) and
39
+ # parses the stored ISO-8601 string. Parsing a stored string is not
40
+ # I/O (allowed in domain, ADR 0024).
41
+ def written_at(envelope)
42
+ raw = envelope.freshness&.checked_at ||
43
+ envelope.meta&.dig("last_fetched_at") ||
44
+ envelope.meta&.dig("generated_at")
45
+ return raw if raw.is_a?(Time)
46
+ return nil if raw.nil?
47
+
48
+ begin
49
+ Time.parse(raw.to_s)
50
+ rescue StandardError
51
+ nil
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Domain
5
+ module Policy
6
+ module Predicates
7
+ # The single source of truth for the predicate vocabulary
8
+ # (ADR 0031 §3). Replaces both Promote::KNOWN and Promotion::REGISTRY.
9
+ # Each entry is name => ->(params:, schemas:) { predicate }.
10
+ module Registry
11
+ ENTRIES = {
12
+ "zone_writable_by" => ->(**) { ZoneWritableBy.new },
13
+ "author_held" => ->(**) { AuthorHeld.new },
14
+ "target_is_canon" => ->(**) { TargetIsCanon.new },
15
+ "schema_valid" => ->(schemas:, **) { SchemaValid.new(schemas: schemas) },
16
+ "etag_match" => ->(params:, **) { EtagMatch.new(if_etag: params) },
17
+ "fresh_within" => ->(params:, **) { FreshWithin.new(duration: params) },
18
+ }.freeze
19
+
20
+ # Accepts either "name" or { "name" => params }.
21
+ def self.build(spec, schemas:)
22
+ name, params =
23
+ if spec.is_a?(Hash)
24
+ spec.first
25
+ else
26
+ [spec.to_s, nil]
27
+ end
28
+ ctor = ENTRIES[name.to_s] or raise Textus::UsageError.new(
29
+ "unknown guard predicate: '#{name}' (known: #{ENTRIES.keys.join(", ")})",
30
+ )
31
+ ctor.call(params: params, schemas: schemas)
32
+ end
33
+
34
+ def self.known = ENTRIES.keys
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end