@joshuafolkken/kit 1.1373.0 → 1.1383.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 (92) hide show
  1. package/.claude/skills/dependency-update/SKILL.md +6 -3
  2. package/.claude/skills/workflow-commands/backlogrun-progress.md +2 -3
  3. package/.claude/skills/workflow-commands/backlogrun-steps.md +2 -4
  4. package/.claude/skills/workflow-commands/retrospective.md +1 -2
  5. package/dist/hooks/batch-guard.js +6 -6
  6. package/dist/hooks/{chunk-UTHMB4O2.js → chunk-2X2ART5G.js} +4 -33
  7. package/dist/hooks/{chunk-NZN2UFI6.js → chunk-4MGHBTOM.js} +162 -233
  8. package/dist/hooks/{chunk-YLBBSSTF.js → chunk-6AC4C2IO.js} +91 -67
  9. package/dist/hooks/{chunk-AKGCP4LX.js → chunk-6MAW7BCM.js} +12 -12
  10. package/dist/hooks/{chunk-JCRDBQXB.js → chunk-7VSZ2N4B.js} +3 -3
  11. package/dist/hooks/{chunk-GG72ANKI.js → chunk-KC6LUZZ2.js} +1 -1
  12. package/dist/hooks/{chunk-BZMPVFDY.js → chunk-KCF2YE7O.js} +5 -5
  13. package/dist/hooks/{chunk-7WKYHEGU.js → chunk-LC56R53A.js} +3 -3
  14. package/dist/hooks/{chunk-V4TKZHER.js → chunk-LOJTBU5V.js} +3 -3
  15. package/dist/hooks/{chunk-JGW24KKM.js → chunk-RYP3TSUJ.js} +3 -3
  16. package/dist/hooks/{chunk-SLSFIWTX.js → chunk-TT742SIO.js} +8 -5
  17. package/dist/hooks/{chunk-Z5TMJIPW.js → chunk-Y5R47QHU.js} +1 -1
  18. package/dist/hooks/{chunk-7UBFU3CL.js → chunk-YKUITFT7.js} +2 -2
  19. package/dist/hooks/codex-hook-adapter.js +13 -13
  20. package/dist/hooks/duplicate-read-guard.js +7 -7
  21. package/dist/hooks/format-edited.js +3 -3
  22. package/dist/hooks/inputs.json +190 -191
  23. package/dist/hooks/investigation-guard.js +7 -7
  24. package/dist/hooks/pretool-guard.js +12 -12
  25. package/dist/hooks/session-lang.js +3 -3
  26. package/dist/hooks/stop-guard.js +19 -15
  27. package/dist/josh.js +40 -5
  28. package/package.json +4 -4
  29. package/prompts/collaboration-workflow/upstream-interrupt.md +1 -1
  30. package/scripts/adopt/adopt-logic.ts +4 -1
  31. package/scripts/adopt/adopt.ts +7 -4
  32. package/scripts/backlog/backlog-budget-cli.ts +2 -20
  33. package/scripts/backlog/backlog-budget.ts +72 -109
  34. package/scripts/backlog/backlog-drive-cli.ts +10 -3
  35. package/scripts/backlog/backlog-drive.ts +5 -2
  36. package/scripts/backlog/backlog-offer-cli.ts +1 -15
  37. package/scripts/cost-runtime/context-cut-payback.ts +9 -4
  38. package/scripts/cost-runtime/cost-pricing.ts +94 -33
  39. package/scripts/document/document-byte-budget.ts +1 -1
  40. package/scripts/document/read-set-pricing.ts +7 -5
  41. package/scripts/epic/epic-candidate-confirm.ts +1 -1
  42. package/scripts/epic/epic-cross-repo.ts +4 -4
  43. package/scripts/epic/epic-next.ts +1 -1
  44. package/scripts/git/git-spawn-sync.ts +9 -1
  45. package/scripts/hooks/stop-guard.ts +20 -9
  46. package/scripts/init/init-ai-copy.ts +10 -3
  47. package/scripts/init/init-copy-content.ts +8 -1
  48. package/scripts/init/security-policy.ts +79 -0
  49. package/scripts/josh/josh-commands-ai.ts +1 -1
  50. package/scripts/josh/josh-commands-maintenance.ts +3 -0
  51. package/scripts/josh/josh-commands-project.ts +1 -1
  52. package/scripts/josh/josh-commands-versioning.ts +2 -1
  53. package/scripts/lane/lane-cli.ts +1 -3
  54. package/scripts/propagate/propagate-run.ts +17 -1
  55. package/scripts/propagate/propagate-steps.ts +9 -1
  56. package/scripts/run/board/run-board-header.ts +107 -84
  57. package/scripts/run/board/run-board-labels.ts +18 -17
  58. package/scripts/run/board/run-board-machine.ts +21 -8
  59. package/scripts/run/board/run-board-render.ts +1 -5
  60. package/scripts/run/board/run-board-span.ts +0 -1
  61. package/scripts/run/board/run-board-status.ts +0 -17
  62. package/scripts/run/board/run-board-tick.ts +1 -0
  63. package/scripts/run/carry/run-carry-cli.ts +8 -62
  64. package/scripts/run/carry/run-carry-ended.ts +1 -1
  65. package/scripts/run/carry/run-carry.ts +22 -68
  66. package/scripts/run/cut/run-cut.ts +4 -4
  67. package/scripts/run/event/run-event-render.ts +0 -1
  68. package/scripts/run/event/run-event-scope.ts +4 -8
  69. package/scripts/run/event/run-event-stream-emit.ts +0 -25
  70. package/scripts/run/event/run-event-stream.ts +23 -31
  71. package/scripts/run/merge/run-merge-collect.ts +1 -1
  72. package/scripts/run/merge/run-merge-steps.ts +1 -1
  73. package/scripts/run/progress/run-progress-read.ts +1 -1
  74. package/scripts/run/run-headless.ts +2 -2
  75. package/scripts/run/run-status-cli.ts +1 -5
  76. package/scripts/run/run-step.ts +2 -9
  77. package/scripts/run/run-stop-notify.ts +1 -1
  78. package/scripts/run/run-stranded-detect.ts +3 -3
  79. package/scripts/run/run-stranded.ts +25 -17
  80. package/scripts/run/wake/run-wake-cli.ts +7 -9
  81. package/scripts/run/wake/run-wake-describe.ts +3 -5
  82. package/scripts/run/wake/run-wake-driver.ts +3 -3
  83. package/scripts/run/wake/run-wake-loop.ts +4 -21
  84. package/scripts/run/wake/run-wake-restart.ts +1 -1
  85. package/scripts/run/wake/run-wake.ts +6 -7
  86. package/scripts/sync/sync-ai-files.ts +2 -0
  87. package/scripts/version/latest-update.ts +20 -6
  88. package/scripts/version/peer-floor.ts +99 -0
  89. package/scripts/version/playwright-browsers.ts +109 -0
  90. package/scripts/version/publishable-range-check.ts +41 -4
  91. package/templates/SECURITY.md +23 -0
  92. package/scripts/backlog/backlog-idle.ts +0 -88
