kimi-plugin-cc 2.0.7 → 2.2.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 (85) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +50 -0
  4. package/README.md +22 -6
  5. package/agents/kimi-ask.md +3 -0
  6. package/agents/kimi-challenge.md +3 -1
  7. package/agents/kimi-rescue.md +3 -0
  8. package/agents/kimi-review.md +3 -1
  9. package/commands/ask.md +6 -2
  10. package/commands/challenge.md +6 -2
  11. package/commands/rescue.md +6 -2
  12. package/commands/review.md +6 -2
  13. package/dist/cli-client.js +36 -13
  14. package/dist/commands/ask.js +16 -7
  15. package/dist/commands/cli-helpers.js +80 -0
  16. package/dist/commands/pursue.js +4 -2
  17. package/dist/commands/repair-sessions.js +2 -1
  18. package/dist/commands/replay.js +3 -2
  19. package/dist/commands/rescue.js +19 -9
  20. package/dist/commands/result.js +7 -1
  21. package/dist/commands/review-gate.js +2 -1
  22. package/dist/commands/review.js +8 -5
  23. package/dist/commands/setup.js +2 -1
  24. package/dist/commands/status.js +2 -2
  25. package/dist/commands/swarm.js +7 -3
  26. package/dist/companion.js +6 -0
  27. package/dist/help.js +40 -0
  28. package/dist/hooks/config-safety.js +18 -9
  29. package/dist/job-store.js +43 -6
  30. package/dist/jobs.js +7 -3
  31. package/dist/kimi-engine.js +1 -0
  32. package/dist/kimi-errors.js +35 -1
  33. package/dist/kimi-timeouts.js +7 -7
  34. package/dist/parsing.js +87 -13
  35. package/dist/render.js +19 -1
  36. package/dist/session-recovery.js +203 -0
  37. package/dist/vendor/smol-toml/date.js +50 -21
  38. package/dist/vendor/smol-toml/error.js +6 -2
  39. package/dist/vendor/smol-toml/extract.js +259 -66
  40. package/dist/vendor/smol-toml/parse.js +64 -48
  41. package/dist/vendor/smol-toml/primitive.js +107 -135
  42. package/dist/vendor/smol-toml/struct.js +106 -134
  43. package/dist/vendor/smol-toml/util.js +22 -78
  44. package/dist/version.js +1 -1
  45. package/docs/migration.md +3 -3
  46. package/docs/registry-releases.md +35 -1
  47. package/docs/safety.md +49 -2
  48. package/package.json +2 -2
  49. package/plugins/kimi-codex/.codex-plugin/plugin.json +1 -1
  50. package/plugins/kimi-codex/dist/cli-client.js +36 -13
  51. package/plugins/kimi-codex/dist/commands/ask.js +16 -7
  52. package/plugins/kimi-codex/dist/commands/cli-helpers.js +80 -0
  53. package/plugins/kimi-codex/dist/commands/pursue.js +4 -2
  54. package/plugins/kimi-codex/dist/commands/repair-sessions.js +2 -1
  55. package/plugins/kimi-codex/dist/commands/replay.js +3 -2
  56. package/plugins/kimi-codex/dist/commands/rescue.js +19 -9
  57. package/plugins/kimi-codex/dist/commands/result.js +7 -1
  58. package/plugins/kimi-codex/dist/commands/review-gate.js +2 -1
  59. package/plugins/kimi-codex/dist/commands/review.js +8 -5
  60. package/plugins/kimi-codex/dist/commands/setup.js +2 -1
  61. package/plugins/kimi-codex/dist/commands/status.js +2 -2
  62. package/plugins/kimi-codex/dist/commands/swarm.js +7 -3
  63. package/plugins/kimi-codex/dist/companion.js +6 -0
  64. package/plugins/kimi-codex/dist/help.js +40 -0
  65. package/plugins/kimi-codex/dist/hooks/config-safety.js +18 -9
  66. package/plugins/kimi-codex/dist/job-store.js +43 -6
  67. package/plugins/kimi-codex/dist/jobs.js +7 -3
  68. package/plugins/kimi-codex/dist/kimi-engine.js +1 -0
  69. package/plugins/kimi-codex/dist/kimi-errors.js +35 -1
  70. package/plugins/kimi-codex/dist/kimi-timeouts.js +7 -7
  71. package/plugins/kimi-codex/dist/parsing.js +87 -13
  72. package/plugins/kimi-codex/dist/render.js +19 -1
  73. package/plugins/kimi-codex/dist/session-recovery.js +203 -0
  74. package/plugins/kimi-codex/dist/vendor/smol-toml/date.js +50 -21
  75. package/plugins/kimi-codex/dist/vendor/smol-toml/error.js +6 -2
  76. package/plugins/kimi-codex/dist/vendor/smol-toml/extract.js +259 -66
  77. package/plugins/kimi-codex/dist/vendor/smol-toml/parse.js +64 -48
  78. package/plugins/kimi-codex/dist/vendor/smol-toml/primitive.js +107 -135
  79. package/plugins/kimi-codex/dist/vendor/smol-toml/struct.js +106 -134
  80. package/plugins/kimi-codex/dist/vendor/smol-toml/util.js +22 -78
  81. package/plugins/kimi-codex/dist/version.js +1 -1
  82. package/plugins/kimi-codex/skills/kimi-ask/SKILL.md +3 -1
  83. package/plugins/kimi-codex/skills/kimi-challenge/SKILL.md +2 -1
  84. package/plugins/kimi-codex/skills/kimi-rescue/SKILL.md +3 -1
  85. package/plugins/kimi-codex/skills/kimi-review/SKILL.md +3 -2
