textus 0.50.0 → 0.52.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 (130) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +38 -0
  3. data/README.md +41 -43
  4. data/SPEC.md +176 -176
  5. data/docs/architecture/README.md +46 -42
  6. data/docs/reference/conventions.md +31 -26
  7. data/lib/textus/boot.rb +15 -17
  8. data/lib/textus/call.rb +1 -1
  9. data/lib/textus/cli/runner.rb +15 -10
  10. data/lib/textus/cli/verb/get.rb +1 -3
  11. data/lib/textus/cli/verb/hook_run.rb +1 -1
  12. data/lib/textus/cli/verb/put.rb +4 -20
  13. data/lib/textus/cli/verb/serve.rb +19 -0
  14. data/lib/textus/cli.rb +1 -3
  15. data/lib/textus/dispatcher.rb +3 -3
  16. data/lib/textus/doctor/check/generator_drift.rb +4 -3
  17. data/lib/textus/doctor/check/handler_allowlist.rb +1 -1
  18. data/lib/textus/doctor/check/intake_registration.rb +5 -5
  19. data/lib/textus/doctor/check/rule_ambiguity.rb +3 -3
  20. data/lib/textus/doctor/check/sentinels.rb +2 -2
  21. data/lib/textus/doctor/check/templates.rb +13 -11
  22. data/lib/textus/doctor.rb +0 -2
  23. data/lib/textus/domain/freshness/evaluator.rb +150 -14
  24. data/lib/textus/domain/freshness/verdict.rb +28 -6
  25. data/lib/textus/domain/freshness.rb +4 -33
  26. data/lib/textus/domain/jobs/job.rb +58 -0
  27. data/lib/textus/domain/jobs/registry.rb +37 -0
  28. data/lib/textus/domain/policy/base_guards.rb +1 -1
  29. data/lib/textus/domain/policy/predicates/fresh_within.rb +1 -1
  30. data/lib/textus/domain/policy/publish_target.rb +34 -0
  31. data/lib/textus/domain/policy/retention.rb +29 -0
  32. data/lib/textus/domain/policy/source.rb +73 -0
  33. data/lib/textus/domain/retention/sweep.rb +57 -0
  34. data/lib/textus/domain/retention.rb +11 -0
  35. data/lib/textus/errors.rb +4 -4
  36. data/lib/textus/hooks/builtin.rb +5 -5
  37. data/lib/textus/hooks/catalog.rb +7 -7
  38. data/lib/textus/hooks/context.rb +5 -10
  39. data/lib/textus/init/templates/machine_intake.rb +4 -4
  40. data/lib/textus/init.rb +47 -47
  41. data/lib/textus/jobs/handlers.rb +62 -0
  42. data/lib/textus/jobs/scheduler.rb +36 -0
  43. data/lib/textus/jobs/seeder.rb +57 -0
  44. data/lib/textus/key/matching.rb +24 -0
  45. data/lib/textus/layout.rb +8 -0
  46. data/lib/textus/maintenance/drain.rb +42 -0
  47. data/lib/textus/maintenance/retention/apply.rb +52 -0
  48. data/lib/textus/maintenance/serve.rb +30 -0
  49. data/lib/textus/maintenance/worker.rb +74 -0
  50. data/lib/textus/manifest/capabilities.rb +1 -1
  51. data/lib/textus/manifest/data.rb +18 -3
  52. data/lib/textus/manifest/entry/base.rb +28 -9
  53. data/lib/textus/manifest/entry/nested.rb +3 -4
  54. data/lib/textus/manifest/entry/parser.rb +25 -21
  55. data/lib/textus/manifest/entry/produced.rb +56 -0
  56. data/lib/textus/manifest/entry/publish/subtree_mirror.rb +7 -6
  57. data/lib/textus/manifest/entry/publish/to_paths.rb +62 -11
  58. data/lib/textus/manifest/entry/validators/format_matrix.rb +3 -11
  59. data/lib/textus/manifest/entry/validators/publish.rb +3 -1
  60. data/lib/textus/manifest/entry/validators.rb +0 -1
  61. data/lib/textus/manifest/policy.rb +16 -4
  62. data/lib/textus/manifest/resolver.rb +10 -4
  63. data/lib/textus/manifest/rules.rb +37 -36
  64. data/lib/textus/manifest/schema/keys.rb +98 -0
  65. data/lib/textus/manifest/schema/validator.rb +324 -0
  66. data/lib/textus/manifest/schema/vocabulary.rb +24 -0
  67. data/lib/textus/manifest/schema.rb +27 -247
  68. data/lib/textus/manifest.rb +5 -3
  69. data/lib/textus/mcp/server.rb +1 -1
  70. data/lib/textus/ports/audit_log.rb +6 -0
  71. data/lib/textus/ports/build_lock.rb +6 -0
  72. data/lib/textus/ports/clock.rb +4 -3
  73. data/lib/textus/ports/produce_on_write_subscriber.rb +73 -0
  74. data/lib/textus/ports/publisher.rb +11 -7
  75. data/lib/textus/ports/queue.rb +130 -0
  76. data/lib/textus/produce/acquire/handler.rb +29 -0
  77. data/lib/textus/produce/acquire/intake.rb +130 -0
  78. data/lib/textus/produce/acquire/projection.rb +127 -0
  79. data/lib/textus/produce/acquire/serializer/json.rb +31 -0
  80. data/lib/textus/produce/acquire/serializer/text.rb +16 -0
  81. data/lib/textus/produce/acquire/serializer/yaml.rb +31 -0
  82. data/lib/textus/produce/acquire/serializer.rb +17 -0
  83. data/lib/textus/produce/engine.rb +95 -0
  84. data/lib/textus/produce/events.rb +36 -0
  85. data/lib/textus/produce/render.rb +23 -0
  86. data/lib/textus/projection.rb +17 -6
  87. data/lib/textus/read/deps.rb +3 -3
  88. data/lib/textus/read/freshness.rb +61 -31
  89. data/lib/textus/read/get.rb +20 -102
  90. data/lib/textus/read/jobs.rb +31 -0
  91. data/lib/textus/read/rdeps.rb +3 -3
  92. data/lib/textus/read/rule_explain.rb +41 -23
  93. data/lib/textus/read/rule_list.rb +25 -8
  94. data/lib/textus/read/validate_all.rb +14 -0
  95. data/lib/textus/role.rb +2 -1
  96. data/lib/textus/schemas.rb +8 -0
  97. data/lib/textus/store.rb +1 -0
  98. data/lib/textus/version.rb +1 -1
  99. data/lib/textus/write/enqueue.rb +50 -0
  100. data/lib/textus/write/put.rb +1 -1
  101. metadata +35 -30
  102. data/lib/textus/builder/pipeline.rb +0 -88
  103. data/lib/textus/builder/renderer/json.rb +0 -45
  104. data/lib/textus/builder/renderer/markdown.rb +0 -24
  105. data/lib/textus/builder/renderer/text.rb +0 -14
  106. data/lib/textus/builder/renderer/yaml.rb +0 -45
  107. data/lib/textus/builder/renderer.rb +0 -17
  108. data/lib/textus/cli/verb/boot.rb +0 -14
  109. data/lib/textus/cli/verb/build.rb +0 -15
  110. data/lib/textus/doctor/check/fetch_locks.rb +0 -49
  111. data/lib/textus/doctor/check/lifecycle_action_invalid.rb +0 -39
  112. data/lib/textus/domain/freshness/policy.rb +0 -18
  113. data/lib/textus/domain/lifecycle.rb +0 -83
  114. data/lib/textus/domain/outcome.rb +0 -10
  115. data/lib/textus/domain/policy/lifecycle.rb +0 -35
  116. data/lib/textus/domain/staleness/generator_check.rb +0 -109
  117. data/lib/textus/domain/staleness.rb +0 -29
  118. data/lib/textus/maintenance/tend.rb +0 -110
  119. data/lib/textus/manifest/entry/derived.rb +0 -67
  120. data/lib/textus/manifest/entry/intake.rb +0 -31
  121. data/lib/textus/manifest/entry/validators/inject_boot.rb +0 -21
  122. data/lib/textus/mcp/tools.rb +0 -14
  123. data/lib/textus/ports/fetch/detached.rb +0 -52
  124. data/lib/textus/ports/fetch/lock.rb +0 -44
  125. data/lib/textus/write/build.rb +0 -90
  126. data/lib/textus/write/fetch_events.rb +0 -42
  127. data/lib/textus/write/fetch_orchestrator.rb +0 -101
  128. data/lib/textus/write/fetch_worker.rb +0 -127
  129. data/lib/textus/write/intake_fetch.rb +0 -25
  130. data/lib/textus/write/materializer.rb +0 -51
