tina4ruby 3.13.167 → 3.13.169

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf9cbd70decc616d093b7afdeee71b3e60be05767c7a1b9f1f886b636b1c7a6a
4
- data.tar.gz: 360d7553e0ab7c70f2b2b76da81bcd0ad5b37b3194f9e0455a0b80757ac0de3f
3
+ metadata.gz: 0d6e19c42c25658a10f4c46db06e6ab07d5acc144278ae8fed9e51f8a8844321
4
+ data.tar.gz: 36f3cccf40308a427df9cefdf607c75f75cd3e84f6fe2af4fa9ec22a71172cef
5
5
  SHA512:
6
- metadata.gz: 9083cf03a64c921103bb384c0148a6225737b6325f6cd9004c4c97f30e8c745c182897976ac2e701e5bbb70d68bf25523042b1fb8c8a5a09ce3205fc21657297
7
- data.tar.gz: 1843fb88895bd6ada61cde4b4417e62a90790844cbf03124a8c895cc3bb2b3da328e9a841120ddbdc9b19c34ad1f05df3c43f54566ce32648af65c6063418f3e
6
+ metadata.gz: a5039b825b26654fe5e208667b6f82bb0f4e86a97d892d500aefb674c0797fe2b9056c6cc29875970197c8108bb372f6b2fbbc0d5e4337b72ee5cb781781ba79
7
+ data.tar.gz: '09e8def1489bfd54ccfc697411caf49bed1c949ad7fb4037066651c47101e1543dd714d3c7b79c6114adb443392baed6780ccb53c4a5a8f399d144112c358267'
data/CHANGELOG.md CHANGED
@@ -6,6 +6,41 @@ number means the same thing everywhere.
6
6
  **The authoritative release notes for every shipped version live in the documentation:**
7
7
  https://tina4.com/ruby/36-releases
8
8
 