@@ -0,0 +1,99 @@
1
+ import semver from 'semver'
2
+ import { z } from 'zod'
3
+
4
+ // A package built on a toolkit publishes its own `peerDependencies`, and a toolkit raising a peer
5
+ // floor edits nobody's manifest but its own. `josh latest` rewrites `dependencies` and
6
+ // `devDependencies` only, so the consumer keeps declaring a floor it no longer develops or verifies
7
+ // against: a user on a version between the two floors satisfies the consumer's declaration and
8
+ // breaks the toolkit's.
9
+
10
+ interface LaggingPeer {
11
+ name: string
12
+ range: string
13
+ /** The installed toolkit as `name@version`, so the reader knows which release set the floor. */
14
+ toolkit: string
15
+ toolkit_range: string
16
+ }
17
+
18
+ const peer_manifest_schema = z.looseObject({
19
+ name: z.string().optional(),
20
+ version: z.string().optional(),
21
+ peerDependencies: z.record(z.string(), z.string()).optional(),
22
+ })
23
+
24
+ type PeerManifest = z.infer<typeof peer_manifest_schema>
25
+
26
+ function parse_manifest(package_json_content: string): PeerManifest {
27
+ return peer_manifest_schema.parse(JSON.parse(package_json_content))
28
+ }
29
+
30
+ // A range semver cannot read (`workspace:*`, `catalog:`) has no floor to compare, and neither does
31
+ // one nothing can satisfy; both answer "not below" rather than inventing a violation.
32
+ function is_floor_below(range: string, toolkit_range: string): boolean {
33
+ if (semver.validRange(range) === null || semver.validRange(toolkit_range) === null) return false
34
+
35
+ const floor = semver.minVersion(range)
36
+ const toolkit_floor = semver.minVersion(toolkit_range)
37
+
38
+ if (floor === null || toolkit_floor === null) return false
39
+
40
+ return semver.lt(floor, toolkit_floor)
41
+ }
42
+
43
+ function toolkit_label(toolkit: PeerManifest): string {
44
+ return [toolkit.name, toolkit.version].filter((part) => part !== undefined).join('@')
45
+ }
46
+
47
+ function to_lagging_peer(
48
+ [name, range]: [string, string],
49
+ toolkit: PeerManifest,
50
+ ): Array<LaggingPeer> {
51
+ const toolkit_range = toolkit.peerDependencies?.[name]
52
+
53
+ if (toolkit_range === undefined || !is_floor_below(range, toolkit_range)) return []
54
+
55
+ return [{ name, range, toolkit: toolkit_label(toolkit), toolkit_range }]
56
+ }
57
+
58
+ // A toolkit's own manifest is never compared with itself: there it is the source of the floors, not
59
+ // a consumer of them.
60
+ function find_lagging_against(manifest: PeerManifest, toolkit: PeerManifest): Array<LaggingPeer> {
61
+ if (manifest.name === toolkit.name) return []
62
+
63
+ return Object.entries(manifest.peerDependencies ?? {}).flatMap((entry) =>
64
+ to_lagging_peer(entry, toolkit),
65
+ )
66
+ }
67
+
68
+ // Every installed toolkit is compared, not kit alone: a package built on app-kit inherits app-kit's
69
+ // floors exactly as app-kit inherits kit's, and the gap is the same one a tier up.
70
+ function find_lagging(
71
+ package_json_content: string,
72
+ toolkit_json_contents: ReadonlyArray<string>,
73
+ ): Array<LaggingPeer> {
74
+ const manifest = parse_manifest(package_json_content)
75
+
76
+ return toolkit_json_contents.flatMap((content) =>
77
+ find_lagging_against(manifest, parse_manifest(content)),
78
+ )
79
+ }
80
+
81
+ function format_lagging(peer: LaggingPeer): string {
82
+ return ` ${peer.name}@${peer.range} — ${peer.toolkit} requires ${peer.toolkit_range}`
83
+ }
84
+
85
+ function format_failure(lagging: ReadonlyArray<LaggingPeer>): string {
86
+ return [
87
+ `\n✖ ${String(lagging.length)} published peer range(s) fall below an installed toolkit's:`,
88
+ ...lagging.map((peer) => format_lagging(peer)),
89
+ '',
90
+ ' A user inside the gap satisfies this package and breaks the toolkit it is built on, so the',
91
+ ' declaration promises a combination nobody verified. Raise each `peerDependencies` floor',
92
+ ' in package.json to the range the toolkit requires.',
93
+ ].join('\n')
94
+ }
95
+
96
+ const peer_floor = { find_lagging, format_failure }
97
+
98
+ export { peer_floor }
99
+ export type { LaggingPeer }
@@ -0,0 +1,109 @@
1
+ import { existsSync } from 'node:fs'
2
+ import path from 'node:path'
3
+ import { json_value } from '#scripts/lib/json-value'
4
+ import { file_reader } from '#scripts/lib/read-file'
5
+ import { execaSync } from 'execa'
6
+ import { z } from 'zod'
7
+
8
+ // A Playwright release drives one exact browser build, so a bumped `@playwright/test` cannot launch
9
+ // the browsers the previous version downloaded: every browser test fails with "Executable doesn't
10
+ // exist" until `playwright install` runs. `josh latest` is what moves the version, so it is also
11
+ // what has to leave the matching browsers behind — `josh adopt` runs the related tests right after
12
+ // it, with nobody in between to run the install by hand (joshuafolkken/kit#3675).
13
+
14
+ const INSTALLED_MANIFEST_PATH = path.join('node_modules', '@playwright', 'test', 'package.json')
15
+ const INSTALLED_MANIFEST_SCHEMA = z.object({ version: z.string() })
16
+ const BROWSERS_VARIABLE = 'JOSH_PLAYWRIGHT_BROWSERS'
17
+ // Leading letter required: a value starting with a dash would reach the command as an option.
18
+ const BROWSER_NAME_RE = /^[A-Za-z]/u
19
+ const FAILURE_EXIT_CODE = 1
20
+ const INSTALL_COMMAND: ReadonlyArray<string> = ['pnpm', 'exec', 'playwright', 'install']
21
+ const DRY_RUN_FLAG = '--dry-run'
22
+ const INSTALL_LOCATION_RE = /Install location: +(\S.*)/gu
23
+ // Written last by `playwright install`, so a directory an interrupted download left behind lacks it.
24
+ const COMPLETE_MARKER = 'INSTALLATION_COMPLETE'
25
+
26
+ // Chromium alone, as the CI fallback installs: a bare `playwright install` fetches chromium, firefox
27
+ // and webkit — roughly 1 GB — for the single chromium project kit scaffolds.
28
+ const DEFAULT_BROWSERS: ReadonlyArray<string> = ['chromium']
29
+
30
+ // The version actually installed, not the range in `package.json`: the browsers follow the resolved
31
+ // release, and a repository without the package answers `undefined` on both sides of the update.
32
+ function read_installed_version(): string | undefined {
33
+ const content = file_reader.read_file_or_empty(INSTALLED_MANIFEST_PATH)
34
+
35
+ return json_value.parse_with(content, INSTALLED_MANIFEST_SCHEMA)?.version
36
+ }
37
+
38
+ // The same variable name the CI workflow reads, sanitized the way its `tr -cd 'A-Za-z -'` does:
39
+ // everything but letters, dashes and the whitespace between names is stripped out of a name rather
40
+ // than the whole name dropped, so a mistyped `Chromium` or `chromium,firefox` reaches Playwright as
41
+ // an unknown browser and fails at the install — never silently narrowed to chromium, which would
42
+ // resurface later as a missing executable. A value with no usable name falls back to the default
43
+ // rather than to a bare install.
44
+ function resolve_browsers(environment: NodeJS.ProcessEnv = process.env): ReadonlyArray<string> {
45
+ const value = (environment[BROWSERS_VARIABLE] ?? '').replaceAll(/[^A-Za-z\s-]/gu, '')
46
+ const browsers = value.split(/\s+/u).filter((name) => BROWSER_NAME_RE.test(name))
47
+
48
+ return browsers.length > 0 ? browsers : DEFAULT_BROWSERS
49
+ }
50
+
51
+ // A branded channel (`chrome`, `msedge`) reports `<system>` instead of a directory Playwright owns;
52
+ // there is nothing to look for, so it never counts as missing.
53
+ function is_downloaded(location: string): boolean {
54
+ return !path.isAbsolute(location) || existsSync(path.join(location, COMPLETE_MARKER))
55
+ }
56
+
57
+ // Asks Playwright where the named browsers belong and whether each one is completely there. A
58
+ // version comparison alone cannot tell: an install that failed or was interrupted leaves the next
59
+ // run with the same version on both sides of the update and the browsers still absent, and that run
60
+ // has to retry it rather than finish as if nothing were missing. A plan Playwright refuses counts as
61
+ // missing, so the install runs and reports the refusal.
62
+ function has_missing_browser(browsers: ReadonlyArray<string>): boolean {
63
+ const [cmd = '', ...rest] = [...INSTALL_COMMAND, DRY_RUN_FLAG, ...browsers]
64
+ const plan = execaSync(cmd, rest, { reject: false })
65
+ if (plan.exitCode !== 0) return true
66
+
67
+ const locations = [...plan.stdout.matchAll(INSTALL_LOCATION_RE)].map(
68
+ ([, location = '']) => location,
69
+ )
70
+
71
+ return locations.some((location) => !is_downloaded(location))
72
+ }
73
+
74
+ function run_install(version: string, browsers: ReadonlyArray<string>): number {
75
+ const command = [...INSTALL_COMMAND, ...browsers]
76
+ const [cmd = '', ...rest] = command
77
+
78
+ console.info(`\n▶ ${command.join(' ')} (browsers for @playwright/test ${version})`)
79
+
80
+ const status = execaSync(cmd, rest, { stdio: 'inherit', reject: false }).exitCode
81
+ if (status === 0) return 0
82
+
83
+ console.error(
84
+ `\n✖ Playwright browser install failed (${command.join(' ')} exited ${String(status)}); browser tests cannot run until it succeeds.`,
85
+ )
86
+
87
+ return FAILURE_EXIT_CODE
88
+ }
89
+
90
+ // `before` is the version read ahead of the update. A moved version always installs; an unmoved one
91
+ // installs only what an earlier run left missing, so a run that leaves Playwright alone with its
92
+ // browsers in place costs no download. Nothing runs when the package is absent.
93
+ function install_when_needed(before: string | undefined): number {
94
+ const after = read_installed_version()
95
+ if (after === undefined) return 0
96
+
97
+ const browsers = resolve_browsers()
98
+ if (after === before && !has_missing_browser(browsers)) return 0
99
+
100
+ return run_install(after, browsers)
101
+ }
102
+
103
+ const playwright_browsers = {
104
+ read_installed_version,
105
+ resolve_browsers,
106
+ install_when_needed,
107
+ }
108
+
109
+ export { playwright_browsers }
@@ -1,13 +1,17 @@
1
1
  #!/usr/bin/env tsx
