thinqos 3.1.2__tar.gz → 3.2.0__tar.gz

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 (136) hide show
  1. {thinqos-3.1.2 → thinqos-3.2.0}/CHANGELOG.md +14 -0
  2. {thinqos-3.1.2 → thinqos-3.2.0}/PKG-INFO +1 -1
  3. {thinqos-3.1.2 → thinqos-3.2.0}/pyproject.toml +1 -1
  4. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/client.py +5 -0
  5. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/hook.py +160 -22
  6. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/primecache.py +16 -1
  7. thinqos-3.2.0/src/thinqos_capture/questionrecall.py +330 -0
  8. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/tool_turns.py +4 -4
  9. thinqos-3.2.0/tests/test_client_limits.py +129 -0
  10. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_codex_hook_runtime_contract.py +10 -6
  11. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_hook_prime.py +10 -5
  12. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tool_turns.py +2 -2
  13. thinqos-3.2.0/tests/test_tos_4486_question_recall.py +504 -0
  14. thinqos-3.1.2/tests/test_client_limits.py +0 -53
  15. {thinqos-3.1.2 → thinqos-3.2.0}/.gitignore +0 -0
  16. {thinqos-3.1.2 → thinqos-3.2.0}/README.md +0 -0
  17. {thinqos-3.1.2 → thinqos-3.2.0}/scripts/sync_cognitive_contract.py +0 -0
  18. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/__init__.py +0 -0
  19. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/__main__.py +0 -0
  20. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/accountscope.py +0 -0
  21. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/__init__.py +0 -0
  22. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/claude_code.py +0 -0
  23. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/cline.py +0 -0
  24. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/cline_tasks.py +0 -0
  25. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/codex.py +0 -0
  26. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/grok_build.py +0 -0
  27. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/vscode_chat_store.py +0 -0
  28. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/adapters/vscode_copilot.py +0 -0
  29. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/attribution.py +0 -0
  30. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/capture_history_policy.py +0 -0
  31. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/cli.py +0 -0
  32. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/cline_sweep.py +0 -0
  33. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/connections.py +0 -0
  34. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/denylist.py +0 -0
  35. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/discovery.py +0 -0
  36. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/filelock.py +0 -0
  37. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/importer.py +0 -0
  38. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/incremental.py +0 -0
  39. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/install_marker.py +0 -0
  40. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/installs.py +0 -0
  41. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/logsetup.py +0 -0
  42. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/mcp_transport.py +0 -0
  43. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/process.py +0 -0
  44. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/recovery.py +0 -0
  45. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/scheduler.py +0 -0
  46. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/secretstore.py +0 -0
  47. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/selfupdate.py +0 -0
  48. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/server_revision.py +0 -0
  49. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/shim.py +0 -0
  50. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/state.py +0 -0
  51. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/taskstate.py +0 -0
  52. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/tls.py +0 -0
  53. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/vscode_chat_sweep.py +0 -0
  54. {thinqos-3.1.2 → thinqos-3.2.0}/src/thinqos_capture/writeredirect.py +0 -0
  55. {thinqos-3.1.2 → thinqos-3.2.0}/tests/conftest.py +0 -0
  56. {thinqos-3.1.2 → thinqos-3.2.0}/tests/fixtures/claude_code_session.jsonl +0 -0
  57. {thinqos-3.1.2 → thinqos-3.2.0}/tests/fixtures/codex_session.jsonl +0 -0
  58. {thinqos-3.1.2 → thinqos-3.2.0}/tests/fixtures/grok_build_summary.json +0 -0
  59. {thinqos-3.1.2 → thinqos-3.2.0}/tests/fixtures/grok_build_updates.jsonl +0 -0
  60. {thinqos-3.1.2 → thinqos-3.2.0}/tests/fixtures/tos_6776_recovery_failures.json +0 -0
  61. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_attribution.py +0 -0
  62. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_capture_completeness.py +0 -0
  63. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_capture_incremental.py +0 -0
  64. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_claude_code_adapter.py +0 -0
  65. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cli_drain.py +0 -0
  66. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cli_install_hook.py +0 -0
  67. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cli_list_forget.py +0 -0
  68. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cli_run.py +0 -0
  69. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_client_version_header.py +0 -0
  70. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cline_adapter.py +0 -0
  71. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cline_mcp_registration.py +0 -0
  72. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cline_sweep.py +0 -0
  73. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_cline_tasks.py +0 -0
  74. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_codex_adapter.py +0 -0
  75. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_codex_capture_staleness.py +0 -0
  76. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_codex_hook_trust.py +0 -0
  77. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_codex_plugin_setup.py +0 -0
  78. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_codex_rollout_memory.py +0 -0
  79. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_doctor.py +0 -0
  80. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_doctor_queue.py +0 -0
  81. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_drain_partition_split.py +0 -0
  82. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_drain_poison.py +0 -0
  83. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_drain_timeout.py +0 -0
  84. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_env_key_remnant.py +0 -0
  85. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_grok_build_adapter.py +0 -0
  86. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_hook.py +0 -0
  87. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_hook_autodrain.py +0 -0
  88. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_hook_secret_resolution.py +0 -0
  89. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_hook_self_update.py +0 -0
  90. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_hook_stdin.py +0 -0
  91. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_hook_task_state.py +0 -0
  92. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_importer.py +0 -0
  93. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_incremental.py +0 -0
  94. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_install.py +0 -0
  95. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_installer_hardening.py +0 -0
  96. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_liveness.py +0 -0
  97. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_logsetup.py +0 -0
  98. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_mcp_transport.py +0 -0
  99. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_no_retired_tool_names.py +0 -0
  100. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_plugin_autoupdate.py +0 -0
  101. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_plugin_mode.py +0 -0
  102. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_prime_cache.py +0 -0
  103. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_process.py +0 -0
  104. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_queue_policy.py +0 -0
  105. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_scheduler.py +0 -0
  106. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_secretstore.py +0 -0
  107. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_selfupdate.py +0 -0
  108. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_shim.py +0 -0
  109. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_state.py +0 -0
  110. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_state_migration.py +0 -0
  111. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_taskstate.py +0 -0
  112. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tls_cloud_proxy.py +0 -0
  113. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_token_economy.py +0 -0
  114. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_2840_completion.py +0 -0
  115. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_2840_repair_round1.py +0 -0
  116. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_2840_repair_round2.py +0 -0
  117. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_4483_account_scope.py +0 -0
  118. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_4485_capture_history_policy.py +0 -0
  119. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_4485_doctor_update_state.py +0 -0
  120. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_4485_prime_refresh_coalescing.py +0 -0
  121. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_4487_install_marker.py +0 -0
  122. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_5043_connections.py +0 -0
  123. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_5043_discovery.py +0 -0
  124. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_5043_install_precedence.py +0 -0
  125. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_5100_key_prompt_links.py +0 -0
  126. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_5102_selfheal.py +0 -0
  127. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_5226_installs_registry.py +0 -0
  128. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_5369_mcp_revision.py +0 -0
  129. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_6776_recovery.py +0 -0
  130. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_6807_plugin_install.py +0 -0
  131. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_tos_7410_prime_refusals.py +0 -0
  132. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_verify.py +0 -0
  133. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_vscode_chat_store.py +0 -0
  134. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_vscode_copilot_adapter.py +0 -0
  135. {thinqos-3.1.2 → thinqos-3.2.0}/tests/test_writeredirect.py +0 -0
  136. {thinqos-3.1.2 → thinqos-3.2.0}/uv.lock +0 -0
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [3.2.0](https://github.com/AI4Outcomes/thinqos/compare/tools/thinqos-v3.1.3...tools/thinqos-v3.2.0) (2026-10-08)
4
+
5
+
6
+ ### Features
7
+
8
+ * **TOS-4486:** CLI hook adds a per-prompt question recall and never caches a degraded prime ([#8220](https://github.com/AI4Outcomes/thinqos/issues/8220)) ([7520abf](https://github.com/AI4Outcomes/thinqos/commit/7520abf888b24590b5f68fef0039e3d0277d82ff))
9
+
10
+ ## [3.1.3](https://github.com/AI4Outcomes/thinqos/compare/tools/thinqos-v3.1.2...tools/thinqos-v3.1.3) (2026-10-07)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * filter native capture tool traffic before size validation ([#8156](https://github.com/AI4Outcomes/thinqos/issues/8156)) ([d12e256](https://github.com/AI4Outcomes/thinqos/commit/d12e256ed06d42c254caf0b852011d9759522a81))
16
+
3
17
  ## [3.1.2](https://github.com/AI4Outcomes/thinqos/compare/tools/thinqos-v3.1.1...tools/thinqos-v3.1.2) (2026-10-06)
4
18
 
5
19
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: thinqos
3
- Version: 3.1.2
3
+ Version: 3.2.0
4
4
  Summary: Connect Claude Code, Codex, Grok Build, VS Code, and Cline to your thinqOS Mind.
5
5
  Project-URL: Homepage, https://thinqos.com
6
6
  Project-URL: Documentation, https://thinqos.com/mcp-memory-server
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "thinqos"
3
- version = "3.1.2"
3
+ version = "3.2.0"
4
4
  description = "Connect Claude Code, Codex, Grok Build, VS Code, and Cline to your thinqOS Mind."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.13"
@@ -80,6 +80,11 @@ class ThinqosClient:
80
80
  self.last_expired_count = 0
81
81
 
82
82
  def post_envelope(self, source: str, sessions: list[dict[str, Any]]) -> dict[str, Any]:
83
+ # Live hooks must use the same conversation snapshot as queued retries.
84
+ # Filter tool traffic before the finite transport limits are checked.
85
+ sessions, dropped = strip_tool_turns_from_sessions(sessions, source=source)
86
+ if dropped:
87
+ logger.info("post.tool_turns_stripped source=%s dropped=%d", source, dropped)
83
88
  envelope = {"source": source, "sessions": sessions}
84
89
  _validate_v1_envelope(envelope)
85
90
  return self._post_with_equal_revision_recovery(envelope)
@@ -75,6 +75,19 @@ from thinqos_capture.primecache import (
75
75
  spawn_background_refresh,
76
76
  )
77
77
  from thinqos_capture.process import spawn_detached
78
+ from thinqos_capture.questionrecall import (
79
+ QUESTION_RECALL_LIMIT,
80
+ SKIPPED_SLOW,
81
+ QuestionRecall,
82
+ leading_json_object,
83
+ log_question_recall,
84
+ question_from_prompt,
85
+ question_layer_cooling_down,
86
+ question_recall_budget_seconds,
87
+ question_recall_enabled,
88
+ recall_degradation,
89
+ relevant_section,
90
+ )
78
91
  from thinqos_capture.secretstore import (
79
92
  DEFAULT_BASE_URL,
80
93
  read_active_connection,
@@ -252,18 +265,27 @@ def _call_mcp_tool(
252
265
  return result if isinstance(result, dict) else {}
253
266
 
254
267
 
255
- def _prime_session_from_stdin() -> str:
256
- """Use the host's real session, never a generated per-request discriminator."""
268
+ def _prime_payload_from_stdin() -> tuple[str, str]:
269
+ """The host's real session id and the user's prompt from the hook's stdin JSON.
270
+
271
+ The session id is the host's own, never a generated per-request
272
+ discriminator. The prompt is present on UserPromptSubmit (Claude Code and
273
+ Codex both send ``prompt``) and absent on SessionStart.
274
+ """
257
275
  if sys.stdin.isatty():
258
- return ""
276
+ return "", ""
259
277
  try:
260
278
  payload = json.loads(sys.stdin.read())
261
279
  except (OSError, ValueError):
262
- return ""
280
+ return "", ""
263
281
  if not isinstance(payload, dict):
264
- return ""
265
- value = payload.get("session_id") or payload.get("sessionId")
266
- return value if isinstance(value, str) else ""
282
+ return "", ""
283
+ session = payload.get("session_id") or payload.get("sessionId")
284
+ prompt = payload.get("prompt") or payload.get("user_prompt")
285
+ return (
286
+ session if isinstance(session, str) else "",
287
+ prompt if isinstance(prompt, str) else "",
288
+ )
267
289
 
268
290
 
269
291
  def _call_cli_rest(
@@ -1923,19 +1945,75 @@ def _fetch_memory_block(
1923
1945
  timeout=timeout,
1924
1946
  session_id=session_id,
1925
1947
  )
1926
- block = _prime_block_from_recall_result(result, limit=limit)
1927
- structured = result.get("structuredContent")
1928
- if not block and isinstance(structured, dict):
1929
- block = str(structured.get("context", ""))
1948
+ structured = _recall_structured(result)
1949
+ if structured is None:
1950
+ # No structured result and text that is not a JSON object: a server
1951
+ # older than the catalog output schema. The text is the context.
1952
+ return _recall_text(result).strip()
1953
+ # TOS-4486: a degraded response (deadline-cut prime, Mind leg refused) is
1954
+ # not context. Raising keeps it out of the cache and out of the output;
1955
+ # the caller falls back exactly as for a timeout.
1956
+ reason = recall_degradation(structured)
1957
+ if reason:
1958
+ raise _DegradedRecall(reason)
1959
+ block = _prime_block_from_recall_result({"structuredContent": structured}, limit=limit)
1930
1960
  if not block:
1931
- content = result.get("content", [])
1932
- if isinstance(content, list):
1933
- for chunk in content:
1934
- if isinstance(chunk, dict) and chunk.get("type") == "text":
1935
- block += str(chunk.get("text", ""))
1961
+ block = str(structured.get("context", ""))
1936
1962
  return block.strip()
1937
1963
 
1938
1964
 
1965
+ class _DegradedRecall(RuntimeError):
1966
+ """The server answered, but said its Mind read was cut short or refused."""
1967
+
1968
+
1969
+ def _recall_text(result: dict[str, object]) -> str:
1970
+ content = result.get("content", [])
1971
+ if not isinstance(content, list):
1972
+ return ""
1973
+ return "".join(
1974
+ str(chunk.get("text", ""))
1975
+ for chunk in content
1976
+ if isinstance(chunk, dict) and chunk.get("type") == "text"
1977
+ )
1978
+
1979
+
1980
+ def _recall_structured(result: dict[str, object]) -> dict[str, object] | None:
1981
+ """The recall's structured result, from ``structuredContent`` or its JSON text.
1982
+
1983
+ The text content of a structured tool result is the JSON of the same
1984
+ object. Before TOS-4486 that JSON was treated as Mind context whenever the
1985
+ structured context was empty, which is how a degraded prime got cached.
1986
+ """
1987
+ structured = result.get("structuredContent")
1988
+ if isinstance(structured, dict):
1989
+ return structured
1990
+ return leading_json_object(_recall_text(result))
1991
+
1992
+
1993
+ def _fetch_question_context(
1994
+ base_url: str, token: str, question: str, timeout: float, *, session_id: str = ""
1995
+ ) -> dict[str, object]:
1996
+ """One ``recall depth=normal`` for the user's prompt; returns the structured result.
1997
+
1998
+ ``intent`` is always sent: an omitted intent makes the server run a model
1999
+ call to guess it (about 6.6 s median, TOS-4486 performance catalog item 1).
2000
+ """
2001
+ result = _call_mcp_tool(
2002
+ base_url,
2003
+ token,
2004
+ "recall",
2005
+ {
2006
+ "query": question,
2007
+ "depth": "normal",
2008
+ "intent": "general",
2009
+ "limit": QUESTION_RECALL_LIMIT,
2010
+ },
2011
+ timeout=timeout,
2012
+ session_id=session_id,
2013
+ )
2014
+ return _recall_structured(result) or {"context": _recall_text(result)}
2015
+
2016
+
1939
2017
  @hook_group.command("prime")
1940
2018
  @click.option(
1941
2019
  "--query",
@@ -1969,6 +2047,13 @@ def prime(query: str, limit: int) -> None:
1969
2047
  recall is the on-demand Query leg (``recall`` at depth=normal/deep), not
1970
2048
  the reflexive prime. An explicit ``--query`` is still honored for manual use.
1971
2049
 
2050
+ TOS-4486 Phase 6 C: the prompt drives a separate question layer instead
2051
+ (see ``questionrecall``): one uncached ``recall depth=normal`` started at
2052
+ hook entry, overlapped with the prime, joined within
2053
+ ``THINQOS_QUESTION_RECALL_BUDGET_S`` (default 2.0 s), and appended as a
2054
+ "Relevant to this prompt" block. Any miss leaves the prime alone. Disable
2055
+ with ``THINQOS_QUESTION_RECALL=off``.
2056
+
1972
2057
  Latency budget + local cache: a FRESH cache hit for this exact query is
1973
2058
  served instantly with zero network calls, then a detached background
1974
2059
  process refreshes the cache for next time. A cache miss makes the live
@@ -1995,6 +2080,7 @@ def prime(query: str, limit: int) -> None:
1995
2080
  Fail-open in every branch: worst case prints warning lines + the
1996
2081
  write-redirect and exits 0, so it never blocks a turn.
1997
2082
  """
2083
+ hook_started = time.monotonic()
1998
2084
  # TOS-5226 upgrade path, trigger 3 of 3 (hook prime). Idempotent and gated
1999
2085
  # on `layout_version`, so after the first success this is one file read.
2000
2086
  # It cannot raise and cannot block the turn - that is the whole contract.
@@ -2004,13 +2090,12 @@ def prime(query: str, limit: int) -> None:
2004
2090
  sys.exit(0)
2005
2091
  account = _account_scope(DEFAULT_STATE_DIR, base_url, token)
2006
2092
  cache_dir = account.state_dir
2007
- session_id = _prime_session_from_stdin()
2093
+ session_id, prompt = _prime_payload_from_stdin()
2008
2094
 
2009
2095
  # TOS-1505: the reflexive prime is query-independent (salient stable packet).
2010
- # We no longer read the per-turn prompt from stdin as the retrieval query -
2011
- # that keyed the cache by a volatile string, guaranteeing a miss + a live
2012
- # call + a timeout every turn. An explicit --query is still honored so a
2013
- # human can request a query-specific prime by hand.
2096
+ # The per-turn prompt never keys the prime cache - that guaranteed a miss +
2097
+ # a live call + a timeout every turn. An explicit --query is still honored
2098
+ # so a human can request a query-specific prime by hand.
2014
2099
  query = query.strip()
2015
2100
  binary = thinqos_binary()
2016
2101
  max_age = prime_cache_max_age_seconds()
@@ -2022,6 +2107,30 @@ def prime(query: str, limit: int) -> None:
2022
2107
  receipt_client = os.environ.get("THINQOS_HOOK_OWNER") or "claude"
2023
2108
  receipt_event = os.environ.get("GROK_HOOK_EVENT") or ""
2024
2109
 
2110
+ # TOS-4486 Phase 6 C: the question layer. One recall keyed on the prompt,
2111
+ # started now so it overlaps the prime, joined in _emit against a fixed
2112
+ # budget from hook entry. It is additive: on a miss, a timeout or a
2113
+ # degraded answer the turn gets the prime alone, exactly as before.
2114
+ question = question_from_prompt(prompt) if not query else ""
2115
+ question_budget_s = question_recall_budget_seconds()
2116
+ question_recall: QuestionRecall | None = None
2117
+ if question and question_recall_enabled():
2118
+ if question_layer_cooling_down(cache_dir):
2119
+ log_question_recall(
2120
+ cache_dir,
2121
+ outcome=SKIPPED_SLOW,
2122
+ latency_ms=0.0,
2123
+ budget_s=question_budget_s,
2124
+ client=receipt_client,
2125
+ )
2126
+ else:
2127
+ question_recall = QuestionRecall(
2128
+ lambda timeout_s: _fetch_question_context(
2129
+ base_url, token, question, timeout_s, session_id=session_id
2130
+ ),
2131
+ deadline=hook_started + question_budget_s,
2132
+ ).start()
2133
+
2025
2134
  def _spawn_refresh() -> None:
2026
2135
  if query_is_deferred(cache_dir, query):
2027
2136
  return
@@ -2052,9 +2161,30 @@ def prime(query: str, limit: int) -> None:
2052
2161
  except Exception:
2053
2162
  logger.warning("hook.prime_refresh_spawn_failed", exc_info=True)
2054
2163
 
2164
+ def _question_block(prime_block: str) -> str:
2165
+ if question_recall is None:
2166
+ return ""
2167
+ result = question_recall.wait()
2168
+ outcome = result.outcome
2169
+ section = ""
2170
+ if outcome == "ok":
2171
+ section = relevant_section(result.context, already_shown=prime_block)
2172
+ outcome = "delivered" if section else "no_relevant"
2173
+ log_question_recall(
2174
+ cache_dir,
2175
+ outcome=outcome,
2176
+ latency_ms=result.latency_ms,
2177
+ budget_s=question_budget_s,
2178
+ client=receipt_client,
2179
+ detail=result.detail,
2180
+ )
2181
+ if not section:
2182
+ return ""
2183
+ return f"# {WORDMARK} Relevant to this prompt\n\n{section}"
2184
+
2055
2185
  def _emit(block: str, *, warnings: list[str]) -> None:
2056
2186
  banner = degradation_banner(cache_dir)
2057
- parts = [p for p in [banner, *warnings, block] if p]
2187
+ parts = [p for p in [banner, *warnings, block, _question_block(block)] if p]
2058
2188
  click.echo(compose_prime_output("\n\n".join(parts)))
2059
2189
 
2060
2190
  cached = load_cached_block(cache_dir, query)
@@ -2115,6 +2245,11 @@ def prime(query: str, limit: int) -> None:
2115
2245
  defer_refused_query(cache_dir, query)
2116
2246
  logger.warning("hook.prime_refused", exc_info=True)
2117
2247
  outcome = "error"
2248
+ except _DegradedRecall as exc:
2249
+ # The server cut the prime at its deadline or refused the Mind leg.
2250
+ # Graded as a failed live attempt; nothing is cached (TOS-4486).
2251
+ failure_detail = f"degraded: {exc}"
2252
+ outcome = "error"
2118
2253
  except httpx.TimeoutException:
2119
2254
  outcome = "timeout"
2120
2255
  except Exception:
@@ -2214,6 +2349,9 @@ def prime_refresh(query: str, limit: int, expected_scope: str, session_id: str)
2214
2349
  if block:
2215
2350
  save_cached_block(account.state_dir, query, block)
2216
2351
  log_refresh_event(account.state_dir, query=query, event="completed")
2352
+ except _DegradedRecall as exc:
2353
+ # Keep the last good block: a deadline-cut answer must never replace it.
2354
+ log_refresh_event(account.state_dir, query=query, event="degraded", detail=str(exc))
2217
2355
  except _MCPToolRefusal as exc:
2218
2356
  defer_refused_query(account.state_dir, query)
2219
2357
  log_refresh_event(account.state_dir, query=query, event="error", detail=str(exc))
@@ -38,6 +38,7 @@ from typing import Any
38
38
  from thinqos_capture.accountscope import ensure_private_dir, write_private_json
39
39
  from thinqos_capture.filelock import lock_exclusive, unlock
40
40
  from thinqos_capture.process import spawn_detached
41
+ from thinqos_capture.questionrecall import is_raw_recall_payload
41
42
 
42
43
  logger = logging.getLogger(__name__)
43
44
 
@@ -98,6 +99,12 @@ def load_cached_block(state_dir: Path, query: str) -> tuple[str, float] | None:
98
99
  # those entries or use them as a stale fallback (TOS-7410).
99
100
  if _CACHED_REPEAT_REFUSAL.fullmatch(block.strip()):
100
101
  return None
102
+ # 3.1.3 and earlier cached the JSON of a deadline-cut prime (no items,
103
+ # mind leg skipped_deadline) as if it were context, then replayed it on
104
+ # every prompt until the next refresh. A serialized tool result is never
105
+ # Mind context: not deliverable, not a stale fallback (TOS-4486).
106
+ if is_raw_recall_payload(block):
107
+ return None
101
108
  try:
102
109
  cached_dt = datetime.fromisoformat(cached_at)
103
110
  except ValueError:
@@ -107,7 +114,14 @@ def load_cached_block(state_dir: Path, query: str) -> tuple[str, float] | None:
107
114
 
108
115
 
109
116
  def save_cached_block(state_dir: Path, query: str, block: str) -> None:
110
- """Atomically upsert ``query -> block`` into the bounded LRU cache file."""
117
+ """Atomically upsert ``query -> block`` into the bounded LRU cache file.
118
+
119
+ An empty block or a serialized tool result is refused here as well as at
120
+ every caller, so no path can cache a non-answer and replay it (TOS-4486).
121
+ """
122
+ if not block.strip() or is_raw_recall_payload(block):
123
+ logger.warning("primecache.refused_non_context_block")
124
+ return
111
125
  path = _cache_path(state_dir)
112
126
  ensure_private_dir(state_dir)
113
127
  lock_file = state_dir / "prime_cache.lock"
@@ -342,6 +356,7 @@ def log_refresh_event(state_dir: Path, *, query: str, event: str, detail: str =
342
356
  "query": query,
343
357
  # "spawn" | "coalesce" | "reclaim_stale"
344
358
  # | "completed" | "timeout" | "error"
359
+ # | "degraded" (TOS-4486: answered, Mind read cut short; not cached)
345
360
  "event": event,
346
361
  "detail": detail,
347
362
  }