@@ -0,0 +1,203 @@
1
+ import { constants } from "node:fs";
2
+ import { lstat, open, opendir, realpath } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { digestPrompt } from "./jobs.js";
5
+ // Total entries visited across both enumeration levels. A real store holds a
6
+ // few thousand sessions (2,494 entries on the reference host when this was
7
+ // written, growing by one per plugin run), so the preflight's 4,096 would have
8
+ // switched recovery off within months; the cap is a guard against a
9
+ // pathological tree, not a size budget. opendir streams, nothing is buffered.
10
+ // Dot-entries (`.index-dirty`, `.index-cache`) are upstream index state, never
11
+ // workspaces, and are skipped without being counted. Directories whose mtime
12
+ // predates the spawn cannot have been created by this run and are skipped
13
+ // before any read, so the byte budget scales with concurrent activity, not
14
+ // with store size.
15
+ const DIR_ENUM_CAP = 65_536;
16
+ const STATE_MAX_BYTES = 1024 * 1024;
17
+ const JOURNAL_PREFIX_BYTES = 4 * 1024 * 1024; // The prompt is argv-bounded (< 1 MiB); the first user message follows a few config records.
18
+ const SCAN_MAX_BYTES = 64 * 1024 * 1024;
19
+ const MTIME_SLACK_MS = 1000; // Filesystems with coarse mtime granularity.
20
+ const SESSION_ID = /^session_[0-9a-f-]{36}$/;
21
+ /** Metadata recovery only, never a substitute for resume lineage/plan scans.
22
+ * A capped or unreadable enumeration cannot establish uniqueness and refuses;
23
+ * skipping symlinked or dot entries is not partial — neither can be a session
24
+ * the child created.
25
+ *
26
+ * A candidate is bound to THIS job by content, not only by time: the first
27
+ * user message in the session's main journal must carry a text item whose
28
+ * sha256 equals the job's `prompt_digest` (the exact `-p` argument). Time and
29
+ * cwd alone would admit a concurrent TUI session, another host's finished job
30
+ * (host-local stores cannot see each other's session ids), or a `state.json`
31
+ * planted by repository code running under rescue. The digest is not unique
32
+ * on its own either (the same prompt can run twice), so the window and the
33
+ * exactly-one rule stay in force. */
34
+ export async function recoverSessionIdFromStore(options) {
35
+ const entryCap = options.entryCap ?? DIR_ENUM_CAP;
36
+ let entries = 0;
37
+ let bytes = 0;
38
+ let digestMismatches = 0;
39
+ let missingJournals = 0;
40
+ const candidates = [];
41
+ const capped = new Error("enumeration_capped");
42
+ async function* directories(dir) {
43
+ if ((await lstat(dir)).isSymbolicLink())
44
+ throw new Error("unreadable");
45
+ const handle = await opendir(dir);
46
+ for await (const entry of handle) {
47
+ if (entry.name.startsWith("."))
48
+ continue;
49
+ if (++entries > entryCap)
50
+ throw capped;
51
+ // A symlinked entry is never a session the child created; skipping it
52
+ // keeps one stray link from disabling recovery host-wide. Directories
53
+ // we descend are still lstat-anchored above.
54
+ if (entry.isSymbolicLink())
55
+ continue;
56
+ if (entry.isDirectory())
57
+ yield entry.name;
58
+ }
59
+ }
60
+ // Bounded no-follow read of a regular file. Returns undefined when the file
61
+ // is absent; throws "unreadable" for anything that is not a plain file. With
62
+ // `prefix`, a larger file yields its first `maxBytes` instead of refusing.
63
+ async function readBounded(file, maxBytes, prefix = false) {
64
+ let handle;
65
+ try {
66
+ handle = await open(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
67
+ }
68
+ catch (error) {
69
+ if (error.code === "ENOENT")
70
+ return undefined;
71
+ throw error;
72
+ }
73
+ try {
74
+ const stat = await handle.stat();
75
+ if (!stat.isFile() || (stat.size > maxBytes && !prefix))
76
+ throw new Error("unreadable");
77
+ const size = Math.min(stat.size, maxBytes);
78
+ bytes += size;
79
+ if (bytes > SCAN_MAX_BYTES)
80
+ throw capped;
81
+ const buffer = Buffer.alloc(size);
82
+ let offset = 0;
83
+ while (offset < buffer.length) {
84
+ const read = await handle.read(buffer, offset, buffer.length - offset, offset);
85
+ if (!read.bytesRead)
86
+ break;
87
+ offset += read.bytesRead;
88
+ }
89
+ return buffer.subarray(0, offset);
90
+ }
91
+ finally {
92
+ await handle.close();
93
+ }
94
+ }
95
+ try {
96
+ if (!Number.isFinite(options.spawnedAt) || !Number.isFinite(options.settledAt) ||
97
+ options.settledAt < options.spawnedAt || !/^[0-9a-f]{64}$/.test(options.promptDigest)) {
98
+ return { reason: "none" };
99
+ }
100
+ const cwd = await realpath(options.cwd);
101
+ const sessions = path.join(options.kimiHome, "sessions");
102
+ // Missing nested dirs mean an incomplete scan; only a missing root is normal.
103
+ try {
104
+ await lstat(sessions);
105
+ }
106
+ catch (error) {
107
+ if (error.code === "ENOENT")
108
+ return { reason: "none" };
109
+ throw error;
110
+ }
111
+ for await (const workspace of directories(sessions)) {
112
+ const workspacePath = path.join(sessions, workspace);
113
+ for await (const session of directories(workspacePath)) {
114
+ if (!SESSION_ID.test(session))
115
+ continue;
116
+ const sessionPath = path.join(workspacePath, session);
117
+ // A directory this run created was modified at or after the spawn.
118
+ if ((await lstat(sessionPath)).mtimeMs < options.spawnedAt - MTIME_SLACK_MS)
119
+ continue;
120
+ const raw = await readBounded(path.join(sessionPath, "state.json"), STATE_MAX_BYTES);
121
+ if (!raw)
122
+ continue;
123
+ let state;
124
+ try {
125
+ state = JSON.parse(raw.toString("utf8"));
126
+ }
127
+ catch {
128
+ continue;
129
+ }
130
+ if (!state || typeof state !== "object")
131
+ continue;
132
+ const value = state;
133
+ // The child writes createdAt after the parent stamped spawnedAt on the
134
+ // same clock, so causality orders them; no pre-spawn slack is justified.
135
+ if (value.version !== 2 || typeof value.cwd !== "string" || !path.isAbsolute(value.cwd) ||
136
+ typeof value.id !== "string" || !SESSION_ID.test(value.id) || value.id !== session ||
137
+ typeof value.createdAt !== "number" || !Number.isFinite(value.createdAt) ||
138
+ value.createdAt < options.spawnedAt || value.createdAt > options.settledAt)
139
+ continue;
140
+ let stateCwd;
141
+ try {
142
+ stateCwd = await realpath(value.cwd);
143
+ }
144
+ catch {
145
+ continue;
146
+ }
147
+ if (stateCwd !== cwd || options.store.hasSessionId(value.id, options.jobId))
148
+ continue;
149
+ const journal = await readBounded(path.join(sessionPath, "agents", "main", "wire.jsonl"), JOURNAL_PREFIX_BYTES, true);
150
+ if (!journal) {
151
+ missingJournals += 1;
152
+ continue;
153
+ }
154
+ // A prefix cut mid-record must never be parsed as a complete line.
155
+ const text = journal.length === JOURNAL_PREFIX_BYTES
156
+ ? journal.subarray(0, journal.lastIndexOf(0x0a) + 1).toString("utf8") : journal.toString("utf8");
157
+ if (!firstUserMessageMatches(text, options.promptDigest)) {
158
+ digestMismatches += 1;
159
+ continue;
160
+ }
161
+ candidates.push(value.id);
162
+ }
163
+ }
164
+ if (candidates.length === 1)
165
+ return { sessionId: candidates[0] };
166
+ if (candidates.length > 1)
167
+ return { reason: `ambiguous:${candidates.length}` };
168
+ if (digestMismatches > 0)
169
+ return { reason: `digest_mismatch:${digestMismatches}` };
170
+ return { reason: missingJournals > 0 ? `journal_missing:${missingJournals}` : "none" };
171
+ }
172
+ catch (error) {
173
+ return { reason: error === capped ? "enumeration_capped" : "unreadable" };
174
+ }
175
+ }
176
+ /** True when the FIRST user message in a native-v2 main journal carries a
177
+ * text item equal to the prompt the job spawned with. Later user turns are
178
+ * never consulted: only the opening prompt binds a session to its spawn. */
179
+ export function firstUserMessageMatches(journal, promptDigest) {
180
+ for (const line of journal.split("\n")) {
181
+ if (!line.trim())
182
+ continue;
183
+ let record;
184
+ try {
185
+ record = JSON.parse(line);
186
+ }
187
+ catch {
188
+ continue;
189
+ }
190
+ if (!record || typeof record !== "object")
191
+ continue;
192
+ const message = record.message;
193
+ if (!message || typeof message !== "object" || message.role !== "user")
194
+ continue;
195
+ const content = message.content;
196
+ if (!Array.isArray(content))
197
+ return false;
198
+ return content.some((item) => item && typeof item === "object" && item.type === "text" &&
199
+ typeof item.text === "string" &&
200
+ digestPrompt(item.text) === promptDigest);
201
+ }
202
+ return false;
203
+ }
@@ -25,39 +25,68 @@
25
25
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
- let DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[T ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[-+]\d{2}:\d{2})?$/i;
28
+ let DATE_TIME_RE = /^(\d{4}-\d{2}-\d{2})?[Tt ]?(?:(\d{2}):\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|z|[-+]\d{2}:\d{2})?$/i;
29
29
  export class TomlDate extends Date {
30
30
  #hasDate = false;
31
31
  #hasTime = false;
32
32
  #offset = null;
33
- constructor(date) {
33
+ constructor(date, fasttype, unsafeDelim) {
34
34
  let hasDate = true;
35
35
  let hasTime = true;
36
36
  let offset = 'Z';
37
+ let c;
37
38
  if (typeof date === 'string') {
38
- let match = date.match(DATE_TIME_RE);
39
- if (match) {
40
- if (!match[1]) {
41
- hasDate = false;
42
- date = `0000-01-01T${date}`;
39
+ if (fasttype)
40
+ prep: {
41
+ // Date-time
42
+ if (fasttype < 3) {
43
+ if (+date.slice(11, 13) > 23) {
44
+ date = '';
45
+ break prep;
46
+ }
47
+ // Local
48
+ if (fasttype === 2) {
49
+ offset = null;
50
+ date += 'Z';
51
+ }
52
+ // Offset; keep track of offset if not Z
53
+ else if ((c = date.charCodeAt(date.length - 1)) !== 0x5a /* Z */ && c !== 0x7a /* z */) {
54
+ offset = date.slice(date.length - 6);
55
+ }
56
+ if (unsafeDelim)
57
+ date = date.slice(0, 10) + 'T' + date.slice(11);
58
+ }
59
+ // Time
60
+ else if (fasttype === 4) {
61
+ date = +date.slice(0, 2) > 23 ? '' : `0000-01-01T${date}Z`;
62
+ }
63
+ hasDate = fasttype !== 4;
64
+ hasTime = fasttype !== 3;
43
65
  }
44
- hasTime = !!match[2];
45
- // Make sure to use T instead of a space. Breaks in case of extreme values otherwise.
46
- hasTime && date[10] === ' ' && (date = date.replace(' ', 'T'));
47
- // Do not allow rollover hours.
48
- if (match[2] && +match[2] > 23) {
49
- date = '';
66
+ else {
67
+ let match = date.match(DATE_TIME_RE);
68
+ if (match) {
69
+ if (!match[1]) {
70
+ hasDate = false;
71
+ date = `0000-01-01T${date}`;
72
+ }
73
+ hasTime = !!match[2];
74
+ // Make sure to use T instead of a space. Breaks in case of extreme values otherwise.
75
+ hasTime && date[10] === ' ' && (date = date.replace(' ', 'T'));
76
+ // Do not allow rollover hours.
77
+ if (match[2] && +match[2] > 23) {
78
+ date = '';
79
+ }
80
+ else {
81
+ offset = match[3] || null;
82
+ if (!offset && hasTime)
83
+ date += 'Z';
84
+ }
50
85
  }
51
86
  else {
52
- offset = match[3] || null;
53
- date = date.toUpperCase();
54
- if (!offset && hasTime)
55
- date += 'Z';
87
+ date = '';
56
88
  }
57
89
  }
58
- else {
59
- date = '';
60
- }
61
90
  }
62
91
  super(date);
63
92
  if (!isNaN(this.getTime())) {
@@ -93,7 +122,7 @@ export class TomlDate extends Date {
93
122
  if (this.#offset === null)
94
123
  return iso.slice(0, -1);
95
124
  // Offset DateTime
96
- if (this.#offset === 'Z')
125
+ if (this.#offset === 'Z' || this.#offset === 'z')
97
126
  return iso;
98
127
  // This part is quite annoying: JS strips the original timezone from the ISO string representation
99
128
  // Instead of using a "modified" date and "Z", we restore the representation "as authored"
@@ -26,11 +26,11 @@
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
  function getLineColFromPtr(string, ptr) {
29
- let lines = string.slice(0, ptr).split(/\r\n|\n|\r/g);
29
+ let lines = string.slice(0, ptr).split(/\r?\n/);
30
30
  return [lines.length, lines.pop().length + 1];
31
31
  }
32
32
  function makeCodeBlock(string, line, column) {
33
- let lines = string.split(/\r\n|\n|\r/g);
33
+ let lines = string.split(/\r?\n/);
34
34
  let codeblock = '';
35
35
  let numberLen = (Math.log10(line + 1) | 0) + 1;
36
36
  for (let i = line - 1; i <= line + 1; i++) {
@@ -60,4 +60,8 @@ export class TomlError extends Error {
60
60
  this.column = column;
61
61
  this.codeblock = codeblock;
62
62
  }
63
+ /** @internal */
64
+ static x(message, ctx, ptr) {
65
+ throw new TomlError(message, { toml: ctx.s, ptr: ptr ?? ctx.p });
66
+ }
63
67
  }
@@ -25,76 +25,269 @@
25
25
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
- import { parseString, parseValue } from './primitive.js';
28
+ import { parseString } from './primitive.js';
29
29
  import { parseArray, parseInlineTable } from './struct.js';
30
- import { skipVoid, skipUntil, skipComment, getStringEnd } from './util.js';
31
30
  import { TomlError } from './error.js';
32
- function sliceAndTrimEndOf(str, startPtr, endPtr) {
33
- let value = str.slice(startPtr, endPtr);
34
- let commentIdx = value.indexOf('#');
35
- if (commentIdx > -1) {
36
- // The call to skipComment allows to "validate" the comment
37
- // (absence of control characters)
38
- skipComment(str, commentIdx);
39
- value = value.slice(0, commentIdx);
40
- }
41
- return [value.trimEnd(), commentIdx];
31
+ import { TomlDate } from './date.js';
32
+ function isDigit(char, base = 10) {
33
+ return base === 16
34
+ ? (char > 0x2f && char < 0x3a) || (char > 0x40 && char < 0x47) || (char > 0x60 && char < 0x67)
35
+ : (char > 0x2f && char < 0x30 + base);
42
36
  }
43
- export function extractValue(str, ptr, end, depth, integersAsBigInt) {
44
- if (depth === 0) {
45
- throw new TomlError('document contains excessively nested structures. aborting.', {
46
- toml: str,
47
- ptr: ptr
48
- });
49
- }
50
- let c = str[ptr];
51
- if (c === '[' || c === '{') {
52
- let [value, endPtr] = c === '['
53
- ? parseArray(str, ptr, depth, integersAsBigInt)
54
- : parseInlineTable(str, ptr, depth, integersAsBigInt);
55
- if (end) {
56
- endPtr = skipVoid(str, endPtr);
57
- if (str[endPtr] === ',')
58
- endPtr++;
59
- else if (str[endPtr] !== end) {
60
- throw new TomlError('expected comma or end of structure', {
61
- toml: str,
62
- ptr: endPtr,
63
- });
64
- }
37
+ function isEndOfValue(char, delim) {
38
+ // Whitespace -- we're permissive on `\r` for performance; it'll be dealt with later anyway
39
+ return char === 0x20 || char === 0x9 /* \t */ || char === 0xa /* \n */ || char === 0xd /* \r */ ||
40
+ // Structure end or next value delimiter
41
+ (delim && (char === delim || char === 0x2c /* , */)) ||
42
+ // Comment
43
+ char === 0x23; /* # */
44
+ }
45
+ /** @internal */
46
+ export function extractValue(ctx, end) {
47
+ let errPtr = ctx.p;
48
+ let c = ctx.s.charCodeAt(ctx.p);
49
+ // Structs
50
+ if (c === 0x5b /* [ */ || c === 0x7b /* { */) {
51
+ ctx.d-- || TomlError.x('document contains excessively nested structures. aborting.', ctx);
52
+ let value = c === 0x5b /* [ */
53
+ ? parseArray(ctx)
54
+ : parseInlineTable(ctx);
55
+ ctx.d++;
56
+ return value;
57
+ }
58
+ // Strings
59
+ if (c === 0x22 /* " */ || c === 0x27 /* ' */) {
60
+ return parseString(ctx);
61
+ }
62
+ // Booleans
63
+ // We can fast-path because the first character is enough to know the only possible value
64
+ if (c === 0x74 /* t */) { // Only possible valid value is `true`
65
+ if (ctx.s.charCodeAt(++ctx.p) !== 0x72 || ctx.s.charCodeAt(++ctx.p) !== 0x75 || ctx.s.charCodeAt(++ctx.p) !== 0x65)
66
+ TomlError.x('invalid value', ctx, errPtr);
67
+ return ctx.p++, true;
68
+ }
69
+ if (c === 0x66 /* f */) { // Only possible valid value is `false`
70
+ if (ctx.s.charCodeAt(++ctx.p) !== 0x61 || ctx.s.charCodeAt(++ctx.p) !== 0x6c || ctx.s.charCodeAt(++ctx.p) !== 0x73 || ctx.s.charCodeAt(++ctx.p) !== 0x65)
71
+ TomlError.x('invalid value', ctx, errPtr);
72
+ return ctx.p++, false;
73
+ }
74
+ if (c === 0x2b /* + */ || c === 0x2d /* - */) {
75
+ return parseNumber(ctx, ctx.p, ctx.s.charCodeAt(++ctx.p), 0x2c - c, end);
76
+ }
77
+ // Rough heuristic, but `parseDate` falls back to number parsing if it's a false positive.
78
+ // e.g.: `a = 1\n-------------- = 1` would match this heuristic, but gracefully fallback to numbers.
79
+ if (ctx.s.charCodeAt(ctx.p + 4) === 0x2d /* - */ && ctx.s.charCodeAt(ctx.p + 7) === 0x2d /* - */) {
80
+ return parseDate(ctx, c, end);
81
+ }
82
+ // Same logic as above; `parseTime` falls back to number parsing if it's a false positive.
83
+ if (ctx.s.charCodeAt(ctx.p + 2) === 0x3a /* : */) {
84
+ return parseTime(ctx, c, end);
85
+ }
86
+ return parseNumber(ctx, ctx.p, c, 0, end);
87
+ }
88
+ // State:
89
+ // 0: init
90
+ // 1: integer
91
+ // 2: fractional dot
92
+ // 3: fractional
93
+ // 4: exponent letter
94
+ // 5: exponent
95
+ // 6: exponent sign
96
+ // 7: exponent with sign
97
+ // >9: underscore; see below
98
+ // -> 12: was integer
99
+ // -> 14: was fractional
100
+ // -> 16: was exponent
101
+ // states 2-5, 14, 16 are ONLY permitted iif base === 10
102
+ function parseNumber(ctx, startPtr, startChr, sign, endChr) {
103
+ let c = startChr;
104
+ let state = 0;
105
+ let hasUnderscores = false;
106
+ // (+/-)inf
107
+ if (c === 0x69 /* i */) {
108
+ if (ctx.s.charCodeAt(++ctx.p) !== 0x6e || ctx.s.charCodeAt(++ctx.p) !== 0x66)
109
+ TomlError.x('invalid value', ctx, startPtr);
110
+ return ctx.p++, (sign || 1) / 0;
111
+ }
112
+ // (+/-)nan
113
+ if (c === 0x6e /* n */) {
114
+ if (ctx.s.charCodeAt(++ctx.p) !== 0x61 || ctx.s.charCodeAt(++ctx.p) !== 0x6e)
115
+ TomlError.x('invalid value', ctx, startPtr);
116
+ return ctx.p++, NaN;
117
+ }
118
+ // Leading zero
119
+ // Only allowed cases: `0<EOV>`, `0.(...)`, `0e(...)`, `0x(...)`, `0b(...)`, `0o(...)`
120
+ // FWIW, `0e(...)` is a stupid case, but it's not banned per-se so we have to parse it
121
+ if (c === 0x30 /* 0 */) {
122
+ if (++ctx.p >= ctx.s.length || isEndOfValue(c = ctx.s.charCodeAt(ctx.p), endChr))
123
+ return ctx.bi === true ? 0n : 0; // note: conveniently deals with `-0`
124
+ if (!sign) {
125
+ if (c === 0x78 /* x */)
126
+ return parseIntegerBaseN(ctx, startPtr, 16, endChr);
127
+ else if (c === 0x62 /* b */)
128
+ return parseIntegerBaseN(ctx, startPtr, 2, endChr);
129
+ else if (c === 0x6f /* o */)
130
+ return parseIntegerBaseN(ctx, startPtr, 8, endChr);
131
+ }
132
+ if (c === 0x2e /* . */)
133
+ state = 2;
134
+ else if (c === 0x65 /* e */ || c === 0x45 /* E */)
135
+ state = 4;
136
+ else
137
+ TomlError.x('illegal leading zero', ctx, startPtr);
138
+ }
139
+ // If the 1st char is not a digit by now, then it's not a valid TOML value at all
140
+ else if (!isDigit(c))
141
+ TomlError.x('invalid value', ctx, startPtr);
142
+ while (++ctx.p < ctx.s.length && (c = ctx.s.charCodeAt(ctx.p), !isEndOfValue(c, endChr))) {
143
+ if (!state)
144
+ state = 1; // Detects single-digit numbers we can use a fast parse path for
145
+ // The way the states are numbered is not random: underscores are always permitted in odd-numbered states and
146
+ // never permitted in even-numbered ones.
147
+ if (c === 0x5f /* _ */) {
148
+ if (!(state & 1))
149
+ TomlError.x('illegal underscore', ctx);
150
+ state += 11; // 11 is a marker that makes the state even and greater than 9; see the numbering + rationale above
151
+ hasUnderscores = true;
152
+ }
153
+ // Transition to fractional part.
154
+ else if (state === 1 && c === 0x2e /* . */)
155
+ state = 2;
156
+ // Transition to exponent part.
157
+ else if ((state === 1 || state === 3) && (c === 0x65 /* e */ || c === 0x45 /* E */))
158
+ state = 4;
159
+ // + and - are permitted in state 4 only (handled before entering the function for state 0)
160
+ else if (state === 4 && (c === 0x2b /* + */ || c === 0x2d /* - */)) {
161
+ state = 6;
162
+ }
163
+ // All special cases have been handled; only digits are allowed here
164
+ else if (!isDigit(c))
165
+ TomlError.x(`illegal character in numeric literal`, ctx);
166
+ // Clear state flags
167
+ else if (state > 9)
168
+ state -= 11;
169
+ else if (!(state & 1))
170
+ state++;
171
+ }
172
+ // Single-char number; we can fast-path these very easily.
173
+ if (!state) {
174
+ let val = (startChr - 0x30 /* 0 */) * (sign || 1);
175
+ return ctx.bi === true ? BigInt(val) : val;
176
+ }
177
+ // Even-numbered states absolutely require a digit next; not even end of value is permitted
178
+ if (!(state & 1))
179
+ TomlError.x('unfinished numeric value', ctx, startPtr);
180
+ let str = ctx.s.slice(startPtr, ctx.p);
181
+ if (hasUnderscores)
182
+ str = str.replaceAll('_', ''); // perf: replaceAll 1.25x faster than replace with a regex
183
+ return state > 1
184
+ ? parseFloat(str)
185
+ : parseInteger(ctx, str, 10, startPtr);
186
+ }
187
+ function parseIntegerBaseN(ctx, startPtr, base, endChr) {
188
+ let c, underscore = 1;
189
+ while (++ctx.p < ctx.s.length && (c = ctx.s.charCodeAt(ctx.p), !isEndOfValue(c, endChr))) {
190
+ if (c === 0x5f /* _ */) {
191
+ if (underscore & 1)
192
+ TomlError.x('illegal underscore', ctx);
193
+ underscore = 3;
65
194
  }
66
- return [value, endPtr];
67
- }
68
- let endPtr;
69
- if (c === '"' || c === "'") {
70
- endPtr = getStringEnd(str, ptr);
71
- let parsed = parseString(str, ptr, endPtr);
72
- if (end) {
73
- endPtr = skipVoid(str, endPtr);
74
- if (str[endPtr] && str[endPtr] !== ',' && str[endPtr] !== end && str[endPtr] !== '\n' && str[endPtr] !== '\r') {
75
- throw new TomlError('unexpected character encountered', {
76
- toml: str,
77
- ptr: endPtr,
78
- });
195
+ // We only need to check if the number is a valid digit, nothing else is permitted
196
+ else if (!isDigit(c, base))
197
+ TomlError.x(`illegal character in numeric literal`, ctx);
198
+ // Clear underscore flag
199
+ else if (underscore & 1)
200
+ underscore--;
201
+ }
202
+ // Trailing underscore is not allowed
203
+ if (underscore & 1)
204
+ TomlError.x('unfinished numeric value', ctx);
205
+ let str = ctx.s.slice(startPtr + 2, ctx.p);
206
+ if (underscore)
207
+ str = str.replaceAll('_', ''); // perf: replaceAll 1.25x faster than replace with a regex
208
+ return parseInteger(ctx, str, base, startPtr);
209
+ }
210
+ function parseInteger(ctx, str, base, startPtr) {
211
+ if (ctx.bi !== true)
212
+ int: {
213
+ let val = parseInt(str, base);
214
+ if (!Number.isSafeInteger(val)) {
215
+ if (ctx.bi)
216
+ break int;
217
+ TomlError.x('integer value cannot be represented losslessly', ctx, startPtr);
79
218
  }
80
- endPtr += (+(str[endPtr] === ','));
219
+ return val;
220
+ }
221
+ return base === 10 ? BigInt(str) : BigInt((base === 2 ? '0b' : base === 8 ? '0o' : '0x') + str);
222
+ }
223
+ function parseDate(ctx, c, endChr) {
224
+ let startPtr = ctx.p++, unsafeSeparator;
225
+ if (!isDigit(c) ||
226
+ !isDigit(ctx.s.charCodeAt(ctx.p++)) ||
227
+ !isDigit(ctx.s.charCodeAt(ctx.p++)) ||
228
+ !isDigit(ctx.s.charCodeAt(ctx.p++))) {
229
+ return parseNumber(ctx, ctx.p = startPtr, c, 0, endChr);
230
+ }
231
+ ctx.p += 5;
232
+ if (!isDigit(ctx.s.charCodeAt(ctx.p++)))
233
+ TomlError.x('invalid date-time: date part is malformed', ctx, startPtr);
234
+ if (ctx.p >= ctx.s.length || (((c = ctx.s.charCodeAt(ctx.p)) !== 0x20 || (unsafeSeparator = true, !isDigit(ctx.s.charCodeAt(ctx.p + 1)))) && c !== 0x54 /* T */ && c !== 0x74 /* t */)) {
235
+ let t = ctx.s.slice(startPtr, ctx.p);
236
+ return readDate(ctx, t, 3, false, startPtr);
237
+ }
238
+ if (ctx.s.charCodeAt(ctx.p += 3) !== 0x3a /* : */)
239
+ TomlError.x('invalid date-time: time part is malformed', ctx, startPtr);
240
+ if (ctx.s.charCodeAt(ctx.p += 3) === 0x3a /* : */)
241
+ ctx.p += 3;
242
+ if (ctx.s.charCodeAt(ctx.p) === 0x2e /* . */)
243
+ while (isDigit(ctx.s.charCodeAt(++ctx.p)))
244
+ ;
245
+ if (c = ctx.s.charCodeAt(ctx.p)) {
246
+ if (c === 0x5a /* Z */ || c === 0x7a /* z */) {
247
+ let t = ctx.s.slice(startPtr, ++ctx.p);
248
+ return readDate(ctx, t, 1, unsafeSeparator, startPtr, '[+00:00]');
249
+ }
250
+ if (c === 0x2b /* + */ || c === 0x2d /* - */) {
251
+ // Temporal's ZonedDateTime is weird asf when it comes to dealing with traditional offsets...
252
+ // It's 1.2x faster to allocate a new string to pass to ZDT than use Instant.toZonedDateTimeISO
253
+ let t = ctx.s.slice(startPtr, ctx.p += 6);
254
+ return readDate(ctx, t, 1, unsafeSeparator, startPtr, !ctx.ld && ('[' + ctx.s.slice(ctx.p - 6, ctx.p) + ']'));
81
255
  }
82
- return [parsed, endPtr];
83
- }
84
- endPtr = skipUntil(str, ptr, ',', end);
85
- let slice = sliceAndTrimEndOf(str, ptr, endPtr - (+(str[endPtr - 1] === ',')));
86
- if (!slice[0]) {
87
- throw new TomlError('incomplete key-value declaration: no value specified', {
88
- toml: str,
89
- ptr: ptr
90
- });
91
- }
92
- if (end && slice[1] > -1) {
93
- endPtr = skipVoid(str, ptr + slice[1]);
94
- endPtr += +(str[endPtr] === ',');
95
- }
96
- return [
97
- parseValue(slice[0], str, ptr, integersAsBigInt),
98
- endPtr,
99
- ];
256
+ }
257
+ let t = ctx.s.slice(startPtr, ctx.p);
258
+ return readDate(ctx, t, 2, unsafeSeparator, startPtr);
259
+ }
260
+ function parseTime(ctx, c, endChr) {
261
+ let start = ctx.p;
262
+ if (!isDigit(c) || !isDigit(ctx.s.charCodeAt(++ctx.p))) {
263
+ return parseNumber(ctx, --ctx.p, c, 0, endChr);
264
+ }
265
+ if (ctx.s.charCodeAt(ctx.p += 4) === 0x3a /* : */)
266
+ ctx.p += 3;
267
+ if (ctx.s.charCodeAt(ctx.p) === 0x2e /* . */)
268
+ while (isDigit(ctx.s.charCodeAt(++ctx.p)))
269
+ ;
270
+ let t = ctx.s.slice(start, ctx.p);
271
+ return readDate(ctx, t, 4, false, start);
272
+ }
273
+ function readDate(ctx, str, type, unsafeDelim, errPtr, temporalSuffix) {
274
+ if (ctx.ld) {
275
+ let date = new TomlDate(str, type, unsafeDelim);
276
+ if (!date.isValid())
277
+ TomlError.x('invalid date', ctx, errPtr);
278
+ return date;
279
+ }
280
+ try {
281
+ if (temporalSuffix)
282
+ str += temporalSuffix;
283
+ switch (type) {
284
+ case 1: return Temporal.ZonedDateTime.from(str);
285
+ case 2: return Temporal.PlainDateTime.from(str);
286
+ case 3: return Temporal.PlainDate.from(str);
287
+ case 4: return Temporal.PlainTime.from(str);
288
+ }
289
+ }
290
+ catch (e) {
291
+ TomlError.x(e instanceof Error ? e.message : '' + e, ctx, errPtr);
292
+ }
100
293
  }