@@ -1,253 +1,33 @@
1
1
  module Textus
2
2
  class Manifest
3
+ # The manifest schema. Its data is split across Schema::Vocabulary (the
4
+ # coordination vocabulary) and Schema::Keys (key whitelists + FIELD_REGISTRY)
5
+ # as of ADR 0109; the validation walk lives in Schema::Validator (ADR 0107).
6
+ # The constants are re-exported here so callers keep saying `Schema::LANES`.
3
7
  module Schema
4
- ROOT_KEYS = %w[version roles zones entries rules audit].freeze
5
- ROLE_KEYS = %w[name can].freeze
6
- ZONE_KEYS = %w[name kind owner desc].freeze
7
- # The closed coordination vocabulary (ADR 0028; completed at five in ADR
8
- # 0033; unified here in ADR 0034). Each lane pairs a zone-kind with the
9
- # single capability that authorizes originating bytes in it — a total
10
- # bijection. This table is the ONE source of truth; the three legacy
11
- # constants below are derived from it so a zone-kind and its required
12
- # capability cannot drift. Key order is canon-first so the unknown-kind
13
- # error message reads canon, workspace, quarantine, queue, derived.
14
- LANES = {
15
- "canon" => "author",
16
- "workspace" => "keep",
17
- "quarantine" => "fetch",
18
- "queue" => "propose",
19
- "derived" => "build",
20
- }.freeze
21
-
22
- ZONE_KINDS = LANES.keys.freeze
23
- CAPABILITIES = LANES.values.freeze
24
- KIND_REQUIRES_VERB = LANES
25
- ENTRY_KEYS = %w[
26
- key path zone kind schema owner nested format
27
- compute template publish
28
- intake events inject_boot provenance ignore tracked
29
- ].freeze
30
- # ADR 0052: the typed publish block — `publish: { to: [...] }` (file
31
- # fan-out) xor `publish: { tree: "dir" }` (subtree mirror).
32
- PUBLISH_KEYS = %w[to tree].freeze
33
- COMPUTE_KEYS = %w[kind select pluck sort_by limit transform command sources].freeze
34
- INTAKE_KEYS = %w[handler config].freeze
35
- RULE_KEYS = %w[match intake_handler_allowlist guard lifecycle].freeze
36
- LIFECYCLE_KEYS = %w[ttl on_expire budget_ms].freeze
37
- AUDIT_KEYS = %w[max_size keep].freeze
38
-
39
- # Syntactic shape of an `owner:` subject token (the `patrick` in
40
- # `human:patrick`) — the subject half of the owner-validation rule below.
41
- # Role supplies the archetype set (Role::NAMES); this pattern is the
42
- # owner-specific part, so it lives with the rule that composes them
43
- # (ADR 0045 D1). Acting-role *names* are gated by Role::NAMES, not a regex.
44
- OWNER_SUBJECT_PATTERN = /\A[a-z][a-z0-9_-]*\z/
45
-
46
- def self.validate!(raw)
47
- raise BadManifest.new("manifest must be a hash") unless raw.is_a?(Hash)
48
-
49
- walk(raw, ROOT_KEYS, "$")
50
- validate_roles!(raw["roles"])
51
- validate_zones!(raw["zones"])
52
- validate_entries!(raw["entries"])
53
- validate_owners!(raw["zones"], raw["entries"])
54
- validate_rules!(raw["rules"])
55
- walk(raw["audit"], AUDIT_KEYS, "$.audit") if raw["audit"].is_a?(Hash)
56
- validate_single_queue!(raw)
57
- validate_zone_kind_consistency!(raw)
58
- end
59
-
60
- def self.validate_zones!(zones)
61
- Array(zones).each_with_index do |z, i|
62
- walk(z, ZONE_KEYS, "$.zones[#{i}]")
63
- if z["kind"].nil?
64
- raise BadManifest.new("zone '#{z["name"]}' at '$.zones[#{i}]' must declare a kind (one of: #{ZONE_KINDS.join(", ")})")
65
- end
66
- next if ZONE_KINDS.include?(z["kind"])
67
-
68
- raise BadManifest.new(
69
- "unknown zone kind '#{z["kind"]}' at '$.zones[#{i}]' (known: #{ZONE_KINDS.join(", ")})",
70
- )
71
- end
72
- end
73
-
74
- def self.validate_entries!(entries)
75
- Array(entries).each_with_index do |e, i|
76
- path = "$.entries[#{i}]"
77
- reject_retired_publish_keys!(e, path)
78
- walk(e, ENTRY_KEYS, path)
79
- validate_publish_block!(e, path)
80
- walk(e["compute"], COMPUTE_KEYS, "#{path}.compute") if e["compute"].is_a?(Hash)
81
- walk(e["intake"], INTAKE_KEYS, "#{path}.intake") if e["intake"].is_a?(Hash)
82
- end
83
- end
84
-
85
- # Retired keys are no longer allowed, so `walk` would reject them as merely
86
- # "unknown"; intercept first with the migration path so a pre-0.43 manifest
87
- # gets a useful error. `publish_each` was removed (ADR 0051); `publish_to`/
88
- # `publish_tree` were folded into the `publish:` block (ADR 0052);
89
- # `index_filename` was removed (ADR 0053).
90
- def self.reject_retired_publish_keys!(entry, path)
91
- return unless entry.is_a?(Hash)
92
-
93
- if entry.key?("publish_each")
94
- raise BadManifest.new(
95
- "publish_each was removed in 0.42.0 (ADR 0051) at '#{path}' — " \
96
- "mirror the subtree with `publish: { tree: \"...\" }`.",
97
- )
98
- end
99
-
100
- if entry.key?("publish_to")
101
- raise BadManifest.new(
102
- "publish_to was replaced by the publish: block in 0.43.0 (ADR 0052) at '#{path}' — " \
103
- "use `publish: { to: [...] }`.",
104
- )
105
- end
106
-
107
- if entry.key?("publish_tree")
108
- raise BadManifest.new(
109
- "publish_tree was replaced by the publish: block in 0.43.0 (ADR 0052) at '#{path}' — " \
110
- "use `publish: { tree: \"...\" }`.",
111
- )
112
- end
113
-
114
- return unless entry.key?("index_filename")
115
-
116
- raise BadManifest.new(
117
- "index_filename was removed in 0.43.0 (ADR 0053) at '#{path}' — a nested entry now enumerates " \
118
- "each file as a key; to mirror a directory of files to a consumer path use `publish: { tree: \"...\" }`.",
119
- )
120
- end
121
-
122
- # Shape of the ADR 0052 publish block: a Hash whose only keys are to/tree.
123
- # Exclusivity (both set) and per-mode rules stay in Publish.resolve (ADR 0049).
124
- def self.validate_publish_block!(entry, path)
125
- return unless entry.is_a?(Hash) && entry.key?("publish")
126
-
127
- block = entry["publish"]
128
- raise BadManifest.new("publish: must be a mapping with `to:` or `tree:` at '#{path}.publish'") unless block.is_a?(Hash)
129
-
130
- walk(block, PUBLISH_KEYS, "#{path}.publish")
131
- end
132
-
133
- def self.validate_rules!(rules)
134
- Array(rules).each_with_index do |r, i|
135
- path = "$.rules[#{i}]"
136
- walk(r, RULE_KEYS, path)
137
- walk(r["lifecycle"], LIFECYCLE_KEYS, "#{path}.lifecycle") if r["lifecycle"].is_a?(Hash)
138
- end
139
- end
140
-
141
- def self.validate_roles!(roles)
142
- return if roles.nil?
143
- raise BadManifest.new("roles: must be a list") unless roles.is_a?(Array)
144
-
145
- roles.each_with_index do |r, i|
146
- path = "$.roles[#{i}]"
147
- walk(r, ROLE_KEYS, path)
148
- name = r["name"] or raise BadManifest.new("role at '#{path}' missing name")
149
- unless Textus::Role::NAMES.include?(name)
150
- raise BadManifest.new(
151
- "unknown role name '#{name}' at '#{path}' " \
152
- "(allowed: #{Textus::Role::NAMES.join(", ")})",
153
- )
154
- end
155
- Array(r["can"]).each do |verb|
156
- next if CAPABILITIES.include?(verb)
157
-
158
- raise BadManifest.new(
159
- "unknown capability '#{verb}' for role '#{name}' at '#{path}' " \
160
- "(known: #{CAPABILITIES.join(", ")})",
161
- )
162
- end
163
- end
164
-
165
- author_holders = roles.count { |r| Array(r["can"]).include?("author") }
166
- return if author_holders <= 1
167
-
168
- raise BadManifest.new(
169
- "manifest declares #{author_holders} roles with the author capability; at most one is allowed",
170
- )
171
- end
172
-
173
- # Owners are validated against the SAME closed archetype set as role names
174
- # (ADR 0045 D1) so attribution can't bypass the closed-name guarantee.
175
- # Applies to both zone owners and entry owners; owner is optional, so a
176
- # nil owner is not an error.
177
- def self.validate_owners!(zones, entries)
178
- Array(zones).each_with_index do |z, i|
179
- check_owner!(z["owner"], "$.zones[#{i}]")
180
- end
181
- Array(entries).each_with_index do |e, i|
182
- check_owner!(e["owner"], "$.entries[#{i}]")
183
- end
184
- end
185
-
186
- def self.check_owner!(owner, path)
187
- return if owner.nil?
188
- return if valid_owner?(owner)
189
-
190
- raise BadManifest.new(
191
- "invalid owner '#{owner}' at '#{path}' " \
192
- "(expected <archetype> or <archetype>:<subject>, " \
193
- "archetype one of: #{Textus::Role::NAMES.join(", ")})",
194
- )
195
- end
196
-
197
- # The owner-validation rule: an `owner:` token is either a bare archetype
198
- # (`agent`) or `<archetype>:<subject>` (`human:patrick`). The archetype is
199
- # gated against the closed Role::NAMES set (so attribution can't smuggle in
200
- # a name the role side rejects, ADR 0045 D1); the subject is the free-form
201
- # principal, validated by OWNER_SUBJECT_PATTERN. Split on the FIRST ':'
202
- # only — a subject may not itself contain ':' (the pattern excludes it), so
203
- # `human:a:b` is rejected.
204
- def self.valid_owner?(token)
205
- return false unless token.is_a?(String) && !token.empty?
206
-
207
- archetype, subject = token.split(":", 2)
208
- return false unless Textus::Role::NAMES.include?(archetype)
209
- return true if subject.nil?
210
-
211
- OWNER_SUBJECT_PATTERN.match?(subject)
212
- end
213
-
214
- def self.walk(hash, allowed, path)
215
- return unless hash.is_a?(Hash)
216
-
217
- hash.each_key do |k|
218
- next if allowed.include?(k)
219
-
220
- raise BadManifest.new("unknown key '#{k}' at '#{path}'")
221
- end
222
- end
223
-
224
- def self.validate_single_queue!(raw)
225
- queues = Array(raw["zones"]).select { |z| z["kind"] == "queue" }.map { |z| z["name"] }
226
- return if queues.size <= 1
227
-
228
- raise BadManifest.new(
229
- "at most one zone may declare kind: queue (found: #{queues.join(", ")})",
230
- )
231
- end
232
-
233
- # Write authority is derived from capabilities (ADR 0030): a zone of a
234
- # given kind can only be written by a role that holds the kind's required
235
- # verb. Reject a manifest declaring a zone whose required verb is held by
236
- # no role. Capabilities.resolve returns the defaults when `roles:` is nil,
237
- # so the capability union is all four verbs and every kind is satisfied.
238
- def self.validate_zone_kind_consistency!(raw)
239
- held = Capabilities.resolve(raw["roles"]).values.flatten.uniq
240
-
241
- Array(raw["zones"]).each_with_index do |z, i|
242
- verb = KIND_REQUIRES_VERB[z["kind"]]
243
- next if verb.nil? || held.include?(verb)
244
-
245
- raise BadManifest.new(
246
- "zone '#{z["name"]}' (#{z["kind"]}) at '$.zones[#{i}]' " \
247
- "needs a role with capability '#{verb}'; none declared",
248
- )
249
- end
250
- end
8
+ # Re-export the vocabulary.
9
+ LANES = Vocabulary::LANES
10
+ ZONE_KINDS = Vocabulary::ZONE_KINDS
11
+ CAPABILITIES = Vocabulary::CAPABILITIES
12
+ KIND_REQUIRES_VERB = Vocabulary::KIND_REQUIRES_VERB
13
+ # Re-export the keys + registry.
14
+ ROOT_KEYS = Keys::ROOT_KEYS
15
+ ROLE_KEYS = Keys::ROLE_KEYS
16
+ ZONE_KEYS = Keys::ZONE_KEYS
17
+ ENTRY_KEYS = Keys::ENTRY_KEYS
18
+ PUBLISH_KEYS = Keys::PUBLISH_KEYS
19
+ SOURCE_KEYS = Keys::SOURCE_KEYS
20
+ RETENTION_KEYS = Keys::RETENTION_KEYS
21
+ AUDIT_KEYS = Keys::AUDIT_KEYS
22
+ FIELD_REGISTRY = Keys::FIELD_REGISTRY
23
+ RULE_KEYS = Keys::RULE_KEYS
24
+ OWNER_SUBJECT_PATTERN = Keys::OWNER_SUBJECT_PATTERN
25
+
26
+ # Public entry points — the validation walk lives in Schema::Validator
27
+ # (ADR 0107). Kept here so callers keep speaking to `Schema`.
28
+ def self.validate!(raw) = Validator.validate!(raw)
29
+
30
+ def self.validate_source_and_retention!(manifest) = Validator.validate_source_and_retention!(manifest)
251
31
  end