2
2
  /**
3
- * Release gate: every dependency range this package publishes must resolve for a consumer.
3
+ * Release gate: every dependency range this package publishes must resolve for a consumer, and no
4
+ * peer range it publishes may fall below an installed toolkit's.
4
5
  *
5
6
  * Usage: tsx scripts/version/publishable-range-check.ts
6
7
  */
7
- import { readFileSync } from 'node:fs'
8
+ import { existsSync, readFileSync } from 'node:fs'
9
+ import path from 'node:path'
8
10
  import { fileURLToPath } from 'node:url'
11
+ import { adopt_toolkits } from '#scripts/adopt/adopt-toolkits'
9
12
  import { COMMAND_TIMEOUT_MS } from '#scripts/lib/timeouts'
10
13
  import { execaSync } from 'execa'
14
+ import { peer_floor } from './peer-floor'
11
15
  import {
12
16
  publishable_range,
13
17
  type ProbeResult,
@@ -17,6 +21,7 @@ import {
17
21
 
18
22
  const PACKAGE_JSON_PATH = 'package.json'
19
23
  const FAILURE_EXIT_CODE = 1
24
+ const NODE_MODULES = 'node_modules'
20
25
 
21
26
  // A registry query rather than a direct fetch: safe-chain installs shims ahead of the package
22
27
  // managers on PATH, so the answer comes back already filtered by the same minimum-age policy a
@@ -64,12 +69,44 @@ function check(package_json_content: string, probe: RangeProbe): number {
64
69
  return report(publishable_range.find_unsatisfiable(checked, probe), checked.length)
65
70
  }
66
71
 
72
+ /** No installed toolkit means nothing to compare against, which passes in silence. */
73
+ function check_peer_floors(
74
+ package_json_content: string,
75
+ toolkit_json_contents: ReadonlyArray<string>,
76
+ ): number {
77
+ const lagging = peer_floor.find_lagging(package_json_content, toolkit_json_contents)
78
+
79
+ if (lagging.length === 0) return 0
80
+
81
+ console.error(peer_floor.format_failure(lagging))
82
+
83
+ return FAILURE_EXIT_CODE
84
+ }
85
+
86
+ // The manifest of every `@joshuafolkken/*` toolkit this project declares and has installed. The
87
+ // roster is `josh adopt`'s own rather than a second one kept here, so a toolkit published later is
88
+ // compared the day it is installed. kit's own checkout declares none, and a declared toolkit that is
89
+ // not installed has no manifest to read.
90
+ function read_installed_toolkit_manifests(project_root: string): Array<string> {
91
+ return adopt_toolkits
92
+ .all_declared_toolkits(project_root)
93
+ .map((package_name) => path.join(project_root, NODE_MODULES, package_name, PACKAGE_JSON_PATH))
94
+ .filter((manifest_path) => existsSync(manifest_path))
95
+ .map((manifest_path) => readFileSync(manifest_path, 'utf8'))
96
+ }
97
+
98
+ // Both checks always run, so one invocation reports everything there is to fix.
67
99
  function main(): void {
68
- process.exit(check(readFileSync(PACKAGE_JSON_PATH, 'utf8'), probe_range))
100
+ const package_json_content = readFileSync(PACKAGE_JSON_PATH, 'utf8')
101
+ const range_exit_code = check(package_json_content, probe_range)
102
+ const toolkit_json_contents = read_installed_toolkit_manifests(process.cwd())
103
+ const peer_exit_code = check_peer_floors(package_json_content, toolkit_json_contents)
104
+
105
+ process.exit(Math.max(range_exit_code, peer_exit_code))
69
106
  }
70
107
 
71
108
  if (process.argv[1] === fileURLToPath(import.meta.url)) main()
72
109
 
73
- const publishable_range_check = { check }
110
+ const publishable_range_check = { check, check_peer_floors, read_installed_toolkit_manifests }
74
111
 
75
112
  export { publishable_range_check }
@@ -0,0 +1,23 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ | Version | Supported |
6
+ | ------- | ------------------ |
7
+ | Latest | :white_check_mark: |
8
+ | Older | :x: |
9
+
10
+ Fixes ship in a new release rather than as patches to older versions; upgrade to the latest release to receive them. Each release's changes are listed on the [Releases](https://github.com/{{REPOSITORY}}/releases) page.
11
+
12
+ ## Reporting a Vulnerability
13
+
14
+ If you discover a security vulnerability, please do not report it publicly.
15
+
16
+ - **Preferred**: Open a [draft security advisory](https://github.com/{{REPOSITORY}}/security/advisories/new) on GitHub.
17
+ - **Alternative**: If private reporting is not enabled there, contact a maintainer privately rather than opening a public issue.
18
+
19
+ We aim to acknowledge reports within **48 hours** and provide a resolution or mitigation plan within **7 days** for confirmed vulnerabilities.
20
+
21
+ ## Development Tooling
22
+
23
+ This repository takes development tooling from the [`@joshuafolkken/kit`](https://github.com/joshuafolkken/kit) package — depending on the profile it was set up with, the `josh` command, GitHub Actions workflows, Git hooks, AI agent instructions and hooks, and configuration presets. What that package ships and runs on your machine and in CI, the secrets its tooling reads, how to install safe-chain and the external MCP endpoints it configures are described in [kit's security policy](https://github.com/joshuafolkken/kit/blob/main/SECURITY.md). Report a vulnerability in that tooling to kit rather than here.
@@ -1,88 +0,0 @@
1
- import { backlog_budget, type BudgetInput } from './backlog-budget'
2
-
3
- // The idle watch as a window a person can read: when the run began waiting on
4
- // an empty backlog, when the wait ends, and which bound ends it. Without it the moment the wait began
5
- // would live only in `backlog:drive`'s memory as `--active`, so a reader of the event stream could
6
- // tell the run was waiting but not until when. The budget names the
7
- // window, `backlog:offer` records it as an `idle` event, and `run:board` reads it back — all through the
8
- // text written and parsed here, so the three never spell it differently.
9
-
10
- type IdleBound = 'idle' | 'whole-run'
11
-
12
- interface IdleWindow {
13
- since_ms: number
14
- until_ms: number
15
- // Which bound ends the wait: the idle budget running out, or the whole-run bound arriving first.
16
- bound: IdleBound
17
- // When the watching loop last asked the backlog — the poll the next one is counted from.
18
- asked_ms: number
19
- }
20
-
21
- const TEXT_PATTERN =
22
- /^idle since (?<since>\S+) until (?<until>\S+) \((?<bound>idle|whole-run)\) asked (?<asked>\S+)$/u
23
- const IDLE_BOUND: IdleBound = 'idle'
24
- const WHOLE_RUN_BOUND: IdleBound = 'whole-run'
25
-
26
- // The window an idle watch opened at `active_at_ms` runs for, as asked at `now_ms`, or `undefined` when
27
- // the watch is off. The wait ends at the earlier of the idle budget and the whole-run bound — the order
28
- // `decide` applies.
29
- function window_of(input: BudgetInput): IdleWindow | undefined {
30
- if (input.idle_budget_ms === undefined) return undefined
31
-
32
- const idle_until = input.active_at_ms + input.idle_budget_ms
33
- const run_until = input.started_at_ms + backlog_budget.WHOLE_RUN_BUDGET_MS
34
- const bound = run_until < idle_until ? WHOLE_RUN_BOUND : IDLE_BOUND
35
-
36
- return {
37
- since_ms: input.active_at_ms,
38
- until_ms: Math.min(idle_until, run_until),
39
- bound,
40
- asked_ms: input.now_ms,
41
- }
42
- }
43
-
44
- function iso_of(ms: number): string {
45
- return new Date(ms).toISOString()
46
- }
47
-
48
- function text_of(window: IdleWindow): string {
49
- const { since_ms, until_ms, bound, asked_ms } = window
50
-
51
- return `idle since ${iso_of(since_ms)} until ${iso_of(until_ms)} (${bound}) asked ${iso_of(asked_ms)}`
52
- }
53
-
54
- function is_bound(value: string | undefined): value is IdleBound {
55
- return value === IDLE_BOUND || value === WHOLE_RUN_BOUND
56
- }
57
-
58
- // A malformed stamp reads as no window rather than as `NaN`, so a mangled line is never drawn.
59
- function window_from(groups: Record<string, string>, bound: IdleBound): IdleWindow | undefined {
60
- const since_ms = Date.parse(String(groups['since']))
61
- const until_ms = Date.parse(String(groups['until']))
62
- const asked_ms = Date.parse(String(groups['asked']))
63
-
64
- return Number.isNaN(since_ms + until_ms + asked_ms)
65
- ? undefined
66
- : { since_ms, until_ms, bound, asked_ms }
67
- }
68
-
69
- function parse(text: string): IdleWindow | undefined {
70
- const groups = TEXT_PATTERN.exec(text)?.groups
71
-
72
- if (groups === undefined || !is_bound(groups['bound'])) return undefined
73
-
74
- return window_from(groups, groups['bound'])
75
- }
76
-
77
- // The next time the watching loop asks the backlog again: one `IDLE_POLL_MINUTES` after the ask the
78
- // stream recorded, never past the end of the wait — the interval `idle_watch_reason` tells the loop.
79
- function next_check_ms(window: IdleWindow): number {
80
- const poll_ms = backlog_budget.IDLE_POLL_MINUTES * backlog_budget.MS_PER_MINUTE
81
-
82
- return Math.min(window.asked_ms + poll_ms, window.until_ms)
83
- }
84
-
85
- const backlog_idle = { next_check_ms, parse, text_of, window_of }
86
-
87
- export { backlog_idle }
88
- export type { IdleBound, IdleWindow }