Skip to content

fix(read): support negative offsets counting from end of file#39

Open
T0mSIlver wants to merge 1 commit into
microsoft:mainfrom
T0mSIlver:fix/read-negative-offset
Open

fix(read): support negative offsets counting from end of file#39
T0mSIlver wants to merge 1 commit into
microsoft:mainfrom
T0mSIlver:fix/read-negative-offset

Conversation

@T0mSIlver

Copy link
Copy Markdown

Problem

The FastContext paper specifies that Read's offset parameter supports negative values:

offset (integer, optional): The line number to start reading from. Positive values are 1-indexed from the start of the file. Negative values count backwards from the end of the file.
FastContext paper (arXiv:2606.14066), Appendix E "FastContext Explorer: Tool Schemas", p. 19

The implementation does the opposite — it rejects them:

if not isinstance(offset, int) or offset <= 0:
    return "...Error: offset must be a positive integer..."

…and the shipped schema description omits the "Negative values count backwards from the end" sentence. So a documented capability (read the last N lines via a negative offset) is both unimplemented and undocumented.

Fix

  • Resolve a negative offset to a 1-indexed line from the end (len(lines) + offset + 1), clamping over-long negatives to line 1.
  • Reject only offset == 0 (and booleans) instead of all non-positive values.
  • Restore the "Negative values count backwards from the end of the file" sentence in the offset schema description, matching the paper.
  • Add tests/test_read_negative_offset.py covering -2, -1 with a limit, over-long negatives (clamp), and 0 (rejected).

Note

This issue was surfaced by cross-checking the implementation against the paper's canonical tool schema (p. 19); it is distinct from the earlier prompt/code mismatches.

The FastContext paper (Appendix E, p. 19) specifies that Read's offset
'Negative values count backwards from the end of the file', but the
implementation rejected any offset <= 0 and the schema description
dropped the sentence. Resolve negative offsets to a 1-indexed line from
the end (clamping over-long negatives to the start), restore the
documented behavior in the schema, and add a regression test.
@T0mSIlver T0mSIlver closed this Jun 27, 2026
@T0mSIlver T0mSIlver deleted the fix/read-negative-offset branch June 27, 2026 11:32
@T0mSIlver T0mSIlver restored the fix/read-negative-offset branch June 27, 2026 11:56
@T0mSIlver T0mSIlver reopened this Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant