advisers 0.2.2 → 0.3.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 (70) hide show
  1. package/LICENSE +0 -0
  2. package/NOTICE +0 -0
  3. package/README-zh.md +11 -7
  4. package/README.md +16 -8
  5. package/THIRD-PARTY-NOTICES.md +1 -1
  6. package/bin/advisers.js +6347 -542
  7. package/index.js +1 -1
  8. package/lib/agent.js +529 -80
  9. package/lib/attachments.js +0 -0
  10. package/lib/audit-anchor.js +683 -0
  11. package/lib/backup.js +1345 -0
  12. package/lib/capture.js +0 -0
  13. package/lib/context-window.js +0 -0
  14. package/lib/digest.js +0 -0
  15. package/lib/doc-review.js +0 -0
  16. package/lib/egress.js +0 -0
  17. package/lib/error-codes.js +1373 -0
  18. package/lib/evidence.js +352 -43
  19. package/lib/fs-safe.js +347 -0
  20. package/lib/ical.js +0 -0
  21. package/lib/imap.js +6 -2
  22. package/lib/importers.js +0 -0
  23. package/lib/learning.js +0 -0
  24. package/lib/ledger-read.js +608 -0
  25. package/lib/mcp-server.js +331 -50
  26. package/lib/model.js +73 -1
  27. package/lib/node-warning-filter.js +15 -0
  28. package/lib/notify.js +13 -3
  29. package/lib/nudge.js +0 -0
  30. package/lib/ocr.js +29 -9
  31. package/lib/pdf.js +0 -0
  32. package/lib/pet-brief-store.js +0 -0
  33. package/lib/pet-brief.js +126 -64
  34. package/lib/receipt-verify.js +213 -15
  35. package/lib/relative-date.js +0 -0
  36. package/lib/remind.js +91 -36
  37. package/lib/research.js +0 -0
  38. package/lib/review.js +0 -0
  39. package/lib/routine.js +0 -0
  40. package/lib/safety/classifier.js +55 -2
  41. package/lib/safety/explain.js +0 -0
  42. package/lib/safety/index.js +0 -0
  43. package/lib/safety/redact.js +0 -0
  44. package/lib/safety/rules.js +28 -0
  45. package/lib/scheduler.js +0 -0
  46. package/lib/source.js +0 -0
  47. package/lib/sqlite.js +2302 -55
  48. package/lib/sync.js +398 -71
  49. package/lib/text-safe.js +229 -0
  50. package/lib/timestamp.js +29 -9
  51. package/lib/tos/index.js +1 -1
  52. package/lib/tos/manifest.json +6 -6
  53. package/lib/tos/privacy.zh-CN.md +12 -9
  54. package/lib/tos/safety.zh-CN.md +4 -3
  55. package/lib/tos/summary.zh-CN.md +3 -3
  56. package/lib/tos/terms.zh-CN.md +8 -6
  57. package/lib/tui/color.js +164 -0
  58. package/lib/tui/index.js +33 -0
  59. package/lib/tui/json.js +124 -0
  60. package/lib/tui/labels.js +50 -0
  61. package/lib/tui/policy.js +108 -0
  62. package/lib/tui/render.js +371 -0
  63. package/lib/tui/sanitize.js +142 -0
  64. package/lib/tui/text-file.js +116 -0
  65. package/lib/tui/width.js +577 -0
  66. package/lib/ui.js +0 -0
  67. package/lib/undo.js +0 -0
  68. package/lib/untrusted.js +186 -0
  69. package/lib/vault.js +0 -0
  70. package/package.json +6 -5
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Colour policy and the 16-colour SGR whitelist (0.3.0 output kernel).
3
+ *
4
+ * Semantics, in priority order (documented in docs/TUI.md and tested):
5
+ *
6
+ * 1. --color=always|never explicit flag wins over environment
7
+ * 2. NO_COLOR (present, non-empty) disables colour in auto mode
8
+ * 3. TERM=dumb / non-TTY stdout disables colour in auto mode
9
+ * 4. --plain / --accessible disable colour and all cursor control
10
+ *
11
+ * Only the following SGR codes may ever be emitted (16-colour safe palette):
12
+ * 1 bold, 2 dim, 22 reset bold/dim, 31 red, 32 green, 33 yellow, 39 reset fg.
13
+ * No 256-colour, truecolour, background, reverse, blink, underline or OSC of any
14
+ * kind. Colour never carries the only meaning: every line has a text prefix too.
15
+ */
16
+
17
+ /** Accepted --color values. */
18
+ export const COLOR_MODES = Object.freeze(['auto', 'always', 'never'])
19
+
20
+ /** Named SGR codes the kernel may emit. */
21
+ export const SGR = Object.freeze({
22
+ bold: 1,
23
+ dim: 2,
24
+ resetIntensity: 22,
25
+ red: 31,
26
+ green: 32,
27
+ yellow: 33,
28
+ resetForeground: 39,
29
+ })
30
+
31
+ /** The complete SGR allowlist. Tests extract every emitted code and compare. */
32
+ export const ALLOWED_SGR_CODES = Object.freeze([1, 2, 22, 31, 32, 33, 39])
33
+
34
+ const SGR_PATTERN = /\u001b\[(\d+(?:;\d+)*)m/g
35
+
36
+ /**
37
+ * Parse a --color value.
38
+ *
39
+ * @param {unknown} value
40
+ * @returns {'auto' | 'always' | 'never' | null} null when the value is not supported
41
+ */
42
+ export function parseColorMode(value) {
43
+ if (typeof value !== 'string') return null
44
+ const mode = value.trim().toLowerCase()
45
+ return COLOR_MODES.includes(mode) ? mode : null
46
+ }
47
+
48
+ /**
49
+ * Find --color in an argv-style array.
50
+ *
51
+ * Supports both `--color=never` and `--color never`.
52
+ *
53
+ * @param {unknown} argv
54
+ * @returns {{ found: boolean, mode: 'auto' | 'always' | 'never' | null, invalid: boolean }}
55
+ */
56
+ export function readColorArg(argv) {
57
+ if (!Array.isArray(argv)) return { found: false, mode: null, invalid: false }
58
+ for (let index = 0; index < argv.length; index += 1) {
59
+ const argument = argv[index]
60
+ if (typeof argument !== 'string') continue
61
+ if (argument === '--color') {
62
+ const mode = parseColorMode(argv[index + 1])
63
+ return { found: true, mode, invalid: mode === null }
64
+ }
65
+ if (argument.startsWith('--color=')) {
66
+ const mode = parseColorMode(argument.slice('--color='.length))
67
+ return { found: true, mode, invalid: mode === null }
68
+ }
69
+ }
70
+ return { found: false, mode: null, invalid: false }
71
+ }
72
+
73
+ /**
74
+ * NO_COLOR convention: present and not an empty string disables colour.
75
+ *
76
+ * @param {NodeJS.ProcessEnv | Record<string, unknown>} [env]
77
+ * @returns {boolean}
78
+ */
79
+ export function noColorEnv(env = process.env) {
80
+ return Boolean(env) && typeof env.NO_COLOR === 'string' && env.NO_COLOR !== ''
81
+ }
82
+
83
+ /**
84
+ * Resolve whether SGR colour may be emitted.
85
+ *
86
+ * @param {{ mode?: 'auto' | 'always' | 'never', env?: Record<string, unknown>, stdoutIsTTY?: boolean, term?: string }} [options]
87
+ * @returns {boolean}
88
+ */
89
+ export function resolveColorEnabled({ mode = 'auto', env = process.env, stdoutIsTTY = false, term } = {}) {
90
+ if (mode === 'always') return true
91
+ if (mode === 'never') return false
92
+ const terminal = term === undefined ? env?.TERM : term
93
+ if (!stdoutIsTTY) return false
94
+ if (terminal === 'dumb') return false
95
+ if (noColorEnv(env)) return false
96
+ return true
97
+ }
98
+
99
+ /**
100
+ * Wrap text in whitelisted SGR codes.
101
+ *
102
+ * @param {unknown} text
103
+ * @param {string | string[] | null | undefined} style one or more keys of SGR
104
+ * @param {{ enabled?: boolean }} [options]
105
+ * @returns {string}
106
+ */
107
+ export function styleText(text, style, { enabled = true } = {}) {
108
+ const value = text === null || text === undefined ? '' : String(text)
109
+ if (!enabled || style === null || style === undefined) return value
110
+ const names = Array.isArray(style) ? style : [style]
111
+ const codes = []
112
+ for (const name of names) {
113
+ if (typeof name !== 'string') continue
114
+ const code = SGR[name]
115
+ if (typeof code === 'number') codes.push(code)
116
+ }
117
+ if (codes.length === 0) return value
118
+ const open = codes.map((code) => `\u001b[${code}m`).join('')
119
+ const close = [...codes].reverse().map((code) => `\u001b[${resetCodeFor(code)}m`).join('')
120
+ return open + value + close
121
+ }
122
+
123
+ function resetCodeFor(code) {
124
+ if (code === SGR.dim || code === SGR.bold) return SGR.resetIntensity
125
+ return SGR.resetForeground
126
+ }
127
+
128
+ /**
129
+ * Remove only SGR colour sequences (other control characters are left for sanitisation).
130
+ *
131
+ * @param {unknown} value
132
+ * @returns {string}
133
+ */
134
+ export function stripSgr(value) {
135
+ if (value === null || value === undefined) return ''
136
+ return String(value).replace(SGR_PATTERN, '')
137
+ }
138
+
139
+ /**
140
+ * Extract every SGR code present in a string (test/audit helper).
141
+ *
142
+ * @param {unknown} value
143
+ * @returns {number[]}
144
+ */
145
+ export function sgrCodes(value) {
146
+ if (value === null || value === undefined) return []
147
+ const text = String(value)
148
+ const codes = []
149
+ for (const match of text.matchAll(SGR_PATTERN)) {
150
+ for (const part of match[1].split(';')) codes.push(Number.parseInt(part, 10))
151
+ }
152
+ return codes
153
+ }
154
+
155
+ /**
156
+ * True when every SGR sequence in the string uses a whitelisted code.
157
+ *
158
+ * @param {unknown} value
159
+ * @returns {boolean}
160
+ */
161
+ export function hasOnlyWhitelistedSgr(value) {
162
+ return sgrCodes(value).every((code) => ALLOWED_SGR_CODES.includes(code))
163
+ }
164
+
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Advisers 0.3.0 output kernel (pure functions, no terminal I/O).
3
+ *
4
+ * Modules:
5
+ * width.js display width / graphemes / wrap / truncate / pad / columns
6
+ * sanitize.js sanitizeDisplay / jsonSafe / display safety predicates
7
+ * color.js --color / NO_COLOR precedence + 16-colour SGR whitelist
8
+ * policy.js resolveOutputPolicy: TTY / plain / accessible / interactive
9
+ * render.js renderStatus / renderResult / renderError / renderConfirm / renderSnapshot
10
+ * json.js JSON purity helpers (stringifyJson / isJsonPure / assertJsonPure)
11
+ * text-file.js BOM-aware text/JSON reads + BOM-less atomic writes
12
+ *
13
+ * lib/text-safe.js holds the low-level console/log primitives (sanitizeConsoleText);
14
+ * it is re-exported here for convenience. bin/advisers.js wiring lands in Wave 2; this
15
+ * barrel adds no side effects and does not read process.argv/env.
16
+ */
17
+ export * from './width.js'
18
+ export * from './sanitize.js'
19
+ export * from './color.js'
20
+ export * from './policy.js'
21
+ export * from './render.js'
22
+ export * from './json.js'
23
+ export * from './text-file.js'
24
+ export {
25
+ sanitizeConsoleText,
26
+ stripBom,
27
+ stripTerminalSequences,
28
+ stripBidiControls,
29
+ stripControlChars,
30
+ DEFAULT_CONSOLE_TEXT_MAX,
31
+ TRUNCATION_MARK,
32
+ } from '../text-safe.js'
33
+
@@ -0,0 +1,124 @@
1
+ /**
2
+ * JSON purity helpers for --json / --out consumers (0.3.0 output kernel).
3
+ *
4
+ * The CLI JSON contract says: stdout is exactly one JSON document, no human prose, no
5
+ * ANSI, diagnostics on stderr. These helpers make that checkable in unit tests and in
6
+ * CI without parsing human text:
7
+ *
8
+ * stringifyJson(value) deterministic serialisation; optional display sanitisation
9
+ * isJsonPure(text) parseable JSON + no raw C0/C1/DEL bytes
10
+ * assertJsonPure(text) throws JsonPurityError with the offending offset
11
+ *
12
+ * Purity is about the emitted byte stream, not about rewriting data. Data fidelity is
13
+ * the JSON contract's default (see docs/CLI-JSON.md); jsonSafe() from sanitize.js is
14
+ * opt-in for human-facing fields (message/hint/subject).
15
+ */
16
+ import { jsonSafe } from './sanitize.js'
17
+
18
+ /** Error code reported by assertJsonPure(). */
19
+ export const JSON_PURITY_CODE = 'E_JSON_IMPURE'
20
+
21
+ /**
22
+ * Error thrown when a --json stream is not pure JSON.
23
+ */
24
+ export class JsonPurityError extends Error {
25
+ /**
26
+ * @param {string} message
27
+ * @param {{ offset?: number, reason?: string }} [details]
28
+ */
29
+ constructor(message, { offset = -1, reason = 'unknown' } = {}) {
30
+ super(message)
31
+ this.name = 'JsonPurityError'
32
+ this.code = JSON_PURITY_CODE
33
+ this.offset = offset
34
+ this.reason = reason
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Serialise a value as JSON.
40
+ *
41
+ * @param {unknown} value
42
+ * @param {{ sanitize?: boolean, space?: number }} [options] `sanitize: true` runs
43
+ * jsonSafe() on strings/keys first (display fields only; not the data default).
44
+ * @returns {string}
45
+ */
46
+ export function stringifyJson(value, { sanitize = false, space = 0 } = {}) {
47
+ const prepared = sanitize ? jsonSafe(value) : value
48
+ const text = JSON.stringify(prepared, null, space)
49
+ if (typeof text !== 'string') throw new TypeError('stringifyJson: value is not JSON-serialisable')
50
+ return text
51
+ }
52
+
53
+ /**
54
+ * Locate the first reason a text is not a pure JSON stream.
55
+ *
56
+ * @param {unknown} text
57
+ * @returns {{ reason: 'not-json' | 'empty' | 'control-char' | 'c1-control', offset: number } | null}
58
+ */
59
+ export function findJsonImpurity(text) {
60
+ if (typeof text !== 'string') return { reason: 'not-json', offset: -1 }
61
+ const trimmed = text.trim()
62
+ if (trimmed === '') return { reason: 'empty', offset: -1 }
63
+ try {
64
+ JSON.parse(trimmed)
65
+ } catch {
66
+ return { reason: 'not-json', offset: -1 }
67
+ }
68
+ for (let index = 0; index < trimmed.length; index += 1) {
69
+ const code = trimmed.charCodeAt(index)
70
+ const isForbidden = code <= 0x08 || (code >= 0x0b && code <= 0x0c) || (code >= 0x0e && code <= 0x1f) || (code >= 0x7f && code <= 0x9f)
71
+ if (!isForbidden) continue
72
+ return { reason: code >= 0x7f ? 'c1-control' : 'control-char', offset: index }
73
+ }
74
+ return null
75
+ }
76
+
77
+ /**
78
+ * True when text is exactly one JSON document with no raw control bytes that could
79
+ * forge terminal state (ANSI/C1). Human prose mixed into the stream fails this check.
80
+ *
81
+ * @param {unknown} text
82
+ * @returns {boolean}
83
+ */
84
+ export function isJsonPure(text) {
85
+ return findJsonImpurity(text) === null
86
+ }
87
+
88
+ /**
89
+ * Assert a --json stream is pure; throws JsonPurityError otherwise.
90
+ *
91
+ * @param {unknown} text
92
+ * @param {{ label?: string }} [options] label names the stream in the error message
93
+ * @returns {string} the input text (for fluent use)
94
+ */
95
+ export function assertJsonPure(text, { label = 'stdout' } = {}) {
96
+ if (typeof text !== 'string') throw new JsonPurityError(`${label}: expected a JSON string, got ${typeof text}`, { reason: 'not-json' })
97
+ const impurity = findJsonImpurity(text)
98
+ if (impurity === null) return text
99
+ if (impurity.reason === 'empty') throw new JsonPurityError(`${label}: expected one JSON document, got an empty stream`, { reason: 'empty' })
100
+ if (impurity.reason === 'not-json') {
101
+ const preview = text.trim().slice(0, 80).replace(/[\u0000-\u001f\u007f-\u009f]/g, '?')
102
+ throw new JsonPurityError(`${label}: stream is not one JSON document (human text or a second document leaked in): ${preview}`, { reason: 'not-json' })
103
+ }
104
+ const codePoint = text.trim().charCodeAt(impurity.offset)
105
+ const hex = codePoint.toString(16).toUpperCase().padStart(2, '0')
106
+ throw new JsonPurityError(`${label}: raw control character U+${hex} at offset ${impurity.offset} would corrupt a terminal; JSON output must not contain ANSI/C0/C1 bytes`, { offset: impurity.offset, reason: impurity.reason })
107
+ }
108
+
109
+ /**
110
+ * Build a stderr error envelope object for a failed --json command.
111
+ *
112
+ * Rendering is the caller's job (stdout stays empty); this helper keeps the envelope
113
+ * shape consistent without owning the error-code registry.
114
+ *
115
+ * @param {{ code: string, message: string, hint?: string, details?: unknown }} error
116
+ * @returns {{ error: { code: string, message: string, hint?: string, details?: unknown } }}
117
+ */
118
+ export function errorEnvelope({ code, message, hint, details } = {}) {
119
+ const envelope = { error: { code: String(code ?? 'E_UNKNOWN'), message: String(message ?? '') } }
120
+ if (hint !== undefined) envelope.error.hint = String(hint)
121
+ if (details !== undefined) envelope.error.details = jsonSafe(details)
122
+ return envelope
123
+ }
124
+
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Default (zh-CN) label table for the 0.3.0 output kernel.
3
+ *
4
+ * 0.3.0 ships Simplified Chinese human-facing text. This table is the kernel's single
5
+ * place for display words so callers can inject a different table (tests use it to
6
+ * prove no bare literals leak into render logic) and so the 0.3.1 i18n seam can
7
+ * re-export or replace it without touching render.js.
8
+ *
9
+ * Machine-facing strings (command names, flags, JSON keys, error codes) are English
10
+ * and never live here.
11
+ */
12
+ export const DEFAULT_LABELS = Object.freeze({
13
+ userPrefix: '> ',
14
+ itemPrefix: ' - ',
15
+ indent: ' ',
16
+ filePrefix: ' 文件:',
17
+ checkPrefix: ' 校验:',
18
+ warningPrefix: ' ! ',
19
+ nextPrefix: ' 下一步:',
20
+ errorPrefix: '! 没做成:',
21
+ confirmPrefix: '! 需要确认:',
22
+ reasonPrefix: ' 原因:',
23
+ keptPrefix: ' 已保留:',
24
+ writePrefix: ' 写入:',
25
+ summaryPrefix: ' 摘要:',
26
+ digestPrefix: ' 摘要:',
27
+ codePrefix: ' 代码:',
28
+ confirmPrompt: 'y 写入 / n 取消(默认 n)',
29
+ statusSeparator: ' · ',
30
+ statusAccessiblePrefix: '[状态] ',
31
+ statusReady: '就绪',
32
+ statusBusy: (seconds) => `工作中 ${seconds}s · Esc 中断`,
33
+ statusBusyNoTimer: '工作中 · Esc 中断',
34
+ statusConfirm: '待确认 · y 写入 / n 取消(默认 n)',
35
+ statusError: (reason) => `上次失败:${reason} · /retry`,
36
+ statusUnknownReason: '未记录',
37
+ statusOffline: '模型不可达(上次失败) · /status',
38
+ statusLocalModel: '本地模式',
39
+ statusHelpHint: '/help',
40
+ statusToday: (due, overdue) => `今天 ${due} 件到期 / ${overdue} 件逾期`,
41
+ statusTodayClear: '今天没有到期项',
42
+ overflowItems: (count) => ` …(+${count} 条,/more 或 --json 查看)`,
43
+ overflowLines: (count) => ` …(+${count} 行,/more 或 --json 查看)`,
44
+ warningOverflow: (count) => `(+${count} 条)`,
45
+ verboseTool: (name, ok) => ` · ${name} ${ok ? 'ok' : 'FAILED'}`,
46
+ verboseDuration: (duration) => ` 耗时:${duration}`,
47
+ artifactBytes: (bytes) => bytes,
48
+ snapshotTitle: 'Advisers TUI',
49
+ })
50
+
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Output policy: TTY detection, plain/accessible modes and colour resolution.
3
+ *
4
+ * One policy object is computed at process start and passed to render* functions;
5
+ * it decides every terminal capability question in one place:
6
+ *
7
+ * plain no SGR, no cursor control (CR, clear-line, hide cursor, alt-screen)
8
+ * accessible plain + [状态] status lines + no timed refresh (screen readers)
9
+ * color SGR allowed (see lib/tui/color.js for the precedence)
10
+ * cursorControl CR / ESC[2K / cursor visibility sequences allowed
11
+ * statusRefresh the status line may be rewritten in place (TTY, not accessible)
12
+ * interactive stdin is a TTY, so a shell may prompt
13
+ *
14
+ * Precedence: --accessible and --plain force plain; otherwise non-TTY stdout or
15
+ * TERM=dumb forces plain; colour still follows --color/NO_COLOR inside non-plain
16
+ * mode. A non-TTY stdin means no prompt is possible (interactive=false) even when
17
+ * stdout is a TTY.
18
+ */
19
+ import { noColorEnv, readColorArg, resolveColorEnabled } from './color.js'
20
+
21
+ const TRUTHY_ENV = /^(1|true|yes|on)$/i
22
+ const FALSY_ENV = /^(0|false|no|off)$/i
23
+
24
+ /**
25
+ * Environment flag: set and not empty/false.
26
+ *
27
+ * @param {Record<string, unknown> | undefined} env
28
+ * @param {string} name
29
+ * @returns {boolean}
30
+ */
31
+ export function envFlag(env, name) {
32
+ if (!env) return false
33
+ const value = env[name]
34
+ if (value === undefined || value === null) return false
35
+ const text = String(value).trim()
36
+ if (text === '') return false
37
+ if (FALSY_ENV.test(text)) return false
38
+ if (TRUTHY_ENV.test(text)) return true
39
+ return true
40
+ }
41
+
42
+ /**
43
+ * Resolve the output policy for one invocation.
44
+ *
45
+ * @param {{
46
+ * argv?: string[],
47
+ * env?: Record<string, unknown>,
48
+ * stdinIsTTY?: boolean,
49
+ * stdoutIsTTY?: boolean,
50
+ * stderrIsTTY?: boolean,
51
+ * }} [options]
52
+ * @returns {Readonly<{ mode: 'tty' | 'plain' | 'accessible', color: boolean, plain: boolean,
53
+ * accessible: boolean, cursorControl: boolean, statusRefresh: boolean,
54
+ * interactive: boolean, stdinIsTTY: boolean, stdoutIsTTY: boolean, stderrIsTTY: boolean,
55
+ * colorMode: 'auto' | 'always' | 'never', reason: string, colorReason: string }>}
56
+ */
57
+ export function resolveOutputPolicy({ argv = [], env = process.env, stdinIsTTY = false, stdoutIsTTY = false, stderrIsTTY = false } = {}) {
58
+ const args = Array.isArray(argv) ? argv : []
59
+ const colorArg = readColorArg(args)
60
+ if (colorArg.invalid) throw new RangeError('--color must be auto, always or never')
61
+ const accessible = args.includes('--accessible') || envFlag(env, 'ADVISERS_ACCESSIBLE')
62
+ const plainFlag = args.includes('--plain') || envFlag(env, 'ADVISERS_PLAIN')
63
+ const hardPlain = accessible || plainFlag
64
+ const term = typeof env?.TERM === 'string' ? env.TERM : ''
65
+ const autoPlain = !stdoutIsTTY || term === 'dumb'
66
+ let reason = 'tty'
67
+ if (accessible) reason = 'accessible'
68
+ else if (plainFlag) reason = '--plain'
69
+ else if (!stdoutIsTTY) reason = 'stdout-not-tty'
70
+ else if (term === 'dumb') reason = 'term-dumb'
71
+ const colorMode = colorArg.found ? colorArg.mode : 'auto'
72
+ let color = false
73
+ let colorReason = 'plain'
74
+ if (!hardPlain) {
75
+ color = resolveColorEnabled({ mode: colorMode, env, stdoutIsTTY, term })
76
+ if (colorMode === 'always') colorReason = 'forced-always'
77
+ else if (colorMode === 'never') colorReason = 'forced-never'
78
+ else if (noColorEnv(env)) colorReason = 'no-color-env'
79
+ else if (term === 'dumb') colorReason = 'term-dumb'
80
+ else if (!stdoutIsTTY) colorReason = 'stdout-not-tty'
81
+ else colorReason = 'auto-tty'
82
+ }
83
+ // --plain/--accessible always mean "no ANSI at all"; auto-detection (non-TTY stdout,
84
+ // TERM=dumb) disables colour in auto mode and always suppresses cursor control. An
85
+ // explicit --color=always keeps its SGR even in auto-plain mode, but cursor control
86
+ // stays off, so `plain` still describes the suppressed interactive features.
87
+ const plain = hardPlain || autoPlain
88
+ const cursorControl = stdoutIsTTY && !hardPlain && !autoPlain
89
+ const statusRefresh = cursorControl && !accessible
90
+ return Object.freeze({
91
+ mode: accessible ? 'accessible' : plain ? 'plain' : 'tty',
92
+ color,
93
+ plain,
94
+ accessible,
95
+ cursorControl,
96
+ statusRefresh,
97
+ interactive: stdinIsTTY,
98
+ stdinIsTTY,
99
+ stdoutIsTTY,
100
+ stderrIsTTY,
101
+ colorMode,
102
+ reason,
103
+ colorReason,
104
+ hardPlain,
105
+ autoPlain,
106
+ })
107
+ }
108
+