252
32
  end
253
33
  end
@@ -6,9 +6,9 @@ module Textus
6
6
  #
7
7
  # * data — frozen value: raw, root, zones, entries, audit_config, role_caps
8
8
  # * resolver — resolves keys → entry + path
9
- # * policy — zone/role authority (zone_writers, declared_kind/derived_zone?/
9
+ # * policy — zone/role authority (zone_writers, declared_kind, derived_entry?,
10
10
  # queue_zone?, permission_for, …)
11
- # * rules — match-block rule engine (fetch, handler allowlist, promotion, …)
11
+ # * rules — match-block rule engine (lifecycle, handler allowlist, materialize, …)
12
12
  #
13
13
  # Use `manifest.data.entries`, `manifest.policy.declared_kind(z)`, etc.
14
14
  Manifest = Data.define(:data, :resolver, :policy, :rules)
@@ -44,12 +44,14 @@ module Textus # rubocop:disable Style/OneClassPerFile
44
44
 
45
45
  def build(raw, root)
46
46
  data = Manifest::Data.parse(raw, root: root)
47
- new(
47
+ manifest = new(
48
48
  data: data,
49
49
  resolver: Manifest::Resolver.new(data),
50
50
  policy: data.policy,
51
51
  rules: Manifest::Rules.parse(raw["rules"] || []),
52
52
  )
53
+ Manifest::Schema.validate_source_and_retention!(manifest) # ADR 0093
54
+ manifest
53
55
  end
54
56
 
55
57
  def check_version!(raw, source)
@@ -94,7 +94,7 @@ module Textus
94
94
 
95
95
  # ADR 0083: the contract-drift guard gates mutating verbs — every MCP
96
96
  # verb that is NOT a pure read (Write:: + the destructive Maintenance::
97
- # verbs tend/zone_mv/key_*_prefix). Reads and boot bypass it (a stale
97
+ # verbs drain/zone_mv/key_*_prefix). Reads and boot bypass it (a stale
98
98
  # read returns on-disk truth; boot re-orients). Keying on read_verbs
99
99
  # (not write_verbs) keeps the destructive Maintenance:: verbs gated.
100
100
  @session.check_etag!(contract_etag) unless Catalog.read_verbs.include?(name)
@@ -4,6 +4,12 @@ require "time"
4
4
 
5
5
  module Textus
6
6
  module Ports
7
+ # Append-only audit log adapter: writes and rotates the on-disk audit JSONL
8
+ # under the store root. An instantiable class — it holds collaborators (the
9
+ # root path + size/keep config), so each store binds its own instance. It
10
+ # already satisfied ADR 0109's single-shape rule (every port is an
11
+ # instantiable class) before that ADR's Clock/Publisher conversions, so it
12
+ # was unchanged by them.
7
13
  class AuditLog
8
14
  DEFAULT_MAX_SIZE = 10_485_760
9
15
  DEFAULT_KEEP = 5
@@ -4,6 +4,12 @@ require "time"
4
4
 
5
5
  module Textus
6
6
  module Ports
7
+ # Cross-process build lock: a pid/host-stamped lockfile under the store root
8
+ # that serializes converge's produce/sweep. An instantiable class — it holds
9
+ # the root and lock state; `self.with(root:)` is a convenience that constructs
10
+ # one and runs the block under the held lock. It already satisfied ADR 0109's
11
+ # single-shape rule (every port is an instantiable class) before that ADR's
12
+ # Clock/Publisher conversions, so it was unchanged by them.
7
13
  class BuildLock
8
14
  MAX_HOLDER_BYTES = 512
9
15
 
@@ -1,8 +1,9 @@
1
1
  module Textus
2
2
  module Ports
3
- module Clock
4
- module_function
5
-
3
+ # The wall clock. An instantiable class (ADR 0109) — uniform with the other
4
+ # ports; `now` reads the system time. Callers that need a fixed time still
5
+ # pass it as data via `Call#now`.
6
+ class Clock
6
7
  def now = Time.now
7
8
  end
8
9
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Textus
4
+ module Ports
5
+ # ADR 0093 / job-queue model: on a canon write, enqueue a `materialize` job
6
+ # for each derived entry that depends on the written key (rdeps ∩ producible).
7
+ # Async-only — the write returns immediately; a worker (drain/serve) converges
8
+ # the jobs. There is no inline `sync` path and no in-process thread: freshness
9
+ # is re-homed to drain (at the commit/CI gate) and the daemon. A write INTO a
10
+ # derived entry does not fan out (recursion guard). Produce self-elevates, so
11
+ # the job is stamped automation. Attached at Store boot, alongside
12
+ # AuditSubscriber.
13
+ class ProduceOnWriteSubscriber
14
+ def initialize(container)
15
+ @container = container
16
+ end
17
+
18
+ def attach(bus)
19
+ bus.on(:entry_written, :produce_on_write) do |key:, **|
20
+ on_write(key: key)
21
+ end
22
+ # Closes the ADR 0087 gap: a delete/rename of a source must re-materialize
23
+ # its orphaned dependents too, not just a write. These fire distinct
24
+ # events (:entry_deleted / :entry_renamed), so subscribe to each.
25
+ bus.on(:entry_deleted, :produce_on_delete) do |key:, **|
26
+ on_write(key: key)
27
+ end
28
+ bus.on(:entry_renamed, :produce_on_rename) do |from_key:, to_key:, **|
29
+ on_write(key: from_key)
30
+ on_write(key: to_key)
31
+ end
32
+ self
33
+ end
34
+
35
+ def on_write(key:)
36
+ return if derived_write?(key) # recursion guard: produce output is not a source change
37
+
38
+ affected = Textus::Read::Rdeps.new(container: @container).call(key)["rdeps"]
39
+ producible = affected.select { |k| producible?(k) }
40
+ return if producible.empty?
41
+
42
+ queue = Textus::Ports::Queue.new(root: @container.root)
43
+ producible.each do |k|
44
+ queue.enqueue(
45
+ Textus::Domain::Jobs::Job.new(
46
+ type: "materialize", args: { "key" => k }, enqueued_by: Textus::Role::AUTOMATION,
47
+ ),
48
+ )
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def derived_write?(key)
55
+ @container.manifest.resolver.resolve(key).entry.derived?
56
+ rescue Textus::Error
57
+ false
58
+ end
59
+
60
+ # The producible scope mirrors Produce::Engine#produce_one: derived
61
+ # entries render+publish, and nested publish_tree entries mirror their
62
+ # source subtree (ADR 0047). Including the latter restores reactive
63
+ # re-mirroring on a write into a tree's source — dropped when the scope
64
+ # narrowed to `derived?` only.
65
+ def producible?(key)
66
+ entry = @container.manifest.resolver.resolve(key).entry
67
+ entry.derived? || !entry.publish_tree.nil?
68
+ rescue Textus::Error
69
+ false
70
+ end
71
+ end
72
+ end
73
+ end
@@ -10,18 +10,20 @@ module Textus
10
10
  # under `<store_root>/.run/sentinels/` (runtime, git-ignored — ADR 0070) and
11
11
  # mirror the target's repo-relative layout so consumer directories aren't
12
12
  # polluted with `.textus-managed.json` siblings.
13
- module Publisher
14
- def self.publish(source:, target:, store_root:)
13
+ #
14
+ # An instantiable class (ADR 0109).
15
+ class Publisher
16
+ def publish(source:, target:, store_root:, provenance_source: source)
15
17
  FileUtils.mkdir_p(File.dirname(target))
16
18
  guard_clobber(source, target, store_root)
17
19
  File.delete(target) if File.symlink?(target)
18
20
  FileUtils.cp(source, target)
19
- Textus::Ports::SentinelStore.new.write!(target: target, source: source, store_root: store_root)
21
+ Textus::Ports::SentinelStore.new.write!(target: target, source: provenance_source, store_root: store_root)
20
22
  end
21
23
 
22
24
  # Removes a previously-published file and its sentinel. No-op unless the
23
25
  # target is textus-managed — never deletes an unmanaged file.
24
- def self.unpublish(target:, store_root:)
26
+ def unpublish(target:, store_root:)
25
27
  return unless managed?(target, store_root)
26
28
 
27
29
  FileUtils.rm_f(target)
@@ -29,6 +31,8 @@ module Textus
29
31
  FileUtils.rm_f(sentinel)
30
32
  end
31
33
 
34
+ private
35
+
32
36
  # Refuse to clobber an unmanaged target — EXCEPT adopt one whose bytes
33
37
  # already equal the source (ADR 0050: a migration copies files into the
34
38
  # store and publishes them back to where they already live, so the target
@@ -36,7 +40,7 @@ module Textus
36
40
  # here; the normal publish path below does, and the cp is a content no-op.
37
41
  # An unmanaged target whose content DIFFERS, or any unmanaged symlink, is
38
42
  # still refused — that is the guard's real job.
39
- def self.guard_clobber(source, target, store_root)
43
+ def guard_clobber(source, target, store_root)
40
44
  return unless File.exist?(target) || File.symlink?(target)
41
45
  return if managed?(target, store_root)
42
46
  return if adoptable?(source, target)
@@ -44,11 +48,11 @@ module Textus
44
48
  raise PublishError.new("refusing to clobber unmanaged file at #{target}", target: target)
45
49
  end
46
50
 
47
- def self.adoptable?(source, target)
51
+ def adoptable?(source, target)
48
52
  !File.symlink?(target) && File.file?(target) && FileUtils.identical?(source, target)
49
53
  end
50
54
 
51
- def self.managed?(target, store_root)
55
+ def managed?(target, store_root)
52
56
  File.exist?(Textus::Ports::SentinelStore.new.sentinel_path(target, store_root))
53
57
  end
54
58
  end
@@ -0,0 +1,130 @@
1
+ require "fileutils"
2
+ require "json"
3
+ require "time"
4
+
5
+ module Textus
6
+ module Ports
7
+ # File-backed durable job queue under `<root>/.run/queue/`. Each job state
8
+ # is a directory; a job is one `<id>.json` file. Claiming is an atomic
9
+ # `rename(2)` from ready/ to leased/ — the rename winner owns the job, so a
10
+ # worker pool needs no central lock. Dedup falls out of the id-as-filename:
11
+ # enqueueing an id that already exists is a no-op. ADR 0038 (runtime subtree),
12
+ # ADR 0108 (instantiable port).
13
+ class Queue
14
+ STATES = %i[ready leased done failed].freeze
15
+
16
+ def initialize(root:)
17
+ @root = root
18
+ STATES.each { |s| FileUtils.mkdir_p(Textus::Layout.queue_state(root, s)) }
19
+ end
20
+
21
+ def enqueue(job)
22
+ dest = path(:ready, job.id)
23
+ return if File.exist?(dest) # dedup: identical work already queued
24
+
25
+ write_atomic(dest, job.to_h)
26
+ end
27
+
28
+ def ready_ids
29
+ Dir.children(Textus::Layout.queue_state(@root, :ready)).map { |f| File.basename(f, ".json") }
30
+ end
31
+
32
+ # A claimed job plus the path it lives at, so ack/fail act on this copy.
33
+ Leased = Struct.new(:job, :leased_path, keyword_init: true)
34
+
35
+ def lease(worker_id:, lease_ttl:)
36
+ ready_dir = Textus::Layout.queue_state(@root, :ready)
37
+ Dir.children(ready_dir).each do |name|
38
+ src = File.join(ready_dir, name)
39
+ dst = File.join(Textus::Layout.queue_state(@root, :leased), name)
40
+ begin
41
+ File.rename(src, dst) # atomic claim; loser's rename raises ENOENT
42
+ rescue Errno::ENOENT
43
+ next # another worker won this one
44
+ end
45
+ job = Textus::Domain::Jobs::Job.from_h(JSON.parse(File.read(dst)))
46
+ stamp_lease(dst, worker_id: worker_id, expires_at: Time.now.utc + lease_ttl)
47
+ return Leased.new(job: job, leased_path: dst)
48
+ end
49
+ nil
50
+ end
51
+
52
+ def ack(leased)
53
+ dest = File.join(Textus::Layout.queue_state(@root, :done), File.basename(leased.leased_path))
54
+ File.rename(leased.leased_path, dest)
55
+ end
56
+
57
+ # Increment attempts and either requeue (transient) or dead-letter (attempts
58
+ # exhausted). Returns :requeued or :dead_lettered so the worker can count
59
+ # terminal failures distinctly from retries.
60
+ def fail(leased, error:)
61
+ job = leased.job
62
+ job.attempts += 1
63
+ job.last_error = error
64
+ dead = job.attempts >= job.max_attempts
65
+ write_atomic(path(dead ? :failed : :ready, job.id), job.to_h)
66
+ File.delete(leased.leased_path)
67
+ dead ? :dead_lettered : :requeued
68
+ end
69
+
70
+ # Return expired leases to ready/ (the holding worker crashed). Returns the
71
+ # count reclaimed. At-least-once delivery: a job whose handler actually
72
+ # finished but whose ack was lost will re-run — handlers must be idempotent.
73
+ def reclaim(now:)
74
+ leased_dir = Textus::Layout.queue_state(@root, :leased)
75
+ count = 0
76
+ Dir.children(leased_dir).each do |name|
77
+ src = File.join(leased_dir, name)
78
+ data = JSON.parse(File.read(src))
79
+ expires = data.dig("lease", "expires_at")
80
+ next if expires && Time.parse(expires) > now
81
+
82
+ dst = File.join(Textus::Layout.queue_state(@root, :ready), name)
83
+ data.delete("lease")
84
+ File.write(src, JSON.pretty_generate(data))
85
+ File.rename(src, dst)
86
+ count += 1
87
+ rescue Errno::ENOENT
88
+ next # raced with another reclaimer / the worker's ack
89
+ end
90
+ count
91
+ end
92
+
93
+ def list(state)
94
+ Dir.children(Textus::Layout.queue_state(@root, state.to_sym)).map { |f| File.basename(f, ".json") }
95
+ end
96
+
97
+ def retry_failed(job_id)
98
+ src = path(:failed, job_id)
99
+ data = JSON.parse(File.read(src))
100
+ data["attempts"] = 0
101
+ data["last_error"] = nil
102
+ write_atomic(path(:ready, job_id), data)
103
+ File.delete(src)
104
+ end
105
+
106
+ def purge(state)
107
+ dir = Textus::Layout.queue_state(@root, state.to_sym)
108
+ Dir.children(dir).each { |f| File.delete(File.join(dir, f)) }
109
+ end
110
+
111
+ private
112
+
113
+ def stamp_lease(leased_path, worker_id:, expires_at:)
114
+ data = JSON.parse(File.read(leased_path))
115
+ data["lease"] = { "worker_id" => worker_id, "expires_at" => expires_at.iso8601 }
116
+ File.write(leased_path, JSON.pretty_generate(data))
117
+ end
118
+
119
+ def path(state, job_id)
120
+ File.join(Textus::Layout.queue_state(@root, state), "#{job_id}.json")
121
+ end
122
+
123
+ def write_atomic(dest, hash)
124
+ tmp = "#{dest}.#{Process.pid}.tmp"
125
+ File.write(tmp, JSON.pretty_generate(hash))
126
+ File.rename(tmp, dest) # atomic on same filesystem
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,29 @@
1
+ require "timeout"
2
+
3
+ module Textus
4
+ module Produce
5
+ module Acquire
6
+ # Invokes a :resolve_handler hook handler by name under a timeout — the single
7
+ # home for "call the intake handler under a deadline" (ADR 0048 D1). Shared by
8
+ # Produce::Acquire::Intake (the internal ingest mechanism — no public verb since ADR 0079)
9
+ # as driven by the converge sweep (drain/serve) and `textus hook run` (ADR 0089 made
10
+ # ingest system-pushed; there is no read or put trigger).
11
+ # Always passes a Container as `caps:` so the hook contract (ADR 0027) is
12
+ # uniform across every entry point. Maps Timeout::Error to a UsageError;
13
+ # leaves any other error to the caller (call sites differ in how they wrap).
14
+ module Handler
15
+ FETCH_TIMEOUT_SECONDS = 30
16
+
17
+ module_function
18
+
19
+ def invoke(caps:, handler:, config:, args:, label:, timeout: FETCH_TIMEOUT_SECONDS)
20
+ Timeout.timeout(timeout) do
21
+ caps.rpc.invoke(:resolve_handler, handler, caps: caps, config: config, args: args)
22
+ end
23
+ rescue Timeout::Error
24
+ raise Textus::UsageError.new("#{label} '#{handler}' exceeded #{timeout}s timeout")
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end