Word Integration: rebuildCiteprocState wiped in resetRequest, breaking citation insertion
### Title:
`Session.prototype.resetRequest` clears `rebuildCiteprocState`, causing `TypeError: can't access property "citationItems", c is undefined` when citing after session disruption or style re-init
### Environment:
- Zotero 7.x, 8.x, 9.x, 10.0.5, 10.0.6
- Word for Windows / LibreOffice Integration
- `extensions.zotero.cite.useCiteprocRs = false` (default)
### Description:
When a word processor integration session requires its processor state to be rebuilt (e.g. after a session disruption / RPC reconnect, style re-initialization via `resetSessionStyles()`, or when an empty CSL engine is paired with an existing session cache), inserting a citation fails with:
1. **During preview in the citation dialog:**
```text
TypeError: can't access property "citationItems", c is undefined
CSL.Engine.prototype.processCitationCluster@chrome://zotero/content/xpcom/citeproc.js:7056:25
CSL.Engine.prototype.previewCitationCluster@chrome://zotero/content/xpcom/citeproc.js:6990:20
Zotero.Integration.Session.prototype.cite/previewFn<@chrome://zotero/content/xpcom/integration.js:1583:31
```
2. **Upon accepting the citation dialog:**
```text
TypeError: can't access property "properties", this.registry.citationreg.citationById[preCitation[0]] is undefined
CSL.Engine.prototype.processCitationCluster@chrome://zotero/content/xpcom/citeproc.js:7158:9
Zotero.Integration.Session.prototype._updateCitations@chrome://zotero/content/xpcom/integration.js:2297:45
```
Word displays `"An error occurred while updating your document"`.
---
### Root Cause Analysis:
1. **Rebuild Flag Mechanism:**
In `xpcom/integration.js`, whenever the CSL engine needs to repopulate existing citations into its registry, Zotero flags `session.rebuildCiteprocState = true` (e.g. in `getSession`: line 574; `setData`: line 2007; `setDocPrefs`: line 2088).
The intention is that `updateFromDocument` will restore processor state at lines 1192–1195:
```javascript
if (this.rebuildCiteprocState) {
this.restoreProcessorState();
this.rebuildCiteprocState = false;
}
```
2. **Premature Clearing in `resetRequest`:**
When `session.cite()` begins, it calls `this.updateFromDocument(FORCE_CITATIONS_FALSE)`.
The very first call in `updateFromDocument` (line 1161) is `this.resetRequest(this._doc)`.
In `Session.prototype.resetRequest(doc)` (line 1914):
```javascript
// Reloads citeproc with all citation data.
this.rebuildCiteprocState = false;
```
Because `forceUpdateAllCitations` is `0`, line 1174 is skipped.
Consequently, by line 1192 `this.rebuildCiteprocState` is already `false`, and `restoreProcessorState()` is skipped completely.
3. **Why the TypeErrors occur:**
When existing citations are already in `this.citationsByIndex`, `resetRequest` preserves them in `this.oldCitations`, so they are not flagged in `this.newIndices`.
When `previewFn` calls `this.getCiteprocLists()`, all preceding citations in the document are passed into `citationsPre`.
Citeproc looks up the first citation ID in `this.registry.citationreg.citationById`. Because `restoreProcessorState()` was skipped, `citationById` is empty. Accessing `c.citationItems` in `citeproc.js:7056` (and subsequently `.properties` in line 7158) throws the TypeErrors.
---
### Suggested Fix:
In `xpcom/integration.js`, `Session.prototype.resetRequest` should preserve any pending `rebuildCiteprocState` flag rather than wiping it:
```javascript
Zotero.Integration.Session.prototype.resetRequest = function (doc) {
this.uriMap = new Zotero.Integration.URIMap(this);
...
// Preserve rebuild flag so updateFromDocument can call restoreProcessorState()
this.rebuildCiteprocState = !!this.rebuildCiteprocState;
...
};
```
*(Line 1194 in `updateFromDocument` already clears `this.rebuildCiteprocState = false` once `restoreProcessorState()` has run).*
`Session.prototype.resetRequest` clears `rebuildCiteprocState`, causing `TypeError: can't access property "citationItems", c is undefined` when citing after session disruption or style re-init
### Environment:
- Zotero 7.x, 8.x, 9.x, 10.0.5, 10.0.6
- Word for Windows / LibreOffice Integration
- `extensions.zotero.cite.useCiteprocRs = false` (default)
### Description:
When a word processor integration session requires its processor state to be rebuilt (e.g. after a session disruption / RPC reconnect, style re-initialization via `resetSessionStyles()`, or when an empty CSL engine is paired with an existing session cache), inserting a citation fails with:
1. **During preview in the citation dialog:**
```text
TypeError: can't access property "citationItems", c is undefined
CSL.Engine.prototype.processCitationCluster@chrome://zotero/content/xpcom/citeproc.js:7056:25
CSL.Engine.prototype.previewCitationCluster@chrome://zotero/content/xpcom/citeproc.js:6990:20
Zotero.Integration.Session.prototype.cite/previewFn<@chrome://zotero/content/xpcom/integration.js:1583:31
```
2. **Upon accepting the citation dialog:**
```text
TypeError: can't access property "properties", this.registry.citationreg.citationById[preCitation[0]] is undefined
CSL.Engine.prototype.processCitationCluster@chrome://zotero/content/xpcom/citeproc.js:7158:9
Zotero.Integration.Session.prototype._updateCitations@chrome://zotero/content/xpcom/integration.js:2297:45
```
Word displays `"An error occurred while updating your document"`.
---
### Root Cause Analysis:
1. **Rebuild Flag Mechanism:**
In `xpcom/integration.js`, whenever the CSL engine needs to repopulate existing citations into its registry, Zotero flags `session.rebuildCiteprocState = true` (e.g. in `getSession`: line 574; `setData`: line 2007; `setDocPrefs`: line 2088).
The intention is that `updateFromDocument` will restore processor state at lines 1192–1195:
```javascript
if (this.rebuildCiteprocState) {
this.restoreProcessorState();
this.rebuildCiteprocState = false;
}
```
2. **Premature Clearing in `resetRequest`:**
When `session.cite()` begins, it calls `this.updateFromDocument(FORCE_CITATIONS_FALSE)`.
The very first call in `updateFromDocument` (line 1161) is `this.resetRequest(this._doc)`.
In `Session.prototype.resetRequest(doc)` (line 1914):
```javascript
// Reloads citeproc with all citation data.
this.rebuildCiteprocState = false;
```
Because `forceUpdateAllCitations` is `0`, line 1174 is skipped.
Consequently, by line 1192 `this.rebuildCiteprocState` is already `false`, and `restoreProcessorState()` is skipped completely.
3. **Why the TypeErrors occur:**
When existing citations are already in `this.citationsByIndex`, `resetRequest` preserves them in `this.oldCitations`, so they are not flagged in `this.newIndices`.
When `previewFn` calls `this.getCiteprocLists()`, all preceding citations in the document are passed into `citationsPre`.
Citeproc looks up the first citation ID in `this.registry.citationreg.citationById`. Because `restoreProcessorState()` was skipped, `citationById` is empty. Accessing `c.citationItems` in `citeproc.js:7056` (and subsequently `.properties` in line 7158) throws the TypeErrors.
---
### Suggested Fix:
In `xpcom/integration.js`, `Session.prototype.resetRequest` should preserve any pending `rebuildCiteprocState` flag rather than wiping it:
```javascript
Zotero.Integration.Session.prototype.resetRequest = function (doc) {
this.uriMap = new Zotero.Integration.URIMap(this);
...
// Preserve rebuild flag so updateFromDocument can call restoreProcessorState()
this.rebuildCiteprocState = !!this.rebuildCiteprocState;
...
};
```
*(Line 1194 in `updateFromDocument` already clears `this.rebuildCiteprocState = false` once `restoreProcessorState()` has run).*
Upgrade Storage
Since the error message didn't really give any useful info, I enabled logging in Zotero and repeated the steps. The error reproduced.
Since I have a Gemini Pro sub, I fed Gemini this log + my error description. In boost mode and after a few follow-up questions by myself, it found the root cause. The AI patched the error for me via one of my plugins. After that, the error didn't occur anymore. The AI said the error probably happened on my end due to a connection interruption. It also gave me a way to still trigger the error though. To make sure it wasn't caused by installed plugins, I started Zotero in troubleshooting mode and repeated the following steps:
1. Created a Word doc and added at least one citation with Zotero.
2. Opened Zotero's settings and downloaded a citation style.
3. Then switched back to Word, clicked Add/Edit Citation, and confirmed the citation. That's when the error message appeared: "Ein Fehler ist beim Update Ihres Dokuments aufgetreten. Möchten Sie die Anleitung zur Fehlerbehebung ansehen?"
I saved the log. If it helps, I can send it over.