-
Notifications
You must be signed in to change notification settings - Fork 0
test json watch #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mikkel
wants to merge
4
commits into
master
Choose a base branch
from
task/500-test-json-watch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
88583db
task/500: test json watch
5571d0e
fix: address Codex review — Preserve text buffer when no JSON object …
3e36469
fix: address Codex review — Do not consume following lines after a fa…
ec6fa0c
fix: address review — Preserve text buffer when no JSON object is parsed
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /** | ||
| * Watch mode parsers — extracted for testability. | ||
| * | ||
| * parseJsonObjects(buffer) - Extract JSON objects from a raw stdout buffer. | ||
| * parseHumanReadable(buffer) - Fallback: extract task.created events from | ||
| * human-readable ATS watch output. | ||
| */ | ||
|
|
||
| /** | ||
| * Attempt to extract one or more complete JSON objects from `buffer`. | ||
| * | ||
| * Returns { events: [ parsed objects ], remaining: unprocessed buffer tail }. | ||
| */ | ||
| export function parseJsonObjects(buffer) { | ||
| const events = []; | ||
| let pos = 0; | ||
|
|
||
| while (true) { | ||
| const startIdx = buffer.indexOf('{', pos); | ||
| if (startIdx === -1) break; | ||
|
|
||
| let depth = 0; | ||
| let endIdx = -1; | ||
| for (let i = startIdx; i < buffer.length; i++) { | ||
| if (buffer[i] === '{') depth++; | ||
| else if (buffer[i] === '}') { | ||
| depth--; | ||
| if (depth === 0) { | ||
| endIdx = i; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (endIdx === -1) { | ||
| // Incomplete JSON — return everything from startIdx onward as remaining | ||
| return { events, remaining: buffer.slice(startIdx) }; | ||
| } | ||
|
|
||
| const jsonStr = buffer.slice(startIdx, endIdx + 1); | ||
| pos = endIdx + 1; | ||
|
|
||
| try { | ||
| events.push(JSON.parse(jsonStr)); | ||
| } catch { | ||
| // Not valid JSON, skip this candidate and keep scanning | ||
| } | ||
| } | ||
|
|
||
| // Everything was consumed (or no braces found at all) | ||
| return { events, remaining: buffer.slice(pos) }; | ||
| } | ||
|
|
||
| /** | ||
| * Parse human-readable ATS watch output. | ||
| * | ||
| * Looks for lines containing "task.created" followed by a line matching | ||
| * "Task #<id>: <title>". | ||
| * | ||
| * Returns { events: [ { task_id, title, event } ], remaining: unprocessed tail }. | ||
| */ | ||
| export function parseHumanReadable(buffer) { | ||
| const events = []; | ||
| const lines = buffer.split('\n'); | ||
| const consumedIndices = new Set(); | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| if (consumedIndices.has(i)) continue; | ||
| if (/task\.created/.test(lines[i])) { | ||
| const window = lines.slice(i, i + 3).join('\n'); | ||
| const taskMatch = window.match(/Task #(\d+):\s*(.+)/); | ||
| if (taskMatch) { | ||
| events.push({ | ||
| task_id: parseInt(taskMatch[1], 10), | ||
| title: taskMatch[2].trim(), | ||
| event: 'task.created', | ||
| }); | ||
| consumedIndices.add(i); | ||
| consumedIndices.add(i + 1); | ||
| consumedIndices.add(i + 2); | ||
|
mikkel marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| if (consumedIndices.size === 0) { | ||
| return { events, remaining: buffer }; | ||
| } | ||
|
|
||
| const lastConsumed = Math.max(...consumedIndices); | ||
| const remaining = lines.slice(lastConsumed + 1).join('\n'); | ||
| return { events, remaining }; | ||
| } | ||
|
|
||
| /** | ||
| * Trim an oversized buffer to avoid unbounded memory growth. | ||
| * Keeps content from the last '{' onward, or clears the buffer entirely. | ||
| */ | ||
| export function trimBuffer(buffer, maxLength = 10000) { | ||
| if (buffer.length <= maxLength) return buffer; | ||
| const lastBrace = buffer.lastIndexOf('{'); | ||
| return lastBrace > 0 ? buffer.slice(lastBrace) : ''; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.