@tiens.nguyen/gonext-local-worker 1.0.255 → 1.0.256

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.
@@ -3890,22 +3890,49 @@ def run_agent_chat(cfg):
3890
3890
  except Exception: # noqa: BLE001
3891
3891
  pass
3892
3892
 
3893
- # (B) Trim OLD, LARGE tool observations out of the CodeAgent's step memory so
3894
- # they aren't re-sent to the model on every subsequent step (the #63 O(n²) token
3895
- # blow-up: an uncapped read_file_lines dump cost ~5-6k tokens PER remaining step).
3896
- # Keep the last 2 steps' observations intact (the model just acted on them);
3897
- # replace older long ones with a short placeholder the model can re-read.
3893
+ # (B) Trim OLD, LARGE step content out of the CodeAgent's step memory so it isn't
3894
+ # re-sent to the model on every subsequent step (the #63 O(n²) token blow-up: an
3895
+ # uncapped dump cost ~5-6k tokens PER remaining step). Keep the last 2 steps intact
3896
+ # (the model just acted on them); shrink older long content. write_memory_to_messages
3897
+ # rebuilds the prompt from memory.steps on EVERY request (no cache), so mutating the
3898
+ # step objects here takes effect on the next request; old steps are only rendered
3899
+ # into history, never re-parsed for execution, so trimming them is safe.
3900
+ # - observations = the tool's OUTPUT (a file/dir dump). Trimmed since #63.
3901
+ # - model_output = the model's own Thought + <code> block (task #87). For an OLD
3902
+ # step the code ALREADY ran and its result is in the observation, so re-sending
3903
+ # the code verbatim every step is pure waste — keep the Thought, drop the code.
3904
+ # Both trims are IDEMPOTENT (a re-trimmed field falls under the size threshold, so a
3905
+ # later step_callback skips it — the counter below only logs NEW trims).
3898
3906
  try:
3899
3907
  _mem = getattr(_agent_ref.get("a"), "memory", None)
3900
3908
  _steps = getattr(_mem, "steps", None)
3901
3909
  if _steps and len(_steps) > 2:
3910
+ _trim_n, _trim_saved = 0, 0
3902
3911
  for _st in _steps[:-2]:
3903
3912
  _obs = getattr(_st, "observations", None)
3904
3913
  if isinstance(_obs, str) and len(_obs) > 900:
3914
+ _before = len(_obs)
3905
3915
  _st.observations = (
3906
3916
  _obs[:400].rstrip()
3907
3917
  + "\n…[earlier observation trimmed to save context — "
3908
3918
  "re-read the file/dir with a small range if you still need it]")
3919
+ _trim_n += 1
3920
+ _trim_saved += _before - len(_st.observations)
3921
+ _mo = getattr(_st, "model_output", None)
3922
+ if isinstance(_mo, str) and len(_mo) > 700:
3923
+ _before = len(_mo)
3924
+ # Keep the Thought (everything before the code block); the code is
3925
+ # redundant with the observation that already came back.
3926
+ _head = re.split(r"<code[\s>]|```", _mo, maxsplit=1)[0].rstrip()
3927
+ _kept = (_head[:500].rstrip() if _head else _mo[:300].rstrip())
3928
+ _new = _kept + "\n…[code trimmed to save context — its result is in the Observation]"
3929
+ if len(_new) < _before:
3930
+ _st.model_output = _new
3931
+ _trim_n += 1
3932
+ _trim_saved += _before - len(_new)
3933
+ if _trim_n:
3934
+ _log(f"context trim: shrank {_trim_n} old step field(s), "
3935
+ f"saved ~{_trim_saved} chars (#63/#87)")
3909
3936
  except Exception as _e: # noqa: BLE001
3910
3937
  _log(f"memory-trim skip: {_e}")
3911
3938
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiens.nguyen/gonext-local-worker",
3
- "version": "1.0.255",
3
+ "version": "1.0.256",
4
4
  "description": "Polls GoNext cloud API for async local LLM jobs and runs them against Ollama/OpenAI-compatible servers on this Mac",
5
5
  "type": "module",
6
6
  "license": "MIT",