beatrina 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/LICENSE +92 -0
  2. package/NOTICES +72 -0
  3. package/README.md +124 -0
  4. package/bin/beatrina.mjs +223 -0
  5. package/bin/cli.mjs +80 -0
  6. package/bin/failsafe.mjs +74 -0
  7. package/bin/identity.mjs +45 -0
  8. package/bin/prova-post.mjs +51 -0
  9. package/bin/sessions.mjs +95 -0
  10. package/bin/shortcut.mjs +151 -0
  11. package/bin/update-check.mjs +55 -0
  12. package/bin/upgrade.mjs +76 -0
  13. package/build-info.json +1 -0
  14. package/carmar_V0.8.6.html +1310 -0
  15. package/check/acceptance.mjs +278 -0
  16. package/check/session.mjs +215 -0
  17. package/engines/js/document-exec.mjs +82 -0
  18. package/engines/js/persist.mjs +214 -0
  19. package/engines/js/worker.mjs +424 -0
  20. package/engines/python/adapter.py +577 -0
  21. package/engines/python/analyze.py +814 -0
  22. package/engines/python/bootstrap.py +309 -0
  23. package/engines/python/dataview.py +735 -0
  24. package/engines/python/debugger.py +346 -0
  25. package/engines/python/document_exec.py +158 -0
  26. package/engines/python/engine.json +28 -0
  27. package/engines/python/handoff.py +118 -0
  28. package/engines/python/worker.py +564 -0
  29. package/engines/r/engine.json +25 -0
  30. package/engines/r/handoff.R +92 -0
  31. package/failsafe/ai-policy.R +255 -0
  32. package/failsafe/ai-store.R +373 -0
  33. package/failsafe/cite.R +418 -0
  34. package/failsafe/journal.R +684 -0
  35. package/failsafe/plugins.R +809 -0
  36. package/failsafe/serve.R +5500 -0
  37. package/host/ai-policy.mjs +218 -0
  38. package/host/deployment.mjs +160 -0
  39. package/host/engine-js.mjs +98 -0
  40. package/host/engine-pool.mjs +383 -0
  41. package/host/engine-python.mjs +228 -0
  42. package/host/engine-r.mjs +206 -0
  43. package/host/engine-stdio.mjs +401 -0
  44. package/host/journal-store.mjs +749 -0
  45. package/host/main.mjs +503 -0
  46. package/host/planes/README.md +41 -0
  47. package/host/planes/ai-store.mjs +327 -0
  48. package/host/planes/ai.mjs +467 -0
  49. package/host/planes/analyze.mjs +397 -0
  50. package/host/planes/cite.mjs +517 -0
  51. package/host/planes/files.mjs +0 -0
  52. package/host/planes/jobs.mjs +704 -0
  53. package/host/planes/journal.mjs +53 -0
  54. package/host/planes/latex.mjs +201 -0
  55. package/host/planes/mcp.mjs +493 -0
  56. package/host/planes/pair.mjs +325 -0
  57. package/host/planes/pipe-term.mjs +122 -0
  58. package/host/planes/plugins.mjs +112 -0
  59. package/host/planes/proc-tree.mjs +76 -0
  60. package/host/planes/sessions.mjs +434 -0
  61. package/host/planes/settings.mjs +164 -0
  62. package/host/planes/terminal.mjs +286 -0
  63. package/host/planes/test-file.mjs +80 -0
  64. package/host/planes/update.mjs +214 -0
  65. package/host/plugin-store.mjs +838 -0
  66. package/host/server.mjs +441 -0
  67. package/host/settings.mjs +379 -0
  68. package/host/update-record.mjs +59 -0
  69. package/host/user-dirs.mjs +117 -0
  70. package/host/windows-runtime.mjs +145 -0
  71. package/host/worker-plane.mjs +713 -0
  72. package/host/ws.mjs +190 -0
  73. package/kernel/analyze.R +668 -0
  74. package/kernel/deployment.R +165 -0
  75. package/kernel/examples/NOTICE.md +38 -0
  76. package/kernel/examples/tna-complete-tutorial.Rmd +210 -0
  77. package/kernel/fileio.R +656 -0
  78. package/kernel/index.html +96 -0
  79. package/kernel/job-run.R +391 -0
  80. package/kernel/jobs.R +276 -0
  81. package/kernel/kernel-protocol +1 -0
  82. package/kernel/kernel-version +1 -0
  83. package/kernel/kernel.R +671 -0
  84. package/kernel/knitr-run.R +245 -0
  85. package/kernel/latex.R +609 -0
  86. package/kernel/mcp/carmar-mcp.mjs +516 -0
  87. package/kernel/notebook-page.R +67 -0
  88. package/kernel/plugins/csl/apa/apa.csl +2273 -0
  89. package/kernel/plugins/csl/apa/plugin.json +19 -0
  90. package/kernel/plugins/csl/chicago-author-date/chicago-author-date.csl +4216 -0
  91. package/kernel/plugins/csl/chicago-author-date/plugin.json +19 -0
  92. package/kernel/plugins/csl/harvard-cite-them-right/harvard-cite-them-right.csl +316 -0
  93. package/kernel/plugins/csl/harvard-cite-them-right/plugin.json +19 -0
  94. package/kernel/plugins/csl/ieee/ieee.csl +519 -0
  95. package/kernel/plugins/csl/ieee/plugin.json +19 -0
  96. package/kernel/plugins/csl/modern-language-association/modern-language-association.csl +1184 -0
  97. package/kernel/plugins/csl/modern-language-association/plugin.json +19 -0
  98. package/kernel/plugins/csl/nature/nature.csl +189 -0
  99. package/kernel/plugins/csl/nature/plugin.json +19 -0
  100. package/kernel/plugins/latex/apa7/apa7.json +14 -0
  101. package/kernel/plugins/latex/apa7/plugin.json +19 -0
  102. package/kernel/plugins/latex/elsarticle/elsarticle.json +14 -0
  103. package/kernel/plugins/latex/elsarticle/plugin.json +19 -0
  104. package/kernel/plugins/latex/ieeetran/ieeetran.json +10 -0
  105. package/kernel/plugins/latex/ieeetran/plugin.json +19 -0
  106. package/kernel/project.R +131 -0
  107. package/kernel/settings.R +410 -0
  108. package/kernel/sniff.R +769 -0
  109. package/kernel/worker-boot.R +22 -0
  110. package/kernel/worker.R +3496 -0
  111. package/lib/agent-authoring-contract.js +547 -0
  112. package/lib/cell-kinds.js +108 -0
  113. package/lib/engine-labels.js +324 -0
  114. package/package.json +32 -0
