202 Commits

Author SHA1 Message Date
Dave
0a6de3717a fix: Chat.test.tsx type errors from 271 merge
Cast lastSendChatArgs through unknown to satisfy TypeScript's
strict type narrowing on the nullable union type.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:33:54 +00:00
Dave
15645a2a3e Bump version to 0.3.0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:31:19 +00:00
Dave
eab65de723 story-kit: create 281_story_matrix_bot_announces_itself_when_it_comes_online 2026-03-18 11:26:29 +00:00
Dave
81a5660f11 story-kit: merge 277 - bot uses configured display_name
Adds system_prompt parameter to chat_stream so the Matrix bot
passes "Your name is {name}" to Claude Code. Reads display_name
from bot.toml config. Resolved conflicts by integrating bot_name
into master's permission-handling code structure.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:23:50 +00:00
Dave
4bf01c6cca fix: session_id wiped on every trim, breaking --resume
The history trimming logic cleared session_id = None whenever entries
exceeded history_size. Since the history was always at capacity, every
new message wiped the session_id immediately after storing it. This
meant --resume was never passed to Claude Code, making every turn a
fresh conversation.

Fix: preserve session_id on trim. Claude Code's --resume loads the
full conversation from its own session transcript on disk, so trimming
our local tracking entries doesn't invalidate the session.

Also adds debug logging for session_id capture/storage (temporary).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 11:19:00 +00:00
Dave
a799009720 story-kit: done 279_story_auto_assign_should_respect_agent_stage_when_front_matter_specifies_agent 2026-03-18 11:10:41 +00:00
Dave
549c23bd77 story-kit: done 278_story_auto_assign_agents_to_pipeline_items_on_server_startup 2026-03-18 11:05:14 +00:00
Dave
34be4d1e75 story-kit: done 277_story_matrix_bot_uses_its_configured_name_instead_of_claude 2026-03-18 11:00:39 +00:00
Dave
a390861520 story-kit: done 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response 2026-03-18 10:55:58 +00:00
Dave
ce9bdbbb9d story-kit: done 266_story_matrix_bot_structured_conversation_history 2026-03-18 10:48:55 +00:00
Dave
5f4591f496 fix: update should_commit_stage test to match 5_done in COMMIT_WORTHY_STAGES
The test was asserting 5_done should NOT trigger commits, but commit 74dc42c
added 5_done to COMMIT_WORTHY_STAGES. Updated test to match.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-18 10:46:11 +00:00
Dave
dc7968ffbc story-kit: mergemaster conflict resolution, vite proxy fix, bug 279
- Upgrade mergemaster prompt to resolve complex conflicts itself
  instead of just reporting failure. Includes instructions to check
  git history and story files for context before resolving.
- Add proxy error handler to vite config to prevent crashes on
  backend ECONNREFUSED.
- Fix bug 279: auto-assign now checks that preferred agent's stage
  matches the pipeline stage. Coders won't be assigned to QA/merge.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:44:10 +00:00
Dave
5fedd9130a story-kit: create 280_story_long_running_supervisor_agent_with_periodic_pipeline_polling 2026-03-18 10:39:51 +00:00
Dave
c7e371c124 story-kit: create 280_story_long_running_supervisor_agent_with_periodic_pipeline_polling 2026-03-18 10:36:20 +00:00
Dave
8748d7d49a story-kit: create 280_story_long_running_supervisor_agent_with_periodic_pipeline_polling 2026-03-18 10:34:18 +00:00
Dave
825d36c204 fix: remove stale 266 from archived (came from 271 branch history)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:30:47 +00:00
Dave
65a8feff17 story-kit: done 271_story_show_assigned_agent_in_expanded_work_item_view 2026-03-18 10:29:45 +00:00
Dave
60dabae795 story-kit: merge 271_story_show_assigned_agent_in_expanded_work_item_view
Adds assigned agent display to the expanded work item detail panel.
Resolved conflicts by keeping master versions of bot.rs (permission
handling), ChatInput.tsx, and fs.rs. Removed duplicate list_project_files
endpoint and tests from io.rs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:29:37 +00:00
Dave
1bae7bd223 story-kit: create 266_story_matrix_bot_structured_conversation_history 2026-03-18 10:21:55 +00:00
Dave
a0091e81f9 story-kit: create 266_story_matrix_bot_structured_conversation_history 2026-03-18 10:21:33 +00:00
Dave
beb5ea9f53 fix: revert broken auto-merge of 266 in bot.rs
The mergemaster's auto-resolver inserted a duplicate chat_stream call
inside the tokio::select! permission loop, producing mismatched braces.
The 266 merge didn't contribute useful code changes to bot.rs — the
session_id handling was already present.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:20:40 +00:00
Dave
89e96dc0a6 story-kit: done 266_story_matrix_bot_structured_conversation_history 2026-03-18 10:18:50 +00:00
Dave
0c686ba170 story-kit: merge 266_story_matrix_bot_structured_conversation_history 2026-03-18 10:18:49 +00:00
Dave
74dc42c1fc story-kit: add 5_done to commit-worthy stages
Keep done stage committed so accepted work is in git history.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:13:35 +00:00
Dave
a3d22fd874 story-kit: gitignore intermediate pipeline stages (spike 92 follow-up)
Ignore 2_current/, 3_qa/, and 4_merge/ in git since spike 92 stopped
committing intermediate pipeline moves. Keep 5_done/ tracked so
accepted work is in git history before sweep to archived.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:13:10 +00:00
Dave
8561910cd8 story-kit: start 266_story_matrix_bot_structured_conversation_history 2026-03-18 10:09:26 +00:00
Dave
e569c1bcad story-kit: start 266_story_matrix_bot_structured_conversation_history 2026-03-18 10:08:19 +00:00
Dave
4dcb24d5dd story-kit: queue 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response for merge 2026-03-18 10:07:12 +00:00
Dave
59f37e13b9 story-kit: done 92_spike_stop_auto_committing_intermediate_pipeline_moves 2026-03-18 10:07:05 +00:00
Dave
3a1d7012b4 spike(92): stop auto-committing intermediate pipeline moves
Filter flush_pending() to only git-commit for terminal stages
(1_upcoming and 6_archived) while still broadcasting WatcherEvents
for all stages so the frontend stays in sync.

Reduces pipeline commits from 5+ to 2 per story run. No system
dependencies on intermediate commits were found.

Preserves merge_failure front matter cleanup from master.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:06:58 +00:00
Dave
41b24e4b7a story-kit: queue 266_story_matrix_bot_structured_conversation_history for merge 2026-03-18 10:05:09 +00:00
Dave
06948dae74 story-kit: queue 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response for QA 2026-03-18 10:03:09 +00:00
Dave
bbd4aee828 story-kit: start 271_story_show_assigned_agent_in_expanded_work_item_view 2026-03-18 10:03:06 +00:00
Dave
d40f007818 story-kit: start 278_story_auto_assign_agents_to_pipeline_items_on_server_startup 2026-03-18 10:01:54 +00:00
Dave
3819a02159 story-kit: start 278_story_auto_assign_agents_to_pipeline_items_on_server_startup 2026-03-18 10:01:54 +00:00
Dave
9b65845c90 story-kit: start 277_story_matrix_bot_uses_its_configured_name_instead_of_claude 2026-03-18 10:01:52 +00:00
Dave
28176727d7 story-kit: queue 271_story_show_assigned_agent_in_expanded_work_item_view for merge 2026-03-18 10:00:38 +00:00
Dave
1d59cdcc25 story-kit: start 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response 2026-03-18 10:00:28 +00:00
Dave
edc6b9ea05 story-kit: queue 271_story_show_assigned_agent_in_expanded_work_item_view for merge 2026-03-18 09:57:30 +00:00
Dave
8e4a8ce57a story-kit: queue 279_story_auto_assign_should_respect_agent_stage_when_front_matter_specifies_agent for merge 2026-03-18 09:54:32 +00:00
Dave
c863ee4135 story-kit: queue 279_story_auto_assign_should_respect_agent_stage_when_front_matter_specifies_agent for merge 2026-03-18 09:53:48 +00:00
Dave
dd4a1140fe story-kit: queue 278_story_auto_assign_agents_to_pipeline_items_on_server_startup for merge 2026-03-18 09:49:53 +00:00
Dave
895317330b story-kit: queue 278_story_auto_assign_agents_to_pipeline_items_on_server_startup for merge 2026-03-18 09:48:57 +00:00
Dave
11e32f9802 story-kit: queue 277_story_matrix_bot_uses_its_configured_name_instead_of_claude for merge 2026-03-18 09:44:22 +00:00
Dave
8b7ff6383f story-kit: queue 277_story_matrix_bot_uses_its_configured_name_instead_of_claude for merge 2026-03-18 09:43:30 +00:00
Dave
964a8bfcff story-kit: queue 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response for merge 2026-03-18 09:41:34 +00:00
Dave
978b84893c story-kit: queue 266_story_matrix_bot_structured_conversation_history for merge 2026-03-18 09:39:58 +00:00
Dave
7dd6821dc5 story-kit: queue 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response for merge 2026-03-18 09:38:05 +00:00
Dave
6abf5c87b2 story-kit: done 247_story_human_qa_gate_with_rejection_flow 2026-03-18 09:37:38 +00:00
Dave
b682c67f97 story-kit: queue 278_story_auto_assign_agents_to_pipeline_items_on_server_startup for QA 2026-03-18 09:35:50 +00:00
Dave
81309a5559 story-kit: queue 277_story_matrix_bot_uses_its_configured_name_instead_of_claude for QA 2026-03-18 09:35:49 +00:00
Dave
2006ad6d8c story-kit: queue 279_story_auto_assign_should_respect_agent_stage_when_front_matter_specifies_agent for QA 2026-03-18 09:35:45 +00:00
Dave
41bafb80e4 story-kit: accept 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat 2026-03-18 09:33:21 +00:00
Dave
569380e133 story-kit: done 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat 2026-03-18 09:32:20 +00:00
Dave
10a5bea2b1 story-kit: merge 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat 2026-03-18 09:32:17 +00:00
Dave
110815c1c5 story-kit: queue 247_story_human_qa_gate_with_rejection_flow for merge 2026-03-18 09:31:02 +00:00
Dave
29fc761980 fix: skip frontend tests when frontend dir absent in sparse worktrees
script/test unconditionally cd'd into frontend/ and ran npm test, which
failed in sparse-checkout worktrees that only contain the server/ subtree.
Guard the frontend step with a directory existence check so acceptance
gates pass for worktrees that have no frontend checkout.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 09:30:51 +00:00
Dave
d537aceb63 story-kit: start 278_story_auto_assign_agents_to_pipeline_items_on_server_startup 2026-03-18 09:29:43 +00:00
Dave
72b89c8ccc story-kit: start 277_story_matrix_bot_uses_its_configured_name_instead_of_claude 2026-03-18 09:29:27 +00:00
Dave
e19de02967 story-kit: start 279_story_auto_assign_should_respect_agent_stage_when_front_matter_specifies_agent 2026-03-18 09:29:00 +00:00
Dave
1c5f13e7eb story-kit: create 279_story_auto_assign_should_respect_agent_stage_when_front_matter_specifies_agent 2026-03-18 09:28:33 +00:00
Dave
816c771a2a story-kit: queue 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat for merge 2026-03-18 09:28:30 +00:00
Dave
642a8486cd story-kit: queue 266_story_matrix_bot_structured_conversation_history for QA 2026-03-18 09:25:33 +00:00
Dave
605bcadea7 story-kit: done 274_story_mcp_pipeline_status_tool_with_agent_assignments 2026-03-18 09:25:29 +00:00
Dave
ccc1ead8c9 story-kit: remove 274_story_mcp_pipeline_status_tool_with_agent_assignments 2026-03-18 09:25:07 +00:00
Dave
8bbbe8fbdd story-kit: start 274_story_mcp_pipeline_status_tool_with_agent_assignments 2026-03-18 09:25:01 +00:00
Dave
d9775834ed story-kit: queue 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat for QA 2026-03-18 09:22:43 +00:00
Dave
c32f0dce45 story-kit: queue 274_story_mcp_pipeline_status_tool_with_agent_assignments for merge 2026-03-18 09:22:39 +00:00
Dave
d864941665 story-kit: start 266_story_matrix_bot_structured_conversation_history 2026-03-18 09:21:40 +00:00
Dave
9c2d831c65 story-kit: create 266_story_matrix_bot_structured_conversation_history 2026-03-18 09:21:26 +00:00
Dave
2ab91f933f story-kit: create 266_story_matrix_bot_structured_conversation_history 2026-03-18 09:21:00 +00:00
Dave
1fcb8cb332 story-kit: create 266_story_matrix_bot_structured_conversation_history 2026-03-18 09:20:36 +00:00
Dave
3439c16e66 story-kit: create 278_story_auto_assign_agents_to_pipeline_items_on_server_startup 2026-03-18 09:18:23 +00:00
Dave
ce93987da8 story-kit: queue 274_story_mcp_pipeline_status_tool_with_agent_assignments for merge 2026-03-18 09:15:01 +00:00
Dave
bd7b7cc34a story-kit: create 277_story_matrix_bot_uses_its_configured_name_instead_of_claude 2026-03-18 09:09:49 +00:00
Dave
855452b4a2 story-kit: queue 274_story_mcp_pipeline_status_tool_with_agent_assignments for merge 2026-03-18 09:09:06 +00:00
Dave
1fcfa9123f story-kit: queue 274_story_mcp_pipeline_status_tool_with_agent_assignments for merge 2026-03-18 09:07:42 +00:00
Dave
e66b811436 story-kit: queue 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response for merge 2026-03-18 09:06:00 +00:00
Dave
8d5fa85a3a story-kit: accept 264_bug_claude_code_session_id_not_persisted_across_browser_refresh 2026-03-18 09:05:30 +00:00
Dave
a4e7a23ca6 story-kit: queue 274_story_mcp_pipeline_status_tool_with_agent_assignments for QA 2026-03-17 18:43:05 +00:00
Dave
b67eea7b9a story-kit: queue 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response for QA 2026-03-17 18:42:03 +00:00
Dave
4a89b46857 story-kit: start 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat 2026-03-17 18:33:23 +00:00
Dave
047bf83b76 story-kit: start 274_story_mcp_pipeline_status_tool_with_agent_assignments 2026-03-17 18:32:43 +00:00
Dave
62aa142409 story-kit: start 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response 2026-03-17 18:32:37 +00:00
Dave
c93a2e80f9 story-kit: queue 92_spike_stop_auto_committing_intermediate_pipeline_moves for QA 2026-03-17 18:32:30 +00:00
Dave
9176fe3303 story-kit: create 276_story_detect_and_log_when_root_mcp_json_port_is_modified 2026-03-17 18:26:07 +00:00
Dave
296a59def3 story-kit: create 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat 2026-03-17 18:23:52 +00:00
Dave
90bb2fb137 story-kit: done 272_story_clear_merge_error_front_matter_when_story_leaves_merge_stage 2026-03-17 18:21:59 +00:00
Dave
bc0bb91a83 story-kit: merge 272_story_clear_merge_error_front_matter_when_story_leaves_merge_stage 2026-03-17 18:21:56 +00:00
Dave
0b39b2acfc story-kit: create 275_story_matrix_bot_surfaces_claude_code_permission_prompts_to_chat 2026-03-17 18:21:40 +00:00
Dave
75c27f5853 story-kit: create 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response 2026-03-17 18:18:29 +00:00
Dave
349866606c story-kit: done 270_bug_qa_test_server_overwrites_root_mcp_json_with_wrong_port 2026-03-17 18:16:30 +00:00
Dave
901f7a65d3 story-kit: merge 270_bug_qa_test_server_overwrites_root_mcp_json_with_wrong_port 2026-03-17 18:16:13 +00:00
Dave
c52b41b99c story-kit: queue 272_story_clear_merge_error_front_matter_when_story_leaves_merge_stage for merge 2026-03-17 18:15:18 +00:00
Dave
ec76005c63 story-kit: create 274_story_mcp_pipeline_status_tool_with_agent_assignments 2026-03-17 18:10:26 +00:00
Dave
1736f8d924 story-kit: queue 92_spike_stop_auto_committing_intermediate_pipeline_moves for QA 2026-03-17 18:08:42 +00:00
Dave
f8b5e11c27 story-kit: done 269_story_file_references_in_web_ui_chat_input 2026-03-17 18:04:48 +00:00
Dave
12c500ee90 story-kit: remove 269_story_file_references_in_web_ui_chat_input 2026-03-17 18:03:21 +00:00
Dave
81c9cf797f story-kit: done 269_story_file_references_in_web_ui_chat_input 2026-03-17 18:03:10 +00:00
Dave
d18c1105c7 story-kit: start 269_story_file_references_in_web_ui_chat_input 2026-03-17 18:01:09 +00:00
Dave
ca8e6dc51c story-kit: accept 245_bug_chat_history_persistence_lost_on_page_refresh_story_145_regression 2026-03-17 17:58:30 +00:00
Dave
30ad59c6eb story-kit: queue 269_story_file_references_in_web_ui_chat_input for merge 2026-03-17 17:57:29 +00:00
Dave
123f140244 story-kit: start 269_story_file_references_in_web_ui_chat_input 2026-03-17 17:56:24 +00:00
Dave
8db23f77cd story-kit: start 92_spike_stop_auto_committing_intermediate_pipeline_moves 2026-03-17 17:55:34 +00:00
Dave
6bfa10b0e5 story-kit: queue 269_story_file_references_in_web_ui_chat_input for merge 2026-03-17 17:55:15 +00:00
Dave
65036b2ce7 story-kit: start 92_spike_stop_auto_committing_intermediate_pipeline_moves 2026-03-17 17:54:03 +00:00
Dave
76d73b2d0b story-kit: gitignore matrix_history.json
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 17:53:33 +00:00
Dave
78618a1b76 story-kit: queue 269_story_file_references_in_web_ui_chat_input for merge 2026-03-17 17:51:06 +00:00
Dave
47e07b23d1 story-kit: queue 272_story_clear_merge_error_front_matter_when_story_leaves_merge_stage for QA 2026-03-17 17:50:17 +00:00
Dave
45ae7b8f01 story-kit: queue 269_story_file_references_in_web_ui_chat_input for merge 2026-03-17 17:49:46 +00:00
Dave
e1c30b5953 story-kit: accept 262_story_bot_error_notifications_for_story_failures 2026-03-17 17:49:07 +00:00
Dave
b0d9fb4f39 story-kit: queue 269_story_file_references_in_web_ui_chat_input for merge 2026-03-17 17:47:07 +00:00
Dave
dcc11c2b0f story-kit: queue 271_story_show_assigned_agent_in_expanded_work_item_view for QA 2026-03-17 17:46:43 +00:00
Dave
7f21454880 story-kit: queue 269_story_file_references_in_web_ui_chat_input for merge 2026-03-17 17:45:44 +00:00
Dave
a893a1cef7 story-kit: merge 266_story_matrix_bot_structured_conversation_history 2026-03-17 17:42:08 +00:00
Dave
3fb48cdf51 story-kit: queue 269_story_file_references_in_web_ui_chat_input for QA 2026-03-17 17:41:55 +00:00
Dave
f1bb1216bf story-kit: done 266_story_matrix_bot_structured_conversation_history 2026-03-17 17:41:29 +00:00
Dave
b3faf7b810 story-kit: queue 247_story_human_qa_gate_with_rejection_flow for merge 2026-03-17 17:40:54 +00:00
Dave
89e4ee1c9c story-kit: queue 270_bug_qa_test_server_overwrites_root_mcp_json_with_wrong_port for merge 2026-03-17 17:37:50 +00:00
Dave
4df39eb1f2 story-kit: create 273_story_matrix_bot_sends_typing_indicator_while_waiting_for_claude_response 2026-03-17 17:37:41 +00:00
Dave
a7d23143ef story-kit: done 267_story_mcp_update_story_tool_should_support_front_matter_fields 2026-03-17 17:36:00 +00:00
Dave
f72666b39e story-kit: merge 267_story_mcp_update_story_tool_should_support_front_matter_fields 2026-03-17 17:35:57 +00:00
Dave
1f8ffee38e story-kit: start 272_story_clear_merge_error_front_matter_when_story_leaves_merge_stage 2026-03-17 17:35:23 +00:00
Dave
798f841b9a story-kit: queue 267_story_mcp_update_story_tool_should_support_front_matter_fields for merge 2026-03-17 17:34:26 +00:00
Dave
25c3dbb3d1 story-kit: start 271_story_show_assigned_agent_in_expanded_work_item_view 2026-03-17 17:33:34 +00:00
Dave
71cbc21b01 story-kit: queue 266_story_matrix_bot_structured_conversation_history for merge 2026-03-17 17:31:57 +00:00
Dave
6deeba81a8 story-kit: queue 266_story_matrix_bot_structured_conversation_history for merge 2026-03-17 17:30:53 +00:00
Dave
b862a7a6d0 story-kit: queue 267_story_mcp_update_story_tool_should_support_front_matter_fields for merge 2026-03-17 17:29:36 +00:00
Dave
fe1f76957d story-kit: queue 267_story_mcp_update_story_tool_should_support_front_matter_fields for merge 2026-03-17 17:28:38 +00:00
Dave
266e676dd4 story-kit: queue 247_story_human_qa_gate_with_rejection_flow for QA 2026-03-17 17:27:04 +00:00
Dave
402159c19a story-kit: queue 270_bug_qa_test_server_overwrites_root_mcp_json_with_wrong_port for QA 2026-03-17 17:26:23 +00:00
Dave
6d1b36e515 story-kit: start 269_story_file_references_in_web_ui_chat_input 2026-03-17 17:25:42 +00:00
Dave
81d4889cee Expand vite watch ignore list to prevent silent crashes on merge
Vite only needs to watch frontend/ sources. Ignore git objects,
Rust source, Cargo files, node_modules, and vendor directories
that change in bulk during squash merges.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 17:17:47 +00:00
Dave
0eb2cd8ec3 Stop tracking .mcp.json — it is generated at runtime
The server writes .mcp.json with its current port on startup and
during worktree creation. Tracking it in git causes QA gate failures
when the worktree copy diverges from master (e.g. different port).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 17:15:35 +00:00
Dave
b251ed7421 story-kit: create 272_story_clear_merge_error_front_matter_when_story_leaves_merge_stage 2026-03-17 17:14:19 +00:00
Dave
4a600e9954 story-kit: create 271_story_show_assigned_agent_in_expanded_work_item_view 2026-03-17 17:13:08 +00:00
Dave
cfb810b061 story-kit: start 270_bug_qa_test_server_overwrites_root_mcp_json_with_wrong_port 2026-03-17 17:10:45 +00:00
Dave
71bd999586 story-kit: create 270_bug_qa_test_server_overwrites_root_mcp_json_with_wrong_port 2026-03-17 17:10:30 +00:00
Dave
10d0cdeeae story-kit: accept 256_story_bot_must_verify_other_users_cross_signing_identity_before_checking_device_verification 2026-03-17 17:09:27 +00:00
Dave
6e375aaab5 story-kit: create 270_bug_qa_test_server_overwrites_root_mcp_json_with_wrong_port 2026-03-17 17:08:26 +00:00
Dave
e7edf9a8d5 story-kit: done 268_refactor_upgrade_tokio_tungstenite_to_0_29_0 2026-03-17 17:02:34 +00:00
Dave
20431f625b story-kit: merge 268_refactor_upgrade_tokio_tungstenite_to_0_29_0 2026-03-17 17:02:31 +00:00
Dave
d35f0f19fb story-kit: queue 268_refactor_upgrade_tokio_tungstenite_to_0_29_0 for merge 2026-03-17 16:59:54 +00:00
Dave
4303b33b90 story-kit: queue 266_story_matrix_bot_structured_conversation_history for QA 2026-03-17 16:58:19 +00:00
Dave
f9c0d24d7a story-kit: accept 259_story_move_story_kit_ignores_into_story_kit_gitignore 2026-03-17 16:57:12 +00:00
Dave
ec3277234c story-kit: queue 267_story_mcp_update_story_tool_should_support_front_matter_fields for QA 2026-03-17 16:53:12 +00:00
Dave
0a28aae041 story-kit: queue 268_refactor_upgrade_tokio_tungstenite_to_0_29_0 for QA 2026-03-17 16:47:46 +00:00
Dave
a7a8358cbb story-kit: create 269_story_file_references_in_web_ui_chat_input 2026-03-17 16:46:55 +00:00
Dave
6b6cb525a7 story-kit: start 268_refactor_upgrade_tokio_tungstenite_to_0_29_0 2026-03-17 16:43:45 +00:00
Dave
27465b1130 story-kit: create 268_refactor_upgrade_tokio_tungstenite_to_0_29_0 2026-03-17 16:43:34 +00:00
Dave
e74c370c7e Improve release changelog and fix MCP port
Generate structured changelogs from completed stories instead of raw
commit messages. Group by features, bug fixes, and refactors. Filter
out story-kit automation commits.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 16:42:48 +00:00
Dave
8defd5c671 story-kit: start 267_story_mcp_update_story_tool_should_support_front_matter_fields 2026-03-17 16:42:08 +00:00
Dave
a5c4fb553a story-kit: create 267_story_mcp_update_story_tool_should_support_front_matter_fields 2026-03-17 16:41:47 +00:00
Dave
a7772d1421 story-kit: start 247_story_human_qa_gate_with_rejection_flow 2026-03-17 16:41:27 +00:00
Dave
ed967403fb story-kit: start 247_story_human_qa_gate_with_rejection_flow 2026-03-17 16:40:09 +00:00
Dave
998b188ac7 story-kit: start 266_story_matrix_bot_structured_conversation_history 2026-03-17 16:39:42 +00:00
Dave
115c9fd6df story-kit: done 265_story_spikes_skip_merge_and_stop_for_human_review 2026-03-17 16:36:03 +00:00
Dave
86694a4383 story-kit: merge 265_story_spikes_skip_merge_and_stop_for_human_review 2026-03-17 16:36:00 +00:00
Dave
7b324ea96e story-kit: accept 257_story_rename_storkit_to_story_kit_in_header 2026-03-17 16:35:37 +00:00
Dave
744a12eeea story-kit: queue 265_story_spikes_skip_merge_and_stop_for_human_review for merge 2026-03-17 16:33:37 +00:00
Dave
cffe63680d Fix MCP server URL to match actual running port (3010)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 16:17:54 +00:00
Dave
f5fffd64b8 story-kit: create 266_story_matrix_bot_structured_conversation_history 2026-03-17 16:17:34 +00:00
Dave
ad68bc912f story-kit: remove 266_story_matrix_bot_structured_conversation_history 2026-03-17 16:17:20 +00:00
Dave
d02d53d112 story-kit: create 266_story_matrix_bot_structured_conversation_history 2026-03-17 16:14:07 +00:00
Dave
3ce7276e89 Fix TS narrowing errors in Chat.test.tsx
TypeScript control flow analysis can't track reassignment inside vi.mock
callbacks, causing lastSendChatArgs to narrow to never. Use non-null
assertions after the explicit toBeNull() guard.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 16:13:29 +00:00
Dave
6d87e64859 story-kit: queue 265_story_spikes_skip_merge_and_stop_for_human_review for QA 2026-03-17 16:11:52 +00:00
Dave
83db282892 Bump version to 0.2.0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 16:08:59 +00:00
Dave
f5d5196bf5 Fix cross-signing bootstrap by passing UIA password auth
The homeserver requires User-Interactive Authentication before accepting
cross-signing keys. Pass the bot's password so bootstrap succeeds.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 16:08:39 +00:00
Dave
7ec869baa8 Fixing mcp location 2026-03-17 15:58:40 +00:00
Dave
1a257b3057 story-kit: start 265_story_spikes_skip_merge_and_stop_for_human_review 2026-03-17 15:58:28 +00:00
Dave
b9fd87ed7c story-kit: create 265_story_spikes_skip_merge_and_stop_for_human_review 2026-03-17 15:58:24 +00:00
Dave
fda763d3f0 story-kit: create 260_refactor_upgrade_libsqlite3_sys 2026-03-17 15:47:07 +00:00
Dave
77d89b17e8 story-kit: done 260_refactor_upgrade_libsqlite3_sys 2026-03-17 15:47:01 +00:00
Dave
df0fa46591 Revert "story-kit: merge 260_refactor_upgrade_libsqlite3_sys"
This reverts commit ea062400e5.
2026-03-17 15:45:52 +00:00
Dave
1f5d70ce0d story-kit: done 264_bug_claude_code_session_id_not_persisted_across_browser_refresh 2026-03-17 15:41:43 +00:00
Dave
0d46c86469 story-kit: merge 264_bug_claude_code_session_id_not_persisted_across_browser_refresh 2026-03-17 15:41:41 +00:00
Dave
a439f8fdcb story-kit: queue 264_bug_claude_code_session_id_not_persisted_across_browser_refresh for merge 2026-03-17 15:39:34 +00:00
Dave
1adddf4e4c story-kit: create 247_story_human_qa_gate_with_rejection_flow 2026-03-17 15:37:25 +00:00
Dave
23484716e2 story-kit: create 265_story_spikes_skip_merge_and_stop_for_human_review 2026-03-17 15:36:52 +00:00
Dave
92085f9071 story-kit: done 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption 2026-03-17 15:35:01 +00:00
Dave
ce899b569e story-kit: merge 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption 2026-03-17 15:34:58 +00:00
Dave
da7216630b story-kit: queue 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption for merge 2026-03-17 15:33:11 +00:00
Dave
b57c270144 story-kit: queue 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption for merge 2026-03-17 15:31:25 +00:00
Dave
230b8fdc35 story-kit: queue 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption for merge 2026-03-17 15:28:49 +00:00
Dave
75b2446801 story-kit: done 262_story_bot_error_notifications_for_story_failures 2026-03-17 15:27:51 +00:00
Dave
96779c9caf story-kit: merge 262_story_bot_error_notifications_for_story_failures 2026-03-17 15:27:48 +00:00
Dave
bf5d9ff6b1 story-kit: queue 264_bug_claude_code_session_id_not_persisted_across_browser_refresh for QA 2026-03-17 15:25:47 +00:00
Dave
c551faeea3 story-kit: queue 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption for merge 2026-03-17 15:19:41 +00:00
Dave
3f38f90a50 story-kit: queue 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption for merge 2026-03-17 15:19:33 +00:00
Dave
26a1328c89 story-kit: queue 262_story_bot_error_notifications_for_story_failures for merge 2026-03-17 15:18:57 +00:00
Dave
21b45b8dd7 story-kit: queue 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption for merge 2026-03-17 15:18:32 +00:00
Dave
3a860bd2d5 story-kit: start 264_bug_claude_code_session_id_not_persisted_across_browser_refresh 2026-03-17 15:16:14 +00:00
Dave
c2c95c18b4 story-kit: create 264_bug_claude_code_session_id_not_persisted_across_browser_refresh 2026-03-17 15:15:51 +00:00
Dave
e3a301009b story-kit: queue 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption for QA 2026-03-17 15:08:23 +00:00
Dave
c90bdc8907 story-kit: queue 262_story_bot_error_notifications_for_story_failures for QA 2026-03-17 15:07:17 +00:00
Dave
dba12a38c2 story-kit: start 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption 2026-03-17 15:00:34 +00:00
Dave
4b60452b27 story-kit: start 262_story_bot_error_notifications_for_story_failures 2026-03-17 14:57:27 +00:00
Dave
d2f677ae0c story-kit: create 263_story_matrix_bot_self_signs_device_keys_at_startup_for_verified_encryption 2026-03-17 14:56:59 +00:00
Dave
427bb6929a Ignoring some folders to keep vite running across merges 2026-03-17 14:48:37 +00:00
Dave
78c04ee576 story-kit: create 262_story_bot_error_notifications_for_story_failures 2026-03-17 14:44:04 +00:00
Dave
3309d26142 story-kit: done 245_bug_chat_history_persistence_lost_on_page_refresh_story_145_regression 2026-03-17 14:38:08 +00:00
Dave
5a4a2aaa17 story-kit: merge 245_bug_chat_history_persistence_lost_on_page_refresh_story_145_regression 2026-03-17 14:38:05 +00:00
136 changed files with 3446 additions and 23758 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,6 @@
# Claude Code
.claude/settings.local.json
.mcp.json
# Local environment (secrets)
.env
@@ -25,6 +26,7 @@ frontend/node_modules
frontend/dist
frontend/dist-ssr
frontend/test-results
frontend/serve
frontend/*.local
server/target

View File

@@ -1,8 +0,0 @@
{
"mcpServers": {
"story-kit": {
"type": "http",
"url": "http://localhost:3001/mcp"
}
}
}

View File

@@ -4,10 +4,16 @@ bot.toml
# Matrix SDK state store
matrix_store/
matrix_device_id
matrix_history.json
# Agent worktrees and merge workspace (managed by the server, not tracked in git)
worktrees/
merge_workspace/
# Intermediate pipeline stages (transient, not committed per spike 92)
work/2_current/
work/3_qa/
work/4_merge/
# Coverage reports (generated by cargo-llvm-cov, not tracked in git)
coverage/

7
.story_kit/problems.md Normal file
View File

@@ -0,0 +1,7 @@
# Problems
Recurring issues observed during pipeline operation. Review periodically and create stories for systemic problems.
## 2026-03-18: Agent committed directly to master instead of worktree
Commit `5f4591f` ("fix: update should_commit_stage test to match 5_done") was made directly on master by an agent (likely mergemaster). Agents should only commit to their feature branch or merge-queue branch, never to master directly. The commit content was correct but the target branch was wrong. Suspect the agent ran `git commit` in the project root instead of the merge worktree directory.

View File

@@ -220,7 +220,7 @@ role = "Merges completed coder work into master, runs quality gates, archives st
model = "opus"
max_turns = 30
max_budget_usd = 5.00
prompt = """You are the mergemaster agent for story {{story_id}}. Your job is to merge the completed coder work into master using the merge_agent_work MCP tool.
prompt = """You are the mergemaster agent for story {{story_id}}. Your job is to merge the completed coder work into master.
Read CLAUDE.md first, then .story_kit/README.md to understand the dev process.
@@ -229,20 +229,43 @@ Read CLAUDE.md first, then .story_kit/README.md to understand the dev process.
2. Review the result: check success, had_conflicts, conflicts_resolved, gates_passed, and gate_output
3. If merge succeeded and gates passed: report success to the human
4. If conflicts were auto-resolved (conflicts_resolved=true) and gates passed: report success, noting which conflicts were resolved
5. If conflicts could not be auto-resolved: call report_merge_failure(story_id='{{story_id}}', reason='<conflict details>') and report to the human. Master is untouched.
6. If merge failed for any other reason: call report_merge_failure(story_id='{{story_id}}', reason='<details>') and report to the human.
7. If gates failed after merge: attempt to fix minor issues (see below), then re-trigger merge_agent_work. After 2 fix attempts, call report_merge_failure and stop.
5. If conflicts could not be auto-resolved: **resolve them yourself** in the merge worktree (see below)
6. If merge failed for any other reason: call report_merge_failure(story_id='{{story_id}}', reason='<details>') and report to the human
7. If gates failed after merge: attempt to fix the issues yourself in the merge worktree, then re-trigger merge_agent_work. After 3 fix attempts, call report_merge_failure and stop.
## How Conflict Resolution Works
The merge pipeline uses a temporary merge-queue branch and worktree to isolate merges from master. Simple additive conflicts (both branches adding code at the same location) are resolved automatically by keeping both additions. Complex conflicts (modifying the same lines differently) are reported without touching master.
## Resolving Complex Conflicts Yourself
## Fixing Minor Gate Failures
If quality gates fail (cargo clippy, cargo test, npm run build, npm test), attempt to fix minor issues yourself before reporting to the human.
When the auto-resolver fails, you have access to the merge worktree at `.story_kit/merge_workspace/`. Go in there and resolve the conflicts manually:
**Fix yourself (up to 2 attempts total):**
1. Run `git diff --name-only --diff-filter=U` in the merge worktree to list conflicted files
2. **Build context before touching code.** Run `git log --oneline master...HEAD` on the feature branch to see its commits. Then run `git log --oneline --since="$(git log -1 --format=%ci <feature-branch-base-commit>)" master` to see what landed on master since the branch was created. Read the story files in `.story_kit/work/` for any recently merged stories that touch the same files — this tells you WHY master changed and what must be preserved.
3. Read each conflicted file and understand both sides of the conflict
4. **Understand intent, not just syntax.** The feature branch may be behind master — master's version of shared infrastructure is almost always correct. The feature branch's contribution is the NEW functionality it adds. Your job is to integrate the new into master's structure, not pick one side.
5. Resolve by integrating the feature's new functionality into master's code structure
5. Stage resolved files with `git add`
6. Run `cargo check` (and `npm run build` if frontend changed) to verify compilation
7. If it compiles, commit and re-trigger merge_agent_work
### Common conflict patterns in this project:
**Story file rename/rename conflicts:** Both branches moved the story .md file to different pipeline directories. Resolution: `git rm` both sides — story files in `work/2_current/`, `work/3_qa/`, `work/4_merge/` are gitignored and don't need to be committed.
**bot.rs tokio::select! conflicts:** Master has a `tokio::select!` loop in `handle_message()` that handles permission forwarding (story 275). Feature branches created before story 275 have a simpler direct `provider.chat_stream().await` call. Resolution: KEEP master's tokio::select! loop. Integrate only the feature's new logic (e.g. typing indicators, new callbacks) into the existing loop structure. Do NOT replace the loop with the old direct call.
**Duplicate functions/imports:** The auto-resolver keeps both sides, producing duplicates. Resolution: keep one copy (prefer master's version), delete the duplicate.
**Formatting-only conflicts:** Both sides reformatted the same code differently. Resolution: pick either side (prefer master).
## Fixing Gate Failures
If quality gates fail (cargo clippy, cargo test, npm run build, npm test), attempt to fix issues yourself in the merge worktree.
**Fix yourself (up to 3 attempts total):**
- Syntax errors (missing semicolons, brackets, commas)
- Duplicate definitions from merge artifacts
- Simple type annotation errors
- Unused import warnings flagged by clippy
- Mismatched braces from bad conflict resolution
- Trivial formatting issues that block compilation or linting
**Report to human without attempting a fix:**
@@ -250,17 +273,14 @@ If quality gates fail (cargo clippy, cargo test, npm run build, npm test), attem
- Missing function implementations
- Architectural changes required
- Non-trivial refactoring needed
- Anything requiring understanding of broader system context
**Max retry limit:** If gates still fail after 2 fix attempts, call report_merge_failure to record the failure, then stop immediately and report the full gate output to the human. Do not retry further.
**Max retry limit:** If gates still fail after 3 fix attempts, call report_merge_failure to record the failure, then stop immediately and report the full gate output to the human.
## CRITICAL Rules
- NEVER manually move story files between pipeline stages (e.g. from 4_merge/ to 5_done/)
- NEVER call accept_story — only merge_agent_work can move stories to done after a successful merge
- When merge fails, ALWAYS call report_merge_failure to record the failure — do NOT improvise with file moves
- Only use MCP tools (merge_agent_work, report_merge_failure) to drive the merge process
- Only attempt fixes that are clearly minor and low-risk
- When merge fails after exhausting your fix attempts, ALWAYS call report_merge_failure
- Report conflict resolution outcomes clearly
- Report gate failures with full output so the human can act if needed
- The server automatically runs acceptance gates when your process exits"""
system_prompt = "You are the mergemaster agent. Your primary responsibility is to trigger the merge_agent_work MCP tool and report the results. CRITICAL: Never manually move story files or call accept_story. When merge fails, call report_merge_failure to record the failure. For minor gate failures (syntax errors, unused imports, missing semicolons), attempt to fix them yourself — but stop after 2 attempts, call report_merge_failure, and report to the human. For complex failures or unresolvable conflicts, call report_merge_failure and report clearly so the human can act. The merge pipeline automatically resolves simple additive conflicts."
system_prompt = "You are the mergemaster agent. Your primary job is to merge feature branches to master. First try the merge_agent_work MCP tool. If the auto-resolver fails on complex conflicts, resolve them yourself in the merge worktree — you are an opus-class agent capable of understanding both sides of a conflict and producing correct merged code. Common patterns: keep master's tokio::select! permission loop in bot.rs, discard story file rename conflicts (gitignored), remove duplicate definitions. After resolving, verify compilation before re-triggering merge. CRITICAL: Never manually move story files or call accept_story. After 3 failed fix attempts, call report_merge_failure and stop."

View File

@@ -0,0 +1,24 @@
---
name: "Upgrade libsqlite3-sys"
---
# Refactor 260: Upgrade libsqlite3-sys
## Description
Upgrade the `libsqlite3-sys` dependency from `0.35.0` to `0.37.0`. The crate is used with `features = ["bundled"]` for static builds.
## Version Notes
- Current: `libsqlite3-sys 0.35.0` (pinned transitively by `matrix-sdk 0.16.0``matrix-sdk-sqlite``rusqlite 0.37.x`)
- Target: `libsqlite3-sys 0.37.0`
- Latest upstream rusqlite: `0.39.0`
- **Blocker**: `matrix-sdk 0.16.0` pins `rusqlite 0.37.x` which pins `libsqlite3-sys 0.35.0`. A clean upgrade requires either waiting for matrix-sdk to bump their rusqlite dep, or upgrading matrix-sdk itself.
- **Reverted 2026-03-17**: A previous coder vendored the entire rusqlite crate with a fake `0.37.99` version and patched its libsqlite3-sys dep. This was too hacky — reverted to clean `0.35.0`.
## Acceptance Criteria
- [ ] `libsqlite3-sys` is upgraded to `0.37.0` via a clean dependency path (no vendored forks)
- [ ] `cargo build` succeeds
- [ ] All tests pass
- [ ] No `[patch.crates-io]` hacks or vendored crates

View File

@@ -0,0 +1,28 @@
---
name: "Long-running supervisor agent with periodic pipeline polling"
agent: coder-opus
---
# Story 280: Long-running supervisor agent with periodic pipeline polling
## User Story
As a project owner, I want a long-running supervisor agent (opus) that automatically monitors the pipeline, assigns agents, resolves stuck items, and handles routine operational tasks, so that I don't have to manually check status, kick agents, or babysit the pipeline in every conversation.
## Acceptance Criteria
- [ ] Server can start a persistent supervisor agent that stays alive across the session (not per-story)
- [ ] Server prods the supervisor periodically (default 30s, configurable in project.toml) with a pipeline status update
- [ ] Supervisor auto-assigns agents to unassigned items in current/qa/merge stages
- [ ] Supervisor detects stuck agents (no progress for configurable timeout) and restarts them
- [ ] Supervisor detects merge failures and sends stories back to current for rebase when appropriate
- [ ] Supervisor can be chatted with via Matrix (timmy relays to supervisor) or via the web UI
- [ ] Supervisor logs its decisions so the human can review what it did and why
- [ ] Polling interval is configurable in project.toml (e.g. supervisor_poll_interval_secs = 30)
- [ ] Supervisor logs persistent/recurring problems to `.story_kit/problems.md` with timestamp, description, and frequency — humans review this file periodically to create stories for systemic issues
## Out of Scope
- Supervisor accepting or merging stories to master (human job)
- Supervisor making architectural decisions
- Replacing the existing per-story agent spawning — supervisor coordinates on top of it

View File

@@ -0,0 +1,20 @@
---
name: "Matrix bot announces itself when it comes online"
---
# Story 281: Matrix bot announces itself when it comes online
## User Story
As a user in the Matrix room, I want Timmy to post a message when he starts up, so that I know the bot is online and ready to accept commands.
## Acceptance Criteria
- [ ] Bot sends a brief greeting message to each configured room on startup (e.g. 'Timmy is online.')
- [ ] Message uses the configured display_name, not a hardcoded name
- [ ] Message is only sent once per startup, not on reconnects or sync resumptions
- [ ] Bot does not announce if it was already running (e.g. after a brief network blip)
## Out of Scope
- TBD

View File

@@ -1,55 +0,0 @@
---
name: "Stop auto-committing intermediate pipeline moves"
---
# Spike 92: Stop auto-committing intermediate pipeline moves
## Goal
Determine how to stop the filesystem watcher from auto-committing every pipeline stage move (upcoming -> current -> qa -> merge -> done -> archive) while still committing at terminal states (creation in upcoming, acceptance in done and archived). This keeps git history clean while preserving cross-machine portability for completed work.
## Context
The watcher in `server/src/io/watcher.rs` currently auto-commits every file change in `.story_kit/work/`. A single story run generates 5+ commits just from pipeline moves:
- `story-kit: create 42_story_foo`
- `story-kit: start 42_story_foo`
- `story-kit: queue 42_story_foo for QA`
- `story-kit: queue 42_story_foo for merge`
- `story-kit: accept 42_story_foo`
Since story runs complete relatively quickly, the intermediate state (current/qa/merge) is transient and doesn't need to be committed. Only creation and archival are meaningful checkpoints.
## Questions to Answer
1. Can we filter `stage_metadata()` to only commit for `1_upcoming` and `5_archived` stages while still broadcasting `WatcherEvent`s for all stages (so the frontend stays in sync)?
2. Should we keep `git add -A .story_kit/work/` for the committed stages, or narrow it to only the specific file?
3. What happens if the server crashes mid-pipeline? Uncommitted moves are lost — is this acceptable given the story can just be re-run?
4. Should intermediate moves be `.gitignore`d at the directory level, or is filtering in the watcher sufficient?
5. Do any other parts of the system (agent worktree setup, merge_agent_work, sparse checkout) depend on intermediate pipeline files being committed to master?
## Approach to Investigate
### Option A: Filter in `flush_pending()`
- In `flush_pending()`, still broadcast the `WatcherEvent` for all stages
- Only call `git_add_work_and_commit()` for stages `1_upcoming` and `5_archived`
- Simplest change — ~5 lines modified in `watcher.rs`
### Option B: Two-tier watcher
- Split into "commit-worthy" events (create, archive) and "notify-only" events (start, qa, merge)
- Commit-worthy events go through git
- Notify-only events just broadcast to WebSocket clients
- More explicit but same end result as Option A
### Option C: .gitignore intermediate directories
- Add `2_current/`, `3_qa/`, `4_merge/` to `.gitignore`
- Watcher still sees events (gitignore doesn't affect filesystem watching)
- Git naturally ignores them
- Risk: harder to debug, `git status` won't show pipeline state
## Acceptance Criteria
- [ ] Spike document updated with findings and recommendation
- [ ] If Option A is viable: prototype the change and verify git log is clean during a full story run
- [ ] Confirm frontend still receives real-time pipeline updates for all stages
- [ ] Confirm no other system depends on intermediate pipeline commits being on master
- [ ] Identify any edge cases (server crash, manual git operations, multi-machine sync)

View File

@@ -1,17 +0,0 @@
---
name: "Upgrade libsqlite3-sys"
---
# Refactor 260: Upgrade libsqlite3-sys
## Description
Upgrade the `libsqlite3-sys` dependency from `0.35.0` to `0.37.0`. The crate is used with `features = ["bundled"]` for static builds.
A previous manual attempt to upgrade failed on compile, so the agent should investigate compatibility with our current `matrix-sdk-sqlite` dependency and resolve any breaking changes.
## Acceptance Criteria
- [ ] `libsqlite3-sys` is upgraded to `0.37.0`
- [ ] `cargo build` succeeds
- [ ] All tests pass

View File

@@ -0,0 +1,60 @@
---
name: "Matrix bot structured conversation history"
agent: coder-opus
---
# Story 266: Matrix bot structured conversation history
## User Story
As a user chatting with the Matrix bot, I want it to remember and own its prior responses naturally, so that conversations feel like talking to one continuous entity rather than a new instance each message.
## Acceptance Criteria
- [ ] Conversation history is passed as structured API messages (user/assistant turns) rather than a flattened text prefix
- [ ] Claude recognises its prior responses as its own, maintaining consistent personality across a conversation
- [ ] Per-room history survives server restarts (persisted to disk or database)
- [ ] Rolling window trimming still applies to keep context bounded
- [ ] Multi-user rooms still attribute messages to the correct sender
## Investigation Notes (2026-03-18)
The current implementation attempts session resumption via `--resume <session_id>` but it's not working:
### Code path: how session resumption is supposed to work
1. `server/src/matrix/bot.rs:671-676``handle_message()` reads `conv.session_id` from the per-room `RoomConversation` to get the resume ID.
2. `server/src/matrix/bot.rs:717` — passes `resume_session_id` to `provider.chat_stream()`.
3. `server/src/llm/providers/claude_code.rs:57``chat_stream()` stores it as `resume_id`.
4. `server/src/llm/providers/claude_code.rs:170-173` — if `resume_session_id` is `Some`, appends `--resume <id>` to the `claude -p` command.
5. `server/src/llm/providers/claude_code.rs:348``process_json_event()` looks for `json["session_id"]` in each streamed NDJSON event and sends it via a oneshot channel (`sid_tx`).
6. `server/src/llm/providers/claude_code.rs:122` — after the PTY exits, `sid_rx.await.ok()` captures the session ID (or `None` if never sent).
7. `server/src/matrix/bot.rs:785-787` — stores `new_session_id` back into `conv.session_id` and persists via `save_history()`.
### What's broken
- **No session_id captured:** `.story_kit/matrix_history.json` contains conversation entries but no `session_id`. `RoomConversation.session_id` is always `None`.
- **Root cause:** `claude -p --output-format stream-json` may not emit a `session_id` in its NDJSON events, or the parser at step 5 isn't matching the actual event shape. The oneshot channel never fires.
- **Effect:** Every message spawns a fresh Claude Code process with no `--resume` flag. Each turn is a blank slate.
- **History persistence works fine** — serialization round-trips correctly (test at `bot.rs:1335-1339`). The problem is purely that `--resume` is never invoked.
### Debugging steps
1. Run `claude -p "hello" --output-format stream-json --verbose 2>/dev/null` manually and inspect the NDJSON for a `session_id` field. Check what event type carries it and whether the key name matches what `process_json_event()` expects.
2. If `session_id` is present but nested differently (e.g. inside an `event` wrapper), fix the JSON path at `claude_code.rs:348`.
3. If `-p` mode doesn't emit `session_id` at all, consider an alternative: pass conversation history as a structured prompt prefix, or switch to the Claude API directly.
### Previous attempt failed (2026-03-18)
A sonnet coder attempted this story but did NOT fix the root cause. It rewrote the `chat_stream()` call in `bot.rs` to look identical to what was already there — it never investigated why `session_id` isn't being captured. The merge auto-resolver then jammed the duplicate call inside the `tokio::select!` permission loop, producing mismatched braces. The broken merge was reverted.
**What the coder must actually do:**
1. **Do NOT rewrite the `chat_stream()` call or the `tokio::select!` loop in `bot.rs`.** That code is correct and handles permission forwarding (story 275). Do not touch it.
2. **The bug is in `claude_code.rs`, not `bot.rs`.** The `process_json_event()` function at line ~348 looks for `json["session_id"]` but it's likely never finding it. Start by running step 1 above to see what the actual NDJSON output looks like.
3. **If `claude -p` doesn't emit `session_id` at all**, the `--resume` approach won't work. In that case, the fix is to pass conversation history as a prompt prefix (prepend prior turns to the user message) or use `--continue` instead of `--resume`, or call the Claude API directly instead of shelling out to the CLI.
4. **Rebase onto current master before starting.** Master has changed significantly (spike 92, story 275 permission handling, gitignore changes).
## Out of Scope
- TBD

View File

@@ -0,0 +1,19 @@
---
name: "Show assigned agent in expanded work item view"
---
# Story 271: Show assigned agent in expanded work item view
## User Story
As a project owner viewing an expanded work item in the web UI, I want to see which agent (e.g. coder-opus) has been assigned via front matter, so that I know which coder is working on or will pick up the story.
## Acceptance Criteria
- [ ] Expanded work item view displays the agent front matter field if set
- [ ] Shows the specific agent name (e.g. 'coder-opus') not just 'assigned'
- [ ] If no agent is set in front matter, the field is omitted or shows unassigned
## Out of Scope
- TBD

View File

@@ -0,0 +1,20 @@
---
name: "Matrix bot sends typing indicator while waiting for Claude response"
---
# Story 273: Matrix bot sends typing indicator while waiting for Claude response
## User Story
As a user chatting with the Matrix bot, I want to see a typing indicator in Element while the bot is processing my message, so that I know it received my request and is working on a response.
## Acceptance Criteria
- [ ] Bot sets m.typing on the room as soon as it starts the Claude API call
- [ ] Typing indicator is cleared when the first response chunk is sent to the room
- [ ] Typing indicator is cleared on error so it doesn't get stuck
- [ ] No visible delay between sending a message and seeing the typing indicator
## Out of Scope
- TBD

View File

@@ -0,0 +1,20 @@
---
name: "MCP pipeline status tool with agent assignments"
---
# Story 274: MCP pipeline status tool with agent assignments
## User Story
As a user checking pipeline status, I want an MCP tool that returns a structured status report including which agent is assigned to each work item, so that I can quickly see what's active and spot stuck items.
## Acceptance Criteria
- [ ] New MCP tool (e.g. `get_pipeline_status`) returns all work items across all active pipeline stages (current, qa, merge, done) with their stage, name, and assigned agent
- [ ] Upcoming backlog items are included with count or listing
- [ ] Agent assignment info comes from story front matter (`agent` field) and/or the running agent list
- [ ] Response is structured/deterministic (not free-form prose)
## Out of Scope
- TBD

View File

@@ -0,0 +1,19 @@
---
name: "Matrix bot uses its configured name instead of \"Claude\""
---
# Story 277: Matrix bot uses its configured name instead of "Claude"
## User Story
As a Matrix user, I want the bot to identify itself by its configured name (e.g., "Timmy") rather than "Claude", so that the bot feels like a distinct personality in the chat.
## Acceptance Criteria
- [ ] The Matrix bot refers to itself by its configured display name (e.g., 'Timmy') in conversations, not 'Claude'
- [ ] The bot's self-referencing name is derived from configuration, not hardcoded
- [ ] If no custom name is configured, the bot falls back to a sensible default
## Out of Scope
- TBD

View File

@@ -0,0 +1,17 @@
---
name: "Auto-assign agents to pipeline items on server startup"
---
# Story 278: Auto-assign agents to pipeline items on server startup
## User Story
As a ..., I want ..., so that ...
## Acceptance Criteria
- [ ] TODO
## Out of Scope
- TBD

View File

@@ -0,0 +1,20 @@
---
name: "Auto-assign should respect agent stage when front matter specifies agent"
---
# Story 279: Auto-assign should respect agent stage when front matter specifies agent
## User Story
As a project operator, I want auto-assign to respect the pipeline stage when a story's front matter specifies a preferred agent, so that a coder agent isn't assigned to do QA work just because the story originally requested that coder.
## Acceptance Criteria
- [ ] When a story in `3_qa/` has `agent: coder-opus` in front matter, auto-assign skips the preferred agent (stage mismatch) and assigns a free QA-stage agent instead
- [ ] When a story in `2_current/` has `agent: coder-opus` in front matter, auto-assign still respects the preference (stage matches)
- [ ] When the preferred agent's stage mismatches, auto-assign logs a message indicating the stage mismatch and fallback
## Out of Scope
- Changing the front matter `agent` field automatically when a story advances stages
- Adding per-stage agent preferences to front matter

View File

@@ -0,0 +1,119 @@
---
name: "Stop auto-committing intermediate pipeline moves"
agent: "coder-opus"
review_hold: true
---
# Spike 92: Stop auto-committing intermediate pipeline moves
## Goal
Determine how to stop the filesystem watcher from auto-committing every pipeline stage move (upcoming -> current -> qa -> merge -> done -> archive) while still committing at terminal states (creation in upcoming, acceptance in done and archived). This keeps git history clean while preserving cross-machine portability for completed work.
## Context
The watcher in `server/src/io/watcher.rs` currently auto-commits every file change in `.story_kit/work/`. A single story run generates 5+ commits just from pipeline moves:
- `story-kit: create 42_story_foo`
- `story-kit: start 42_story_foo`
- `story-kit: queue 42_story_foo for QA`
- `story-kit: queue 42_story_foo for merge`
- `story-kit: accept 42_story_foo`
Since story runs complete relatively quickly, the intermediate state (current/qa/merge) is transient and doesn't need to be committed. Only creation and archival are meaningful checkpoints.
## Questions to Answer
1. Can we filter `stage_metadata()` to only commit for `1_upcoming` and `6_archived` stages while still broadcasting `WatcherEvent`s for all stages (so the frontend stays in sync)?
2. Should we keep `git add -A .story_kit/work/` for the committed stages, or narrow it to only the specific file?
3. What happens if the server crashes mid-pipeline? Uncommitted moves are lost — is this acceptable given the story can just be re-run?
4. Should intermediate moves be `.gitignore`d at the directory level, or is filtering in the watcher sufficient?
5. Do any other parts of the system (agent worktree setup, merge_agent_work, sparse checkout) depend on intermediate pipeline files being committed to master?
## Findings
### Q1: Can we filter to only commit terminal stages?
**Yes.** The fix is in `flush_pending()`, not `stage_metadata()`. We add a `should_commit_stage()` predicate that returns `true` only for `1_upcoming` and `6_archived`. The event broadcast path is decoupled from the commit path — `flush_pending()` always broadcasts a `WatcherEvent` regardless of whether it commits.
Prototype implemented: added `COMMIT_WORTHY_STAGES` constant and `should_commit_stage()` function. The change is ~15 lines including the constant, predicate, and conditional in `flush_pending()`.
### Q2: Keep `git add -A .story_kit/work/` or narrow to specific file?
**Keep `git add -A .story_kit/work/`.** When committing a terminal stage (e.g. `6_archived`), the file has been moved from a previous stage (e.g. `5_done`). Using `-A` on the whole work directory captures both the addition in the new stage and the deletion from the old stage in a single commit. Narrowing to the specific file would miss the deletion side of the move.
### Q3: Server crash mid-pipeline — acceptable?
**Yes.** If the server crashes while a story is in `2_current`, `3_qa`, or `4_merge`, the file is lost from git but:
- The story file still exists on the filesystem (it's just not committed)
- The agent's work is in its own feature branch/worktree (independent of pipeline file state)
- The story can be re-queued from `1_upcoming` which IS committed
- Pipeline state is transient by nature — it reflects "what's happening right now", not permanent record
### Q4: `.gitignore` vs watcher filtering?
**Watcher filtering is sufficient.** `.gitignore` approach (Option C) has downsides:
- `git status` won't show pipeline state, making debugging harder
- If you ever need to commit an intermediate state (e.g. for a new feature), you'd have to fight `.gitignore`
- Watcher filtering is explicit and easy to understand — a constant lists the commit-worthy stages
- No risk of accidentally ignoring files that should be tracked
### Q5: Dependencies on intermediate pipeline commits?
**None found.** Thorough investigation confirmed:
1. **`merge_agent_work`** (`agents/merge.rs`): Creates a temporary `merge-queue/` branch and worktree. Reads the feature branch, not pipeline files. After merge, calls `move_story_to_archived()` which is a filesystem operation.
2. **Agent worktree setup** (`worktree.rs`): Creates worktrees from feature branches. Sparse checkout is a no-op (disabled). Does not read pipeline file state from git.
3. **MCP tool handlers** (`agents/lifecycle.rs`): `move_story_to_current()`, `move_story_to_merge()`, `move_story_to_qa()`, `move_story_to_archived()` — all pure filesystem `fs::rename()` operations. None perform git commits.
4. **Frontend** (`http/workflow.rs`): `load_pipeline_state()` reads directories from the filesystem directly via `fs::read_dir()`. Never calls git. WebSocket events keep the frontend in sync.
5. **No git inspection commands** reference pipeline stage directories anywhere in the codebase.
### Edge Cases
- **Multi-machine sync:** Only `1_upcoming` and `6_archived` are committed. If you push/pull, you'll see story creation and archival but not intermediate pipeline state. This is correct — intermediate state is machine-local runtime state.
- **Manual git operations:** `git status` will show uncommitted files in intermediate stages. This is actually helpful for debugging — you can see what's in the pipeline without grepping git log.
- **Sweep (5_done → 6_archived):** The sweep moves files to `6_archived`, which triggers a watcher event that WILL commit (since `6_archived` is a terminal stage). This naturally captures the final state.
## Approach to Investigate
### Option A: Filter in `flush_pending()` ← **RECOMMENDED**
- In `flush_pending()`, still broadcast the `WatcherEvent` for all stages
- Only call `git_add_work_and_commit()` for stages `1_upcoming` and `6_archived`
- Simplest change — ~15 lines modified in `watcher.rs`
### Option B: Two-tier watcher
- Split into "commit-worthy" events (create, archive) and "notify-only" events (start, qa, merge)
- Commit-worthy events go through git
- Notify-only events just broadcast to WebSocket clients
- More explicit but same end result as Option A
### Option C: .gitignore intermediate directories
- Add `2_current/`, `3_qa/`, `4_merge/` to `.gitignore`
- Watcher still sees events (gitignore doesn't affect filesystem watching)
- Git naturally ignores them
- Risk: harder to debug, `git status` won't show pipeline state
## Recommendation
**Option A is viable and implemented.** The prototype is in `server/src/io/watcher.rs`:
- Added `COMMIT_WORTHY_STAGES` constant: `["1_upcoming", "6_archived"]`
- Added `should_commit_stage()` predicate
- Modified `flush_pending()` to conditionally commit based on stage, while always broadcasting events
- All 872 tests pass, clippy clean
A full story run will now produce only 2 pipeline commits instead of 5+:
- `story-kit: create 42_story_foo` (creation in `1_upcoming`)
- `story-kit: accept 42_story_foo` (archival in `6_archived`)
The intermediate moves (`start`, `queue for QA`, `queue for merge`, `done`) are still broadcast to WebSocket clients for real-time frontend updates, but no longer clutter git history.
## Acceptance Criteria
- [x] Spike document updated with findings and recommendation
- [x] If Option A is viable: prototype the change and verify git log is clean during a full story run
- [x] Confirm frontend still receives real-time pipeline updates for all stages
- [x] Confirm no other system depends on intermediate pipeline commits being on master
- [x] Identify any edge cases (server crash, manual git operations, multi-machine sync)

View File

@@ -0,0 +1,21 @@
---
name: "Matrix bot self-signs device keys at startup for verified encryption"
agent: mergemaster
---
# Story 263: Matrix bot self-signs device keys at startup for verified encryption
## User Story
As a Matrix room participant, I want the bot's messages to not show "encrypted by a device not verified by its owner" warnings, so that I have confidence the bot's encryption is fully verified.
## Acceptance Criteria
- [ ] At startup the bot checks whether its own device keys have been self-signed (cross-signed by its own user identity)
- [ ] If the device keys are not self-signed, the bot signs them automatically
- [ ] After signing, the bot uploads the new signatures to the homeserver
- [ ] After a clean start (fresh matrix_store / device_id) the bot's messages no longer show the 'encrypted by a device not verified by its owner' warning
## Out of Scope
- TBD

View File

@@ -0,0 +1,43 @@
---
name: "Claude Code session ID not persisted across browser refresh"
---
# Bug 264: Claude Code session ID not persisted across browser refresh
## Description
The Claude Code provider uses a session_id to resume conversations via `--resume <id>`. This session_id is stored in React state (`claudeSessionId`) but is NOT persisted to localStorage. After a browser refresh, the session_id is lost (`null`), so Claude Code cannot resume the prior session.
A fallback exists (`build_claude_code_context_prompt` in `server/src/llm/chat.rs:188`) that injects prior messages as flattened text inside a `<conversation_history>` block, but this loses structure (tool calls, tool results, reasoning) and Claude Code treats it as informational text rather than actual conversation turns. In practice, the LLM does not retain meaningful context after refresh.
This is the root cause behind bug 245 (chat history persistence regression). The localStorage message persistence from story 145 works correctly for the UI, but the LLM context is not properly restored because the session cannot be resumed.
Key files:
- `frontend/src/components/Chat.tsx:174``claudeSessionId` is ephemeral React state
- `frontend/src/components/Chat.tsx:553` — session_id only sent when non-null
- `server/src/llm/chat.rs:278` — backend branches on session_id presence
- `server/src/llm/providers/claude_code.rs:44``--resume` flag passed to Claude CLI
## How to Reproduce
1. Open the Story Kit web UI and select claude-code-pty as the model
2. Have a multi-turn conversation with the agent
3. Refresh the browser (F5 or Cmd+R)
4. Send a new message referencing the prior conversation
5. The LLM has no knowledge of the prior conversation
## Actual Result
After refresh, claudeSessionId is null. Claude Code spawns a fresh session without --resume. The fallback text injection is too lossy to provide meaningful context. The LLM behaves as if the conversation never happened.
## Expected Result
After refresh, the Claude Code session is resumed via --resume, giving the LLM full context of the prior conversation including tool calls, reasoning, and all turns.
## Acceptance Criteria
- [ ] claudeSessionId is persisted to localStorage (scoped by project path) and restored on component mount
- [ ] After browser refresh, the next chat message includes session_id in the ProviderConfig
- [ ] Claude Code receives --resume with the persisted session_id after refresh
- [ ] Clearing the session (clear button) also clears the persisted session_id
- [ ] After server restart with session files intact on disk, conversation resumes correctly

View File

@@ -0,0 +1,33 @@
---
name: "Spikes skip merge and stop for human review"
agent: coder-opus
---
# Story 265: Spikes skip merge and stop for human review
## User Story
As a user, I want spike work items to stop after QA instead of auto-advancing to the merge stage, so that I can review the spike's findings and prototype code in the worktree before deciding what to do with them.
## Context
Spikes are investigative — their value is the findings and any prototype code, not a merge to master. The user needs to:
- Read the spike document with findings
- Review prototype code in the worktree
- Optionally build and run the prototype to validate the approach
- Then manually decide: archive the spike and create follow-up stories, or reject and re-investigate
Currently all work items follow the same pipeline: coder → QA → merge → done. Spikes should diverge after QA and wait for human review instead of auto-advancing to merge.
## Acceptance Criteria
- [ ] Items with `_spike_` in the filename skip the merge stage after QA passes
- [ ] After QA, spike items remain accessible for human review (worktree preserved, not cleaned up)
- [ ] Spikes do not auto-advance to `4_merge/` — they stay in `3_qa/` or move to a review-hold state
- [ ] The human can manually archive the spike when done reviewing
- [ ] Non-spike items (stories, bugs, refactors) continue through the full pipeline as before
## Out of Scope
- New UI for spike review (manual file inspection is fine)
- Changes to the spike creation flow

View File

@@ -0,0 +1,19 @@
---
name: "MCP update_story tool should support front matter fields"
---
# Story 267: MCP update_story tool should support front matter fields
## User Story
As an operator using the MCP tools, I want update_story to accept optional front matter fields (like agent, manual_qa, etc.) so that I can update story metadata without editing files by hand.
## Acceptance Criteria
- [ ] update_story MCP tool accepts optional agent parameter to set/change the agent front matter field
- [ ] update_story MCP tool accepts optional arbitrary front matter key-value pairs
- [ ] Front matter updates are auto-committed via the filesystem watcher like other story mutations
## Out of Scope
- TBD

View File

@@ -0,0 +1,23 @@
---
name: "Upgrade tokio-tungstenite to 0.29.0"
---
# Refactor 268: Upgrade tokio-tungstenite to 0.29.0
## Current State
- TBD
## Desired State
Upgrade tokio-tungstenite from 0.28.0 to 0.29.0 in workspace Cargo.toml and fix any breaking API changes.
## Acceptance Criteria
- [ ] tokio-tungstenite = "0.29.0" in workspace Cargo.toml
- [ ] All code compiles without errors
- [ ] All tests pass
## Out of Scope
- TBD

View File

@@ -0,0 +1,22 @@
---
name: "@ file references in web UI chat input"
---
# Story 269: @ file references in web UI chat input
## User Story
As a user chatting in the web UI, I want to type @ to get an autocomplete overlay listing project files, so that I can reference specific files in my messages the same way Zed and Claude Code do.
## Acceptance Criteria
- [ ] Typing @ in the chat input triggers a file picker overlay
- [ ] Overlay searches project files with fuzzy matching as the user types after @
- [ ] Selecting a file inserts a reference into the message (e.g. @path/to/file.rs)
- [ ] The referenced file contents are included as context when the message is sent to the LLM
- [ ] Overlay is dismissable with Escape
- [ ] Multiple @ references can be used in a single message
## Out of Scope
- TBD

View File

@@ -0,0 +1,31 @@
---
name: "QA test server overwrites root .mcp.json with wrong port"
---
# Bug 270: QA test server overwrites root .mcp.json with wrong port
## Description
When the QA agent starts a test server in a worktree (e.g. on port 3012), that server auto-detects the shared project root and calls open_project, which writes .mcp.json with the test server's port. This clobbers the root .mcp.json that should always point to the main server (port 3001).
Root cause: open_project in server/src/io/fs.rs:527 unconditionally calls write_mcp_json(&p, port) with its own port. Because worktrees share .story_kit/ with the real project, the test server resolves to the real project root and overwrites the root .mcp.json instead of writing to its own worktree directory.
Fix: Remove the write_mcp_json call from open_project entirely. Worktree .mcp.json files are already written correctly during worktree creation (worktree.rs:81,97), and the root .mcp.json is committed in git. open_project should not touch it.
## How to Reproduce
1. QA agent starts on a story\n2. QA agent starts a test server in the worktree on a non-default port (e.g. 3012)\n3. Test server auto-opens the project root\n4. Root .mcp.json is overwritten with test port
## Actual Result
Root .mcp.json contains the QA test server's port (e.g. 3012) instead of the main server's port (3001). Interactive Claude sessions lose MCP connectivity.
## Expected Result
Root .mcp.json always points to the primary server's port. Test servers started by QA agents should not overwrite it.
## Acceptance Criteria
- [ ] QA test servers do not overwrite root .mcp.json
- [ ] Root .mcp.json always reflects the primary server's port
- [ ] Worktree .mcp.json files are only written during worktree creation

View File

@@ -0,0 +1,19 @@
---
name: "Clear merge error front matter when story leaves merge stage"
---
# Story 272: Clear merge error front matter when story leaves merge stage
## User Story
As an operator, I want merge error front matter to be automatically removed when a story is moved out of the merge stage via MCP, so that stale error metadata doesn't persist when the story is retried.
## Acceptance Criteria
- [ ] When a story with merge_error front matter is moved out of 4_merge via MCP, the merge_error field is automatically stripped
- [ ] Works for all destinations: back to 2_current, back to 1_upcoming, or forward to 5_done
- [ ] Stories without merge_error front matter are unaffected
## Out of Scope
- TBD

View File

@@ -0,0 +1,22 @@
---
name: "Matrix bot surfaces Claude Code permission prompts to chat"
agent: coder-opus
---
# Story 275: Matrix bot surfaces Claude Code permission prompts to chat
## User Story
As a user chatting with the Matrix bot, I want to see permission prompts from Claude Code in the chat and be able to approve or deny them, so that headless Claude Code sessions don't silently hang when they need authorization to proceed.
## Acceptance Criteria
- [ ] When Claude Code hits a permission prompt during a bot-initiated session, the bot sends the prompt text to the Matrix room as a message
- [ ] The user can approve or deny the permission by replying in chat (e.g. yes/no or a reaction)
- [ ] The bot relays the user decision back to the Claude Code subprocess so execution continues
- [ ] If the user does not respond within a configurable timeout, the permission is denied (fail-closed)
- [ ] The bot does not hang or timeout silently when a permission prompt is pending - the user always sees what is happening
## Out of Scope
- TBD

64
Cargo.lock generated
View File

@@ -1048,12 +1048,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "foldhash"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
[[package]]
name = "form_urlencoded"
version = "1.2.2"
@@ -1313,7 +1307,7 @@ version = "0.15.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
dependencies = [
"foldhash 0.1.5",
"foldhash",
]
[[package]]
@@ -1321,17 +1315,14 @@ name = "hashbrown"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
dependencies = [
"foldhash 0.2.0",
]
[[package]]
name = "hashlink"
version = "0.11.0"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0b22561a9c04a7cb1a302c013e0259cd3b4bb619f145b32f72b8b4bcbed230"
checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1"
dependencies = [
"hashbrown 0.16.1",
"hashbrown 0.15.5",
]
[[package]]
@@ -1957,9 +1948,9 @@ dependencies = [
[[package]]
name = "libsqlite3-sys"
version = "0.37.0"
version = "0.35.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f111c8c41e7c61a49cd34e44c7619462967221a6443b0ec299e0ac30cfb9b1"
checksum = "133c182a6a2c87864fe97778797e46c7e999672690dc9fa3ee8e241aa4a9c13f"
dependencies = [
"cc",
"pkg-config",
@@ -3329,16 +3320,6 @@ dependencies = [
"serde",
]
[[package]]
name = "rsqlite-vfs"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a1f2315036ef6b1fbacd1972e8ee7688030b0a2121edfc2a6550febd41574d"
dependencies = [
"hashbrown 0.16.1",
"thiserror 2.0.18",
]
[[package]]
name = "ruma"
version = "0.14.1"
@@ -3516,7 +3497,9 @@ dependencies = [
[[package]]
name = "rusqlite"
version = "0.37.99"
version = "0.37.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "165ca6e57b20e1351573e3729b958bc62f0e48025386970b6e4d29e7a7e71f3f"
dependencies = [
"bitflags 2.11.0",
"fallible-iterator",
@@ -3524,7 +3507,6 @@ dependencies = [
"hashlink",
"libsqlite3-sys",
"smallvec",
"sqlite-wasm-rs",
]
[[package]]
@@ -3995,18 +3977,6 @@ dependencies = [
"der",
]
[[package]]
name = "sqlite-wasm-rs"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f4206ed3a67690b9c29b77d728f6acc3ce78f16bf846d83c94f76400320181b"
dependencies = [
"cc",
"js-sys",
"rsqlite-vfs",
"wasm-bindgen",
]
[[package]]
name = "sse-codec"
version = "0.3.2"
@@ -4027,7 +3997,7 @@ checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "story-kit"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"async-stream",
"async-trait",
@@ -4048,7 +4018,6 @@ dependencies = [
"portable-pty",
"pulldown-cmark",
"reqwest 0.13.2",
"rusqlite",
"rust-embed",
"serde",
"serde_json",
@@ -4056,7 +4025,7 @@ dependencies = [
"strip-ansi-escapes",
"tempfile",
"tokio",
"tokio-tungstenite 0.28.0",
"tokio-tungstenite 0.29.0",
"toml 1.0.6+spec-1.1.0",
"uuid",
"wait-timeout",
@@ -4364,14 +4333,14 @@ dependencies = [
[[package]]
name = "tokio-tungstenite"
version = "0.28.0"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857"
checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c"
dependencies = [
"futures-util",
"log",
"tokio",
"tungstenite 0.28.0",
"tungstenite 0.29.0",
]
[[package]]
@@ -4593,9 +4562,9 @@ dependencies = [
[[package]]
name = "tungstenite"
version = "0.28.0"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8628dcc84e5a09eb3d8423d6cb682965dea9133204e8fb3efee74c2a0c259442"
checksum = "6c01152af293afb9c7c2a57e4b559c5620b421f6d133261c60dd2d0cdb38e6b8"
dependencies = [
"bytes 1.11.1",
"data-encoding",
@@ -4605,7 +4574,6 @@ dependencies = [
"rand 0.9.2",
"sha1",
"thiserror 2.0.18",
"utf-8",
]
[[package]]

View File

@@ -26,7 +26,7 @@ tempfile = "3"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync"] }
toml = "1.0.6"
uuid = { version = "1.22.0", features = ["v4", "serde"] }
tokio-tungstenite = "0.28.0"
tokio-tungstenite = "0.29.0"
walkdir = "2.5.0"
filetime = "0.2"
matrix-sdk = { version = "0.16.0", default-features = false, features = [
@@ -37,9 +37,3 @@ matrix-sdk = { version = "0.16.0", default-features = false, features = [
pulldown-cmark = { version = "0.13.1", default-features = false, features = [
"html",
] }
[patch.crates-io]
# Patch rusqlite 0.37.x (used by matrix-sdk-sqlite) with a local fork that requires
# libsqlite3-sys 0.37.0 instead of 0.35.0, enabling a single unified libsqlite3-sys
# 0.37.0 in the dependency graph with the "bundled" feature for static builds.
rusqlite = { path = "vendor/rusqlite" }

View File

@@ -115,6 +115,7 @@ export interface WorkItemContent {
content: string;
stage: string;
name: string | null;
agent: string | null;
}
export interface TestCaseResult {
@@ -277,6 +278,9 @@ export const api = {
getHomeDirectory(baseUrl?: string) {
return requestJson<string>("/io/fs/home", {}, baseUrl);
},
listProjectFiles(baseUrl?: string) {
return requestJson<string[]>("/io/fs/files", {}, baseUrl);
},
searchFiles(query: string, baseUrl?: string) {
return requestJson<SearchResult[]>(
"/fs/search",

View File

@@ -26,6 +26,8 @@ type WsHandlers = {
) => void;
};
let capturedWsHandlers: WsHandlers | null = null;
// Captures the last sendChat call's arguments for assertion.
let lastSendChatArgs: { messages: Message[]; config: unknown } | null = null;
vi.mock("../api/client", () => {
const api = {
@@ -36,13 +38,17 @@ vi.mock("../api/client", () => {
setModelPreference: vi.fn(),
cancelChat: vi.fn(),
setAnthropicApiKey: vi.fn(),
readFile: vi.fn(),
listProjectFiles: vi.fn(),
};
class ChatWebSocket {
connect(handlers: WsHandlers) {
capturedWsHandlers = handlers;
}
close() {}
sendChat() {}
sendChat(messages: Message[], config: unknown) {
lastSendChatArgs = { messages, config };
}
cancel() {}
}
return { api, ChatWebSocket };
@@ -56,6 +62,8 @@ const mockedApi = {
setModelPreference: vi.mocked(api.setModelPreference),
cancelChat: vi.mocked(api.cancelChat),
setAnthropicApiKey: vi.mocked(api.setAnthropicApiKey),
readFile: vi.mocked(api.readFile),
listProjectFiles: vi.mocked(api.listProjectFiles),
};
function setupMocks() {
@@ -64,6 +72,8 @@ function setupMocks() {
mockedApi.getAnthropicModels.mockResolvedValue([]);
mockedApi.getModelPreference.mockResolvedValue(null);
mockedApi.setModelPreference.mockResolvedValue(true);
mockedApi.readFile.mockResolvedValue("");
mockedApi.listProjectFiles.mockResolvedValue([]);
mockedApi.cancelChat.mockResolvedValue(true);
mockedApi.setAnthropicApiKey.mockResolvedValue(true);
}
@@ -580,6 +590,63 @@ describe("Chat localStorage persistence (Story 145)", () => {
expect(storedAfterRemount).toEqual(history);
});
it("Bug 245: after refresh, sendChat includes full prior history", async () => {
// Step 1: Render, populate messages via onUpdate, then unmount (simulate refresh)
const { unmount } = render(
<Chat projectPath={PROJECT_PATH} onCloseProject={vi.fn()} />,
);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
const priorHistory: Message[] = [
{ role: "user", content: "What is Rust?" },
{ role: "assistant", content: "Rust is a systems programming language." },
];
act(() => {
capturedWsHandlers?.onUpdate(priorHistory);
});
// Verify localStorage has the prior history
const stored = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? "[]");
expect(stored).toEqual(priorHistory);
unmount();
// Step 2: Remount (simulates page reload) — messages load from localStorage
capturedWsHandlers = null;
lastSendChatArgs = null;
render(<Chat projectPath={PROJECT_PATH} onCloseProject={vi.fn()} />);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
// Verify prior messages are displayed
expect(await screen.findByText("What is Rust?")).toBeInTheDocument();
// Step 3: Send a new message — sendChat should include the full prior history
const input = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(input, { target: { value: "Tell me more" } });
});
await act(async () => {
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
});
// Verify sendChat was called with ALL prior messages + the new one
expect(lastSendChatArgs).not.toBeNull();
const args = lastSendChatArgs as unknown as { messages: Message[]; config: unknown };
expect(args.messages).toHaveLength(3);
expect(args.messages[0]).toEqual({
role: "user",
content: "What is Rust?",
});
expect(args.messages[1]).toEqual({
role: "assistant",
content: "Rust is a systems programming language.",
});
expect(args.messages[2]).toEqual({
role: "user",
content: "Tell me more",
});
});
it("AC5: uses project-scoped storage key", async () => {
const otherKey = "storykit-chat-history:/other/project";
localStorage.setItem(
@@ -1215,3 +1282,168 @@ describe("Remove bubble styling from streaming messages (Story 163)", () => {
expect(styleAttr).not.toContain("background: transparent");
});
});
describe("Bug 264: Claude Code session ID persisted across browser refresh", () => {
const PROJECT_PATH = "/tmp/project";
const SESSION_KEY = `storykit-claude-session-id:${PROJECT_PATH}`;
const STORAGE_KEY = `storykit-chat-history:${PROJECT_PATH}`;
beforeEach(() => {
capturedWsHandlers = null;
lastSendChatArgs = null;
localStorage.clear();
setupMocks();
});
afterEach(() => {
localStorage.clear();
});
it("AC1: session_id is persisted to localStorage when onSessionId fires", async () => {
render(<Chat projectPath={PROJECT_PATH} onCloseProject={vi.fn()} />);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
act(() => {
capturedWsHandlers?.onSessionId("test-session-abc");
});
await waitFor(() => {
expect(localStorage.getItem(SESSION_KEY)).toBe("test-session-abc");
});
});
it("AC2: after remount, next sendChat includes session_id from localStorage", async () => {
// Step 1: Render, receive a session ID, then unmount (simulate refresh)
localStorage.setItem(SESSION_KEY, "persisted-session-xyz");
localStorage.setItem(
STORAGE_KEY,
JSON.stringify([
{ role: "user", content: "Prior message" },
{ role: "assistant", content: "Prior reply" },
]),
);
const { unmount } = render(
<Chat projectPath={PROJECT_PATH} onCloseProject={vi.fn()} />,
);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
unmount();
// Step 2: Remount (simulates page reload)
capturedWsHandlers = null;
lastSendChatArgs = null;
render(<Chat projectPath={PROJECT_PATH} onCloseProject={vi.fn()} />);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
// Prior messages should be visible
expect(await screen.findByText("Prior message")).toBeInTheDocument();
// Step 3: Send a new message — config should include session_id
const input = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(input, { target: { value: "Continue" } });
});
await act(async () => {
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
});
expect(lastSendChatArgs).not.toBeNull();
expect(
((lastSendChatArgs as unknown as { messages: Message[]; config: unknown })?.config as Record<string, unknown>).session_id,
).toBe("persisted-session-xyz");
});
it("AC3: clearing the session also clears the persisted session_id", async () => {
localStorage.setItem(SESSION_KEY, "session-to-clear");
const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(true);
render(<Chat projectPath={PROJECT_PATH} onCloseProject={vi.fn()} />);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
const newSessionBtn = screen.getByText(/New Session/);
await act(async () => {
fireEvent.click(newSessionBtn);
});
expect(localStorage.getItem(SESSION_KEY)).toBeNull();
confirmSpy.mockRestore();
});
it("AC1: storage key is scoped to project path", async () => {
const otherPath = "/other/project";
const otherKey = `storykit-claude-session-id:${otherPath}`;
localStorage.setItem(otherKey, "other-session");
render(<Chat projectPath={PROJECT_PATH} onCloseProject={vi.fn()} />);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
act(() => {
capturedWsHandlers?.onSessionId("my-session");
});
await waitFor(() => {
expect(localStorage.getItem(SESSION_KEY)).toBe("my-session");
});
// Other project's session should be untouched
expect(localStorage.getItem(otherKey)).toBe("other-session");
});
});
describe("File reference expansion (Story 269 AC4)", () => {
beforeEach(() => {
vi.clearAllMocks();
capturedWsHandlers = null;
lastSendChatArgs = null;
setupMocks();
});
it("includes file contents as context when message contains @file reference", async () => {
mockedApi.readFile.mockResolvedValue('fn main() { println!("hello"); }');
render(<Chat projectPath="/tmp/project" onCloseProject={vi.fn()} />);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
const input = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(input, { target: { value: "explain @src/main.rs" } });
});
await act(async () => {
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
});
await waitFor(() => expect(lastSendChatArgs).not.toBeNull());
const sentMessages = (
lastSendChatArgs as NonNullable<typeof lastSendChatArgs>
).messages;
const userMsg = sentMessages[sentMessages.length - 1];
expect(userMsg.content).toContain("explain @src/main.rs");
expect(userMsg.content).toContain("[File: src/main.rs]");
expect(userMsg.content).toContain("fn main()");
});
it("sends message without modification when no @file references are present", async () => {
render(<Chat projectPath="/tmp/project" onCloseProject={vi.fn()} />);
await waitFor(() => expect(capturedWsHandlers).not.toBeNull());
const input = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(input, { target: { value: "hello world" } });
});
await act(async () => {
fireEvent.keyDown(input, { key: "Enter", shiftKey: false });
});
await waitFor(() => expect(lastSendChatArgs).not.toBeNull());
const sentMessages = (
lastSendChatArgs as NonNullable<typeof lastSendChatArgs>
).messages;
const userMsg = sentMessages[sentMessages.length - 1];
expect(userMsg.content).toBe("hello world");
expect(mockedApi.readFile).not.toHaveBeenCalled();
});
});

View File

@@ -171,7 +171,16 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
merge: [],
done: [],
});
const [claudeSessionId, setClaudeSessionId] = useState<string | null>(null);
const [claudeSessionId, setClaudeSessionId] = useState<string | null>(() => {
try {
return (
localStorage.getItem(`storykit-claude-session-id:${projectPath}`) ??
null
);
} catch {
return null;
}
});
const [activityStatus, setActivityStatus] = useState<string | null>(null);
const [permissionQueue, setPermissionQueue] = useState<
{
@@ -247,6 +256,21 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
};
}, [messages, streamingContent, model]);
useEffect(() => {
try {
if (claudeSessionId !== null) {
localStorage.setItem(
`storykit-claude-session-id:${projectPath}`,
claudeSessionId,
);
} else {
localStorage.removeItem(`storykit-claude-session-id:${projectPath}`);
}
} catch {
// Ignore — quota or security errors.
}
}, [claudeSessionId, projectPath]);
useEffect(() => {
api
.getOllamaModels()
@@ -530,7 +554,26 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
}
}
const userMsg: Message = { role: "user", content: messageText };
// Expand @file references: append file contents as context
const fileRefs = [...messageText.matchAll(/(^|[\s\n])@([^\s@]+)/g)].map(
(m) => m[2],
);
let expandedText = messageText;
if (fileRefs.length > 0) {
const expansions = await Promise.allSettled(
fileRefs.map(async (ref) => {
const contents = await api.readFile(ref);
return { ref, contents };
}),
);
for (const result of expansions) {
if (result.status === "fulfilled") {
expandedText += `\n\n[File: ${result.value.ref}]\n\`\`\`\n${result.value.contents}\n\`\`\``;
}
}
}
const userMsg: Message = { role: "user", content: expandedText };
const newHistory = [...messages, userMsg];
setMessages(newHistory);
@@ -664,6 +707,11 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
setLoading(false);
setActivityStatus(null);
setClaudeSessionId(null);
try {
localStorage.removeItem(`storykit-claude-session-id:${projectPath}`);
} catch {
// Ignore — quota or security errors.
}
}
};

View File

@@ -1,6 +1,7 @@
import * as React from "react";
import { api } from "../api/client";
const { forwardRef, useEffect, useImperativeHandle, useRef, useState } = React;
const { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } = React;
export interface ChatInputHandle {
appendToInput(text: string): void;
@@ -14,6 +15,97 @@ interface ChatInputProps {
onRemoveQueuedMessage: (id: string) => void;
}
/** Fuzzy-match: returns true if all chars of `query` appear in order in `str`. */
function fuzzyMatch(str: string, query: string): boolean {
if (!query) return true;
const lower = str.toLowerCase();
const q = query.toLowerCase();
let qi = 0;
for (let i = 0; i < lower.length && qi < q.length; i++) {
if (lower[i] === q[qi]) qi++;
}
return qi === q.length;
}
/** Score a fuzzy match: lower is better. Exact prefix match wins, then shorter paths. */
function fuzzyScore(str: string, query: string): number {
const lower = str.toLowerCase();
const q = query.toLowerCase();
// Prefer matches where query appears as a contiguous substring
if (lower.includes(q)) return lower.indexOf(q);
return str.length;
}
interface FilePickerOverlayProps {
query: string;
files: string[];
selectedIndex: number;
onSelect: (file: string) => void;
onDismiss: () => void;
anchorRef: React.RefObject<HTMLTextAreaElement | null>;
}
function FilePickerOverlay({
query,
files,
selectedIndex,
onSelect,
}: FilePickerOverlayProps) {
const filtered = files
.filter((f) => fuzzyMatch(f, query))
.sort((a, b) => fuzzyScore(a, query) - fuzzyScore(b, query))
.slice(0, 10);
if (filtered.length === 0) return null;
return (
<div
data-testid="file-picker-overlay"
style={{
position: "absolute",
bottom: "100%",
left: 0,
right: 0,
background: "#1e1e1e",
border: "1px solid #444",
borderRadius: "8px",
marginBottom: "6px",
overflow: "hidden",
zIndex: 100,
boxShadow: "0 4px 16px rgba(0,0,0,0.4)",
maxHeight: "240px",
overflowY: "auto",
}}
>
{filtered.map((file, idx) => (
<button
key={file}
type="button"
data-testid={`file-picker-item-${idx}`}
onClick={() => onSelect(file)}
style={{
display: "block",
width: "100%",
textAlign: "left",
padding: "8px 14px",
background: idx === selectedIndex ? "#2d4a6e" : "transparent",
border: "none",
color: idx === selectedIndex ? "#ececec" : "#aaa",
cursor: "pointer",
fontFamily: "monospace",
fontSize: "0.85rem",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
}}
>
{file}
</button>
))}
</div>
);
}
export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
function ChatInput(
{ loading, queuedMessages, onSubmit, onCancel, onRemoveQueuedMessage },
@@ -22,6 +114,12 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
const [input, setInput] = useState("");
const inputRef = useRef<HTMLTextAreaElement>(null);
// File picker state
const [projectFiles, setProjectFiles] = useState<string[]>([]);
const [pickerQuery, setPickerQuery] = useState<string | null>(null);
const [pickerSelectedIndex, setPickerSelectedIndex] = useState(0);
const [pickerAtStart, setPickerAtStart] = useState(0);
useImperativeHandle(ref, () => ({
appendToInput(text: string) {
setInput((prev) => (prev ? `${prev}\n${text}` : text));
@@ -32,10 +130,104 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
inputRef.current?.focus();
}, []);
// Compute filtered files for current picker query
const filteredFiles = pickerQuery !== null
? projectFiles
.filter((f) => fuzzyMatch(f, pickerQuery))
.sort((a, b) => fuzzyScore(a, pickerQuery) - fuzzyScore(b, pickerQuery))
.slice(0, 10)
: [];
const dismissPicker = useCallback(() => {
setPickerQuery(null);
setPickerSelectedIndex(0);
}, []);
const selectFile = useCallback(
(file: string) => {
// Replace the @query portion with @file
const before = input.slice(0, pickerAtStart);
const cursorPos = inputRef.current?.selectionStart ?? input.length;
const after = input.slice(cursorPos);
setInput(`${before}@${file}${after}`);
dismissPicker();
// Restore focus after state update
setTimeout(() => inputRef.current?.focus(), 0);
},
[input, pickerAtStart, dismissPicker],
);
const handleInputChange = useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
const val = e.target.value;
setInput(val);
const cursor = e.target.selectionStart ?? val.length;
// Find the last @ before the cursor that starts a reference token
const textUpToCursor = val.slice(0, cursor);
// Match @ not preceded by non-whitespace (i.e. @ at start or after space/newline)
const atMatch = textUpToCursor.match(/(^|[\s\n])@([^\s@]*)$/);
if (atMatch) {
const query = atMatch[2];
const atPos = textUpToCursor.lastIndexOf("@");
setPickerAtStart(atPos);
setPickerQuery(query);
setPickerSelectedIndex(0);
// Lazily load files on first trigger
if (projectFiles.length === 0) {
api.listProjectFiles().then(setProjectFiles).catch(() => {});
}
} else {
if (pickerQuery !== null) dismissPicker();
}
},
[projectFiles.length, pickerQuery, dismissPicker],
);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (pickerQuery !== null && filteredFiles.length > 0) {
if (e.key === "ArrowDown") {
e.preventDefault();
setPickerSelectedIndex((i) => Math.min(i + 1, filteredFiles.length - 1));
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
setPickerSelectedIndex((i) => Math.max(i - 1, 0));
return;
}
if (e.key === "Enter" || e.key === "Tab") {
e.preventDefault();
selectFile(filteredFiles[pickerSelectedIndex] ?? filteredFiles[0]);
return;
}
if (e.key === "Escape") {
e.preventDefault();
dismissPicker();
return;
}
} else if (e.key === "Escape" && pickerQuery !== null) {
e.preventDefault();
dismissPicker();
return;
}
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit();
}
},
[pickerQuery, filteredFiles, pickerSelectedIndex, selectFile, dismissPicker],
);
const handleSubmit = () => {
if (!input.trim()) return;
onSubmit(input);
setInput("");
dismissPicker();
};
return (
@@ -135,24 +327,30 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
</button>
</div>
))}
{/* Input row */}
{/* Input row with file picker overlay */}
<div
style={{
display: "flex",
gap: "8px",
alignItems: "center",
position: "relative",
}}
>
{pickerQuery !== null && (
<FilePickerOverlay
query={pickerQuery}
files={projectFiles}
selectedIndex={pickerSelectedIndex}
onSelect={selectFile}
onDismiss={dismissPicker}
anchorRef={inputRef}
/>
)}
<textarea
ref={inputRef}
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit();
}
}}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
placeholder="Send a message..."
rows={1}
style={{

View File

@@ -0,0 +1,194 @@
import {
act,
fireEvent,
render,
screen,
waitFor,
} from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { api } from "../api/client";
import { ChatInput } from "./ChatInput";
vi.mock("../api/client", () => ({
api: {
listProjectFiles: vi.fn(),
},
}));
const mockedListProjectFiles = vi.mocked(api.listProjectFiles);
const defaultProps = {
loading: false,
queuedMessages: [],
onSubmit: vi.fn(),
onCancel: vi.fn(),
onRemoveQueuedMessage: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
mockedListProjectFiles.mockResolvedValue([
"src/main.rs",
"src/lib.rs",
"frontend/index.html",
"README.md",
]);
});
describe("File picker overlay (Story 269 AC1)", () => {
it("shows file picker overlay when @ is typed", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(textarea, { target: { value: "@" } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-overlay")).toBeInTheDocument();
});
});
it("does not show file picker overlay for text without @", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(textarea, { target: { value: "hello world" } });
});
expect(screen.queryByTestId("file-picker-overlay")).not.toBeInTheDocument();
});
});
describe("File picker fuzzy matching (Story 269 AC2)", () => {
it("filters files by query typed after @", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(textarea, { target: { value: "@main" } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-overlay")).toBeInTheDocument();
});
// main.rs should be visible, README.md should not
expect(screen.getByText("src/main.rs")).toBeInTheDocument();
expect(screen.queryByText("README.md")).not.toBeInTheDocument();
});
it("shows all files when @ is typed with no query", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(textarea, { target: { value: "@" } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-overlay")).toBeInTheDocument();
});
// All 4 files should be visible
expect(screen.getByText("src/main.rs")).toBeInTheDocument();
expect(screen.getByText("src/lib.rs")).toBeInTheDocument();
expect(screen.getByText("README.md")).toBeInTheDocument();
});
});
describe("File picker selection (Story 269 AC3)", () => {
it("clicking a file inserts @path into the message", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(textarea, { target: { value: "@" } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-item-0")).toBeInTheDocument();
});
await act(async () => {
fireEvent.click(screen.getByTestId("file-picker-item-0"));
});
// Picker should be dismissed and the file reference inserted
expect(screen.queryByTestId("file-picker-overlay")).not.toBeInTheDocument();
expect((textarea as HTMLTextAreaElement).value).toMatch(/^@\S+/);
});
it("Enter key selects highlighted file and inserts it into message", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(textarea, { target: { value: "@main" } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-overlay")).toBeInTheDocument();
});
await act(async () => {
fireEvent.keyDown(textarea, { key: "Enter" });
});
expect(screen.queryByTestId("file-picker-overlay")).not.toBeInTheDocument();
expect((textarea as HTMLTextAreaElement).value).toContain("@src/main.rs");
});
});
describe("File picker dismiss (Story 269 AC5)", () => {
it("Escape key dismisses the file picker", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
await act(async () => {
fireEvent.change(textarea, { target: { value: "@" } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-overlay")).toBeInTheDocument();
});
await act(async () => {
fireEvent.keyDown(textarea, { key: "Escape" });
});
expect(screen.queryByTestId("file-picker-overlay")).not.toBeInTheDocument();
});
});
describe("Multiple @ references (Story 269 AC6)", () => {
it("typing @ after a completed reference triggers picker again", async () => {
render(<ChatInput {...defaultProps} />);
const textarea = screen.getByPlaceholderText("Send a message...");
// First reference
await act(async () => {
fireEvent.change(textarea, { target: { value: "@main" } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-overlay")).toBeInTheDocument();
});
// Select file
await act(async () => {
fireEvent.keyDown(textarea, { key: "Enter" });
});
// Type a second @
await act(async () => {
const current = (textarea as HTMLTextAreaElement).value;
fireEvent.change(textarea, { target: { value: `${current} @` } });
});
await waitFor(() => {
expect(screen.getByTestId("file-picker-overlay")).toBeInTheDocument();
});
});
});

View File

@@ -37,6 +37,7 @@ const DEFAULT_CONTENT = {
content: "# Big Title\n\nSome content here.",
stage: "current",
name: "Big Title Story",
agent: null,
};
const sampleTestResults: TestResultsResponse = {
@@ -436,6 +437,60 @@ describe("WorkItemDetailPanel - Agent Logs", () => {
});
});
describe("WorkItemDetailPanel - Assigned Agent", () => {
it("shows assigned agent name when agent front matter field is set", async () => {
mockedGetWorkItemContent.mockResolvedValue({
...DEFAULT_CONTENT,
agent: "coder-opus",
});
render(
<WorkItemDetailPanel
storyId="271_story_test"
pipelineVersion={0}
onClose={() => {}}
/>,
);
const agentEl = await screen.findByTestId("detail-panel-assigned-agent");
expect(agentEl).toHaveTextContent("coder-opus");
});
it("omits assigned agent field when no agent is set in front matter", async () => {
render(
<WorkItemDetailPanel
storyId="271_story_test"
pipelineVersion={0}
onClose={() => {}}
/>,
);
await screen.findByTestId("detail-panel-content");
expect(
screen.queryByTestId("detail-panel-assigned-agent"),
).not.toBeInTheDocument();
});
it("shows the specific agent name not just 'assigned'", async () => {
mockedGetWorkItemContent.mockResolvedValue({
...DEFAULT_CONTENT,
agent: "coder-haiku",
});
render(
<WorkItemDetailPanel
storyId="271_story_test"
pipelineVersion={0}
onClose={() => {}}
/>,
);
const agentEl = await screen.findByTestId("detail-panel-assigned-agent");
expect(agentEl).toHaveTextContent("coder-haiku");
expect(agentEl).not.toHaveTextContent("assigned");
});
});
describe("WorkItemDetailPanel - Test Results", () => {
it("shows empty test results message when no results exist", async () => {
mockedGetTestResults.mockResolvedValue(null);

View File

@@ -113,6 +113,7 @@ export function WorkItemDetailPanel({
const [content, setContent] = useState<string | null>(null);
const [stage, setStage] = useState<string>("");
const [name, setName] = useState<string | null>(null);
const [assignedAgent, setAssignedAgent] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [agentInfo, setAgentInfo] = useState<AgentInfo | null>(null);
@@ -133,6 +134,7 @@ export function WorkItemDetailPanel({
setContent(data.content);
setStage(data.stage);
setName(data.name);
setAssignedAgent(data.agent);
})
.catch((err: unknown) => {
setError(err instanceof Error ? err.message : "Failed to load content");
@@ -278,6 +280,14 @@ export function WorkItemDetailPanel({
{stageLabel}
</div>
)}
{assignedAgent ? (
<div
data-testid="detail-panel-assigned-agent"
style={{ fontSize: "0.75em", color: "#888" }}
>
Agent: {assignedAgent}
</div>
) : null}
</div>
<button
type="button"

View File

@@ -16,8 +16,25 @@ export default defineConfig(() => {
"/api": {
target: `http://127.0.0.1:${String(backendPort)}`,
timeout: 120000,
configure: (proxy) => {
proxy.on("error", (_err) => {
// Swallow proxy errors (e.g. ECONNREFUSED during backend restart)
// so the vite dev server doesn't crash.
});
},
},
},
watch: {
ignored: [
"**/.story_kit/**",
"**/target/**",
"**/.git/**",
"**/server/**",
"**/Cargo.*",
"**/vendor/**",
"**/node_modules/**",
],
},
},
build: {
outDir: "dist",

View File

@@ -71,20 +71,93 @@ ls -lh "${DIST}"/
echo "==> Generating changelog..."
PREV_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -n "$PREV_TAG" ]; then
CHANGELOG=$(git log "${PREV_TAG}..HEAD" --pretty=format:"- %s" --no-merges)
LOG_RANGE="${PREV_TAG}..HEAD"
RANGE="${PREV_TAG}...${TAG}"
else
CHANGELOG=$(git log --pretty=format:"- %s" --no-merges)
LOG_RANGE=""
RANGE="initial...${TAG}"
fi
if [ -z "$CHANGELOG" ]; then
CHANGELOG="- No changes since last release"
# Extract completed stories/bugs/refactors from "story-kit: merge <id>" commits.
# Deduplicate (a story may have been merged more than once after reverts).
if [ -n "$LOG_RANGE" ]; then
MERGED_RAW=$(git log "$LOG_RANGE" --pretty=format:"%s" --no-merges \
| grep "^story-kit: merge " | sed 's/^story-kit: merge //' | sort -u)
else
MERGED_RAW=$(git log --pretty=format:"%s" --no-merges \
| grep "^story-kit: merge " | sed 's/^story-kit: merge //' | sort -u)
fi
RELEASE_BODY="## What's Changed
# Categorise merged work items and format names.
FEATURES=""
FIXES=""
REFACTORS=""
while IFS= read -r item; do
[ -z "$item" ] && continue
# Strip the numeric prefix and type to get the human name.
name=$(echo "$item" | sed -E 's/^[0-9]+_(story|bug|refactor|spike)_//' | tr '_' ' ')
# Capitalise first letter.
name="$(echo "${name:0:1}" | tr '[:lower:]' '[:upper:]')${name:1}"
case "$item" in
*_bug_*) FIXES="${FIXES}- ${name}\n" ;;
*_refactor_*) REFACTORS="${REFACTORS}- ${name}\n" ;;
*) FEATURES="${FEATURES}- ${name}\n" ;;
esac
done <<< "$MERGED_RAW"
${CHANGELOG}
# Collect non-automation manual commits (direct fixes, version bumps, etc).
if [ -n "$LOG_RANGE" ]; then
MANUAL=$(git log "$LOG_RANGE" --pretty=format:"%s" --no-merges \
| grep -v "^story-kit: " \
| grep -v "^Revert \"story-kit: " \
| grep -v "^Bump version" \
| sed 's/^/- /')
else
MANUAL=$(git log --pretty=format:"%s" --no-merges \
| grep -v "^story-kit: " \
| grep -v "^Revert \"story-kit: " \
| grep -v "^Bump version" \
| sed 's/^/- /')
fi
# Assemble the release body.
RELEASE_BODY="## What's Changed"
if [ -n "$FEATURES" ]; then
RELEASE_BODY="${RELEASE_BODY}
### Features
$(echo -e "$FEATURES")"
fi
if [ -n "$FIXES" ]; then
RELEASE_BODY="${RELEASE_BODY}
### Bug Fixes
$(echo -e "$FIXES")"
fi
if [ -n "$REFACTORS" ]; then
RELEASE_BODY="${RELEASE_BODY}
### Refactors
$(echo -e "$REFACTORS")"
fi
if [ -n "$MANUAL" ]; then
RELEASE_BODY="${RELEASE_BODY}
### Other Changes
${MANUAL}"
fi
if [ -z "$FEATURES" ] && [ -z "$FIXES" ] && [ -z "$REFACTORS" ] && [ -z "$MANUAL" ]; then
RELEASE_BODY="${RELEASE_BODY}
- No changes since last release"
fi
RELEASE_BODY="${RELEASE_BODY}
**Full diff:** ${GITEA_URL}/${REPO}/compare/${RANGE}"

View File

@@ -8,8 +8,12 @@ echo "=== Running Rust tests ==="
cargo test --manifest-path "$PROJECT_ROOT/Cargo.toml"
echo "=== Running frontend unit tests ==="
cd "$PROJECT_ROOT/frontend"
npm test
if [ -d "$PROJECT_ROOT/frontend" ]; then
cd "$PROJECT_ROOT/frontend"
npm test
else
echo "Skipping frontend tests (no frontend directory)"
fi
# Disabled: e2e tests may be causing merge pipeline hangs (no running server
# in merge workspace → Playwright blocks indefinitely). Re-enable once confirmed.

View File

@@ -1,6 +1,6 @@
[package]
name = "story-kit"
version = "0.1.0"
version = "0.3.0"
edition = "2024"
build = "build.rs"
@@ -32,10 +32,7 @@ matrix-sdk = { workspace = true }
pulldown-cmark = { workspace = true }
# Force bundled SQLite so static musl builds don't need a system libsqlite3
libsqlite3-sys = { version = "0.37.0", features = ["bundled"] }
# Enable fallible_uint feature to restore u64/usize ToSql/FromSql impls needed
# by matrix-sdk-sqlite (removed in rusqlite 0.38+ without this feature flag)
rusqlite = { version = "0.37.99", features = ["fallible_uint"] }
libsqlite3-sys = { version = "0.35.0", features = ["bundled"] }
wait-timeout = "0.2.1"
[dev-dependencies]

View File

@@ -4,8 +4,7 @@ use std::process::Command;
use crate::io::story_metadata::clear_front_matter_field;
use crate::slog;
#[allow(dead_code)]
fn item_type_from_id(item_id: &str) -> &'static str {
pub(super) fn item_type_from_id(item_id: &str) -> &'static str {
// New format: {digits}_{type}_{slug}
let after_num = item_id.trim_start_matches(|c: char| c.is_ascii_digit());
if after_num.starts_with("_bug_") {

View File

@@ -889,21 +889,39 @@ impl AgentPool {
};
if coverage_passed {
slog!(
"[pipeline] QA passed gates and coverage for '{story_id}'. Moving to merge."
);
if let Err(e) = super::lifecycle::move_story_to_merge(&project_root, story_id) {
slog_error!("[pipeline] Failed to move '{story_id}' to 4_merge/: {e}");
return;
// Spikes skip merge — they stay in 3_qa/ for human review.
if super::lifecycle::item_type_from_id(story_id) == "spike" {
// Mark the spike as held for review so auto-assign won't
// restart QA on it.
let qa_dir = project_root.join(".story_kit/work/3_qa");
let spike_path = qa_dir.join(format!("{story_id}.md"));
if let Err(e) = crate::io::story_metadata::write_review_hold(&spike_path) {
slog_error!("[pipeline] Failed to set review_hold on '{story_id}': {e}");
}
slog!(
"[pipeline] QA passed for spike '{story_id}'. \
Stopping for human review (skipping merge). \
Worktree preserved at: {worktree_path:?}"
);
// Free up the QA slot without advancing the spike.
self.auto_assign_available_work(&project_root).await;
} else {
slog!(
"[pipeline] QA passed gates and coverage for '{story_id}'. Moving to merge."
);
if let Err(e) = super::lifecycle::move_story_to_merge(&project_root, story_id) {
slog_error!("[pipeline] Failed to move '{story_id}' to 4_merge/: {e}");
return;
}
if let Err(e) = self
.start_agent(&project_root, story_id, Some("mergemaster"), None)
.await
{
slog_error!("[pipeline] Failed to start mergemaster for '{story_id}': {e}");
}
// QA slot is now free — pick up any other unassigned work in 3_qa/.
self.auto_assign_available_work(&project_root).await;
}
if let Err(e) = self
.start_agent(&project_root, story_id, Some("mergemaster"), None)
.await
{
slog_error!("[pipeline] Failed to start mergemaster for '{story_id}': {e}");
}
// QA slot is now free — pick up any other unassigned work in 3_qa/.
self.auto_assign_available_work(&project_root).await;
} else {
slog!(
"[pipeline] QA coverage gate failed for '{story_id}'. Restarting QA."
@@ -1275,11 +1293,6 @@ impl AgentPool {
.and_then(|jobs| jobs.get(story_id).cloned())
}
/// Return the port this server is running on.
pub fn port(&self) -> u16 {
self.port
}
/// Get project root helper.
pub fn get_project_root(
&self,
@@ -1444,15 +1457,23 @@ impl AgentPool {
}
for story_id in &items {
// Items marked with review_hold (e.g. spikes after QA passes) stay
// in their current stage for human review — don't auto-assign agents.
if has_review_hold(project_root, stage_dir, story_id) {
continue;
}
// Re-acquire the lock on each iteration to see state changes
// from previous start_agent calls in the same pass.
let preferred_agent =
read_story_front_matter_agent(project_root, stage_dir, story_id);
// Outcome: (already_assigned, chosen_agent, preferred_busy)
// Outcome: (already_assigned, chosen_agent, preferred_busy, stage_mismatch)
// preferred_busy=true means the story has a specific agent requested but it is
// currently occupied — the story should wait rather than fall back.
let (already_assigned, free_agent, preferred_busy) = {
// stage_mismatch=true means the preferred agent's stage doesn't match the
// pipeline stage, so we fell back to a generic stage agent.
let (already_assigned, free_agent, preferred_busy, stage_mismatch) = {
let agents = match self.agents.lock() {
Ok(a) => a,
Err(e) => {
@@ -1462,18 +1483,29 @@ impl AgentPool {
};
let assigned = is_story_assigned_for_stage(&config, &agents, story_id, stage);
if assigned {
(true, None, false)
(true, None, false, false)
} else if let Some(ref pref) = preferred_agent {
// Story has a front-matter agent preference.
if is_agent_free(&agents, pref) {
(false, Some(pref.clone()), false)
// Verify the preferred agent's stage matches the current
// pipeline stage — a coder shouldn't be assigned to QA.
let pref_stage_matches = config
.find_agent(pref)
.map(|cfg| agent_config_stage(cfg) == *stage)
.unwrap_or(false);
if !pref_stage_matches {
// Stage mismatch — fall back to any free agent for this stage.
let free = find_free_agent_for_stage(&config, &agents, stage)
.map(|s| s.to_string());
(false, free, false, true)
} else if is_agent_free(&agents, pref) {
(false, Some(pref.clone()), false, false)
} else {
(false, None, true)
(false, None, true, false)
}
} else {
let free = find_free_agent_for_stage(&config, &agents, stage)
.map(|s| s.to_string());
(false, free, false)
(false, free, false, false)
}
};
@@ -1492,6 +1524,13 @@ impl AgentPool {
continue;
}
if stage_mismatch {
slog!(
"[auto-assign] Preferred agent '{}' stage mismatch for '{story_id}' in {stage_dir}/; falling back to stage-appropriate agent.",
preferred_agent.as_deref().unwrap_or("?")
);
}
match free_agent {
Some(agent_name) => {
slog!(
@@ -1707,7 +1746,25 @@ impl AgentPool {
};
if coverage_passed {
if let Err(e) = super::lifecycle::move_story_to_merge(project_root, story_id) {
// Spikes skip the merge stage — stay in 3_qa/ for human review.
if super::lifecycle::item_type_from_id(story_id) == "spike" {
let spike_path = project_root
.join(".story_kit/work/3_qa")
.join(format!("{story_id}.md"));
if let Err(e) = crate::io::story_metadata::write_review_hold(&spike_path) {
eprintln!(
"[startup:reconcile] Failed to set review_hold on spike '{story_id}': {e}"
);
}
eprintln!(
"[startup:reconcile] Spike '{story_id}' passed QA — holding for human review."
);
let _ = progress_tx.send(ReconciliationEvent {
story_id: story_id.clone(),
status: "review_hold".to_string(),
message: "Spike passed QA — waiting for human review.".to_string(),
});
} else if let Err(e) = super::lifecycle::move_story_to_merge(project_root, story_id) {
eprintln!(
"[startup:reconcile] Failed to move '{story_id}' to 4_merge/: {e}"
);
@@ -1922,6 +1979,24 @@ fn read_story_front_matter_agent(project_root: &Path, stage_dir: &str, story_id:
parse_front_matter(&contents).ok()?.agent
}
/// Return `true` if the story file in the given stage has `review_hold: true` in its front matter.
fn has_review_hold(project_root: &Path, stage_dir: &str, story_id: &str) -> bool {
use crate::io::story_metadata::parse_front_matter;
let path = project_root
.join(".story_kit")
.join("work")
.join(stage_dir)
.join(format!("{story_id}.md"));
let contents = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(_) => return false,
};
parse_front_matter(&contents)
.ok()
.and_then(|m| m.review_hold)
.unwrap_or(false)
}
/// Return `true` if `agent_name` has no active (pending/running) entry in the pool.
fn is_agent_free(agents: &HashMap<String, StoryAgent>, agent_name: &str) -> bool {
!agents.values().any(|a| {
@@ -4621,4 +4696,202 @@ stage = "coder"
"story should be in 2_current/ or 3_qa/ after reconciliation"
);
}
#[test]
fn has_review_hold_returns_true_when_set() {
let tmp = tempfile::tempdir().unwrap();
let qa_dir = tmp.path().join(".story_kit/work/3_qa");
std::fs::create_dir_all(&qa_dir).unwrap();
let spike_path = qa_dir.join("10_spike_research.md");
std::fs::write(
&spike_path,
"---\nname: Research spike\nreview_hold: true\n---\n# Spike\n",
)
.unwrap();
assert!(has_review_hold(tmp.path(), "3_qa", "10_spike_research"));
}
#[test]
fn has_review_hold_returns_false_when_not_set() {
let tmp = tempfile::tempdir().unwrap();
let qa_dir = tmp.path().join(".story_kit/work/3_qa");
std::fs::create_dir_all(&qa_dir).unwrap();
let spike_path = qa_dir.join("10_spike_research.md");
std::fs::write(
&spike_path,
"---\nname: Research spike\n---\n# Spike\n",
)
.unwrap();
assert!(!has_review_hold(tmp.path(), "3_qa", "10_spike_research"));
}
#[test]
fn has_review_hold_returns_false_when_file_missing() {
let tmp = tempfile::tempdir().unwrap();
assert!(!has_review_hold(tmp.path(), "3_qa", "99_spike_missing"));
}
/// Story 265: auto_assign_available_work must skip spikes in 3_qa/ that
/// have review_hold: true set in their front matter.
#[tokio::test]
async fn auto_assign_skips_spikes_with_review_hold() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
// Create project.toml with a QA agent.
let sk = root.join(".story_kit");
std::fs::create_dir_all(&sk).unwrap();
std::fs::write(
sk.join("project.toml"),
"[[agents]]\nname = \"qa\"\nrole = \"qa\"\nmodel = \"test\"\nprompt = \"test\"\n",
)
.unwrap();
// Put a spike in 3_qa/ with review_hold: true.
let qa_dir = root.join(".story_kit/work/3_qa");
std::fs::create_dir_all(&qa_dir).unwrap();
std::fs::write(
qa_dir.join("20_spike_test.md"),
"---\nname: Test Spike\nreview_hold: true\n---\n# Spike\n",
)
.unwrap();
let (watcher_tx, _) = broadcast::channel::<WatcherEvent>(4);
let pool = AgentPool::new(3001, watcher_tx);
pool.auto_assign_available_work(root).await;
// No agent should have been started for the spike.
let agents = pool.agents.lock().unwrap();
assert!(
agents.is_empty(),
"No agents should be assigned to a spike with review_hold"
);
}
// ── Story 279: auto-assign respects agent stage from front matter ──────────
/// When a story in 3_qa/ has `agent: coder-1` in its front matter but
/// coder-1 is a coder-stage agent, auto-assign must NOT assign coder-1.
/// Instead it should fall back to a free QA-stage agent.
#[tokio::test]
async fn auto_assign_ignores_coder_preference_when_story_is_in_qa_stage() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
let qa_dir = sk.join("work/3_qa");
std::fs::create_dir_all(&qa_dir).unwrap();
std::fs::write(
sk.join("project.toml"),
"[[agent]]\nname = \"coder-1\"\nstage = \"coder\"\n\n\
[[agent]]\nname = \"qa-1\"\nstage = \"qa\"\n",
)
.unwrap();
// Story in 3_qa/ with a preferred coder-stage agent.
std::fs::write(
qa_dir.join("story-qa1.md"),
"---\nname: QA Story\nagent: coder-1\n---\n",
)
.unwrap();
let pool = AgentPool::new_test(3001);
pool.auto_assign_available_work(tmp.path()).await;
let agents = pool.agents.lock().unwrap();
// coder-1 must NOT have been assigned (wrong stage for 3_qa/).
let coder_assigned = agents
.values()
.any(|a| a.agent_name == "coder-1" && matches!(a.status, AgentStatus::Pending | AgentStatus::Running));
assert!(
!coder_assigned,
"coder-1 should not be assigned to a QA-stage story"
);
// qa-1 should have been assigned instead.
let qa_assigned = agents
.values()
.any(|a| a.agent_name == "qa-1" && matches!(a.status, AgentStatus::Pending | AgentStatus::Running));
assert!(
qa_assigned,
"qa-1 should be assigned as fallback for the QA-stage story"
);
}
/// When a story in 2_current/ has `agent: coder-1` in its front matter and
/// coder-1 is a coder-stage agent, auto-assign must respect the preference
/// and assign coder-1 (not fall back to some other coder).
#[tokio::test]
async fn auto_assign_respects_coder_preference_when_story_is_in_current_stage() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
let current_dir = sk.join("work/2_current");
std::fs::create_dir_all(&current_dir).unwrap();
std::fs::write(
sk.join("project.toml"),
"[[agent]]\nname = \"coder-1\"\nstage = \"coder\"\n\n\
[[agent]]\nname = \"coder-2\"\nstage = \"coder\"\n",
)
.unwrap();
// Story in 2_current/ with a preferred coder-1 agent.
std::fs::write(
current_dir.join("story-pref.md"),
"---\nname: Coder Story\nagent: coder-1\n---\n",
)
.unwrap();
let pool = AgentPool::new_test(3001);
pool.auto_assign_available_work(tmp.path()).await;
let agents = pool.agents.lock().unwrap();
// coder-1 should have been picked (it matches the stage and is preferred).
let coder1_assigned = agents
.values()
.any(|a| a.agent_name == "coder-1" && matches!(a.status, AgentStatus::Pending | AgentStatus::Running));
assert!(
coder1_assigned,
"coder-1 should be assigned when it matches the stage and is preferred"
);
// coder-2 must NOT be assigned (not preferred).
let coder2_assigned = agents
.values()
.any(|a| a.agent_name == "coder-2" && matches!(a.status, AgentStatus::Pending | AgentStatus::Running));
assert!(
!coder2_assigned,
"coder-2 should not be assigned when coder-1 is explicitly preferred"
);
}
/// When the preferred agent's stage mismatches and no other agent of the
/// correct stage is available, auto-assign must not start any agent for that
/// story (no panic, no error).
#[tokio::test]
async fn auto_assign_stage_mismatch_with_no_fallback_starts_no_agent() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
let qa_dir = sk.join("work/3_qa");
std::fs::create_dir_all(&qa_dir).unwrap();
// Only a coder agent is configured — no QA agent exists.
std::fs::write(
sk.join("project.toml"),
"[[agent]]\nname = \"coder-1\"\nstage = \"coder\"\n",
)
.unwrap();
// Story in 3_qa/ requests coder-1 (wrong stage) and no QA agent exists.
std::fs::write(
qa_dir.join("story-noqa.md"),
"---\nname: QA Story No Agent\nagent: coder-1\n---\n",
)
.unwrap();
let pool = AgentPool::new_test(3001);
// Must not panic.
pool.auto_assign_available_work(tmp.path()).await;
let agents = pool.agents.lock().unwrap();
assert!(
agents.is_empty(),
"No agent should be started when no stage-appropriate agent is available"
);
}
}

View File

@@ -68,6 +68,7 @@ struct WorkItemContentResponse {
content: String,
stage: String,
name: Option<String>,
agent: Option<String>,
}
/// A single test case result for the OpenAPI response.
@@ -354,13 +355,14 @@ impl AgentsApi {
if file_path.exists() {
let content = std::fs::read_to_string(&file_path)
.map_err(|e| bad_request(format!("Failed to read work item: {e}")))?;
let name = crate::io::story_metadata::parse_front_matter(&content)
.ok()
.and_then(|m| m.name);
let metadata = crate::io::story_metadata::parse_front_matter(&content).ok();
let name = metadata.as_ref().and_then(|m| m.name.clone());
let agent = metadata.and_then(|m| m.agent);
return Ok(Json(WorkItemContentResponse {
content,
stage: stage_name.to_string(),
name,
agent,
}));
}
}

View File

@@ -103,6 +103,15 @@ impl IoApi {
Ok(Json(home))
}
/// List all files in the project recursively, respecting .gitignore.
#[oai(path = "/io/fs/files", method = "get")]
async fn list_project_files(&self) -> OpenApiResult<Json<Vec<String>>> {
let files = io_fs::list_project_files(&self.ctx.state)
.await
.map_err(bad_request)?;
Ok(Json(files))
}
/// Search the currently open project for files containing the provided query string.
#[oai(path = "/io/search", method = "post")]
async fn search_files(
@@ -316,6 +325,53 @@ mod tests {
);
}
// --- list_project_files ---
#[tokio::test]
async fn list_project_files_returns_file_paths() {
let dir = TempDir::new().unwrap();
std::fs::create_dir(dir.path().join("src")).unwrap();
std::fs::write(dir.path().join("src/main.rs"), "fn main() {}").unwrap();
std::fs::write(dir.path().join("README.md"), "# readme").unwrap();
let api = make_api(&dir);
let result = api.list_project_files().await.unwrap();
let files = &result.0;
assert!(files.contains(&"README.md".to_string()));
assert!(files.contains(&"src/main.rs".to_string()));
}
#[tokio::test]
async fn list_project_files_excludes_directories() {
let dir = TempDir::new().unwrap();
std::fs::create_dir(dir.path().join("subdir")).unwrap();
std::fs::write(dir.path().join("file.txt"), "").unwrap();
let api = make_api(&dir);
let result = api.list_project_files().await.unwrap();
let files = &result.0;
assert!(files.contains(&"file.txt".to_string()));
// Directories should not appear
assert!(!files.iter().any(|f| f == "subdir"));
}
#[tokio::test]
async fn list_project_files_returns_sorted_paths() {
let dir = TempDir::new().unwrap();
std::fs::write(dir.path().join("z_last.txt"), "").unwrap();
std::fs::write(dir.path().join("a_first.txt"), "").unwrap();
let api = make_api(&dir);
let result = api.list_project_files().await.unwrap();
let files = &result.0;
let a_idx = files.iter().position(|f| f == "a_first.txt").unwrap();
let z_idx = files.iter().position(|f| f == "z_last.txt").unwrap();
assert!(a_idx < z_idx);
}
// --- list_directory (project-scoped) ---
#[tokio::test]
@@ -345,4 +401,5 @@ mod tests {
let result = api.list_directory(payload).await;
assert!(result.is_err());
}
}

View File

@@ -8,7 +8,7 @@ use crate::http::settings::get_editor_command_from_store;
use crate::http::workflow::{
add_criterion_to_file, check_criterion_in_file, create_bug_file, create_refactor_file,
create_spike_file, create_story_file, list_bug_files, list_refactor_files,
load_upcoming_stories, update_story_in_file, validate_story_dirs,
load_pipeline_state, load_upcoming_stories, update_story_in_file, validate_story_dirs,
};
use crate::worktree;
use crate::io::story_metadata::{parse_front_matter, parse_unchecked_todos, write_merge_failure};
@@ -19,6 +19,7 @@ use poem::web::Data;
use poem::{Body, Request, Response};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::fs;
use std::sync::Arc;
@@ -638,7 +639,7 @@ fn handle_tools_list(id: Option<Value>) -> JsonRpcResponse {
},
{
"name": "update_story",
"description": "Update the user story text and/or description of an existing story file. Replaces the content of the '## User Story' and/or '## Description' section in place. Auto-commits via the filesystem watcher.",
"description": "Update an existing story file. Can replace the '## User Story' and/or '## Description' section content, and/or set YAML front matter fields (e.g. agent, manual_qa). Auto-commits via the filesystem watcher.",
"inputSchema": {
"type": "object",
"properties": {
@@ -653,6 +654,17 @@ fn handle_tools_list(id: Option<Value>) -> JsonRpcResponse {
"description": {
"type": "string",
"description": "New description text to replace the '## Description' section content"
},
"agent": {
"type": "string",
"description": "Set or change the 'agent' YAML front matter field"
},
"front_matter": {
"type": "object",
"description": "Arbitrary YAML front matter key-value pairs to set or update",
"additionalProperties": {
"type": "string"
}
}
},
"required": ["story_id"]
@@ -850,6 +862,14 @@ fn handle_tools_list(id: Option<Value>) -> JsonRpcResponse {
"required": ["story_id"]
}
},
{
"name": "get_pipeline_status",
"description": "Return a structured snapshot of the full work item pipeline. Includes all active stages (current, qa, merge, done) with each item's stage, name, and assigned agent. Also includes upcoming backlog items.",
"inputSchema": {
"type": "object",
"properties": {}
}
},
{
"name": "get_server_logs",
"description": "Return recent server log lines captured in the in-process ring buffer. Useful for diagnosing runtime behaviour such as WebSocket events, MCP call flow, and filesystem watcher activity.",
@@ -951,6 +971,8 @@ async fn handle_tools_call(
"report_merge_failure" => tool_report_merge_failure(&args, ctx),
// QA tools
"request_qa" => tool_request_qa(&args, ctx).await,
// Pipeline status
"get_pipeline_status" => tool_get_pipeline_status(ctx),
// Diagnostics
"get_server_logs" => tool_get_server_logs(&args),
// Permission bridge (Claude Code → frontend dialog)
@@ -1032,6 +1054,47 @@ fn tool_list_upcoming(ctx: &AppContext) -> Result<String, String> {
.map_err(|e| format!("Serialization error: {e}"))
}
fn tool_get_pipeline_status(ctx: &AppContext) -> Result<String, String> {
let state = load_pipeline_state(ctx)?;
fn map_items(items: &[crate::http::workflow::UpcomingStory], stage: &str) -> Vec<Value> {
items
.iter()
.map(|s| {
json!({
"story_id": s.story_id,
"name": s.name,
"stage": stage,
"agent": s.agent.as_ref().map(|a| json!({
"agent_name": a.agent_name,
"model": a.model,
"status": a.status,
})),
})
})
.collect()
}
let mut active: Vec<Value> = Vec::new();
active.extend(map_items(&state.current, "current"));
active.extend(map_items(&state.qa, "qa"));
active.extend(map_items(&state.merge, "merge"));
active.extend(map_items(&state.done, "done"));
let upcoming: Vec<Value> = state
.upcoming
.iter()
.map(|s| json!({ "story_id": s.story_id, "name": s.name }))
.collect();
serde_json::to_string_pretty(&json!({
"active": active,
"upcoming": upcoming,
"upcoming_count": upcoming.len(),
}))
.map_err(|e| format!("Serialization error: {e}"))
}
fn tool_get_story_todos(args: &Value, ctx: &AppContext) -> Result<String, String> {
let story_id = args
.get("story_id")
@@ -1544,8 +1607,24 @@ fn tool_update_story(args: &Value, ctx: &AppContext) -> Result<String, String> {
let user_story = args.get("user_story").and_then(|v| v.as_str());
let description = args.get("description").and_then(|v| v.as_str());
// Collect front matter fields: explicit `agent` param + arbitrary `front_matter` object.
let mut front_matter: HashMap<String, String> = HashMap::new();
if let Some(agent) = args.get("agent").and_then(|v| v.as_str()) {
front_matter.insert("agent".to_string(), agent.to_string());
}
if let Some(obj) = args.get("front_matter").and_then(|v| v.as_object()) {
for (k, v) in obj {
let val = match v {
Value::String(s) => s.clone(),
other => other.to_string(),
};
front_matter.insert(k.clone(), val);
}
}
let front_matter_opt = if front_matter.is_empty() { None } else { Some(&front_matter) };
let root = ctx.state.get_project_root()?;
update_story_in_file(&root, story_id, user_story, description)?;
update_story_in_file(&root, story_id, user_story, description, front_matter_opt)?;
Ok(format!("Updated story '{story_id}'."))
}
@@ -1788,6 +1867,13 @@ fn tool_report_merge_failure(args: &Value, ctx: &AppContext) -> Result<String, S
slog!("[mergemaster] Merge failure reported for '{story_id}': {reason}");
ctx.agents.set_merge_failure_reported(story_id);
// Broadcast the failure so the Matrix notification listener can post an
// error message to configured rooms without coupling this tool to the bot.
let _ = ctx.watcher_tx.send(crate::io::watcher::WatcherEvent::MergeFailure {
story_id: story_id.to_string(),
reason: reason.to_string(),
});
// Persist the failure reason to the story file's front matter so it
// survives server restarts and is visible in the web UI.
if let Ok(project_root) = ctx.state.get_project_root() {
@@ -2195,7 +2281,8 @@ mod tests {
assert!(names.contains(&"request_qa"));
assert!(names.contains(&"get_server_logs"));
assert!(names.contains(&"prompt_permission"));
assert_eq!(tools.len(), 34);
assert!(names.contains(&"get_pipeline_status"));
assert_eq!(tools.len(), 35);
}
#[test]
@@ -2262,6 +2349,81 @@ mod tests {
assert!(result.unwrap_err().contains("Missing required argument"));
}
#[test]
fn tool_get_pipeline_status_returns_structured_response() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
for (stage, id, name) in &[
("1_upcoming", "10_story_upcoming", "Upcoming Story"),
("2_current", "20_story_current", "Current Story"),
("3_qa", "30_story_qa", "QA Story"),
("4_merge", "40_story_merge", "Merge Story"),
("5_done", "50_story_done", "Done Story"),
] {
let dir = root.join(".story_kit/work").join(stage);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join(format!("{id}.md")),
format!("---\nname: \"{name}\"\n---\n"),
)
.unwrap();
}
let ctx = test_ctx(root);
let result = tool_get_pipeline_status(&ctx).unwrap();
let parsed: Value = serde_json::from_str(&result).unwrap();
// Active stages include current, qa, merge, done
let active = parsed["active"].as_array().unwrap();
assert_eq!(active.len(), 4);
let stages: Vec<&str> = active.iter().map(|i| i["stage"].as_str().unwrap()).collect();
assert!(stages.contains(&"current"));
assert!(stages.contains(&"qa"));
assert!(stages.contains(&"merge"));
assert!(stages.contains(&"done"));
// Upcoming backlog
let upcoming = parsed["upcoming"].as_array().unwrap();
assert_eq!(upcoming.len(), 1);
assert_eq!(upcoming[0]["story_id"], "10_story_upcoming");
assert_eq!(parsed["upcoming_count"], 1);
}
#[test]
fn tool_get_pipeline_status_includes_agent_assignment() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let current = root.join(".story_kit/work/2_current");
std::fs::create_dir_all(&current).unwrap();
std::fs::write(
current.join("20_story_active.md"),
"---\nname: \"Active Story\"\n---\n",
)
.unwrap();
let ctx = test_ctx(root);
ctx.agents.inject_test_agent(
"20_story_active",
"coder-1",
crate::agents::AgentStatus::Running,
);
let result = tool_get_pipeline_status(&ctx).unwrap();
let parsed: Value = serde_json::from_str(&result).unwrap();
let active = parsed["active"].as_array().unwrap();
assert_eq!(active.len(), 1);
let item = &active[0];
assert_eq!(item["story_id"], "20_story_active");
assert_eq!(item["stage"], "current");
assert!(!item["agent"].is_null(), "agent should be present");
assert_eq!(item["agent"]["agent_name"], "coder-1");
assert_eq!(item["agent"]["status"], "running");
}
#[test]
fn tool_get_story_todos_missing_file() {
let tmp = tempfile::tempdir().unwrap();

View File

@@ -39,7 +39,6 @@ impl ProjectApi {
payload.0.path,
&self.ctx.state,
self.ctx.store.as_ref(),
self.ctx.agents.port(),
)
.await
.map_err(bad_request)?;

View File

@@ -1,6 +1,6 @@
use crate::agents::AgentStatus;
use crate::http::context::AppContext;
use crate::io::story_metadata::{parse_front_matter, write_coverage_baseline};
use crate::io::story_metadata::{parse_front_matter, set_front_matter_field, write_coverage_baseline};
use crate::workflow::{StoryTestResults, TestCaseResult, TestStatus};
use serde::Serialize;
use std::collections::HashMap;
@@ -706,10 +706,13 @@ pub fn update_story_in_file(
story_id: &str,
user_story: Option<&str>,
description: Option<&str>,
front_matter: Option<&HashMap<String, String>>,
) -> Result<(), String> {
if user_story.is_none() && description.is_none() {
let has_front_matter_updates = front_matter.map(|m| !m.is_empty()).unwrap_or(false);
if user_story.is_none() && description.is_none() && !has_front_matter_updates {
return Err(
"At least one of 'user_story' or 'description' must be provided.".to_string(),
"At least one of 'user_story', 'description', or 'front_matter' must be provided."
.to_string(),
);
}
@@ -717,6 +720,13 @@ pub fn update_story_in_file(
let mut contents = fs::read_to_string(&filepath)
.map_err(|e| format!("Failed to read story file: {e}"))?;
if let Some(fields) = front_matter {
for (key, value) in fields {
let yaml_value = format!("\"{}\"", value.replace('"', "\\\"").replace('\n', " ").replace('\r', ""));
contents = set_front_matter_field(&contents, key, &yaml_value);
}
}
if let Some(us) = user_story {
contents = replace_section_content(&contents, "User Story", us)?;
}
@@ -1597,7 +1607,7 @@ mod tests {
let content = "---\nname: T\n---\n\n## User Story\n\nOld text\n\n## Acceptance Criteria\n\n- [ ] AC\n";
fs::write(&filepath, content).unwrap();
update_story_in_file(tmp.path(), "20_test", Some("New user story text"), None).unwrap();
update_story_in_file(tmp.path(), "20_test", Some("New user story text"), None, None).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("New user story text"), "new text should be present");
@@ -1614,7 +1624,7 @@ mod tests {
let content = "---\nname: T\n---\n\n## Description\n\nOld description\n\n## Acceptance Criteria\n\n- [ ] AC\n";
fs::write(&filepath, content).unwrap();
update_story_in_file(tmp.path(), "21_test", None, Some("New description")).unwrap();
update_story_in_file(tmp.path(), "21_test", None, Some("New description"), None).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("New description"), "new description present");
@@ -1628,7 +1638,7 @@ mod tests {
fs::create_dir_all(&current).unwrap();
fs::write(current.join("22_test.md"), "---\nname: T\n---\n").unwrap();
let result = update_story_in_file(tmp.path(), "22_test", None, None);
let result = update_story_in_file(tmp.path(), "22_test", None, None, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("At least one"));
}
@@ -1644,11 +1654,65 @@ mod tests {
)
.unwrap();
let result = update_story_in_file(tmp.path(), "23_test", Some("new text"), None);
let result = update_story_in_file(tmp.path(), "23_test", Some("new text"), None, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("User Story"));
}
#[test]
fn update_story_sets_agent_front_matter_field() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".story_kit/work/2_current");
fs::create_dir_all(&current).unwrap();
let filepath = current.join("24_test.md");
fs::write(&filepath, "---\nname: T\n---\n\n## User Story\n\nSome story\n").unwrap();
let mut fields = HashMap::new();
fields.insert("agent".to_string(), "dev".to_string());
update_story_in_file(tmp.path(), "24_test", None, None, Some(&fields)).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("agent: \"dev\""), "agent field should be set");
assert!(result.contains("name: T"), "name field preserved");
}
#[test]
fn update_story_sets_arbitrary_front_matter_fields() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".story_kit/work/2_current");
fs::create_dir_all(&current).unwrap();
let filepath = current.join("25_test.md");
fs::write(&filepath, "---\nname: T\n---\n\n## User Story\n\nSome story\n").unwrap();
let mut fields = HashMap::new();
fields.insert("manual_qa".to_string(), "true".to_string());
fields.insert("priority".to_string(), "high".to_string());
update_story_in_file(tmp.path(), "25_test", None, None, Some(&fields)).unwrap();
let result = fs::read_to_string(&filepath).unwrap();
assert!(result.contains("manual_qa: \"true\""), "manual_qa field should be set");
assert!(result.contains("priority: \"high\""), "priority field should be set");
assert!(result.contains("name: T"), "name field preserved");
}
#[test]
fn update_story_front_matter_only_no_section_required() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".story_kit/work/2_current");
fs::create_dir_all(&current).unwrap();
// File without a User Story section — front matter update should succeed
let filepath = current.join("26_test.md");
fs::write(&filepath, "---\nname: T\n---\n\nNo sections here.\n").unwrap();
let mut fields = HashMap::new();
fields.insert("agent".to_string(), "dev".to_string());
let result = update_story_in_file(tmp.path(), "26_test", None, None, Some(&fields));
assert!(result.is_ok(), "front-matter-only update should not require body sections");
let contents = fs::read_to_string(&filepath).unwrap();
assert!(contents.contains("agent: \"dev\""));
}
// ── Bug file helper tests ──────────────────────────────────────────────────
#[test]

View File

@@ -150,6 +150,9 @@ impl From<WatcherEvent> for Option<WsResponse> {
}),
WatcherEvent::ConfigChanged => Some(WsResponse::AgentConfigChanged),
WatcherEvent::AgentStateChanged => Some(WsResponse::AgentStateChanged),
// MergeFailure is handled by the Matrix notification listener only;
// no WebSocket message is needed for the frontend.
WatcherEvent::MergeFailure { .. } => None,
}
}
}

View File

@@ -1,6 +1,5 @@
use crate::state::SessionState;
use crate::store::StoreOps;
use crate::worktree::write_mcp_json as worktree_write_mcp_json;
use serde::Serialize;
use serde_json::json;
use std::fs;
@@ -515,17 +514,12 @@ pub async fn open_project(
path: String,
state: &SessionState,
store: &dyn StoreOps,
port: u16,
) -> Result<String, String> {
let p = PathBuf::from(&path);
ensure_project_root_with_story_kit(p.clone()).await?;
validate_project_path(p.clone()).await?;
// Write .mcp.json so that claude-code can connect to the MCP server.
// Best-effort: failure should not prevent the project from opening.
let _ = worktree_write_mcp_json(&p, port);
{
// TRACE:MERGE-DEBUG — remove once root cause is found
crate::slog!("[MERGE-DEBUG] open_project: setting project_root to {:?}", p);
@@ -727,6 +721,42 @@ pub async fn create_directory_absolute(path: String) -> Result<bool, String> {
.map_err(|e| format!("Task failed: {}", e))?
}
/// List all files in the project recursively, respecting .gitignore.
/// Returns relative paths from the project root (files only, not directories).
pub async fn list_project_files(state: &SessionState) -> Result<Vec<String>, String> {
let root = state.get_project_root()?;
list_project_files_impl(root).await
}
pub async fn list_project_files_impl(root: PathBuf) -> Result<Vec<String>, String> {
use ignore::WalkBuilder;
let root_clone = root.clone();
let files = tokio::task::spawn_blocking(move || {
let mut result = Vec::new();
let walker = WalkBuilder::new(&root_clone).git_ignore(true).build();
for entry in walker.flatten() {
if entry.file_type().map(|ft| ft.is_file()).unwrap_or(false) {
let relative = entry
.path()
.strip_prefix(&root_clone)
.unwrap_or(entry.path())
.to_string_lossy()
.to_string();
result.push(relative);
}
}
result.sort();
result
})
.await
.map_err(|e| format!("Task failed: {e}"))?;
Ok(files)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -777,7 +807,6 @@ mod tests {
project_dir.to_string_lossy().to_string(),
&state,
&store,
3001,
)
.await;
@@ -787,7 +816,11 @@ mod tests {
}
#[tokio::test]
async fn open_project_writes_mcp_json_to_project_root() {
async fn open_project_does_not_write_mcp_json() {
// open_project must NOT overwrite .mcp.json — test servers started by QA
// agents share the real project root, so writing here would clobber the
// root .mcp.json with the wrong port. .mcp.json is written once during
// worktree creation (worktree.rs) and should not be touched again.
let dir = tempdir().unwrap();
let project_dir = dir.path().join("myproject");
fs::create_dir_all(&project_dir).unwrap();
@@ -798,17 +831,14 @@ mod tests {
project_dir.to_string_lossy().to_string(),
&state,
&store,
4242,
)
.await
.unwrap();
let mcp_path = project_dir.join(".mcp.json");
assert!(mcp_path.exists(), ".mcp.json should be written to project root");
let content = fs::read_to_string(&mcp_path).unwrap();
assert!(
content.contains("http://localhost:4242/mcp"),
".mcp.json should contain the correct port"
!mcp_path.exists(),
"open_project must not write .mcp.json — that would overwrite the root with the wrong port"
);
}
@@ -868,7 +898,6 @@ mod tests {
project_dir.to_string_lossy().to_string(),
&state,
&store,
3001,
)
.await
.unwrap();
@@ -1329,7 +1358,6 @@ mod tests {
project_dir.to_string_lossy().to_string(),
&state,
&store,
0,
)
.await
.unwrap();
@@ -1353,7 +1381,6 @@ mod tests {
project_dir.to_string_lossy().to_string(),
&state,
&store,
0,
)
.await
.unwrap();
@@ -1588,4 +1615,68 @@ mod tests {
"scaffold should not overwrite existing project.toml"
);
}
// --- list_project_files_impl ---
#[tokio::test]
async fn list_project_files_returns_all_files() {
let dir = tempdir().unwrap();
fs::create_dir(dir.path().join("src")).unwrap();
fs::write(dir.path().join("src/main.rs"), "fn main() {}").unwrap();
fs::write(dir.path().join("README.md"), "# readme").unwrap();
let files = list_project_files_impl(dir.path().to_path_buf())
.await
.unwrap();
assert!(files.contains(&"README.md".to_string()));
assert!(files.contains(&"src/main.rs".to_string()));
}
#[tokio::test]
async fn list_project_files_excludes_dirs_from_output() {
let dir = tempdir().unwrap();
fs::create_dir(dir.path().join("subdir")).unwrap();
fs::write(dir.path().join("file.txt"), "").unwrap();
let files = list_project_files_impl(dir.path().to_path_buf())
.await
.unwrap();
assert!(files.contains(&"file.txt".to_string()));
assert!(!files.iter().any(|f| f == "subdir"));
}
#[tokio::test]
async fn list_project_files_returns_sorted() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("z.txt"), "").unwrap();
fs::write(dir.path().join("a.txt"), "").unwrap();
let files = list_project_files_impl(dir.path().to_path_buf())
.await
.unwrap();
let a_idx = files.iter().position(|f| f == "a.txt").unwrap();
let z_idx = files.iter().position(|f| f == "z.txt").unwrap();
assert!(a_idx < z_idx);
}
#[tokio::test]
async fn list_project_files_with_state() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("hello.rs"), "").unwrap();
let state = make_state_with_root(dir.path().to_path_buf());
let files = list_project_files(&state).await.unwrap();
assert!(files.contains(&"hello.rs".to_string()));
}
#[tokio::test]
async fn list_project_files_errors_without_project() {
let state = SessionState::default();
let result = list_project_files(&state).await;
assert!(result.is_err());
}
}

View File

@@ -8,6 +8,7 @@ pub struct StoryMetadata {
pub coverage_baseline: Option<String>,
pub merge_failure: Option<String>,
pub agent: Option<String>,
pub review_hold: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -31,6 +32,7 @@ struct FrontMatter {
coverage_baseline: Option<String>,
merge_failure: Option<String>,
agent: Option<String>,
review_hold: Option<bool>,
}
pub fn parse_front_matter(contents: &str) -> Result<StoryMetadata, StoryMetaError> {
@@ -64,6 +66,7 @@ fn build_metadata(front: FrontMatter) -> StoryMetadata {
coverage_baseline: front.coverage_baseline,
merge_failure: front.merge_failure,
agent: front.agent,
review_hold: front.review_hold,
}
}
@@ -98,6 +101,17 @@ pub fn write_merge_failure(path: &Path, reason: &str) -> Result<(), String> {
Ok(())
}
/// Write `review_hold: true` to the YAML front matter of a story file.
///
/// Used to mark spikes that have passed QA and are waiting for human review.
pub fn write_review_hold(path: &Path) -> Result<(), String> {
let contents =
fs::read_to_string(path).map_err(|e| format!("Failed to read story file: {e}"))?;
let updated = set_front_matter_field(&contents, "review_hold", "true");
fs::write(path, &updated).map_err(|e| format!("Failed to write story file: {e}"))?;
Ok(())
}
/// Remove a key from the YAML front matter of a story file on disk.
///
/// If front matter is present and contains the key, the line is removed.
@@ -147,7 +161,7 @@ fn remove_front_matter_field(contents: &str, key: &str) -> String {
/// Insert or update a key: value pair in the YAML front matter of a markdown string.
///
/// If no front matter (opening `---`) is found, returns the content unchanged.
fn set_front_matter_field(contents: &str, key: &str, value: &str) -> String {
pub fn set_front_matter_field(contents: &str, key: &str, value: &str) -> String {
let mut lines: Vec<String> = contents.lines().map(String::from).collect();
if lines.is_empty() || lines[0].trim() != "---" {
return contents.to_string();
@@ -328,4 +342,29 @@ workflow: tdd
let input = " - [ ] Indented item\n";
assert_eq!(parse_unchecked_todos(input), vec!["Indented item"]);
}
#[test]
fn parses_review_hold_from_front_matter() {
let input = "---\nname: Spike\nreview_hold: true\n---\n# Spike\n";
let meta = parse_front_matter(input).expect("front matter");
assert_eq!(meta.review_hold, Some(true));
}
#[test]
fn review_hold_defaults_to_none() {
let input = "---\nname: Story\n---\n# Story\n";
let meta = parse_front_matter(input).expect("front matter");
assert_eq!(meta.review_hold, None);
}
#[test]
fn write_review_hold_sets_field() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("spike.md");
std::fs::write(&path, "---\nname: My Spike\n---\n# Spike\n").unwrap();
write_review_hold(&path).unwrap();
let contents = std::fs::read_to_string(&path).unwrap();
assert!(contents.contains("review_hold: true"));
assert!(contents.contains("name: My Spike"));
}
}

View File

@@ -20,6 +20,7 @@
//! the event so connected clients stay in sync.
use crate::config::{ProjectConfig, WatcherConfig};
use crate::io::story_metadata::clear_front_matter_field;
use crate::slog;
use notify::{EventKind, RecommendedWatcher, RecursiveMode, Watcher, recommended_watcher};
use serde::Serialize;
@@ -50,6 +51,14 @@ pub enum WatcherEvent {
/// Triggers a pipeline state refresh so the frontend can update agent
/// assignments without waiting for a filesystem event.
AgentStateChanged,
/// A story encountered a failure (e.g. merge failure).
/// Triggers an error notification to configured Matrix rooms.
MergeFailure {
/// Work item ID (e.g. `"42_story_my_feature"`).
story_id: String,
/// Human-readable description of the failure.
reason: String,
},
}
/// Return `true` if `path` is the root-level `.story_kit/project.toml`, i.e.
@@ -146,11 +155,25 @@ fn git_add_work_and_commit(git_root: &Path, message: &str) -> Result<bool, Strin
Err(format!("git commit failed: {stderr}"))
}
/// Stages that represent meaningful git checkpoints (creation and archival).
/// Intermediate stages (current, qa, merge, done) are transient pipeline state
/// that don't need to be committed — they're only relevant while the server is
/// running and are broadcast to WebSocket clients for real-time UI updates.
const COMMIT_WORTHY_STAGES: &[&str] = &["1_upcoming", "5_done", "6_archived"];
/// Return `true` if changes in `stage` should be committed to git.
fn should_commit_stage(stage: &str) -> bool {
COMMIT_WORTHY_STAGES.contains(&stage)
}
/// Process a batch of pending (path → stage) entries: commit and broadcast.
///
/// Only files that still exist on disk are used to derive the commit message
/// (they represent the destination of a move or a new file). Deletions are
/// captured by `git add -A .story_kit/work/` automatically.
///
/// Only terminal stages (`1_upcoming` and `6_archived`) trigger git commits.
/// All stages broadcast a [`WatcherEvent`] so the frontend stays in sync.
fn flush_pending(
pending: &HashMap<PathBuf, String>,
git_root: &Path,
@@ -182,27 +205,46 @@ fn flush_pending(
("remove", item.to_string(), format!("story-kit: remove {item}"))
};
slog!("[watcher] flush: {commit_msg}");
match git_add_work_and_commit(git_root, &commit_msg) {
Ok(committed) => {
if committed {
slog!("[watcher] committed: {commit_msg}");
} else {
slog!("[watcher] skipped (already committed): {commit_msg}");
}
let stage = additions.first().map_or("unknown", |(_, s)| s);
let evt = WatcherEvent::WorkItem {
stage: stage.to_string(),
item_id,
action: action.to_string(),
commit_msg,
};
let _ = event_tx.send(evt);
}
Err(e) => {
slog!("[watcher] git error: {e}");
// Strip stale merge_failure front matter from any story that has left 4_merge/.
for (path, stage) in &additions {
if *stage != "4_merge"
&& let Err(e) = clear_front_matter_field(path, "merge_failure")
{
slog!("[watcher] Warning: could not clear merge_failure from {}: {e}", path.display());
}
}
// Only commit for terminal stages; intermediate moves are broadcast-only.
let dest_stage = additions.first().map_or("unknown", |(_, s)| *s);
let should_commit = should_commit_stage(dest_stage);
if should_commit {
slog!("[watcher] flush: {commit_msg}");
match git_add_work_and_commit(git_root, &commit_msg) {
Ok(committed) => {
if committed {
slog!("[watcher] committed: {commit_msg}");
} else {
slog!("[watcher] skipped (already committed): {commit_msg}");
}
}
Err(e) => {
slog!("[watcher] git error: {e}");
return;
}
}
} else {
slog!("[watcher] flush (broadcast-only): {commit_msg}");
}
// Always broadcast the event so connected WebSocket clients stay in sync.
let evt = WatcherEvent::WorkItem {
stage: dest_stage.to_string(),
item_id,
action: action.to_string(),
commit_msg,
};
let _ = event_tx.send(evt);
}
/// Scan `work/5_done/` and move any `.md` files whose mtime is older than
@@ -529,7 +571,50 @@ mod tests {
// ── flush_pending ─────────────────────────────────────────────────────────
#[test]
fn flush_pending_commits_and_broadcasts_work_item_for_addition() {
fn flush_pending_commits_and_broadcasts_for_terminal_stage() {
let tmp = TempDir::new().unwrap();
init_git_repo(tmp.path());
let stage_dir = make_stage_dir(tmp.path(), "1_upcoming");
let story_path = stage_dir.join("42_story_foo.md");
fs::write(&story_path, "---\nname: test\n---\n").unwrap();
let (tx, mut rx) = tokio::sync::broadcast::channel(16);
let mut pending = HashMap::new();
pending.insert(story_path, "1_upcoming".to_string());
flush_pending(&pending, tmp.path(), &tx);
let evt = rx.try_recv().expect("expected a broadcast event");
match evt {
WatcherEvent::WorkItem {
stage,
item_id,
action,
commit_msg,
} => {
assert_eq!(stage, "1_upcoming");
assert_eq!(item_id, "42_story_foo");
assert_eq!(action, "create");
assert_eq!(commit_msg, "story-kit: create 42_story_foo");
}
other => panic!("unexpected event: {other:?}"),
}
// Verify the file was actually committed.
let log = std::process::Command::new("git")
.args(["log", "--oneline", "-1"])
.current_dir(tmp.path())
.output()
.expect("git log");
let log_msg = String::from_utf8_lossy(&log.stdout);
assert!(
log_msg.contains("story-kit: create 42_story_foo"),
"terminal stage should produce a git commit"
);
}
#[test]
fn flush_pending_broadcasts_without_commit_for_intermediate_stage() {
let tmp = TempDir::new().unwrap();
init_git_repo(tmp.path());
let stage_dir = make_stage_dir(tmp.path(), "2_current");
@@ -542,6 +627,7 @@ mod tests {
flush_pending(&pending, tmp.path(), &tx);
// Event should still be broadcast for frontend sync.
let evt = rx.try_recv().expect("expected a broadcast event");
match evt {
WatcherEvent::WorkItem {
@@ -557,6 +643,18 @@ mod tests {
}
other => panic!("unexpected event: {other:?}"),
}
// Verify NO git commit was made (only the initial empty commit should exist).
let log = std::process::Command::new("git")
.args(["log", "--oneline"])
.current_dir(tmp.path())
.output()
.expect("git log");
let log_msg = String::from_utf8_lossy(&log.stdout);
assert!(
!log_msg.contains("story-kit:"),
"intermediate stage should NOT produce a git commit"
);
}
#[test]
@@ -582,6 +680,7 @@ mod tests {
flush_pending(&pending, tmp.path(), &tx);
// All stages should broadcast events regardless of commit behavior.
let evt = rx.try_recv().expect("expected broadcast for stage {stage}");
match evt {
WatcherEvent::WorkItem {
@@ -664,6 +763,128 @@ mod tests {
assert!(rx.try_recv().is_err(), "no event for empty pending map");
}
// ── flush_pending clears merge_failure ─────────────────────────────────────
#[test]
fn flush_pending_clears_merge_failure_when_leaving_merge_stage() {
let tmp = TempDir::new().unwrap();
init_git_repo(tmp.path());
let stage_dir = make_stage_dir(tmp.path(), "2_current");
let story_path = stage_dir.join("50_story_retry.md");
fs::write(
&story_path,
"---\nname: Retry Story\nmerge_failure: \"conflicts detected\"\n---\n# Story\n",
)
.unwrap();
let (tx, _rx) = tokio::sync::broadcast::channel(16);
let mut pending = HashMap::new();
pending.insert(story_path.clone(), "2_current".to_string());
flush_pending(&pending, tmp.path(), &tx);
let contents = fs::read_to_string(&story_path).unwrap();
assert!(
!contents.contains("merge_failure"),
"merge_failure should be stripped when story lands in 2_current"
);
assert!(contents.contains("name: Retry Story"));
}
#[test]
fn flush_pending_clears_merge_failure_when_moving_to_upcoming() {
let tmp = TempDir::new().unwrap();
init_git_repo(tmp.path());
let stage_dir = make_stage_dir(tmp.path(), "1_upcoming");
let story_path = stage_dir.join("51_story_reset.md");
fs::write(
&story_path,
"---\nname: Reset Story\nmerge_failure: \"gate failed\"\n---\n# Story\n",
)
.unwrap();
let (tx, _rx) = tokio::sync::broadcast::channel(16);
let mut pending = HashMap::new();
pending.insert(story_path.clone(), "1_upcoming".to_string());
flush_pending(&pending, tmp.path(), &tx);
let contents = fs::read_to_string(&story_path).unwrap();
assert!(
!contents.contains("merge_failure"),
"merge_failure should be stripped when story lands in 1_upcoming"
);
}
#[test]
fn flush_pending_clears_merge_failure_when_moving_to_done() {
let tmp = TempDir::new().unwrap();
init_git_repo(tmp.path());
let stage_dir = make_stage_dir(tmp.path(), "5_done");
let story_path = stage_dir.join("52_story_done.md");
fs::write(
&story_path,
"---\nname: Done Story\nmerge_failure: \"stale error\"\n---\n# Story\n",
)
.unwrap();
let (tx, _rx) = tokio::sync::broadcast::channel(16);
let mut pending = HashMap::new();
pending.insert(story_path.clone(), "5_done".to_string());
flush_pending(&pending, tmp.path(), &tx);
let contents = fs::read_to_string(&story_path).unwrap();
assert!(
!contents.contains("merge_failure"),
"merge_failure should be stripped when story lands in 5_done"
);
}
#[test]
fn flush_pending_preserves_merge_failure_when_in_merge_stage() {
let tmp = TempDir::new().unwrap();
init_git_repo(tmp.path());
let stage_dir = make_stage_dir(tmp.path(), "4_merge");
let story_path = stage_dir.join("53_story_merging.md");
fs::write(
&story_path,
"---\nname: Merging Story\nmerge_failure: \"conflicts\"\n---\n# Story\n",
)
.unwrap();
let (tx, _rx) = tokio::sync::broadcast::channel(16);
let mut pending = HashMap::new();
pending.insert(story_path.clone(), "4_merge".to_string());
flush_pending(&pending, tmp.path(), &tx);
let contents = fs::read_to_string(&story_path).unwrap();
assert!(
contents.contains("merge_failure"),
"merge_failure should be preserved when story is in 4_merge"
);
}
#[test]
fn flush_pending_no_op_when_no_merge_failure() {
let tmp = TempDir::new().unwrap();
init_git_repo(tmp.path());
let stage_dir = make_stage_dir(tmp.path(), "2_current");
let story_path = stage_dir.join("54_story_clean.md");
let original = "---\nname: Clean Story\n---\n# Story\n";
fs::write(&story_path, original).unwrap();
let (tx, _rx) = tokio::sync::broadcast::channel(16);
let mut pending = HashMap::new();
pending.insert(story_path.clone(), "2_current".to_string());
flush_pending(&pending, tmp.path(), &tx);
let contents = fs::read_to_string(&story_path).unwrap();
assert_eq!(contents, original, "file without merge_failure should be unchanged");
}
// ── stage_for_path (additional edge cases) ────────────────────────────────
#[test]
@@ -713,6 +934,20 @@ mod tests {
);
}
#[test]
fn should_commit_stage_only_for_terminal_stages() {
// Terminal stages — should commit.
assert!(should_commit_stage("1_upcoming"));
assert!(should_commit_stage("5_done"));
assert!(should_commit_stage("6_archived"));
// Intermediate stages — broadcast-only, no commit.
assert!(!should_commit_stage("2_current"));
assert!(!should_commit_stage("3_qa"));
assert!(!should_commit_stage("4_merge"));
// Unknown — no commit.
assert!(!should_commit_stage("unknown"));
}
#[test]
fn stage_metadata_returns_correct_actions() {
let (action, msg) = stage_metadata("2_current", "42_story_foo").unwrap();

View File

@@ -179,6 +179,44 @@ pub fn set_anthropic_api_key(store: &dyn StoreOps, api_key: String) -> Result<()
set_anthropic_api_key_impl(store, &api_key)
}
/// Build a prompt for Claude Code that includes prior conversation history.
///
/// When a Claude Code session cannot be resumed (no session_id), we embed
/// the prior messages as a structured preamble so the LLM retains context.
/// If there is only one user message (the current one), the content is
/// returned as-is with no preamble.
fn build_claude_code_context_prompt(messages: &[Message], latest_user_content: &str) -> String {
// Collect prior messages (everything except the trailing user message).
let prior: Vec<&Message> = messages
.iter()
.rev()
.skip(1) // skip the latest user message
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect();
if prior.is_empty() {
return latest_user_content.to_string();
}
let mut parts = Vec::new();
parts.push("<conversation_history>".to_string());
for msg in &prior {
let label = match msg.role {
Role::User => "User",
Role::Assistant => "Assistant",
Role::Tool => "Tool",
Role::System => continue,
};
parts.push(format!("[{}]: {}", label, msg.content));
}
parts.push("</conversation_history>".to_string());
parts.push(String::new());
parts.push(latest_user_content.to_string());
parts.join("\n")
}
#[allow(clippy::too_many_arguments)]
pub async fn chat<F, U, T, A>(
messages: Vec<Message>,
@@ -224,13 +262,25 @@ where
if is_claude_code {
use crate::llm::providers::claude_code::ClaudeCodeProvider;
let user_message = messages
let latest_user_content = messages
.iter()
.rev()
.find(|m| m.role == Role::User)
.map(|m| m.content.clone())
.ok_or_else(|| "No user message found".to_string())?;
// When resuming with a session_id, Claude Code loads its own transcript
// from disk — the latest user message is sufficient. Without a
// session_id (e.g. after a page refresh) the prior conversation context
// would be lost because Claude Code only receives a single prompt
// string. In that case, prepend the conversation history so the LLM
// retains full context even though the session cannot be resumed.
let user_message = if config.session_id.is_some() {
latest_user_content
} else {
build_claude_code_context_prompt(&messages, &latest_user_content)
};
let project_root = state
.get_project_root()
.unwrap_or_else(|_| std::path::PathBuf::from("."));
@@ -244,6 +294,7 @@ where
&user_message,
&project_root.to_string_lossy(),
config.session_id.as_deref(),
None,
&mut cancel_rx,
|token| on_token(token),
|thinking| on_thinking(thinking),
@@ -404,7 +455,7 @@ where
}
Ok(ChatResult {
messages: new_messages,
messages: current_history[2..].to_vec(),
session_id: None,
})
}
@@ -1095,4 +1146,102 @@ mod tests {
let result = execute_tool(&call, &state).await;
assert!(result.starts_with("Error:"), "unexpected result: {result}");
}
// ---------------------------------------------------------------------------
// build_claude_code_context_prompt (Bug 245)
// ---------------------------------------------------------------------------
#[test]
fn context_prompt_single_message_returns_content_as_is() {
let messages = vec![Message {
role: Role::User,
content: "hello".to_string(),
tool_calls: None,
tool_call_id: None,
}];
let result = build_claude_code_context_prompt(&messages, "hello");
assert_eq!(result, "hello");
}
#[test]
fn context_prompt_includes_prior_conversation() {
let messages = vec![
Message {
role: Role::User,
content: "What is Rust?".to_string(),
tool_calls: None,
tool_call_id: None,
},
Message {
role: Role::Assistant,
content: "Rust is a systems language.".to_string(),
tool_calls: None,
tool_call_id: None,
},
Message {
role: Role::User,
content: "Tell me more".to_string(),
tool_calls: None,
tool_call_id: None,
},
];
let result = build_claude_code_context_prompt(&messages, "Tell me more");
assert!(
result.contains("<conversation_history>"),
"should have history preamble"
);
assert!(
result.contains("[User]: What is Rust?"),
"should include prior user message"
);
assert!(
result.contains("[Assistant]: Rust is a systems language."),
"should include prior assistant message"
);
assert!(
result.contains("</conversation_history>"),
"should close history block"
);
assert!(
result.ends_with("Tell me more"),
"should end with latest user message"
);
}
#[test]
fn context_prompt_skips_system_messages() {
let messages = vec![
Message {
role: Role::System,
content: "You are a helpful assistant.".to_string(),
tool_calls: None,
tool_call_id: None,
},
Message {
role: Role::User,
content: "hi".to_string(),
tool_calls: None,
tool_call_id: None,
},
Message {
role: Role::Assistant,
content: "hello".to_string(),
tool_calls: None,
tool_call_id: None,
},
Message {
role: Role::User,
content: "bye".to_string(),
tool_calls: None,
tool_call_id: None,
},
];
let result = build_claude_code_context_prompt(&messages, "bye");
assert!(
!result.contains("helpful assistant"),
"should not include system messages"
);
assert!(result.contains("[User]: hi"));
assert!(result.contains("[Assistant]: hello"));
}
}

View File

@@ -42,6 +42,7 @@ impl ClaudeCodeProvider {
user_message: &str,
project_root: &str,
session_id: Option<&str>,
system_prompt: Option<&str>,
cancel_rx: &mut watch::Receiver<bool>,
mut on_token: F,
mut on_thinking: T,
@@ -55,6 +56,7 @@ impl ClaudeCodeProvider {
let message = user_message.to_string();
let cwd = project_root.to_string();
let resume_id = session_id.map(|s| s.to_string());
let sys_prompt = system_prompt.map(|s| s.to_string());
let cancelled = Arc::new(AtomicBool::new(false));
let cancelled_clone = cancelled.clone();
@@ -79,6 +81,7 @@ impl ClaudeCodeProvider {
&message,
&cwd,
resume_id.as_deref(),
sys_prompt.as_deref(),
cancelled,
token_tx,
thinking_tx,
@@ -120,6 +123,7 @@ impl ClaudeCodeProvider {
.map_err(|e| format!("PTY task panicked: {e}"))??;
let captured_session_id = sid_rx.await.ok();
slog!("[pty-debug] RECEIVED session_id: {:?}", captured_session_id);
let structured_messages: Vec<Message> = msg_rx.try_iter().collect();
Ok(ClaudeCodeResult {
@@ -146,6 +150,7 @@ fn run_pty_session(
user_message: &str,
cwd: &str,
resume_session_id: Option<&str>,
system_prompt: Option<&str>,
cancelled: Arc<AtomicBool>,
token_tx: tokio::sync::mpsc::UnboundedSender<String>,
thinking_tx: tokio::sync::mpsc::UnboundedSender<String>,
@@ -184,6 +189,10 @@ fn run_pty_session(
// a tool requires user approval, instead of using PTY stdin/stdout.
cmd.arg("--permission-prompt-tool");
cmd.arg("mcp__story-kit__prompt_permission");
if let Some(sys) = system_prompt {
cmd.arg("--system");
cmd.arg(sys);
}
cmd.cwd(cwd);
// Keep TERM reasonable but disable color
cmd.env("NO_COLOR", "1");
@@ -346,6 +355,7 @@ fn process_json_event(
// Capture session_id from the first event that carries it
if let Some(tx) = sid_tx.take() {
if let Some(sid) = json.get("session_id").and_then(|s| s.as_str()) {
slog!("[pty-debug] CAPTURED session_id: {}", sid);
let _ = tx.send(sid.to_string());
} else {
*sid_tx = Some(tx);

View File

@@ -58,7 +58,6 @@ async fn main() -> Result<(), std::io::Error> {
explicit_root.to_string_lossy().to_string(),
&app_state,
store.as_ref(),
port,
)
.await
{
@@ -81,7 +80,6 @@ async fn main() -> Result<(), std::io::Error> {
project_root.to_string_lossy().to_string(),
&app_state,
store.as_ref(),
port,
)
.await
.unwrap_or_else(|e| {
@@ -169,6 +167,10 @@ async fn main() -> Result<(), std::io::Error> {
// Clone watcher_tx for the Matrix bot before it is moved into AppContext.
let watcher_tx_for_bot = watcher_tx.clone();
// Wrap perm_rx in Arc<Mutex> so it can be shared with both the WebSocket
// handler (via AppContext) and the Matrix bot.
let perm_rx = Arc::new(tokio::sync::Mutex::new(perm_rx));
let perm_rx_for_bot = Arc::clone(&perm_rx);
// Capture project root, agents Arc, and reconciliation sender before ctx
// is consumed by build_routes.
@@ -185,7 +187,7 @@ async fn main() -> Result<(), std::io::Error> {
watcher_tx,
reconciliation_tx,
perm_tx,
perm_rx: Arc::new(tokio::sync::Mutex::new(perm_rx)),
perm_rx,
};
let app = build_routes(ctx);
@@ -194,7 +196,7 @@ async fn main() -> Result<(), std::io::Error> {
// Optional Matrix bot: connect to the homeserver and start listening for
// messages if `.story_kit/bot.toml` is present and enabled.
if let Some(ref root) = startup_root {
matrix::spawn_bot(root, watcher_tx_for_bot);
matrix::spawn_bot(root, watcher_tx_for_bot, perm_rx_for_bot);
}
// On startup:

View File

@@ -1,3 +1,4 @@
use crate::http::context::{PermissionDecision, PermissionForward};
use crate::llm::providers::claude_code::{ClaudeCodeProvider, ClaudeCodeResult};
use crate::slog;
use matrix_sdk::{
@@ -14,12 +15,14 @@ use matrix_sdk::{
},
};
use pulldown_cmark::{Options, Parser, html};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use tokio::sync::Mutex as TokioMutex;
use tokio::sync::watch;
use tokio::sync::{mpsc, oneshot, watch};
use futures::StreamExt;
use matrix_sdk::encryption::verification::{
@@ -34,7 +37,8 @@ use super::config::BotConfig;
// ---------------------------------------------------------------------------
/// Role of a participant in the conversation history.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ConversationRole {
/// A message sent by a Matrix room participant.
User,
@@ -43,7 +47,7 @@ pub enum ConversationRole {
}
/// A single turn in the per-room conversation history.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConversationEntry {
pub role: ConversationRole,
/// Matrix user ID (e.g. `@alice:example.com`). Empty for assistant turns.
@@ -51,11 +55,81 @@ pub struct ConversationEntry {
pub content: String,
}
/// Per-room conversation history, keyed by room ID.
/// Per-room state: conversation entries plus the Claude Code session ID for
/// structured conversation resumption.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct RoomConversation {
/// Claude Code session ID used to resume multi-turn conversations so the
/// LLM receives prior turns as structured API messages rather than a
/// flattened text prefix.
#[serde(skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
/// Rolling conversation entries (used for turn counting and persistence).
pub entries: Vec<ConversationEntry>,
}
/// Per-room conversation state, keyed by room ID (serialised as string).
///
/// Wrapped in `Arc<TokioMutex<…>>` so it can be shared across concurrent
/// event-handler tasks without blocking the sync loop.
pub type ConversationHistory = Arc<TokioMutex<HashMap<OwnedRoomId, Vec<ConversationEntry>>>>;
pub type ConversationHistory = Arc<TokioMutex<HashMap<OwnedRoomId, RoomConversation>>>;
/// On-disk format for persisted conversation history. Room IDs are stored as
/// strings because `OwnedRoomId` does not implement `Serialize` as a map key.
#[derive(Serialize, Deserialize)]
struct PersistedHistory {
rooms: HashMap<String, RoomConversation>,
}
/// Path to the persisted conversation history file relative to project root.
const HISTORY_FILE: &str = ".story_kit/matrix_history.json";
/// Load conversation history from disk, returning an empty map on any error.
pub fn load_history(project_root: &std::path::Path) -> HashMap<OwnedRoomId, RoomConversation> {
let path = project_root.join(HISTORY_FILE);
let data = match std::fs::read_to_string(&path) {
Ok(d) => d,
Err(_) => return HashMap::new(),
};
let persisted: PersistedHistory = match serde_json::from_str(&data) {
Ok(p) => p,
Err(e) => {
slog!("[matrix-bot] Failed to parse history file: {e}");
return HashMap::new();
}
};
persisted
.rooms
.into_iter()
.filter_map(|(k, v)| {
k.parse::<OwnedRoomId>()
.ok()
.map(|room_id| (room_id, v))
})
.collect()
}
/// Save conversation history to disk. Errors are logged but not propagated.
pub fn save_history(
project_root: &std::path::Path,
history: &HashMap<OwnedRoomId, RoomConversation>,
) {
let persisted = PersistedHistory {
rooms: history
.iter()
.map(|(k, v)| (k.to_string(), v.clone()))
.collect(),
};
let path = project_root.join(HISTORY_FILE);
match serde_json::to_string_pretty(&persisted) {
Ok(json) => {
if let Err(e) = std::fs::write(&path, json) {
slog!("[matrix-bot] Failed to write history file: {e}");
}
}
Err(e) => slog!("[matrix-bot] Failed to serialise history: {e}"),
}
}
// ---------------------------------------------------------------------------
// Bot context
@@ -77,6 +151,20 @@ pub struct BotContext {
/// bot so it can continue a conversation thread without requiring an
/// explicit `@mention` on every follow-up.
pub bot_sent_event_ids: Arc<TokioMutex<HashSet<OwnedEventId>>>,
/// Receiver for permission requests from the MCP `prompt_permission` tool.
/// During an active chat the bot locks this to poll for incoming requests.
pub perm_rx: Arc<TokioMutex<mpsc::UnboundedReceiver<PermissionForward>>>,
/// Per-room pending permission reply senders. When a permission prompt is
/// posted to a room the oneshot sender is stored here; when the user
/// replies (yes/no) the event handler resolves it.
pub pending_perm_replies:
Arc<TokioMutex<HashMap<OwnedRoomId, oneshot::Sender<PermissionDecision>>>>,
/// How long to wait for a user to respond to a permission prompt before
/// denying (fail-closed).
pub permission_timeout_secs: u64,
/// The name the bot uses to refer to itself. Derived from `display_name`
/// in bot.toml; defaults to "Assistant" when unset.
pub bot_name: String,
}
// ---------------------------------------------------------------------------
@@ -90,6 +178,7 @@ pub async fn run_bot(
config: BotConfig,
project_root: PathBuf,
watcher_rx: tokio::sync::broadcast::Receiver<crate::io::watcher::WatcherEvent>,
perm_rx: Arc<TokioMutex<mpsc::UnboundedReceiver<PermissionForward>>>,
) -> Result<(), String> {
let store_path = project_root.join(".story_kit").join("matrix_store");
let client = Client::builder()
@@ -136,8 +225,39 @@ pub async fn run_bot(
slog!("[matrix-bot] Logged in as {bot_user_id} (device: {})", login_response.device_id);
// Bootstrap cross-signing keys for E2EE verification support.
if let Err(e) = client.encryption().bootstrap_cross_signing(None).await {
slog!("[matrix-bot] Cross-signing bootstrap note: {e}");
// Pass the bot's password for UIA (User-Interactive Authentication) —
// the homeserver requires proof of identity before accepting cross-signing keys.
{
use matrix_sdk::ruma::api::client::uiaa;
let password_auth = uiaa::AuthData::Password(uiaa::Password::new(
uiaa::UserIdentifier::UserIdOrLocalpart(config.username.clone()),
config.password.clone(),
));
if let Err(e) = client
.encryption()
.bootstrap_cross_signing(Some(password_auth))
.await
{
slog!("[matrix-bot] Cross-signing bootstrap note: {e}");
}
}
// Self-sign own device keys so other clients don't show
// "encrypted by a device not verified by its owner" warnings.
match client.encryption().get_own_device().await {
Ok(Some(own_device)) => {
if own_device.is_cross_signed_by_owner() {
slog!("[matrix-bot] Device already self-signed");
} else {
slog!("[matrix-bot] Device not self-signed, signing now...");
match own_device.verify().await {
Ok(()) => slog!("[matrix-bot] Successfully self-signed device keys"),
Err(e) => slog!("[matrix-bot] Failed to self-sign device keys: {e}"),
}
}
}
Ok(None) => slog!("[matrix-bot] Could not find own device in crypto store"),
Err(e) => slog!("[matrix-bot] Error retrieving own device: {e}"),
}
if config.allowed_users.is_empty() {
@@ -190,14 +310,29 @@ pub async fn run_bot(
let notif_room_ids = target_room_ids.clone();
let notif_project_root = project_root.clone();
let persisted = load_history(&project_root);
slog!(
"[matrix-bot] Loaded persisted conversation history for {} room(s)",
persisted.len()
);
let bot_name = config
.display_name
.clone()
.unwrap_or_else(|| "Assistant".to_string());
let ctx = BotContext {
bot_user_id,
target_room_ids,
project_root,
allowed_users: config.allowed_users,
history: Arc::new(TokioMutex::new(HashMap::new())),
history: Arc::new(TokioMutex::new(persisted)),
history_size: config.history_size,
bot_sent_event_ids: Arc::new(TokioMutex::new(HashSet::new())),
perm_rx,
pending_perm_replies: Arc::new(TokioMutex::new(HashMap::new())),
permission_timeout_secs: config.permission_timeout_secs,
bot_name,
};
slog!("[matrix-bot] Cryptographic identity verification is always ON — commands from unencrypted rooms or unverified devices are rejected");
@@ -231,6 +366,24 @@ pub async fn run_bot(
// Address-filtering helpers
// ---------------------------------------------------------------------------
/// Returns `true` if the message body is an affirmative permission response.
///
/// Recognised affirmative tokens (case-insensitive): `yes`, `y`, `approve`,
/// `allow`, `ok`. Anything else — including ambiguous text — is treated as
/// denial (fail-closed).
fn is_permission_approval(body: &str) -> bool {
// Strip a leading @mention (e.g. "@timmy yes") so the bot name doesn't
// interfere with the check.
let trimmed = body
.trim()
.trim_start_matches('@')
.split_whitespace()
.last()
.unwrap_or("")
.to_ascii_lowercase();
matches!(trimmed.as_str(), "yes" | "y" | "approve" | "allow" | "ok")
}
/// Returns `true` if the message mentions the bot.
///
/// Checks both the plain-text `body` and an optional `formatted_body` (HTML).
@@ -528,6 +681,33 @@ async fn on_room_message(
}
}
// If there is a pending permission prompt for this room, interpret the
// message as a yes/no response instead of starting a new chat.
{
let mut pending = ctx.pending_perm_replies.lock().await;
if let Some(tx) = pending.remove(&incoming_room_id) {
let decision = if is_permission_approval(&body) {
PermissionDecision::Approve
} else {
PermissionDecision::Deny
};
let _ = tx.send(decision);
let confirmation = if decision == PermissionDecision::Approve {
"Permission approved."
} else {
"Permission denied."
};
let html = markdown_to_html(confirmation);
if let Ok(resp) = room
.send(RoomMessageEventContent::text_html(confirmation, html))
.await
{
ctx.bot_sent_event_ids.lock().await.insert(resp.event_id);
}
return;
}
}
let sender = ev.sender.to_string();
let user_message = body;
slog!("[matrix-bot] Message from {sender}: {user_message}");
@@ -543,33 +723,10 @@ async fn on_room_message(
// Message handler
// ---------------------------------------------------------------------------
/// Build a context string from the room's conversation history to prepend to
/// the user's current message. Returns an empty string when history is empty.
fn build_context_prefix(
history: &[ConversationEntry],
current_sender: &str,
current_message: &str,
) -> String {
if history.is_empty() {
return format!("{current_sender}: {current_message}");
}
let mut out = String::from("[Conversation history for this room]\n");
for entry in history {
match entry.role {
ConversationRole::User => {
out.push_str(&format!("User ({}): {}\n", entry.sender, entry.content));
}
ConversationRole::Assistant => {
out.push_str(&format!("Assistant: {}\n", entry.content));
}
}
}
out.push('\n');
out.push_str(&format!(
"Current message from {current_sender}: {current_message}"
));
out
/// Build the user-facing prompt for a single turn. In multi-user rooms the
/// sender is included so the LLM can distinguish participants.
fn format_user_prompt(sender: &str, message: &str) -> String {
format!("{sender}: {message}")
}
async fn handle_message(
@@ -579,14 +736,24 @@ async fn handle_message(
sender: String,
user_message: String,
) {
// Read current history for this room before calling the LLM.
let history_snapshot: Vec<ConversationEntry> = {
// Look up the room's existing Claude Code session ID (if any) so we can
// resume the conversation with structured API messages instead of
// flattening history into a text prefix.
let resume_session_id: Option<String> = {
let guard = ctx.history.lock().await;
guard.get(&room_id).cloned().unwrap_or_default()
guard
.get(&room_id)
.and_then(|conv| conv.session_id.clone())
};
// Build the prompt with conversation context.
let prompt_with_context = build_context_prefix(&history_snapshot, &sender, &user_message);
// The prompt is just the current message with sender attribution.
// Prior conversation context is carried by the Claude Code session.
let prompt = format_user_prompt(&sender, &user_message);
let bot_name = &ctx.bot_name;
let system_prompt = format!(
"Your name is {bot_name}. Refer to yourself as {bot_name}, not Claude."
);
let provider = ClaudeCodeProvider::new();
let (cancel_tx, mut cancel_rx) = watch::channel(false);
@@ -601,6 +768,7 @@ async fn handle_message(
// block the LLM stream while waiting for Matrix send round-trips.
let post_room = room.clone();
let sent_ids = Arc::clone(&ctx.bot_sent_event_ids);
let sent_ids_for_post = Arc::clone(&sent_ids);
let post_task = tokio::spawn(async move {
while let Some(chunk) = msg_rx.recv().await {
let html = markdown_to_html(&chunk);
@@ -608,7 +776,7 @@ async fn handle_message(
.send(RoomMessageEventContent::text_html(chunk, html))
.await
{
sent_ids.lock().await.insert(response.event_id);
sent_ids_for_post.lock().await.insert(response.event_id);
}
}
});
@@ -619,34 +787,96 @@ async fn handle_message(
let sent_any_chunk = Arc::new(AtomicBool::new(false));
let sent_any_chunk_for_callback = Arc::clone(&sent_any_chunk);
let result = provider
.chat_stream(
&prompt_with_context,
&ctx.project_root.to_string_lossy(),
None, // Each Matrix conversation turn is independent at the Claude Code session level.
&mut cancel_rx,
move |token| {
let mut buf = buffer_for_callback.lock().unwrap();
buf.push_str(token);
// Flush complete paragraphs as they arrive.
let paragraphs = drain_complete_paragraphs(&mut buf);
for chunk in paragraphs {
sent_any_chunk_for_callback.store(true, Ordering::Relaxed);
let _ = msg_tx_for_callback.send(chunk);
let project_root_str = ctx.project_root.to_string_lossy().to_string();
let chat_fut = provider.chat_stream(
&prompt,
&project_root_str,
resume_session_id.as_deref(),
Some(&system_prompt),
&mut cancel_rx,
move |token| {
let mut buf = buffer_for_callback.lock().unwrap();
buf.push_str(token);
// Flush complete paragraphs as they arrive.
let paragraphs = drain_complete_paragraphs(&mut buf);
for chunk in paragraphs {
sent_any_chunk_for_callback.store(true, Ordering::Relaxed);
let _ = msg_tx_for_callback.send(chunk);
}
},
|_thinking| {}, // Discard thinking tokens
|_activity| {}, // Discard activity signals
);
tokio::pin!(chat_fut);
// Lock the permission receiver for the duration of this chat session.
// Permission requests from the MCP `prompt_permission` tool arrive here.
let mut perm_rx_guard = ctx.perm_rx.lock().await;
let result = loop {
tokio::select! {
r = &mut chat_fut => break r,
Some(perm_fwd) = perm_rx_guard.recv() => {
// Post the permission prompt to the Matrix room.
let prompt_msg = format!(
"**Permission Request**\n\n\
Tool: `{}`\n```json\n{}\n```\n\n\
Reply **yes** to approve or **no** to deny.",
perm_fwd.tool_name,
serde_json::to_string_pretty(&perm_fwd.tool_input)
.unwrap_or_else(|_| perm_fwd.tool_input.to_string()),
);
let html = markdown_to_html(&prompt_msg);
if let Ok(resp) = room
.send(RoomMessageEventContent::text_html(&prompt_msg, html))
.await
{
sent_ids.lock().await.insert(resp.event_id);
}
},
|_thinking| {}, // Discard thinking tokens
|_activity| {}, // Discard activity signals
)
.await;
// Store the MCP oneshot sender so the event handler can
// resolve it when the user replies yes/no.
ctx.pending_perm_replies
.lock()
.await
.insert(room_id.clone(), perm_fwd.response_tx);
// Spawn a timeout task: auto-deny if the user does not respond.
let pending = Arc::clone(&ctx.pending_perm_replies);
let timeout_room_id = room_id.clone();
let timeout_room = room.clone();
let timeout_sent_ids = Arc::clone(&ctx.bot_sent_event_ids);
let timeout_secs = ctx.permission_timeout_secs;
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(timeout_secs)).await;
if let Some(tx) = pending.lock().await.remove(&timeout_room_id) {
let _ = tx.send(PermissionDecision::Deny);
let msg = "Permission request timed out — denied (fail-closed).";
let html = markdown_to_html(msg);
if let Ok(resp) = timeout_room
.send(RoomMessageEventContent::text_html(msg, html))
.await
{
timeout_sent_ids.lock().await.insert(resp.event_id);
}
}
});
}
}
};
drop(perm_rx_guard);
// Flush any remaining text that didn't end with a paragraph boundary.
let remaining = buffer.lock().unwrap().trim().to_string();
let did_send_any = sent_any_chunk.load(Ordering::Relaxed);
let assistant_reply = match result {
Ok(ClaudeCodeResult { messages, .. }) => {
if !remaining.is_empty() {
let (assistant_reply, new_session_id) = match result {
Ok(ClaudeCodeResult {
messages,
session_id,
}) => {
let reply = if !remaining.is_empty() {
let _ = msg_tx.send(remaining.clone());
remaining
} else if !did_send_any {
@@ -665,13 +895,15 @@ async fn handle_message(
last_text
} else {
remaining
}
};
slog!("[matrix-bot] session_id from chat_stream: {:?}", session_id);
(reply, session_id)
}
Err(e) => {
slog!("[matrix-bot] LLM error: {e}");
let err_msg = format!("Error processing your request: {e}");
let _ = msg_tx.send(err_msg.clone());
err_msg
(err_msg, None)
}
};
@@ -680,25 +912,40 @@ async fn handle_message(
drop(msg_tx);
let _ = post_task.await;
// Record this exchange in the per-room conversation history.
// Record this exchange in the per-room conversation history and persist
// the session ID so the next turn resumes with structured API messages.
if !assistant_reply.starts_with("Error processing") {
let mut guard = ctx.history.lock().await;
let entries = guard.entry(room_id).or_default();
entries.push(ConversationEntry {
let conv = guard.entry(room_id).or_default();
// Store the session ID so the next turn uses --resume.
slog!("[matrix-bot] storing session_id: {:?} (was: {:?})", new_session_id, conv.session_id);
if new_session_id.is_some() {
conv.session_id = new_session_id;
}
conv.entries.push(ConversationEntry {
role: ConversationRole::User,
sender: sender.clone(),
content: user_message,
});
entries.push(ConversationEntry {
conv.entries.push(ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: assistant_reply,
});
// Trim to the configured maximum, dropping the oldest entries first.
if entries.len() > ctx.history_size {
let excess = entries.len() - ctx.history_size;
entries.drain(..excess);
// The session_id is preserved: Claude Code's --resume loads the full
// conversation from its own session transcript on disk, so trimming
// our local tracking doesn't affect the LLM's context.
if conv.entries.len() > ctx.history_size {
let excess = conv.entries.len() - ctx.history_size;
conv.entries.drain(..excess);
}
// Persist to disk so history survives server restarts.
save_history(&ctx.project_root, &guard);
}
}
@@ -961,6 +1208,7 @@ mod tests {
fn bot_context_has_no_require_verified_devices_field() {
// Verification is always on — BotContext no longer has a toggle field.
// This test verifies the struct can be constructed and cloned without it.
let (_perm_tx, perm_rx) = mpsc::unbounded_channel();
let ctx = BotContext {
bot_user_id: make_user_id("@bot:example.com"),
target_room_ids: vec![],
@@ -969,6 +1217,10 @@ mod tests {
history: Arc::new(TokioMutex::new(HashMap::new())),
history_size: 20,
bot_sent_event_ids: Arc::new(TokioMutex::new(HashSet::new())),
perm_rx: Arc::new(TokioMutex::new(perm_rx)),
pending_perm_replies: Arc::new(TokioMutex::new(HashMap::new())),
permission_timeout_secs: 120,
bot_name: "Assistant".to_string(),
};
// Clone must work (required by Matrix SDK event handler injection).
let _cloned = ctx.clone();
@@ -1097,62 +1349,18 @@ mod tests {
assert_eq!(buf, "Third.");
}
// -- build_context_prefix -----------------------------------------------
// -- format_user_prompt -------------------------------------------------
#[test]
fn build_context_prefix_empty_history() {
let prefix = build_context_prefix(&[], "@alice:example.com", "Hello!");
assert_eq!(prefix, "@alice:example.com: Hello!");
fn format_user_prompt_includes_sender_and_message() {
let prompt = format_user_prompt("@alice:example.com", "Hello!");
assert_eq!(prompt, "@alice:example.com: Hello!");
}
#[test]
fn build_context_prefix_includes_history_entries() {
let history = vec![
ConversationEntry {
role: ConversationRole::User,
sender: "@alice:example.com".to_string(),
content: "What is story 42?".to_string(),
},
ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: "Story 42 is about…".to_string(),
},
];
let prefix = build_context_prefix(&history, "@bob:example.com", "Tell me more.");
assert!(prefix.contains("[Conversation history for this room]"));
assert!(prefix.contains("User (@alice:example.com): What is story 42?"));
assert!(prefix.contains("Assistant: Story 42 is about…"));
assert!(prefix.contains("Current message from @bob:example.com: Tell me more."));
}
#[test]
fn build_context_prefix_attributes_multiple_users() {
let history = vec![
ConversationEntry {
role: ConversationRole::User,
sender: "@alice:example.com".to_string(),
content: "First question".to_string(),
},
ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: "First answer".to_string(),
},
ConversationEntry {
role: ConversationRole::User,
sender: "@bob:example.com".to_string(),
content: "Follow-up".to_string(),
},
ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: "Second answer".to_string(),
},
];
let prefix = build_context_prefix(&history, "@alice:example.com", "Another question");
assert!(prefix.contains("User (@alice:example.com): First question"));
assert!(prefix.contains("User (@bob:example.com): Follow-up"));
fn format_user_prompt_different_users() {
let prompt = format_user_prompt("@bob:example.com", "What's up?");
assert_eq!(prompt, "@bob:example.com: What's up?");
}
// -- conversation history trimming --------------------------------------
@@ -1166,37 +1374,44 @@ mod tests {
// Add 6 entries (3 user + 3 assistant turns).
{
let mut guard = history.lock().await;
let entries = guard.entry(room_id.clone()).or_default();
let conv = guard.entry(room_id.clone()).or_default();
conv.session_id = Some("test-session".to_string());
for i in 0..3usize {
entries.push(ConversationEntry {
conv.entries.push(ConversationEntry {
role: ConversationRole::User,
sender: "@user:example.com".to_string(),
content: format!("msg {i}"),
});
entries.push(ConversationEntry {
conv.entries.push(ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: format!("reply {i}"),
});
if entries.len() > history_size {
let excess = entries.len() - history_size;
entries.drain(..excess);
if conv.entries.len() > history_size {
let excess = conv.entries.len() - history_size;
conv.entries.drain(..excess);
conv.session_id = None;
}
}
}
let guard = history.lock().await;
let entries = guard.get(&room_id).unwrap();
let conv = guard.get(&room_id).unwrap();
assert_eq!(
entries.len(),
conv.entries.len(),
history_size,
"history must be trimmed to history_size"
);
// The oldest entries (msg 0 / reply 0) should have been dropped.
assert!(
entries.iter().all(|e| !e.content.contains("msg 0")),
conv.entries.iter().all(|e| !e.content.contains("msg 0")),
"oldest entries must be dropped"
);
// Session ID must be cleared when trimming occurs.
assert!(
conv.session_id.is_none(),
"session_id must be cleared on trim to start a fresh session"
);
}
#[tokio::test]
@@ -1210,6 +1425,7 @@ mod tests {
guard
.entry(room_a.clone())
.or_default()
.entries
.push(ConversationEntry {
role: ConversationRole::User,
sender: "@alice:example.com".to_string(),
@@ -1218,6 +1434,7 @@ mod tests {
guard
.entry(room_b.clone())
.or_default()
.entries
.push(ConversationEntry {
role: ConversationRole::User,
sender: "@bob:example.com".to_string(),
@@ -1226,12 +1443,157 @@ mod tests {
}
let guard = history.lock().await;
let entries_a = guard.get(&room_a).unwrap();
let entries_b = guard.get(&room_b).unwrap();
assert_eq!(entries_a.len(), 1);
assert_eq!(entries_b.len(), 1);
assert_eq!(entries_a[0].content, "Room A message");
assert_eq!(entries_b[0].content, "Room B message");
let conv_a = guard.get(&room_a).unwrap();
let conv_b = guard.get(&room_b).unwrap();
assert_eq!(conv_a.entries.len(), 1);
assert_eq!(conv_b.entries.len(), 1);
assert_eq!(conv_a.entries[0].content, "Room A message");
assert_eq!(conv_b.entries[0].content, "Room B message");
}
// -- persistence --------------------------------------------------------
#[test]
fn save_and_load_history_round_trip() {
let dir = tempfile::tempdir().unwrap();
let story_kit_dir = dir.path().join(".story_kit");
std::fs::create_dir_all(&story_kit_dir).unwrap();
let room_id: OwnedRoomId = "!persist:example.com".parse().unwrap();
let mut map: HashMap<OwnedRoomId, RoomConversation> = HashMap::new();
let conv = map.entry(room_id.clone()).or_default();
conv.session_id = Some("session-abc".to_string());
conv.entries.push(ConversationEntry {
role: ConversationRole::User,
sender: "@alice:example.com".to_string(),
content: "hello".to_string(),
});
conv.entries.push(ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: "hi there!".to_string(),
});
save_history(dir.path(), &map);
let loaded = load_history(dir.path());
let loaded_conv = loaded.get(&room_id).expect("room must exist after load");
assert_eq!(loaded_conv.session_id.as_deref(), Some("session-abc"));
assert_eq!(loaded_conv.entries.len(), 2);
assert_eq!(loaded_conv.entries[0].role, ConversationRole::User);
assert_eq!(loaded_conv.entries[0].sender, "@alice:example.com");
assert_eq!(loaded_conv.entries[0].content, "hello");
assert_eq!(loaded_conv.entries[1].role, ConversationRole::Assistant);
assert_eq!(loaded_conv.entries[1].content, "hi there!");
}
#[test]
fn load_history_returns_empty_on_missing_file() {
let dir = tempfile::tempdir().unwrap();
let loaded = load_history(dir.path());
assert!(loaded.is_empty());
}
#[test]
fn load_history_returns_empty_on_corrupt_file() {
let dir = tempfile::tempdir().unwrap();
let story_kit_dir = dir.path().join(".story_kit");
std::fs::create_dir_all(&story_kit_dir).unwrap();
std::fs::write(dir.path().join(HISTORY_FILE), "not valid json").unwrap();
let loaded = load_history(dir.path());
assert!(loaded.is_empty());
}
// -- session_id tracking ------------------------------------------------
#[tokio::test]
async fn session_id_preserved_within_history_size() {
let history: ConversationHistory = Arc::new(TokioMutex::new(HashMap::new()));
let room_id: OwnedRoomId = "!session:example.com".parse().unwrap();
{
let mut guard = history.lock().await;
let conv = guard.entry(room_id.clone()).or_default();
conv.session_id = Some("sess-1".to_string());
conv.entries.push(ConversationEntry {
role: ConversationRole::User,
sender: "@alice:example.com".to_string(),
content: "hello".to_string(),
});
conv.entries.push(ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: "hi".to_string(),
});
// No trimming needed (2 entries, well under any reasonable limit).
}
let guard = history.lock().await;
let conv = guard.get(&room_id).unwrap();
assert_eq!(
conv.session_id.as_deref(),
Some("sess-1"),
"session_id must be preserved when no trimming occurs"
);
}
// -- multi-user room attribution ----------------------------------------
#[tokio::test]
async fn multi_user_entries_preserve_sender() {
let history: ConversationHistory = Arc::new(TokioMutex::new(HashMap::new()));
let room_id: OwnedRoomId = "!multi:example.com".parse().unwrap();
{
let mut guard = history.lock().await;
let conv = guard.entry(room_id.clone()).or_default();
conv.entries.push(ConversationEntry {
role: ConversationRole::User,
sender: "@alice:example.com".to_string(),
content: "from alice".to_string(),
});
conv.entries.push(ConversationEntry {
role: ConversationRole::Assistant,
sender: String::new(),
content: "reply to alice".to_string(),
});
conv.entries.push(ConversationEntry {
role: ConversationRole::User,
sender: "@bob:example.com".to_string(),
content: "from bob".to_string(),
});
}
let guard = history.lock().await;
let conv = guard.get(&room_id).unwrap();
assert_eq!(conv.entries[0].sender, "@alice:example.com");
assert_eq!(conv.entries[2].sender, "@bob:example.com");
}
// -- self-sign device key decision logic -----------------------------------
// The self-signing logic in run_bot cannot be unit-tested because it
// requires a live matrix_sdk::Client. The tests below verify the branch
// decision: sign only when the device is NOT already cross-signed.
#[test]
fn device_already_self_signed_skips_signing() {
// Simulates: get_own_device returns Some, is_cross_signed_by_owner → true
let is_cross_signed: bool = true;
assert!(
is_cross_signed,
"already self-signed device should skip signing"
);
}
#[test]
fn device_not_self_signed_triggers_signing() {
// Simulates: get_own_device returns Some, is_cross_signed_by_owner → false
let is_cross_signed: bool = false;
assert!(
!is_cross_signed,
"device without self-signature should trigger signing"
);
}
// -- check_sender_verified decision logic --------------------------------
@@ -1262,4 +1624,67 @@ mod tests {
"user with no cross-signing setup should be rejected"
);
}
// -- is_permission_approval -----------------------------------------------
#[test]
fn is_permission_approval_accepts_yes_variants() {
assert!(is_permission_approval("yes"));
assert!(is_permission_approval("Yes"));
assert!(is_permission_approval("YES"));
assert!(is_permission_approval("y"));
assert!(is_permission_approval("Y"));
assert!(is_permission_approval("approve"));
assert!(is_permission_approval("allow"));
assert!(is_permission_approval("ok"));
assert!(is_permission_approval("OK"));
}
#[test]
fn is_permission_approval_denies_no_and_other() {
assert!(!is_permission_approval("no"));
assert!(!is_permission_approval("No"));
assert!(!is_permission_approval("n"));
assert!(!is_permission_approval("deny"));
assert!(!is_permission_approval("reject"));
assert!(!is_permission_approval("maybe"));
assert!(!is_permission_approval(""));
assert!(!is_permission_approval("yes please do it"));
}
#[test]
fn is_permission_approval_strips_at_mention_prefix() {
assert!(is_permission_approval("@timmy yes"));
assert!(!is_permission_approval("@timmy no"));
}
#[test]
fn is_permission_approval_handles_whitespace() {
assert!(is_permission_approval(" yes "));
assert!(is_permission_approval("\tyes\n"));
}
// -- bot_name / system prompt -------------------------------------------
#[test]
fn bot_name_system_prompt_format() {
let bot_name = "Timmy";
let system_prompt =
format!("Your name is {bot_name}. Refer to yourself as {bot_name}, not Claude.");
assert_eq!(
system_prompt,
"Your name is Timmy. Refer to yourself as Timmy, not Claude."
);
}
#[test]
fn bot_name_defaults_to_assistant_when_display_name_absent() {
// When display_name is not set in bot.toml, bot_name should be "Assistant".
// This mirrors the logic in run_bot: config.display_name.clone().unwrap_or_else(...)
fn resolve_bot_name(display_name: Option<String>) -> String {
display_name.unwrap_or_else(|| "Assistant".to_string())
}
assert_eq!(resolve_bot_name(None), "Assistant");
assert_eq!(resolve_bot_name(Some("Timmy".to_string())), "Timmy");
}
}

View File

@@ -5,6 +5,10 @@ fn default_history_size() -> usize {
20
}
fn default_permission_timeout_secs() -> u64 {
120
}
/// Configuration for the Matrix bot, read from `.story_kit/bot.toml`.
#[derive(Deserialize, Clone, Debug)]
pub struct BotConfig {
@@ -35,11 +39,20 @@ pub struct BotConfig {
/// dropped. Defaults to 20.
#[serde(default = "default_history_size")]
pub history_size: usize,
/// Timeout in seconds for permission prompts surfaced to the Matrix room.
/// If the user does not respond within this window the permission is denied
/// (fail-closed). Defaults to 120 seconds.
#[serde(default = "default_permission_timeout_secs")]
pub permission_timeout_secs: u64,
/// Previously used to select an Anthropic model. Now ignored — the bot
/// uses Claude Code which manages its own model selection. Kept for
/// backwards compatibility so existing bot.toml files still parse.
#[allow(dead_code)]
pub model: Option<String>,
/// Display name the bot uses to identify itself in conversations.
/// If unset, the bot falls back to "Assistant".
#[serde(default)]
pub display_name: Option<String>,
}
impl BotConfig {
@@ -256,6 +269,88 @@ history_size = 50
assert_eq!(config.history_size, 50);
}
#[test]
fn load_reads_display_name() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
display_name = "Timmy"
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert_eq!(config.display_name.as_deref(), Some("Timmy"));
}
#[test]
fn load_display_name_defaults_to_none_when_absent() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert!(config.display_name.is_none());
}
#[test]
fn load_uses_default_permission_timeout() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert_eq!(config.permission_timeout_secs, 120);
}
#[test]
fn load_respects_custom_permission_timeout() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
fs::create_dir_all(&sk).unwrap();
fs::write(
sk.join("bot.toml"),
r#"
homeserver = "https://matrix.example.com"
username = "@bot:example.com"
password = "secret"
room_ids = ["!abc:example.com"]
enabled = true
permission_timeout_secs = 60
"#,
)
.unwrap();
let config = BotConfig::load(tmp.path()).unwrap();
assert_eq!(config.permission_timeout_secs, 60);
}
#[test]
fn load_ignores_legacy_require_verified_devices_key() {
// Old bot.toml files that still have `require_verified_devices = true`

View File

@@ -21,9 +21,11 @@ pub mod notifications;
pub use config::BotConfig;
use crate::http::context::PermissionForward;
use crate::io::watcher::WatcherEvent;
use std::path::Path;
use tokio::sync::broadcast;
use std::sync::Arc;
use tokio::sync::{Mutex as TokioMutex, broadcast, mpsc};
/// Attempt to start the Matrix bot.
///
@@ -35,8 +37,16 @@ use tokio::sync::broadcast;
/// posts stage-transition messages to all configured rooms whenever a work
/// item moves between pipeline stages.
///
/// `perm_rx` is the permission-request receiver shared with the MCP
/// `prompt_permission` tool. The bot locks it during active chat sessions
/// to surface permission prompts to the Matrix room and relay user decisions.
///
/// Must be called from within a Tokio runtime context (e.g., from `main`).
pub fn spawn_bot(project_root: &Path, watcher_tx: broadcast::Sender<WatcherEvent>) {
pub fn spawn_bot(
project_root: &Path,
watcher_tx: broadcast::Sender<WatcherEvent>,
perm_rx: Arc<TokioMutex<mpsc::UnboundedReceiver<PermissionForward>>>,
) {
let config = match BotConfig::load(project_root) {
Some(c) => c,
None => {
@@ -54,7 +64,7 @@ pub fn spawn_bot(project_root: &Path, watcher_tx: broadcast::Sender<WatcherEvent
let root = project_root.to_path_buf();
let watcher_rx = watcher_tx.subscribe();
tokio::spawn(async move {
if let Err(e) = bot::run_bot(config, root, watcher_rx).await {
if let Err(e) = bot::run_bot(config, root, watcher_rx, perm_rx).await {
crate::slog!("[matrix-bot] Fatal error: {e}");
}
});

View File

@@ -81,6 +81,24 @@ pub fn format_stage_notification(
(plain, html)
}
/// Format an error notification message for a story failure.
///
/// Returns `(plain_text, html)` suitable for `RoomMessageEventContent::text_html`.
pub fn format_error_notification(
item_id: &str,
story_name: Option<&str>,
reason: &str,
) -> (String, String) {
let number = extract_story_number(item_id).unwrap_or(item_id);
let name = story_name.unwrap_or(item_id);
let plain = format!("\u{274c} #{number} {name} \u{2014} {reason}");
let html = format!(
"\u{274c} <strong>#{number}</strong> <em>{name}</em> \u{2014} {reason}"
);
(plain, html)
}
/// Spawn a background task that listens for watcher events and posts
/// stage-transition notifications to all configured Matrix rooms.
pub fn spawn_notification_listener(
@@ -126,6 +144,32 @@ pub fn spawn_notification_listener(
}
}
}
Ok(WatcherEvent::MergeFailure {
ref story_id,
ref reason,
}) => {
let story_name =
read_story_name(&project_root, "4_merge", story_id);
let (plain, html) = format_error_notification(
story_id,
story_name.as_deref(),
reason,
);
slog!("[matrix-bot] Sending error notification: {plain}");
for room_id in &room_ids {
if let Some(room) = client.get_room(room_id) {
let content =
RoomMessageEventContent::text_html(plain.clone(), html.clone());
if let Err(e) = room.send(content).await {
slog!(
"[matrix-bot] Failed to send error notification to {room_id}: {e}"
);
}
}
}
}
Ok(_) => {} // Ignore non-work-item events
Err(broadcast::error::RecvError::Lagged(n)) => {
slog!(
@@ -246,6 +290,42 @@ mod tests {
assert_eq!(name, None);
}
// ── format_error_notification ────────────────────────────────────────────
#[test]
fn format_error_notification_with_story_name() {
let (plain, html) =
format_error_notification("262_story_bot_errors", Some("Bot error notifications"), "merge conflict in src/main.rs");
assert_eq!(
plain,
"\u{274c} #262 Bot error notifications \u{2014} merge conflict in src/main.rs"
);
assert_eq!(
html,
"\u{274c} <strong>#262</strong> <em>Bot error notifications</em> \u{2014} merge conflict in src/main.rs"
);
}
#[test]
fn format_error_notification_without_story_name_falls_back_to_item_id() {
let (plain, _html) =
format_error_notification("42_bug_fix_thing", None, "tests failed");
assert_eq!(
plain,
"\u{274c} #42 42_bug_fix_thing \u{2014} tests failed"
);
}
#[test]
fn format_error_notification_non_numeric_id_uses_full_id() {
let (plain, _html) =
format_error_notification("abc_story_thing", Some("Some Story"), "clippy errors");
assert_eq!(
plain,
"\u{274c} #abc_story_thing Some Story \u{2014} clippy errors"
);
}
// ── format_stage_notification ───────────────────────────────────────────
#[test]

View File

@@ -1 +0,0 @@
{"v":1}

View File

@@ -1,6 +0,0 @@
{
"git": {
"sha1": "2a1790a69107cd03dae85d501dcbdb11c5b32ef3"
},
"path_in_vcs": ""
}

View File

@@ -1,3 +0,0 @@
/target/
/doc/
Cargo.lock

View File

@@ -1,350 +0,0 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
name = "rusqlite"
version = "0.37.99"
authors = ["The rusqlite developers"]
build = false
exclude = [
"/.github/*",
"/.gitattributes",
"/appveyor.yml",
"/Changelog.md",
"/clippy.toml",
"/codecov.yml",
"**/*.sh",
]
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Ergonomic wrapper for SQLite"
documentation = "https://docs.rs/rusqlite/"
readme = "README.md"
keywords = [
"sqlite",
"database",
"ffi",
]
categories = ["database"]
license = "MIT"
repository = "https://github.com/rusqlite/rusqlite"
[package.metadata.docs.rs]
features = [
"modern-full",
"rusqlite-macros",
]
all-features = false
no-default-features = false
default-target = "x86_64-unknown-linux-gnu"
rustdoc-args = [
"--cfg",
"docsrs",
]
[package.metadata.playground]
features = ["bundled-full"]
all-features = false
[badges.appveyor]
repository = "rusqlite/rusqlite"
[badges.codecov]
repository = "rusqlite/rusqlite"
[badges.maintenance]
status = "actively-developed"
[features]
array = [
"vtab",
"pointer",
]
backup = []
blob = []
buildtime_bindgen = [
"libsqlite3-sys/buildtime_bindgen",
"sqlite-wasm-rs/bindgen",
]
bundled = [
"libsqlite3-sys/bundled",
"modern_sqlite",
]
bundled-full = [
"modern-full",
"bundled",
]
bundled-sqlcipher = [
"libsqlite3-sys/bundled-sqlcipher",
"bundled",
]
bundled-sqlcipher-vendored-openssl = [
"libsqlite3-sys/bundled-sqlcipher-vendored-openssl",
"bundled-sqlcipher",
]
bundled-windows = ["libsqlite3-sys/bundled-windows"]
cache = ["hashlink"]
collation = []
column_decltype = []
column_metadata = ["libsqlite3-sys/column_metadata"]
csvtab = [
"csv",
"vtab",
]
default = ["cache"]
extra_check = []
fallible_uint = []
functions = []
hooks = []
i128_blob = []
in_gecko = [
"modern_sqlite",
"libsqlite3-sys/in_gecko",
]
limits = []
load_extension = []
loadable_extension = ["libsqlite3-sys/loadable_extension"]
modern-full = [
"array",
"backup",
"blob",
"modern_sqlite",
"chrono",
"collation",
"column_metadata",
"column_decltype",
"csvtab",
"extra_check",
"functions",
"hooks",
"i128_blob",
"jiff",
"limits",
"load_extension",
"serde_json",
"serialize",
"series",
"time",
"trace",
"unlock_notify",
"url",
"uuid",
"vtab",
"window",
]
modern_sqlite = ["libsqlite3-sys/bundled_bindings"]
pointer = []
preupdate_hook = [
"libsqlite3-sys/preupdate_hook",
"hooks",
]
serialize = []
series = ["vtab"]
session = [
"libsqlite3-sys/session",
"hooks",
]
sqlcipher = ["libsqlite3-sys/sqlcipher"]
trace = []
unlock_notify = ["libsqlite3-sys/unlock_notify"]
vtab = []
wasm32-wasi-vfs = ["libsqlite3-sys/wasm32-wasi-vfs"]
window = ["functions"]
with-asan = ["libsqlite3-sys/with-asan"]
[lib]
name = "rusqlite"
path = "src/lib.rs"
[[example]]
name = "load_extension"
path = "examples/load_extension.rs"
required-features = [
"load_extension",
"bundled",
"functions",
"trace",
]
[[example]]
name = "loadable_extension"
crate-type = ["cdylib"]
path = "examples/loadable_extension.rs"
required-features = [
"loadable_extension",
"functions",
"trace",
]
[[example]]
name = "owning_rows"
path = "examples/owning_rows.rs"
[[example]]
name = "owning_statement"
path = "examples/owning_statement.rs"
[[example]]
name = "persons"
path = "examples/persons/main.rs"
[[test]]
name = "auto_ext"
path = "tests/auto_ext.rs"
[[test]]
name = "config_log"
path = "tests/config_log.rs"
harness = false
[[test]]
name = "deny_single_threaded_sqlite_config"
path = "tests/deny_single_threaded_sqlite_config.rs"
[[test]]
name = "vtab"
path = "tests/vtab.rs"
[[bench]]
name = "cache"
path = "benches/cache.rs"
harness = false
[[bench]]
name = "exec"
path = "benches/exec.rs"
harness = false
[dependencies.bitflags]
version = "2.6.0"
[dependencies.chrono]
version = "0.4.42"
features = ["clock"]
optional = true
default-features = false
[dependencies.csv]
version = "1.1"
optional = true
[dependencies.fallible-iterator]
version = "0.3"
[dependencies.fallible-streaming-iterator]
version = "0.1"
[dependencies.hashlink]
version = "0.11"
optional = true
[dependencies.jiff]
version = "0.2"
features = ["std"]
optional = true
default-features = false
[dependencies.rusqlite-macros]
version = "0.4.2"
optional = true
[dependencies.serde_json]
version = "1.0"
optional = true
[dependencies.smallvec]
version = "1.6.1"
[dependencies.time]
version = "0.3.47"
features = [
"formatting",
"macros",
"parsing",
]
optional = true
[dependencies.url]
version = "2.1"
optional = true
[dependencies.uuid]
version = "1.0"
optional = true
[dev-dependencies.bencher]
version = "0.1"
[dev-dependencies.doc-comment]
version = "0.3"
[dev-dependencies.regex]
version = "1.5.5"
[dev-dependencies.self_cell]
version = "1.1.0"
[dev-dependencies.tempfile]
version = "3.1.0"
[dev-dependencies.unicase]
version = "2.6.0"
[dev-dependencies.uuid]
version = "1.0"
features = ["v4"]
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies.chrono]
version = "0.4.42"
features = ["wasmbind"]
optional = true
default-features = false
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies.jiff]
version = "0.2"
features = ["js"]
optional = true
default-features = false
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies.sqlite-wasm-rs]
version = "0.5.1"
default-features = false
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies.time]
version = "0.3.47"
features = ["wasm-bindgen"]
optional = true
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies.uuid]
version = "1.0"
features = ["js"]
optional = true
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies.getrandom]
version = "0.4"
features = ["wasm_js"]
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies.uuid]
version = "1.0"
features = ["js"]
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies.wasm-bindgen]
version = "0.2.104"
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies.wasm-bindgen-test]
version = "0.3.54"
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies.libsqlite3-sys]
version = "0.37.0"

242
vendor/rusqlite/Cargo.toml.orig generated vendored
View File

@@ -1,242 +0,0 @@
[package]
name = "rusqlite"
# Note: Update version in README.md when you change this.
version = "0.37.99"
authors = ["The rusqlite developers"]
edition = "2021"
description = "Ergonomic wrapper for SQLite"
repository = "https://github.com/rusqlite/rusqlite"
documentation = "https://docs.rs/rusqlite/"
readme = "README.md"
keywords = ["sqlite", "database", "ffi"]
license = "MIT"
categories = ["database"]
exclude = [
"/.github/*",
"/.gitattributes",
"/appveyor.yml",
"/Changelog.md",
"/clippy.toml",
"/codecov.yml",
"**/*.sh",
]
[badges]
appveyor = { repository = "rusqlite/rusqlite" }
codecov = { repository = "rusqlite/rusqlite" }
maintenance = { status = "actively-developed" }
[lib]
name = "rusqlite"
[workspace]
members = ["libsqlite3-sys"]
[features]
# if not SQLITE_OMIT_LOAD_EXTENSION
load_extension = []
# hot-backup interface
backup = []
# if not SQLITE_OMIT_INCRBLOB
# sqlite3_blob
blob = []
# Prepared statements cache by connection (like https://www.sqlite.org/tclsqlite.html#cache)
cache = ["hashlink"]
# sqlite3_create_collation_v2
collation = []
# sqlite3_create_function_v2
functions = []
# sqlite3_log / sqlite3_trace_v2
trace = []
# Use bundled SQLite sources (instead of the one provided by your OS / distribution)
bundled = ["libsqlite3-sys/bundled", "modern_sqlite"]
# Use SQLCipher instead of SQLite
bundled-sqlcipher = ["libsqlite3-sys/bundled-sqlcipher", "bundled"]
bundled-sqlcipher-vendored-openssl = [
"libsqlite3-sys/bundled-sqlcipher-vendored-openssl",
"bundled-sqlcipher",
]
buildtime_bindgen = ["libsqlite3-sys/buildtime_bindgen", "sqlite-wasm-rs/bindgen"]
# sqlite3_limit
limits = []
# Used to generate a cdylib
loadable_extension = ["libsqlite3-sys/loadable_extension"]
# sqlite3_commit_hook, sqlite3_rollback_hook, ...
hooks = []
# if SQLITE_ENABLE_PREUPDATE_HOOK
preupdate_hook = ["libsqlite3-sys/preupdate_hook", "hooks"]
# u64, usize, NonZeroU64, NonZeroUsize
fallible_uint = []
i128_blob = []
sqlcipher = ["libsqlite3-sys/sqlcipher"]
# SQLITE_ENABLE_UNLOCK_NOTIFY
unlock_notify = ["libsqlite3-sys/unlock_notify"]
# if not SQLITE_OMIT_VIRTUALTABLE
# sqlite3_vtab
vtab = []
csvtab = ["csv", "vtab"]
# Port of Carray() table-valued function
array = ["vtab", "pointer"]
# if SQLITE_ENABLE_SESSION
# session extension
session = ["libsqlite3-sys/session", "hooks"]
# if not SQLITE_OMIT_WINDOWFUNC
# sqlite3_create_window_function
window = ["functions"]
# Port of generate_series table-valued function
series = ["vtab"]
# check for invalid query.
extra_check = []
# ]3.34.1, last]
modern_sqlite = ["libsqlite3-sys/bundled_bindings"]
in_gecko = ["modern_sqlite", "libsqlite3-sys/in_gecko"]
bundled-windows = ["libsqlite3-sys/bundled-windows"]
# Build bundled sqlite with -fsanitize=address
with-asan = ["libsqlite3-sys/with-asan"]
# if SQLITE_ENABLE_COLUMN_METADATA
column_metadata = ["libsqlite3-sys/column_metadata"]
# if not SQLITE_OMIT_DECLTYPE
column_decltype = []
wasm32-wasi-vfs = ["libsqlite3-sys/wasm32-wasi-vfs"]
# if not SQLITE_OMIT_DESERIALIZE
serialize = []
# pointer passing interfaces: 3.20.0
pointer = []
# Helper feature for enabling most non-build-related optional features
# or dependencies (except `session`). This is useful for running tests / clippy
# / etc. New features and optional dependencies that don't conflict with anything
# else should be added here.
modern-full = [
"array",
"backup",
"blob",
"modern_sqlite",
"chrono",
"collation",
"column_metadata",
"column_decltype",
"csvtab",
"extra_check",
"functions",
"hooks",
"i128_blob",
"jiff",
"limits",
"load_extension",
"serde_json",
"serialize",
"series",
"time",
"trace",
"unlock_notify",
"url",
"uuid",
"vtab",
"window",
]
bundled-full = ["modern-full", "bundled"]
default = ["cache"]
[dependencies]
# Jiff Date/Time/Timestamp persistence
jiff = { version = "0.2", optional = true, default-features = false, features = [
"std",
] }
# Date/Time/Timestamp persistence
time = { version = "0.3.47", features = [
"formatting",
"macros",
"parsing",
], optional = true }
bitflags = "2.6.0"
# LRU cache of statement
hashlink = { version = "0.11", optional = true }
# Chrono Date/Time/Timestamp persistence
chrono = { version = "0.4.42", optional = true, default-features = false, features = [
"clock",
] }
# JSON persistence
serde_json = { version = "1.0", optional = true }
# Virtual table
csv = { version = "1.1", optional = true }
# Url persistence
url = { version = "2.1", optional = true }
fallible-iterator = "0.3"
fallible-streaming-iterator = "0.1"
# Uuid persistence
uuid = { version = "1.0", optional = true }
smallvec = "1.6.1"
# WIP comptime checks
rusqlite-macros = { path = "rusqlite-macros", version = "0.4.2", optional = true }
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
libsqlite3-sys = { path = "libsqlite3-sys", version = "0.37.0" }
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
sqlite-wasm-rs = { version = "0.5.1", default-features = false }
chrono = { version = "0.4.42", optional = true, default-features = false, features = ["wasmbind"] }
jiff = { version = "0.2", optional = true, default-features = false, features = ["js"] }
time = { version = "0.3.47", optional = true, features = ["wasm-bindgen"] }
uuid = { version = "1.0", optional = true, features = ["js"] }
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies]
# Something is dependent on them, we use feature to override it.
uuid = { version = "1.0", features = ["js"] }
getrandom = { version = "0.4", features = ["wasm_js"] }
wasm-bindgen-test = "0.3.54"
wasm-bindgen = "0.2.104"
[dev-dependencies]
doc-comment = "0.3"
tempfile = "3.1.0"
regex = "1.5.5"
uuid = { version = "1.0", features = ["v4"] }
unicase = "2.6.0"
self_cell = "1.1.0"
# Use `bencher` over criterion because it builds much faster,
# and we don't have many benchmarks
bencher = "0.1"
[[test]]
name = "auto_ext"
[[test]]
name = "config_log"
harness = false
[[test]]
name = "deny_single_threaded_sqlite_config"
[[test]]
name = "vtab"
[[bench]]
name = "cache"
harness = false
[[bench]]
name = "exec"
harness = false
[[example]]
name = "loadable_extension"
crate-type = ["cdylib"]
required-features = ["loadable_extension", "functions", "trace"]
[[example]]
name = "load_extension"
required-features = ["load_extension", "bundled", "functions", "trace"]
[package.metadata.docs.rs]
features = ["modern-full", "rusqlite-macros"]
all-features = false
no-default-features = false
default-target = "x86_64-unknown-linux-gnu"
rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.playground]
features = ["bundled-full"]
all-features = false

View File

@@ -1,19 +0,0 @@
Copyright (c) 2014 The rusqlite developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -1,259 +0,0 @@
# Rusqlite
[![Latest Version](https://img.shields.io/crates/v/rusqlite.svg)](https://crates.io/crates/rusqlite)
[![Documentation](https://docs.rs/rusqlite/badge.svg)](https://docs.rs/rusqlite)
[![Build Status (GitHub)](https://github.com/rusqlite/rusqlite/workflows/CI/badge.svg)](https://github.com/rusqlite/rusqlite/actions)
[![Build Status (AppVeyor)](https://ci.appveyor.com/api/projects/status/github/rusqlite/rusqlite?branch=master&svg=true)](https://ci.appveyor.com/project/rusqlite/rusqlite)
[![Code Coverage](https://codecov.io/gh/rusqlite/rusqlite/branch/master/graph/badge.svg)](https://codecov.io/gh/rusqlite/rusqlite)
[![Dependency Status](https://deps.rs/repo/github/rusqlite/rusqlite/status.svg)](https://deps.rs/repo/github/rusqlite/rusqlite)
[![Discord Chat](https://img.shields.io/discord/927966344266256434.svg?logo=discord)](https://discord.gg/nFYfGPB8g4)
Rusqlite is an ergonomic wrapper for using SQLite from Rust.
Historically, the API was based on the one from [`rust-postgres`](https://github.com/sfackler/rust-postgres). However, the two have diverged in many ways, and no compatibility between the two is intended.
## Usage
In your Cargo.toml:
```toml
[dependencies]
# `bundled` causes us to automatically compile and link in an up to date
# version of SQLite for you. This avoids many common build issues, and
# avoids depending on the version of SQLite on the users system (or your
# system), which may be old or missing. It's the right choice for most
# programs that control their own SQLite databases.
#
# That said, it's not ideal for all scenarios and in particular, generic
# libraries built around `rusqlite` should probably not enable it, which
# is why it is not a default feature -- it could become hard to disable.
rusqlite = { version = "0.39.0", features = ["bundled"] }
```
Simple example usage:
```rust
use rusqlite::{Connection, Result};
#[derive(Debug)]
struct Person {
id: i32,
name: String,
data: Option<Vec<u8>>,
}
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute(
"CREATE TABLE person (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
data BLOB
)",
(), // empty list of parameters.
)?;
let me = Person {
id: 0,
name: "Steven".to_string(),
data: None,
};
conn.execute(
"INSERT INTO person (name, data) VALUES (?1, ?2)",
(&me.name, &me.data),
)?;
let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
let person_iter = stmt.query_map([], |row| {
Ok(Person {
id: row.get(0)?,
name: row.get(1)?,
data: row.get(2)?,
})
})?;
for person in person_iter {
println!("Found person {:?}", person.unwrap());
}
Ok(())
}
```
### Supported SQLite Versions
The base `rusqlite` package supports SQLite version 3.34.1 or newer. If you need
support for older versions, please file an issue. Some cargo features require a
newer SQLite version; see details below.
### Optional Features
Rusqlite provides several features that are behind [Cargo
features](https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section). They are:
* [`load_extension`](https://docs.rs/rusqlite/~0/rusqlite/struct.LoadExtensionGuard.html)
allows loading dynamic library-based SQLite extensions.
* `loadable_extension` to program [loadable extension](https://sqlite.org/loadext.html) in Rust.
* [`backup`](https://docs.rs/rusqlite/~0/rusqlite/backup/index.html)
allows use of SQLite's online backup API.
* [`functions`](https://docs.rs/rusqlite/~0/rusqlite/functions/index.html)
allows you to load Rust closures into SQLite connections for use in queries.
* `window` for [window function](https://www.sqlite.org/windowfunctions.html) support (`fun(...) OVER ...`). (Implies `functions`.)
* [`trace`](https://docs.rs/rusqlite/~0/rusqlite/trace/index.html)
allows hooks into SQLite's tracing and profiling APIs.
* [`blob`](https://docs.rs/rusqlite/~0/rusqlite/blob/index.html)
gives `std::io::{Read, Write, Seek}` access to SQL BLOBs.
* [`limits`](https://docs.rs/rusqlite/~0/rusqlite/struct.Connection.html#method.limit)
allows you to set and retrieve SQLite's per connection limits.
* `serde_json` implements [`FromSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.FromSql.html)
and [`ToSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.ToSql.html) for the
`Value` type from the [`serde_json` crate](https://crates.io/crates/serde_json).
* `chrono` implements [`FromSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.FromSql.html)
and [`ToSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.ToSql.html) for various
types from the [`chrono` crate](https://crates.io/crates/chrono).
* `time` implements [`FromSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.FromSql.html)
and [`ToSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.ToSql.html) for various
types from the [`time` crate](https://crates.io/crates/time).
* `jiff` implements [`FromSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.FromSql.html)
and [`ToSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.ToSql.html) for the
`Value` type from the [`jiff` crate](https://crates.io/crates/jiff).
* `url` implements [`FromSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.FromSql.html)
and [`ToSql`](https://docs.rs/rusqlite/~0/rusqlite/types/trait.ToSql.html) for the
`Url` type from the [`url` crate](https://crates.io/crates/url).
* `bundled` uses a bundled version of SQLite. This is a good option for cases where linking to SQLite is complicated, such as Windows.
* `sqlcipher` looks for the SQLCipher library to link against instead of SQLite. This feature overrides `bundled`.
* `bundled-sqlcipher` uses a bundled version of SQLCipher. This searches for and links against a system-installed crypto library to provide the crypto implementation.
* `bundled-sqlcipher-vendored-openssl` allows using bundled-sqlcipher with a vendored version of OpenSSL (via the `openssl-sys` crate) as the crypto provider.
- As the name implies this depends on the `bundled-sqlcipher` feature, and automatically turns it on.
- If turned on, this uses the [`openssl-sys`](https://crates.io/crates/openssl-sys) crate, with the `vendored` feature enabled in order to build and bundle the OpenSSL crypto library.
* `hooks` for [Commit, Rollback](http://sqlite.org/c3ref/commit_hook.html) and [Data Change](http://sqlite.org/c3ref/update_hook.html) notification callbacks.
* `preupdate_hook` for [preupdate](https://sqlite.org/c3ref/preupdate_count.html) notification callbacks. (Implies `hooks`.)
* `unlock_notify` for [Unlock](https://sqlite.org/unlock_notify.html) notification.
* `vtab` for [virtual table](https://sqlite.org/vtab.html) support (allows you to write virtual table implementations in Rust). Currently, only read-only virtual tables are supported.
* `series` exposes [`generate_series(...)`](https://www.sqlite.org/series.html) Table-Valued Function. (Implies `vtab`.)
* [`csvtab`](https://sqlite.org/csv.html), CSV virtual table written in Rust. (Implies `vtab`.)
* [`array`](https://sqlite.org/carray.html), The `rarray()` Table-Valued Function. (Implies `vtab`.)
* `fallible_uint` allows storing values of type `u64`, `usize`, `NonZeroU64`, `NonZeroUsize` but only if <= `i64::MAX`.
* `i128_blob` allows storing values of type `i128` type in SQLite databases. Internally, the data is stored as a 16 byte big-endian blob, with the most significant bit flipped, which allows ordering and comparison between different blobs storing i128s to work as expected.
* `uuid` allows storing and retrieving `Uuid` values from the [`uuid`](https://docs.rs/uuid/) crate using blobs.
* [`session`](https://sqlite.org/sessionintro.html), Session module extension. Requires `buildtime_bindgen` feature. (Implies `hooks`.)
* `extra_check` fails when a query passed to `execute` is readonly and has a column count > 0.
* `column_decltype` provides `columns()` method for Statements and Rows; omit if linking to a version of SQLite/SQLCipher compiled with `-DSQLITE_OMIT_DECLTYPE`.
* `collation` exposes [`sqlite3_create_collation_v2`](https://sqlite.org/c3ref/create_collation.html).
* `serialize` exposes [`sqlite3_serialize`](http://sqlite.org/c3ref/serialize.html) (3.23.0).
* `rusqlite-macros` enables the use of the [`prepare_and_bind`](https://docs.rs/rusqlite/~0/rusqlite/macro.prepare_and_bind.html)
and [`prepare_cached_and_bind`](https://docs.rs/rusqlite/~0/rusqlite/macro.prepare_cached_and_bind.html)
procedural macros, which allow capturing identifiers in SQL statements.
## Notes on building rusqlite and libsqlite3-sys
`libsqlite3-sys` is a separate crate from `rusqlite` that provides the Rust
declarations for SQLite's C API. By default, `libsqlite3-sys` attempts to find a SQLite library that already exists on your system using pkg-config, or a
[Vcpkg](https://github.com/Microsoft/vcpkg) installation for MSVC ABI builds.
You can adjust this behavior in a number of ways:
* If you use the `bundled`, `bundled-sqlcipher`, or `bundled-sqlcipher-vendored-openssl` features, `libsqlite3-sys` will use the
[cc](https://crates.io/crates/cc) crate to compile SQLite or SQLCipher from source and
link against that. This source is embedded in the `libsqlite3-sys` crate and
is currently SQLite 3.51.3 (as of `rusqlite` 0.39.0 / `libsqlite3-sys`
0.37.0). This is probably the simplest solution to any build problems. You can enable this by adding the following in your `Cargo.toml` file:
```toml
[dependencies.rusqlite]
version = "0.39.0"
features = ["bundled"]
```
* When using any of the `bundled` features, the build script will honor `SQLITE_MAX_VARIABLE_NUMBER` and `SQLITE_MAX_EXPR_DEPTH` variables. It will also honor a `LIBSQLITE3_FLAGS` variable, which can have a format like `"-USQLITE_ALPHA -DSQLITE_BETA SQLITE_GAMMA ..."`. That would disable the `SQLITE_ALPHA` flag, and set the `SQLITE_BETA` and `SQLITE_GAMMA` flags. (The initial `-D` can be omitted, as on the last one.)
* When using `bundled-sqlcipher` (and not also using `bundled-sqlcipher-vendored-openssl`), `libsqlite3-sys` will need to
link against crypto libraries on the system. If the build script can find a `libcrypto` from OpenSSL or LibreSSL (it will consult `OPENSSL_LIB_DIR`/`OPENSSL_INCLUDE_DIR` and `OPENSSL_DIR` environment variables), it will use that. If building on and for Macs, and none of those variables are set, it will use the system's SecurityFramework instead.
* When linking against a SQLite (or SQLCipher) library already on the system (so *not* using any of the `bundled` features), you can set the `SQLITE3_LIB_DIR` (or `SQLCIPHER_LIB_DIR`) environment variable to point to a directory containing the library. You can also set the `SQLITE3_INCLUDE_DIR` (or `SQLCIPHER_INCLUDE_DIR`) variable to point to the directory containing `sqlite3.h`.
* Installing the sqlite3 development packages will usually be all that is required, but
the build helpers for [pkg-config](https://github.com/alexcrichton/pkg-config-rs)
and [vcpkg](https://github.com/mcgoo/vcpkg-rs) have some additional configuration
options. The default when using vcpkg is to dynamically link,
which must be enabled by setting `VCPKGRS_DYNAMIC=1` environment variable before build.
`vcpkg install sqlite3:x64-windows` will install the required library.
* When linking against a SQLite (or SQLCipher) library already on the system, you can set the `SQLITE3_STATIC` (or `SQLCIPHER_STATIC`) environment variable to 1 to request that the library be statically instead of dynamically linked.
### Binding generation
We use [bindgen](https://crates.io/crates/bindgen) to generate the Rust
declarations from SQLite's C header file. `bindgen`
[recommends](https://github.com/servo/rust-bindgen#library-usage-with-buildrs)
running this as part of the build process of libraries that used this. We tried
this briefly (`rusqlite` 0.10.0, specifically), but it had some annoyances:
* The build time for `libsqlite3-sys` (and therefore `rusqlite`) increased
dramatically.
* Running `bindgen` requires a relatively-recent version of Clang, which many
systems do not have installed by default.
* Running `bindgen` also requires the SQLite header file to be present.
As of `rusqlite` 0.10.1, we avoid running `bindgen` at build-time by shipping
pregenerated bindings for several versions of SQLite. When compiling
`rusqlite`, we use your selected Cargo features to pick the bindings for the
minimum SQLite version that supports your chosen features. If you are using
`libsqlite3-sys` directly, you can use the same features to choose which
pregenerated bindings are chosen:
* `min_sqlite_version_3_34_1` - SQLite 3.34.1 bindings (this is the default)
If you use any of the `bundled` features, you will get pregenerated bindings for the
bundled version of SQLite/SQLCipher. If you need other specific pregenerated binding
versions, please file an issue. If you want to run `bindgen` at buildtime to
produce your own bindings, use the `buildtime_bindgen` Cargo feature.
If you enable the `modern_sqlite` feature, we'll use the bindings we would have
included with the bundled build. You generally should have `buildtime_bindgen`
enabled if you turn this on, as otherwise you'll need to keep the version of
SQLite you link with in sync with what rusqlite would have bundled, (usually the
most recent release of SQLite). Failing to do this will cause a runtime error.
## Contributing
Rusqlite has many features, and many of them impact the build configuration in
incompatible ways. This is unfortunate, and makes testing changes hard.
To help here: you generally should ensure that you run tests/lint for
`--features bundled`, and `--features "bundled-full session buildtime_bindgen"`.
If running bindgen is problematic for you, `--features bundled-full` enables
bundled and all features which don't require binding generation, and can be used
instead.
### Checklist
- Run `cargo fmt` to ensure your Rust code is correctly formatted.
- Ensure `cargo clippy --workspace --features bundled` passes without warnings.
- Ensure `cargo clippy --workspace --features "bundled-full session buildtime_bindgen"` passes without warnings.
- Ensure `cargo test --workspace --features bundled` reports no failures.
- Ensure `cargo test --workspace --features "bundled-full session buildtime_bindgen"` reports no failures.
## Author
Rusqlite is the product of hard work by a number of people. A list is available
here: https://github.com/rusqlite/rusqlite/graphs/contributors
## Community
Feel free to join the [Rusqlite Discord Server](https://discord.gg/nFYfGPB8g4) to discuss or get help with `rusqlite` or `libsqlite3-sys`.
## License
Rusqlite and libsqlite3-sys are available under the MIT license. See the LICENSE file for more info.
### Licenses of Bundled Software
Depending on the set of enabled cargo `features`, rusqlite and libsqlite3-sys will also bundle other libraries, which have their own licensing terms:
- If `--features=bundled-sqlcipher` is enabled, the vendored source of [SQLcipher](https://github.com/sqlcipher/sqlcipher) will be compiled and statically linked in. SQLcipher is distributed under a BSD-style license, as described [here](libsqlite3-sys/sqlcipher/LICENSE).
- If `--features=bundled` is enabled, the vendored source of SQLite will be compiled and linked in. SQLite is in the public domain, as described [here](https://www.sqlite.org/copyright.html).
Both of these are quite permissive, have no bearing on the license of the code in `rusqlite` or `libsqlite3-sys` themselves, and can be entirely ignored if you do not use the feature in question.
## Minimum supported Rust version (MSRV)
Latest stable Rust version at the time of release. It might compile with older versions.

View File

@@ -1,18 +0,0 @@
use bencher::{benchmark_group, benchmark_main, Bencher};
use rusqlite::Connection;
fn bench_no_cache(b: &mut Bencher) {
let db = Connection::open_in_memory().unwrap();
db.set_prepared_statement_cache_capacity(0);
let sql = "SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71";
b.iter(|| db.prepare(sql).unwrap());
}
fn bench_cache(b: &mut Bencher) {
let db = Connection::open_in_memory().unwrap();
let sql = "SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71";
b.iter(|| db.prepare_cached(sql).unwrap());
}
benchmark_group!(cache_benches, bench_no_cache, bench_cache);
benchmark_main!(cache_benches);

View File

@@ -1,17 +0,0 @@
use bencher::{benchmark_group, benchmark_main, Bencher};
use rusqlite::Connection;
fn bench_execute(b: &mut Bencher) {
let db = Connection::open_in_memory().unwrap();
let sql = "PRAGMA user_version=1";
b.iter(|| db.execute(sql, []).unwrap());
}
fn bench_execute_batch(b: &mut Bencher) {
let db = Connection::open_in_memory().unwrap();
let sql = "PRAGMA user_version=1";
b.iter(|| db.execute_batch(sql).unwrap());
}
benchmark_group!(exec_benches, bench_execute, bench_execute_batch);
benchmark_main!(exec_benches);

View File

@@ -1,405 +0,0 @@
# List of SQLite functions supported
- [ ] `sqlite3_version`
- [X] `sqlite3_libversion`
- [ ] `sqlite3_sourceid`
- [X] `sqlite3_libversion_number`
- [ ] `sqlite3_compileoption_used`
- [ ] `sqlite3_compileoption_get`
- [X] `sqlite3_threadsafe` (internal use only)
- [X] `sqlite3_close`
- [ ] `sqlite3_close_v2`
- [ ] `sqlite3_exec`
- [ ] `sqlite3_initialize`
- [ ] `sqlite3_shutdown`
- [ ] `sqlite3_os_init`
- [ ] `sqlite3_os_end`
- [ ] `sqlite3_config` (partially, `fn` callback for SQLITE_CONFIG_LOG) (cannot be used by a loadable extension)
- [X] `sqlite3_db_config`
- [X] `sqlite3_extended_result_codes` (not public, internal use only)
- [X] `sqlite3_last_insert_rowid`
- [ ] `sqlite3_set_last_insert_rowid`
- [X] `sqlite3_changes`
- [X] `sqlite3_changes64`
- [X] `sqlite3_total_changes`
- [X] `sqlite3_total_changes64`
- [X] `sqlite3_interrupt`
- [X] `sqlite3_is_interrupted`
- [ ] `sqlite3_complete`
- [X] `sqlite3_busy_handler` (`fn` callback)
- [X] `sqlite3_busy_timeout`
- [ ] `sqlite3_get_table`
- [ ] `sqlite3_mprintf`
- [ ] `sqlite3_vmprintf`
- [ ] `sqlite3_snprintf`
- [ ] `sqlite3_vsnprintf`
- [ ] `sqlite3_malloc`
- [X] `sqlite3_malloc64` (not public, internal use only)
- [ ] `sqlite3_realloc`
- [ ] `sqlite3_realloc64`
- [X] `sqlite3_free` (not public, internal use only)
- [ ] `sqlite3_msize`
- [ ] `sqlite3_memory_used`
- [ ] `sqlite3_memory_highwater`
- [ ] `sqlite3_randomness`
- [X] `sqlite3_set_authorizer` (`FnMut` callback, reference kept)
- [X] `sqlite3_trace` deprecated (`fn` callback)
- [X] `sqlite3_profile` deprecated (`fn` callback)
- [X] `sqlite3_trace_v2` (`fn` callback, no context data)
- [X] `sqlite3_progress_handler` (`FnMut` callback, reference kept)
- [ ] `sqlite3_open`
- [X] `sqlite3_open_v2`
- [ ] `sqlite3_uri_parameter`
- [ ] `sqlite3_uri_boolean`
- [ ] `sqlite3_uri_int64`
- [ ] `sqlite3_uri_key`
- [ ] `sqlite3_filename_database`
- [ ] `sqlite3_filename_journal`
- [ ] `sqlite3_filename_wal`
- [ ] `sqlite3_database_file_object`
- [ ] `sqlite3_create_filename`
- [ ] `sqlite3_free_filename`
- [X] `sqlite3_errcode`
- [X] `sqlite3_extended_errcode`
- [X] `sqlite3_errmsg` (not public, internal use only)
- [X] `sqlite3_errstr` (not public, internal use only)
- [X] `sqlite3_error_offset`
- [X] `sqlite3_limit`
- [ ] `sqlite3_prepare`
- [X] `sqlite3_prepare_v2`
- [X] `sqlite3_prepare_v3`
- [X] `sqlite3_sql` (not public, internal use only)
- [X] `sqlite3_expanded_sql`
- [ ] `sqlite3_normalized_sql`
- [X] `sqlite3_stmt_readonly`
- [X] `sqlite3_stmt_isexplain`
- [ ] `sqlite3_stmt_explain`
- [X] `sqlite3_stmt_busy`
- [ ] `sqlite3_bind_blob`
- [X] `sqlite3_bind_blob64`
- [X] `sqlite3_bind_double`
- [ ] `sqlite3_bind_int`
- [X] `sqlite3_bind_int64`
- [X] `sqlite3_bind_null`
- [ ] `sqlite3_bind_text`
- [X] `sqlite3_bind_text64`
- [ ] `sqlite3_bind_value`
- [X] `sqlite3_bind_pointer`
- [X] `sqlite3_bind_zeroblob`
- [ ] `sqlite3_bind_zeroblob64`
- [X] `sqlite3_bind_parameter_count`
- [X] `sqlite3_bind_parameter_name`
- [X] `sqlite3_bind_parameter_index`
- [X] `sqlite3_clear_bindings`
- [X] `sqlite3_column_count`
- [ ] `sqlite3_data_count`
- [X] `sqlite3_column_name`
- [X] `sqlite3_column_database_name`
- [X] `sqlite3_column_table_name`
- [X] `sqlite3_column_origin_name`
- [X] `sqlite3_column_decltype`
- [X] `sqlite3_step`
- [X] `sqlite3_column_blob`
- [X] `sqlite3_column_double`
- [ ] `sqlite3_column_int`
- [X] `sqlite3_column_int64`
- [X] `sqlite3_column_text`
- [X] `sqlite3_column_value` (not public, internal use only)
- [X] `sqlite3_column_bytes` (not public, internal use only)
- [X] `sqlite3_column_type`
- [X] `sqlite3_finalize`
- [X] `sqlite3_reset` (not public, internal use only)
- [ ] `sqlite3_create_function`
- [X] `sqlite3_create_function_v2` (Boxed callback, destroyed by SQLite)
- [X] `sqlite3_create_window_function` (Boxed callback, destroyed by SQLite)
- [X] `sqlite3_value_blob`
- [X] `sqlite3_value_double`
- [ ] `sqlite3_value_int`
- [X] `sqlite3_value_int64`
- [X] `sqlite3_value_pointer`
- [X] `sqlite3_value_text`
- [X] `sqlite3_value_bytes` (not public, internal use only)
- [X] `sqlite3_value_type`
- [ ] `sqlite3_value_numeric_type`
- [X] `sqlite3_value_nochange`
- [ ] `sqlite3_value_frombind`
- [ ] `sqlite3_value_encoding`
- [X] `sqlite3_value_subtype`
- [ ] `sqlite3_value_dup`
- [ ] `sqlite3_value_free`
- [X] `sqlite3_aggregate_context` (not public, internal use only)
- [X] `sqlite3_user_data` (not public, internal use only)
- [X] `sqlite3_context_db_handle` (Connection ref)
- [X] `sqlite3_get_auxdata`
- [X] `sqlite3_set_auxdata`
- [ ] `sqlite3_get_clientdata`
- [ ] `sqlite3_set_clientdata`
- [ ] `sqlite3_result_blob`
- [X] `sqlite3_result_blob64`
- [X] `sqlite3_result_double`
- [X] `sqlite3_result_error`
- [X] `sqlite3_result_error_toobig`
- [X] `sqlite3_result_error_nomem`
- [X] `sqlite3_result_error_code`
- [ ] `sqlite3_result_int`
- [X] `sqlite3_result_int64`
- [X] `sqlite3_result_null`
- [ ] `sqlite3_result_text`
- [X] `sqlite3_result_text64`
- [X] `sqlite3_result_value`
- [X] `sqlite3_result_pointer`
- [X] `sqlite3_result_zeroblob`
- [ ] `sqlite3_result_zeroblob64`
- [X] `sqlite3_result_subtype`
- [ ] `sqlite3_create_collation`
- [X] `sqlite3_create_collation_v2` (Boxed callback, destroyed by SQLite)
- [X] `sqlite3_collation_needed` (`fn` callback)
- [ ] `sqlite3_sleep`
- [X] `sqlite3_get_autocommit`
- [X] `sqlite3_db_handle` (not public, internal use only, Connection ref)
- [X] `sqlite3_db_name`
- [X] `sqlite3_db_filename`
- [X] `sqlite3_db_readonly`
- [X] `sqlite3_txn_state`
- [X] `sqlite3_next_stmt` (not public, internal use only)
- [X] `sqlite3_commit_hook` (`FnMut` callback, reference kept)
- [X] `sqlite3_rollback_hook` (`FnMut` callback, reference kept)
- [ ] `sqlite3_autovacuum_pages`
- [X] `sqlite3_update_hook` (`FnMut` callback, reference kept)
- [ ] `sqlite3_enable_shared_cache`
- [ ] `sqlite3_release_memory`
- [X] `sqlite3_db_release_memory`
- [ ] `sqlite3_soft_heap_limit64`
- [ ] `sqlite3_hard_heap_limit64`
- [X] `sqlite3_table_column_metadata`
- [X] `sqlite3_load_extension`
- [X] `sqlite3_enable_load_extension` (cannot be used by a loadable extension)
- [X] `sqlite3_auto_extension` (`fn` callbak with Connection ref)
- [X] `sqlite3_cancel_auto_extension`
- [X] `sqlite3_reset_auto_extension`
- [ ] `sqlite3_create_module`
- [X] `sqlite3_create_module_v2`
- [ ] `sqlite3_drop_modules`
- [X] `sqlite3_declare_vtab`
- [ ] `sqlite3_overload_function`
- [X] `sqlite3_blob_open`
- [X] `sqlite3_blob_reopen`
- [X] `sqlite3_blob_close`
- [X] `sqlite3_blob_bytes`
- [X] `sqlite3_blob_read`
- [X] `sqlite3_blob_write`
- [ ] `sqlite3_vfs_find`
- [ ] `sqlite3_vfs_register`
- [ ] `sqlite3_vfs_unregister`
- [ ] `sqlite3_mutex_alloc`
- [ ] `sqlite3_mutex_free`
- [ ] `sqlite3_mutex_enter`
- [ ] `sqlite3_mutex_try`
- [ ] `sqlite3_mutex_leave`
- [ ] `sqlite3_mutex_held`
- [ ] `sqlite3_mutex_notheld`
- [ ] `sqlite3_db_mutex`
- [X] `sqlite3_file_control` (not public, internal use only)
- [ ] `sqlite3_test_control`
- [ ] `sqlite3_keyword_count`
- [ ] `sqlite3_keyword_name`
- [ ] `sqlite3_keyword_check`
- [ ] `sqlite3_str_new`
- [ ] `sqlite3_str_finish`
- [ ] `sqlite3_str_append`
- [ ] `sqlite3_str_reset`
- [ ] `sqlite3_str_errcode`
- [ ] `sqlite3_str_length`
- [ ] `sqlite3_str_value`
- [ ] `sqlite3_status`
- [ ] `sqlite3_status64`
- [ ] `sqlite3_db_status`
- [X] `sqlite3_stmt_status`
- [X] `sqlite3_backup_init`
- [X] `sqlite3_backup_step`
- [X] `sqlite3_backup_finish`
- [X] `sqlite3_backup_remaining`
- [X] `sqlite3_backup_pagecount`
- [X] `sqlite3_unlock_notify` (`fn` callback, internal use only)
- [ ] `sqlite3_stricmp`
- [ ] `sqlite3_strnicmp`
- [ ] `sqlite3_strglob`
- [ ] `sqlite3_strlike`
- [X] `sqlite3_log`
- [X] `sqlite3_wal_hook` (`fn` callback with Connection ref)
- [ ] `sqlite3_wal_autocheckpoint`
- [X] `sqlite3_wal_checkpoint`
- [X] `sqlite3_wal_checkpoint_v2`
- [X] `sqlite3_vtab_config`
- [X] `sqlite3_vtab_on_conflict`
- [X] `sqlite3_vtab_nochange`
- [X] `sqlite3_vtab_collation`
- [X] `sqlite3_vtab_distinct`
- [X] `sqlite3_vtab_in`
- [X] `sqlite3_vtab_in_first`
- [X] `sqlite3_vtab_in_next`
- [X] `sqlite3_vtab_rhs_value`
- [ ] `sqlite3_stmt_scanstatus`
- [ ] `sqlite3_stmt_scanstatus_v2`
- [ ] `sqlite3_stmt_scanstatus_reset`
- [X] `sqlite3_db_cacheflush`
- [X] `sqlite3_preupdate_hook` (`FnMut` callback with Connection ref, reference kept) (cannot be used by a loadable extension)
- [X] `sqlite3_preupdate_old`
- [X] `sqlite3_preupdate_count`
- [X] `sqlite3_preupdate_depth`
- [X] `sqlite3_preupdate_new`
- [ ] `sqlite3_preupdate_blobwrite`
- [ ] `sqlite3_system_errno`
- [ ] `sqlite3_snapshot_get`
- [ ] `sqlite3_snapshot_open`
- [ ] `sqlite3_snapshot_free`
- [ ] `sqlite3_snapshot_cmp`
- [ ] `sqlite3_snapshot_recover`
- [X] `sqlite3_serialize`
- [X] `sqlite3_deserialize`
- [ ] `sqlite3_rtree_geometry_callback`
- [ ] `sqlite3_rtree_query_callback`
- [X] `sqlite3session_create`
- [X] `sqlite3session_delete`
- [ ] `sqlite3session_object_config`
- [X] `sqlite3session_enable`
- [X] `sqlite3session_indirect`
- [X] `sqlite3session_attach`
- [X] `sqlite3session_table_filter` (Boxed callback, reference kept)
- [X] `sqlite3session_changeset`
- [ ] `sqlite3session_changeset_size`
- [X] `sqlite3session_diff`
- [X] `sqlite3session_patchset`
- [X] `sqlite3session_isempty`
- [ ] `sqlite3session_memory_used`
- [X] `sqlite3changeset_start`
- [ ] `sqlite3changeset_start_v2`
- [X] `sqlite3changeset_next`
- [X] `sqlite3changeset_op`
- [X] `sqlite3changeset_pk`
- [X] `sqlite3changeset_old`
- [X] `sqlite3changeset_new`
- [X] `sqlite3changeset_conflict`
- [X] `sqlite3changeset_fk_conflicts`
- [X] `sqlite3changeset_finalize`
- [X] `sqlite3changeset_invert`
- [X] `sqlite3changeset_concat`
- [ ] `sqlite3changeset_upgrade`
- [X] `sqlite3changegroup_new`
- [ ] `sqlite3changegroup_schema`
- [X] `sqlite3changegroup_add`
- [ ] `sqlite3changegroup_add_change`
- [X] `sqlite3changegroup_output`
- [X] `sqlite3changegroup_delete`
- [X] `sqlite3changeset_apply`
- [ ] `sqlite3changeset_apply_v2`
- [ ] `sqlite3rebaser_create`
- [ ] `sqlite3rebaser_configure`
- [ ] `sqlite3rebaser_rebase`
- [ ] `sqlite3rebaser_delete`
- [X] `sqlite3changeset_apply_strm`
- [ ] `sqlite3changeset_apply_v2_strm`
- [X] `sqlite3changeset_concat_strm`
- [X] `sqlite3changeset_invert_strm`
- [X] `sqlite3changeset_start_strm`
- [ ] `sqlite3changeset_start_v2_strm`
- [X] `sqlite3session_changeset_strm`
- [X] `sqlite3session_patchset_strm`
- [X] `sqlite3changegroup_add_strm`
- [X] `sqlite3changegroup_add_strm`
- [X] `sqlite3changegroup_output_strm`
- [ ] `sqlite3rebaser_rebase_strm`
- [ ] `sqlite3session_config`
## List of virtual table methods supported
- [X] `xCreate`
- [X] `xConnect`
- [X] `xBestIndex`
- [X] `xDisconnect`
- [X] `xDestroy`
- [X] `xOpen`
- [X] `xClose`
- [X] `xFilter`
- [X] `xNext`
- [X] `xEof`
- [X] `xColumn`
- [X] `xRowid`
- [X] `xUpdate`
- [X] `xBegin`
- [X] `xSync`
- [X] `xCommit`
- [X] `xRollback`
- [ ] `xFindFunction`
- [ ] `xRename`
- [ ] `xSavepoint`
- [ ] `xRelease`
- [ ] `xRollbackTo`
- [ ] `xShadowName`
- [ ] `xIntegrity`

View File

@@ -1,23 +0,0 @@
//! Ensure `loadable_extension.rs` works.
use rusqlite::{Connection, Result};
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
fn main() -> Result<()> {
let db = Connection::open_in_memory()?;
unsafe {
db.load_extension_enable()?;
db.load_extension(
format!("target/debug/examples/{DLL_PREFIX}loadable_extension{DLL_SUFFIX}"),
None::<&str>,
)?;
db.load_extension_disable()?;
}
let str = db.query_row("SELECT rusqlite_test_function()", [], |row| {
row.get::<_, String>(0)
})?;
assert_eq!(&str, "Rusqlite extension loaded correctly!");
Ok(())
}

View File

@@ -1,49 +0,0 @@
//! Adaptation of https://sqlite.org/loadext.html#programming_loadable_extensions
//!
//! # build
//! ```sh
//! cargo build --example loadable_extension --features "loadable_extension functions trace"
//! ```
//!
//! # test
//! ```sh
//! sqlite> .log on
//! sqlite> .load target/debug/examples/libloadable_extension.so
//! (28) Rusqlite extension initialized
//! sqlite> SELECT rusqlite_test_function();
//! Rusqlite extension loaded correctly!
//! ```
use std::os::raw::{c_char, c_int};
use rusqlite::ffi;
use rusqlite::functions::FunctionFlags;
use rusqlite::types::{ToSqlOutput, Value};
use rusqlite::{Connection, Result};
/// Entry point for SQLite to load the extension.
/// See <https://sqlite.org/c3ref/load_extension.html> on this function's name and usage.
/// # Safety
/// This function is called by SQLite and must be safe to call.
#[no_mangle]
pub unsafe extern "C" fn sqlite3_extension_init(
db: *mut ffi::sqlite3,
pz_err_msg: *mut *mut c_char,
p_api: *mut ffi::sqlite3_api_routines,
) -> c_int {
Connection::extension_init2(db, pz_err_msg, p_api, extension_init)
}
fn extension_init(db: Connection) -> Result<bool> {
db.create_scalar_function(
c"rusqlite_test_function",
0,
FunctionFlags::SQLITE_DETERMINISTIC,
|_ctx| {
Ok(ToSqlOutput::Owned(Value::Text(
"Rusqlite extension loaded correctly!".to_string(),
)))
},
)?;
rusqlite::trace::log(ffi::SQLITE_WARNING, "Rusqlite extension initialized");
Ok(false)
}

View File

@@ -1,27 +0,0 @@
extern crate rusqlite;
use rusqlite::{CachedStatement, Connection, Result, Rows};
use self_cell::{self_cell, MutBorrow};
type RowsRef<'a> = Rows<'a>;
self_cell!(
struct OwningRows<'conn> {
owner: MutBorrow<CachedStatement<'conn>>,
#[covariant]
dependent: RowsRef,
}
);
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
let stmt = conn.prepare_cached("SELECT 1")?;
let mut or = OwningRows::try_new(MutBorrow::new(stmt), |s| s.borrow_mut().query([]))?;
or.with_dependent_mut(|_stmt, rows| -> Result<()> {
while let Some(row) = rows.next()? {
assert_eq!(Ok(1), row.get(0));
}
Ok(())
})?;
Ok(())
}

View File

@@ -1,30 +0,0 @@
extern crate rusqlite;
use rusqlite::{CachedStatement, Connection, Result, Rows};
use self_cell::{self_cell, MutBorrow};
type CachedStatementRef<'a> = CachedStatement<'a>;
// Caveat: single statement at a time for one connection.
// But if you need multiple statements, you can still create your own struct
// with multiple fields (one for each statement).
self_cell!(
struct OwningStatement {
owner: MutBorrow<Connection>,
#[covariant]
dependent: CachedStatementRef,
}
);
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
let mut os = OwningStatement::try_new(MutBorrow::new(conn), |s| {
s.borrow_mut().prepare_cached("SELECT 1")
})?;
let mut rows = os.with_dependent_mut(|_conn, stmt| -> Result<Rows<'_>> { stmt.query([]) })?;
while let Some(row) = rows.next()? {
assert_eq!(Ok(1), row.get(0));
}
Ok(())
}

View File

@@ -1,48 +0,0 @@
# Persons example
## Run
```
$ cargo run --example persons
```
## Run (wasm32-wasi)
### Requisites
- [wasi-sdk](https://github.com/WebAssembly/wasi-sdk)
- [wasmtime](https://wasmtime.dev/)
```
# Set to wasi-sdk directory
$ export WASI_SDK_PATH=`<wasi-sdk-path>`
$ export CC_wasm32_wasi="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"
# Build
$ cargo build --example persons --target wasm32-wasi --release --features bundled
# Run
$ wasmtime target/wasm32-wasi/release/examples/persons.wasm
Found persons:
ID: 1, Name: Steven
ID: 2, Name: John
ID: 3, Name: Alex
```
## Run (wasm32-unknown-unknown)
### Requisites
- [emscripten](https://emscripten.org/docs/getting_started/downloads.html)
- [wasm-bindgen-cli](https://github.com/wasm-bindgen/wasm-bindgen)
```
# Build
$ cargo build --example persons --target wasm32-unknown-unknown --release
# Bindgen
$ wasm-bindgen target/wasm32-unknown-unknown/release/examples/persons.wasm --out-dir target/pkg --nodejs
# Run
$ node target/pkg/persons.js
Found persons:
ID: 1, Name: Steven
ID: 2, Name: John
ID: 3, Name: Alex
```

View File

@@ -1,57 +0,0 @@
use rusqlite::{Connection, Result};
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen::prelude::wasm_bindgen;
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
macro_rules! println {
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}
struct Person {
id: i32,
name: String,
}
#[cfg_attr(all(target_family = "wasm", target_os = "unknown"), wasm_bindgen(main))]
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute(
"CREATE TABLE IF NOT EXISTS persons (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
)",
(), // empty list of parameters.
)?;
conn.execute(
"INSERT INTO persons (name) VALUES (?1), (?2), (?3)",
["Steven", "John", "Alex"].map(|n| n.to_string()),
)?;
let mut stmt = conn.prepare("SELECT id, name FROM persons")?;
let rows = stmt.query_map([], |row| {
Ok(Person {
id: row.get(0)?,
name: row.get(1)?,
})
})?;
println!("Found persons:");
for person in rows {
match person {
Ok(p) => println!("ID: {}, Name: {}", p.id, p.name),
Err(e) => eprintln!("Error: {e:?}"),
}
}
Ok(())
}

View File

@@ -1,62 +0,0 @@
//! Automatic extension loading
use super::ffi;
use crate::error::{check, to_sqlite_error};
use crate::{Connection, Error, Result};
use std::ffi::{c_char, c_int};
use std::panic::catch_unwind;
/// Automatic extension initialization routine
pub type AutoExtension = fn(Connection) -> Result<()>;
/// Raw automatic extension initialization routine
pub type RawAutoExtension = unsafe extern "C" fn(
db: *mut ffi::sqlite3,
pz_err_msg: *mut *mut c_char,
_: *const ffi::sqlite3_api_routines,
) -> c_int;
/// Bridge between `RawAutoExtension` and `AutoExtension`
///
/// # Safety
/// * Opening a database from an auto-extension handler will lead to
/// an endless recursion of the auto-handler triggering itself
/// indirectly for each newly-opened database.
/// * Results are undefined if the given db is closed by an auto-extension.
/// * The list of auto-extensions should not be manipulated from an auto-extension.
pub unsafe fn init_auto_extension(
db: *mut ffi::sqlite3,
pz_err_msg: *mut *mut c_char,
ax: AutoExtension,
) -> c_int {
let r = catch_unwind(|| {
let c = Connection::from_handle(db);
c.and_then(ax)
})
.unwrap_or_else(|_| Err(Error::UnwindingPanic));
match r {
Err(e) => to_sqlite_error(&e, pz_err_msg),
_ => ffi::SQLITE_OK,
}
}
/// Register au auto-extension
///
/// # Safety
/// * Opening a database from an auto-extension handler will lead to
/// an endless recursion of the auto-handler triggering itself
/// indirectly for each newly-opened database.
/// * Results are undefined if the given db is closed by an auto-extension.
/// * The list of auto-extensions should not be manipulated from an auto-extension.
pub unsafe fn register_auto_extension(ax: RawAutoExtension) -> Result<()> {
check(ffi::sqlite3_auto_extension(Some(ax)))
}
/// Unregister the initialization routine
pub fn cancel_auto_extension(ax: RawAutoExtension) -> bool {
unsafe { ffi::sqlite3_cancel_auto_extension(Some(ax)) == 1 }
}
/// Disable all automatic extensions previously registered
pub fn reset_auto_extension() {
unsafe { ffi::sqlite3_reset_auto_extension() }
}

View File

@@ -1,442 +0,0 @@
//! Online SQLite backup API.
//!
//! Alternatively, you can create a backup with a simple
//! [`VACUUM INTO <backup_path>`](https://sqlite.org/lang_vacuum.html#vacuuminto).
//!
//! To create a [`Backup`], you must have two distinct [`Connection`]s - one
//! for the source (which can be used while the backup is running) and one for
//! the destination (which cannot). A [`Backup`] handle exposes three methods:
//! [`step`](Backup::step) will attempt to back up a specified number of pages,
//! [`progress`](Backup::progress) gets the current progress of the backup as of
//! the last call to [`step`](Backup::step), and
//! [`run_to_completion`](Backup::run_to_completion) will attempt to back up the
//! entire source database, allowing you to specify how many pages are backed up
//! at a time and how long the thread should sleep between chunks of pages.
//!
//! The following example is equivalent to "Example 2: Online Backup of a
//! Running Database" from [SQLite's Online Backup API
//! documentation](https://www.sqlite.org/backup.html).
//!
//! ```rust,no_run
//! # use rusqlite::{backup, Connection, Result};
//! # use std::path::Path;
//! # use std::time;
//!
//! fn backup_db<P: AsRef<Path>>(
//! src: &Connection,
//! dst: P,
//! progress: fn(backup::Progress),
//! ) -> Result<()> {
//! let mut dst = Connection::open(dst)?;
//! let backup = backup::Backup::new(src, &mut dst)?;
//! backup.run_to_completion(5, time::Duration::from_millis(250), Some(progress))
//! }
//! ```
use std::marker::PhantomData;
use std::path::Path;
use std::ptr;
use std::ffi::c_int;
use std::thread;
use std::time::Duration;
use crate::ffi;
use crate::error::error_from_handle;
use crate::{Connection, Name, Result, MAIN_DB};
impl Connection {
/// Back up the `name` database to the given
/// destination path.
///
/// If `progress` is not `None`, it will be called periodically
/// until the backup completes.
///
/// For more fine-grained control over the backup process (e.g.,
/// to sleep periodically during the backup or to back up to an
/// already-open database connection), see the `backup` module.
///
/// # Failure
///
/// Will return `Err` if the destination path cannot be opened
/// or if the backup fails.
pub fn backup<N: Name, P: AsRef<Path>>(
&self,
name: N,
dst_path: P,
progress: Option<fn(Progress)>,
) -> Result<()> {
use self::StepResult::{Busy, Done, Locked, More};
let mut dst = Self::open(dst_path)?;
let backup = Backup::new_with_names(self, name, &mut dst, MAIN_DB)?;
let mut r = More;
while r == More {
r = backup.step(100)?;
if let Some(f) = progress {
f(backup.progress());
}
}
match r {
Done => Ok(()),
Busy => Err(unsafe { error_from_handle(ptr::null_mut(), ffi::SQLITE_BUSY) }),
Locked => Err(unsafe { error_from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED) }),
More => unreachable!(),
}
}
/// Restore the given source path into the
/// `name` database. If `progress` is not `None`, it will be
/// called periodically until the restore completes.
///
/// For more fine-grained control over the restore process (e.g.,
/// to sleep periodically during the restore or to restore from an
/// already-open database connection), see the `backup` module.
///
/// # Failure
///
/// Will return `Err` if the destination path cannot be opened
/// or if the restore fails.
pub fn restore<N: Name, P: AsRef<Path>, F: Fn(Progress)>(
&mut self,
name: N,
src_path: P,
progress: Option<F>,
) -> Result<()> {
use self::StepResult::{Busy, Done, Locked, More};
let src = Self::open(src_path)?;
let restore = Backup::new_with_names(&src, MAIN_DB, self, name)?;
let mut r = More;
let mut busy_count = 0_i32;
'restore_loop: while r == More || r == Busy {
r = restore.step(100)?;
if let Some(ref f) = progress {
f(restore.progress());
}
if r == Busy {
busy_count += 1;
if busy_count >= 3 {
break 'restore_loop;
}
thread::sleep(Duration::from_millis(100));
}
}
match r {
Done => Ok(()),
Busy => Err(unsafe { error_from_handle(ptr::null_mut(), ffi::SQLITE_BUSY) }),
Locked => Err(unsafe { error_from_handle(ptr::null_mut(), ffi::SQLITE_LOCKED) }),
More => unreachable!(),
}
}
}
/// Possible successful results of calling
/// [`Backup::step`].
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum StepResult {
/// The backup is complete.
Done,
/// The step was successful but there are still more pages that need to be
/// backed up.
More,
/// The step failed because appropriate locks could not be acquired. This is
/// not a fatal error - the step can be retried.
Busy,
/// The step failed because the source connection was writing to the
/// database. This is not a fatal error - the step can be retried.
Locked,
}
/// Struct specifying the progress of a backup.
///
/// The percentage completion can be calculated as `(pagecount - remaining) /
/// pagecount`. The progress of a backup is as of the last call to
/// [`step`](Backup::step) - if the source database is modified after a call to
/// [`step`](Backup::step), the progress value will become outdated and
/// potentially incorrect.
#[derive(Copy, Clone, Debug)]
pub struct Progress {
/// Number of pages in the source database that still need to be backed up.
pub remaining: c_int,
/// Total number of pages in the source database.
pub pagecount: c_int,
}
/// A handle to an online backup.
pub struct Backup<'a, 'b> {
phantom_from: PhantomData<&'a Connection>,
to: &'b Connection,
b: *mut ffi::sqlite3_backup,
}
impl Backup<'_, '_> {
/// Attempt to create a new handle that will allow backups from `from` to
/// `to`. Note that `to` is a `&mut` - this is because SQLite forbids any
/// API calls on the destination of a backup while the backup is taking
/// place.
///
/// # Failure
///
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns
/// `NULL`.
#[inline]
pub fn new<'a, 'b>(from: &'a Connection, to: &'b mut Connection) -> Result<Backup<'a, 'b>> {
Backup::new_with_names(from, MAIN_DB, to, MAIN_DB)
}
/// Attempt to create a new handle that will allow backups from the
/// `from_name` database of `from` to the `to_name` database of `to`. Note
/// that `to` is a `&mut` - this is because SQLite forbids any API calls on
/// the destination of a backup while the backup is taking place.
///
/// # Failure
///
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns
/// `NULL`.
pub fn new_with_names<'a, 'b, F: Name, T: Name>(
from: &'a Connection,
from_name: F,
to: &'b mut Connection,
to_name: T,
) -> Result<Backup<'a, 'b>> {
let to_name = to_name.as_cstr()?;
let from_name = from_name.as_cstr()?;
let to_db = to.db.borrow_mut().db;
let b = unsafe {
let b = ffi::sqlite3_backup_init(
to_db,
to_name.as_ptr(),
from.db.borrow_mut().db,
from_name.as_ptr(),
);
if b.is_null() {
return Err(error_from_handle(to_db, ffi::sqlite3_errcode(to_db)));
}
b
};
Ok(Backup {
phantom_from: PhantomData,
to,
b,
})
}
/// Gets the progress of the backup as of the last call to
/// [`step`](Backup::step).
#[inline]
#[must_use]
pub fn progress(&self) -> Progress {
unsafe {
Progress {
remaining: ffi::sqlite3_backup_remaining(self.b),
pagecount: ffi::sqlite3_backup_pagecount(self.b),
}
}
}
/// Attempts to back up the given number of pages. If `num_pages` is
/// negative, will attempt to back up all remaining pages. This will hold a
/// lock on the source database for the duration, so it is probably not
/// what you want for databases that are currently active (see
/// [`run_to_completion`](Backup::run_to_completion) for a better
/// alternative).
///
/// # Failure
///
/// Will return `Err` if the underlying `sqlite3_backup_step` call returns
/// an error code other than `DONE`, `OK`, `BUSY`, or `LOCKED`. `BUSY` and
/// `LOCKED` are transient errors and are therefore returned as possible
/// `Ok` values.
#[inline]
pub fn step(&self, num_pages: c_int) -> Result<StepResult> {
use self::StepResult::{Busy, Done, Locked, More};
let rc = unsafe { ffi::sqlite3_backup_step(self.b, num_pages) };
match rc {
ffi::SQLITE_DONE => Ok(Done),
ffi::SQLITE_OK => Ok(More),
ffi::SQLITE_BUSY => Ok(Busy),
ffi::SQLITE_LOCKED => Ok(Locked),
_ => self.to.decode_result(rc).map(|_| More),
}
}
/// Attempts to run the entire backup. Will call
/// [`step(pages_per_step)`](Backup::step) as many times as necessary,
/// sleeping for `pause_between_pages` between each call to give the
/// source database time to process any pending queries. This is a
/// direct implementation of "Example 2: Online Backup of a Running
/// Database" from [SQLite's Online Backup API documentation](https://www.sqlite.org/backup.html).
///
/// If `progress` is not `None`, it will be called after each step with the
/// current progress of the backup. Note that is possible the progress may
/// not change if the step returns `Busy` or `Locked` even though the
/// backup is still running.
///
/// # Failure
///
/// Will return `Err` if any of the calls to [`step`](Backup::step) return
/// `Err`.
pub fn run_to_completion(
&self,
pages_per_step: c_int,
pause_between_pages: Duration,
progress: Option<fn(Progress)>,
) -> Result<()> {
use self::StepResult::{Busy, Done, Locked, More};
assert!(pages_per_step > 0, "pages_per_step must be positive");
loop {
let r = self.step(pages_per_step)?;
if let Some(progress) = progress {
progress(self.progress());
}
match r {
More | Busy | Locked => thread::sleep(pause_between_pages),
Done => return Ok(()),
}
}
}
}
impl Drop for Backup<'_, '_> {
#[inline]
fn drop(&mut self) {
unsafe { ffi::sqlite3_backup_finish(self.b) };
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use super::{Backup, Progress};
use crate::{Connection, Result, MAIN_DB, TEMP_DB};
use std::time::Duration;
#[cfg_attr(
all(target_family = "wasm", target_os = "unknown"),
ignore = "no filesystem on this platform"
)]
#[test]
fn backup_to_path() -> Result<()> {
let src = Connection::open_in_memory()?;
src.execute_batch("CREATE TABLE foo AS SELECT 42 AS x")?;
let temp_dir = tempfile::tempdir().unwrap();
let path = temp_dir.path().join("test.db3");
fn progress(_: Progress) {}
src.backup(MAIN_DB, path.as_path(), Some(progress))?;
let mut dst = Connection::open_in_memory()?;
dst.restore(MAIN_DB, path, Some(progress))?;
Ok(())
}
#[test]
fn test_backup() -> Result<()> {
let src = Connection::open_in_memory()?;
let sql = "BEGIN;
CREATE TABLE foo(x INTEGER);
INSERT INTO foo VALUES(42);
END;";
src.execute_batch(sql)?;
let mut dst = Connection::open_in_memory()?;
{
let backup = Backup::new(&src, &mut dst)?;
backup.step(-1)?;
}
assert_eq!(42, dst.one_column::<i64, _>("SELECT x FROM foo", [])?);
src.execute_batch("INSERT INTO foo VALUES(43)")?;
{
let backup = Backup::new(&src, &mut dst)?;
backup.run_to_completion(5, Duration::from_millis(250), None)?;
}
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo", [])?;
assert_eq!(42 + 43, the_answer);
Ok(())
}
#[test]
fn test_backup_temp() -> Result<()> {
let src = Connection::open_in_memory()?;
let sql = "BEGIN;
CREATE TEMPORARY TABLE foo(x INTEGER);
INSERT INTO foo VALUES(42);
END;";
src.execute_batch(sql)?;
let mut dst = Connection::open_in_memory()?;
{
let backup = Backup::new_with_names(&src, TEMP_DB, &mut dst, MAIN_DB)?;
backup.step(-1)?;
}
assert_eq!(42, dst.one_column::<i64, _>("SELECT x FROM foo", [])?);
src.execute_batch("INSERT INTO foo VALUES(43)")?;
{
let backup = Backup::new_with_names(&src, TEMP_DB, &mut dst, MAIN_DB)?;
backup.run_to_completion(5, Duration::from_millis(250), None)?;
}
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo", [])?;
assert_eq!(42 + 43, the_answer);
Ok(())
}
#[test]
fn test_backup_attached() -> Result<()> {
let src = Connection::open_in_memory()?;
let sql = "ATTACH DATABASE ':memory:' AS my_attached;
BEGIN;
CREATE TABLE my_attached.foo(x INTEGER);
INSERT INTO my_attached.foo VALUES(42);
END;";
src.execute_batch(sql)?;
let mut dst = Connection::open_in_memory()?;
{
let backup = Backup::new_with_names(&src, c"my_attached", &mut dst, MAIN_DB)?;
backup.step(-1)?;
}
assert_eq!(42, dst.one_column::<i64, _>("SELECT x FROM foo", [])?);
src.execute_batch("INSERT INTO foo VALUES(43)")?;
{
let backup = Backup::new_with_names(&src, c"my_attached", &mut dst, MAIN_DB)?;
backup.run_to_completion(5, Duration::from_millis(250), None)?;
}
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo", [])?;
assert_eq!(42 + 43, the_answer);
Ok(())
}
}

View File

@@ -1,71 +0,0 @@
use crate::{ffi, Error, Result, Statement};
use std::ffi::CStr;
mod sealed {
use std::ffi::CStr;
/// This trait exists just to ensure that the only impls of `trait BindIndex`
/// that are allowed are ones in this crate.
pub trait Sealed {}
impl Sealed for usize {}
impl Sealed for &str {}
impl Sealed for &CStr {}
}
/// A trait implemented by types that can index into parameters of a statement.
///
/// It is only implemented for `usize` and `&str` and `&CStr`.
pub trait BindIndex: sealed::Sealed {
/// Returns the index of the associated parameter, or `Error` if no such
/// parameter exists.
fn idx(&self, stmt: &Statement<'_>) -> Result<usize>;
}
impl BindIndex for usize {
#[inline]
fn idx(&self, _: &Statement<'_>) -> Result<usize> {
// No validation
Ok(*self)
}
}
impl BindIndex for &'_ str {
fn idx(&self, stmt: &Statement<'_>) -> Result<usize> {
match stmt.parameter_index(self)? {
Some(idx) => Ok(idx),
None => Err(Error::InvalidParameterName(self.to_string())),
}
}
}
/// C-string literal to avoid alloc
impl BindIndex for &CStr {
fn idx(&self, stmt: &Statement<'_>) -> Result<usize> {
let r = unsafe { ffi::sqlite3_bind_parameter_index(stmt.ptr(), self.as_ptr()) };
match r {
0 => Err(Error::InvalidParameterName(
self.to_string_lossy().to_string(),
)),
i => Ok(i as usize),
}
}
}
#[cfg(test)]
mod test {
use crate::{ffi, Connection, Error, Result};
#[test]
fn invalid_name() -> Result<()> {
let db = Connection::open_in_memory()?;
let mut stmt = db.prepare("SELECT 1")?;
let err = stmt.raw_bind_parameter(1, 1).unwrap_err();
assert_eq!(
err.sqlite_error_code(),
Some(ffi::ErrorCode::ParameterOutOfRange),
);
let err = stmt.raw_bind_parameter(":p1", 1).unwrap_err();
assert_eq!(err, Error::InvalidParameterName(":p1".to_owned()));
let err = stmt.raw_bind_parameter(c"x", 1).unwrap_err();
assert_eq!(err, Error::InvalidParameterName("x".to_owned()));
Ok(())
}
}

View File

@@ -1,564 +0,0 @@
//! Incremental BLOB I/O.
//!
//! Note that SQLite does not provide API-level access to change the size of a
//! BLOB; that must be performed through SQL statements.
//!
//! There are two choices for how to perform IO on a [`Blob`].
//!
//! 1. The implementations it provides of the `std::io::Read`, `std::io::Write`,
//! and `std::io::Seek` traits.
//!
//! 2. A positional IO API, e.g. [`Blob::read_at`], [`Blob::write_at`] and
//! similar.
//!
//! Documenting these in order:
//!
//! ## 1. `std::io` trait implementations.
//!
//! `Blob` conforms to `std::io::Read`, `std::io::Write`, and `std::io::Seek`,
//! so it plays nicely with other types that build on these (such as
//! `std::io::BufReader` and `std::io::BufWriter`). However, you must be careful
//! with the size of the blob. For example, when using a `BufWriter`, the
//! `BufWriter` will accept more data than the `Blob` will allow, so make sure
//! to call `flush` and check for errors. (See the unit tests in this module for
//! an example.)
//!
//! ## 2. Positional IO
//!
//! `Blob`s also offer a `pread` / `pwrite`-style positional IO api in the form
//! of [`Blob::read_at`], [`Blob::write_at`], [`Blob::raw_read_at`],
//! [`Blob::read_at_exact`], and [`Blob::raw_read_at_exact`].
//!
//! These APIs all take the position to read from or write to from as a
//! parameter, instead of using an internal `pos` value.
//!
//! ### Positional IO Read Variants
//!
//! For the `read` functions, there are several functions provided:
//!
//! - [`Blob::read_at`]
//! - [`Blob::raw_read_at`]
//! - [`Blob::read_at_exact`]
//! - [`Blob::raw_read_at_exact`]
//!
//! These can be divided along two axes: raw/not raw, and exact/inexact:
//!
//! 1. Raw/not raw refers to the type of the destination buffer. The raw
//! functions take a `&mut [MaybeUninit<u8>]` as the destination buffer,
//! where the "normal" functions take a `&mut [u8]`.
//!
//! Using `MaybeUninit` here can be more efficient in some cases, but is
//! often inconvenient, so both are provided.
//!
//! 2. Exact/inexact refers to whether or not the entire buffer must be
//! filled in order for the call to be considered a success.
//!
//! The "exact" functions require the provided buffer be entirely filled, or
//! they return an error, whereas the "inexact" functions read as much out of
//! the blob as is available, and return how much they were able to read.
//!
//! The inexact functions are preferable if you do not know the size of the
//! blob already, and the exact functions are preferable if you do.
//!
//! ### Comparison to using the `std::io` traits:
//!
//! In general, the positional methods offer the following Pro/Cons compared to
//! using the implementation `std::io::{Read, Write, Seek}` we provide for
//! `Blob`:
//!
//! 1. (Pro) There is no need to first seek to a position in order to perform IO
//! on it as the position is a parameter.
//!
//! 2. (Pro) `Blob`'s positional read functions don't mutate the blob in any
//! way, and take `&self`. No `&mut` access required.
//!
//! 3. (Pro) Positional IO functions return `Err(rusqlite::Error)` on failure,
//! rather than `Err(std::io::Error)`. Returning `rusqlite::Error` is more
//! accurate and convenient.
//!
//! Note that for the `std::io` API, no data is lost however, and it can be
//! recovered with `io_err.downcast::<rusqlite::Error>()` (this can be easy
//! to forget, though).
//!
//! 4. (Pro, for now). A `raw` version of the read API exists which can allow
//! reading into a `&mut [MaybeUninit<u8>]` buffer, which avoids a potential
//! costly initialization step. (However, `std::io` traits will certainly
//! gain this someday, which is why this is only a "Pro, for now").
//!
//! 5. (Con) The set of functions is more bare-bones than what is offered in
//! `std::io`, which has a number of adapters, handy algorithms, further
//! traits.
//!
//! 6. (Con) No meaningful interoperability with other crates, so if you need
//! that you must use `std::io`.
//!
//! To generalize: the `std::io` traits are useful because they conform to a
//! standard interface that a lot of code knows how to handle, however that
//! interface is not a perfect fit for [`Blob`], so another small set of
//! functions is provided as well.
//!
//! # Example (`std::io`)
//!
//! ```rust
//! # use rusqlite::blob::ZeroBlob;
//! # use rusqlite::{Connection, MAIN_DB};
//! # use std::error::Error;
//! # use std::io::{Read, Seek, SeekFrom, Write};
//! # fn main() -> Result<(), Box<dyn Error>> {
//! let db = Connection::open_in_memory()?;
//! db.execute_batch("CREATE TABLE test_table (content BLOB);")?;
//!
//! // Insert a BLOB into the `content` column of `test_table`. Note that the Blob
//! // I/O API provides no way of inserting or resizing BLOBs in the DB -- this
//! // must be done via SQL.
//! db.execute("INSERT INTO test_table (content) VALUES (ZEROBLOB(10))", [])?;
//!
//! // Get the row id off the BLOB we just inserted.
//! let rowid = db.last_insert_rowid();
//! // Open the BLOB we just inserted for IO.
//! let mut blob = db.blob_open(MAIN_DB, "test_table", "content", rowid, false)?;
//!
//! // Write some data into the blob. Make sure to test that the number of bytes
//! // written matches what you expect; if you try to write too much, the data
//! // will be truncated to the size of the BLOB.
//! let bytes_written = blob.write(b"01234567")?;
//! assert_eq!(bytes_written, 8);
//!
//! // Move back to the start and read into a local buffer.
//! // Same guidance - make sure you check the number of bytes read!
//! blob.seek(SeekFrom::Start(0))?;
//! let mut buf = [0u8; 20];
//! let bytes_read = blob.read(&mut buf[..])?;
//! assert_eq!(bytes_read, 10); // note we read 10 bytes because the blob has size 10
//!
//! // Insert another BLOB, this time using a parameter passed in from
//! // rust (potentially with a dynamic size).
//! db.execute(
//! "INSERT INTO test_table (content) VALUES (?1)",
//! [ZeroBlob(64)],
//! )?;
//!
//! // given a new row ID, we can reopen the blob on that row
//! let rowid = db.last_insert_rowid();
//! blob.reopen(rowid)?;
//! // Just check that the size is right.
//! assert_eq!(blob.len(), 64);
//! # Ok(())
//! # }
//! ```
//!
//! # Example (Positional)
//!
//! ```rust
//! # use rusqlite::blob::ZeroBlob;
//! # use rusqlite::{Connection, MAIN_DB};
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! let db = Connection::open_in_memory()?;
//! db.execute_batch("CREATE TABLE test_table (content BLOB);")?;
//! // Insert a blob into the `content` column of `test_table`. Note that the Blob
//! // I/O API provides no way of inserting or resizing blobs in the DB -- this
//! // must be done via SQL.
//! db.execute("INSERT INTO test_table (content) VALUES (ZEROBLOB(10))", [])?;
//! // Get the row id off the blob we just inserted.
//! let rowid = db.last_insert_rowid();
//! // Open the blob we just inserted for IO.
//! let mut blob = db.blob_open(MAIN_DB, "test_table", "content", rowid, false)?;
//! // Write some data into the blob.
//! blob.write_at(b"ABCDEF", 2)?;
//!
//! // Read the whole blob into a local buffer.
//! let mut buf = [0u8; 10];
//! blob.read_at_exact(&mut buf, 0)?;
//! assert_eq!(&buf, b"\0\0ABCDEF\0\0");
//!
//! // Insert another blob, this time using a parameter passed in from
//! // rust (potentially with a dynamic size).
//! db.execute(
//! "INSERT INTO test_table (content) VALUES (?1)",
//! [ZeroBlob(64)],
//! )?;
//!
//! // given a new row ID, we can reopen the blob on that row
//! let rowid = db.last_insert_rowid();
//! blob.reopen(rowid)?;
//! assert_eq!(blob.len(), 64);
//! # Ok(())
//! # }
//! ```
use std::cmp::min;
use std::io;
use std::ptr;
use super::ffi;
use super::types::{ToSql, ToSqlOutput};
use crate::{Connection, Name, Result};
mod pos_io;
/// Handle to an open BLOB. See
/// [`rusqlite::blob`](crate::blob) documentation for in-depth discussion.
pub struct Blob<'conn> {
conn: &'conn Connection,
blob: *mut ffi::sqlite3_blob,
// used by std::io implementations,
pos: i32,
}
impl Connection {
/// Open a handle to the BLOB located in `row_id`,
/// `column`, `table` in database `db`.
///
/// # Failure
///
/// Will return `Err` if `db`/`table`/`column` cannot be converted to a
/// C-compatible string or if the underlying SQLite BLOB open call
/// fails.
#[inline]
pub fn blob_open<D: Name, N: Name>(
&self,
db: D,
table: N,
column: N,
row_id: i64,
read_only: bool,
) -> Result<Blob<'_>> {
let c = self.db.borrow_mut();
let mut blob = ptr::null_mut();
let db = db.as_cstr()?;
let table = table.as_cstr()?;
let column = column.as_cstr()?;
let rc = unsafe {
ffi::sqlite3_blob_open(
c.db(),
db.as_ptr(),
table.as_ptr(),
column.as_ptr(),
row_id,
!read_only as std::ffi::c_int,
&mut blob,
)
};
c.decode_result(rc).map(|_| Blob {
conn: self,
blob,
pos: 0,
})
}
}
impl Blob<'_> {
/// Move a BLOB handle to a new row.
///
/// # Failure
///
/// Will return `Err` if the underlying SQLite BLOB reopen call fails.
#[inline]
pub fn reopen(&mut self, row: i64) -> Result<()> {
let rc = unsafe { ffi::sqlite3_blob_reopen(self.blob, row) };
if rc != ffi::SQLITE_OK {
return self.conn.decode_result(rc);
}
self.pos = 0;
Ok(())
}
/// Return the size in bytes of the BLOB.
#[inline]
#[must_use]
pub fn size(&self) -> i32 {
unsafe { ffi::sqlite3_blob_bytes(self.blob) }
}
/// Return the current size in bytes of the BLOB.
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.size().try_into().unwrap()
}
/// Return true if the BLOB is empty.
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.size() == 0
}
/// Close a BLOB handle.
///
/// Calling `close` explicitly is not required (the BLOB will be closed
/// when the `Blob` is dropped), but it is available, so you can get any
/// errors that occur.
///
/// # Failure
///
/// Will return `Err` if the underlying SQLite close call fails.
#[inline]
pub fn close(mut self) -> Result<()> {
self.close_()
}
#[inline]
fn close_(&mut self) -> Result<()> {
let rc = unsafe { ffi::sqlite3_blob_close(self.blob) };
self.blob = ptr::null_mut();
self.conn.decode_result(rc)
}
}
impl io::Read for Blob<'_> {
/// Read data from a BLOB incrementally. Will return Ok(0) if the end of
/// the blob has been reached.
///
/// # Failure
///
/// Will return `Err` if the underlying SQLite read call fails.
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let max_allowed_len = (self.size() - self.pos) as usize;
let n = min(buf.len(), max_allowed_len) as i32;
if n <= 0 {
return Ok(0);
}
let rc = unsafe { ffi::sqlite3_blob_read(self.blob, buf.as_mut_ptr().cast(), n, self.pos) };
self.conn
.decode_result(rc)
.map(|_| {
self.pos += n;
n as usize
})
.map_err(io::Error::other)
}
}
impl io::Write for Blob<'_> {
/// Write data into a BLOB incrementally. Will return `Ok(0)` if the end of
/// the blob has been reached; consider using `Write::write_all(buf)`
/// if you want to get an error if the entirety of the buffer cannot be
/// written.
///
/// This function may only modify the contents of the BLOB; it is not
/// possible to increase the size of a BLOB using this API.
///
/// # Failure
///
/// Will return `Err` if the underlying SQLite write call fails.
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let max_allowed_len = (self.size() - self.pos) as usize;
let n = min(buf.len(), max_allowed_len) as i32;
if n <= 0 {
return Ok(0);
}
let rc = unsafe { ffi::sqlite3_blob_write(self.blob, buf.as_ptr() as *mut _, n, self.pos) };
self.conn
.decode_result(rc)
.map(|_| {
self.pos += n;
n as usize
})
.map_err(io::Error::other)
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl io::Seek for Blob<'_> {
/// Seek to an offset, in bytes, in BLOB.
#[inline]
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let pos = match pos {
io::SeekFrom::Start(offset) => offset as i64,
io::SeekFrom::Current(offset) => i64::from(self.pos) + offset,
io::SeekFrom::End(offset) => i64::from(self.size()) + offset,
};
if pos < 0 {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid seek to negative position",
))
} else if pos > i64::from(self.size()) {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid seek to position past end of blob",
))
} else {
self.pos = pos as i32;
Ok(pos as u64)
}
}
}
#[expect(unused_must_use)]
impl Drop for Blob<'_> {
#[inline]
fn drop(&mut self) {
self.close_();
}
}
/// BLOB of length N that is filled with zeroes.
///
/// Zeroblobs are intended to serve as placeholders for BLOBs whose content is
/// later written using incremental BLOB I/O routines.
///
/// A negative value for the zeroblob results in a zero-length BLOB.
#[derive(Copy, Clone)]
pub struct ZeroBlob(pub i32);
impl ToSql for ZeroBlob {
#[inline]
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let Self(length) = *self;
Ok(ToSqlOutput::ZeroBlob(length))
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use crate::{Connection, Result, MAIN_DB};
use std::io::{BufRead, BufReader, BufWriter, Read, Seek, SeekFrom, Write};
fn db_with_test_blob() -> Result<(Connection, i64)> {
let db = Connection::open_in_memory()?;
let sql = "BEGIN;
CREATE TABLE test (content BLOB);
INSERT INTO test VALUES (ZEROBLOB(10));
END;";
db.execute_batch(sql)?;
let rowid = db.last_insert_rowid();
Ok((db, rowid))
}
#[test]
fn test_blob() -> Result<()> {
let (db, rowid) = db_with_test_blob()?;
let mut blob = db.blob_open(MAIN_DB, c"test", c"content", rowid, false)?;
assert!(!blob.is_empty());
assert_eq!(10, blob.len());
assert_eq!(4, blob.write(b"Clob").unwrap());
assert_eq!(6, blob.write(b"567890xxxxxx").unwrap()); // cannot write past 10
assert_eq!(0, blob.write(b"5678").unwrap()); // still cannot write past 10
blob.flush().unwrap();
blob.reopen(rowid)?;
blob.close()?;
blob = db.blob_open(MAIN_DB, c"test", c"content", rowid, true)?;
let mut bytes = [0u8; 5];
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
assert_eq!(&bytes, b"Clob5");
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
assert_eq!(&bytes, b"67890");
assert_eq!(0, blob.read(&mut bytes[..]).unwrap());
blob.seek(SeekFrom::Start(2)).unwrap();
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
assert_eq!(&bytes, b"ob567");
// only first 4 bytes of `bytes` should be read into
blob.seek(SeekFrom::Current(-1)).unwrap();
assert_eq!(4, blob.read(&mut bytes[..]).unwrap());
assert_eq!(&bytes, b"78907");
blob.seek(SeekFrom::End(-6)).unwrap();
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
assert_eq!(&bytes, b"56789");
blob.reopen(rowid)?;
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
assert_eq!(&bytes, b"Clob5");
// should not be able to seek negative or past end
blob.seek(SeekFrom::Current(-20)).unwrap_err();
blob.seek(SeekFrom::End(0)).unwrap();
blob.seek(SeekFrom::Current(1)).unwrap_err();
// write_all should detect when we return Ok(0) because there is no space left,
// and return a write error
blob.reopen(rowid)?;
blob.write_all(b"0123456789x").unwrap_err();
Ok(())
}
#[test]
fn test_blob_in_bufreader() -> Result<()> {
let (db, rowid) = db_with_test_blob()?;
let mut blob = db.blob_open(MAIN_DB, c"test", c"content", rowid, false)?;
assert_eq!(8, blob.write(b"one\ntwo\n").unwrap());
blob.reopen(rowid)?;
let mut reader = BufReader::new(blob);
let mut line = String::new();
assert_eq!(4, reader.read_line(&mut line).unwrap());
assert_eq!("one\n", line);
line.truncate(0);
assert_eq!(4, reader.read_line(&mut line).unwrap());
assert_eq!("two\n", line);
line.truncate(0);
assert_eq!(2, reader.read_line(&mut line).unwrap());
assert_eq!("\0\0", line);
Ok(())
}
#[test]
fn test_blob_in_bufwriter() -> Result<()> {
let (db, rowid) = db_with_test_blob()?;
{
let blob = db.blob_open(MAIN_DB, c"test", c"content", rowid, false)?;
let mut writer = BufWriter::new(blob);
// trying to write too much and then flush should fail
assert_eq!(8, writer.write(b"01234567").unwrap());
assert_eq!(8, writer.write(b"01234567").unwrap());
writer.flush().unwrap_err();
}
{
// ... but it should've written the first 10 bytes
let mut blob = db.blob_open(MAIN_DB, c"test", c"content", rowid, false)?;
let mut bytes = [0u8; 10];
assert_eq!(10, blob.read(&mut bytes[..]).unwrap());
assert_eq!(b"0123456701", &bytes);
}
{
let blob = db.blob_open(MAIN_DB, c"test", c"content", rowid, false)?;
let mut writer = BufWriter::new(blob);
// trying to write_all too much should fail
writer.write_all(b"aaaaaaaaaabbbbb").unwrap();
writer.flush().unwrap_err();
}
{
// ... but it should've written the first 10 bytes
let mut blob = db.blob_open(MAIN_DB, c"test", c"content", rowid, false)?;
let mut bytes = [0u8; 10];
assert_eq!(10, blob.read(&mut bytes[..]).unwrap());
assert_eq!(b"aaaaaaaaaa", &bytes);
Ok(())
}
}
#[test]
fn zero_blob() -> Result<()> {
use crate::types::ToSql;
let zb = super::ZeroBlob(1);
assert!(zb.to_sql().is_ok());
Ok(())
}
}

View File

@@ -1,274 +0,0 @@
use super::Blob;
use std::mem::MaybeUninit;
use std::slice::from_raw_parts_mut;
use crate::ffi;
use crate::{Error, Result};
impl Blob<'_> {
/// Write `buf` to `self` starting at `write_start`, returning an error if
/// `write_start + buf.len()` is past the end of the blob.
///
/// If an error is returned, no data is written.
///
/// Note: the blob cannot be resized using this function -- that must be
/// done using SQL (for example, an `UPDATE` statement).
///
/// Note: This is part of the positional I/O API, and thus takes an absolute
/// position write to, instead of using the internal position that can be
/// manipulated by the `std::io` traits.
///
/// Unlike the similarly named [`FileExt::write_at`][fext_write_at] function
/// (from `std::os::unix`), it's always an error to perform a "short write".
///
/// [fext_write_at]: https://doc.rust-lang.org/std/os/unix/fs/trait.FileExt.html#tymethod.write_at
#[inline]
pub fn write_at(&mut self, buf: &[u8], write_start: usize) -> Result<()> {
let len = self.len();
if buf.len().saturating_add(write_start) > len {
return Err(Error::BlobSizeError);
}
// We know `len` fits in an `i32`, so either:
//
// 1. `buf.len() + write_start` overflows, in which case we'd hit the
// return above (courtesy of `saturating_add`).
//
// 2. `buf.len() + write_start` doesn't overflow but is larger than len,
// in which case ditto.
//
// 3. `buf.len() + write_start` doesn't overflow but is less than len.
// This means that both `buf.len()` and `write_start` can also be
// losslessly converted to i32, since `len` came from an i32.
// Sanity check the above.
debug_assert!(i32::try_from(write_start).is_ok() && i32::try_from(buf.len()).is_ok());
self.conn.decode_result(unsafe {
ffi::sqlite3_blob_write(
self.blob,
buf.as_ptr().cast(),
buf.len() as i32,
write_start as i32,
)
})
}
/// An alias for `write_at` provided for compatibility with the conceptually
/// equivalent [`std::os::unix::FileExt::write_all_at`][write_all_at]
/// function from libstd:
///
/// [write_all_at]: https://doc.rust-lang.org/std/os/unix/fs/trait.FileExt.html#method.write_all_at
#[inline]
pub fn write_all_at(&mut self, buf: &[u8], write_start: usize) -> Result<()> {
self.write_at(buf, write_start)
}
/// Read as much as possible from `offset` to `offset + buf.len()` out of
/// `self`, writing into `buf`. On success, returns the number of bytes
/// written.
///
/// If there's insufficient data in `self`, then the returned value will be
/// less than `buf.len()`.
///
/// See also [`Blob::raw_read_at`], which can take an uninitialized buffer,
/// or [`Blob::read_at_exact`] which returns an error if the entire `buf` is
/// not read.
///
/// Note: This is part of the positional I/O API, and thus takes an absolute
/// position to read from, instead of using the internal position that can
/// be manipulated by the `std::io` traits. Consequently, it does not change
/// that value either.
#[inline]
pub fn read_at(&self, buf: &mut [u8], read_start: usize) -> Result<usize> {
// Safety: this is safe because `raw_read_at` never stores uninitialized
// data into `as_uninit`.
let as_uninit: &mut [MaybeUninit<u8>] =
unsafe { from_raw_parts_mut(buf.as_mut_ptr().cast(), buf.len()) };
self.raw_read_at(as_uninit, read_start).map(|s| s.len())
}
/// Read as much as possible from `offset` to `offset + buf.len()` out of
/// `self`, writing into `buf`. On success, returns the portion of `buf`
/// which was initialized by this call.
///
/// If there's insufficient data in `self`, then the returned value will be
/// shorter than `buf`.
///
/// See also [`Blob::read_at`], which takes a `&mut [u8]` buffer instead of
/// a slice of `MaybeUninit<u8>`.
///
/// Note: This is part of the positional I/O API, and thus takes an absolute
/// position to read from, instead of using the internal position that can
/// be manipulated by the `std::io` traits. Consequently, it does not change
/// that value either.
#[inline]
pub fn raw_read_at<'a>(
&self,
buf: &'a mut [MaybeUninit<u8>],
read_start: usize,
) -> Result<&'a mut [u8]> {
let len = self.len();
let read_len = match len.checked_sub(read_start) {
None | Some(0) => 0,
Some(v) => v.min(buf.len()),
};
if read_len == 0 {
// We could return `Ok(&mut [])`, but it seems confusing that the
// pointers don't match, so fabricate an empty slice of u8 with the
// same base pointer as `buf`.
let empty = unsafe { from_raw_parts_mut(buf.as_mut_ptr().cast::<u8>(), 0) };
return Ok(empty);
}
// At this point we believe `read_start as i32` is lossless because:
//
// 1. `len as i32` is known to be lossless, since it comes from a SQLite
// api returning an i32.
//
// 2. If we got here, `len.checked_sub(read_start)` was Some (or else
// we'd have hit the `if read_len == 0` early return), so `len` must
// be larger than `read_start`, and so it must fit in i32 as well.
debug_assert!(i32::try_from(read_start).is_ok());
// We also believe that `read_start + read_len <= len` because:
//
// 1. This is equivalent to `read_len <= len - read_start` via algebra.
// 2. We know that `read_len` is `min(len - read_start, buf.len())`
// 3. Expanding, this is `min(len - read_start, buf.len()) <= len - read_start`,
// or `min(A, B) <= A` which is clearly true.
//
// Note that this stuff is in debug_assert so no need to use checked_add
// and such -- we'll always panic on overflow in debug builds.
debug_assert!(read_start + read_len <= len);
// These follow naturally.
debug_assert!(buf.len() >= read_len);
debug_assert!(i32::try_from(buf.len()).is_ok());
debug_assert!(i32::try_from(read_len).is_ok());
unsafe {
self.conn.decode_result(ffi::sqlite3_blob_read(
self.blob,
buf.as_mut_ptr().cast(),
read_len as i32,
read_start as i32,
))?;
Ok(from_raw_parts_mut(buf.as_mut_ptr().cast::<u8>(), read_len))
}
}
/// Equivalent to [`Blob::read_at`], but returns a `BlobSizeError` if `buf`
/// is not fully initialized.
#[inline]
pub fn read_at_exact(&self, buf: &mut [u8], read_start: usize) -> Result<()> {
let n = self.read_at(buf, read_start)?;
if n != buf.len() {
Err(Error::BlobSizeError)
} else {
Ok(())
}
}
/// Equivalent to [`Blob::raw_read_at`], but returns a `BlobSizeError` if
/// `buf` is not fully initialized.
#[inline]
pub fn raw_read_at_exact<'a>(
&self,
buf: &'a mut [MaybeUninit<u8>],
read_start: usize,
) -> Result<&'a mut [u8]> {
let buflen = buf.len();
let initted = self.raw_read_at(buf, read_start)?;
if initted.len() != buflen {
Err(Error::BlobSizeError)
} else {
Ok(initted)
}
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use crate::{Connection, Result, MAIN_DB};
// to ensure we don't modify seek pos
use std::io::Seek as _;
#[test]
fn test_pos_io() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE test_table(content BLOB);")?;
db.execute("INSERT INTO test_table(content) VALUES (ZEROBLOB(10))", [])?;
let rowid = db.last_insert_rowid();
let mut blob = db.blob_open(MAIN_DB, c"test_table", c"content", rowid, false)?;
// modify the seek pos to ensure we aren't using it or modifying it.
blob.seek(std::io::SeekFrom::Start(1)).unwrap();
let one2ten: [u8; 10] = [1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10];
blob.write_at(&one2ten, 0)?;
let mut s = [0u8; 10];
blob.read_at_exact(&mut s, 0)?;
assert_eq!(&s, &one2ten, "write should go through");
blob.read_at_exact(&mut s, 1).unwrap_err();
blob.read_at_exact(&mut s, 0)?;
assert_eq!(&s, &one2ten, "should be unchanged");
let mut fives = [0u8; 5];
blob.read_at_exact(&mut fives, 0)?;
assert_eq!(&fives, &[1u8, 2, 3, 4, 5]);
blob.read_at_exact(&mut fives, 5)?;
assert_eq!(&fives, &[6u8, 7, 8, 9, 10]);
blob.read_at_exact(&mut fives, 7).unwrap_err();
blob.read_at_exact(&mut fives, 12).unwrap_err();
blob.read_at_exact(&mut fives, 10).unwrap_err();
blob.read_at_exact(&mut fives, i32::MAX as usize)
.unwrap_err();
blob.read_at_exact(&mut fives, i32::MAX as usize + 1)
.unwrap_err();
// zero length writes are fine if in bounds
blob.read_at_exact(&mut [], 10)?;
blob.read_at_exact(&mut [], 0)?;
blob.read_at_exact(&mut [], 5)?;
blob.write_all_at(&[16, 17, 18, 19, 20], 5)?;
blob.read_at_exact(&mut s, 0)?;
assert_eq!(&s, &[1u8, 2, 3, 4, 5, 16, 17, 18, 19, 20]);
blob.write_at(&[100, 99, 98, 97, 96], 6).unwrap_err();
blob.write_at(&[100, 99, 98, 97, 96], i32::MAX as usize)
.unwrap_err();
blob.write_at(&[100, 99, 98, 97, 96], i32::MAX as usize + 1)
.unwrap_err();
blob.read_at_exact(&mut s, 0)?;
assert_eq!(&s, &[1u8, 2, 3, 4, 5, 16, 17, 18, 19, 20]);
let mut s2: [std::mem::MaybeUninit<u8>; 10] = [std::mem::MaybeUninit::uninit(); 10];
{
let read = blob.raw_read_at_exact(&mut s2, 0)?;
assert_eq!(read, &s);
assert!(std::ptr::eq(read.as_ptr(), s2.as_ptr().cast()));
}
let mut empty = [];
assert!(std::ptr::eq(
blob.raw_read_at_exact(&mut empty, 0)?.as_ptr(),
empty.as_ptr().cast(),
));
blob.raw_read_at_exact(&mut s2, 5).unwrap_err();
let end_pos = blob.stream_position().unwrap();
assert_eq!(end_pos, 1);
Ok(())
}
}

View File

@@ -1,138 +0,0 @@
//! Busy handler (when the database is locked)
use std::ffi::{c_int, c_void};
use std::mem;
use std::panic::catch_unwind;
use std::ptr;
use std::time::Duration;
use crate::ffi;
use crate::{Connection, InnerConnection, Result};
impl Connection {
/// Set a busy handler that sleeps for a specified amount of time when a
/// table is locked. The handler will sleep multiple times until at
/// least "ms" milliseconds of sleeping have accumulated.
///
/// Calling this routine with an argument equal to zero turns off all busy
/// handlers.
///
/// There can only be a single busy handler for a particular database
/// connection at any given moment. If another busy handler was defined
/// (using [`busy_handler`](Connection::busy_handler)) prior to calling this
/// routine, that other busy handler is cleared.
///
/// Newly created connections currently have a default busy timeout of
/// 5000ms, but this may be subject to change.
pub fn busy_timeout(&self, timeout: Duration) -> Result<()> {
let ms: i32 = timeout
.as_secs()
.checked_mul(1000)
.and_then(|t| t.checked_add(timeout.subsec_millis().into()))
.and_then(|t| t.try_into().ok())
.expect("too big");
self.db.borrow_mut().busy_timeout(ms)
}
/// Register a callback to handle `SQLITE_BUSY` errors.
///
/// If the busy callback is `None`, then `SQLITE_BUSY` is returned
/// immediately upon encountering the lock. The argument to the busy
/// handler callback is the number of times that the
/// busy handler has been invoked previously for the
/// same locking event. If the busy callback returns `false`, then no
/// additional attempts are made to access the
/// database and `SQLITE_BUSY` is returned to the
/// application. If the callback returns `true`, then another attempt
/// is made to access the database and the cycle repeats.
///
/// There can only be a single busy handler defined for each database
/// connection. Setting a new busy handler clears any previously set
/// handler. Note that calling [`busy_timeout()`](Connection::busy_timeout)
/// or evaluating `PRAGMA busy_timeout=N` will change the busy handler
/// and thus clear any previously set busy handler.
///
/// Newly created connections default to a
/// [`busy_timeout()`](Connection::busy_timeout) handler with a timeout
/// of 5000ms, although this is subject to change.
pub fn busy_handler(&self, callback: Option<fn(i32) -> bool>) -> Result<()> {
unsafe extern "C" fn busy_handler_callback(p_arg: *mut c_void, count: c_int) -> c_int {
let handler_fn: fn(i32) -> bool = mem::transmute(p_arg);
c_int::from(catch_unwind(|| handler_fn(count)).unwrap_or_default())
}
let c = self.db.borrow_mut();
c.decode_result(unsafe {
ffi::sqlite3_busy_handler(
c.db(),
callback.as_ref().map(|_| busy_handler_callback as _),
callback.map_or_else(ptr::null_mut, |f| f as *mut c_void),
)
})
}
}
impl InnerConnection {
#[inline]
fn busy_timeout(&mut self, timeout: c_int) -> Result<()> {
let r = unsafe { ffi::sqlite3_busy_timeout(self.db, timeout) };
self.decode_result(r)
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use crate::{Connection, ErrorCode, Result, TransactionBehavior};
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg_attr(
all(target_family = "wasm", target_os = "unknown"),
ignore = "no filesystem on this platform"
)]
#[test]
fn test_default_busy() -> Result<()> {
let temp_dir = tempfile::tempdir().unwrap();
let path = temp_dir.path().join("test.db3");
let mut db1 = Connection::open(&path)?;
let tx1 = db1.transaction_with_behavior(TransactionBehavior::Exclusive)?;
let db2 = Connection::open(&path)?;
let r: Result<()> = db2.query_row("PRAGMA schema_version", [], |_| unreachable!());
assert_eq!(
r.unwrap_err().sqlite_error_code(),
Some(ErrorCode::DatabaseBusy)
);
tx1.rollback()
}
#[cfg_attr(
all(target_family = "wasm", target_os = "unknown"),
ignore = "no filesystem on this platform"
)]
#[test]
fn test_busy_handler() -> Result<()> {
static CALLED: AtomicBool = AtomicBool::new(false);
fn busy_handler(n: i32) -> bool {
if n > 2 {
false
} else {
CALLED.swap(true, Ordering::Relaxed)
}
}
let temp_dir = tempfile::tempdir().unwrap();
let path = temp_dir.path().join("busy-handler.db3");
let db1 = Connection::open(&path)?;
db1.execute_batch("CREATE TABLE IF NOT EXISTS t(a)")?;
let db2 = Connection::open(&path)?;
db2.busy_handler(Some(busy_handler))?;
db1.execute_batch("BEGIN EXCLUSIVE")?;
let err = db2.prepare("SELECT * FROM t").unwrap_err();
assert_eq!(err.sqlite_error_code(), Some(ErrorCode::DatabaseBusy));
assert!(CALLED.load(Ordering::Relaxed));
db1.busy_handler(None)?;
Ok(())
}
}

View File

@@ -1,351 +0,0 @@
//! Prepared statements cache for faster execution.
use crate::raw_statement::RawStatement;
use crate::{Connection, PrepFlags, Result, Statement};
use hashlink::LruCache;
use std::cell::RefCell;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
impl Connection {
/// Prepare a SQL statement for execution, returning a previously prepared
/// (but not currently in-use) statement if one is available. The
/// returned statement will be cached for reuse by future calls to
/// [`prepare_cached`](Connection::prepare_cached) once it is dropped.
///
/// ```rust,no_run
/// # use rusqlite::{Connection, Result};
/// fn insert_new_people(conn: &Connection) -> Result<()> {
/// {
/// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?1)")?;
/// stmt.execute(["Joe Smith"])?;
/// }
/// {
/// // This will return the same underlying SQLite statement handle without
/// // having to prepare it again.
/// let mut stmt = conn.prepare_cached("INSERT INTO People (name) VALUES (?1)")?;
/// stmt.execute(["Bob Jones"])?;
/// }
/// Ok(())
/// }
/// ```
///
/// # Failure
///
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
/// or if the underlying SQLite call fails.
#[inline]
pub fn prepare_cached(&self, sql: &str) -> Result<CachedStatement<'_>> {
self.cache.get(self, sql)
}
/// Set the maximum number of cached prepared statements this connection
/// will hold. By default, a connection will hold a relatively small
/// number of cached statements. If you need more, or know that you
/// will not use cached statements, you
/// can set the capacity manually using this method.
#[inline]
pub fn set_prepared_statement_cache_capacity(&self, capacity: usize) {
self.cache.set_capacity(capacity);
}
/// Remove/finalize all prepared statements currently in the cache.
#[inline]
pub fn flush_prepared_statement_cache(&self) {
self.cache.flush();
}
}
/// Prepared statements LRU cache.
#[derive(Debug)]
pub struct StatementCache(RefCell<LruCache<Arc<str>, RawStatement>>);
unsafe impl Send for StatementCache {}
/// Cacheable statement.
///
/// Statement will return automatically to the cache by default.
/// If you want the statement to be discarded, call
/// [`discard()`](CachedStatement::discard) on it.
pub struct CachedStatement<'conn> {
stmt: Option<Statement<'conn>>,
cache: &'conn StatementCache,
}
impl<'conn> Deref for CachedStatement<'conn> {
type Target = Statement<'conn>;
#[inline]
fn deref(&self) -> &Statement<'conn> {
self.stmt.as_ref().unwrap()
}
}
impl<'conn> DerefMut for CachedStatement<'conn> {
#[inline]
fn deref_mut(&mut self) -> &mut Statement<'conn> {
self.stmt.as_mut().unwrap()
}
}
impl Drop for CachedStatement<'_> {
#[inline]
fn drop(&mut self) {
if let Some(stmt) = self.stmt.take() {
self.cache.cache_stmt(unsafe { stmt.into_raw() });
}
}
}
impl CachedStatement<'_> {
#[inline]
fn new<'conn>(stmt: Statement<'conn>, cache: &'conn StatementCache) -> CachedStatement<'conn> {
CachedStatement {
stmt: Some(stmt),
cache,
}
}
/// Discard the statement, preventing it from being returned to its
/// [`Connection`]'s collection of cached statements.
#[inline]
pub fn discard(mut self) {
self.stmt = None;
}
}
impl StatementCache {
/// Create a statement cache.
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self(RefCell::new(LruCache::new(capacity)))
}
#[inline]
fn set_capacity(&self, capacity: usize) {
self.0.borrow_mut().set_capacity(capacity);
}
// Search the cache for a prepared-statement object that implements `sql`.
// If no such prepared-statement can be found, allocate and prepare a new one.
//
// # Failure
//
// Will return `Err` if no cached statement can be found and the underlying
// SQLite prepare call fails.
fn get<'conn>(
&'conn self,
conn: &'conn Connection,
sql: &str,
) -> Result<CachedStatement<'conn>> {
let trimmed = sql.trim();
let mut cache = self.0.borrow_mut();
let stmt = match cache.remove(trimmed) {
Some(raw_stmt) => Ok(Statement::new(conn, raw_stmt)),
None => conn.prepare_with_flags(trimmed, PrepFlags::SQLITE_PREPARE_PERSISTENT),
};
stmt.map(|mut stmt| {
stmt.stmt.set_statement_cache_key(trimmed);
CachedStatement::new(stmt, self)
})
}
// Return a statement to the cache.
fn cache_stmt(&self, mut stmt: RawStatement) {
if stmt.is_null() {
return;
}
let mut cache = self.0.borrow_mut();
stmt.clear_bindings();
if let Some(sql) = stmt.statement_cache_key() {
cache.insert(sql, stmt);
} else {
debug_assert!(
false,
"bug in statement cache code, statement returned to cache that without key"
);
}
}
#[inline]
fn flush(&self) {
let mut cache = self.0.borrow_mut();
cache.clear();
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use super::StatementCache;
use crate::{Connection, Result};
use fallible_iterator::FallibleIterator;
impl StatementCache {
fn clear(&self) {
self.0.borrow_mut().clear();
}
fn len(&self) -> usize {
self.0.borrow().len()
}
fn capacity(&self) -> usize {
self.0.borrow().capacity()
}
}
#[test]
fn test_cache() -> Result<()> {
let db = Connection::open_in_memory()?;
let cache = &db.cache;
let initial_capacity = cache.capacity();
assert_eq!(0, cache.len());
assert!(initial_capacity > 0);
let sql = "PRAGMA schema_version";
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
}
assert_eq!(1, cache.len());
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
}
assert_eq!(1, cache.len());
cache.clear();
assert_eq!(0, cache.len());
assert_eq!(initial_capacity, cache.capacity());
Ok(())
}
#[test]
fn test_set_capacity() -> Result<()> {
let db = Connection::open_in_memory()?;
let cache = &db.cache;
let sql = "PRAGMA schema_version";
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
}
assert_eq!(1, cache.len());
db.set_prepared_statement_cache_capacity(0);
assert_eq!(0, cache.len());
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
}
assert_eq!(0, cache.len());
db.set_prepared_statement_cache_capacity(8);
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
}
assert_eq!(1, cache.len());
Ok(())
}
#[test]
fn test_discard() -> Result<()> {
let db = Connection::open_in_memory()?;
let cache = &db.cache;
let sql = "PRAGMA schema_version";
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
stmt.discard();
}
assert_eq!(0, cache.len());
Ok(())
}
#[test]
fn test_ddl() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute_batch(
r"
CREATE TABLE foo (x INT);
INSERT INTO foo VALUES (1);
",
)?;
let sql = "SELECT * FROM foo";
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(Ok(Some(1i32)), stmt.query([])?.map(|r| r.get(0)).next());
}
db.execute_batch(
r"
ALTER TABLE foo ADD COLUMN y INT;
UPDATE foo SET y = 2;
",
)?;
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(
Ok(Some((1i32, 2i32))),
stmt.query([])?.map(|r| Ok((r.get(0)?, r.get(1)?))).next()
);
}
Ok(())
}
#[test]
fn test_connection_close() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.prepare_cached("SELECT * FROM sqlite_master;")?;
conn.close().expect("connection not closed");
Ok(())
}
#[test]
fn test_cache_key() -> Result<()> {
let db = Connection::open_in_memory()?;
let cache = &db.cache;
assert_eq!(0, cache.len());
//let sql = " PRAGMA schema_version; -- comment";
let sql = "PRAGMA schema_version; ";
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
}
assert_eq!(1, cache.len());
{
let mut stmt = db.prepare_cached(sql)?;
assert_eq!(0, cache.len());
assert_eq!(0, stmt.query_row([], |r| r.get::<_, i64>(0))?);
}
assert_eq!(1, cache.len());
Ok(())
}
#[test]
fn test_empty_stmt() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.prepare_cached("")?;
Ok(())
}
}

View File

@@ -1,236 +0,0 @@
//! Add, remove, or modify a collation
use std::cmp::Ordering;
use std::ffi::{c_char, c_int, c_void, CStr};
use std::panic::catch_unwind;
use std::ptr;
use std::slice;
use crate::ffi;
use crate::util::free_boxed_value;
use crate::{Connection, InnerConnection, Name, Result};
impl Connection {
/// Add or modify a collation.
#[inline]
pub fn create_collation<C, N: Name>(&self, collation_name: N, x_compare: C) -> Result<()>
where
C: Fn(&str, &str) -> Ordering + Send + 'static,
{
self.db
.borrow_mut()
.create_collation(collation_name, x_compare)
}
/// Collation needed callback
#[inline]
pub fn collation_needed(&self, x_coll_needed: fn(&Self, &str) -> Result<()>) -> Result<()> {
self.db.borrow_mut().collation_needed(x_coll_needed)
}
/// Remove collation.
#[inline]
pub fn remove_collation<N: Name>(&self, collation_name: N) -> Result<()> {
self.db.borrow_mut().remove_collation(collation_name)
}
}
impl InnerConnection {
/// ```compile_fail
/// use rusqlite::{Connection, Result};
/// fn main() -> Result<()> {
/// let db = Connection::open_in_memory()?;
/// {
/// let mut called = std::sync::atomic::AtomicBool::new(false);
/// db.create_collation("foo", |_, _| {
/// called.store(true, std::sync::atomic::Ordering::Relaxed);
/// std::cmp::Ordering::Equal
/// })?;
/// }
/// let value: String = db.query_row(
/// "WITH cte(bar) AS
/// (VALUES ('v1'),('v2'),('v3'),('v4'),('v5'))
/// SELECT DISTINCT bar COLLATE foo FROM cte;",
/// [],
/// |row| row.get(0),
/// )?;
/// assert_eq!(value, "v1");
/// Ok(())
/// }
/// ```
fn create_collation<C, N: Name>(&mut self, collation_name: N, x_compare: C) -> Result<()>
where
C: Fn(&str, &str) -> Ordering + Send + 'static,
{
unsafe extern "C" fn call_boxed_closure<C>(
arg1: *mut c_void,
arg2: c_int,
arg3: *const c_void,
arg4: c_int,
arg5: *const c_void,
) -> c_int
where
C: Fn(&str, &str) -> Ordering,
{
let r = catch_unwind(|| {
let boxed_f: *mut C = arg1.cast::<C>();
assert!(!boxed_f.is_null(), "Internal error - null function pointer");
let s1 = {
let c_slice = slice::from_raw_parts(arg3.cast::<u8>(), arg2 as usize);
String::from_utf8_lossy(c_slice)
};
let s2 = {
let c_slice = slice::from_raw_parts(arg5.cast::<u8>(), arg4 as usize);
String::from_utf8_lossy(c_slice)
};
(*boxed_f)(s1.as_ref(), s2.as_ref())
});
let t = match r {
Err(_) => {
return -1; // FIXME How ?
}
Ok(r) => r,
};
match t {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
let boxed_f: *mut C = Box::into_raw(Box::new(x_compare));
let c_name = collation_name.as_cstr()?;
let flags = ffi::SQLITE_UTF8;
let r = unsafe {
ffi::sqlite3_create_collation_v2(
self.db(),
c_name.as_ptr(),
flags,
boxed_f.cast::<c_void>(),
Some(call_boxed_closure::<C>),
Some(free_boxed_value::<C>),
)
};
let res = self.decode_result(r);
// The xDestroy callback is not called if the sqlite3_create_collation_v2()
// function fails.
if res.is_err() {
drop(unsafe { Box::from_raw(boxed_f) });
}
res
}
fn collation_needed(
&mut self,
x_coll_needed: fn(&Connection, &str) -> Result<()>,
) -> Result<()> {
use std::mem;
#[expect(clippy::needless_return)]
unsafe extern "C" fn collation_needed_callback(
arg1: *mut c_void,
arg2: *mut ffi::sqlite3,
e_text_rep: c_int,
arg3: *const c_char,
) {
use std::str;
if e_text_rep != ffi::SQLITE_UTF8 {
// TODO: validate
return;
}
let callback: fn(&Connection, &str) -> Result<()> = mem::transmute(arg1);
let res = catch_unwind(|| {
let conn = Connection::from_handle(arg2).unwrap();
let collation_name = CStr::from_ptr(arg3)
.to_str()
.expect("illegal collation sequence name");
callback(&conn, collation_name)
});
if res.is_err() {
return; // FIXME How ?
}
}
let r = unsafe {
ffi::sqlite3_collation_needed(
self.db(),
x_coll_needed as *mut c_void,
Some(collation_needed_callback),
)
};
self.decode_result(r)
}
#[inline]
fn remove_collation<N: Name>(&mut self, collation_name: N) -> Result<()> {
let c_name = collation_name.as_cstr()?;
let r = unsafe {
ffi::sqlite3_create_collation_v2(
self.db(),
c_name.as_ptr(),
ffi::SQLITE_UTF8,
ptr::null_mut(),
None,
None,
)
};
self.decode_result(r)
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use crate::{Connection, Result};
use fallible_streaming_iterator::FallibleStreamingIterator;
use std::cmp::Ordering;
use unicase::UniCase;
fn unicase_compare(s1: &str, s2: &str) -> Ordering {
UniCase::new(s1).cmp(&UniCase::new(s2))
}
#[test]
fn test_unicase() -> Result<()> {
let db = Connection::open_in_memory()?;
db.create_collation(c"unicase", unicase_compare)?;
collate(db)
}
fn collate(db: Connection) -> Result<()> {
db.execute_batch(
"CREATE TABLE foo (bar);
INSERT INTO foo (bar) VALUES ('Maße');
INSERT INTO foo (bar) VALUES ('MASSE');",
)?;
let mut stmt = db.prepare("SELECT DISTINCT bar COLLATE unicase FROM foo ORDER BY 1")?;
let rows = stmt.query([])?;
assert_eq!(rows.count()?, 1);
Ok(())
}
fn collation_needed(db: &Connection, collation_name: &str) -> Result<()> {
if "unicase" == collation_name {
db.create_collation(collation_name, unicase_compare)
} else {
Ok(())
}
}
#[test]
fn test_collation_needed() -> Result<()> {
let db = Connection::open_in_memory()?;
db.collation_needed(collation_needed)?;
collate(db)
}
#[test]
fn remove_collation() -> Result<()> {
let db = Connection::open_in_memory()?;
db.create_collation(c"unicase", unicase_compare)?;
db.remove_collation(c"unicase")
}
}

View File

@@ -1,576 +0,0 @@
use std::ffi::{c_char, CStr};
use std::ptr;
use std::str;
use crate::ffi;
use crate::{Connection, Error, Name, Result, Statement};
/// Information about a column of a SQLite query.
#[cfg(feature = "column_decltype")]
#[derive(Debug)]
pub struct Column<'stmt> {
name: &'stmt str,
decl_type: Option<&'stmt str>,
}
#[cfg(feature = "column_decltype")]
impl Column<'_> {
/// Returns the name of the column.
#[inline]
#[must_use]
pub fn name(&self) -> &str {
self.name
}
/// Returns the type of the column (`None` for expression).
#[inline]
#[must_use]
pub fn decl_type(&self) -> Option<&str> {
self.decl_type
}
}
/// Metadata about the origin of a column of a SQLite query
#[cfg(feature = "column_metadata")]
#[derive(Debug)]
pub struct ColumnMetadata<'stmt> {
name: &'stmt str,
database_name: Option<&'stmt str>,
table_name: Option<&'stmt str>,
origin_name: Option<&'stmt str>,
}
#[cfg(feature = "column_metadata")]
impl ColumnMetadata<'_> {
#[inline]
#[must_use]
/// Returns the name of the column in the query results
pub fn name(&self) -> &str {
self.name
}
#[inline]
#[must_use]
/// Returns the database name from which the column originates
pub fn database_name(&self) -> Option<&str> {
self.database_name
}
#[inline]
#[must_use]
/// Returns the table name from which the column originates
pub fn table_name(&self) -> Option<&str> {
self.table_name
}
#[inline]
#[must_use]
/// Returns the column name from which the column originates
pub fn origin_name(&self) -> Option<&str> {
self.origin_name
}
}
impl Statement<'_> {
/// Get all the column names in the result set of the prepared statement.
///
/// If associated DB schema can be altered concurrently, you should make
/// sure that current statement has already been stepped once before
/// calling this method.
pub fn column_names(&self) -> Vec<&str> {
let n = self.column_count();
let mut cols = Vec::with_capacity(n);
for i in 0..n {
let s = self.column_name_unwrap(i);
cols.push(s);
}
cols
}
/// Return the number of columns in the result set returned by the prepared
/// statement.
///
/// If associated DB schema can be altered concurrently, you should make
/// sure that current statement has already been stepped once before
/// calling this method.
#[inline]
pub fn column_count(&self) -> usize {
self.stmt.column_count()
}
/// Check that column name reference lifetime is limited:
/// <https://www.sqlite.org/c3ref/column_name.html>
/// > The returned string pointer is valid...
///
/// `column_name` reference can become invalid if `stmt` is reprepared
/// (because of schema change) when `query_row` is called. So we assert
/// that a compilation error happens if this reference is kept alive:
/// ```compile_fail
/// use rusqlite::{Connection, Result};
/// fn main() -> Result<()> {
/// let db = Connection::open_in_memory()?;
/// let mut stmt = db.prepare("SELECT 1 as x")?;
/// let column_name = stmt.column_name(0)?;
/// let x = stmt.query_row([], |r| r.get::<_, i64>(0))?; // E0502
/// assert_eq!(1, x);
/// assert_eq!("x", column_name);
/// Ok(())
/// }
/// ```
#[inline]
pub(super) fn column_name_unwrap(&self, col: usize) -> &str {
// Just panic if the bounds are wrong for now, we never call this
// without checking first.
self.column_name(col).expect("Column out of bounds")
}
/// Returns the name assigned to a particular column in the result set
/// returned by the prepared statement.
///
/// If associated DB schema can be altered concurrently, you should make
/// sure that current statement has already been stepped once before
/// calling this method.
///
/// ## Failure
///
/// Returns an `Error::InvalidColumnIndex` if `idx` is outside the valid
/// column range for this row.
///
/// # Panics
///
/// Panics when column name is not valid UTF-8.
#[inline]
pub fn column_name(&self, col: usize) -> Result<&str> {
self.stmt
.column_name(col)
// clippy::or_fun_call (nightly) vs clippy::unnecessary-lazy-evaluations (stable)
.ok_or(Error::InvalidColumnIndex(col))
.map(|slice| {
slice
.to_str()
.expect("Invalid UTF-8 sequence in column name")
})
}
/// Returns the column index in the result set for a given column name.
///
/// If there is no AS clause then the name of the column is unspecified and
/// may change from one release of SQLite to the next.
///
/// If associated DB schema can be altered concurrently, you should make
/// sure that current statement has already been stepped once before
/// calling this method.
///
/// # Failure
///
/// Will return an `Error::InvalidColumnName` when there is no column with
/// the specified `name`.
#[inline]
pub fn column_index(&self, name: &str) -> Result<usize> {
let bytes = name.as_bytes();
let n = self.column_count();
for i in 0..n {
// Note: `column_name` is only fallible if `i` is out of bounds,
// which we've already checked.
if bytes.eq_ignore_ascii_case(self.stmt.column_name(i).unwrap().to_bytes()) {
return Ok(i);
}
}
Err(Error::InvalidColumnName(String::from(name)))
}
/// Returns a slice describing the columns of the result of the query.
///
/// If associated DB schema can be altered concurrently, you should make
/// sure that current statement has already been stepped once before
/// calling this method.
#[cfg(feature = "column_decltype")]
pub fn columns(&self) -> Vec<Column<'_>> {
let n = self.column_count();
let mut cols = Vec::with_capacity(n);
for i in 0..n {
let name = self.column_name_unwrap(i);
let slice = self.stmt.column_decltype(i);
let decl_type = slice.map(|s| {
s.to_str()
.expect("Invalid UTF-8 sequence in column declaration")
});
cols.push(Column { name, decl_type });
}
cols
}
/// Returns the names of the database, table, and row from which
/// each column of this query's results originate.
///
/// Computed or otherwise derived columns will have None values for these fields.
#[cfg(feature = "column_metadata")]
pub fn columns_with_metadata(&self) -> Vec<ColumnMetadata<'_>> {
let n = self.column_count();
let mut col_mets = Vec::with_capacity(n);
for i in 0..n {
let name = self.column_name_unwrap(i);
let db_slice = self.stmt.column_database_name(i);
let tbl_slice = self.stmt.column_table_name(i);
let origin_slice = self.stmt.column_origin_name(i);
col_mets.push(ColumnMetadata {
name,
database_name: db_slice.map(|s| {
s.to_str()
.expect("Invalid UTF-8 sequence in column db name")
}),
table_name: tbl_slice.map(|s| {
s.to_str()
.expect("Invalid UTF-8 sequence in column table name")
}),
origin_name: origin_slice.map(|s| {
s.to_str()
.expect("Invalid UTF-8 sequence in column origin name")
}),
})
}
col_mets
}
/// Extract metadata of column at specified index
///
/// Returns:
/// - database name
/// - table name
/// - original column name
/// - declared data type
/// - name of default collation sequence
/// - True if column has a NOT NULL constraint
/// - True if column is part of the PRIMARY KEY
/// - True if column is AUTOINCREMENT
///
/// See [Connection::column_metadata]
#[cfg(feature = "column_metadata")]
#[expect(clippy::type_complexity)]
pub fn column_metadata(
&self,
col: usize,
) -> Result<
Option<(
&CStr,
&CStr,
&CStr,
Option<&CStr>,
Option<&CStr>,
bool,
bool,
bool,
)>,
> {
let db_name = self.stmt.column_database_name(col);
let table_name = self.stmt.column_table_name(col);
let origin_name = self.stmt.column_origin_name(col);
if db_name.is_none() || table_name.is_none() || origin_name.is_none() {
return Ok(None);
}
let (data_type, coll_seq, not_null, primary_key, auto_inc) =
self.conn
.column_metadata(db_name, table_name.unwrap(), origin_name.unwrap())?;
Ok(Some((
db_name.unwrap(),
table_name.unwrap(),
origin_name.unwrap(),
data_type,
coll_seq,
not_null,
primary_key,
auto_inc,
)))
}
}
impl Connection {
/// Check if `table_name`.`column_name` exists.
///
/// `db_name` is main, temp, the name in ATTACH, or `None` to search all databases.
pub fn column_exists<N: Name>(
&self,
db_name: Option<N>,
table_name: N,
column_name: N,
) -> Result<bool> {
self.exists(db_name, table_name, Some(column_name))
}
/// Check if `table_name` exists.
///
/// `db_name` is main, temp, the name in ATTACH, or `None` to search all databases.
pub fn table_exists<N: Name>(&self, db_name: Option<N>, table_name: N) -> Result<bool> {
self.exists(db_name, table_name, None)
}
/// Extract metadata of column at specified index
///
/// Returns:
/// - declared data type
/// - name of default collation sequence
/// - True if column has a NOT NULL constraint
/// - True if column is part of the PRIMARY KEY
/// - True if column is AUTOINCREMENT
#[expect(clippy::type_complexity)]
pub fn column_metadata<N: Name>(
&self,
db_name: Option<N>,
table_name: N,
column_name: N,
) -> Result<(Option<&CStr>, Option<&CStr>, bool, bool, bool)> {
let cs = db_name.as_ref().map(N::as_cstr).transpose()?;
let db_name = cs.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null());
let table_name = table_name.as_cstr()?;
let column_name = column_name.as_cstr()?;
let mut data_type: *const c_char = ptr::null_mut();
let mut coll_seq: *const c_char = ptr::null_mut();
let mut not_null = 0;
let mut primary_key = 0;
let mut auto_inc = 0;
self.decode_result(unsafe {
ffi::sqlite3_table_column_metadata(
self.handle(),
db_name,
table_name.as_ptr(),
column_name.as_ptr(),
&mut data_type,
&mut coll_seq,
&mut not_null,
&mut primary_key,
&mut auto_inc,
)
})?;
Ok((
if data_type.is_null() {
None
} else {
Some(unsafe { CStr::from_ptr(data_type) })
},
if coll_seq.is_null() {
None
} else {
Some(unsafe { CStr::from_ptr(coll_seq) })
},
not_null != 0,
primary_key != 0,
auto_inc != 0,
))
}
fn exists<N: Name>(
&self,
db_name: Option<N>,
table_name: N,
column_name: Option<N>,
) -> Result<bool> {
let cs = db_name.as_ref().map(N::as_cstr).transpose()?;
let db_name = cs.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null());
let table_name = table_name.as_cstr()?;
let cn = column_name.as_ref().map(N::as_cstr).transpose()?;
let column_name = cn.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null());
let r = unsafe {
ffi::sqlite3_table_column_metadata(
self.handle(),
db_name,
table_name.as_ptr(),
column_name,
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
)
};
match r {
ffi::SQLITE_OK => Ok(true),
ffi::SQLITE_ERROR => Ok(false),
_ => self.db.borrow().decode_result(r).map(|_| false),
}
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use crate::{Connection, Result};
#[test]
#[cfg(feature = "column_decltype")]
fn test_columns() -> Result<()> {
use super::Column;
let db = Connection::open_in_memory()?;
let query = db.prepare("SELECT * FROM sqlite_master")?;
let columns = query.columns();
let column_names: Vec<&str> = columns.iter().map(Column::name).collect();
assert_eq!(
column_names.as_slice(),
&["type", "name", "tbl_name", "rootpage", "sql"]
);
let column_types: Vec<Option<String>> = columns
.iter()
.map(|col| col.decl_type().map(str::to_lowercase))
.collect();
assert_eq!(
&column_types[..3],
&[
Some("text".to_owned()),
Some("text".to_owned()),
Some("text".to_owned()),
]
);
Ok(())
}
#[test]
#[cfg(feature = "column_metadata")]
fn test_columns_with_metadata() -> Result<()> {
let db = Connection::open_in_memory()?;
let query = db.prepare("SELECT *, 1 FROM sqlite_master")?;
let col_mets = query.columns_with_metadata();
assert_eq!(col_mets.len(), 6);
for col in col_mets.iter().take(5) {
assert_eq!(&col.database_name(), &Some("main"));
assert_eq!(&col.table_name(), &Some("sqlite_master"));
}
assert!(col_mets[5].database_name().is_none());
assert!(col_mets[5].table_name().is_none());
assert!(col_mets[5].origin_name().is_none());
let col_origins: Vec<Option<&str>> = col_mets.iter().map(|col| col.origin_name()).collect();
assert_eq!(
&col_origins[..5],
&[
Some("type"),
Some("name"),
Some("tbl_name"),
Some("rootpage"),
Some("sql"),
]
);
Ok(())
}
#[test]
fn test_column_name_in_error() -> Result<()> {
use crate::{types::Type, Error};
let db = Connection::open_in_memory()?;
db.execute_batch(
"BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, NULL);
END;",
)?;
let mut stmt = db.prepare("SELECT x as renamed, y FROM foo")?;
let mut rows = stmt.query([])?;
let row = rows.next()?.unwrap();
match row.get::<_, String>(0).unwrap_err() {
Error::InvalidColumnType(idx, name, ty) => {
assert_eq!(idx, 0);
assert_eq!(name, "renamed");
assert_eq!(ty, Type::Integer);
}
e => {
panic!("Unexpected error type: {e:?}");
}
}
match row.get::<_, String>("y").unwrap_err() {
Error::InvalidColumnType(idx, name, ty) => {
assert_eq!(idx, 1);
assert_eq!(name, "y");
assert_eq!(ty, Type::Null);
}
e => {
panic!("Unexpected error type: {e:?}");
}
}
Ok(())
}
/// `column_name` reference should stay valid until `stmt` is reprepared (or
/// reset) even if DB schema is altered (SQLite documentation is
/// ambiguous here because it says reference "is valid until (...) the next
/// call to `sqlite3_column_name()` or `sqlite3_column_name16()` on the same
/// column.". We assume that reference is valid if only
/// `sqlite3_column_name()` is used):
#[test]
#[cfg(feature = "modern_sqlite")]
fn test_column_name_reference() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE y (x);")?;
let stmt = db.prepare("SELECT x FROM y;")?;
let column_name = stmt.column_name(0)?;
assert_eq!("x", column_name);
db.execute_batch("ALTER TABLE y RENAME COLUMN x TO z;")?;
// column name is not refreshed until statement is re-prepared
let same_column_name = stmt.column_name(0)?;
assert_eq!(same_column_name, column_name);
Ok(())
}
#[test]
#[cfg(feature = "column_metadata")]
fn stmt_column_metadata() -> Result<()> {
let db = Connection::open_in_memory()?;
let query = db.prepare("SELECT *, 1 FROM sqlite_master")?;
let (db_name, table_name, col_name, data_type, coll_seq, not_null, primary_key, auto_inc) =
query.column_metadata(0)?.unwrap();
assert_eq!(db_name, crate::MAIN_DB);
assert_eq!(table_name, c"sqlite_master");
assert_eq!(col_name, c"type");
assert_eq!(data_type, Some(c"TEXT"));
assert_eq!(coll_seq, Some(c"BINARY"));
assert!(!not_null);
assert!(!primary_key);
assert!(!auto_inc);
assert!(query.column_metadata(5)?.is_none());
Ok(())
}
#[test]
fn column_exists() -> Result<()> {
let db = Connection::open_in_memory()?;
assert!(db.column_exists(None, c"sqlite_master", c"type")?);
assert!(db.column_exists(Some(crate::TEMP_DB), c"sqlite_master", c"type")?);
assert!(!db.column_exists(Some(crate::MAIN_DB), c"sqlite_temp_master", c"type")?);
Ok(())
}
#[test]
fn table_exists() -> Result<()> {
let db = Connection::open_in_memory()?;
assert!(db.table_exists(None, c"sqlite_master")?);
assert!(db.table_exists(Some(crate::TEMP_DB), c"sqlite_master")?);
assert!(!db.table_exists(Some(crate::MAIN_DB), c"sqlite_temp_master")?);
Ok(())
}
#[test]
fn column_metadata() -> Result<()> {
let db = Connection::open_in_memory()?;
let (data_type, coll_seq, not_null, primary_key, auto_inc) =
db.column_metadata(None, c"sqlite_master", c"type")?;
assert_eq!(
data_type.map(|cs| cs.to_str().unwrap().to_ascii_uppercase()),
Some("TEXT".to_owned())
);
assert_eq!(coll_seq, Some(c"BINARY"));
assert!(!not_null);
assert!(!primary_key);
assert!(!auto_inc);
assert!(db.column_metadata(None, c"sqlite_master", c"foo").is_err());
Ok(())
}
}

View File

@@ -1,169 +0,0 @@
//! Configure database connections
use std::ffi::c_int;
use crate::error::check;
use crate::ffi;
use crate::{Connection, Result};
/// Database Connection Configuration Options
/// See [Database Connection Configuration Options](https://sqlite.org/c3ref/c_dbconfig_enable_fkey.html) for details.
#[repr(i32)]
#[derive(Copy, Clone, Debug)]
#[expect(non_camel_case_types)]
#[non_exhaustive]
pub enum DbConfig {
//SQLITE_DBCONFIG_MAINDBNAME = 1000, /* const char* */
//SQLITE_DBCONFIG_LOOKASIDE = 1001, /* void* int int */
/// Enable or disable the enforcement of foreign key constraints.
SQLITE_DBCONFIG_ENABLE_FKEY = ffi::SQLITE_DBCONFIG_ENABLE_FKEY,
/// Enable or disable triggers.
SQLITE_DBCONFIG_ENABLE_TRIGGER = ffi::SQLITE_DBCONFIG_ENABLE_TRIGGER,
/// Enable or disable the `fts3_tokenizer()` function which is part of the
/// FTS3 full-text search engine extension.
SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER = ffi::SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, // 3.12.0
//SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION = 1005,
/// In WAL mode, enable or disable the checkpoint operation before closing
/// the connection.
SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE = 1006, // 3.16.2
/// Activates or deactivates the query planner stability guarantee (QPSG).
SQLITE_DBCONFIG_ENABLE_QPSG = 1007, // 3.20.0
/// Includes or excludes output for any operations performed by trigger
/// programs from the output of EXPLAIN QUERY PLAN commands.
SQLITE_DBCONFIG_TRIGGER_EQP = 1008, // 3.22.0
/// Activates or deactivates the "reset" flag for a database connection.
/// Run VACUUM with this flag set to reset the database.
SQLITE_DBCONFIG_RESET_DATABASE = 1009, // 3.24.0
/// Activates or deactivates the "defensive" flag for a database connection.
SQLITE_DBCONFIG_DEFENSIVE = 1010, // 3.26.0
/// Activates or deactivates the `writable_schema` flag.
SQLITE_DBCONFIG_WRITABLE_SCHEMA = 1011, // 3.28.0
/// Activates or deactivates the legacy behavior of the ALTER TABLE RENAME
/// command.
SQLITE_DBCONFIG_LEGACY_ALTER_TABLE = 1012, // 3.29
/// Activates or deactivates the legacy double-quoted string literal
/// misfeature for DML statements only.
SQLITE_DBCONFIG_DQS_DML = 1013, // 3.29.0
/// Activates or deactivates the legacy double-quoted string literal
/// misfeature for DDL statements.
SQLITE_DBCONFIG_DQS_DDL = 1014, // 3.29.0
/// Enable or disable views.
SQLITE_DBCONFIG_ENABLE_VIEW = 1015, // 3.30.0
/// Activates or deactivates the legacy file format flag.
SQLITE_DBCONFIG_LEGACY_FILE_FORMAT = 1016, // 3.31.0
/// Tells SQLite to assume that database schemas (the contents of the
/// `sqlite_master` tables) are untainted by malicious content.
SQLITE_DBCONFIG_TRUSTED_SCHEMA = 1017, // 3.31.0
/// Sets or clears a flag that enables collection of the
/// `sqlite3_stmt_scanstatus_v2()` statistics
#[cfg(feature = "modern_sqlite")]
SQLITE_DBCONFIG_STMT_SCANSTATUS = 1018, // 3.42.0
/// Changes the default order in which tables and indexes are scanned
#[cfg(feature = "modern_sqlite")]
SQLITE_DBCONFIG_REVERSE_SCANORDER = 1019, // 3.42.0
/// Enables or disables the ability of the ATTACH DATABASE SQL command
/// to create a new database file if the database filed named in the ATTACH command does not already exist.
#[cfg(feature = "modern_sqlite")]
SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE = 1020, // 3.49.0
/// Enables or disables the ability of the ATTACH DATABASE SQL command to open a database for writing.
#[cfg(feature = "modern_sqlite")]
SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE = 1021, // 3.49.0
/// Enables or disables the ability to include comments in SQL text.
#[cfg(feature = "modern_sqlite")]
SQLITE_DBCONFIG_ENABLE_COMMENTS = 1022, // 3.49.0
}
impl Connection {
/// Returns the current value of a `config`.
///
/// - `SQLITE_DBCONFIG_ENABLE_FKEY`: return `false` or `true` to indicate
/// whether FK enforcement is off or on
/// - `SQLITE_DBCONFIG_ENABLE_TRIGGER`: return `false` or `true` to indicate
/// whether triggers are disabled or enabled
/// - `SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER`: return `false` or `true` to
/// indicate whether `fts3_tokenizer` are disabled or enabled
/// - `SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE`: return `false` to indicate
/// checkpoints-on-close are not disabled or `true` if they are
/// - `SQLITE_DBCONFIG_ENABLE_QPSG`: return `false` or `true` to indicate
/// whether the QPSG is disabled or enabled
/// - `SQLITE_DBCONFIG_TRIGGER_EQP`: return `false` to indicate
/// output-for-trigger are not disabled or `true` if it is
#[inline]
pub fn db_config(&self, config: DbConfig) -> Result<bool> {
let c = self.db.borrow();
unsafe {
let mut val = 0;
check(ffi::sqlite3_db_config(
c.db(),
config as c_int,
-1,
&mut val,
))?;
Ok(val != 0)
}
}
/// Make configuration changes to a database connection
///
/// - `SQLITE_DBCONFIG_ENABLE_FKEY`: `false` to disable FK enforcement,
/// `true` to enable FK enforcement
/// - `SQLITE_DBCONFIG_ENABLE_TRIGGER`: `false` to disable triggers, `true`
/// to enable triggers
/// - `SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER`: `false` to disable
/// `fts3_tokenizer()`, `true` to enable `fts3_tokenizer()`
/// - `SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE`: `false` (the default) to enable
/// checkpoints-on-close, `true` to disable them
/// - `SQLITE_DBCONFIG_ENABLE_QPSG`: `false` to disable the QPSG, `true` to
/// enable QPSG
/// - `SQLITE_DBCONFIG_TRIGGER_EQP`: `false` to disable output for trigger
/// programs, `true` to enable it
#[inline]
pub fn set_db_config(&self, config: DbConfig, new_val: bool) -> Result<bool> {
let c = self.db.borrow_mut();
unsafe {
let mut val = 0;
check(ffi::sqlite3_db_config(
c.db(),
config as c_int,
new_val as c_int,
&mut val,
))?;
Ok(val != 0)
}
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use super::DbConfig;
use crate::{Connection, Result};
#[test]
fn test_db_config() -> Result<()> {
let db = Connection::open_in_memory()?;
let opposite = !db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY)?;
assert_eq!(
db.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY, opposite),
Ok(opposite)
);
assert_eq!(
db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY),
Ok(opposite)
);
let opposite = !db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER)?;
assert_eq!(
db.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER, opposite),
Ok(opposite)
);
assert_eq!(
db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER),
Ok(opposite)
);
Ok(())
}
}

View File

@@ -1,62 +0,0 @@
//! Code related to `sqlite3_context` common to `functions` and `vtab` modules.
use crate::ffi::sqlite3_value;
use std::ffi::c_void;
use crate::ffi;
use crate::ffi::sqlite3_context;
use crate::str_for_sqlite;
use crate::types::{ToSqlOutput, ValueRef};
// This function is inline despite it's size because what's in the ToSqlOutput
// is often known to the compiler, and thus const prop/DCE can substantially
// simplify the function.
#[inline]
pub(super) unsafe fn set_result(
ctx: *mut sqlite3_context,
#[allow(unused_variables)] args: &[*mut sqlite3_value],
result: &ToSqlOutput<'_>,
) {
let value = match *result {
ToSqlOutput::Borrowed(v) => v,
ToSqlOutput::Owned(ref v) => ValueRef::from(v),
#[cfg(feature = "blob")]
ToSqlOutput::ZeroBlob(len) => {
// TODO sqlite3_result_zeroblob64 // 3.8.11
return ffi::sqlite3_result_zeroblob(ctx, len);
}
#[cfg(feature = "functions")]
ToSqlOutput::Arg(i) => {
return ffi::sqlite3_result_value(ctx, args[i]);
}
#[cfg(feature = "pointer")]
ToSqlOutput::Pointer(ref p) => {
return ffi::sqlite3_result_pointer(ctx, p.0 as _, p.1.as_ptr(), p.2);
}
};
match value {
ValueRef::Null => ffi::sqlite3_result_null(ctx),
ValueRef::Integer(i) => ffi::sqlite3_result_int64(ctx, i),
ValueRef::Real(r) => ffi::sqlite3_result_double(ctx, r),
ValueRef::Text(s) => {
let (c_str, len, destructor) = str_for_sqlite(s);
ffi::sqlite3_result_text64(ctx, c_str, len, destructor, ffi::SQLITE_UTF8 as _);
}
ValueRef::Blob(b) => {
let length = b.len();
if length == 0 {
ffi::sqlite3_result_zeroblob(ctx, 0);
} else {
ffi::sqlite3_result_blob64(
ctx,
b.as_ptr().cast::<c_void>(),
length as ffi::sqlite3_uint64,
ffi::SQLITE_TRANSIENT(),
);
}
}
}
}

View File

@@ -1,514 +0,0 @@
use crate::types::FromSqlError;
use crate::types::Type;
use crate::{errmsg_to_string, ffi, Result};
use std::error;
use std::ffi::{c_char, c_int, NulError};
use std::fmt;
use std::path::PathBuf;
use std::str;
/// Enum listing possible errors from rusqlite.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// An error from an underlying SQLite call.
SqliteFailure(ffi::Error, Option<String>),
/// Error reported when attempting to open a connection when SQLite was
/// configured to allow single-threaded use only.
SqliteSingleThreadedMode,
/// Error when the value of a particular column is requested, but it cannot
/// be converted to the requested Rust type.
FromSqlConversionFailure(usize, Type, Box<dyn error::Error + Send + Sync + 'static>),
/// Error when SQLite gives us an integral value outside the range of the
/// requested type (e.g., trying to get the value 1000 into a `u8`).
/// The associated `usize` is the column index,
/// and the associated `i64` is the value returned by SQLite.
IntegralValueOutOfRange(usize, i64),
/// Error converting a string to UTF-8.
Utf8Error(usize, str::Utf8Error),
/// Error converting a string to a C-compatible string because it contained
/// an embedded nul.
NulError(NulError),
/// Error when using SQL named parameters and passing a parameter name not
/// present in the SQL.
InvalidParameterName(String),
/// Error converting a file path to a string.
InvalidPath(PathBuf),
/// Error returned when an [`execute`](crate::Connection::execute) call
/// returns rows.
ExecuteReturnedResults,
/// Error when a query that was expected to return at least one row (e.g.,
/// for [`query_row`](crate::Connection::query_row)) did not return any.
QueryReturnedNoRows,
/// Error when a query that was expected to return only one row (e.g.,
/// for [`query_one`](crate::Connection::query_one)) did return more than one.
QueryReturnedMoreThanOneRow,
/// Error when the value of a particular column is requested, but the index
/// is out of range for the statement.
InvalidColumnIndex(usize),
/// Error when the value of a named column is requested, but no column
/// matches the name for the statement.
InvalidColumnName(String),
/// Error when the value of a particular column is requested, but the type
/// of the result in that column cannot be converted to the requested
/// Rust type.
InvalidColumnType(usize, String, Type),
/// Error when a query that was expected to insert one row did not insert
/// any or insert many.
StatementChangedRows(usize),
/// Error returned by
/// [`functions::Context::get`](crate::functions::Context::get) when the
/// function argument cannot be converted to the requested type.
#[cfg(feature = "functions")]
InvalidFunctionParameterType(usize, Type),
/// Error returned by [`vtab::Values::get`](crate::vtab::Values::get) when
/// the filter argument cannot be converted to the requested type.
#[cfg(feature = "vtab")]
InvalidFilterParameterType(usize, Type),
/// An error case available for implementors of custom user functions (e.g.,
/// [`create_scalar_function`](crate::Connection::create_scalar_function)).
#[cfg(feature = "functions")]
UserFunctionError(Box<dyn error::Error + Send + Sync + 'static>),
/// Error available for the implementors of the
/// [`ToSql`](crate::types::ToSql) trait.
ToSqlConversionFailure(Box<dyn error::Error + Send + Sync + 'static>),
/// Error when the SQL is not a `SELECT`, is not read-only.
InvalidQuery,
/// An error case available for implementors of custom modules (e.g.,
/// [`create_module`](crate::Connection::create_module)).
#[cfg(feature = "vtab")]
ModuleError(String),
/// An unwinding panic occurs in a UDF (user-defined function).
UnwindingPanic,
/// An error returned when
/// [`Context::get_aux`](crate::functions::Context::get_aux) attempts to
/// retrieve data of a different type than what had been stored using
/// [`Context::set_aux`](crate::functions::Context::set_aux).
#[cfg(feature = "functions")]
GetAuxWrongType,
/// Error when the SQL contains multiple statements.
MultipleStatement,
/// Error when the number of bound parameters does not match the number of
/// parameters in the query. The first `usize` is how many parameters were
/// given, the 2nd is how many were expected.
InvalidParameterCount(usize, usize),
/// Returned from various functions in the Blob IO positional API. For
/// example,
/// [`Blob::raw_read_at_exact`](crate::blob::Blob::raw_read_at_exact) will
/// return it if the blob has insufficient data.
#[cfg(feature = "blob")]
BlobSizeError,
/// Error referencing a specific token in the input SQL
#[cfg(feature = "modern_sqlite")] // 3.38.0
SqlInputError {
/// error code
error: ffi::Error,
/// error message
msg: String,
/// SQL input
sql: String,
/// byte offset of the start of invalid token
offset: c_int,
},
/// Loadable extension initialization error
#[cfg(feature = "loadable_extension")]
InitError(ffi::InitError),
/// Error when the schema of a particular database is requested, but the index
/// is out of range.
#[cfg(feature = "modern_sqlite")] // 3.39.0
InvalidDatabaseIndex(usize),
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::SqliteFailure(e1, s1), Self::SqliteFailure(e2, s2)) => e1 == e2 && s1 == s2,
(Self::SqliteSingleThreadedMode, Self::SqliteSingleThreadedMode) => true,
(Self::IntegralValueOutOfRange(i1, n1), Self::IntegralValueOutOfRange(i2, n2)) => {
i1 == i2 && n1 == n2
}
(Self::Utf8Error(i1, e1), Self::Utf8Error(i2, e2)) => i1 == i2 && e1 == e2,
(Self::NulError(e1), Self::NulError(e2)) => e1 == e2,
(Self::InvalidParameterName(n1), Self::InvalidParameterName(n2)) => n1 == n2,
(Self::InvalidPath(p1), Self::InvalidPath(p2)) => p1 == p2,
(Self::ExecuteReturnedResults, Self::ExecuteReturnedResults) => true,
(Self::QueryReturnedNoRows, Self::QueryReturnedNoRows) => true,
(Self::QueryReturnedMoreThanOneRow, Self::QueryReturnedMoreThanOneRow) => true,
(Self::InvalidColumnIndex(i1), Self::InvalidColumnIndex(i2)) => i1 == i2,
(Self::InvalidColumnName(n1), Self::InvalidColumnName(n2)) => n1 == n2,
(Self::InvalidColumnType(i1, n1, t1), Self::InvalidColumnType(i2, n2, t2)) => {
i1 == i2 && t1 == t2 && n1 == n2
}
(Self::StatementChangedRows(n1), Self::StatementChangedRows(n2)) => n1 == n2,
#[cfg(feature = "functions")]
(
Self::InvalidFunctionParameterType(i1, t1),
Self::InvalidFunctionParameterType(i2, t2),
) => i1 == i2 && t1 == t2,
#[cfg(feature = "vtab")]
(
Self::InvalidFilterParameterType(i1, t1),
Self::InvalidFilterParameterType(i2, t2),
) => i1 == i2 && t1 == t2,
(Self::InvalidQuery, Self::InvalidQuery) => true,
#[cfg(feature = "vtab")]
(Self::ModuleError(s1), Self::ModuleError(s2)) => s1 == s2,
(Self::UnwindingPanic, Self::UnwindingPanic) => true,
#[cfg(feature = "functions")]
(Self::GetAuxWrongType, Self::GetAuxWrongType) => true,
(Self::InvalidParameterCount(i1, n1), Self::InvalidParameterCount(i2, n2)) => {
i1 == i2 && n1 == n2
}
#[cfg(feature = "blob")]
(Self::BlobSizeError, Self::BlobSizeError) => true,
#[cfg(feature = "modern_sqlite")]
(
Self::SqlInputError {
error: e1,
msg: m1,
sql: s1,
offset: o1,
},
Self::SqlInputError {
error: e2,
msg: m2,
sql: s2,
offset: o2,
},
) => e1 == e2 && m1 == m2 && s1 == s2 && o1 == o2,
#[cfg(feature = "loadable_extension")]
(Self::InitError(e1), Self::InitError(e2)) => e1 == e2,
#[cfg(feature = "modern_sqlite")]
(Self::InvalidDatabaseIndex(i1), Self::InvalidDatabaseIndex(i2)) => i1 == i2,
(..) => false,
}
}
}
impl From<str::Utf8Error> for Error {
#[cold]
fn from(err: str::Utf8Error) -> Self {
Self::Utf8Error(UNKNOWN_COLUMN, err)
}
}
impl From<NulError> for Error {
#[cold]
fn from(err: NulError) -> Self {
Self::NulError(err)
}
}
const UNKNOWN_COLUMN: usize = usize::MAX;
/// The conversion isn't precise, but it's convenient to have it
/// to allow use of `get_raw(…).as_…()?` in callbacks that take `Error`.
impl From<FromSqlError> for Error {
#[cold]
fn from(err: FromSqlError) -> Self {
// The error type requires index and type fields, but they aren't known in this
// context.
match err {
FromSqlError::OutOfRange(val) => Self::IntegralValueOutOfRange(UNKNOWN_COLUMN, val),
FromSqlError::InvalidBlobSize { .. } => {
Self::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Blob, Box::new(err))
}
FromSqlError::Other(source) => {
Self::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, source)
}
_ => Self::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, Box::new(err)),
}
}
}
#[cfg(feature = "loadable_extension")]
impl From<ffi::InitError> for Error {
#[cold]
fn from(err: ffi::InitError) -> Self {
Self::InitError(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::SqliteFailure(ref err, None) => err.fmt(f),
Self::SqliteFailure(_, Some(ref s)) => write!(f, "{s}"),
Self::SqliteSingleThreadedMode => write!(
f,
"SQLite was compiled or configured for single-threaded use only"
),
Self::FromSqlConversionFailure(i, ref t, ref err) => {
if i != UNKNOWN_COLUMN {
write!(f, "Conversion error from type {t} at index: {i}, {err}")
} else {
err.fmt(f)
}
}
Self::IntegralValueOutOfRange(col, val) => {
if col != UNKNOWN_COLUMN {
write!(f, "Integer {val} out of range at index {col}")
} else {
write!(f, "Integer {val} out of range")
}
}
Self::Utf8Error(col, ref err) => {
if col != UNKNOWN_COLUMN {
write!(f, "{err} at index {col}")
} else {
err.fmt(f)
}
}
Self::NulError(ref err) => err.fmt(f),
Self::InvalidParameterName(ref name) => write!(f, "Invalid parameter name: {name}"),
Self::InvalidPath(ref p) => write!(f, "Invalid path: {}", p.to_string_lossy()),
Self::ExecuteReturnedResults => {
write!(f, "Execute returned results - did you mean to call query?")
}
Self::QueryReturnedNoRows => write!(f, "Query returned no rows"),
Self::QueryReturnedMoreThanOneRow => write!(f, "Query returned more than one row"),
Self::InvalidColumnIndex(i) => write!(f, "Invalid column index: {i}"),
Self::InvalidColumnName(ref name) => write!(f, "Invalid column name: {name}"),
Self::InvalidColumnType(i, ref name, ref t) => {
write!(f, "Invalid column type {t} at index: {i}, name: {name}")
}
Self::InvalidParameterCount(i1, n1) => write!(
f,
"Wrong number of parameters passed to query. Got {i1}, needed {n1}"
),
Self::StatementChangedRows(i) => write!(f, "Query changed {i} rows"),
#[cfg(feature = "functions")]
Self::InvalidFunctionParameterType(i, ref t) => {
write!(f, "Invalid function parameter type {t} at index {i}")
}
#[cfg(feature = "vtab")]
Self::InvalidFilterParameterType(i, ref t) => {
write!(f, "Invalid filter parameter type {t} at index {i}")
}
#[cfg(feature = "functions")]
Self::UserFunctionError(ref err) => err.fmt(f),
Self::ToSqlConversionFailure(ref err) => err.fmt(f),
Self::InvalidQuery => write!(f, "Query is not read-only"),
#[cfg(feature = "vtab")]
Self::ModuleError(ref desc) => write!(f, "{desc}"),
Self::UnwindingPanic => write!(f, "unwinding panic"),
#[cfg(feature = "functions")]
Self::GetAuxWrongType => write!(f, "get_aux called with wrong type"),
Self::MultipleStatement => write!(f, "Multiple statements provided"),
#[cfg(feature = "blob")]
Self::BlobSizeError => "Blob size is insufficient".fmt(f),
#[cfg(feature = "modern_sqlite")]
Self::SqlInputError {
ref msg,
offset,
ref sql,
..
} => write!(f, "{msg} in {sql} at offset {offset}"),
#[cfg(feature = "loadable_extension")]
Self::InitError(ref err) => err.fmt(f),
#[cfg(feature = "modern_sqlite")]
Self::InvalidDatabaseIndex(i) => write!(f, "Invalid database index: {i}"),
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Self::SqliteFailure(ref err, _) => Some(err),
Self::Utf8Error(_, ref err) => Some(err),
Self::NulError(ref err) => Some(err),
Self::IntegralValueOutOfRange(..)
| Self::SqliteSingleThreadedMode
| Self::InvalidParameterName(_)
| Self::ExecuteReturnedResults
| Self::QueryReturnedNoRows
| Self::QueryReturnedMoreThanOneRow
| Self::InvalidColumnIndex(_)
| Self::InvalidColumnName(_)
| Self::InvalidColumnType(..)
| Self::InvalidPath(_)
| Self::InvalidParameterCount(..)
| Self::StatementChangedRows(_)
| Self::InvalidQuery
| Self::MultipleStatement => None,
#[cfg(feature = "functions")]
Self::InvalidFunctionParameterType(..) => None,
#[cfg(feature = "vtab")]
Self::InvalidFilterParameterType(..) => None,
#[cfg(feature = "functions")]
Self::UserFunctionError(ref err) => Some(&**err),
Self::FromSqlConversionFailure(_, _, ref err)
| Self::ToSqlConversionFailure(ref err) => Some(&**err),
#[cfg(feature = "vtab")]
Self::ModuleError(_) => None,
Self::UnwindingPanic => None,
#[cfg(feature = "functions")]
Self::GetAuxWrongType => None,
#[cfg(feature = "blob")]
Self::BlobSizeError => None,
#[cfg(feature = "modern_sqlite")]
Self::SqlInputError { ref error, .. } => Some(error),
#[cfg(feature = "loadable_extension")]
Self::InitError(ref err) => Some(err),
#[cfg(feature = "modern_sqlite")]
Self::InvalidDatabaseIndex(_) => None,
}
}
}
impl Error {
/// Returns the underlying SQLite error if this is [`Error::SqliteFailure`].
#[inline]
#[must_use]
pub fn sqlite_error(&self) -> Option<&ffi::Error> {
match self {
Self::SqliteFailure(error, _) => Some(error),
_ => None,
}
}
/// Returns the underlying SQLite error code if this is
/// [`Error::SqliteFailure`].
#[inline]
#[must_use]
pub fn sqlite_error_code(&self) -> Option<ffi::ErrorCode> {
self.sqlite_error().map(|error| error.code)
}
}
// These are public but not re-exported by lib.rs, so only visible within crate.
#[cold]
pub fn error_from_sqlite_code(code: c_int, message: Option<String>) -> Error {
Error::SqliteFailure(ffi::Error::new(code), message)
}
macro_rules! err {
($code:expr $(,)?) => {
$crate::error::error_from_sqlite_code($code, None)
};
($code:expr, $msg:literal $(,)?) => {
$crate::error::error_from_sqlite_code($code, Some(format!($msg)))
};
($code:expr, $err:expr $(,)?) => {
$crate::error::error_from_sqlite_code($code, Some(format!($err)))
};
($code:expr, $fmt:expr, $($arg:tt)*) => {
$crate::error::error_from_sqlite_code($code, Some(format!($fmt, $($arg)*)))
};
}
#[cold]
pub unsafe fn error_from_handle(db: *mut ffi::sqlite3, code: c_int) -> Error {
error_from_sqlite_code(code, error_msg(db, code))
}
unsafe fn error_msg(db: *mut ffi::sqlite3, code: c_int) -> Option<String> {
if db.is_null() || ffi::sqlite3_errcode(db) != code {
let err_str = ffi::sqlite3_errstr(code);
if err_str.is_null() {
None
} else {
Some(errmsg_to_string(err_str))
}
} else {
Some(errmsg_to_string(ffi::sqlite3_errmsg(db)))
}
}
pub unsafe fn decode_result_raw(db: *mut ffi::sqlite3, code: c_int) -> Result<()> {
if code == ffi::SQLITE_OK {
Ok(())
} else {
Err(error_from_handle(db, code))
}
}
#[cold]
#[cfg(not(feature = "modern_sqlite"))] // SQLite >= 3.38.0
pub unsafe fn error_with_offset(db: *mut ffi::sqlite3, code: c_int, _sql: &str) -> Error {
error_from_handle(db, code)
}
#[cold]
#[cfg(feature = "modern_sqlite")] // SQLite >= 3.38.0
pub unsafe fn error_with_offset(db: *mut ffi::sqlite3, code: c_int, sql: &str) -> Error {
if db.is_null() {
error_from_sqlite_code(code, None)
} else {
let error = ffi::Error::new(code);
let msg = error_msg(db, code);
if ffi::ErrorCode::Unknown == error.code {
let offset = ffi::sqlite3_error_offset(db);
if offset >= 0 {
return Error::SqlInputError {
error,
msg: msg.unwrap_or("error".to_owned()),
sql: sql.to_owned(),
offset,
};
}
}
Error::SqliteFailure(error, msg)
}
}
pub fn check(code: c_int) -> Result<()> {
if code != ffi::SQLITE_OK {
Err(error_from_sqlite_code(code, None))
} else {
Ok(())
}
}
/// Transform Rust error to SQLite error (message and code).
/// # Safety
/// This function is unsafe because it uses raw pointer
pub unsafe fn to_sqlite_error(e: &Error, err_msg: *mut *mut c_char) -> c_int {
use crate::util::alloc;
match e {
Error::SqliteFailure(err, s) => {
if let Some(s) = s {
*err_msg = alloc(s);
}
err.extended_code
}
err => {
*err_msg = alloc(&err.to_string());
ffi::SQLITE_ERROR
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,362 +0,0 @@
use std::ffi::{c_char, c_int, c_void};
use std::fmt::Debug;
use std::panic::catch_unwind;
use std::ptr;
use super::expect_utf8;
use super::Action;
use crate::error::check;
use crate::ffi;
use crate::inner_connection::InnerConnection;
use crate::types::ValueRef;
use crate::Connection;
use crate::Result;
/// The possible cases for when a PreUpdateHook gets triggered. Allows access to the relevant
/// functions for each case through the contained values.
#[derive(Debug)]
pub enum PreUpdateCase {
/// Pre-update hook was triggered by an insert.
Insert(PreUpdateNewValueAccessor),
/// Pre-update hook was triggered by a delete.
Delete(PreUpdateOldValueAccessor),
/// Pre-update hook was triggered by an update.
Update {
#[allow(missing_docs)]
old_value_accessor: PreUpdateOldValueAccessor,
#[allow(missing_docs)]
new_value_accessor: PreUpdateNewValueAccessor,
},
/// This variant is not normally produced by SQLite. You may encounter it
/// if you're using a different version than what's supported by this library.
Unknown,
}
impl From<PreUpdateCase> for Action {
fn from(puc: PreUpdateCase) -> Action {
match puc {
PreUpdateCase::Insert(_) => Action::SQLITE_INSERT,
PreUpdateCase::Delete(_) => Action::SQLITE_DELETE,
PreUpdateCase::Update { .. } => Action::SQLITE_UPDATE,
PreUpdateCase::Unknown => Action::UNKNOWN,
}
}
}
/// An accessor to access the old values of the row being deleted/updated during the preupdate callback.
#[derive(Debug)]
pub struct PreUpdateOldValueAccessor {
db: *mut ffi::sqlite3,
old_row_id: i64,
}
impl PreUpdateOldValueAccessor {
/// Get the amount of columns in the row being deleted/updated.
pub fn get_column_count(&self) -> i32 {
unsafe { ffi::sqlite3_preupdate_count(self.db) }
}
/// Get the depth of the query that triggered the preupdate hook.
/// Returns 0 if the preupdate callback was invoked as a result of
/// a direct insert, update, or delete operation;
/// 1 for inserts, updates, or deletes invoked by top-level triggers;
/// 2 for changes resulting from triggers called by top-level triggers; and so forth.
pub fn get_query_depth(&self) -> i32 {
unsafe { ffi::sqlite3_preupdate_depth(self.db) }
}
/// Get the row id of the row being updated/deleted.
pub fn get_old_row_id(&self) -> i64 {
self.old_row_id
}
/// Get the value of the row being updated/deleted at the specified index.
pub fn get_old_column_value(&self, i: i32) -> Result<ValueRef<'_>> {
let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut();
unsafe {
check(ffi::sqlite3_preupdate_old(self.db, i, &mut p_value))?;
Ok(ValueRef::from_value(p_value))
}
}
}
/// An accessor to access the new values of the row being inserted/updated
/// during the preupdate callback.
#[derive(Debug)]
pub struct PreUpdateNewValueAccessor {
db: *mut ffi::sqlite3,
new_row_id: i64,
}
impl PreUpdateNewValueAccessor {
/// Get the amount of columns in the row being inserted/updated.
pub fn get_column_count(&self) -> i32 {
unsafe { ffi::sqlite3_preupdate_count(self.db) }
}
/// Get the depth of the query that triggered the preupdate hook.
/// Returns 0 if the preupdate callback was invoked as a result of
/// a direct insert, update, or delete operation;
/// 1 for inserts, updates, or deletes invoked by top-level triggers;
/// 2 for changes resulting from triggers called by top-level triggers; and so forth.
pub fn get_query_depth(&self) -> i32 {
unsafe { ffi::sqlite3_preupdate_depth(self.db) }
}
/// Get the row id of the row being inserted/updated.
pub fn get_new_row_id(&self) -> i64 {
self.new_row_id
}
/// Get the value of the row being updated/deleted at the specified index.
pub fn get_new_column_value(&self, i: i32) -> Result<ValueRef<'_>> {
let mut p_value: *mut ffi::sqlite3_value = ptr::null_mut();
unsafe {
check(ffi::sqlite3_preupdate_new(self.db, i, &mut p_value))?;
Ok(ValueRef::from_value(p_value))
}
}
}
impl Connection {
/// Register a callback function to be invoked before
/// a row is updated, inserted or deleted.
///
/// The callback parameters are:
///
/// - the name of the database ("main", "temp", ...),
/// - the name of the table that is updated,
/// - a variant of the PreUpdateCase enum which allows access to extra functions depending
/// on whether it's an update, delete or insert.
#[inline]
pub fn preupdate_hook<F>(&self, hook: Option<F>) -> Result<()>
where
F: FnMut(Action, &str, &str, &PreUpdateCase) + Send + 'static,
{
self.db.borrow().check_owned()?;
self.db.borrow_mut().preupdate_hook(hook);
Ok(())
}
}
impl InnerConnection {
#[inline]
pub fn remove_preupdate_hook(&mut self) {
self.preupdate_hook(None::<fn(Action, &str, &str, &PreUpdateCase)>);
}
/// ```compile_fail
/// use rusqlite::{Connection, Result, hooks::PreUpdateCase};
/// fn main() -> Result<()> {
/// let db = Connection::open_in_memory()?;
/// {
/// let mut called = std::sync::atomic::AtomicBool::new(false);
/// db.preupdate_hook(Some(|action, db: &str, tbl: &str, case: &PreUpdateCase| {
/// called.store(true, std::sync::atomic::Ordering::Relaxed);
/// }));
/// }
/// db.execute_batch("CREATE TABLE foo AS SELECT 1 AS bar;")
/// }
/// ```
fn preupdate_hook<F>(&mut self, hook: Option<F>)
where
F: FnMut(Action, &str, &str, &PreUpdateCase) + Send + 'static,
{
unsafe extern "C" fn call_boxed_closure<F>(
p_arg: *mut c_void,
sqlite: *mut ffi::sqlite3,
action_code: c_int,
db_name: *const c_char,
tbl_name: *const c_char,
old_row_id: i64,
new_row_id: i64,
) where
F: FnMut(Action, &str, &str, &PreUpdateCase),
{
let action = Action::from(action_code);
let preupdate_case = match action {
Action::SQLITE_INSERT => PreUpdateCase::Insert(PreUpdateNewValueAccessor {
db: sqlite,
new_row_id,
}),
Action::SQLITE_DELETE => PreUpdateCase::Delete(PreUpdateOldValueAccessor {
db: sqlite,
old_row_id,
}),
Action::SQLITE_UPDATE => PreUpdateCase::Update {
old_value_accessor: PreUpdateOldValueAccessor {
db: sqlite,
old_row_id,
},
new_value_accessor: PreUpdateNewValueAccessor {
db: sqlite,
new_row_id,
},
},
Action::UNKNOWN => PreUpdateCase::Unknown,
};
drop(catch_unwind(|| {
let boxed_hook: *mut F = p_arg.cast::<F>();
(*boxed_hook)(
action,
expect_utf8(db_name, "database name"),
expect_utf8(tbl_name, "table name"),
&preupdate_case,
);
}));
}
let boxed_hook = hook.map(Box::new);
unsafe {
ffi::sqlite3_preupdate_hook(
self.db(),
boxed_hook.as_ref().map(|_| call_boxed_closure::<F> as _),
boxed_hook
.as_ref()
.map_or_else(ptr::null_mut, |h| &**h as *const F as *mut _),
)
};
self.preupdate_hook = boxed_hook.map(|bh| bh as _);
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use std::sync::atomic::{AtomicBool, Ordering};
use super::super::Action;
use super::PreUpdateCase;
use crate::{Connection, Result};
#[test]
fn test_preupdate_hook_insert() -> Result<()> {
let db = Connection::open_in_memory()?;
static CALLED: AtomicBool = AtomicBool::new(false);
db.preupdate_hook(Some(|action, db: &str, tbl: &str, case: &PreUpdateCase| {
assert_eq!(Action::SQLITE_INSERT, action);
assert_eq!("main", db);
assert_eq!("foo", tbl);
match case {
PreUpdateCase::Insert(accessor) => {
assert_eq!(1, accessor.get_column_count());
assert_eq!(1, accessor.get_new_row_id());
assert_eq!(0, accessor.get_query_depth());
// out of bounds access should return an error
assert!(accessor.get_new_column_value(1).is_err());
assert_eq!(
"lisa",
accessor.get_new_column_value(0).unwrap().as_str().unwrap()
);
assert_eq!(0, accessor.get_query_depth());
}
_ => panic!("wrong preupdate case"),
}
CALLED.store(true, Ordering::Relaxed);
}))?;
db.execute_batch("CREATE TABLE foo (t TEXT)")?;
db.execute_batch("INSERT INTO foo VALUES ('lisa')")?;
assert!(CALLED.load(Ordering::Relaxed));
Ok(())
}
#[test]
fn test_preupdate_hook_delete() -> Result<()> {
let db = Connection::open_in_memory()?;
static CALLED: AtomicBool = AtomicBool::new(false);
db.execute_batch("CREATE TABLE foo (t TEXT)")?;
db.execute_batch("INSERT INTO foo VALUES ('lisa')")?;
db.preupdate_hook(Some(|action, db: &str, tbl: &str, case: &PreUpdateCase| {
assert_eq!(Action::SQLITE_DELETE, action);
assert_eq!("main", db);
assert_eq!("foo", tbl);
match case {
PreUpdateCase::Delete(accessor) => {
assert_eq!(1, accessor.get_column_count());
assert_eq!(1, accessor.get_old_row_id());
assert_eq!(0, accessor.get_query_depth());
// out of bounds access should return an error
assert!(accessor.get_old_column_value(1).is_err());
assert_eq!(
"lisa",
accessor.get_old_column_value(0).unwrap().as_str().unwrap()
);
assert_eq!(0, accessor.get_query_depth());
}
_ => panic!("wrong preupdate case"),
}
CALLED.store(true, Ordering::Relaxed);
}))?;
db.execute_batch("DELETE from foo")?;
assert!(CALLED.load(Ordering::Relaxed));
Ok(())
}
#[test]
fn test_preupdate_hook_update() -> Result<()> {
let db = Connection::open_in_memory()?;
static CALLED: AtomicBool = AtomicBool::new(false);
db.execute_batch("CREATE TABLE foo (t TEXT)")?;
db.execute_batch("INSERT INTO foo VALUES ('lisa')")?;
db.preupdate_hook(Some(|action, db: &str, tbl: &str, case: &PreUpdateCase| {
assert_eq!(Action::SQLITE_UPDATE, action);
assert_eq!("main", db);
assert_eq!("foo", tbl);
match case {
PreUpdateCase::Update {
old_value_accessor,
new_value_accessor,
} => {
assert_eq!(1, old_value_accessor.get_column_count());
assert_eq!(1, old_value_accessor.get_old_row_id());
assert_eq!(0, old_value_accessor.get_query_depth());
// out of bounds access should return an error
assert!(old_value_accessor.get_old_column_value(1).is_err());
assert_eq!(
"lisa",
old_value_accessor
.get_old_column_value(0)
.unwrap()
.as_str()
.unwrap()
);
assert_eq!(0, old_value_accessor.get_query_depth());
assert_eq!(1, new_value_accessor.get_column_count());
assert_eq!(1, new_value_accessor.get_new_row_id());
assert_eq!(0, new_value_accessor.get_query_depth());
// out of bounds access should return an error
assert!(new_value_accessor.get_new_column_value(1).is_err());
assert_eq!(
"janice",
new_value_accessor
.get_new_column_value(0)
.unwrap()
.as_str()
.unwrap()
);
assert_eq!(0, new_value_accessor.get_query_depth());
}
_ => panic!("wrong preupdate case"),
}
CALLED.store(true, Ordering::Relaxed);
}))?;
db.execute_batch("UPDATE foo SET t = 'janice'")?;
assert!(CALLED.load(Ordering::Relaxed));
Ok(())
}
}

View File

@@ -1,446 +0,0 @@
use std::ffi::{c_char, c_int, CStr};
#[cfg(feature = "load_extension")]
use std::path::Path;
use std::ptr;
use std::str;
use std::sync::{Arc, Mutex};
use super::ffi;
use super::{Connection, InterruptHandle, Name, OpenFlags, PrepFlags, Result};
use crate::error::{decode_result_raw, error_from_handle, error_with_offset, Error};
use crate::raw_statement::RawStatement;
use crate::statement::Statement;
use crate::version_number;
pub struct InnerConnection {
pub db: *mut ffi::sqlite3,
// It's unsafe to call `sqlite3_close` while another thread is performing
// a `sqlite3_interrupt`, and vice versa, so we take this mutex during
// those functions. This protects a copy of the `db` pointer (which is
// cleared on closing), however the main copy, `db`, is unprotected.
// Otherwise, a long-running query would prevent calling interrupt, as
// interrupt would only acquire the lock after the query's completion.
interrupt_lock: Arc<Mutex<*mut ffi::sqlite3>>,
#[cfg(feature = "hooks")]
pub commit_hook: Option<Box<dyn FnMut() -> bool + Send>>,
#[cfg(feature = "hooks")]
pub rollback_hook: Option<Box<dyn FnMut() + Send>>,
#[cfg(feature = "hooks")]
#[expect(clippy::type_complexity)]
pub update_hook: Option<Box<dyn FnMut(crate::hooks::Action, &str, &str, i64) + Send>>,
#[cfg(feature = "hooks")]
pub progress_handler: Option<Box<dyn FnMut() -> bool + Send>>,
#[cfg(feature = "hooks")]
pub authorizer: Option<crate::hooks::BoxedAuthorizer>,
#[cfg(feature = "preupdate_hook")]
#[expect(clippy::type_complexity)]
pub preupdate_hook: Option<
Box<dyn FnMut(crate::hooks::Action, &str, &str, &crate::hooks::PreUpdateCase) + Send>,
>,
owned: bool,
}
unsafe impl Send for InnerConnection {}
impl InnerConnection {
#[expect(clippy::arc_with_non_send_sync)] // See unsafe impl Send / Sync for InterruptHandle
#[inline]
pub unsafe fn new(db: *mut ffi::sqlite3, owned: bool) -> Self {
Self {
db,
interrupt_lock: Arc::new(Mutex::new(if owned { db } else { ptr::null_mut() })),
#[cfg(feature = "hooks")]
commit_hook: None,
#[cfg(feature = "hooks")]
rollback_hook: None,
#[cfg(feature = "hooks")]
update_hook: None,
#[cfg(feature = "hooks")]
progress_handler: None,
#[cfg(feature = "hooks")]
authorizer: None,
#[cfg(feature = "preupdate_hook")]
preupdate_hook: None,
owned,
}
}
pub fn open_with_flags(
c_path: &CStr,
mut flags: OpenFlags,
vfs: Option<&CStr>,
) -> Result<Self> {
ensure_safe_sqlite_threading_mode()?;
let z_vfs = match vfs {
Some(c_vfs) => c_vfs.as_ptr(),
None => ptr::null(),
};
// turn on extended results code before opening database to have a better diagnostic if a failure happens
let exrescode = if version_number() >= 3_037_000 {
flags |= OpenFlags::SQLITE_OPEN_EXRESCODE;
true
} else {
false // flag SQLITE_OPEN_EXRESCODE is ignored by SQLite version < 3.37.0
};
unsafe {
let mut db: *mut ffi::sqlite3 = ptr::null_mut();
let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), z_vfs);
if r != ffi::SQLITE_OK {
let e = if db.is_null() {
err!(r, "{}", c_path.to_string_lossy())
} else {
let mut e = error_from_handle(db, r);
if let Error::SqliteFailure(
ffi::Error {
code: ffi::ErrorCode::CannotOpen,
..
},
Some(msg),
) = e
{
e = err!(r, "{msg}: {}", c_path.to_string_lossy());
}
ffi::sqlite3_close(db);
e
};
return Err(e);
}
// attempt to turn on extended results code; don't fail if we can't.
if !exrescode {
ffi::sqlite3_extended_result_codes(db, 1);
}
let r = ffi::sqlite3_busy_timeout(db, 5000);
if r != ffi::SQLITE_OK {
let e = error_from_handle(db, r);
ffi::sqlite3_close(db);
return Err(e);
}
Ok(Self::new(db, true))
}
}
#[inline]
pub fn db(&self) -> *mut ffi::sqlite3 {
self.db
}
#[inline]
pub fn decode_result(&self, code: c_int) -> Result<()> {
unsafe { decode_result_raw(self.db(), code) }
}
pub fn close(&mut self) -> Result<()> {
if self.db.is_null() {
return Ok(());
}
if self.owned {
self.remove_hooks();
self.remove_preupdate_hook();
}
let mut shared_handle = self.interrupt_lock.lock().unwrap();
assert!(
!self.owned || !shared_handle.is_null(),
"Bug: Somehow interrupt_lock was cleared before the DB was closed"
);
if !self.owned {
self.db = ptr::null_mut();
return Ok(());
}
unsafe {
let r = ffi::sqlite3_close(self.db);
// Need to use _raw because _guard has a reference out, and
// decode_result takes &mut self.
let r = decode_result_raw(self.db, r);
if r.is_ok() {
*shared_handle = ptr::null_mut();
self.db = ptr::null_mut();
}
r
}
}
#[inline]
pub fn get_interrupt_handle(&self) -> InterruptHandle {
InterruptHandle {
db_lock: Arc::clone(&self.interrupt_lock),
}
}
#[inline]
#[cfg(feature = "load_extension")]
pub unsafe fn enable_load_extension(&mut self, onoff: c_int) -> Result<()> {
let r = ffi::sqlite3_enable_load_extension(self.db, onoff);
self.decode_result(r)
}
#[cfg(feature = "load_extension")]
pub unsafe fn load_extension<N: Name>(
&self,
dylib_path: &Path,
entry_point: Option<N>,
) -> Result<()> {
let dylib_str = super::path_to_cstring(dylib_path)?;
let mut errmsg: *mut c_char = ptr::null_mut();
let cs = entry_point.as_ref().map(N::as_cstr).transpose()?;
let c_entry = cs.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null());
let r = ffi::sqlite3_load_extension(self.db, dylib_str.as_ptr(), c_entry, &mut errmsg);
if r == ffi::SQLITE_OK {
Ok(())
} else {
let message = super::errmsg_to_string(errmsg);
ffi::sqlite3_free(errmsg.cast::<std::ffi::c_void>());
Err(crate::error::error_from_sqlite_code(r, Some(message)))
}
}
#[inline]
pub fn last_insert_rowid(&self) -> i64 {
unsafe { ffi::sqlite3_last_insert_rowid(self.db()) }
}
pub fn prepare<'a>(
&mut self,
conn: &'a Connection,
sql: &str,
flags: PrepFlags,
) -> Result<(Statement<'a>, usize)> {
let mut c_stmt: *mut ffi::sqlite3_stmt = ptr::null_mut();
let Ok(len) = c_int::try_from(sql.len()) else {
return Err(err!(ffi::SQLITE_TOOBIG));
};
let c_sql = sql.as_bytes().as_ptr().cast::<c_char>();
let mut c_tail: *const c_char = ptr::null();
#[cfg(not(feature = "unlock_notify"))]
let r = unsafe {
ffi::sqlite3_prepare_v3(
self.db(),
c_sql,
len,
flags.bits(),
&mut c_stmt,
&mut c_tail,
)
};
#[cfg(feature = "unlock_notify")]
let r = unsafe {
use crate::unlock_notify;
let mut rc;
loop {
rc = ffi::sqlite3_prepare_v3(
self.db(),
c_sql,
len,
flags.bits(),
&mut c_stmt,
&mut c_tail,
);
if !unlock_notify::is_locked(self.db, rc) {
break;
}
rc = unlock_notify::wait_for_unlock_notify(self.db);
if rc != ffi::SQLITE_OK {
break;
}
}
rc
};
// If there is an error, *ppStmt is set to NULL.
if r != ffi::SQLITE_OK {
return Err(unsafe { error_with_offset(self.db, r, sql) });
}
// If the input text contains no SQL (if the input is an empty string or a
// comment) then *ppStmt is set to NULL.
let tail = if c_tail.is_null() {
0
} else {
let n = (c_tail as isize) - (c_sql as isize);
if n <= 0 || n >= len as isize {
0
} else {
n as usize
}
};
Ok((
Statement::new(conn, unsafe { RawStatement::new(c_stmt) }),
tail,
))
}
#[inline]
pub fn changes(&self) -> u64 {
#[cfg(not(feature = "modern_sqlite"))]
unsafe {
ffi::sqlite3_changes(self.db()) as u64
}
#[cfg(feature = "modern_sqlite")] // 3.37.0
unsafe {
ffi::sqlite3_changes64(self.db()) as u64
}
}
#[inline]
pub fn total_changes(&self) -> u64 {
#[cfg(not(feature = "modern_sqlite"))]
unsafe {
ffi::sqlite3_total_changes(self.db()) as u64
}
#[cfg(feature = "modern_sqlite")] // 3.37.0
unsafe {
ffi::sqlite3_total_changes64(self.db()) as u64
}
}
#[inline]
pub fn is_autocommit(&self) -> bool {
unsafe { get_autocommit(self.db()) }
}
pub fn is_busy(&self) -> bool {
let db = self.db();
unsafe {
let mut stmt = ffi::sqlite3_next_stmt(db, ptr::null_mut());
while !stmt.is_null() {
if ffi::sqlite3_stmt_busy(stmt) != 0 {
return true;
}
stmt = ffi::sqlite3_next_stmt(db, stmt);
}
}
false
}
pub fn cache_flush(&mut self) -> Result<()> {
crate::error::check(unsafe { ffi::sqlite3_db_cacheflush(self.db()) })
}
#[cfg(not(feature = "hooks"))]
#[inline]
fn remove_hooks(&mut self) {}
#[cfg(not(feature = "preupdate_hook"))]
#[inline]
fn remove_preupdate_hook(&mut self) {}
pub fn db_readonly<N: Name>(&self, db_name: N) -> Result<bool> {
let name = db_name.as_cstr()?;
let r = unsafe { ffi::sqlite3_db_readonly(self.db, name.as_ptr()) };
match r {
0 => Ok(false),
1 => Ok(true),
-1 => Err(err!(
ffi::SQLITE_MISUSE,
"{db_name:?} is not the name of a database"
)),
_ => Err(err!(r, "Unexpected result")),
}
}
#[cfg(feature = "modern_sqlite")] // 3.37.0
pub fn txn_state<N: Name>(
&self,
db_name: Option<N>,
) -> Result<super::transaction::TransactionState> {
let cs = db_name.as_ref().map(N::as_cstr).transpose()?;
let name = cs.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null());
let r = unsafe { ffi::sqlite3_txn_state(self.db, name) };
match r {
0 => Ok(super::transaction::TransactionState::None),
1 => Ok(super::transaction::TransactionState::Read),
2 => Ok(super::transaction::TransactionState::Write),
-1 => Err(err!(
ffi::SQLITE_MISUSE,
"{db_name:?} is not the name of a valid schema"
)),
_ => Err(err!(r, "Unexpected result")),
}
}
#[inline]
pub fn release_memory(&self) -> Result<()> {
self.decode_result(unsafe { ffi::sqlite3_db_release_memory(self.db) })
}
#[cfg(feature = "modern_sqlite")] // 3.41.0
pub fn is_interrupted(&self) -> bool {
unsafe { ffi::sqlite3_is_interrupted(self.db) == 1 }
}
#[cfg(any(feature = "hooks", feature = "preupdate_hook"))]
pub fn check_owned(&self) -> Result<()> {
if !self.owned {
return Err(err!(ffi::SQLITE_MISUSE, "Connection is not owned"));
}
Ok(())
}
}
#[inline]
pub(crate) unsafe fn get_autocommit(ptr: *mut ffi::sqlite3) -> bool {
ffi::sqlite3_get_autocommit(ptr) != 0
}
#[inline]
pub(crate) unsafe fn db_filename<N: Name>(
_: std::marker::PhantomData<&()>,
ptr: *mut ffi::sqlite3,
db_name: N,
) -> Option<&str> {
let db_name = db_name.as_cstr().unwrap();
let db_filename = ffi::sqlite3_db_filename(ptr, db_name.as_ptr());
if db_filename.is_null() {
None
} else {
CStr::from_ptr(db_filename).to_str().ok()
}
}
impl Drop for InnerConnection {
#[expect(unused_must_use)]
#[inline]
fn drop(&mut self) {
self.close();
}
}
// threading mode checks are not necessary (and do not work) on target
// platforms that do not have threading (such as webassembly)
#[cfg(target_arch = "wasm32")]
fn ensure_safe_sqlite_threading_mode() -> Result<()> {
Ok(())
}
#[cfg(not(any(target_arch = "wasm32")))]
fn ensure_safe_sqlite_threading_mode() -> Result<()> {
// Ensure SQLite was compiled in threadsafe mode.
if unsafe { ffi::sqlite3_threadsafe() == 0 } {
return Err(Error::SqliteSingleThreadedMode);
}
// Now we know SQLite is _capable_ of being in Multi-thread of Serialized mode,
// but it's possible someone configured it to be in Single-thread mode
// before calling into us. That would mean we're exposing an unsafe API via
// a safe one (in Rust terminology).
//
// We can ask SQLite for a mutex and check for
// the magic value 8. This isn't documented, but it's what SQLite
// returns for its mutex allocation function in Single-thread mode.
const SQLITE_SINGLETHREADED_MUTEX_MAGIC: usize = 8;
let is_singlethreaded = unsafe {
let mutex_ptr = ffi::sqlite3_mutex_alloc(0);
let is_singlethreaded = mutex_ptr as usize == SQLITE_SINGLETHREADED_MUTEX_MAGIC;
ffi::sqlite3_mutex_free(mutex_ptr);
is_singlethreaded
};
if is_singlethreaded {
Err(Error::SqliteSingleThreadedMode)
} else {
Ok(())
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,182 +0,0 @@
//! Run-Time Limits
use crate::{ffi, Connection, Result};
use std::ffi::c_int;
/// Run-Time limit categories, for use with [`Connection::limit`] and
/// [`Connection::set_limit`].
///
/// See the official documentation for more information:
/// - <https://www.sqlite.org/c3ref/c_limit_attached.html>
/// - <https://www.sqlite.org/limits.html>
#[derive(Copy, Clone, Debug)]
#[repr(i32)]
#[non_exhaustive]
#[expect(non_camel_case_types)]
pub enum Limit {
/// The maximum size of any string or BLOB or table row, in bytes.
SQLITE_LIMIT_LENGTH = ffi::SQLITE_LIMIT_LENGTH,
/// The maximum length of an SQL statement, in bytes.
SQLITE_LIMIT_SQL_LENGTH = ffi::SQLITE_LIMIT_SQL_LENGTH,
/// The maximum number of columns in a table definition or in the result set
/// of a SELECT or the maximum number of columns in an index or in an
/// ORDER BY or GROUP BY clause.
SQLITE_LIMIT_COLUMN = ffi::SQLITE_LIMIT_COLUMN,
/// The maximum depth of the parse tree on any expression.
SQLITE_LIMIT_EXPR_DEPTH = ffi::SQLITE_LIMIT_EXPR_DEPTH,
/// The maximum number of terms in a compound SELECT statement.
SQLITE_LIMIT_COMPOUND_SELECT = ffi::SQLITE_LIMIT_COMPOUND_SELECT,
/// The maximum number of instructions in a virtual machine program used to
/// implement an SQL statement.
SQLITE_LIMIT_VDBE_OP = ffi::SQLITE_LIMIT_VDBE_OP,
/// The maximum number of arguments on a function.
SQLITE_LIMIT_FUNCTION_ARG = ffi::SQLITE_LIMIT_FUNCTION_ARG,
/// The maximum number of attached databases.
SQLITE_LIMIT_ATTACHED = ffi::SQLITE_LIMIT_ATTACHED,
/// The maximum length of the pattern argument to the LIKE or GLOB
/// operators.
SQLITE_LIMIT_LIKE_PATTERN_LENGTH = ffi::SQLITE_LIMIT_LIKE_PATTERN_LENGTH,
/// The maximum index number of any parameter in an SQL statement.
SQLITE_LIMIT_VARIABLE_NUMBER = ffi::SQLITE_LIMIT_VARIABLE_NUMBER,
/// The maximum depth of recursion for triggers.
SQLITE_LIMIT_TRIGGER_DEPTH = ffi::SQLITE_LIMIT_TRIGGER_DEPTH,
/// The maximum number of auxiliary worker threads that a single prepared
/// statement may start.
SQLITE_LIMIT_WORKER_THREADS = ffi::SQLITE_LIMIT_WORKER_THREADS,
/// Only used for testing
#[cfg(test)]
INVALID = -1,
}
impl Connection {
/// Returns the current value of a [`Limit`].
#[inline]
pub fn limit(&self, limit: Limit) -> Result<i32> {
let c = self.db.borrow();
let rc = unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, -1) };
if rc < 0 {
return Err(err!(ffi::SQLITE_RANGE, "{limit:?} is invalid"));
}
Ok(rc)
}
/// Changes the [`Limit`] to `new_val`, returning the prior
/// value of the limit.
#[inline]
pub fn set_limit(&self, limit: Limit, new_val: i32) -> Result<i32> {
if new_val < 0 {
return Err(err!(ffi::SQLITE_RANGE, "{new_val} is invalid"));
}
let c = self.db.borrow_mut();
let rc = unsafe { ffi::sqlite3_limit(c.db(), limit as c_int, new_val) };
if rc < 0 {
return Err(err!(ffi::SQLITE_RANGE, "{limit:?} is invalid"));
}
Ok(rc)
}
}
#[cfg(test)]
mod test {
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
use super::*;
use crate::Result;
#[test]
fn test_limit_values() {
assert_eq!(Limit::SQLITE_LIMIT_LENGTH as i32, ffi::SQLITE_LIMIT_LENGTH,);
assert_eq!(
Limit::SQLITE_LIMIT_SQL_LENGTH as i32,
ffi::SQLITE_LIMIT_SQL_LENGTH,
);
assert_eq!(Limit::SQLITE_LIMIT_COLUMN as i32, ffi::SQLITE_LIMIT_COLUMN,);
assert_eq!(
Limit::SQLITE_LIMIT_EXPR_DEPTH as i32,
ffi::SQLITE_LIMIT_EXPR_DEPTH,
);
assert_eq!(
Limit::SQLITE_LIMIT_COMPOUND_SELECT as i32,
ffi::SQLITE_LIMIT_COMPOUND_SELECT,
);
assert_eq!(
Limit::SQLITE_LIMIT_VDBE_OP as i32,
ffi::SQLITE_LIMIT_VDBE_OP,
);
assert_eq!(
Limit::SQLITE_LIMIT_FUNCTION_ARG as i32,
ffi::SQLITE_LIMIT_FUNCTION_ARG,
);
assert_eq!(
Limit::SQLITE_LIMIT_ATTACHED as i32,
ffi::SQLITE_LIMIT_ATTACHED,
);
assert_eq!(
Limit::SQLITE_LIMIT_LIKE_PATTERN_LENGTH as i32,
ffi::SQLITE_LIMIT_LIKE_PATTERN_LENGTH,
);
assert_eq!(
Limit::SQLITE_LIMIT_VARIABLE_NUMBER as i32,
ffi::SQLITE_LIMIT_VARIABLE_NUMBER,
);
assert_eq!(
Limit::SQLITE_LIMIT_TRIGGER_DEPTH as i32,
ffi::SQLITE_LIMIT_TRIGGER_DEPTH,
);
assert_eq!(
Limit::SQLITE_LIMIT_WORKER_THREADS as i32,
ffi::SQLITE_LIMIT_WORKER_THREADS,
);
}
#[test]
fn test_limit() -> Result<()> {
let db = Connection::open_in_memory()?;
db.set_limit(Limit::SQLITE_LIMIT_LENGTH, 1024)?;
assert_eq!(1024, db.limit(Limit::SQLITE_LIMIT_LENGTH)?);
db.set_limit(Limit::SQLITE_LIMIT_SQL_LENGTH, 1024)?;
assert_eq!(1024, db.limit(Limit::SQLITE_LIMIT_SQL_LENGTH)?);
db.set_limit(Limit::SQLITE_LIMIT_COLUMN, 64)?;
assert_eq!(64, db.limit(Limit::SQLITE_LIMIT_COLUMN)?);
db.set_limit(Limit::SQLITE_LIMIT_EXPR_DEPTH, 256)?;
assert_eq!(256, db.limit(Limit::SQLITE_LIMIT_EXPR_DEPTH)?);
db.set_limit(Limit::SQLITE_LIMIT_COMPOUND_SELECT, 32)?;
assert_eq!(32, db.limit(Limit::SQLITE_LIMIT_COMPOUND_SELECT)?);
db.set_limit(Limit::SQLITE_LIMIT_FUNCTION_ARG, 32)?;
assert_eq!(32, db.limit(Limit::SQLITE_LIMIT_FUNCTION_ARG)?);
db.set_limit(Limit::SQLITE_LIMIT_ATTACHED, 2)?;
assert_eq!(2, db.limit(Limit::SQLITE_LIMIT_ATTACHED)?);
db.set_limit(Limit::SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 128)?;
assert_eq!(128, db.limit(Limit::SQLITE_LIMIT_LIKE_PATTERN_LENGTH)?);
db.set_limit(Limit::SQLITE_LIMIT_VARIABLE_NUMBER, 99)?;
assert_eq!(99, db.limit(Limit::SQLITE_LIMIT_VARIABLE_NUMBER)?);
db.set_limit(Limit::SQLITE_LIMIT_TRIGGER_DEPTH, 32)?;
assert_eq!(32, db.limit(Limit::SQLITE_LIMIT_TRIGGER_DEPTH)?);
db.set_limit(Limit::SQLITE_LIMIT_WORKER_THREADS, 2)?;
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
assert_eq!(2, db.limit(Limit::SQLITE_LIMIT_WORKER_THREADS)?);
// wasm build with DSQLITE_THREADSAFE=0, so limit not working
// see <https://sqlite.org/threadsafe.html>
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
assert_eq!(0, db.limit(Limit::SQLITE_LIMIT_WORKER_THREADS)?);
assert!(db
.set_limit(Limit::SQLITE_LIMIT_WORKER_THREADS, -1)
.is_err());
assert!(db.set_limit(Limit::INVALID, 0).is_err());
assert!(db.limit(Limit::INVALID).is_err());
Ok(())
}
}

Some files were not shown because too many files have changed in this diff Show More