9
+ ## 3.13.169 - 2026-12-02
10
+
11
+ ### Breaking: behaviour that changed
12
+
13
+ - **A `src/` file that fails to load stops startup (ADR-0112).** Outside `TINA4_DEBUG`, a route or model file that fails to load (a parse error included) now throws `Tina4\DiscoveryError`. `php index.php` and `tina4php serve` exit 1 before listening and name the file and the cause; under `php -S`, PHP-FPM or Apache every request, `/health` included, answers 500. Before, the file was logged, skipped and the app served without it. Debug boots and re-scans keep skip-and-log. **Upgrade:** fix the file, or set `TINA4_STRICT_DISCOVER=false` to restore the old behaviour (#404).
14
+ - **Database URL query options reach the driver (ADR-0113).** The query string of a database URL is now connection options, read once by `Tina4\DatabaseUrl`, handed to the driver and never dropped or folded into the database name. An option the engine or driver cannot honour is refused at connect instead of being ignored. `?sslmode=require` on PostgreSQL now applies (it used to connect in plain text), `?application_name=` reaches the server, MySQL `?charset=` sets the connection charset, and MSSQL `?encrypt=` / `?trustServerCertificate=` map to ext-sqlsrv (pdo_dblib refuses `encrypt=true`, as FreeTDS cannot require TLS per connection). Firebird accepts `charset` and `driver` only; PostgreSQL refuses endpoint keys (`host`, `port`, `dbname`, `user`, `password` and similar) in the query. **Upgrade:** a URL that carries an option your driver cannot honour now fails at connect; remove it or use a driver that supports it (#403).
15
+ - **One connection per process.** A forked process never uses a connection it inherited. Under `TINA4_SERVE_FORK` (the default where pcntl exists) and the `TINA4_SERVE_WORKERS` pool, a database opened before the fork was the same server session in every process, so one request's rollback could undo another request's write on PostgreSQL and MySQL. Each child now opens its own connection on first use; the parent's stays untouched. SQLite is unaffected (#399).
16
+ - **Docker base image is `php:8.6.0-nts-alpine3.23`.** The image moves from `php:8.4.27-cli-alpine3.23` to the PHP 8.6 non-thread-safe build, with mbstring compiled in (PHP 8.6 images no longer bundle it). The Dockerfile that `tina4php init` writes matches (`php:8.6-nts-alpine3.23`, mbstring built, `TINA4_OVERRIDE_CLIENT=true` so the container starts). Do not use `php:8.6-cli-*`, which is thread-safe from PHP 8.6 (#400).
17
+ - **A path parameter no longer replaces the handler's `request` / `response` argument.** A `Request` / `Response` type hint (or, untyped, `request` / `req` and `response` / `res`) now binds the request and response first, so `/items/{request}` no longer handed the segment to `$request` or raised a `TypeError` for a typed handler. The segment stays in `$request->params` (#398).
18
+ - **`tina4php generate` takes nested names for `route` only.** `generate route reports/sales` writes `src/routes/reports/sales.php`; every other generator refuses a nested name before writing anything, with a message and a non-zero exit. A failed write now rolls the whole generate back and says `Nothing was written.`; it no longer prints `Created` after a failed write (#402).
19
+
20
+ ### Security
21
+
22
+ - The standalone `WebSocket` server reads each connection's upgrade request in its select loop, within `WebSocket::HANDSHAKE_TIMEOUT` (10 seconds) and `WebSocket::MAX_REQUEST_HEAD` (16 KiB), the same limits as tina4-python and tina4-ruby. A request that arrives in pieces is served, other clients are answered meanwhile, a connection that sends no complete request in time is closed, and an oversized head is answered 400 (#407).
23
+ - `TINA4_WS_MAX_CONNECTIONS` (default 10000; a value that is not a positive whole number uses the default) caps open WebSocket connections on both the built-in and the standalone server. Past the cap the upgrade is answered 503 before the handshake, and a closed connection frees its slot. In a `TINA4_SERVE_WORKERS` pool the cap applies per worker (#405).
24
+ - When a handler has already sent its status line and part of its body, an error afterwards is logged and the response ends there: no error page is appended and `http_response_code()` is not called again (#408).
25
+ - The value of a secret-valued database URL option (`password`, `pass`, `pwd`, `passwd`, `sslpassword`, or any name containing `password`, `secret` or `token`, in any letter case) is masked as `key=***` wherever a URL is shown: `DatabaseUrl::redact()`, `toSafeString()`, `print_r` / `var_dump` / `json_encode` of a `DatabaseUrl`, `scrubMessage()`, the PostgreSQL connect error and the dev MCP `env_list`. Other options such as `sslmode=require` stay readable (#409).
26
+
27
+ ### Added
28
+
29
+ - `TINA4_SSO_VERIFY=jwks` verifies the ID token against the provider's JWKS (RS256 and ES256 only; strict `iss`, `aud`, `azp`, `exp`, `nbf` and `nonce`), so providers with no introspection endpoint, such as Microsoft Entra ID, work (ADR-0114). `TINA4_SSO_CLOCK_SKEW` (default 60) and `TINA4_SSO_JWKS_MAX_AGE` (default 3600) tune it; a token with no `kid` is accepted only when exactly one key fits. In both modes UserInfo is merged only when its `sub` matches the ID token's. Needs ext-openssl (#401).
30
+ - `Database::addConnectHook()` registers a hook that runs on every open connection and on each new one; the ext-pgsql and pdo_pgsql adapters replay it after a reconnect, before any statement runs. `TINA4_DB_RECONNECT_RETRY=false` surfaces a dropped connection instead of re-running the statement (#404).
31
+
32
+ ### Fixed
33
+
34
+ - A PostgreSQL connection that drops after a raw-SQL `BEGIN` is no longer retried as a lone statement in autocommit, which committed half the transaction. The retry reads the server's transaction status and refuses it; pdo_pgsql also no longer swallows a drop in `fetch()`'s count probe, and reopens its handle after a `COMMIT` or `ROLLBACK` fails on a dead link. Both adapters log a warning when they reconnect (#404).
35
+ - `Migration::detectDialect()` reads the database type every wrapper forwards, so a cached or wrapped adapter gets its engine's migration DDL (#403).
36
+ - The standalone `WebSocket` server no longer discards bytes that arrive after the upgrade request head; they are kept as the connection's first frames. On Windows, a request that arrived a moment after the connection opened is served instead of being closed unanswered (#407).
37
+ - `.claude/commands/tina4-websocket.md` gave the WebSocket connection default as 1000; it is 10000 (#405).
38
+
39
+ ### Changed
40
+
41
+ - The dev MCP `env_list` masks secret-valued options in a database URL or keyword DSN instead of returning it as written (#409).
42
+ - Tests pin that binary stream chunks reach the wire byte for byte and binary `Api` bodies are returned byte for byte, and that a WebSocket connection is released when the peer closes its side (#398, #406).
43
+
9
44
  ## 3.13.167 - 2026-11-30
10
45
 
11
46
  ### Security
data/lib/tina4/cli.rb CHANGED
@@ -1641,6 +1641,131 @@ module Tina4
1641
1641
  # for AI agents that need to know exactly what a `generate model Order`
1642
1642
  # will produce before it runs.
1643
1643
 
1644
+ # ── Generator name rules + transactional writes ──────────────────────
1645
+ #
1646
+ # A generate run that fails part-way must leave NOTHING behind (a route
1647
+ # half-scaffolded with its spec missing is worse than no route), so every
1648
+ # generator write goes through gen_mkdir / gen_write / gen_copy, which record
1649
+ # what this run created; #rollback_writes undoes it. Identical contract in
1650
+ # all four frameworks (tina4-nodejs#232).
1651
+
1652
+ # Generators whose name may nest (`reports/sales`): the name IS a URL path.
1653
+ NESTED_NAME_GENERATORS = %w[route].freeze
1654
+ # Generators that already turn any name into a flat slug, so a `/` is harmless.
1655
+ SLUGGING_GENERATORS = %w[queue websocket listener].freeze
1656
+
1657
+ # Why +name+ can't be used for `generate <what>`, or nil when it can. Checked
1658
+ # BEFORE anything is written. A model, middleware, service, ... name becomes
1659
+ # a class / method identifier, so a nested name has no valid spelling there.
1660
+ def name_problem(what, name)
1661
+ name = name.to_s
1662
+ return nil if name.empty? || SLUGGING_GENERATORS.include?(what)
1663
+
1664
+ nested = NESTED_NAME_GENERATORS.include?(what)
1665
+ body = nested ? name.sub(%r{\A/}, "") : name
1666
+ if body.match?(/[\\:]/)
1667
+ return "'#{name}' has a backslash or colon; use '/' to nest (route only)"
1668
+ end
1669
+ return nil unless body.include?("/")
1670
+
1671
+ unless nested
1672
+ return "nested names ('#{name}') are not supported for 'generate #{what}': use a flat name. " \
1673
+ "Only 'generate route' takes a nested name (reports/sales -> /api/reports/sales)"
1674
+ end
1675
+ if body.split("/", -1).any? { |seg| seg.empty? || seg == "." || seg == ".." }
1676
+ return "'#{name}' has an empty, '.' or '..' segment"
1677
+ end
1678
+ nil
1679
+ end
1680
+
1681
+ # Exit non-zero, writing nothing, when +name+ is unusable for +what+.
1682
+ def refuse_bad_name!(what, name)
1683
+ problem = name_problem(what, name)
1684
+ return unless problem
1685
+
1686
+ warn " Cannot generate #{what}: #{problem}. Nothing was written."
1687
+ exit 1
1688
+ end
1689
+
1690
+ def written_state
1691
+ @written_state ||= { files: [], dirs: [] }
1692
+ end
1693
+
1694
+ def begin_writes
1695
+ @written_state = { files: [], dirs: [] }
1696
+ end
1697
+
1698
+ # Undo this run's writes: files first, then the folders it created (deepest first).
1699
+ def rollback_writes
1700
+ st = written_state
1701
+ st[:files].reverse_each { |f| File.delete(f) if File.file?(f) rescue nil }
1702
+ st[:dirs].sort_by { |d| -d.length }.each { |d| Dir.rmdir(d) rescue nil }
1703
+ @written_state = { files: [], dirs: [] }
1704
+ end
1705
+
1706
+ # Every missing folder from +dir+ up to the first one that exists, deepest first.
1707
+ def missing_dirs(dir)
1708
+ missing = []
1709
+ d = File.expand_path(dir)
1710
+ until File.exist?(d)
1711
+ missing << d
1712
+ parent = File.dirname(d)
1713
+ break if parent == d
1714
+ d = parent
1715
+ end
1716
+ missing
1717
+ end
1718
+
1719
+ # Refuse a target that cannot be written BEFORE anything is written: a path
1720
+ # that leaves the project, or one whose nearest existing ancestor is a file.
1721
+ def assert_writable!(path)
1722
+ full = File.expand_path(path)
1723
+ cwd = File.expand_path(Dir.pwd)
1724
+ unless full == cwd || full.start_with?(cwd.end_with?("/") ? cwd : "#{cwd}/")
1725
+ raise "refusing to write outside the project: #{path}"
1726
+ end
1727
+ missing = missing_dirs(File.dirname(full))
1728
+ anchor = missing.empty? ? File.dirname(full) : File.dirname(missing.last)
1729
+ if File.exist?(anchor) && !File.directory?(anchor)
1730
+ raise "cannot create #{path}: #{anchor} is a file, not a folder"
1731
+ end
1732
+ end
1733
+
1734
+ def gen_mkdir(dir)
1735
+ missing = missing_dirs(dir)
1736
+ return if missing.empty?
1737
+ FileUtils.mkdir_p(dir)
1738
+ written_state[:dirs].concat(missing)
1739
+ end
1740
+
1741
+ def gen_write(path, content)
1742
+ assert_writable!(path)
1743
+ gen_mkdir(File.dirname(path)) # a nested name's parent folders
1744
+ File.write(path, content)
1745
+ written_state[:files] << File.expand_path(path)
1746
+ end
1747
+
1748
+ def gen_copy(src, dest)
1749
+ assert_writable!(dest)
1750
+ FileUtils.cp(src, dest)
1751
+ written_state[:files] << File.expand_path(dest)
1752
+ end
1753
+
1754
+ # Run the block as one all-or-nothing generate: any failure rolls back what
1755
+ # it wrote and exits non-zero with a clean one-line reason.
1756
+ def transactional_generate(what)
1757
+ begin_writes
1758
+ yield
1759
+ rescue SystemExit => e
1760
+ rollback_writes unless e.success?
1761
+ raise
1762
+ rescue StandardError, ScriptError => e
1763
+ rollback_writes
1764
+ warn " Generate #{what} failed: #{e.message}"
1765
+ warn " Nothing was written."
1766
+ exit 1
1767
+ end
1768
+
1644
1769
  def cmd_generate(argv)
1645
1770
  # ADR-0063: fresh timestamp per command run so the sandbox pre-run + the
1646
1771
  # real generator both agree on the migration filename.
@@ -1685,6 +1810,7 @@ module Tina4
1685
1810
  end
1686
1811
 
1687
1812
  name = no_name_generators.include?(what) ? "" : argv.shift
1813
+ refuse_bad_name!(what, name)
1688
1814
  flags, _positional = parse_flags(argv)
1689
1815
 
1690
1816
  json_mode = flags.delete("json") ? true : false
@@ -1695,7 +1821,7 @@ module Tina4
1695
1821
  # ADR-0063 (3.13.121): form + view joined so their baked `{# tina4:edit ... #}`
1696
1822
  # markers surface in resolution.edit_hints[] under the same v1.1 envelope.
1697
1823
  unless RESOLUTION_TARGETS.include?(what)
1698
- send(gen_spec[:handler], name, flags)
1824
+ transactional_generate(what) { send(gen_spec[:handler], name, flags) }
1699
1825
  return
1700
1826
  end
1701
1827
 
@@ -1717,13 +1843,26 @@ module Tina4
1717
1843
  gen_spec: nil,
1718
1844
  generator_kwargs: {})
1719
1845
  gen_spec ||= GENERATORS[what]
1846
+ transactional_generate(what) do
1847
+ run_resolution_aware_generator_body(
1848
+ what, name, flags, json_mode: json_mode, dry_run: dry_run,
1849
+ gen_spec: gen_spec, generator_kwargs: generator_kwargs
1850
+ )
1851
+ end
1852
+ end
1720
1853
 
1854
+ def run_resolution_aware_generator_body(what, name, flags, json_mode:, dry_run:,
1855
+ gen_spec:, generator_kwargs:)
1721
1856
  resolution = build_generate_resolution(what, name, flags)
1722
1857
  # ADR-0063 additive v1.1 keys: curated next-steps (pure fn of resolution)
1723
1858
  # and edit_hints (scan of generated files for `# tina4:edit <label>`).
1724
1859
  resolution["next"] = build_next_steps(what, resolution)
1725
1860
  resolution["edit_hints"] =
1726
1861
  collect_edit_hints_via_sandbox(gen_spec, name, flags, **generator_kwargs)
1862
+ # The sandbox pre-run showed every file the real run will write: check
1863
+ # each against the REAL tree now, so a target that cannot be written
1864
+ # fails here (dry-run included) instead of half-way through.
1865
+ @sandbox_files.each { |rel| assert_writable!(File.join(Dir.pwd, rel)) }
1727
1866
 
1728
1867
  if json_mode
1729
1868
  actions_taken =
@@ -1796,6 +1935,9 @@ module Tina4
1796
1935
  # `emit_test: false` + `description:` through without a bespoke sandbox.
1797
1936
  def collect_edit_hints_via_sandbox(gen_spec, name, flags, **generator_kwargs)
1798
1937
  hints = []
1938
+ @sandbox_files = []
1939
+ outer_writes = @written_state # the sandbox keeps its own write record
1940
+ begin_writes
1799
1941
  Dir.mktmpdir("tina4_edit_hints") do |sandbox|
1800
1942
  # Block-form Dir.chdir composes cleanly under an outer block-form
1801
1943
  # Dir.chdir (Ruby 3.x warns on nested non-block chdir). No `ensure`
@@ -1817,10 +1959,12 @@ module Tina4
1817
1959
  Dir.glob("**/*", File::FNM_DOTMATCH).sort.each do |rel_path|
1818
1960
  next if [".", ".."].include?(File.basename(rel_path))
1819
1961
  next unless File.file?(rel_path)
1962
+ @sandbox_files << rel_path
1820
1963
  hints.concat(scan_edit_hints_from_files([rel_path]))
1821
1964
  end
1822
1965
  end
1823
1966
  end
1967
+ @written_state = outer_writes
1824
1968
  hints
1825
1969
  end
1826
1970
 
@@ -1893,7 +2037,7 @@ module Tina4
1893
2037
  "migration_path" => nil,
1894
2038
  "transformations" => [],
1895
2039
  "routes" => ["/api/#{route_path}", "/api/#{route_path}/{id}"],
1896
- "test_paths" => ["spec/routes/#{route_path}_spec.rb"]
2040
+ "test_paths" => ["spec/#{route_path.tr("/", "_")}_spec.rb"]
1897
2041
  }
1898
2042
  end
1899
2043
 
@@ -2098,7 +2242,7 @@ module Tina4
2098
2242
 
2099
2243
  # Write model file
2100
2244
  dir = "src/orm"
2101
- FileUtils.mkdir_p(dir)
2245
+ gen_mkdir(dir)
2102
2246
  path = File.join(dir, "#{snake}.rb")
2103
2247
  if File.exist?(path)
2104
2248
  puts " File already exists: #{path}"
@@ -2114,7 +2258,7 @@ module Tina4
2114
2258
  end
2115
2259
  RUBY
2116
2260
 
2117
- File.write(path, content)
2261
+ gen_write(path, content)
2118
2262
  puts " Created #{path}"
2119
2263
 
2120
2264
  # Generate matching migration (unless --no-migration). The model's own
@@ -2154,8 +2298,10 @@ module Tina4
2154
2298
  public_writes = flags["public"] ? true : false
2155
2299
 
2156
2300
  dir = "src/routes"
2157
- FileUtils.mkdir_p(dir)
2301
+ gen_mkdir(dir)
2158
2302
  path = File.join(dir, "#{route_path}.rb")
2303
+ # A nested name (reports/sales) puts the route one folder deeper per segment.
2304
+ up = "../" * route_path.split("/").length
2159
2305
  if File.exist?(path)
2160
2306
  puts " File already exists: #{path}"
2161
2307
  return
@@ -2183,7 +2329,7 @@ module Tina4
2183
2329
  'e.g. enforce ownership; ground: tina4_context("authorize update", "ruby")'
2184
2330
  )
2185
2331
  content = <<~RUBY
2186
- require_relative "../orm/#{model_snake}"
2332
+ require_relative "#{up}orm/#{model_snake}"
2187
2333
 
2188
2334
  # List all #{route_path} with pagination — public read (GET is ungated).
2189
2335
  Tina4::Router.get "/api/#{route_path}" do |request, response|
@@ -2243,7 +2389,7 @@ module Tina4
2243
2389
  b_list = ai_fill(
2244
2390
  "list_#{route_path}",
2245
2391
  "return the #{route_path} collection (add pagination if it grows)",
2246
- "#{m}.all(limit:, offset:) or #{m}.where(sql, params) (require_relative \"../orm/#{to_snake_case(m)}\")",
2392
+ "#{m}.all(limit:, offset:) or #{m}.where(sql, params) (require_relative \"#{up}orm/#{to_snake_case(m)}\")",
2247
2393
  "list_#{route_path}: query and return the records",
2248
2394
  ret: 'response.json({ data: rows.map(&:to_h) })',
2249
2395
  ground: 'tina4_context("list ORM records with pagination", "ruby")'
@@ -2251,7 +2397,7 @@ module Tina4
2251
2397
  b_get = ai_fill(
2252
2398
  "get_#{singular}",
2253
2399
  "fetch one #{singular} by id",
2254
- "#{m}.find(request.params[\"id\"]) (require_relative \"../orm/#{to_snake_case(m)}\")",
2400
+ "#{m}.find(request.params[\"id\"]) (require_relative \"#{up}orm/#{to_snake_case(m)}\")",
2255
2401
  "get_#{singular}: fetch by id or 404",
2256
2402
  given: 'request.params["id"] -> Integer',
2257
2403
  ret: 'response.json(item.to_h) or response.json({ error: "Not found" }, 404)',
@@ -2260,7 +2406,7 @@ module Tina4
2260
2406
  b_create = ai_fill(
2261
2407
  "create_#{singular}",
2262
2408
  "validate the body and persist a new #{singular}",
2263
- "#{m}.create(request.body) (require_relative \"../orm/#{to_snake_case(m)}\")",
2409
+ "#{m}.create(request.body) (require_relative \"#{up}orm/#{to_snake_case(m)}\")",
2264
2410
  "create_#{singular}: persist and return the new record",
2265
2411
  given: "request.body -> Hash of fields",
2266
2412
  ret: "response.json(item.to_h, 201)",
@@ -2312,7 +2458,7 @@ module Tina4
2312
2458
  RUBY
2313
2459
  end
2314
2460
 
2315
- File.write(path, content)
2461
+ gen_write(path, content)
2316
2462
  puts " Created #{path}"
2317
2463
 
2318
2464
  # Co-emit a real spec. A --model route is working code → reuse the
@@ -2376,7 +2522,7 @@ module Tina4
2376
2522
  def generate_crud_admin_route(model, table, is_public)
2377
2523
  model_snake = to_snake_case(model)
2378
2524
  dir = "src/routes"
2379
- FileUtils.mkdir_p(dir)
2525
+ gen_mkdir(dir)
2380
2526
  path = File.join(dir, "#{table}.rb")
2381
2527
  if File.exist?(path)
2382
2528
  puts " File already exists: #{path}"
@@ -2403,7 +2549,7 @@ module Tina4
2403
2549
  end
2404
2550
  RUBY
2405
2551
 
2406
- File.write(path, content)
2552
+ gen_write(path, content)
2407
2553
  puts " Created #{path}"
2408
2554
  end
2409
2555
 
@@ -2414,13 +2560,13 @@ module Tina4
2414
2560
  def copy_crud_templates
2415
2561
  source_dir = File.join(__dir__, "templates", "crud")
2416
2562
  target_dir = File.join("src", "templates", "crud")
2417
- FileUtils.mkdir_p(target_dir)
2563
+ gen_mkdir(target_dir)
2418
2564
  Dir.glob(File.join(source_dir, "*.twig")).sort.each do |src|
2419
2565
  dest = File.join(target_dir, File.basename(src))
2420
2566
  if File.exist?(dest)
2421
2567
  puts " File already exists: #{dest}"
2422
2568
  else
2423
- FileUtils.cp(src, dest)
2569
+ gen_copy(src, dest)
2424
2570
  puts " Created #{dest}"
2425
2571
  end
2426
2572
  end
@@ -2436,7 +2582,7 @@ module Tina4
2436
2582
  now = generate_timestamp
2437
2583
  timestamp = now.strftime("%Y%m%d%H%M%S")
2438
2584
  dir = "migrations"
2439
- FileUtils.mkdir_p(dir)
2585
+ gen_mkdir(dir)
2440
2586
 
2441
2587
  # `description` (when supplied by `cmd_migrate_create`) keeps the raw
2442
2588
  # human prose that appears in the file HEADER — the filename still uses
@@ -2514,7 +2660,7 @@ module Tina4
2514
2660
  #{up_sql}
2515
2661
  SQL
2516
2662
 
2517
- File.write(path, content)
2663
+ gen_write(path, content)
2518
2664
  puts " Created #{path}"
2519
2665
 
2520
2666
  # Also create .down.sql for the migration runner
@@ -2527,7 +2673,7 @@ module Tina4
2527
2673
  #{down_sql}
2528
2674
  SQL
2529
2675
 
2530
- File.write(down_path, down_content)
2676
+ gen_write(down_path, down_content)
2531
2677
  puts " Created #{down_path}"
2532
2678
 
2533
2679
  # Co-emit a real apply-UP/DOWN spec — only for CREATE TABLE migrations,
@@ -2541,7 +2687,7 @@ module Tina4
2541
2687
  def generate_middleware(name, flags = {})
2542
2688
  snake = to_snake_case(name)
2543
2689
  dir = "src/middleware"
2544
- FileUtils.mkdir_p(dir)
2690
+ gen_mkdir(dir)
2545
2691
  path = File.join(dir, "#{snake}.rb")
2546
2692
  if File.exist?(path)
2547
2693
  puts " File already exists: #{path}"
@@ -2575,7 +2721,7 @@ module Tina4
2575
2721
  end
2576
2722
  RUBY
2577
2723
 
2578
- File.write(path, content)
2724
+ gen_write(path, content)
2579
2725
  puts " Created #{path}"
2580
2726
 
2581
2727
  # Co-emit a real dispatch spec (drives the scaffold through the real
@@ -2750,7 +2896,7 @@ module Tina4
2750
2896
  }
2751
2897
 
2752
2898
  dir = "src/templates/forms"
2753
- FileUtils.mkdir_p(dir)
2899
+ gen_mkdir(dir)
2754
2900
  # Form template is for creating/editing ONE record, so the filename is
2755
2901
  # keyed by the singular class snake (order.twig), never by the plural
2756
2902
  # table (which for a reserved-word class Order would be orders.twig
@@ -2812,7 +2958,7 @@ module Tina4
2812
2958
  {% endblock %}
2813
2959
  HTML
2814
2960
 
2815
- File.write(path, content)
2961
+ gen_write(path, content)
2816
2962
  puts " Created #{path}"
2817
2963
  end
2818
2964
 
@@ -2827,7 +2973,7 @@ module Tina4
2827
2973
  cols = fields.map { |f, _| f }
2828
2974
 
2829
2975
  dir = "src/templates/pages"
2830
- FileUtils.mkdir_p(dir)
2976
+ gen_mkdir(dir)
2831
2977
 
2832
2978
  # List view
2833
2979
  list_path = File.join(dir, "#{route_name}.twig")
@@ -2870,7 +3016,7 @@ module Tina4
2870
3016
  {% endblock %}
2871
3017
  HTML
2872
3018
 
2873
- File.write(list_path, list_content)
3019
+ gen_write(list_path, list_content)
2874
3020
  puts " Created #{list_path}"
2875
3021
  end
2876
3022
 
@@ -2904,7 +3050,7 @@ module Tina4
2904
3050
  {% endblock %}
2905
3051
  HTML
2906
3052
 
2907
- File.write(detail_path, detail_content)
3053
+ gen_write(detail_path, detail_content)
2908
3054
  puts " Created #{detail_path}"
2909
3055
  end
2910
3056
  end
@@ -2920,7 +3066,7 @@ module Tina4
2920
3066
 
2921
3067
  # 2. Auth routes
2922
3068
  dir = "src/routes"
2923
- FileUtils.mkdir_p(dir)
3069
+ gen_mkdir(dir)
2924
3070
  auth_path = File.join(dir, "auth.rb")
2925
3071
  unless File.exist?(auth_path)
2926
3072
  content = <<~'RUBY'
@@ -2991,16 +3137,16 @@ module Tina4
2991
3137
  end
2992
3138
  RUBY
2993
3139
 
2994
- File.write(auth_path, content)
3140
+ gen_write(auth_path, content)
2995
3141
  puts " Created #{auth_path}"
2996
3142
  end
2997
3143
 
2998
3144
  # 3. Login template
2999
3145
  forms_dir = "src/templates/forms"
3000
- FileUtils.mkdir_p(forms_dir)
3146
+ gen_mkdir(forms_dir)
3001
3147
  login_path = File.join(forms_dir, "login.twig")
3002
3148
  unless File.exist?(login_path)
3003
- File.write(login_path, <<~HTML)
3149
+ gen_write(login_path, <<~HTML)
3004
3150
  {% extends "base.twig" %}
3005
3151
  {% block title %}Login{% endblock %}
3006
3152
  {% block content %}
@@ -3032,7 +3178,7 @@ module Tina4
3032
3178
  # 4. Register template
3033
3179
  register_path = File.join(forms_dir, "register.twig")
3034
3180
  unless File.exist?(register_path)
3035
- File.write(register_path, <<~HTML)
3181
+ gen_write(register_path, <<~HTML)
3036
3182
  {% extends "base.twig" %}
3037
3183
  {% block title %}Register{% endblock %}
3038
3184
  {% block content %}
@@ -3100,7 +3246,7 @@ module Tina4
3100
3246
  end
3101
3247
 
3102
3248
  dir = "src/services"
3103
- FileUtils.mkdir_p(dir)
3249
+ gen_mkdir(dir)
3104
3250
  path = File.join(dir, "#{snake}.rb")
3105
3251
  if File.exist?(path)
3106
3252
  puts " File already exists: #{path}"
@@ -3136,7 +3282,7 @@ module Tina4
3136
3282
  # appears in Tina4::ServiceRunner.list.
3137
3283
  Tina4.service("#{snake}", #{options_kw}) { |context| #{snake}_task(context) }
3138
3284
  RUBY
3139
- File.write(path, content)
3285
+ gen_write(path, content)
3140
3286
  puts " Created #{path}"
3141
3287
 
3142
3288
  # Co-emit a real ServiceRunner registration/discovery spec.
@@ -3152,7 +3298,7 @@ module Tina4
3152
3298
  slug = "topic" if slug.empty?
3153
3299
 
3154
3300
  dir = "src/services" # consumer runs as a daemon service (see below)
3155
- FileUtils.mkdir_p(dir)
3301
+ gen_mkdir(dir)
3156
3302
  path = File.join(dir, "#{slug}_consumer.rb")
3157
3303
  if File.exist?(path)
3158
3304
  puts " File already exists: #{path}"
@@ -3203,7 +3349,7 @@ module Tina4
3203
3349
  # wiring a ServiceRunner.
3204
3350
  Tina4.service("#{topic}-consumer", daemon: true, topic: "#{topic}", handle: method(:handle_#{slug})) { |context| consume_#{slug}(context) }
3205
3351
  RUBY
3206
- File.write(path, content)
3352
+ gen_write(path, content)
3207
3353
  puts " Created #{path}"
3208
3354
 
3209
3355
  # Co-emit a real file-backed Queue spec (push a real job + daemon wiring).
@@ -3216,7 +3362,7 @@ module Tina4
3216
3362
  def generate_validator(name, flags = {})
3217
3363
  snake = to_snake_case(name)
3218
3364
  dir = "src/validators"
3219
- FileUtils.mkdir_p(dir)
3365
+ gen_mkdir(dir)
3220
3366
  path = File.join(dir, "#{snake}.rb")
3221
3367
  if File.exist?(path)
3222
3368
  puts " File already exists: #{path}"
@@ -3248,7 +3394,7 @@ module Tina4
3248
3394
  validator
3249
3395
  end
3250
3396
  RUBY
3251
- File.write(path, content)
3397
+ gen_write(path, content)
3252
3398
  puts " Created #{path}"
3253
3399
 
3254
3400
  # Co-emit a real valid + invalid spec against the starter rule.
@@ -3263,7 +3409,7 @@ module Tina4
3263
3409
  table = resolve_table(name, flags)
3264
3410
  model_snake = to_snake_case(name)
3265
3411
  dir = "seeds"
3266
- FileUtils.mkdir_p(dir)
3412
+ gen_mkdir(dir)
3267
3413
  path = File.join(dir, "#{table}_seeder.rb")
3268
3414
  if File.exist?(path)
3269
3415
  puts " File already exists: #{path}"
@@ -3304,7 +3450,7 @@ module Tina4
3304
3450
  puts "Seeded #{'#'}{summary.seeded} #{name} row(s), #{'#'}{summary.failed} failed"
3305
3451
  end
3306
3452
  RUBY
3307
- File.write(path, content)
3453
+ gen_write(path, content)
3308
3454
  puts " Created #{path}"
3309
3455
 
3310
3456
  # Co-emit a real seeding spec (runs the seeder against real SQLite).
@@ -3325,7 +3471,7 @@ module Tina4
3325
3471
  handler = "#{base}_ws"
3326
3472
 
3327
3473
  dir = "src/routes" # Tina4.websocket auto-registers on load (src/routes is discovered)
3328
- FileUtils.mkdir_p(dir)
3474
+ gen_mkdir(dir)
3329
3475
  path = File.join(dir, "ws_#{base}.rb")
3330
3476
  if File.exist?(path)
3331
3477
  puts " File already exists: #{path}"
@@ -3361,7 +3507,7 @@ module Tina4
3361
3507
  end
3362
3508
  end
3363
3509
  RUBY
3364
- File.write(path, content)
3510
+ gen_write(path, content)
3365
3511
  puts " Created #{path}"
3366
3512
 
3367
3513
  # Co-emit a real handler spec (Router registration + drives the handler).
@@ -3377,7 +3523,7 @@ module Tina4
3377
3523
  slug = "event" if slug.empty?
3378
3524
 
3379
3525
  dir = "src/listeners"
3380
- FileUtils.mkdir_p(dir)
3526
+ gen_mkdir(dir)
3381
3527
  path = File.join(dir, "#{slug}.rb")
3382
3528
  if File.exist?(path)
3383
3529
  puts " File already exists: #{path}"
@@ -3409,7 +3555,7 @@ module Tina4
3409
3555
  # Wiring: bind the reaction on the real event bus.
3410
3556
  Tina4::Events.on("#{event}") { |data| on_#{slug}(data) }
3411
3557
  RUBY
3412
- File.write(path, content)
3558
+ gen_write(path, content)
3413
3559
  puts " Created #{path}"
3414
3560
 
3415
3561
  # Co-emit a real event-bus spec (emit the real event, assert it ran).
@@ -3436,13 +3582,13 @@ module Tina4
3436
3582
  # share one rule instead of a dozen copy-pasted write blocks.
3437
3583
  def write_test(test_name, content)
3438
3584
  dir = "spec"
3439
- FileUtils.mkdir_p(dir)
3440
- path = File.join(dir, "#{test_name}_spec.rb")
3585
+ gen_mkdir(dir)
3586
+ path = File.join(dir, "#{test_name.to_s.tr("/", "_")}_spec.rb") # a nested route name flattens
3441
3587
  if File.exist?(path)
3442
3588
  puts " File already exists: #{path}"
3443
3589
  return
3444
3590
  end
3445
- File.write(path, content)
3591
+ gen_write(path, content)
3446
3592
  puts " Created #{path}"
3447
3593
  end
3448
3594
 
@@ -4125,11 +4271,18 @@ module Tina4
4125
4271
  # ── shared helpers ────────────────────────────────────────────────────
4126
4272
 
4127
4273
  def load_routes(root_dir, entrypoints: true)
4128
- route_dirs = %w[src/routes routes src/api api src/orm orm]
4129
- route_dirs.each do |dir|
4130
- route_dir = File.join(root_dir, dir)
4131
- next unless Dir.exist?(route_dir)
4132
- Dir.glob(File.join(route_dir, "**/*.rb")).sort.each { |f| load f }
4274
+ # Tina4.initialize! has already discovered these folders for this root,
4275
+ # under the boot discovery rules (ADR-0112: strict outside debug, logged
4276
+ # and marked in debug). Loading them a second time here re-ran every
4277
+ # file, and with no rescue, so one broken route file killed `serve`
4278
+ # even in debug.
4279
+ unless Tina4.root_dir == root_dir
4280
+ route_dirs = %w[src/routes routes src/api api src/orm orm]
4281
+ route_dirs.each do |dir|
4282
+ route_dir = File.join(root_dir, dir)
4283
+ next unless Dir.exist?(route_dir)
4284
+ Dir.glob(File.join(route_dir, "**/*.rb")).sort.each { |f| load f }
4285
+ end
4133
4286
  end
4134
4287
 
4135
4288
  return unless entrypoints
@@ -4229,7 +4382,7 @@ module Tina4
4229
4382
  unless File.exist?(File.join(dir, "Dockerfile"))
4230
4383
  File.write(File.join(dir, "Dockerfile"), <<~DOCKERFILE)
4231
4384
  # === Build Stage ===
4232
- FROM ruby:3.3-alpine AS builder
4385
+ FROM ruby:4.0-alpine3.23 AS builder
4233
4386
 
4234
4387
  # Install build dependencies
4235
4388
  RUN apk add --no-cache \\
@@ -4250,7 +4403,7 @@ module Tina4
4250
4403
  COPY . .
4251
4404
 
4252
4405
  # === Runtime Stage ===
4253
- FROM ruby:3.3-alpine
4406
+ FROM ruby:4.0-alpine3.23
4254
4407
 
4255
4408
  # Runtime packages only
4256
4409
  RUN apk add --no-cache libffi gcompat