@@ -0,0 +1,245 @@
1
+ # Interactive notebook execution using knitr's PUBLIC cache and option APIs.
2
+ # Sourced into worker.R's private environment. The native evaluator retains
3
+ # breakpoints, interactive input, rich tables, progress and interrupt handling.
4
+ # Only the notebook exec envelope uses this adapter; console exec is unchanged.
5
+ KNIT_DOCS <- new.env(parent = emptyenv())
6
+ KNIT_BASE <- NULL
7
+ KNIT_CAPTURE <- NULL
8
+ KNIT_PROTOCOL <- FALSE
9
+ KNIT_MARKER <- "CARMAR_KNIT_OUTPUT_V1:"
10
+
11
+ run_document_cell <- function(id, source, document, dims = NULL, srcname = NULL) {
12
+ KNIT_PROTOCOL <<- TRUE
13
+ terminal <- list(type = "done", id = id, status = "ok", message = NULL)
14
+ setup_report <- NULL
15
+ cache_hit <- FALSE
16
+ effective <- NULL
17
+ saved_hooks <- saved_options <- saved_templates <- NULL
18
+ on.exit({
19
+ KNIT_CAPTURE <<- NULL
20
+ KNIT_PROTOCOL <<- FALSE
21
+ if (!is.null(saved_hooks)) knitr::knit_hooks$restore(saved_hooks)
22
+ if (!is.null(saved_options)) knitr::opts_chunk$restore(saved_options)
23
+ if (!is.null(saved_templates)) knitr::opts_template$restore(saved_templates)
24
+ }, add = TRUE)
25
+ outcome <- tryCatch({
26
+ if (!is.list(document) || !is.character(document$id) || length(document$id) != 1L ||
27
+ !nzchar(document$id)) stop("A notebook run needs its document identity.")
28
+ for (package in c("knitr", "evaluate")) {
29
+ if (!requireNamespace(package, quietly = TRUE)) {
30
+ terminal$missingPackage <- package
31
+ stop(paste0("Notebook chunk options require ", package, ". Install it from Tools > Packages."))
32
+ }
33
+ }
34
+ saved_options <- knitr::opts_chunk$get()
35
+ saved_hooks <- knitr::knit_hooks$get()
36
+ saved_templates <- knitr::opts_template$get()
37
+ if (is.null(KNIT_BASE)) KNIT_BASE <<- saved_options
38
+ setup <- if (length(document$setup)) document$setup else NULL
39
+ if (!is.null(setup) && (!is.list(setup) || !is.character(setup$source) || length(setup$source) != 1L))
40
+ stop("Invalid setup chunk descriptor.")
41
+ previous <- KNIT_DOCS[[document$id]]
42
+ setup_key <- if (is.null(setup)) "" else setup$source
43
+ if (!is.null(previous) && identical(previous$source, setup_key) &&
44
+ identical(previous$status, "failed") && !isTRUE(document$isSetup) && !isTRUE(document$retrySetup))
45
+ stop("Setup previously failed. Fix it or explicitly run the setup chunk before running dependent chunks.")
46
+ setup_changed <- is.null(previous) || !identical(previous$source, setup_key)
47
+ opts <- if (setup_changed || is.null(previous$options)) KNIT_BASE else previous$options
48
+ knitr::opts_chunk$restore(opts)
49
+
50
+ # All protocol writes bypass evaluate's output sink. The callback captures
51
+ # only this run's result frames; debugger/input/progress stay live and are
52
+ # never cached. A native done frame is held until knitr commits its cache.
53
+ native <- function(code, options, debug_name, save_figures = TRUE) {
54
+ frames <- list()
55
+ native_done <- NULL
56
+ capture_error <- NULL
57
+ plot_n <- 0L
58
+ KNIT_CAPTURE <<- function(frame) {
59
+ if (!identical(frame$id, id)) return(TRUE)
60
+ if (!identical(frame$type, "stdout")) evaluate::flush_console()
61
+ if (identical(frame$type, "progress") && !identical(debug_name, srcname)) {
62
+ frame$phase <- "setup"
63
+ emit_wire(frame)
64
+ return(FALSE)
65
+ }
66
+ if (identical(frame$type, "done")) { native_done <<- frame; return(FALSE) }
67
+ if (!frame$type %in% c("stdout", "stderr", "stream", "plot", "dataframe", "widget", "traceback")) return(TRUE)
68
+ if (identical(frame$type, "stream") &&
69
+ ((identical(frame$kind, "warning") && identical(options$warning, FALSE)) ||
70
+ (identical(frame$kind, "message") && identical(options$message, FALSE)))) return(FALSE)
71
+ if (identical(frame$type, "stdout") && !is.null(options$comment) && nzchar(options$comment)) {
72
+ frame$text <- paste0(options$comment, " ", strsplit(frame$text, "\n", fixed = TRUE)[[1L]], collapse = "\n")
73
+ }
74
+ if (identical(frame$type, "plot") && save_figures) {
75
+ plot_n <<- plot_n + 1L
76
+ suffix <- if (identical(frame$mime, "image/svg+xml")) ".svg" else ".png"
77
+ target <- paste0(options$fig.path, options$label, "-", plot_n, suffix)
78
+ tryCatch({
79
+ dir.create(dirname(target), recursive = TRUE, showWarnings = FALSE)
80
+ writeBin(jsonlite::base64_dec(frame$data), target)
81
+ frame$path <- target
82
+ }, error = function(e) {
83
+ # Native final plot harvesting deliberately tolerates a failed
84
+ # device. Do not let that swallow a requested figure-file write
85
+ # failure and mark this run/cache successful.
86
+ capture_error <<- paste0("Could not write figure ", target, ": ", conditionMessage(e))
87
+ })
88
+ }
89
+ frames[[length(frames) + 1L]] <<- frame
90
+ # Re-emit the transformed copy, not the original frame.
91
+ if (!identical(options$include, FALSE) || identical(frame$type, "traceback")) emit_wire(frame)
92
+ FALSE
93
+ }
94
+ on.exit(KNIT_CAPTURE <<- NULL, add = TRUE)
95
+ dpi <- options$dpi
96
+ width <- options$fig.width
97
+ height <- options$fig.height
98
+ valid_dimension <- function(x) is.numeric(x) && length(x) == 1L && is.finite(x) && x > 0
99
+ if (!all(vapply(list(dpi, width, height), valid_dimension, logical(1))))
100
+ stop("dpi, fig.width and fig.height must each be one positive finite number.")
101
+ if (width * dpi > 20000 || height * dpi > 20000 || width * height * dpi^2 > 1e8)
102
+ stop("The requested figure is too large (maximum 20,000 pixels per edge / 100 megapixels). Reduce its size or dpi.")
103
+ native_dims <- list(width = width * dpi, height = height * dpi, res = dpi,
104
+ format = if (identical(options$dev, "svg")) "svg" else "png")
105
+ runner <- function() {
106
+ RUN_STATE$capture_depth <- sink.number()
107
+ on.exit(RUN_STATE$capture_depth <- NULL, add = TRUE)
108
+ run_cell(id, code, native_dims, debug_name)
109
+ }
110
+ evaluate::evaluate("runner()", envir = environment(), new_device = FALSE,
111
+ stop_on_error = 2L, output_handler = evaluate::new_output_handler(
112
+ text = function(text) { emit(list(type = "stdout", id = id, text = sub("\n$", "", text))); invisible(NULL) }))
113
+ if (is.null(native_done)) stop("The native evaluator did not finish this chunk.")
114
+ if (!identical(native_done$status, "ok")) {
115
+ terminal <<- native_done
116
+ stop(native_done$message %||% paste("Chunk", native_done$status), call. = FALSE)
117
+ }
118
+ if (!is.null(capture_error)) stop(capture_error, call. = FALSE)
119
+ frames
120
+ }
121
+
122
+ if (!is.null(setup) && (setup_changed || isTRUE(document$isSetup) || identical(previous$status, "failed"))) {
123
+ # Invalidate BEFORE executing: a failed partial setup must never leave a
124
+ # previous Ready snapshot available to dependent chunks.
125
+ KNIT_DOCS[[document$id]] <- NULL
126
+ setup_report <- list(source = setup$source, srcname = setup$srcname, status = "running")
127
+ emit(list(type = "setup", id = id, source = setup$source, srcname = setup$srcname, status = "running"))
128
+ setup_options <- modifyList(KNIT_BASE, list(cache = FALSE, include = FALSE, label = "setup"))
129
+ native(setup$source, setup_options, setup$srcname, save_figures = FALSE)
130
+ opts <- knitr::opts_chunk$get()
131
+ KNIT_DOCS[[document$id]] <- list(source = setup_key, options = opts)
132
+ setup_report$status <- "ok"
133
+ } else if (is.null(setup)) {
134
+ KNIT_DOCS[[document$id]] <- list(source = "", options = opts)
135
+ }
136
+ if (!isTRUE(document$isSetup)) {
137
+ label <- document$label
138
+ if (!is.character(label) || length(label) != 1L || !grepl("^[A-Za-z0-9_.-]+$", label))
139
+ stop("Use a unique chunk label containing letters, numbers, dots, hyphens or underscores.")
140
+ knitr::opts_chunk$restore(opts)
141
+ chunk_options <- document$options %||% list()
142
+ expressions <- document$expressions %||% list()
143
+ if (!is.list(chunk_options) || !is.list(expressions)) stop("Invalid chunk options.")
144
+ # Parsed expression source is evaluated only at Run, in the real R
145
+ # environment, just as a knitr header is. Menu opening never evaluates R.
146
+ for (key in names(expressions)) chunk_options[key] <- list(eval(parse(text = expressions[[key]]), globalenv()))
147
+ for (key in names(chunk_options)) if (grepl("-", key, fixed = TRUE)) {
148
+ alias <- gsub("-", ".", key, fixed = TRUE)
149
+ if (!alias %in% names(chunk_options)) chunk_options[alias] <- chunk_options[key]
150
+ chunk_options[[key]] <- NULL
151
+ }
152
+ # Older hand-authored notebooks stored device dimensions separately
153
+ # from chunk headers. Preserve those until a document setup or explicit
154
+ # figure option takes ownership; do not reset existing plots to 7 x 7.
155
+ if (is.null(setup) && is.list(dims) && is.numeric(dims$res) && length(dims$res) == 1L && is.finite(dims$res) && dims$res > 0) {
156
+ legacy <- list(fig.width = dims$width / dims$res, fig.height = dims$height / dims$res, dpi = dims$res)
157
+ for (key in names(legacy)) if (is.null(chunk_options[[key]]) && identical(opts[[key]], KNIT_BASE[[key]]) && length(legacy[[key]]) == 1L)
158
+ chunk_options[[key]] <- legacy[[key]]
159
+ }
160
+ chunk_options$label <- label
161
+ # Errors/interruption never become successful cached results.
162
+ chunk_options$error <- FALSE
163
+ chunk_options$engine <- "R"
164
+ chunk_options$carmar.adapter <- 1L
165
+ chunk_options$carmar.document <- if (is.character(document$cacheId) && length(document$cacheId) == 1L && nzchar(document$cacheId)) document$cacheId else document$id
166
+ chunk_options$carmar.setup <- setup_key
167
+ chunk_options$carmar.cwd <- getwd()
168
+ # Selection runs are native, uncached, but still inherit setup defaults.
169
+ if (isTRUE(document$selection)) chunk_options$cache <- FALSE
170
+ if (!is.null(dims$format) && identical(dims$format, "svg")) chunk_options$dev <- "svg"
171
+ # Local options are a public knitr template, not opts_chunk globals.
172
+ # Code reading opts_chunk sees document defaults; opts_current sees its
173
+ # effective local options. A local override never leaks to the next cell.
174
+ knitr::opts_template$set(.carmar_interactive = chunk_options)
175
+ merged <- modifyList(opts, chunk_options, keep.null = TRUE)
176
+ if (!is.language(merged$eval) && (!is.logical(merged$eval) || length(merged$eval) != 1L || is.na(merged$eval)))
177
+ stop("Interactive eval must be TRUE or FALSE. Use a full knitr render for expression-index selections.")
178
+ requested_cache <- merged$cache
179
+ if (!isTRUE(requested_cache) && !identical(requested_cache, FALSE) && !identical(requested_cache, 3) && !identical(requested_cache, 3L))
180
+ stop("Interactive caching supports cache=TRUE, FALSE or 3. Use a full knitr render for cache levels 1 and 2.")
181
+ effective <- merged[c("warning", "message", "comment", "fig.path", "dpi", "fig.width", "fig.height", "cache", "cache.path", "include", "eval")]
182
+ collected <- list()
183
+ evaluated <- FALSE
184
+ knitr::knit_hooks$set(evaluate = function(code, envir, ...) {
185
+ evaluated <<- TRUE
186
+ current <- knitr::opts_current$get()
187
+ if (!is.logical(current$eval) || length(current$eval) != 1L || is.na(current$eval))
188
+ stop("Interactive eval must resolve to TRUE or FALSE; numeric expression selections require a full knitr render.")
189
+ effective <<- current[intersect(names(current), c("warning", "message", "comment", "fig.path", "dpi", "fig.width", "fig.height", "cache", "cache.path", "include", "eval"))]
190
+ collected <<- native(source, current, srcname)
191
+ KNIT_DOCS[[document$id]] <- list(source = setup_key, options = knitr::opts_chunk$get())
192
+ list()
193
+ }, chunk = function(x, options) {
194
+ paste0(KNIT_MARKER, jsonlite::base64_enc(charToRaw(jsonlite::toJSON(collected,
195
+ auto_unbox = TRUE, null = "null", na = "null", digits = NA))))
196
+ })
197
+ # Source stays the REAL chunk source so knitr discovers/cache-restores
198
+ # the correct R objects. The evaluator hook retains CarmaR's native path.
199
+ fence <- paste(rep("`", max(3L, max(nchar(regmatches(source, gregexpr("`+", source))[[1L]]), 0L) + 1L)), collapse = "")
200
+ input <- paste0(fence, "{r ", label, ", opts.label='.carmar_interactive'}\n", source, "\n", fence, "\n")
201
+ output <- withCallingHandlers(knitr::knit(text = input, envir = globalenv(), quiet = TRUE),
202
+ message = function(m) {
203
+ # The native traceback already names the real chunk/source. knitr's
204
+ # synthetic one-chunk wrapper would add "Quitting from :1-3", which
205
+ # is not a location the user can open.
206
+ if (grepl("^\\s*Quitting from", conditionMessage(m))) invokeRestart("muffleMessage")
207
+ })
208
+ if (!evaluated && identical(output, input)) stop("knitr did not recognize the generated R chunk fence.")
209
+ cache_hit <- !evaluated && !identical(merged$eval, FALSE)
210
+ if (!evaluated && identical(merged$eval, FALSE)) {
211
+ terminal$status <- "skipped"
212
+ terminal$message <- "eval=FALSE in the document defaults — this chunk was not evaluated."
213
+ terminal$reason <- terminal$message
214
+ }
215
+ if (cache_hit && startsWith(trimws(output), KNIT_MARKER)) {
216
+ payload <- substring(trimws(output), nchar(KNIT_MARKER) + 1L)
217
+ restored <- jsonlite::fromJSON(rawToChar(jsonlite::base64_dec(payload)), simplifyVector = FALSE)
218
+ for (frame in restored) { frame$id <- id; emit(frame) }
219
+ }
220
+ }
221
+ NULL
222
+ }, interrupt = function(e) {
223
+ terminal$status <<- "interrupted"; terminal$message <<- "Interrupted"
224
+ }, error = function(e) {
225
+ if (identical(terminal$status, "ok")) terminal$status <<- "error"
226
+ terminal$message <<- conditionMessage(e)
227
+ if (!is.null(setup_report) && identical(setup_report$status, "running")) {
228
+ setup_report$status <<- terminal$status
229
+ KNIT_DOCS[[document$id]] <- list(source = setup_report$source, status = "failed")
230
+ terminal$message <<- paste0("Setup chunk failed; dependent chunk was not run. ", terminal$message)
231
+ }
232
+ })
233
+ KNIT_CAPTURE <<- NULL
234
+ terminal$cwd <- getwd()
235
+ terminal$setup <- setup_report
236
+ terminal$cacheHit <- cache_hit
237
+ terminal$effectiveOptions <- effective
238
+ if (is.list(document)) terminal$execution <- list(
239
+ source = if (isTRUE(document$isSetup) && is.list(document$setup)) document$setup$source else source,
240
+ setupSource = if (is.list(document$setup)) document$setup$source %||% "" else "",
241
+ optionsKey = document$optionsKey,
242
+ full = !isTRUE(document$selection))
243
+ emit(terminal)
244
+ invisible(NULL)
245
+ }